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