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