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 * All data structures here are protected by the global debug_lock.
101 * Mutex key structs only get allocated, once during bootup, and never
102 * get freed - this significantly simplifies the debugging code.
104 unsigned long nr_lock_classes;
105 static struct lock_class lock_classes[MAX_LOCKDEP_KEYS];
108 * We keep a global list of all lock classes. The list only grows,
109 * never shrinks. The list is only accessed with the lockdep
110 * spinlock lock held.
112 LIST_HEAD(all_lock_classes);
115 * The lockdep classes are in a hash-table as well, for fast lookup:
117 #define CLASSHASH_BITS (MAX_LOCKDEP_KEYS_BITS - 1)
118 #define CLASSHASH_SIZE (1UL << CLASSHASH_BITS)
119 #define CLASSHASH_MASK (CLASSHASH_SIZE - 1)
120 #define __classhashfn(key) ((((unsigned long)key >> CLASSHASH_BITS) + (unsigned long)key) & CLASSHASH_MASK)
121 #define classhashentry(key) (classhash_table + __classhashfn((key)))
123 static struct list_head classhash_table[CLASSHASH_SIZE];
126 * We put the lock dependency chains into a hash-table as well, to cache
129 #define CHAINHASH_BITS (MAX_LOCKDEP_CHAINS_BITS-1)
130 #define CHAINHASH_SIZE (1UL << CHAINHASH_BITS)
131 #define CHAINHASH_MASK (CHAINHASH_SIZE - 1)
132 #define __chainhashfn(chain) \
133 (((chain >> CHAINHASH_BITS) + chain) & CHAINHASH_MASK)
134 #define chainhashentry(chain) (chainhash_table + __chainhashfn((chain)))
136 static struct list_head chainhash_table[CHAINHASH_SIZE];
139 * The hash key of the lock dependency chains is a hash itself too:
140 * it's a hash of all locks taken up to that lock, including that lock.
141 * It's a 64-bit hash, because it's important for the keys to be
144 #define iterate_chain_key(key1, key2) \
145 (((key1) << MAX_LOCKDEP_KEYS_BITS) ^ \
146 ((key1) >> (64-MAX_LOCKDEP_KEYS_BITS)) ^ \
149 void lockdep_off(void)
151 current->lockdep_recursion++;
154 EXPORT_SYMBOL(lockdep_off);
156 void lockdep_on(void)
158 current->lockdep_recursion--;
161 EXPORT_SYMBOL(lockdep_on);
164 * Debugging switches:
168 #define VERY_VERBOSE 0
171 # define HARDIRQ_VERBOSE 1
172 # define SOFTIRQ_VERBOSE 1
174 # define HARDIRQ_VERBOSE 0
175 # define SOFTIRQ_VERBOSE 0
178 #if VERBOSE || HARDIRQ_VERBOSE || SOFTIRQ_VERBOSE
180 * Quick filtering for interesting events:
182 static int class_filter(struct lock_class *class)
186 if (class->name_version == 1 &&
187 !strcmp(class->name, "lockname"))
189 if (class->name_version == 1 &&
190 !strcmp(class->name, "&struct->lockfield"))
193 /* Filter everything else. 1 would be to allow everything else */
198 static int verbose(struct lock_class *class)
201 return class_filter(class);
207 * Stack-trace: tightly packed array of stack backtrace
208 * addresses. Protected by the graph_lock.
210 unsigned long nr_stack_trace_entries;
211 static unsigned long stack_trace[MAX_STACK_TRACE_ENTRIES];
213 static int save_trace(struct stack_trace *trace)
215 trace->nr_entries = 0;
216 trace->max_entries = MAX_STACK_TRACE_ENTRIES - nr_stack_trace_entries;
217 trace->entries = stack_trace + nr_stack_trace_entries;
221 save_stack_trace(trace);
223 trace->max_entries = trace->nr_entries;
225 nr_stack_trace_entries += trace->nr_entries;
227 if (nr_stack_trace_entries == MAX_STACK_TRACE_ENTRIES) {
228 if (!debug_locks_off_graph_unlock())
231 printk("BUG: MAX_STACK_TRACE_ENTRIES too low!\n");
232 printk("turning off the locking correctness validator.\n");
241 unsigned int nr_hardirq_chains;
242 unsigned int nr_softirq_chains;
243 unsigned int nr_process_chains;
244 unsigned int max_lockdep_depth;
245 unsigned int max_recursion_depth;
247 #ifdef CONFIG_DEBUG_LOCKDEP
249 * We cannot printk in early bootup code. Not even early_printk()
250 * might work. So we mark any initialization errors and printk
251 * about it later on, in lockdep_info().
253 static int lockdep_init_error;
256 * Various lockdep statistics:
258 atomic_t chain_lookup_hits;
259 atomic_t chain_lookup_misses;
260 atomic_t hardirqs_on_events;
261 atomic_t hardirqs_off_events;
262 atomic_t redundant_hardirqs_on;
263 atomic_t redundant_hardirqs_off;
264 atomic_t softirqs_on_events;
265 atomic_t softirqs_off_events;
266 atomic_t redundant_softirqs_on;
267 atomic_t redundant_softirqs_off;
268 atomic_t nr_unused_locks;
269 atomic_t nr_cyclic_checks;
270 atomic_t nr_cyclic_check_recursions;
271 atomic_t nr_find_usage_forwards_checks;
272 atomic_t nr_find_usage_forwards_recursions;
273 atomic_t nr_find_usage_backwards_checks;
274 atomic_t nr_find_usage_backwards_recursions;
275 # define debug_atomic_inc(ptr) atomic_inc(ptr)
276 # define debug_atomic_dec(ptr) atomic_dec(ptr)
277 # define debug_atomic_read(ptr) atomic_read(ptr)
279 # define debug_atomic_inc(ptr) do { } while (0)
280 # define debug_atomic_dec(ptr) do { } while (0)
281 # define debug_atomic_read(ptr) 0
288 static const char *usage_str[] =
290 [LOCK_USED] = "initial-use ",
291 [LOCK_USED_IN_HARDIRQ] = "in-hardirq-W",
292 [LOCK_USED_IN_SOFTIRQ] = "in-softirq-W",
293 [LOCK_ENABLED_SOFTIRQS] = "softirq-on-W",
294 [LOCK_ENABLED_HARDIRQS] = "hardirq-on-W",
295 [LOCK_USED_IN_HARDIRQ_READ] = "in-hardirq-R",
296 [LOCK_USED_IN_SOFTIRQ_READ] = "in-softirq-R",
297 [LOCK_ENABLED_SOFTIRQS_READ] = "softirq-on-R",
298 [LOCK_ENABLED_HARDIRQS_READ] = "hardirq-on-R",
301 const char * __get_key_name(struct lockdep_subclass_key *key, char *str)
303 return kallsyms_lookup((unsigned long)key, NULL, NULL, NULL, str);
307 get_usage_chars(struct lock_class *class, char *c1, char *c2, char *c3, char *c4)
309 *c1 = '.', *c2 = '.', *c3 = '.', *c4 = '.';
311 if (class->usage_mask & LOCKF_USED_IN_HARDIRQ)
314 if (class->usage_mask & LOCKF_ENABLED_HARDIRQS)
317 if (class->usage_mask & LOCKF_USED_IN_SOFTIRQ)
320 if (class->usage_mask & LOCKF_ENABLED_SOFTIRQS)
323 if (class->usage_mask & LOCKF_ENABLED_HARDIRQS_READ)
325 if (class->usage_mask & LOCKF_USED_IN_HARDIRQ_READ) {
327 if (class->usage_mask & LOCKF_ENABLED_HARDIRQS_READ)
331 if (class->usage_mask & LOCKF_ENABLED_SOFTIRQS_READ)
333 if (class->usage_mask & LOCKF_USED_IN_SOFTIRQ_READ) {
335 if (class->usage_mask & LOCKF_ENABLED_SOFTIRQS_READ)
340 static void print_lock_name(struct lock_class *class)
342 char str[KSYM_NAME_LEN], c1, c2, c3, c4;
345 get_usage_chars(class, &c1, &c2, &c3, &c4);
349 name = __get_key_name(class->key, str);
350 printk(" (%s", name);
352 printk(" (%s", name);
353 if (class->name_version > 1)
354 printk("#%d", class->name_version);
356 printk("/%d", class->subclass);
358 printk("){%c%c%c%c}", c1, c2, c3, c4);
361 static void print_lockdep_cache(struct lockdep_map *lock)
364 char str[KSYM_NAME_LEN];
368 name = __get_key_name(lock->key->subkeys, str);
373 static void print_lock(struct held_lock *hlock)
375 print_lock_name(hlock->class);
377 print_ip_sym(hlock->acquire_ip);
380 static void lockdep_print_held_locks(struct task_struct *curr)
382 int i, depth = curr->lockdep_depth;
385 printk("no locks held by %s/%d.\n", curr->comm, curr->pid);
388 printk("%d lock%s held by %s/%d:\n",
389 depth, depth > 1 ? "s" : "", curr->comm, curr->pid);
391 for (i = 0; i < depth; i++) {
393 print_lock(curr->held_locks + i);
397 static void print_lock_class_header(struct lock_class *class, int depth)
401 printk("%*s->", depth, "");
402 print_lock_name(class);
403 printk(" ops: %lu", class->ops);
406 for (bit = 0; bit < LOCK_USAGE_STATES; bit++) {
407 if (class->usage_mask & (1 << bit)) {
410 len += printk("%*s %s", depth, "", usage_str[bit]);
411 len += printk(" at:\n");
412 print_stack_trace(class->usage_traces + bit, len);
415 printk("%*s }\n", depth, "");
417 printk("%*s ... key at: ",depth,"");
418 print_ip_sym((unsigned long)class->key);
422 * printk all lock dependencies starting at <entry>:
424 static void print_lock_dependencies(struct lock_class *class, int depth)
426 struct lock_list *entry;
428 if (DEBUG_LOCKS_WARN_ON(depth >= 20))
431 print_lock_class_header(class, depth);
433 list_for_each_entry(entry, &class->locks_after, entry) {
434 if (DEBUG_LOCKS_WARN_ON(!entry->class))
437 print_lock_dependencies(entry->class, depth + 1);
439 printk("%*s ... acquired at:\n",depth,"");
440 print_stack_trace(&entry->trace, 2);
445 static void print_kernel_version(void)
447 printk("%s %.*s\n", init_utsname()->release,
448 (int)strcspn(init_utsname()->version, " "),
449 init_utsname()->version);
452 static int very_verbose(struct lock_class *class)
455 return class_filter(class);
461 * Is this the address of a static object:
463 static int static_obj(void *obj)
465 unsigned long start = (unsigned long) &_stext,
466 end = (unsigned long) &_end,
467 addr = (unsigned long) obj;
475 if ((addr >= start) && (addr < end))
482 for_each_possible_cpu(i) {
483 start = (unsigned long) &__per_cpu_start + per_cpu_offset(i);
484 end = (unsigned long) &__per_cpu_start + PERCPU_ENOUGH_ROOM
487 if ((addr >= start) && (addr < end))
495 return is_module_address(addr);
499 * To make lock name printouts unique, we calculate a unique
500 * class->name_version generation counter:
502 static int count_matching_names(struct lock_class *new_class)
504 struct lock_class *class;
507 if (!new_class->name)
510 list_for_each_entry(class, &all_lock_classes, lock_entry) {
511 if (new_class->key - new_class->subclass == class->key)
512 return class->name_version;
513 if (class->name && !strcmp(class->name, new_class->name))
514 count = max(count, class->name_version);
521 * Register a lock's class in the hash-table, if the class is not present
522 * yet. Otherwise we look it up. We cache the result in the lock object
523 * itself, so actual lookup of the hash should be once per lock object.
525 static inline struct lock_class *
526 look_up_lock_class(struct lockdep_map *lock, unsigned int subclass)
528 struct lockdep_subclass_key *key;
529 struct list_head *hash_head;
530 struct lock_class *class;
532 #ifdef CONFIG_DEBUG_LOCKDEP
534 * If the architecture calls into lockdep before initializing
535 * the hashes then we'll warn about it later. (we cannot printk
538 if (unlikely(!lockdep_initialized)) {
540 lockdep_init_error = 1;
545 * Static locks do not have their class-keys yet - for them the key
546 * is the lock object itself:
548 if (unlikely(!lock->key))
549 lock->key = (void *)lock;
552 * NOTE: the class-key must be unique. For dynamic locks, a static
553 * lock_class_key variable is passed in through the mutex_init()
554 * (or spin_lock_init()) call - which acts as the key. For static
555 * locks we use the lock object itself as the key.
557 BUILD_BUG_ON(sizeof(struct lock_class_key) > sizeof(struct lock_class));
559 key = lock->key->subkeys + subclass;
561 hash_head = classhashentry(key);
564 * We can walk the hash lockfree, because the hash only
565 * grows, and we are careful when adding entries to the end:
567 list_for_each_entry(class, hash_head, hash_entry)
568 if (class->key == key)
575 * Register a lock's class in the hash-table, if the class is not present
576 * yet. Otherwise we look it up. We cache the result in the lock object
577 * itself, so actual lookup of the hash should be once per lock object.
579 static inline struct lock_class *
580 register_lock_class(struct lockdep_map *lock, unsigned int subclass, int force)
582 struct lockdep_subclass_key *key;
583 struct list_head *hash_head;
584 struct lock_class *class;
587 class = look_up_lock_class(lock, subclass);
592 * Debug-check: all keys must be persistent!
594 if (!static_obj(lock->key)) {
596 printk("INFO: trying to register non-static key.\n");
597 printk("the code is fine but needs lockdep annotation.\n");
598 printk("turning off the locking correctness validator.\n");
604 key = lock->key->subkeys + subclass;
605 hash_head = classhashentry(key);
607 raw_local_irq_save(flags);
609 raw_local_irq_restore(flags);
613 * We have to do the hash-walk again, to avoid races
616 list_for_each_entry(class, hash_head, hash_entry)
617 if (class->key == key)
620 * Allocate a new key from the static array, and add it to
623 if (nr_lock_classes >= MAX_LOCKDEP_KEYS) {
624 if (!debug_locks_off_graph_unlock()) {
625 raw_local_irq_restore(flags);
628 raw_local_irq_restore(flags);
630 printk("BUG: MAX_LOCKDEP_KEYS too low!\n");
631 printk("turning off the locking correctness validator.\n");
634 class = lock_classes + nr_lock_classes++;
635 debug_atomic_inc(&nr_unused_locks);
637 class->name = lock->name;
638 class->subclass = subclass;
639 INIT_LIST_HEAD(&class->lock_entry);
640 INIT_LIST_HEAD(&class->locks_before);
641 INIT_LIST_HEAD(&class->locks_after);
642 class->name_version = count_matching_names(class);
644 * We use RCU's safe list-add method to make
645 * parallel walking of the hash-list safe:
647 list_add_tail_rcu(&class->hash_entry, hash_head);
649 if (verbose(class)) {
651 raw_local_irq_restore(flags);
653 printk("\nnew class %p: %s", class->key, class->name);
654 if (class->name_version > 1)
655 printk("#%d", class->name_version);
659 raw_local_irq_save(flags);
661 raw_local_irq_restore(flags);
667 raw_local_irq_restore(flags);
669 if (!subclass || force)
670 lock->class_cache = class;
672 if (DEBUG_LOCKS_WARN_ON(class->subclass != subclass))
678 #ifdef CONFIG_PROVE_LOCKING
680 * Allocate a lockdep entry. (assumes the graph_lock held, returns
681 * with NULL on failure)
683 static struct lock_list *alloc_list_entry(void)
685 if (nr_list_entries >= MAX_LOCKDEP_ENTRIES) {
686 if (!debug_locks_off_graph_unlock())
689 printk("BUG: MAX_LOCKDEP_ENTRIES too low!\n");
690 printk("turning off the locking correctness validator.\n");
693 return list_entries + nr_list_entries++;
697 * Add a new dependency to the head of the list:
699 static int add_lock_to_list(struct lock_class *class, struct lock_class *this,
700 struct list_head *head, unsigned long ip, int distance)
702 struct lock_list *entry;
704 * Lock not present yet - get a new dependency struct and
705 * add it to the list:
707 entry = alloc_list_entry();
712 entry->distance = distance;
713 if (!save_trace(&entry->trace))
717 * Since we never remove from the dependency list, the list can
718 * be walked lockless by other CPUs, it's only allocation
719 * that must be protected by the spinlock. But this also means
720 * we must make new entries visible only once writes to the
721 * entry become visible - hence the RCU op:
723 list_add_tail_rcu(&entry->entry, head);
729 * Recursive, forwards-direction lock-dependency checking, used for
730 * both noncyclic checking and for hardirq-unsafe/softirq-unsafe
733 * (to keep the stackframe of the recursive functions small we
734 * use these global variables, and we also mark various helper
735 * functions as noinline.)
737 static struct held_lock *check_source, *check_target;
740 * Print a dependency chain entry (this is only done when a deadlock
741 * has been detected):
744 print_circular_bug_entry(struct lock_list *target, unsigned int depth)
746 if (debug_locks_silent)
748 printk("\n-> #%u", depth);
749 print_lock_name(target->class);
751 print_stack_trace(&target->trace, 6);
757 * When a circular dependency is detected, print the
761 print_circular_bug_header(struct lock_list *entry, unsigned int depth)
763 struct task_struct *curr = current;
765 if (!debug_locks_off_graph_unlock() || debug_locks_silent)
768 printk("\n=======================================================\n");
769 printk( "[ INFO: possible circular locking dependency detected ]\n");
770 print_kernel_version();
771 printk( "-------------------------------------------------------\n");
772 printk("%s/%d is trying to acquire lock:\n",
773 curr->comm, curr->pid);
774 print_lock(check_source);
775 printk("\nbut task is already holding lock:\n");
776 print_lock(check_target);
777 printk("\nwhich lock already depends on the new lock.\n\n");
778 printk("\nthe existing dependency chain (in reverse order) is:\n");
780 print_circular_bug_entry(entry, depth);
785 static noinline int print_circular_bug_tail(void)
787 struct task_struct *curr = current;
788 struct lock_list this;
790 if (debug_locks_silent)
793 this.class = check_source->class;
794 if (!save_trace(&this.trace))
797 print_circular_bug_entry(&this, 0);
799 printk("\nother info that might help us debug this:\n\n");
800 lockdep_print_held_locks(curr);
802 printk("\nstack backtrace:\n");
808 #define RECURSION_LIMIT 40
810 static int noinline print_infinite_recursion_bug(void)
812 if (!debug_locks_off_graph_unlock())
821 * Prove that the dependency graph starting at <entry> can not
822 * lead to <target>. Print an error and return 0 if it does.
825 check_noncircular(struct lock_class *source, unsigned int depth)
827 struct lock_list *entry;
829 debug_atomic_inc(&nr_cyclic_check_recursions);
830 if (depth > max_recursion_depth)
831 max_recursion_depth = depth;
832 if (depth >= RECURSION_LIMIT)
833 return print_infinite_recursion_bug();
835 * Check this lock's dependency list:
837 list_for_each_entry(entry, &source->locks_after, entry) {
838 if (entry->class == check_target->class)
839 return print_circular_bug_header(entry, depth+1);
840 debug_atomic_inc(&nr_cyclic_checks);
841 if (!check_noncircular(entry->class, depth+1))
842 return print_circular_bug_entry(entry, depth+1);
847 #ifdef CONFIG_TRACE_IRQFLAGS
849 * Forwards and backwards subgraph searching, for the purposes of
850 * proving that two subgraphs can be connected by a new dependency
851 * without creating any illegal irq-safe -> irq-unsafe lock dependency.
853 static enum lock_usage_bit find_usage_bit;
854 static struct lock_class *forwards_match, *backwards_match;
857 * Find a node in the forwards-direction dependency sub-graph starting
858 * at <source> that matches <find_usage_bit>.
860 * Return 2 if such a node exists in the subgraph, and put that node
861 * into <forwards_match>.
863 * Return 1 otherwise and keep <forwards_match> unchanged.
867 find_usage_forwards(struct lock_class *source, unsigned int depth)
869 struct lock_list *entry;
872 if (depth > max_recursion_depth)
873 max_recursion_depth = depth;
874 if (depth >= RECURSION_LIMIT)
875 return print_infinite_recursion_bug();
877 debug_atomic_inc(&nr_find_usage_forwards_checks);
878 if (source->usage_mask & (1 << find_usage_bit)) {
879 forwards_match = source;
884 * Check this lock's dependency list:
886 list_for_each_entry(entry, &source->locks_after, entry) {
887 debug_atomic_inc(&nr_find_usage_forwards_recursions);
888 ret = find_usage_forwards(entry->class, depth+1);
889 if (ret == 2 || ret == 0)
896 * Find a node in the backwards-direction dependency sub-graph starting
897 * at <source> that matches <find_usage_bit>.
899 * Return 2 if such a node exists in the subgraph, and put that node
900 * into <backwards_match>.
902 * Return 1 otherwise and keep <backwards_match> unchanged.
906 find_usage_backwards(struct lock_class *source, unsigned int depth)
908 struct lock_list *entry;
911 if (!__raw_spin_is_locked(&lockdep_lock))
912 return DEBUG_LOCKS_WARN_ON(1);
914 if (depth > max_recursion_depth)
915 max_recursion_depth = depth;
916 if (depth >= RECURSION_LIMIT)
917 return print_infinite_recursion_bug();
919 debug_atomic_inc(&nr_find_usage_backwards_checks);
920 if (source->usage_mask & (1 << find_usage_bit)) {
921 backwards_match = source;
926 * Check this lock's dependency list:
928 list_for_each_entry(entry, &source->locks_before, entry) {
929 debug_atomic_inc(&nr_find_usage_backwards_recursions);
930 ret = find_usage_backwards(entry->class, depth+1);
931 if (ret == 2 || ret == 0)
938 print_bad_irq_dependency(struct task_struct *curr,
939 struct held_lock *prev,
940 struct held_lock *next,
941 enum lock_usage_bit bit1,
942 enum lock_usage_bit bit2,
943 const char *irqclass)
945 if (!debug_locks_off_graph_unlock() || debug_locks_silent)
948 printk("\n======================================================\n");
949 printk( "[ INFO: %s-safe -> %s-unsafe lock order detected ]\n",
951 print_kernel_version();
952 printk( "------------------------------------------------------\n");
953 printk("%s/%d [HC%u[%lu]:SC%u[%lu]:HE%u:SE%u] is trying to acquire:\n",
954 curr->comm, curr->pid,
955 curr->hardirq_context, hardirq_count() >> HARDIRQ_SHIFT,
956 curr->softirq_context, softirq_count() >> SOFTIRQ_SHIFT,
957 curr->hardirqs_enabled,
958 curr->softirqs_enabled);
961 printk("\nand this task is already holding:\n");
963 printk("which would create a new lock dependency:\n");
964 print_lock_name(prev->class);
966 print_lock_name(next->class);
969 printk("\nbut this new dependency connects a %s-irq-safe lock:\n",
971 print_lock_name(backwards_match);
972 printk("\n... which became %s-irq-safe at:\n", irqclass);
974 print_stack_trace(backwards_match->usage_traces + bit1, 1);
976 printk("\nto a %s-irq-unsafe lock:\n", irqclass);
977 print_lock_name(forwards_match);
978 printk("\n... which became %s-irq-unsafe at:\n", irqclass);
981 print_stack_trace(forwards_match->usage_traces + bit2, 1);
983 printk("\nother info that might help us debug this:\n\n");
984 lockdep_print_held_locks(curr);
986 printk("\nthe %s-irq-safe lock's dependencies:\n", irqclass);
987 print_lock_dependencies(backwards_match, 0);
989 printk("\nthe %s-irq-unsafe lock's dependencies:\n", irqclass);
990 print_lock_dependencies(forwards_match, 0);
992 printk("\nstack backtrace:\n");
999 check_usage(struct task_struct *curr, struct held_lock *prev,
1000 struct held_lock *next, enum lock_usage_bit bit_backwards,
1001 enum lock_usage_bit bit_forwards, const char *irqclass)
1005 find_usage_bit = bit_backwards;
1006 /* fills in <backwards_match> */
1007 ret = find_usage_backwards(prev->class, 0);
1008 if (!ret || ret == 1)
1011 find_usage_bit = bit_forwards;
1012 ret = find_usage_forwards(next->class, 0);
1013 if (!ret || ret == 1)
1016 return print_bad_irq_dependency(curr, prev, next,
1017 bit_backwards, bit_forwards, irqclass);
1021 check_prev_add_irq(struct task_struct *curr, struct held_lock *prev,
1022 struct held_lock *next)
1025 * Prove that the new dependency does not connect a hardirq-safe
1026 * lock with a hardirq-unsafe lock - to achieve this we search
1027 * the backwards-subgraph starting at <prev>, and the
1028 * forwards-subgraph starting at <next>:
1030 if (!check_usage(curr, prev, next, LOCK_USED_IN_HARDIRQ,
1031 LOCK_ENABLED_HARDIRQS, "hard"))
1035 * Prove that the new dependency does not connect a hardirq-safe-read
1036 * lock with a hardirq-unsafe lock - to achieve this we search
1037 * the backwards-subgraph starting at <prev>, and the
1038 * forwards-subgraph starting at <next>:
1040 if (!check_usage(curr, prev, next, LOCK_USED_IN_HARDIRQ_READ,
1041 LOCK_ENABLED_HARDIRQS, "hard-read"))
1045 * Prove that the new dependency does not connect a softirq-safe
1046 * lock with a softirq-unsafe lock - to achieve this we search
1047 * the backwards-subgraph starting at <prev>, and the
1048 * forwards-subgraph starting at <next>:
1050 if (!check_usage(curr, prev, next, LOCK_USED_IN_SOFTIRQ,
1051 LOCK_ENABLED_SOFTIRQS, "soft"))
1054 * Prove that the new dependency does not connect a softirq-safe-read
1055 * lock with a softirq-unsafe lock - to achieve this we search
1056 * the backwards-subgraph starting at <prev>, and the
1057 * forwards-subgraph starting at <next>:
1059 if (!check_usage(curr, prev, next, LOCK_USED_IN_SOFTIRQ_READ,
1060 LOCK_ENABLED_SOFTIRQS, "soft"))
1066 static void inc_chains(void)
1068 if (current->hardirq_context)
1069 nr_hardirq_chains++;
1071 if (current->softirq_context)
1072 nr_softirq_chains++;
1074 nr_process_chains++;
1081 check_prev_add_irq(struct task_struct *curr, struct held_lock *prev,
1082 struct held_lock *next)
1087 static inline void inc_chains(void)
1089 nr_process_chains++;
1095 print_deadlock_bug(struct task_struct *curr, struct held_lock *prev,
1096 struct held_lock *next)
1098 if (!debug_locks_off_graph_unlock() || debug_locks_silent)
1101 printk("\n=============================================\n");
1102 printk( "[ INFO: possible recursive locking detected ]\n");
1103 print_kernel_version();
1104 printk( "---------------------------------------------\n");
1105 printk("%s/%d is trying to acquire lock:\n",
1106 curr->comm, curr->pid);
1108 printk("\nbut task is already holding lock:\n");
1111 printk("\nother info that might help us debug this:\n");
1112 lockdep_print_held_locks(curr);
1114 printk("\nstack backtrace:\n");
1121 * Check whether we are holding such a class already.
1123 * (Note that this has to be done separately, because the graph cannot
1124 * detect such classes of deadlocks.)
1126 * Returns: 0 on deadlock detected, 1 on OK, 2 on recursive read
1129 check_deadlock(struct task_struct *curr, struct held_lock *next,
1130 struct lockdep_map *next_instance, int read)
1132 struct held_lock *prev;
1135 for (i = 0; i < curr->lockdep_depth; i++) {
1136 prev = curr->held_locks + i;
1137 if (prev->class != next->class)
1140 * Allow read-after-read recursion of the same
1141 * lock class (i.e. read_lock(lock)+read_lock(lock)):
1143 if ((read == 2) && prev->read)
1145 return print_deadlock_bug(curr, prev, next);
1151 * There was a chain-cache miss, and we are about to add a new dependency
1152 * to a previous lock. We recursively validate the following rules:
1154 * - would the adding of the <prev> -> <next> dependency create a
1155 * circular dependency in the graph? [== circular deadlock]
1157 * - does the new prev->next dependency connect any hardirq-safe lock
1158 * (in the full backwards-subgraph starting at <prev>) with any
1159 * hardirq-unsafe lock (in the full forwards-subgraph starting at
1160 * <next>)? [== illegal lock inversion with hardirq contexts]
1162 * - does the new prev->next dependency connect any softirq-safe lock
1163 * (in the full backwards-subgraph starting at <prev>) with any
1164 * softirq-unsafe lock (in the full forwards-subgraph starting at
1165 * <next>)? [== illegal lock inversion with softirq contexts]
1167 * any of these scenarios could lead to a deadlock.
1169 * Then if all the validations pass, we add the forwards and backwards
1173 check_prev_add(struct task_struct *curr, struct held_lock *prev,
1174 struct held_lock *next, int distance)
1176 struct lock_list *entry;
1180 * Prove that the new <prev> -> <next> dependency would not
1181 * create a circular dependency in the graph. (We do this by
1182 * forward-recursing into the graph starting at <next>, and
1183 * checking whether we can reach <prev>.)
1185 * We are using global variables to control the recursion, to
1186 * keep the stackframe size of the recursive functions low:
1188 check_source = next;
1189 check_target = prev;
1190 if (!(check_noncircular(next->class, 0)))
1191 return print_circular_bug_tail();
1193 if (!check_prev_add_irq(curr, prev, next))
1197 * For recursive read-locks we do all the dependency checks,
1198 * but we dont store read-triggered dependencies (only
1199 * write-triggered dependencies). This ensures that only the
1200 * write-side dependencies matter, and that if for example a
1201 * write-lock never takes any other locks, then the reads are
1202 * equivalent to a NOP.
1204 if (next->read == 2 || prev->read == 2)
1207 * Is the <prev> -> <next> dependency already present?
1209 * (this may occur even though this is a new chain: consider
1210 * e.g. the L1 -> L2 -> L3 -> L4 and the L5 -> L1 -> L2 -> L3
1211 * chains - the second one will be new, but L1 already has
1212 * L2 added to its dependency list, due to the first chain.)
1214 list_for_each_entry(entry, &prev->class->locks_after, entry) {
1215 if (entry->class == next->class) {
1217 entry->distance = 1;
1223 * Ok, all validations passed, add the new lock
1224 * to the previous lock's dependency list:
1226 ret = add_lock_to_list(prev->class, next->class,
1227 &prev->class->locks_after, next->acquire_ip, distance);
1232 ret = add_lock_to_list(next->class, prev->class,
1233 &next->class->locks_before, next->acquire_ip, distance);
1238 * Debugging printouts:
1240 if (verbose(prev->class) || verbose(next->class)) {
1242 printk("\n new dependency: ");
1243 print_lock_name(prev->class);
1245 print_lock_name(next->class);
1248 return graph_lock();
1254 * Add the dependency to all directly-previous locks that are 'relevant'.
1255 * The ones that are relevant are (in increasing distance from curr):
1256 * all consecutive trylock entries and the final non-trylock entry - or
1257 * the end of this context's lock-chain - whichever comes first.
1260 check_prevs_add(struct task_struct *curr, struct held_lock *next)
1262 int depth = curr->lockdep_depth;
1263 struct held_lock *hlock;
1268 * Depth must not be zero for a non-head lock:
1273 * At least two relevant locks must exist for this
1276 if (curr->held_locks[depth].irq_context !=
1277 curr->held_locks[depth-1].irq_context)
1281 int distance = curr->lockdep_depth - depth + 1;
1282 hlock = curr->held_locks + depth-1;
1284 * Only non-recursive-read entries get new dependencies
1287 if (hlock->read != 2) {
1288 if (!check_prev_add(curr, hlock, next, distance))
1291 * Stop after the first non-trylock entry,
1292 * as non-trylock entries have added their
1293 * own direct dependencies already, so this
1294 * lock is connected to them indirectly:
1296 if (!hlock->trylock)
1301 * End of lock-stack?
1306 * Stop the search if we cross into another context:
1308 if (curr->held_locks[depth].irq_context !=
1309 curr->held_locks[depth-1].irq_context)
1314 if (!debug_locks_off_graph_unlock())
1322 unsigned long nr_lock_chains;
1323 static struct lock_chain lock_chains[MAX_LOCKDEP_CHAINS];
1326 * Look up a dependency chain. If the key is not present yet then
1327 * add it and return 1 - in this case the new dependency chain is
1328 * validated. If the key is already hashed, return 0.
1329 * (On return with 1 graph_lock is held.)
1331 static inline int lookup_chain_cache(u64 chain_key, struct lock_class *class)
1333 struct list_head *hash_head = chainhashentry(chain_key);
1334 struct lock_chain *chain;
1336 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
1339 * We can walk it lock-free, because entries only get added
1342 list_for_each_entry(chain, hash_head, entry) {
1343 if (chain->chain_key == chain_key) {
1345 debug_atomic_inc(&chain_lookup_hits);
1346 if (very_verbose(class))
1347 printk("\nhash chain already cached, key: "
1348 "%016Lx tail class: [%p] %s\n",
1349 (unsigned long long)chain_key,
1350 class->key, class->name);
1354 if (very_verbose(class))
1355 printk("\nnew hash chain, key: %016Lx tail class: [%p] %s\n",
1356 (unsigned long long)chain_key, class->key, class->name);
1358 * Allocate a new chain entry from the static array, and add
1364 * We have to walk the chain again locked - to avoid duplicates:
1366 list_for_each_entry(chain, hash_head, entry) {
1367 if (chain->chain_key == chain_key) {
1372 if (unlikely(nr_lock_chains >= MAX_LOCKDEP_CHAINS)) {
1373 if (!debug_locks_off_graph_unlock())
1376 printk("BUG: MAX_LOCKDEP_CHAINS too low!\n");
1377 printk("turning off the locking correctness validator.\n");
1380 chain = lock_chains + nr_lock_chains++;
1381 chain->chain_key = chain_key;
1382 list_add_tail_rcu(&chain->entry, hash_head);
1383 debug_atomic_inc(&chain_lookup_misses);
1389 static int validate_chain(struct task_struct *curr, struct lockdep_map *lock,
1390 struct held_lock *hlock, int chain_head)
1393 * Trylock needs to maintain the stack of held locks, but it
1394 * does not add new dependencies, because trylock can be done
1397 * We look up the chain_key and do the O(N^2) check and update of
1398 * the dependencies only if this is a new dependency chain.
1399 * (If lookup_chain_cache() returns with 1 it acquires
1400 * graph_lock for us)
1402 if (!hlock->trylock && (hlock->check == 2) &&
1403 lookup_chain_cache(curr->curr_chain_key, hlock->class)) {
1405 * Check whether last held lock:
1407 * - is irq-safe, if this lock is irq-unsafe
1408 * - is softirq-safe, if this lock is hardirq-unsafe
1410 * And check whether the new lock's dependency graph
1411 * could lead back to the previous lock.
1413 * any of these scenarios could lead to a deadlock. If
1416 int ret = check_deadlock(curr, hlock, lock, hlock->read);
1421 * Mark recursive read, as we jump over it when
1422 * building dependencies (just like we jump over
1428 * Add dependency only if this lock is not the head
1429 * of the chain, and if it's not a secondary read-lock:
1431 if (!chain_head && ret != 2)
1432 if (!check_prevs_add(curr, hlock))
1436 /* after lookup_chain_cache(): */
1437 if (unlikely(!debug_locks))
1443 static inline int validate_chain(struct task_struct *curr,
1444 struct lockdep_map *lock, struct held_lock *hlock,
1452 * We are building curr_chain_key incrementally, so double-check
1453 * it from scratch, to make sure that it's done correctly:
1455 static void check_chain_key(struct task_struct *curr)
1457 #ifdef CONFIG_DEBUG_LOCKDEP
1458 struct held_lock *hlock, *prev_hlock = NULL;
1462 for (i = 0; i < curr->lockdep_depth; i++) {
1463 hlock = curr->held_locks + i;
1464 if (chain_key != hlock->prev_chain_key) {
1466 printk("hm#1, depth: %u [%u], %016Lx != %016Lx\n",
1467 curr->lockdep_depth, i,
1468 (unsigned long long)chain_key,
1469 (unsigned long long)hlock->prev_chain_key);
1473 id = hlock->class - lock_classes;
1474 if (DEBUG_LOCKS_WARN_ON(id >= MAX_LOCKDEP_KEYS))
1477 if (prev_hlock && (prev_hlock->irq_context !=
1478 hlock->irq_context))
1480 chain_key = iterate_chain_key(chain_key, id);
1483 if (chain_key != curr->curr_chain_key) {
1485 printk("hm#2, depth: %u [%u], %016Lx != %016Lx\n",
1486 curr->lockdep_depth, i,
1487 (unsigned long long)chain_key,
1488 (unsigned long long)curr->curr_chain_key);
1495 print_usage_bug(struct task_struct *curr, struct held_lock *this,
1496 enum lock_usage_bit prev_bit, enum lock_usage_bit new_bit)
1498 if (!debug_locks_off_graph_unlock() || debug_locks_silent)
1501 printk("\n=================================\n");
1502 printk( "[ INFO: inconsistent lock state ]\n");
1503 print_kernel_version();
1504 printk( "---------------------------------\n");
1506 printk("inconsistent {%s} -> {%s} usage.\n",
1507 usage_str[prev_bit], usage_str[new_bit]);
1509 printk("%s/%d [HC%u[%lu]:SC%u[%lu]:HE%u:SE%u] takes:\n",
1510 curr->comm, curr->pid,
1511 trace_hardirq_context(curr), hardirq_count() >> HARDIRQ_SHIFT,
1512 trace_softirq_context(curr), softirq_count() >> SOFTIRQ_SHIFT,
1513 trace_hardirqs_enabled(curr),
1514 trace_softirqs_enabled(curr));
1517 printk("{%s} state was registered at:\n", usage_str[prev_bit]);
1518 print_stack_trace(this->class->usage_traces + prev_bit, 1);
1520 print_irqtrace_events(curr);
1521 printk("\nother info that might help us debug this:\n");
1522 lockdep_print_held_locks(curr);
1524 printk("\nstack backtrace:\n");
1531 * Print out an error if an invalid bit is set:
1534 valid_state(struct task_struct *curr, struct held_lock *this,
1535 enum lock_usage_bit new_bit, enum lock_usage_bit bad_bit)
1537 if (unlikely(this->class->usage_mask & (1 << bad_bit)))
1538 return print_usage_bug(curr, this, bad_bit, new_bit);
1542 static int mark_lock(struct task_struct *curr, struct held_lock *this,
1543 enum lock_usage_bit new_bit);
1545 #ifdef CONFIG_TRACE_IRQFLAGS
1548 * print irq inversion bug:
1551 print_irq_inversion_bug(struct task_struct *curr, struct lock_class *other,
1552 struct held_lock *this, int forwards,
1553 const char *irqclass)
1555 if (!debug_locks_off_graph_unlock() || debug_locks_silent)
1558 printk("\n=========================================================\n");
1559 printk( "[ INFO: possible irq lock inversion dependency detected ]\n");
1560 print_kernel_version();
1561 printk( "---------------------------------------------------------\n");
1562 printk("%s/%d just changed the state of lock:\n",
1563 curr->comm, curr->pid);
1566 printk("but this lock took another, %s-irq-unsafe lock in the past:\n", irqclass);
1568 printk("but this lock was taken by another, %s-irq-safe lock in the past:\n", irqclass);
1569 print_lock_name(other);
1570 printk("\n\nand interrupts could create inverse lock ordering between them.\n\n");
1572 printk("\nother info that might help us debug this:\n");
1573 lockdep_print_held_locks(curr);
1575 printk("\nthe first lock's dependencies:\n");
1576 print_lock_dependencies(this->class, 0);
1578 printk("\nthe second lock's dependencies:\n");
1579 print_lock_dependencies(other, 0);
1581 printk("\nstack backtrace:\n");
1588 * Prove that in the forwards-direction subgraph starting at <this>
1589 * there is no lock matching <mask>:
1592 check_usage_forwards(struct task_struct *curr, struct held_lock *this,
1593 enum lock_usage_bit bit, const char *irqclass)
1597 find_usage_bit = bit;
1598 /* fills in <forwards_match> */
1599 ret = find_usage_forwards(this->class, 0);
1600 if (!ret || ret == 1)
1603 return print_irq_inversion_bug(curr, forwards_match, this, 1, irqclass);
1607 * Prove that in the backwards-direction subgraph starting at <this>
1608 * there is no lock matching <mask>:
1611 check_usage_backwards(struct task_struct *curr, struct held_lock *this,
1612 enum lock_usage_bit bit, const char *irqclass)
1616 find_usage_bit = bit;
1617 /* fills in <backwards_match> */
1618 ret = find_usage_backwards(this->class, 0);
1619 if (!ret || ret == 1)
1622 return print_irq_inversion_bug(curr, backwards_match, this, 0, irqclass);
1625 void print_irqtrace_events(struct task_struct *curr)
1627 printk("irq event stamp: %u\n", curr->irq_events);
1628 printk("hardirqs last enabled at (%u): ", curr->hardirq_enable_event);
1629 print_ip_sym(curr->hardirq_enable_ip);
1630 printk("hardirqs last disabled at (%u): ", curr->hardirq_disable_event);
1631 print_ip_sym(curr->hardirq_disable_ip);
1632 printk("softirqs last enabled at (%u): ", curr->softirq_enable_event);
1633 print_ip_sym(curr->softirq_enable_ip);
1634 printk("softirqs last disabled at (%u): ", curr->softirq_disable_event);
1635 print_ip_sym(curr->softirq_disable_ip);
1638 static int hardirq_verbose(struct lock_class *class)
1641 return class_filter(class);
1646 static int softirq_verbose(struct lock_class *class)
1649 return class_filter(class);
1654 #define STRICT_READ_CHECKS 1
1656 static int mark_lock_irq(struct task_struct *curr, struct held_lock *this,
1657 enum lock_usage_bit new_bit)
1662 case LOCK_USED_IN_HARDIRQ:
1663 if (!valid_state(curr, this, new_bit, LOCK_ENABLED_HARDIRQS))
1665 if (!valid_state(curr, this, new_bit,
1666 LOCK_ENABLED_HARDIRQS_READ))
1669 * just marked it hardirq-safe, check that this lock
1670 * took no hardirq-unsafe lock in the past:
1672 if (!check_usage_forwards(curr, this,
1673 LOCK_ENABLED_HARDIRQS, "hard"))
1675 #if STRICT_READ_CHECKS
1677 * just marked it hardirq-safe, check that this lock
1678 * took no hardirq-unsafe-read lock in the past:
1680 if (!check_usage_forwards(curr, this,
1681 LOCK_ENABLED_HARDIRQS_READ, "hard-read"))
1684 if (hardirq_verbose(this->class))
1687 case LOCK_USED_IN_SOFTIRQ:
1688 if (!valid_state(curr, this, new_bit, LOCK_ENABLED_SOFTIRQS))
1690 if (!valid_state(curr, this, new_bit,
1691 LOCK_ENABLED_SOFTIRQS_READ))
1694 * just marked it softirq-safe, check that this lock
1695 * took no softirq-unsafe lock in the past:
1697 if (!check_usage_forwards(curr, this,
1698 LOCK_ENABLED_SOFTIRQS, "soft"))
1700 #if STRICT_READ_CHECKS
1702 * just marked it softirq-safe, check that this lock
1703 * took no softirq-unsafe-read lock in the past:
1705 if (!check_usage_forwards(curr, this,
1706 LOCK_ENABLED_SOFTIRQS_READ, "soft-read"))
1709 if (softirq_verbose(this->class))
1712 case LOCK_USED_IN_HARDIRQ_READ:
1713 if (!valid_state(curr, this, new_bit, LOCK_ENABLED_HARDIRQS))
1716 * just marked it hardirq-read-safe, check that this lock
1717 * took no hardirq-unsafe lock in the past:
1719 if (!check_usage_forwards(curr, this,
1720 LOCK_ENABLED_HARDIRQS, "hard"))
1722 if (hardirq_verbose(this->class))
1725 case LOCK_USED_IN_SOFTIRQ_READ:
1726 if (!valid_state(curr, this, new_bit, LOCK_ENABLED_SOFTIRQS))
1729 * just marked it softirq-read-safe, check that this lock
1730 * took no softirq-unsafe lock in the past:
1732 if (!check_usage_forwards(curr, this,
1733 LOCK_ENABLED_SOFTIRQS, "soft"))
1735 if (softirq_verbose(this->class))
1738 case LOCK_ENABLED_HARDIRQS:
1739 if (!valid_state(curr, this, new_bit, LOCK_USED_IN_HARDIRQ))
1741 if (!valid_state(curr, this, new_bit,
1742 LOCK_USED_IN_HARDIRQ_READ))
1745 * just marked it hardirq-unsafe, check that no hardirq-safe
1746 * lock in the system ever took it in the past:
1748 if (!check_usage_backwards(curr, this,
1749 LOCK_USED_IN_HARDIRQ, "hard"))
1751 #if STRICT_READ_CHECKS
1753 * just marked it hardirq-unsafe, check that no
1754 * hardirq-safe-read lock in the system ever took
1757 if (!check_usage_backwards(curr, this,
1758 LOCK_USED_IN_HARDIRQ_READ, "hard-read"))
1761 if (hardirq_verbose(this->class))
1764 case LOCK_ENABLED_SOFTIRQS:
1765 if (!valid_state(curr, this, new_bit, LOCK_USED_IN_SOFTIRQ))
1767 if (!valid_state(curr, this, new_bit,
1768 LOCK_USED_IN_SOFTIRQ_READ))
1771 * just marked it softirq-unsafe, check that no softirq-safe
1772 * lock in the system ever took it in the past:
1774 if (!check_usage_backwards(curr, this,
1775 LOCK_USED_IN_SOFTIRQ, "soft"))
1777 #if STRICT_READ_CHECKS
1779 * just marked it softirq-unsafe, check that no
1780 * softirq-safe-read lock in the system ever took
1783 if (!check_usage_backwards(curr, this,
1784 LOCK_USED_IN_SOFTIRQ_READ, "soft-read"))
1787 if (softirq_verbose(this->class))
1790 case LOCK_ENABLED_HARDIRQS_READ:
1791 if (!valid_state(curr, this, new_bit, LOCK_USED_IN_HARDIRQ))
1793 #if STRICT_READ_CHECKS
1795 * just marked it hardirq-read-unsafe, check that no
1796 * hardirq-safe lock in the system ever took it in the past:
1798 if (!check_usage_backwards(curr, this,
1799 LOCK_USED_IN_HARDIRQ, "hard"))
1802 if (hardirq_verbose(this->class))
1805 case LOCK_ENABLED_SOFTIRQS_READ:
1806 if (!valid_state(curr, this, new_bit, LOCK_USED_IN_SOFTIRQ))
1808 #if STRICT_READ_CHECKS
1810 * just marked it softirq-read-unsafe, check that no
1811 * softirq-safe lock in the system ever took it in the past:
1813 if (!check_usage_backwards(curr, this,
1814 LOCK_USED_IN_SOFTIRQ, "soft"))
1817 if (softirq_verbose(this->class))
1829 * Mark all held locks with a usage bit:
1832 mark_held_locks(struct task_struct *curr, int hardirq)
1834 enum lock_usage_bit usage_bit;
1835 struct held_lock *hlock;
1838 for (i = 0; i < curr->lockdep_depth; i++) {
1839 hlock = curr->held_locks + i;
1843 usage_bit = LOCK_ENABLED_HARDIRQS_READ;
1845 usage_bit = LOCK_ENABLED_HARDIRQS;
1848 usage_bit = LOCK_ENABLED_SOFTIRQS_READ;
1850 usage_bit = LOCK_ENABLED_SOFTIRQS;
1852 if (!mark_lock(curr, hlock, usage_bit))
1860 * Debugging helper: via this flag we know that we are in
1861 * 'early bootup code', and will warn about any invalid irqs-on event:
1863 static int early_boot_irqs_enabled;
1865 void early_boot_irqs_off(void)
1867 early_boot_irqs_enabled = 0;
1870 void early_boot_irqs_on(void)
1872 early_boot_irqs_enabled = 1;
1876 * Hardirqs will be enabled:
1878 void trace_hardirqs_on(void)
1880 struct task_struct *curr = current;
1883 if (unlikely(!debug_locks || current->lockdep_recursion))
1886 if (DEBUG_LOCKS_WARN_ON(unlikely(!early_boot_irqs_enabled)))
1889 if (unlikely(curr->hardirqs_enabled)) {
1890 debug_atomic_inc(&redundant_hardirqs_on);
1893 /* we'll do an OFF -> ON transition: */
1894 curr->hardirqs_enabled = 1;
1895 ip = (unsigned long) __builtin_return_address(0);
1897 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
1899 if (DEBUG_LOCKS_WARN_ON(current->hardirq_context))
1902 * We are going to turn hardirqs on, so set the
1903 * usage bit for all held locks:
1905 if (!mark_held_locks(curr, 1))
1908 * If we have softirqs enabled, then set the usage
1909 * bit for all held locks. (disabled hardirqs prevented
1910 * this bit from being set before)
1912 if (curr->softirqs_enabled)
1913 if (!mark_held_locks(curr, 0))
1916 curr->hardirq_enable_ip = ip;
1917 curr->hardirq_enable_event = ++curr->irq_events;
1918 debug_atomic_inc(&hardirqs_on_events);
1921 EXPORT_SYMBOL(trace_hardirqs_on);
1924 * Hardirqs were disabled:
1926 void trace_hardirqs_off(void)
1928 struct task_struct *curr = current;
1930 if (unlikely(!debug_locks || current->lockdep_recursion))
1933 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
1936 if (curr->hardirqs_enabled) {
1938 * We have done an ON -> OFF transition:
1940 curr->hardirqs_enabled = 0;
1941 curr->hardirq_disable_ip = _RET_IP_;
1942 curr->hardirq_disable_event = ++curr->irq_events;
1943 debug_atomic_inc(&hardirqs_off_events);
1945 debug_atomic_inc(&redundant_hardirqs_off);
1948 EXPORT_SYMBOL(trace_hardirqs_off);
1951 * Softirqs will be enabled:
1953 void trace_softirqs_on(unsigned long ip)
1955 struct task_struct *curr = current;
1957 if (unlikely(!debug_locks))
1960 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
1963 if (curr->softirqs_enabled) {
1964 debug_atomic_inc(&redundant_softirqs_on);
1969 * We'll do an OFF -> ON transition:
1971 curr->softirqs_enabled = 1;
1972 curr->softirq_enable_ip = ip;
1973 curr->softirq_enable_event = ++curr->irq_events;
1974 debug_atomic_inc(&softirqs_on_events);
1976 * We are going to turn softirqs on, so set the
1977 * usage bit for all held locks, if hardirqs are
1980 if (curr->hardirqs_enabled)
1981 mark_held_locks(curr, 0);
1985 * Softirqs were disabled:
1987 void trace_softirqs_off(unsigned long ip)
1989 struct task_struct *curr = current;
1991 if (unlikely(!debug_locks))
1994 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
1997 if (curr->softirqs_enabled) {
1999 * We have done an ON -> OFF transition:
2001 curr->softirqs_enabled = 0;
2002 curr->softirq_disable_ip = ip;
2003 curr->softirq_disable_event = ++curr->irq_events;
2004 debug_atomic_inc(&softirqs_off_events);
2005 DEBUG_LOCKS_WARN_ON(!softirq_count());
2007 debug_atomic_inc(&redundant_softirqs_off);
2010 static int mark_irqflags(struct task_struct *curr, struct held_lock *hlock)
2013 * If non-trylock use in a hardirq or softirq context, then
2014 * mark the lock as used in these contexts:
2016 if (!hlock->trylock) {
2018 if (curr->hardirq_context)
2019 if (!mark_lock(curr, hlock,
2020 LOCK_USED_IN_HARDIRQ_READ))
2022 if (curr->softirq_context)
2023 if (!mark_lock(curr, hlock,
2024 LOCK_USED_IN_SOFTIRQ_READ))
2027 if (curr->hardirq_context)
2028 if (!mark_lock(curr, hlock, LOCK_USED_IN_HARDIRQ))
2030 if (curr->softirq_context)
2031 if (!mark_lock(curr, hlock, LOCK_USED_IN_SOFTIRQ))
2035 if (!hlock->hardirqs_off) {
2037 if (!mark_lock(curr, hlock,
2038 LOCK_ENABLED_HARDIRQS_READ))
2040 if (curr->softirqs_enabled)
2041 if (!mark_lock(curr, hlock,
2042 LOCK_ENABLED_SOFTIRQS_READ))
2045 if (!mark_lock(curr, hlock,
2046 LOCK_ENABLED_HARDIRQS))
2048 if (curr->softirqs_enabled)
2049 if (!mark_lock(curr, hlock,
2050 LOCK_ENABLED_SOFTIRQS))
2058 static int separate_irq_context(struct task_struct *curr,
2059 struct held_lock *hlock)
2061 unsigned int depth = curr->lockdep_depth;
2064 * Keep track of points where we cross into an interrupt context:
2066 hlock->irq_context = 2*(curr->hardirq_context ? 1 : 0) +
2067 curr->softirq_context;
2069 struct held_lock *prev_hlock;
2071 prev_hlock = curr->held_locks + depth-1;
2073 * If we cross into another context, reset the
2074 * hash key (this also prevents the checking and the
2075 * adding of the dependency to 'prev'):
2077 if (prev_hlock->irq_context != hlock->irq_context)
2086 int mark_lock_irq(struct task_struct *curr, struct held_lock *this,
2087 enum lock_usage_bit new_bit)
2093 static inline int mark_irqflags(struct task_struct *curr,
2094 struct held_lock *hlock)
2099 static inline int separate_irq_context(struct task_struct *curr,
2100 struct held_lock *hlock)
2108 * Mark a lock with a usage bit, and validate the state transition:
2110 static int mark_lock(struct task_struct *curr, struct held_lock *this,
2111 enum lock_usage_bit new_bit)
2113 unsigned int new_mask = 1 << new_bit, ret = 1;
2116 * If already set then do not dirty the cacheline,
2117 * nor do any checks:
2119 if (likely(this->class->usage_mask & new_mask))
2125 * Make sure we didnt race:
2127 if (unlikely(this->class->usage_mask & new_mask)) {
2132 this->class->usage_mask |= new_mask;
2134 if (!save_trace(this->class->usage_traces + new_bit))
2138 case LOCK_USED_IN_HARDIRQ:
2139 case LOCK_USED_IN_SOFTIRQ:
2140 case LOCK_USED_IN_HARDIRQ_READ:
2141 case LOCK_USED_IN_SOFTIRQ_READ:
2142 case LOCK_ENABLED_HARDIRQS:
2143 case LOCK_ENABLED_SOFTIRQS:
2144 case LOCK_ENABLED_HARDIRQS_READ:
2145 case LOCK_ENABLED_SOFTIRQS_READ:
2146 ret = mark_lock_irq(curr, this, new_bit);
2152 * Add it to the global list of classes:
2154 list_add_tail_rcu(&this->class->lock_entry, &all_lock_classes);
2155 debug_atomic_dec(&nr_unused_locks);
2158 if (!debug_locks_off_graph_unlock())
2167 * We must printk outside of the graph_lock:
2170 printk("\nmarked lock as {%s}:\n", usage_str[new_bit]);
2172 print_irqtrace_events(curr);
2180 * Initialize a lock instance's lock-class mapping info:
2182 void lockdep_init_map(struct lockdep_map *lock, const char *name,
2183 struct lock_class_key *key, int subclass)
2185 if (unlikely(!debug_locks))
2188 if (DEBUG_LOCKS_WARN_ON(!key))
2190 if (DEBUG_LOCKS_WARN_ON(!name))
2193 * Sanity check, the lock-class key must be persistent:
2195 if (!static_obj(key)) {
2196 printk("BUG: key %p not in .data!\n", key);
2197 DEBUG_LOCKS_WARN_ON(1);
2202 lock->class_cache = NULL;
2204 register_lock_class(lock, subclass, 1);
2207 EXPORT_SYMBOL_GPL(lockdep_init_map);
2210 * This gets called for every mutex_lock*()/spin_lock*() operation.
2211 * We maintain the dependency maps and validate the locking attempt:
2213 static int __lock_acquire(struct lockdep_map *lock, unsigned int subclass,
2214 int trylock, int read, int check, int hardirqs_off,
2217 struct task_struct *curr = current;
2218 struct lock_class *class = NULL;
2219 struct held_lock *hlock;
2220 unsigned int depth, id;
2224 if (unlikely(!debug_locks))
2227 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2230 if (unlikely(subclass >= MAX_LOCKDEP_SUBCLASSES)) {
2232 printk("BUG: MAX_LOCKDEP_SUBCLASSES too low!\n");
2233 printk("turning off the locking correctness validator.\n");
2238 class = lock->class_cache;
2240 * Not cached yet or subclass?
2242 if (unlikely(!class)) {
2243 class = register_lock_class(lock, subclass, 0);
2247 debug_atomic_inc((atomic_t *)&class->ops);
2248 if (very_verbose(class)) {
2249 printk("\nacquire class [%p] %s", class->key, class->name);
2250 if (class->name_version > 1)
2251 printk("#%d", class->name_version);
2257 * Add the lock to the list of currently held locks.
2258 * (we dont increase the depth just yet, up until the
2259 * dependency checks are done)
2261 depth = curr->lockdep_depth;
2262 if (DEBUG_LOCKS_WARN_ON(depth >= MAX_LOCK_DEPTH))
2265 hlock = curr->held_locks + depth;
2267 hlock->class = class;
2268 hlock->acquire_ip = ip;
2269 hlock->instance = lock;
2270 hlock->trylock = trylock;
2272 hlock->check = check;
2273 hlock->hardirqs_off = hardirqs_off;
2275 if (check == 2 && !mark_irqflags(curr, hlock))
2278 /* mark it as used: */
2279 if (!mark_lock(curr, hlock, LOCK_USED))
2283 * Calculate the chain hash: it's the combined has of all the
2284 * lock keys along the dependency chain. We save the hash value
2285 * at every step so that we can get the current hash easily
2286 * after unlock. The chain hash is then used to cache dependency
2289 * The 'key ID' is what is the most compact key value to drive
2290 * the hash, not class->key.
2292 id = class - lock_classes;
2293 if (DEBUG_LOCKS_WARN_ON(id >= MAX_LOCKDEP_KEYS))
2296 chain_key = curr->curr_chain_key;
2298 if (DEBUG_LOCKS_WARN_ON(chain_key != 0))
2303 hlock->prev_chain_key = chain_key;
2304 if (separate_irq_context(curr, hlock)) {
2308 chain_key = iterate_chain_key(chain_key, id);
2309 curr->curr_chain_key = chain_key;
2311 if (!validate_chain(curr, lock, hlock, chain_head))
2314 curr->lockdep_depth++;
2315 check_chain_key(curr);
2316 #ifdef CONFIG_DEBUG_LOCKDEP
2317 if (unlikely(!debug_locks))
2320 if (unlikely(curr->lockdep_depth >= MAX_LOCK_DEPTH)) {
2322 printk("BUG: MAX_LOCK_DEPTH too low!\n");
2323 printk("turning off the locking correctness validator.\n");
2327 if (unlikely(curr->lockdep_depth > max_lockdep_depth))
2328 max_lockdep_depth = curr->lockdep_depth;
2334 print_unlock_inbalance_bug(struct task_struct *curr, struct lockdep_map *lock,
2337 if (!debug_locks_off())
2339 if (debug_locks_silent)
2342 printk("\n=====================================\n");
2343 printk( "[ BUG: bad unlock balance detected! ]\n");
2344 printk( "-------------------------------------\n");
2345 printk("%s/%d is trying to release lock (",
2346 curr->comm, curr->pid);
2347 print_lockdep_cache(lock);
2350 printk("but there are no more locks to release!\n");
2351 printk("\nother info that might help us debug this:\n");
2352 lockdep_print_held_locks(curr);
2354 printk("\nstack backtrace:\n");
2361 * Common debugging checks for both nested and non-nested unlock:
2363 static int check_unlock(struct task_struct *curr, struct lockdep_map *lock,
2366 if (unlikely(!debug_locks))
2368 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2371 if (curr->lockdep_depth <= 0)
2372 return print_unlock_inbalance_bug(curr, lock, ip);
2378 * Remove the lock to the list of currently held locks in a
2379 * potentially non-nested (out of order) manner. This is a
2380 * relatively rare operation, as all the unlock APIs default
2381 * to nested mode (which uses lock_release()):
2384 lock_release_non_nested(struct task_struct *curr,
2385 struct lockdep_map *lock, unsigned long ip)
2387 struct held_lock *hlock, *prev_hlock;
2392 * Check whether the lock exists in the current stack
2395 depth = curr->lockdep_depth;
2396 if (DEBUG_LOCKS_WARN_ON(!depth))
2400 for (i = depth-1; i >= 0; i--) {
2401 hlock = curr->held_locks + i;
2403 * We must not cross into another context:
2405 if (prev_hlock && prev_hlock->irq_context != hlock->irq_context)
2407 if (hlock->instance == lock)
2411 return print_unlock_inbalance_bug(curr, lock, ip);
2415 * We have the right lock to unlock, 'hlock' points to it.
2416 * Now we remove it from the stack, and add back the other
2417 * entries (if any), recalculating the hash along the way:
2419 curr->lockdep_depth = i;
2420 curr->curr_chain_key = hlock->prev_chain_key;
2422 for (i++; i < depth; i++) {
2423 hlock = curr->held_locks + i;
2424 if (!__lock_acquire(hlock->instance,
2425 hlock->class->subclass, hlock->trylock,
2426 hlock->read, hlock->check, hlock->hardirqs_off,
2431 if (DEBUG_LOCKS_WARN_ON(curr->lockdep_depth != depth - 1))
2437 * Remove the lock to the list of currently held locks - this gets
2438 * called on mutex_unlock()/spin_unlock*() (or on a failed
2439 * mutex_lock_interruptible()). This is done for unlocks that nest
2440 * perfectly. (i.e. the current top of the lock-stack is unlocked)
2442 static int lock_release_nested(struct task_struct *curr,
2443 struct lockdep_map *lock, unsigned long ip)
2445 struct held_lock *hlock;
2449 * Pop off the top of the lock stack:
2451 depth = curr->lockdep_depth - 1;
2452 hlock = curr->held_locks + depth;
2455 * Is the unlock non-nested:
2457 if (hlock->instance != lock)
2458 return lock_release_non_nested(curr, lock, ip);
2459 curr->lockdep_depth--;
2461 if (DEBUG_LOCKS_WARN_ON(!depth && (hlock->prev_chain_key != 0)))
2464 curr->curr_chain_key = hlock->prev_chain_key;
2466 #ifdef CONFIG_DEBUG_LOCKDEP
2467 hlock->prev_chain_key = 0;
2468 hlock->class = NULL;
2469 hlock->acquire_ip = 0;
2470 hlock->irq_context = 0;
2476 * Remove the lock to the list of currently held locks - this gets
2477 * called on mutex_unlock()/spin_unlock*() (or on a failed
2478 * mutex_lock_interruptible()). This is done for unlocks that nest
2479 * perfectly. (i.e. the current top of the lock-stack is unlocked)
2482 __lock_release(struct lockdep_map *lock, int nested, unsigned long ip)
2484 struct task_struct *curr = current;
2486 if (!check_unlock(curr, lock, ip))
2490 if (!lock_release_nested(curr, lock, ip))
2493 if (!lock_release_non_nested(curr, lock, ip))
2497 check_chain_key(curr);
2501 * Check whether we follow the irq-flags state precisely:
2503 static void check_flags(unsigned long flags)
2505 #if defined(CONFIG_DEBUG_LOCKDEP) && defined(CONFIG_TRACE_IRQFLAGS)
2509 if (irqs_disabled_flags(flags))
2510 DEBUG_LOCKS_WARN_ON(current->hardirqs_enabled);
2512 DEBUG_LOCKS_WARN_ON(!current->hardirqs_enabled);
2515 * We dont accurately track softirq state in e.g.
2516 * hardirq contexts (such as on 4KSTACKS), so only
2517 * check if not in hardirq contexts:
2519 if (!hardirq_count()) {
2520 if (softirq_count())
2521 DEBUG_LOCKS_WARN_ON(current->softirqs_enabled);
2523 DEBUG_LOCKS_WARN_ON(!current->softirqs_enabled);
2527 print_irqtrace_events(current);
2532 * We are not always called with irqs disabled - do that here,
2533 * and also avoid lockdep recursion:
2535 void lock_acquire(struct lockdep_map *lock, unsigned int subclass,
2536 int trylock, int read, int check, unsigned long ip)
2538 unsigned long flags;
2540 if (unlikely(current->lockdep_recursion))
2543 raw_local_irq_save(flags);
2546 current->lockdep_recursion = 1;
2547 __lock_acquire(lock, subclass, trylock, read, check,
2548 irqs_disabled_flags(flags), ip);
2549 current->lockdep_recursion = 0;
2550 raw_local_irq_restore(flags);
2553 EXPORT_SYMBOL_GPL(lock_acquire);
2555 void lock_release(struct lockdep_map *lock, int nested, unsigned long ip)
2557 unsigned long flags;
2559 if (unlikely(current->lockdep_recursion))
2562 raw_local_irq_save(flags);
2564 current->lockdep_recursion = 1;
2565 __lock_release(lock, nested, ip);
2566 current->lockdep_recursion = 0;
2567 raw_local_irq_restore(flags);
2570 EXPORT_SYMBOL_GPL(lock_release);
2573 * Used by the testsuite, sanitize the validator state
2574 * after a simulated failure:
2577 void lockdep_reset(void)
2579 unsigned long flags;
2582 raw_local_irq_save(flags);
2583 current->curr_chain_key = 0;
2584 current->lockdep_depth = 0;
2585 current->lockdep_recursion = 0;
2586 memset(current->held_locks, 0, MAX_LOCK_DEPTH*sizeof(struct held_lock));
2587 nr_hardirq_chains = 0;
2588 nr_softirq_chains = 0;
2589 nr_process_chains = 0;
2591 for (i = 0; i < CHAINHASH_SIZE; i++)
2592 INIT_LIST_HEAD(chainhash_table + i);
2593 raw_local_irq_restore(flags);
2596 static void zap_class(struct lock_class *class)
2601 * Remove all dependencies this lock is
2604 for (i = 0; i < nr_list_entries; i++) {
2605 if (list_entries[i].class == class)
2606 list_del_rcu(&list_entries[i].entry);
2609 * Unhash the class and remove it from the all_lock_classes list:
2611 list_del_rcu(&class->hash_entry);
2612 list_del_rcu(&class->lock_entry);
2616 static inline int within(void *addr, void *start, unsigned long size)
2618 return addr >= start && addr < start + size;
2621 void lockdep_free_key_range(void *start, unsigned long size)
2623 struct lock_class *class, *next;
2624 struct list_head *head;
2625 unsigned long flags;
2628 raw_local_irq_save(flags);
2632 * Unhash all classes that were created by this module:
2634 for (i = 0; i < CLASSHASH_SIZE; i++) {
2635 head = classhash_table + i;
2636 if (list_empty(head))
2638 list_for_each_entry_safe(class, next, head, hash_entry)
2639 if (within(class->key, start, size))
2644 raw_local_irq_restore(flags);
2647 void lockdep_reset_lock(struct lockdep_map *lock)
2649 struct lock_class *class, *next;
2650 struct list_head *head;
2651 unsigned long flags;
2654 raw_local_irq_save(flags);
2657 * Remove all classes this lock might have:
2659 for (j = 0; j < MAX_LOCKDEP_SUBCLASSES; j++) {
2661 * If the class exists we look it up and zap it:
2663 class = look_up_lock_class(lock, j);
2668 * Debug check: in the end all mapped classes should
2672 for (i = 0; i < CLASSHASH_SIZE; i++) {
2673 head = classhash_table + i;
2674 if (list_empty(head))
2676 list_for_each_entry_safe(class, next, head, hash_entry) {
2677 if (unlikely(class == lock->class_cache)) {
2678 if (debug_locks_off_graph_unlock())
2687 raw_local_irq_restore(flags);
2690 void lockdep_init(void)
2695 * Some architectures have their own start_kernel()
2696 * code which calls lockdep_init(), while we also
2697 * call lockdep_init() from the start_kernel() itself,
2698 * and we want to initialize the hashes only once:
2700 if (lockdep_initialized)
2703 for (i = 0; i < CLASSHASH_SIZE; i++)
2704 INIT_LIST_HEAD(classhash_table + i);
2706 for (i = 0; i < CHAINHASH_SIZE; i++)
2707 INIT_LIST_HEAD(chainhash_table + i);
2709 lockdep_initialized = 1;
2712 void __init lockdep_info(void)
2714 printk("Lock dependency validator: Copyright (c) 2006 Red Hat, Inc., Ingo Molnar\n");
2716 printk("... MAX_LOCKDEP_SUBCLASSES: %lu\n", MAX_LOCKDEP_SUBCLASSES);
2717 printk("... MAX_LOCK_DEPTH: %lu\n", MAX_LOCK_DEPTH);
2718 printk("... MAX_LOCKDEP_KEYS: %lu\n", MAX_LOCKDEP_KEYS);
2719 printk("... CLASSHASH_SIZE: %lu\n", CLASSHASH_SIZE);
2720 printk("... MAX_LOCKDEP_ENTRIES: %lu\n", MAX_LOCKDEP_ENTRIES);
2721 printk("... MAX_LOCKDEP_CHAINS: %lu\n", MAX_LOCKDEP_CHAINS);
2722 printk("... CHAINHASH_SIZE: %lu\n", CHAINHASH_SIZE);
2724 printk(" memory used by lock dependency info: %lu kB\n",
2725 (sizeof(struct lock_class) * MAX_LOCKDEP_KEYS +
2726 sizeof(struct list_head) * CLASSHASH_SIZE +
2727 sizeof(struct lock_list) * MAX_LOCKDEP_ENTRIES +
2728 sizeof(struct lock_chain) * MAX_LOCKDEP_CHAINS +
2729 sizeof(struct list_head) * CHAINHASH_SIZE) / 1024);
2731 printk(" per task-struct memory footprint: %lu bytes\n",
2732 sizeof(struct held_lock) * MAX_LOCK_DEPTH);
2734 #ifdef CONFIG_DEBUG_LOCKDEP
2735 if (lockdep_init_error)
2736 printk("WARNING: lockdep init error! Arch code didnt call lockdep_init() early enough?\n");
2740 static inline int in_range(const void *start, const void *addr, const void *end)
2742 return addr >= start && addr <= end;
2746 print_freed_lock_bug(struct task_struct *curr, const void *mem_from,
2747 const void *mem_to, struct held_lock *hlock)
2749 if (!debug_locks_off())
2751 if (debug_locks_silent)
2754 printk("\n=========================\n");
2755 printk( "[ BUG: held lock freed! ]\n");
2756 printk( "-------------------------\n");
2757 printk("%s/%d is freeing memory %p-%p, with a lock still held there!\n",
2758 curr->comm, curr->pid, mem_from, mem_to-1);
2760 lockdep_print_held_locks(curr);
2762 printk("\nstack backtrace:\n");
2767 * Called when kernel memory is freed (or unmapped), or if a lock
2768 * is destroyed or reinitialized - this code checks whether there is
2769 * any held lock in the memory range of <from> to <to>:
2771 void debug_check_no_locks_freed(const void *mem_from, unsigned long mem_len)
2773 const void *mem_to = mem_from + mem_len, *lock_from, *lock_to;
2774 struct task_struct *curr = current;
2775 struct held_lock *hlock;
2776 unsigned long flags;
2779 if (unlikely(!debug_locks))
2782 local_irq_save(flags);
2783 for (i = 0; i < curr->lockdep_depth; i++) {
2784 hlock = curr->held_locks + i;
2786 lock_from = (void *)hlock->instance;
2787 lock_to = (void *)(hlock->instance + 1);
2789 if (!in_range(mem_from, lock_from, mem_to) &&
2790 !in_range(mem_from, lock_to, mem_to))
2793 print_freed_lock_bug(curr, mem_from, mem_to, hlock);
2796 local_irq_restore(flags);
2798 EXPORT_SYMBOL_GPL(debug_check_no_locks_freed);
2800 static void print_held_locks_bug(struct task_struct *curr)
2802 if (!debug_locks_off())
2804 if (debug_locks_silent)
2807 printk("\n=====================================\n");
2808 printk( "[ BUG: lock held at task exit time! ]\n");
2809 printk( "-------------------------------------\n");
2810 printk("%s/%d is exiting with locks still held!\n",
2811 curr->comm, curr->pid);
2812 lockdep_print_held_locks(curr);
2814 printk("\nstack backtrace:\n");
2818 void debug_check_no_locks_held(struct task_struct *task)
2820 if (unlikely(task->lockdep_depth > 0))
2821 print_held_locks_bug(task);
2824 void debug_show_all_locks(void)
2826 struct task_struct *g, *p;
2830 if (unlikely(!debug_locks)) {
2831 printk("INFO: lockdep is turned off.\n");
2834 printk("\nShowing all locks held in the system:\n");
2837 * Here we try to get the tasklist_lock as hard as possible,
2838 * if not successful after 2 seconds we ignore it (but keep
2839 * trying). This is to enable a debug printout even if a
2840 * tasklist_lock-holding task deadlocks or crashes.
2843 if (!read_trylock(&tasklist_lock)) {
2845 printk("hm, tasklist_lock locked, retrying... ");
2848 printk(" #%d", 10-count);
2852 printk(" ignoring it.\n");
2856 printk(" locked it.\n");
2858 do_each_thread(g, p) {
2859 if (p->lockdep_depth)
2860 lockdep_print_held_locks(p);
2862 if (read_trylock(&tasklist_lock))
2864 } while_each_thread(g, p);
2867 printk("=============================================\n\n");
2870 read_unlock(&tasklist_lock);
2873 EXPORT_SYMBOL_GPL(debug_show_all_locks);
2875 void debug_show_held_locks(struct task_struct *task)
2877 if (unlikely(!debug_locks)) {
2878 printk("INFO: lockdep is turned off.\n");
2881 lockdep_print_held_locks(task);
2884 EXPORT_SYMBOL_GPL(debug_show_held_locks);