]> git.proxmox.com Git - mirror_ubuntu-focal-kernel.git/blame - kernel/locking/lockdep.c
sched/headers: Prepare for new header dependencies before moving code to <linux/sched...
[mirror_ubuntu-focal-kernel.git] / kernel / locking / lockdep.c
CommitLineData
fbb9ce95
IM
1/*
2 * kernel/lockdep.c
3 *
4 * Runtime locking correctness validator
5 *
6 * Started by Ingo Molnar:
7 *
4b32d0a4 8 * Copyright (C) 2006,2007 Red Hat, Inc., Ingo Molnar <mingo@redhat.com>
90eec103 9 * Copyright (C) 2007 Red Hat, Inc., Peter Zijlstra
fbb9ce95
IM
10 *
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 */
a5e25883 28#define DISABLE_BRANCH_PROFILING
fbb9ce95
IM
29#include <linux/mutex.h>
30#include <linux/sched.h>
e6017571 31#include <linux/sched/clock.h>
29930025 32#include <linux/sched/task.h>
fbb9ce95
IM
33#include <linux/delay.h>
34#include <linux/module.h>
35#include <linux/proc_fs.h>
36#include <linux/seq_file.h>
37#include <linux/spinlock.h>
38#include <linux/kallsyms.h>
39#include <linux/interrupt.h>
40#include <linux/stacktrace.h>
41#include <linux/debug_locks.h>
42#include <linux/irqflags.h>
99de055a 43#include <linux/utsname.h>
4b32d0a4 44#include <linux/hash.h>
81d68a96 45#include <linux/ftrace.h>
b4b136f4 46#include <linux/stringify.h>
d588e461 47#include <linux/bitops.h>
5a0e3ad6 48#include <linux/gfp.h>
d3d03d4f 49#include <linux/kmemcheck.h>
e7904a28 50#include <linux/random.h>
dfaaf3fa 51#include <linux/jhash.h>
af012961 52
fbb9ce95
IM
53#include <asm/sections.h>
54
55#include "lockdep_internals.h"
56
a8d154b0 57#define CREATE_TRACE_POINTS
67178767 58#include <trace/events/lock.h>
a8d154b0 59
f20786ff
PZ
60#ifdef CONFIG_PROVE_LOCKING
61int prove_locking = 1;
62module_param(prove_locking, int, 0644);
63#else
64#define prove_locking 0
65#endif
66
67#ifdef CONFIG_LOCK_STAT
68int lock_stat = 1;
69module_param(lock_stat, int, 0644);
70#else
71#define lock_stat 0
72#endif
73
fbb9ce95 74/*
74c383f1
IM
75 * lockdep_lock: protects the lockdep graph, the hashes and the
76 * class/list/hash allocators.
fbb9ce95
IM
77 *
78 * This is one of the rare exceptions where it's justified
79 * to use a raw spinlock - we really dont want the spinlock
74c383f1 80 * code to recurse back into the lockdep code...
fbb9ce95 81 */
edc35bd7 82static arch_spinlock_t lockdep_lock = (arch_spinlock_t)__ARCH_SPIN_LOCK_UNLOCKED;
74c383f1
IM
83
84static int graph_lock(void)
85{
0199c4e6 86 arch_spin_lock(&lockdep_lock);
74c383f1
IM
87 /*
88 * Make sure that if another CPU detected a bug while
89 * walking the graph we dont change it (while the other
90 * CPU is busy printing out stuff with the graph lock
91 * dropped already)
92 */
93 if (!debug_locks) {
0199c4e6 94 arch_spin_unlock(&lockdep_lock);
74c383f1
IM
95 return 0;
96 }
bb065afb
SR
97 /* prevent any recursions within lockdep from causing deadlocks */
98 current->lockdep_recursion++;
74c383f1
IM
99 return 1;
100}
101
102static inline int graph_unlock(void)
103{
0119fee4
PZ
104 if (debug_locks && !arch_spin_is_locked(&lockdep_lock)) {
105 /*
106 * The lockdep graph lock isn't locked while we expect it to
107 * be, we're confused now, bye!
108 */
381a2292 109 return DEBUG_LOCKS_WARN_ON(1);
0119fee4 110 }
381a2292 111
bb065afb 112 current->lockdep_recursion--;
0199c4e6 113 arch_spin_unlock(&lockdep_lock);
74c383f1
IM
114 return 0;
115}
116
117/*
118 * Turn lock debugging off and return with 0 if it was off already,
119 * and also release the graph lock:
120 */
121static inline int debug_locks_off_graph_unlock(void)
122{
123 int ret = debug_locks_off();
124
0199c4e6 125 arch_spin_unlock(&lockdep_lock);
74c383f1
IM
126
127 return ret;
128}
fbb9ce95 129
fbb9ce95 130unsigned long nr_list_entries;
af012961 131static struct lock_list list_entries[MAX_LOCKDEP_ENTRIES];
fbb9ce95 132
fbb9ce95
IM
133/*
134 * All data structures here are protected by the global debug_lock.
135 *
136 * Mutex key structs only get allocated, once during bootup, and never
137 * get freed - this significantly simplifies the debugging code.
138 */
139unsigned long nr_lock_classes;
140static struct lock_class lock_classes[MAX_LOCKDEP_KEYS];
141
f82b217e
DJ
142static inline struct lock_class *hlock_class(struct held_lock *hlock)
143{
144 if (!hlock->class_idx) {
0119fee4
PZ
145 /*
146 * Someone passed in garbage, we give up.
147 */
f82b217e
DJ
148 DEBUG_LOCKS_WARN_ON(1);
149 return NULL;
150 }
151 return lock_classes + hlock->class_idx - 1;
152}
153
f20786ff 154#ifdef CONFIG_LOCK_STAT
25528213 155static DEFINE_PER_CPU(struct lock_class_stats[MAX_LOCKDEP_KEYS], cpu_lock_stats);
f20786ff 156
3365e779
PZ
157static inline u64 lockstat_clock(void)
158{
c676329a 159 return local_clock();
3365e779
PZ
160}
161
c7e78cff 162static int lock_point(unsigned long points[], unsigned long ip)
f20786ff
PZ
163{
164 int i;
165
c7e78cff
PZ
166 for (i = 0; i < LOCKSTAT_POINTS; i++) {
167 if (points[i] == 0) {
168 points[i] = ip;
f20786ff
PZ
169 break;
170 }
c7e78cff 171 if (points[i] == ip)
f20786ff
PZ
172 break;
173 }
174
175 return i;
176}
177
3365e779 178static void lock_time_inc(struct lock_time *lt, u64 time)
f20786ff
PZ
179{
180 if (time > lt->max)
181 lt->max = time;
182
109d71c6 183 if (time < lt->min || !lt->nr)
f20786ff
PZ
184 lt->min = time;
185
186 lt->total += time;
187 lt->nr++;
188}
189
c46261de
PZ
190static inline void lock_time_add(struct lock_time *src, struct lock_time *dst)
191{
109d71c6
FR
192 if (!src->nr)
193 return;
194
195 if (src->max > dst->max)
196 dst->max = src->max;
197
198 if (src->min < dst->min || !dst->nr)
199 dst->min = src->min;
200
c46261de
PZ
201 dst->total += src->total;
202 dst->nr += src->nr;
203}
204
205struct lock_class_stats lock_stats(struct lock_class *class)
206{
207 struct lock_class_stats stats;
208 int cpu, i;
209
210 memset(&stats, 0, sizeof(struct lock_class_stats));
211 for_each_possible_cpu(cpu) {
212 struct lock_class_stats *pcs =
1871e52c 213 &per_cpu(cpu_lock_stats, cpu)[class - lock_classes];
c46261de
PZ
214
215 for (i = 0; i < ARRAY_SIZE(stats.contention_point); i++)
216 stats.contention_point[i] += pcs->contention_point[i];
217
c7e78cff
PZ
218 for (i = 0; i < ARRAY_SIZE(stats.contending_point); i++)
219 stats.contending_point[i] += pcs->contending_point[i];
220
c46261de
PZ
221 lock_time_add(&pcs->read_waittime, &stats.read_waittime);
222 lock_time_add(&pcs->write_waittime, &stats.write_waittime);
223
224 lock_time_add(&pcs->read_holdtime, &stats.read_holdtime);
225 lock_time_add(&pcs->write_holdtime, &stats.write_holdtime);
96645678
PZ
226
227 for (i = 0; i < ARRAY_SIZE(stats.bounces); i++)
228 stats.bounces[i] += pcs->bounces[i];
c46261de
PZ
229 }
230
231 return stats;
232}
233
234void clear_lock_stats(struct lock_class *class)
235{
236 int cpu;
237
238 for_each_possible_cpu(cpu) {
239 struct lock_class_stats *cpu_stats =
1871e52c 240 &per_cpu(cpu_lock_stats, cpu)[class - lock_classes];
c46261de
PZ
241
242 memset(cpu_stats, 0, sizeof(struct lock_class_stats));
243 }
244 memset(class->contention_point, 0, sizeof(class->contention_point));
c7e78cff 245 memset(class->contending_point, 0, sizeof(class->contending_point));
c46261de
PZ
246}
247
f20786ff
PZ
248static struct lock_class_stats *get_lock_stats(struct lock_class *class)
249{
1871e52c 250 return &get_cpu_var(cpu_lock_stats)[class - lock_classes];
f20786ff
PZ
251}
252
253static void put_lock_stats(struct lock_class_stats *stats)
254{
1871e52c 255 put_cpu_var(cpu_lock_stats);
f20786ff
PZ
256}
257
258static void lock_release_holdtime(struct held_lock *hlock)
259{
260 struct lock_class_stats *stats;
3365e779 261 u64 holdtime;
f20786ff
PZ
262
263 if (!lock_stat)
264 return;
265
3365e779 266 holdtime = lockstat_clock() - hlock->holdtime_stamp;
f20786ff 267
f82b217e 268 stats = get_lock_stats(hlock_class(hlock));
f20786ff
PZ
269 if (hlock->read)
270 lock_time_inc(&stats->read_holdtime, holdtime);
271 else
272 lock_time_inc(&stats->write_holdtime, holdtime);
273 put_lock_stats(stats);
274}
275#else
276static inline void lock_release_holdtime(struct held_lock *hlock)
277{
278}
279#endif
280
fbb9ce95
IM
281/*
282 * We keep a global list of all lock classes. The list only grows,
283 * never shrinks. The list is only accessed with the lockdep
284 * spinlock lock held.
285 */
286LIST_HEAD(all_lock_classes);
287
288/*
289 * The lockdep classes are in a hash-table as well, for fast lookup:
290 */
291#define CLASSHASH_BITS (MAX_LOCKDEP_KEYS_BITS - 1)
292#define CLASSHASH_SIZE (1UL << CLASSHASH_BITS)
4b32d0a4 293#define __classhashfn(key) hash_long((unsigned long)key, CLASSHASH_BITS)
fbb9ce95
IM
294#define classhashentry(key) (classhash_table + __classhashfn((key)))
295
a63f38cc 296static struct hlist_head classhash_table[CLASSHASH_SIZE];
fbb9ce95 297
fbb9ce95
IM
298/*
299 * We put the lock dependency chains into a hash-table as well, to cache
300 * their existence:
301 */
302#define CHAINHASH_BITS (MAX_LOCKDEP_CHAINS_BITS-1)
303#define CHAINHASH_SIZE (1UL << CHAINHASH_BITS)
4b32d0a4 304#define __chainhashfn(chain) hash_long(chain, CHAINHASH_BITS)
fbb9ce95
IM
305#define chainhashentry(chain) (chainhash_table + __chainhashfn((chain)))
306
a63f38cc 307static struct hlist_head chainhash_table[CHAINHASH_SIZE];
fbb9ce95
IM
308
309/*
310 * The hash key of the lock dependency chains is a hash itself too:
311 * it's a hash of all locks taken up to that lock, including that lock.
312 * It's a 64-bit hash, because it's important for the keys to be
313 * unique.
314 */
dfaaf3fa
PZ
315static inline u64 iterate_chain_key(u64 key, u32 idx)
316{
317 u32 k0 = key, k1 = key >> 32;
318
319 __jhash_mix(idx, k0, k1); /* Macro that modifies arguments! */
320
321 return k0 | (u64)k1 << 32;
322}
fbb9ce95 323
1d09daa5 324void lockdep_off(void)
fbb9ce95
IM
325{
326 current->lockdep_recursion++;
327}
fbb9ce95
IM
328EXPORT_SYMBOL(lockdep_off);
329
1d09daa5 330void lockdep_on(void)
fbb9ce95
IM
331{
332 current->lockdep_recursion--;
333}
fbb9ce95
IM
334EXPORT_SYMBOL(lockdep_on);
335
fbb9ce95
IM
336/*
337 * Debugging switches:
338 */
339
340#define VERBOSE 0
33e94e96 341#define VERY_VERBOSE 0
fbb9ce95
IM
342
343#if VERBOSE
344# define HARDIRQ_VERBOSE 1
345# define SOFTIRQ_VERBOSE 1
cf40bd16 346# define RECLAIM_VERBOSE 1
fbb9ce95
IM
347#else
348# define HARDIRQ_VERBOSE 0
349# define SOFTIRQ_VERBOSE 0
cf40bd16 350# define RECLAIM_VERBOSE 0
fbb9ce95
IM
351#endif
352
cf40bd16 353#if VERBOSE || HARDIRQ_VERBOSE || SOFTIRQ_VERBOSE || RECLAIM_VERBOSE
fbb9ce95
IM
354/*
355 * Quick filtering for interesting events:
356 */
357static int class_filter(struct lock_class *class)
358{
f9829cce
AK
359#if 0
360 /* Example */
fbb9ce95 361 if (class->name_version == 1 &&
f9829cce 362 !strcmp(class->name, "lockname"))
fbb9ce95
IM
363 return 1;
364 if (class->name_version == 1 &&
f9829cce 365 !strcmp(class->name, "&struct->lockfield"))
fbb9ce95 366 return 1;
f9829cce 367#endif
a6640897
IM
368 /* Filter everything else. 1 would be to allow everything else */
369 return 0;
fbb9ce95
IM
370}
371#endif
372
373static int verbose(struct lock_class *class)
374{
375#if VERBOSE
376 return class_filter(class);
377#endif
378 return 0;
379}
380
fbb9ce95
IM
381/*
382 * Stack-trace: tightly packed array of stack backtrace
74c383f1 383 * addresses. Protected by the graph_lock.
fbb9ce95
IM
384 */
385unsigned long nr_stack_trace_entries;
386static unsigned long stack_trace[MAX_STACK_TRACE_ENTRIES];
387
2c522836
DJ
388static void print_lockdep_off(const char *bug_msg)
389{
390 printk(KERN_DEBUG "%s\n", bug_msg);
391 printk(KERN_DEBUG "turning off the locking correctness validator.\n");
acf59377 392#ifdef CONFIG_LOCK_STAT
2c522836 393 printk(KERN_DEBUG "Please attach the output of /proc/lock_stat to the bug report\n");
acf59377 394#endif
2c522836
DJ
395}
396
fbb9ce95
IM
397static int save_trace(struct stack_trace *trace)
398{
399 trace->nr_entries = 0;
400 trace->max_entries = MAX_STACK_TRACE_ENTRIES - nr_stack_trace_entries;
401 trace->entries = stack_trace + nr_stack_trace_entries;
402
5a1b3999 403 trace->skip = 3;
5a1b3999 404
ab1b6f03 405 save_stack_trace(trace);
fbb9ce95 406
4f84f433
PZ
407 /*
408 * Some daft arches put -1 at the end to indicate its a full trace.
409 *
410 * <rant> this is buggy anyway, since it takes a whole extra entry so a
411 * complete trace that maxes out the entries provided will be reported
412 * as incomplete, friggin useless </rant>
413 */
ea5b41f9
TL
414 if (trace->nr_entries != 0 &&
415 trace->entries[trace->nr_entries-1] == ULONG_MAX)
4f84f433
PZ
416 trace->nr_entries--;
417
fbb9ce95
IM
418 trace->max_entries = trace->nr_entries;
419
420 nr_stack_trace_entries += trace->nr_entries;
fbb9ce95 421
4f84f433 422 if (nr_stack_trace_entries >= MAX_STACK_TRACE_ENTRIES-1) {
74c383f1
IM
423 if (!debug_locks_off_graph_unlock())
424 return 0;
425
2c522836 426 print_lockdep_off("BUG: MAX_STACK_TRACE_ENTRIES too low!");
74c383f1
IM
427 dump_stack();
428
fbb9ce95
IM
429 return 0;
430 }
431
432 return 1;
433}
434
435unsigned int nr_hardirq_chains;
436unsigned int nr_softirq_chains;
437unsigned int nr_process_chains;
438unsigned int max_lockdep_depth;
fbb9ce95
IM
439
440#ifdef CONFIG_DEBUG_LOCKDEP
fbb9ce95
IM
441/*
442 * Various lockdep statistics:
443 */
bd6d29c2 444DEFINE_PER_CPU(struct lockdep_stats, lockdep_stats);
fbb9ce95
IM
445#endif
446
447/*
448 * Locking printouts:
449 */
450
fabe9c42 451#define __USAGE(__STATE) \
b4b136f4
PZ
452 [LOCK_USED_IN_##__STATE] = "IN-"__stringify(__STATE)"-W", \
453 [LOCK_ENABLED_##__STATE] = __stringify(__STATE)"-ON-W", \
454 [LOCK_USED_IN_##__STATE##_READ] = "IN-"__stringify(__STATE)"-R",\
455 [LOCK_ENABLED_##__STATE##_READ] = __stringify(__STATE)"-ON-R",
fabe9c42 456
fbb9ce95
IM
457static const char *usage_str[] =
458{
fabe9c42
PZ
459#define LOCKDEP_STATE(__STATE) __USAGE(__STATE)
460#include "lockdep_states.h"
461#undef LOCKDEP_STATE
462 [LOCK_USED] = "INITIAL USE",
fbb9ce95
IM
463};
464
465const char * __get_key_name(struct lockdep_subclass_key *key, char *str)
466{
ffb45122 467 return kallsyms_lookup((unsigned long)key, NULL, NULL, NULL, str);
fbb9ce95
IM
468}
469
3ff176ca 470static inline unsigned long lock_flag(enum lock_usage_bit bit)
fbb9ce95 471{
3ff176ca
PZ
472 return 1UL << bit;
473}
fbb9ce95 474
3ff176ca
PZ
475static char get_usage_char(struct lock_class *class, enum lock_usage_bit bit)
476{
477 char c = '.';
478
479 if (class->usage_mask & lock_flag(bit + 2))
480 c = '+';
481 if (class->usage_mask & lock_flag(bit)) {
482 c = '-';
483 if (class->usage_mask & lock_flag(bit + 2))
484 c = '?';
fbb9ce95
IM
485 }
486
3ff176ca
PZ
487 return c;
488}
cf40bd16 489
f510b233 490void get_usage_chars(struct lock_class *class, char usage[LOCK_USAGE_CHARS])
3ff176ca 491{
f510b233 492 int i = 0;
cf40bd16 493
f510b233
PZ
494#define LOCKDEP_STATE(__STATE) \
495 usage[i++] = get_usage_char(class, LOCK_USED_IN_##__STATE); \
496 usage[i++] = get_usage_char(class, LOCK_USED_IN_##__STATE##_READ);
497#include "lockdep_states.h"
498#undef LOCKDEP_STATE
499
500 usage[i] = '\0';
fbb9ce95
IM
501}
502
e5e78d08 503static void __print_lock_name(struct lock_class *class)
3003eba3
SR
504{
505 char str[KSYM_NAME_LEN];
506 const char *name;
507
fbb9ce95
IM
508 name = class->name;
509 if (!name) {
510 name = __get_key_name(class->key, str);
f943fe0f 511 printk(KERN_CONT "%s", name);
fbb9ce95 512 } else {
f943fe0f 513 printk(KERN_CONT "%s", name);
fbb9ce95 514 if (class->name_version > 1)
f943fe0f 515 printk(KERN_CONT "#%d", class->name_version);
fbb9ce95 516 if (class->subclass)
f943fe0f 517 printk(KERN_CONT "/%d", class->subclass);
fbb9ce95 518 }
e5e78d08
SR
519}
520
521static void print_lock_name(struct lock_class *class)
522{
523 char usage[LOCK_USAGE_CHARS];
524
525 get_usage_chars(class, usage);
526
f943fe0f 527 printk(KERN_CONT " (");
e5e78d08 528 __print_lock_name(class);
f943fe0f 529 printk(KERN_CONT "){%s}", usage);
fbb9ce95
IM
530}
531
532static void print_lockdep_cache(struct lockdep_map *lock)
533{
534 const char *name;
9281acea 535 char str[KSYM_NAME_LEN];
fbb9ce95
IM
536
537 name = lock->name;
538 if (!name)
539 name = __get_key_name(lock->key->subkeys, str);
540
f943fe0f 541 printk(KERN_CONT "%s", name);
fbb9ce95
IM
542}
543
544static void print_lock(struct held_lock *hlock)
545{
d7bc3197
PZ
546 /*
547 * We can be called locklessly through debug_show_all_locks() so be
548 * extra careful, the hlock might have been released and cleared.
549 */
550 unsigned int class_idx = hlock->class_idx;
551
552 /* Don't re-read hlock->class_idx, can't use READ_ONCE() on bitfields: */
553 barrier();
554
555 if (!class_idx || (class_idx - 1) >= MAX_LOCKDEP_KEYS) {
f943fe0f 556 printk(KERN_CONT "<RELEASED>\n");
d7bc3197
PZ
557 return;
558 }
559
560 print_lock_name(lock_classes + class_idx - 1);
f943fe0f
DV
561 printk(KERN_CONT ", at: [<%p>] %pS\n",
562 (void *)hlock->acquire_ip, (void *)hlock->acquire_ip);
fbb9ce95
IM
563}
564
565static void lockdep_print_held_locks(struct task_struct *curr)
566{
567 int i, depth = curr->lockdep_depth;
568
569 if (!depth) {
ba25f9dc 570 printk("no locks held by %s/%d.\n", curr->comm, task_pid_nr(curr));
fbb9ce95
IM
571 return;
572 }
573 printk("%d lock%s held by %s/%d:\n",
ba25f9dc 574 depth, depth > 1 ? "s" : "", curr->comm, task_pid_nr(curr));
fbb9ce95
IM
575
576 for (i = 0; i < depth; i++) {
577 printk(" #%d: ", i);
578 print_lock(curr->held_locks + i);
579 }
580}
fbb9ce95 581
fbdc4b9a 582static void print_kernel_ident(void)
8e18257d 583{
fbdc4b9a 584 printk("%s %.*s %s\n", init_utsname()->release,
8e18257d 585 (int)strcspn(init_utsname()->version, " "),
fbdc4b9a
BH
586 init_utsname()->version,
587 print_tainted());
8e18257d
PZ
588}
589
590static int very_verbose(struct lock_class *class)
591{
592#if VERY_VERBOSE
593 return class_filter(class);
594#endif
595 return 0;
596}
597
fbb9ce95 598/*
8e18257d 599 * Is this the address of a static object:
fbb9ce95 600 */
8dce7a9a 601#ifdef __KERNEL__
8e18257d 602static int static_obj(void *obj)
fbb9ce95 603{
8e18257d
PZ
604 unsigned long start = (unsigned long) &_stext,
605 end = (unsigned long) &_end,
606 addr = (unsigned long) obj;
8e18257d 607
fbb9ce95 608 /*
8e18257d 609 * static variable?
fbb9ce95 610 */
8e18257d
PZ
611 if ((addr >= start) && (addr < end))
612 return 1;
fbb9ce95 613
2a9ad18d
MF
614 if (arch_is_kernel_data(addr))
615 return 1;
616
fbb9ce95 617 /*
10fad5e4 618 * in-kernel percpu var?
fbb9ce95 619 */
10fad5e4
TH
620 if (is_kernel_percpu_address(addr))
621 return 1;
fbb9ce95 622
8e18257d 623 /*
10fad5e4 624 * module static or percpu var?
8e18257d 625 */
10fad5e4 626 return is_module_address(addr) || is_module_percpu_address(addr);
99de055a 627}
8dce7a9a 628#endif
99de055a 629
fbb9ce95 630/*
8e18257d
PZ
631 * To make lock name printouts unique, we calculate a unique
632 * class->name_version generation counter:
fbb9ce95 633 */
8e18257d 634static int count_matching_names(struct lock_class *new_class)
fbb9ce95 635{
8e18257d
PZ
636 struct lock_class *class;
637 int count = 0;
fbb9ce95 638
8e18257d 639 if (!new_class->name)
fbb9ce95
IM
640 return 0;
641
35a9393c 642 list_for_each_entry_rcu(class, &all_lock_classes, lock_entry) {
8e18257d
PZ
643 if (new_class->key - new_class->subclass == class->key)
644 return class->name_version;
645 if (class->name && !strcmp(class->name, new_class->name))
646 count = max(count, class->name_version);
647 }
fbb9ce95 648
8e18257d 649 return count + 1;
fbb9ce95
IM
650}
651
8e18257d
PZ
652/*
653 * Register a lock's class in the hash-table, if the class is not present
654 * yet. Otherwise we look it up. We cache the result in the lock object
655 * itself, so actual lookup of the hash should be once per lock object.
656 */
657static inline struct lock_class *
658look_up_lock_class(struct lockdep_map *lock, unsigned int subclass)
fbb9ce95 659{
8e18257d 660 struct lockdep_subclass_key *key;
a63f38cc 661 struct hlist_head *hash_head;
8e18257d 662 struct lock_class *class;
fbb9ce95 663
4ba053c0
HM
664 if (unlikely(subclass >= MAX_LOCKDEP_SUBCLASSES)) {
665 debug_locks_off();
666 printk(KERN_ERR
667 "BUG: looking up invalid subclass: %u\n", subclass);
668 printk(KERN_ERR
669 "turning off the locking correctness validator.\n");
670 dump_stack();
671 return NULL;
672 }
673
8e18257d
PZ
674 /*
675 * Static locks do not have their class-keys yet - for them the key
676 * is the lock object itself:
677 */
678 if (unlikely(!lock->key))
679 lock->key = (void *)lock;
fbb9ce95 680
8e18257d
PZ
681 /*
682 * NOTE: the class-key must be unique. For dynamic locks, a static
683 * lock_class_key variable is passed in through the mutex_init()
684 * (or spin_lock_init()) call - which acts as the key. For static
685 * locks we use the lock object itself as the key.
686 */
4b32d0a4
PZ
687 BUILD_BUG_ON(sizeof(struct lock_class_key) >
688 sizeof(struct lockdep_map));
fbb9ce95 689
8e18257d 690 key = lock->key->subkeys + subclass;
ca268c69 691
8e18257d 692 hash_head = classhashentry(key);
74c383f1 693
8e18257d 694 /*
35a9393c 695 * We do an RCU walk of the hash, see lockdep_free_key_range().
8e18257d 696 */
35a9393c
PZ
697 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
698 return NULL;
699
a63f38cc 700 hlist_for_each_entry_rcu(class, hash_head, hash_entry) {
4b32d0a4 701 if (class->key == key) {
0119fee4
PZ
702 /*
703 * Huh! same key, different name? Did someone trample
704 * on some memory? We're most confused.
705 */
4b32d0a4 706 WARN_ON_ONCE(class->name != lock->name);
8e18257d 707 return class;
4b32d0a4
PZ
708 }
709 }
fbb9ce95 710
8e18257d 711 return NULL;
fbb9ce95
IM
712}
713
714/*
8e18257d
PZ
715 * Register a lock's class in the hash-table, if the class is not present
716 * yet. Otherwise we look it up. We cache the result in the lock object
717 * itself, so actual lookup of the hash should be once per lock object.
fbb9ce95 718 */
c003ed92 719static struct lock_class *
8e18257d 720register_lock_class(struct lockdep_map *lock, unsigned int subclass, int force)
fbb9ce95 721{
8e18257d 722 struct lockdep_subclass_key *key;
a63f38cc 723 struct hlist_head *hash_head;
8e18257d 724 struct lock_class *class;
35a9393c
PZ
725
726 DEBUG_LOCKS_WARN_ON(!irqs_disabled());
8e18257d
PZ
727
728 class = look_up_lock_class(lock, subclass);
729 if (likely(class))
87cdee71 730 goto out_set_class_cache;
8e18257d
PZ
731
732 /*
733 * Debug-check: all keys must be persistent!
734 */
735 if (!static_obj(lock->key)) {
736 debug_locks_off();
737 printk("INFO: trying to register non-static key.\n");
738 printk("the code is fine but needs lockdep annotation.\n");
739 printk("turning off the locking correctness validator.\n");
740 dump_stack();
741
742 return NULL;
743 }
744
745 key = lock->key->subkeys + subclass;
746 hash_head = classhashentry(key);
747
8e18257d 748 if (!graph_lock()) {
8e18257d
PZ
749 return NULL;
750 }
751 /*
752 * We have to do the hash-walk again, to avoid races
753 * with another CPU:
754 */
a63f38cc 755 hlist_for_each_entry_rcu(class, hash_head, hash_entry) {
8e18257d
PZ
756 if (class->key == key)
757 goto out_unlock_set;
35a9393c
PZ
758 }
759
8e18257d
PZ
760 /*
761 * Allocate a new key from the static array, and add it to
762 * the hash:
763 */
764 if (nr_lock_classes >= MAX_LOCKDEP_KEYS) {
765 if (!debug_locks_off_graph_unlock()) {
8e18257d
PZ
766 return NULL;
767 }
8e18257d 768
2c522836 769 print_lockdep_off("BUG: MAX_LOCKDEP_KEYS too low!");
eedeeabd 770 dump_stack();
8e18257d
PZ
771 return NULL;
772 }
773 class = lock_classes + nr_lock_classes++;
bd6d29c2 774 debug_atomic_inc(nr_unused_locks);
8e18257d
PZ
775 class->key = key;
776 class->name = lock->name;
777 class->subclass = subclass;
778 INIT_LIST_HEAD(&class->lock_entry);
779 INIT_LIST_HEAD(&class->locks_before);
780 INIT_LIST_HEAD(&class->locks_after);
781 class->name_version = count_matching_names(class);
782 /*
783 * We use RCU's safe list-add method to make
784 * parallel walking of the hash-list safe:
785 */
a63f38cc 786 hlist_add_head_rcu(&class->hash_entry, hash_head);
1481197b
DF
787 /*
788 * Add it to the global list of classes:
789 */
790 list_add_tail_rcu(&class->lock_entry, &all_lock_classes);
8e18257d
PZ
791
792 if (verbose(class)) {
793 graph_unlock();
8e18257d
PZ
794
795 printk("\nnew class %p: %s", class->key, class->name);
796 if (class->name_version > 1)
f943fe0f
DV
797 printk(KERN_CONT "#%d", class->name_version);
798 printk(KERN_CONT "\n");
8e18257d
PZ
799 dump_stack();
800
8e18257d 801 if (!graph_lock()) {
8e18257d
PZ
802 return NULL;
803 }
804 }
805out_unlock_set:
806 graph_unlock();
8e18257d 807
87cdee71 808out_set_class_cache:
8e18257d 809 if (!subclass || force)
62016250
HM
810 lock->class_cache[0] = class;
811 else if (subclass < NR_LOCKDEP_CACHING_CLASSES)
812 lock->class_cache[subclass] = class;
8e18257d 813
0119fee4
PZ
814 /*
815 * Hash collision, did we smoke some? We found a class with a matching
816 * hash but the subclass -- which is hashed in -- didn't match.
817 */
8e18257d
PZ
818 if (DEBUG_LOCKS_WARN_ON(class->subclass != subclass))
819 return NULL;
820
821 return class;
822}
823
824#ifdef CONFIG_PROVE_LOCKING
825/*
826 * Allocate a lockdep entry. (assumes the graph_lock held, returns
827 * with NULL on failure)
828 */
829static struct lock_list *alloc_list_entry(void)
830{
831 if (nr_list_entries >= MAX_LOCKDEP_ENTRIES) {
832 if (!debug_locks_off_graph_unlock())
833 return NULL;
834
2c522836 835 print_lockdep_off("BUG: MAX_LOCKDEP_ENTRIES too low!");
eedeeabd 836 dump_stack();
8e18257d
PZ
837 return NULL;
838 }
839 return list_entries + nr_list_entries++;
840}
841
842/*
843 * Add a new dependency to the head of the list:
844 */
83f06168
TE
845static int add_lock_to_list(struct lock_class *this, struct list_head *head,
846 unsigned long ip, int distance,
847 struct stack_trace *trace)
8e18257d
PZ
848{
849 struct lock_list *entry;
850 /*
851 * Lock not present yet - get a new dependency struct and
852 * add it to the list:
853 */
854 entry = alloc_list_entry();
855 if (!entry)
856 return 0;
857
74870172
ZY
858 entry->class = this;
859 entry->distance = distance;
4726f2a6 860 entry->trace = *trace;
8e18257d 861 /*
35a9393c
PZ
862 * Both allocation and removal are done under the graph lock; but
863 * iteration is under RCU-sched; see look_up_lock_class() and
864 * lockdep_free_key_range().
8e18257d
PZ
865 */
866 list_add_tail_rcu(&entry->entry, head);
867
868 return 1;
869}
870
98c33edd
PZ
871/*
872 * For good efficiency of modular, we use power of 2
873 */
af012961
PZ
874#define MAX_CIRCULAR_QUEUE_SIZE 4096UL
875#define CQ_MASK (MAX_CIRCULAR_QUEUE_SIZE-1)
876
98c33edd
PZ
877/*
878 * The circular_queue and helpers is used to implement the
af012961
PZ
879 * breadth-first search(BFS)algorithem, by which we can build
880 * the shortest path from the next lock to be acquired to the
881 * previous held lock if there is a circular between them.
98c33edd 882 */
af012961
PZ
883struct circular_queue {
884 unsigned long element[MAX_CIRCULAR_QUEUE_SIZE];
885 unsigned int front, rear;
886};
887
888static struct circular_queue lock_cq;
af012961 889
12f3dfd0 890unsigned int max_bfs_queue_depth;
af012961 891
e351b660
ML
892static unsigned int lockdep_dependency_gen_id;
893
af012961
PZ
894static inline void __cq_init(struct circular_queue *cq)
895{
896 cq->front = cq->rear = 0;
e351b660 897 lockdep_dependency_gen_id++;
af012961
PZ
898}
899
900static inline int __cq_empty(struct circular_queue *cq)
901{
902 return (cq->front == cq->rear);
903}
904
905static inline int __cq_full(struct circular_queue *cq)
906{
907 return ((cq->rear + 1) & CQ_MASK) == cq->front;
908}
909
910static inline int __cq_enqueue(struct circular_queue *cq, unsigned long elem)
911{
912 if (__cq_full(cq))
913 return -1;
914
915 cq->element[cq->rear] = elem;
916 cq->rear = (cq->rear + 1) & CQ_MASK;
917 return 0;
918}
919
920static inline int __cq_dequeue(struct circular_queue *cq, unsigned long *elem)
921{
922 if (__cq_empty(cq))
923 return -1;
924
925 *elem = cq->element[cq->front];
926 cq->front = (cq->front + 1) & CQ_MASK;
927 return 0;
928}
929
930static inline unsigned int __cq_get_elem_count(struct circular_queue *cq)
931{
932 return (cq->rear - cq->front) & CQ_MASK;
933}
934
935static inline void mark_lock_accessed(struct lock_list *lock,
936 struct lock_list *parent)
937{
938 unsigned long nr;
98c33edd 939
af012961 940 nr = lock - list_entries;
0119fee4 941 WARN_ON(nr >= nr_list_entries); /* Out-of-bounds, input fail */
af012961 942 lock->parent = parent;
e351b660 943 lock->class->dep_gen_id = lockdep_dependency_gen_id;
af012961
PZ
944}
945
946static inline unsigned long lock_accessed(struct lock_list *lock)
947{
948 unsigned long nr;
98c33edd 949
af012961 950 nr = lock - list_entries;
0119fee4 951 WARN_ON(nr >= nr_list_entries); /* Out-of-bounds, input fail */
e351b660 952 return lock->class->dep_gen_id == lockdep_dependency_gen_id;
af012961
PZ
953}
954
955static inline struct lock_list *get_lock_parent(struct lock_list *child)
956{
957 return child->parent;
958}
959
960static inline int get_lock_depth(struct lock_list *child)
961{
962 int depth = 0;
963 struct lock_list *parent;
964
965 while ((parent = get_lock_parent(child))) {
966 child = parent;
967 depth++;
968 }
969 return depth;
970}
971
9e2d551e 972static int __bfs(struct lock_list *source_entry,
af012961
PZ
973 void *data,
974 int (*match)(struct lock_list *entry, void *data),
975 struct lock_list **target_entry,
976 int forward)
c94aa5ca
ML
977{
978 struct lock_list *entry;
d588e461 979 struct list_head *head;
c94aa5ca
ML
980 struct circular_queue *cq = &lock_cq;
981 int ret = 1;
982
9e2d551e 983 if (match(source_entry, data)) {
c94aa5ca
ML
984 *target_entry = source_entry;
985 ret = 0;
986 goto exit;
987 }
988
d588e461
ML
989 if (forward)
990 head = &source_entry->class->locks_after;
991 else
992 head = &source_entry->class->locks_before;
993
994 if (list_empty(head))
995 goto exit;
996
997 __cq_init(cq);
c94aa5ca
ML
998 __cq_enqueue(cq, (unsigned long)source_entry);
999
1000 while (!__cq_empty(cq)) {
1001 struct lock_list *lock;
c94aa5ca
ML
1002
1003 __cq_dequeue(cq, (unsigned long *)&lock);
1004
1005 if (!lock->class) {
1006 ret = -2;
1007 goto exit;
1008 }
1009
1010 if (forward)
1011 head = &lock->class->locks_after;
1012 else
1013 head = &lock->class->locks_before;
1014
35a9393c
PZ
1015 DEBUG_LOCKS_WARN_ON(!irqs_disabled());
1016
1017 list_for_each_entry_rcu(entry, head, entry) {
c94aa5ca 1018 if (!lock_accessed(entry)) {
12f3dfd0 1019 unsigned int cq_depth;
c94aa5ca 1020 mark_lock_accessed(entry, lock);
9e2d551e 1021 if (match(entry, data)) {
c94aa5ca
ML
1022 *target_entry = entry;
1023 ret = 0;
1024 goto exit;
1025 }
1026
1027 if (__cq_enqueue(cq, (unsigned long)entry)) {
1028 ret = -1;
1029 goto exit;
1030 }
12f3dfd0
ML
1031 cq_depth = __cq_get_elem_count(cq);
1032 if (max_bfs_queue_depth < cq_depth)
1033 max_bfs_queue_depth = cq_depth;
c94aa5ca
ML
1034 }
1035 }
1036 }
1037exit:
1038 return ret;
1039}
1040
d7aaba14 1041static inline int __bfs_forwards(struct lock_list *src_entry,
9e2d551e
ML
1042 void *data,
1043 int (*match)(struct lock_list *entry, void *data),
1044 struct lock_list **target_entry)
c94aa5ca 1045{
9e2d551e 1046 return __bfs(src_entry, data, match, target_entry, 1);
c94aa5ca
ML
1047
1048}
1049
d7aaba14 1050static inline int __bfs_backwards(struct lock_list *src_entry,
9e2d551e
ML
1051 void *data,
1052 int (*match)(struct lock_list *entry, void *data),
1053 struct lock_list **target_entry)
c94aa5ca 1054{
9e2d551e 1055 return __bfs(src_entry, data, match, target_entry, 0);
c94aa5ca
ML
1056
1057}
1058
8e18257d
PZ
1059/*
1060 * Recursive, forwards-direction lock-dependency checking, used for
1061 * both noncyclic checking and for hardirq-unsafe/softirq-unsafe
1062 * checking.
8e18257d 1063 */
8e18257d
PZ
1064
1065/*
1066 * Print a dependency chain entry (this is only done when a deadlock
1067 * has been detected):
1068 */
1069static noinline int
24208ca7 1070print_circular_bug_entry(struct lock_list *target, int depth)
8e18257d
PZ
1071{
1072 if (debug_locks_silent)
1073 return 0;
1074 printk("\n-> #%u", depth);
1075 print_lock_name(target->class);
f943fe0f 1076 printk(KERN_CONT ":\n");
8e18257d
PZ
1077 print_stack_trace(&target->trace, 6);
1078
1079 return 0;
1080}
1081
f4185812
SR
1082static void
1083print_circular_lock_scenario(struct held_lock *src,
1084 struct held_lock *tgt,
1085 struct lock_list *prt)
1086{
1087 struct lock_class *source = hlock_class(src);
1088 struct lock_class *target = hlock_class(tgt);
1089 struct lock_class *parent = prt->class;
1090
1091 /*
1092 * A direct locking problem where unsafe_class lock is taken
1093 * directly by safe_class lock, then all we need to show
1094 * is the deadlock scenario, as it is obvious that the
1095 * unsafe lock is taken under the safe lock.
1096 *
1097 * But if there is a chain instead, where the safe lock takes
1098 * an intermediate lock (middle_class) where this lock is
1099 * not the same as the safe lock, then the lock chain is
1100 * used to describe the problem. Otherwise we would need
1101 * to show a different CPU case for each link in the chain
1102 * from the safe_class lock to the unsafe_class lock.
1103 */
1104 if (parent != source) {
1105 printk("Chain exists of:\n ");
1106 __print_lock_name(source);
f943fe0f 1107 printk(KERN_CONT " --> ");
f4185812 1108 __print_lock_name(parent);
f943fe0f 1109 printk(KERN_CONT " --> ");
f4185812 1110 __print_lock_name(target);
f943fe0f 1111 printk(KERN_CONT "\n\n");
f4185812
SR
1112 }
1113
1114 printk(" Possible unsafe locking scenario:\n\n");
1115 printk(" CPU0 CPU1\n");
1116 printk(" ---- ----\n");
1117 printk(" lock(");
1118 __print_lock_name(target);
f943fe0f 1119 printk(KERN_CONT ");\n");
f4185812
SR
1120 printk(" lock(");
1121 __print_lock_name(parent);
f943fe0f 1122 printk(KERN_CONT ");\n");
f4185812
SR
1123 printk(" lock(");
1124 __print_lock_name(target);
f943fe0f 1125 printk(KERN_CONT ");\n");
f4185812
SR
1126 printk(" lock(");
1127 __print_lock_name(source);
f943fe0f 1128 printk(KERN_CONT ");\n");
f4185812
SR
1129 printk("\n *** DEADLOCK ***\n\n");
1130}
1131
8e18257d
PZ
1132/*
1133 * When a circular dependency is detected, print the
1134 * header first:
1135 */
1136static noinline int
db0002a3
ML
1137print_circular_bug_header(struct lock_list *entry, unsigned int depth,
1138 struct held_lock *check_src,
1139 struct held_lock *check_tgt)
8e18257d
PZ
1140{
1141 struct task_struct *curr = current;
1142
c94aa5ca 1143 if (debug_locks_silent)
8e18257d
PZ
1144 return 0;
1145
b3fbab05
PM
1146 printk("\n");
1147 printk("======================================================\n");
1148 printk("[ INFO: possible circular locking dependency detected ]\n");
fbdc4b9a 1149 print_kernel_ident();
b3fbab05 1150 printk("-------------------------------------------------------\n");
8e18257d 1151 printk("%s/%d is trying to acquire lock:\n",
ba25f9dc 1152 curr->comm, task_pid_nr(curr));
db0002a3 1153 print_lock(check_src);
8e18257d 1154 printk("\nbut task is already holding lock:\n");
db0002a3 1155 print_lock(check_tgt);
8e18257d
PZ
1156 printk("\nwhich lock already depends on the new lock.\n\n");
1157 printk("\nthe existing dependency chain (in reverse order) is:\n");
1158
1159 print_circular_bug_entry(entry, depth);
1160
1161 return 0;
1162}
1163
9e2d551e
ML
1164static inline int class_equal(struct lock_list *entry, void *data)
1165{
1166 return entry->class == data;
1167}
1168
db0002a3
ML
1169static noinline int print_circular_bug(struct lock_list *this,
1170 struct lock_list *target,
1171 struct held_lock *check_src,
1172 struct held_lock *check_tgt)
8e18257d
PZ
1173{
1174 struct task_struct *curr = current;
c94aa5ca 1175 struct lock_list *parent;
f4185812 1176 struct lock_list *first_parent;
24208ca7 1177 int depth;
8e18257d 1178
c94aa5ca 1179 if (!debug_locks_off_graph_unlock() || debug_locks_silent)
8e18257d
PZ
1180 return 0;
1181
db0002a3 1182 if (!save_trace(&this->trace))
8e18257d
PZ
1183 return 0;
1184
c94aa5ca
ML
1185 depth = get_lock_depth(target);
1186
db0002a3 1187 print_circular_bug_header(target, depth, check_src, check_tgt);
c94aa5ca
ML
1188
1189 parent = get_lock_parent(target);
f4185812 1190 first_parent = parent;
c94aa5ca
ML
1191
1192 while (parent) {
1193 print_circular_bug_entry(parent, --depth);
1194 parent = get_lock_parent(parent);
1195 }
8e18257d
PZ
1196
1197 printk("\nother info that might help us debug this:\n\n");
f4185812
SR
1198 print_circular_lock_scenario(check_src, check_tgt,
1199 first_parent);
1200
8e18257d
PZ
1201 lockdep_print_held_locks(curr);
1202
1203 printk("\nstack backtrace:\n");
1204 dump_stack();
1205
1206 return 0;
1207}
1208
db0002a3
ML
1209static noinline int print_bfs_bug(int ret)
1210{
1211 if (!debug_locks_off_graph_unlock())
1212 return 0;
1213
0119fee4
PZ
1214 /*
1215 * Breadth-first-search failed, graph got corrupted?
1216 */
db0002a3
ML
1217 WARN(1, "lockdep bfs error:%d\n", ret);
1218
1219 return 0;
1220}
1221
ef681026 1222static int noop_count(struct lock_list *entry, void *data)
419ca3f1 1223{
ef681026
ML
1224 (*(unsigned long *)data)++;
1225 return 0;
1226}
419ca3f1 1227
5216d530 1228static unsigned long __lockdep_count_forward_deps(struct lock_list *this)
ef681026
ML
1229{
1230 unsigned long count = 0;
1231 struct lock_list *uninitialized_var(target_entry);
419ca3f1 1232
ef681026 1233 __bfs_forwards(this, (void *)&count, noop_count, &target_entry);
419ca3f1 1234
ef681026 1235 return count;
419ca3f1 1236}
419ca3f1
DM
1237unsigned long lockdep_count_forward_deps(struct lock_class *class)
1238{
1239 unsigned long ret, flags;
ef681026
ML
1240 struct lock_list this;
1241
1242 this.parent = NULL;
1243 this.class = class;
419ca3f1
DM
1244
1245 local_irq_save(flags);
0199c4e6 1246 arch_spin_lock(&lockdep_lock);
ef681026 1247 ret = __lockdep_count_forward_deps(&this);
0199c4e6 1248 arch_spin_unlock(&lockdep_lock);
419ca3f1
DM
1249 local_irq_restore(flags);
1250
1251 return ret;
1252}
1253
5216d530 1254static unsigned long __lockdep_count_backward_deps(struct lock_list *this)
419ca3f1 1255{
ef681026
ML
1256 unsigned long count = 0;
1257 struct lock_list *uninitialized_var(target_entry);
419ca3f1 1258
ef681026 1259 __bfs_backwards(this, (void *)&count, noop_count, &target_entry);
419ca3f1 1260
ef681026 1261 return count;
419ca3f1
DM
1262}
1263
1264unsigned long lockdep_count_backward_deps(struct lock_class *class)
1265{
1266 unsigned long ret, flags;
ef681026
ML
1267 struct lock_list this;
1268
1269 this.parent = NULL;
1270 this.class = class;
419ca3f1
DM
1271
1272 local_irq_save(flags);
0199c4e6 1273 arch_spin_lock(&lockdep_lock);
ef681026 1274 ret = __lockdep_count_backward_deps(&this);
0199c4e6 1275 arch_spin_unlock(&lockdep_lock);
419ca3f1
DM
1276 local_irq_restore(flags);
1277
1278 return ret;
1279}
1280
8e18257d
PZ
1281/*
1282 * Prove that the dependency graph starting at <entry> can not
1283 * lead to <target>. Print an error and return 0 if it does.
1284 */
1285static noinline int
db0002a3
ML
1286check_noncircular(struct lock_list *root, struct lock_class *target,
1287 struct lock_list **target_entry)
8e18257d 1288{
db0002a3 1289 int result;
8e18257d 1290
bd6d29c2 1291 debug_atomic_inc(nr_cyclic_checks);
419ca3f1 1292
d7aaba14 1293 result = __bfs_forwards(root, target, class_equal, target_entry);
fbb9ce95 1294
db0002a3
ML
1295 return result;
1296}
c94aa5ca 1297
81d68a96 1298#if defined(CONFIG_TRACE_IRQFLAGS) && defined(CONFIG_PROVE_LOCKING)
fbb9ce95
IM
1299/*
1300 * Forwards and backwards subgraph searching, for the purposes of
1301 * proving that two subgraphs can be connected by a new dependency
1302 * without creating any illegal irq-safe -> irq-unsafe lock dependency.
1303 */
fbb9ce95 1304
d7aaba14
ML
1305static inline int usage_match(struct lock_list *entry, void *bit)
1306{
1307 return entry->class->usage_mask & (1 << (enum lock_usage_bit)bit);
1308}
1309
1310
1311
fbb9ce95
IM
1312/*
1313 * Find a node in the forwards-direction dependency sub-graph starting
d7aaba14 1314 * at @root->class that matches @bit.
fbb9ce95 1315 *
d7aaba14
ML
1316 * Return 0 if such a node exists in the subgraph, and put that node
1317 * into *@target_entry.
fbb9ce95 1318 *
d7aaba14
ML
1319 * Return 1 otherwise and keep *@target_entry unchanged.
1320 * Return <0 on error.
fbb9ce95 1321 */
d7aaba14
ML
1322static int
1323find_usage_forwards(struct lock_list *root, enum lock_usage_bit bit,
1324 struct lock_list **target_entry)
fbb9ce95 1325{
d7aaba14 1326 int result;
fbb9ce95 1327
bd6d29c2 1328 debug_atomic_inc(nr_find_usage_forwards_checks);
fbb9ce95 1329
d7aaba14
ML
1330 result = __bfs_forwards(root, (void *)bit, usage_match, target_entry);
1331
1332 return result;
fbb9ce95
IM
1333}
1334
1335/*
1336 * Find a node in the backwards-direction dependency sub-graph starting
d7aaba14 1337 * at @root->class that matches @bit.
fbb9ce95 1338 *
d7aaba14
ML
1339 * Return 0 if such a node exists in the subgraph, and put that node
1340 * into *@target_entry.
fbb9ce95 1341 *
d7aaba14
ML
1342 * Return 1 otherwise and keep *@target_entry unchanged.
1343 * Return <0 on error.
fbb9ce95 1344 */
d7aaba14
ML
1345static int
1346find_usage_backwards(struct lock_list *root, enum lock_usage_bit bit,
1347 struct lock_list **target_entry)
fbb9ce95 1348{
d7aaba14 1349 int result;
fbb9ce95 1350
bd6d29c2 1351 debug_atomic_inc(nr_find_usage_backwards_checks);
fbb9ce95 1352
d7aaba14 1353 result = __bfs_backwards(root, (void *)bit, usage_match, target_entry);
f82b217e 1354
d7aaba14 1355 return result;
fbb9ce95
IM
1356}
1357
af012961
PZ
1358static void print_lock_class_header(struct lock_class *class, int depth)
1359{
1360 int bit;
1361
1362 printk("%*s->", depth, "");
1363 print_lock_name(class);
f943fe0f
DV
1364 printk(KERN_CONT " ops: %lu", class->ops);
1365 printk(KERN_CONT " {\n");
af012961
PZ
1366
1367 for (bit = 0; bit < LOCK_USAGE_STATES; bit++) {
1368 if (class->usage_mask & (1 << bit)) {
1369 int len = depth;
1370
1371 len += printk("%*s %s", depth, "", usage_str[bit]);
f943fe0f 1372 len += printk(KERN_CONT " at:\n");
af012961
PZ
1373 print_stack_trace(class->usage_traces + bit, len);
1374 }
1375 }
1376 printk("%*s }\n", depth, "");
1377
f943fe0f
DV
1378 printk("%*s ... key at: [<%p>] %pS\n",
1379 depth, "", class->key, class->key);
af012961
PZ
1380}
1381
1382/*
1383 * printk the shortest lock dependencies from @start to @end in reverse order:
1384 */
1385static void __used
1386print_shortest_lock_dependencies(struct lock_list *leaf,
1387 struct lock_list *root)
1388{
1389 struct lock_list *entry = leaf;
1390 int depth;
1391
1392 /*compute depth from generated tree by BFS*/
1393 depth = get_lock_depth(leaf);
1394
1395 do {
1396 print_lock_class_header(entry->class, depth);
1397 printk("%*s ... acquired at:\n", depth, "");
1398 print_stack_trace(&entry->trace, 2);
1399 printk("\n");
1400
1401 if (depth == 0 && (entry != root)) {
6be8c393 1402 printk("lockdep:%s bad path found in chain graph\n", __func__);
af012961
PZ
1403 break;
1404 }
1405
1406 entry = get_lock_parent(entry);
1407 depth--;
1408 } while (entry && (depth >= 0));
1409
1410 return;
1411}
d7aaba14 1412
3003eba3
SR
1413static void
1414print_irq_lock_scenario(struct lock_list *safe_entry,
1415 struct lock_list *unsafe_entry,
dad3d743
SR
1416 struct lock_class *prev_class,
1417 struct lock_class *next_class)
3003eba3
SR
1418{
1419 struct lock_class *safe_class = safe_entry->class;
1420 struct lock_class *unsafe_class = unsafe_entry->class;
dad3d743 1421 struct lock_class *middle_class = prev_class;
3003eba3
SR
1422
1423 if (middle_class == safe_class)
dad3d743 1424 middle_class = next_class;
3003eba3
SR
1425
1426 /*
1427 * A direct locking problem where unsafe_class lock is taken
1428 * directly by safe_class lock, then all we need to show
1429 * is the deadlock scenario, as it is obvious that the
1430 * unsafe lock is taken under the safe lock.
1431 *
1432 * But if there is a chain instead, where the safe lock takes
1433 * an intermediate lock (middle_class) where this lock is
1434 * not the same as the safe lock, then the lock chain is
1435 * used to describe the problem. Otherwise we would need
1436 * to show a different CPU case for each link in the chain
1437 * from the safe_class lock to the unsafe_class lock.
1438 */
1439 if (middle_class != unsafe_class) {
1440 printk("Chain exists of:\n ");
1441 __print_lock_name(safe_class);
f943fe0f 1442 printk(KERN_CONT " --> ");
3003eba3 1443 __print_lock_name(middle_class);
f943fe0f 1444 printk(KERN_CONT " --> ");
3003eba3 1445 __print_lock_name(unsafe_class);
f943fe0f 1446 printk(KERN_CONT "\n\n");
3003eba3
SR
1447 }
1448
1449 printk(" Possible interrupt unsafe locking scenario:\n\n");
1450 printk(" CPU0 CPU1\n");
1451 printk(" ---- ----\n");
1452 printk(" lock(");
1453 __print_lock_name(unsafe_class);
f943fe0f 1454 printk(KERN_CONT ");\n");
3003eba3
SR
1455 printk(" local_irq_disable();\n");
1456 printk(" lock(");
1457 __print_lock_name(safe_class);
f943fe0f 1458 printk(KERN_CONT ");\n");
3003eba3
SR
1459 printk(" lock(");
1460 __print_lock_name(middle_class);
f943fe0f 1461 printk(KERN_CONT ");\n");
3003eba3
SR
1462 printk(" <Interrupt>\n");
1463 printk(" lock(");
1464 __print_lock_name(safe_class);
f943fe0f 1465 printk(KERN_CONT ");\n");
3003eba3
SR
1466 printk("\n *** DEADLOCK ***\n\n");
1467}
1468
fbb9ce95
IM
1469static int
1470print_bad_irq_dependency(struct task_struct *curr,
24208ca7
ML
1471 struct lock_list *prev_root,
1472 struct lock_list *next_root,
1473 struct lock_list *backwards_entry,
1474 struct lock_list *forwards_entry,
fbb9ce95
IM
1475 struct held_lock *prev,
1476 struct held_lock *next,
1477 enum lock_usage_bit bit1,
1478 enum lock_usage_bit bit2,
1479 const char *irqclass)
1480{
74c383f1 1481 if (!debug_locks_off_graph_unlock() || debug_locks_silent)
fbb9ce95
IM
1482 return 0;
1483
b3fbab05
PM
1484 printk("\n");
1485 printk("======================================================\n");
1486 printk("[ INFO: %s-safe -> %s-unsafe lock order detected ]\n",
fbb9ce95 1487 irqclass, irqclass);
fbdc4b9a 1488 print_kernel_ident();
b3fbab05 1489 printk("------------------------------------------------------\n");
fbb9ce95 1490 printk("%s/%d [HC%u[%lu]:SC%u[%lu]:HE%u:SE%u] is trying to acquire:\n",
ba25f9dc 1491 curr->comm, task_pid_nr(curr),
fbb9ce95
IM
1492 curr->hardirq_context, hardirq_count() >> HARDIRQ_SHIFT,
1493 curr->softirq_context, softirq_count() >> SOFTIRQ_SHIFT,
1494 curr->hardirqs_enabled,
1495 curr->softirqs_enabled);
1496 print_lock(next);
1497
1498 printk("\nand this task is already holding:\n");
1499 print_lock(prev);
1500 printk("which would create a new lock dependency:\n");
f82b217e 1501 print_lock_name(hlock_class(prev));
f943fe0f 1502 printk(KERN_CONT " ->");
f82b217e 1503 print_lock_name(hlock_class(next));
f943fe0f 1504 printk(KERN_CONT "\n");
fbb9ce95
IM
1505
1506 printk("\nbut this new dependency connects a %s-irq-safe lock:\n",
1507 irqclass);
24208ca7 1508 print_lock_name(backwards_entry->class);
fbb9ce95
IM
1509 printk("\n... which became %s-irq-safe at:\n", irqclass);
1510
24208ca7 1511 print_stack_trace(backwards_entry->class->usage_traces + bit1, 1);
fbb9ce95
IM
1512
1513 printk("\nto a %s-irq-unsafe lock:\n", irqclass);
24208ca7 1514 print_lock_name(forwards_entry->class);
fbb9ce95
IM
1515 printk("\n... which became %s-irq-unsafe at:\n", irqclass);
1516 printk("...");
1517
24208ca7 1518 print_stack_trace(forwards_entry->class->usage_traces + bit2, 1);
fbb9ce95
IM
1519
1520 printk("\nother info that might help us debug this:\n\n");
dad3d743
SR
1521 print_irq_lock_scenario(backwards_entry, forwards_entry,
1522 hlock_class(prev), hlock_class(next));
3003eba3 1523
fbb9ce95
IM
1524 lockdep_print_held_locks(curr);
1525
f943fe0f 1526 printk("\nthe dependencies between %s-irq-safe lock and the holding lock:\n", irqclass);
24208ca7
ML
1527 if (!save_trace(&prev_root->trace))
1528 return 0;
1529 print_shortest_lock_dependencies(backwards_entry, prev_root);
fbb9ce95 1530
24208ca7
ML
1531 printk("\nthe dependencies between the lock to be acquired");
1532 printk(" and %s-irq-unsafe lock:\n", irqclass);
1533 if (!save_trace(&next_root->trace))
1534 return 0;
1535 print_shortest_lock_dependencies(forwards_entry, next_root);
fbb9ce95
IM
1536
1537 printk("\nstack backtrace:\n");
1538 dump_stack();
1539
1540 return 0;
1541}
1542
1543static int
1544check_usage(struct task_struct *curr, struct held_lock *prev,
1545 struct held_lock *next, enum lock_usage_bit bit_backwards,
1546 enum lock_usage_bit bit_forwards, const char *irqclass)
1547{
1548 int ret;
24208ca7 1549 struct lock_list this, that;
d7aaba14 1550 struct lock_list *uninitialized_var(target_entry);
24208ca7 1551 struct lock_list *uninitialized_var(target_entry1);
d7aaba14
ML
1552
1553 this.parent = NULL;
1554
1555 this.class = hlock_class(prev);
1556 ret = find_usage_backwards(&this, bit_backwards, &target_entry);
af012961
PZ
1557 if (ret < 0)
1558 return print_bfs_bug(ret);
1559 if (ret == 1)
1560 return ret;
d7aaba14 1561
24208ca7
ML
1562 that.parent = NULL;
1563 that.class = hlock_class(next);
1564 ret = find_usage_forwards(&that, bit_forwards, &target_entry1);
af012961
PZ
1565 if (ret < 0)
1566 return print_bfs_bug(ret);
1567 if (ret == 1)
1568 return ret;
fbb9ce95 1569
24208ca7
ML
1570 return print_bad_irq_dependency(curr, &this, &that,
1571 target_entry, target_entry1,
1572 prev, next,
fbb9ce95
IM
1573 bit_backwards, bit_forwards, irqclass);
1574}
1575
4f367d8a
PZ
1576static const char *state_names[] = {
1577#define LOCKDEP_STATE(__STATE) \
b4b136f4 1578 __stringify(__STATE),
4f367d8a
PZ
1579#include "lockdep_states.h"
1580#undef LOCKDEP_STATE
1581};
1582
1583static const char *state_rnames[] = {
1584#define LOCKDEP_STATE(__STATE) \
b4b136f4 1585 __stringify(__STATE)"-READ",
4f367d8a
PZ
1586#include "lockdep_states.h"
1587#undef LOCKDEP_STATE
1588};
1589
1590static inline const char *state_name(enum lock_usage_bit bit)
8e18257d 1591{
4f367d8a
PZ
1592 return (bit & 1) ? state_rnames[bit >> 2] : state_names[bit >> 2];
1593}
8e18257d 1594
4f367d8a
PZ
1595static int exclusive_bit(int new_bit)
1596{
8e18257d 1597 /*
4f367d8a
PZ
1598 * USED_IN
1599 * USED_IN_READ
1600 * ENABLED
1601 * ENABLED_READ
1602 *
1603 * bit 0 - write/read
1604 * bit 1 - used_in/enabled
1605 * bit 2+ state
8e18257d 1606 */
4f367d8a
PZ
1607
1608 int state = new_bit & ~3;
1609 int dir = new_bit & 2;
8e18257d
PZ
1610
1611 /*
4f367d8a 1612 * keep state, bit flip the direction and strip read.
8e18257d 1613 */
4f367d8a
PZ
1614 return state | (dir ^ 2);
1615}
1616
1617static int check_irq_usage(struct task_struct *curr, struct held_lock *prev,
1618 struct held_lock *next, enum lock_usage_bit bit)
1619{
8e18257d 1620 /*
4f367d8a
PZ
1621 * Prove that the new dependency does not connect a hardirq-safe
1622 * lock with a hardirq-unsafe lock - to achieve this we search
8e18257d
PZ
1623 * the backwards-subgraph starting at <prev>, and the
1624 * forwards-subgraph starting at <next>:
1625 */
4f367d8a
PZ
1626 if (!check_usage(curr, prev, next, bit,
1627 exclusive_bit(bit), state_name(bit)))
8e18257d
PZ
1628 return 0;
1629
4f367d8a
PZ
1630 bit++; /* _READ */
1631
cf40bd16 1632 /*
4f367d8a
PZ
1633 * Prove that the new dependency does not connect a hardirq-safe-read
1634 * lock with a hardirq-unsafe lock - to achieve this we search
cf40bd16
NP
1635 * the backwards-subgraph starting at <prev>, and the
1636 * forwards-subgraph starting at <next>:
1637 */
4f367d8a
PZ
1638 if (!check_usage(curr, prev, next, bit,
1639 exclusive_bit(bit), state_name(bit)))
cf40bd16
NP
1640 return 0;
1641
4f367d8a
PZ
1642 return 1;
1643}
1644
1645static int
1646check_prev_add_irq(struct task_struct *curr, struct held_lock *prev,
1647 struct held_lock *next)
1648{
1649#define LOCKDEP_STATE(__STATE) \
1650 if (!check_irq_usage(curr, prev, next, LOCK_USED_IN_##__STATE)) \
cf40bd16 1651 return 0;
4f367d8a
PZ
1652#include "lockdep_states.h"
1653#undef LOCKDEP_STATE
cf40bd16 1654
8e18257d
PZ
1655 return 1;
1656}
1657
1658static void inc_chains(void)
1659{
1660 if (current->hardirq_context)
1661 nr_hardirq_chains++;
1662 else {
1663 if (current->softirq_context)
1664 nr_softirq_chains++;
1665 else
1666 nr_process_chains++;
1667 }
1668}
1669
1670#else
1671
1672static inline int
1673check_prev_add_irq(struct task_struct *curr, struct held_lock *prev,
1674 struct held_lock *next)
1675{
1676 return 1;
1677}
1678
1679static inline void inc_chains(void)
1680{
1681 nr_process_chains++;
1682}
1683
fbb9ce95
IM
1684#endif
1685
48702ecf
SR
1686static void
1687print_deadlock_scenario(struct held_lock *nxt,
1688 struct held_lock *prv)
1689{
1690 struct lock_class *next = hlock_class(nxt);
1691 struct lock_class *prev = hlock_class(prv);
1692
1693 printk(" Possible unsafe locking scenario:\n\n");
1694 printk(" CPU0\n");
1695 printk(" ----\n");
1696 printk(" lock(");
1697 __print_lock_name(prev);
f943fe0f 1698 printk(KERN_CONT ");\n");
48702ecf
SR
1699 printk(" lock(");
1700 __print_lock_name(next);
f943fe0f 1701 printk(KERN_CONT ");\n");
48702ecf
SR
1702 printk("\n *** DEADLOCK ***\n\n");
1703 printk(" May be due to missing lock nesting notation\n\n");
1704}
1705
fbb9ce95
IM
1706static int
1707print_deadlock_bug(struct task_struct *curr, struct held_lock *prev,
1708 struct held_lock *next)
1709{
74c383f1 1710 if (!debug_locks_off_graph_unlock() || debug_locks_silent)
fbb9ce95
IM
1711 return 0;
1712
b3fbab05
PM
1713 printk("\n");
1714 printk("=============================================\n");
1715 printk("[ INFO: possible recursive locking detected ]\n");
fbdc4b9a 1716 print_kernel_ident();
b3fbab05 1717 printk("---------------------------------------------\n");
fbb9ce95 1718 printk("%s/%d is trying to acquire lock:\n",
ba25f9dc 1719 curr->comm, task_pid_nr(curr));
fbb9ce95
IM
1720 print_lock(next);
1721 printk("\nbut task is already holding lock:\n");
1722 print_lock(prev);
1723
1724 printk("\nother info that might help us debug this:\n");
48702ecf 1725 print_deadlock_scenario(next, prev);
fbb9ce95
IM
1726 lockdep_print_held_locks(curr);
1727
1728 printk("\nstack backtrace:\n");
1729 dump_stack();
1730
1731 return 0;
1732}
1733
1734/*
1735 * Check whether we are holding such a class already.
1736 *
1737 * (Note that this has to be done separately, because the graph cannot
1738 * detect such classes of deadlocks.)
1739 *
1740 * Returns: 0 on deadlock detected, 1 on OK, 2 on recursive read
1741 */
1742static int
1743check_deadlock(struct task_struct *curr, struct held_lock *next,
1744 struct lockdep_map *next_instance, int read)
1745{
1746 struct held_lock *prev;
7531e2f3 1747 struct held_lock *nest = NULL;
fbb9ce95
IM
1748 int i;
1749
1750 for (i = 0; i < curr->lockdep_depth; i++) {
1751 prev = curr->held_locks + i;
7531e2f3
PZ
1752
1753 if (prev->instance == next->nest_lock)
1754 nest = prev;
1755
f82b217e 1756 if (hlock_class(prev) != hlock_class(next))
fbb9ce95 1757 continue;
7531e2f3 1758
fbb9ce95
IM
1759 /*
1760 * Allow read-after-read recursion of the same
6c9076ec 1761 * lock class (i.e. read_lock(lock)+read_lock(lock)):
fbb9ce95 1762 */
6c9076ec 1763 if ((read == 2) && prev->read)
fbb9ce95 1764 return 2;
7531e2f3
PZ
1765
1766 /*
1767 * We're holding the nest_lock, which serializes this lock's
1768 * nesting behaviour.
1769 */
1770 if (nest)
1771 return 2;
1772
fbb9ce95
IM
1773 return print_deadlock_bug(curr, prev, next);
1774 }
1775 return 1;
1776}
1777
1778/*
1779 * There was a chain-cache miss, and we are about to add a new dependency
1780 * to a previous lock. We recursively validate the following rules:
1781 *
1782 * - would the adding of the <prev> -> <next> dependency create a
1783 * circular dependency in the graph? [== circular deadlock]
1784 *
1785 * - does the new prev->next dependency connect any hardirq-safe lock
1786 * (in the full backwards-subgraph starting at <prev>) with any
1787 * hardirq-unsafe lock (in the full forwards-subgraph starting at
1788 * <next>)? [== illegal lock inversion with hardirq contexts]
1789 *
1790 * - does the new prev->next dependency connect any softirq-safe lock
1791 * (in the full backwards-subgraph starting at <prev>) with any
1792 * softirq-unsafe lock (in the full forwards-subgraph starting at
1793 * <next>)? [== illegal lock inversion with softirq contexts]
1794 *
1795 * any of these scenarios could lead to a deadlock.
1796 *
1797 * Then if all the validations pass, we add the forwards and backwards
1798 * dependency.
1799 */
1800static int
1801check_prev_add(struct task_struct *curr, struct held_lock *prev,
8a5fd564 1802 struct held_lock *next, int distance, int *stack_saved)
fbb9ce95
IM
1803{
1804 struct lock_list *entry;
1805 int ret;
db0002a3
ML
1806 struct lock_list this;
1807 struct lock_list *uninitialized_var(target_entry);
4726f2a6
YZ
1808 /*
1809 * Static variable, serialized by the graph_lock().
1810 *
1811 * We use this static variable to save the stack trace in case
1812 * we call into this function multiple times due to encountering
1813 * trylocks in the held lock stack.
1814 */
1815 static struct stack_trace trace;
fbb9ce95
IM
1816
1817 /*
1818 * Prove that the new <prev> -> <next> dependency would not
1819 * create a circular dependency in the graph. (We do this by
1820 * forward-recursing into the graph starting at <next>, and
1821 * checking whether we can reach <prev>.)
1822 *
1823 * We are using global variables to control the recursion, to
1824 * keep the stackframe size of the recursive functions low:
1825 */
db0002a3
ML
1826 this.class = hlock_class(next);
1827 this.parent = NULL;
1828 ret = check_noncircular(&this, hlock_class(prev), &target_entry);
1829 if (unlikely(!ret))
1830 return print_circular_bug(&this, target_entry, next, prev);
1831 else if (unlikely(ret < 0))
1832 return print_bfs_bug(ret);
c94aa5ca 1833
8e18257d 1834 if (!check_prev_add_irq(curr, prev, next))
fbb9ce95
IM
1835 return 0;
1836
fbb9ce95
IM
1837 /*
1838 * For recursive read-locks we do all the dependency checks,
1839 * but we dont store read-triggered dependencies (only
1840 * write-triggered dependencies). This ensures that only the
1841 * write-side dependencies matter, and that if for example a
1842 * write-lock never takes any other locks, then the reads are
1843 * equivalent to a NOP.
1844 */
1845 if (next->read == 2 || prev->read == 2)
1846 return 1;
1847 /*
1848 * Is the <prev> -> <next> dependency already present?
1849 *
1850 * (this may occur even though this is a new chain: consider
1851 * e.g. the L1 -> L2 -> L3 -> L4 and the L5 -> L1 -> L2 -> L3
1852 * chains - the second one will be new, but L1 already has
1853 * L2 added to its dependency list, due to the first chain.)
1854 */
f82b217e
DJ
1855 list_for_each_entry(entry, &hlock_class(prev)->locks_after, entry) {
1856 if (entry->class == hlock_class(next)) {
068135e6
JB
1857 if (distance == 1)
1858 entry->distance = 1;
fbb9ce95 1859 return 2;
068135e6 1860 }
fbb9ce95
IM
1861 }
1862
8a5fd564
DV
1863 if (!*stack_saved) {
1864 if (!save_trace(&trace))
1865 return 0;
1866 *stack_saved = 1;
1867 }
4726f2a6 1868
fbb9ce95
IM
1869 /*
1870 * Ok, all validations passed, add the new lock
1871 * to the previous lock's dependency list:
1872 */
83f06168 1873 ret = add_lock_to_list(hlock_class(next),
f82b217e 1874 &hlock_class(prev)->locks_after,
4726f2a6 1875 next->acquire_ip, distance, &trace);
068135e6 1876
fbb9ce95
IM
1877 if (!ret)
1878 return 0;
910b1b2e 1879
83f06168 1880 ret = add_lock_to_list(hlock_class(prev),
f82b217e 1881 &hlock_class(next)->locks_before,
4726f2a6 1882 next->acquire_ip, distance, &trace);
910b1b2e
JP
1883 if (!ret)
1884 return 0;
fbb9ce95
IM
1885
1886 /*
8e18257d
PZ
1887 * Debugging printouts:
1888 */
f82b217e 1889 if (verbose(hlock_class(prev)) || verbose(hlock_class(next))) {
8a5fd564
DV
1890 /* We drop graph lock, so another thread can overwrite trace. */
1891 *stack_saved = 0;
8e18257d
PZ
1892 graph_unlock();
1893 printk("\n new dependency: ");
f82b217e 1894 print_lock_name(hlock_class(prev));
f943fe0f 1895 printk(KERN_CONT " => ");
f82b217e 1896 print_lock_name(hlock_class(next));
f943fe0f 1897 printk(KERN_CONT "\n");
fbb9ce95 1898 dump_stack();
8e18257d 1899 return graph_lock();
fbb9ce95 1900 }
8e18257d
PZ
1901 return 1;
1902}
fbb9ce95 1903
8e18257d
PZ
1904/*
1905 * Add the dependency to all directly-previous locks that are 'relevant'.
1906 * The ones that are relevant are (in increasing distance from curr):
1907 * all consecutive trylock entries and the final non-trylock entry - or
1908 * the end of this context's lock-chain - whichever comes first.
1909 */
1910static int
1911check_prevs_add(struct task_struct *curr, struct held_lock *next)
1912{
1913 int depth = curr->lockdep_depth;
8a5fd564 1914 int stack_saved = 0;
8e18257d 1915 struct held_lock *hlock;
d6d897ce 1916
fbb9ce95 1917 /*
8e18257d
PZ
1918 * Debugging checks.
1919 *
1920 * Depth must not be zero for a non-head lock:
fbb9ce95 1921 */
8e18257d
PZ
1922 if (!depth)
1923 goto out_bug;
fbb9ce95 1924 /*
8e18257d
PZ
1925 * At least two relevant locks must exist for this
1926 * to be a head:
fbb9ce95 1927 */
8e18257d
PZ
1928 if (curr->held_locks[depth].irq_context !=
1929 curr->held_locks[depth-1].irq_context)
1930 goto out_bug;
74c383f1 1931
8e18257d
PZ
1932 for (;;) {
1933 int distance = curr->lockdep_depth - depth + 1;
1b5ff816 1934 hlock = curr->held_locks + depth - 1;
8e18257d
PZ
1935 /*
1936 * Only non-recursive-read entries get new dependencies
1937 * added:
1938 */
1b5ff816 1939 if (hlock->read != 2 && hlock->check) {
4726f2a6 1940 if (!check_prev_add(curr, hlock, next,
8a5fd564 1941 distance, &stack_saved))
8e18257d
PZ
1942 return 0;
1943 /*
1944 * Stop after the first non-trylock entry,
1945 * as non-trylock entries have added their
1946 * own direct dependencies already, so this
1947 * lock is connected to them indirectly:
1948 */
1949 if (!hlock->trylock)
1950 break;
74c383f1 1951 }
8e18257d
PZ
1952 depth--;
1953 /*
1954 * End of lock-stack?
1955 */
1956 if (!depth)
1957 break;
1958 /*
1959 * Stop the search if we cross into another context:
1960 */
1961 if (curr->held_locks[depth].irq_context !=
1962 curr->held_locks[depth-1].irq_context)
1963 break;
fbb9ce95 1964 }
8e18257d
PZ
1965 return 1;
1966out_bug:
1967 if (!debug_locks_off_graph_unlock())
1968 return 0;
fbb9ce95 1969
0119fee4
PZ
1970 /*
1971 * Clearly we all shouldn't be here, but since we made it we
1972 * can reliable say we messed up our state. See the above two
1973 * gotos for reasons why we could possibly end up here.
1974 */
8e18257d 1975 WARN_ON(1);
fbb9ce95 1976
8e18257d 1977 return 0;
fbb9ce95
IM
1978}
1979
8e18257d 1980unsigned long nr_lock_chains;
443cd507 1981struct lock_chain lock_chains[MAX_LOCKDEP_CHAINS];
cd1a28e8 1982int nr_chain_hlocks;
443cd507
HY
1983static u16 chain_hlocks[MAX_LOCKDEP_CHAIN_HLOCKS];
1984
1985struct lock_class *lock_chain_get_class(struct lock_chain *chain, int i)
1986{
1987 return lock_classes + chain_hlocks[chain->base + i];
1988}
8e18257d 1989
9e4e7554
IM
1990/*
1991 * Returns the index of the first held_lock of the current chain
1992 */
1993static inline int get_first_held_lock(struct task_struct *curr,
1994 struct held_lock *hlock)
1995{
1996 int i;
1997 struct held_lock *hlock_curr;
1998
1999 for (i = curr->lockdep_depth - 1; i >= 0; i--) {
2000 hlock_curr = curr->held_locks + i;
2001 if (hlock_curr->irq_context != hlock->irq_context)
2002 break;
2003
2004 }
2005
2006 return ++i;
2007}
2008
5c8a010c 2009#ifdef CONFIG_DEBUG_LOCKDEP
39e2e173
AAF
2010/*
2011 * Returns the next chain_key iteration
2012 */
2013static u64 print_chain_key_iteration(int class_idx, u64 chain_key)
2014{
2015 u64 new_chain_key = iterate_chain_key(chain_key, class_idx);
2016
2017 printk(" class_idx:%d -> chain_key:%016Lx",
2018 class_idx,
2019 (unsigned long long)new_chain_key);
2020 return new_chain_key;
2021}
2022
2023static void
2024print_chain_keys_held_locks(struct task_struct *curr, struct held_lock *hlock_next)
2025{
2026 struct held_lock *hlock;
2027 u64 chain_key = 0;
2028 int depth = curr->lockdep_depth;
2029 int i;
2030
2031 printk("depth: %u\n", depth + 1);
2032 for (i = get_first_held_lock(curr, hlock_next); i < depth; i++) {
2033 hlock = curr->held_locks + i;
2034 chain_key = print_chain_key_iteration(hlock->class_idx, chain_key);
2035
2036 print_lock(hlock);
2037 }
2038
2039 print_chain_key_iteration(hlock_next->class_idx, chain_key);
2040 print_lock(hlock_next);
2041}
2042
2043static void print_chain_keys_chain(struct lock_chain *chain)
2044{
2045 int i;
2046 u64 chain_key = 0;
2047 int class_id;
2048
2049 printk("depth: %u\n", chain->depth);
2050 for (i = 0; i < chain->depth; i++) {
2051 class_id = chain_hlocks[chain->base + i];
2052 chain_key = print_chain_key_iteration(class_id + 1, chain_key);
2053
2054 print_lock_name(lock_classes + class_id);
2055 printk("\n");
2056 }
2057}
2058
2059static void print_collision(struct task_struct *curr,
2060 struct held_lock *hlock_next,
2061 struct lock_chain *chain)
2062{
2063 printk("\n");
2064 printk("======================\n");
2065 printk("[chain_key collision ]\n");
2066 print_kernel_ident();
2067 printk("----------------------\n");
2068 printk("%s/%d: ", current->comm, task_pid_nr(current));
2069 printk("Hash chain already cached but the contents don't match!\n");
2070
2071 printk("Held locks:");
2072 print_chain_keys_held_locks(curr, hlock_next);
2073
2074 printk("Locks in cached chain:");
2075 print_chain_keys_chain(chain);
2076
2077 printk("\nstack backtrace:\n");
2078 dump_stack();
2079}
5c8a010c 2080#endif
39e2e173 2081
9e4e7554
IM
2082/*
2083 * Checks whether the chain and the current held locks are consistent
2084 * in depth and also in content. If they are not it most likely means
2085 * that there was a collision during the calculation of the chain_key.
2086 * Returns: 0 not passed, 1 passed
2087 */
2088static int check_no_collision(struct task_struct *curr,
2089 struct held_lock *hlock,
2090 struct lock_chain *chain)
2091{
2092#ifdef CONFIG_DEBUG_LOCKDEP
2093 int i, j, id;
2094
2095 i = get_first_held_lock(curr, hlock);
2096
39e2e173
AAF
2097 if (DEBUG_LOCKS_WARN_ON(chain->depth != curr->lockdep_depth - (i - 1))) {
2098 print_collision(curr, hlock, chain);
9e4e7554 2099 return 0;
39e2e173 2100 }
9e4e7554
IM
2101
2102 for (j = 0; j < chain->depth - 1; j++, i++) {
2103 id = curr->held_locks[i].class_idx - 1;
2104
39e2e173
AAF
2105 if (DEBUG_LOCKS_WARN_ON(chain_hlocks[chain->base + j] != id)) {
2106 print_collision(curr, hlock, chain);
9e4e7554 2107 return 0;
39e2e173 2108 }
9e4e7554
IM
2109 }
2110#endif
2111 return 1;
2112}
2113
fbb9ce95
IM
2114/*
2115 * Look up a dependency chain. If the key is not present yet then
9e860d00
JP
2116 * add it and return 1 - in this case the new dependency chain is
2117 * validated. If the key is already hashed, return 0.
2118 * (On return with 1 graph_lock is held.)
fbb9ce95 2119 */
443cd507
HY
2120static inline int lookup_chain_cache(struct task_struct *curr,
2121 struct held_lock *hlock,
2122 u64 chain_key)
fbb9ce95 2123{
f82b217e 2124 struct lock_class *class = hlock_class(hlock);
a63f38cc 2125 struct hlist_head *hash_head = chainhashentry(chain_key);
fbb9ce95 2126 struct lock_chain *chain;
e0944ee6 2127 int i, j;
fbb9ce95 2128
0119fee4
PZ
2129 /*
2130 * We might need to take the graph lock, ensure we've got IRQs
2131 * disabled to make this an IRQ-safe lock.. for recursion reasons
2132 * lockdep won't complain about its own locking errors.
2133 */
381a2292
JP
2134 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2135 return 0;
fbb9ce95
IM
2136 /*
2137 * We can walk it lock-free, because entries only get added
2138 * to the hash:
2139 */
a63f38cc 2140 hlist_for_each_entry_rcu(chain, hash_head, entry) {
fbb9ce95
IM
2141 if (chain->chain_key == chain_key) {
2142cache_hit:
bd6d29c2 2143 debug_atomic_inc(chain_lookup_hits);
9e4e7554
IM
2144 if (!check_no_collision(curr, hlock, chain))
2145 return 0;
2146
81fc685a 2147 if (very_verbose(class))
755cd900
AM
2148 printk("\nhash chain already cached, key: "
2149 "%016Lx tail class: [%p] %s\n",
2150 (unsigned long long)chain_key,
2151 class->key, class->name);
fbb9ce95
IM
2152 return 0;
2153 }
2154 }
81fc685a 2155 if (very_verbose(class))
755cd900
AM
2156 printk("\nnew hash chain, key: %016Lx tail class: [%p] %s\n",
2157 (unsigned long long)chain_key, class->key, class->name);
fbb9ce95
IM
2158 /*
2159 * Allocate a new chain entry from the static array, and add
2160 * it to the hash:
2161 */
74c383f1
IM
2162 if (!graph_lock())
2163 return 0;
fbb9ce95
IM
2164 /*
2165 * We have to walk the chain again locked - to avoid duplicates:
2166 */
a63f38cc 2167 hlist_for_each_entry(chain, hash_head, entry) {
fbb9ce95 2168 if (chain->chain_key == chain_key) {
74c383f1 2169 graph_unlock();
fbb9ce95
IM
2170 goto cache_hit;
2171 }
2172 }
2173 if (unlikely(nr_lock_chains >= MAX_LOCKDEP_CHAINS)) {
74c383f1
IM
2174 if (!debug_locks_off_graph_unlock())
2175 return 0;
2176
2c522836 2177 print_lockdep_off("BUG: MAX_LOCKDEP_CHAINS too low!");
eedeeabd 2178 dump_stack();
fbb9ce95
IM
2179 return 0;
2180 }
2181 chain = lock_chains + nr_lock_chains++;
2182 chain->chain_key = chain_key;
443cd507 2183 chain->irq_context = hlock->irq_context;
9e4e7554 2184 i = get_first_held_lock(curr, hlock);
443cd507 2185 chain->depth = curr->lockdep_depth + 1 - i;
75dd602a
PZ
2186
2187 BUILD_BUG_ON((1UL << 24) <= ARRAY_SIZE(chain_hlocks));
2188 BUILD_BUG_ON((1UL << 6) <= ARRAY_SIZE(curr->held_locks));
2189 BUILD_BUG_ON((1UL << 8*sizeof(chain_hlocks[0])) <= ARRAY_SIZE(lock_classes));
2190
e0944ee6
SR
2191 if (likely(nr_chain_hlocks + chain->depth <= MAX_LOCKDEP_CHAIN_HLOCKS)) {
2192 chain->base = nr_chain_hlocks;
443cd507 2193 for (j = 0; j < chain->depth - 1; j++, i++) {
f82b217e 2194 int lock_id = curr->held_locks[i].class_idx - 1;
443cd507
HY
2195 chain_hlocks[chain->base + j] = lock_id;
2196 }
2197 chain_hlocks[chain->base + j] = class - lock_classes;
2198 }
75dd602a
PZ
2199
2200 if (nr_chain_hlocks < MAX_LOCKDEP_CHAIN_HLOCKS)
2201 nr_chain_hlocks += chain->depth;
2202
2203#ifdef CONFIG_DEBUG_LOCKDEP
2204 /*
2205 * Important for check_no_collision().
2206 */
2207 if (unlikely(nr_chain_hlocks > MAX_LOCKDEP_CHAIN_HLOCKS)) {
f9af456a 2208 if (!debug_locks_off_graph_unlock())
75dd602a
PZ
2209 return 0;
2210
2211 print_lockdep_off("BUG: MAX_LOCKDEP_CHAIN_HLOCKS too low!");
2212 dump_stack();
2213 return 0;
2214 }
2215#endif
2216
a63f38cc 2217 hlist_add_head_rcu(&chain->entry, hash_head);
bd6d29c2 2218 debug_atomic_inc(chain_lookup_misses);
8e18257d
PZ
2219 inc_chains();
2220
2221 return 1;
2222}
2223
2224static int validate_chain(struct task_struct *curr, struct lockdep_map *lock,
4e6045f1 2225 struct held_lock *hlock, int chain_head, u64 chain_key)
8e18257d
PZ
2226{
2227 /*
2228 * Trylock needs to maintain the stack of held locks, but it
2229 * does not add new dependencies, because trylock can be done
2230 * in any order.
2231 *
2232 * We look up the chain_key and do the O(N^2) check and update of
2233 * the dependencies only if this is a new dependency chain.
2234 * (If lookup_chain_cache() returns with 1 it acquires
2235 * graph_lock for us)
2236 */
fb9edbe9 2237 if (!hlock->trylock && hlock->check &&
443cd507 2238 lookup_chain_cache(curr, hlock, chain_key)) {
8e18257d
PZ
2239 /*
2240 * Check whether last held lock:
2241 *
2242 * - is irq-safe, if this lock is irq-unsafe
2243 * - is softirq-safe, if this lock is hardirq-unsafe
2244 *
2245 * And check whether the new lock's dependency graph
2246 * could lead back to the previous lock.
2247 *
2248 * any of these scenarios could lead to a deadlock. If
2249 * All validations
2250 */
2251 int ret = check_deadlock(curr, hlock, lock, hlock->read);
2252
2253 if (!ret)
2254 return 0;
2255 /*
2256 * Mark recursive read, as we jump over it when
2257 * building dependencies (just like we jump over
2258 * trylock entries):
2259 */
2260 if (ret == 2)
2261 hlock->read = 2;
2262 /*
2263 * Add dependency only if this lock is not the head
2264 * of the chain, and if it's not a secondary read-lock:
2265 */
2266 if (!chain_head && ret != 2)
2267 if (!check_prevs_add(curr, hlock))
2268 return 0;
2269 graph_unlock();
2270 } else
2271 /* after lookup_chain_cache(): */
2272 if (unlikely(!debug_locks))
2273 return 0;
fbb9ce95
IM
2274
2275 return 1;
2276}
8e18257d
PZ
2277#else
2278static inline int validate_chain(struct task_struct *curr,
2279 struct lockdep_map *lock, struct held_lock *hlock,
3aa416b0 2280 int chain_head, u64 chain_key)
8e18257d
PZ
2281{
2282 return 1;
2283}
ca58abcb 2284#endif
fbb9ce95
IM
2285
2286/*
2287 * We are building curr_chain_key incrementally, so double-check
2288 * it from scratch, to make sure that it's done correctly:
2289 */
1d09daa5 2290static void check_chain_key(struct task_struct *curr)
fbb9ce95
IM
2291{
2292#ifdef CONFIG_DEBUG_LOCKDEP
2293 struct held_lock *hlock, *prev_hlock = NULL;
5f18ab5c 2294 unsigned int i;
fbb9ce95
IM
2295 u64 chain_key = 0;
2296
2297 for (i = 0; i < curr->lockdep_depth; i++) {
2298 hlock = curr->held_locks + i;
2299 if (chain_key != hlock->prev_chain_key) {
2300 debug_locks_off();
0119fee4
PZ
2301 /*
2302 * We got mighty confused, our chain keys don't match
2303 * with what we expect, someone trample on our task state?
2304 */
2df8b1d6 2305 WARN(1, "hm#1, depth: %u [%u], %016Lx != %016Lx\n",
fbb9ce95
IM
2306 curr->lockdep_depth, i,
2307 (unsigned long long)chain_key,
2308 (unsigned long long)hlock->prev_chain_key);
fbb9ce95
IM
2309 return;
2310 }
0119fee4
PZ
2311 /*
2312 * Whoops ran out of static storage again?
2313 */
5f18ab5c 2314 if (DEBUG_LOCKS_WARN_ON(hlock->class_idx > MAX_LOCKDEP_KEYS))
381a2292
JP
2315 return;
2316
fbb9ce95
IM
2317 if (prev_hlock && (prev_hlock->irq_context !=
2318 hlock->irq_context))
2319 chain_key = 0;
5f18ab5c 2320 chain_key = iterate_chain_key(chain_key, hlock->class_idx);
fbb9ce95
IM
2321 prev_hlock = hlock;
2322 }
2323 if (chain_key != curr->curr_chain_key) {
2324 debug_locks_off();
0119fee4
PZ
2325 /*
2326 * More smoking hash instead of calculating it, damn see these
2327 * numbers float.. I bet that a pink elephant stepped on my memory.
2328 */
2df8b1d6 2329 WARN(1, "hm#2, depth: %u [%u], %016Lx != %016Lx\n",
fbb9ce95
IM
2330 curr->lockdep_depth, i,
2331 (unsigned long long)chain_key,
2332 (unsigned long long)curr->curr_chain_key);
fbb9ce95
IM
2333 }
2334#endif
2335}
2336
282b5c2f
SR
2337static void
2338print_usage_bug_scenario(struct held_lock *lock)
2339{
2340 struct lock_class *class = hlock_class(lock);
2341
2342 printk(" Possible unsafe locking scenario:\n\n");
2343 printk(" CPU0\n");
2344 printk(" ----\n");
2345 printk(" lock(");
2346 __print_lock_name(class);
f943fe0f 2347 printk(KERN_CONT ");\n");
282b5c2f
SR
2348 printk(" <Interrupt>\n");
2349 printk(" lock(");
2350 __print_lock_name(class);
f943fe0f 2351 printk(KERN_CONT ");\n");
282b5c2f
SR
2352 printk("\n *** DEADLOCK ***\n\n");
2353}
2354
8e18257d
PZ
2355static int
2356print_usage_bug(struct task_struct *curr, struct held_lock *this,
2357 enum lock_usage_bit prev_bit, enum lock_usage_bit new_bit)
2358{
2359 if (!debug_locks_off_graph_unlock() || debug_locks_silent)
2360 return 0;
2361
b3fbab05
PM
2362 printk("\n");
2363 printk("=================================\n");
2364 printk("[ INFO: inconsistent lock state ]\n");
fbdc4b9a 2365 print_kernel_ident();
b3fbab05 2366 printk("---------------------------------\n");
8e18257d
PZ
2367
2368 printk("inconsistent {%s} -> {%s} usage.\n",
2369 usage_str[prev_bit], usage_str[new_bit]);
2370
2371 printk("%s/%d [HC%u[%lu]:SC%u[%lu]:HE%u:SE%u] takes:\n",
ba25f9dc 2372 curr->comm, task_pid_nr(curr),
8e18257d
PZ
2373 trace_hardirq_context(curr), hardirq_count() >> HARDIRQ_SHIFT,
2374 trace_softirq_context(curr), softirq_count() >> SOFTIRQ_SHIFT,
2375 trace_hardirqs_enabled(curr),
2376 trace_softirqs_enabled(curr));
2377 print_lock(this);
2378
2379 printk("{%s} state was registered at:\n", usage_str[prev_bit]);
f82b217e 2380 print_stack_trace(hlock_class(this)->usage_traces + prev_bit, 1);
8e18257d
PZ
2381
2382 print_irqtrace_events(curr);
2383 printk("\nother info that might help us debug this:\n");
282b5c2f
SR
2384 print_usage_bug_scenario(this);
2385
8e18257d
PZ
2386 lockdep_print_held_locks(curr);
2387
2388 printk("\nstack backtrace:\n");
2389 dump_stack();
2390
2391 return 0;
2392}
2393
2394/*
2395 * Print out an error if an invalid bit is set:
2396 */
2397static inline int
2398valid_state(struct task_struct *curr, struct held_lock *this,
2399 enum lock_usage_bit new_bit, enum lock_usage_bit bad_bit)
2400{
f82b217e 2401 if (unlikely(hlock_class(this)->usage_mask & (1 << bad_bit)))
8e18257d
PZ
2402 return print_usage_bug(curr, this, bad_bit, new_bit);
2403 return 1;
2404}
2405
2406static int mark_lock(struct task_struct *curr, struct held_lock *this,
2407 enum lock_usage_bit new_bit);
2408
81d68a96 2409#if defined(CONFIG_TRACE_IRQFLAGS) && defined(CONFIG_PROVE_LOCKING)
fbb9ce95
IM
2410
2411/*
2412 * print irq inversion bug:
2413 */
2414static int
24208ca7
ML
2415print_irq_inversion_bug(struct task_struct *curr,
2416 struct lock_list *root, struct lock_list *other,
fbb9ce95
IM
2417 struct held_lock *this, int forwards,
2418 const char *irqclass)
2419{
dad3d743
SR
2420 struct lock_list *entry = other;
2421 struct lock_list *middle = NULL;
2422 int depth;
2423
74c383f1 2424 if (!debug_locks_off_graph_unlock() || debug_locks_silent)
fbb9ce95
IM
2425 return 0;
2426
b3fbab05
PM
2427 printk("\n");
2428 printk("=========================================================\n");
2429 printk("[ INFO: possible irq lock inversion dependency detected ]\n");
fbdc4b9a 2430 print_kernel_ident();
b3fbab05 2431 printk("---------------------------------------------------------\n");
fbb9ce95 2432 printk("%s/%d just changed the state of lock:\n",
ba25f9dc 2433 curr->comm, task_pid_nr(curr));
fbb9ce95
IM
2434 print_lock(this);
2435 if (forwards)
26575e28 2436 printk("but this lock took another, %s-unsafe lock in the past:\n", irqclass);
fbb9ce95 2437 else
26575e28 2438 printk("but this lock was taken by another, %s-safe lock in the past:\n", irqclass);
24208ca7 2439 print_lock_name(other->class);
fbb9ce95
IM
2440 printk("\n\nand interrupts could create inverse lock ordering between them.\n\n");
2441
2442 printk("\nother info that might help us debug this:\n");
dad3d743
SR
2443
2444 /* Find a middle lock (if one exists) */
2445 depth = get_lock_depth(other);
2446 do {
2447 if (depth == 0 && (entry != root)) {
2448 printk("lockdep:%s bad path found in chain graph\n", __func__);
2449 break;
2450 }
2451 middle = entry;
2452 entry = get_lock_parent(entry);
2453 depth--;
2454 } while (entry && entry != root && (depth >= 0));
2455 if (forwards)
2456 print_irq_lock_scenario(root, other,
2457 middle ? middle->class : root->class, other->class);
2458 else
2459 print_irq_lock_scenario(other, root,
2460 middle ? middle->class : other->class, root->class);
2461
fbb9ce95
IM
2462 lockdep_print_held_locks(curr);
2463
24208ca7
ML
2464 printk("\nthe shortest dependencies between 2nd lock and 1st lock:\n");
2465 if (!save_trace(&root->trace))
2466 return 0;
2467 print_shortest_lock_dependencies(other, root);
fbb9ce95
IM
2468
2469 printk("\nstack backtrace:\n");
2470 dump_stack();
2471
2472 return 0;
2473}
2474
2475/*
2476 * Prove that in the forwards-direction subgraph starting at <this>
2477 * there is no lock matching <mask>:
2478 */
2479static int
2480check_usage_forwards(struct task_struct *curr, struct held_lock *this,
2481 enum lock_usage_bit bit, const char *irqclass)
2482{
2483 int ret;
d7aaba14
ML
2484 struct lock_list root;
2485 struct lock_list *uninitialized_var(target_entry);
fbb9ce95 2486
d7aaba14
ML
2487 root.parent = NULL;
2488 root.class = hlock_class(this);
2489 ret = find_usage_forwards(&root, bit, &target_entry);
af012961
PZ
2490 if (ret < 0)
2491 return print_bfs_bug(ret);
2492 if (ret == 1)
2493 return ret;
fbb9ce95 2494
24208ca7 2495 return print_irq_inversion_bug(curr, &root, target_entry,
d7aaba14 2496 this, 1, irqclass);
fbb9ce95
IM
2497}
2498
2499/*
2500 * Prove that in the backwards-direction subgraph starting at <this>
2501 * there is no lock matching <mask>:
2502 */
2503static int
2504check_usage_backwards(struct task_struct *curr, struct held_lock *this,
2505 enum lock_usage_bit bit, const char *irqclass)
2506{
2507 int ret;
d7aaba14
ML
2508 struct lock_list root;
2509 struct lock_list *uninitialized_var(target_entry);
fbb9ce95 2510
d7aaba14
ML
2511 root.parent = NULL;
2512 root.class = hlock_class(this);
2513 ret = find_usage_backwards(&root, bit, &target_entry);
af012961
PZ
2514 if (ret < 0)
2515 return print_bfs_bug(ret);
2516 if (ret == 1)
2517 return ret;
fbb9ce95 2518
24208ca7 2519 return print_irq_inversion_bug(curr, &root, target_entry,
48d50674 2520 this, 0, irqclass);
fbb9ce95
IM
2521}
2522
3117df04 2523void print_irqtrace_events(struct task_struct *curr)
fbb9ce95
IM
2524{
2525 printk("irq event stamp: %u\n", curr->irq_events);
f943fe0f
DV
2526 printk("hardirqs last enabled at (%u): [<%p>] %pS\n",
2527 curr->hardirq_enable_event, (void *)curr->hardirq_enable_ip,
2528 (void *)curr->hardirq_enable_ip);
2529 printk("hardirqs last disabled at (%u): [<%p>] %pS\n",
2530 curr->hardirq_disable_event, (void *)curr->hardirq_disable_ip,
2531 (void *)curr->hardirq_disable_ip);
2532 printk("softirqs last enabled at (%u): [<%p>] %pS\n",
2533 curr->softirq_enable_event, (void *)curr->softirq_enable_ip,
2534 (void *)curr->softirq_enable_ip);
2535 printk("softirqs last disabled at (%u): [<%p>] %pS\n",
2536 curr->softirq_disable_event, (void *)curr->softirq_disable_ip,
2537 (void *)curr->softirq_disable_ip);
fbb9ce95
IM
2538}
2539
cd95302d 2540static int HARDIRQ_verbose(struct lock_class *class)
fbb9ce95 2541{
8e18257d
PZ
2542#if HARDIRQ_VERBOSE
2543 return class_filter(class);
2544#endif
fbb9ce95
IM
2545 return 0;
2546}
2547
cd95302d 2548static int SOFTIRQ_verbose(struct lock_class *class)
fbb9ce95 2549{
8e18257d
PZ
2550#if SOFTIRQ_VERBOSE
2551 return class_filter(class);
2552#endif
2553 return 0;
fbb9ce95
IM
2554}
2555
cd95302d 2556static int RECLAIM_FS_verbose(struct lock_class *class)
cf40bd16
NP
2557{
2558#if RECLAIM_VERBOSE
2559 return class_filter(class);
2560#endif
2561 return 0;
2562}
2563
fbb9ce95
IM
2564#define STRICT_READ_CHECKS 1
2565
cd95302d
PZ
2566static int (*state_verbose_f[])(struct lock_class *class) = {
2567#define LOCKDEP_STATE(__STATE) \
2568 __STATE##_verbose,
2569#include "lockdep_states.h"
2570#undef LOCKDEP_STATE
2571};
2572
2573static inline int state_verbose(enum lock_usage_bit bit,
2574 struct lock_class *class)
2575{
2576 return state_verbose_f[bit >> 2](class);
2577}
2578
42c50d54
PZ
2579typedef int (*check_usage_f)(struct task_struct *, struct held_lock *,
2580 enum lock_usage_bit bit, const char *name);
2581
6a6904d3 2582static int
1c21f14e
PZ
2583mark_lock_irq(struct task_struct *curr, struct held_lock *this,
2584 enum lock_usage_bit new_bit)
6a6904d3 2585{
f989209e 2586 int excl_bit = exclusive_bit(new_bit);
9d3651a2 2587 int read = new_bit & 1;
42c50d54
PZ
2588 int dir = new_bit & 2;
2589
38aa2714
PZ
2590 /*
2591 * mark USED_IN has to look forwards -- to ensure no dependency
2592 * has ENABLED state, which would allow recursion deadlocks.
2593 *
2594 * mark ENABLED has to look backwards -- to ensure no dependee
2595 * has USED_IN state, which, again, would allow recursion deadlocks.
2596 */
42c50d54
PZ
2597 check_usage_f usage = dir ?
2598 check_usage_backwards : check_usage_forwards;
f989209e 2599
38aa2714
PZ
2600 /*
2601 * Validate that this particular lock does not have conflicting
2602 * usage states.
2603 */
6a6904d3
PZ
2604 if (!valid_state(curr, this, new_bit, excl_bit))
2605 return 0;
42c50d54 2606
38aa2714
PZ
2607 /*
2608 * Validate that the lock dependencies don't have conflicting usage
2609 * states.
2610 */
2611 if ((!read || !dir || STRICT_READ_CHECKS) &&
1c21f14e 2612 !usage(curr, this, excl_bit, state_name(new_bit & ~1)))
6a6904d3 2613 return 0;
780e820b 2614
38aa2714
PZ
2615 /*
2616 * Check for read in write conflicts
2617 */
2618 if (!read) {
2619 if (!valid_state(curr, this, new_bit, excl_bit + 1))
2620 return 0;
2621
2622 if (STRICT_READ_CHECKS &&
4f367d8a
PZ
2623 !usage(curr, this, excl_bit + 1,
2624 state_name(new_bit + 1)))
38aa2714
PZ
2625 return 0;
2626 }
780e820b 2627
cd95302d 2628 if (state_verbose(new_bit, hlock_class(this)))
6a6904d3
PZ
2629 return 2;
2630
2631 return 1;
2632}
2633
cf40bd16 2634enum mark_type {
36bfb9bb
PZ
2635#define LOCKDEP_STATE(__STATE) __STATE,
2636#include "lockdep_states.h"
2637#undef LOCKDEP_STATE
cf40bd16
NP
2638};
2639
fbb9ce95
IM
2640/*
2641 * Mark all held locks with a usage bit:
2642 */
1d09daa5 2643static int
cf40bd16 2644mark_held_locks(struct task_struct *curr, enum mark_type mark)
fbb9ce95
IM
2645{
2646 enum lock_usage_bit usage_bit;
2647 struct held_lock *hlock;
2648 int i;
2649
2650 for (i = 0; i < curr->lockdep_depth; i++) {
2651 hlock = curr->held_locks + i;
2652
cf2ad4d1
PZ
2653 usage_bit = 2 + (mark << 2); /* ENABLED */
2654 if (hlock->read)
2655 usage_bit += 1; /* READ */
2656
2657 BUG_ON(usage_bit >= LOCK_USAGE_STATES);
cf40bd16 2658
34d0ed5e 2659 if (!hlock->check)
efbe2eee
PZ
2660 continue;
2661
4ff773bb 2662 if (!mark_lock(curr, hlock, usage_bit))
fbb9ce95
IM
2663 return 0;
2664 }
2665
2666 return 1;
2667}
2668
fbb9ce95
IM
2669/*
2670 * Hardirqs will be enabled:
2671 */
dd4e5d3a 2672static void __trace_hardirqs_on_caller(unsigned long ip)
fbb9ce95
IM
2673{
2674 struct task_struct *curr = current;
fbb9ce95 2675
fbb9ce95
IM
2676 /* we'll do an OFF -> ON transition: */
2677 curr->hardirqs_enabled = 1;
fbb9ce95 2678
fbb9ce95
IM
2679 /*
2680 * We are going to turn hardirqs on, so set the
2681 * usage bit for all held locks:
2682 */
cf40bd16 2683 if (!mark_held_locks(curr, HARDIRQ))
fbb9ce95
IM
2684 return;
2685 /*
2686 * If we have softirqs enabled, then set the usage
2687 * bit for all held locks. (disabled hardirqs prevented
2688 * this bit from being set before)
2689 */
2690 if (curr->softirqs_enabled)
cf40bd16 2691 if (!mark_held_locks(curr, SOFTIRQ))
fbb9ce95
IM
2692 return;
2693
8e18257d
PZ
2694 curr->hardirq_enable_ip = ip;
2695 curr->hardirq_enable_event = ++curr->irq_events;
bd6d29c2 2696 debug_atomic_inc(hardirqs_on_events);
8e18257d 2697}
dd4e5d3a 2698
b35f8305 2699__visible void trace_hardirqs_on_caller(unsigned long ip)
dd4e5d3a
PZ
2700{
2701 time_hardirqs_on(CALLER_ADDR0, ip);
2702
2703 if (unlikely(!debug_locks || current->lockdep_recursion))
2704 return;
2705
7d36b26b
PZ
2706 if (unlikely(current->hardirqs_enabled)) {
2707 /*
2708 * Neither irq nor preemption are disabled here
2709 * so this is racy by nature but losing one hit
2710 * in a stat is not a big deal.
2711 */
2712 __debug_atomic_inc(redundant_hardirqs_on);
2713 return;
2714 }
2715
0119fee4
PZ
2716 /*
2717 * We're enabling irqs and according to our state above irqs weren't
2718 * already enabled, yet we find the hardware thinks they are in fact
2719 * enabled.. someone messed up their IRQ state tracing.
2720 */
dd4e5d3a
PZ
2721 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2722 return;
2723
0119fee4
PZ
2724 /*
2725 * See the fine text that goes along with this variable definition.
2726 */
7d36b26b
PZ
2727 if (DEBUG_LOCKS_WARN_ON(unlikely(early_boot_irqs_disabled)))
2728 return;
2729
0119fee4
PZ
2730 /*
2731 * Can't allow enabling interrupts while in an interrupt handler,
2732 * that's general bad form and such. Recursion, limited stack etc..
2733 */
7d36b26b
PZ
2734 if (DEBUG_LOCKS_WARN_ON(current->hardirq_context))
2735 return;
2736
dd4e5d3a
PZ
2737 current->lockdep_recursion = 1;
2738 __trace_hardirqs_on_caller(ip);
2739 current->lockdep_recursion = 0;
2740}
81d68a96 2741EXPORT_SYMBOL(trace_hardirqs_on_caller);
8e18257d 2742
1d09daa5 2743void trace_hardirqs_on(void)
81d68a96
SR
2744{
2745 trace_hardirqs_on_caller(CALLER_ADDR0);
2746}
8e18257d
PZ
2747EXPORT_SYMBOL(trace_hardirqs_on);
2748
2749/*
2750 * Hardirqs were disabled:
2751 */
b35f8305 2752__visible void trace_hardirqs_off_caller(unsigned long ip)
8e18257d
PZ
2753{
2754 struct task_struct *curr = current;
2755
6afe40b4 2756 time_hardirqs_off(CALLER_ADDR0, ip);
81d68a96 2757
8e18257d
PZ
2758 if (unlikely(!debug_locks || current->lockdep_recursion))
2759 return;
2760
0119fee4
PZ
2761 /*
2762 * So we're supposed to get called after you mask local IRQs, but for
2763 * some reason the hardware doesn't quite think you did a proper job.
2764 */
8e18257d
PZ
2765 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2766 return;
2767
2768 if (curr->hardirqs_enabled) {
2769 /*
2770 * We have done an ON -> OFF transition:
2771 */
2772 curr->hardirqs_enabled = 0;
6afe40b4 2773 curr->hardirq_disable_ip = ip;
8e18257d 2774 curr->hardirq_disable_event = ++curr->irq_events;
bd6d29c2 2775 debug_atomic_inc(hardirqs_off_events);
8e18257d 2776 } else
bd6d29c2 2777 debug_atomic_inc(redundant_hardirqs_off);
8e18257d 2778}
81d68a96 2779EXPORT_SYMBOL(trace_hardirqs_off_caller);
8e18257d 2780
1d09daa5 2781void trace_hardirqs_off(void)
81d68a96
SR
2782{
2783 trace_hardirqs_off_caller(CALLER_ADDR0);
2784}
8e18257d
PZ
2785EXPORT_SYMBOL(trace_hardirqs_off);
2786
2787/*
2788 * Softirqs will be enabled:
2789 */
2790void trace_softirqs_on(unsigned long ip)
2791{
2792 struct task_struct *curr = current;
2793
dd4e5d3a 2794 if (unlikely(!debug_locks || current->lockdep_recursion))
8e18257d
PZ
2795 return;
2796
0119fee4
PZ
2797 /*
2798 * We fancy IRQs being disabled here, see softirq.c, avoids
2799 * funny state and nesting things.
2800 */
8e18257d
PZ
2801 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2802 return;
2803
2804 if (curr->softirqs_enabled) {
bd6d29c2 2805 debug_atomic_inc(redundant_softirqs_on);
8e18257d
PZ
2806 return;
2807 }
2808
dd4e5d3a 2809 current->lockdep_recursion = 1;
8e18257d
PZ
2810 /*
2811 * We'll do an OFF -> ON transition:
2812 */
2813 curr->softirqs_enabled = 1;
2814 curr->softirq_enable_ip = ip;
2815 curr->softirq_enable_event = ++curr->irq_events;
bd6d29c2 2816 debug_atomic_inc(softirqs_on_events);
8e18257d
PZ
2817 /*
2818 * We are going to turn softirqs on, so set the
2819 * usage bit for all held locks, if hardirqs are
2820 * enabled too:
2821 */
2822 if (curr->hardirqs_enabled)
cf40bd16 2823 mark_held_locks(curr, SOFTIRQ);
dd4e5d3a 2824 current->lockdep_recursion = 0;
8e18257d
PZ
2825}
2826
2827/*
2828 * Softirqs were disabled:
2829 */
2830void trace_softirqs_off(unsigned long ip)
2831{
2832 struct task_struct *curr = current;
2833
dd4e5d3a 2834 if (unlikely(!debug_locks || current->lockdep_recursion))
8e18257d
PZ
2835 return;
2836
0119fee4
PZ
2837 /*
2838 * We fancy IRQs being disabled here, see softirq.c
2839 */
8e18257d
PZ
2840 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2841 return;
2842
2843 if (curr->softirqs_enabled) {
2844 /*
2845 * We have done an ON -> OFF transition:
2846 */
2847 curr->softirqs_enabled = 0;
2848 curr->softirq_disable_ip = ip;
2849 curr->softirq_disable_event = ++curr->irq_events;
bd6d29c2 2850 debug_atomic_inc(softirqs_off_events);
0119fee4
PZ
2851 /*
2852 * Whoops, we wanted softirqs off, so why aren't they?
2853 */
8e18257d
PZ
2854 DEBUG_LOCKS_WARN_ON(!softirq_count());
2855 } else
bd6d29c2 2856 debug_atomic_inc(redundant_softirqs_off);
8e18257d
PZ
2857}
2858
2f850181 2859static void __lockdep_trace_alloc(gfp_t gfp_mask, unsigned long flags)
cf40bd16
NP
2860{
2861 struct task_struct *curr = current;
2862
2863 if (unlikely(!debug_locks))
2864 return;
2865
2866 /* no reclaim without waiting on it */
d0164adc 2867 if (!(gfp_mask & __GFP_DIRECT_RECLAIM))
cf40bd16
NP
2868 return;
2869
2870 /* this guy won't enter reclaim */
2871 if ((curr->flags & PF_MEMALLOC) && !(gfp_mask & __GFP_NOMEMALLOC))
2872 return;
2873
2874 /* We're only interested __GFP_FS allocations for now */
2875 if (!(gfp_mask & __GFP_FS))
2876 return;
2877
0119fee4
PZ
2878 /*
2879 * Oi! Can't be having __GFP_FS allocations with IRQs disabled.
2880 */
2f850181 2881 if (DEBUG_LOCKS_WARN_ON(irqs_disabled_flags(flags)))
cf40bd16
NP
2882 return;
2883
2884 mark_held_locks(curr, RECLAIM_FS);
2885}
2886
2f850181
PZ
2887static void check_flags(unsigned long flags);
2888
2889void lockdep_trace_alloc(gfp_t gfp_mask)
2890{
2891 unsigned long flags;
2892
2893 if (unlikely(current->lockdep_recursion))
2894 return;
2895
2896 raw_local_irq_save(flags);
2897 check_flags(flags);
2898 current->lockdep_recursion = 1;
2899 __lockdep_trace_alloc(gfp_mask, flags);
2900 current->lockdep_recursion = 0;
2901 raw_local_irq_restore(flags);
2902}
2903
8e18257d
PZ
2904static int mark_irqflags(struct task_struct *curr, struct held_lock *hlock)
2905{
2906 /*
2907 * If non-trylock use in a hardirq or softirq context, then
2908 * mark the lock as used in these contexts:
2909 */
2910 if (!hlock->trylock) {
2911 if (hlock->read) {
2912 if (curr->hardirq_context)
2913 if (!mark_lock(curr, hlock,
2914 LOCK_USED_IN_HARDIRQ_READ))
2915 return 0;
2916 if (curr->softirq_context)
2917 if (!mark_lock(curr, hlock,
2918 LOCK_USED_IN_SOFTIRQ_READ))
2919 return 0;
2920 } else {
2921 if (curr->hardirq_context)
2922 if (!mark_lock(curr, hlock, LOCK_USED_IN_HARDIRQ))
2923 return 0;
2924 if (curr->softirq_context)
2925 if (!mark_lock(curr, hlock, LOCK_USED_IN_SOFTIRQ))
2926 return 0;
2927 }
2928 }
2929 if (!hlock->hardirqs_off) {
2930 if (hlock->read) {
2931 if (!mark_lock(curr, hlock,
4fc95e86 2932 LOCK_ENABLED_HARDIRQ_READ))
8e18257d
PZ
2933 return 0;
2934 if (curr->softirqs_enabled)
2935 if (!mark_lock(curr, hlock,
4fc95e86 2936 LOCK_ENABLED_SOFTIRQ_READ))
8e18257d
PZ
2937 return 0;
2938 } else {
2939 if (!mark_lock(curr, hlock,
4fc95e86 2940 LOCK_ENABLED_HARDIRQ))
8e18257d
PZ
2941 return 0;
2942 if (curr->softirqs_enabled)
2943 if (!mark_lock(curr, hlock,
4fc95e86 2944 LOCK_ENABLED_SOFTIRQ))
8e18257d
PZ
2945 return 0;
2946 }
2947 }
2948
cf40bd16
NP
2949 /*
2950 * We reuse the irq context infrastructure more broadly as a general
2951 * context checking code. This tests GFP_FS recursion (a lock taken
2952 * during reclaim for a GFP_FS allocation is held over a GFP_FS
2953 * allocation).
2954 */
2955 if (!hlock->trylock && (curr->lockdep_reclaim_gfp & __GFP_FS)) {
2956 if (hlock->read) {
2957 if (!mark_lock(curr, hlock, LOCK_USED_IN_RECLAIM_FS_READ))
2958 return 0;
2959 } else {
2960 if (!mark_lock(curr, hlock, LOCK_USED_IN_RECLAIM_FS))
2961 return 0;
2962 }
2963 }
2964
8e18257d
PZ
2965 return 1;
2966}
2967
c2469756
BF
2968static inline unsigned int task_irq_context(struct task_struct *task)
2969{
2970 return 2 * !!task->hardirq_context + !!task->softirq_context;
2971}
2972
8e18257d
PZ
2973static int separate_irq_context(struct task_struct *curr,
2974 struct held_lock *hlock)
2975{
2976 unsigned int depth = curr->lockdep_depth;
2977
2978 /*
2979 * Keep track of points where we cross into an interrupt context:
2980 */
8e18257d
PZ
2981 if (depth) {
2982 struct held_lock *prev_hlock;
2983
2984 prev_hlock = curr->held_locks + depth-1;
2985 /*
2986 * If we cross into another context, reset the
2987 * hash key (this also prevents the checking and the
2988 * adding of the dependency to 'prev'):
2989 */
2990 if (prev_hlock->irq_context != hlock->irq_context)
2991 return 1;
2992 }
2993 return 0;
fbb9ce95
IM
2994}
2995
0119fee4 2996#else /* defined(CONFIG_TRACE_IRQFLAGS) && defined(CONFIG_PROVE_LOCKING) */
fbb9ce95 2997
8e18257d
PZ
2998static inline
2999int mark_lock_irq(struct task_struct *curr, struct held_lock *this,
3000 enum lock_usage_bit new_bit)
fbb9ce95 3001{
0119fee4 3002 WARN_ON(1); /* Impossible innit? when we don't have TRACE_IRQFLAG */
8e18257d
PZ
3003 return 1;
3004}
fbb9ce95 3005
8e18257d
PZ
3006static inline int mark_irqflags(struct task_struct *curr,
3007 struct held_lock *hlock)
3008{
3009 return 1;
3010}
fbb9ce95 3011
c2469756
BF
3012static inline unsigned int task_irq_context(struct task_struct *task)
3013{
3014 return 0;
3015}
3016
8e18257d
PZ
3017static inline int separate_irq_context(struct task_struct *curr,
3018 struct held_lock *hlock)
3019{
3020 return 0;
fbb9ce95
IM
3021}
3022
868a23a8
PZ
3023void lockdep_trace_alloc(gfp_t gfp_mask)
3024{
3025}
3026
0119fee4 3027#endif /* defined(CONFIG_TRACE_IRQFLAGS) && defined(CONFIG_PROVE_LOCKING) */
fbb9ce95
IM
3028
3029/*
8e18257d 3030 * Mark a lock with a usage bit, and validate the state transition:
fbb9ce95 3031 */
1d09daa5 3032static int mark_lock(struct task_struct *curr, struct held_lock *this,
0764d23c 3033 enum lock_usage_bit new_bit)
fbb9ce95 3034{
8e18257d 3035 unsigned int new_mask = 1 << new_bit, ret = 1;
fbb9ce95
IM
3036
3037 /*
8e18257d
PZ
3038 * If already set then do not dirty the cacheline,
3039 * nor do any checks:
fbb9ce95 3040 */
f82b217e 3041 if (likely(hlock_class(this)->usage_mask & new_mask))
8e18257d
PZ
3042 return 1;
3043
3044 if (!graph_lock())
3045 return 0;
fbb9ce95 3046 /*
25985edc 3047 * Make sure we didn't race:
fbb9ce95 3048 */
f82b217e 3049 if (unlikely(hlock_class(this)->usage_mask & new_mask)) {
8e18257d
PZ
3050 graph_unlock();
3051 return 1;
3052 }
fbb9ce95 3053
f82b217e 3054 hlock_class(this)->usage_mask |= new_mask;
fbb9ce95 3055
f82b217e 3056 if (!save_trace(hlock_class(this)->usage_traces + new_bit))
8e18257d 3057 return 0;
fbb9ce95 3058
8e18257d 3059 switch (new_bit) {
5346417e
PZ
3060#define LOCKDEP_STATE(__STATE) \
3061 case LOCK_USED_IN_##__STATE: \
3062 case LOCK_USED_IN_##__STATE##_READ: \
3063 case LOCK_ENABLED_##__STATE: \
3064 case LOCK_ENABLED_##__STATE##_READ:
3065#include "lockdep_states.h"
3066#undef LOCKDEP_STATE
8e18257d
PZ
3067 ret = mark_lock_irq(curr, this, new_bit);
3068 if (!ret)
3069 return 0;
3070 break;
3071 case LOCK_USED:
bd6d29c2 3072 debug_atomic_dec(nr_unused_locks);
8e18257d
PZ
3073 break;
3074 default:
3075 if (!debug_locks_off_graph_unlock())
3076 return 0;
3077 WARN_ON(1);
3078 return 0;
3079 }
fbb9ce95 3080
8e18257d
PZ
3081 graph_unlock();
3082
3083 /*
3084 * We must printk outside of the graph_lock:
3085 */
3086 if (ret == 2) {
3087 printk("\nmarked lock as {%s}:\n", usage_str[new_bit]);
3088 print_lock(this);
3089 print_irqtrace_events(curr);
3090 dump_stack();
3091 }
3092
3093 return ret;
3094}
fbb9ce95
IM
3095
3096/*
3097 * Initialize a lock instance's lock-class mapping info:
3098 */
3099void lockdep_init_map(struct lockdep_map *lock, const char *name,
4dfbb9d8 3100 struct lock_class_key *key, int subclass)
fbb9ce95 3101{
d3d03d4f
YZ
3102 int i;
3103
3104 kmemcheck_mark_initialized(lock, sizeof(*lock));
3105
3106 for (i = 0; i < NR_LOCKDEP_CACHING_CLASSES; i++)
3107 lock->class_cache[i] = NULL;
62016250 3108
c8a25005
PZ
3109#ifdef CONFIG_LOCK_STAT
3110 lock->cpu = raw_smp_processor_id();
3111#endif
3112
0119fee4
PZ
3113 /*
3114 * Can't be having no nameless bastards around this place!
3115 */
c8a25005
PZ
3116 if (DEBUG_LOCKS_WARN_ON(!name)) {
3117 lock->name = "NULL";
fbb9ce95 3118 return;
c8a25005
PZ
3119 }
3120
3121 lock->name = name;
fbb9ce95 3122
0119fee4
PZ
3123 /*
3124 * No key, no joy, we need to hash something.
3125 */
fbb9ce95
IM
3126 if (DEBUG_LOCKS_WARN_ON(!key))
3127 return;
fbb9ce95
IM
3128 /*
3129 * Sanity check, the lock-class key must be persistent:
3130 */
3131 if (!static_obj(key)) {
3132 printk("BUG: key %p not in .data!\n", key);
0119fee4
PZ
3133 /*
3134 * What it says above ^^^^^, I suggest you read it.
3135 */
fbb9ce95
IM
3136 DEBUG_LOCKS_WARN_ON(1);
3137 return;
3138 }
fbb9ce95 3139 lock->key = key;
c8a25005
PZ
3140
3141 if (unlikely(!debug_locks))
3142 return;
3143
35a9393c
PZ
3144 if (subclass) {
3145 unsigned long flags;
3146
3147 if (DEBUG_LOCKS_WARN_ON(current->lockdep_recursion))
3148 return;
3149
3150 raw_local_irq_save(flags);
3151 current->lockdep_recursion = 1;
4dfbb9d8 3152 register_lock_class(lock, subclass, 1);
35a9393c
PZ
3153 current->lockdep_recursion = 0;
3154 raw_local_irq_restore(flags);
3155 }
fbb9ce95 3156}
fbb9ce95
IM
3157EXPORT_SYMBOL_GPL(lockdep_init_map);
3158
1704f47b 3159struct lock_class_key __lockdep_no_validate__;
ea6749c7 3160EXPORT_SYMBOL_GPL(__lockdep_no_validate__);
1704f47b 3161
d0945950
ML
3162static int
3163print_lock_nested_lock_not_held(struct task_struct *curr,
3164 struct held_lock *hlock,
3165 unsigned long ip)
3166{
3167 if (!debug_locks_off())
3168 return 0;
3169 if (debug_locks_silent)
3170 return 0;
3171
3172 printk("\n");
3173 printk("==================================\n");
3174 printk("[ BUG: Nested lock was not taken ]\n");
3175 print_kernel_ident();
3176 printk("----------------------------------\n");
3177
3178 printk("%s/%d is trying to lock:\n", curr->comm, task_pid_nr(curr));
3179 print_lock(hlock);
3180
3181 printk("\nbut this task is not holding:\n");
3182 printk("%s\n", hlock->nest_lock->name);
3183
3184 printk("\nstack backtrace:\n");
3185 dump_stack();
3186
3187 printk("\nother info that might help us debug this:\n");
3188 lockdep_print_held_locks(curr);
3189
3190 printk("\nstack backtrace:\n");
3191 dump_stack();
3192
3193 return 0;
3194}
3195
f8319483 3196static int __lock_is_held(struct lockdep_map *lock, int read);
d0945950 3197
fbb9ce95
IM
3198/*
3199 * This gets called for every mutex_lock*()/spin_lock*() operation.
3200 * We maintain the dependency maps and validate the locking attempt:
3201 */
3202static int __lock_acquire(struct lockdep_map *lock, unsigned int subclass,
3203 int trylock, int read, int check, int hardirqs_off,
bb97a91e 3204 struct lockdep_map *nest_lock, unsigned long ip,
21199f27 3205 int references, int pin_count)
fbb9ce95
IM
3206{
3207 struct task_struct *curr = current;
d6d897ce 3208 struct lock_class *class = NULL;
fbb9ce95 3209 struct held_lock *hlock;
5f18ab5c 3210 unsigned int depth;
fbb9ce95 3211 int chain_head = 0;
bb97a91e 3212 int class_idx;
fbb9ce95
IM
3213 u64 chain_key;
3214
3215 if (unlikely(!debug_locks))
3216 return 0;
3217
0119fee4
PZ
3218 /*
3219 * Lockdep should run with IRQs disabled, otherwise we could
3220 * get an interrupt which would want to take locks, which would
3221 * end up in lockdep and have you got a head-ache already?
3222 */
fbb9ce95
IM
3223 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
3224 return 0;
3225
fb9edbe9
ON
3226 if (!prove_locking || lock->key == &__lockdep_no_validate__)
3227 check = 0;
1704f47b 3228
62016250
HM
3229 if (subclass < NR_LOCKDEP_CACHING_CLASSES)
3230 class = lock->class_cache[subclass];
d6d897ce 3231 /*
62016250 3232 * Not cached?
d6d897ce 3233 */
fbb9ce95 3234 if (unlikely(!class)) {
4dfbb9d8 3235 class = register_lock_class(lock, subclass, 0);
fbb9ce95
IM
3236 if (!class)
3237 return 0;
3238 }
bd6d29c2 3239 atomic_inc((atomic_t *)&class->ops);
fbb9ce95
IM
3240 if (very_verbose(class)) {
3241 printk("\nacquire class [%p] %s", class->key, class->name);
3242 if (class->name_version > 1)
f943fe0f
DV
3243 printk(KERN_CONT "#%d", class->name_version);
3244 printk(KERN_CONT "\n");
fbb9ce95
IM
3245 dump_stack();
3246 }
3247
3248 /*
3249 * Add the lock to the list of currently held locks.
3250 * (we dont increase the depth just yet, up until the
3251 * dependency checks are done)
3252 */
3253 depth = curr->lockdep_depth;
0119fee4
PZ
3254 /*
3255 * Ran out of static storage for our per-task lock stack again have we?
3256 */
fbb9ce95
IM
3257 if (DEBUG_LOCKS_WARN_ON(depth >= MAX_LOCK_DEPTH))
3258 return 0;
3259
bb97a91e
PZ
3260 class_idx = class - lock_classes + 1;
3261
3262 if (depth) {
3263 hlock = curr->held_locks + depth - 1;
3264 if (hlock->class_idx == class_idx && nest_lock) {
3265 if (hlock->references)
3266 hlock->references++;
3267 else
3268 hlock->references = 2;
3269
3270 return 1;
3271 }
3272 }
3273
fbb9ce95 3274 hlock = curr->held_locks + depth;
0119fee4
PZ
3275 /*
3276 * Plain impossible, we just registered it and checked it weren't no
3277 * NULL like.. I bet this mushroom I ate was good!
3278 */
f82b217e
DJ
3279 if (DEBUG_LOCKS_WARN_ON(!class))
3280 return 0;
bb97a91e 3281 hlock->class_idx = class_idx;
fbb9ce95
IM
3282 hlock->acquire_ip = ip;
3283 hlock->instance = lock;
7531e2f3 3284 hlock->nest_lock = nest_lock;
c2469756 3285 hlock->irq_context = task_irq_context(curr);
fbb9ce95
IM
3286 hlock->trylock = trylock;
3287 hlock->read = read;
3288 hlock->check = check;
6951b12a 3289 hlock->hardirqs_off = !!hardirqs_off;
bb97a91e 3290 hlock->references = references;
f20786ff
PZ
3291#ifdef CONFIG_LOCK_STAT
3292 hlock->waittime_stamp = 0;
3365e779 3293 hlock->holdtime_stamp = lockstat_clock();
f20786ff 3294#endif
21199f27 3295 hlock->pin_count = pin_count;
fbb9ce95 3296
fb9edbe9 3297 if (check && !mark_irqflags(curr, hlock))
8e18257d
PZ
3298 return 0;
3299
fbb9ce95 3300 /* mark it as used: */
4ff773bb 3301 if (!mark_lock(curr, hlock, LOCK_USED))
fbb9ce95 3302 return 0;
8e18257d 3303
fbb9ce95 3304 /*
17aacfb9 3305 * Calculate the chain hash: it's the combined hash of all the
fbb9ce95
IM
3306 * lock keys along the dependency chain. We save the hash value
3307 * at every step so that we can get the current hash easily
3308 * after unlock. The chain hash is then used to cache dependency
3309 * results.
3310 *
3311 * The 'key ID' is what is the most compact key value to drive
3312 * the hash, not class->key.
3313 */
0119fee4
PZ
3314 /*
3315 * Whoops, we did it again.. ran straight out of our static allocation.
3316 */
5f18ab5c 3317 if (DEBUG_LOCKS_WARN_ON(class_idx > MAX_LOCKDEP_KEYS))
fbb9ce95
IM
3318 return 0;
3319
3320 chain_key = curr->curr_chain_key;
3321 if (!depth) {
0119fee4
PZ
3322 /*
3323 * How can we have a chain hash when we ain't got no keys?!
3324 */
fbb9ce95
IM
3325 if (DEBUG_LOCKS_WARN_ON(chain_key != 0))
3326 return 0;
3327 chain_head = 1;
3328 }
3329
3330 hlock->prev_chain_key = chain_key;
8e18257d
PZ
3331 if (separate_irq_context(curr, hlock)) {
3332 chain_key = 0;
3333 chain_head = 1;
fbb9ce95 3334 }
5f18ab5c 3335 chain_key = iterate_chain_key(chain_key, class_idx);
fbb9ce95 3336
f8319483 3337 if (nest_lock && !__lock_is_held(nest_lock, -1))
d0945950
ML
3338 return print_lock_nested_lock_not_held(curr, hlock, ip);
3339
3aa416b0 3340 if (!validate_chain(curr, lock, hlock, chain_head, chain_key))
8e18257d 3341 return 0;
381a2292 3342
3aa416b0 3343 curr->curr_chain_key = chain_key;
fbb9ce95
IM
3344 curr->lockdep_depth++;
3345 check_chain_key(curr);
60e114d1
JP
3346#ifdef CONFIG_DEBUG_LOCKDEP
3347 if (unlikely(!debug_locks))
3348 return 0;
3349#endif
fbb9ce95
IM
3350 if (unlikely(curr->lockdep_depth >= MAX_LOCK_DEPTH)) {
3351 debug_locks_off();
2c522836
DJ
3352 print_lockdep_off("BUG: MAX_LOCK_DEPTH too low!");
3353 printk(KERN_DEBUG "depth: %i max: %lu!\n",
c0540606 3354 curr->lockdep_depth, MAX_LOCK_DEPTH);
c0540606
BG
3355
3356 lockdep_print_held_locks(current);
3357 debug_show_all_locks();
eedeeabd 3358 dump_stack();
c0540606 3359
fbb9ce95
IM
3360 return 0;
3361 }
381a2292 3362
fbb9ce95
IM
3363 if (unlikely(curr->lockdep_depth > max_lockdep_depth))
3364 max_lockdep_depth = curr->lockdep_depth;
3365
3366 return 1;
3367}
3368
3369static int
f86f7554 3370print_unlock_imbalance_bug(struct task_struct *curr, struct lockdep_map *lock,
fbb9ce95
IM
3371 unsigned long ip)
3372{
3373 if (!debug_locks_off())
3374 return 0;
3375 if (debug_locks_silent)
3376 return 0;
3377
b3fbab05
PM
3378 printk("\n");
3379 printk("=====================================\n");
3380 printk("[ BUG: bad unlock balance detected! ]\n");
fbdc4b9a 3381 print_kernel_ident();
b3fbab05 3382 printk("-------------------------------------\n");
fbb9ce95 3383 printk("%s/%d is trying to release lock (",
ba25f9dc 3384 curr->comm, task_pid_nr(curr));
fbb9ce95 3385 print_lockdep_cache(lock);
f943fe0f 3386 printk(KERN_CONT ") at:\n");
fbb9ce95
IM
3387 print_ip_sym(ip);
3388 printk("but there are no more locks to release!\n");
3389 printk("\nother info that might help us debug this:\n");
3390 lockdep_print_held_locks(curr);
3391
3392 printk("\nstack backtrace:\n");
3393 dump_stack();
3394
3395 return 0;
3396}
3397
bb97a91e
PZ
3398static int match_held_lock(struct held_lock *hlock, struct lockdep_map *lock)
3399{
3400 if (hlock->instance == lock)
3401 return 1;
3402
3403 if (hlock->references) {
62016250 3404 struct lock_class *class = lock->class_cache[0];
bb97a91e
PZ
3405
3406 if (!class)
3407 class = look_up_lock_class(lock, 0);
3408
80e0401e
PZ
3409 /*
3410 * If look_up_lock_class() failed to find a class, we're trying
3411 * to test if we hold a lock that has never yet been acquired.
3412 * Clearly if the lock hasn't been acquired _ever_, we're not
3413 * holding it either, so report failure.
3414 */
3415 if (!class)
bb97a91e
PZ
3416 return 0;
3417
0119fee4
PZ
3418 /*
3419 * References, but not a lock we're actually ref-counting?
3420 * State got messed up, follow the sites that change ->references
3421 * and try to make sense of it.
3422 */
bb97a91e
PZ
3423 if (DEBUG_LOCKS_WARN_ON(!hlock->nest_lock))
3424 return 0;
3425
3426 if (hlock->class_idx == class - lock_classes + 1)
3427 return 1;
3428 }
3429
3430 return 0;
3431}
3432
64aa348e 3433static int
00ef9f73
PZ
3434__lock_set_class(struct lockdep_map *lock, const char *name,
3435 struct lock_class_key *key, unsigned int subclass,
3436 unsigned long ip)
64aa348e
PZ
3437{
3438 struct task_struct *curr = current;
3439 struct held_lock *hlock, *prev_hlock;
3440 struct lock_class *class;
3441 unsigned int depth;
3442 int i;
3443
3444 depth = curr->lockdep_depth;
0119fee4
PZ
3445 /*
3446 * This function is about (re)setting the class of a held lock,
3447 * yet we're not actually holding any locks. Naughty user!
3448 */
64aa348e
PZ
3449 if (DEBUG_LOCKS_WARN_ON(!depth))
3450 return 0;
3451
3452 prev_hlock = NULL;
3453 for (i = depth-1; i >= 0; i--) {
3454 hlock = curr->held_locks + i;
3455 /*
3456 * We must not cross into another context:
3457 */
3458 if (prev_hlock && prev_hlock->irq_context != hlock->irq_context)
3459 break;
bb97a91e 3460 if (match_held_lock(hlock, lock))
64aa348e
PZ
3461 goto found_it;
3462 prev_hlock = hlock;
3463 }
f86f7554 3464 return print_unlock_imbalance_bug(curr, lock, ip);
64aa348e
PZ
3465
3466found_it:
00ef9f73 3467 lockdep_init_map(lock, name, key, 0);
64aa348e 3468 class = register_lock_class(lock, subclass, 0);
f82b217e 3469 hlock->class_idx = class - lock_classes + 1;
64aa348e
PZ
3470
3471 curr->lockdep_depth = i;
3472 curr->curr_chain_key = hlock->prev_chain_key;
3473
3474 for (; i < depth; i++) {
3475 hlock = curr->held_locks + i;
3476 if (!__lock_acquire(hlock->instance,
f82b217e 3477 hlock_class(hlock)->subclass, hlock->trylock,
64aa348e 3478 hlock->read, hlock->check, hlock->hardirqs_off,
bb97a91e 3479 hlock->nest_lock, hlock->acquire_ip,
21199f27 3480 hlock->references, hlock->pin_count))
64aa348e
PZ
3481 return 0;
3482 }
3483
0119fee4
PZ
3484 /*
3485 * I took it apart and put it back together again, except now I have
3486 * these 'spare' parts.. where shall I put them.
3487 */
64aa348e
PZ
3488 if (DEBUG_LOCKS_WARN_ON(curr->lockdep_depth != depth))
3489 return 0;
3490 return 1;
3491}
3492
fbb9ce95 3493/*
e0f56fd7
PZ
3494 * Remove the lock to the list of currently held locks - this gets
3495 * called on mutex_unlock()/spin_unlock*() (or on a failed
3496 * mutex_lock_interruptible()).
3497 *
3498 * @nested is an hysterical artifact, needs a tree wide cleanup.
fbb9ce95
IM
3499 */
3500static int
e0f56fd7 3501__lock_release(struct lockdep_map *lock, int nested, unsigned long ip)
fbb9ce95 3502{
e0f56fd7 3503 struct task_struct *curr = current;
fbb9ce95
IM
3504 struct held_lock *hlock, *prev_hlock;
3505 unsigned int depth;
3506 int i;
3507
e0f56fd7
PZ
3508 if (unlikely(!debug_locks))
3509 return 0;
3510
fbb9ce95 3511 depth = curr->lockdep_depth;
0119fee4
PZ
3512 /*
3513 * So we're all set to release this lock.. wait what lock? We don't
3514 * own any locks, you've been drinking again?
3515 */
e0f56fd7
PZ
3516 if (DEBUG_LOCKS_WARN_ON(depth <= 0))
3517 return print_unlock_imbalance_bug(curr, lock, ip);
fbb9ce95 3518
e0f56fd7
PZ
3519 /*
3520 * Check whether the lock exists in the current stack
3521 * of held locks:
3522 */
fbb9ce95
IM
3523 prev_hlock = NULL;
3524 for (i = depth-1; i >= 0; i--) {
3525 hlock = curr->held_locks + i;
3526 /*
3527 * We must not cross into another context:
3528 */
3529 if (prev_hlock && prev_hlock->irq_context != hlock->irq_context)
3530 break;
bb97a91e 3531 if (match_held_lock(hlock, lock))
fbb9ce95
IM
3532 goto found_it;
3533 prev_hlock = hlock;
3534 }
f86f7554 3535 return print_unlock_imbalance_bug(curr, lock, ip);
fbb9ce95
IM
3536
3537found_it:
bb97a91e
PZ
3538 if (hlock->instance == lock)
3539 lock_release_holdtime(hlock);
3540
a24fc60d
PZ
3541 WARN(hlock->pin_count, "releasing a pinned lock\n");
3542
bb97a91e
PZ
3543 if (hlock->references) {
3544 hlock->references--;
3545 if (hlock->references) {
3546 /*
3547 * We had, and after removing one, still have
3548 * references, the current lock stack is still
3549 * valid. We're done!
3550 */
3551 return 1;
3552 }
3553 }
f20786ff 3554
fbb9ce95
IM
3555 /*
3556 * We have the right lock to unlock, 'hlock' points to it.
3557 * Now we remove it from the stack, and add back the other
3558 * entries (if any), recalculating the hash along the way:
3559 */
bb97a91e 3560
fbb9ce95
IM
3561 curr->lockdep_depth = i;
3562 curr->curr_chain_key = hlock->prev_chain_key;
3563
3564 for (i++; i < depth; i++) {
3565 hlock = curr->held_locks + i;
3566 if (!__lock_acquire(hlock->instance,
f82b217e 3567 hlock_class(hlock)->subclass, hlock->trylock,
fbb9ce95 3568 hlock->read, hlock->check, hlock->hardirqs_off,
bb97a91e 3569 hlock->nest_lock, hlock->acquire_ip,
21199f27 3570 hlock->references, hlock->pin_count))
fbb9ce95
IM
3571 return 0;
3572 }
3573
0119fee4
PZ
3574 /*
3575 * We had N bottles of beer on the wall, we drank one, but now
3576 * there's not N-1 bottles of beer left on the wall...
3577 */
fbb9ce95
IM
3578 if (DEBUG_LOCKS_WARN_ON(curr->lockdep_depth != depth - 1))
3579 return 0;
f20786ff 3580
fbb9ce95
IM
3581 return 1;
3582}
3583
f8319483 3584static int __lock_is_held(struct lockdep_map *lock, int read)
fbb9ce95 3585{
f607c668
PZ
3586 struct task_struct *curr = current;
3587 int i;
fbb9ce95 3588
f607c668 3589 for (i = 0; i < curr->lockdep_depth; i++) {
bb97a91e 3590 struct held_lock *hlock = curr->held_locks + i;
fbb9ce95 3591
f8319483
PZ
3592 if (match_held_lock(hlock, lock)) {
3593 if (read == -1 || hlock->read == read)
3594 return 1;
3595
3596 return 0;
3597 }
f607c668 3598 }
f20786ff 3599
f607c668 3600 return 0;
fbb9ce95
IM
3601}
3602
e7904a28
PZ
3603static struct pin_cookie __lock_pin_lock(struct lockdep_map *lock)
3604{
3605 struct pin_cookie cookie = NIL_COOKIE;
3606 struct task_struct *curr = current;
3607 int i;
3608
3609 if (unlikely(!debug_locks))
3610 return cookie;
3611
3612 for (i = 0; i < curr->lockdep_depth; i++) {
3613 struct held_lock *hlock = curr->held_locks + i;
3614
3615 if (match_held_lock(hlock, lock)) {
3616 /*
3617 * Grab 16bits of randomness; this is sufficient to not
3618 * be guessable and still allows some pin nesting in
3619 * our u32 pin_count.
3620 */
3621 cookie.val = 1 + (prandom_u32() >> 16);
3622 hlock->pin_count += cookie.val;
3623 return cookie;
3624 }
3625 }
3626
3627 WARN(1, "pinning an unheld lock\n");
3628 return cookie;
3629}
3630
3631static void __lock_repin_lock(struct lockdep_map *lock, struct pin_cookie cookie)
fbb9ce95
IM
3632{
3633 struct task_struct *curr = current;
a24fc60d 3634 int i;
fbb9ce95 3635
a24fc60d 3636 if (unlikely(!debug_locks))
fbb9ce95
IM
3637 return;
3638
a24fc60d
PZ
3639 for (i = 0; i < curr->lockdep_depth; i++) {
3640 struct held_lock *hlock = curr->held_locks + i;
3641
3642 if (match_held_lock(hlock, lock)) {
e7904a28 3643 hlock->pin_count += cookie.val;
fbb9ce95 3644 return;
a24fc60d 3645 }
fbb9ce95
IM
3646 }
3647
a24fc60d 3648 WARN(1, "pinning an unheld lock\n");
fbb9ce95
IM
3649}
3650
e7904a28 3651static void __lock_unpin_lock(struct lockdep_map *lock, struct pin_cookie cookie)
f607c668
PZ
3652{
3653 struct task_struct *curr = current;
3654 int i;
3655
a24fc60d
PZ
3656 if (unlikely(!debug_locks))
3657 return;
3658
f607c668 3659 for (i = 0; i < curr->lockdep_depth; i++) {
bb97a91e
PZ
3660 struct held_lock *hlock = curr->held_locks + i;
3661
a24fc60d
PZ
3662 if (match_held_lock(hlock, lock)) {
3663 if (WARN(!hlock->pin_count, "unpinning an unpinned lock\n"))
3664 return;
3665
e7904a28
PZ
3666 hlock->pin_count -= cookie.val;
3667
3668 if (WARN((int)hlock->pin_count < 0, "pin count corrupted\n"))
3669 hlock->pin_count = 0;
3670
a24fc60d
PZ
3671 return;
3672 }
f607c668
PZ
3673 }
3674
a24fc60d 3675 WARN(1, "unpinning an unheld lock\n");
f607c668
PZ
3676}
3677
fbb9ce95
IM
3678/*
3679 * Check whether we follow the irq-flags state precisely:
3680 */
1d09daa5 3681static void check_flags(unsigned long flags)
fbb9ce95 3682{
992860e9
IM
3683#if defined(CONFIG_PROVE_LOCKING) && defined(CONFIG_DEBUG_LOCKDEP) && \
3684 defined(CONFIG_TRACE_IRQFLAGS)
fbb9ce95
IM
3685 if (!debug_locks)
3686 return;
3687
5f9fa8a6
IM
3688 if (irqs_disabled_flags(flags)) {
3689 if (DEBUG_LOCKS_WARN_ON(current->hardirqs_enabled)) {
3690 printk("possible reason: unannotated irqs-off.\n");
3691 }
3692 } else {
3693 if (DEBUG_LOCKS_WARN_ON(!current->hardirqs_enabled)) {
3694 printk("possible reason: unannotated irqs-on.\n");
3695 }
3696 }
fbb9ce95
IM
3697
3698 /*
3699 * We dont accurately track softirq state in e.g.
3700 * hardirq contexts (such as on 4KSTACKS), so only
3701 * check if not in hardirq contexts:
3702 */
3703 if (!hardirq_count()) {
0119fee4
PZ
3704 if (softirq_count()) {
3705 /* like the above, but with softirqs */
fbb9ce95 3706 DEBUG_LOCKS_WARN_ON(current->softirqs_enabled);
0119fee4
PZ
3707 } else {
3708 /* lick the above, does it taste good? */
fbb9ce95 3709 DEBUG_LOCKS_WARN_ON(!current->softirqs_enabled);
0119fee4 3710 }
fbb9ce95
IM
3711 }
3712
3713 if (!debug_locks)
3714 print_irqtrace_events(current);
3715#endif
3716}
3717
00ef9f73
PZ
3718void lock_set_class(struct lockdep_map *lock, const char *name,
3719 struct lock_class_key *key, unsigned int subclass,
3720 unsigned long ip)
64aa348e
PZ
3721{
3722 unsigned long flags;
3723
3724 if (unlikely(current->lockdep_recursion))
3725 return;
3726
3727 raw_local_irq_save(flags);
3728 current->lockdep_recursion = 1;
3729 check_flags(flags);
00ef9f73 3730 if (__lock_set_class(lock, name, key, subclass, ip))
64aa348e
PZ
3731 check_chain_key(current);
3732 current->lockdep_recursion = 0;
3733 raw_local_irq_restore(flags);
3734}
00ef9f73 3735EXPORT_SYMBOL_GPL(lock_set_class);
64aa348e 3736
fbb9ce95
IM
3737/*
3738 * We are not always called with irqs disabled - do that here,
3739 * and also avoid lockdep recursion:
3740 */
1d09daa5 3741void lock_acquire(struct lockdep_map *lock, unsigned int subclass,
7531e2f3
PZ
3742 int trylock, int read, int check,
3743 struct lockdep_map *nest_lock, unsigned long ip)
fbb9ce95
IM
3744{
3745 unsigned long flags;
3746
3747 if (unlikely(current->lockdep_recursion))
3748 return;
3749
3750 raw_local_irq_save(flags);
3751 check_flags(flags);
3752
3753 current->lockdep_recursion = 1;
db2c4c77 3754 trace_lock_acquire(lock, subclass, trylock, read, check, nest_lock, ip);
fbb9ce95 3755 __lock_acquire(lock, subclass, trylock, read, check,
21199f27 3756 irqs_disabled_flags(flags), nest_lock, ip, 0, 0);
fbb9ce95
IM
3757 current->lockdep_recursion = 0;
3758 raw_local_irq_restore(flags);
3759}
fbb9ce95
IM
3760EXPORT_SYMBOL_GPL(lock_acquire);
3761
1d09daa5 3762void lock_release(struct lockdep_map *lock, int nested,
0764d23c 3763 unsigned long ip)
fbb9ce95
IM
3764{
3765 unsigned long flags;
3766
3767 if (unlikely(current->lockdep_recursion))
3768 return;
3769
3770 raw_local_irq_save(flags);
3771 check_flags(flags);
3772 current->lockdep_recursion = 1;
93135439 3773 trace_lock_release(lock, ip);
e0f56fd7
PZ
3774 if (__lock_release(lock, nested, ip))
3775 check_chain_key(current);
fbb9ce95
IM
3776 current->lockdep_recursion = 0;
3777 raw_local_irq_restore(flags);
3778}
fbb9ce95
IM
3779EXPORT_SYMBOL_GPL(lock_release);
3780
f8319483 3781int lock_is_held_type(struct lockdep_map *lock, int read)
f607c668
PZ
3782{
3783 unsigned long flags;
3784 int ret = 0;
3785
3786 if (unlikely(current->lockdep_recursion))
f2513cde 3787 return 1; /* avoid false negative lockdep_assert_held() */
f607c668
PZ
3788
3789 raw_local_irq_save(flags);
3790 check_flags(flags);
3791
3792 current->lockdep_recursion = 1;
f8319483 3793 ret = __lock_is_held(lock, read);
f607c668
PZ
3794 current->lockdep_recursion = 0;
3795 raw_local_irq_restore(flags);
3796
3797 return ret;
3798}
f8319483 3799EXPORT_SYMBOL_GPL(lock_is_held_type);
f607c668 3800
e7904a28 3801struct pin_cookie lock_pin_lock(struct lockdep_map *lock)
a24fc60d 3802{
e7904a28 3803 struct pin_cookie cookie = NIL_COOKIE;
a24fc60d
PZ
3804 unsigned long flags;
3805
3806 if (unlikely(current->lockdep_recursion))
e7904a28 3807 return cookie;
a24fc60d
PZ
3808
3809 raw_local_irq_save(flags);
3810 check_flags(flags);
3811
3812 current->lockdep_recursion = 1;
e7904a28 3813 cookie = __lock_pin_lock(lock);
a24fc60d
PZ
3814 current->lockdep_recursion = 0;
3815 raw_local_irq_restore(flags);
e7904a28
PZ
3816
3817 return cookie;
a24fc60d
PZ
3818}
3819EXPORT_SYMBOL_GPL(lock_pin_lock);
3820
e7904a28
PZ
3821void lock_repin_lock(struct lockdep_map *lock, struct pin_cookie cookie)
3822{
3823 unsigned long flags;
3824
3825 if (unlikely(current->lockdep_recursion))
3826 return;
3827
3828 raw_local_irq_save(flags);
3829 check_flags(flags);
3830
3831 current->lockdep_recursion = 1;
3832 __lock_repin_lock(lock, cookie);
3833 current->lockdep_recursion = 0;
3834 raw_local_irq_restore(flags);
3835}
3836EXPORT_SYMBOL_GPL(lock_repin_lock);
3837
3838void lock_unpin_lock(struct lockdep_map *lock, struct pin_cookie cookie)
a24fc60d
PZ
3839{
3840 unsigned long flags;
3841
3842 if (unlikely(current->lockdep_recursion))
3843 return;
3844
3845 raw_local_irq_save(flags);
3846 check_flags(flags);
3847
3848 current->lockdep_recursion = 1;
e7904a28 3849 __lock_unpin_lock(lock, cookie);
a24fc60d
PZ
3850 current->lockdep_recursion = 0;
3851 raw_local_irq_restore(flags);
3852}
3853EXPORT_SYMBOL_GPL(lock_unpin_lock);
3854
cf40bd16
NP
3855void lockdep_set_current_reclaim_state(gfp_t gfp_mask)
3856{
3857 current->lockdep_reclaim_gfp = gfp_mask;
3858}
3859
3860void lockdep_clear_current_reclaim_state(void)
3861{
3862 current->lockdep_reclaim_gfp = 0;
3863}
3864
f20786ff
PZ
3865#ifdef CONFIG_LOCK_STAT
3866static int
3867print_lock_contention_bug(struct task_struct *curr, struct lockdep_map *lock,
3868 unsigned long ip)
3869{
3870 if (!debug_locks_off())
3871 return 0;
3872 if (debug_locks_silent)
3873 return 0;
3874
b3fbab05
PM
3875 printk("\n");
3876 printk("=================================\n");
3877 printk("[ BUG: bad contention detected! ]\n");
fbdc4b9a 3878 print_kernel_ident();
b3fbab05 3879 printk("---------------------------------\n");
f20786ff 3880 printk("%s/%d is trying to contend lock (",
ba25f9dc 3881 curr->comm, task_pid_nr(curr));
f20786ff 3882 print_lockdep_cache(lock);
f943fe0f 3883 printk(KERN_CONT ") at:\n");
f20786ff
PZ
3884 print_ip_sym(ip);
3885 printk("but there are no locks held!\n");
3886 printk("\nother info that might help us debug this:\n");
3887 lockdep_print_held_locks(curr);
3888
3889 printk("\nstack backtrace:\n");
3890 dump_stack();
3891
3892 return 0;
3893}
3894
3895static void
3896__lock_contended(struct lockdep_map *lock, unsigned long ip)
3897{
3898 struct task_struct *curr = current;
3899 struct held_lock *hlock, *prev_hlock;
3900 struct lock_class_stats *stats;
3901 unsigned int depth;
c7e78cff 3902 int i, contention_point, contending_point;
f20786ff
PZ
3903
3904 depth = curr->lockdep_depth;
0119fee4
PZ
3905 /*
3906 * Whee, we contended on this lock, except it seems we're not
3907 * actually trying to acquire anything much at all..
3908 */
f20786ff
PZ
3909 if (DEBUG_LOCKS_WARN_ON(!depth))
3910 return;
3911
3912 prev_hlock = NULL;
3913 for (i = depth-1; i >= 0; i--) {
3914 hlock = curr->held_locks + i;
3915 /*
3916 * We must not cross into another context:
3917 */
3918 if (prev_hlock && prev_hlock->irq_context != hlock->irq_context)
3919 break;
bb97a91e 3920 if (match_held_lock(hlock, lock))
f20786ff
PZ
3921 goto found_it;
3922 prev_hlock = hlock;
3923 }
3924 print_lock_contention_bug(curr, lock, ip);
3925 return;
3926
3927found_it:
bb97a91e
PZ
3928 if (hlock->instance != lock)
3929 return;
3930
3365e779 3931 hlock->waittime_stamp = lockstat_clock();
f20786ff 3932
c7e78cff
PZ
3933 contention_point = lock_point(hlock_class(hlock)->contention_point, ip);
3934 contending_point = lock_point(hlock_class(hlock)->contending_point,
3935 lock->ip);
f20786ff 3936
f82b217e 3937 stats = get_lock_stats(hlock_class(hlock));
c7e78cff
PZ
3938 if (contention_point < LOCKSTAT_POINTS)
3939 stats->contention_point[contention_point]++;
3940 if (contending_point < LOCKSTAT_POINTS)
3941 stats->contending_point[contending_point]++;
96645678
PZ
3942 if (lock->cpu != smp_processor_id())
3943 stats->bounces[bounce_contended + !!hlock->read]++;
f20786ff
PZ
3944 put_lock_stats(stats);
3945}
3946
3947static void
c7e78cff 3948__lock_acquired(struct lockdep_map *lock, unsigned long ip)
f20786ff
PZ
3949{
3950 struct task_struct *curr = current;
3951 struct held_lock *hlock, *prev_hlock;
3952 struct lock_class_stats *stats;
3953 unsigned int depth;
3365e779 3954 u64 now, waittime = 0;
96645678 3955 int i, cpu;
f20786ff
PZ
3956
3957 depth = curr->lockdep_depth;
0119fee4
PZ
3958 /*
3959 * Yay, we acquired ownership of this lock we didn't try to
3960 * acquire, how the heck did that happen?
3961 */
f20786ff
PZ
3962 if (DEBUG_LOCKS_WARN_ON(!depth))
3963 return;
3964
3965 prev_hlock = NULL;
3966 for (i = depth-1; i >= 0; i--) {
3967 hlock = curr->held_locks + i;
3968 /*
3969 * We must not cross into another context:
3970 */
3971 if (prev_hlock && prev_hlock->irq_context != hlock->irq_context)
3972 break;
bb97a91e 3973 if (match_held_lock(hlock, lock))
f20786ff
PZ
3974 goto found_it;
3975 prev_hlock = hlock;
3976 }
3977 print_lock_contention_bug(curr, lock, _RET_IP_);
3978 return;
3979
3980found_it:
bb97a91e
PZ
3981 if (hlock->instance != lock)
3982 return;
3983
96645678
PZ
3984 cpu = smp_processor_id();
3985 if (hlock->waittime_stamp) {
3365e779 3986 now = lockstat_clock();
96645678
PZ
3987 waittime = now - hlock->waittime_stamp;
3988 hlock->holdtime_stamp = now;
3989 }
f20786ff 3990
883a2a31 3991 trace_lock_acquired(lock, ip);
2062501a 3992
f82b217e 3993 stats = get_lock_stats(hlock_class(hlock));
96645678
PZ
3994 if (waittime) {
3995 if (hlock->read)
3996 lock_time_inc(&stats->read_waittime, waittime);
3997 else
3998 lock_time_inc(&stats->write_waittime, waittime);
3999 }
4000 if (lock->cpu != cpu)
4001 stats->bounces[bounce_acquired + !!hlock->read]++;
f20786ff 4002 put_lock_stats(stats);
96645678
PZ
4003
4004 lock->cpu = cpu;
c7e78cff 4005 lock->ip = ip;
f20786ff
PZ
4006}
4007
4008void lock_contended(struct lockdep_map *lock, unsigned long ip)
4009{
4010 unsigned long flags;
4011
4012 if (unlikely(!lock_stat))
4013 return;
4014
4015 if (unlikely(current->lockdep_recursion))
4016 return;
4017
4018 raw_local_irq_save(flags);
4019 check_flags(flags);
4020 current->lockdep_recursion = 1;
db2c4c77 4021 trace_lock_contended(lock, ip);
f20786ff
PZ
4022 __lock_contended(lock, ip);
4023 current->lockdep_recursion = 0;
4024 raw_local_irq_restore(flags);
4025}
4026EXPORT_SYMBOL_GPL(lock_contended);
4027
c7e78cff 4028void lock_acquired(struct lockdep_map *lock, unsigned long ip)
f20786ff
PZ
4029{
4030 unsigned long flags;
4031
4032 if (unlikely(!lock_stat))
4033 return;
4034
4035 if (unlikely(current->lockdep_recursion))
4036 return;
4037
4038 raw_local_irq_save(flags);
4039 check_flags(flags);
4040 current->lockdep_recursion = 1;
c7e78cff 4041 __lock_acquired(lock, ip);
f20786ff
PZ
4042 current->lockdep_recursion = 0;
4043 raw_local_irq_restore(flags);
4044}
4045EXPORT_SYMBOL_GPL(lock_acquired);
4046#endif
4047
fbb9ce95
IM
4048/*
4049 * Used by the testsuite, sanitize the validator state
4050 * after a simulated failure:
4051 */
4052
4053void lockdep_reset(void)
4054{
4055 unsigned long flags;
23d95a03 4056 int i;
fbb9ce95
IM
4057
4058 raw_local_irq_save(flags);
4059 current->curr_chain_key = 0;
4060 current->lockdep_depth = 0;
4061 current->lockdep_recursion = 0;
4062 memset(current->held_locks, 0, MAX_LOCK_DEPTH*sizeof(struct held_lock));
4063 nr_hardirq_chains = 0;
4064 nr_softirq_chains = 0;
4065 nr_process_chains = 0;
4066 debug_locks = 1;
23d95a03 4067 for (i = 0; i < CHAINHASH_SIZE; i++)
a63f38cc 4068 INIT_HLIST_HEAD(chainhash_table + i);
fbb9ce95
IM
4069 raw_local_irq_restore(flags);
4070}
4071
4072static void zap_class(struct lock_class *class)
4073{
4074 int i;
4075
4076 /*
4077 * Remove all dependencies this lock is
4078 * involved in:
4079 */
4080 for (i = 0; i < nr_list_entries; i++) {
4081 if (list_entries[i].class == class)
4082 list_del_rcu(&list_entries[i].entry);
4083 }
4084 /*
4085 * Unhash the class and remove it from the all_lock_classes list:
4086 */
a63f38cc 4087 hlist_del_rcu(&class->hash_entry);
fbb9ce95
IM
4088 list_del_rcu(&class->lock_entry);
4089
cee34d88
PZ
4090 RCU_INIT_POINTER(class->key, NULL);
4091 RCU_INIT_POINTER(class->name, NULL);
fbb9ce95
IM
4092}
4093
fabe874a 4094static inline int within(const void *addr, void *start, unsigned long size)
fbb9ce95
IM
4095{
4096 return addr >= start && addr < start + size;
4097}
4098
35a9393c
PZ
4099/*
4100 * Used in module.c to remove lock classes from memory that is going to be
4101 * freed; and possibly re-used by other modules.
4102 *
4103 * We will have had one sync_sched() before getting here, so we're guaranteed
4104 * nobody will look up these exact classes -- they're properly dead but still
4105 * allocated.
4106 */
fbb9ce95
IM
4107void lockdep_free_key_range(void *start, unsigned long size)
4108{
35a9393c 4109 struct lock_class *class;
a63f38cc 4110 struct hlist_head *head;
fbb9ce95
IM
4111 unsigned long flags;
4112 int i;
5a26db5b 4113 int locked;
fbb9ce95
IM
4114
4115 raw_local_irq_save(flags);
5a26db5b 4116 locked = graph_lock();
fbb9ce95
IM
4117
4118 /*
4119 * Unhash all classes that were created by this module:
4120 */
4121 for (i = 0; i < CLASSHASH_SIZE; i++) {
4122 head = classhash_table + i;
a63f38cc 4123 hlist_for_each_entry_rcu(class, head, hash_entry) {
fbb9ce95
IM
4124 if (within(class->key, start, size))
4125 zap_class(class);
fabe874a
AV
4126 else if (within(class->name, start, size))
4127 zap_class(class);
4128 }
fbb9ce95
IM
4129 }
4130
5a26db5b
NP
4131 if (locked)
4132 graph_unlock();
fbb9ce95 4133 raw_local_irq_restore(flags);
35a9393c
PZ
4134
4135 /*
4136 * Wait for any possible iterators from look_up_lock_class() to pass
4137 * before continuing to free the memory they refer to.
4138 *
4139 * sync_sched() is sufficient because the read-side is IRQ disable.
4140 */
4141 synchronize_sched();
4142
4143 /*
4144 * XXX at this point we could return the resources to the pool;
4145 * instead we leak them. We would need to change to bitmap allocators
4146 * instead of the linear allocators we have now.
4147 */
fbb9ce95
IM
4148}
4149
4150void lockdep_reset_lock(struct lockdep_map *lock)
4151{
35a9393c 4152 struct lock_class *class;
a63f38cc 4153 struct hlist_head *head;
fbb9ce95
IM
4154 unsigned long flags;
4155 int i, j;
5a26db5b 4156 int locked;
fbb9ce95
IM
4157
4158 raw_local_irq_save(flags);
fbb9ce95
IM
4159
4160 /*
d6d897ce
IM
4161 * Remove all classes this lock might have:
4162 */
4163 for (j = 0; j < MAX_LOCKDEP_SUBCLASSES; j++) {
4164 /*
4165 * If the class exists we look it up and zap it:
4166 */
4167 class = look_up_lock_class(lock, j);
4168 if (class)
4169 zap_class(class);
4170 }
4171 /*
4172 * Debug check: in the end all mapped classes should
4173 * be gone.
fbb9ce95 4174 */
5a26db5b 4175 locked = graph_lock();
fbb9ce95
IM
4176 for (i = 0; i < CLASSHASH_SIZE; i++) {
4177 head = classhash_table + i;
a63f38cc 4178 hlist_for_each_entry_rcu(class, head, hash_entry) {
62016250
HM
4179 int match = 0;
4180
4181 for (j = 0; j < NR_LOCKDEP_CACHING_CLASSES; j++)
4182 match |= class == lock->class_cache[j];
4183
4184 if (unlikely(match)) {
0119fee4
PZ
4185 if (debug_locks_off_graph_unlock()) {
4186 /*
4187 * We all just reset everything, how did it match?
4188 */
74c383f1 4189 WARN_ON(1);
0119fee4 4190 }
d6d897ce 4191 goto out_restore;
fbb9ce95
IM
4192 }
4193 }
4194 }
5a26db5b
NP
4195 if (locked)
4196 graph_unlock();
d6d897ce
IM
4197
4198out_restore:
fbb9ce95
IM
4199 raw_local_irq_restore(flags);
4200}
4201
fbb9ce95
IM
4202void __init lockdep_info(void)
4203{
4204 printk("Lock dependency validator: Copyright (c) 2006 Red Hat, Inc., Ingo Molnar\n");
4205
b0788caf 4206 printk("... MAX_LOCKDEP_SUBCLASSES: %lu\n", MAX_LOCKDEP_SUBCLASSES);
fbb9ce95
IM
4207 printk("... MAX_LOCK_DEPTH: %lu\n", MAX_LOCK_DEPTH);
4208 printk("... MAX_LOCKDEP_KEYS: %lu\n", MAX_LOCKDEP_KEYS);
b0788caf 4209 printk("... CLASSHASH_SIZE: %lu\n", CLASSHASH_SIZE);
fbb9ce95
IM
4210 printk("... MAX_LOCKDEP_ENTRIES: %lu\n", MAX_LOCKDEP_ENTRIES);
4211 printk("... MAX_LOCKDEP_CHAINS: %lu\n", MAX_LOCKDEP_CHAINS);
4212 printk("... CHAINHASH_SIZE: %lu\n", CHAINHASH_SIZE);
4213
4214 printk(" memory used by lock dependency info: %lu kB\n",
4215 (sizeof(struct lock_class) * MAX_LOCKDEP_KEYS +
4216 sizeof(struct list_head) * CLASSHASH_SIZE +
4217 sizeof(struct lock_list) * MAX_LOCKDEP_ENTRIES +
4218 sizeof(struct lock_chain) * MAX_LOCKDEP_CHAINS +
90629209 4219 sizeof(struct list_head) * CHAINHASH_SIZE
4dd861d6 4220#ifdef CONFIG_PROVE_LOCKING
e351b660 4221 + sizeof(struct circular_queue)
4dd861d6 4222#endif
90629209 4223 ) / 1024
4dd861d6 4224 );
fbb9ce95
IM
4225
4226 printk(" per task-struct memory footprint: %lu bytes\n",
4227 sizeof(struct held_lock) * MAX_LOCK_DEPTH);
fbb9ce95
IM
4228}
4229
fbb9ce95
IM
4230static void
4231print_freed_lock_bug(struct task_struct *curr, const void *mem_from,
55794a41 4232 const void *mem_to, struct held_lock *hlock)
fbb9ce95
IM
4233{
4234 if (!debug_locks_off())
4235 return;
4236 if (debug_locks_silent)
4237 return;
4238
b3fbab05
PM
4239 printk("\n");
4240 printk("=========================\n");
4241 printk("[ BUG: held lock freed! ]\n");
fbdc4b9a 4242 print_kernel_ident();
b3fbab05 4243 printk("-------------------------\n");
fbb9ce95 4244 printk("%s/%d is freeing memory %p-%p, with a lock still held there!\n",
ba25f9dc 4245 curr->comm, task_pid_nr(curr), mem_from, mem_to-1);
55794a41 4246 print_lock(hlock);
fbb9ce95
IM
4247 lockdep_print_held_locks(curr);
4248
4249 printk("\nstack backtrace:\n");
4250 dump_stack();
4251}
4252
54561783
ON
4253static inline int not_in_range(const void* mem_from, unsigned long mem_len,
4254 const void* lock_from, unsigned long lock_len)
4255{
4256 return lock_from + lock_len <= mem_from ||
4257 mem_from + mem_len <= lock_from;
4258}
4259
fbb9ce95
IM
4260/*
4261 * Called when kernel memory is freed (or unmapped), or if a lock
4262 * is destroyed or reinitialized - this code checks whether there is
4263 * any held lock in the memory range of <from> to <to>:
4264 */
4265void debug_check_no_locks_freed(const void *mem_from, unsigned long mem_len)
4266{
fbb9ce95
IM
4267 struct task_struct *curr = current;
4268 struct held_lock *hlock;
4269 unsigned long flags;
4270 int i;
4271
4272 if (unlikely(!debug_locks))
4273 return;
4274
4275 local_irq_save(flags);
4276 for (i = 0; i < curr->lockdep_depth; i++) {
4277 hlock = curr->held_locks + i;
4278
54561783
ON
4279 if (not_in_range(mem_from, mem_len, hlock->instance,
4280 sizeof(*hlock->instance)))
fbb9ce95
IM
4281 continue;
4282
54561783 4283 print_freed_lock_bug(curr, mem_from, mem_from + mem_len, hlock);
fbb9ce95
IM
4284 break;
4285 }
4286 local_irq_restore(flags);
4287}
ed07536e 4288EXPORT_SYMBOL_GPL(debug_check_no_locks_freed);
fbb9ce95 4289
1b1d2fb4 4290static void print_held_locks_bug(void)
fbb9ce95
IM
4291{
4292 if (!debug_locks_off())
4293 return;
4294 if (debug_locks_silent)
4295 return;
4296
b3fbab05
PM
4297 printk("\n");
4298 printk("=====================================\n");
1b1d2fb4
CC
4299 printk("[ BUG: %s/%d still has locks held! ]\n",
4300 current->comm, task_pid_nr(current));
fbdc4b9a 4301 print_kernel_ident();
b3fbab05 4302 printk("-------------------------------------\n");
1b1d2fb4 4303 lockdep_print_held_locks(current);
fbb9ce95
IM
4304 printk("\nstack backtrace:\n");
4305 dump_stack();
4306}
4307
1b1d2fb4 4308void debug_check_no_locks_held(void)
fbb9ce95 4309{
1b1d2fb4
CC
4310 if (unlikely(current->lockdep_depth > 0))
4311 print_held_locks_bug();
fbb9ce95 4312}
1b1d2fb4 4313EXPORT_SYMBOL_GPL(debug_check_no_locks_held);
fbb9ce95 4314
8dce7a9a 4315#ifdef __KERNEL__
fbb9ce95
IM
4316void debug_show_all_locks(void)
4317{
4318 struct task_struct *g, *p;
4319 int count = 10;
4320 int unlock = 1;
4321
9c35dd7f
JP
4322 if (unlikely(!debug_locks)) {
4323 printk("INFO: lockdep is turned off.\n");
4324 return;
4325 }
fbb9ce95
IM
4326 printk("\nShowing all locks held in the system:\n");
4327
4328 /*
4329 * Here we try to get the tasklist_lock as hard as possible,
4330 * if not successful after 2 seconds we ignore it (but keep
4331 * trying). This is to enable a debug printout even if a
4332 * tasklist_lock-holding task deadlocks or crashes.
4333 */
4334retry:
4335 if (!read_trylock(&tasklist_lock)) {
4336 if (count == 10)
4337 printk("hm, tasklist_lock locked, retrying... ");
4338 if (count) {
4339 count--;
4340 printk(" #%d", 10-count);
4341 mdelay(200);
4342 goto retry;
4343 }
4344 printk(" ignoring it.\n");
4345 unlock = 0;
46fec7ac 4346 } else {
4347 if (count != 10)
4348 printk(KERN_CONT " locked it.\n");
fbb9ce95 4349 }
fbb9ce95
IM
4350
4351 do_each_thread(g, p) {
85684873
IM
4352 /*
4353 * It's not reliable to print a task's held locks
4354 * if it's not sleeping (or if it's not the current
4355 * task):
4356 */
4357 if (p->state == TASK_RUNNING && p != current)
4358 continue;
fbb9ce95
IM
4359 if (p->lockdep_depth)
4360 lockdep_print_held_locks(p);
4361 if (!unlock)
4362 if (read_trylock(&tasklist_lock))
4363 unlock = 1;
4364 } while_each_thread(g, p);
4365
4366 printk("\n");
4367 printk("=============================================\n\n");
4368
4369 if (unlock)
4370 read_unlock(&tasklist_lock);
4371}
fbb9ce95 4372EXPORT_SYMBOL_GPL(debug_show_all_locks);
8dce7a9a 4373#endif
fbb9ce95 4374
82a1fcb9
IM
4375/*
4376 * Careful: only use this function if you are sure that
4377 * the task cannot run in parallel!
4378 */
f1b499f0 4379void debug_show_held_locks(struct task_struct *task)
fbb9ce95 4380{
9c35dd7f
JP
4381 if (unlikely(!debug_locks)) {
4382 printk("INFO: lockdep is turned off.\n");
4383 return;
4384 }
fbb9ce95
IM
4385 lockdep_print_held_locks(task);
4386}
fbb9ce95 4387EXPORT_SYMBOL_GPL(debug_show_held_locks);
b351d164 4388
722a9f92 4389asmlinkage __visible void lockdep_sys_exit(void)
b351d164
PZ
4390{
4391 struct task_struct *curr = current;
4392
4393 if (unlikely(curr->lockdep_depth)) {
4394 if (!debug_locks_off())
4395 return;
b3fbab05
PM
4396 printk("\n");
4397 printk("================================================\n");
4398 printk("[ BUG: lock held when returning to user space! ]\n");
fbdc4b9a 4399 print_kernel_ident();
b3fbab05 4400 printk("------------------------------------------------\n");
b351d164
PZ
4401 printk("%s/%d is leaving the kernel with locks still held!\n",
4402 curr->comm, curr->pid);
4403 lockdep_print_held_locks(curr);
4404 }
4405}
0632eb3d 4406
b3fbab05 4407void lockdep_rcu_suspicious(const char *file, const int line, const char *s)
0632eb3d
PM
4408{
4409 struct task_struct *curr = current;
4410
2b3fc35f 4411#ifndef CONFIG_PROVE_RCU_REPEATEDLY
0632eb3d
PM
4412 if (!debug_locks_off())
4413 return;
2b3fc35f
LJ
4414#endif /* #ifdef CONFIG_PROVE_RCU_REPEATEDLY */
4415 /* Note: the following can be executed concurrently, so be careful. */
b3fbab05 4416 printk("\n");
4d4f88fa
PM
4417 pr_err("===============================\n");
4418 pr_err("[ ERR: suspicious RCU usage. ]\n");
fbdc4b9a 4419 print_kernel_ident();
4d4f88fa
PM
4420 pr_err("-------------------------------\n");
4421 pr_err("%s:%d %s!\n", file, line, s);
4422 pr_err("\nother info that might help us debug this:\n\n");
4423 pr_err("\n%srcu_scheduler_active = %d, debug_locks = %d\n",
c5fdcec9
PM
4424 !rcu_lockdep_current_cpu_online()
4425 ? "RCU used illegally from offline CPU!\n"
5c173eb8 4426 : !rcu_is_watching()
c5fdcec9
PM
4427 ? "RCU used illegally from idle CPU!\n"
4428 : "",
4429 rcu_scheduler_active, debug_locks);
0464e937
FW
4430
4431 /*
4432 * If a CPU is in the RCU-free window in idle (ie: in the section
4433 * between rcu_idle_enter() and rcu_idle_exit(), then RCU
4434 * considers that CPU to be in an "extended quiescent state",
4435 * which means that RCU will be completely ignoring that CPU.
4436 * Therefore, rcu_read_lock() and friends have absolutely no
4437 * effect on a CPU running in that state. In other words, even if
4438 * such an RCU-idle CPU has called rcu_read_lock(), RCU might well
4439 * delete data structures out from under it. RCU really has no
4440 * choice here: we need to keep an RCU-free window in idle where
4441 * the CPU may possibly enter into low power mode. This way we can
4442 * notice an extended quiescent state to other CPUs that started a grace
4443 * period. Otherwise we would delay any grace period as long as we run
4444 * in the idle task.
4445 *
4446 * So complain bitterly if someone does call rcu_read_lock(),
4447 * rcu_read_lock_bh() and so on from extended quiescent states.
4448 */
5c173eb8 4449 if (!rcu_is_watching())
0464e937
FW
4450 printk("RCU used illegally from extended quiescent state!\n");
4451
0632eb3d
PM
4452 lockdep_print_held_locks(curr);
4453 printk("\nstack backtrace:\n");
4454 dump_stack();
4455}
b3fbab05 4456EXPORT_SYMBOL_GPL(lockdep_rcu_suspicious);