]> git.proxmox.com Git - mirror_ubuntu-hirsute-kernel.git/blame - kernel/lockdep.c
init/main.c: Execute lockdep_init() as early as possible
[mirror_ubuntu-hirsute-kernel.git] / kernel / 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
383static int save_trace(struct stack_trace *trace)
384{
385 trace->nr_entries = 0;
386 trace->max_entries = MAX_STACK_TRACE_ENTRIES - nr_stack_trace_entries;
387 trace->entries = stack_trace + nr_stack_trace_entries;
388
5a1b3999 389 trace->skip = 3;
5a1b3999 390
ab1b6f03 391 save_stack_trace(trace);
fbb9ce95 392
4f84f433
PZ
393 /*
394 * Some daft arches put -1 at the end to indicate its a full trace.
395 *
396 * <rant> this is buggy anyway, since it takes a whole extra entry so a
397 * complete trace that maxes out the entries provided will be reported
398 * as incomplete, friggin useless </rant>
399 */
ea5b41f9
TL
400 if (trace->nr_entries != 0 &&
401 trace->entries[trace->nr_entries-1] == ULONG_MAX)
4f84f433
PZ
402 trace->nr_entries--;
403
fbb9ce95
IM
404 trace->max_entries = trace->nr_entries;
405
406 nr_stack_trace_entries += trace->nr_entries;
fbb9ce95 407
4f84f433 408 if (nr_stack_trace_entries >= MAX_STACK_TRACE_ENTRIES-1) {
74c383f1
IM
409 if (!debug_locks_off_graph_unlock())
410 return 0;
411
412 printk("BUG: MAX_STACK_TRACE_ENTRIES too low!\n");
413 printk("turning off the locking correctness validator.\n");
414 dump_stack();
415
fbb9ce95
IM
416 return 0;
417 }
418
419 return 1;
420}
421
422unsigned int nr_hardirq_chains;
423unsigned int nr_softirq_chains;
424unsigned int nr_process_chains;
425unsigned int max_lockdep_depth;
fbb9ce95
IM
426
427#ifdef CONFIG_DEBUG_LOCKDEP
428/*
429 * We cannot printk in early bootup code. Not even early_printk()
430 * might work. So we mark any initialization errors and printk
431 * about it later on, in lockdep_info().
432 */
433static int lockdep_init_error;
c71063c9
JB
434static unsigned long lockdep_init_trace_data[20];
435static struct stack_trace lockdep_init_trace = {
436 .max_entries = ARRAY_SIZE(lockdep_init_trace_data),
437 .entries = lockdep_init_trace_data,
438};
fbb9ce95
IM
439
440/*
441 * Various lockdep statistics:
442 */
bd6d29c2 443DEFINE_PER_CPU(struct lockdep_stats, lockdep_stats);
fbb9ce95
IM
444#endif
445
446/*
447 * Locking printouts:
448 */
449
fabe9c42 450#define __USAGE(__STATE) \
b4b136f4
PZ
451 [LOCK_USED_IN_##__STATE] = "IN-"__stringify(__STATE)"-W", \
452 [LOCK_ENABLED_##__STATE] = __stringify(__STATE)"-ON-W", \
453 [LOCK_USED_IN_##__STATE##_READ] = "IN-"__stringify(__STATE)"-R",\
454 [LOCK_ENABLED_##__STATE##_READ] = __stringify(__STATE)"-ON-R",
fabe9c42 455
fbb9ce95
IM
456static const char *usage_str[] =
457{
fabe9c42
PZ
458#define LOCKDEP_STATE(__STATE) __USAGE(__STATE)
459#include "lockdep_states.h"
460#undef LOCKDEP_STATE
461 [LOCK_USED] = "INITIAL USE",
fbb9ce95
IM
462};
463
464const char * __get_key_name(struct lockdep_subclass_key *key, char *str)
465{
ffb45122 466 return kallsyms_lookup((unsigned long)key, NULL, NULL, NULL, str);
fbb9ce95
IM
467}
468
3ff176ca 469static inline unsigned long lock_flag(enum lock_usage_bit bit)
fbb9ce95 470{
3ff176ca
PZ
471 return 1UL << bit;
472}
fbb9ce95 473
3ff176ca
PZ
474static char get_usage_char(struct lock_class *class, enum lock_usage_bit bit)
475{
476 char c = '.';
477
478 if (class->usage_mask & lock_flag(bit + 2))
479 c = '+';
480 if (class->usage_mask & lock_flag(bit)) {
481 c = '-';
482 if (class->usage_mask & lock_flag(bit + 2))
483 c = '?';
fbb9ce95
IM
484 }
485
3ff176ca
PZ
486 return c;
487}
cf40bd16 488
f510b233 489void get_usage_chars(struct lock_class *class, char usage[LOCK_USAGE_CHARS])
3ff176ca 490{
f510b233 491 int i = 0;
cf40bd16 492
f510b233
PZ
493#define LOCKDEP_STATE(__STATE) \
494 usage[i++] = get_usage_char(class, LOCK_USED_IN_##__STATE); \
495 usage[i++] = get_usage_char(class, LOCK_USED_IN_##__STATE##_READ);
496#include "lockdep_states.h"
497#undef LOCKDEP_STATE
498
499 usage[i] = '\0';
fbb9ce95
IM
500}
501
3003eba3
SR
502static int __print_lock_name(struct lock_class *class)
503{
504 char str[KSYM_NAME_LEN];
505 const char *name;
506
507 name = class->name;
508 if (!name)
509 name = __get_key_name(class->key, str);
510
511 return printk("%s", name);
512}
513
fbb9ce95
IM
514static void print_lock_name(struct lock_class *class)
515{
f510b233 516 char str[KSYM_NAME_LEN], usage[LOCK_USAGE_CHARS];
fbb9ce95
IM
517 const char *name;
518
f510b233 519 get_usage_chars(class, usage);
fbb9ce95
IM
520
521 name = class->name;
522 if (!name) {
523 name = __get_key_name(class->key, str);
524 printk(" (%s", name);
525 } else {
526 printk(" (%s", name);
527 if (class->name_version > 1)
528 printk("#%d", class->name_version);
529 if (class->subclass)
530 printk("/%d", class->subclass);
531 }
f510b233 532 printk("){%s}", usage);
fbb9ce95
IM
533}
534
535static void print_lockdep_cache(struct lockdep_map *lock)
536{
537 const char *name;
9281acea 538 char str[KSYM_NAME_LEN];
fbb9ce95
IM
539
540 name = lock->name;
541 if (!name)
542 name = __get_key_name(lock->key->subkeys, str);
543
544 printk("%s", name);
545}
546
547static void print_lock(struct held_lock *hlock)
548{
f82b217e 549 print_lock_name(hlock_class(hlock));
fbb9ce95
IM
550 printk(", at: ");
551 print_ip_sym(hlock->acquire_ip);
552}
553
554static void lockdep_print_held_locks(struct task_struct *curr)
555{
556 int i, depth = curr->lockdep_depth;
557
558 if (!depth) {
ba25f9dc 559 printk("no locks held by %s/%d.\n", curr->comm, task_pid_nr(curr));
fbb9ce95
IM
560 return;
561 }
562 printk("%d lock%s held by %s/%d:\n",
ba25f9dc 563 depth, depth > 1 ? "s" : "", curr->comm, task_pid_nr(curr));
fbb9ce95
IM
564
565 for (i = 0; i < depth; i++) {
566 printk(" #%d: ", i);
567 print_lock(curr->held_locks + i);
568 }
569}
fbb9ce95 570
fbdc4b9a 571static void print_kernel_ident(void)
8e18257d 572{
fbdc4b9a 573 printk("%s %.*s %s\n", init_utsname()->release,
8e18257d 574 (int)strcspn(init_utsname()->version, " "),
fbdc4b9a
BH
575 init_utsname()->version,
576 print_tainted());
8e18257d
PZ
577}
578
579static int very_verbose(struct lock_class *class)
580{
581#if VERY_VERBOSE
582 return class_filter(class);
583#endif
584 return 0;
585}
586
fbb9ce95 587/*
8e18257d 588 * Is this the address of a static object:
fbb9ce95 589 */
8e18257d 590static int static_obj(void *obj)
fbb9ce95 591{
8e18257d
PZ
592 unsigned long start = (unsigned long) &_stext,
593 end = (unsigned long) &_end,
594 addr = (unsigned long) obj;
8e18257d 595
fbb9ce95 596 /*
8e18257d 597 * static variable?
fbb9ce95 598 */
8e18257d
PZ
599 if ((addr >= start) && (addr < end))
600 return 1;
fbb9ce95 601
2a9ad18d
MF
602 if (arch_is_kernel_data(addr))
603 return 1;
604
fbb9ce95 605 /*
10fad5e4 606 * in-kernel percpu var?
fbb9ce95 607 */
10fad5e4
TH
608 if (is_kernel_percpu_address(addr))
609 return 1;
fbb9ce95 610
8e18257d 611 /*
10fad5e4 612 * module static or percpu var?
8e18257d 613 */
10fad5e4 614 return is_module_address(addr) || is_module_percpu_address(addr);
99de055a
DJ
615}
616
fbb9ce95 617/*
8e18257d
PZ
618 * To make lock name printouts unique, we calculate a unique
619 * class->name_version generation counter:
fbb9ce95 620 */
8e18257d 621static int count_matching_names(struct lock_class *new_class)
fbb9ce95 622{
8e18257d
PZ
623 struct lock_class *class;
624 int count = 0;
fbb9ce95 625
8e18257d 626 if (!new_class->name)
fbb9ce95
IM
627 return 0;
628
8e18257d
PZ
629 list_for_each_entry(class, &all_lock_classes, lock_entry) {
630 if (new_class->key - new_class->subclass == class->key)
631 return class->name_version;
632 if (class->name && !strcmp(class->name, new_class->name))
633 count = max(count, class->name_version);
634 }
fbb9ce95 635
8e18257d 636 return count + 1;
fbb9ce95
IM
637}
638
8e18257d
PZ
639/*
640 * Register a lock's class in the hash-table, if the class is not present
641 * yet. Otherwise we look it up. We cache the result in the lock object
642 * itself, so actual lookup of the hash should be once per lock object.
643 */
644static inline struct lock_class *
645look_up_lock_class(struct lockdep_map *lock, unsigned int subclass)
fbb9ce95 646{
8e18257d
PZ
647 struct lockdep_subclass_key *key;
648 struct list_head *hash_head;
649 struct lock_class *class;
fbb9ce95 650
8e18257d
PZ
651#ifdef CONFIG_DEBUG_LOCKDEP
652 /*
653 * If the architecture calls into lockdep before initializing
654 * the hashes then we'll warn about it later. (we cannot printk
655 * right now)
656 */
657 if (unlikely(!lockdep_initialized)) {
658 lockdep_init();
659 lockdep_init_error = 1;
c71063c9 660 save_stack_trace(&lockdep_init_trace);
8e18257d
PZ
661 }
662#endif
fbb9ce95 663
4ba053c0
HM
664 if (unlikely(subclass >= MAX_LOCKDEP_SUBCLASSES)) {
665 debug_locks_off();
666 printk(KERN_ERR
667 "BUG: looking up invalid subclass: %u\n", subclass);
668 printk(KERN_ERR
669 "turning off the locking correctness validator.\n");
670 dump_stack();
671 return NULL;
672 }
673
8e18257d
PZ
674 /*
675 * Static locks do not have their class-keys yet - for them the key
676 * is the lock object itself:
677 */
678 if (unlikely(!lock->key))
679 lock->key = (void *)lock;
fbb9ce95 680
8e18257d
PZ
681 /*
682 * NOTE: the class-key must be unique. For dynamic locks, a static
683 * lock_class_key variable is passed in through the mutex_init()
684 * (or spin_lock_init()) call - which acts as the key. For static
685 * locks we use the lock object itself as the key.
686 */
4b32d0a4
PZ
687 BUILD_BUG_ON(sizeof(struct lock_class_key) >
688 sizeof(struct lockdep_map));
fbb9ce95 689
8e18257d 690 key = lock->key->subkeys + subclass;
ca268c69 691
8e18257d 692 hash_head = classhashentry(key);
74c383f1 693
8e18257d
PZ
694 /*
695 * We can walk the hash lockfree, because the hash only
696 * grows, and we are careful when adding entries to the end:
697 */
4b32d0a4
PZ
698 list_for_each_entry(class, hash_head, hash_entry) {
699 if (class->key == key) {
0119fee4
PZ
700 /*
701 * Huh! same key, different name? Did someone trample
702 * on some memory? We're most confused.
703 */
4b32d0a4 704 WARN_ON_ONCE(class->name != lock->name);
8e18257d 705 return class;
4b32d0a4
PZ
706 }
707 }
fbb9ce95 708
8e18257d 709 return NULL;
fbb9ce95
IM
710}
711
712/*
8e18257d
PZ
713 * Register a lock's class in the hash-table, if the class is not present
714 * yet. Otherwise we look it up. We cache the result in the lock object
715 * itself, so actual lookup of the hash should be once per lock object.
fbb9ce95 716 */
8e18257d
PZ
717static inline struct lock_class *
718register_lock_class(struct lockdep_map *lock, unsigned int subclass, int force)
fbb9ce95 719{
8e18257d
PZ
720 struct lockdep_subclass_key *key;
721 struct list_head *hash_head;
722 struct lock_class *class;
723 unsigned long flags;
724
725 class = look_up_lock_class(lock, subclass);
726 if (likely(class))
87cdee71 727 goto out_set_class_cache;
8e18257d
PZ
728
729 /*
730 * Debug-check: all keys must be persistent!
731 */
732 if (!static_obj(lock->key)) {
733 debug_locks_off();
734 printk("INFO: trying to register non-static key.\n");
735 printk("the code is fine but needs lockdep annotation.\n");
736 printk("turning off the locking correctness validator.\n");
737 dump_stack();
738
739 return NULL;
740 }
741
742 key = lock->key->subkeys + subclass;
743 hash_head = classhashentry(key);
744
745 raw_local_irq_save(flags);
746 if (!graph_lock()) {
747 raw_local_irq_restore(flags);
748 return NULL;
749 }
750 /*
751 * We have to do the hash-walk again, to avoid races
752 * with another CPU:
753 */
754 list_for_each_entry(class, hash_head, hash_entry)
755 if (class->key == key)
756 goto out_unlock_set;
757 /*
758 * Allocate a new key from the static array, and add it to
759 * the hash:
760 */
761 if (nr_lock_classes >= MAX_LOCKDEP_KEYS) {
762 if (!debug_locks_off_graph_unlock()) {
763 raw_local_irq_restore(flags);
764 return NULL;
765 }
766 raw_local_irq_restore(flags);
767
768 printk("BUG: MAX_LOCKDEP_KEYS too low!\n");
769 printk("turning off the locking correctness validator.\n");
eedeeabd 770 dump_stack();
8e18257d
PZ
771 return NULL;
772 }
773 class = lock_classes + nr_lock_classes++;
bd6d29c2 774 debug_atomic_inc(nr_unused_locks);
8e18257d
PZ
775 class->key = key;
776 class->name = lock->name;
777 class->subclass = subclass;
778 INIT_LIST_HEAD(&class->lock_entry);
779 INIT_LIST_HEAD(&class->locks_before);
780 INIT_LIST_HEAD(&class->locks_after);
781 class->name_version = count_matching_names(class);
782 /*
783 * We use RCU's safe list-add method to make
784 * parallel walking of the hash-list safe:
785 */
786 list_add_tail_rcu(&class->hash_entry, hash_head);
1481197b
DF
787 /*
788 * Add it to the global list of classes:
789 */
790 list_add_tail_rcu(&class->lock_entry, &all_lock_classes);
8e18257d
PZ
791
792 if (verbose(class)) {
793 graph_unlock();
794 raw_local_irq_restore(flags);
795
796 printk("\nnew class %p: %s", class->key, class->name);
797 if (class->name_version > 1)
798 printk("#%d", class->name_version);
799 printk("\n");
800 dump_stack();
801
802 raw_local_irq_save(flags);
803 if (!graph_lock()) {
804 raw_local_irq_restore(flags);
805 return NULL;
806 }
807 }
808out_unlock_set:
809 graph_unlock();
810 raw_local_irq_restore(flags);
811
87cdee71 812out_set_class_cache:
8e18257d 813 if (!subclass || force)
62016250
HM
814 lock->class_cache[0] = class;
815 else if (subclass < NR_LOCKDEP_CACHING_CLASSES)
816 lock->class_cache[subclass] = class;
8e18257d 817
0119fee4
PZ
818 /*
819 * Hash collision, did we smoke some? We found a class with a matching
820 * hash but the subclass -- which is hashed in -- didn't match.
821 */
8e18257d
PZ
822 if (DEBUG_LOCKS_WARN_ON(class->subclass != subclass))
823 return NULL;
824
825 return class;
826}
827
828#ifdef CONFIG_PROVE_LOCKING
829/*
830 * Allocate a lockdep entry. (assumes the graph_lock held, returns
831 * with NULL on failure)
832 */
833static struct lock_list *alloc_list_entry(void)
834{
835 if (nr_list_entries >= MAX_LOCKDEP_ENTRIES) {
836 if (!debug_locks_off_graph_unlock())
837 return NULL;
838
839 printk("BUG: MAX_LOCKDEP_ENTRIES too low!\n");
840 printk("turning off the locking correctness validator.\n");
eedeeabd 841 dump_stack();
8e18257d
PZ
842 return NULL;
843 }
844 return list_entries + nr_list_entries++;
845}
846
847/*
848 * Add a new dependency to the head of the list:
849 */
850static int add_lock_to_list(struct lock_class *class, struct lock_class *this,
4726f2a6
YZ
851 struct list_head *head, unsigned long ip,
852 int distance, struct stack_trace *trace)
8e18257d
PZ
853{
854 struct lock_list *entry;
855 /*
856 * Lock not present yet - get a new dependency struct and
857 * add it to the list:
858 */
859 entry = alloc_list_entry();
860 if (!entry)
861 return 0;
862
74870172
ZY
863 entry->class = this;
864 entry->distance = distance;
4726f2a6 865 entry->trace = *trace;
8e18257d
PZ
866 /*
867 * Since we never remove from the dependency list, the list can
868 * be walked lockless by other CPUs, it's only allocation
869 * that must be protected by the spinlock. But this also means
870 * we must make new entries visible only once writes to the
871 * entry become visible - hence the RCU op:
872 */
873 list_add_tail_rcu(&entry->entry, head);
874
875 return 1;
876}
877
98c33edd
PZ
878/*
879 * For good efficiency of modular, we use power of 2
880 */
af012961
PZ
881#define MAX_CIRCULAR_QUEUE_SIZE 4096UL
882#define CQ_MASK (MAX_CIRCULAR_QUEUE_SIZE-1)
883
98c33edd
PZ
884/*
885 * The circular_queue and helpers is used to implement the
af012961
PZ
886 * breadth-first search(BFS)algorithem, by which we can build
887 * the shortest path from the next lock to be acquired to the
888 * previous held lock if there is a circular between them.
98c33edd 889 */
af012961
PZ
890struct circular_queue {
891 unsigned long element[MAX_CIRCULAR_QUEUE_SIZE];
892 unsigned int front, rear;
893};
894
895static struct circular_queue lock_cq;
af012961 896
12f3dfd0 897unsigned int max_bfs_queue_depth;
af012961 898
e351b660
ML
899static unsigned int lockdep_dependency_gen_id;
900
af012961
PZ
901static inline void __cq_init(struct circular_queue *cq)
902{
903 cq->front = cq->rear = 0;
e351b660 904 lockdep_dependency_gen_id++;
af012961
PZ
905}
906
907static inline int __cq_empty(struct circular_queue *cq)
908{
909 return (cq->front == cq->rear);
910}
911
912static inline int __cq_full(struct circular_queue *cq)
913{
914 return ((cq->rear + 1) & CQ_MASK) == cq->front;
915}
916
917static inline int __cq_enqueue(struct circular_queue *cq, unsigned long elem)
918{
919 if (__cq_full(cq))
920 return -1;
921
922 cq->element[cq->rear] = elem;
923 cq->rear = (cq->rear + 1) & CQ_MASK;
924 return 0;
925}
926
927static inline int __cq_dequeue(struct circular_queue *cq, unsigned long *elem)
928{
929 if (__cq_empty(cq))
930 return -1;
931
932 *elem = cq->element[cq->front];
933 cq->front = (cq->front + 1) & CQ_MASK;
934 return 0;
935}
936
937static inline unsigned int __cq_get_elem_count(struct circular_queue *cq)
938{
939 return (cq->rear - cq->front) & CQ_MASK;
940}
941
942static inline void mark_lock_accessed(struct lock_list *lock,
943 struct lock_list *parent)
944{
945 unsigned long nr;
98c33edd 946
af012961 947 nr = lock - list_entries;
0119fee4 948 WARN_ON(nr >= nr_list_entries); /* Out-of-bounds, input fail */
af012961 949 lock->parent = parent;
e351b660 950 lock->class->dep_gen_id = lockdep_dependency_gen_id;
af012961
PZ
951}
952
953static inline unsigned long lock_accessed(struct lock_list *lock)
954{
955 unsigned long nr;
98c33edd 956
af012961 957 nr = lock - list_entries;
0119fee4 958 WARN_ON(nr >= nr_list_entries); /* Out-of-bounds, input fail */
e351b660 959 return lock->class->dep_gen_id == lockdep_dependency_gen_id;
af012961
PZ
960}
961
962static inline struct lock_list *get_lock_parent(struct lock_list *child)
963{
964 return child->parent;
965}
966
967static inline int get_lock_depth(struct lock_list *child)
968{
969 int depth = 0;
970 struct lock_list *parent;
971
972 while ((parent = get_lock_parent(child))) {
973 child = parent;
974 depth++;
975 }
976 return depth;
977}
978
9e2d551e 979static int __bfs(struct lock_list *source_entry,
af012961
PZ
980 void *data,
981 int (*match)(struct lock_list *entry, void *data),
982 struct lock_list **target_entry,
983 int forward)
c94aa5ca
ML
984{
985 struct lock_list *entry;
d588e461 986 struct list_head *head;
c94aa5ca
ML
987 struct circular_queue *cq = &lock_cq;
988 int ret = 1;
989
9e2d551e 990 if (match(source_entry, data)) {
c94aa5ca
ML
991 *target_entry = source_entry;
992 ret = 0;
993 goto exit;
994 }
995
d588e461
ML
996 if (forward)
997 head = &source_entry->class->locks_after;
998 else
999 head = &source_entry->class->locks_before;
1000
1001 if (list_empty(head))
1002 goto exit;
1003
1004 __cq_init(cq);
c94aa5ca
ML
1005 __cq_enqueue(cq, (unsigned long)source_entry);
1006
1007 while (!__cq_empty(cq)) {
1008 struct lock_list *lock;
c94aa5ca
ML
1009
1010 __cq_dequeue(cq, (unsigned long *)&lock);
1011
1012 if (!lock->class) {
1013 ret = -2;
1014 goto exit;
1015 }
1016
1017 if (forward)
1018 head = &lock->class->locks_after;
1019 else
1020 head = &lock->class->locks_before;
1021
1022 list_for_each_entry(entry, head, entry) {
1023 if (!lock_accessed(entry)) {
12f3dfd0 1024 unsigned int cq_depth;
c94aa5ca 1025 mark_lock_accessed(entry, lock);
9e2d551e 1026 if (match(entry, data)) {
c94aa5ca
ML
1027 *target_entry = entry;
1028 ret = 0;
1029 goto exit;
1030 }
1031
1032 if (__cq_enqueue(cq, (unsigned long)entry)) {
1033 ret = -1;
1034 goto exit;
1035 }
12f3dfd0
ML
1036 cq_depth = __cq_get_elem_count(cq);
1037 if (max_bfs_queue_depth < cq_depth)
1038 max_bfs_queue_depth = cq_depth;
c94aa5ca
ML
1039 }
1040 }
1041 }
1042exit:
1043 return ret;
1044}
1045
d7aaba14 1046static inline int __bfs_forwards(struct lock_list *src_entry,
9e2d551e
ML
1047 void *data,
1048 int (*match)(struct lock_list *entry, void *data),
1049 struct lock_list **target_entry)
c94aa5ca 1050{
9e2d551e 1051 return __bfs(src_entry, data, match, target_entry, 1);
c94aa5ca
ML
1052
1053}
1054
d7aaba14 1055static inline int __bfs_backwards(struct lock_list *src_entry,
9e2d551e
ML
1056 void *data,
1057 int (*match)(struct lock_list *entry, void *data),
1058 struct lock_list **target_entry)
c94aa5ca 1059{
9e2d551e 1060 return __bfs(src_entry, data, match, target_entry, 0);
c94aa5ca
ML
1061
1062}
1063
8e18257d
PZ
1064/*
1065 * Recursive, forwards-direction lock-dependency checking, used for
1066 * both noncyclic checking and for hardirq-unsafe/softirq-unsafe
1067 * checking.
8e18257d 1068 */
8e18257d
PZ
1069
1070/*
1071 * Print a dependency chain entry (this is only done when a deadlock
1072 * has been detected):
1073 */
1074static noinline int
24208ca7 1075print_circular_bug_entry(struct lock_list *target, int depth)
8e18257d
PZ
1076{
1077 if (debug_locks_silent)
1078 return 0;
1079 printk("\n-> #%u", depth);
1080 print_lock_name(target->class);
1081 printk(":\n");
1082 print_stack_trace(&target->trace, 6);
1083
1084 return 0;
1085}
1086
f4185812
SR
1087static void
1088print_circular_lock_scenario(struct held_lock *src,
1089 struct held_lock *tgt,
1090 struct lock_list *prt)
1091{
1092 struct lock_class *source = hlock_class(src);
1093 struct lock_class *target = hlock_class(tgt);
1094 struct lock_class *parent = prt->class;
1095
1096 /*
1097 * A direct locking problem where unsafe_class lock is taken
1098 * directly by safe_class lock, then all we need to show
1099 * is the deadlock scenario, as it is obvious that the
1100 * unsafe lock is taken under the safe lock.
1101 *
1102 * But if there is a chain instead, where the safe lock takes
1103 * an intermediate lock (middle_class) where this lock is
1104 * not the same as the safe lock, then the lock chain is
1105 * used to describe the problem. Otherwise we would need
1106 * to show a different CPU case for each link in the chain
1107 * from the safe_class lock to the unsafe_class lock.
1108 */
1109 if (parent != source) {
1110 printk("Chain exists of:\n ");
1111 __print_lock_name(source);
1112 printk(" --> ");
1113 __print_lock_name(parent);
1114 printk(" --> ");
1115 __print_lock_name(target);
1116 printk("\n\n");
1117 }
1118
1119 printk(" Possible unsafe locking scenario:\n\n");
1120 printk(" CPU0 CPU1\n");
1121 printk(" ---- ----\n");
1122 printk(" lock(");
1123 __print_lock_name(target);
1124 printk(");\n");
1125 printk(" lock(");
1126 __print_lock_name(parent);
1127 printk(");\n");
1128 printk(" lock(");
1129 __print_lock_name(target);
1130 printk(");\n");
1131 printk(" lock(");
1132 __print_lock_name(source);
1133 printk(");\n");
1134 printk("\n *** DEADLOCK ***\n\n");
1135}
1136
8e18257d
PZ
1137/*
1138 * When a circular dependency is detected, print the
1139 * header first:
1140 */
1141static noinline int
db0002a3
ML
1142print_circular_bug_header(struct lock_list *entry, unsigned int depth,
1143 struct held_lock *check_src,
1144 struct held_lock *check_tgt)
8e18257d
PZ
1145{
1146 struct task_struct *curr = current;
1147
c94aa5ca 1148 if (debug_locks_silent)
8e18257d
PZ
1149 return 0;
1150
b3fbab05
PM
1151 printk("\n");
1152 printk("======================================================\n");
1153 printk("[ INFO: possible circular locking dependency detected ]\n");
fbdc4b9a 1154 print_kernel_ident();
b3fbab05 1155 printk("-------------------------------------------------------\n");
8e18257d 1156 printk("%s/%d is trying to acquire lock:\n",
ba25f9dc 1157 curr->comm, task_pid_nr(curr));
db0002a3 1158 print_lock(check_src);
8e18257d 1159 printk("\nbut task is already holding lock:\n");
db0002a3 1160 print_lock(check_tgt);
8e18257d
PZ
1161 printk("\nwhich lock already depends on the new lock.\n\n");
1162 printk("\nthe existing dependency chain (in reverse order) is:\n");
1163
1164 print_circular_bug_entry(entry, depth);
1165
1166 return 0;
1167}
1168
9e2d551e
ML
1169static inline int class_equal(struct lock_list *entry, void *data)
1170{
1171 return entry->class == data;
1172}
1173
db0002a3
ML
1174static noinline int print_circular_bug(struct lock_list *this,
1175 struct lock_list *target,
1176 struct held_lock *check_src,
1177 struct held_lock *check_tgt)
8e18257d
PZ
1178{
1179 struct task_struct *curr = current;
c94aa5ca 1180 struct lock_list *parent;
f4185812 1181 struct lock_list *first_parent;
24208ca7 1182 int depth;
8e18257d 1183
c94aa5ca 1184 if (!debug_locks_off_graph_unlock() || debug_locks_silent)
8e18257d
PZ
1185 return 0;
1186
db0002a3 1187 if (!save_trace(&this->trace))
8e18257d
PZ
1188 return 0;
1189
c94aa5ca
ML
1190 depth = get_lock_depth(target);
1191
db0002a3 1192 print_circular_bug_header(target, depth, check_src, check_tgt);
c94aa5ca
ML
1193
1194 parent = get_lock_parent(target);
f4185812 1195 first_parent = parent;
c94aa5ca
ML
1196
1197 while (parent) {
1198 print_circular_bug_entry(parent, --depth);
1199 parent = get_lock_parent(parent);
1200 }
8e18257d
PZ
1201
1202 printk("\nother info that might help us debug this:\n\n");
f4185812
SR
1203 print_circular_lock_scenario(check_src, check_tgt,
1204 first_parent);
1205
8e18257d
PZ
1206 lockdep_print_held_locks(curr);
1207
1208 printk("\nstack backtrace:\n");
1209 dump_stack();
1210
1211 return 0;
1212}
1213
db0002a3
ML
1214static noinline int print_bfs_bug(int ret)
1215{
1216 if (!debug_locks_off_graph_unlock())
1217 return 0;
1218
0119fee4
PZ
1219 /*
1220 * Breadth-first-search failed, graph got corrupted?
1221 */
db0002a3
ML
1222 WARN(1, "lockdep bfs error:%d\n", ret);
1223
1224 return 0;
1225}
1226
ef681026 1227static int noop_count(struct lock_list *entry, void *data)
419ca3f1 1228{
ef681026
ML
1229 (*(unsigned long *)data)++;
1230 return 0;
1231}
419ca3f1 1232
ef681026
ML
1233unsigned long __lockdep_count_forward_deps(struct lock_list *this)
1234{
1235 unsigned long count = 0;
1236 struct lock_list *uninitialized_var(target_entry);
419ca3f1 1237
ef681026 1238 __bfs_forwards(this, (void *)&count, noop_count, &target_entry);
419ca3f1 1239
ef681026 1240 return count;
419ca3f1 1241}
419ca3f1
DM
1242unsigned long lockdep_count_forward_deps(struct lock_class *class)
1243{
1244 unsigned long ret, flags;
ef681026
ML
1245 struct lock_list this;
1246
1247 this.parent = NULL;
1248 this.class = class;
419ca3f1
DM
1249
1250 local_irq_save(flags);
0199c4e6 1251 arch_spin_lock(&lockdep_lock);
ef681026 1252 ret = __lockdep_count_forward_deps(&this);
0199c4e6 1253 arch_spin_unlock(&lockdep_lock);
419ca3f1
DM
1254 local_irq_restore(flags);
1255
1256 return ret;
1257}
1258
ef681026 1259unsigned long __lockdep_count_backward_deps(struct lock_list *this)
419ca3f1 1260{
ef681026
ML
1261 unsigned long count = 0;
1262 struct lock_list *uninitialized_var(target_entry);
419ca3f1 1263
ef681026 1264 __bfs_backwards(this, (void *)&count, noop_count, &target_entry);
419ca3f1 1265
ef681026 1266 return count;
419ca3f1
DM
1267}
1268
1269unsigned long lockdep_count_backward_deps(struct lock_class *class)
1270{
1271 unsigned long ret, flags;
ef681026
ML
1272 struct lock_list this;
1273
1274 this.parent = NULL;
1275 this.class = class;
419ca3f1
DM
1276
1277 local_irq_save(flags);
0199c4e6 1278 arch_spin_lock(&lockdep_lock);
ef681026 1279 ret = __lockdep_count_backward_deps(&this);
0199c4e6 1280 arch_spin_unlock(&lockdep_lock);
419ca3f1
DM
1281 local_irq_restore(flags);
1282
1283 return ret;
1284}
1285
8e18257d
PZ
1286/*
1287 * Prove that the dependency graph starting at <entry> can not
1288 * lead to <target>. Print an error and return 0 if it does.
1289 */
1290static noinline int
db0002a3
ML
1291check_noncircular(struct lock_list *root, struct lock_class *target,
1292 struct lock_list **target_entry)
8e18257d 1293{
db0002a3 1294 int result;
8e18257d 1295
bd6d29c2 1296 debug_atomic_inc(nr_cyclic_checks);
419ca3f1 1297
d7aaba14 1298 result = __bfs_forwards(root, target, class_equal, target_entry);
fbb9ce95 1299
db0002a3
ML
1300 return result;
1301}
c94aa5ca 1302
81d68a96 1303#if defined(CONFIG_TRACE_IRQFLAGS) && defined(CONFIG_PROVE_LOCKING)
fbb9ce95
IM
1304/*
1305 * Forwards and backwards subgraph searching, for the purposes of
1306 * proving that two subgraphs can be connected by a new dependency
1307 * without creating any illegal irq-safe -> irq-unsafe lock dependency.
1308 */
fbb9ce95 1309
d7aaba14
ML
1310static inline int usage_match(struct lock_list *entry, void *bit)
1311{
1312 return entry->class->usage_mask & (1 << (enum lock_usage_bit)bit);
1313}
1314
1315
1316
fbb9ce95
IM
1317/*
1318 * Find a node in the forwards-direction dependency sub-graph starting
d7aaba14 1319 * at @root->class that matches @bit.
fbb9ce95 1320 *
d7aaba14
ML
1321 * Return 0 if such a node exists in the subgraph, and put that node
1322 * into *@target_entry.
fbb9ce95 1323 *
d7aaba14
ML
1324 * Return 1 otherwise and keep *@target_entry unchanged.
1325 * Return <0 on error.
fbb9ce95 1326 */
d7aaba14
ML
1327static int
1328find_usage_forwards(struct lock_list *root, enum lock_usage_bit bit,
1329 struct lock_list **target_entry)
fbb9ce95 1330{
d7aaba14 1331 int result;
fbb9ce95 1332
bd6d29c2 1333 debug_atomic_inc(nr_find_usage_forwards_checks);
fbb9ce95 1334
d7aaba14
ML
1335 result = __bfs_forwards(root, (void *)bit, usage_match, target_entry);
1336
1337 return result;
fbb9ce95
IM
1338}
1339
1340/*
1341 * Find a node in the backwards-direction dependency sub-graph starting
d7aaba14 1342 * at @root->class that matches @bit.
fbb9ce95 1343 *
d7aaba14
ML
1344 * Return 0 if such a node exists in the subgraph, and put that node
1345 * into *@target_entry.
fbb9ce95 1346 *
d7aaba14
ML
1347 * Return 1 otherwise and keep *@target_entry unchanged.
1348 * Return <0 on error.
fbb9ce95 1349 */
d7aaba14
ML
1350static int
1351find_usage_backwards(struct lock_list *root, enum lock_usage_bit bit,
1352 struct lock_list **target_entry)
fbb9ce95 1353{
d7aaba14 1354 int result;
fbb9ce95 1355
bd6d29c2 1356 debug_atomic_inc(nr_find_usage_backwards_checks);
fbb9ce95 1357
d7aaba14 1358 result = __bfs_backwards(root, (void *)bit, usage_match, target_entry);
f82b217e 1359
d7aaba14 1360 return result;
fbb9ce95
IM
1361}
1362
af012961
PZ
1363static void print_lock_class_header(struct lock_class *class, int depth)
1364{
1365 int bit;
1366
1367 printk("%*s->", depth, "");
1368 print_lock_name(class);
1369 printk(" ops: %lu", class->ops);
1370 printk(" {\n");
1371
1372 for (bit = 0; bit < LOCK_USAGE_STATES; bit++) {
1373 if (class->usage_mask & (1 << bit)) {
1374 int len = depth;
1375
1376 len += printk("%*s %s", depth, "", usage_str[bit]);
1377 len += printk(" at:\n");
1378 print_stack_trace(class->usage_traces + bit, len);
1379 }
1380 }
1381 printk("%*s }\n", depth, "");
1382
1383 printk("%*s ... key at: ",depth,"");
1384 print_ip_sym((unsigned long)class->key);
1385}
1386
1387/*
1388 * printk the shortest lock dependencies from @start to @end in reverse order:
1389 */
1390static void __used
1391print_shortest_lock_dependencies(struct lock_list *leaf,
1392 struct lock_list *root)
1393{
1394 struct lock_list *entry = leaf;
1395 int depth;
1396
1397 /*compute depth from generated tree by BFS*/
1398 depth = get_lock_depth(leaf);
1399
1400 do {
1401 print_lock_class_header(entry->class, depth);
1402 printk("%*s ... acquired at:\n", depth, "");
1403 print_stack_trace(&entry->trace, 2);
1404 printk("\n");
1405
1406 if (depth == 0 && (entry != root)) {
6be8c393 1407 printk("lockdep:%s bad path found in chain graph\n", __func__);
af012961
PZ
1408 break;
1409 }
1410
1411 entry = get_lock_parent(entry);
1412 depth--;
1413 } while (entry && (depth >= 0));
1414
1415 return;
1416}
d7aaba14 1417
3003eba3
SR
1418static void
1419print_irq_lock_scenario(struct lock_list *safe_entry,
1420 struct lock_list *unsafe_entry,
dad3d743
SR
1421 struct lock_class *prev_class,
1422 struct lock_class *next_class)
3003eba3
SR
1423{
1424 struct lock_class *safe_class = safe_entry->class;
1425 struct lock_class *unsafe_class = unsafe_entry->class;
dad3d743 1426 struct lock_class *middle_class = prev_class;
3003eba3
SR
1427
1428 if (middle_class == safe_class)
dad3d743 1429 middle_class = next_class;
3003eba3
SR
1430
1431 /*
1432 * A direct locking problem where unsafe_class lock is taken
1433 * directly by safe_class lock, then all we need to show
1434 * is the deadlock scenario, as it is obvious that the
1435 * unsafe lock is taken under the safe lock.
1436 *
1437 * But if there is a chain instead, where the safe lock takes
1438 * an intermediate lock (middle_class) where this lock is
1439 * not the same as the safe lock, then the lock chain is
1440 * used to describe the problem. Otherwise we would need
1441 * to show a different CPU case for each link in the chain
1442 * from the safe_class lock to the unsafe_class lock.
1443 */
1444 if (middle_class != unsafe_class) {
1445 printk("Chain exists of:\n ");
1446 __print_lock_name(safe_class);
1447 printk(" --> ");
1448 __print_lock_name(middle_class);
1449 printk(" --> ");
1450 __print_lock_name(unsafe_class);
1451 printk("\n\n");
1452 }
1453
1454 printk(" Possible interrupt unsafe locking scenario:\n\n");
1455 printk(" CPU0 CPU1\n");
1456 printk(" ---- ----\n");
1457 printk(" lock(");
1458 __print_lock_name(unsafe_class);
1459 printk(");\n");
1460 printk(" local_irq_disable();\n");
1461 printk(" lock(");
1462 __print_lock_name(safe_class);
1463 printk(");\n");
1464 printk(" lock(");
1465 __print_lock_name(middle_class);
1466 printk(");\n");
1467 printk(" <Interrupt>\n");
1468 printk(" lock(");
1469 __print_lock_name(safe_class);
1470 printk(");\n");
1471 printk("\n *** DEADLOCK ***\n\n");
1472}
1473
fbb9ce95
IM
1474static int
1475print_bad_irq_dependency(struct task_struct *curr,
24208ca7
ML
1476 struct lock_list *prev_root,
1477 struct lock_list *next_root,
1478 struct lock_list *backwards_entry,
1479 struct lock_list *forwards_entry,
fbb9ce95
IM
1480 struct held_lock *prev,
1481 struct held_lock *next,
1482 enum lock_usage_bit bit1,
1483 enum lock_usage_bit bit2,
1484 const char *irqclass)
1485{
74c383f1 1486 if (!debug_locks_off_graph_unlock() || debug_locks_silent)
fbb9ce95
IM
1487 return 0;
1488
b3fbab05
PM
1489 printk("\n");
1490 printk("======================================================\n");
1491 printk("[ INFO: %s-safe -> %s-unsafe lock order detected ]\n",
fbb9ce95 1492 irqclass, irqclass);
fbdc4b9a 1493 print_kernel_ident();
b3fbab05 1494 printk("------------------------------------------------------\n");
fbb9ce95 1495 printk("%s/%d [HC%u[%lu]:SC%u[%lu]:HE%u:SE%u] is trying to acquire:\n",
ba25f9dc 1496 curr->comm, task_pid_nr(curr),
fbb9ce95
IM
1497 curr->hardirq_context, hardirq_count() >> HARDIRQ_SHIFT,
1498 curr->softirq_context, softirq_count() >> SOFTIRQ_SHIFT,
1499 curr->hardirqs_enabled,
1500 curr->softirqs_enabled);
1501 print_lock(next);
1502
1503 printk("\nand this task is already holding:\n");
1504 print_lock(prev);
1505 printk("which would create a new lock dependency:\n");
f82b217e 1506 print_lock_name(hlock_class(prev));
fbb9ce95 1507 printk(" ->");
f82b217e 1508 print_lock_name(hlock_class(next));
fbb9ce95
IM
1509 printk("\n");
1510
1511 printk("\nbut this new dependency connects a %s-irq-safe lock:\n",
1512 irqclass);
24208ca7 1513 print_lock_name(backwards_entry->class);
fbb9ce95
IM
1514 printk("\n... which became %s-irq-safe at:\n", irqclass);
1515
24208ca7 1516 print_stack_trace(backwards_entry->class->usage_traces + bit1, 1);
fbb9ce95
IM
1517
1518 printk("\nto a %s-irq-unsafe lock:\n", irqclass);
24208ca7 1519 print_lock_name(forwards_entry->class);
fbb9ce95
IM
1520 printk("\n... which became %s-irq-unsafe at:\n", irqclass);
1521 printk("...");
1522
24208ca7 1523 print_stack_trace(forwards_entry->class->usage_traces + bit2, 1);
fbb9ce95
IM
1524
1525 printk("\nother info that might help us debug this:\n\n");
dad3d743
SR
1526 print_irq_lock_scenario(backwards_entry, forwards_entry,
1527 hlock_class(prev), hlock_class(next));
3003eba3 1528
fbb9ce95
IM
1529 lockdep_print_held_locks(curr);
1530
24208ca7
ML
1531 printk("\nthe dependencies between %s-irq-safe lock", irqclass);
1532 printk(" and the holding lock:\n");
1533 if (!save_trace(&prev_root->trace))
1534 return 0;
1535 print_shortest_lock_dependencies(backwards_entry, prev_root);
fbb9ce95 1536
24208ca7
ML
1537 printk("\nthe dependencies between the lock to be acquired");
1538 printk(" and %s-irq-unsafe lock:\n", irqclass);
1539 if (!save_trace(&next_root->trace))
1540 return 0;
1541 print_shortest_lock_dependencies(forwards_entry, next_root);
fbb9ce95
IM
1542
1543 printk("\nstack backtrace:\n");
1544 dump_stack();
1545
1546 return 0;
1547}
1548
1549static int
1550check_usage(struct task_struct *curr, struct held_lock *prev,
1551 struct held_lock *next, enum lock_usage_bit bit_backwards,
1552 enum lock_usage_bit bit_forwards, const char *irqclass)
1553{
1554 int ret;
24208ca7 1555 struct lock_list this, that;
d7aaba14 1556 struct lock_list *uninitialized_var(target_entry);
24208ca7 1557 struct lock_list *uninitialized_var(target_entry1);
d7aaba14
ML
1558
1559 this.parent = NULL;
1560
1561 this.class = hlock_class(prev);
1562 ret = find_usage_backwards(&this, bit_backwards, &target_entry);
af012961
PZ
1563 if (ret < 0)
1564 return print_bfs_bug(ret);
1565 if (ret == 1)
1566 return ret;
d7aaba14 1567
24208ca7
ML
1568 that.parent = NULL;
1569 that.class = hlock_class(next);
1570 ret = find_usage_forwards(&that, bit_forwards, &target_entry1);
af012961
PZ
1571 if (ret < 0)
1572 return print_bfs_bug(ret);
1573 if (ret == 1)
1574 return ret;
fbb9ce95 1575
24208ca7
ML
1576 return print_bad_irq_dependency(curr, &this, &that,
1577 target_entry, target_entry1,
1578 prev, next,
fbb9ce95
IM
1579 bit_backwards, bit_forwards, irqclass);
1580}
1581
4f367d8a
PZ
1582static const char *state_names[] = {
1583#define LOCKDEP_STATE(__STATE) \
b4b136f4 1584 __stringify(__STATE),
4f367d8a
PZ
1585#include "lockdep_states.h"
1586#undef LOCKDEP_STATE
1587};
1588
1589static const char *state_rnames[] = {
1590#define LOCKDEP_STATE(__STATE) \
b4b136f4 1591 __stringify(__STATE)"-READ",
4f367d8a
PZ
1592#include "lockdep_states.h"
1593#undef LOCKDEP_STATE
1594};
1595
1596static inline const char *state_name(enum lock_usage_bit bit)
8e18257d 1597{
4f367d8a
PZ
1598 return (bit & 1) ? state_rnames[bit >> 2] : state_names[bit >> 2];
1599}
8e18257d 1600
4f367d8a
PZ
1601static int exclusive_bit(int new_bit)
1602{
8e18257d 1603 /*
4f367d8a
PZ
1604 * USED_IN
1605 * USED_IN_READ
1606 * ENABLED
1607 * ENABLED_READ
1608 *
1609 * bit 0 - write/read
1610 * bit 1 - used_in/enabled
1611 * bit 2+ state
8e18257d 1612 */
4f367d8a
PZ
1613
1614 int state = new_bit & ~3;
1615 int dir = new_bit & 2;
8e18257d
PZ
1616
1617 /*
4f367d8a 1618 * keep state, bit flip the direction and strip read.
8e18257d 1619 */
4f367d8a
PZ
1620 return state | (dir ^ 2);
1621}
1622
1623static int check_irq_usage(struct task_struct *curr, struct held_lock *prev,
1624 struct held_lock *next, enum lock_usage_bit bit)
1625{
8e18257d 1626 /*
4f367d8a
PZ
1627 * Prove that the new dependency does not connect a hardirq-safe
1628 * lock with a hardirq-unsafe lock - to achieve this we search
8e18257d
PZ
1629 * the backwards-subgraph starting at <prev>, and the
1630 * forwards-subgraph starting at <next>:
1631 */
4f367d8a
PZ
1632 if (!check_usage(curr, prev, next, bit,
1633 exclusive_bit(bit), state_name(bit)))
8e18257d
PZ
1634 return 0;
1635
4f367d8a
PZ
1636 bit++; /* _READ */
1637
cf40bd16 1638 /*
4f367d8a
PZ
1639 * Prove that the new dependency does not connect a hardirq-safe-read
1640 * lock with a hardirq-unsafe lock - to achieve this we search
cf40bd16
NP
1641 * the backwards-subgraph starting at <prev>, and the
1642 * forwards-subgraph starting at <next>:
1643 */
4f367d8a
PZ
1644 if (!check_usage(curr, prev, next, bit,
1645 exclusive_bit(bit), state_name(bit)))
cf40bd16
NP
1646 return 0;
1647
4f367d8a
PZ
1648 return 1;
1649}
1650
1651static int
1652check_prev_add_irq(struct task_struct *curr, struct held_lock *prev,
1653 struct held_lock *next)
1654{
1655#define LOCKDEP_STATE(__STATE) \
1656 if (!check_irq_usage(curr, prev, next, LOCK_USED_IN_##__STATE)) \
cf40bd16 1657 return 0;
4f367d8a
PZ
1658#include "lockdep_states.h"
1659#undef LOCKDEP_STATE
cf40bd16 1660
8e18257d
PZ
1661 return 1;
1662}
1663
1664static void inc_chains(void)
1665{
1666 if (current->hardirq_context)
1667 nr_hardirq_chains++;
1668 else {
1669 if (current->softirq_context)
1670 nr_softirq_chains++;
1671 else
1672 nr_process_chains++;
1673 }
1674}
1675
1676#else
1677
1678static inline int
1679check_prev_add_irq(struct task_struct *curr, struct held_lock *prev,
1680 struct held_lock *next)
1681{
1682 return 1;
1683}
1684
1685static inline void inc_chains(void)
1686{
1687 nr_process_chains++;
1688}
1689
fbb9ce95
IM
1690#endif
1691
48702ecf
SR
1692static void
1693print_deadlock_scenario(struct held_lock *nxt,
1694 struct held_lock *prv)
1695{
1696 struct lock_class *next = hlock_class(nxt);
1697 struct lock_class *prev = hlock_class(prv);
1698
1699 printk(" Possible unsafe locking scenario:\n\n");
1700 printk(" CPU0\n");
1701 printk(" ----\n");
1702 printk(" lock(");
1703 __print_lock_name(prev);
1704 printk(");\n");
1705 printk(" lock(");
1706 __print_lock_name(next);
1707 printk(");\n");
1708 printk("\n *** DEADLOCK ***\n\n");
1709 printk(" May be due to missing lock nesting notation\n\n");
1710}
1711
fbb9ce95
IM
1712static int
1713print_deadlock_bug(struct task_struct *curr, struct held_lock *prev,
1714 struct held_lock *next)
1715{
74c383f1 1716 if (!debug_locks_off_graph_unlock() || debug_locks_silent)
fbb9ce95
IM
1717 return 0;
1718
b3fbab05
PM
1719 printk("\n");
1720 printk("=============================================\n");
1721 printk("[ INFO: possible recursive locking detected ]\n");
fbdc4b9a 1722 print_kernel_ident();
b3fbab05 1723 printk("---------------------------------------------\n");
fbb9ce95 1724 printk("%s/%d is trying to acquire lock:\n",
ba25f9dc 1725 curr->comm, task_pid_nr(curr));
fbb9ce95
IM
1726 print_lock(next);
1727 printk("\nbut task is already holding lock:\n");
1728 print_lock(prev);
1729
1730 printk("\nother info that might help us debug this:\n");
48702ecf 1731 print_deadlock_scenario(next, prev);
fbb9ce95
IM
1732 lockdep_print_held_locks(curr);
1733
1734 printk("\nstack backtrace:\n");
1735 dump_stack();
1736
1737 return 0;
1738}
1739
1740/*
1741 * Check whether we are holding such a class already.
1742 *
1743 * (Note that this has to be done separately, because the graph cannot
1744 * detect such classes of deadlocks.)
1745 *
1746 * Returns: 0 on deadlock detected, 1 on OK, 2 on recursive read
1747 */
1748static int
1749check_deadlock(struct task_struct *curr, struct held_lock *next,
1750 struct lockdep_map *next_instance, int read)
1751{
1752 struct held_lock *prev;
7531e2f3 1753 struct held_lock *nest = NULL;
fbb9ce95
IM
1754 int i;
1755
1756 for (i = 0; i < curr->lockdep_depth; i++) {
1757 prev = curr->held_locks + i;
7531e2f3
PZ
1758
1759 if (prev->instance == next->nest_lock)
1760 nest = prev;
1761
f82b217e 1762 if (hlock_class(prev) != hlock_class(next))
fbb9ce95 1763 continue;
7531e2f3 1764
fbb9ce95
IM
1765 /*
1766 * Allow read-after-read recursion of the same
6c9076ec 1767 * lock class (i.e. read_lock(lock)+read_lock(lock)):
fbb9ce95 1768 */
6c9076ec 1769 if ((read == 2) && prev->read)
fbb9ce95 1770 return 2;
7531e2f3
PZ
1771
1772 /*
1773 * We're holding the nest_lock, which serializes this lock's
1774 * nesting behaviour.
1775 */
1776 if (nest)
1777 return 2;
1778
fbb9ce95
IM
1779 return print_deadlock_bug(curr, prev, next);
1780 }
1781 return 1;
1782}
1783
1784/*
1785 * There was a chain-cache miss, and we are about to add a new dependency
1786 * to a previous lock. We recursively validate the following rules:
1787 *
1788 * - would the adding of the <prev> -> <next> dependency create a
1789 * circular dependency in the graph? [== circular deadlock]
1790 *
1791 * - does the new prev->next dependency connect any hardirq-safe lock
1792 * (in the full backwards-subgraph starting at <prev>) with any
1793 * hardirq-unsafe lock (in the full forwards-subgraph starting at
1794 * <next>)? [== illegal lock inversion with hardirq contexts]
1795 *
1796 * - does the new prev->next dependency connect any softirq-safe lock
1797 * (in the full backwards-subgraph starting at <prev>) with any
1798 * softirq-unsafe lock (in the full forwards-subgraph starting at
1799 * <next>)? [== illegal lock inversion with softirq contexts]
1800 *
1801 * any of these scenarios could lead to a deadlock.
1802 *
1803 * Then if all the validations pass, we add the forwards and backwards
1804 * dependency.
1805 */
1806static int
1807check_prev_add(struct task_struct *curr, struct held_lock *prev,
4726f2a6 1808 struct held_lock *next, int distance, int trylock_loop)
fbb9ce95
IM
1809{
1810 struct lock_list *entry;
1811 int ret;
db0002a3
ML
1812 struct lock_list this;
1813 struct lock_list *uninitialized_var(target_entry);
4726f2a6
YZ
1814 /*
1815 * Static variable, serialized by the graph_lock().
1816 *
1817 * We use this static variable to save the stack trace in case
1818 * we call into this function multiple times due to encountering
1819 * trylocks in the held lock stack.
1820 */
1821 static struct stack_trace trace;
fbb9ce95
IM
1822
1823 /*
1824 * Prove that the new <prev> -> <next> dependency would not
1825 * create a circular dependency in the graph. (We do this by
1826 * forward-recursing into the graph starting at <next>, and
1827 * checking whether we can reach <prev>.)
1828 *
1829 * We are using global variables to control the recursion, to
1830 * keep the stackframe size of the recursive functions low:
1831 */
db0002a3
ML
1832 this.class = hlock_class(next);
1833 this.parent = NULL;
1834 ret = check_noncircular(&this, hlock_class(prev), &target_entry);
1835 if (unlikely(!ret))
1836 return print_circular_bug(&this, target_entry, next, prev);
1837 else if (unlikely(ret < 0))
1838 return print_bfs_bug(ret);
c94aa5ca 1839
8e18257d 1840 if (!check_prev_add_irq(curr, prev, next))
fbb9ce95
IM
1841 return 0;
1842
fbb9ce95
IM
1843 /*
1844 * For recursive read-locks we do all the dependency checks,
1845 * but we dont store read-triggered dependencies (only
1846 * write-triggered dependencies). This ensures that only the
1847 * write-side dependencies matter, and that if for example a
1848 * write-lock never takes any other locks, then the reads are
1849 * equivalent to a NOP.
1850 */
1851 if (next->read == 2 || prev->read == 2)
1852 return 1;
1853 /*
1854 * Is the <prev> -> <next> dependency already present?
1855 *
1856 * (this may occur even though this is a new chain: consider
1857 * e.g. the L1 -> L2 -> L3 -> L4 and the L5 -> L1 -> L2 -> L3
1858 * chains - the second one will be new, but L1 already has
1859 * L2 added to its dependency list, due to the first chain.)
1860 */
f82b217e
DJ
1861 list_for_each_entry(entry, &hlock_class(prev)->locks_after, entry) {
1862 if (entry->class == hlock_class(next)) {
068135e6
JB
1863 if (distance == 1)
1864 entry->distance = 1;
fbb9ce95 1865 return 2;
068135e6 1866 }
fbb9ce95
IM
1867 }
1868
4726f2a6
YZ
1869 if (!trylock_loop && !save_trace(&trace))
1870 return 0;
1871
fbb9ce95
IM
1872 /*
1873 * Ok, all validations passed, add the new lock
1874 * to the previous lock's dependency list:
1875 */
f82b217e
DJ
1876 ret = add_lock_to_list(hlock_class(prev), hlock_class(next),
1877 &hlock_class(prev)->locks_after,
4726f2a6 1878 next->acquire_ip, distance, &trace);
068135e6 1879
fbb9ce95
IM
1880 if (!ret)
1881 return 0;
910b1b2e 1882
f82b217e
DJ
1883 ret = add_lock_to_list(hlock_class(next), hlock_class(prev),
1884 &hlock_class(next)->locks_before,
4726f2a6 1885 next->acquire_ip, distance, &trace);
910b1b2e
JP
1886 if (!ret)
1887 return 0;
fbb9ce95
IM
1888
1889 /*
8e18257d
PZ
1890 * Debugging printouts:
1891 */
f82b217e 1892 if (verbose(hlock_class(prev)) || verbose(hlock_class(next))) {
8e18257d
PZ
1893 graph_unlock();
1894 printk("\n new dependency: ");
f82b217e 1895 print_lock_name(hlock_class(prev));
8e18257d 1896 printk(" => ");
f82b217e 1897 print_lock_name(hlock_class(next));
8e18257d 1898 printk("\n");
fbb9ce95 1899 dump_stack();
8e18257d 1900 return graph_lock();
fbb9ce95 1901 }
8e18257d
PZ
1902 return 1;
1903}
fbb9ce95 1904
8e18257d
PZ
1905/*
1906 * Add the dependency to all directly-previous locks that are 'relevant'.
1907 * The ones that are relevant are (in increasing distance from curr):
1908 * all consecutive trylock entries and the final non-trylock entry - or
1909 * the end of this context's lock-chain - whichever comes first.
1910 */
1911static int
1912check_prevs_add(struct task_struct *curr, struct held_lock *next)
1913{
1914 int depth = curr->lockdep_depth;
4726f2a6 1915 int trylock_loop = 0;
8e18257d 1916 struct held_lock *hlock;
d6d897ce 1917
fbb9ce95 1918 /*
8e18257d
PZ
1919 * Debugging checks.
1920 *
1921 * Depth must not be zero for a non-head lock:
fbb9ce95 1922 */
8e18257d
PZ
1923 if (!depth)
1924 goto out_bug;
fbb9ce95 1925 /*
8e18257d
PZ
1926 * At least two relevant locks must exist for this
1927 * to be a head:
fbb9ce95 1928 */
8e18257d
PZ
1929 if (curr->held_locks[depth].irq_context !=
1930 curr->held_locks[depth-1].irq_context)
1931 goto out_bug;
74c383f1 1932
8e18257d
PZ
1933 for (;;) {
1934 int distance = curr->lockdep_depth - depth + 1;
1935 hlock = curr->held_locks + depth-1;
1936 /*
1937 * Only non-recursive-read entries get new dependencies
1938 * added:
1939 */
1940 if (hlock->read != 2) {
4726f2a6
YZ
1941 if (!check_prev_add(curr, hlock, next,
1942 distance, trylock_loop))
8e18257d
PZ
1943 return 0;
1944 /*
1945 * Stop after the first non-trylock entry,
1946 * as non-trylock entries have added their
1947 * own direct dependencies already, so this
1948 * lock is connected to them indirectly:
1949 */
1950 if (!hlock->trylock)
1951 break;
74c383f1 1952 }
8e18257d
PZ
1953 depth--;
1954 /*
1955 * End of lock-stack?
1956 */
1957 if (!depth)
1958 break;
1959 /*
1960 * Stop the search if we cross into another context:
1961 */
1962 if (curr->held_locks[depth].irq_context !=
1963 curr->held_locks[depth-1].irq_context)
1964 break;
4726f2a6 1965 trylock_loop = 1;
fbb9ce95 1966 }
8e18257d
PZ
1967 return 1;
1968out_bug:
1969 if (!debug_locks_off_graph_unlock())
1970 return 0;
fbb9ce95 1971
0119fee4
PZ
1972 /*
1973 * Clearly we all shouldn't be here, but since we made it we
1974 * can reliable say we messed up our state. See the above two
1975 * gotos for reasons why we could possibly end up here.
1976 */
8e18257d 1977 WARN_ON(1);
fbb9ce95 1978
8e18257d 1979 return 0;
fbb9ce95
IM
1980}
1981
8e18257d 1982unsigned long nr_lock_chains;
443cd507 1983struct lock_chain lock_chains[MAX_LOCKDEP_CHAINS];
cd1a28e8 1984int nr_chain_hlocks;
443cd507
HY
1985static u16 chain_hlocks[MAX_LOCKDEP_CHAIN_HLOCKS];
1986
1987struct lock_class *lock_chain_get_class(struct lock_chain *chain, int i)
1988{
1989 return lock_classes + chain_hlocks[chain->base + i];
1990}
8e18257d 1991
fbb9ce95
IM
1992/*
1993 * Look up a dependency chain. If the key is not present yet then
9e860d00
JP
1994 * add it and return 1 - in this case the new dependency chain is
1995 * validated. If the key is already hashed, return 0.
1996 * (On return with 1 graph_lock is held.)
fbb9ce95 1997 */
443cd507
HY
1998static inline int lookup_chain_cache(struct task_struct *curr,
1999 struct held_lock *hlock,
2000 u64 chain_key)
fbb9ce95 2001{
f82b217e 2002 struct lock_class *class = hlock_class(hlock);
fbb9ce95
IM
2003 struct list_head *hash_head = chainhashentry(chain_key);
2004 struct lock_chain *chain;
443cd507 2005 struct held_lock *hlock_curr, *hlock_next;
e0944ee6 2006 int i, j;
fbb9ce95 2007
0119fee4
PZ
2008 /*
2009 * We might need to take the graph lock, ensure we've got IRQs
2010 * disabled to make this an IRQ-safe lock.. for recursion reasons
2011 * lockdep won't complain about its own locking errors.
2012 */
381a2292
JP
2013 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2014 return 0;
fbb9ce95
IM
2015 /*
2016 * We can walk it lock-free, because entries only get added
2017 * to the hash:
2018 */
2019 list_for_each_entry(chain, hash_head, entry) {
2020 if (chain->chain_key == chain_key) {
2021cache_hit:
bd6d29c2 2022 debug_atomic_inc(chain_lookup_hits);
81fc685a 2023 if (very_verbose(class))
755cd900
AM
2024 printk("\nhash chain already cached, key: "
2025 "%016Lx tail class: [%p] %s\n",
2026 (unsigned long long)chain_key,
2027 class->key, class->name);
fbb9ce95
IM
2028 return 0;
2029 }
2030 }
81fc685a 2031 if (very_verbose(class))
755cd900
AM
2032 printk("\nnew hash chain, key: %016Lx tail class: [%p] %s\n",
2033 (unsigned long long)chain_key, class->key, class->name);
fbb9ce95
IM
2034 /*
2035 * Allocate a new chain entry from the static array, and add
2036 * it to the hash:
2037 */
74c383f1
IM
2038 if (!graph_lock())
2039 return 0;
fbb9ce95
IM
2040 /*
2041 * We have to walk the chain again locked - to avoid duplicates:
2042 */
2043 list_for_each_entry(chain, hash_head, entry) {
2044 if (chain->chain_key == chain_key) {
74c383f1 2045 graph_unlock();
fbb9ce95
IM
2046 goto cache_hit;
2047 }
2048 }
2049 if (unlikely(nr_lock_chains >= MAX_LOCKDEP_CHAINS)) {
74c383f1
IM
2050 if (!debug_locks_off_graph_unlock())
2051 return 0;
2052
fbb9ce95
IM
2053 printk("BUG: MAX_LOCKDEP_CHAINS too low!\n");
2054 printk("turning off the locking correctness validator.\n");
eedeeabd 2055 dump_stack();
fbb9ce95
IM
2056 return 0;
2057 }
2058 chain = lock_chains + nr_lock_chains++;
2059 chain->chain_key = chain_key;
443cd507
HY
2060 chain->irq_context = hlock->irq_context;
2061 /* Find the first held_lock of current chain */
2062 hlock_next = hlock;
2063 for (i = curr->lockdep_depth - 1; i >= 0; i--) {
2064 hlock_curr = curr->held_locks + i;
2065 if (hlock_curr->irq_context != hlock_next->irq_context)
2066 break;
2067 hlock_next = hlock;
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 */
2100 if (!hlock->trylock && (hlock->check == 2) &&
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
70a0686a 2519 if (hlock_class(hlock)->key == __lockdep_no_validate__.subkeys)
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
PZ
2558
2559void trace_hardirqs_on_caller(unsigned long ip)
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 */
6afe40b4 2612void 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
4dfbb9d8
PZ
2996 if (subclass)
2997 register_lock_class(lock, subclass, 1);
fbb9ce95 2998}
fbb9ce95
IM
2999EXPORT_SYMBOL_GPL(lockdep_init_map);
3000
1704f47b
PZ
3001struct lock_class_key __lockdep_no_validate__;
3002
fbb9ce95
IM
3003/*
3004 * This gets called for every mutex_lock*()/spin_lock*() operation.
3005 * We maintain the dependency maps and validate the locking attempt:
3006 */
3007static int __lock_acquire(struct lockdep_map *lock, unsigned int subclass,
3008 int trylock, int read, int check, int hardirqs_off,
bb97a91e
PZ
3009 struct lockdep_map *nest_lock, unsigned long ip,
3010 int references)
fbb9ce95
IM
3011{
3012 struct task_struct *curr = current;
d6d897ce 3013 struct lock_class *class = NULL;
fbb9ce95 3014 struct held_lock *hlock;
fbb9ce95
IM
3015 unsigned int depth, id;
3016 int chain_head = 0;
bb97a91e 3017 int class_idx;
fbb9ce95
IM
3018 u64 chain_key;
3019
f20786ff
PZ
3020 if (!prove_locking)
3021 check = 1;
3022
fbb9ce95
IM
3023 if (unlikely(!debug_locks))
3024 return 0;
3025
0119fee4
PZ
3026 /*
3027 * Lockdep should run with IRQs disabled, otherwise we could
3028 * get an interrupt which would want to take locks, which would
3029 * end up in lockdep and have you got a head-ache already?
3030 */
fbb9ce95
IM
3031 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
3032 return 0;
3033
1704f47b
PZ
3034 if (lock->key == &__lockdep_no_validate__)
3035 check = 1;
3036
62016250
HM
3037 if (subclass < NR_LOCKDEP_CACHING_CLASSES)
3038 class = lock->class_cache[subclass];
d6d897ce 3039 /*
62016250 3040 * Not cached?
d6d897ce 3041 */
fbb9ce95 3042 if (unlikely(!class)) {
4dfbb9d8 3043 class = register_lock_class(lock, subclass, 0);
fbb9ce95
IM
3044 if (!class)
3045 return 0;
3046 }
bd6d29c2 3047 atomic_inc((atomic_t *)&class->ops);
fbb9ce95
IM
3048 if (very_verbose(class)) {
3049 printk("\nacquire class [%p] %s", class->key, class->name);
3050 if (class->name_version > 1)
3051 printk("#%d", class->name_version);
3052 printk("\n");
3053 dump_stack();
3054 }
3055
3056 /*
3057 * Add the lock to the list of currently held locks.
3058 * (we dont increase the depth just yet, up until the
3059 * dependency checks are done)
3060 */
3061 depth = curr->lockdep_depth;
0119fee4
PZ
3062 /*
3063 * Ran out of static storage for our per-task lock stack again have we?
3064 */
fbb9ce95
IM
3065 if (DEBUG_LOCKS_WARN_ON(depth >= MAX_LOCK_DEPTH))
3066 return 0;
3067
bb97a91e
PZ
3068 class_idx = class - lock_classes + 1;
3069
3070 if (depth) {
3071 hlock = curr->held_locks + depth - 1;
3072 if (hlock->class_idx == class_idx && nest_lock) {
3073 if (hlock->references)
3074 hlock->references++;
3075 else
3076 hlock->references = 2;
3077
3078 return 1;
3079 }
3080 }
3081
fbb9ce95 3082 hlock = curr->held_locks + depth;
0119fee4
PZ
3083 /*
3084 * Plain impossible, we just registered it and checked it weren't no
3085 * NULL like.. I bet this mushroom I ate was good!
3086 */
f82b217e
DJ
3087 if (DEBUG_LOCKS_WARN_ON(!class))
3088 return 0;
bb97a91e 3089 hlock->class_idx = class_idx;
fbb9ce95
IM
3090 hlock->acquire_ip = ip;
3091 hlock->instance = lock;
7531e2f3 3092 hlock->nest_lock = nest_lock;
fbb9ce95
IM
3093 hlock->trylock = trylock;
3094 hlock->read = read;
3095 hlock->check = check;
6951b12a 3096 hlock->hardirqs_off = !!hardirqs_off;
bb97a91e 3097 hlock->references = references;
f20786ff
PZ
3098#ifdef CONFIG_LOCK_STAT
3099 hlock->waittime_stamp = 0;
3365e779 3100 hlock->holdtime_stamp = lockstat_clock();
f20786ff 3101#endif
fbb9ce95 3102
8e18257d
PZ
3103 if (check == 2 && !mark_irqflags(curr, hlock))
3104 return 0;
3105
fbb9ce95 3106 /* mark it as used: */
4ff773bb 3107 if (!mark_lock(curr, hlock, LOCK_USED))
fbb9ce95 3108 return 0;
8e18257d 3109
fbb9ce95 3110 /*
17aacfb9 3111 * Calculate the chain hash: it's the combined hash of all the
fbb9ce95
IM
3112 * lock keys along the dependency chain. We save the hash value
3113 * at every step so that we can get the current hash easily
3114 * after unlock. The chain hash is then used to cache dependency
3115 * results.
3116 *
3117 * The 'key ID' is what is the most compact key value to drive
3118 * the hash, not class->key.
3119 */
3120 id = class - lock_classes;
0119fee4
PZ
3121 /*
3122 * Whoops, we did it again.. ran straight out of our static allocation.
3123 */
fbb9ce95
IM
3124 if (DEBUG_LOCKS_WARN_ON(id >= MAX_LOCKDEP_KEYS))
3125 return 0;
3126
3127 chain_key = curr->curr_chain_key;
3128 if (!depth) {
0119fee4
PZ
3129 /*
3130 * How can we have a chain hash when we ain't got no keys?!
3131 */
fbb9ce95
IM
3132 if (DEBUG_LOCKS_WARN_ON(chain_key != 0))
3133 return 0;
3134 chain_head = 1;
3135 }
3136
3137 hlock->prev_chain_key = chain_key;
8e18257d
PZ
3138 if (separate_irq_context(curr, hlock)) {
3139 chain_key = 0;
3140 chain_head = 1;
fbb9ce95 3141 }
fbb9ce95 3142 chain_key = iterate_chain_key(chain_key, id);
fbb9ce95 3143
3aa416b0 3144 if (!validate_chain(curr, lock, hlock, chain_head, chain_key))
8e18257d 3145 return 0;
381a2292 3146
3aa416b0 3147 curr->curr_chain_key = chain_key;
fbb9ce95
IM
3148 curr->lockdep_depth++;
3149 check_chain_key(curr);
60e114d1
JP
3150#ifdef CONFIG_DEBUG_LOCKDEP
3151 if (unlikely(!debug_locks))
3152 return 0;
3153#endif
fbb9ce95
IM
3154 if (unlikely(curr->lockdep_depth >= MAX_LOCK_DEPTH)) {
3155 debug_locks_off();
3156 printk("BUG: MAX_LOCK_DEPTH too low!\n");
3157 printk("turning off the locking correctness validator.\n");
eedeeabd 3158 dump_stack();
fbb9ce95
IM
3159 return 0;
3160 }
381a2292 3161
fbb9ce95
IM
3162 if (unlikely(curr->lockdep_depth > max_lockdep_depth))
3163 max_lockdep_depth = curr->lockdep_depth;
3164
3165 return 1;
3166}
3167
3168static int
3169print_unlock_inbalance_bug(struct task_struct *curr, struct lockdep_map *lock,
3170 unsigned long ip)
3171{
3172 if (!debug_locks_off())
3173 return 0;
3174 if (debug_locks_silent)
3175 return 0;
3176
b3fbab05
PM
3177 printk("\n");
3178 printk("=====================================\n");
3179 printk("[ BUG: bad unlock balance detected! ]\n");
fbdc4b9a 3180 print_kernel_ident();
b3fbab05 3181 printk("-------------------------------------\n");
fbb9ce95 3182 printk("%s/%d is trying to release lock (",
ba25f9dc 3183 curr->comm, task_pid_nr(curr));
fbb9ce95
IM
3184 print_lockdep_cache(lock);
3185 printk(") at:\n");
3186 print_ip_sym(ip);
3187 printk("but there are no more locks to release!\n");
3188 printk("\nother info that might help us debug this:\n");
3189 lockdep_print_held_locks(curr);
3190
3191 printk("\nstack backtrace:\n");
3192 dump_stack();
3193
3194 return 0;
3195}
3196
3197/*
3198 * Common debugging checks for both nested and non-nested unlock:
3199 */
3200static int check_unlock(struct task_struct *curr, struct lockdep_map *lock,
3201 unsigned long ip)
3202{
3203 if (unlikely(!debug_locks))
3204 return 0;
0119fee4
PZ
3205 /*
3206 * Lockdep should run with IRQs disabled, recursion, head-ache, etc..
3207 */
fbb9ce95
IM
3208 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
3209 return 0;
3210
3211 if (curr->lockdep_depth <= 0)
3212 return print_unlock_inbalance_bug(curr, lock, ip);
3213
3214 return 1;
3215}
3216
bb97a91e
PZ
3217static int match_held_lock(struct held_lock *hlock, struct lockdep_map *lock)
3218{
3219 if (hlock->instance == lock)
3220 return 1;
3221
3222 if (hlock->references) {
62016250 3223 struct lock_class *class = lock->class_cache[0];
bb97a91e
PZ
3224
3225 if (!class)
3226 class = look_up_lock_class(lock, 0);
3227
80e0401e
PZ
3228 /*
3229 * If look_up_lock_class() failed to find a class, we're trying
3230 * to test if we hold a lock that has never yet been acquired.
3231 * Clearly if the lock hasn't been acquired _ever_, we're not
3232 * holding it either, so report failure.
3233 */
3234 if (!class)
bb97a91e
PZ
3235 return 0;
3236
0119fee4
PZ
3237 /*
3238 * References, but not a lock we're actually ref-counting?
3239 * State got messed up, follow the sites that change ->references
3240 * and try to make sense of it.
3241 */
bb97a91e
PZ
3242 if (DEBUG_LOCKS_WARN_ON(!hlock->nest_lock))
3243 return 0;
3244
3245 if (hlock->class_idx == class - lock_classes + 1)
3246 return 1;
3247 }
3248
3249 return 0;
3250}
3251
64aa348e 3252static int
00ef9f73
PZ
3253__lock_set_class(struct lockdep_map *lock, const char *name,
3254 struct lock_class_key *key, unsigned int subclass,
3255 unsigned long ip)
64aa348e
PZ
3256{
3257 struct task_struct *curr = current;
3258 struct held_lock *hlock, *prev_hlock;
3259 struct lock_class *class;
3260 unsigned int depth;
3261 int i;
3262
3263 depth = curr->lockdep_depth;
0119fee4
PZ
3264 /*
3265 * This function is about (re)setting the class of a held lock,
3266 * yet we're not actually holding any locks. Naughty user!
3267 */
64aa348e
PZ
3268 if (DEBUG_LOCKS_WARN_ON(!depth))
3269 return 0;
3270
3271 prev_hlock = NULL;
3272 for (i = depth-1; i >= 0; i--) {
3273 hlock = curr->held_locks + i;
3274 /*
3275 * We must not cross into another context:
3276 */
3277 if (prev_hlock && prev_hlock->irq_context != hlock->irq_context)
3278 break;
bb97a91e 3279 if (match_held_lock(hlock, lock))
64aa348e
PZ
3280 goto found_it;
3281 prev_hlock = hlock;
3282 }
3283 return print_unlock_inbalance_bug(curr, lock, ip);
3284
3285found_it:
00ef9f73 3286 lockdep_init_map(lock, name, key, 0);
64aa348e 3287 class = register_lock_class(lock, subclass, 0);
f82b217e 3288 hlock->class_idx = class - lock_classes + 1;
64aa348e
PZ
3289
3290 curr->lockdep_depth = i;
3291 curr->curr_chain_key = hlock->prev_chain_key;
3292
3293 for (; i < depth; i++) {
3294 hlock = curr->held_locks + i;
3295 if (!__lock_acquire(hlock->instance,
f82b217e 3296 hlock_class(hlock)->subclass, hlock->trylock,
64aa348e 3297 hlock->read, hlock->check, hlock->hardirqs_off,
bb97a91e
PZ
3298 hlock->nest_lock, hlock->acquire_ip,
3299 hlock->references))
64aa348e
PZ
3300 return 0;
3301 }
3302
0119fee4
PZ
3303 /*
3304 * I took it apart and put it back together again, except now I have
3305 * these 'spare' parts.. where shall I put them.
3306 */
64aa348e
PZ
3307 if (DEBUG_LOCKS_WARN_ON(curr->lockdep_depth != depth))
3308 return 0;
3309 return 1;
3310}
3311
fbb9ce95
IM
3312/*
3313 * Remove the lock to the list of currently held locks in a
3314 * potentially non-nested (out of order) manner. This is a
3315 * relatively rare operation, as all the unlock APIs default
3316 * to nested mode (which uses lock_release()):
3317 */
3318static int
3319lock_release_non_nested(struct task_struct *curr,
3320 struct lockdep_map *lock, unsigned long ip)
3321{
3322 struct held_lock *hlock, *prev_hlock;
3323 unsigned int depth;
3324 int i;
3325
3326 /*
3327 * Check whether the lock exists in the current stack
3328 * of held locks:
3329 */
3330 depth = curr->lockdep_depth;
0119fee4
PZ
3331 /*
3332 * So we're all set to release this lock.. wait what lock? We don't
3333 * own any locks, you've been drinking again?
3334 */
fbb9ce95
IM
3335 if (DEBUG_LOCKS_WARN_ON(!depth))
3336 return 0;
3337
3338 prev_hlock = NULL;
3339 for (i = depth-1; i >= 0; i--) {
3340 hlock = curr->held_locks + i;
3341 /*
3342 * We must not cross into another context:
3343 */
3344 if (prev_hlock && prev_hlock->irq_context != hlock->irq_context)
3345 break;
bb97a91e 3346 if (match_held_lock(hlock, lock))
fbb9ce95
IM
3347 goto found_it;
3348 prev_hlock = hlock;
3349 }
3350 return print_unlock_inbalance_bug(curr, lock, ip);
3351
3352found_it:
bb97a91e
PZ
3353 if (hlock->instance == lock)
3354 lock_release_holdtime(hlock);
3355
3356 if (hlock->references) {
3357 hlock->references--;
3358 if (hlock->references) {
3359 /*
3360 * We had, and after removing one, still have
3361 * references, the current lock stack is still
3362 * valid. We're done!
3363 */
3364 return 1;
3365 }
3366 }
f20786ff 3367
fbb9ce95
IM
3368 /*
3369 * We have the right lock to unlock, 'hlock' points to it.
3370 * Now we remove it from the stack, and add back the other
3371 * entries (if any), recalculating the hash along the way:
3372 */
bb97a91e 3373
fbb9ce95
IM
3374 curr->lockdep_depth = i;
3375 curr->curr_chain_key = hlock->prev_chain_key;
3376
3377 for (i++; i < depth; i++) {
3378 hlock = curr->held_locks + i;
3379 if (!__lock_acquire(hlock->instance,
f82b217e 3380 hlock_class(hlock)->subclass, hlock->trylock,
fbb9ce95 3381 hlock->read, hlock->check, hlock->hardirqs_off,
bb97a91e
PZ
3382 hlock->nest_lock, hlock->acquire_ip,
3383 hlock->references))
fbb9ce95
IM
3384 return 0;
3385 }
3386
0119fee4
PZ
3387 /*
3388 * We had N bottles of beer on the wall, we drank one, but now
3389 * there's not N-1 bottles of beer left on the wall...
3390 */
fbb9ce95
IM
3391 if (DEBUG_LOCKS_WARN_ON(curr->lockdep_depth != depth - 1))
3392 return 0;
3393 return 1;
3394}
3395
3396/*
3397 * Remove the lock to the list of currently held locks - this gets
3398 * called on mutex_unlock()/spin_unlock*() (or on a failed
3399 * mutex_lock_interruptible()). This is done for unlocks that nest
3400 * perfectly. (i.e. the current top of the lock-stack is unlocked)
3401 */
3402static int lock_release_nested(struct task_struct *curr,
3403 struct lockdep_map *lock, unsigned long ip)
3404{
3405 struct held_lock *hlock;
3406 unsigned int depth;
3407
3408 /*
3409 * Pop off the top of the lock stack:
3410 */
3411 depth = curr->lockdep_depth - 1;
3412 hlock = curr->held_locks + depth;
3413
3414 /*
3415 * Is the unlock non-nested:
3416 */
bb97a91e 3417 if (hlock->instance != lock || hlock->references)
fbb9ce95
IM
3418 return lock_release_non_nested(curr, lock, ip);
3419 curr->lockdep_depth--;
3420
0119fee4
PZ
3421 /*
3422 * No more locks, but somehow we've got hash left over, who left it?
3423 */
fbb9ce95
IM
3424 if (DEBUG_LOCKS_WARN_ON(!depth && (hlock->prev_chain_key != 0)))
3425 return 0;
3426
3427 curr->curr_chain_key = hlock->prev_chain_key;
3428
f20786ff
PZ
3429 lock_release_holdtime(hlock);
3430
fbb9ce95
IM
3431#ifdef CONFIG_DEBUG_LOCKDEP
3432 hlock->prev_chain_key = 0;
f82b217e 3433 hlock->class_idx = 0;
fbb9ce95
IM
3434 hlock->acquire_ip = 0;
3435 hlock->irq_context = 0;
3436#endif
3437 return 1;
3438}
3439
3440/*
3441 * Remove the lock to the list of currently held locks - this gets
3442 * called on mutex_unlock()/spin_unlock*() (or on a failed
3443 * mutex_lock_interruptible()). This is done for unlocks that nest
3444 * perfectly. (i.e. the current top of the lock-stack is unlocked)
3445 */
3446static void
3447__lock_release(struct lockdep_map *lock, int nested, unsigned long ip)
3448{
3449 struct task_struct *curr = current;
3450
3451 if (!check_unlock(curr, lock, ip))
3452 return;
3453
3454 if (nested) {
3455 if (!lock_release_nested(curr, lock, ip))
3456 return;
3457 } else {
3458 if (!lock_release_non_nested(curr, lock, ip))
3459 return;
3460 }
3461
3462 check_chain_key(curr);
3463}
3464
f607c668
PZ
3465static int __lock_is_held(struct lockdep_map *lock)
3466{
3467 struct task_struct *curr = current;
3468 int i;
3469
3470 for (i = 0; i < curr->lockdep_depth; i++) {
bb97a91e
PZ
3471 struct held_lock *hlock = curr->held_locks + i;
3472
3473 if (match_held_lock(hlock, lock))
f607c668
PZ
3474 return 1;
3475 }
3476
3477 return 0;
3478}
3479
fbb9ce95
IM
3480/*
3481 * Check whether we follow the irq-flags state precisely:
3482 */
1d09daa5 3483static void check_flags(unsigned long flags)
fbb9ce95 3484{
992860e9
IM
3485#if defined(CONFIG_PROVE_LOCKING) && defined(CONFIG_DEBUG_LOCKDEP) && \
3486 defined(CONFIG_TRACE_IRQFLAGS)
fbb9ce95
IM
3487 if (!debug_locks)
3488 return;
3489
5f9fa8a6
IM
3490 if (irqs_disabled_flags(flags)) {
3491 if (DEBUG_LOCKS_WARN_ON(current->hardirqs_enabled)) {
3492 printk("possible reason: unannotated irqs-off.\n");
3493 }
3494 } else {
3495 if (DEBUG_LOCKS_WARN_ON(!current->hardirqs_enabled)) {
3496 printk("possible reason: unannotated irqs-on.\n");
3497 }
3498 }
fbb9ce95
IM
3499
3500 /*
3501 * We dont accurately track softirq state in e.g.
3502 * hardirq contexts (such as on 4KSTACKS), so only
3503 * check if not in hardirq contexts:
3504 */
3505 if (!hardirq_count()) {
0119fee4
PZ
3506 if (softirq_count()) {
3507 /* like the above, but with softirqs */
fbb9ce95 3508 DEBUG_LOCKS_WARN_ON(current->softirqs_enabled);
0119fee4
PZ
3509 } else {
3510 /* lick the above, does it taste good? */
fbb9ce95 3511 DEBUG_LOCKS_WARN_ON(!current->softirqs_enabled);
0119fee4 3512 }
fbb9ce95
IM
3513 }
3514
3515 if (!debug_locks)
3516 print_irqtrace_events(current);
3517#endif
3518}
3519
00ef9f73
PZ
3520void lock_set_class(struct lockdep_map *lock, const char *name,
3521 struct lock_class_key *key, unsigned int subclass,
3522 unsigned long ip)
64aa348e
PZ
3523{
3524 unsigned long flags;
3525
3526 if (unlikely(current->lockdep_recursion))
3527 return;
3528
3529 raw_local_irq_save(flags);
3530 current->lockdep_recursion = 1;
3531 check_flags(flags);
00ef9f73 3532 if (__lock_set_class(lock, name, key, subclass, ip))
64aa348e
PZ
3533 check_chain_key(current);
3534 current->lockdep_recursion = 0;
3535 raw_local_irq_restore(flags);
3536}
00ef9f73 3537EXPORT_SYMBOL_GPL(lock_set_class);
64aa348e 3538
fbb9ce95
IM
3539/*
3540 * We are not always called with irqs disabled - do that here,
3541 * and also avoid lockdep recursion:
3542 */
1d09daa5 3543void lock_acquire(struct lockdep_map *lock, unsigned int subclass,
7531e2f3
PZ
3544 int trylock, int read, int check,
3545 struct lockdep_map *nest_lock, unsigned long ip)
fbb9ce95
IM
3546{
3547 unsigned long flags;
3548
3549 if (unlikely(current->lockdep_recursion))
3550 return;
3551
3552 raw_local_irq_save(flags);
3553 check_flags(flags);
3554
3555 current->lockdep_recursion = 1;
db2c4c77 3556 trace_lock_acquire(lock, subclass, trylock, read, check, nest_lock, ip);
fbb9ce95 3557 __lock_acquire(lock, subclass, trylock, read, check,
bb97a91e 3558 irqs_disabled_flags(flags), nest_lock, ip, 0);
fbb9ce95
IM
3559 current->lockdep_recursion = 0;
3560 raw_local_irq_restore(flags);
3561}
fbb9ce95
IM
3562EXPORT_SYMBOL_GPL(lock_acquire);
3563
1d09daa5 3564void lock_release(struct lockdep_map *lock, int nested,
0764d23c 3565 unsigned long ip)
fbb9ce95
IM
3566{
3567 unsigned long flags;
3568
3569 if (unlikely(current->lockdep_recursion))
3570 return;
3571
3572 raw_local_irq_save(flags);
3573 check_flags(flags);
3574 current->lockdep_recursion = 1;
93135439 3575 trace_lock_release(lock, ip);
fbb9ce95
IM
3576 __lock_release(lock, nested, ip);
3577 current->lockdep_recursion = 0;
3578 raw_local_irq_restore(flags);
3579}
fbb9ce95
IM
3580EXPORT_SYMBOL_GPL(lock_release);
3581
f607c668
PZ
3582int lock_is_held(struct lockdep_map *lock)
3583{
3584 unsigned long flags;
3585 int ret = 0;
3586
3587 if (unlikely(current->lockdep_recursion))
f2513cde 3588 return 1; /* avoid false negative lockdep_assert_held() */
f607c668
PZ
3589
3590 raw_local_irq_save(flags);
3591 check_flags(flags);
3592
3593 current->lockdep_recursion = 1;
3594 ret = __lock_is_held(lock);
3595 current->lockdep_recursion = 0;
3596 raw_local_irq_restore(flags);
3597
3598 return ret;
3599}
3600EXPORT_SYMBOL_GPL(lock_is_held);
3601
cf40bd16
NP
3602void lockdep_set_current_reclaim_state(gfp_t gfp_mask)
3603{
3604 current->lockdep_reclaim_gfp = gfp_mask;
3605}
3606
3607void lockdep_clear_current_reclaim_state(void)
3608{
3609 current->lockdep_reclaim_gfp = 0;
3610}
3611
f20786ff
PZ
3612#ifdef CONFIG_LOCK_STAT
3613static int
3614print_lock_contention_bug(struct task_struct *curr, struct lockdep_map *lock,
3615 unsigned long ip)
3616{
3617 if (!debug_locks_off())
3618 return 0;
3619 if (debug_locks_silent)
3620 return 0;
3621
b3fbab05
PM
3622 printk("\n");
3623 printk("=================================\n");
3624 printk("[ BUG: bad contention detected! ]\n");
fbdc4b9a 3625 print_kernel_ident();
b3fbab05 3626 printk("---------------------------------\n");
f20786ff 3627 printk("%s/%d is trying to contend lock (",
ba25f9dc 3628 curr->comm, task_pid_nr(curr));
f20786ff
PZ
3629 print_lockdep_cache(lock);
3630 printk(") at:\n");
3631 print_ip_sym(ip);
3632 printk("but there are no locks held!\n");
3633 printk("\nother info that might help us debug this:\n");
3634 lockdep_print_held_locks(curr);
3635
3636 printk("\nstack backtrace:\n");
3637 dump_stack();
3638
3639 return 0;
3640}
3641
3642static void
3643__lock_contended(struct lockdep_map *lock, unsigned long ip)
3644{
3645 struct task_struct *curr = current;
3646 struct held_lock *hlock, *prev_hlock;
3647 struct lock_class_stats *stats;
3648 unsigned int depth;
c7e78cff 3649 int i, contention_point, contending_point;
f20786ff
PZ
3650
3651 depth = curr->lockdep_depth;
0119fee4
PZ
3652 /*
3653 * Whee, we contended on this lock, except it seems we're not
3654 * actually trying to acquire anything much at all..
3655 */
f20786ff
PZ
3656 if (DEBUG_LOCKS_WARN_ON(!depth))
3657 return;
3658
3659 prev_hlock = NULL;
3660 for (i = depth-1; i >= 0; i--) {
3661 hlock = curr->held_locks + i;
3662 /*
3663 * We must not cross into another context:
3664 */
3665 if (prev_hlock && prev_hlock->irq_context != hlock->irq_context)
3666 break;
bb97a91e 3667 if (match_held_lock(hlock, lock))
f20786ff
PZ
3668 goto found_it;
3669 prev_hlock = hlock;
3670 }
3671 print_lock_contention_bug(curr, lock, ip);
3672 return;
3673
3674found_it:
bb97a91e
PZ
3675 if (hlock->instance != lock)
3676 return;
3677
3365e779 3678 hlock->waittime_stamp = lockstat_clock();
f20786ff 3679
c7e78cff
PZ
3680 contention_point = lock_point(hlock_class(hlock)->contention_point, ip);
3681 contending_point = lock_point(hlock_class(hlock)->contending_point,
3682 lock->ip);
f20786ff 3683
f82b217e 3684 stats = get_lock_stats(hlock_class(hlock));
c7e78cff
PZ
3685 if (contention_point < LOCKSTAT_POINTS)
3686 stats->contention_point[contention_point]++;
3687 if (contending_point < LOCKSTAT_POINTS)
3688 stats->contending_point[contending_point]++;
96645678
PZ
3689 if (lock->cpu != smp_processor_id())
3690 stats->bounces[bounce_contended + !!hlock->read]++;
f20786ff
PZ
3691 put_lock_stats(stats);
3692}
3693
3694static void
c7e78cff 3695__lock_acquired(struct lockdep_map *lock, unsigned long ip)
f20786ff
PZ
3696{
3697 struct task_struct *curr = current;
3698 struct held_lock *hlock, *prev_hlock;
3699 struct lock_class_stats *stats;
3700 unsigned int depth;
3365e779 3701 u64 now, waittime = 0;
96645678 3702 int i, cpu;
f20786ff
PZ
3703
3704 depth = curr->lockdep_depth;
0119fee4
PZ
3705 /*
3706 * Yay, we acquired ownership of this lock we didn't try to
3707 * acquire, how the heck did that happen?
3708 */
f20786ff
PZ
3709 if (DEBUG_LOCKS_WARN_ON(!depth))
3710 return;
3711
3712 prev_hlock = NULL;
3713 for (i = depth-1; i >= 0; i--) {
3714 hlock = curr->held_locks + i;
3715 /*
3716 * We must not cross into another context:
3717 */
3718 if (prev_hlock && prev_hlock->irq_context != hlock->irq_context)
3719 break;
bb97a91e 3720 if (match_held_lock(hlock, lock))
f20786ff
PZ
3721 goto found_it;
3722 prev_hlock = hlock;
3723 }
3724 print_lock_contention_bug(curr, lock, _RET_IP_);
3725 return;
3726
3727found_it:
bb97a91e
PZ
3728 if (hlock->instance != lock)
3729 return;
3730
96645678
PZ
3731 cpu = smp_processor_id();
3732 if (hlock->waittime_stamp) {
3365e779 3733 now = lockstat_clock();
96645678
PZ
3734 waittime = now - hlock->waittime_stamp;
3735 hlock->holdtime_stamp = now;
3736 }
f20786ff 3737
883a2a31 3738 trace_lock_acquired(lock, ip);
2062501a 3739
f82b217e 3740 stats = get_lock_stats(hlock_class(hlock));
96645678
PZ
3741 if (waittime) {
3742 if (hlock->read)
3743 lock_time_inc(&stats->read_waittime, waittime);
3744 else
3745 lock_time_inc(&stats->write_waittime, waittime);
3746 }
3747 if (lock->cpu != cpu)
3748 stats->bounces[bounce_acquired + !!hlock->read]++;
f20786ff 3749 put_lock_stats(stats);
96645678
PZ
3750
3751 lock->cpu = cpu;
c7e78cff 3752 lock->ip = ip;
f20786ff
PZ
3753}
3754
3755void lock_contended(struct lockdep_map *lock, unsigned long ip)
3756{
3757 unsigned long flags;
3758
3759 if (unlikely(!lock_stat))
3760 return;
3761
3762 if (unlikely(current->lockdep_recursion))
3763 return;
3764
3765 raw_local_irq_save(flags);
3766 check_flags(flags);
3767 current->lockdep_recursion = 1;
db2c4c77 3768 trace_lock_contended(lock, ip);
f20786ff
PZ
3769 __lock_contended(lock, ip);
3770 current->lockdep_recursion = 0;
3771 raw_local_irq_restore(flags);
3772}
3773EXPORT_SYMBOL_GPL(lock_contended);
3774
c7e78cff 3775void lock_acquired(struct lockdep_map *lock, unsigned long ip)
f20786ff
PZ
3776{
3777 unsigned long flags;
3778
3779 if (unlikely(!lock_stat))
3780 return;
3781
3782 if (unlikely(current->lockdep_recursion))
3783 return;
3784
3785 raw_local_irq_save(flags);
3786 check_flags(flags);
3787 current->lockdep_recursion = 1;
c7e78cff 3788 __lock_acquired(lock, ip);
f20786ff
PZ
3789 current->lockdep_recursion = 0;
3790 raw_local_irq_restore(flags);
3791}
3792EXPORT_SYMBOL_GPL(lock_acquired);
3793#endif
3794
fbb9ce95
IM
3795/*
3796 * Used by the testsuite, sanitize the validator state
3797 * after a simulated failure:
3798 */
3799
3800void lockdep_reset(void)
3801{
3802 unsigned long flags;
23d95a03 3803 int i;
fbb9ce95
IM
3804
3805 raw_local_irq_save(flags);
3806 current->curr_chain_key = 0;
3807 current->lockdep_depth = 0;
3808 current->lockdep_recursion = 0;
3809 memset(current->held_locks, 0, MAX_LOCK_DEPTH*sizeof(struct held_lock));
3810 nr_hardirq_chains = 0;
3811 nr_softirq_chains = 0;
3812 nr_process_chains = 0;
3813 debug_locks = 1;
23d95a03
IM
3814 for (i = 0; i < CHAINHASH_SIZE; i++)
3815 INIT_LIST_HEAD(chainhash_table + i);
fbb9ce95
IM
3816 raw_local_irq_restore(flags);
3817}
3818
3819static void zap_class(struct lock_class *class)
3820{
3821 int i;
3822
3823 /*
3824 * Remove all dependencies this lock is
3825 * involved in:
3826 */
3827 for (i = 0; i < nr_list_entries; i++) {
3828 if (list_entries[i].class == class)
3829 list_del_rcu(&list_entries[i].entry);
3830 }
3831 /*
3832 * Unhash the class and remove it from the all_lock_classes list:
3833 */
3834 list_del_rcu(&class->hash_entry);
3835 list_del_rcu(&class->lock_entry);
3836
8bfe0298 3837 class->key = NULL;
fbb9ce95
IM
3838}
3839
fabe874a 3840static inline int within(const void *addr, void *start, unsigned long size)
fbb9ce95
IM
3841{
3842 return addr >= start && addr < start + size;
3843}
3844
3845void lockdep_free_key_range(void *start, unsigned long size)
3846{
3847 struct lock_class *class, *next;
3848 struct list_head *head;
3849 unsigned long flags;
3850 int i;
5a26db5b 3851 int locked;
fbb9ce95
IM
3852
3853 raw_local_irq_save(flags);
5a26db5b 3854 locked = graph_lock();
fbb9ce95
IM
3855
3856 /*
3857 * Unhash all classes that were created by this module:
3858 */
3859 for (i = 0; i < CLASSHASH_SIZE; i++) {
3860 head = classhash_table + i;
3861 if (list_empty(head))
3862 continue;
fabe874a 3863 list_for_each_entry_safe(class, next, head, hash_entry) {
fbb9ce95
IM
3864 if (within(class->key, start, size))
3865 zap_class(class);
fabe874a
AV
3866 else if (within(class->name, start, size))
3867 zap_class(class);
3868 }
fbb9ce95
IM
3869 }
3870
5a26db5b
NP
3871 if (locked)
3872 graph_unlock();
fbb9ce95
IM
3873 raw_local_irq_restore(flags);
3874}
3875
3876void lockdep_reset_lock(struct lockdep_map *lock)
3877{
d6d897ce 3878 struct lock_class *class, *next;
fbb9ce95
IM
3879 struct list_head *head;
3880 unsigned long flags;
3881 int i, j;
5a26db5b 3882 int locked;
fbb9ce95
IM
3883
3884 raw_local_irq_save(flags);
fbb9ce95
IM
3885
3886 /*
d6d897ce
IM
3887 * Remove all classes this lock might have:
3888 */
3889 for (j = 0; j < MAX_LOCKDEP_SUBCLASSES; j++) {
3890 /*
3891 * If the class exists we look it up and zap it:
3892 */
3893 class = look_up_lock_class(lock, j);
3894 if (class)
3895 zap_class(class);
3896 }
3897 /*
3898 * Debug check: in the end all mapped classes should
3899 * be gone.
fbb9ce95 3900 */
5a26db5b 3901 locked = graph_lock();
fbb9ce95
IM
3902 for (i = 0; i < CLASSHASH_SIZE; i++) {
3903 head = classhash_table + i;
3904 if (list_empty(head))
3905 continue;
3906 list_for_each_entry_safe(class, next, head, hash_entry) {
62016250
HM
3907 int match = 0;
3908
3909 for (j = 0; j < NR_LOCKDEP_CACHING_CLASSES; j++)
3910 match |= class == lock->class_cache[j];
3911
3912 if (unlikely(match)) {
0119fee4
PZ
3913 if (debug_locks_off_graph_unlock()) {
3914 /*
3915 * We all just reset everything, how did it match?
3916 */
74c383f1 3917 WARN_ON(1);
0119fee4 3918 }
d6d897ce 3919 goto out_restore;
fbb9ce95
IM
3920 }
3921 }
3922 }
5a26db5b
NP
3923 if (locked)
3924 graph_unlock();
d6d897ce
IM
3925
3926out_restore:
fbb9ce95
IM
3927 raw_local_irq_restore(flags);
3928}
3929
1499993c 3930void lockdep_init(void)
fbb9ce95
IM
3931{
3932 int i;
3933
3934 /*
3935 * Some architectures have their own start_kernel()
3936 * code which calls lockdep_init(), while we also
3937 * call lockdep_init() from the start_kernel() itself,
3938 * and we want to initialize the hashes only once:
3939 */
3940 if (lockdep_initialized)
3941 return;
3942
3943 for (i = 0; i < CLASSHASH_SIZE; i++)
3944 INIT_LIST_HEAD(classhash_table + i);
3945
3946 for (i = 0; i < CHAINHASH_SIZE; i++)
3947 INIT_LIST_HEAD(chainhash_table + i);
3948
3949 lockdep_initialized = 1;
3950}
3951
3952void __init lockdep_info(void)
3953{
3954 printk("Lock dependency validator: Copyright (c) 2006 Red Hat, Inc., Ingo Molnar\n");
3955
b0788caf 3956 printk("... MAX_LOCKDEP_SUBCLASSES: %lu\n", MAX_LOCKDEP_SUBCLASSES);
fbb9ce95
IM
3957 printk("... MAX_LOCK_DEPTH: %lu\n", MAX_LOCK_DEPTH);
3958 printk("... MAX_LOCKDEP_KEYS: %lu\n", MAX_LOCKDEP_KEYS);
b0788caf 3959 printk("... CLASSHASH_SIZE: %lu\n", CLASSHASH_SIZE);
fbb9ce95
IM
3960 printk("... MAX_LOCKDEP_ENTRIES: %lu\n", MAX_LOCKDEP_ENTRIES);
3961 printk("... MAX_LOCKDEP_CHAINS: %lu\n", MAX_LOCKDEP_CHAINS);
3962 printk("... CHAINHASH_SIZE: %lu\n", CHAINHASH_SIZE);
3963
3964 printk(" memory used by lock dependency info: %lu kB\n",
3965 (sizeof(struct lock_class) * MAX_LOCKDEP_KEYS +
3966 sizeof(struct list_head) * CLASSHASH_SIZE +
3967 sizeof(struct lock_list) * MAX_LOCKDEP_ENTRIES +
3968 sizeof(struct lock_chain) * MAX_LOCKDEP_CHAINS +
90629209 3969 sizeof(struct list_head) * CHAINHASH_SIZE
4dd861d6 3970#ifdef CONFIG_PROVE_LOCKING
e351b660 3971 + sizeof(struct circular_queue)
4dd861d6 3972#endif
90629209 3973 ) / 1024
4dd861d6 3974 );
fbb9ce95
IM
3975
3976 printk(" per task-struct memory footprint: %lu bytes\n",
3977 sizeof(struct held_lock) * MAX_LOCK_DEPTH);
3978
3979#ifdef CONFIG_DEBUG_LOCKDEP
c71063c9
JB
3980 if (lockdep_init_error) {
3981 printk("WARNING: lockdep init error! Arch code didn't call lockdep_init() early enough?\n");
3982 printk("Call stack leading to lockdep invocation was:\n");
3983 print_stack_trace(&lockdep_init_trace, 0);
3984 }
fbb9ce95
IM
3985#endif
3986}
3987
fbb9ce95
IM
3988static void
3989print_freed_lock_bug(struct task_struct *curr, const void *mem_from,
55794a41 3990 const void *mem_to, struct held_lock *hlock)
fbb9ce95
IM
3991{
3992 if (!debug_locks_off())
3993 return;
3994 if (debug_locks_silent)
3995 return;
3996
b3fbab05
PM
3997 printk("\n");
3998 printk("=========================\n");
3999 printk("[ BUG: held lock freed! ]\n");
fbdc4b9a 4000 print_kernel_ident();
b3fbab05 4001 printk("-------------------------\n");
fbb9ce95 4002 printk("%s/%d is freeing memory %p-%p, with a lock still held there!\n",
ba25f9dc 4003 curr->comm, task_pid_nr(curr), mem_from, mem_to-1);
55794a41 4004 print_lock(hlock);
fbb9ce95
IM
4005 lockdep_print_held_locks(curr);
4006
4007 printk("\nstack backtrace:\n");
4008 dump_stack();
4009}
4010
54561783
ON
4011static inline int not_in_range(const void* mem_from, unsigned long mem_len,
4012 const void* lock_from, unsigned long lock_len)
4013{
4014 return lock_from + lock_len <= mem_from ||
4015 mem_from + mem_len <= lock_from;
4016}
4017
fbb9ce95
IM
4018/*
4019 * Called when kernel memory is freed (or unmapped), or if a lock
4020 * is destroyed or reinitialized - this code checks whether there is
4021 * any held lock in the memory range of <from> to <to>:
4022 */
4023void debug_check_no_locks_freed(const void *mem_from, unsigned long mem_len)
4024{
fbb9ce95
IM
4025 struct task_struct *curr = current;
4026 struct held_lock *hlock;
4027 unsigned long flags;
4028 int i;
4029
4030 if (unlikely(!debug_locks))
4031 return;
4032
4033 local_irq_save(flags);
4034 for (i = 0; i < curr->lockdep_depth; i++) {
4035 hlock = curr->held_locks + i;
4036
54561783
ON
4037 if (not_in_range(mem_from, mem_len, hlock->instance,
4038 sizeof(*hlock->instance)))
fbb9ce95
IM
4039 continue;
4040
54561783 4041 print_freed_lock_bug(curr, mem_from, mem_from + mem_len, hlock);
fbb9ce95
IM
4042 break;
4043 }
4044 local_irq_restore(flags);
4045}
ed07536e 4046EXPORT_SYMBOL_GPL(debug_check_no_locks_freed);
fbb9ce95
IM
4047
4048static void print_held_locks_bug(struct task_struct *curr)
4049{
4050 if (!debug_locks_off())
4051 return;
4052 if (debug_locks_silent)
4053 return;
4054
b3fbab05
PM
4055 printk("\n");
4056 printk("=====================================\n");
4057 printk("[ BUG: lock held at task exit time! ]\n");
fbdc4b9a 4058 print_kernel_ident();
b3fbab05 4059 printk("-------------------------------------\n");
fbb9ce95 4060 printk("%s/%d is exiting with locks still held!\n",
ba25f9dc 4061 curr->comm, task_pid_nr(curr));
fbb9ce95
IM
4062 lockdep_print_held_locks(curr);
4063
4064 printk("\nstack backtrace:\n");
4065 dump_stack();
4066}
4067
4068void debug_check_no_locks_held(struct task_struct *task)
4069{
4070 if (unlikely(task->lockdep_depth > 0))
4071 print_held_locks_bug(task);
4072}
4073
4074void debug_show_all_locks(void)
4075{
4076 struct task_struct *g, *p;
4077 int count = 10;
4078 int unlock = 1;
4079
9c35dd7f
JP
4080 if (unlikely(!debug_locks)) {
4081 printk("INFO: lockdep is turned off.\n");
4082 return;
4083 }
fbb9ce95
IM
4084 printk("\nShowing all locks held in the system:\n");
4085
4086 /*
4087 * Here we try to get the tasklist_lock as hard as possible,
4088 * if not successful after 2 seconds we ignore it (but keep
4089 * trying). This is to enable a debug printout even if a
4090 * tasklist_lock-holding task deadlocks or crashes.
4091 */
4092retry:
4093 if (!read_trylock(&tasklist_lock)) {
4094 if (count == 10)
4095 printk("hm, tasklist_lock locked, retrying... ");
4096 if (count) {
4097 count--;
4098 printk(" #%d", 10-count);
4099 mdelay(200);
4100 goto retry;
4101 }
4102 printk(" ignoring it.\n");
4103 unlock = 0;
46fec7ac 4104 } else {
4105 if (count != 10)
4106 printk(KERN_CONT " locked it.\n");
fbb9ce95 4107 }
fbb9ce95
IM
4108
4109 do_each_thread(g, p) {
85684873
IM
4110 /*
4111 * It's not reliable to print a task's held locks
4112 * if it's not sleeping (or if it's not the current
4113 * task):
4114 */
4115 if (p->state == TASK_RUNNING && p != current)
4116 continue;
fbb9ce95
IM
4117 if (p->lockdep_depth)
4118 lockdep_print_held_locks(p);
4119 if (!unlock)
4120 if (read_trylock(&tasklist_lock))
4121 unlock = 1;
4122 } while_each_thread(g, p);
4123
4124 printk("\n");
4125 printk("=============================================\n\n");
4126
4127 if (unlock)
4128 read_unlock(&tasklist_lock);
4129}
fbb9ce95
IM
4130EXPORT_SYMBOL_GPL(debug_show_all_locks);
4131
82a1fcb9
IM
4132/*
4133 * Careful: only use this function if you are sure that
4134 * the task cannot run in parallel!
4135 */
f1b499f0 4136void debug_show_held_locks(struct task_struct *task)
fbb9ce95 4137{
9c35dd7f
JP
4138 if (unlikely(!debug_locks)) {
4139 printk("INFO: lockdep is turned off.\n");
4140 return;
4141 }
fbb9ce95
IM
4142 lockdep_print_held_locks(task);
4143}
fbb9ce95 4144EXPORT_SYMBOL_GPL(debug_show_held_locks);
b351d164
PZ
4145
4146void lockdep_sys_exit(void)
4147{
4148 struct task_struct *curr = current;
4149
4150 if (unlikely(curr->lockdep_depth)) {
4151 if (!debug_locks_off())
4152 return;
b3fbab05
PM
4153 printk("\n");
4154 printk("================================================\n");
4155 printk("[ BUG: lock held when returning to user space! ]\n");
fbdc4b9a 4156 print_kernel_ident();
b3fbab05 4157 printk("------------------------------------------------\n");
b351d164
PZ
4158 printk("%s/%d is leaving the kernel with locks still held!\n",
4159 curr->comm, curr->pid);
4160 lockdep_print_held_locks(curr);
4161 }
4162}
0632eb3d 4163
b3fbab05 4164void lockdep_rcu_suspicious(const char *file, const int line, const char *s)
0632eb3d
PM
4165{
4166 struct task_struct *curr = current;
4167
2b3fc35f 4168#ifndef CONFIG_PROVE_RCU_REPEATEDLY
0632eb3d
PM
4169 if (!debug_locks_off())
4170 return;
2b3fc35f
LJ
4171#endif /* #ifdef CONFIG_PROVE_RCU_REPEATEDLY */
4172 /* Note: the following can be executed concurrently, so be careful. */
b3fbab05
PM
4173 printk("\n");
4174 printk("===============================\n");
4175 printk("[ INFO: suspicious RCU usage. ]\n");
fbdc4b9a 4176 print_kernel_ident();
b3fbab05
PM
4177 printk("-------------------------------\n");
4178 printk("%s:%d %s!\n", file, line, s);
0632eb3d 4179 printk("\nother info that might help us debug this:\n\n");
cc5b83a9 4180 printk("\nrcu_scheduler_active = %d, debug_locks = %d\n", rcu_scheduler_active, debug_locks);
0632eb3d
PM
4181 lockdep_print_held_locks(curr);
4182 printk("\nstack backtrace:\n");
4183 dump_stack();
4184}
b3fbab05 4185EXPORT_SYMBOL_GPL(lockdep_rcu_suspicious);