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
);
144 * Debugging switches:
149 # define VERY_VERBOSE 0
153 # define HARDIRQ_VERBOSE 1
154 # define SOFTIRQ_VERBOSE 1
156 # define HARDIRQ_VERBOSE 0
157 # define SOFTIRQ_VERBOSE 0
160 #if VERBOSE || HARDIRQ_VERBOSE || SOFTIRQ_VERBOSE
162 * Quick filtering for interesting events:
164 static int class_filter(struct lock_class
*class)
168 if (class->name_version
== 1 &&
169 !strcmp(class->name
, "lockname"))
171 if (class->name_version
== 1 &&
172 !strcmp(class->name
, "&struct->lockfield"))
175 /* Allow everything else. 0 would be filter everything else */
180 static int verbose(struct lock_class
*class)
183 return class_filter(class);
188 #ifdef CONFIG_TRACE_IRQFLAGS
190 static int hardirq_verbose(struct lock_class
*class)
193 return class_filter(class);
198 static int softirq_verbose(struct lock_class
*class)
201 return class_filter(class);
209 * Stack-trace: tightly packed array of stack backtrace
210 * addresses. Protected by the hash_lock.
212 unsigned long nr_stack_trace_entries
;
213 static unsigned long stack_trace
[MAX_STACK_TRACE_ENTRIES
];
215 static int save_trace(struct stack_trace
*trace
)
217 trace
->nr_entries
= 0;
218 trace
->max_entries
= MAX_STACK_TRACE_ENTRIES
- nr_stack_trace_entries
;
219 trace
->entries
= stack_trace
+ nr_stack_trace_entries
;
222 trace
->all_contexts
= 0;
224 /* Make sure to not recurse in case the the unwinder needs to tak
227 save_stack_trace(trace
, NULL
);
230 trace
->max_entries
= trace
->nr_entries
;
232 nr_stack_trace_entries
+= trace
->nr_entries
;
233 if (DEBUG_LOCKS_WARN_ON(nr_stack_trace_entries
> MAX_STACK_TRACE_ENTRIES
)) {
234 __raw_spin_unlock(&hash_lock
);
238 if (nr_stack_trace_entries
== MAX_STACK_TRACE_ENTRIES
) {
239 __raw_spin_unlock(&hash_lock
);
240 if (debug_locks_off()) {
241 printk("BUG: MAX_STACK_TRACE_ENTRIES too low!\n");
242 printk("turning off the locking correctness validator.\n");
251 unsigned int nr_hardirq_chains
;
252 unsigned int nr_softirq_chains
;
253 unsigned int nr_process_chains
;
254 unsigned int max_lockdep_depth
;
255 unsigned int max_recursion_depth
;
257 #ifdef CONFIG_DEBUG_LOCKDEP
259 * We cannot printk in early bootup code. Not even early_printk()
260 * might work. So we mark any initialization errors and printk
261 * about it later on, in lockdep_info().
263 static int lockdep_init_error
;
266 * Various lockdep statistics:
268 atomic_t chain_lookup_hits
;
269 atomic_t chain_lookup_misses
;
270 atomic_t hardirqs_on_events
;
271 atomic_t hardirqs_off_events
;
272 atomic_t redundant_hardirqs_on
;
273 atomic_t redundant_hardirqs_off
;
274 atomic_t softirqs_on_events
;
275 atomic_t softirqs_off_events
;
276 atomic_t redundant_softirqs_on
;
277 atomic_t redundant_softirqs_off
;
278 atomic_t nr_unused_locks
;
279 atomic_t nr_cyclic_checks
;
280 atomic_t nr_cyclic_check_recursions
;
281 atomic_t nr_find_usage_forwards_checks
;
282 atomic_t nr_find_usage_forwards_recursions
;
283 atomic_t nr_find_usage_backwards_checks
;
284 atomic_t nr_find_usage_backwards_recursions
;
285 # define debug_atomic_inc(ptr) atomic_inc(ptr)
286 # define debug_atomic_dec(ptr) atomic_dec(ptr)
287 # define debug_atomic_read(ptr) atomic_read(ptr)
289 # define debug_atomic_inc(ptr) do { } while (0)
290 # define debug_atomic_dec(ptr) do { } while (0)
291 # define debug_atomic_read(ptr) 0
298 static const char *usage_str
[] =
300 [LOCK_USED
] = "initial-use ",
301 [LOCK_USED_IN_HARDIRQ
] = "in-hardirq-W",
302 [LOCK_USED_IN_SOFTIRQ
] = "in-softirq-W",
303 [LOCK_ENABLED_SOFTIRQS
] = "softirq-on-W",
304 [LOCK_ENABLED_HARDIRQS
] = "hardirq-on-W",
305 [LOCK_USED_IN_HARDIRQ_READ
] = "in-hardirq-R",
306 [LOCK_USED_IN_SOFTIRQ_READ
] = "in-softirq-R",
307 [LOCK_ENABLED_SOFTIRQS_READ
] = "softirq-on-R",
308 [LOCK_ENABLED_HARDIRQS_READ
] = "hardirq-on-R",
311 const char * __get_key_name(struct lockdep_subclass_key
*key
, char *str
)
313 unsigned long offs
, size
;
316 return kallsyms_lookup((unsigned long)key
, &size
, &offs
, &modname
, str
);
320 get_usage_chars(struct lock_class
*class, char *c1
, char *c2
, char *c3
, char *c4
)
322 *c1
= '.', *c2
= '.', *c3
= '.', *c4
= '.';
324 if (class->usage_mask
& LOCKF_USED_IN_HARDIRQ
)
327 if (class->usage_mask
& LOCKF_ENABLED_HARDIRQS
)
330 if (class->usage_mask
& LOCKF_USED_IN_SOFTIRQ
)
333 if (class->usage_mask
& LOCKF_ENABLED_SOFTIRQS
)
336 if (class->usage_mask
& LOCKF_ENABLED_HARDIRQS_READ
)
338 if (class->usage_mask
& LOCKF_USED_IN_HARDIRQ_READ
) {
340 if (class->usage_mask
& LOCKF_ENABLED_HARDIRQS_READ
)
344 if (class->usage_mask
& LOCKF_ENABLED_SOFTIRQS_READ
)
346 if (class->usage_mask
& LOCKF_USED_IN_SOFTIRQ_READ
) {
348 if (class->usage_mask
& LOCKF_ENABLED_SOFTIRQS_READ
)
353 static void print_lock_name(struct lock_class
*class)
355 char str
[KSYM_NAME_LEN
+ 1], c1
, c2
, c3
, c4
;
358 get_usage_chars(class, &c1
, &c2
, &c3
, &c4
);
362 name
= __get_key_name(class->key
, str
);
363 printk(" (%s", name
);
365 printk(" (%s", name
);
366 if (class->name_version
> 1)
367 printk("#%d", class->name_version
);
369 printk("/%d", class->subclass
);
371 printk("){%c%c%c%c}", c1
, c2
, c3
, c4
);
374 static void print_lockdep_cache(struct lockdep_map
*lock
)
377 char str
[KSYM_NAME_LEN
+ 1];
381 name
= __get_key_name(lock
->key
->subkeys
, str
);
386 static void print_lock(struct held_lock
*hlock
)
388 print_lock_name(hlock
->class);
390 print_ip_sym(hlock
->acquire_ip
);
393 static void lockdep_print_held_locks(struct task_struct
*curr
)
395 int i
, depth
= curr
->lockdep_depth
;
398 printk("no locks held by %s/%d.\n", curr
->comm
, curr
->pid
);
401 printk("%d lock%s held by %s/%d:\n",
402 depth
, depth
> 1 ? "s" : "", curr
->comm
, curr
->pid
);
404 for (i
= 0; i
< depth
; i
++) {
406 print_lock(curr
->held_locks
+ i
);
410 static void print_lock_class_header(struct lock_class
*class, int depth
)
414 printk("%*s->", depth
, "");
415 print_lock_name(class);
416 printk(" ops: %lu", class->ops
);
419 for (bit
= 0; bit
< LOCK_USAGE_STATES
; bit
++) {
420 if (class->usage_mask
& (1 << bit
)) {
423 len
+= printk("%*s %s", depth
, "", usage_str
[bit
]);
424 len
+= printk(" at:\n");
425 print_stack_trace(class->usage_traces
+ bit
, len
);
428 printk("%*s }\n", depth
, "");
430 printk("%*s ... key at: ",depth
,"");
431 print_ip_sym((unsigned long)class->key
);
435 * printk all lock dependencies starting at <entry>:
437 static void print_lock_dependencies(struct lock_class
*class, int depth
)
439 struct lock_list
*entry
;
441 if (DEBUG_LOCKS_WARN_ON(depth
>= 20))
444 print_lock_class_header(class, depth
);
446 list_for_each_entry(entry
, &class->locks_after
, entry
) {
447 if (DEBUG_LOCKS_WARN_ON(!entry
->class))
450 print_lock_dependencies(entry
->class, depth
+ 1);
452 printk("%*s ... acquired at:\n",depth
,"");
453 print_stack_trace(&entry
->trace
, 2);
459 * Add a new dependency to the head of the list:
461 static int add_lock_to_list(struct lock_class
*class, struct lock_class
*this,
462 struct list_head
*head
, unsigned long ip
)
464 struct lock_list
*entry
;
466 * Lock not present yet - get a new dependency struct and
467 * add it to the list:
469 entry
= alloc_list_entry();
474 if (!save_trace(&entry
->trace
))
478 * Since we never remove from the dependency list, the list can
479 * be walked lockless by other CPUs, it's only allocation
480 * that must be protected by the spinlock. But this also means
481 * we must make new entries visible only once writes to the
482 * entry become visible - hence the RCU op:
484 list_add_tail_rcu(&entry
->entry
, head
);
490 * Recursive, forwards-direction lock-dependency checking, used for
491 * both noncyclic checking and for hardirq-unsafe/softirq-unsafe
494 * (to keep the stackframe of the recursive functions small we
495 * use these global variables, and we also mark various helper
496 * functions as noinline.)
498 static struct held_lock
*check_source
, *check_target
;
501 * Print a dependency chain entry (this is only done when a deadlock
502 * has been detected):
505 print_circular_bug_entry(struct lock_list
*target
, unsigned int depth
)
507 if (debug_locks_silent
)
509 printk("\n-> #%u", depth
);
510 print_lock_name(target
->class);
512 print_stack_trace(&target
->trace
, 6);
517 static void print_kernel_version(void)
519 printk("%s %.*s\n", init_utsname()->release
,
520 (int)strcspn(init_utsname()->version
, " "),
521 init_utsname()->version
);
525 * When a circular dependency is detected, print the
529 print_circular_bug_header(struct lock_list
*entry
, unsigned int depth
)
531 struct task_struct
*curr
= current
;
533 __raw_spin_unlock(&hash_lock
);
535 if (debug_locks_silent
)
538 printk("\n=======================================================\n");
539 printk( "[ INFO: possible circular locking dependency detected ]\n");
540 print_kernel_version();
541 printk( "-------------------------------------------------------\n");
542 printk("%s/%d is trying to acquire lock:\n",
543 curr
->comm
, curr
->pid
);
544 print_lock(check_source
);
545 printk("\nbut task is already holding lock:\n");
546 print_lock(check_target
);
547 printk("\nwhich lock already depends on the new lock.\n\n");
548 printk("\nthe existing dependency chain (in reverse order) is:\n");
550 print_circular_bug_entry(entry
, depth
);
555 static noinline
int print_circular_bug_tail(void)
557 struct task_struct
*curr
= current
;
558 struct lock_list
this;
560 if (debug_locks_silent
)
563 /* hash_lock unlocked by the header */
564 __raw_spin_lock(&hash_lock
);
565 this.class = check_source
->class;
566 if (!save_trace(&this.trace
))
568 __raw_spin_unlock(&hash_lock
);
569 print_circular_bug_entry(&this, 0);
571 printk("\nother info that might help us debug this:\n\n");
572 lockdep_print_held_locks(curr
);
574 printk("\nstack backtrace:\n");
580 #define RECURSION_LIMIT 40
582 static int noinline
print_infinite_recursion_bug(void)
584 __raw_spin_unlock(&hash_lock
);
585 DEBUG_LOCKS_WARN_ON(1);
591 * Prove that the dependency graph starting at <entry> can not
592 * lead to <target>. Print an error and return 0 if it does.
595 check_noncircular(struct lock_class
*source
, unsigned int depth
)
597 struct lock_list
*entry
;
599 debug_atomic_inc(&nr_cyclic_check_recursions
);
600 if (depth
> max_recursion_depth
)
601 max_recursion_depth
= depth
;
602 if (depth
>= RECURSION_LIMIT
)
603 return print_infinite_recursion_bug();
605 * Check this lock's dependency list:
607 list_for_each_entry(entry
, &source
->locks_after
, entry
) {
608 if (entry
->class == check_target
->class)
609 return print_circular_bug_header(entry
, depth
+1);
610 debug_atomic_inc(&nr_cyclic_checks
);
611 if (!check_noncircular(entry
->class, depth
+1))
612 return print_circular_bug_entry(entry
, depth
+1);
617 static int very_verbose(struct lock_class
*class)
620 return class_filter(class);
624 #ifdef CONFIG_TRACE_IRQFLAGS
627 * Forwards and backwards subgraph searching, for the purposes of
628 * proving that two subgraphs can be connected by a new dependency
629 * without creating any illegal irq-safe -> irq-unsafe lock dependency.
631 static enum lock_usage_bit find_usage_bit
;
632 static struct lock_class
*forwards_match
, *backwards_match
;
635 * Find a node in the forwards-direction dependency sub-graph starting
636 * at <source> that matches <find_usage_bit>.
638 * Return 2 if such a node exists in the subgraph, and put that node
639 * into <forwards_match>.
641 * Return 1 otherwise and keep <forwards_match> unchanged.
645 find_usage_forwards(struct lock_class
*source
, unsigned int depth
)
647 struct lock_list
*entry
;
650 if (depth
> max_recursion_depth
)
651 max_recursion_depth
= depth
;
652 if (depth
>= RECURSION_LIMIT
)
653 return print_infinite_recursion_bug();
655 debug_atomic_inc(&nr_find_usage_forwards_checks
);
656 if (source
->usage_mask
& (1 << find_usage_bit
)) {
657 forwards_match
= source
;
662 * Check this lock's dependency list:
664 list_for_each_entry(entry
, &source
->locks_after
, entry
) {
665 debug_atomic_inc(&nr_find_usage_forwards_recursions
);
666 ret
= find_usage_forwards(entry
->class, depth
+1);
667 if (ret
== 2 || ret
== 0)
674 * Find a node in the backwards-direction dependency sub-graph starting
675 * at <source> that matches <find_usage_bit>.
677 * Return 2 if such a node exists in the subgraph, and put that node
678 * into <backwards_match>.
680 * Return 1 otherwise and keep <backwards_match> unchanged.
684 find_usage_backwards(struct lock_class
*source
, unsigned int depth
)
686 struct lock_list
*entry
;
689 if (depth
> max_recursion_depth
)
690 max_recursion_depth
= depth
;
691 if (depth
>= RECURSION_LIMIT
)
692 return print_infinite_recursion_bug();
694 debug_atomic_inc(&nr_find_usage_backwards_checks
);
695 if (source
->usage_mask
& (1 << find_usage_bit
)) {
696 backwards_match
= source
;
701 * Check this lock's dependency list:
703 list_for_each_entry(entry
, &source
->locks_before
, entry
) {
704 debug_atomic_inc(&nr_find_usage_backwards_recursions
);
705 ret
= find_usage_backwards(entry
->class, depth
+1);
706 if (ret
== 2 || ret
== 0)
713 print_bad_irq_dependency(struct task_struct
*curr
,
714 struct held_lock
*prev
,
715 struct held_lock
*next
,
716 enum lock_usage_bit bit1
,
717 enum lock_usage_bit bit2
,
718 const char *irqclass
)
720 __raw_spin_unlock(&hash_lock
);
722 if (debug_locks_silent
)
725 printk("\n======================================================\n");
726 printk( "[ INFO: %s-safe -> %s-unsafe lock order detected ]\n",
728 print_kernel_version();
729 printk( "------------------------------------------------------\n");
730 printk("%s/%d [HC%u[%lu]:SC%u[%lu]:HE%u:SE%u] is trying to acquire:\n",
731 curr
->comm
, curr
->pid
,
732 curr
->hardirq_context
, hardirq_count() >> HARDIRQ_SHIFT
,
733 curr
->softirq_context
, softirq_count() >> SOFTIRQ_SHIFT
,
734 curr
->hardirqs_enabled
,
735 curr
->softirqs_enabled
);
738 printk("\nand this task is already holding:\n");
740 printk("which would create a new lock dependency:\n");
741 print_lock_name(prev
->class);
743 print_lock_name(next
->class);
746 printk("\nbut this new dependency connects a %s-irq-safe lock:\n",
748 print_lock_name(backwards_match
);
749 printk("\n... which became %s-irq-safe at:\n", irqclass
);
751 print_stack_trace(backwards_match
->usage_traces
+ bit1
, 1);
753 printk("\nto a %s-irq-unsafe lock:\n", irqclass
);
754 print_lock_name(forwards_match
);
755 printk("\n... which became %s-irq-unsafe at:\n", irqclass
);
758 print_stack_trace(forwards_match
->usage_traces
+ bit2
, 1);
760 printk("\nother info that might help us debug this:\n\n");
761 lockdep_print_held_locks(curr
);
763 printk("\nthe %s-irq-safe lock's dependencies:\n", irqclass
);
764 print_lock_dependencies(backwards_match
, 0);
766 printk("\nthe %s-irq-unsafe lock's dependencies:\n", irqclass
);
767 print_lock_dependencies(forwards_match
, 0);
769 printk("\nstack backtrace:\n");
776 check_usage(struct task_struct
*curr
, struct held_lock
*prev
,
777 struct held_lock
*next
, enum lock_usage_bit bit_backwards
,
778 enum lock_usage_bit bit_forwards
, const char *irqclass
)
782 find_usage_bit
= bit_backwards
;
783 /* fills in <backwards_match> */
784 ret
= find_usage_backwards(prev
->class, 0);
785 if (!ret
|| ret
== 1)
788 find_usage_bit
= bit_forwards
;
789 ret
= find_usage_forwards(next
->class, 0);
790 if (!ret
|| ret
== 1)
793 return print_bad_irq_dependency(curr
, prev
, next
,
794 bit_backwards
, bit_forwards
, irqclass
);
800 print_deadlock_bug(struct task_struct
*curr
, struct held_lock
*prev
,
801 struct held_lock
*next
)
804 __raw_spin_unlock(&hash_lock
);
805 if (debug_locks_silent
)
808 printk("\n=============================================\n");
809 printk( "[ INFO: possible recursive locking detected ]\n");
810 print_kernel_version();
811 printk( "---------------------------------------------\n");
812 printk("%s/%d is trying to acquire lock:\n",
813 curr
->comm
, curr
->pid
);
815 printk("\nbut task is already holding lock:\n");
818 printk("\nother info that might help us debug this:\n");
819 lockdep_print_held_locks(curr
);
821 printk("\nstack backtrace:\n");
828 * Check whether we are holding such a class already.
830 * (Note that this has to be done separately, because the graph cannot
831 * detect such classes of deadlocks.)
833 * Returns: 0 on deadlock detected, 1 on OK, 2 on recursive read
836 check_deadlock(struct task_struct
*curr
, struct held_lock
*next
,
837 struct lockdep_map
*next_instance
, int read
)
839 struct held_lock
*prev
;
842 for (i
= 0; i
< curr
->lockdep_depth
; i
++) {
843 prev
= curr
->held_locks
+ i
;
844 if (prev
->class != next
->class)
847 * Allow read-after-read recursion of the same
848 * lock class (i.e. read_lock(lock)+read_lock(lock)):
850 if ((read
== 2) && prev
->read
)
852 return print_deadlock_bug(curr
, prev
, next
);
858 * There was a chain-cache miss, and we are about to add a new dependency
859 * to a previous lock. We recursively validate the following rules:
861 * - would the adding of the <prev> -> <next> dependency create a
862 * circular dependency in the graph? [== circular deadlock]
864 * - does the new prev->next dependency connect any hardirq-safe lock
865 * (in the full backwards-subgraph starting at <prev>) with any
866 * hardirq-unsafe lock (in the full forwards-subgraph starting at
867 * <next>)? [== illegal lock inversion with hardirq contexts]
869 * - does the new prev->next dependency connect any softirq-safe lock
870 * (in the full backwards-subgraph starting at <prev>) with any
871 * softirq-unsafe lock (in the full forwards-subgraph starting at
872 * <next>)? [== illegal lock inversion with softirq contexts]
874 * any of these scenarios could lead to a deadlock.
876 * Then if all the validations pass, we add the forwards and backwards
880 check_prev_add(struct task_struct
*curr
, struct held_lock
*prev
,
881 struct held_lock
*next
)
883 struct lock_list
*entry
;
887 * Prove that the new <prev> -> <next> dependency would not
888 * create a circular dependency in the graph. (We do this by
889 * forward-recursing into the graph starting at <next>, and
890 * checking whether we can reach <prev>.)
892 * We are using global variables to control the recursion, to
893 * keep the stackframe size of the recursive functions low:
897 if (!(check_noncircular(next
->class, 0)))
898 return print_circular_bug_tail();
900 #ifdef CONFIG_TRACE_IRQFLAGS
902 * Prove that the new dependency does not connect a hardirq-safe
903 * lock with a hardirq-unsafe lock - to achieve this we search
904 * the backwards-subgraph starting at <prev>, and the
905 * forwards-subgraph starting at <next>:
907 if (!check_usage(curr
, prev
, next
, LOCK_USED_IN_HARDIRQ
,
908 LOCK_ENABLED_HARDIRQS
, "hard"))
912 * Prove that the new dependency does not connect a hardirq-safe-read
913 * lock with a hardirq-unsafe lock - to achieve this we search
914 * the backwards-subgraph starting at <prev>, and the
915 * forwards-subgraph starting at <next>:
917 if (!check_usage(curr
, prev
, next
, LOCK_USED_IN_HARDIRQ_READ
,
918 LOCK_ENABLED_HARDIRQS
, "hard-read"))
922 * Prove that the new dependency does not connect a softirq-safe
923 * lock with a softirq-unsafe lock - to achieve this we search
924 * the backwards-subgraph starting at <prev>, and the
925 * forwards-subgraph starting at <next>:
927 if (!check_usage(curr
, prev
, next
, LOCK_USED_IN_SOFTIRQ
,
928 LOCK_ENABLED_SOFTIRQS
, "soft"))
931 * Prove that the new dependency does not connect a softirq-safe-read
932 * lock with a softirq-unsafe lock - to achieve this we search
933 * the backwards-subgraph starting at <prev>, and the
934 * forwards-subgraph starting at <next>:
936 if (!check_usage(curr
, prev
, next
, LOCK_USED_IN_SOFTIRQ_READ
,
937 LOCK_ENABLED_SOFTIRQS
, "soft"))
941 * For recursive read-locks we do all the dependency checks,
942 * but we dont store read-triggered dependencies (only
943 * write-triggered dependencies). This ensures that only the
944 * write-side dependencies matter, and that if for example a
945 * write-lock never takes any other locks, then the reads are
946 * equivalent to a NOP.
948 if (next
->read
== 2 || prev
->read
== 2)
951 * Is the <prev> -> <next> dependency already present?
953 * (this may occur even though this is a new chain: consider
954 * e.g. the L1 -> L2 -> L3 -> L4 and the L5 -> L1 -> L2 -> L3
955 * chains - the second one will be new, but L1 already has
956 * L2 added to its dependency list, due to the first chain.)
958 list_for_each_entry(entry
, &prev
->class->locks_after
, entry
) {
959 if (entry
->class == next
->class)
964 * Ok, all validations passed, add the new lock
965 * to the previous lock's dependency list:
967 ret
= add_lock_to_list(prev
->class, next
->class,
968 &prev
->class->locks_after
, next
->acquire_ip
);
972 ret
= add_lock_to_list(next
->class, prev
->class,
973 &next
->class->locks_before
, next
->acquire_ip
);
978 * Debugging printouts:
980 if (verbose(prev
->class) || verbose(next
->class)) {
981 __raw_spin_unlock(&hash_lock
);
982 printk("\n new dependency: ");
983 print_lock_name(prev
->class);
985 print_lock_name(next
->class);
988 __raw_spin_lock(&hash_lock
);
994 * Add the dependency to all directly-previous locks that are 'relevant'.
995 * The ones that are relevant are (in increasing distance from curr):
996 * all consecutive trylock entries and the final non-trylock entry - or
997 * the end of this context's lock-chain - whichever comes first.
1000 check_prevs_add(struct task_struct
*curr
, struct held_lock
*next
)
1002 int depth
= curr
->lockdep_depth
;
1003 struct held_lock
*hlock
;
1008 * Depth must not be zero for a non-head lock:
1013 * At least two relevant locks must exist for this
1016 if (curr
->held_locks
[depth
].irq_context
!=
1017 curr
->held_locks
[depth
-1].irq_context
)
1021 hlock
= curr
->held_locks
+ depth
-1;
1023 * Only non-recursive-read entries get new dependencies
1026 if (hlock
->read
!= 2) {
1027 if (!check_prev_add(curr
, hlock
, next
))
1030 * Stop after the first non-trylock entry,
1031 * as non-trylock entries have added their
1032 * own direct dependencies already, so this
1033 * lock is connected to them indirectly:
1035 if (!hlock
->trylock
)
1040 * End of lock-stack?
1045 * Stop the search if we cross into another context:
1047 if (curr
->held_locks
[depth
].irq_context
!=
1048 curr
->held_locks
[depth
-1].irq_context
)
1053 __raw_spin_unlock(&hash_lock
);
1054 DEBUG_LOCKS_WARN_ON(1);
1061 * Is this the address of a static object:
1063 static int static_obj(void *obj
)
1065 unsigned long start
= (unsigned long) &_stext
,
1066 end
= (unsigned long) &_end
,
1067 addr
= (unsigned long) obj
;
1075 if ((addr
>= start
) && (addr
< end
))
1082 for_each_possible_cpu(i
) {
1083 start
= (unsigned long) &__per_cpu_start
+ per_cpu_offset(i
);
1084 end
= (unsigned long) &__per_cpu_start
+ PERCPU_ENOUGH_ROOM
1085 + per_cpu_offset(i
);
1087 if ((addr
>= start
) && (addr
< end
))
1095 return is_module_address(addr
);
1099 * To make lock name printouts unique, we calculate a unique
1100 * class->name_version generation counter:
1102 static int count_matching_names(struct lock_class
*new_class
)
1104 struct lock_class
*class;
1107 if (!new_class
->name
)
1110 list_for_each_entry(class, &all_lock_classes
, lock_entry
) {
1111 if (new_class
->key
- new_class
->subclass
== class->key
)
1112 return class->name_version
;
1113 if (class->name
&& !strcmp(class->name
, new_class
->name
))
1114 count
= max(count
, class->name_version
);
1121 * Register a lock's class in the hash-table, if the class is not present
1122 * yet. Otherwise we look it up. We cache the result in the lock object
1123 * itself, so actual lookup of the hash should be once per lock object.
1125 static inline struct lock_class
*
1126 look_up_lock_class(struct lockdep_map
*lock
, unsigned int subclass
)
1128 struct lockdep_subclass_key
*key
;
1129 struct list_head
*hash_head
;
1130 struct lock_class
*class;
1132 #ifdef CONFIG_DEBUG_LOCKDEP
1134 * If the architecture calls into lockdep before initializing
1135 * the hashes then we'll warn about it later. (we cannot printk
1138 if (unlikely(!lockdep_initialized
)) {
1140 lockdep_init_error
= 1;
1145 * Static locks do not have their class-keys yet - for them the key
1146 * is the lock object itself:
1148 if (unlikely(!lock
->key
))
1149 lock
->key
= (void *)lock
;
1152 * NOTE: the class-key must be unique. For dynamic locks, a static
1153 * lock_class_key variable is passed in through the mutex_init()
1154 * (or spin_lock_init()) call - which acts as the key. For static
1155 * locks we use the lock object itself as the key.
1157 BUILD_BUG_ON(sizeof(struct lock_class_key
) > sizeof(struct lock_class
));
1159 key
= lock
->key
->subkeys
+ subclass
;
1161 hash_head
= classhashentry(key
);
1164 * We can walk the hash lockfree, because the hash only
1165 * grows, and we are careful when adding entries to the end:
1167 list_for_each_entry(class, hash_head
, hash_entry
)
1168 if (class->key
== key
)
1175 * Register a lock's class in the hash-table, if the class is not present
1176 * yet. Otherwise we look it up. We cache the result in the lock object
1177 * itself, so actual lookup of the hash should be once per lock object.
1179 static inline struct lock_class
*
1180 register_lock_class(struct lockdep_map
*lock
, unsigned int subclass
, int force
)
1182 struct lockdep_subclass_key
*key
;
1183 struct list_head
*hash_head
;
1184 struct lock_class
*class;
1186 class = look_up_lock_class(lock
, subclass
);
1191 * Debug-check: all keys must be persistent!
1193 if (!static_obj(lock
->key
)) {
1195 printk("INFO: trying to register non-static key.\n");
1196 printk("the code is fine but needs lockdep annotation.\n");
1197 printk("turning off the locking correctness validator.\n");
1203 key
= lock
->key
->subkeys
+ subclass
;
1204 hash_head
= classhashentry(key
);
1206 __raw_spin_lock(&hash_lock
);
1208 * We have to do the hash-walk again, to avoid races
1211 list_for_each_entry(class, hash_head
, hash_entry
)
1212 if (class->key
== key
)
1213 goto out_unlock_set
;
1215 * Allocate a new key from the static array, and add it to
1218 if (nr_lock_classes
>= MAX_LOCKDEP_KEYS
) {
1219 __raw_spin_unlock(&hash_lock
);
1221 printk("BUG: MAX_LOCKDEP_KEYS too low!\n");
1222 printk("turning off the locking correctness validator.\n");
1225 class = lock_classes
+ nr_lock_classes
++;
1226 debug_atomic_inc(&nr_unused_locks
);
1228 class->name
= lock
->name
;
1229 class->subclass
= subclass
;
1230 INIT_LIST_HEAD(&class->lock_entry
);
1231 INIT_LIST_HEAD(&class->locks_before
);
1232 INIT_LIST_HEAD(&class->locks_after
);
1233 class->name_version
= count_matching_names(class);
1235 * We use RCU's safe list-add method to make
1236 * parallel walking of the hash-list safe:
1238 list_add_tail_rcu(&class->hash_entry
, hash_head
);
1240 if (verbose(class)) {
1241 __raw_spin_unlock(&hash_lock
);
1242 printk("\nnew class %p: %s", class->key
, class->name
);
1243 if (class->name_version
> 1)
1244 printk("#%d", class->name_version
);
1247 __raw_spin_lock(&hash_lock
);
1250 __raw_spin_unlock(&hash_lock
);
1252 if (!subclass
|| force
)
1253 lock
->class_cache
= class;
1255 DEBUG_LOCKS_WARN_ON(class->subclass
!= subclass
);
1261 * Look up a dependency chain. If the key is not present yet then
1262 * add it and return 0 - in this case the new dependency chain is
1263 * validated. If the key is already hashed, return 1.
1265 static inline int lookup_chain_cache(u64 chain_key
)
1267 struct list_head
*hash_head
= chainhashentry(chain_key
);
1268 struct lock_chain
*chain
;
1270 DEBUG_LOCKS_WARN_ON(!irqs_disabled());
1272 * We can walk it lock-free, because entries only get added
1275 list_for_each_entry(chain
, hash_head
, entry
) {
1276 if (chain
->chain_key
== chain_key
) {
1278 debug_atomic_inc(&chain_lookup_hits
);
1280 * In the debugging case, force redundant checking
1283 #ifdef CONFIG_DEBUG_LOCKDEP
1284 __raw_spin_lock(&hash_lock
);
1291 * Allocate a new chain entry from the static array, and add
1294 __raw_spin_lock(&hash_lock
);
1296 * We have to walk the chain again locked - to avoid duplicates:
1298 list_for_each_entry(chain
, hash_head
, entry
) {
1299 if (chain
->chain_key
== chain_key
) {
1300 __raw_spin_unlock(&hash_lock
);
1304 if (unlikely(nr_lock_chains
>= MAX_LOCKDEP_CHAINS
)) {
1305 __raw_spin_unlock(&hash_lock
);
1307 printk("BUG: MAX_LOCKDEP_CHAINS too low!\n");
1308 printk("turning off the locking correctness validator.\n");
1311 chain
= lock_chains
+ nr_lock_chains
++;
1312 chain
->chain_key
= chain_key
;
1313 list_add_tail_rcu(&chain
->entry
, hash_head
);
1314 debug_atomic_inc(&chain_lookup_misses
);
1315 #ifdef CONFIG_TRACE_IRQFLAGS
1316 if (current
->hardirq_context
)
1317 nr_hardirq_chains
++;
1319 if (current
->softirq_context
)
1320 nr_softirq_chains
++;
1322 nr_process_chains
++;
1325 nr_process_chains
++;
1332 * We are building curr_chain_key incrementally, so double-check
1333 * it from scratch, to make sure that it's done correctly:
1335 static void check_chain_key(struct task_struct
*curr
)
1337 #ifdef CONFIG_DEBUG_LOCKDEP
1338 struct held_lock
*hlock
, *prev_hlock
= NULL
;
1342 for (i
= 0; i
< curr
->lockdep_depth
; i
++) {
1343 hlock
= curr
->held_locks
+ i
;
1344 if (chain_key
!= hlock
->prev_chain_key
) {
1346 printk("hm#1, depth: %u [%u], %016Lx != %016Lx\n",
1347 curr
->lockdep_depth
, i
,
1348 (unsigned long long)chain_key
,
1349 (unsigned long long)hlock
->prev_chain_key
);
1353 id
= hlock
->class - lock_classes
;
1354 DEBUG_LOCKS_WARN_ON(id
>= MAX_LOCKDEP_KEYS
);
1355 if (prev_hlock
&& (prev_hlock
->irq_context
!=
1356 hlock
->irq_context
))
1358 chain_key
= iterate_chain_key(chain_key
, id
);
1361 if (chain_key
!= curr
->curr_chain_key
) {
1363 printk("hm#2, depth: %u [%u], %016Lx != %016Lx\n",
1364 curr
->lockdep_depth
, i
,
1365 (unsigned long long)chain_key
,
1366 (unsigned long long)curr
->curr_chain_key
);
1372 #ifdef CONFIG_TRACE_IRQFLAGS
1375 * print irq inversion bug:
1378 print_irq_inversion_bug(struct task_struct
*curr
, struct lock_class
*other
,
1379 struct held_lock
*this, int forwards
,
1380 const char *irqclass
)
1382 __raw_spin_unlock(&hash_lock
);
1384 if (debug_locks_silent
)
1387 printk("\n=========================================================\n");
1388 printk( "[ INFO: possible irq lock inversion dependency detected ]\n");
1389 print_kernel_version();
1390 printk( "---------------------------------------------------------\n");
1391 printk("%s/%d just changed the state of lock:\n",
1392 curr
->comm
, curr
->pid
);
1395 printk("but this lock took another, %s-irq-unsafe lock in the past:\n", irqclass
);
1397 printk("but this lock was taken by another, %s-irq-safe lock in the past:\n", irqclass
);
1398 print_lock_name(other
);
1399 printk("\n\nand interrupts could create inverse lock ordering between them.\n\n");
1401 printk("\nother info that might help us debug this:\n");
1402 lockdep_print_held_locks(curr
);
1404 printk("\nthe first lock's dependencies:\n");
1405 print_lock_dependencies(this->class, 0);
1407 printk("\nthe second lock's dependencies:\n");
1408 print_lock_dependencies(other
, 0);
1410 printk("\nstack backtrace:\n");
1417 * Prove that in the forwards-direction subgraph starting at <this>
1418 * there is no lock matching <mask>:
1421 check_usage_forwards(struct task_struct
*curr
, struct held_lock
*this,
1422 enum lock_usage_bit bit
, const char *irqclass
)
1426 find_usage_bit
= bit
;
1427 /* fills in <forwards_match> */
1428 ret
= find_usage_forwards(this->class, 0);
1429 if (!ret
|| ret
== 1)
1432 return print_irq_inversion_bug(curr
, forwards_match
, this, 1, irqclass
);
1436 * Prove that in the backwards-direction subgraph starting at <this>
1437 * there is no lock matching <mask>:
1440 check_usage_backwards(struct task_struct
*curr
, struct held_lock
*this,
1441 enum lock_usage_bit bit
, const char *irqclass
)
1445 find_usage_bit
= bit
;
1446 /* fills in <backwards_match> */
1447 ret
= find_usage_backwards(this->class, 0);
1448 if (!ret
|| ret
== 1)
1451 return print_irq_inversion_bug(curr
, backwards_match
, this, 0, irqclass
);
1454 static inline void print_irqtrace_events(struct task_struct
*curr
)
1456 printk("irq event stamp: %u\n", curr
->irq_events
);
1457 printk("hardirqs last enabled at (%u): ", curr
->hardirq_enable_event
);
1458 print_ip_sym(curr
->hardirq_enable_ip
);
1459 printk("hardirqs last disabled at (%u): ", curr
->hardirq_disable_event
);
1460 print_ip_sym(curr
->hardirq_disable_ip
);
1461 printk("softirqs last enabled at (%u): ", curr
->softirq_enable_event
);
1462 print_ip_sym(curr
->softirq_enable_ip
);
1463 printk("softirqs last disabled at (%u): ", curr
->softirq_disable_event
);
1464 print_ip_sym(curr
->softirq_disable_ip
);
1468 static inline void print_irqtrace_events(struct task_struct
*curr
)
1474 print_usage_bug(struct task_struct
*curr
, struct held_lock
*this,
1475 enum lock_usage_bit prev_bit
, enum lock_usage_bit new_bit
)
1477 __raw_spin_unlock(&hash_lock
);
1479 if (debug_locks_silent
)
1482 printk("\n=================================\n");
1483 printk( "[ INFO: inconsistent lock state ]\n");
1484 print_kernel_version();
1485 printk( "---------------------------------\n");
1487 printk("inconsistent {%s} -> {%s} usage.\n",
1488 usage_str
[prev_bit
], usage_str
[new_bit
]);
1490 printk("%s/%d [HC%u[%lu]:SC%u[%lu]:HE%u:SE%u] takes:\n",
1491 curr
->comm
, curr
->pid
,
1492 trace_hardirq_context(curr
), hardirq_count() >> HARDIRQ_SHIFT
,
1493 trace_softirq_context(curr
), softirq_count() >> SOFTIRQ_SHIFT
,
1494 trace_hardirqs_enabled(curr
),
1495 trace_softirqs_enabled(curr
));
1498 printk("{%s} state was registered at:\n", usage_str
[prev_bit
]);
1499 print_stack_trace(this->class->usage_traces
+ prev_bit
, 1);
1501 print_irqtrace_events(curr
);
1502 printk("\nother info that might help us debug this:\n");
1503 lockdep_print_held_locks(curr
);
1505 printk("\nstack backtrace:\n");
1512 * Print out an error if an invalid bit is set:
1515 valid_state(struct task_struct
*curr
, struct held_lock
*this,
1516 enum lock_usage_bit new_bit
, enum lock_usage_bit bad_bit
)
1518 if (unlikely(this->class->usage_mask
& (1 << bad_bit
)))
1519 return print_usage_bug(curr
, this, bad_bit
, new_bit
);
1523 #define STRICT_READ_CHECKS 1
1526 * Mark a lock with a usage bit, and validate the state transition:
1528 static int mark_lock(struct task_struct
*curr
, struct held_lock
*this,
1529 enum lock_usage_bit new_bit
, unsigned long ip
)
1531 unsigned int new_mask
= 1 << new_bit
, ret
= 1;
1534 * If already set then do not dirty the cacheline,
1535 * nor do any checks:
1537 if (likely(this->class->usage_mask
& new_mask
))
1540 __raw_spin_lock(&hash_lock
);
1542 * Make sure we didnt race:
1544 if (unlikely(this->class->usage_mask
& new_mask
)) {
1545 __raw_spin_unlock(&hash_lock
);
1549 this->class->usage_mask
|= new_mask
;
1551 #ifdef CONFIG_TRACE_IRQFLAGS
1552 if (new_bit
== LOCK_ENABLED_HARDIRQS
||
1553 new_bit
== LOCK_ENABLED_HARDIRQS_READ
)
1554 ip
= curr
->hardirq_enable_ip
;
1555 else if (new_bit
== LOCK_ENABLED_SOFTIRQS
||
1556 new_bit
== LOCK_ENABLED_SOFTIRQS_READ
)
1557 ip
= curr
->softirq_enable_ip
;
1559 if (!save_trace(this->class->usage_traces
+ new_bit
))
1563 #ifdef CONFIG_TRACE_IRQFLAGS
1564 case LOCK_USED_IN_HARDIRQ
:
1565 if (!valid_state(curr
, this, new_bit
, LOCK_ENABLED_HARDIRQS
))
1567 if (!valid_state(curr
, this, new_bit
,
1568 LOCK_ENABLED_HARDIRQS_READ
))
1571 * just marked it hardirq-safe, check that this lock
1572 * took no hardirq-unsafe lock in the past:
1574 if (!check_usage_forwards(curr
, this,
1575 LOCK_ENABLED_HARDIRQS
, "hard"))
1577 #if STRICT_READ_CHECKS
1579 * just marked it hardirq-safe, check that this lock
1580 * took no hardirq-unsafe-read lock in the past:
1582 if (!check_usage_forwards(curr
, this,
1583 LOCK_ENABLED_HARDIRQS_READ
, "hard-read"))
1586 if (hardirq_verbose(this->class))
1589 case LOCK_USED_IN_SOFTIRQ
:
1590 if (!valid_state(curr
, this, new_bit
, LOCK_ENABLED_SOFTIRQS
))
1592 if (!valid_state(curr
, this, new_bit
,
1593 LOCK_ENABLED_SOFTIRQS_READ
))
1596 * just marked it softirq-safe, check that this lock
1597 * took no softirq-unsafe lock in the past:
1599 if (!check_usage_forwards(curr
, this,
1600 LOCK_ENABLED_SOFTIRQS
, "soft"))
1602 #if STRICT_READ_CHECKS
1604 * just marked it softirq-safe, check that this lock
1605 * took no softirq-unsafe-read lock in the past:
1607 if (!check_usage_forwards(curr
, this,
1608 LOCK_ENABLED_SOFTIRQS_READ
, "soft-read"))
1611 if (softirq_verbose(this->class))
1614 case LOCK_USED_IN_HARDIRQ_READ
:
1615 if (!valid_state(curr
, this, new_bit
, LOCK_ENABLED_HARDIRQS
))
1618 * just marked it hardirq-read-safe, check that this lock
1619 * took no hardirq-unsafe lock in the past:
1621 if (!check_usage_forwards(curr
, this,
1622 LOCK_ENABLED_HARDIRQS
, "hard"))
1624 if (hardirq_verbose(this->class))
1627 case LOCK_USED_IN_SOFTIRQ_READ
:
1628 if (!valid_state(curr
, this, new_bit
, LOCK_ENABLED_SOFTIRQS
))
1631 * just marked it softirq-read-safe, check that this lock
1632 * took no softirq-unsafe lock in the past:
1634 if (!check_usage_forwards(curr
, this,
1635 LOCK_ENABLED_SOFTIRQS
, "soft"))
1637 if (softirq_verbose(this->class))
1640 case LOCK_ENABLED_HARDIRQS
:
1641 if (!valid_state(curr
, this, new_bit
, LOCK_USED_IN_HARDIRQ
))
1643 if (!valid_state(curr
, this, new_bit
,
1644 LOCK_USED_IN_HARDIRQ_READ
))
1647 * just marked it hardirq-unsafe, check that no hardirq-safe
1648 * lock in the system ever took it in the past:
1650 if (!check_usage_backwards(curr
, this,
1651 LOCK_USED_IN_HARDIRQ
, "hard"))
1653 #if STRICT_READ_CHECKS
1655 * just marked it hardirq-unsafe, check that no
1656 * hardirq-safe-read lock in the system ever took
1659 if (!check_usage_backwards(curr
, this,
1660 LOCK_USED_IN_HARDIRQ_READ
, "hard-read"))
1663 if (hardirq_verbose(this->class))
1666 case LOCK_ENABLED_SOFTIRQS
:
1667 if (!valid_state(curr
, this, new_bit
, LOCK_USED_IN_SOFTIRQ
))
1669 if (!valid_state(curr
, this, new_bit
,
1670 LOCK_USED_IN_SOFTIRQ_READ
))
1673 * just marked it softirq-unsafe, check that no softirq-safe
1674 * lock in the system ever took it in the past:
1676 if (!check_usage_backwards(curr
, this,
1677 LOCK_USED_IN_SOFTIRQ
, "soft"))
1679 #if STRICT_READ_CHECKS
1681 * just marked it softirq-unsafe, check that no
1682 * softirq-safe-read lock in the system ever took
1685 if (!check_usage_backwards(curr
, this,
1686 LOCK_USED_IN_SOFTIRQ_READ
, "soft-read"))
1689 if (softirq_verbose(this->class))
1692 case LOCK_ENABLED_HARDIRQS_READ
:
1693 if (!valid_state(curr
, this, new_bit
, LOCK_USED_IN_HARDIRQ
))
1695 #if STRICT_READ_CHECKS
1697 * just marked it hardirq-read-unsafe, check that no
1698 * hardirq-safe lock in the system ever took it in the past:
1700 if (!check_usage_backwards(curr
, this,
1701 LOCK_USED_IN_HARDIRQ
, "hard"))
1704 if (hardirq_verbose(this->class))
1707 case LOCK_ENABLED_SOFTIRQS_READ
:
1708 if (!valid_state(curr
, this, new_bit
, LOCK_USED_IN_SOFTIRQ
))
1710 #if STRICT_READ_CHECKS
1712 * just marked it softirq-read-unsafe, check that no
1713 * softirq-safe lock in the system ever took it in the past:
1715 if (!check_usage_backwards(curr
, this,
1716 LOCK_USED_IN_SOFTIRQ
, "soft"))
1719 if (softirq_verbose(this->class))
1725 * Add it to the global list of classes:
1727 list_add_tail_rcu(&this->class->lock_entry
, &all_lock_classes
);
1728 debug_atomic_dec(&nr_unused_locks
);
1731 __raw_spin_unlock(&hash_lock
);
1737 __raw_spin_unlock(&hash_lock
);
1740 * We must printk outside of the hash_lock:
1743 printk("\nmarked lock as {%s}:\n", usage_str
[new_bit
]);
1745 print_irqtrace_events(curr
);
1752 #ifdef CONFIG_TRACE_IRQFLAGS
1754 * Mark all held locks with a usage bit:
1757 mark_held_locks(struct task_struct
*curr
, int hardirq
, unsigned long ip
)
1759 enum lock_usage_bit usage_bit
;
1760 struct held_lock
*hlock
;
1763 for (i
= 0; i
< curr
->lockdep_depth
; i
++) {
1764 hlock
= curr
->held_locks
+ i
;
1768 usage_bit
= LOCK_ENABLED_HARDIRQS_READ
;
1770 usage_bit
= LOCK_ENABLED_HARDIRQS
;
1773 usage_bit
= LOCK_ENABLED_SOFTIRQS_READ
;
1775 usage_bit
= LOCK_ENABLED_SOFTIRQS
;
1777 if (!mark_lock(curr
, hlock
, usage_bit
, ip
))
1785 * Debugging helper: via this flag we know that we are in
1786 * 'early bootup code', and will warn about any invalid irqs-on event:
1788 static int early_boot_irqs_enabled
;
1790 void early_boot_irqs_off(void)
1792 early_boot_irqs_enabled
= 0;
1795 void early_boot_irqs_on(void)
1797 early_boot_irqs_enabled
= 1;
1801 * Hardirqs will be enabled:
1803 void trace_hardirqs_on(void)
1805 struct task_struct
*curr
= current
;
1808 if (unlikely(!debug_locks
|| current
->lockdep_recursion
))
1811 if (DEBUG_LOCKS_WARN_ON(unlikely(!early_boot_irqs_enabled
)))
1814 if (unlikely(curr
->hardirqs_enabled
)) {
1815 debug_atomic_inc(&redundant_hardirqs_on
);
1818 /* we'll do an OFF -> ON transition: */
1819 curr
->hardirqs_enabled
= 1;
1820 ip
= (unsigned long) __builtin_return_address(0);
1822 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
1824 if (DEBUG_LOCKS_WARN_ON(current
->hardirq_context
))
1827 * We are going to turn hardirqs on, so set the
1828 * usage bit for all held locks:
1830 if (!mark_held_locks(curr
, 1, ip
))
1833 * If we have softirqs enabled, then set the usage
1834 * bit for all held locks. (disabled hardirqs prevented
1835 * this bit from being set before)
1837 if (curr
->softirqs_enabled
)
1838 if (!mark_held_locks(curr
, 0, ip
))
1841 curr
->hardirq_enable_ip
= ip
;
1842 curr
->hardirq_enable_event
= ++curr
->irq_events
;
1843 debug_atomic_inc(&hardirqs_on_events
);
1846 EXPORT_SYMBOL(trace_hardirqs_on
);
1849 * Hardirqs were disabled:
1851 void trace_hardirqs_off(void)
1853 struct task_struct
*curr
= current
;
1855 if (unlikely(!debug_locks
|| current
->lockdep_recursion
))
1858 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
1861 if (curr
->hardirqs_enabled
) {
1863 * We have done an ON -> OFF transition:
1865 curr
->hardirqs_enabled
= 0;
1866 curr
->hardirq_disable_ip
= _RET_IP_
;
1867 curr
->hardirq_disable_event
= ++curr
->irq_events
;
1868 debug_atomic_inc(&hardirqs_off_events
);
1870 debug_atomic_inc(&redundant_hardirqs_off
);
1873 EXPORT_SYMBOL(trace_hardirqs_off
);
1876 * Softirqs will be enabled:
1878 void trace_softirqs_on(unsigned long ip
)
1880 struct task_struct
*curr
= current
;
1882 if (unlikely(!debug_locks
))
1885 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
1888 if (curr
->softirqs_enabled
) {
1889 debug_atomic_inc(&redundant_softirqs_on
);
1894 * We'll do an OFF -> ON transition:
1896 curr
->softirqs_enabled
= 1;
1897 curr
->softirq_enable_ip
= ip
;
1898 curr
->softirq_enable_event
= ++curr
->irq_events
;
1899 debug_atomic_inc(&softirqs_on_events
);
1901 * We are going to turn softirqs on, so set the
1902 * usage bit for all held locks, if hardirqs are
1905 if (curr
->hardirqs_enabled
)
1906 mark_held_locks(curr
, 0, ip
);
1910 * Softirqs were disabled:
1912 void trace_softirqs_off(unsigned long ip
)
1914 struct task_struct
*curr
= current
;
1916 if (unlikely(!debug_locks
))
1919 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
1922 if (curr
->softirqs_enabled
) {
1924 * We have done an ON -> OFF transition:
1926 curr
->softirqs_enabled
= 0;
1927 curr
->softirq_disable_ip
= ip
;
1928 curr
->softirq_disable_event
= ++curr
->irq_events
;
1929 debug_atomic_inc(&softirqs_off_events
);
1930 DEBUG_LOCKS_WARN_ON(!softirq_count());
1932 debug_atomic_inc(&redundant_softirqs_off
);
1938 * Initialize a lock instance's lock-class mapping info:
1940 void lockdep_init_map(struct lockdep_map
*lock
, const char *name
,
1941 struct lock_class_key
*key
, int subclass
)
1943 if (unlikely(!debug_locks
))
1946 if (DEBUG_LOCKS_WARN_ON(!key
))
1948 if (DEBUG_LOCKS_WARN_ON(!name
))
1951 * Sanity check, the lock-class key must be persistent:
1953 if (!static_obj(key
)) {
1954 printk("BUG: key %p not in .data!\n", key
);
1955 DEBUG_LOCKS_WARN_ON(1);
1960 lock
->class_cache
= NULL
;
1962 register_lock_class(lock
, subclass
, 1);
1965 EXPORT_SYMBOL_GPL(lockdep_init_map
);
1968 * This gets called for every mutex_lock*()/spin_lock*() operation.
1969 * We maintain the dependency maps and validate the locking attempt:
1971 static int __lock_acquire(struct lockdep_map
*lock
, unsigned int subclass
,
1972 int trylock
, int read
, int check
, int hardirqs_off
,
1975 struct task_struct
*curr
= current
;
1976 struct lock_class
*class = NULL
;
1977 struct held_lock
*hlock
;
1978 unsigned int depth
, id
;
1982 if (unlikely(!debug_locks
))
1985 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
1988 if (unlikely(subclass
>= MAX_LOCKDEP_SUBCLASSES
)) {
1990 printk("BUG: MAX_LOCKDEP_SUBCLASSES too low!\n");
1991 printk("turning off the locking correctness validator.\n");
1996 class = lock
->class_cache
;
1998 * Not cached yet or subclass?
2000 if (unlikely(!class)) {
2001 class = register_lock_class(lock
, subclass
, 0);
2005 debug_atomic_inc((atomic_t
*)&class->ops
);
2006 if (very_verbose(class)) {
2007 printk("\nacquire class [%p] %s", class->key
, class->name
);
2008 if (class->name_version
> 1)
2009 printk("#%d", class->name_version
);
2015 * Add the lock to the list of currently held locks.
2016 * (we dont increase the depth just yet, up until the
2017 * dependency checks are done)
2019 depth
= curr
->lockdep_depth
;
2020 if (DEBUG_LOCKS_WARN_ON(depth
>= MAX_LOCK_DEPTH
))
2023 hlock
= curr
->held_locks
+ depth
;
2025 hlock
->class = class;
2026 hlock
->acquire_ip
= ip
;
2027 hlock
->instance
= lock
;
2028 hlock
->trylock
= trylock
;
2030 hlock
->check
= check
;
2031 hlock
->hardirqs_off
= hardirqs_off
;
2035 #ifdef CONFIG_TRACE_IRQFLAGS
2037 * If non-trylock use in a hardirq or softirq context, then
2038 * mark the lock as used in these contexts:
2042 if (curr
->hardirq_context
)
2043 if (!mark_lock(curr
, hlock
,
2044 LOCK_USED_IN_HARDIRQ_READ
, ip
))
2046 if (curr
->softirq_context
)
2047 if (!mark_lock(curr
, hlock
,
2048 LOCK_USED_IN_SOFTIRQ_READ
, ip
))
2051 if (curr
->hardirq_context
)
2052 if (!mark_lock(curr
, hlock
, LOCK_USED_IN_HARDIRQ
, ip
))
2054 if (curr
->softirq_context
)
2055 if (!mark_lock(curr
, hlock
, LOCK_USED_IN_SOFTIRQ
, ip
))
2059 if (!hardirqs_off
) {
2061 if (!mark_lock(curr
, hlock
,
2062 LOCK_ENABLED_HARDIRQS_READ
, ip
))
2064 if (curr
->softirqs_enabled
)
2065 if (!mark_lock(curr
, hlock
,
2066 LOCK_ENABLED_SOFTIRQS_READ
, ip
))
2069 if (!mark_lock(curr
, hlock
,
2070 LOCK_ENABLED_HARDIRQS
, ip
))
2072 if (curr
->softirqs_enabled
)
2073 if (!mark_lock(curr
, hlock
,
2074 LOCK_ENABLED_SOFTIRQS
, ip
))
2079 /* mark it as used: */
2080 if (!mark_lock(curr
, hlock
, LOCK_USED
, ip
))
2084 * Calculate the chain hash: it's the combined has of all the
2085 * lock keys along the dependency chain. We save the hash value
2086 * at every step so that we can get the current hash easily
2087 * after unlock. The chain hash is then used to cache dependency
2090 * The 'key ID' is what is the most compact key value to drive
2091 * the hash, not class->key.
2093 id
= class - lock_classes
;
2094 if (DEBUG_LOCKS_WARN_ON(id
>= MAX_LOCKDEP_KEYS
))
2097 chain_key
= curr
->curr_chain_key
;
2099 if (DEBUG_LOCKS_WARN_ON(chain_key
!= 0))
2104 hlock
->prev_chain_key
= chain_key
;
2106 #ifdef CONFIG_TRACE_IRQFLAGS
2108 * Keep track of points where we cross into an interrupt context:
2110 hlock
->irq_context
= 2*(curr
->hardirq_context
? 1 : 0) +
2111 curr
->softirq_context
;
2113 struct held_lock
*prev_hlock
;
2115 prev_hlock
= curr
->held_locks
+ depth
-1;
2117 * If we cross into another context, reset the
2118 * hash key (this also prevents the checking and the
2119 * adding of the dependency to 'prev'):
2121 if (prev_hlock
->irq_context
!= hlock
->irq_context
) {
2127 chain_key
= iterate_chain_key(chain_key
, id
);
2128 curr
->curr_chain_key
= chain_key
;
2131 * Trylock needs to maintain the stack of held locks, but it
2132 * does not add new dependencies, because trylock can be done
2135 * We look up the chain_key and do the O(N^2) check and update of
2136 * the dependencies only if this is a new dependency chain.
2137 * (If lookup_chain_cache() returns with 1 it acquires
2140 if (!trylock
&& (check
== 2) && lookup_chain_cache(chain_key
)) {
2142 * Check whether last held lock:
2144 * - is irq-safe, if this lock is irq-unsafe
2145 * - is softirq-safe, if this lock is hardirq-unsafe
2147 * And check whether the new lock's dependency graph
2148 * could lead back to the previous lock.
2150 * any of these scenarios could lead to a deadlock. If
2153 int ret
= check_deadlock(curr
, hlock
, lock
, read
);
2158 * Mark recursive read, as we jump over it when
2159 * building dependencies (just like we jump over
2165 * Add dependency only if this lock is not the head
2166 * of the chain, and if it's not a secondary read-lock:
2168 if (!chain_head
&& ret
!= 2)
2169 if (!check_prevs_add(curr
, hlock
))
2171 __raw_spin_unlock(&hash_lock
);
2173 curr
->lockdep_depth
++;
2174 check_chain_key(curr
);
2175 if (unlikely(curr
->lockdep_depth
>= MAX_LOCK_DEPTH
)) {
2177 printk("BUG: MAX_LOCK_DEPTH too low!\n");
2178 printk("turning off the locking correctness validator.\n");
2181 if (unlikely(curr
->lockdep_depth
> max_lockdep_depth
))
2182 max_lockdep_depth
= curr
->lockdep_depth
;
2188 print_unlock_inbalance_bug(struct task_struct
*curr
, struct lockdep_map
*lock
,
2191 if (!debug_locks_off())
2193 if (debug_locks_silent
)
2196 printk("\n=====================================\n");
2197 printk( "[ BUG: bad unlock balance detected! ]\n");
2198 printk( "-------------------------------------\n");
2199 printk("%s/%d is trying to release lock (",
2200 curr
->comm
, curr
->pid
);
2201 print_lockdep_cache(lock
);
2204 printk("but there are no more locks to release!\n");
2205 printk("\nother info that might help us debug this:\n");
2206 lockdep_print_held_locks(curr
);
2208 printk("\nstack backtrace:\n");
2215 * Common debugging checks for both nested and non-nested unlock:
2217 static int check_unlock(struct task_struct
*curr
, struct lockdep_map
*lock
,
2220 if (unlikely(!debug_locks
))
2222 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2225 if (curr
->lockdep_depth
<= 0)
2226 return print_unlock_inbalance_bug(curr
, lock
, ip
);
2232 * Remove the lock to the list of currently held locks in a
2233 * potentially non-nested (out of order) manner. This is a
2234 * relatively rare operation, as all the unlock APIs default
2235 * to nested mode (which uses lock_release()):
2238 lock_release_non_nested(struct task_struct
*curr
,
2239 struct lockdep_map
*lock
, unsigned long ip
)
2241 struct held_lock
*hlock
, *prev_hlock
;
2246 * Check whether the lock exists in the current stack
2249 depth
= curr
->lockdep_depth
;
2250 if (DEBUG_LOCKS_WARN_ON(!depth
))
2254 for (i
= depth
-1; i
>= 0; i
--) {
2255 hlock
= curr
->held_locks
+ i
;
2257 * We must not cross into another context:
2259 if (prev_hlock
&& prev_hlock
->irq_context
!= hlock
->irq_context
)
2261 if (hlock
->instance
== lock
)
2265 return print_unlock_inbalance_bug(curr
, lock
, ip
);
2269 * We have the right lock to unlock, 'hlock' points to it.
2270 * Now we remove it from the stack, and add back the other
2271 * entries (if any), recalculating the hash along the way:
2273 curr
->lockdep_depth
= i
;
2274 curr
->curr_chain_key
= hlock
->prev_chain_key
;
2276 for (i
++; i
< depth
; i
++) {
2277 hlock
= curr
->held_locks
+ i
;
2278 if (!__lock_acquire(hlock
->instance
,
2279 hlock
->class->subclass
, hlock
->trylock
,
2280 hlock
->read
, hlock
->check
, hlock
->hardirqs_off
,
2285 if (DEBUG_LOCKS_WARN_ON(curr
->lockdep_depth
!= depth
- 1))
2291 * Remove the lock to the list of currently held locks - this gets
2292 * called on mutex_unlock()/spin_unlock*() (or on a failed
2293 * mutex_lock_interruptible()). This is done for unlocks that nest
2294 * perfectly. (i.e. the current top of the lock-stack is unlocked)
2296 static int lock_release_nested(struct task_struct
*curr
,
2297 struct lockdep_map
*lock
, unsigned long ip
)
2299 struct held_lock
*hlock
;
2303 * Pop off the top of the lock stack:
2305 depth
= curr
->lockdep_depth
- 1;
2306 hlock
= curr
->held_locks
+ depth
;
2309 * Is the unlock non-nested:
2311 if (hlock
->instance
!= lock
)
2312 return lock_release_non_nested(curr
, lock
, ip
);
2313 curr
->lockdep_depth
--;
2315 if (DEBUG_LOCKS_WARN_ON(!depth
&& (hlock
->prev_chain_key
!= 0)))
2318 curr
->curr_chain_key
= hlock
->prev_chain_key
;
2320 #ifdef CONFIG_DEBUG_LOCKDEP
2321 hlock
->prev_chain_key
= 0;
2322 hlock
->class = NULL
;
2323 hlock
->acquire_ip
= 0;
2324 hlock
->irq_context
= 0;
2330 * Remove the lock to the list of currently held locks - this gets
2331 * called on mutex_unlock()/spin_unlock*() (or on a failed
2332 * mutex_lock_interruptible()). This is done for unlocks that nest
2333 * perfectly. (i.e. the current top of the lock-stack is unlocked)
2336 __lock_release(struct lockdep_map
*lock
, int nested
, unsigned long ip
)
2338 struct task_struct
*curr
= current
;
2340 if (!check_unlock(curr
, lock
, ip
))
2344 if (!lock_release_nested(curr
, lock
, ip
))
2347 if (!lock_release_non_nested(curr
, lock
, ip
))
2351 check_chain_key(curr
);
2355 * Check whether we follow the irq-flags state precisely:
2357 static void check_flags(unsigned long flags
)
2359 #if defined(CONFIG_DEBUG_LOCKDEP) && defined(CONFIG_TRACE_IRQFLAGS)
2363 if (irqs_disabled_flags(flags
))
2364 DEBUG_LOCKS_WARN_ON(current
->hardirqs_enabled
);
2366 DEBUG_LOCKS_WARN_ON(!current
->hardirqs_enabled
);
2369 * We dont accurately track softirq state in e.g.
2370 * hardirq contexts (such as on 4KSTACKS), so only
2371 * check if not in hardirq contexts:
2373 if (!hardirq_count()) {
2374 if (softirq_count())
2375 DEBUG_LOCKS_WARN_ON(current
->softirqs_enabled
);
2377 DEBUG_LOCKS_WARN_ON(!current
->softirqs_enabled
);
2381 print_irqtrace_events(current
);
2386 * We are not always called with irqs disabled - do that here,
2387 * and also avoid lockdep recursion:
2389 void lock_acquire(struct lockdep_map
*lock
, unsigned int subclass
,
2390 int trylock
, int read
, int check
, unsigned long ip
)
2392 unsigned long flags
;
2394 if (unlikely(current
->lockdep_recursion
))
2397 raw_local_irq_save(flags
);
2400 current
->lockdep_recursion
= 1;
2401 __lock_acquire(lock
, subclass
, trylock
, read
, check
,
2402 irqs_disabled_flags(flags
), ip
);
2403 current
->lockdep_recursion
= 0;
2404 raw_local_irq_restore(flags
);
2407 EXPORT_SYMBOL_GPL(lock_acquire
);
2409 void lock_release(struct lockdep_map
*lock
, int nested
, unsigned long ip
)
2411 unsigned long flags
;
2413 if (unlikely(current
->lockdep_recursion
))
2416 raw_local_irq_save(flags
);
2418 current
->lockdep_recursion
= 1;
2419 __lock_release(lock
, nested
, ip
);
2420 current
->lockdep_recursion
= 0;
2421 raw_local_irq_restore(flags
);
2424 EXPORT_SYMBOL_GPL(lock_release
);
2427 * Used by the testsuite, sanitize the validator state
2428 * after a simulated failure:
2431 void lockdep_reset(void)
2433 unsigned long flags
;
2435 raw_local_irq_save(flags
);
2436 current
->curr_chain_key
= 0;
2437 current
->lockdep_depth
= 0;
2438 current
->lockdep_recursion
= 0;
2439 memset(current
->held_locks
, 0, MAX_LOCK_DEPTH
*sizeof(struct held_lock
));
2440 nr_hardirq_chains
= 0;
2441 nr_softirq_chains
= 0;
2442 nr_process_chains
= 0;
2444 raw_local_irq_restore(flags
);
2447 static void zap_class(struct lock_class
*class)
2452 * Remove all dependencies this lock is
2455 for (i
= 0; i
< nr_list_entries
; i
++) {
2456 if (list_entries
[i
].class == class)
2457 list_del_rcu(&list_entries
[i
].entry
);
2460 * Unhash the class and remove it from the all_lock_classes list:
2462 list_del_rcu(&class->hash_entry
);
2463 list_del_rcu(&class->lock_entry
);
2467 static inline int within(void *addr
, void *start
, unsigned long size
)
2469 return addr
>= start
&& addr
< start
+ size
;
2472 void lockdep_free_key_range(void *start
, unsigned long size
)
2474 struct lock_class
*class, *next
;
2475 struct list_head
*head
;
2476 unsigned long flags
;
2479 raw_local_irq_save(flags
);
2480 __raw_spin_lock(&hash_lock
);
2483 * Unhash all classes that were created by this module:
2485 for (i
= 0; i
< CLASSHASH_SIZE
; i
++) {
2486 head
= classhash_table
+ i
;
2487 if (list_empty(head
))
2489 list_for_each_entry_safe(class, next
, head
, hash_entry
)
2490 if (within(class->key
, start
, size
))
2494 __raw_spin_unlock(&hash_lock
);
2495 raw_local_irq_restore(flags
);
2498 void lockdep_reset_lock(struct lockdep_map
*lock
)
2500 struct lock_class
*class, *next
;
2501 struct list_head
*head
;
2502 unsigned long flags
;
2505 raw_local_irq_save(flags
);
2508 * Remove all classes this lock might have:
2510 for (j
= 0; j
< MAX_LOCKDEP_SUBCLASSES
; j
++) {
2512 * If the class exists we look it up and zap it:
2514 class = look_up_lock_class(lock
, j
);
2519 * Debug check: in the end all mapped classes should
2522 __raw_spin_lock(&hash_lock
);
2523 for (i
= 0; i
< CLASSHASH_SIZE
; i
++) {
2524 head
= classhash_table
+ i
;
2525 if (list_empty(head
))
2527 list_for_each_entry_safe(class, next
, head
, hash_entry
) {
2528 if (unlikely(class == lock
->class_cache
)) {
2529 __raw_spin_unlock(&hash_lock
);
2530 DEBUG_LOCKS_WARN_ON(1);
2535 __raw_spin_unlock(&hash_lock
);
2538 raw_local_irq_restore(flags
);
2541 void __init
lockdep_init(void)
2546 * Some architectures have their own start_kernel()
2547 * code which calls lockdep_init(), while we also
2548 * call lockdep_init() from the start_kernel() itself,
2549 * and we want to initialize the hashes only once:
2551 if (lockdep_initialized
)
2554 for (i
= 0; i
< CLASSHASH_SIZE
; i
++)
2555 INIT_LIST_HEAD(classhash_table
+ i
);
2557 for (i
= 0; i
< CHAINHASH_SIZE
; i
++)
2558 INIT_LIST_HEAD(chainhash_table
+ i
);
2560 lockdep_initialized
= 1;
2563 void __init
lockdep_info(void)
2565 printk("Lock dependency validator: Copyright (c) 2006 Red Hat, Inc., Ingo Molnar\n");
2567 printk("... MAX_LOCKDEP_SUBCLASSES: %lu\n", MAX_LOCKDEP_SUBCLASSES
);
2568 printk("... MAX_LOCK_DEPTH: %lu\n", MAX_LOCK_DEPTH
);
2569 printk("... MAX_LOCKDEP_KEYS: %lu\n", MAX_LOCKDEP_KEYS
);
2570 printk("... CLASSHASH_SIZE: %lu\n", CLASSHASH_SIZE
);
2571 printk("... MAX_LOCKDEP_ENTRIES: %lu\n", MAX_LOCKDEP_ENTRIES
);
2572 printk("... MAX_LOCKDEP_CHAINS: %lu\n", MAX_LOCKDEP_CHAINS
);
2573 printk("... CHAINHASH_SIZE: %lu\n", CHAINHASH_SIZE
);
2575 printk(" memory used by lock dependency info: %lu kB\n",
2576 (sizeof(struct lock_class
) * MAX_LOCKDEP_KEYS
+
2577 sizeof(struct list_head
) * CLASSHASH_SIZE
+
2578 sizeof(struct lock_list
) * MAX_LOCKDEP_ENTRIES
+
2579 sizeof(struct lock_chain
) * MAX_LOCKDEP_CHAINS
+
2580 sizeof(struct list_head
) * CHAINHASH_SIZE
) / 1024);
2582 printk(" per task-struct memory footprint: %lu bytes\n",
2583 sizeof(struct held_lock
) * MAX_LOCK_DEPTH
);
2585 #ifdef CONFIG_DEBUG_LOCKDEP
2586 if (lockdep_init_error
)
2587 printk("WARNING: lockdep init error! Arch code didnt call lockdep_init() early enough?\n");
2591 static inline int in_range(const void *start
, const void *addr
, const void *end
)
2593 return addr
>= start
&& addr
<= end
;
2597 print_freed_lock_bug(struct task_struct
*curr
, const void *mem_from
,
2598 const void *mem_to
, struct held_lock
*hlock
)
2600 if (!debug_locks_off())
2602 if (debug_locks_silent
)
2605 printk("\n=========================\n");
2606 printk( "[ BUG: held lock freed! ]\n");
2607 printk( "-------------------------\n");
2608 printk("%s/%d is freeing memory %p-%p, with a lock still held there!\n",
2609 curr
->comm
, curr
->pid
, mem_from
, mem_to
-1);
2611 lockdep_print_held_locks(curr
);
2613 printk("\nstack backtrace:\n");
2618 * Called when kernel memory is freed (or unmapped), or if a lock
2619 * is destroyed or reinitialized - this code checks whether there is
2620 * any held lock in the memory range of <from> to <to>:
2622 void debug_check_no_locks_freed(const void *mem_from
, unsigned long mem_len
)
2624 const void *mem_to
= mem_from
+ mem_len
, *lock_from
, *lock_to
;
2625 struct task_struct
*curr
= current
;
2626 struct held_lock
*hlock
;
2627 unsigned long flags
;
2630 if (unlikely(!debug_locks
))
2633 local_irq_save(flags
);
2634 for (i
= 0; i
< curr
->lockdep_depth
; i
++) {
2635 hlock
= curr
->held_locks
+ i
;
2637 lock_from
= (void *)hlock
->instance
;
2638 lock_to
= (void *)(hlock
->instance
+ 1);
2640 if (!in_range(mem_from
, lock_from
, mem_to
) &&
2641 !in_range(mem_from
, lock_to
, mem_to
))
2644 print_freed_lock_bug(curr
, mem_from
, mem_to
, hlock
);
2647 local_irq_restore(flags
);
2649 EXPORT_SYMBOL_GPL(debug_check_no_locks_freed
);
2651 static void print_held_locks_bug(struct task_struct
*curr
)
2653 if (!debug_locks_off())
2655 if (debug_locks_silent
)
2658 printk("\n=====================================\n");
2659 printk( "[ BUG: lock held at task exit time! ]\n");
2660 printk( "-------------------------------------\n");
2661 printk("%s/%d is exiting with locks still held!\n",
2662 curr
->comm
, curr
->pid
);
2663 lockdep_print_held_locks(curr
);
2665 printk("\nstack backtrace:\n");
2669 void debug_check_no_locks_held(struct task_struct
*task
)
2671 if (unlikely(task
->lockdep_depth
> 0))
2672 print_held_locks_bug(task
);
2675 void debug_show_all_locks(void)
2677 struct task_struct
*g
, *p
;
2681 printk("\nShowing all locks held in the system:\n");
2684 * Here we try to get the tasklist_lock as hard as possible,
2685 * if not successful after 2 seconds we ignore it (but keep
2686 * trying). This is to enable a debug printout even if a
2687 * tasklist_lock-holding task deadlocks or crashes.
2690 if (!read_trylock(&tasklist_lock
)) {
2692 printk("hm, tasklist_lock locked, retrying... ");
2695 printk(" #%d", 10-count
);
2699 printk(" ignoring it.\n");
2703 printk(" locked it.\n");
2705 do_each_thread(g
, p
) {
2706 if (p
->lockdep_depth
)
2707 lockdep_print_held_locks(p
);
2709 if (read_trylock(&tasklist_lock
))
2711 } while_each_thread(g
, p
);
2714 printk("=============================================\n\n");
2717 read_unlock(&tasklist_lock
);
2720 EXPORT_SYMBOL_GPL(debug_show_all_locks
);
2722 void debug_show_held_locks(struct task_struct
*task
)
2724 lockdep_print_held_locks(task
);
2727 EXPORT_SYMBOL_GPL(debug_show_held_locks
);