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