1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * kernel/lockdep.c
4 *
5 * Runtime locking correctness validator
6 *
7 * Started by Ingo Molnar:
8 *
9 * Copyright (C) 2006,2007 Red Hat, Inc., Ingo Molnar <[email protected]>
10 * Copyright (C) 2007 Red Hat, Inc., Peter Zijlstra
11 *
12 * this code maps all the lock dependencies as they occur in a live kernel
13 * and will warn about the following classes of locking bugs:
14 *
15 * - lock inversion scenarios
16 * - circular lock dependencies
17 * - hardirq/softirq safe/unsafe locking bugs
18 *
19 * Bugs are reported even if the current locking scenario does not cause
20 * any deadlock at this point.
21 *
22 * I.e. if anytime in the past two locks were taken in a different order,
23 * even if it happened for another task, even if those were different
24 * locks (but of the same class as this lock), this code will detect it.
25 *
26 * Thanks to Arjan van de Ven for coming up with the initial idea of
27 * mapping lock dependencies runtime.
28 */
29 #define DISABLE_BRANCH_PROFILING
30 #include <linux/mutex.h>
31 #include <linux/sched.h>
32 #include <linux/sched/clock.h>
33 #include <linux/sched/task.h>
34 #include <linux/sched/mm.h>
35 #include <linux/delay.h>
36 #include <linux/module.h>
37 #include <linux/proc_fs.h>
38 #include <linux/seq_file.h>
39 #include <linux/spinlock.h>
40 #include <linux/kallsyms.h>
41 #include <linux/interrupt.h>
42 #include <linux/stacktrace.h>
43 #include <linux/debug_locks.h>
44 #include <linux/irqflags.h>
45 #include <linux/utsname.h>
46 #include <linux/hash.h>
47 #include <linux/ftrace.h>
48 #include <linux/stringify.h>
49 #include <linux/bitmap.h>
50 #include <linux/bitops.h>
51 #include <linux/gfp.h>
52 #include <linux/random.h>
53 #include <linux/jhash.h>
54 #include <linux/nmi.h>
55 #include <linux/rcupdate.h>
56 #include <linux/kprobes.h>
57 #include <linux/lockdep.h>
58 #include <linux/context_tracking.h>
59 #include <linux/console.h>
60
61 #include <asm/sections.h>
62
63 #include "lockdep_internals.h"
64
65 #include <trace/events/lock.h>
66
67 #ifdef CONFIG_PROVE_LOCKING
68 static int prove_locking = 1;
69 module_param(prove_locking, int, 0644);
70 #else
71 #define prove_locking 0
72 #endif
73
74 #ifdef CONFIG_LOCK_STAT
75 static int lock_stat = 1;
76 module_param(lock_stat, int, 0644);
77 #else
78 #define lock_stat 0
79 #endif
80
81 #ifdef CONFIG_SYSCTL
82 static const struct ctl_table kern_lockdep_table[] = {
83 #ifdef CONFIG_PROVE_LOCKING
84 {
85 .procname = "prove_locking",
86 .data = &prove_locking,
87 .maxlen = sizeof(int),
88 .mode = 0644,
89 .proc_handler = proc_dointvec,
90 },
91 #endif /* CONFIG_PROVE_LOCKING */
92 #ifdef CONFIG_LOCK_STAT
93 {
94 .procname = "lock_stat",
95 .data = &lock_stat,
96 .maxlen = sizeof(int),
97 .mode = 0644,
98 .proc_handler = proc_dointvec,
99 },
100 #endif /* CONFIG_LOCK_STAT */
101 };
102
kernel_lockdep_sysctls_init(void)103 static __init int kernel_lockdep_sysctls_init(void)
104 {
105 register_sysctl_init("kernel", kern_lockdep_table);
106 return 0;
107 }
108 late_initcall(kernel_lockdep_sysctls_init);
109 #endif /* CONFIG_SYSCTL */
110
111 DEFINE_PER_CPU(unsigned int, lockdep_recursion);
112 EXPORT_PER_CPU_SYMBOL_GPL(lockdep_recursion);
113
lockdep_enabled(void)114 static __always_inline bool lockdep_enabled(void)
115 {
116 if (!debug_locks)
117 return false;
118
119 if (this_cpu_read(lockdep_recursion))
120 return false;
121
122 if (current->lockdep_recursion)
123 return false;
124
125 return true;
126 }
127
128 /*
129 * lockdep_lock: protects the lockdep graph, the hashes and the
130 * class/list/hash allocators.
131 *
132 * This is one of the rare exceptions where it's justified
133 * to use a raw spinlock - we really dont want the spinlock
134 * code to recurse back into the lockdep code...
135 */
136 static arch_spinlock_t __lock = (arch_spinlock_t)__ARCH_SPIN_LOCK_UNLOCKED;
137 static struct task_struct *__owner;
138
lockdep_lock(void)139 static inline void lockdep_lock(void)
140 {
141 DEBUG_LOCKS_WARN_ON(!irqs_disabled());
142
143 __this_cpu_inc(lockdep_recursion);
144 arch_spin_lock(&__lock);
145 __owner = current;
146 }
147
lockdep_unlock(void)148 static inline void lockdep_unlock(void)
149 {
150 DEBUG_LOCKS_WARN_ON(!irqs_disabled());
151
152 if (debug_locks && DEBUG_LOCKS_WARN_ON(__owner != current))
153 return;
154
155 __owner = NULL;
156 arch_spin_unlock(&__lock);
157 __this_cpu_dec(lockdep_recursion);
158 }
159
160 #ifdef CONFIG_PROVE_LOCKING
lockdep_assert_locked(void)161 static inline bool lockdep_assert_locked(void)
162 {
163 return DEBUG_LOCKS_WARN_ON(__owner != current);
164 }
165 #endif
166
167 static struct task_struct *lockdep_selftest_task_struct;
168
169
graph_lock(void)170 static int graph_lock(void)
171 {
172 lockdep_lock();
173 /*
174 * Make sure that if another CPU detected a bug while
175 * walking the graph we dont change it (while the other
176 * CPU is busy printing out stuff with the graph lock
177 * dropped already)
178 */
179 if (!debug_locks) {
180 lockdep_unlock();
181 return 0;
182 }
183 return 1;
184 }
185
graph_unlock(void)186 static inline void graph_unlock(void)
187 {
188 lockdep_unlock();
189 }
190
191 /*
192 * Turn lock debugging off and return with 0 if it was off already,
193 * and also release the graph lock:
194 */
debug_locks_off_graph_unlock(void)195 static inline int debug_locks_off_graph_unlock(void)
196 {
197 int ret = debug_locks_off();
198
199 lockdep_unlock();
200
201 return ret;
202 }
203
204 unsigned long nr_list_entries;
205 static struct lock_list list_entries[MAX_LOCKDEP_ENTRIES];
206 static DECLARE_BITMAP(list_entries_in_use, MAX_LOCKDEP_ENTRIES);
207
208 /*
209 * All data structures here are protected by the global debug_lock.
210 *
211 * nr_lock_classes is the number of elements of lock_classes[] that is
212 * in use.
213 */
214 #define KEYHASH_BITS (MAX_LOCKDEP_KEYS_BITS - 1)
215 #define KEYHASH_SIZE (1UL << KEYHASH_BITS)
216 static struct hlist_head lock_keys_hash[KEYHASH_SIZE];
217 unsigned long nr_lock_classes;
218 unsigned long nr_zapped_classes;
219 unsigned long max_lock_class_idx;
220 struct lock_class lock_classes[MAX_LOCKDEP_KEYS];
221 DECLARE_BITMAP(lock_classes_in_use, MAX_LOCKDEP_KEYS);
222
hlock_class(struct held_lock * hlock)223 static inline struct lock_class *hlock_class(struct held_lock *hlock)
224 {
225 unsigned int class_idx = hlock->class_idx;
226
227 /* Don't re-read hlock->class_idx, can't use READ_ONCE() on bitfield */
228 barrier();
229
230 if (!test_bit(class_idx, lock_classes_in_use)) {
231 /*
232 * Someone passed in garbage, we give up.
233 */
234 DEBUG_LOCKS_WARN_ON(1);
235 return NULL;
236 }
237
238 /*
239 * At this point, if the passed hlock->class_idx is still garbage,
240 * we just have to live with it
241 */
242 return lock_classes + class_idx;
243 }
244
245 #ifdef CONFIG_LOCK_STAT
246 static DEFINE_PER_CPU(struct lock_class_stats[MAX_LOCKDEP_KEYS], cpu_lock_stats);
247
lockstat_clock(void)248 static inline u64 lockstat_clock(void)
249 {
250 return local_clock();
251 }
252
lock_point(unsigned long points[],unsigned long ip)253 static int lock_point(unsigned long points[], unsigned long ip)
254 {
255 int i;
256
257 for (i = 0; i < LOCKSTAT_POINTS; i++) {
258 if (points[i] == 0) {
259 points[i] = ip;
260 break;
261 }
262 if (points[i] == ip)
263 break;
264 }
265
266 return i;
267 }
268
lock_time_inc(struct lock_time * lt,u64 time)269 static void lock_time_inc(struct lock_time *lt, u64 time)
270 {
271 if (time > lt->max)
272 lt->max = time;
273
274 if (time < lt->min || !lt->nr)
275 lt->min = time;
276
277 lt->total += time;
278 lt->nr++;
279 }
280
lock_time_add(struct lock_time * src,struct lock_time * dst)281 static inline void lock_time_add(struct lock_time *src, struct lock_time *dst)
282 {
283 if (!src->nr)
284 return;
285
286 if (src->max > dst->max)
287 dst->max = src->max;
288
289 if (src->min < dst->min || !dst->nr)
290 dst->min = src->min;
291
292 dst->total += src->total;
293 dst->nr += src->nr;
294 }
295
lock_stats(struct lock_class * class)296 struct lock_class_stats lock_stats(struct lock_class *class)
297 {
298 struct lock_class_stats stats;
299 int cpu, i;
300
301 memset(&stats, 0, sizeof(struct lock_class_stats));
302 for_each_possible_cpu(cpu) {
303 struct lock_class_stats *pcs =
304 &per_cpu(cpu_lock_stats, cpu)[class - lock_classes];
305
306 for (i = 0; i < ARRAY_SIZE(stats.contention_point); i++)
307 stats.contention_point[i] += pcs->contention_point[i];
308
309 for (i = 0; i < ARRAY_SIZE(stats.contending_point); i++)
310 stats.contending_point[i] += pcs->contending_point[i];
311
312 lock_time_add(&pcs->read_waittime, &stats.read_waittime);
313 lock_time_add(&pcs->write_waittime, &stats.write_waittime);
314
315 lock_time_add(&pcs->read_holdtime, &stats.read_holdtime);
316 lock_time_add(&pcs->write_holdtime, &stats.write_holdtime);
317
318 for (i = 0; i < ARRAY_SIZE(stats.bounces); i++)
319 stats.bounces[i] += pcs->bounces[i];
320 }
321
322 return stats;
323 }
324
clear_lock_stats(struct lock_class * class)325 void clear_lock_stats(struct lock_class *class)
326 {
327 int cpu;
328
329 for_each_possible_cpu(cpu) {
330 struct lock_class_stats *cpu_stats =
331 &per_cpu(cpu_lock_stats, cpu)[class - lock_classes];
332
333 memset(cpu_stats, 0, sizeof(struct lock_class_stats));
334 }
335 memset(class->contention_point, 0, sizeof(class->contention_point));
336 memset(class->contending_point, 0, sizeof(class->contending_point));
337 }
338
get_lock_stats(struct lock_class * class)339 static struct lock_class_stats *get_lock_stats(struct lock_class *class)
340 {
341 return &this_cpu_ptr(cpu_lock_stats)[class - lock_classes];
342 }
343
lock_release_holdtime(struct held_lock * hlock)344 static void lock_release_holdtime(struct held_lock *hlock)
345 {
346 struct lock_class_stats *stats;
347 u64 holdtime;
348
349 if (!lock_stat)
350 return;
351
352 holdtime = lockstat_clock() - hlock->holdtime_stamp;
353
354 stats = get_lock_stats(hlock_class(hlock));
355 if (hlock->read)
356 lock_time_inc(&stats->read_holdtime, holdtime);
357 else
358 lock_time_inc(&stats->write_holdtime, holdtime);
359 }
360 #else
lock_release_holdtime(struct held_lock * hlock)361 static inline void lock_release_holdtime(struct held_lock *hlock)
362 {
363 }
364 #endif
365
366 /*
367 * We keep a global list of all lock classes. The list is only accessed with
368 * the lockdep spinlock lock held. free_lock_classes is a list with free
369 * elements. These elements are linked together by the lock_entry member in
370 * struct lock_class.
371 */
372 static LIST_HEAD(all_lock_classes);
373 static LIST_HEAD(free_lock_classes);
374
375 /**
376 * struct pending_free - information about data structures about to be freed
377 * @zapped: Head of a list with struct lock_class elements.
378 * @lock_chains_being_freed: Bitmap that indicates which lock_chains[] elements
379 * are about to be freed.
380 */
381 struct pending_free {
382 struct list_head zapped;
383 DECLARE_BITMAP(lock_chains_being_freed, MAX_LOCKDEP_CHAINS);
384 };
385
386 /**
387 * struct delayed_free - data structures used for delayed freeing
388 *
389 * A data structure for delayed freeing of data structures that may be
390 * accessed by RCU readers at the time these were freed.
391 *
392 * @rcu_head: Used to schedule an RCU callback for freeing data structures.
393 * @index: Index of @pf to which freed data structures are added.
394 * @scheduled: Whether or not an RCU callback has been scheduled.
395 * @pf: Array with information about data structures about to be freed.
396 */
397 static struct delayed_free {
398 struct rcu_head rcu_head;
399 int index;
400 int scheduled;
401 struct pending_free pf[2];
402 } delayed_free;
403
404 /*
405 * The lockdep classes are in a hash-table as well, for fast lookup:
406 */
407 #define CLASSHASH_BITS (MAX_LOCKDEP_KEYS_BITS - 1)
408 #define CLASSHASH_SIZE (1UL << CLASSHASH_BITS)
409 #define __classhashfn(key) hash_long((unsigned long)key, CLASSHASH_BITS)
410 #define classhashentry(key) (classhash_table + __classhashfn((key)))
411
412 static struct hlist_head classhash_table[CLASSHASH_SIZE];
413
414 /*
415 * We put the lock dependency chains into a hash-table as well, to cache
416 * their existence:
417 */
418 #define CHAINHASH_BITS (MAX_LOCKDEP_CHAINS_BITS-1)
419 #define CHAINHASH_SIZE (1UL << CHAINHASH_BITS)
420 #define __chainhashfn(chain) hash_long(chain, CHAINHASH_BITS)
421 #define chainhashentry(chain) (chainhash_table + __chainhashfn((chain)))
422
423 static struct hlist_head chainhash_table[CHAINHASH_SIZE];
424
425 /*
426 * the id of held_lock
427 */
hlock_id(struct held_lock * hlock)428 static inline u16 hlock_id(struct held_lock *hlock)
429 {
430 BUILD_BUG_ON(MAX_LOCKDEP_KEYS_BITS + 2 > 16);
431
432 return (hlock->class_idx | (hlock->read << MAX_LOCKDEP_KEYS_BITS));
433 }
434
chain_hlock_class_idx(u16 hlock_id)435 static inline __maybe_unused unsigned int chain_hlock_class_idx(u16 hlock_id)
436 {
437 return hlock_id & (MAX_LOCKDEP_KEYS - 1);
438 }
439
440 /*
441 * The hash key of the lock dependency chains is a hash itself too:
442 * it's a hash of all locks taken up to that lock, including that lock.
443 * It's a 64-bit hash, because it's important for the keys to be
444 * unique.
445 */
iterate_chain_key(u64 key,u32 idx)446 static inline u64 iterate_chain_key(u64 key, u32 idx)
447 {
448 u32 k0 = key, k1 = key >> 32;
449
450 __jhash_mix(idx, k0, k1); /* Macro that modifies arguments! */
451
452 return k0 | (u64)k1 << 32;
453 }
454
lockdep_init_task(struct task_struct * task)455 void lockdep_init_task(struct task_struct *task)
456 {
457 task->lockdep_depth = 0; /* no locks held yet */
458 task->curr_chain_key = INITIAL_CHAIN_KEY;
459 task->lockdep_recursion = 0;
460 }
461
lockdep_recursion_inc(void)462 static __always_inline void lockdep_recursion_inc(void)
463 {
464 __this_cpu_inc(lockdep_recursion);
465 }
466
lockdep_recursion_finish(void)467 static __always_inline void lockdep_recursion_finish(void)
468 {
469 if (WARN_ON_ONCE(__this_cpu_dec_return(lockdep_recursion)))
470 __this_cpu_write(lockdep_recursion, 0);
471 }
472
lockdep_set_selftest_task(struct task_struct * task)473 void lockdep_set_selftest_task(struct task_struct *task)
474 {
475 lockdep_selftest_task_struct = task;
476 }
477
478 /*
479 * Debugging switches:
480 */
481
482 #define VERBOSE 0
483 #define VERY_VERBOSE 0
484
485 #if VERBOSE
486 # define HARDIRQ_VERBOSE 1
487 # define SOFTIRQ_VERBOSE 1
488 #else
489 # define HARDIRQ_VERBOSE 0
490 # define SOFTIRQ_VERBOSE 0
491 #endif
492
493 #if VERBOSE || HARDIRQ_VERBOSE || SOFTIRQ_VERBOSE
494 /*
495 * Quick filtering for interesting events:
496 */
class_filter(struct lock_class * class)497 static int class_filter(struct lock_class *class)
498 {
499 #if 0
500 /* Example */
501 if (class->name_version == 1 &&
502 !strcmp(class->name, "lockname"))
503 return 1;
504 if (class->name_version == 1 &&
505 !strcmp(class->name, "&struct->lockfield"))
506 return 1;
507 #endif
508 /* Filter everything else. 1 would be to allow everything else */
509 return 0;
510 }
511 #endif
512
verbose(struct lock_class * class)513 static int verbose(struct lock_class *class)
514 {
515 #if VERBOSE
516 return class_filter(class);
517 #endif
518 return 0;
519 }
520
print_lockdep_off(const char * bug_msg)521 static void print_lockdep_off(const char *bug_msg)
522 {
523 printk(KERN_DEBUG "%s\n", bug_msg);
524 printk(KERN_DEBUG "turning off the locking correctness validator.\n");
525 #ifdef CONFIG_LOCK_STAT
526 printk(KERN_DEBUG "Please attach the output of /proc/lock_stat to the bug report\n");
527 #endif
528 }
529
530 unsigned long nr_stack_trace_entries;
531
532 #ifdef CONFIG_PROVE_LOCKING
533 /**
534 * struct lock_trace - single stack backtrace
535 * @hash_entry: Entry in a stack_trace_hash[] list.
536 * @hash: jhash() of @entries.
537 * @nr_entries: Number of entries in @entries.
538 * @entries: Actual stack backtrace.
539 */
540 struct lock_trace {
541 struct hlist_node hash_entry;
542 u32 hash;
543 u32 nr_entries;
544 unsigned long entries[] __aligned(sizeof(unsigned long));
545 };
546 #define LOCK_TRACE_SIZE_IN_LONGS \
547 (sizeof(struct lock_trace) / sizeof(unsigned long))
548 /*
549 * Stack-trace: sequence of lock_trace structures. Protected by the graph_lock.
550 */
551 static unsigned long stack_trace[MAX_STACK_TRACE_ENTRIES];
552 static struct hlist_head stack_trace_hash[STACK_TRACE_HASH_SIZE];
553
traces_identical(struct lock_trace * t1,struct lock_trace * t2)554 static bool traces_identical(struct lock_trace *t1, struct lock_trace *t2)
555 {
556 return t1->hash == t2->hash && t1->nr_entries == t2->nr_entries &&
557 memcmp(t1->entries, t2->entries,
558 t1->nr_entries * sizeof(t1->entries[0])) == 0;
559 }
560
save_trace(void)561 static struct lock_trace *save_trace(void)
562 {
563 struct lock_trace *trace, *t2;
564 struct hlist_head *hash_head;
565 u32 hash;
566 int max_entries;
567
568 BUILD_BUG_ON_NOT_POWER_OF_2(STACK_TRACE_HASH_SIZE);
569 BUILD_BUG_ON(LOCK_TRACE_SIZE_IN_LONGS >= MAX_STACK_TRACE_ENTRIES);
570
571 trace = (struct lock_trace *)(stack_trace + nr_stack_trace_entries);
572 max_entries = MAX_STACK_TRACE_ENTRIES - nr_stack_trace_entries -
573 LOCK_TRACE_SIZE_IN_LONGS;
574
575 if (max_entries <= 0) {
576 if (!debug_locks_off_graph_unlock())
577 return NULL;
578
579 nbcon_cpu_emergency_enter();
580 print_lockdep_off("BUG: MAX_STACK_TRACE_ENTRIES too low!");
581 dump_stack();
582 nbcon_cpu_emergency_exit();
583
584 return NULL;
585 }
586 trace->nr_entries = stack_trace_save(trace->entries, max_entries, 3);
587
588 hash = jhash(trace->entries, trace->nr_entries *
589 sizeof(trace->entries[0]), 0);
590 trace->hash = hash;
591 hash_head = stack_trace_hash + (hash & (STACK_TRACE_HASH_SIZE - 1));
592 hlist_for_each_entry(t2, hash_head, hash_entry) {
593 if (traces_identical(trace, t2))
594 return t2;
595 }
596 nr_stack_trace_entries += LOCK_TRACE_SIZE_IN_LONGS + trace->nr_entries;
597 hlist_add_head(&trace->hash_entry, hash_head);
598
599 return trace;
600 }
601
602 /* Return the number of stack traces in the stack_trace[] array. */
lockdep_stack_trace_count(void)603 u64 lockdep_stack_trace_count(void)
604 {
605 struct lock_trace *trace;
606 u64 c = 0;
607 int i;
608
609 for (i = 0; i < ARRAY_SIZE(stack_trace_hash); i++) {
610 hlist_for_each_entry(trace, &stack_trace_hash[i], hash_entry) {
611 c++;
612 }
613 }
614
615 return c;
616 }
617
618 /* Return the number of stack hash chains that have at least one stack trace. */
lockdep_stack_hash_count(void)619 u64 lockdep_stack_hash_count(void)
620 {
621 u64 c = 0;
622 int i;
623
624 for (i = 0; i < ARRAY_SIZE(stack_trace_hash); i++)
625 if (!hlist_empty(&stack_trace_hash[i]))
626 c++;
627
628 return c;
629 }
630 #endif
631
632 unsigned int nr_hardirq_chains;
633 unsigned int nr_softirq_chains;
634 unsigned int nr_process_chains;
635 unsigned int max_lockdep_depth;
636
637 #ifdef CONFIG_DEBUG_LOCKDEP
638 /*
639 * Various lockdep statistics:
640 */
641 DEFINE_PER_CPU(struct lockdep_stats, lockdep_stats);
642 #endif
643
644 #ifdef CONFIG_PROVE_LOCKING
645 /*
646 * Locking printouts:
647 */
648
649 #define __USAGE(__STATE) \
650 [LOCK_USED_IN_##__STATE] = "IN-"__stringify(__STATE)"-W", \
651 [LOCK_ENABLED_##__STATE] = __stringify(__STATE)"-ON-W", \
652 [LOCK_USED_IN_##__STATE##_READ] = "IN-"__stringify(__STATE)"-R",\
653 [LOCK_ENABLED_##__STATE##_READ] = __stringify(__STATE)"-ON-R",
654
655 static const char *usage_str[] =
656 {
657 #define LOCKDEP_STATE(__STATE) __USAGE(__STATE)
658 #include "lockdep_states.h"
659 #undef LOCKDEP_STATE
660 [LOCK_USED] = "INITIAL USE",
661 [LOCK_USED_READ] = "INITIAL READ USE",
662 /* abused as string storage for verify_lock_unused() */
663 [LOCK_USAGE_STATES] = "IN-NMI",
664 };
665 #endif
666
__get_key_name(const struct lockdep_subclass_key * key,char * str)667 const char *__get_key_name(const struct lockdep_subclass_key *key, char *str)
668 {
669 return kallsyms_lookup((unsigned long)key, NULL, NULL, NULL, str);
670 }
671
lock_flag(enum lock_usage_bit bit)672 static inline unsigned long lock_flag(enum lock_usage_bit bit)
673 {
674 return 1UL << bit;
675 }
676
get_usage_char(struct lock_class * class,enum lock_usage_bit bit)677 static char get_usage_char(struct lock_class *class, enum lock_usage_bit bit)
678 {
679 /*
680 * The usage character defaults to '.' (i.e., irqs disabled and not in
681 * irq context), which is the safest usage category.
682 */
683 char c = '.';
684
685 /*
686 * The order of the following usage checks matters, which will
687 * result in the outcome character as follows:
688 *
689 * - '+': irq is enabled and not in irq context
690 * - '-': in irq context and irq is disabled
691 * - '?': in irq context and irq is enabled
692 */
693 if (class->usage_mask & lock_flag(bit + LOCK_USAGE_DIR_MASK)) {
694 c = '+';
695 if (class->usage_mask & lock_flag(bit))
696 c = '?';
697 } else if (class->usage_mask & lock_flag(bit))
698 c = '-';
699
700 return c;
701 }
702
get_usage_chars(struct lock_class * class,char usage[LOCK_USAGE_CHARS])703 void get_usage_chars(struct lock_class *class, char usage[LOCK_USAGE_CHARS])
704 {
705 int i = 0;
706
707 #define LOCKDEP_STATE(__STATE) \
708 usage[i++] = get_usage_char(class, LOCK_USED_IN_##__STATE); \
709 usage[i++] = get_usage_char(class, LOCK_USED_IN_##__STATE##_READ);
710 #include "lockdep_states.h"
711 #undef LOCKDEP_STATE
712
713 usage[i] = '\0';
714 }
715
__print_lock_name(struct held_lock * hlock,struct lock_class * class)716 static void __print_lock_name(struct held_lock *hlock, struct lock_class *class)
717 {
718 char str[KSYM_NAME_LEN];
719 const char *name;
720
721 name = class->name;
722 if (!name) {
723 name = __get_key_name(class->key, str);
724 printk(KERN_CONT "%s", name);
725 } else {
726 printk(KERN_CONT "%s", name);
727 if (class->name_version > 1)
728 printk(KERN_CONT "#%d", class->name_version);
729 if (class->subclass)
730 printk(KERN_CONT "/%d", class->subclass);
731 if (hlock && class->print_fn)
732 class->print_fn(hlock->instance);
733 }
734 }
735
print_lock_name(struct held_lock * hlock,struct lock_class * class)736 static void print_lock_name(struct held_lock *hlock, struct lock_class *class)
737 {
738 char usage[LOCK_USAGE_CHARS];
739
740 get_usage_chars(class, usage);
741
742 printk(KERN_CONT " (");
743 __print_lock_name(hlock, class);
744 printk(KERN_CONT "){%s}-{%d:%d}", usage,
745 class->wait_type_outer ?: class->wait_type_inner,
746 class->wait_type_inner);
747 }
748
print_lockdep_cache(struct lockdep_map * lock)749 static void print_lockdep_cache(struct lockdep_map *lock)
750 {
751 const char *name;
752 char str[KSYM_NAME_LEN];
753
754 name = lock->name;
755 if (!name)
756 name = __get_key_name(lock->key->subkeys, str);
757
758 printk(KERN_CONT "%s", name);
759 }
760
print_lock(struct held_lock * hlock)761 static void print_lock(struct held_lock *hlock)
762 {
763 /*
764 * We can be called locklessly through debug_show_all_locks() so be
765 * extra careful, the hlock might have been released and cleared.
766 *
767 * If this indeed happens, lets pretend it does not hurt to continue
768 * to print the lock unless the hlock class_idx does not point to a
769 * registered class. The rationale here is: since we don't attempt
770 * to distinguish whether we are in this situation, if it just
771 * happened we can't count on class_idx to tell either.
772 */
773 struct lock_class *lock = hlock_class(hlock);
774
775 if (!lock) {
776 printk(KERN_CONT "<RELEASED>\n");
777 return;
778 }
779
780 printk(KERN_CONT "%px", hlock->instance);
781 print_lock_name(hlock, lock);
782 printk(KERN_CONT ", at: %pS\n", (void *)hlock->acquire_ip);
783 }
784
lockdep_print_held_locks(struct task_struct * p)785 static void lockdep_print_held_locks(struct task_struct *p)
786 {
787 int i, depth = READ_ONCE(p->lockdep_depth);
788
789 if (!depth)
790 printk("no locks held by %s/%d.\n", p->comm, task_pid_nr(p));
791 else
792 printk("%d lock%s held by %s/%d:\n", depth,
793 str_plural(depth), p->comm, task_pid_nr(p));
794 /*
795 * It's not reliable to print a task's held locks if it's not sleeping
796 * and it's not the current task.
797 */
798 if (p != current && task_is_running(p))
799 return;
800 for (i = 0; i < depth; i++) {
801 printk(" #%d: ", i);
802 print_lock(p->held_locks + i);
803 }
804 }
805
print_kernel_ident(void)806 static void print_kernel_ident(void)
807 {
808 printk("%s %.*s %s\n", init_utsname()->release,
809 (int)strcspn(init_utsname()->version, " "),
810 init_utsname()->version,
811 print_tainted());
812 }
813
very_verbose(struct lock_class * class)814 static int very_verbose(struct lock_class *class)
815 {
816 #if VERY_VERBOSE
817 return class_filter(class);
818 #endif
819 return 0;
820 }
821
822 /*
823 * Is this the address of a static object:
824 */
825 #ifdef __KERNEL__
static_obj(const void * obj)826 static int static_obj(const void *obj)
827 {
828 unsigned long addr = (unsigned long) obj;
829
830 if (is_kernel_core_data(addr))
831 return 1;
832
833 /*
834 * keys are allowed in the __ro_after_init section.
835 */
836 if (is_kernel_rodata(addr))
837 return 1;
838
839 /*
840 * in initdata section and used during bootup only?
841 * NOTE: On some platforms the initdata section is
842 * outside of the _stext ... _end range.
843 */
844 if (system_state < SYSTEM_FREEING_INITMEM &&
845 init_section_contains((void *)addr, 1))
846 return 1;
847
848 /*
849 * in-kernel percpu var?
850 */
851 if (is_kernel_percpu_address(addr))
852 return 1;
853
854 /*
855 * module static or percpu var?
856 */
857 return is_module_address(addr) || is_module_percpu_address(addr);
858 }
859 #endif
860
861 /*
862 * To make lock name printouts unique, we calculate a unique
863 * class->name_version generation counter. The caller must hold the graph
864 * lock.
865 */
count_matching_names(struct lock_class * new_class)866 static int count_matching_names(struct lock_class *new_class)
867 {
868 struct lock_class *class;
869 int count = 0;
870
871 if (!new_class->name)
872 return 0;
873
874 list_for_each_entry(class, &all_lock_classes, lock_entry) {
875 if (new_class->key - new_class->subclass == class->key)
876 return class->name_version;
877 if (class->name && !strcmp(class->name, new_class->name))
878 count = max(count, class->name_version);
879 }
880
881 return count + 1;
882 }
883
884 /* used from NMI context -- must be lockless */
885 static noinstr struct lock_class *
look_up_lock_class(const struct lockdep_map * lock,unsigned int subclass)886 look_up_lock_class(const struct lockdep_map *lock, unsigned int subclass)
887 {
888 struct lockdep_subclass_key *key;
889 struct hlist_head *hash_head;
890 struct lock_class *class;
891
892 if (unlikely(subclass >= MAX_LOCKDEP_SUBCLASSES)) {
893 instrumentation_begin();
894 debug_locks_off();
895 nbcon_cpu_emergency_enter();
896 printk(KERN_ERR
897 "BUG: looking up invalid subclass: %u\n", subclass);
898 printk(KERN_ERR
899 "turning off the locking correctness validator.\n");
900 dump_stack();
901 nbcon_cpu_emergency_exit();
902 instrumentation_end();
903 return NULL;
904 }
905
906 /*
907 * If it is not initialised then it has never been locked,
908 * so it won't be present in the hash table.
909 */
910 if (unlikely(!lock->key))
911 return NULL;
912
913 /*
914 * NOTE: the class-key must be unique. For dynamic locks, a static
915 * lock_class_key variable is passed in through the mutex_init()
916 * (or spin_lock_init()) call - which acts as the key. For static
917 * locks we use the lock object itself as the key.
918 */
919 BUILD_BUG_ON(sizeof(struct lock_class_key) >
920 sizeof(struct lockdep_map));
921
922 key = lock->key->subkeys + subclass;
923
924 hash_head = classhashentry(key);
925
926 /*
927 * We do an RCU walk of the hash, see lockdep_free_key_range().
928 */
929 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
930 return NULL;
931
932 hlist_for_each_entry_rcu_notrace(class, hash_head, hash_entry) {
933 if (class->key == key) {
934 /*
935 * Huh! same key, different name? Did someone trample
936 * on some memory? We're most confused.
937 */
938 WARN_ONCE(class->name != lock->name &&
939 lock->key != &__lockdep_no_validate__,
940 "Looking for class \"%s\" with key %ps, but found a different class \"%s\" with the same key\n",
941 lock->name, lock->key, class->name);
942 return class;
943 }
944 }
945
946 return NULL;
947 }
948
949 /*
950 * Static locks do not have their class-keys yet - for them the key is
951 * the lock object itself. If the lock is in the per cpu area, the
952 * canonical address of the lock (per cpu offset removed) is used.
953 */
assign_lock_key(struct lockdep_map * lock)954 static bool assign_lock_key(struct lockdep_map *lock)
955 {
956 unsigned long can_addr, addr = (unsigned long)lock;
957
958 #ifdef __KERNEL__
959 /*
960 * lockdep_free_key_range() assumes that struct lock_class_key
961 * objects do not overlap. Since we use the address of lock
962 * objects as class key for static objects, check whether the
963 * size of lock_class_key objects does not exceed the size of
964 * the smallest lock object.
965 */
966 BUILD_BUG_ON(sizeof(struct lock_class_key) > sizeof(raw_spinlock_t));
967 #endif
968
969 if (__is_kernel_percpu_address(addr, &can_addr))
970 lock->key = (void *)can_addr;
971 else if (__is_module_percpu_address(addr, &can_addr))
972 lock->key = (void *)can_addr;
973 else if (static_obj(lock))
974 lock->key = (void *)lock;
975 else {
976 /* Debug-check: all keys must be persistent! */
977 debug_locks_off();
978 nbcon_cpu_emergency_enter();
979 pr_err("INFO: trying to register non-static key.\n");
980 pr_err("The code is fine but needs lockdep annotation, or maybe\n");
981 pr_err("you didn't initialize this object before use?\n");
982 pr_err("turning off the locking correctness validator.\n");
983 dump_stack();
984 nbcon_cpu_emergency_exit();
985 return false;
986 }
987
988 return true;
989 }
990
991 #ifdef CONFIG_DEBUG_LOCKDEP
992
993 /* Check whether element @e occurs in list @h */
in_list(struct list_head * e,struct list_head * h)994 static bool in_list(struct list_head *e, struct list_head *h)
995 {
996 struct list_head *f;
997
998 list_for_each(f, h) {
999 if (e == f)
1000 return true;
1001 }
1002
1003 return false;
1004 }
1005
1006 /*
1007 * Check whether entry @e occurs in any of the locks_after or locks_before
1008 * lists.
1009 */
in_any_class_list(struct list_head * e)1010 static bool in_any_class_list(struct list_head *e)
1011 {
1012 struct lock_class *class;
1013 int i;
1014
1015 for (i = 0; i < ARRAY_SIZE(lock_classes); i++) {
1016 class = &lock_classes[i];
1017 if (in_list(e, &class->locks_after) ||
1018 in_list(e, &class->locks_before))
1019 return true;
1020 }
1021 return false;
1022 }
1023
class_lock_list_valid(struct lock_class * c,struct list_head * h)1024 static bool class_lock_list_valid(struct lock_class *c, struct list_head *h)
1025 {
1026 struct lock_list *e;
1027
1028 list_for_each_entry(e, h, entry) {
1029 if (e->links_to != c) {
1030 printk(KERN_INFO "class %s: mismatch for lock entry %ld; class %s <> %s",
1031 c->name ? : "(?)",
1032 (unsigned long)(e - list_entries),
1033 e->links_to && e->links_to->name ?
1034 e->links_to->name : "(?)",
1035 e->class && e->class->name ? e->class->name :
1036 "(?)");
1037 return false;
1038 }
1039 }
1040 return true;
1041 }
1042
1043 #ifdef CONFIG_PROVE_LOCKING
1044 static u16 chain_hlocks[MAX_LOCKDEP_CHAIN_HLOCKS];
1045 #endif
1046
check_lock_chain_key(struct lock_chain * chain)1047 static bool check_lock_chain_key(struct lock_chain *chain)
1048 {
1049 #ifdef CONFIG_PROVE_LOCKING
1050 u64 chain_key = INITIAL_CHAIN_KEY;
1051 int i;
1052
1053 for (i = chain->base; i < chain->base + chain->depth; i++)
1054 chain_key = iterate_chain_key(chain_key, chain_hlocks[i]);
1055 /*
1056 * The 'unsigned long long' casts avoid that a compiler warning
1057 * is reported when building tools/lib/lockdep.
1058 */
1059 if (chain->chain_key != chain_key) {
1060 printk(KERN_INFO "chain %lld: key %#llx <> %#llx\n",
1061 (unsigned long long)(chain - lock_chains),
1062 (unsigned long long)chain->chain_key,
1063 (unsigned long long)chain_key);
1064 return false;
1065 }
1066 #endif
1067 return true;
1068 }
1069
in_any_zapped_class_list(struct lock_class * class)1070 static bool in_any_zapped_class_list(struct lock_class *class)
1071 {
1072 struct pending_free *pf;
1073 int i;
1074
1075 for (i = 0, pf = delayed_free.pf; i < ARRAY_SIZE(delayed_free.pf); i++, pf++) {
1076 if (in_list(&class->lock_entry, &pf->zapped))
1077 return true;
1078 }
1079
1080 return false;
1081 }
1082
__check_data_structures(void)1083 static bool __check_data_structures(void)
1084 {
1085 struct lock_class *class;
1086 struct lock_chain *chain;
1087 struct hlist_head *head;
1088 struct lock_list *e;
1089 int i;
1090
1091 /* Check whether all classes occur in a lock list. */
1092 for (i = 0; i < ARRAY_SIZE(lock_classes); i++) {
1093 class = &lock_classes[i];
1094 if (!in_list(&class->lock_entry, &all_lock_classes) &&
1095 !in_list(&class->lock_entry, &free_lock_classes) &&
1096 !in_any_zapped_class_list(class)) {
1097 printk(KERN_INFO "class %px/%s is not in any class list\n",
1098 class, class->name ? : "(?)");
1099 return false;
1100 }
1101 }
1102
1103 /* Check whether all classes have valid lock lists. */
1104 for (i = 0; i < ARRAY_SIZE(lock_classes); i++) {
1105 class = &lock_classes[i];
1106 if (!class_lock_list_valid(class, &class->locks_before))
1107 return false;
1108 if (!class_lock_list_valid(class, &class->locks_after))
1109 return false;
1110 }
1111
1112 /* Check the chain_key of all lock chains. */
1113 for (i = 0; i < ARRAY_SIZE(chainhash_table); i++) {
1114 head = chainhash_table + i;
1115 hlist_for_each_entry_rcu(chain, head, entry) {
1116 if (!check_lock_chain_key(chain))
1117 return false;
1118 }
1119 }
1120
1121 /*
1122 * Check whether all list entries that are in use occur in a class
1123 * lock list.
1124 */
1125 for_each_set_bit(i, list_entries_in_use, ARRAY_SIZE(list_entries)) {
1126 e = list_entries + i;
1127 if (!in_any_class_list(&e->entry)) {
1128 printk(KERN_INFO "list entry %d is not in any class list; class %s <> %s\n",
1129 (unsigned int)(e - list_entries),
1130 e->class->name ? : "(?)",
1131 e->links_to->name ? : "(?)");
1132 return false;
1133 }
1134 }
1135
1136 /*
1137 * Check whether all list entries that are not in use do not occur in
1138 * a class lock list.
1139 */
1140 for_each_clear_bit(i, list_entries_in_use, ARRAY_SIZE(list_entries)) {
1141 e = list_entries + i;
1142 if (in_any_class_list(&e->entry)) {
1143 printk(KERN_INFO "list entry %d occurs in a class list; class %s <> %s\n",
1144 (unsigned int)(e - list_entries),
1145 e->class && e->class->name ? e->class->name :
1146 "(?)",
1147 e->links_to && e->links_to->name ?
1148 e->links_to->name : "(?)");
1149 return false;
1150 }
1151 }
1152
1153 return true;
1154 }
1155
1156 int check_consistency = 0;
1157 module_param(check_consistency, int, 0644);
1158
check_data_structures(void)1159 static void check_data_structures(void)
1160 {
1161 static bool once = false;
1162
1163 if (check_consistency && !once) {
1164 if (!__check_data_structures()) {
1165 once = true;
1166 WARN_ON(once);
1167 }
1168 }
1169 }
1170
1171 #else /* CONFIG_DEBUG_LOCKDEP */
1172
check_data_structures(void)1173 static inline void check_data_structures(void) { }
1174
1175 #endif /* CONFIG_DEBUG_LOCKDEP */
1176
1177 static void init_chain_block_buckets(void);
1178
1179 /*
1180 * Initialize the lock_classes[] array elements, the free_lock_classes list
1181 * and also the delayed_free structure.
1182 */
init_data_structures_once(void)1183 static void init_data_structures_once(void)
1184 {
1185 static bool __read_mostly ds_initialized, rcu_head_initialized;
1186 int i;
1187
1188 if (likely(rcu_head_initialized))
1189 return;
1190
1191 if (system_state >= SYSTEM_SCHEDULING) {
1192 init_rcu_head(&delayed_free.rcu_head);
1193 rcu_head_initialized = true;
1194 }
1195
1196 if (ds_initialized)
1197 return;
1198
1199 ds_initialized = true;
1200
1201 INIT_LIST_HEAD(&delayed_free.pf[0].zapped);
1202 INIT_LIST_HEAD(&delayed_free.pf[1].zapped);
1203
1204 for (i = 0; i < ARRAY_SIZE(lock_classes); i++) {
1205 list_add_tail(&lock_classes[i].lock_entry, &free_lock_classes);
1206 INIT_LIST_HEAD(&lock_classes[i].locks_after);
1207 INIT_LIST_HEAD(&lock_classes[i].locks_before);
1208 }
1209 init_chain_block_buckets();
1210 }
1211
keyhashentry(const struct lock_class_key * key)1212 static inline struct hlist_head *keyhashentry(const struct lock_class_key *key)
1213 {
1214 unsigned long hash = hash_long((uintptr_t)key, KEYHASH_BITS);
1215
1216 return lock_keys_hash + hash;
1217 }
1218
1219 /* Register a dynamically allocated key. */
lockdep_register_key(struct lock_class_key * key)1220 void lockdep_register_key(struct lock_class_key *key)
1221 {
1222 struct hlist_head *hash_head;
1223 struct lock_class_key *k;
1224 unsigned long flags;
1225
1226 if (WARN_ON_ONCE(static_obj(key)))
1227 return;
1228 hash_head = keyhashentry(key);
1229
1230 raw_local_irq_save(flags);
1231 if (!graph_lock())
1232 goto restore_irqs;
1233 hlist_for_each_entry_rcu(k, hash_head, hash_entry) {
1234 if (WARN_ON_ONCE(k == key))
1235 goto out_unlock;
1236 }
1237 hlist_add_head_rcu(&key->hash_entry, hash_head);
1238 out_unlock:
1239 graph_unlock();
1240 restore_irqs:
1241 raw_local_irq_restore(flags);
1242 }
1243 EXPORT_SYMBOL_GPL(lockdep_register_key);
1244
1245 /* Check whether a key has been registered as a dynamic key. */
is_dynamic_key(const struct lock_class_key * key)1246 static bool is_dynamic_key(const struct lock_class_key *key)
1247 {
1248 struct hlist_head *hash_head;
1249 struct lock_class_key *k;
1250 bool found = false;
1251
1252 if (WARN_ON_ONCE(static_obj(key)))
1253 return false;
1254
1255 /*
1256 * If lock debugging is disabled lock_keys_hash[] may contain
1257 * pointers to memory that has already been freed. Avoid triggering
1258 * a use-after-free in that case by returning early.
1259 */
1260 if (!debug_locks)
1261 return true;
1262
1263 hash_head = keyhashentry(key);
1264
1265 rcu_read_lock();
1266 hlist_for_each_entry_rcu(k, hash_head, hash_entry) {
1267 if (k == key) {
1268 found = true;
1269 break;
1270 }
1271 }
1272 rcu_read_unlock();
1273
1274 return found;
1275 }
1276
1277 /*
1278 * Register a lock's class in the hash-table, if the class is not present
1279 * yet. Otherwise we look it up. We cache the result in the lock object
1280 * itself, so actual lookup of the hash should be once per lock object.
1281 */
1282 static struct lock_class *
register_lock_class(struct lockdep_map * lock,unsigned int subclass,int force)1283 register_lock_class(struct lockdep_map *lock, unsigned int subclass, int force)
1284 {
1285 struct lockdep_subclass_key *key;
1286 struct hlist_head *hash_head;
1287 struct lock_class *class;
1288 int idx;
1289
1290 DEBUG_LOCKS_WARN_ON(!irqs_disabled());
1291
1292 class = look_up_lock_class(lock, subclass);
1293 if (likely(class))
1294 goto out_set_class_cache;
1295
1296 if (!lock->key) {
1297 if (!assign_lock_key(lock))
1298 return NULL;
1299 } else if (!static_obj(lock->key) && !is_dynamic_key(lock->key)) {
1300 return NULL;
1301 }
1302
1303 key = lock->key->subkeys + subclass;
1304 hash_head = classhashentry(key);
1305
1306 if (!graph_lock()) {
1307 return NULL;
1308 }
1309 /*
1310 * We have to do the hash-walk again, to avoid races
1311 * with another CPU:
1312 */
1313 hlist_for_each_entry_rcu(class, hash_head, hash_entry) {
1314 if (class->key == key)
1315 goto out_unlock_set;
1316 }
1317
1318 init_data_structures_once();
1319
1320 /* Allocate a new lock class and add it to the hash. */
1321 class = list_first_entry_or_null(&free_lock_classes, typeof(*class),
1322 lock_entry);
1323 if (!class) {
1324 if (!debug_locks_off_graph_unlock()) {
1325 return NULL;
1326 }
1327
1328 nbcon_cpu_emergency_enter();
1329 print_lockdep_off("BUG: MAX_LOCKDEP_KEYS too low!");
1330 dump_stack();
1331 nbcon_cpu_emergency_exit();
1332 return NULL;
1333 }
1334 nr_lock_classes++;
1335 __set_bit(class - lock_classes, lock_classes_in_use);
1336 debug_atomic_inc(nr_unused_locks);
1337 class->key = key;
1338 class->name = lock->name;
1339 class->subclass = subclass;
1340 WARN_ON_ONCE(!list_empty(&class->locks_before));
1341 WARN_ON_ONCE(!list_empty(&class->locks_after));
1342 class->name_version = count_matching_names(class);
1343 class->wait_type_inner = lock->wait_type_inner;
1344 class->wait_type_outer = lock->wait_type_outer;
1345 class->lock_type = lock->lock_type;
1346 /*
1347 * We use RCU's safe list-add method to make
1348 * parallel walking of the hash-list safe:
1349 */
1350 hlist_add_head_rcu(&class->hash_entry, hash_head);
1351 /*
1352 * Remove the class from the free list and add it to the global list
1353 * of classes.
1354 */
1355 list_move_tail(&class->lock_entry, &all_lock_classes);
1356 idx = class - lock_classes;
1357 if (idx > max_lock_class_idx)
1358 max_lock_class_idx = idx;
1359
1360 if (verbose(class)) {
1361 graph_unlock();
1362
1363 nbcon_cpu_emergency_enter();
1364 printk("\nnew class %px: %s", class->key, class->name);
1365 if (class->name_version > 1)
1366 printk(KERN_CONT "#%d", class->name_version);
1367 printk(KERN_CONT "\n");
1368 dump_stack();
1369 nbcon_cpu_emergency_exit();
1370
1371 if (!graph_lock()) {
1372 return NULL;
1373 }
1374 }
1375 out_unlock_set:
1376 graph_unlock();
1377
1378 out_set_class_cache:
1379 if (!subclass || force)
1380 lock->class_cache[0] = class;
1381 else if (subclass < NR_LOCKDEP_CACHING_CLASSES)
1382 lock->class_cache[subclass] = class;
1383
1384 /*
1385 * Hash collision, did we smoke some? We found a class with a matching
1386 * hash but the subclass -- which is hashed in -- didn't match.
1387 */
1388 if (DEBUG_LOCKS_WARN_ON(class->subclass != subclass))
1389 return NULL;
1390
1391 return class;
1392 }
1393
1394 #ifdef CONFIG_PROVE_LOCKING
1395 /*
1396 * Allocate a lockdep entry. (assumes the graph_lock held, returns
1397 * with NULL on failure)
1398 */
alloc_list_entry(void)1399 static struct lock_list *alloc_list_entry(void)
1400 {
1401 int idx = find_first_zero_bit(list_entries_in_use,
1402 ARRAY_SIZE(list_entries));
1403
1404 if (idx >= ARRAY_SIZE(list_entries)) {
1405 if (!debug_locks_off_graph_unlock())
1406 return NULL;
1407
1408 nbcon_cpu_emergency_enter();
1409 print_lockdep_off("BUG: MAX_LOCKDEP_ENTRIES too low!");
1410 dump_stack();
1411 nbcon_cpu_emergency_exit();
1412 return NULL;
1413 }
1414 nr_list_entries++;
1415 __set_bit(idx, list_entries_in_use);
1416 return list_entries + idx;
1417 }
1418
1419 /*
1420 * Add a new dependency to the head of the list:
1421 */
add_lock_to_list(struct lock_class * this,struct lock_class * links_to,struct list_head * head,u16 distance,u8 dep,const struct lock_trace * trace)1422 static int add_lock_to_list(struct lock_class *this,
1423 struct lock_class *links_to, struct list_head *head,
1424 u16 distance, u8 dep,
1425 const struct lock_trace *trace)
1426 {
1427 struct lock_list *entry;
1428 /*
1429 * Lock not present yet - get a new dependency struct and
1430 * add it to the list:
1431 */
1432 entry = alloc_list_entry();
1433 if (!entry)
1434 return 0;
1435
1436 entry->class = this;
1437 entry->links_to = links_to;
1438 entry->dep = dep;
1439 entry->distance = distance;
1440 entry->trace = trace;
1441 /*
1442 * Both allocation and removal are done under the graph lock; but
1443 * iteration is under RCU-sched; see look_up_lock_class() and
1444 * lockdep_free_key_range().
1445 */
1446 list_add_tail_rcu(&entry->entry, head);
1447
1448 return 1;
1449 }
1450
1451 /*
1452 * For good efficiency of modular, we use power of 2
1453 */
1454 #define MAX_CIRCULAR_QUEUE_SIZE (1UL << CONFIG_LOCKDEP_CIRCULAR_QUEUE_BITS)
1455 #define CQ_MASK (MAX_CIRCULAR_QUEUE_SIZE-1)
1456
1457 /*
1458 * The circular_queue and helpers are used to implement graph
1459 * breadth-first search (BFS) algorithm, by which we can determine
1460 * whether there is a path from a lock to another. In deadlock checks,
1461 * a path from the next lock to be acquired to a previous held lock
1462 * indicates that adding the <prev> -> <next> lock dependency will
1463 * produce a circle in the graph. Breadth-first search instead of
1464 * depth-first search is used in order to find the shortest (circular)
1465 * path.
1466 */
1467 struct circular_queue {
1468 struct lock_list *element[MAX_CIRCULAR_QUEUE_SIZE];
1469 unsigned int front, rear;
1470 };
1471
1472 static struct circular_queue lock_cq;
1473
1474 unsigned int max_bfs_queue_depth;
1475
1476 static unsigned int lockdep_dependency_gen_id;
1477
__cq_init(struct circular_queue * cq)1478 static inline void __cq_init(struct circular_queue *cq)
1479 {
1480 cq->front = cq->rear = 0;
1481 lockdep_dependency_gen_id++;
1482 }
1483
__cq_empty(struct circular_queue * cq)1484 static inline int __cq_empty(struct circular_queue *cq)
1485 {
1486 return (cq->front == cq->rear);
1487 }
1488
__cq_full(struct circular_queue * cq)1489 static inline int __cq_full(struct circular_queue *cq)
1490 {
1491 return ((cq->rear + 1) & CQ_MASK) == cq->front;
1492 }
1493
__cq_enqueue(struct circular_queue * cq,struct lock_list * elem)1494 static inline int __cq_enqueue(struct circular_queue *cq, struct lock_list *elem)
1495 {
1496 if (__cq_full(cq))
1497 return -1;
1498
1499 cq->element[cq->rear] = elem;
1500 cq->rear = (cq->rear + 1) & CQ_MASK;
1501 return 0;
1502 }
1503
1504 /*
1505 * Dequeue an element from the circular_queue, return a lock_list if
1506 * the queue is not empty, or NULL if otherwise.
1507 */
__cq_dequeue(struct circular_queue * cq)1508 static inline struct lock_list * __cq_dequeue(struct circular_queue *cq)
1509 {
1510 struct lock_list * lock;
1511
1512 if (__cq_empty(cq))
1513 return NULL;
1514
1515 lock = cq->element[cq->front];
1516 cq->front = (cq->front + 1) & CQ_MASK;
1517
1518 return lock;
1519 }
1520
__cq_get_elem_count(struct circular_queue * cq)1521 static inline unsigned int __cq_get_elem_count(struct circular_queue *cq)
1522 {
1523 return (cq->rear - cq->front) & CQ_MASK;
1524 }
1525
mark_lock_accessed(struct lock_list * lock)1526 static inline void mark_lock_accessed(struct lock_list *lock)
1527 {
1528 lock->class->dep_gen_id = lockdep_dependency_gen_id;
1529 }
1530
visit_lock_entry(struct lock_list * lock,struct lock_list * parent)1531 static inline void visit_lock_entry(struct lock_list *lock,
1532 struct lock_list *parent)
1533 {
1534 lock->parent = parent;
1535 }
1536
lock_accessed(struct lock_list * lock)1537 static inline unsigned long lock_accessed(struct lock_list *lock)
1538 {
1539 return lock->class->dep_gen_id == lockdep_dependency_gen_id;
1540 }
1541
get_lock_parent(struct lock_list * child)1542 static inline struct lock_list *get_lock_parent(struct lock_list *child)
1543 {
1544 return child->parent;
1545 }
1546
get_lock_depth(struct lock_list * child)1547 static inline int get_lock_depth(struct lock_list *child)
1548 {
1549 int depth = 0;
1550 struct lock_list *parent;
1551
1552 while ((parent = get_lock_parent(child))) {
1553 child = parent;
1554 depth++;
1555 }
1556 return depth;
1557 }
1558
1559 /*
1560 * Return the forward or backward dependency list.
1561 *
1562 * @lock: the lock_list to get its class's dependency list
1563 * @offset: the offset to struct lock_class to determine whether it is
1564 * locks_after or locks_before
1565 */
get_dep_list(struct lock_list * lock,int offset)1566 static inline struct list_head *get_dep_list(struct lock_list *lock, int offset)
1567 {
1568 void *lock_class = lock->class;
1569
1570 return lock_class + offset;
1571 }
1572 /*
1573 * Return values of a bfs search:
1574 *
1575 * BFS_E* indicates an error
1576 * BFS_R* indicates a result (match or not)
1577 *
1578 * BFS_EINVALIDNODE: Find a invalid node in the graph.
1579 *
1580 * BFS_EQUEUEFULL: The queue is full while doing the bfs.
1581 *
1582 * BFS_RMATCH: Find the matched node in the graph, and put that node into
1583 * *@target_entry.
1584 *
1585 * BFS_RNOMATCH: Haven't found the matched node and keep *@target_entry
1586 * _unchanged_.
1587 */
1588 enum bfs_result {
1589 BFS_EINVALIDNODE = -2,
1590 BFS_EQUEUEFULL = -1,
1591 BFS_RMATCH = 0,
1592 BFS_RNOMATCH = 1,
1593 };
1594
1595 /*
1596 * bfs_result < 0 means error
1597 */
bfs_error(enum bfs_result res)1598 static inline bool bfs_error(enum bfs_result res)
1599 {
1600 return res < 0;
1601 }
1602
1603 /*
1604 * DEP_*_BIT in lock_list::dep
1605 *
1606 * For dependency @prev -> @next:
1607 *
1608 * SR: @prev is shared reader (->read != 0) and @next is recursive reader
1609 * (->read == 2)
1610 * ER: @prev is exclusive locker (->read == 0) and @next is recursive reader
1611 * SN: @prev is shared reader and @next is non-recursive locker (->read != 2)
1612 * EN: @prev is exclusive locker and @next is non-recursive locker
1613 *
1614 * Note that we define the value of DEP_*_BITs so that:
1615 * bit0 is prev->read == 0
1616 * bit1 is next->read != 2
1617 */
1618 #define DEP_SR_BIT (0 + (0 << 1)) /* 0 */
1619 #define DEP_ER_BIT (1 + (0 << 1)) /* 1 */
1620 #define DEP_SN_BIT (0 + (1 << 1)) /* 2 */
1621 #define DEP_EN_BIT (1 + (1 << 1)) /* 3 */
1622
1623 #define DEP_SR_MASK (1U << (DEP_SR_BIT))
1624 #define DEP_ER_MASK (1U << (DEP_ER_BIT))
1625 #define DEP_SN_MASK (1U << (DEP_SN_BIT))
1626 #define DEP_EN_MASK (1U << (DEP_EN_BIT))
1627
1628 static inline unsigned int
__calc_dep_bit(struct held_lock * prev,struct held_lock * next)1629 __calc_dep_bit(struct held_lock *prev, struct held_lock *next)
1630 {
1631 return (prev->read == 0) + ((next->read != 2) << 1);
1632 }
1633
calc_dep(struct held_lock * prev,struct held_lock * next)1634 static inline u8 calc_dep(struct held_lock *prev, struct held_lock *next)
1635 {
1636 return 1U << __calc_dep_bit(prev, next);
1637 }
1638
1639 /*
1640 * calculate the dep_bit for backwards edges. We care about whether @prev is
1641 * shared and whether @next is recursive.
1642 */
1643 static inline unsigned int
__calc_dep_bitb(struct held_lock * prev,struct held_lock * next)1644 __calc_dep_bitb(struct held_lock *prev, struct held_lock *next)
1645 {
1646 return (next->read != 2) + ((prev->read == 0) << 1);
1647 }
1648
calc_depb(struct held_lock * prev,struct held_lock * next)1649 static inline u8 calc_depb(struct held_lock *prev, struct held_lock *next)
1650 {
1651 return 1U << __calc_dep_bitb(prev, next);
1652 }
1653
1654 /*
1655 * Initialize a lock_list entry @lock belonging to @class as the root for a BFS
1656 * search.
1657 */
__bfs_init_root(struct lock_list * lock,struct lock_class * class)1658 static inline void __bfs_init_root(struct lock_list *lock,
1659 struct lock_class *class)
1660 {
1661 lock->class = class;
1662 lock->parent = NULL;
1663 lock->only_xr = 0;
1664 }
1665
1666 /*
1667 * Initialize a lock_list entry @lock based on a lock acquisition @hlock as the
1668 * root for a BFS search.
1669 *
1670 * ->only_xr of the initial lock node is set to @hlock->read == 2, to make sure
1671 * that <prev> -> @hlock and @hlock -> <whatever __bfs() found> is not -(*R)->
1672 * and -(S*)->.
1673 */
bfs_init_root(struct lock_list * lock,struct held_lock * hlock)1674 static inline void bfs_init_root(struct lock_list *lock,
1675 struct held_lock *hlock)
1676 {
1677 __bfs_init_root(lock, hlock_class(hlock));
1678 lock->only_xr = (hlock->read == 2);
1679 }
1680
1681 /*
1682 * Similar to bfs_init_root() but initialize the root for backwards BFS.
1683 *
1684 * ->only_xr of the initial lock node is set to @hlock->read != 0, to make sure
1685 * that <next> -> @hlock and @hlock -> <whatever backwards BFS found> is not
1686 * -(*S)-> and -(R*)-> (reverse order of -(*R)-> and -(S*)->).
1687 */
bfs_init_rootb(struct lock_list * lock,struct held_lock * hlock)1688 static inline void bfs_init_rootb(struct lock_list *lock,
1689 struct held_lock *hlock)
1690 {
1691 __bfs_init_root(lock, hlock_class(hlock));
1692 lock->only_xr = (hlock->read != 0);
1693 }
1694
__bfs_next(struct lock_list * lock,int offset)1695 static inline struct lock_list *__bfs_next(struct lock_list *lock, int offset)
1696 {
1697 if (!lock || !lock->parent)
1698 return NULL;
1699
1700 return list_next_or_null_rcu(get_dep_list(lock->parent, offset),
1701 &lock->entry, struct lock_list, entry);
1702 }
1703
1704 /*
1705 * Breadth-First Search to find a strong path in the dependency graph.
1706 *
1707 * @source_entry: the source of the path we are searching for.
1708 * @data: data used for the second parameter of @match function
1709 * @match: match function for the search
1710 * @target_entry: pointer to the target of a matched path
1711 * @offset: the offset to struct lock_class to determine whether it is
1712 * locks_after or locks_before
1713 *
1714 * We may have multiple edges (considering different kinds of dependencies,
1715 * e.g. ER and SN) between two nodes in the dependency graph. But
1716 * only the strong dependency path in the graph is relevant to deadlocks. A
1717 * strong dependency path is a dependency path that doesn't have two adjacent
1718 * dependencies as -(*R)-> -(S*)->, please see:
1719 *
1720 * Documentation/locking/lockdep-design.rst
1721 *
1722 * for more explanation of the definition of strong dependency paths
1723 *
1724 * In __bfs(), we only traverse in the strong dependency path:
1725 *
1726 * In lock_list::only_xr, we record whether the previous dependency only
1727 * has -(*R)-> in the search, and if it does (prev only has -(*R)->), we
1728 * filter out any -(S*)-> in the current dependency and after that, the
1729 * ->only_xr is set according to whether we only have -(*R)-> left.
1730 */
__bfs(struct lock_list * source_entry,void * data,bool (* match)(struct lock_list * entry,void * data),bool (* skip)(struct lock_list * entry,void * data),struct lock_list ** target_entry,int offset)1731 static enum bfs_result __bfs(struct lock_list *source_entry,
1732 void *data,
1733 bool (*match)(struct lock_list *entry, void *data),
1734 bool (*skip)(struct lock_list *entry, void *data),
1735 struct lock_list **target_entry,
1736 int offset)
1737 {
1738 struct circular_queue *cq = &lock_cq;
1739 struct lock_list *lock = NULL;
1740 struct lock_list *entry;
1741 struct list_head *head;
1742 unsigned int cq_depth;
1743 bool first;
1744
1745 lockdep_assert_locked();
1746
1747 __cq_init(cq);
1748 __cq_enqueue(cq, source_entry);
1749
1750 while ((lock = __bfs_next(lock, offset)) || (lock = __cq_dequeue(cq))) {
1751 if (!lock->class)
1752 return BFS_EINVALIDNODE;
1753
1754 /*
1755 * Step 1: check whether we already finish on this one.
1756 *
1757 * If we have visited all the dependencies from this @lock to
1758 * others (iow, if we have visited all lock_list entries in
1759 * @lock->class->locks_{after,before}) we skip, otherwise go
1760 * and visit all the dependencies in the list and mark this
1761 * list accessed.
1762 */
1763 if (lock_accessed(lock))
1764 continue;
1765 else
1766 mark_lock_accessed(lock);
1767
1768 /*
1769 * Step 2: check whether prev dependency and this form a strong
1770 * dependency path.
1771 */
1772 if (lock->parent) { /* Parent exists, check prev dependency */
1773 u8 dep = lock->dep;
1774 bool prev_only_xr = lock->parent->only_xr;
1775
1776 /*
1777 * Mask out all -(S*)-> if we only have *R in previous
1778 * step, because -(*R)-> -(S*)-> don't make up a strong
1779 * dependency.
1780 */
1781 if (prev_only_xr)
1782 dep &= ~(DEP_SR_MASK | DEP_SN_MASK);
1783
1784 /* If nothing left, we skip */
1785 if (!dep)
1786 continue;
1787
1788 /* If there are only -(*R)-> left, set that for the next step */
1789 lock->only_xr = !(dep & (DEP_SN_MASK | DEP_EN_MASK));
1790 }
1791
1792 /*
1793 * Step 3: we haven't visited this and there is a strong
1794 * dependency path to this, so check with @match.
1795 * If @skip is provide and returns true, we skip this
1796 * lock (and any path this lock is in).
1797 */
1798 if (skip && skip(lock, data))
1799 continue;
1800
1801 if (match(lock, data)) {
1802 *target_entry = lock;
1803 return BFS_RMATCH;
1804 }
1805
1806 /*
1807 * Step 4: if not match, expand the path by adding the
1808 * forward or backwards dependencies in the search
1809 *
1810 */
1811 first = true;
1812 head = get_dep_list(lock, offset);
1813 list_for_each_entry_rcu(entry, head, entry) {
1814 visit_lock_entry(entry, lock);
1815
1816 /*
1817 * Note we only enqueue the first of the list into the
1818 * queue, because we can always find a sibling
1819 * dependency from one (see __bfs_next()), as a result
1820 * the space of queue is saved.
1821 */
1822 if (!first)
1823 continue;
1824
1825 first = false;
1826
1827 if (__cq_enqueue(cq, entry))
1828 return BFS_EQUEUEFULL;
1829
1830 cq_depth = __cq_get_elem_count(cq);
1831 if (max_bfs_queue_depth < cq_depth)
1832 max_bfs_queue_depth = cq_depth;
1833 }
1834 }
1835
1836 return BFS_RNOMATCH;
1837 }
1838
1839 static inline enum bfs_result
__bfs_forwards(struct lock_list * src_entry,void * data,bool (* match)(struct lock_list * entry,void * data),bool (* skip)(struct lock_list * entry,void * data),struct lock_list ** target_entry)1840 __bfs_forwards(struct lock_list *src_entry,
1841 void *data,
1842 bool (*match)(struct lock_list *entry, void *data),
1843 bool (*skip)(struct lock_list *entry, void *data),
1844 struct lock_list **target_entry)
1845 {
1846 return __bfs(src_entry, data, match, skip, target_entry,
1847 offsetof(struct lock_class, locks_after));
1848
1849 }
1850
1851 static inline enum bfs_result
__bfs_backwards(struct lock_list * src_entry,void * data,bool (* match)(struct lock_list * entry,void * data),bool (* skip)(struct lock_list * entry,void * data),struct lock_list ** target_entry)1852 __bfs_backwards(struct lock_list *src_entry,
1853 void *data,
1854 bool (*match)(struct lock_list *entry, void *data),
1855 bool (*skip)(struct lock_list *entry, void *data),
1856 struct lock_list **target_entry)
1857 {
1858 return __bfs(src_entry, data, match, skip, target_entry,
1859 offsetof(struct lock_class, locks_before));
1860
1861 }
1862
print_lock_trace(const struct lock_trace * trace,unsigned int spaces)1863 static void print_lock_trace(const struct lock_trace *trace,
1864 unsigned int spaces)
1865 {
1866 stack_trace_print(trace->entries, trace->nr_entries, spaces);
1867 }
1868
1869 /*
1870 * Print a dependency chain entry (this is only done when a deadlock
1871 * has been detected):
1872 */
1873 static noinline void
print_circular_bug_entry(struct lock_list * target,int depth)1874 print_circular_bug_entry(struct lock_list *target, int depth)
1875 {
1876 if (debug_locks_silent)
1877 return;
1878 printk("\n-> #%u", depth);
1879 print_lock_name(NULL, target->class);
1880 printk(KERN_CONT ":\n");
1881 print_lock_trace(target->trace, 6);
1882 }
1883
1884 static void
print_circular_lock_scenario(struct held_lock * src,struct held_lock * tgt,struct lock_list * prt)1885 print_circular_lock_scenario(struct held_lock *src,
1886 struct held_lock *tgt,
1887 struct lock_list *prt)
1888 {
1889 struct lock_class *source = hlock_class(src);
1890 struct lock_class *target = hlock_class(tgt);
1891 struct lock_class *parent = prt->class;
1892 int src_read = src->read;
1893 int tgt_read = tgt->read;
1894
1895 /*
1896 * A direct locking problem where unsafe_class lock is taken
1897 * directly by safe_class lock, then all we need to show
1898 * is the deadlock scenario, as it is obvious that the
1899 * unsafe lock is taken under the safe lock.
1900 *
1901 * But if there is a chain instead, where the safe lock takes
1902 * an intermediate lock (middle_class) where this lock is
1903 * not the same as the safe lock, then the lock chain is
1904 * used to describe the problem. Otherwise we would need
1905 * to show a different CPU case for each link in the chain
1906 * from the safe_class lock to the unsafe_class lock.
1907 */
1908 if (parent != source) {
1909 printk("Chain exists of:\n ");
1910 __print_lock_name(src, source);
1911 printk(KERN_CONT " --> ");
1912 __print_lock_name(NULL, parent);
1913 printk(KERN_CONT " --> ");
1914 __print_lock_name(tgt, target);
1915 printk(KERN_CONT "\n\n");
1916 }
1917
1918 printk(" Possible unsafe locking scenario:\n\n");
1919 printk(" CPU0 CPU1\n");
1920 printk(" ---- ----\n");
1921 if (tgt_read != 0)
1922 printk(" rlock(");
1923 else
1924 printk(" lock(");
1925 __print_lock_name(tgt, target);
1926 printk(KERN_CONT ");\n");
1927 printk(" lock(");
1928 __print_lock_name(NULL, parent);
1929 printk(KERN_CONT ");\n");
1930 printk(" lock(");
1931 __print_lock_name(tgt, target);
1932 printk(KERN_CONT ");\n");
1933 if (src_read != 0)
1934 printk(" rlock(");
1935 else if (src->sync)
1936 printk(" sync(");
1937 else
1938 printk(" lock(");
1939 __print_lock_name(src, source);
1940 printk(KERN_CONT ");\n");
1941 printk("\n *** DEADLOCK ***\n\n");
1942 }
1943
1944 /*
1945 * When a circular dependency is detected, print the
1946 * header first:
1947 */
1948 static noinline void
print_circular_bug_header(struct lock_list * entry,unsigned int depth,struct held_lock * check_src,struct held_lock * check_tgt)1949 print_circular_bug_header(struct lock_list *entry, unsigned int depth,
1950 struct held_lock *check_src,
1951 struct held_lock *check_tgt)
1952 {
1953 struct task_struct *curr = current;
1954
1955 if (debug_locks_silent)
1956 return;
1957
1958 pr_warn("\n");
1959 pr_warn("======================================================\n");
1960 pr_warn("WARNING: possible circular locking dependency detected\n");
1961 print_kernel_ident();
1962 pr_warn("------------------------------------------------------\n");
1963 pr_warn("%s/%d is trying to acquire lock:\n",
1964 curr->comm, task_pid_nr(curr));
1965 print_lock(check_src);
1966
1967 pr_warn("\nbut task is already holding lock:\n");
1968
1969 print_lock(check_tgt);
1970 pr_warn("\nwhich lock already depends on the new lock.\n\n");
1971 pr_warn("\nthe existing dependency chain (in reverse order) is:\n");
1972
1973 print_circular_bug_entry(entry, depth);
1974 }
1975
1976 /*
1977 * We are about to add A -> B into the dependency graph, and in __bfs() a
1978 * strong dependency path A -> .. -> B is found: hlock_class equals
1979 * entry->class.
1980 *
1981 * If A -> .. -> B can replace A -> B in any __bfs() search (means the former
1982 * is _stronger_ than or equal to the latter), we consider A -> B as redundant.
1983 * For example if A -> .. -> B is -(EN)-> (i.e. A -(E*)-> .. -(*N)-> B), and A
1984 * -> B is -(ER)-> or -(EN)->, then we don't need to add A -> B into the
1985 * dependency graph, as any strong path ..-> A -> B ->.. we can get with
1986 * having dependency A -> B, we could already get a equivalent path ..-> A ->
1987 * .. -> B -> .. with A -> .. -> B. Therefore A -> B is redundant.
1988 *
1989 * We need to make sure both the start and the end of A -> .. -> B is not
1990 * weaker than A -> B. For the start part, please see the comment in
1991 * check_redundant(). For the end part, we need:
1992 *
1993 * Either
1994 *
1995 * a) A -> B is -(*R)-> (everything is not weaker than that)
1996 *
1997 * or
1998 *
1999 * b) A -> .. -> B is -(*N)-> (nothing is stronger than this)
2000 *
2001 */
hlock_equal(struct lock_list * entry,void * data)2002 static inline bool hlock_equal(struct lock_list *entry, void *data)
2003 {
2004 struct held_lock *hlock = (struct held_lock *)data;
2005
2006 return hlock_class(hlock) == entry->class && /* Found A -> .. -> B */
2007 (hlock->read == 2 || /* A -> B is -(*R)-> */
2008 !entry->only_xr); /* A -> .. -> B is -(*N)-> */
2009 }
2010
2011 /*
2012 * We are about to add B -> A into the dependency graph, and in __bfs() a
2013 * strong dependency path A -> .. -> B is found: hlock_class equals
2014 * entry->class.
2015 *
2016 * We will have a deadlock case (conflict) if A -> .. -> B -> A is a strong
2017 * dependency cycle, that means:
2018 *
2019 * Either
2020 *
2021 * a) B -> A is -(E*)->
2022 *
2023 * or
2024 *
2025 * b) A -> .. -> B is -(*N)-> (i.e. A -> .. -(*N)-> B)
2026 *
2027 * as then we don't have -(*R)-> -(S*)-> in the cycle.
2028 */
hlock_conflict(struct lock_list * entry,void * data)2029 static inline bool hlock_conflict(struct lock_list *entry, void *data)
2030 {
2031 struct held_lock *hlock = (struct held_lock *)data;
2032
2033 return hlock_class(hlock) == entry->class && /* Found A -> .. -> B */
2034 (hlock->read == 0 || /* B -> A is -(E*)-> */
2035 !entry->only_xr); /* A -> .. -> B is -(*N)-> */
2036 }
2037
print_circular_bug(struct lock_list * this,struct lock_list * target,struct held_lock * check_src,struct held_lock * check_tgt)2038 static noinline void print_circular_bug(struct lock_list *this,
2039 struct lock_list *target,
2040 struct held_lock *check_src,
2041 struct held_lock *check_tgt)
2042 {
2043 struct task_struct *curr = current;
2044 struct lock_list *parent;
2045 struct lock_list *first_parent;
2046 int depth;
2047
2048 if (!debug_locks_off_graph_unlock() || debug_locks_silent)
2049 return;
2050
2051 this->trace = save_trace();
2052 if (!this->trace)
2053 return;
2054
2055 depth = get_lock_depth(target);
2056
2057 nbcon_cpu_emergency_enter();
2058
2059 print_circular_bug_header(target, depth, check_src, check_tgt);
2060
2061 parent = get_lock_parent(target);
2062 first_parent = parent;
2063
2064 while (parent) {
2065 print_circular_bug_entry(parent, --depth);
2066 parent = get_lock_parent(parent);
2067 }
2068
2069 printk("\nother info that might help us debug this:\n\n");
2070 print_circular_lock_scenario(check_src, check_tgt,
2071 first_parent);
2072
2073 lockdep_print_held_locks(curr);
2074
2075 printk("\nstack backtrace:\n");
2076 dump_stack();
2077
2078 nbcon_cpu_emergency_exit();
2079 }
2080
print_bfs_bug(int ret)2081 static noinline void print_bfs_bug(int ret)
2082 {
2083 if (!debug_locks_off_graph_unlock())
2084 return;
2085
2086 /*
2087 * Breadth-first-search failed, graph got corrupted?
2088 */
2089 if (ret == BFS_EQUEUEFULL)
2090 pr_warn("Increase LOCKDEP_CIRCULAR_QUEUE_BITS to avoid this warning:\n");
2091
2092 WARN(1, "lockdep bfs error:%d\n", ret);
2093 }
2094
noop_count(struct lock_list * entry,void * data)2095 static bool noop_count(struct lock_list *entry, void *data)
2096 {
2097 (*(unsigned long *)data)++;
2098 return false;
2099 }
2100
__lockdep_count_forward_deps(struct lock_list * this)2101 static unsigned long __lockdep_count_forward_deps(struct lock_list *this)
2102 {
2103 unsigned long count = 0;
2104 struct lock_list *target_entry;
2105
2106 __bfs_forwards(this, (void *)&count, noop_count, NULL, &target_entry);
2107
2108 return count;
2109 }
lockdep_count_forward_deps(struct lock_class * class)2110 unsigned long lockdep_count_forward_deps(struct lock_class *class)
2111 {
2112 unsigned long ret, flags;
2113 struct lock_list this;
2114
2115 __bfs_init_root(&this, class);
2116
2117 raw_local_irq_save(flags);
2118 lockdep_lock();
2119 ret = __lockdep_count_forward_deps(&this);
2120 lockdep_unlock();
2121 raw_local_irq_restore(flags);
2122
2123 return ret;
2124 }
2125
__lockdep_count_backward_deps(struct lock_list * this)2126 static unsigned long __lockdep_count_backward_deps(struct lock_list *this)
2127 {
2128 unsigned long count = 0;
2129 struct lock_list *target_entry;
2130
2131 __bfs_backwards(this, (void *)&count, noop_count, NULL, &target_entry);
2132
2133 return count;
2134 }
2135
lockdep_count_backward_deps(struct lock_class * class)2136 unsigned long lockdep_count_backward_deps(struct lock_class *class)
2137 {
2138 unsigned long ret, flags;
2139 struct lock_list this;
2140
2141 __bfs_init_root(&this, class);
2142
2143 raw_local_irq_save(flags);
2144 lockdep_lock();
2145 ret = __lockdep_count_backward_deps(&this);
2146 lockdep_unlock();
2147 raw_local_irq_restore(flags);
2148
2149 return ret;
2150 }
2151
2152 /*
2153 * Check that the dependency graph starting at <src> can lead to
2154 * <target> or not.
2155 */
2156 static noinline enum bfs_result
check_path(struct held_lock * target,struct lock_list * src_entry,bool (* match)(struct lock_list * entry,void * data),bool (* skip)(struct lock_list * entry,void * data),struct lock_list ** target_entry)2157 check_path(struct held_lock *target, struct lock_list *src_entry,
2158 bool (*match)(struct lock_list *entry, void *data),
2159 bool (*skip)(struct lock_list *entry, void *data),
2160 struct lock_list **target_entry)
2161 {
2162 enum bfs_result ret;
2163
2164 ret = __bfs_forwards(src_entry, target, match, skip, target_entry);
2165
2166 if (unlikely(bfs_error(ret)))
2167 print_bfs_bug(ret);
2168
2169 return ret;
2170 }
2171
2172 static void print_deadlock_bug(struct task_struct *, struct held_lock *, struct held_lock *);
2173
2174 /*
2175 * Prove that the dependency graph starting at <src> can not
2176 * lead to <target>. If it can, there is a circle when adding
2177 * <target> -> <src> dependency.
2178 *
2179 * Print an error and return BFS_RMATCH if it does.
2180 */
2181 static noinline enum bfs_result
check_noncircular(struct held_lock * src,struct held_lock * target,struct lock_trace ** const trace)2182 check_noncircular(struct held_lock *src, struct held_lock *target,
2183 struct lock_trace **const trace)
2184 {
2185 enum bfs_result ret;
2186 struct lock_list *target_entry;
2187 struct lock_list src_entry;
2188
2189 bfs_init_root(&src_entry, src);
2190
2191 debug_atomic_inc(nr_cyclic_checks);
2192
2193 ret = check_path(target, &src_entry, hlock_conflict, NULL, &target_entry);
2194
2195 if (unlikely(ret == BFS_RMATCH)) {
2196 if (!*trace) {
2197 /*
2198 * If save_trace fails here, the printing might
2199 * trigger a WARN but because of the !nr_entries it
2200 * should not do bad things.
2201 */
2202 *trace = save_trace();
2203 }
2204
2205 if (src->class_idx == target->class_idx)
2206 print_deadlock_bug(current, src, target);
2207 else
2208 print_circular_bug(&src_entry, target_entry, src, target);
2209 }
2210
2211 return ret;
2212 }
2213
2214 #ifdef CONFIG_TRACE_IRQFLAGS
2215
2216 /*
2217 * Forwards and backwards subgraph searching, for the purposes of
2218 * proving that two subgraphs can be connected by a new dependency
2219 * without creating any illegal irq-safe -> irq-unsafe lock dependency.
2220 *
2221 * A irq safe->unsafe deadlock happens with the following conditions:
2222 *
2223 * 1) We have a strong dependency path A -> ... -> B
2224 *
2225 * 2) and we have ENABLED_IRQ usage of B and USED_IN_IRQ usage of A, therefore
2226 * irq can create a new dependency B -> A (consider the case that a holder
2227 * of B gets interrupted by an irq whose handler will try to acquire A).
2228 *
2229 * 3) the dependency circle A -> ... -> B -> A we get from 1) and 2) is a
2230 * strong circle:
2231 *
2232 * For the usage bits of B:
2233 * a) if A -> B is -(*N)->, then B -> A could be any type, so any
2234 * ENABLED_IRQ usage suffices.
2235 * b) if A -> B is -(*R)->, then B -> A must be -(E*)->, so only
2236 * ENABLED_IRQ_*_READ usage suffices.
2237 *
2238 * For the usage bits of A:
2239 * c) if A -> B is -(E*)->, then B -> A could be any type, so any
2240 * USED_IN_IRQ usage suffices.
2241 * d) if A -> B is -(S*)->, then B -> A must be -(*N)->, so only
2242 * USED_IN_IRQ_*_READ usage suffices.
2243 */
2244
2245 /*
2246 * There is a strong dependency path in the dependency graph: A -> B, and now
2247 * we need to decide which usage bit of A should be accumulated to detect
2248 * safe->unsafe bugs.
2249 *
2250 * Note that usage_accumulate() is used in backwards search, so ->only_xr
2251 * stands for whether A -> B only has -(S*)-> (in this case ->only_xr is true).
2252 *
2253 * As above, if only_xr is false, which means A -> B has -(E*)-> dependency
2254 * path, any usage of A should be considered. Otherwise, we should only
2255 * consider _READ usage.
2256 */
usage_accumulate(struct lock_list * entry,void * mask)2257 static inline bool usage_accumulate(struct lock_list *entry, void *mask)
2258 {
2259 if (!entry->only_xr)
2260 *(unsigned long *)mask |= entry->class->usage_mask;
2261 else /* Mask out _READ usage bits */
2262 *(unsigned long *)mask |= (entry->class->usage_mask & LOCKF_IRQ);
2263
2264 return false;
2265 }
2266
2267 /*
2268 * There is a strong dependency path in the dependency graph: A -> B, and now
2269 * we need to decide which usage bit of B conflicts with the usage bits of A,
2270 * i.e. which usage bit of B may introduce safe->unsafe deadlocks.
2271 *
2272 * As above, if only_xr is false, which means A -> B has -(*N)-> dependency
2273 * path, any usage of B should be considered. Otherwise, we should only
2274 * consider _READ usage.
2275 */
usage_match(struct lock_list * entry,void * mask)2276 static inline bool usage_match(struct lock_list *entry, void *mask)
2277 {
2278 if (!entry->only_xr)
2279 return !!(entry->class->usage_mask & *(unsigned long *)mask);
2280 else /* Mask out _READ usage bits */
2281 return !!((entry->class->usage_mask & LOCKF_IRQ) & *(unsigned long *)mask);
2282 }
2283
usage_skip(struct lock_list * entry,void * mask)2284 static inline bool usage_skip(struct lock_list *entry, void *mask)
2285 {
2286 if (entry->class->lock_type == LD_LOCK_NORMAL)
2287 return false;
2288
2289 /*
2290 * Skip local_lock() for irq inversion detection.
2291 *
2292 * For !RT, local_lock() is not a real lock, so it won't carry any
2293 * dependency.
2294 *
2295 * For RT, an irq inversion happens when we have lock A and B, and on
2296 * some CPU we can have:
2297 *
2298 * lock(A);
2299 * <interrupted>
2300 * lock(B);
2301 *
2302 * where lock(B) cannot sleep, and we have a dependency B -> ... -> A.
2303 *
2304 * Now we prove local_lock() cannot exist in that dependency. First we
2305 * have the observation for any lock chain L1 -> ... -> Ln, for any
2306 * 1 <= i <= n, Li.inner_wait_type <= L1.inner_wait_type, otherwise
2307 * wait context check will complain. And since B is not a sleep lock,
2308 * therefore B.inner_wait_type >= 2, and since the inner_wait_type of
2309 * local_lock() is 3, which is greater than 2, therefore there is no
2310 * way the local_lock() exists in the dependency B -> ... -> A.
2311 *
2312 * As a result, we will skip local_lock(), when we search for irq
2313 * inversion bugs.
2314 */
2315 if (entry->class->lock_type == LD_LOCK_PERCPU &&
2316 DEBUG_LOCKS_WARN_ON(entry->class->wait_type_inner < LD_WAIT_CONFIG))
2317 return false;
2318
2319 /*
2320 * Skip WAIT_OVERRIDE for irq inversion detection -- it's not actually
2321 * a lock and only used to override the wait_type.
2322 */
2323
2324 return true;
2325 }
2326
2327 /*
2328 * Find a node in the forwards-direction dependency sub-graph starting
2329 * at @root->class that matches @bit.
2330 *
2331 * Return BFS_MATCH if such a node exists in the subgraph, and put that node
2332 * into *@target_entry.
2333 */
2334 static enum bfs_result
find_usage_forwards(struct lock_list * root,unsigned long usage_mask,struct lock_list ** target_entry)2335 find_usage_forwards(struct lock_list *root, unsigned long usage_mask,
2336 struct lock_list **target_entry)
2337 {
2338 enum bfs_result result;
2339
2340 debug_atomic_inc(nr_find_usage_forwards_checks);
2341
2342 result = __bfs_forwards(root, &usage_mask, usage_match, usage_skip, target_entry);
2343
2344 return result;
2345 }
2346
2347 /*
2348 * Find a node in the backwards-direction dependency sub-graph starting
2349 * at @root->class that matches @bit.
2350 */
2351 static enum bfs_result
find_usage_backwards(struct lock_list * root,unsigned long usage_mask,struct lock_list ** target_entry)2352 find_usage_backwards(struct lock_list *root, unsigned long usage_mask,
2353 struct lock_list **target_entry)
2354 {
2355 enum bfs_result result;
2356
2357 debug_atomic_inc(nr_find_usage_backwards_checks);
2358
2359 result = __bfs_backwards(root, &usage_mask, usage_match, usage_skip, target_entry);
2360
2361 return result;
2362 }
2363
print_lock_class_header(struct lock_class * class,int depth)2364 static void print_lock_class_header(struct lock_class *class, int depth)
2365 {
2366 int bit;
2367
2368 printk("%*s->", depth, "");
2369 print_lock_name(NULL, class);
2370 #ifdef CONFIG_DEBUG_LOCKDEP
2371 printk(KERN_CONT " ops: %lu", debug_class_ops_read(class));
2372 #endif
2373 printk(KERN_CONT " {\n");
2374
2375 for (bit = 0; bit < LOCK_TRACE_STATES; bit++) {
2376 if (class->usage_mask & (1 << bit)) {
2377 int len = depth;
2378
2379 len += printk("%*s %s", depth, "", usage_str[bit]);
2380 len += printk(KERN_CONT " at:\n");
2381 print_lock_trace(class->usage_traces[bit], len);
2382 }
2383 }
2384 printk("%*s }\n", depth, "");
2385
2386 printk("%*s ... key at: [<%px>] %pS\n",
2387 depth, "", class->key, class->key);
2388 }
2389
2390 /*
2391 * Dependency path printing:
2392 *
2393 * After BFS we get a lock dependency path (linked via ->parent of lock_list),
2394 * printing out each lock in the dependency path will help on understanding how
2395 * the deadlock could happen. Here are some details about dependency path
2396 * printing:
2397 *
2398 * 1) A lock_list can be either forwards or backwards for a lock dependency,
2399 * for a lock dependency A -> B, there are two lock_lists:
2400 *
2401 * a) lock_list in the ->locks_after list of A, whose ->class is B and
2402 * ->links_to is A. In this case, we can say the lock_list is
2403 * "A -> B" (forwards case).
2404 *
2405 * b) lock_list in the ->locks_before list of B, whose ->class is A
2406 * and ->links_to is B. In this case, we can say the lock_list is
2407 * "B <- A" (bacwards case).
2408 *
2409 * The ->trace of both a) and b) point to the call trace where B was
2410 * acquired with A held.
2411 *
2412 * 2) A "helper" lock_list is introduced during BFS, this lock_list doesn't
2413 * represent a certain lock dependency, it only provides an initial entry
2414 * for BFS. For example, BFS may introduce a "helper" lock_list whose
2415 * ->class is A, as a result BFS will search all dependencies starting with
2416 * A, e.g. A -> B or A -> C.
2417 *
2418 * The notation of a forwards helper lock_list is like "-> A", which means
2419 * we should search the forwards dependencies starting with "A", e.g A -> B
2420 * or A -> C.
2421 *
2422 * The notation of a bacwards helper lock_list is like "<- B", which means
2423 * we should search the backwards dependencies ending with "B", e.g.
2424 * B <- A or B <- C.
2425 */
2426
2427 /*
2428 * printk the shortest lock dependencies from @root to @leaf in reverse order.
2429 *
2430 * We have a lock dependency path as follow:
2431 *
2432 * @root @leaf
2433 * | |
2434 * V V
2435 * ->parent ->parent
2436 * | lock_list | <--------- | lock_list | ... | lock_list | <--------- | lock_list |
2437 * | -> L1 | | L1 -> L2 | ... |Ln-2 -> Ln-1| | Ln-1 -> Ln|
2438 *
2439 * , so it's natural that we start from @leaf and print every ->class and
2440 * ->trace until we reach the @root.
2441 */
2442 static void __used
print_shortest_lock_dependencies(struct lock_list * leaf,struct lock_list * root)2443 print_shortest_lock_dependencies(struct lock_list *leaf,
2444 struct lock_list *root)
2445 {
2446 struct lock_list *entry = leaf;
2447 int depth;
2448
2449 /*compute depth from generated tree by BFS*/
2450 depth = get_lock_depth(leaf);
2451
2452 do {
2453 print_lock_class_header(entry->class, depth);
2454 printk("%*s ... acquired at:\n", depth, "");
2455 print_lock_trace(entry->trace, 2);
2456 printk("\n");
2457
2458 if (depth == 0 && (entry != root)) {
2459 printk("lockdep:%s bad path found in chain graph\n", __func__);
2460 break;
2461 }
2462
2463 entry = get_lock_parent(entry);
2464 depth--;
2465 } while (entry && (depth >= 0));
2466 }
2467
2468 /*
2469 * printk the shortest lock dependencies from @leaf to @root.
2470 *
2471 * We have a lock dependency path (from a backwards search) as follow:
2472 *
2473 * @leaf @root
2474 * | |
2475 * V V
2476 * ->parent ->parent
2477 * | lock_list | ---------> | lock_list | ... | lock_list | ---------> | lock_list |
2478 * | L2 <- L1 | | L3 <- L2 | ... | Ln <- Ln-1 | | <- Ln |
2479 *
2480 * , so when we iterate from @leaf to @root, we actually print the lock
2481 * dependency path L1 -> L2 -> .. -> Ln in the non-reverse order.
2482 *
2483 * Another thing to notice here is that ->class of L2 <- L1 is L1, while the
2484 * ->trace of L2 <- L1 is the call trace of L2, in fact we don't have the call
2485 * trace of L1 in the dependency path, which is alright, because most of the
2486 * time we can figure out where L1 is held from the call trace of L2.
2487 */
2488 static void __used
print_shortest_lock_dependencies_backwards(struct lock_list * leaf,struct lock_list * root)2489 print_shortest_lock_dependencies_backwards(struct lock_list *leaf,
2490 struct lock_list *root)
2491 {
2492 struct lock_list *entry = leaf;
2493 const struct lock_trace *trace = NULL;
2494 int depth;
2495
2496 /*compute depth from generated tree by BFS*/
2497 depth = get_lock_depth(leaf);
2498
2499 do {
2500 print_lock_class_header(entry->class, depth);
2501 if (trace) {
2502 printk("%*s ... acquired at:\n", depth, "");
2503 print_lock_trace(trace, 2);
2504 printk("\n");
2505 }
2506
2507 /*
2508 * Record the pointer to the trace for the next lock_list
2509 * entry, see the comments for the function.
2510 */
2511 trace = entry->trace;
2512
2513 if (depth == 0 && (entry != root)) {
2514 printk("lockdep:%s bad path found in chain graph\n", __func__);
2515 break;
2516 }
2517
2518 entry = get_lock_parent(entry);
2519 depth--;
2520 } while (entry && (depth >= 0));
2521 }
2522
2523 static void
print_irq_lock_scenario(struct lock_list * safe_entry,struct lock_list * unsafe_entry,struct lock_class * prev_class,struct lock_class * next_class)2524 print_irq_lock_scenario(struct lock_list *safe_entry,
2525 struct lock_list *unsafe_entry,
2526 struct lock_class *prev_class,
2527 struct lock_class *next_class)
2528 {
2529 struct lock_class *safe_class = safe_entry->class;
2530 struct lock_class *unsafe_class = unsafe_entry->class;
2531 struct lock_class *middle_class = prev_class;
2532
2533 if (middle_class == safe_class)
2534 middle_class = next_class;
2535
2536 /*
2537 * A direct locking problem where unsafe_class lock is taken
2538 * directly by safe_class lock, then all we need to show
2539 * is the deadlock scenario, as it is obvious that the
2540 * unsafe lock is taken under the safe lock.
2541 *
2542 * But if there is a chain instead, where the safe lock takes
2543 * an intermediate lock (middle_class) where this lock is
2544 * not the same as the safe lock, then the lock chain is
2545 * used to describe the problem. Otherwise we would need
2546 * to show a different CPU case for each link in the chain
2547 * from the safe_class lock to the unsafe_class lock.
2548 */
2549 if (middle_class != unsafe_class) {
2550 printk("Chain exists of:\n ");
2551 __print_lock_name(NULL, safe_class);
2552 printk(KERN_CONT " --> ");
2553 __print_lock_name(NULL, middle_class);
2554 printk(KERN_CONT " --> ");
2555 __print_lock_name(NULL, unsafe_class);
2556 printk(KERN_CONT "\n\n");
2557 }
2558
2559 printk(" Possible interrupt unsafe locking scenario:\n\n");
2560 printk(" CPU0 CPU1\n");
2561 printk(" ---- ----\n");
2562 printk(" lock(");
2563 __print_lock_name(NULL, unsafe_class);
2564 printk(KERN_CONT ");\n");
2565 printk(" local_irq_disable();\n");
2566 printk(" lock(");
2567 __print_lock_name(NULL, safe_class);
2568 printk(KERN_CONT ");\n");
2569 printk(" lock(");
2570 __print_lock_name(NULL, middle_class);
2571 printk(KERN_CONT ");\n");
2572 printk(" <Interrupt>\n");
2573 printk(" lock(");
2574 __print_lock_name(NULL, safe_class);
2575 printk(KERN_CONT ");\n");
2576 printk("\n *** DEADLOCK ***\n\n");
2577 }
2578
2579 static void
print_bad_irq_dependency(struct task_struct * curr,struct lock_list * prev_root,struct lock_list * next_root,struct lock_list * backwards_entry,struct lock_list * forwards_entry,struct held_lock * prev,struct held_lock * next,enum lock_usage_bit bit1,enum lock_usage_bit bit2,const char * irqclass)2580 print_bad_irq_dependency(struct task_struct *curr,
2581 struct lock_list *prev_root,
2582 struct lock_list *next_root,
2583 struct lock_list *backwards_entry,
2584 struct lock_list *forwards_entry,
2585 struct held_lock *prev,
2586 struct held_lock *next,
2587 enum lock_usage_bit bit1,
2588 enum lock_usage_bit bit2,
2589 const char *irqclass)
2590 {
2591 if (!debug_locks_off_graph_unlock() || debug_locks_silent)
2592 return;
2593
2594 nbcon_cpu_emergency_enter();
2595
2596 pr_warn("\n");
2597 pr_warn("=====================================================\n");
2598 pr_warn("WARNING: %s-safe -> %s-unsafe lock order detected\n",
2599 irqclass, irqclass);
2600 print_kernel_ident();
2601 pr_warn("-----------------------------------------------------\n");
2602 pr_warn("%s/%d [HC%u[%lu]:SC%u[%lu]:HE%u:SE%u] is trying to acquire:\n",
2603 curr->comm, task_pid_nr(curr),
2604 lockdep_hardirq_context(), hardirq_count() >> HARDIRQ_SHIFT,
2605 curr->softirq_context, softirq_count() >> SOFTIRQ_SHIFT,
2606 lockdep_hardirqs_enabled(),
2607 curr->softirqs_enabled);
2608 print_lock(next);
2609
2610 pr_warn("\nand this task is already holding:\n");
2611 print_lock(prev);
2612 pr_warn("which would create a new lock dependency:\n");
2613 print_lock_name(prev, hlock_class(prev));
2614 pr_cont(" ->");
2615 print_lock_name(next, hlock_class(next));
2616 pr_cont("\n");
2617
2618 pr_warn("\nbut this new dependency connects a %s-irq-safe lock:\n",
2619 irqclass);
2620 print_lock_name(NULL, backwards_entry->class);
2621 pr_warn("\n... which became %s-irq-safe at:\n", irqclass);
2622
2623 print_lock_trace(backwards_entry->class->usage_traces[bit1], 1);
2624
2625 pr_warn("\nto a %s-irq-unsafe lock:\n", irqclass);
2626 print_lock_name(NULL, forwards_entry->class);
2627 pr_warn("\n... which became %s-irq-unsafe at:\n", irqclass);
2628 pr_warn("...");
2629
2630 print_lock_trace(forwards_entry->class->usage_traces[bit2], 1);
2631
2632 pr_warn("\nother info that might help us debug this:\n\n");
2633 print_irq_lock_scenario(backwards_entry, forwards_entry,
2634 hlock_class(prev), hlock_class(next));
2635
2636 lockdep_print_held_locks(curr);
2637
2638 pr_warn("\nthe dependencies between %s-irq-safe lock and the holding lock:\n", irqclass);
2639 print_shortest_lock_dependencies_backwards(backwards_entry, prev_root);
2640
2641 pr_warn("\nthe dependencies between the lock to be acquired");
2642 pr_warn(" and %s-irq-unsafe lock:\n", irqclass);
2643 next_root->trace = save_trace();
2644 if (!next_root->trace)
2645 goto out;
2646 print_shortest_lock_dependencies(forwards_entry, next_root);
2647
2648 pr_warn("\nstack backtrace:\n");
2649 dump_stack();
2650 out:
2651 nbcon_cpu_emergency_exit();
2652 }
2653
2654 static const char *state_names[] = {
2655 #define LOCKDEP_STATE(__STATE) \
2656 __stringify(__STATE),
2657 #include "lockdep_states.h"
2658 #undef LOCKDEP_STATE
2659 };
2660
2661 static const char *state_rnames[] = {
2662 #define LOCKDEP_STATE(__STATE) \
2663 __stringify(__STATE)"-READ",
2664 #include "lockdep_states.h"
2665 #undef LOCKDEP_STATE
2666 };
2667
state_name(enum lock_usage_bit bit)2668 static inline const char *state_name(enum lock_usage_bit bit)
2669 {
2670 if (bit & LOCK_USAGE_READ_MASK)
2671 return state_rnames[bit >> LOCK_USAGE_DIR_MASK];
2672 else
2673 return state_names[bit >> LOCK_USAGE_DIR_MASK];
2674 }
2675
2676 /*
2677 * The bit number is encoded like:
2678 *
2679 * bit0: 0 exclusive, 1 read lock
2680 * bit1: 0 used in irq, 1 irq enabled
2681 * bit2-n: state
2682 */
exclusive_bit(int new_bit)2683 static int exclusive_bit(int new_bit)
2684 {
2685 int state = new_bit & LOCK_USAGE_STATE_MASK;
2686 int dir = new_bit & LOCK_USAGE_DIR_MASK;
2687
2688 /*
2689 * keep state, bit flip the direction and strip read.
2690 */
2691 return state | (dir ^ LOCK_USAGE_DIR_MASK);
2692 }
2693
2694 /*
2695 * Observe that when given a bitmask where each bitnr is encoded as above, a
2696 * right shift of the mask transforms the individual bitnrs as -1 and
2697 * conversely, a left shift transforms into +1 for the individual bitnrs.
2698 *
2699 * So for all bits whose number have LOCK_ENABLED_* set (bitnr1 == 1), we can
2700 * create the mask with those bit numbers using LOCK_USED_IN_* (bitnr1 == 0)
2701 * instead by subtracting the bit number by 2, or shifting the mask right by 2.
2702 *
2703 * Similarly, bitnr1 == 0 becomes bitnr1 == 1 by adding 2, or shifting left 2.
2704 *
2705 * So split the mask (note that LOCKF_ENABLED_IRQ_ALL|LOCKF_USED_IN_IRQ_ALL is
2706 * all bits set) and recompose with bitnr1 flipped.
2707 */
invert_dir_mask(unsigned long mask)2708 static unsigned long invert_dir_mask(unsigned long mask)
2709 {
2710 unsigned long excl = 0;
2711
2712 /* Invert dir */
2713 excl |= (mask & LOCKF_ENABLED_IRQ_ALL) >> LOCK_USAGE_DIR_MASK;
2714 excl |= (mask & LOCKF_USED_IN_IRQ_ALL) << LOCK_USAGE_DIR_MASK;
2715
2716 return excl;
2717 }
2718
2719 /*
2720 * Note that a LOCK_ENABLED_IRQ_*_READ usage and a LOCK_USED_IN_IRQ_*_READ
2721 * usage may cause deadlock too, for example:
2722 *
2723 * P1 P2
2724 * <irq disabled>
2725 * write_lock(l1); <irq enabled>
2726 * read_lock(l2);
2727 * write_lock(l2);
2728 * <in irq>
2729 * read_lock(l1);
2730 *
2731 * , in above case, l1 will be marked as LOCK_USED_IN_IRQ_HARDIRQ_READ and l2
2732 * will marked as LOCK_ENABLE_IRQ_HARDIRQ_READ, and this is a possible
2733 * deadlock.
2734 *
2735 * In fact, all of the following cases may cause deadlocks:
2736 *
2737 * LOCK_USED_IN_IRQ_* -> LOCK_ENABLED_IRQ_*
2738 * LOCK_USED_IN_IRQ_*_READ -> LOCK_ENABLED_IRQ_*
2739 * LOCK_USED_IN_IRQ_* -> LOCK_ENABLED_IRQ_*_READ
2740 * LOCK_USED_IN_IRQ_*_READ -> LOCK_ENABLED_IRQ_*_READ
2741 *
2742 * As a result, to calculate the "exclusive mask", first we invert the
2743 * direction (USED_IN/ENABLED) of the original mask, and 1) for all bits with
2744 * bitnr0 set (LOCK_*_READ), add those with bitnr0 cleared (LOCK_*). 2) for all
2745 * bits with bitnr0 cleared (LOCK_*_READ), add those with bitnr0 set (LOCK_*).
2746 */
exclusive_mask(unsigned long mask)2747 static unsigned long exclusive_mask(unsigned long mask)
2748 {
2749 unsigned long excl = invert_dir_mask(mask);
2750
2751 excl |= (excl & LOCKF_IRQ_READ) >> LOCK_USAGE_READ_MASK;
2752 excl |= (excl & LOCKF_IRQ) << LOCK_USAGE_READ_MASK;
2753
2754 return excl;
2755 }
2756
2757 /*
2758 * Retrieve the _possible_ original mask to which @mask is
2759 * exclusive. Ie: this is the opposite of exclusive_mask().
2760 * Note that 2 possible original bits can match an exclusive
2761 * bit: one has LOCK_USAGE_READ_MASK set, the other has it
2762 * cleared. So both are returned for each exclusive bit.
2763 */
original_mask(unsigned long mask)2764 static unsigned long original_mask(unsigned long mask)
2765 {
2766 unsigned long excl = invert_dir_mask(mask);
2767
2768 /* Include read in existing usages */
2769 excl |= (excl & LOCKF_IRQ_READ) >> LOCK_USAGE_READ_MASK;
2770 excl |= (excl & LOCKF_IRQ) << LOCK_USAGE_READ_MASK;
2771
2772 return excl;
2773 }
2774
2775 /*
2776 * Find the first pair of bit match between an original
2777 * usage mask and an exclusive usage mask.
2778 */
find_exclusive_match(unsigned long mask,unsigned long excl_mask,enum lock_usage_bit * bitp,enum lock_usage_bit * excl_bitp)2779 static int find_exclusive_match(unsigned long mask,
2780 unsigned long excl_mask,
2781 enum lock_usage_bit *bitp,
2782 enum lock_usage_bit *excl_bitp)
2783 {
2784 int bit, excl, excl_read;
2785
2786 for_each_set_bit(bit, &mask, LOCK_USED) {
2787 /*
2788 * exclusive_bit() strips the read bit, however,
2789 * LOCK_ENABLED_IRQ_*_READ may cause deadlocks too, so we need
2790 * to search excl | LOCK_USAGE_READ_MASK as well.
2791 */
2792 excl = exclusive_bit(bit);
2793 excl_read = excl | LOCK_USAGE_READ_MASK;
2794 if (excl_mask & lock_flag(excl)) {
2795 *bitp = bit;
2796 *excl_bitp = excl;
2797 return 0;
2798 } else if (excl_mask & lock_flag(excl_read)) {
2799 *bitp = bit;
2800 *excl_bitp = excl_read;
2801 return 0;
2802 }
2803 }
2804 return -1;
2805 }
2806
2807 /*
2808 * Prove that the new dependency does not connect a hardirq-safe(-read)
2809 * lock with a hardirq-unsafe lock - to achieve this we search
2810 * the backwards-subgraph starting at <prev>, and the
2811 * forwards-subgraph starting at <next>:
2812 */
check_irq_usage(struct task_struct * curr,struct held_lock * prev,struct held_lock * next)2813 static int check_irq_usage(struct task_struct *curr, struct held_lock *prev,
2814 struct held_lock *next)
2815 {
2816 unsigned long usage_mask = 0, forward_mask, backward_mask;
2817 enum lock_usage_bit forward_bit = 0, backward_bit = 0;
2818 struct lock_list *target_entry1;
2819 struct lock_list *target_entry;
2820 struct lock_list this, that;
2821 enum bfs_result ret;
2822
2823 /*
2824 * Step 1: gather all hard/soft IRQs usages backward in an
2825 * accumulated usage mask.
2826 */
2827 bfs_init_rootb(&this, prev);
2828
2829 ret = __bfs_backwards(&this, &usage_mask, usage_accumulate, usage_skip, NULL);
2830 if (bfs_error(ret)) {
2831 print_bfs_bug(ret);
2832 return 0;
2833 }
2834
2835 usage_mask &= LOCKF_USED_IN_IRQ_ALL;
2836 if (!usage_mask)
2837 return 1;
2838
2839 /*
2840 * Step 2: find exclusive uses forward that match the previous
2841 * backward accumulated mask.
2842 */
2843 forward_mask = exclusive_mask(usage_mask);
2844
2845 bfs_init_root(&that, next);
2846
2847 ret = find_usage_forwards(&that, forward_mask, &target_entry1);
2848 if (bfs_error(ret)) {
2849 print_bfs_bug(ret);
2850 return 0;
2851 }
2852 if (ret == BFS_RNOMATCH)
2853 return 1;
2854
2855 /*
2856 * Step 3: we found a bad match! Now retrieve a lock from the backward
2857 * list whose usage mask matches the exclusive usage mask from the
2858 * lock found on the forward list.
2859 *
2860 * Note, we should only keep the LOCKF_ENABLED_IRQ_ALL bits, considering
2861 * the follow case:
2862 *
2863 * When trying to add A -> B to the graph, we find that there is a
2864 * hardirq-safe L, that L -> ... -> A, and another hardirq-unsafe M,
2865 * that B -> ... -> M. However M is **softirq-safe**, if we use exact
2866 * invert bits of M's usage_mask, we will find another lock N that is
2867 * **softirq-unsafe** and N -> ... -> A, however N -> .. -> M will not
2868 * cause a inversion deadlock.
2869 */
2870 backward_mask = original_mask(target_entry1->class->usage_mask & LOCKF_ENABLED_IRQ_ALL);
2871
2872 ret = find_usage_backwards(&this, backward_mask, &target_entry);
2873 if (bfs_error(ret)) {
2874 print_bfs_bug(ret);
2875 return 0;
2876 }
2877 if (DEBUG_LOCKS_WARN_ON(ret == BFS_RNOMATCH))
2878 return 1;
2879
2880 /*
2881 * Step 4: narrow down to a pair of incompatible usage bits
2882 * and report it.
2883 */
2884 ret = find_exclusive_match(target_entry->class->usage_mask,
2885 target_entry1->class->usage_mask,
2886 &backward_bit, &forward_bit);
2887 if (DEBUG_LOCKS_WARN_ON(ret == -1))
2888 return 1;
2889
2890 print_bad_irq_dependency(curr, &this, &that,
2891 target_entry, target_entry1,
2892 prev, next,
2893 backward_bit, forward_bit,
2894 state_name(backward_bit));
2895
2896 return 0;
2897 }
2898
2899 #else
2900
check_irq_usage(struct task_struct * curr,struct held_lock * prev,struct held_lock * next)2901 static inline int check_irq_usage(struct task_struct *curr,
2902 struct held_lock *prev, struct held_lock *next)
2903 {
2904 return 1;
2905 }
2906
usage_skip(struct lock_list * entry,void * mask)2907 static inline bool usage_skip(struct lock_list *entry, void *mask)
2908 {
2909 return false;
2910 }
2911
2912 #endif /* CONFIG_TRACE_IRQFLAGS */
2913
2914 #ifdef CONFIG_LOCKDEP_SMALL
2915 /*
2916 * Check that the dependency graph starting at <src> can lead to
2917 * <target> or not. If it can, <src> -> <target> dependency is already
2918 * in the graph.
2919 *
2920 * Return BFS_RMATCH if it does, or BFS_RNOMATCH if it does not, return BFS_E* if
2921 * any error appears in the bfs search.
2922 */
2923 static noinline enum bfs_result
check_redundant(struct held_lock * src,struct held_lock * target)2924 check_redundant(struct held_lock *src, struct held_lock *target)
2925 {
2926 enum bfs_result ret;
2927 struct lock_list *target_entry;
2928 struct lock_list src_entry;
2929
2930 bfs_init_root(&src_entry, src);
2931 /*
2932 * Special setup for check_redundant().
2933 *
2934 * To report redundant, we need to find a strong dependency path that
2935 * is equal to or stronger than <src> -> <target>. So if <src> is E,
2936 * we need to let __bfs() only search for a path starting at a -(E*)->,
2937 * we achieve this by setting the initial node's ->only_xr to true in
2938 * that case. And if <prev> is S, we set initial ->only_xr to false
2939 * because both -(S*)-> (equal) and -(E*)-> (stronger) are redundant.
2940 */
2941 src_entry.only_xr = src->read == 0;
2942
2943 debug_atomic_inc(nr_redundant_checks);
2944
2945 /*
2946 * Note: we skip local_lock() for redundant check, because as the
2947 * comment in usage_skip(), A -> local_lock() -> B and A -> B are not
2948 * the same.
2949 */
2950 ret = check_path(target, &src_entry, hlock_equal, usage_skip, &target_entry);
2951
2952 if (ret == BFS_RMATCH)
2953 debug_atomic_inc(nr_redundant);
2954
2955 return ret;
2956 }
2957
2958 #else
2959
2960 static inline enum bfs_result
check_redundant(struct held_lock * src,struct held_lock * target)2961 check_redundant(struct held_lock *src, struct held_lock *target)
2962 {
2963 return BFS_RNOMATCH;
2964 }
2965
2966 #endif
2967
inc_chains(int irq_context)2968 static void inc_chains(int irq_context)
2969 {
2970 if (irq_context & LOCK_CHAIN_HARDIRQ_CONTEXT)
2971 nr_hardirq_chains++;
2972 else if (irq_context & LOCK_CHAIN_SOFTIRQ_CONTEXT)
2973 nr_softirq_chains++;
2974 else
2975 nr_process_chains++;
2976 }
2977
dec_chains(int irq_context)2978 static void dec_chains(int irq_context)
2979 {
2980 if (irq_context & LOCK_CHAIN_HARDIRQ_CONTEXT)
2981 nr_hardirq_chains--;
2982 else if (irq_context & LOCK_CHAIN_SOFTIRQ_CONTEXT)
2983 nr_softirq_chains--;
2984 else
2985 nr_process_chains--;
2986 }
2987
2988 static void
print_deadlock_scenario(struct held_lock * nxt,struct held_lock * prv)2989 print_deadlock_scenario(struct held_lock *nxt, struct held_lock *prv)
2990 {
2991 struct lock_class *next = hlock_class(nxt);
2992 struct lock_class *prev = hlock_class(prv);
2993
2994 printk(" Possible unsafe locking scenario:\n\n");
2995 printk(" CPU0\n");
2996 printk(" ----\n");
2997 printk(" lock(");
2998 __print_lock_name(prv, prev);
2999 printk(KERN_CONT ");\n");
3000 printk(" lock(");
3001 __print_lock_name(nxt, next);
3002 printk(KERN_CONT ");\n");
3003 printk("\n *** DEADLOCK ***\n\n");
3004 printk(" May be due to missing lock nesting notation\n\n");
3005 }
3006
3007 static void
print_deadlock_bug(struct task_struct * curr,struct held_lock * prev,struct held_lock * next)3008 print_deadlock_bug(struct task_struct *curr, struct held_lock *prev,
3009 struct held_lock *next)
3010 {
3011 struct lock_class *class = hlock_class(prev);
3012
3013 if (!debug_locks_off_graph_unlock() || debug_locks_silent)
3014 return;
3015
3016 nbcon_cpu_emergency_enter();
3017
3018 pr_warn("\n");
3019 pr_warn("============================================\n");
3020 pr_warn("WARNING: possible recursive locking detected\n");
3021 print_kernel_ident();
3022 pr_warn("--------------------------------------------\n");
3023 pr_warn("%s/%d is trying to acquire lock:\n",
3024 curr->comm, task_pid_nr(curr));
3025 print_lock(next);
3026 pr_warn("\nbut task is already holding lock:\n");
3027 print_lock(prev);
3028
3029 if (class->cmp_fn) {
3030 pr_warn("and the lock comparison function returns %i:\n",
3031 class->cmp_fn(prev->instance, next->instance));
3032 }
3033
3034 pr_warn("\nother info that might help us debug this:\n");
3035 print_deadlock_scenario(next, prev);
3036 lockdep_print_held_locks(curr);
3037
3038 pr_warn("\nstack backtrace:\n");
3039 dump_stack();
3040
3041 nbcon_cpu_emergency_exit();
3042 }
3043
3044 /*
3045 * Check whether we are holding such a class already.
3046 *
3047 * (Note that this has to be done separately, because the graph cannot
3048 * detect such classes of deadlocks.)
3049 *
3050 * Returns: 0 on deadlock detected, 1 on OK, 2 if another lock with the same
3051 * lock class is held but nest_lock is also held, i.e. we rely on the
3052 * nest_lock to avoid the deadlock.
3053 */
3054 static int
check_deadlock(struct task_struct * curr,struct held_lock * next)3055 check_deadlock(struct task_struct *curr, struct held_lock *next)
3056 {
3057 struct lock_class *class;
3058 struct held_lock *prev;
3059 struct held_lock *nest = NULL;
3060 int i;
3061
3062 for (i = 0; i < curr->lockdep_depth; i++) {
3063 prev = curr->held_locks + i;
3064
3065 if (prev->instance == next->nest_lock)
3066 nest = prev;
3067
3068 if (hlock_class(prev) != hlock_class(next))
3069 continue;
3070
3071 /*
3072 * Allow read-after-read recursion of the same
3073 * lock class (i.e. read_lock(lock)+read_lock(lock)):
3074 */
3075 if ((next->read == 2) && prev->read)
3076 continue;
3077
3078 class = hlock_class(prev);
3079
3080 if (class->cmp_fn &&
3081 class->cmp_fn(prev->instance, next->instance) < 0)
3082 continue;
3083
3084 /*
3085 * We're holding the nest_lock, which serializes this lock's
3086 * nesting behaviour.
3087 */
3088 if (nest)
3089 return 2;
3090
3091 print_deadlock_bug(curr, prev, next);
3092 return 0;
3093 }
3094 return 1;
3095 }
3096
3097 /*
3098 * There was a chain-cache miss, and we are about to add a new dependency
3099 * to a previous lock. We validate the following rules:
3100 *
3101 * - would the adding of the <prev> -> <next> dependency create a
3102 * circular dependency in the graph? [== circular deadlock]
3103 *
3104 * - does the new prev->next dependency connect any hardirq-safe lock
3105 * (in the full backwards-subgraph starting at <prev>) with any
3106 * hardirq-unsafe lock (in the full forwards-subgraph starting at
3107 * <next>)? [== illegal lock inversion with hardirq contexts]
3108 *
3109 * - does the new prev->next dependency connect any softirq-safe lock
3110 * (in the full backwards-subgraph starting at <prev>) with any
3111 * softirq-unsafe lock (in the full forwards-subgraph starting at
3112 * <next>)? [== illegal lock inversion with softirq contexts]
3113 *
3114 * any of these scenarios could lead to a deadlock.
3115 *
3116 * Then if all the validations pass, we add the forwards and backwards
3117 * dependency.
3118 */
3119 static int
check_prev_add(struct task_struct * curr,struct held_lock * prev,struct held_lock * next,u16 distance,struct lock_trace ** const trace)3120 check_prev_add(struct task_struct *curr, struct held_lock *prev,
3121 struct held_lock *next, u16 distance,
3122 struct lock_trace **const trace)
3123 {
3124 struct lock_list *entry;
3125 enum bfs_result ret;
3126
3127 if (!hlock_class(prev)->key || !hlock_class(next)->key) {
3128 /*
3129 * The warning statements below may trigger a use-after-free
3130 * of the class name. It is better to trigger a use-after free
3131 * and to have the class name most of the time instead of not
3132 * having the class name available.
3133 */
3134 WARN_ONCE(!debug_locks_silent && !hlock_class(prev)->key,
3135 "Detected use-after-free of lock class %px/%s\n",
3136 hlock_class(prev),
3137 hlock_class(prev)->name);
3138 WARN_ONCE(!debug_locks_silent && !hlock_class(next)->key,
3139 "Detected use-after-free of lock class %px/%s\n",
3140 hlock_class(next),
3141 hlock_class(next)->name);
3142 return 2;
3143 }
3144
3145 if (prev->class_idx == next->class_idx) {
3146 struct lock_class *class = hlock_class(prev);
3147
3148 if (class->cmp_fn &&
3149 class->cmp_fn(prev->instance, next->instance) < 0)
3150 return 2;
3151 }
3152
3153 /*
3154 * Prove that the new <prev> -> <next> dependency would not
3155 * create a circular dependency in the graph. (We do this by
3156 * a breadth-first search into the graph starting at <next>,
3157 * and check whether we can reach <prev>.)
3158 *
3159 * The search is limited by the size of the circular queue (i.e.,
3160 * MAX_CIRCULAR_QUEUE_SIZE) which keeps track of a breadth of nodes
3161 * in the graph whose neighbours are to be checked.
3162 */
3163 ret = check_noncircular(next, prev, trace);
3164 if (unlikely(bfs_error(ret) || ret == BFS_RMATCH))
3165 return 0;
3166
3167 if (!check_irq_usage(curr, prev, next))
3168 return 0;
3169
3170 /*
3171 * Is the <prev> -> <next> dependency already present?
3172 *
3173 * (this may occur even though this is a new chain: consider
3174 * e.g. the L1 -> L2 -> L3 -> L4 and the L5 -> L1 -> L2 -> L3
3175 * chains - the second one will be new, but L1 already has
3176 * L2 added to its dependency list, due to the first chain.)
3177 */
3178 list_for_each_entry(entry, &hlock_class(prev)->locks_after, entry) {
3179 if (entry->class == hlock_class(next)) {
3180 if (distance == 1)
3181 entry->distance = 1;
3182 entry->dep |= calc_dep(prev, next);
3183
3184 /*
3185 * Also, update the reverse dependency in @next's
3186 * ->locks_before list.
3187 *
3188 * Here we reuse @entry as the cursor, which is fine
3189 * because we won't go to the next iteration of the
3190 * outer loop:
3191 *
3192 * For normal cases, we return in the inner loop.
3193 *
3194 * If we fail to return, we have inconsistency, i.e.
3195 * <prev>::locks_after contains <next> while
3196 * <next>::locks_before doesn't contain <prev>. In
3197 * that case, we return after the inner and indicate
3198 * something is wrong.
3199 */
3200 list_for_each_entry(entry, &hlock_class(next)->locks_before, entry) {
3201 if (entry->class == hlock_class(prev)) {
3202 if (distance == 1)
3203 entry->distance = 1;
3204 entry->dep |= calc_depb(prev, next);
3205 return 1;
3206 }
3207 }
3208
3209 /* <prev> is not found in <next>::locks_before */
3210 return 0;
3211 }
3212 }
3213
3214 /*
3215 * Is the <prev> -> <next> link redundant?
3216 */
3217 ret = check_redundant(prev, next);
3218 if (bfs_error(ret))
3219 return 0;
3220 else if (ret == BFS_RMATCH)
3221 return 2;
3222
3223 if (!*trace) {
3224 *trace = save_trace();
3225 if (!*trace)
3226 return 0;
3227 }
3228
3229 /*
3230 * Ok, all validations passed, add the new lock
3231 * to the previous lock's dependency list:
3232 */
3233 ret = add_lock_to_list(hlock_class(next), hlock_class(prev),
3234 &hlock_class(prev)->locks_after, distance,
3235 calc_dep(prev, next), *trace);
3236
3237 if (!ret)
3238 return 0;
3239
3240 ret = add_lock_to_list(hlock_class(prev), hlock_class(next),
3241 &hlock_class(next)->locks_before, distance,
3242 calc_depb(prev, next), *trace);
3243 if (!ret)
3244 return 0;
3245
3246 return 2;
3247 }
3248
3249 /*
3250 * Add the dependency to all directly-previous locks that are 'relevant'.
3251 * The ones that are relevant are (in increasing distance from curr):
3252 * all consecutive trylock entries and the final non-trylock entry - or
3253 * the end of this context's lock-chain - whichever comes first.
3254 */
3255 static int
check_prevs_add(struct task_struct * curr,struct held_lock * next)3256 check_prevs_add(struct task_struct *curr, struct held_lock *next)
3257 {
3258 struct lock_trace *trace = NULL;
3259 int depth = curr->lockdep_depth;
3260 struct held_lock *hlock;
3261
3262 /*
3263 * Debugging checks.
3264 *
3265 * Depth must not be zero for a non-head lock:
3266 */
3267 if (!depth)
3268 goto out_bug;
3269 /*
3270 * At least two relevant locks must exist for this
3271 * to be a head:
3272 */
3273 if (curr->held_locks[depth].irq_context !=
3274 curr->held_locks[depth-1].irq_context)
3275 goto out_bug;
3276
3277 for (;;) {
3278 u16 distance = curr->lockdep_depth - depth + 1;
3279 hlock = curr->held_locks + depth - 1;
3280
3281 if (hlock->check) {
3282 int ret = check_prev_add(curr, hlock, next, distance, &trace);
3283 if (!ret)
3284 return 0;
3285
3286 /*
3287 * Stop after the first non-trylock entry,
3288 * as non-trylock entries have added their
3289 * own direct dependencies already, so this
3290 * lock is connected to them indirectly:
3291 */
3292 if (!hlock->trylock)
3293 break;
3294 }
3295
3296 depth--;
3297 /*
3298 * End of lock-stack?
3299 */
3300 if (!depth)
3301 break;
3302 /*
3303 * Stop the search if we cross into another context:
3304 */
3305 if (curr->held_locks[depth].irq_context !=
3306 curr->held_locks[depth-1].irq_context)
3307 break;
3308 }
3309 return 1;
3310 out_bug:
3311 if (!debug_locks_off_graph_unlock())
3312 return 0;
3313
3314 /*
3315 * Clearly we all shouldn't be here, but since we made it we
3316 * can reliable say we messed up our state. See the above two
3317 * gotos for reasons why we could possibly end up here.
3318 */
3319 WARN_ON(1);
3320
3321 return 0;
3322 }
3323
3324 struct lock_chain lock_chains[MAX_LOCKDEP_CHAINS];
3325 static DECLARE_BITMAP(lock_chains_in_use, MAX_LOCKDEP_CHAINS);
3326 static u16 chain_hlocks[MAX_LOCKDEP_CHAIN_HLOCKS];
3327 unsigned long nr_zapped_lock_chains;
3328 unsigned int nr_free_chain_hlocks; /* Free chain_hlocks in buckets */
3329 unsigned int nr_lost_chain_hlocks; /* Lost chain_hlocks */
3330 unsigned int nr_large_chain_blocks; /* size > MAX_CHAIN_BUCKETS */
3331
3332 /*
3333 * The first 2 chain_hlocks entries in the chain block in the bucket
3334 * list contains the following meta data:
3335 *
3336 * entry[0]:
3337 * Bit 15 - always set to 1 (it is not a class index)
3338 * Bits 0-14 - upper 15 bits of the next block index
3339 * entry[1] - lower 16 bits of next block index
3340 *
3341 * A next block index of all 1 bits means it is the end of the list.
3342 *
3343 * On the unsized bucket (bucket-0), the 3rd and 4th entries contain
3344 * the chain block size:
3345 *
3346 * entry[2] - upper 16 bits of the chain block size
3347 * entry[3] - lower 16 bits of the chain block size
3348 */
3349 #define MAX_CHAIN_BUCKETS 16
3350 #define CHAIN_BLK_FLAG (1U << 15)
3351 #define CHAIN_BLK_LIST_END 0xFFFFU
3352
3353 static int chain_block_buckets[MAX_CHAIN_BUCKETS];
3354
size_to_bucket(int size)3355 static inline int size_to_bucket(int size)
3356 {
3357 if (size > MAX_CHAIN_BUCKETS)
3358 return 0;
3359
3360 return size - 1;
3361 }
3362
3363 /*
3364 * Iterate all the chain blocks in a bucket.
3365 */
3366 #define for_each_chain_block(bucket, prev, curr) \
3367 for ((prev) = -1, (curr) = chain_block_buckets[bucket]; \
3368 (curr) >= 0; \
3369 (prev) = (curr), (curr) = chain_block_next(curr))
3370
3371 /*
3372 * next block or -1
3373 */
chain_block_next(int offset)3374 static inline int chain_block_next(int offset)
3375 {
3376 int next = chain_hlocks[offset];
3377
3378 WARN_ON_ONCE(!(next & CHAIN_BLK_FLAG));
3379
3380 if (next == CHAIN_BLK_LIST_END)
3381 return -1;
3382
3383 next &= ~CHAIN_BLK_FLAG;
3384 next <<= 16;
3385 next |= chain_hlocks[offset + 1];
3386
3387 return next;
3388 }
3389
3390 /*
3391 * bucket-0 only
3392 */
chain_block_size(int offset)3393 static inline int chain_block_size(int offset)
3394 {
3395 return (chain_hlocks[offset + 2] << 16) | chain_hlocks[offset + 3];
3396 }
3397
init_chain_block(int offset,int next,int bucket,int size)3398 static inline void init_chain_block(int offset, int next, int bucket, int size)
3399 {
3400 chain_hlocks[offset] = (next >> 16) | CHAIN_BLK_FLAG;
3401 chain_hlocks[offset + 1] = (u16)next;
3402
3403 if (size && !bucket) {
3404 chain_hlocks[offset + 2] = size >> 16;
3405 chain_hlocks[offset + 3] = (u16)size;
3406 }
3407 }
3408
add_chain_block(int offset,int size)3409 static inline void add_chain_block(int offset, int size)
3410 {
3411 int bucket = size_to_bucket(size);
3412 int next = chain_block_buckets[bucket];
3413 int prev, curr;
3414
3415 if (unlikely(size < 2)) {
3416 /*
3417 * We can't store single entries on the freelist. Leak them.
3418 *
3419 * One possible way out would be to uniquely mark them, other
3420 * than with CHAIN_BLK_FLAG, such that we can recover them when
3421 * the block before it is re-added.
3422 */
3423 if (size)
3424 nr_lost_chain_hlocks++;
3425 return;
3426 }
3427
3428 nr_free_chain_hlocks += size;
3429 if (!bucket) {
3430 nr_large_chain_blocks++;
3431
3432 /*
3433 * Variable sized, sort large to small.
3434 */
3435 for_each_chain_block(0, prev, curr) {
3436 if (size >= chain_block_size(curr))
3437 break;
3438 }
3439 init_chain_block(offset, curr, 0, size);
3440 if (prev < 0)
3441 chain_block_buckets[0] = offset;
3442 else
3443 init_chain_block(prev, offset, 0, 0);
3444 return;
3445 }
3446 /*
3447 * Fixed size, add to head.
3448 */
3449 init_chain_block(offset, next, bucket, size);
3450 chain_block_buckets[bucket] = offset;
3451 }
3452
3453 /*
3454 * Only the first block in the list can be deleted.
3455 *
3456 * For the variable size bucket[0], the first block (the largest one) is
3457 * returned, broken up and put back into the pool. So if a chain block of
3458 * length > MAX_CHAIN_BUCKETS is ever used and zapped, it will just be
3459 * queued up after the primordial chain block and never be used until the
3460 * hlock entries in the primordial chain block is almost used up. That
3461 * causes fragmentation and reduce allocation efficiency. That can be
3462 * monitored by looking at the "large chain blocks" number in lockdep_stats.
3463 */
del_chain_block(int bucket,int size,int next)3464 static inline void del_chain_block(int bucket, int size, int next)
3465 {
3466 nr_free_chain_hlocks -= size;
3467 chain_block_buckets[bucket] = next;
3468
3469 if (!bucket)
3470 nr_large_chain_blocks--;
3471 }
3472
init_chain_block_buckets(void)3473 static void init_chain_block_buckets(void)
3474 {
3475 int i;
3476
3477 for (i = 0; i < MAX_CHAIN_BUCKETS; i++)
3478 chain_block_buckets[i] = -1;
3479
3480 add_chain_block(0, ARRAY_SIZE(chain_hlocks));
3481 }
3482
3483 /*
3484 * Return offset of a chain block of the right size or -1 if not found.
3485 *
3486 * Fairly simple worst-fit allocator with the addition of a number of size
3487 * specific free lists.
3488 */
alloc_chain_hlocks(int req)3489 static int alloc_chain_hlocks(int req)
3490 {
3491 int bucket, curr, size;
3492
3493 /*
3494 * We rely on the MSB to act as an escape bit to denote freelist
3495 * pointers. Make sure this bit isn't set in 'normal' class_idx usage.
3496 */
3497 BUILD_BUG_ON((MAX_LOCKDEP_KEYS-1) & CHAIN_BLK_FLAG);
3498
3499 init_data_structures_once();
3500
3501 if (nr_free_chain_hlocks < req)
3502 return -1;
3503
3504 /*
3505 * We require a minimum of 2 (u16) entries to encode a freelist
3506 * 'pointer'.
3507 */
3508 req = max(req, 2);
3509 bucket = size_to_bucket(req);
3510 curr = chain_block_buckets[bucket];
3511
3512 if (bucket) {
3513 if (curr >= 0) {
3514 del_chain_block(bucket, req, chain_block_next(curr));
3515 return curr;
3516 }
3517 /* Try bucket 0 */
3518 curr = chain_block_buckets[0];
3519 }
3520
3521 /*
3522 * The variable sized freelist is sorted by size; the first entry is
3523 * the largest. Use it if it fits.
3524 */
3525 if (curr >= 0) {
3526 size = chain_block_size(curr);
3527 if (likely(size >= req)) {
3528 del_chain_block(0, size, chain_block_next(curr));
3529 if (size > req)
3530 add_chain_block(curr + req, size - req);
3531 return curr;
3532 }
3533 }
3534
3535 /*
3536 * Last resort, split a block in a larger sized bucket.
3537 */
3538 for (size = MAX_CHAIN_BUCKETS; size > req; size--) {
3539 bucket = size_to_bucket(size);
3540 curr = chain_block_buckets[bucket];
3541 if (curr < 0)
3542 continue;
3543
3544 del_chain_block(bucket, size, chain_block_next(curr));
3545 add_chain_block(curr + req, size - req);
3546 return curr;
3547 }
3548
3549 return -1;
3550 }
3551
free_chain_hlocks(int base,int size)3552 static inline void free_chain_hlocks(int base, int size)
3553 {
3554 add_chain_block(base, max(size, 2));
3555 }
3556
lock_chain_get_class(struct lock_chain * chain,int i)3557 struct lock_class *lock_chain_get_class(struct lock_chain *chain, int i)
3558 {
3559 u16 chain_hlock = chain_hlocks[chain->base + i];
3560 unsigned int class_idx = chain_hlock_class_idx(chain_hlock);
3561
3562 return lock_classes + class_idx;
3563 }
3564
3565 /*
3566 * Returns the index of the first held_lock of the current chain
3567 */
get_first_held_lock(struct task_struct * curr,struct held_lock * hlock)3568 static inline int get_first_held_lock(struct task_struct *curr,
3569 struct held_lock *hlock)
3570 {
3571 int i;
3572 struct held_lock *hlock_curr;
3573
3574 for (i = curr->lockdep_depth - 1; i >= 0; i--) {
3575 hlock_curr = curr->held_locks + i;
3576 if (hlock_curr->irq_context != hlock->irq_context)
3577 break;
3578
3579 }
3580
3581 return ++i;
3582 }
3583
3584 #ifdef CONFIG_DEBUG_LOCKDEP
3585 /*
3586 * Returns the next chain_key iteration
3587 */
print_chain_key_iteration(u16 hlock_id,u64 chain_key)3588 static u64 print_chain_key_iteration(u16 hlock_id, u64 chain_key)
3589 {
3590 u64 new_chain_key = iterate_chain_key(chain_key, hlock_id);
3591
3592 printk(" hlock_id:%d -> chain_key:%016Lx",
3593 (unsigned int)hlock_id,
3594 (unsigned long long)new_chain_key);
3595 return new_chain_key;
3596 }
3597
3598 static void
print_chain_keys_held_locks(struct task_struct * curr,struct held_lock * hlock_next)3599 print_chain_keys_held_locks(struct task_struct *curr, struct held_lock *hlock_next)
3600 {
3601 struct held_lock *hlock;
3602 u64 chain_key = INITIAL_CHAIN_KEY;
3603 int depth = curr->lockdep_depth;
3604 int i = get_first_held_lock(curr, hlock_next);
3605
3606 printk("depth: %u (irq_context %u)\n", depth - i + 1,
3607 hlock_next->irq_context);
3608 for (; i < depth; i++) {
3609 hlock = curr->held_locks + i;
3610 chain_key = print_chain_key_iteration(hlock_id(hlock), chain_key);
3611
3612 print_lock(hlock);
3613 }
3614
3615 print_chain_key_iteration(hlock_id(hlock_next), chain_key);
3616 print_lock(hlock_next);
3617 }
3618
print_chain_keys_chain(struct lock_chain * chain)3619 static void print_chain_keys_chain(struct lock_chain *chain)
3620 {
3621 int i;
3622 u64 chain_key = INITIAL_CHAIN_KEY;
3623 u16 hlock_id;
3624
3625 printk("depth: %u\n", chain->depth);
3626 for (i = 0; i < chain->depth; i++) {
3627 hlock_id = chain_hlocks[chain->base + i];
3628 chain_key = print_chain_key_iteration(hlock_id, chain_key);
3629
3630 print_lock_name(NULL, lock_classes + chain_hlock_class_idx(hlock_id));
3631 printk("\n");
3632 }
3633 }
3634
print_collision(struct task_struct * curr,struct held_lock * hlock_next,struct lock_chain * chain)3635 static void print_collision(struct task_struct *curr,
3636 struct held_lock *hlock_next,
3637 struct lock_chain *chain)
3638 {
3639 nbcon_cpu_emergency_enter();
3640
3641 pr_warn("\n");
3642 pr_warn("============================\n");
3643 pr_warn("WARNING: chain_key collision\n");
3644 print_kernel_ident();
3645 pr_warn("----------------------------\n");
3646 pr_warn("%s/%d: ", current->comm, task_pid_nr(current));
3647 pr_warn("Hash chain already cached but the contents don't match!\n");
3648
3649 pr_warn("Held locks:");
3650 print_chain_keys_held_locks(curr, hlock_next);
3651
3652 pr_warn("Locks in cached chain:");
3653 print_chain_keys_chain(chain);
3654
3655 pr_warn("\nstack backtrace:\n");
3656 dump_stack();
3657
3658 nbcon_cpu_emergency_exit();
3659 }
3660 #endif
3661
3662 /*
3663 * Checks whether the chain and the current held locks are consistent
3664 * in depth and also in content. If they are not it most likely means
3665 * that there was a collision during the calculation of the chain_key.
3666 * Returns: 0 not passed, 1 passed
3667 */
check_no_collision(struct task_struct * curr,struct held_lock * hlock,struct lock_chain * chain)3668 static int check_no_collision(struct task_struct *curr,
3669 struct held_lock *hlock,
3670 struct lock_chain *chain)
3671 {
3672 #ifdef CONFIG_DEBUG_LOCKDEP
3673 int i, j, id;
3674
3675 i = get_first_held_lock(curr, hlock);
3676
3677 if (DEBUG_LOCKS_WARN_ON(chain->depth != curr->lockdep_depth - (i - 1))) {
3678 print_collision(curr, hlock, chain);
3679 return 0;
3680 }
3681
3682 for (j = 0; j < chain->depth - 1; j++, i++) {
3683 id = hlock_id(&curr->held_locks[i]);
3684
3685 if (DEBUG_LOCKS_WARN_ON(chain_hlocks[chain->base + j] != id)) {
3686 print_collision(curr, hlock, chain);
3687 return 0;
3688 }
3689 }
3690 #endif
3691 return 1;
3692 }
3693
3694 /*
3695 * Given an index that is >= -1, return the index of the next lock chain.
3696 * Return -2 if there is no next lock chain.
3697 */
lockdep_next_lockchain(long i)3698 long lockdep_next_lockchain(long i)
3699 {
3700 i = find_next_bit(lock_chains_in_use, ARRAY_SIZE(lock_chains), i + 1);
3701 return i < ARRAY_SIZE(lock_chains) ? i : -2;
3702 }
3703
lock_chain_count(void)3704 unsigned long lock_chain_count(void)
3705 {
3706 return bitmap_weight(lock_chains_in_use, ARRAY_SIZE(lock_chains));
3707 }
3708
3709 /* Must be called with the graph lock held. */
alloc_lock_chain(void)3710 static struct lock_chain *alloc_lock_chain(void)
3711 {
3712 int idx = find_first_zero_bit(lock_chains_in_use,
3713 ARRAY_SIZE(lock_chains));
3714
3715 if (unlikely(idx >= ARRAY_SIZE(lock_chains)))
3716 return NULL;
3717 __set_bit(idx, lock_chains_in_use);
3718 return lock_chains + idx;
3719 }
3720
3721 /*
3722 * Adds a dependency chain into chain hashtable. And must be called with
3723 * graph_lock held.
3724 *
3725 * Return 0 if fail, and graph_lock is released.
3726 * Return 1 if succeed, with graph_lock held.
3727 */
add_chain_cache(struct task_struct * curr,struct held_lock * hlock,u64 chain_key)3728 static inline int add_chain_cache(struct task_struct *curr,
3729 struct held_lock *hlock,
3730 u64 chain_key)
3731 {
3732 struct hlist_head *hash_head = chainhashentry(chain_key);
3733 struct lock_chain *chain;
3734 int i, j;
3735
3736 /*
3737 * The caller must hold the graph lock, ensure we've got IRQs
3738 * disabled to make this an IRQ-safe lock.. for recursion reasons
3739 * lockdep won't complain about its own locking errors.
3740 */
3741 if (lockdep_assert_locked())
3742 return 0;
3743
3744 chain = alloc_lock_chain();
3745 if (!chain) {
3746 if (!debug_locks_off_graph_unlock())
3747 return 0;
3748
3749 nbcon_cpu_emergency_enter();
3750 print_lockdep_off("BUG: MAX_LOCKDEP_CHAINS too low!");
3751 dump_stack();
3752 nbcon_cpu_emergency_exit();
3753 return 0;
3754 }
3755 chain->chain_key = chain_key;
3756 chain->irq_context = hlock->irq_context;
3757 i = get_first_held_lock(curr, hlock);
3758 chain->depth = curr->lockdep_depth + 1 - i;
3759
3760 BUILD_BUG_ON((1UL << 24) <= ARRAY_SIZE(chain_hlocks));
3761 BUILD_BUG_ON((1UL << 6) <= ARRAY_SIZE(curr->held_locks));
3762 BUILD_BUG_ON((1UL << 8*sizeof(chain_hlocks[0])) <= ARRAY_SIZE(lock_classes));
3763
3764 j = alloc_chain_hlocks(chain->depth);
3765 if (j < 0) {
3766 if (!debug_locks_off_graph_unlock())
3767 return 0;
3768
3769 nbcon_cpu_emergency_enter();
3770 print_lockdep_off("BUG: MAX_LOCKDEP_CHAIN_HLOCKS too low!");
3771 dump_stack();
3772 nbcon_cpu_emergency_exit();
3773 return 0;
3774 }
3775
3776 chain->base = j;
3777 for (j = 0; j < chain->depth - 1; j++, i++) {
3778 int lock_id = hlock_id(curr->held_locks + i);
3779
3780 chain_hlocks[chain->base + j] = lock_id;
3781 }
3782 chain_hlocks[chain->base + j] = hlock_id(hlock);
3783 hlist_add_head_rcu(&chain->entry, hash_head);
3784 debug_atomic_inc(chain_lookup_misses);
3785 inc_chains(chain->irq_context);
3786
3787 return 1;
3788 }
3789
3790 /*
3791 * Look up a dependency chain. Must be called with either the graph lock or
3792 * the RCU read lock held.
3793 */
lookup_chain_cache(u64 chain_key)3794 static inline struct lock_chain *lookup_chain_cache(u64 chain_key)
3795 {
3796 struct hlist_head *hash_head = chainhashentry(chain_key);
3797 struct lock_chain *chain;
3798
3799 hlist_for_each_entry_rcu(chain, hash_head, entry) {
3800 if (READ_ONCE(chain->chain_key) == chain_key) {
3801 debug_atomic_inc(chain_lookup_hits);
3802 return chain;
3803 }
3804 }
3805 return NULL;
3806 }
3807
3808 /*
3809 * If the key is not present yet in dependency chain cache then
3810 * add it and return 1 - in this case the new dependency chain is
3811 * validated. If the key is already hashed, return 0.
3812 * (On return with 1 graph_lock is held.)
3813 */
lookup_chain_cache_add(struct task_struct * curr,struct held_lock * hlock,u64 chain_key)3814 static inline int lookup_chain_cache_add(struct task_struct *curr,
3815 struct held_lock *hlock,
3816 u64 chain_key)
3817 {
3818 struct lock_class *class = hlock_class(hlock);
3819 struct lock_chain *chain = lookup_chain_cache(chain_key);
3820
3821 if (chain) {
3822 cache_hit:
3823 if (!check_no_collision(curr, hlock, chain))
3824 return 0;
3825
3826 if (very_verbose(class)) {
3827 printk("\nhash chain already cached, key: "
3828 "%016Lx tail class: [%px] %s\n",
3829 (unsigned long long)chain_key,
3830 class->key, class->name);
3831 }
3832
3833 return 0;
3834 }
3835
3836 if (very_verbose(class)) {
3837 printk("\nnew hash chain, key: %016Lx tail class: [%px] %s\n",
3838 (unsigned long long)chain_key, class->key, class->name);
3839 }
3840
3841 if (!graph_lock())
3842 return 0;
3843
3844 /*
3845 * We have to walk the chain again locked - to avoid duplicates:
3846 */
3847 chain = lookup_chain_cache(chain_key);
3848 if (chain) {
3849 graph_unlock();
3850 goto cache_hit;
3851 }
3852
3853 if (!add_chain_cache(curr, hlock, chain_key))
3854 return 0;
3855
3856 return 1;
3857 }
3858
validate_chain(struct task_struct * curr,struct held_lock * hlock,int chain_head,u64 chain_key)3859 static int validate_chain(struct task_struct *curr,
3860 struct held_lock *hlock,
3861 int chain_head, u64 chain_key)
3862 {
3863 /*
3864 * Trylock needs to maintain the stack of held locks, but it
3865 * does not add new dependencies, because trylock can be done
3866 * in any order.
3867 *
3868 * We look up the chain_key and do the O(N^2) check and update of
3869 * the dependencies only if this is a new dependency chain.
3870 * (If lookup_chain_cache_add() return with 1 it acquires
3871 * graph_lock for us)
3872 */
3873 if (!hlock->trylock && hlock->check &&
3874 lookup_chain_cache_add(curr, hlock, chain_key)) {
3875 /*
3876 * Check whether last held lock:
3877 *
3878 * - is irq-safe, if this lock is irq-unsafe
3879 * - is softirq-safe, if this lock is hardirq-unsafe
3880 *
3881 * And check whether the new lock's dependency graph
3882 * could lead back to the previous lock:
3883 *
3884 * - within the current held-lock stack
3885 * - across our accumulated lock dependency records
3886 *
3887 * any of these scenarios could lead to a deadlock.
3888 */
3889 /*
3890 * The simple case: does the current hold the same lock
3891 * already?
3892 */
3893 int ret = check_deadlock(curr, hlock);
3894
3895 if (!ret)
3896 return 0;
3897 /*
3898 * Add dependency only if this lock is not the head
3899 * of the chain, and if the new lock introduces no more
3900 * lock dependency (because we already hold a lock with the
3901 * same lock class) nor deadlock (because the nest_lock
3902 * serializes nesting locks), see the comments for
3903 * check_deadlock().
3904 */
3905 if (!chain_head && ret != 2) {
3906 if (!check_prevs_add(curr, hlock))
3907 return 0;
3908 }
3909
3910 graph_unlock();
3911 } else {
3912 /* after lookup_chain_cache_add(): */
3913 if (unlikely(!debug_locks))
3914 return 0;
3915 }
3916
3917 return 1;
3918 }
3919 #else
validate_chain(struct task_struct * curr,struct held_lock * hlock,int chain_head,u64 chain_key)3920 static inline int validate_chain(struct task_struct *curr,
3921 struct held_lock *hlock,
3922 int chain_head, u64 chain_key)
3923 {
3924 return 1;
3925 }
3926
init_chain_block_buckets(void)3927 static void init_chain_block_buckets(void) { }
3928 #endif /* CONFIG_PROVE_LOCKING */
3929
3930 /*
3931 * We are building curr_chain_key incrementally, so double-check
3932 * it from scratch, to make sure that it's done correctly:
3933 */
check_chain_key(struct task_struct * curr)3934 static void check_chain_key(struct task_struct *curr)
3935 {
3936 #ifdef CONFIG_DEBUG_LOCKDEP
3937 struct held_lock *hlock, *prev_hlock = NULL;
3938 unsigned int i;
3939 u64 chain_key = INITIAL_CHAIN_KEY;
3940
3941 for (i = 0; i < curr->lockdep_depth; i++) {
3942 hlock = curr->held_locks + i;
3943 if (chain_key != hlock->prev_chain_key) {
3944 debug_locks_off();
3945 /*
3946 * We got mighty confused, our chain keys don't match
3947 * with what we expect, someone trample on our task state?
3948 */
3949 WARN(1, "hm#1, depth: %u [%u], %016Lx != %016Lx\n",
3950 curr->lockdep_depth, i,
3951 (unsigned long long)chain_key,
3952 (unsigned long long)hlock->prev_chain_key);
3953 return;
3954 }
3955
3956 /*
3957 * hlock->class_idx can't go beyond MAX_LOCKDEP_KEYS, but is
3958 * it registered lock class index?
3959 */
3960 if (DEBUG_LOCKS_WARN_ON(!test_bit(hlock->class_idx, lock_classes_in_use)))
3961 return;
3962
3963 if (prev_hlock && (prev_hlock->irq_context !=
3964 hlock->irq_context))
3965 chain_key = INITIAL_CHAIN_KEY;
3966 chain_key = iterate_chain_key(chain_key, hlock_id(hlock));
3967 prev_hlock = hlock;
3968 }
3969 if (chain_key != curr->curr_chain_key) {
3970 debug_locks_off();
3971 /*
3972 * More smoking hash instead of calculating it, damn see these
3973 * numbers float.. I bet that a pink elephant stepped on my memory.
3974 */
3975 WARN(1, "hm#2, depth: %u [%u], %016Lx != %016Lx\n",
3976 curr->lockdep_depth, i,
3977 (unsigned long long)chain_key,
3978 (unsigned long long)curr->curr_chain_key);
3979 }
3980 #endif
3981 }
3982
3983 #ifdef CONFIG_PROVE_LOCKING
3984 static int mark_lock(struct task_struct *curr, struct held_lock *this,
3985 enum lock_usage_bit new_bit);
3986
print_usage_bug_scenario(struct held_lock * lock)3987 static void print_usage_bug_scenario(struct held_lock *lock)
3988 {
3989 struct lock_class *class = hlock_class(lock);
3990
3991 printk(" Possible unsafe locking scenario:\n\n");
3992 printk(" CPU0\n");
3993 printk(" ----\n");
3994 printk(" lock(");
3995 __print_lock_name(lock, class);
3996 printk(KERN_CONT ");\n");
3997 printk(" <Interrupt>\n");
3998 printk(" lock(");
3999 __print_lock_name(lock, class);
4000 printk(KERN_CONT ");\n");
4001 printk("\n *** DEADLOCK ***\n\n");
4002 }
4003
4004 static void
print_usage_bug(struct task_struct * curr,struct held_lock * this,enum lock_usage_bit prev_bit,enum lock_usage_bit new_bit)4005 print_usage_bug(struct task_struct *curr, struct held_lock *this,
4006 enum lock_usage_bit prev_bit, enum lock_usage_bit new_bit)
4007 {
4008 if (!debug_locks_off() || debug_locks_silent)
4009 return;
4010
4011 nbcon_cpu_emergency_enter();
4012
4013 pr_warn("\n");
4014 pr_warn("================================\n");
4015 pr_warn("WARNING: inconsistent lock state\n");
4016 print_kernel_ident();
4017 pr_warn("--------------------------------\n");
4018
4019 pr_warn("inconsistent {%s} -> {%s} usage.\n",
4020 usage_str[prev_bit], usage_str[new_bit]);
4021
4022 pr_warn("%s/%d [HC%u[%lu]:SC%u[%lu]:HE%u:SE%u] takes:\n",
4023 curr->comm, task_pid_nr(curr),
4024 lockdep_hardirq_context(), hardirq_count() >> HARDIRQ_SHIFT,
4025 lockdep_softirq_context(curr), softirq_count() >> SOFTIRQ_SHIFT,
4026 lockdep_hardirqs_enabled(),
4027 lockdep_softirqs_enabled(curr));
4028 print_lock(this);
4029
4030 pr_warn("{%s} state was registered at:\n", usage_str[prev_bit]);
4031 print_lock_trace(hlock_class(this)->usage_traces[prev_bit], 1);
4032
4033 print_irqtrace_events(curr);
4034 pr_warn("\nother info that might help us debug this:\n");
4035 print_usage_bug_scenario(this);
4036
4037 lockdep_print_held_locks(curr);
4038
4039 pr_warn("\nstack backtrace:\n");
4040 dump_stack();
4041
4042 nbcon_cpu_emergency_exit();
4043 }
4044
4045 /*
4046 * Print out an error if an invalid bit is set:
4047 */
4048 static inline int
valid_state(struct task_struct * curr,struct held_lock * this,enum lock_usage_bit new_bit,enum lock_usage_bit bad_bit)4049 valid_state(struct task_struct *curr, struct held_lock *this,
4050 enum lock_usage_bit new_bit, enum lock_usage_bit bad_bit)
4051 {
4052 if (unlikely(hlock_class(this)->usage_mask & (1 << bad_bit))) {
4053 graph_unlock();
4054 print_usage_bug(curr, this, bad_bit, new_bit);
4055 return 0;
4056 }
4057 return 1;
4058 }
4059
4060
4061 /*
4062 * print irq inversion bug:
4063 */
4064 static void
print_irq_inversion_bug(struct task_struct * curr,struct lock_list * root,struct lock_list * other,struct held_lock * this,int forwards,const char * irqclass)4065 print_irq_inversion_bug(struct task_struct *curr,
4066 struct lock_list *root, struct lock_list *other,
4067 struct held_lock *this, int forwards,
4068 const char *irqclass)
4069 {
4070 struct lock_list *entry = other;
4071 struct lock_list *middle = NULL;
4072 int depth;
4073
4074 if (!debug_locks_off_graph_unlock() || debug_locks_silent)
4075 return;
4076
4077 nbcon_cpu_emergency_enter();
4078
4079 pr_warn("\n");
4080 pr_warn("========================================================\n");
4081 pr_warn("WARNING: possible irq lock inversion dependency detected\n");
4082 print_kernel_ident();
4083 pr_warn("--------------------------------------------------------\n");
4084 pr_warn("%s/%d just changed the state of lock:\n",
4085 curr->comm, task_pid_nr(curr));
4086 print_lock(this);
4087 if (forwards)
4088 pr_warn("but this lock took another, %s-unsafe lock in the past:\n", irqclass);
4089 else
4090 pr_warn("but this lock was taken by another, %s-safe lock in the past:\n", irqclass);
4091 print_lock_name(NULL, other->class);
4092 pr_warn("\n\nand interrupts could create inverse lock ordering between them.\n\n");
4093
4094 pr_warn("\nother info that might help us debug this:\n");
4095
4096 /* Find a middle lock (if one exists) */
4097 depth = get_lock_depth(other);
4098 do {
4099 if (depth == 0 && (entry != root)) {
4100 pr_warn("lockdep:%s bad path found in chain graph\n", __func__);
4101 break;
4102 }
4103 middle = entry;
4104 entry = get_lock_parent(entry);
4105 depth--;
4106 } while (entry && entry != root && (depth >= 0));
4107 if (forwards)
4108 print_irq_lock_scenario(root, other,
4109 middle ? middle->class : root->class, other->class);
4110 else
4111 print_irq_lock_scenario(other, root,
4112 middle ? middle->class : other->class, root->class);
4113
4114 lockdep_print_held_locks(curr);
4115
4116 pr_warn("\nthe shortest dependencies between 2nd lock and 1st lock:\n");
4117 root->trace = save_trace();
4118 if (!root->trace)
4119 goto out;
4120 print_shortest_lock_dependencies(other, root);
4121
4122 pr_warn("\nstack backtrace:\n");
4123 dump_stack();
4124 out:
4125 nbcon_cpu_emergency_exit();
4126 }
4127
4128 /*
4129 * Prove that in the forwards-direction subgraph starting at <this>
4130 * there is no lock matching <mask>:
4131 */
4132 static int
check_usage_forwards(struct task_struct * curr,struct held_lock * this,enum lock_usage_bit bit)4133 check_usage_forwards(struct task_struct *curr, struct held_lock *this,
4134 enum lock_usage_bit bit)
4135 {
4136 enum bfs_result ret;
4137 struct lock_list root;
4138 struct lock_list *target_entry;
4139 enum lock_usage_bit read_bit = bit + LOCK_USAGE_READ_MASK;
4140 unsigned usage_mask = lock_flag(bit) | lock_flag(read_bit);
4141
4142 bfs_init_root(&root, this);
4143 ret = find_usage_forwards(&root, usage_mask, &target_entry);
4144 if (bfs_error(ret)) {
4145 print_bfs_bug(ret);
4146 return 0;
4147 }
4148 if (ret == BFS_RNOMATCH)
4149 return 1;
4150
4151 /* Check whether write or read usage is the match */
4152 if (target_entry->class->usage_mask & lock_flag(bit)) {
4153 print_irq_inversion_bug(curr, &root, target_entry,
4154 this, 1, state_name(bit));
4155 } else {
4156 print_irq_inversion_bug(curr, &root, target_entry,
4157 this, 1, state_name(read_bit));
4158 }
4159
4160 return 0;
4161 }
4162
4163 /*
4164 * Prove that in the backwards-direction subgraph starting at <this>
4165 * there is no lock matching <mask>:
4166 */
4167 static int
check_usage_backwards(struct task_struct * curr,struct held_lock * this,enum lock_usage_bit bit)4168 check_usage_backwards(struct task_struct *curr, struct held_lock *this,
4169 enum lock_usage_bit bit)
4170 {
4171 enum bfs_result ret;
4172 struct lock_list root;
4173 struct lock_list *target_entry;
4174 enum lock_usage_bit read_bit = bit + LOCK_USAGE_READ_MASK;
4175 unsigned usage_mask = lock_flag(bit) | lock_flag(read_bit);
4176
4177 bfs_init_rootb(&root, this);
4178 ret = find_usage_backwards(&root, usage_mask, &target_entry);
4179 if (bfs_error(ret)) {
4180 print_bfs_bug(ret);
4181 return 0;
4182 }
4183 if (ret == BFS_RNOMATCH)
4184 return 1;
4185
4186 /* Check whether write or read usage is the match */
4187 if (target_entry->class->usage_mask & lock_flag(bit)) {
4188 print_irq_inversion_bug(curr, &root, target_entry,
4189 this, 0, state_name(bit));
4190 } else {
4191 print_irq_inversion_bug(curr, &root, target_entry,
4192 this, 0, state_name(read_bit));
4193 }
4194
4195 return 0;
4196 }
4197
print_irqtrace_events(struct task_struct * curr)4198 void print_irqtrace_events(struct task_struct *curr)
4199 {
4200 const struct irqtrace_events *trace = &curr->irqtrace;
4201
4202 nbcon_cpu_emergency_enter();
4203
4204 printk("irq event stamp: %u\n", trace->irq_events);
4205 printk("hardirqs last enabled at (%u): [<%px>] %pS\n",
4206 trace->hardirq_enable_event, (void *)trace->hardirq_enable_ip,
4207 (void *)trace->hardirq_enable_ip);
4208 printk("hardirqs last disabled at (%u): [<%px>] %pS\n",
4209 trace->hardirq_disable_event, (void *)trace->hardirq_disable_ip,
4210 (void *)trace->hardirq_disable_ip);
4211 printk("softirqs last enabled at (%u): [<%px>] %pS\n",
4212 trace->softirq_enable_event, (void *)trace->softirq_enable_ip,
4213 (void *)trace->softirq_enable_ip);
4214 printk("softirqs last disabled at (%u): [<%px>] %pS\n",
4215 trace->softirq_disable_event, (void *)trace->softirq_disable_ip,
4216 (void *)trace->softirq_disable_ip);
4217
4218 nbcon_cpu_emergency_exit();
4219 }
4220
HARDIRQ_verbose(struct lock_class * class)4221 static int HARDIRQ_verbose(struct lock_class *class)
4222 {
4223 #if HARDIRQ_VERBOSE
4224 return class_filter(class);
4225 #endif
4226 return 0;
4227 }
4228
SOFTIRQ_verbose(struct lock_class * class)4229 static int SOFTIRQ_verbose(struct lock_class *class)
4230 {
4231 #if SOFTIRQ_VERBOSE
4232 return class_filter(class);
4233 #endif
4234 return 0;
4235 }
4236
4237 static int (*state_verbose_f[])(struct lock_class *class) = {
4238 #define LOCKDEP_STATE(__STATE) \
4239 __STATE##_verbose,
4240 #include "lockdep_states.h"
4241 #undef LOCKDEP_STATE
4242 };
4243
state_verbose(enum lock_usage_bit bit,struct lock_class * class)4244 static inline int state_verbose(enum lock_usage_bit bit,
4245 struct lock_class *class)
4246 {
4247 return state_verbose_f[bit >> LOCK_USAGE_DIR_MASK](class);
4248 }
4249
4250 typedef int (*check_usage_f)(struct task_struct *, struct held_lock *,
4251 enum lock_usage_bit bit, const char *name);
4252
4253 static int
mark_lock_irq(struct task_struct * curr,struct held_lock * this,enum lock_usage_bit new_bit)4254 mark_lock_irq(struct task_struct *curr, struct held_lock *this,
4255 enum lock_usage_bit new_bit)
4256 {
4257 int excl_bit = exclusive_bit(new_bit);
4258 int read = new_bit & LOCK_USAGE_READ_MASK;
4259 int dir = new_bit & LOCK_USAGE_DIR_MASK;
4260
4261 /*
4262 * Validate that this particular lock does not have conflicting
4263 * usage states.
4264 */
4265 if (!valid_state(curr, this, new_bit, excl_bit))
4266 return 0;
4267
4268 /*
4269 * Check for read in write conflicts
4270 */
4271 if (!read && !valid_state(curr, this, new_bit,
4272 excl_bit + LOCK_USAGE_READ_MASK))
4273 return 0;
4274
4275
4276 /*
4277 * Validate that the lock dependencies don't have conflicting usage
4278 * states.
4279 */
4280 if (dir) {
4281 /*
4282 * mark ENABLED has to look backwards -- to ensure no dependee
4283 * has USED_IN state, which, again, would allow recursion deadlocks.
4284 */
4285 if (!check_usage_backwards(curr, this, excl_bit))
4286 return 0;
4287 } else {
4288 /*
4289 * mark USED_IN has to look forwards -- to ensure no dependency
4290 * has ENABLED state, which would allow recursion deadlocks.
4291 */
4292 if (!check_usage_forwards(curr, this, excl_bit))
4293 return 0;
4294 }
4295
4296 if (state_verbose(new_bit, hlock_class(this)))
4297 return 2;
4298
4299 return 1;
4300 }
4301
4302 /*
4303 * Mark all held locks with a usage bit:
4304 */
4305 static int
mark_held_locks(struct task_struct * curr,enum lock_usage_bit base_bit)4306 mark_held_locks(struct task_struct *curr, enum lock_usage_bit base_bit)
4307 {
4308 struct held_lock *hlock;
4309 int i;
4310
4311 for (i = 0; i < curr->lockdep_depth; i++) {
4312 enum lock_usage_bit hlock_bit = base_bit;
4313 hlock = curr->held_locks + i;
4314
4315 if (hlock->read)
4316 hlock_bit += LOCK_USAGE_READ_MASK;
4317
4318 BUG_ON(hlock_bit >= LOCK_USAGE_STATES);
4319
4320 if (!hlock->check)
4321 continue;
4322
4323 if (!mark_lock(curr, hlock, hlock_bit))
4324 return 0;
4325 }
4326
4327 return 1;
4328 }
4329
4330 /*
4331 * Hardirqs will be enabled:
4332 */
__trace_hardirqs_on_caller(void)4333 static void __trace_hardirqs_on_caller(void)
4334 {
4335 struct task_struct *curr = current;
4336
4337 /*
4338 * We are going to turn hardirqs on, so set the
4339 * usage bit for all held locks:
4340 */
4341 if (!mark_held_locks(curr, LOCK_ENABLED_HARDIRQ))
4342 return;
4343 /*
4344 * If we have softirqs enabled, then set the usage
4345 * bit for all held locks. (disabled hardirqs prevented
4346 * this bit from being set before)
4347 */
4348 if (curr->softirqs_enabled)
4349 mark_held_locks(curr, LOCK_ENABLED_SOFTIRQ);
4350 }
4351
4352 /**
4353 * lockdep_hardirqs_on_prepare - Prepare for enabling interrupts
4354 *
4355 * Invoked before a possible transition to RCU idle from exit to user or
4356 * guest mode. This ensures that all RCU operations are done before RCU
4357 * stops watching. After the RCU transition lockdep_hardirqs_on() has to be
4358 * invoked to set the final state.
4359 */
lockdep_hardirqs_on_prepare(void)4360 void lockdep_hardirqs_on_prepare(void)
4361 {
4362 if (unlikely(!debug_locks))
4363 return;
4364
4365 /*
4366 * NMIs do not (and cannot) track lock dependencies, nothing to do.
4367 */
4368 if (unlikely(in_nmi()))
4369 return;
4370
4371 if (unlikely(this_cpu_read(lockdep_recursion)))
4372 return;
4373
4374 if (unlikely(lockdep_hardirqs_enabled())) {
4375 /*
4376 * Neither irq nor preemption are disabled here
4377 * so this is racy by nature but losing one hit
4378 * in a stat is not a big deal.
4379 */
4380 __debug_atomic_inc(redundant_hardirqs_on);
4381 return;
4382 }
4383
4384 /*
4385 * We're enabling irqs and according to our state above irqs weren't
4386 * already enabled, yet we find the hardware thinks they are in fact
4387 * enabled.. someone messed up their IRQ state tracing.
4388 */
4389 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
4390 return;
4391
4392 /*
4393 * See the fine text that goes along with this variable definition.
4394 */
4395 if (DEBUG_LOCKS_WARN_ON(early_boot_irqs_disabled))
4396 return;
4397
4398 /*
4399 * Can't allow enabling interrupts while in an interrupt handler,
4400 * that's general bad form and such. Recursion, limited stack etc..
4401 */
4402 if (DEBUG_LOCKS_WARN_ON(lockdep_hardirq_context()))
4403 return;
4404
4405 current->hardirq_chain_key = current->curr_chain_key;
4406
4407 lockdep_recursion_inc();
4408 __trace_hardirqs_on_caller();
4409 lockdep_recursion_finish();
4410 }
4411 EXPORT_SYMBOL_GPL(lockdep_hardirqs_on_prepare);
4412
lockdep_hardirqs_on(unsigned long ip)4413 void noinstr lockdep_hardirqs_on(unsigned long ip)
4414 {
4415 struct irqtrace_events *trace = ¤t->irqtrace;
4416
4417 if (unlikely(!debug_locks))
4418 return;
4419
4420 /*
4421 * NMIs can happen in the middle of local_irq_{en,dis}able() where the
4422 * tracking state and hardware state are out of sync.
4423 *
4424 * NMIs must save lockdep_hardirqs_enabled() to restore IRQ state from,
4425 * and not rely on hardware state like normal interrupts.
4426 */
4427 if (unlikely(in_nmi())) {
4428 if (!IS_ENABLED(CONFIG_TRACE_IRQFLAGS_NMI))
4429 return;
4430
4431 /*
4432 * Skip:
4433 * - recursion check, because NMI can hit lockdep;
4434 * - hardware state check, because above;
4435 * - chain_key check, see lockdep_hardirqs_on_prepare().
4436 */
4437 goto skip_checks;
4438 }
4439
4440 if (unlikely(this_cpu_read(lockdep_recursion)))
4441 return;
4442
4443 if (lockdep_hardirqs_enabled()) {
4444 /*
4445 * Neither irq nor preemption are disabled here
4446 * so this is racy by nature but losing one hit
4447 * in a stat is not a big deal.
4448 */
4449 __debug_atomic_inc(redundant_hardirqs_on);
4450 return;
4451 }
4452
4453 /*
4454 * We're enabling irqs and according to our state above irqs weren't
4455 * already enabled, yet we find the hardware thinks they are in fact
4456 * enabled.. someone messed up their IRQ state tracing.
4457 */
4458 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
4459 return;
4460
4461 /*
4462 * Ensure the lock stack remained unchanged between
4463 * lockdep_hardirqs_on_prepare() and lockdep_hardirqs_on().
4464 */
4465 DEBUG_LOCKS_WARN_ON(current->hardirq_chain_key !=
4466 current->curr_chain_key);
4467
4468 skip_checks:
4469 /* we'll do an OFF -> ON transition: */
4470 __this_cpu_write(hardirqs_enabled, 1);
4471 trace->hardirq_enable_ip = ip;
4472 trace->hardirq_enable_event = ++trace->irq_events;
4473 debug_atomic_inc(hardirqs_on_events);
4474 }
4475 EXPORT_SYMBOL_GPL(lockdep_hardirqs_on);
4476
4477 /*
4478 * Hardirqs were disabled:
4479 */
lockdep_hardirqs_off(unsigned long ip)4480 void noinstr lockdep_hardirqs_off(unsigned long ip)
4481 {
4482 if (unlikely(!debug_locks))
4483 return;
4484
4485 /*
4486 * Matching lockdep_hardirqs_on(), allow NMIs in the middle of lockdep;
4487 * they will restore the software state. This ensures the software
4488 * state is consistent inside NMIs as well.
4489 */
4490 if (in_nmi()) {
4491 if (!IS_ENABLED(CONFIG_TRACE_IRQFLAGS_NMI))
4492 return;
4493 } else if (__this_cpu_read(lockdep_recursion))
4494 return;
4495
4496 /*
4497 * So we're supposed to get called after you mask local IRQs, but for
4498 * some reason the hardware doesn't quite think you did a proper job.
4499 */
4500 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
4501 return;
4502
4503 if (lockdep_hardirqs_enabled()) {
4504 struct irqtrace_events *trace = ¤t->irqtrace;
4505
4506 /*
4507 * We have done an ON -> OFF transition:
4508 */
4509 __this_cpu_write(hardirqs_enabled, 0);
4510 trace->hardirq_disable_ip = ip;
4511 trace->hardirq_disable_event = ++trace->irq_events;
4512 debug_atomic_inc(hardirqs_off_events);
4513 } else {
4514 debug_atomic_inc(redundant_hardirqs_off);
4515 }
4516 }
4517 EXPORT_SYMBOL_GPL(lockdep_hardirqs_off);
4518
4519 /*
4520 * Softirqs will be enabled:
4521 */
lockdep_softirqs_on(unsigned long ip)4522 void lockdep_softirqs_on(unsigned long ip)
4523 {
4524 struct irqtrace_events *trace = ¤t->irqtrace;
4525
4526 if (unlikely(!lockdep_enabled()))
4527 return;
4528
4529 /*
4530 * We fancy IRQs being disabled here, see softirq.c, avoids
4531 * funny state and nesting things.
4532 */
4533 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
4534 return;
4535
4536 if (current->softirqs_enabled) {
4537 debug_atomic_inc(redundant_softirqs_on);
4538 return;
4539 }
4540
4541 lockdep_recursion_inc();
4542 /*
4543 * We'll do an OFF -> ON transition:
4544 */
4545 current->softirqs_enabled = 1;
4546 trace->softirq_enable_ip = ip;
4547 trace->softirq_enable_event = ++trace->irq_events;
4548 debug_atomic_inc(softirqs_on_events);
4549 /*
4550 * We are going to turn softirqs on, so set the
4551 * usage bit for all held locks, if hardirqs are
4552 * enabled too:
4553 */
4554 if (lockdep_hardirqs_enabled())
4555 mark_held_locks(current, LOCK_ENABLED_SOFTIRQ);
4556 lockdep_recursion_finish();
4557 }
4558
4559 /*
4560 * Softirqs were disabled:
4561 */
lockdep_softirqs_off(unsigned long ip)4562 void lockdep_softirqs_off(unsigned long ip)
4563 {
4564 if (unlikely(!lockdep_enabled()))
4565 return;
4566
4567 /*
4568 * We fancy IRQs being disabled here, see softirq.c
4569 */
4570 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
4571 return;
4572
4573 if (current->softirqs_enabled) {
4574 struct irqtrace_events *trace = ¤t->irqtrace;
4575
4576 /*
4577 * We have done an ON -> OFF transition:
4578 */
4579 current->softirqs_enabled = 0;
4580 trace->softirq_disable_ip = ip;
4581 trace->softirq_disable_event = ++trace->irq_events;
4582 debug_atomic_inc(softirqs_off_events);
4583 /*
4584 * Whoops, we wanted softirqs off, so why aren't they?
4585 */
4586 DEBUG_LOCKS_WARN_ON(!softirq_count());
4587 } else
4588 debug_atomic_inc(redundant_softirqs_off);
4589 }
4590
4591 /**
4592 * lockdep_cleanup_dead_cpu - Ensure CPU lockdep state is cleanly stopped
4593 *
4594 * @cpu: index of offlined CPU
4595 * @idle: task pointer for offlined CPU's idle thread
4596 *
4597 * Invoked after the CPU is dead. Ensures that the tracing infrastructure
4598 * is left in a suitable state for the CPU to be subsequently brought
4599 * online again.
4600 */
lockdep_cleanup_dead_cpu(unsigned int cpu,struct task_struct * idle)4601 void lockdep_cleanup_dead_cpu(unsigned int cpu, struct task_struct *idle)
4602 {
4603 if (unlikely(!debug_locks))
4604 return;
4605
4606 if (unlikely(per_cpu(hardirqs_enabled, cpu))) {
4607 pr_warn("CPU %u left hardirqs enabled!", cpu);
4608 if (idle)
4609 print_irqtrace_events(idle);
4610 /* Clean it up for when the CPU comes online again. */
4611 per_cpu(hardirqs_enabled, cpu) = 0;
4612 }
4613 }
4614
4615 static int
mark_usage(struct task_struct * curr,struct held_lock * hlock,int check)4616 mark_usage(struct task_struct *curr, struct held_lock *hlock, int check)
4617 {
4618 if (!check)
4619 goto lock_used;
4620
4621 /*
4622 * If non-trylock use in a hardirq or softirq context, then
4623 * mark the lock as used in these contexts:
4624 */
4625 if (!hlock->trylock) {
4626 if (hlock->read) {
4627 if (lockdep_hardirq_context())
4628 if (!mark_lock(curr, hlock,
4629 LOCK_USED_IN_HARDIRQ_READ))
4630 return 0;
4631 if (curr->softirq_context)
4632 if (!mark_lock(curr, hlock,
4633 LOCK_USED_IN_SOFTIRQ_READ))
4634 return 0;
4635 } else {
4636 if (lockdep_hardirq_context())
4637 if (!mark_lock(curr, hlock, LOCK_USED_IN_HARDIRQ))
4638 return 0;
4639 if (curr->softirq_context)
4640 if (!mark_lock(curr, hlock, LOCK_USED_IN_SOFTIRQ))
4641 return 0;
4642 }
4643 }
4644
4645 /*
4646 * For lock_sync(), don't mark the ENABLED usage, since lock_sync()
4647 * creates no critical section and no extra dependency can be introduced
4648 * by interrupts
4649 */
4650 if (!hlock->hardirqs_off && !hlock->sync) {
4651 if (hlock->read) {
4652 if (!mark_lock(curr, hlock,
4653 LOCK_ENABLED_HARDIRQ_READ))
4654 return 0;
4655 if (curr->softirqs_enabled)
4656 if (!mark_lock(curr, hlock,
4657 LOCK_ENABLED_SOFTIRQ_READ))
4658 return 0;
4659 } else {
4660 if (!mark_lock(curr, hlock,
4661 LOCK_ENABLED_HARDIRQ))
4662 return 0;
4663 if (curr->softirqs_enabled)
4664 if (!mark_lock(curr, hlock,
4665 LOCK_ENABLED_SOFTIRQ))
4666 return 0;
4667 }
4668 }
4669
4670 lock_used:
4671 /* mark it as used: */
4672 if (!mark_lock(curr, hlock, LOCK_USED))
4673 return 0;
4674
4675 return 1;
4676 }
4677
task_irq_context(struct task_struct * task)4678 static inline unsigned int task_irq_context(struct task_struct *task)
4679 {
4680 return LOCK_CHAIN_HARDIRQ_CONTEXT * !!lockdep_hardirq_context() +
4681 LOCK_CHAIN_SOFTIRQ_CONTEXT * !!task->softirq_context;
4682 }
4683
separate_irq_context(struct task_struct * curr,struct held_lock * hlock)4684 static int separate_irq_context(struct task_struct *curr,
4685 struct held_lock *hlock)
4686 {
4687 unsigned int depth = curr->lockdep_depth;
4688
4689 /*
4690 * Keep track of points where we cross into an interrupt context:
4691 */
4692 if (depth) {
4693 struct held_lock *prev_hlock;
4694
4695 prev_hlock = curr->held_locks + depth-1;
4696 /*
4697 * If we cross into another context, reset the
4698 * hash key (this also prevents the checking and the
4699 * adding of the dependency to 'prev'):
4700 */
4701 if (prev_hlock->irq_context != hlock->irq_context)
4702 return 1;
4703 }
4704 return 0;
4705 }
4706
4707 /*
4708 * Mark a lock with a usage bit, and validate the state transition:
4709 */
mark_lock(struct task_struct * curr,struct held_lock * this,enum lock_usage_bit new_bit)4710 static int mark_lock(struct task_struct *curr, struct held_lock *this,
4711 enum lock_usage_bit new_bit)
4712 {
4713 unsigned int new_mask, ret = 1;
4714
4715 if (new_bit >= LOCK_USAGE_STATES) {
4716 DEBUG_LOCKS_WARN_ON(1);
4717 return 0;
4718 }
4719
4720 if (new_bit == LOCK_USED && this->read)
4721 new_bit = LOCK_USED_READ;
4722
4723 new_mask = 1 << new_bit;
4724
4725 /*
4726 * If already set then do not dirty the cacheline,
4727 * nor do any checks:
4728 */
4729 if (likely(hlock_class(this)->usage_mask & new_mask))
4730 return 1;
4731
4732 if (!graph_lock())
4733 return 0;
4734 /*
4735 * Make sure we didn't race:
4736 */
4737 if (unlikely(hlock_class(this)->usage_mask & new_mask))
4738 goto unlock;
4739
4740 if (!hlock_class(this)->usage_mask)
4741 debug_atomic_dec(nr_unused_locks);
4742
4743 hlock_class(this)->usage_mask |= new_mask;
4744
4745 if (new_bit < LOCK_TRACE_STATES) {
4746 if (!(hlock_class(this)->usage_traces[new_bit] = save_trace()))
4747 return 0;
4748 }
4749
4750 if (new_bit < LOCK_USED) {
4751 ret = mark_lock_irq(curr, this, new_bit);
4752 if (!ret)
4753 return 0;
4754 }
4755
4756 unlock:
4757 graph_unlock();
4758
4759 /*
4760 * We must printk outside of the graph_lock:
4761 */
4762 if (ret == 2) {
4763 nbcon_cpu_emergency_enter();
4764 printk("\nmarked lock as {%s}:\n", usage_str[new_bit]);
4765 print_lock(this);
4766 print_irqtrace_events(curr);
4767 dump_stack();
4768 nbcon_cpu_emergency_exit();
4769 }
4770
4771 return ret;
4772 }
4773
task_wait_context(struct task_struct * curr)4774 static inline short task_wait_context(struct task_struct *curr)
4775 {
4776 /*
4777 * Set appropriate wait type for the context; for IRQs we have to take
4778 * into account force_irqthread as that is implied by PREEMPT_RT.
4779 */
4780 if (lockdep_hardirq_context()) {
4781 /*
4782 * Check if force_irqthreads will run us threaded.
4783 */
4784 if (curr->hardirq_threaded || curr->irq_config)
4785 return LD_WAIT_CONFIG;
4786
4787 return LD_WAIT_SPIN;
4788 } else if (curr->softirq_context) {
4789 /*
4790 * Softirqs are always threaded.
4791 */
4792 return LD_WAIT_CONFIG;
4793 }
4794
4795 return LD_WAIT_MAX;
4796 }
4797
4798 static int
print_lock_invalid_wait_context(struct task_struct * curr,struct held_lock * hlock)4799 print_lock_invalid_wait_context(struct task_struct *curr,
4800 struct held_lock *hlock)
4801 {
4802 short curr_inner;
4803
4804 if (!debug_locks_off())
4805 return 0;
4806 if (debug_locks_silent)
4807 return 0;
4808
4809 nbcon_cpu_emergency_enter();
4810
4811 pr_warn("\n");
4812 pr_warn("=============================\n");
4813 pr_warn("[ BUG: Invalid wait context ]\n");
4814 print_kernel_ident();
4815 pr_warn("-----------------------------\n");
4816
4817 pr_warn("%s/%d is trying to lock:\n", curr->comm, task_pid_nr(curr));
4818 print_lock(hlock);
4819
4820 pr_warn("other info that might help us debug this:\n");
4821
4822 curr_inner = task_wait_context(curr);
4823 pr_warn("context-{%d:%d}\n", curr_inner, curr_inner);
4824
4825 lockdep_print_held_locks(curr);
4826
4827 pr_warn("stack backtrace:\n");
4828 dump_stack();
4829
4830 nbcon_cpu_emergency_exit();
4831
4832 return 0;
4833 }
4834
4835 /*
4836 * Verify the wait_type context.
4837 *
4838 * This check validates we take locks in the right wait-type order; that is it
4839 * ensures that we do not take mutexes inside spinlocks and do not attempt to
4840 * acquire spinlocks inside raw_spinlocks and the sort.
4841 *
4842 * The entire thing is slightly more complex because of RCU, RCU is a lock that
4843 * can be taken from (pretty much) any context but also has constraints.
4844 * However when taken in a stricter environment the RCU lock does not loosen
4845 * the constraints.
4846 *
4847 * Therefore we must look for the strictest environment in the lock stack and
4848 * compare that to the lock we're trying to acquire.
4849 */
check_wait_context(struct task_struct * curr,struct held_lock * next)4850 static int check_wait_context(struct task_struct *curr, struct held_lock *next)
4851 {
4852 u8 next_inner = hlock_class(next)->wait_type_inner;
4853 u8 next_outer = hlock_class(next)->wait_type_outer;
4854 u8 curr_inner;
4855 int depth;
4856
4857 if (!next_inner || next->trylock)
4858 return 0;
4859
4860 if (!next_outer)
4861 next_outer = next_inner;
4862
4863 /*
4864 * Find start of current irq_context..
4865 */
4866 for (depth = curr->lockdep_depth - 1; depth >= 0; depth--) {
4867 struct held_lock *prev = curr->held_locks + depth;
4868 if (prev->irq_context != next->irq_context)
4869 break;
4870 }
4871 depth++;
4872
4873 curr_inner = task_wait_context(curr);
4874
4875 for (; depth < curr->lockdep_depth; depth++) {
4876 struct held_lock *prev = curr->held_locks + depth;
4877 struct lock_class *class = hlock_class(prev);
4878 u8 prev_inner = class->wait_type_inner;
4879
4880 if (prev_inner) {
4881 /*
4882 * We can have a bigger inner than a previous one
4883 * when outer is smaller than inner, as with RCU.
4884 *
4885 * Also due to trylocks.
4886 */
4887 curr_inner = min(curr_inner, prev_inner);
4888
4889 /*
4890 * Allow override for annotations -- this is typically
4891 * only valid/needed for code that only exists when
4892 * CONFIG_PREEMPT_RT=n.
4893 */
4894 if (unlikely(class->lock_type == LD_LOCK_WAIT_OVERRIDE))
4895 curr_inner = prev_inner;
4896 }
4897 }
4898
4899 if (next_outer > curr_inner)
4900 return print_lock_invalid_wait_context(curr, next);
4901
4902 return 0;
4903 }
4904
4905 #else /* CONFIG_PROVE_LOCKING */
4906
4907 static inline int
mark_usage(struct task_struct * curr,struct held_lock * hlock,int check)4908 mark_usage(struct task_struct *curr, struct held_lock *hlock, int check)
4909 {
4910 return 1;
4911 }
4912
task_irq_context(struct task_struct * task)4913 static inline unsigned int task_irq_context(struct task_struct *task)
4914 {
4915 return 0;
4916 }
4917
separate_irq_context(struct task_struct * curr,struct held_lock * hlock)4918 static inline int separate_irq_context(struct task_struct *curr,
4919 struct held_lock *hlock)
4920 {
4921 return 0;
4922 }
4923
check_wait_context(struct task_struct * curr,struct held_lock * next)4924 static inline int check_wait_context(struct task_struct *curr,
4925 struct held_lock *next)
4926 {
4927 return 0;
4928 }
4929
4930 #endif /* CONFIG_PROVE_LOCKING */
4931
4932 /*
4933 * Initialize a lock instance's lock-class mapping info:
4934 */
lockdep_init_map_type(struct lockdep_map * lock,const char * name,struct lock_class_key * key,int subclass,u8 inner,u8 outer,u8 lock_type)4935 void lockdep_init_map_type(struct lockdep_map *lock, const char *name,
4936 struct lock_class_key *key, int subclass,
4937 u8 inner, u8 outer, u8 lock_type)
4938 {
4939 int i;
4940
4941 for (i = 0; i < NR_LOCKDEP_CACHING_CLASSES; i++)
4942 lock->class_cache[i] = NULL;
4943
4944 #ifdef CONFIG_LOCK_STAT
4945 lock->cpu = raw_smp_processor_id();
4946 #endif
4947
4948 /*
4949 * Can't be having no nameless bastards around this place!
4950 */
4951 if (DEBUG_LOCKS_WARN_ON(!name)) {
4952 lock->name = "NULL";
4953 return;
4954 }
4955
4956 lock->name = name;
4957
4958 lock->wait_type_outer = outer;
4959 lock->wait_type_inner = inner;
4960 lock->lock_type = lock_type;
4961
4962 /*
4963 * No key, no joy, we need to hash something.
4964 */
4965 if (DEBUG_LOCKS_WARN_ON(!key))
4966 return;
4967 /*
4968 * Sanity check, the lock-class key must either have been allocated
4969 * statically or must have been registered as a dynamic key.
4970 */
4971 if (!static_obj(key) && !is_dynamic_key(key)) {
4972 if (debug_locks)
4973 printk(KERN_ERR "BUG: key %px has not been registered!\n", key);
4974 DEBUG_LOCKS_WARN_ON(1);
4975 return;
4976 }
4977 lock->key = key;
4978
4979 if (unlikely(!debug_locks))
4980 return;
4981
4982 if (subclass) {
4983 unsigned long flags;
4984
4985 if (DEBUG_LOCKS_WARN_ON(!lockdep_enabled()))
4986 return;
4987
4988 raw_local_irq_save(flags);
4989 lockdep_recursion_inc();
4990 register_lock_class(lock, subclass, 1);
4991 lockdep_recursion_finish();
4992 raw_local_irq_restore(flags);
4993 }
4994 }
4995 EXPORT_SYMBOL_GPL(lockdep_init_map_type);
4996
4997 struct lock_class_key __lockdep_no_validate__;
4998 EXPORT_SYMBOL_GPL(__lockdep_no_validate__);
4999
5000 struct lock_class_key __lockdep_no_track__;
5001 EXPORT_SYMBOL_GPL(__lockdep_no_track__);
5002
5003 #ifdef CONFIG_PROVE_LOCKING
lockdep_set_lock_cmp_fn(struct lockdep_map * lock,lock_cmp_fn cmp_fn,lock_print_fn print_fn)5004 void lockdep_set_lock_cmp_fn(struct lockdep_map *lock, lock_cmp_fn cmp_fn,
5005 lock_print_fn print_fn)
5006 {
5007 struct lock_class *class = lock->class_cache[0];
5008 unsigned long flags;
5009
5010 raw_local_irq_save(flags);
5011 lockdep_recursion_inc();
5012
5013 if (!class)
5014 class = register_lock_class(lock, 0, 0);
5015
5016 if (class) {
5017 WARN_ON(class->cmp_fn && class->cmp_fn != cmp_fn);
5018 WARN_ON(class->print_fn && class->print_fn != print_fn);
5019
5020 class->cmp_fn = cmp_fn;
5021 class->print_fn = print_fn;
5022 }
5023
5024 lockdep_recursion_finish();
5025 raw_local_irq_restore(flags);
5026 }
5027 EXPORT_SYMBOL_GPL(lockdep_set_lock_cmp_fn);
5028 #endif
5029
5030 static void
print_lock_nested_lock_not_held(struct task_struct * curr,struct held_lock * hlock)5031 print_lock_nested_lock_not_held(struct task_struct *curr,
5032 struct held_lock *hlock)
5033 {
5034 if (!debug_locks_off())
5035 return;
5036 if (debug_locks_silent)
5037 return;
5038
5039 nbcon_cpu_emergency_enter();
5040
5041 pr_warn("\n");
5042 pr_warn("==================================\n");
5043 pr_warn("WARNING: Nested lock was not taken\n");
5044 print_kernel_ident();
5045 pr_warn("----------------------------------\n");
5046
5047 pr_warn("%s/%d is trying to lock:\n", curr->comm, task_pid_nr(curr));
5048 print_lock(hlock);
5049
5050 pr_warn("\nbut this task is not holding:\n");
5051 pr_warn("%s\n", hlock->nest_lock->name);
5052
5053 pr_warn("\nstack backtrace:\n");
5054 dump_stack();
5055
5056 pr_warn("\nother info that might help us debug this:\n");
5057 lockdep_print_held_locks(curr);
5058
5059 pr_warn("\nstack backtrace:\n");
5060 dump_stack();
5061
5062 nbcon_cpu_emergency_exit();
5063 }
5064
5065 static int __lock_is_held(const struct lockdep_map *lock, int read);
5066
5067 /*
5068 * This gets called for every mutex_lock*()/spin_lock*() operation.
5069 * We maintain the dependency maps and validate the locking attempt:
5070 *
5071 * The callers must make sure that IRQs are disabled before calling it,
5072 * otherwise we could get an interrupt which would want to take locks,
5073 * which would end up in lockdep again.
5074 */
__lock_acquire(struct lockdep_map * lock,unsigned int subclass,int trylock,int read,int check,int hardirqs_off,struct lockdep_map * nest_lock,unsigned long ip,int references,int pin_count,int sync)5075 static int __lock_acquire(struct lockdep_map *lock, unsigned int subclass,
5076 int trylock, int read, int check, int hardirqs_off,
5077 struct lockdep_map *nest_lock, unsigned long ip,
5078 int references, int pin_count, int sync)
5079 {
5080 struct task_struct *curr = current;
5081 struct lock_class *class = NULL;
5082 struct held_lock *hlock;
5083 unsigned int depth;
5084 int chain_head = 0;
5085 int class_idx;
5086 u64 chain_key;
5087
5088 if (unlikely(!debug_locks))
5089 return 0;
5090
5091 if (unlikely(lock->key == &__lockdep_no_track__))
5092 return 0;
5093
5094 if (!prove_locking || lock->key == &__lockdep_no_validate__)
5095 check = 0;
5096
5097 if (subclass < NR_LOCKDEP_CACHING_CLASSES)
5098 class = lock->class_cache[subclass];
5099 /*
5100 * Not cached?
5101 */
5102 if (unlikely(!class)) {
5103 class = register_lock_class(lock, subclass, 0);
5104 if (!class)
5105 return 0;
5106 }
5107
5108 debug_class_ops_inc(class);
5109
5110 if (very_verbose(class)) {
5111 nbcon_cpu_emergency_enter();
5112 printk("\nacquire class [%px] %s", class->key, class->name);
5113 if (class->name_version > 1)
5114 printk(KERN_CONT "#%d", class->name_version);
5115 printk(KERN_CONT "\n");
5116 dump_stack();
5117 nbcon_cpu_emergency_exit();
5118 }
5119
5120 /*
5121 * Add the lock to the list of currently held locks.
5122 * (we dont increase the depth just yet, up until the
5123 * dependency checks are done)
5124 */
5125 depth = curr->lockdep_depth;
5126 /*
5127 * Ran out of static storage for our per-task lock stack again have we?
5128 */
5129 if (DEBUG_LOCKS_WARN_ON(depth >= MAX_LOCK_DEPTH))
5130 return 0;
5131
5132 class_idx = class - lock_classes;
5133
5134 if (depth && !sync) {
5135 /* we're holding locks and the new held lock is not a sync */
5136 hlock = curr->held_locks + depth - 1;
5137 if (hlock->class_idx == class_idx && nest_lock) {
5138 if (!references)
5139 references++;
5140
5141 if (!hlock->references)
5142 hlock->references++;
5143
5144 hlock->references += references;
5145
5146 /* Overflow */
5147 if (DEBUG_LOCKS_WARN_ON(hlock->references < references))
5148 return 0;
5149
5150 return 2;
5151 }
5152 }
5153
5154 hlock = curr->held_locks + depth;
5155 /*
5156 * Plain impossible, we just registered it and checked it weren't no
5157 * NULL like.. I bet this mushroom I ate was good!
5158 */
5159 if (DEBUG_LOCKS_WARN_ON(!class))
5160 return 0;
5161 hlock->class_idx = class_idx;
5162 hlock->acquire_ip = ip;
5163 hlock->instance = lock;
5164 hlock->nest_lock = nest_lock;
5165 hlock->irq_context = task_irq_context(curr);
5166 hlock->trylock = trylock;
5167 hlock->read = read;
5168 hlock->check = check;
5169 hlock->sync = !!sync;
5170 hlock->hardirqs_off = !!hardirqs_off;
5171 hlock->references = references;
5172 #ifdef CONFIG_LOCK_STAT
5173 hlock->waittime_stamp = 0;
5174 hlock->holdtime_stamp = lockstat_clock();
5175 #endif
5176 hlock->pin_count = pin_count;
5177
5178 if (check_wait_context(curr, hlock))
5179 return 0;
5180
5181 /* Initialize the lock usage bit */
5182 if (!mark_usage(curr, hlock, check))
5183 return 0;
5184
5185 /*
5186 * Calculate the chain hash: it's the combined hash of all the
5187 * lock keys along the dependency chain. We save the hash value
5188 * at every step so that we can get the current hash easily
5189 * after unlock. The chain hash is then used to cache dependency
5190 * results.
5191 *
5192 * The 'key ID' is what is the most compact key value to drive
5193 * the hash, not class->key.
5194 */
5195 /*
5196 * Whoops, we did it again.. class_idx is invalid.
5197 */
5198 if (DEBUG_LOCKS_WARN_ON(!test_bit(class_idx, lock_classes_in_use)))
5199 return 0;
5200
5201 chain_key = curr->curr_chain_key;
5202 if (!depth) {
5203 /*
5204 * How can we have a chain hash when we ain't got no keys?!
5205 */
5206 if (DEBUG_LOCKS_WARN_ON(chain_key != INITIAL_CHAIN_KEY))
5207 return 0;
5208 chain_head = 1;
5209 }
5210
5211 hlock->prev_chain_key = chain_key;
5212 if (separate_irq_context(curr, hlock)) {
5213 chain_key = INITIAL_CHAIN_KEY;
5214 chain_head = 1;
5215 }
5216 chain_key = iterate_chain_key(chain_key, hlock_id(hlock));
5217
5218 if (nest_lock && !__lock_is_held(nest_lock, -1)) {
5219 print_lock_nested_lock_not_held(curr, hlock);
5220 return 0;
5221 }
5222
5223 if (!debug_locks_silent) {
5224 WARN_ON_ONCE(depth && !hlock_class(hlock - 1)->key);
5225 WARN_ON_ONCE(!hlock_class(hlock)->key);
5226 }
5227
5228 if (!validate_chain(curr, hlock, chain_head, chain_key))
5229 return 0;
5230
5231 /* For lock_sync(), we are done here since no actual critical section */
5232 if (hlock->sync)
5233 return 1;
5234
5235 curr->curr_chain_key = chain_key;
5236 curr->lockdep_depth++;
5237 check_chain_key(curr);
5238 #ifdef CONFIG_DEBUG_LOCKDEP
5239 if (unlikely(!debug_locks))
5240 return 0;
5241 #endif
5242 if (unlikely(curr->lockdep_depth >= MAX_LOCK_DEPTH)) {
5243 debug_locks_off();
5244 nbcon_cpu_emergency_enter();
5245 print_lockdep_off("BUG: MAX_LOCK_DEPTH too low!");
5246 printk(KERN_DEBUG "depth: %i max: %lu!\n",
5247 curr->lockdep_depth, MAX_LOCK_DEPTH);
5248
5249 lockdep_print_held_locks(current);
5250 debug_show_all_locks();
5251 dump_stack();
5252 nbcon_cpu_emergency_exit();
5253
5254 return 0;
5255 }
5256
5257 if (unlikely(curr->lockdep_depth > max_lockdep_depth))
5258 max_lockdep_depth = curr->lockdep_depth;
5259
5260 return 1;
5261 }
5262
print_unlock_imbalance_bug(struct task_struct * curr,struct lockdep_map * lock,unsigned long ip)5263 static void print_unlock_imbalance_bug(struct task_struct *curr,
5264 struct lockdep_map *lock,
5265 unsigned long ip)
5266 {
5267 if (!debug_locks_off())
5268 return;
5269 if (debug_locks_silent)
5270 return;
5271
5272 nbcon_cpu_emergency_enter();
5273
5274 pr_warn("\n");
5275 pr_warn("=====================================\n");
5276 pr_warn("WARNING: bad unlock balance detected!\n");
5277 print_kernel_ident();
5278 pr_warn("-------------------------------------\n");
5279 pr_warn("%s/%d is trying to release lock (",
5280 curr->comm, task_pid_nr(curr));
5281 print_lockdep_cache(lock);
5282 pr_cont(") at:\n");
5283 print_ip_sym(KERN_WARNING, ip);
5284 pr_warn("but there are no more locks to release!\n");
5285 pr_warn("\nother info that might help us debug this:\n");
5286 lockdep_print_held_locks(curr);
5287
5288 pr_warn("\nstack backtrace:\n");
5289 dump_stack();
5290
5291 nbcon_cpu_emergency_exit();
5292 }
5293
match_held_lock(const struct held_lock * hlock,const struct lockdep_map * lock)5294 static noinstr int match_held_lock(const struct held_lock *hlock,
5295 const struct lockdep_map *lock)
5296 {
5297 if (hlock->instance == lock)
5298 return 1;
5299
5300 if (hlock->references) {
5301 const struct lock_class *class = lock->class_cache[0];
5302
5303 if (!class)
5304 class = look_up_lock_class(lock, 0);
5305
5306 /*
5307 * If look_up_lock_class() failed to find a class, we're trying
5308 * to test if we hold a lock that has never yet been acquired.
5309 * Clearly if the lock hasn't been acquired _ever_, we're not
5310 * holding it either, so report failure.
5311 */
5312 if (!class)
5313 return 0;
5314
5315 /*
5316 * References, but not a lock we're actually ref-counting?
5317 * State got messed up, follow the sites that change ->references
5318 * and try to make sense of it.
5319 */
5320 if (DEBUG_LOCKS_WARN_ON(!hlock->nest_lock))
5321 return 0;
5322
5323 if (hlock->class_idx == class - lock_classes)
5324 return 1;
5325 }
5326
5327 return 0;
5328 }
5329
5330 /* @depth must not be zero */
find_held_lock(struct task_struct * curr,struct lockdep_map * lock,unsigned int depth,int * idx)5331 static struct held_lock *find_held_lock(struct task_struct *curr,
5332 struct lockdep_map *lock,
5333 unsigned int depth, int *idx)
5334 {
5335 struct held_lock *ret, *hlock, *prev_hlock;
5336 int i;
5337
5338 i = depth - 1;
5339 hlock = curr->held_locks + i;
5340 ret = hlock;
5341 if (match_held_lock(hlock, lock))
5342 goto out;
5343
5344 ret = NULL;
5345 for (i--, prev_hlock = hlock--;
5346 i >= 0;
5347 i--, prev_hlock = hlock--) {
5348 /*
5349 * We must not cross into another context:
5350 */
5351 if (prev_hlock->irq_context != hlock->irq_context) {
5352 ret = NULL;
5353 break;
5354 }
5355 if (match_held_lock(hlock, lock)) {
5356 ret = hlock;
5357 break;
5358 }
5359 }
5360
5361 out:
5362 *idx = i;
5363 return ret;
5364 }
5365
reacquire_held_locks(struct task_struct * curr,unsigned int depth,int idx,unsigned int * merged)5366 static int reacquire_held_locks(struct task_struct *curr, unsigned int depth,
5367 int idx, unsigned int *merged)
5368 {
5369 struct held_lock *hlock;
5370 int first_idx = idx;
5371
5372 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
5373 return 0;
5374
5375 for (hlock = curr->held_locks + idx; idx < depth; idx++, hlock++) {
5376 switch (__lock_acquire(hlock->instance,
5377 hlock_class(hlock)->subclass,
5378 hlock->trylock,
5379 hlock->read, hlock->check,
5380 hlock->hardirqs_off,
5381 hlock->nest_lock, hlock->acquire_ip,
5382 hlock->references, hlock->pin_count, 0)) {
5383 case 0:
5384 return 1;
5385 case 1:
5386 break;
5387 case 2:
5388 *merged += (idx == first_idx);
5389 break;
5390 default:
5391 WARN_ON(1);
5392 return 0;
5393 }
5394 }
5395 return 0;
5396 }
5397
5398 static int
__lock_set_class(struct lockdep_map * lock,const char * name,struct lock_class_key * key,unsigned int subclass,unsigned long ip)5399 __lock_set_class(struct lockdep_map *lock, const char *name,
5400 struct lock_class_key *key, unsigned int subclass,
5401 unsigned long ip)
5402 {
5403 struct task_struct *curr = current;
5404 unsigned int depth, merged = 0;
5405 struct held_lock *hlock;
5406 struct lock_class *class;
5407 int i;
5408
5409 if (unlikely(!debug_locks))
5410 return 0;
5411
5412 depth = curr->lockdep_depth;
5413 /*
5414 * This function is about (re)setting the class of a held lock,
5415 * yet we're not actually holding any locks. Naughty user!
5416 */
5417 if (DEBUG_LOCKS_WARN_ON(!depth))
5418 return 0;
5419
5420 hlock = find_held_lock(curr, lock, depth, &i);
5421 if (!hlock) {
5422 print_unlock_imbalance_bug(curr, lock, ip);
5423 return 0;
5424 }
5425
5426 lockdep_init_map_type(lock, name, key, 0,
5427 lock->wait_type_inner,
5428 lock->wait_type_outer,
5429 lock->lock_type);
5430 class = register_lock_class(lock, subclass, 0);
5431 hlock->class_idx = class - lock_classes;
5432
5433 curr->lockdep_depth = i;
5434 curr->curr_chain_key = hlock->prev_chain_key;
5435
5436 if (reacquire_held_locks(curr, depth, i, &merged))
5437 return 0;
5438
5439 /*
5440 * I took it apart and put it back together again, except now I have
5441 * these 'spare' parts.. where shall I put them.
5442 */
5443 if (DEBUG_LOCKS_WARN_ON(curr->lockdep_depth != depth - merged))
5444 return 0;
5445 return 1;
5446 }
5447
__lock_downgrade(struct lockdep_map * lock,unsigned long ip)5448 static int __lock_downgrade(struct lockdep_map *lock, unsigned long ip)
5449 {
5450 struct task_struct *curr = current;
5451 unsigned int depth, merged = 0;
5452 struct held_lock *hlock;
5453 int i;
5454
5455 if (unlikely(!debug_locks))
5456 return 0;
5457
5458 depth = curr->lockdep_depth;
5459 /*
5460 * This function is about (re)setting the class of a held lock,
5461 * yet we're not actually holding any locks. Naughty user!
5462 */
5463 if (DEBUG_LOCKS_WARN_ON(!depth))
5464 return 0;
5465
5466 hlock = find_held_lock(curr, lock, depth, &i);
5467 if (!hlock) {
5468 print_unlock_imbalance_bug(curr, lock, ip);
5469 return 0;
5470 }
5471
5472 curr->lockdep_depth = i;
5473 curr->curr_chain_key = hlock->prev_chain_key;
5474
5475 WARN(hlock->read, "downgrading a read lock");
5476 hlock->read = 1;
5477 hlock->acquire_ip = ip;
5478
5479 if (reacquire_held_locks(curr, depth, i, &merged))
5480 return 0;
5481
5482 /* Merging can't happen with unchanged classes.. */
5483 if (DEBUG_LOCKS_WARN_ON(merged))
5484 return 0;
5485
5486 /*
5487 * I took it apart and put it back together again, except now I have
5488 * these 'spare' parts.. where shall I put them.
5489 */
5490 if (DEBUG_LOCKS_WARN_ON(curr->lockdep_depth != depth))
5491 return 0;
5492
5493 return 1;
5494 }
5495
5496 /*
5497 * Remove the lock from the list of currently held locks - this gets
5498 * called on mutex_unlock()/spin_unlock*() (or on a failed
5499 * mutex_lock_interruptible()).
5500 */
5501 static int
__lock_release(struct lockdep_map * lock,unsigned long ip)5502 __lock_release(struct lockdep_map *lock, unsigned long ip)
5503 {
5504 struct task_struct *curr = current;
5505 unsigned int depth, merged = 1;
5506 struct held_lock *hlock;
5507 int i;
5508
5509 if (unlikely(!debug_locks))
5510 return 0;
5511
5512 depth = curr->lockdep_depth;
5513 /*
5514 * So we're all set to release this lock.. wait what lock? We don't
5515 * own any locks, you've been drinking again?
5516 */
5517 if (depth <= 0) {
5518 print_unlock_imbalance_bug(curr, lock, ip);
5519 return 0;
5520 }
5521
5522 /*
5523 * Check whether the lock exists in the current stack
5524 * of held locks:
5525 */
5526 hlock = find_held_lock(curr, lock, depth, &i);
5527 if (!hlock) {
5528 print_unlock_imbalance_bug(curr, lock, ip);
5529 return 0;
5530 }
5531
5532 if (hlock->instance == lock)
5533 lock_release_holdtime(hlock);
5534
5535 WARN(hlock->pin_count, "releasing a pinned lock\n");
5536
5537 if (hlock->references) {
5538 hlock->references--;
5539 if (hlock->references) {
5540 /*
5541 * We had, and after removing one, still have
5542 * references, the current lock stack is still
5543 * valid. We're done!
5544 */
5545 return 1;
5546 }
5547 }
5548
5549 /*
5550 * We have the right lock to unlock, 'hlock' points to it.
5551 * Now we remove it from the stack, and add back the other
5552 * entries (if any), recalculating the hash along the way:
5553 */
5554
5555 curr->lockdep_depth = i;
5556 curr->curr_chain_key = hlock->prev_chain_key;
5557
5558 /*
5559 * The most likely case is when the unlock is on the innermost
5560 * lock. In this case, we are done!
5561 */
5562 if (i == depth-1)
5563 return 1;
5564
5565 if (reacquire_held_locks(curr, depth, i + 1, &merged))
5566 return 0;
5567
5568 /*
5569 * We had N bottles of beer on the wall, we drank one, but now
5570 * there's not N-1 bottles of beer left on the wall...
5571 * Pouring two of the bottles together is acceptable.
5572 */
5573 DEBUG_LOCKS_WARN_ON(curr->lockdep_depth != depth - merged);
5574
5575 /*
5576 * Since reacquire_held_locks() would have called check_chain_key()
5577 * indirectly via __lock_acquire(), we don't need to do it again
5578 * on return.
5579 */
5580 return 0;
5581 }
5582
5583 static __always_inline
__lock_is_held(const struct lockdep_map * lock,int read)5584 int __lock_is_held(const struct lockdep_map *lock, int read)
5585 {
5586 struct task_struct *curr = current;
5587 int i;
5588
5589 for (i = 0; i < curr->lockdep_depth; i++) {
5590 struct held_lock *hlock = curr->held_locks + i;
5591
5592 if (match_held_lock(hlock, lock)) {
5593 if (read == -1 || !!hlock->read == read)
5594 return LOCK_STATE_HELD;
5595
5596 return LOCK_STATE_NOT_HELD;
5597 }
5598 }
5599
5600 return LOCK_STATE_NOT_HELD;
5601 }
5602
__lock_pin_lock(struct lockdep_map * lock)5603 static struct pin_cookie __lock_pin_lock(struct lockdep_map *lock)
5604 {
5605 struct pin_cookie cookie = NIL_COOKIE;
5606 struct task_struct *curr = current;
5607 int i;
5608
5609 if (unlikely(!debug_locks))
5610 return cookie;
5611
5612 for (i = 0; i < curr->lockdep_depth; i++) {
5613 struct held_lock *hlock = curr->held_locks + i;
5614
5615 if (match_held_lock(hlock, lock)) {
5616 /*
5617 * Grab 16bits of randomness; this is sufficient to not
5618 * be guessable and still allows some pin nesting in
5619 * our u32 pin_count.
5620 */
5621 cookie.val = 1 + (sched_clock() & 0xffff);
5622 hlock->pin_count += cookie.val;
5623 return cookie;
5624 }
5625 }
5626
5627 WARN(1, "pinning an unheld lock\n");
5628 return cookie;
5629 }
5630
__lock_repin_lock(struct lockdep_map * lock,struct pin_cookie cookie)5631 static void __lock_repin_lock(struct lockdep_map *lock, struct pin_cookie cookie)
5632 {
5633 struct task_struct *curr = current;
5634 int i;
5635
5636 if (unlikely(!debug_locks))
5637 return;
5638
5639 for (i = 0; i < curr->lockdep_depth; i++) {
5640 struct held_lock *hlock = curr->held_locks + i;
5641
5642 if (match_held_lock(hlock, lock)) {
5643 hlock->pin_count += cookie.val;
5644 return;
5645 }
5646 }
5647
5648 WARN(1, "pinning an unheld lock\n");
5649 }
5650
__lock_unpin_lock(struct lockdep_map * lock,struct pin_cookie cookie)5651 static void __lock_unpin_lock(struct lockdep_map *lock, struct pin_cookie cookie)
5652 {
5653 struct task_struct *curr = current;
5654 int i;
5655
5656 if (unlikely(!debug_locks))
5657 return;
5658
5659 for (i = 0; i < curr->lockdep_depth; i++) {
5660 struct held_lock *hlock = curr->held_locks + i;
5661
5662 if (match_held_lock(hlock, lock)) {
5663 if (WARN(!hlock->pin_count, "unpinning an unpinned lock\n"))
5664 return;
5665
5666 hlock->pin_count -= cookie.val;
5667
5668 if (WARN((int)hlock->pin_count < 0, "pin count corrupted\n"))
5669 hlock->pin_count = 0;
5670
5671 return;
5672 }
5673 }
5674
5675 WARN(1, "unpinning an unheld lock\n");
5676 }
5677
5678 /*
5679 * Check whether we follow the irq-flags state precisely:
5680 */
check_flags(unsigned long flags)5681 static noinstr void check_flags(unsigned long flags)
5682 {
5683 #if defined(CONFIG_PROVE_LOCKING) && defined(CONFIG_DEBUG_LOCKDEP)
5684 if (!debug_locks)
5685 return;
5686
5687 /* Get the warning out.. */
5688 instrumentation_begin();
5689
5690 if (irqs_disabled_flags(flags)) {
5691 if (DEBUG_LOCKS_WARN_ON(lockdep_hardirqs_enabled())) {
5692 printk("possible reason: unannotated irqs-off.\n");
5693 }
5694 } else {
5695 if (DEBUG_LOCKS_WARN_ON(!lockdep_hardirqs_enabled())) {
5696 printk("possible reason: unannotated irqs-on.\n");
5697 }
5698 }
5699
5700 #ifndef CONFIG_PREEMPT_RT
5701 /*
5702 * We dont accurately track softirq state in e.g.
5703 * hardirq contexts (such as on 4KSTACKS), so only
5704 * check if not in hardirq contexts:
5705 */
5706 if (!hardirq_count()) {
5707 if (softirq_count()) {
5708 /* like the above, but with softirqs */
5709 DEBUG_LOCKS_WARN_ON(current->softirqs_enabled);
5710 } else {
5711 /* lick the above, does it taste good? */
5712 DEBUG_LOCKS_WARN_ON(!current->softirqs_enabled);
5713 }
5714 }
5715 #endif
5716
5717 if (!debug_locks)
5718 print_irqtrace_events(current);
5719
5720 instrumentation_end();
5721 #endif
5722 }
5723
lock_set_class(struct lockdep_map * lock,const char * name,struct lock_class_key * key,unsigned int subclass,unsigned long ip)5724 void lock_set_class(struct lockdep_map *lock, const char *name,
5725 struct lock_class_key *key, unsigned int subclass,
5726 unsigned long ip)
5727 {
5728 unsigned long flags;
5729
5730 if (unlikely(!lockdep_enabled()))
5731 return;
5732
5733 raw_local_irq_save(flags);
5734 lockdep_recursion_inc();
5735 check_flags(flags);
5736 if (__lock_set_class(lock, name, key, subclass, ip))
5737 check_chain_key(current);
5738 lockdep_recursion_finish();
5739 raw_local_irq_restore(flags);
5740 }
5741 EXPORT_SYMBOL_GPL(lock_set_class);
5742
lock_downgrade(struct lockdep_map * lock,unsigned long ip)5743 void lock_downgrade(struct lockdep_map *lock, unsigned long ip)
5744 {
5745 unsigned long flags;
5746
5747 if (unlikely(!lockdep_enabled()))
5748 return;
5749
5750 raw_local_irq_save(flags);
5751 lockdep_recursion_inc();
5752 check_flags(flags);
5753 if (__lock_downgrade(lock, ip))
5754 check_chain_key(current);
5755 lockdep_recursion_finish();
5756 raw_local_irq_restore(flags);
5757 }
5758 EXPORT_SYMBOL_GPL(lock_downgrade);
5759
5760 /* NMI context !!! */
verify_lock_unused(struct lockdep_map * lock,struct held_lock * hlock,int subclass)5761 static void verify_lock_unused(struct lockdep_map *lock, struct held_lock *hlock, int subclass)
5762 {
5763 #ifdef CONFIG_PROVE_LOCKING
5764 struct lock_class *class = look_up_lock_class(lock, subclass);
5765 unsigned long mask = LOCKF_USED;
5766
5767 /* if it doesn't have a class (yet), it certainly hasn't been used yet */
5768 if (!class)
5769 return;
5770
5771 /*
5772 * READ locks only conflict with USED, such that if we only ever use
5773 * READ locks, there is no deadlock possible -- RCU.
5774 */
5775 if (!hlock->read)
5776 mask |= LOCKF_USED_READ;
5777
5778 if (!(class->usage_mask & mask))
5779 return;
5780
5781 hlock->class_idx = class - lock_classes;
5782
5783 print_usage_bug(current, hlock, LOCK_USED, LOCK_USAGE_STATES);
5784 #endif
5785 }
5786
lockdep_nmi(void)5787 static bool lockdep_nmi(void)
5788 {
5789 if (raw_cpu_read(lockdep_recursion))
5790 return false;
5791
5792 if (!in_nmi())
5793 return false;
5794
5795 return true;
5796 }
5797
5798 /*
5799 * read_lock() is recursive if:
5800 * 1. We force lockdep think this way in selftests or
5801 * 2. The implementation is not queued read/write lock or
5802 * 3. The locker is at an in_interrupt() context.
5803 */
read_lock_is_recursive(void)5804 bool read_lock_is_recursive(void)
5805 {
5806 return force_read_lock_recursive ||
5807 !IS_ENABLED(CONFIG_QUEUED_RWLOCKS) ||
5808 in_interrupt();
5809 }
5810 EXPORT_SYMBOL_GPL(read_lock_is_recursive);
5811
5812 /*
5813 * We are not always called with irqs disabled - do that here,
5814 * and also avoid lockdep recursion:
5815 */
lock_acquire(struct lockdep_map * lock,unsigned int subclass,int trylock,int read,int check,struct lockdep_map * nest_lock,unsigned long ip)5816 void lock_acquire(struct lockdep_map *lock, unsigned int subclass,
5817 int trylock, int read, int check,
5818 struct lockdep_map *nest_lock, unsigned long ip)
5819 {
5820 unsigned long flags;
5821
5822 trace_lock_acquire(lock, subclass, trylock, read, check, nest_lock, ip);
5823
5824 if (!debug_locks)
5825 return;
5826
5827 if (unlikely(!lockdep_enabled())) {
5828 /* XXX allow trylock from NMI ?!? */
5829 if (lockdep_nmi() && !trylock) {
5830 struct held_lock hlock;
5831
5832 hlock.acquire_ip = ip;
5833 hlock.instance = lock;
5834 hlock.nest_lock = nest_lock;
5835 hlock.irq_context = 2; // XXX
5836 hlock.trylock = trylock;
5837 hlock.read = read;
5838 hlock.check = check;
5839 hlock.hardirqs_off = true;
5840 hlock.references = 0;
5841
5842 verify_lock_unused(lock, &hlock, subclass);
5843 }
5844 return;
5845 }
5846
5847 raw_local_irq_save(flags);
5848 check_flags(flags);
5849
5850 lockdep_recursion_inc();
5851 __lock_acquire(lock, subclass, trylock, read, check,
5852 irqs_disabled_flags(flags), nest_lock, ip, 0, 0, 0);
5853 lockdep_recursion_finish();
5854 raw_local_irq_restore(flags);
5855 }
5856 EXPORT_SYMBOL_GPL(lock_acquire);
5857
lock_release(struct lockdep_map * lock,unsigned long ip)5858 void lock_release(struct lockdep_map *lock, unsigned long ip)
5859 {
5860 unsigned long flags;
5861
5862 trace_lock_release(lock, ip);
5863
5864 if (unlikely(!lockdep_enabled() ||
5865 lock->key == &__lockdep_no_track__))
5866 return;
5867
5868 raw_local_irq_save(flags);
5869 check_flags(flags);
5870
5871 lockdep_recursion_inc();
5872 if (__lock_release(lock, ip))
5873 check_chain_key(current);
5874 lockdep_recursion_finish();
5875 raw_local_irq_restore(flags);
5876 }
5877 EXPORT_SYMBOL_GPL(lock_release);
5878
5879 /*
5880 * lock_sync() - A special annotation for synchronize_{s,}rcu()-like API.
5881 *
5882 * No actual critical section is created by the APIs annotated with this: these
5883 * APIs are used to wait for one or multiple critical sections (on other CPUs
5884 * or threads), and it means that calling these APIs inside these critical
5885 * sections is potential deadlock.
5886 */
lock_sync(struct lockdep_map * lock,unsigned subclass,int read,int check,struct lockdep_map * nest_lock,unsigned long ip)5887 void lock_sync(struct lockdep_map *lock, unsigned subclass, int read,
5888 int check, struct lockdep_map *nest_lock, unsigned long ip)
5889 {
5890 unsigned long flags;
5891
5892 if (unlikely(!lockdep_enabled()))
5893 return;
5894
5895 raw_local_irq_save(flags);
5896 check_flags(flags);
5897
5898 lockdep_recursion_inc();
5899 __lock_acquire(lock, subclass, 0, read, check,
5900 irqs_disabled_flags(flags), nest_lock, ip, 0, 0, 1);
5901 check_chain_key(current);
5902 lockdep_recursion_finish();
5903 raw_local_irq_restore(flags);
5904 }
5905 EXPORT_SYMBOL_GPL(lock_sync);
5906
lock_is_held_type(const struct lockdep_map * lock,int read)5907 noinstr int lock_is_held_type(const struct lockdep_map *lock, int read)
5908 {
5909 unsigned long flags;
5910 int ret = LOCK_STATE_NOT_HELD;
5911
5912 /*
5913 * Avoid false negative lockdep_assert_held() and
5914 * lockdep_assert_not_held().
5915 */
5916 if (unlikely(!lockdep_enabled()))
5917 return LOCK_STATE_UNKNOWN;
5918
5919 raw_local_irq_save(flags);
5920 check_flags(flags);
5921
5922 lockdep_recursion_inc();
5923 ret = __lock_is_held(lock, read);
5924 lockdep_recursion_finish();
5925 raw_local_irq_restore(flags);
5926
5927 return ret;
5928 }
5929 EXPORT_SYMBOL_GPL(lock_is_held_type);
5930 NOKPROBE_SYMBOL(lock_is_held_type);
5931
lock_pin_lock(struct lockdep_map * lock)5932 struct pin_cookie lock_pin_lock(struct lockdep_map *lock)
5933 {
5934 struct pin_cookie cookie = NIL_COOKIE;
5935 unsigned long flags;
5936
5937 if (unlikely(!lockdep_enabled()))
5938 return cookie;
5939
5940 raw_local_irq_save(flags);
5941 check_flags(flags);
5942
5943 lockdep_recursion_inc();
5944 cookie = __lock_pin_lock(lock);
5945 lockdep_recursion_finish();
5946 raw_local_irq_restore(flags);
5947
5948 return cookie;
5949 }
5950 EXPORT_SYMBOL_GPL(lock_pin_lock);
5951
lock_repin_lock(struct lockdep_map * lock,struct pin_cookie cookie)5952 void lock_repin_lock(struct lockdep_map *lock, struct pin_cookie cookie)
5953 {
5954 unsigned long flags;
5955
5956 if (unlikely(!lockdep_enabled()))
5957 return;
5958
5959 raw_local_irq_save(flags);
5960 check_flags(flags);
5961
5962 lockdep_recursion_inc();
5963 __lock_repin_lock(lock, cookie);
5964 lockdep_recursion_finish();
5965 raw_local_irq_restore(flags);
5966 }
5967 EXPORT_SYMBOL_GPL(lock_repin_lock);
5968
lock_unpin_lock(struct lockdep_map * lock,struct pin_cookie cookie)5969 void lock_unpin_lock(struct lockdep_map *lock, struct pin_cookie cookie)
5970 {
5971 unsigned long flags;
5972
5973 if (unlikely(!lockdep_enabled()))
5974 return;
5975
5976 raw_local_irq_save(flags);
5977 check_flags(flags);
5978
5979 lockdep_recursion_inc();
5980 __lock_unpin_lock(lock, cookie);
5981 lockdep_recursion_finish();
5982 raw_local_irq_restore(flags);
5983 }
5984 EXPORT_SYMBOL_GPL(lock_unpin_lock);
5985
5986 #ifdef CONFIG_LOCK_STAT
print_lock_contention_bug(struct task_struct * curr,struct lockdep_map * lock,unsigned long ip)5987 static void print_lock_contention_bug(struct task_struct *curr,
5988 struct lockdep_map *lock,
5989 unsigned long ip)
5990 {
5991 if (!debug_locks_off())
5992 return;
5993 if (debug_locks_silent)
5994 return;
5995
5996 nbcon_cpu_emergency_enter();
5997
5998 pr_warn("\n");
5999 pr_warn("=================================\n");
6000 pr_warn("WARNING: bad contention detected!\n");
6001 print_kernel_ident();
6002 pr_warn("---------------------------------\n");
6003 pr_warn("%s/%d is trying to contend lock (",
6004 curr->comm, task_pid_nr(curr));
6005 print_lockdep_cache(lock);
6006 pr_cont(") at:\n");
6007 print_ip_sym(KERN_WARNING, ip);
6008 pr_warn("but there are no locks held!\n");
6009 pr_warn("\nother info that might help us debug this:\n");
6010 lockdep_print_held_locks(curr);
6011
6012 pr_warn("\nstack backtrace:\n");
6013 dump_stack();
6014
6015 nbcon_cpu_emergency_exit();
6016 }
6017
6018 static void
__lock_contended(struct lockdep_map * lock,unsigned long ip)6019 __lock_contended(struct lockdep_map *lock, unsigned long ip)
6020 {
6021 struct task_struct *curr = current;
6022 struct held_lock *hlock;
6023 struct lock_class_stats *stats;
6024 unsigned int depth;
6025 int i, contention_point, contending_point;
6026
6027 depth = curr->lockdep_depth;
6028 /*
6029 * Whee, we contended on this lock, except it seems we're not
6030 * actually trying to acquire anything much at all..
6031 */
6032 if (DEBUG_LOCKS_WARN_ON(!depth))
6033 return;
6034
6035 if (unlikely(lock->key == &__lockdep_no_track__))
6036 return;
6037
6038 hlock = find_held_lock(curr, lock, depth, &i);
6039 if (!hlock) {
6040 print_lock_contention_bug(curr, lock, ip);
6041 return;
6042 }
6043
6044 if (hlock->instance != lock)
6045 return;
6046
6047 hlock->waittime_stamp = lockstat_clock();
6048
6049 contention_point = lock_point(hlock_class(hlock)->contention_point, ip);
6050 contending_point = lock_point(hlock_class(hlock)->contending_point,
6051 lock->ip);
6052
6053 stats = get_lock_stats(hlock_class(hlock));
6054 if (contention_point < LOCKSTAT_POINTS)
6055 stats->contention_point[contention_point]++;
6056 if (contending_point < LOCKSTAT_POINTS)
6057 stats->contending_point[contending_point]++;
6058 if (lock->cpu != smp_processor_id())
6059 stats->bounces[bounce_contended + !!hlock->read]++;
6060 }
6061
6062 static void
__lock_acquired(struct lockdep_map * lock,unsigned long ip)6063 __lock_acquired(struct lockdep_map *lock, unsigned long ip)
6064 {
6065 struct task_struct *curr = current;
6066 struct held_lock *hlock;
6067 struct lock_class_stats *stats;
6068 unsigned int depth;
6069 u64 now, waittime = 0;
6070 int i, cpu;
6071
6072 depth = curr->lockdep_depth;
6073 /*
6074 * Yay, we acquired ownership of this lock we didn't try to
6075 * acquire, how the heck did that happen?
6076 */
6077 if (DEBUG_LOCKS_WARN_ON(!depth))
6078 return;
6079
6080 if (unlikely(lock->key == &__lockdep_no_track__))
6081 return;
6082
6083 hlock = find_held_lock(curr, lock, depth, &i);
6084 if (!hlock) {
6085 print_lock_contention_bug(curr, lock, _RET_IP_);
6086 return;
6087 }
6088
6089 if (hlock->instance != lock)
6090 return;
6091
6092 cpu = smp_processor_id();
6093 if (hlock->waittime_stamp) {
6094 now = lockstat_clock();
6095 waittime = now - hlock->waittime_stamp;
6096 hlock->holdtime_stamp = now;
6097 }
6098
6099 stats = get_lock_stats(hlock_class(hlock));
6100 if (waittime) {
6101 if (hlock->read)
6102 lock_time_inc(&stats->read_waittime, waittime);
6103 else
6104 lock_time_inc(&stats->write_waittime, waittime);
6105 }
6106 if (lock->cpu != cpu)
6107 stats->bounces[bounce_acquired + !!hlock->read]++;
6108
6109 lock->cpu = cpu;
6110 lock->ip = ip;
6111 }
6112
lock_contended(struct lockdep_map * lock,unsigned long ip)6113 void lock_contended(struct lockdep_map *lock, unsigned long ip)
6114 {
6115 unsigned long flags;
6116
6117 trace_lock_contended(lock, ip);
6118
6119 if (unlikely(!lock_stat || !lockdep_enabled()))
6120 return;
6121
6122 raw_local_irq_save(flags);
6123 check_flags(flags);
6124 lockdep_recursion_inc();
6125 __lock_contended(lock, ip);
6126 lockdep_recursion_finish();
6127 raw_local_irq_restore(flags);
6128 }
6129 EXPORT_SYMBOL_GPL(lock_contended);
6130
lock_acquired(struct lockdep_map * lock,unsigned long ip)6131 void lock_acquired(struct lockdep_map *lock, unsigned long ip)
6132 {
6133 unsigned long flags;
6134
6135 trace_lock_acquired(lock, ip);
6136
6137 if (unlikely(!lock_stat || !lockdep_enabled()))
6138 return;
6139
6140 raw_local_irq_save(flags);
6141 check_flags(flags);
6142 lockdep_recursion_inc();
6143 __lock_acquired(lock, ip);
6144 lockdep_recursion_finish();
6145 raw_local_irq_restore(flags);
6146 }
6147 EXPORT_SYMBOL_GPL(lock_acquired);
6148 #endif
6149
6150 /*
6151 * Used by the testsuite, sanitize the validator state
6152 * after a simulated failure:
6153 */
6154
lockdep_reset(void)6155 void lockdep_reset(void)
6156 {
6157 unsigned long flags;
6158 int i;
6159
6160 raw_local_irq_save(flags);
6161 lockdep_init_task(current);
6162 memset(current->held_locks, 0, MAX_LOCK_DEPTH*sizeof(struct held_lock));
6163 nr_hardirq_chains = 0;
6164 nr_softirq_chains = 0;
6165 nr_process_chains = 0;
6166 debug_locks = 1;
6167 for (i = 0; i < CHAINHASH_SIZE; i++)
6168 INIT_HLIST_HEAD(chainhash_table + i);
6169 raw_local_irq_restore(flags);
6170 }
6171
6172 /* Remove a class from a lock chain. Must be called with the graph lock held. */
remove_class_from_lock_chain(struct pending_free * pf,struct lock_chain * chain,struct lock_class * class)6173 static void remove_class_from_lock_chain(struct pending_free *pf,
6174 struct lock_chain *chain,
6175 struct lock_class *class)
6176 {
6177 #ifdef CONFIG_PROVE_LOCKING
6178 int i;
6179
6180 for (i = chain->base; i < chain->base + chain->depth; i++) {
6181 if (chain_hlock_class_idx(chain_hlocks[i]) != class - lock_classes)
6182 continue;
6183 /*
6184 * Each lock class occurs at most once in a lock chain so once
6185 * we found a match we can break out of this loop.
6186 */
6187 goto free_lock_chain;
6188 }
6189 /* Since the chain has not been modified, return. */
6190 return;
6191
6192 free_lock_chain:
6193 free_chain_hlocks(chain->base, chain->depth);
6194 /* Overwrite the chain key for concurrent RCU readers. */
6195 WRITE_ONCE(chain->chain_key, INITIAL_CHAIN_KEY);
6196 dec_chains(chain->irq_context);
6197
6198 /*
6199 * Note: calling hlist_del_rcu() from inside a
6200 * hlist_for_each_entry_rcu() loop is safe.
6201 */
6202 hlist_del_rcu(&chain->entry);
6203 __set_bit(chain - lock_chains, pf->lock_chains_being_freed);
6204 nr_zapped_lock_chains++;
6205 #endif
6206 }
6207
6208 /* Must be called with the graph lock held. */
remove_class_from_lock_chains(struct pending_free * pf,struct lock_class * class)6209 static void remove_class_from_lock_chains(struct pending_free *pf,
6210 struct lock_class *class)
6211 {
6212 struct lock_chain *chain;
6213 struct hlist_head *head;
6214 int i;
6215
6216 for (i = 0; i < ARRAY_SIZE(chainhash_table); i++) {
6217 head = chainhash_table + i;
6218 hlist_for_each_entry_rcu(chain, head, entry) {
6219 remove_class_from_lock_chain(pf, chain, class);
6220 }
6221 }
6222 }
6223
6224 /*
6225 * Remove all references to a lock class. The caller must hold the graph lock.
6226 */
zap_class(struct pending_free * pf,struct lock_class * class)6227 static void zap_class(struct pending_free *pf, struct lock_class *class)
6228 {
6229 struct lock_list *entry;
6230 int i;
6231
6232 WARN_ON_ONCE(!class->key);
6233
6234 /*
6235 * Remove all dependencies this lock is
6236 * involved in:
6237 */
6238 for_each_set_bit(i, list_entries_in_use, ARRAY_SIZE(list_entries)) {
6239 entry = list_entries + i;
6240 if (entry->class != class && entry->links_to != class)
6241 continue;
6242 __clear_bit(i, list_entries_in_use);
6243 nr_list_entries--;
6244 list_del_rcu(&entry->entry);
6245 }
6246 if (list_empty(&class->locks_after) &&
6247 list_empty(&class->locks_before)) {
6248 list_move_tail(&class->lock_entry, &pf->zapped);
6249 hlist_del_rcu(&class->hash_entry);
6250 WRITE_ONCE(class->key, NULL);
6251 WRITE_ONCE(class->name, NULL);
6252 /* Class allocated but not used, -1 in nr_unused_locks */
6253 if (class->usage_mask == 0)
6254 debug_atomic_dec(nr_unused_locks);
6255 nr_lock_classes--;
6256 __clear_bit(class - lock_classes, lock_classes_in_use);
6257 if (class - lock_classes == max_lock_class_idx)
6258 max_lock_class_idx--;
6259 } else {
6260 WARN_ONCE(true, "%s() failed for class %s\n", __func__,
6261 class->name);
6262 }
6263
6264 remove_class_from_lock_chains(pf, class);
6265 nr_zapped_classes++;
6266 }
6267
reinit_class(struct lock_class * class)6268 static void reinit_class(struct lock_class *class)
6269 {
6270 WARN_ON_ONCE(!class->lock_entry.next);
6271 WARN_ON_ONCE(!list_empty(&class->locks_after));
6272 WARN_ON_ONCE(!list_empty(&class->locks_before));
6273 memset_startat(class, 0, key);
6274 WARN_ON_ONCE(!class->lock_entry.next);
6275 WARN_ON_ONCE(!list_empty(&class->locks_after));
6276 WARN_ON_ONCE(!list_empty(&class->locks_before));
6277 }
6278
within(const void * addr,void * start,unsigned long size)6279 static inline int within(const void *addr, void *start, unsigned long size)
6280 {
6281 return addr >= start && addr < start + size;
6282 }
6283
inside_selftest(void)6284 static bool inside_selftest(void)
6285 {
6286 return current == lockdep_selftest_task_struct;
6287 }
6288
6289 /* The caller must hold the graph lock. */
get_pending_free(void)6290 static struct pending_free *get_pending_free(void)
6291 {
6292 return delayed_free.pf + delayed_free.index;
6293 }
6294
6295 static void free_zapped_rcu(struct rcu_head *cb);
6296
6297 /*
6298 * See if we need to queue an RCU callback, must called with
6299 * the lockdep lock held, returns false if either we don't have
6300 * any pending free or the callback is already scheduled.
6301 * Otherwise, a call_rcu() must follow this function call.
6302 */
prepare_call_rcu_zapped(struct pending_free * pf)6303 static bool prepare_call_rcu_zapped(struct pending_free *pf)
6304 {
6305 WARN_ON_ONCE(inside_selftest());
6306
6307 if (list_empty(&pf->zapped))
6308 return false;
6309
6310 if (delayed_free.scheduled)
6311 return false;
6312
6313 delayed_free.scheduled = true;
6314
6315 WARN_ON_ONCE(delayed_free.pf + delayed_free.index != pf);
6316 delayed_free.index ^= 1;
6317
6318 return true;
6319 }
6320
6321 /* The caller must hold the graph lock. May be called from RCU context. */
__free_zapped_classes(struct pending_free * pf)6322 static void __free_zapped_classes(struct pending_free *pf)
6323 {
6324 struct lock_class *class;
6325
6326 check_data_structures();
6327
6328 list_for_each_entry(class, &pf->zapped, lock_entry)
6329 reinit_class(class);
6330
6331 list_splice_init(&pf->zapped, &free_lock_classes);
6332
6333 #ifdef CONFIG_PROVE_LOCKING
6334 bitmap_andnot(lock_chains_in_use, lock_chains_in_use,
6335 pf->lock_chains_being_freed, ARRAY_SIZE(lock_chains));
6336 bitmap_clear(pf->lock_chains_being_freed, 0, ARRAY_SIZE(lock_chains));
6337 #endif
6338 }
6339
free_zapped_rcu(struct rcu_head * ch)6340 static void free_zapped_rcu(struct rcu_head *ch)
6341 {
6342 struct pending_free *pf;
6343 unsigned long flags;
6344 bool need_callback;
6345
6346 if (WARN_ON_ONCE(ch != &delayed_free.rcu_head))
6347 return;
6348
6349 raw_local_irq_save(flags);
6350 lockdep_lock();
6351
6352 /* closed head */
6353 pf = delayed_free.pf + (delayed_free.index ^ 1);
6354 __free_zapped_classes(pf);
6355 delayed_free.scheduled = false;
6356 need_callback =
6357 prepare_call_rcu_zapped(delayed_free.pf + delayed_free.index);
6358 lockdep_unlock();
6359 raw_local_irq_restore(flags);
6360
6361 /*
6362 * If there's pending free and its callback has not been scheduled,
6363 * queue an RCU callback.
6364 */
6365 if (need_callback)
6366 call_rcu(&delayed_free.rcu_head, free_zapped_rcu);
6367
6368 }
6369
6370 /*
6371 * Remove all lock classes from the class hash table and from the
6372 * all_lock_classes list whose key or name is in the address range [start,
6373 * start + size). Move these lock classes to the zapped_classes list. Must
6374 * be called with the graph lock held.
6375 */
__lockdep_free_key_range(struct pending_free * pf,void * start,unsigned long size)6376 static void __lockdep_free_key_range(struct pending_free *pf, void *start,
6377 unsigned long size)
6378 {
6379 struct lock_class *class;
6380 struct hlist_head *head;
6381 int i;
6382
6383 /* Unhash all classes that were created by a module. */
6384 for (i = 0; i < CLASSHASH_SIZE; i++) {
6385 head = classhash_table + i;
6386 hlist_for_each_entry_rcu(class, head, hash_entry) {
6387 if (!within(class->key, start, size) &&
6388 !within(class->name, start, size))
6389 continue;
6390 zap_class(pf, class);
6391 }
6392 }
6393 }
6394
6395 /*
6396 * Used in module.c to remove lock classes from memory that is going to be
6397 * freed; and possibly re-used by other modules.
6398 *
6399 * We will have had one synchronize_rcu() before getting here, so we're
6400 * guaranteed nobody will look up these exact classes -- they're properly dead
6401 * but still allocated.
6402 */
lockdep_free_key_range_reg(void * start,unsigned long size)6403 static void lockdep_free_key_range_reg(void *start, unsigned long size)
6404 {
6405 struct pending_free *pf;
6406 unsigned long flags;
6407 bool need_callback;
6408
6409 init_data_structures_once();
6410
6411 raw_local_irq_save(flags);
6412 lockdep_lock();
6413 pf = get_pending_free();
6414 __lockdep_free_key_range(pf, start, size);
6415 need_callback = prepare_call_rcu_zapped(pf);
6416 lockdep_unlock();
6417 raw_local_irq_restore(flags);
6418 if (need_callback)
6419 call_rcu(&delayed_free.rcu_head, free_zapped_rcu);
6420 /*
6421 * Wait for any possible iterators from look_up_lock_class() to pass
6422 * before continuing to free the memory they refer to.
6423 */
6424 synchronize_rcu();
6425 }
6426
6427 /*
6428 * Free all lockdep keys in the range [start, start+size). Does not sleep.
6429 * Ignores debug_locks. Must only be used by the lockdep selftests.
6430 */
lockdep_free_key_range_imm(void * start,unsigned long size)6431 static void lockdep_free_key_range_imm(void *start, unsigned long size)
6432 {
6433 struct pending_free *pf = delayed_free.pf;
6434 unsigned long flags;
6435
6436 init_data_structures_once();
6437
6438 raw_local_irq_save(flags);
6439 lockdep_lock();
6440 __lockdep_free_key_range(pf, start, size);
6441 __free_zapped_classes(pf);
6442 lockdep_unlock();
6443 raw_local_irq_restore(flags);
6444 }
6445
lockdep_free_key_range(void * start,unsigned long size)6446 void lockdep_free_key_range(void *start, unsigned long size)
6447 {
6448 init_data_structures_once();
6449
6450 if (inside_selftest())
6451 lockdep_free_key_range_imm(start, size);
6452 else
6453 lockdep_free_key_range_reg(start, size);
6454 }
6455
6456 /*
6457 * Check whether any element of the @lock->class_cache[] array refers to a
6458 * registered lock class. The caller must hold either the graph lock or the
6459 * RCU read lock.
6460 */
lock_class_cache_is_registered(struct lockdep_map * lock)6461 static bool lock_class_cache_is_registered(struct lockdep_map *lock)
6462 {
6463 struct lock_class *class;
6464 struct hlist_head *head;
6465 int i, j;
6466
6467 for (i = 0; i < CLASSHASH_SIZE; i++) {
6468 head = classhash_table + i;
6469 hlist_for_each_entry_rcu(class, head, hash_entry) {
6470 for (j = 0; j < NR_LOCKDEP_CACHING_CLASSES; j++)
6471 if (lock->class_cache[j] == class)
6472 return true;
6473 }
6474 }
6475 return false;
6476 }
6477
6478 /* The caller must hold the graph lock. Does not sleep. */
__lockdep_reset_lock(struct pending_free * pf,struct lockdep_map * lock)6479 static void __lockdep_reset_lock(struct pending_free *pf,
6480 struct lockdep_map *lock)
6481 {
6482 struct lock_class *class;
6483 int j;
6484
6485 /*
6486 * Remove all classes this lock might have:
6487 */
6488 for (j = 0; j < MAX_LOCKDEP_SUBCLASSES; j++) {
6489 /*
6490 * If the class exists we look it up and zap it:
6491 */
6492 class = look_up_lock_class(lock, j);
6493 if (class)
6494 zap_class(pf, class);
6495 }
6496 /*
6497 * Debug check: in the end all mapped classes should
6498 * be gone.
6499 */
6500 if (WARN_ON_ONCE(lock_class_cache_is_registered(lock)))
6501 debug_locks_off();
6502 }
6503
6504 /*
6505 * Remove all information lockdep has about a lock if debug_locks == 1. Free
6506 * released data structures from RCU context.
6507 */
lockdep_reset_lock_reg(struct lockdep_map * lock)6508 static void lockdep_reset_lock_reg(struct lockdep_map *lock)
6509 {
6510 struct pending_free *pf;
6511 unsigned long flags;
6512 int locked;
6513 bool need_callback = false;
6514
6515 raw_local_irq_save(flags);
6516 locked = graph_lock();
6517 if (!locked)
6518 goto out_irq;
6519
6520 pf = get_pending_free();
6521 __lockdep_reset_lock(pf, lock);
6522 need_callback = prepare_call_rcu_zapped(pf);
6523
6524 graph_unlock();
6525 out_irq:
6526 raw_local_irq_restore(flags);
6527 if (need_callback)
6528 call_rcu(&delayed_free.rcu_head, free_zapped_rcu);
6529 }
6530
6531 /*
6532 * Reset a lock. Does not sleep. Ignores debug_locks. Must only be used by the
6533 * lockdep selftests.
6534 */
lockdep_reset_lock_imm(struct lockdep_map * lock)6535 static void lockdep_reset_lock_imm(struct lockdep_map *lock)
6536 {
6537 struct pending_free *pf = delayed_free.pf;
6538 unsigned long flags;
6539
6540 raw_local_irq_save(flags);
6541 lockdep_lock();
6542 __lockdep_reset_lock(pf, lock);
6543 __free_zapped_classes(pf);
6544 lockdep_unlock();
6545 raw_local_irq_restore(flags);
6546 }
6547
lockdep_reset_lock(struct lockdep_map * lock)6548 void lockdep_reset_lock(struct lockdep_map *lock)
6549 {
6550 init_data_structures_once();
6551
6552 if (inside_selftest())
6553 lockdep_reset_lock_imm(lock);
6554 else
6555 lockdep_reset_lock_reg(lock);
6556 }
6557
6558 /*
6559 * Unregister a dynamically allocated key.
6560 *
6561 * Unlike lockdep_register_key(), a search is always done to find a matching
6562 * key irrespective of debug_locks to avoid potential invalid access to freed
6563 * memory in lock_class entry.
6564 */
lockdep_unregister_key(struct lock_class_key * key)6565 void lockdep_unregister_key(struct lock_class_key *key)
6566 {
6567 struct hlist_head *hash_head = keyhashentry(key);
6568 struct lock_class_key *k;
6569 struct pending_free *pf;
6570 unsigned long flags;
6571 bool found = false;
6572 bool need_callback = false;
6573
6574 might_sleep();
6575
6576 if (WARN_ON_ONCE(static_obj(key)))
6577 return;
6578
6579 raw_local_irq_save(flags);
6580 lockdep_lock();
6581
6582 hlist_for_each_entry_rcu(k, hash_head, hash_entry) {
6583 if (k == key) {
6584 hlist_del_rcu(&k->hash_entry);
6585 found = true;
6586 break;
6587 }
6588 }
6589 WARN_ON_ONCE(!found && debug_locks);
6590 if (found) {
6591 pf = get_pending_free();
6592 __lockdep_free_key_range(pf, key, 1);
6593 need_callback = prepare_call_rcu_zapped(pf);
6594 }
6595 lockdep_unlock();
6596 raw_local_irq_restore(flags);
6597
6598 if (need_callback)
6599 call_rcu(&delayed_free.rcu_head, free_zapped_rcu);
6600
6601 /* Wait until is_dynamic_key() has finished accessing k->hash_entry. */
6602 synchronize_rcu();
6603 }
6604 EXPORT_SYMBOL_GPL(lockdep_unregister_key);
6605
lockdep_init(void)6606 void __init lockdep_init(void)
6607 {
6608 pr_info("Lock dependency validator: Copyright (c) 2006 Red Hat, Inc., Ingo Molnar\n");
6609
6610 pr_info("... MAX_LOCKDEP_SUBCLASSES: %lu\n", MAX_LOCKDEP_SUBCLASSES);
6611 pr_info("... MAX_LOCK_DEPTH: %lu\n", MAX_LOCK_DEPTH);
6612 pr_info("... MAX_LOCKDEP_KEYS: %lu\n", MAX_LOCKDEP_KEYS);
6613 pr_info("... CLASSHASH_SIZE: %lu\n", CLASSHASH_SIZE);
6614 pr_info("... MAX_LOCKDEP_ENTRIES: %lu\n", MAX_LOCKDEP_ENTRIES);
6615 pr_info("... MAX_LOCKDEP_CHAINS: %lu\n", MAX_LOCKDEP_CHAINS);
6616 pr_info("... CHAINHASH_SIZE: %lu\n", CHAINHASH_SIZE);
6617
6618 pr_info(" memory used by lock dependency info: %zu kB\n",
6619 (sizeof(lock_classes) +
6620 sizeof(lock_classes_in_use) +
6621 sizeof(classhash_table) +
6622 sizeof(list_entries) +
6623 sizeof(list_entries_in_use) +
6624 sizeof(chainhash_table) +
6625 sizeof(delayed_free)
6626 #ifdef CONFIG_PROVE_LOCKING
6627 + sizeof(lock_cq)
6628 + sizeof(lock_chains)
6629 + sizeof(lock_chains_in_use)
6630 + sizeof(chain_hlocks)
6631 #endif
6632 ) / 1024
6633 );
6634
6635 #if defined(CONFIG_TRACE_IRQFLAGS) && defined(CONFIG_PROVE_LOCKING)
6636 pr_info(" memory used for stack traces: %zu kB\n",
6637 (sizeof(stack_trace) + sizeof(stack_trace_hash)) / 1024
6638 );
6639 #endif
6640
6641 pr_info(" per task-struct memory footprint: %zu bytes\n",
6642 sizeof(((struct task_struct *)NULL)->held_locks));
6643 }
6644
6645 static void
print_freed_lock_bug(struct task_struct * curr,const void * mem_from,const void * mem_to,struct held_lock * hlock)6646 print_freed_lock_bug(struct task_struct *curr, const void *mem_from,
6647 const void *mem_to, struct held_lock *hlock)
6648 {
6649 if (!debug_locks_off())
6650 return;
6651 if (debug_locks_silent)
6652 return;
6653
6654 nbcon_cpu_emergency_enter();
6655
6656 pr_warn("\n");
6657 pr_warn("=========================\n");
6658 pr_warn("WARNING: held lock freed!\n");
6659 print_kernel_ident();
6660 pr_warn("-------------------------\n");
6661 pr_warn("%s/%d is freeing memory %px-%px, with a lock still held there!\n",
6662 curr->comm, task_pid_nr(curr), mem_from, mem_to-1);
6663 print_lock(hlock);
6664 lockdep_print_held_locks(curr);
6665
6666 pr_warn("\nstack backtrace:\n");
6667 dump_stack();
6668
6669 nbcon_cpu_emergency_exit();
6670 }
6671
not_in_range(const void * mem_from,unsigned long mem_len,const void * lock_from,unsigned long lock_len)6672 static inline int not_in_range(const void* mem_from, unsigned long mem_len,
6673 const void* lock_from, unsigned long lock_len)
6674 {
6675 return lock_from + lock_len <= mem_from ||
6676 mem_from + mem_len <= lock_from;
6677 }
6678
6679 /*
6680 * Called when kernel memory is freed (or unmapped), or if a lock
6681 * is destroyed or reinitialized - this code checks whether there is
6682 * any held lock in the memory range of <from> to <to>:
6683 */
debug_check_no_locks_freed(const void * mem_from,unsigned long mem_len)6684 void debug_check_no_locks_freed(const void *mem_from, unsigned long mem_len)
6685 {
6686 struct task_struct *curr = current;
6687 struct held_lock *hlock;
6688 unsigned long flags;
6689 int i;
6690
6691 if (unlikely(!debug_locks))
6692 return;
6693
6694 raw_local_irq_save(flags);
6695 for (i = 0; i < curr->lockdep_depth; i++) {
6696 hlock = curr->held_locks + i;
6697
6698 if (not_in_range(mem_from, mem_len, hlock->instance,
6699 sizeof(*hlock->instance)))
6700 continue;
6701
6702 print_freed_lock_bug(curr, mem_from, mem_from + mem_len, hlock);
6703 break;
6704 }
6705 raw_local_irq_restore(flags);
6706 }
6707 EXPORT_SYMBOL_GPL(debug_check_no_locks_freed);
6708
print_held_locks_bug(void)6709 static void print_held_locks_bug(void)
6710 {
6711 if (!debug_locks_off())
6712 return;
6713 if (debug_locks_silent)
6714 return;
6715
6716 nbcon_cpu_emergency_enter();
6717
6718 pr_warn("\n");
6719 pr_warn("====================================\n");
6720 pr_warn("WARNING: %s/%d still has locks held!\n",
6721 current->comm, task_pid_nr(current));
6722 print_kernel_ident();
6723 pr_warn("------------------------------------\n");
6724 lockdep_print_held_locks(current);
6725 pr_warn("\nstack backtrace:\n");
6726 dump_stack();
6727
6728 nbcon_cpu_emergency_exit();
6729 }
6730
debug_check_no_locks_held(void)6731 void debug_check_no_locks_held(void)
6732 {
6733 if (unlikely(current->lockdep_depth > 0))
6734 print_held_locks_bug();
6735 }
6736 EXPORT_SYMBOL_GPL(debug_check_no_locks_held);
6737
6738 #ifdef __KERNEL__
debug_show_all_locks(void)6739 void debug_show_all_locks(void)
6740 {
6741 struct task_struct *g, *p;
6742
6743 if (unlikely(!debug_locks)) {
6744 pr_warn("INFO: lockdep is turned off.\n");
6745 return;
6746 }
6747 pr_warn("\nShowing all locks held in the system:\n");
6748
6749 rcu_read_lock();
6750 for_each_process_thread(g, p) {
6751 if (!p->lockdep_depth)
6752 continue;
6753 lockdep_print_held_locks(p);
6754 touch_nmi_watchdog();
6755 touch_all_softlockup_watchdogs();
6756 }
6757 rcu_read_unlock();
6758
6759 pr_warn("\n");
6760 pr_warn("=============================================\n\n");
6761 }
6762 EXPORT_SYMBOL_GPL(debug_show_all_locks);
6763 #endif
6764
6765 /*
6766 * Careful: only use this function if you are sure that
6767 * the task cannot run in parallel!
6768 */
debug_show_held_locks(struct task_struct * task)6769 void debug_show_held_locks(struct task_struct *task)
6770 {
6771 if (unlikely(!debug_locks)) {
6772 printk("INFO: lockdep is turned off.\n");
6773 return;
6774 }
6775 lockdep_print_held_locks(task);
6776 }
6777 EXPORT_SYMBOL_GPL(debug_show_held_locks);
6778
lockdep_sys_exit(void)6779 asmlinkage __visible void lockdep_sys_exit(void)
6780 {
6781 struct task_struct *curr = current;
6782
6783 if (unlikely(curr->lockdep_depth)) {
6784 if (!debug_locks_off())
6785 return;
6786 nbcon_cpu_emergency_enter();
6787 pr_warn("\n");
6788 pr_warn("================================================\n");
6789 pr_warn("WARNING: lock held when returning to user space!\n");
6790 print_kernel_ident();
6791 pr_warn("------------------------------------------------\n");
6792 pr_warn("%s/%d is leaving the kernel with locks still held!\n",
6793 curr->comm, curr->pid);
6794 lockdep_print_held_locks(curr);
6795 nbcon_cpu_emergency_exit();
6796 }
6797
6798 /*
6799 * The lock history for each syscall should be independent. So wipe the
6800 * slate clean on return to userspace.
6801 */
6802 lockdep_invariant_state(false);
6803 }
6804
lockdep_rcu_suspicious(const char * file,const int line,const char * s)6805 void lockdep_rcu_suspicious(const char *file, const int line, const char *s)
6806 {
6807 struct task_struct *curr = current;
6808 int dl = READ_ONCE(debug_locks);
6809 bool rcu = warn_rcu_enter();
6810
6811 /* Note: the following can be executed concurrently, so be careful. */
6812 nbcon_cpu_emergency_enter();
6813 pr_warn("\n");
6814 pr_warn("=============================\n");
6815 pr_warn("WARNING: suspicious RCU usage\n");
6816 print_kernel_ident();
6817 pr_warn("-----------------------------\n");
6818 pr_warn("%s:%d %s!\n", file, line, s);
6819 pr_warn("\nother info that might help us debug this:\n\n");
6820 pr_warn("\n%srcu_scheduler_active = %d, debug_locks = %d\n%s",
6821 !rcu_lockdep_current_cpu_online()
6822 ? "RCU used illegally from offline CPU!\n"
6823 : "",
6824 rcu_scheduler_active, dl,
6825 dl ? "" : "Possible false positive due to lockdep disabling via debug_locks = 0\n");
6826
6827 /*
6828 * If a CPU is in the RCU-free window in idle (ie: in the section
6829 * between ct_idle_enter() and ct_idle_exit(), then RCU
6830 * considers that CPU to be in an "extended quiescent state",
6831 * which means that RCU will be completely ignoring that CPU.
6832 * Therefore, rcu_read_lock() and friends have absolutely no
6833 * effect on a CPU running in that state. In other words, even if
6834 * such an RCU-idle CPU has called rcu_read_lock(), RCU might well
6835 * delete data structures out from under it. RCU really has no
6836 * choice here: we need to keep an RCU-free window in idle where
6837 * the CPU may possibly enter into low power mode. This way we can
6838 * notice an extended quiescent state to other CPUs that started a grace
6839 * period. Otherwise we would delay any grace period as long as we run
6840 * in the idle task.
6841 *
6842 * So complain bitterly if someone does call rcu_read_lock(),
6843 * rcu_read_lock_bh() and so on from extended quiescent states.
6844 */
6845 if (!rcu_is_watching())
6846 pr_warn("RCU used illegally from extended quiescent state!\n");
6847
6848 lockdep_print_held_locks(curr);
6849 pr_warn("\nstack backtrace:\n");
6850 dump_stack();
6851 nbcon_cpu_emergency_exit();
6852 warn_rcu_exit(rcu);
6853 }
6854 EXPORT_SYMBOL_GPL(lockdep_rcu_suspicious);
6855