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