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