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