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 instance (i.e. read_lock(lock)+read_lock(lock)):
848 if ((read
== 2) && prev
->read
&&
849 (prev
->instance
== next_instance
))
851 return print_deadlock_bug(curr
, prev
, next
);
857 * There was a chain-cache miss, and we are about to add a new dependency
858 * to a previous lock. We recursively validate the following rules:
860 * - would the adding of the <prev> -> <next> dependency create a
861 * circular dependency in the graph? [== circular deadlock]
863 * - does the new prev->next dependency connect any hardirq-safe lock
864 * (in the full backwards-subgraph starting at <prev>) with any
865 * hardirq-unsafe lock (in the full forwards-subgraph starting at
866 * <next>)? [== illegal lock inversion with hardirq contexts]
868 * - does the new prev->next dependency connect any softirq-safe lock
869 * (in the full backwards-subgraph starting at <prev>) with any
870 * softirq-unsafe lock (in the full forwards-subgraph starting at
871 * <next>)? [== illegal lock inversion with softirq contexts]
873 * any of these scenarios could lead to a deadlock.
875 * Then if all the validations pass, we add the forwards and backwards
879 check_prev_add(struct task_struct
*curr
, struct held_lock
*prev
,
880 struct held_lock
*next
)
882 struct lock_list
*entry
;
886 * Prove that the new <prev> -> <next> dependency would not
887 * create a circular dependency in the graph. (We do this by
888 * forward-recursing into the graph starting at <next>, and
889 * checking whether we can reach <prev>.)
891 * We are using global variables to control the recursion, to
892 * keep the stackframe size of the recursive functions low:
896 if (!(check_noncircular(next
->class, 0)))
897 return print_circular_bug_tail();
899 #ifdef CONFIG_TRACE_IRQFLAGS
901 * Prove that the new dependency does not connect a hardirq-safe
902 * lock with a hardirq-unsafe lock - to achieve this we search
903 * the backwards-subgraph starting at <prev>, and the
904 * forwards-subgraph starting at <next>:
906 if (!check_usage(curr
, prev
, next
, LOCK_USED_IN_HARDIRQ
,
907 LOCK_ENABLED_HARDIRQS
, "hard"))
911 * Prove that the new dependency does not connect a hardirq-safe-read
912 * lock with a hardirq-unsafe lock - to achieve this we search
913 * the backwards-subgraph starting at <prev>, and the
914 * forwards-subgraph starting at <next>:
916 if (!check_usage(curr
, prev
, next
, LOCK_USED_IN_HARDIRQ_READ
,
917 LOCK_ENABLED_HARDIRQS
, "hard-read"))
921 * Prove that the new dependency does not connect a softirq-safe
922 * lock with a softirq-unsafe lock - to achieve this we search
923 * the backwards-subgraph starting at <prev>, and the
924 * forwards-subgraph starting at <next>:
926 if (!check_usage(curr
, prev
, next
, LOCK_USED_IN_SOFTIRQ
,
927 LOCK_ENABLED_SOFTIRQS
, "soft"))
930 * Prove that the new dependency does not connect a softirq-safe-read
931 * lock with a softirq-unsafe lock - to achieve this we search
932 * the backwards-subgraph starting at <prev>, and the
933 * forwards-subgraph starting at <next>:
935 if (!check_usage(curr
, prev
, next
, LOCK_USED_IN_SOFTIRQ_READ
,
936 LOCK_ENABLED_SOFTIRQS
, "soft"))
940 * For recursive read-locks we do all the dependency checks,
941 * but we dont store read-triggered dependencies (only
942 * write-triggered dependencies). This ensures that only the
943 * write-side dependencies matter, and that if for example a
944 * write-lock never takes any other locks, then the reads are
945 * equivalent to a NOP.
947 if (next
->read
== 2 || prev
->read
== 2)
950 * Is the <prev> -> <next> dependency already present?
952 * (this may occur even though this is a new chain: consider
953 * e.g. the L1 -> L2 -> L3 -> L4 and the L5 -> L1 -> L2 -> L3
954 * chains - the second one will be new, but L1 already has
955 * L2 added to its dependency list, due to the first chain.)
957 list_for_each_entry(entry
, &prev
->class->locks_after
, entry
) {
958 if (entry
->class == next
->class)
963 * Ok, all validations passed, add the new lock
964 * to the previous lock's dependency list:
966 ret
= add_lock_to_list(prev
->class, next
->class,
967 &prev
->class->locks_after
, next
->acquire_ip
);
971 * Return value of 2 signals 'dependency already added',
972 * in that case we dont have to add the backlink either.
976 ret
= add_lock_to_list(next
->class, prev
->class,
977 &next
->class->locks_before
, next
->acquire_ip
);
980 * Debugging printouts:
982 if (verbose(prev
->class) || verbose(next
->class)) {
983 __raw_spin_unlock(&hash_lock
);
984 printk("\n new dependency: ");
985 print_lock_name(prev
->class);
987 print_lock_name(next
->class);
990 __raw_spin_lock(&hash_lock
);
996 * Add the dependency to all directly-previous locks that are 'relevant'.
997 * The ones that are relevant are (in increasing distance from curr):
998 * all consecutive trylock entries and the final non-trylock entry - or
999 * the end of this context's lock-chain - whichever comes first.
1002 check_prevs_add(struct task_struct
*curr
, struct held_lock
*next
)
1004 int depth
= curr
->lockdep_depth
;
1005 struct held_lock
*hlock
;
1010 * Depth must not be zero for a non-head lock:
1015 * At least two relevant locks must exist for this
1018 if (curr
->held_locks
[depth
].irq_context
!=
1019 curr
->held_locks
[depth
-1].irq_context
)
1023 hlock
= curr
->held_locks
+ depth
-1;
1025 * Only non-recursive-read entries get new dependencies
1028 if (hlock
->read
!= 2) {
1029 check_prev_add(curr
, hlock
, next
);
1031 * Stop after the first non-trylock entry,
1032 * as non-trylock entries have added their
1033 * own direct dependencies already, so this
1034 * lock is connected to them indirectly:
1036 if (!hlock
->trylock
)
1041 * End of lock-stack?
1046 * Stop the search if we cross into another context:
1048 if (curr
->held_locks
[depth
].irq_context
!=
1049 curr
->held_locks
[depth
-1].irq_context
)
1054 __raw_spin_unlock(&hash_lock
);
1055 DEBUG_LOCKS_WARN_ON(1);
1062 * Is this the address of a static object:
1064 static int static_obj(void *obj
)
1066 unsigned long start
= (unsigned long) &_stext
,
1067 end
= (unsigned long) &_end
,
1068 addr
= (unsigned long) obj
;
1076 if ((addr
>= start
) && (addr
< end
))
1083 for_each_possible_cpu(i
) {
1084 start
= (unsigned long) &__per_cpu_start
+ per_cpu_offset(i
);
1085 end
= (unsigned long) &__per_cpu_end
+ 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
);
1120 extern void __error_too_big_MAX_LOCKDEP_SUBCLASSES(void);
1123 * Register a lock's class in the hash-table, if the class is not present
1124 * yet. Otherwise we look it up. We cache the result in the lock object
1125 * itself, so actual lookup of the hash should be once per lock object.
1127 static inline struct lock_class
*
1128 register_lock_class(struct lockdep_map
*lock
, unsigned int subclass
)
1130 struct lockdep_subclass_key
*key
;
1131 struct list_head
*hash_head
;
1132 struct lock_class
*class;
1134 #ifdef CONFIG_DEBUG_LOCKDEP
1136 * If the architecture calls into lockdep before initializing
1137 * the hashes then we'll warn about it later. (we cannot printk
1140 if (unlikely(!lockdep_initialized
)) {
1142 lockdep_init_error
= 1;
1147 * Static locks do not have their class-keys yet - for them the key
1148 * is the lock object itself:
1150 if (unlikely(!lock
->key
))
1151 lock
->key
= (void *)lock
;
1154 * NOTE: the class-key must be unique. For dynamic locks, a static
1155 * lock_class_key variable is passed in through the mutex_init()
1156 * (or spin_lock_init()) call - which acts as the key. For static
1157 * locks we use the lock object itself as the key.
1159 if (sizeof(struct lock_class_key
) > sizeof(struct lock_class
))
1160 __error_too_big_MAX_LOCKDEP_SUBCLASSES();
1162 key
= lock
->key
->subkeys
+ subclass
;
1164 hash_head
= classhashentry(key
);
1167 * We can walk the hash lockfree, because the hash only
1168 * grows, and we are careful when adding entries to the end:
1170 list_for_each_entry(class, hash_head
, hash_entry
)
1171 if (class->key
== key
)
1175 * Debug-check: all keys must be persistent!
1177 if (!static_obj(lock
->key
)) {
1179 printk("INFO: trying to register non-static key.\n");
1180 printk("the code is fine but needs lockdep annotation.\n");
1181 printk("turning off the locking correctness validator.\n");
1187 __raw_spin_lock(&hash_lock
);
1189 * We have to do the hash-walk again, to avoid races
1192 list_for_each_entry(class, hash_head
, hash_entry
)
1193 if (class->key
== key
)
1194 goto out_unlock_set
;
1196 * Allocate a new key from the static array, and add it to
1199 if (nr_lock_classes
>= MAX_LOCKDEP_KEYS
) {
1200 __raw_spin_unlock(&hash_lock
);
1202 printk("BUG: MAX_LOCKDEP_KEYS too low!\n");
1203 printk("turning off the locking correctness validator.\n");
1206 class = lock_classes
+ nr_lock_classes
++;
1207 debug_atomic_inc(&nr_unused_locks
);
1209 class->name
= lock
->name
;
1210 class->subclass
= subclass
;
1211 INIT_LIST_HEAD(&class->lock_entry
);
1212 INIT_LIST_HEAD(&class->locks_before
);
1213 INIT_LIST_HEAD(&class->locks_after
);
1214 class->name_version
= count_matching_names(class);
1216 * We use RCU's safe list-add method to make
1217 * parallel walking of the hash-list safe:
1219 list_add_tail_rcu(&class->hash_entry
, hash_head
);
1221 if (verbose(class)) {
1222 __raw_spin_unlock(&hash_lock
);
1223 printk("\nnew class %p: %s", class->key
, class->name
);
1224 if (class->name_version
> 1)
1225 printk("#%d", class->name_version
);
1228 __raw_spin_lock(&hash_lock
);
1231 __raw_spin_unlock(&hash_lock
);
1234 lock
->class[subclass
] = class;
1236 DEBUG_LOCKS_WARN_ON(class->subclass
!= subclass
);
1242 * Look up a dependency chain. If the key is not present yet then
1243 * add it and return 0 - in this case the new dependency chain is
1244 * validated. If the key is already hashed, return 1.
1246 static inline int lookup_chain_cache(u64 chain_key
)
1248 struct list_head
*hash_head
= chainhashentry(chain_key
);
1249 struct lock_chain
*chain
;
1251 DEBUG_LOCKS_WARN_ON(!irqs_disabled());
1253 * We can walk it lock-free, because entries only get added
1256 list_for_each_entry(chain
, hash_head
, entry
) {
1257 if (chain
->chain_key
== chain_key
) {
1259 debug_atomic_inc(&chain_lookup_hits
);
1261 * In the debugging case, force redundant checking
1264 #ifdef CONFIG_DEBUG_LOCKDEP
1265 __raw_spin_lock(&hash_lock
);
1272 * Allocate a new chain entry from the static array, and add
1275 __raw_spin_lock(&hash_lock
);
1277 * We have to walk the chain again locked - to avoid duplicates:
1279 list_for_each_entry(chain
, hash_head
, entry
) {
1280 if (chain
->chain_key
== chain_key
) {
1281 __raw_spin_unlock(&hash_lock
);
1285 if (unlikely(nr_lock_chains
>= MAX_LOCKDEP_CHAINS
)) {
1286 __raw_spin_unlock(&hash_lock
);
1288 printk("BUG: MAX_LOCKDEP_CHAINS too low!\n");
1289 printk("turning off the locking correctness validator.\n");
1292 chain
= lock_chains
+ nr_lock_chains
++;
1293 chain
->chain_key
= chain_key
;
1294 list_add_tail_rcu(&chain
->entry
, hash_head
);
1295 debug_atomic_inc(&chain_lookup_misses
);
1296 #ifdef CONFIG_TRACE_IRQFLAGS
1297 if (current
->hardirq_context
)
1298 nr_hardirq_chains
++;
1300 if (current
->softirq_context
)
1301 nr_softirq_chains
++;
1303 nr_process_chains
++;
1306 nr_process_chains
++;
1313 * We are building curr_chain_key incrementally, so double-check
1314 * it from scratch, to make sure that it's done correctly:
1316 static void check_chain_key(struct task_struct
*curr
)
1318 #ifdef CONFIG_DEBUG_LOCKDEP
1319 struct held_lock
*hlock
, *prev_hlock
= NULL
;
1323 for (i
= 0; i
< curr
->lockdep_depth
; i
++) {
1324 hlock
= curr
->held_locks
+ i
;
1325 if (chain_key
!= hlock
->prev_chain_key
) {
1327 printk("hm#1, depth: %u [%u], %016Lx != %016Lx\n",
1328 curr
->lockdep_depth
, i
,
1329 (unsigned long long)chain_key
,
1330 (unsigned long long)hlock
->prev_chain_key
);
1334 id
= hlock
->class - lock_classes
;
1335 DEBUG_LOCKS_WARN_ON(id
>= MAX_LOCKDEP_KEYS
);
1336 if (prev_hlock
&& (prev_hlock
->irq_context
!=
1337 hlock
->irq_context
))
1339 chain_key
= iterate_chain_key(chain_key
, id
);
1342 if (chain_key
!= curr
->curr_chain_key
) {
1344 printk("hm#2, depth: %u [%u], %016Lx != %016Lx\n",
1345 curr
->lockdep_depth
, i
,
1346 (unsigned long long)chain_key
,
1347 (unsigned long long)curr
->curr_chain_key
);
1353 #ifdef CONFIG_TRACE_IRQFLAGS
1356 * print irq inversion bug:
1359 print_irq_inversion_bug(struct task_struct
*curr
, struct lock_class
*other
,
1360 struct held_lock
*this, int forwards
,
1361 const char *irqclass
)
1363 __raw_spin_unlock(&hash_lock
);
1365 if (debug_locks_silent
)
1368 printk("\n=========================================================\n");
1369 printk( "[ INFO: possible irq lock inversion dependency detected ]\n");
1370 printk( "---------------------------------------------------------\n");
1371 printk("%s/%d just changed the state of lock:\n",
1372 curr
->comm
, curr
->pid
);
1375 printk("but this lock took another, %s-irq-unsafe lock in the past:\n", irqclass
);
1377 printk("but this lock was taken by another, %s-irq-safe lock in the past:\n", irqclass
);
1378 print_lock_name(other
);
1379 printk("\n\nand interrupts could create inverse lock ordering between them.\n\n");
1381 printk("\nother info that might help us debug this:\n");
1382 lockdep_print_held_locks(curr
);
1384 printk("\nthe first lock's dependencies:\n");
1385 print_lock_dependencies(this->class, 0);
1387 printk("\nthe second lock's dependencies:\n");
1388 print_lock_dependencies(other
, 0);
1390 printk("\nstack backtrace:\n");
1397 * Prove that in the forwards-direction subgraph starting at <this>
1398 * there is no lock matching <mask>:
1401 check_usage_forwards(struct task_struct
*curr
, struct held_lock
*this,
1402 enum lock_usage_bit bit
, const char *irqclass
)
1406 find_usage_bit
= bit
;
1407 /* fills in <forwards_match> */
1408 ret
= find_usage_forwards(this->class, 0);
1409 if (!ret
|| ret
== 1)
1412 return print_irq_inversion_bug(curr
, forwards_match
, this, 1, irqclass
);
1416 * Prove that in the backwards-direction subgraph starting at <this>
1417 * there is no lock matching <mask>:
1420 check_usage_backwards(struct task_struct
*curr
, struct held_lock
*this,
1421 enum lock_usage_bit bit
, const char *irqclass
)
1425 find_usage_bit
= bit
;
1426 /* fills in <backwards_match> */
1427 ret
= find_usage_backwards(this->class, 0);
1428 if (!ret
|| ret
== 1)
1431 return print_irq_inversion_bug(curr
, backwards_match
, this, 0, irqclass
);
1434 static inline void print_irqtrace_events(struct task_struct
*curr
)
1436 printk("irq event stamp: %u\n", curr
->irq_events
);
1437 printk("hardirqs last enabled at (%u): ", curr
->hardirq_enable_event
);
1438 print_ip_sym(curr
->hardirq_enable_ip
);
1439 printk("hardirqs last disabled at (%u): ", curr
->hardirq_disable_event
);
1440 print_ip_sym(curr
->hardirq_disable_ip
);
1441 printk("softirqs last enabled at (%u): ", curr
->softirq_enable_event
);
1442 print_ip_sym(curr
->softirq_enable_ip
);
1443 printk("softirqs last disabled at (%u): ", curr
->softirq_disable_event
);
1444 print_ip_sym(curr
->softirq_disable_ip
);
1448 static inline void print_irqtrace_events(struct task_struct
*curr
)
1454 print_usage_bug(struct task_struct
*curr
, struct held_lock
*this,
1455 enum lock_usage_bit prev_bit
, enum lock_usage_bit new_bit
)
1457 __raw_spin_unlock(&hash_lock
);
1459 if (debug_locks_silent
)
1462 printk("\n=================================\n");
1463 printk( "[ INFO: inconsistent lock state ]\n");
1464 printk( "---------------------------------\n");
1466 printk("inconsistent {%s} -> {%s} usage.\n",
1467 usage_str
[prev_bit
], usage_str
[new_bit
]);
1469 printk("%s/%d [HC%u[%lu]:SC%u[%lu]:HE%u:SE%u] takes:\n",
1470 curr
->comm
, curr
->pid
,
1471 trace_hardirq_context(curr
), hardirq_count() >> HARDIRQ_SHIFT
,
1472 trace_softirq_context(curr
), softirq_count() >> SOFTIRQ_SHIFT
,
1473 trace_hardirqs_enabled(curr
),
1474 trace_softirqs_enabled(curr
));
1477 printk("{%s} state was registered at:\n", usage_str
[prev_bit
]);
1478 print_stack_trace(this->class->usage_traces
+ prev_bit
, 1);
1480 print_irqtrace_events(curr
);
1481 printk("\nother info that might help us debug this:\n");
1482 lockdep_print_held_locks(curr
);
1484 printk("\nstack backtrace:\n");
1491 * Print out an error if an invalid bit is set:
1494 valid_state(struct task_struct
*curr
, struct held_lock
*this,
1495 enum lock_usage_bit new_bit
, enum lock_usage_bit bad_bit
)
1497 if (unlikely(this->class->usage_mask
& (1 << bad_bit
)))
1498 return print_usage_bug(curr
, this, bad_bit
, new_bit
);
1502 #define STRICT_READ_CHECKS 1
1505 * Mark a lock with a usage bit, and validate the state transition:
1507 static int mark_lock(struct task_struct
*curr
, struct held_lock
*this,
1508 enum lock_usage_bit new_bit
, unsigned long ip
)
1510 unsigned int new_mask
= 1 << new_bit
, ret
= 1;
1513 * If already set then do not dirty the cacheline,
1514 * nor do any checks:
1516 if (likely(this->class->usage_mask
& new_mask
))
1519 __raw_spin_lock(&hash_lock
);
1521 * Make sure we didnt race:
1523 if (unlikely(this->class->usage_mask
& new_mask
)) {
1524 __raw_spin_unlock(&hash_lock
);
1528 this->class->usage_mask
|= new_mask
;
1530 #ifdef CONFIG_TRACE_IRQFLAGS
1531 if (new_bit
== LOCK_ENABLED_HARDIRQS
||
1532 new_bit
== LOCK_ENABLED_HARDIRQS_READ
)
1533 ip
= curr
->hardirq_enable_ip
;
1534 else if (new_bit
== LOCK_ENABLED_SOFTIRQS
||
1535 new_bit
== LOCK_ENABLED_SOFTIRQS_READ
)
1536 ip
= curr
->softirq_enable_ip
;
1538 if (!save_trace(this->class->usage_traces
+ new_bit
))
1542 #ifdef CONFIG_TRACE_IRQFLAGS
1543 case LOCK_USED_IN_HARDIRQ
:
1544 if (!valid_state(curr
, this, new_bit
, LOCK_ENABLED_HARDIRQS
))
1546 if (!valid_state(curr
, this, new_bit
,
1547 LOCK_ENABLED_HARDIRQS_READ
))
1550 * just marked it hardirq-safe, check that this lock
1551 * took no hardirq-unsafe lock in the past:
1553 if (!check_usage_forwards(curr
, this,
1554 LOCK_ENABLED_HARDIRQS
, "hard"))
1556 #if STRICT_READ_CHECKS
1558 * just marked it hardirq-safe, check that this lock
1559 * took no hardirq-unsafe-read lock in the past:
1561 if (!check_usage_forwards(curr
, this,
1562 LOCK_ENABLED_HARDIRQS_READ
, "hard-read"))
1565 if (hardirq_verbose(this->class))
1568 case LOCK_USED_IN_SOFTIRQ
:
1569 if (!valid_state(curr
, this, new_bit
, LOCK_ENABLED_SOFTIRQS
))
1571 if (!valid_state(curr
, this, new_bit
,
1572 LOCK_ENABLED_SOFTIRQS_READ
))
1575 * just marked it softirq-safe, check that this lock
1576 * took no softirq-unsafe lock in the past:
1578 if (!check_usage_forwards(curr
, this,
1579 LOCK_ENABLED_SOFTIRQS
, "soft"))
1581 #if STRICT_READ_CHECKS
1583 * just marked it softirq-safe, check that this lock
1584 * took no softirq-unsafe-read lock in the past:
1586 if (!check_usage_forwards(curr
, this,
1587 LOCK_ENABLED_SOFTIRQS_READ
, "soft-read"))
1590 if (softirq_verbose(this->class))
1593 case LOCK_USED_IN_HARDIRQ_READ
:
1594 if (!valid_state(curr
, this, new_bit
, LOCK_ENABLED_HARDIRQS
))
1597 * just marked it hardirq-read-safe, check that this lock
1598 * took no hardirq-unsafe lock in the past:
1600 if (!check_usage_forwards(curr
, this,
1601 LOCK_ENABLED_HARDIRQS
, "hard"))
1603 if (hardirq_verbose(this->class))
1606 case LOCK_USED_IN_SOFTIRQ_READ
:
1607 if (!valid_state(curr
, this, new_bit
, LOCK_ENABLED_SOFTIRQS
))
1610 * just marked it softirq-read-safe, check that this lock
1611 * took no softirq-unsafe lock in the past:
1613 if (!check_usage_forwards(curr
, this,
1614 LOCK_ENABLED_SOFTIRQS
, "soft"))
1616 if (softirq_verbose(this->class))
1619 case LOCK_ENABLED_HARDIRQS
:
1620 if (!valid_state(curr
, this, new_bit
, LOCK_USED_IN_HARDIRQ
))
1622 if (!valid_state(curr
, this, new_bit
,
1623 LOCK_USED_IN_HARDIRQ_READ
))
1626 * just marked it hardirq-unsafe, check that no hardirq-safe
1627 * lock in the system ever took it in the past:
1629 if (!check_usage_backwards(curr
, this,
1630 LOCK_USED_IN_HARDIRQ
, "hard"))
1632 #if STRICT_READ_CHECKS
1634 * just marked it hardirq-unsafe, check that no
1635 * hardirq-safe-read lock in the system ever took
1638 if (!check_usage_backwards(curr
, this,
1639 LOCK_USED_IN_HARDIRQ_READ
, "hard-read"))
1642 if (hardirq_verbose(this->class))
1645 case LOCK_ENABLED_SOFTIRQS
:
1646 if (!valid_state(curr
, this, new_bit
, LOCK_USED_IN_SOFTIRQ
))
1648 if (!valid_state(curr
, this, new_bit
,
1649 LOCK_USED_IN_SOFTIRQ_READ
))
1652 * just marked it softirq-unsafe, check that no softirq-safe
1653 * lock in the system ever took it in the past:
1655 if (!check_usage_backwards(curr
, this,
1656 LOCK_USED_IN_SOFTIRQ
, "soft"))
1658 #if STRICT_READ_CHECKS
1660 * just marked it softirq-unsafe, check that no
1661 * softirq-safe-read lock in the system ever took
1664 if (!check_usage_backwards(curr
, this,
1665 LOCK_USED_IN_SOFTIRQ_READ
, "soft-read"))
1668 if (softirq_verbose(this->class))
1671 case LOCK_ENABLED_HARDIRQS_READ
:
1672 if (!valid_state(curr
, this, new_bit
, LOCK_USED_IN_HARDIRQ
))
1674 #if STRICT_READ_CHECKS
1676 * just marked it hardirq-read-unsafe, check that no
1677 * hardirq-safe lock in the system ever took it in the past:
1679 if (!check_usage_backwards(curr
, this,
1680 LOCK_USED_IN_HARDIRQ
, "hard"))
1683 if (hardirq_verbose(this->class))
1686 case LOCK_ENABLED_SOFTIRQS_READ
:
1687 if (!valid_state(curr
, this, new_bit
, LOCK_USED_IN_SOFTIRQ
))
1689 #if STRICT_READ_CHECKS
1691 * just marked it softirq-read-unsafe, check that no
1692 * softirq-safe lock in the system ever took it in the past:
1694 if (!check_usage_backwards(curr
, this,
1695 LOCK_USED_IN_SOFTIRQ
, "soft"))
1698 if (softirq_verbose(this->class))
1704 * Add it to the global list of classes:
1706 list_add_tail_rcu(&this->class->lock_entry
, &all_lock_classes
);
1707 debug_atomic_dec(&nr_unused_locks
);
1715 __raw_spin_unlock(&hash_lock
);
1718 * We must printk outside of the hash_lock:
1721 printk("\nmarked lock as {%s}:\n", usage_str
[new_bit
]);
1723 print_irqtrace_events(curr
);
1730 #ifdef CONFIG_TRACE_IRQFLAGS
1732 * Mark all held locks with a usage bit:
1735 mark_held_locks(struct task_struct
*curr
, int hardirq
, unsigned long ip
)
1737 enum lock_usage_bit usage_bit
;
1738 struct held_lock
*hlock
;
1741 for (i
= 0; i
< curr
->lockdep_depth
; i
++) {
1742 hlock
= curr
->held_locks
+ i
;
1746 usage_bit
= LOCK_ENABLED_HARDIRQS_READ
;
1748 usage_bit
= LOCK_ENABLED_HARDIRQS
;
1751 usage_bit
= LOCK_ENABLED_SOFTIRQS_READ
;
1753 usage_bit
= LOCK_ENABLED_SOFTIRQS
;
1755 if (!mark_lock(curr
, hlock
, usage_bit
, ip
))
1763 * Debugging helper: via this flag we know that we are in
1764 * 'early bootup code', and will warn about any invalid irqs-on event:
1766 static int early_boot_irqs_enabled
;
1768 void early_boot_irqs_off(void)
1770 early_boot_irqs_enabled
= 0;
1773 void early_boot_irqs_on(void)
1775 early_boot_irqs_enabled
= 1;
1779 * Hardirqs will be enabled:
1781 void trace_hardirqs_on(void)
1783 struct task_struct
*curr
= current
;
1786 if (unlikely(!debug_locks
|| current
->lockdep_recursion
))
1789 if (DEBUG_LOCKS_WARN_ON(unlikely(!early_boot_irqs_enabled
)))
1792 if (unlikely(curr
->hardirqs_enabled
)) {
1793 debug_atomic_inc(&redundant_hardirqs_on
);
1796 /* we'll do an OFF -> ON transition: */
1797 curr
->hardirqs_enabled
= 1;
1798 ip
= (unsigned long) __builtin_return_address(0);
1800 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
1802 if (DEBUG_LOCKS_WARN_ON(current
->hardirq_context
))
1805 * We are going to turn hardirqs on, so set the
1806 * usage bit for all held locks:
1808 if (!mark_held_locks(curr
, 1, ip
))
1811 * If we have softirqs enabled, then set the usage
1812 * bit for all held locks. (disabled hardirqs prevented
1813 * this bit from being set before)
1815 if (curr
->softirqs_enabled
)
1816 if (!mark_held_locks(curr
, 0, ip
))
1819 curr
->hardirq_enable_ip
= ip
;
1820 curr
->hardirq_enable_event
= ++curr
->irq_events
;
1821 debug_atomic_inc(&hardirqs_on_events
);
1824 EXPORT_SYMBOL(trace_hardirqs_on
);
1827 * Hardirqs were disabled:
1829 void trace_hardirqs_off(void)
1831 struct task_struct
*curr
= current
;
1833 if (unlikely(!debug_locks
|| current
->lockdep_recursion
))
1836 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
1839 if (curr
->hardirqs_enabled
) {
1841 * We have done an ON -> OFF transition:
1843 curr
->hardirqs_enabled
= 0;
1844 curr
->hardirq_disable_ip
= _RET_IP_
;
1845 curr
->hardirq_disable_event
= ++curr
->irq_events
;
1846 debug_atomic_inc(&hardirqs_off_events
);
1848 debug_atomic_inc(&redundant_hardirqs_off
);
1851 EXPORT_SYMBOL(trace_hardirqs_off
);
1854 * Softirqs will be enabled:
1856 void trace_softirqs_on(unsigned long ip
)
1858 struct task_struct
*curr
= current
;
1860 if (unlikely(!debug_locks
))
1863 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
1866 if (curr
->softirqs_enabled
) {
1867 debug_atomic_inc(&redundant_softirqs_on
);
1872 * We'll do an OFF -> ON transition:
1874 curr
->softirqs_enabled
= 1;
1875 curr
->softirq_enable_ip
= ip
;
1876 curr
->softirq_enable_event
= ++curr
->irq_events
;
1877 debug_atomic_inc(&softirqs_on_events
);
1879 * We are going to turn softirqs on, so set the
1880 * usage bit for all held locks, if hardirqs are
1883 if (curr
->hardirqs_enabled
)
1884 mark_held_locks(curr
, 0, ip
);
1888 * Softirqs were disabled:
1890 void trace_softirqs_off(unsigned long ip
)
1892 struct task_struct
*curr
= current
;
1894 if (unlikely(!debug_locks
))
1897 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
1900 if (curr
->softirqs_enabled
) {
1902 * We have done an ON -> OFF transition:
1904 curr
->softirqs_enabled
= 0;
1905 curr
->softirq_disable_ip
= ip
;
1906 curr
->softirq_disable_event
= ++curr
->irq_events
;
1907 debug_atomic_inc(&softirqs_off_events
);
1908 DEBUG_LOCKS_WARN_ON(!softirq_count());
1910 debug_atomic_inc(&redundant_softirqs_off
);
1916 * Initialize a lock instance's lock-class mapping info:
1918 void lockdep_init_map(struct lockdep_map
*lock
, const char *name
,
1919 struct lock_class_key
*key
)
1921 if (unlikely(!debug_locks
))
1924 if (DEBUG_LOCKS_WARN_ON(!key
))
1926 if (DEBUG_LOCKS_WARN_ON(!name
))
1929 * Sanity check, the lock-class key must be persistent:
1931 if (!static_obj(key
)) {
1932 printk("BUG: key %p not in .data!\n", key
);
1933 DEBUG_LOCKS_WARN_ON(1);
1938 memset(lock
->class, 0, sizeof(lock
->class[0])*MAX_LOCKDEP_SUBCLASSES
);
1941 EXPORT_SYMBOL_GPL(lockdep_init_map
);
1944 * This gets called for every mutex_lock*()/spin_lock*() operation.
1945 * We maintain the dependency maps and validate the locking attempt:
1947 static int __lock_acquire(struct lockdep_map
*lock
, unsigned int subclass
,
1948 int trylock
, int read
, int check
, int hardirqs_off
,
1951 struct task_struct
*curr
= current
;
1952 struct held_lock
*hlock
;
1953 struct lock_class
*class;
1954 unsigned int depth
, id
;
1958 if (unlikely(!debug_locks
))
1961 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
1964 if (unlikely(subclass
>= MAX_LOCKDEP_SUBCLASSES
)) {
1966 printk("BUG: MAX_LOCKDEP_SUBCLASSES too low!\n");
1967 printk("turning off the locking correctness validator.\n");
1971 class = lock
->class[subclass
];
1972 /* not cached yet? */
1973 if (unlikely(!class)) {
1974 class = register_lock_class(lock
, subclass
);
1978 debug_atomic_inc((atomic_t
*)&class->ops
);
1979 if (very_verbose(class)) {
1980 printk("\nacquire class [%p] %s", class->key
, class->name
);
1981 if (class->name_version
> 1)
1982 printk("#%d", class->name_version
);
1988 * Add the lock to the list of currently held locks.
1989 * (we dont increase the depth just yet, up until the
1990 * dependency checks are done)
1992 depth
= curr
->lockdep_depth
;
1993 if (DEBUG_LOCKS_WARN_ON(depth
>= MAX_LOCK_DEPTH
))
1996 hlock
= curr
->held_locks
+ depth
;
1998 hlock
->class = class;
1999 hlock
->acquire_ip
= ip
;
2000 hlock
->instance
= lock
;
2001 hlock
->trylock
= trylock
;
2003 hlock
->check
= check
;
2004 hlock
->hardirqs_off
= hardirqs_off
;
2008 #ifdef CONFIG_TRACE_IRQFLAGS
2010 * If non-trylock use in a hardirq or softirq context, then
2011 * mark the lock as used in these contexts:
2015 if (curr
->hardirq_context
)
2016 if (!mark_lock(curr
, hlock
,
2017 LOCK_USED_IN_HARDIRQ_READ
, ip
))
2019 if (curr
->softirq_context
)
2020 if (!mark_lock(curr
, hlock
,
2021 LOCK_USED_IN_SOFTIRQ_READ
, ip
))
2024 if (curr
->hardirq_context
)
2025 if (!mark_lock(curr
, hlock
, LOCK_USED_IN_HARDIRQ
, ip
))
2027 if (curr
->softirq_context
)
2028 if (!mark_lock(curr
, hlock
, LOCK_USED_IN_SOFTIRQ
, ip
))
2032 if (!hardirqs_off
) {
2034 if (!mark_lock(curr
, hlock
,
2035 LOCK_ENABLED_HARDIRQS_READ
, ip
))
2037 if (curr
->softirqs_enabled
)
2038 if (!mark_lock(curr
, hlock
,
2039 LOCK_ENABLED_SOFTIRQS_READ
, ip
))
2042 if (!mark_lock(curr
, hlock
,
2043 LOCK_ENABLED_HARDIRQS
, ip
))
2045 if (curr
->softirqs_enabled
)
2046 if (!mark_lock(curr
, hlock
,
2047 LOCK_ENABLED_SOFTIRQS
, ip
))
2052 /* mark it as used: */
2053 if (!mark_lock(curr
, hlock
, LOCK_USED
, ip
))
2057 * Calculate the chain hash: it's the combined has of all the
2058 * lock keys along the dependency chain. We save the hash value
2059 * at every step so that we can get the current hash easily
2060 * after unlock. The chain hash is then used to cache dependency
2063 * The 'key ID' is what is the most compact key value to drive
2064 * the hash, not class->key.
2066 id
= class - lock_classes
;
2067 if (DEBUG_LOCKS_WARN_ON(id
>= MAX_LOCKDEP_KEYS
))
2070 chain_key
= curr
->curr_chain_key
;
2072 if (DEBUG_LOCKS_WARN_ON(chain_key
!= 0))
2077 hlock
->prev_chain_key
= chain_key
;
2079 #ifdef CONFIG_TRACE_IRQFLAGS
2081 * Keep track of points where we cross into an interrupt context:
2083 hlock
->irq_context
= 2*(curr
->hardirq_context
? 1 : 0) +
2084 curr
->softirq_context
;
2086 struct held_lock
*prev_hlock
;
2088 prev_hlock
= curr
->held_locks
+ depth
-1;
2090 * If we cross into another context, reset the
2091 * hash key (this also prevents the checking and the
2092 * adding of the dependency to 'prev'):
2094 if (prev_hlock
->irq_context
!= hlock
->irq_context
) {
2100 chain_key
= iterate_chain_key(chain_key
, id
);
2101 curr
->curr_chain_key
= chain_key
;
2104 * Trylock needs to maintain the stack of held locks, but it
2105 * does not add new dependencies, because trylock can be done
2108 * We look up the chain_key and do the O(N^2) check and update of
2109 * the dependencies only if this is a new dependency chain.
2110 * (If lookup_chain_cache() returns with 1 it acquires
2113 if (!trylock
&& (check
== 2) && lookup_chain_cache(chain_key
)) {
2115 * Check whether last held lock:
2117 * - is irq-safe, if this lock is irq-unsafe
2118 * - is softirq-safe, if this lock is hardirq-unsafe
2120 * And check whether the new lock's dependency graph
2121 * could lead back to the previous lock.
2123 * any of these scenarios could lead to a deadlock. If
2126 int ret
= check_deadlock(curr
, hlock
, lock
, read
);
2131 * Mark recursive read, as we jump over it when
2132 * building dependencies (just like we jump over
2138 * Add dependency only if this lock is not the head
2139 * of the chain, and if it's not a secondary read-lock:
2141 if (!chain_head
&& ret
!= 2)
2142 if (!check_prevs_add(curr
, hlock
))
2144 __raw_spin_unlock(&hash_lock
);
2146 curr
->lockdep_depth
++;
2147 check_chain_key(curr
);
2148 if (unlikely(curr
->lockdep_depth
>= MAX_LOCK_DEPTH
)) {
2150 printk("BUG: MAX_LOCK_DEPTH too low!\n");
2151 printk("turning off the locking correctness validator.\n");
2154 if (unlikely(curr
->lockdep_depth
> max_lockdep_depth
))
2155 max_lockdep_depth
= curr
->lockdep_depth
;
2161 print_unlock_inbalance_bug(struct task_struct
*curr
, struct lockdep_map
*lock
,
2164 if (!debug_locks_off())
2166 if (debug_locks_silent
)
2169 printk("\n=====================================\n");
2170 printk( "[ BUG: bad unlock balance detected! ]\n");
2171 printk( "-------------------------------------\n");
2172 printk("%s/%d is trying to release lock (",
2173 curr
->comm
, curr
->pid
);
2174 print_lockdep_cache(lock
);
2177 printk("but there are no more locks to release!\n");
2178 printk("\nother info that might help us debug this:\n");
2179 lockdep_print_held_locks(curr
);
2181 printk("\nstack backtrace:\n");
2188 * Common debugging checks for both nested and non-nested unlock:
2190 static int check_unlock(struct task_struct
*curr
, struct lockdep_map
*lock
,
2193 if (unlikely(!debug_locks
))
2195 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2198 if (curr
->lockdep_depth
<= 0)
2199 return print_unlock_inbalance_bug(curr
, lock
, ip
);
2205 * Remove the lock to the list of currently held locks in a
2206 * potentially non-nested (out of order) manner. This is a
2207 * relatively rare operation, as all the unlock APIs default
2208 * to nested mode (which uses lock_release()):
2211 lock_release_non_nested(struct task_struct
*curr
,
2212 struct lockdep_map
*lock
, unsigned long ip
)
2214 struct held_lock
*hlock
, *prev_hlock
;
2219 * Check whether the lock exists in the current stack
2222 depth
= curr
->lockdep_depth
;
2223 if (DEBUG_LOCKS_WARN_ON(!depth
))
2227 for (i
= depth
-1; i
>= 0; i
--) {
2228 hlock
= curr
->held_locks
+ i
;
2230 * We must not cross into another context:
2232 if (prev_hlock
&& prev_hlock
->irq_context
!= hlock
->irq_context
)
2234 if (hlock
->instance
== lock
)
2238 return print_unlock_inbalance_bug(curr
, lock
, ip
);
2242 * We have the right lock to unlock, 'hlock' points to it.
2243 * Now we remove it from the stack, and add back the other
2244 * entries (if any), recalculating the hash along the way:
2246 curr
->lockdep_depth
= i
;
2247 curr
->curr_chain_key
= hlock
->prev_chain_key
;
2249 for (i
++; i
< depth
; i
++) {
2250 hlock
= curr
->held_locks
+ i
;
2251 if (!__lock_acquire(hlock
->instance
,
2252 hlock
->class->subclass
, hlock
->trylock
,
2253 hlock
->read
, hlock
->check
, hlock
->hardirqs_off
,
2258 if (DEBUG_LOCKS_WARN_ON(curr
->lockdep_depth
!= depth
- 1))
2264 * Remove the lock to the list of currently held locks - this gets
2265 * called on mutex_unlock()/spin_unlock*() (or on a failed
2266 * mutex_lock_interruptible()). This is done for unlocks that nest
2267 * perfectly. (i.e. the current top of the lock-stack is unlocked)
2269 static int lock_release_nested(struct task_struct
*curr
,
2270 struct lockdep_map
*lock
, unsigned long ip
)
2272 struct held_lock
*hlock
;
2276 * Pop off the top of the lock stack:
2278 depth
= curr
->lockdep_depth
- 1;
2279 hlock
= curr
->held_locks
+ depth
;
2282 * Is the unlock non-nested:
2284 if (hlock
->instance
!= lock
)
2285 return lock_release_non_nested(curr
, lock
, ip
);
2286 curr
->lockdep_depth
--;
2288 if (DEBUG_LOCKS_WARN_ON(!depth
&& (hlock
->prev_chain_key
!= 0)))
2291 curr
->curr_chain_key
= hlock
->prev_chain_key
;
2293 #ifdef CONFIG_DEBUG_LOCKDEP
2294 hlock
->prev_chain_key
= 0;
2295 hlock
->class = NULL
;
2296 hlock
->acquire_ip
= 0;
2297 hlock
->irq_context
= 0;
2303 * Remove the lock to the list of currently held locks - this gets
2304 * called on mutex_unlock()/spin_unlock*() (or on a failed
2305 * mutex_lock_interruptible()). This is done for unlocks that nest
2306 * perfectly. (i.e. the current top of the lock-stack is unlocked)
2309 __lock_release(struct lockdep_map
*lock
, int nested
, unsigned long ip
)
2311 struct task_struct
*curr
= current
;
2313 if (!check_unlock(curr
, lock
, ip
))
2317 if (!lock_release_nested(curr
, lock
, ip
))
2320 if (!lock_release_non_nested(curr
, lock
, ip
))
2324 check_chain_key(curr
);
2328 * Check whether we follow the irq-flags state precisely:
2330 static void check_flags(unsigned long flags
)
2332 #if defined(CONFIG_DEBUG_LOCKDEP) && defined(CONFIG_TRACE_IRQFLAGS)
2336 if (irqs_disabled_flags(flags
))
2337 DEBUG_LOCKS_WARN_ON(current
->hardirqs_enabled
);
2339 DEBUG_LOCKS_WARN_ON(!current
->hardirqs_enabled
);
2342 * We dont accurately track softirq state in e.g.
2343 * hardirq contexts (such as on 4KSTACKS), so only
2344 * check if not in hardirq contexts:
2346 if (!hardirq_count()) {
2347 if (softirq_count())
2348 DEBUG_LOCKS_WARN_ON(current
->softirqs_enabled
);
2350 DEBUG_LOCKS_WARN_ON(!current
->softirqs_enabled
);
2354 print_irqtrace_events(current
);
2359 * We are not always called with irqs disabled - do that here,
2360 * and also avoid lockdep recursion:
2362 void lock_acquire(struct lockdep_map
*lock
, unsigned int subclass
,
2363 int trylock
, int read
, int check
, unsigned long ip
)
2365 unsigned long flags
;
2367 if (unlikely(current
->lockdep_recursion
))
2370 raw_local_irq_save(flags
);
2373 current
->lockdep_recursion
= 1;
2374 __lock_acquire(lock
, subclass
, trylock
, read
, check
,
2375 irqs_disabled_flags(flags
), ip
);
2376 current
->lockdep_recursion
= 0;
2377 raw_local_irq_restore(flags
);
2380 EXPORT_SYMBOL_GPL(lock_acquire
);
2382 void lock_release(struct lockdep_map
*lock
, int nested
, unsigned long ip
)
2384 unsigned long flags
;
2386 if (unlikely(current
->lockdep_recursion
))
2389 raw_local_irq_save(flags
);
2391 current
->lockdep_recursion
= 1;
2392 __lock_release(lock
, nested
, ip
);
2393 current
->lockdep_recursion
= 0;
2394 raw_local_irq_restore(flags
);
2397 EXPORT_SYMBOL_GPL(lock_release
);
2400 * Used by the testsuite, sanitize the validator state
2401 * after a simulated failure:
2404 void lockdep_reset(void)
2406 unsigned long flags
;
2408 raw_local_irq_save(flags
);
2409 current
->curr_chain_key
= 0;
2410 current
->lockdep_depth
= 0;
2411 current
->lockdep_recursion
= 0;
2412 memset(current
->held_locks
, 0, MAX_LOCK_DEPTH
*sizeof(struct held_lock
));
2413 nr_hardirq_chains
= 0;
2414 nr_softirq_chains
= 0;
2415 nr_process_chains
= 0;
2417 raw_local_irq_restore(flags
);
2420 static void zap_class(struct lock_class
*class)
2425 * Remove all dependencies this lock is
2428 for (i
= 0; i
< nr_list_entries
; i
++) {
2429 if (list_entries
[i
].class == class)
2430 list_del_rcu(&list_entries
[i
].entry
);
2433 * Unhash the class and remove it from the all_lock_classes list:
2435 list_del_rcu(&class->hash_entry
);
2436 list_del_rcu(&class->lock_entry
);
2440 static inline int within(void *addr
, void *start
, unsigned long size
)
2442 return addr
>= start
&& addr
< start
+ size
;
2445 void lockdep_free_key_range(void *start
, unsigned long size
)
2447 struct lock_class
*class, *next
;
2448 struct list_head
*head
;
2449 unsigned long flags
;
2452 raw_local_irq_save(flags
);
2453 __raw_spin_lock(&hash_lock
);
2456 * Unhash all classes that were created by this module:
2458 for (i
= 0; i
< CLASSHASH_SIZE
; i
++) {
2459 head
= classhash_table
+ i
;
2460 if (list_empty(head
))
2462 list_for_each_entry_safe(class, next
, head
, hash_entry
)
2463 if (within(class->key
, start
, size
))
2467 __raw_spin_unlock(&hash_lock
);
2468 raw_local_irq_restore(flags
);
2471 void lockdep_reset_lock(struct lockdep_map
*lock
)
2473 struct lock_class
*class, *next
, *entry
;
2474 struct list_head
*head
;
2475 unsigned long flags
;
2478 raw_local_irq_save(flags
);
2479 __raw_spin_lock(&hash_lock
);
2482 * Remove all classes this lock has:
2484 for (i
= 0; i
< CLASSHASH_SIZE
; i
++) {
2485 head
= classhash_table
+ i
;
2486 if (list_empty(head
))
2488 list_for_each_entry_safe(class, next
, head
, hash_entry
) {
2489 for (j
= 0; j
< MAX_LOCKDEP_SUBCLASSES
; j
++) {
2490 entry
= lock
->class[j
];
2491 if (class == entry
) {
2493 lock
->class[j
] = NULL
;
2501 * Debug check: in the end all mapped classes should
2504 for (j
= 0; j
< MAX_LOCKDEP_SUBCLASSES
; j
++) {
2505 entry
= lock
->class[j
];
2508 __raw_spin_unlock(&hash_lock
);
2509 DEBUG_LOCKS_WARN_ON(1);
2510 raw_local_irq_restore(flags
);
2514 __raw_spin_unlock(&hash_lock
);
2515 raw_local_irq_restore(flags
);
2518 void __init
lockdep_init(void)
2523 * Some architectures have their own start_kernel()
2524 * code which calls lockdep_init(), while we also
2525 * call lockdep_init() from the start_kernel() itself,
2526 * and we want to initialize the hashes only once:
2528 if (lockdep_initialized
)
2531 for (i
= 0; i
< CLASSHASH_SIZE
; i
++)
2532 INIT_LIST_HEAD(classhash_table
+ i
);
2534 for (i
= 0; i
< CHAINHASH_SIZE
; i
++)
2535 INIT_LIST_HEAD(chainhash_table
+ i
);
2537 lockdep_initialized
= 1;
2540 void __init
lockdep_info(void)
2542 printk("Lock dependency validator: Copyright (c) 2006 Red Hat, Inc., Ingo Molnar\n");
2544 printk("... MAX_LOCKDEP_SUBCLASSES: %lu\n", MAX_LOCKDEP_SUBCLASSES
);
2545 printk("... MAX_LOCK_DEPTH: %lu\n", MAX_LOCK_DEPTH
);
2546 printk("... MAX_LOCKDEP_KEYS: %lu\n", MAX_LOCKDEP_KEYS
);
2547 printk("... CLASSHASH_SIZE: %lu\n", CLASSHASH_SIZE
);
2548 printk("... MAX_LOCKDEP_ENTRIES: %lu\n", MAX_LOCKDEP_ENTRIES
);
2549 printk("... MAX_LOCKDEP_CHAINS: %lu\n", MAX_LOCKDEP_CHAINS
);
2550 printk("... CHAINHASH_SIZE: %lu\n", CHAINHASH_SIZE
);
2552 printk(" memory used by lock dependency info: %lu kB\n",
2553 (sizeof(struct lock_class
) * MAX_LOCKDEP_KEYS
+
2554 sizeof(struct list_head
) * CLASSHASH_SIZE
+
2555 sizeof(struct lock_list
) * MAX_LOCKDEP_ENTRIES
+
2556 sizeof(struct lock_chain
) * MAX_LOCKDEP_CHAINS
+
2557 sizeof(struct list_head
) * CHAINHASH_SIZE
) / 1024);
2559 printk(" per task-struct memory footprint: %lu bytes\n",
2560 sizeof(struct held_lock
) * MAX_LOCK_DEPTH
);
2562 #ifdef CONFIG_DEBUG_LOCKDEP
2563 if (lockdep_init_error
)
2564 printk("WARNING: lockdep init error! Arch code didnt call lockdep_init() early enough?\n");
2568 static inline int in_range(const void *start
, const void *addr
, const void *end
)
2570 return addr
>= start
&& addr
<= end
;
2574 print_freed_lock_bug(struct task_struct
*curr
, const void *mem_from
,
2577 if (!debug_locks_off())
2579 if (debug_locks_silent
)
2582 printk("\n=========================\n");
2583 printk( "[ BUG: held lock freed! ]\n");
2584 printk( "-------------------------\n");
2585 printk("%s/%d is freeing memory %p-%p, with a lock still held there!\n",
2586 curr
->comm
, curr
->pid
, mem_from
, mem_to
-1);
2587 lockdep_print_held_locks(curr
);
2589 printk("\nstack backtrace:\n");
2594 * Called when kernel memory is freed (or unmapped), or if a lock
2595 * is destroyed or reinitialized - this code checks whether there is
2596 * any held lock in the memory range of <from> to <to>:
2598 void debug_check_no_locks_freed(const void *mem_from
, unsigned long mem_len
)
2600 const void *mem_to
= mem_from
+ mem_len
, *lock_from
, *lock_to
;
2601 struct task_struct
*curr
= current
;
2602 struct held_lock
*hlock
;
2603 unsigned long flags
;
2606 if (unlikely(!debug_locks
))
2609 local_irq_save(flags
);
2610 for (i
= 0; i
< curr
->lockdep_depth
; i
++) {
2611 hlock
= curr
->held_locks
+ i
;
2613 lock_from
= (void *)hlock
->instance
;
2614 lock_to
= (void *)(hlock
->instance
+ 1);
2616 if (!in_range(mem_from
, lock_from
, mem_to
) &&
2617 !in_range(mem_from
, lock_to
, mem_to
))
2620 print_freed_lock_bug(curr
, mem_from
, mem_to
);
2623 local_irq_restore(flags
);
2626 static void print_held_locks_bug(struct task_struct
*curr
)
2628 if (!debug_locks_off())
2630 if (debug_locks_silent
)
2633 printk("\n=====================================\n");
2634 printk( "[ BUG: lock held at task exit time! ]\n");
2635 printk( "-------------------------------------\n");
2636 printk("%s/%d is exiting with locks still held!\n",
2637 curr
->comm
, curr
->pid
);
2638 lockdep_print_held_locks(curr
);
2640 printk("\nstack backtrace:\n");
2644 void debug_check_no_locks_held(struct task_struct
*task
)
2646 if (unlikely(task
->lockdep_depth
> 0))
2647 print_held_locks_bug(task
);
2650 void debug_show_all_locks(void)
2652 struct task_struct
*g
, *p
;
2656 printk("\nShowing all locks held in the system:\n");
2659 * Here we try to get the tasklist_lock as hard as possible,
2660 * if not successful after 2 seconds we ignore it (but keep
2661 * trying). This is to enable a debug printout even if a
2662 * tasklist_lock-holding task deadlocks or crashes.
2665 if (!read_trylock(&tasklist_lock
)) {
2667 printk("hm, tasklist_lock locked, retrying... ");
2670 printk(" #%d", 10-count
);
2674 printk(" ignoring it.\n");
2678 printk(" locked it.\n");
2680 do_each_thread(g
, p
) {
2681 if (p
->lockdep_depth
)
2682 lockdep_print_held_locks(p
);
2684 if (read_trylock(&tasklist_lock
))
2686 } while_each_thread(g
, p
);
2689 printk("=============================================\n\n");
2692 read_unlock(&tasklist_lock
);
2695 EXPORT_SYMBOL_GPL(debug_show_all_locks
);
2697 void debug_show_held_locks(struct task_struct
*task
)
2699 lockdep_print_held_locks(task
);
2702 EXPORT_SYMBOL_GPL(debug_show_held_locks
);