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 save_stack_trace(trace
, NULL
);
233 trace
->max_entries
= trace
->nr_entries
;
235 nr_stack_trace_entries
+= trace
->nr_entries
;
236 if (DEBUG_LOCKS_WARN_ON(nr_stack_trace_entries
> MAX_STACK_TRACE_ENTRIES
))
239 if (nr_stack_trace_entries
== MAX_STACK_TRACE_ENTRIES
) {
240 __raw_spin_unlock(&hash_lock
);
241 if (debug_locks_off()) {
242 printk("BUG: MAX_STACK_TRACE_ENTRIES too low!\n");
243 printk("turning off the locking correctness validator.\n");
252 unsigned int nr_hardirq_chains
;
253 unsigned int nr_softirq_chains
;
254 unsigned int nr_process_chains
;
255 unsigned int max_lockdep_depth
;
256 unsigned int max_recursion_depth
;
258 #ifdef CONFIG_DEBUG_LOCKDEP
260 * We cannot printk in early bootup code. Not even early_printk()
261 * might work. So we mark any initialization errors and printk
262 * about it later on, in lockdep_info().
264 static int lockdep_init_error
;
267 * Various lockdep statistics:
269 atomic_t chain_lookup_hits
;
270 atomic_t chain_lookup_misses
;
271 atomic_t hardirqs_on_events
;
272 atomic_t hardirqs_off_events
;
273 atomic_t redundant_hardirqs_on
;
274 atomic_t redundant_hardirqs_off
;
275 atomic_t softirqs_on_events
;
276 atomic_t softirqs_off_events
;
277 atomic_t redundant_softirqs_on
;
278 atomic_t redundant_softirqs_off
;
279 atomic_t nr_unused_locks
;
280 atomic_t nr_cyclic_checks
;
281 atomic_t nr_cyclic_check_recursions
;
282 atomic_t nr_find_usage_forwards_checks
;
283 atomic_t nr_find_usage_forwards_recursions
;
284 atomic_t nr_find_usage_backwards_checks
;
285 atomic_t nr_find_usage_backwards_recursions
;
286 # define debug_atomic_inc(ptr) atomic_inc(ptr)
287 # define debug_atomic_dec(ptr) atomic_dec(ptr)
288 # define debug_atomic_read(ptr) atomic_read(ptr)
290 # define debug_atomic_inc(ptr) do { } while (0)
291 # define debug_atomic_dec(ptr) do { } while (0)
292 # define debug_atomic_read(ptr) 0
299 static const char *usage_str
[] =
301 [LOCK_USED
] = "initial-use ",
302 [LOCK_USED_IN_HARDIRQ
] = "in-hardirq-W",
303 [LOCK_USED_IN_SOFTIRQ
] = "in-softirq-W",
304 [LOCK_ENABLED_SOFTIRQS
] = "softirq-on-W",
305 [LOCK_ENABLED_HARDIRQS
] = "hardirq-on-W",
306 [LOCK_USED_IN_HARDIRQ_READ
] = "in-hardirq-R",
307 [LOCK_USED_IN_SOFTIRQ_READ
] = "in-softirq-R",
308 [LOCK_ENABLED_SOFTIRQS_READ
] = "softirq-on-R",
309 [LOCK_ENABLED_HARDIRQS_READ
] = "hardirq-on-R",
312 const char * __get_key_name(struct lockdep_subclass_key
*key
, char *str
)
314 unsigned long offs
, size
;
317 return kallsyms_lookup((unsigned long)key
, &size
, &offs
, &modname
, str
);
321 get_usage_chars(struct lock_class
*class, char *c1
, char *c2
, char *c3
, char *c4
)
323 *c1
= '.', *c2
= '.', *c3
= '.', *c4
= '.';
325 if (class->usage_mask
& LOCKF_USED_IN_HARDIRQ
)
328 if (class->usage_mask
& LOCKF_ENABLED_HARDIRQS
)
331 if (class->usage_mask
& LOCKF_USED_IN_SOFTIRQ
)
334 if (class->usage_mask
& LOCKF_ENABLED_SOFTIRQS
)
337 if (class->usage_mask
& LOCKF_ENABLED_HARDIRQS_READ
)
339 if (class->usage_mask
& LOCKF_USED_IN_HARDIRQ_READ
) {
341 if (class->usage_mask
& LOCKF_ENABLED_HARDIRQS_READ
)
345 if (class->usage_mask
& LOCKF_ENABLED_SOFTIRQS_READ
)
347 if (class->usage_mask
& LOCKF_USED_IN_SOFTIRQ_READ
) {
349 if (class->usage_mask
& LOCKF_ENABLED_SOFTIRQS_READ
)
354 static void print_lock_name(struct lock_class
*class)
356 char str
[128], c1
, c2
, c3
, c4
;
359 get_usage_chars(class, &c1
, &c2
, &c3
, &c4
);
363 name
= __get_key_name(class->key
, str
);
364 printk(" (%s", name
);
366 printk(" (%s", name
);
367 if (class->name_version
> 1)
368 printk("#%d", class->name_version
);
370 printk("/%d", class->subclass
);
372 printk("){%c%c%c%c}", c1
, c2
, c3
, c4
);
375 static void print_lockdep_cache(struct lockdep_map
*lock
)
382 name
= __get_key_name(lock
->key
->subkeys
, str
);
387 static void print_lock(struct held_lock
*hlock
)
389 print_lock_name(hlock
->class);
391 print_ip_sym(hlock
->acquire_ip
);
394 static void lockdep_print_held_locks(struct task_struct
*curr
)
396 int i
, depth
= curr
->lockdep_depth
;
399 printk("no locks held by %s/%d.\n", curr
->comm
, curr
->pid
);
402 printk("%d lock%s held by %s/%d:\n",
403 depth
, depth
> 1 ? "s" : "", curr
->comm
, curr
->pid
);
405 for (i
= 0; i
< depth
; i
++) {
407 print_lock(curr
->held_locks
+ i
);
411 static void print_lock_class_header(struct lock_class
*class, int depth
)
415 printk("%*s->", depth
, "");
416 print_lock_name(class);
417 printk(" ops: %lu", class->ops
);
420 for (bit
= 0; bit
< LOCK_USAGE_STATES
; bit
++) {
421 if (class->usage_mask
& (1 << bit
)) {
424 len
+= printk("%*s %s", depth
, "", usage_str
[bit
]);
425 len
+= printk(" at:\n");
426 print_stack_trace(class->usage_traces
+ bit
, len
);
429 printk("%*s }\n", depth
, "");
431 printk("%*s ... key at: ",depth
,"");
432 print_ip_sym((unsigned long)class->key
);
436 * printk all lock dependencies starting at <entry>:
438 static void print_lock_dependencies(struct lock_class
*class, int depth
)
440 struct lock_list
*entry
;
442 if (DEBUG_LOCKS_WARN_ON(depth
>= 20))
445 print_lock_class_header(class, depth
);
447 list_for_each_entry(entry
, &class->locks_after
, entry
) {
448 DEBUG_LOCKS_WARN_ON(!entry
->class);
449 print_lock_dependencies(entry
->class, depth
+ 1);
451 printk("%*s ... acquired at:\n",depth
,"");
452 print_stack_trace(&entry
->trace
, 2);
458 * Add a new dependency to the head of the list:
460 static int add_lock_to_list(struct lock_class
*class, struct lock_class
*this,
461 struct list_head
*head
, unsigned long ip
)
463 struct lock_list
*entry
;
465 * Lock not present yet - get a new dependency struct and
466 * add it to the list:
468 entry
= alloc_list_entry();
473 save_trace(&entry
->trace
);
476 * Since we never remove from the dependency list, the list can
477 * be walked lockless by other CPUs, it's only allocation
478 * that must be protected by the spinlock. But this also means
479 * we must make new entries visible only once writes to the
480 * entry become visible - hence the RCU op:
482 list_add_tail_rcu(&entry
->entry
, head
);
488 * Recursive, forwards-direction lock-dependency checking, used for
489 * both noncyclic checking and for hardirq-unsafe/softirq-unsafe
492 * (to keep the stackframe of the recursive functions small we
493 * use these global variables, and we also mark various helper
494 * functions as noinline.)
496 static struct held_lock
*check_source
, *check_target
;
499 * Print a dependency chain entry (this is only done when a deadlock
500 * has been detected):
503 print_circular_bug_entry(struct lock_list
*target
, unsigned int depth
)
505 if (debug_locks_silent
)
507 printk("\n-> #%u", depth
);
508 print_lock_name(target
->class);
510 print_stack_trace(&target
->trace
, 6);
515 static void print_kernel_version(void)
517 printk("%s %.*s\n", init_utsname()->release
,
518 (int)strcspn(init_utsname()->version
, " "),
519 init_utsname()->version
);
523 * When a circular dependency is detected, print the
527 print_circular_bug_header(struct lock_list
*entry
, unsigned int depth
)
529 struct task_struct
*curr
= current
;
531 __raw_spin_unlock(&hash_lock
);
533 if (debug_locks_silent
)
536 printk("\n=======================================================\n");
537 printk( "[ INFO: possible circular locking dependency detected ]\n");
538 print_kernel_version();
539 printk( "-------------------------------------------------------\n");
540 printk("%s/%d is trying to acquire lock:\n",
541 curr
->comm
, curr
->pid
);
542 print_lock(check_source
);
543 printk("\nbut task is already holding lock:\n");
544 print_lock(check_target
);
545 printk("\nwhich lock already depends on the new lock.\n\n");
546 printk("\nthe existing dependency chain (in reverse order) is:\n");
548 print_circular_bug_entry(entry
, depth
);
553 static noinline
int print_circular_bug_tail(void)
555 struct task_struct
*curr
= current
;
556 struct lock_list
this;
558 if (debug_locks_silent
)
561 this.class = check_source
->class;
562 save_trace(&this.trace
);
563 print_circular_bug_entry(&this, 0);
565 printk("\nother info that might help us debug this:\n\n");
566 lockdep_print_held_locks(curr
);
568 printk("\nstack backtrace:\n");
574 #define RECURSION_LIMIT 40
576 static int noinline
print_infinite_recursion_bug(void)
578 __raw_spin_unlock(&hash_lock
);
579 DEBUG_LOCKS_WARN_ON(1);
585 * Prove that the dependency graph starting at <entry> can not
586 * lead to <target>. Print an error and return 0 if it does.
589 check_noncircular(struct lock_class
*source
, unsigned int depth
)
591 struct lock_list
*entry
;
593 debug_atomic_inc(&nr_cyclic_check_recursions
);
594 if (depth
> max_recursion_depth
)
595 max_recursion_depth
= depth
;
596 if (depth
>= RECURSION_LIMIT
)
597 return print_infinite_recursion_bug();
599 * Check this lock's dependency list:
601 list_for_each_entry(entry
, &source
->locks_after
, entry
) {
602 if (entry
->class == check_target
->class)
603 return print_circular_bug_header(entry
, depth
+1);
604 debug_atomic_inc(&nr_cyclic_checks
);
605 if (!check_noncircular(entry
->class, depth
+1))
606 return print_circular_bug_entry(entry
, depth
+1);
611 static int very_verbose(struct lock_class
*class)
614 return class_filter(class);
618 #ifdef CONFIG_TRACE_IRQFLAGS
621 * Forwards and backwards subgraph searching, for the purposes of
622 * proving that two subgraphs can be connected by a new dependency
623 * without creating any illegal irq-safe -> irq-unsafe lock dependency.
625 static enum lock_usage_bit find_usage_bit
;
626 static struct lock_class
*forwards_match
, *backwards_match
;
629 * Find a node in the forwards-direction dependency sub-graph starting
630 * at <source> that matches <find_usage_bit>.
632 * Return 2 if such a node exists in the subgraph, and put that node
633 * into <forwards_match>.
635 * Return 1 otherwise and keep <forwards_match> unchanged.
639 find_usage_forwards(struct lock_class
*source
, unsigned int depth
)
641 struct lock_list
*entry
;
644 if (depth
> max_recursion_depth
)
645 max_recursion_depth
= depth
;
646 if (depth
>= RECURSION_LIMIT
)
647 return print_infinite_recursion_bug();
649 debug_atomic_inc(&nr_find_usage_forwards_checks
);
650 if (source
->usage_mask
& (1 << find_usage_bit
)) {
651 forwards_match
= source
;
656 * Check this lock's dependency list:
658 list_for_each_entry(entry
, &source
->locks_after
, entry
) {
659 debug_atomic_inc(&nr_find_usage_forwards_recursions
);
660 ret
= find_usage_forwards(entry
->class, depth
+1);
661 if (ret
== 2 || ret
== 0)
668 * Find a node in the backwards-direction dependency sub-graph starting
669 * at <source> that matches <find_usage_bit>.
671 * Return 2 if such a node exists in the subgraph, and put that node
672 * into <backwards_match>.
674 * Return 1 otherwise and keep <backwards_match> unchanged.
678 find_usage_backwards(struct lock_class
*source
, unsigned int depth
)
680 struct lock_list
*entry
;
683 if (depth
> max_recursion_depth
)
684 max_recursion_depth
= depth
;
685 if (depth
>= RECURSION_LIMIT
)
686 return print_infinite_recursion_bug();
688 debug_atomic_inc(&nr_find_usage_backwards_checks
);
689 if (source
->usage_mask
& (1 << find_usage_bit
)) {
690 backwards_match
= source
;
695 * Check this lock's dependency list:
697 list_for_each_entry(entry
, &source
->locks_before
, entry
) {
698 debug_atomic_inc(&nr_find_usage_backwards_recursions
);
699 ret
= find_usage_backwards(entry
->class, depth
+1);
700 if (ret
== 2 || ret
== 0)
707 print_bad_irq_dependency(struct task_struct
*curr
,
708 struct held_lock
*prev
,
709 struct held_lock
*next
,
710 enum lock_usage_bit bit1
,
711 enum lock_usage_bit bit2
,
712 const char *irqclass
)
714 __raw_spin_unlock(&hash_lock
);
716 if (debug_locks_silent
)
719 printk("\n======================================================\n");
720 printk( "[ INFO: %s-safe -> %s-unsafe lock order detected ]\n",
722 print_kernel_version();
723 printk( "------------------------------------------------------\n");
724 printk("%s/%d [HC%u[%lu]:SC%u[%lu]:HE%u:SE%u] is trying to acquire:\n",
725 curr
->comm
, curr
->pid
,
726 curr
->hardirq_context
, hardirq_count() >> HARDIRQ_SHIFT
,
727 curr
->softirq_context
, softirq_count() >> SOFTIRQ_SHIFT
,
728 curr
->hardirqs_enabled
,
729 curr
->softirqs_enabled
);
732 printk("\nand this task is already holding:\n");
734 printk("which would create a new lock dependency:\n");
735 print_lock_name(prev
->class);
737 print_lock_name(next
->class);
740 printk("\nbut this new dependency connects a %s-irq-safe lock:\n",
742 print_lock_name(backwards_match
);
743 printk("\n... which became %s-irq-safe at:\n", irqclass
);
745 print_stack_trace(backwards_match
->usage_traces
+ bit1
, 1);
747 printk("\nto a %s-irq-unsafe lock:\n", irqclass
);
748 print_lock_name(forwards_match
);
749 printk("\n... which became %s-irq-unsafe at:\n", irqclass
);
752 print_stack_trace(forwards_match
->usage_traces
+ bit2
, 1);
754 printk("\nother info that might help us debug this:\n\n");
755 lockdep_print_held_locks(curr
);
757 printk("\nthe %s-irq-safe lock's dependencies:\n", irqclass
);
758 print_lock_dependencies(backwards_match
, 0);
760 printk("\nthe %s-irq-unsafe lock's dependencies:\n", irqclass
);
761 print_lock_dependencies(forwards_match
, 0);
763 printk("\nstack backtrace:\n");
770 check_usage(struct task_struct
*curr
, struct held_lock
*prev
,
771 struct held_lock
*next
, enum lock_usage_bit bit_backwards
,
772 enum lock_usage_bit bit_forwards
, const char *irqclass
)
776 find_usage_bit
= bit_backwards
;
777 /* fills in <backwards_match> */
778 ret
= find_usage_backwards(prev
->class, 0);
779 if (!ret
|| ret
== 1)
782 find_usage_bit
= bit_forwards
;
783 ret
= find_usage_forwards(next
->class, 0);
784 if (!ret
|| ret
== 1)
787 return print_bad_irq_dependency(curr
, prev
, next
,
788 bit_backwards
, bit_forwards
, irqclass
);
794 print_deadlock_bug(struct task_struct
*curr
, struct held_lock
*prev
,
795 struct held_lock
*next
)
798 __raw_spin_unlock(&hash_lock
);
799 if (debug_locks_silent
)
802 printk("\n=============================================\n");
803 printk( "[ INFO: possible recursive locking detected ]\n");
804 print_kernel_version();
805 printk( "---------------------------------------------\n");
806 printk("%s/%d is trying to acquire lock:\n",
807 curr
->comm
, curr
->pid
);
809 printk("\nbut task is already holding lock:\n");
812 printk("\nother info that might help us debug this:\n");
813 lockdep_print_held_locks(curr
);
815 printk("\nstack backtrace:\n");
822 * Check whether we are holding such a class already.
824 * (Note that this has to be done separately, because the graph cannot
825 * detect such classes of deadlocks.)
827 * Returns: 0 on deadlock detected, 1 on OK, 2 on recursive read
830 check_deadlock(struct task_struct
*curr
, struct held_lock
*next
,
831 struct lockdep_map
*next_instance
, int read
)
833 struct held_lock
*prev
;
836 for (i
= 0; i
< curr
->lockdep_depth
; i
++) {
837 prev
= curr
->held_locks
+ i
;
838 if (prev
->class != next
->class)
841 * Allow read-after-read recursion of the same
842 * lock class (i.e. read_lock(lock)+read_lock(lock)):
844 if ((read
== 2) && prev
->read
)
846 return print_deadlock_bug(curr
, prev
, next
);
852 * There was a chain-cache miss, and we are about to add a new dependency
853 * to a previous lock. We recursively validate the following rules:
855 * - would the adding of the <prev> -> <next> dependency create a
856 * circular dependency in the graph? [== circular deadlock]
858 * - does the new prev->next dependency connect any hardirq-safe lock
859 * (in the full backwards-subgraph starting at <prev>) with any
860 * hardirq-unsafe lock (in the full forwards-subgraph starting at
861 * <next>)? [== illegal lock inversion with hardirq contexts]
863 * - does the new prev->next dependency connect any softirq-safe lock
864 * (in the full backwards-subgraph starting at <prev>) with any
865 * softirq-unsafe lock (in the full forwards-subgraph starting at
866 * <next>)? [== illegal lock inversion with softirq contexts]
868 * any of these scenarios could lead to a deadlock.
870 * Then if all the validations pass, we add the forwards and backwards
874 check_prev_add(struct task_struct
*curr
, struct held_lock
*prev
,
875 struct held_lock
*next
)
877 struct lock_list
*entry
;
881 * Prove that the new <prev> -> <next> dependency would not
882 * create a circular dependency in the graph. (We do this by
883 * forward-recursing into the graph starting at <next>, and
884 * checking whether we can reach <prev>.)
886 * We are using global variables to control the recursion, to
887 * keep the stackframe size of the recursive functions low:
891 if (!(check_noncircular(next
->class, 0)))
892 return print_circular_bug_tail();
894 #ifdef CONFIG_TRACE_IRQFLAGS
896 * Prove that the new dependency does not connect a hardirq-safe
897 * lock with a hardirq-unsafe lock - to achieve this we search
898 * the backwards-subgraph starting at <prev>, and the
899 * forwards-subgraph starting at <next>:
901 if (!check_usage(curr
, prev
, next
, LOCK_USED_IN_HARDIRQ
,
902 LOCK_ENABLED_HARDIRQS
, "hard"))
906 * Prove that the new dependency does not connect a hardirq-safe-read
907 * lock with a hardirq-unsafe lock - to achieve this we search
908 * the backwards-subgraph starting at <prev>, and the
909 * forwards-subgraph starting at <next>:
911 if (!check_usage(curr
, prev
, next
, LOCK_USED_IN_HARDIRQ_READ
,
912 LOCK_ENABLED_HARDIRQS
, "hard-read"))
916 * Prove that the new dependency does not connect a softirq-safe
917 * lock with a softirq-unsafe lock - to achieve this we search
918 * the backwards-subgraph starting at <prev>, and the
919 * forwards-subgraph starting at <next>:
921 if (!check_usage(curr
, prev
, next
, LOCK_USED_IN_SOFTIRQ
,
922 LOCK_ENABLED_SOFTIRQS
, "soft"))
925 * Prove that the new dependency does not connect a softirq-safe-read
926 * lock with a softirq-unsafe lock - to achieve this we search
927 * the backwards-subgraph starting at <prev>, and the
928 * forwards-subgraph starting at <next>:
930 if (!check_usage(curr
, prev
, next
, LOCK_USED_IN_SOFTIRQ_READ
,
931 LOCK_ENABLED_SOFTIRQS
, "soft"))
935 * For recursive read-locks we do all the dependency checks,
936 * but we dont store read-triggered dependencies (only
937 * write-triggered dependencies). This ensures that only the
938 * write-side dependencies matter, and that if for example a
939 * write-lock never takes any other locks, then the reads are
940 * equivalent to a NOP.
942 if (next
->read
== 2 || prev
->read
== 2)
945 * Is the <prev> -> <next> dependency already present?
947 * (this may occur even though this is a new chain: consider
948 * e.g. the L1 -> L2 -> L3 -> L4 and the L5 -> L1 -> L2 -> L3
949 * chains - the second one will be new, but L1 already has
950 * L2 added to its dependency list, due to the first chain.)
952 list_for_each_entry(entry
, &prev
->class->locks_after
, entry
) {
953 if (entry
->class == next
->class)
958 * Ok, all validations passed, add the new lock
959 * to the previous lock's dependency list:
961 ret
= add_lock_to_list(prev
->class, next
->class,
962 &prev
->class->locks_after
, next
->acquire_ip
);
966 * Return value of 2 signals 'dependency already added',
967 * in that case we dont have to add the backlink either.
971 ret
= add_lock_to_list(next
->class, prev
->class,
972 &next
->class->locks_before
, next
->acquire_ip
);
975 * Debugging printouts:
977 if (verbose(prev
->class) || verbose(next
->class)) {
978 __raw_spin_unlock(&hash_lock
);
979 printk("\n new dependency: ");
980 print_lock_name(prev
->class);
982 print_lock_name(next
->class);
985 __raw_spin_lock(&hash_lock
);
991 * Add the dependency to all directly-previous locks that are 'relevant'.
992 * The ones that are relevant are (in increasing distance from curr):
993 * all consecutive trylock entries and the final non-trylock entry - or
994 * the end of this context's lock-chain - whichever comes first.
997 check_prevs_add(struct task_struct
*curr
, struct held_lock
*next
)
999 int depth
= curr
->lockdep_depth
;
1000 struct held_lock
*hlock
;
1005 * Depth must not be zero for a non-head lock:
1010 * At least two relevant locks must exist for this
1013 if (curr
->held_locks
[depth
].irq_context
!=
1014 curr
->held_locks
[depth
-1].irq_context
)
1018 hlock
= curr
->held_locks
+ depth
-1;
1020 * Only non-recursive-read entries get new dependencies
1023 if (hlock
->read
!= 2) {
1024 check_prev_add(curr
, hlock
, next
);
1026 * Stop after the first non-trylock entry,
1027 * as non-trylock entries have added their
1028 * own direct dependencies already, so this
1029 * lock is connected to them indirectly:
1031 if (!hlock
->trylock
)
1036 * End of lock-stack?
1041 * Stop the search if we cross into another context:
1043 if (curr
->held_locks
[depth
].irq_context
!=
1044 curr
->held_locks
[depth
-1].irq_context
)
1049 __raw_spin_unlock(&hash_lock
);
1050 DEBUG_LOCKS_WARN_ON(1);
1057 * Is this the address of a static object:
1059 static int static_obj(void *obj
)
1061 unsigned long start
= (unsigned long) &_stext
,
1062 end
= (unsigned long) &_end
,
1063 addr
= (unsigned long) obj
;
1071 if ((addr
>= start
) && (addr
< end
))
1078 for_each_possible_cpu(i
) {
1079 start
= (unsigned long) &__per_cpu_start
+ per_cpu_offset(i
);
1080 end
= (unsigned long) &__per_cpu_start
+ PERCPU_ENOUGH_ROOM
1081 + per_cpu_offset(i
);
1083 if ((addr
>= start
) && (addr
< end
))
1091 return is_module_address(addr
);
1095 * To make lock name printouts unique, we calculate a unique
1096 * class->name_version generation counter:
1098 static int count_matching_names(struct lock_class
*new_class
)
1100 struct lock_class
*class;
1103 if (!new_class
->name
)
1106 list_for_each_entry(class, &all_lock_classes
, lock_entry
) {
1107 if (new_class
->key
- new_class
->subclass
== class->key
)
1108 return class->name_version
;
1109 if (class->name
&& !strcmp(class->name
, new_class
->name
))
1110 count
= max(count
, class->name_version
);
1117 * Register a lock's class in the hash-table, if the class is not present
1118 * yet. Otherwise we look it up. We cache the result in the lock object
1119 * itself, so actual lookup of the hash should be once per lock object.
1121 static inline struct lock_class
*
1122 look_up_lock_class(struct lockdep_map
*lock
, unsigned int subclass
)
1124 struct lockdep_subclass_key
*key
;
1125 struct list_head
*hash_head
;
1126 struct lock_class
*class;
1128 #ifdef CONFIG_DEBUG_LOCKDEP
1130 * If the architecture calls into lockdep before initializing
1131 * the hashes then we'll warn about it later. (we cannot printk
1134 if (unlikely(!lockdep_initialized
)) {
1136 lockdep_init_error
= 1;
1141 * Static locks do not have their class-keys yet - for them the key
1142 * is the lock object itself:
1144 if (unlikely(!lock
->key
))
1145 lock
->key
= (void *)lock
;
1148 * NOTE: the class-key must be unique. For dynamic locks, a static
1149 * lock_class_key variable is passed in through the mutex_init()
1150 * (or spin_lock_init()) call - which acts as the key. For static
1151 * locks we use the lock object itself as the key.
1153 BUILD_BUG_ON(sizeof(struct lock_class_key
) > sizeof(struct lock_class
));
1155 key
= lock
->key
->subkeys
+ subclass
;
1157 hash_head
= classhashentry(key
);
1160 * We can walk the hash lockfree, because the hash only
1161 * grows, and we are careful when adding entries to the end:
1163 list_for_each_entry(class, hash_head
, hash_entry
)
1164 if (class->key
== key
)
1171 * Register a lock's class in the hash-table, if the class is not present
1172 * yet. Otherwise we look it up. We cache the result in the lock object
1173 * itself, so actual lookup of the hash should be once per lock object.
1175 static inline struct lock_class
*
1176 register_lock_class(struct lockdep_map
*lock
, unsigned int subclass
, int force
)
1178 struct lockdep_subclass_key
*key
;
1179 struct list_head
*hash_head
;
1180 struct lock_class
*class;
1182 class = look_up_lock_class(lock
, subclass
);
1187 * Debug-check: all keys must be persistent!
1189 if (!static_obj(lock
->key
)) {
1191 printk("INFO: trying to register non-static key.\n");
1192 printk("the code is fine but needs lockdep annotation.\n");
1193 printk("turning off the locking correctness validator.\n");
1199 key
= lock
->key
->subkeys
+ subclass
;
1200 hash_head
= classhashentry(key
);
1202 __raw_spin_lock(&hash_lock
);
1204 * We have to do the hash-walk again, to avoid races
1207 list_for_each_entry(class, hash_head
, hash_entry
)
1208 if (class->key
== key
)
1209 goto out_unlock_set
;
1211 * Allocate a new key from the static array, and add it to
1214 if (nr_lock_classes
>= MAX_LOCKDEP_KEYS
) {
1215 __raw_spin_unlock(&hash_lock
);
1217 printk("BUG: MAX_LOCKDEP_KEYS too low!\n");
1218 printk("turning off the locking correctness validator.\n");
1221 class = lock_classes
+ nr_lock_classes
++;
1222 debug_atomic_inc(&nr_unused_locks
);
1224 class->name
= lock
->name
;
1225 class->subclass
= subclass
;
1226 INIT_LIST_HEAD(&class->lock_entry
);
1227 INIT_LIST_HEAD(&class->locks_before
);
1228 INIT_LIST_HEAD(&class->locks_after
);
1229 class->name_version
= count_matching_names(class);
1231 * We use RCU's safe list-add method to make
1232 * parallel walking of the hash-list safe:
1234 list_add_tail_rcu(&class->hash_entry
, hash_head
);
1236 if (verbose(class)) {
1237 __raw_spin_unlock(&hash_lock
);
1238 printk("\nnew class %p: %s", class->key
, class->name
);
1239 if (class->name_version
> 1)
1240 printk("#%d", class->name_version
);
1243 __raw_spin_lock(&hash_lock
);
1246 __raw_spin_unlock(&hash_lock
);
1248 if (!subclass
|| force
)
1249 lock
->class_cache
= class;
1251 DEBUG_LOCKS_WARN_ON(class->subclass
!= subclass
);
1257 * Look up a dependency chain. If the key is not present yet then
1258 * add it and return 0 - in this case the new dependency chain is
1259 * validated. If the key is already hashed, return 1.
1261 static inline int lookup_chain_cache(u64 chain_key
)
1263 struct list_head
*hash_head
= chainhashentry(chain_key
);
1264 struct lock_chain
*chain
;
1266 DEBUG_LOCKS_WARN_ON(!irqs_disabled());
1268 * We can walk it lock-free, because entries only get added
1271 list_for_each_entry(chain
, hash_head
, entry
) {
1272 if (chain
->chain_key
== chain_key
) {
1274 debug_atomic_inc(&chain_lookup_hits
);
1276 * In the debugging case, force redundant checking
1279 #ifdef CONFIG_DEBUG_LOCKDEP
1280 __raw_spin_lock(&hash_lock
);
1287 * Allocate a new chain entry from the static array, and add
1290 __raw_spin_lock(&hash_lock
);
1292 * We have to walk the chain again locked - to avoid duplicates:
1294 list_for_each_entry(chain
, hash_head
, entry
) {
1295 if (chain
->chain_key
== chain_key
) {
1296 __raw_spin_unlock(&hash_lock
);
1300 if (unlikely(nr_lock_chains
>= MAX_LOCKDEP_CHAINS
)) {
1301 __raw_spin_unlock(&hash_lock
);
1303 printk("BUG: MAX_LOCKDEP_CHAINS too low!\n");
1304 printk("turning off the locking correctness validator.\n");
1307 chain
= lock_chains
+ nr_lock_chains
++;
1308 chain
->chain_key
= chain_key
;
1309 list_add_tail_rcu(&chain
->entry
, hash_head
);
1310 debug_atomic_inc(&chain_lookup_misses
);
1311 #ifdef CONFIG_TRACE_IRQFLAGS
1312 if (current
->hardirq_context
)
1313 nr_hardirq_chains
++;
1315 if (current
->softirq_context
)
1316 nr_softirq_chains
++;
1318 nr_process_chains
++;
1321 nr_process_chains
++;
1328 * We are building curr_chain_key incrementally, so double-check
1329 * it from scratch, to make sure that it's done correctly:
1331 static void check_chain_key(struct task_struct
*curr
)
1333 #ifdef CONFIG_DEBUG_LOCKDEP
1334 struct held_lock
*hlock
, *prev_hlock
= NULL
;
1338 for (i
= 0; i
< curr
->lockdep_depth
; i
++) {
1339 hlock
= curr
->held_locks
+ i
;
1340 if (chain_key
!= hlock
->prev_chain_key
) {
1342 printk("hm#1, depth: %u [%u], %016Lx != %016Lx\n",
1343 curr
->lockdep_depth
, i
,
1344 (unsigned long long)chain_key
,
1345 (unsigned long long)hlock
->prev_chain_key
);
1349 id
= hlock
->class - lock_classes
;
1350 DEBUG_LOCKS_WARN_ON(id
>= MAX_LOCKDEP_KEYS
);
1351 if (prev_hlock
&& (prev_hlock
->irq_context
!=
1352 hlock
->irq_context
))
1354 chain_key
= iterate_chain_key(chain_key
, id
);
1357 if (chain_key
!= curr
->curr_chain_key
) {
1359 printk("hm#2, depth: %u [%u], %016Lx != %016Lx\n",
1360 curr
->lockdep_depth
, i
,
1361 (unsigned long long)chain_key
,
1362 (unsigned long long)curr
->curr_chain_key
);
1368 #ifdef CONFIG_TRACE_IRQFLAGS
1371 * print irq inversion bug:
1374 print_irq_inversion_bug(struct task_struct
*curr
, struct lock_class
*other
,
1375 struct held_lock
*this, int forwards
,
1376 const char *irqclass
)
1378 __raw_spin_unlock(&hash_lock
);
1380 if (debug_locks_silent
)
1383 printk("\n=========================================================\n");
1384 printk( "[ INFO: possible irq lock inversion dependency detected ]\n");
1385 print_kernel_version();
1386 printk( "---------------------------------------------------------\n");
1387 printk("%s/%d just changed the state of lock:\n",
1388 curr
->comm
, curr
->pid
);
1391 printk("but this lock took another, %s-irq-unsafe lock in the past:\n", irqclass
);
1393 printk("but this lock was taken by another, %s-irq-safe lock in the past:\n", irqclass
);
1394 print_lock_name(other
);
1395 printk("\n\nand interrupts could create inverse lock ordering between them.\n\n");
1397 printk("\nother info that might help us debug this:\n");
1398 lockdep_print_held_locks(curr
);
1400 printk("\nthe first lock's dependencies:\n");
1401 print_lock_dependencies(this->class, 0);
1403 printk("\nthe second lock's dependencies:\n");
1404 print_lock_dependencies(other
, 0);
1406 printk("\nstack backtrace:\n");
1413 * Prove that in the forwards-direction subgraph starting at <this>
1414 * there is no lock matching <mask>:
1417 check_usage_forwards(struct task_struct
*curr
, struct held_lock
*this,
1418 enum lock_usage_bit bit
, const char *irqclass
)
1422 find_usage_bit
= bit
;
1423 /* fills in <forwards_match> */
1424 ret
= find_usage_forwards(this->class, 0);
1425 if (!ret
|| ret
== 1)
1428 return print_irq_inversion_bug(curr
, forwards_match
, this, 1, irqclass
);
1432 * Prove that in the backwards-direction subgraph starting at <this>
1433 * there is no lock matching <mask>:
1436 check_usage_backwards(struct task_struct
*curr
, struct held_lock
*this,
1437 enum lock_usage_bit bit
, const char *irqclass
)
1441 find_usage_bit
= bit
;
1442 /* fills in <backwards_match> */
1443 ret
= find_usage_backwards(this->class, 0);
1444 if (!ret
|| ret
== 1)
1447 return print_irq_inversion_bug(curr
, backwards_match
, this, 0, irqclass
);
1450 static inline void print_irqtrace_events(struct task_struct
*curr
)
1452 printk("irq event stamp: %u\n", curr
->irq_events
);
1453 printk("hardirqs last enabled at (%u): ", curr
->hardirq_enable_event
);
1454 print_ip_sym(curr
->hardirq_enable_ip
);
1455 printk("hardirqs last disabled at (%u): ", curr
->hardirq_disable_event
);
1456 print_ip_sym(curr
->hardirq_disable_ip
);
1457 printk("softirqs last enabled at (%u): ", curr
->softirq_enable_event
);
1458 print_ip_sym(curr
->softirq_enable_ip
);
1459 printk("softirqs last disabled at (%u): ", curr
->softirq_disable_event
);
1460 print_ip_sym(curr
->softirq_disable_ip
);
1464 static inline void print_irqtrace_events(struct task_struct
*curr
)
1470 print_usage_bug(struct task_struct
*curr
, struct held_lock
*this,
1471 enum lock_usage_bit prev_bit
, enum lock_usage_bit new_bit
)
1473 __raw_spin_unlock(&hash_lock
);
1475 if (debug_locks_silent
)
1478 printk("\n=================================\n");
1479 printk( "[ INFO: inconsistent lock state ]\n");
1480 print_kernel_version();
1481 printk( "---------------------------------\n");
1483 printk("inconsistent {%s} -> {%s} usage.\n",
1484 usage_str
[prev_bit
], usage_str
[new_bit
]);
1486 printk("%s/%d [HC%u[%lu]:SC%u[%lu]:HE%u:SE%u] takes:\n",
1487 curr
->comm
, curr
->pid
,
1488 trace_hardirq_context(curr
), hardirq_count() >> HARDIRQ_SHIFT
,
1489 trace_softirq_context(curr
), softirq_count() >> SOFTIRQ_SHIFT
,
1490 trace_hardirqs_enabled(curr
),
1491 trace_softirqs_enabled(curr
));
1494 printk("{%s} state was registered at:\n", usage_str
[prev_bit
]);
1495 print_stack_trace(this->class->usage_traces
+ prev_bit
, 1);
1497 print_irqtrace_events(curr
);
1498 printk("\nother info that might help us debug this:\n");
1499 lockdep_print_held_locks(curr
);
1501 printk("\nstack backtrace:\n");
1508 * Print out an error if an invalid bit is set:
1511 valid_state(struct task_struct
*curr
, struct held_lock
*this,
1512 enum lock_usage_bit new_bit
, enum lock_usage_bit bad_bit
)
1514 if (unlikely(this->class->usage_mask
& (1 << bad_bit
)))
1515 return print_usage_bug(curr
, this, bad_bit
, new_bit
);
1519 #define STRICT_READ_CHECKS 1
1522 * Mark a lock with a usage bit, and validate the state transition:
1524 static int mark_lock(struct task_struct
*curr
, struct held_lock
*this,
1525 enum lock_usage_bit new_bit
, unsigned long ip
)
1527 unsigned int new_mask
= 1 << new_bit
, ret
= 1;
1530 * If already set then do not dirty the cacheline,
1531 * nor do any checks:
1533 if (likely(this->class->usage_mask
& new_mask
))
1536 __raw_spin_lock(&hash_lock
);
1538 * Make sure we didnt race:
1540 if (unlikely(this->class->usage_mask
& new_mask
)) {
1541 __raw_spin_unlock(&hash_lock
);
1545 this->class->usage_mask
|= new_mask
;
1547 #ifdef CONFIG_TRACE_IRQFLAGS
1548 if (new_bit
== LOCK_ENABLED_HARDIRQS
||
1549 new_bit
== LOCK_ENABLED_HARDIRQS_READ
)
1550 ip
= curr
->hardirq_enable_ip
;
1551 else if (new_bit
== LOCK_ENABLED_SOFTIRQS
||
1552 new_bit
== LOCK_ENABLED_SOFTIRQS_READ
)
1553 ip
= curr
->softirq_enable_ip
;
1555 if (!save_trace(this->class->usage_traces
+ new_bit
))
1559 #ifdef CONFIG_TRACE_IRQFLAGS
1560 case LOCK_USED_IN_HARDIRQ
:
1561 if (!valid_state(curr
, this, new_bit
, LOCK_ENABLED_HARDIRQS
))
1563 if (!valid_state(curr
, this, new_bit
,
1564 LOCK_ENABLED_HARDIRQS_READ
))
1567 * just marked it hardirq-safe, check that this lock
1568 * took no hardirq-unsafe lock in the past:
1570 if (!check_usage_forwards(curr
, this,
1571 LOCK_ENABLED_HARDIRQS
, "hard"))
1573 #if STRICT_READ_CHECKS
1575 * just marked it hardirq-safe, check that this lock
1576 * took no hardirq-unsafe-read lock in the past:
1578 if (!check_usage_forwards(curr
, this,
1579 LOCK_ENABLED_HARDIRQS_READ
, "hard-read"))
1582 if (hardirq_verbose(this->class))
1585 case LOCK_USED_IN_SOFTIRQ
:
1586 if (!valid_state(curr
, this, new_bit
, LOCK_ENABLED_SOFTIRQS
))
1588 if (!valid_state(curr
, this, new_bit
,
1589 LOCK_ENABLED_SOFTIRQS_READ
))
1592 * just marked it softirq-safe, check that this lock
1593 * took no softirq-unsafe lock in the past:
1595 if (!check_usage_forwards(curr
, this,
1596 LOCK_ENABLED_SOFTIRQS
, "soft"))
1598 #if STRICT_READ_CHECKS
1600 * just marked it softirq-safe, check that this lock
1601 * took no softirq-unsafe-read lock in the past:
1603 if (!check_usage_forwards(curr
, this,
1604 LOCK_ENABLED_SOFTIRQS_READ
, "soft-read"))
1607 if (softirq_verbose(this->class))
1610 case LOCK_USED_IN_HARDIRQ_READ
:
1611 if (!valid_state(curr
, this, new_bit
, LOCK_ENABLED_HARDIRQS
))
1614 * just marked it hardirq-read-safe, check that this lock
1615 * took no hardirq-unsafe lock in the past:
1617 if (!check_usage_forwards(curr
, this,
1618 LOCK_ENABLED_HARDIRQS
, "hard"))
1620 if (hardirq_verbose(this->class))
1623 case LOCK_USED_IN_SOFTIRQ_READ
:
1624 if (!valid_state(curr
, this, new_bit
, LOCK_ENABLED_SOFTIRQS
))
1627 * just marked it softirq-read-safe, check that this lock
1628 * took no softirq-unsafe lock in the past:
1630 if (!check_usage_forwards(curr
, this,
1631 LOCK_ENABLED_SOFTIRQS
, "soft"))
1633 if (softirq_verbose(this->class))
1636 case LOCK_ENABLED_HARDIRQS
:
1637 if (!valid_state(curr
, this, new_bit
, LOCK_USED_IN_HARDIRQ
))
1639 if (!valid_state(curr
, this, new_bit
,
1640 LOCK_USED_IN_HARDIRQ_READ
))
1643 * just marked it hardirq-unsafe, check that no hardirq-safe
1644 * lock in the system ever took it in the past:
1646 if (!check_usage_backwards(curr
, this,
1647 LOCK_USED_IN_HARDIRQ
, "hard"))
1649 #if STRICT_READ_CHECKS
1651 * just marked it hardirq-unsafe, check that no
1652 * hardirq-safe-read lock in the system ever took
1655 if (!check_usage_backwards(curr
, this,
1656 LOCK_USED_IN_HARDIRQ_READ
, "hard-read"))
1659 if (hardirq_verbose(this->class))
1662 case LOCK_ENABLED_SOFTIRQS
:
1663 if (!valid_state(curr
, this, new_bit
, LOCK_USED_IN_SOFTIRQ
))
1665 if (!valid_state(curr
, this, new_bit
,
1666 LOCK_USED_IN_SOFTIRQ_READ
))
1669 * just marked it softirq-unsafe, check that no softirq-safe
1670 * lock in the system ever took it in the past:
1672 if (!check_usage_backwards(curr
, this,
1673 LOCK_USED_IN_SOFTIRQ
, "soft"))
1675 #if STRICT_READ_CHECKS
1677 * just marked it softirq-unsafe, check that no
1678 * softirq-safe-read lock in the system ever took
1681 if (!check_usage_backwards(curr
, this,
1682 LOCK_USED_IN_SOFTIRQ_READ
, "soft-read"))
1685 if (softirq_verbose(this->class))
1688 case LOCK_ENABLED_HARDIRQS_READ
:
1689 if (!valid_state(curr
, this, new_bit
, LOCK_USED_IN_HARDIRQ
))
1691 #if STRICT_READ_CHECKS
1693 * just marked it hardirq-read-unsafe, check that no
1694 * hardirq-safe lock in the system ever took it in the past:
1696 if (!check_usage_backwards(curr
, this,
1697 LOCK_USED_IN_HARDIRQ
, "hard"))
1700 if (hardirq_verbose(this->class))
1703 case LOCK_ENABLED_SOFTIRQS_READ
:
1704 if (!valid_state(curr
, this, new_bit
, LOCK_USED_IN_SOFTIRQ
))
1706 #if STRICT_READ_CHECKS
1708 * just marked it softirq-read-unsafe, check that no
1709 * softirq-safe lock in the system ever took it in the past:
1711 if (!check_usage_backwards(curr
, this,
1712 LOCK_USED_IN_SOFTIRQ
, "soft"))
1715 if (softirq_verbose(this->class))
1721 * Add it to the global list of classes:
1723 list_add_tail_rcu(&this->class->lock_entry
, &all_lock_classes
);
1724 debug_atomic_dec(&nr_unused_locks
);
1732 __raw_spin_unlock(&hash_lock
);
1735 * We must printk outside of the hash_lock:
1738 printk("\nmarked lock as {%s}:\n", usage_str
[new_bit
]);
1740 print_irqtrace_events(curr
);
1747 #ifdef CONFIG_TRACE_IRQFLAGS
1749 * Mark all held locks with a usage bit:
1752 mark_held_locks(struct task_struct
*curr
, int hardirq
, unsigned long ip
)
1754 enum lock_usage_bit usage_bit
;
1755 struct held_lock
*hlock
;
1758 for (i
= 0; i
< curr
->lockdep_depth
; i
++) {
1759 hlock
= curr
->held_locks
+ i
;
1763 usage_bit
= LOCK_ENABLED_HARDIRQS_READ
;
1765 usage_bit
= LOCK_ENABLED_HARDIRQS
;
1768 usage_bit
= LOCK_ENABLED_SOFTIRQS_READ
;
1770 usage_bit
= LOCK_ENABLED_SOFTIRQS
;
1772 if (!mark_lock(curr
, hlock
, usage_bit
, ip
))
1780 * Debugging helper: via this flag we know that we are in
1781 * 'early bootup code', and will warn about any invalid irqs-on event:
1783 static int early_boot_irqs_enabled
;
1785 void early_boot_irqs_off(void)
1787 early_boot_irqs_enabled
= 0;
1790 void early_boot_irqs_on(void)
1792 early_boot_irqs_enabled
= 1;
1796 * Hardirqs will be enabled:
1798 void trace_hardirqs_on(void)
1800 struct task_struct
*curr
= current
;
1803 if (unlikely(!debug_locks
|| current
->lockdep_recursion
))
1806 if (DEBUG_LOCKS_WARN_ON(unlikely(!early_boot_irqs_enabled
)))
1809 if (unlikely(curr
->hardirqs_enabled
)) {
1810 debug_atomic_inc(&redundant_hardirqs_on
);
1813 /* we'll do an OFF -> ON transition: */
1814 curr
->hardirqs_enabled
= 1;
1815 ip
= (unsigned long) __builtin_return_address(0);
1817 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
1819 if (DEBUG_LOCKS_WARN_ON(current
->hardirq_context
))
1822 * We are going to turn hardirqs on, so set the
1823 * usage bit for all held locks:
1825 if (!mark_held_locks(curr
, 1, ip
))
1828 * If we have softirqs enabled, then set the usage
1829 * bit for all held locks. (disabled hardirqs prevented
1830 * this bit from being set before)
1832 if (curr
->softirqs_enabled
)
1833 if (!mark_held_locks(curr
, 0, ip
))
1836 curr
->hardirq_enable_ip
= ip
;
1837 curr
->hardirq_enable_event
= ++curr
->irq_events
;
1838 debug_atomic_inc(&hardirqs_on_events
);
1841 EXPORT_SYMBOL(trace_hardirqs_on
);
1844 * Hardirqs were disabled:
1846 void trace_hardirqs_off(void)
1848 struct task_struct
*curr
= current
;
1850 if (unlikely(!debug_locks
|| current
->lockdep_recursion
))
1853 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
1856 if (curr
->hardirqs_enabled
) {
1858 * We have done an ON -> OFF transition:
1860 curr
->hardirqs_enabled
= 0;
1861 curr
->hardirq_disable_ip
= _RET_IP_
;
1862 curr
->hardirq_disable_event
= ++curr
->irq_events
;
1863 debug_atomic_inc(&hardirqs_off_events
);
1865 debug_atomic_inc(&redundant_hardirqs_off
);
1868 EXPORT_SYMBOL(trace_hardirqs_off
);
1871 * Softirqs will be enabled:
1873 void trace_softirqs_on(unsigned long ip
)
1875 struct task_struct
*curr
= current
;
1877 if (unlikely(!debug_locks
))
1880 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
1883 if (curr
->softirqs_enabled
) {
1884 debug_atomic_inc(&redundant_softirqs_on
);
1889 * We'll do an OFF -> ON transition:
1891 curr
->softirqs_enabled
= 1;
1892 curr
->softirq_enable_ip
= ip
;
1893 curr
->softirq_enable_event
= ++curr
->irq_events
;
1894 debug_atomic_inc(&softirqs_on_events
);
1896 * We are going to turn softirqs on, so set the
1897 * usage bit for all held locks, if hardirqs are
1900 if (curr
->hardirqs_enabled
)
1901 mark_held_locks(curr
, 0, ip
);
1905 * Softirqs were disabled:
1907 void trace_softirqs_off(unsigned long ip
)
1909 struct task_struct
*curr
= current
;
1911 if (unlikely(!debug_locks
))
1914 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
1917 if (curr
->softirqs_enabled
) {
1919 * We have done an ON -> OFF transition:
1921 curr
->softirqs_enabled
= 0;
1922 curr
->softirq_disable_ip
= ip
;
1923 curr
->softirq_disable_event
= ++curr
->irq_events
;
1924 debug_atomic_inc(&softirqs_off_events
);
1925 DEBUG_LOCKS_WARN_ON(!softirq_count());
1927 debug_atomic_inc(&redundant_softirqs_off
);
1933 * Initialize a lock instance's lock-class mapping info:
1935 void lockdep_init_map(struct lockdep_map
*lock
, const char *name
,
1936 struct lock_class_key
*key
, int subclass
)
1938 if (unlikely(!debug_locks
))
1941 if (DEBUG_LOCKS_WARN_ON(!key
))
1943 if (DEBUG_LOCKS_WARN_ON(!name
))
1946 * Sanity check, the lock-class key must be persistent:
1948 if (!static_obj(key
)) {
1949 printk("BUG: key %p not in .data!\n", key
);
1950 DEBUG_LOCKS_WARN_ON(1);
1955 lock
->class_cache
= NULL
;
1957 register_lock_class(lock
, subclass
, 1);
1960 EXPORT_SYMBOL_GPL(lockdep_init_map
);
1963 * This gets called for every mutex_lock*()/spin_lock*() operation.
1964 * We maintain the dependency maps and validate the locking attempt:
1966 static int __lock_acquire(struct lockdep_map
*lock
, unsigned int subclass
,
1967 int trylock
, int read
, int check
, int hardirqs_off
,
1970 struct task_struct
*curr
= current
;
1971 struct lock_class
*class = NULL
;
1972 struct held_lock
*hlock
;
1973 unsigned int depth
, id
;
1977 if (unlikely(!debug_locks
))
1980 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
1983 if (unlikely(subclass
>= MAX_LOCKDEP_SUBCLASSES
)) {
1985 printk("BUG: MAX_LOCKDEP_SUBCLASSES too low!\n");
1986 printk("turning off the locking correctness validator.\n");
1991 class = lock
->class_cache
;
1993 * Not cached yet or subclass?
1995 if (unlikely(!class)) {
1996 class = register_lock_class(lock
, subclass
, 0);
2000 debug_atomic_inc((atomic_t
*)&class->ops
);
2001 if (very_verbose(class)) {
2002 printk("\nacquire class [%p] %s", class->key
, class->name
);
2003 if (class->name_version
> 1)
2004 printk("#%d", class->name_version
);
2010 * Add the lock to the list of currently held locks.
2011 * (we dont increase the depth just yet, up until the
2012 * dependency checks are done)
2014 depth
= curr
->lockdep_depth
;
2015 if (DEBUG_LOCKS_WARN_ON(depth
>= MAX_LOCK_DEPTH
))
2018 hlock
= curr
->held_locks
+ depth
;
2020 hlock
->class = class;
2021 hlock
->acquire_ip
= ip
;
2022 hlock
->instance
= lock
;
2023 hlock
->trylock
= trylock
;
2025 hlock
->check
= check
;
2026 hlock
->hardirqs_off
= hardirqs_off
;
2030 #ifdef CONFIG_TRACE_IRQFLAGS
2032 * If non-trylock use in a hardirq or softirq context, then
2033 * mark the lock as used in these contexts:
2037 if (curr
->hardirq_context
)
2038 if (!mark_lock(curr
, hlock
,
2039 LOCK_USED_IN_HARDIRQ_READ
, ip
))
2041 if (curr
->softirq_context
)
2042 if (!mark_lock(curr
, hlock
,
2043 LOCK_USED_IN_SOFTIRQ_READ
, ip
))
2046 if (curr
->hardirq_context
)
2047 if (!mark_lock(curr
, hlock
, LOCK_USED_IN_HARDIRQ
, ip
))
2049 if (curr
->softirq_context
)
2050 if (!mark_lock(curr
, hlock
, LOCK_USED_IN_SOFTIRQ
, ip
))
2054 if (!hardirqs_off
) {
2056 if (!mark_lock(curr
, hlock
,
2057 LOCK_ENABLED_HARDIRQS_READ
, ip
))
2059 if (curr
->softirqs_enabled
)
2060 if (!mark_lock(curr
, hlock
,
2061 LOCK_ENABLED_SOFTIRQS_READ
, ip
))
2064 if (!mark_lock(curr
, hlock
,
2065 LOCK_ENABLED_HARDIRQS
, ip
))
2067 if (curr
->softirqs_enabled
)
2068 if (!mark_lock(curr
, hlock
,
2069 LOCK_ENABLED_SOFTIRQS
, ip
))
2074 /* mark it as used: */
2075 if (!mark_lock(curr
, hlock
, LOCK_USED
, ip
))
2079 * Calculate the chain hash: it's the combined has of all the
2080 * lock keys along the dependency chain. We save the hash value
2081 * at every step so that we can get the current hash easily
2082 * after unlock. The chain hash is then used to cache dependency
2085 * The 'key ID' is what is the most compact key value to drive
2086 * the hash, not class->key.
2088 id
= class - lock_classes
;
2089 if (DEBUG_LOCKS_WARN_ON(id
>= MAX_LOCKDEP_KEYS
))
2092 chain_key
= curr
->curr_chain_key
;
2094 if (DEBUG_LOCKS_WARN_ON(chain_key
!= 0))
2099 hlock
->prev_chain_key
= chain_key
;
2101 #ifdef CONFIG_TRACE_IRQFLAGS
2103 * Keep track of points where we cross into an interrupt context:
2105 hlock
->irq_context
= 2*(curr
->hardirq_context
? 1 : 0) +
2106 curr
->softirq_context
;
2108 struct held_lock
*prev_hlock
;
2110 prev_hlock
= curr
->held_locks
+ depth
-1;
2112 * If we cross into another context, reset the
2113 * hash key (this also prevents the checking and the
2114 * adding of the dependency to 'prev'):
2116 if (prev_hlock
->irq_context
!= hlock
->irq_context
) {
2122 chain_key
= iterate_chain_key(chain_key
, id
);
2123 curr
->curr_chain_key
= chain_key
;
2126 * Trylock needs to maintain the stack of held locks, but it
2127 * does not add new dependencies, because trylock can be done
2130 * We look up the chain_key and do the O(N^2) check and update of
2131 * the dependencies only if this is a new dependency chain.
2132 * (If lookup_chain_cache() returns with 1 it acquires
2135 if (!trylock
&& (check
== 2) && lookup_chain_cache(chain_key
)) {
2137 * Check whether last held lock:
2139 * - is irq-safe, if this lock is irq-unsafe
2140 * - is softirq-safe, if this lock is hardirq-unsafe
2142 * And check whether the new lock's dependency graph
2143 * could lead back to the previous lock.
2145 * any of these scenarios could lead to a deadlock. If
2148 int ret
= check_deadlock(curr
, hlock
, lock
, read
);
2153 * Mark recursive read, as we jump over it when
2154 * building dependencies (just like we jump over
2160 * Add dependency only if this lock is not the head
2161 * of the chain, and if it's not a secondary read-lock:
2163 if (!chain_head
&& ret
!= 2)
2164 if (!check_prevs_add(curr
, hlock
))
2166 __raw_spin_unlock(&hash_lock
);
2168 curr
->lockdep_depth
++;
2169 check_chain_key(curr
);
2170 if (unlikely(curr
->lockdep_depth
>= MAX_LOCK_DEPTH
)) {
2172 printk("BUG: MAX_LOCK_DEPTH too low!\n");
2173 printk("turning off the locking correctness validator.\n");
2176 if (unlikely(curr
->lockdep_depth
> max_lockdep_depth
))
2177 max_lockdep_depth
= curr
->lockdep_depth
;
2183 print_unlock_inbalance_bug(struct task_struct
*curr
, struct lockdep_map
*lock
,
2186 if (!debug_locks_off())
2188 if (debug_locks_silent
)
2191 printk("\n=====================================\n");
2192 printk( "[ BUG: bad unlock balance detected! ]\n");
2193 printk( "-------------------------------------\n");
2194 printk("%s/%d is trying to release lock (",
2195 curr
->comm
, curr
->pid
);
2196 print_lockdep_cache(lock
);
2199 printk("but there are no more locks to release!\n");
2200 printk("\nother info that might help us debug this:\n");
2201 lockdep_print_held_locks(curr
);
2203 printk("\nstack backtrace:\n");
2210 * Common debugging checks for both nested and non-nested unlock:
2212 static int check_unlock(struct task_struct
*curr
, struct lockdep_map
*lock
,
2215 if (unlikely(!debug_locks
))
2217 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2220 if (curr
->lockdep_depth
<= 0)
2221 return print_unlock_inbalance_bug(curr
, lock
, ip
);
2227 * Remove the lock to the list of currently held locks in a
2228 * potentially non-nested (out of order) manner. This is a
2229 * relatively rare operation, as all the unlock APIs default
2230 * to nested mode (which uses lock_release()):
2233 lock_release_non_nested(struct task_struct
*curr
,
2234 struct lockdep_map
*lock
, unsigned long ip
)
2236 struct held_lock
*hlock
, *prev_hlock
;
2241 * Check whether the lock exists in the current stack
2244 depth
= curr
->lockdep_depth
;
2245 if (DEBUG_LOCKS_WARN_ON(!depth
))
2249 for (i
= depth
-1; i
>= 0; i
--) {
2250 hlock
= curr
->held_locks
+ i
;
2252 * We must not cross into another context:
2254 if (prev_hlock
&& prev_hlock
->irq_context
!= hlock
->irq_context
)
2256 if (hlock
->instance
== lock
)
2260 return print_unlock_inbalance_bug(curr
, lock
, ip
);
2264 * We have the right lock to unlock, 'hlock' points to it.
2265 * Now we remove it from the stack, and add back the other
2266 * entries (if any), recalculating the hash along the way:
2268 curr
->lockdep_depth
= i
;
2269 curr
->curr_chain_key
= hlock
->prev_chain_key
;
2271 for (i
++; i
< depth
; i
++) {
2272 hlock
= curr
->held_locks
+ i
;
2273 if (!__lock_acquire(hlock
->instance
,
2274 hlock
->class->subclass
, hlock
->trylock
,
2275 hlock
->read
, hlock
->check
, hlock
->hardirqs_off
,
2280 if (DEBUG_LOCKS_WARN_ON(curr
->lockdep_depth
!= depth
- 1))
2286 * Remove the lock to the list of currently held locks - this gets
2287 * called on mutex_unlock()/spin_unlock*() (or on a failed
2288 * mutex_lock_interruptible()). This is done for unlocks that nest
2289 * perfectly. (i.e. the current top of the lock-stack is unlocked)
2291 static int lock_release_nested(struct task_struct
*curr
,
2292 struct lockdep_map
*lock
, unsigned long ip
)
2294 struct held_lock
*hlock
;
2298 * Pop off the top of the lock stack:
2300 depth
= curr
->lockdep_depth
- 1;
2301 hlock
= curr
->held_locks
+ depth
;
2304 * Is the unlock non-nested:
2306 if (hlock
->instance
!= lock
)
2307 return lock_release_non_nested(curr
, lock
, ip
);
2308 curr
->lockdep_depth
--;
2310 if (DEBUG_LOCKS_WARN_ON(!depth
&& (hlock
->prev_chain_key
!= 0)))
2313 curr
->curr_chain_key
= hlock
->prev_chain_key
;
2315 #ifdef CONFIG_DEBUG_LOCKDEP
2316 hlock
->prev_chain_key
= 0;
2317 hlock
->class = NULL
;
2318 hlock
->acquire_ip
= 0;
2319 hlock
->irq_context
= 0;
2325 * Remove the lock to the list of currently held locks - this gets
2326 * called on mutex_unlock()/spin_unlock*() (or on a failed
2327 * mutex_lock_interruptible()). This is done for unlocks that nest
2328 * perfectly. (i.e. the current top of the lock-stack is unlocked)
2331 __lock_release(struct lockdep_map
*lock
, int nested
, unsigned long ip
)
2333 struct task_struct
*curr
= current
;
2335 if (!check_unlock(curr
, lock
, ip
))
2339 if (!lock_release_nested(curr
, lock
, ip
))
2342 if (!lock_release_non_nested(curr
, lock
, ip
))
2346 check_chain_key(curr
);
2350 * Check whether we follow the irq-flags state precisely:
2352 static void check_flags(unsigned long flags
)
2354 #if defined(CONFIG_DEBUG_LOCKDEP) && defined(CONFIG_TRACE_IRQFLAGS)
2358 if (irqs_disabled_flags(flags
))
2359 DEBUG_LOCKS_WARN_ON(current
->hardirqs_enabled
);
2361 DEBUG_LOCKS_WARN_ON(!current
->hardirqs_enabled
);
2364 * We dont accurately track softirq state in e.g.
2365 * hardirq contexts (such as on 4KSTACKS), so only
2366 * check if not in hardirq contexts:
2368 if (!hardirq_count()) {
2369 if (softirq_count())
2370 DEBUG_LOCKS_WARN_ON(current
->softirqs_enabled
);
2372 DEBUG_LOCKS_WARN_ON(!current
->softirqs_enabled
);
2376 print_irqtrace_events(current
);
2381 * We are not always called with irqs disabled - do that here,
2382 * and also avoid lockdep recursion:
2384 void lock_acquire(struct lockdep_map
*lock
, unsigned int subclass
,
2385 int trylock
, int read
, int check
, unsigned long ip
)
2387 unsigned long flags
;
2389 if (unlikely(current
->lockdep_recursion
))
2392 raw_local_irq_save(flags
);
2395 current
->lockdep_recursion
= 1;
2396 __lock_acquire(lock
, subclass
, trylock
, read
, check
,
2397 irqs_disabled_flags(flags
), ip
);
2398 current
->lockdep_recursion
= 0;
2399 raw_local_irq_restore(flags
);
2402 EXPORT_SYMBOL_GPL(lock_acquire
);
2404 void lock_release(struct lockdep_map
*lock
, int nested
, unsigned long ip
)
2406 unsigned long flags
;
2408 if (unlikely(current
->lockdep_recursion
))
2411 raw_local_irq_save(flags
);
2413 current
->lockdep_recursion
= 1;
2414 __lock_release(lock
, nested
, ip
);
2415 current
->lockdep_recursion
= 0;
2416 raw_local_irq_restore(flags
);
2419 EXPORT_SYMBOL_GPL(lock_release
);
2422 * Used by the testsuite, sanitize the validator state
2423 * after a simulated failure:
2426 void lockdep_reset(void)
2428 unsigned long flags
;
2430 raw_local_irq_save(flags
);
2431 current
->curr_chain_key
= 0;
2432 current
->lockdep_depth
= 0;
2433 current
->lockdep_recursion
= 0;
2434 memset(current
->held_locks
, 0, MAX_LOCK_DEPTH
*sizeof(struct held_lock
));
2435 nr_hardirq_chains
= 0;
2436 nr_softirq_chains
= 0;
2437 nr_process_chains
= 0;
2439 raw_local_irq_restore(flags
);
2442 static void zap_class(struct lock_class
*class)
2447 * Remove all dependencies this lock is
2450 for (i
= 0; i
< nr_list_entries
; i
++) {
2451 if (list_entries
[i
].class == class)
2452 list_del_rcu(&list_entries
[i
].entry
);
2455 * Unhash the class and remove it from the all_lock_classes list:
2457 list_del_rcu(&class->hash_entry
);
2458 list_del_rcu(&class->lock_entry
);
2462 static inline int within(void *addr
, void *start
, unsigned long size
)
2464 return addr
>= start
&& addr
< start
+ size
;
2467 void lockdep_free_key_range(void *start
, unsigned long size
)
2469 struct lock_class
*class, *next
;
2470 struct list_head
*head
;
2471 unsigned long flags
;
2474 raw_local_irq_save(flags
);
2475 __raw_spin_lock(&hash_lock
);
2478 * Unhash all classes that were created by this module:
2480 for (i
= 0; i
< CLASSHASH_SIZE
; i
++) {
2481 head
= classhash_table
+ i
;
2482 if (list_empty(head
))
2484 list_for_each_entry_safe(class, next
, head
, hash_entry
)
2485 if (within(class->key
, start
, size
))
2489 __raw_spin_unlock(&hash_lock
);
2490 raw_local_irq_restore(flags
);
2493 void lockdep_reset_lock(struct lockdep_map
*lock
)
2495 struct lock_class
*class, *next
;
2496 struct list_head
*head
;
2497 unsigned long flags
;
2500 raw_local_irq_save(flags
);
2503 * Remove all classes this lock might have:
2505 for (j
= 0; j
< MAX_LOCKDEP_SUBCLASSES
; j
++) {
2507 * If the class exists we look it up and zap it:
2509 class = look_up_lock_class(lock
, j
);
2514 * Debug check: in the end all mapped classes should
2517 __raw_spin_lock(&hash_lock
);
2518 for (i
= 0; i
< CLASSHASH_SIZE
; i
++) {
2519 head
= classhash_table
+ i
;
2520 if (list_empty(head
))
2522 list_for_each_entry_safe(class, next
, head
, hash_entry
) {
2523 if (unlikely(class == lock
->class_cache
)) {
2524 __raw_spin_unlock(&hash_lock
);
2525 DEBUG_LOCKS_WARN_ON(1);
2530 __raw_spin_unlock(&hash_lock
);
2533 raw_local_irq_restore(flags
);
2536 void __init
lockdep_init(void)
2541 * Some architectures have their own start_kernel()
2542 * code which calls lockdep_init(), while we also
2543 * call lockdep_init() from the start_kernel() itself,
2544 * and we want to initialize the hashes only once:
2546 if (lockdep_initialized
)
2549 for (i
= 0; i
< CLASSHASH_SIZE
; i
++)
2550 INIT_LIST_HEAD(classhash_table
+ i
);
2552 for (i
= 0; i
< CHAINHASH_SIZE
; i
++)
2553 INIT_LIST_HEAD(chainhash_table
+ i
);
2555 lockdep_initialized
= 1;
2558 void __init
lockdep_info(void)
2560 printk("Lock dependency validator: Copyright (c) 2006 Red Hat, Inc., Ingo Molnar\n");
2562 printk("... MAX_LOCKDEP_SUBCLASSES: %lu\n", MAX_LOCKDEP_SUBCLASSES
);
2563 printk("... MAX_LOCK_DEPTH: %lu\n", MAX_LOCK_DEPTH
);
2564 printk("... MAX_LOCKDEP_KEYS: %lu\n", MAX_LOCKDEP_KEYS
);
2565 printk("... CLASSHASH_SIZE: %lu\n", CLASSHASH_SIZE
);
2566 printk("... MAX_LOCKDEP_ENTRIES: %lu\n", MAX_LOCKDEP_ENTRIES
);
2567 printk("... MAX_LOCKDEP_CHAINS: %lu\n", MAX_LOCKDEP_CHAINS
);
2568 printk("... CHAINHASH_SIZE: %lu\n", CHAINHASH_SIZE
);
2570 printk(" memory used by lock dependency info: %lu kB\n",
2571 (sizeof(struct lock_class
) * MAX_LOCKDEP_KEYS
+
2572 sizeof(struct list_head
) * CLASSHASH_SIZE
+
2573 sizeof(struct lock_list
) * MAX_LOCKDEP_ENTRIES
+
2574 sizeof(struct lock_chain
) * MAX_LOCKDEP_CHAINS
+
2575 sizeof(struct list_head
) * CHAINHASH_SIZE
) / 1024);
2577 printk(" per task-struct memory footprint: %lu bytes\n",
2578 sizeof(struct held_lock
) * MAX_LOCK_DEPTH
);
2580 #ifdef CONFIG_DEBUG_LOCKDEP
2581 if (lockdep_init_error
)
2582 printk("WARNING: lockdep init error! Arch code didnt call lockdep_init() early enough?\n");
2586 static inline int in_range(const void *start
, const void *addr
, const void *end
)
2588 return addr
>= start
&& addr
<= end
;
2592 print_freed_lock_bug(struct task_struct
*curr
, const void *mem_from
,
2593 const void *mem_to
, struct held_lock
*hlock
)
2595 if (!debug_locks_off())
2597 if (debug_locks_silent
)
2600 printk("\n=========================\n");
2601 printk( "[ BUG: held lock freed! ]\n");
2602 printk( "-------------------------\n");
2603 printk("%s/%d is freeing memory %p-%p, with a lock still held there!\n",
2604 curr
->comm
, curr
->pid
, mem_from
, mem_to
-1);
2606 lockdep_print_held_locks(curr
);
2608 printk("\nstack backtrace:\n");
2613 * Called when kernel memory is freed (or unmapped), or if a lock
2614 * is destroyed or reinitialized - this code checks whether there is
2615 * any held lock in the memory range of <from> to <to>:
2617 void debug_check_no_locks_freed(const void *mem_from
, unsigned long mem_len
)
2619 const void *mem_to
= mem_from
+ mem_len
, *lock_from
, *lock_to
;
2620 struct task_struct
*curr
= current
;
2621 struct held_lock
*hlock
;
2622 unsigned long flags
;
2625 if (unlikely(!debug_locks
))
2628 local_irq_save(flags
);
2629 for (i
= 0; i
< curr
->lockdep_depth
; i
++) {
2630 hlock
= curr
->held_locks
+ i
;
2632 lock_from
= (void *)hlock
->instance
;
2633 lock_to
= (void *)(hlock
->instance
+ 1);
2635 if (!in_range(mem_from
, lock_from
, mem_to
) &&
2636 !in_range(mem_from
, lock_to
, mem_to
))
2639 print_freed_lock_bug(curr
, mem_from
, mem_to
, hlock
);
2642 local_irq_restore(flags
);
2645 static void print_held_locks_bug(struct task_struct
*curr
)
2647 if (!debug_locks_off())
2649 if (debug_locks_silent
)
2652 printk("\n=====================================\n");
2653 printk( "[ BUG: lock held at task exit time! ]\n");
2654 printk( "-------------------------------------\n");
2655 printk("%s/%d is exiting with locks still held!\n",
2656 curr
->comm
, curr
->pid
);
2657 lockdep_print_held_locks(curr
);
2659 printk("\nstack backtrace:\n");
2663 void debug_check_no_locks_held(struct task_struct
*task
)
2665 if (unlikely(task
->lockdep_depth
> 0))
2666 print_held_locks_bug(task
);
2669 void debug_show_all_locks(void)
2671 struct task_struct
*g
, *p
;
2675 printk("\nShowing all locks held in the system:\n");
2678 * Here we try to get the tasklist_lock as hard as possible,
2679 * if not successful after 2 seconds we ignore it (but keep
2680 * trying). This is to enable a debug printout even if a
2681 * tasklist_lock-holding task deadlocks or crashes.
2684 if (!read_trylock(&tasklist_lock
)) {
2686 printk("hm, tasklist_lock locked, retrying... ");
2689 printk(" #%d", 10-count
);
2693 printk(" ignoring it.\n");
2697 printk(" locked it.\n");
2699 do_each_thread(g
, p
) {
2700 if (p
->lockdep_depth
)
2701 lockdep_print_held_locks(p
);
2703 if (read_trylock(&tasklist_lock
))
2705 } while_each_thread(g
, p
);
2708 printk("=============================================\n\n");
2711 read_unlock(&tasklist_lock
);
2714 EXPORT_SYMBOL_GPL(debug_show_all_locks
);
2716 void debug_show_held_locks(struct task_struct
*task
)
2718 lockdep_print_held_locks(task
);
2721 EXPORT_SYMBOL_GPL(debug_show_held_locks
);