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