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