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