4 * Runtime locking correctness validator
6 * Started by Ingo Molnar:
8 * Copyright (C) 2006 Red Hat, Inc., Ingo Molnar <mingo@redhat.com>
10 * this code maps all the lock dependencies as they occur in a live kernel
11 * and will warn about the following classes of locking bugs:
13 * - lock inversion scenarios
14 * - circular lock dependencies
15 * - hardirq/softirq safe/unsafe locking bugs
17 * Bugs are reported even if the current locking scenario does not cause
18 * any deadlock at this point.
20 * I.e. if anytime in the past two locks were taken in a different order,
21 * even if it happened for another task, even if those were different
22 * locks (but of the same class as this lock), this code will detect it.
24 * Thanks to Arjan van de Ven for coming up with the initial idea of
25 * mapping lock dependencies runtime.
27 #include <linux/mutex.h>
28 #include <linux/sched.h>
29 #include <linux/delay.h>
30 #include <linux/module.h>
31 #include <linux/proc_fs.h>
32 #include <linux/seq_file.h>
33 #include <linux/spinlock.h>
34 #include <linux/kallsyms.h>
35 #include <linux/interrupt.h>
36 #include <linux/stacktrace.h>
37 #include <linux/debug_locks.h>
38 #include <linux/irqflags.h>
39 #include <linux/utsname.h>
41 #include <asm/sections.h>
43 #include "lockdep_internals.h"
46 * lockdep_lock: protects the lockdep graph, the hashes and the
47 * class/list/hash allocators.
49 * This is one of the rare exceptions where it's justified
50 * to use a raw spinlock - we really dont want the spinlock
51 * code to recurse back into the lockdep code...
53 static raw_spinlock_t lockdep_lock
= (raw_spinlock_t
)__RAW_SPIN_LOCK_UNLOCKED
;
55 static int graph_lock(void)
57 __raw_spin_lock(&lockdep_lock
);
59 * Make sure that if another CPU detected a bug while
60 * walking the graph we dont change it (while the other
61 * CPU is busy printing out stuff with the graph lock
65 __raw_spin_unlock(&lockdep_lock
);
71 static inline int graph_unlock(void)
73 if (debug_locks
&& !__raw_spin_is_locked(&lockdep_lock
))
74 return DEBUG_LOCKS_WARN_ON(1);
76 __raw_spin_unlock(&lockdep_lock
);
81 * Turn lock debugging off and return with 0 if it was off already,
82 * and also release the graph lock:
84 static inline int debug_locks_off_graph_unlock(void)
86 int ret
= debug_locks_off();
88 __raw_spin_unlock(&lockdep_lock
);
93 static int lockdep_initialized
;
95 unsigned long nr_list_entries
;
96 static struct lock_list list_entries
[MAX_LOCKDEP_ENTRIES
];
99 * Allocate a lockdep entry. (assumes the graph_lock held, returns
100 * with NULL on failure)
102 static struct lock_list
*alloc_list_entry(void)
104 if (nr_list_entries
>= MAX_LOCKDEP_ENTRIES
) {
105 if (!debug_locks_off_graph_unlock())
108 printk("BUG: MAX_LOCKDEP_ENTRIES too low!\n");
109 printk("turning off the locking correctness validator.\n");
112 return list_entries
+ nr_list_entries
++;
116 * All data structures here are protected by the global debug_lock.
118 * Mutex key structs only get allocated, once during bootup, and never
119 * get freed - this significantly simplifies the debugging code.
121 unsigned long nr_lock_classes
;
122 static struct lock_class lock_classes
[MAX_LOCKDEP_KEYS
];
125 * We keep a global list of all lock classes. The list only grows,
126 * never shrinks. The list is only accessed with the lockdep
127 * spinlock lock held.
129 LIST_HEAD(all_lock_classes
);
132 * The lockdep classes are in a hash-table as well, for fast lookup:
134 #define CLASSHASH_BITS (MAX_LOCKDEP_KEYS_BITS - 1)
135 #define CLASSHASH_SIZE (1UL << CLASSHASH_BITS)
136 #define CLASSHASH_MASK (CLASSHASH_SIZE - 1)
137 #define __classhashfn(key) ((((unsigned long)key >> CLASSHASH_BITS) + (unsigned long)key) & CLASSHASH_MASK)
138 #define classhashentry(key) (classhash_table + __classhashfn((key)))
140 static struct list_head classhash_table
[CLASSHASH_SIZE
];
142 unsigned long nr_lock_chains
;
143 static struct lock_chain lock_chains
[MAX_LOCKDEP_CHAINS
];
146 * We put the lock dependency chains into a hash-table as well, to cache
149 #define CHAINHASH_BITS (MAX_LOCKDEP_CHAINS_BITS-1)
150 #define CHAINHASH_SIZE (1UL << CHAINHASH_BITS)
151 #define CHAINHASH_MASK (CHAINHASH_SIZE - 1)
152 #define __chainhashfn(chain) \
153 (((chain >> CHAINHASH_BITS) + chain) & CHAINHASH_MASK)
154 #define chainhashentry(chain) (chainhash_table + __chainhashfn((chain)))
156 static struct list_head chainhash_table
[CHAINHASH_SIZE
];
159 * The hash key of the lock dependency chains is a hash itself too:
160 * it's a hash of all locks taken up to that lock, including that lock.
161 * It's a 64-bit hash, because it's important for the keys to be
164 #define iterate_chain_key(key1, key2) \
165 (((key1) << MAX_LOCKDEP_KEYS_BITS) ^ \
166 ((key1) >> (64-MAX_LOCKDEP_KEYS_BITS)) ^ \
169 void lockdep_off(void)
171 current
->lockdep_recursion
++;
174 EXPORT_SYMBOL(lockdep_off
);
176 void lockdep_on(void)
178 current
->lockdep_recursion
--;
181 EXPORT_SYMBOL(lockdep_on
);
184 * Debugging switches:
188 #define VERY_VERBOSE 0
191 # define HARDIRQ_VERBOSE 1
192 # define SOFTIRQ_VERBOSE 1
194 # define HARDIRQ_VERBOSE 0
195 # define SOFTIRQ_VERBOSE 0
198 #if VERBOSE || HARDIRQ_VERBOSE || SOFTIRQ_VERBOSE
200 * Quick filtering for interesting events:
202 static int class_filter(struct lock_class
*class)
206 if (class->name_version
== 1 &&
207 !strcmp(class->name
, "lockname"))
209 if (class->name_version
== 1 &&
210 !strcmp(class->name
, "&struct->lockfield"))
213 /* Filter everything else. 1 would be to allow everything else */
218 static int verbose(struct lock_class
*class)
221 return class_filter(class);
226 #ifdef CONFIG_TRACE_IRQFLAGS
228 static int hardirq_verbose(struct lock_class
*class)
231 return class_filter(class);
236 static int softirq_verbose(struct lock_class
*class)
239 return class_filter(class);
247 * Stack-trace: tightly packed array of stack backtrace
248 * addresses. Protected by the graph_lock.
250 unsigned long nr_stack_trace_entries
;
251 static unsigned long stack_trace
[MAX_STACK_TRACE_ENTRIES
];
253 static int save_trace(struct stack_trace
*trace
)
255 trace
->nr_entries
= 0;
256 trace
->max_entries
= MAX_STACK_TRACE_ENTRIES
- nr_stack_trace_entries
;
257 trace
->entries
= stack_trace
+ nr_stack_trace_entries
;
261 save_stack_trace(trace
);
263 trace
->max_entries
= trace
->nr_entries
;
265 nr_stack_trace_entries
+= trace
->nr_entries
;
267 if (nr_stack_trace_entries
== MAX_STACK_TRACE_ENTRIES
) {
268 if (!debug_locks_off_graph_unlock())
271 printk("BUG: MAX_STACK_TRACE_ENTRIES too low!\n");
272 printk("turning off the locking correctness validator.\n");
281 unsigned int nr_hardirq_chains
;
282 unsigned int nr_softirq_chains
;
283 unsigned int nr_process_chains
;
284 unsigned int max_lockdep_depth
;
285 unsigned int max_recursion_depth
;
287 #ifdef CONFIG_DEBUG_LOCKDEP
289 * We cannot printk in early bootup code. Not even early_printk()
290 * might work. So we mark any initialization errors and printk
291 * about it later on, in lockdep_info().
293 static int lockdep_init_error
;
296 * Various lockdep statistics:
298 atomic_t chain_lookup_hits
;
299 atomic_t chain_lookup_misses
;
300 atomic_t hardirqs_on_events
;
301 atomic_t hardirqs_off_events
;
302 atomic_t redundant_hardirqs_on
;
303 atomic_t redundant_hardirqs_off
;
304 atomic_t softirqs_on_events
;
305 atomic_t softirqs_off_events
;
306 atomic_t redundant_softirqs_on
;
307 atomic_t redundant_softirqs_off
;
308 atomic_t nr_unused_locks
;
309 atomic_t nr_cyclic_checks
;
310 atomic_t nr_cyclic_check_recursions
;
311 atomic_t nr_find_usage_forwards_checks
;
312 atomic_t nr_find_usage_forwards_recursions
;
313 atomic_t nr_find_usage_backwards_checks
;
314 atomic_t nr_find_usage_backwards_recursions
;
315 # define debug_atomic_inc(ptr) atomic_inc(ptr)
316 # define debug_atomic_dec(ptr) atomic_dec(ptr)
317 # define debug_atomic_read(ptr) atomic_read(ptr)
319 # define debug_atomic_inc(ptr) do { } while (0)
320 # define debug_atomic_dec(ptr) do { } while (0)
321 # define debug_atomic_read(ptr) 0
328 static const char *usage_str
[] =
330 [LOCK_USED
] = "initial-use ",
331 [LOCK_USED_IN_HARDIRQ
] = "in-hardirq-W",
332 [LOCK_USED_IN_SOFTIRQ
] = "in-softirq-W",
333 [LOCK_ENABLED_SOFTIRQS
] = "softirq-on-W",
334 [LOCK_ENABLED_HARDIRQS
] = "hardirq-on-W",
335 [LOCK_USED_IN_HARDIRQ_READ
] = "in-hardirq-R",
336 [LOCK_USED_IN_SOFTIRQ_READ
] = "in-softirq-R",
337 [LOCK_ENABLED_SOFTIRQS_READ
] = "softirq-on-R",
338 [LOCK_ENABLED_HARDIRQS_READ
] = "hardirq-on-R",
341 const char * __get_key_name(struct lockdep_subclass_key
*key
, char *str
)
343 return kallsyms_lookup((unsigned long)key
, NULL
, NULL
, NULL
, str
);
347 get_usage_chars(struct lock_class
*class, char *c1
, char *c2
, char *c3
, char *c4
)
349 *c1
= '.', *c2
= '.', *c3
= '.', *c4
= '.';
351 if (class->usage_mask
& LOCKF_USED_IN_HARDIRQ
)
354 if (class->usage_mask
& LOCKF_ENABLED_HARDIRQS
)
357 if (class->usage_mask
& LOCKF_USED_IN_SOFTIRQ
)
360 if (class->usage_mask
& LOCKF_ENABLED_SOFTIRQS
)
363 if (class->usage_mask
& LOCKF_ENABLED_HARDIRQS_READ
)
365 if (class->usage_mask
& LOCKF_USED_IN_HARDIRQ_READ
) {
367 if (class->usage_mask
& LOCKF_ENABLED_HARDIRQS_READ
)
371 if (class->usage_mask
& LOCKF_ENABLED_SOFTIRQS_READ
)
373 if (class->usage_mask
& LOCKF_USED_IN_SOFTIRQ_READ
) {
375 if (class->usage_mask
& LOCKF_ENABLED_SOFTIRQS_READ
)
380 static void print_lock_name(struct lock_class
*class)
382 char str
[KSYM_NAME_LEN
+ 1], c1
, c2
, c3
, c4
;
385 get_usage_chars(class, &c1
, &c2
, &c3
, &c4
);
389 name
= __get_key_name(class->key
, str
);
390 printk(" (%s", name
);
392 printk(" (%s", name
);
393 if (class->name_version
> 1)
394 printk("#%d", class->name_version
);
396 printk("/%d", class->subclass
);
398 printk("){%c%c%c%c}", c1
, c2
, c3
, c4
);
401 static void print_lockdep_cache(struct lockdep_map
*lock
)
404 char str
[KSYM_NAME_LEN
+ 1];
408 name
= __get_key_name(lock
->key
->subkeys
, str
);
413 static void print_lock(struct held_lock
*hlock
)
415 print_lock_name(hlock
->class);
417 print_ip_sym(hlock
->acquire_ip
);
420 static void lockdep_print_held_locks(struct task_struct
*curr
)
422 int i
, depth
= curr
->lockdep_depth
;
425 printk("no locks held by %s/%d.\n", curr
->comm
, curr
->pid
);
428 printk("%d lock%s held by %s/%d:\n",
429 depth
, depth
> 1 ? "s" : "", curr
->comm
, curr
->pid
);
431 for (i
= 0; i
< depth
; i
++) {
433 print_lock(curr
->held_locks
+ i
);
437 static void print_lock_class_header(struct lock_class
*class, int depth
)
441 printk("%*s->", depth
, "");
442 print_lock_name(class);
443 printk(" ops: %lu", class->ops
);
446 for (bit
= 0; bit
< LOCK_USAGE_STATES
; bit
++) {
447 if (class->usage_mask
& (1 << bit
)) {
450 len
+= printk("%*s %s", depth
, "", usage_str
[bit
]);
451 len
+= printk(" at:\n");
452 print_stack_trace(class->usage_traces
+ bit
, len
);
455 printk("%*s }\n", depth
, "");
457 printk("%*s ... key at: ",depth
,"");
458 print_ip_sym((unsigned long)class->key
);
462 * printk all lock dependencies starting at <entry>:
464 static void print_lock_dependencies(struct lock_class
*class, int depth
)
466 struct lock_list
*entry
;
468 if (DEBUG_LOCKS_WARN_ON(depth
>= 20))
471 print_lock_class_header(class, depth
);
473 list_for_each_entry(entry
, &class->locks_after
, entry
) {
474 if (DEBUG_LOCKS_WARN_ON(!entry
->class))
477 print_lock_dependencies(entry
->class, depth
+ 1);
479 printk("%*s ... acquired at:\n",depth
,"");
480 print_stack_trace(&entry
->trace
, 2);
486 * Add a new dependency to the head of the list:
488 static int add_lock_to_list(struct lock_class
*class, struct lock_class
*this,
489 struct list_head
*head
, unsigned long ip
, int distance
)
491 struct lock_list
*entry
;
493 * Lock not present yet - get a new dependency struct and
494 * add it to the list:
496 entry
= alloc_list_entry();
501 entry
->distance
= distance
;
502 if (!save_trace(&entry
->trace
))
506 * Since we never remove from the dependency list, the list can
507 * be walked lockless by other CPUs, it's only allocation
508 * that must be protected by the spinlock. But this also means
509 * we must make new entries visible only once writes to the
510 * entry become visible - hence the RCU op:
512 list_add_tail_rcu(&entry
->entry
, head
);
518 * Recursive, forwards-direction lock-dependency checking, used for
519 * both noncyclic checking and for hardirq-unsafe/softirq-unsafe
522 * (to keep the stackframe of the recursive functions small we
523 * use these global variables, and we also mark various helper
524 * functions as noinline.)
526 static struct held_lock
*check_source
, *check_target
;
529 * Print a dependency chain entry (this is only done when a deadlock
530 * has been detected):
533 print_circular_bug_entry(struct lock_list
*target
, unsigned int depth
)
535 if (debug_locks_silent
)
537 printk("\n-> #%u", depth
);
538 print_lock_name(target
->class);
540 print_stack_trace(&target
->trace
, 6);
545 static void print_kernel_version(void)
547 printk("%s %.*s\n", init_utsname()->release
,
548 (int)strcspn(init_utsname()->version
, " "),
549 init_utsname()->version
);
553 * When a circular dependency is detected, print the
557 print_circular_bug_header(struct lock_list
*entry
, unsigned int depth
)
559 struct task_struct
*curr
= current
;
561 if (!debug_locks_off_graph_unlock() || debug_locks_silent
)
564 printk("\n=======================================================\n");
565 printk( "[ INFO: possible circular locking dependency detected ]\n");
566 print_kernel_version();
567 printk( "-------------------------------------------------------\n");
568 printk("%s/%d is trying to acquire lock:\n",
569 curr
->comm
, curr
->pid
);
570 print_lock(check_source
);
571 printk("\nbut task is already holding lock:\n");
572 print_lock(check_target
);
573 printk("\nwhich lock already depends on the new lock.\n\n");
574 printk("\nthe existing dependency chain (in reverse order) is:\n");
576 print_circular_bug_entry(entry
, depth
);
581 static noinline
int print_circular_bug_tail(void)
583 struct task_struct
*curr
= current
;
584 struct lock_list
this;
586 if (debug_locks_silent
)
589 this.class = check_source
->class;
590 if (!save_trace(&this.trace
))
593 print_circular_bug_entry(&this, 0);
595 printk("\nother info that might help us debug this:\n\n");
596 lockdep_print_held_locks(curr
);
598 printk("\nstack backtrace:\n");
604 #define RECURSION_LIMIT 40
606 static int noinline
print_infinite_recursion_bug(void)
608 if (!debug_locks_off_graph_unlock())
617 * Prove that the dependency graph starting at <entry> can not
618 * lead to <target>. Print an error and return 0 if it does.
621 check_noncircular(struct lock_class
*source
, unsigned int depth
)
623 struct lock_list
*entry
;
625 debug_atomic_inc(&nr_cyclic_check_recursions
);
626 if (depth
> max_recursion_depth
)
627 max_recursion_depth
= depth
;
628 if (depth
>= RECURSION_LIMIT
)
629 return print_infinite_recursion_bug();
631 * Check this lock's dependency list:
633 list_for_each_entry(entry
, &source
->locks_after
, entry
) {
634 if (entry
->class == check_target
->class)
635 return print_circular_bug_header(entry
, depth
+1);
636 debug_atomic_inc(&nr_cyclic_checks
);
637 if (!check_noncircular(entry
->class, depth
+1))
638 return print_circular_bug_entry(entry
, depth
+1);
643 static int very_verbose(struct lock_class
*class)
646 return class_filter(class);
650 #ifdef CONFIG_TRACE_IRQFLAGS
653 * Forwards and backwards subgraph searching, for the purposes of
654 * proving that two subgraphs can be connected by a new dependency
655 * without creating any illegal irq-safe -> irq-unsafe lock dependency.
657 static enum lock_usage_bit find_usage_bit
;
658 static struct lock_class
*forwards_match
, *backwards_match
;
661 * Find a node in the forwards-direction dependency sub-graph starting
662 * at <source> that matches <find_usage_bit>.
664 * Return 2 if such a node exists in the subgraph, and put that node
665 * into <forwards_match>.
667 * Return 1 otherwise and keep <forwards_match> unchanged.
671 find_usage_forwards(struct lock_class
*source
, unsigned int depth
)
673 struct lock_list
*entry
;
676 if (depth
> max_recursion_depth
)
677 max_recursion_depth
= depth
;
678 if (depth
>= RECURSION_LIMIT
)
679 return print_infinite_recursion_bug();
681 debug_atomic_inc(&nr_find_usage_forwards_checks
);
682 if (source
->usage_mask
& (1 << find_usage_bit
)) {
683 forwards_match
= source
;
688 * Check this lock's dependency list:
690 list_for_each_entry(entry
, &source
->locks_after
, entry
) {
691 debug_atomic_inc(&nr_find_usage_forwards_recursions
);
692 ret
= find_usage_forwards(entry
->class, depth
+1);
693 if (ret
== 2 || ret
== 0)
700 * Find a node in the backwards-direction dependency sub-graph starting
701 * at <source> that matches <find_usage_bit>.
703 * Return 2 if such a node exists in the subgraph, and put that node
704 * into <backwards_match>.
706 * Return 1 otherwise and keep <backwards_match> unchanged.
710 find_usage_backwards(struct lock_class
*source
, unsigned int depth
)
712 struct lock_list
*entry
;
715 if (!__raw_spin_is_locked(&lockdep_lock
))
716 return DEBUG_LOCKS_WARN_ON(1);
718 if (depth
> max_recursion_depth
)
719 max_recursion_depth
= depth
;
720 if (depth
>= RECURSION_LIMIT
)
721 return print_infinite_recursion_bug();
723 debug_atomic_inc(&nr_find_usage_backwards_checks
);
724 if (source
->usage_mask
& (1 << find_usage_bit
)) {
725 backwards_match
= source
;
730 * Check this lock's dependency list:
732 list_for_each_entry(entry
, &source
->locks_before
, entry
) {
733 debug_atomic_inc(&nr_find_usage_backwards_recursions
);
734 ret
= find_usage_backwards(entry
->class, depth
+1);
735 if (ret
== 2 || ret
== 0)
742 print_bad_irq_dependency(struct task_struct
*curr
,
743 struct held_lock
*prev
,
744 struct held_lock
*next
,
745 enum lock_usage_bit bit1
,
746 enum lock_usage_bit bit2
,
747 const char *irqclass
)
749 if (!debug_locks_off_graph_unlock() || debug_locks_silent
)
752 printk("\n======================================================\n");
753 printk( "[ INFO: %s-safe -> %s-unsafe lock order detected ]\n",
755 print_kernel_version();
756 printk( "------------------------------------------------------\n");
757 printk("%s/%d [HC%u[%lu]:SC%u[%lu]:HE%u:SE%u] is trying to acquire:\n",
758 curr
->comm
, curr
->pid
,
759 curr
->hardirq_context
, hardirq_count() >> HARDIRQ_SHIFT
,
760 curr
->softirq_context
, softirq_count() >> SOFTIRQ_SHIFT
,
761 curr
->hardirqs_enabled
,
762 curr
->softirqs_enabled
);
765 printk("\nand this task is already holding:\n");
767 printk("which would create a new lock dependency:\n");
768 print_lock_name(prev
->class);
770 print_lock_name(next
->class);
773 printk("\nbut this new dependency connects a %s-irq-safe lock:\n",
775 print_lock_name(backwards_match
);
776 printk("\n... which became %s-irq-safe at:\n", irqclass
);
778 print_stack_trace(backwards_match
->usage_traces
+ bit1
, 1);
780 printk("\nto a %s-irq-unsafe lock:\n", irqclass
);
781 print_lock_name(forwards_match
);
782 printk("\n... which became %s-irq-unsafe at:\n", irqclass
);
785 print_stack_trace(forwards_match
->usage_traces
+ bit2
, 1);
787 printk("\nother info that might help us debug this:\n\n");
788 lockdep_print_held_locks(curr
);
790 printk("\nthe %s-irq-safe lock's dependencies:\n", irqclass
);
791 print_lock_dependencies(backwards_match
, 0);
793 printk("\nthe %s-irq-unsafe lock's dependencies:\n", irqclass
);
794 print_lock_dependencies(forwards_match
, 0);
796 printk("\nstack backtrace:\n");
803 check_usage(struct task_struct
*curr
, struct held_lock
*prev
,
804 struct held_lock
*next
, enum lock_usage_bit bit_backwards
,
805 enum lock_usage_bit bit_forwards
, const char *irqclass
)
809 find_usage_bit
= bit_backwards
;
810 /* fills in <backwards_match> */
811 ret
= find_usage_backwards(prev
->class, 0);
812 if (!ret
|| ret
== 1)
815 find_usage_bit
= bit_forwards
;
816 ret
= find_usage_forwards(next
->class, 0);
817 if (!ret
|| ret
== 1)
820 return print_bad_irq_dependency(curr
, prev
, next
,
821 bit_backwards
, bit_forwards
, irqclass
);
827 print_deadlock_bug(struct task_struct
*curr
, struct held_lock
*prev
,
828 struct held_lock
*next
)
830 if (!debug_locks_off_graph_unlock() || debug_locks_silent
)
833 printk("\n=============================================\n");
834 printk( "[ INFO: possible recursive locking detected ]\n");
835 print_kernel_version();
836 printk( "---------------------------------------------\n");
837 printk("%s/%d is trying to acquire lock:\n",
838 curr
->comm
, curr
->pid
);
840 printk("\nbut task is already holding lock:\n");
843 printk("\nother info that might help us debug this:\n");
844 lockdep_print_held_locks(curr
);
846 printk("\nstack backtrace:\n");
853 * Check whether we are holding such a class already.
855 * (Note that this has to be done separately, because the graph cannot
856 * detect such classes of deadlocks.)
858 * Returns: 0 on deadlock detected, 1 on OK, 2 on recursive read
861 check_deadlock(struct task_struct
*curr
, struct held_lock
*next
,
862 struct lockdep_map
*next_instance
, int read
)
864 struct held_lock
*prev
;
867 for (i
= 0; i
< curr
->lockdep_depth
; i
++) {
868 prev
= curr
->held_locks
+ i
;
869 if (prev
->class != next
->class)
872 * Allow read-after-read recursion of the same
873 * lock class (i.e. read_lock(lock)+read_lock(lock)):
875 if ((read
== 2) && prev
->read
)
877 return print_deadlock_bug(curr
, prev
, next
);
883 * There was a chain-cache miss, and we are about to add a new dependency
884 * to a previous lock. We recursively validate the following rules:
886 * - would the adding of the <prev> -> <next> dependency create a
887 * circular dependency in the graph? [== circular deadlock]
889 * - does the new prev->next dependency connect any hardirq-safe lock
890 * (in the full backwards-subgraph starting at <prev>) with any
891 * hardirq-unsafe lock (in the full forwards-subgraph starting at
892 * <next>)? [== illegal lock inversion with hardirq contexts]
894 * - does the new prev->next dependency connect any softirq-safe lock
895 * (in the full backwards-subgraph starting at <prev>) with any
896 * softirq-unsafe lock (in the full forwards-subgraph starting at
897 * <next>)? [== illegal lock inversion with softirq contexts]
899 * any of these scenarios could lead to a deadlock.
901 * Then if all the validations pass, we add the forwards and backwards
905 check_prev_add(struct task_struct
*curr
, struct held_lock
*prev
,
906 struct held_lock
*next
, int distance
)
908 struct lock_list
*entry
;
912 * Prove that the new <prev> -> <next> dependency would not
913 * create a circular dependency in the graph. (We do this by
914 * forward-recursing into the graph starting at <next>, and
915 * checking whether we can reach <prev>.)
917 * We are using global variables to control the recursion, to
918 * keep the stackframe size of the recursive functions low:
922 if (!(check_noncircular(next
->class, 0)))
923 return print_circular_bug_tail();
925 #ifdef CONFIG_TRACE_IRQFLAGS
927 * Prove that the new dependency does not connect a hardirq-safe
928 * lock with a hardirq-unsafe lock - to achieve this we search
929 * the backwards-subgraph starting at <prev>, and the
930 * forwards-subgraph starting at <next>:
932 if (!check_usage(curr
, prev
, next
, LOCK_USED_IN_HARDIRQ
,
933 LOCK_ENABLED_HARDIRQS
, "hard"))
937 * Prove that the new dependency does not connect a hardirq-safe-read
938 * lock with a hardirq-unsafe lock - to achieve this we search
939 * the backwards-subgraph starting at <prev>, and the
940 * forwards-subgraph starting at <next>:
942 if (!check_usage(curr
, prev
, next
, LOCK_USED_IN_HARDIRQ_READ
,
943 LOCK_ENABLED_HARDIRQS
, "hard-read"))
947 * Prove that the new dependency does not connect a softirq-safe
948 * lock with a softirq-unsafe lock - to achieve this we search
949 * the backwards-subgraph starting at <prev>, and the
950 * forwards-subgraph starting at <next>:
952 if (!check_usage(curr
, prev
, next
, LOCK_USED_IN_SOFTIRQ
,
953 LOCK_ENABLED_SOFTIRQS
, "soft"))
956 * Prove that the new dependency does not connect a softirq-safe-read
957 * lock with a softirq-unsafe lock - to achieve this we search
958 * the backwards-subgraph starting at <prev>, and the
959 * forwards-subgraph starting at <next>:
961 if (!check_usage(curr
, prev
, next
, LOCK_USED_IN_SOFTIRQ_READ
,
962 LOCK_ENABLED_SOFTIRQS
, "soft"))
966 * For recursive read-locks we do all the dependency checks,
967 * but we dont store read-triggered dependencies (only
968 * write-triggered dependencies). This ensures that only the
969 * write-side dependencies matter, and that if for example a
970 * write-lock never takes any other locks, then the reads are
971 * equivalent to a NOP.
973 if (next
->read
== 2 || prev
->read
== 2)
976 * Is the <prev> -> <next> dependency already present?
978 * (this may occur even though this is a new chain: consider
979 * e.g. the L1 -> L2 -> L3 -> L4 and the L5 -> L1 -> L2 -> L3
980 * chains - the second one will be new, but L1 already has
981 * L2 added to its dependency list, due to the first chain.)
983 list_for_each_entry(entry
, &prev
->class->locks_after
, entry
) {
984 if (entry
->class == next
->class) {
992 * Ok, all validations passed, add the new lock
993 * to the previous lock's dependency list:
995 ret
= add_lock_to_list(prev
->class, next
->class,
996 &prev
->class->locks_after
, next
->acquire_ip
, distance
);
1001 ret
= add_lock_to_list(next
->class, prev
->class,
1002 &next
->class->locks_before
, next
->acquire_ip
, distance
);
1007 * Debugging printouts:
1009 if (verbose(prev
->class) || verbose(next
->class)) {
1011 printk("\n new dependency: ");
1012 print_lock_name(prev
->class);
1014 print_lock_name(next
->class);
1017 return graph_lock();
1023 * Add the dependency to all directly-previous locks that are 'relevant'.
1024 * The ones that are relevant are (in increasing distance from curr):
1025 * all consecutive trylock entries and the final non-trylock entry - or
1026 * the end of this context's lock-chain - whichever comes first.
1029 check_prevs_add(struct task_struct
*curr
, struct held_lock
*next
)
1031 int depth
= curr
->lockdep_depth
;
1032 struct held_lock
*hlock
;
1037 * Depth must not be zero for a non-head lock:
1042 * At least two relevant locks must exist for this
1045 if (curr
->held_locks
[depth
].irq_context
!=
1046 curr
->held_locks
[depth
-1].irq_context
)
1050 int distance
= curr
->lockdep_depth
- depth
+ 1;
1051 hlock
= curr
->held_locks
+ depth
-1;
1053 * Only non-recursive-read entries get new dependencies
1056 if (hlock
->read
!= 2) {
1057 if (!check_prev_add(curr
, hlock
, next
, distance
))
1060 * Stop after the first non-trylock entry,
1061 * as non-trylock entries have added their
1062 * own direct dependencies already, so this
1063 * lock is connected to them indirectly:
1065 if (!hlock
->trylock
)
1070 * End of lock-stack?
1075 * Stop the search if we cross into another context:
1077 if (curr
->held_locks
[depth
].irq_context
!=
1078 curr
->held_locks
[depth
-1].irq_context
)
1083 if (!debug_locks_off_graph_unlock())
1093 * Is this the address of a static object:
1095 static int static_obj(void *obj
)
1097 unsigned long start
= (unsigned long) &_stext
,
1098 end
= (unsigned long) &_end
,
1099 addr
= (unsigned long) obj
;
1107 if ((addr
>= start
) && (addr
< end
))
1114 for_each_possible_cpu(i
) {
1115 start
= (unsigned long) &__per_cpu_start
+ per_cpu_offset(i
);
1116 end
= (unsigned long) &__per_cpu_start
+ PERCPU_ENOUGH_ROOM
1117 + per_cpu_offset(i
);
1119 if ((addr
>= start
) && (addr
< end
))
1127 return is_module_address(addr
);
1131 * To make lock name printouts unique, we calculate a unique
1132 * class->name_version generation counter:
1134 static int count_matching_names(struct lock_class
*new_class
)
1136 struct lock_class
*class;
1139 if (!new_class
->name
)
1142 list_for_each_entry(class, &all_lock_classes
, lock_entry
) {
1143 if (new_class
->key
- new_class
->subclass
== class->key
)
1144 return class->name_version
;
1145 if (class->name
&& !strcmp(class->name
, new_class
->name
))
1146 count
= max(count
, class->name_version
);
1153 * Register a lock's class in the hash-table, if the class is not present
1154 * yet. Otherwise we look it up. We cache the result in the lock object
1155 * itself, so actual lookup of the hash should be once per lock object.
1157 static inline struct lock_class
*
1158 look_up_lock_class(struct lockdep_map
*lock
, unsigned int subclass
)
1160 struct lockdep_subclass_key
*key
;
1161 struct list_head
*hash_head
;
1162 struct lock_class
*class;
1164 #ifdef CONFIG_DEBUG_LOCKDEP
1166 * If the architecture calls into lockdep before initializing
1167 * the hashes then we'll warn about it later. (we cannot printk
1170 if (unlikely(!lockdep_initialized
)) {
1172 lockdep_init_error
= 1;
1177 * Static locks do not have their class-keys yet - for them the key
1178 * is the lock object itself:
1180 if (unlikely(!lock
->key
))
1181 lock
->key
= (void *)lock
;
1184 * NOTE: the class-key must be unique. For dynamic locks, a static
1185 * lock_class_key variable is passed in through the mutex_init()
1186 * (or spin_lock_init()) call - which acts as the key. For static
1187 * locks we use the lock object itself as the key.
1189 BUILD_BUG_ON(sizeof(struct lock_class_key
) > sizeof(struct lock_class
));
1191 key
= lock
->key
->subkeys
+ subclass
;
1193 hash_head
= classhashentry(key
);
1196 * We can walk the hash lockfree, because the hash only
1197 * grows, and we are careful when adding entries to the end:
1199 list_for_each_entry(class, hash_head
, hash_entry
)
1200 if (class->key
== key
)
1207 * Register a lock's class in the hash-table, if the class is not present
1208 * yet. Otherwise we look it up. We cache the result in the lock object
1209 * itself, so actual lookup of the hash should be once per lock object.
1211 static inline struct lock_class
*
1212 register_lock_class(struct lockdep_map
*lock
, unsigned int subclass
, int force
)
1214 struct lockdep_subclass_key
*key
;
1215 struct list_head
*hash_head
;
1216 struct lock_class
*class;
1217 unsigned long flags
;
1219 class = look_up_lock_class(lock
, subclass
);
1224 * Debug-check: all keys must be persistent!
1226 if (!static_obj(lock
->key
)) {
1228 printk("INFO: trying to register non-static key.\n");
1229 printk("the code is fine but needs lockdep annotation.\n");
1230 printk("turning off the locking correctness validator.\n");
1236 key
= lock
->key
->subkeys
+ subclass
;
1237 hash_head
= classhashentry(key
);
1239 raw_local_irq_save(flags
);
1240 if (!graph_lock()) {
1241 raw_local_irq_restore(flags
);
1245 * We have to do the hash-walk again, to avoid races
1248 list_for_each_entry(class, hash_head
, hash_entry
)
1249 if (class->key
== key
)
1250 goto out_unlock_set
;
1252 * Allocate a new key from the static array, and add it to
1255 if (nr_lock_classes
>= MAX_LOCKDEP_KEYS
) {
1256 if (!debug_locks_off_graph_unlock()) {
1257 raw_local_irq_restore(flags
);
1260 raw_local_irq_restore(flags
);
1262 printk("BUG: MAX_LOCKDEP_KEYS too low!\n");
1263 printk("turning off the locking correctness validator.\n");
1266 class = lock_classes
+ nr_lock_classes
++;
1267 debug_atomic_inc(&nr_unused_locks
);
1269 class->name
= lock
->name
;
1270 class->subclass
= subclass
;
1271 INIT_LIST_HEAD(&class->lock_entry
);
1272 INIT_LIST_HEAD(&class->locks_before
);
1273 INIT_LIST_HEAD(&class->locks_after
);
1274 class->name_version
= count_matching_names(class);
1276 * We use RCU's safe list-add method to make
1277 * parallel walking of the hash-list safe:
1279 list_add_tail_rcu(&class->hash_entry
, hash_head
);
1281 if (verbose(class)) {
1283 raw_local_irq_restore(flags
);
1285 printk("\nnew class %p: %s", class->key
, class->name
);
1286 if (class->name_version
> 1)
1287 printk("#%d", class->name_version
);
1291 raw_local_irq_save(flags
);
1292 if (!graph_lock()) {
1293 raw_local_irq_restore(flags
);
1299 raw_local_irq_restore(flags
);
1301 if (!subclass
|| force
)
1302 lock
->class_cache
= class;
1304 if (DEBUG_LOCKS_WARN_ON(class->subclass
!= subclass
))
1311 * Look up a dependency chain. If the key is not present yet then
1312 * add it and return 0 - in this case the new dependency chain is
1313 * validated. If the key is already hashed, return 1.
1315 static inline int lookup_chain_cache(u64 chain_key
, struct lock_class
*class)
1317 struct list_head
*hash_head
= chainhashentry(chain_key
);
1318 struct lock_chain
*chain
;
1320 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
1323 * We can walk it lock-free, because entries only get added
1326 list_for_each_entry(chain
, hash_head
, entry
) {
1327 if (chain
->chain_key
== chain_key
) {
1329 debug_atomic_inc(&chain_lookup_hits
);
1330 if (very_verbose(class))
1331 printk("\nhash chain already cached, key: "
1332 "%016Lx tail class: [%p] %s\n",
1333 (unsigned long long)chain_key
,
1334 class->key
, class->name
);
1338 if (very_verbose(class))
1339 printk("\nnew hash chain, key: %016Lx tail class: [%p] %s\n",
1340 (unsigned long long)chain_key
, class->key
, class->name
);
1342 * Allocate a new chain entry from the static array, and add
1348 * We have to walk the chain again locked - to avoid duplicates:
1350 list_for_each_entry(chain
, hash_head
, entry
) {
1351 if (chain
->chain_key
== chain_key
) {
1356 if (unlikely(nr_lock_chains
>= MAX_LOCKDEP_CHAINS
)) {
1357 if (!debug_locks_off_graph_unlock())
1360 printk("BUG: MAX_LOCKDEP_CHAINS too low!\n");
1361 printk("turning off the locking correctness validator.\n");
1364 chain
= lock_chains
+ nr_lock_chains
++;
1365 chain
->chain_key
= chain_key
;
1366 list_add_tail_rcu(&chain
->entry
, hash_head
);
1367 debug_atomic_inc(&chain_lookup_misses
);
1368 #ifdef CONFIG_TRACE_IRQFLAGS
1369 if (current
->hardirq_context
)
1370 nr_hardirq_chains
++;
1372 if (current
->softirq_context
)
1373 nr_softirq_chains
++;
1375 nr_process_chains
++;
1378 nr_process_chains
++;
1385 * We are building curr_chain_key incrementally, so double-check
1386 * it from scratch, to make sure that it's done correctly:
1388 static void check_chain_key(struct task_struct
*curr
)
1390 #ifdef CONFIG_DEBUG_LOCKDEP
1391 struct held_lock
*hlock
, *prev_hlock
= NULL
;
1395 for (i
= 0; i
< curr
->lockdep_depth
; i
++) {
1396 hlock
= curr
->held_locks
+ i
;
1397 if (chain_key
!= hlock
->prev_chain_key
) {
1399 printk("hm#1, depth: %u [%u], %016Lx != %016Lx\n",
1400 curr
->lockdep_depth
, i
,
1401 (unsigned long long)chain_key
,
1402 (unsigned long long)hlock
->prev_chain_key
);
1406 id
= hlock
->class - lock_classes
;
1407 if (DEBUG_LOCKS_WARN_ON(id
>= MAX_LOCKDEP_KEYS
))
1410 if (prev_hlock
&& (prev_hlock
->irq_context
!=
1411 hlock
->irq_context
))
1413 chain_key
= iterate_chain_key(chain_key
, id
);
1416 if (chain_key
!= curr
->curr_chain_key
) {
1418 printk("hm#2, depth: %u [%u], %016Lx != %016Lx\n",
1419 curr
->lockdep_depth
, i
,
1420 (unsigned long long)chain_key
,
1421 (unsigned long long)curr
->curr_chain_key
);
1427 #ifdef CONFIG_TRACE_IRQFLAGS
1430 * print irq inversion bug:
1433 print_irq_inversion_bug(struct task_struct
*curr
, struct lock_class
*other
,
1434 struct held_lock
*this, int forwards
,
1435 const char *irqclass
)
1437 if (!debug_locks_off_graph_unlock() || debug_locks_silent
)
1440 printk("\n=========================================================\n");
1441 printk( "[ INFO: possible irq lock inversion dependency detected ]\n");
1442 print_kernel_version();
1443 printk( "---------------------------------------------------------\n");
1444 printk("%s/%d just changed the state of lock:\n",
1445 curr
->comm
, curr
->pid
);
1448 printk("but this lock took another, %s-irq-unsafe lock in the past:\n", irqclass
);
1450 printk("but this lock was taken by another, %s-irq-safe lock in the past:\n", irqclass
);
1451 print_lock_name(other
);
1452 printk("\n\nand interrupts could create inverse lock ordering between them.\n\n");
1454 printk("\nother info that might help us debug this:\n");
1455 lockdep_print_held_locks(curr
);
1457 printk("\nthe first lock's dependencies:\n");
1458 print_lock_dependencies(this->class, 0);
1460 printk("\nthe second lock's dependencies:\n");
1461 print_lock_dependencies(other
, 0);
1463 printk("\nstack backtrace:\n");
1470 * Prove that in the forwards-direction subgraph starting at <this>
1471 * there is no lock matching <mask>:
1474 check_usage_forwards(struct task_struct
*curr
, struct held_lock
*this,
1475 enum lock_usage_bit bit
, const char *irqclass
)
1479 find_usage_bit
= bit
;
1480 /* fills in <forwards_match> */
1481 ret
= find_usage_forwards(this->class, 0);
1482 if (!ret
|| ret
== 1)
1485 return print_irq_inversion_bug(curr
, forwards_match
, this, 1, irqclass
);
1489 * Prove that in the backwards-direction subgraph starting at <this>
1490 * there is no lock matching <mask>:
1493 check_usage_backwards(struct task_struct
*curr
, struct held_lock
*this,
1494 enum lock_usage_bit bit
, const char *irqclass
)
1498 find_usage_bit
= bit
;
1499 /* fills in <backwards_match> */
1500 ret
= find_usage_backwards(this->class, 0);
1501 if (!ret
|| ret
== 1)
1504 return print_irq_inversion_bug(curr
, backwards_match
, this, 0, irqclass
);
1507 void print_irqtrace_events(struct task_struct
*curr
)
1509 printk("irq event stamp: %u\n", curr
->irq_events
);
1510 printk("hardirqs last enabled at (%u): ", curr
->hardirq_enable_event
);
1511 print_ip_sym(curr
->hardirq_enable_ip
);
1512 printk("hardirqs last disabled at (%u): ", curr
->hardirq_disable_event
);
1513 print_ip_sym(curr
->hardirq_disable_ip
);
1514 printk("softirqs last enabled at (%u): ", curr
->softirq_enable_event
);
1515 print_ip_sym(curr
->softirq_enable_ip
);
1516 printk("softirqs last disabled at (%u): ", curr
->softirq_disable_event
);
1517 print_ip_sym(curr
->softirq_disable_ip
);
1523 print_usage_bug(struct task_struct
*curr
, struct held_lock
*this,
1524 enum lock_usage_bit prev_bit
, enum lock_usage_bit new_bit
)
1526 if (!debug_locks_off_graph_unlock() || debug_locks_silent
)
1529 printk("\n=================================\n");
1530 printk( "[ INFO: inconsistent lock state ]\n");
1531 print_kernel_version();
1532 printk( "---------------------------------\n");
1534 printk("inconsistent {%s} -> {%s} usage.\n",
1535 usage_str
[prev_bit
], usage_str
[new_bit
]);
1537 printk("%s/%d [HC%u[%lu]:SC%u[%lu]:HE%u:SE%u] takes:\n",
1538 curr
->comm
, curr
->pid
,
1539 trace_hardirq_context(curr
), hardirq_count() >> HARDIRQ_SHIFT
,
1540 trace_softirq_context(curr
), softirq_count() >> SOFTIRQ_SHIFT
,
1541 trace_hardirqs_enabled(curr
),
1542 trace_softirqs_enabled(curr
));
1545 printk("{%s} state was registered at:\n", usage_str
[prev_bit
]);
1546 print_stack_trace(this->class->usage_traces
+ prev_bit
, 1);
1548 print_irqtrace_events(curr
);
1549 printk("\nother info that might help us debug this:\n");
1550 lockdep_print_held_locks(curr
);
1552 printk("\nstack backtrace:\n");
1559 * Print out an error if an invalid bit is set:
1562 valid_state(struct task_struct
*curr
, struct held_lock
*this,
1563 enum lock_usage_bit new_bit
, enum lock_usage_bit bad_bit
)
1565 if (unlikely(this->class->usage_mask
& (1 << bad_bit
)))
1566 return print_usage_bug(curr
, this, bad_bit
, new_bit
);
1570 #define STRICT_READ_CHECKS 1
1573 * Mark a lock with a usage bit, and validate the state transition:
1575 static int mark_lock(struct task_struct
*curr
, struct held_lock
*this,
1576 enum lock_usage_bit new_bit
, unsigned long ip
)
1578 unsigned int new_mask
= 1 << new_bit
, ret
= 1;
1581 * If already set then do not dirty the cacheline,
1582 * nor do any checks:
1584 if (likely(this->class->usage_mask
& new_mask
))
1590 * Make sure we didnt race:
1592 if (unlikely(this->class->usage_mask
& new_mask
)) {
1597 this->class->usage_mask
|= new_mask
;
1599 #ifdef CONFIG_TRACE_IRQFLAGS
1600 if (new_bit
== LOCK_ENABLED_HARDIRQS
||
1601 new_bit
== LOCK_ENABLED_HARDIRQS_READ
)
1602 ip
= curr
->hardirq_enable_ip
;
1603 else if (new_bit
== LOCK_ENABLED_SOFTIRQS
||
1604 new_bit
== LOCK_ENABLED_SOFTIRQS_READ
)
1605 ip
= curr
->softirq_enable_ip
;
1607 if (!save_trace(this->class->usage_traces
+ new_bit
))
1611 #ifdef CONFIG_TRACE_IRQFLAGS
1612 case LOCK_USED_IN_HARDIRQ
:
1613 if (!valid_state(curr
, this, new_bit
, LOCK_ENABLED_HARDIRQS
))
1615 if (!valid_state(curr
, this, new_bit
,
1616 LOCK_ENABLED_HARDIRQS_READ
))
1619 * just marked it hardirq-safe, check that this lock
1620 * took no hardirq-unsafe lock in the past:
1622 if (!check_usage_forwards(curr
, this,
1623 LOCK_ENABLED_HARDIRQS
, "hard"))
1625 #if STRICT_READ_CHECKS
1627 * just marked it hardirq-safe, check that this lock
1628 * took no hardirq-unsafe-read lock in the past:
1630 if (!check_usage_forwards(curr
, this,
1631 LOCK_ENABLED_HARDIRQS_READ
, "hard-read"))
1634 if (hardirq_verbose(this->class))
1637 case LOCK_USED_IN_SOFTIRQ
:
1638 if (!valid_state(curr
, this, new_bit
, LOCK_ENABLED_SOFTIRQS
))
1640 if (!valid_state(curr
, this, new_bit
,
1641 LOCK_ENABLED_SOFTIRQS_READ
))
1644 * just marked it softirq-safe, check that this lock
1645 * took no softirq-unsafe lock in the past:
1647 if (!check_usage_forwards(curr
, this,
1648 LOCK_ENABLED_SOFTIRQS
, "soft"))
1650 #if STRICT_READ_CHECKS
1652 * just marked it softirq-safe, check that this lock
1653 * took no softirq-unsafe-read lock in the past:
1655 if (!check_usage_forwards(curr
, this,
1656 LOCK_ENABLED_SOFTIRQS_READ
, "soft-read"))
1659 if (softirq_verbose(this->class))
1662 case LOCK_USED_IN_HARDIRQ_READ
:
1663 if (!valid_state(curr
, this, new_bit
, LOCK_ENABLED_HARDIRQS
))
1666 * just marked it hardirq-read-safe, check that this lock
1667 * took no hardirq-unsafe lock in the past:
1669 if (!check_usage_forwards(curr
, this,
1670 LOCK_ENABLED_HARDIRQS
, "hard"))
1672 if (hardirq_verbose(this->class))
1675 case LOCK_USED_IN_SOFTIRQ_READ
:
1676 if (!valid_state(curr
, this, new_bit
, LOCK_ENABLED_SOFTIRQS
))
1679 * just marked it softirq-read-safe, check that this lock
1680 * took no softirq-unsafe lock in the past:
1682 if (!check_usage_forwards(curr
, this,
1683 LOCK_ENABLED_SOFTIRQS
, "soft"))
1685 if (softirq_verbose(this->class))
1688 case LOCK_ENABLED_HARDIRQS
:
1689 if (!valid_state(curr
, this, new_bit
, LOCK_USED_IN_HARDIRQ
))
1691 if (!valid_state(curr
, this, new_bit
,
1692 LOCK_USED_IN_HARDIRQ_READ
))
1695 * just marked it hardirq-unsafe, check that no hardirq-safe
1696 * lock in the system ever took it in the past:
1698 if (!check_usage_backwards(curr
, this,
1699 LOCK_USED_IN_HARDIRQ
, "hard"))
1701 #if STRICT_READ_CHECKS
1703 * just marked it hardirq-unsafe, check that no
1704 * hardirq-safe-read lock in the system ever took
1707 if (!check_usage_backwards(curr
, this,
1708 LOCK_USED_IN_HARDIRQ_READ
, "hard-read"))
1711 if (hardirq_verbose(this->class))
1714 case LOCK_ENABLED_SOFTIRQS
:
1715 if (!valid_state(curr
, this, new_bit
, LOCK_USED_IN_SOFTIRQ
))
1717 if (!valid_state(curr
, this, new_bit
,
1718 LOCK_USED_IN_SOFTIRQ_READ
))
1721 * just marked it softirq-unsafe, check that no softirq-safe
1722 * lock in the system ever took it in the past:
1724 if (!check_usage_backwards(curr
, this,
1725 LOCK_USED_IN_SOFTIRQ
, "soft"))
1727 #if STRICT_READ_CHECKS
1729 * just marked it softirq-unsafe, check that no
1730 * softirq-safe-read lock in the system ever took
1733 if (!check_usage_backwards(curr
, this,
1734 LOCK_USED_IN_SOFTIRQ_READ
, "soft-read"))
1737 if (softirq_verbose(this->class))
1740 case LOCK_ENABLED_HARDIRQS_READ
:
1741 if (!valid_state(curr
, this, new_bit
, LOCK_USED_IN_HARDIRQ
))
1743 #if STRICT_READ_CHECKS
1745 * just marked it hardirq-read-unsafe, check that no
1746 * hardirq-safe lock in the system ever took it in the past:
1748 if (!check_usage_backwards(curr
, this,
1749 LOCK_USED_IN_HARDIRQ
, "hard"))
1752 if (hardirq_verbose(this->class))
1755 case LOCK_ENABLED_SOFTIRQS_READ
:
1756 if (!valid_state(curr
, this, new_bit
, LOCK_USED_IN_SOFTIRQ
))
1758 #if STRICT_READ_CHECKS
1760 * just marked it softirq-read-unsafe, check that no
1761 * softirq-safe lock in the system ever took it in the past:
1763 if (!check_usage_backwards(curr
, this,
1764 LOCK_USED_IN_SOFTIRQ
, "soft"))
1767 if (softirq_verbose(this->class))
1773 * Add it to the global list of classes:
1775 list_add_tail_rcu(&this->class->lock_entry
, &all_lock_classes
);
1776 debug_atomic_dec(&nr_unused_locks
);
1779 if (!debug_locks_off_graph_unlock())
1788 * We must printk outside of the graph_lock:
1791 printk("\nmarked lock as {%s}:\n", usage_str
[new_bit
]);
1793 print_irqtrace_events(curr
);
1800 #ifdef CONFIG_TRACE_IRQFLAGS
1802 * Mark all held locks with a usage bit:
1805 mark_held_locks(struct task_struct
*curr
, int hardirq
, unsigned long ip
)
1807 enum lock_usage_bit usage_bit
;
1808 struct held_lock
*hlock
;
1811 for (i
= 0; i
< curr
->lockdep_depth
; i
++) {
1812 hlock
= curr
->held_locks
+ i
;
1816 usage_bit
= LOCK_ENABLED_HARDIRQS_READ
;
1818 usage_bit
= LOCK_ENABLED_HARDIRQS
;
1821 usage_bit
= LOCK_ENABLED_SOFTIRQS_READ
;
1823 usage_bit
= LOCK_ENABLED_SOFTIRQS
;
1825 if (!mark_lock(curr
, hlock
, usage_bit
, ip
))
1833 * Debugging helper: via this flag we know that we are in
1834 * 'early bootup code', and will warn about any invalid irqs-on event:
1836 static int early_boot_irqs_enabled
;
1838 void early_boot_irqs_off(void)
1840 early_boot_irqs_enabled
= 0;
1843 void early_boot_irqs_on(void)
1845 early_boot_irqs_enabled
= 1;
1849 * Hardirqs will be enabled:
1851 void trace_hardirqs_on(void)
1853 struct task_struct
*curr
= current
;
1856 if (unlikely(!debug_locks
|| current
->lockdep_recursion
))
1859 if (DEBUG_LOCKS_WARN_ON(unlikely(!early_boot_irqs_enabled
)))
1862 if (unlikely(curr
->hardirqs_enabled
)) {
1863 debug_atomic_inc(&redundant_hardirqs_on
);
1866 /* we'll do an OFF -> ON transition: */
1867 curr
->hardirqs_enabled
= 1;
1868 ip
= (unsigned long) __builtin_return_address(0);
1870 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
1872 if (DEBUG_LOCKS_WARN_ON(current
->hardirq_context
))
1875 * We are going to turn hardirqs on, so set the
1876 * usage bit for all held locks:
1878 if (!mark_held_locks(curr
, 1, ip
))
1881 * If we have softirqs enabled, then set the usage
1882 * bit for all held locks. (disabled hardirqs prevented
1883 * this bit from being set before)
1885 if (curr
->softirqs_enabled
)
1886 if (!mark_held_locks(curr
, 0, ip
))
1889 curr
->hardirq_enable_ip
= ip
;
1890 curr
->hardirq_enable_event
= ++curr
->irq_events
;
1891 debug_atomic_inc(&hardirqs_on_events
);
1894 EXPORT_SYMBOL(trace_hardirqs_on
);
1897 * Hardirqs were disabled:
1899 void trace_hardirqs_off(void)
1901 struct task_struct
*curr
= current
;
1903 if (unlikely(!debug_locks
|| current
->lockdep_recursion
))
1906 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
1909 if (curr
->hardirqs_enabled
) {
1911 * We have done an ON -> OFF transition:
1913 curr
->hardirqs_enabled
= 0;
1914 curr
->hardirq_disable_ip
= _RET_IP_
;
1915 curr
->hardirq_disable_event
= ++curr
->irq_events
;
1916 debug_atomic_inc(&hardirqs_off_events
);
1918 debug_atomic_inc(&redundant_hardirqs_off
);
1921 EXPORT_SYMBOL(trace_hardirqs_off
);
1924 * Softirqs will be enabled:
1926 void trace_softirqs_on(unsigned long ip
)
1928 struct task_struct
*curr
= current
;
1930 if (unlikely(!debug_locks
))
1933 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
1936 if (curr
->softirqs_enabled
) {
1937 debug_atomic_inc(&redundant_softirqs_on
);
1942 * We'll do an OFF -> ON transition:
1944 curr
->softirqs_enabled
= 1;
1945 curr
->softirq_enable_ip
= ip
;
1946 curr
->softirq_enable_event
= ++curr
->irq_events
;
1947 debug_atomic_inc(&softirqs_on_events
);
1949 * We are going to turn softirqs on, so set the
1950 * usage bit for all held locks, if hardirqs are
1953 if (curr
->hardirqs_enabled
)
1954 mark_held_locks(curr
, 0, ip
);
1958 * Softirqs were disabled:
1960 void trace_softirqs_off(unsigned long ip
)
1962 struct task_struct
*curr
= current
;
1964 if (unlikely(!debug_locks
))
1967 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
1970 if (curr
->softirqs_enabled
) {
1972 * We have done an ON -> OFF transition:
1974 curr
->softirqs_enabled
= 0;
1975 curr
->softirq_disable_ip
= ip
;
1976 curr
->softirq_disable_event
= ++curr
->irq_events
;
1977 debug_atomic_inc(&softirqs_off_events
);
1978 DEBUG_LOCKS_WARN_ON(!softirq_count());
1980 debug_atomic_inc(&redundant_softirqs_off
);
1986 * Initialize a lock instance's lock-class mapping info:
1988 void lockdep_init_map(struct lockdep_map
*lock
, const char *name
,
1989 struct lock_class_key
*key
, int subclass
)
1991 if (unlikely(!debug_locks
))
1994 if (DEBUG_LOCKS_WARN_ON(!key
))
1996 if (DEBUG_LOCKS_WARN_ON(!name
))
1999 * Sanity check, the lock-class key must be persistent:
2001 if (!static_obj(key
)) {
2002 printk("BUG: key %p not in .data!\n", key
);
2003 DEBUG_LOCKS_WARN_ON(1);
2008 lock
->class_cache
= NULL
;
2010 register_lock_class(lock
, subclass
, 1);
2013 EXPORT_SYMBOL_GPL(lockdep_init_map
);
2016 * This gets called for every mutex_lock*()/spin_lock*() operation.
2017 * We maintain the dependency maps and validate the locking attempt:
2019 static int __lock_acquire(struct lockdep_map
*lock
, unsigned int subclass
,
2020 int trylock
, int read
, int check
, int hardirqs_off
,
2023 struct task_struct
*curr
= current
;
2024 struct lock_class
*class = NULL
;
2025 struct held_lock
*hlock
;
2026 unsigned int depth
, id
;
2030 if (unlikely(!debug_locks
))
2033 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2036 if (unlikely(subclass
>= MAX_LOCKDEP_SUBCLASSES
)) {
2038 printk("BUG: MAX_LOCKDEP_SUBCLASSES too low!\n");
2039 printk("turning off the locking correctness validator.\n");
2044 class = lock
->class_cache
;
2046 * Not cached yet or subclass?
2048 if (unlikely(!class)) {
2049 class = register_lock_class(lock
, subclass
, 0);
2053 debug_atomic_inc((atomic_t
*)&class->ops
);
2054 if (very_verbose(class)) {
2055 printk("\nacquire class [%p] %s", class->key
, class->name
);
2056 if (class->name_version
> 1)
2057 printk("#%d", class->name_version
);
2063 * Add the lock to the list of currently held locks.
2064 * (we dont increase the depth just yet, up until the
2065 * dependency checks are done)
2067 depth
= curr
->lockdep_depth
;
2068 if (DEBUG_LOCKS_WARN_ON(depth
>= MAX_LOCK_DEPTH
))
2071 hlock
= curr
->held_locks
+ depth
;
2073 hlock
->class = class;
2074 hlock
->acquire_ip
= ip
;
2075 hlock
->instance
= lock
;
2076 hlock
->trylock
= trylock
;
2078 hlock
->check
= check
;
2079 hlock
->hardirqs_off
= hardirqs_off
;
2083 #ifdef CONFIG_TRACE_IRQFLAGS
2085 * If non-trylock use in a hardirq or softirq context, then
2086 * mark the lock as used in these contexts:
2090 if (curr
->hardirq_context
)
2091 if (!mark_lock(curr
, hlock
,
2092 LOCK_USED_IN_HARDIRQ_READ
, ip
))
2094 if (curr
->softirq_context
)
2095 if (!mark_lock(curr
, hlock
,
2096 LOCK_USED_IN_SOFTIRQ_READ
, ip
))
2099 if (curr
->hardirq_context
)
2100 if (!mark_lock(curr
, hlock
, LOCK_USED_IN_HARDIRQ
, ip
))
2102 if (curr
->softirq_context
)
2103 if (!mark_lock(curr
, hlock
, LOCK_USED_IN_SOFTIRQ
, ip
))
2107 if (!hardirqs_off
) {
2109 if (!mark_lock(curr
, hlock
,
2110 LOCK_ENABLED_HARDIRQS_READ
, ip
))
2112 if (curr
->softirqs_enabled
)
2113 if (!mark_lock(curr
, hlock
,
2114 LOCK_ENABLED_SOFTIRQS_READ
, ip
))
2117 if (!mark_lock(curr
, hlock
,
2118 LOCK_ENABLED_HARDIRQS
, ip
))
2120 if (curr
->softirqs_enabled
)
2121 if (!mark_lock(curr
, hlock
,
2122 LOCK_ENABLED_SOFTIRQS
, ip
))
2127 /* mark it as used: */
2128 if (!mark_lock(curr
, hlock
, LOCK_USED
, ip
))
2132 * Calculate the chain hash: it's the combined has of all the
2133 * lock keys along the dependency chain. We save the hash value
2134 * at every step so that we can get the current hash easily
2135 * after unlock. The chain hash is then used to cache dependency
2138 * The 'key ID' is what is the most compact key value to drive
2139 * the hash, not class->key.
2141 id
= class - lock_classes
;
2142 if (DEBUG_LOCKS_WARN_ON(id
>= MAX_LOCKDEP_KEYS
))
2145 chain_key
= curr
->curr_chain_key
;
2147 if (DEBUG_LOCKS_WARN_ON(chain_key
!= 0))
2152 hlock
->prev_chain_key
= chain_key
;
2154 #ifdef CONFIG_TRACE_IRQFLAGS
2156 * Keep track of points where we cross into an interrupt context:
2158 hlock
->irq_context
= 2*(curr
->hardirq_context
? 1 : 0) +
2159 curr
->softirq_context
;
2161 struct held_lock
*prev_hlock
;
2163 prev_hlock
= curr
->held_locks
+ depth
-1;
2165 * If we cross into another context, reset the
2166 * hash key (this also prevents the checking and the
2167 * adding of the dependency to 'prev'):
2169 if (prev_hlock
->irq_context
!= hlock
->irq_context
) {
2175 chain_key
= iterate_chain_key(chain_key
, id
);
2176 curr
->curr_chain_key
= chain_key
;
2179 * Trylock needs to maintain the stack of held locks, but it
2180 * does not add new dependencies, because trylock can be done
2183 * We look up the chain_key and do the O(N^2) check and update of
2184 * the dependencies only if this is a new dependency chain.
2185 * (If lookup_chain_cache() returns with 1 it acquires
2186 * graph_lock for us)
2188 if (!trylock
&& (check
== 2) && lookup_chain_cache(chain_key
, class)) {
2190 * Check whether last held lock:
2192 * - is irq-safe, if this lock is irq-unsafe
2193 * - is softirq-safe, if this lock is hardirq-unsafe
2195 * And check whether the new lock's dependency graph
2196 * could lead back to the previous lock.
2198 * any of these scenarios could lead to a deadlock. If
2201 int ret
= check_deadlock(curr
, hlock
, lock
, read
);
2206 * Mark recursive read, as we jump over it when
2207 * building dependencies (just like we jump over
2213 * Add dependency only if this lock is not the head
2214 * of the chain, and if it's not a secondary read-lock:
2216 if (!chain_head
&& ret
!= 2)
2217 if (!check_prevs_add(curr
, hlock
))
2221 /* after lookup_chain_cache(): */
2222 if (unlikely(!debug_locks
))
2225 curr
->lockdep_depth
++;
2226 check_chain_key(curr
);
2227 #ifdef CONFIG_DEBUG_LOCKDEP
2228 if (unlikely(!debug_locks
))
2231 if (unlikely(curr
->lockdep_depth
>= MAX_LOCK_DEPTH
)) {
2233 printk("BUG: MAX_LOCK_DEPTH too low!\n");
2234 printk("turning off the locking correctness validator.\n");
2238 if (unlikely(curr
->lockdep_depth
> max_lockdep_depth
))
2239 max_lockdep_depth
= curr
->lockdep_depth
;
2245 print_unlock_inbalance_bug(struct task_struct
*curr
, struct lockdep_map
*lock
,
2248 if (!debug_locks_off())
2250 if (debug_locks_silent
)
2253 printk("\n=====================================\n");
2254 printk( "[ BUG: bad unlock balance detected! ]\n");
2255 printk( "-------------------------------------\n");
2256 printk("%s/%d is trying to release lock (",
2257 curr
->comm
, curr
->pid
);
2258 print_lockdep_cache(lock
);
2261 printk("but there are no more locks to release!\n");
2262 printk("\nother info that might help us debug this:\n");
2263 lockdep_print_held_locks(curr
);
2265 printk("\nstack backtrace:\n");
2272 * Common debugging checks for both nested and non-nested unlock:
2274 static int check_unlock(struct task_struct
*curr
, struct lockdep_map
*lock
,
2277 if (unlikely(!debug_locks
))
2279 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2282 if (curr
->lockdep_depth
<= 0)
2283 return print_unlock_inbalance_bug(curr
, lock
, ip
);
2289 * Remove the lock to the list of currently held locks in a
2290 * potentially non-nested (out of order) manner. This is a
2291 * relatively rare operation, as all the unlock APIs default
2292 * to nested mode (which uses lock_release()):
2295 lock_release_non_nested(struct task_struct
*curr
,
2296 struct lockdep_map
*lock
, unsigned long ip
)
2298 struct held_lock
*hlock
, *prev_hlock
;
2303 * Check whether the lock exists in the current stack
2306 depth
= curr
->lockdep_depth
;
2307 if (DEBUG_LOCKS_WARN_ON(!depth
))
2311 for (i
= depth
-1; i
>= 0; i
--) {
2312 hlock
= curr
->held_locks
+ i
;
2314 * We must not cross into another context:
2316 if (prev_hlock
&& prev_hlock
->irq_context
!= hlock
->irq_context
)
2318 if (hlock
->instance
== lock
)
2322 return print_unlock_inbalance_bug(curr
, lock
, ip
);
2326 * We have the right lock to unlock, 'hlock' points to it.
2327 * Now we remove it from the stack, and add back the other
2328 * entries (if any), recalculating the hash along the way:
2330 curr
->lockdep_depth
= i
;
2331 curr
->curr_chain_key
= hlock
->prev_chain_key
;
2333 for (i
++; i
< depth
; i
++) {
2334 hlock
= curr
->held_locks
+ i
;
2335 if (!__lock_acquire(hlock
->instance
,
2336 hlock
->class->subclass
, hlock
->trylock
,
2337 hlock
->read
, hlock
->check
, hlock
->hardirqs_off
,
2342 if (DEBUG_LOCKS_WARN_ON(curr
->lockdep_depth
!= depth
- 1))
2348 * Remove the lock to the list of currently held locks - this gets
2349 * called on mutex_unlock()/spin_unlock*() (or on a failed
2350 * mutex_lock_interruptible()). This is done for unlocks that nest
2351 * perfectly. (i.e. the current top of the lock-stack is unlocked)
2353 static int lock_release_nested(struct task_struct
*curr
,
2354 struct lockdep_map
*lock
, unsigned long ip
)
2356 struct held_lock
*hlock
;
2360 * Pop off the top of the lock stack:
2362 depth
= curr
->lockdep_depth
- 1;
2363 hlock
= curr
->held_locks
+ depth
;
2366 * Is the unlock non-nested:
2368 if (hlock
->instance
!= lock
)
2369 return lock_release_non_nested(curr
, lock
, ip
);
2370 curr
->lockdep_depth
--;
2372 if (DEBUG_LOCKS_WARN_ON(!depth
&& (hlock
->prev_chain_key
!= 0)))
2375 curr
->curr_chain_key
= hlock
->prev_chain_key
;
2377 #ifdef CONFIG_DEBUG_LOCKDEP
2378 hlock
->prev_chain_key
= 0;
2379 hlock
->class = NULL
;
2380 hlock
->acquire_ip
= 0;
2381 hlock
->irq_context
= 0;
2387 * Remove the lock to the list of currently held locks - this gets
2388 * called on mutex_unlock()/spin_unlock*() (or on a failed
2389 * mutex_lock_interruptible()). This is done for unlocks that nest
2390 * perfectly. (i.e. the current top of the lock-stack is unlocked)
2393 __lock_release(struct lockdep_map
*lock
, int nested
, unsigned long ip
)
2395 struct task_struct
*curr
= current
;
2397 if (!check_unlock(curr
, lock
, ip
))
2401 if (!lock_release_nested(curr
, lock
, ip
))
2404 if (!lock_release_non_nested(curr
, lock
, ip
))
2408 check_chain_key(curr
);
2412 * Check whether we follow the irq-flags state precisely:
2414 static void check_flags(unsigned long flags
)
2416 #if defined(CONFIG_DEBUG_LOCKDEP) && defined(CONFIG_TRACE_IRQFLAGS)
2420 if (irqs_disabled_flags(flags
))
2421 DEBUG_LOCKS_WARN_ON(current
->hardirqs_enabled
);
2423 DEBUG_LOCKS_WARN_ON(!current
->hardirqs_enabled
);
2426 * We dont accurately track softirq state in e.g.
2427 * hardirq contexts (such as on 4KSTACKS), so only
2428 * check if not in hardirq contexts:
2430 if (!hardirq_count()) {
2431 if (softirq_count())
2432 DEBUG_LOCKS_WARN_ON(current
->softirqs_enabled
);
2434 DEBUG_LOCKS_WARN_ON(!current
->softirqs_enabled
);
2438 print_irqtrace_events(current
);
2443 * We are not always called with irqs disabled - do that here,
2444 * and also avoid lockdep recursion:
2446 void lock_acquire(struct lockdep_map
*lock
, unsigned int subclass
,
2447 int trylock
, int read
, int check
, unsigned long ip
)
2449 unsigned long flags
;
2451 if (unlikely(current
->lockdep_recursion
))
2454 raw_local_irq_save(flags
);
2457 current
->lockdep_recursion
= 1;
2458 __lock_acquire(lock
, subclass
, trylock
, read
, check
,
2459 irqs_disabled_flags(flags
), ip
);
2460 current
->lockdep_recursion
= 0;
2461 raw_local_irq_restore(flags
);
2464 EXPORT_SYMBOL_GPL(lock_acquire
);
2466 void lock_release(struct lockdep_map
*lock
, int nested
, unsigned long ip
)
2468 unsigned long flags
;
2470 if (unlikely(current
->lockdep_recursion
))
2473 raw_local_irq_save(flags
);
2475 current
->lockdep_recursion
= 1;
2476 __lock_release(lock
, nested
, ip
);
2477 current
->lockdep_recursion
= 0;
2478 raw_local_irq_restore(flags
);
2481 EXPORT_SYMBOL_GPL(lock_release
);
2484 * Used by the testsuite, sanitize the validator state
2485 * after a simulated failure:
2488 void lockdep_reset(void)
2490 unsigned long flags
;
2493 raw_local_irq_save(flags
);
2494 current
->curr_chain_key
= 0;
2495 current
->lockdep_depth
= 0;
2496 current
->lockdep_recursion
= 0;
2497 memset(current
->held_locks
, 0, MAX_LOCK_DEPTH
*sizeof(struct held_lock
));
2498 nr_hardirq_chains
= 0;
2499 nr_softirq_chains
= 0;
2500 nr_process_chains
= 0;
2502 for (i
= 0; i
< CHAINHASH_SIZE
; i
++)
2503 INIT_LIST_HEAD(chainhash_table
+ i
);
2504 raw_local_irq_restore(flags
);
2507 static void zap_class(struct lock_class
*class)
2512 * Remove all dependencies this lock is
2515 for (i
= 0; i
< nr_list_entries
; i
++) {
2516 if (list_entries
[i
].class == class)
2517 list_del_rcu(&list_entries
[i
].entry
);
2520 * Unhash the class and remove it from the all_lock_classes list:
2522 list_del_rcu(&class->hash_entry
);
2523 list_del_rcu(&class->lock_entry
);
2527 static inline int within(void *addr
, void *start
, unsigned long size
)
2529 return addr
>= start
&& addr
< start
+ size
;
2532 void lockdep_free_key_range(void *start
, unsigned long size
)
2534 struct lock_class
*class, *next
;
2535 struct list_head
*head
;
2536 unsigned long flags
;
2539 raw_local_irq_save(flags
);
2543 * Unhash all classes that were created by this module:
2545 for (i
= 0; i
< CLASSHASH_SIZE
; i
++) {
2546 head
= classhash_table
+ i
;
2547 if (list_empty(head
))
2549 list_for_each_entry_safe(class, next
, head
, hash_entry
)
2550 if (within(class->key
, start
, size
))
2555 raw_local_irq_restore(flags
);
2558 void lockdep_reset_lock(struct lockdep_map
*lock
)
2560 struct lock_class
*class, *next
;
2561 struct list_head
*head
;
2562 unsigned long flags
;
2565 raw_local_irq_save(flags
);
2568 * Remove all classes this lock might have:
2570 for (j
= 0; j
< MAX_LOCKDEP_SUBCLASSES
; j
++) {
2572 * If the class exists we look it up and zap it:
2574 class = look_up_lock_class(lock
, j
);
2579 * Debug check: in the end all mapped classes should
2583 for (i
= 0; i
< CLASSHASH_SIZE
; i
++) {
2584 head
= classhash_table
+ i
;
2585 if (list_empty(head
))
2587 list_for_each_entry_safe(class, next
, head
, hash_entry
) {
2588 if (unlikely(class == lock
->class_cache
)) {
2589 if (debug_locks_off_graph_unlock())
2598 raw_local_irq_restore(flags
);
2601 void lockdep_init(void)
2606 * Some architectures have their own start_kernel()
2607 * code which calls lockdep_init(), while we also
2608 * call lockdep_init() from the start_kernel() itself,
2609 * and we want to initialize the hashes only once:
2611 if (lockdep_initialized
)
2614 for (i
= 0; i
< CLASSHASH_SIZE
; i
++)
2615 INIT_LIST_HEAD(classhash_table
+ i
);
2617 for (i
= 0; i
< CHAINHASH_SIZE
; i
++)
2618 INIT_LIST_HEAD(chainhash_table
+ i
);
2620 lockdep_initialized
= 1;
2623 void __init
lockdep_info(void)
2625 printk("Lock dependency validator: Copyright (c) 2006 Red Hat, Inc., Ingo Molnar\n");
2627 printk("... MAX_LOCKDEP_SUBCLASSES: %lu\n", MAX_LOCKDEP_SUBCLASSES
);
2628 printk("... MAX_LOCK_DEPTH: %lu\n", MAX_LOCK_DEPTH
);
2629 printk("... MAX_LOCKDEP_KEYS: %lu\n", MAX_LOCKDEP_KEYS
);
2630 printk("... CLASSHASH_SIZE: %lu\n", CLASSHASH_SIZE
);
2631 printk("... MAX_LOCKDEP_ENTRIES: %lu\n", MAX_LOCKDEP_ENTRIES
);
2632 printk("... MAX_LOCKDEP_CHAINS: %lu\n", MAX_LOCKDEP_CHAINS
);
2633 printk("... CHAINHASH_SIZE: %lu\n", CHAINHASH_SIZE
);
2635 printk(" memory used by lock dependency info: %lu kB\n",
2636 (sizeof(struct lock_class
) * MAX_LOCKDEP_KEYS
+
2637 sizeof(struct list_head
) * CLASSHASH_SIZE
+
2638 sizeof(struct lock_list
) * MAX_LOCKDEP_ENTRIES
+
2639 sizeof(struct lock_chain
) * MAX_LOCKDEP_CHAINS
+
2640 sizeof(struct list_head
) * CHAINHASH_SIZE
) / 1024);
2642 printk(" per task-struct memory footprint: %lu bytes\n",
2643 sizeof(struct held_lock
) * MAX_LOCK_DEPTH
);
2645 #ifdef CONFIG_DEBUG_LOCKDEP
2646 if (lockdep_init_error
)
2647 printk("WARNING: lockdep init error! Arch code didnt call lockdep_init() early enough?\n");
2651 static inline int in_range(const void *start
, const void *addr
, const void *end
)
2653 return addr
>= start
&& addr
<= end
;
2657 print_freed_lock_bug(struct task_struct
*curr
, const void *mem_from
,
2658 const void *mem_to
, struct held_lock
*hlock
)
2660 if (!debug_locks_off())
2662 if (debug_locks_silent
)
2665 printk("\n=========================\n");
2666 printk( "[ BUG: held lock freed! ]\n");
2667 printk( "-------------------------\n");
2668 printk("%s/%d is freeing memory %p-%p, with a lock still held there!\n",
2669 curr
->comm
, curr
->pid
, mem_from
, mem_to
-1);
2671 lockdep_print_held_locks(curr
);
2673 printk("\nstack backtrace:\n");
2678 * Called when kernel memory is freed (or unmapped), or if a lock
2679 * is destroyed or reinitialized - this code checks whether there is
2680 * any held lock in the memory range of <from> to <to>:
2682 void debug_check_no_locks_freed(const void *mem_from
, unsigned long mem_len
)
2684 const void *mem_to
= mem_from
+ mem_len
, *lock_from
, *lock_to
;
2685 struct task_struct
*curr
= current
;
2686 struct held_lock
*hlock
;
2687 unsigned long flags
;
2690 if (unlikely(!debug_locks
))
2693 local_irq_save(flags
);
2694 for (i
= 0; i
< curr
->lockdep_depth
; i
++) {
2695 hlock
= curr
->held_locks
+ i
;
2697 lock_from
= (void *)hlock
->instance
;
2698 lock_to
= (void *)(hlock
->instance
+ 1);
2700 if (!in_range(mem_from
, lock_from
, mem_to
) &&
2701 !in_range(mem_from
, lock_to
, mem_to
))
2704 print_freed_lock_bug(curr
, mem_from
, mem_to
, hlock
);
2707 local_irq_restore(flags
);
2709 EXPORT_SYMBOL_GPL(debug_check_no_locks_freed
);
2711 static void print_held_locks_bug(struct task_struct
*curr
)
2713 if (!debug_locks_off())
2715 if (debug_locks_silent
)
2718 printk("\n=====================================\n");
2719 printk( "[ BUG: lock held at task exit time! ]\n");
2720 printk( "-------------------------------------\n");
2721 printk("%s/%d is exiting with locks still held!\n",
2722 curr
->comm
, curr
->pid
);
2723 lockdep_print_held_locks(curr
);
2725 printk("\nstack backtrace:\n");
2729 void debug_check_no_locks_held(struct task_struct
*task
)
2731 if (unlikely(task
->lockdep_depth
> 0))
2732 print_held_locks_bug(task
);
2735 void debug_show_all_locks(void)
2737 struct task_struct
*g
, *p
;
2741 if (unlikely(!debug_locks
)) {
2742 printk("INFO: lockdep is turned off.\n");
2745 printk("\nShowing all locks held in the system:\n");
2748 * Here we try to get the tasklist_lock as hard as possible,
2749 * if not successful after 2 seconds we ignore it (but keep
2750 * trying). This is to enable a debug printout even if a
2751 * tasklist_lock-holding task deadlocks or crashes.
2754 if (!read_trylock(&tasklist_lock
)) {
2756 printk("hm, tasklist_lock locked, retrying... ");
2759 printk(" #%d", 10-count
);
2763 printk(" ignoring it.\n");
2767 printk(" locked it.\n");
2769 do_each_thread(g
, p
) {
2770 if (p
->lockdep_depth
)
2771 lockdep_print_held_locks(p
);
2773 if (read_trylock(&tasklist_lock
))
2775 } while_each_thread(g
, p
);
2778 printk("=============================================\n\n");
2781 read_unlock(&tasklist_lock
);
2784 EXPORT_SYMBOL_GPL(debug_show_all_locks
);
2786 void debug_show_held_locks(struct task_struct
*task
)
2788 if (unlikely(!debug_locks
)) {
2789 printk("INFO: lockdep is turned off.\n");
2792 lockdep_print_held_locks(task
);
2795 EXPORT_SYMBOL_GPL(debug_show_held_locks
);