]> bbs.cooldavid.org Git - net-next-2.6.git/blame - kernel/lockdep.c
lockdep: sanitise CONFIG_PROVE_LOCKING
[net-next-2.6.git] / kernel / lockdep.c
CommitLineData
fbb9ce95
IM
1/*
2 * kernel/lockdep.c
3 *
4 * Runtime locking correctness validator
5 *
6 * Started by Ingo Molnar:
7 *
8 * Copyright (C) 2006 Red Hat, Inc., Ingo Molnar <mingo@redhat.com>
9 *
10 * this code maps all the lock dependencies as they occur in a live kernel
11 * and will warn about the following classes of locking bugs:
12 *
13 * - lock inversion scenarios
14 * - circular lock dependencies
15 * - hardirq/softirq safe/unsafe locking bugs
16 *
17 * Bugs are reported even if the current locking scenario does not cause
18 * any deadlock at this point.
19 *
20 * I.e. if anytime in the past two locks were taken in a different order,
21 * even if it happened for another task, even if those were different
22 * locks (but of the same class as this lock), this code will detect it.
23 *
24 * Thanks to Arjan van de Ven for coming up with the initial idea of
25 * mapping lock dependencies runtime.
26 */
27#include <linux/mutex.h>
28#include <linux/sched.h>
29#include <linux/delay.h>
30#include <linux/module.h>
31#include <linux/proc_fs.h>
32#include <linux/seq_file.h>
33#include <linux/spinlock.h>
34#include <linux/kallsyms.h>
35#include <linux/interrupt.h>
36#include <linux/stacktrace.h>
37#include <linux/debug_locks.h>
38#include <linux/irqflags.h>
99de055a 39#include <linux/utsname.h>
fbb9ce95
IM
40
41#include <asm/sections.h>
42
43#include "lockdep_internals.h"
44
45/*
74c383f1
IM
46 * lockdep_lock: protects the lockdep graph, the hashes and the
47 * class/list/hash allocators.
fbb9ce95
IM
48 *
49 * This is one of the rare exceptions where it's justified
50 * to use a raw spinlock - we really dont want the spinlock
74c383f1 51 * code to recurse back into the lockdep code...
fbb9ce95 52 */
74c383f1
IM
53static raw_spinlock_t lockdep_lock = (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED;
54
55static int graph_lock(void)
56{
57 __raw_spin_lock(&lockdep_lock);
58 /*
59 * Make sure that if another CPU detected a bug while
60 * walking the graph we dont change it (while the other
61 * CPU is busy printing out stuff with the graph lock
62 * dropped already)
63 */
64 if (!debug_locks) {
65 __raw_spin_unlock(&lockdep_lock);
66 return 0;
67 }
68 return 1;
69}
70
71static inline int graph_unlock(void)
72{
381a2292
JP
73 if (debug_locks && !__raw_spin_is_locked(&lockdep_lock))
74 return DEBUG_LOCKS_WARN_ON(1);
75
74c383f1
IM
76 __raw_spin_unlock(&lockdep_lock);
77 return 0;
78}
79
80/*
81 * Turn lock debugging off and return with 0 if it was off already,
82 * and also release the graph lock:
83 */
84static inline int debug_locks_off_graph_unlock(void)
85{
86 int ret = debug_locks_off();
87
88 __raw_spin_unlock(&lockdep_lock);
89
90 return ret;
91}
fbb9ce95
IM
92
93static int lockdep_initialized;
94
95unsigned long nr_list_entries;
96static struct lock_list list_entries[MAX_LOCKDEP_ENTRIES];
97
ca58abcb 98#ifdef CONFIG_PROVE_LOCKING
fbb9ce95 99/*
74c383f1 100 * Allocate a lockdep entry. (assumes the graph_lock held, returns
fbb9ce95
IM
101 * with NULL on failure)
102 */
103static struct lock_list *alloc_list_entry(void)
104{
105 if (nr_list_entries >= MAX_LOCKDEP_ENTRIES) {
74c383f1
IM
106 if (!debug_locks_off_graph_unlock())
107 return NULL;
108
fbb9ce95
IM
109 printk("BUG: MAX_LOCKDEP_ENTRIES too low!\n");
110 printk("turning off the locking correctness validator.\n");
111 return NULL;
112 }
113 return list_entries + nr_list_entries++;
114}
ca58abcb 115#endif
fbb9ce95
IM
116
117/*
118 * All data structures here are protected by the global debug_lock.
119 *
120 * Mutex key structs only get allocated, once during bootup, and never
121 * get freed - this significantly simplifies the debugging code.
122 */
123unsigned long nr_lock_classes;
124static struct lock_class lock_classes[MAX_LOCKDEP_KEYS];
125
126/*
127 * We keep a global list of all lock classes. The list only grows,
128 * never shrinks. The list is only accessed with the lockdep
129 * spinlock lock held.
130 */
131LIST_HEAD(all_lock_classes);
132
133/*
134 * The lockdep classes are in a hash-table as well, for fast lookup:
135 */
136#define CLASSHASH_BITS (MAX_LOCKDEP_KEYS_BITS - 1)
137#define CLASSHASH_SIZE (1UL << CLASSHASH_BITS)
138#define CLASSHASH_MASK (CLASSHASH_SIZE - 1)
139#define __classhashfn(key) ((((unsigned long)key >> CLASSHASH_BITS) + (unsigned long)key) & CLASSHASH_MASK)
140#define classhashentry(key) (classhash_table + __classhashfn((key)))
141
142static struct list_head classhash_table[CLASSHASH_SIZE];
143
144unsigned long nr_lock_chains;
ca58abcb 145#ifdef CONFIG_PROVE_LOCKING
fbb9ce95 146static struct lock_chain lock_chains[MAX_LOCKDEP_CHAINS];
ca58abcb 147#endif
fbb9ce95
IM
148
149/*
150 * We put the lock dependency chains into a hash-table as well, to cache
151 * their existence:
152 */
153#define CHAINHASH_BITS (MAX_LOCKDEP_CHAINS_BITS-1)
154#define CHAINHASH_SIZE (1UL << CHAINHASH_BITS)
155#define CHAINHASH_MASK (CHAINHASH_SIZE - 1)
156#define __chainhashfn(chain) \
157 (((chain >> CHAINHASH_BITS) + chain) & CHAINHASH_MASK)
158#define chainhashentry(chain) (chainhash_table + __chainhashfn((chain)))
159
160static struct list_head chainhash_table[CHAINHASH_SIZE];
161
162/*
163 * The hash key of the lock dependency chains is a hash itself too:
164 * it's a hash of all locks taken up to that lock, including that lock.
165 * It's a 64-bit hash, because it's important for the keys to be
166 * unique.
167 */
168#define iterate_chain_key(key1, key2) \
03cbc358
IM
169 (((key1) << MAX_LOCKDEP_KEYS_BITS) ^ \
170 ((key1) >> (64-MAX_LOCKDEP_KEYS_BITS)) ^ \
fbb9ce95
IM
171 (key2))
172
173void lockdep_off(void)
174{
175 current->lockdep_recursion++;
176}
177
178EXPORT_SYMBOL(lockdep_off);
179
180void lockdep_on(void)
181{
182 current->lockdep_recursion--;
183}
184
185EXPORT_SYMBOL(lockdep_on);
186
fbb9ce95
IM
187/*
188 * Debugging switches:
189 */
190
191#define VERBOSE 0
33e94e96 192#define VERY_VERBOSE 0
fbb9ce95
IM
193
194#if VERBOSE
195# define HARDIRQ_VERBOSE 1
196# define SOFTIRQ_VERBOSE 1
197#else
198# define HARDIRQ_VERBOSE 0
199# define SOFTIRQ_VERBOSE 0
200#endif
201
202#if VERBOSE || HARDIRQ_VERBOSE || SOFTIRQ_VERBOSE
203/*
204 * Quick filtering for interesting events:
205 */
206static int class_filter(struct lock_class *class)
207{
f9829cce
AK
208#if 0
209 /* Example */
fbb9ce95 210 if (class->name_version == 1 &&
f9829cce 211 !strcmp(class->name, "lockname"))
fbb9ce95
IM
212 return 1;
213 if (class->name_version == 1 &&
f9829cce 214 !strcmp(class->name, "&struct->lockfield"))
fbb9ce95 215 return 1;
f9829cce 216#endif
a6640897
IM
217 /* Filter everything else. 1 would be to allow everything else */
218 return 0;
fbb9ce95
IM
219}
220#endif
221
222static int verbose(struct lock_class *class)
223{
224#if VERBOSE
225 return class_filter(class);
226#endif
227 return 0;
228}
229
230#ifdef CONFIG_TRACE_IRQFLAGS
231
232static int hardirq_verbose(struct lock_class *class)
233{
234#if HARDIRQ_VERBOSE
235 return class_filter(class);
236#endif
237 return 0;
238}
239
240static int softirq_verbose(struct lock_class *class)
241{
242#if SOFTIRQ_VERBOSE
243 return class_filter(class);
244#endif
245 return 0;
246}
247
248#endif
249
250/*
251 * Stack-trace: tightly packed array of stack backtrace
74c383f1 252 * addresses. Protected by the graph_lock.
fbb9ce95
IM
253 */
254unsigned long nr_stack_trace_entries;
255static unsigned long stack_trace[MAX_STACK_TRACE_ENTRIES];
256
257static int save_trace(struct stack_trace *trace)
258{
259 trace->nr_entries = 0;
260 trace->max_entries = MAX_STACK_TRACE_ENTRIES - nr_stack_trace_entries;
261 trace->entries = stack_trace + nr_stack_trace_entries;
262
5a1b3999 263 trace->skip = 3;
5a1b3999 264
ab1b6f03 265 save_stack_trace(trace);
fbb9ce95
IM
266
267 trace->max_entries = trace->nr_entries;
268
269 nr_stack_trace_entries += trace->nr_entries;
fbb9ce95
IM
270
271 if (nr_stack_trace_entries == MAX_STACK_TRACE_ENTRIES) {
74c383f1
IM
272 if (!debug_locks_off_graph_unlock())
273 return 0;
274
275 printk("BUG: MAX_STACK_TRACE_ENTRIES too low!\n");
276 printk("turning off the locking correctness validator.\n");
277 dump_stack();
278
fbb9ce95
IM
279 return 0;
280 }
281
282 return 1;
283}
284
285unsigned int nr_hardirq_chains;
286unsigned int nr_softirq_chains;
287unsigned int nr_process_chains;
288unsigned int max_lockdep_depth;
289unsigned int max_recursion_depth;
290
291#ifdef CONFIG_DEBUG_LOCKDEP
292/*
293 * We cannot printk in early bootup code. Not even early_printk()
294 * might work. So we mark any initialization errors and printk
295 * about it later on, in lockdep_info().
296 */
297static int lockdep_init_error;
298
299/*
300 * Various lockdep statistics:
301 */
302atomic_t chain_lookup_hits;
303atomic_t chain_lookup_misses;
304atomic_t hardirqs_on_events;
305atomic_t hardirqs_off_events;
306atomic_t redundant_hardirqs_on;
307atomic_t redundant_hardirqs_off;
308atomic_t softirqs_on_events;
309atomic_t softirqs_off_events;
310atomic_t redundant_softirqs_on;
311atomic_t redundant_softirqs_off;
312atomic_t nr_unused_locks;
313atomic_t nr_cyclic_checks;
314atomic_t nr_cyclic_check_recursions;
315atomic_t nr_find_usage_forwards_checks;
316atomic_t nr_find_usage_forwards_recursions;
317atomic_t nr_find_usage_backwards_checks;
318atomic_t nr_find_usage_backwards_recursions;
319# define debug_atomic_inc(ptr) atomic_inc(ptr)
320# define debug_atomic_dec(ptr) atomic_dec(ptr)
321# define debug_atomic_read(ptr) atomic_read(ptr)
322#else
323# define debug_atomic_inc(ptr) do { } while (0)
324# define debug_atomic_dec(ptr) do { } while (0)
325# define debug_atomic_read(ptr) 0
326#endif
327
328/*
329 * Locking printouts:
330 */
331
332static const char *usage_str[] =
333{
334 [LOCK_USED] = "initial-use ",
335 [LOCK_USED_IN_HARDIRQ] = "in-hardirq-W",
336 [LOCK_USED_IN_SOFTIRQ] = "in-softirq-W",
337 [LOCK_ENABLED_SOFTIRQS] = "softirq-on-W",
338 [LOCK_ENABLED_HARDIRQS] = "hardirq-on-W",
339 [LOCK_USED_IN_HARDIRQ_READ] = "in-hardirq-R",
340 [LOCK_USED_IN_SOFTIRQ_READ] = "in-softirq-R",
341 [LOCK_ENABLED_SOFTIRQS_READ] = "softirq-on-R",
342 [LOCK_ENABLED_HARDIRQS_READ] = "hardirq-on-R",
343};
344
345const char * __get_key_name(struct lockdep_subclass_key *key, char *str)
346{
ffb45122 347 return kallsyms_lookup((unsigned long)key, NULL, NULL, NULL, str);
fbb9ce95
IM
348}
349
350void
351get_usage_chars(struct lock_class *class, char *c1, char *c2, char *c3, char *c4)
352{
353 *c1 = '.', *c2 = '.', *c3 = '.', *c4 = '.';
354
355 if (class->usage_mask & LOCKF_USED_IN_HARDIRQ)
356 *c1 = '+';
357 else
358 if (class->usage_mask & LOCKF_ENABLED_HARDIRQS)
359 *c1 = '-';
360
361 if (class->usage_mask & LOCKF_USED_IN_SOFTIRQ)
362 *c2 = '+';
363 else
364 if (class->usage_mask & LOCKF_ENABLED_SOFTIRQS)
365 *c2 = '-';
366
367 if (class->usage_mask & LOCKF_ENABLED_HARDIRQS_READ)
368 *c3 = '-';
369 if (class->usage_mask & LOCKF_USED_IN_HARDIRQ_READ) {
370 *c3 = '+';
371 if (class->usage_mask & LOCKF_ENABLED_HARDIRQS_READ)
372 *c3 = '?';
373 }
374
375 if (class->usage_mask & LOCKF_ENABLED_SOFTIRQS_READ)
376 *c4 = '-';
377 if (class->usage_mask & LOCKF_USED_IN_SOFTIRQ_READ) {
378 *c4 = '+';
379 if (class->usage_mask & LOCKF_ENABLED_SOFTIRQS_READ)
380 *c4 = '?';
381 }
382}
383
384static void print_lock_name(struct lock_class *class)
385{
9281acea 386 char str[KSYM_NAME_LEN], c1, c2, c3, c4;
fbb9ce95
IM
387 const char *name;
388
389 get_usage_chars(class, &c1, &c2, &c3, &c4);
390
391 name = class->name;
392 if (!name) {
393 name = __get_key_name(class->key, str);
394 printk(" (%s", name);
395 } else {
396 printk(" (%s", name);
397 if (class->name_version > 1)
398 printk("#%d", class->name_version);
399 if (class->subclass)
400 printk("/%d", class->subclass);
401 }
402 printk("){%c%c%c%c}", c1, c2, c3, c4);
403}
404
405static void print_lockdep_cache(struct lockdep_map *lock)
406{
407 const char *name;
9281acea 408 char str[KSYM_NAME_LEN];
fbb9ce95
IM
409
410 name = lock->name;
411 if (!name)
412 name = __get_key_name(lock->key->subkeys, str);
413
414 printk("%s", name);
415}
416
417static void print_lock(struct held_lock *hlock)
418{
419 print_lock_name(hlock->class);
420 printk(", at: ");
421 print_ip_sym(hlock->acquire_ip);
422}
423
424static void lockdep_print_held_locks(struct task_struct *curr)
425{
426 int i, depth = curr->lockdep_depth;
427
428 if (!depth) {
429 printk("no locks held by %s/%d.\n", curr->comm, curr->pid);
430 return;
431 }
432 printk("%d lock%s held by %s/%d:\n",
433 depth, depth > 1 ? "s" : "", curr->comm, curr->pid);
434
435 for (i = 0; i < depth; i++) {
436 printk(" #%d: ", i);
437 print_lock(curr->held_locks + i);
438 }
439}
fbb9ce95
IM
440
441static void print_lock_class_header(struct lock_class *class, int depth)
442{
443 int bit;
444
f9829cce 445 printk("%*s->", depth, "");
fbb9ce95
IM
446 print_lock_name(class);
447 printk(" ops: %lu", class->ops);
448 printk(" {\n");
449
450 for (bit = 0; bit < LOCK_USAGE_STATES; bit++) {
451 if (class->usage_mask & (1 << bit)) {
452 int len = depth;
453
f9829cce 454 len += printk("%*s %s", depth, "", usage_str[bit]);
fbb9ce95
IM
455 len += printk(" at:\n");
456 print_stack_trace(class->usage_traces + bit, len);
457 }
458 }
f9829cce 459 printk("%*s }\n", depth, "");
fbb9ce95 460
f9829cce 461 printk("%*s ... key at: ",depth,"");
fbb9ce95
IM
462 print_ip_sym((unsigned long)class->key);
463}
464
465/*
466 * printk all lock dependencies starting at <entry>:
467 */
468static void print_lock_dependencies(struct lock_class *class, int depth)
469{
470 struct lock_list *entry;
471
472 if (DEBUG_LOCKS_WARN_ON(depth >= 20))
473 return;
474
475 print_lock_class_header(class, depth);
476
477 list_for_each_entry(entry, &class->locks_after, entry) {
b23984d0
JP
478 if (DEBUG_LOCKS_WARN_ON(!entry->class))
479 return;
480
fbb9ce95
IM
481 print_lock_dependencies(entry->class, depth + 1);
482
f9829cce 483 printk("%*s ... acquired at:\n",depth,"");
fbb9ce95
IM
484 print_stack_trace(&entry->trace, 2);
485 printk("\n");
486 }
487}
488
ca58abcb 489#ifdef CONFIG_PROVE_LOCKING
fbb9ce95
IM
490/*
491 * Add a new dependency to the head of the list:
492 */
493static int add_lock_to_list(struct lock_class *class, struct lock_class *this,
068135e6 494 struct list_head *head, unsigned long ip, int distance)
fbb9ce95
IM
495{
496 struct lock_list *entry;
497 /*
498 * Lock not present yet - get a new dependency struct and
499 * add it to the list:
500 */
501 entry = alloc_list_entry();
502 if (!entry)
503 return 0;
504
505 entry->class = this;
068135e6 506 entry->distance = distance;
910b1b2e
JP
507 if (!save_trace(&entry->trace))
508 return 0;
fbb9ce95
IM
509
510 /*
511 * Since we never remove from the dependency list, the list can
512 * be walked lockless by other CPUs, it's only allocation
513 * that must be protected by the spinlock. But this also means
514 * we must make new entries visible only once writes to the
515 * entry become visible - hence the RCU op:
516 */
517 list_add_tail_rcu(&entry->entry, head);
518
519 return 1;
520}
521
522/*
523 * Recursive, forwards-direction lock-dependency checking, used for
524 * both noncyclic checking and for hardirq-unsafe/softirq-unsafe
525 * checking.
526 *
527 * (to keep the stackframe of the recursive functions small we
528 * use these global variables, and we also mark various helper
529 * functions as noinline.)
530 */
531static struct held_lock *check_source, *check_target;
532
533/*
534 * Print a dependency chain entry (this is only done when a deadlock
535 * has been detected):
536 */
537static noinline int
538print_circular_bug_entry(struct lock_list *target, unsigned int depth)
539{
540 if (debug_locks_silent)
541 return 0;
542 printk("\n-> #%u", depth);
543 print_lock_name(target->class);
544 printk(":\n");
545 print_stack_trace(&target->trace, 6);
546
547 return 0;
548}
ca58abcb 549#endif
fbb9ce95 550
99de055a
DJ
551static void print_kernel_version(void)
552{
96b644bd
SH
553 printk("%s %.*s\n", init_utsname()->release,
554 (int)strcspn(init_utsname()->version, " "),
555 init_utsname()->version);
99de055a
DJ
556}
557
ca58abcb 558#ifdef CONFIG_PROVE_LOCKING
fbb9ce95
IM
559/*
560 * When a circular dependency is detected, print the
561 * header first:
562 */
563static noinline int
564print_circular_bug_header(struct lock_list *entry, unsigned int depth)
565{
566 struct task_struct *curr = current;
567
74c383f1 568 if (!debug_locks_off_graph_unlock() || debug_locks_silent)
fbb9ce95
IM
569 return 0;
570
571 printk("\n=======================================================\n");
572 printk( "[ INFO: possible circular locking dependency detected ]\n");
99de055a 573 print_kernel_version();
fbb9ce95
IM
574 printk( "-------------------------------------------------------\n");
575 printk("%s/%d is trying to acquire lock:\n",
576 curr->comm, curr->pid);
577 print_lock(check_source);
578 printk("\nbut task is already holding lock:\n");
579 print_lock(check_target);
580 printk("\nwhich lock already depends on the new lock.\n\n");
581 printk("\nthe existing dependency chain (in reverse order) is:\n");
582
583 print_circular_bug_entry(entry, depth);
584
585 return 0;
586}
587
588static noinline int print_circular_bug_tail(void)
589{
590 struct task_struct *curr = current;
591 struct lock_list this;
592
593 if (debug_locks_silent)
594 return 0;
595
596 this.class = check_source->class;
910b1b2e
JP
597 if (!save_trace(&this.trace))
598 return 0;
74c383f1 599
fbb9ce95
IM
600 print_circular_bug_entry(&this, 0);
601
602 printk("\nother info that might help us debug this:\n\n");
603 lockdep_print_held_locks(curr);
604
605 printk("\nstack backtrace:\n");
606 dump_stack();
607
608 return 0;
609}
610
ca268c69
IM
611#define RECURSION_LIMIT 40
612
fbb9ce95
IM
613static int noinline print_infinite_recursion_bug(void)
614{
74c383f1
IM
615 if (!debug_locks_off_graph_unlock())
616 return 0;
617
618 WARN_ON(1);
fbb9ce95
IM
619
620 return 0;
621}
622
623/*
624 * Prove that the dependency graph starting at <entry> can not
625 * lead to <target>. Print an error and return 0 if it does.
626 */
627static noinline int
628check_noncircular(struct lock_class *source, unsigned int depth)
629{
630 struct lock_list *entry;
631
632 debug_atomic_inc(&nr_cyclic_check_recursions);
633 if (depth > max_recursion_depth)
634 max_recursion_depth = depth;
ca268c69 635 if (depth >= RECURSION_LIMIT)
fbb9ce95
IM
636 return print_infinite_recursion_bug();
637 /*
638 * Check this lock's dependency list:
639 */
640 list_for_each_entry(entry, &source->locks_after, entry) {
641 if (entry->class == check_target->class)
642 return print_circular_bug_header(entry, depth+1);
643 debug_atomic_inc(&nr_cyclic_checks);
644 if (!check_noncircular(entry->class, depth+1))
645 return print_circular_bug_entry(entry, depth+1);
646 }
647 return 1;
648}
ca58abcb 649#endif
fbb9ce95
IM
650
651static int very_verbose(struct lock_class *class)
652{
653#if VERY_VERBOSE
654 return class_filter(class);
655#endif
656 return 0;
657}
658#ifdef CONFIG_TRACE_IRQFLAGS
659
660/*
661 * Forwards and backwards subgraph searching, for the purposes of
662 * proving that two subgraphs can be connected by a new dependency
663 * without creating any illegal irq-safe -> irq-unsafe lock dependency.
664 */
665static enum lock_usage_bit find_usage_bit;
666static struct lock_class *forwards_match, *backwards_match;
667
668/*
669 * Find a node in the forwards-direction dependency sub-graph starting
670 * at <source> that matches <find_usage_bit>.
671 *
672 * Return 2 if such a node exists in the subgraph, and put that node
673 * into <forwards_match>.
674 *
675 * Return 1 otherwise and keep <forwards_match> unchanged.
676 * Return 0 on error.
677 */
678static noinline int
679find_usage_forwards(struct lock_class *source, unsigned int depth)
680{
681 struct lock_list *entry;
682 int ret;
683
684 if (depth > max_recursion_depth)
685 max_recursion_depth = depth;
ca268c69 686 if (depth >= RECURSION_LIMIT)
fbb9ce95
IM
687 return print_infinite_recursion_bug();
688
689 debug_atomic_inc(&nr_find_usage_forwards_checks);
690 if (source->usage_mask & (1 << find_usage_bit)) {
691 forwards_match = source;
692 return 2;
693 }
694
695 /*
696 * Check this lock's dependency list:
697 */
698 list_for_each_entry(entry, &source->locks_after, entry) {
699 debug_atomic_inc(&nr_find_usage_forwards_recursions);
700 ret = find_usage_forwards(entry->class, depth+1);
701 if (ret == 2 || ret == 0)
702 return ret;
703 }
704 return 1;
705}
706
707/*
708 * Find a node in the backwards-direction dependency sub-graph starting
709 * at <source> that matches <find_usage_bit>.
710 *
711 * Return 2 if such a node exists in the subgraph, and put that node
712 * into <backwards_match>.
713 *
714 * Return 1 otherwise and keep <backwards_match> unchanged.
715 * Return 0 on error.
716 */
717static noinline int
718find_usage_backwards(struct lock_class *source, unsigned int depth)
719{
720 struct lock_list *entry;
721 int ret;
722
381a2292
JP
723 if (!__raw_spin_is_locked(&lockdep_lock))
724 return DEBUG_LOCKS_WARN_ON(1);
725
fbb9ce95
IM
726 if (depth > max_recursion_depth)
727 max_recursion_depth = depth;
ca268c69 728 if (depth >= RECURSION_LIMIT)
fbb9ce95
IM
729 return print_infinite_recursion_bug();
730
731 debug_atomic_inc(&nr_find_usage_backwards_checks);
732 if (source->usage_mask & (1 << find_usage_bit)) {
733 backwards_match = source;
734 return 2;
735 }
736
737 /*
738 * Check this lock's dependency list:
739 */
740 list_for_each_entry(entry, &source->locks_before, entry) {
741 debug_atomic_inc(&nr_find_usage_backwards_recursions);
742 ret = find_usage_backwards(entry->class, depth+1);
743 if (ret == 2 || ret == 0)
744 return ret;
745 }
746 return 1;
747}
748
749static int
750print_bad_irq_dependency(struct task_struct *curr,
751 struct held_lock *prev,
752 struct held_lock *next,
753 enum lock_usage_bit bit1,
754 enum lock_usage_bit bit2,
755 const char *irqclass)
756{
74c383f1 757 if (!debug_locks_off_graph_unlock() || debug_locks_silent)
fbb9ce95
IM
758 return 0;
759
760 printk("\n======================================================\n");
761 printk( "[ INFO: %s-safe -> %s-unsafe lock order detected ]\n",
762 irqclass, irqclass);
99de055a 763 print_kernel_version();
fbb9ce95
IM
764 printk( "------------------------------------------------------\n");
765 printk("%s/%d [HC%u[%lu]:SC%u[%lu]:HE%u:SE%u] is trying to acquire:\n",
766 curr->comm, curr->pid,
767 curr->hardirq_context, hardirq_count() >> HARDIRQ_SHIFT,
768 curr->softirq_context, softirq_count() >> SOFTIRQ_SHIFT,
769 curr->hardirqs_enabled,
770 curr->softirqs_enabled);
771 print_lock(next);
772
773 printk("\nand this task is already holding:\n");
774 print_lock(prev);
775 printk("which would create a new lock dependency:\n");
776 print_lock_name(prev->class);
777 printk(" ->");
778 print_lock_name(next->class);
779 printk("\n");
780
781 printk("\nbut this new dependency connects a %s-irq-safe lock:\n",
782 irqclass);
783 print_lock_name(backwards_match);
784 printk("\n... which became %s-irq-safe at:\n", irqclass);
785
786 print_stack_trace(backwards_match->usage_traces + bit1, 1);
787
788 printk("\nto a %s-irq-unsafe lock:\n", irqclass);
789 print_lock_name(forwards_match);
790 printk("\n... which became %s-irq-unsafe at:\n", irqclass);
791 printk("...");
792
793 print_stack_trace(forwards_match->usage_traces + bit2, 1);
794
795 printk("\nother info that might help us debug this:\n\n");
796 lockdep_print_held_locks(curr);
797
798 printk("\nthe %s-irq-safe lock's dependencies:\n", irqclass);
799 print_lock_dependencies(backwards_match, 0);
800
801 printk("\nthe %s-irq-unsafe lock's dependencies:\n", irqclass);
802 print_lock_dependencies(forwards_match, 0);
803
804 printk("\nstack backtrace:\n");
805 dump_stack();
806
807 return 0;
808}
809
810static int
811check_usage(struct task_struct *curr, struct held_lock *prev,
812 struct held_lock *next, enum lock_usage_bit bit_backwards,
813 enum lock_usage_bit bit_forwards, const char *irqclass)
814{
815 int ret;
816
817 find_usage_bit = bit_backwards;
818 /* fills in <backwards_match> */
819 ret = find_usage_backwards(prev->class, 0);
820 if (!ret || ret == 1)
821 return ret;
822
823 find_usage_bit = bit_forwards;
824 ret = find_usage_forwards(next->class, 0);
825 if (!ret || ret == 1)
826 return ret;
827 /* ret == 2 */
828 return print_bad_irq_dependency(curr, prev, next,
829 bit_backwards, bit_forwards, irqclass);
830}
831
832#endif
833
ca58abcb 834#ifdef CONFIG_PROVE_LOCKING
fbb9ce95
IM
835static int
836print_deadlock_bug(struct task_struct *curr, struct held_lock *prev,
837 struct held_lock *next)
838{
74c383f1 839 if (!debug_locks_off_graph_unlock() || debug_locks_silent)
fbb9ce95
IM
840 return 0;
841
842 printk("\n=============================================\n");
843 printk( "[ INFO: possible recursive locking detected ]\n");
99de055a 844 print_kernel_version();
fbb9ce95
IM
845 printk( "---------------------------------------------\n");
846 printk("%s/%d is trying to acquire lock:\n",
847 curr->comm, curr->pid);
848 print_lock(next);
849 printk("\nbut task is already holding lock:\n");
850 print_lock(prev);
851
852 printk("\nother info that might help us debug this:\n");
853 lockdep_print_held_locks(curr);
854
855 printk("\nstack backtrace:\n");
856 dump_stack();
857
858 return 0;
859}
860
861/*
862 * Check whether we are holding such a class already.
863 *
864 * (Note that this has to be done separately, because the graph cannot
865 * detect such classes of deadlocks.)
866 *
867 * Returns: 0 on deadlock detected, 1 on OK, 2 on recursive read
868 */
869static int
870check_deadlock(struct task_struct *curr, struct held_lock *next,
871 struct lockdep_map *next_instance, int read)
872{
873 struct held_lock *prev;
874 int i;
875
876 for (i = 0; i < curr->lockdep_depth; i++) {
877 prev = curr->held_locks + i;
878 if (prev->class != next->class)
879 continue;
880 /*
881 * Allow read-after-read recursion of the same
6c9076ec 882 * lock class (i.e. read_lock(lock)+read_lock(lock)):
fbb9ce95 883 */
6c9076ec 884 if ((read == 2) && prev->read)
fbb9ce95
IM
885 return 2;
886 return print_deadlock_bug(curr, prev, next);
887 }
888 return 1;
889}
890
891/*
892 * There was a chain-cache miss, and we are about to add a new dependency
893 * to a previous lock. We recursively validate the following rules:
894 *
895 * - would the adding of the <prev> -> <next> dependency create a
896 * circular dependency in the graph? [== circular deadlock]
897 *
898 * - does the new prev->next dependency connect any hardirq-safe lock
899 * (in the full backwards-subgraph starting at <prev>) with any
900 * hardirq-unsafe lock (in the full forwards-subgraph starting at
901 * <next>)? [== illegal lock inversion with hardirq contexts]
902 *
903 * - does the new prev->next dependency connect any softirq-safe lock
904 * (in the full backwards-subgraph starting at <prev>) with any
905 * softirq-unsafe lock (in the full forwards-subgraph starting at
906 * <next>)? [== illegal lock inversion with softirq contexts]
907 *
908 * any of these scenarios could lead to a deadlock.
909 *
910 * Then if all the validations pass, we add the forwards and backwards
911 * dependency.
912 */
913static int
914check_prev_add(struct task_struct *curr, struct held_lock *prev,
068135e6 915 struct held_lock *next, int distance)
fbb9ce95
IM
916{
917 struct lock_list *entry;
918 int ret;
919
920 /*
921 * Prove that the new <prev> -> <next> dependency would not
922 * create a circular dependency in the graph. (We do this by
923 * forward-recursing into the graph starting at <next>, and
924 * checking whether we can reach <prev>.)
925 *
926 * We are using global variables to control the recursion, to
927 * keep the stackframe size of the recursive functions low:
928 */
929 check_source = next;
930 check_target = prev;
931 if (!(check_noncircular(next->class, 0)))
932 return print_circular_bug_tail();
933
934#ifdef CONFIG_TRACE_IRQFLAGS
935 /*
936 * Prove that the new dependency does not connect a hardirq-safe
937 * lock with a hardirq-unsafe lock - to achieve this we search
938 * the backwards-subgraph starting at <prev>, and the
939 * forwards-subgraph starting at <next>:
940 */
941 if (!check_usage(curr, prev, next, LOCK_USED_IN_HARDIRQ,
942 LOCK_ENABLED_HARDIRQS, "hard"))
943 return 0;
944
945 /*
946 * Prove that the new dependency does not connect a hardirq-safe-read
947 * lock with a hardirq-unsafe lock - to achieve this we search
948 * the backwards-subgraph starting at <prev>, and the
949 * forwards-subgraph starting at <next>:
950 */
951 if (!check_usage(curr, prev, next, LOCK_USED_IN_HARDIRQ_READ,
952 LOCK_ENABLED_HARDIRQS, "hard-read"))
953 return 0;
954
955 /*
956 * Prove that the new dependency does not connect a softirq-safe
957 * lock with a softirq-unsafe lock - to achieve this we search
958 * the backwards-subgraph starting at <prev>, and the
959 * forwards-subgraph starting at <next>:
960 */
961 if (!check_usage(curr, prev, next, LOCK_USED_IN_SOFTIRQ,
962 LOCK_ENABLED_SOFTIRQS, "soft"))
963 return 0;
964 /*
965 * Prove that the new dependency does not connect a softirq-safe-read
966 * lock with a softirq-unsafe lock - to achieve this we search
967 * the backwards-subgraph starting at <prev>, and the
968 * forwards-subgraph starting at <next>:
969 */
970 if (!check_usage(curr, prev, next, LOCK_USED_IN_SOFTIRQ_READ,
971 LOCK_ENABLED_SOFTIRQS, "soft"))
972 return 0;
973#endif
974 /*
975 * For recursive read-locks we do all the dependency checks,
976 * but we dont store read-triggered dependencies (only
977 * write-triggered dependencies). This ensures that only the
978 * write-side dependencies matter, and that if for example a
979 * write-lock never takes any other locks, then the reads are
980 * equivalent to a NOP.
981 */
982 if (next->read == 2 || prev->read == 2)
983 return 1;
984 /*
985 * Is the <prev> -> <next> dependency already present?
986 *
987 * (this may occur even though this is a new chain: consider
988 * e.g. the L1 -> L2 -> L3 -> L4 and the L5 -> L1 -> L2 -> L3
989 * chains - the second one will be new, but L1 already has
990 * L2 added to its dependency list, due to the first chain.)
991 */
992 list_for_each_entry(entry, &prev->class->locks_after, entry) {
068135e6
JB
993 if (entry->class == next->class) {
994 if (distance == 1)
995 entry->distance = 1;
fbb9ce95 996 return 2;
068135e6 997 }
fbb9ce95
IM
998 }
999
1000 /*
1001 * Ok, all validations passed, add the new lock
1002 * to the previous lock's dependency list:
1003 */
1004 ret = add_lock_to_list(prev->class, next->class,
068135e6
JB
1005 &prev->class->locks_after, next->acquire_ip, distance);
1006
fbb9ce95
IM
1007 if (!ret)
1008 return 0;
910b1b2e 1009
fbb9ce95 1010 ret = add_lock_to_list(next->class, prev->class,
068135e6 1011 &next->class->locks_before, next->acquire_ip, distance);
910b1b2e
JP
1012 if (!ret)
1013 return 0;
fbb9ce95
IM
1014
1015 /*
1016 * Debugging printouts:
1017 */
1018 if (verbose(prev->class) || verbose(next->class)) {
74c383f1 1019 graph_unlock();
fbb9ce95
IM
1020 printk("\n new dependency: ");
1021 print_lock_name(prev->class);
1022 printk(" => ");
1023 print_lock_name(next->class);
1024 printk("\n");
1025 dump_stack();
74c383f1 1026 return graph_lock();
fbb9ce95
IM
1027 }
1028 return 1;
1029}
1030
1031/*
1032 * Add the dependency to all directly-previous locks that are 'relevant'.
1033 * The ones that are relevant are (in increasing distance from curr):
1034 * all consecutive trylock entries and the final non-trylock entry - or
1035 * the end of this context's lock-chain - whichever comes first.
1036 */
1037static int
1038check_prevs_add(struct task_struct *curr, struct held_lock *next)
1039{
1040 int depth = curr->lockdep_depth;
1041 struct held_lock *hlock;
1042
1043 /*
1044 * Debugging checks.
1045 *
1046 * Depth must not be zero for a non-head lock:
1047 */
1048 if (!depth)
1049 goto out_bug;
1050 /*
1051 * At least two relevant locks must exist for this
1052 * to be a head:
1053 */
1054 if (curr->held_locks[depth].irq_context !=
1055 curr->held_locks[depth-1].irq_context)
1056 goto out_bug;
1057
1058 for (;;) {
068135e6 1059 int distance = curr->lockdep_depth - depth + 1;
fbb9ce95
IM
1060 hlock = curr->held_locks + depth-1;
1061 /*
1062 * Only non-recursive-read entries get new dependencies
1063 * added:
1064 */
1065 if (hlock->read != 2) {
068135e6 1066 if (!check_prev_add(curr, hlock, next, distance))
910b1b2e 1067 return 0;
fbb9ce95
IM
1068 /*
1069 * Stop after the first non-trylock entry,
1070 * as non-trylock entries have added their
1071 * own direct dependencies already, so this
1072 * lock is connected to them indirectly:
1073 */
1074 if (!hlock->trylock)
1075 break;
1076 }
1077 depth--;
1078 /*
1079 * End of lock-stack?
1080 */
1081 if (!depth)
1082 break;
1083 /*
1084 * Stop the search if we cross into another context:
1085 */
1086 if (curr->held_locks[depth].irq_context !=
1087 curr->held_locks[depth-1].irq_context)
1088 break;
1089 }
1090 return 1;
1091out_bug:
74c383f1
IM
1092 if (!debug_locks_off_graph_unlock())
1093 return 0;
1094
1095 WARN_ON(1);
fbb9ce95
IM
1096
1097 return 0;
1098}
ca58abcb 1099#endif
fbb9ce95
IM
1100
1101/*
1102 * Is this the address of a static object:
1103 */
1104static int static_obj(void *obj)
1105{
1106 unsigned long start = (unsigned long) &_stext,
1107 end = (unsigned long) &_end,
1108 addr = (unsigned long) obj;
1109#ifdef CONFIG_SMP
1110 int i;
1111#endif
1112
1113 /*
1114 * static variable?
1115 */
1116 if ((addr >= start) && (addr < end))
1117 return 1;
1118
1119#ifdef CONFIG_SMP
1120 /*
1121 * percpu var?
1122 */
1123 for_each_possible_cpu(i) {
1124 start = (unsigned long) &__per_cpu_start + per_cpu_offset(i);
1ff56830
IM
1125 end = (unsigned long) &__per_cpu_start + PERCPU_ENOUGH_ROOM
1126 + per_cpu_offset(i);
fbb9ce95
IM
1127
1128 if ((addr >= start) && (addr < end))
1129 return 1;
1130 }
1131#endif
1132
1133 /*
1134 * module var?
1135 */
1136 return is_module_address(addr);
1137}
1138
1139/*
1140 * To make lock name printouts unique, we calculate a unique
1141 * class->name_version generation counter:
1142 */
1143static int count_matching_names(struct lock_class *new_class)
1144{
1145 struct lock_class *class;
1146 int count = 0;
1147
1148 if (!new_class->name)
1149 return 0;
1150
1151 list_for_each_entry(class, &all_lock_classes, lock_entry) {
1152 if (new_class->key - new_class->subclass == class->key)
1153 return class->name_version;
1154 if (class->name && !strcmp(class->name, new_class->name))
1155 count = max(count, class->name_version);
1156 }
1157
1158 return count + 1;
1159}
1160
fbb9ce95
IM
1161/*
1162 * Register a lock's class in the hash-table, if the class is not present
1163 * yet. Otherwise we look it up. We cache the result in the lock object
1164 * itself, so actual lookup of the hash should be once per lock object.
1165 */
1166static inline struct lock_class *
d6d897ce 1167look_up_lock_class(struct lockdep_map *lock, unsigned int subclass)
fbb9ce95
IM
1168{
1169 struct lockdep_subclass_key *key;
1170 struct list_head *hash_head;
1171 struct lock_class *class;
1172
1173#ifdef CONFIG_DEBUG_LOCKDEP
1174 /*
1175 * If the architecture calls into lockdep before initializing
1176 * the hashes then we'll warn about it later. (we cannot printk
1177 * right now)
1178 */
1179 if (unlikely(!lockdep_initialized)) {
1180 lockdep_init();
1181 lockdep_init_error = 1;
1182 }
1183#endif
1184
1185 /*
1186 * Static locks do not have their class-keys yet - for them the key
1187 * is the lock object itself:
1188 */
1189 if (unlikely(!lock->key))
1190 lock->key = (void *)lock;
1191
1192 /*
1193 * NOTE: the class-key must be unique. For dynamic locks, a static
1194 * lock_class_key variable is passed in through the mutex_init()
1195 * (or spin_lock_init()) call - which acts as the key. For static
1196 * locks we use the lock object itself as the key.
1197 */
3dc3099a 1198 BUILD_BUG_ON(sizeof(struct lock_class_key) > sizeof(struct lock_class));
fbb9ce95
IM
1199
1200 key = lock->key->subkeys + subclass;
1201
1202 hash_head = classhashentry(key);
1203
1204 /*
1205 * We can walk the hash lockfree, because the hash only
1206 * grows, and we are careful when adding entries to the end:
1207 */
1208 list_for_each_entry(class, hash_head, hash_entry)
1209 if (class->key == key)
d6d897ce
IM
1210 return class;
1211
1212 return NULL;
1213}
1214
1215/*
1216 * Register a lock's class in the hash-table, if the class is not present
1217 * yet. Otherwise we look it up. We cache the result in the lock object
1218 * itself, so actual lookup of the hash should be once per lock object.
1219 */
1220static inline struct lock_class *
4dfbb9d8 1221register_lock_class(struct lockdep_map *lock, unsigned int subclass, int force)
d6d897ce
IM
1222{
1223 struct lockdep_subclass_key *key;
1224 struct list_head *hash_head;
1225 struct lock_class *class;
70e45067 1226 unsigned long flags;
d6d897ce
IM
1227
1228 class = look_up_lock_class(lock, subclass);
1229 if (likely(class))
1230 return class;
fbb9ce95
IM
1231
1232 /*
1233 * Debug-check: all keys must be persistent!
1234 */
1235 if (!static_obj(lock->key)) {
1236 debug_locks_off();
1237 printk("INFO: trying to register non-static key.\n");
1238 printk("the code is fine but needs lockdep annotation.\n");
1239 printk("turning off the locking correctness validator.\n");
1240 dump_stack();
1241
1242 return NULL;
1243 }
1244
d6d897ce
IM
1245 key = lock->key->subkeys + subclass;
1246 hash_head = classhashentry(key);
1247
70e45067 1248 raw_local_irq_save(flags);
74c383f1
IM
1249 if (!graph_lock()) {
1250 raw_local_irq_restore(flags);
1251 return NULL;
1252 }
fbb9ce95
IM
1253 /*
1254 * We have to do the hash-walk again, to avoid races
1255 * with another CPU:
1256 */
1257 list_for_each_entry(class, hash_head, hash_entry)
1258 if (class->key == key)
1259 goto out_unlock_set;
1260 /*
1261 * Allocate a new key from the static array, and add it to
1262 * the hash:
1263 */
1264 if (nr_lock_classes >= MAX_LOCKDEP_KEYS) {
74c383f1
IM
1265 if (!debug_locks_off_graph_unlock()) {
1266 raw_local_irq_restore(flags);
1267 return NULL;
1268 }
70e45067 1269 raw_local_irq_restore(flags);
74c383f1 1270
fbb9ce95
IM
1271 printk("BUG: MAX_LOCKDEP_KEYS too low!\n");
1272 printk("turning off the locking correctness validator.\n");
1273 return NULL;
1274 }
1275 class = lock_classes + nr_lock_classes++;
1276 debug_atomic_inc(&nr_unused_locks);
1277 class->key = key;
1278 class->name = lock->name;
1279 class->subclass = subclass;
1280 INIT_LIST_HEAD(&class->lock_entry);
1281 INIT_LIST_HEAD(&class->locks_before);
1282 INIT_LIST_HEAD(&class->locks_after);
1283 class->name_version = count_matching_names(class);
1284 /*
1285 * We use RCU's safe list-add method to make
1286 * parallel walking of the hash-list safe:
1287 */
1288 list_add_tail_rcu(&class->hash_entry, hash_head);
1289
1290 if (verbose(class)) {
74c383f1 1291 graph_unlock();
70e45067 1292 raw_local_irq_restore(flags);
74c383f1 1293
fbb9ce95
IM
1294 printk("\nnew class %p: %s", class->key, class->name);
1295 if (class->name_version > 1)
1296 printk("#%d", class->name_version);
1297 printk("\n");
1298 dump_stack();
74c383f1 1299
70e45067 1300 raw_local_irq_save(flags);
74c383f1
IM
1301 if (!graph_lock()) {
1302 raw_local_irq_restore(flags);
1303 return NULL;
1304 }
fbb9ce95
IM
1305 }
1306out_unlock_set:
74c383f1 1307 graph_unlock();
70e45067 1308 raw_local_irq_restore(flags);
fbb9ce95 1309
4dfbb9d8 1310 if (!subclass || force)
d6d897ce 1311 lock->class_cache = class;
fbb9ce95 1312
381a2292
JP
1313 if (DEBUG_LOCKS_WARN_ON(class->subclass != subclass))
1314 return NULL;
fbb9ce95
IM
1315
1316 return class;
1317}
1318
ca58abcb 1319#ifdef CONFIG_PROVE_LOCKING
fbb9ce95
IM
1320/*
1321 * Look up a dependency chain. If the key is not present yet then
9e860d00
JP
1322 * add it and return 1 - in this case the new dependency chain is
1323 * validated. If the key is already hashed, return 0.
1324 * (On return with 1 graph_lock is held.)
fbb9ce95 1325 */
81fc685a 1326static inline int lookup_chain_cache(u64 chain_key, struct lock_class *class)
fbb9ce95
IM
1327{
1328 struct list_head *hash_head = chainhashentry(chain_key);
1329 struct lock_chain *chain;
1330
381a2292
JP
1331 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
1332 return 0;
fbb9ce95
IM
1333 /*
1334 * We can walk it lock-free, because entries only get added
1335 * to the hash:
1336 */
1337 list_for_each_entry(chain, hash_head, entry) {
1338 if (chain->chain_key == chain_key) {
1339cache_hit:
1340 debug_atomic_inc(&chain_lookup_hits);
81fc685a 1341 if (very_verbose(class))
755cd900
AM
1342 printk("\nhash chain already cached, key: "
1343 "%016Lx tail class: [%p] %s\n",
1344 (unsigned long long)chain_key,
1345 class->key, class->name);
fbb9ce95
IM
1346 return 0;
1347 }
1348 }
81fc685a 1349 if (very_verbose(class))
755cd900
AM
1350 printk("\nnew hash chain, key: %016Lx tail class: [%p] %s\n",
1351 (unsigned long long)chain_key, class->key, class->name);
fbb9ce95
IM
1352 /*
1353 * Allocate a new chain entry from the static array, and add
1354 * it to the hash:
1355 */
74c383f1
IM
1356 if (!graph_lock())
1357 return 0;
fbb9ce95
IM
1358 /*
1359 * We have to walk the chain again locked - to avoid duplicates:
1360 */
1361 list_for_each_entry(chain, hash_head, entry) {
1362 if (chain->chain_key == chain_key) {
74c383f1 1363 graph_unlock();
fbb9ce95
IM
1364 goto cache_hit;
1365 }
1366 }
1367 if (unlikely(nr_lock_chains >= MAX_LOCKDEP_CHAINS)) {
74c383f1
IM
1368 if (!debug_locks_off_graph_unlock())
1369 return 0;
1370
fbb9ce95
IM
1371 printk("BUG: MAX_LOCKDEP_CHAINS too low!\n");
1372 printk("turning off the locking correctness validator.\n");
1373 return 0;
1374 }
1375 chain = lock_chains + nr_lock_chains++;
1376 chain->chain_key = chain_key;
1377 list_add_tail_rcu(&chain->entry, hash_head);
1378 debug_atomic_inc(&chain_lookup_misses);
1379#ifdef CONFIG_TRACE_IRQFLAGS
1380 if (current->hardirq_context)
1381 nr_hardirq_chains++;
1382 else {
1383 if (current->softirq_context)
1384 nr_softirq_chains++;
1385 else
1386 nr_process_chains++;
1387 }
1388#else
1389 nr_process_chains++;
1390#endif
1391
1392 return 1;
1393}
ca58abcb 1394#endif
fbb9ce95
IM
1395
1396/*
1397 * We are building curr_chain_key incrementally, so double-check
1398 * it from scratch, to make sure that it's done correctly:
1399 */
1400static void check_chain_key(struct task_struct *curr)
1401{
1402#ifdef CONFIG_DEBUG_LOCKDEP
1403 struct held_lock *hlock, *prev_hlock = NULL;
1404 unsigned int i, id;
1405 u64 chain_key = 0;
1406
1407 for (i = 0; i < curr->lockdep_depth; i++) {
1408 hlock = curr->held_locks + i;
1409 if (chain_key != hlock->prev_chain_key) {
1410 debug_locks_off();
1411 printk("hm#1, depth: %u [%u], %016Lx != %016Lx\n",
1412 curr->lockdep_depth, i,
1413 (unsigned long long)chain_key,
1414 (unsigned long long)hlock->prev_chain_key);
1415 WARN_ON(1);
1416 return;
1417 }
1418 id = hlock->class - lock_classes;
381a2292
JP
1419 if (DEBUG_LOCKS_WARN_ON(id >= MAX_LOCKDEP_KEYS))
1420 return;
1421
fbb9ce95
IM
1422 if (prev_hlock && (prev_hlock->irq_context !=
1423 hlock->irq_context))
1424 chain_key = 0;
1425 chain_key = iterate_chain_key(chain_key, id);
1426 prev_hlock = hlock;
1427 }
1428 if (chain_key != curr->curr_chain_key) {
1429 debug_locks_off();
1430 printk("hm#2, depth: %u [%u], %016Lx != %016Lx\n",
1431 curr->lockdep_depth, i,
1432 (unsigned long long)chain_key,
1433 (unsigned long long)curr->curr_chain_key);
1434 WARN_ON(1);
1435 }
1436#endif
1437}
1438
1439#ifdef CONFIG_TRACE_IRQFLAGS
1440
1441/*
1442 * print irq inversion bug:
1443 */
1444static int
1445print_irq_inversion_bug(struct task_struct *curr, struct lock_class *other,
1446 struct held_lock *this, int forwards,
1447 const char *irqclass)
1448{
74c383f1 1449 if (!debug_locks_off_graph_unlock() || debug_locks_silent)
fbb9ce95
IM
1450 return 0;
1451
1452 printk("\n=========================================================\n");
1453 printk( "[ INFO: possible irq lock inversion dependency detected ]\n");
99de055a 1454 print_kernel_version();
fbb9ce95
IM
1455 printk( "---------------------------------------------------------\n");
1456 printk("%s/%d just changed the state of lock:\n",
1457 curr->comm, curr->pid);
1458 print_lock(this);
1459 if (forwards)
1460 printk("but this lock took another, %s-irq-unsafe lock in the past:\n", irqclass);
1461 else
1462 printk("but this lock was taken by another, %s-irq-safe lock in the past:\n", irqclass);
1463 print_lock_name(other);
1464 printk("\n\nand interrupts could create inverse lock ordering between them.\n\n");
1465
1466 printk("\nother info that might help us debug this:\n");
1467 lockdep_print_held_locks(curr);
1468
1469 printk("\nthe first lock's dependencies:\n");
1470 print_lock_dependencies(this->class, 0);
1471
1472 printk("\nthe second lock's dependencies:\n");
1473 print_lock_dependencies(other, 0);
1474
1475 printk("\nstack backtrace:\n");
1476 dump_stack();
1477
1478 return 0;
1479}
1480
1481/*
1482 * Prove that in the forwards-direction subgraph starting at <this>
1483 * there is no lock matching <mask>:
1484 */
1485static int
1486check_usage_forwards(struct task_struct *curr, struct held_lock *this,
1487 enum lock_usage_bit bit, const char *irqclass)
1488{
1489 int ret;
1490
1491 find_usage_bit = bit;
1492 /* fills in <forwards_match> */
1493 ret = find_usage_forwards(this->class, 0);
1494 if (!ret || ret == 1)
1495 return ret;
1496
1497 return print_irq_inversion_bug(curr, forwards_match, this, 1, irqclass);
1498}
1499
1500/*
1501 * Prove that in the backwards-direction subgraph starting at <this>
1502 * there is no lock matching <mask>:
1503 */
1504static int
1505check_usage_backwards(struct task_struct *curr, struct held_lock *this,
1506 enum lock_usage_bit bit, const char *irqclass)
1507{
1508 int ret;
1509
1510 find_usage_bit = bit;
1511 /* fills in <backwards_match> */
1512 ret = find_usage_backwards(this->class, 0);
1513 if (!ret || ret == 1)
1514 return ret;
1515
1516 return print_irq_inversion_bug(curr, backwards_match, this, 0, irqclass);
1517}
1518
3117df04 1519void print_irqtrace_events(struct task_struct *curr)
fbb9ce95
IM
1520{
1521 printk("irq event stamp: %u\n", curr->irq_events);
1522 printk("hardirqs last enabled at (%u): ", curr->hardirq_enable_event);
1523 print_ip_sym(curr->hardirq_enable_ip);
1524 printk("hardirqs last disabled at (%u): ", curr->hardirq_disable_event);
1525 print_ip_sym(curr->hardirq_disable_ip);
1526 printk("softirqs last enabled at (%u): ", curr->softirq_enable_event);
1527 print_ip_sym(curr->softirq_enable_ip);
1528 printk("softirqs last disabled at (%u): ", curr->softirq_disable_event);
1529 print_ip_sym(curr->softirq_disable_ip);
1530}
1531
fbb9ce95
IM
1532#endif
1533
1534static int
1535print_usage_bug(struct task_struct *curr, struct held_lock *this,
1536 enum lock_usage_bit prev_bit, enum lock_usage_bit new_bit)
1537{
74c383f1 1538 if (!debug_locks_off_graph_unlock() || debug_locks_silent)
fbb9ce95
IM
1539 return 0;
1540
1541 printk("\n=================================\n");
1542 printk( "[ INFO: inconsistent lock state ]\n");
99de055a 1543 print_kernel_version();
fbb9ce95
IM
1544 printk( "---------------------------------\n");
1545
1546 printk("inconsistent {%s} -> {%s} usage.\n",
1547 usage_str[prev_bit], usage_str[new_bit]);
1548
1549 printk("%s/%d [HC%u[%lu]:SC%u[%lu]:HE%u:SE%u] takes:\n",
1550 curr->comm, curr->pid,
1551 trace_hardirq_context(curr), hardirq_count() >> HARDIRQ_SHIFT,
1552 trace_softirq_context(curr), softirq_count() >> SOFTIRQ_SHIFT,
1553 trace_hardirqs_enabled(curr),
1554 trace_softirqs_enabled(curr));
1555 print_lock(this);
1556
1557 printk("{%s} state was registered at:\n", usage_str[prev_bit]);
1558 print_stack_trace(this->class->usage_traces + prev_bit, 1);
1559
1560 print_irqtrace_events(curr);
1561 printk("\nother info that might help us debug this:\n");
1562 lockdep_print_held_locks(curr);
1563
1564 printk("\nstack backtrace:\n");
1565 dump_stack();
1566
1567 return 0;
1568}
1569
1570/*
1571 * Print out an error if an invalid bit is set:
1572 */
1573static inline int
1574valid_state(struct task_struct *curr, struct held_lock *this,
1575 enum lock_usage_bit new_bit, enum lock_usage_bit bad_bit)
1576{
1577 if (unlikely(this->class->usage_mask & (1 << bad_bit)))
1578 return print_usage_bug(curr, this, bad_bit, new_bit);
1579 return 1;
1580}
1581
1582#define STRICT_READ_CHECKS 1
1583
1584/*
1585 * Mark a lock with a usage bit, and validate the state transition:
1586 */
1587static int mark_lock(struct task_struct *curr, struct held_lock *this,
4ff773bb 1588 enum lock_usage_bit new_bit)
fbb9ce95
IM
1589{
1590 unsigned int new_mask = 1 << new_bit, ret = 1;
1591
1592 /*
1593 * If already set then do not dirty the cacheline,
1594 * nor do any checks:
1595 */
1596 if (likely(this->class->usage_mask & new_mask))
1597 return 1;
1598
74c383f1
IM
1599 if (!graph_lock())
1600 return 0;
fbb9ce95
IM
1601 /*
1602 * Make sure we didnt race:
1603 */
1604 if (unlikely(this->class->usage_mask & new_mask)) {
74c383f1 1605 graph_unlock();
fbb9ce95
IM
1606 return 1;
1607 }
1608
1609 this->class->usage_mask |= new_mask;
1610
fbb9ce95
IM
1611 if (!save_trace(this->class->usage_traces + new_bit))
1612 return 0;
1613
1614 switch (new_bit) {
1615#ifdef CONFIG_TRACE_IRQFLAGS
1616 case LOCK_USED_IN_HARDIRQ:
1617 if (!valid_state(curr, this, new_bit, LOCK_ENABLED_HARDIRQS))
1618 return 0;
1619 if (!valid_state(curr, this, new_bit,
1620 LOCK_ENABLED_HARDIRQS_READ))
1621 return 0;
1622 /*
1623 * just marked it hardirq-safe, check that this lock
1624 * took no hardirq-unsafe lock in the past:
1625 */
1626 if (!check_usage_forwards(curr, this,
1627 LOCK_ENABLED_HARDIRQS, "hard"))
1628 return 0;
1629#if STRICT_READ_CHECKS
1630 /*
1631 * just marked it hardirq-safe, check that this lock
1632 * took no hardirq-unsafe-read lock in the past:
1633 */
1634 if (!check_usage_forwards(curr, this,
1635 LOCK_ENABLED_HARDIRQS_READ, "hard-read"))
1636 return 0;
1637#endif
1638 if (hardirq_verbose(this->class))
1639 ret = 2;
1640 break;
1641 case LOCK_USED_IN_SOFTIRQ:
1642 if (!valid_state(curr, this, new_bit, LOCK_ENABLED_SOFTIRQS))
1643 return 0;
1644 if (!valid_state(curr, this, new_bit,
1645 LOCK_ENABLED_SOFTIRQS_READ))
1646 return 0;
1647 /*
1648 * just marked it softirq-safe, check that this lock
1649 * took no softirq-unsafe lock in the past:
1650 */
1651 if (!check_usage_forwards(curr, this,
1652 LOCK_ENABLED_SOFTIRQS, "soft"))
1653 return 0;
1654#if STRICT_READ_CHECKS
1655 /*
1656 * just marked it softirq-safe, check that this lock
1657 * took no softirq-unsafe-read lock in the past:
1658 */
1659 if (!check_usage_forwards(curr, this,
1660 LOCK_ENABLED_SOFTIRQS_READ, "soft-read"))
1661 return 0;
1662#endif
1663 if (softirq_verbose(this->class))
1664 ret = 2;
1665 break;
1666 case LOCK_USED_IN_HARDIRQ_READ:
1667 if (!valid_state(curr, this, new_bit, LOCK_ENABLED_HARDIRQS))
1668 return 0;
1669 /*
1670 * just marked it hardirq-read-safe, check that this lock
1671 * took no hardirq-unsafe lock in the past:
1672 */
1673 if (!check_usage_forwards(curr, this,
1674 LOCK_ENABLED_HARDIRQS, "hard"))
1675 return 0;
1676 if (hardirq_verbose(this->class))
1677 ret = 2;
1678 break;
1679 case LOCK_USED_IN_SOFTIRQ_READ:
1680 if (!valid_state(curr, this, new_bit, LOCK_ENABLED_SOFTIRQS))
1681 return 0;
1682 /*
1683 * just marked it softirq-read-safe, check that this lock
1684 * took no softirq-unsafe lock in the past:
1685 */
1686 if (!check_usage_forwards(curr, this,
1687 LOCK_ENABLED_SOFTIRQS, "soft"))
1688 return 0;
1689 if (softirq_verbose(this->class))
1690 ret = 2;
1691 break;
1692 case LOCK_ENABLED_HARDIRQS:
1693 if (!valid_state(curr, this, new_bit, LOCK_USED_IN_HARDIRQ))
1694 return 0;
1695 if (!valid_state(curr, this, new_bit,
1696 LOCK_USED_IN_HARDIRQ_READ))
1697 return 0;
1698 /*
1699 * just marked it hardirq-unsafe, check that no hardirq-safe
1700 * lock in the system ever took it in the past:
1701 */
1702 if (!check_usage_backwards(curr, this,
1703 LOCK_USED_IN_HARDIRQ, "hard"))
1704 return 0;
1705#if STRICT_READ_CHECKS
1706 /*
1707 * just marked it hardirq-unsafe, check that no
1708 * hardirq-safe-read lock in the system ever took
1709 * it in the past:
1710 */
1711 if (!check_usage_backwards(curr, this,
1712 LOCK_USED_IN_HARDIRQ_READ, "hard-read"))
1713 return 0;
1714#endif
1715 if (hardirq_verbose(this->class))
1716 ret = 2;
1717 break;
1718 case LOCK_ENABLED_SOFTIRQS:
1719 if (!valid_state(curr, this, new_bit, LOCK_USED_IN_SOFTIRQ))
1720 return 0;
1721 if (!valid_state(curr, this, new_bit,
1722 LOCK_USED_IN_SOFTIRQ_READ))
1723 return 0;
1724 /*
1725 * just marked it softirq-unsafe, check that no softirq-safe
1726 * lock in the system ever took it in the past:
1727 */
1728 if (!check_usage_backwards(curr, this,
1729 LOCK_USED_IN_SOFTIRQ, "soft"))
1730 return 0;
1731#if STRICT_READ_CHECKS
1732 /*
1733 * just marked it softirq-unsafe, check that no
1734 * softirq-safe-read lock in the system ever took
1735 * it in the past:
1736 */
1737 if (!check_usage_backwards(curr, this,
1738 LOCK_USED_IN_SOFTIRQ_READ, "soft-read"))
1739 return 0;
1740#endif
1741 if (softirq_verbose(this->class))
1742 ret = 2;
1743 break;
1744 case LOCK_ENABLED_HARDIRQS_READ:
1745 if (!valid_state(curr, this, new_bit, LOCK_USED_IN_HARDIRQ))
1746 return 0;
1747#if STRICT_READ_CHECKS
1748 /*
1749 * just marked it hardirq-read-unsafe, check that no
1750 * hardirq-safe lock in the system ever took it in the past:
1751 */
1752 if (!check_usage_backwards(curr, this,
1753 LOCK_USED_IN_HARDIRQ, "hard"))
1754 return 0;
1755#endif
1756 if (hardirq_verbose(this->class))
1757 ret = 2;
1758 break;
1759 case LOCK_ENABLED_SOFTIRQS_READ:
1760 if (!valid_state(curr, this, new_bit, LOCK_USED_IN_SOFTIRQ))
1761 return 0;
1762#if STRICT_READ_CHECKS
1763 /*
1764 * just marked it softirq-read-unsafe, check that no
1765 * softirq-safe lock in the system ever took it in the past:
1766 */
1767 if (!check_usage_backwards(curr, this,
1768 LOCK_USED_IN_SOFTIRQ, "soft"))
1769 return 0;
1770#endif
1771 if (softirq_verbose(this->class))
1772 ret = 2;
1773 break;
1774#endif
1775 case LOCK_USED:
1776 /*
1777 * Add it to the global list of classes:
1778 */
1779 list_add_tail_rcu(&this->class->lock_entry, &all_lock_classes);
1780 debug_atomic_dec(&nr_unused_locks);
1781 break;
1782 default:
74c383f1
IM
1783 if (!debug_locks_off_graph_unlock())
1784 return 0;
fbb9ce95
IM
1785 WARN_ON(1);
1786 return 0;
1787 }
1788
74c383f1 1789 graph_unlock();
fbb9ce95
IM
1790
1791 /*
74c383f1 1792 * We must printk outside of the graph_lock:
fbb9ce95
IM
1793 */
1794 if (ret == 2) {
1795 printk("\nmarked lock as {%s}:\n", usage_str[new_bit]);
1796 print_lock(this);
1797 print_irqtrace_events(curr);
1798 dump_stack();
1799 }
1800
1801 return ret;
1802}
1803
1804#ifdef CONFIG_TRACE_IRQFLAGS
1805/*
1806 * Mark all held locks with a usage bit:
1807 */
1808static int
4ff773bb 1809mark_held_locks(struct task_struct *curr, int hardirq)
fbb9ce95
IM
1810{
1811 enum lock_usage_bit usage_bit;
1812 struct held_lock *hlock;
1813 int i;
1814
1815 for (i = 0; i < curr->lockdep_depth; i++) {
1816 hlock = curr->held_locks + i;
1817
1818 if (hardirq) {
1819 if (hlock->read)
1820 usage_bit = LOCK_ENABLED_HARDIRQS_READ;
1821 else
1822 usage_bit = LOCK_ENABLED_HARDIRQS;
1823 } else {
1824 if (hlock->read)
1825 usage_bit = LOCK_ENABLED_SOFTIRQS_READ;
1826 else
1827 usage_bit = LOCK_ENABLED_SOFTIRQS;
1828 }
4ff773bb 1829 if (!mark_lock(curr, hlock, usage_bit))
fbb9ce95
IM
1830 return 0;
1831 }
1832
1833 return 1;
1834}
1835
1836/*
1837 * Debugging helper: via this flag we know that we are in
1838 * 'early bootup code', and will warn about any invalid irqs-on event:
1839 */
1840static int early_boot_irqs_enabled;
1841
1842void early_boot_irqs_off(void)
1843{
1844 early_boot_irqs_enabled = 0;
1845}
1846
1847void early_boot_irqs_on(void)
1848{
1849 early_boot_irqs_enabled = 1;
1850}
1851
1852/*
1853 * Hardirqs will be enabled:
1854 */
1855void trace_hardirqs_on(void)
1856{
1857 struct task_struct *curr = current;
1858 unsigned long ip;
1859
1860 if (unlikely(!debug_locks || current->lockdep_recursion))
1861 return;
1862
1863 if (DEBUG_LOCKS_WARN_ON(unlikely(!early_boot_irqs_enabled)))
1864 return;
1865
1866 if (unlikely(curr->hardirqs_enabled)) {
1867 debug_atomic_inc(&redundant_hardirqs_on);
1868 return;
1869 }
1870 /* we'll do an OFF -> ON transition: */
1871 curr->hardirqs_enabled = 1;
1872 ip = (unsigned long) __builtin_return_address(0);
1873
1874 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
1875 return;
1876 if (DEBUG_LOCKS_WARN_ON(current->hardirq_context))
1877 return;
1878 /*
1879 * We are going to turn hardirqs on, so set the
1880 * usage bit for all held locks:
1881 */
4ff773bb 1882 if (!mark_held_locks(curr, 1))
fbb9ce95
IM
1883 return;
1884 /*
1885 * If we have softirqs enabled, then set the usage
1886 * bit for all held locks. (disabled hardirqs prevented
1887 * this bit from being set before)
1888 */
1889 if (curr->softirqs_enabled)
4ff773bb 1890 if (!mark_held_locks(curr, 0))
fbb9ce95
IM
1891 return;
1892
1893 curr->hardirq_enable_ip = ip;
1894 curr->hardirq_enable_event = ++curr->irq_events;
1895 debug_atomic_inc(&hardirqs_on_events);
1896}
1897
1898EXPORT_SYMBOL(trace_hardirqs_on);
1899
1900/*
1901 * Hardirqs were disabled:
1902 */
1903void trace_hardirqs_off(void)
1904{
1905 struct task_struct *curr = current;
1906
1907 if (unlikely(!debug_locks || current->lockdep_recursion))
1908 return;
1909
1910 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
1911 return;
1912
1913 if (curr->hardirqs_enabled) {
1914 /*
1915 * We have done an ON -> OFF transition:
1916 */
1917 curr->hardirqs_enabled = 0;
1918 curr->hardirq_disable_ip = _RET_IP_;
1919 curr->hardirq_disable_event = ++curr->irq_events;
1920 debug_atomic_inc(&hardirqs_off_events);
1921 } else
1922 debug_atomic_inc(&redundant_hardirqs_off);
1923}
1924
1925EXPORT_SYMBOL(trace_hardirqs_off);
1926
1927/*
1928 * Softirqs will be enabled:
1929 */
1930void trace_softirqs_on(unsigned long ip)
1931{
1932 struct task_struct *curr = current;
1933
1934 if (unlikely(!debug_locks))
1935 return;
1936
1937 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
1938 return;
1939
1940 if (curr->softirqs_enabled) {
1941 debug_atomic_inc(&redundant_softirqs_on);
1942 return;
1943 }
1944
1945 /*
1946 * We'll do an OFF -> ON transition:
1947 */
1948 curr->softirqs_enabled = 1;
1949 curr->softirq_enable_ip = ip;
1950 curr->softirq_enable_event = ++curr->irq_events;
1951 debug_atomic_inc(&softirqs_on_events);
1952 /*
1953 * We are going to turn softirqs on, so set the
1954 * usage bit for all held locks, if hardirqs are
1955 * enabled too:
1956 */
1957 if (curr->hardirqs_enabled)
4ff773bb 1958 mark_held_locks(curr, 0);
fbb9ce95
IM
1959}
1960
1961/*
1962 * Softirqs were disabled:
1963 */
1964void trace_softirqs_off(unsigned long ip)
1965{
1966 struct task_struct *curr = current;
1967
1968 if (unlikely(!debug_locks))
1969 return;
1970
1971 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
1972 return;
1973
1974 if (curr->softirqs_enabled) {
1975 /*
1976 * We have done an ON -> OFF transition:
1977 */
1978 curr->softirqs_enabled = 0;
1979 curr->softirq_disable_ip = ip;
1980 curr->softirq_disable_event = ++curr->irq_events;
1981 debug_atomic_inc(&softirqs_off_events);
1982 DEBUG_LOCKS_WARN_ON(!softirq_count());
1983 } else
1984 debug_atomic_inc(&redundant_softirqs_off);
1985}
1986
1987#endif
1988
1989/*
1990 * Initialize a lock instance's lock-class mapping info:
1991 */
1992void lockdep_init_map(struct lockdep_map *lock, const char *name,
4dfbb9d8 1993 struct lock_class_key *key, int subclass)
fbb9ce95
IM
1994{
1995 if (unlikely(!debug_locks))
1996 return;
1997
1998 if (DEBUG_LOCKS_WARN_ON(!key))
1999 return;
2000 if (DEBUG_LOCKS_WARN_ON(!name))
2001 return;
2002 /*
2003 * Sanity check, the lock-class key must be persistent:
2004 */
2005 if (!static_obj(key)) {
2006 printk("BUG: key %p not in .data!\n", key);
2007 DEBUG_LOCKS_WARN_ON(1);
2008 return;
2009 }
2010 lock->name = name;
2011 lock->key = key;
d6d897ce 2012 lock->class_cache = NULL;
4dfbb9d8
PZ
2013 if (subclass)
2014 register_lock_class(lock, subclass, 1);
fbb9ce95
IM
2015}
2016
2017EXPORT_SYMBOL_GPL(lockdep_init_map);
2018
2019/*
2020 * This gets called for every mutex_lock*()/spin_lock*() operation.
2021 * We maintain the dependency maps and validate the locking attempt:
2022 */
2023static int __lock_acquire(struct lockdep_map *lock, unsigned int subclass,
2024 int trylock, int read, int check, int hardirqs_off,
2025 unsigned long ip)
2026{
2027 struct task_struct *curr = current;
d6d897ce 2028 struct lock_class *class = NULL;
fbb9ce95 2029 struct held_lock *hlock;
fbb9ce95
IM
2030 unsigned int depth, id;
2031 int chain_head = 0;
2032 u64 chain_key;
2033
2034 if (unlikely(!debug_locks))
2035 return 0;
2036
2037 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2038 return 0;
2039
2040 if (unlikely(subclass >= MAX_LOCKDEP_SUBCLASSES)) {
2041 debug_locks_off();
2042 printk("BUG: MAX_LOCKDEP_SUBCLASSES too low!\n");
2043 printk("turning off the locking correctness validator.\n");
2044 return 0;
2045 }
2046
d6d897ce
IM
2047 if (!subclass)
2048 class = lock->class_cache;
2049 /*
2050 * Not cached yet or subclass?
2051 */
fbb9ce95 2052 if (unlikely(!class)) {
4dfbb9d8 2053 class = register_lock_class(lock, subclass, 0);
fbb9ce95
IM
2054 if (!class)
2055 return 0;
2056 }
2057 debug_atomic_inc((atomic_t *)&class->ops);
2058 if (very_verbose(class)) {
2059 printk("\nacquire class [%p] %s", class->key, class->name);
2060 if (class->name_version > 1)
2061 printk("#%d", class->name_version);
2062 printk("\n");
2063 dump_stack();
2064 }
2065
2066 /*
2067 * Add the lock to the list of currently held locks.
2068 * (we dont increase the depth just yet, up until the
2069 * dependency checks are done)
2070 */
2071 depth = curr->lockdep_depth;
2072 if (DEBUG_LOCKS_WARN_ON(depth >= MAX_LOCK_DEPTH))
2073 return 0;
2074
2075 hlock = curr->held_locks + depth;
2076
2077 hlock->class = class;
2078 hlock->acquire_ip = ip;
2079 hlock->instance = lock;
2080 hlock->trylock = trylock;
2081 hlock->read = read;
2082 hlock->check = check;
2083 hlock->hardirqs_off = hardirqs_off;
2084
2085 if (check != 2)
2086 goto out_calc_hash;
2087#ifdef CONFIG_TRACE_IRQFLAGS
2088 /*
2089 * If non-trylock use in a hardirq or softirq context, then
2090 * mark the lock as used in these contexts:
2091 */
2092 if (!trylock) {
2093 if (read) {
2094 if (curr->hardirq_context)
2095 if (!mark_lock(curr, hlock,
4ff773bb 2096 LOCK_USED_IN_HARDIRQ_READ))
fbb9ce95
IM
2097 return 0;
2098 if (curr->softirq_context)
2099 if (!mark_lock(curr, hlock,
4ff773bb 2100 LOCK_USED_IN_SOFTIRQ_READ))
fbb9ce95
IM
2101 return 0;
2102 } else {
2103 if (curr->hardirq_context)
4ff773bb 2104 if (!mark_lock(curr, hlock, LOCK_USED_IN_HARDIRQ))
fbb9ce95
IM
2105 return 0;
2106 if (curr->softirq_context)
4ff773bb 2107 if (!mark_lock(curr, hlock, LOCK_USED_IN_SOFTIRQ))
fbb9ce95
IM
2108 return 0;
2109 }
2110 }
2111 if (!hardirqs_off) {
2112 if (read) {
2113 if (!mark_lock(curr, hlock,
4ff773bb 2114 LOCK_ENABLED_HARDIRQS_READ))
fbb9ce95
IM
2115 return 0;
2116 if (curr->softirqs_enabled)
2117 if (!mark_lock(curr, hlock,
4ff773bb 2118 LOCK_ENABLED_SOFTIRQS_READ))
fbb9ce95
IM
2119 return 0;
2120 } else {
2121 if (!mark_lock(curr, hlock,
4ff773bb 2122 LOCK_ENABLED_HARDIRQS))
fbb9ce95
IM
2123 return 0;
2124 if (curr->softirqs_enabled)
2125 if (!mark_lock(curr, hlock,
4ff773bb 2126 LOCK_ENABLED_SOFTIRQS))
fbb9ce95
IM
2127 return 0;
2128 }
2129 }
2130#endif
2131 /* mark it as used: */
4ff773bb 2132 if (!mark_lock(curr, hlock, LOCK_USED))
fbb9ce95
IM
2133 return 0;
2134out_calc_hash:
2135 /*
2136 * Calculate the chain hash: it's the combined has of all the
2137 * lock keys along the dependency chain. We save the hash value
2138 * at every step so that we can get the current hash easily
2139 * after unlock. The chain hash is then used to cache dependency
2140 * results.
2141 *
2142 * The 'key ID' is what is the most compact key value to drive
2143 * the hash, not class->key.
2144 */
2145 id = class - lock_classes;
2146 if (DEBUG_LOCKS_WARN_ON(id >= MAX_LOCKDEP_KEYS))
2147 return 0;
2148
2149 chain_key = curr->curr_chain_key;
2150 if (!depth) {
2151 if (DEBUG_LOCKS_WARN_ON(chain_key != 0))
2152 return 0;
2153 chain_head = 1;
2154 }
2155
2156 hlock->prev_chain_key = chain_key;
2157
2158#ifdef CONFIG_TRACE_IRQFLAGS
2159 /*
2160 * Keep track of points where we cross into an interrupt context:
2161 */
2162 hlock->irq_context = 2*(curr->hardirq_context ? 1 : 0) +
2163 curr->softirq_context;
2164 if (depth) {
2165 struct held_lock *prev_hlock;
2166
2167 prev_hlock = curr->held_locks + depth-1;
2168 /*
2169 * If we cross into another context, reset the
2170 * hash key (this also prevents the checking and the
2171 * adding of the dependency to 'prev'):
2172 */
2173 if (prev_hlock->irq_context != hlock->irq_context) {
2174 chain_key = 0;
2175 chain_head = 1;
2176 }
2177 }
2178#endif
2179 chain_key = iterate_chain_key(chain_key, id);
2180 curr->curr_chain_key = chain_key;
2181
2182 /*
2183 * Trylock needs to maintain the stack of held locks, but it
2184 * does not add new dependencies, because trylock can be done
2185 * in any order.
2186 *
2187 * We look up the chain_key and do the O(N^2) check and update of
2188 * the dependencies only if this is a new dependency chain.
2189 * (If lookup_chain_cache() returns with 1 it acquires
74c383f1 2190 * graph_lock for us)
fbb9ce95 2191 */
81fc685a 2192 if (!trylock && (check == 2) && lookup_chain_cache(chain_key, class)) {
fbb9ce95
IM
2193 /*
2194 * Check whether last held lock:
2195 *
2196 * - is irq-safe, if this lock is irq-unsafe
2197 * - is softirq-safe, if this lock is hardirq-unsafe
2198 *
2199 * And check whether the new lock's dependency graph
2200 * could lead back to the previous lock.
2201 *
2202 * any of these scenarios could lead to a deadlock. If
2203 * All validations
2204 */
2205 int ret = check_deadlock(curr, hlock, lock, read);
2206
2207 if (!ret)
2208 return 0;
2209 /*
2210 * Mark recursive read, as we jump over it when
2211 * building dependencies (just like we jump over
2212 * trylock entries):
2213 */
2214 if (ret == 2)
2215 hlock->read = 2;
2216 /*
2217 * Add dependency only if this lock is not the head
2218 * of the chain, and if it's not a secondary read-lock:
2219 */
2220 if (!chain_head && ret != 2)
2221 if (!check_prevs_add(curr, hlock))
2222 return 0;
74c383f1 2223 graph_unlock();
381a2292
JP
2224 } else
2225 /* after lookup_chain_cache(): */
2226 if (unlikely(!debug_locks))
2227 return 0;
2228
fbb9ce95
IM
2229 curr->lockdep_depth++;
2230 check_chain_key(curr);
60e114d1
JP
2231#ifdef CONFIG_DEBUG_LOCKDEP
2232 if (unlikely(!debug_locks))
2233 return 0;
2234#endif
fbb9ce95
IM
2235 if (unlikely(curr->lockdep_depth >= MAX_LOCK_DEPTH)) {
2236 debug_locks_off();
2237 printk("BUG: MAX_LOCK_DEPTH too low!\n");
2238 printk("turning off the locking correctness validator.\n");
2239 return 0;
2240 }
381a2292 2241
fbb9ce95
IM
2242 if (unlikely(curr->lockdep_depth > max_lockdep_depth))
2243 max_lockdep_depth = curr->lockdep_depth;
2244
2245 return 1;
2246}
2247
2248static int
2249print_unlock_inbalance_bug(struct task_struct *curr, struct lockdep_map *lock,
2250 unsigned long ip)
2251{
2252 if (!debug_locks_off())
2253 return 0;
2254 if (debug_locks_silent)
2255 return 0;
2256
2257 printk("\n=====================================\n");
2258 printk( "[ BUG: bad unlock balance detected! ]\n");
2259 printk( "-------------------------------------\n");
2260 printk("%s/%d is trying to release lock (",
2261 curr->comm, curr->pid);
2262 print_lockdep_cache(lock);
2263 printk(") at:\n");
2264 print_ip_sym(ip);
2265 printk("but there are no more locks to release!\n");
2266 printk("\nother info that might help us debug this:\n");
2267 lockdep_print_held_locks(curr);
2268
2269 printk("\nstack backtrace:\n");
2270 dump_stack();
2271
2272 return 0;
2273}
2274
2275/*
2276 * Common debugging checks for both nested and non-nested unlock:
2277 */
2278static int check_unlock(struct task_struct *curr, struct lockdep_map *lock,
2279 unsigned long ip)
2280{
2281 if (unlikely(!debug_locks))
2282 return 0;
2283 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2284 return 0;
2285
2286 if (curr->lockdep_depth <= 0)
2287 return print_unlock_inbalance_bug(curr, lock, ip);
2288
2289 return 1;
2290}
2291
2292/*
2293 * Remove the lock to the list of currently held locks in a
2294 * potentially non-nested (out of order) manner. This is a
2295 * relatively rare operation, as all the unlock APIs default
2296 * to nested mode (which uses lock_release()):
2297 */
2298static int
2299lock_release_non_nested(struct task_struct *curr,
2300 struct lockdep_map *lock, unsigned long ip)
2301{
2302 struct held_lock *hlock, *prev_hlock;
2303 unsigned int depth;
2304 int i;
2305
2306 /*
2307 * Check whether the lock exists in the current stack
2308 * of held locks:
2309 */
2310 depth = curr->lockdep_depth;
2311 if (DEBUG_LOCKS_WARN_ON(!depth))
2312 return 0;
2313
2314 prev_hlock = NULL;
2315 for (i = depth-1; i >= 0; i--) {
2316 hlock = curr->held_locks + i;
2317 /*
2318 * We must not cross into another context:
2319 */
2320 if (prev_hlock && prev_hlock->irq_context != hlock->irq_context)
2321 break;
2322 if (hlock->instance == lock)
2323 goto found_it;
2324 prev_hlock = hlock;
2325 }
2326 return print_unlock_inbalance_bug(curr, lock, ip);
2327
2328found_it:
2329 /*
2330 * We have the right lock to unlock, 'hlock' points to it.
2331 * Now we remove it from the stack, and add back the other
2332 * entries (if any), recalculating the hash along the way:
2333 */
2334 curr->lockdep_depth = i;
2335 curr->curr_chain_key = hlock->prev_chain_key;
2336
2337 for (i++; i < depth; i++) {
2338 hlock = curr->held_locks + i;
2339 if (!__lock_acquire(hlock->instance,
2340 hlock->class->subclass, hlock->trylock,
2341 hlock->read, hlock->check, hlock->hardirqs_off,
2342 hlock->acquire_ip))
2343 return 0;
2344 }
2345
2346 if (DEBUG_LOCKS_WARN_ON(curr->lockdep_depth != depth - 1))
2347 return 0;
2348 return 1;
2349}
2350
2351/*
2352 * Remove the lock to the list of currently held locks - this gets
2353 * called on mutex_unlock()/spin_unlock*() (or on a failed
2354 * mutex_lock_interruptible()). This is done for unlocks that nest
2355 * perfectly. (i.e. the current top of the lock-stack is unlocked)
2356 */
2357static int lock_release_nested(struct task_struct *curr,
2358 struct lockdep_map *lock, unsigned long ip)
2359{
2360 struct held_lock *hlock;
2361 unsigned int depth;
2362
2363 /*
2364 * Pop off the top of the lock stack:
2365 */
2366 depth = curr->lockdep_depth - 1;
2367 hlock = curr->held_locks + depth;
2368
2369 /*
2370 * Is the unlock non-nested:
2371 */
2372 if (hlock->instance != lock)
2373 return lock_release_non_nested(curr, lock, ip);
2374 curr->lockdep_depth--;
2375
2376 if (DEBUG_LOCKS_WARN_ON(!depth && (hlock->prev_chain_key != 0)))
2377 return 0;
2378
2379 curr->curr_chain_key = hlock->prev_chain_key;
2380
2381#ifdef CONFIG_DEBUG_LOCKDEP
2382 hlock->prev_chain_key = 0;
2383 hlock->class = NULL;
2384 hlock->acquire_ip = 0;
2385 hlock->irq_context = 0;
2386#endif
2387 return 1;
2388}
2389
2390/*
2391 * Remove the lock to the list of currently held locks - this gets
2392 * called on mutex_unlock()/spin_unlock*() (or on a failed
2393 * mutex_lock_interruptible()). This is done for unlocks that nest
2394 * perfectly. (i.e. the current top of the lock-stack is unlocked)
2395 */
2396static void
2397__lock_release(struct lockdep_map *lock, int nested, unsigned long ip)
2398{
2399 struct task_struct *curr = current;
2400
2401 if (!check_unlock(curr, lock, ip))
2402 return;
2403
2404 if (nested) {
2405 if (!lock_release_nested(curr, lock, ip))
2406 return;
2407 } else {
2408 if (!lock_release_non_nested(curr, lock, ip))
2409 return;
2410 }
2411
2412 check_chain_key(curr);
2413}
2414
2415/*
2416 * Check whether we follow the irq-flags state precisely:
2417 */
2418static void check_flags(unsigned long flags)
2419{
2420#if defined(CONFIG_DEBUG_LOCKDEP) && defined(CONFIG_TRACE_IRQFLAGS)
2421 if (!debug_locks)
2422 return;
2423
2424 if (irqs_disabled_flags(flags))
2425 DEBUG_LOCKS_WARN_ON(current->hardirqs_enabled);
2426 else
2427 DEBUG_LOCKS_WARN_ON(!current->hardirqs_enabled);
2428
2429 /*
2430 * We dont accurately track softirq state in e.g.
2431 * hardirq contexts (such as on 4KSTACKS), so only
2432 * check if not in hardirq contexts:
2433 */
2434 if (!hardirq_count()) {
2435 if (softirq_count())
2436 DEBUG_LOCKS_WARN_ON(current->softirqs_enabled);
2437 else
2438 DEBUG_LOCKS_WARN_ON(!current->softirqs_enabled);
2439 }
2440
2441 if (!debug_locks)
2442 print_irqtrace_events(current);
2443#endif
2444}
2445
2446/*
2447 * We are not always called with irqs disabled - do that here,
2448 * and also avoid lockdep recursion:
2449 */
2450void lock_acquire(struct lockdep_map *lock, unsigned int subclass,
2451 int trylock, int read, int check, unsigned long ip)
2452{
2453 unsigned long flags;
2454
2455 if (unlikely(current->lockdep_recursion))
2456 return;
2457
2458 raw_local_irq_save(flags);
2459 check_flags(flags);
2460
2461 current->lockdep_recursion = 1;
2462 __lock_acquire(lock, subclass, trylock, read, check,
2463 irqs_disabled_flags(flags), ip);
2464 current->lockdep_recursion = 0;
2465 raw_local_irq_restore(flags);
2466}
2467
2468EXPORT_SYMBOL_GPL(lock_acquire);
2469
2470void lock_release(struct lockdep_map *lock, int nested, unsigned long ip)
2471{
2472 unsigned long flags;
2473
2474 if (unlikely(current->lockdep_recursion))
2475 return;
2476
2477 raw_local_irq_save(flags);
2478 check_flags(flags);
2479 current->lockdep_recursion = 1;
2480 __lock_release(lock, nested, ip);
2481 current->lockdep_recursion = 0;
2482 raw_local_irq_restore(flags);
2483}
2484
2485EXPORT_SYMBOL_GPL(lock_release);
2486
2487/*
2488 * Used by the testsuite, sanitize the validator state
2489 * after a simulated failure:
2490 */
2491
2492void lockdep_reset(void)
2493{
2494 unsigned long flags;
23d95a03 2495 int i;
fbb9ce95
IM
2496
2497 raw_local_irq_save(flags);
2498 current->curr_chain_key = 0;
2499 current->lockdep_depth = 0;
2500 current->lockdep_recursion = 0;
2501 memset(current->held_locks, 0, MAX_LOCK_DEPTH*sizeof(struct held_lock));
2502 nr_hardirq_chains = 0;
2503 nr_softirq_chains = 0;
2504 nr_process_chains = 0;
2505 debug_locks = 1;
23d95a03
IM
2506 for (i = 0; i < CHAINHASH_SIZE; i++)
2507 INIT_LIST_HEAD(chainhash_table + i);
fbb9ce95
IM
2508 raw_local_irq_restore(flags);
2509}
2510
2511static void zap_class(struct lock_class *class)
2512{
2513 int i;
2514
2515 /*
2516 * Remove all dependencies this lock is
2517 * involved in:
2518 */
2519 for (i = 0; i < nr_list_entries; i++) {
2520 if (list_entries[i].class == class)
2521 list_del_rcu(&list_entries[i].entry);
2522 }
2523 /*
2524 * Unhash the class and remove it from the all_lock_classes list:
2525 */
2526 list_del_rcu(&class->hash_entry);
2527 list_del_rcu(&class->lock_entry);
2528
2529}
2530
2531static inline int within(void *addr, void *start, unsigned long size)
2532{
2533 return addr >= start && addr < start + size;
2534}
2535
2536void lockdep_free_key_range(void *start, unsigned long size)
2537{
2538 struct lock_class *class, *next;
2539 struct list_head *head;
2540 unsigned long flags;
2541 int i;
2542
2543 raw_local_irq_save(flags);
74c383f1 2544 graph_lock();
fbb9ce95
IM
2545
2546 /*
2547 * Unhash all classes that were created by this module:
2548 */
2549 for (i = 0; i < CLASSHASH_SIZE; i++) {
2550 head = classhash_table + i;
2551 if (list_empty(head))
2552 continue;
2553 list_for_each_entry_safe(class, next, head, hash_entry)
2554 if (within(class->key, start, size))
2555 zap_class(class);
2556 }
2557
74c383f1 2558 graph_unlock();
fbb9ce95
IM
2559 raw_local_irq_restore(flags);
2560}
2561
2562void lockdep_reset_lock(struct lockdep_map *lock)
2563{
d6d897ce 2564 struct lock_class *class, *next;
fbb9ce95
IM
2565 struct list_head *head;
2566 unsigned long flags;
2567 int i, j;
2568
2569 raw_local_irq_save(flags);
fbb9ce95
IM
2570
2571 /*
d6d897ce
IM
2572 * Remove all classes this lock might have:
2573 */
2574 for (j = 0; j < MAX_LOCKDEP_SUBCLASSES; j++) {
2575 /*
2576 * If the class exists we look it up and zap it:
2577 */
2578 class = look_up_lock_class(lock, j);
2579 if (class)
2580 zap_class(class);
2581 }
2582 /*
2583 * Debug check: in the end all mapped classes should
2584 * be gone.
fbb9ce95 2585 */
74c383f1 2586 graph_lock();
fbb9ce95
IM
2587 for (i = 0; i < CLASSHASH_SIZE; i++) {
2588 head = classhash_table + i;
2589 if (list_empty(head))
2590 continue;
2591 list_for_each_entry_safe(class, next, head, hash_entry) {
d6d897ce 2592 if (unlikely(class == lock->class_cache)) {
74c383f1
IM
2593 if (debug_locks_off_graph_unlock())
2594 WARN_ON(1);
d6d897ce 2595 goto out_restore;
fbb9ce95
IM
2596 }
2597 }
2598 }
74c383f1 2599 graph_unlock();
d6d897ce
IM
2600
2601out_restore:
fbb9ce95
IM
2602 raw_local_irq_restore(flags);
2603}
2604
1499993c 2605void lockdep_init(void)
fbb9ce95
IM
2606{
2607 int i;
2608
2609 /*
2610 * Some architectures have their own start_kernel()
2611 * code which calls lockdep_init(), while we also
2612 * call lockdep_init() from the start_kernel() itself,
2613 * and we want to initialize the hashes only once:
2614 */
2615 if (lockdep_initialized)
2616 return;
2617
2618 for (i = 0; i < CLASSHASH_SIZE; i++)
2619 INIT_LIST_HEAD(classhash_table + i);
2620
2621 for (i = 0; i < CHAINHASH_SIZE; i++)
2622 INIT_LIST_HEAD(chainhash_table + i);
2623
2624 lockdep_initialized = 1;
2625}
2626
2627void __init lockdep_info(void)
2628{
2629 printk("Lock dependency validator: Copyright (c) 2006 Red Hat, Inc., Ingo Molnar\n");
2630
2631 printk("... MAX_LOCKDEP_SUBCLASSES: %lu\n", MAX_LOCKDEP_SUBCLASSES);
2632 printk("... MAX_LOCK_DEPTH: %lu\n", MAX_LOCK_DEPTH);
2633 printk("... MAX_LOCKDEP_KEYS: %lu\n", MAX_LOCKDEP_KEYS);
2634 printk("... CLASSHASH_SIZE: %lu\n", CLASSHASH_SIZE);
2635 printk("... MAX_LOCKDEP_ENTRIES: %lu\n", MAX_LOCKDEP_ENTRIES);
2636 printk("... MAX_LOCKDEP_CHAINS: %lu\n", MAX_LOCKDEP_CHAINS);
2637 printk("... CHAINHASH_SIZE: %lu\n", CHAINHASH_SIZE);
2638
2639 printk(" memory used by lock dependency info: %lu kB\n",
2640 (sizeof(struct lock_class) * MAX_LOCKDEP_KEYS +
2641 sizeof(struct list_head) * CLASSHASH_SIZE +
2642 sizeof(struct lock_list) * MAX_LOCKDEP_ENTRIES +
2643 sizeof(struct lock_chain) * MAX_LOCKDEP_CHAINS +
2644 sizeof(struct list_head) * CHAINHASH_SIZE) / 1024);
2645
2646 printk(" per task-struct memory footprint: %lu bytes\n",
2647 sizeof(struct held_lock) * MAX_LOCK_DEPTH);
2648
2649#ifdef CONFIG_DEBUG_LOCKDEP
2650 if (lockdep_init_error)
2651 printk("WARNING: lockdep init error! Arch code didnt call lockdep_init() early enough?\n");
2652#endif
2653}
2654
2655static inline int in_range(const void *start, const void *addr, const void *end)
2656{
2657 return addr >= start && addr <= end;
2658}
2659
2660static void
2661print_freed_lock_bug(struct task_struct *curr, const void *mem_from,
55794a41 2662 const void *mem_to, struct held_lock *hlock)
fbb9ce95
IM
2663{
2664 if (!debug_locks_off())
2665 return;
2666 if (debug_locks_silent)
2667 return;
2668
2669 printk("\n=========================\n");
2670 printk( "[ BUG: held lock freed! ]\n");
2671 printk( "-------------------------\n");
2672 printk("%s/%d is freeing memory %p-%p, with a lock still held there!\n",
2673 curr->comm, curr->pid, mem_from, mem_to-1);
55794a41 2674 print_lock(hlock);
fbb9ce95
IM
2675 lockdep_print_held_locks(curr);
2676
2677 printk("\nstack backtrace:\n");
2678 dump_stack();
2679}
2680
2681/*
2682 * Called when kernel memory is freed (or unmapped), or if a lock
2683 * is destroyed or reinitialized - this code checks whether there is
2684 * any held lock in the memory range of <from> to <to>:
2685 */
2686void debug_check_no_locks_freed(const void *mem_from, unsigned long mem_len)
2687{
2688 const void *mem_to = mem_from + mem_len, *lock_from, *lock_to;
2689 struct task_struct *curr = current;
2690 struct held_lock *hlock;
2691 unsigned long flags;
2692 int i;
2693
2694 if (unlikely(!debug_locks))
2695 return;
2696
2697 local_irq_save(flags);
2698 for (i = 0; i < curr->lockdep_depth; i++) {
2699 hlock = curr->held_locks + i;
2700
2701 lock_from = (void *)hlock->instance;
2702 lock_to = (void *)(hlock->instance + 1);
2703
2704 if (!in_range(mem_from, lock_from, mem_to) &&
2705 !in_range(mem_from, lock_to, mem_to))
2706 continue;
2707
55794a41 2708 print_freed_lock_bug(curr, mem_from, mem_to, hlock);
fbb9ce95
IM
2709 break;
2710 }
2711 local_irq_restore(flags);
2712}
ed07536e 2713EXPORT_SYMBOL_GPL(debug_check_no_locks_freed);
fbb9ce95
IM
2714
2715static void print_held_locks_bug(struct task_struct *curr)
2716{
2717 if (!debug_locks_off())
2718 return;
2719 if (debug_locks_silent)
2720 return;
2721
2722 printk("\n=====================================\n");
2723 printk( "[ BUG: lock held at task exit time! ]\n");
2724 printk( "-------------------------------------\n");
2725 printk("%s/%d is exiting with locks still held!\n",
2726 curr->comm, curr->pid);
2727 lockdep_print_held_locks(curr);
2728
2729 printk("\nstack backtrace:\n");
2730 dump_stack();
2731}
2732
2733void debug_check_no_locks_held(struct task_struct *task)
2734{
2735 if (unlikely(task->lockdep_depth > 0))
2736 print_held_locks_bug(task);
2737}
2738
2739void debug_show_all_locks(void)
2740{
2741 struct task_struct *g, *p;
2742 int count = 10;
2743 int unlock = 1;
2744
9c35dd7f
JP
2745 if (unlikely(!debug_locks)) {
2746 printk("INFO: lockdep is turned off.\n");
2747 return;
2748 }
fbb9ce95
IM
2749 printk("\nShowing all locks held in the system:\n");
2750
2751 /*
2752 * Here we try to get the tasklist_lock as hard as possible,
2753 * if not successful after 2 seconds we ignore it (but keep
2754 * trying). This is to enable a debug printout even if a
2755 * tasklist_lock-holding task deadlocks or crashes.
2756 */
2757retry:
2758 if (!read_trylock(&tasklist_lock)) {
2759 if (count == 10)
2760 printk("hm, tasklist_lock locked, retrying... ");
2761 if (count) {
2762 count--;
2763 printk(" #%d", 10-count);
2764 mdelay(200);
2765 goto retry;
2766 }
2767 printk(" ignoring it.\n");
2768 unlock = 0;
2769 }
2770 if (count != 10)
2771 printk(" locked it.\n");
2772
2773 do_each_thread(g, p) {
2774 if (p->lockdep_depth)
2775 lockdep_print_held_locks(p);
2776 if (!unlock)
2777 if (read_trylock(&tasklist_lock))
2778 unlock = 1;
2779 } while_each_thread(g, p);
2780
2781 printk("\n");
2782 printk("=============================================\n\n");
2783
2784 if (unlock)
2785 read_unlock(&tasklist_lock);
2786}
2787
2788EXPORT_SYMBOL_GPL(debug_show_all_locks);
2789
2790void debug_show_held_locks(struct task_struct *task)
2791{
9c35dd7f
JP
2792 if (unlikely(!debug_locks)) {
2793 printk("INFO: lockdep is turned off.\n");
2794 return;
2795 }
fbb9ce95
IM
2796 lockdep_print_held_locks(task);
2797}
2798
2799EXPORT_SYMBOL_GPL(debug_show_held_locks);