]> pilppa.org Git - linux-2.6-omap-h63xx.git/blob - kernel/lockdep.c
62e73ce681972ad350cda5b50037ab30c381c67a
[linux-2.6-omap-h63xx.git] / kernel / lockdep.c
1 /*
2  * kernel/lockdep.c
3  *
4  * Runtime locking correctness validator
5  *
6  * Started by Ingo Molnar:
7  *
8  *  Copyright (C) 2006 Red Hat, Inc., Ingo Molnar <mingo@redhat.com>
9  *
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:
12  *
13  * - lock inversion scenarios
14  * - circular lock dependencies
15  * - hardirq/softirq safe/unsafe locking bugs
16  *
17  * Bugs are reported even if the current locking scenario does not cause
18  * any deadlock at this point.
19  *
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.
23  *
24  * Thanks to Arjan van de Ven for coming up with the initial idea of
25  * mapping lock dependencies runtime.
26  */
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>
40
41 #include <asm/sections.h>
42
43 #include "lockdep_internals.h"
44
45 /*
46  * hash_lock: protects the lockdep hashes and class/list/hash allocators.
47  *
48  * This is one of the rare exceptions where it's justified
49  * to use a raw spinlock - we really dont want the spinlock
50  * code to recurse back into the lockdep code.
51  */
52 static raw_spinlock_t hash_lock = (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED;
53
54 static int lockdep_initialized;
55
56 unsigned long nr_list_entries;
57 static struct lock_list list_entries[MAX_LOCKDEP_ENTRIES];
58
59 /*
60  * Allocate a lockdep entry. (assumes hash_lock held, returns
61  * with NULL on failure)
62  */
63 static struct lock_list *alloc_list_entry(void)
64 {
65         if (nr_list_entries >= MAX_LOCKDEP_ENTRIES) {
66                 __raw_spin_unlock(&hash_lock);
67                 debug_locks_off();
68                 printk("BUG: MAX_LOCKDEP_ENTRIES too low!\n");
69                 printk("turning off the locking correctness validator.\n");
70                 return NULL;
71         }
72         return list_entries + nr_list_entries++;
73 }
74
75 /*
76  * All data structures here are protected by the global debug_lock.
77  *
78  * Mutex key structs only get allocated, once during bootup, and never
79  * get freed - this significantly simplifies the debugging code.
80  */
81 unsigned long nr_lock_classes;
82 static struct lock_class lock_classes[MAX_LOCKDEP_KEYS];
83
84 /*
85  * We keep a global list of all lock classes. The list only grows,
86  * never shrinks. The list is only accessed with the lockdep
87  * spinlock lock held.
88  */
89 LIST_HEAD(all_lock_classes);
90
91 /*
92  * The lockdep classes are in a hash-table as well, for fast lookup:
93  */
94 #define CLASSHASH_BITS          (MAX_LOCKDEP_KEYS_BITS - 1)
95 #define CLASSHASH_SIZE          (1UL << CLASSHASH_BITS)
96 #define CLASSHASH_MASK          (CLASSHASH_SIZE - 1)
97 #define __classhashfn(key)      ((((unsigned long)key >> CLASSHASH_BITS) + (unsigned long)key) & CLASSHASH_MASK)
98 #define classhashentry(key)     (classhash_table + __classhashfn((key)))
99
100 static struct list_head classhash_table[CLASSHASH_SIZE];
101
102 unsigned long nr_lock_chains;
103 static struct lock_chain lock_chains[MAX_LOCKDEP_CHAINS];
104
105 /*
106  * We put the lock dependency chains into a hash-table as well, to cache
107  * their existence:
108  */
109 #define CHAINHASH_BITS          (MAX_LOCKDEP_CHAINS_BITS-1)
110 #define CHAINHASH_SIZE          (1UL << CHAINHASH_BITS)
111 #define CHAINHASH_MASK          (CHAINHASH_SIZE - 1)
112 #define __chainhashfn(chain) \
113                 (((chain >> CHAINHASH_BITS) + chain) & CHAINHASH_MASK)
114 #define chainhashentry(chain)   (chainhash_table + __chainhashfn((chain)))
115
116 static struct list_head chainhash_table[CHAINHASH_SIZE];
117
118 /*
119  * The hash key of the lock dependency chains is a hash itself too:
120  * it's a hash of all locks taken up to that lock, including that lock.
121  * It's a 64-bit hash, because it's important for the keys to be
122  * unique.
123  */
124 #define iterate_chain_key(key1, key2) \
125         (((key1) << MAX_LOCKDEP_KEYS_BITS) ^ \
126         ((key1) >> (64-MAX_LOCKDEP_KEYS_BITS)) ^ \
127         (key2))
128
129 void lockdep_off(void)
130 {
131         current->lockdep_recursion++;
132 }
133
134 EXPORT_SYMBOL(lockdep_off);
135
136 void lockdep_on(void)
137 {
138         current->lockdep_recursion--;
139 }
140
141 EXPORT_SYMBOL(lockdep_on);
142
143 /*
144  * Debugging switches:
145  */
146
147 #define VERBOSE                 0
148 #ifdef VERBOSE
149 # define VERY_VERBOSE           0
150 #endif
151
152 #if VERBOSE
153 # define HARDIRQ_VERBOSE        1
154 # define SOFTIRQ_VERBOSE        1
155 #else
156 # define HARDIRQ_VERBOSE        0
157 # define SOFTIRQ_VERBOSE        0
158 #endif
159
160 #if VERBOSE || HARDIRQ_VERBOSE || SOFTIRQ_VERBOSE
161 /*
162  * Quick filtering for interesting events:
163  */
164 static int class_filter(struct lock_class *class)
165 {
166 #if 0
167         /* Example */
168         if (class->name_version == 1 &&
169                         !strcmp(class->name, "lockname"))
170                 return 1;
171         if (class->name_version == 1 &&
172                         !strcmp(class->name, "&struct->lockfield"))
173                 return 1;
174 #endif
175         /* Allow everything else. 0 would be filter everything else */
176         return 1;
177 }
178 #endif
179
180 static int verbose(struct lock_class *class)
181 {
182 #if VERBOSE
183         return class_filter(class);
184 #endif
185         return 0;
186 }
187
188 #ifdef CONFIG_TRACE_IRQFLAGS
189
190 static int hardirq_verbose(struct lock_class *class)
191 {
192 #if HARDIRQ_VERBOSE
193         return class_filter(class);
194 #endif
195         return 0;
196 }
197
198 static int softirq_verbose(struct lock_class *class)
199 {
200 #if SOFTIRQ_VERBOSE
201         return class_filter(class);
202 #endif
203         return 0;
204 }
205
206 #endif
207
208 /*
209  * Stack-trace: tightly packed array of stack backtrace
210  * addresses. Protected by the hash_lock.
211  */
212 unsigned long nr_stack_trace_entries;
213 static unsigned long stack_trace[MAX_STACK_TRACE_ENTRIES];
214
215 static int save_trace(struct stack_trace *trace)
216 {
217         trace->nr_entries = 0;
218         trace->max_entries = MAX_STACK_TRACE_ENTRIES - nr_stack_trace_entries;
219         trace->entries = stack_trace + nr_stack_trace_entries;
220
221         trace->skip = 3;
222         trace->all_contexts = 0;
223
224         /* Make sure to not recurse in case the the unwinder needs to tak
225 e          locks. */
226         lockdep_off();
227         save_stack_trace(trace, NULL);
228         lockdep_on();
229
230         trace->max_entries = trace->nr_entries;
231
232         nr_stack_trace_entries += trace->nr_entries;
233         if (DEBUG_LOCKS_WARN_ON(nr_stack_trace_entries > MAX_STACK_TRACE_ENTRIES)) {
234                 __raw_spin_unlock(&hash_lock);
235                 return 0;
236         }
237
238         if (nr_stack_trace_entries == MAX_STACK_TRACE_ENTRIES) {
239                 __raw_spin_unlock(&hash_lock);
240                 if (debug_locks_off()) {
241                         printk("BUG: MAX_STACK_TRACE_ENTRIES too low!\n");
242                         printk("turning off the locking correctness validator.\n");
243                         dump_stack();
244                 }
245                 return 0;
246         }
247
248         return 1;
249 }
250
251 unsigned int nr_hardirq_chains;
252 unsigned int nr_softirq_chains;
253 unsigned int nr_process_chains;
254 unsigned int max_lockdep_depth;
255 unsigned int max_recursion_depth;
256
257 #ifdef CONFIG_DEBUG_LOCKDEP
258 /*
259  * We cannot printk in early bootup code. Not even early_printk()
260  * might work. So we mark any initialization errors and printk
261  * about it later on, in lockdep_info().
262  */
263 static int lockdep_init_error;
264
265 /*
266  * Various lockdep statistics:
267  */
268 atomic_t chain_lookup_hits;
269 atomic_t chain_lookup_misses;
270 atomic_t hardirqs_on_events;
271 atomic_t hardirqs_off_events;
272 atomic_t redundant_hardirqs_on;
273 atomic_t redundant_hardirqs_off;
274 atomic_t softirqs_on_events;
275 atomic_t softirqs_off_events;
276 atomic_t redundant_softirqs_on;
277 atomic_t redundant_softirqs_off;
278 atomic_t nr_unused_locks;
279 atomic_t nr_cyclic_checks;
280 atomic_t nr_cyclic_check_recursions;
281 atomic_t nr_find_usage_forwards_checks;
282 atomic_t nr_find_usage_forwards_recursions;
283 atomic_t nr_find_usage_backwards_checks;
284 atomic_t nr_find_usage_backwards_recursions;
285 # define debug_atomic_inc(ptr)          atomic_inc(ptr)
286 # define debug_atomic_dec(ptr)          atomic_dec(ptr)
287 # define debug_atomic_read(ptr)         atomic_read(ptr)
288 #else
289 # define debug_atomic_inc(ptr)          do { } while (0)
290 # define debug_atomic_dec(ptr)          do { } while (0)
291 # define debug_atomic_read(ptr)         0
292 #endif
293
294 /*
295  * Locking printouts:
296  */
297
298 static const char *usage_str[] =
299 {
300         [LOCK_USED] =                   "initial-use ",
301         [LOCK_USED_IN_HARDIRQ] =        "in-hardirq-W",
302         [LOCK_USED_IN_SOFTIRQ] =        "in-softirq-W",
303         [LOCK_ENABLED_SOFTIRQS] =       "softirq-on-W",
304         [LOCK_ENABLED_HARDIRQS] =       "hardirq-on-W",
305         [LOCK_USED_IN_HARDIRQ_READ] =   "in-hardirq-R",
306         [LOCK_USED_IN_SOFTIRQ_READ] =   "in-softirq-R",
307         [LOCK_ENABLED_SOFTIRQS_READ] =  "softirq-on-R",
308         [LOCK_ENABLED_HARDIRQS_READ] =  "hardirq-on-R",
309 };
310
311 const char * __get_key_name(struct lockdep_subclass_key *key, char *str)
312 {
313         unsigned long offs, size;
314         char *modname;
315
316         return kallsyms_lookup((unsigned long)key, &size, &offs, &modname, str);
317 }
318
319 void
320 get_usage_chars(struct lock_class *class, char *c1, char *c2, char *c3, char *c4)
321 {
322         *c1 = '.', *c2 = '.', *c3 = '.', *c4 = '.';
323
324         if (class->usage_mask & LOCKF_USED_IN_HARDIRQ)
325                 *c1 = '+';
326         else
327                 if (class->usage_mask & LOCKF_ENABLED_HARDIRQS)
328                         *c1 = '-';
329
330         if (class->usage_mask & LOCKF_USED_IN_SOFTIRQ)
331                 *c2 = '+';
332         else
333                 if (class->usage_mask & LOCKF_ENABLED_SOFTIRQS)
334                         *c2 = '-';
335
336         if (class->usage_mask & LOCKF_ENABLED_HARDIRQS_READ)
337                 *c3 = '-';
338         if (class->usage_mask & LOCKF_USED_IN_HARDIRQ_READ) {
339                 *c3 = '+';
340                 if (class->usage_mask & LOCKF_ENABLED_HARDIRQS_READ)
341                         *c3 = '?';
342         }
343
344         if (class->usage_mask & LOCKF_ENABLED_SOFTIRQS_READ)
345                 *c4 = '-';
346         if (class->usage_mask & LOCKF_USED_IN_SOFTIRQ_READ) {
347                 *c4 = '+';
348                 if (class->usage_mask & LOCKF_ENABLED_SOFTIRQS_READ)
349                         *c4 = '?';
350         }
351 }
352
353 static void print_lock_name(struct lock_class *class)
354 {
355         char str[KSYM_NAME_LEN + 1], c1, c2, c3, c4;
356         const char *name;
357
358         get_usage_chars(class, &c1, &c2, &c3, &c4);
359
360         name = class->name;
361         if (!name) {
362                 name = __get_key_name(class->key, str);
363                 printk(" (%s", name);
364         } else {
365                 printk(" (%s", name);
366                 if (class->name_version > 1)
367                         printk("#%d", class->name_version);
368                 if (class->subclass)
369                         printk("/%d", class->subclass);
370         }
371         printk("){%c%c%c%c}", c1, c2, c3, c4);
372 }
373
374 static void print_lockdep_cache(struct lockdep_map *lock)
375 {
376         const char *name;
377         char str[KSYM_NAME_LEN + 1];
378
379         name = lock->name;
380         if (!name)
381                 name = __get_key_name(lock->key->subkeys, str);
382
383         printk("%s", name);
384 }
385
386 static void print_lock(struct held_lock *hlock)
387 {
388         print_lock_name(hlock->class);
389         printk(", at: ");
390         print_ip_sym(hlock->acquire_ip);
391 }
392
393 static void lockdep_print_held_locks(struct task_struct *curr)
394 {
395         int i, depth = curr->lockdep_depth;
396
397         if (!depth) {
398                 printk("no locks held by %s/%d.\n", curr->comm, curr->pid);
399                 return;
400         }
401         printk("%d lock%s held by %s/%d:\n",
402                 depth, depth > 1 ? "s" : "", curr->comm, curr->pid);
403
404         for (i = 0; i < depth; i++) {
405                 printk(" #%d: ", i);
406                 print_lock(curr->held_locks + i);
407         }
408 }
409
410 static void print_lock_class_header(struct lock_class *class, int depth)
411 {
412         int bit;
413
414         printk("%*s->", depth, "");
415         print_lock_name(class);
416         printk(" ops: %lu", class->ops);
417         printk(" {\n");
418
419         for (bit = 0; bit < LOCK_USAGE_STATES; bit++) {
420                 if (class->usage_mask & (1 << bit)) {
421                         int len = depth;
422
423                         len += printk("%*s   %s", depth, "", usage_str[bit]);
424                         len += printk(" at:\n");
425                         print_stack_trace(class->usage_traces + bit, len);
426                 }
427         }
428         printk("%*s }\n", depth, "");
429
430         printk("%*s ... key      at: ",depth,"");
431         print_ip_sym((unsigned long)class->key);
432 }
433
434 /*
435  * printk all lock dependencies starting at <entry>:
436  */
437 static void print_lock_dependencies(struct lock_class *class, int depth)
438 {
439         struct lock_list *entry;
440
441         if (DEBUG_LOCKS_WARN_ON(depth >= 20))
442                 return;
443
444         print_lock_class_header(class, depth);
445
446         list_for_each_entry(entry, &class->locks_after, entry) {
447                 if (DEBUG_LOCKS_WARN_ON(!entry->class))
448                         return;
449
450                 print_lock_dependencies(entry->class, depth + 1);
451
452                 printk("%*s ... acquired at:\n",depth,"");
453                 print_stack_trace(&entry->trace, 2);
454                 printk("\n");
455         }
456 }
457
458 /*
459  * Add a new dependency to the head of the list:
460  */
461 static int add_lock_to_list(struct lock_class *class, struct lock_class *this,
462                             struct list_head *head, unsigned long ip)
463 {
464         struct lock_list *entry;
465         /*
466          * Lock not present yet - get a new dependency struct and
467          * add it to the list:
468          */
469         entry = alloc_list_entry();
470         if (!entry)
471                 return 0;
472
473         entry->class = this;
474         if (!save_trace(&entry->trace))
475                 return 0;
476
477         /*
478          * Since we never remove from the dependency list, the list can
479          * be walked lockless by other CPUs, it's only allocation
480          * that must be protected by the spinlock. But this also means
481          * we must make new entries visible only once writes to the
482          * entry become visible - hence the RCU op:
483          */
484         list_add_tail_rcu(&entry->entry, head);
485
486         return 1;
487 }
488
489 /*
490  * Recursive, forwards-direction lock-dependency checking, used for
491  * both noncyclic checking and for hardirq-unsafe/softirq-unsafe
492  * checking.
493  *
494  * (to keep the stackframe of the recursive functions small we
495  *  use these global variables, and we also mark various helper
496  *  functions as noinline.)
497  */
498 static struct held_lock *check_source, *check_target;
499
500 /*
501  * Print a dependency chain entry (this is only done when a deadlock
502  * has been detected):
503  */
504 static noinline int
505 print_circular_bug_entry(struct lock_list *target, unsigned int depth)
506 {
507         if (debug_locks_silent)
508                 return 0;
509         printk("\n-> #%u", depth);
510         print_lock_name(target->class);
511         printk(":\n");
512         print_stack_trace(&target->trace, 6);
513
514         return 0;
515 }
516
517 static void print_kernel_version(void)
518 {
519         printk("%s %.*s\n", init_utsname()->release,
520                 (int)strcspn(init_utsname()->version, " "),
521                 init_utsname()->version);
522 }
523
524 /*
525  * When a circular dependency is detected, print the
526  * header first:
527  */
528 static noinline int
529 print_circular_bug_header(struct lock_list *entry, unsigned int depth)
530 {
531         struct task_struct *curr = current;
532
533         __raw_spin_unlock(&hash_lock);
534         debug_locks_off();
535         if (debug_locks_silent)
536                 return 0;
537
538         printk("\n=======================================================\n");
539         printk(  "[ INFO: possible circular locking dependency detected ]\n");
540         print_kernel_version();
541         printk(  "-------------------------------------------------------\n");
542         printk("%s/%d is trying to acquire lock:\n",
543                 curr->comm, curr->pid);
544         print_lock(check_source);
545         printk("\nbut task is already holding lock:\n");
546         print_lock(check_target);
547         printk("\nwhich lock already depends on the new lock.\n\n");
548         printk("\nthe existing dependency chain (in reverse order) is:\n");
549
550         print_circular_bug_entry(entry, depth);
551
552         return 0;
553 }
554
555 static noinline int print_circular_bug_tail(void)
556 {
557         struct task_struct *curr = current;
558         struct lock_list this;
559
560         if (debug_locks_silent)
561                 return 0;
562
563         /* hash_lock unlocked by the header */
564         __raw_spin_lock(&hash_lock);
565         this.class = check_source->class;
566         if (!save_trace(&this.trace))
567                 return 0;
568         __raw_spin_unlock(&hash_lock);
569         print_circular_bug_entry(&this, 0);
570
571         printk("\nother info that might help us debug this:\n\n");
572         lockdep_print_held_locks(curr);
573
574         printk("\nstack backtrace:\n");
575         dump_stack();
576
577         return 0;
578 }
579
580 #define RECURSION_LIMIT 40
581
582 static int noinline print_infinite_recursion_bug(void)
583 {
584         __raw_spin_unlock(&hash_lock);
585         DEBUG_LOCKS_WARN_ON(1);
586
587         return 0;
588 }
589
590 /*
591  * Prove that the dependency graph starting at <entry> can not
592  * lead to <target>. Print an error and return 0 if it does.
593  */
594 static noinline int
595 check_noncircular(struct lock_class *source, unsigned int depth)
596 {
597         struct lock_list *entry;
598
599         debug_atomic_inc(&nr_cyclic_check_recursions);
600         if (depth > max_recursion_depth)
601                 max_recursion_depth = depth;
602         if (depth >= RECURSION_LIMIT)
603                 return print_infinite_recursion_bug();
604         /*
605          * Check this lock's dependency list:
606          */
607         list_for_each_entry(entry, &source->locks_after, entry) {
608                 if (entry->class == check_target->class)
609                         return print_circular_bug_header(entry, depth+1);
610                 debug_atomic_inc(&nr_cyclic_checks);
611                 if (!check_noncircular(entry->class, depth+1))
612                         return print_circular_bug_entry(entry, depth+1);
613         }
614         return 1;
615 }
616
617 static int very_verbose(struct lock_class *class)
618 {
619 #if VERY_VERBOSE
620         return class_filter(class);
621 #endif
622         return 0;
623 }
624 #ifdef CONFIG_TRACE_IRQFLAGS
625
626 /*
627  * Forwards and backwards subgraph searching, for the purposes of
628  * proving that two subgraphs can be connected by a new dependency
629  * without creating any illegal irq-safe -> irq-unsafe lock dependency.
630  */
631 static enum lock_usage_bit find_usage_bit;
632 static struct lock_class *forwards_match, *backwards_match;
633
634 /*
635  * Find a node in the forwards-direction dependency sub-graph starting
636  * at <source> that matches <find_usage_bit>.
637  *
638  * Return 2 if such a node exists in the subgraph, and put that node
639  * into <forwards_match>.
640  *
641  * Return 1 otherwise and keep <forwards_match> unchanged.
642  * Return 0 on error.
643  */
644 static noinline int
645 find_usage_forwards(struct lock_class *source, unsigned int depth)
646 {
647         struct lock_list *entry;
648         int ret;
649
650         if (depth > max_recursion_depth)
651                 max_recursion_depth = depth;
652         if (depth >= RECURSION_LIMIT)
653                 return print_infinite_recursion_bug();
654
655         debug_atomic_inc(&nr_find_usage_forwards_checks);
656         if (source->usage_mask & (1 << find_usage_bit)) {
657                 forwards_match = source;
658                 return 2;
659         }
660
661         /*
662          * Check this lock's dependency list:
663          */
664         list_for_each_entry(entry, &source->locks_after, entry) {
665                 debug_atomic_inc(&nr_find_usage_forwards_recursions);
666                 ret = find_usage_forwards(entry->class, depth+1);
667                 if (ret == 2 || ret == 0)
668                         return ret;
669         }
670         return 1;
671 }
672
673 /*
674  * Find a node in the backwards-direction dependency sub-graph starting
675  * at <source> that matches <find_usage_bit>.
676  *
677  * Return 2 if such a node exists in the subgraph, and put that node
678  * into <backwards_match>.
679  *
680  * Return 1 otherwise and keep <backwards_match> unchanged.
681  * Return 0 on error.
682  */
683 static noinline int
684 find_usage_backwards(struct lock_class *source, unsigned int depth)
685 {
686         struct lock_list *entry;
687         int ret;
688
689         if (depth > max_recursion_depth)
690                 max_recursion_depth = depth;
691         if (depth >= RECURSION_LIMIT)
692                 return print_infinite_recursion_bug();
693
694         debug_atomic_inc(&nr_find_usage_backwards_checks);
695         if (source->usage_mask & (1 << find_usage_bit)) {
696                 backwards_match = source;
697                 return 2;
698         }
699
700         /*
701          * Check this lock's dependency list:
702          */
703         list_for_each_entry(entry, &source->locks_before, entry) {
704                 debug_atomic_inc(&nr_find_usage_backwards_recursions);
705                 ret = find_usage_backwards(entry->class, depth+1);
706                 if (ret == 2 || ret == 0)
707                         return ret;
708         }
709         return 1;
710 }
711
712 static int
713 print_bad_irq_dependency(struct task_struct *curr,
714                          struct held_lock *prev,
715                          struct held_lock *next,
716                          enum lock_usage_bit bit1,
717                          enum lock_usage_bit bit2,
718                          const char *irqclass)
719 {
720         __raw_spin_unlock(&hash_lock);
721         debug_locks_off();
722         if (debug_locks_silent)
723                 return 0;
724
725         printk("\n======================================================\n");
726         printk(  "[ INFO: %s-safe -> %s-unsafe lock order detected ]\n",
727                 irqclass, irqclass);
728         print_kernel_version();
729         printk(  "------------------------------------------------------\n");
730         printk("%s/%d [HC%u[%lu]:SC%u[%lu]:HE%u:SE%u] is trying to acquire:\n",
731                 curr->comm, curr->pid,
732                 curr->hardirq_context, hardirq_count() >> HARDIRQ_SHIFT,
733                 curr->softirq_context, softirq_count() >> SOFTIRQ_SHIFT,
734                 curr->hardirqs_enabled,
735                 curr->softirqs_enabled);
736         print_lock(next);
737
738         printk("\nand this task is already holding:\n");
739         print_lock(prev);
740         printk("which would create a new lock dependency:\n");
741         print_lock_name(prev->class);
742         printk(" ->");
743         print_lock_name(next->class);
744         printk("\n");
745
746         printk("\nbut this new dependency connects a %s-irq-safe lock:\n",
747                 irqclass);
748         print_lock_name(backwards_match);
749         printk("\n... which became %s-irq-safe at:\n", irqclass);
750
751         print_stack_trace(backwards_match->usage_traces + bit1, 1);
752
753         printk("\nto a %s-irq-unsafe lock:\n", irqclass);
754         print_lock_name(forwards_match);
755         printk("\n... which became %s-irq-unsafe at:\n", irqclass);
756         printk("...");
757
758         print_stack_trace(forwards_match->usage_traces + bit2, 1);
759
760         printk("\nother info that might help us debug this:\n\n");
761         lockdep_print_held_locks(curr);
762
763         printk("\nthe %s-irq-safe lock's dependencies:\n", irqclass);
764         print_lock_dependencies(backwards_match, 0);
765
766         printk("\nthe %s-irq-unsafe lock's dependencies:\n", irqclass);
767         print_lock_dependencies(forwards_match, 0);
768
769         printk("\nstack backtrace:\n");
770         dump_stack();
771
772         return 0;
773 }
774
775 static int
776 check_usage(struct task_struct *curr, struct held_lock *prev,
777             struct held_lock *next, enum lock_usage_bit bit_backwards,
778             enum lock_usage_bit bit_forwards, const char *irqclass)
779 {
780         int ret;
781
782         find_usage_bit = bit_backwards;
783         /* fills in <backwards_match> */
784         ret = find_usage_backwards(prev->class, 0);
785         if (!ret || ret == 1)
786                 return ret;
787
788         find_usage_bit = bit_forwards;
789         ret = find_usage_forwards(next->class, 0);
790         if (!ret || ret == 1)
791                 return ret;
792         /* ret == 2 */
793         return print_bad_irq_dependency(curr, prev, next,
794                         bit_backwards, bit_forwards, irqclass);
795 }
796
797 #endif
798
799 static int
800 print_deadlock_bug(struct task_struct *curr, struct held_lock *prev,
801                    struct held_lock *next)
802 {
803         debug_locks_off();
804         __raw_spin_unlock(&hash_lock);
805         if (debug_locks_silent)
806                 return 0;
807
808         printk("\n=============================================\n");
809         printk(  "[ INFO: possible recursive locking detected ]\n");
810         print_kernel_version();
811         printk(  "---------------------------------------------\n");
812         printk("%s/%d is trying to acquire lock:\n",
813                 curr->comm, curr->pid);
814         print_lock(next);
815         printk("\nbut task is already holding lock:\n");
816         print_lock(prev);
817
818         printk("\nother info that might help us debug this:\n");
819         lockdep_print_held_locks(curr);
820
821         printk("\nstack backtrace:\n");
822         dump_stack();
823
824         return 0;
825 }
826
827 /*
828  * Check whether we are holding such a class already.
829  *
830  * (Note that this has to be done separately, because the graph cannot
831  * detect such classes of deadlocks.)
832  *
833  * Returns: 0 on deadlock detected, 1 on OK, 2 on recursive read
834  */
835 static int
836 check_deadlock(struct task_struct *curr, struct held_lock *next,
837                struct lockdep_map *next_instance, int read)
838 {
839         struct held_lock *prev;
840         int i;
841
842         for (i = 0; i < curr->lockdep_depth; i++) {
843                 prev = curr->held_locks + i;
844                 if (prev->class != next->class)
845                         continue;
846                 /*
847                  * Allow read-after-read recursion of the same
848                  * lock class (i.e. read_lock(lock)+read_lock(lock)):
849                  */
850                 if ((read == 2) && prev->read)
851                         return 2;
852                 return print_deadlock_bug(curr, prev, next);
853         }
854         return 1;
855 }
856
857 /*
858  * There was a chain-cache miss, and we are about to add a new dependency
859  * to a previous lock. We recursively validate the following rules:
860  *
861  *  - would the adding of the <prev> -> <next> dependency create a
862  *    circular dependency in the graph? [== circular deadlock]
863  *
864  *  - does the new prev->next dependency connect any hardirq-safe lock
865  *    (in the full backwards-subgraph starting at <prev>) with any
866  *    hardirq-unsafe lock (in the full forwards-subgraph starting at
867  *    <next>)? [== illegal lock inversion with hardirq contexts]
868  *
869  *  - does the new prev->next dependency connect any softirq-safe lock
870  *    (in the full backwards-subgraph starting at <prev>) with any
871  *    softirq-unsafe lock (in the full forwards-subgraph starting at
872  *    <next>)? [== illegal lock inversion with softirq contexts]
873  *
874  * any of these scenarios could lead to a deadlock.
875  *
876  * Then if all the validations pass, we add the forwards and backwards
877  * dependency.
878  */
879 static int
880 check_prev_add(struct task_struct *curr, struct held_lock *prev,
881                struct held_lock *next)
882 {
883         struct lock_list *entry;
884         int ret;
885
886         /*
887          * Prove that the new <prev> -> <next> dependency would not
888          * create a circular dependency in the graph. (We do this by
889          * forward-recursing into the graph starting at <next>, and
890          * checking whether we can reach <prev>.)
891          *
892          * We are using global variables to control the recursion, to
893          * keep the stackframe size of the recursive functions low:
894          */
895         check_source = next;
896         check_target = prev;
897         if (!(check_noncircular(next->class, 0)))
898                 return print_circular_bug_tail();
899
900 #ifdef CONFIG_TRACE_IRQFLAGS
901         /*
902          * Prove that the new dependency does not connect a hardirq-safe
903          * lock with a hardirq-unsafe lock - to achieve this we search
904          * the backwards-subgraph starting at <prev>, and the
905          * forwards-subgraph starting at <next>:
906          */
907         if (!check_usage(curr, prev, next, LOCK_USED_IN_HARDIRQ,
908                                         LOCK_ENABLED_HARDIRQS, "hard"))
909                 return 0;
910
911         /*
912          * Prove that the new dependency does not connect a hardirq-safe-read
913          * lock with a hardirq-unsafe lock - to achieve this we search
914          * the backwards-subgraph starting at <prev>, and the
915          * forwards-subgraph starting at <next>:
916          */
917         if (!check_usage(curr, prev, next, LOCK_USED_IN_HARDIRQ_READ,
918                                         LOCK_ENABLED_HARDIRQS, "hard-read"))
919                 return 0;
920
921         /*
922          * Prove that the new dependency does not connect a softirq-safe
923          * lock with a softirq-unsafe lock - to achieve this we search
924          * the backwards-subgraph starting at <prev>, and the
925          * forwards-subgraph starting at <next>:
926          */
927         if (!check_usage(curr, prev, next, LOCK_USED_IN_SOFTIRQ,
928                                         LOCK_ENABLED_SOFTIRQS, "soft"))
929                 return 0;
930         /*
931          * Prove that the new dependency does not connect a softirq-safe-read
932          * lock with a softirq-unsafe lock - to achieve this we search
933          * the backwards-subgraph starting at <prev>, and the
934          * forwards-subgraph starting at <next>:
935          */
936         if (!check_usage(curr, prev, next, LOCK_USED_IN_SOFTIRQ_READ,
937                                         LOCK_ENABLED_SOFTIRQS, "soft"))
938                 return 0;
939 #endif
940         /*
941          * For recursive read-locks we do all the dependency checks,
942          * but we dont store read-triggered dependencies (only
943          * write-triggered dependencies). This ensures that only the
944          * write-side dependencies matter, and that if for example a
945          * write-lock never takes any other locks, then the reads are
946          * equivalent to a NOP.
947          */
948         if (next->read == 2 || prev->read == 2)
949                 return 1;
950         /*
951          * Is the <prev> -> <next> dependency already present?
952          *
953          * (this may occur even though this is a new chain: consider
954          *  e.g. the L1 -> L2 -> L3 -> L4 and the L5 -> L1 -> L2 -> L3
955          *  chains - the second one will be new, but L1 already has
956          *  L2 added to its dependency list, due to the first chain.)
957          */
958         list_for_each_entry(entry, &prev->class->locks_after, entry) {
959                 if (entry->class == next->class)
960                         return 2;
961         }
962
963         /*
964          * Ok, all validations passed, add the new lock
965          * to the previous lock's dependency list:
966          */
967         ret = add_lock_to_list(prev->class, next->class,
968                                &prev->class->locks_after, next->acquire_ip);
969         if (!ret)
970                 return 0;
971
972         ret = add_lock_to_list(next->class, prev->class,
973                                &next->class->locks_before, next->acquire_ip);
974         if (!ret)
975                 return 0;
976
977         /*
978          * Debugging printouts:
979          */
980         if (verbose(prev->class) || verbose(next->class)) {
981                 __raw_spin_unlock(&hash_lock);
982                 printk("\n new dependency: ");
983                 print_lock_name(prev->class);
984                 printk(" => ");
985                 print_lock_name(next->class);
986                 printk("\n");
987                 dump_stack();
988                 __raw_spin_lock(&hash_lock);
989         }
990         return 1;
991 }
992
993 /*
994  * Add the dependency to all directly-previous locks that are 'relevant'.
995  * The ones that are relevant are (in increasing distance from curr):
996  * all consecutive trylock entries and the final non-trylock entry - or
997  * the end of this context's lock-chain - whichever comes first.
998  */
999 static int
1000 check_prevs_add(struct task_struct *curr, struct held_lock *next)
1001 {
1002         int depth = curr->lockdep_depth;
1003         struct held_lock *hlock;
1004
1005         /*
1006          * Debugging checks.
1007          *
1008          * Depth must not be zero for a non-head lock:
1009          */
1010         if (!depth)
1011                 goto out_bug;
1012         /*
1013          * At least two relevant locks must exist for this
1014          * to be a head:
1015          */
1016         if (curr->held_locks[depth].irq_context !=
1017                         curr->held_locks[depth-1].irq_context)
1018                 goto out_bug;
1019
1020         for (;;) {
1021                 hlock = curr->held_locks + depth-1;
1022                 /*
1023                  * Only non-recursive-read entries get new dependencies
1024                  * added:
1025                  */
1026                 if (hlock->read != 2) {
1027                         if (!check_prev_add(curr, hlock, next))
1028                                 return 0;
1029                         /*
1030                          * Stop after the first non-trylock entry,
1031                          * as non-trylock entries have added their
1032                          * own direct dependencies already, so this
1033                          * lock is connected to them indirectly:
1034                          */
1035                         if (!hlock->trylock)
1036                                 break;
1037                 }
1038                 depth--;
1039                 /*
1040                  * End of lock-stack?
1041                  */
1042                 if (!depth)
1043                         break;
1044                 /*
1045                  * Stop the search if we cross into another context:
1046                  */
1047                 if (curr->held_locks[depth].irq_context !=
1048                                 curr->held_locks[depth-1].irq_context)
1049                         break;
1050         }
1051         return 1;
1052 out_bug:
1053         __raw_spin_unlock(&hash_lock);
1054         DEBUG_LOCKS_WARN_ON(1);
1055
1056         return 0;
1057 }
1058
1059
1060 /*
1061  * Is this the address of a static object:
1062  */
1063 static int static_obj(void *obj)
1064 {
1065         unsigned long start = (unsigned long) &_stext,
1066                       end   = (unsigned long) &_end,
1067                       addr  = (unsigned long) obj;
1068 #ifdef CONFIG_SMP
1069         int i;
1070 #endif
1071
1072         /*
1073          * static variable?
1074          */
1075         if ((addr >= start) && (addr < end))
1076                 return 1;
1077
1078 #ifdef CONFIG_SMP
1079         /*
1080          * percpu var?
1081          */
1082         for_each_possible_cpu(i) {
1083                 start = (unsigned long) &__per_cpu_start + per_cpu_offset(i);
1084                 end   = (unsigned long) &__per_cpu_start + PERCPU_ENOUGH_ROOM
1085                                         + per_cpu_offset(i);
1086
1087                 if ((addr >= start) && (addr < end))
1088                         return 1;
1089         }
1090 #endif
1091
1092         /*
1093          * module var?
1094          */
1095         return is_module_address(addr);
1096 }
1097
1098 /*
1099  * To make lock name printouts unique, we calculate a unique
1100  * class->name_version generation counter:
1101  */
1102 static int count_matching_names(struct lock_class *new_class)
1103 {
1104         struct lock_class *class;
1105         int count = 0;
1106
1107         if (!new_class->name)
1108                 return 0;
1109
1110         list_for_each_entry(class, &all_lock_classes, lock_entry) {
1111                 if (new_class->key - new_class->subclass == class->key)
1112                         return class->name_version;
1113                 if (class->name && !strcmp(class->name, new_class->name))
1114                         count = max(count, class->name_version);
1115         }
1116
1117         return count + 1;
1118 }
1119
1120 /*
1121  * Register a lock's class in the hash-table, if the class is not present
1122  * yet. Otherwise we look it up. We cache the result in the lock object
1123  * itself, so actual lookup of the hash should be once per lock object.
1124  */
1125 static inline struct lock_class *
1126 look_up_lock_class(struct lockdep_map *lock, unsigned int subclass)
1127 {
1128         struct lockdep_subclass_key *key;
1129         struct list_head *hash_head;
1130         struct lock_class *class;
1131
1132 #ifdef CONFIG_DEBUG_LOCKDEP
1133         /*
1134          * If the architecture calls into lockdep before initializing
1135          * the hashes then we'll warn about it later. (we cannot printk
1136          * right now)
1137          */
1138         if (unlikely(!lockdep_initialized)) {
1139                 lockdep_init();
1140                 lockdep_init_error = 1;
1141         }
1142 #endif
1143
1144         /*
1145          * Static locks do not have their class-keys yet - for them the key
1146          * is the lock object itself:
1147          */
1148         if (unlikely(!lock->key))
1149                 lock->key = (void *)lock;
1150
1151         /*
1152          * NOTE: the class-key must be unique. For dynamic locks, a static
1153          * lock_class_key variable is passed in through the mutex_init()
1154          * (or spin_lock_init()) call - which acts as the key. For static
1155          * locks we use the lock object itself as the key.
1156          */
1157         BUILD_BUG_ON(sizeof(struct lock_class_key) > sizeof(struct lock_class));
1158
1159         key = lock->key->subkeys + subclass;
1160
1161         hash_head = classhashentry(key);
1162
1163         /*
1164          * We can walk the hash lockfree, because the hash only
1165          * grows, and we are careful when adding entries to the end:
1166          */
1167         list_for_each_entry(class, hash_head, hash_entry)
1168                 if (class->key == key)
1169                         return class;
1170
1171         return NULL;
1172 }
1173
1174 /*
1175  * Register a lock's class in the hash-table, if the class is not present
1176  * yet. Otherwise we look it up. We cache the result in the lock object
1177  * itself, so actual lookup of the hash should be once per lock object.
1178  */
1179 static inline struct lock_class *
1180 register_lock_class(struct lockdep_map *lock, unsigned int subclass, int force)
1181 {
1182         struct lockdep_subclass_key *key;
1183         struct list_head *hash_head;
1184         struct lock_class *class;
1185         unsigned long flags;
1186
1187         class = look_up_lock_class(lock, subclass);
1188         if (likely(class))
1189                 return class;
1190
1191         /*
1192          * Debug-check: all keys must be persistent!
1193          */
1194         if (!static_obj(lock->key)) {
1195                 debug_locks_off();
1196                 printk("INFO: trying to register non-static key.\n");
1197                 printk("the code is fine but needs lockdep annotation.\n");
1198                 printk("turning off the locking correctness validator.\n");
1199                 dump_stack();
1200
1201                 return NULL;
1202         }
1203
1204         key = lock->key->subkeys + subclass;
1205         hash_head = classhashentry(key);
1206
1207         raw_local_irq_save(flags);
1208         __raw_spin_lock(&hash_lock);
1209         /*
1210          * We have to do the hash-walk again, to avoid races
1211          * with another CPU:
1212          */
1213         list_for_each_entry(class, hash_head, hash_entry)
1214                 if (class->key == key)
1215                         goto out_unlock_set;
1216         /*
1217          * Allocate a new key from the static array, and add it to
1218          * the hash:
1219          */
1220         if (nr_lock_classes >= MAX_LOCKDEP_KEYS) {
1221                 __raw_spin_unlock(&hash_lock);
1222                 raw_local_irq_restore(flags);
1223                 debug_locks_off();
1224                 printk("BUG: MAX_LOCKDEP_KEYS too low!\n");
1225                 printk("turning off the locking correctness validator.\n");
1226                 return NULL;
1227         }
1228         class = lock_classes + nr_lock_classes++;
1229         debug_atomic_inc(&nr_unused_locks);
1230         class->key = key;
1231         class->name = lock->name;
1232         class->subclass = subclass;
1233         INIT_LIST_HEAD(&class->lock_entry);
1234         INIT_LIST_HEAD(&class->locks_before);
1235         INIT_LIST_HEAD(&class->locks_after);
1236         class->name_version = count_matching_names(class);
1237         /*
1238          * We use RCU's safe list-add method to make
1239          * parallel walking of the hash-list safe:
1240          */
1241         list_add_tail_rcu(&class->hash_entry, hash_head);
1242
1243         if (verbose(class)) {
1244                 __raw_spin_unlock(&hash_lock);
1245                 raw_local_irq_restore(flags);
1246                 printk("\nnew class %p: %s", class->key, class->name);
1247                 if (class->name_version > 1)
1248                         printk("#%d", class->name_version);
1249                 printk("\n");
1250                 dump_stack();
1251                 raw_local_irq_save(flags);
1252                 __raw_spin_lock(&hash_lock);
1253         }
1254 out_unlock_set:
1255         __raw_spin_unlock(&hash_lock);
1256         raw_local_irq_restore(flags);
1257
1258         if (!subclass || force)
1259                 lock->class_cache = class;
1260
1261         DEBUG_LOCKS_WARN_ON(class->subclass != subclass);
1262
1263         return class;
1264 }
1265
1266 /*
1267  * Look up a dependency chain. If the key is not present yet then
1268  * add it and return 0 - in this case the new dependency chain is
1269  * validated. If the key is already hashed, return 1.
1270  */
1271 static inline int lookup_chain_cache(u64 chain_key)
1272 {
1273         struct list_head *hash_head = chainhashentry(chain_key);
1274         struct lock_chain *chain;
1275
1276         DEBUG_LOCKS_WARN_ON(!irqs_disabled());
1277         /*
1278          * We can walk it lock-free, because entries only get added
1279          * to the hash:
1280          */
1281         list_for_each_entry(chain, hash_head, entry) {
1282                 if (chain->chain_key == chain_key) {
1283 cache_hit:
1284                         debug_atomic_inc(&chain_lookup_hits);
1285                         /*
1286                          * In the debugging case, force redundant checking
1287                          * by returning 1:
1288                          */
1289 #ifdef CONFIG_DEBUG_LOCKDEP
1290                         __raw_spin_lock(&hash_lock);
1291                         return 1;
1292 #endif
1293                         return 0;
1294                 }
1295         }
1296         /*
1297          * Allocate a new chain entry from the static array, and add
1298          * it to the hash:
1299          */
1300         __raw_spin_lock(&hash_lock);
1301         /*
1302          * We have to walk the chain again locked - to avoid duplicates:
1303          */
1304         list_for_each_entry(chain, hash_head, entry) {
1305                 if (chain->chain_key == chain_key) {
1306                         __raw_spin_unlock(&hash_lock);
1307                         goto cache_hit;
1308                 }
1309         }
1310         if (unlikely(nr_lock_chains >= MAX_LOCKDEP_CHAINS)) {
1311                 __raw_spin_unlock(&hash_lock);
1312                 debug_locks_off();
1313                 printk("BUG: MAX_LOCKDEP_CHAINS too low!\n");
1314                 printk("turning off the locking correctness validator.\n");
1315                 return 0;
1316         }
1317         chain = lock_chains + nr_lock_chains++;
1318         chain->chain_key = chain_key;
1319         list_add_tail_rcu(&chain->entry, hash_head);
1320         debug_atomic_inc(&chain_lookup_misses);
1321 #ifdef CONFIG_TRACE_IRQFLAGS
1322         if (current->hardirq_context)
1323                 nr_hardirq_chains++;
1324         else {
1325                 if (current->softirq_context)
1326                         nr_softirq_chains++;
1327                 else
1328                         nr_process_chains++;
1329         }
1330 #else
1331         nr_process_chains++;
1332 #endif
1333
1334         return 1;
1335 }
1336
1337 /*
1338  * We are building curr_chain_key incrementally, so double-check
1339  * it from scratch, to make sure that it's done correctly:
1340  */
1341 static void check_chain_key(struct task_struct *curr)
1342 {
1343 #ifdef CONFIG_DEBUG_LOCKDEP
1344         struct held_lock *hlock, *prev_hlock = NULL;
1345         unsigned int i, id;
1346         u64 chain_key = 0;
1347
1348         for (i = 0; i < curr->lockdep_depth; i++) {
1349                 hlock = curr->held_locks + i;
1350                 if (chain_key != hlock->prev_chain_key) {
1351                         debug_locks_off();
1352                         printk("hm#1, depth: %u [%u], %016Lx != %016Lx\n",
1353                                 curr->lockdep_depth, i,
1354                                 (unsigned long long)chain_key,
1355                                 (unsigned long long)hlock->prev_chain_key);
1356                         WARN_ON(1);
1357                         return;
1358                 }
1359                 id = hlock->class - lock_classes;
1360                 DEBUG_LOCKS_WARN_ON(id >= MAX_LOCKDEP_KEYS);
1361                 if (prev_hlock && (prev_hlock->irq_context !=
1362                                                         hlock->irq_context))
1363                         chain_key = 0;
1364                 chain_key = iterate_chain_key(chain_key, id);
1365                 prev_hlock = hlock;
1366         }
1367         if (chain_key != curr->curr_chain_key) {
1368                 debug_locks_off();
1369                 printk("hm#2, depth: %u [%u], %016Lx != %016Lx\n",
1370                         curr->lockdep_depth, i,
1371                         (unsigned long long)chain_key,
1372                         (unsigned long long)curr->curr_chain_key);
1373                 WARN_ON(1);
1374         }
1375 #endif
1376 }
1377
1378 #ifdef CONFIG_TRACE_IRQFLAGS
1379
1380 /*
1381  * print irq inversion bug:
1382  */
1383 static int
1384 print_irq_inversion_bug(struct task_struct *curr, struct lock_class *other,
1385                         struct held_lock *this, int forwards,
1386                         const char *irqclass)
1387 {
1388         __raw_spin_unlock(&hash_lock);
1389         debug_locks_off();
1390         if (debug_locks_silent)
1391                 return 0;
1392
1393         printk("\n=========================================================\n");
1394         printk(  "[ INFO: possible irq lock inversion dependency detected ]\n");
1395         print_kernel_version();
1396         printk(  "---------------------------------------------------------\n");
1397         printk("%s/%d just changed the state of lock:\n",
1398                 curr->comm, curr->pid);
1399         print_lock(this);
1400         if (forwards)
1401                 printk("but this lock took another, %s-irq-unsafe lock in the past:\n", irqclass);
1402         else
1403                 printk("but this lock was taken by another, %s-irq-safe lock in the past:\n", irqclass);
1404         print_lock_name(other);
1405         printk("\n\nand interrupts could create inverse lock ordering between them.\n\n");
1406
1407         printk("\nother info that might help us debug this:\n");
1408         lockdep_print_held_locks(curr);
1409
1410         printk("\nthe first lock's dependencies:\n");
1411         print_lock_dependencies(this->class, 0);
1412
1413         printk("\nthe second lock's dependencies:\n");
1414         print_lock_dependencies(other, 0);
1415
1416         printk("\nstack backtrace:\n");
1417         dump_stack();
1418
1419         return 0;
1420 }
1421
1422 /*
1423  * Prove that in the forwards-direction subgraph starting at <this>
1424  * there is no lock matching <mask>:
1425  */
1426 static int
1427 check_usage_forwards(struct task_struct *curr, struct held_lock *this,
1428                      enum lock_usage_bit bit, const char *irqclass)
1429 {
1430         int ret;
1431
1432         find_usage_bit = bit;
1433         /* fills in <forwards_match> */
1434         ret = find_usage_forwards(this->class, 0);
1435         if (!ret || ret == 1)
1436                 return ret;
1437
1438         return print_irq_inversion_bug(curr, forwards_match, this, 1, irqclass);
1439 }
1440
1441 /*
1442  * Prove that in the backwards-direction subgraph starting at <this>
1443  * there is no lock matching <mask>:
1444  */
1445 static int
1446 check_usage_backwards(struct task_struct *curr, struct held_lock *this,
1447                       enum lock_usage_bit bit, const char *irqclass)
1448 {
1449         int ret;
1450
1451         find_usage_bit = bit;
1452         /* fills in <backwards_match> */
1453         ret = find_usage_backwards(this->class, 0);
1454         if (!ret || ret == 1)
1455                 return ret;
1456
1457         return print_irq_inversion_bug(curr, backwards_match, this, 0, irqclass);
1458 }
1459
1460 static inline void print_irqtrace_events(struct task_struct *curr)
1461 {
1462         printk("irq event stamp: %u\n", curr->irq_events);
1463         printk("hardirqs last  enabled at (%u): ", curr->hardirq_enable_event);
1464         print_ip_sym(curr->hardirq_enable_ip);
1465         printk("hardirqs last disabled at (%u): ", curr->hardirq_disable_event);
1466         print_ip_sym(curr->hardirq_disable_ip);
1467         printk("softirqs last  enabled at (%u): ", curr->softirq_enable_event);
1468         print_ip_sym(curr->softirq_enable_ip);
1469         printk("softirqs last disabled at (%u): ", curr->softirq_disable_event);
1470         print_ip_sym(curr->softirq_disable_ip);
1471 }
1472
1473 #else
1474 static inline void print_irqtrace_events(struct task_struct *curr)
1475 {
1476 }
1477 #endif
1478
1479 static int
1480 print_usage_bug(struct task_struct *curr, struct held_lock *this,
1481                 enum lock_usage_bit prev_bit, enum lock_usage_bit new_bit)
1482 {
1483         __raw_spin_unlock(&hash_lock);
1484         debug_locks_off();
1485         if (debug_locks_silent)
1486                 return 0;
1487
1488         printk("\n=================================\n");
1489         printk(  "[ INFO: inconsistent lock state ]\n");
1490         print_kernel_version();
1491         printk(  "---------------------------------\n");
1492
1493         printk("inconsistent {%s} -> {%s} usage.\n",
1494                 usage_str[prev_bit], usage_str[new_bit]);
1495
1496         printk("%s/%d [HC%u[%lu]:SC%u[%lu]:HE%u:SE%u] takes:\n",
1497                 curr->comm, curr->pid,
1498                 trace_hardirq_context(curr), hardirq_count() >> HARDIRQ_SHIFT,
1499                 trace_softirq_context(curr), softirq_count() >> SOFTIRQ_SHIFT,
1500                 trace_hardirqs_enabled(curr),
1501                 trace_softirqs_enabled(curr));
1502         print_lock(this);
1503
1504         printk("{%s} state was registered at:\n", usage_str[prev_bit]);
1505         print_stack_trace(this->class->usage_traces + prev_bit, 1);
1506
1507         print_irqtrace_events(curr);
1508         printk("\nother info that might help us debug this:\n");
1509         lockdep_print_held_locks(curr);
1510
1511         printk("\nstack backtrace:\n");
1512         dump_stack();
1513
1514         return 0;
1515 }
1516
1517 /*
1518  * Print out an error if an invalid bit is set:
1519  */
1520 static inline int
1521 valid_state(struct task_struct *curr, struct held_lock *this,
1522             enum lock_usage_bit new_bit, enum lock_usage_bit bad_bit)
1523 {
1524         if (unlikely(this->class->usage_mask & (1 << bad_bit)))
1525                 return print_usage_bug(curr, this, bad_bit, new_bit);
1526         return 1;
1527 }
1528
1529 #define STRICT_READ_CHECKS      1
1530
1531 /*
1532  * Mark a lock with a usage bit, and validate the state transition:
1533  */
1534 static int mark_lock(struct task_struct *curr, struct held_lock *this,
1535                      enum lock_usage_bit new_bit, unsigned long ip)
1536 {
1537         unsigned int new_mask = 1 << new_bit, ret = 1;
1538
1539         /*
1540          * If already set then do not dirty the cacheline,
1541          * nor do any checks:
1542          */
1543         if (likely(this->class->usage_mask & new_mask))
1544                 return 1;
1545
1546         __raw_spin_lock(&hash_lock);
1547         /*
1548          * Make sure we didnt race:
1549          */
1550         if (unlikely(this->class->usage_mask & new_mask)) {
1551                 __raw_spin_unlock(&hash_lock);
1552                 return 1;
1553         }
1554
1555         this->class->usage_mask |= new_mask;
1556
1557 #ifdef CONFIG_TRACE_IRQFLAGS
1558         if (new_bit == LOCK_ENABLED_HARDIRQS ||
1559                         new_bit == LOCK_ENABLED_HARDIRQS_READ)
1560                 ip = curr->hardirq_enable_ip;
1561         else if (new_bit == LOCK_ENABLED_SOFTIRQS ||
1562                         new_bit == LOCK_ENABLED_SOFTIRQS_READ)
1563                 ip = curr->softirq_enable_ip;
1564 #endif
1565         if (!save_trace(this->class->usage_traces + new_bit))
1566                 return 0;
1567
1568         switch (new_bit) {
1569 #ifdef CONFIG_TRACE_IRQFLAGS
1570         case LOCK_USED_IN_HARDIRQ:
1571                 if (!valid_state(curr, this, new_bit, LOCK_ENABLED_HARDIRQS))
1572                         return 0;
1573                 if (!valid_state(curr, this, new_bit,
1574                                  LOCK_ENABLED_HARDIRQS_READ))
1575                         return 0;
1576                 /*
1577                  * just marked it hardirq-safe, check that this lock
1578                  * took no hardirq-unsafe lock in the past:
1579                  */
1580                 if (!check_usage_forwards(curr, this,
1581                                           LOCK_ENABLED_HARDIRQS, "hard"))
1582                         return 0;
1583 #if STRICT_READ_CHECKS
1584                 /*
1585                  * just marked it hardirq-safe, check that this lock
1586                  * took no hardirq-unsafe-read lock in the past:
1587                  */
1588                 if (!check_usage_forwards(curr, this,
1589                                 LOCK_ENABLED_HARDIRQS_READ, "hard-read"))
1590                         return 0;
1591 #endif
1592                 if (hardirq_verbose(this->class))
1593                         ret = 2;
1594                 break;
1595         case LOCK_USED_IN_SOFTIRQ:
1596                 if (!valid_state(curr, this, new_bit, LOCK_ENABLED_SOFTIRQS))
1597                         return 0;
1598                 if (!valid_state(curr, this, new_bit,
1599                                  LOCK_ENABLED_SOFTIRQS_READ))
1600                         return 0;
1601                 /*
1602                  * just marked it softirq-safe, check that this lock
1603                  * took no softirq-unsafe lock in the past:
1604                  */
1605                 if (!check_usage_forwards(curr, this,
1606                                           LOCK_ENABLED_SOFTIRQS, "soft"))
1607                         return 0;
1608 #if STRICT_READ_CHECKS
1609                 /*
1610                  * just marked it softirq-safe, check that this lock
1611                  * took no softirq-unsafe-read lock in the past:
1612                  */
1613                 if (!check_usage_forwards(curr, this,
1614                                 LOCK_ENABLED_SOFTIRQS_READ, "soft-read"))
1615                         return 0;
1616 #endif
1617                 if (softirq_verbose(this->class))
1618                         ret = 2;
1619                 break;
1620         case LOCK_USED_IN_HARDIRQ_READ:
1621                 if (!valid_state(curr, this, new_bit, LOCK_ENABLED_HARDIRQS))
1622                         return 0;
1623                 /*
1624                  * just marked it hardirq-read-safe, check that this lock
1625                  * took no hardirq-unsafe lock in the past:
1626                  */
1627                 if (!check_usage_forwards(curr, this,
1628                                           LOCK_ENABLED_HARDIRQS, "hard"))
1629                         return 0;
1630                 if (hardirq_verbose(this->class))
1631                         ret = 2;
1632                 break;
1633         case LOCK_USED_IN_SOFTIRQ_READ:
1634                 if (!valid_state(curr, this, new_bit, LOCK_ENABLED_SOFTIRQS))
1635                         return 0;
1636                 /*
1637                  * just marked it softirq-read-safe, check that this lock
1638                  * took no softirq-unsafe lock in the past:
1639                  */
1640                 if (!check_usage_forwards(curr, this,
1641                                           LOCK_ENABLED_SOFTIRQS, "soft"))
1642                         return 0;
1643                 if (softirq_verbose(this->class))
1644                         ret = 2;
1645                 break;
1646         case LOCK_ENABLED_HARDIRQS:
1647                 if (!valid_state(curr, this, new_bit, LOCK_USED_IN_HARDIRQ))
1648                         return 0;
1649                 if (!valid_state(curr, this, new_bit,
1650                                  LOCK_USED_IN_HARDIRQ_READ))
1651                         return 0;
1652                 /*
1653                  * just marked it hardirq-unsafe, check that no hardirq-safe
1654                  * lock in the system ever took it in the past:
1655                  */
1656                 if (!check_usage_backwards(curr, this,
1657                                            LOCK_USED_IN_HARDIRQ, "hard"))
1658                         return 0;
1659 #if STRICT_READ_CHECKS
1660                 /*
1661                  * just marked it hardirq-unsafe, check that no
1662                  * hardirq-safe-read lock in the system ever took
1663                  * it in the past:
1664                  */
1665                 if (!check_usage_backwards(curr, this,
1666                                    LOCK_USED_IN_HARDIRQ_READ, "hard-read"))
1667                         return 0;
1668 #endif
1669                 if (hardirq_verbose(this->class))
1670                         ret = 2;
1671                 break;
1672         case LOCK_ENABLED_SOFTIRQS:
1673                 if (!valid_state(curr, this, new_bit, LOCK_USED_IN_SOFTIRQ))
1674                         return 0;
1675                 if (!valid_state(curr, this, new_bit,
1676                                  LOCK_USED_IN_SOFTIRQ_READ))
1677                         return 0;
1678                 /*
1679                  * just marked it softirq-unsafe, check that no softirq-safe
1680                  * lock in the system ever took it in the past:
1681                  */
1682                 if (!check_usage_backwards(curr, this,
1683                                            LOCK_USED_IN_SOFTIRQ, "soft"))
1684                         return 0;
1685 #if STRICT_READ_CHECKS
1686                 /*
1687                  * just marked it softirq-unsafe, check that no
1688                  * softirq-safe-read lock in the system ever took
1689                  * it in the past:
1690                  */
1691                 if (!check_usage_backwards(curr, this,
1692                                    LOCK_USED_IN_SOFTIRQ_READ, "soft-read"))
1693                         return 0;
1694 #endif
1695                 if (softirq_verbose(this->class))
1696                         ret = 2;
1697                 break;
1698         case LOCK_ENABLED_HARDIRQS_READ:
1699                 if (!valid_state(curr, this, new_bit, LOCK_USED_IN_HARDIRQ))
1700                         return 0;
1701 #if STRICT_READ_CHECKS
1702                 /*
1703                  * just marked it hardirq-read-unsafe, check that no
1704                  * hardirq-safe lock in the system ever took it in the past:
1705                  */
1706                 if (!check_usage_backwards(curr, this,
1707                                            LOCK_USED_IN_HARDIRQ, "hard"))
1708                         return 0;
1709 #endif
1710                 if (hardirq_verbose(this->class))
1711                         ret = 2;
1712                 break;
1713         case LOCK_ENABLED_SOFTIRQS_READ:
1714                 if (!valid_state(curr, this, new_bit, LOCK_USED_IN_SOFTIRQ))
1715                         return 0;
1716 #if STRICT_READ_CHECKS
1717                 /*
1718                  * just marked it softirq-read-unsafe, check that no
1719                  * softirq-safe lock in the system ever took it in the past:
1720                  */
1721                 if (!check_usage_backwards(curr, this,
1722                                            LOCK_USED_IN_SOFTIRQ, "soft"))
1723                         return 0;
1724 #endif
1725                 if (softirq_verbose(this->class))
1726                         ret = 2;
1727                 break;
1728 #endif
1729         case LOCK_USED:
1730                 /*
1731                  * Add it to the global list of classes:
1732                  */
1733                 list_add_tail_rcu(&this->class->lock_entry, &all_lock_classes);
1734                 debug_atomic_dec(&nr_unused_locks);
1735                 break;
1736         default:
1737                 __raw_spin_unlock(&hash_lock);
1738                 debug_locks_off();
1739                 WARN_ON(1);
1740                 return 0;
1741         }
1742
1743         __raw_spin_unlock(&hash_lock);
1744
1745         /*
1746          * We must printk outside of the hash_lock:
1747          */
1748         if (ret == 2) {
1749                 printk("\nmarked lock as {%s}:\n", usage_str[new_bit]);
1750                 print_lock(this);
1751                 print_irqtrace_events(curr);
1752                 dump_stack();
1753         }
1754
1755         return ret;
1756 }
1757
1758 #ifdef CONFIG_TRACE_IRQFLAGS
1759 /*
1760  * Mark all held locks with a usage bit:
1761  */
1762 static int
1763 mark_held_locks(struct task_struct *curr, int hardirq, unsigned long ip)
1764 {
1765         enum lock_usage_bit usage_bit;
1766         struct held_lock *hlock;
1767         int i;
1768
1769         for (i = 0; i < curr->lockdep_depth; i++) {
1770                 hlock = curr->held_locks + i;
1771
1772                 if (hardirq) {
1773                         if (hlock->read)
1774                                 usage_bit = LOCK_ENABLED_HARDIRQS_READ;
1775                         else
1776                                 usage_bit = LOCK_ENABLED_HARDIRQS;
1777                 } else {
1778                         if (hlock->read)
1779                                 usage_bit = LOCK_ENABLED_SOFTIRQS_READ;
1780                         else
1781                                 usage_bit = LOCK_ENABLED_SOFTIRQS;
1782                 }
1783                 if (!mark_lock(curr, hlock, usage_bit, ip))
1784                         return 0;
1785         }
1786
1787         return 1;
1788 }
1789
1790 /*
1791  * Debugging helper: via this flag we know that we are in
1792  * 'early bootup code', and will warn about any invalid irqs-on event:
1793  */
1794 static int early_boot_irqs_enabled;
1795
1796 void early_boot_irqs_off(void)
1797 {
1798         early_boot_irqs_enabled = 0;
1799 }
1800
1801 void early_boot_irqs_on(void)
1802 {
1803         early_boot_irqs_enabled = 1;
1804 }
1805
1806 /*
1807  * Hardirqs will be enabled:
1808  */
1809 void trace_hardirqs_on(void)
1810 {
1811         struct task_struct *curr = current;
1812         unsigned long ip;
1813
1814         if (unlikely(!debug_locks || current->lockdep_recursion))
1815                 return;
1816
1817         if (DEBUG_LOCKS_WARN_ON(unlikely(!early_boot_irqs_enabled)))
1818                 return;
1819
1820         if (unlikely(curr->hardirqs_enabled)) {
1821                 debug_atomic_inc(&redundant_hardirqs_on);
1822                 return;
1823         }
1824         /* we'll do an OFF -> ON transition: */
1825         curr->hardirqs_enabled = 1;
1826         ip = (unsigned long) __builtin_return_address(0);
1827
1828         if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
1829                 return;
1830         if (DEBUG_LOCKS_WARN_ON(current->hardirq_context))
1831                 return;
1832         /*
1833          * We are going to turn hardirqs on, so set the
1834          * usage bit for all held locks:
1835          */
1836         if (!mark_held_locks(curr, 1, ip))
1837                 return;
1838         /*
1839          * If we have softirqs enabled, then set the usage
1840          * bit for all held locks. (disabled hardirqs prevented
1841          * this bit from being set before)
1842          */
1843         if (curr->softirqs_enabled)
1844                 if (!mark_held_locks(curr, 0, ip))
1845                         return;
1846
1847         curr->hardirq_enable_ip = ip;
1848         curr->hardirq_enable_event = ++curr->irq_events;
1849         debug_atomic_inc(&hardirqs_on_events);
1850 }
1851
1852 EXPORT_SYMBOL(trace_hardirqs_on);
1853
1854 /*
1855  * Hardirqs were disabled:
1856  */
1857 void trace_hardirqs_off(void)
1858 {
1859         struct task_struct *curr = current;
1860
1861         if (unlikely(!debug_locks || current->lockdep_recursion))
1862                 return;
1863
1864         if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
1865                 return;
1866
1867         if (curr->hardirqs_enabled) {
1868                 /*
1869                  * We have done an ON -> OFF transition:
1870                  */
1871                 curr->hardirqs_enabled = 0;
1872                 curr->hardirq_disable_ip = _RET_IP_;
1873                 curr->hardirq_disable_event = ++curr->irq_events;
1874                 debug_atomic_inc(&hardirqs_off_events);
1875         } else
1876                 debug_atomic_inc(&redundant_hardirqs_off);
1877 }
1878
1879 EXPORT_SYMBOL(trace_hardirqs_off);
1880
1881 /*
1882  * Softirqs will be enabled:
1883  */
1884 void trace_softirqs_on(unsigned long ip)
1885 {
1886         struct task_struct *curr = current;
1887
1888         if (unlikely(!debug_locks))
1889                 return;
1890
1891         if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
1892                 return;
1893
1894         if (curr->softirqs_enabled) {
1895                 debug_atomic_inc(&redundant_softirqs_on);
1896                 return;
1897         }
1898
1899         /*
1900          * We'll do an OFF -> ON transition:
1901          */
1902         curr->softirqs_enabled = 1;
1903         curr->softirq_enable_ip = ip;
1904         curr->softirq_enable_event = ++curr->irq_events;
1905         debug_atomic_inc(&softirqs_on_events);
1906         /*
1907          * We are going to turn softirqs on, so set the
1908          * usage bit for all held locks, if hardirqs are
1909          * enabled too:
1910          */
1911         if (curr->hardirqs_enabled)
1912                 mark_held_locks(curr, 0, ip);
1913 }
1914
1915 /*
1916  * Softirqs were disabled:
1917  */
1918 void trace_softirqs_off(unsigned long ip)
1919 {
1920         struct task_struct *curr = current;
1921
1922         if (unlikely(!debug_locks))
1923                 return;
1924
1925         if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
1926                 return;
1927
1928         if (curr->softirqs_enabled) {
1929                 /*
1930                  * We have done an ON -> OFF transition:
1931                  */
1932                 curr->softirqs_enabled = 0;
1933                 curr->softirq_disable_ip = ip;
1934                 curr->softirq_disable_event = ++curr->irq_events;
1935                 debug_atomic_inc(&softirqs_off_events);
1936                 DEBUG_LOCKS_WARN_ON(!softirq_count());
1937         } else
1938                 debug_atomic_inc(&redundant_softirqs_off);
1939 }
1940
1941 #endif
1942
1943 /*
1944  * Initialize a lock instance's lock-class mapping info:
1945  */
1946 void lockdep_init_map(struct lockdep_map *lock, const char *name,
1947                       struct lock_class_key *key, int subclass)
1948 {
1949         if (unlikely(!debug_locks))
1950                 return;
1951
1952         if (DEBUG_LOCKS_WARN_ON(!key))
1953                 return;
1954         if (DEBUG_LOCKS_WARN_ON(!name))
1955                 return;
1956         /*
1957          * Sanity check, the lock-class key must be persistent:
1958          */
1959         if (!static_obj(key)) {
1960                 printk("BUG: key %p not in .data!\n", key);
1961                 DEBUG_LOCKS_WARN_ON(1);
1962                 return;
1963         }
1964         lock->name = name;
1965         lock->key = key;
1966         lock->class_cache = NULL;
1967         if (subclass)
1968                 register_lock_class(lock, subclass, 1);
1969 }
1970
1971 EXPORT_SYMBOL_GPL(lockdep_init_map);
1972
1973 /*
1974  * This gets called for every mutex_lock*()/spin_lock*() operation.
1975  * We maintain the dependency maps and validate the locking attempt:
1976  */
1977 static int __lock_acquire(struct lockdep_map *lock, unsigned int subclass,
1978                           int trylock, int read, int check, int hardirqs_off,
1979                           unsigned long ip)
1980 {
1981         struct task_struct *curr = current;
1982         struct lock_class *class = NULL;
1983         struct held_lock *hlock;
1984         unsigned int depth, id;
1985         int chain_head = 0;
1986         u64 chain_key;
1987
1988         if (unlikely(!debug_locks))
1989                 return 0;
1990
1991         if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
1992                 return 0;
1993
1994         if (unlikely(subclass >= MAX_LOCKDEP_SUBCLASSES)) {
1995                 debug_locks_off();
1996                 printk("BUG: MAX_LOCKDEP_SUBCLASSES too low!\n");
1997                 printk("turning off the locking correctness validator.\n");
1998                 return 0;
1999         }
2000
2001         if (!subclass)
2002                 class = lock->class_cache;
2003         /*
2004          * Not cached yet or subclass?
2005          */
2006         if (unlikely(!class)) {
2007                 class = register_lock_class(lock, subclass, 0);
2008                 if (!class)
2009                         return 0;
2010         }
2011         debug_atomic_inc((atomic_t *)&class->ops);
2012         if (very_verbose(class)) {
2013                 printk("\nacquire class [%p] %s", class->key, class->name);
2014                 if (class->name_version > 1)
2015                         printk("#%d", class->name_version);
2016                 printk("\n");
2017                 dump_stack();
2018         }
2019
2020         /*
2021          * Add the lock to the list of currently held locks.
2022          * (we dont increase the depth just yet, up until the
2023          * dependency checks are done)
2024          */
2025         depth = curr->lockdep_depth;
2026         if (DEBUG_LOCKS_WARN_ON(depth >= MAX_LOCK_DEPTH))
2027                 return 0;
2028
2029         hlock = curr->held_locks + depth;
2030
2031         hlock->class = class;
2032         hlock->acquire_ip = ip;
2033         hlock->instance = lock;
2034         hlock->trylock = trylock;
2035         hlock->read = read;
2036         hlock->check = check;
2037         hlock->hardirqs_off = hardirqs_off;
2038
2039         if (check != 2)
2040                 goto out_calc_hash;
2041 #ifdef CONFIG_TRACE_IRQFLAGS
2042         /*
2043          * If non-trylock use in a hardirq or softirq context, then
2044          * mark the lock as used in these contexts:
2045          */
2046         if (!trylock) {
2047                 if (read) {
2048                         if (curr->hardirq_context)
2049                                 if (!mark_lock(curr, hlock,
2050                                                 LOCK_USED_IN_HARDIRQ_READ, ip))
2051                                         return 0;
2052                         if (curr->softirq_context)
2053                                 if (!mark_lock(curr, hlock,
2054                                                 LOCK_USED_IN_SOFTIRQ_READ, ip))
2055                                         return 0;
2056                 } else {
2057                         if (curr->hardirq_context)
2058                                 if (!mark_lock(curr, hlock, LOCK_USED_IN_HARDIRQ, ip))
2059                                         return 0;
2060                         if (curr->softirq_context)
2061                                 if (!mark_lock(curr, hlock, LOCK_USED_IN_SOFTIRQ, ip))
2062                                         return 0;
2063                 }
2064         }
2065         if (!hardirqs_off) {
2066                 if (read) {
2067                         if (!mark_lock(curr, hlock,
2068                                         LOCK_ENABLED_HARDIRQS_READ, ip))
2069                                 return 0;
2070                         if (curr->softirqs_enabled)
2071                                 if (!mark_lock(curr, hlock,
2072                                                 LOCK_ENABLED_SOFTIRQS_READ, ip))
2073                                         return 0;
2074                 } else {
2075                         if (!mark_lock(curr, hlock,
2076                                         LOCK_ENABLED_HARDIRQS, ip))
2077                                 return 0;
2078                         if (curr->softirqs_enabled)
2079                                 if (!mark_lock(curr, hlock,
2080                                                 LOCK_ENABLED_SOFTIRQS, ip))
2081                                         return 0;
2082                 }
2083         }
2084 #endif
2085         /* mark it as used: */
2086         if (!mark_lock(curr, hlock, LOCK_USED, ip))
2087                 return 0;
2088 out_calc_hash:
2089         /*
2090          * Calculate the chain hash: it's the combined has of all the
2091          * lock keys along the dependency chain. We save the hash value
2092          * at every step so that we can get the current hash easily
2093          * after unlock. The chain hash is then used to cache dependency
2094          * results.
2095          *
2096          * The 'key ID' is what is the most compact key value to drive
2097          * the hash, not class->key.
2098          */
2099         id = class - lock_classes;
2100         if (DEBUG_LOCKS_WARN_ON(id >= MAX_LOCKDEP_KEYS))
2101                 return 0;
2102
2103         chain_key = curr->curr_chain_key;
2104         if (!depth) {
2105                 if (DEBUG_LOCKS_WARN_ON(chain_key != 0))
2106                         return 0;
2107                 chain_head = 1;
2108         }
2109
2110         hlock->prev_chain_key = chain_key;
2111
2112 #ifdef CONFIG_TRACE_IRQFLAGS
2113         /*
2114          * Keep track of points where we cross into an interrupt context:
2115          */
2116         hlock->irq_context = 2*(curr->hardirq_context ? 1 : 0) +
2117                                 curr->softirq_context;
2118         if (depth) {
2119                 struct held_lock *prev_hlock;
2120
2121                 prev_hlock = curr->held_locks + depth-1;
2122                 /*
2123                  * If we cross into another context, reset the
2124                  * hash key (this also prevents the checking and the
2125                  * adding of the dependency to 'prev'):
2126                  */
2127                 if (prev_hlock->irq_context != hlock->irq_context) {
2128                         chain_key = 0;
2129                         chain_head = 1;
2130                 }
2131         }
2132 #endif
2133         chain_key = iterate_chain_key(chain_key, id);
2134         curr->curr_chain_key = chain_key;
2135
2136         /*
2137          * Trylock needs to maintain the stack of held locks, but it
2138          * does not add new dependencies, because trylock can be done
2139          * in any order.
2140          *
2141          * We look up the chain_key and do the O(N^2) check and update of
2142          * the dependencies only if this is a new dependency chain.
2143          * (If lookup_chain_cache() returns with 1 it acquires
2144          * hash_lock for us)
2145          */
2146         if (!trylock && (check == 2) && lookup_chain_cache(chain_key)) {
2147                 /*
2148                  * Check whether last held lock:
2149                  *
2150                  * - is irq-safe, if this lock is irq-unsafe
2151                  * - is softirq-safe, if this lock is hardirq-unsafe
2152                  *
2153                  * And check whether the new lock's dependency graph
2154                  * could lead back to the previous lock.
2155                  *
2156                  * any of these scenarios could lead to a deadlock. If
2157                  * All validations
2158                  */
2159                 int ret = check_deadlock(curr, hlock, lock, read);
2160
2161                 if (!ret)
2162                         return 0;
2163                 /*
2164                  * Mark recursive read, as we jump over it when
2165                  * building dependencies (just like we jump over
2166                  * trylock entries):
2167                  */
2168                 if (ret == 2)
2169                         hlock->read = 2;
2170                 /*
2171                  * Add dependency only if this lock is not the head
2172                  * of the chain, and if it's not a secondary read-lock:
2173                  */
2174                 if (!chain_head && ret != 2)
2175                         if (!check_prevs_add(curr, hlock))
2176                                 return 0;
2177                 __raw_spin_unlock(&hash_lock);
2178         }
2179         curr->lockdep_depth++;
2180         check_chain_key(curr);
2181         if (unlikely(curr->lockdep_depth >= MAX_LOCK_DEPTH)) {
2182                 debug_locks_off();
2183                 printk("BUG: MAX_LOCK_DEPTH too low!\n");
2184                 printk("turning off the locking correctness validator.\n");
2185                 return 0;
2186         }
2187         if (unlikely(curr->lockdep_depth > max_lockdep_depth))
2188                 max_lockdep_depth = curr->lockdep_depth;
2189
2190         return 1;
2191 }
2192
2193 static int
2194 print_unlock_inbalance_bug(struct task_struct *curr, struct lockdep_map *lock,
2195                            unsigned long ip)
2196 {
2197         if (!debug_locks_off())
2198                 return 0;
2199         if (debug_locks_silent)
2200                 return 0;
2201
2202         printk("\n=====================================\n");
2203         printk(  "[ BUG: bad unlock balance detected! ]\n");
2204         printk(  "-------------------------------------\n");
2205         printk("%s/%d is trying to release lock (",
2206                 curr->comm, curr->pid);
2207         print_lockdep_cache(lock);
2208         printk(") at:\n");
2209         print_ip_sym(ip);
2210         printk("but there are no more locks to release!\n");
2211         printk("\nother info that might help us debug this:\n");
2212         lockdep_print_held_locks(curr);
2213
2214         printk("\nstack backtrace:\n");
2215         dump_stack();
2216
2217         return 0;
2218 }
2219
2220 /*
2221  * Common debugging checks for both nested and non-nested unlock:
2222  */
2223 static int check_unlock(struct task_struct *curr, struct lockdep_map *lock,
2224                         unsigned long ip)
2225 {
2226         if (unlikely(!debug_locks))
2227                 return 0;
2228         if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2229                 return 0;
2230
2231         if (curr->lockdep_depth <= 0)
2232                 return print_unlock_inbalance_bug(curr, lock, ip);
2233
2234         return 1;
2235 }
2236
2237 /*
2238  * Remove the lock to the list of currently held locks in a
2239  * potentially non-nested (out of order) manner. This is a
2240  * relatively rare operation, as all the unlock APIs default
2241  * to nested mode (which uses lock_release()):
2242  */
2243 static int
2244 lock_release_non_nested(struct task_struct *curr,
2245                         struct lockdep_map *lock, unsigned long ip)
2246 {
2247         struct held_lock *hlock, *prev_hlock;
2248         unsigned int depth;
2249         int i;
2250
2251         /*
2252          * Check whether the lock exists in the current stack
2253          * of held locks:
2254          */
2255         depth = curr->lockdep_depth;
2256         if (DEBUG_LOCKS_WARN_ON(!depth))
2257                 return 0;
2258
2259         prev_hlock = NULL;
2260         for (i = depth-1; i >= 0; i--) {
2261                 hlock = curr->held_locks + i;
2262                 /*
2263                  * We must not cross into another context:
2264                  */
2265                 if (prev_hlock && prev_hlock->irq_context != hlock->irq_context)
2266                         break;
2267                 if (hlock->instance == lock)
2268                         goto found_it;
2269                 prev_hlock = hlock;
2270         }
2271         return print_unlock_inbalance_bug(curr, lock, ip);
2272
2273 found_it:
2274         /*
2275          * We have the right lock to unlock, 'hlock' points to it.
2276          * Now we remove it from the stack, and add back the other
2277          * entries (if any), recalculating the hash along the way:
2278          */
2279         curr->lockdep_depth = i;
2280         curr->curr_chain_key = hlock->prev_chain_key;
2281
2282         for (i++; i < depth; i++) {
2283                 hlock = curr->held_locks + i;
2284                 if (!__lock_acquire(hlock->instance,
2285                         hlock->class->subclass, hlock->trylock,
2286                                 hlock->read, hlock->check, hlock->hardirqs_off,
2287                                 hlock->acquire_ip))
2288                         return 0;
2289         }
2290
2291         if (DEBUG_LOCKS_WARN_ON(curr->lockdep_depth != depth - 1))
2292                 return 0;
2293         return 1;
2294 }
2295
2296 /*
2297  * Remove the lock to the list of currently held locks - this gets
2298  * called on mutex_unlock()/spin_unlock*() (or on a failed
2299  * mutex_lock_interruptible()). This is done for unlocks that nest
2300  * perfectly. (i.e. the current top of the lock-stack is unlocked)
2301  */
2302 static int lock_release_nested(struct task_struct *curr,
2303                                struct lockdep_map *lock, unsigned long ip)
2304 {
2305         struct held_lock *hlock;
2306         unsigned int depth;
2307
2308         /*
2309          * Pop off the top of the lock stack:
2310          */
2311         depth = curr->lockdep_depth - 1;
2312         hlock = curr->held_locks + depth;
2313
2314         /*
2315          * Is the unlock non-nested:
2316          */
2317         if (hlock->instance != lock)
2318                 return lock_release_non_nested(curr, lock, ip);
2319         curr->lockdep_depth--;
2320
2321         if (DEBUG_LOCKS_WARN_ON(!depth && (hlock->prev_chain_key != 0)))
2322                 return 0;
2323
2324         curr->curr_chain_key = hlock->prev_chain_key;
2325
2326 #ifdef CONFIG_DEBUG_LOCKDEP
2327         hlock->prev_chain_key = 0;
2328         hlock->class = NULL;
2329         hlock->acquire_ip = 0;
2330         hlock->irq_context = 0;
2331 #endif
2332         return 1;
2333 }
2334
2335 /*
2336  * Remove the lock to the list of currently held locks - this gets
2337  * called on mutex_unlock()/spin_unlock*() (or on a failed
2338  * mutex_lock_interruptible()). This is done for unlocks that nest
2339  * perfectly. (i.e. the current top of the lock-stack is unlocked)
2340  */
2341 static void
2342 __lock_release(struct lockdep_map *lock, int nested, unsigned long ip)
2343 {
2344         struct task_struct *curr = current;
2345
2346         if (!check_unlock(curr, lock, ip))
2347                 return;
2348
2349         if (nested) {
2350                 if (!lock_release_nested(curr, lock, ip))
2351                         return;
2352         } else {
2353                 if (!lock_release_non_nested(curr, lock, ip))
2354                         return;
2355         }
2356
2357         check_chain_key(curr);
2358 }
2359
2360 /*
2361  * Check whether we follow the irq-flags state precisely:
2362  */
2363 static void check_flags(unsigned long flags)
2364 {
2365 #if defined(CONFIG_DEBUG_LOCKDEP) && defined(CONFIG_TRACE_IRQFLAGS)
2366         if (!debug_locks)
2367                 return;
2368
2369         if (irqs_disabled_flags(flags))
2370                 DEBUG_LOCKS_WARN_ON(current->hardirqs_enabled);
2371         else
2372                 DEBUG_LOCKS_WARN_ON(!current->hardirqs_enabled);
2373
2374         /*
2375          * We dont accurately track softirq state in e.g.
2376          * hardirq contexts (such as on 4KSTACKS), so only
2377          * check if not in hardirq contexts:
2378          */
2379         if (!hardirq_count()) {
2380                 if (softirq_count())
2381                         DEBUG_LOCKS_WARN_ON(current->softirqs_enabled);
2382                 else
2383                         DEBUG_LOCKS_WARN_ON(!current->softirqs_enabled);
2384         }
2385
2386         if (!debug_locks)
2387                 print_irqtrace_events(current);
2388 #endif
2389 }
2390
2391 /*
2392  * We are not always called with irqs disabled - do that here,
2393  * and also avoid lockdep recursion:
2394  */
2395 void lock_acquire(struct lockdep_map *lock, unsigned int subclass,
2396                   int trylock, int read, int check, unsigned long ip)
2397 {
2398         unsigned long flags;
2399
2400         if (unlikely(current->lockdep_recursion))
2401                 return;
2402
2403         raw_local_irq_save(flags);
2404         check_flags(flags);
2405
2406         current->lockdep_recursion = 1;
2407         __lock_acquire(lock, subclass, trylock, read, check,
2408                        irqs_disabled_flags(flags), ip);
2409         current->lockdep_recursion = 0;
2410         raw_local_irq_restore(flags);
2411 }
2412
2413 EXPORT_SYMBOL_GPL(lock_acquire);
2414
2415 void lock_release(struct lockdep_map *lock, int nested, unsigned long ip)
2416 {
2417         unsigned long flags;
2418
2419         if (unlikely(current->lockdep_recursion))
2420                 return;
2421
2422         raw_local_irq_save(flags);
2423         check_flags(flags);
2424         current->lockdep_recursion = 1;
2425         __lock_release(lock, nested, ip);
2426         current->lockdep_recursion = 0;
2427         raw_local_irq_restore(flags);
2428 }
2429
2430 EXPORT_SYMBOL_GPL(lock_release);
2431
2432 /*
2433  * Used by the testsuite, sanitize the validator state
2434  * after a simulated failure:
2435  */
2436
2437 void lockdep_reset(void)
2438 {
2439         unsigned long flags;
2440
2441         raw_local_irq_save(flags);
2442         current->curr_chain_key = 0;
2443         current->lockdep_depth = 0;
2444         current->lockdep_recursion = 0;
2445         memset(current->held_locks, 0, MAX_LOCK_DEPTH*sizeof(struct held_lock));
2446         nr_hardirq_chains = 0;
2447         nr_softirq_chains = 0;
2448         nr_process_chains = 0;
2449         debug_locks = 1;
2450         raw_local_irq_restore(flags);
2451 }
2452
2453 static void zap_class(struct lock_class *class)
2454 {
2455         int i;
2456
2457         /*
2458          * Remove all dependencies this lock is
2459          * involved in:
2460          */
2461         for (i = 0; i < nr_list_entries; i++) {
2462                 if (list_entries[i].class == class)
2463                         list_del_rcu(&list_entries[i].entry);
2464         }
2465         /*
2466          * Unhash the class and remove it from the all_lock_classes list:
2467          */
2468         list_del_rcu(&class->hash_entry);
2469         list_del_rcu(&class->lock_entry);
2470
2471 }
2472
2473 static inline int within(void *addr, void *start, unsigned long size)
2474 {
2475         return addr >= start && addr < start + size;
2476 }
2477
2478 void lockdep_free_key_range(void *start, unsigned long size)
2479 {
2480         struct lock_class *class, *next;
2481         struct list_head *head;
2482         unsigned long flags;
2483         int i;
2484
2485         raw_local_irq_save(flags);
2486         __raw_spin_lock(&hash_lock);
2487
2488         /*
2489          * Unhash all classes that were created by this module:
2490          */
2491         for (i = 0; i < CLASSHASH_SIZE; i++) {
2492                 head = classhash_table + i;
2493                 if (list_empty(head))
2494                         continue;
2495                 list_for_each_entry_safe(class, next, head, hash_entry)
2496                         if (within(class->key, start, size))
2497                                 zap_class(class);
2498         }
2499
2500         __raw_spin_unlock(&hash_lock);
2501         raw_local_irq_restore(flags);
2502 }
2503
2504 void lockdep_reset_lock(struct lockdep_map *lock)
2505 {
2506         struct lock_class *class, *next;
2507         struct list_head *head;
2508         unsigned long flags;
2509         int i, j;
2510
2511         raw_local_irq_save(flags);
2512
2513         /*
2514          * Remove all classes this lock might have:
2515          */
2516         for (j = 0; j < MAX_LOCKDEP_SUBCLASSES; j++) {
2517                 /*
2518                  * If the class exists we look it up and zap it:
2519                  */
2520                 class = look_up_lock_class(lock, j);
2521                 if (class)
2522                         zap_class(class);
2523         }
2524         /*
2525          * Debug check: in the end all mapped classes should
2526          * be gone.
2527          */
2528         __raw_spin_lock(&hash_lock);
2529         for (i = 0; i < CLASSHASH_SIZE; i++) {
2530                 head = classhash_table + i;
2531                 if (list_empty(head))
2532                         continue;
2533                 list_for_each_entry_safe(class, next, head, hash_entry) {
2534                         if (unlikely(class == lock->class_cache)) {
2535                                 __raw_spin_unlock(&hash_lock);
2536                                 DEBUG_LOCKS_WARN_ON(1);
2537                                 goto out_restore;
2538                         }
2539                 }
2540         }
2541         __raw_spin_unlock(&hash_lock);
2542
2543 out_restore:
2544         raw_local_irq_restore(flags);
2545 }
2546
2547 void __init lockdep_init(void)
2548 {
2549         int i;
2550
2551         /*
2552          * Some architectures have their own start_kernel()
2553          * code which calls lockdep_init(), while we also
2554          * call lockdep_init() from the start_kernel() itself,
2555          * and we want to initialize the hashes only once:
2556          */
2557         if (lockdep_initialized)
2558                 return;
2559
2560         for (i = 0; i < CLASSHASH_SIZE; i++)
2561                 INIT_LIST_HEAD(classhash_table + i);
2562
2563         for (i = 0; i < CHAINHASH_SIZE; i++)
2564                 INIT_LIST_HEAD(chainhash_table + i);
2565
2566         lockdep_initialized = 1;
2567 }
2568
2569 void __init lockdep_info(void)
2570 {
2571         printk("Lock dependency validator: Copyright (c) 2006 Red Hat, Inc., Ingo Molnar\n");
2572
2573         printk("... MAX_LOCKDEP_SUBCLASSES:    %lu\n", MAX_LOCKDEP_SUBCLASSES);
2574         printk("... MAX_LOCK_DEPTH:          %lu\n", MAX_LOCK_DEPTH);
2575         printk("... MAX_LOCKDEP_KEYS:        %lu\n", MAX_LOCKDEP_KEYS);
2576         printk("... CLASSHASH_SIZE:           %lu\n", CLASSHASH_SIZE);
2577         printk("... MAX_LOCKDEP_ENTRIES:     %lu\n", MAX_LOCKDEP_ENTRIES);
2578         printk("... MAX_LOCKDEP_CHAINS:      %lu\n", MAX_LOCKDEP_CHAINS);
2579         printk("... CHAINHASH_SIZE:          %lu\n", CHAINHASH_SIZE);
2580
2581         printk(" memory used by lock dependency info: %lu kB\n",
2582                 (sizeof(struct lock_class) * MAX_LOCKDEP_KEYS +
2583                 sizeof(struct list_head) * CLASSHASH_SIZE +
2584                 sizeof(struct lock_list) * MAX_LOCKDEP_ENTRIES +
2585                 sizeof(struct lock_chain) * MAX_LOCKDEP_CHAINS +
2586                 sizeof(struct list_head) * CHAINHASH_SIZE) / 1024);
2587
2588         printk(" per task-struct memory footprint: %lu bytes\n",
2589                 sizeof(struct held_lock) * MAX_LOCK_DEPTH);
2590
2591 #ifdef CONFIG_DEBUG_LOCKDEP
2592         if (lockdep_init_error)
2593                 printk("WARNING: lockdep init error! Arch code didnt call lockdep_init() early enough?\n");
2594 #endif
2595 }
2596
2597 static inline int in_range(const void *start, const void *addr, const void *end)
2598 {
2599         return addr >= start && addr <= end;
2600 }
2601
2602 static void
2603 print_freed_lock_bug(struct task_struct *curr, const void *mem_from,
2604                      const void *mem_to, struct held_lock *hlock)
2605 {
2606         if (!debug_locks_off())
2607                 return;
2608         if (debug_locks_silent)
2609                 return;
2610
2611         printk("\n=========================\n");
2612         printk(  "[ BUG: held lock freed! ]\n");
2613         printk(  "-------------------------\n");
2614         printk("%s/%d is freeing memory %p-%p, with a lock still held there!\n",
2615                 curr->comm, curr->pid, mem_from, mem_to-1);
2616         print_lock(hlock);
2617         lockdep_print_held_locks(curr);
2618
2619         printk("\nstack backtrace:\n");
2620         dump_stack();
2621 }
2622
2623 /*
2624  * Called when kernel memory is freed (or unmapped), or if a lock
2625  * is destroyed or reinitialized - this code checks whether there is
2626  * any held lock in the memory range of <from> to <to>:
2627  */
2628 void debug_check_no_locks_freed(const void *mem_from, unsigned long mem_len)
2629 {
2630         const void *mem_to = mem_from + mem_len, *lock_from, *lock_to;
2631         struct task_struct *curr = current;
2632         struct held_lock *hlock;
2633         unsigned long flags;
2634         int i;
2635
2636         if (unlikely(!debug_locks))
2637                 return;
2638
2639         local_irq_save(flags);
2640         for (i = 0; i < curr->lockdep_depth; i++) {
2641                 hlock = curr->held_locks + i;
2642
2643                 lock_from = (void *)hlock->instance;
2644                 lock_to = (void *)(hlock->instance + 1);
2645
2646                 if (!in_range(mem_from, lock_from, mem_to) &&
2647                                         !in_range(mem_from, lock_to, mem_to))
2648                         continue;
2649
2650                 print_freed_lock_bug(curr, mem_from, mem_to, hlock);
2651                 break;
2652         }
2653         local_irq_restore(flags);
2654 }
2655 EXPORT_SYMBOL_GPL(debug_check_no_locks_freed);
2656
2657 static void print_held_locks_bug(struct task_struct *curr)
2658 {
2659         if (!debug_locks_off())
2660                 return;
2661         if (debug_locks_silent)
2662                 return;
2663
2664         printk("\n=====================================\n");
2665         printk(  "[ BUG: lock held at task exit time! ]\n");
2666         printk(  "-------------------------------------\n");
2667         printk("%s/%d is exiting with locks still held!\n",
2668                 curr->comm, curr->pid);
2669         lockdep_print_held_locks(curr);
2670
2671         printk("\nstack backtrace:\n");
2672         dump_stack();
2673 }
2674
2675 void debug_check_no_locks_held(struct task_struct *task)
2676 {
2677         if (unlikely(task->lockdep_depth > 0))
2678                 print_held_locks_bug(task);
2679 }
2680
2681 void debug_show_all_locks(void)
2682 {
2683         struct task_struct *g, *p;
2684         int count = 10;
2685         int unlock = 1;
2686
2687         printk("\nShowing all locks held in the system:\n");
2688
2689         /*
2690          * Here we try to get the tasklist_lock as hard as possible,
2691          * if not successful after 2 seconds we ignore it (but keep
2692          * trying). This is to enable a debug printout even if a
2693          * tasklist_lock-holding task deadlocks or crashes.
2694          */
2695 retry:
2696         if (!read_trylock(&tasklist_lock)) {
2697                 if (count == 10)
2698                         printk("hm, tasklist_lock locked, retrying... ");
2699                 if (count) {
2700                         count--;
2701                         printk(" #%d", 10-count);
2702                         mdelay(200);
2703                         goto retry;
2704                 }
2705                 printk(" ignoring it.\n");
2706                 unlock = 0;
2707         }
2708         if (count != 10)
2709                 printk(" locked it.\n");
2710
2711         do_each_thread(g, p) {
2712                 if (p->lockdep_depth)
2713                         lockdep_print_held_locks(p);
2714                 if (!unlock)
2715                         if (read_trylock(&tasklist_lock))
2716                                 unlock = 1;
2717         } while_each_thread(g, p);
2718
2719         printk("\n");
2720         printk("=============================================\n\n");
2721
2722         if (unlock)
2723                 read_unlock(&tasklist_lock);
2724 }
2725
2726 EXPORT_SYMBOL_GPL(debug_show_all_locks);
2727
2728 void debug_show_held_locks(struct task_struct *task)
2729 {
2730         lockdep_print_held_locks(task);
2731 }
2732
2733 EXPORT_SYMBOL_GPL(debug_show_held_locks);
2734