lockdep: simplify get_user_chars()
[safe/jmp/linux-2.6] / 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,2007 Red Hat, Inc., Ingo Molnar <mingo@redhat.com>
9  *  Copyright (C) 2007 Red Hat, Inc., Peter Zijlstra <pzijlstr@redhat.com>
10  *
11  * this code maps all the lock dependencies as they occur in a live kernel
12  * and will warn about the following classes of locking bugs:
13  *
14  * - lock inversion scenarios
15  * - circular lock dependencies
16  * - hardirq/softirq safe/unsafe locking bugs
17  *
18  * Bugs are reported even if the current locking scenario does not cause
19  * any deadlock at this point.
20  *
21  * I.e. if anytime in the past two locks were taken in a different order,
22  * even if it happened for another task, even if those were different
23  * locks (but of the same class as this lock), this code will detect it.
24  *
25  * Thanks to Arjan van de Ven for coming up with the initial idea of
26  * mapping lock dependencies runtime.
27  */
28 #define DISABLE_BRANCH_PROFILING
29 #include <linux/mutex.h>
30 #include <linux/sched.h>
31 #include <linux/delay.h>
32 #include <linux/module.h>
33 #include <linux/proc_fs.h>
34 #include <linux/seq_file.h>
35 #include <linux/spinlock.h>
36 #include <linux/kallsyms.h>
37 #include <linux/interrupt.h>
38 #include <linux/stacktrace.h>
39 #include <linux/debug_locks.h>
40 #include <linux/irqflags.h>
41 #include <linux/utsname.h>
42 #include <linux/hash.h>
43 #include <linux/ftrace.h>
44
45 #include <asm/sections.h>
46
47 #include "lockdep_internals.h"
48
49 #ifdef CONFIG_PROVE_LOCKING
50 int prove_locking = 1;
51 module_param(prove_locking, int, 0644);
52 #else
53 #define prove_locking 0
54 #endif
55
56 #ifdef CONFIG_LOCK_STAT
57 int lock_stat = 1;
58 module_param(lock_stat, int, 0644);
59 #else
60 #define lock_stat 0
61 #endif
62
63 /*
64  * lockdep_lock: protects the lockdep graph, the hashes and the
65  *               class/list/hash allocators.
66  *
67  * This is one of the rare exceptions where it's justified
68  * to use a raw spinlock - we really dont want the spinlock
69  * code to recurse back into the lockdep code...
70  */
71 static raw_spinlock_t lockdep_lock = (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED;
72
73 static int graph_lock(void)
74 {
75         __raw_spin_lock(&lockdep_lock);
76         /*
77          * Make sure that if another CPU detected a bug while
78          * walking the graph we dont change it (while the other
79          * CPU is busy printing out stuff with the graph lock
80          * dropped already)
81          */
82         if (!debug_locks) {
83                 __raw_spin_unlock(&lockdep_lock);
84                 return 0;
85         }
86         /* prevent any recursions within lockdep from causing deadlocks */
87         current->lockdep_recursion++;
88         return 1;
89 }
90
91 static inline int graph_unlock(void)
92 {
93         if (debug_locks && !__raw_spin_is_locked(&lockdep_lock))
94                 return DEBUG_LOCKS_WARN_ON(1);
95
96         current->lockdep_recursion--;
97         __raw_spin_unlock(&lockdep_lock);
98         return 0;
99 }
100
101 /*
102  * Turn lock debugging off and return with 0 if it was off already,
103  * and also release the graph lock:
104  */
105 static inline int debug_locks_off_graph_unlock(void)
106 {
107         int ret = debug_locks_off();
108
109         __raw_spin_unlock(&lockdep_lock);
110
111         return ret;
112 }
113
114 static int lockdep_initialized;
115
116 unsigned long nr_list_entries;
117 static struct lock_list list_entries[MAX_LOCKDEP_ENTRIES];
118
119 /*
120  * All data structures here are protected by the global debug_lock.
121  *
122  * Mutex key structs only get allocated, once during bootup, and never
123  * get freed - this significantly simplifies the debugging code.
124  */
125 unsigned long nr_lock_classes;
126 static struct lock_class lock_classes[MAX_LOCKDEP_KEYS];
127
128 static inline struct lock_class *hlock_class(struct held_lock *hlock)
129 {
130         if (!hlock->class_idx) {
131                 DEBUG_LOCKS_WARN_ON(1);
132                 return NULL;
133         }
134         return lock_classes + hlock->class_idx - 1;
135 }
136
137 #ifdef CONFIG_LOCK_STAT
138 static DEFINE_PER_CPU(struct lock_class_stats[MAX_LOCKDEP_KEYS], lock_stats);
139
140 static int lock_point(unsigned long points[], unsigned long ip)
141 {
142         int i;
143
144         for (i = 0; i < LOCKSTAT_POINTS; i++) {
145                 if (points[i] == 0) {
146                         points[i] = ip;
147                         break;
148                 }
149                 if (points[i] == ip)
150                         break;
151         }
152
153         return i;
154 }
155
156 static void lock_time_inc(struct lock_time *lt, s64 time)
157 {
158         if (time > lt->max)
159                 lt->max = time;
160
161         if (time < lt->min || !lt->min)
162                 lt->min = time;
163
164         lt->total += time;
165         lt->nr++;
166 }
167
168 static inline void lock_time_add(struct lock_time *src, struct lock_time *dst)
169 {
170         dst->min += src->min;
171         dst->max += src->max;
172         dst->total += src->total;
173         dst->nr += src->nr;
174 }
175
176 struct lock_class_stats lock_stats(struct lock_class *class)
177 {
178         struct lock_class_stats stats;
179         int cpu, i;
180
181         memset(&stats, 0, sizeof(struct lock_class_stats));
182         for_each_possible_cpu(cpu) {
183                 struct lock_class_stats *pcs =
184                         &per_cpu(lock_stats, cpu)[class - lock_classes];
185
186                 for (i = 0; i < ARRAY_SIZE(stats.contention_point); i++)
187                         stats.contention_point[i] += pcs->contention_point[i];
188
189                 for (i = 0; i < ARRAY_SIZE(stats.contending_point); i++)
190                         stats.contending_point[i] += pcs->contending_point[i];
191
192                 lock_time_add(&pcs->read_waittime, &stats.read_waittime);
193                 lock_time_add(&pcs->write_waittime, &stats.write_waittime);
194
195                 lock_time_add(&pcs->read_holdtime, &stats.read_holdtime);
196                 lock_time_add(&pcs->write_holdtime, &stats.write_holdtime);
197
198                 for (i = 0; i < ARRAY_SIZE(stats.bounces); i++)
199                         stats.bounces[i] += pcs->bounces[i];
200         }
201
202         return stats;
203 }
204
205 void clear_lock_stats(struct lock_class *class)
206 {
207         int cpu;
208
209         for_each_possible_cpu(cpu) {
210                 struct lock_class_stats *cpu_stats =
211                         &per_cpu(lock_stats, cpu)[class - lock_classes];
212
213                 memset(cpu_stats, 0, sizeof(struct lock_class_stats));
214         }
215         memset(class->contention_point, 0, sizeof(class->contention_point));
216         memset(class->contending_point, 0, sizeof(class->contending_point));
217 }
218
219 static struct lock_class_stats *get_lock_stats(struct lock_class *class)
220 {
221         return &get_cpu_var(lock_stats)[class - lock_classes];
222 }
223
224 static void put_lock_stats(struct lock_class_stats *stats)
225 {
226         put_cpu_var(lock_stats);
227 }
228
229 static void lock_release_holdtime(struct held_lock *hlock)
230 {
231         struct lock_class_stats *stats;
232         s64 holdtime;
233
234         if (!lock_stat)
235                 return;
236
237         holdtime = sched_clock() - hlock->holdtime_stamp;
238
239         stats = get_lock_stats(hlock_class(hlock));
240         if (hlock->read)
241                 lock_time_inc(&stats->read_holdtime, holdtime);
242         else
243                 lock_time_inc(&stats->write_holdtime, holdtime);
244         put_lock_stats(stats);
245 }
246 #else
247 static inline void lock_release_holdtime(struct held_lock *hlock)
248 {
249 }
250 #endif
251
252 /*
253  * We keep a global list of all lock classes. The list only grows,
254  * never shrinks. The list is only accessed with the lockdep
255  * spinlock lock held.
256  */
257 LIST_HEAD(all_lock_classes);
258
259 /*
260  * The lockdep classes are in a hash-table as well, for fast lookup:
261  */
262 #define CLASSHASH_BITS          (MAX_LOCKDEP_KEYS_BITS - 1)
263 #define CLASSHASH_SIZE          (1UL << CLASSHASH_BITS)
264 #define __classhashfn(key)      hash_long((unsigned long)key, CLASSHASH_BITS)
265 #define classhashentry(key)     (classhash_table + __classhashfn((key)))
266
267 static struct list_head classhash_table[CLASSHASH_SIZE];
268
269 /*
270  * We put the lock dependency chains into a hash-table as well, to cache
271  * their existence:
272  */
273 #define CHAINHASH_BITS          (MAX_LOCKDEP_CHAINS_BITS-1)
274 #define CHAINHASH_SIZE          (1UL << CHAINHASH_BITS)
275 #define __chainhashfn(chain)    hash_long(chain, CHAINHASH_BITS)
276 #define chainhashentry(chain)   (chainhash_table + __chainhashfn((chain)))
277
278 static struct list_head chainhash_table[CHAINHASH_SIZE];
279
280 /*
281  * The hash key of the lock dependency chains is a hash itself too:
282  * it's a hash of all locks taken up to that lock, including that lock.
283  * It's a 64-bit hash, because it's important for the keys to be
284  * unique.
285  */
286 #define iterate_chain_key(key1, key2) \
287         (((key1) << MAX_LOCKDEP_KEYS_BITS) ^ \
288         ((key1) >> (64-MAX_LOCKDEP_KEYS_BITS)) ^ \
289         (key2))
290
291 void lockdep_off(void)
292 {
293         current->lockdep_recursion++;
294 }
295 EXPORT_SYMBOL(lockdep_off);
296
297 void lockdep_on(void)
298 {
299         current->lockdep_recursion--;
300 }
301 EXPORT_SYMBOL(lockdep_on);
302
303 /*
304  * Debugging switches:
305  */
306
307 #define VERBOSE                 0
308 #define VERY_VERBOSE            0
309
310 #if VERBOSE
311 # define HARDIRQ_VERBOSE        1
312 # define SOFTIRQ_VERBOSE        1
313 # define RECLAIM_VERBOSE        1
314 #else
315 # define HARDIRQ_VERBOSE        0
316 # define SOFTIRQ_VERBOSE        0
317 # define RECLAIM_VERBOSE        0
318 #endif
319
320 #if VERBOSE || HARDIRQ_VERBOSE || SOFTIRQ_VERBOSE || RECLAIM_VERBOSE
321 /*
322  * Quick filtering for interesting events:
323  */
324 static int class_filter(struct lock_class *class)
325 {
326 #if 0
327         /* Example */
328         if (class->name_version == 1 &&
329                         !strcmp(class->name, "lockname"))
330                 return 1;
331         if (class->name_version == 1 &&
332                         !strcmp(class->name, "&struct->lockfield"))
333                 return 1;
334 #endif
335         /* Filter everything else. 1 would be to allow everything else */
336         return 0;
337 }
338 #endif
339
340 static int verbose(struct lock_class *class)
341 {
342 #if VERBOSE
343         return class_filter(class);
344 #endif
345         return 0;
346 }
347
348 /*
349  * Stack-trace: tightly packed array of stack backtrace
350  * addresses. Protected by the graph_lock.
351  */
352 unsigned long nr_stack_trace_entries;
353 static unsigned long stack_trace[MAX_STACK_TRACE_ENTRIES];
354
355 static int save_trace(struct stack_trace *trace)
356 {
357         trace->nr_entries = 0;
358         trace->max_entries = MAX_STACK_TRACE_ENTRIES - nr_stack_trace_entries;
359         trace->entries = stack_trace + nr_stack_trace_entries;
360
361         trace->skip = 3;
362
363         save_stack_trace(trace);
364
365         trace->max_entries = trace->nr_entries;
366
367         nr_stack_trace_entries += trace->nr_entries;
368
369         if (nr_stack_trace_entries == MAX_STACK_TRACE_ENTRIES) {
370                 if (!debug_locks_off_graph_unlock())
371                         return 0;
372
373                 printk("BUG: MAX_STACK_TRACE_ENTRIES too low!\n");
374                 printk("turning off the locking correctness validator.\n");
375                 dump_stack();
376
377                 return 0;
378         }
379
380         return 1;
381 }
382
383 unsigned int nr_hardirq_chains;
384 unsigned int nr_softirq_chains;
385 unsigned int nr_process_chains;
386 unsigned int max_lockdep_depth;
387 unsigned int max_recursion_depth;
388
389 static unsigned int lockdep_dependency_gen_id;
390
391 static bool lockdep_dependency_visit(struct lock_class *source,
392                                      unsigned int depth)
393 {
394         if (!depth)
395                 lockdep_dependency_gen_id++;
396         if (source->dep_gen_id == lockdep_dependency_gen_id)
397                 return true;
398         source->dep_gen_id = lockdep_dependency_gen_id;
399         return false;
400 }
401
402 #ifdef CONFIG_DEBUG_LOCKDEP
403 /*
404  * We cannot printk in early bootup code. Not even early_printk()
405  * might work. So we mark any initialization errors and printk
406  * about it later on, in lockdep_info().
407  */
408 static int lockdep_init_error;
409 static unsigned long lockdep_init_trace_data[20];
410 static struct stack_trace lockdep_init_trace = {
411         .max_entries = ARRAY_SIZE(lockdep_init_trace_data),
412         .entries = lockdep_init_trace_data,
413 };
414
415 /*
416  * Various lockdep statistics:
417  */
418 atomic_t chain_lookup_hits;
419 atomic_t chain_lookup_misses;
420 atomic_t hardirqs_on_events;
421 atomic_t hardirqs_off_events;
422 atomic_t redundant_hardirqs_on;
423 atomic_t redundant_hardirqs_off;
424 atomic_t softirqs_on_events;
425 atomic_t softirqs_off_events;
426 atomic_t redundant_softirqs_on;
427 atomic_t redundant_softirqs_off;
428 atomic_t nr_unused_locks;
429 atomic_t nr_cyclic_checks;
430 atomic_t nr_cyclic_check_recursions;
431 atomic_t nr_find_usage_forwards_checks;
432 atomic_t nr_find_usage_forwards_recursions;
433 atomic_t nr_find_usage_backwards_checks;
434 atomic_t nr_find_usage_backwards_recursions;
435 # define debug_atomic_inc(ptr)          atomic_inc(ptr)
436 # define debug_atomic_dec(ptr)          atomic_dec(ptr)
437 # define debug_atomic_read(ptr)         atomic_read(ptr)
438 #else
439 # define debug_atomic_inc(ptr)          do { } while (0)
440 # define debug_atomic_dec(ptr)          do { } while (0)
441 # define debug_atomic_read(ptr)         0
442 #endif
443
444 /*
445  * Locking printouts:
446  */
447
448 #define __STR(foo)      #foo
449 #define STR(foo)        __STR(foo)
450
451 #define __USAGE(__STATE)                                                \
452         [LOCK_USED_IN_##__STATE] = "IN-"STR(__STATE)"-W",               \
453         [LOCK_ENABLED_##__STATE] = STR(__STATE)"-ON-W",                 \
454         [LOCK_USED_IN_##__STATE##_READ] = "IN-"STR(__STATE)"-R",        \
455         [LOCK_ENABLED_##__STATE##_READ] = STR(__STATE)"-ON-R",
456
457 static const char *usage_str[] =
458 {
459 #define LOCKDEP_STATE(__STATE) __USAGE(__STATE)
460 #include "lockdep_states.h"
461 #undef LOCKDEP_STATE
462         [LOCK_USED] = "INITIAL USE",
463 };
464
465 const char * __get_key_name(struct lockdep_subclass_key *key, char *str)
466 {
467         return kallsyms_lookup((unsigned long)key, NULL, NULL, NULL, str);
468 }
469
470 static inline unsigned long lock_flag(enum lock_usage_bit bit)
471 {
472         return 1UL << bit;
473 }
474
475 static char get_usage_char(struct lock_class *class, enum lock_usage_bit bit)
476 {
477         char c = '.';
478
479         if (class->usage_mask & lock_flag(bit + 2))
480                 c = '+';
481         if (class->usage_mask & lock_flag(bit)) {
482                 c = '-';
483                 if (class->usage_mask & lock_flag(bit + 2))
484                         c = '?';
485         }
486
487         return c;
488 }
489
490 void
491 get_usage_chars(struct lock_class *class, char *c1, char *c2, char *c3,
492                                         char *c4, char *c5, char *c6)
493 {
494         *c1 = get_usage_char(class, LOCK_USED_IN_HARDIRQ);
495         *c2 = get_usage_char(class, LOCK_USED_IN_SOFTITQ);
496         *c3 = get_usage_char(class, LOCK_USED_IN_HARDIRQ_READ);
497         *c4 = get_usage_char(class, LOCK_USED_IN_SOFTITQ_READ);
498
499         *c5 = get_usage_char(class, LOCK_USED_IN_RECLAIM_FS);
500         *c6 = get_usage_char(class, LOCK_USED_IN_RECLAIM_FS_READ);
501 }
502
503 static void print_lock_name(struct lock_class *class)
504 {
505         char str[KSYM_NAME_LEN], c1, c2, c3, c4, c5, c6;
506         const char *name;
507
508         get_usage_chars(class, &c1, &c2, &c3, &c4, &c5, &c6);
509
510         name = class->name;
511         if (!name) {
512                 name = __get_key_name(class->key, str);
513                 printk(" (%s", name);
514         } else {
515                 printk(" (%s", name);
516                 if (class->name_version > 1)
517                         printk("#%d", class->name_version);
518                 if (class->subclass)
519                         printk("/%d", class->subclass);
520         }
521         printk("){%c%c%c%c%c%c}", c1, c2, c3, c4, c5, c6);
522 }
523
524 static void print_lockdep_cache(struct lockdep_map *lock)
525 {
526         const char *name;
527         char str[KSYM_NAME_LEN];
528
529         name = lock->name;
530         if (!name)
531                 name = __get_key_name(lock->key->subkeys, str);
532
533         printk("%s", name);
534 }
535
536 static void print_lock(struct held_lock *hlock)
537 {
538         print_lock_name(hlock_class(hlock));
539         printk(", at: ");
540         print_ip_sym(hlock->acquire_ip);
541 }
542
543 static void lockdep_print_held_locks(struct task_struct *curr)
544 {
545         int i, depth = curr->lockdep_depth;
546
547         if (!depth) {
548                 printk("no locks held by %s/%d.\n", curr->comm, task_pid_nr(curr));
549                 return;
550         }
551         printk("%d lock%s held by %s/%d:\n",
552                 depth, depth > 1 ? "s" : "", curr->comm, task_pid_nr(curr));
553
554         for (i = 0; i < depth; i++) {
555                 printk(" #%d: ", i);
556                 print_lock(curr->held_locks + i);
557         }
558 }
559
560 static void print_lock_class_header(struct lock_class *class, int depth)
561 {
562         int bit;
563
564         printk("%*s->", depth, "");
565         print_lock_name(class);
566         printk(" ops: %lu", class->ops);
567         printk(" {\n");
568
569         for (bit = 0; bit < LOCK_USAGE_STATES; bit++) {
570                 if (class->usage_mask & (1 << bit)) {
571                         int len = depth;
572
573                         len += printk("%*s   %s", depth, "", usage_str[bit]);
574                         len += printk(" at:\n");
575                         print_stack_trace(class->usage_traces + bit, len);
576                 }
577         }
578         printk("%*s }\n", depth, "");
579
580         printk("%*s ... key      at: ",depth,"");
581         print_ip_sym((unsigned long)class->key);
582 }
583
584 /*
585  * printk all lock dependencies starting at <entry>:
586  */
587 static void __used
588 print_lock_dependencies(struct lock_class *class, int depth)
589 {
590         struct lock_list *entry;
591
592         if (lockdep_dependency_visit(class, depth))
593                 return;
594
595         if (DEBUG_LOCKS_WARN_ON(depth >= 20))
596                 return;
597
598         print_lock_class_header(class, depth);
599
600         list_for_each_entry(entry, &class->locks_after, entry) {
601                 if (DEBUG_LOCKS_WARN_ON(!entry->class))
602                         return;
603
604                 print_lock_dependencies(entry->class, depth + 1);
605
606                 printk("%*s ... acquired at:\n",depth,"");
607                 print_stack_trace(&entry->trace, 2);
608                 printk("\n");
609         }
610 }
611
612 static void print_kernel_version(void)
613 {
614         printk("%s %.*s\n", init_utsname()->release,
615                 (int)strcspn(init_utsname()->version, " "),
616                 init_utsname()->version);
617 }
618
619 static int very_verbose(struct lock_class *class)
620 {
621 #if VERY_VERBOSE
622         return class_filter(class);
623 #endif
624         return 0;
625 }
626
627 /*
628  * Is this the address of a static object:
629  */
630 static int static_obj(void *obj)
631 {
632         unsigned long start = (unsigned long) &_stext,
633                       end   = (unsigned long) &_end,
634                       addr  = (unsigned long) obj;
635 #ifdef CONFIG_SMP
636         int i;
637 #endif
638
639         /*
640          * static variable?
641          */
642         if ((addr >= start) && (addr < end))
643                 return 1;
644
645 #ifdef CONFIG_SMP
646         /*
647          * percpu var?
648          */
649         for_each_possible_cpu(i) {
650                 start = (unsigned long) &__per_cpu_start + per_cpu_offset(i);
651                 end   = (unsigned long) &__per_cpu_start + PERCPU_ENOUGH_ROOM
652                                         + per_cpu_offset(i);
653
654                 if ((addr >= start) && (addr < end))
655                         return 1;
656         }
657 #endif
658
659         /*
660          * module var?
661          */
662         return is_module_address(addr);
663 }
664
665 /*
666  * To make lock name printouts unique, we calculate a unique
667  * class->name_version generation counter:
668  */
669 static int count_matching_names(struct lock_class *new_class)
670 {
671         struct lock_class *class;
672         int count = 0;
673
674         if (!new_class->name)
675                 return 0;
676
677         list_for_each_entry(class, &all_lock_classes, lock_entry) {
678                 if (new_class->key - new_class->subclass == class->key)
679                         return class->name_version;
680                 if (class->name && !strcmp(class->name, new_class->name))
681                         count = max(count, class->name_version);
682         }
683
684         return count + 1;
685 }
686
687 /*
688  * Register a lock's class in the hash-table, if the class is not present
689  * yet. Otherwise we look it up. We cache the result in the lock object
690  * itself, so actual lookup of the hash should be once per lock object.
691  */
692 static inline struct lock_class *
693 look_up_lock_class(struct lockdep_map *lock, unsigned int subclass)
694 {
695         struct lockdep_subclass_key *key;
696         struct list_head *hash_head;
697         struct lock_class *class;
698
699 #ifdef CONFIG_DEBUG_LOCKDEP
700         /*
701          * If the architecture calls into lockdep before initializing
702          * the hashes then we'll warn about it later. (we cannot printk
703          * right now)
704          */
705         if (unlikely(!lockdep_initialized)) {
706                 lockdep_init();
707                 lockdep_init_error = 1;
708                 save_stack_trace(&lockdep_init_trace);
709         }
710 #endif
711
712         /*
713          * Static locks do not have their class-keys yet - for them the key
714          * is the lock object itself:
715          */
716         if (unlikely(!lock->key))
717                 lock->key = (void *)lock;
718
719         /*
720          * NOTE: the class-key must be unique. For dynamic locks, a static
721          * lock_class_key variable is passed in through the mutex_init()
722          * (or spin_lock_init()) call - which acts as the key. For static
723          * locks we use the lock object itself as the key.
724          */
725         BUILD_BUG_ON(sizeof(struct lock_class_key) >
726                         sizeof(struct lockdep_map));
727
728         key = lock->key->subkeys + subclass;
729
730         hash_head = classhashentry(key);
731
732         /*
733          * We can walk the hash lockfree, because the hash only
734          * grows, and we are careful when adding entries to the end:
735          */
736         list_for_each_entry(class, hash_head, hash_entry) {
737                 if (class->key == key) {
738                         WARN_ON_ONCE(class->name != lock->name);
739                         return class;
740                 }
741         }
742
743         return NULL;
744 }
745
746 /*
747  * Register a lock's class in the hash-table, if the class is not present
748  * yet. Otherwise we look it up. We cache the result in the lock object
749  * itself, so actual lookup of the hash should be once per lock object.
750  */
751 static inline struct lock_class *
752 register_lock_class(struct lockdep_map *lock, unsigned int subclass, int force)
753 {
754         struct lockdep_subclass_key *key;
755         struct list_head *hash_head;
756         struct lock_class *class;
757         unsigned long flags;
758
759         class = look_up_lock_class(lock, subclass);
760         if (likely(class))
761                 return class;
762
763         /*
764          * Debug-check: all keys must be persistent!
765          */
766         if (!static_obj(lock->key)) {
767                 debug_locks_off();
768                 printk("INFO: trying to register non-static key.\n");
769                 printk("the code is fine but needs lockdep annotation.\n");
770                 printk("turning off the locking correctness validator.\n");
771                 dump_stack();
772
773                 return NULL;
774         }
775
776         key = lock->key->subkeys + subclass;
777         hash_head = classhashentry(key);
778
779         raw_local_irq_save(flags);
780         if (!graph_lock()) {
781                 raw_local_irq_restore(flags);
782                 return NULL;
783         }
784         /*
785          * We have to do the hash-walk again, to avoid races
786          * with another CPU:
787          */
788         list_for_each_entry(class, hash_head, hash_entry)
789                 if (class->key == key)
790                         goto out_unlock_set;
791         /*
792          * Allocate a new key from the static array, and add it to
793          * the hash:
794          */
795         if (nr_lock_classes >= MAX_LOCKDEP_KEYS) {
796                 if (!debug_locks_off_graph_unlock()) {
797                         raw_local_irq_restore(flags);
798                         return NULL;
799                 }
800                 raw_local_irq_restore(flags);
801
802                 printk("BUG: MAX_LOCKDEP_KEYS too low!\n");
803                 printk("turning off the locking correctness validator.\n");
804                 return NULL;
805         }
806         class = lock_classes + nr_lock_classes++;
807         debug_atomic_inc(&nr_unused_locks);
808         class->key = key;
809         class->name = lock->name;
810         class->subclass = subclass;
811         INIT_LIST_HEAD(&class->lock_entry);
812         INIT_LIST_HEAD(&class->locks_before);
813         INIT_LIST_HEAD(&class->locks_after);
814         class->name_version = count_matching_names(class);
815         /*
816          * We use RCU's safe list-add method to make
817          * parallel walking of the hash-list safe:
818          */
819         list_add_tail_rcu(&class->hash_entry, hash_head);
820         /*
821          * Add it to the global list of classes:
822          */
823         list_add_tail_rcu(&class->lock_entry, &all_lock_classes);
824
825         if (verbose(class)) {
826                 graph_unlock();
827                 raw_local_irq_restore(flags);
828
829                 printk("\nnew class %p: %s", class->key, class->name);
830                 if (class->name_version > 1)
831                         printk("#%d", class->name_version);
832                 printk("\n");
833                 dump_stack();
834
835                 raw_local_irq_save(flags);
836                 if (!graph_lock()) {
837                         raw_local_irq_restore(flags);
838                         return NULL;
839                 }
840         }
841 out_unlock_set:
842         graph_unlock();
843         raw_local_irq_restore(flags);
844
845         if (!subclass || force)
846                 lock->class_cache = class;
847
848         if (DEBUG_LOCKS_WARN_ON(class->subclass != subclass))
849                 return NULL;
850
851         return class;
852 }
853
854 #ifdef CONFIG_PROVE_LOCKING
855 /*
856  * Allocate a lockdep entry. (assumes the graph_lock held, returns
857  * with NULL on failure)
858  */
859 static struct lock_list *alloc_list_entry(void)
860 {
861         if (nr_list_entries >= MAX_LOCKDEP_ENTRIES) {
862                 if (!debug_locks_off_graph_unlock())
863                         return NULL;
864
865                 printk("BUG: MAX_LOCKDEP_ENTRIES too low!\n");
866                 printk("turning off the locking correctness validator.\n");
867                 return NULL;
868         }
869         return list_entries + nr_list_entries++;
870 }
871
872 /*
873  * Add a new dependency to the head of the list:
874  */
875 static int add_lock_to_list(struct lock_class *class, struct lock_class *this,
876                             struct list_head *head, unsigned long ip, int distance)
877 {
878         struct lock_list *entry;
879         /*
880          * Lock not present yet - get a new dependency struct and
881          * add it to the list:
882          */
883         entry = alloc_list_entry();
884         if (!entry)
885                 return 0;
886
887         if (!save_trace(&entry->trace))
888                 return 0;
889
890         entry->class = this;
891         entry->distance = distance;
892         /*
893          * Since we never remove from the dependency list, the list can
894          * be walked lockless by other CPUs, it's only allocation
895          * that must be protected by the spinlock. But this also means
896          * we must make new entries visible only once writes to the
897          * entry become visible - hence the RCU op:
898          */
899         list_add_tail_rcu(&entry->entry, head);
900
901         return 1;
902 }
903
904 /*
905  * Recursive, forwards-direction lock-dependency checking, used for
906  * both noncyclic checking and for hardirq-unsafe/softirq-unsafe
907  * checking.
908  *
909  * (to keep the stackframe of the recursive functions small we
910  *  use these global variables, and we also mark various helper
911  *  functions as noinline.)
912  */
913 static struct held_lock *check_source, *check_target;
914
915 /*
916  * Print a dependency chain entry (this is only done when a deadlock
917  * has been detected):
918  */
919 static noinline int
920 print_circular_bug_entry(struct lock_list *target, unsigned int depth)
921 {
922         if (debug_locks_silent)
923                 return 0;
924         printk("\n-> #%u", depth);
925         print_lock_name(target->class);
926         printk(":\n");
927         print_stack_trace(&target->trace, 6);
928
929         return 0;
930 }
931
932 /*
933  * When a circular dependency is detected, print the
934  * header first:
935  */
936 static noinline int
937 print_circular_bug_header(struct lock_list *entry, unsigned int depth)
938 {
939         struct task_struct *curr = current;
940
941         if (!debug_locks_off_graph_unlock() || debug_locks_silent)
942                 return 0;
943
944         printk("\n=======================================================\n");
945         printk(  "[ INFO: possible circular locking dependency detected ]\n");
946         print_kernel_version();
947         printk(  "-------------------------------------------------------\n");
948         printk("%s/%d is trying to acquire lock:\n",
949                 curr->comm, task_pid_nr(curr));
950         print_lock(check_source);
951         printk("\nbut task is already holding lock:\n");
952         print_lock(check_target);
953         printk("\nwhich lock already depends on the new lock.\n\n");
954         printk("\nthe existing dependency chain (in reverse order) is:\n");
955
956         print_circular_bug_entry(entry, depth);
957
958         return 0;
959 }
960
961 static noinline int print_circular_bug_tail(void)
962 {
963         struct task_struct *curr = current;
964         struct lock_list this;
965
966         if (debug_locks_silent)
967                 return 0;
968
969         this.class = hlock_class(check_source);
970         if (!save_trace(&this.trace))
971                 return 0;
972
973         print_circular_bug_entry(&this, 0);
974
975         printk("\nother info that might help us debug this:\n\n");
976         lockdep_print_held_locks(curr);
977
978         printk("\nstack backtrace:\n");
979         dump_stack();
980
981         return 0;
982 }
983
984 #define RECURSION_LIMIT 40
985
986 static int noinline print_infinite_recursion_bug(void)
987 {
988         if (!debug_locks_off_graph_unlock())
989                 return 0;
990
991         WARN_ON(1);
992
993         return 0;
994 }
995
996 unsigned long __lockdep_count_forward_deps(struct lock_class *class,
997                                            unsigned int depth)
998 {
999         struct lock_list *entry;
1000         unsigned long ret = 1;
1001
1002         if (lockdep_dependency_visit(class, depth))
1003                 return 0;
1004
1005         /*
1006          * Recurse this class's dependency list:
1007          */
1008         list_for_each_entry(entry, &class->locks_after, entry)
1009                 ret += __lockdep_count_forward_deps(entry->class, depth + 1);
1010
1011         return ret;
1012 }
1013
1014 unsigned long lockdep_count_forward_deps(struct lock_class *class)
1015 {
1016         unsigned long ret, flags;
1017
1018         local_irq_save(flags);
1019         __raw_spin_lock(&lockdep_lock);
1020         ret = __lockdep_count_forward_deps(class, 0);
1021         __raw_spin_unlock(&lockdep_lock);
1022         local_irq_restore(flags);
1023
1024         return ret;
1025 }
1026
1027 unsigned long __lockdep_count_backward_deps(struct lock_class *class,
1028                                             unsigned int depth)
1029 {
1030         struct lock_list *entry;
1031         unsigned long ret = 1;
1032
1033         if (lockdep_dependency_visit(class, depth))
1034                 return 0;
1035         /*
1036          * Recurse this class's dependency list:
1037          */
1038         list_for_each_entry(entry, &class->locks_before, entry)
1039                 ret += __lockdep_count_backward_deps(entry->class, depth + 1);
1040
1041         return ret;
1042 }
1043
1044 unsigned long lockdep_count_backward_deps(struct lock_class *class)
1045 {
1046         unsigned long ret, flags;
1047
1048         local_irq_save(flags);
1049         __raw_spin_lock(&lockdep_lock);
1050         ret = __lockdep_count_backward_deps(class, 0);
1051         __raw_spin_unlock(&lockdep_lock);
1052         local_irq_restore(flags);
1053
1054         return ret;
1055 }
1056
1057 /*
1058  * Prove that the dependency graph starting at <entry> can not
1059  * lead to <target>. Print an error and return 0 if it does.
1060  */
1061 static noinline int
1062 check_noncircular(struct lock_class *source, unsigned int depth)
1063 {
1064         struct lock_list *entry;
1065
1066         if (lockdep_dependency_visit(source, depth))
1067                 return 1;
1068
1069         debug_atomic_inc(&nr_cyclic_check_recursions);
1070         if (depth > max_recursion_depth)
1071                 max_recursion_depth = depth;
1072         if (depth >= RECURSION_LIMIT)
1073                 return print_infinite_recursion_bug();
1074         /*
1075          * Check this lock's dependency list:
1076          */
1077         list_for_each_entry(entry, &source->locks_after, entry) {
1078                 if (entry->class == hlock_class(check_target))
1079                         return print_circular_bug_header(entry, depth+1);
1080                 debug_atomic_inc(&nr_cyclic_checks);
1081                 if (!check_noncircular(entry->class, depth+1))
1082                         return print_circular_bug_entry(entry, depth+1);
1083         }
1084         return 1;
1085 }
1086
1087 #if defined(CONFIG_TRACE_IRQFLAGS) && defined(CONFIG_PROVE_LOCKING)
1088 /*
1089  * Forwards and backwards subgraph searching, for the purposes of
1090  * proving that two subgraphs can be connected by a new dependency
1091  * without creating any illegal irq-safe -> irq-unsafe lock dependency.
1092  */
1093 static enum lock_usage_bit find_usage_bit;
1094 static struct lock_class *forwards_match, *backwards_match;
1095
1096 /*
1097  * Find a node in the forwards-direction dependency sub-graph starting
1098  * at <source> that matches <find_usage_bit>.
1099  *
1100  * Return 2 if such a node exists in the subgraph, and put that node
1101  * into <forwards_match>.
1102  *
1103  * Return 1 otherwise and keep <forwards_match> unchanged.
1104  * Return 0 on error.
1105  */
1106 static noinline int
1107 find_usage_forwards(struct lock_class *source, unsigned int depth)
1108 {
1109         struct lock_list *entry;
1110         int ret;
1111
1112         if (lockdep_dependency_visit(source, depth))
1113                 return 1;
1114
1115         if (depth > max_recursion_depth)
1116                 max_recursion_depth = depth;
1117         if (depth >= RECURSION_LIMIT)
1118                 return print_infinite_recursion_bug();
1119
1120         debug_atomic_inc(&nr_find_usage_forwards_checks);
1121         if (source->usage_mask & (1 << find_usage_bit)) {
1122                 forwards_match = source;
1123                 return 2;
1124         }
1125
1126         /*
1127          * Check this lock's dependency list:
1128          */
1129         list_for_each_entry(entry, &source->locks_after, entry) {
1130                 debug_atomic_inc(&nr_find_usage_forwards_recursions);
1131                 ret = find_usage_forwards(entry->class, depth+1);
1132                 if (ret == 2 || ret == 0)
1133                         return ret;
1134         }
1135         return 1;
1136 }
1137
1138 /*
1139  * Find a node in the backwards-direction dependency sub-graph starting
1140  * at <source> that matches <find_usage_bit>.
1141  *
1142  * Return 2 if such a node exists in the subgraph, and put that node
1143  * into <backwards_match>.
1144  *
1145  * Return 1 otherwise and keep <backwards_match> unchanged.
1146  * Return 0 on error.
1147  */
1148 static noinline int
1149 find_usage_backwards(struct lock_class *source, unsigned int depth)
1150 {
1151         struct lock_list *entry;
1152         int ret;
1153
1154         if (lockdep_dependency_visit(source, depth))
1155                 return 1;
1156
1157         if (!__raw_spin_is_locked(&lockdep_lock))
1158                 return DEBUG_LOCKS_WARN_ON(1);
1159
1160         if (depth > max_recursion_depth)
1161                 max_recursion_depth = depth;
1162         if (depth >= RECURSION_LIMIT)
1163                 return print_infinite_recursion_bug();
1164
1165         debug_atomic_inc(&nr_find_usage_backwards_checks);
1166         if (source->usage_mask & (1 << find_usage_bit)) {
1167                 backwards_match = source;
1168                 return 2;
1169         }
1170
1171         if (!source && debug_locks_off_graph_unlock()) {
1172                 WARN_ON(1);
1173                 return 0;
1174         }
1175
1176         /*
1177          * Check this lock's dependency list:
1178          */
1179         list_for_each_entry(entry, &source->locks_before, entry) {
1180                 debug_atomic_inc(&nr_find_usage_backwards_recursions);
1181                 ret = find_usage_backwards(entry->class, depth+1);
1182                 if (ret == 2 || ret == 0)
1183                         return ret;
1184         }
1185         return 1;
1186 }
1187
1188 static int
1189 print_bad_irq_dependency(struct task_struct *curr,
1190                          struct held_lock *prev,
1191                          struct held_lock *next,
1192                          enum lock_usage_bit bit1,
1193                          enum lock_usage_bit bit2,
1194                          const char *irqclass)
1195 {
1196         if (!debug_locks_off_graph_unlock() || debug_locks_silent)
1197                 return 0;
1198
1199         printk("\n======================================================\n");
1200         printk(  "[ INFO: %s-safe -> %s-unsafe lock order detected ]\n",
1201                 irqclass, irqclass);
1202         print_kernel_version();
1203         printk(  "------------------------------------------------------\n");
1204         printk("%s/%d [HC%u[%lu]:SC%u[%lu]:HE%u:SE%u] is trying to acquire:\n",
1205                 curr->comm, task_pid_nr(curr),
1206                 curr->hardirq_context, hardirq_count() >> HARDIRQ_SHIFT,
1207                 curr->softirq_context, softirq_count() >> SOFTIRQ_SHIFT,
1208                 curr->hardirqs_enabled,
1209                 curr->softirqs_enabled);
1210         print_lock(next);
1211
1212         printk("\nand this task is already holding:\n");
1213         print_lock(prev);
1214         printk("which would create a new lock dependency:\n");
1215         print_lock_name(hlock_class(prev));
1216         printk(" ->");
1217         print_lock_name(hlock_class(next));
1218         printk("\n");
1219
1220         printk("\nbut this new dependency connects a %s-irq-safe lock:\n",
1221                 irqclass);
1222         print_lock_name(backwards_match);
1223         printk("\n... which became %s-irq-safe at:\n", irqclass);
1224
1225         print_stack_trace(backwards_match->usage_traces + bit1, 1);
1226
1227         printk("\nto a %s-irq-unsafe lock:\n", irqclass);
1228         print_lock_name(forwards_match);
1229         printk("\n... which became %s-irq-unsafe at:\n", irqclass);
1230         printk("...");
1231
1232         print_stack_trace(forwards_match->usage_traces + bit2, 1);
1233
1234         printk("\nother info that might help us debug this:\n\n");
1235         lockdep_print_held_locks(curr);
1236
1237         printk("\nthe %s-irq-safe lock's dependencies:\n", irqclass);
1238         print_lock_dependencies(backwards_match, 0);
1239
1240         printk("\nthe %s-irq-unsafe lock's dependencies:\n", irqclass);
1241         print_lock_dependencies(forwards_match, 0);
1242
1243         printk("\nstack backtrace:\n");
1244         dump_stack();
1245
1246         return 0;
1247 }
1248
1249 static int
1250 check_usage(struct task_struct *curr, struct held_lock *prev,
1251             struct held_lock *next, enum lock_usage_bit bit_backwards,
1252             enum lock_usage_bit bit_forwards, const char *irqclass)
1253 {
1254         int ret;
1255
1256         find_usage_bit = bit_backwards;
1257         /* fills in <backwards_match> */
1258         ret = find_usage_backwards(hlock_class(prev), 0);
1259         if (!ret || ret == 1)
1260                 return ret;
1261
1262         find_usage_bit = bit_forwards;
1263         ret = find_usage_forwards(hlock_class(next), 0);
1264         if (!ret || ret == 1)
1265                 return ret;
1266         /* ret == 2 */
1267         return print_bad_irq_dependency(curr, prev, next,
1268                         bit_backwards, bit_forwards, irqclass);
1269 }
1270
1271 static int
1272 check_prev_add_irq(struct task_struct *curr, struct held_lock *prev,
1273                 struct held_lock *next)
1274 {
1275         /*
1276          * Prove that the new dependency does not connect a hardirq-safe
1277          * lock with a hardirq-unsafe lock - to achieve this we search
1278          * the backwards-subgraph starting at <prev>, and the
1279          * forwards-subgraph starting at <next>:
1280          */
1281         if (!check_usage(curr, prev, next, LOCK_USED_IN_HARDIRQ,
1282                                         LOCK_ENABLED_HARDIRQ, "hard"))
1283                 return 0;
1284
1285         /*
1286          * Prove that the new dependency does not connect a hardirq-safe-read
1287          * lock with a hardirq-unsafe lock - to achieve this we search
1288          * the backwards-subgraph starting at <prev>, and the
1289          * forwards-subgraph starting at <next>:
1290          */
1291         if (!check_usage(curr, prev, next, LOCK_USED_IN_HARDIRQ_READ,
1292                                         LOCK_ENABLED_HARDIRQ, "hard-read"))
1293                 return 0;
1294
1295         /*
1296          * Prove that the new dependency does not connect a softirq-safe
1297          * lock with a softirq-unsafe lock - to achieve this we search
1298          * the backwards-subgraph starting at <prev>, and the
1299          * forwards-subgraph starting at <next>:
1300          */
1301         if (!check_usage(curr, prev, next, LOCK_USED_IN_SOFTIRQ,
1302                                         LOCK_ENABLED_SOFTIRQ, "soft"))
1303                 return 0;
1304         /*
1305          * Prove that the new dependency does not connect a softirq-safe-read
1306          * lock with a softirq-unsafe lock - to achieve this we search
1307          * the backwards-subgraph starting at <prev>, and the
1308          * forwards-subgraph starting at <next>:
1309          */
1310         if (!check_usage(curr, prev, next, LOCK_USED_IN_SOFTIRQ_READ,
1311                                         LOCK_ENABLED_SOFTIRQ, "soft"))
1312                 return 0;
1313
1314         /*
1315          * Prove that the new dependency does not connect a reclaim-fs-safe
1316          * lock with a reclaim-fs-unsafe lock - to achieve this we search
1317          * the backwards-subgraph starting at <prev>, and the
1318          * forwards-subgraph starting at <next>:
1319          */
1320         if (!check_usage(curr, prev, next, LOCK_USED_IN_RECLAIM_FS,
1321                                         LOCK_ENABLED_RECLAIM_FS, "reclaim-fs"))
1322                 return 0;
1323
1324         /*
1325          * Prove that the new dependency does not connect a reclaim-fs-safe-read
1326          * lock with a reclaim-fs-unsafe lock - to achieve this we search
1327          * the backwards-subgraph starting at <prev>, and the
1328          * forwards-subgraph starting at <next>:
1329          */
1330         if (!check_usage(curr, prev, next, LOCK_USED_IN_RECLAIM_FS_READ,
1331                                         LOCK_ENABLED_RECLAIM_FS, "reclaim-fs-read"))
1332                 return 0;
1333
1334         return 1;
1335 }
1336
1337 static void inc_chains(void)
1338 {
1339         if (current->hardirq_context)
1340                 nr_hardirq_chains++;
1341         else {
1342                 if (current->softirq_context)
1343                         nr_softirq_chains++;
1344                 else
1345                         nr_process_chains++;
1346         }
1347 }
1348
1349 #else
1350
1351 static inline int
1352 check_prev_add_irq(struct task_struct *curr, struct held_lock *prev,
1353                 struct held_lock *next)
1354 {
1355         return 1;
1356 }
1357
1358 static inline void inc_chains(void)
1359 {
1360         nr_process_chains++;
1361 }
1362
1363 #endif
1364
1365 static int
1366 print_deadlock_bug(struct task_struct *curr, struct held_lock *prev,
1367                    struct held_lock *next)
1368 {
1369         if (!debug_locks_off_graph_unlock() || debug_locks_silent)
1370                 return 0;
1371
1372         printk("\n=============================================\n");
1373         printk(  "[ INFO: possible recursive locking detected ]\n");
1374         print_kernel_version();
1375         printk(  "---------------------------------------------\n");
1376         printk("%s/%d is trying to acquire lock:\n",
1377                 curr->comm, task_pid_nr(curr));
1378         print_lock(next);
1379         printk("\nbut task is already holding lock:\n");
1380         print_lock(prev);
1381
1382         printk("\nother info that might help us debug this:\n");
1383         lockdep_print_held_locks(curr);
1384
1385         printk("\nstack backtrace:\n");
1386         dump_stack();
1387
1388         return 0;
1389 }
1390
1391 /*
1392  * Check whether we are holding such a class already.
1393  *
1394  * (Note that this has to be done separately, because the graph cannot
1395  * detect such classes of deadlocks.)
1396  *
1397  * Returns: 0 on deadlock detected, 1 on OK, 2 on recursive read
1398  */
1399 static int
1400 check_deadlock(struct task_struct *curr, struct held_lock *next,
1401                struct lockdep_map *next_instance, int read)
1402 {
1403         struct held_lock *prev;
1404         struct held_lock *nest = NULL;
1405         int i;
1406
1407         for (i = 0; i < curr->lockdep_depth; i++) {
1408                 prev = curr->held_locks + i;
1409
1410                 if (prev->instance == next->nest_lock)
1411                         nest = prev;
1412
1413                 if (hlock_class(prev) != hlock_class(next))
1414                         continue;
1415
1416                 /*
1417                  * Allow read-after-read recursion of the same
1418                  * lock class (i.e. read_lock(lock)+read_lock(lock)):
1419                  */
1420                 if ((read == 2) && prev->read)
1421                         return 2;
1422
1423                 /*
1424                  * We're holding the nest_lock, which serializes this lock's
1425                  * nesting behaviour.
1426                  */
1427                 if (nest)
1428                         return 2;
1429
1430                 return print_deadlock_bug(curr, prev, next);
1431         }
1432         return 1;
1433 }
1434
1435 /*
1436  * There was a chain-cache miss, and we are about to add a new dependency
1437  * to a previous lock. We recursively validate the following rules:
1438  *
1439  *  - would the adding of the <prev> -> <next> dependency create a
1440  *    circular dependency in the graph? [== circular deadlock]
1441  *
1442  *  - does the new prev->next dependency connect any hardirq-safe lock
1443  *    (in the full backwards-subgraph starting at <prev>) with any
1444  *    hardirq-unsafe lock (in the full forwards-subgraph starting at
1445  *    <next>)? [== illegal lock inversion with hardirq contexts]
1446  *
1447  *  - does the new prev->next dependency connect any softirq-safe lock
1448  *    (in the full backwards-subgraph starting at <prev>) with any
1449  *    softirq-unsafe lock (in the full forwards-subgraph starting at
1450  *    <next>)? [== illegal lock inversion with softirq contexts]
1451  *
1452  * any of these scenarios could lead to a deadlock.
1453  *
1454  * Then if all the validations pass, we add the forwards and backwards
1455  * dependency.
1456  */
1457 static int
1458 check_prev_add(struct task_struct *curr, struct held_lock *prev,
1459                struct held_lock *next, int distance)
1460 {
1461         struct lock_list *entry;
1462         int ret;
1463
1464         /*
1465          * Prove that the new <prev> -> <next> dependency would not
1466          * create a circular dependency in the graph. (We do this by
1467          * forward-recursing into the graph starting at <next>, and
1468          * checking whether we can reach <prev>.)
1469          *
1470          * We are using global variables to control the recursion, to
1471          * keep the stackframe size of the recursive functions low:
1472          */
1473         check_source = next;
1474         check_target = prev;
1475         if (!(check_noncircular(hlock_class(next), 0)))
1476                 return print_circular_bug_tail();
1477
1478         if (!check_prev_add_irq(curr, prev, next))
1479                 return 0;
1480
1481         /*
1482          * For recursive read-locks we do all the dependency checks,
1483          * but we dont store read-triggered dependencies (only
1484          * write-triggered dependencies). This ensures that only the
1485          * write-side dependencies matter, and that if for example a
1486          * write-lock never takes any other locks, then the reads are
1487          * equivalent to a NOP.
1488          */
1489         if (next->read == 2 || prev->read == 2)
1490                 return 1;
1491         /*
1492          * Is the <prev> -> <next> dependency already present?
1493          *
1494          * (this may occur even though this is a new chain: consider
1495          *  e.g. the L1 -> L2 -> L3 -> L4 and the L5 -> L1 -> L2 -> L3
1496          *  chains - the second one will be new, but L1 already has
1497          *  L2 added to its dependency list, due to the first chain.)
1498          */
1499         list_for_each_entry(entry, &hlock_class(prev)->locks_after, entry) {
1500                 if (entry->class == hlock_class(next)) {
1501                         if (distance == 1)
1502                                 entry->distance = 1;
1503                         return 2;
1504                 }
1505         }
1506
1507         /*
1508          * Ok, all validations passed, add the new lock
1509          * to the previous lock's dependency list:
1510          */
1511         ret = add_lock_to_list(hlock_class(prev), hlock_class(next),
1512                                &hlock_class(prev)->locks_after,
1513                                next->acquire_ip, distance);
1514
1515         if (!ret)
1516                 return 0;
1517
1518         ret = add_lock_to_list(hlock_class(next), hlock_class(prev),
1519                                &hlock_class(next)->locks_before,
1520                                next->acquire_ip, distance);
1521         if (!ret)
1522                 return 0;
1523
1524         /*
1525          * Debugging printouts:
1526          */
1527         if (verbose(hlock_class(prev)) || verbose(hlock_class(next))) {
1528                 graph_unlock();
1529                 printk("\n new dependency: ");
1530                 print_lock_name(hlock_class(prev));
1531                 printk(" => ");
1532                 print_lock_name(hlock_class(next));
1533                 printk("\n");
1534                 dump_stack();
1535                 return graph_lock();
1536         }
1537         return 1;
1538 }
1539
1540 /*
1541  * Add the dependency to all directly-previous locks that are 'relevant'.
1542  * The ones that are relevant are (in increasing distance from curr):
1543  * all consecutive trylock entries and the final non-trylock entry - or
1544  * the end of this context's lock-chain - whichever comes first.
1545  */
1546 static int
1547 check_prevs_add(struct task_struct *curr, struct held_lock *next)
1548 {
1549         int depth = curr->lockdep_depth;
1550         struct held_lock *hlock;
1551
1552         /*
1553          * Debugging checks.
1554          *
1555          * Depth must not be zero for a non-head lock:
1556          */
1557         if (!depth)
1558                 goto out_bug;
1559         /*
1560          * At least two relevant locks must exist for this
1561          * to be a head:
1562          */
1563         if (curr->held_locks[depth].irq_context !=
1564                         curr->held_locks[depth-1].irq_context)
1565                 goto out_bug;
1566
1567         for (;;) {
1568                 int distance = curr->lockdep_depth - depth + 1;
1569                 hlock = curr->held_locks + depth-1;
1570                 /*
1571                  * Only non-recursive-read entries get new dependencies
1572                  * added:
1573                  */
1574                 if (hlock->read != 2) {
1575                         if (!check_prev_add(curr, hlock, next, distance))
1576                                 return 0;
1577                         /*
1578                          * Stop after the first non-trylock entry,
1579                          * as non-trylock entries have added their
1580                          * own direct dependencies already, so this
1581                          * lock is connected to them indirectly:
1582                          */
1583                         if (!hlock->trylock)
1584                                 break;
1585                 }
1586                 depth--;
1587                 /*
1588                  * End of lock-stack?
1589                  */
1590                 if (!depth)
1591                         break;
1592                 /*
1593                  * Stop the search if we cross into another context:
1594                  */
1595                 if (curr->held_locks[depth].irq_context !=
1596                                 curr->held_locks[depth-1].irq_context)
1597                         break;
1598         }
1599         return 1;
1600 out_bug:
1601         if (!debug_locks_off_graph_unlock())
1602                 return 0;
1603
1604         WARN_ON(1);
1605
1606         return 0;
1607 }
1608
1609 unsigned long nr_lock_chains;
1610 struct lock_chain lock_chains[MAX_LOCKDEP_CHAINS];
1611 int nr_chain_hlocks;
1612 static u16 chain_hlocks[MAX_LOCKDEP_CHAIN_HLOCKS];
1613
1614 struct lock_class *lock_chain_get_class(struct lock_chain *chain, int i)
1615 {
1616         return lock_classes + chain_hlocks[chain->base + i];
1617 }
1618
1619 /*
1620  * Look up a dependency chain. If the key is not present yet then
1621  * add it and return 1 - in this case the new dependency chain is
1622  * validated. If the key is already hashed, return 0.
1623  * (On return with 1 graph_lock is held.)
1624  */
1625 static inline int lookup_chain_cache(struct task_struct *curr,
1626                                      struct held_lock *hlock,
1627                                      u64 chain_key)
1628 {
1629         struct lock_class *class = hlock_class(hlock);
1630         struct list_head *hash_head = chainhashentry(chain_key);
1631         struct lock_chain *chain;
1632         struct held_lock *hlock_curr, *hlock_next;
1633         int i, j, n, cn;
1634
1635         if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
1636                 return 0;
1637         /*
1638          * We can walk it lock-free, because entries only get added
1639          * to the hash:
1640          */
1641         list_for_each_entry(chain, hash_head, entry) {
1642                 if (chain->chain_key == chain_key) {
1643 cache_hit:
1644                         debug_atomic_inc(&chain_lookup_hits);
1645                         if (very_verbose(class))
1646                                 printk("\nhash chain already cached, key: "
1647                                         "%016Lx tail class: [%p] %s\n",
1648                                         (unsigned long long)chain_key,
1649                                         class->key, class->name);
1650                         return 0;
1651                 }
1652         }
1653         if (very_verbose(class))
1654                 printk("\nnew hash chain, key: %016Lx tail class: [%p] %s\n",
1655                         (unsigned long long)chain_key, class->key, class->name);
1656         /*
1657          * Allocate a new chain entry from the static array, and add
1658          * it to the hash:
1659          */
1660         if (!graph_lock())
1661                 return 0;
1662         /*
1663          * We have to walk the chain again locked - to avoid duplicates:
1664          */
1665         list_for_each_entry(chain, hash_head, entry) {
1666                 if (chain->chain_key == chain_key) {
1667                         graph_unlock();
1668                         goto cache_hit;
1669                 }
1670         }
1671         if (unlikely(nr_lock_chains >= MAX_LOCKDEP_CHAINS)) {
1672                 if (!debug_locks_off_graph_unlock())
1673                         return 0;
1674
1675                 printk("BUG: MAX_LOCKDEP_CHAINS too low!\n");
1676                 printk("turning off the locking correctness validator.\n");
1677                 return 0;
1678         }
1679         chain = lock_chains + nr_lock_chains++;
1680         chain->chain_key = chain_key;
1681         chain->irq_context = hlock->irq_context;
1682         /* Find the first held_lock of current chain */
1683         hlock_next = hlock;
1684         for (i = curr->lockdep_depth - 1; i >= 0; i--) {
1685                 hlock_curr = curr->held_locks + i;
1686                 if (hlock_curr->irq_context != hlock_next->irq_context)
1687                         break;
1688                 hlock_next = hlock;
1689         }
1690         i++;
1691         chain->depth = curr->lockdep_depth + 1 - i;
1692         cn = nr_chain_hlocks;
1693         while (cn + chain->depth <= MAX_LOCKDEP_CHAIN_HLOCKS) {
1694                 n = cmpxchg(&nr_chain_hlocks, cn, cn + chain->depth);
1695                 if (n == cn)
1696                         break;
1697                 cn = n;
1698         }
1699         if (likely(cn + chain->depth <= MAX_LOCKDEP_CHAIN_HLOCKS)) {
1700                 chain->base = cn;
1701                 for (j = 0; j < chain->depth - 1; j++, i++) {
1702                         int lock_id = curr->held_locks[i].class_idx - 1;
1703                         chain_hlocks[chain->base + j] = lock_id;
1704                 }
1705                 chain_hlocks[chain->base + j] = class - lock_classes;
1706         }
1707         list_add_tail_rcu(&chain->entry, hash_head);
1708         debug_atomic_inc(&chain_lookup_misses);
1709         inc_chains();
1710
1711         return 1;
1712 }
1713
1714 static int validate_chain(struct task_struct *curr, struct lockdep_map *lock,
1715                 struct held_lock *hlock, int chain_head, u64 chain_key)
1716 {
1717         /*
1718          * Trylock needs to maintain the stack of held locks, but it
1719          * does not add new dependencies, because trylock can be done
1720          * in any order.
1721          *
1722          * We look up the chain_key and do the O(N^2) check and update of
1723          * the dependencies only if this is a new dependency chain.
1724          * (If lookup_chain_cache() returns with 1 it acquires
1725          * graph_lock for us)
1726          */
1727         if (!hlock->trylock && (hlock->check == 2) &&
1728             lookup_chain_cache(curr, hlock, chain_key)) {
1729                 /*
1730                  * Check whether last held lock:
1731                  *
1732                  * - is irq-safe, if this lock is irq-unsafe
1733                  * - is softirq-safe, if this lock is hardirq-unsafe
1734                  *
1735                  * And check whether the new lock's dependency graph
1736                  * could lead back to the previous lock.
1737                  *
1738                  * any of these scenarios could lead to a deadlock. If
1739                  * All validations
1740                  */
1741                 int ret = check_deadlock(curr, hlock, lock, hlock->read);
1742
1743                 if (!ret)
1744                         return 0;
1745                 /*
1746                  * Mark recursive read, as we jump over it when
1747                  * building dependencies (just like we jump over
1748                  * trylock entries):
1749                  */
1750                 if (ret == 2)
1751                         hlock->read = 2;
1752                 /*
1753                  * Add dependency only if this lock is not the head
1754                  * of the chain, and if it's not a secondary read-lock:
1755                  */
1756                 if (!chain_head && ret != 2)
1757                         if (!check_prevs_add(curr, hlock))
1758                                 return 0;
1759                 graph_unlock();
1760         } else
1761                 /* after lookup_chain_cache(): */
1762                 if (unlikely(!debug_locks))
1763                         return 0;
1764
1765         return 1;
1766 }
1767 #else
1768 static inline int validate_chain(struct task_struct *curr,
1769                 struct lockdep_map *lock, struct held_lock *hlock,
1770                 int chain_head, u64 chain_key)
1771 {
1772         return 1;
1773 }
1774 #endif
1775
1776 /*
1777  * We are building curr_chain_key incrementally, so double-check
1778  * it from scratch, to make sure that it's done correctly:
1779  */
1780 static void check_chain_key(struct task_struct *curr)
1781 {
1782 #ifdef CONFIG_DEBUG_LOCKDEP
1783         struct held_lock *hlock, *prev_hlock = NULL;
1784         unsigned int i, id;
1785         u64 chain_key = 0;
1786
1787         for (i = 0; i < curr->lockdep_depth; i++) {
1788                 hlock = curr->held_locks + i;
1789                 if (chain_key != hlock->prev_chain_key) {
1790                         debug_locks_off();
1791                         WARN(1, "hm#1, depth: %u [%u], %016Lx != %016Lx\n",
1792                                 curr->lockdep_depth, i,
1793                                 (unsigned long long)chain_key,
1794                                 (unsigned long long)hlock->prev_chain_key);
1795                         return;
1796                 }
1797                 id = hlock->class_idx - 1;
1798                 if (DEBUG_LOCKS_WARN_ON(id >= MAX_LOCKDEP_KEYS))
1799                         return;
1800
1801                 if (prev_hlock && (prev_hlock->irq_context !=
1802                                                         hlock->irq_context))
1803                         chain_key = 0;
1804                 chain_key = iterate_chain_key(chain_key, id);
1805                 prev_hlock = hlock;
1806         }
1807         if (chain_key != curr->curr_chain_key) {
1808                 debug_locks_off();
1809                 WARN(1, "hm#2, depth: %u [%u], %016Lx != %016Lx\n",
1810                         curr->lockdep_depth, i,
1811                         (unsigned long long)chain_key,
1812                         (unsigned long long)curr->curr_chain_key);
1813         }
1814 #endif
1815 }
1816
1817 static int
1818 print_usage_bug(struct task_struct *curr, struct held_lock *this,
1819                 enum lock_usage_bit prev_bit, enum lock_usage_bit new_bit)
1820 {
1821         if (!debug_locks_off_graph_unlock() || debug_locks_silent)
1822                 return 0;
1823
1824         printk("\n=================================\n");
1825         printk(  "[ INFO: inconsistent lock state ]\n");
1826         print_kernel_version();
1827         printk(  "---------------------------------\n");
1828
1829         printk("inconsistent {%s} -> {%s} usage.\n",
1830                 usage_str[prev_bit], usage_str[new_bit]);
1831
1832         printk("%s/%d [HC%u[%lu]:SC%u[%lu]:HE%u:SE%u] takes:\n",
1833                 curr->comm, task_pid_nr(curr),
1834                 trace_hardirq_context(curr), hardirq_count() >> HARDIRQ_SHIFT,
1835                 trace_softirq_context(curr), softirq_count() >> SOFTIRQ_SHIFT,
1836                 trace_hardirqs_enabled(curr),
1837                 trace_softirqs_enabled(curr));
1838         print_lock(this);
1839
1840         printk("{%s} state was registered at:\n", usage_str[prev_bit]);
1841         print_stack_trace(hlock_class(this)->usage_traces + prev_bit, 1);
1842
1843         print_irqtrace_events(curr);
1844         printk("\nother info that might help us debug this:\n");
1845         lockdep_print_held_locks(curr);
1846
1847         printk("\nstack backtrace:\n");
1848         dump_stack();
1849
1850         return 0;
1851 }
1852
1853 /*
1854  * Print out an error if an invalid bit is set:
1855  */
1856 static inline int
1857 valid_state(struct task_struct *curr, struct held_lock *this,
1858             enum lock_usage_bit new_bit, enum lock_usage_bit bad_bit)
1859 {
1860         if (unlikely(hlock_class(this)->usage_mask & (1 << bad_bit)))
1861                 return print_usage_bug(curr, this, bad_bit, new_bit);
1862         return 1;
1863 }
1864
1865 static int mark_lock(struct task_struct *curr, struct held_lock *this,
1866                      enum lock_usage_bit new_bit);
1867
1868 #if defined(CONFIG_TRACE_IRQFLAGS) && defined(CONFIG_PROVE_LOCKING)
1869
1870 /*
1871  * print irq inversion bug:
1872  */
1873 static int
1874 print_irq_inversion_bug(struct task_struct *curr, struct lock_class *other,
1875                         struct held_lock *this, int forwards,
1876                         const char *irqclass)
1877 {
1878         if (!debug_locks_off_graph_unlock() || debug_locks_silent)
1879                 return 0;
1880
1881         printk("\n=========================================================\n");
1882         printk(  "[ INFO: possible irq lock inversion dependency detected ]\n");
1883         print_kernel_version();
1884         printk(  "---------------------------------------------------------\n");
1885         printk("%s/%d just changed the state of lock:\n",
1886                 curr->comm, task_pid_nr(curr));
1887         print_lock(this);
1888         if (forwards)
1889                 printk("but this lock took another, %s-irq-unsafe lock in the past:\n", irqclass);
1890         else
1891                 printk("but this lock was taken by another, %s-irq-safe lock in the past:\n", irqclass);
1892         print_lock_name(other);
1893         printk("\n\nand interrupts could create inverse lock ordering between them.\n\n");
1894
1895         printk("\nother info that might help us debug this:\n");
1896         lockdep_print_held_locks(curr);
1897
1898         printk("\nthe first lock's dependencies:\n");
1899         print_lock_dependencies(hlock_class(this), 0);
1900
1901         printk("\nthe second lock's dependencies:\n");
1902         print_lock_dependencies(other, 0);
1903
1904         printk("\nstack backtrace:\n");
1905         dump_stack();
1906
1907         return 0;
1908 }
1909
1910 /*
1911  * Prove that in the forwards-direction subgraph starting at <this>
1912  * there is no lock matching <mask>:
1913  */
1914 static int
1915 check_usage_forwards(struct task_struct *curr, struct held_lock *this,
1916                      enum lock_usage_bit bit, const char *irqclass)
1917 {
1918         int ret;
1919
1920         find_usage_bit = bit;
1921         /* fills in <forwards_match> */
1922         ret = find_usage_forwards(hlock_class(this), 0);
1923         if (!ret || ret == 1)
1924                 return ret;
1925
1926         return print_irq_inversion_bug(curr, forwards_match, this, 1, irqclass);
1927 }
1928
1929 /*
1930  * Prove that in the backwards-direction subgraph starting at <this>
1931  * there is no lock matching <mask>:
1932  */
1933 static int
1934 check_usage_backwards(struct task_struct *curr, struct held_lock *this,
1935                       enum lock_usage_bit bit, const char *irqclass)
1936 {
1937         int ret;
1938
1939         find_usage_bit = bit;
1940         /* fills in <backwards_match> */
1941         ret = find_usage_backwards(hlock_class(this), 0);
1942         if (!ret || ret == 1)
1943                 return ret;
1944
1945         return print_irq_inversion_bug(curr, backwards_match, this, 0, irqclass);
1946 }
1947
1948 void print_irqtrace_events(struct task_struct *curr)
1949 {
1950         printk("irq event stamp: %u\n", curr->irq_events);
1951         printk("hardirqs last  enabled at (%u): ", curr->hardirq_enable_event);
1952         print_ip_sym(curr->hardirq_enable_ip);
1953         printk("hardirqs last disabled at (%u): ", curr->hardirq_disable_event);
1954         print_ip_sym(curr->hardirq_disable_ip);
1955         printk("softirqs last  enabled at (%u): ", curr->softirq_enable_event);
1956         print_ip_sym(curr->softirq_enable_ip);
1957         printk("softirqs last disabled at (%u): ", curr->softirq_disable_event);
1958         print_ip_sym(curr->softirq_disable_ip);
1959 }
1960
1961 static int HARDIRQ_verbose(struct lock_class *class)
1962 {
1963 #if HARDIRQ_VERBOSE
1964         return class_filter(class);
1965 #endif
1966         return 0;
1967 }
1968
1969 static int SOFTIRQ_verbose(struct lock_class *class)
1970 {
1971 #if SOFTIRQ_VERBOSE
1972         return class_filter(class);
1973 #endif
1974         return 0;
1975 }
1976
1977 static int RECLAIM_FS_verbose(struct lock_class *class)
1978 {
1979 #if RECLAIM_VERBOSE
1980         return class_filter(class);
1981 #endif
1982         return 0;
1983 }
1984
1985 #define STRICT_READ_CHECKS      1
1986
1987 static const char *state_names[] = {
1988 #define LOCKDEP_STATE(__STATE) \
1989         STR(__STATE),
1990 #include "lockdep_states.h"
1991 #undef LOCKDEP_STATE
1992 };
1993
1994 static inline const char *state_name(enum lock_usage_bit bit)
1995 {
1996         return state_names[bit >> 2];
1997 }
1998
1999 static const char *state_rnames[] = {
2000 #define LOCKDEP_STATE(__STATE) \
2001         STR(__STATE)"-READ",
2002 #include "lockdep_states.h"
2003 #undef LOCKDEP_STATE
2004 };
2005
2006 static inline const char *state_rname(enum lock_usage_bit bit)
2007 {
2008         return state_rnames[bit >> 2];
2009 }
2010
2011 static int (*state_verbose_f[])(struct lock_class *class) = {
2012 #define LOCKDEP_STATE(__STATE) \
2013         __STATE##_verbose,
2014 #include "lockdep_states.h"
2015 #undef LOCKDEP_STATE
2016 };
2017
2018 static inline int state_verbose(enum lock_usage_bit bit,
2019                                 struct lock_class *class)
2020 {
2021         return state_verbose_f[bit >> 2](class);
2022 }
2023
2024 static int exclusive_bit(int new_bit)
2025 {
2026         /*
2027          * USED_IN
2028          * USED_IN_READ
2029          * ENABLED
2030          * ENABLED_READ
2031          *
2032          * bit 0 - write/read
2033          * bit 1 - used_in/enabled
2034          * bit 2+  state
2035          */
2036
2037         int state = new_bit & ~3;
2038         int dir = new_bit & 2;
2039
2040         /*
2041          * keep state, bit flip the direction and strip read.
2042          */
2043         return state | (dir ^ 2);
2044 }
2045
2046 typedef int (*check_usage_f)(struct task_struct *, struct held_lock *,
2047                              enum lock_usage_bit bit, const char *name);
2048
2049 static int
2050 mark_lock_irq(struct task_struct *curr, struct held_lock *this, int new_bit)
2051 {
2052         const char *name = state_name(new_bit);
2053         const char *rname = state_rname(new_bit);
2054
2055         int excl_bit = exclusive_bit(new_bit);
2056         int read = new_bit & 1;
2057         int dir = new_bit & 2;
2058
2059         /*
2060          * mark USED_IN has to look forwards -- to ensure no dependency
2061          * has ENABLED state, which would allow recursion deadlocks.
2062          *
2063          * mark ENABLED has to look backwards -- to ensure no dependee
2064          * has USED_IN state, which, again, would allow  recursion deadlocks.
2065          */
2066         check_usage_f usage = dir ?
2067                 check_usage_backwards : check_usage_forwards;
2068
2069         /*
2070          * Validate that this particular lock does not have conflicting
2071          * usage states.
2072          */
2073         if (!valid_state(curr, this, new_bit, excl_bit))
2074                 return 0;
2075
2076         /*
2077          * Validate that the lock dependencies don't have conflicting usage
2078          * states.
2079          */
2080         if ((!read || !dir || STRICT_READ_CHECKS) &&
2081                         !usage(curr, this, excl_bit, name))
2082                 return 0;
2083
2084         /*
2085          * Check for read in write conflicts
2086          */
2087         if (!read) {
2088                 if (!valid_state(curr, this, new_bit, excl_bit + 1))
2089                         return 0;
2090
2091                 if (STRICT_READ_CHECKS &&
2092                                 !usage(curr, this, excl_bit + 1, rname))
2093                         return 0;
2094         }
2095
2096         if (state_verbose(new_bit, hlock_class(this)))
2097                 return 2;
2098
2099         return 1;
2100 }
2101
2102 enum mark_type {
2103 #define LOCKDEP_STATE(__STATE)  __STATE,
2104 #include "lockdep_states.h"
2105 #undef LOCKDEP_STATE
2106 };
2107
2108 /*
2109  * Mark all held locks with a usage bit:
2110  */
2111 static int
2112 mark_held_locks(struct task_struct *curr, enum mark_type mark)
2113 {
2114         enum lock_usage_bit usage_bit;
2115         struct held_lock *hlock;
2116         int i;
2117
2118         for (i = 0; i < curr->lockdep_depth; i++) {
2119                 hlock = curr->held_locks + i;
2120
2121                 usage_bit = 2 + (mark << 2); /* ENABLED */
2122                 if (hlock->read)
2123                         usage_bit += 1; /* READ */
2124
2125                 BUG_ON(usage_bit >= LOCK_USAGE_STATES);
2126
2127                 if (!mark_lock(curr, hlock, usage_bit))
2128                         return 0;
2129         }
2130
2131         return 1;
2132 }
2133
2134 /*
2135  * Debugging helper: via this flag we know that we are in
2136  * 'early bootup code', and will warn about any invalid irqs-on event:
2137  */
2138 static int early_boot_irqs_enabled;
2139
2140 void early_boot_irqs_off(void)
2141 {
2142         early_boot_irqs_enabled = 0;
2143 }
2144
2145 void early_boot_irqs_on(void)
2146 {
2147         early_boot_irqs_enabled = 1;
2148 }
2149
2150 /*
2151  * Hardirqs will be enabled:
2152  */
2153 void trace_hardirqs_on_caller(unsigned long ip)
2154 {
2155         struct task_struct *curr = current;
2156
2157         time_hardirqs_on(CALLER_ADDR0, ip);
2158
2159         if (unlikely(!debug_locks || current->lockdep_recursion))
2160                 return;
2161
2162         if (DEBUG_LOCKS_WARN_ON(unlikely(!early_boot_irqs_enabled)))
2163                 return;
2164
2165         if (unlikely(curr->hardirqs_enabled)) {
2166                 debug_atomic_inc(&redundant_hardirqs_on);
2167                 return;
2168         }
2169         /* we'll do an OFF -> ON transition: */
2170         curr->hardirqs_enabled = 1;
2171
2172         if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2173                 return;
2174         if (DEBUG_LOCKS_WARN_ON(current->hardirq_context))
2175                 return;
2176         /*
2177          * We are going to turn hardirqs on, so set the
2178          * usage bit for all held locks:
2179          */
2180         if (!mark_held_locks(curr, HARDIRQ))
2181                 return;
2182         /*
2183          * If we have softirqs enabled, then set the usage
2184          * bit for all held locks. (disabled hardirqs prevented
2185          * this bit from being set before)
2186          */
2187         if (curr->softirqs_enabled)
2188                 if (!mark_held_locks(curr, SOFTIRQ))
2189                         return;
2190
2191         curr->hardirq_enable_ip = ip;
2192         curr->hardirq_enable_event = ++curr->irq_events;
2193         debug_atomic_inc(&hardirqs_on_events);
2194 }
2195 EXPORT_SYMBOL(trace_hardirqs_on_caller);
2196
2197 void trace_hardirqs_on(void)
2198 {
2199         trace_hardirqs_on_caller(CALLER_ADDR0);
2200 }
2201 EXPORT_SYMBOL(trace_hardirqs_on);
2202
2203 /*
2204  * Hardirqs were disabled:
2205  */
2206 void trace_hardirqs_off_caller(unsigned long ip)
2207 {
2208         struct task_struct *curr = current;
2209
2210         time_hardirqs_off(CALLER_ADDR0, ip);
2211
2212         if (unlikely(!debug_locks || current->lockdep_recursion))
2213                 return;
2214
2215         if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2216                 return;
2217
2218         if (curr->hardirqs_enabled) {
2219                 /*
2220                  * We have done an ON -> OFF transition:
2221                  */
2222                 curr->hardirqs_enabled = 0;
2223                 curr->hardirq_disable_ip = ip;
2224                 curr->hardirq_disable_event = ++curr->irq_events;
2225                 debug_atomic_inc(&hardirqs_off_events);
2226         } else
2227                 debug_atomic_inc(&redundant_hardirqs_off);
2228 }
2229 EXPORT_SYMBOL(trace_hardirqs_off_caller);
2230
2231 void trace_hardirqs_off(void)
2232 {
2233         trace_hardirqs_off_caller(CALLER_ADDR0);
2234 }
2235 EXPORT_SYMBOL(trace_hardirqs_off);
2236
2237 /*
2238  * Softirqs will be enabled:
2239  */
2240 void trace_softirqs_on(unsigned long ip)
2241 {
2242         struct task_struct *curr = current;
2243
2244         if (unlikely(!debug_locks))
2245                 return;
2246
2247         if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2248                 return;
2249
2250         if (curr->softirqs_enabled) {
2251                 debug_atomic_inc(&redundant_softirqs_on);
2252                 return;
2253         }
2254
2255         /*
2256          * We'll do an OFF -> ON transition:
2257          */
2258         curr->softirqs_enabled = 1;
2259         curr->softirq_enable_ip = ip;
2260         curr->softirq_enable_event = ++curr->irq_events;
2261         debug_atomic_inc(&softirqs_on_events);
2262         /*
2263          * We are going to turn softirqs on, so set the
2264          * usage bit for all held locks, if hardirqs are
2265          * enabled too:
2266          */
2267         if (curr->hardirqs_enabled)
2268                 mark_held_locks(curr, SOFTIRQ);
2269 }
2270
2271 /*
2272  * Softirqs were disabled:
2273  */
2274 void trace_softirqs_off(unsigned long ip)
2275 {
2276         struct task_struct *curr = current;
2277
2278         if (unlikely(!debug_locks))
2279                 return;
2280
2281         if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2282                 return;
2283
2284         if (curr->softirqs_enabled) {
2285                 /*
2286                  * We have done an ON -> OFF transition:
2287                  */
2288                 curr->softirqs_enabled = 0;
2289                 curr->softirq_disable_ip = ip;
2290                 curr->softirq_disable_event = ++curr->irq_events;
2291                 debug_atomic_inc(&softirqs_off_events);
2292                 DEBUG_LOCKS_WARN_ON(!softirq_count());
2293         } else
2294                 debug_atomic_inc(&redundant_softirqs_off);
2295 }
2296
2297 void lockdep_trace_alloc(gfp_t gfp_mask)
2298 {
2299         struct task_struct *curr = current;
2300
2301         if (unlikely(!debug_locks))
2302                 return;
2303
2304         /* no reclaim without waiting on it */
2305         if (!(gfp_mask & __GFP_WAIT))
2306                 return;
2307
2308         /* this guy won't enter reclaim */
2309         if ((curr->flags & PF_MEMALLOC) && !(gfp_mask & __GFP_NOMEMALLOC))
2310                 return;
2311
2312         /* We're only interested __GFP_FS allocations for now */
2313         if (!(gfp_mask & __GFP_FS))
2314                 return;
2315
2316         if (DEBUG_LOCKS_WARN_ON(irqs_disabled()))
2317                 return;
2318
2319         mark_held_locks(curr, RECLAIM_FS);
2320 }
2321
2322 static int mark_irqflags(struct task_struct *curr, struct held_lock *hlock)
2323 {
2324         /*
2325          * If non-trylock use in a hardirq or softirq context, then
2326          * mark the lock as used in these contexts:
2327          */
2328         if (!hlock->trylock) {
2329                 if (hlock->read) {
2330                         if (curr->hardirq_context)
2331                                 if (!mark_lock(curr, hlock,
2332                                                 LOCK_USED_IN_HARDIRQ_READ))
2333                                         return 0;
2334                         if (curr->softirq_context)
2335                                 if (!mark_lock(curr, hlock,
2336                                                 LOCK_USED_IN_SOFTIRQ_READ))
2337                                         return 0;
2338                 } else {
2339                         if (curr->hardirq_context)
2340                                 if (!mark_lock(curr, hlock, LOCK_USED_IN_HARDIRQ))
2341                                         return 0;
2342                         if (curr->softirq_context)
2343                                 if (!mark_lock(curr, hlock, LOCK_USED_IN_SOFTIRQ))
2344                                         return 0;
2345                 }
2346         }
2347         if (!hlock->hardirqs_off) {
2348                 if (hlock->read) {
2349                         if (!mark_lock(curr, hlock,
2350                                         LOCK_ENABLED_HARDIRQ_READ))
2351                                 return 0;
2352                         if (curr->softirqs_enabled)
2353                                 if (!mark_lock(curr, hlock,
2354                                                 LOCK_ENABLED_SOFTIRQ_READ))
2355                                         return 0;
2356                 } else {
2357                         if (!mark_lock(curr, hlock,
2358                                         LOCK_ENABLED_HARDIRQ))
2359                                 return 0;
2360                         if (curr->softirqs_enabled)
2361                                 if (!mark_lock(curr, hlock,
2362                                                 LOCK_ENABLED_SOFTIRQ))
2363                                         return 0;
2364                 }
2365         }
2366
2367         /*
2368          * We reuse the irq context infrastructure more broadly as a general
2369          * context checking code. This tests GFP_FS recursion (a lock taken
2370          * during reclaim for a GFP_FS allocation is held over a GFP_FS
2371          * allocation).
2372          */
2373         if (!hlock->trylock && (curr->lockdep_reclaim_gfp & __GFP_FS)) {
2374                 if (hlock->read) {
2375                         if (!mark_lock(curr, hlock, LOCK_USED_IN_RECLAIM_FS_READ))
2376                                         return 0;
2377                 } else {
2378                         if (!mark_lock(curr, hlock, LOCK_USED_IN_RECLAIM_FS))
2379                                         return 0;
2380                 }
2381         }
2382
2383         return 1;
2384 }
2385
2386 static int separate_irq_context(struct task_struct *curr,
2387                 struct held_lock *hlock)
2388 {
2389         unsigned int depth = curr->lockdep_depth;
2390
2391         /*
2392          * Keep track of points where we cross into an interrupt context:
2393          */
2394         hlock->irq_context = 2*(curr->hardirq_context ? 1 : 0) +
2395                                 curr->softirq_context;
2396         if (depth) {
2397                 struct held_lock *prev_hlock;
2398
2399                 prev_hlock = curr->held_locks + depth-1;
2400                 /*
2401                  * If we cross into another context, reset the
2402                  * hash key (this also prevents the checking and the
2403                  * adding of the dependency to 'prev'):
2404                  */
2405                 if (prev_hlock->irq_context != hlock->irq_context)
2406                         return 1;
2407         }
2408         return 0;
2409 }
2410
2411 #else
2412
2413 static inline
2414 int mark_lock_irq(struct task_struct *curr, struct held_lock *this,
2415                 enum lock_usage_bit new_bit)
2416 {
2417         WARN_ON(1);
2418         return 1;
2419 }
2420
2421 static inline int mark_irqflags(struct task_struct *curr,
2422                 struct held_lock *hlock)
2423 {
2424         return 1;
2425 }
2426
2427 static inline int separate_irq_context(struct task_struct *curr,
2428                 struct held_lock *hlock)
2429 {
2430         return 0;
2431 }
2432
2433 #endif
2434
2435 /*
2436  * Mark a lock with a usage bit, and validate the state transition:
2437  */
2438 static int mark_lock(struct task_struct *curr, struct held_lock *this,
2439                              enum lock_usage_bit new_bit)
2440 {
2441         unsigned int new_mask = 1 << new_bit, ret = 1;
2442
2443         /*
2444          * If already set then do not dirty the cacheline,
2445          * nor do any checks:
2446          */
2447         if (likely(hlock_class(this)->usage_mask & new_mask))
2448                 return 1;
2449
2450         if (!graph_lock())
2451                 return 0;
2452         /*
2453          * Make sure we didnt race:
2454          */
2455         if (unlikely(hlock_class(this)->usage_mask & new_mask)) {
2456                 graph_unlock();
2457                 return 1;
2458         }
2459
2460         hlock_class(this)->usage_mask |= new_mask;
2461
2462         if (!save_trace(hlock_class(this)->usage_traces + new_bit))
2463                 return 0;
2464
2465         switch (new_bit) {
2466 #define LOCKDEP_STATE(__STATE)                  \
2467         case LOCK_USED_IN_##__STATE:            \
2468         case LOCK_USED_IN_##__STATE##_READ:     \
2469         case LOCK_ENABLED_##__STATE:            \
2470         case LOCK_ENABLED_##__STATE##_READ:
2471 #include "lockdep_states.h"
2472 #undef LOCKDEP_STATE
2473                 ret = mark_lock_irq(curr, this, new_bit);
2474                 if (!ret)
2475                         return 0;
2476                 break;
2477         case LOCK_USED:
2478                 debug_atomic_dec(&nr_unused_locks);
2479                 break;
2480         default:
2481                 if (!debug_locks_off_graph_unlock())
2482                         return 0;
2483                 WARN_ON(1);
2484                 return 0;
2485         }
2486
2487         graph_unlock();
2488
2489         /*
2490          * We must printk outside of the graph_lock:
2491          */
2492         if (ret == 2) {
2493                 printk("\nmarked lock as {%s}:\n", usage_str[new_bit]);
2494                 print_lock(this);
2495                 print_irqtrace_events(curr);
2496                 dump_stack();
2497         }
2498
2499         return ret;
2500 }
2501
2502 /*
2503  * Initialize a lock instance's lock-class mapping info:
2504  */
2505 void lockdep_init_map(struct lockdep_map *lock, const char *name,
2506                       struct lock_class_key *key, int subclass)
2507 {
2508         if (unlikely(!debug_locks))
2509                 return;
2510
2511         if (DEBUG_LOCKS_WARN_ON(!key))
2512                 return;
2513         if (DEBUG_LOCKS_WARN_ON(!name))
2514                 return;
2515         /*
2516          * Sanity check, the lock-class key must be persistent:
2517          */
2518         if (!static_obj(key)) {
2519                 printk("BUG: key %p not in .data!\n", key);
2520                 DEBUG_LOCKS_WARN_ON(1);
2521                 return;
2522         }
2523         lock->name = name;
2524         lock->key = key;
2525         lock->class_cache = NULL;
2526 #ifdef CONFIG_LOCK_STAT
2527         lock->cpu = raw_smp_processor_id();
2528 #endif
2529         if (subclass)
2530                 register_lock_class(lock, subclass, 1);
2531 }
2532 EXPORT_SYMBOL_GPL(lockdep_init_map);
2533
2534 /*
2535  * This gets called for every mutex_lock*()/spin_lock*() operation.
2536  * We maintain the dependency maps and validate the locking attempt:
2537  */
2538 static int __lock_acquire(struct lockdep_map *lock, unsigned int subclass,
2539                           int trylock, int read, int check, int hardirqs_off,
2540                           struct lockdep_map *nest_lock, unsigned long ip)
2541 {
2542         struct task_struct *curr = current;
2543         struct lock_class *class = NULL;
2544         struct held_lock *hlock;
2545         unsigned int depth, id;
2546         int chain_head = 0;
2547         u64 chain_key;
2548
2549         if (!prove_locking)
2550                 check = 1;
2551
2552         if (unlikely(!debug_locks))
2553                 return 0;
2554
2555         if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2556                 return 0;
2557
2558         if (unlikely(subclass >= MAX_LOCKDEP_SUBCLASSES)) {
2559                 debug_locks_off();
2560                 printk("BUG: MAX_LOCKDEP_SUBCLASSES too low!\n");
2561                 printk("turning off the locking correctness validator.\n");
2562                 return 0;
2563         }
2564
2565         if (!subclass)
2566                 class = lock->class_cache;
2567         /*
2568          * Not cached yet or subclass?
2569          */
2570         if (unlikely(!class)) {
2571                 class = register_lock_class(lock, subclass, 0);
2572                 if (!class)
2573                         return 0;
2574         }
2575         debug_atomic_inc((atomic_t *)&class->ops);
2576         if (very_verbose(class)) {
2577                 printk("\nacquire class [%p] %s", class->key, class->name);
2578                 if (class->name_version > 1)
2579                         printk("#%d", class->name_version);
2580                 printk("\n");
2581                 dump_stack();
2582         }
2583
2584         /*
2585          * Add the lock to the list of currently held locks.
2586          * (we dont increase the depth just yet, up until the
2587          * dependency checks are done)
2588          */
2589         depth = curr->lockdep_depth;
2590         if (DEBUG_LOCKS_WARN_ON(depth >= MAX_LOCK_DEPTH))
2591                 return 0;
2592
2593         hlock = curr->held_locks + depth;
2594         if (DEBUG_LOCKS_WARN_ON(!class))
2595                 return 0;
2596         hlock->class_idx = class - lock_classes + 1;
2597         hlock->acquire_ip = ip;
2598         hlock->instance = lock;
2599         hlock->nest_lock = nest_lock;
2600         hlock->trylock = trylock;
2601         hlock->read = read;
2602         hlock->check = check;
2603         hlock->hardirqs_off = !!hardirqs_off;
2604 #ifdef CONFIG_LOCK_STAT
2605         hlock->waittime_stamp = 0;
2606         hlock->holdtime_stamp = sched_clock();
2607 #endif
2608
2609         if (check == 2 && !mark_irqflags(curr, hlock))
2610                 return 0;
2611
2612         /* mark it as used: */
2613         if (!mark_lock(curr, hlock, LOCK_USED))
2614                 return 0;
2615
2616         /*
2617          * Calculate the chain hash: it's the combined hash of all the
2618          * lock keys along the dependency chain. We save the hash value
2619          * at every step so that we can get the current hash easily
2620          * after unlock. The chain hash is then used to cache dependency
2621          * results.
2622          *
2623          * The 'key ID' is what is the most compact key value to drive
2624          * the hash, not class->key.
2625          */
2626         id = class - lock_classes;
2627         if (DEBUG_LOCKS_WARN_ON(id >= MAX_LOCKDEP_KEYS))
2628                 return 0;
2629
2630         chain_key = curr->curr_chain_key;
2631         if (!depth) {
2632                 if (DEBUG_LOCKS_WARN_ON(chain_key != 0))
2633                         return 0;
2634                 chain_head = 1;
2635         }
2636
2637         hlock->prev_chain_key = chain_key;
2638         if (separate_irq_context(curr, hlock)) {
2639                 chain_key = 0;
2640                 chain_head = 1;
2641         }
2642         chain_key = iterate_chain_key(chain_key, id);
2643
2644         if (!validate_chain(curr, lock, hlock, chain_head, chain_key))
2645                 return 0;
2646
2647         curr->curr_chain_key = chain_key;
2648         curr->lockdep_depth++;
2649         check_chain_key(curr);
2650 #ifdef CONFIG_DEBUG_LOCKDEP
2651         if (unlikely(!debug_locks))
2652                 return 0;
2653 #endif
2654         if (unlikely(curr->lockdep_depth >= MAX_LOCK_DEPTH)) {
2655                 debug_locks_off();
2656                 printk("BUG: MAX_LOCK_DEPTH too low!\n");
2657                 printk("turning off the locking correctness validator.\n");
2658                 return 0;
2659         }
2660
2661         if (unlikely(curr->lockdep_depth > max_lockdep_depth))
2662                 max_lockdep_depth = curr->lockdep_depth;
2663
2664         return 1;
2665 }
2666
2667 static int
2668 print_unlock_inbalance_bug(struct task_struct *curr, struct lockdep_map *lock,
2669                            unsigned long ip)
2670 {
2671         if (!debug_locks_off())
2672                 return 0;
2673         if (debug_locks_silent)
2674                 return 0;
2675
2676         printk("\n=====================================\n");
2677         printk(  "[ BUG: bad unlock balance detected! ]\n");
2678         printk(  "-------------------------------------\n");
2679         printk("%s/%d is trying to release lock (",
2680                 curr->comm, task_pid_nr(curr));
2681         print_lockdep_cache(lock);
2682         printk(") at:\n");
2683         print_ip_sym(ip);
2684         printk("but there are no more locks to release!\n");
2685         printk("\nother info that might help us debug this:\n");
2686         lockdep_print_held_locks(curr);
2687
2688         printk("\nstack backtrace:\n");
2689         dump_stack();
2690
2691         return 0;
2692 }
2693
2694 /*
2695  * Common debugging checks for both nested and non-nested unlock:
2696  */
2697 static int check_unlock(struct task_struct *curr, struct lockdep_map *lock,
2698                         unsigned long ip)
2699 {
2700         if (unlikely(!debug_locks))
2701                 return 0;
2702         if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2703                 return 0;
2704
2705         if (curr->lockdep_depth <= 0)
2706                 return print_unlock_inbalance_bug(curr, lock, ip);
2707
2708         return 1;
2709 }
2710
2711 static int
2712 __lock_set_class(struct lockdep_map *lock, const char *name,
2713                  struct lock_class_key *key, unsigned int subclass,
2714                  unsigned long ip)
2715 {
2716         struct task_struct *curr = current;
2717         struct held_lock *hlock, *prev_hlock;
2718         struct lock_class *class;
2719         unsigned int depth;
2720         int i;
2721
2722         depth = curr->lockdep_depth;
2723         if (DEBUG_LOCKS_WARN_ON(!depth))
2724                 return 0;
2725
2726         prev_hlock = NULL;
2727         for (i = depth-1; i >= 0; i--) {
2728                 hlock = curr->held_locks + i;
2729                 /*
2730                  * We must not cross into another context:
2731                  */
2732                 if (prev_hlock && prev_hlock->irq_context != hlock->irq_context)
2733                         break;
2734                 if (hlock->instance == lock)
2735                         goto found_it;
2736                 prev_hlock = hlock;
2737         }
2738         return print_unlock_inbalance_bug(curr, lock, ip);
2739
2740 found_it:
2741         lockdep_init_map(lock, name, key, 0);
2742         class = register_lock_class(lock, subclass, 0);
2743         hlock->class_idx = class - lock_classes + 1;
2744
2745         curr->lockdep_depth = i;
2746         curr->curr_chain_key = hlock->prev_chain_key;
2747
2748         for (; i < depth; i++) {
2749                 hlock = curr->held_locks + i;
2750                 if (!__lock_acquire(hlock->instance,
2751                         hlock_class(hlock)->subclass, hlock->trylock,
2752                                 hlock->read, hlock->check, hlock->hardirqs_off,
2753                                 hlock->nest_lock, hlock->acquire_ip))
2754                         return 0;
2755         }
2756
2757         if (DEBUG_LOCKS_WARN_ON(curr->lockdep_depth != depth))
2758                 return 0;
2759         return 1;
2760 }
2761
2762 /*
2763  * Remove the lock to the list of currently held locks in a
2764  * potentially non-nested (out of order) manner. This is a
2765  * relatively rare operation, as all the unlock APIs default
2766  * to nested mode (which uses lock_release()):
2767  */
2768 static int
2769 lock_release_non_nested(struct task_struct *curr,
2770                         struct lockdep_map *lock, unsigned long ip)
2771 {
2772         struct held_lock *hlock, *prev_hlock;
2773         unsigned int depth;
2774         int i;
2775
2776         /*
2777          * Check whether the lock exists in the current stack
2778          * of held locks:
2779          */
2780         depth = curr->lockdep_depth;
2781         if (DEBUG_LOCKS_WARN_ON(!depth))
2782                 return 0;
2783
2784         prev_hlock = NULL;
2785         for (i = depth-1; i >= 0; i--) {
2786                 hlock = curr->held_locks + i;
2787                 /*
2788                  * We must not cross into another context:
2789                  */
2790                 if (prev_hlock && prev_hlock->irq_context != hlock->irq_context)
2791                         break;
2792                 if (hlock->instance == lock)
2793                         goto found_it;
2794                 prev_hlock = hlock;
2795         }
2796         return print_unlock_inbalance_bug(curr, lock, ip);
2797
2798 found_it:
2799         lock_release_holdtime(hlock);
2800
2801         /*
2802          * We have the right lock to unlock, 'hlock' points to it.
2803          * Now we remove it from the stack, and add back the other
2804          * entries (if any), recalculating the hash along the way:
2805          */
2806         curr->lockdep_depth = i;
2807         curr->curr_chain_key = hlock->prev_chain_key;
2808
2809         for (i++; i < depth; i++) {
2810                 hlock = curr->held_locks + i;
2811                 if (!__lock_acquire(hlock->instance,
2812                         hlock_class(hlock)->subclass, hlock->trylock,
2813                                 hlock->read, hlock->check, hlock->hardirqs_off,
2814                                 hlock->nest_lock, hlock->acquire_ip))
2815                         return 0;
2816         }
2817
2818         if (DEBUG_LOCKS_WARN_ON(curr->lockdep_depth != depth - 1))
2819                 return 0;
2820         return 1;
2821 }
2822
2823 /*
2824  * Remove the lock to the list of currently held locks - this gets
2825  * called on mutex_unlock()/spin_unlock*() (or on a failed
2826  * mutex_lock_interruptible()). This is done for unlocks that nest
2827  * perfectly. (i.e. the current top of the lock-stack is unlocked)
2828  */
2829 static int lock_release_nested(struct task_struct *curr,
2830                                struct lockdep_map *lock, unsigned long ip)
2831 {
2832         struct held_lock *hlock;
2833         unsigned int depth;
2834
2835         /*
2836          * Pop off the top of the lock stack:
2837          */
2838         depth = curr->lockdep_depth - 1;
2839         hlock = curr->held_locks + depth;
2840
2841         /*
2842          * Is the unlock non-nested:
2843          */
2844         if (hlock->instance != lock)
2845                 return lock_release_non_nested(curr, lock, ip);
2846         curr->lockdep_depth--;
2847
2848         if (DEBUG_LOCKS_WARN_ON(!depth && (hlock->prev_chain_key != 0)))
2849                 return 0;
2850
2851         curr->curr_chain_key = hlock->prev_chain_key;
2852
2853         lock_release_holdtime(hlock);
2854
2855 #ifdef CONFIG_DEBUG_LOCKDEP
2856         hlock->prev_chain_key = 0;
2857         hlock->class_idx = 0;
2858         hlock->acquire_ip = 0;
2859         hlock->irq_context = 0;
2860 #endif
2861         return 1;
2862 }
2863
2864 /*
2865  * Remove the lock to the list of currently held locks - this gets
2866  * called on mutex_unlock()/spin_unlock*() (or on a failed
2867  * mutex_lock_interruptible()). This is done for unlocks that nest
2868  * perfectly. (i.e. the current top of the lock-stack is unlocked)
2869  */
2870 static void
2871 __lock_release(struct lockdep_map *lock, int nested, unsigned long ip)
2872 {
2873         struct task_struct *curr = current;
2874
2875         if (!check_unlock(curr, lock, ip))
2876                 return;
2877
2878         if (nested) {
2879                 if (!lock_release_nested(curr, lock, ip))
2880                         return;
2881         } else {
2882                 if (!lock_release_non_nested(curr, lock, ip))
2883                         return;
2884         }
2885
2886         check_chain_key(curr);
2887 }
2888
2889 /*
2890  * Check whether we follow the irq-flags state precisely:
2891  */
2892 static void check_flags(unsigned long flags)
2893 {
2894 #if defined(CONFIG_PROVE_LOCKING) && defined(CONFIG_DEBUG_LOCKDEP) && \
2895     defined(CONFIG_TRACE_IRQFLAGS)
2896         if (!debug_locks)
2897                 return;
2898
2899         if (irqs_disabled_flags(flags)) {
2900                 if (DEBUG_LOCKS_WARN_ON(current->hardirqs_enabled)) {
2901                         printk("possible reason: unannotated irqs-off.\n");
2902                 }
2903         } else {
2904                 if (DEBUG_LOCKS_WARN_ON(!current->hardirqs_enabled)) {
2905                         printk("possible reason: unannotated irqs-on.\n");
2906                 }
2907         }
2908
2909         /*
2910          * We dont accurately track softirq state in e.g.
2911          * hardirq contexts (such as on 4KSTACKS), so only
2912          * check if not in hardirq contexts:
2913          */
2914         if (!hardirq_count()) {
2915                 if (softirq_count())
2916                         DEBUG_LOCKS_WARN_ON(current->softirqs_enabled);
2917                 else
2918                         DEBUG_LOCKS_WARN_ON(!current->softirqs_enabled);
2919         }
2920
2921         if (!debug_locks)
2922                 print_irqtrace_events(current);
2923 #endif
2924 }
2925
2926 void lock_set_class(struct lockdep_map *lock, const char *name,
2927                     struct lock_class_key *key, unsigned int subclass,
2928                     unsigned long ip)
2929 {
2930         unsigned long flags;
2931
2932         if (unlikely(current->lockdep_recursion))
2933                 return;
2934
2935         raw_local_irq_save(flags);
2936         current->lockdep_recursion = 1;
2937         check_flags(flags);
2938         if (__lock_set_class(lock, name, key, subclass, ip))
2939                 check_chain_key(current);
2940         current->lockdep_recursion = 0;
2941         raw_local_irq_restore(flags);
2942 }
2943 EXPORT_SYMBOL_GPL(lock_set_class);
2944
2945 /*
2946  * We are not always called with irqs disabled - do that here,
2947  * and also avoid lockdep recursion:
2948  */
2949 void lock_acquire(struct lockdep_map *lock, unsigned int subclass,
2950                           int trylock, int read, int check,
2951                           struct lockdep_map *nest_lock, unsigned long ip)
2952 {
2953         unsigned long flags;
2954
2955         if (unlikely(current->lockdep_recursion))
2956                 return;
2957
2958         raw_local_irq_save(flags);
2959         check_flags(flags);
2960
2961         current->lockdep_recursion = 1;
2962         __lock_acquire(lock, subclass, trylock, read, check,
2963                        irqs_disabled_flags(flags), nest_lock, ip);
2964         current->lockdep_recursion = 0;
2965         raw_local_irq_restore(flags);
2966 }
2967 EXPORT_SYMBOL_GPL(lock_acquire);
2968
2969 void lock_release(struct lockdep_map *lock, int nested,
2970                           unsigned long ip)
2971 {
2972         unsigned long flags;
2973
2974         if (unlikely(current->lockdep_recursion))
2975                 return;
2976
2977         raw_local_irq_save(flags);
2978         check_flags(flags);
2979         current->lockdep_recursion = 1;
2980         __lock_release(lock, nested, ip);
2981         current->lockdep_recursion = 0;
2982         raw_local_irq_restore(flags);
2983 }
2984 EXPORT_SYMBOL_GPL(lock_release);
2985
2986 void lockdep_set_current_reclaim_state(gfp_t gfp_mask)
2987 {
2988         current->lockdep_reclaim_gfp = gfp_mask;
2989 }
2990
2991 void lockdep_clear_current_reclaim_state(void)
2992 {
2993         current->lockdep_reclaim_gfp = 0;
2994 }
2995
2996 #ifdef CONFIG_LOCK_STAT
2997 static int
2998 print_lock_contention_bug(struct task_struct *curr, struct lockdep_map *lock,
2999                            unsigned long ip)
3000 {
3001         if (!debug_locks_off())
3002                 return 0;
3003         if (debug_locks_silent)
3004                 return 0;
3005
3006         printk("\n=================================\n");
3007         printk(  "[ BUG: bad contention detected! ]\n");
3008         printk(  "---------------------------------\n");
3009         printk("%s/%d is trying to contend lock (",
3010                 curr->comm, task_pid_nr(curr));
3011         print_lockdep_cache(lock);
3012         printk(") at:\n");
3013         print_ip_sym(ip);
3014         printk("but there are no locks held!\n");
3015         printk("\nother info that might help us debug this:\n");
3016         lockdep_print_held_locks(curr);
3017
3018         printk("\nstack backtrace:\n");
3019         dump_stack();
3020
3021         return 0;
3022 }
3023
3024 static void
3025 __lock_contended(struct lockdep_map *lock, unsigned long ip)
3026 {
3027         struct task_struct *curr = current;
3028         struct held_lock *hlock, *prev_hlock;
3029         struct lock_class_stats *stats;
3030         unsigned int depth;
3031         int i, contention_point, contending_point;
3032
3033         depth = curr->lockdep_depth;
3034         if (DEBUG_LOCKS_WARN_ON(!depth))
3035                 return;
3036
3037         prev_hlock = NULL;
3038         for (i = depth-1; i >= 0; i--) {
3039                 hlock = curr->held_locks + i;
3040                 /*
3041                  * We must not cross into another context:
3042                  */
3043                 if (prev_hlock && prev_hlock->irq_context != hlock->irq_context)
3044                         break;
3045                 if (hlock->instance == lock)
3046                         goto found_it;
3047                 prev_hlock = hlock;
3048         }
3049         print_lock_contention_bug(curr, lock, ip);
3050         return;
3051
3052 found_it:
3053         hlock->waittime_stamp = sched_clock();
3054
3055         contention_point = lock_point(hlock_class(hlock)->contention_point, ip);
3056         contending_point = lock_point(hlock_class(hlock)->contending_point,
3057                                       lock->ip);
3058
3059         stats = get_lock_stats(hlock_class(hlock));
3060         if (contention_point < LOCKSTAT_POINTS)
3061                 stats->contention_point[contention_point]++;
3062         if (contending_point < LOCKSTAT_POINTS)
3063                 stats->contending_point[contending_point]++;
3064         if (lock->cpu != smp_processor_id())
3065                 stats->bounces[bounce_contended + !!hlock->read]++;
3066         put_lock_stats(stats);
3067 }
3068
3069 static void
3070 __lock_acquired(struct lockdep_map *lock, unsigned long ip)
3071 {
3072         struct task_struct *curr = current;
3073         struct held_lock *hlock, *prev_hlock;
3074         struct lock_class_stats *stats;
3075         unsigned int depth;
3076         u64 now;
3077         s64 waittime = 0;
3078         int i, cpu;
3079
3080         depth = curr->lockdep_depth;
3081         if (DEBUG_LOCKS_WARN_ON(!depth))
3082                 return;
3083
3084         prev_hlock = NULL;
3085         for (i = depth-1; i >= 0; i--) {
3086                 hlock = curr->held_locks + i;
3087                 /*
3088                  * We must not cross into another context:
3089                  */
3090                 if (prev_hlock && prev_hlock->irq_context != hlock->irq_context)
3091                         break;
3092                 if (hlock->instance == lock)
3093                         goto found_it;
3094                 prev_hlock = hlock;
3095         }
3096         print_lock_contention_bug(curr, lock, _RET_IP_);
3097         return;
3098
3099 found_it:
3100         cpu = smp_processor_id();
3101         if (hlock->waittime_stamp) {
3102                 now = sched_clock();
3103                 waittime = now - hlock->waittime_stamp;
3104                 hlock->holdtime_stamp = now;
3105         }
3106
3107         stats = get_lock_stats(hlock_class(hlock));
3108         if (waittime) {
3109                 if (hlock->read)
3110                         lock_time_inc(&stats->read_waittime, waittime);
3111                 else
3112                         lock_time_inc(&stats->write_waittime, waittime);
3113         }
3114         if (lock->cpu != cpu)
3115                 stats->bounces[bounce_acquired + !!hlock->read]++;
3116         put_lock_stats(stats);
3117
3118         lock->cpu = cpu;
3119         lock->ip = ip;
3120 }
3121
3122 void lock_contended(struct lockdep_map *lock, unsigned long ip)
3123 {
3124         unsigned long flags;
3125
3126         if (unlikely(!lock_stat))
3127                 return;
3128
3129         if (unlikely(current->lockdep_recursion))
3130                 return;
3131
3132         raw_local_irq_save(flags);
3133         check_flags(flags);
3134         current->lockdep_recursion = 1;
3135         __lock_contended(lock, ip);
3136         current->lockdep_recursion = 0;
3137         raw_local_irq_restore(flags);
3138 }
3139 EXPORT_SYMBOL_GPL(lock_contended);
3140
3141 void lock_acquired(struct lockdep_map *lock, unsigned long ip)
3142 {
3143         unsigned long flags;
3144
3145         if (unlikely(!lock_stat))
3146                 return;
3147
3148         if (unlikely(current->lockdep_recursion))
3149                 return;
3150
3151         raw_local_irq_save(flags);
3152         check_flags(flags);
3153         current->lockdep_recursion = 1;
3154         __lock_acquired(lock, ip);
3155         current->lockdep_recursion = 0;
3156         raw_local_irq_restore(flags);
3157 }
3158 EXPORT_SYMBOL_GPL(lock_acquired);
3159 #endif
3160
3161 /*
3162  * Used by the testsuite, sanitize the validator state
3163  * after a simulated failure:
3164  */
3165
3166 void lockdep_reset(void)
3167 {
3168         unsigned long flags;
3169         int i;
3170
3171         raw_local_irq_save(flags);
3172         current->curr_chain_key = 0;
3173         current->lockdep_depth = 0;
3174         current->lockdep_recursion = 0;
3175         memset(current->held_locks, 0, MAX_LOCK_DEPTH*sizeof(struct held_lock));
3176         nr_hardirq_chains = 0;
3177         nr_softirq_chains = 0;
3178         nr_process_chains = 0;
3179         debug_locks = 1;
3180         for (i = 0; i < CHAINHASH_SIZE; i++)
3181                 INIT_LIST_HEAD(chainhash_table + i);
3182         raw_local_irq_restore(flags);
3183 }
3184
3185 static void zap_class(struct lock_class *class)
3186 {
3187         int i;
3188
3189         /*
3190          * Remove all dependencies this lock is
3191          * involved in:
3192          */
3193         for (i = 0; i < nr_list_entries; i++) {
3194                 if (list_entries[i].class == class)
3195                         list_del_rcu(&list_entries[i].entry);
3196         }
3197         /*
3198          * Unhash the class and remove it from the all_lock_classes list:
3199          */
3200         list_del_rcu(&class->hash_entry);
3201         list_del_rcu(&class->lock_entry);
3202
3203         class->key = NULL;
3204 }
3205
3206 static inline int within(const void *addr, void *start, unsigned long size)
3207 {
3208         return addr >= start && addr < start + size;
3209 }
3210
3211 void lockdep_free_key_range(void *start, unsigned long size)
3212 {
3213         struct lock_class *class, *next;
3214         struct list_head *head;
3215         unsigned long flags;
3216         int i;
3217         int locked;
3218
3219         raw_local_irq_save(flags);
3220         locked = graph_lock();
3221
3222         /*
3223          * Unhash all classes that were created by this module:
3224          */
3225         for (i = 0; i < CLASSHASH_SIZE; i++) {
3226                 head = classhash_table + i;
3227                 if (list_empty(head))
3228                         continue;
3229                 list_for_each_entry_safe(class, next, head, hash_entry) {
3230                         if (within(class->key, start, size))
3231                                 zap_class(class);
3232                         else if (within(class->name, start, size))
3233                                 zap_class(class);
3234                 }
3235         }
3236
3237         if (locked)
3238                 graph_unlock();
3239         raw_local_irq_restore(flags);
3240 }
3241
3242 void lockdep_reset_lock(struct lockdep_map *lock)
3243 {
3244         struct lock_class *class, *next;
3245         struct list_head *head;
3246         unsigned long flags;
3247         int i, j;
3248         int locked;
3249
3250         raw_local_irq_save(flags);
3251
3252         /*
3253          * Remove all classes this lock might have:
3254          */
3255         for (j = 0; j < MAX_LOCKDEP_SUBCLASSES; j++) {
3256                 /*
3257                  * If the class exists we look it up and zap it:
3258                  */
3259                 class = look_up_lock_class(lock, j);
3260                 if (class)
3261                         zap_class(class);
3262         }
3263         /*
3264          * Debug check: in the end all mapped classes should
3265          * be gone.
3266          */
3267         locked = graph_lock();
3268         for (i = 0; i < CLASSHASH_SIZE; i++) {
3269                 head = classhash_table + i;
3270                 if (list_empty(head))
3271                         continue;
3272                 list_for_each_entry_safe(class, next, head, hash_entry) {
3273                         if (unlikely(class == lock->class_cache)) {
3274                                 if (debug_locks_off_graph_unlock())
3275                                         WARN_ON(1);
3276                                 goto out_restore;
3277                         }
3278                 }
3279         }
3280         if (locked)
3281                 graph_unlock();
3282
3283 out_restore:
3284         raw_local_irq_restore(flags);
3285 }
3286
3287 void lockdep_init(void)
3288 {
3289         int i;
3290
3291         /*
3292          * Some architectures have their own start_kernel()
3293          * code which calls lockdep_init(), while we also
3294          * call lockdep_init() from the start_kernel() itself,
3295          * and we want to initialize the hashes only once:
3296          */
3297         if (lockdep_initialized)
3298                 return;
3299
3300         for (i = 0; i < CLASSHASH_SIZE; i++)
3301                 INIT_LIST_HEAD(classhash_table + i);
3302
3303         for (i = 0; i < CHAINHASH_SIZE; i++)
3304                 INIT_LIST_HEAD(chainhash_table + i);
3305
3306         lockdep_initialized = 1;
3307 }
3308
3309 void __init lockdep_info(void)
3310 {
3311         printk("Lock dependency validator: Copyright (c) 2006 Red Hat, Inc., Ingo Molnar\n");
3312
3313         printk("... MAX_LOCKDEP_SUBCLASSES:  %lu\n", MAX_LOCKDEP_SUBCLASSES);
3314         printk("... MAX_LOCK_DEPTH:          %lu\n", MAX_LOCK_DEPTH);
3315         printk("... MAX_LOCKDEP_KEYS:        %lu\n", MAX_LOCKDEP_KEYS);
3316         printk("... CLASSHASH_SIZE:          %lu\n", CLASSHASH_SIZE);
3317         printk("... MAX_LOCKDEP_ENTRIES:     %lu\n", MAX_LOCKDEP_ENTRIES);
3318         printk("... MAX_LOCKDEP_CHAINS:      %lu\n", MAX_LOCKDEP_CHAINS);
3319         printk("... CHAINHASH_SIZE:          %lu\n", CHAINHASH_SIZE);
3320
3321         printk(" memory used by lock dependency info: %lu kB\n",
3322                 (sizeof(struct lock_class) * MAX_LOCKDEP_KEYS +
3323                 sizeof(struct list_head) * CLASSHASH_SIZE +
3324                 sizeof(struct lock_list) * MAX_LOCKDEP_ENTRIES +
3325                 sizeof(struct lock_chain) * MAX_LOCKDEP_CHAINS +
3326                 sizeof(struct list_head) * CHAINHASH_SIZE) / 1024);
3327
3328         printk(" per task-struct memory footprint: %lu bytes\n",
3329                 sizeof(struct held_lock) * MAX_LOCK_DEPTH);
3330
3331 #ifdef CONFIG_DEBUG_LOCKDEP
3332         if (lockdep_init_error) {
3333                 printk("WARNING: lockdep init error! Arch code didn't call lockdep_init() early enough?\n");
3334                 printk("Call stack leading to lockdep invocation was:\n");
3335                 print_stack_trace(&lockdep_init_trace, 0);
3336         }
3337 #endif
3338 }
3339
3340 static void
3341 print_freed_lock_bug(struct task_struct *curr, const void *mem_from,
3342                      const void *mem_to, struct held_lock *hlock)
3343 {
3344         if (!debug_locks_off())
3345                 return;
3346         if (debug_locks_silent)
3347                 return;
3348
3349         printk("\n=========================\n");
3350         printk(  "[ BUG: held lock freed! ]\n");
3351         printk(  "-------------------------\n");
3352         printk("%s/%d is freeing memory %p-%p, with a lock still held there!\n",
3353                 curr->comm, task_pid_nr(curr), mem_from, mem_to-1);
3354         print_lock(hlock);
3355         lockdep_print_held_locks(curr);
3356
3357         printk("\nstack backtrace:\n");
3358         dump_stack();
3359 }
3360
3361 static inline int not_in_range(const void* mem_from, unsigned long mem_len,
3362                                 const void* lock_from, unsigned long lock_len)
3363 {
3364         return lock_from + lock_len <= mem_from ||
3365                 mem_from + mem_len <= lock_from;
3366 }
3367
3368 /*
3369  * Called when kernel memory is freed (or unmapped), or if a lock
3370  * is destroyed or reinitialized - this code checks whether there is
3371  * any held lock in the memory range of <from> to <to>:
3372  */
3373 void debug_check_no_locks_freed(const void *mem_from, unsigned long mem_len)
3374 {
3375         struct task_struct *curr = current;
3376         struct held_lock *hlock;
3377         unsigned long flags;
3378         int i;
3379
3380         if (unlikely(!debug_locks))
3381                 return;
3382
3383         local_irq_save(flags);
3384         for (i = 0; i < curr->lockdep_depth; i++) {
3385                 hlock = curr->held_locks + i;
3386
3387                 if (not_in_range(mem_from, mem_len, hlock->instance,
3388                                         sizeof(*hlock->instance)))
3389                         continue;
3390
3391                 print_freed_lock_bug(curr, mem_from, mem_from + mem_len, hlock);
3392                 break;
3393         }
3394         local_irq_restore(flags);
3395 }
3396 EXPORT_SYMBOL_GPL(debug_check_no_locks_freed);
3397
3398 static void print_held_locks_bug(struct task_struct *curr)
3399 {
3400         if (!debug_locks_off())
3401                 return;
3402         if (debug_locks_silent)
3403                 return;
3404
3405         printk("\n=====================================\n");
3406         printk(  "[ BUG: lock held at task exit time! ]\n");
3407         printk(  "-------------------------------------\n");
3408         printk("%s/%d is exiting with locks still held!\n",
3409                 curr->comm, task_pid_nr(curr));
3410         lockdep_print_held_locks(curr);
3411
3412         printk("\nstack backtrace:\n");
3413         dump_stack();
3414 }
3415
3416 void debug_check_no_locks_held(struct task_struct *task)
3417 {
3418         if (unlikely(task->lockdep_depth > 0))
3419                 print_held_locks_bug(task);
3420 }
3421
3422 void debug_show_all_locks(void)
3423 {
3424         struct task_struct *g, *p;
3425         int count = 10;
3426         int unlock = 1;
3427
3428         if (unlikely(!debug_locks)) {
3429                 printk("INFO: lockdep is turned off.\n");
3430                 return;
3431         }
3432         printk("\nShowing all locks held in the system:\n");
3433
3434         /*
3435          * Here we try to get the tasklist_lock as hard as possible,
3436          * if not successful after 2 seconds we ignore it (but keep
3437          * trying). This is to enable a debug printout even if a
3438          * tasklist_lock-holding task deadlocks or crashes.
3439          */
3440 retry:
3441         if (!read_trylock(&tasklist_lock)) {
3442                 if (count == 10)
3443                         printk("hm, tasklist_lock locked, retrying... ");
3444                 if (count) {
3445                         count--;
3446                         printk(" #%d", 10-count);
3447                         mdelay(200);
3448                         goto retry;
3449                 }
3450                 printk(" ignoring it.\n");
3451                 unlock = 0;
3452         } else {
3453                 if (count != 10)
3454                         printk(KERN_CONT " locked it.\n");
3455         }
3456
3457         do_each_thread(g, p) {
3458                 /*
3459                  * It's not reliable to print a task's held locks
3460                  * if it's not sleeping (or if it's not the current
3461                  * task):
3462                  */
3463                 if (p->state == TASK_RUNNING && p != current)
3464                         continue;
3465                 if (p->lockdep_depth)
3466                         lockdep_print_held_locks(p);
3467                 if (!unlock)
3468                         if (read_trylock(&tasklist_lock))
3469                                 unlock = 1;
3470         } while_each_thread(g, p);
3471
3472         printk("\n");
3473         printk("=============================================\n\n");
3474
3475         if (unlock)
3476                 read_unlock(&tasklist_lock);
3477 }
3478 EXPORT_SYMBOL_GPL(debug_show_all_locks);
3479
3480 /*
3481  * Careful: only use this function if you are sure that
3482  * the task cannot run in parallel!
3483  */
3484 void __debug_show_held_locks(struct task_struct *task)
3485 {
3486         if (unlikely(!debug_locks)) {
3487                 printk("INFO: lockdep is turned off.\n");
3488                 return;
3489         }
3490         lockdep_print_held_locks(task);
3491 }
3492 EXPORT_SYMBOL_GPL(__debug_show_held_locks);
3493
3494 void debug_show_held_locks(struct task_struct *task)
3495 {
3496                 __debug_show_held_locks(task);
3497 }
3498 EXPORT_SYMBOL_GPL(debug_show_held_locks);
3499
3500 void lockdep_sys_exit(void)
3501 {
3502         struct task_struct *curr = current;
3503
3504         if (unlikely(curr->lockdep_depth)) {
3505                 if (!debug_locks_off())
3506                         return;
3507                 printk("\n================================================\n");
3508                 printk(  "[ BUG: lock held when returning to user space! ]\n");
3509                 printk(  "------------------------------------------------\n");
3510                 printk("%s/%d is leaving the kernel with locks still held!\n",
3511                                 curr->comm, curr->pid);
3512                 lockdep_print_held_locks(curr);
3513         }
3514 }