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