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