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