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