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