blob: d7f425698a4a1d15e763a1a262a2a2f75f55a052 [file] [log] [blame]
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001/*
2 * kernel/lockdep.c
3 *
4 * Runtime locking correctness validator
5 *
6 * Started by Ingo Molnar:
7 *
Peter Zijlstra4b32d0a2007-07-19 01:48:59 -07008 * Copyright (C) 2006,2007 Red Hat, Inc., Ingo Molnar <mingo@redhat.com>
Peter Zijlstra90eec102015-11-16 11:08:45 +01009 * Copyright (C) 2007 Red Hat, Inc., Peter Zijlstra
Ingo Molnarfbb9ce952006-07-03 00:24:50 -070010 *
11 * this code maps all the lock dependencies as they occur in a live kernel
12 * and will warn about the following classes of locking bugs:
13 *
14 * - lock inversion scenarios
15 * - circular lock dependencies
16 * - hardirq/softirq safe/unsafe locking bugs
17 *
18 * Bugs are reported even if the current locking scenario does not cause
19 * any deadlock at this point.
20 *
21 * I.e. if anytime in the past two locks were taken in a different order,
22 * even if it happened for another task, even if those were different
23 * locks (but of the same class as this lock), this code will detect it.
24 *
25 * Thanks to Arjan van de Ven for coming up with the initial idea of
26 * mapping lock dependencies runtime.
27 */
Steven Rostedta5e25882008-12-02 15:34:05 -050028#define DISABLE_BRANCH_PROFILING
Ingo Molnarfbb9ce952006-07-03 00:24:50 -070029#include <linux/mutex.h>
30#include <linux/sched.h>
31#include <linux/delay.h>
32#include <linux/module.h>
33#include <linux/proc_fs.h>
34#include <linux/seq_file.h>
35#include <linux/spinlock.h>
36#include <linux/kallsyms.h>
37#include <linux/interrupt.h>
38#include <linux/stacktrace.h>
39#include <linux/debug_locks.h>
40#include <linux/irqflags.h>
Dave Jones99de0552006-09-29 02:00:10 -070041#include <linux/utsname.h>
Peter Zijlstra4b32d0a2007-07-19 01:48:59 -070042#include <linux/hash.h>
Steven Rostedt81d68a92008-05-12 21:20:42 +020043#include <linux/ftrace.h>
Peter Zijlstrab4b136f2009-01-29 14:50:36 +010044#include <linux/stringify.h>
Ming Leid588e462009-07-16 15:44:29 +020045#include <linux/bitops.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090046#include <linux/gfp.h>
Yong Zhangd3d03d42011-11-09 16:04:51 +080047#include <linux/kmemcheck.h>
Peter Zijlstrae7904a22015-08-01 19:25:08 +020048#include <linux/random.h>
Peter Zijlstradfaaf3f2016-05-30 18:31:33 +020049#include <linux/jhash.h>
Peter Zijlstraaf012962009-07-16 15:44:29 +020050
Ingo Molnarfbb9ce952006-07-03 00:24:50 -070051#include <asm/sections.h>
52
53#include "lockdep_internals.h"
54
Steven Rostedta8d154b2009-04-10 09:36:00 -040055#define CREATE_TRACE_POINTS
Frederic Weisbecker67178762009-11-13 10:06:34 +010056#include <trace/events/lock.h>
Steven Rostedta8d154b2009-04-10 09:36:00 -040057
Peter Zijlstraf20786f2007-07-19 01:48:56 -070058#ifdef CONFIG_PROVE_LOCKING
59int prove_locking = 1;
60module_param(prove_locking, int, 0644);
61#else
62#define prove_locking 0
63#endif
64
65#ifdef CONFIG_LOCK_STAT
66int lock_stat = 1;
67module_param(lock_stat, int, 0644);
68#else
69#define lock_stat 0
70#endif
71
Ingo Molnarfbb9ce952006-07-03 00:24:50 -070072/*
Ingo Molnar74c383f2006-12-13 00:34:43 -080073 * lockdep_lock: protects the lockdep graph, the hashes and the
74 * class/list/hash allocators.
Ingo Molnarfbb9ce952006-07-03 00:24:50 -070075 *
76 * This is one of the rare exceptions where it's justified
77 * to use a raw spinlock - we really dont want the spinlock
Ingo Molnar74c383f2006-12-13 00:34:43 -080078 * code to recurse back into the lockdep code...
Ingo Molnarfbb9ce952006-07-03 00:24:50 -070079 */
Thomas Gleixneredc35bd2009-12-03 12:38:57 +010080static arch_spinlock_t lockdep_lock = (arch_spinlock_t)__ARCH_SPIN_LOCK_UNLOCKED;
Ingo Molnar74c383f2006-12-13 00:34:43 -080081
82static int graph_lock(void)
83{
Thomas Gleixner0199c4e2009-12-02 20:01:25 +010084 arch_spin_lock(&lockdep_lock);
Ingo Molnar74c383f2006-12-13 00:34:43 -080085 /*
86 * Make sure that if another CPU detected a bug while
87 * walking the graph we dont change it (while the other
88 * CPU is busy printing out stuff with the graph lock
89 * dropped already)
90 */
91 if (!debug_locks) {
Thomas Gleixner0199c4e2009-12-02 20:01:25 +010092 arch_spin_unlock(&lockdep_lock);
Ingo Molnar74c383f2006-12-13 00:34:43 -080093 return 0;
94 }
Steven Rostedtbb065af2008-05-12 21:21:00 +020095 /* prevent any recursions within lockdep from causing deadlocks */
96 current->lockdep_recursion++;
Ingo Molnar74c383f2006-12-13 00:34:43 -080097 return 1;
98}
99
100static inline int graph_unlock(void)
101{
Peter Zijlstra0119fee2011-09-02 01:30:29 +0200102 if (debug_locks && !arch_spin_is_locked(&lockdep_lock)) {
103 /*
104 * The lockdep graph lock isn't locked while we expect it to
105 * be, we're confused now, bye!
106 */
Jarek Poplawski381a2292007-02-10 01:44:58 -0800107 return DEBUG_LOCKS_WARN_ON(1);
Peter Zijlstra0119fee2011-09-02 01:30:29 +0200108 }
Jarek Poplawski381a2292007-02-10 01:44:58 -0800109
Steven Rostedtbb065af2008-05-12 21:21:00 +0200110 current->lockdep_recursion--;
Thomas Gleixner0199c4e2009-12-02 20:01:25 +0100111 arch_spin_unlock(&lockdep_lock);
Ingo Molnar74c383f2006-12-13 00:34:43 -0800112 return 0;
113}
114
115/*
116 * Turn lock debugging off and return with 0 if it was off already,
117 * and also release the graph lock:
118 */
119static inline int debug_locks_off_graph_unlock(void)
120{
121 int ret = debug_locks_off();
122
Thomas Gleixner0199c4e2009-12-02 20:01:25 +0100123 arch_spin_unlock(&lockdep_lock);
Ingo Molnar74c383f2006-12-13 00:34:43 -0800124
125 return ret;
126}
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700127
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700128unsigned long nr_list_entries;
Peter Zijlstraaf012962009-07-16 15:44:29 +0200129static struct lock_list list_entries[MAX_LOCKDEP_ENTRIES];
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700130
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700131/*
132 * All data structures here are protected by the global debug_lock.
133 *
134 * Mutex key structs only get allocated, once during bootup, and never
135 * get freed - this significantly simplifies the debugging code.
136 */
137unsigned long nr_lock_classes;
138static struct lock_class lock_classes[MAX_LOCKDEP_KEYS];
139
Dave Jonesf82b2172008-08-11 09:30:23 +0200140static inline struct lock_class *hlock_class(struct held_lock *hlock)
141{
142 if (!hlock->class_idx) {
Peter Zijlstra0119fee2011-09-02 01:30:29 +0200143 /*
144 * Someone passed in garbage, we give up.
145 */
Dave Jonesf82b2172008-08-11 09:30:23 +0200146 DEBUG_LOCKS_WARN_ON(1);
147 return NULL;
148 }
149 return lock_classes + hlock->class_idx - 1;
150}
151
Peter Zijlstraf20786f2007-07-19 01:48:56 -0700152#ifdef CONFIG_LOCK_STAT
Peter Zijlstra25528212016-03-15 14:52:49 -0700153static DEFINE_PER_CPU(struct lock_class_stats[MAX_LOCKDEP_KEYS], cpu_lock_stats);
Peter Zijlstraf20786f2007-07-19 01:48:56 -0700154
Peter Zijlstra3365e7792009-10-09 10:12:41 +0200155static inline u64 lockstat_clock(void)
156{
Peter Zijlstrac6763292010-05-25 10:48:51 +0200157 return local_clock();
Peter Zijlstra3365e7792009-10-09 10:12:41 +0200158}
159
Peter Zijlstrac7e78cf2008-10-16 23:17:09 +0200160static int lock_point(unsigned long points[], unsigned long ip)
Peter Zijlstraf20786f2007-07-19 01:48:56 -0700161{
162 int i;
163
Peter Zijlstrac7e78cf2008-10-16 23:17:09 +0200164 for (i = 0; i < LOCKSTAT_POINTS; i++) {
165 if (points[i] == 0) {
166 points[i] = ip;
Peter Zijlstraf20786f2007-07-19 01:48:56 -0700167 break;
168 }
Peter Zijlstrac7e78cf2008-10-16 23:17:09 +0200169 if (points[i] == ip)
Peter Zijlstraf20786f2007-07-19 01:48:56 -0700170 break;
171 }
172
173 return i;
174}
175
Peter Zijlstra3365e7792009-10-09 10:12:41 +0200176static void lock_time_inc(struct lock_time *lt, u64 time)
Peter Zijlstraf20786f2007-07-19 01:48:56 -0700177{
178 if (time > lt->max)
179 lt->max = time;
180
Frank Rowand109d71c2009-11-19 13:42:06 -0800181 if (time < lt->min || !lt->nr)
Peter Zijlstraf20786f2007-07-19 01:48:56 -0700182 lt->min = time;
183
184 lt->total += time;
185 lt->nr++;
186}
187
Peter Zijlstrac46261d2007-07-19 01:48:57 -0700188static inline void lock_time_add(struct lock_time *src, struct lock_time *dst)
189{
Frank Rowand109d71c2009-11-19 13:42:06 -0800190 if (!src->nr)
191 return;
192
193 if (src->max > dst->max)
194 dst->max = src->max;
195
196 if (src->min < dst->min || !dst->nr)
197 dst->min = src->min;
198
Peter Zijlstrac46261d2007-07-19 01:48:57 -0700199 dst->total += src->total;
200 dst->nr += src->nr;
201}
202
203struct lock_class_stats lock_stats(struct lock_class *class)
204{
205 struct lock_class_stats stats;
206 int cpu, i;
207
208 memset(&stats, 0, sizeof(struct lock_class_stats));
209 for_each_possible_cpu(cpu) {
210 struct lock_class_stats *pcs =
Tejun Heo1871e522009-10-29 22:34:13 +0900211 &per_cpu(cpu_lock_stats, cpu)[class - lock_classes];
Peter Zijlstrac46261d2007-07-19 01:48:57 -0700212
213 for (i = 0; i < ARRAY_SIZE(stats.contention_point); i++)
214 stats.contention_point[i] += pcs->contention_point[i];
215
Peter Zijlstrac7e78cf2008-10-16 23:17:09 +0200216 for (i = 0; i < ARRAY_SIZE(stats.contending_point); i++)
217 stats.contending_point[i] += pcs->contending_point[i];
218
Peter Zijlstrac46261d2007-07-19 01:48:57 -0700219 lock_time_add(&pcs->read_waittime, &stats.read_waittime);
220 lock_time_add(&pcs->write_waittime, &stats.write_waittime);
221
222 lock_time_add(&pcs->read_holdtime, &stats.read_holdtime);
223 lock_time_add(&pcs->write_holdtime, &stats.write_holdtime);
Peter Zijlstra96645672007-07-19 01:49:00 -0700224
225 for (i = 0; i < ARRAY_SIZE(stats.bounces); i++)
226 stats.bounces[i] += pcs->bounces[i];
Peter Zijlstrac46261d2007-07-19 01:48:57 -0700227 }
228
229 return stats;
230}
231
232void clear_lock_stats(struct lock_class *class)
233{
234 int cpu;
235
236 for_each_possible_cpu(cpu) {
237 struct lock_class_stats *cpu_stats =
Tejun Heo1871e522009-10-29 22:34:13 +0900238 &per_cpu(cpu_lock_stats, cpu)[class - lock_classes];
Peter Zijlstrac46261d2007-07-19 01:48:57 -0700239
240 memset(cpu_stats, 0, sizeof(struct lock_class_stats));
241 }
242 memset(class->contention_point, 0, sizeof(class->contention_point));
Peter Zijlstrac7e78cf2008-10-16 23:17:09 +0200243 memset(class->contending_point, 0, sizeof(class->contending_point));
Peter Zijlstrac46261d2007-07-19 01:48:57 -0700244}
245
Peter Zijlstraf20786f2007-07-19 01:48:56 -0700246static struct lock_class_stats *get_lock_stats(struct lock_class *class)
247{
Tejun Heo1871e522009-10-29 22:34:13 +0900248 return &get_cpu_var(cpu_lock_stats)[class - lock_classes];
Peter Zijlstraf20786f2007-07-19 01:48:56 -0700249}
250
251static void put_lock_stats(struct lock_class_stats *stats)
252{
Tejun Heo1871e522009-10-29 22:34:13 +0900253 put_cpu_var(cpu_lock_stats);
Peter Zijlstraf20786f2007-07-19 01:48:56 -0700254}
255
256static void lock_release_holdtime(struct held_lock *hlock)
257{
258 struct lock_class_stats *stats;
Peter Zijlstra3365e7792009-10-09 10:12:41 +0200259 u64 holdtime;
Peter Zijlstraf20786f2007-07-19 01:48:56 -0700260
261 if (!lock_stat)
262 return;
263
Peter Zijlstra3365e7792009-10-09 10:12:41 +0200264 holdtime = lockstat_clock() - hlock->holdtime_stamp;
Peter Zijlstraf20786f2007-07-19 01:48:56 -0700265
Dave Jonesf82b2172008-08-11 09:30:23 +0200266 stats = get_lock_stats(hlock_class(hlock));
Peter Zijlstraf20786f2007-07-19 01:48:56 -0700267 if (hlock->read)
268 lock_time_inc(&stats->read_holdtime, holdtime);
269 else
270 lock_time_inc(&stats->write_holdtime, holdtime);
271 put_lock_stats(stats);
272}
273#else
274static inline void lock_release_holdtime(struct held_lock *hlock)
275{
276}
277#endif
278
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700279/*
280 * We keep a global list of all lock classes. The list only grows,
281 * never shrinks. The list is only accessed with the lockdep
282 * spinlock lock held.
283 */
284LIST_HEAD(all_lock_classes);
285
286/*
287 * The lockdep classes are in a hash-table as well, for fast lookup:
288 */
289#define CLASSHASH_BITS (MAX_LOCKDEP_KEYS_BITS - 1)
290#define CLASSHASH_SIZE (1UL << CLASSHASH_BITS)
Peter Zijlstra4b32d0a2007-07-19 01:48:59 -0700291#define __classhashfn(key) hash_long((unsigned long)key, CLASSHASH_BITS)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700292#define classhashentry(key) (classhash_table + __classhashfn((key)))
293
Andrew Mortona63f38c2016-02-03 13:44:12 -0800294static struct hlist_head classhash_table[CLASSHASH_SIZE];
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700295
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700296/*
297 * We put the lock dependency chains into a hash-table as well, to cache
298 * their existence:
299 */
300#define CHAINHASH_BITS (MAX_LOCKDEP_CHAINS_BITS-1)
301#define CHAINHASH_SIZE (1UL << CHAINHASH_BITS)
Peter Zijlstra4b32d0a2007-07-19 01:48:59 -0700302#define __chainhashfn(chain) hash_long(chain, CHAINHASH_BITS)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700303#define chainhashentry(chain) (chainhash_table + __chainhashfn((chain)))
304
Andrew Mortona63f38c2016-02-03 13:44:12 -0800305static struct hlist_head chainhash_table[CHAINHASH_SIZE];
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700306
307/*
308 * The hash key of the lock dependency chains is a hash itself too:
309 * it's a hash of all locks taken up to that lock, including that lock.
310 * It's a 64-bit hash, because it's important for the keys to be
311 * unique.
312 */
Peter Zijlstradfaaf3f2016-05-30 18:31:33 +0200313static inline u64 iterate_chain_key(u64 key, u32 idx)
314{
315 u32 k0 = key, k1 = key >> 32;
316
317 __jhash_mix(idx, k0, k1); /* Macro that modifies arguments! */
318
319 return k0 | (u64)k1 << 32;
320}
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700321
Steven Rostedt1d09daa2008-05-12 21:20:55 +0200322void lockdep_off(void)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700323{
324 current->lockdep_recursion++;
325}
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700326EXPORT_SYMBOL(lockdep_off);
327
Steven Rostedt1d09daa2008-05-12 21:20:55 +0200328void lockdep_on(void)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700329{
330 current->lockdep_recursion--;
331}
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700332EXPORT_SYMBOL(lockdep_on);
333
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700334/*
335 * Debugging switches:
336 */
337
338#define VERBOSE 0
Ingo Molnar33e94e92006-12-13 00:34:41 -0800339#define VERY_VERBOSE 0
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700340
341#if VERBOSE
342# define HARDIRQ_VERBOSE 1
343# define SOFTIRQ_VERBOSE 1
Nick Piggincf40bd12009-01-21 08:12:39 +0100344# define RECLAIM_VERBOSE 1
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700345#else
346# define HARDIRQ_VERBOSE 0
347# define SOFTIRQ_VERBOSE 0
Nick Piggincf40bd12009-01-21 08:12:39 +0100348# define RECLAIM_VERBOSE 0
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700349#endif
350
Nick Piggincf40bd12009-01-21 08:12:39 +0100351#if VERBOSE || HARDIRQ_VERBOSE || SOFTIRQ_VERBOSE || RECLAIM_VERBOSE
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700352/*
353 * Quick filtering for interesting events:
354 */
355static int class_filter(struct lock_class *class)
356{
Andi Kleenf9829cc2006-07-10 04:44:01 -0700357#if 0
358 /* Example */
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700359 if (class->name_version == 1 &&
Andi Kleenf9829cc2006-07-10 04:44:01 -0700360 !strcmp(class->name, "lockname"))
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700361 return 1;
362 if (class->name_version == 1 &&
Andi Kleenf9829cc2006-07-10 04:44:01 -0700363 !strcmp(class->name, "&struct->lockfield"))
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700364 return 1;
Andi Kleenf9829cc2006-07-10 04:44:01 -0700365#endif
Ingo Molnara6640892006-12-13 00:34:39 -0800366 /* Filter everything else. 1 would be to allow everything else */
367 return 0;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700368}
369#endif
370
371static int verbose(struct lock_class *class)
372{
373#if VERBOSE
374 return class_filter(class);
375#endif
376 return 0;
377}
378
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700379/*
380 * Stack-trace: tightly packed array of stack backtrace
Ingo Molnar74c383f2006-12-13 00:34:43 -0800381 * addresses. Protected by the graph_lock.
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700382 */
383unsigned long nr_stack_trace_entries;
384static unsigned long stack_trace[MAX_STACK_TRACE_ENTRIES];
385
Dave Jones2c522832013-04-25 13:40:02 -0400386static void print_lockdep_off(const char *bug_msg)
387{
388 printk(KERN_DEBUG "%s\n", bug_msg);
389 printk(KERN_DEBUG "turning off the locking correctness validator.\n");
Andreas Gruenbacheracf59372014-07-15 21:10:52 +0200390#ifdef CONFIG_LOCK_STAT
Dave Jones2c522832013-04-25 13:40:02 -0400391 printk(KERN_DEBUG "Please attach the output of /proc/lock_stat to the bug report\n");
Andreas Gruenbacheracf59372014-07-15 21:10:52 +0200392#endif
Dave Jones2c522832013-04-25 13:40:02 -0400393}
394
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700395static int save_trace(struct stack_trace *trace)
396{
397 trace->nr_entries = 0;
398 trace->max_entries = MAX_STACK_TRACE_ENTRIES - nr_stack_trace_entries;
399 trace->entries = stack_trace + nr_stack_trace_entries;
400
Andi Kleen5a1b3992006-09-26 10:52:34 +0200401 trace->skip = 3;
Andi Kleen5a1b3992006-09-26 10:52:34 +0200402
Christoph Hellwigab1b6f02007-05-08 00:23:29 -0700403 save_stack_trace(trace);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700404
Peter Zijlstra4f84f432009-07-20 15:27:04 +0200405 /*
406 * Some daft arches put -1 at the end to indicate its a full trace.
407 *
408 * <rant> this is buggy anyway, since it takes a whole extra entry so a
409 * complete trace that maxes out the entries provided will be reported
410 * as incomplete, friggin useless </rant>
411 */
Luck, Tonyea5b41f2009-12-09 14:29:36 -0800412 if (trace->nr_entries != 0 &&
413 trace->entries[trace->nr_entries-1] == ULONG_MAX)
Peter Zijlstra4f84f432009-07-20 15:27:04 +0200414 trace->nr_entries--;
415
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700416 trace->max_entries = trace->nr_entries;
417
418 nr_stack_trace_entries += trace->nr_entries;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700419
Peter Zijlstra4f84f432009-07-20 15:27:04 +0200420 if (nr_stack_trace_entries >= MAX_STACK_TRACE_ENTRIES-1) {
Ingo Molnar74c383f2006-12-13 00:34:43 -0800421 if (!debug_locks_off_graph_unlock())
422 return 0;
423
Dave Jones2c522832013-04-25 13:40:02 -0400424 print_lockdep_off("BUG: MAX_STACK_TRACE_ENTRIES too low!");
Ingo Molnar74c383f2006-12-13 00:34:43 -0800425 dump_stack();
426
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700427 return 0;
428 }
429
430 return 1;
431}
432
433unsigned int nr_hardirq_chains;
434unsigned int nr_softirq_chains;
435unsigned int nr_process_chains;
436unsigned int max_lockdep_depth;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700437
438#ifdef CONFIG_DEBUG_LOCKDEP
439/*
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700440 * Various lockdep statistics:
441 */
Frederic Weisbeckerbd6d29c2010-04-06 00:10:17 +0200442DEFINE_PER_CPU(struct lockdep_stats, lockdep_stats);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700443#endif
444
445/*
446 * Locking printouts:
447 */
448
Peter Zijlstrafabe9c42009-01-22 14:51:01 +0100449#define __USAGE(__STATE) \
Peter Zijlstrab4b136f2009-01-29 14:50:36 +0100450 [LOCK_USED_IN_##__STATE] = "IN-"__stringify(__STATE)"-W", \
451 [LOCK_ENABLED_##__STATE] = __stringify(__STATE)"-ON-W", \
452 [LOCK_USED_IN_##__STATE##_READ] = "IN-"__stringify(__STATE)"-R",\
453 [LOCK_ENABLED_##__STATE##_READ] = __stringify(__STATE)"-ON-R",
Peter Zijlstrafabe9c42009-01-22 14:51:01 +0100454
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700455static const char *usage_str[] =
456{
Peter Zijlstrafabe9c42009-01-22 14:51:01 +0100457#define LOCKDEP_STATE(__STATE) __USAGE(__STATE)
458#include "lockdep_states.h"
459#undef LOCKDEP_STATE
460 [LOCK_USED] = "INITIAL USE",
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700461};
462
463const char * __get_key_name(struct lockdep_subclass_key *key, char *str)
464{
Alexey Dobriyanffb45122007-05-08 00:28:41 -0700465 return kallsyms_lookup((unsigned long)key, NULL, NULL, NULL, str);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700466}
467
Peter Zijlstra3ff176c2009-01-22 17:40:42 +0100468static inline unsigned long lock_flag(enum lock_usage_bit bit)
469{
470 return 1UL << bit;
471}
472
473static char get_usage_char(struct lock_class *class, enum lock_usage_bit bit)
474{
475 char c = '.';
476
477 if (class->usage_mask & lock_flag(bit + 2))
478 c = '+';
479 if (class->usage_mask & lock_flag(bit)) {
480 c = '-';
481 if (class->usage_mask & lock_flag(bit + 2))
482 c = '?';
483 }
484
485 return c;
486}
487
Peter Zijlstraf510b232009-01-22 17:53:47 +0100488void get_usage_chars(struct lock_class *class, char usage[LOCK_USAGE_CHARS])
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700489{
Peter Zijlstraf510b232009-01-22 17:53:47 +0100490 int i = 0;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700491
Peter Zijlstraf510b232009-01-22 17:53:47 +0100492#define LOCKDEP_STATE(__STATE) \
493 usage[i++] = get_usage_char(class, LOCK_USED_IN_##__STATE); \
494 usage[i++] = get_usage_char(class, LOCK_USED_IN_##__STATE##_READ);
495#include "lockdep_states.h"
496#undef LOCKDEP_STATE
497
498 usage[i] = '\0';
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700499}
500
Steven Rostedte5e78d02011-11-02 20:24:16 -0400501static void __print_lock_name(struct lock_class *class)
Steven Rostedt3003eba2011-04-20 21:41:54 -0400502{
503 char str[KSYM_NAME_LEN];
504 const char *name;
505
506 name = class->name;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700507 if (!name) {
508 name = __get_key_name(class->key, str);
Dmitry Vyukovf943fe02016-11-28 15:24:43 +0100509 printk(KERN_CONT "%s", name);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700510 } else {
Dmitry Vyukovf943fe02016-11-28 15:24:43 +0100511 printk(KERN_CONT "%s", name);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700512 if (class->name_version > 1)
Dmitry Vyukovf943fe02016-11-28 15:24:43 +0100513 printk(KERN_CONT "#%d", class->name_version);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700514 if (class->subclass)
Dmitry Vyukovf943fe02016-11-28 15:24:43 +0100515 printk(KERN_CONT "/%d", class->subclass);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700516 }
Steven Rostedte5e78d02011-11-02 20:24:16 -0400517}
518
519static void print_lock_name(struct lock_class *class)
520{
521 char usage[LOCK_USAGE_CHARS];
522
523 get_usage_chars(class, usage);
524
Dmitry Vyukovf943fe02016-11-28 15:24:43 +0100525 printk(KERN_CONT " (");
Steven Rostedte5e78d02011-11-02 20:24:16 -0400526 __print_lock_name(class);
Dmitry Vyukovf943fe02016-11-28 15:24:43 +0100527 printk(KERN_CONT "){%s}", usage);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700528}
529
530static void print_lockdep_cache(struct lockdep_map *lock)
531{
532 const char *name;
Tejun Heo9281ace2007-07-17 04:03:51 -0700533 char str[KSYM_NAME_LEN];
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700534
535 name = lock->name;
536 if (!name)
537 name = __get_key_name(lock->key->subkeys, str);
538
Dmitry Vyukovf943fe02016-11-28 15:24:43 +0100539 printk(KERN_CONT "%s", name);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700540}
541
542static void print_lock(struct held_lock *hlock)
543{
Peter Zijlstrad7bc3192015-04-15 17:11:57 +0200544 /*
545 * We can be called locklessly through debug_show_all_locks() so be
546 * extra careful, the hlock might have been released and cleared.
547 */
548 unsigned int class_idx = hlock->class_idx;
549
550 /* Don't re-read hlock->class_idx, can't use READ_ONCE() on bitfields: */
551 barrier();
552
553 if (!class_idx || (class_idx - 1) >= MAX_LOCKDEP_KEYS) {
Dmitry Vyukovf943fe02016-11-28 15:24:43 +0100554 printk(KERN_CONT "<RELEASED>\n");
Peter Zijlstrad7bc3192015-04-15 17:11:57 +0200555 return;
556 }
557
558 print_lock_name(lock_classes + class_idx - 1);
Dmitry Vyukovf943fe02016-11-28 15:24:43 +0100559 printk(KERN_CONT ", at: [<%p>] %pS\n",
560 (void *)hlock->acquire_ip, (void *)hlock->acquire_ip);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700561}
562
563static void lockdep_print_held_locks(struct task_struct *curr)
564{
565 int i, depth = curr->lockdep_depth;
566
567 if (!depth) {
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -0700568 printk("no locks held by %s/%d.\n", curr->comm, task_pid_nr(curr));
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700569 return;
570 }
571 printk("%d lock%s held by %s/%d:\n",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -0700572 depth, depth > 1 ? "s" : "", curr->comm, task_pid_nr(curr));
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700573
574 for (i = 0; i < depth; i++) {
575 printk(" #%d: ", i);
576 print_lock(curr->held_locks + i);
577 }
578}
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700579
Ben Hutchingsfbdc4b92011-10-28 04:36:55 +0100580static void print_kernel_ident(void)
Dave Jones99de0552006-09-29 02:00:10 -0700581{
Ben Hutchingsfbdc4b92011-10-28 04:36:55 +0100582 printk("%s %.*s %s\n", init_utsname()->release,
Serge E. Hallyn96b644b2006-10-02 02:18:13 -0700583 (int)strcspn(init_utsname()->version, " "),
Ben Hutchingsfbdc4b92011-10-28 04:36:55 +0100584 init_utsname()->version,
585 print_tainted());
Dave Jones99de0552006-09-29 02:00:10 -0700586}
587
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700588static int very_verbose(struct lock_class *class)
589{
590#if VERY_VERBOSE
591 return class_filter(class);
592#endif
593 return 0;
594}
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700595
596/*
597 * Is this the address of a static object:
598 */
Sasha Levin8dce7a92013-06-13 18:41:16 -0400599#ifdef __KERNEL__
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700600static int static_obj(void *obj)
601{
602 unsigned long start = (unsigned long) &_stext,
603 end = (unsigned long) &_end,
604 addr = (unsigned long) obj;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700605
606 /*
607 * static variable?
608 */
609 if ((addr >= start) && (addr < end))
610 return 1;
611
Mike Frysinger2a9ad182009-09-22 16:44:16 -0700612 if (arch_is_kernel_data(addr))
613 return 1;
614
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700615 /*
Tejun Heo10fad5e2010-03-10 18:57:54 +0900616 * in-kernel percpu var?
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700617 */
Tejun Heo10fad5e2010-03-10 18:57:54 +0900618 if (is_kernel_percpu_address(addr))
619 return 1;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700620
621 /*
Tejun Heo10fad5e2010-03-10 18:57:54 +0900622 * module static or percpu var?
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700623 */
Tejun Heo10fad5e2010-03-10 18:57:54 +0900624 return is_module_address(addr) || is_module_percpu_address(addr);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700625}
Sasha Levin8dce7a92013-06-13 18:41:16 -0400626#endif
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700627
628/*
629 * To make lock name printouts unique, we calculate a unique
630 * class->name_version generation counter:
631 */
632static int count_matching_names(struct lock_class *new_class)
633{
634 struct lock_class *class;
635 int count = 0;
636
637 if (!new_class->name)
638 return 0;
639
Peter Zijlstra35a93932015-02-26 16:23:11 +0100640 list_for_each_entry_rcu(class, &all_lock_classes, lock_entry) {
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700641 if (new_class->key - new_class->subclass == class->key)
642 return class->name_version;
643 if (class->name && !strcmp(class->name, new_class->name))
644 count = max(count, class->name_version);
645 }
646
647 return count + 1;
648}
649
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700650/*
651 * Register a lock's class in the hash-table, if the class is not present
652 * yet. Otherwise we look it up. We cache the result in the lock object
653 * itself, so actual lookup of the hash should be once per lock object.
654 */
655static inline struct lock_class *
Ingo Molnard6d897c2006-07-10 04:44:04 -0700656look_up_lock_class(struct lockdep_map *lock, unsigned int subclass)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700657{
658 struct lockdep_subclass_key *key;
Andrew Mortona63f38c2016-02-03 13:44:12 -0800659 struct hlist_head *hash_head;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700660 struct lock_class *class;
661
Hitoshi Mitake4ba053c2010-10-13 17:30:26 +0900662 if (unlikely(subclass >= MAX_LOCKDEP_SUBCLASSES)) {
663 debug_locks_off();
664 printk(KERN_ERR
665 "BUG: looking up invalid subclass: %u\n", subclass);
666 printk(KERN_ERR
667 "turning off the locking correctness validator.\n");
668 dump_stack();
669 return NULL;
670 }
671
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700672 /*
673 * Static locks do not have their class-keys yet - for them the key
674 * is the lock object itself:
675 */
676 if (unlikely(!lock->key))
677 lock->key = (void *)lock;
678
679 /*
680 * NOTE: the class-key must be unique. For dynamic locks, a static
681 * lock_class_key variable is passed in through the mutex_init()
682 * (or spin_lock_init()) call - which acts as the key. For static
683 * locks we use the lock object itself as the key.
684 */
Peter Zijlstra4b32d0a2007-07-19 01:48:59 -0700685 BUILD_BUG_ON(sizeof(struct lock_class_key) >
686 sizeof(struct lockdep_map));
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700687
688 key = lock->key->subkeys + subclass;
689
690 hash_head = classhashentry(key);
691
692 /*
Peter Zijlstra35a93932015-02-26 16:23:11 +0100693 * We do an RCU walk of the hash, see lockdep_free_key_range().
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700694 */
Peter Zijlstra35a93932015-02-26 16:23:11 +0100695 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
696 return NULL;
697
Andrew Mortona63f38c2016-02-03 13:44:12 -0800698 hlist_for_each_entry_rcu(class, hash_head, hash_entry) {
Peter Zijlstra4b32d0a2007-07-19 01:48:59 -0700699 if (class->key == key) {
Peter Zijlstra0119fee2011-09-02 01:30:29 +0200700 /*
701 * Huh! same key, different name? Did someone trample
702 * on some memory? We're most confused.
703 */
Peter Zijlstra4b32d0a2007-07-19 01:48:59 -0700704 WARN_ON_ONCE(class->name != lock->name);
Ingo Molnard6d897c2006-07-10 04:44:04 -0700705 return class;
Peter Zijlstra4b32d0a2007-07-19 01:48:59 -0700706 }
707 }
Ingo Molnard6d897c2006-07-10 04:44:04 -0700708
709 return NULL;
710}
711
712/*
713 * Register a lock's class in the hash-table, if the class is not present
714 * yet. Otherwise we look it up. We cache the result in the lock object
715 * itself, so actual lookup of the hash should be once per lock object.
716 */
Denys Vlasenkoc003ed92016-04-08 20:58:46 +0200717static struct lock_class *
Peter Zijlstra4dfbb9d2006-10-11 01:45:14 -0400718register_lock_class(struct lockdep_map *lock, unsigned int subclass, int force)
Ingo Molnard6d897c2006-07-10 04:44:04 -0700719{
720 struct lockdep_subclass_key *key;
Andrew Mortona63f38c2016-02-03 13:44:12 -0800721 struct hlist_head *hash_head;
Ingo Molnard6d897c2006-07-10 04:44:04 -0700722 struct lock_class *class;
Peter Zijlstra35a93932015-02-26 16:23:11 +0100723
724 DEBUG_LOCKS_WARN_ON(!irqs_disabled());
Ingo Molnard6d897c2006-07-10 04:44:04 -0700725
726 class = look_up_lock_class(lock, subclass);
727 if (likely(class))
Yong Zhang87cdee72011-11-09 16:07:14 +0800728 goto out_set_class_cache;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700729
730 /*
731 * Debug-check: all keys must be persistent!
732 */
733 if (!static_obj(lock->key)) {
734 debug_locks_off();
735 printk("INFO: trying to register non-static key.\n");
736 printk("the code is fine but needs lockdep annotation.\n");
737 printk("turning off the locking correctness validator.\n");
738 dump_stack();
739
740 return NULL;
741 }
742
Ingo Molnard6d897c2006-07-10 04:44:04 -0700743 key = lock->key->subkeys + subclass;
744 hash_head = classhashentry(key);
745
Ingo Molnar74c383f2006-12-13 00:34:43 -0800746 if (!graph_lock()) {
Ingo Molnar74c383f2006-12-13 00:34:43 -0800747 return NULL;
748 }
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700749 /*
750 * We have to do the hash-walk again, to avoid races
751 * with another CPU:
752 */
Andrew Mortona63f38c2016-02-03 13:44:12 -0800753 hlist_for_each_entry_rcu(class, hash_head, hash_entry) {
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700754 if (class->key == key)
755 goto out_unlock_set;
Peter Zijlstra35a93932015-02-26 16:23:11 +0100756 }
757
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700758 /*
759 * Allocate a new key from the static array, and add it to
760 * the hash:
761 */
762 if (nr_lock_classes >= MAX_LOCKDEP_KEYS) {
Ingo Molnar74c383f2006-12-13 00:34:43 -0800763 if (!debug_locks_off_graph_unlock()) {
Ingo Molnar74c383f2006-12-13 00:34:43 -0800764 return NULL;
765 }
Ingo Molnar74c383f2006-12-13 00:34:43 -0800766
Dave Jones2c522832013-04-25 13:40:02 -0400767 print_lockdep_off("BUG: MAX_LOCKDEP_KEYS too low!");
Peter Zijlstraeedeeab2009-03-18 12:38:47 +0100768 dump_stack();
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700769 return NULL;
770 }
771 class = lock_classes + nr_lock_classes++;
Frederic Weisbeckerbd6d29c2010-04-06 00:10:17 +0200772 debug_atomic_inc(nr_unused_locks);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700773 class->key = key;
774 class->name = lock->name;
775 class->subclass = subclass;
776 INIT_LIST_HEAD(&class->lock_entry);
777 INIT_LIST_HEAD(&class->locks_before);
778 INIT_LIST_HEAD(&class->locks_after);
779 class->name_version = count_matching_names(class);
780 /*
781 * We use RCU's safe list-add method to make
782 * parallel walking of the hash-list safe:
783 */
Andrew Mortona63f38c2016-02-03 13:44:12 -0800784 hlist_add_head_rcu(&class->hash_entry, hash_head);
Dale Farnsworth14811972008-02-25 23:03:02 +0100785 /*
786 * Add it to the global list of classes:
787 */
788 list_add_tail_rcu(&class->lock_entry, &all_lock_classes);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700789
790 if (verbose(class)) {
Ingo Molnar74c383f2006-12-13 00:34:43 -0800791 graph_unlock();
Ingo Molnar74c383f2006-12-13 00:34:43 -0800792
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700793 printk("\nnew class %p: %s", class->key, class->name);
794 if (class->name_version > 1)
Dmitry Vyukovf943fe02016-11-28 15:24:43 +0100795 printk(KERN_CONT "#%d", class->name_version);
796 printk(KERN_CONT "\n");
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700797 dump_stack();
Ingo Molnar74c383f2006-12-13 00:34:43 -0800798
Ingo Molnar74c383f2006-12-13 00:34:43 -0800799 if (!graph_lock()) {
Ingo Molnar74c383f2006-12-13 00:34:43 -0800800 return NULL;
801 }
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700802 }
803out_unlock_set:
Ingo Molnar74c383f2006-12-13 00:34:43 -0800804 graph_unlock();
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700805
Yong Zhang87cdee72011-11-09 16:07:14 +0800806out_set_class_cache:
Peter Zijlstra4dfbb9d2006-10-11 01:45:14 -0400807 if (!subclass || force)
Hitoshi Mitake62016252010-10-05 18:01:51 +0900808 lock->class_cache[0] = class;
809 else if (subclass < NR_LOCKDEP_CACHING_CLASSES)
810 lock->class_cache[subclass] = class;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700811
Peter Zijlstra0119fee2011-09-02 01:30:29 +0200812 /*
813 * Hash collision, did we smoke some? We found a class with a matching
814 * hash but the subclass -- which is hashed in -- didn't match.
815 */
Jarek Poplawski381a2292007-02-10 01:44:58 -0800816 if (DEBUG_LOCKS_WARN_ON(class->subclass != subclass))
817 return NULL;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700818
819 return class;
820}
821
Peter Zijlstraca58abc2007-07-19 01:48:53 -0700822#ifdef CONFIG_PROVE_LOCKING
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700823/*
Peter Zijlstra8e182572007-07-19 01:48:54 -0700824 * Allocate a lockdep entry. (assumes the graph_lock held, returns
825 * with NULL on failure)
826 */
827static struct lock_list *alloc_list_entry(void)
828{
829 if (nr_list_entries >= MAX_LOCKDEP_ENTRIES) {
830 if (!debug_locks_off_graph_unlock())
831 return NULL;
832
Dave Jones2c522832013-04-25 13:40:02 -0400833 print_lockdep_off("BUG: MAX_LOCKDEP_ENTRIES too low!");
Peter Zijlstraeedeeab2009-03-18 12:38:47 +0100834 dump_stack();
Peter Zijlstra8e182572007-07-19 01:48:54 -0700835 return NULL;
836 }
837 return list_entries + nr_list_entries++;
838}
839
840/*
841 * Add a new dependency to the head of the list:
842 */
843static int add_lock_to_list(struct lock_class *class, struct lock_class *this,
Yong Zhang4726f2a2010-05-04 14:16:48 +0800844 struct list_head *head, unsigned long ip,
845 int distance, struct stack_trace *trace)
Peter Zijlstra8e182572007-07-19 01:48:54 -0700846{
847 struct lock_list *entry;
848 /*
849 * Lock not present yet - get a new dependency struct and
850 * add it to the list:
851 */
852 entry = alloc_list_entry();
853 if (!entry)
854 return 0;
855
Zhu Yi74870172008-08-27 14:33:00 +0800856 entry->class = this;
857 entry->distance = distance;
Yong Zhang4726f2a2010-05-04 14:16:48 +0800858 entry->trace = *trace;
Peter Zijlstra8e182572007-07-19 01:48:54 -0700859 /*
Peter Zijlstra35a93932015-02-26 16:23:11 +0100860 * Both allocation and removal are done under the graph lock; but
861 * iteration is under RCU-sched; see look_up_lock_class() and
862 * lockdep_free_key_range().
Peter Zijlstra8e182572007-07-19 01:48:54 -0700863 */
864 list_add_tail_rcu(&entry->entry, head);
865
866 return 1;
867}
868
Peter Zijlstra98c33ed2009-07-21 13:19:07 +0200869/*
870 * For good efficiency of modular, we use power of 2
871 */
Peter Zijlstraaf012962009-07-16 15:44:29 +0200872#define MAX_CIRCULAR_QUEUE_SIZE 4096UL
873#define CQ_MASK (MAX_CIRCULAR_QUEUE_SIZE-1)
874
Peter Zijlstra98c33ed2009-07-21 13:19:07 +0200875/*
876 * The circular_queue and helpers is used to implement the
Peter Zijlstraaf012962009-07-16 15:44:29 +0200877 * breadth-first search(BFS)algorithem, by which we can build
878 * the shortest path from the next lock to be acquired to the
879 * previous held lock if there is a circular between them.
Peter Zijlstra98c33ed2009-07-21 13:19:07 +0200880 */
Peter Zijlstraaf012962009-07-16 15:44:29 +0200881struct circular_queue {
882 unsigned long element[MAX_CIRCULAR_QUEUE_SIZE];
883 unsigned int front, rear;
884};
885
886static struct circular_queue lock_cq;
Peter Zijlstraaf012962009-07-16 15:44:29 +0200887
Ming Lei12f3dfd2009-07-16 15:44:29 +0200888unsigned int max_bfs_queue_depth;
Peter Zijlstraaf012962009-07-16 15:44:29 +0200889
Ming Leie351b662009-07-22 22:48:09 +0800890static unsigned int lockdep_dependency_gen_id;
891
Peter Zijlstraaf012962009-07-16 15:44:29 +0200892static inline void __cq_init(struct circular_queue *cq)
893{
894 cq->front = cq->rear = 0;
Ming Leie351b662009-07-22 22:48:09 +0800895 lockdep_dependency_gen_id++;
Peter Zijlstraaf012962009-07-16 15:44:29 +0200896}
897
898static inline int __cq_empty(struct circular_queue *cq)
899{
900 return (cq->front == cq->rear);
901}
902
903static inline int __cq_full(struct circular_queue *cq)
904{
905 return ((cq->rear + 1) & CQ_MASK) == cq->front;
906}
907
908static inline int __cq_enqueue(struct circular_queue *cq, unsigned long elem)
909{
910 if (__cq_full(cq))
911 return -1;
912
913 cq->element[cq->rear] = elem;
914 cq->rear = (cq->rear + 1) & CQ_MASK;
915 return 0;
916}
917
918static inline int __cq_dequeue(struct circular_queue *cq, unsigned long *elem)
919{
920 if (__cq_empty(cq))
921 return -1;
922
923 *elem = cq->element[cq->front];
924 cq->front = (cq->front + 1) & CQ_MASK;
925 return 0;
926}
927
928static inline unsigned int __cq_get_elem_count(struct circular_queue *cq)
929{
930 return (cq->rear - cq->front) & CQ_MASK;
931}
932
933static inline void mark_lock_accessed(struct lock_list *lock,
934 struct lock_list *parent)
935{
936 unsigned long nr;
Peter Zijlstra98c33ed2009-07-21 13:19:07 +0200937
Peter Zijlstraaf012962009-07-16 15:44:29 +0200938 nr = lock - list_entries;
Peter Zijlstra0119fee2011-09-02 01:30:29 +0200939 WARN_ON(nr >= nr_list_entries); /* Out-of-bounds, input fail */
Peter Zijlstraaf012962009-07-16 15:44:29 +0200940 lock->parent = parent;
Ming Leie351b662009-07-22 22:48:09 +0800941 lock->class->dep_gen_id = lockdep_dependency_gen_id;
Peter Zijlstraaf012962009-07-16 15:44:29 +0200942}
943
944static inline unsigned long lock_accessed(struct lock_list *lock)
945{
946 unsigned long nr;
Peter Zijlstra98c33ed2009-07-21 13:19:07 +0200947
Peter Zijlstraaf012962009-07-16 15:44:29 +0200948 nr = lock - list_entries;
Peter Zijlstra0119fee2011-09-02 01:30:29 +0200949 WARN_ON(nr >= nr_list_entries); /* Out-of-bounds, input fail */
Ming Leie351b662009-07-22 22:48:09 +0800950 return lock->class->dep_gen_id == lockdep_dependency_gen_id;
Peter Zijlstraaf012962009-07-16 15:44:29 +0200951}
952
953static inline struct lock_list *get_lock_parent(struct lock_list *child)
954{
955 return child->parent;
956}
957
958static inline int get_lock_depth(struct lock_list *child)
959{
960 int depth = 0;
961 struct lock_list *parent;
962
963 while ((parent = get_lock_parent(child))) {
964 child = parent;
965 depth++;
966 }
967 return depth;
968}
969
Ming Lei9e2d5512009-07-16 15:44:29 +0200970static int __bfs(struct lock_list *source_entry,
Peter Zijlstraaf012962009-07-16 15:44:29 +0200971 void *data,
972 int (*match)(struct lock_list *entry, void *data),
973 struct lock_list **target_entry,
974 int forward)
Ming Leic94aa5c2009-07-16 15:44:29 +0200975{
976 struct lock_list *entry;
Ming Leid588e462009-07-16 15:44:29 +0200977 struct list_head *head;
Ming Leic94aa5c2009-07-16 15:44:29 +0200978 struct circular_queue *cq = &lock_cq;
979 int ret = 1;
980
Ming Lei9e2d5512009-07-16 15:44:29 +0200981 if (match(source_entry, data)) {
Ming Leic94aa5c2009-07-16 15:44:29 +0200982 *target_entry = source_entry;
983 ret = 0;
984 goto exit;
985 }
986
Ming Leid588e462009-07-16 15:44:29 +0200987 if (forward)
988 head = &source_entry->class->locks_after;
989 else
990 head = &source_entry->class->locks_before;
991
992 if (list_empty(head))
993 goto exit;
994
995 __cq_init(cq);
Ming Leic94aa5c2009-07-16 15:44:29 +0200996 __cq_enqueue(cq, (unsigned long)source_entry);
997
998 while (!__cq_empty(cq)) {
999 struct lock_list *lock;
Ming Leic94aa5c2009-07-16 15:44:29 +02001000
1001 __cq_dequeue(cq, (unsigned long *)&lock);
1002
1003 if (!lock->class) {
1004 ret = -2;
1005 goto exit;
1006 }
1007
1008 if (forward)
1009 head = &lock->class->locks_after;
1010 else
1011 head = &lock->class->locks_before;
1012
Peter Zijlstra35a93932015-02-26 16:23:11 +01001013 DEBUG_LOCKS_WARN_ON(!irqs_disabled());
1014
1015 list_for_each_entry_rcu(entry, head, entry) {
Ming Leic94aa5c2009-07-16 15:44:29 +02001016 if (!lock_accessed(entry)) {
Ming Lei12f3dfd2009-07-16 15:44:29 +02001017 unsigned int cq_depth;
Ming Leic94aa5c2009-07-16 15:44:29 +02001018 mark_lock_accessed(entry, lock);
Ming Lei9e2d5512009-07-16 15:44:29 +02001019 if (match(entry, data)) {
Ming Leic94aa5c2009-07-16 15:44:29 +02001020 *target_entry = entry;
1021 ret = 0;
1022 goto exit;
1023 }
1024
1025 if (__cq_enqueue(cq, (unsigned long)entry)) {
1026 ret = -1;
1027 goto exit;
1028 }
Ming Lei12f3dfd2009-07-16 15:44:29 +02001029 cq_depth = __cq_get_elem_count(cq);
1030 if (max_bfs_queue_depth < cq_depth)
1031 max_bfs_queue_depth = cq_depth;
Ming Leic94aa5c2009-07-16 15:44:29 +02001032 }
1033 }
1034 }
1035exit:
1036 return ret;
1037}
1038
Ming Leid7aaba12009-07-16 15:44:29 +02001039static inline int __bfs_forwards(struct lock_list *src_entry,
Ming Lei9e2d5512009-07-16 15:44:29 +02001040 void *data,
1041 int (*match)(struct lock_list *entry, void *data),
1042 struct lock_list **target_entry)
Ming Leic94aa5c2009-07-16 15:44:29 +02001043{
Ming Lei9e2d5512009-07-16 15:44:29 +02001044 return __bfs(src_entry, data, match, target_entry, 1);
Ming Leic94aa5c2009-07-16 15:44:29 +02001045
1046}
1047
Ming Leid7aaba12009-07-16 15:44:29 +02001048static inline int __bfs_backwards(struct lock_list *src_entry,
Ming Lei9e2d5512009-07-16 15:44:29 +02001049 void *data,
1050 int (*match)(struct lock_list *entry, void *data),
1051 struct lock_list **target_entry)
Ming Leic94aa5c2009-07-16 15:44:29 +02001052{
Ming Lei9e2d5512009-07-16 15:44:29 +02001053 return __bfs(src_entry, data, match, target_entry, 0);
Ming Leic94aa5c2009-07-16 15:44:29 +02001054
1055}
1056
Peter Zijlstra8e182572007-07-19 01:48:54 -07001057/*
1058 * Recursive, forwards-direction lock-dependency checking, used for
1059 * both noncyclic checking and for hardirq-unsafe/softirq-unsafe
1060 * checking.
Peter Zijlstra8e182572007-07-19 01:48:54 -07001061 */
Peter Zijlstra8e182572007-07-19 01:48:54 -07001062
1063/*
1064 * Print a dependency chain entry (this is only done when a deadlock
1065 * has been detected):
1066 */
1067static noinline int
Ming Lei24208ca2009-07-16 15:44:29 +02001068print_circular_bug_entry(struct lock_list *target, int depth)
Peter Zijlstra8e182572007-07-19 01:48:54 -07001069{
1070 if (debug_locks_silent)
1071 return 0;
1072 printk("\n-> #%u", depth);
1073 print_lock_name(target->class);
Dmitry Vyukovf943fe02016-11-28 15:24:43 +01001074 printk(KERN_CONT ":\n");
Peter Zijlstra8e182572007-07-19 01:48:54 -07001075 print_stack_trace(&target->trace, 6);
1076
1077 return 0;
1078}
1079
Steven Rostedtf4185812011-04-20 21:41:55 -04001080static void
1081print_circular_lock_scenario(struct held_lock *src,
1082 struct held_lock *tgt,
1083 struct lock_list *prt)
1084{
1085 struct lock_class *source = hlock_class(src);
1086 struct lock_class *target = hlock_class(tgt);
1087 struct lock_class *parent = prt->class;
1088
1089 /*
1090 * A direct locking problem where unsafe_class lock is taken
1091 * directly by safe_class lock, then all we need to show
1092 * is the deadlock scenario, as it is obvious that the
1093 * unsafe lock is taken under the safe lock.
1094 *
1095 * But if there is a chain instead, where the safe lock takes
1096 * an intermediate lock (middle_class) where this lock is
1097 * not the same as the safe lock, then the lock chain is
1098 * used to describe the problem. Otherwise we would need
1099 * to show a different CPU case for each link in the chain
1100 * from the safe_class lock to the unsafe_class lock.
1101 */
1102 if (parent != source) {
1103 printk("Chain exists of:\n ");
1104 __print_lock_name(source);
Dmitry Vyukovf943fe02016-11-28 15:24:43 +01001105 printk(KERN_CONT " --> ");
Steven Rostedtf4185812011-04-20 21:41:55 -04001106 __print_lock_name(parent);
Dmitry Vyukovf943fe02016-11-28 15:24:43 +01001107 printk(KERN_CONT " --> ");
Steven Rostedtf4185812011-04-20 21:41:55 -04001108 __print_lock_name(target);
Dmitry Vyukovf943fe02016-11-28 15:24:43 +01001109 printk(KERN_CONT "\n\n");
Steven Rostedtf4185812011-04-20 21:41:55 -04001110 }
1111
1112 printk(" Possible unsafe locking scenario:\n\n");
1113 printk(" CPU0 CPU1\n");
1114 printk(" ---- ----\n");
1115 printk(" lock(");
1116 __print_lock_name(target);
Dmitry Vyukovf943fe02016-11-28 15:24:43 +01001117 printk(KERN_CONT ");\n");
Steven Rostedtf4185812011-04-20 21:41:55 -04001118 printk(" lock(");
1119 __print_lock_name(parent);
Dmitry Vyukovf943fe02016-11-28 15:24:43 +01001120 printk(KERN_CONT ");\n");
Steven Rostedtf4185812011-04-20 21:41:55 -04001121 printk(" lock(");
1122 __print_lock_name(target);
Dmitry Vyukovf943fe02016-11-28 15:24:43 +01001123 printk(KERN_CONT ");\n");
Steven Rostedtf4185812011-04-20 21:41:55 -04001124 printk(" lock(");
1125 __print_lock_name(source);
Dmitry Vyukovf943fe02016-11-28 15:24:43 +01001126 printk(KERN_CONT ");\n");
Steven Rostedtf4185812011-04-20 21:41:55 -04001127 printk("\n *** DEADLOCK ***\n\n");
1128}
1129
Peter Zijlstra8e182572007-07-19 01:48:54 -07001130/*
1131 * When a circular dependency is detected, print the
1132 * header first:
1133 */
1134static noinline int
Ming Leidb0002a2009-07-16 15:44:29 +02001135print_circular_bug_header(struct lock_list *entry, unsigned int depth,
1136 struct held_lock *check_src,
1137 struct held_lock *check_tgt)
Peter Zijlstra8e182572007-07-19 01:48:54 -07001138{
1139 struct task_struct *curr = current;
1140
Ming Leic94aa5c2009-07-16 15:44:29 +02001141 if (debug_locks_silent)
Peter Zijlstra8e182572007-07-19 01:48:54 -07001142 return 0;
1143
Paul E. McKenneyb3fbab02011-05-24 08:31:09 -07001144 printk("\n");
1145 printk("======================================================\n");
1146 printk("[ INFO: possible circular locking dependency detected ]\n");
Ben Hutchingsfbdc4b92011-10-28 04:36:55 +01001147 print_kernel_ident();
Paul E. McKenneyb3fbab02011-05-24 08:31:09 -07001148 printk("-------------------------------------------------------\n");
Peter Zijlstra8e182572007-07-19 01:48:54 -07001149 printk("%s/%d is trying to acquire lock:\n",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07001150 curr->comm, task_pid_nr(curr));
Ming Leidb0002a2009-07-16 15:44:29 +02001151 print_lock(check_src);
Peter Zijlstra8e182572007-07-19 01:48:54 -07001152 printk("\nbut task is already holding lock:\n");
Ming Leidb0002a2009-07-16 15:44:29 +02001153 print_lock(check_tgt);
Peter Zijlstra8e182572007-07-19 01:48:54 -07001154 printk("\nwhich lock already depends on the new lock.\n\n");
1155 printk("\nthe existing dependency chain (in reverse order) is:\n");
1156
1157 print_circular_bug_entry(entry, depth);
1158
1159 return 0;
1160}
1161
Ming Lei9e2d5512009-07-16 15:44:29 +02001162static inline int class_equal(struct lock_list *entry, void *data)
1163{
1164 return entry->class == data;
1165}
1166
Ming Leidb0002a2009-07-16 15:44:29 +02001167static noinline int print_circular_bug(struct lock_list *this,
1168 struct lock_list *target,
1169 struct held_lock *check_src,
1170 struct held_lock *check_tgt)
Peter Zijlstra8e182572007-07-19 01:48:54 -07001171{
1172 struct task_struct *curr = current;
Ming Leic94aa5c2009-07-16 15:44:29 +02001173 struct lock_list *parent;
Steven Rostedtf4185812011-04-20 21:41:55 -04001174 struct lock_list *first_parent;
Ming Lei24208ca2009-07-16 15:44:29 +02001175 int depth;
Peter Zijlstra8e182572007-07-19 01:48:54 -07001176
Ming Leic94aa5c2009-07-16 15:44:29 +02001177 if (!debug_locks_off_graph_unlock() || debug_locks_silent)
Peter Zijlstra8e182572007-07-19 01:48:54 -07001178 return 0;
1179
Ming Leidb0002a2009-07-16 15:44:29 +02001180 if (!save_trace(&this->trace))
Peter Zijlstra8e182572007-07-19 01:48:54 -07001181 return 0;
1182
Ming Leic94aa5c2009-07-16 15:44:29 +02001183 depth = get_lock_depth(target);
1184
Ming Leidb0002a2009-07-16 15:44:29 +02001185 print_circular_bug_header(target, depth, check_src, check_tgt);
Ming Leic94aa5c2009-07-16 15:44:29 +02001186
1187 parent = get_lock_parent(target);
Steven Rostedtf4185812011-04-20 21:41:55 -04001188 first_parent = parent;
Ming Leic94aa5c2009-07-16 15:44:29 +02001189
1190 while (parent) {
1191 print_circular_bug_entry(parent, --depth);
1192 parent = get_lock_parent(parent);
1193 }
Peter Zijlstra8e182572007-07-19 01:48:54 -07001194
1195 printk("\nother info that might help us debug this:\n\n");
Steven Rostedtf4185812011-04-20 21:41:55 -04001196 print_circular_lock_scenario(check_src, check_tgt,
1197 first_parent);
1198
Peter Zijlstra8e182572007-07-19 01:48:54 -07001199 lockdep_print_held_locks(curr);
1200
1201 printk("\nstack backtrace:\n");
1202 dump_stack();
1203
1204 return 0;
1205}
1206
Ming Leidb0002a2009-07-16 15:44:29 +02001207static noinline int print_bfs_bug(int ret)
1208{
1209 if (!debug_locks_off_graph_unlock())
1210 return 0;
1211
Peter Zijlstra0119fee2011-09-02 01:30:29 +02001212 /*
1213 * Breadth-first-search failed, graph got corrupted?
1214 */
Ming Leidb0002a2009-07-16 15:44:29 +02001215 WARN(1, "lockdep bfs error:%d\n", ret);
1216
1217 return 0;
1218}
1219
Ming Leief681022009-07-16 15:44:29 +02001220static int noop_count(struct lock_list *entry, void *data)
David Miller419ca3f2008-07-29 21:45:03 -07001221{
Ming Leief681022009-07-16 15:44:29 +02001222 (*(unsigned long *)data)++;
1223 return 0;
David Miller419ca3f2008-07-29 21:45:03 -07001224}
1225
Fengguang Wu5216d532013-11-09 00:55:35 +08001226static unsigned long __lockdep_count_forward_deps(struct lock_list *this)
Ming Leief681022009-07-16 15:44:29 +02001227{
1228 unsigned long count = 0;
1229 struct lock_list *uninitialized_var(target_entry);
1230
1231 __bfs_forwards(this, (void *)&count, noop_count, &target_entry);
1232
1233 return count;
1234}
David Miller419ca3f2008-07-29 21:45:03 -07001235unsigned long lockdep_count_forward_deps(struct lock_class *class)
1236{
1237 unsigned long ret, flags;
Ming Leief681022009-07-16 15:44:29 +02001238 struct lock_list this;
1239
1240 this.parent = NULL;
1241 this.class = class;
David Miller419ca3f2008-07-29 21:45:03 -07001242
Steven Rostedt (VMware)a8b8e522018-04-04 14:06:30 -04001243 raw_local_irq_save(flags);
Thomas Gleixner0199c4e2009-12-02 20:01:25 +01001244 arch_spin_lock(&lockdep_lock);
Ming Leief681022009-07-16 15:44:29 +02001245 ret = __lockdep_count_forward_deps(&this);
Thomas Gleixner0199c4e2009-12-02 20:01:25 +01001246 arch_spin_unlock(&lockdep_lock);
Steven Rostedt (VMware)a8b8e522018-04-04 14:06:30 -04001247 raw_local_irq_restore(flags);
David Miller419ca3f2008-07-29 21:45:03 -07001248
1249 return ret;
1250}
1251
Fengguang Wu5216d532013-11-09 00:55:35 +08001252static unsigned long __lockdep_count_backward_deps(struct lock_list *this)
David Miller419ca3f2008-07-29 21:45:03 -07001253{
Ming Leief681022009-07-16 15:44:29 +02001254 unsigned long count = 0;
1255 struct lock_list *uninitialized_var(target_entry);
David Miller419ca3f2008-07-29 21:45:03 -07001256
Ming Leief681022009-07-16 15:44:29 +02001257 __bfs_backwards(this, (void *)&count, noop_count, &target_entry);
David Miller419ca3f2008-07-29 21:45:03 -07001258
Ming Leief681022009-07-16 15:44:29 +02001259 return count;
David Miller419ca3f2008-07-29 21:45:03 -07001260}
1261
1262unsigned long lockdep_count_backward_deps(struct lock_class *class)
1263{
1264 unsigned long ret, flags;
Ming Leief681022009-07-16 15:44:29 +02001265 struct lock_list this;
1266
1267 this.parent = NULL;
1268 this.class = class;
David Miller419ca3f2008-07-29 21:45:03 -07001269
Steven Rostedt (VMware)a8b8e522018-04-04 14:06:30 -04001270 raw_local_irq_save(flags);
Thomas Gleixner0199c4e2009-12-02 20:01:25 +01001271 arch_spin_lock(&lockdep_lock);
Ming Leief681022009-07-16 15:44:29 +02001272 ret = __lockdep_count_backward_deps(&this);
Thomas Gleixner0199c4e2009-12-02 20:01:25 +01001273 arch_spin_unlock(&lockdep_lock);
Steven Rostedt (VMware)a8b8e522018-04-04 14:06:30 -04001274 raw_local_irq_restore(flags);
David Miller419ca3f2008-07-29 21:45:03 -07001275
1276 return ret;
1277}
1278
Peter Zijlstra8e182572007-07-19 01:48:54 -07001279/*
1280 * Prove that the dependency graph starting at <entry> can not
1281 * lead to <target>. Print an error and return 0 if it does.
1282 */
1283static noinline int
Ming Leidb0002a2009-07-16 15:44:29 +02001284check_noncircular(struct lock_list *root, struct lock_class *target,
1285 struct lock_list **target_entry)
Peter Zijlstra8e182572007-07-19 01:48:54 -07001286{
Ming Leidb0002a2009-07-16 15:44:29 +02001287 int result;
Peter Zijlstra8e182572007-07-19 01:48:54 -07001288
Frederic Weisbeckerbd6d29c2010-04-06 00:10:17 +02001289 debug_atomic_inc(nr_cyclic_checks);
David Miller419ca3f2008-07-29 21:45:03 -07001290
Ming Leid7aaba12009-07-16 15:44:29 +02001291 result = __bfs_forwards(root, target, class_equal, target_entry);
Ming Leidb0002a2009-07-16 15:44:29 +02001292
1293 return result;
Peter Zijlstra8e182572007-07-19 01:48:54 -07001294}
1295
Steven Rostedt81d68a92008-05-12 21:20:42 +02001296#if defined(CONFIG_TRACE_IRQFLAGS) && defined(CONFIG_PROVE_LOCKING)
Peter Zijlstra8e182572007-07-19 01:48:54 -07001297/*
1298 * Forwards and backwards subgraph searching, for the purposes of
1299 * proving that two subgraphs can be connected by a new dependency
1300 * without creating any illegal irq-safe -> irq-unsafe lock dependency.
1301 */
Peter Zijlstra8e182572007-07-19 01:48:54 -07001302
Ming Leid7aaba12009-07-16 15:44:29 +02001303static inline int usage_match(struct lock_list *entry, void *bit)
1304{
1305 return entry->class->usage_mask & (1 << (enum lock_usage_bit)bit);
1306}
1307
1308
1309
Peter Zijlstra8e182572007-07-19 01:48:54 -07001310/*
1311 * Find a node in the forwards-direction dependency sub-graph starting
Ming Leid7aaba12009-07-16 15:44:29 +02001312 * at @root->class that matches @bit.
Peter Zijlstra8e182572007-07-19 01:48:54 -07001313 *
Ming Leid7aaba12009-07-16 15:44:29 +02001314 * Return 0 if such a node exists in the subgraph, and put that node
1315 * into *@target_entry.
Peter Zijlstra8e182572007-07-19 01:48:54 -07001316 *
Ming Leid7aaba12009-07-16 15:44:29 +02001317 * Return 1 otherwise and keep *@target_entry unchanged.
1318 * Return <0 on error.
Peter Zijlstra8e182572007-07-19 01:48:54 -07001319 */
Ming Leid7aaba12009-07-16 15:44:29 +02001320static int
1321find_usage_forwards(struct lock_list *root, enum lock_usage_bit bit,
1322 struct lock_list **target_entry)
Peter Zijlstra8e182572007-07-19 01:48:54 -07001323{
Ming Leid7aaba12009-07-16 15:44:29 +02001324 int result;
Peter Zijlstra8e182572007-07-19 01:48:54 -07001325
Frederic Weisbeckerbd6d29c2010-04-06 00:10:17 +02001326 debug_atomic_inc(nr_find_usage_forwards_checks);
Peter Zijlstra8e182572007-07-19 01:48:54 -07001327
Ming Leid7aaba12009-07-16 15:44:29 +02001328 result = __bfs_forwards(root, (void *)bit, usage_match, target_entry);
1329
1330 return result;
Peter Zijlstra8e182572007-07-19 01:48:54 -07001331}
1332
1333/*
1334 * Find a node in the backwards-direction dependency sub-graph starting
Ming Leid7aaba12009-07-16 15:44:29 +02001335 * at @root->class that matches @bit.
Peter Zijlstra8e182572007-07-19 01:48:54 -07001336 *
Ming Leid7aaba12009-07-16 15:44:29 +02001337 * Return 0 if such a node exists in the subgraph, and put that node
1338 * into *@target_entry.
Peter Zijlstra8e182572007-07-19 01:48:54 -07001339 *
Ming Leid7aaba12009-07-16 15:44:29 +02001340 * Return 1 otherwise and keep *@target_entry unchanged.
1341 * Return <0 on error.
Peter Zijlstra8e182572007-07-19 01:48:54 -07001342 */
Ming Leid7aaba12009-07-16 15:44:29 +02001343static int
1344find_usage_backwards(struct lock_list *root, enum lock_usage_bit bit,
1345 struct lock_list **target_entry)
Peter Zijlstra8e182572007-07-19 01:48:54 -07001346{
Ming Leid7aaba12009-07-16 15:44:29 +02001347 int result;
Peter Zijlstra8e182572007-07-19 01:48:54 -07001348
Frederic Weisbeckerbd6d29c2010-04-06 00:10:17 +02001349 debug_atomic_inc(nr_find_usage_backwards_checks);
Peter Zijlstra8e182572007-07-19 01:48:54 -07001350
Ming Leid7aaba12009-07-16 15:44:29 +02001351 result = __bfs_backwards(root, (void *)bit, usage_match, target_entry);
Dave Jonesf82b2172008-08-11 09:30:23 +02001352
Ming Leid7aaba12009-07-16 15:44:29 +02001353 return result;
Peter Zijlstra8e182572007-07-19 01:48:54 -07001354}
1355
Peter Zijlstraaf012962009-07-16 15:44:29 +02001356static void print_lock_class_header(struct lock_class *class, int depth)
1357{
1358 int bit;
1359
1360 printk("%*s->", depth, "");
1361 print_lock_name(class);
Dmitry Vyukovf943fe02016-11-28 15:24:43 +01001362 printk(KERN_CONT " ops: %lu", class->ops);
1363 printk(KERN_CONT " {\n");
Peter Zijlstraaf012962009-07-16 15:44:29 +02001364
1365 for (bit = 0; bit < LOCK_USAGE_STATES; bit++) {
1366 if (class->usage_mask & (1 << bit)) {
1367 int len = depth;
1368
1369 len += printk("%*s %s", depth, "", usage_str[bit]);
Dmitry Vyukovf943fe02016-11-28 15:24:43 +01001370 len += printk(KERN_CONT " at:\n");
Peter Zijlstraaf012962009-07-16 15:44:29 +02001371 print_stack_trace(class->usage_traces + bit, len);
1372 }
1373 }
1374 printk("%*s }\n", depth, "");
1375
Dmitry Vyukovf943fe02016-11-28 15:24:43 +01001376 printk("%*s ... key at: [<%p>] %pS\n",
1377 depth, "", class->key, class->key);
Peter Zijlstraaf012962009-07-16 15:44:29 +02001378}
1379
1380/*
1381 * printk the shortest lock dependencies from @start to @end in reverse order:
1382 */
1383static void __used
1384print_shortest_lock_dependencies(struct lock_list *leaf,
1385 struct lock_list *root)
1386{
1387 struct lock_list *entry = leaf;
1388 int depth;
1389
1390 /*compute depth from generated tree by BFS*/
1391 depth = get_lock_depth(leaf);
1392
1393 do {
1394 print_lock_class_header(entry->class, depth);
1395 printk("%*s ... acquired at:\n", depth, "");
1396 print_stack_trace(&entry->trace, 2);
1397 printk("\n");
1398
1399 if (depth == 0 && (entry != root)) {
Steven Rostedt6be8c392011-04-20 21:41:58 -04001400 printk("lockdep:%s bad path found in chain graph\n", __func__);
Peter Zijlstraaf012962009-07-16 15:44:29 +02001401 break;
1402 }
1403
1404 entry = get_lock_parent(entry);
1405 depth--;
1406 } while (entry && (depth >= 0));
1407
1408 return;
1409}
Ming Leid7aaba12009-07-16 15:44:29 +02001410
Steven Rostedt3003eba2011-04-20 21:41:54 -04001411static void
1412print_irq_lock_scenario(struct lock_list *safe_entry,
1413 struct lock_list *unsafe_entry,
Steven Rostedtdad3d742011-04-20 21:41:57 -04001414 struct lock_class *prev_class,
1415 struct lock_class *next_class)
Steven Rostedt3003eba2011-04-20 21:41:54 -04001416{
1417 struct lock_class *safe_class = safe_entry->class;
1418 struct lock_class *unsafe_class = unsafe_entry->class;
Steven Rostedtdad3d742011-04-20 21:41:57 -04001419 struct lock_class *middle_class = prev_class;
Steven Rostedt3003eba2011-04-20 21:41:54 -04001420
1421 if (middle_class == safe_class)
Steven Rostedtdad3d742011-04-20 21:41:57 -04001422 middle_class = next_class;
Steven Rostedt3003eba2011-04-20 21:41:54 -04001423
1424 /*
1425 * A direct locking problem where unsafe_class lock is taken
1426 * directly by safe_class lock, then all we need to show
1427 * is the deadlock scenario, as it is obvious that the
1428 * unsafe lock is taken under the safe lock.
1429 *
1430 * But if there is a chain instead, where the safe lock takes
1431 * an intermediate lock (middle_class) where this lock is
1432 * not the same as the safe lock, then the lock chain is
1433 * used to describe the problem. Otherwise we would need
1434 * to show a different CPU case for each link in the chain
1435 * from the safe_class lock to the unsafe_class lock.
1436 */
1437 if (middle_class != unsafe_class) {
1438 printk("Chain exists of:\n ");
1439 __print_lock_name(safe_class);
Dmitry Vyukovf943fe02016-11-28 15:24:43 +01001440 printk(KERN_CONT " --> ");
Steven Rostedt3003eba2011-04-20 21:41:54 -04001441 __print_lock_name(middle_class);
Dmitry Vyukovf943fe02016-11-28 15:24:43 +01001442 printk(KERN_CONT " --> ");
Steven Rostedt3003eba2011-04-20 21:41:54 -04001443 __print_lock_name(unsafe_class);
Dmitry Vyukovf943fe02016-11-28 15:24:43 +01001444 printk(KERN_CONT "\n\n");
Steven Rostedt3003eba2011-04-20 21:41:54 -04001445 }
1446
1447 printk(" Possible interrupt unsafe locking scenario:\n\n");
1448 printk(" CPU0 CPU1\n");
1449 printk(" ---- ----\n");
1450 printk(" lock(");
1451 __print_lock_name(unsafe_class);
Dmitry Vyukovf943fe02016-11-28 15:24:43 +01001452 printk(KERN_CONT ");\n");
Steven Rostedt3003eba2011-04-20 21:41:54 -04001453 printk(" local_irq_disable();\n");
1454 printk(" lock(");
1455 __print_lock_name(safe_class);
Dmitry Vyukovf943fe02016-11-28 15:24:43 +01001456 printk(KERN_CONT ");\n");
Steven Rostedt3003eba2011-04-20 21:41:54 -04001457 printk(" lock(");
1458 __print_lock_name(middle_class);
Dmitry Vyukovf943fe02016-11-28 15:24:43 +01001459 printk(KERN_CONT ");\n");
Steven Rostedt3003eba2011-04-20 21:41:54 -04001460 printk(" <Interrupt>\n");
1461 printk(" lock(");
1462 __print_lock_name(safe_class);
Dmitry Vyukovf943fe02016-11-28 15:24:43 +01001463 printk(KERN_CONT ");\n");
Steven Rostedt3003eba2011-04-20 21:41:54 -04001464 printk("\n *** DEADLOCK ***\n\n");
1465}
1466
Peter Zijlstra8e182572007-07-19 01:48:54 -07001467static int
1468print_bad_irq_dependency(struct task_struct *curr,
Ming Lei24208ca2009-07-16 15:44:29 +02001469 struct lock_list *prev_root,
1470 struct lock_list *next_root,
1471 struct lock_list *backwards_entry,
1472 struct lock_list *forwards_entry,
Peter Zijlstra8e182572007-07-19 01:48:54 -07001473 struct held_lock *prev,
1474 struct held_lock *next,
1475 enum lock_usage_bit bit1,
1476 enum lock_usage_bit bit2,
1477 const char *irqclass)
1478{
1479 if (!debug_locks_off_graph_unlock() || debug_locks_silent)
1480 return 0;
1481
Paul E. McKenneyb3fbab02011-05-24 08:31:09 -07001482 printk("\n");
1483 printk("======================================================\n");
1484 printk("[ INFO: %s-safe -> %s-unsafe lock order detected ]\n",
Peter Zijlstra8e182572007-07-19 01:48:54 -07001485 irqclass, irqclass);
Ben Hutchingsfbdc4b92011-10-28 04:36:55 +01001486 print_kernel_ident();
Paul E. McKenneyb3fbab02011-05-24 08:31:09 -07001487 printk("------------------------------------------------------\n");
Peter Zijlstra8e182572007-07-19 01:48:54 -07001488 printk("%s/%d [HC%u[%lu]:SC%u[%lu]:HE%u:SE%u] is trying to acquire:\n",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07001489 curr->comm, task_pid_nr(curr),
Peter Zijlstra8e182572007-07-19 01:48:54 -07001490 curr->hardirq_context, hardirq_count() >> HARDIRQ_SHIFT,
1491 curr->softirq_context, softirq_count() >> SOFTIRQ_SHIFT,
1492 curr->hardirqs_enabled,
1493 curr->softirqs_enabled);
1494 print_lock(next);
1495
1496 printk("\nand this task is already holding:\n");
1497 print_lock(prev);
1498 printk("which would create a new lock dependency:\n");
Dave Jonesf82b2172008-08-11 09:30:23 +02001499 print_lock_name(hlock_class(prev));
Dmitry Vyukovf943fe02016-11-28 15:24:43 +01001500 printk(KERN_CONT " ->");
Dave Jonesf82b2172008-08-11 09:30:23 +02001501 print_lock_name(hlock_class(next));
Dmitry Vyukovf943fe02016-11-28 15:24:43 +01001502 printk(KERN_CONT "\n");
Peter Zijlstra8e182572007-07-19 01:48:54 -07001503
1504 printk("\nbut this new dependency connects a %s-irq-safe lock:\n",
1505 irqclass);
Ming Lei24208ca2009-07-16 15:44:29 +02001506 print_lock_name(backwards_entry->class);
Peter Zijlstra8e182572007-07-19 01:48:54 -07001507 printk("\n... which became %s-irq-safe at:\n", irqclass);
1508
Ming Lei24208ca2009-07-16 15:44:29 +02001509 print_stack_trace(backwards_entry->class->usage_traces + bit1, 1);
Peter Zijlstra8e182572007-07-19 01:48:54 -07001510
1511 printk("\nto a %s-irq-unsafe lock:\n", irqclass);
Ming Lei24208ca2009-07-16 15:44:29 +02001512 print_lock_name(forwards_entry->class);
Peter Zijlstra8e182572007-07-19 01:48:54 -07001513 printk("\n... which became %s-irq-unsafe at:\n", irqclass);
1514 printk("...");
1515
Ming Lei24208ca2009-07-16 15:44:29 +02001516 print_stack_trace(forwards_entry->class->usage_traces + bit2, 1);
Peter Zijlstra8e182572007-07-19 01:48:54 -07001517
1518 printk("\nother info that might help us debug this:\n\n");
Steven Rostedtdad3d742011-04-20 21:41:57 -04001519 print_irq_lock_scenario(backwards_entry, forwards_entry,
1520 hlock_class(prev), hlock_class(next));
Steven Rostedt3003eba2011-04-20 21:41:54 -04001521
Peter Zijlstra8e182572007-07-19 01:48:54 -07001522 lockdep_print_held_locks(curr);
1523
Dmitry Vyukovf943fe02016-11-28 15:24:43 +01001524 printk("\nthe dependencies between %s-irq-safe lock and the holding lock:\n", irqclass);
Ming Lei24208ca2009-07-16 15:44:29 +02001525 if (!save_trace(&prev_root->trace))
1526 return 0;
1527 print_shortest_lock_dependencies(backwards_entry, prev_root);
Peter Zijlstra8e182572007-07-19 01:48:54 -07001528
Ming Lei24208ca2009-07-16 15:44:29 +02001529 printk("\nthe dependencies between the lock to be acquired");
1530 printk(" and %s-irq-unsafe lock:\n", irqclass);
1531 if (!save_trace(&next_root->trace))
1532 return 0;
1533 print_shortest_lock_dependencies(forwards_entry, next_root);
Peter Zijlstra8e182572007-07-19 01:48:54 -07001534
1535 printk("\nstack backtrace:\n");
1536 dump_stack();
1537
1538 return 0;
1539}
1540
1541static int
1542check_usage(struct task_struct *curr, struct held_lock *prev,
1543 struct held_lock *next, enum lock_usage_bit bit_backwards,
1544 enum lock_usage_bit bit_forwards, const char *irqclass)
1545{
1546 int ret;
Ming Lei24208ca2009-07-16 15:44:29 +02001547 struct lock_list this, that;
Ming Leid7aaba12009-07-16 15:44:29 +02001548 struct lock_list *uninitialized_var(target_entry);
Ming Lei24208ca2009-07-16 15:44:29 +02001549 struct lock_list *uninitialized_var(target_entry1);
Peter Zijlstra8e182572007-07-19 01:48:54 -07001550
Ming Leid7aaba12009-07-16 15:44:29 +02001551 this.parent = NULL;
Peter Zijlstra8e182572007-07-19 01:48:54 -07001552
Ming Leid7aaba12009-07-16 15:44:29 +02001553 this.class = hlock_class(prev);
1554 ret = find_usage_backwards(&this, bit_backwards, &target_entry);
Peter Zijlstraaf012962009-07-16 15:44:29 +02001555 if (ret < 0)
1556 return print_bfs_bug(ret);
1557 if (ret == 1)
1558 return ret;
Ming Leid7aaba12009-07-16 15:44:29 +02001559
Ming Lei24208ca2009-07-16 15:44:29 +02001560 that.parent = NULL;
1561 that.class = hlock_class(next);
1562 ret = find_usage_forwards(&that, bit_forwards, &target_entry1);
Peter Zijlstraaf012962009-07-16 15:44:29 +02001563 if (ret < 0)
1564 return print_bfs_bug(ret);
1565 if (ret == 1)
1566 return ret;
Ming Leid7aaba12009-07-16 15:44:29 +02001567
Ming Lei24208ca2009-07-16 15:44:29 +02001568 return print_bad_irq_dependency(curr, &this, &that,
1569 target_entry, target_entry1,
1570 prev, next,
Peter Zijlstra8e182572007-07-19 01:48:54 -07001571 bit_backwards, bit_forwards, irqclass);
1572}
1573
Peter Zijlstra4f367d8a2009-01-22 18:10:42 +01001574static const char *state_names[] = {
1575#define LOCKDEP_STATE(__STATE) \
Peter Zijlstrab4b136f2009-01-29 14:50:36 +01001576 __stringify(__STATE),
Peter Zijlstra4f367d8a2009-01-22 18:10:42 +01001577#include "lockdep_states.h"
1578#undef LOCKDEP_STATE
1579};
1580
1581static const char *state_rnames[] = {
1582#define LOCKDEP_STATE(__STATE) \
Peter Zijlstrab4b136f2009-01-29 14:50:36 +01001583 __stringify(__STATE)"-READ",
Peter Zijlstra4f367d8a2009-01-22 18:10:42 +01001584#include "lockdep_states.h"
1585#undef LOCKDEP_STATE
1586};
1587
1588static inline const char *state_name(enum lock_usage_bit bit)
1589{
1590 return (bit & 1) ? state_rnames[bit >> 2] : state_names[bit >> 2];
1591}
1592
1593static int exclusive_bit(int new_bit)
1594{
1595 /*
1596 * USED_IN
1597 * USED_IN_READ
1598 * ENABLED
1599 * ENABLED_READ
1600 *
1601 * bit 0 - write/read
1602 * bit 1 - used_in/enabled
1603 * bit 2+ state
1604 */
1605
1606 int state = new_bit & ~3;
1607 int dir = new_bit & 2;
1608
1609 /*
1610 * keep state, bit flip the direction and strip read.
1611 */
1612 return state | (dir ^ 2);
1613}
1614
1615static int check_irq_usage(struct task_struct *curr, struct held_lock *prev,
1616 struct held_lock *next, enum lock_usage_bit bit)
Peter Zijlstra8e182572007-07-19 01:48:54 -07001617{
1618 /*
1619 * Prove that the new dependency does not connect a hardirq-safe
1620 * lock with a hardirq-unsafe lock - to achieve this we search
1621 * the backwards-subgraph starting at <prev>, and the
1622 * forwards-subgraph starting at <next>:
1623 */
Peter Zijlstra4f367d8a2009-01-22 18:10:42 +01001624 if (!check_usage(curr, prev, next, bit,
1625 exclusive_bit(bit), state_name(bit)))
Peter Zijlstra8e182572007-07-19 01:48:54 -07001626 return 0;
1627
Peter Zijlstra4f367d8a2009-01-22 18:10:42 +01001628 bit++; /* _READ */
1629
Peter Zijlstra8e182572007-07-19 01:48:54 -07001630 /*
1631 * Prove that the new dependency does not connect a hardirq-safe-read
1632 * lock with a hardirq-unsafe lock - to achieve this we search
1633 * the backwards-subgraph starting at <prev>, and the
1634 * forwards-subgraph starting at <next>:
1635 */
Peter Zijlstra4f367d8a2009-01-22 18:10:42 +01001636 if (!check_usage(curr, prev, next, bit,
1637 exclusive_bit(bit), state_name(bit)))
Peter Zijlstra8e182572007-07-19 01:48:54 -07001638 return 0;
1639
Peter Zijlstra4f367d8a2009-01-22 18:10:42 +01001640 return 1;
1641}
Peter Zijlstra8e182572007-07-19 01:48:54 -07001642
Peter Zijlstra4f367d8a2009-01-22 18:10:42 +01001643static int
1644check_prev_add_irq(struct task_struct *curr, struct held_lock *prev,
1645 struct held_lock *next)
1646{
1647#define LOCKDEP_STATE(__STATE) \
1648 if (!check_irq_usage(curr, prev, next, LOCK_USED_IN_##__STATE)) \
Nick Piggincf40bd12009-01-21 08:12:39 +01001649 return 0;
Peter Zijlstra4f367d8a2009-01-22 18:10:42 +01001650#include "lockdep_states.h"
1651#undef LOCKDEP_STATE
Nick Piggincf40bd12009-01-21 08:12:39 +01001652
Peter Zijlstra8e182572007-07-19 01:48:54 -07001653 return 1;
1654}
1655
1656static void inc_chains(void)
1657{
1658 if (current->hardirq_context)
1659 nr_hardirq_chains++;
1660 else {
1661 if (current->softirq_context)
1662 nr_softirq_chains++;
1663 else
1664 nr_process_chains++;
1665 }
1666}
1667
1668#else
1669
1670static inline int
1671check_prev_add_irq(struct task_struct *curr, struct held_lock *prev,
1672 struct held_lock *next)
1673{
1674 return 1;
1675}
1676
1677static inline void inc_chains(void)
1678{
1679 nr_process_chains++;
1680}
1681
1682#endif
1683
Steven Rostedt48702ec2011-04-20 21:41:56 -04001684static void
1685print_deadlock_scenario(struct held_lock *nxt,
1686 struct held_lock *prv)
1687{
1688 struct lock_class *next = hlock_class(nxt);
1689 struct lock_class *prev = hlock_class(prv);
1690
1691 printk(" Possible unsafe locking scenario:\n\n");
1692 printk(" CPU0\n");
1693 printk(" ----\n");
1694 printk(" lock(");
1695 __print_lock_name(prev);
Dmitry Vyukovf943fe02016-11-28 15:24:43 +01001696 printk(KERN_CONT ");\n");
Steven Rostedt48702ec2011-04-20 21:41:56 -04001697 printk(" lock(");
1698 __print_lock_name(next);
Dmitry Vyukovf943fe02016-11-28 15:24:43 +01001699 printk(KERN_CONT ");\n");
Steven Rostedt48702ec2011-04-20 21:41:56 -04001700 printk("\n *** DEADLOCK ***\n\n");
1701 printk(" May be due to missing lock nesting notation\n\n");
1702}
1703
Peter Zijlstra8e182572007-07-19 01:48:54 -07001704static int
1705print_deadlock_bug(struct task_struct *curr, struct held_lock *prev,
1706 struct held_lock *next)
1707{
1708 if (!debug_locks_off_graph_unlock() || debug_locks_silent)
1709 return 0;
1710
Paul E. McKenneyb3fbab02011-05-24 08:31:09 -07001711 printk("\n");
1712 printk("=============================================\n");
1713 printk("[ INFO: possible recursive locking detected ]\n");
Ben Hutchingsfbdc4b92011-10-28 04:36:55 +01001714 print_kernel_ident();
Paul E. McKenneyb3fbab02011-05-24 08:31:09 -07001715 printk("---------------------------------------------\n");
Peter Zijlstra8e182572007-07-19 01:48:54 -07001716 printk("%s/%d is trying to acquire lock:\n",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07001717 curr->comm, task_pid_nr(curr));
Peter Zijlstra8e182572007-07-19 01:48:54 -07001718 print_lock(next);
1719 printk("\nbut task is already holding lock:\n");
1720 print_lock(prev);
1721
1722 printk("\nother info that might help us debug this:\n");
Steven Rostedt48702ec2011-04-20 21:41:56 -04001723 print_deadlock_scenario(next, prev);
Peter Zijlstra8e182572007-07-19 01:48:54 -07001724 lockdep_print_held_locks(curr);
1725
1726 printk("\nstack backtrace:\n");
1727 dump_stack();
1728
1729 return 0;
1730}
1731
1732/*
1733 * Check whether we are holding such a class already.
1734 *
1735 * (Note that this has to be done separately, because the graph cannot
1736 * detect such classes of deadlocks.)
1737 *
1738 * Returns: 0 on deadlock detected, 1 on OK, 2 on recursive read
1739 */
1740static int
1741check_deadlock(struct task_struct *curr, struct held_lock *next,
1742 struct lockdep_map *next_instance, int read)
1743{
1744 struct held_lock *prev;
Peter Zijlstra7531e2f2008-08-11 09:30:24 +02001745 struct held_lock *nest = NULL;
Peter Zijlstra8e182572007-07-19 01:48:54 -07001746 int i;
1747
1748 for (i = 0; i < curr->lockdep_depth; i++) {
1749 prev = curr->held_locks + i;
Peter Zijlstra7531e2f2008-08-11 09:30:24 +02001750
1751 if (prev->instance == next->nest_lock)
1752 nest = prev;
1753
Dave Jonesf82b2172008-08-11 09:30:23 +02001754 if (hlock_class(prev) != hlock_class(next))
Peter Zijlstra8e182572007-07-19 01:48:54 -07001755 continue;
Peter Zijlstra7531e2f2008-08-11 09:30:24 +02001756
Peter Zijlstra8e182572007-07-19 01:48:54 -07001757 /*
1758 * Allow read-after-read recursion of the same
1759 * lock class (i.e. read_lock(lock)+read_lock(lock)):
1760 */
1761 if ((read == 2) && prev->read)
1762 return 2;
Peter Zijlstra7531e2f2008-08-11 09:30:24 +02001763
1764 /*
1765 * We're holding the nest_lock, which serializes this lock's
1766 * nesting behaviour.
1767 */
1768 if (nest)
1769 return 2;
1770
Peter Zijlstra8e182572007-07-19 01:48:54 -07001771 return print_deadlock_bug(curr, prev, next);
1772 }
1773 return 1;
1774}
1775
1776/*
1777 * There was a chain-cache miss, and we are about to add a new dependency
1778 * to a previous lock. We recursively validate the following rules:
1779 *
1780 * - would the adding of the <prev> -> <next> dependency create a
1781 * circular dependency in the graph? [== circular deadlock]
1782 *
1783 * - does the new prev->next dependency connect any hardirq-safe lock
1784 * (in the full backwards-subgraph starting at <prev>) with any
1785 * hardirq-unsafe lock (in the full forwards-subgraph starting at
1786 * <next>)? [== illegal lock inversion with hardirq contexts]
1787 *
1788 * - does the new prev->next dependency connect any softirq-safe lock
1789 * (in the full backwards-subgraph starting at <prev>) with any
1790 * softirq-unsafe lock (in the full forwards-subgraph starting at
1791 * <next>)? [== illegal lock inversion with softirq contexts]
1792 *
1793 * any of these scenarios could lead to a deadlock.
1794 *
1795 * Then if all the validations pass, we add the forwards and backwards
1796 * dependency.
1797 */
1798static int
1799check_prev_add(struct task_struct *curr, struct held_lock *prev,
Dmitry Vyukov8a5fd562016-02-04 14:40:40 +01001800 struct held_lock *next, int distance, int *stack_saved)
Peter Zijlstra8e182572007-07-19 01:48:54 -07001801{
1802 struct lock_list *entry;
1803 int ret;
Ming Leidb0002a2009-07-16 15:44:29 +02001804 struct lock_list this;
1805 struct lock_list *uninitialized_var(target_entry);
Yong Zhang4726f2a2010-05-04 14:16:48 +08001806 /*
1807 * Static variable, serialized by the graph_lock().
1808 *
1809 * We use this static variable to save the stack trace in case
1810 * we call into this function multiple times due to encountering
1811 * trylocks in the held lock stack.
1812 */
1813 static struct stack_trace trace;
Peter Zijlstra8e182572007-07-19 01:48:54 -07001814
1815 /*
1816 * Prove that the new <prev> -> <next> dependency would not
1817 * create a circular dependency in the graph. (We do this by
1818 * forward-recursing into the graph starting at <next>, and
1819 * checking whether we can reach <prev>.)
1820 *
1821 * We are using global variables to control the recursion, to
1822 * keep the stackframe size of the recursive functions low:
1823 */
Ming Leidb0002a2009-07-16 15:44:29 +02001824 this.class = hlock_class(next);
1825 this.parent = NULL;
1826 ret = check_noncircular(&this, hlock_class(prev), &target_entry);
1827 if (unlikely(!ret))
1828 return print_circular_bug(&this, target_entry, next, prev);
1829 else if (unlikely(ret < 0))
1830 return print_bfs_bug(ret);
Ming Leic94aa5c2009-07-16 15:44:29 +02001831
Peter Zijlstra8e182572007-07-19 01:48:54 -07001832 if (!check_prev_add_irq(curr, prev, next))
1833 return 0;
1834
1835 /*
1836 * For recursive read-locks we do all the dependency checks,
1837 * but we dont store read-triggered dependencies (only
1838 * write-triggered dependencies). This ensures that only the
1839 * write-side dependencies matter, and that if for example a
1840 * write-lock never takes any other locks, then the reads are
1841 * equivalent to a NOP.
1842 */
1843 if (next->read == 2 || prev->read == 2)
1844 return 1;
1845 /*
1846 * Is the <prev> -> <next> dependency already present?
1847 *
1848 * (this may occur even though this is a new chain: consider
1849 * e.g. the L1 -> L2 -> L3 -> L4 and the L5 -> L1 -> L2 -> L3
1850 * chains - the second one will be new, but L1 already has
1851 * L2 added to its dependency list, due to the first chain.)
1852 */
Dave Jonesf82b2172008-08-11 09:30:23 +02001853 list_for_each_entry(entry, &hlock_class(prev)->locks_after, entry) {
1854 if (entry->class == hlock_class(next)) {
Peter Zijlstra8e182572007-07-19 01:48:54 -07001855 if (distance == 1)
1856 entry->distance = 1;
1857 return 2;
1858 }
1859 }
1860
Dmitry Vyukov8a5fd562016-02-04 14:40:40 +01001861 if (!*stack_saved) {
1862 if (!save_trace(&trace))
1863 return 0;
1864 *stack_saved = 1;
1865 }
Yong Zhang4726f2a2010-05-04 14:16:48 +08001866
Peter Zijlstra8e182572007-07-19 01:48:54 -07001867 /*
1868 * Ok, all validations passed, add the new lock
1869 * to the previous lock's dependency list:
1870 */
Dave Jonesf82b2172008-08-11 09:30:23 +02001871 ret = add_lock_to_list(hlock_class(prev), hlock_class(next),
1872 &hlock_class(prev)->locks_after,
Yong Zhang4726f2a2010-05-04 14:16:48 +08001873 next->acquire_ip, distance, &trace);
Peter Zijlstra8e182572007-07-19 01:48:54 -07001874
1875 if (!ret)
1876 return 0;
1877
Dave Jonesf82b2172008-08-11 09:30:23 +02001878 ret = add_lock_to_list(hlock_class(next), hlock_class(prev),
1879 &hlock_class(next)->locks_before,
Yong Zhang4726f2a2010-05-04 14:16:48 +08001880 next->acquire_ip, distance, &trace);
Peter Zijlstra8e182572007-07-19 01:48:54 -07001881 if (!ret)
1882 return 0;
1883
1884 /*
1885 * Debugging printouts:
1886 */
Dave Jonesf82b2172008-08-11 09:30:23 +02001887 if (verbose(hlock_class(prev)) || verbose(hlock_class(next))) {
Dmitry Vyukov8a5fd562016-02-04 14:40:40 +01001888 /* We drop graph lock, so another thread can overwrite trace. */
1889 *stack_saved = 0;
Peter Zijlstra8e182572007-07-19 01:48:54 -07001890 graph_unlock();
1891 printk("\n new dependency: ");
Dave Jonesf82b2172008-08-11 09:30:23 +02001892 print_lock_name(hlock_class(prev));
Dmitry Vyukovf943fe02016-11-28 15:24:43 +01001893 printk(KERN_CONT " => ");
Dave Jonesf82b2172008-08-11 09:30:23 +02001894 print_lock_name(hlock_class(next));
Dmitry Vyukovf943fe02016-11-28 15:24:43 +01001895 printk(KERN_CONT "\n");
Peter Zijlstra8e182572007-07-19 01:48:54 -07001896 dump_stack();
1897 return graph_lock();
1898 }
1899 return 1;
1900}
1901
1902/*
1903 * Add the dependency to all directly-previous locks that are 'relevant'.
1904 * The ones that are relevant are (in increasing distance from curr):
1905 * all consecutive trylock entries and the final non-trylock entry - or
1906 * the end of this context's lock-chain - whichever comes first.
1907 */
1908static int
1909check_prevs_add(struct task_struct *curr, struct held_lock *next)
1910{
1911 int depth = curr->lockdep_depth;
Dmitry Vyukov8a5fd562016-02-04 14:40:40 +01001912 int stack_saved = 0;
Peter Zijlstra8e182572007-07-19 01:48:54 -07001913 struct held_lock *hlock;
1914
1915 /*
1916 * Debugging checks.
1917 *
1918 * Depth must not be zero for a non-head lock:
1919 */
1920 if (!depth)
1921 goto out_bug;
1922 /*
1923 * At least two relevant locks must exist for this
1924 * to be a head:
1925 */
1926 if (curr->held_locks[depth].irq_context !=
1927 curr->held_locks[depth-1].irq_context)
1928 goto out_bug;
1929
1930 for (;;) {
1931 int distance = curr->lockdep_depth - depth + 1;
Oleg Nesterov1b5ff812014-01-20 19:20:10 +01001932 hlock = curr->held_locks + depth - 1;
Peter Zijlstra8e182572007-07-19 01:48:54 -07001933 /*
1934 * Only non-recursive-read entries get new dependencies
1935 * added:
1936 */
Oleg Nesterov1b5ff812014-01-20 19:20:10 +01001937 if (hlock->read != 2 && hlock->check) {
Yong Zhang4726f2a2010-05-04 14:16:48 +08001938 if (!check_prev_add(curr, hlock, next,
Dmitry Vyukov8a5fd562016-02-04 14:40:40 +01001939 distance, &stack_saved))
Peter Zijlstra8e182572007-07-19 01:48:54 -07001940 return 0;
1941 /*
1942 * Stop after the first non-trylock entry,
1943 * as non-trylock entries have added their
1944 * own direct dependencies already, so this
1945 * lock is connected to them indirectly:
1946 */
1947 if (!hlock->trylock)
1948 break;
1949 }
1950 depth--;
1951 /*
1952 * End of lock-stack?
1953 */
1954 if (!depth)
1955 break;
1956 /*
1957 * Stop the search if we cross into another context:
1958 */
1959 if (curr->held_locks[depth].irq_context !=
1960 curr->held_locks[depth-1].irq_context)
1961 break;
1962 }
1963 return 1;
1964out_bug:
1965 if (!debug_locks_off_graph_unlock())
1966 return 0;
1967
Peter Zijlstra0119fee2011-09-02 01:30:29 +02001968 /*
1969 * Clearly we all shouldn't be here, but since we made it we
1970 * can reliable say we messed up our state. See the above two
1971 * gotos for reasons why we could possibly end up here.
1972 */
Peter Zijlstra8e182572007-07-19 01:48:54 -07001973 WARN_ON(1);
1974
1975 return 0;
1976}
1977
1978unsigned long nr_lock_chains;
Huang, Ying443cd502008-06-20 16:39:21 +08001979struct lock_chain lock_chains[MAX_LOCKDEP_CHAINS];
Huang, Yingcd1a28e2008-06-23 11:20:54 +08001980int nr_chain_hlocks;
Huang, Ying443cd502008-06-20 16:39:21 +08001981static u16 chain_hlocks[MAX_LOCKDEP_CHAIN_HLOCKS];
1982
1983struct lock_class *lock_chain_get_class(struct lock_chain *chain, int i)
1984{
1985 return lock_classes + chain_hlocks[chain->base + i];
1986}
Peter Zijlstra8e182572007-07-19 01:48:54 -07001987
1988/*
Ingo Molnar9e4e7552016-02-29 10:03:58 +01001989 * Returns the index of the first held_lock of the current chain
1990 */
1991static inline int get_first_held_lock(struct task_struct *curr,
1992 struct held_lock *hlock)
1993{
1994 int i;
1995 struct held_lock *hlock_curr;
1996
1997 for (i = curr->lockdep_depth - 1; i >= 0; i--) {
1998 hlock_curr = curr->held_locks + i;
1999 if (hlock_curr->irq_context != hlock->irq_context)
2000 break;
2001
2002 }
2003
2004 return ++i;
2005}
2006
Borislav Petkov5c8a0102016-04-04 10:42:07 +02002007#ifdef CONFIG_DEBUG_LOCKDEP
Ingo Molnar9e4e7552016-02-29 10:03:58 +01002008/*
Alfredo Alvarez Fernandez39e2e172016-03-30 19:03:36 +02002009 * Returns the next chain_key iteration
2010 */
2011static u64 print_chain_key_iteration(int class_idx, u64 chain_key)
2012{
2013 u64 new_chain_key = iterate_chain_key(chain_key, class_idx);
2014
2015 printk(" class_idx:%d -> chain_key:%016Lx",
2016 class_idx,
2017 (unsigned long long)new_chain_key);
2018 return new_chain_key;
2019}
2020
2021static void
2022print_chain_keys_held_locks(struct task_struct *curr, struct held_lock *hlock_next)
2023{
2024 struct held_lock *hlock;
2025 u64 chain_key = 0;
2026 int depth = curr->lockdep_depth;
2027 int i;
2028
2029 printk("depth: %u\n", depth + 1);
2030 for (i = get_first_held_lock(curr, hlock_next); i < depth; i++) {
2031 hlock = curr->held_locks + i;
2032 chain_key = print_chain_key_iteration(hlock->class_idx, chain_key);
2033
2034 print_lock(hlock);
2035 }
2036
2037 print_chain_key_iteration(hlock_next->class_idx, chain_key);
2038 print_lock(hlock_next);
2039}
2040
2041static void print_chain_keys_chain(struct lock_chain *chain)
2042{
2043 int i;
2044 u64 chain_key = 0;
2045 int class_id;
2046
2047 printk("depth: %u\n", chain->depth);
2048 for (i = 0; i < chain->depth; i++) {
2049 class_id = chain_hlocks[chain->base + i];
2050 chain_key = print_chain_key_iteration(class_id + 1, chain_key);
2051
2052 print_lock_name(lock_classes + class_id);
2053 printk("\n");
2054 }
2055}
2056
2057static void print_collision(struct task_struct *curr,
2058 struct held_lock *hlock_next,
2059 struct lock_chain *chain)
2060{
2061 printk("\n");
2062 printk("======================\n");
2063 printk("[chain_key collision ]\n");
2064 print_kernel_ident();
2065 printk("----------------------\n");
2066 printk("%s/%d: ", current->comm, task_pid_nr(current));
2067 printk("Hash chain already cached but the contents don't match!\n");
2068
2069 printk("Held locks:");
2070 print_chain_keys_held_locks(curr, hlock_next);
2071
2072 printk("Locks in cached chain:");
2073 print_chain_keys_chain(chain);
2074
2075 printk("\nstack backtrace:\n");
2076 dump_stack();
2077}
Borislav Petkov5c8a0102016-04-04 10:42:07 +02002078#endif
Alfredo Alvarez Fernandez39e2e172016-03-30 19:03:36 +02002079
2080/*
Ingo Molnar9e4e7552016-02-29 10:03:58 +01002081 * Checks whether the chain and the current held locks are consistent
2082 * in depth and also in content. If they are not it most likely means
2083 * that there was a collision during the calculation of the chain_key.
2084 * Returns: 0 not passed, 1 passed
2085 */
2086static int check_no_collision(struct task_struct *curr,
2087 struct held_lock *hlock,
2088 struct lock_chain *chain)
2089{
2090#ifdef CONFIG_DEBUG_LOCKDEP
2091 int i, j, id;
2092
2093 i = get_first_held_lock(curr, hlock);
2094
Alfredo Alvarez Fernandez39e2e172016-03-30 19:03:36 +02002095 if (DEBUG_LOCKS_WARN_ON(chain->depth != curr->lockdep_depth - (i - 1))) {
2096 print_collision(curr, hlock, chain);
Ingo Molnar9e4e7552016-02-29 10:03:58 +01002097 return 0;
Alfredo Alvarez Fernandez39e2e172016-03-30 19:03:36 +02002098 }
Ingo Molnar9e4e7552016-02-29 10:03:58 +01002099
2100 for (j = 0; j < chain->depth - 1; j++, i++) {
2101 id = curr->held_locks[i].class_idx - 1;
2102
Alfredo Alvarez Fernandez39e2e172016-03-30 19:03:36 +02002103 if (DEBUG_LOCKS_WARN_ON(chain_hlocks[chain->base + j] != id)) {
2104 print_collision(curr, hlock, chain);
Ingo Molnar9e4e7552016-02-29 10:03:58 +01002105 return 0;
Alfredo Alvarez Fernandez39e2e172016-03-30 19:03:36 +02002106 }
Ingo Molnar9e4e7552016-02-29 10:03:58 +01002107 }
2108#endif
2109 return 1;
2110}
2111
2112/*
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002113 * Look up a dependency chain. If the key is not present yet then
Jarek Poplawski9e860d02007-05-08 00:30:12 -07002114 * add it and return 1 - in this case the new dependency chain is
2115 * validated. If the key is already hashed, return 0.
2116 * (On return with 1 graph_lock is held.)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002117 */
Huang, Ying443cd502008-06-20 16:39:21 +08002118static inline int lookup_chain_cache(struct task_struct *curr,
2119 struct held_lock *hlock,
2120 u64 chain_key)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002121{
Dave Jonesf82b2172008-08-11 09:30:23 +02002122 struct lock_class *class = hlock_class(hlock);
Andrew Mortona63f38c2016-02-03 13:44:12 -08002123 struct hlist_head *hash_head = chainhashentry(chain_key);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002124 struct lock_chain *chain;
Steven Rostedte0944ee2011-04-20 21:42:00 -04002125 int i, j;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002126
Peter Zijlstra0119fee2011-09-02 01:30:29 +02002127 /*
2128 * We might need to take the graph lock, ensure we've got IRQs
2129 * disabled to make this an IRQ-safe lock.. for recursion reasons
2130 * lockdep won't complain about its own locking errors.
2131 */
Jarek Poplawski381a2292007-02-10 01:44:58 -08002132 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2133 return 0;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002134 /*
2135 * We can walk it lock-free, because entries only get added
2136 * to the hash:
2137 */
Andrew Mortona63f38c2016-02-03 13:44:12 -08002138 hlist_for_each_entry_rcu(chain, hash_head, entry) {
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002139 if (chain->chain_key == chain_key) {
2140cache_hit:
Frederic Weisbeckerbd6d29c2010-04-06 00:10:17 +02002141 debug_atomic_inc(chain_lookup_hits);
Ingo Molnar9e4e7552016-02-29 10:03:58 +01002142 if (!check_no_collision(curr, hlock, chain))
2143 return 0;
2144
Ingo Molnar81fc6852006-12-13 00:34:40 -08002145 if (very_verbose(class))
Andrew Morton755cd902006-12-29 16:49:14 -08002146 printk("\nhash chain already cached, key: "
2147 "%016Lx tail class: [%p] %s\n",
2148 (unsigned long long)chain_key,
2149 class->key, class->name);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002150 return 0;
2151 }
2152 }
Ingo Molnar81fc6852006-12-13 00:34:40 -08002153 if (very_verbose(class))
Andrew Morton755cd902006-12-29 16:49:14 -08002154 printk("\nnew hash chain, key: %016Lx tail class: [%p] %s\n",
2155 (unsigned long long)chain_key, class->key, class->name);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002156 /*
2157 * Allocate a new chain entry from the static array, and add
2158 * it to the hash:
2159 */
Ingo Molnar74c383f2006-12-13 00:34:43 -08002160 if (!graph_lock())
2161 return 0;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002162 /*
2163 * We have to walk the chain again locked - to avoid duplicates:
2164 */
Andrew Mortona63f38c2016-02-03 13:44:12 -08002165 hlist_for_each_entry(chain, hash_head, entry) {
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002166 if (chain->chain_key == chain_key) {
Ingo Molnar74c383f2006-12-13 00:34:43 -08002167 graph_unlock();
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002168 goto cache_hit;
2169 }
2170 }
2171 if (unlikely(nr_lock_chains >= MAX_LOCKDEP_CHAINS)) {
Ingo Molnar74c383f2006-12-13 00:34:43 -08002172 if (!debug_locks_off_graph_unlock())
2173 return 0;
2174
Dave Jones2c522832013-04-25 13:40:02 -04002175 print_lockdep_off("BUG: MAX_LOCKDEP_CHAINS too low!");
Peter Zijlstraeedeeab2009-03-18 12:38:47 +01002176 dump_stack();
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002177 return 0;
2178 }
2179 chain = lock_chains + nr_lock_chains++;
2180 chain->chain_key = chain_key;
Huang, Ying443cd502008-06-20 16:39:21 +08002181 chain->irq_context = hlock->irq_context;
Ingo Molnar9e4e7552016-02-29 10:03:58 +01002182 i = get_first_held_lock(curr, hlock);
Huang, Ying443cd502008-06-20 16:39:21 +08002183 chain->depth = curr->lockdep_depth + 1 - i;
Peter Zijlstra75dd6022016-03-30 11:36:59 +02002184
2185 BUILD_BUG_ON((1UL << 24) <= ARRAY_SIZE(chain_hlocks));
2186 BUILD_BUG_ON((1UL << 6) <= ARRAY_SIZE(curr->held_locks));
2187 BUILD_BUG_ON((1UL << 8*sizeof(chain_hlocks[0])) <= ARRAY_SIZE(lock_classes));
2188
Steven Rostedte0944ee2011-04-20 21:42:00 -04002189 if (likely(nr_chain_hlocks + chain->depth <= MAX_LOCKDEP_CHAIN_HLOCKS)) {
2190 chain->base = nr_chain_hlocks;
Huang, Ying443cd502008-06-20 16:39:21 +08002191 for (j = 0; j < chain->depth - 1; j++, i++) {
Dave Jonesf82b2172008-08-11 09:30:23 +02002192 int lock_id = curr->held_locks[i].class_idx - 1;
Huang, Ying443cd502008-06-20 16:39:21 +08002193 chain_hlocks[chain->base + j] = lock_id;
2194 }
2195 chain_hlocks[chain->base + j] = class - lock_classes;
2196 }
Peter Zijlstra75dd6022016-03-30 11:36:59 +02002197
2198 if (nr_chain_hlocks < MAX_LOCKDEP_CHAIN_HLOCKS)
2199 nr_chain_hlocks += chain->depth;
2200
2201#ifdef CONFIG_DEBUG_LOCKDEP
2202 /*
2203 * Important for check_no_collision().
2204 */
2205 if (unlikely(nr_chain_hlocks > MAX_LOCKDEP_CHAIN_HLOCKS)) {
2206 if (debug_locks_off_graph_unlock())
2207 return 0;
2208
2209 print_lockdep_off("BUG: MAX_LOCKDEP_CHAIN_HLOCKS too low!");
2210 dump_stack();
2211 return 0;
2212 }
2213#endif
2214
Andrew Mortona63f38c2016-02-03 13:44:12 -08002215 hlist_add_head_rcu(&chain->entry, hash_head);
Frederic Weisbeckerbd6d29c2010-04-06 00:10:17 +02002216 debug_atomic_inc(chain_lookup_misses);
Peter Zijlstra8e182572007-07-19 01:48:54 -07002217 inc_chains();
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002218
2219 return 1;
2220}
Peter Zijlstra8e182572007-07-19 01:48:54 -07002221
2222static int validate_chain(struct task_struct *curr, struct lockdep_map *lock,
Johannes Berg4e6045f2007-10-18 23:39:55 -07002223 struct held_lock *hlock, int chain_head, u64 chain_key)
Peter Zijlstra8e182572007-07-19 01:48:54 -07002224{
2225 /*
2226 * Trylock needs to maintain the stack of held locks, but it
2227 * does not add new dependencies, because trylock can be done
2228 * in any order.
2229 *
2230 * We look up the chain_key and do the O(N^2) check and update of
2231 * the dependencies only if this is a new dependency chain.
2232 * (If lookup_chain_cache() returns with 1 it acquires
2233 * graph_lock for us)
2234 */
Oleg Nesterovfb9edbe2014-01-20 19:20:06 +01002235 if (!hlock->trylock && hlock->check &&
Huang, Ying443cd502008-06-20 16:39:21 +08002236 lookup_chain_cache(curr, hlock, chain_key)) {
Peter Zijlstra8e182572007-07-19 01:48:54 -07002237 /*
2238 * Check whether last held lock:
2239 *
2240 * - is irq-safe, if this lock is irq-unsafe
2241 * - is softirq-safe, if this lock is hardirq-unsafe
2242 *
2243 * And check whether the new lock's dependency graph
2244 * could lead back to the previous lock.
2245 *
2246 * any of these scenarios could lead to a deadlock. If
2247 * All validations
2248 */
2249 int ret = check_deadlock(curr, hlock, lock, hlock->read);
2250
2251 if (!ret)
2252 return 0;
2253 /*
2254 * Mark recursive read, as we jump over it when
2255 * building dependencies (just like we jump over
2256 * trylock entries):
2257 */
2258 if (ret == 2)
2259 hlock->read = 2;
2260 /*
2261 * Add dependency only if this lock is not the head
2262 * of the chain, and if it's not a secondary read-lock:
2263 */
2264 if (!chain_head && ret != 2)
2265 if (!check_prevs_add(curr, hlock))
2266 return 0;
2267 graph_unlock();
2268 } else
2269 /* after lookup_chain_cache(): */
2270 if (unlikely(!debug_locks))
2271 return 0;
2272
2273 return 1;
2274}
2275#else
2276static inline int validate_chain(struct task_struct *curr,
2277 struct lockdep_map *lock, struct held_lock *hlock,
Gregory Haskins3aa416b2007-10-11 22:11:11 +02002278 int chain_head, u64 chain_key)
Peter Zijlstra8e182572007-07-19 01:48:54 -07002279{
2280 return 1;
2281}
Peter Zijlstraca58abc2007-07-19 01:48:53 -07002282#endif
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002283
2284/*
2285 * We are building curr_chain_key incrementally, so double-check
2286 * it from scratch, to make sure that it's done correctly:
2287 */
Steven Rostedt1d09daa2008-05-12 21:20:55 +02002288static void check_chain_key(struct task_struct *curr)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002289{
2290#ifdef CONFIG_DEBUG_LOCKDEP
2291 struct held_lock *hlock, *prev_hlock = NULL;
Alfredo Alvarez Fernandez5f18ab52016-02-11 00:33:32 +01002292 unsigned int i;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002293 u64 chain_key = 0;
2294
2295 for (i = 0; i < curr->lockdep_depth; i++) {
2296 hlock = curr->held_locks + i;
2297 if (chain_key != hlock->prev_chain_key) {
2298 debug_locks_off();
Peter Zijlstra0119fee2011-09-02 01:30:29 +02002299 /*
2300 * We got mighty confused, our chain keys don't match
2301 * with what we expect, someone trample on our task state?
2302 */
Arjan van de Ven2df8b1d2008-07-30 12:43:11 -07002303 WARN(1, "hm#1, depth: %u [%u], %016Lx != %016Lx\n",
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002304 curr->lockdep_depth, i,
2305 (unsigned long long)chain_key,
2306 (unsigned long long)hlock->prev_chain_key);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002307 return;
2308 }
Peter Zijlstra0119fee2011-09-02 01:30:29 +02002309 /*
2310 * Whoops ran out of static storage again?
2311 */
Alfredo Alvarez Fernandez5f18ab52016-02-11 00:33:32 +01002312 if (DEBUG_LOCKS_WARN_ON(hlock->class_idx > MAX_LOCKDEP_KEYS))
Jarek Poplawski381a2292007-02-10 01:44:58 -08002313 return;
2314
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002315 if (prev_hlock && (prev_hlock->irq_context !=
2316 hlock->irq_context))
2317 chain_key = 0;
Alfredo Alvarez Fernandez5f18ab52016-02-11 00:33:32 +01002318 chain_key = iterate_chain_key(chain_key, hlock->class_idx);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002319 prev_hlock = hlock;
2320 }
2321 if (chain_key != curr->curr_chain_key) {
2322 debug_locks_off();
Peter Zijlstra0119fee2011-09-02 01:30:29 +02002323 /*
2324 * More smoking hash instead of calculating it, damn see these
2325 * numbers float.. I bet that a pink elephant stepped on my memory.
2326 */
Arjan van de Ven2df8b1d2008-07-30 12:43:11 -07002327 WARN(1, "hm#2, depth: %u [%u], %016Lx != %016Lx\n",
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002328 curr->lockdep_depth, i,
2329 (unsigned long long)chain_key,
2330 (unsigned long long)curr->curr_chain_key);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002331 }
2332#endif
2333}
2334
Steven Rostedt282b5c22011-04-20 21:41:59 -04002335static void
2336print_usage_bug_scenario(struct held_lock *lock)
2337{
2338 struct lock_class *class = hlock_class(lock);
2339
2340 printk(" Possible unsafe locking scenario:\n\n");
2341 printk(" CPU0\n");
2342 printk(" ----\n");
2343 printk(" lock(");
2344 __print_lock_name(class);
Dmitry Vyukovf943fe02016-11-28 15:24:43 +01002345 printk(KERN_CONT ");\n");
Steven Rostedt282b5c22011-04-20 21:41:59 -04002346 printk(" <Interrupt>\n");
2347 printk(" lock(");
2348 __print_lock_name(class);
Dmitry Vyukovf943fe02016-11-28 15:24:43 +01002349 printk(KERN_CONT ");\n");
Steven Rostedt282b5c22011-04-20 21:41:59 -04002350 printk("\n *** DEADLOCK ***\n\n");
2351}
2352
Peter Zijlstra8e182572007-07-19 01:48:54 -07002353static int
2354print_usage_bug(struct task_struct *curr, struct held_lock *this,
2355 enum lock_usage_bit prev_bit, enum lock_usage_bit new_bit)
2356{
2357 if (!debug_locks_off_graph_unlock() || debug_locks_silent)
2358 return 0;
2359
Paul E. McKenneyb3fbab02011-05-24 08:31:09 -07002360 printk("\n");
2361 printk("=================================\n");
2362 printk("[ INFO: inconsistent lock state ]\n");
Ben Hutchingsfbdc4b92011-10-28 04:36:55 +01002363 print_kernel_ident();
Paul E. McKenneyb3fbab02011-05-24 08:31:09 -07002364 printk("---------------------------------\n");
Peter Zijlstra8e182572007-07-19 01:48:54 -07002365
2366 printk("inconsistent {%s} -> {%s} usage.\n",
2367 usage_str[prev_bit], usage_str[new_bit]);
2368
2369 printk("%s/%d [HC%u[%lu]:SC%u[%lu]:HE%u:SE%u] takes:\n",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07002370 curr->comm, task_pid_nr(curr),
Peter Zijlstra8e182572007-07-19 01:48:54 -07002371 trace_hardirq_context(curr), hardirq_count() >> HARDIRQ_SHIFT,
2372 trace_softirq_context(curr), softirq_count() >> SOFTIRQ_SHIFT,
2373 trace_hardirqs_enabled(curr),
2374 trace_softirqs_enabled(curr));
2375 print_lock(this);
2376
2377 printk("{%s} state was registered at:\n", usage_str[prev_bit]);
Dave Jonesf82b2172008-08-11 09:30:23 +02002378 print_stack_trace(hlock_class(this)->usage_traces + prev_bit, 1);
Peter Zijlstra8e182572007-07-19 01:48:54 -07002379
2380 print_irqtrace_events(curr);
2381 printk("\nother info that might help us debug this:\n");
Steven Rostedt282b5c22011-04-20 21:41:59 -04002382 print_usage_bug_scenario(this);
2383
Peter Zijlstra8e182572007-07-19 01:48:54 -07002384 lockdep_print_held_locks(curr);
2385
2386 printk("\nstack backtrace:\n");
2387 dump_stack();
2388
2389 return 0;
2390}
2391
2392/*
2393 * Print out an error if an invalid bit is set:
2394 */
2395static inline int
2396valid_state(struct task_struct *curr, struct held_lock *this,
2397 enum lock_usage_bit new_bit, enum lock_usage_bit bad_bit)
2398{
Dave Jonesf82b2172008-08-11 09:30:23 +02002399 if (unlikely(hlock_class(this)->usage_mask & (1 << bad_bit)))
Peter Zijlstra8e182572007-07-19 01:48:54 -07002400 return print_usage_bug(curr, this, bad_bit, new_bit);
2401 return 1;
2402}
2403
2404static int mark_lock(struct task_struct *curr, struct held_lock *this,
2405 enum lock_usage_bit new_bit);
2406
Steven Rostedt81d68a92008-05-12 21:20:42 +02002407#if defined(CONFIG_TRACE_IRQFLAGS) && defined(CONFIG_PROVE_LOCKING)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002408
2409/*
2410 * print irq inversion bug:
2411 */
2412static int
Ming Lei24208ca2009-07-16 15:44:29 +02002413print_irq_inversion_bug(struct task_struct *curr,
2414 struct lock_list *root, struct lock_list *other,
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002415 struct held_lock *this, int forwards,
2416 const char *irqclass)
2417{
Steven Rostedtdad3d742011-04-20 21:41:57 -04002418 struct lock_list *entry = other;
2419 struct lock_list *middle = NULL;
2420 int depth;
2421
Ingo Molnar74c383f2006-12-13 00:34:43 -08002422 if (!debug_locks_off_graph_unlock() || debug_locks_silent)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002423 return 0;
2424
Paul E. McKenneyb3fbab02011-05-24 08:31:09 -07002425 printk("\n");
2426 printk("=========================================================\n");
2427 printk("[ INFO: possible irq lock inversion dependency detected ]\n");
Ben Hutchingsfbdc4b92011-10-28 04:36:55 +01002428 print_kernel_ident();
Paul E. McKenneyb3fbab02011-05-24 08:31:09 -07002429 printk("---------------------------------------------------------\n");
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002430 printk("%s/%d just changed the state of lock:\n",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07002431 curr->comm, task_pid_nr(curr));
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002432 print_lock(this);
2433 if (forwards)
Peter Zijlstra26575e22009-03-04 14:53:24 +01002434 printk("but this lock took another, %s-unsafe lock in the past:\n", irqclass);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002435 else
Peter Zijlstra26575e22009-03-04 14:53:24 +01002436 printk("but this lock was taken by another, %s-safe lock in the past:\n", irqclass);
Ming Lei24208ca2009-07-16 15:44:29 +02002437 print_lock_name(other->class);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002438 printk("\n\nand interrupts could create inverse lock ordering between them.\n\n");
2439
2440 printk("\nother info that might help us debug this:\n");
Steven Rostedtdad3d742011-04-20 21:41:57 -04002441
2442 /* Find a middle lock (if one exists) */
2443 depth = get_lock_depth(other);
2444 do {
2445 if (depth == 0 && (entry != root)) {
2446 printk("lockdep:%s bad path found in chain graph\n", __func__);
2447 break;
2448 }
2449 middle = entry;
2450 entry = get_lock_parent(entry);
2451 depth--;
2452 } while (entry && entry != root && (depth >= 0));
2453 if (forwards)
2454 print_irq_lock_scenario(root, other,
2455 middle ? middle->class : root->class, other->class);
2456 else
2457 print_irq_lock_scenario(other, root,
2458 middle ? middle->class : other->class, root->class);
2459
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002460 lockdep_print_held_locks(curr);
2461
Ming Lei24208ca2009-07-16 15:44:29 +02002462 printk("\nthe shortest dependencies between 2nd lock and 1st lock:\n");
2463 if (!save_trace(&root->trace))
2464 return 0;
2465 print_shortest_lock_dependencies(other, root);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002466
2467 printk("\nstack backtrace:\n");
2468 dump_stack();
2469
2470 return 0;
2471}
2472
2473/*
2474 * Prove that in the forwards-direction subgraph starting at <this>
2475 * there is no lock matching <mask>:
2476 */
2477static int
2478check_usage_forwards(struct task_struct *curr, struct held_lock *this,
2479 enum lock_usage_bit bit, const char *irqclass)
2480{
2481 int ret;
Ming Leid7aaba12009-07-16 15:44:29 +02002482 struct lock_list root;
2483 struct lock_list *uninitialized_var(target_entry);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002484
Ming Leid7aaba12009-07-16 15:44:29 +02002485 root.parent = NULL;
2486 root.class = hlock_class(this);
2487 ret = find_usage_forwards(&root, bit, &target_entry);
Peter Zijlstraaf012962009-07-16 15:44:29 +02002488 if (ret < 0)
2489 return print_bfs_bug(ret);
2490 if (ret == 1)
2491 return ret;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002492
Ming Lei24208ca2009-07-16 15:44:29 +02002493 return print_irq_inversion_bug(curr, &root, target_entry,
Ming Leid7aaba12009-07-16 15:44:29 +02002494 this, 1, irqclass);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002495}
2496
2497/*
2498 * Prove that in the backwards-direction subgraph starting at <this>
2499 * there is no lock matching <mask>:
2500 */
2501static int
2502check_usage_backwards(struct task_struct *curr, struct held_lock *this,
2503 enum lock_usage_bit bit, const char *irqclass)
2504{
2505 int ret;
Ming Leid7aaba12009-07-16 15:44:29 +02002506 struct lock_list root;
2507 struct lock_list *uninitialized_var(target_entry);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002508
Ming Leid7aaba12009-07-16 15:44:29 +02002509 root.parent = NULL;
2510 root.class = hlock_class(this);
2511 ret = find_usage_backwards(&root, bit, &target_entry);
Peter Zijlstraaf012962009-07-16 15:44:29 +02002512 if (ret < 0)
2513 return print_bfs_bug(ret);
2514 if (ret == 1)
2515 return ret;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002516
Ming Lei24208ca2009-07-16 15:44:29 +02002517 return print_irq_inversion_bug(curr, &root, target_entry,
Oleg Nesterov48d50672010-01-26 19:16:41 +01002518 this, 0, irqclass);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002519}
2520
Ingo Molnar3117df02006-12-13 00:34:43 -08002521void print_irqtrace_events(struct task_struct *curr)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002522{
2523 printk("irq event stamp: %u\n", curr->irq_events);
Dmitry Vyukovf943fe02016-11-28 15:24:43 +01002524 printk("hardirqs last enabled at (%u): [<%p>] %pS\n",
2525 curr->hardirq_enable_event, (void *)curr->hardirq_enable_ip,
2526 (void *)curr->hardirq_enable_ip);
2527 printk("hardirqs last disabled at (%u): [<%p>] %pS\n",
2528 curr->hardirq_disable_event, (void *)curr->hardirq_disable_ip,
2529 (void *)curr->hardirq_disable_ip);
2530 printk("softirqs last enabled at (%u): [<%p>] %pS\n",
2531 curr->softirq_enable_event, (void *)curr->softirq_enable_ip,
2532 (void *)curr->softirq_enable_ip);
2533 printk("softirqs last disabled at (%u): [<%p>] %pS\n",
2534 curr->softirq_disable_event, (void *)curr->softirq_disable_ip,
2535 (void *)curr->softirq_disable_ip);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002536}
2537
Peter Zijlstracd953022009-01-22 16:38:21 +01002538static int HARDIRQ_verbose(struct lock_class *class)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002539{
Peter Zijlstra8e182572007-07-19 01:48:54 -07002540#if HARDIRQ_VERBOSE
2541 return class_filter(class);
2542#endif
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002543 return 0;
2544}
2545
Peter Zijlstracd953022009-01-22 16:38:21 +01002546static int SOFTIRQ_verbose(struct lock_class *class)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002547{
Peter Zijlstra8e182572007-07-19 01:48:54 -07002548#if SOFTIRQ_VERBOSE
2549 return class_filter(class);
2550#endif
2551 return 0;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002552}
2553
Peter Zijlstracd953022009-01-22 16:38:21 +01002554static int RECLAIM_FS_verbose(struct lock_class *class)
Nick Piggincf40bd12009-01-21 08:12:39 +01002555{
2556#if RECLAIM_VERBOSE
2557 return class_filter(class);
2558#endif
2559 return 0;
2560}
2561
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002562#define STRICT_READ_CHECKS 1
2563
Peter Zijlstracd953022009-01-22 16:38:21 +01002564static int (*state_verbose_f[])(struct lock_class *class) = {
2565#define LOCKDEP_STATE(__STATE) \
2566 __STATE##_verbose,
2567#include "lockdep_states.h"
2568#undef LOCKDEP_STATE
2569};
2570
2571static inline int state_verbose(enum lock_usage_bit bit,
2572 struct lock_class *class)
2573{
2574 return state_verbose_f[bit >> 2](class);
2575}
2576
Peter Zijlstra42c50d52009-01-22 16:58:16 +01002577typedef int (*check_usage_f)(struct task_struct *, struct held_lock *,
2578 enum lock_usage_bit bit, const char *name);
2579
Peter Zijlstra6a6904d2009-01-22 16:07:44 +01002580static int
Peter Zijlstra1c21f142009-03-04 13:51:13 +01002581mark_lock_irq(struct task_struct *curr, struct held_lock *this,
2582 enum lock_usage_bit new_bit)
Peter Zijlstra6a6904d2009-01-22 16:07:44 +01002583{
Peter Zijlstraf9892092009-01-22 16:09:59 +01002584 int excl_bit = exclusive_bit(new_bit);
Peter Zijlstra9d3651a2009-01-22 17:18:32 +01002585 int read = new_bit & 1;
Peter Zijlstra42c50d52009-01-22 16:58:16 +01002586 int dir = new_bit & 2;
2587
Peter Zijlstra38aa2712009-01-27 14:53:50 +01002588 /*
2589 * mark USED_IN has to look forwards -- to ensure no dependency
2590 * has ENABLED state, which would allow recursion deadlocks.
2591 *
2592 * mark ENABLED has to look backwards -- to ensure no dependee
2593 * has USED_IN state, which, again, would allow recursion deadlocks.
2594 */
Peter Zijlstra42c50d52009-01-22 16:58:16 +01002595 check_usage_f usage = dir ?
2596 check_usage_backwards : check_usage_forwards;
Peter Zijlstraf9892092009-01-22 16:09:59 +01002597
Peter Zijlstra38aa2712009-01-27 14:53:50 +01002598 /*
2599 * Validate that this particular lock does not have conflicting
2600 * usage states.
2601 */
Peter Zijlstra6a6904d2009-01-22 16:07:44 +01002602 if (!valid_state(curr, this, new_bit, excl_bit))
2603 return 0;
Peter Zijlstra9d3651a2009-01-22 17:18:32 +01002604
Peter Zijlstra38aa2712009-01-27 14:53:50 +01002605 /*
2606 * Validate that the lock dependencies don't have conflicting usage
2607 * states.
2608 */
2609 if ((!read || !dir || STRICT_READ_CHECKS) &&
Peter Zijlstra1c21f142009-03-04 13:51:13 +01002610 !usage(curr, this, excl_bit, state_name(new_bit & ~1)))
Peter Zijlstra6a6904d2009-01-22 16:07:44 +01002611 return 0;
Peter Zijlstra780e8202009-01-22 16:51:29 +01002612
Peter Zijlstra38aa2712009-01-27 14:53:50 +01002613 /*
2614 * Check for read in write conflicts
2615 */
2616 if (!read) {
2617 if (!valid_state(curr, this, new_bit, excl_bit + 1))
2618 return 0;
2619
2620 if (STRICT_READ_CHECKS &&
Peter Zijlstra4f367d8a2009-01-22 18:10:42 +01002621 !usage(curr, this, excl_bit + 1,
2622 state_name(new_bit + 1)))
Peter Zijlstra38aa2712009-01-27 14:53:50 +01002623 return 0;
2624 }
Peter Zijlstra780e8202009-01-22 16:51:29 +01002625
Peter Zijlstracd953022009-01-22 16:38:21 +01002626 if (state_verbose(new_bit, hlock_class(this)))
Peter Zijlstra6a6904d2009-01-22 16:07:44 +01002627 return 2;
2628
2629 return 1;
2630}
2631
Nick Piggincf40bd12009-01-21 08:12:39 +01002632enum mark_type {
Peter Zijlstra36bfb9b2009-01-22 14:12:41 +01002633#define LOCKDEP_STATE(__STATE) __STATE,
2634#include "lockdep_states.h"
2635#undef LOCKDEP_STATE
Nick Piggincf40bd12009-01-21 08:12:39 +01002636};
2637
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002638/*
2639 * Mark all held locks with a usage bit:
2640 */
Steven Rostedt1d09daa2008-05-12 21:20:55 +02002641static int
Nick Piggincf40bd12009-01-21 08:12:39 +01002642mark_held_locks(struct task_struct *curr, enum mark_type mark)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002643{
2644 enum lock_usage_bit usage_bit;
2645 struct held_lock *hlock;
2646 int i;
2647
2648 for (i = 0; i < curr->lockdep_depth; i++) {
2649 hlock = curr->held_locks + i;
2650
Peter Zijlstracf2ad4d2009-01-27 13:58:08 +01002651 usage_bit = 2 + (mark << 2); /* ENABLED */
2652 if (hlock->read)
2653 usage_bit += 1; /* READ */
2654
2655 BUG_ON(usage_bit >= LOCK_USAGE_STATES);
Nick Piggincf40bd12009-01-21 08:12:39 +01002656
Oleg Nesterov34d0ed52014-01-20 19:20:13 +01002657 if (!hlock->check)
Peter Zijlstraefbe2ee2011-07-07 11:39:45 +02002658 continue;
2659
Jarek Poplawski4ff773bb2007-05-08 00:31:00 -07002660 if (!mark_lock(curr, hlock, usage_bit))
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002661 return 0;
2662 }
2663
2664 return 1;
2665}
2666
2667/*
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002668 * Hardirqs will be enabled:
2669 */
Peter Zijlstradd4e5d32011-06-21 17:17:27 +02002670static void __trace_hardirqs_on_caller(unsigned long ip)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002671{
2672 struct task_struct *curr = current;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002673
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002674 /* we'll do an OFF -> ON transition: */
2675 curr->hardirqs_enabled = 1;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002676
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002677 /*
2678 * We are going to turn hardirqs on, so set the
2679 * usage bit for all held locks:
2680 */
Nick Piggincf40bd12009-01-21 08:12:39 +01002681 if (!mark_held_locks(curr, HARDIRQ))
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002682 return;
2683 /*
2684 * If we have softirqs enabled, then set the usage
2685 * bit for all held locks. (disabled hardirqs prevented
2686 * this bit from being set before)
2687 */
2688 if (curr->softirqs_enabled)
Nick Piggincf40bd12009-01-21 08:12:39 +01002689 if (!mark_held_locks(curr, SOFTIRQ))
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002690 return;
2691
2692 curr->hardirq_enable_ip = ip;
2693 curr->hardirq_enable_event = ++curr->irq_events;
Frederic Weisbeckerbd6d29c2010-04-06 00:10:17 +02002694 debug_atomic_inc(hardirqs_on_events);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002695}
Peter Zijlstradd4e5d32011-06-21 17:17:27 +02002696
Andi Kleenb35f8302014-02-08 08:52:02 +01002697__visible void trace_hardirqs_on_caller(unsigned long ip)
Peter Zijlstradd4e5d32011-06-21 17:17:27 +02002698{
2699 time_hardirqs_on(CALLER_ADDR0, ip);
2700
2701 if (unlikely(!debug_locks || current->lockdep_recursion))
2702 return;
2703
Peter Zijlstra7d36b262011-07-26 13:13:44 +02002704 if (unlikely(current->hardirqs_enabled)) {
2705 /*
2706 * Neither irq nor preemption are disabled here
2707 * so this is racy by nature but losing one hit
2708 * in a stat is not a big deal.
2709 */
2710 __debug_atomic_inc(redundant_hardirqs_on);
2711 return;
2712 }
2713
Peter Zijlstra0119fee2011-09-02 01:30:29 +02002714 /*
2715 * We're enabling irqs and according to our state above irqs weren't
2716 * already enabled, yet we find the hardware thinks they are in fact
2717 * enabled.. someone messed up their IRQ state tracing.
2718 */
Peter Zijlstradd4e5d32011-06-21 17:17:27 +02002719 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2720 return;
2721
Peter Zijlstra0119fee2011-09-02 01:30:29 +02002722 /*
2723 * See the fine text that goes along with this variable definition.
2724 */
Peter Zijlstra7d36b262011-07-26 13:13:44 +02002725 if (DEBUG_LOCKS_WARN_ON(unlikely(early_boot_irqs_disabled)))
2726 return;
2727
Peter Zijlstra0119fee2011-09-02 01:30:29 +02002728 /*
2729 * Can't allow enabling interrupts while in an interrupt handler,
2730 * that's general bad form and such. Recursion, limited stack etc..
2731 */
Peter Zijlstra7d36b262011-07-26 13:13:44 +02002732 if (DEBUG_LOCKS_WARN_ON(current->hardirq_context))
2733 return;
2734
Peter Zijlstradd4e5d32011-06-21 17:17:27 +02002735 current->lockdep_recursion = 1;
2736 __trace_hardirqs_on_caller(ip);
2737 current->lockdep_recursion = 0;
2738}
Steven Rostedt81d68a92008-05-12 21:20:42 +02002739EXPORT_SYMBOL(trace_hardirqs_on_caller);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002740
Steven Rostedt1d09daa2008-05-12 21:20:55 +02002741void trace_hardirqs_on(void)
Steven Rostedt81d68a92008-05-12 21:20:42 +02002742{
2743 trace_hardirqs_on_caller(CALLER_ADDR0);
2744}
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002745EXPORT_SYMBOL(trace_hardirqs_on);
2746
2747/*
2748 * Hardirqs were disabled:
2749 */
Andi Kleenb35f8302014-02-08 08:52:02 +01002750__visible void trace_hardirqs_off_caller(unsigned long ip)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002751{
2752 struct task_struct *curr = current;
2753
Heiko Carstens6afe40b2008-10-28 11:14:58 +01002754 time_hardirqs_off(CALLER_ADDR0, ip);
Steven Rostedt81d68a92008-05-12 21:20:42 +02002755
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002756 if (unlikely(!debug_locks || current->lockdep_recursion))
2757 return;
2758
Peter Zijlstra0119fee2011-09-02 01:30:29 +02002759 /*
2760 * So we're supposed to get called after you mask local IRQs, but for
2761 * some reason the hardware doesn't quite think you did a proper job.
2762 */
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002763 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2764 return;
2765
2766 if (curr->hardirqs_enabled) {
2767 /*
2768 * We have done an ON -> OFF transition:
2769 */
2770 curr->hardirqs_enabled = 0;
Heiko Carstens6afe40b2008-10-28 11:14:58 +01002771 curr->hardirq_disable_ip = ip;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002772 curr->hardirq_disable_event = ++curr->irq_events;
Frederic Weisbeckerbd6d29c2010-04-06 00:10:17 +02002773 debug_atomic_inc(hardirqs_off_events);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002774 } else
Frederic Weisbeckerbd6d29c2010-04-06 00:10:17 +02002775 debug_atomic_inc(redundant_hardirqs_off);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002776}
Steven Rostedt81d68a92008-05-12 21:20:42 +02002777EXPORT_SYMBOL(trace_hardirqs_off_caller);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002778
Steven Rostedt1d09daa2008-05-12 21:20:55 +02002779void trace_hardirqs_off(void)
Steven Rostedt81d68a92008-05-12 21:20:42 +02002780{
2781 trace_hardirqs_off_caller(CALLER_ADDR0);
2782}
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002783EXPORT_SYMBOL(trace_hardirqs_off);
2784
2785/*
2786 * Softirqs will be enabled:
2787 */
2788void trace_softirqs_on(unsigned long ip)
2789{
2790 struct task_struct *curr = current;
2791
Peter Zijlstradd4e5d32011-06-21 17:17:27 +02002792 if (unlikely(!debug_locks || current->lockdep_recursion))
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002793 return;
2794
Peter Zijlstra0119fee2011-09-02 01:30:29 +02002795 /*
2796 * We fancy IRQs being disabled here, see softirq.c, avoids
2797 * funny state and nesting things.
2798 */
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002799 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2800 return;
2801
2802 if (curr->softirqs_enabled) {
Frederic Weisbeckerbd6d29c2010-04-06 00:10:17 +02002803 debug_atomic_inc(redundant_softirqs_on);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002804 return;
2805 }
2806
Peter Zijlstradd4e5d32011-06-21 17:17:27 +02002807 current->lockdep_recursion = 1;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002808 /*
2809 * We'll do an OFF -> ON transition:
2810 */
2811 curr->softirqs_enabled = 1;
2812 curr->softirq_enable_ip = ip;
2813 curr->softirq_enable_event = ++curr->irq_events;
Frederic Weisbeckerbd6d29c2010-04-06 00:10:17 +02002814 debug_atomic_inc(softirqs_on_events);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002815 /*
2816 * We are going to turn softirqs on, so set the
2817 * usage bit for all held locks, if hardirqs are
2818 * enabled too:
2819 */
2820 if (curr->hardirqs_enabled)
Nick Piggincf40bd12009-01-21 08:12:39 +01002821 mark_held_locks(curr, SOFTIRQ);
Peter Zijlstradd4e5d32011-06-21 17:17:27 +02002822 current->lockdep_recursion = 0;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002823}
2824
2825/*
2826 * Softirqs were disabled:
2827 */
2828void trace_softirqs_off(unsigned long ip)
2829{
2830 struct task_struct *curr = current;
2831
Peter Zijlstradd4e5d32011-06-21 17:17:27 +02002832 if (unlikely(!debug_locks || current->lockdep_recursion))
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002833 return;
2834
Peter Zijlstra0119fee2011-09-02 01:30:29 +02002835 /*
2836 * We fancy IRQs being disabled here, see softirq.c
2837 */
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002838 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2839 return;
2840
2841 if (curr->softirqs_enabled) {
2842 /*
2843 * We have done an ON -> OFF transition:
2844 */
2845 curr->softirqs_enabled = 0;
2846 curr->softirq_disable_ip = ip;
2847 curr->softirq_disable_event = ++curr->irq_events;
Frederic Weisbeckerbd6d29c2010-04-06 00:10:17 +02002848 debug_atomic_inc(softirqs_off_events);
Peter Zijlstra0119fee2011-09-02 01:30:29 +02002849 /*
2850 * Whoops, we wanted softirqs off, so why aren't they?
2851 */
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002852 DEBUG_LOCKS_WARN_ON(!softirq_count());
2853 } else
Frederic Weisbeckerbd6d29c2010-04-06 00:10:17 +02002854 debug_atomic_inc(redundant_softirqs_off);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002855}
2856
Peter Zijlstra2f850182009-03-20 11:13:20 +01002857static void __lockdep_trace_alloc(gfp_t gfp_mask, unsigned long flags)
Nick Piggincf40bd12009-01-21 08:12:39 +01002858{
2859 struct task_struct *curr = current;
2860
2861 if (unlikely(!debug_locks))
2862 return;
2863
2864 /* no reclaim without waiting on it */
Mel Gormand0164ad2015-11-06 16:28:21 -08002865 if (!(gfp_mask & __GFP_DIRECT_RECLAIM))
Nick Piggincf40bd12009-01-21 08:12:39 +01002866 return;
2867
2868 /* this guy won't enter reclaim */
2869 if ((curr->flags & PF_MEMALLOC) && !(gfp_mask & __GFP_NOMEMALLOC))
2870 return;
2871
2872 /* We're only interested __GFP_FS allocations for now */
2873 if (!(gfp_mask & __GFP_FS))
2874 return;
2875
Peter Zijlstra0119fee2011-09-02 01:30:29 +02002876 /*
2877 * Oi! Can't be having __GFP_FS allocations with IRQs disabled.
2878 */
Peter Zijlstra2f850182009-03-20 11:13:20 +01002879 if (DEBUG_LOCKS_WARN_ON(irqs_disabled_flags(flags)))
Nick Piggincf40bd12009-01-21 08:12:39 +01002880 return;
2881
2882 mark_held_locks(curr, RECLAIM_FS);
2883}
2884
Peter Zijlstra2f850182009-03-20 11:13:20 +01002885static void check_flags(unsigned long flags);
2886
2887void lockdep_trace_alloc(gfp_t gfp_mask)
2888{
2889 unsigned long flags;
2890
2891 if (unlikely(current->lockdep_recursion))
2892 return;
2893
2894 raw_local_irq_save(flags);
2895 check_flags(flags);
2896 current->lockdep_recursion = 1;
2897 __lockdep_trace_alloc(gfp_mask, flags);
2898 current->lockdep_recursion = 0;
2899 raw_local_irq_restore(flags);
2900}
2901
Peter Zijlstra8e182572007-07-19 01:48:54 -07002902static int mark_irqflags(struct task_struct *curr, struct held_lock *hlock)
2903{
2904 /*
2905 * If non-trylock use in a hardirq or softirq context, then
2906 * mark the lock as used in these contexts:
2907 */
2908 if (!hlock->trylock) {
2909 if (hlock->read) {
2910 if (curr->hardirq_context)
2911 if (!mark_lock(curr, hlock,
2912 LOCK_USED_IN_HARDIRQ_READ))
2913 return 0;
2914 if (curr->softirq_context)
2915 if (!mark_lock(curr, hlock,
2916 LOCK_USED_IN_SOFTIRQ_READ))
2917 return 0;
2918 } else {
2919 if (curr->hardirq_context)
2920 if (!mark_lock(curr, hlock, LOCK_USED_IN_HARDIRQ))
2921 return 0;
2922 if (curr->softirq_context)
2923 if (!mark_lock(curr, hlock, LOCK_USED_IN_SOFTIRQ))
2924 return 0;
2925 }
2926 }
2927 if (!hlock->hardirqs_off) {
2928 if (hlock->read) {
2929 if (!mark_lock(curr, hlock,
Peter Zijlstra4fc95e82009-01-22 13:10:52 +01002930 LOCK_ENABLED_HARDIRQ_READ))
Peter Zijlstra8e182572007-07-19 01:48:54 -07002931 return 0;
2932 if (curr->softirqs_enabled)
2933 if (!mark_lock(curr, hlock,
Peter Zijlstra4fc95e82009-01-22 13:10:52 +01002934 LOCK_ENABLED_SOFTIRQ_READ))
Peter Zijlstra8e182572007-07-19 01:48:54 -07002935 return 0;
2936 } else {
2937 if (!mark_lock(curr, hlock,
Peter Zijlstra4fc95e82009-01-22 13:10:52 +01002938 LOCK_ENABLED_HARDIRQ))
Peter Zijlstra8e182572007-07-19 01:48:54 -07002939 return 0;
2940 if (curr->softirqs_enabled)
2941 if (!mark_lock(curr, hlock,
Peter Zijlstra4fc95e82009-01-22 13:10:52 +01002942 LOCK_ENABLED_SOFTIRQ))
Peter Zijlstra8e182572007-07-19 01:48:54 -07002943 return 0;
2944 }
2945 }
2946
Nick Piggincf40bd12009-01-21 08:12:39 +01002947 /*
2948 * We reuse the irq context infrastructure more broadly as a general
2949 * context checking code. This tests GFP_FS recursion (a lock taken
2950 * during reclaim for a GFP_FS allocation is held over a GFP_FS
2951 * allocation).
2952 */
2953 if (!hlock->trylock && (curr->lockdep_reclaim_gfp & __GFP_FS)) {
2954 if (hlock->read) {
2955 if (!mark_lock(curr, hlock, LOCK_USED_IN_RECLAIM_FS_READ))
2956 return 0;
2957 } else {
2958 if (!mark_lock(curr, hlock, LOCK_USED_IN_RECLAIM_FS))
2959 return 0;
2960 }
2961 }
2962
Peter Zijlstra8e182572007-07-19 01:48:54 -07002963 return 1;
2964}
2965
Boqun Fengc2469752016-02-16 13:57:40 +08002966static inline unsigned int task_irq_context(struct task_struct *task)
2967{
2968 return 2 * !!task->hardirq_context + !!task->softirq_context;
2969}
2970
Peter Zijlstra8e182572007-07-19 01:48:54 -07002971static int separate_irq_context(struct task_struct *curr,
2972 struct held_lock *hlock)
2973{
2974 unsigned int depth = curr->lockdep_depth;
2975
2976 /*
2977 * Keep track of points where we cross into an interrupt context:
2978 */
Peter Zijlstra8e182572007-07-19 01:48:54 -07002979 if (depth) {
2980 struct held_lock *prev_hlock;
2981
2982 prev_hlock = curr->held_locks + depth-1;
2983 /*
2984 * If we cross into another context, reset the
2985 * hash key (this also prevents the checking and the
2986 * adding of the dependency to 'prev'):
2987 */
2988 if (prev_hlock->irq_context != hlock->irq_context)
2989 return 1;
2990 }
2991 return 0;
2992}
2993
Peter Zijlstra0119fee2011-09-02 01:30:29 +02002994#else /* defined(CONFIG_TRACE_IRQFLAGS) && defined(CONFIG_PROVE_LOCKING) */
Peter Zijlstra8e182572007-07-19 01:48:54 -07002995
2996static inline
2997int mark_lock_irq(struct task_struct *curr, struct held_lock *this,
2998 enum lock_usage_bit new_bit)
2999{
Peter Zijlstra0119fee2011-09-02 01:30:29 +02003000 WARN_ON(1); /* Impossible innit? when we don't have TRACE_IRQFLAG */
Peter Zijlstra8e182572007-07-19 01:48:54 -07003001 return 1;
3002}
3003
3004static inline int mark_irqflags(struct task_struct *curr,
3005 struct held_lock *hlock)
3006{
3007 return 1;
3008}
3009
Boqun Fengc2469752016-02-16 13:57:40 +08003010static inline unsigned int task_irq_context(struct task_struct *task)
3011{
3012 return 0;
3013}
3014
Peter Zijlstra8e182572007-07-19 01:48:54 -07003015static inline int separate_irq_context(struct task_struct *curr,
3016 struct held_lock *hlock)
3017{
3018 return 0;
3019}
3020
Peter Zijlstra868a23a2009-02-15 00:25:21 +01003021void lockdep_trace_alloc(gfp_t gfp_mask)
3022{
3023}
3024
Peter Zijlstra0119fee2011-09-02 01:30:29 +02003025#endif /* defined(CONFIG_TRACE_IRQFLAGS) && defined(CONFIG_PROVE_LOCKING) */
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003026
3027/*
Peter Zijlstra8e182572007-07-19 01:48:54 -07003028 * Mark a lock with a usage bit, and validate the state transition:
3029 */
Steven Rostedt1d09daa2008-05-12 21:20:55 +02003030static int mark_lock(struct task_struct *curr, struct held_lock *this,
Steven Rostedt0764d232008-05-12 21:20:44 +02003031 enum lock_usage_bit new_bit)
Peter Zijlstra8e182572007-07-19 01:48:54 -07003032{
3033 unsigned int new_mask = 1 << new_bit, ret = 1;
3034
3035 /*
3036 * If already set then do not dirty the cacheline,
3037 * nor do any checks:
3038 */
Dave Jonesf82b2172008-08-11 09:30:23 +02003039 if (likely(hlock_class(this)->usage_mask & new_mask))
Peter Zijlstra8e182572007-07-19 01:48:54 -07003040 return 1;
3041
3042 if (!graph_lock())
3043 return 0;
3044 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003045 * Make sure we didn't race:
Peter Zijlstra8e182572007-07-19 01:48:54 -07003046 */
Dave Jonesf82b2172008-08-11 09:30:23 +02003047 if (unlikely(hlock_class(this)->usage_mask & new_mask)) {
Peter Zijlstra8e182572007-07-19 01:48:54 -07003048 graph_unlock();
3049 return 1;
3050 }
3051
Dave Jonesf82b2172008-08-11 09:30:23 +02003052 hlock_class(this)->usage_mask |= new_mask;
Peter Zijlstra8e182572007-07-19 01:48:54 -07003053
Dave Jonesf82b2172008-08-11 09:30:23 +02003054 if (!save_trace(hlock_class(this)->usage_traces + new_bit))
Peter Zijlstra8e182572007-07-19 01:48:54 -07003055 return 0;
3056
3057 switch (new_bit) {
Peter Zijlstra53464172009-01-22 14:15:53 +01003058#define LOCKDEP_STATE(__STATE) \
3059 case LOCK_USED_IN_##__STATE: \
3060 case LOCK_USED_IN_##__STATE##_READ: \
3061 case LOCK_ENABLED_##__STATE: \
3062 case LOCK_ENABLED_##__STATE##_READ:
3063#include "lockdep_states.h"
3064#undef LOCKDEP_STATE
Peter Zijlstra8e182572007-07-19 01:48:54 -07003065 ret = mark_lock_irq(curr, this, new_bit);
3066 if (!ret)
3067 return 0;
3068 break;
3069 case LOCK_USED:
Frederic Weisbeckerbd6d29c2010-04-06 00:10:17 +02003070 debug_atomic_dec(nr_unused_locks);
Peter Zijlstra8e182572007-07-19 01:48:54 -07003071 break;
3072 default:
3073 if (!debug_locks_off_graph_unlock())
3074 return 0;
3075 WARN_ON(1);
3076 return 0;
3077 }
3078
3079 graph_unlock();
3080
3081 /*
3082 * We must printk outside of the graph_lock:
3083 */
3084 if (ret == 2) {
3085 printk("\nmarked lock as {%s}:\n", usage_str[new_bit]);
3086 print_lock(this);
3087 print_irqtrace_events(curr);
3088 dump_stack();
3089 }
3090
3091 return ret;
3092}
3093
3094/*
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003095 * Initialize a lock instance's lock-class mapping info:
3096 */
3097void lockdep_init_map(struct lockdep_map *lock, const char *name,
Peter Zijlstra4dfbb9d2006-10-11 01:45:14 -04003098 struct lock_class_key *key, int subclass)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003099{
Yong Zhangd3d03d42011-11-09 16:04:51 +08003100 int i;
3101
3102 kmemcheck_mark_initialized(lock, sizeof(*lock));
3103
3104 for (i = 0; i < NR_LOCKDEP_CACHING_CLASSES; i++)
3105 lock->class_cache[i] = NULL;
Hitoshi Mitake62016252010-10-05 18:01:51 +09003106
Peter Zijlstrac8a25002009-04-17 09:40:49 +02003107#ifdef CONFIG_LOCK_STAT
3108 lock->cpu = raw_smp_processor_id();
3109#endif
3110
Peter Zijlstra0119fee2011-09-02 01:30:29 +02003111 /*
3112 * Can't be having no nameless bastards around this place!
3113 */
Peter Zijlstrac8a25002009-04-17 09:40:49 +02003114 if (DEBUG_LOCKS_WARN_ON(!name)) {
3115 lock->name = "NULL";
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003116 return;
Peter Zijlstrac8a25002009-04-17 09:40:49 +02003117 }
3118
3119 lock->name = name;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003120
Peter Zijlstra0119fee2011-09-02 01:30:29 +02003121 /*
3122 * No key, no joy, we need to hash something.
3123 */
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003124 if (DEBUG_LOCKS_WARN_ON(!key))
3125 return;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003126 /*
3127 * Sanity check, the lock-class key must be persistent:
3128 */
3129 if (!static_obj(key)) {
3130 printk("BUG: key %p not in .data!\n", key);
Peter Zijlstra0119fee2011-09-02 01:30:29 +02003131 /*
3132 * What it says above ^^^^^, I suggest you read it.
3133 */
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003134 DEBUG_LOCKS_WARN_ON(1);
3135 return;
3136 }
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003137 lock->key = key;
Peter Zijlstrac8a25002009-04-17 09:40:49 +02003138
3139 if (unlikely(!debug_locks))
3140 return;
3141
Peter Zijlstra35a93932015-02-26 16:23:11 +01003142 if (subclass) {
3143 unsigned long flags;
3144
3145 if (DEBUG_LOCKS_WARN_ON(current->lockdep_recursion))
3146 return;
3147
3148 raw_local_irq_save(flags);
3149 current->lockdep_recursion = 1;
Peter Zijlstra4dfbb9d2006-10-11 01:45:14 -04003150 register_lock_class(lock, subclass, 1);
Peter Zijlstra35a93932015-02-26 16:23:11 +01003151 current->lockdep_recursion = 0;
3152 raw_local_irq_restore(flags);
3153 }
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003154}
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003155EXPORT_SYMBOL_GPL(lockdep_init_map);
3156
Peter Zijlstra1704f472010-03-19 01:37:42 +01003157struct lock_class_key __lockdep_no_validate__;
Kent Overstreetea6749c2012-12-27 22:21:58 -08003158EXPORT_SYMBOL_GPL(__lockdep_no_validate__);
Peter Zijlstra1704f472010-03-19 01:37:42 +01003159
Maarten Lankhorstd0945952012-09-13 11:39:51 +02003160static int
3161print_lock_nested_lock_not_held(struct task_struct *curr,
3162 struct held_lock *hlock,
3163 unsigned long ip)
3164{
3165 if (!debug_locks_off())
3166 return 0;
3167 if (debug_locks_silent)
3168 return 0;
3169
3170 printk("\n");
3171 printk("==================================\n");
3172 printk("[ BUG: Nested lock was not taken ]\n");
3173 print_kernel_ident();
3174 printk("----------------------------------\n");
3175
3176 printk("%s/%d is trying to lock:\n", curr->comm, task_pid_nr(curr));
3177 print_lock(hlock);
3178
3179 printk("\nbut this task is not holding:\n");
3180 printk("%s\n", hlock->nest_lock->name);
3181
3182 printk("\nstack backtrace:\n");
3183 dump_stack();
3184
3185 printk("\nother info that might help us debug this:\n");
3186 lockdep_print_held_locks(curr);
3187
3188 printk("\nstack backtrace:\n");
3189 dump_stack();
3190
3191 return 0;
3192}
3193
3194static int __lock_is_held(struct lockdep_map *lock);
3195
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003196/*
3197 * This gets called for every mutex_lock*()/spin_lock*() operation.
3198 * We maintain the dependency maps and validate the locking attempt:
3199 */
3200static int __lock_acquire(struct lockdep_map *lock, unsigned int subclass,
3201 int trylock, int read, int check, int hardirqs_off,
Peter Zijlstrabb97a912009-07-20 19:15:35 +02003202 struct lockdep_map *nest_lock, unsigned long ip,
Peter Zijlstra21199f22015-09-16 16:10:40 +02003203 int references, int pin_count)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003204{
3205 struct task_struct *curr = current;
Ingo Molnard6d897c2006-07-10 04:44:04 -07003206 struct lock_class *class = NULL;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003207 struct held_lock *hlock;
Alfredo Alvarez Fernandez5f18ab52016-02-11 00:33:32 +01003208 unsigned int depth;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003209 int chain_head = 0;
Peter Zijlstrabb97a912009-07-20 19:15:35 +02003210 int class_idx;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003211 u64 chain_key;
3212
3213 if (unlikely(!debug_locks))
3214 return 0;
3215
Peter Zijlstra0119fee2011-09-02 01:30:29 +02003216 /*
3217 * Lockdep should run with IRQs disabled, otherwise we could
3218 * get an interrupt which would want to take locks, which would
3219 * end up in lockdep and have you got a head-ache already?
3220 */
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003221 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
3222 return 0;
3223
Oleg Nesterovfb9edbe2014-01-20 19:20:06 +01003224 if (!prove_locking || lock->key == &__lockdep_no_validate__)
3225 check = 0;
Peter Zijlstra1704f472010-03-19 01:37:42 +01003226
Hitoshi Mitake62016252010-10-05 18:01:51 +09003227 if (subclass < NR_LOCKDEP_CACHING_CLASSES)
3228 class = lock->class_cache[subclass];
Ingo Molnard6d897c2006-07-10 04:44:04 -07003229 /*
Hitoshi Mitake62016252010-10-05 18:01:51 +09003230 * Not cached?
Ingo Molnard6d897c2006-07-10 04:44:04 -07003231 */
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003232 if (unlikely(!class)) {
Peter Zijlstra4dfbb9d2006-10-11 01:45:14 -04003233 class = register_lock_class(lock, subclass, 0);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003234 if (!class)
3235 return 0;
3236 }
Frederic Weisbeckerbd6d29c2010-04-06 00:10:17 +02003237 atomic_inc((atomic_t *)&class->ops);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003238 if (very_verbose(class)) {
3239 printk("\nacquire class [%p] %s", class->key, class->name);
3240 if (class->name_version > 1)
Dmitry Vyukovf943fe02016-11-28 15:24:43 +01003241 printk(KERN_CONT "#%d", class->name_version);
3242 printk(KERN_CONT "\n");
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003243 dump_stack();
3244 }
3245
3246 /*
3247 * Add the lock to the list of currently held locks.
3248 * (we dont increase the depth just yet, up until the
3249 * dependency checks are done)
3250 */
3251 depth = curr->lockdep_depth;
Peter Zijlstra0119fee2011-09-02 01:30:29 +02003252 /*
3253 * Ran out of static storage for our per-task lock stack again have we?
3254 */
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003255 if (DEBUG_LOCKS_WARN_ON(depth >= MAX_LOCK_DEPTH))
3256 return 0;
3257
Peter Zijlstrabb97a912009-07-20 19:15:35 +02003258 class_idx = class - lock_classes + 1;
3259
3260 if (depth) {
3261 hlock = curr->held_locks + depth - 1;
3262 if (hlock->class_idx == class_idx && nest_lock) {
Imre Deak1aa8b252019-05-24 23:15:09 +03003263 if (!references)
3264 references++;
Peter Zijlstra8b0be542017-03-01 16:23:30 +01003265
Imre Deak1aa8b252019-05-24 23:15:09 +03003266 if (!hlock->references)
Peter Zijlstrabb97a912009-07-20 19:15:35 +02003267 hlock->references++;
Imre Deak1aa8b252019-05-24 23:15:09 +03003268
3269 hlock->references += references;
3270
3271 /* Overflow */
3272 if (DEBUG_LOCKS_WARN_ON(hlock->references < references))
3273 return 0;
Peter Zijlstrabb97a912009-07-20 19:15:35 +02003274
3275 return 1;
3276 }
3277 }
3278
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003279 hlock = curr->held_locks + depth;
Peter Zijlstra0119fee2011-09-02 01:30:29 +02003280 /*
3281 * Plain impossible, we just registered it and checked it weren't no
3282 * NULL like.. I bet this mushroom I ate was good!
3283 */
Dave Jonesf82b2172008-08-11 09:30:23 +02003284 if (DEBUG_LOCKS_WARN_ON(!class))
3285 return 0;
Peter Zijlstrabb97a912009-07-20 19:15:35 +02003286 hlock->class_idx = class_idx;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003287 hlock->acquire_ip = ip;
3288 hlock->instance = lock;
Peter Zijlstra7531e2f2008-08-11 09:30:24 +02003289 hlock->nest_lock = nest_lock;
Boqun Fengc2469752016-02-16 13:57:40 +08003290 hlock->irq_context = task_irq_context(curr);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003291 hlock->trylock = trylock;
3292 hlock->read = read;
3293 hlock->check = check;
Dmitry Baryshkov6951b122008-08-18 04:26:37 +04003294 hlock->hardirqs_off = !!hardirqs_off;
Peter Zijlstrabb97a912009-07-20 19:15:35 +02003295 hlock->references = references;
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003296#ifdef CONFIG_LOCK_STAT
3297 hlock->waittime_stamp = 0;
Peter Zijlstra3365e7792009-10-09 10:12:41 +02003298 hlock->holdtime_stamp = lockstat_clock();
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003299#endif
Peter Zijlstra21199f22015-09-16 16:10:40 +02003300 hlock->pin_count = pin_count;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003301
Oleg Nesterovfb9edbe2014-01-20 19:20:06 +01003302 if (check && !mark_irqflags(curr, hlock))
Peter Zijlstra8e182572007-07-19 01:48:54 -07003303 return 0;
3304
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003305 /* mark it as used: */
Jarek Poplawski4ff773bb2007-05-08 00:31:00 -07003306 if (!mark_lock(curr, hlock, LOCK_USED))
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003307 return 0;
Peter Zijlstra8e182572007-07-19 01:48:54 -07003308
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003309 /*
Gautham R Shenoy17aacfb2007-10-28 20:47:01 +01003310 * Calculate the chain hash: it's the combined hash of all the
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003311 * lock keys along the dependency chain. We save the hash value
3312 * at every step so that we can get the current hash easily
3313 * after unlock. The chain hash is then used to cache dependency
3314 * results.
3315 *
3316 * The 'key ID' is what is the most compact key value to drive
3317 * the hash, not class->key.
3318 */
Peter Zijlstra0119fee2011-09-02 01:30:29 +02003319 /*
3320 * Whoops, we did it again.. ran straight out of our static allocation.
3321 */
Alfredo Alvarez Fernandez5f18ab52016-02-11 00:33:32 +01003322 if (DEBUG_LOCKS_WARN_ON(class_idx > MAX_LOCKDEP_KEYS))
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003323 return 0;
3324
3325 chain_key = curr->curr_chain_key;
3326 if (!depth) {
Peter Zijlstra0119fee2011-09-02 01:30:29 +02003327 /*
3328 * How can we have a chain hash when we ain't got no keys?!
3329 */
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003330 if (DEBUG_LOCKS_WARN_ON(chain_key != 0))
3331 return 0;
3332 chain_head = 1;
3333 }
3334
3335 hlock->prev_chain_key = chain_key;
Peter Zijlstra8e182572007-07-19 01:48:54 -07003336 if (separate_irq_context(curr, hlock)) {
3337 chain_key = 0;
3338 chain_head = 1;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003339 }
Alfredo Alvarez Fernandez5f18ab52016-02-11 00:33:32 +01003340 chain_key = iterate_chain_key(chain_key, class_idx);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003341
Maarten Lankhorstd0945952012-09-13 11:39:51 +02003342 if (nest_lock && !__lock_is_held(nest_lock))
3343 return print_lock_nested_lock_not_held(curr, hlock, ip);
3344
Gregory Haskins3aa416b2007-10-11 22:11:11 +02003345 if (!validate_chain(curr, lock, hlock, chain_head, chain_key))
Peter Zijlstra8e182572007-07-19 01:48:54 -07003346 return 0;
Jarek Poplawski381a2292007-02-10 01:44:58 -08003347
Gregory Haskins3aa416b2007-10-11 22:11:11 +02003348 curr->curr_chain_key = chain_key;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003349 curr->lockdep_depth++;
3350 check_chain_key(curr);
Jarek Poplawski60e114d2007-02-20 13:58:00 -08003351#ifdef CONFIG_DEBUG_LOCKDEP
3352 if (unlikely(!debug_locks))
3353 return 0;
3354#endif
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003355 if (unlikely(curr->lockdep_depth >= MAX_LOCK_DEPTH)) {
3356 debug_locks_off();
Dave Jones2c522832013-04-25 13:40:02 -04003357 print_lockdep_off("BUG: MAX_LOCK_DEPTH too low!");
3358 printk(KERN_DEBUG "depth: %i max: %lu!\n",
Ben Greearc0540602013-02-06 10:56:19 -08003359 curr->lockdep_depth, MAX_LOCK_DEPTH);
Ben Greearc0540602013-02-06 10:56:19 -08003360
3361 lockdep_print_held_locks(current);
3362 debug_show_all_locks();
Peter Zijlstraeedeeab2009-03-18 12:38:47 +01003363 dump_stack();
Ben Greearc0540602013-02-06 10:56:19 -08003364
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003365 return 0;
3366 }
Jarek Poplawski381a2292007-02-10 01:44:58 -08003367
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003368 if (unlikely(curr->lockdep_depth > max_lockdep_depth))
3369 max_lockdep_depth = curr->lockdep_depth;
3370
3371 return 1;
3372}
3373
3374static int
Srivatsa S. Bhatf86f7552013-01-08 18:35:58 +05303375print_unlock_imbalance_bug(struct task_struct *curr, struct lockdep_map *lock,
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003376 unsigned long ip)
3377{
3378 if (!debug_locks_off())
3379 return 0;
3380 if (debug_locks_silent)
3381 return 0;
3382
Paul E. McKenneyb3fbab02011-05-24 08:31:09 -07003383 printk("\n");
3384 printk("=====================================\n");
3385 printk("[ BUG: bad unlock balance detected! ]\n");
Ben Hutchingsfbdc4b92011-10-28 04:36:55 +01003386 print_kernel_ident();
Paul E. McKenneyb3fbab02011-05-24 08:31:09 -07003387 printk("-------------------------------------\n");
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003388 printk("%s/%d is trying to release lock (",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07003389 curr->comm, task_pid_nr(curr));
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003390 print_lockdep_cache(lock);
Dmitry Vyukovf943fe02016-11-28 15:24:43 +01003391 printk(KERN_CONT ") at:\n");
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003392 print_ip_sym(ip);
3393 printk("but there are no more locks to release!\n");
3394 printk("\nother info that might help us debug this:\n");
3395 lockdep_print_held_locks(curr);
3396
3397 printk("\nstack backtrace:\n");
3398 dump_stack();
3399
3400 return 0;
3401}
3402
Peter Zijlstrabb97a912009-07-20 19:15:35 +02003403static int match_held_lock(struct held_lock *hlock, struct lockdep_map *lock)
3404{
3405 if (hlock->instance == lock)
3406 return 1;
3407
3408 if (hlock->references) {
Hitoshi Mitake62016252010-10-05 18:01:51 +09003409 struct lock_class *class = lock->class_cache[0];
Peter Zijlstrabb97a912009-07-20 19:15:35 +02003410
3411 if (!class)
3412 class = look_up_lock_class(lock, 0);
3413
Peter Zijlstra80e04012011-08-05 14:26:17 +02003414 /*
3415 * If look_up_lock_class() failed to find a class, we're trying
3416 * to test if we hold a lock that has never yet been acquired.
3417 * Clearly if the lock hasn't been acquired _ever_, we're not
3418 * holding it either, so report failure.
3419 */
3420 if (!class)
Peter Zijlstrabb97a912009-07-20 19:15:35 +02003421 return 0;
3422
Peter Zijlstra0119fee2011-09-02 01:30:29 +02003423 /*
3424 * References, but not a lock we're actually ref-counting?
3425 * State got messed up, follow the sites that change ->references
3426 * and try to make sense of it.
3427 */
Peter Zijlstrabb97a912009-07-20 19:15:35 +02003428 if (DEBUG_LOCKS_WARN_ON(!hlock->nest_lock))
3429 return 0;
3430
3431 if (hlock->class_idx == class - lock_classes + 1)
3432 return 1;
3433 }
3434
3435 return 0;
3436}
3437
Peter Zijlstra64aa3482008-08-11 09:30:21 +02003438static int
Peter Zijlstra00ef9f72008-12-04 09:00:17 +01003439__lock_set_class(struct lockdep_map *lock, const char *name,
3440 struct lock_class_key *key, unsigned int subclass,
3441 unsigned long ip)
Peter Zijlstra64aa3482008-08-11 09:30:21 +02003442{
3443 struct task_struct *curr = current;
3444 struct held_lock *hlock, *prev_hlock;
3445 struct lock_class *class;
3446 unsigned int depth;
3447 int i;
3448
Waiman Long59da7942019-01-09 23:03:25 -05003449 if (unlikely(!debug_locks))
3450 return 0;
3451
Peter Zijlstra64aa3482008-08-11 09:30:21 +02003452 depth = curr->lockdep_depth;
Peter Zijlstra0119fee2011-09-02 01:30:29 +02003453 /*
3454 * This function is about (re)setting the class of a held lock,
3455 * yet we're not actually holding any locks. Naughty user!
3456 */
Peter Zijlstra64aa3482008-08-11 09:30:21 +02003457 if (DEBUG_LOCKS_WARN_ON(!depth))
3458 return 0;
3459
3460 prev_hlock = NULL;
3461 for (i = depth-1; i >= 0; i--) {
3462 hlock = curr->held_locks + i;
3463 /*
3464 * We must not cross into another context:
3465 */
3466 if (prev_hlock && prev_hlock->irq_context != hlock->irq_context)
3467 break;
Peter Zijlstrabb97a912009-07-20 19:15:35 +02003468 if (match_held_lock(hlock, lock))
Peter Zijlstra64aa3482008-08-11 09:30:21 +02003469 goto found_it;
3470 prev_hlock = hlock;
3471 }
Srivatsa S. Bhatf86f7552013-01-08 18:35:58 +05303472 return print_unlock_imbalance_bug(curr, lock, ip);
Peter Zijlstra64aa3482008-08-11 09:30:21 +02003473
3474found_it:
Peter Zijlstra00ef9f72008-12-04 09:00:17 +01003475 lockdep_init_map(lock, name, key, 0);
Peter Zijlstra64aa3482008-08-11 09:30:21 +02003476 class = register_lock_class(lock, subclass, 0);
Dave Jonesf82b2172008-08-11 09:30:23 +02003477 hlock->class_idx = class - lock_classes + 1;
Peter Zijlstra64aa3482008-08-11 09:30:21 +02003478
3479 curr->lockdep_depth = i;
3480 curr->curr_chain_key = hlock->prev_chain_key;
3481
3482 for (; i < depth; i++) {
3483 hlock = curr->held_locks + i;
3484 if (!__lock_acquire(hlock->instance,
Dave Jonesf82b2172008-08-11 09:30:23 +02003485 hlock_class(hlock)->subclass, hlock->trylock,
Peter Zijlstra64aa3482008-08-11 09:30:21 +02003486 hlock->read, hlock->check, hlock->hardirqs_off,
Peter Zijlstrabb97a912009-07-20 19:15:35 +02003487 hlock->nest_lock, hlock->acquire_ip,
Peter Zijlstra21199f22015-09-16 16:10:40 +02003488 hlock->references, hlock->pin_count))
Peter Zijlstra64aa3482008-08-11 09:30:21 +02003489 return 0;
3490 }
3491
Peter Zijlstra0119fee2011-09-02 01:30:29 +02003492 /*
3493 * I took it apart and put it back together again, except now I have
3494 * these 'spare' parts.. where shall I put them.
3495 */
Peter Zijlstra64aa3482008-08-11 09:30:21 +02003496 if (DEBUG_LOCKS_WARN_ON(curr->lockdep_depth != depth))
3497 return 0;
3498 return 1;
3499}
3500
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003501/*
Peter Zijlstrae0f56fd2015-06-11 14:46:52 +02003502 * Remove the lock to the list of currently held locks - this gets
3503 * called on mutex_unlock()/spin_unlock*() (or on a failed
3504 * mutex_lock_interruptible()).
3505 *
3506 * @nested is an hysterical artifact, needs a tree wide cleanup.
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003507 */
3508static int
Peter Zijlstrae0f56fd2015-06-11 14:46:52 +02003509__lock_release(struct lockdep_map *lock, int nested, unsigned long ip)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003510{
Peter Zijlstrae0f56fd2015-06-11 14:46:52 +02003511 struct task_struct *curr = current;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003512 struct held_lock *hlock, *prev_hlock;
3513 unsigned int depth;
3514 int i;
3515
Peter Zijlstrae0f56fd2015-06-11 14:46:52 +02003516 if (unlikely(!debug_locks))
3517 return 0;
3518
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003519 depth = curr->lockdep_depth;
Peter Zijlstra0119fee2011-09-02 01:30:29 +02003520 /*
3521 * So we're all set to release this lock.. wait what lock? We don't
3522 * own any locks, you've been drinking again?
3523 */
Peter Zijlstrae0f56fd2015-06-11 14:46:52 +02003524 if (DEBUG_LOCKS_WARN_ON(depth <= 0))
3525 return print_unlock_imbalance_bug(curr, lock, ip);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003526
Peter Zijlstrae0f56fd2015-06-11 14:46:52 +02003527 /*
3528 * Check whether the lock exists in the current stack
3529 * of held locks:
3530 */
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003531 prev_hlock = NULL;
3532 for (i = depth-1; i >= 0; i--) {
3533 hlock = curr->held_locks + i;
3534 /*
3535 * We must not cross into another context:
3536 */
3537 if (prev_hlock && prev_hlock->irq_context != hlock->irq_context)
3538 break;
Peter Zijlstrabb97a912009-07-20 19:15:35 +02003539 if (match_held_lock(hlock, lock))
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003540 goto found_it;
3541 prev_hlock = hlock;
3542 }
Srivatsa S. Bhatf86f7552013-01-08 18:35:58 +05303543 return print_unlock_imbalance_bug(curr, lock, ip);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003544
3545found_it:
Peter Zijlstrabb97a912009-07-20 19:15:35 +02003546 if (hlock->instance == lock)
3547 lock_release_holdtime(hlock);
3548
Peter Zijlstraa24fc602015-06-11 14:46:53 +02003549 WARN(hlock->pin_count, "releasing a pinned lock\n");
3550
Peter Zijlstrabb97a912009-07-20 19:15:35 +02003551 if (hlock->references) {
3552 hlock->references--;
3553 if (hlock->references) {
3554 /*
3555 * We had, and after removing one, still have
3556 * references, the current lock stack is still
3557 * valid. We're done!
3558 */
3559 return 1;
3560 }
3561 }
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003562
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003563 /*
3564 * We have the right lock to unlock, 'hlock' points to it.
3565 * Now we remove it from the stack, and add back the other
3566 * entries (if any), recalculating the hash along the way:
3567 */
Peter Zijlstrabb97a912009-07-20 19:15:35 +02003568
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003569 curr->lockdep_depth = i;
3570 curr->curr_chain_key = hlock->prev_chain_key;
3571
3572 for (i++; i < depth; i++) {
3573 hlock = curr->held_locks + i;
3574 if (!__lock_acquire(hlock->instance,
Dave Jonesf82b2172008-08-11 09:30:23 +02003575 hlock_class(hlock)->subclass, hlock->trylock,
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003576 hlock->read, hlock->check, hlock->hardirqs_off,
Peter Zijlstrabb97a912009-07-20 19:15:35 +02003577 hlock->nest_lock, hlock->acquire_ip,
Peter Zijlstra21199f22015-09-16 16:10:40 +02003578 hlock->references, hlock->pin_count))
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003579 return 0;
3580 }
3581
Peter Zijlstra0119fee2011-09-02 01:30:29 +02003582 /*
3583 * We had N bottles of beer on the wall, we drank one, but now
3584 * there's not N-1 bottles of beer left on the wall...
3585 */
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003586 if (DEBUG_LOCKS_WARN_ON(curr->lockdep_depth != depth - 1))
3587 return 0;
Peter Zijlstrae0f56fd2015-06-11 14:46:52 +02003588
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003589 return 1;
3590}
3591
Peter Zijlstraf607c662009-07-20 19:16:29 +02003592static int __lock_is_held(struct lockdep_map *lock)
3593{
3594 struct task_struct *curr = current;
3595 int i;
3596
3597 for (i = 0; i < curr->lockdep_depth; i++) {
Peter Zijlstrabb97a912009-07-20 19:15:35 +02003598 struct held_lock *hlock = curr->held_locks + i;
3599
3600 if (match_held_lock(hlock, lock))
Peter Zijlstraf607c662009-07-20 19:16:29 +02003601 return 1;
3602 }
3603
3604 return 0;
3605}
3606
Peter Zijlstrae7904a22015-08-01 19:25:08 +02003607static struct pin_cookie __lock_pin_lock(struct lockdep_map *lock)
3608{
3609 struct pin_cookie cookie = NIL_COOKIE;
3610 struct task_struct *curr = current;
3611 int i;
3612
3613 if (unlikely(!debug_locks))
3614 return cookie;
3615
3616 for (i = 0; i < curr->lockdep_depth; i++) {
3617 struct held_lock *hlock = curr->held_locks + i;
3618
3619 if (match_held_lock(hlock, lock)) {
3620 /*
3621 * Grab 16bits of randomness; this is sufficient to not
3622 * be guessable and still allows some pin nesting in
3623 * our u32 pin_count.
3624 */
3625 cookie.val = 1 + (prandom_u32() >> 16);
3626 hlock->pin_count += cookie.val;
3627 return cookie;
3628 }
3629 }
3630
3631 WARN(1, "pinning an unheld lock\n");
3632 return cookie;
3633}
3634
3635static void __lock_repin_lock(struct lockdep_map *lock, struct pin_cookie cookie)
Peter Zijlstraa24fc602015-06-11 14:46:53 +02003636{
3637 struct task_struct *curr = current;
3638 int i;
3639
3640 if (unlikely(!debug_locks))
3641 return;
3642
3643 for (i = 0; i < curr->lockdep_depth; i++) {
3644 struct held_lock *hlock = curr->held_locks + i;
3645
3646 if (match_held_lock(hlock, lock)) {
Peter Zijlstrae7904a22015-08-01 19:25:08 +02003647 hlock->pin_count += cookie.val;
Peter Zijlstraa24fc602015-06-11 14:46:53 +02003648 return;
3649 }
3650 }
3651
3652 WARN(1, "pinning an unheld lock\n");
3653}
3654
Peter Zijlstrae7904a22015-08-01 19:25:08 +02003655static void __lock_unpin_lock(struct lockdep_map *lock, struct pin_cookie cookie)
Peter Zijlstraa24fc602015-06-11 14:46:53 +02003656{
3657 struct task_struct *curr = current;
3658 int i;
3659
3660 if (unlikely(!debug_locks))
3661 return;
3662
3663 for (i = 0; i < curr->lockdep_depth; i++) {
3664 struct held_lock *hlock = curr->held_locks + i;
3665
3666 if (match_held_lock(hlock, lock)) {
3667 if (WARN(!hlock->pin_count, "unpinning an unpinned lock\n"))
3668 return;
3669
Peter Zijlstrae7904a22015-08-01 19:25:08 +02003670 hlock->pin_count -= cookie.val;
3671
3672 if (WARN((int)hlock->pin_count < 0, "pin count corrupted\n"))
3673 hlock->pin_count = 0;
3674
Peter Zijlstraa24fc602015-06-11 14:46:53 +02003675 return;
3676 }
3677 }
3678
3679 WARN(1, "unpinning an unheld lock\n");
3680}
3681
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003682/*
3683 * Check whether we follow the irq-flags state precisely:
3684 */
Steven Rostedt1d09daa2008-05-12 21:20:55 +02003685static void check_flags(unsigned long flags)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003686{
Ingo Molnar992860e2008-07-14 10:28:38 +02003687#if defined(CONFIG_PROVE_LOCKING) && defined(CONFIG_DEBUG_LOCKDEP) && \
3688 defined(CONFIG_TRACE_IRQFLAGS)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003689 if (!debug_locks)
3690 return;
3691
Ingo Molnar5f9fa8a2007-12-07 19:02:47 +01003692 if (irqs_disabled_flags(flags)) {
3693 if (DEBUG_LOCKS_WARN_ON(current->hardirqs_enabled)) {
3694 printk("possible reason: unannotated irqs-off.\n");
3695 }
3696 } else {
3697 if (DEBUG_LOCKS_WARN_ON(!current->hardirqs_enabled)) {
3698 printk("possible reason: unannotated irqs-on.\n");
3699 }
3700 }
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003701
3702 /*
3703 * We dont accurately track softirq state in e.g.
3704 * hardirq contexts (such as on 4KSTACKS), so only
3705 * check if not in hardirq contexts:
3706 */
3707 if (!hardirq_count()) {
Peter Zijlstra0119fee2011-09-02 01:30:29 +02003708 if (softirq_count()) {
3709 /* like the above, but with softirqs */
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003710 DEBUG_LOCKS_WARN_ON(current->softirqs_enabled);
Peter Zijlstra0119fee2011-09-02 01:30:29 +02003711 } else {
3712 /* lick the above, does it taste good? */
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003713 DEBUG_LOCKS_WARN_ON(!current->softirqs_enabled);
Peter Zijlstra0119fee2011-09-02 01:30:29 +02003714 }
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003715 }
3716
3717 if (!debug_locks)
3718 print_irqtrace_events(current);
3719#endif
3720}
3721
Peter Zijlstra00ef9f72008-12-04 09:00:17 +01003722void lock_set_class(struct lockdep_map *lock, const char *name,
3723 struct lock_class_key *key, unsigned int subclass,
3724 unsigned long ip)
Peter Zijlstra64aa3482008-08-11 09:30:21 +02003725{
3726 unsigned long flags;
3727
3728 if (unlikely(current->lockdep_recursion))
3729 return;
3730
3731 raw_local_irq_save(flags);
3732 current->lockdep_recursion = 1;
3733 check_flags(flags);
Peter Zijlstra00ef9f72008-12-04 09:00:17 +01003734 if (__lock_set_class(lock, name, key, subclass, ip))
Peter Zijlstra64aa3482008-08-11 09:30:21 +02003735 check_chain_key(current);
3736 current->lockdep_recursion = 0;
3737 raw_local_irq_restore(flags);
3738}
Peter Zijlstra00ef9f72008-12-04 09:00:17 +01003739EXPORT_SYMBOL_GPL(lock_set_class);
Peter Zijlstra64aa3482008-08-11 09:30:21 +02003740
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003741/*
3742 * We are not always called with irqs disabled - do that here,
3743 * and also avoid lockdep recursion:
3744 */
Steven Rostedt1d09daa2008-05-12 21:20:55 +02003745void lock_acquire(struct lockdep_map *lock, unsigned int subclass,
Peter Zijlstra7531e2f2008-08-11 09:30:24 +02003746 int trylock, int read, int check,
3747 struct lockdep_map *nest_lock, unsigned long ip)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003748{
3749 unsigned long flags;
3750
3751 if (unlikely(current->lockdep_recursion))
3752 return;
3753
3754 raw_local_irq_save(flags);
3755 check_flags(flags);
3756
3757 current->lockdep_recursion = 1;
Frederic Weisbeckerdb2c4c72010-02-02 23:34:40 +01003758 trace_lock_acquire(lock, subclass, trylock, read, check, nest_lock, ip);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003759 __lock_acquire(lock, subclass, trylock, read, check,
Peter Zijlstra21199f22015-09-16 16:10:40 +02003760 irqs_disabled_flags(flags), nest_lock, ip, 0, 0);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003761 current->lockdep_recursion = 0;
3762 raw_local_irq_restore(flags);
3763}
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003764EXPORT_SYMBOL_GPL(lock_acquire);
3765
Steven Rostedt1d09daa2008-05-12 21:20:55 +02003766void lock_release(struct lockdep_map *lock, int nested,
Steven Rostedt0764d232008-05-12 21:20:44 +02003767 unsigned long ip)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003768{
3769 unsigned long flags;
3770
3771 if (unlikely(current->lockdep_recursion))
3772 return;
3773
3774 raw_local_irq_save(flags);
3775 check_flags(flags);
3776 current->lockdep_recursion = 1;
Frederic Weisbecker93135432010-05-08 06:24:25 +02003777 trace_lock_release(lock, ip);
Peter Zijlstrae0f56fd2015-06-11 14:46:52 +02003778 if (__lock_release(lock, nested, ip))
3779 check_chain_key(current);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003780 current->lockdep_recursion = 0;
3781 raw_local_irq_restore(flags);
3782}
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003783EXPORT_SYMBOL_GPL(lock_release);
3784
Peter Zijlstraf607c662009-07-20 19:16:29 +02003785int lock_is_held(struct lockdep_map *lock)
3786{
3787 unsigned long flags;
3788 int ret = 0;
3789
3790 if (unlikely(current->lockdep_recursion))
Peter Zijlstraf2513cd2011-06-06 12:32:43 +02003791 return 1; /* avoid false negative lockdep_assert_held() */
Peter Zijlstraf607c662009-07-20 19:16:29 +02003792
3793 raw_local_irq_save(flags);
3794 check_flags(flags);
3795
3796 current->lockdep_recursion = 1;
3797 ret = __lock_is_held(lock);
3798 current->lockdep_recursion = 0;
3799 raw_local_irq_restore(flags);
3800
3801 return ret;
3802}
3803EXPORT_SYMBOL_GPL(lock_is_held);
3804
Peter Zijlstrae7904a22015-08-01 19:25:08 +02003805struct pin_cookie lock_pin_lock(struct lockdep_map *lock)
Peter Zijlstraa24fc602015-06-11 14:46:53 +02003806{
Peter Zijlstrae7904a22015-08-01 19:25:08 +02003807 struct pin_cookie cookie = NIL_COOKIE;
Peter Zijlstraa24fc602015-06-11 14:46:53 +02003808 unsigned long flags;
3809
3810 if (unlikely(current->lockdep_recursion))
Peter Zijlstrae7904a22015-08-01 19:25:08 +02003811 return cookie;
Peter Zijlstraa24fc602015-06-11 14:46:53 +02003812
3813 raw_local_irq_save(flags);
3814 check_flags(flags);
3815
3816 current->lockdep_recursion = 1;
Peter Zijlstrae7904a22015-08-01 19:25:08 +02003817 cookie = __lock_pin_lock(lock);
Peter Zijlstraa24fc602015-06-11 14:46:53 +02003818 current->lockdep_recursion = 0;
3819 raw_local_irq_restore(flags);
Peter Zijlstrae7904a22015-08-01 19:25:08 +02003820
3821 return cookie;
Peter Zijlstraa24fc602015-06-11 14:46:53 +02003822}
3823EXPORT_SYMBOL_GPL(lock_pin_lock);
3824
Peter Zijlstrae7904a22015-08-01 19:25:08 +02003825void lock_repin_lock(struct lockdep_map *lock, struct pin_cookie cookie)
Peter Zijlstraa24fc602015-06-11 14:46:53 +02003826{
3827 unsigned long flags;
3828
3829 if (unlikely(current->lockdep_recursion))
3830 return;
3831
3832 raw_local_irq_save(flags);
3833 check_flags(flags);
3834
3835 current->lockdep_recursion = 1;
Peter Zijlstrae7904a22015-08-01 19:25:08 +02003836 __lock_repin_lock(lock, cookie);
3837 current->lockdep_recursion = 0;
3838 raw_local_irq_restore(flags);
3839}
3840EXPORT_SYMBOL_GPL(lock_repin_lock);
3841
3842void lock_unpin_lock(struct lockdep_map *lock, struct pin_cookie cookie)
3843{
3844 unsigned long flags;
3845
3846 if (unlikely(current->lockdep_recursion))
3847 return;
3848
3849 raw_local_irq_save(flags);
3850 check_flags(flags);
3851
3852 current->lockdep_recursion = 1;
3853 __lock_unpin_lock(lock, cookie);
Peter Zijlstraa24fc602015-06-11 14:46:53 +02003854 current->lockdep_recursion = 0;
3855 raw_local_irq_restore(flags);
3856}
3857EXPORT_SYMBOL_GPL(lock_unpin_lock);
3858
Nick Piggincf40bd12009-01-21 08:12:39 +01003859void lockdep_set_current_reclaim_state(gfp_t gfp_mask)
3860{
3861 current->lockdep_reclaim_gfp = gfp_mask;
3862}
3863
3864void lockdep_clear_current_reclaim_state(void)
3865{
3866 current->lockdep_reclaim_gfp = 0;
3867}
3868
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003869#ifdef CONFIG_LOCK_STAT
3870static int
3871print_lock_contention_bug(struct task_struct *curr, struct lockdep_map *lock,
3872 unsigned long ip)
3873{
3874 if (!debug_locks_off())
3875 return 0;
3876 if (debug_locks_silent)
3877 return 0;
3878
Paul E. McKenneyb3fbab02011-05-24 08:31:09 -07003879 printk("\n");
3880 printk("=================================\n");
3881 printk("[ BUG: bad contention detected! ]\n");
Ben Hutchingsfbdc4b92011-10-28 04:36:55 +01003882 print_kernel_ident();
Paul E. McKenneyb3fbab02011-05-24 08:31:09 -07003883 printk("---------------------------------\n");
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003884 printk("%s/%d is trying to contend lock (",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07003885 curr->comm, task_pid_nr(curr));
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003886 print_lockdep_cache(lock);
Dmitry Vyukovf943fe02016-11-28 15:24:43 +01003887 printk(KERN_CONT ") at:\n");
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003888 print_ip_sym(ip);
3889 printk("but there are no locks held!\n");
3890 printk("\nother info that might help us debug this:\n");
3891 lockdep_print_held_locks(curr);
3892
3893 printk("\nstack backtrace:\n");
3894 dump_stack();
3895
3896 return 0;
3897}
3898
3899static void
3900__lock_contended(struct lockdep_map *lock, unsigned long ip)
3901{
3902 struct task_struct *curr = current;
3903 struct held_lock *hlock, *prev_hlock;
3904 struct lock_class_stats *stats;
3905 unsigned int depth;
Peter Zijlstrac7e78cf2008-10-16 23:17:09 +02003906 int i, contention_point, contending_point;
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003907
3908 depth = curr->lockdep_depth;
Peter Zijlstra0119fee2011-09-02 01:30:29 +02003909 /*
3910 * Whee, we contended on this lock, except it seems we're not
3911 * actually trying to acquire anything much at all..
3912 */
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003913 if (DEBUG_LOCKS_WARN_ON(!depth))
3914 return;
3915
3916 prev_hlock = NULL;
3917 for (i = depth-1; i >= 0; i--) {
3918 hlock = curr->held_locks + i;
3919 /*
3920 * We must not cross into another context:
3921 */
3922 if (prev_hlock && prev_hlock->irq_context != hlock->irq_context)
3923 break;
Peter Zijlstrabb97a912009-07-20 19:15:35 +02003924 if (match_held_lock(hlock, lock))
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003925 goto found_it;
3926 prev_hlock = hlock;
3927 }
3928 print_lock_contention_bug(curr, lock, ip);
3929 return;
3930
3931found_it:
Peter Zijlstrabb97a912009-07-20 19:15:35 +02003932 if (hlock->instance != lock)
3933 return;
3934
Peter Zijlstra3365e7792009-10-09 10:12:41 +02003935 hlock->waittime_stamp = lockstat_clock();
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003936
Peter Zijlstrac7e78cf2008-10-16 23:17:09 +02003937 contention_point = lock_point(hlock_class(hlock)->contention_point, ip);
3938 contending_point = lock_point(hlock_class(hlock)->contending_point,
3939 lock->ip);
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003940
Dave Jonesf82b2172008-08-11 09:30:23 +02003941 stats = get_lock_stats(hlock_class(hlock));
Peter Zijlstrac7e78cf2008-10-16 23:17:09 +02003942 if (contention_point < LOCKSTAT_POINTS)
3943 stats->contention_point[contention_point]++;
3944 if (contending_point < LOCKSTAT_POINTS)
3945 stats->contending_point[contending_point]++;
Peter Zijlstra96645672007-07-19 01:49:00 -07003946 if (lock->cpu != smp_processor_id())
3947 stats->bounces[bounce_contended + !!hlock->read]++;
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003948 put_lock_stats(stats);
3949}
3950
3951static void
Peter Zijlstrac7e78cf2008-10-16 23:17:09 +02003952__lock_acquired(struct lockdep_map *lock, unsigned long ip)
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003953{
3954 struct task_struct *curr = current;
3955 struct held_lock *hlock, *prev_hlock;
3956 struct lock_class_stats *stats;
3957 unsigned int depth;
Peter Zijlstra3365e7792009-10-09 10:12:41 +02003958 u64 now, waittime = 0;
Peter Zijlstra96645672007-07-19 01:49:00 -07003959 int i, cpu;
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003960
3961 depth = curr->lockdep_depth;
Peter Zijlstra0119fee2011-09-02 01:30:29 +02003962 /*
3963 * Yay, we acquired ownership of this lock we didn't try to
3964 * acquire, how the heck did that happen?
3965 */
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003966 if (DEBUG_LOCKS_WARN_ON(!depth))
3967 return;
3968
3969 prev_hlock = NULL;
3970 for (i = depth-1; i >= 0; i--) {
3971 hlock = curr->held_locks + i;
3972 /*
3973 * We must not cross into another context:
3974 */
3975 if (prev_hlock && prev_hlock->irq_context != hlock->irq_context)
3976 break;
Peter Zijlstrabb97a912009-07-20 19:15:35 +02003977 if (match_held_lock(hlock, lock))
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003978 goto found_it;
3979 prev_hlock = hlock;
3980 }
3981 print_lock_contention_bug(curr, lock, _RET_IP_);
3982 return;
3983
3984found_it:
Peter Zijlstrabb97a912009-07-20 19:15:35 +02003985 if (hlock->instance != lock)
3986 return;
3987
Peter Zijlstra96645672007-07-19 01:49:00 -07003988 cpu = smp_processor_id();
3989 if (hlock->waittime_stamp) {
Peter Zijlstra3365e7792009-10-09 10:12:41 +02003990 now = lockstat_clock();
Peter Zijlstra96645672007-07-19 01:49:00 -07003991 waittime = now - hlock->waittime_stamp;
3992 hlock->holdtime_stamp = now;
3993 }
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003994
Frederic Weisbecker883a2a32010-05-08 06:16:11 +02003995 trace_lock_acquired(lock, ip);
Frederic Weisbecker20625012009-04-06 01:49:33 +02003996
Dave Jonesf82b2172008-08-11 09:30:23 +02003997 stats = get_lock_stats(hlock_class(hlock));
Peter Zijlstra96645672007-07-19 01:49:00 -07003998 if (waittime) {
3999 if (hlock->read)
4000 lock_time_inc(&stats->read_waittime, waittime);
4001 else
4002 lock_time_inc(&stats->write_waittime, waittime);
4003 }
4004 if (lock->cpu != cpu)
4005 stats->bounces[bounce_acquired + !!hlock->read]++;
Peter Zijlstraf20786f2007-07-19 01:48:56 -07004006 put_lock_stats(stats);
Peter Zijlstra96645672007-07-19 01:49:00 -07004007
4008 lock->cpu = cpu;
Peter Zijlstrac7e78cf2008-10-16 23:17:09 +02004009 lock->ip = ip;
Peter Zijlstraf20786f2007-07-19 01:48:56 -07004010}
4011
4012void lock_contended(struct lockdep_map *lock, unsigned long ip)
4013{
4014 unsigned long flags;
4015
Waiman Long81301a12018-10-18 21:45:17 -04004016 if (unlikely(!lock_stat || !debug_locks))
Peter Zijlstraf20786f2007-07-19 01:48:56 -07004017 return;
4018
4019 if (unlikely(current->lockdep_recursion))
4020 return;
4021
4022 raw_local_irq_save(flags);
4023 check_flags(flags);
4024 current->lockdep_recursion = 1;
Frederic Weisbeckerdb2c4c72010-02-02 23:34:40 +01004025 trace_lock_contended(lock, ip);
Peter Zijlstraf20786f2007-07-19 01:48:56 -07004026 __lock_contended(lock, ip);
4027 current->lockdep_recursion = 0;
4028 raw_local_irq_restore(flags);
4029}
4030EXPORT_SYMBOL_GPL(lock_contended);
4031
Peter Zijlstrac7e78cf2008-10-16 23:17:09 +02004032void lock_acquired(struct lockdep_map *lock, unsigned long ip)
Peter Zijlstraf20786f2007-07-19 01:48:56 -07004033{
4034 unsigned long flags;
4035
Waiman Long81301a12018-10-18 21:45:17 -04004036 if (unlikely(!lock_stat || !debug_locks))
Peter Zijlstraf20786f2007-07-19 01:48:56 -07004037 return;
4038
4039 if (unlikely(current->lockdep_recursion))
4040 return;
4041
4042 raw_local_irq_save(flags);
4043 check_flags(flags);
4044 current->lockdep_recursion = 1;
Peter Zijlstrac7e78cf2008-10-16 23:17:09 +02004045 __lock_acquired(lock, ip);
Peter Zijlstraf20786f2007-07-19 01:48:56 -07004046 current->lockdep_recursion = 0;
4047 raw_local_irq_restore(flags);
4048}
4049EXPORT_SYMBOL_GPL(lock_acquired);
4050#endif
4051
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004052/*
4053 * Used by the testsuite, sanitize the validator state
4054 * after a simulated failure:
4055 */
4056
4057void lockdep_reset(void)
4058{
4059 unsigned long flags;
Ingo Molnar23d95a02006-12-13 00:34:40 -08004060 int i;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004061
4062 raw_local_irq_save(flags);
4063 current->curr_chain_key = 0;
4064 current->lockdep_depth = 0;
4065 current->lockdep_recursion = 0;
4066 memset(current->held_locks, 0, MAX_LOCK_DEPTH*sizeof(struct held_lock));
4067 nr_hardirq_chains = 0;
4068 nr_softirq_chains = 0;
4069 nr_process_chains = 0;
4070 debug_locks = 1;
Ingo Molnar23d95a02006-12-13 00:34:40 -08004071 for (i = 0; i < CHAINHASH_SIZE; i++)
Andrew Mortona63f38c2016-02-03 13:44:12 -08004072 INIT_HLIST_HEAD(chainhash_table + i);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004073 raw_local_irq_restore(flags);
4074}
4075
4076static void zap_class(struct lock_class *class)
4077{
4078 int i;
4079
4080 /*
4081 * Remove all dependencies this lock is
4082 * involved in:
4083 */
4084 for (i = 0; i < nr_list_entries; i++) {
4085 if (list_entries[i].class == class)
4086 list_del_rcu(&list_entries[i].entry);
4087 }
4088 /*
4089 * Unhash the class and remove it from the all_lock_classes list:
4090 */
Andrew Mortona63f38c2016-02-03 13:44:12 -08004091 hlist_del_rcu(&class->hash_entry);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004092 list_del_rcu(&class->lock_entry);
4093
Peter Zijlstracee34d82015-06-02 12:50:13 +02004094 RCU_INIT_POINTER(class->key, NULL);
4095 RCU_INIT_POINTER(class->name, NULL);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004096}
4097
Arjan van de Venfabe8742008-01-24 07:00:45 +01004098static inline int within(const void *addr, void *start, unsigned long size)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004099{
4100 return addr >= start && addr < start + size;
4101}
4102
Peter Zijlstra35a93932015-02-26 16:23:11 +01004103/*
4104 * Used in module.c to remove lock classes from memory that is going to be
4105 * freed; and possibly re-used by other modules.
4106 *
4107 * We will have had one sync_sched() before getting here, so we're guaranteed
4108 * nobody will look up these exact classes -- they're properly dead but still
4109 * allocated.
4110 */
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004111void lockdep_free_key_range(void *start, unsigned long size)
4112{
Peter Zijlstra35a93932015-02-26 16:23:11 +01004113 struct lock_class *class;
Andrew Mortona63f38c2016-02-03 13:44:12 -08004114 struct hlist_head *head;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004115 unsigned long flags;
4116 int i;
Nick Piggin5a26db52008-01-16 09:51:58 +01004117 int locked;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004118
4119 raw_local_irq_save(flags);
Nick Piggin5a26db52008-01-16 09:51:58 +01004120 locked = graph_lock();
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004121
4122 /*
4123 * Unhash all classes that were created by this module:
4124 */
4125 for (i = 0; i < CLASSHASH_SIZE; i++) {
4126 head = classhash_table + i;
Andrew Mortona63f38c2016-02-03 13:44:12 -08004127 hlist_for_each_entry_rcu(class, head, hash_entry) {
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004128 if (within(class->key, start, size))
4129 zap_class(class);
Arjan van de Venfabe8742008-01-24 07:00:45 +01004130 else if (within(class->name, start, size))
4131 zap_class(class);
4132 }
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004133 }
4134
Nick Piggin5a26db52008-01-16 09:51:58 +01004135 if (locked)
4136 graph_unlock();
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004137 raw_local_irq_restore(flags);
Peter Zijlstra35a93932015-02-26 16:23:11 +01004138
4139 /*
4140 * Wait for any possible iterators from look_up_lock_class() to pass
4141 * before continuing to free the memory they refer to.
4142 *
4143 * sync_sched() is sufficient because the read-side is IRQ disable.
4144 */
4145 synchronize_sched();
4146
4147 /*
4148 * XXX at this point we could return the resources to the pool;
4149 * instead we leak them. We would need to change to bitmap allocators
4150 * instead of the linear allocators we have now.
4151 */
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004152}
4153
4154void lockdep_reset_lock(struct lockdep_map *lock)
4155{
Peter Zijlstra35a93932015-02-26 16:23:11 +01004156 struct lock_class *class;
Andrew Mortona63f38c2016-02-03 13:44:12 -08004157 struct hlist_head *head;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004158 unsigned long flags;
4159 int i, j;
Nick Piggin5a26db52008-01-16 09:51:58 +01004160 int locked;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004161
4162 raw_local_irq_save(flags);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004163
4164 /*
Ingo Molnard6d897c2006-07-10 04:44:04 -07004165 * Remove all classes this lock might have:
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004166 */
Ingo Molnard6d897c2006-07-10 04:44:04 -07004167 for (j = 0; j < MAX_LOCKDEP_SUBCLASSES; j++) {
4168 /*
4169 * If the class exists we look it up and zap it:
4170 */
4171 class = look_up_lock_class(lock, j);
4172 if (class)
4173 zap_class(class);
4174 }
4175 /*
4176 * Debug check: in the end all mapped classes should
4177 * be gone.
4178 */
Nick Piggin5a26db52008-01-16 09:51:58 +01004179 locked = graph_lock();
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004180 for (i = 0; i < CLASSHASH_SIZE; i++) {
4181 head = classhash_table + i;
Andrew Mortona63f38c2016-02-03 13:44:12 -08004182 hlist_for_each_entry_rcu(class, head, hash_entry) {
Hitoshi Mitake62016252010-10-05 18:01:51 +09004183 int match = 0;
4184
4185 for (j = 0; j < NR_LOCKDEP_CACHING_CLASSES; j++)
4186 match |= class == lock->class_cache[j];
4187
4188 if (unlikely(match)) {
Peter Zijlstra0119fee2011-09-02 01:30:29 +02004189 if (debug_locks_off_graph_unlock()) {
4190 /*
4191 * We all just reset everything, how did it match?
4192 */
Ingo Molnar74c383f2006-12-13 00:34:43 -08004193 WARN_ON(1);
Peter Zijlstra0119fee2011-09-02 01:30:29 +02004194 }
Ingo Molnard6d897c2006-07-10 04:44:04 -07004195 goto out_restore;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004196 }
4197 }
4198 }
Nick Piggin5a26db52008-01-16 09:51:58 +01004199 if (locked)
4200 graph_unlock();
Ingo Molnard6d897c2006-07-10 04:44:04 -07004201
4202out_restore:
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004203 raw_local_irq_restore(flags);
4204}
4205
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004206void __init lockdep_info(void)
4207{
4208 printk("Lock dependency validator: Copyright (c) 2006 Red Hat, Inc., Ingo Molnar\n");
4209
Li Zefanb0788ca2008-11-21 15:57:32 +08004210 printk("... MAX_LOCKDEP_SUBCLASSES: %lu\n", MAX_LOCKDEP_SUBCLASSES);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004211 printk("... MAX_LOCK_DEPTH: %lu\n", MAX_LOCK_DEPTH);
4212 printk("... MAX_LOCKDEP_KEYS: %lu\n", MAX_LOCKDEP_KEYS);
Li Zefanb0788ca2008-11-21 15:57:32 +08004213 printk("... CLASSHASH_SIZE: %lu\n", CLASSHASH_SIZE);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004214 printk("... MAX_LOCKDEP_ENTRIES: %lu\n", MAX_LOCKDEP_ENTRIES);
4215 printk("... MAX_LOCKDEP_CHAINS: %lu\n", MAX_LOCKDEP_CHAINS);
4216 printk("... CHAINHASH_SIZE: %lu\n", CHAINHASH_SIZE);
4217
4218 printk(" memory used by lock dependency info: %lu kB\n",
4219 (sizeof(struct lock_class) * MAX_LOCKDEP_KEYS +
4220 sizeof(struct list_head) * CLASSHASH_SIZE +
4221 sizeof(struct lock_list) * MAX_LOCKDEP_ENTRIES +
4222 sizeof(struct lock_chain) * MAX_LOCKDEP_CHAINS +
Ming Lei90629202009-08-02 21:43:36 +08004223 sizeof(struct list_head) * CHAINHASH_SIZE
Ming Lei4dd861d2009-07-16 15:44:29 +02004224#ifdef CONFIG_PROVE_LOCKING
Ming Leie351b662009-07-22 22:48:09 +08004225 + sizeof(struct circular_queue)
Ming Lei4dd861d2009-07-16 15:44:29 +02004226#endif
Ming Lei90629202009-08-02 21:43:36 +08004227 ) / 1024
Ming Lei4dd861d2009-07-16 15:44:29 +02004228 );
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004229
4230 printk(" per task-struct memory footprint: %lu bytes\n",
4231 sizeof(struct held_lock) * MAX_LOCK_DEPTH);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004232}
4233
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004234static void
4235print_freed_lock_bug(struct task_struct *curr, const void *mem_from,
Arjan van de Ven55794a42006-07-10 04:44:03 -07004236 const void *mem_to, struct held_lock *hlock)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004237{
4238 if (!debug_locks_off())
4239 return;
4240 if (debug_locks_silent)
4241 return;
4242
Paul E. McKenneyb3fbab02011-05-24 08:31:09 -07004243 printk("\n");
4244 printk("=========================\n");
4245 printk("[ BUG: held lock freed! ]\n");
Ben Hutchingsfbdc4b92011-10-28 04:36:55 +01004246 print_kernel_ident();
Paul E. McKenneyb3fbab02011-05-24 08:31:09 -07004247 printk("-------------------------\n");
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004248 printk("%s/%d is freeing memory %p-%p, with a lock still held there!\n",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07004249 curr->comm, task_pid_nr(curr), mem_from, mem_to-1);
Arjan van de Ven55794a42006-07-10 04:44:03 -07004250 print_lock(hlock);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004251 lockdep_print_held_locks(curr);
4252
4253 printk("\nstack backtrace:\n");
4254 dump_stack();
4255}
4256
Oleg Nesterov54561782007-12-05 15:46:09 +01004257static inline int not_in_range(const void* mem_from, unsigned long mem_len,
4258 const void* lock_from, unsigned long lock_len)
4259{
4260 return lock_from + lock_len <= mem_from ||
4261 mem_from + mem_len <= lock_from;
4262}
4263
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004264/*
4265 * Called when kernel memory is freed (or unmapped), or if a lock
4266 * is destroyed or reinitialized - this code checks whether there is
4267 * any held lock in the memory range of <from> to <to>:
4268 */
4269void debug_check_no_locks_freed(const void *mem_from, unsigned long mem_len)
4270{
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004271 struct task_struct *curr = current;
4272 struct held_lock *hlock;
4273 unsigned long flags;
4274 int i;
4275
4276 if (unlikely(!debug_locks))
4277 return;
4278
Steven Rostedt (VMware)a8b8e522018-04-04 14:06:30 -04004279 raw_local_irq_save(flags);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004280 for (i = 0; i < curr->lockdep_depth; i++) {
4281 hlock = curr->held_locks + i;
4282
Oleg Nesterov54561782007-12-05 15:46:09 +01004283 if (not_in_range(mem_from, mem_len, hlock->instance,
4284 sizeof(*hlock->instance)))
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004285 continue;
4286
Oleg Nesterov54561782007-12-05 15:46:09 +01004287 print_freed_lock_bug(curr, mem_from, mem_from + mem_len, hlock);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004288 break;
4289 }
Steven Rostedt (VMware)a8b8e522018-04-04 14:06:30 -04004290 raw_local_irq_restore(flags);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004291}
Peter Zijlstraed075362006-12-06 20:35:24 -08004292EXPORT_SYMBOL_GPL(debug_check_no_locks_freed);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004293
Colin Cross1b1d2fb2013-05-06 23:50:08 +00004294static void print_held_locks_bug(void)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004295{
4296 if (!debug_locks_off())
4297 return;
4298 if (debug_locks_silent)
4299 return;
4300
Paul E. McKenneyb3fbab02011-05-24 08:31:09 -07004301 printk("\n");
4302 printk("=====================================\n");
Colin Cross1b1d2fb2013-05-06 23:50:08 +00004303 printk("[ BUG: %s/%d still has locks held! ]\n",
4304 current->comm, task_pid_nr(current));
Ben Hutchingsfbdc4b92011-10-28 04:36:55 +01004305 print_kernel_ident();
Paul E. McKenneyb3fbab02011-05-24 08:31:09 -07004306 printk("-------------------------------------\n");
Colin Cross1b1d2fb2013-05-06 23:50:08 +00004307 lockdep_print_held_locks(current);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004308 printk("\nstack backtrace:\n");
4309 dump_stack();
4310}
4311
Colin Cross1b1d2fb2013-05-06 23:50:08 +00004312void debug_check_no_locks_held(void)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004313{
Colin Cross1b1d2fb2013-05-06 23:50:08 +00004314 if (unlikely(current->lockdep_depth > 0))
4315 print_held_locks_bug();
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004316}
Colin Cross1b1d2fb2013-05-06 23:50:08 +00004317EXPORT_SYMBOL_GPL(debug_check_no_locks_held);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004318
Sasha Levin8dce7a92013-06-13 18:41:16 -04004319#ifdef __KERNEL__
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004320void debug_show_all_locks(void)
4321{
4322 struct task_struct *g, *p;
4323 int count = 10;
4324 int unlock = 1;
4325
Jarek Poplawski9c35dd72007-03-22 00:11:28 -08004326 if (unlikely(!debug_locks)) {
4327 printk("INFO: lockdep is turned off.\n");
4328 return;
4329 }
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004330 printk("\nShowing all locks held in the system:\n");
4331
4332 /*
4333 * Here we try to get the tasklist_lock as hard as possible,
4334 * if not successful after 2 seconds we ignore it (but keep
4335 * trying). This is to enable a debug printout even if a
4336 * tasklist_lock-holding task deadlocks or crashes.
4337 */
4338retry:
4339 if (!read_trylock(&tasklist_lock)) {
4340 if (count == 10)
4341 printk("hm, tasklist_lock locked, retrying... ");
4342 if (count) {
4343 count--;
4344 printk(" #%d", 10-count);
4345 mdelay(200);
4346 goto retry;
4347 }
4348 printk(" ignoring it.\n");
4349 unlock = 0;
qinghuang feng46fec7a2008-10-28 17:24:28 +08004350 } else {
4351 if (count != 10)
4352 printk(KERN_CONT " locked it.\n");
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004353 }
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004354
4355 do_each_thread(g, p) {
Ingo Molnar85684872007-12-05 15:46:09 +01004356 /*
4357 * It's not reliable to print a task's held locks
4358 * if it's not sleeping (or if it's not the current
4359 * task):
4360 */
4361 if (p->state == TASK_RUNNING && p != current)
4362 continue;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004363 if (p->lockdep_depth)
4364 lockdep_print_held_locks(p);
4365 if (!unlock)
4366 if (read_trylock(&tasklist_lock))
4367 unlock = 1;
4368 } while_each_thread(g, p);
4369
4370 printk("\n");
4371 printk("=============================================\n\n");
4372
4373 if (unlock)
4374 read_unlock(&tasklist_lock);
4375}
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004376EXPORT_SYMBOL_GPL(debug_show_all_locks);
Sasha Levin8dce7a92013-06-13 18:41:16 -04004377#endif
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004378
Ingo Molnar82a1fcb2008-01-25 21:08:02 +01004379/*
4380 * Careful: only use this function if you are sure that
4381 * the task cannot run in parallel!
4382 */
John Kacurf1b499f2010-08-05 17:10:53 +02004383void debug_show_held_locks(struct task_struct *task)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004384{
Jarek Poplawski9c35dd72007-03-22 00:11:28 -08004385 if (unlikely(!debug_locks)) {
4386 printk("INFO: lockdep is turned off.\n");
4387 return;
4388 }
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004389 lockdep_print_held_locks(task);
4390}
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004391EXPORT_SYMBOL_GPL(debug_show_held_locks);
Peter Zijlstrab351d162007-10-11 22:11:12 +02004392
Andi Kleen722a9f92014-05-02 00:44:38 +02004393asmlinkage __visible void lockdep_sys_exit(void)
Peter Zijlstrab351d162007-10-11 22:11:12 +02004394{
4395 struct task_struct *curr = current;
4396
4397 if (unlikely(curr->lockdep_depth)) {
4398 if (!debug_locks_off())
4399 return;
Paul E. McKenneyb3fbab02011-05-24 08:31:09 -07004400 printk("\n");
4401 printk("================================================\n");
4402 printk("[ BUG: lock held when returning to user space! ]\n");
Ben Hutchingsfbdc4b92011-10-28 04:36:55 +01004403 print_kernel_ident();
Paul E. McKenneyb3fbab02011-05-24 08:31:09 -07004404 printk("------------------------------------------------\n");
Peter Zijlstrab351d162007-10-11 22:11:12 +02004405 printk("%s/%d is leaving the kernel with locks still held!\n",
4406 curr->comm, curr->pid);
4407 lockdep_print_held_locks(curr);
4408 }
4409}
Paul E. McKenney0632eb32010-02-22 17:04:47 -08004410
Paul E. McKenneyb3fbab02011-05-24 08:31:09 -07004411void lockdep_rcu_suspicious(const char *file, const int line, const char *s)
Paul E. McKenney0632eb32010-02-22 17:04:47 -08004412{
4413 struct task_struct *curr = current;
4414
Lai Jiangshan2b3fc352010-04-20 16:23:07 +08004415#ifndef CONFIG_PROVE_RCU_REPEATEDLY
Paul E. McKenney0632eb32010-02-22 17:04:47 -08004416 if (!debug_locks_off())
4417 return;
Lai Jiangshan2b3fc352010-04-20 16:23:07 +08004418#endif /* #ifdef CONFIG_PROVE_RCU_REPEATEDLY */
4419 /* Note: the following can be executed concurrently, so be careful. */
Paul E. McKenneyb3fbab02011-05-24 08:31:09 -07004420 printk("\n");
4421 printk("===============================\n");
4422 printk("[ INFO: suspicious RCU usage. ]\n");
Ben Hutchingsfbdc4b92011-10-28 04:36:55 +01004423 print_kernel_ident();
Paul E. McKenneyb3fbab02011-05-24 08:31:09 -07004424 printk("-------------------------------\n");
4425 printk("%s:%d %s!\n", file, line, s);
Paul E. McKenney0632eb32010-02-22 17:04:47 -08004426 printk("\nother info that might help us debug this:\n\n");
Paul E. McKenneyc5fdcec2012-01-30 08:46:32 -08004427 printk("\n%srcu_scheduler_active = %d, debug_locks = %d\n",
4428 !rcu_lockdep_current_cpu_online()
4429 ? "RCU used illegally from offline CPU!\n"
Paul E. McKenney5c173eb2013-09-13 17:20:11 -07004430 : !rcu_is_watching()
Paul E. McKenneyc5fdcec2012-01-30 08:46:32 -08004431 ? "RCU used illegally from idle CPU!\n"
4432 : "",
4433 rcu_scheduler_active, debug_locks);
Frederic Weisbecker0464e932011-10-07 18:22:01 +02004434
4435 /*
4436 * If a CPU is in the RCU-free window in idle (ie: in the section
4437 * between rcu_idle_enter() and rcu_idle_exit(), then RCU
4438 * considers that CPU to be in an "extended quiescent state",
4439 * which means that RCU will be completely ignoring that CPU.
4440 * Therefore, rcu_read_lock() and friends have absolutely no
4441 * effect on a CPU running in that state. In other words, even if
4442 * such an RCU-idle CPU has called rcu_read_lock(), RCU might well
4443 * delete data structures out from under it. RCU really has no
4444 * choice here: we need to keep an RCU-free window in idle where
4445 * the CPU may possibly enter into low power mode. This way we can
4446 * notice an extended quiescent state to other CPUs that started a grace
4447 * period. Otherwise we would delay any grace period as long as we run
4448 * in the idle task.
4449 *
4450 * So complain bitterly if someone does call rcu_read_lock(),
4451 * rcu_read_lock_bh() and so on from extended quiescent states.
4452 */
Paul E. McKenney5c173eb2013-09-13 17:20:11 -07004453 if (!rcu_is_watching())
Frederic Weisbecker0464e932011-10-07 18:22:01 +02004454 printk("RCU used illegally from extended quiescent state!\n");
4455
Paul E. McKenney0632eb32010-02-22 17:04:47 -08004456 lockdep_print_held_locks(curr);
4457 printk("\nstack backtrace:\n");
4458 dump_stack();
4459}
Paul E. McKenneyb3fbab02011-05-24 08:31:09 -07004460EXPORT_SYMBOL_GPL(lockdep_rcu_suspicious);