[PATCH] shmem and simple const super_operations
[linux-2.6.22.y-op.git] / kernel / lockdep.c
blob8dc24c92dc6d2c1e867c8ce381caec43c9408583
1 /*
2 * kernel/lockdep.c
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
62 * dropped already)
64 if (!debug_locks) {
65 __raw_spin_unlock(&lockdep_lock);
66 return 0;
68 return 1;
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);
77 return 0;
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);
90 return ret;
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())
106 return NULL;
108 printk("BUG: MAX_LOCKDEP_ENTRIES too low!\n");
109 printk("turning off the locking correctness validator.\n");
110 return NULL;
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
147 * their existence:
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
162 * unique.
164 #define iterate_chain_key(key1, key2) \
165 (((key1) << MAX_LOCKDEP_KEYS_BITS) ^ \
166 ((key1) >> (64-MAX_LOCKDEP_KEYS_BITS)) ^ \
167 (key2))
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:
187 #define VERBOSE 0
188 #define VERY_VERBOSE 0
190 #if VERBOSE
191 # define HARDIRQ_VERBOSE 1
192 # define SOFTIRQ_VERBOSE 1
193 #else
194 # define HARDIRQ_VERBOSE 0
195 # define SOFTIRQ_VERBOSE 0
196 #endif
198 #if VERBOSE || HARDIRQ_VERBOSE || SOFTIRQ_VERBOSE
200 * Quick filtering for interesting events:
202 static int class_filter(struct lock_class *class)
204 #if 0
205 /* Example */
206 if (class->name_version == 1 &&
207 !strcmp(class->name, "lockname"))
208 return 1;
209 if (class->name_version == 1 &&
210 !strcmp(class->name, "&struct->lockfield"))
211 return 1;
212 #endif
213 /* Filter everything else. 1 would be to allow everything else */
214 return 0;
216 #endif
218 static int verbose(struct lock_class *class)
220 #if VERBOSE
221 return class_filter(class);
222 #endif
223 return 0;
226 #ifdef CONFIG_TRACE_IRQFLAGS
228 static int hardirq_verbose(struct lock_class *class)
230 #if HARDIRQ_VERBOSE
231 return class_filter(class);
232 #endif
233 return 0;
236 static int softirq_verbose(struct lock_class *class)
238 #if SOFTIRQ_VERBOSE
239 return class_filter(class);
240 #endif
241 return 0;
244 #endif
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;
259 trace->skip = 3;
260 trace->all_contexts = 0;
262 save_stack_trace(trace, NULL);
264 trace->max_entries = trace->nr_entries;
266 nr_stack_trace_entries += trace->nr_entries;
268 if (nr_stack_trace_entries == MAX_STACK_TRACE_ENTRIES) {
269 if (!debug_locks_off_graph_unlock())
270 return 0;
272 printk("BUG: MAX_STACK_TRACE_ENTRIES too low!\n");
273 printk("turning off the locking correctness validator.\n");
274 dump_stack();
276 return 0;
279 return 1;
282 unsigned int nr_hardirq_chains;
283 unsigned int nr_softirq_chains;
284 unsigned int nr_process_chains;
285 unsigned int max_lockdep_depth;
286 unsigned int max_recursion_depth;
288 #ifdef CONFIG_DEBUG_LOCKDEP
290 * We cannot printk in early bootup code. Not even early_printk()
291 * might work. So we mark any initialization errors and printk
292 * about it later on, in lockdep_info().
294 static int lockdep_init_error;
297 * Various lockdep statistics:
299 atomic_t chain_lookup_hits;
300 atomic_t chain_lookup_misses;
301 atomic_t hardirqs_on_events;
302 atomic_t hardirqs_off_events;
303 atomic_t redundant_hardirqs_on;
304 atomic_t redundant_hardirqs_off;
305 atomic_t softirqs_on_events;
306 atomic_t softirqs_off_events;
307 atomic_t redundant_softirqs_on;
308 atomic_t redundant_softirqs_off;
309 atomic_t nr_unused_locks;
310 atomic_t nr_cyclic_checks;
311 atomic_t nr_cyclic_check_recursions;
312 atomic_t nr_find_usage_forwards_checks;
313 atomic_t nr_find_usage_forwards_recursions;
314 atomic_t nr_find_usage_backwards_checks;
315 atomic_t nr_find_usage_backwards_recursions;
316 # define debug_atomic_inc(ptr) atomic_inc(ptr)
317 # define debug_atomic_dec(ptr) atomic_dec(ptr)
318 # define debug_atomic_read(ptr) atomic_read(ptr)
319 #else
320 # define debug_atomic_inc(ptr) do { } while (0)
321 # define debug_atomic_dec(ptr) do { } while (0)
322 # define debug_atomic_read(ptr) 0
323 #endif
326 * Locking printouts:
329 static const char *usage_str[] =
331 [LOCK_USED] = "initial-use ",
332 [LOCK_USED_IN_HARDIRQ] = "in-hardirq-W",
333 [LOCK_USED_IN_SOFTIRQ] = "in-softirq-W",
334 [LOCK_ENABLED_SOFTIRQS] = "softirq-on-W",
335 [LOCK_ENABLED_HARDIRQS] = "hardirq-on-W",
336 [LOCK_USED_IN_HARDIRQ_READ] = "in-hardirq-R",
337 [LOCK_USED_IN_SOFTIRQ_READ] = "in-softirq-R",
338 [LOCK_ENABLED_SOFTIRQS_READ] = "softirq-on-R",
339 [LOCK_ENABLED_HARDIRQS_READ] = "hardirq-on-R",
342 const char * __get_key_name(struct lockdep_subclass_key *key, char *str)
344 unsigned long offs, size;
345 char *modname;
347 return kallsyms_lookup((unsigned long)key, &size, &offs, &modname, str);
350 void
351 get_usage_chars(struct lock_class *class, char *c1, char *c2, char *c3, char *c4)
353 *c1 = '.', *c2 = '.', *c3 = '.', *c4 = '.';
355 if (class->usage_mask & LOCKF_USED_IN_HARDIRQ)
356 *c1 = '+';
357 else
358 if (class->usage_mask & LOCKF_ENABLED_HARDIRQS)
359 *c1 = '-';
361 if (class->usage_mask & LOCKF_USED_IN_SOFTIRQ)
362 *c2 = '+';
363 else
364 if (class->usage_mask & LOCKF_ENABLED_SOFTIRQS)
365 *c2 = '-';
367 if (class->usage_mask & LOCKF_ENABLED_HARDIRQS_READ)
368 *c3 = '-';
369 if (class->usage_mask & LOCKF_USED_IN_HARDIRQ_READ) {
370 *c3 = '+';
371 if (class->usage_mask & LOCKF_ENABLED_HARDIRQS_READ)
372 *c3 = '?';
375 if (class->usage_mask & LOCKF_ENABLED_SOFTIRQS_READ)
376 *c4 = '-';
377 if (class->usage_mask & LOCKF_USED_IN_SOFTIRQ_READ) {
378 *c4 = '+';
379 if (class->usage_mask & LOCKF_ENABLED_SOFTIRQS_READ)
380 *c4 = '?';
384 static void print_lock_name(struct lock_class *class)
386 char str[KSYM_NAME_LEN + 1], c1, c2, c3, c4;
387 const char *name;
389 get_usage_chars(class, &c1, &c2, &c3, &c4);
391 name = class->name;
392 if (!name) {
393 name = __get_key_name(class->key, str);
394 printk(" (%s", name);
395 } else {
396 printk(" (%s", name);
397 if (class->name_version > 1)
398 printk("#%d", class->name_version);
399 if (class->subclass)
400 printk("/%d", class->subclass);
402 printk("){%c%c%c%c}", c1, c2, c3, c4);
405 static void print_lockdep_cache(struct lockdep_map *lock)
407 const char *name;
408 char str[KSYM_NAME_LEN + 1];
410 name = lock->name;
411 if (!name)
412 name = __get_key_name(lock->key->subkeys, str);
414 printk("%s", name);
417 static void print_lock(struct held_lock *hlock)
419 print_lock_name(hlock->class);
420 printk(", at: ");
421 print_ip_sym(hlock->acquire_ip);
424 static void lockdep_print_held_locks(struct task_struct *curr)
426 int i, depth = curr->lockdep_depth;
428 if (!depth) {
429 printk("no locks held by %s/%d.\n", curr->comm, curr->pid);
430 return;
432 printk("%d lock%s held by %s/%d:\n",
433 depth, depth > 1 ? "s" : "", curr->comm, curr->pid);
435 for (i = 0; i < depth; i++) {
436 printk(" #%d: ", i);
437 print_lock(curr->held_locks + i);
441 static void print_lock_class_header(struct lock_class *class, int depth)
443 int bit;
445 printk("%*s->", depth, "");
446 print_lock_name(class);
447 printk(" ops: %lu", class->ops);
448 printk(" {\n");
450 for (bit = 0; bit < LOCK_USAGE_STATES; bit++) {
451 if (class->usage_mask & (1 << bit)) {
452 int len = depth;
454 len += printk("%*s %s", depth, "", usage_str[bit]);
455 len += printk(" at:\n");
456 print_stack_trace(class->usage_traces + bit, len);
459 printk("%*s }\n", depth, "");
461 printk("%*s ... key at: ",depth,"");
462 print_ip_sym((unsigned long)class->key);
466 * printk all lock dependencies starting at <entry>:
468 static void print_lock_dependencies(struct lock_class *class, int depth)
470 struct lock_list *entry;
472 if (DEBUG_LOCKS_WARN_ON(depth >= 20))
473 return;
475 print_lock_class_header(class, depth);
477 list_for_each_entry(entry, &class->locks_after, entry) {
478 if (DEBUG_LOCKS_WARN_ON(!entry->class))
479 return;
481 print_lock_dependencies(entry->class, depth + 1);
483 printk("%*s ... acquired at:\n",depth,"");
484 print_stack_trace(&entry->trace, 2);
485 printk("\n");
490 * Add a new dependency to the head of the list:
492 static int add_lock_to_list(struct lock_class *class, struct lock_class *this,
493 struct list_head *head, unsigned long ip, int distance)
495 struct lock_list *entry;
497 * Lock not present yet - get a new dependency struct and
498 * add it to the list:
500 entry = alloc_list_entry();
501 if (!entry)
502 return 0;
504 entry->class = this;
505 entry->distance = distance;
506 if (!save_trace(&entry->trace))
507 return 0;
510 * Since we never remove from the dependency list, the list can
511 * be walked lockless by other CPUs, it's only allocation
512 * that must be protected by the spinlock. But this also means
513 * we must make new entries visible only once writes to the
514 * entry become visible - hence the RCU op:
516 list_add_tail_rcu(&entry->entry, head);
518 return 1;
522 * Recursive, forwards-direction lock-dependency checking, used for
523 * both noncyclic checking and for hardirq-unsafe/softirq-unsafe
524 * checking.
526 * (to keep the stackframe of the recursive functions small we
527 * use these global variables, and we also mark various helper
528 * functions as noinline.)
530 static struct held_lock *check_source, *check_target;
533 * Print a dependency chain entry (this is only done when a deadlock
534 * has been detected):
536 static noinline int
537 print_circular_bug_entry(struct lock_list *target, unsigned int depth)
539 if (debug_locks_silent)
540 return 0;
541 printk("\n-> #%u", depth);
542 print_lock_name(target->class);
543 printk(":\n");
544 print_stack_trace(&target->trace, 6);
546 return 0;
549 static void print_kernel_version(void)
551 printk("%s %.*s\n", init_utsname()->release,
552 (int)strcspn(init_utsname()->version, " "),
553 init_utsname()->version);
557 * When a circular dependency is detected, print the
558 * header first:
560 static noinline int
561 print_circular_bug_header(struct lock_list *entry, unsigned int depth)
563 struct task_struct *curr = current;
565 if (!debug_locks_off_graph_unlock() || debug_locks_silent)
566 return 0;
568 printk("\n=======================================================\n");
569 printk( "[ INFO: possible circular locking dependency detected ]\n");
570 print_kernel_version();
571 printk( "-------------------------------------------------------\n");
572 printk("%s/%d is trying to acquire lock:\n",
573 curr->comm, curr->pid);
574 print_lock(check_source);
575 printk("\nbut task is already holding lock:\n");
576 print_lock(check_target);
577 printk("\nwhich lock already depends on the new lock.\n\n");
578 printk("\nthe existing dependency chain (in reverse order) is:\n");
580 print_circular_bug_entry(entry, depth);
582 return 0;
585 static noinline int print_circular_bug_tail(void)
587 struct task_struct *curr = current;
588 struct lock_list this;
590 if (debug_locks_silent)
591 return 0;
593 this.class = check_source->class;
594 if (!save_trace(&this.trace))
595 return 0;
597 print_circular_bug_entry(&this, 0);
599 printk("\nother info that might help us debug this:\n\n");
600 lockdep_print_held_locks(curr);
602 printk("\nstack backtrace:\n");
603 dump_stack();
605 return 0;
608 #define RECURSION_LIMIT 40
610 static int noinline print_infinite_recursion_bug(void)
612 if (!debug_locks_off_graph_unlock())
613 return 0;
615 WARN_ON(1);
617 return 0;
621 * Prove that the dependency graph starting at <entry> can not
622 * lead to <target>. Print an error and return 0 if it does.
624 static noinline int
625 check_noncircular(struct lock_class *source, unsigned int depth)
627 struct lock_list *entry;
629 debug_atomic_inc(&nr_cyclic_check_recursions);
630 if (depth > max_recursion_depth)
631 max_recursion_depth = depth;
632 if (depth >= RECURSION_LIMIT)
633 return print_infinite_recursion_bug();
635 * Check this lock's dependency list:
637 list_for_each_entry(entry, &source->locks_after, entry) {
638 if (entry->class == check_target->class)
639 return print_circular_bug_header(entry, depth+1);
640 debug_atomic_inc(&nr_cyclic_checks);
641 if (!check_noncircular(entry->class, depth+1))
642 return print_circular_bug_entry(entry, depth+1);
644 return 1;
647 static int very_verbose(struct lock_class *class)
649 #if VERY_VERBOSE
650 return class_filter(class);
651 #endif
652 return 0;
654 #ifdef CONFIG_TRACE_IRQFLAGS
657 * Forwards and backwards subgraph searching, for the purposes of
658 * proving that two subgraphs can be connected by a new dependency
659 * without creating any illegal irq-safe -> irq-unsafe lock dependency.
661 static enum lock_usage_bit find_usage_bit;
662 static struct lock_class *forwards_match, *backwards_match;
665 * Find a node in the forwards-direction dependency sub-graph starting
666 * at <source> that matches <find_usage_bit>.
668 * Return 2 if such a node exists in the subgraph, and put that node
669 * into <forwards_match>.
671 * Return 1 otherwise and keep <forwards_match> unchanged.
672 * Return 0 on error.
674 static noinline int
675 find_usage_forwards(struct lock_class *source, unsigned int depth)
677 struct lock_list *entry;
678 int ret;
680 if (depth > max_recursion_depth)
681 max_recursion_depth = depth;
682 if (depth >= RECURSION_LIMIT)
683 return print_infinite_recursion_bug();
685 debug_atomic_inc(&nr_find_usage_forwards_checks);
686 if (source->usage_mask & (1 << find_usage_bit)) {
687 forwards_match = source;
688 return 2;
692 * Check this lock's dependency list:
694 list_for_each_entry(entry, &source->locks_after, entry) {
695 debug_atomic_inc(&nr_find_usage_forwards_recursions);
696 ret = find_usage_forwards(entry->class, depth+1);
697 if (ret == 2 || ret == 0)
698 return ret;
700 return 1;
704 * Find a node in the backwards-direction dependency sub-graph starting
705 * at <source> that matches <find_usage_bit>.
707 * Return 2 if such a node exists in the subgraph, and put that node
708 * into <backwards_match>.
710 * Return 1 otherwise and keep <backwards_match> unchanged.
711 * Return 0 on error.
713 static noinline int
714 find_usage_backwards(struct lock_class *source, unsigned int depth)
716 struct lock_list *entry;
717 int ret;
719 if (!__raw_spin_is_locked(&lockdep_lock))
720 return DEBUG_LOCKS_WARN_ON(1);
722 if (depth > max_recursion_depth)
723 max_recursion_depth = depth;
724 if (depth >= RECURSION_LIMIT)
725 return print_infinite_recursion_bug();
727 debug_atomic_inc(&nr_find_usage_backwards_checks);
728 if (source->usage_mask & (1 << find_usage_bit)) {
729 backwards_match = source;
730 return 2;
734 * Check this lock's dependency list:
736 list_for_each_entry(entry, &source->locks_before, entry) {
737 debug_atomic_inc(&nr_find_usage_backwards_recursions);
738 ret = find_usage_backwards(entry->class, depth+1);
739 if (ret == 2 || ret == 0)
740 return ret;
742 return 1;
745 static int
746 print_bad_irq_dependency(struct task_struct *curr,
747 struct held_lock *prev,
748 struct held_lock *next,
749 enum lock_usage_bit bit1,
750 enum lock_usage_bit bit2,
751 const char *irqclass)
753 if (!debug_locks_off_graph_unlock() || debug_locks_silent)
754 return 0;
756 printk("\n======================================================\n");
757 printk( "[ INFO: %s-safe -> %s-unsafe lock order detected ]\n",
758 irqclass, irqclass);
759 print_kernel_version();
760 printk( "------------------------------------------------------\n");
761 printk("%s/%d [HC%u[%lu]:SC%u[%lu]:HE%u:SE%u] is trying to acquire:\n",
762 curr->comm, curr->pid,
763 curr->hardirq_context, hardirq_count() >> HARDIRQ_SHIFT,
764 curr->softirq_context, softirq_count() >> SOFTIRQ_SHIFT,
765 curr->hardirqs_enabled,
766 curr->softirqs_enabled);
767 print_lock(next);
769 printk("\nand this task is already holding:\n");
770 print_lock(prev);
771 printk("which would create a new lock dependency:\n");
772 print_lock_name(prev->class);
773 printk(" ->");
774 print_lock_name(next->class);
775 printk("\n");
777 printk("\nbut this new dependency connects a %s-irq-safe lock:\n",
778 irqclass);
779 print_lock_name(backwards_match);
780 printk("\n... which became %s-irq-safe at:\n", irqclass);
782 print_stack_trace(backwards_match->usage_traces + bit1, 1);
784 printk("\nto a %s-irq-unsafe lock:\n", irqclass);
785 print_lock_name(forwards_match);
786 printk("\n... which became %s-irq-unsafe at:\n", irqclass);
787 printk("...");
789 print_stack_trace(forwards_match->usage_traces + bit2, 1);
791 printk("\nother info that might help us debug this:\n\n");
792 lockdep_print_held_locks(curr);
794 printk("\nthe %s-irq-safe lock's dependencies:\n", irqclass);
795 print_lock_dependencies(backwards_match, 0);
797 printk("\nthe %s-irq-unsafe lock's dependencies:\n", irqclass);
798 print_lock_dependencies(forwards_match, 0);
800 printk("\nstack backtrace:\n");
801 dump_stack();
803 return 0;
806 static int
807 check_usage(struct task_struct *curr, struct held_lock *prev,
808 struct held_lock *next, enum lock_usage_bit bit_backwards,
809 enum lock_usage_bit bit_forwards, const char *irqclass)
811 int ret;
813 find_usage_bit = bit_backwards;
814 /* fills in <backwards_match> */
815 ret = find_usage_backwards(prev->class, 0);
816 if (!ret || ret == 1)
817 return ret;
819 find_usage_bit = bit_forwards;
820 ret = find_usage_forwards(next->class, 0);
821 if (!ret || ret == 1)
822 return ret;
823 /* ret == 2 */
824 return print_bad_irq_dependency(curr, prev, next,
825 bit_backwards, bit_forwards, irqclass);
828 #endif
830 static int
831 print_deadlock_bug(struct task_struct *curr, struct held_lock *prev,
832 struct held_lock *next)
834 if (!debug_locks_off_graph_unlock() || debug_locks_silent)
835 return 0;
837 printk("\n=============================================\n");
838 printk( "[ INFO: possible recursive locking detected ]\n");
839 print_kernel_version();
840 printk( "---------------------------------------------\n");
841 printk("%s/%d is trying to acquire lock:\n",
842 curr->comm, curr->pid);
843 print_lock(next);
844 printk("\nbut task is already holding lock:\n");
845 print_lock(prev);
847 printk("\nother info that might help us debug this:\n");
848 lockdep_print_held_locks(curr);
850 printk("\nstack backtrace:\n");
851 dump_stack();
853 return 0;
857 * Check whether we are holding such a class already.
859 * (Note that this has to be done separately, because the graph cannot
860 * detect such classes of deadlocks.)
862 * Returns: 0 on deadlock detected, 1 on OK, 2 on recursive read
864 static int
865 check_deadlock(struct task_struct *curr, struct held_lock *next,
866 struct lockdep_map *next_instance, int read)
868 struct held_lock *prev;
869 int i;
871 for (i = 0; i < curr->lockdep_depth; i++) {
872 prev = curr->held_locks + i;
873 if (prev->class != next->class)
874 continue;
876 * Allow read-after-read recursion of the same
877 * lock class (i.e. read_lock(lock)+read_lock(lock)):
879 if ((read == 2) && prev->read)
880 return 2;
881 return print_deadlock_bug(curr, prev, next);
883 return 1;
887 * There was a chain-cache miss, and we are about to add a new dependency
888 * to a previous lock. We recursively validate the following rules:
890 * - would the adding of the <prev> -> <next> dependency create a
891 * circular dependency in the graph? [== circular deadlock]
893 * - does the new prev->next dependency connect any hardirq-safe lock
894 * (in the full backwards-subgraph starting at <prev>) with any
895 * hardirq-unsafe lock (in the full forwards-subgraph starting at
896 * <next>)? [== illegal lock inversion with hardirq contexts]
898 * - does the new prev->next dependency connect any softirq-safe lock
899 * (in the full backwards-subgraph starting at <prev>) with any
900 * softirq-unsafe lock (in the full forwards-subgraph starting at
901 * <next>)? [== illegal lock inversion with softirq contexts]
903 * any of these scenarios could lead to a deadlock.
905 * Then if all the validations pass, we add the forwards and backwards
906 * dependency.
908 static int
909 check_prev_add(struct task_struct *curr, struct held_lock *prev,
910 struct held_lock *next, int distance)
912 struct lock_list *entry;
913 int ret;
916 * Prove that the new <prev> -> <next> dependency would not
917 * create a circular dependency in the graph. (We do this by
918 * forward-recursing into the graph starting at <next>, and
919 * checking whether we can reach <prev>.)
921 * We are using global variables to control the recursion, to
922 * keep the stackframe size of the recursive functions low:
924 check_source = next;
925 check_target = prev;
926 if (!(check_noncircular(next->class, 0)))
927 return print_circular_bug_tail();
929 #ifdef CONFIG_TRACE_IRQFLAGS
931 * Prove that the new dependency does not connect a hardirq-safe
932 * lock with a hardirq-unsafe lock - to achieve this we search
933 * the backwards-subgraph starting at <prev>, and the
934 * forwards-subgraph starting at <next>:
936 if (!check_usage(curr, prev, next, LOCK_USED_IN_HARDIRQ,
937 LOCK_ENABLED_HARDIRQS, "hard"))
938 return 0;
941 * Prove that the new dependency does not connect a hardirq-safe-read
942 * lock with a hardirq-unsafe lock - to achieve this we search
943 * the backwards-subgraph starting at <prev>, and the
944 * forwards-subgraph starting at <next>:
946 if (!check_usage(curr, prev, next, LOCK_USED_IN_HARDIRQ_READ,
947 LOCK_ENABLED_HARDIRQS, "hard-read"))
948 return 0;
951 * Prove that the new dependency does not connect a softirq-safe
952 * lock with a softirq-unsafe lock - to achieve this we search
953 * the backwards-subgraph starting at <prev>, and the
954 * forwards-subgraph starting at <next>:
956 if (!check_usage(curr, prev, next, LOCK_USED_IN_SOFTIRQ,
957 LOCK_ENABLED_SOFTIRQS, "soft"))
958 return 0;
960 * Prove that the new dependency does not connect a softirq-safe-read
961 * lock with a softirq-unsafe lock - to achieve this we search
962 * the backwards-subgraph starting at <prev>, and the
963 * forwards-subgraph starting at <next>:
965 if (!check_usage(curr, prev, next, LOCK_USED_IN_SOFTIRQ_READ,
966 LOCK_ENABLED_SOFTIRQS, "soft"))
967 return 0;
968 #endif
970 * For recursive read-locks we do all the dependency checks,
971 * but we dont store read-triggered dependencies (only
972 * write-triggered dependencies). This ensures that only the
973 * write-side dependencies matter, and that if for example a
974 * write-lock never takes any other locks, then the reads are
975 * equivalent to a NOP.
977 if (next->read == 2 || prev->read == 2)
978 return 1;
980 * Is the <prev> -> <next> dependency already present?
982 * (this may occur even though this is a new chain: consider
983 * e.g. the L1 -> L2 -> L3 -> L4 and the L5 -> L1 -> L2 -> L3
984 * chains - the second one will be new, but L1 already has
985 * L2 added to its dependency list, due to the first chain.)
987 list_for_each_entry(entry, &prev->class->locks_after, entry) {
988 if (entry->class == next->class) {
989 if (distance == 1)
990 entry->distance = 1;
991 return 2;
996 * Ok, all validations passed, add the new lock
997 * to the previous lock's dependency list:
999 ret = add_lock_to_list(prev->class, next->class,
1000 &prev->class->locks_after, next->acquire_ip, distance);
1002 if (!ret)
1003 return 0;
1005 ret = add_lock_to_list(next->class, prev->class,
1006 &next->class->locks_before, next->acquire_ip, distance);
1007 if (!ret)
1008 return 0;
1011 * Debugging printouts:
1013 if (verbose(prev->class) || verbose(next->class)) {
1014 graph_unlock();
1015 printk("\n new dependency: ");
1016 print_lock_name(prev->class);
1017 printk(" => ");
1018 print_lock_name(next->class);
1019 printk("\n");
1020 dump_stack();
1021 return graph_lock();
1023 return 1;
1027 * Add the dependency to all directly-previous locks that are 'relevant'.
1028 * The ones that are relevant are (in increasing distance from curr):
1029 * all consecutive trylock entries and the final non-trylock entry - or
1030 * the end of this context's lock-chain - whichever comes first.
1032 static int
1033 check_prevs_add(struct task_struct *curr, struct held_lock *next)
1035 int depth = curr->lockdep_depth;
1036 struct held_lock *hlock;
1039 * Debugging checks.
1041 * Depth must not be zero for a non-head lock:
1043 if (!depth)
1044 goto out_bug;
1046 * At least two relevant locks must exist for this
1047 * to be a head:
1049 if (curr->held_locks[depth].irq_context !=
1050 curr->held_locks[depth-1].irq_context)
1051 goto out_bug;
1053 for (;;) {
1054 int distance = curr->lockdep_depth - depth + 1;
1055 hlock = curr->held_locks + depth-1;
1057 * Only non-recursive-read entries get new dependencies
1058 * added:
1060 if (hlock->read != 2) {
1061 if (!check_prev_add(curr, hlock, next, distance))
1062 return 0;
1064 * Stop after the first non-trylock entry,
1065 * as non-trylock entries have added their
1066 * own direct dependencies already, so this
1067 * lock is connected to them indirectly:
1069 if (!hlock->trylock)
1070 break;
1072 depth--;
1074 * End of lock-stack?
1076 if (!depth)
1077 break;
1079 * Stop the search if we cross into another context:
1081 if (curr->held_locks[depth].irq_context !=
1082 curr->held_locks[depth-1].irq_context)
1083 break;
1085 return 1;
1086 out_bug:
1087 if (!debug_locks_off_graph_unlock())
1088 return 0;
1090 WARN_ON(1);
1092 return 0;
1097 * Is this the address of a static object:
1099 static int static_obj(void *obj)
1101 unsigned long start = (unsigned long) &_stext,
1102 end = (unsigned long) &_end,
1103 addr = (unsigned long) obj;
1104 #ifdef CONFIG_SMP
1105 int i;
1106 #endif
1109 * static variable?
1111 if ((addr >= start) && (addr < end))
1112 return 1;
1114 #ifdef CONFIG_SMP
1116 * percpu var?
1118 for_each_possible_cpu(i) {
1119 start = (unsigned long) &__per_cpu_start + per_cpu_offset(i);
1120 end = (unsigned long) &__per_cpu_start + PERCPU_ENOUGH_ROOM
1121 + per_cpu_offset(i);
1123 if ((addr >= start) && (addr < end))
1124 return 1;
1126 #endif
1129 * module var?
1131 return is_module_address(addr);
1135 * To make lock name printouts unique, we calculate a unique
1136 * class->name_version generation counter:
1138 static int count_matching_names(struct lock_class *new_class)
1140 struct lock_class *class;
1141 int count = 0;
1143 if (!new_class->name)
1144 return 0;
1146 list_for_each_entry(class, &all_lock_classes, lock_entry) {
1147 if (new_class->key - new_class->subclass == class->key)
1148 return class->name_version;
1149 if (class->name && !strcmp(class->name, new_class->name))
1150 count = max(count, class->name_version);
1153 return count + 1;
1157 * Register a lock's class in the hash-table, if the class is not present
1158 * yet. Otherwise we look it up. We cache the result in the lock object
1159 * itself, so actual lookup of the hash should be once per lock object.
1161 static inline struct lock_class *
1162 look_up_lock_class(struct lockdep_map *lock, unsigned int subclass)
1164 struct lockdep_subclass_key *key;
1165 struct list_head *hash_head;
1166 struct lock_class *class;
1168 #ifdef CONFIG_DEBUG_LOCKDEP
1170 * If the architecture calls into lockdep before initializing
1171 * the hashes then we'll warn about it later. (we cannot printk
1172 * right now)
1174 if (unlikely(!lockdep_initialized)) {
1175 lockdep_init();
1176 lockdep_init_error = 1;
1178 #endif
1181 * Static locks do not have their class-keys yet - for them the key
1182 * is the lock object itself:
1184 if (unlikely(!lock->key))
1185 lock->key = (void *)lock;
1188 * NOTE: the class-key must be unique. For dynamic locks, a static
1189 * lock_class_key variable is passed in through the mutex_init()
1190 * (or spin_lock_init()) call - which acts as the key. For static
1191 * locks we use the lock object itself as the key.
1193 BUILD_BUG_ON(sizeof(struct lock_class_key) > sizeof(struct lock_class));
1195 key = lock->key->subkeys + subclass;
1197 hash_head = classhashentry(key);
1200 * We can walk the hash lockfree, because the hash only
1201 * grows, and we are careful when adding entries to the end:
1203 list_for_each_entry(class, hash_head, hash_entry)
1204 if (class->key == key)
1205 return class;
1207 return NULL;
1211 * Register a lock's class in the hash-table, if the class is not present
1212 * yet. Otherwise we look it up. We cache the result in the lock object
1213 * itself, so actual lookup of the hash should be once per lock object.
1215 static inline struct lock_class *
1216 register_lock_class(struct lockdep_map *lock, unsigned int subclass, int force)
1218 struct lockdep_subclass_key *key;
1219 struct list_head *hash_head;
1220 struct lock_class *class;
1221 unsigned long flags;
1223 class = look_up_lock_class(lock, subclass);
1224 if (likely(class))
1225 return class;
1228 * Debug-check: all keys must be persistent!
1230 if (!static_obj(lock->key)) {
1231 debug_locks_off();
1232 printk("INFO: trying to register non-static key.\n");
1233 printk("the code is fine but needs lockdep annotation.\n");
1234 printk("turning off the locking correctness validator.\n");
1235 dump_stack();
1237 return NULL;
1240 key = lock->key->subkeys + subclass;
1241 hash_head = classhashentry(key);
1243 raw_local_irq_save(flags);
1244 if (!graph_lock()) {
1245 raw_local_irq_restore(flags);
1246 return NULL;
1249 * We have to do the hash-walk again, to avoid races
1250 * with another CPU:
1252 list_for_each_entry(class, hash_head, hash_entry)
1253 if (class->key == key)
1254 goto out_unlock_set;
1256 * Allocate a new key from the static array, and add it to
1257 * the hash:
1259 if (nr_lock_classes >= MAX_LOCKDEP_KEYS) {
1260 if (!debug_locks_off_graph_unlock()) {
1261 raw_local_irq_restore(flags);
1262 return NULL;
1264 raw_local_irq_restore(flags);
1266 printk("BUG: MAX_LOCKDEP_KEYS too low!\n");
1267 printk("turning off the locking correctness validator.\n");
1268 return NULL;
1270 class = lock_classes + nr_lock_classes++;
1271 debug_atomic_inc(&nr_unused_locks);
1272 class->key = key;
1273 class->name = lock->name;
1274 class->subclass = subclass;
1275 INIT_LIST_HEAD(&class->lock_entry);
1276 INIT_LIST_HEAD(&class->locks_before);
1277 INIT_LIST_HEAD(&class->locks_after);
1278 class->name_version = count_matching_names(class);
1280 * We use RCU's safe list-add method to make
1281 * parallel walking of the hash-list safe:
1283 list_add_tail_rcu(&class->hash_entry, hash_head);
1285 if (verbose(class)) {
1286 graph_unlock();
1287 raw_local_irq_restore(flags);
1289 printk("\nnew class %p: %s", class->key, class->name);
1290 if (class->name_version > 1)
1291 printk("#%d", class->name_version);
1292 printk("\n");
1293 dump_stack();
1295 raw_local_irq_save(flags);
1296 if (!graph_lock()) {
1297 raw_local_irq_restore(flags);
1298 return NULL;
1301 out_unlock_set:
1302 graph_unlock();
1303 raw_local_irq_restore(flags);
1305 if (!subclass || force)
1306 lock->class_cache = class;
1308 if (DEBUG_LOCKS_WARN_ON(class->subclass != subclass))
1309 return NULL;
1311 return class;
1315 * Look up a dependency chain. If the key is not present yet then
1316 * add it and return 0 - in this case the new dependency chain is
1317 * validated. If the key is already hashed, return 1.
1319 static inline int lookup_chain_cache(u64 chain_key, struct lock_class *class)
1321 struct list_head *hash_head = chainhashentry(chain_key);
1322 struct lock_chain *chain;
1324 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
1325 return 0;
1327 * We can walk it lock-free, because entries only get added
1328 * to the hash:
1330 list_for_each_entry(chain, hash_head, entry) {
1331 if (chain->chain_key == chain_key) {
1332 cache_hit:
1333 debug_atomic_inc(&chain_lookup_hits);
1334 if (very_verbose(class))
1335 printk("\nhash chain already cached, key: "
1336 "%016Lx tail class: [%p] %s\n",
1337 (unsigned long long)chain_key,
1338 class->key, class->name);
1339 return 0;
1342 if (very_verbose(class))
1343 printk("\nnew hash chain, key: %016Lx tail class: [%p] %s\n",
1344 (unsigned long long)chain_key, class->key, class->name);
1346 * Allocate a new chain entry from the static array, and add
1347 * it to the hash:
1349 if (!graph_lock())
1350 return 0;
1352 * We have to walk the chain again locked - to avoid duplicates:
1354 list_for_each_entry(chain, hash_head, entry) {
1355 if (chain->chain_key == chain_key) {
1356 graph_unlock();
1357 goto cache_hit;
1360 if (unlikely(nr_lock_chains >= MAX_LOCKDEP_CHAINS)) {
1361 if (!debug_locks_off_graph_unlock())
1362 return 0;
1364 printk("BUG: MAX_LOCKDEP_CHAINS too low!\n");
1365 printk("turning off the locking correctness validator.\n");
1366 return 0;
1368 chain = lock_chains + nr_lock_chains++;
1369 chain->chain_key = chain_key;
1370 list_add_tail_rcu(&chain->entry, hash_head);
1371 debug_atomic_inc(&chain_lookup_misses);
1372 #ifdef CONFIG_TRACE_IRQFLAGS
1373 if (current->hardirq_context)
1374 nr_hardirq_chains++;
1375 else {
1376 if (current->softirq_context)
1377 nr_softirq_chains++;
1378 else
1379 nr_process_chains++;
1381 #else
1382 nr_process_chains++;
1383 #endif
1385 return 1;
1389 * We are building curr_chain_key incrementally, so double-check
1390 * it from scratch, to make sure that it's done correctly:
1392 static void check_chain_key(struct task_struct *curr)
1394 #ifdef CONFIG_DEBUG_LOCKDEP
1395 struct held_lock *hlock, *prev_hlock = NULL;
1396 unsigned int i, id;
1397 u64 chain_key = 0;
1399 for (i = 0; i < curr->lockdep_depth; i++) {
1400 hlock = curr->held_locks + i;
1401 if (chain_key != hlock->prev_chain_key) {
1402 debug_locks_off();
1403 printk("hm#1, depth: %u [%u], %016Lx != %016Lx\n",
1404 curr->lockdep_depth, i,
1405 (unsigned long long)chain_key,
1406 (unsigned long long)hlock->prev_chain_key);
1407 WARN_ON(1);
1408 return;
1410 id = hlock->class - lock_classes;
1411 if (DEBUG_LOCKS_WARN_ON(id >= MAX_LOCKDEP_KEYS))
1412 return;
1414 if (prev_hlock && (prev_hlock->irq_context !=
1415 hlock->irq_context))
1416 chain_key = 0;
1417 chain_key = iterate_chain_key(chain_key, id);
1418 prev_hlock = hlock;
1420 if (chain_key != curr->curr_chain_key) {
1421 debug_locks_off();
1422 printk("hm#2, depth: %u [%u], %016Lx != %016Lx\n",
1423 curr->lockdep_depth, i,
1424 (unsigned long long)chain_key,
1425 (unsigned long long)curr->curr_chain_key);
1426 WARN_ON(1);
1428 #endif
1431 #ifdef CONFIG_TRACE_IRQFLAGS
1434 * print irq inversion bug:
1436 static int
1437 print_irq_inversion_bug(struct task_struct *curr, struct lock_class *other,
1438 struct held_lock *this, int forwards,
1439 const char *irqclass)
1441 if (!debug_locks_off_graph_unlock() || debug_locks_silent)
1442 return 0;
1444 printk("\n=========================================================\n");
1445 printk( "[ INFO: possible irq lock inversion dependency detected ]\n");
1446 print_kernel_version();
1447 printk( "---------------------------------------------------------\n");
1448 printk("%s/%d just changed the state of lock:\n",
1449 curr->comm, curr->pid);
1450 print_lock(this);
1451 if (forwards)
1452 printk("but this lock took another, %s-irq-unsafe lock in the past:\n", irqclass);
1453 else
1454 printk("but this lock was taken by another, %s-irq-safe lock in the past:\n", irqclass);
1455 print_lock_name(other);
1456 printk("\n\nand interrupts could create inverse lock ordering between them.\n\n");
1458 printk("\nother info that might help us debug this:\n");
1459 lockdep_print_held_locks(curr);
1461 printk("\nthe first lock's dependencies:\n");
1462 print_lock_dependencies(this->class, 0);
1464 printk("\nthe second lock's dependencies:\n");
1465 print_lock_dependencies(other, 0);
1467 printk("\nstack backtrace:\n");
1468 dump_stack();
1470 return 0;
1474 * Prove that in the forwards-direction subgraph starting at <this>
1475 * there is no lock matching <mask>:
1477 static int
1478 check_usage_forwards(struct task_struct *curr, struct held_lock *this,
1479 enum lock_usage_bit bit, const char *irqclass)
1481 int ret;
1483 find_usage_bit = bit;
1484 /* fills in <forwards_match> */
1485 ret = find_usage_forwards(this->class, 0);
1486 if (!ret || ret == 1)
1487 return ret;
1489 return print_irq_inversion_bug(curr, forwards_match, this, 1, irqclass);
1493 * Prove that in the backwards-direction subgraph starting at <this>
1494 * there is no lock matching <mask>:
1496 static int
1497 check_usage_backwards(struct task_struct *curr, struct held_lock *this,
1498 enum lock_usage_bit bit, const char *irqclass)
1500 int ret;
1502 find_usage_bit = bit;
1503 /* fills in <backwards_match> */
1504 ret = find_usage_backwards(this->class, 0);
1505 if (!ret || ret == 1)
1506 return ret;
1508 return print_irq_inversion_bug(curr, backwards_match, this, 0, irqclass);
1511 void print_irqtrace_events(struct task_struct *curr)
1513 printk("irq event stamp: %u\n", curr->irq_events);
1514 printk("hardirqs last enabled at (%u): ", curr->hardirq_enable_event);
1515 print_ip_sym(curr->hardirq_enable_ip);
1516 printk("hardirqs last disabled at (%u): ", curr->hardirq_disable_event);
1517 print_ip_sym(curr->hardirq_disable_ip);
1518 printk("softirqs last enabled at (%u): ", curr->softirq_enable_event);
1519 print_ip_sym(curr->softirq_enable_ip);
1520 printk("softirqs last disabled at (%u): ", curr->softirq_disable_event);
1521 print_ip_sym(curr->softirq_disable_ip);
1524 #endif
1526 static int
1527 print_usage_bug(struct task_struct *curr, struct held_lock *this,
1528 enum lock_usage_bit prev_bit, enum lock_usage_bit new_bit)
1530 if (!debug_locks_off_graph_unlock() || debug_locks_silent)
1531 return 0;
1533 printk("\n=================================\n");
1534 printk( "[ INFO: inconsistent lock state ]\n");
1535 print_kernel_version();
1536 printk( "---------------------------------\n");
1538 printk("inconsistent {%s} -> {%s} usage.\n",
1539 usage_str[prev_bit], usage_str[new_bit]);
1541 printk("%s/%d [HC%u[%lu]:SC%u[%lu]:HE%u:SE%u] takes:\n",
1542 curr->comm, curr->pid,
1543 trace_hardirq_context(curr), hardirq_count() >> HARDIRQ_SHIFT,
1544 trace_softirq_context(curr), softirq_count() >> SOFTIRQ_SHIFT,
1545 trace_hardirqs_enabled(curr),
1546 trace_softirqs_enabled(curr));
1547 print_lock(this);
1549 printk("{%s} state was registered at:\n", usage_str[prev_bit]);
1550 print_stack_trace(this->class->usage_traces + prev_bit, 1);
1552 print_irqtrace_events(curr);
1553 printk("\nother info that might help us debug this:\n");
1554 lockdep_print_held_locks(curr);
1556 printk("\nstack backtrace:\n");
1557 dump_stack();
1559 return 0;
1563 * Print out an error if an invalid bit is set:
1565 static inline int
1566 valid_state(struct task_struct *curr, struct held_lock *this,
1567 enum lock_usage_bit new_bit, enum lock_usage_bit bad_bit)
1569 if (unlikely(this->class->usage_mask & (1 << bad_bit)))
1570 return print_usage_bug(curr, this, bad_bit, new_bit);
1571 return 1;
1574 #define STRICT_READ_CHECKS 1
1577 * Mark a lock with a usage bit, and validate the state transition:
1579 static int mark_lock(struct task_struct *curr, struct held_lock *this,
1580 enum lock_usage_bit new_bit, unsigned long ip)
1582 unsigned int new_mask = 1 << new_bit, ret = 1;
1585 * If already set then do not dirty the cacheline,
1586 * nor do any checks:
1588 if (likely(this->class->usage_mask & new_mask))
1589 return 1;
1591 if (!graph_lock())
1592 return 0;
1594 * Make sure we didnt race:
1596 if (unlikely(this->class->usage_mask & new_mask)) {
1597 graph_unlock();
1598 return 1;
1601 this->class->usage_mask |= new_mask;
1603 #ifdef CONFIG_TRACE_IRQFLAGS
1604 if (new_bit == LOCK_ENABLED_HARDIRQS ||
1605 new_bit == LOCK_ENABLED_HARDIRQS_READ)
1606 ip = curr->hardirq_enable_ip;
1607 else if (new_bit == LOCK_ENABLED_SOFTIRQS ||
1608 new_bit == LOCK_ENABLED_SOFTIRQS_READ)
1609 ip = curr->softirq_enable_ip;
1610 #endif
1611 if (!save_trace(this->class->usage_traces + new_bit))
1612 return 0;
1614 switch (new_bit) {
1615 #ifdef CONFIG_TRACE_IRQFLAGS
1616 case LOCK_USED_IN_HARDIRQ:
1617 if (!valid_state(curr, this, new_bit, LOCK_ENABLED_HARDIRQS))
1618 return 0;
1619 if (!valid_state(curr, this, new_bit,
1620 LOCK_ENABLED_HARDIRQS_READ))
1621 return 0;
1623 * just marked it hardirq-safe, check that this lock
1624 * took no hardirq-unsafe lock in the past:
1626 if (!check_usage_forwards(curr, this,
1627 LOCK_ENABLED_HARDIRQS, "hard"))
1628 return 0;
1629 #if STRICT_READ_CHECKS
1631 * just marked it hardirq-safe, check that this lock
1632 * took no hardirq-unsafe-read lock in the past:
1634 if (!check_usage_forwards(curr, this,
1635 LOCK_ENABLED_HARDIRQS_READ, "hard-read"))
1636 return 0;
1637 #endif
1638 if (hardirq_verbose(this->class))
1639 ret = 2;
1640 break;
1641 case LOCK_USED_IN_SOFTIRQ:
1642 if (!valid_state(curr, this, new_bit, LOCK_ENABLED_SOFTIRQS))
1643 return 0;
1644 if (!valid_state(curr, this, new_bit,
1645 LOCK_ENABLED_SOFTIRQS_READ))
1646 return 0;
1648 * just marked it softirq-safe, check that this lock
1649 * took no softirq-unsafe lock in the past:
1651 if (!check_usage_forwards(curr, this,
1652 LOCK_ENABLED_SOFTIRQS, "soft"))
1653 return 0;
1654 #if STRICT_READ_CHECKS
1656 * just marked it softirq-safe, check that this lock
1657 * took no softirq-unsafe-read lock in the past:
1659 if (!check_usage_forwards(curr, this,
1660 LOCK_ENABLED_SOFTIRQS_READ, "soft-read"))
1661 return 0;
1662 #endif
1663 if (softirq_verbose(this->class))
1664 ret = 2;
1665 break;
1666 case LOCK_USED_IN_HARDIRQ_READ:
1667 if (!valid_state(curr, this, new_bit, LOCK_ENABLED_HARDIRQS))
1668 return 0;
1670 * just marked it hardirq-read-safe, check that this lock
1671 * took no hardirq-unsafe lock in the past:
1673 if (!check_usage_forwards(curr, this,
1674 LOCK_ENABLED_HARDIRQS, "hard"))
1675 return 0;
1676 if (hardirq_verbose(this->class))
1677 ret = 2;
1678 break;
1679 case LOCK_USED_IN_SOFTIRQ_READ:
1680 if (!valid_state(curr, this, new_bit, LOCK_ENABLED_SOFTIRQS))
1681 return 0;
1683 * just marked it softirq-read-safe, check that this lock
1684 * took no softirq-unsafe lock in the past:
1686 if (!check_usage_forwards(curr, this,
1687 LOCK_ENABLED_SOFTIRQS, "soft"))
1688 return 0;
1689 if (softirq_verbose(this->class))
1690 ret = 2;
1691 break;
1692 case LOCK_ENABLED_HARDIRQS:
1693 if (!valid_state(curr, this, new_bit, LOCK_USED_IN_HARDIRQ))
1694 return 0;
1695 if (!valid_state(curr, this, new_bit,
1696 LOCK_USED_IN_HARDIRQ_READ))
1697 return 0;
1699 * just marked it hardirq-unsafe, check that no hardirq-safe
1700 * lock in the system ever took it in the past:
1702 if (!check_usage_backwards(curr, this,
1703 LOCK_USED_IN_HARDIRQ, "hard"))
1704 return 0;
1705 #if STRICT_READ_CHECKS
1707 * just marked it hardirq-unsafe, check that no
1708 * hardirq-safe-read lock in the system ever took
1709 * it in the past:
1711 if (!check_usage_backwards(curr, this,
1712 LOCK_USED_IN_HARDIRQ_READ, "hard-read"))
1713 return 0;
1714 #endif
1715 if (hardirq_verbose(this->class))
1716 ret = 2;
1717 break;
1718 case LOCK_ENABLED_SOFTIRQS:
1719 if (!valid_state(curr, this, new_bit, LOCK_USED_IN_SOFTIRQ))
1720 return 0;
1721 if (!valid_state(curr, this, new_bit,
1722 LOCK_USED_IN_SOFTIRQ_READ))
1723 return 0;
1725 * just marked it softirq-unsafe, check that no softirq-safe
1726 * lock in the system ever took it in the past:
1728 if (!check_usage_backwards(curr, this,
1729 LOCK_USED_IN_SOFTIRQ, "soft"))
1730 return 0;
1731 #if STRICT_READ_CHECKS
1733 * just marked it softirq-unsafe, check that no
1734 * softirq-safe-read lock in the system ever took
1735 * it in the past:
1737 if (!check_usage_backwards(curr, this,
1738 LOCK_USED_IN_SOFTIRQ_READ, "soft-read"))
1739 return 0;
1740 #endif
1741 if (softirq_verbose(this->class))
1742 ret = 2;
1743 break;
1744 case LOCK_ENABLED_HARDIRQS_READ:
1745 if (!valid_state(curr, this, new_bit, LOCK_USED_IN_HARDIRQ))
1746 return 0;
1747 #if STRICT_READ_CHECKS
1749 * just marked it hardirq-read-unsafe, check that no
1750 * hardirq-safe lock in the system ever took it in the past:
1752 if (!check_usage_backwards(curr, this,
1753 LOCK_USED_IN_HARDIRQ, "hard"))
1754 return 0;
1755 #endif
1756 if (hardirq_verbose(this->class))
1757 ret = 2;
1758 break;
1759 case LOCK_ENABLED_SOFTIRQS_READ:
1760 if (!valid_state(curr, this, new_bit, LOCK_USED_IN_SOFTIRQ))
1761 return 0;
1762 #if STRICT_READ_CHECKS
1764 * just marked it softirq-read-unsafe, check that no
1765 * softirq-safe lock in the system ever took it in the past:
1767 if (!check_usage_backwards(curr, this,
1768 LOCK_USED_IN_SOFTIRQ, "soft"))
1769 return 0;
1770 #endif
1771 if (softirq_verbose(this->class))
1772 ret = 2;
1773 break;
1774 #endif
1775 case LOCK_USED:
1777 * Add it to the global list of classes:
1779 list_add_tail_rcu(&this->class->lock_entry, &all_lock_classes);
1780 debug_atomic_dec(&nr_unused_locks);
1781 break;
1782 default:
1783 if (!debug_locks_off_graph_unlock())
1784 return 0;
1785 WARN_ON(1);
1786 return 0;
1789 graph_unlock();
1792 * We must printk outside of the graph_lock:
1794 if (ret == 2) {
1795 printk("\nmarked lock as {%s}:\n", usage_str[new_bit]);
1796 print_lock(this);
1797 print_irqtrace_events(curr);
1798 dump_stack();
1801 return ret;
1804 #ifdef CONFIG_TRACE_IRQFLAGS
1806 * Mark all held locks with a usage bit:
1808 static int
1809 mark_held_locks(struct task_struct *curr, int hardirq, unsigned long ip)
1811 enum lock_usage_bit usage_bit;
1812 struct held_lock *hlock;
1813 int i;
1815 for (i = 0; i < curr->lockdep_depth; i++) {
1816 hlock = curr->held_locks + i;
1818 if (hardirq) {
1819 if (hlock->read)
1820 usage_bit = LOCK_ENABLED_HARDIRQS_READ;
1821 else
1822 usage_bit = LOCK_ENABLED_HARDIRQS;
1823 } else {
1824 if (hlock->read)
1825 usage_bit = LOCK_ENABLED_SOFTIRQS_READ;
1826 else
1827 usage_bit = LOCK_ENABLED_SOFTIRQS;
1829 if (!mark_lock(curr, hlock, usage_bit, ip))
1830 return 0;
1833 return 1;
1837 * Debugging helper: via this flag we know that we are in
1838 * 'early bootup code', and will warn about any invalid irqs-on event:
1840 static int early_boot_irqs_enabled;
1842 void early_boot_irqs_off(void)
1844 early_boot_irqs_enabled = 0;
1847 void early_boot_irqs_on(void)
1849 early_boot_irqs_enabled = 1;
1853 * Hardirqs will be enabled:
1855 void trace_hardirqs_on(void)
1857 struct task_struct *curr = current;
1858 unsigned long ip;
1860 if (unlikely(!debug_locks || current->lockdep_recursion))
1861 return;
1863 if (DEBUG_LOCKS_WARN_ON(unlikely(!early_boot_irqs_enabled)))
1864 return;
1866 if (unlikely(curr->hardirqs_enabled)) {
1867 debug_atomic_inc(&redundant_hardirqs_on);
1868 return;
1870 /* we'll do an OFF -> ON transition: */
1871 curr->hardirqs_enabled = 1;
1872 ip = (unsigned long) __builtin_return_address(0);
1874 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
1875 return;
1876 if (DEBUG_LOCKS_WARN_ON(current->hardirq_context))
1877 return;
1879 * We are going to turn hardirqs on, so set the
1880 * usage bit for all held locks:
1882 if (!mark_held_locks(curr, 1, ip))
1883 return;
1885 * If we have softirqs enabled, then set the usage
1886 * bit for all held locks. (disabled hardirqs prevented
1887 * this bit from being set before)
1889 if (curr->softirqs_enabled)
1890 if (!mark_held_locks(curr, 0, ip))
1891 return;
1893 curr->hardirq_enable_ip = ip;
1894 curr->hardirq_enable_event = ++curr->irq_events;
1895 debug_atomic_inc(&hardirqs_on_events);
1898 EXPORT_SYMBOL(trace_hardirqs_on);
1901 * Hardirqs were disabled:
1903 void trace_hardirqs_off(void)
1905 struct task_struct *curr = current;
1907 if (unlikely(!debug_locks || current->lockdep_recursion))
1908 return;
1910 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
1911 return;
1913 if (curr->hardirqs_enabled) {
1915 * We have done an ON -> OFF transition:
1917 curr->hardirqs_enabled = 0;
1918 curr->hardirq_disable_ip = _RET_IP_;
1919 curr->hardirq_disable_event = ++curr->irq_events;
1920 debug_atomic_inc(&hardirqs_off_events);
1921 } else
1922 debug_atomic_inc(&redundant_hardirqs_off);
1925 EXPORT_SYMBOL(trace_hardirqs_off);
1928 * Softirqs will be enabled:
1930 void trace_softirqs_on(unsigned long ip)
1932 struct task_struct *curr = current;
1934 if (unlikely(!debug_locks))
1935 return;
1937 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
1938 return;
1940 if (curr->softirqs_enabled) {
1941 debug_atomic_inc(&redundant_softirqs_on);
1942 return;
1946 * We'll do an OFF -> ON transition:
1948 curr->softirqs_enabled = 1;
1949 curr->softirq_enable_ip = ip;
1950 curr->softirq_enable_event = ++curr->irq_events;
1951 debug_atomic_inc(&softirqs_on_events);
1953 * We are going to turn softirqs on, so set the
1954 * usage bit for all held locks, if hardirqs are
1955 * enabled too:
1957 if (curr->hardirqs_enabled)
1958 mark_held_locks(curr, 0, ip);
1962 * Softirqs were disabled:
1964 void trace_softirqs_off(unsigned long ip)
1966 struct task_struct *curr = current;
1968 if (unlikely(!debug_locks))
1969 return;
1971 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
1972 return;
1974 if (curr->softirqs_enabled) {
1976 * We have done an ON -> OFF transition:
1978 curr->softirqs_enabled = 0;
1979 curr->softirq_disable_ip = ip;
1980 curr->softirq_disable_event = ++curr->irq_events;
1981 debug_atomic_inc(&softirqs_off_events);
1982 DEBUG_LOCKS_WARN_ON(!softirq_count());
1983 } else
1984 debug_atomic_inc(&redundant_softirqs_off);
1987 #endif
1990 * Initialize a lock instance's lock-class mapping info:
1992 void lockdep_init_map(struct lockdep_map *lock, const char *name,
1993 struct lock_class_key *key, int subclass)
1995 if (unlikely(!debug_locks))
1996 return;
1998 if (DEBUG_LOCKS_WARN_ON(!key))
1999 return;
2000 if (DEBUG_LOCKS_WARN_ON(!name))
2001 return;
2003 * Sanity check, the lock-class key must be persistent:
2005 if (!static_obj(key)) {
2006 printk("BUG: key %p not in .data!\n", key);
2007 DEBUG_LOCKS_WARN_ON(1);
2008 return;
2010 lock->name = name;
2011 lock->key = key;
2012 lock->class_cache = NULL;
2013 if (subclass)
2014 register_lock_class(lock, subclass, 1);
2017 EXPORT_SYMBOL_GPL(lockdep_init_map);
2020 * This gets called for every mutex_lock*()/spin_lock*() operation.
2021 * We maintain the dependency maps and validate the locking attempt:
2023 static int __lock_acquire(struct lockdep_map *lock, unsigned int subclass,
2024 int trylock, int read, int check, int hardirqs_off,
2025 unsigned long ip)
2027 struct task_struct *curr = current;
2028 struct lock_class *class = NULL;
2029 struct held_lock *hlock;
2030 unsigned int depth, id;
2031 int chain_head = 0;
2032 u64 chain_key;
2034 if (unlikely(!debug_locks))
2035 return 0;
2037 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2038 return 0;
2040 if (unlikely(subclass >= MAX_LOCKDEP_SUBCLASSES)) {
2041 debug_locks_off();
2042 printk("BUG: MAX_LOCKDEP_SUBCLASSES too low!\n");
2043 printk("turning off the locking correctness validator.\n");
2044 return 0;
2047 if (!subclass)
2048 class = lock->class_cache;
2050 * Not cached yet or subclass?
2052 if (unlikely(!class)) {
2053 class = register_lock_class(lock, subclass, 0);
2054 if (!class)
2055 return 0;
2057 debug_atomic_inc((atomic_t *)&class->ops);
2058 if (very_verbose(class)) {
2059 printk("\nacquire class [%p] %s", class->key, class->name);
2060 if (class->name_version > 1)
2061 printk("#%d", class->name_version);
2062 printk("\n");
2063 dump_stack();
2067 * Add the lock to the list of currently held locks.
2068 * (we dont increase the depth just yet, up until the
2069 * dependency checks are done)
2071 depth = curr->lockdep_depth;
2072 if (DEBUG_LOCKS_WARN_ON(depth >= MAX_LOCK_DEPTH))
2073 return 0;
2075 hlock = curr->held_locks + depth;
2077 hlock->class = class;
2078 hlock->acquire_ip = ip;
2079 hlock->instance = lock;
2080 hlock->trylock = trylock;
2081 hlock->read = read;
2082 hlock->check = check;
2083 hlock->hardirqs_off = hardirqs_off;
2085 if (check != 2)
2086 goto out_calc_hash;
2087 #ifdef CONFIG_TRACE_IRQFLAGS
2089 * If non-trylock use in a hardirq or softirq context, then
2090 * mark the lock as used in these contexts:
2092 if (!trylock) {
2093 if (read) {
2094 if (curr->hardirq_context)
2095 if (!mark_lock(curr, hlock,
2096 LOCK_USED_IN_HARDIRQ_READ, ip))
2097 return 0;
2098 if (curr->softirq_context)
2099 if (!mark_lock(curr, hlock,
2100 LOCK_USED_IN_SOFTIRQ_READ, ip))
2101 return 0;
2102 } else {
2103 if (curr->hardirq_context)
2104 if (!mark_lock(curr, hlock, LOCK_USED_IN_HARDIRQ, ip))
2105 return 0;
2106 if (curr->softirq_context)
2107 if (!mark_lock(curr, hlock, LOCK_USED_IN_SOFTIRQ, ip))
2108 return 0;
2111 if (!hardirqs_off) {
2112 if (read) {
2113 if (!mark_lock(curr, hlock,
2114 LOCK_ENABLED_HARDIRQS_READ, ip))
2115 return 0;
2116 if (curr->softirqs_enabled)
2117 if (!mark_lock(curr, hlock,
2118 LOCK_ENABLED_SOFTIRQS_READ, ip))
2119 return 0;
2120 } else {
2121 if (!mark_lock(curr, hlock,
2122 LOCK_ENABLED_HARDIRQS, ip))
2123 return 0;
2124 if (curr->softirqs_enabled)
2125 if (!mark_lock(curr, hlock,
2126 LOCK_ENABLED_SOFTIRQS, ip))
2127 return 0;
2130 #endif
2131 /* mark it as used: */
2132 if (!mark_lock(curr, hlock, LOCK_USED, ip))
2133 return 0;
2134 out_calc_hash:
2136 * Calculate the chain hash: it's the combined has of all the
2137 * lock keys along the dependency chain. We save the hash value
2138 * at every step so that we can get the current hash easily
2139 * after unlock. The chain hash is then used to cache dependency
2140 * results.
2142 * The 'key ID' is what is the most compact key value to drive
2143 * the hash, not class->key.
2145 id = class - lock_classes;
2146 if (DEBUG_LOCKS_WARN_ON(id >= MAX_LOCKDEP_KEYS))
2147 return 0;
2149 chain_key = curr->curr_chain_key;
2150 if (!depth) {
2151 if (DEBUG_LOCKS_WARN_ON(chain_key != 0))
2152 return 0;
2153 chain_head = 1;
2156 hlock->prev_chain_key = chain_key;
2158 #ifdef CONFIG_TRACE_IRQFLAGS
2160 * Keep track of points where we cross into an interrupt context:
2162 hlock->irq_context = 2*(curr->hardirq_context ? 1 : 0) +
2163 curr->softirq_context;
2164 if (depth) {
2165 struct held_lock *prev_hlock;
2167 prev_hlock = curr->held_locks + depth-1;
2169 * If we cross into another context, reset the
2170 * hash key (this also prevents the checking and the
2171 * adding of the dependency to 'prev'):
2173 if (prev_hlock->irq_context != hlock->irq_context) {
2174 chain_key = 0;
2175 chain_head = 1;
2178 #endif
2179 chain_key = iterate_chain_key(chain_key, id);
2180 curr->curr_chain_key = chain_key;
2183 * Trylock needs to maintain the stack of held locks, but it
2184 * does not add new dependencies, because trylock can be done
2185 * in any order.
2187 * We look up the chain_key and do the O(N^2) check and update of
2188 * the dependencies only if this is a new dependency chain.
2189 * (If lookup_chain_cache() returns with 1 it acquires
2190 * graph_lock for us)
2192 if (!trylock && (check == 2) && lookup_chain_cache(chain_key, class)) {
2194 * Check whether last held lock:
2196 * - is irq-safe, if this lock is irq-unsafe
2197 * - is softirq-safe, if this lock is hardirq-unsafe
2199 * And check whether the new lock's dependency graph
2200 * could lead back to the previous lock.
2202 * any of these scenarios could lead to a deadlock. If
2203 * All validations
2205 int ret = check_deadlock(curr, hlock, lock, read);
2207 if (!ret)
2208 return 0;
2210 * Mark recursive read, as we jump over it when
2211 * building dependencies (just like we jump over
2212 * trylock entries):
2214 if (ret == 2)
2215 hlock->read = 2;
2217 * Add dependency only if this lock is not the head
2218 * of the chain, and if it's not a secondary read-lock:
2220 if (!chain_head && ret != 2)
2221 if (!check_prevs_add(curr, hlock))
2222 return 0;
2223 graph_unlock();
2224 } else
2225 /* after lookup_chain_cache(): */
2226 if (unlikely(!debug_locks))
2227 return 0;
2229 curr->lockdep_depth++;
2230 check_chain_key(curr);
2231 #ifdef CONFIG_DEBUG_LOCKDEP
2232 if (unlikely(!debug_locks))
2233 return 0;
2234 #endif
2235 if (unlikely(curr->lockdep_depth >= MAX_LOCK_DEPTH)) {
2236 debug_locks_off();
2237 printk("BUG: MAX_LOCK_DEPTH too low!\n");
2238 printk("turning off the locking correctness validator.\n");
2239 return 0;
2242 if (unlikely(curr->lockdep_depth > max_lockdep_depth))
2243 max_lockdep_depth = curr->lockdep_depth;
2245 return 1;
2248 static int
2249 print_unlock_inbalance_bug(struct task_struct *curr, struct lockdep_map *lock,
2250 unsigned long ip)
2252 if (!debug_locks_off())
2253 return 0;
2254 if (debug_locks_silent)
2255 return 0;
2257 printk("\n=====================================\n");
2258 printk( "[ BUG: bad unlock balance detected! ]\n");
2259 printk( "-------------------------------------\n");
2260 printk("%s/%d is trying to release lock (",
2261 curr->comm, curr->pid);
2262 print_lockdep_cache(lock);
2263 printk(") at:\n");
2264 print_ip_sym(ip);
2265 printk("but there are no more locks to release!\n");
2266 printk("\nother info that might help us debug this:\n");
2267 lockdep_print_held_locks(curr);
2269 printk("\nstack backtrace:\n");
2270 dump_stack();
2272 return 0;
2276 * Common debugging checks for both nested and non-nested unlock:
2278 static int check_unlock(struct task_struct *curr, struct lockdep_map *lock,
2279 unsigned long ip)
2281 if (unlikely(!debug_locks))
2282 return 0;
2283 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2284 return 0;
2286 if (curr->lockdep_depth <= 0)
2287 return print_unlock_inbalance_bug(curr, lock, ip);
2289 return 1;
2293 * Remove the lock to the list of currently held locks in a
2294 * potentially non-nested (out of order) manner. This is a
2295 * relatively rare operation, as all the unlock APIs default
2296 * to nested mode (which uses lock_release()):
2298 static int
2299 lock_release_non_nested(struct task_struct *curr,
2300 struct lockdep_map *lock, unsigned long ip)
2302 struct held_lock *hlock, *prev_hlock;
2303 unsigned int depth;
2304 int i;
2307 * Check whether the lock exists in the current stack
2308 * of held locks:
2310 depth = curr->lockdep_depth;
2311 if (DEBUG_LOCKS_WARN_ON(!depth))
2312 return 0;
2314 prev_hlock = NULL;
2315 for (i = depth-1; i >= 0; i--) {
2316 hlock = curr->held_locks + i;
2318 * We must not cross into another context:
2320 if (prev_hlock && prev_hlock->irq_context != hlock->irq_context)
2321 break;
2322 if (hlock->instance == lock)
2323 goto found_it;
2324 prev_hlock = hlock;
2326 return print_unlock_inbalance_bug(curr, lock, ip);
2328 found_it:
2330 * We have the right lock to unlock, 'hlock' points to it.
2331 * Now we remove it from the stack, and add back the other
2332 * entries (if any), recalculating the hash along the way:
2334 curr->lockdep_depth = i;
2335 curr->curr_chain_key = hlock->prev_chain_key;
2337 for (i++; i < depth; i++) {
2338 hlock = curr->held_locks + i;
2339 if (!__lock_acquire(hlock->instance,
2340 hlock->class->subclass, hlock->trylock,
2341 hlock->read, hlock->check, hlock->hardirqs_off,
2342 hlock->acquire_ip))
2343 return 0;
2346 if (DEBUG_LOCKS_WARN_ON(curr->lockdep_depth != depth - 1))
2347 return 0;
2348 return 1;
2352 * Remove the lock to the list of currently held locks - this gets
2353 * called on mutex_unlock()/spin_unlock*() (or on a failed
2354 * mutex_lock_interruptible()). This is done for unlocks that nest
2355 * perfectly. (i.e. the current top of the lock-stack is unlocked)
2357 static int lock_release_nested(struct task_struct *curr,
2358 struct lockdep_map *lock, unsigned long ip)
2360 struct held_lock *hlock;
2361 unsigned int depth;
2364 * Pop off the top of the lock stack:
2366 depth = curr->lockdep_depth - 1;
2367 hlock = curr->held_locks + depth;
2370 * Is the unlock non-nested:
2372 if (hlock->instance != lock)
2373 return lock_release_non_nested(curr, lock, ip);
2374 curr->lockdep_depth--;
2376 if (DEBUG_LOCKS_WARN_ON(!depth && (hlock->prev_chain_key != 0)))
2377 return 0;
2379 curr->curr_chain_key = hlock->prev_chain_key;
2381 #ifdef CONFIG_DEBUG_LOCKDEP
2382 hlock->prev_chain_key = 0;
2383 hlock->class = NULL;
2384 hlock->acquire_ip = 0;
2385 hlock->irq_context = 0;
2386 #endif
2387 return 1;
2391 * Remove the lock to the list of currently held locks - this gets
2392 * called on mutex_unlock()/spin_unlock*() (or on a failed
2393 * mutex_lock_interruptible()). This is done for unlocks that nest
2394 * perfectly. (i.e. the current top of the lock-stack is unlocked)
2396 static void
2397 __lock_release(struct lockdep_map *lock, int nested, unsigned long ip)
2399 struct task_struct *curr = current;
2401 if (!check_unlock(curr, lock, ip))
2402 return;
2404 if (nested) {
2405 if (!lock_release_nested(curr, lock, ip))
2406 return;
2407 } else {
2408 if (!lock_release_non_nested(curr, lock, ip))
2409 return;
2412 check_chain_key(curr);
2416 * Check whether we follow the irq-flags state precisely:
2418 static void check_flags(unsigned long flags)
2420 #if defined(CONFIG_DEBUG_LOCKDEP) && defined(CONFIG_TRACE_IRQFLAGS)
2421 if (!debug_locks)
2422 return;
2424 if (irqs_disabled_flags(flags))
2425 DEBUG_LOCKS_WARN_ON(current->hardirqs_enabled);
2426 else
2427 DEBUG_LOCKS_WARN_ON(!current->hardirqs_enabled);
2430 * We dont accurately track softirq state in e.g.
2431 * hardirq contexts (such as on 4KSTACKS), so only
2432 * check if not in hardirq contexts:
2434 if (!hardirq_count()) {
2435 if (softirq_count())
2436 DEBUG_LOCKS_WARN_ON(current->softirqs_enabled);
2437 else
2438 DEBUG_LOCKS_WARN_ON(!current->softirqs_enabled);
2441 if (!debug_locks)
2442 print_irqtrace_events(current);
2443 #endif
2447 * We are not always called with irqs disabled - do that here,
2448 * and also avoid lockdep recursion:
2450 void lock_acquire(struct lockdep_map *lock, unsigned int subclass,
2451 int trylock, int read, int check, unsigned long ip)
2453 unsigned long flags;
2455 if (unlikely(current->lockdep_recursion))
2456 return;
2458 raw_local_irq_save(flags);
2459 check_flags(flags);
2461 current->lockdep_recursion = 1;
2462 __lock_acquire(lock, subclass, trylock, read, check,
2463 irqs_disabled_flags(flags), ip);
2464 current->lockdep_recursion = 0;
2465 raw_local_irq_restore(flags);
2468 EXPORT_SYMBOL_GPL(lock_acquire);
2470 void lock_release(struct lockdep_map *lock, int nested, unsigned long ip)
2472 unsigned long flags;
2474 if (unlikely(current->lockdep_recursion))
2475 return;
2477 raw_local_irq_save(flags);
2478 check_flags(flags);
2479 current->lockdep_recursion = 1;
2480 __lock_release(lock, nested, ip);
2481 current->lockdep_recursion = 0;
2482 raw_local_irq_restore(flags);
2485 EXPORT_SYMBOL_GPL(lock_release);
2488 * Used by the testsuite, sanitize the validator state
2489 * after a simulated failure:
2492 void lockdep_reset(void)
2494 unsigned long flags;
2495 int i;
2497 raw_local_irq_save(flags);
2498 current->curr_chain_key = 0;
2499 current->lockdep_depth = 0;
2500 current->lockdep_recursion = 0;
2501 memset(current->held_locks, 0, MAX_LOCK_DEPTH*sizeof(struct held_lock));
2502 nr_hardirq_chains = 0;
2503 nr_softirq_chains = 0;
2504 nr_process_chains = 0;
2505 debug_locks = 1;
2506 for (i = 0; i < CHAINHASH_SIZE; i++)
2507 INIT_LIST_HEAD(chainhash_table + i);
2508 raw_local_irq_restore(flags);
2511 static void zap_class(struct lock_class *class)
2513 int i;
2516 * Remove all dependencies this lock is
2517 * involved in:
2519 for (i = 0; i < nr_list_entries; i++) {
2520 if (list_entries[i].class == class)
2521 list_del_rcu(&list_entries[i].entry);
2524 * Unhash the class and remove it from the all_lock_classes list:
2526 list_del_rcu(&class->hash_entry);
2527 list_del_rcu(&class->lock_entry);
2531 static inline int within(void *addr, void *start, unsigned long size)
2533 return addr >= start && addr < start + size;
2536 void lockdep_free_key_range(void *start, unsigned long size)
2538 struct lock_class *class, *next;
2539 struct list_head *head;
2540 unsigned long flags;
2541 int i;
2543 raw_local_irq_save(flags);
2544 graph_lock();
2547 * Unhash all classes that were created by this module:
2549 for (i = 0; i < CLASSHASH_SIZE; i++) {
2550 head = classhash_table + i;
2551 if (list_empty(head))
2552 continue;
2553 list_for_each_entry_safe(class, next, head, hash_entry)
2554 if (within(class->key, start, size))
2555 zap_class(class);
2558 graph_unlock();
2559 raw_local_irq_restore(flags);
2562 void lockdep_reset_lock(struct lockdep_map *lock)
2564 struct lock_class *class, *next;
2565 struct list_head *head;
2566 unsigned long flags;
2567 int i, j;
2569 raw_local_irq_save(flags);
2572 * Remove all classes this lock might have:
2574 for (j = 0; j < MAX_LOCKDEP_SUBCLASSES; j++) {
2576 * If the class exists we look it up and zap it:
2578 class = look_up_lock_class(lock, j);
2579 if (class)
2580 zap_class(class);
2583 * Debug check: in the end all mapped classes should
2584 * be gone.
2586 graph_lock();
2587 for (i = 0; i < CLASSHASH_SIZE; i++) {
2588 head = classhash_table + i;
2589 if (list_empty(head))
2590 continue;
2591 list_for_each_entry_safe(class, next, head, hash_entry) {
2592 if (unlikely(class == lock->class_cache)) {
2593 if (debug_locks_off_graph_unlock())
2594 WARN_ON(1);
2595 goto out_restore;
2599 graph_unlock();
2601 out_restore:
2602 raw_local_irq_restore(flags);
2605 void lockdep_init(void)
2607 int i;
2610 * Some architectures have their own start_kernel()
2611 * code which calls lockdep_init(), while we also
2612 * call lockdep_init() from the start_kernel() itself,
2613 * and we want to initialize the hashes only once:
2615 if (lockdep_initialized)
2616 return;
2618 for (i = 0; i < CLASSHASH_SIZE; i++)
2619 INIT_LIST_HEAD(classhash_table + i);
2621 for (i = 0; i < CHAINHASH_SIZE; i++)
2622 INIT_LIST_HEAD(chainhash_table + i);
2624 lockdep_initialized = 1;
2627 void __init lockdep_info(void)
2629 printk("Lock dependency validator: Copyright (c) 2006 Red Hat, Inc., Ingo Molnar\n");
2631 printk("... MAX_LOCKDEP_SUBCLASSES: %lu\n", MAX_LOCKDEP_SUBCLASSES);
2632 printk("... MAX_LOCK_DEPTH: %lu\n", MAX_LOCK_DEPTH);
2633 printk("... MAX_LOCKDEP_KEYS: %lu\n", MAX_LOCKDEP_KEYS);
2634 printk("... CLASSHASH_SIZE: %lu\n", CLASSHASH_SIZE);
2635 printk("... MAX_LOCKDEP_ENTRIES: %lu\n", MAX_LOCKDEP_ENTRIES);
2636 printk("... MAX_LOCKDEP_CHAINS: %lu\n", MAX_LOCKDEP_CHAINS);
2637 printk("... CHAINHASH_SIZE: %lu\n", CHAINHASH_SIZE);
2639 printk(" memory used by lock dependency info: %lu kB\n",
2640 (sizeof(struct lock_class) * MAX_LOCKDEP_KEYS +
2641 sizeof(struct list_head) * CLASSHASH_SIZE +
2642 sizeof(struct lock_list) * MAX_LOCKDEP_ENTRIES +
2643 sizeof(struct lock_chain) * MAX_LOCKDEP_CHAINS +
2644 sizeof(struct list_head) * CHAINHASH_SIZE) / 1024);
2646 printk(" per task-struct memory footprint: %lu bytes\n",
2647 sizeof(struct held_lock) * MAX_LOCK_DEPTH);
2649 #ifdef CONFIG_DEBUG_LOCKDEP
2650 if (lockdep_init_error)
2651 printk("WARNING: lockdep init error! Arch code didnt call lockdep_init() early enough?\n");
2652 #endif
2655 static inline int in_range(const void *start, const void *addr, const void *end)
2657 return addr >= start && addr <= end;
2660 static void
2661 print_freed_lock_bug(struct task_struct *curr, const void *mem_from,
2662 const void *mem_to, struct held_lock *hlock)
2664 if (!debug_locks_off())
2665 return;
2666 if (debug_locks_silent)
2667 return;
2669 printk("\n=========================\n");
2670 printk( "[ BUG: held lock freed! ]\n");
2671 printk( "-------------------------\n");
2672 printk("%s/%d is freeing memory %p-%p, with a lock still held there!\n",
2673 curr->comm, curr->pid, mem_from, mem_to-1);
2674 print_lock(hlock);
2675 lockdep_print_held_locks(curr);
2677 printk("\nstack backtrace:\n");
2678 dump_stack();
2682 * Called when kernel memory is freed (or unmapped), or if a lock
2683 * is destroyed or reinitialized - this code checks whether there is
2684 * any held lock in the memory range of <from> to <to>:
2686 void debug_check_no_locks_freed(const void *mem_from, unsigned long mem_len)
2688 const void *mem_to = mem_from + mem_len, *lock_from, *lock_to;
2689 struct task_struct *curr = current;
2690 struct held_lock *hlock;
2691 unsigned long flags;
2692 int i;
2694 if (unlikely(!debug_locks))
2695 return;
2697 local_irq_save(flags);
2698 for (i = 0; i < curr->lockdep_depth; i++) {
2699 hlock = curr->held_locks + i;
2701 lock_from = (void *)hlock->instance;
2702 lock_to = (void *)(hlock->instance + 1);
2704 if (!in_range(mem_from, lock_from, mem_to) &&
2705 !in_range(mem_from, lock_to, mem_to))
2706 continue;
2708 print_freed_lock_bug(curr, mem_from, mem_to, hlock);
2709 break;
2711 local_irq_restore(flags);
2713 EXPORT_SYMBOL_GPL(debug_check_no_locks_freed);
2715 static void print_held_locks_bug(struct task_struct *curr)
2717 if (!debug_locks_off())
2718 return;
2719 if (debug_locks_silent)
2720 return;
2722 printk("\n=====================================\n");
2723 printk( "[ BUG: lock held at task exit time! ]\n");
2724 printk( "-------------------------------------\n");
2725 printk("%s/%d is exiting with locks still held!\n",
2726 curr->comm, curr->pid);
2727 lockdep_print_held_locks(curr);
2729 printk("\nstack backtrace:\n");
2730 dump_stack();
2733 void debug_check_no_locks_held(struct task_struct *task)
2735 if (unlikely(task->lockdep_depth > 0))
2736 print_held_locks_bug(task);
2739 void debug_show_all_locks(void)
2741 struct task_struct *g, *p;
2742 int count = 10;
2743 int unlock = 1;
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.
2753 retry:
2754 if (!read_trylock(&tasklist_lock)) {
2755 if (count == 10)
2756 printk("hm, tasklist_lock locked, retrying... ");
2757 if (count) {
2758 count--;
2759 printk(" #%d", 10-count);
2760 mdelay(200);
2761 goto retry;
2763 printk(" ignoring it.\n");
2764 unlock = 0;
2766 if (count != 10)
2767 printk(" locked it.\n");
2769 do_each_thread(g, p) {
2770 if (p->lockdep_depth)
2771 lockdep_print_held_locks(p);
2772 if (!unlock)
2773 if (read_trylock(&tasklist_lock))
2774 unlock = 1;
2775 } while_each_thread(g, p);
2777 printk("\n");
2778 printk("=============================================\n\n");
2780 if (unlock)
2781 read_unlock(&tasklist_lock);
2784 EXPORT_SYMBOL_GPL(debug_show_all_locks);
2786 void debug_show_held_locks(struct task_struct *task)
2788 lockdep_print_held_locks(task);
2791 EXPORT_SYMBOL_GPL(debug_show_held_locks);