4 * Runtime locking correctness validator
6 * Started by Ingo Molnar:
8 * Copyright (C) 2006 Red Hat, Inc., Ingo Molnar <mingo@redhat.com>
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:
13 * - lock inversion scenarios
14 * - circular lock dependencies
15 * - hardirq/softirq safe/unsafe locking bugs
17 * Bugs are reported even if the current locking scenario does not cause
18 * any deadlock at this point.
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.
24 * Thanks to Arjan van de Ven for coming up with the initial idea of
25 * mapping lock dependencies runtime.
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>
39 #include <linux/utsname.h>
41 #include <asm/sections.h>
43 #include "lockdep_internals.h"
46 * hash_lock: protects the lockdep hashes and class/list/hash allocators.
48 * This is one of the rare exceptions where it's justified
49 * to use a raw spinlock - we really dont want the spinlock
50 * code to recurse back into the lockdep code.
52 static raw_spinlock_t hash_lock = (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED;
54 static int lockdep_initialized;
56 unsigned long nr_list_entries;
57 static struct lock_list list_entries[MAX_LOCKDEP_ENTRIES];
60 * Allocate a lockdep entry. (assumes hash_lock held, returns
61 * with NULL on failure)
63 static struct lock_list *alloc_list_entry(void)
65 if (nr_list_entries >= MAX_LOCKDEP_ENTRIES) {
66 __raw_spin_unlock(&hash_lock);
68 printk("BUG: MAX_LOCKDEP_ENTRIES too low!\n");
69 printk("turning off the locking correctness validator.\n");
72 return list_entries + nr_list_entries++;
76 * All data structures here are protected by the global debug_lock.
78 * Mutex key structs only get allocated, once during bootup, and never
79 * get freed - this significantly simplifies the debugging code.
81 unsigned long nr_lock_classes;
82 static struct lock_class lock_classes[MAX_LOCKDEP_KEYS];
85 * We keep a global list of all lock classes. The list only grows,
86 * never shrinks. The list is only accessed with the lockdep
89 LIST_HEAD(all_lock_classes);
92 * The lockdep classes are in a hash-table as well, for fast lookup:
94 #define CLASSHASH_BITS (MAX_LOCKDEP_KEYS_BITS - 1)
95 #define CLASSHASH_SIZE (1UL << CLASSHASH_BITS)
96 #define CLASSHASH_MASK (CLASSHASH_SIZE - 1)
97 #define __classhashfn(key) ((((unsigned long)key >> CLASSHASH_BITS) + (unsigned long)key) & CLASSHASH_MASK)
98 #define classhashentry(key) (classhash_table + __classhashfn((key)))
100 static struct list_head classhash_table[CLASSHASH_SIZE];
102 unsigned long nr_lock_chains;
103 static struct lock_chain lock_chains[MAX_LOCKDEP_CHAINS];
106 * We put the lock dependency chains into a hash-table as well, to cache
109 #define CHAINHASH_BITS (MAX_LOCKDEP_CHAINS_BITS-1)
110 #define CHAINHASH_SIZE (1UL << CHAINHASH_BITS)
111 #define CHAINHASH_MASK (CHAINHASH_SIZE - 1)
112 #define __chainhashfn(chain) \
113 (((chain >> CHAINHASH_BITS) + chain) & CHAINHASH_MASK)
114 #define chainhashentry(chain) (chainhash_table + __chainhashfn((chain)))
116 static struct list_head chainhash_table[CHAINHASH_SIZE];
119 * The hash key of the lock dependency chains is a hash itself too:
120 * it's a hash of all locks taken up to that lock, including that lock.
121 * It's a 64-bit hash, because it's important for the keys to be
124 #define iterate_chain_key(key1, key2) \
125 (((key1) << MAX_LOCKDEP_KEYS_BITS) ^ \
126 ((key1) >> (64-MAX_LOCKDEP_KEYS_BITS)) ^ \
129 void lockdep_off(void)
131 current->lockdep_recursion++;
134 EXPORT_SYMBOL(lockdep_off);
136 void lockdep_on(void)
138 current->lockdep_recursion--;
141 EXPORT_SYMBOL(lockdep_on);
143 int lockdep_internal(void)
145 return current->lockdep_recursion != 0;
148 EXPORT_SYMBOL(lockdep_internal);
151 * Debugging switches:
156 # define VERY_VERBOSE 0
160 # define HARDIRQ_VERBOSE 1
161 # define SOFTIRQ_VERBOSE 1
163 # define HARDIRQ_VERBOSE 0
164 # define SOFTIRQ_VERBOSE 0
167 #if VERBOSE || HARDIRQ_VERBOSE || SOFTIRQ_VERBOSE
169 * Quick filtering for interesting events:
171 static int class_filter(struct lock_class *class)
175 if (class->name_version == 1 &&
176 !strcmp(class->name, "lockname"))
178 if (class->name_version == 1 &&
179 !strcmp(class->name, "&struct->lockfield"))
182 /* Allow everything else. 0 would be filter everything else */
187 static int verbose(struct lock_class *class)
190 return class_filter(class);
195 #ifdef CONFIG_TRACE_IRQFLAGS
197 static int hardirq_verbose(struct lock_class *class)
200 return class_filter(class);
205 static int softirq_verbose(struct lock_class *class)
208 return class_filter(class);
216 * Stack-trace: tightly packed array of stack backtrace
217 * addresses. Protected by the hash_lock.
219 unsigned long nr_stack_trace_entries;
220 static unsigned long stack_trace[MAX_STACK_TRACE_ENTRIES];
222 static int save_trace(struct stack_trace *trace)
224 trace->nr_entries = 0;
225 trace->max_entries = MAX_STACK_TRACE_ENTRIES - nr_stack_trace_entries;
226 trace->entries = stack_trace + nr_stack_trace_entries;
229 trace->all_contexts = 0;
231 /* Make sure to not recurse in case the the unwinder needs to tak
234 save_stack_trace(trace, NULL);
237 trace->max_entries = trace->nr_entries;
239 nr_stack_trace_entries += trace->nr_entries;
240 if (DEBUG_LOCKS_WARN_ON(nr_stack_trace_entries > MAX_STACK_TRACE_ENTRIES)) {
241 __raw_spin_unlock(&hash_lock);
245 if (nr_stack_trace_entries == MAX_STACK_TRACE_ENTRIES) {
246 __raw_spin_unlock(&hash_lock);
247 if (debug_locks_off()) {
248 printk("BUG: MAX_STACK_TRACE_ENTRIES too low!\n");
249 printk("turning off the locking correctness validator.\n");
258 unsigned int nr_hardirq_chains;
259 unsigned int nr_softirq_chains;
260 unsigned int nr_process_chains;
261 unsigned int max_lockdep_depth;
262 unsigned int max_recursion_depth;
264 #ifdef CONFIG_DEBUG_LOCKDEP
266 * We cannot printk in early bootup code. Not even early_printk()
267 * might work. So we mark any initialization errors and printk
268 * about it later on, in lockdep_info().
270 static int lockdep_init_error;
273 * Various lockdep statistics:
275 atomic_t chain_lookup_hits;
276 atomic_t chain_lookup_misses;
277 atomic_t hardirqs_on_events;
278 atomic_t hardirqs_off_events;
279 atomic_t redundant_hardirqs_on;
280 atomic_t redundant_hardirqs_off;
281 atomic_t softirqs_on_events;
282 atomic_t softirqs_off_events;
283 atomic_t redundant_softirqs_on;
284 atomic_t redundant_softirqs_off;
285 atomic_t nr_unused_locks;
286 atomic_t nr_cyclic_checks;
287 atomic_t nr_cyclic_check_recursions;
288 atomic_t nr_find_usage_forwards_checks;
289 atomic_t nr_find_usage_forwards_recursions;
290 atomic_t nr_find_usage_backwards_checks;
291 atomic_t nr_find_usage_backwards_recursions;
292 # define debug_atomic_inc(ptr) atomic_inc(ptr)
293 # define debug_atomic_dec(ptr) atomic_dec(ptr)
294 # define debug_atomic_read(ptr) atomic_read(ptr)
296 # define debug_atomic_inc(ptr) do { } while (0)
297 # define debug_atomic_dec(ptr) do { } while (0)
298 # define debug_atomic_read(ptr) 0
305 static const char *usage_str[] =
307 [LOCK_USED] = "initial-use ",
308 [LOCK_USED_IN_HARDIRQ] = "in-hardirq-W",
309 [LOCK_USED_IN_SOFTIRQ] = "in-softirq-W",
310 [LOCK_ENABLED_SOFTIRQS] = "softirq-on-W",
311 [LOCK_ENABLED_HARDIRQS] = "hardirq-on-W",
312 [LOCK_USED_IN_HARDIRQ_READ] = "in-hardirq-R",
313 [LOCK_USED_IN_SOFTIRQ_READ] = "in-softirq-R",
314 [LOCK_ENABLED_SOFTIRQS_READ] = "softirq-on-R",
315 [LOCK_ENABLED_HARDIRQS_READ] = "hardirq-on-R",
318 const char * __get_key_name(struct lockdep_subclass_key *key, char *str)
320 unsigned long offs, size;
323 return kallsyms_lookup((unsigned long)key, &size, &offs, &modname, str);
327 get_usage_chars(struct lock_class *class, char *c1, char *c2, char *c3, char *c4)
329 *c1 = '.', *c2 = '.', *c3 = '.', *c4 = '.';
331 if (class->usage_mask & LOCKF_USED_IN_HARDIRQ)
334 if (class->usage_mask & LOCKF_ENABLED_HARDIRQS)
337 if (class->usage_mask & LOCKF_USED_IN_SOFTIRQ)
340 if (class->usage_mask & LOCKF_ENABLED_SOFTIRQS)
343 if (class->usage_mask & LOCKF_ENABLED_HARDIRQS_READ)
345 if (class->usage_mask & LOCKF_USED_IN_HARDIRQ_READ) {
347 if (class->usage_mask & LOCKF_ENABLED_HARDIRQS_READ)
351 if (class->usage_mask & LOCKF_ENABLED_SOFTIRQS_READ)
353 if (class->usage_mask & LOCKF_USED_IN_SOFTIRQ_READ) {
355 if (class->usage_mask & LOCKF_ENABLED_SOFTIRQS_READ)
360 static void print_lock_name(struct lock_class *class)
362 char str[128], c1, c2, c3, c4;
365 get_usage_chars(class, &c1, &c2, &c3, &c4);
369 name = __get_key_name(class->key, str);
370 printk(" (%s", name);
372 printk(" (%s", name);
373 if (class->name_version > 1)
374 printk("#%d", class->name_version);
376 printk("/%d", class->subclass);
378 printk("){%c%c%c%c}", c1, c2, c3, c4);
381 static void print_lockdep_cache(struct lockdep_map *lock)
388 name = __get_key_name(lock->key->subkeys, str);
393 static void print_lock(struct held_lock *hlock)
395 print_lock_name(hlock->class);
397 print_ip_sym(hlock->acquire_ip);
400 static void lockdep_print_held_locks(struct task_struct *curr)
402 int i, depth = curr->lockdep_depth;
405 printk("no locks held by %s/%d.\n", curr->comm, curr->pid);
408 printk("%d lock%s held by %s/%d:\n",
409 depth, depth > 1 ? "s" : "", curr->comm, curr->pid);
411 for (i = 0; i < depth; i++) {
413 print_lock(curr->held_locks + i);
417 static void print_lock_class_header(struct lock_class *class, int depth)
421 printk("%*s->", depth, "");
422 print_lock_name(class);
423 printk(" ops: %lu", class->ops);
426 for (bit = 0; bit < LOCK_USAGE_STATES; bit++) {
427 if (class->usage_mask & (1 << bit)) {
430 len += printk("%*s %s", depth, "", usage_str[bit]);
431 len += printk(" at:\n");
432 print_stack_trace(class->usage_traces + bit, len);
435 printk("%*s }\n", depth, "");
437 printk("%*s ... key at: ",depth,"");
438 print_ip_sym((unsigned long)class->key);
442 * printk all lock dependencies starting at <entry>:
444 static void print_lock_dependencies(struct lock_class *class, int depth)
446 struct lock_list *entry;
448 if (DEBUG_LOCKS_WARN_ON(depth >= 20))
451 print_lock_class_header(class, depth);
453 list_for_each_entry(entry, &class->locks_after, entry) {
454 DEBUG_LOCKS_WARN_ON(!entry->class);
455 print_lock_dependencies(entry->class, depth + 1);
457 printk("%*s ... acquired at:\n",depth,"");
458 print_stack_trace(&entry->trace, 2);
464 * Add a new dependency to the head of the list:
466 static int add_lock_to_list(struct lock_class *class, struct lock_class *this,
467 struct list_head *head, unsigned long ip)
469 struct lock_list *entry;
471 * Lock not present yet - get a new dependency struct and
472 * add it to the list:
474 entry = alloc_list_entry();
479 if (!save_trace(&entry->trace))
483 * Since we never remove from the dependency list, the list can
484 * be walked lockless by other CPUs, it's only allocation
485 * that must be protected by the spinlock. But this also means
486 * we must make new entries visible only once writes to the
487 * entry become visible - hence the RCU op:
489 list_add_tail_rcu(&entry->entry, head);
495 * Recursive, forwards-direction lock-dependency checking, used for
496 * both noncyclic checking and for hardirq-unsafe/softirq-unsafe
499 * (to keep the stackframe of the recursive functions small we
500 * use these global variables, and we also mark various helper
501 * functions as noinline.)
503 static struct held_lock *check_source, *check_target;
506 * Print a dependency chain entry (this is only done when a deadlock
507 * has been detected):
510 print_circular_bug_entry(struct lock_list *target, unsigned int depth)
512 if (debug_locks_silent)
514 printk("\n-> #%u", depth);
515 print_lock_name(target->class);
517 print_stack_trace(&target->trace, 6);
522 static void print_kernel_version(void)
524 printk("%s %.*s\n", init_utsname()->release,
525 (int)strcspn(init_utsname()->version, " "),
526 init_utsname()->version);
530 * When a circular dependency is detected, print the
534 print_circular_bug_header(struct lock_list *entry, unsigned int depth)
536 struct task_struct *curr = current;
538 __raw_spin_unlock(&hash_lock);
540 if (debug_locks_silent)
543 printk("\n=======================================================\n");
544 printk( "[ INFO: possible circular locking dependency detected ]\n");
545 print_kernel_version();
546 printk( "-------------------------------------------------------\n");
547 printk("%s/%d is trying to acquire lock:\n",
548 curr->comm, curr->pid);
549 print_lock(check_source);
550 printk("\nbut task is already holding lock:\n");
551 print_lock(check_target);
552 printk("\nwhich lock already depends on the new lock.\n\n");
553 printk("\nthe existing dependency chain (in reverse order) is:\n");
555 print_circular_bug_entry(entry, depth);
560 static noinline int print_circular_bug_tail(void)
562 struct task_struct *curr = current;
563 struct lock_list this;
565 if (debug_locks_silent)
568 /* hash_lock unlocked by the header */
569 __raw_spin_lock(&hash_lock);
570 this.class = check_source->class;
571 if (!save_trace(&this.trace))
573 __raw_spin_unlock(&hash_lock);
574 print_circular_bug_entry(&this, 0);
576 printk("\nother info that might help us debug this:\n\n");
577 lockdep_print_held_locks(curr);
579 printk("\nstack backtrace:\n");
585 #define RECURSION_LIMIT 40
587 static int noinline print_infinite_recursion_bug(void)
589 __raw_spin_unlock(&hash_lock);
590 DEBUG_LOCKS_WARN_ON(1);
596 * Prove that the dependency graph starting at <entry> can not
597 * lead to <target>. Print an error and return 0 if it does.
600 check_noncircular(struct lock_class *source, unsigned int depth)
602 struct lock_list *entry;
604 debug_atomic_inc(&nr_cyclic_check_recursions);
605 if (depth > max_recursion_depth)
606 max_recursion_depth = depth;
607 if (depth >= RECURSION_LIMIT)
608 return print_infinite_recursion_bug();
610 * Check this lock's dependency list:
612 list_for_each_entry(entry, &source->locks_after, entry) {
613 if (entry->class == check_target->class)
614 return print_circular_bug_header(entry, depth+1);
615 debug_atomic_inc(&nr_cyclic_checks);
616 if (!check_noncircular(entry->class, depth+1))
617 return print_circular_bug_entry(entry, depth+1);
622 static int very_verbose(struct lock_class *class)
625 return class_filter(class);
629 #ifdef CONFIG_TRACE_IRQFLAGS
632 * Forwards and backwards subgraph searching, for the purposes of
633 * proving that two subgraphs can be connected by a new dependency
634 * without creating any illegal irq-safe -> irq-unsafe lock dependency.
636 static enum lock_usage_bit find_usage_bit;
637 static struct lock_class *forwards_match, *backwards_match;
640 * Find a node in the forwards-direction dependency sub-graph starting
641 * at <source> that matches <find_usage_bit>.
643 * Return 2 if such a node exists in the subgraph, and put that node
644 * into <forwards_match>.
646 * Return 1 otherwise and keep <forwards_match> unchanged.
650 find_usage_forwards(struct lock_class *source, unsigned int depth)
652 struct lock_list *entry;
655 if (depth > max_recursion_depth)
656 max_recursion_depth = depth;
657 if (depth >= RECURSION_LIMIT)
658 return print_infinite_recursion_bug();
660 debug_atomic_inc(&nr_find_usage_forwards_checks);
661 if (source->usage_mask & (1 << find_usage_bit)) {
662 forwards_match = source;
667 * Check this lock's dependency list:
669 list_for_each_entry(entry, &source->locks_after, entry) {
670 debug_atomic_inc(&nr_find_usage_forwards_recursions);
671 ret = find_usage_forwards(entry->class, depth+1);
672 if (ret == 2 || ret == 0)
679 * Find a node in the backwards-direction dependency sub-graph starting
680 * at <source> that matches <find_usage_bit>.
682 * Return 2 if such a node exists in the subgraph, and put that node
683 * into <backwards_match>.
685 * Return 1 otherwise and keep <backwards_match> unchanged.
689 find_usage_backwards(struct lock_class *source, unsigned int depth)
691 struct lock_list *entry;
694 if (depth > max_recursion_depth)
695 max_recursion_depth = depth;
696 if (depth >= RECURSION_LIMIT)
697 return print_infinite_recursion_bug();
699 debug_atomic_inc(&nr_find_usage_backwards_checks);
700 if (source->usage_mask & (1 << find_usage_bit)) {
701 backwards_match = source;
706 * Check this lock's dependency list:
708 list_for_each_entry(entry, &source->locks_before, entry) {
709 debug_atomic_inc(&nr_find_usage_backwards_recursions);
710 ret = find_usage_backwards(entry->class, depth+1);
711 if (ret == 2 || ret == 0)
718 print_bad_irq_dependency(struct task_struct *curr,
719 struct held_lock *prev,
720 struct held_lock *next,
721 enum lock_usage_bit bit1,
722 enum lock_usage_bit bit2,
723 const char *irqclass)
725 __raw_spin_unlock(&hash_lock);
727 if (debug_locks_silent)
730 printk("\n======================================================\n");
731 printk( "[ INFO: %s-safe -> %s-unsafe lock order detected ]\n",
733 print_kernel_version();
734 printk( "------------------------------------------------------\n");
735 printk("%s/%d [HC%u[%lu]:SC%u[%lu]:HE%u:SE%u] is trying to acquire:\n",
736 curr->comm, curr->pid,
737 curr->hardirq_context, hardirq_count() >> HARDIRQ_SHIFT,
738 curr->softirq_context, softirq_count() >> SOFTIRQ_SHIFT,
739 curr->hardirqs_enabled,
740 curr->softirqs_enabled);
743 printk("\nand this task is already holding:\n");
745 printk("which would create a new lock dependency:\n");
746 print_lock_name(prev->class);
748 print_lock_name(next->class);
751 printk("\nbut this new dependency connects a %s-irq-safe lock:\n",
753 print_lock_name(backwards_match);
754 printk("\n... which became %s-irq-safe at:\n", irqclass);
756 print_stack_trace(backwards_match->usage_traces + bit1, 1);
758 printk("\nto a %s-irq-unsafe lock:\n", irqclass);
759 print_lock_name(forwards_match);
760 printk("\n... which became %s-irq-unsafe at:\n", irqclass);
763 print_stack_trace(forwards_match->usage_traces + bit2, 1);
765 printk("\nother info that might help us debug this:\n\n");
766 lockdep_print_held_locks(curr);
768 printk("\nthe %s-irq-safe lock's dependencies:\n", irqclass);
769 print_lock_dependencies(backwards_match, 0);
771 printk("\nthe %s-irq-unsafe lock's dependencies:\n", irqclass);
772 print_lock_dependencies(forwards_match, 0);
774 printk("\nstack backtrace:\n");
781 check_usage(struct task_struct *curr, struct held_lock *prev,
782 struct held_lock *next, enum lock_usage_bit bit_backwards,
783 enum lock_usage_bit bit_forwards, const char *irqclass)
787 find_usage_bit = bit_backwards;
788 /* fills in <backwards_match> */
789 ret = find_usage_backwards(prev->class, 0);
790 if (!ret || ret == 1)
793 find_usage_bit = bit_forwards;
794 ret = find_usage_forwards(next->class, 0);
795 if (!ret || ret == 1)
798 return print_bad_irq_dependency(curr, prev, next,
799 bit_backwards, bit_forwards, irqclass);
805 print_deadlock_bug(struct task_struct *curr, struct held_lock *prev,
806 struct held_lock *next)
809 __raw_spin_unlock(&hash_lock);
810 if (debug_locks_silent)
813 printk("\n=============================================\n");
814 printk( "[ INFO: possible recursive locking detected ]\n");
815 print_kernel_version();
816 printk( "---------------------------------------------\n");
817 printk("%s/%d is trying to acquire lock:\n",
818 curr->comm, curr->pid);
820 printk("\nbut task is already holding lock:\n");
823 printk("\nother info that might help us debug this:\n");
824 lockdep_print_held_locks(curr);
826 printk("\nstack backtrace:\n");
833 * Check whether we are holding such a class already.
835 * (Note that this has to be done separately, because the graph cannot
836 * detect such classes of deadlocks.)
838 * Returns: 0 on deadlock detected, 1 on OK, 2 on recursive read
841 check_deadlock(struct task_struct *curr, struct held_lock *next,
842 struct lockdep_map *next_instance, int read)
844 struct held_lock *prev;
847 for (i = 0; i < curr->lockdep_depth; i++) {
848 prev = curr->held_locks + i;
849 if (prev->class != next->class)
852 * Allow read-after-read recursion of the same
853 * lock class (i.e. read_lock(lock)+read_lock(lock)):
855 if ((read == 2) && prev->read)
857 return print_deadlock_bug(curr, prev, next);
863 * There was a chain-cache miss, and we are about to add a new dependency
864 * to a previous lock. We recursively validate the following rules:
866 * - would the adding of the <prev> -> <next> dependency create a
867 * circular dependency in the graph? [== circular deadlock]
869 * - does the new prev->next dependency connect any hardirq-safe lock
870 * (in the full backwards-subgraph starting at <prev>) with any
871 * hardirq-unsafe lock (in the full forwards-subgraph starting at
872 * <next>)? [== illegal lock inversion with hardirq contexts]
874 * - does the new prev->next dependency connect any softirq-safe lock
875 * (in the full backwards-subgraph starting at <prev>) with any
876 * softirq-unsafe lock (in the full forwards-subgraph starting at
877 * <next>)? [== illegal lock inversion with softirq contexts]
879 * any of these scenarios could lead to a deadlock.
881 * Then if all the validations pass, we add the forwards and backwards
885 check_prev_add(struct task_struct *curr, struct held_lock *prev,
886 struct held_lock *next)
888 struct lock_list *entry;
892 * Prove that the new <prev> -> <next> dependency would not
893 * create a circular dependency in the graph. (We do this by
894 * forward-recursing into the graph starting at <next>, and
895 * checking whether we can reach <prev>.)
897 * We are using global variables to control the recursion, to
898 * keep the stackframe size of the recursive functions low:
902 if (!(check_noncircular(next->class, 0)))
903 return print_circular_bug_tail();
905 #ifdef CONFIG_TRACE_IRQFLAGS
907 * Prove that the new dependency does not connect a hardirq-safe
908 * lock with a hardirq-unsafe lock - to achieve this we search
909 * the backwards-subgraph starting at <prev>, and the
910 * forwards-subgraph starting at <next>:
912 if (!check_usage(curr, prev, next, LOCK_USED_IN_HARDIRQ,
913 LOCK_ENABLED_HARDIRQS, "hard"))
917 * Prove that the new dependency does not connect a hardirq-safe-read
918 * lock with a hardirq-unsafe lock - to achieve this we search
919 * the backwards-subgraph starting at <prev>, and the
920 * forwards-subgraph starting at <next>:
922 if (!check_usage(curr, prev, next, LOCK_USED_IN_HARDIRQ_READ,
923 LOCK_ENABLED_HARDIRQS, "hard-read"))
927 * Prove that the new dependency does not connect a softirq-safe
928 * lock with a softirq-unsafe lock - to achieve this we search
929 * the backwards-subgraph starting at <prev>, and the
930 * forwards-subgraph starting at <next>:
932 if (!check_usage(curr, prev, next, LOCK_USED_IN_SOFTIRQ,
933 LOCK_ENABLED_SOFTIRQS, "soft"))
936 * Prove that the new dependency does not connect a softirq-safe-read
937 * lock with a softirq-unsafe lock - to achieve this we search
938 * the backwards-subgraph starting at <prev>, and the
939 * forwards-subgraph starting at <next>:
941 if (!check_usage(curr, prev, next, LOCK_USED_IN_SOFTIRQ_READ,
942 LOCK_ENABLED_SOFTIRQS, "soft"))
946 * For recursive read-locks we do all the dependency checks,
947 * but we dont store read-triggered dependencies (only
948 * write-triggered dependencies). This ensures that only the
949 * write-side dependencies matter, and that if for example a
950 * write-lock never takes any other locks, then the reads are
951 * equivalent to a NOP.
953 if (next->read == 2 || prev->read == 2)
956 * Is the <prev> -> <next> dependency already present?
958 * (this may occur even though this is a new chain: consider
959 * e.g. the L1 -> L2 -> L3 -> L4 and the L5 -> L1 -> L2 -> L3
960 * chains - the second one will be new, but L1 already has
961 * L2 added to its dependency list, due to the first chain.)
963 list_for_each_entry(entry, &prev->class->locks_after, entry) {
964 if (entry->class == next->class)
969 * Ok, all validations passed, add the new lock
970 * to the previous lock's dependency list:
972 ret = add_lock_to_list(prev->class, next->class,
973 &prev->class->locks_after, next->acquire_ip);
977 ret = add_lock_to_list(next->class, prev->class,
978 &next->class->locks_before, next->acquire_ip);
983 * Debugging printouts:
985 if (verbose(prev->class) || verbose(next->class)) {
986 __raw_spin_unlock(&hash_lock);
987 printk("\n new dependency: ");
988 print_lock_name(prev->class);
990 print_lock_name(next->class);
993 __raw_spin_lock(&hash_lock);
999 * Add the dependency to all directly-previous locks that are 'relevant'.
1000 * The ones that are relevant are (in increasing distance from curr):
1001 * all consecutive trylock entries and the final non-trylock entry - or
1002 * the end of this context's lock-chain - whichever comes first.
1005 check_prevs_add(struct task_struct *curr, struct held_lock *next)
1007 int depth = curr->lockdep_depth;
1008 struct held_lock *hlock;
1013 * Depth must not be zero for a non-head lock:
1018 * At least two relevant locks must exist for this
1021 if (curr->held_locks[depth].irq_context !=
1022 curr->held_locks[depth-1].irq_context)
1026 hlock = curr->held_locks + depth-1;
1028 * Only non-recursive-read entries get new dependencies
1031 if (hlock->read != 2) {
1032 if (!check_prev_add(curr, hlock, next))
1035 * Stop after the first non-trylock entry,
1036 * as non-trylock entries have added their
1037 * own direct dependencies already, so this
1038 * lock is connected to them indirectly:
1040 if (!hlock->trylock)
1045 * End of lock-stack?
1050 * Stop the search if we cross into another context:
1052 if (curr->held_locks[depth].irq_context !=
1053 curr->held_locks[depth-1].irq_context)
1058 __raw_spin_unlock(&hash_lock);
1059 DEBUG_LOCKS_WARN_ON(1);
1066 * Is this the address of a static object:
1068 static int static_obj(void *obj)
1070 unsigned long start = (unsigned long) &_stext,
1071 end = (unsigned long) &_end,
1072 addr = (unsigned long) obj;
1080 if ((addr >= start) && (addr < end))
1087 for_each_possible_cpu(i) {
1088 start = (unsigned long) &__per_cpu_start + per_cpu_offset(i);
1089 end = (unsigned long) &__per_cpu_start + PERCPU_ENOUGH_ROOM
1090 + per_cpu_offset(i);
1092 if ((addr >= start) && (addr < end))
1100 return is_module_address(addr);
1104 * To make lock name printouts unique, we calculate a unique
1105 * class->name_version generation counter:
1107 static int count_matching_names(struct lock_class *new_class)
1109 struct lock_class *class;
1112 if (!new_class->name)
1115 list_for_each_entry(class, &all_lock_classes, lock_entry) {
1116 if (new_class->key - new_class->subclass == class->key)
1117 return class->name_version;
1118 if (class->name && !strcmp(class->name, new_class->name))
1119 count = max(count, class->name_version);
1126 * Register a lock's class in the hash-table, if the class is not present
1127 * yet. Otherwise we look it up. We cache the result in the lock object
1128 * itself, so actual lookup of the hash should be once per lock object.
1130 static inline struct lock_class *
1131 look_up_lock_class(struct lockdep_map *lock, unsigned int subclass)
1133 struct lockdep_subclass_key *key;
1134 struct list_head *hash_head;
1135 struct lock_class *class;
1137 #ifdef CONFIG_DEBUG_LOCKDEP
1139 * If the architecture calls into lockdep before initializing
1140 * the hashes then we'll warn about it later. (we cannot printk
1143 if (unlikely(!lockdep_initialized)) {
1145 lockdep_init_error = 1;
1150 * Static locks do not have their class-keys yet - for them the key
1151 * is the lock object itself:
1153 if (unlikely(!lock->key))
1154 lock->key = (void *)lock;
1157 * NOTE: the class-key must be unique. For dynamic locks, a static
1158 * lock_class_key variable is passed in through the mutex_init()
1159 * (or spin_lock_init()) call - which acts as the key. For static
1160 * locks we use the lock object itself as the key.
1162 BUILD_BUG_ON(sizeof(struct lock_class_key) > sizeof(struct lock_class));
1164 key = lock->key->subkeys + subclass;
1166 hash_head = classhashentry(key);
1169 * We can walk the hash lockfree, because the hash only
1170 * grows, and we are careful when adding entries to the end:
1172 list_for_each_entry(class, hash_head, hash_entry)
1173 if (class->key == key)
1180 * Register a lock's class in the hash-table, if the class is not present
1181 * yet. Otherwise we look it up. We cache the result in the lock object
1182 * itself, so actual lookup of the hash should be once per lock object.
1184 static inline struct lock_class *
1185 register_lock_class(struct lockdep_map *lock, unsigned int subclass, int force)
1187 struct lockdep_subclass_key *key;
1188 struct list_head *hash_head;
1189 struct lock_class *class;
1191 class = look_up_lock_class(lock, subclass);
1196 * Debug-check: all keys must be persistent!
1198 if (!static_obj(lock->key)) {
1200 printk("INFO: trying to register non-static key.\n");
1201 printk("the code is fine but needs lockdep annotation.\n");
1202 printk("turning off the locking correctness validator.\n");
1208 key = lock->key->subkeys + subclass;
1209 hash_head = classhashentry(key);
1211 __raw_spin_lock(&hash_lock);
1213 * We have to do the hash-walk again, to avoid races
1216 list_for_each_entry(class, hash_head, hash_entry)
1217 if (class->key == key)
1218 goto out_unlock_set;
1220 * Allocate a new key from the static array, and add it to
1223 if (nr_lock_classes >= MAX_LOCKDEP_KEYS) {
1224 __raw_spin_unlock(&hash_lock);
1226 printk("BUG: MAX_LOCKDEP_KEYS too low!\n");
1227 printk("turning off the locking correctness validator.\n");
1230 class = lock_classes + nr_lock_classes++;
1231 debug_atomic_inc(&nr_unused_locks);
1233 class->name = lock->name;
1234 class->subclass = subclass;
1235 INIT_LIST_HEAD(&class->lock_entry);
1236 INIT_LIST_HEAD(&class->locks_before);
1237 INIT_LIST_HEAD(&class->locks_after);
1238 class->name_version = count_matching_names(class);
1240 * We use RCU's safe list-add method to make
1241 * parallel walking of the hash-list safe:
1243 list_add_tail_rcu(&class->hash_entry, hash_head);
1245 if (verbose(class)) {
1246 __raw_spin_unlock(&hash_lock);
1247 printk("\nnew class %p: %s", class->key, class->name);
1248 if (class->name_version > 1)
1249 printk("#%d", class->name_version);
1252 __raw_spin_lock(&hash_lock);
1255 __raw_spin_unlock(&hash_lock);
1257 if (!subclass || force)
1258 lock->class_cache = class;
1260 DEBUG_LOCKS_WARN_ON(class->subclass != subclass);
1266 * Look up a dependency chain. If the key is not present yet then
1267 * add it and return 0 - in this case the new dependency chain is
1268 * validated. If the key is already hashed, return 1.
1270 static inline int lookup_chain_cache(u64 chain_key)
1272 struct list_head *hash_head = chainhashentry(chain_key);
1273 struct lock_chain *chain;
1275 DEBUG_LOCKS_WARN_ON(!irqs_disabled());
1277 * We can walk it lock-free, because entries only get added
1280 list_for_each_entry(chain, hash_head, entry) {
1281 if (chain->chain_key == chain_key) {
1283 debug_atomic_inc(&chain_lookup_hits);
1285 * In the debugging case, force redundant checking
1288 #ifdef CONFIG_DEBUG_LOCKDEP
1289 __raw_spin_lock(&hash_lock);
1296 * Allocate a new chain entry from the static array, and add
1299 __raw_spin_lock(&hash_lock);
1301 * We have to walk the chain again locked - to avoid duplicates:
1303 list_for_each_entry(chain, hash_head, entry) {
1304 if (chain->chain_key == chain_key) {
1305 __raw_spin_unlock(&hash_lock);
1309 if (unlikely(nr_lock_chains >= MAX_LOCKDEP_CHAINS)) {
1310 __raw_spin_unlock(&hash_lock);
1312 printk("BUG: MAX_LOCKDEP_CHAINS too low!\n");
1313 printk("turning off the locking correctness validator.\n");
1316 chain = lock_chains + nr_lock_chains++;
1317 chain->chain_key = chain_key;
1318 list_add_tail_rcu(&chain->entry, hash_head);
1319 debug_atomic_inc(&chain_lookup_misses);
1320 #ifdef CONFIG_TRACE_IRQFLAGS
1321 if (current->hardirq_context)
1322 nr_hardirq_chains++;
1324 if (current->softirq_context)
1325 nr_softirq_chains++;
1327 nr_process_chains++;
1330 nr_process_chains++;
1337 * We are building curr_chain_key incrementally, so double-check
1338 * it from scratch, to make sure that it's done correctly:
1340 static void check_chain_key(struct task_struct *curr)
1342 #ifdef CONFIG_DEBUG_LOCKDEP
1343 struct held_lock *hlock, *prev_hlock = NULL;
1347 for (i = 0; i < curr->lockdep_depth; i++) {
1348 hlock = curr->held_locks + i;
1349 if (chain_key != hlock->prev_chain_key) {
1351 printk("hm#1, depth: %u [%u], %016Lx != %016Lx\n",
1352 curr->lockdep_depth, i,
1353 (unsigned long long)chain_key,
1354 (unsigned long long)hlock->prev_chain_key);
1358 id = hlock->class - lock_classes;
1359 DEBUG_LOCKS_WARN_ON(id >= MAX_LOCKDEP_KEYS);
1360 if (prev_hlock && (prev_hlock->irq_context !=
1361 hlock->irq_context))
1363 chain_key = iterate_chain_key(chain_key, id);
1366 if (chain_key != curr->curr_chain_key) {
1368 printk("hm#2, depth: %u [%u], %016Lx != %016Lx\n",
1369 curr->lockdep_depth, i,
1370 (unsigned long long)chain_key,
1371 (unsigned long long)curr->curr_chain_key);
1377 #ifdef CONFIG_TRACE_IRQFLAGS
1380 * print irq inversion bug:
1383 print_irq_inversion_bug(struct task_struct *curr, struct lock_class *other,
1384 struct held_lock *this, int forwards,
1385 const char *irqclass)
1387 __raw_spin_unlock(&hash_lock);
1389 if (debug_locks_silent)
1392 printk("\n=========================================================\n");
1393 printk( "[ INFO: possible irq lock inversion dependency detected ]\n");
1394 print_kernel_version();
1395 printk( "---------------------------------------------------------\n");
1396 printk("%s/%d just changed the state of lock:\n",
1397 curr->comm, curr->pid);
1400 printk("but this lock took another, %s-irq-unsafe lock in the past:\n", irqclass);
1402 printk("but this lock was taken by another, %s-irq-safe lock in the past:\n", irqclass);
1403 print_lock_name(other);
1404 printk("\n\nand interrupts could create inverse lock ordering between them.\n\n");
1406 printk("\nother info that might help us debug this:\n");
1407 lockdep_print_held_locks(curr);
1409 printk("\nthe first lock's dependencies:\n");
1410 print_lock_dependencies(this->class, 0);
1412 printk("\nthe second lock's dependencies:\n");
1413 print_lock_dependencies(other, 0);
1415 printk("\nstack backtrace:\n");
1422 * Prove that in the forwards-direction subgraph starting at <this>
1423 * there is no lock matching <mask>:
1426 check_usage_forwards(struct task_struct *curr, struct held_lock *this,
1427 enum lock_usage_bit bit, const char *irqclass)
1431 find_usage_bit = bit;
1432 /* fills in <forwards_match> */
1433 ret = find_usage_forwards(this->class, 0);
1434 if (!ret || ret == 1)
1437 return print_irq_inversion_bug(curr, forwards_match, this, 1, irqclass);
1441 * Prove that in the backwards-direction subgraph starting at <this>
1442 * there is no lock matching <mask>:
1445 check_usage_backwards(struct task_struct *curr, struct held_lock *this,
1446 enum lock_usage_bit bit, const char *irqclass)
1450 find_usage_bit = bit;
1451 /* fills in <backwards_match> */
1452 ret = find_usage_backwards(this->class, 0);
1453 if (!ret || ret == 1)
1456 return print_irq_inversion_bug(curr, backwards_match, this, 0, irqclass);
1459 static inline void print_irqtrace_events(struct task_struct *curr)
1461 printk("irq event stamp: %u\n", curr->irq_events);
1462 printk("hardirqs last enabled at (%u): ", curr->hardirq_enable_event);
1463 print_ip_sym(curr->hardirq_enable_ip);
1464 printk("hardirqs last disabled at (%u): ", curr->hardirq_disable_event);
1465 print_ip_sym(curr->hardirq_disable_ip);
1466 printk("softirqs last enabled at (%u): ", curr->softirq_enable_event);
1467 print_ip_sym(curr->softirq_enable_ip);
1468 printk("softirqs last disabled at (%u): ", curr->softirq_disable_event);
1469 print_ip_sym(curr->softirq_disable_ip);
1473 static inline void print_irqtrace_events(struct task_struct *curr)
1479 print_usage_bug(struct task_struct *curr, struct held_lock *this,
1480 enum lock_usage_bit prev_bit, enum lock_usage_bit new_bit)
1482 __raw_spin_unlock(&hash_lock);
1484 if (debug_locks_silent)
1487 printk("\n=================================\n");
1488 printk( "[ INFO: inconsistent lock state ]\n");
1489 print_kernel_version();
1490 printk( "---------------------------------\n");
1492 printk("inconsistent {%s} -> {%s} usage.\n",
1493 usage_str[prev_bit], usage_str[new_bit]);
1495 printk("%s/%d [HC%u[%lu]:SC%u[%lu]:HE%u:SE%u] takes:\n",
1496 curr->comm, curr->pid,
1497 trace_hardirq_context(curr), hardirq_count() >> HARDIRQ_SHIFT,
1498 trace_softirq_context(curr), softirq_count() >> SOFTIRQ_SHIFT,
1499 trace_hardirqs_enabled(curr),
1500 trace_softirqs_enabled(curr));
1503 printk("{%s} state was registered at:\n", usage_str[prev_bit]);
1504 print_stack_trace(this->class->usage_traces + prev_bit, 1);
1506 print_irqtrace_events(curr);
1507 printk("\nother info that might help us debug this:\n");
1508 lockdep_print_held_locks(curr);
1510 printk("\nstack backtrace:\n");
1517 * Print out an error if an invalid bit is set:
1520 valid_state(struct task_struct *curr, struct held_lock *this,
1521 enum lock_usage_bit new_bit, enum lock_usage_bit bad_bit)
1523 if (unlikely(this->class->usage_mask & (1 << bad_bit)))
1524 return print_usage_bug(curr, this, bad_bit, new_bit);
1528 #define STRICT_READ_CHECKS 1
1531 * Mark a lock with a usage bit, and validate the state transition:
1533 static int mark_lock(struct task_struct *curr, struct held_lock *this,
1534 enum lock_usage_bit new_bit, unsigned long ip)
1536 unsigned int new_mask = 1 << new_bit, ret = 1;
1539 * If already set then do not dirty the cacheline,
1540 * nor do any checks:
1542 if (likely(this->class->usage_mask & new_mask))
1545 __raw_spin_lock(&hash_lock);
1547 * Make sure we didnt race:
1549 if (unlikely(this->class->usage_mask & new_mask)) {
1550 __raw_spin_unlock(&hash_lock);
1554 this->class->usage_mask |= new_mask;
1556 #ifdef CONFIG_TRACE_IRQFLAGS
1557 if (new_bit == LOCK_ENABLED_HARDIRQS ||
1558 new_bit == LOCK_ENABLED_HARDIRQS_READ)
1559 ip = curr->hardirq_enable_ip;
1560 else if (new_bit == LOCK_ENABLED_SOFTIRQS ||
1561 new_bit == LOCK_ENABLED_SOFTIRQS_READ)
1562 ip = curr->softirq_enable_ip;
1564 if (!save_trace(this->class->usage_traces + new_bit))
1568 #ifdef CONFIG_TRACE_IRQFLAGS
1569 case LOCK_USED_IN_HARDIRQ:
1570 if (!valid_state(curr, this, new_bit, LOCK_ENABLED_HARDIRQS))
1572 if (!valid_state(curr, this, new_bit,
1573 LOCK_ENABLED_HARDIRQS_READ))
1576 * just marked it hardirq-safe, check that this lock
1577 * took no hardirq-unsafe lock in the past:
1579 if (!check_usage_forwards(curr, this,
1580 LOCK_ENABLED_HARDIRQS, "hard"))
1582 #if STRICT_READ_CHECKS
1584 * just marked it hardirq-safe, check that this lock
1585 * took no hardirq-unsafe-read lock in the past:
1587 if (!check_usage_forwards(curr, this,
1588 LOCK_ENABLED_HARDIRQS_READ, "hard-read"))
1591 if (hardirq_verbose(this->class))
1594 case LOCK_USED_IN_SOFTIRQ:
1595 if (!valid_state(curr, this, new_bit, LOCK_ENABLED_SOFTIRQS))
1597 if (!valid_state(curr, this, new_bit,
1598 LOCK_ENABLED_SOFTIRQS_READ))
1601 * just marked it softirq-safe, check that this lock
1602 * took no softirq-unsafe lock in the past:
1604 if (!check_usage_forwards(curr, this,
1605 LOCK_ENABLED_SOFTIRQS, "soft"))
1607 #if STRICT_READ_CHECKS
1609 * just marked it softirq-safe, check that this lock
1610 * took no softirq-unsafe-read lock in the past:
1612 if (!check_usage_forwards(curr, this,
1613 LOCK_ENABLED_SOFTIRQS_READ, "soft-read"))
1616 if (softirq_verbose(this->class))
1619 case LOCK_USED_IN_HARDIRQ_READ:
1620 if (!valid_state(curr, this, new_bit, LOCK_ENABLED_HARDIRQS))
1623 * just marked it hardirq-read-safe, check that this lock
1624 * took no hardirq-unsafe lock in the past:
1626 if (!check_usage_forwards(curr, this,
1627 LOCK_ENABLED_HARDIRQS, "hard"))
1629 if (hardirq_verbose(this->class))
1632 case LOCK_USED_IN_SOFTIRQ_READ:
1633 if (!valid_state(curr, this, new_bit, LOCK_ENABLED_SOFTIRQS))
1636 * just marked it softirq-read-safe, check that this lock
1637 * took no softirq-unsafe lock in the past:
1639 if (!check_usage_forwards(curr, this,
1640 LOCK_ENABLED_SOFTIRQS, "soft"))
1642 if (softirq_verbose(this->class))
1645 case LOCK_ENABLED_HARDIRQS:
1646 if (!valid_state(curr, this, new_bit, LOCK_USED_IN_HARDIRQ))
1648 if (!valid_state(curr, this, new_bit,
1649 LOCK_USED_IN_HARDIRQ_READ))
1652 * just marked it hardirq-unsafe, check that no hardirq-safe
1653 * lock in the system ever took it in the past:
1655 if (!check_usage_backwards(curr, this,
1656 LOCK_USED_IN_HARDIRQ, "hard"))
1658 #if STRICT_READ_CHECKS
1660 * just marked it hardirq-unsafe, check that no
1661 * hardirq-safe-read lock in the system ever took
1664 if (!check_usage_backwards(curr, this,
1665 LOCK_USED_IN_HARDIRQ_READ, "hard-read"))
1668 if (hardirq_verbose(this->class))
1671 case LOCK_ENABLED_SOFTIRQS:
1672 if (!valid_state(curr, this, new_bit, LOCK_USED_IN_SOFTIRQ))
1674 if (!valid_state(curr, this, new_bit,
1675 LOCK_USED_IN_SOFTIRQ_READ))
1678 * just marked it softirq-unsafe, check that no softirq-safe
1679 * lock in the system ever took it in the past:
1681 if (!check_usage_backwards(curr, this,
1682 LOCK_USED_IN_SOFTIRQ, "soft"))
1684 #if STRICT_READ_CHECKS
1686 * just marked it softirq-unsafe, check that no
1687 * softirq-safe-read lock in the system ever took
1690 if (!check_usage_backwards(curr, this,
1691 LOCK_USED_IN_SOFTIRQ_READ, "soft-read"))
1694 if (softirq_verbose(this->class))
1697 case LOCK_ENABLED_HARDIRQS_READ:
1698 if (!valid_state(curr, this, new_bit, LOCK_USED_IN_HARDIRQ))
1700 #if STRICT_READ_CHECKS
1702 * just marked it hardirq-read-unsafe, check that no
1703 * hardirq-safe lock in the system ever took it in the past:
1705 if (!check_usage_backwards(curr, this,
1706 LOCK_USED_IN_HARDIRQ, "hard"))
1709 if (hardirq_verbose(this->class))
1712 case LOCK_ENABLED_SOFTIRQS_READ:
1713 if (!valid_state(curr, this, new_bit, LOCK_USED_IN_SOFTIRQ))
1715 #if STRICT_READ_CHECKS
1717 * just marked it softirq-read-unsafe, check that no
1718 * softirq-safe lock in the system ever took it in the past:
1720 if (!check_usage_backwards(curr, this,
1721 LOCK_USED_IN_SOFTIRQ, "soft"))
1724 if (softirq_verbose(this->class))
1730 * Add it to the global list of classes:
1732 list_add_tail_rcu(&this->class->lock_entry, &all_lock_classes);
1733 debug_atomic_dec(&nr_unused_locks);
1741 __raw_spin_unlock(&hash_lock);
1744 * We must printk outside of the hash_lock:
1747 printk("\nmarked lock as {%s}:\n", usage_str[new_bit]);
1749 print_irqtrace_events(curr);
1756 #ifdef CONFIG_TRACE_IRQFLAGS
1758 * Mark all held locks with a usage bit:
1761 mark_held_locks(struct task_struct *curr, int hardirq, unsigned long ip)
1763 enum lock_usage_bit usage_bit;
1764 struct held_lock *hlock;
1767 for (i = 0; i < curr->lockdep_depth; i++) {
1768 hlock = curr->held_locks + i;
1772 usage_bit = LOCK_ENABLED_HARDIRQS_READ;
1774 usage_bit = LOCK_ENABLED_HARDIRQS;
1777 usage_bit = LOCK_ENABLED_SOFTIRQS_READ;
1779 usage_bit = LOCK_ENABLED_SOFTIRQS;
1781 if (!mark_lock(curr, hlock, usage_bit, ip))
1789 * Debugging helper: via this flag we know that we are in
1790 * 'early bootup code', and will warn about any invalid irqs-on event:
1792 static int early_boot_irqs_enabled;
1794 void early_boot_irqs_off(void)
1796 early_boot_irqs_enabled = 0;
1799 void early_boot_irqs_on(void)
1801 early_boot_irqs_enabled = 1;
1805 * Hardirqs will be enabled:
1807 void trace_hardirqs_on(void)
1809 struct task_struct *curr = current;
1812 if (unlikely(!debug_locks || current->lockdep_recursion))
1815 if (DEBUG_LOCKS_WARN_ON(unlikely(!early_boot_irqs_enabled)))
1818 if (unlikely(curr->hardirqs_enabled)) {
1819 debug_atomic_inc(&redundant_hardirqs_on);
1822 /* we'll do an OFF -> ON transition: */
1823 curr->hardirqs_enabled = 1;
1824 ip = (unsigned long) __builtin_return_address(0);
1826 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
1828 if (DEBUG_LOCKS_WARN_ON(current->hardirq_context))
1831 * We are going to turn hardirqs on, so set the
1832 * usage bit for all held locks:
1834 if (!mark_held_locks(curr, 1, ip))
1837 * If we have softirqs enabled, then set the usage
1838 * bit for all held locks. (disabled hardirqs prevented
1839 * this bit from being set before)
1841 if (curr->softirqs_enabled)
1842 if (!mark_held_locks(curr, 0, ip))
1845 curr->hardirq_enable_ip = ip;
1846 curr->hardirq_enable_event = ++curr->irq_events;
1847 debug_atomic_inc(&hardirqs_on_events);
1850 EXPORT_SYMBOL(trace_hardirqs_on);
1853 * Hardirqs were disabled:
1855 void trace_hardirqs_off(void)
1857 struct task_struct *curr = current;
1859 if (unlikely(!debug_locks || current->lockdep_recursion))
1862 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
1865 if (curr->hardirqs_enabled) {
1867 * We have done an ON -> OFF transition:
1869 curr->hardirqs_enabled = 0;
1870 curr->hardirq_disable_ip = _RET_IP_;
1871 curr->hardirq_disable_event = ++curr->irq_events;
1872 debug_atomic_inc(&hardirqs_off_events);
1874 debug_atomic_inc(&redundant_hardirqs_off);
1877 EXPORT_SYMBOL(trace_hardirqs_off);
1880 * Softirqs will be enabled:
1882 void trace_softirqs_on(unsigned long ip)
1884 struct task_struct *curr = current;
1886 if (unlikely(!debug_locks))
1889 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
1892 if (curr->softirqs_enabled) {
1893 debug_atomic_inc(&redundant_softirqs_on);
1898 * We'll do an OFF -> ON transition:
1900 curr->softirqs_enabled = 1;
1901 curr->softirq_enable_ip = ip;
1902 curr->softirq_enable_event = ++curr->irq_events;
1903 debug_atomic_inc(&softirqs_on_events);
1905 * We are going to turn softirqs on, so set the
1906 * usage bit for all held locks, if hardirqs are
1909 if (curr->hardirqs_enabled)
1910 mark_held_locks(curr, 0, ip);
1914 * Softirqs were disabled:
1916 void trace_softirqs_off(unsigned long ip)
1918 struct task_struct *curr = current;
1920 if (unlikely(!debug_locks))
1923 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
1926 if (curr->softirqs_enabled) {
1928 * We have done an ON -> OFF transition:
1930 curr->softirqs_enabled = 0;
1931 curr->softirq_disable_ip = ip;
1932 curr->softirq_disable_event = ++curr->irq_events;
1933 debug_atomic_inc(&softirqs_off_events);
1934 DEBUG_LOCKS_WARN_ON(!softirq_count());
1936 debug_atomic_inc(&redundant_softirqs_off);
1942 * Initialize a lock instance's lock-class mapping info:
1944 void lockdep_init_map(struct lockdep_map *lock, const char *name,
1945 struct lock_class_key *key, int subclass)
1947 if (unlikely(!debug_locks))
1950 if (DEBUG_LOCKS_WARN_ON(!key))
1952 if (DEBUG_LOCKS_WARN_ON(!name))
1955 * Sanity check, the lock-class key must be persistent:
1957 if (!static_obj(key)) {
1958 printk("BUG: key %p not in .data!\n", key);
1959 DEBUG_LOCKS_WARN_ON(1);
1964 lock->class_cache = NULL;
1966 register_lock_class(lock, subclass, 1);
1969 EXPORT_SYMBOL_GPL(lockdep_init_map);
1972 * This gets called for every mutex_lock*()/spin_lock*() operation.
1973 * We maintain the dependency maps and validate the locking attempt:
1975 static int __lock_acquire(struct lockdep_map *lock, unsigned int subclass,
1976 int trylock, int read, int check, int hardirqs_off,
1979 struct task_struct *curr = current;
1980 struct lock_class *class = NULL;
1981 struct held_lock *hlock;
1982 unsigned int depth, id;
1986 if (unlikely(!debug_locks))
1989 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
1992 if (unlikely(subclass >= MAX_LOCKDEP_SUBCLASSES)) {
1994 printk("BUG: MAX_LOCKDEP_SUBCLASSES too low!\n");
1995 printk("turning off the locking correctness validator.\n");
2000 class = lock->class_cache;
2002 * Not cached yet or subclass?
2004 if (unlikely(!class)) {
2005 class = register_lock_class(lock, subclass, 0);
2009 debug_atomic_inc((atomic_t *)&class->ops);
2010 if (very_verbose(class)) {
2011 printk("\nacquire class [%p] %s", class->key, class->name);
2012 if (class->name_version > 1)
2013 printk("#%d", class->name_version);
2019 * Add the lock to the list of currently held locks.
2020 * (we dont increase the depth just yet, up until the
2021 * dependency checks are done)
2023 depth = curr->lockdep_depth;
2024 if (DEBUG_LOCKS_WARN_ON(depth >= MAX_LOCK_DEPTH))
2027 hlock = curr->held_locks + depth;
2029 hlock->class = class;
2030 hlock->acquire_ip = ip;
2031 hlock->instance = lock;
2032 hlock->trylock = trylock;
2034 hlock->check = check;
2035 hlock->hardirqs_off = hardirqs_off;
2039 #ifdef CONFIG_TRACE_IRQFLAGS
2041 * If non-trylock use in a hardirq or softirq context, then
2042 * mark the lock as used in these contexts:
2046 if (curr->hardirq_context)
2047 if (!mark_lock(curr, hlock,
2048 LOCK_USED_IN_HARDIRQ_READ, ip))
2050 if (curr->softirq_context)
2051 if (!mark_lock(curr, hlock,
2052 LOCK_USED_IN_SOFTIRQ_READ, ip))
2055 if (curr->hardirq_context)
2056 if (!mark_lock(curr, hlock, LOCK_USED_IN_HARDIRQ, ip))
2058 if (curr->softirq_context)
2059 if (!mark_lock(curr, hlock, LOCK_USED_IN_SOFTIRQ, ip))
2063 if (!hardirqs_off) {
2065 if (!mark_lock(curr, hlock,
2066 LOCK_ENABLED_HARDIRQS_READ, ip))
2068 if (curr->softirqs_enabled)
2069 if (!mark_lock(curr, hlock,
2070 LOCK_ENABLED_SOFTIRQS_READ, ip))
2073 if (!mark_lock(curr, hlock,
2074 LOCK_ENABLED_HARDIRQS, ip))
2076 if (curr->softirqs_enabled)
2077 if (!mark_lock(curr, hlock,
2078 LOCK_ENABLED_SOFTIRQS, ip))
2083 /* mark it as used: */
2084 if (!mark_lock(curr, hlock, LOCK_USED, ip))
2088 * Calculate the chain hash: it's the combined has of all the
2089 * lock keys along the dependency chain. We save the hash value
2090 * at every step so that we can get the current hash easily
2091 * after unlock. The chain hash is then used to cache dependency
2094 * The 'key ID' is what is the most compact key value to drive
2095 * the hash, not class->key.
2097 id = class - lock_classes;
2098 if (DEBUG_LOCKS_WARN_ON(id >= MAX_LOCKDEP_KEYS))
2101 chain_key = curr->curr_chain_key;
2103 if (DEBUG_LOCKS_WARN_ON(chain_key != 0))
2108 hlock->prev_chain_key = chain_key;
2110 #ifdef CONFIG_TRACE_IRQFLAGS
2112 * Keep track of points where we cross into an interrupt context:
2114 hlock->irq_context = 2*(curr->hardirq_context ? 1 : 0) +
2115 curr->softirq_context;
2117 struct held_lock *prev_hlock;
2119 prev_hlock = curr->held_locks + depth-1;
2121 * If we cross into another context, reset the
2122 * hash key (this also prevents the checking and the
2123 * adding of the dependency to 'prev'):
2125 if (prev_hlock->irq_context != hlock->irq_context) {
2131 chain_key = iterate_chain_key(chain_key, id);
2132 curr->curr_chain_key = chain_key;
2135 * Trylock needs to maintain the stack of held locks, but it
2136 * does not add new dependencies, because trylock can be done
2139 * We look up the chain_key and do the O(N^2) check and update of
2140 * the dependencies only if this is a new dependency chain.
2141 * (If lookup_chain_cache() returns with 1 it acquires
2144 if (!trylock && (check == 2) && lookup_chain_cache(chain_key)) {
2146 * Check whether last held lock:
2148 * - is irq-safe, if this lock is irq-unsafe
2149 * - is softirq-safe, if this lock is hardirq-unsafe
2151 * And check whether the new lock's dependency graph
2152 * could lead back to the previous lock.
2154 * any of these scenarios could lead to a deadlock. If
2157 int ret = check_deadlock(curr, hlock, lock, read);
2162 * Mark recursive read, as we jump over it when
2163 * building dependencies (just like we jump over
2169 * Add dependency only if this lock is not the head
2170 * of the chain, and if it's not a secondary read-lock:
2172 if (!chain_head && ret != 2)
2173 if (!check_prevs_add(curr, hlock))
2175 __raw_spin_unlock(&hash_lock);
2177 curr->lockdep_depth++;
2178 check_chain_key(curr);
2179 if (unlikely(curr->lockdep_depth >= MAX_LOCK_DEPTH)) {
2181 printk("BUG: MAX_LOCK_DEPTH too low!\n");
2182 printk("turning off the locking correctness validator.\n");
2185 if (unlikely(curr->lockdep_depth > max_lockdep_depth))
2186 max_lockdep_depth = curr->lockdep_depth;
2192 print_unlock_inbalance_bug(struct task_struct *curr, struct lockdep_map *lock,
2195 if (!debug_locks_off())
2197 if (debug_locks_silent)
2200 printk("\n=====================================\n");
2201 printk( "[ BUG: bad unlock balance detected! ]\n");
2202 printk( "-------------------------------------\n");
2203 printk("%s/%d is trying to release lock (",
2204 curr->comm, curr->pid);
2205 print_lockdep_cache(lock);
2208 printk("but there are no more locks to release!\n");
2209 printk("\nother info that might help us debug this:\n");
2210 lockdep_print_held_locks(curr);
2212 printk("\nstack backtrace:\n");
2219 * Common debugging checks for both nested and non-nested unlock:
2221 static int check_unlock(struct task_struct *curr, struct lockdep_map *lock,
2224 if (unlikely(!debug_locks))
2226 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2229 if (curr->lockdep_depth <= 0)
2230 return print_unlock_inbalance_bug(curr, lock, ip);
2236 * Remove the lock to the list of currently held locks in a
2237 * potentially non-nested (out of order) manner. This is a
2238 * relatively rare operation, as all the unlock APIs default
2239 * to nested mode (which uses lock_release()):
2242 lock_release_non_nested(struct task_struct *curr,
2243 struct lockdep_map *lock, unsigned long ip)
2245 struct held_lock *hlock, *prev_hlock;
2250 * Check whether the lock exists in the current stack
2253 depth = curr->lockdep_depth;
2254 if (DEBUG_LOCKS_WARN_ON(!depth))
2258 for (i = depth-1; i >= 0; i--) {
2259 hlock = curr->held_locks + i;
2261 * We must not cross into another context:
2263 if (prev_hlock && prev_hlock->irq_context != hlock->irq_context)
2265 if (hlock->instance == lock)
2269 return print_unlock_inbalance_bug(curr, lock, ip);
2273 * We have the right lock to unlock, 'hlock' points to it.
2274 * Now we remove it from the stack, and add back the other
2275 * entries (if any), recalculating the hash along the way:
2277 curr->lockdep_depth = i;
2278 curr->curr_chain_key = hlock->prev_chain_key;
2280 for (i++; i < depth; i++) {
2281 hlock = curr->held_locks + i;
2282 if (!__lock_acquire(hlock->instance,
2283 hlock->class->subclass, hlock->trylock,
2284 hlock->read, hlock->check, hlock->hardirqs_off,
2289 if (DEBUG_LOCKS_WARN_ON(curr->lockdep_depth != depth - 1))
2295 * Remove the lock to the list of currently held locks - this gets
2296 * called on mutex_unlock()/spin_unlock*() (or on a failed
2297 * mutex_lock_interruptible()). This is done for unlocks that nest
2298 * perfectly. (i.e. the current top of the lock-stack is unlocked)
2300 static int lock_release_nested(struct task_struct *curr,
2301 struct lockdep_map *lock, unsigned long ip)
2303 struct held_lock *hlock;
2307 * Pop off the top of the lock stack:
2309 depth = curr->lockdep_depth - 1;
2310 hlock = curr->held_locks + depth;
2313 * Is the unlock non-nested:
2315 if (hlock->instance != lock)
2316 return lock_release_non_nested(curr, lock, ip);
2317 curr->lockdep_depth--;
2319 if (DEBUG_LOCKS_WARN_ON(!depth && (hlock->prev_chain_key != 0)))
2322 curr->curr_chain_key = hlock->prev_chain_key;
2324 #ifdef CONFIG_DEBUG_LOCKDEP
2325 hlock->prev_chain_key = 0;
2326 hlock->class = NULL;
2327 hlock->acquire_ip = 0;
2328 hlock->irq_context = 0;
2334 * Remove the lock to the list of currently held locks - this gets
2335 * called on mutex_unlock()/spin_unlock*() (or on a failed
2336 * mutex_lock_interruptible()). This is done for unlocks that nest
2337 * perfectly. (i.e. the current top of the lock-stack is unlocked)
2340 __lock_release(struct lockdep_map *lock, int nested, unsigned long ip)
2342 struct task_struct *curr = current;
2344 if (!check_unlock(curr, lock, ip))
2348 if (!lock_release_nested(curr, lock, ip))
2351 if (!lock_release_non_nested(curr, lock, ip))
2355 check_chain_key(curr);
2359 * Check whether we follow the irq-flags state precisely:
2361 static void check_flags(unsigned long flags)
2363 #if defined(CONFIG_DEBUG_LOCKDEP) && defined(CONFIG_TRACE_IRQFLAGS)
2367 if (irqs_disabled_flags(flags))
2368 DEBUG_LOCKS_WARN_ON(current->hardirqs_enabled);
2370 DEBUG_LOCKS_WARN_ON(!current->hardirqs_enabled);
2373 * We dont accurately track softirq state in e.g.
2374 * hardirq contexts (such as on 4KSTACKS), so only
2375 * check if not in hardirq contexts:
2377 if (!hardirq_count()) {
2378 if (softirq_count())
2379 DEBUG_LOCKS_WARN_ON(current->softirqs_enabled);
2381 DEBUG_LOCKS_WARN_ON(!current->softirqs_enabled);
2385 print_irqtrace_events(current);
2390 * We are not always called with irqs disabled - do that here,
2391 * and also avoid lockdep recursion:
2393 void lock_acquire(struct lockdep_map *lock, unsigned int subclass,
2394 int trylock, int read, int check, unsigned long ip)
2396 unsigned long flags;
2398 if (unlikely(current->lockdep_recursion))
2401 raw_local_irq_save(flags);
2404 current->lockdep_recursion = 1;
2405 __lock_acquire(lock, subclass, trylock, read, check,
2406 irqs_disabled_flags(flags), ip);
2407 current->lockdep_recursion = 0;
2408 raw_local_irq_restore(flags);
2411 EXPORT_SYMBOL_GPL(lock_acquire);
2413 void lock_release(struct lockdep_map *lock, int nested, unsigned long ip)
2415 unsigned long flags;
2417 if (unlikely(current->lockdep_recursion))
2420 raw_local_irq_save(flags);
2422 current->lockdep_recursion = 1;
2423 __lock_release(lock, nested, ip);
2424 current->lockdep_recursion = 0;
2425 raw_local_irq_restore(flags);
2428 EXPORT_SYMBOL_GPL(lock_release);
2431 * Used by the testsuite, sanitize the validator state
2432 * after a simulated failure:
2435 void lockdep_reset(void)
2437 unsigned long flags;
2439 raw_local_irq_save(flags);
2440 current->curr_chain_key = 0;
2441 current->lockdep_depth = 0;
2442 current->lockdep_recursion = 0;
2443 memset(current->held_locks, 0, MAX_LOCK_DEPTH*sizeof(struct held_lock));
2444 nr_hardirq_chains = 0;
2445 nr_softirq_chains = 0;
2446 nr_process_chains = 0;
2448 raw_local_irq_restore(flags);
2451 static void zap_class(struct lock_class *class)
2456 * Remove all dependencies this lock is
2459 for (i = 0; i < nr_list_entries; i++) {
2460 if (list_entries[i].class == class)
2461 list_del_rcu(&list_entries[i].entry);
2464 * Unhash the class and remove it from the all_lock_classes list:
2466 list_del_rcu(&class->hash_entry);
2467 list_del_rcu(&class->lock_entry);
2471 static inline int within(void *addr, void *start, unsigned long size)
2473 return addr >= start && addr < start + size;
2476 void lockdep_free_key_range(void *start, unsigned long size)
2478 struct lock_class *class, *next;
2479 struct list_head *head;
2480 unsigned long flags;
2483 raw_local_irq_save(flags);
2484 __raw_spin_lock(&hash_lock);
2487 * Unhash all classes that were created by this module:
2489 for (i = 0; i < CLASSHASH_SIZE; i++) {
2490 head = classhash_table + i;
2491 if (list_empty(head))
2493 list_for_each_entry_safe(class, next, head, hash_entry)
2494 if (within(class->key, start, size))
2498 __raw_spin_unlock(&hash_lock);
2499 raw_local_irq_restore(flags);
2502 void lockdep_reset_lock(struct lockdep_map *lock)
2504 struct lock_class *class, *next;
2505 struct list_head *head;
2506 unsigned long flags;
2509 raw_local_irq_save(flags);
2512 * Remove all classes this lock might have:
2514 for (j = 0; j < MAX_LOCKDEP_SUBCLASSES; j++) {
2516 * If the class exists we look it up and zap it:
2518 class = look_up_lock_class(lock, j);
2523 * Debug check: in the end all mapped classes should
2526 __raw_spin_lock(&hash_lock);
2527 for (i = 0; i < CLASSHASH_SIZE; i++) {
2528 head = classhash_table + i;
2529 if (list_empty(head))
2531 list_for_each_entry_safe(class, next, head, hash_entry) {
2532 if (unlikely(class == lock->class_cache)) {
2533 __raw_spin_unlock(&hash_lock);
2534 DEBUG_LOCKS_WARN_ON(1);
2539 __raw_spin_unlock(&hash_lock);
2542 raw_local_irq_restore(flags);
2545 void __init lockdep_init(void)
2550 * Some architectures have their own start_kernel()
2551 * code which calls lockdep_init(), while we also
2552 * call lockdep_init() from the start_kernel() itself,
2553 * and we want to initialize the hashes only once:
2555 if (lockdep_initialized)
2558 for (i = 0; i < CLASSHASH_SIZE; i++)
2559 INIT_LIST_HEAD(classhash_table + i);
2561 for (i = 0; i < CHAINHASH_SIZE; i++)
2562 INIT_LIST_HEAD(chainhash_table + i);
2564 lockdep_initialized = 1;
2567 void __init lockdep_info(void)
2569 printk("Lock dependency validator: Copyright (c) 2006 Red Hat, Inc., Ingo Molnar\n");
2571 printk("... MAX_LOCKDEP_SUBCLASSES: %lu\n", MAX_LOCKDEP_SUBCLASSES);
2572 printk("... MAX_LOCK_DEPTH: %lu\n", MAX_LOCK_DEPTH);
2573 printk("... MAX_LOCKDEP_KEYS: %lu\n", MAX_LOCKDEP_KEYS);
2574 printk("... CLASSHASH_SIZE: %lu\n", CLASSHASH_SIZE);
2575 printk("... MAX_LOCKDEP_ENTRIES: %lu\n", MAX_LOCKDEP_ENTRIES);
2576 printk("... MAX_LOCKDEP_CHAINS: %lu\n", MAX_LOCKDEP_CHAINS);
2577 printk("... CHAINHASH_SIZE: %lu\n", CHAINHASH_SIZE);
2579 printk(" memory used by lock dependency info: %lu kB\n",
2580 (sizeof(struct lock_class) * MAX_LOCKDEP_KEYS +
2581 sizeof(struct list_head) * CLASSHASH_SIZE +
2582 sizeof(struct lock_list) * MAX_LOCKDEP_ENTRIES +
2583 sizeof(struct lock_chain) * MAX_LOCKDEP_CHAINS +
2584 sizeof(struct list_head) * CHAINHASH_SIZE) / 1024);
2586 printk(" per task-struct memory footprint: %lu bytes\n",
2587 sizeof(struct held_lock) * MAX_LOCK_DEPTH);
2589 #ifdef CONFIG_DEBUG_LOCKDEP
2590 if (lockdep_init_error)
2591 printk("WARNING: lockdep init error! Arch code didnt call lockdep_init() early enough?\n");
2595 static inline int in_range(const void *start, const void *addr, const void *end)
2597 return addr >= start && addr <= end;
2601 print_freed_lock_bug(struct task_struct *curr, const void *mem_from,
2602 const void *mem_to, struct held_lock *hlock)
2604 if (!debug_locks_off())
2606 if (debug_locks_silent)
2609 printk("\n=========================\n");
2610 printk( "[ BUG: held lock freed! ]\n");
2611 printk( "-------------------------\n");
2612 printk("%s/%d is freeing memory %p-%p, with a lock still held there!\n",
2613 curr->comm, curr->pid, mem_from, mem_to-1);
2615 lockdep_print_held_locks(curr);
2617 printk("\nstack backtrace:\n");
2622 * Called when kernel memory is freed (or unmapped), or if a lock
2623 * is destroyed or reinitialized - this code checks whether there is
2624 * any held lock in the memory range of <from> to <to>:
2626 void debug_check_no_locks_freed(const void *mem_from, unsigned long mem_len)
2628 const void *mem_to = mem_from + mem_len, *lock_from, *lock_to;
2629 struct task_struct *curr = current;
2630 struct held_lock *hlock;
2631 unsigned long flags;
2634 if (unlikely(!debug_locks))
2637 local_irq_save(flags);
2638 for (i = 0; i < curr->lockdep_depth; i++) {
2639 hlock = curr->held_locks + i;
2641 lock_from = (void *)hlock->instance;
2642 lock_to = (void *)(hlock->instance + 1);
2644 if (!in_range(mem_from, lock_from, mem_to) &&
2645 !in_range(mem_from, lock_to, mem_to))
2648 print_freed_lock_bug(curr, mem_from, mem_to, hlock);
2651 local_irq_restore(flags);
2653 EXPORT_SYMBOL_GPL(debug_check_no_locks_freed);
2655 static void print_held_locks_bug(struct task_struct *curr)
2657 if (!debug_locks_off())
2659 if (debug_locks_silent)
2662 printk("\n=====================================\n");
2663 printk( "[ BUG: lock held at task exit time! ]\n");
2664 printk( "-------------------------------------\n");
2665 printk("%s/%d is exiting with locks still held!\n",
2666 curr->comm, curr->pid);
2667 lockdep_print_held_locks(curr);
2669 printk("\nstack backtrace:\n");
2673 void debug_check_no_locks_held(struct task_struct *task)
2675 if (unlikely(task->lockdep_depth > 0))
2676 print_held_locks_bug(task);
2679 void debug_show_all_locks(void)
2681 struct task_struct *g, *p;
2685 printk("\nShowing all locks held in the system:\n");
2688 * Here we try to get the tasklist_lock as hard as possible,
2689 * if not successful after 2 seconds we ignore it (but keep
2690 * trying). This is to enable a debug printout even if a
2691 * tasklist_lock-holding task deadlocks or crashes.
2694 if (!read_trylock(&tasklist_lock)) {
2696 printk("hm, tasklist_lock locked, retrying... ");
2699 printk(" #%d", 10-count);
2703 printk(" ignoring it.\n");
2707 printk(" locked it.\n");
2709 do_each_thread(g, p) {
2710 if (p->lockdep_depth)
2711 lockdep_print_held_locks(p);
2713 if (read_trylock(&tasklist_lock))
2715 } while_each_thread(g, p);
2718 printk("=============================================\n\n");
2721 read_unlock(&tasklist_lock);
2724 EXPORT_SYMBOL_GPL(debug_show_all_locks);
2726 void debug_show_held_locks(struct task_struct *task)
2728 lockdep_print_held_locks(task);
2731 EXPORT_SYMBOL_GPL(debug_show_held_locks);