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