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>
40 #include <asm/sections.h>
42 #include "lockdep_internals.h"
45 * hash_lock: protects the lockdep hashes and class/list/hash allocators.
47 * This is one of the rare exceptions where it's justified
48 * to use a raw spinlock - we really dont want the spinlock
49 * code to recurse back into the lockdep code.
51 static raw_spinlock_t hash_lock
= (raw_spinlock_t
)__RAW_SPIN_LOCK_UNLOCKED
;
53 static int lockdep_initialized
;
55 unsigned long nr_list_entries
;
56 static struct lock_list list_entries
[MAX_LOCKDEP_ENTRIES
];
59 * Allocate a lockdep entry. (assumes hash_lock held, returns
60 * with NULL on failure)
62 static struct lock_list
*alloc_list_entry(void)
64 if (nr_list_entries
>= MAX_LOCKDEP_ENTRIES
) {
65 __raw_spin_unlock(&hash_lock
);
67 printk("BUG: MAX_LOCKDEP_ENTRIES too low!\n");
68 printk("turning off the locking correctness validator.\n");
71 return list_entries
+ nr_list_entries
++;
75 * All data structures here are protected by the global debug_lock.
77 * Mutex key structs only get allocated, once during bootup, and never
78 * get freed - this significantly simplifies the debugging code.
80 unsigned long nr_lock_classes
;
81 static struct lock_class lock_classes
[MAX_LOCKDEP_KEYS
];
84 * We keep a global list of all lock classes. The list only grows,
85 * never shrinks. The list is only accessed with the lockdep
88 LIST_HEAD(all_lock_classes
);
91 * The lockdep classes are in a hash-table as well, for fast lookup:
93 #define CLASSHASH_BITS (MAX_LOCKDEP_KEYS_BITS - 1)
94 #define CLASSHASH_SIZE (1UL << CLASSHASH_BITS)
95 #define CLASSHASH_MASK (CLASSHASH_SIZE - 1)
96 #define __classhashfn(key) ((((unsigned long)key >> CLASSHASH_BITS) + (unsigned long)key) & CLASSHASH_MASK)
97 #define classhashentry(key) (classhash_table + __classhashfn((key)))
99 static struct list_head classhash_table
[CLASSHASH_SIZE
];
101 unsigned long nr_lock_chains
;
102 static struct lock_chain lock_chains
[MAX_LOCKDEP_CHAINS
];
105 * We put the lock dependency chains into a hash-table as well, to cache
108 #define CHAINHASH_BITS (MAX_LOCKDEP_CHAINS_BITS-1)
109 #define CHAINHASH_SIZE (1UL << CHAINHASH_BITS)
110 #define CHAINHASH_MASK (CHAINHASH_SIZE - 1)
111 #define __chainhashfn(chain) \
112 (((chain >> CHAINHASH_BITS) + chain) & CHAINHASH_MASK)
113 #define chainhashentry(chain) (chainhash_table + __chainhashfn((chain)))
115 static struct list_head chainhash_table
[CHAINHASH_SIZE
];
118 * The hash key of the lock dependency chains is a hash itself too:
119 * it's a hash of all locks taken up to that lock, including that lock.
120 * It's a 64-bit hash, because it's important for the keys to be
123 #define iterate_chain_key(key1, key2) \
124 (((key1) << MAX_LOCKDEP_KEYS_BITS/2) ^ \
125 ((key1) >> (64-MAX_LOCKDEP_KEYS_BITS/2)) ^ \
128 void lockdep_off(void)
130 current
->lockdep_recursion
++;
133 EXPORT_SYMBOL(lockdep_off
);
135 void lockdep_on(void)
137 current
->lockdep_recursion
--;
140 EXPORT_SYMBOL(lockdep_on
);
142 int lockdep_internal(void)
144 return current
->lockdep_recursion
!= 0;
147 EXPORT_SYMBOL(lockdep_internal
);
150 * Debugging switches:
155 # define VERY_VERBOSE 0
159 # define HARDIRQ_VERBOSE 1
160 # define SOFTIRQ_VERBOSE 1
162 # define HARDIRQ_VERBOSE 0
163 # define SOFTIRQ_VERBOSE 0
166 #if VERBOSE || HARDIRQ_VERBOSE || SOFTIRQ_VERBOSE
168 * Quick filtering for interesting events:
170 static int class_filter(struct lock_class
*class)
172 if (class->name_version
== 1 &&
173 !strcmp(class->name
, "&rl->lock"))
175 if (class->name_version
== 1 &&
176 !strcmp(class->name
, "&ni->mrec_lock"))
178 if (class->name_version
== 1 &&
179 !strcmp(class->name
, "mft_ni_runlist_lock"))
181 if (class->name_version
== 1 &&
182 !strcmp(class->name
, "mft_ni_mrec_lock"))
184 if (class->name_version
== 1 &&
185 !strcmp(class->name
, "&vol->lcnbmp_lock"))
191 static int verbose(struct lock_class
*class)
194 return class_filter(class);
199 #ifdef CONFIG_TRACE_IRQFLAGS
201 static int hardirq_verbose(struct lock_class
*class)
204 return class_filter(class);
209 static int softirq_verbose(struct lock_class
*class)
212 return class_filter(class);
220 * Stack-trace: tightly packed array of stack backtrace
221 * addresses. Protected by the hash_lock.
223 unsigned long nr_stack_trace_entries
;
224 static unsigned long stack_trace
[MAX_STACK_TRACE_ENTRIES
];
226 static int save_trace(struct stack_trace
*trace
)
228 trace
->nr_entries
= 0;
229 trace
->max_entries
= MAX_STACK_TRACE_ENTRIES
- nr_stack_trace_entries
;
230 trace
->entries
= stack_trace
+ nr_stack_trace_entries
;
232 save_stack_trace(trace
, NULL
, 0, 3);
234 trace
->max_entries
= trace
->nr_entries
;
236 nr_stack_trace_entries
+= trace
->nr_entries
;
237 if (DEBUG_LOCKS_WARN_ON(nr_stack_trace_entries
> MAX_STACK_TRACE_ENTRIES
))
240 if (nr_stack_trace_entries
== MAX_STACK_TRACE_ENTRIES
) {
241 __raw_spin_unlock(&hash_lock
);
242 if (debug_locks_off()) {
243 printk("BUG: MAX_STACK_TRACE_ENTRIES too low!\n");
244 printk("turning off the locking correctness validator.\n");
253 unsigned int nr_hardirq_chains
;
254 unsigned int nr_softirq_chains
;
255 unsigned int nr_process_chains
;
256 unsigned int max_lockdep_depth
;
257 unsigned int max_recursion_depth
;
259 #ifdef CONFIG_DEBUG_LOCKDEP
261 * We cannot printk in early bootup code. Not even early_printk()
262 * might work. So we mark any initialization errors and printk
263 * about it later on, in lockdep_info().
265 static int lockdep_init_error
;
268 * Various lockdep statistics:
270 atomic_t chain_lookup_hits
;
271 atomic_t chain_lookup_misses
;
272 atomic_t hardirqs_on_events
;
273 atomic_t hardirqs_off_events
;
274 atomic_t redundant_hardirqs_on
;
275 atomic_t redundant_hardirqs_off
;
276 atomic_t softirqs_on_events
;
277 atomic_t softirqs_off_events
;
278 atomic_t redundant_softirqs_on
;
279 atomic_t redundant_softirqs_off
;
280 atomic_t nr_unused_locks
;
281 atomic_t nr_cyclic_checks
;
282 atomic_t nr_cyclic_check_recursions
;
283 atomic_t nr_find_usage_forwards_checks
;
284 atomic_t nr_find_usage_forwards_recursions
;
285 atomic_t nr_find_usage_backwards_checks
;
286 atomic_t nr_find_usage_backwards_recursions
;
287 # define debug_atomic_inc(ptr) atomic_inc(ptr)
288 # define debug_atomic_dec(ptr) atomic_dec(ptr)
289 # define debug_atomic_read(ptr) atomic_read(ptr)
291 # define debug_atomic_inc(ptr) do { } while (0)
292 # define debug_atomic_dec(ptr) do { } while (0)
293 # define debug_atomic_read(ptr) 0
300 static const char *usage_str
[] =
302 [LOCK_USED
] = "initial-use ",
303 [LOCK_USED_IN_HARDIRQ
] = "in-hardirq-W",
304 [LOCK_USED_IN_SOFTIRQ
] = "in-softirq-W",
305 [LOCK_ENABLED_SOFTIRQS
] = "softirq-on-W",
306 [LOCK_ENABLED_HARDIRQS
] = "hardirq-on-W",
307 [LOCK_USED_IN_HARDIRQ_READ
] = "in-hardirq-R",
308 [LOCK_USED_IN_SOFTIRQ_READ
] = "in-softirq-R",
309 [LOCK_ENABLED_SOFTIRQS_READ
] = "softirq-on-R",
310 [LOCK_ENABLED_HARDIRQS_READ
] = "hardirq-on-R",
313 const char * __get_key_name(struct lockdep_subclass_key
*key
, char *str
)
315 unsigned long offs
, size
;
318 return kallsyms_lookup((unsigned long)key
, &size
, &offs
, &modname
, str
);
322 get_usage_chars(struct lock_class
*class, char *c1
, char *c2
, char *c3
, char *c4
)
324 *c1
= '.', *c2
= '.', *c3
= '.', *c4
= '.';
326 if (class->usage_mask
& LOCKF_USED_IN_HARDIRQ
)
329 if (class->usage_mask
& LOCKF_ENABLED_HARDIRQS
)
332 if (class->usage_mask
& LOCKF_USED_IN_SOFTIRQ
)
335 if (class->usage_mask
& LOCKF_ENABLED_SOFTIRQS
)
338 if (class->usage_mask
& LOCKF_ENABLED_HARDIRQS_READ
)
340 if (class->usage_mask
& LOCKF_USED_IN_HARDIRQ_READ
) {
342 if (class->usage_mask
& LOCKF_ENABLED_HARDIRQS_READ
)
346 if (class->usage_mask
& LOCKF_ENABLED_SOFTIRQS_READ
)
348 if (class->usage_mask
& LOCKF_USED_IN_SOFTIRQ_READ
) {
350 if (class->usage_mask
& LOCKF_ENABLED_SOFTIRQS_READ
)
355 static void print_lock_name(struct lock_class
*class)
357 char str
[128], c1
, c2
, c3
, c4
;
360 get_usage_chars(class, &c1
, &c2
, &c3
, &c4
);
364 name
= __get_key_name(class->key
, str
);
365 printk(" (%s", name
);
367 printk(" (%s", name
);
368 if (class->name_version
> 1)
369 printk("#%d", class->name_version
);
371 printk("/%d", class->subclass
);
373 printk("){%c%c%c%c}", c1
, c2
, c3
, c4
);
376 static void print_lockdep_cache(struct lockdep_map
*lock
)
383 name
= __get_key_name(lock
->key
->subkeys
, str
);
388 static void print_lock(struct held_lock
*hlock
)
390 print_lock_name(hlock
->class);
392 print_ip_sym(hlock
->acquire_ip
);
395 static void lockdep_print_held_locks(struct task_struct
*curr
)
397 int i
, depth
= curr
->lockdep_depth
;
400 printk("no locks held by %s/%d.\n", curr
->comm
, curr
->pid
);
403 printk("%d lock%s held by %s/%d:\n",
404 depth
, depth
> 1 ? "s" : "", curr
->comm
, curr
->pid
);
406 for (i
= 0; i
< depth
; i
++) {
408 print_lock(curr
->held_locks
+ i
);
412 * Helper to print a nice hierarchy of lock dependencies:
414 static void print_spaces(int nr
)
418 for (i
= 0; i
< nr
; i
++)
422 static void print_lock_class_header(struct lock_class
*class, int depth
)
428 print_lock_name(class);
429 printk(" ops: %lu", class->ops
);
432 for (bit
= 0; bit
< LOCK_USAGE_STATES
; bit
++) {
433 if (class->usage_mask
& (1 << bit
)) {
437 len
+= printk(" %s", usage_str
[bit
]);
438 len
+= printk(" at:\n");
439 print_stack_trace(class->usage_traces
+ bit
, len
);
446 printk(" ... key at: ");
447 print_ip_sym((unsigned long)class->key
);
451 * printk all lock dependencies starting at <entry>:
453 static void print_lock_dependencies(struct lock_class
*class, int depth
)
455 struct lock_list
*entry
;
457 if (DEBUG_LOCKS_WARN_ON(depth
>= 20))
460 print_lock_class_header(class, depth
);
462 list_for_each_entry(entry
, &class->locks_after
, entry
) {
463 DEBUG_LOCKS_WARN_ON(!entry
->class);
464 print_lock_dependencies(entry
->class, depth
+ 1);
467 printk(" ... acquired at:\n");
468 print_stack_trace(&entry
->trace
, 2);
474 * Add a new dependency to the head of the list:
476 static int add_lock_to_list(struct lock_class
*class, struct lock_class
*this,
477 struct list_head
*head
, unsigned long ip
)
479 struct lock_list
*entry
;
481 * Lock not present yet - get a new dependency struct and
482 * add it to the list:
484 entry
= alloc_list_entry();
489 save_trace(&entry
->trace
);
492 * Since we never remove from the dependency list, the list can
493 * be walked lockless by other CPUs, it's only allocation
494 * that must be protected by the spinlock. But this also means
495 * we must make new entries visible only once writes to the
496 * entry become visible - hence the RCU op:
498 list_add_tail_rcu(&entry
->entry
, head
);
504 * Recursive, forwards-direction lock-dependency checking, used for
505 * both noncyclic checking and for hardirq-unsafe/softirq-unsafe
508 * (to keep the stackframe of the recursive functions small we
509 * use these global variables, and we also mark various helper
510 * functions as noinline.)
512 static struct held_lock
*check_source
, *check_target
;
515 * Print a dependency chain entry (this is only done when a deadlock
516 * has been detected):
519 print_circular_bug_entry(struct lock_list
*target
, unsigned int depth
)
521 if (debug_locks_silent
)
523 printk("\n-> #%u", depth
);
524 print_lock_name(target
->class);
526 print_stack_trace(&target
->trace
, 6);
532 * When a circular dependency is detected, print the
536 print_circular_bug_header(struct lock_list
*entry
, unsigned int depth
)
538 struct task_struct
*curr
= current
;
540 __raw_spin_unlock(&hash_lock
);
542 if (debug_locks_silent
)
545 printk("\n=======================================================\n");
546 printk( "[ INFO: possible circular locking dependency detected ]\n");
547 printk( "-------------------------------------------------------\n");
548 printk("%s/%d is trying to acquire lock:\n",
549 curr
->comm
, curr
->pid
);
550 print_lock(check_source
);
551 printk("\nbut task is already holding lock:\n");
552 print_lock(check_target
);
553 printk("\nwhich lock already depends on the new lock.\n\n");
554 printk("\nthe existing dependency chain (in reverse order) is:\n");
556 print_circular_bug_entry(entry
, depth
);
561 static noinline
int print_circular_bug_tail(void)
563 struct task_struct
*curr
= current
;
564 struct lock_list
this;
566 if (debug_locks_silent
)
569 this.class = check_source
->class;
570 save_trace(&this.trace
);
571 print_circular_bug_entry(&this, 0);
573 printk("\nother info that might help us debug this:\n\n");
574 lockdep_print_held_locks(curr
);
576 printk("\nstack backtrace:\n");
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
;
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
;
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
;
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 printk( "------------------------------------------------------\n");
729 printk("%s/%d [HC%u[%lu]:SC%u[%lu]:HE%u:SE%u] is trying to acquire:\n",
730 curr
->comm
, curr
->pid
,
731 curr
->hardirq_context
, hardirq_count() >> HARDIRQ_SHIFT
,
732 curr
->softirq_context
, softirq_count() >> SOFTIRQ_SHIFT
,
733 curr
->hardirqs_enabled
,
734 curr
->softirqs_enabled
);
737 printk("\nand this task is already holding:\n");
739 printk("which would create a new lock dependency:\n");
740 print_lock_name(prev
->class);
742 print_lock_name(next
->class);
745 printk("\nbut this new dependency connects a %s-irq-safe lock:\n",
747 print_lock_name(backwards_match
);
748 printk("\n... which became %s-irq-safe at:\n", irqclass
);
750 print_stack_trace(backwards_match
->usage_traces
+ bit1
, 1);
752 printk("\nto a %s-irq-unsafe lock:\n", irqclass
);
753 print_lock_name(forwards_match
);
754 printk("\n... which became %s-irq-unsafe at:\n", irqclass
);
757 print_stack_trace(forwards_match
->usage_traces
+ bit2
, 1);
759 printk("\nother info that might help us debug this:\n\n");
760 lockdep_print_held_locks(curr
);
762 printk("\nthe %s-irq-safe lock's dependencies:\n", irqclass
);
763 print_lock_dependencies(backwards_match
, 0);
765 printk("\nthe %s-irq-unsafe lock's dependencies:\n", irqclass
);
766 print_lock_dependencies(forwards_match
, 0);
768 printk("\nstack backtrace:\n");
775 check_usage(struct task_struct
*curr
, struct held_lock
*prev
,
776 struct held_lock
*next
, enum lock_usage_bit bit_backwards
,
777 enum lock_usage_bit bit_forwards
, const char *irqclass
)
781 find_usage_bit
= bit_backwards
;
782 /* fills in <backwards_match> */
783 ret
= find_usage_backwards(prev
->class, 0);
784 if (!ret
|| ret
== 1)
787 find_usage_bit
= bit_forwards
;
788 ret
= find_usage_forwards(next
->class, 0);
789 if (!ret
|| ret
== 1)
792 return print_bad_irq_dependency(curr
, prev
, next
,
793 bit_backwards
, bit_forwards
, irqclass
);
799 print_deadlock_bug(struct task_struct
*curr
, struct held_lock
*prev
,
800 struct held_lock
*next
)
803 __raw_spin_unlock(&hash_lock
);
804 if (debug_locks_silent
)
807 printk("\n=============================================\n");
808 printk( "[ INFO: possible recursive locking detected ]\n");
809 printk( "---------------------------------------------\n");
810 printk("%s/%d is trying to acquire lock:\n",
811 curr
->comm
, curr
->pid
);
813 printk("\nbut task is already holding lock:\n");
816 printk("\nother info that might help us debug this:\n");
817 lockdep_print_held_locks(curr
);
819 printk("\nstack backtrace:\n");
826 * Check whether we are holding such a class already.
828 * (Note that this has to be done separately, because the graph cannot
829 * detect such classes of deadlocks.)
831 * Returns: 0 on deadlock detected, 1 on OK, 2 on recursive read
834 check_deadlock(struct task_struct
*curr
, struct held_lock
*next
,
835 struct lockdep_map
*next_instance
, int read
)
837 struct held_lock
*prev
;
840 for (i
= 0; i
< curr
->lockdep_depth
; i
++) {
841 prev
= curr
->held_locks
+ i
;
842 if (prev
->class != next
->class)
845 * Allow read-after-read recursion of the same
846 * lock class (i.e. read_lock(lock)+read_lock(lock)):
848 if ((read
== 2) && prev
->read
)
850 return print_deadlock_bug(curr
, prev
, next
);
856 * There was a chain-cache miss, and we are about to add a new dependency
857 * to a previous lock. We recursively validate the following rules:
859 * - would the adding of the <prev> -> <next> dependency create a
860 * circular dependency in the graph? [== circular deadlock]
862 * - does the new prev->next dependency connect any hardirq-safe lock
863 * (in the full backwards-subgraph starting at <prev>) with any
864 * hardirq-unsafe lock (in the full forwards-subgraph starting at
865 * <next>)? [== illegal lock inversion with hardirq contexts]
867 * - does the new prev->next dependency connect any softirq-safe lock
868 * (in the full backwards-subgraph starting at <prev>) with any
869 * softirq-unsafe lock (in the full forwards-subgraph starting at
870 * <next>)? [== illegal lock inversion with softirq contexts]
872 * any of these scenarios could lead to a deadlock.
874 * Then if all the validations pass, we add the forwards and backwards
878 check_prev_add(struct task_struct
*curr
, struct held_lock
*prev
,
879 struct held_lock
*next
)
881 struct lock_list
*entry
;
885 * Prove that the new <prev> -> <next> dependency would not
886 * create a circular dependency in the graph. (We do this by
887 * forward-recursing into the graph starting at <next>, and
888 * checking whether we can reach <prev>.)
890 * We are using global variables to control the recursion, to
891 * keep the stackframe size of the recursive functions low:
895 if (!(check_noncircular(next
->class, 0)))
896 return print_circular_bug_tail();
898 #ifdef CONFIG_TRACE_IRQFLAGS
900 * Prove that the new dependency does not connect a hardirq-safe
901 * lock with a hardirq-unsafe lock - to achieve this we search
902 * the backwards-subgraph starting at <prev>, and the
903 * forwards-subgraph starting at <next>:
905 if (!check_usage(curr
, prev
, next
, LOCK_USED_IN_HARDIRQ
,
906 LOCK_ENABLED_HARDIRQS
, "hard"))
910 * Prove that the new dependency does not connect a hardirq-safe-read
911 * lock with a hardirq-unsafe lock - to achieve this we search
912 * the backwards-subgraph starting at <prev>, and the
913 * forwards-subgraph starting at <next>:
915 if (!check_usage(curr
, prev
, next
, LOCK_USED_IN_HARDIRQ_READ
,
916 LOCK_ENABLED_HARDIRQS
, "hard-read"))
920 * Prove that the new dependency does not connect a softirq-safe
921 * lock with a softirq-unsafe lock - to achieve this we search
922 * the backwards-subgraph starting at <prev>, and the
923 * forwards-subgraph starting at <next>:
925 if (!check_usage(curr
, prev
, next
, LOCK_USED_IN_SOFTIRQ
,
926 LOCK_ENABLED_SOFTIRQS
, "soft"))
929 * Prove that the new dependency does not connect a softirq-safe-read
930 * lock with a softirq-unsafe lock - to achieve this we search
931 * the backwards-subgraph starting at <prev>, and the
932 * forwards-subgraph starting at <next>:
934 if (!check_usage(curr
, prev
, next
, LOCK_USED_IN_SOFTIRQ_READ
,
935 LOCK_ENABLED_SOFTIRQS
, "soft"))
939 * For recursive read-locks we do all the dependency checks,
940 * but we dont store read-triggered dependencies (only
941 * write-triggered dependencies). This ensures that only the
942 * write-side dependencies matter, and that if for example a
943 * write-lock never takes any other locks, then the reads are
944 * equivalent to a NOP.
946 if (next
->read
== 2 || prev
->read
== 2)
949 * Is the <prev> -> <next> dependency already present?
951 * (this may occur even though this is a new chain: consider
952 * e.g. the L1 -> L2 -> L3 -> L4 and the L5 -> L1 -> L2 -> L3
953 * chains - the second one will be new, but L1 already has
954 * L2 added to its dependency list, due to the first chain.)
956 list_for_each_entry(entry
, &prev
->class->locks_after
, entry
) {
957 if (entry
->class == next
->class)
962 * Ok, all validations passed, add the new lock
963 * to the previous lock's dependency list:
965 ret
= add_lock_to_list(prev
->class, next
->class,
966 &prev
->class->locks_after
, next
->acquire_ip
);
970 * Return value of 2 signals 'dependency already added',
971 * in that case we dont have to add the backlink either.
975 ret
= add_lock_to_list(next
->class, prev
->class,
976 &next
->class->locks_before
, next
->acquire_ip
);
979 * Debugging printouts:
981 if (verbose(prev
->class) || verbose(next
->class)) {
982 __raw_spin_unlock(&hash_lock
);
983 printk("\n new dependency: ");
984 print_lock_name(prev
->class);
986 print_lock_name(next
->class);
989 __raw_spin_lock(&hash_lock
);
995 * Add the dependency to all directly-previous locks that are 'relevant'.
996 * The ones that are relevant are (in increasing distance from curr):
997 * all consecutive trylock entries and the final non-trylock entry - or
998 * the end of this context's lock-chain - whichever comes first.
1001 check_prevs_add(struct task_struct
*curr
, struct held_lock
*next
)
1003 int depth
= curr
->lockdep_depth
;
1004 struct held_lock
*hlock
;
1009 * Depth must not be zero for a non-head lock:
1014 * At least two relevant locks must exist for this
1017 if (curr
->held_locks
[depth
].irq_context
!=
1018 curr
->held_locks
[depth
-1].irq_context
)
1022 hlock
= curr
->held_locks
+ depth
-1;
1024 * Only non-recursive-read entries get new dependencies
1027 if (hlock
->read
!= 2) {
1028 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_end
+ per_cpu_offset(i
);
1086 if ((addr
>= start
) && (addr
< end
))
1094 return is_module_address(addr
);
1098 * To make lock name printouts unique, we calculate a unique
1099 * class->name_version generation counter:
1101 static int count_matching_names(struct lock_class
*new_class
)
1103 struct lock_class
*class;
1106 if (!new_class
->name
)
1109 list_for_each_entry(class, &all_lock_classes
, lock_entry
) {
1110 if (new_class
->key
- new_class
->subclass
== class->key
)
1111 return class->name_version
;
1112 if (class->name
&& !strcmp(class->name
, new_class
->name
))
1113 count
= max(count
, class->name_version
);
1119 extern void __error_too_big_MAX_LOCKDEP_SUBCLASSES(void);
1122 * Register a lock's class in the hash-table, if the class is not present
1123 * yet. Otherwise we look it up. We cache the result in the lock object
1124 * itself, so actual lookup of the hash should be once per lock object.
1126 static inline struct lock_class
*
1127 register_lock_class(struct lockdep_map
*lock
, unsigned int subclass
)
1129 struct lockdep_subclass_key
*key
;
1130 struct list_head
*hash_head
;
1131 struct lock_class
*class;
1133 #ifdef CONFIG_DEBUG_LOCKDEP
1135 * If the architecture calls into lockdep before initializing
1136 * the hashes then we'll warn about it later. (we cannot printk
1139 if (unlikely(!lockdep_initialized
)) {
1141 lockdep_init_error
= 1;
1146 * Static locks do not have their class-keys yet - for them the key
1147 * is the lock object itself:
1149 if (unlikely(!lock
->key
))
1150 lock
->key
= (void *)lock
;
1153 * NOTE: the class-key must be unique. For dynamic locks, a static
1154 * lock_class_key variable is passed in through the mutex_init()
1155 * (or spin_lock_init()) call - which acts as the key. For static
1156 * locks we use the lock object itself as the key.
1158 if (sizeof(struct lock_class_key
) > sizeof(struct lock_class
))
1159 __error_too_big_MAX_LOCKDEP_SUBCLASSES();
1161 key
= lock
->key
->subkeys
+ subclass
;
1163 hash_head
= classhashentry(key
);
1166 * We can walk the hash lockfree, because the hash only
1167 * grows, and we are careful when adding entries to the end:
1169 list_for_each_entry(class, hash_head
, hash_entry
)
1170 if (class->key
== key
)
1174 * Debug-check: all keys must be persistent!
1176 if (!static_obj(lock
->key
)) {
1178 printk("INFO: trying to register non-static key.\n");
1179 printk("the code is fine but needs lockdep annotation.\n");
1180 printk("turning off the locking correctness validator.\n");
1186 __raw_spin_lock(&hash_lock
);
1188 * We have to do the hash-walk again, to avoid races
1191 list_for_each_entry(class, hash_head
, hash_entry
)
1192 if (class->key
== key
)
1193 goto out_unlock_set
;
1195 * Allocate a new key from the static array, and add it to
1198 if (nr_lock_classes
>= MAX_LOCKDEP_KEYS
) {
1199 __raw_spin_unlock(&hash_lock
);
1201 printk("BUG: MAX_LOCKDEP_KEYS too low!\n");
1202 printk("turning off the locking correctness validator.\n");
1205 class = lock_classes
+ nr_lock_classes
++;
1206 debug_atomic_inc(&nr_unused_locks
);
1208 class->name
= lock
->name
;
1209 class->subclass
= subclass
;
1210 INIT_LIST_HEAD(&class->lock_entry
);
1211 INIT_LIST_HEAD(&class->locks_before
);
1212 INIT_LIST_HEAD(&class->locks_after
);
1213 class->name_version
= count_matching_names(class);
1215 * We use RCU's safe list-add method to make
1216 * parallel walking of the hash-list safe:
1218 list_add_tail_rcu(&class->hash_entry
, hash_head
);
1220 if (verbose(class)) {
1221 __raw_spin_unlock(&hash_lock
);
1222 printk("\nnew class %p: %s", class->key
, class->name
);
1223 if (class->name_version
> 1)
1224 printk("#%d", class->name_version
);
1227 __raw_spin_lock(&hash_lock
);
1230 __raw_spin_unlock(&hash_lock
);
1233 lock
->class[subclass
] = class;
1235 DEBUG_LOCKS_WARN_ON(class->subclass
!= subclass
);
1241 * Look up a dependency chain. If the key is not present yet then
1242 * add it and return 0 - in this case the new dependency chain is
1243 * validated. If the key is already hashed, return 1.
1245 static inline int lookup_chain_cache(u64 chain_key
)
1247 struct list_head
*hash_head
= chainhashentry(chain_key
);
1248 struct lock_chain
*chain
;
1250 DEBUG_LOCKS_WARN_ON(!irqs_disabled());
1252 * We can walk it lock-free, because entries only get added
1255 list_for_each_entry(chain
, hash_head
, entry
) {
1256 if (chain
->chain_key
== chain_key
) {
1258 debug_atomic_inc(&chain_lookup_hits
);
1260 * In the debugging case, force redundant checking
1263 #ifdef CONFIG_DEBUG_LOCKDEP
1264 __raw_spin_lock(&hash_lock
);
1271 * Allocate a new chain entry from the static array, and add
1274 __raw_spin_lock(&hash_lock
);
1276 * We have to walk the chain again locked - to avoid duplicates:
1278 list_for_each_entry(chain
, hash_head
, entry
) {
1279 if (chain
->chain_key
== chain_key
) {
1280 __raw_spin_unlock(&hash_lock
);
1284 if (unlikely(nr_lock_chains
>= MAX_LOCKDEP_CHAINS
)) {
1285 __raw_spin_unlock(&hash_lock
);
1287 printk("BUG: MAX_LOCKDEP_CHAINS too low!\n");
1288 printk("turning off the locking correctness validator.\n");
1291 chain
= lock_chains
+ nr_lock_chains
++;
1292 chain
->chain_key
= chain_key
;
1293 list_add_tail_rcu(&chain
->entry
, hash_head
);
1294 debug_atomic_inc(&chain_lookup_misses
);
1295 #ifdef CONFIG_TRACE_IRQFLAGS
1296 if (current
->hardirq_context
)
1297 nr_hardirq_chains
++;
1299 if (current
->softirq_context
)
1300 nr_softirq_chains
++;
1302 nr_process_chains
++;
1305 nr_process_chains
++;
1312 * We are building curr_chain_key incrementally, so double-check
1313 * it from scratch, to make sure that it's done correctly:
1315 static void check_chain_key(struct task_struct
*curr
)
1317 #ifdef CONFIG_DEBUG_LOCKDEP
1318 struct held_lock
*hlock
, *prev_hlock
= NULL
;
1322 for (i
= 0; i
< curr
->lockdep_depth
; i
++) {
1323 hlock
= curr
->held_locks
+ i
;
1324 if (chain_key
!= hlock
->prev_chain_key
) {
1326 printk("hm#1, depth: %u [%u], %016Lx != %016Lx\n",
1327 curr
->lockdep_depth
, i
,
1328 (unsigned long long)chain_key
,
1329 (unsigned long long)hlock
->prev_chain_key
);
1333 id
= hlock
->class - lock_classes
;
1334 DEBUG_LOCKS_WARN_ON(id
>= MAX_LOCKDEP_KEYS
);
1335 if (prev_hlock
&& (prev_hlock
->irq_context
!=
1336 hlock
->irq_context
))
1338 chain_key
= iterate_chain_key(chain_key
, id
);
1341 if (chain_key
!= curr
->curr_chain_key
) {
1343 printk("hm#2, depth: %u [%u], %016Lx != %016Lx\n",
1344 curr
->lockdep_depth
, i
,
1345 (unsigned long long)chain_key
,
1346 (unsigned long long)curr
->curr_chain_key
);
1352 #ifdef CONFIG_TRACE_IRQFLAGS
1355 * print irq inversion bug:
1358 print_irq_inversion_bug(struct task_struct
*curr
, struct lock_class
*other
,
1359 struct held_lock
*this, int forwards
,
1360 const char *irqclass
)
1362 __raw_spin_unlock(&hash_lock
);
1364 if (debug_locks_silent
)
1367 printk("\n=========================================================\n");
1368 printk( "[ INFO: possible irq lock inversion dependency detected ]\n");
1369 printk( "---------------------------------------------------------\n");
1370 printk("%s/%d just changed the state of lock:\n",
1371 curr
->comm
, curr
->pid
);
1374 printk("but this lock took another, %s-irq-unsafe lock in the past:\n", irqclass
);
1376 printk("but this lock was taken by another, %s-irq-safe lock in the past:\n", irqclass
);
1377 print_lock_name(other
);
1378 printk("\n\nand interrupts could create inverse lock ordering between them.\n\n");
1380 printk("\nother info that might help us debug this:\n");
1381 lockdep_print_held_locks(curr
);
1383 printk("\nthe first lock's dependencies:\n");
1384 print_lock_dependencies(this->class, 0);
1386 printk("\nthe second lock's dependencies:\n");
1387 print_lock_dependencies(other
, 0);
1389 printk("\nstack backtrace:\n");
1396 * Prove that in the forwards-direction subgraph starting at <this>
1397 * there is no lock matching <mask>:
1400 check_usage_forwards(struct task_struct
*curr
, struct held_lock
*this,
1401 enum lock_usage_bit bit
, const char *irqclass
)
1405 find_usage_bit
= bit
;
1406 /* fills in <forwards_match> */
1407 ret
= find_usage_forwards(this->class, 0);
1408 if (!ret
|| ret
== 1)
1411 return print_irq_inversion_bug(curr
, forwards_match
, this, 1, irqclass
);
1415 * Prove that in the backwards-direction subgraph starting at <this>
1416 * there is no lock matching <mask>:
1419 check_usage_backwards(struct task_struct
*curr
, struct held_lock
*this,
1420 enum lock_usage_bit bit
, const char *irqclass
)
1424 find_usage_bit
= bit
;
1425 /* fills in <backwards_match> */
1426 ret
= find_usage_backwards(this->class, 0);
1427 if (!ret
|| ret
== 1)
1430 return print_irq_inversion_bug(curr
, backwards_match
, this, 0, irqclass
);
1433 static inline void print_irqtrace_events(struct task_struct
*curr
)
1435 printk("irq event stamp: %u\n", curr
->irq_events
);
1436 printk("hardirqs last enabled at (%u): ", curr
->hardirq_enable_event
);
1437 print_ip_sym(curr
->hardirq_enable_ip
);
1438 printk("hardirqs last disabled at (%u): ", curr
->hardirq_disable_event
);
1439 print_ip_sym(curr
->hardirq_disable_ip
);
1440 printk("softirqs last enabled at (%u): ", curr
->softirq_enable_event
);
1441 print_ip_sym(curr
->softirq_enable_ip
);
1442 printk("softirqs last disabled at (%u): ", curr
->softirq_disable_event
);
1443 print_ip_sym(curr
->softirq_disable_ip
);
1447 static inline void print_irqtrace_events(struct task_struct
*curr
)
1453 print_usage_bug(struct task_struct
*curr
, struct held_lock
*this,
1454 enum lock_usage_bit prev_bit
, enum lock_usage_bit new_bit
)
1456 __raw_spin_unlock(&hash_lock
);
1458 if (debug_locks_silent
)
1461 printk("\n=================================\n");
1462 printk( "[ INFO: inconsistent lock state ]\n");
1463 printk( "---------------------------------\n");
1465 printk("inconsistent {%s} -> {%s} usage.\n",
1466 usage_str
[prev_bit
], usage_str
[new_bit
]);
1468 printk("%s/%d [HC%u[%lu]:SC%u[%lu]:HE%u:SE%u] takes:\n",
1469 curr
->comm
, curr
->pid
,
1470 trace_hardirq_context(curr
), hardirq_count() >> HARDIRQ_SHIFT
,
1471 trace_softirq_context(curr
), softirq_count() >> SOFTIRQ_SHIFT
,
1472 trace_hardirqs_enabled(curr
),
1473 trace_softirqs_enabled(curr
));
1476 printk("{%s} state was registered at:\n", usage_str
[prev_bit
]);
1477 print_stack_trace(this->class->usage_traces
+ prev_bit
, 1);
1479 print_irqtrace_events(curr
);
1480 printk("\nother info that might help us debug this:\n");
1481 lockdep_print_held_locks(curr
);
1483 printk("\nstack backtrace:\n");
1490 * Print out an error if an invalid bit is set:
1493 valid_state(struct task_struct
*curr
, struct held_lock
*this,
1494 enum lock_usage_bit new_bit
, enum lock_usage_bit bad_bit
)
1496 if (unlikely(this->class->usage_mask
& (1 << bad_bit
)))
1497 return print_usage_bug(curr
, this, bad_bit
, new_bit
);
1501 #define STRICT_READ_CHECKS 1
1504 * Mark a lock with a usage bit, and validate the state transition:
1506 static int mark_lock(struct task_struct
*curr
, struct held_lock
*this,
1507 enum lock_usage_bit new_bit
, unsigned long ip
)
1509 unsigned int new_mask
= 1 << new_bit
, ret
= 1;
1512 * If already set then do not dirty the cacheline,
1513 * nor do any checks:
1515 if (likely(this->class->usage_mask
& new_mask
))
1518 __raw_spin_lock(&hash_lock
);
1520 * Make sure we didnt race:
1522 if (unlikely(this->class->usage_mask
& new_mask
)) {
1523 __raw_spin_unlock(&hash_lock
);
1527 this->class->usage_mask
|= new_mask
;
1529 #ifdef CONFIG_TRACE_IRQFLAGS
1530 if (new_bit
== LOCK_ENABLED_HARDIRQS
||
1531 new_bit
== LOCK_ENABLED_HARDIRQS_READ
)
1532 ip
= curr
->hardirq_enable_ip
;
1533 else if (new_bit
== LOCK_ENABLED_SOFTIRQS
||
1534 new_bit
== LOCK_ENABLED_SOFTIRQS_READ
)
1535 ip
= curr
->softirq_enable_ip
;
1537 if (!save_trace(this->class->usage_traces
+ new_bit
))
1541 #ifdef CONFIG_TRACE_IRQFLAGS
1542 case LOCK_USED_IN_HARDIRQ
:
1543 if (!valid_state(curr
, this, new_bit
, LOCK_ENABLED_HARDIRQS
))
1545 if (!valid_state(curr
, this, new_bit
,
1546 LOCK_ENABLED_HARDIRQS_READ
))
1549 * just marked it hardirq-safe, check that this lock
1550 * took no hardirq-unsafe lock in the past:
1552 if (!check_usage_forwards(curr
, this,
1553 LOCK_ENABLED_HARDIRQS
, "hard"))
1555 #if STRICT_READ_CHECKS
1557 * just marked it hardirq-safe, check that this lock
1558 * took no hardirq-unsafe-read lock in the past:
1560 if (!check_usage_forwards(curr
, this,
1561 LOCK_ENABLED_HARDIRQS_READ
, "hard-read"))
1564 if (hardirq_verbose(this->class))
1567 case LOCK_USED_IN_SOFTIRQ
:
1568 if (!valid_state(curr
, this, new_bit
, LOCK_ENABLED_SOFTIRQS
))
1570 if (!valid_state(curr
, this, new_bit
,
1571 LOCK_ENABLED_SOFTIRQS_READ
))
1574 * just marked it softirq-safe, check that this lock
1575 * took no softirq-unsafe lock in the past:
1577 if (!check_usage_forwards(curr
, this,
1578 LOCK_ENABLED_SOFTIRQS
, "soft"))
1580 #if STRICT_READ_CHECKS
1582 * just marked it softirq-safe, check that this lock
1583 * took no softirq-unsafe-read lock in the past:
1585 if (!check_usage_forwards(curr
, this,
1586 LOCK_ENABLED_SOFTIRQS_READ
, "soft-read"))
1589 if (softirq_verbose(this->class))
1592 case LOCK_USED_IN_HARDIRQ_READ
:
1593 if (!valid_state(curr
, this, new_bit
, LOCK_ENABLED_HARDIRQS
))
1596 * just marked it hardirq-read-safe, check that this lock
1597 * took no hardirq-unsafe lock in the past:
1599 if (!check_usage_forwards(curr
, this,
1600 LOCK_ENABLED_HARDIRQS
, "hard"))
1602 if (hardirq_verbose(this->class))
1605 case LOCK_USED_IN_SOFTIRQ_READ
:
1606 if (!valid_state(curr
, this, new_bit
, LOCK_ENABLED_SOFTIRQS
))
1609 * just marked it softirq-read-safe, check that this lock
1610 * took no softirq-unsafe lock in the past:
1612 if (!check_usage_forwards(curr
, this,
1613 LOCK_ENABLED_SOFTIRQS
, "soft"))
1615 if (softirq_verbose(this->class))
1618 case LOCK_ENABLED_HARDIRQS
:
1619 if (!valid_state(curr
, this, new_bit
, LOCK_USED_IN_HARDIRQ
))
1621 if (!valid_state(curr
, this, new_bit
,
1622 LOCK_USED_IN_HARDIRQ_READ
))
1625 * just marked it hardirq-unsafe, check that no hardirq-safe
1626 * lock in the system ever took it in the past:
1628 if (!check_usage_backwards(curr
, this,
1629 LOCK_USED_IN_HARDIRQ
, "hard"))
1631 #if STRICT_READ_CHECKS
1633 * just marked it hardirq-unsafe, check that no
1634 * hardirq-safe-read lock in the system ever took
1637 if (!check_usage_backwards(curr
, this,
1638 LOCK_USED_IN_HARDIRQ_READ
, "hard-read"))
1641 if (hardirq_verbose(this->class))
1644 case LOCK_ENABLED_SOFTIRQS
:
1645 if (!valid_state(curr
, this, new_bit
, LOCK_USED_IN_SOFTIRQ
))
1647 if (!valid_state(curr
, this, new_bit
,
1648 LOCK_USED_IN_SOFTIRQ_READ
))
1651 * just marked it softirq-unsafe, check that no softirq-safe
1652 * lock in the system ever took it in the past:
1654 if (!check_usage_backwards(curr
, this,
1655 LOCK_USED_IN_SOFTIRQ
, "soft"))
1657 #if STRICT_READ_CHECKS
1659 * just marked it softirq-unsafe, check that no
1660 * softirq-safe-read lock in the system ever took
1663 if (!check_usage_backwards(curr
, this,
1664 LOCK_USED_IN_SOFTIRQ_READ
, "soft-read"))
1667 if (softirq_verbose(this->class))
1670 case LOCK_ENABLED_HARDIRQS_READ
:
1671 if (!valid_state(curr
, this, new_bit
, LOCK_USED_IN_HARDIRQ
))
1673 #if STRICT_READ_CHECKS
1675 * just marked it hardirq-read-unsafe, check that no
1676 * hardirq-safe lock in the system ever took it in the past:
1678 if (!check_usage_backwards(curr
, this,
1679 LOCK_USED_IN_HARDIRQ
, "hard"))
1682 if (hardirq_verbose(this->class))
1685 case LOCK_ENABLED_SOFTIRQS_READ
:
1686 if (!valid_state(curr
, this, new_bit
, LOCK_USED_IN_SOFTIRQ
))
1688 #if STRICT_READ_CHECKS
1690 * just marked it softirq-read-unsafe, check that no
1691 * softirq-safe lock in the system ever took it in the past:
1693 if (!check_usage_backwards(curr
, this,
1694 LOCK_USED_IN_SOFTIRQ
, "soft"))
1697 if (softirq_verbose(this->class))
1703 * Add it to the global list of classes:
1705 list_add_tail_rcu(&this->class->lock_entry
, &all_lock_classes
);
1706 debug_atomic_dec(&nr_unused_locks
);
1714 __raw_spin_unlock(&hash_lock
);
1717 * We must printk outside of the hash_lock:
1720 printk("\nmarked lock as {%s}:\n", usage_str
[new_bit
]);
1722 print_irqtrace_events(curr
);
1729 #ifdef CONFIG_TRACE_IRQFLAGS
1731 * Mark all held locks with a usage bit:
1734 mark_held_locks(struct task_struct
*curr
, int hardirq
, unsigned long ip
)
1736 enum lock_usage_bit usage_bit
;
1737 struct held_lock
*hlock
;
1740 for (i
= 0; i
< curr
->lockdep_depth
; i
++) {
1741 hlock
= curr
->held_locks
+ i
;
1745 usage_bit
= LOCK_ENABLED_HARDIRQS_READ
;
1747 usage_bit
= LOCK_ENABLED_HARDIRQS
;
1750 usage_bit
= LOCK_ENABLED_SOFTIRQS_READ
;
1752 usage_bit
= LOCK_ENABLED_SOFTIRQS
;
1754 if (!mark_lock(curr
, hlock
, usage_bit
, ip
))
1762 * Debugging helper: via this flag we know that we are in
1763 * 'early bootup code', and will warn about any invalid irqs-on event:
1765 static int early_boot_irqs_enabled
;
1767 void early_boot_irqs_off(void)
1769 early_boot_irqs_enabled
= 0;
1772 void early_boot_irqs_on(void)
1774 early_boot_irqs_enabled
= 1;
1778 * Hardirqs will be enabled:
1780 void trace_hardirqs_on(void)
1782 struct task_struct
*curr
= current
;
1785 if (unlikely(!debug_locks
|| current
->lockdep_recursion
))
1788 if (DEBUG_LOCKS_WARN_ON(unlikely(!early_boot_irqs_enabled
)))
1791 if (unlikely(curr
->hardirqs_enabled
)) {
1792 debug_atomic_inc(&redundant_hardirqs_on
);
1795 /* we'll do an OFF -> ON transition: */
1796 curr
->hardirqs_enabled
= 1;
1797 ip
= (unsigned long) __builtin_return_address(0);
1799 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
1801 if (DEBUG_LOCKS_WARN_ON(current
->hardirq_context
))
1804 * We are going to turn hardirqs on, so set the
1805 * usage bit for all held locks:
1807 if (!mark_held_locks(curr
, 1, ip
))
1810 * If we have softirqs enabled, then set the usage
1811 * bit for all held locks. (disabled hardirqs prevented
1812 * this bit from being set before)
1814 if (curr
->softirqs_enabled
)
1815 if (!mark_held_locks(curr
, 0, ip
))
1818 curr
->hardirq_enable_ip
= ip
;
1819 curr
->hardirq_enable_event
= ++curr
->irq_events
;
1820 debug_atomic_inc(&hardirqs_on_events
);
1823 EXPORT_SYMBOL(trace_hardirqs_on
);
1826 * Hardirqs were disabled:
1828 void trace_hardirqs_off(void)
1830 struct task_struct
*curr
= current
;
1832 if (unlikely(!debug_locks
|| current
->lockdep_recursion
))
1835 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
1838 if (curr
->hardirqs_enabled
) {
1840 * We have done an ON -> OFF transition:
1842 curr
->hardirqs_enabled
= 0;
1843 curr
->hardirq_disable_ip
= _RET_IP_
;
1844 curr
->hardirq_disable_event
= ++curr
->irq_events
;
1845 debug_atomic_inc(&hardirqs_off_events
);
1847 debug_atomic_inc(&redundant_hardirqs_off
);
1850 EXPORT_SYMBOL(trace_hardirqs_off
);
1853 * Softirqs will be enabled:
1855 void trace_softirqs_on(unsigned long ip
)
1857 struct task_struct
*curr
= current
;
1859 if (unlikely(!debug_locks
))
1862 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
1865 if (curr
->softirqs_enabled
) {
1866 debug_atomic_inc(&redundant_softirqs_on
);
1871 * We'll do an OFF -> ON transition:
1873 curr
->softirqs_enabled
= 1;
1874 curr
->softirq_enable_ip
= ip
;
1875 curr
->softirq_enable_event
= ++curr
->irq_events
;
1876 debug_atomic_inc(&softirqs_on_events
);
1878 * We are going to turn softirqs on, so set the
1879 * usage bit for all held locks, if hardirqs are
1882 if (curr
->hardirqs_enabled
)
1883 mark_held_locks(curr
, 0, ip
);
1887 * Softirqs were disabled:
1889 void trace_softirqs_off(unsigned long ip
)
1891 struct task_struct
*curr
= current
;
1893 if (unlikely(!debug_locks
))
1896 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
1899 if (curr
->softirqs_enabled
) {
1901 * We have done an ON -> OFF transition:
1903 curr
->softirqs_enabled
= 0;
1904 curr
->softirq_disable_ip
= ip
;
1905 curr
->softirq_disable_event
= ++curr
->irq_events
;
1906 debug_atomic_inc(&softirqs_off_events
);
1907 DEBUG_LOCKS_WARN_ON(!softirq_count());
1909 debug_atomic_inc(&redundant_softirqs_off
);
1915 * Initialize a lock instance's lock-class mapping info:
1917 void lockdep_init_map(struct lockdep_map
*lock
, const char *name
,
1918 struct lock_class_key
*key
)
1920 if (unlikely(!debug_locks
))
1923 if (DEBUG_LOCKS_WARN_ON(!key
))
1925 if (DEBUG_LOCKS_WARN_ON(!name
))
1928 * Sanity check, the lock-class key must be persistent:
1930 if (!static_obj(key
)) {
1931 printk("BUG: key %p not in .data!\n", key
);
1932 DEBUG_LOCKS_WARN_ON(1);
1937 memset(lock
->class, 0, sizeof(lock
->class[0])*MAX_LOCKDEP_SUBCLASSES
);
1940 EXPORT_SYMBOL_GPL(lockdep_init_map
);
1943 * This gets called for every mutex_lock*()/spin_lock*() operation.
1944 * We maintain the dependency maps and validate the locking attempt:
1946 static int __lock_acquire(struct lockdep_map
*lock
, unsigned int subclass
,
1947 int trylock
, int read
, int check
, int hardirqs_off
,
1950 struct task_struct
*curr
= current
;
1951 struct held_lock
*hlock
;
1952 struct lock_class
*class;
1953 unsigned int depth
, id
;
1957 if (unlikely(!debug_locks
))
1960 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
1963 if (unlikely(subclass
>= MAX_LOCKDEP_SUBCLASSES
)) {
1965 printk("BUG: MAX_LOCKDEP_SUBCLASSES too low!\n");
1966 printk("turning off the locking correctness validator.\n");
1970 class = lock
->class[subclass
];
1971 /* not cached yet? */
1972 if (unlikely(!class)) {
1973 class = register_lock_class(lock
, subclass
);
1977 debug_atomic_inc((atomic_t
*)&class->ops
);
1978 if (very_verbose(class)) {
1979 printk("\nacquire class [%p] %s", class->key
, class->name
);
1980 if (class->name_version
> 1)
1981 printk("#%d", class->name_version
);
1987 * Add the lock to the list of currently held locks.
1988 * (we dont increase the depth just yet, up until the
1989 * dependency checks are done)
1991 depth
= curr
->lockdep_depth
;
1992 if (DEBUG_LOCKS_WARN_ON(depth
>= MAX_LOCK_DEPTH
))
1995 hlock
= curr
->held_locks
+ depth
;
1997 hlock
->class = class;
1998 hlock
->acquire_ip
= ip
;
1999 hlock
->instance
= lock
;
2000 hlock
->trylock
= trylock
;
2002 hlock
->check
= check
;
2003 hlock
->hardirqs_off
= hardirqs_off
;
2007 #ifdef CONFIG_TRACE_IRQFLAGS
2009 * If non-trylock use in a hardirq or softirq context, then
2010 * mark the lock as used in these contexts:
2014 if (curr
->hardirq_context
)
2015 if (!mark_lock(curr
, hlock
,
2016 LOCK_USED_IN_HARDIRQ_READ
, ip
))
2018 if (curr
->softirq_context
)
2019 if (!mark_lock(curr
, hlock
,
2020 LOCK_USED_IN_SOFTIRQ_READ
, ip
))
2023 if (curr
->hardirq_context
)
2024 if (!mark_lock(curr
, hlock
, LOCK_USED_IN_HARDIRQ
, ip
))
2026 if (curr
->softirq_context
)
2027 if (!mark_lock(curr
, hlock
, LOCK_USED_IN_SOFTIRQ
, ip
))
2031 if (!hardirqs_off
) {
2033 if (!mark_lock(curr
, hlock
,
2034 LOCK_ENABLED_HARDIRQS_READ
, ip
))
2036 if (curr
->softirqs_enabled
)
2037 if (!mark_lock(curr
, hlock
,
2038 LOCK_ENABLED_SOFTIRQS_READ
, ip
))
2041 if (!mark_lock(curr
, hlock
,
2042 LOCK_ENABLED_HARDIRQS
, ip
))
2044 if (curr
->softirqs_enabled
)
2045 if (!mark_lock(curr
, hlock
,
2046 LOCK_ENABLED_SOFTIRQS
, ip
))
2051 /* mark it as used: */
2052 if (!mark_lock(curr
, hlock
, LOCK_USED
, ip
))
2056 * Calculate the chain hash: it's the combined has of all the
2057 * lock keys along the dependency chain. We save the hash value
2058 * at every step so that we can get the current hash easily
2059 * after unlock. The chain hash is then used to cache dependency
2062 * The 'key ID' is what is the most compact key value to drive
2063 * the hash, not class->key.
2065 id
= class - lock_classes
;
2066 if (DEBUG_LOCKS_WARN_ON(id
>= MAX_LOCKDEP_KEYS
))
2069 chain_key
= curr
->curr_chain_key
;
2071 if (DEBUG_LOCKS_WARN_ON(chain_key
!= 0))
2076 hlock
->prev_chain_key
= chain_key
;
2078 #ifdef CONFIG_TRACE_IRQFLAGS
2080 * Keep track of points where we cross into an interrupt context:
2082 hlock
->irq_context
= 2*(curr
->hardirq_context
? 1 : 0) +
2083 curr
->softirq_context
;
2085 struct held_lock
*prev_hlock
;
2087 prev_hlock
= curr
->held_locks
+ depth
-1;
2089 * If we cross into another context, reset the
2090 * hash key (this also prevents the checking and the
2091 * adding of the dependency to 'prev'):
2093 if (prev_hlock
->irq_context
!= hlock
->irq_context
) {
2099 chain_key
= iterate_chain_key(chain_key
, id
);
2100 curr
->curr_chain_key
= chain_key
;
2103 * Trylock needs to maintain the stack of held locks, but it
2104 * does not add new dependencies, because trylock can be done
2107 * We look up the chain_key and do the O(N^2) check and update of
2108 * the dependencies only if this is a new dependency chain.
2109 * (If lookup_chain_cache() returns with 1 it acquires
2112 if (!trylock
&& (check
== 2) && lookup_chain_cache(chain_key
)) {
2114 * Check whether last held lock:
2116 * - is irq-safe, if this lock is irq-unsafe
2117 * - is softirq-safe, if this lock is hardirq-unsafe
2119 * And check whether the new lock's dependency graph
2120 * could lead back to the previous lock.
2122 * any of these scenarios could lead to a deadlock. If
2125 int ret
= check_deadlock(curr
, hlock
, lock
, read
);
2130 * Mark recursive read, as we jump over it when
2131 * building dependencies (just like we jump over
2137 * Add dependency only if this lock is not the head
2138 * of the chain, and if it's not a secondary read-lock:
2140 if (!chain_head
&& ret
!= 2)
2141 if (!check_prevs_add(curr
, hlock
))
2143 __raw_spin_unlock(&hash_lock
);
2145 curr
->lockdep_depth
++;
2146 check_chain_key(curr
);
2147 if (unlikely(curr
->lockdep_depth
>= MAX_LOCK_DEPTH
)) {
2149 printk("BUG: MAX_LOCK_DEPTH too low!\n");
2150 printk("turning off the locking correctness validator.\n");
2153 if (unlikely(curr
->lockdep_depth
> max_lockdep_depth
))
2154 max_lockdep_depth
= curr
->lockdep_depth
;
2160 print_unlock_inbalance_bug(struct task_struct
*curr
, struct lockdep_map
*lock
,
2163 if (!debug_locks_off())
2165 if (debug_locks_silent
)
2168 printk("\n=====================================\n");
2169 printk( "[ BUG: bad unlock balance detected! ]\n");
2170 printk( "-------------------------------------\n");
2171 printk("%s/%d is trying to release lock (",
2172 curr
->comm
, curr
->pid
);
2173 print_lockdep_cache(lock
);
2176 printk("but there are no more locks to release!\n");
2177 printk("\nother info that might help us debug this:\n");
2178 lockdep_print_held_locks(curr
);
2180 printk("\nstack backtrace:\n");
2187 * Common debugging checks for both nested and non-nested unlock:
2189 static int check_unlock(struct task_struct
*curr
, struct lockdep_map
*lock
,
2192 if (unlikely(!debug_locks
))
2194 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2197 if (curr
->lockdep_depth
<= 0)
2198 return print_unlock_inbalance_bug(curr
, lock
, ip
);
2204 * Remove the lock to the list of currently held locks in a
2205 * potentially non-nested (out of order) manner. This is a
2206 * relatively rare operation, as all the unlock APIs default
2207 * to nested mode (which uses lock_release()):
2210 lock_release_non_nested(struct task_struct
*curr
,
2211 struct lockdep_map
*lock
, unsigned long ip
)
2213 struct held_lock
*hlock
, *prev_hlock
;
2218 * Check whether the lock exists in the current stack
2221 depth
= curr
->lockdep_depth
;
2222 if (DEBUG_LOCKS_WARN_ON(!depth
))
2226 for (i
= depth
-1; i
>= 0; i
--) {
2227 hlock
= curr
->held_locks
+ i
;
2229 * We must not cross into another context:
2231 if (prev_hlock
&& prev_hlock
->irq_context
!= hlock
->irq_context
)
2233 if (hlock
->instance
== lock
)
2237 return print_unlock_inbalance_bug(curr
, lock
, ip
);
2241 * We have the right lock to unlock, 'hlock' points to it.
2242 * Now we remove it from the stack, and add back the other
2243 * entries (if any), recalculating the hash along the way:
2245 curr
->lockdep_depth
= i
;
2246 curr
->curr_chain_key
= hlock
->prev_chain_key
;
2248 for (i
++; i
< depth
; i
++) {
2249 hlock
= curr
->held_locks
+ i
;
2250 if (!__lock_acquire(hlock
->instance
,
2251 hlock
->class->subclass
, hlock
->trylock
,
2252 hlock
->read
, hlock
->check
, hlock
->hardirqs_off
,
2257 if (DEBUG_LOCKS_WARN_ON(curr
->lockdep_depth
!= depth
- 1))
2263 * Remove the lock to the list of currently held locks - this gets
2264 * called on mutex_unlock()/spin_unlock*() (or on a failed
2265 * mutex_lock_interruptible()). This is done for unlocks that nest
2266 * perfectly. (i.e. the current top of the lock-stack is unlocked)
2268 static int lock_release_nested(struct task_struct
*curr
,
2269 struct lockdep_map
*lock
, unsigned long ip
)
2271 struct held_lock
*hlock
;
2275 * Pop off the top of the lock stack:
2277 depth
= curr
->lockdep_depth
- 1;
2278 hlock
= curr
->held_locks
+ depth
;
2281 * Is the unlock non-nested:
2283 if (hlock
->instance
!= lock
)
2284 return lock_release_non_nested(curr
, lock
, ip
);
2285 curr
->lockdep_depth
--;
2287 if (DEBUG_LOCKS_WARN_ON(!depth
&& (hlock
->prev_chain_key
!= 0)))
2290 curr
->curr_chain_key
= hlock
->prev_chain_key
;
2292 #ifdef CONFIG_DEBUG_LOCKDEP
2293 hlock
->prev_chain_key
= 0;
2294 hlock
->class = NULL
;
2295 hlock
->acquire_ip
= 0;
2296 hlock
->irq_context
= 0;
2302 * Remove the lock to the list of currently held locks - this gets
2303 * called on mutex_unlock()/spin_unlock*() (or on a failed
2304 * mutex_lock_interruptible()). This is done for unlocks that nest
2305 * perfectly. (i.e. the current top of the lock-stack is unlocked)
2308 __lock_release(struct lockdep_map
*lock
, int nested
, unsigned long ip
)
2310 struct task_struct
*curr
= current
;
2312 if (!check_unlock(curr
, lock
, ip
))
2316 if (!lock_release_nested(curr
, lock
, ip
))
2319 if (!lock_release_non_nested(curr
, lock
, ip
))
2323 check_chain_key(curr
);
2327 * Check whether we follow the irq-flags state precisely:
2329 static void check_flags(unsigned long flags
)
2331 #if defined(CONFIG_DEBUG_LOCKDEP) && defined(CONFIG_TRACE_IRQFLAGS)
2335 if (irqs_disabled_flags(flags
))
2336 DEBUG_LOCKS_WARN_ON(current
->hardirqs_enabled
);
2338 DEBUG_LOCKS_WARN_ON(!current
->hardirqs_enabled
);
2341 * We dont accurately track softirq state in e.g.
2342 * hardirq contexts (such as on 4KSTACKS), so only
2343 * check if not in hardirq contexts:
2345 if (!hardirq_count()) {
2346 if (softirq_count())
2347 DEBUG_LOCKS_WARN_ON(current
->softirqs_enabled
);
2349 DEBUG_LOCKS_WARN_ON(!current
->softirqs_enabled
);
2353 print_irqtrace_events(current
);
2358 * We are not always called with irqs disabled - do that here,
2359 * and also avoid lockdep recursion:
2361 void lock_acquire(struct lockdep_map
*lock
, unsigned int subclass
,
2362 int trylock
, int read
, int check
, unsigned long ip
)
2364 unsigned long flags
;
2366 if (unlikely(current
->lockdep_recursion
))
2369 raw_local_irq_save(flags
);
2372 current
->lockdep_recursion
= 1;
2373 __lock_acquire(lock
, subclass
, trylock
, read
, check
,
2374 irqs_disabled_flags(flags
), ip
);
2375 current
->lockdep_recursion
= 0;
2376 raw_local_irq_restore(flags
);
2379 EXPORT_SYMBOL_GPL(lock_acquire
);
2381 void lock_release(struct lockdep_map
*lock
, int nested
, unsigned long ip
)
2383 unsigned long flags
;
2385 if (unlikely(current
->lockdep_recursion
))
2388 raw_local_irq_save(flags
);
2390 current
->lockdep_recursion
= 1;
2391 __lock_release(lock
, nested
, ip
);
2392 current
->lockdep_recursion
= 0;
2393 raw_local_irq_restore(flags
);
2396 EXPORT_SYMBOL_GPL(lock_release
);
2399 * Used by the testsuite, sanitize the validator state
2400 * after a simulated failure:
2403 void lockdep_reset(void)
2405 unsigned long flags
;
2407 raw_local_irq_save(flags
);
2408 current
->curr_chain_key
= 0;
2409 current
->lockdep_depth
= 0;
2410 current
->lockdep_recursion
= 0;
2411 memset(current
->held_locks
, 0, MAX_LOCK_DEPTH
*sizeof(struct held_lock
));
2412 nr_hardirq_chains
= 0;
2413 nr_softirq_chains
= 0;
2414 nr_process_chains
= 0;
2416 raw_local_irq_restore(flags
);
2419 static void zap_class(struct lock_class
*class)
2424 * Remove all dependencies this lock is
2427 for (i
= 0; i
< nr_list_entries
; i
++) {
2428 if (list_entries
[i
].class == class)
2429 list_del_rcu(&list_entries
[i
].entry
);
2432 * Unhash the class and remove it from the all_lock_classes list:
2434 list_del_rcu(&class->hash_entry
);
2435 list_del_rcu(&class->lock_entry
);
2439 static inline int within(void *addr
, void *start
, unsigned long size
)
2441 return addr
>= start
&& addr
< start
+ size
;
2444 void lockdep_free_key_range(void *start
, unsigned long size
)
2446 struct lock_class
*class, *next
;
2447 struct list_head
*head
;
2448 unsigned long flags
;
2451 raw_local_irq_save(flags
);
2452 __raw_spin_lock(&hash_lock
);
2455 * Unhash all classes that were created by this module:
2457 for (i
= 0; i
< CLASSHASH_SIZE
; i
++) {
2458 head
= classhash_table
+ i
;
2459 if (list_empty(head
))
2461 list_for_each_entry_safe(class, next
, head
, hash_entry
)
2462 if (within(class->key
, start
, size
))
2466 __raw_spin_unlock(&hash_lock
);
2467 raw_local_irq_restore(flags
);
2470 void lockdep_reset_lock(struct lockdep_map
*lock
)
2472 struct lock_class
*class, *next
, *entry
;
2473 struct list_head
*head
;
2474 unsigned long flags
;
2477 raw_local_irq_save(flags
);
2478 __raw_spin_lock(&hash_lock
);
2481 * Remove all classes this lock has:
2483 for (i
= 0; i
< CLASSHASH_SIZE
; i
++) {
2484 head
= classhash_table
+ i
;
2485 if (list_empty(head
))
2487 list_for_each_entry_safe(class, next
, head
, hash_entry
) {
2488 for (j
= 0; j
< MAX_LOCKDEP_SUBCLASSES
; j
++) {
2489 entry
= lock
->class[j
];
2490 if (class == entry
) {
2492 lock
->class[j
] = NULL
;
2500 * Debug check: in the end all mapped classes should
2503 for (j
= 0; j
< MAX_LOCKDEP_SUBCLASSES
; j
++) {
2504 entry
= lock
->class[j
];
2507 __raw_spin_unlock(&hash_lock
);
2508 DEBUG_LOCKS_WARN_ON(1);
2509 raw_local_irq_restore(flags
);
2513 __raw_spin_unlock(&hash_lock
);
2514 raw_local_irq_restore(flags
);
2517 void __init
lockdep_init(void)
2522 * Some architectures have their own start_kernel()
2523 * code which calls lockdep_init(), while we also
2524 * call lockdep_init() from the start_kernel() itself,
2525 * and we want to initialize the hashes only once:
2527 if (lockdep_initialized
)
2530 for (i
= 0; i
< CLASSHASH_SIZE
; i
++)
2531 INIT_LIST_HEAD(classhash_table
+ i
);
2533 for (i
= 0; i
< CHAINHASH_SIZE
; i
++)
2534 INIT_LIST_HEAD(chainhash_table
+ i
);
2536 lockdep_initialized
= 1;
2539 void __init
lockdep_info(void)
2541 printk("Lock dependency validator: Copyright (c) 2006 Red Hat, Inc., Ingo Molnar\n");
2543 printk("... MAX_LOCKDEP_SUBCLASSES: %lu\n", MAX_LOCKDEP_SUBCLASSES
);
2544 printk("... MAX_LOCK_DEPTH: %lu\n", MAX_LOCK_DEPTH
);
2545 printk("... MAX_LOCKDEP_KEYS: %lu\n", MAX_LOCKDEP_KEYS
);
2546 printk("... CLASSHASH_SIZE: %lu\n", CLASSHASH_SIZE
);
2547 printk("... MAX_LOCKDEP_ENTRIES: %lu\n", MAX_LOCKDEP_ENTRIES
);
2548 printk("... MAX_LOCKDEP_CHAINS: %lu\n", MAX_LOCKDEP_CHAINS
);
2549 printk("... CHAINHASH_SIZE: %lu\n", CHAINHASH_SIZE
);
2551 printk(" memory used by lock dependency info: %lu kB\n",
2552 (sizeof(struct lock_class
) * MAX_LOCKDEP_KEYS
+
2553 sizeof(struct list_head
) * CLASSHASH_SIZE
+
2554 sizeof(struct lock_list
) * MAX_LOCKDEP_ENTRIES
+
2555 sizeof(struct lock_chain
) * MAX_LOCKDEP_CHAINS
+
2556 sizeof(struct list_head
) * CHAINHASH_SIZE
) / 1024);
2558 printk(" per task-struct memory footprint: %lu bytes\n",
2559 sizeof(struct held_lock
) * MAX_LOCK_DEPTH
);
2561 #ifdef CONFIG_DEBUG_LOCKDEP
2562 if (lockdep_init_error
)
2563 printk("WARNING: lockdep init error! Arch code didnt call lockdep_init() early enough?\n");
2567 static inline int in_range(const void *start
, const void *addr
, const void *end
)
2569 return addr
>= start
&& addr
<= end
;
2573 print_freed_lock_bug(struct task_struct
*curr
, const void *mem_from
,
2576 if (!debug_locks_off())
2578 if (debug_locks_silent
)
2581 printk("\n=========================\n");
2582 printk( "[ BUG: held lock freed! ]\n");
2583 printk( "-------------------------\n");
2584 printk("%s/%d is freeing memory %p-%p, with a lock still held there!\n",
2585 curr
->comm
, curr
->pid
, mem_from
, mem_to
-1);
2586 lockdep_print_held_locks(curr
);
2588 printk("\nstack backtrace:\n");
2593 * Called when kernel memory is freed (or unmapped), or if a lock
2594 * is destroyed or reinitialized - this code checks whether there is
2595 * any held lock in the memory range of <from> to <to>:
2597 void debug_check_no_locks_freed(const void *mem_from
, unsigned long mem_len
)
2599 const void *mem_to
= mem_from
+ mem_len
, *lock_from
, *lock_to
;
2600 struct task_struct
*curr
= current
;
2601 struct held_lock
*hlock
;
2602 unsigned long flags
;
2605 if (unlikely(!debug_locks
))
2608 local_irq_save(flags
);
2609 for (i
= 0; i
< curr
->lockdep_depth
; i
++) {
2610 hlock
= curr
->held_locks
+ i
;
2612 lock_from
= (void *)hlock
->instance
;
2613 lock_to
= (void *)(hlock
->instance
+ 1);
2615 if (!in_range(mem_from
, lock_from
, mem_to
) &&
2616 !in_range(mem_from
, lock_to
, mem_to
))
2619 print_freed_lock_bug(curr
, mem_from
, mem_to
);
2622 local_irq_restore(flags
);
2625 static void print_held_locks_bug(struct task_struct
*curr
)
2627 if (!debug_locks_off())
2629 if (debug_locks_silent
)
2632 printk("\n=====================================\n");
2633 printk( "[ BUG: lock held at task exit time! ]\n");
2634 printk( "-------------------------------------\n");
2635 printk("%s/%d is exiting with locks still held!\n",
2636 curr
->comm
, curr
->pid
);
2637 lockdep_print_held_locks(curr
);
2639 printk("\nstack backtrace:\n");
2643 void debug_check_no_locks_held(struct task_struct
*task
)
2645 if (unlikely(task
->lockdep_depth
> 0))
2646 print_held_locks_bug(task
);
2649 void debug_show_all_locks(void)
2651 struct task_struct
*g
, *p
;
2655 printk("\nShowing all locks held in the system:\n");
2658 * Here we try to get the tasklist_lock as hard as possible,
2659 * if not successful after 2 seconds we ignore it (but keep
2660 * trying). This is to enable a debug printout even if a
2661 * tasklist_lock-holding task deadlocks or crashes.
2664 if (!read_trylock(&tasklist_lock
)) {
2666 printk("hm, tasklist_lock locked, retrying... ");
2669 printk(" #%d", 10-count
);
2673 printk(" ignoring it.\n");
2677 printk(" locked it.\n");
2679 do_each_thread(g
, p
) {
2680 if (p
->lockdep_depth
)
2681 lockdep_print_held_locks(p
);
2683 if (read_trylock(&tasklist_lock
))
2685 } while_each_thread(g
, p
);
2688 printk("=============================================\n\n");
2691 read_unlock(&tasklist_lock
);
2694 EXPORT_SYMBOL_GPL(debug_show_all_locks
);
2696 void debug_show_held_locks(struct task_struct
*task
)
2698 lockdep_print_held_locks(task
);
2701 EXPORT_SYMBOL_GPL(debug_show_held_locks
);