]> git.ipfire.org Git - thirdparty/linux.git/blob - kernel/locking/lockdep.c
locking/lockdep: Make it easy to detect whether or not inside a selftest
[thirdparty/linux.git] / kernel / locking / 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
10 *
11 * this code maps all the lock dependencies as they occur in a live kernel
12 * and will warn about the following classes of locking bugs:
13 *
14 * - lock inversion scenarios
15 * - circular lock dependencies
16 * - hardirq/softirq safe/unsafe locking bugs
17 *
18 * Bugs are reported even if the current locking scenario does not cause
19 * any deadlock at this point.
20 *
21 * I.e. if anytime in the past two locks were taken in a different order,
22 * even if it happened for another task, even if those were different
23 * locks (but of the same class as this lock), this code will detect it.
24 *
25 * Thanks to Arjan van de Ven for coming up with the initial idea of
26 * mapping lock dependencies runtime.
27 */
28 #define DISABLE_BRANCH_PROFILING
29 #include <linux/mutex.h>
30 #include <linux/sched.h>
31 #include <linux/sched/clock.h>
32 #include <linux/sched/task.h>
33 #include <linux/sched/mm.h>
34 #include <linux/delay.h>
35 #include <linux/module.h>
36 #include <linux/proc_fs.h>
37 #include <linux/seq_file.h>
38 #include <linux/spinlock.h>
39 #include <linux/kallsyms.h>
40 #include <linux/interrupt.h>
41 #include <linux/stacktrace.h>
42 #include <linux/debug_locks.h>
43 #include <linux/irqflags.h>
44 #include <linux/utsname.h>
45 #include <linux/hash.h>
46 #include <linux/ftrace.h>
47 #include <linux/stringify.h>
48 #include <linux/bitops.h>
49 #include <linux/gfp.h>
50 #include <linux/random.h>
51 #include <linux/jhash.h>
52 #include <linux/nmi.h>
53
54 #include <asm/sections.h>
55
56 #include "lockdep_internals.h"
57
58 #define CREATE_TRACE_POINTS
59 #include <trace/events/lock.h>
60
61 #ifdef CONFIG_PROVE_LOCKING
62 int prove_locking = 1;
63 module_param(prove_locking, int, 0644);
64 #else
65 #define prove_locking 0
66 #endif
67
68 #ifdef CONFIG_LOCK_STAT
69 int lock_stat = 1;
70 module_param(lock_stat, int, 0644);
71 #else
72 #define lock_stat 0
73 #endif
74
75 /*
76 * lockdep_lock: protects the lockdep graph, the hashes and the
77 * class/list/hash allocators.
78 *
79 * This is one of the rare exceptions where it's justified
80 * to use a raw spinlock - we really dont want the spinlock
81 * code to recurse back into the lockdep code...
82 */
83 static arch_spinlock_t lockdep_lock = (arch_spinlock_t)__ARCH_SPIN_LOCK_UNLOCKED;
84 static struct task_struct *lockdep_selftest_task_struct;
85
86 static int graph_lock(void)
87 {
88 arch_spin_lock(&lockdep_lock);
89 /*
90 * Make sure that if another CPU detected a bug while
91 * walking the graph we dont change it (while the other
92 * CPU is busy printing out stuff with the graph lock
93 * dropped already)
94 */
95 if (!debug_locks) {
96 arch_spin_unlock(&lockdep_lock);
97 return 0;
98 }
99 /* prevent any recursions within lockdep from causing deadlocks */
100 current->lockdep_recursion++;
101 return 1;
102 }
103
104 static inline int graph_unlock(void)
105 {
106 if (debug_locks && !arch_spin_is_locked(&lockdep_lock)) {
107 /*
108 * The lockdep graph lock isn't locked while we expect it to
109 * be, we're confused now, bye!
110 */
111 return DEBUG_LOCKS_WARN_ON(1);
112 }
113
114 current->lockdep_recursion--;
115 arch_spin_unlock(&lockdep_lock);
116 return 0;
117 }
118
119 /*
120 * Turn lock debugging off and return with 0 if it was off already,
121 * and also release the graph lock:
122 */
123 static inline int debug_locks_off_graph_unlock(void)
124 {
125 int ret = debug_locks_off();
126
127 arch_spin_unlock(&lockdep_lock);
128
129 return ret;
130 }
131
132 unsigned long nr_list_entries;
133 static struct lock_list list_entries[MAX_LOCKDEP_ENTRIES];
134
135 /*
136 * All data structures here are protected by the global debug_lock.
137 *
138 * Mutex key structs only get allocated, once during bootup, and never
139 * get freed - this significantly simplifies the debugging code.
140 */
141 unsigned long nr_lock_classes;
142 #ifndef CONFIG_DEBUG_LOCKDEP
143 static
144 #endif
145 struct lock_class lock_classes[MAX_LOCKDEP_KEYS];
146
147 static inline struct lock_class *hlock_class(struct held_lock *hlock)
148 {
149 if (!hlock->class_idx) {
150 /*
151 * Someone passed in garbage, we give up.
152 */
153 DEBUG_LOCKS_WARN_ON(1);
154 return NULL;
155 }
156 return lock_classes + hlock->class_idx - 1;
157 }
158
159 #ifdef CONFIG_LOCK_STAT
160 static DEFINE_PER_CPU(struct lock_class_stats[MAX_LOCKDEP_KEYS], cpu_lock_stats);
161
162 static inline u64 lockstat_clock(void)
163 {
164 return local_clock();
165 }
166
167 static int lock_point(unsigned long points[], unsigned long ip)
168 {
169 int i;
170
171 for (i = 0; i < LOCKSTAT_POINTS; i++) {
172 if (points[i] == 0) {
173 points[i] = ip;
174 break;
175 }
176 if (points[i] == ip)
177 break;
178 }
179
180 return i;
181 }
182
183 static void lock_time_inc(struct lock_time *lt, u64 time)
184 {
185 if (time > lt->max)
186 lt->max = time;
187
188 if (time < lt->min || !lt->nr)
189 lt->min = time;
190
191 lt->total += time;
192 lt->nr++;
193 }
194
195 static inline void lock_time_add(struct lock_time *src, struct lock_time *dst)
196 {
197 if (!src->nr)
198 return;
199
200 if (src->max > dst->max)
201 dst->max = src->max;
202
203 if (src->min < dst->min || !dst->nr)
204 dst->min = src->min;
205
206 dst->total += src->total;
207 dst->nr += src->nr;
208 }
209
210 struct lock_class_stats lock_stats(struct lock_class *class)
211 {
212 struct lock_class_stats stats;
213 int cpu, i;
214
215 memset(&stats, 0, sizeof(struct lock_class_stats));
216 for_each_possible_cpu(cpu) {
217 struct lock_class_stats *pcs =
218 &per_cpu(cpu_lock_stats, cpu)[class - lock_classes];
219
220 for (i = 0; i < ARRAY_SIZE(stats.contention_point); i++)
221 stats.contention_point[i] += pcs->contention_point[i];
222
223 for (i = 0; i < ARRAY_SIZE(stats.contending_point); i++)
224 stats.contending_point[i] += pcs->contending_point[i];
225
226 lock_time_add(&pcs->read_waittime, &stats.read_waittime);
227 lock_time_add(&pcs->write_waittime, &stats.write_waittime);
228
229 lock_time_add(&pcs->read_holdtime, &stats.read_holdtime);
230 lock_time_add(&pcs->write_holdtime, &stats.write_holdtime);
231
232 for (i = 0; i < ARRAY_SIZE(stats.bounces); i++)
233 stats.bounces[i] += pcs->bounces[i];
234 }
235
236 return stats;
237 }
238
239 void clear_lock_stats(struct lock_class *class)
240 {
241 int cpu;
242
243 for_each_possible_cpu(cpu) {
244 struct lock_class_stats *cpu_stats =
245 &per_cpu(cpu_lock_stats, cpu)[class - lock_classes];
246
247 memset(cpu_stats, 0, sizeof(struct lock_class_stats));
248 }
249 memset(class->contention_point, 0, sizeof(class->contention_point));
250 memset(class->contending_point, 0, sizeof(class->contending_point));
251 }
252
253 static struct lock_class_stats *get_lock_stats(struct lock_class *class)
254 {
255 return &this_cpu_ptr(cpu_lock_stats)[class - lock_classes];
256 }
257
258 static void lock_release_holdtime(struct held_lock *hlock)
259 {
260 struct lock_class_stats *stats;
261 u64 holdtime;
262
263 if (!lock_stat)
264 return;
265
266 holdtime = lockstat_clock() - hlock->holdtime_stamp;
267
268 stats = get_lock_stats(hlock_class(hlock));
269 if (hlock->read)
270 lock_time_inc(&stats->read_holdtime, holdtime);
271 else
272 lock_time_inc(&stats->write_holdtime, holdtime);
273 }
274 #else
275 static inline void lock_release_holdtime(struct held_lock *hlock)
276 {
277 }
278 #endif
279
280 /*
281 * We keep a global list of all lock classes. The list only grows,
282 * never shrinks. The list is only accessed with the lockdep
283 * spinlock lock held.
284 */
285 LIST_HEAD(all_lock_classes);
286
287 /*
288 * The lockdep classes are in a hash-table as well, for fast lookup:
289 */
290 #define CLASSHASH_BITS (MAX_LOCKDEP_KEYS_BITS - 1)
291 #define CLASSHASH_SIZE (1UL << CLASSHASH_BITS)
292 #define __classhashfn(key) hash_long((unsigned long)key, CLASSHASH_BITS)
293 #define classhashentry(key) (classhash_table + __classhashfn((key)))
294
295 static struct hlist_head classhash_table[CLASSHASH_SIZE];
296
297 /*
298 * We put the lock dependency chains into a hash-table as well, to cache
299 * their existence:
300 */
301 #define CHAINHASH_BITS (MAX_LOCKDEP_CHAINS_BITS-1)
302 #define CHAINHASH_SIZE (1UL << CHAINHASH_BITS)
303 #define __chainhashfn(chain) hash_long(chain, CHAINHASH_BITS)
304 #define chainhashentry(chain) (chainhash_table + __chainhashfn((chain)))
305
306 static struct hlist_head chainhash_table[CHAINHASH_SIZE];
307
308 /*
309 * The hash key of the lock dependency chains is a hash itself too:
310 * it's a hash of all locks taken up to that lock, including that lock.
311 * It's a 64-bit hash, because it's important for the keys to be
312 * unique.
313 */
314 static inline u64 iterate_chain_key(u64 key, u32 idx)
315 {
316 u32 k0 = key, k1 = key >> 32;
317
318 __jhash_mix(idx, k0, k1); /* Macro that modifies arguments! */
319
320 return k0 | (u64)k1 << 32;
321 }
322
323 void lockdep_off(void)
324 {
325 current->lockdep_recursion++;
326 }
327 EXPORT_SYMBOL(lockdep_off);
328
329 void lockdep_on(void)
330 {
331 current->lockdep_recursion--;
332 }
333 EXPORT_SYMBOL(lockdep_on);
334
335 void lockdep_set_selftest_task(struct task_struct *task)
336 {
337 lockdep_selftest_task_struct = task;
338 }
339
340 /*
341 * Debugging switches:
342 */
343
344 #define VERBOSE 0
345 #define VERY_VERBOSE 0
346
347 #if VERBOSE
348 # define HARDIRQ_VERBOSE 1
349 # define SOFTIRQ_VERBOSE 1
350 #else
351 # define HARDIRQ_VERBOSE 0
352 # define SOFTIRQ_VERBOSE 0
353 #endif
354
355 #if VERBOSE || HARDIRQ_VERBOSE || SOFTIRQ_VERBOSE
356 /*
357 * Quick filtering for interesting events:
358 */
359 static int class_filter(struct lock_class *class)
360 {
361 #if 0
362 /* Example */
363 if (class->name_version == 1 &&
364 !strcmp(class->name, "lockname"))
365 return 1;
366 if (class->name_version == 1 &&
367 !strcmp(class->name, "&struct->lockfield"))
368 return 1;
369 #endif
370 /* Filter everything else. 1 would be to allow everything else */
371 return 0;
372 }
373 #endif
374
375 static int verbose(struct lock_class *class)
376 {
377 #if VERBOSE
378 return class_filter(class);
379 #endif
380 return 0;
381 }
382
383 /*
384 * Stack-trace: tightly packed array of stack backtrace
385 * addresses. Protected by the graph_lock.
386 */
387 unsigned long nr_stack_trace_entries;
388 static unsigned long stack_trace[MAX_STACK_TRACE_ENTRIES];
389
390 static void print_lockdep_off(const char *bug_msg)
391 {
392 printk(KERN_DEBUG "%s\n", bug_msg);
393 printk(KERN_DEBUG "turning off the locking correctness validator.\n");
394 #ifdef CONFIG_LOCK_STAT
395 printk(KERN_DEBUG "Please attach the output of /proc/lock_stat to the bug report\n");
396 #endif
397 }
398
399 static int save_trace(struct stack_trace *trace)
400 {
401 trace->nr_entries = 0;
402 trace->max_entries = MAX_STACK_TRACE_ENTRIES - nr_stack_trace_entries;
403 trace->entries = stack_trace + nr_stack_trace_entries;
404
405 trace->skip = 3;
406
407 save_stack_trace(trace);
408
409 /*
410 * Some daft arches put -1 at the end to indicate its a full trace.
411 *
412 * <rant> this is buggy anyway, since it takes a whole extra entry so a
413 * complete trace that maxes out the entries provided will be reported
414 * as incomplete, friggin useless </rant>
415 */
416 if (trace->nr_entries != 0 &&
417 trace->entries[trace->nr_entries-1] == ULONG_MAX)
418 trace->nr_entries--;
419
420 trace->max_entries = trace->nr_entries;
421
422 nr_stack_trace_entries += trace->nr_entries;
423
424 if (nr_stack_trace_entries >= MAX_STACK_TRACE_ENTRIES-1) {
425 if (!debug_locks_off_graph_unlock())
426 return 0;
427
428 print_lockdep_off("BUG: MAX_STACK_TRACE_ENTRIES too low!");
429 dump_stack();
430
431 return 0;
432 }
433
434 return 1;
435 }
436
437 unsigned int nr_hardirq_chains;
438 unsigned int nr_softirq_chains;
439 unsigned int nr_process_chains;
440 unsigned int max_lockdep_depth;
441
442 #ifdef CONFIG_DEBUG_LOCKDEP
443 /*
444 * Various lockdep statistics:
445 */
446 DEFINE_PER_CPU(struct lockdep_stats, lockdep_stats);
447 #endif
448
449 /*
450 * Locking printouts:
451 */
452
453 #define __USAGE(__STATE) \
454 [LOCK_USED_IN_##__STATE] = "IN-"__stringify(__STATE)"-W", \
455 [LOCK_ENABLED_##__STATE] = __stringify(__STATE)"-ON-W", \
456 [LOCK_USED_IN_##__STATE##_READ] = "IN-"__stringify(__STATE)"-R",\
457 [LOCK_ENABLED_##__STATE##_READ] = __stringify(__STATE)"-ON-R",
458
459 static const char *usage_str[] =
460 {
461 #define LOCKDEP_STATE(__STATE) __USAGE(__STATE)
462 #include "lockdep_states.h"
463 #undef LOCKDEP_STATE
464 [LOCK_USED] = "INITIAL USE",
465 };
466
467 const char * __get_key_name(struct lockdep_subclass_key *key, char *str)
468 {
469 return kallsyms_lookup((unsigned long)key, NULL, NULL, NULL, str);
470 }
471
472 static inline unsigned long lock_flag(enum lock_usage_bit bit)
473 {
474 return 1UL << bit;
475 }
476
477 static char get_usage_char(struct lock_class *class, enum lock_usage_bit bit)
478 {
479 char c = '.';
480
481 if (class->usage_mask & lock_flag(bit + 2))
482 c = '+';
483 if (class->usage_mask & lock_flag(bit)) {
484 c = '-';
485 if (class->usage_mask & lock_flag(bit + 2))
486 c = '?';
487 }
488
489 return c;
490 }
491
492 void get_usage_chars(struct lock_class *class, char usage[LOCK_USAGE_CHARS])
493 {
494 int i = 0;
495
496 #define LOCKDEP_STATE(__STATE) \
497 usage[i++] = get_usage_char(class, LOCK_USED_IN_##__STATE); \
498 usage[i++] = get_usage_char(class, LOCK_USED_IN_##__STATE##_READ);
499 #include "lockdep_states.h"
500 #undef LOCKDEP_STATE
501
502 usage[i] = '\0';
503 }
504
505 static void __print_lock_name(struct lock_class *class)
506 {
507 char str[KSYM_NAME_LEN];
508 const char *name;
509
510 name = class->name;
511 if (!name) {
512 name = __get_key_name(class->key, str);
513 printk(KERN_CONT "%s", name);
514 } else {
515 printk(KERN_CONT "%s", name);
516 if (class->name_version > 1)
517 printk(KERN_CONT "#%d", class->name_version);
518 if (class->subclass)
519 printk(KERN_CONT "/%d", class->subclass);
520 }
521 }
522
523 static void print_lock_name(struct lock_class *class)
524 {
525 char usage[LOCK_USAGE_CHARS];
526
527 get_usage_chars(class, usage);
528
529 printk(KERN_CONT " (");
530 __print_lock_name(class);
531 printk(KERN_CONT "){%s}", usage);
532 }
533
534 static void print_lockdep_cache(struct lockdep_map *lock)
535 {
536 const char *name;
537 char str[KSYM_NAME_LEN];
538
539 name = lock->name;
540 if (!name)
541 name = __get_key_name(lock->key->subkeys, str);
542
543 printk(KERN_CONT "%s", name);
544 }
545
546 static void print_lock(struct held_lock *hlock)
547 {
548 /*
549 * We can be called locklessly through debug_show_all_locks() so be
550 * extra careful, the hlock might have been released and cleared.
551 */
552 unsigned int class_idx = hlock->class_idx;
553
554 /* Don't re-read hlock->class_idx, can't use READ_ONCE() on bitfields: */
555 barrier();
556
557 if (!class_idx || (class_idx - 1) >= MAX_LOCKDEP_KEYS) {
558 printk(KERN_CONT "<RELEASED>\n");
559 return;
560 }
561
562 printk(KERN_CONT "%p", hlock->instance);
563 print_lock_name(lock_classes + class_idx - 1);
564 printk(KERN_CONT ", at: %pS\n", (void *)hlock->acquire_ip);
565 }
566
567 static void lockdep_print_held_locks(struct task_struct *p)
568 {
569 int i, depth = READ_ONCE(p->lockdep_depth);
570
571 if (!depth)
572 printk("no locks held by %s/%d.\n", p->comm, task_pid_nr(p));
573 else
574 printk("%d lock%s held by %s/%d:\n", depth,
575 depth > 1 ? "s" : "", p->comm, task_pid_nr(p));
576 /*
577 * It's not reliable to print a task's held locks if it's not sleeping
578 * and it's not the current task.
579 */
580 if (p->state == TASK_RUNNING && p != current)
581 return;
582 for (i = 0; i < depth; i++) {
583 printk(" #%d: ", i);
584 print_lock(p->held_locks + i);
585 }
586 }
587
588 static void print_kernel_ident(void)
589 {
590 printk("%s %.*s %s\n", init_utsname()->release,
591 (int)strcspn(init_utsname()->version, " "),
592 init_utsname()->version,
593 print_tainted());
594 }
595
596 static int very_verbose(struct lock_class *class)
597 {
598 #if VERY_VERBOSE
599 return class_filter(class);
600 #endif
601 return 0;
602 }
603
604 /*
605 * Is this the address of a static object:
606 */
607 #ifdef __KERNEL__
608 static int static_obj(void *obj)
609 {
610 unsigned long start = (unsigned long) &_stext,
611 end = (unsigned long) &_end,
612 addr = (unsigned long) obj;
613
614 /*
615 * static variable?
616 */
617 if ((addr >= start) && (addr < end))
618 return 1;
619
620 if (arch_is_kernel_data(addr))
621 return 1;
622
623 /*
624 * in-kernel percpu var?
625 */
626 if (is_kernel_percpu_address(addr))
627 return 1;
628
629 /*
630 * module static or percpu var?
631 */
632 return is_module_address(addr) || is_module_percpu_address(addr);
633 }
634 #endif
635
636 /*
637 * To make lock name printouts unique, we calculate a unique
638 * class->name_version generation counter. The caller must hold the graph
639 * lock.
640 */
641 static int count_matching_names(struct lock_class *new_class)
642 {
643 struct lock_class *class;
644 int count = 0;
645
646 if (!new_class->name)
647 return 0;
648
649 list_for_each_entry(class, &all_lock_classes, lock_entry) {
650 if (new_class->key - new_class->subclass == class->key)
651 return class->name_version;
652 if (class->name && !strcmp(class->name, new_class->name))
653 count = max(count, class->name_version);
654 }
655
656 return count + 1;
657 }
658
659 static inline struct lock_class *
660 look_up_lock_class(const struct lockdep_map *lock, unsigned int subclass)
661 {
662 struct lockdep_subclass_key *key;
663 struct hlist_head *hash_head;
664 struct lock_class *class;
665
666 if (unlikely(subclass >= MAX_LOCKDEP_SUBCLASSES)) {
667 debug_locks_off();
668 printk(KERN_ERR
669 "BUG: looking up invalid subclass: %u\n", subclass);
670 printk(KERN_ERR
671 "turning off the locking correctness validator.\n");
672 dump_stack();
673 return NULL;
674 }
675
676 /*
677 * If it is not initialised then it has never been locked,
678 * so it won't be present in the hash table.
679 */
680 if (unlikely(!lock->key))
681 return NULL;
682
683 /*
684 * NOTE: the class-key must be unique. For dynamic locks, a static
685 * lock_class_key variable is passed in through the mutex_init()
686 * (or spin_lock_init()) call - which acts as the key. For static
687 * locks we use the lock object itself as the key.
688 */
689 BUILD_BUG_ON(sizeof(struct lock_class_key) >
690 sizeof(struct lockdep_map));
691
692 key = lock->key->subkeys + subclass;
693
694 hash_head = classhashentry(key);
695
696 /*
697 * We do an RCU walk of the hash, see lockdep_free_key_range().
698 */
699 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
700 return NULL;
701
702 hlist_for_each_entry_rcu(class, hash_head, hash_entry) {
703 if (class->key == key) {
704 /*
705 * Huh! same key, different name? Did someone trample
706 * on some memory? We're most confused.
707 */
708 WARN_ON_ONCE(class->name != lock->name);
709 return class;
710 }
711 }
712
713 return NULL;
714 }
715
716 /*
717 * Static locks do not have their class-keys yet - for them the key is
718 * the lock object itself. If the lock is in the per cpu area, the
719 * canonical address of the lock (per cpu offset removed) is used.
720 */
721 static bool assign_lock_key(struct lockdep_map *lock)
722 {
723 unsigned long can_addr, addr = (unsigned long)lock;
724
725 if (__is_kernel_percpu_address(addr, &can_addr))
726 lock->key = (void *)can_addr;
727 else if (__is_module_percpu_address(addr, &can_addr))
728 lock->key = (void *)can_addr;
729 else if (static_obj(lock))
730 lock->key = (void *)lock;
731 else {
732 /* Debug-check: all keys must be persistent! */
733 debug_locks_off();
734 pr_err("INFO: trying to register non-static key.\n");
735 pr_err("the code is fine but needs lockdep annotation.\n");
736 pr_err("turning off the locking correctness validator.\n");
737 dump_stack();
738 return false;
739 }
740
741 return true;
742 }
743
744 /*
745 * Initialize the lock_classes[] array elements.
746 */
747 static void init_data_structures_once(void)
748 {
749 static bool initialization_happened;
750 int i;
751
752 if (likely(initialization_happened))
753 return;
754
755 initialization_happened = true;
756
757 for (i = 0; i < ARRAY_SIZE(lock_classes); i++) {
758 INIT_LIST_HEAD(&lock_classes[i].locks_after);
759 INIT_LIST_HEAD(&lock_classes[i].locks_before);
760 }
761 }
762
763 /*
764 * Register a lock's class in the hash-table, if the class is not present
765 * yet. Otherwise we look it up. We cache the result in the lock object
766 * itself, so actual lookup of the hash should be once per lock object.
767 */
768 static struct lock_class *
769 register_lock_class(struct lockdep_map *lock, unsigned int subclass, int force)
770 {
771 struct lockdep_subclass_key *key;
772 struct hlist_head *hash_head;
773 struct lock_class *class;
774
775 DEBUG_LOCKS_WARN_ON(!irqs_disabled());
776
777 class = look_up_lock_class(lock, subclass);
778 if (likely(class))
779 goto out_set_class_cache;
780
781 if (!lock->key) {
782 if (!assign_lock_key(lock))
783 return NULL;
784 } else if (!static_obj(lock->key)) {
785 return NULL;
786 }
787
788 key = lock->key->subkeys + subclass;
789 hash_head = classhashentry(key);
790
791 if (!graph_lock()) {
792 return NULL;
793 }
794 /*
795 * We have to do the hash-walk again, to avoid races
796 * with another CPU:
797 */
798 hlist_for_each_entry_rcu(class, hash_head, hash_entry) {
799 if (class->key == key)
800 goto out_unlock_set;
801 }
802
803 init_data_structures_once();
804
805 /*
806 * Allocate a new key from the static array, and add it to
807 * the hash:
808 */
809 if (nr_lock_classes >= MAX_LOCKDEP_KEYS) {
810 if (!debug_locks_off_graph_unlock()) {
811 return NULL;
812 }
813
814 print_lockdep_off("BUG: MAX_LOCKDEP_KEYS too low!");
815 dump_stack();
816 return NULL;
817 }
818 class = lock_classes + nr_lock_classes++;
819 debug_atomic_inc(nr_unused_locks);
820 class->key = key;
821 class->name = lock->name;
822 class->subclass = subclass;
823 WARN_ON_ONCE(!list_empty(&class->locks_before));
824 WARN_ON_ONCE(!list_empty(&class->locks_after));
825 class->name_version = count_matching_names(class);
826 /*
827 * We use RCU's safe list-add method to make
828 * parallel walking of the hash-list safe:
829 */
830 hlist_add_head_rcu(&class->hash_entry, hash_head);
831 /*
832 * Add it to the global list of classes:
833 */
834 list_add_tail(&class->lock_entry, &all_lock_classes);
835
836 if (verbose(class)) {
837 graph_unlock();
838
839 printk("\nnew class %px: %s", class->key, class->name);
840 if (class->name_version > 1)
841 printk(KERN_CONT "#%d", class->name_version);
842 printk(KERN_CONT "\n");
843 dump_stack();
844
845 if (!graph_lock()) {
846 return NULL;
847 }
848 }
849 out_unlock_set:
850 graph_unlock();
851
852 out_set_class_cache:
853 if (!subclass || force)
854 lock->class_cache[0] = class;
855 else if (subclass < NR_LOCKDEP_CACHING_CLASSES)
856 lock->class_cache[subclass] = class;
857
858 /*
859 * Hash collision, did we smoke some? We found a class with a matching
860 * hash but the subclass -- which is hashed in -- didn't match.
861 */
862 if (DEBUG_LOCKS_WARN_ON(class->subclass != subclass))
863 return NULL;
864
865 return class;
866 }
867
868 #ifdef CONFIG_PROVE_LOCKING
869 /*
870 * Allocate a lockdep entry. (assumes the graph_lock held, returns
871 * with NULL on failure)
872 */
873 static struct lock_list *alloc_list_entry(void)
874 {
875 if (nr_list_entries >= MAX_LOCKDEP_ENTRIES) {
876 if (!debug_locks_off_graph_unlock())
877 return NULL;
878
879 print_lockdep_off("BUG: MAX_LOCKDEP_ENTRIES too low!");
880 dump_stack();
881 return NULL;
882 }
883 return list_entries + nr_list_entries++;
884 }
885
886 /*
887 * Add a new dependency to the head of the list:
888 */
889 static int add_lock_to_list(struct lock_class *this,
890 struct lock_class *links_to, struct list_head *head,
891 unsigned long ip, int distance,
892 struct stack_trace *trace)
893 {
894 struct lock_list *entry;
895 /*
896 * Lock not present yet - get a new dependency struct and
897 * add it to the list:
898 */
899 entry = alloc_list_entry();
900 if (!entry)
901 return 0;
902
903 entry->class = this;
904 entry->links_to = links_to;
905 entry->distance = distance;
906 entry->trace = *trace;
907 /*
908 * Both allocation and removal are done under the graph lock; but
909 * iteration is under RCU-sched; see look_up_lock_class() and
910 * lockdep_free_key_range().
911 */
912 list_add_tail_rcu(&entry->entry, head);
913
914 return 1;
915 }
916
917 /*
918 * For good efficiency of modular, we use power of 2
919 */
920 #define MAX_CIRCULAR_QUEUE_SIZE 4096UL
921 #define CQ_MASK (MAX_CIRCULAR_QUEUE_SIZE-1)
922
923 /*
924 * The circular_queue and helpers is used to implement the
925 * breadth-first search(BFS)algorithem, by which we can build
926 * the shortest path from the next lock to be acquired to the
927 * previous held lock if there is a circular between them.
928 */
929 struct circular_queue {
930 unsigned long element[MAX_CIRCULAR_QUEUE_SIZE];
931 unsigned int front, rear;
932 };
933
934 static struct circular_queue lock_cq;
935
936 unsigned int max_bfs_queue_depth;
937
938 static unsigned int lockdep_dependency_gen_id;
939
940 static inline void __cq_init(struct circular_queue *cq)
941 {
942 cq->front = cq->rear = 0;
943 lockdep_dependency_gen_id++;
944 }
945
946 static inline int __cq_empty(struct circular_queue *cq)
947 {
948 return (cq->front == cq->rear);
949 }
950
951 static inline int __cq_full(struct circular_queue *cq)
952 {
953 return ((cq->rear + 1) & CQ_MASK) == cq->front;
954 }
955
956 static inline int __cq_enqueue(struct circular_queue *cq, unsigned long elem)
957 {
958 if (__cq_full(cq))
959 return -1;
960
961 cq->element[cq->rear] = elem;
962 cq->rear = (cq->rear + 1) & CQ_MASK;
963 return 0;
964 }
965
966 static inline int __cq_dequeue(struct circular_queue *cq, unsigned long *elem)
967 {
968 if (__cq_empty(cq))
969 return -1;
970
971 *elem = cq->element[cq->front];
972 cq->front = (cq->front + 1) & CQ_MASK;
973 return 0;
974 }
975
976 static inline unsigned int __cq_get_elem_count(struct circular_queue *cq)
977 {
978 return (cq->rear - cq->front) & CQ_MASK;
979 }
980
981 static inline void mark_lock_accessed(struct lock_list *lock,
982 struct lock_list *parent)
983 {
984 unsigned long nr;
985
986 nr = lock - list_entries;
987 WARN_ON(nr >= nr_list_entries); /* Out-of-bounds, input fail */
988 lock->parent = parent;
989 lock->class->dep_gen_id = lockdep_dependency_gen_id;
990 }
991
992 static inline unsigned long lock_accessed(struct lock_list *lock)
993 {
994 unsigned long nr;
995
996 nr = lock - list_entries;
997 WARN_ON(nr >= nr_list_entries); /* Out-of-bounds, input fail */
998 return lock->class->dep_gen_id == lockdep_dependency_gen_id;
999 }
1000
1001 static inline struct lock_list *get_lock_parent(struct lock_list *child)
1002 {
1003 return child->parent;
1004 }
1005
1006 static inline int get_lock_depth(struct lock_list *child)
1007 {
1008 int depth = 0;
1009 struct lock_list *parent;
1010
1011 while ((parent = get_lock_parent(child))) {
1012 child = parent;
1013 depth++;
1014 }
1015 return depth;
1016 }
1017
1018 static int __bfs(struct lock_list *source_entry,
1019 void *data,
1020 int (*match)(struct lock_list *entry, void *data),
1021 struct lock_list **target_entry,
1022 int forward)
1023 {
1024 struct lock_list *entry;
1025 struct list_head *head;
1026 struct circular_queue *cq = &lock_cq;
1027 int ret = 1;
1028
1029 if (match(source_entry, data)) {
1030 *target_entry = source_entry;
1031 ret = 0;
1032 goto exit;
1033 }
1034
1035 if (forward)
1036 head = &source_entry->class->locks_after;
1037 else
1038 head = &source_entry->class->locks_before;
1039
1040 if (list_empty(head))
1041 goto exit;
1042
1043 __cq_init(cq);
1044 __cq_enqueue(cq, (unsigned long)source_entry);
1045
1046 while (!__cq_empty(cq)) {
1047 struct lock_list *lock;
1048
1049 __cq_dequeue(cq, (unsigned long *)&lock);
1050
1051 if (!lock->class) {
1052 ret = -2;
1053 goto exit;
1054 }
1055
1056 if (forward)
1057 head = &lock->class->locks_after;
1058 else
1059 head = &lock->class->locks_before;
1060
1061 DEBUG_LOCKS_WARN_ON(!irqs_disabled());
1062
1063 list_for_each_entry_rcu(entry, head, entry) {
1064 if (!lock_accessed(entry)) {
1065 unsigned int cq_depth;
1066 mark_lock_accessed(entry, lock);
1067 if (match(entry, data)) {
1068 *target_entry = entry;
1069 ret = 0;
1070 goto exit;
1071 }
1072
1073 if (__cq_enqueue(cq, (unsigned long)entry)) {
1074 ret = -1;
1075 goto exit;
1076 }
1077 cq_depth = __cq_get_elem_count(cq);
1078 if (max_bfs_queue_depth < cq_depth)
1079 max_bfs_queue_depth = cq_depth;
1080 }
1081 }
1082 }
1083 exit:
1084 return ret;
1085 }
1086
1087 static inline int __bfs_forwards(struct lock_list *src_entry,
1088 void *data,
1089 int (*match)(struct lock_list *entry, void *data),
1090 struct lock_list **target_entry)
1091 {
1092 return __bfs(src_entry, data, match, target_entry, 1);
1093
1094 }
1095
1096 static inline int __bfs_backwards(struct lock_list *src_entry,
1097 void *data,
1098 int (*match)(struct lock_list *entry, void *data),
1099 struct lock_list **target_entry)
1100 {
1101 return __bfs(src_entry, data, match, target_entry, 0);
1102
1103 }
1104
1105 /*
1106 * Recursive, forwards-direction lock-dependency checking, used for
1107 * both noncyclic checking and for hardirq-unsafe/softirq-unsafe
1108 * checking.
1109 */
1110
1111 /*
1112 * Print a dependency chain entry (this is only done when a deadlock
1113 * has been detected):
1114 */
1115 static noinline int
1116 print_circular_bug_entry(struct lock_list *target, int depth)
1117 {
1118 if (debug_locks_silent)
1119 return 0;
1120 printk("\n-> #%u", depth);
1121 print_lock_name(target->class);
1122 printk(KERN_CONT ":\n");
1123 print_stack_trace(&target->trace, 6);
1124
1125 return 0;
1126 }
1127
1128 static void
1129 print_circular_lock_scenario(struct held_lock *src,
1130 struct held_lock *tgt,
1131 struct lock_list *prt)
1132 {
1133 struct lock_class *source = hlock_class(src);
1134 struct lock_class *target = hlock_class(tgt);
1135 struct lock_class *parent = prt->class;
1136
1137 /*
1138 * A direct locking problem where unsafe_class lock is taken
1139 * directly by safe_class lock, then all we need to show
1140 * is the deadlock scenario, as it is obvious that the
1141 * unsafe lock is taken under the safe lock.
1142 *
1143 * But if there is a chain instead, where the safe lock takes
1144 * an intermediate lock (middle_class) where this lock is
1145 * not the same as the safe lock, then the lock chain is
1146 * used to describe the problem. Otherwise we would need
1147 * to show a different CPU case for each link in the chain
1148 * from the safe_class lock to the unsafe_class lock.
1149 */
1150 if (parent != source) {
1151 printk("Chain exists of:\n ");
1152 __print_lock_name(source);
1153 printk(KERN_CONT " --> ");
1154 __print_lock_name(parent);
1155 printk(KERN_CONT " --> ");
1156 __print_lock_name(target);
1157 printk(KERN_CONT "\n\n");
1158 }
1159
1160 printk(" Possible unsafe locking scenario:\n\n");
1161 printk(" CPU0 CPU1\n");
1162 printk(" ---- ----\n");
1163 printk(" lock(");
1164 __print_lock_name(target);
1165 printk(KERN_CONT ");\n");
1166 printk(" lock(");
1167 __print_lock_name(parent);
1168 printk(KERN_CONT ");\n");
1169 printk(" lock(");
1170 __print_lock_name(target);
1171 printk(KERN_CONT ");\n");
1172 printk(" lock(");
1173 __print_lock_name(source);
1174 printk(KERN_CONT ");\n");
1175 printk("\n *** DEADLOCK ***\n\n");
1176 }
1177
1178 /*
1179 * When a circular dependency is detected, print the
1180 * header first:
1181 */
1182 static noinline int
1183 print_circular_bug_header(struct lock_list *entry, unsigned int depth,
1184 struct held_lock *check_src,
1185 struct held_lock *check_tgt)
1186 {
1187 struct task_struct *curr = current;
1188
1189 if (debug_locks_silent)
1190 return 0;
1191
1192 pr_warn("\n");
1193 pr_warn("======================================================\n");
1194 pr_warn("WARNING: possible circular locking dependency detected\n");
1195 print_kernel_ident();
1196 pr_warn("------------------------------------------------------\n");
1197 pr_warn("%s/%d is trying to acquire lock:\n",
1198 curr->comm, task_pid_nr(curr));
1199 print_lock(check_src);
1200
1201 pr_warn("\nbut task is already holding lock:\n");
1202
1203 print_lock(check_tgt);
1204 pr_warn("\nwhich lock already depends on the new lock.\n\n");
1205 pr_warn("\nthe existing dependency chain (in reverse order) is:\n");
1206
1207 print_circular_bug_entry(entry, depth);
1208
1209 return 0;
1210 }
1211
1212 static inline int class_equal(struct lock_list *entry, void *data)
1213 {
1214 return entry->class == data;
1215 }
1216
1217 static noinline int print_circular_bug(struct lock_list *this,
1218 struct lock_list *target,
1219 struct held_lock *check_src,
1220 struct held_lock *check_tgt,
1221 struct stack_trace *trace)
1222 {
1223 struct task_struct *curr = current;
1224 struct lock_list *parent;
1225 struct lock_list *first_parent;
1226 int depth;
1227
1228 if (!debug_locks_off_graph_unlock() || debug_locks_silent)
1229 return 0;
1230
1231 if (!save_trace(&this->trace))
1232 return 0;
1233
1234 depth = get_lock_depth(target);
1235
1236 print_circular_bug_header(target, depth, check_src, check_tgt);
1237
1238 parent = get_lock_parent(target);
1239 first_parent = parent;
1240
1241 while (parent) {
1242 print_circular_bug_entry(parent, --depth);
1243 parent = get_lock_parent(parent);
1244 }
1245
1246 printk("\nother info that might help us debug this:\n\n");
1247 print_circular_lock_scenario(check_src, check_tgt,
1248 first_parent);
1249
1250 lockdep_print_held_locks(curr);
1251
1252 printk("\nstack backtrace:\n");
1253 dump_stack();
1254
1255 return 0;
1256 }
1257
1258 static noinline int print_bfs_bug(int ret)
1259 {
1260 if (!debug_locks_off_graph_unlock())
1261 return 0;
1262
1263 /*
1264 * Breadth-first-search failed, graph got corrupted?
1265 */
1266 WARN(1, "lockdep bfs error:%d\n", ret);
1267
1268 return 0;
1269 }
1270
1271 static int noop_count(struct lock_list *entry, void *data)
1272 {
1273 (*(unsigned long *)data)++;
1274 return 0;
1275 }
1276
1277 static unsigned long __lockdep_count_forward_deps(struct lock_list *this)
1278 {
1279 unsigned long count = 0;
1280 struct lock_list *uninitialized_var(target_entry);
1281
1282 __bfs_forwards(this, (void *)&count, noop_count, &target_entry);
1283
1284 return count;
1285 }
1286 unsigned long lockdep_count_forward_deps(struct lock_class *class)
1287 {
1288 unsigned long ret, flags;
1289 struct lock_list this;
1290
1291 this.parent = NULL;
1292 this.class = class;
1293
1294 raw_local_irq_save(flags);
1295 arch_spin_lock(&lockdep_lock);
1296 ret = __lockdep_count_forward_deps(&this);
1297 arch_spin_unlock(&lockdep_lock);
1298 raw_local_irq_restore(flags);
1299
1300 return ret;
1301 }
1302
1303 static unsigned long __lockdep_count_backward_deps(struct lock_list *this)
1304 {
1305 unsigned long count = 0;
1306 struct lock_list *uninitialized_var(target_entry);
1307
1308 __bfs_backwards(this, (void *)&count, noop_count, &target_entry);
1309
1310 return count;
1311 }
1312
1313 unsigned long lockdep_count_backward_deps(struct lock_class *class)
1314 {
1315 unsigned long ret, flags;
1316 struct lock_list this;
1317
1318 this.parent = NULL;
1319 this.class = class;
1320
1321 raw_local_irq_save(flags);
1322 arch_spin_lock(&lockdep_lock);
1323 ret = __lockdep_count_backward_deps(&this);
1324 arch_spin_unlock(&lockdep_lock);
1325 raw_local_irq_restore(flags);
1326
1327 return ret;
1328 }
1329
1330 /*
1331 * Prove that the dependency graph starting at <entry> can not
1332 * lead to <target>. Print an error and return 0 if it does.
1333 */
1334 static noinline int
1335 check_noncircular(struct lock_list *root, struct lock_class *target,
1336 struct lock_list **target_entry)
1337 {
1338 int result;
1339
1340 debug_atomic_inc(nr_cyclic_checks);
1341
1342 result = __bfs_forwards(root, target, class_equal, target_entry);
1343
1344 return result;
1345 }
1346
1347 static noinline int
1348 check_redundant(struct lock_list *root, struct lock_class *target,
1349 struct lock_list **target_entry)
1350 {
1351 int result;
1352
1353 debug_atomic_inc(nr_redundant_checks);
1354
1355 result = __bfs_forwards(root, target, class_equal, target_entry);
1356
1357 return result;
1358 }
1359
1360 #if defined(CONFIG_TRACE_IRQFLAGS) && defined(CONFIG_PROVE_LOCKING)
1361 /*
1362 * Forwards and backwards subgraph searching, for the purposes of
1363 * proving that two subgraphs can be connected by a new dependency
1364 * without creating any illegal irq-safe -> irq-unsafe lock dependency.
1365 */
1366
1367 static inline int usage_match(struct lock_list *entry, void *bit)
1368 {
1369 return entry->class->usage_mask & (1 << (enum lock_usage_bit)bit);
1370 }
1371
1372
1373
1374 /*
1375 * Find a node in the forwards-direction dependency sub-graph starting
1376 * at @root->class that matches @bit.
1377 *
1378 * Return 0 if such a node exists in the subgraph, and put that node
1379 * into *@target_entry.
1380 *
1381 * Return 1 otherwise and keep *@target_entry unchanged.
1382 * Return <0 on error.
1383 */
1384 static int
1385 find_usage_forwards(struct lock_list *root, enum lock_usage_bit bit,
1386 struct lock_list **target_entry)
1387 {
1388 int result;
1389
1390 debug_atomic_inc(nr_find_usage_forwards_checks);
1391
1392 result = __bfs_forwards(root, (void *)bit, usage_match, target_entry);
1393
1394 return result;
1395 }
1396
1397 /*
1398 * Find a node in the backwards-direction dependency sub-graph starting
1399 * at @root->class that matches @bit.
1400 *
1401 * Return 0 if such a node exists in the subgraph, and put that node
1402 * into *@target_entry.
1403 *
1404 * Return 1 otherwise and keep *@target_entry unchanged.
1405 * Return <0 on error.
1406 */
1407 static int
1408 find_usage_backwards(struct lock_list *root, enum lock_usage_bit bit,
1409 struct lock_list **target_entry)
1410 {
1411 int result;
1412
1413 debug_atomic_inc(nr_find_usage_backwards_checks);
1414
1415 result = __bfs_backwards(root, (void *)bit, usage_match, target_entry);
1416
1417 return result;
1418 }
1419
1420 static void print_lock_class_header(struct lock_class *class, int depth)
1421 {
1422 int bit;
1423
1424 printk("%*s->", depth, "");
1425 print_lock_name(class);
1426 #ifdef CONFIG_DEBUG_LOCKDEP
1427 printk(KERN_CONT " ops: %lu", debug_class_ops_read(class));
1428 #endif
1429 printk(KERN_CONT " {\n");
1430
1431 for (bit = 0; bit < LOCK_USAGE_STATES; bit++) {
1432 if (class->usage_mask & (1 << bit)) {
1433 int len = depth;
1434
1435 len += printk("%*s %s", depth, "", usage_str[bit]);
1436 len += printk(KERN_CONT " at:\n");
1437 print_stack_trace(class->usage_traces + bit, len);
1438 }
1439 }
1440 printk("%*s }\n", depth, "");
1441
1442 printk("%*s ... key at: [<%px>] %pS\n",
1443 depth, "", class->key, class->key);
1444 }
1445
1446 /*
1447 * printk the shortest lock dependencies from @start to @end in reverse order:
1448 */
1449 static void __used
1450 print_shortest_lock_dependencies(struct lock_list *leaf,
1451 struct lock_list *root)
1452 {
1453 struct lock_list *entry = leaf;
1454 int depth;
1455
1456 /*compute depth from generated tree by BFS*/
1457 depth = get_lock_depth(leaf);
1458
1459 do {
1460 print_lock_class_header(entry->class, depth);
1461 printk("%*s ... acquired at:\n", depth, "");
1462 print_stack_trace(&entry->trace, 2);
1463 printk("\n");
1464
1465 if (depth == 0 && (entry != root)) {
1466 printk("lockdep:%s bad path found in chain graph\n", __func__);
1467 break;
1468 }
1469
1470 entry = get_lock_parent(entry);
1471 depth--;
1472 } while (entry && (depth >= 0));
1473
1474 return;
1475 }
1476
1477 static void
1478 print_irq_lock_scenario(struct lock_list *safe_entry,
1479 struct lock_list *unsafe_entry,
1480 struct lock_class *prev_class,
1481 struct lock_class *next_class)
1482 {
1483 struct lock_class *safe_class = safe_entry->class;
1484 struct lock_class *unsafe_class = unsafe_entry->class;
1485 struct lock_class *middle_class = prev_class;
1486
1487 if (middle_class == safe_class)
1488 middle_class = next_class;
1489
1490 /*
1491 * A direct locking problem where unsafe_class lock is taken
1492 * directly by safe_class lock, then all we need to show
1493 * is the deadlock scenario, as it is obvious that the
1494 * unsafe lock is taken under the safe lock.
1495 *
1496 * But if there is a chain instead, where the safe lock takes
1497 * an intermediate lock (middle_class) where this lock is
1498 * not the same as the safe lock, then the lock chain is
1499 * used to describe the problem. Otherwise we would need
1500 * to show a different CPU case for each link in the chain
1501 * from the safe_class lock to the unsafe_class lock.
1502 */
1503 if (middle_class != unsafe_class) {
1504 printk("Chain exists of:\n ");
1505 __print_lock_name(safe_class);
1506 printk(KERN_CONT " --> ");
1507 __print_lock_name(middle_class);
1508 printk(KERN_CONT " --> ");
1509 __print_lock_name(unsafe_class);
1510 printk(KERN_CONT "\n\n");
1511 }
1512
1513 printk(" Possible interrupt unsafe locking scenario:\n\n");
1514 printk(" CPU0 CPU1\n");
1515 printk(" ---- ----\n");
1516 printk(" lock(");
1517 __print_lock_name(unsafe_class);
1518 printk(KERN_CONT ");\n");
1519 printk(" local_irq_disable();\n");
1520 printk(" lock(");
1521 __print_lock_name(safe_class);
1522 printk(KERN_CONT ");\n");
1523 printk(" lock(");
1524 __print_lock_name(middle_class);
1525 printk(KERN_CONT ");\n");
1526 printk(" <Interrupt>\n");
1527 printk(" lock(");
1528 __print_lock_name(safe_class);
1529 printk(KERN_CONT ");\n");
1530 printk("\n *** DEADLOCK ***\n\n");
1531 }
1532
1533 static int
1534 print_bad_irq_dependency(struct task_struct *curr,
1535 struct lock_list *prev_root,
1536 struct lock_list *next_root,
1537 struct lock_list *backwards_entry,
1538 struct lock_list *forwards_entry,
1539 struct held_lock *prev,
1540 struct held_lock *next,
1541 enum lock_usage_bit bit1,
1542 enum lock_usage_bit bit2,
1543 const char *irqclass)
1544 {
1545 if (!debug_locks_off_graph_unlock() || debug_locks_silent)
1546 return 0;
1547
1548 pr_warn("\n");
1549 pr_warn("=====================================================\n");
1550 pr_warn("WARNING: %s-safe -> %s-unsafe lock order detected\n",
1551 irqclass, irqclass);
1552 print_kernel_ident();
1553 pr_warn("-----------------------------------------------------\n");
1554 pr_warn("%s/%d [HC%u[%lu]:SC%u[%lu]:HE%u:SE%u] is trying to acquire:\n",
1555 curr->comm, task_pid_nr(curr),
1556 curr->hardirq_context, hardirq_count() >> HARDIRQ_SHIFT,
1557 curr->softirq_context, softirq_count() >> SOFTIRQ_SHIFT,
1558 curr->hardirqs_enabled,
1559 curr->softirqs_enabled);
1560 print_lock(next);
1561
1562 pr_warn("\nand this task is already holding:\n");
1563 print_lock(prev);
1564 pr_warn("which would create a new lock dependency:\n");
1565 print_lock_name(hlock_class(prev));
1566 pr_cont(" ->");
1567 print_lock_name(hlock_class(next));
1568 pr_cont("\n");
1569
1570 pr_warn("\nbut this new dependency connects a %s-irq-safe lock:\n",
1571 irqclass);
1572 print_lock_name(backwards_entry->class);
1573 pr_warn("\n... which became %s-irq-safe at:\n", irqclass);
1574
1575 print_stack_trace(backwards_entry->class->usage_traces + bit1, 1);
1576
1577 pr_warn("\nto a %s-irq-unsafe lock:\n", irqclass);
1578 print_lock_name(forwards_entry->class);
1579 pr_warn("\n... which became %s-irq-unsafe at:\n", irqclass);
1580 pr_warn("...");
1581
1582 print_stack_trace(forwards_entry->class->usage_traces + bit2, 1);
1583
1584 pr_warn("\nother info that might help us debug this:\n\n");
1585 print_irq_lock_scenario(backwards_entry, forwards_entry,
1586 hlock_class(prev), hlock_class(next));
1587
1588 lockdep_print_held_locks(curr);
1589
1590 pr_warn("\nthe dependencies between %s-irq-safe lock and the holding lock:\n", irqclass);
1591 if (!save_trace(&prev_root->trace))
1592 return 0;
1593 print_shortest_lock_dependencies(backwards_entry, prev_root);
1594
1595 pr_warn("\nthe dependencies between the lock to be acquired");
1596 pr_warn(" and %s-irq-unsafe lock:\n", irqclass);
1597 if (!save_trace(&next_root->trace))
1598 return 0;
1599 print_shortest_lock_dependencies(forwards_entry, next_root);
1600
1601 pr_warn("\nstack backtrace:\n");
1602 dump_stack();
1603
1604 return 0;
1605 }
1606
1607 static int
1608 check_usage(struct task_struct *curr, struct held_lock *prev,
1609 struct held_lock *next, enum lock_usage_bit bit_backwards,
1610 enum lock_usage_bit bit_forwards, const char *irqclass)
1611 {
1612 int ret;
1613 struct lock_list this, that;
1614 struct lock_list *uninitialized_var(target_entry);
1615 struct lock_list *uninitialized_var(target_entry1);
1616
1617 this.parent = NULL;
1618
1619 this.class = hlock_class(prev);
1620 ret = find_usage_backwards(&this, bit_backwards, &target_entry);
1621 if (ret < 0)
1622 return print_bfs_bug(ret);
1623 if (ret == 1)
1624 return ret;
1625
1626 that.parent = NULL;
1627 that.class = hlock_class(next);
1628 ret = find_usage_forwards(&that, bit_forwards, &target_entry1);
1629 if (ret < 0)
1630 return print_bfs_bug(ret);
1631 if (ret == 1)
1632 return ret;
1633
1634 return print_bad_irq_dependency(curr, &this, &that,
1635 target_entry, target_entry1,
1636 prev, next,
1637 bit_backwards, bit_forwards, irqclass);
1638 }
1639
1640 static const char *state_names[] = {
1641 #define LOCKDEP_STATE(__STATE) \
1642 __stringify(__STATE),
1643 #include "lockdep_states.h"
1644 #undef LOCKDEP_STATE
1645 };
1646
1647 static const char *state_rnames[] = {
1648 #define LOCKDEP_STATE(__STATE) \
1649 __stringify(__STATE)"-READ",
1650 #include "lockdep_states.h"
1651 #undef LOCKDEP_STATE
1652 };
1653
1654 static inline const char *state_name(enum lock_usage_bit bit)
1655 {
1656 return (bit & LOCK_USAGE_READ_MASK) ? state_rnames[bit >> 2] : state_names[bit >> 2];
1657 }
1658
1659 static int exclusive_bit(int new_bit)
1660 {
1661 int state = new_bit & LOCK_USAGE_STATE_MASK;
1662 int dir = new_bit & LOCK_USAGE_DIR_MASK;
1663
1664 /*
1665 * keep state, bit flip the direction and strip read.
1666 */
1667 return state | (dir ^ LOCK_USAGE_DIR_MASK);
1668 }
1669
1670 static int check_irq_usage(struct task_struct *curr, struct held_lock *prev,
1671 struct held_lock *next, enum lock_usage_bit bit)
1672 {
1673 /*
1674 * Prove that the new dependency does not connect a hardirq-safe
1675 * lock with a hardirq-unsafe lock - to achieve this we search
1676 * the backwards-subgraph starting at <prev>, and the
1677 * forwards-subgraph starting at <next>:
1678 */
1679 if (!check_usage(curr, prev, next, bit,
1680 exclusive_bit(bit), state_name(bit)))
1681 return 0;
1682
1683 bit++; /* _READ */
1684
1685 /*
1686 * Prove that the new dependency does not connect a hardirq-safe-read
1687 * lock with a hardirq-unsafe lock - to achieve this we search
1688 * the backwards-subgraph starting at <prev>, and the
1689 * forwards-subgraph starting at <next>:
1690 */
1691 if (!check_usage(curr, prev, next, bit,
1692 exclusive_bit(bit), state_name(bit)))
1693 return 0;
1694
1695 return 1;
1696 }
1697
1698 static int
1699 check_prev_add_irq(struct task_struct *curr, struct held_lock *prev,
1700 struct held_lock *next)
1701 {
1702 #define LOCKDEP_STATE(__STATE) \
1703 if (!check_irq_usage(curr, prev, next, LOCK_USED_IN_##__STATE)) \
1704 return 0;
1705 #include "lockdep_states.h"
1706 #undef LOCKDEP_STATE
1707
1708 return 1;
1709 }
1710
1711 static void inc_chains(void)
1712 {
1713 if (current->hardirq_context)
1714 nr_hardirq_chains++;
1715 else {
1716 if (current->softirq_context)
1717 nr_softirq_chains++;
1718 else
1719 nr_process_chains++;
1720 }
1721 }
1722
1723 #else
1724
1725 static inline int
1726 check_prev_add_irq(struct task_struct *curr, struct held_lock *prev,
1727 struct held_lock *next)
1728 {
1729 return 1;
1730 }
1731
1732 static inline void inc_chains(void)
1733 {
1734 nr_process_chains++;
1735 }
1736
1737 #endif
1738
1739 static void
1740 print_deadlock_scenario(struct held_lock *nxt,
1741 struct held_lock *prv)
1742 {
1743 struct lock_class *next = hlock_class(nxt);
1744 struct lock_class *prev = hlock_class(prv);
1745
1746 printk(" Possible unsafe locking scenario:\n\n");
1747 printk(" CPU0\n");
1748 printk(" ----\n");
1749 printk(" lock(");
1750 __print_lock_name(prev);
1751 printk(KERN_CONT ");\n");
1752 printk(" lock(");
1753 __print_lock_name(next);
1754 printk(KERN_CONT ");\n");
1755 printk("\n *** DEADLOCK ***\n\n");
1756 printk(" May be due to missing lock nesting notation\n\n");
1757 }
1758
1759 static int
1760 print_deadlock_bug(struct task_struct *curr, struct held_lock *prev,
1761 struct held_lock *next)
1762 {
1763 if (!debug_locks_off_graph_unlock() || debug_locks_silent)
1764 return 0;
1765
1766 pr_warn("\n");
1767 pr_warn("============================================\n");
1768 pr_warn("WARNING: possible recursive locking detected\n");
1769 print_kernel_ident();
1770 pr_warn("--------------------------------------------\n");
1771 pr_warn("%s/%d is trying to acquire lock:\n",
1772 curr->comm, task_pid_nr(curr));
1773 print_lock(next);
1774 pr_warn("\nbut task is already holding lock:\n");
1775 print_lock(prev);
1776
1777 pr_warn("\nother info that might help us debug this:\n");
1778 print_deadlock_scenario(next, prev);
1779 lockdep_print_held_locks(curr);
1780
1781 pr_warn("\nstack backtrace:\n");
1782 dump_stack();
1783
1784 return 0;
1785 }
1786
1787 /*
1788 * Check whether we are holding such a class already.
1789 *
1790 * (Note that this has to be done separately, because the graph cannot
1791 * detect such classes of deadlocks.)
1792 *
1793 * Returns: 0 on deadlock detected, 1 on OK, 2 on recursive read
1794 */
1795 static int
1796 check_deadlock(struct task_struct *curr, struct held_lock *next,
1797 struct lockdep_map *next_instance, int read)
1798 {
1799 struct held_lock *prev;
1800 struct held_lock *nest = NULL;
1801 int i;
1802
1803 for (i = 0; i < curr->lockdep_depth; i++) {
1804 prev = curr->held_locks + i;
1805
1806 if (prev->instance == next->nest_lock)
1807 nest = prev;
1808
1809 if (hlock_class(prev) != hlock_class(next))
1810 continue;
1811
1812 /*
1813 * Allow read-after-read recursion of the same
1814 * lock class (i.e. read_lock(lock)+read_lock(lock)):
1815 */
1816 if ((read == 2) && prev->read)
1817 return 2;
1818
1819 /*
1820 * We're holding the nest_lock, which serializes this lock's
1821 * nesting behaviour.
1822 */
1823 if (nest)
1824 return 2;
1825
1826 return print_deadlock_bug(curr, prev, next);
1827 }
1828 return 1;
1829 }
1830
1831 /*
1832 * There was a chain-cache miss, and we are about to add a new dependency
1833 * to a previous lock. We recursively validate the following rules:
1834 *
1835 * - would the adding of the <prev> -> <next> dependency create a
1836 * circular dependency in the graph? [== circular deadlock]
1837 *
1838 * - does the new prev->next dependency connect any hardirq-safe lock
1839 * (in the full backwards-subgraph starting at <prev>) with any
1840 * hardirq-unsafe lock (in the full forwards-subgraph starting at
1841 * <next>)? [== illegal lock inversion with hardirq contexts]
1842 *
1843 * - does the new prev->next dependency connect any softirq-safe lock
1844 * (in the full backwards-subgraph starting at <prev>) with any
1845 * softirq-unsafe lock (in the full forwards-subgraph starting at
1846 * <next>)? [== illegal lock inversion with softirq contexts]
1847 *
1848 * any of these scenarios could lead to a deadlock.
1849 *
1850 * Then if all the validations pass, we add the forwards and backwards
1851 * dependency.
1852 */
1853 static int
1854 check_prev_add(struct task_struct *curr, struct held_lock *prev,
1855 struct held_lock *next, int distance, struct stack_trace *trace,
1856 int (*save)(struct stack_trace *trace))
1857 {
1858 struct lock_list *uninitialized_var(target_entry);
1859 struct lock_list *entry;
1860 struct lock_list this;
1861 int ret;
1862
1863 /*
1864 * Prove that the new <prev> -> <next> dependency would not
1865 * create a circular dependency in the graph. (We do this by
1866 * forward-recursing into the graph starting at <next>, and
1867 * checking whether we can reach <prev>.)
1868 *
1869 * We are using global variables to control the recursion, to
1870 * keep the stackframe size of the recursive functions low:
1871 */
1872 this.class = hlock_class(next);
1873 this.parent = NULL;
1874 ret = check_noncircular(&this, hlock_class(prev), &target_entry);
1875 if (unlikely(!ret)) {
1876 if (!trace->entries) {
1877 /*
1878 * If @save fails here, the printing might trigger
1879 * a WARN but because of the !nr_entries it should
1880 * not do bad things.
1881 */
1882 save(trace);
1883 }
1884 return print_circular_bug(&this, target_entry, next, prev, trace);
1885 }
1886 else if (unlikely(ret < 0))
1887 return print_bfs_bug(ret);
1888
1889 if (!check_prev_add_irq(curr, prev, next))
1890 return 0;
1891
1892 /*
1893 * For recursive read-locks we do all the dependency checks,
1894 * but we dont store read-triggered dependencies (only
1895 * write-triggered dependencies). This ensures that only the
1896 * write-side dependencies matter, and that if for example a
1897 * write-lock never takes any other locks, then the reads are
1898 * equivalent to a NOP.
1899 */
1900 if (next->read == 2 || prev->read == 2)
1901 return 1;
1902 /*
1903 * Is the <prev> -> <next> dependency already present?
1904 *
1905 * (this may occur even though this is a new chain: consider
1906 * e.g. the L1 -> L2 -> L3 -> L4 and the L5 -> L1 -> L2 -> L3
1907 * chains - the second one will be new, but L1 already has
1908 * L2 added to its dependency list, due to the first chain.)
1909 */
1910 list_for_each_entry(entry, &hlock_class(prev)->locks_after, entry) {
1911 if (entry->class == hlock_class(next)) {
1912 if (distance == 1)
1913 entry->distance = 1;
1914 return 1;
1915 }
1916 }
1917
1918 /*
1919 * Is the <prev> -> <next> link redundant?
1920 */
1921 this.class = hlock_class(prev);
1922 this.parent = NULL;
1923 ret = check_redundant(&this, hlock_class(next), &target_entry);
1924 if (!ret) {
1925 debug_atomic_inc(nr_redundant);
1926 return 2;
1927 }
1928 if (ret < 0)
1929 return print_bfs_bug(ret);
1930
1931
1932 if (!trace->entries && !save(trace))
1933 return 0;
1934
1935 /*
1936 * Ok, all validations passed, add the new lock
1937 * to the previous lock's dependency list:
1938 */
1939 ret = add_lock_to_list(hlock_class(next), hlock_class(prev),
1940 &hlock_class(prev)->locks_after,
1941 next->acquire_ip, distance, trace);
1942
1943 if (!ret)
1944 return 0;
1945
1946 ret = add_lock_to_list(hlock_class(prev), hlock_class(next),
1947 &hlock_class(next)->locks_before,
1948 next->acquire_ip, distance, trace);
1949 if (!ret)
1950 return 0;
1951
1952 return 2;
1953 }
1954
1955 /*
1956 * Add the dependency to all directly-previous locks that are 'relevant'.
1957 * The ones that are relevant are (in increasing distance from curr):
1958 * all consecutive trylock entries and the final non-trylock entry - or
1959 * the end of this context's lock-chain - whichever comes first.
1960 */
1961 static int
1962 check_prevs_add(struct task_struct *curr, struct held_lock *next)
1963 {
1964 int depth = curr->lockdep_depth;
1965 struct held_lock *hlock;
1966 struct stack_trace trace = {
1967 .nr_entries = 0,
1968 .max_entries = 0,
1969 .entries = NULL,
1970 .skip = 0,
1971 };
1972
1973 /*
1974 * Debugging checks.
1975 *
1976 * Depth must not be zero for a non-head lock:
1977 */
1978 if (!depth)
1979 goto out_bug;
1980 /*
1981 * At least two relevant locks must exist for this
1982 * to be a head:
1983 */
1984 if (curr->held_locks[depth].irq_context !=
1985 curr->held_locks[depth-1].irq_context)
1986 goto out_bug;
1987
1988 for (;;) {
1989 int distance = curr->lockdep_depth - depth + 1;
1990 hlock = curr->held_locks + depth - 1;
1991
1992 /*
1993 * Only non-recursive-read entries get new dependencies
1994 * added:
1995 */
1996 if (hlock->read != 2 && hlock->check) {
1997 int ret = check_prev_add(curr, hlock, next, distance, &trace, save_trace);
1998 if (!ret)
1999 return 0;
2000
2001 /*
2002 * Stop after the first non-trylock entry,
2003 * as non-trylock entries have added their
2004 * own direct dependencies already, so this
2005 * lock is connected to them indirectly:
2006 */
2007 if (!hlock->trylock)
2008 break;
2009 }
2010
2011 depth--;
2012 /*
2013 * End of lock-stack?
2014 */
2015 if (!depth)
2016 break;
2017 /*
2018 * Stop the search if we cross into another context:
2019 */
2020 if (curr->held_locks[depth].irq_context !=
2021 curr->held_locks[depth-1].irq_context)
2022 break;
2023 }
2024 return 1;
2025 out_bug:
2026 if (!debug_locks_off_graph_unlock())
2027 return 0;
2028
2029 /*
2030 * Clearly we all shouldn't be here, but since we made it we
2031 * can reliable say we messed up our state. See the above two
2032 * gotos for reasons why we could possibly end up here.
2033 */
2034 WARN_ON(1);
2035
2036 return 0;
2037 }
2038
2039 unsigned long nr_lock_chains;
2040 struct lock_chain lock_chains[MAX_LOCKDEP_CHAINS];
2041 int nr_chain_hlocks;
2042 static u16 chain_hlocks[MAX_LOCKDEP_CHAIN_HLOCKS];
2043
2044 struct lock_class *lock_chain_get_class(struct lock_chain *chain, int i)
2045 {
2046 return lock_classes + chain_hlocks[chain->base + i];
2047 }
2048
2049 /*
2050 * Returns the index of the first held_lock of the current chain
2051 */
2052 static inline int get_first_held_lock(struct task_struct *curr,
2053 struct held_lock *hlock)
2054 {
2055 int i;
2056 struct held_lock *hlock_curr;
2057
2058 for (i = curr->lockdep_depth - 1; i >= 0; i--) {
2059 hlock_curr = curr->held_locks + i;
2060 if (hlock_curr->irq_context != hlock->irq_context)
2061 break;
2062
2063 }
2064
2065 return ++i;
2066 }
2067
2068 #ifdef CONFIG_DEBUG_LOCKDEP
2069 /*
2070 * Returns the next chain_key iteration
2071 */
2072 static u64 print_chain_key_iteration(int class_idx, u64 chain_key)
2073 {
2074 u64 new_chain_key = iterate_chain_key(chain_key, class_idx);
2075
2076 printk(" class_idx:%d -> chain_key:%016Lx",
2077 class_idx,
2078 (unsigned long long)new_chain_key);
2079 return new_chain_key;
2080 }
2081
2082 static void
2083 print_chain_keys_held_locks(struct task_struct *curr, struct held_lock *hlock_next)
2084 {
2085 struct held_lock *hlock;
2086 u64 chain_key = 0;
2087 int depth = curr->lockdep_depth;
2088 int i;
2089
2090 printk("depth: %u\n", depth + 1);
2091 for (i = get_first_held_lock(curr, hlock_next); i < depth; i++) {
2092 hlock = curr->held_locks + i;
2093 chain_key = print_chain_key_iteration(hlock->class_idx, chain_key);
2094
2095 print_lock(hlock);
2096 }
2097
2098 print_chain_key_iteration(hlock_next->class_idx, chain_key);
2099 print_lock(hlock_next);
2100 }
2101
2102 static void print_chain_keys_chain(struct lock_chain *chain)
2103 {
2104 int i;
2105 u64 chain_key = 0;
2106 int class_id;
2107
2108 printk("depth: %u\n", chain->depth);
2109 for (i = 0; i < chain->depth; i++) {
2110 class_id = chain_hlocks[chain->base + i];
2111 chain_key = print_chain_key_iteration(class_id + 1, chain_key);
2112
2113 print_lock_name(lock_classes + class_id);
2114 printk("\n");
2115 }
2116 }
2117
2118 static void print_collision(struct task_struct *curr,
2119 struct held_lock *hlock_next,
2120 struct lock_chain *chain)
2121 {
2122 pr_warn("\n");
2123 pr_warn("============================\n");
2124 pr_warn("WARNING: chain_key collision\n");
2125 print_kernel_ident();
2126 pr_warn("----------------------------\n");
2127 pr_warn("%s/%d: ", current->comm, task_pid_nr(current));
2128 pr_warn("Hash chain already cached but the contents don't match!\n");
2129
2130 pr_warn("Held locks:");
2131 print_chain_keys_held_locks(curr, hlock_next);
2132
2133 pr_warn("Locks in cached chain:");
2134 print_chain_keys_chain(chain);
2135
2136 pr_warn("\nstack backtrace:\n");
2137 dump_stack();
2138 }
2139 #endif
2140
2141 /*
2142 * Checks whether the chain and the current held locks are consistent
2143 * in depth and also in content. If they are not it most likely means
2144 * that there was a collision during the calculation of the chain_key.
2145 * Returns: 0 not passed, 1 passed
2146 */
2147 static int check_no_collision(struct task_struct *curr,
2148 struct held_lock *hlock,
2149 struct lock_chain *chain)
2150 {
2151 #ifdef CONFIG_DEBUG_LOCKDEP
2152 int i, j, id;
2153
2154 i = get_first_held_lock(curr, hlock);
2155
2156 if (DEBUG_LOCKS_WARN_ON(chain->depth != curr->lockdep_depth - (i - 1))) {
2157 print_collision(curr, hlock, chain);
2158 return 0;
2159 }
2160
2161 for (j = 0; j < chain->depth - 1; j++, i++) {
2162 id = curr->held_locks[i].class_idx - 1;
2163
2164 if (DEBUG_LOCKS_WARN_ON(chain_hlocks[chain->base + j] != id)) {
2165 print_collision(curr, hlock, chain);
2166 return 0;
2167 }
2168 }
2169 #endif
2170 return 1;
2171 }
2172
2173 /*
2174 * Adds a dependency chain into chain hashtable. And must be called with
2175 * graph_lock held.
2176 *
2177 * Return 0 if fail, and graph_lock is released.
2178 * Return 1 if succeed, with graph_lock held.
2179 */
2180 static inline int add_chain_cache(struct task_struct *curr,
2181 struct held_lock *hlock,
2182 u64 chain_key)
2183 {
2184 struct lock_class *class = hlock_class(hlock);
2185 struct hlist_head *hash_head = chainhashentry(chain_key);
2186 struct lock_chain *chain;
2187 int i, j;
2188
2189 /*
2190 * Allocate a new chain entry from the static array, and add
2191 * it to the hash:
2192 */
2193
2194 /*
2195 * We might need to take the graph lock, ensure we've got IRQs
2196 * disabled to make this an IRQ-safe lock.. for recursion reasons
2197 * lockdep won't complain about its own locking errors.
2198 */
2199 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2200 return 0;
2201
2202 if (unlikely(nr_lock_chains >= MAX_LOCKDEP_CHAINS)) {
2203 if (!debug_locks_off_graph_unlock())
2204 return 0;
2205
2206 print_lockdep_off("BUG: MAX_LOCKDEP_CHAINS too low!");
2207 dump_stack();
2208 return 0;
2209 }
2210 chain = lock_chains + nr_lock_chains++;
2211 chain->chain_key = chain_key;
2212 chain->irq_context = hlock->irq_context;
2213 i = get_first_held_lock(curr, hlock);
2214 chain->depth = curr->lockdep_depth + 1 - i;
2215
2216 BUILD_BUG_ON((1UL << 24) <= ARRAY_SIZE(chain_hlocks));
2217 BUILD_BUG_ON((1UL << 6) <= ARRAY_SIZE(curr->held_locks));
2218 BUILD_BUG_ON((1UL << 8*sizeof(chain_hlocks[0])) <= ARRAY_SIZE(lock_classes));
2219
2220 if (likely(nr_chain_hlocks + chain->depth <= MAX_LOCKDEP_CHAIN_HLOCKS)) {
2221 chain->base = nr_chain_hlocks;
2222 for (j = 0; j < chain->depth - 1; j++, i++) {
2223 int lock_id = curr->held_locks[i].class_idx - 1;
2224 chain_hlocks[chain->base + j] = lock_id;
2225 }
2226 chain_hlocks[chain->base + j] = class - lock_classes;
2227 nr_chain_hlocks += chain->depth;
2228 } else {
2229 if (!debug_locks_off_graph_unlock())
2230 return 0;
2231
2232 print_lockdep_off("BUG: MAX_LOCKDEP_CHAIN_HLOCKS too low!");
2233 dump_stack();
2234 return 0;
2235 }
2236
2237 hlist_add_head_rcu(&chain->entry, hash_head);
2238 debug_atomic_inc(chain_lookup_misses);
2239 inc_chains();
2240
2241 return 1;
2242 }
2243
2244 /*
2245 * Look up a dependency chain.
2246 */
2247 static inline struct lock_chain *lookup_chain_cache(u64 chain_key)
2248 {
2249 struct hlist_head *hash_head = chainhashentry(chain_key);
2250 struct lock_chain *chain;
2251
2252 /*
2253 * We can walk it lock-free, because entries only get added
2254 * to the hash:
2255 */
2256 hlist_for_each_entry_rcu(chain, hash_head, entry) {
2257 if (chain->chain_key == chain_key) {
2258 debug_atomic_inc(chain_lookup_hits);
2259 return chain;
2260 }
2261 }
2262 return NULL;
2263 }
2264
2265 /*
2266 * If the key is not present yet in dependency chain cache then
2267 * add it and return 1 - in this case the new dependency chain is
2268 * validated. If the key is already hashed, return 0.
2269 * (On return with 1 graph_lock is held.)
2270 */
2271 static inline int lookup_chain_cache_add(struct task_struct *curr,
2272 struct held_lock *hlock,
2273 u64 chain_key)
2274 {
2275 struct lock_class *class = hlock_class(hlock);
2276 struct lock_chain *chain = lookup_chain_cache(chain_key);
2277
2278 if (chain) {
2279 cache_hit:
2280 if (!check_no_collision(curr, hlock, chain))
2281 return 0;
2282
2283 if (very_verbose(class)) {
2284 printk("\nhash chain already cached, key: "
2285 "%016Lx tail class: [%px] %s\n",
2286 (unsigned long long)chain_key,
2287 class->key, class->name);
2288 }
2289
2290 return 0;
2291 }
2292
2293 if (very_verbose(class)) {
2294 printk("\nnew hash chain, key: %016Lx tail class: [%px] %s\n",
2295 (unsigned long long)chain_key, class->key, class->name);
2296 }
2297
2298 if (!graph_lock())
2299 return 0;
2300
2301 /*
2302 * We have to walk the chain again locked - to avoid duplicates:
2303 */
2304 chain = lookup_chain_cache(chain_key);
2305 if (chain) {
2306 graph_unlock();
2307 goto cache_hit;
2308 }
2309
2310 if (!add_chain_cache(curr, hlock, chain_key))
2311 return 0;
2312
2313 return 1;
2314 }
2315
2316 static int validate_chain(struct task_struct *curr, struct lockdep_map *lock,
2317 struct held_lock *hlock, int chain_head, u64 chain_key)
2318 {
2319 /*
2320 * Trylock needs to maintain the stack of held locks, but it
2321 * does not add new dependencies, because trylock can be done
2322 * in any order.
2323 *
2324 * We look up the chain_key and do the O(N^2) check and update of
2325 * the dependencies only if this is a new dependency chain.
2326 * (If lookup_chain_cache_add() return with 1 it acquires
2327 * graph_lock for us)
2328 */
2329 if (!hlock->trylock && hlock->check &&
2330 lookup_chain_cache_add(curr, hlock, chain_key)) {
2331 /*
2332 * Check whether last held lock:
2333 *
2334 * - is irq-safe, if this lock is irq-unsafe
2335 * - is softirq-safe, if this lock is hardirq-unsafe
2336 *
2337 * And check whether the new lock's dependency graph
2338 * could lead back to the previous lock.
2339 *
2340 * any of these scenarios could lead to a deadlock. If
2341 * All validations
2342 */
2343 int ret = check_deadlock(curr, hlock, lock, hlock->read);
2344
2345 if (!ret)
2346 return 0;
2347 /*
2348 * Mark recursive read, as we jump over it when
2349 * building dependencies (just like we jump over
2350 * trylock entries):
2351 */
2352 if (ret == 2)
2353 hlock->read = 2;
2354 /*
2355 * Add dependency only if this lock is not the head
2356 * of the chain, and if it's not a secondary read-lock:
2357 */
2358 if (!chain_head && ret != 2) {
2359 if (!check_prevs_add(curr, hlock))
2360 return 0;
2361 }
2362
2363 graph_unlock();
2364 } else {
2365 /* after lookup_chain_cache_add(): */
2366 if (unlikely(!debug_locks))
2367 return 0;
2368 }
2369
2370 return 1;
2371 }
2372 #else
2373 static inline int validate_chain(struct task_struct *curr,
2374 struct lockdep_map *lock, struct held_lock *hlock,
2375 int chain_head, u64 chain_key)
2376 {
2377 return 1;
2378 }
2379 #endif
2380
2381 /*
2382 * We are building curr_chain_key incrementally, so double-check
2383 * it from scratch, to make sure that it's done correctly:
2384 */
2385 static void check_chain_key(struct task_struct *curr)
2386 {
2387 #ifdef CONFIG_DEBUG_LOCKDEP
2388 struct held_lock *hlock, *prev_hlock = NULL;
2389 unsigned int i;
2390 u64 chain_key = 0;
2391
2392 for (i = 0; i < curr->lockdep_depth; i++) {
2393 hlock = curr->held_locks + i;
2394 if (chain_key != hlock->prev_chain_key) {
2395 debug_locks_off();
2396 /*
2397 * We got mighty confused, our chain keys don't match
2398 * with what we expect, someone trample on our task state?
2399 */
2400 WARN(1, "hm#1, depth: %u [%u], %016Lx != %016Lx\n",
2401 curr->lockdep_depth, i,
2402 (unsigned long long)chain_key,
2403 (unsigned long long)hlock->prev_chain_key);
2404 return;
2405 }
2406 /*
2407 * Whoops ran out of static storage again?
2408 */
2409 if (DEBUG_LOCKS_WARN_ON(hlock->class_idx > MAX_LOCKDEP_KEYS))
2410 return;
2411
2412 if (prev_hlock && (prev_hlock->irq_context !=
2413 hlock->irq_context))
2414 chain_key = 0;
2415 chain_key = iterate_chain_key(chain_key, hlock->class_idx);
2416 prev_hlock = hlock;
2417 }
2418 if (chain_key != curr->curr_chain_key) {
2419 debug_locks_off();
2420 /*
2421 * More smoking hash instead of calculating it, damn see these
2422 * numbers float.. I bet that a pink elephant stepped on my memory.
2423 */
2424 WARN(1, "hm#2, depth: %u [%u], %016Lx != %016Lx\n",
2425 curr->lockdep_depth, i,
2426 (unsigned long long)chain_key,
2427 (unsigned long long)curr->curr_chain_key);
2428 }
2429 #endif
2430 }
2431
2432 static void
2433 print_usage_bug_scenario(struct held_lock *lock)
2434 {
2435 struct lock_class *class = hlock_class(lock);
2436
2437 printk(" Possible unsafe locking scenario:\n\n");
2438 printk(" CPU0\n");
2439 printk(" ----\n");
2440 printk(" lock(");
2441 __print_lock_name(class);
2442 printk(KERN_CONT ");\n");
2443 printk(" <Interrupt>\n");
2444 printk(" lock(");
2445 __print_lock_name(class);
2446 printk(KERN_CONT ");\n");
2447 printk("\n *** DEADLOCK ***\n\n");
2448 }
2449
2450 static int
2451 print_usage_bug(struct task_struct *curr, struct held_lock *this,
2452 enum lock_usage_bit prev_bit, enum lock_usage_bit new_bit)
2453 {
2454 if (!debug_locks_off_graph_unlock() || debug_locks_silent)
2455 return 0;
2456
2457 pr_warn("\n");
2458 pr_warn("================================\n");
2459 pr_warn("WARNING: inconsistent lock state\n");
2460 print_kernel_ident();
2461 pr_warn("--------------------------------\n");
2462
2463 pr_warn("inconsistent {%s} -> {%s} usage.\n",
2464 usage_str[prev_bit], usage_str[new_bit]);
2465
2466 pr_warn("%s/%d [HC%u[%lu]:SC%u[%lu]:HE%u:SE%u] takes:\n",
2467 curr->comm, task_pid_nr(curr),
2468 trace_hardirq_context(curr), hardirq_count() >> HARDIRQ_SHIFT,
2469 trace_softirq_context(curr), softirq_count() >> SOFTIRQ_SHIFT,
2470 trace_hardirqs_enabled(curr),
2471 trace_softirqs_enabled(curr));
2472 print_lock(this);
2473
2474 pr_warn("{%s} state was registered at:\n", usage_str[prev_bit]);
2475 print_stack_trace(hlock_class(this)->usage_traces + prev_bit, 1);
2476
2477 print_irqtrace_events(curr);
2478 pr_warn("\nother info that might help us debug this:\n");
2479 print_usage_bug_scenario(this);
2480
2481 lockdep_print_held_locks(curr);
2482
2483 pr_warn("\nstack backtrace:\n");
2484 dump_stack();
2485
2486 return 0;
2487 }
2488
2489 /*
2490 * Print out an error if an invalid bit is set:
2491 */
2492 static inline int
2493 valid_state(struct task_struct *curr, struct held_lock *this,
2494 enum lock_usage_bit new_bit, enum lock_usage_bit bad_bit)
2495 {
2496 if (unlikely(hlock_class(this)->usage_mask & (1 << bad_bit)))
2497 return print_usage_bug(curr, this, bad_bit, new_bit);
2498 return 1;
2499 }
2500
2501 static int mark_lock(struct task_struct *curr, struct held_lock *this,
2502 enum lock_usage_bit new_bit);
2503
2504 #if defined(CONFIG_TRACE_IRQFLAGS) && defined(CONFIG_PROVE_LOCKING)
2505
2506 /*
2507 * print irq inversion bug:
2508 */
2509 static int
2510 print_irq_inversion_bug(struct task_struct *curr,
2511 struct lock_list *root, struct lock_list *other,
2512 struct held_lock *this, int forwards,
2513 const char *irqclass)
2514 {
2515 struct lock_list *entry = other;
2516 struct lock_list *middle = NULL;
2517 int depth;
2518
2519 if (!debug_locks_off_graph_unlock() || debug_locks_silent)
2520 return 0;
2521
2522 pr_warn("\n");
2523 pr_warn("========================================================\n");
2524 pr_warn("WARNING: possible irq lock inversion dependency detected\n");
2525 print_kernel_ident();
2526 pr_warn("--------------------------------------------------------\n");
2527 pr_warn("%s/%d just changed the state of lock:\n",
2528 curr->comm, task_pid_nr(curr));
2529 print_lock(this);
2530 if (forwards)
2531 pr_warn("but this lock took another, %s-unsafe lock in the past:\n", irqclass);
2532 else
2533 pr_warn("but this lock was taken by another, %s-safe lock in the past:\n", irqclass);
2534 print_lock_name(other->class);
2535 pr_warn("\n\nand interrupts could create inverse lock ordering between them.\n\n");
2536
2537 pr_warn("\nother info that might help us debug this:\n");
2538
2539 /* Find a middle lock (if one exists) */
2540 depth = get_lock_depth(other);
2541 do {
2542 if (depth == 0 && (entry != root)) {
2543 pr_warn("lockdep:%s bad path found in chain graph\n", __func__);
2544 break;
2545 }
2546 middle = entry;
2547 entry = get_lock_parent(entry);
2548 depth--;
2549 } while (entry && entry != root && (depth >= 0));
2550 if (forwards)
2551 print_irq_lock_scenario(root, other,
2552 middle ? middle->class : root->class, other->class);
2553 else
2554 print_irq_lock_scenario(other, root,
2555 middle ? middle->class : other->class, root->class);
2556
2557 lockdep_print_held_locks(curr);
2558
2559 pr_warn("\nthe shortest dependencies between 2nd lock and 1st lock:\n");
2560 if (!save_trace(&root->trace))
2561 return 0;
2562 print_shortest_lock_dependencies(other, root);
2563
2564 pr_warn("\nstack backtrace:\n");
2565 dump_stack();
2566
2567 return 0;
2568 }
2569
2570 /*
2571 * Prove that in the forwards-direction subgraph starting at <this>
2572 * there is no lock matching <mask>:
2573 */
2574 static int
2575 check_usage_forwards(struct task_struct *curr, struct held_lock *this,
2576 enum lock_usage_bit bit, const char *irqclass)
2577 {
2578 int ret;
2579 struct lock_list root;
2580 struct lock_list *uninitialized_var(target_entry);
2581
2582 root.parent = NULL;
2583 root.class = hlock_class(this);
2584 ret = find_usage_forwards(&root, bit, &target_entry);
2585 if (ret < 0)
2586 return print_bfs_bug(ret);
2587 if (ret == 1)
2588 return ret;
2589
2590 return print_irq_inversion_bug(curr, &root, target_entry,
2591 this, 1, irqclass);
2592 }
2593
2594 /*
2595 * Prove that in the backwards-direction subgraph starting at <this>
2596 * there is no lock matching <mask>:
2597 */
2598 static int
2599 check_usage_backwards(struct task_struct *curr, struct held_lock *this,
2600 enum lock_usage_bit bit, const char *irqclass)
2601 {
2602 int ret;
2603 struct lock_list root;
2604 struct lock_list *uninitialized_var(target_entry);
2605
2606 root.parent = NULL;
2607 root.class = hlock_class(this);
2608 ret = find_usage_backwards(&root, bit, &target_entry);
2609 if (ret < 0)
2610 return print_bfs_bug(ret);
2611 if (ret == 1)
2612 return ret;
2613
2614 return print_irq_inversion_bug(curr, &root, target_entry,
2615 this, 0, irqclass);
2616 }
2617
2618 void print_irqtrace_events(struct task_struct *curr)
2619 {
2620 printk("irq event stamp: %u\n", curr->irq_events);
2621 printk("hardirqs last enabled at (%u): [<%px>] %pS\n",
2622 curr->hardirq_enable_event, (void *)curr->hardirq_enable_ip,
2623 (void *)curr->hardirq_enable_ip);
2624 printk("hardirqs last disabled at (%u): [<%px>] %pS\n",
2625 curr->hardirq_disable_event, (void *)curr->hardirq_disable_ip,
2626 (void *)curr->hardirq_disable_ip);
2627 printk("softirqs last enabled at (%u): [<%px>] %pS\n",
2628 curr->softirq_enable_event, (void *)curr->softirq_enable_ip,
2629 (void *)curr->softirq_enable_ip);
2630 printk("softirqs last disabled at (%u): [<%px>] %pS\n",
2631 curr->softirq_disable_event, (void *)curr->softirq_disable_ip,
2632 (void *)curr->softirq_disable_ip);
2633 }
2634
2635 static int HARDIRQ_verbose(struct lock_class *class)
2636 {
2637 #if HARDIRQ_VERBOSE
2638 return class_filter(class);
2639 #endif
2640 return 0;
2641 }
2642
2643 static int SOFTIRQ_verbose(struct lock_class *class)
2644 {
2645 #if SOFTIRQ_VERBOSE
2646 return class_filter(class);
2647 #endif
2648 return 0;
2649 }
2650
2651 #define STRICT_READ_CHECKS 1
2652
2653 static int (*state_verbose_f[])(struct lock_class *class) = {
2654 #define LOCKDEP_STATE(__STATE) \
2655 __STATE##_verbose,
2656 #include "lockdep_states.h"
2657 #undef LOCKDEP_STATE
2658 };
2659
2660 static inline int state_verbose(enum lock_usage_bit bit,
2661 struct lock_class *class)
2662 {
2663 return state_verbose_f[bit >> 2](class);
2664 }
2665
2666 typedef int (*check_usage_f)(struct task_struct *, struct held_lock *,
2667 enum lock_usage_bit bit, const char *name);
2668
2669 static int
2670 mark_lock_irq(struct task_struct *curr, struct held_lock *this,
2671 enum lock_usage_bit new_bit)
2672 {
2673 int excl_bit = exclusive_bit(new_bit);
2674 int read = new_bit & LOCK_USAGE_READ_MASK;
2675 int dir = new_bit & LOCK_USAGE_DIR_MASK;
2676
2677 /*
2678 * mark USED_IN has to look forwards -- to ensure no dependency
2679 * has ENABLED state, which would allow recursion deadlocks.
2680 *
2681 * mark ENABLED has to look backwards -- to ensure no dependee
2682 * has USED_IN state, which, again, would allow recursion deadlocks.
2683 */
2684 check_usage_f usage = dir ?
2685 check_usage_backwards : check_usage_forwards;
2686
2687 /*
2688 * Validate that this particular lock does not have conflicting
2689 * usage states.
2690 */
2691 if (!valid_state(curr, this, new_bit, excl_bit))
2692 return 0;
2693
2694 /*
2695 * Validate that the lock dependencies don't have conflicting usage
2696 * states.
2697 */
2698 if ((!read || !dir || STRICT_READ_CHECKS) &&
2699 !usage(curr, this, excl_bit, state_name(new_bit & ~LOCK_USAGE_READ_MASK)))
2700 return 0;
2701
2702 /*
2703 * Check for read in write conflicts
2704 */
2705 if (!read) {
2706 if (!valid_state(curr, this, new_bit, excl_bit + LOCK_USAGE_READ_MASK))
2707 return 0;
2708
2709 if (STRICT_READ_CHECKS &&
2710 !usage(curr, this, excl_bit + LOCK_USAGE_READ_MASK,
2711 state_name(new_bit + LOCK_USAGE_READ_MASK)))
2712 return 0;
2713 }
2714
2715 if (state_verbose(new_bit, hlock_class(this)))
2716 return 2;
2717
2718 return 1;
2719 }
2720
2721 /*
2722 * Mark all held locks with a usage bit:
2723 */
2724 static int
2725 mark_held_locks(struct task_struct *curr, enum lock_usage_bit base_bit)
2726 {
2727 struct held_lock *hlock;
2728 int i;
2729
2730 for (i = 0; i < curr->lockdep_depth; i++) {
2731 enum lock_usage_bit hlock_bit = base_bit;
2732 hlock = curr->held_locks + i;
2733
2734 if (hlock->read)
2735 hlock_bit += LOCK_USAGE_READ_MASK;
2736
2737 BUG_ON(hlock_bit >= LOCK_USAGE_STATES);
2738
2739 if (!hlock->check)
2740 continue;
2741
2742 if (!mark_lock(curr, hlock, hlock_bit))
2743 return 0;
2744 }
2745
2746 return 1;
2747 }
2748
2749 /*
2750 * Hardirqs will be enabled:
2751 */
2752 static void __trace_hardirqs_on_caller(unsigned long ip)
2753 {
2754 struct task_struct *curr = current;
2755
2756 /* we'll do an OFF -> ON transition: */
2757 curr->hardirqs_enabled = 1;
2758
2759 /*
2760 * We are going to turn hardirqs on, so set the
2761 * usage bit for all held locks:
2762 */
2763 if (!mark_held_locks(curr, LOCK_ENABLED_HARDIRQ))
2764 return;
2765 /*
2766 * If we have softirqs enabled, then set the usage
2767 * bit for all held locks. (disabled hardirqs prevented
2768 * this bit from being set before)
2769 */
2770 if (curr->softirqs_enabled)
2771 if (!mark_held_locks(curr, LOCK_ENABLED_SOFTIRQ))
2772 return;
2773
2774 curr->hardirq_enable_ip = ip;
2775 curr->hardirq_enable_event = ++curr->irq_events;
2776 debug_atomic_inc(hardirqs_on_events);
2777 }
2778
2779 void lockdep_hardirqs_on(unsigned long ip)
2780 {
2781 if (unlikely(!debug_locks || current->lockdep_recursion))
2782 return;
2783
2784 if (unlikely(current->hardirqs_enabled)) {
2785 /*
2786 * Neither irq nor preemption are disabled here
2787 * so this is racy by nature but losing one hit
2788 * in a stat is not a big deal.
2789 */
2790 __debug_atomic_inc(redundant_hardirqs_on);
2791 return;
2792 }
2793
2794 /*
2795 * We're enabling irqs and according to our state above irqs weren't
2796 * already enabled, yet we find the hardware thinks they are in fact
2797 * enabled.. someone messed up their IRQ state tracing.
2798 */
2799 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2800 return;
2801
2802 /*
2803 * See the fine text that goes along with this variable definition.
2804 */
2805 if (DEBUG_LOCKS_WARN_ON(unlikely(early_boot_irqs_disabled)))
2806 return;
2807
2808 /*
2809 * Can't allow enabling interrupts while in an interrupt handler,
2810 * that's general bad form and such. Recursion, limited stack etc..
2811 */
2812 if (DEBUG_LOCKS_WARN_ON(current->hardirq_context))
2813 return;
2814
2815 current->lockdep_recursion = 1;
2816 __trace_hardirqs_on_caller(ip);
2817 current->lockdep_recursion = 0;
2818 }
2819
2820 /*
2821 * Hardirqs were disabled:
2822 */
2823 void lockdep_hardirqs_off(unsigned long ip)
2824 {
2825 struct task_struct *curr = current;
2826
2827 if (unlikely(!debug_locks || current->lockdep_recursion))
2828 return;
2829
2830 /*
2831 * So we're supposed to get called after you mask local IRQs, but for
2832 * some reason the hardware doesn't quite think you did a proper job.
2833 */
2834 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2835 return;
2836
2837 if (curr->hardirqs_enabled) {
2838 /*
2839 * We have done an ON -> OFF transition:
2840 */
2841 curr->hardirqs_enabled = 0;
2842 curr->hardirq_disable_ip = ip;
2843 curr->hardirq_disable_event = ++curr->irq_events;
2844 debug_atomic_inc(hardirqs_off_events);
2845 } else
2846 debug_atomic_inc(redundant_hardirqs_off);
2847 }
2848
2849 /*
2850 * Softirqs will be enabled:
2851 */
2852 void trace_softirqs_on(unsigned long ip)
2853 {
2854 struct task_struct *curr = current;
2855
2856 if (unlikely(!debug_locks || current->lockdep_recursion))
2857 return;
2858
2859 /*
2860 * We fancy IRQs being disabled here, see softirq.c, avoids
2861 * funny state and nesting things.
2862 */
2863 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2864 return;
2865
2866 if (curr->softirqs_enabled) {
2867 debug_atomic_inc(redundant_softirqs_on);
2868 return;
2869 }
2870
2871 current->lockdep_recursion = 1;
2872 /*
2873 * We'll do an OFF -> ON transition:
2874 */
2875 curr->softirqs_enabled = 1;
2876 curr->softirq_enable_ip = ip;
2877 curr->softirq_enable_event = ++curr->irq_events;
2878 debug_atomic_inc(softirqs_on_events);
2879 /*
2880 * We are going to turn softirqs on, so set the
2881 * usage bit for all held locks, if hardirqs are
2882 * enabled too:
2883 */
2884 if (curr->hardirqs_enabled)
2885 mark_held_locks(curr, LOCK_ENABLED_SOFTIRQ);
2886 current->lockdep_recursion = 0;
2887 }
2888
2889 /*
2890 * Softirqs were disabled:
2891 */
2892 void trace_softirqs_off(unsigned long ip)
2893 {
2894 struct task_struct *curr = current;
2895
2896 if (unlikely(!debug_locks || current->lockdep_recursion))
2897 return;
2898
2899 /*
2900 * We fancy IRQs being disabled here, see softirq.c
2901 */
2902 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2903 return;
2904
2905 if (curr->softirqs_enabled) {
2906 /*
2907 * We have done an ON -> OFF transition:
2908 */
2909 curr->softirqs_enabled = 0;
2910 curr->softirq_disable_ip = ip;
2911 curr->softirq_disable_event = ++curr->irq_events;
2912 debug_atomic_inc(softirqs_off_events);
2913 /*
2914 * Whoops, we wanted softirqs off, so why aren't they?
2915 */
2916 DEBUG_LOCKS_WARN_ON(!softirq_count());
2917 } else
2918 debug_atomic_inc(redundant_softirqs_off);
2919 }
2920
2921 static int mark_irqflags(struct task_struct *curr, struct held_lock *hlock)
2922 {
2923 /*
2924 * If non-trylock use in a hardirq or softirq context, then
2925 * mark the lock as used in these contexts:
2926 */
2927 if (!hlock->trylock) {
2928 if (hlock->read) {
2929 if (curr->hardirq_context)
2930 if (!mark_lock(curr, hlock,
2931 LOCK_USED_IN_HARDIRQ_READ))
2932 return 0;
2933 if (curr->softirq_context)
2934 if (!mark_lock(curr, hlock,
2935 LOCK_USED_IN_SOFTIRQ_READ))
2936 return 0;
2937 } else {
2938 if (curr->hardirq_context)
2939 if (!mark_lock(curr, hlock, LOCK_USED_IN_HARDIRQ))
2940 return 0;
2941 if (curr->softirq_context)
2942 if (!mark_lock(curr, hlock, LOCK_USED_IN_SOFTIRQ))
2943 return 0;
2944 }
2945 }
2946 if (!hlock->hardirqs_off) {
2947 if (hlock->read) {
2948 if (!mark_lock(curr, hlock,
2949 LOCK_ENABLED_HARDIRQ_READ))
2950 return 0;
2951 if (curr->softirqs_enabled)
2952 if (!mark_lock(curr, hlock,
2953 LOCK_ENABLED_SOFTIRQ_READ))
2954 return 0;
2955 } else {
2956 if (!mark_lock(curr, hlock,
2957 LOCK_ENABLED_HARDIRQ))
2958 return 0;
2959 if (curr->softirqs_enabled)
2960 if (!mark_lock(curr, hlock,
2961 LOCK_ENABLED_SOFTIRQ))
2962 return 0;
2963 }
2964 }
2965
2966 return 1;
2967 }
2968
2969 static inline unsigned int task_irq_context(struct task_struct *task)
2970 {
2971 return 2 * !!task->hardirq_context + !!task->softirq_context;
2972 }
2973
2974 static int separate_irq_context(struct task_struct *curr,
2975 struct held_lock *hlock)
2976 {
2977 unsigned int depth = curr->lockdep_depth;
2978
2979 /*
2980 * Keep track of points where we cross into an interrupt context:
2981 */
2982 if (depth) {
2983 struct held_lock *prev_hlock;
2984
2985 prev_hlock = curr->held_locks + depth-1;
2986 /*
2987 * If we cross into another context, reset the
2988 * hash key (this also prevents the checking and the
2989 * adding of the dependency to 'prev'):
2990 */
2991 if (prev_hlock->irq_context != hlock->irq_context)
2992 return 1;
2993 }
2994 return 0;
2995 }
2996
2997 #else /* defined(CONFIG_TRACE_IRQFLAGS) && defined(CONFIG_PROVE_LOCKING) */
2998
2999 static inline
3000 int mark_lock_irq(struct task_struct *curr, struct held_lock *this,
3001 enum lock_usage_bit new_bit)
3002 {
3003 WARN_ON(1); /* Impossible innit? when we don't have TRACE_IRQFLAG */
3004 return 1;
3005 }
3006
3007 static inline int mark_irqflags(struct task_struct *curr,
3008 struct held_lock *hlock)
3009 {
3010 return 1;
3011 }
3012
3013 static inline unsigned int task_irq_context(struct task_struct *task)
3014 {
3015 return 0;
3016 }
3017
3018 static inline int separate_irq_context(struct task_struct *curr,
3019 struct held_lock *hlock)
3020 {
3021 return 0;
3022 }
3023
3024 #endif /* defined(CONFIG_TRACE_IRQFLAGS) && defined(CONFIG_PROVE_LOCKING) */
3025
3026 /*
3027 * Mark a lock with a usage bit, and validate the state transition:
3028 */
3029 static int mark_lock(struct task_struct *curr, struct held_lock *this,
3030 enum lock_usage_bit new_bit)
3031 {
3032 unsigned int new_mask = 1 << new_bit, ret = 1;
3033
3034 /*
3035 * If already set then do not dirty the cacheline,
3036 * nor do any checks:
3037 */
3038 if (likely(hlock_class(this)->usage_mask & new_mask))
3039 return 1;
3040
3041 if (!graph_lock())
3042 return 0;
3043 /*
3044 * Make sure we didn't race:
3045 */
3046 if (unlikely(hlock_class(this)->usage_mask & new_mask)) {
3047 graph_unlock();
3048 return 1;
3049 }
3050
3051 hlock_class(this)->usage_mask |= new_mask;
3052
3053 if (!save_trace(hlock_class(this)->usage_traces + new_bit))
3054 return 0;
3055
3056 switch (new_bit) {
3057 #define LOCKDEP_STATE(__STATE) \
3058 case LOCK_USED_IN_##__STATE: \
3059 case LOCK_USED_IN_##__STATE##_READ: \
3060 case LOCK_ENABLED_##__STATE: \
3061 case LOCK_ENABLED_##__STATE##_READ:
3062 #include "lockdep_states.h"
3063 #undef LOCKDEP_STATE
3064 ret = mark_lock_irq(curr, this, new_bit);
3065 if (!ret)
3066 return 0;
3067 break;
3068 case LOCK_USED:
3069 debug_atomic_dec(nr_unused_locks);
3070 break;
3071 default:
3072 if (!debug_locks_off_graph_unlock())
3073 return 0;
3074 WARN_ON(1);
3075 return 0;
3076 }
3077
3078 graph_unlock();
3079
3080 /*
3081 * We must printk outside of the graph_lock:
3082 */
3083 if (ret == 2) {
3084 printk("\nmarked lock as {%s}:\n", usage_str[new_bit]);
3085 print_lock(this);
3086 print_irqtrace_events(curr);
3087 dump_stack();
3088 }
3089
3090 return ret;
3091 }
3092
3093 /*
3094 * Initialize a lock instance's lock-class mapping info:
3095 */
3096 void lockdep_init_map(struct lockdep_map *lock, const char *name,
3097 struct lock_class_key *key, int subclass)
3098 {
3099 int i;
3100
3101 for (i = 0; i < NR_LOCKDEP_CACHING_CLASSES; i++)
3102 lock->class_cache[i] = NULL;
3103
3104 #ifdef CONFIG_LOCK_STAT
3105 lock->cpu = raw_smp_processor_id();
3106 #endif
3107
3108 /*
3109 * Can't be having no nameless bastards around this place!
3110 */
3111 if (DEBUG_LOCKS_WARN_ON(!name)) {
3112 lock->name = "NULL";
3113 return;
3114 }
3115
3116 lock->name = name;
3117
3118 /*
3119 * No key, no joy, we need to hash something.
3120 */
3121 if (DEBUG_LOCKS_WARN_ON(!key))
3122 return;
3123 /*
3124 * Sanity check, the lock-class key must be persistent:
3125 */
3126 if (!static_obj(key)) {
3127 printk("BUG: key %px not in .data!\n", key);
3128 /*
3129 * What it says above ^^^^^, I suggest you read it.
3130 */
3131 DEBUG_LOCKS_WARN_ON(1);
3132 return;
3133 }
3134 lock->key = key;
3135
3136 if (unlikely(!debug_locks))
3137 return;
3138
3139 if (subclass) {
3140 unsigned long flags;
3141
3142 if (DEBUG_LOCKS_WARN_ON(current->lockdep_recursion))
3143 return;
3144
3145 raw_local_irq_save(flags);
3146 current->lockdep_recursion = 1;
3147 register_lock_class(lock, subclass, 1);
3148 current->lockdep_recursion = 0;
3149 raw_local_irq_restore(flags);
3150 }
3151 }
3152 EXPORT_SYMBOL_GPL(lockdep_init_map);
3153
3154 struct lock_class_key __lockdep_no_validate__;
3155 EXPORT_SYMBOL_GPL(__lockdep_no_validate__);
3156
3157 static int
3158 print_lock_nested_lock_not_held(struct task_struct *curr,
3159 struct held_lock *hlock,
3160 unsigned long ip)
3161 {
3162 if (!debug_locks_off())
3163 return 0;
3164 if (debug_locks_silent)
3165 return 0;
3166
3167 pr_warn("\n");
3168 pr_warn("==================================\n");
3169 pr_warn("WARNING: Nested lock was not taken\n");
3170 print_kernel_ident();
3171 pr_warn("----------------------------------\n");
3172
3173 pr_warn("%s/%d is trying to lock:\n", curr->comm, task_pid_nr(curr));
3174 print_lock(hlock);
3175
3176 pr_warn("\nbut this task is not holding:\n");
3177 pr_warn("%s\n", hlock->nest_lock->name);
3178
3179 pr_warn("\nstack backtrace:\n");
3180 dump_stack();
3181
3182 pr_warn("\nother info that might help us debug this:\n");
3183 lockdep_print_held_locks(curr);
3184
3185 pr_warn("\nstack backtrace:\n");
3186 dump_stack();
3187
3188 return 0;
3189 }
3190
3191 static int __lock_is_held(const struct lockdep_map *lock, int read);
3192
3193 /*
3194 * This gets called for every mutex_lock*()/spin_lock*() operation.
3195 * We maintain the dependency maps and validate the locking attempt:
3196 *
3197 * The callers must make sure that IRQs are disabled before calling it,
3198 * otherwise we could get an interrupt which would want to take locks,
3199 * which would end up in lockdep again.
3200 */
3201 static int __lock_acquire(struct lockdep_map *lock, unsigned int subclass,
3202 int trylock, int read, int check, int hardirqs_off,
3203 struct lockdep_map *nest_lock, unsigned long ip,
3204 int references, int pin_count)
3205 {
3206 struct task_struct *curr = current;
3207 struct lock_class *class = NULL;
3208 struct held_lock *hlock;
3209 unsigned int depth;
3210 int chain_head = 0;
3211 int class_idx;
3212 u64 chain_key;
3213
3214 if (unlikely(!debug_locks))
3215 return 0;
3216
3217 if (!prove_locking || lock->key == &__lockdep_no_validate__)
3218 check = 0;
3219
3220 if (subclass < NR_LOCKDEP_CACHING_CLASSES)
3221 class = lock->class_cache[subclass];
3222 /*
3223 * Not cached?
3224 */
3225 if (unlikely(!class)) {
3226 class = register_lock_class(lock, subclass, 0);
3227 if (!class)
3228 return 0;
3229 }
3230
3231 debug_class_ops_inc(class);
3232
3233 if (very_verbose(class)) {
3234 printk("\nacquire class [%px] %s", class->key, class->name);
3235 if (class->name_version > 1)
3236 printk(KERN_CONT "#%d", class->name_version);
3237 printk(KERN_CONT "\n");
3238 dump_stack();
3239 }
3240
3241 /*
3242 * Add the lock to the list of currently held locks.
3243 * (we dont increase the depth just yet, up until the
3244 * dependency checks are done)
3245 */
3246 depth = curr->lockdep_depth;
3247 /*
3248 * Ran out of static storage for our per-task lock stack again have we?
3249 */
3250 if (DEBUG_LOCKS_WARN_ON(depth >= MAX_LOCK_DEPTH))
3251 return 0;
3252
3253 class_idx = class - lock_classes + 1;
3254
3255 if (depth) {
3256 hlock = curr->held_locks + depth - 1;
3257 if (hlock->class_idx == class_idx && nest_lock) {
3258 if (hlock->references) {
3259 /*
3260 * Check: unsigned int references:12, overflow.
3261 */
3262 if (DEBUG_LOCKS_WARN_ON(hlock->references == (1 << 12)-1))
3263 return 0;
3264
3265 hlock->references++;
3266 } else {
3267 hlock->references = 2;
3268 }
3269
3270 return 1;
3271 }
3272 }
3273
3274 hlock = curr->held_locks + depth;
3275 /*
3276 * Plain impossible, we just registered it and checked it weren't no
3277 * NULL like.. I bet this mushroom I ate was good!
3278 */
3279 if (DEBUG_LOCKS_WARN_ON(!class))
3280 return 0;
3281 hlock->class_idx = class_idx;
3282 hlock->acquire_ip = ip;
3283 hlock->instance = lock;
3284 hlock->nest_lock = nest_lock;
3285 hlock->irq_context = task_irq_context(curr);
3286 hlock->trylock = trylock;
3287 hlock->read = read;
3288 hlock->check = check;
3289 hlock->hardirqs_off = !!hardirqs_off;
3290 hlock->references = references;
3291 #ifdef CONFIG_LOCK_STAT
3292 hlock->waittime_stamp = 0;
3293 hlock->holdtime_stamp = lockstat_clock();
3294 #endif
3295 hlock->pin_count = pin_count;
3296
3297 if (check && !mark_irqflags(curr, hlock))
3298 return 0;
3299
3300 /* mark it as used: */
3301 if (!mark_lock(curr, hlock, LOCK_USED))
3302 return 0;
3303
3304 /*
3305 * Calculate the chain hash: it's the combined hash of all the
3306 * lock keys along the dependency chain. We save the hash value
3307 * at every step so that we can get the current hash easily
3308 * after unlock. The chain hash is then used to cache dependency
3309 * results.
3310 *
3311 * The 'key ID' is what is the most compact key value to drive
3312 * the hash, not class->key.
3313 */
3314 /*
3315 * Whoops, we did it again.. ran straight out of our static allocation.
3316 */
3317 if (DEBUG_LOCKS_WARN_ON(class_idx > MAX_LOCKDEP_KEYS))
3318 return 0;
3319
3320 chain_key = curr->curr_chain_key;
3321 if (!depth) {
3322 /*
3323 * How can we have a chain hash when we ain't got no keys?!
3324 */
3325 if (DEBUG_LOCKS_WARN_ON(chain_key != 0))
3326 return 0;
3327 chain_head = 1;
3328 }
3329
3330 hlock->prev_chain_key = chain_key;
3331 if (separate_irq_context(curr, hlock)) {
3332 chain_key = 0;
3333 chain_head = 1;
3334 }
3335 chain_key = iterate_chain_key(chain_key, class_idx);
3336
3337 if (nest_lock && !__lock_is_held(nest_lock, -1))
3338 return print_lock_nested_lock_not_held(curr, hlock, ip);
3339
3340 if (!validate_chain(curr, lock, hlock, chain_head, chain_key))
3341 return 0;
3342
3343 curr->curr_chain_key = chain_key;
3344 curr->lockdep_depth++;
3345 check_chain_key(curr);
3346 #ifdef CONFIG_DEBUG_LOCKDEP
3347 if (unlikely(!debug_locks))
3348 return 0;
3349 #endif
3350 if (unlikely(curr->lockdep_depth >= MAX_LOCK_DEPTH)) {
3351 debug_locks_off();
3352 print_lockdep_off("BUG: MAX_LOCK_DEPTH too low!");
3353 printk(KERN_DEBUG "depth: %i max: %lu!\n",
3354 curr->lockdep_depth, MAX_LOCK_DEPTH);
3355
3356 lockdep_print_held_locks(current);
3357 debug_show_all_locks();
3358 dump_stack();
3359
3360 return 0;
3361 }
3362
3363 if (unlikely(curr->lockdep_depth > max_lockdep_depth))
3364 max_lockdep_depth = curr->lockdep_depth;
3365
3366 return 1;
3367 }
3368
3369 static int
3370 print_unlock_imbalance_bug(struct task_struct *curr, struct lockdep_map *lock,
3371 unsigned long ip)
3372 {
3373 if (!debug_locks_off())
3374 return 0;
3375 if (debug_locks_silent)
3376 return 0;
3377
3378 pr_warn("\n");
3379 pr_warn("=====================================\n");
3380 pr_warn("WARNING: bad unlock balance detected!\n");
3381 print_kernel_ident();
3382 pr_warn("-------------------------------------\n");
3383 pr_warn("%s/%d is trying to release lock (",
3384 curr->comm, task_pid_nr(curr));
3385 print_lockdep_cache(lock);
3386 pr_cont(") at:\n");
3387 print_ip_sym(ip);
3388 pr_warn("but there are no more locks to release!\n");
3389 pr_warn("\nother info that might help us debug this:\n");
3390 lockdep_print_held_locks(curr);
3391
3392 pr_warn("\nstack backtrace:\n");
3393 dump_stack();
3394
3395 return 0;
3396 }
3397
3398 static int match_held_lock(const struct held_lock *hlock,
3399 const struct lockdep_map *lock)
3400 {
3401 if (hlock->instance == lock)
3402 return 1;
3403
3404 if (hlock->references) {
3405 const struct lock_class *class = lock->class_cache[0];
3406
3407 if (!class)
3408 class = look_up_lock_class(lock, 0);
3409
3410 /*
3411 * If look_up_lock_class() failed to find a class, we're trying
3412 * to test if we hold a lock that has never yet been acquired.
3413 * Clearly if the lock hasn't been acquired _ever_, we're not
3414 * holding it either, so report failure.
3415 */
3416 if (!class)
3417 return 0;
3418
3419 /*
3420 * References, but not a lock we're actually ref-counting?
3421 * State got messed up, follow the sites that change ->references
3422 * and try to make sense of it.
3423 */
3424 if (DEBUG_LOCKS_WARN_ON(!hlock->nest_lock))
3425 return 0;
3426
3427 if (hlock->class_idx == class - lock_classes + 1)
3428 return 1;
3429 }
3430
3431 return 0;
3432 }
3433
3434 /* @depth must not be zero */
3435 static struct held_lock *find_held_lock(struct task_struct *curr,
3436 struct lockdep_map *lock,
3437 unsigned int depth, int *idx)
3438 {
3439 struct held_lock *ret, *hlock, *prev_hlock;
3440 int i;
3441
3442 i = depth - 1;
3443 hlock = curr->held_locks + i;
3444 ret = hlock;
3445 if (match_held_lock(hlock, lock))
3446 goto out;
3447
3448 ret = NULL;
3449 for (i--, prev_hlock = hlock--;
3450 i >= 0;
3451 i--, prev_hlock = hlock--) {
3452 /*
3453 * We must not cross into another context:
3454 */
3455 if (prev_hlock->irq_context != hlock->irq_context) {
3456 ret = NULL;
3457 break;
3458 }
3459 if (match_held_lock(hlock, lock)) {
3460 ret = hlock;
3461 break;
3462 }
3463 }
3464
3465 out:
3466 *idx = i;
3467 return ret;
3468 }
3469
3470 static int reacquire_held_locks(struct task_struct *curr, unsigned int depth,
3471 int idx)
3472 {
3473 struct held_lock *hlock;
3474
3475 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
3476 return 0;
3477
3478 for (hlock = curr->held_locks + idx; idx < depth; idx++, hlock++) {
3479 if (!__lock_acquire(hlock->instance,
3480 hlock_class(hlock)->subclass,
3481 hlock->trylock,
3482 hlock->read, hlock->check,
3483 hlock->hardirqs_off,
3484 hlock->nest_lock, hlock->acquire_ip,
3485 hlock->references, hlock->pin_count))
3486 return 1;
3487 }
3488 return 0;
3489 }
3490
3491 static int
3492 __lock_set_class(struct lockdep_map *lock, const char *name,
3493 struct lock_class_key *key, unsigned int subclass,
3494 unsigned long ip)
3495 {
3496 struct task_struct *curr = current;
3497 struct held_lock *hlock;
3498 struct lock_class *class;
3499 unsigned int depth;
3500 int i;
3501
3502 if (unlikely(!debug_locks))
3503 return 0;
3504
3505 depth = curr->lockdep_depth;
3506 /*
3507 * This function is about (re)setting the class of a held lock,
3508 * yet we're not actually holding any locks. Naughty user!
3509 */
3510 if (DEBUG_LOCKS_WARN_ON(!depth))
3511 return 0;
3512
3513 hlock = find_held_lock(curr, lock, depth, &i);
3514 if (!hlock)
3515 return print_unlock_imbalance_bug(curr, lock, ip);
3516
3517 lockdep_init_map(lock, name, key, 0);
3518 class = register_lock_class(lock, subclass, 0);
3519 hlock->class_idx = class - lock_classes + 1;
3520
3521 curr->lockdep_depth = i;
3522 curr->curr_chain_key = hlock->prev_chain_key;
3523
3524 if (reacquire_held_locks(curr, depth, i))
3525 return 0;
3526
3527 /*
3528 * I took it apart and put it back together again, except now I have
3529 * these 'spare' parts.. where shall I put them.
3530 */
3531 if (DEBUG_LOCKS_WARN_ON(curr->lockdep_depth != depth))
3532 return 0;
3533 return 1;
3534 }
3535
3536 static int __lock_downgrade(struct lockdep_map *lock, unsigned long ip)
3537 {
3538 struct task_struct *curr = current;
3539 struct held_lock *hlock;
3540 unsigned int depth;
3541 int i;
3542
3543 if (unlikely(!debug_locks))
3544 return 0;
3545
3546 depth = curr->lockdep_depth;
3547 /*
3548 * This function is about (re)setting the class of a held lock,
3549 * yet we're not actually holding any locks. Naughty user!
3550 */
3551 if (DEBUG_LOCKS_WARN_ON(!depth))
3552 return 0;
3553
3554 hlock = find_held_lock(curr, lock, depth, &i);
3555 if (!hlock)
3556 return print_unlock_imbalance_bug(curr, lock, ip);
3557
3558 curr->lockdep_depth = i;
3559 curr->curr_chain_key = hlock->prev_chain_key;
3560
3561 WARN(hlock->read, "downgrading a read lock");
3562 hlock->read = 1;
3563 hlock->acquire_ip = ip;
3564
3565 if (reacquire_held_locks(curr, depth, i))
3566 return 0;
3567
3568 /*
3569 * I took it apart and put it back together again, except now I have
3570 * these 'spare' parts.. where shall I put them.
3571 */
3572 if (DEBUG_LOCKS_WARN_ON(curr->lockdep_depth != depth))
3573 return 0;
3574 return 1;
3575 }
3576
3577 /*
3578 * Remove the lock to the list of currently held locks - this gets
3579 * called on mutex_unlock()/spin_unlock*() (or on a failed
3580 * mutex_lock_interruptible()).
3581 *
3582 * @nested is an hysterical artifact, needs a tree wide cleanup.
3583 */
3584 static int
3585 __lock_release(struct lockdep_map *lock, int nested, unsigned long ip)
3586 {
3587 struct task_struct *curr = current;
3588 struct held_lock *hlock;
3589 unsigned int depth;
3590 int i;
3591
3592 if (unlikely(!debug_locks))
3593 return 0;
3594
3595 depth = curr->lockdep_depth;
3596 /*
3597 * So we're all set to release this lock.. wait what lock? We don't
3598 * own any locks, you've been drinking again?
3599 */
3600 if (DEBUG_LOCKS_WARN_ON(depth <= 0))
3601 return print_unlock_imbalance_bug(curr, lock, ip);
3602
3603 /*
3604 * Check whether the lock exists in the current stack
3605 * of held locks:
3606 */
3607 hlock = find_held_lock(curr, lock, depth, &i);
3608 if (!hlock)
3609 return print_unlock_imbalance_bug(curr, lock, ip);
3610
3611 if (hlock->instance == lock)
3612 lock_release_holdtime(hlock);
3613
3614 WARN(hlock->pin_count, "releasing a pinned lock\n");
3615
3616 if (hlock->references) {
3617 hlock->references--;
3618 if (hlock->references) {
3619 /*
3620 * We had, and after removing one, still have
3621 * references, the current lock stack is still
3622 * valid. We're done!
3623 */
3624 return 1;
3625 }
3626 }
3627
3628 /*
3629 * We have the right lock to unlock, 'hlock' points to it.
3630 * Now we remove it from the stack, and add back the other
3631 * entries (if any), recalculating the hash along the way:
3632 */
3633
3634 curr->lockdep_depth = i;
3635 curr->curr_chain_key = hlock->prev_chain_key;
3636
3637 /*
3638 * The most likely case is when the unlock is on the innermost
3639 * lock. In this case, we are done!
3640 */
3641 if (i == depth-1)
3642 return 1;
3643
3644 if (reacquire_held_locks(curr, depth, i + 1))
3645 return 0;
3646
3647 /*
3648 * We had N bottles of beer on the wall, we drank one, but now
3649 * there's not N-1 bottles of beer left on the wall...
3650 */
3651 DEBUG_LOCKS_WARN_ON(curr->lockdep_depth != depth-1);
3652
3653 /*
3654 * Since reacquire_held_locks() would have called check_chain_key()
3655 * indirectly via __lock_acquire(), we don't need to do it again
3656 * on return.
3657 */
3658 return 0;
3659 }
3660
3661 static int __lock_is_held(const struct lockdep_map *lock, int read)
3662 {
3663 struct task_struct *curr = current;
3664 int i;
3665
3666 for (i = 0; i < curr->lockdep_depth; i++) {
3667 struct held_lock *hlock = curr->held_locks + i;
3668
3669 if (match_held_lock(hlock, lock)) {
3670 if (read == -1 || hlock->read == read)
3671 return 1;
3672
3673 return 0;
3674 }
3675 }
3676
3677 return 0;
3678 }
3679
3680 static struct pin_cookie __lock_pin_lock(struct lockdep_map *lock)
3681 {
3682 struct pin_cookie cookie = NIL_COOKIE;
3683 struct task_struct *curr = current;
3684 int i;
3685
3686 if (unlikely(!debug_locks))
3687 return cookie;
3688
3689 for (i = 0; i < curr->lockdep_depth; i++) {
3690 struct held_lock *hlock = curr->held_locks + i;
3691
3692 if (match_held_lock(hlock, lock)) {
3693 /*
3694 * Grab 16bits of randomness; this is sufficient to not
3695 * be guessable and still allows some pin nesting in
3696 * our u32 pin_count.
3697 */
3698 cookie.val = 1 + (prandom_u32() >> 16);
3699 hlock->pin_count += cookie.val;
3700 return cookie;
3701 }
3702 }
3703
3704 WARN(1, "pinning an unheld lock\n");
3705 return cookie;
3706 }
3707
3708 static void __lock_repin_lock(struct lockdep_map *lock, struct pin_cookie cookie)
3709 {
3710 struct task_struct *curr = current;
3711 int i;
3712
3713 if (unlikely(!debug_locks))
3714 return;
3715
3716 for (i = 0; i < curr->lockdep_depth; i++) {
3717 struct held_lock *hlock = curr->held_locks + i;
3718
3719 if (match_held_lock(hlock, lock)) {
3720 hlock->pin_count += cookie.val;
3721 return;
3722 }
3723 }
3724
3725 WARN(1, "pinning an unheld lock\n");
3726 }
3727
3728 static void __lock_unpin_lock(struct lockdep_map *lock, struct pin_cookie cookie)
3729 {
3730 struct task_struct *curr = current;
3731 int i;
3732
3733 if (unlikely(!debug_locks))
3734 return;
3735
3736 for (i = 0; i < curr->lockdep_depth; i++) {
3737 struct held_lock *hlock = curr->held_locks + i;
3738
3739 if (match_held_lock(hlock, lock)) {
3740 if (WARN(!hlock->pin_count, "unpinning an unpinned lock\n"))
3741 return;
3742
3743 hlock->pin_count -= cookie.val;
3744
3745 if (WARN((int)hlock->pin_count < 0, "pin count corrupted\n"))
3746 hlock->pin_count = 0;
3747
3748 return;
3749 }
3750 }
3751
3752 WARN(1, "unpinning an unheld lock\n");
3753 }
3754
3755 /*
3756 * Check whether we follow the irq-flags state precisely:
3757 */
3758 static void check_flags(unsigned long flags)
3759 {
3760 #if defined(CONFIG_PROVE_LOCKING) && defined(CONFIG_DEBUG_LOCKDEP) && \
3761 defined(CONFIG_TRACE_IRQFLAGS)
3762 if (!debug_locks)
3763 return;
3764
3765 if (irqs_disabled_flags(flags)) {
3766 if (DEBUG_LOCKS_WARN_ON(current->hardirqs_enabled)) {
3767 printk("possible reason: unannotated irqs-off.\n");
3768 }
3769 } else {
3770 if (DEBUG_LOCKS_WARN_ON(!current->hardirqs_enabled)) {
3771 printk("possible reason: unannotated irqs-on.\n");
3772 }
3773 }
3774
3775 /*
3776 * We dont accurately track softirq state in e.g.
3777 * hardirq contexts (such as on 4KSTACKS), so only
3778 * check if not in hardirq contexts:
3779 */
3780 if (!hardirq_count()) {
3781 if (softirq_count()) {
3782 /* like the above, but with softirqs */
3783 DEBUG_LOCKS_WARN_ON(current->softirqs_enabled);
3784 } else {
3785 /* lick the above, does it taste good? */
3786 DEBUG_LOCKS_WARN_ON(!current->softirqs_enabled);
3787 }
3788 }
3789
3790 if (!debug_locks)
3791 print_irqtrace_events(current);
3792 #endif
3793 }
3794
3795 void lock_set_class(struct lockdep_map *lock, const char *name,
3796 struct lock_class_key *key, unsigned int subclass,
3797 unsigned long ip)
3798 {
3799 unsigned long flags;
3800
3801 if (unlikely(current->lockdep_recursion))
3802 return;
3803
3804 raw_local_irq_save(flags);
3805 current->lockdep_recursion = 1;
3806 check_flags(flags);
3807 if (__lock_set_class(lock, name, key, subclass, ip))
3808 check_chain_key(current);
3809 current->lockdep_recursion = 0;
3810 raw_local_irq_restore(flags);
3811 }
3812 EXPORT_SYMBOL_GPL(lock_set_class);
3813
3814 void lock_downgrade(struct lockdep_map *lock, unsigned long ip)
3815 {
3816 unsigned long flags;
3817
3818 if (unlikely(current->lockdep_recursion))
3819 return;
3820
3821 raw_local_irq_save(flags);
3822 current->lockdep_recursion = 1;
3823 check_flags(flags);
3824 if (__lock_downgrade(lock, ip))
3825 check_chain_key(current);
3826 current->lockdep_recursion = 0;
3827 raw_local_irq_restore(flags);
3828 }
3829 EXPORT_SYMBOL_GPL(lock_downgrade);
3830
3831 /*
3832 * We are not always called with irqs disabled - do that here,
3833 * and also avoid lockdep recursion:
3834 */
3835 void lock_acquire(struct lockdep_map *lock, unsigned int subclass,
3836 int trylock, int read, int check,
3837 struct lockdep_map *nest_lock, unsigned long ip)
3838 {
3839 unsigned long flags;
3840
3841 if (unlikely(current->lockdep_recursion))
3842 return;
3843
3844 raw_local_irq_save(flags);
3845 check_flags(flags);
3846
3847 current->lockdep_recursion = 1;
3848 trace_lock_acquire(lock, subclass, trylock, read, check, nest_lock, ip);
3849 __lock_acquire(lock, subclass, trylock, read, check,
3850 irqs_disabled_flags(flags), nest_lock, ip, 0, 0);
3851 current->lockdep_recursion = 0;
3852 raw_local_irq_restore(flags);
3853 }
3854 EXPORT_SYMBOL_GPL(lock_acquire);
3855
3856 void lock_release(struct lockdep_map *lock, int nested,
3857 unsigned long ip)
3858 {
3859 unsigned long flags;
3860
3861 if (unlikely(current->lockdep_recursion))
3862 return;
3863
3864 raw_local_irq_save(flags);
3865 check_flags(flags);
3866 current->lockdep_recursion = 1;
3867 trace_lock_release(lock, ip);
3868 if (__lock_release(lock, nested, ip))
3869 check_chain_key(current);
3870 current->lockdep_recursion = 0;
3871 raw_local_irq_restore(flags);
3872 }
3873 EXPORT_SYMBOL_GPL(lock_release);
3874
3875 int lock_is_held_type(const struct lockdep_map *lock, int read)
3876 {
3877 unsigned long flags;
3878 int ret = 0;
3879
3880 if (unlikely(current->lockdep_recursion))
3881 return 1; /* avoid false negative lockdep_assert_held() */
3882
3883 raw_local_irq_save(flags);
3884 check_flags(flags);
3885
3886 current->lockdep_recursion = 1;
3887 ret = __lock_is_held(lock, read);
3888 current->lockdep_recursion = 0;
3889 raw_local_irq_restore(flags);
3890
3891 return ret;
3892 }
3893 EXPORT_SYMBOL_GPL(lock_is_held_type);
3894
3895 struct pin_cookie lock_pin_lock(struct lockdep_map *lock)
3896 {
3897 struct pin_cookie cookie = NIL_COOKIE;
3898 unsigned long flags;
3899
3900 if (unlikely(current->lockdep_recursion))
3901 return cookie;
3902
3903 raw_local_irq_save(flags);
3904 check_flags(flags);
3905
3906 current->lockdep_recursion = 1;
3907 cookie = __lock_pin_lock(lock);
3908 current->lockdep_recursion = 0;
3909 raw_local_irq_restore(flags);
3910
3911 return cookie;
3912 }
3913 EXPORT_SYMBOL_GPL(lock_pin_lock);
3914
3915 void lock_repin_lock(struct lockdep_map *lock, struct pin_cookie cookie)
3916 {
3917 unsigned long flags;
3918
3919 if (unlikely(current->lockdep_recursion))
3920 return;
3921
3922 raw_local_irq_save(flags);
3923 check_flags(flags);
3924
3925 current->lockdep_recursion = 1;
3926 __lock_repin_lock(lock, cookie);
3927 current->lockdep_recursion = 0;
3928 raw_local_irq_restore(flags);
3929 }
3930 EXPORT_SYMBOL_GPL(lock_repin_lock);
3931
3932 void lock_unpin_lock(struct lockdep_map *lock, struct pin_cookie cookie)
3933 {
3934 unsigned long flags;
3935
3936 if (unlikely(current->lockdep_recursion))
3937 return;
3938
3939 raw_local_irq_save(flags);
3940 check_flags(flags);
3941
3942 current->lockdep_recursion = 1;
3943 __lock_unpin_lock(lock, cookie);
3944 current->lockdep_recursion = 0;
3945 raw_local_irq_restore(flags);
3946 }
3947 EXPORT_SYMBOL_GPL(lock_unpin_lock);
3948
3949 #ifdef CONFIG_LOCK_STAT
3950 static int
3951 print_lock_contention_bug(struct task_struct *curr, struct lockdep_map *lock,
3952 unsigned long ip)
3953 {
3954 if (!debug_locks_off())
3955 return 0;
3956 if (debug_locks_silent)
3957 return 0;
3958
3959 pr_warn("\n");
3960 pr_warn("=================================\n");
3961 pr_warn("WARNING: bad contention detected!\n");
3962 print_kernel_ident();
3963 pr_warn("---------------------------------\n");
3964 pr_warn("%s/%d is trying to contend lock (",
3965 curr->comm, task_pid_nr(curr));
3966 print_lockdep_cache(lock);
3967 pr_cont(") at:\n");
3968 print_ip_sym(ip);
3969 pr_warn("but there are no locks held!\n");
3970 pr_warn("\nother info that might help us debug this:\n");
3971 lockdep_print_held_locks(curr);
3972
3973 pr_warn("\nstack backtrace:\n");
3974 dump_stack();
3975
3976 return 0;
3977 }
3978
3979 static void
3980 __lock_contended(struct lockdep_map *lock, unsigned long ip)
3981 {
3982 struct task_struct *curr = current;
3983 struct held_lock *hlock;
3984 struct lock_class_stats *stats;
3985 unsigned int depth;
3986 int i, contention_point, contending_point;
3987
3988 depth = curr->lockdep_depth;
3989 /*
3990 * Whee, we contended on this lock, except it seems we're not
3991 * actually trying to acquire anything much at all..
3992 */
3993 if (DEBUG_LOCKS_WARN_ON(!depth))
3994 return;
3995
3996 hlock = find_held_lock(curr, lock, depth, &i);
3997 if (!hlock) {
3998 print_lock_contention_bug(curr, lock, ip);
3999 return;
4000 }
4001
4002 if (hlock->instance != lock)
4003 return;
4004
4005 hlock->waittime_stamp = lockstat_clock();
4006
4007 contention_point = lock_point(hlock_class(hlock)->contention_point, ip);
4008 contending_point = lock_point(hlock_class(hlock)->contending_point,
4009 lock->ip);
4010
4011 stats = get_lock_stats(hlock_class(hlock));
4012 if (contention_point < LOCKSTAT_POINTS)
4013 stats->contention_point[contention_point]++;
4014 if (contending_point < LOCKSTAT_POINTS)
4015 stats->contending_point[contending_point]++;
4016 if (lock->cpu != smp_processor_id())
4017 stats->bounces[bounce_contended + !!hlock->read]++;
4018 }
4019
4020 static void
4021 __lock_acquired(struct lockdep_map *lock, unsigned long ip)
4022 {
4023 struct task_struct *curr = current;
4024 struct held_lock *hlock;
4025 struct lock_class_stats *stats;
4026 unsigned int depth;
4027 u64 now, waittime = 0;
4028 int i, cpu;
4029
4030 depth = curr->lockdep_depth;
4031 /*
4032 * Yay, we acquired ownership of this lock we didn't try to
4033 * acquire, how the heck did that happen?
4034 */
4035 if (DEBUG_LOCKS_WARN_ON(!depth))
4036 return;
4037
4038 hlock = find_held_lock(curr, lock, depth, &i);
4039 if (!hlock) {
4040 print_lock_contention_bug(curr, lock, _RET_IP_);
4041 return;
4042 }
4043
4044 if (hlock->instance != lock)
4045 return;
4046
4047 cpu = smp_processor_id();
4048 if (hlock->waittime_stamp) {
4049 now = lockstat_clock();
4050 waittime = now - hlock->waittime_stamp;
4051 hlock->holdtime_stamp = now;
4052 }
4053
4054 trace_lock_acquired(lock, ip);
4055
4056 stats = get_lock_stats(hlock_class(hlock));
4057 if (waittime) {
4058 if (hlock->read)
4059 lock_time_inc(&stats->read_waittime, waittime);
4060 else
4061 lock_time_inc(&stats->write_waittime, waittime);
4062 }
4063 if (lock->cpu != cpu)
4064 stats->bounces[bounce_acquired + !!hlock->read]++;
4065
4066 lock->cpu = cpu;
4067 lock->ip = ip;
4068 }
4069
4070 void lock_contended(struct lockdep_map *lock, unsigned long ip)
4071 {
4072 unsigned long flags;
4073
4074 if (unlikely(!lock_stat || !debug_locks))
4075 return;
4076
4077 if (unlikely(current->lockdep_recursion))
4078 return;
4079
4080 raw_local_irq_save(flags);
4081 check_flags(flags);
4082 current->lockdep_recursion = 1;
4083 trace_lock_contended(lock, ip);
4084 __lock_contended(lock, ip);
4085 current->lockdep_recursion = 0;
4086 raw_local_irq_restore(flags);
4087 }
4088 EXPORT_SYMBOL_GPL(lock_contended);
4089
4090 void lock_acquired(struct lockdep_map *lock, unsigned long ip)
4091 {
4092 unsigned long flags;
4093
4094 if (unlikely(!lock_stat || !debug_locks))
4095 return;
4096
4097 if (unlikely(current->lockdep_recursion))
4098 return;
4099
4100 raw_local_irq_save(flags);
4101 check_flags(flags);
4102 current->lockdep_recursion = 1;
4103 __lock_acquired(lock, ip);
4104 current->lockdep_recursion = 0;
4105 raw_local_irq_restore(flags);
4106 }
4107 EXPORT_SYMBOL_GPL(lock_acquired);
4108 #endif
4109
4110 /*
4111 * Used by the testsuite, sanitize the validator state
4112 * after a simulated failure:
4113 */
4114
4115 void lockdep_reset(void)
4116 {
4117 unsigned long flags;
4118 int i;
4119
4120 raw_local_irq_save(flags);
4121 current->curr_chain_key = 0;
4122 current->lockdep_depth = 0;
4123 current->lockdep_recursion = 0;
4124 memset(current->held_locks, 0, MAX_LOCK_DEPTH*sizeof(struct held_lock));
4125 nr_hardirq_chains = 0;
4126 nr_softirq_chains = 0;
4127 nr_process_chains = 0;
4128 debug_locks = 1;
4129 for (i = 0; i < CHAINHASH_SIZE; i++)
4130 INIT_HLIST_HEAD(chainhash_table + i);
4131 raw_local_irq_restore(flags);
4132 }
4133
4134 /*
4135 * Remove all references to a lock class. The caller must hold the graph lock.
4136 */
4137 static void zap_class(struct lock_class *class)
4138 {
4139 struct lock_list *entry;
4140 int i;
4141
4142 /*
4143 * Remove all dependencies this lock is
4144 * involved in:
4145 */
4146 for (i = 0, entry = list_entries; i < nr_list_entries; i++, entry++) {
4147 if (entry->class != class && entry->links_to != class)
4148 continue;
4149 list_del_rcu(&entry->entry);
4150 /* Clear .class and .links_to to avoid double removal. */
4151 WRITE_ONCE(entry->class, NULL);
4152 WRITE_ONCE(entry->links_to, NULL);
4153 }
4154 /*
4155 * Unhash the class and remove it from the all_lock_classes list:
4156 */
4157 hlist_del_rcu(&class->hash_entry);
4158 list_del(&class->lock_entry);
4159
4160 RCU_INIT_POINTER(class->key, NULL);
4161 RCU_INIT_POINTER(class->name, NULL);
4162 }
4163
4164 static inline int within(const void *addr, void *start, unsigned long size)
4165 {
4166 return addr >= start && addr < start + size;
4167 }
4168
4169 static void __lockdep_free_key_range(void *start, unsigned long size)
4170 {
4171 struct lock_class *class;
4172 struct hlist_head *head;
4173 int i;
4174
4175 /* Unhash all classes that were created by a module. */
4176 for (i = 0; i < CLASSHASH_SIZE; i++) {
4177 head = classhash_table + i;
4178 hlist_for_each_entry_rcu(class, head, hash_entry) {
4179 if (!within(class->key, start, size) &&
4180 !within(class->name, start, size))
4181 continue;
4182 zap_class(class);
4183 }
4184 }
4185 }
4186
4187 /*
4188 * Used in module.c to remove lock classes from memory that is going to be
4189 * freed; and possibly re-used by other modules.
4190 *
4191 * We will have had one sync_sched() before getting here, so we're guaranteed
4192 * nobody will look up these exact classes -- they're properly dead but still
4193 * allocated.
4194 */
4195 void lockdep_free_key_range(void *start, unsigned long size)
4196 {
4197 unsigned long flags;
4198 int locked;
4199
4200 init_data_structures_once();
4201
4202 raw_local_irq_save(flags);
4203 locked = graph_lock();
4204 __lockdep_free_key_range(start, size);
4205 if (locked)
4206 graph_unlock();
4207 raw_local_irq_restore(flags);
4208
4209 /*
4210 * Wait for any possible iterators from look_up_lock_class() to pass
4211 * before continuing to free the memory they refer to.
4212 *
4213 * sync_sched() is sufficient because the read-side is IRQ disable.
4214 */
4215 synchronize_rcu();
4216
4217 /*
4218 * XXX at this point we could return the resources to the pool;
4219 * instead we leak them. We would need to change to bitmap allocators
4220 * instead of the linear allocators we have now.
4221 */
4222 }
4223
4224 /*
4225 * Check whether any element of the @lock->class_cache[] array refers to a
4226 * registered lock class. The caller must hold either the graph lock or the
4227 * RCU read lock.
4228 */
4229 static bool lock_class_cache_is_registered(struct lockdep_map *lock)
4230 {
4231 struct lock_class *class;
4232 struct hlist_head *head;
4233 int i, j;
4234
4235 for (i = 0; i < CLASSHASH_SIZE; i++) {
4236 head = classhash_table + i;
4237 hlist_for_each_entry_rcu(class, head, hash_entry) {
4238 for (j = 0; j < NR_LOCKDEP_CACHING_CLASSES; j++)
4239 if (lock->class_cache[j] == class)
4240 return true;
4241 }
4242 }
4243 return false;
4244 }
4245
4246 /* The caller must hold the graph lock. Does not sleep. */
4247 static void __lockdep_reset_lock(struct lockdep_map *lock)
4248 {
4249 struct lock_class *class;
4250 int j;
4251
4252 /*
4253 * Remove all classes this lock might have:
4254 */
4255 for (j = 0; j < MAX_LOCKDEP_SUBCLASSES; j++) {
4256 /*
4257 * If the class exists we look it up and zap it:
4258 */
4259 class = look_up_lock_class(lock, j);
4260 if (class)
4261 zap_class(class);
4262 }
4263 /*
4264 * Debug check: in the end all mapped classes should
4265 * be gone.
4266 */
4267 if (WARN_ON_ONCE(lock_class_cache_is_registered(lock)))
4268 debug_locks_off();
4269 }
4270
4271 void lockdep_reset_lock(struct lockdep_map *lock)
4272 {
4273 unsigned long flags;
4274 int locked;
4275
4276 init_data_structures_once();
4277
4278 raw_local_irq_save(flags);
4279 locked = graph_lock();
4280 __lockdep_reset_lock(lock);
4281 if (locked)
4282 graph_unlock();
4283 raw_local_irq_restore(flags);
4284 }
4285
4286 void __init lockdep_init(void)
4287 {
4288 printk("Lock dependency validator: Copyright (c) 2006 Red Hat, Inc., Ingo Molnar\n");
4289
4290 printk("... MAX_LOCKDEP_SUBCLASSES: %lu\n", MAX_LOCKDEP_SUBCLASSES);
4291 printk("... MAX_LOCK_DEPTH: %lu\n", MAX_LOCK_DEPTH);
4292 printk("... MAX_LOCKDEP_KEYS: %lu\n", MAX_LOCKDEP_KEYS);
4293 printk("... CLASSHASH_SIZE: %lu\n", CLASSHASH_SIZE);
4294 printk("... MAX_LOCKDEP_ENTRIES: %lu\n", MAX_LOCKDEP_ENTRIES);
4295 printk("... MAX_LOCKDEP_CHAINS: %lu\n", MAX_LOCKDEP_CHAINS);
4296 printk("... CHAINHASH_SIZE: %lu\n", CHAINHASH_SIZE);
4297
4298 printk(" memory used by lock dependency info: %zu kB\n",
4299 (sizeof(lock_classes) +
4300 sizeof(classhash_table) +
4301 sizeof(list_entries) +
4302 sizeof(chainhash_table)
4303 #ifdef CONFIG_PROVE_LOCKING
4304 + sizeof(lock_cq)
4305 + sizeof(lock_chains)
4306 + sizeof(chain_hlocks)
4307 #endif
4308 ) / 1024
4309 );
4310
4311 printk(" per task-struct memory footprint: %zu bytes\n",
4312 sizeof(((struct task_struct *)NULL)->held_locks));
4313 }
4314
4315 static void
4316 print_freed_lock_bug(struct task_struct *curr, const void *mem_from,
4317 const void *mem_to, struct held_lock *hlock)
4318 {
4319 if (!debug_locks_off())
4320 return;
4321 if (debug_locks_silent)
4322 return;
4323
4324 pr_warn("\n");
4325 pr_warn("=========================\n");
4326 pr_warn("WARNING: held lock freed!\n");
4327 print_kernel_ident();
4328 pr_warn("-------------------------\n");
4329 pr_warn("%s/%d is freeing memory %px-%px, with a lock still held there!\n",
4330 curr->comm, task_pid_nr(curr), mem_from, mem_to-1);
4331 print_lock(hlock);
4332 lockdep_print_held_locks(curr);
4333
4334 pr_warn("\nstack backtrace:\n");
4335 dump_stack();
4336 }
4337
4338 static inline int not_in_range(const void* mem_from, unsigned long mem_len,
4339 const void* lock_from, unsigned long lock_len)
4340 {
4341 return lock_from + lock_len <= mem_from ||
4342 mem_from + mem_len <= lock_from;
4343 }
4344
4345 /*
4346 * Called when kernel memory is freed (or unmapped), or if a lock
4347 * is destroyed or reinitialized - this code checks whether there is
4348 * any held lock in the memory range of <from> to <to>:
4349 */
4350 void debug_check_no_locks_freed(const void *mem_from, unsigned long mem_len)
4351 {
4352 struct task_struct *curr = current;
4353 struct held_lock *hlock;
4354 unsigned long flags;
4355 int i;
4356
4357 if (unlikely(!debug_locks))
4358 return;
4359
4360 raw_local_irq_save(flags);
4361 for (i = 0; i < curr->lockdep_depth; i++) {
4362 hlock = curr->held_locks + i;
4363
4364 if (not_in_range(mem_from, mem_len, hlock->instance,
4365 sizeof(*hlock->instance)))
4366 continue;
4367
4368 print_freed_lock_bug(curr, mem_from, mem_from + mem_len, hlock);
4369 break;
4370 }
4371 raw_local_irq_restore(flags);
4372 }
4373 EXPORT_SYMBOL_GPL(debug_check_no_locks_freed);
4374
4375 static void print_held_locks_bug(void)
4376 {
4377 if (!debug_locks_off())
4378 return;
4379 if (debug_locks_silent)
4380 return;
4381
4382 pr_warn("\n");
4383 pr_warn("====================================\n");
4384 pr_warn("WARNING: %s/%d still has locks held!\n",
4385 current->comm, task_pid_nr(current));
4386 print_kernel_ident();
4387 pr_warn("------------------------------------\n");
4388 lockdep_print_held_locks(current);
4389 pr_warn("\nstack backtrace:\n");
4390 dump_stack();
4391 }
4392
4393 void debug_check_no_locks_held(void)
4394 {
4395 if (unlikely(current->lockdep_depth > 0))
4396 print_held_locks_bug();
4397 }
4398 EXPORT_SYMBOL_GPL(debug_check_no_locks_held);
4399
4400 #ifdef __KERNEL__
4401 void debug_show_all_locks(void)
4402 {
4403 struct task_struct *g, *p;
4404
4405 if (unlikely(!debug_locks)) {
4406 pr_warn("INFO: lockdep is turned off.\n");
4407 return;
4408 }
4409 pr_warn("\nShowing all locks held in the system:\n");
4410
4411 rcu_read_lock();
4412 for_each_process_thread(g, p) {
4413 if (!p->lockdep_depth)
4414 continue;
4415 lockdep_print_held_locks(p);
4416 touch_nmi_watchdog();
4417 touch_all_softlockup_watchdogs();
4418 }
4419 rcu_read_unlock();
4420
4421 pr_warn("\n");
4422 pr_warn("=============================================\n\n");
4423 }
4424 EXPORT_SYMBOL_GPL(debug_show_all_locks);
4425 #endif
4426
4427 /*
4428 * Careful: only use this function if you are sure that
4429 * the task cannot run in parallel!
4430 */
4431 void debug_show_held_locks(struct task_struct *task)
4432 {
4433 if (unlikely(!debug_locks)) {
4434 printk("INFO: lockdep is turned off.\n");
4435 return;
4436 }
4437 lockdep_print_held_locks(task);
4438 }
4439 EXPORT_SYMBOL_GPL(debug_show_held_locks);
4440
4441 asmlinkage __visible void lockdep_sys_exit(void)
4442 {
4443 struct task_struct *curr = current;
4444
4445 if (unlikely(curr->lockdep_depth)) {
4446 if (!debug_locks_off())
4447 return;
4448 pr_warn("\n");
4449 pr_warn("================================================\n");
4450 pr_warn("WARNING: lock held when returning to user space!\n");
4451 print_kernel_ident();
4452 pr_warn("------------------------------------------------\n");
4453 pr_warn("%s/%d is leaving the kernel with locks still held!\n",
4454 curr->comm, curr->pid);
4455 lockdep_print_held_locks(curr);
4456 }
4457
4458 /*
4459 * The lock history for each syscall should be independent. So wipe the
4460 * slate clean on return to userspace.
4461 */
4462 lockdep_invariant_state(false);
4463 }
4464
4465 void lockdep_rcu_suspicious(const char *file, const int line, const char *s)
4466 {
4467 struct task_struct *curr = current;
4468
4469 /* Note: the following can be executed concurrently, so be careful. */
4470 pr_warn("\n");
4471 pr_warn("=============================\n");
4472 pr_warn("WARNING: suspicious RCU usage\n");
4473 print_kernel_ident();
4474 pr_warn("-----------------------------\n");
4475 pr_warn("%s:%d %s!\n", file, line, s);
4476 pr_warn("\nother info that might help us debug this:\n\n");
4477 pr_warn("\n%srcu_scheduler_active = %d, debug_locks = %d\n",
4478 !rcu_lockdep_current_cpu_online()
4479 ? "RCU used illegally from offline CPU!\n"
4480 : !rcu_is_watching()
4481 ? "RCU used illegally from idle CPU!\n"
4482 : "",
4483 rcu_scheduler_active, debug_locks);
4484
4485 /*
4486 * If a CPU is in the RCU-free window in idle (ie: in the section
4487 * between rcu_idle_enter() and rcu_idle_exit(), then RCU
4488 * considers that CPU to be in an "extended quiescent state",
4489 * which means that RCU will be completely ignoring that CPU.
4490 * Therefore, rcu_read_lock() and friends have absolutely no
4491 * effect on a CPU running in that state. In other words, even if
4492 * such an RCU-idle CPU has called rcu_read_lock(), RCU might well
4493 * delete data structures out from under it. RCU really has no
4494 * choice here: we need to keep an RCU-free window in idle where
4495 * the CPU may possibly enter into low power mode. This way we can
4496 * notice an extended quiescent state to other CPUs that started a grace
4497 * period. Otherwise we would delay any grace period as long as we run
4498 * in the idle task.
4499 *
4500 * So complain bitterly if someone does call rcu_read_lock(),
4501 * rcu_read_lock_bh() and so on from extended quiescent states.
4502 */
4503 if (!rcu_is_watching())
4504 pr_warn("RCU used illegally from extended quiescent state!\n");
4505
4506 lockdep_print_held_locks(curr);
4507 pr_warn("\nstack backtrace:\n");
4508 dump_stack();
4509 }
4510 EXPORT_SYMBOL_GPL(lockdep_rcu_suspicious);