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