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