Merge git://git.kernel.org/pub/scm/linux/kernel/git/sfrench/cifs-2.6
[linux-2.6.22.y-op.git] / kernel / lockdep.c
blob509efd49540f9beb09ddf347725690f939e09aa2
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 __raw_spin_unlock(&lockdep_lock);
74 return 0;
78 * Turn lock debugging off and return with 0 if it was off already,
79 * and also release the graph lock:
81 static inline int debug_locks_off_graph_unlock(void)
83 int ret = debug_locks_off();
85 __raw_spin_unlock(&lockdep_lock);
87 return ret;
90 static int lockdep_initialized;
92 unsigned long nr_list_entries;
93 static struct lock_list list_entries[MAX_LOCKDEP_ENTRIES];
96 * Allocate a lockdep entry. (assumes the graph_lock held, returns
97 * with NULL on failure)
99 static struct lock_list *alloc_list_entry(void)
101 if (nr_list_entries >= MAX_LOCKDEP_ENTRIES) {
102 if (!debug_locks_off_graph_unlock())
103 return NULL;
105 printk("BUG: MAX_LOCKDEP_ENTRIES too low!\n");
106 printk("turning off the locking correctness validator.\n");
107 return NULL;
109 return list_entries + nr_list_entries++;
113 * All data structures here are protected by the global debug_lock.
115 * Mutex key structs only get allocated, once during bootup, and never
116 * get freed - this significantly simplifies the debugging code.
118 unsigned long nr_lock_classes;
119 static struct lock_class lock_classes[MAX_LOCKDEP_KEYS];
122 * We keep a global list of all lock classes. The list only grows,
123 * never shrinks. The list is only accessed with the lockdep
124 * spinlock lock held.
126 LIST_HEAD(all_lock_classes);
129 * The lockdep classes are in a hash-table as well, for fast lookup:
131 #define CLASSHASH_BITS (MAX_LOCKDEP_KEYS_BITS - 1)
132 #define CLASSHASH_SIZE (1UL << CLASSHASH_BITS)
133 #define CLASSHASH_MASK (CLASSHASH_SIZE - 1)
134 #define __classhashfn(key) ((((unsigned long)key >> CLASSHASH_BITS) + (unsigned long)key) & CLASSHASH_MASK)
135 #define classhashentry(key) (classhash_table + __classhashfn((key)))
137 static struct list_head classhash_table[CLASSHASH_SIZE];
139 unsigned long nr_lock_chains;
140 static struct lock_chain lock_chains[MAX_LOCKDEP_CHAINS];
143 * We put the lock dependency chains into a hash-table as well, to cache
144 * their existence:
146 #define CHAINHASH_BITS (MAX_LOCKDEP_CHAINS_BITS-1)
147 #define CHAINHASH_SIZE (1UL << CHAINHASH_BITS)
148 #define CHAINHASH_MASK (CHAINHASH_SIZE - 1)
149 #define __chainhashfn(chain) \
150 (((chain >> CHAINHASH_BITS) + chain) & CHAINHASH_MASK)
151 #define chainhashentry(chain) (chainhash_table + __chainhashfn((chain)))
153 static struct list_head chainhash_table[CHAINHASH_SIZE];
156 * The hash key of the lock dependency chains is a hash itself too:
157 * it's a hash of all locks taken up to that lock, including that lock.
158 * It's a 64-bit hash, because it's important for the keys to be
159 * unique.
161 #define iterate_chain_key(key1, key2) \
162 (((key1) << MAX_LOCKDEP_KEYS_BITS) ^ \
163 ((key1) >> (64-MAX_LOCKDEP_KEYS_BITS)) ^ \
164 (key2))
166 void lockdep_off(void)
168 current->lockdep_recursion++;
171 EXPORT_SYMBOL(lockdep_off);
173 void lockdep_on(void)
175 current->lockdep_recursion--;
178 EXPORT_SYMBOL(lockdep_on);
181 * Debugging switches:
184 #define VERBOSE 0
185 #define VERY_VERBOSE 0
187 #if VERBOSE
188 # define HARDIRQ_VERBOSE 1
189 # define SOFTIRQ_VERBOSE 1
190 #else
191 # define HARDIRQ_VERBOSE 0
192 # define SOFTIRQ_VERBOSE 0
193 #endif
195 #if VERBOSE || HARDIRQ_VERBOSE || SOFTIRQ_VERBOSE
197 * Quick filtering for interesting events:
199 static int class_filter(struct lock_class *class)
201 #if 0
202 /* Example */
203 if (class->name_version == 1 &&
204 !strcmp(class->name, "lockname"))
205 return 1;
206 if (class->name_version == 1 &&
207 !strcmp(class->name, "&struct->lockfield"))
208 return 1;
209 #endif
210 /* Filter everything else. 1 would be to allow everything else */
211 return 0;
213 #endif
215 static int verbose(struct lock_class *class)
217 #if VERBOSE
218 return class_filter(class);
219 #endif
220 return 0;
223 #ifdef CONFIG_TRACE_IRQFLAGS
225 static int hardirq_verbose(struct lock_class *class)
227 #if HARDIRQ_VERBOSE
228 return class_filter(class);
229 #endif
230 return 0;
233 static int softirq_verbose(struct lock_class *class)
235 #if SOFTIRQ_VERBOSE
236 return class_filter(class);
237 #endif
238 return 0;
241 #endif
244 * Stack-trace: tightly packed array of stack backtrace
245 * addresses. Protected by the graph_lock.
247 unsigned long nr_stack_trace_entries;
248 static unsigned long stack_trace[MAX_STACK_TRACE_ENTRIES];
250 static int save_trace(struct stack_trace *trace)
252 trace->nr_entries = 0;
253 trace->max_entries = MAX_STACK_TRACE_ENTRIES - nr_stack_trace_entries;
254 trace->entries = stack_trace + nr_stack_trace_entries;
256 trace->skip = 3;
257 trace->all_contexts = 0;
259 save_stack_trace(trace, NULL);
261 trace->max_entries = trace->nr_entries;
263 nr_stack_trace_entries += trace->nr_entries;
265 if (nr_stack_trace_entries == MAX_STACK_TRACE_ENTRIES) {
266 if (!debug_locks_off_graph_unlock())
267 return 0;
269 printk("BUG: MAX_STACK_TRACE_ENTRIES too low!\n");
270 printk("turning off the locking correctness validator.\n");
271 dump_stack();
273 return 0;
276 return 1;
279 unsigned int nr_hardirq_chains;
280 unsigned int nr_softirq_chains;
281 unsigned int nr_process_chains;
282 unsigned int max_lockdep_depth;
283 unsigned int max_recursion_depth;
285 #ifdef CONFIG_DEBUG_LOCKDEP
287 * We cannot printk in early bootup code. Not even early_printk()
288 * might work. So we mark any initialization errors and printk
289 * about it later on, in lockdep_info().
291 static int lockdep_init_error;
294 * Various lockdep statistics:
296 atomic_t chain_lookup_hits;
297 atomic_t chain_lookup_misses;
298 atomic_t hardirqs_on_events;
299 atomic_t hardirqs_off_events;
300 atomic_t redundant_hardirqs_on;
301 atomic_t redundant_hardirqs_off;
302 atomic_t softirqs_on_events;
303 atomic_t softirqs_off_events;
304 atomic_t redundant_softirqs_on;
305 atomic_t redundant_softirqs_off;
306 atomic_t nr_unused_locks;
307 atomic_t nr_cyclic_checks;
308 atomic_t nr_cyclic_check_recursions;
309 atomic_t nr_find_usage_forwards_checks;
310 atomic_t nr_find_usage_forwards_recursions;
311 atomic_t nr_find_usage_backwards_checks;
312 atomic_t nr_find_usage_backwards_recursions;
313 # define debug_atomic_inc(ptr) atomic_inc(ptr)
314 # define debug_atomic_dec(ptr) atomic_dec(ptr)
315 # define debug_atomic_read(ptr) atomic_read(ptr)
316 #else
317 # define debug_atomic_inc(ptr) do { } while (0)
318 # define debug_atomic_dec(ptr) do { } while (0)
319 # define debug_atomic_read(ptr) 0
320 #endif
323 * Locking printouts:
326 static const char *usage_str[] =
328 [LOCK_USED] = "initial-use ",
329 [LOCK_USED_IN_HARDIRQ] = "in-hardirq-W",
330 [LOCK_USED_IN_SOFTIRQ] = "in-softirq-W",
331 [LOCK_ENABLED_SOFTIRQS] = "softirq-on-W",
332 [LOCK_ENABLED_HARDIRQS] = "hardirq-on-W",
333 [LOCK_USED_IN_HARDIRQ_READ] = "in-hardirq-R",
334 [LOCK_USED_IN_SOFTIRQ_READ] = "in-softirq-R",
335 [LOCK_ENABLED_SOFTIRQS_READ] = "softirq-on-R",
336 [LOCK_ENABLED_HARDIRQS_READ] = "hardirq-on-R",
339 const char * __get_key_name(struct lockdep_subclass_key *key, char *str)
341 unsigned long offs, size;
342 char *modname;
344 return kallsyms_lookup((unsigned long)key, &size, &offs, &modname, str);
347 void
348 get_usage_chars(struct lock_class *class, char *c1, char *c2, char *c3, char *c4)
350 *c1 = '.', *c2 = '.', *c3 = '.', *c4 = '.';
352 if (class->usage_mask & LOCKF_USED_IN_HARDIRQ)
353 *c1 = '+';
354 else
355 if (class->usage_mask & LOCKF_ENABLED_HARDIRQS)
356 *c1 = '-';
358 if (class->usage_mask & LOCKF_USED_IN_SOFTIRQ)
359 *c2 = '+';
360 else
361 if (class->usage_mask & LOCKF_ENABLED_SOFTIRQS)
362 *c2 = '-';
364 if (class->usage_mask & LOCKF_ENABLED_HARDIRQS_READ)
365 *c3 = '-';
366 if (class->usage_mask & LOCKF_USED_IN_HARDIRQ_READ) {
367 *c3 = '+';
368 if (class->usage_mask & LOCKF_ENABLED_HARDIRQS_READ)
369 *c3 = '?';
372 if (class->usage_mask & LOCKF_ENABLED_SOFTIRQS_READ)
373 *c4 = '-';
374 if (class->usage_mask & LOCKF_USED_IN_SOFTIRQ_READ) {
375 *c4 = '+';
376 if (class->usage_mask & LOCKF_ENABLED_SOFTIRQS_READ)
377 *c4 = '?';
381 static void print_lock_name(struct lock_class *class)
383 char str[KSYM_NAME_LEN + 1], c1, c2, c3, c4;
384 const char *name;
386 get_usage_chars(class, &c1, &c2, &c3, &c4);
388 name = class->name;
389 if (!name) {
390 name = __get_key_name(class->key, str);
391 printk(" (%s", name);
392 } else {
393 printk(" (%s", name);
394 if (class->name_version > 1)
395 printk("#%d", class->name_version);
396 if (class->subclass)
397 printk("/%d", class->subclass);
399 printk("){%c%c%c%c}", c1, c2, c3, c4);
402 static void print_lockdep_cache(struct lockdep_map *lock)
404 const char *name;
405 char str[KSYM_NAME_LEN + 1];
407 name = lock->name;
408 if (!name)
409 name = __get_key_name(lock->key->subkeys, str);
411 printk("%s", name);
414 static void print_lock(struct held_lock *hlock)
416 print_lock_name(hlock->class);
417 printk(", at: ");
418 print_ip_sym(hlock->acquire_ip);
421 static void lockdep_print_held_locks(struct task_struct *curr)
423 int i, depth = curr->lockdep_depth;
425 if (!depth) {
426 printk("no locks held by %s/%d.\n", curr->comm, curr->pid);
427 return;
429 printk("%d lock%s held by %s/%d:\n",
430 depth, depth > 1 ? "s" : "", curr->comm, curr->pid);
432 for (i = 0; i < depth; i++) {
433 printk(" #%d: ", i);
434 print_lock(curr->held_locks + i);
438 static void print_lock_class_header(struct lock_class *class, int depth)
440 int bit;
442 printk("%*s->", depth, "");
443 print_lock_name(class);
444 printk(" ops: %lu", class->ops);
445 printk(" {\n");
447 for (bit = 0; bit < LOCK_USAGE_STATES; bit++) {
448 if (class->usage_mask & (1 << bit)) {
449 int len = depth;
451 len += printk("%*s %s", depth, "", usage_str[bit]);
452 len += printk(" at:\n");
453 print_stack_trace(class->usage_traces + bit, len);
456 printk("%*s }\n", depth, "");
458 printk("%*s ... key at: ",depth,"");
459 print_ip_sym((unsigned long)class->key);
463 * printk all lock dependencies starting at <entry>:
465 static void print_lock_dependencies(struct lock_class *class, int depth)
467 struct lock_list *entry;
469 if (DEBUG_LOCKS_WARN_ON(depth >= 20))
470 return;
472 print_lock_class_header(class, depth);
474 list_for_each_entry(entry, &class->locks_after, entry) {
475 if (DEBUG_LOCKS_WARN_ON(!entry->class))
476 return;
478 print_lock_dependencies(entry->class, depth + 1);
480 printk("%*s ... acquired at:\n",depth,"");
481 print_stack_trace(&entry->trace, 2);
482 printk("\n");
487 * Add a new dependency to the head of the list:
489 static int add_lock_to_list(struct lock_class *class, struct lock_class *this,
490 struct list_head *head, unsigned long ip)
492 struct lock_list *entry;
494 * Lock not present yet - get a new dependency struct and
495 * add it to the list:
497 entry = alloc_list_entry();
498 if (!entry)
499 return 0;
501 entry->class = this;
502 if (!save_trace(&entry->trace))
503 return 0;
506 * Since we never remove from the dependency list, the list can
507 * be walked lockless by other CPUs, it's only allocation
508 * that must be protected by the spinlock. But this also means
509 * we must make new entries visible only once writes to the
510 * entry become visible - hence the RCU op:
512 list_add_tail_rcu(&entry->entry, head);
514 return 1;
518 * Recursive, forwards-direction lock-dependency checking, used for
519 * both noncyclic checking and for hardirq-unsafe/softirq-unsafe
520 * checking.
522 * (to keep the stackframe of the recursive functions small we
523 * use these global variables, and we also mark various helper
524 * functions as noinline.)
526 static struct held_lock *check_source, *check_target;
529 * Print a dependency chain entry (this is only done when a deadlock
530 * has been detected):
532 static noinline int
533 print_circular_bug_entry(struct lock_list *target, unsigned int depth)
535 if (debug_locks_silent)
536 return 0;
537 printk("\n-> #%u", depth);
538 print_lock_name(target->class);
539 printk(":\n");
540 print_stack_trace(&target->trace, 6);
542 return 0;
545 static void print_kernel_version(void)
547 printk("%s %.*s\n", init_utsname()->release,
548 (int)strcspn(init_utsname()->version, " "),
549 init_utsname()->version);
553 * When a circular dependency is detected, print the
554 * header first:
556 static noinline int
557 print_circular_bug_header(struct lock_list *entry, unsigned int depth)
559 struct task_struct *curr = current;
561 if (!debug_locks_off_graph_unlock() || debug_locks_silent)
562 return 0;
564 printk("\n=======================================================\n");
565 printk( "[ INFO: possible circular locking dependency detected ]\n");
566 print_kernel_version();
567 printk( "-------------------------------------------------------\n");
568 printk("%s/%d is trying to acquire lock:\n",
569 curr->comm, curr->pid);
570 print_lock(check_source);
571 printk("\nbut task is already holding lock:\n");
572 print_lock(check_target);
573 printk("\nwhich lock already depends on the new lock.\n\n");
574 printk("\nthe existing dependency chain (in reverse order) is:\n");
576 print_circular_bug_entry(entry, depth);
578 return 0;
581 static noinline int print_circular_bug_tail(void)
583 struct task_struct *curr = current;
584 struct lock_list this;
586 if (debug_locks_silent)
587 return 0;
589 this.class = check_source->class;
590 if (!save_trace(&this.trace))
591 return 0;
593 print_circular_bug_entry(&this, 0);
595 printk("\nother info that might help us debug this:\n\n");
596 lockdep_print_held_locks(curr);
598 printk("\nstack backtrace:\n");
599 dump_stack();
601 return 0;
604 #define RECURSION_LIMIT 40
606 static int noinline print_infinite_recursion_bug(void)
608 if (!debug_locks_off_graph_unlock())
609 return 0;
611 WARN_ON(1);
613 return 0;
617 * Prove that the dependency graph starting at <entry> can not
618 * lead to <target>. Print an error and return 0 if it does.
620 static noinline int
621 check_noncircular(struct lock_class *source, unsigned int depth)
623 struct lock_list *entry;
625 debug_atomic_inc(&nr_cyclic_check_recursions);
626 if (depth > max_recursion_depth)
627 max_recursion_depth = depth;
628 if (depth >= RECURSION_LIMIT)
629 return print_infinite_recursion_bug();
631 * Check this lock's dependency list:
633 list_for_each_entry(entry, &source->locks_after, entry) {
634 if (entry->class == check_target->class)
635 return print_circular_bug_header(entry, depth+1);
636 debug_atomic_inc(&nr_cyclic_checks);
637 if (!check_noncircular(entry->class, depth+1))
638 return print_circular_bug_entry(entry, depth+1);
640 return 1;
643 static int very_verbose(struct lock_class *class)
645 #if VERY_VERBOSE
646 return class_filter(class);
647 #endif
648 return 0;
650 #ifdef CONFIG_TRACE_IRQFLAGS
653 * Forwards and backwards subgraph searching, for the purposes of
654 * proving that two subgraphs can be connected by a new dependency
655 * without creating any illegal irq-safe -> irq-unsafe lock dependency.
657 static enum lock_usage_bit find_usage_bit;
658 static struct lock_class *forwards_match, *backwards_match;
661 * Find a node in the forwards-direction dependency sub-graph starting
662 * at <source> that matches <find_usage_bit>.
664 * Return 2 if such a node exists in the subgraph, and put that node
665 * into <forwards_match>.
667 * Return 1 otherwise and keep <forwards_match> unchanged.
668 * Return 0 on error.
670 static noinline int
671 find_usage_forwards(struct lock_class *source, unsigned int depth)
673 struct lock_list *entry;
674 int ret;
676 if (depth > max_recursion_depth)
677 max_recursion_depth = depth;
678 if (depth >= RECURSION_LIMIT)
679 return print_infinite_recursion_bug();
681 debug_atomic_inc(&nr_find_usage_forwards_checks);
682 if (source->usage_mask & (1 << find_usage_bit)) {
683 forwards_match = source;
684 return 2;
688 * Check this lock's dependency list:
690 list_for_each_entry(entry, &source->locks_after, entry) {
691 debug_atomic_inc(&nr_find_usage_forwards_recursions);
692 ret = find_usage_forwards(entry->class, depth+1);
693 if (ret == 2 || ret == 0)
694 return ret;
696 return 1;
700 * Find a node in the backwards-direction dependency sub-graph starting
701 * at <source> that matches <find_usage_bit>.
703 * Return 2 if such a node exists in the subgraph, and put that node
704 * into <backwards_match>.
706 * Return 1 otherwise and keep <backwards_match> unchanged.
707 * Return 0 on error.
709 static noinline int
710 find_usage_backwards(struct lock_class *source, unsigned int depth)
712 struct lock_list *entry;
713 int ret;
715 if (depth > max_recursion_depth)
716 max_recursion_depth = depth;
717 if (depth >= RECURSION_LIMIT)
718 return print_infinite_recursion_bug();
720 debug_atomic_inc(&nr_find_usage_backwards_checks);
721 if (source->usage_mask & (1 << find_usage_bit)) {
722 backwards_match = source;
723 return 2;
727 * Check this lock's dependency list:
729 list_for_each_entry(entry, &source->locks_before, entry) {
730 debug_atomic_inc(&nr_find_usage_backwards_recursions);
731 ret = find_usage_backwards(entry->class, depth+1);
732 if (ret == 2 || ret == 0)
733 return ret;
735 return 1;
738 static int
739 print_bad_irq_dependency(struct task_struct *curr,
740 struct held_lock *prev,
741 struct held_lock *next,
742 enum lock_usage_bit bit1,
743 enum lock_usage_bit bit2,
744 const char *irqclass)
746 if (!debug_locks_off_graph_unlock() || debug_locks_silent)
747 return 0;
749 printk("\n======================================================\n");
750 printk( "[ INFO: %s-safe -> %s-unsafe lock order detected ]\n",
751 irqclass, irqclass);
752 print_kernel_version();
753 printk( "------------------------------------------------------\n");
754 printk("%s/%d [HC%u[%lu]:SC%u[%lu]:HE%u:SE%u] is trying to acquire:\n",
755 curr->comm, curr->pid,
756 curr->hardirq_context, hardirq_count() >> HARDIRQ_SHIFT,
757 curr->softirq_context, softirq_count() >> SOFTIRQ_SHIFT,
758 curr->hardirqs_enabled,
759 curr->softirqs_enabled);
760 print_lock(next);
762 printk("\nand this task is already holding:\n");
763 print_lock(prev);
764 printk("which would create a new lock dependency:\n");
765 print_lock_name(prev->class);
766 printk(" ->");
767 print_lock_name(next->class);
768 printk("\n");
770 printk("\nbut this new dependency connects a %s-irq-safe lock:\n",
771 irqclass);
772 print_lock_name(backwards_match);
773 printk("\n... which became %s-irq-safe at:\n", irqclass);
775 print_stack_trace(backwards_match->usage_traces + bit1, 1);
777 printk("\nto a %s-irq-unsafe lock:\n", irqclass);
778 print_lock_name(forwards_match);
779 printk("\n... which became %s-irq-unsafe at:\n", irqclass);
780 printk("...");
782 print_stack_trace(forwards_match->usage_traces + bit2, 1);
784 printk("\nother info that might help us debug this:\n\n");
785 lockdep_print_held_locks(curr);
787 printk("\nthe %s-irq-safe lock's dependencies:\n", irqclass);
788 print_lock_dependencies(backwards_match, 0);
790 printk("\nthe %s-irq-unsafe lock's dependencies:\n", irqclass);
791 print_lock_dependencies(forwards_match, 0);
793 printk("\nstack backtrace:\n");
794 dump_stack();
796 return 0;
799 static int
800 check_usage(struct task_struct *curr, struct held_lock *prev,
801 struct held_lock *next, enum lock_usage_bit bit_backwards,
802 enum lock_usage_bit bit_forwards, const char *irqclass)
804 int ret;
806 find_usage_bit = bit_backwards;
807 /* fills in <backwards_match> */
808 ret = find_usage_backwards(prev->class, 0);
809 if (!ret || ret == 1)
810 return ret;
812 find_usage_bit = bit_forwards;
813 ret = find_usage_forwards(next->class, 0);
814 if (!ret || ret == 1)
815 return ret;
816 /* ret == 2 */
817 return print_bad_irq_dependency(curr, prev, next,
818 bit_backwards, bit_forwards, irqclass);
821 #endif
823 static int
824 print_deadlock_bug(struct task_struct *curr, struct held_lock *prev,
825 struct held_lock *next)
827 if (!debug_locks_off_graph_unlock() || debug_locks_silent)
828 return 0;
830 printk("\n=============================================\n");
831 printk( "[ INFO: possible recursive locking detected ]\n");
832 print_kernel_version();
833 printk( "---------------------------------------------\n");
834 printk("%s/%d is trying to acquire lock:\n",
835 curr->comm, curr->pid);
836 print_lock(next);
837 printk("\nbut task is already holding lock:\n");
838 print_lock(prev);
840 printk("\nother info that might help us debug this:\n");
841 lockdep_print_held_locks(curr);
843 printk("\nstack backtrace:\n");
844 dump_stack();
846 return 0;
850 * Check whether we are holding such a class already.
852 * (Note that this has to be done separately, because the graph cannot
853 * detect such classes of deadlocks.)
855 * Returns: 0 on deadlock detected, 1 on OK, 2 on recursive read
857 static int
858 check_deadlock(struct task_struct *curr, struct held_lock *next,
859 struct lockdep_map *next_instance, int read)
861 struct held_lock *prev;
862 int i;
864 for (i = 0; i < curr->lockdep_depth; i++) {
865 prev = curr->held_locks + i;
866 if (prev->class != next->class)
867 continue;
869 * Allow read-after-read recursion of the same
870 * lock class (i.e. read_lock(lock)+read_lock(lock)):
872 if ((read == 2) && prev->read)
873 return 2;
874 return print_deadlock_bug(curr, prev, next);
876 return 1;
880 * There was a chain-cache miss, and we are about to add a new dependency
881 * to a previous lock. We recursively validate the following rules:
883 * - would the adding of the <prev> -> <next> dependency create a
884 * circular dependency in the graph? [== circular deadlock]
886 * - does the new prev->next dependency connect any hardirq-safe lock
887 * (in the full backwards-subgraph starting at <prev>) with any
888 * hardirq-unsafe lock (in the full forwards-subgraph starting at
889 * <next>)? [== illegal lock inversion with hardirq contexts]
891 * - does the new prev->next dependency connect any softirq-safe lock
892 * (in the full backwards-subgraph starting at <prev>) with any
893 * softirq-unsafe lock (in the full forwards-subgraph starting at
894 * <next>)? [== illegal lock inversion with softirq contexts]
896 * any of these scenarios could lead to a deadlock.
898 * Then if all the validations pass, we add the forwards and backwards
899 * dependency.
901 static int
902 check_prev_add(struct task_struct *curr, struct held_lock *prev,
903 struct held_lock *next)
905 struct lock_list *entry;
906 int ret;
909 * Prove that the new <prev> -> <next> dependency would not
910 * create a circular dependency in the graph. (We do this by
911 * forward-recursing into the graph starting at <next>, and
912 * checking whether we can reach <prev>.)
914 * We are using global variables to control the recursion, to
915 * keep the stackframe size of the recursive functions low:
917 check_source = next;
918 check_target = prev;
919 if (!(check_noncircular(next->class, 0)))
920 return print_circular_bug_tail();
922 #ifdef CONFIG_TRACE_IRQFLAGS
924 * Prove that the new dependency does not connect a hardirq-safe
925 * lock with a hardirq-unsafe lock - to achieve this we search
926 * the backwards-subgraph starting at <prev>, and the
927 * forwards-subgraph starting at <next>:
929 if (!check_usage(curr, prev, next, LOCK_USED_IN_HARDIRQ,
930 LOCK_ENABLED_HARDIRQS, "hard"))
931 return 0;
934 * Prove that the new dependency does not connect a hardirq-safe-read
935 * lock with a hardirq-unsafe lock - to achieve this we search
936 * the backwards-subgraph starting at <prev>, and the
937 * forwards-subgraph starting at <next>:
939 if (!check_usage(curr, prev, next, LOCK_USED_IN_HARDIRQ_READ,
940 LOCK_ENABLED_HARDIRQS, "hard-read"))
941 return 0;
944 * Prove that the new dependency does not connect a softirq-safe
945 * lock with a softirq-unsafe lock - to achieve this we search
946 * the backwards-subgraph starting at <prev>, and the
947 * forwards-subgraph starting at <next>:
949 if (!check_usage(curr, prev, next, LOCK_USED_IN_SOFTIRQ,
950 LOCK_ENABLED_SOFTIRQS, "soft"))
951 return 0;
953 * Prove that the new dependency does not connect a softirq-safe-read
954 * lock with a softirq-unsafe lock - to achieve this we search
955 * the backwards-subgraph starting at <prev>, and the
956 * forwards-subgraph starting at <next>:
958 if (!check_usage(curr, prev, next, LOCK_USED_IN_SOFTIRQ_READ,
959 LOCK_ENABLED_SOFTIRQS, "soft"))
960 return 0;
961 #endif
963 * For recursive read-locks we do all the dependency checks,
964 * but we dont store read-triggered dependencies (only
965 * write-triggered dependencies). This ensures that only the
966 * write-side dependencies matter, and that if for example a
967 * write-lock never takes any other locks, then the reads are
968 * equivalent to a NOP.
970 if (next->read == 2 || prev->read == 2)
971 return 1;
973 * Is the <prev> -> <next> dependency already present?
975 * (this may occur even though this is a new chain: consider
976 * e.g. the L1 -> L2 -> L3 -> L4 and the L5 -> L1 -> L2 -> L3
977 * chains - the second one will be new, but L1 already has
978 * L2 added to its dependency list, due to the first chain.)
980 list_for_each_entry(entry, &prev->class->locks_after, entry) {
981 if (entry->class == next->class)
982 return 2;
986 * Ok, all validations passed, add the new lock
987 * to the previous lock's dependency list:
989 ret = add_lock_to_list(prev->class, next->class,
990 &prev->class->locks_after, next->acquire_ip);
991 if (!ret)
992 return 0;
994 ret = add_lock_to_list(next->class, prev->class,
995 &next->class->locks_before, next->acquire_ip);
996 if (!ret)
997 return 0;
1000 * Debugging printouts:
1002 if (verbose(prev->class) || verbose(next->class)) {
1003 graph_unlock();
1004 printk("\n new dependency: ");
1005 print_lock_name(prev->class);
1006 printk(" => ");
1007 print_lock_name(next->class);
1008 printk("\n");
1009 dump_stack();
1010 return graph_lock();
1012 return 1;
1016 * Add the dependency to all directly-previous locks that are 'relevant'.
1017 * The ones that are relevant are (in increasing distance from curr):
1018 * all consecutive trylock entries and the final non-trylock entry - or
1019 * the end of this context's lock-chain - whichever comes first.
1021 static int
1022 check_prevs_add(struct task_struct *curr, struct held_lock *next)
1024 int depth = curr->lockdep_depth;
1025 struct held_lock *hlock;
1028 * Debugging checks.
1030 * Depth must not be zero for a non-head lock:
1032 if (!depth)
1033 goto out_bug;
1035 * At least two relevant locks must exist for this
1036 * to be a head:
1038 if (curr->held_locks[depth].irq_context !=
1039 curr->held_locks[depth-1].irq_context)
1040 goto out_bug;
1042 for (;;) {
1043 hlock = curr->held_locks + depth-1;
1045 * Only non-recursive-read entries get new dependencies
1046 * added:
1048 if (hlock->read != 2) {
1049 if (!check_prev_add(curr, hlock, next))
1050 return 0;
1052 * Stop after the first non-trylock entry,
1053 * as non-trylock entries have added their
1054 * own direct dependencies already, so this
1055 * lock is connected to them indirectly:
1057 if (!hlock->trylock)
1058 break;
1060 depth--;
1062 * End of lock-stack?
1064 if (!depth)
1065 break;
1067 * Stop the search if we cross into another context:
1069 if (curr->held_locks[depth].irq_context !=
1070 curr->held_locks[depth-1].irq_context)
1071 break;
1073 return 1;
1074 out_bug:
1075 if (!debug_locks_off_graph_unlock())
1076 return 0;
1078 WARN_ON(1);
1080 return 0;
1085 * Is this the address of a static object:
1087 static int static_obj(void *obj)
1089 unsigned long start = (unsigned long) &_stext,
1090 end = (unsigned long) &_end,
1091 addr = (unsigned long) obj;
1092 #ifdef CONFIG_SMP
1093 int i;
1094 #endif
1097 * static variable?
1099 if ((addr >= start) && (addr < end))
1100 return 1;
1102 #ifdef CONFIG_SMP
1104 * percpu var?
1106 for_each_possible_cpu(i) {
1107 start = (unsigned long) &__per_cpu_start + per_cpu_offset(i);
1108 end = (unsigned long) &__per_cpu_start + PERCPU_ENOUGH_ROOM
1109 + per_cpu_offset(i);
1111 if ((addr >= start) && (addr < end))
1112 return 1;
1114 #endif
1117 * module var?
1119 return is_module_address(addr);
1123 * To make lock name printouts unique, we calculate a unique
1124 * class->name_version generation counter:
1126 static int count_matching_names(struct lock_class *new_class)
1128 struct lock_class *class;
1129 int count = 0;
1131 if (!new_class->name)
1132 return 0;
1134 list_for_each_entry(class, &all_lock_classes, lock_entry) {
1135 if (new_class->key - new_class->subclass == class->key)
1136 return class->name_version;
1137 if (class->name && !strcmp(class->name, new_class->name))
1138 count = max(count, class->name_version);
1141 return count + 1;
1145 * Register a lock's class in the hash-table, if the class is not present
1146 * yet. Otherwise we look it up. We cache the result in the lock object
1147 * itself, so actual lookup of the hash should be once per lock object.
1149 static inline struct lock_class *
1150 look_up_lock_class(struct lockdep_map *lock, unsigned int subclass)
1152 struct lockdep_subclass_key *key;
1153 struct list_head *hash_head;
1154 struct lock_class *class;
1156 #ifdef CONFIG_DEBUG_LOCKDEP
1158 * If the architecture calls into lockdep before initializing
1159 * the hashes then we'll warn about it later. (we cannot printk
1160 * right now)
1162 if (unlikely(!lockdep_initialized)) {
1163 lockdep_init();
1164 lockdep_init_error = 1;
1166 #endif
1169 * Static locks do not have their class-keys yet - for them the key
1170 * is the lock object itself:
1172 if (unlikely(!lock->key))
1173 lock->key = (void *)lock;
1176 * NOTE: the class-key must be unique. For dynamic locks, a static
1177 * lock_class_key variable is passed in through the mutex_init()
1178 * (or spin_lock_init()) call - which acts as the key. For static
1179 * locks we use the lock object itself as the key.
1181 BUILD_BUG_ON(sizeof(struct lock_class_key) > sizeof(struct lock_class));
1183 key = lock->key->subkeys + subclass;
1185 hash_head = classhashentry(key);
1188 * We can walk the hash lockfree, because the hash only
1189 * grows, and we are careful when adding entries to the end:
1191 list_for_each_entry(class, hash_head, hash_entry)
1192 if (class->key == key)
1193 return class;
1195 return NULL;
1199 * Register a lock's class in the hash-table, if the class is not present
1200 * yet. Otherwise we look it up. We cache the result in the lock object
1201 * itself, so actual lookup of the hash should be once per lock object.
1203 static inline struct lock_class *
1204 register_lock_class(struct lockdep_map *lock, unsigned int subclass, int force)
1206 struct lockdep_subclass_key *key;
1207 struct list_head *hash_head;
1208 struct lock_class *class;
1209 unsigned long flags;
1211 class = look_up_lock_class(lock, subclass);
1212 if (likely(class))
1213 return class;
1216 * Debug-check: all keys must be persistent!
1218 if (!static_obj(lock->key)) {
1219 debug_locks_off();
1220 printk("INFO: trying to register non-static key.\n");
1221 printk("the code is fine but needs lockdep annotation.\n");
1222 printk("turning off the locking correctness validator.\n");
1223 dump_stack();
1225 return NULL;
1228 key = lock->key->subkeys + subclass;
1229 hash_head = classhashentry(key);
1231 raw_local_irq_save(flags);
1232 if (!graph_lock()) {
1233 raw_local_irq_restore(flags);
1234 return NULL;
1237 * We have to do the hash-walk again, to avoid races
1238 * with another CPU:
1240 list_for_each_entry(class, hash_head, hash_entry)
1241 if (class->key == key)
1242 goto out_unlock_set;
1244 * Allocate a new key from the static array, and add it to
1245 * the hash:
1247 if (nr_lock_classes >= MAX_LOCKDEP_KEYS) {
1248 if (!debug_locks_off_graph_unlock()) {
1249 raw_local_irq_restore(flags);
1250 return NULL;
1252 raw_local_irq_restore(flags);
1254 printk("BUG: MAX_LOCKDEP_KEYS too low!\n");
1255 printk("turning off the locking correctness validator.\n");
1256 return NULL;
1258 class = lock_classes + nr_lock_classes++;
1259 debug_atomic_inc(&nr_unused_locks);
1260 class->key = key;
1261 class->name = lock->name;
1262 class->subclass = subclass;
1263 INIT_LIST_HEAD(&class->lock_entry);
1264 INIT_LIST_HEAD(&class->locks_before);
1265 INIT_LIST_HEAD(&class->locks_after);
1266 class->name_version = count_matching_names(class);
1268 * We use RCU's safe list-add method to make
1269 * parallel walking of the hash-list safe:
1271 list_add_tail_rcu(&class->hash_entry, hash_head);
1273 if (verbose(class)) {
1274 graph_unlock();
1275 raw_local_irq_restore(flags);
1277 printk("\nnew class %p: %s", class->key, class->name);
1278 if (class->name_version > 1)
1279 printk("#%d", class->name_version);
1280 printk("\n");
1281 dump_stack();
1283 raw_local_irq_save(flags);
1284 if (!graph_lock()) {
1285 raw_local_irq_restore(flags);
1286 return NULL;
1289 out_unlock_set:
1290 graph_unlock();
1291 raw_local_irq_restore(flags);
1293 if (!subclass || force)
1294 lock->class_cache = class;
1296 DEBUG_LOCKS_WARN_ON(class->subclass != subclass);
1298 return class;
1302 * Look up a dependency chain. If the key is not present yet then
1303 * add it and return 0 - in this case the new dependency chain is
1304 * validated. If the key is already hashed, return 1.
1306 static inline int lookup_chain_cache(u64 chain_key, struct lock_class *class)
1308 struct list_head *hash_head = chainhashentry(chain_key);
1309 struct lock_chain *chain;
1311 DEBUG_LOCKS_WARN_ON(!irqs_disabled());
1313 * We can walk it lock-free, because entries only get added
1314 * to the hash:
1316 list_for_each_entry(chain, hash_head, entry) {
1317 if (chain->chain_key == chain_key) {
1318 cache_hit:
1319 debug_atomic_inc(&chain_lookup_hits);
1320 if (very_verbose(class))
1321 printk("\nhash chain already cached, key: "
1322 "%016Lx tail class: [%p] %s\n",
1323 (unsigned long long)chain_key,
1324 class->key, class->name);
1325 return 0;
1328 if (very_verbose(class))
1329 printk("\nnew hash chain, key: %016Lx tail class: [%p] %s\n",
1330 (unsigned long long)chain_key, class->key, class->name);
1332 * Allocate a new chain entry from the static array, and add
1333 * it to the hash:
1335 if (!graph_lock())
1336 return 0;
1338 * We have to walk the chain again locked - to avoid duplicates:
1340 list_for_each_entry(chain, hash_head, entry) {
1341 if (chain->chain_key == chain_key) {
1342 graph_unlock();
1343 goto cache_hit;
1346 if (unlikely(nr_lock_chains >= MAX_LOCKDEP_CHAINS)) {
1347 if (!debug_locks_off_graph_unlock())
1348 return 0;
1350 printk("BUG: MAX_LOCKDEP_CHAINS too low!\n");
1351 printk("turning off the locking correctness validator.\n");
1352 return 0;
1354 chain = lock_chains + nr_lock_chains++;
1355 chain->chain_key = chain_key;
1356 list_add_tail_rcu(&chain->entry, hash_head);
1357 debug_atomic_inc(&chain_lookup_misses);
1358 #ifdef CONFIG_TRACE_IRQFLAGS
1359 if (current->hardirq_context)
1360 nr_hardirq_chains++;
1361 else {
1362 if (current->softirq_context)
1363 nr_softirq_chains++;
1364 else
1365 nr_process_chains++;
1367 #else
1368 nr_process_chains++;
1369 #endif
1371 return 1;
1375 * We are building curr_chain_key incrementally, so double-check
1376 * it from scratch, to make sure that it's done correctly:
1378 static void check_chain_key(struct task_struct *curr)
1380 #ifdef CONFIG_DEBUG_LOCKDEP
1381 struct held_lock *hlock, *prev_hlock = NULL;
1382 unsigned int i, id;
1383 u64 chain_key = 0;
1385 for (i = 0; i < curr->lockdep_depth; i++) {
1386 hlock = curr->held_locks + i;
1387 if (chain_key != hlock->prev_chain_key) {
1388 debug_locks_off();
1389 printk("hm#1, depth: %u [%u], %016Lx != %016Lx\n",
1390 curr->lockdep_depth, i,
1391 (unsigned long long)chain_key,
1392 (unsigned long long)hlock->prev_chain_key);
1393 WARN_ON(1);
1394 return;
1396 id = hlock->class - lock_classes;
1397 DEBUG_LOCKS_WARN_ON(id >= MAX_LOCKDEP_KEYS);
1398 if (prev_hlock && (prev_hlock->irq_context !=
1399 hlock->irq_context))
1400 chain_key = 0;
1401 chain_key = iterate_chain_key(chain_key, id);
1402 prev_hlock = hlock;
1404 if (chain_key != curr->curr_chain_key) {
1405 debug_locks_off();
1406 printk("hm#2, depth: %u [%u], %016Lx != %016Lx\n",
1407 curr->lockdep_depth, i,
1408 (unsigned long long)chain_key,
1409 (unsigned long long)curr->curr_chain_key);
1410 WARN_ON(1);
1412 #endif
1415 #ifdef CONFIG_TRACE_IRQFLAGS
1418 * print irq inversion bug:
1420 static int
1421 print_irq_inversion_bug(struct task_struct *curr, struct lock_class *other,
1422 struct held_lock *this, int forwards,
1423 const char *irqclass)
1425 if (!debug_locks_off_graph_unlock() || debug_locks_silent)
1426 return 0;
1428 printk("\n=========================================================\n");
1429 printk( "[ INFO: possible irq lock inversion dependency detected ]\n");
1430 print_kernel_version();
1431 printk( "---------------------------------------------------------\n");
1432 printk("%s/%d just changed the state of lock:\n",
1433 curr->comm, curr->pid);
1434 print_lock(this);
1435 if (forwards)
1436 printk("but this lock took another, %s-irq-unsafe lock in the past:\n", irqclass);
1437 else
1438 printk("but this lock was taken by another, %s-irq-safe lock in the past:\n", irqclass);
1439 print_lock_name(other);
1440 printk("\n\nand interrupts could create inverse lock ordering between them.\n\n");
1442 printk("\nother info that might help us debug this:\n");
1443 lockdep_print_held_locks(curr);
1445 printk("\nthe first lock's dependencies:\n");
1446 print_lock_dependencies(this->class, 0);
1448 printk("\nthe second lock's dependencies:\n");
1449 print_lock_dependencies(other, 0);
1451 printk("\nstack backtrace:\n");
1452 dump_stack();
1454 return 0;
1458 * Prove that in the forwards-direction subgraph starting at <this>
1459 * there is no lock matching <mask>:
1461 static int
1462 check_usage_forwards(struct task_struct *curr, struct held_lock *this,
1463 enum lock_usage_bit bit, const char *irqclass)
1465 int ret;
1467 find_usage_bit = bit;
1468 /* fills in <forwards_match> */
1469 ret = find_usage_forwards(this->class, 0);
1470 if (!ret || ret == 1)
1471 return ret;
1473 return print_irq_inversion_bug(curr, forwards_match, this, 1, irqclass);
1477 * Prove that in the backwards-direction subgraph starting at <this>
1478 * there is no lock matching <mask>:
1480 static int
1481 check_usage_backwards(struct task_struct *curr, struct held_lock *this,
1482 enum lock_usage_bit bit, const char *irqclass)
1484 int ret;
1486 find_usage_bit = bit;
1487 /* fills in <backwards_match> */
1488 ret = find_usage_backwards(this->class, 0);
1489 if (!ret || ret == 1)
1490 return ret;
1492 return print_irq_inversion_bug(curr, backwards_match, this, 0, irqclass);
1495 void print_irqtrace_events(struct task_struct *curr)
1497 printk("irq event stamp: %u\n", curr->irq_events);
1498 printk("hardirqs last enabled at (%u): ", curr->hardirq_enable_event);
1499 print_ip_sym(curr->hardirq_enable_ip);
1500 printk("hardirqs last disabled at (%u): ", curr->hardirq_disable_event);
1501 print_ip_sym(curr->hardirq_disable_ip);
1502 printk("softirqs last enabled at (%u): ", curr->softirq_enable_event);
1503 print_ip_sym(curr->softirq_enable_ip);
1504 printk("softirqs last disabled at (%u): ", curr->softirq_disable_event);
1505 print_ip_sym(curr->softirq_disable_ip);
1508 #endif
1510 static int
1511 print_usage_bug(struct task_struct *curr, struct held_lock *this,
1512 enum lock_usage_bit prev_bit, enum lock_usage_bit new_bit)
1514 if (!debug_locks_off_graph_unlock() || debug_locks_silent)
1515 return 0;
1517 printk("\n=================================\n");
1518 printk( "[ INFO: inconsistent lock state ]\n");
1519 print_kernel_version();
1520 printk( "---------------------------------\n");
1522 printk("inconsistent {%s} -> {%s} usage.\n",
1523 usage_str[prev_bit], usage_str[new_bit]);
1525 printk("%s/%d [HC%u[%lu]:SC%u[%lu]:HE%u:SE%u] takes:\n",
1526 curr->comm, curr->pid,
1527 trace_hardirq_context(curr), hardirq_count() >> HARDIRQ_SHIFT,
1528 trace_softirq_context(curr), softirq_count() >> SOFTIRQ_SHIFT,
1529 trace_hardirqs_enabled(curr),
1530 trace_softirqs_enabled(curr));
1531 print_lock(this);
1533 printk("{%s} state was registered at:\n", usage_str[prev_bit]);
1534 print_stack_trace(this->class->usage_traces + prev_bit, 1);
1536 print_irqtrace_events(curr);
1537 printk("\nother info that might help us debug this:\n");
1538 lockdep_print_held_locks(curr);
1540 printk("\nstack backtrace:\n");
1541 dump_stack();
1543 return 0;
1547 * Print out an error if an invalid bit is set:
1549 static inline int
1550 valid_state(struct task_struct *curr, struct held_lock *this,
1551 enum lock_usage_bit new_bit, enum lock_usage_bit bad_bit)
1553 if (unlikely(this->class->usage_mask & (1 << bad_bit)))
1554 return print_usage_bug(curr, this, bad_bit, new_bit);
1555 return 1;
1558 #define STRICT_READ_CHECKS 1
1561 * Mark a lock with a usage bit, and validate the state transition:
1563 static int mark_lock(struct task_struct *curr, struct held_lock *this,
1564 enum lock_usage_bit new_bit, unsigned long ip)
1566 unsigned int new_mask = 1 << new_bit, ret = 1;
1569 * If already set then do not dirty the cacheline,
1570 * nor do any checks:
1572 if (likely(this->class->usage_mask & new_mask))
1573 return 1;
1575 if (!graph_lock())
1576 return 0;
1578 * Make sure we didnt race:
1580 if (unlikely(this->class->usage_mask & new_mask)) {
1581 graph_unlock();
1582 return 1;
1585 this->class->usage_mask |= new_mask;
1587 #ifdef CONFIG_TRACE_IRQFLAGS
1588 if (new_bit == LOCK_ENABLED_HARDIRQS ||
1589 new_bit == LOCK_ENABLED_HARDIRQS_READ)
1590 ip = curr->hardirq_enable_ip;
1591 else if (new_bit == LOCK_ENABLED_SOFTIRQS ||
1592 new_bit == LOCK_ENABLED_SOFTIRQS_READ)
1593 ip = curr->softirq_enable_ip;
1594 #endif
1595 if (!save_trace(this->class->usage_traces + new_bit))
1596 return 0;
1598 switch (new_bit) {
1599 #ifdef CONFIG_TRACE_IRQFLAGS
1600 case LOCK_USED_IN_HARDIRQ:
1601 if (!valid_state(curr, this, new_bit, LOCK_ENABLED_HARDIRQS))
1602 return 0;
1603 if (!valid_state(curr, this, new_bit,
1604 LOCK_ENABLED_HARDIRQS_READ))
1605 return 0;
1607 * just marked it hardirq-safe, check that this lock
1608 * took no hardirq-unsafe lock in the past:
1610 if (!check_usage_forwards(curr, this,
1611 LOCK_ENABLED_HARDIRQS, "hard"))
1612 return 0;
1613 #if STRICT_READ_CHECKS
1615 * just marked it hardirq-safe, check that this lock
1616 * took no hardirq-unsafe-read lock in the past:
1618 if (!check_usage_forwards(curr, this,
1619 LOCK_ENABLED_HARDIRQS_READ, "hard-read"))
1620 return 0;
1621 #endif
1622 if (hardirq_verbose(this->class))
1623 ret = 2;
1624 break;
1625 case LOCK_USED_IN_SOFTIRQ:
1626 if (!valid_state(curr, this, new_bit, LOCK_ENABLED_SOFTIRQS))
1627 return 0;
1628 if (!valid_state(curr, this, new_bit,
1629 LOCK_ENABLED_SOFTIRQS_READ))
1630 return 0;
1632 * just marked it softirq-safe, check that this lock
1633 * took no softirq-unsafe lock in the past:
1635 if (!check_usage_forwards(curr, this,
1636 LOCK_ENABLED_SOFTIRQS, "soft"))
1637 return 0;
1638 #if STRICT_READ_CHECKS
1640 * just marked it softirq-safe, check that this lock
1641 * took no softirq-unsafe-read lock in the past:
1643 if (!check_usage_forwards(curr, this,
1644 LOCK_ENABLED_SOFTIRQS_READ, "soft-read"))
1645 return 0;
1646 #endif
1647 if (softirq_verbose(this->class))
1648 ret = 2;
1649 break;
1650 case LOCK_USED_IN_HARDIRQ_READ:
1651 if (!valid_state(curr, this, new_bit, LOCK_ENABLED_HARDIRQS))
1652 return 0;
1654 * just marked it hardirq-read-safe, check that this lock
1655 * took no hardirq-unsafe lock in the past:
1657 if (!check_usage_forwards(curr, this,
1658 LOCK_ENABLED_HARDIRQS, "hard"))
1659 return 0;
1660 if (hardirq_verbose(this->class))
1661 ret = 2;
1662 break;
1663 case LOCK_USED_IN_SOFTIRQ_READ:
1664 if (!valid_state(curr, this, new_bit, LOCK_ENABLED_SOFTIRQS))
1665 return 0;
1667 * just marked it softirq-read-safe, check that this lock
1668 * took no softirq-unsafe lock in the past:
1670 if (!check_usage_forwards(curr, this,
1671 LOCK_ENABLED_SOFTIRQS, "soft"))
1672 return 0;
1673 if (softirq_verbose(this->class))
1674 ret = 2;
1675 break;
1676 case LOCK_ENABLED_HARDIRQS:
1677 if (!valid_state(curr, this, new_bit, LOCK_USED_IN_HARDIRQ))
1678 return 0;
1679 if (!valid_state(curr, this, new_bit,
1680 LOCK_USED_IN_HARDIRQ_READ))
1681 return 0;
1683 * just marked it hardirq-unsafe, check that no hardirq-safe
1684 * lock in the system ever took it in the past:
1686 if (!check_usage_backwards(curr, this,
1687 LOCK_USED_IN_HARDIRQ, "hard"))
1688 return 0;
1689 #if STRICT_READ_CHECKS
1691 * just marked it hardirq-unsafe, check that no
1692 * hardirq-safe-read lock in the system ever took
1693 * it in the past:
1695 if (!check_usage_backwards(curr, this,
1696 LOCK_USED_IN_HARDIRQ_READ, "hard-read"))
1697 return 0;
1698 #endif
1699 if (hardirq_verbose(this->class))
1700 ret = 2;
1701 break;
1702 case LOCK_ENABLED_SOFTIRQS:
1703 if (!valid_state(curr, this, new_bit, LOCK_USED_IN_SOFTIRQ))
1704 return 0;
1705 if (!valid_state(curr, this, new_bit,
1706 LOCK_USED_IN_SOFTIRQ_READ))
1707 return 0;
1709 * just marked it softirq-unsafe, check that no softirq-safe
1710 * lock in the system ever took it in the past:
1712 if (!check_usage_backwards(curr, this,
1713 LOCK_USED_IN_SOFTIRQ, "soft"))
1714 return 0;
1715 #if STRICT_READ_CHECKS
1717 * just marked it softirq-unsafe, check that no
1718 * softirq-safe-read lock in the system ever took
1719 * it in the past:
1721 if (!check_usage_backwards(curr, this,
1722 LOCK_USED_IN_SOFTIRQ_READ, "soft-read"))
1723 return 0;
1724 #endif
1725 if (softirq_verbose(this->class))
1726 ret = 2;
1727 break;
1728 case LOCK_ENABLED_HARDIRQS_READ:
1729 if (!valid_state(curr, this, new_bit, LOCK_USED_IN_HARDIRQ))
1730 return 0;
1731 #if STRICT_READ_CHECKS
1733 * just marked it hardirq-read-unsafe, check that no
1734 * hardirq-safe lock in the system ever took it in the past:
1736 if (!check_usage_backwards(curr, this,
1737 LOCK_USED_IN_HARDIRQ, "hard"))
1738 return 0;
1739 #endif
1740 if (hardirq_verbose(this->class))
1741 ret = 2;
1742 break;
1743 case LOCK_ENABLED_SOFTIRQS_READ:
1744 if (!valid_state(curr, this, new_bit, LOCK_USED_IN_SOFTIRQ))
1745 return 0;
1746 #if STRICT_READ_CHECKS
1748 * just marked it softirq-read-unsafe, check that no
1749 * softirq-safe lock in the system ever took it in the past:
1751 if (!check_usage_backwards(curr, this,
1752 LOCK_USED_IN_SOFTIRQ, "soft"))
1753 return 0;
1754 #endif
1755 if (softirq_verbose(this->class))
1756 ret = 2;
1757 break;
1758 #endif
1759 case LOCK_USED:
1761 * Add it to the global list of classes:
1763 list_add_tail_rcu(&this->class->lock_entry, &all_lock_classes);
1764 debug_atomic_dec(&nr_unused_locks);
1765 break;
1766 default:
1767 if (!debug_locks_off_graph_unlock())
1768 return 0;
1769 WARN_ON(1);
1770 return 0;
1773 graph_unlock();
1776 * We must printk outside of the graph_lock:
1778 if (ret == 2) {
1779 printk("\nmarked lock as {%s}:\n", usage_str[new_bit]);
1780 print_lock(this);
1781 print_irqtrace_events(curr);
1782 dump_stack();
1785 return ret;
1788 #ifdef CONFIG_TRACE_IRQFLAGS
1790 * Mark all held locks with a usage bit:
1792 static int
1793 mark_held_locks(struct task_struct *curr, int hardirq, unsigned long ip)
1795 enum lock_usage_bit usage_bit;
1796 struct held_lock *hlock;
1797 int i;
1799 for (i = 0; i < curr->lockdep_depth; i++) {
1800 hlock = curr->held_locks + i;
1802 if (hardirq) {
1803 if (hlock->read)
1804 usage_bit = LOCK_ENABLED_HARDIRQS_READ;
1805 else
1806 usage_bit = LOCK_ENABLED_HARDIRQS;
1807 } else {
1808 if (hlock->read)
1809 usage_bit = LOCK_ENABLED_SOFTIRQS_READ;
1810 else
1811 usage_bit = LOCK_ENABLED_SOFTIRQS;
1813 if (!mark_lock(curr, hlock, usage_bit, ip))
1814 return 0;
1817 return 1;
1821 * Debugging helper: via this flag we know that we are in
1822 * 'early bootup code', and will warn about any invalid irqs-on event:
1824 static int early_boot_irqs_enabled;
1826 void early_boot_irqs_off(void)
1828 early_boot_irqs_enabled = 0;
1831 void early_boot_irqs_on(void)
1833 early_boot_irqs_enabled = 1;
1837 * Hardirqs will be enabled:
1839 void trace_hardirqs_on(void)
1841 struct task_struct *curr = current;
1842 unsigned long ip;
1844 if (unlikely(!debug_locks || current->lockdep_recursion))
1845 return;
1847 if (DEBUG_LOCKS_WARN_ON(unlikely(!early_boot_irqs_enabled)))
1848 return;
1850 if (unlikely(curr->hardirqs_enabled)) {
1851 debug_atomic_inc(&redundant_hardirqs_on);
1852 return;
1854 /* we'll do an OFF -> ON transition: */
1855 curr->hardirqs_enabled = 1;
1856 ip = (unsigned long) __builtin_return_address(0);
1858 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
1859 return;
1860 if (DEBUG_LOCKS_WARN_ON(current->hardirq_context))
1861 return;
1863 * We are going to turn hardirqs on, so set the
1864 * usage bit for all held locks:
1866 if (!mark_held_locks(curr, 1, ip))
1867 return;
1869 * If we have softirqs enabled, then set the usage
1870 * bit for all held locks. (disabled hardirqs prevented
1871 * this bit from being set before)
1873 if (curr->softirqs_enabled)
1874 if (!mark_held_locks(curr, 0, ip))
1875 return;
1877 curr->hardirq_enable_ip = ip;
1878 curr->hardirq_enable_event = ++curr->irq_events;
1879 debug_atomic_inc(&hardirqs_on_events);
1882 EXPORT_SYMBOL(trace_hardirqs_on);
1885 * Hardirqs were disabled:
1887 void trace_hardirqs_off(void)
1889 struct task_struct *curr = current;
1891 if (unlikely(!debug_locks || current->lockdep_recursion))
1892 return;
1894 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
1895 return;
1897 if (curr->hardirqs_enabled) {
1899 * We have done an ON -> OFF transition:
1901 curr->hardirqs_enabled = 0;
1902 curr->hardirq_disable_ip = _RET_IP_;
1903 curr->hardirq_disable_event = ++curr->irq_events;
1904 debug_atomic_inc(&hardirqs_off_events);
1905 } else
1906 debug_atomic_inc(&redundant_hardirqs_off);
1909 EXPORT_SYMBOL(trace_hardirqs_off);
1912 * Softirqs will be enabled:
1914 void trace_softirqs_on(unsigned long ip)
1916 struct task_struct *curr = current;
1918 if (unlikely(!debug_locks))
1919 return;
1921 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
1922 return;
1924 if (curr->softirqs_enabled) {
1925 debug_atomic_inc(&redundant_softirqs_on);
1926 return;
1930 * We'll do an OFF -> ON transition:
1932 curr->softirqs_enabled = 1;
1933 curr->softirq_enable_ip = ip;
1934 curr->softirq_enable_event = ++curr->irq_events;
1935 debug_atomic_inc(&softirqs_on_events);
1937 * We are going to turn softirqs on, so set the
1938 * usage bit for all held locks, if hardirqs are
1939 * enabled too:
1941 if (curr->hardirqs_enabled)
1942 mark_held_locks(curr, 0, ip);
1946 * Softirqs were disabled:
1948 void trace_softirqs_off(unsigned long ip)
1950 struct task_struct *curr = current;
1952 if (unlikely(!debug_locks))
1953 return;
1955 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
1956 return;
1958 if (curr->softirqs_enabled) {
1960 * We have done an ON -> OFF transition:
1962 curr->softirqs_enabled = 0;
1963 curr->softirq_disable_ip = ip;
1964 curr->softirq_disable_event = ++curr->irq_events;
1965 debug_atomic_inc(&softirqs_off_events);
1966 DEBUG_LOCKS_WARN_ON(!softirq_count());
1967 } else
1968 debug_atomic_inc(&redundant_softirqs_off);
1971 #endif
1974 * Initialize a lock instance's lock-class mapping info:
1976 void lockdep_init_map(struct lockdep_map *lock, const char *name,
1977 struct lock_class_key *key, int subclass)
1979 if (unlikely(!debug_locks))
1980 return;
1982 if (DEBUG_LOCKS_WARN_ON(!key))
1983 return;
1984 if (DEBUG_LOCKS_WARN_ON(!name))
1985 return;
1987 * Sanity check, the lock-class key must be persistent:
1989 if (!static_obj(key)) {
1990 printk("BUG: key %p not in .data!\n", key);
1991 DEBUG_LOCKS_WARN_ON(1);
1992 return;
1994 lock->name = name;
1995 lock->key = key;
1996 lock->class_cache = NULL;
1997 if (subclass)
1998 register_lock_class(lock, subclass, 1);
2001 EXPORT_SYMBOL_GPL(lockdep_init_map);
2004 * This gets called for every mutex_lock*()/spin_lock*() operation.
2005 * We maintain the dependency maps and validate the locking attempt:
2007 static int __lock_acquire(struct lockdep_map *lock, unsigned int subclass,
2008 int trylock, int read, int check, int hardirqs_off,
2009 unsigned long ip)
2011 struct task_struct *curr = current;
2012 struct lock_class *class = NULL;
2013 struct held_lock *hlock;
2014 unsigned int depth, id;
2015 int chain_head = 0;
2016 u64 chain_key;
2018 if (unlikely(!debug_locks))
2019 return 0;
2021 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2022 return 0;
2024 if (unlikely(subclass >= MAX_LOCKDEP_SUBCLASSES)) {
2025 debug_locks_off();
2026 printk("BUG: MAX_LOCKDEP_SUBCLASSES too low!\n");
2027 printk("turning off the locking correctness validator.\n");
2028 return 0;
2031 if (!subclass)
2032 class = lock->class_cache;
2034 * Not cached yet or subclass?
2036 if (unlikely(!class)) {
2037 class = register_lock_class(lock, subclass, 0);
2038 if (!class)
2039 return 0;
2041 debug_atomic_inc((atomic_t *)&class->ops);
2042 if (very_verbose(class)) {
2043 printk("\nacquire class [%p] %s", class->key, class->name);
2044 if (class->name_version > 1)
2045 printk("#%d", class->name_version);
2046 printk("\n");
2047 dump_stack();
2051 * Add the lock to the list of currently held locks.
2052 * (we dont increase the depth just yet, up until the
2053 * dependency checks are done)
2055 depth = curr->lockdep_depth;
2056 if (DEBUG_LOCKS_WARN_ON(depth >= MAX_LOCK_DEPTH))
2057 return 0;
2059 hlock = curr->held_locks + depth;
2061 hlock->class = class;
2062 hlock->acquire_ip = ip;
2063 hlock->instance = lock;
2064 hlock->trylock = trylock;
2065 hlock->read = read;
2066 hlock->check = check;
2067 hlock->hardirqs_off = hardirqs_off;
2069 if (check != 2)
2070 goto out_calc_hash;
2071 #ifdef CONFIG_TRACE_IRQFLAGS
2073 * If non-trylock use in a hardirq or softirq context, then
2074 * mark the lock as used in these contexts:
2076 if (!trylock) {
2077 if (read) {
2078 if (curr->hardirq_context)
2079 if (!mark_lock(curr, hlock,
2080 LOCK_USED_IN_HARDIRQ_READ, ip))
2081 return 0;
2082 if (curr->softirq_context)
2083 if (!mark_lock(curr, hlock,
2084 LOCK_USED_IN_SOFTIRQ_READ, ip))
2085 return 0;
2086 } else {
2087 if (curr->hardirq_context)
2088 if (!mark_lock(curr, hlock, LOCK_USED_IN_HARDIRQ, ip))
2089 return 0;
2090 if (curr->softirq_context)
2091 if (!mark_lock(curr, hlock, LOCK_USED_IN_SOFTIRQ, ip))
2092 return 0;
2095 if (!hardirqs_off) {
2096 if (read) {
2097 if (!mark_lock(curr, hlock,
2098 LOCK_ENABLED_HARDIRQS_READ, ip))
2099 return 0;
2100 if (curr->softirqs_enabled)
2101 if (!mark_lock(curr, hlock,
2102 LOCK_ENABLED_SOFTIRQS_READ, ip))
2103 return 0;
2104 } else {
2105 if (!mark_lock(curr, hlock,
2106 LOCK_ENABLED_HARDIRQS, ip))
2107 return 0;
2108 if (curr->softirqs_enabled)
2109 if (!mark_lock(curr, hlock,
2110 LOCK_ENABLED_SOFTIRQS, ip))
2111 return 0;
2114 #endif
2115 /* mark it as used: */
2116 if (!mark_lock(curr, hlock, LOCK_USED, ip))
2117 return 0;
2118 out_calc_hash:
2120 * Calculate the chain hash: it's the combined has of all the
2121 * lock keys along the dependency chain. We save the hash value
2122 * at every step so that we can get the current hash easily
2123 * after unlock. The chain hash is then used to cache dependency
2124 * results.
2126 * The 'key ID' is what is the most compact key value to drive
2127 * the hash, not class->key.
2129 id = class - lock_classes;
2130 if (DEBUG_LOCKS_WARN_ON(id >= MAX_LOCKDEP_KEYS))
2131 return 0;
2133 chain_key = curr->curr_chain_key;
2134 if (!depth) {
2135 if (DEBUG_LOCKS_WARN_ON(chain_key != 0))
2136 return 0;
2137 chain_head = 1;
2140 hlock->prev_chain_key = chain_key;
2142 #ifdef CONFIG_TRACE_IRQFLAGS
2144 * Keep track of points where we cross into an interrupt context:
2146 hlock->irq_context = 2*(curr->hardirq_context ? 1 : 0) +
2147 curr->softirq_context;
2148 if (depth) {
2149 struct held_lock *prev_hlock;
2151 prev_hlock = curr->held_locks + depth-1;
2153 * If we cross into another context, reset the
2154 * hash key (this also prevents the checking and the
2155 * adding of the dependency to 'prev'):
2157 if (prev_hlock->irq_context != hlock->irq_context) {
2158 chain_key = 0;
2159 chain_head = 1;
2162 #endif
2163 chain_key = iterate_chain_key(chain_key, id);
2164 curr->curr_chain_key = chain_key;
2167 * Trylock needs to maintain the stack of held locks, but it
2168 * does not add new dependencies, because trylock can be done
2169 * in any order.
2171 * We look up the chain_key and do the O(N^2) check and update of
2172 * the dependencies only if this is a new dependency chain.
2173 * (If lookup_chain_cache() returns with 1 it acquires
2174 * graph_lock for us)
2176 if (!trylock && (check == 2) && lookup_chain_cache(chain_key, class)) {
2178 * Check whether last held lock:
2180 * - is irq-safe, if this lock is irq-unsafe
2181 * - is softirq-safe, if this lock is hardirq-unsafe
2183 * And check whether the new lock's dependency graph
2184 * could lead back to the previous lock.
2186 * any of these scenarios could lead to a deadlock. If
2187 * All validations
2189 int ret = check_deadlock(curr, hlock, lock, read);
2191 if (!ret)
2192 return 0;
2194 * Mark recursive read, as we jump over it when
2195 * building dependencies (just like we jump over
2196 * trylock entries):
2198 if (ret == 2)
2199 hlock->read = 2;
2201 * Add dependency only if this lock is not the head
2202 * of the chain, and if it's not a secondary read-lock:
2204 if (!chain_head && ret != 2)
2205 if (!check_prevs_add(curr, hlock))
2206 return 0;
2207 graph_unlock();
2209 curr->lockdep_depth++;
2210 check_chain_key(curr);
2211 if (unlikely(curr->lockdep_depth >= MAX_LOCK_DEPTH)) {
2212 debug_locks_off();
2213 printk("BUG: MAX_LOCK_DEPTH too low!\n");
2214 printk("turning off the locking correctness validator.\n");
2215 return 0;
2217 if (unlikely(curr->lockdep_depth > max_lockdep_depth))
2218 max_lockdep_depth = curr->lockdep_depth;
2220 return 1;
2223 static int
2224 print_unlock_inbalance_bug(struct task_struct *curr, struct lockdep_map *lock,
2225 unsigned long ip)
2227 if (!debug_locks_off())
2228 return 0;
2229 if (debug_locks_silent)
2230 return 0;
2232 printk("\n=====================================\n");
2233 printk( "[ BUG: bad unlock balance detected! ]\n");
2234 printk( "-------------------------------------\n");
2235 printk("%s/%d is trying to release lock (",
2236 curr->comm, curr->pid);
2237 print_lockdep_cache(lock);
2238 printk(") at:\n");
2239 print_ip_sym(ip);
2240 printk("but there are no more locks to release!\n");
2241 printk("\nother info that might help us debug this:\n");
2242 lockdep_print_held_locks(curr);
2244 printk("\nstack backtrace:\n");
2245 dump_stack();
2247 return 0;
2251 * Common debugging checks for both nested and non-nested unlock:
2253 static int check_unlock(struct task_struct *curr, struct lockdep_map *lock,
2254 unsigned long ip)
2256 if (unlikely(!debug_locks))
2257 return 0;
2258 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2259 return 0;
2261 if (curr->lockdep_depth <= 0)
2262 return print_unlock_inbalance_bug(curr, lock, ip);
2264 return 1;
2268 * Remove the lock to the list of currently held locks in a
2269 * potentially non-nested (out of order) manner. This is a
2270 * relatively rare operation, as all the unlock APIs default
2271 * to nested mode (which uses lock_release()):
2273 static int
2274 lock_release_non_nested(struct task_struct *curr,
2275 struct lockdep_map *lock, unsigned long ip)
2277 struct held_lock *hlock, *prev_hlock;
2278 unsigned int depth;
2279 int i;
2282 * Check whether the lock exists in the current stack
2283 * of held locks:
2285 depth = curr->lockdep_depth;
2286 if (DEBUG_LOCKS_WARN_ON(!depth))
2287 return 0;
2289 prev_hlock = NULL;
2290 for (i = depth-1; i >= 0; i--) {
2291 hlock = curr->held_locks + i;
2293 * We must not cross into another context:
2295 if (prev_hlock && prev_hlock->irq_context != hlock->irq_context)
2296 break;
2297 if (hlock->instance == lock)
2298 goto found_it;
2299 prev_hlock = hlock;
2301 return print_unlock_inbalance_bug(curr, lock, ip);
2303 found_it:
2305 * We have the right lock to unlock, 'hlock' points to it.
2306 * Now we remove it from the stack, and add back the other
2307 * entries (if any), recalculating the hash along the way:
2309 curr->lockdep_depth = i;
2310 curr->curr_chain_key = hlock->prev_chain_key;
2312 for (i++; i < depth; i++) {
2313 hlock = curr->held_locks + i;
2314 if (!__lock_acquire(hlock->instance,
2315 hlock->class->subclass, hlock->trylock,
2316 hlock->read, hlock->check, hlock->hardirqs_off,
2317 hlock->acquire_ip))
2318 return 0;
2321 if (DEBUG_LOCKS_WARN_ON(curr->lockdep_depth != depth - 1))
2322 return 0;
2323 return 1;
2327 * Remove the lock to the list of currently held locks - this gets
2328 * called on mutex_unlock()/spin_unlock*() (or on a failed
2329 * mutex_lock_interruptible()). This is done for unlocks that nest
2330 * perfectly. (i.e. the current top of the lock-stack is unlocked)
2332 static int lock_release_nested(struct task_struct *curr,
2333 struct lockdep_map *lock, unsigned long ip)
2335 struct held_lock *hlock;
2336 unsigned int depth;
2339 * Pop off the top of the lock stack:
2341 depth = curr->lockdep_depth - 1;
2342 hlock = curr->held_locks + depth;
2345 * Is the unlock non-nested:
2347 if (hlock->instance != lock)
2348 return lock_release_non_nested(curr, lock, ip);
2349 curr->lockdep_depth--;
2351 if (DEBUG_LOCKS_WARN_ON(!depth && (hlock->prev_chain_key != 0)))
2352 return 0;
2354 curr->curr_chain_key = hlock->prev_chain_key;
2356 #ifdef CONFIG_DEBUG_LOCKDEP
2357 hlock->prev_chain_key = 0;
2358 hlock->class = NULL;
2359 hlock->acquire_ip = 0;
2360 hlock->irq_context = 0;
2361 #endif
2362 return 1;
2366 * Remove the lock to the list of currently held locks - this gets
2367 * called on mutex_unlock()/spin_unlock*() (or on a failed
2368 * mutex_lock_interruptible()). This is done for unlocks that nest
2369 * perfectly. (i.e. the current top of the lock-stack is unlocked)
2371 static void
2372 __lock_release(struct lockdep_map *lock, int nested, unsigned long ip)
2374 struct task_struct *curr = current;
2376 if (!check_unlock(curr, lock, ip))
2377 return;
2379 if (nested) {
2380 if (!lock_release_nested(curr, lock, ip))
2381 return;
2382 } else {
2383 if (!lock_release_non_nested(curr, lock, ip))
2384 return;
2387 check_chain_key(curr);
2391 * Check whether we follow the irq-flags state precisely:
2393 static void check_flags(unsigned long flags)
2395 #if defined(CONFIG_DEBUG_LOCKDEP) && defined(CONFIG_TRACE_IRQFLAGS)
2396 if (!debug_locks)
2397 return;
2399 if (irqs_disabled_flags(flags))
2400 DEBUG_LOCKS_WARN_ON(current->hardirqs_enabled);
2401 else
2402 DEBUG_LOCKS_WARN_ON(!current->hardirqs_enabled);
2405 * We dont accurately track softirq state in e.g.
2406 * hardirq contexts (such as on 4KSTACKS), so only
2407 * check if not in hardirq contexts:
2409 if (!hardirq_count()) {
2410 if (softirq_count())
2411 DEBUG_LOCKS_WARN_ON(current->softirqs_enabled);
2412 else
2413 DEBUG_LOCKS_WARN_ON(!current->softirqs_enabled);
2416 if (!debug_locks)
2417 print_irqtrace_events(current);
2418 #endif
2422 * We are not always called with irqs disabled - do that here,
2423 * and also avoid lockdep recursion:
2425 void lock_acquire(struct lockdep_map *lock, unsigned int subclass,
2426 int trylock, int read, int check, unsigned long ip)
2428 unsigned long flags;
2430 if (unlikely(current->lockdep_recursion))
2431 return;
2433 raw_local_irq_save(flags);
2434 check_flags(flags);
2436 current->lockdep_recursion = 1;
2437 __lock_acquire(lock, subclass, trylock, read, check,
2438 irqs_disabled_flags(flags), ip);
2439 current->lockdep_recursion = 0;
2440 raw_local_irq_restore(flags);
2443 EXPORT_SYMBOL_GPL(lock_acquire);
2445 void lock_release(struct lockdep_map *lock, int nested, unsigned long ip)
2447 unsigned long flags;
2449 if (unlikely(current->lockdep_recursion))
2450 return;
2452 raw_local_irq_save(flags);
2453 check_flags(flags);
2454 current->lockdep_recursion = 1;
2455 __lock_release(lock, nested, ip);
2456 current->lockdep_recursion = 0;
2457 raw_local_irq_restore(flags);
2460 EXPORT_SYMBOL_GPL(lock_release);
2463 * Used by the testsuite, sanitize the validator state
2464 * after a simulated failure:
2467 void lockdep_reset(void)
2469 unsigned long flags;
2470 int i;
2472 raw_local_irq_save(flags);
2473 current->curr_chain_key = 0;
2474 current->lockdep_depth = 0;
2475 current->lockdep_recursion = 0;
2476 memset(current->held_locks, 0, MAX_LOCK_DEPTH*sizeof(struct held_lock));
2477 nr_hardirq_chains = 0;
2478 nr_softirq_chains = 0;
2479 nr_process_chains = 0;
2480 debug_locks = 1;
2481 for (i = 0; i < CHAINHASH_SIZE; i++)
2482 INIT_LIST_HEAD(chainhash_table + i);
2483 raw_local_irq_restore(flags);
2486 static void zap_class(struct lock_class *class)
2488 int i;
2491 * Remove all dependencies this lock is
2492 * involved in:
2494 for (i = 0; i < nr_list_entries; i++) {
2495 if (list_entries[i].class == class)
2496 list_del_rcu(&list_entries[i].entry);
2499 * Unhash the class and remove it from the all_lock_classes list:
2501 list_del_rcu(&class->hash_entry);
2502 list_del_rcu(&class->lock_entry);
2506 static inline int within(void *addr, void *start, unsigned long size)
2508 return addr >= start && addr < start + size;
2511 void lockdep_free_key_range(void *start, unsigned long size)
2513 struct lock_class *class, *next;
2514 struct list_head *head;
2515 unsigned long flags;
2516 int i;
2518 raw_local_irq_save(flags);
2519 graph_lock();
2522 * Unhash all classes that were created by this module:
2524 for (i = 0; i < CLASSHASH_SIZE; i++) {
2525 head = classhash_table + i;
2526 if (list_empty(head))
2527 continue;
2528 list_for_each_entry_safe(class, next, head, hash_entry)
2529 if (within(class->key, start, size))
2530 zap_class(class);
2533 graph_unlock();
2534 raw_local_irq_restore(flags);
2537 void lockdep_reset_lock(struct lockdep_map *lock)
2539 struct lock_class *class, *next;
2540 struct list_head *head;
2541 unsigned long flags;
2542 int i, j;
2544 raw_local_irq_save(flags);
2547 * Remove all classes this lock might have:
2549 for (j = 0; j < MAX_LOCKDEP_SUBCLASSES; j++) {
2551 * If the class exists we look it up and zap it:
2553 class = look_up_lock_class(lock, j);
2554 if (class)
2555 zap_class(class);
2558 * Debug check: in the end all mapped classes should
2559 * be gone.
2561 graph_lock();
2562 for (i = 0; i < CLASSHASH_SIZE; i++) {
2563 head = classhash_table + i;
2564 if (list_empty(head))
2565 continue;
2566 list_for_each_entry_safe(class, next, head, hash_entry) {
2567 if (unlikely(class == lock->class_cache)) {
2568 if (debug_locks_off_graph_unlock())
2569 WARN_ON(1);
2570 goto out_restore;
2574 graph_unlock();
2576 out_restore:
2577 raw_local_irq_restore(flags);
2580 void __init lockdep_init(void)
2582 int i;
2585 * Some architectures have their own start_kernel()
2586 * code which calls lockdep_init(), while we also
2587 * call lockdep_init() from the start_kernel() itself,
2588 * and we want to initialize the hashes only once:
2590 if (lockdep_initialized)
2591 return;
2593 for (i = 0; i < CLASSHASH_SIZE; i++)
2594 INIT_LIST_HEAD(classhash_table + i);
2596 for (i = 0; i < CHAINHASH_SIZE; i++)
2597 INIT_LIST_HEAD(chainhash_table + i);
2599 lockdep_initialized = 1;
2602 void __init lockdep_info(void)
2604 printk("Lock dependency validator: Copyright (c) 2006 Red Hat, Inc., Ingo Molnar\n");
2606 printk("... MAX_LOCKDEP_SUBCLASSES: %lu\n", MAX_LOCKDEP_SUBCLASSES);
2607 printk("... MAX_LOCK_DEPTH: %lu\n", MAX_LOCK_DEPTH);
2608 printk("... MAX_LOCKDEP_KEYS: %lu\n", MAX_LOCKDEP_KEYS);
2609 printk("... CLASSHASH_SIZE: %lu\n", CLASSHASH_SIZE);
2610 printk("... MAX_LOCKDEP_ENTRIES: %lu\n", MAX_LOCKDEP_ENTRIES);
2611 printk("... MAX_LOCKDEP_CHAINS: %lu\n", MAX_LOCKDEP_CHAINS);
2612 printk("... CHAINHASH_SIZE: %lu\n", CHAINHASH_SIZE);
2614 printk(" memory used by lock dependency info: %lu kB\n",
2615 (sizeof(struct lock_class) * MAX_LOCKDEP_KEYS +
2616 sizeof(struct list_head) * CLASSHASH_SIZE +
2617 sizeof(struct lock_list) * MAX_LOCKDEP_ENTRIES +
2618 sizeof(struct lock_chain) * MAX_LOCKDEP_CHAINS +
2619 sizeof(struct list_head) * CHAINHASH_SIZE) / 1024);
2621 printk(" per task-struct memory footprint: %lu bytes\n",
2622 sizeof(struct held_lock) * MAX_LOCK_DEPTH);
2624 #ifdef CONFIG_DEBUG_LOCKDEP
2625 if (lockdep_init_error)
2626 printk("WARNING: lockdep init error! Arch code didnt call lockdep_init() early enough?\n");
2627 #endif
2630 static inline int in_range(const void *start, const void *addr, const void *end)
2632 return addr >= start && addr <= end;
2635 static void
2636 print_freed_lock_bug(struct task_struct *curr, const void *mem_from,
2637 const void *mem_to, struct held_lock *hlock)
2639 if (!debug_locks_off())
2640 return;
2641 if (debug_locks_silent)
2642 return;
2644 printk("\n=========================\n");
2645 printk( "[ BUG: held lock freed! ]\n");
2646 printk( "-------------------------\n");
2647 printk("%s/%d is freeing memory %p-%p, with a lock still held there!\n",
2648 curr->comm, curr->pid, mem_from, mem_to-1);
2649 print_lock(hlock);
2650 lockdep_print_held_locks(curr);
2652 printk("\nstack backtrace:\n");
2653 dump_stack();
2657 * Called when kernel memory is freed (or unmapped), or if a lock
2658 * is destroyed or reinitialized - this code checks whether there is
2659 * any held lock in the memory range of <from> to <to>:
2661 void debug_check_no_locks_freed(const void *mem_from, unsigned long mem_len)
2663 const void *mem_to = mem_from + mem_len, *lock_from, *lock_to;
2664 struct task_struct *curr = current;
2665 struct held_lock *hlock;
2666 unsigned long flags;
2667 int i;
2669 if (unlikely(!debug_locks))
2670 return;
2672 local_irq_save(flags);
2673 for (i = 0; i < curr->lockdep_depth; i++) {
2674 hlock = curr->held_locks + i;
2676 lock_from = (void *)hlock->instance;
2677 lock_to = (void *)(hlock->instance + 1);
2679 if (!in_range(mem_from, lock_from, mem_to) &&
2680 !in_range(mem_from, lock_to, mem_to))
2681 continue;
2683 print_freed_lock_bug(curr, mem_from, mem_to, hlock);
2684 break;
2686 local_irq_restore(flags);
2688 EXPORT_SYMBOL_GPL(debug_check_no_locks_freed);
2690 static void print_held_locks_bug(struct task_struct *curr)
2692 if (!debug_locks_off())
2693 return;
2694 if (debug_locks_silent)
2695 return;
2697 printk("\n=====================================\n");
2698 printk( "[ BUG: lock held at task exit time! ]\n");
2699 printk( "-------------------------------------\n");
2700 printk("%s/%d is exiting with locks still held!\n",
2701 curr->comm, curr->pid);
2702 lockdep_print_held_locks(curr);
2704 printk("\nstack backtrace:\n");
2705 dump_stack();
2708 void debug_check_no_locks_held(struct task_struct *task)
2710 if (unlikely(task->lockdep_depth > 0))
2711 print_held_locks_bug(task);
2714 void debug_show_all_locks(void)
2716 struct task_struct *g, *p;
2717 int count = 10;
2718 int unlock = 1;
2720 printk("\nShowing all locks held in the system:\n");
2723 * Here we try to get the tasklist_lock as hard as possible,
2724 * if not successful after 2 seconds we ignore it (but keep
2725 * trying). This is to enable a debug printout even if a
2726 * tasklist_lock-holding task deadlocks or crashes.
2728 retry:
2729 if (!read_trylock(&tasklist_lock)) {
2730 if (count == 10)
2731 printk("hm, tasklist_lock locked, retrying... ");
2732 if (count) {
2733 count--;
2734 printk(" #%d", 10-count);
2735 mdelay(200);
2736 goto retry;
2738 printk(" ignoring it.\n");
2739 unlock = 0;
2741 if (count != 10)
2742 printk(" locked it.\n");
2744 do_each_thread(g, p) {
2745 if (p->lockdep_depth)
2746 lockdep_print_held_locks(p);
2747 if (!unlock)
2748 if (read_trylock(&tasklist_lock))
2749 unlock = 1;
2750 } while_each_thread(g, p);
2752 printk("\n");
2753 printk("=============================================\n\n");
2755 if (unlock)
2756 read_unlock(&tasklist_lock);
2759 EXPORT_SYMBOL_GPL(debug_show_all_locks);
2761 void debug_show_held_locks(struct task_struct *task)
2763 lockdep_print_held_locks(task);
2766 EXPORT_SYMBOL_GPL(debug_show_held_locks);