[PATCH] i386: Remove lock section support in semaphore.h
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / kernel / lockdep.c
blob9bad178845135fba9f6b76ac59d408b723c5b5bc
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>
40 #include <asm/sections.h>
42 #include "lockdep_internals.h"
45 * hash_lock: protects the lockdep hashes and class/list/hash allocators.
47 * This is one of the rare exceptions where it's justified
48 * to use a raw spinlock - we really dont want the spinlock
49 * code to recurse back into the lockdep code.
51 static raw_spinlock_t hash_lock = (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED;
53 static int lockdep_initialized;
55 unsigned long nr_list_entries;
56 static struct lock_list list_entries[MAX_LOCKDEP_ENTRIES];
59 * Allocate a lockdep entry. (assumes hash_lock held, returns
60 * with NULL on failure)
62 static struct lock_list *alloc_list_entry(void)
64 if (nr_list_entries >= MAX_LOCKDEP_ENTRIES) {
65 __raw_spin_unlock(&hash_lock);
66 debug_locks_off();
67 printk("BUG: MAX_LOCKDEP_ENTRIES too low!\n");
68 printk("turning off the locking correctness validator.\n");
69 return NULL;
71 return list_entries + nr_list_entries++;
75 * All data structures here are protected by the global debug_lock.
77 * Mutex key structs only get allocated, once during bootup, and never
78 * get freed - this significantly simplifies the debugging code.
80 unsigned long nr_lock_classes;
81 static struct lock_class lock_classes[MAX_LOCKDEP_KEYS];
84 * We keep a global list of all lock classes. The list only grows,
85 * never shrinks. The list is only accessed with the lockdep
86 * spinlock lock held.
88 LIST_HEAD(all_lock_classes);
91 * The lockdep classes are in a hash-table as well, for fast lookup:
93 #define CLASSHASH_BITS (MAX_LOCKDEP_KEYS_BITS - 1)
94 #define CLASSHASH_SIZE (1UL << CLASSHASH_BITS)
95 #define CLASSHASH_MASK (CLASSHASH_SIZE - 1)
96 #define __classhashfn(key) ((((unsigned long)key >> CLASSHASH_BITS) + (unsigned long)key) & CLASSHASH_MASK)
97 #define classhashentry(key) (classhash_table + __classhashfn((key)))
99 static struct list_head classhash_table[CLASSHASH_SIZE];
101 unsigned long nr_lock_chains;
102 static struct lock_chain lock_chains[MAX_LOCKDEP_CHAINS];
105 * We put the lock dependency chains into a hash-table as well, to cache
106 * their existence:
108 #define CHAINHASH_BITS (MAX_LOCKDEP_CHAINS_BITS-1)
109 #define CHAINHASH_SIZE (1UL << CHAINHASH_BITS)
110 #define CHAINHASH_MASK (CHAINHASH_SIZE - 1)
111 #define __chainhashfn(chain) \
112 (((chain >> CHAINHASH_BITS) + chain) & CHAINHASH_MASK)
113 #define chainhashentry(chain) (chainhash_table + __chainhashfn((chain)))
115 static struct list_head chainhash_table[CHAINHASH_SIZE];
118 * The hash key of the lock dependency chains is a hash itself too:
119 * it's a hash of all locks taken up to that lock, including that lock.
120 * It's a 64-bit hash, because it's important for the keys to be
121 * unique.
123 #define iterate_chain_key(key1, key2) \
124 (((key1) << MAX_LOCKDEP_KEYS_BITS/2) ^ \
125 ((key1) >> (64-MAX_LOCKDEP_KEYS_BITS/2)) ^ \
126 (key2))
128 void lockdep_off(void)
130 current->lockdep_recursion++;
133 EXPORT_SYMBOL(lockdep_off);
135 void lockdep_on(void)
137 current->lockdep_recursion--;
140 EXPORT_SYMBOL(lockdep_on);
142 int lockdep_internal(void)
144 return current->lockdep_recursion != 0;
147 EXPORT_SYMBOL(lockdep_internal);
150 * Debugging switches:
153 #define VERBOSE 0
154 #ifdef VERBOSE
155 # define VERY_VERBOSE 0
156 #endif
158 #if VERBOSE
159 # define HARDIRQ_VERBOSE 1
160 # define SOFTIRQ_VERBOSE 1
161 #else
162 # define HARDIRQ_VERBOSE 0
163 # define SOFTIRQ_VERBOSE 0
164 #endif
166 #if VERBOSE || HARDIRQ_VERBOSE || SOFTIRQ_VERBOSE
168 * Quick filtering for interesting events:
170 static int class_filter(struct lock_class *class)
172 #if 0
173 /* Example */
174 if (class->name_version == 1 &&
175 !strcmp(class->name, "lockname"))
176 return 1;
177 if (class->name_version == 1 &&
178 !strcmp(class->name, "&struct->lockfield"))
179 return 1;
180 #endif
181 /* Allow everything else. 0 would be filter everything else */
182 return 1;
184 #endif
186 static int verbose(struct lock_class *class)
188 #if VERBOSE
189 return class_filter(class);
190 #endif
191 return 0;
194 #ifdef CONFIG_TRACE_IRQFLAGS
196 static int hardirq_verbose(struct lock_class *class)
198 #if HARDIRQ_VERBOSE
199 return class_filter(class);
200 #endif
201 return 0;
204 static int softirq_verbose(struct lock_class *class)
206 #if SOFTIRQ_VERBOSE
207 return class_filter(class);
208 #endif
209 return 0;
212 #endif
215 * Stack-trace: tightly packed array of stack backtrace
216 * addresses. Protected by the hash_lock.
218 unsigned long nr_stack_trace_entries;
219 static unsigned long stack_trace[MAX_STACK_TRACE_ENTRIES];
221 static int save_trace(struct stack_trace *trace)
223 trace->nr_entries = 0;
224 trace->max_entries = MAX_STACK_TRACE_ENTRIES - nr_stack_trace_entries;
225 trace->entries = stack_trace + nr_stack_trace_entries;
227 save_stack_trace(trace, NULL, 0, 3);
229 trace->max_entries = trace->nr_entries;
231 nr_stack_trace_entries += trace->nr_entries;
232 if (DEBUG_LOCKS_WARN_ON(nr_stack_trace_entries > MAX_STACK_TRACE_ENTRIES))
233 return 0;
235 if (nr_stack_trace_entries == MAX_STACK_TRACE_ENTRIES) {
236 __raw_spin_unlock(&hash_lock);
237 if (debug_locks_off()) {
238 printk("BUG: MAX_STACK_TRACE_ENTRIES too low!\n");
239 printk("turning off the locking correctness validator.\n");
240 dump_stack();
242 return 0;
245 return 1;
248 unsigned int nr_hardirq_chains;
249 unsigned int nr_softirq_chains;
250 unsigned int nr_process_chains;
251 unsigned int max_lockdep_depth;
252 unsigned int max_recursion_depth;
254 #ifdef CONFIG_DEBUG_LOCKDEP
256 * We cannot printk in early bootup code. Not even early_printk()
257 * might work. So we mark any initialization errors and printk
258 * about it later on, in lockdep_info().
260 static int lockdep_init_error;
263 * Various lockdep statistics:
265 atomic_t chain_lookup_hits;
266 atomic_t chain_lookup_misses;
267 atomic_t hardirqs_on_events;
268 atomic_t hardirqs_off_events;
269 atomic_t redundant_hardirqs_on;
270 atomic_t redundant_hardirqs_off;
271 atomic_t softirqs_on_events;
272 atomic_t softirqs_off_events;
273 atomic_t redundant_softirqs_on;
274 atomic_t redundant_softirqs_off;
275 atomic_t nr_unused_locks;
276 atomic_t nr_cyclic_checks;
277 atomic_t nr_cyclic_check_recursions;
278 atomic_t nr_find_usage_forwards_checks;
279 atomic_t nr_find_usage_forwards_recursions;
280 atomic_t nr_find_usage_backwards_checks;
281 atomic_t nr_find_usage_backwards_recursions;
282 # define debug_atomic_inc(ptr) atomic_inc(ptr)
283 # define debug_atomic_dec(ptr) atomic_dec(ptr)
284 # define debug_atomic_read(ptr) atomic_read(ptr)
285 #else
286 # define debug_atomic_inc(ptr) do { } while (0)
287 # define debug_atomic_dec(ptr) do { } while (0)
288 # define debug_atomic_read(ptr) 0
289 #endif
292 * Locking printouts:
295 static const char *usage_str[] =
297 [LOCK_USED] = "initial-use ",
298 [LOCK_USED_IN_HARDIRQ] = "in-hardirq-W",
299 [LOCK_USED_IN_SOFTIRQ] = "in-softirq-W",
300 [LOCK_ENABLED_SOFTIRQS] = "softirq-on-W",
301 [LOCK_ENABLED_HARDIRQS] = "hardirq-on-W",
302 [LOCK_USED_IN_HARDIRQ_READ] = "in-hardirq-R",
303 [LOCK_USED_IN_SOFTIRQ_READ] = "in-softirq-R",
304 [LOCK_ENABLED_SOFTIRQS_READ] = "softirq-on-R",
305 [LOCK_ENABLED_HARDIRQS_READ] = "hardirq-on-R",
308 const char * __get_key_name(struct lockdep_subclass_key *key, char *str)
310 unsigned long offs, size;
311 char *modname;
313 return kallsyms_lookup((unsigned long)key, &size, &offs, &modname, str);
316 void
317 get_usage_chars(struct lock_class *class, char *c1, char *c2, char *c3, char *c4)
319 *c1 = '.', *c2 = '.', *c3 = '.', *c4 = '.';
321 if (class->usage_mask & LOCKF_USED_IN_HARDIRQ)
322 *c1 = '+';
323 else
324 if (class->usage_mask & LOCKF_ENABLED_HARDIRQS)
325 *c1 = '-';
327 if (class->usage_mask & LOCKF_USED_IN_SOFTIRQ)
328 *c2 = '+';
329 else
330 if (class->usage_mask & LOCKF_ENABLED_SOFTIRQS)
331 *c2 = '-';
333 if (class->usage_mask & LOCKF_ENABLED_HARDIRQS_READ)
334 *c3 = '-';
335 if (class->usage_mask & LOCKF_USED_IN_HARDIRQ_READ) {
336 *c3 = '+';
337 if (class->usage_mask & LOCKF_ENABLED_HARDIRQS_READ)
338 *c3 = '?';
341 if (class->usage_mask & LOCKF_ENABLED_SOFTIRQS_READ)
342 *c4 = '-';
343 if (class->usage_mask & LOCKF_USED_IN_SOFTIRQ_READ) {
344 *c4 = '+';
345 if (class->usage_mask & LOCKF_ENABLED_SOFTIRQS_READ)
346 *c4 = '?';
350 static void print_lock_name(struct lock_class *class)
352 char str[128], c1, c2, c3, c4;
353 const char *name;
355 get_usage_chars(class, &c1, &c2, &c3, &c4);
357 name = class->name;
358 if (!name) {
359 name = __get_key_name(class->key, str);
360 printk(" (%s", name);
361 } else {
362 printk(" (%s", name);
363 if (class->name_version > 1)
364 printk("#%d", class->name_version);
365 if (class->subclass)
366 printk("/%d", class->subclass);
368 printk("){%c%c%c%c}", c1, c2, c3, c4);
371 static void print_lockdep_cache(struct lockdep_map *lock)
373 const char *name;
374 char str[128];
376 name = lock->name;
377 if (!name)
378 name = __get_key_name(lock->key->subkeys, str);
380 printk("%s", name);
383 static void print_lock(struct held_lock *hlock)
385 print_lock_name(hlock->class);
386 printk(", at: ");
387 print_ip_sym(hlock->acquire_ip);
390 static void lockdep_print_held_locks(struct task_struct *curr)
392 int i, depth = curr->lockdep_depth;
394 if (!depth) {
395 printk("no locks held by %s/%d.\n", curr->comm, curr->pid);
396 return;
398 printk("%d lock%s held by %s/%d:\n",
399 depth, depth > 1 ? "s" : "", curr->comm, curr->pid);
401 for (i = 0; i < depth; i++) {
402 printk(" #%d: ", i);
403 print_lock(curr->held_locks + i);
407 static void print_lock_class_header(struct lock_class *class, int depth)
409 int bit;
411 printk("%*s->", depth, "");
412 print_lock_name(class);
413 printk(" ops: %lu", class->ops);
414 printk(" {\n");
416 for (bit = 0; bit < LOCK_USAGE_STATES; bit++) {
417 if (class->usage_mask & (1 << bit)) {
418 int len = depth;
420 len += printk("%*s %s", depth, "", usage_str[bit]);
421 len += printk(" at:\n");
422 print_stack_trace(class->usage_traces + bit, len);
425 printk("%*s }\n", depth, "");
427 printk("%*s ... key at: ",depth,"");
428 print_ip_sym((unsigned long)class->key);
432 * printk all lock dependencies starting at <entry>:
434 static void print_lock_dependencies(struct lock_class *class, int depth)
436 struct lock_list *entry;
438 if (DEBUG_LOCKS_WARN_ON(depth >= 20))
439 return;
441 print_lock_class_header(class, depth);
443 list_for_each_entry(entry, &class->locks_after, entry) {
444 DEBUG_LOCKS_WARN_ON(!entry->class);
445 print_lock_dependencies(entry->class, depth + 1);
447 printk("%*s ... acquired at:\n",depth,"");
448 print_stack_trace(&entry->trace, 2);
449 printk("\n");
454 * Add a new dependency to the head of the list:
456 static int add_lock_to_list(struct lock_class *class, struct lock_class *this,
457 struct list_head *head, unsigned long ip)
459 struct lock_list *entry;
461 * Lock not present yet - get a new dependency struct and
462 * add it to the list:
464 entry = alloc_list_entry();
465 if (!entry)
466 return 0;
468 entry->class = this;
469 save_trace(&entry->trace);
472 * Since we never remove from the dependency list, the list can
473 * be walked lockless by other CPUs, it's only allocation
474 * that must be protected by the spinlock. But this also means
475 * we must make new entries visible only once writes to the
476 * entry become visible - hence the RCU op:
478 list_add_tail_rcu(&entry->entry, head);
480 return 1;
484 * Recursive, forwards-direction lock-dependency checking, used for
485 * both noncyclic checking and for hardirq-unsafe/softirq-unsafe
486 * checking.
488 * (to keep the stackframe of the recursive functions small we
489 * use these global variables, and we also mark various helper
490 * functions as noinline.)
492 static struct held_lock *check_source, *check_target;
495 * Print a dependency chain entry (this is only done when a deadlock
496 * has been detected):
498 static noinline int
499 print_circular_bug_entry(struct lock_list *target, unsigned int depth)
501 if (debug_locks_silent)
502 return 0;
503 printk("\n-> #%u", depth);
504 print_lock_name(target->class);
505 printk(":\n");
506 print_stack_trace(&target->trace, 6);
508 return 0;
512 * When a circular dependency is detected, print the
513 * header first:
515 static noinline int
516 print_circular_bug_header(struct lock_list *entry, unsigned int depth)
518 struct task_struct *curr = current;
520 __raw_spin_unlock(&hash_lock);
521 debug_locks_off();
522 if (debug_locks_silent)
523 return 0;
525 printk("\n=======================================================\n");
526 printk( "[ INFO: possible circular locking dependency detected ]\n");
527 printk( "-------------------------------------------------------\n");
528 printk("%s/%d is trying to acquire lock:\n",
529 curr->comm, curr->pid);
530 print_lock(check_source);
531 printk("\nbut task is already holding lock:\n");
532 print_lock(check_target);
533 printk("\nwhich lock already depends on the new lock.\n\n");
534 printk("\nthe existing dependency chain (in reverse order) is:\n");
536 print_circular_bug_entry(entry, depth);
538 return 0;
541 static noinline int print_circular_bug_tail(void)
543 struct task_struct *curr = current;
544 struct lock_list this;
546 if (debug_locks_silent)
547 return 0;
549 this.class = check_source->class;
550 save_trace(&this.trace);
551 print_circular_bug_entry(&this, 0);
553 printk("\nother info that might help us debug this:\n\n");
554 lockdep_print_held_locks(curr);
556 printk("\nstack backtrace:\n");
557 dump_stack();
559 return 0;
562 static int noinline print_infinite_recursion_bug(void)
564 __raw_spin_unlock(&hash_lock);
565 DEBUG_LOCKS_WARN_ON(1);
567 return 0;
571 * Prove that the dependency graph starting at <entry> can not
572 * lead to <target>. Print an error and return 0 if it does.
574 static noinline int
575 check_noncircular(struct lock_class *source, unsigned int depth)
577 struct lock_list *entry;
579 debug_atomic_inc(&nr_cyclic_check_recursions);
580 if (depth > max_recursion_depth)
581 max_recursion_depth = depth;
582 if (depth >= 20)
583 return print_infinite_recursion_bug();
585 * Check this lock's dependency list:
587 list_for_each_entry(entry, &source->locks_after, entry) {
588 if (entry->class == check_target->class)
589 return print_circular_bug_header(entry, depth+1);
590 debug_atomic_inc(&nr_cyclic_checks);
591 if (!check_noncircular(entry->class, depth+1))
592 return print_circular_bug_entry(entry, depth+1);
594 return 1;
597 static int very_verbose(struct lock_class *class)
599 #if VERY_VERBOSE
600 return class_filter(class);
601 #endif
602 return 0;
604 #ifdef CONFIG_TRACE_IRQFLAGS
607 * Forwards and backwards subgraph searching, for the purposes of
608 * proving that two subgraphs can be connected by a new dependency
609 * without creating any illegal irq-safe -> irq-unsafe lock dependency.
611 static enum lock_usage_bit find_usage_bit;
612 static struct lock_class *forwards_match, *backwards_match;
615 * Find a node in the forwards-direction dependency sub-graph starting
616 * at <source> that matches <find_usage_bit>.
618 * Return 2 if such a node exists in the subgraph, and put that node
619 * into <forwards_match>.
621 * Return 1 otherwise and keep <forwards_match> unchanged.
622 * Return 0 on error.
624 static noinline int
625 find_usage_forwards(struct lock_class *source, unsigned int depth)
627 struct lock_list *entry;
628 int ret;
630 if (depth > max_recursion_depth)
631 max_recursion_depth = depth;
632 if (depth >= 20)
633 return print_infinite_recursion_bug();
635 debug_atomic_inc(&nr_find_usage_forwards_checks);
636 if (source->usage_mask & (1 << find_usage_bit)) {
637 forwards_match = source;
638 return 2;
642 * Check this lock's dependency list:
644 list_for_each_entry(entry, &source->locks_after, entry) {
645 debug_atomic_inc(&nr_find_usage_forwards_recursions);
646 ret = find_usage_forwards(entry->class, depth+1);
647 if (ret == 2 || ret == 0)
648 return ret;
650 return 1;
654 * Find a node in the backwards-direction dependency sub-graph starting
655 * at <source> that matches <find_usage_bit>.
657 * Return 2 if such a node exists in the subgraph, and put that node
658 * into <backwards_match>.
660 * Return 1 otherwise and keep <backwards_match> unchanged.
661 * Return 0 on error.
663 static noinline int
664 find_usage_backwards(struct lock_class *source, unsigned int depth)
666 struct lock_list *entry;
667 int ret;
669 if (depth > max_recursion_depth)
670 max_recursion_depth = depth;
671 if (depth >= 20)
672 return print_infinite_recursion_bug();
674 debug_atomic_inc(&nr_find_usage_backwards_checks);
675 if (source->usage_mask & (1 << find_usage_bit)) {
676 backwards_match = source;
677 return 2;
681 * Check this lock's dependency list:
683 list_for_each_entry(entry, &source->locks_before, entry) {
684 debug_atomic_inc(&nr_find_usage_backwards_recursions);
685 ret = find_usage_backwards(entry->class, depth+1);
686 if (ret == 2 || ret == 0)
687 return ret;
689 return 1;
692 static int
693 print_bad_irq_dependency(struct task_struct *curr,
694 struct held_lock *prev,
695 struct held_lock *next,
696 enum lock_usage_bit bit1,
697 enum lock_usage_bit bit2,
698 const char *irqclass)
700 __raw_spin_unlock(&hash_lock);
701 debug_locks_off();
702 if (debug_locks_silent)
703 return 0;
705 printk("\n======================================================\n");
706 printk( "[ INFO: %s-safe -> %s-unsafe lock order detected ]\n",
707 irqclass, irqclass);
708 printk( "------------------------------------------------------\n");
709 printk("%s/%d [HC%u[%lu]:SC%u[%lu]:HE%u:SE%u] is trying to acquire:\n",
710 curr->comm, curr->pid,
711 curr->hardirq_context, hardirq_count() >> HARDIRQ_SHIFT,
712 curr->softirq_context, softirq_count() >> SOFTIRQ_SHIFT,
713 curr->hardirqs_enabled,
714 curr->softirqs_enabled);
715 print_lock(next);
717 printk("\nand this task is already holding:\n");
718 print_lock(prev);
719 printk("which would create a new lock dependency:\n");
720 print_lock_name(prev->class);
721 printk(" ->");
722 print_lock_name(next->class);
723 printk("\n");
725 printk("\nbut this new dependency connects a %s-irq-safe lock:\n",
726 irqclass);
727 print_lock_name(backwards_match);
728 printk("\n... which became %s-irq-safe at:\n", irqclass);
730 print_stack_trace(backwards_match->usage_traces + bit1, 1);
732 printk("\nto a %s-irq-unsafe lock:\n", irqclass);
733 print_lock_name(forwards_match);
734 printk("\n... which became %s-irq-unsafe at:\n", irqclass);
735 printk("...");
737 print_stack_trace(forwards_match->usage_traces + bit2, 1);
739 printk("\nother info that might help us debug this:\n\n");
740 lockdep_print_held_locks(curr);
742 printk("\nthe %s-irq-safe lock's dependencies:\n", irqclass);
743 print_lock_dependencies(backwards_match, 0);
745 printk("\nthe %s-irq-unsafe lock's dependencies:\n", irqclass);
746 print_lock_dependencies(forwards_match, 0);
748 printk("\nstack backtrace:\n");
749 dump_stack();
751 return 0;
754 static int
755 check_usage(struct task_struct *curr, struct held_lock *prev,
756 struct held_lock *next, enum lock_usage_bit bit_backwards,
757 enum lock_usage_bit bit_forwards, const char *irqclass)
759 int ret;
761 find_usage_bit = bit_backwards;
762 /* fills in <backwards_match> */
763 ret = find_usage_backwards(prev->class, 0);
764 if (!ret || ret == 1)
765 return ret;
767 find_usage_bit = bit_forwards;
768 ret = find_usage_forwards(next->class, 0);
769 if (!ret || ret == 1)
770 return ret;
771 /* ret == 2 */
772 return print_bad_irq_dependency(curr, prev, next,
773 bit_backwards, bit_forwards, irqclass);
776 #endif
778 static int
779 print_deadlock_bug(struct task_struct *curr, struct held_lock *prev,
780 struct held_lock *next)
782 debug_locks_off();
783 __raw_spin_unlock(&hash_lock);
784 if (debug_locks_silent)
785 return 0;
787 printk("\n=============================================\n");
788 printk( "[ INFO: possible recursive locking detected ]\n");
789 printk( "---------------------------------------------\n");
790 printk("%s/%d is trying to acquire lock:\n",
791 curr->comm, curr->pid);
792 print_lock(next);
793 printk("\nbut task is already holding lock:\n");
794 print_lock(prev);
796 printk("\nother info that might help us debug this:\n");
797 lockdep_print_held_locks(curr);
799 printk("\nstack backtrace:\n");
800 dump_stack();
802 return 0;
806 * Check whether we are holding such a class already.
808 * (Note that this has to be done separately, because the graph cannot
809 * detect such classes of deadlocks.)
811 * Returns: 0 on deadlock detected, 1 on OK, 2 on recursive read
813 static int
814 check_deadlock(struct task_struct *curr, struct held_lock *next,
815 struct lockdep_map *next_instance, int read)
817 struct held_lock *prev;
818 int i;
820 for (i = 0; i < curr->lockdep_depth; i++) {
821 prev = curr->held_locks + i;
822 if (prev->class != next->class)
823 continue;
825 * Allow read-after-read recursion of the same
826 * lock class (i.e. read_lock(lock)+read_lock(lock)):
828 if ((read == 2) && prev->read)
829 return 2;
830 return print_deadlock_bug(curr, prev, next);
832 return 1;
836 * There was a chain-cache miss, and we are about to add a new dependency
837 * to a previous lock. We recursively validate the following rules:
839 * - would the adding of the <prev> -> <next> dependency create a
840 * circular dependency in the graph? [== circular deadlock]
842 * - does the new prev->next dependency connect any hardirq-safe lock
843 * (in the full backwards-subgraph starting at <prev>) with any
844 * hardirq-unsafe lock (in the full forwards-subgraph starting at
845 * <next>)? [== illegal lock inversion with hardirq contexts]
847 * - does the new prev->next dependency connect any softirq-safe lock
848 * (in the full backwards-subgraph starting at <prev>) with any
849 * softirq-unsafe lock (in the full forwards-subgraph starting at
850 * <next>)? [== illegal lock inversion with softirq contexts]
852 * any of these scenarios could lead to a deadlock.
854 * Then if all the validations pass, we add the forwards and backwards
855 * dependency.
857 static int
858 check_prev_add(struct task_struct *curr, struct held_lock *prev,
859 struct held_lock *next)
861 struct lock_list *entry;
862 int ret;
865 * Prove that the new <prev> -> <next> dependency would not
866 * create a circular dependency in the graph. (We do this by
867 * forward-recursing into the graph starting at <next>, and
868 * checking whether we can reach <prev>.)
870 * We are using global variables to control the recursion, to
871 * keep the stackframe size of the recursive functions low:
873 check_source = next;
874 check_target = prev;
875 if (!(check_noncircular(next->class, 0)))
876 return print_circular_bug_tail();
878 #ifdef CONFIG_TRACE_IRQFLAGS
880 * Prove that the new dependency does not connect a hardirq-safe
881 * lock with a hardirq-unsafe lock - to achieve this we search
882 * the backwards-subgraph starting at <prev>, and the
883 * forwards-subgraph starting at <next>:
885 if (!check_usage(curr, prev, next, LOCK_USED_IN_HARDIRQ,
886 LOCK_ENABLED_HARDIRQS, "hard"))
887 return 0;
890 * Prove that the new dependency does not connect a hardirq-safe-read
891 * lock with a hardirq-unsafe lock - to achieve this we search
892 * the backwards-subgraph starting at <prev>, and the
893 * forwards-subgraph starting at <next>:
895 if (!check_usage(curr, prev, next, LOCK_USED_IN_HARDIRQ_READ,
896 LOCK_ENABLED_HARDIRQS, "hard-read"))
897 return 0;
900 * Prove that the new dependency does not connect a softirq-safe
901 * lock with a softirq-unsafe lock - to achieve this we search
902 * the backwards-subgraph starting at <prev>, and the
903 * forwards-subgraph starting at <next>:
905 if (!check_usage(curr, prev, next, LOCK_USED_IN_SOFTIRQ,
906 LOCK_ENABLED_SOFTIRQS, "soft"))
907 return 0;
909 * Prove that the new dependency does not connect a softirq-safe-read
910 * lock with a softirq-unsafe lock - to achieve this we search
911 * the backwards-subgraph starting at <prev>, and the
912 * forwards-subgraph starting at <next>:
914 if (!check_usage(curr, prev, next, LOCK_USED_IN_SOFTIRQ_READ,
915 LOCK_ENABLED_SOFTIRQS, "soft"))
916 return 0;
917 #endif
919 * For recursive read-locks we do all the dependency checks,
920 * but we dont store read-triggered dependencies (only
921 * write-triggered dependencies). This ensures that only the
922 * write-side dependencies matter, and that if for example a
923 * write-lock never takes any other locks, then the reads are
924 * equivalent to a NOP.
926 if (next->read == 2 || prev->read == 2)
927 return 1;
929 * Is the <prev> -> <next> dependency already present?
931 * (this may occur even though this is a new chain: consider
932 * e.g. the L1 -> L2 -> L3 -> L4 and the L5 -> L1 -> L2 -> L3
933 * chains - the second one will be new, but L1 already has
934 * L2 added to its dependency list, due to the first chain.)
936 list_for_each_entry(entry, &prev->class->locks_after, entry) {
937 if (entry->class == next->class)
938 return 2;
942 * Ok, all validations passed, add the new lock
943 * to the previous lock's dependency list:
945 ret = add_lock_to_list(prev->class, next->class,
946 &prev->class->locks_after, next->acquire_ip);
947 if (!ret)
948 return 0;
950 * Return value of 2 signals 'dependency already added',
951 * in that case we dont have to add the backlink either.
953 if (ret == 2)
954 return 2;
955 ret = add_lock_to_list(next->class, prev->class,
956 &next->class->locks_before, next->acquire_ip);
959 * Debugging printouts:
961 if (verbose(prev->class) || verbose(next->class)) {
962 __raw_spin_unlock(&hash_lock);
963 printk("\n new dependency: ");
964 print_lock_name(prev->class);
965 printk(" => ");
966 print_lock_name(next->class);
967 printk("\n");
968 dump_stack();
969 __raw_spin_lock(&hash_lock);
971 return 1;
975 * Add the dependency to all directly-previous locks that are 'relevant'.
976 * The ones that are relevant are (in increasing distance from curr):
977 * all consecutive trylock entries and the final non-trylock entry - or
978 * the end of this context's lock-chain - whichever comes first.
980 static int
981 check_prevs_add(struct task_struct *curr, struct held_lock *next)
983 int depth = curr->lockdep_depth;
984 struct held_lock *hlock;
987 * Debugging checks.
989 * Depth must not be zero for a non-head lock:
991 if (!depth)
992 goto out_bug;
994 * At least two relevant locks must exist for this
995 * to be a head:
997 if (curr->held_locks[depth].irq_context !=
998 curr->held_locks[depth-1].irq_context)
999 goto out_bug;
1001 for (;;) {
1002 hlock = curr->held_locks + depth-1;
1004 * Only non-recursive-read entries get new dependencies
1005 * added:
1007 if (hlock->read != 2) {
1008 check_prev_add(curr, hlock, next);
1010 * Stop after the first non-trylock entry,
1011 * as non-trylock entries have added their
1012 * own direct dependencies already, so this
1013 * lock is connected to them indirectly:
1015 if (!hlock->trylock)
1016 break;
1018 depth--;
1020 * End of lock-stack?
1022 if (!depth)
1023 break;
1025 * Stop the search if we cross into another context:
1027 if (curr->held_locks[depth].irq_context !=
1028 curr->held_locks[depth-1].irq_context)
1029 break;
1031 return 1;
1032 out_bug:
1033 __raw_spin_unlock(&hash_lock);
1034 DEBUG_LOCKS_WARN_ON(1);
1036 return 0;
1041 * Is this the address of a static object:
1043 static int static_obj(void *obj)
1045 unsigned long start = (unsigned long) &_stext,
1046 end = (unsigned long) &_end,
1047 addr = (unsigned long) obj;
1048 #ifdef CONFIG_SMP
1049 int i;
1050 #endif
1053 * static variable?
1055 if ((addr >= start) && (addr < end))
1056 return 1;
1058 #ifdef CONFIG_SMP
1060 * percpu var?
1062 for_each_possible_cpu(i) {
1063 start = (unsigned long) &__per_cpu_start + per_cpu_offset(i);
1064 end = (unsigned long) &__per_cpu_end + per_cpu_offset(i);
1066 if ((addr >= start) && (addr < end))
1067 return 1;
1069 #endif
1072 * module var?
1074 return is_module_address(addr);
1078 * To make lock name printouts unique, we calculate a unique
1079 * class->name_version generation counter:
1081 static int count_matching_names(struct lock_class *new_class)
1083 struct lock_class *class;
1084 int count = 0;
1086 if (!new_class->name)
1087 return 0;
1089 list_for_each_entry(class, &all_lock_classes, lock_entry) {
1090 if (new_class->key - new_class->subclass == class->key)
1091 return class->name_version;
1092 if (class->name && !strcmp(class->name, new_class->name))
1093 count = max(count, class->name_version);
1096 return count + 1;
1099 extern void __error_too_big_MAX_LOCKDEP_SUBCLASSES(void);
1102 * Register a lock's class in the hash-table, if the class is not present
1103 * yet. Otherwise we look it up. We cache the result in the lock object
1104 * itself, so actual lookup of the hash should be once per lock object.
1106 static inline struct lock_class *
1107 look_up_lock_class(struct lockdep_map *lock, unsigned int subclass)
1109 struct lockdep_subclass_key *key;
1110 struct list_head *hash_head;
1111 struct lock_class *class;
1113 #ifdef CONFIG_DEBUG_LOCKDEP
1115 * If the architecture calls into lockdep before initializing
1116 * the hashes then we'll warn about it later. (we cannot printk
1117 * right now)
1119 if (unlikely(!lockdep_initialized)) {
1120 lockdep_init();
1121 lockdep_init_error = 1;
1123 #endif
1126 * Static locks do not have their class-keys yet - for them the key
1127 * is the lock object itself:
1129 if (unlikely(!lock->key))
1130 lock->key = (void *)lock;
1133 * NOTE: the class-key must be unique. For dynamic locks, a static
1134 * lock_class_key variable is passed in through the mutex_init()
1135 * (or spin_lock_init()) call - which acts as the key. For static
1136 * locks we use the lock object itself as the key.
1138 if (sizeof(struct lock_class_key) > sizeof(struct lock_class))
1139 __error_too_big_MAX_LOCKDEP_SUBCLASSES();
1141 key = lock->key->subkeys + subclass;
1143 hash_head = classhashentry(key);
1146 * We can walk the hash lockfree, because the hash only
1147 * grows, and we are careful when adding entries to the end:
1149 list_for_each_entry(class, hash_head, hash_entry)
1150 if (class->key == key)
1151 return class;
1153 return NULL;
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 register_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 class = look_up_lock_class(lock, subclass);
1169 if (likely(class))
1170 return class;
1173 * Debug-check: all keys must be persistent!
1175 if (!static_obj(lock->key)) {
1176 debug_locks_off();
1177 printk("INFO: trying to register non-static key.\n");
1178 printk("the code is fine but needs lockdep annotation.\n");
1179 printk("turning off the locking correctness validator.\n");
1180 dump_stack();
1182 return NULL;
1185 key = lock->key->subkeys + subclass;
1186 hash_head = classhashentry(key);
1188 __raw_spin_lock(&hash_lock);
1190 * We have to do the hash-walk again, to avoid races
1191 * with another CPU:
1193 list_for_each_entry(class, hash_head, hash_entry)
1194 if (class->key == key)
1195 goto out_unlock_set;
1197 * Allocate a new key from the static array, and add it to
1198 * the hash:
1200 if (nr_lock_classes >= MAX_LOCKDEP_KEYS) {
1201 __raw_spin_unlock(&hash_lock);
1202 debug_locks_off();
1203 printk("BUG: MAX_LOCKDEP_KEYS too low!\n");
1204 printk("turning off the locking correctness validator.\n");
1205 return NULL;
1207 class = lock_classes + nr_lock_classes++;
1208 debug_atomic_inc(&nr_unused_locks);
1209 class->key = key;
1210 class->name = lock->name;
1211 class->subclass = subclass;
1212 INIT_LIST_HEAD(&class->lock_entry);
1213 INIT_LIST_HEAD(&class->locks_before);
1214 INIT_LIST_HEAD(&class->locks_after);
1215 class->name_version = count_matching_names(class);
1217 * We use RCU's safe list-add method to make
1218 * parallel walking of the hash-list safe:
1220 list_add_tail_rcu(&class->hash_entry, hash_head);
1222 if (verbose(class)) {
1223 __raw_spin_unlock(&hash_lock);
1224 printk("\nnew class %p: %s", class->key, class->name);
1225 if (class->name_version > 1)
1226 printk("#%d", class->name_version);
1227 printk("\n");
1228 dump_stack();
1229 __raw_spin_lock(&hash_lock);
1231 out_unlock_set:
1232 __raw_spin_unlock(&hash_lock);
1234 if (!subclass)
1235 lock->class_cache = class;
1237 DEBUG_LOCKS_WARN_ON(class->subclass != subclass);
1239 return class;
1243 * Look up a dependency chain. If the key is not present yet then
1244 * add it and return 0 - in this case the new dependency chain is
1245 * validated. If the key is already hashed, return 1.
1247 static inline int lookup_chain_cache(u64 chain_key)
1249 struct list_head *hash_head = chainhashentry(chain_key);
1250 struct lock_chain *chain;
1252 DEBUG_LOCKS_WARN_ON(!irqs_disabled());
1254 * We can walk it lock-free, because entries only get added
1255 * to the hash:
1257 list_for_each_entry(chain, hash_head, entry) {
1258 if (chain->chain_key == chain_key) {
1259 cache_hit:
1260 debug_atomic_inc(&chain_lookup_hits);
1262 * In the debugging case, force redundant checking
1263 * by returning 1:
1265 #ifdef CONFIG_DEBUG_LOCKDEP
1266 __raw_spin_lock(&hash_lock);
1267 return 1;
1268 #endif
1269 return 0;
1273 * Allocate a new chain entry from the static array, and add
1274 * it to the hash:
1276 __raw_spin_lock(&hash_lock);
1278 * We have to walk the chain again locked - to avoid duplicates:
1280 list_for_each_entry(chain, hash_head, entry) {
1281 if (chain->chain_key == chain_key) {
1282 __raw_spin_unlock(&hash_lock);
1283 goto cache_hit;
1286 if (unlikely(nr_lock_chains >= MAX_LOCKDEP_CHAINS)) {
1287 __raw_spin_unlock(&hash_lock);
1288 debug_locks_off();
1289 printk("BUG: MAX_LOCKDEP_CHAINS too low!\n");
1290 printk("turning off the locking correctness validator.\n");
1291 return 0;
1293 chain = lock_chains + nr_lock_chains++;
1294 chain->chain_key = chain_key;
1295 list_add_tail_rcu(&chain->entry, hash_head);
1296 debug_atomic_inc(&chain_lookup_misses);
1297 #ifdef CONFIG_TRACE_IRQFLAGS
1298 if (current->hardirq_context)
1299 nr_hardirq_chains++;
1300 else {
1301 if (current->softirq_context)
1302 nr_softirq_chains++;
1303 else
1304 nr_process_chains++;
1306 #else
1307 nr_process_chains++;
1308 #endif
1310 return 1;
1314 * We are building curr_chain_key incrementally, so double-check
1315 * it from scratch, to make sure that it's done correctly:
1317 static void check_chain_key(struct task_struct *curr)
1319 #ifdef CONFIG_DEBUG_LOCKDEP
1320 struct held_lock *hlock, *prev_hlock = NULL;
1321 unsigned int i, id;
1322 u64 chain_key = 0;
1324 for (i = 0; i < curr->lockdep_depth; i++) {
1325 hlock = curr->held_locks + i;
1326 if (chain_key != hlock->prev_chain_key) {
1327 debug_locks_off();
1328 printk("hm#1, depth: %u [%u], %016Lx != %016Lx\n",
1329 curr->lockdep_depth, i,
1330 (unsigned long long)chain_key,
1331 (unsigned long long)hlock->prev_chain_key);
1332 WARN_ON(1);
1333 return;
1335 id = hlock->class - lock_classes;
1336 DEBUG_LOCKS_WARN_ON(id >= MAX_LOCKDEP_KEYS);
1337 if (prev_hlock && (prev_hlock->irq_context !=
1338 hlock->irq_context))
1339 chain_key = 0;
1340 chain_key = iterate_chain_key(chain_key, id);
1341 prev_hlock = hlock;
1343 if (chain_key != curr->curr_chain_key) {
1344 debug_locks_off();
1345 printk("hm#2, depth: %u [%u], %016Lx != %016Lx\n",
1346 curr->lockdep_depth, i,
1347 (unsigned long long)chain_key,
1348 (unsigned long long)curr->curr_chain_key);
1349 WARN_ON(1);
1351 #endif
1354 #ifdef CONFIG_TRACE_IRQFLAGS
1357 * print irq inversion bug:
1359 static int
1360 print_irq_inversion_bug(struct task_struct *curr, struct lock_class *other,
1361 struct held_lock *this, int forwards,
1362 const char *irqclass)
1364 __raw_spin_unlock(&hash_lock);
1365 debug_locks_off();
1366 if (debug_locks_silent)
1367 return 0;
1369 printk("\n=========================================================\n");
1370 printk( "[ INFO: possible irq lock inversion dependency detected ]\n");
1371 printk( "---------------------------------------------------------\n");
1372 printk("%s/%d just changed the state of lock:\n",
1373 curr->comm, curr->pid);
1374 print_lock(this);
1375 if (forwards)
1376 printk("but this lock took another, %s-irq-unsafe lock in the past:\n", irqclass);
1377 else
1378 printk("but this lock was taken by another, %s-irq-safe lock in the past:\n", irqclass);
1379 print_lock_name(other);
1380 printk("\n\nand interrupts could create inverse lock ordering between them.\n\n");
1382 printk("\nother info that might help us debug this:\n");
1383 lockdep_print_held_locks(curr);
1385 printk("\nthe first lock's dependencies:\n");
1386 print_lock_dependencies(this->class, 0);
1388 printk("\nthe second lock's dependencies:\n");
1389 print_lock_dependencies(other, 0);
1391 printk("\nstack backtrace:\n");
1392 dump_stack();
1394 return 0;
1398 * Prove that in the forwards-direction subgraph starting at <this>
1399 * there is no lock matching <mask>:
1401 static int
1402 check_usage_forwards(struct task_struct *curr, struct held_lock *this,
1403 enum lock_usage_bit bit, const char *irqclass)
1405 int ret;
1407 find_usage_bit = bit;
1408 /* fills in <forwards_match> */
1409 ret = find_usage_forwards(this->class, 0);
1410 if (!ret || ret == 1)
1411 return ret;
1413 return print_irq_inversion_bug(curr, forwards_match, this, 1, irqclass);
1417 * Prove that in the backwards-direction subgraph starting at <this>
1418 * there is no lock matching <mask>:
1420 static int
1421 check_usage_backwards(struct task_struct *curr, struct held_lock *this,
1422 enum lock_usage_bit bit, const char *irqclass)
1424 int ret;
1426 find_usage_bit = bit;
1427 /* fills in <backwards_match> */
1428 ret = find_usage_backwards(this->class, 0);
1429 if (!ret || ret == 1)
1430 return ret;
1432 return print_irq_inversion_bug(curr, backwards_match, this, 0, irqclass);
1435 static inline void print_irqtrace_events(struct task_struct *curr)
1437 printk("irq event stamp: %u\n", curr->irq_events);
1438 printk("hardirqs last enabled at (%u): ", curr->hardirq_enable_event);
1439 print_ip_sym(curr->hardirq_enable_ip);
1440 printk("hardirqs last disabled at (%u): ", curr->hardirq_disable_event);
1441 print_ip_sym(curr->hardirq_disable_ip);
1442 printk("softirqs last enabled at (%u): ", curr->softirq_enable_event);
1443 print_ip_sym(curr->softirq_enable_ip);
1444 printk("softirqs last disabled at (%u): ", curr->softirq_disable_event);
1445 print_ip_sym(curr->softirq_disable_ip);
1448 #else
1449 static inline void print_irqtrace_events(struct task_struct *curr)
1452 #endif
1454 static int
1455 print_usage_bug(struct task_struct *curr, struct held_lock *this,
1456 enum lock_usage_bit prev_bit, enum lock_usage_bit new_bit)
1458 __raw_spin_unlock(&hash_lock);
1459 debug_locks_off();
1460 if (debug_locks_silent)
1461 return 0;
1463 printk("\n=================================\n");
1464 printk( "[ INFO: inconsistent lock state ]\n");
1465 printk( "---------------------------------\n");
1467 printk("inconsistent {%s} -> {%s} usage.\n",
1468 usage_str[prev_bit], usage_str[new_bit]);
1470 printk("%s/%d [HC%u[%lu]:SC%u[%lu]:HE%u:SE%u] takes:\n",
1471 curr->comm, curr->pid,
1472 trace_hardirq_context(curr), hardirq_count() >> HARDIRQ_SHIFT,
1473 trace_softirq_context(curr), softirq_count() >> SOFTIRQ_SHIFT,
1474 trace_hardirqs_enabled(curr),
1475 trace_softirqs_enabled(curr));
1476 print_lock(this);
1478 printk("{%s} state was registered at:\n", usage_str[prev_bit]);
1479 print_stack_trace(this->class->usage_traces + prev_bit, 1);
1481 print_irqtrace_events(curr);
1482 printk("\nother info that might help us debug this:\n");
1483 lockdep_print_held_locks(curr);
1485 printk("\nstack backtrace:\n");
1486 dump_stack();
1488 return 0;
1492 * Print out an error if an invalid bit is set:
1494 static inline int
1495 valid_state(struct task_struct *curr, struct held_lock *this,
1496 enum lock_usage_bit new_bit, enum lock_usage_bit bad_bit)
1498 if (unlikely(this->class->usage_mask & (1 << bad_bit)))
1499 return print_usage_bug(curr, this, bad_bit, new_bit);
1500 return 1;
1503 #define STRICT_READ_CHECKS 1
1506 * Mark a lock with a usage bit, and validate the state transition:
1508 static int mark_lock(struct task_struct *curr, struct held_lock *this,
1509 enum lock_usage_bit new_bit, unsigned long ip)
1511 unsigned int new_mask = 1 << new_bit, ret = 1;
1514 * If already set then do not dirty the cacheline,
1515 * nor do any checks:
1517 if (likely(this->class->usage_mask & new_mask))
1518 return 1;
1520 __raw_spin_lock(&hash_lock);
1522 * Make sure we didnt race:
1524 if (unlikely(this->class->usage_mask & new_mask)) {
1525 __raw_spin_unlock(&hash_lock);
1526 return 1;
1529 this->class->usage_mask |= new_mask;
1531 #ifdef CONFIG_TRACE_IRQFLAGS
1532 if (new_bit == LOCK_ENABLED_HARDIRQS ||
1533 new_bit == LOCK_ENABLED_HARDIRQS_READ)
1534 ip = curr->hardirq_enable_ip;
1535 else if (new_bit == LOCK_ENABLED_SOFTIRQS ||
1536 new_bit == LOCK_ENABLED_SOFTIRQS_READ)
1537 ip = curr->softirq_enable_ip;
1538 #endif
1539 if (!save_trace(this->class->usage_traces + new_bit))
1540 return 0;
1542 switch (new_bit) {
1543 #ifdef CONFIG_TRACE_IRQFLAGS
1544 case LOCK_USED_IN_HARDIRQ:
1545 if (!valid_state(curr, this, new_bit, LOCK_ENABLED_HARDIRQS))
1546 return 0;
1547 if (!valid_state(curr, this, new_bit,
1548 LOCK_ENABLED_HARDIRQS_READ))
1549 return 0;
1551 * just marked it hardirq-safe, check that this lock
1552 * took no hardirq-unsafe lock in the past:
1554 if (!check_usage_forwards(curr, this,
1555 LOCK_ENABLED_HARDIRQS, "hard"))
1556 return 0;
1557 #if STRICT_READ_CHECKS
1559 * just marked it hardirq-safe, check that this lock
1560 * took no hardirq-unsafe-read lock in the past:
1562 if (!check_usage_forwards(curr, this,
1563 LOCK_ENABLED_HARDIRQS_READ, "hard-read"))
1564 return 0;
1565 #endif
1566 if (hardirq_verbose(this->class))
1567 ret = 2;
1568 break;
1569 case LOCK_USED_IN_SOFTIRQ:
1570 if (!valid_state(curr, this, new_bit, LOCK_ENABLED_SOFTIRQS))
1571 return 0;
1572 if (!valid_state(curr, this, new_bit,
1573 LOCK_ENABLED_SOFTIRQS_READ))
1574 return 0;
1576 * just marked it softirq-safe, check that this lock
1577 * took no softirq-unsafe lock in the past:
1579 if (!check_usage_forwards(curr, this,
1580 LOCK_ENABLED_SOFTIRQS, "soft"))
1581 return 0;
1582 #if STRICT_READ_CHECKS
1584 * just marked it softirq-safe, check that this lock
1585 * took no softirq-unsafe-read lock in the past:
1587 if (!check_usage_forwards(curr, this,
1588 LOCK_ENABLED_SOFTIRQS_READ, "soft-read"))
1589 return 0;
1590 #endif
1591 if (softirq_verbose(this->class))
1592 ret = 2;
1593 break;
1594 case LOCK_USED_IN_HARDIRQ_READ:
1595 if (!valid_state(curr, this, new_bit, LOCK_ENABLED_HARDIRQS))
1596 return 0;
1598 * just marked it hardirq-read-safe, check that this lock
1599 * took no hardirq-unsafe lock in the past:
1601 if (!check_usage_forwards(curr, this,
1602 LOCK_ENABLED_HARDIRQS, "hard"))
1603 return 0;
1604 if (hardirq_verbose(this->class))
1605 ret = 2;
1606 break;
1607 case LOCK_USED_IN_SOFTIRQ_READ:
1608 if (!valid_state(curr, this, new_bit, LOCK_ENABLED_SOFTIRQS))
1609 return 0;
1611 * just marked it softirq-read-safe, check that this lock
1612 * took no softirq-unsafe lock in the past:
1614 if (!check_usage_forwards(curr, this,
1615 LOCK_ENABLED_SOFTIRQS, "soft"))
1616 return 0;
1617 if (softirq_verbose(this->class))
1618 ret = 2;
1619 break;
1620 case LOCK_ENABLED_HARDIRQS:
1621 if (!valid_state(curr, this, new_bit, LOCK_USED_IN_HARDIRQ))
1622 return 0;
1623 if (!valid_state(curr, this, new_bit,
1624 LOCK_USED_IN_HARDIRQ_READ))
1625 return 0;
1627 * just marked it hardirq-unsafe, check that no hardirq-safe
1628 * lock in the system ever took it in the past:
1630 if (!check_usage_backwards(curr, this,
1631 LOCK_USED_IN_HARDIRQ, "hard"))
1632 return 0;
1633 #if STRICT_READ_CHECKS
1635 * just marked it hardirq-unsafe, check that no
1636 * hardirq-safe-read lock in the system ever took
1637 * it in the past:
1639 if (!check_usage_backwards(curr, this,
1640 LOCK_USED_IN_HARDIRQ_READ, "hard-read"))
1641 return 0;
1642 #endif
1643 if (hardirq_verbose(this->class))
1644 ret = 2;
1645 break;
1646 case LOCK_ENABLED_SOFTIRQS:
1647 if (!valid_state(curr, this, new_bit, LOCK_USED_IN_SOFTIRQ))
1648 return 0;
1649 if (!valid_state(curr, this, new_bit,
1650 LOCK_USED_IN_SOFTIRQ_READ))
1651 return 0;
1653 * just marked it softirq-unsafe, check that no softirq-safe
1654 * lock in the system ever took it in the past:
1656 if (!check_usage_backwards(curr, this,
1657 LOCK_USED_IN_SOFTIRQ, "soft"))
1658 return 0;
1659 #if STRICT_READ_CHECKS
1661 * just marked it softirq-unsafe, check that no
1662 * softirq-safe-read lock in the system ever took
1663 * it in the past:
1665 if (!check_usage_backwards(curr, this,
1666 LOCK_USED_IN_SOFTIRQ_READ, "soft-read"))
1667 return 0;
1668 #endif
1669 if (softirq_verbose(this->class))
1670 ret = 2;
1671 break;
1672 case LOCK_ENABLED_HARDIRQS_READ:
1673 if (!valid_state(curr, this, new_bit, LOCK_USED_IN_HARDIRQ))
1674 return 0;
1675 #if STRICT_READ_CHECKS
1677 * just marked it hardirq-read-unsafe, check that no
1678 * hardirq-safe lock in the system ever took it in the past:
1680 if (!check_usage_backwards(curr, this,
1681 LOCK_USED_IN_HARDIRQ, "hard"))
1682 return 0;
1683 #endif
1684 if (hardirq_verbose(this->class))
1685 ret = 2;
1686 break;
1687 case LOCK_ENABLED_SOFTIRQS_READ:
1688 if (!valid_state(curr, this, new_bit, LOCK_USED_IN_SOFTIRQ))
1689 return 0;
1690 #if STRICT_READ_CHECKS
1692 * just marked it softirq-read-unsafe, check that no
1693 * softirq-safe lock in the system ever took it in the past:
1695 if (!check_usage_backwards(curr, this,
1696 LOCK_USED_IN_SOFTIRQ, "soft"))
1697 return 0;
1698 #endif
1699 if (softirq_verbose(this->class))
1700 ret = 2;
1701 break;
1702 #endif
1703 case LOCK_USED:
1705 * Add it to the global list of classes:
1707 list_add_tail_rcu(&this->class->lock_entry, &all_lock_classes);
1708 debug_atomic_dec(&nr_unused_locks);
1709 break;
1710 default:
1711 debug_locks_off();
1712 WARN_ON(1);
1713 return 0;
1716 __raw_spin_unlock(&hash_lock);
1719 * We must printk outside of the hash_lock:
1721 if (ret == 2) {
1722 printk("\nmarked lock as {%s}:\n", usage_str[new_bit]);
1723 print_lock(this);
1724 print_irqtrace_events(curr);
1725 dump_stack();
1728 return ret;
1731 #ifdef CONFIG_TRACE_IRQFLAGS
1733 * Mark all held locks with a usage bit:
1735 static int
1736 mark_held_locks(struct task_struct *curr, int hardirq, unsigned long ip)
1738 enum lock_usage_bit usage_bit;
1739 struct held_lock *hlock;
1740 int i;
1742 for (i = 0; i < curr->lockdep_depth; i++) {
1743 hlock = curr->held_locks + i;
1745 if (hardirq) {
1746 if (hlock->read)
1747 usage_bit = LOCK_ENABLED_HARDIRQS_READ;
1748 else
1749 usage_bit = LOCK_ENABLED_HARDIRQS;
1750 } else {
1751 if (hlock->read)
1752 usage_bit = LOCK_ENABLED_SOFTIRQS_READ;
1753 else
1754 usage_bit = LOCK_ENABLED_SOFTIRQS;
1756 if (!mark_lock(curr, hlock, usage_bit, ip))
1757 return 0;
1760 return 1;
1764 * Debugging helper: via this flag we know that we are in
1765 * 'early bootup code', and will warn about any invalid irqs-on event:
1767 static int early_boot_irqs_enabled;
1769 void early_boot_irqs_off(void)
1771 early_boot_irqs_enabled = 0;
1774 void early_boot_irqs_on(void)
1776 early_boot_irqs_enabled = 1;
1780 * Hardirqs will be enabled:
1782 void trace_hardirqs_on(void)
1784 struct task_struct *curr = current;
1785 unsigned long ip;
1787 if (unlikely(!debug_locks || current->lockdep_recursion))
1788 return;
1790 if (DEBUG_LOCKS_WARN_ON(unlikely(!early_boot_irqs_enabled)))
1791 return;
1793 if (unlikely(curr->hardirqs_enabled)) {
1794 debug_atomic_inc(&redundant_hardirqs_on);
1795 return;
1797 /* we'll do an OFF -> ON transition: */
1798 curr->hardirqs_enabled = 1;
1799 ip = (unsigned long) __builtin_return_address(0);
1801 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
1802 return;
1803 if (DEBUG_LOCKS_WARN_ON(current->hardirq_context))
1804 return;
1806 * We are going to turn hardirqs on, so set the
1807 * usage bit for all held locks:
1809 if (!mark_held_locks(curr, 1, ip))
1810 return;
1812 * If we have softirqs enabled, then set the usage
1813 * bit for all held locks. (disabled hardirqs prevented
1814 * this bit from being set before)
1816 if (curr->softirqs_enabled)
1817 if (!mark_held_locks(curr, 0, ip))
1818 return;
1820 curr->hardirq_enable_ip = ip;
1821 curr->hardirq_enable_event = ++curr->irq_events;
1822 debug_atomic_inc(&hardirqs_on_events);
1825 EXPORT_SYMBOL(trace_hardirqs_on);
1828 * Hardirqs were disabled:
1830 void trace_hardirqs_off(void)
1832 struct task_struct *curr = current;
1834 if (unlikely(!debug_locks || current->lockdep_recursion))
1835 return;
1837 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
1838 return;
1840 if (curr->hardirqs_enabled) {
1842 * We have done an ON -> OFF transition:
1844 curr->hardirqs_enabled = 0;
1845 curr->hardirq_disable_ip = _RET_IP_;
1846 curr->hardirq_disable_event = ++curr->irq_events;
1847 debug_atomic_inc(&hardirqs_off_events);
1848 } else
1849 debug_atomic_inc(&redundant_hardirqs_off);
1852 EXPORT_SYMBOL(trace_hardirqs_off);
1855 * Softirqs will be enabled:
1857 void trace_softirqs_on(unsigned long ip)
1859 struct task_struct *curr = current;
1861 if (unlikely(!debug_locks))
1862 return;
1864 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
1865 return;
1867 if (curr->softirqs_enabled) {
1868 debug_atomic_inc(&redundant_softirqs_on);
1869 return;
1873 * We'll do an OFF -> ON transition:
1875 curr->softirqs_enabled = 1;
1876 curr->softirq_enable_ip = ip;
1877 curr->softirq_enable_event = ++curr->irq_events;
1878 debug_atomic_inc(&softirqs_on_events);
1880 * We are going to turn softirqs on, so set the
1881 * usage bit for all held locks, if hardirqs are
1882 * enabled too:
1884 if (curr->hardirqs_enabled)
1885 mark_held_locks(curr, 0, ip);
1889 * Softirqs were disabled:
1891 void trace_softirqs_off(unsigned long ip)
1893 struct task_struct *curr = current;
1895 if (unlikely(!debug_locks))
1896 return;
1898 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
1899 return;
1901 if (curr->softirqs_enabled) {
1903 * We have done an ON -> OFF transition:
1905 curr->softirqs_enabled = 0;
1906 curr->softirq_disable_ip = ip;
1907 curr->softirq_disable_event = ++curr->irq_events;
1908 debug_atomic_inc(&softirqs_off_events);
1909 DEBUG_LOCKS_WARN_ON(!softirq_count());
1910 } else
1911 debug_atomic_inc(&redundant_softirqs_off);
1914 #endif
1917 * Initialize a lock instance's lock-class mapping info:
1919 void lockdep_init_map(struct lockdep_map *lock, const char *name,
1920 struct lock_class_key *key)
1922 if (unlikely(!debug_locks))
1923 return;
1925 if (DEBUG_LOCKS_WARN_ON(!key))
1926 return;
1927 if (DEBUG_LOCKS_WARN_ON(!name))
1928 return;
1930 * Sanity check, the lock-class key must be persistent:
1932 if (!static_obj(key)) {
1933 printk("BUG: key %p not in .data!\n", key);
1934 DEBUG_LOCKS_WARN_ON(1);
1935 return;
1937 lock->name = name;
1938 lock->key = key;
1939 lock->class_cache = NULL;
1942 EXPORT_SYMBOL_GPL(lockdep_init_map);
1945 * This gets called for every mutex_lock*()/spin_lock*() operation.
1946 * We maintain the dependency maps and validate the locking attempt:
1948 static int __lock_acquire(struct lockdep_map *lock, unsigned int subclass,
1949 int trylock, int read, int check, int hardirqs_off,
1950 unsigned long ip)
1952 struct task_struct *curr = current;
1953 struct lock_class *class = NULL;
1954 struct held_lock *hlock;
1955 unsigned int depth, id;
1956 int chain_head = 0;
1957 u64 chain_key;
1959 if (unlikely(!debug_locks))
1960 return 0;
1962 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
1963 return 0;
1965 if (unlikely(subclass >= MAX_LOCKDEP_SUBCLASSES)) {
1966 debug_locks_off();
1967 printk("BUG: MAX_LOCKDEP_SUBCLASSES too low!\n");
1968 printk("turning off the locking correctness validator.\n");
1969 return 0;
1972 if (!subclass)
1973 class = lock->class_cache;
1975 * Not cached yet or subclass?
1977 if (unlikely(!class)) {
1978 class = register_lock_class(lock, subclass);
1979 if (!class)
1980 return 0;
1982 debug_atomic_inc((atomic_t *)&class->ops);
1983 if (very_verbose(class)) {
1984 printk("\nacquire class [%p] %s", class->key, class->name);
1985 if (class->name_version > 1)
1986 printk("#%d", class->name_version);
1987 printk("\n");
1988 dump_stack();
1992 * Add the lock to the list of currently held locks.
1993 * (we dont increase the depth just yet, up until the
1994 * dependency checks are done)
1996 depth = curr->lockdep_depth;
1997 if (DEBUG_LOCKS_WARN_ON(depth >= MAX_LOCK_DEPTH))
1998 return 0;
2000 hlock = curr->held_locks + depth;
2002 hlock->class = class;
2003 hlock->acquire_ip = ip;
2004 hlock->instance = lock;
2005 hlock->trylock = trylock;
2006 hlock->read = read;
2007 hlock->check = check;
2008 hlock->hardirqs_off = hardirqs_off;
2010 if (check != 2)
2011 goto out_calc_hash;
2012 #ifdef CONFIG_TRACE_IRQFLAGS
2014 * If non-trylock use in a hardirq or softirq context, then
2015 * mark the lock as used in these contexts:
2017 if (!trylock) {
2018 if (read) {
2019 if (curr->hardirq_context)
2020 if (!mark_lock(curr, hlock,
2021 LOCK_USED_IN_HARDIRQ_READ, ip))
2022 return 0;
2023 if (curr->softirq_context)
2024 if (!mark_lock(curr, hlock,
2025 LOCK_USED_IN_SOFTIRQ_READ, ip))
2026 return 0;
2027 } else {
2028 if (curr->hardirq_context)
2029 if (!mark_lock(curr, hlock, LOCK_USED_IN_HARDIRQ, ip))
2030 return 0;
2031 if (curr->softirq_context)
2032 if (!mark_lock(curr, hlock, LOCK_USED_IN_SOFTIRQ, ip))
2033 return 0;
2036 if (!hardirqs_off) {
2037 if (read) {
2038 if (!mark_lock(curr, hlock,
2039 LOCK_ENABLED_HARDIRQS_READ, ip))
2040 return 0;
2041 if (curr->softirqs_enabled)
2042 if (!mark_lock(curr, hlock,
2043 LOCK_ENABLED_SOFTIRQS_READ, ip))
2044 return 0;
2045 } else {
2046 if (!mark_lock(curr, hlock,
2047 LOCK_ENABLED_HARDIRQS, ip))
2048 return 0;
2049 if (curr->softirqs_enabled)
2050 if (!mark_lock(curr, hlock,
2051 LOCK_ENABLED_SOFTIRQS, ip))
2052 return 0;
2055 #endif
2056 /* mark it as used: */
2057 if (!mark_lock(curr, hlock, LOCK_USED, ip))
2058 return 0;
2059 out_calc_hash:
2061 * Calculate the chain hash: it's the combined has of all the
2062 * lock keys along the dependency chain. We save the hash value
2063 * at every step so that we can get the current hash easily
2064 * after unlock. The chain hash is then used to cache dependency
2065 * results.
2067 * The 'key ID' is what is the most compact key value to drive
2068 * the hash, not class->key.
2070 id = class - lock_classes;
2071 if (DEBUG_LOCKS_WARN_ON(id >= MAX_LOCKDEP_KEYS))
2072 return 0;
2074 chain_key = curr->curr_chain_key;
2075 if (!depth) {
2076 if (DEBUG_LOCKS_WARN_ON(chain_key != 0))
2077 return 0;
2078 chain_head = 1;
2081 hlock->prev_chain_key = chain_key;
2083 #ifdef CONFIG_TRACE_IRQFLAGS
2085 * Keep track of points where we cross into an interrupt context:
2087 hlock->irq_context = 2*(curr->hardirq_context ? 1 : 0) +
2088 curr->softirq_context;
2089 if (depth) {
2090 struct held_lock *prev_hlock;
2092 prev_hlock = curr->held_locks + depth-1;
2094 * If we cross into another context, reset the
2095 * hash key (this also prevents the checking and the
2096 * adding of the dependency to 'prev'):
2098 if (prev_hlock->irq_context != hlock->irq_context) {
2099 chain_key = 0;
2100 chain_head = 1;
2103 #endif
2104 chain_key = iterate_chain_key(chain_key, id);
2105 curr->curr_chain_key = chain_key;
2108 * Trylock needs to maintain the stack of held locks, but it
2109 * does not add new dependencies, because trylock can be done
2110 * in any order.
2112 * We look up the chain_key and do the O(N^2) check and update of
2113 * the dependencies only if this is a new dependency chain.
2114 * (If lookup_chain_cache() returns with 1 it acquires
2115 * hash_lock for us)
2117 if (!trylock && (check == 2) && lookup_chain_cache(chain_key)) {
2119 * Check whether last held lock:
2121 * - is irq-safe, if this lock is irq-unsafe
2122 * - is softirq-safe, if this lock is hardirq-unsafe
2124 * And check whether the new lock's dependency graph
2125 * could lead back to the previous lock.
2127 * any of these scenarios could lead to a deadlock. If
2128 * All validations
2130 int ret = check_deadlock(curr, hlock, lock, read);
2132 if (!ret)
2133 return 0;
2135 * Mark recursive read, as we jump over it when
2136 * building dependencies (just like we jump over
2137 * trylock entries):
2139 if (ret == 2)
2140 hlock->read = 2;
2142 * Add dependency only if this lock is not the head
2143 * of the chain, and if it's not a secondary read-lock:
2145 if (!chain_head && ret != 2)
2146 if (!check_prevs_add(curr, hlock))
2147 return 0;
2148 __raw_spin_unlock(&hash_lock);
2150 curr->lockdep_depth++;
2151 check_chain_key(curr);
2152 if (unlikely(curr->lockdep_depth >= MAX_LOCK_DEPTH)) {
2153 debug_locks_off();
2154 printk("BUG: MAX_LOCK_DEPTH too low!\n");
2155 printk("turning off the locking correctness validator.\n");
2156 return 0;
2158 if (unlikely(curr->lockdep_depth > max_lockdep_depth))
2159 max_lockdep_depth = curr->lockdep_depth;
2161 return 1;
2164 static int
2165 print_unlock_inbalance_bug(struct task_struct *curr, struct lockdep_map *lock,
2166 unsigned long ip)
2168 if (!debug_locks_off())
2169 return 0;
2170 if (debug_locks_silent)
2171 return 0;
2173 printk("\n=====================================\n");
2174 printk( "[ BUG: bad unlock balance detected! ]\n");
2175 printk( "-------------------------------------\n");
2176 printk("%s/%d is trying to release lock (",
2177 curr->comm, curr->pid);
2178 print_lockdep_cache(lock);
2179 printk(") at:\n");
2180 print_ip_sym(ip);
2181 printk("but there are no more locks to release!\n");
2182 printk("\nother info that might help us debug this:\n");
2183 lockdep_print_held_locks(curr);
2185 printk("\nstack backtrace:\n");
2186 dump_stack();
2188 return 0;
2192 * Common debugging checks for both nested and non-nested unlock:
2194 static int check_unlock(struct task_struct *curr, struct lockdep_map *lock,
2195 unsigned long ip)
2197 if (unlikely(!debug_locks))
2198 return 0;
2199 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2200 return 0;
2202 if (curr->lockdep_depth <= 0)
2203 return print_unlock_inbalance_bug(curr, lock, ip);
2205 return 1;
2209 * Remove the lock to the list of currently held locks in a
2210 * potentially non-nested (out of order) manner. This is a
2211 * relatively rare operation, as all the unlock APIs default
2212 * to nested mode (which uses lock_release()):
2214 static int
2215 lock_release_non_nested(struct task_struct *curr,
2216 struct lockdep_map *lock, unsigned long ip)
2218 struct held_lock *hlock, *prev_hlock;
2219 unsigned int depth;
2220 int i;
2223 * Check whether the lock exists in the current stack
2224 * of held locks:
2226 depth = curr->lockdep_depth;
2227 if (DEBUG_LOCKS_WARN_ON(!depth))
2228 return 0;
2230 prev_hlock = NULL;
2231 for (i = depth-1; i >= 0; i--) {
2232 hlock = curr->held_locks + i;
2234 * We must not cross into another context:
2236 if (prev_hlock && prev_hlock->irq_context != hlock->irq_context)
2237 break;
2238 if (hlock->instance == lock)
2239 goto found_it;
2240 prev_hlock = hlock;
2242 return print_unlock_inbalance_bug(curr, lock, ip);
2244 found_it:
2246 * We have the right lock to unlock, 'hlock' points to it.
2247 * Now we remove it from the stack, and add back the other
2248 * entries (if any), recalculating the hash along the way:
2250 curr->lockdep_depth = i;
2251 curr->curr_chain_key = hlock->prev_chain_key;
2253 for (i++; i < depth; i++) {
2254 hlock = curr->held_locks + i;
2255 if (!__lock_acquire(hlock->instance,
2256 hlock->class->subclass, hlock->trylock,
2257 hlock->read, hlock->check, hlock->hardirqs_off,
2258 hlock->acquire_ip))
2259 return 0;
2262 if (DEBUG_LOCKS_WARN_ON(curr->lockdep_depth != depth - 1))
2263 return 0;
2264 return 1;
2268 * Remove the lock to the list of currently held locks - this gets
2269 * called on mutex_unlock()/spin_unlock*() (or on a failed
2270 * mutex_lock_interruptible()). This is done for unlocks that nest
2271 * perfectly. (i.e. the current top of the lock-stack is unlocked)
2273 static int lock_release_nested(struct task_struct *curr,
2274 struct lockdep_map *lock, unsigned long ip)
2276 struct held_lock *hlock;
2277 unsigned int depth;
2280 * Pop off the top of the lock stack:
2282 depth = curr->lockdep_depth - 1;
2283 hlock = curr->held_locks + depth;
2286 * Is the unlock non-nested:
2288 if (hlock->instance != lock)
2289 return lock_release_non_nested(curr, lock, ip);
2290 curr->lockdep_depth--;
2292 if (DEBUG_LOCKS_WARN_ON(!depth && (hlock->prev_chain_key != 0)))
2293 return 0;
2295 curr->curr_chain_key = hlock->prev_chain_key;
2297 #ifdef CONFIG_DEBUG_LOCKDEP
2298 hlock->prev_chain_key = 0;
2299 hlock->class = NULL;
2300 hlock->acquire_ip = 0;
2301 hlock->irq_context = 0;
2302 #endif
2303 return 1;
2307 * Remove the lock to the list of currently held locks - this gets
2308 * called on mutex_unlock()/spin_unlock*() (or on a failed
2309 * mutex_lock_interruptible()). This is done for unlocks that nest
2310 * perfectly. (i.e. the current top of the lock-stack is unlocked)
2312 static void
2313 __lock_release(struct lockdep_map *lock, int nested, unsigned long ip)
2315 struct task_struct *curr = current;
2317 if (!check_unlock(curr, lock, ip))
2318 return;
2320 if (nested) {
2321 if (!lock_release_nested(curr, lock, ip))
2322 return;
2323 } else {
2324 if (!lock_release_non_nested(curr, lock, ip))
2325 return;
2328 check_chain_key(curr);
2332 * Check whether we follow the irq-flags state precisely:
2334 static void check_flags(unsigned long flags)
2336 #if defined(CONFIG_DEBUG_LOCKDEP) && defined(CONFIG_TRACE_IRQFLAGS)
2337 if (!debug_locks)
2338 return;
2340 if (irqs_disabled_flags(flags))
2341 DEBUG_LOCKS_WARN_ON(current->hardirqs_enabled);
2342 else
2343 DEBUG_LOCKS_WARN_ON(!current->hardirqs_enabled);
2346 * We dont accurately track softirq state in e.g.
2347 * hardirq contexts (such as on 4KSTACKS), so only
2348 * check if not in hardirq contexts:
2350 if (!hardirq_count()) {
2351 if (softirq_count())
2352 DEBUG_LOCKS_WARN_ON(current->softirqs_enabled);
2353 else
2354 DEBUG_LOCKS_WARN_ON(!current->softirqs_enabled);
2357 if (!debug_locks)
2358 print_irqtrace_events(current);
2359 #endif
2363 * We are not always called with irqs disabled - do that here,
2364 * and also avoid lockdep recursion:
2366 void lock_acquire(struct lockdep_map *lock, unsigned int subclass,
2367 int trylock, int read, int check, unsigned long ip)
2369 unsigned long flags;
2371 if (unlikely(current->lockdep_recursion))
2372 return;
2374 raw_local_irq_save(flags);
2375 check_flags(flags);
2377 current->lockdep_recursion = 1;
2378 __lock_acquire(lock, subclass, trylock, read, check,
2379 irqs_disabled_flags(flags), ip);
2380 current->lockdep_recursion = 0;
2381 raw_local_irq_restore(flags);
2384 EXPORT_SYMBOL_GPL(lock_acquire);
2386 void lock_release(struct lockdep_map *lock, int nested, unsigned long ip)
2388 unsigned long flags;
2390 if (unlikely(current->lockdep_recursion))
2391 return;
2393 raw_local_irq_save(flags);
2394 check_flags(flags);
2395 current->lockdep_recursion = 1;
2396 __lock_release(lock, nested, ip);
2397 current->lockdep_recursion = 0;
2398 raw_local_irq_restore(flags);
2401 EXPORT_SYMBOL_GPL(lock_release);
2404 * Used by the testsuite, sanitize the validator state
2405 * after a simulated failure:
2408 void lockdep_reset(void)
2410 unsigned long flags;
2412 raw_local_irq_save(flags);
2413 current->curr_chain_key = 0;
2414 current->lockdep_depth = 0;
2415 current->lockdep_recursion = 0;
2416 memset(current->held_locks, 0, MAX_LOCK_DEPTH*sizeof(struct held_lock));
2417 nr_hardirq_chains = 0;
2418 nr_softirq_chains = 0;
2419 nr_process_chains = 0;
2420 debug_locks = 1;
2421 raw_local_irq_restore(flags);
2424 static void zap_class(struct lock_class *class)
2426 int i;
2429 * Remove all dependencies this lock is
2430 * involved in:
2432 for (i = 0; i < nr_list_entries; i++) {
2433 if (list_entries[i].class == class)
2434 list_del_rcu(&list_entries[i].entry);
2437 * Unhash the class and remove it from the all_lock_classes list:
2439 list_del_rcu(&class->hash_entry);
2440 list_del_rcu(&class->lock_entry);
2444 static inline int within(void *addr, void *start, unsigned long size)
2446 return addr >= start && addr < start + size;
2449 void lockdep_free_key_range(void *start, unsigned long size)
2451 struct lock_class *class, *next;
2452 struct list_head *head;
2453 unsigned long flags;
2454 int i;
2456 raw_local_irq_save(flags);
2457 __raw_spin_lock(&hash_lock);
2460 * Unhash all classes that were created by this module:
2462 for (i = 0; i < CLASSHASH_SIZE; i++) {
2463 head = classhash_table + i;
2464 if (list_empty(head))
2465 continue;
2466 list_for_each_entry_safe(class, next, head, hash_entry)
2467 if (within(class->key, start, size))
2468 zap_class(class);
2471 __raw_spin_unlock(&hash_lock);
2472 raw_local_irq_restore(flags);
2475 void lockdep_reset_lock(struct lockdep_map *lock)
2477 struct lock_class *class, *next;
2478 struct list_head *head;
2479 unsigned long flags;
2480 int i, j;
2482 raw_local_irq_save(flags);
2485 * Remove all classes this lock might have:
2487 for (j = 0; j < MAX_LOCKDEP_SUBCLASSES; j++) {
2489 * If the class exists we look it up and zap it:
2491 class = look_up_lock_class(lock, j);
2492 if (class)
2493 zap_class(class);
2496 * Debug check: in the end all mapped classes should
2497 * be gone.
2499 __raw_spin_lock(&hash_lock);
2500 for (i = 0; i < CLASSHASH_SIZE; i++) {
2501 head = classhash_table + i;
2502 if (list_empty(head))
2503 continue;
2504 list_for_each_entry_safe(class, next, head, hash_entry) {
2505 if (unlikely(class == lock->class_cache)) {
2506 __raw_spin_unlock(&hash_lock);
2507 DEBUG_LOCKS_WARN_ON(1);
2508 goto out_restore;
2512 __raw_spin_unlock(&hash_lock);
2514 out_restore:
2515 raw_local_irq_restore(flags);
2518 void __init lockdep_init(void)
2520 int i;
2523 * Some architectures have their own start_kernel()
2524 * code which calls lockdep_init(), while we also
2525 * call lockdep_init() from the start_kernel() itself,
2526 * and we want to initialize the hashes only once:
2528 if (lockdep_initialized)
2529 return;
2531 for (i = 0; i < CLASSHASH_SIZE; i++)
2532 INIT_LIST_HEAD(classhash_table + i);
2534 for (i = 0; i < CHAINHASH_SIZE; i++)
2535 INIT_LIST_HEAD(chainhash_table + i);
2537 lockdep_initialized = 1;
2540 void __init lockdep_info(void)
2542 printk("Lock dependency validator: Copyright (c) 2006 Red Hat, Inc., Ingo Molnar\n");
2544 printk("... MAX_LOCKDEP_SUBCLASSES: %lu\n", MAX_LOCKDEP_SUBCLASSES);
2545 printk("... MAX_LOCK_DEPTH: %lu\n", MAX_LOCK_DEPTH);
2546 printk("... MAX_LOCKDEP_KEYS: %lu\n", MAX_LOCKDEP_KEYS);
2547 printk("... CLASSHASH_SIZE: %lu\n", CLASSHASH_SIZE);
2548 printk("... MAX_LOCKDEP_ENTRIES: %lu\n", MAX_LOCKDEP_ENTRIES);
2549 printk("... MAX_LOCKDEP_CHAINS: %lu\n", MAX_LOCKDEP_CHAINS);
2550 printk("... CHAINHASH_SIZE: %lu\n", CHAINHASH_SIZE);
2552 printk(" memory used by lock dependency info: %lu kB\n",
2553 (sizeof(struct lock_class) * MAX_LOCKDEP_KEYS +
2554 sizeof(struct list_head) * CLASSHASH_SIZE +
2555 sizeof(struct lock_list) * MAX_LOCKDEP_ENTRIES +
2556 sizeof(struct lock_chain) * MAX_LOCKDEP_CHAINS +
2557 sizeof(struct list_head) * CHAINHASH_SIZE) / 1024);
2559 printk(" per task-struct memory footprint: %lu bytes\n",
2560 sizeof(struct held_lock) * MAX_LOCK_DEPTH);
2562 #ifdef CONFIG_DEBUG_LOCKDEP
2563 if (lockdep_init_error)
2564 printk("WARNING: lockdep init error! Arch code didnt call lockdep_init() early enough?\n");
2565 #endif
2568 static inline int in_range(const void *start, const void *addr, const void *end)
2570 return addr >= start && addr <= end;
2573 static void
2574 print_freed_lock_bug(struct task_struct *curr, const void *mem_from,
2575 const void *mem_to, struct held_lock *hlock)
2577 if (!debug_locks_off())
2578 return;
2579 if (debug_locks_silent)
2580 return;
2582 printk("\n=========================\n");
2583 printk( "[ BUG: held lock freed! ]\n");
2584 printk( "-------------------------\n");
2585 printk("%s/%d is freeing memory %p-%p, with a lock still held there!\n",
2586 curr->comm, curr->pid, mem_from, mem_to-1);
2587 print_lock(hlock);
2588 lockdep_print_held_locks(curr);
2590 printk("\nstack backtrace:\n");
2591 dump_stack();
2595 * Called when kernel memory is freed (or unmapped), or if a lock
2596 * is destroyed or reinitialized - this code checks whether there is
2597 * any held lock in the memory range of <from> to <to>:
2599 void debug_check_no_locks_freed(const void *mem_from, unsigned long mem_len)
2601 const void *mem_to = mem_from + mem_len, *lock_from, *lock_to;
2602 struct task_struct *curr = current;
2603 struct held_lock *hlock;
2604 unsigned long flags;
2605 int i;
2607 if (unlikely(!debug_locks))
2608 return;
2610 local_irq_save(flags);
2611 for (i = 0; i < curr->lockdep_depth; i++) {
2612 hlock = curr->held_locks + i;
2614 lock_from = (void *)hlock->instance;
2615 lock_to = (void *)(hlock->instance + 1);
2617 if (!in_range(mem_from, lock_from, mem_to) &&
2618 !in_range(mem_from, lock_to, mem_to))
2619 continue;
2621 print_freed_lock_bug(curr, mem_from, mem_to, hlock);
2622 break;
2624 local_irq_restore(flags);
2627 static void print_held_locks_bug(struct task_struct *curr)
2629 if (!debug_locks_off())
2630 return;
2631 if (debug_locks_silent)
2632 return;
2634 printk("\n=====================================\n");
2635 printk( "[ BUG: lock held at task exit time! ]\n");
2636 printk( "-------------------------------------\n");
2637 printk("%s/%d is exiting with locks still held!\n",
2638 curr->comm, curr->pid);
2639 lockdep_print_held_locks(curr);
2641 printk("\nstack backtrace:\n");
2642 dump_stack();
2645 void debug_check_no_locks_held(struct task_struct *task)
2647 if (unlikely(task->lockdep_depth > 0))
2648 print_held_locks_bug(task);
2651 void debug_show_all_locks(void)
2653 struct task_struct *g, *p;
2654 int count = 10;
2655 int unlock = 1;
2657 printk("\nShowing all locks held in the system:\n");
2660 * Here we try to get the tasklist_lock as hard as possible,
2661 * if not successful after 2 seconds we ignore it (but keep
2662 * trying). This is to enable a debug printout even if a
2663 * tasklist_lock-holding task deadlocks or crashes.
2665 retry:
2666 if (!read_trylock(&tasklist_lock)) {
2667 if (count == 10)
2668 printk("hm, tasklist_lock locked, retrying... ");
2669 if (count) {
2670 count--;
2671 printk(" #%d", 10-count);
2672 mdelay(200);
2673 goto retry;
2675 printk(" ignoring it.\n");
2676 unlock = 0;
2678 if (count != 10)
2679 printk(" locked it.\n");
2681 do_each_thread(g, p) {
2682 if (p->lockdep_depth)
2683 lockdep_print_held_locks(p);
2684 if (!unlock)
2685 if (read_trylock(&tasklist_lock))
2686 unlock = 1;
2687 } while_each_thread(g, p);
2689 printk("\n");
2690 printk("=============================================\n\n");
2692 if (unlock)
2693 read_unlock(&tasklist_lock);
2696 EXPORT_SYMBOL_GPL(debug_show_all_locks);
2698 void debug_show_held_locks(struct task_struct *task)
2700 lockdep_print_held_locks(task);
2703 EXPORT_SYMBOL_GPL(debug_show_held_locks);