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