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