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