blob: 46a404173db231a982baf3941c72e96911003906 [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
Dave Jonesf82b2172008-08-11 09:30:23 +0200127static inline struct lock_class *hlock_class(struct held_lock *hlock)
128{
129 if (!hlock->class_idx) {
130 DEBUG_LOCKS_WARN_ON(1);
131 return NULL;
132 }
133 return lock_classes + hlock->class_idx - 1;
134}
135
Peter Zijlstraf20786f2007-07-19 01:48:56 -0700136#ifdef CONFIG_LOCK_STAT
137static DEFINE_PER_CPU(struct lock_class_stats[MAX_LOCKDEP_KEYS], lock_stats);
138
139static int lock_contention_point(struct lock_class *class, unsigned long ip)
140{
141 int i;
142
143 for (i = 0; i < ARRAY_SIZE(class->contention_point); i++) {
144 if (class->contention_point[i] == 0) {
145 class->contention_point[i] = ip;
146 break;
147 }
148 if (class->contention_point[i] == ip)
149 break;
150 }
151
152 return i;
153}
154
155static void lock_time_inc(struct lock_time *lt, s64 time)
156{
157 if (time > lt->max)
158 lt->max = time;
159
160 if (time < lt->min || !lt->min)
161 lt->min = time;
162
163 lt->total += time;
164 lt->nr++;
165}
166
Peter Zijlstrac46261d2007-07-19 01:48:57 -0700167static inline void lock_time_add(struct lock_time *src, struct lock_time *dst)
168{
169 dst->min += src->min;
170 dst->max += src->max;
171 dst->total += src->total;
172 dst->nr += src->nr;
173}
174
175struct lock_class_stats lock_stats(struct lock_class *class)
176{
177 struct lock_class_stats stats;
178 int cpu, i;
179
180 memset(&stats, 0, sizeof(struct lock_class_stats));
181 for_each_possible_cpu(cpu) {
182 struct lock_class_stats *pcs =
183 &per_cpu(lock_stats, cpu)[class - lock_classes];
184
185 for (i = 0; i < ARRAY_SIZE(stats.contention_point); i++)
186 stats.contention_point[i] += pcs->contention_point[i];
187
188 lock_time_add(&pcs->read_waittime, &stats.read_waittime);
189 lock_time_add(&pcs->write_waittime, &stats.write_waittime);
190
191 lock_time_add(&pcs->read_holdtime, &stats.read_holdtime);
192 lock_time_add(&pcs->write_holdtime, &stats.write_holdtime);
Peter Zijlstra96645672007-07-19 01:49:00 -0700193
194 for (i = 0; i < ARRAY_SIZE(stats.bounces); i++)
195 stats.bounces[i] += pcs->bounces[i];
Peter Zijlstrac46261d2007-07-19 01:48:57 -0700196 }
197
198 return stats;
199}
200
201void clear_lock_stats(struct lock_class *class)
202{
203 int cpu;
204
205 for_each_possible_cpu(cpu) {
206 struct lock_class_stats *cpu_stats =
207 &per_cpu(lock_stats, cpu)[class - lock_classes];
208
209 memset(cpu_stats, 0, sizeof(struct lock_class_stats));
210 }
211 memset(class->contention_point, 0, sizeof(class->contention_point));
212}
213
Peter Zijlstraf20786f2007-07-19 01:48:56 -0700214static struct lock_class_stats *get_lock_stats(struct lock_class *class)
215{
216 return &get_cpu_var(lock_stats)[class - lock_classes];
217}
218
219static void put_lock_stats(struct lock_class_stats *stats)
220{
221 put_cpu_var(lock_stats);
222}
223
224static void lock_release_holdtime(struct held_lock *hlock)
225{
226 struct lock_class_stats *stats;
227 s64 holdtime;
228
229 if (!lock_stat)
230 return;
231
232 holdtime = sched_clock() - hlock->holdtime_stamp;
233
Dave Jonesf82b2172008-08-11 09:30:23 +0200234 stats = get_lock_stats(hlock_class(hlock));
Peter Zijlstraf20786f2007-07-19 01:48:56 -0700235 if (hlock->read)
236 lock_time_inc(&stats->read_holdtime, holdtime);
237 else
238 lock_time_inc(&stats->write_holdtime, holdtime);
239 put_lock_stats(stats);
240}
241#else
242static inline void lock_release_holdtime(struct held_lock *hlock)
243{
244}
245#endif
246
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700247/*
248 * We keep a global list of all lock classes. The list only grows,
249 * never shrinks. The list is only accessed with the lockdep
250 * spinlock lock held.
251 */
252LIST_HEAD(all_lock_classes);
253
254/*
255 * The lockdep classes are in a hash-table as well, for fast lookup:
256 */
257#define CLASSHASH_BITS (MAX_LOCKDEP_KEYS_BITS - 1)
258#define CLASSHASH_SIZE (1UL << CLASSHASH_BITS)
Peter Zijlstra4b32d0a2007-07-19 01:48:59 -0700259#define __classhashfn(key) hash_long((unsigned long)key, CLASSHASH_BITS)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700260#define classhashentry(key) (classhash_table + __classhashfn((key)))
261
262static struct list_head classhash_table[CLASSHASH_SIZE];
263
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700264/*
265 * We put the lock dependency chains into a hash-table as well, to cache
266 * their existence:
267 */
268#define CHAINHASH_BITS (MAX_LOCKDEP_CHAINS_BITS-1)
269#define CHAINHASH_SIZE (1UL << CHAINHASH_BITS)
Peter Zijlstra4b32d0a2007-07-19 01:48:59 -0700270#define __chainhashfn(chain) hash_long(chain, CHAINHASH_BITS)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700271#define chainhashentry(chain) (chainhash_table + __chainhashfn((chain)))
272
273static struct list_head chainhash_table[CHAINHASH_SIZE];
274
275/*
276 * The hash key of the lock dependency chains is a hash itself too:
277 * it's a hash of all locks taken up to that lock, including that lock.
278 * It's a 64-bit hash, because it's important for the keys to be
279 * unique.
280 */
281#define iterate_chain_key(key1, key2) \
Ingo Molnar03cbc352006-09-29 02:01:46 -0700282 (((key1) << MAX_LOCKDEP_KEYS_BITS) ^ \
283 ((key1) >> (64-MAX_LOCKDEP_KEYS_BITS)) ^ \
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700284 (key2))
285
Steven Rostedt1d09daa2008-05-12 21:20:55 +0200286void lockdep_off(void)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700287{
288 current->lockdep_recursion++;
289}
290
291EXPORT_SYMBOL(lockdep_off);
292
Steven Rostedt1d09daa2008-05-12 21:20:55 +0200293void lockdep_on(void)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700294{
295 current->lockdep_recursion--;
296}
297
298EXPORT_SYMBOL(lockdep_on);
299
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700300/*
301 * Debugging switches:
302 */
303
304#define VERBOSE 0
Ingo Molnar33e94e92006-12-13 00:34:41 -0800305#define VERY_VERBOSE 0
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700306
307#if VERBOSE
308# define HARDIRQ_VERBOSE 1
309# define SOFTIRQ_VERBOSE 1
310#else
311# define HARDIRQ_VERBOSE 0
312# define SOFTIRQ_VERBOSE 0
313#endif
314
315#if VERBOSE || HARDIRQ_VERBOSE || SOFTIRQ_VERBOSE
316/*
317 * Quick filtering for interesting events:
318 */
319static int class_filter(struct lock_class *class)
320{
Andi Kleenf9829cc2006-07-10 04:44:01 -0700321#if 0
322 /* Example */
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700323 if (class->name_version == 1 &&
Andi Kleenf9829cc2006-07-10 04:44:01 -0700324 !strcmp(class->name, "lockname"))
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700325 return 1;
326 if (class->name_version == 1 &&
Andi Kleenf9829cc2006-07-10 04:44:01 -0700327 !strcmp(class->name, "&struct->lockfield"))
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700328 return 1;
Andi Kleenf9829cc2006-07-10 04:44:01 -0700329#endif
Ingo Molnara6640892006-12-13 00:34:39 -0800330 /* Filter everything else. 1 would be to allow everything else */
331 return 0;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700332}
333#endif
334
335static int verbose(struct lock_class *class)
336{
337#if VERBOSE
338 return class_filter(class);
339#endif
340 return 0;
341}
342
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700343/*
344 * Stack-trace: tightly packed array of stack backtrace
Ingo Molnar74c383f2006-12-13 00:34:43 -0800345 * addresses. Protected by the graph_lock.
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700346 */
347unsigned long nr_stack_trace_entries;
348static unsigned long stack_trace[MAX_STACK_TRACE_ENTRIES];
349
350static int save_trace(struct stack_trace *trace)
351{
352 trace->nr_entries = 0;
353 trace->max_entries = MAX_STACK_TRACE_ENTRIES - nr_stack_trace_entries;
354 trace->entries = stack_trace + nr_stack_trace_entries;
355
Andi Kleen5a1b3992006-09-26 10:52:34 +0200356 trace->skip = 3;
Andi Kleen5a1b3992006-09-26 10:52:34 +0200357
Christoph Hellwigab1b6f02007-05-08 00:23:29 -0700358 save_stack_trace(trace);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700359
360 trace->max_entries = trace->nr_entries;
361
362 nr_stack_trace_entries += trace->nr_entries;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700363
364 if (nr_stack_trace_entries == MAX_STACK_TRACE_ENTRIES) {
Ingo Molnar74c383f2006-12-13 00:34:43 -0800365 if (!debug_locks_off_graph_unlock())
366 return 0;
367
368 printk("BUG: MAX_STACK_TRACE_ENTRIES too low!\n");
369 printk("turning off the locking correctness validator.\n");
370 dump_stack();
371
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700372 return 0;
373 }
374
375 return 1;
376}
377
378unsigned int nr_hardirq_chains;
379unsigned int nr_softirq_chains;
380unsigned int nr_process_chains;
381unsigned int max_lockdep_depth;
382unsigned int max_recursion_depth;
383
David Miller419ca3f2008-07-29 21:45:03 -0700384static unsigned int lockdep_dependency_gen_id;
385
386static bool lockdep_dependency_visit(struct lock_class *source,
387 unsigned int depth)
388{
389 if (!depth)
390 lockdep_dependency_gen_id++;
391 if (source->dep_gen_id == lockdep_dependency_gen_id)
392 return true;
393 source->dep_gen_id = lockdep_dependency_gen_id;
394 return false;
395}
396
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700397#ifdef CONFIG_DEBUG_LOCKDEP
398/*
399 * We cannot printk in early bootup code. Not even early_printk()
400 * might work. So we mark any initialization errors and printk
401 * about it later on, in lockdep_info().
402 */
403static int lockdep_init_error;
Johannes Bergc71063c2007-07-19 01:49:02 -0700404static unsigned long lockdep_init_trace_data[20];
405static struct stack_trace lockdep_init_trace = {
406 .max_entries = ARRAY_SIZE(lockdep_init_trace_data),
407 .entries = lockdep_init_trace_data,
408};
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700409
410/*
411 * Various lockdep statistics:
412 */
413atomic_t chain_lookup_hits;
414atomic_t chain_lookup_misses;
415atomic_t hardirqs_on_events;
416atomic_t hardirqs_off_events;
417atomic_t redundant_hardirqs_on;
418atomic_t redundant_hardirqs_off;
419atomic_t softirqs_on_events;
420atomic_t softirqs_off_events;
421atomic_t redundant_softirqs_on;
422atomic_t redundant_softirqs_off;
423atomic_t nr_unused_locks;
424atomic_t nr_cyclic_checks;
425atomic_t nr_cyclic_check_recursions;
426atomic_t nr_find_usage_forwards_checks;
427atomic_t nr_find_usage_forwards_recursions;
428atomic_t nr_find_usage_backwards_checks;
429atomic_t nr_find_usage_backwards_recursions;
430# define debug_atomic_inc(ptr) atomic_inc(ptr)
431# define debug_atomic_dec(ptr) atomic_dec(ptr)
432# define debug_atomic_read(ptr) atomic_read(ptr)
433#else
434# define debug_atomic_inc(ptr) do { } while (0)
435# define debug_atomic_dec(ptr) do { } while (0)
436# define debug_atomic_read(ptr) 0
437#endif
438
439/*
440 * Locking printouts:
441 */
442
443static const char *usage_str[] =
444{
445 [LOCK_USED] = "initial-use ",
446 [LOCK_USED_IN_HARDIRQ] = "in-hardirq-W",
447 [LOCK_USED_IN_SOFTIRQ] = "in-softirq-W",
448 [LOCK_ENABLED_SOFTIRQS] = "softirq-on-W",
449 [LOCK_ENABLED_HARDIRQS] = "hardirq-on-W",
450 [LOCK_USED_IN_HARDIRQ_READ] = "in-hardirq-R",
451 [LOCK_USED_IN_SOFTIRQ_READ] = "in-softirq-R",
452 [LOCK_ENABLED_SOFTIRQS_READ] = "softirq-on-R",
453 [LOCK_ENABLED_HARDIRQS_READ] = "hardirq-on-R",
454};
455
456const char * __get_key_name(struct lockdep_subclass_key *key, char *str)
457{
Alexey Dobriyanffb45122007-05-08 00:28:41 -0700458 return kallsyms_lookup((unsigned long)key, NULL, NULL, NULL, str);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700459}
460
461void
462get_usage_chars(struct lock_class *class, char *c1, char *c2, char *c3, char *c4)
463{
464 *c1 = '.', *c2 = '.', *c3 = '.', *c4 = '.';
465
466 if (class->usage_mask & LOCKF_USED_IN_HARDIRQ)
467 *c1 = '+';
468 else
469 if (class->usage_mask & LOCKF_ENABLED_HARDIRQS)
470 *c1 = '-';
471
472 if (class->usage_mask & LOCKF_USED_IN_SOFTIRQ)
473 *c2 = '+';
474 else
475 if (class->usage_mask & LOCKF_ENABLED_SOFTIRQS)
476 *c2 = '-';
477
478 if (class->usage_mask & LOCKF_ENABLED_HARDIRQS_READ)
479 *c3 = '-';
480 if (class->usage_mask & LOCKF_USED_IN_HARDIRQ_READ) {
481 *c3 = '+';
482 if (class->usage_mask & LOCKF_ENABLED_HARDIRQS_READ)
483 *c3 = '?';
484 }
485
486 if (class->usage_mask & LOCKF_ENABLED_SOFTIRQS_READ)
487 *c4 = '-';
488 if (class->usage_mask & LOCKF_USED_IN_SOFTIRQ_READ) {
489 *c4 = '+';
490 if (class->usage_mask & LOCKF_ENABLED_SOFTIRQS_READ)
491 *c4 = '?';
492 }
493}
494
495static void print_lock_name(struct lock_class *class)
496{
Tejun Heo9281ace2007-07-17 04:03:51 -0700497 char str[KSYM_NAME_LEN], c1, c2, c3, c4;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700498 const char *name;
499
500 get_usage_chars(class, &c1, &c2, &c3, &c4);
501
502 name = class->name;
503 if (!name) {
504 name = __get_key_name(class->key, str);
505 printk(" (%s", name);
506 } else {
507 printk(" (%s", name);
508 if (class->name_version > 1)
509 printk("#%d", class->name_version);
510 if (class->subclass)
511 printk("/%d", class->subclass);
512 }
513 printk("){%c%c%c%c}", c1, c2, c3, c4);
514}
515
516static void print_lockdep_cache(struct lockdep_map *lock)
517{
518 const char *name;
Tejun Heo9281ace2007-07-17 04:03:51 -0700519 char str[KSYM_NAME_LEN];
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700520
521 name = lock->name;
522 if (!name)
523 name = __get_key_name(lock->key->subkeys, str);
524
525 printk("%s", name);
526}
527
528static void print_lock(struct held_lock *hlock)
529{
Dave Jonesf82b2172008-08-11 09:30:23 +0200530 print_lock_name(hlock_class(hlock));
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700531 printk(", at: ");
532 print_ip_sym(hlock->acquire_ip);
533}
534
535static void lockdep_print_held_locks(struct task_struct *curr)
536{
537 int i, depth = curr->lockdep_depth;
538
539 if (!depth) {
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -0700540 printk("no locks held by %s/%d.\n", curr->comm, task_pid_nr(curr));
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700541 return;
542 }
543 printk("%d lock%s held by %s/%d:\n",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -0700544 depth, depth > 1 ? "s" : "", curr->comm, task_pid_nr(curr));
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700545
546 for (i = 0; i < depth; i++) {
547 printk(" #%d: ", i);
548 print_lock(curr->held_locks + i);
549 }
550}
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700551
552static void print_lock_class_header(struct lock_class *class, int depth)
553{
554 int bit;
555
Andi Kleenf9829cc2006-07-10 04:44:01 -0700556 printk("%*s->", depth, "");
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700557 print_lock_name(class);
558 printk(" ops: %lu", class->ops);
559 printk(" {\n");
560
561 for (bit = 0; bit < LOCK_USAGE_STATES; bit++) {
562 if (class->usage_mask & (1 << bit)) {
563 int len = depth;
564
Andi Kleenf9829cc2006-07-10 04:44:01 -0700565 len += printk("%*s %s", depth, "", usage_str[bit]);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700566 len += printk(" at:\n");
567 print_stack_trace(class->usage_traces + bit, len);
568 }
569 }
Andi Kleenf9829cc2006-07-10 04:44:01 -0700570 printk("%*s }\n", depth, "");
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700571
Andi Kleenf9829cc2006-07-10 04:44:01 -0700572 printk("%*s ... key at: ",depth,"");
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700573 print_ip_sym((unsigned long)class->key);
574}
575
576/*
577 * printk all lock dependencies starting at <entry>:
578 */
579static void print_lock_dependencies(struct lock_class *class, int depth)
580{
581 struct lock_list *entry;
582
David Miller419ca3f2008-07-29 21:45:03 -0700583 if (lockdep_dependency_visit(class, depth))
584 return;
585
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700586 if (DEBUG_LOCKS_WARN_ON(depth >= 20))
587 return;
588
589 print_lock_class_header(class, depth);
590
591 list_for_each_entry(entry, &class->locks_after, entry) {
Jarek Poplawskib23984d2006-12-06 20:36:23 -0800592 if (DEBUG_LOCKS_WARN_ON(!entry->class))
593 return;
594
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700595 print_lock_dependencies(entry->class, depth + 1);
596
Andi Kleenf9829cc2006-07-10 04:44:01 -0700597 printk("%*s ... acquired at:\n",depth,"");
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700598 print_stack_trace(&entry->trace, 2);
599 printk("\n");
600 }
601}
602
Dave Jones99de0552006-09-29 02:00:10 -0700603static void print_kernel_version(void)
604{
Serge E. Hallyn96b644b2006-10-02 02:18:13 -0700605 printk("%s %.*s\n", init_utsname()->release,
606 (int)strcspn(init_utsname()->version, " "),
607 init_utsname()->version);
Dave Jones99de0552006-09-29 02:00:10 -0700608}
609
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700610static int very_verbose(struct lock_class *class)
611{
612#if VERY_VERBOSE
613 return class_filter(class);
614#endif
615 return 0;
616}
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700617
618/*
619 * Is this the address of a static object:
620 */
621static int static_obj(void *obj)
622{
623 unsigned long start = (unsigned long) &_stext,
624 end = (unsigned long) &_end,
625 addr = (unsigned long) obj;
626#ifdef CONFIG_SMP
627 int i;
628#endif
629
630 /*
631 * static variable?
632 */
633 if ((addr >= start) && (addr < end))
634 return 1;
635
636#ifdef CONFIG_SMP
637 /*
638 * percpu var?
639 */
640 for_each_possible_cpu(i) {
641 start = (unsigned long) &__per_cpu_start + per_cpu_offset(i);
Ingo Molnar1ff56832006-11-17 19:57:22 +0100642 end = (unsigned long) &__per_cpu_start + PERCPU_ENOUGH_ROOM
643 + per_cpu_offset(i);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700644
645 if ((addr >= start) && (addr < end))
646 return 1;
647 }
648#endif
649
650 /*
651 * module var?
652 */
653 return is_module_address(addr);
654}
655
656/*
657 * To make lock name printouts unique, we calculate a unique
658 * class->name_version generation counter:
659 */
660static int count_matching_names(struct lock_class *new_class)
661{
662 struct lock_class *class;
663 int count = 0;
664
665 if (!new_class->name)
666 return 0;
667
668 list_for_each_entry(class, &all_lock_classes, lock_entry) {
669 if (new_class->key - new_class->subclass == class->key)
670 return class->name_version;
671 if (class->name && !strcmp(class->name, new_class->name))
672 count = max(count, class->name_version);
673 }
674
675 return count + 1;
676}
677
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700678/*
679 * Register a lock's class in the hash-table, if the class is not present
680 * yet. Otherwise we look it up. We cache the result in the lock object
681 * itself, so actual lookup of the hash should be once per lock object.
682 */
683static inline struct lock_class *
Ingo Molnard6d897c2006-07-10 04:44:04 -0700684look_up_lock_class(struct lockdep_map *lock, unsigned int subclass)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700685{
686 struct lockdep_subclass_key *key;
687 struct list_head *hash_head;
688 struct lock_class *class;
689
690#ifdef CONFIG_DEBUG_LOCKDEP
691 /*
692 * If the architecture calls into lockdep before initializing
693 * the hashes then we'll warn about it later. (we cannot printk
694 * right now)
695 */
696 if (unlikely(!lockdep_initialized)) {
697 lockdep_init();
698 lockdep_init_error = 1;
Johannes Bergc71063c2007-07-19 01:49:02 -0700699 save_stack_trace(&lockdep_init_trace);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700700 }
701#endif
702
703 /*
704 * Static locks do not have their class-keys yet - for them the key
705 * is the lock object itself:
706 */
707 if (unlikely(!lock->key))
708 lock->key = (void *)lock;
709
710 /*
711 * NOTE: the class-key must be unique. For dynamic locks, a static
712 * lock_class_key variable is passed in through the mutex_init()
713 * (or spin_lock_init()) call - which acts as the key. For static
714 * locks we use the lock object itself as the key.
715 */
Peter Zijlstra4b32d0a2007-07-19 01:48:59 -0700716 BUILD_BUG_ON(sizeof(struct lock_class_key) >
717 sizeof(struct lockdep_map));
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700718
719 key = lock->key->subkeys + subclass;
720
721 hash_head = classhashentry(key);
722
723 /*
724 * We can walk the hash lockfree, because the hash only
725 * grows, and we are careful when adding entries to the end:
726 */
Peter Zijlstra4b32d0a2007-07-19 01:48:59 -0700727 list_for_each_entry(class, hash_head, hash_entry) {
728 if (class->key == key) {
729 WARN_ON_ONCE(class->name != lock->name);
Ingo Molnard6d897c2006-07-10 04:44:04 -0700730 return class;
Peter Zijlstra4b32d0a2007-07-19 01:48:59 -0700731 }
732 }
Ingo Molnard6d897c2006-07-10 04:44:04 -0700733
734 return NULL;
735}
736
737/*
738 * Register a lock's class in the hash-table, if the class is not present
739 * yet. Otherwise we look it up. We cache the result in the lock object
740 * itself, so actual lookup of the hash should be once per lock object.
741 */
742static inline struct lock_class *
Peter Zijlstra4dfbb9d2006-10-11 01:45:14 -0400743register_lock_class(struct lockdep_map *lock, unsigned int subclass, int force)
Ingo Molnard6d897c2006-07-10 04:44:04 -0700744{
745 struct lockdep_subclass_key *key;
746 struct list_head *hash_head;
747 struct lock_class *class;
Ingo Molnar70e45062006-12-06 20:40:50 -0800748 unsigned long flags;
Ingo Molnard6d897c2006-07-10 04:44:04 -0700749
750 class = look_up_lock_class(lock, subclass);
751 if (likely(class))
752 return class;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700753
754 /*
755 * Debug-check: all keys must be persistent!
756 */
757 if (!static_obj(lock->key)) {
758 debug_locks_off();
759 printk("INFO: trying to register non-static key.\n");
760 printk("the code is fine but needs lockdep annotation.\n");
761 printk("turning off the locking correctness validator.\n");
762 dump_stack();
763
764 return NULL;
765 }
766
Ingo Molnard6d897c2006-07-10 04:44:04 -0700767 key = lock->key->subkeys + subclass;
768 hash_head = classhashentry(key);
769
Ingo Molnar70e45062006-12-06 20:40:50 -0800770 raw_local_irq_save(flags);
Ingo Molnar74c383f2006-12-13 00:34:43 -0800771 if (!graph_lock()) {
772 raw_local_irq_restore(flags);
773 return NULL;
774 }
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700775 /*
776 * We have to do the hash-walk again, to avoid races
777 * with another CPU:
778 */
779 list_for_each_entry(class, hash_head, hash_entry)
780 if (class->key == key)
781 goto out_unlock_set;
782 /*
783 * Allocate a new key from the static array, and add it to
784 * the hash:
785 */
786 if (nr_lock_classes >= MAX_LOCKDEP_KEYS) {
Ingo Molnar74c383f2006-12-13 00:34:43 -0800787 if (!debug_locks_off_graph_unlock()) {
788 raw_local_irq_restore(flags);
789 return NULL;
790 }
Ingo Molnar70e45062006-12-06 20:40:50 -0800791 raw_local_irq_restore(flags);
Ingo Molnar74c383f2006-12-13 00:34:43 -0800792
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700793 printk("BUG: MAX_LOCKDEP_KEYS too low!\n");
794 printk("turning off the locking correctness validator.\n");
795 return NULL;
796 }
797 class = lock_classes + nr_lock_classes++;
798 debug_atomic_inc(&nr_unused_locks);
799 class->key = key;
800 class->name = lock->name;
801 class->subclass = subclass;
802 INIT_LIST_HEAD(&class->lock_entry);
803 INIT_LIST_HEAD(&class->locks_before);
804 INIT_LIST_HEAD(&class->locks_after);
805 class->name_version = count_matching_names(class);
806 /*
807 * We use RCU's safe list-add method to make
808 * parallel walking of the hash-list safe:
809 */
810 list_add_tail_rcu(&class->hash_entry, hash_head);
Dale Farnsworth14811972008-02-25 23:03:02 +0100811 /*
812 * Add it to the global list of classes:
813 */
814 list_add_tail_rcu(&class->lock_entry, &all_lock_classes);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700815
816 if (verbose(class)) {
Ingo Molnar74c383f2006-12-13 00:34:43 -0800817 graph_unlock();
Ingo Molnar70e45062006-12-06 20:40:50 -0800818 raw_local_irq_restore(flags);
Ingo Molnar74c383f2006-12-13 00:34:43 -0800819
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700820 printk("\nnew class %p: %s", class->key, class->name);
821 if (class->name_version > 1)
822 printk("#%d", class->name_version);
823 printk("\n");
824 dump_stack();
Ingo Molnar74c383f2006-12-13 00:34:43 -0800825
Ingo Molnar70e45062006-12-06 20:40:50 -0800826 raw_local_irq_save(flags);
Ingo Molnar74c383f2006-12-13 00:34:43 -0800827 if (!graph_lock()) {
828 raw_local_irq_restore(flags);
829 return NULL;
830 }
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700831 }
832out_unlock_set:
Ingo Molnar74c383f2006-12-13 00:34:43 -0800833 graph_unlock();
Ingo Molnar70e45062006-12-06 20:40:50 -0800834 raw_local_irq_restore(flags);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700835
Peter Zijlstra4dfbb9d2006-10-11 01:45:14 -0400836 if (!subclass || force)
Ingo Molnard6d897c2006-07-10 04:44:04 -0700837 lock->class_cache = class;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700838
Jarek Poplawski381a2292007-02-10 01:44:58 -0800839 if (DEBUG_LOCKS_WARN_ON(class->subclass != subclass))
840 return NULL;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700841
842 return class;
843}
844
Peter Zijlstraca58abc2007-07-19 01:48:53 -0700845#ifdef CONFIG_PROVE_LOCKING
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700846/*
Peter Zijlstra8e182572007-07-19 01:48:54 -0700847 * Allocate a lockdep entry. (assumes the graph_lock held, returns
848 * with NULL on failure)
849 */
850static struct lock_list *alloc_list_entry(void)
851{
852 if (nr_list_entries >= MAX_LOCKDEP_ENTRIES) {
853 if (!debug_locks_off_graph_unlock())
854 return NULL;
855
856 printk("BUG: MAX_LOCKDEP_ENTRIES too low!\n");
857 printk("turning off the locking correctness validator.\n");
858 return NULL;
859 }
860 return list_entries + nr_list_entries++;
861}
862
863/*
864 * Add a new dependency to the head of the list:
865 */
866static int add_lock_to_list(struct lock_class *class, struct lock_class *this,
867 struct list_head *head, unsigned long ip, int distance)
868{
869 struct lock_list *entry;
870 /*
871 * Lock not present yet - get a new dependency struct and
872 * add it to the list:
873 */
874 entry = alloc_list_entry();
875 if (!entry)
876 return 0;
877
Peter Zijlstra8e182572007-07-19 01:48:54 -0700878 if (!save_trace(&entry->trace))
879 return 0;
880
Zhu Yi74870172008-08-27 14:33:00 +0800881 entry->class = this;
882 entry->distance = distance;
Peter Zijlstra8e182572007-07-19 01:48:54 -0700883 /*
884 * Since we never remove from the dependency list, the list can
885 * be walked lockless by other CPUs, it's only allocation
886 * that must be protected by the spinlock. But this also means
887 * we must make new entries visible only once writes to the
888 * entry become visible - hence the RCU op:
889 */
890 list_add_tail_rcu(&entry->entry, head);
891
892 return 1;
893}
894
895/*
896 * Recursive, forwards-direction lock-dependency checking, used for
897 * both noncyclic checking and for hardirq-unsafe/softirq-unsafe
898 * checking.
899 *
900 * (to keep the stackframe of the recursive functions small we
901 * use these global variables, and we also mark various helper
902 * functions as noinline.)
903 */
904static struct held_lock *check_source, *check_target;
905
906/*
907 * Print a dependency chain entry (this is only done when a deadlock
908 * has been detected):
909 */
910static noinline int
911print_circular_bug_entry(struct lock_list *target, unsigned int depth)
912{
913 if (debug_locks_silent)
914 return 0;
915 printk("\n-> #%u", depth);
916 print_lock_name(target->class);
917 printk(":\n");
918 print_stack_trace(&target->trace, 6);
919
920 return 0;
921}
922
923/*
924 * When a circular dependency is detected, print the
925 * header first:
926 */
927static noinline int
928print_circular_bug_header(struct lock_list *entry, unsigned int depth)
929{
930 struct task_struct *curr = current;
931
932 if (!debug_locks_off_graph_unlock() || debug_locks_silent)
933 return 0;
934
935 printk("\n=======================================================\n");
936 printk( "[ INFO: possible circular locking dependency detected ]\n");
937 print_kernel_version();
938 printk( "-------------------------------------------------------\n");
939 printk("%s/%d is trying to acquire lock:\n",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -0700940 curr->comm, task_pid_nr(curr));
Peter Zijlstra8e182572007-07-19 01:48:54 -0700941 print_lock(check_source);
942 printk("\nbut task is already holding lock:\n");
943 print_lock(check_target);
944 printk("\nwhich lock already depends on the new lock.\n\n");
945 printk("\nthe existing dependency chain (in reverse order) is:\n");
946
947 print_circular_bug_entry(entry, depth);
948
949 return 0;
950}
951
952static noinline int print_circular_bug_tail(void)
953{
954 struct task_struct *curr = current;
955 struct lock_list this;
956
957 if (debug_locks_silent)
958 return 0;
959
Dave Jonesf82b2172008-08-11 09:30:23 +0200960 this.class = hlock_class(check_source);
Peter Zijlstra8e182572007-07-19 01:48:54 -0700961 if (!save_trace(&this.trace))
962 return 0;
963
964 print_circular_bug_entry(&this, 0);
965
966 printk("\nother info that might help us debug this:\n\n");
967 lockdep_print_held_locks(curr);
968
969 printk("\nstack backtrace:\n");
970 dump_stack();
971
972 return 0;
973}
974
975#define RECURSION_LIMIT 40
976
977static int noinline print_infinite_recursion_bug(void)
978{
979 if (!debug_locks_off_graph_unlock())
980 return 0;
981
982 WARN_ON(1);
983
984 return 0;
985}
986
David Miller419ca3f2008-07-29 21:45:03 -0700987unsigned long __lockdep_count_forward_deps(struct lock_class *class,
988 unsigned int depth)
989{
990 struct lock_list *entry;
991 unsigned long ret = 1;
992
993 if (lockdep_dependency_visit(class, depth))
994 return 0;
995
996 /*
997 * Recurse this class's dependency list:
998 */
999 list_for_each_entry(entry, &class->locks_after, entry)
1000 ret += __lockdep_count_forward_deps(entry->class, depth + 1);
1001
1002 return ret;
1003}
1004
1005unsigned long lockdep_count_forward_deps(struct lock_class *class)
1006{
1007 unsigned long ret, flags;
1008
1009 local_irq_save(flags);
1010 __raw_spin_lock(&lockdep_lock);
1011 ret = __lockdep_count_forward_deps(class, 0);
1012 __raw_spin_unlock(&lockdep_lock);
1013 local_irq_restore(flags);
1014
1015 return ret;
1016}
1017
1018unsigned long __lockdep_count_backward_deps(struct lock_class *class,
1019 unsigned int depth)
1020{
1021 struct lock_list *entry;
1022 unsigned long ret = 1;
1023
1024 if (lockdep_dependency_visit(class, depth))
1025 return 0;
1026 /*
1027 * Recurse this class's dependency list:
1028 */
1029 list_for_each_entry(entry, &class->locks_before, entry)
1030 ret += __lockdep_count_backward_deps(entry->class, depth + 1);
1031
1032 return ret;
1033}
1034
1035unsigned long lockdep_count_backward_deps(struct lock_class *class)
1036{
1037 unsigned long ret, flags;
1038
1039 local_irq_save(flags);
1040 __raw_spin_lock(&lockdep_lock);
1041 ret = __lockdep_count_backward_deps(class, 0);
1042 __raw_spin_unlock(&lockdep_lock);
1043 local_irq_restore(flags);
1044
1045 return ret;
1046}
1047
Peter Zijlstra8e182572007-07-19 01:48:54 -07001048/*
1049 * Prove that the dependency graph starting at <entry> can not
1050 * lead to <target>. Print an error and return 0 if it does.
1051 */
1052static noinline int
1053check_noncircular(struct lock_class *source, unsigned int depth)
1054{
1055 struct lock_list *entry;
1056
David Miller419ca3f2008-07-29 21:45:03 -07001057 if (lockdep_dependency_visit(source, depth))
1058 return 1;
1059
Peter Zijlstra8e182572007-07-19 01:48:54 -07001060 debug_atomic_inc(&nr_cyclic_check_recursions);
1061 if (depth > max_recursion_depth)
1062 max_recursion_depth = depth;
1063 if (depth >= RECURSION_LIMIT)
1064 return print_infinite_recursion_bug();
1065 /*
1066 * Check this lock's dependency list:
1067 */
1068 list_for_each_entry(entry, &source->locks_after, entry) {
Dave Jonesf82b2172008-08-11 09:30:23 +02001069 if (entry->class == hlock_class(check_target))
Peter Zijlstra8e182572007-07-19 01:48:54 -07001070 return print_circular_bug_header(entry, depth+1);
1071 debug_atomic_inc(&nr_cyclic_checks);
1072 if (!check_noncircular(entry->class, depth+1))
1073 return print_circular_bug_entry(entry, depth+1);
1074 }
1075 return 1;
1076}
1077
Steven Rostedt81d68a92008-05-12 21:20:42 +02001078#if defined(CONFIG_TRACE_IRQFLAGS) && defined(CONFIG_PROVE_LOCKING)
Peter Zijlstra8e182572007-07-19 01:48:54 -07001079/*
1080 * Forwards and backwards subgraph searching, for the purposes of
1081 * proving that two subgraphs can be connected by a new dependency
1082 * without creating any illegal irq-safe -> irq-unsafe lock dependency.
1083 */
1084static enum lock_usage_bit find_usage_bit;
1085static struct lock_class *forwards_match, *backwards_match;
1086
1087/*
1088 * Find a node in the forwards-direction dependency sub-graph starting
1089 * at <source> that matches <find_usage_bit>.
1090 *
1091 * Return 2 if such a node exists in the subgraph, and put that node
1092 * into <forwards_match>.
1093 *
1094 * Return 1 otherwise and keep <forwards_match> unchanged.
1095 * Return 0 on error.
1096 */
1097static noinline int
1098find_usage_forwards(struct lock_class *source, unsigned int depth)
1099{
1100 struct lock_list *entry;
1101 int ret;
1102
David Miller419ca3f2008-07-29 21:45:03 -07001103 if (lockdep_dependency_visit(source, depth))
1104 return 1;
1105
Peter Zijlstra8e182572007-07-19 01:48:54 -07001106 if (depth > max_recursion_depth)
1107 max_recursion_depth = depth;
1108 if (depth >= RECURSION_LIMIT)
1109 return print_infinite_recursion_bug();
1110
1111 debug_atomic_inc(&nr_find_usage_forwards_checks);
1112 if (source->usage_mask & (1 << find_usage_bit)) {
1113 forwards_match = source;
1114 return 2;
1115 }
1116
1117 /*
1118 * Check this lock's dependency list:
1119 */
1120 list_for_each_entry(entry, &source->locks_after, entry) {
1121 debug_atomic_inc(&nr_find_usage_forwards_recursions);
1122 ret = find_usage_forwards(entry->class, depth+1);
1123 if (ret == 2 || ret == 0)
1124 return ret;
1125 }
1126 return 1;
1127}
1128
1129/*
1130 * Find a node in the backwards-direction dependency sub-graph starting
1131 * at <source> that matches <find_usage_bit>.
1132 *
1133 * Return 2 if such a node exists in the subgraph, and put that node
1134 * into <backwards_match>.
1135 *
1136 * Return 1 otherwise and keep <backwards_match> unchanged.
1137 * Return 0 on error.
1138 */
Steven Rostedt1d09daa2008-05-12 21:20:55 +02001139static noinline int
Peter Zijlstra8e182572007-07-19 01:48:54 -07001140find_usage_backwards(struct lock_class *source, unsigned int depth)
1141{
1142 struct lock_list *entry;
1143 int ret;
1144
David Miller419ca3f2008-07-29 21:45:03 -07001145 if (lockdep_dependency_visit(source, depth))
1146 return 1;
1147
Peter Zijlstra8e182572007-07-19 01:48:54 -07001148 if (!__raw_spin_is_locked(&lockdep_lock))
1149 return DEBUG_LOCKS_WARN_ON(1);
1150
1151 if (depth > max_recursion_depth)
1152 max_recursion_depth = depth;
1153 if (depth >= RECURSION_LIMIT)
1154 return print_infinite_recursion_bug();
1155
1156 debug_atomic_inc(&nr_find_usage_backwards_checks);
1157 if (source->usage_mask & (1 << find_usage_bit)) {
1158 backwards_match = source;
1159 return 2;
1160 }
1161
Dave Jonesf82b2172008-08-11 09:30:23 +02001162 if (!source && debug_locks_off_graph_unlock()) {
1163 WARN_ON(1);
1164 return 0;
1165 }
1166
Peter Zijlstra8e182572007-07-19 01:48:54 -07001167 /*
1168 * Check this lock's dependency list:
1169 */
1170 list_for_each_entry(entry, &source->locks_before, entry) {
1171 debug_atomic_inc(&nr_find_usage_backwards_recursions);
1172 ret = find_usage_backwards(entry->class, depth+1);
1173 if (ret == 2 || ret == 0)
1174 return ret;
1175 }
1176 return 1;
1177}
1178
1179static int
1180print_bad_irq_dependency(struct task_struct *curr,
1181 struct held_lock *prev,
1182 struct held_lock *next,
1183 enum lock_usage_bit bit1,
1184 enum lock_usage_bit bit2,
1185 const char *irqclass)
1186{
1187 if (!debug_locks_off_graph_unlock() || debug_locks_silent)
1188 return 0;
1189
1190 printk("\n======================================================\n");
1191 printk( "[ INFO: %s-safe -> %s-unsafe lock order detected ]\n",
1192 irqclass, irqclass);
1193 print_kernel_version();
1194 printk( "------------------------------------------------------\n");
1195 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 -07001196 curr->comm, task_pid_nr(curr),
Peter Zijlstra8e182572007-07-19 01:48:54 -07001197 curr->hardirq_context, hardirq_count() >> HARDIRQ_SHIFT,
1198 curr->softirq_context, softirq_count() >> SOFTIRQ_SHIFT,
1199 curr->hardirqs_enabled,
1200 curr->softirqs_enabled);
1201 print_lock(next);
1202
1203 printk("\nand this task is already holding:\n");
1204 print_lock(prev);
1205 printk("which would create a new lock dependency:\n");
Dave Jonesf82b2172008-08-11 09:30:23 +02001206 print_lock_name(hlock_class(prev));
Peter Zijlstra8e182572007-07-19 01:48:54 -07001207 printk(" ->");
Dave Jonesf82b2172008-08-11 09:30:23 +02001208 print_lock_name(hlock_class(next));
Peter Zijlstra8e182572007-07-19 01:48:54 -07001209 printk("\n");
1210
1211 printk("\nbut this new dependency connects a %s-irq-safe lock:\n",
1212 irqclass);
1213 print_lock_name(backwards_match);
1214 printk("\n... which became %s-irq-safe at:\n", irqclass);
1215
1216 print_stack_trace(backwards_match->usage_traces + bit1, 1);
1217
1218 printk("\nto a %s-irq-unsafe lock:\n", irqclass);
1219 print_lock_name(forwards_match);
1220 printk("\n... which became %s-irq-unsafe at:\n", irqclass);
1221 printk("...");
1222
1223 print_stack_trace(forwards_match->usage_traces + bit2, 1);
1224
1225 printk("\nother info that might help us debug this:\n\n");
1226 lockdep_print_held_locks(curr);
1227
1228 printk("\nthe %s-irq-safe lock's dependencies:\n", irqclass);
1229 print_lock_dependencies(backwards_match, 0);
1230
1231 printk("\nthe %s-irq-unsafe lock's dependencies:\n", irqclass);
1232 print_lock_dependencies(forwards_match, 0);
1233
1234 printk("\nstack backtrace:\n");
1235 dump_stack();
1236
1237 return 0;
1238}
1239
1240static int
1241check_usage(struct task_struct *curr, struct held_lock *prev,
1242 struct held_lock *next, enum lock_usage_bit bit_backwards,
1243 enum lock_usage_bit bit_forwards, const char *irqclass)
1244{
1245 int ret;
1246
1247 find_usage_bit = bit_backwards;
1248 /* fills in <backwards_match> */
Dave Jonesf82b2172008-08-11 09:30:23 +02001249 ret = find_usage_backwards(hlock_class(prev), 0);
Peter Zijlstra8e182572007-07-19 01:48:54 -07001250 if (!ret || ret == 1)
1251 return ret;
1252
1253 find_usage_bit = bit_forwards;
Dave Jonesf82b2172008-08-11 09:30:23 +02001254 ret = find_usage_forwards(hlock_class(next), 0);
Peter Zijlstra8e182572007-07-19 01:48:54 -07001255 if (!ret || ret == 1)
1256 return ret;
1257 /* ret == 2 */
1258 return print_bad_irq_dependency(curr, prev, next,
1259 bit_backwards, bit_forwards, irqclass);
1260}
1261
1262static int
1263check_prev_add_irq(struct task_struct *curr, struct held_lock *prev,
1264 struct held_lock *next)
1265{
1266 /*
1267 * Prove that the new dependency does not connect a hardirq-safe
1268 * lock with a hardirq-unsafe lock - to achieve this we search
1269 * the backwards-subgraph starting at <prev>, and the
1270 * forwards-subgraph starting at <next>:
1271 */
1272 if (!check_usage(curr, prev, next, LOCK_USED_IN_HARDIRQ,
1273 LOCK_ENABLED_HARDIRQS, "hard"))
1274 return 0;
1275
1276 /*
1277 * Prove that the new dependency does not connect a hardirq-safe-read
1278 * lock with a hardirq-unsafe lock - to achieve this we search
1279 * the backwards-subgraph starting at <prev>, and the
1280 * forwards-subgraph starting at <next>:
1281 */
1282 if (!check_usage(curr, prev, next, LOCK_USED_IN_HARDIRQ_READ,
1283 LOCK_ENABLED_HARDIRQS, "hard-read"))
1284 return 0;
1285
1286 /*
1287 * Prove that the new dependency does not connect a softirq-safe
1288 * lock with a softirq-unsafe lock - to achieve this we search
1289 * the backwards-subgraph starting at <prev>, and the
1290 * forwards-subgraph starting at <next>:
1291 */
1292 if (!check_usage(curr, prev, next, LOCK_USED_IN_SOFTIRQ,
1293 LOCK_ENABLED_SOFTIRQS, "soft"))
1294 return 0;
1295 /*
1296 * Prove that the new dependency does not connect a softirq-safe-read
1297 * lock with a softirq-unsafe lock - to achieve this we search
1298 * the backwards-subgraph starting at <prev>, and the
1299 * forwards-subgraph starting at <next>:
1300 */
1301 if (!check_usage(curr, prev, next, LOCK_USED_IN_SOFTIRQ_READ,
1302 LOCK_ENABLED_SOFTIRQS, "soft"))
1303 return 0;
1304
1305 return 1;
1306}
1307
1308static void inc_chains(void)
1309{
1310 if (current->hardirq_context)
1311 nr_hardirq_chains++;
1312 else {
1313 if (current->softirq_context)
1314 nr_softirq_chains++;
1315 else
1316 nr_process_chains++;
1317 }
1318}
1319
1320#else
1321
1322static inline int
1323check_prev_add_irq(struct task_struct *curr, struct held_lock *prev,
1324 struct held_lock *next)
1325{
1326 return 1;
1327}
1328
1329static inline void inc_chains(void)
1330{
1331 nr_process_chains++;
1332}
1333
1334#endif
1335
1336static int
1337print_deadlock_bug(struct task_struct *curr, struct held_lock *prev,
1338 struct held_lock *next)
1339{
1340 if (!debug_locks_off_graph_unlock() || debug_locks_silent)
1341 return 0;
1342
1343 printk("\n=============================================\n");
1344 printk( "[ INFO: possible recursive locking detected ]\n");
1345 print_kernel_version();
1346 printk( "---------------------------------------------\n");
1347 printk("%s/%d is trying to acquire lock:\n",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07001348 curr->comm, task_pid_nr(curr));
Peter Zijlstra8e182572007-07-19 01:48:54 -07001349 print_lock(next);
1350 printk("\nbut task is already holding lock:\n");
1351 print_lock(prev);
1352
1353 printk("\nother info that might help us debug this:\n");
1354 lockdep_print_held_locks(curr);
1355
1356 printk("\nstack backtrace:\n");
1357 dump_stack();
1358
1359 return 0;
1360}
1361
1362/*
1363 * Check whether we are holding such a class already.
1364 *
1365 * (Note that this has to be done separately, because the graph cannot
1366 * detect such classes of deadlocks.)
1367 *
1368 * Returns: 0 on deadlock detected, 1 on OK, 2 on recursive read
1369 */
1370static int
1371check_deadlock(struct task_struct *curr, struct held_lock *next,
1372 struct lockdep_map *next_instance, int read)
1373{
1374 struct held_lock *prev;
Peter Zijlstra7531e2f2008-08-11 09:30:24 +02001375 struct held_lock *nest = NULL;
Peter Zijlstra8e182572007-07-19 01:48:54 -07001376 int i;
1377
1378 for (i = 0; i < curr->lockdep_depth; i++) {
1379 prev = curr->held_locks + i;
Peter Zijlstra7531e2f2008-08-11 09:30:24 +02001380
1381 if (prev->instance == next->nest_lock)
1382 nest = prev;
1383
Dave Jonesf82b2172008-08-11 09:30:23 +02001384 if (hlock_class(prev) != hlock_class(next))
Peter Zijlstra8e182572007-07-19 01:48:54 -07001385 continue;
Peter Zijlstra7531e2f2008-08-11 09:30:24 +02001386
Peter Zijlstra8e182572007-07-19 01:48:54 -07001387 /*
1388 * Allow read-after-read recursion of the same
1389 * lock class (i.e. read_lock(lock)+read_lock(lock)):
1390 */
1391 if ((read == 2) && prev->read)
1392 return 2;
Peter Zijlstra7531e2f2008-08-11 09:30:24 +02001393
1394 /*
1395 * We're holding the nest_lock, which serializes this lock's
1396 * nesting behaviour.
1397 */
1398 if (nest)
1399 return 2;
1400
Peter Zijlstra8e182572007-07-19 01:48:54 -07001401 return print_deadlock_bug(curr, prev, next);
1402 }
1403 return 1;
1404}
1405
1406/*
1407 * There was a chain-cache miss, and we are about to add a new dependency
1408 * to a previous lock. We recursively validate the following rules:
1409 *
1410 * - would the adding of the <prev> -> <next> dependency create a
1411 * circular dependency in the graph? [== circular deadlock]
1412 *
1413 * - does the new prev->next dependency connect any hardirq-safe lock
1414 * (in the full backwards-subgraph starting at <prev>) with any
1415 * hardirq-unsafe lock (in the full forwards-subgraph starting at
1416 * <next>)? [== illegal lock inversion with hardirq contexts]
1417 *
1418 * - does the new prev->next dependency connect any softirq-safe lock
1419 * (in the full backwards-subgraph starting at <prev>) with any
1420 * softirq-unsafe lock (in the full forwards-subgraph starting at
1421 * <next>)? [== illegal lock inversion with softirq contexts]
1422 *
1423 * any of these scenarios could lead to a deadlock.
1424 *
1425 * Then if all the validations pass, we add the forwards and backwards
1426 * dependency.
1427 */
1428static int
1429check_prev_add(struct task_struct *curr, struct held_lock *prev,
1430 struct held_lock *next, int distance)
1431{
1432 struct lock_list *entry;
1433 int ret;
1434
1435 /*
1436 * Prove that the new <prev> -> <next> dependency would not
1437 * create a circular dependency in the graph. (We do this by
1438 * forward-recursing into the graph starting at <next>, and
1439 * checking whether we can reach <prev>.)
1440 *
1441 * We are using global variables to control the recursion, to
1442 * keep the stackframe size of the recursive functions low:
1443 */
1444 check_source = next;
1445 check_target = prev;
Dave Jonesf82b2172008-08-11 09:30:23 +02001446 if (!(check_noncircular(hlock_class(next), 0)))
Peter Zijlstra8e182572007-07-19 01:48:54 -07001447 return print_circular_bug_tail();
1448
1449 if (!check_prev_add_irq(curr, prev, next))
1450 return 0;
1451
1452 /*
1453 * For recursive read-locks we do all the dependency checks,
1454 * but we dont store read-triggered dependencies (only
1455 * write-triggered dependencies). This ensures that only the
1456 * write-side dependencies matter, and that if for example a
1457 * write-lock never takes any other locks, then the reads are
1458 * equivalent to a NOP.
1459 */
1460 if (next->read == 2 || prev->read == 2)
1461 return 1;
1462 /*
1463 * Is the <prev> -> <next> dependency already present?
1464 *
1465 * (this may occur even though this is a new chain: consider
1466 * e.g. the L1 -> L2 -> L3 -> L4 and the L5 -> L1 -> L2 -> L3
1467 * chains - the second one will be new, but L1 already has
1468 * L2 added to its dependency list, due to the first chain.)
1469 */
Dave Jonesf82b2172008-08-11 09:30:23 +02001470 list_for_each_entry(entry, &hlock_class(prev)->locks_after, entry) {
1471 if (entry->class == hlock_class(next)) {
Peter Zijlstra8e182572007-07-19 01:48:54 -07001472 if (distance == 1)
1473 entry->distance = 1;
1474 return 2;
1475 }
1476 }
1477
1478 /*
1479 * Ok, all validations passed, add the new lock
1480 * to the previous lock's dependency list:
1481 */
Dave Jonesf82b2172008-08-11 09:30:23 +02001482 ret = add_lock_to_list(hlock_class(prev), hlock_class(next),
1483 &hlock_class(prev)->locks_after,
1484 next->acquire_ip, distance);
Peter Zijlstra8e182572007-07-19 01:48:54 -07001485
1486 if (!ret)
1487 return 0;
1488
Dave Jonesf82b2172008-08-11 09:30:23 +02001489 ret = add_lock_to_list(hlock_class(next), hlock_class(prev),
1490 &hlock_class(next)->locks_before,
1491 next->acquire_ip, distance);
Peter Zijlstra8e182572007-07-19 01:48:54 -07001492 if (!ret)
1493 return 0;
1494
1495 /*
1496 * Debugging printouts:
1497 */
Dave Jonesf82b2172008-08-11 09:30:23 +02001498 if (verbose(hlock_class(prev)) || verbose(hlock_class(next))) {
Peter Zijlstra8e182572007-07-19 01:48:54 -07001499 graph_unlock();
1500 printk("\n new dependency: ");
Dave Jonesf82b2172008-08-11 09:30:23 +02001501 print_lock_name(hlock_class(prev));
Peter Zijlstra8e182572007-07-19 01:48:54 -07001502 printk(" => ");
Dave Jonesf82b2172008-08-11 09:30:23 +02001503 print_lock_name(hlock_class(next));
Peter Zijlstra8e182572007-07-19 01:48:54 -07001504 printk("\n");
1505 dump_stack();
1506 return graph_lock();
1507 }
1508 return 1;
1509}
1510
1511/*
1512 * Add the dependency to all directly-previous locks that are 'relevant'.
1513 * The ones that are relevant are (in increasing distance from curr):
1514 * all consecutive trylock entries and the final non-trylock entry - or
1515 * the end of this context's lock-chain - whichever comes first.
1516 */
1517static int
1518check_prevs_add(struct task_struct *curr, struct held_lock *next)
1519{
1520 int depth = curr->lockdep_depth;
1521 struct held_lock *hlock;
1522
1523 /*
1524 * Debugging checks.
1525 *
1526 * Depth must not be zero for a non-head lock:
1527 */
1528 if (!depth)
1529 goto out_bug;
1530 /*
1531 * At least two relevant locks must exist for this
1532 * to be a head:
1533 */
1534 if (curr->held_locks[depth].irq_context !=
1535 curr->held_locks[depth-1].irq_context)
1536 goto out_bug;
1537
1538 for (;;) {
1539 int distance = curr->lockdep_depth - depth + 1;
1540 hlock = curr->held_locks + depth-1;
1541 /*
1542 * Only non-recursive-read entries get new dependencies
1543 * added:
1544 */
1545 if (hlock->read != 2) {
1546 if (!check_prev_add(curr, hlock, next, distance))
1547 return 0;
1548 /*
1549 * Stop after the first non-trylock entry,
1550 * as non-trylock entries have added their
1551 * own direct dependencies already, so this
1552 * lock is connected to them indirectly:
1553 */
1554 if (!hlock->trylock)
1555 break;
1556 }
1557 depth--;
1558 /*
1559 * End of lock-stack?
1560 */
1561 if (!depth)
1562 break;
1563 /*
1564 * Stop the search if we cross into another context:
1565 */
1566 if (curr->held_locks[depth].irq_context !=
1567 curr->held_locks[depth-1].irq_context)
1568 break;
1569 }
1570 return 1;
1571out_bug:
1572 if (!debug_locks_off_graph_unlock())
1573 return 0;
1574
1575 WARN_ON(1);
1576
1577 return 0;
1578}
1579
1580unsigned long nr_lock_chains;
Huang, Ying443cd502008-06-20 16:39:21 +08001581struct lock_chain lock_chains[MAX_LOCKDEP_CHAINS];
Huang, Yingcd1a28e2008-06-23 11:20:54 +08001582int nr_chain_hlocks;
Huang, Ying443cd502008-06-20 16:39:21 +08001583static u16 chain_hlocks[MAX_LOCKDEP_CHAIN_HLOCKS];
1584
1585struct lock_class *lock_chain_get_class(struct lock_chain *chain, int i)
1586{
1587 return lock_classes + chain_hlocks[chain->base + i];
1588}
Peter Zijlstra8e182572007-07-19 01:48:54 -07001589
1590/*
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001591 * Look up a dependency chain. If the key is not present yet then
Jarek Poplawski9e860d02007-05-08 00:30:12 -07001592 * add it and return 1 - in this case the new dependency chain is
1593 * validated. If the key is already hashed, return 0.
1594 * (On return with 1 graph_lock is held.)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001595 */
Huang, Ying443cd502008-06-20 16:39:21 +08001596static inline int lookup_chain_cache(struct task_struct *curr,
1597 struct held_lock *hlock,
1598 u64 chain_key)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001599{
Dave Jonesf82b2172008-08-11 09:30:23 +02001600 struct lock_class *class = hlock_class(hlock);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001601 struct list_head *hash_head = chainhashentry(chain_key);
1602 struct lock_chain *chain;
Huang, Ying443cd502008-06-20 16:39:21 +08001603 struct held_lock *hlock_curr, *hlock_next;
Huang, Yingcd1a28e2008-06-23 11:20:54 +08001604 int i, j, n, cn;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001605
Jarek Poplawski381a2292007-02-10 01:44:58 -08001606 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
1607 return 0;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001608 /*
1609 * We can walk it lock-free, because entries only get added
1610 * to the hash:
1611 */
1612 list_for_each_entry(chain, hash_head, entry) {
1613 if (chain->chain_key == chain_key) {
1614cache_hit:
1615 debug_atomic_inc(&chain_lookup_hits);
Ingo Molnar81fc6852006-12-13 00:34:40 -08001616 if (very_verbose(class))
Andrew Morton755cd902006-12-29 16:49:14 -08001617 printk("\nhash chain already cached, key: "
1618 "%016Lx tail class: [%p] %s\n",
1619 (unsigned long long)chain_key,
1620 class->key, class->name);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001621 return 0;
1622 }
1623 }
Ingo Molnar81fc6852006-12-13 00:34:40 -08001624 if (very_verbose(class))
Andrew Morton755cd902006-12-29 16:49:14 -08001625 printk("\nnew hash chain, key: %016Lx tail class: [%p] %s\n",
1626 (unsigned long long)chain_key, class->key, class->name);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001627 /*
1628 * Allocate a new chain entry from the static array, and add
1629 * it to the hash:
1630 */
Ingo Molnar74c383f2006-12-13 00:34:43 -08001631 if (!graph_lock())
1632 return 0;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001633 /*
1634 * We have to walk the chain again locked - to avoid duplicates:
1635 */
1636 list_for_each_entry(chain, hash_head, entry) {
1637 if (chain->chain_key == chain_key) {
Ingo Molnar74c383f2006-12-13 00:34:43 -08001638 graph_unlock();
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001639 goto cache_hit;
1640 }
1641 }
1642 if (unlikely(nr_lock_chains >= MAX_LOCKDEP_CHAINS)) {
Ingo Molnar74c383f2006-12-13 00:34:43 -08001643 if (!debug_locks_off_graph_unlock())
1644 return 0;
1645
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001646 printk("BUG: MAX_LOCKDEP_CHAINS too low!\n");
1647 printk("turning off the locking correctness validator.\n");
1648 return 0;
1649 }
1650 chain = lock_chains + nr_lock_chains++;
1651 chain->chain_key = chain_key;
Huang, Ying443cd502008-06-20 16:39:21 +08001652 chain->irq_context = hlock->irq_context;
1653 /* Find the first held_lock of current chain */
1654 hlock_next = hlock;
1655 for (i = curr->lockdep_depth - 1; i >= 0; i--) {
1656 hlock_curr = curr->held_locks + i;
1657 if (hlock_curr->irq_context != hlock_next->irq_context)
1658 break;
1659 hlock_next = hlock;
1660 }
1661 i++;
1662 chain->depth = curr->lockdep_depth + 1 - i;
Huang, Yingcd1a28e2008-06-23 11:20:54 +08001663 cn = nr_chain_hlocks;
1664 while (cn + chain->depth <= MAX_LOCKDEP_CHAIN_HLOCKS) {
1665 n = cmpxchg(&nr_chain_hlocks, cn, cn + chain->depth);
1666 if (n == cn)
1667 break;
1668 cn = n;
1669 }
1670 if (likely(cn + chain->depth <= MAX_LOCKDEP_CHAIN_HLOCKS)) {
1671 chain->base = cn;
Huang, Ying443cd502008-06-20 16:39:21 +08001672 for (j = 0; j < chain->depth - 1; j++, i++) {
Dave Jonesf82b2172008-08-11 09:30:23 +02001673 int lock_id = curr->held_locks[i].class_idx - 1;
Huang, Ying443cd502008-06-20 16:39:21 +08001674 chain_hlocks[chain->base + j] = lock_id;
1675 }
1676 chain_hlocks[chain->base + j] = class - lock_classes;
1677 }
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001678 list_add_tail_rcu(&chain->entry, hash_head);
1679 debug_atomic_inc(&chain_lookup_misses);
Peter Zijlstra8e182572007-07-19 01:48:54 -07001680 inc_chains();
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001681
1682 return 1;
1683}
Peter Zijlstra8e182572007-07-19 01:48:54 -07001684
1685static int validate_chain(struct task_struct *curr, struct lockdep_map *lock,
Johannes Berg4e6045f2007-10-18 23:39:55 -07001686 struct held_lock *hlock, int chain_head, u64 chain_key)
Peter Zijlstra8e182572007-07-19 01:48:54 -07001687{
1688 /*
1689 * Trylock needs to maintain the stack of held locks, but it
1690 * does not add new dependencies, because trylock can be done
1691 * in any order.
1692 *
1693 * We look up the chain_key and do the O(N^2) check and update of
1694 * the dependencies only if this is a new dependency chain.
1695 * (If lookup_chain_cache() returns with 1 it acquires
1696 * graph_lock for us)
1697 */
1698 if (!hlock->trylock && (hlock->check == 2) &&
Huang, Ying443cd502008-06-20 16:39:21 +08001699 lookup_chain_cache(curr, hlock, chain_key)) {
Peter Zijlstra8e182572007-07-19 01:48:54 -07001700 /*
1701 * Check whether last held lock:
1702 *
1703 * - is irq-safe, if this lock is irq-unsafe
1704 * - is softirq-safe, if this lock is hardirq-unsafe
1705 *
1706 * And check whether the new lock's dependency graph
1707 * could lead back to the previous lock.
1708 *
1709 * any of these scenarios could lead to a deadlock. If
1710 * All validations
1711 */
1712 int ret = check_deadlock(curr, hlock, lock, hlock->read);
1713
1714 if (!ret)
1715 return 0;
1716 /*
1717 * Mark recursive read, as we jump over it when
1718 * building dependencies (just like we jump over
1719 * trylock entries):
1720 */
1721 if (ret == 2)
1722 hlock->read = 2;
1723 /*
1724 * Add dependency only if this lock is not the head
1725 * of the chain, and if it's not a secondary read-lock:
1726 */
1727 if (!chain_head && ret != 2)
1728 if (!check_prevs_add(curr, hlock))
1729 return 0;
1730 graph_unlock();
1731 } else
1732 /* after lookup_chain_cache(): */
1733 if (unlikely(!debug_locks))
1734 return 0;
1735
1736 return 1;
1737}
1738#else
1739static inline int validate_chain(struct task_struct *curr,
1740 struct lockdep_map *lock, struct held_lock *hlock,
Gregory Haskins3aa416b2007-10-11 22:11:11 +02001741 int chain_head, u64 chain_key)
Peter Zijlstra8e182572007-07-19 01:48:54 -07001742{
1743 return 1;
1744}
Peter Zijlstraca58abc2007-07-19 01:48:53 -07001745#endif
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001746
1747/*
1748 * We are building curr_chain_key incrementally, so double-check
1749 * it from scratch, to make sure that it's done correctly:
1750 */
Steven Rostedt1d09daa2008-05-12 21:20:55 +02001751static void check_chain_key(struct task_struct *curr)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001752{
1753#ifdef CONFIG_DEBUG_LOCKDEP
1754 struct held_lock *hlock, *prev_hlock = NULL;
1755 unsigned int i, id;
1756 u64 chain_key = 0;
1757
1758 for (i = 0; i < curr->lockdep_depth; i++) {
1759 hlock = curr->held_locks + i;
1760 if (chain_key != hlock->prev_chain_key) {
1761 debug_locks_off();
Arjan van de Ven2df8b1d2008-07-30 12:43:11 -07001762 WARN(1, "hm#1, depth: %u [%u], %016Lx != %016Lx\n",
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001763 curr->lockdep_depth, i,
1764 (unsigned long long)chain_key,
1765 (unsigned long long)hlock->prev_chain_key);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001766 return;
1767 }
Dave Jonesf82b2172008-08-11 09:30:23 +02001768 id = hlock->class_idx - 1;
Jarek Poplawski381a2292007-02-10 01:44:58 -08001769 if (DEBUG_LOCKS_WARN_ON(id >= MAX_LOCKDEP_KEYS))
1770 return;
1771
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001772 if (prev_hlock && (prev_hlock->irq_context !=
1773 hlock->irq_context))
1774 chain_key = 0;
1775 chain_key = iterate_chain_key(chain_key, id);
1776 prev_hlock = hlock;
1777 }
1778 if (chain_key != curr->curr_chain_key) {
1779 debug_locks_off();
Arjan van de Ven2df8b1d2008-07-30 12:43:11 -07001780 WARN(1, "hm#2, depth: %u [%u], %016Lx != %016Lx\n",
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001781 curr->lockdep_depth, i,
1782 (unsigned long long)chain_key,
1783 (unsigned long long)curr->curr_chain_key);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001784 }
1785#endif
1786}
1787
Peter Zijlstra8e182572007-07-19 01:48:54 -07001788static int
1789print_usage_bug(struct task_struct *curr, struct held_lock *this,
1790 enum lock_usage_bit prev_bit, enum lock_usage_bit new_bit)
1791{
1792 if (!debug_locks_off_graph_unlock() || debug_locks_silent)
1793 return 0;
1794
1795 printk("\n=================================\n");
1796 printk( "[ INFO: inconsistent lock state ]\n");
1797 print_kernel_version();
1798 printk( "---------------------------------\n");
1799
1800 printk("inconsistent {%s} -> {%s} usage.\n",
1801 usage_str[prev_bit], usage_str[new_bit]);
1802
1803 printk("%s/%d [HC%u[%lu]:SC%u[%lu]:HE%u:SE%u] takes:\n",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07001804 curr->comm, task_pid_nr(curr),
Peter Zijlstra8e182572007-07-19 01:48:54 -07001805 trace_hardirq_context(curr), hardirq_count() >> HARDIRQ_SHIFT,
1806 trace_softirq_context(curr), softirq_count() >> SOFTIRQ_SHIFT,
1807 trace_hardirqs_enabled(curr),
1808 trace_softirqs_enabled(curr));
1809 print_lock(this);
1810
1811 printk("{%s} state was registered at:\n", usage_str[prev_bit]);
Dave Jonesf82b2172008-08-11 09:30:23 +02001812 print_stack_trace(hlock_class(this)->usage_traces + prev_bit, 1);
Peter Zijlstra8e182572007-07-19 01:48:54 -07001813
1814 print_irqtrace_events(curr);
1815 printk("\nother info that might help us debug this:\n");
1816 lockdep_print_held_locks(curr);
1817
1818 printk("\nstack backtrace:\n");
1819 dump_stack();
1820
1821 return 0;
1822}
1823
1824/*
1825 * Print out an error if an invalid bit is set:
1826 */
1827static inline int
1828valid_state(struct task_struct *curr, struct held_lock *this,
1829 enum lock_usage_bit new_bit, enum lock_usage_bit bad_bit)
1830{
Dave Jonesf82b2172008-08-11 09:30:23 +02001831 if (unlikely(hlock_class(this)->usage_mask & (1 << bad_bit)))
Peter Zijlstra8e182572007-07-19 01:48:54 -07001832 return print_usage_bug(curr, this, bad_bit, new_bit);
1833 return 1;
1834}
1835
1836static int mark_lock(struct task_struct *curr, struct held_lock *this,
1837 enum lock_usage_bit new_bit);
1838
Steven Rostedt81d68a92008-05-12 21:20:42 +02001839#if defined(CONFIG_TRACE_IRQFLAGS) && defined(CONFIG_PROVE_LOCKING)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001840
1841/*
1842 * print irq inversion bug:
1843 */
1844static int
1845print_irq_inversion_bug(struct task_struct *curr, struct lock_class *other,
1846 struct held_lock *this, int forwards,
1847 const char *irqclass)
1848{
Ingo Molnar74c383f2006-12-13 00:34:43 -08001849 if (!debug_locks_off_graph_unlock() || debug_locks_silent)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001850 return 0;
1851
1852 printk("\n=========================================================\n");
1853 printk( "[ INFO: possible irq lock inversion dependency detected ]\n");
Dave Jones99de0552006-09-29 02:00:10 -07001854 print_kernel_version();
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001855 printk( "---------------------------------------------------------\n");
1856 printk("%s/%d just changed the state of lock:\n",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07001857 curr->comm, task_pid_nr(curr));
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001858 print_lock(this);
1859 if (forwards)
1860 printk("but this lock took another, %s-irq-unsafe lock in the past:\n", irqclass);
1861 else
1862 printk("but this lock was taken by another, %s-irq-safe lock in the past:\n", irqclass);
1863 print_lock_name(other);
1864 printk("\n\nand interrupts could create inverse lock ordering between them.\n\n");
1865
1866 printk("\nother info that might help us debug this:\n");
1867 lockdep_print_held_locks(curr);
1868
1869 printk("\nthe first lock's dependencies:\n");
Dave Jonesf82b2172008-08-11 09:30:23 +02001870 print_lock_dependencies(hlock_class(this), 0);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001871
1872 printk("\nthe second lock's dependencies:\n");
1873 print_lock_dependencies(other, 0);
1874
1875 printk("\nstack backtrace:\n");
1876 dump_stack();
1877
1878 return 0;
1879}
1880
1881/*
1882 * Prove that in the forwards-direction subgraph starting at <this>
1883 * there is no lock matching <mask>:
1884 */
1885static int
1886check_usage_forwards(struct task_struct *curr, struct held_lock *this,
1887 enum lock_usage_bit bit, const char *irqclass)
1888{
1889 int ret;
1890
1891 find_usage_bit = bit;
1892 /* fills in <forwards_match> */
Dave Jonesf82b2172008-08-11 09:30:23 +02001893 ret = find_usage_forwards(hlock_class(this), 0);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001894 if (!ret || ret == 1)
1895 return ret;
1896
1897 return print_irq_inversion_bug(curr, forwards_match, this, 1, irqclass);
1898}
1899
1900/*
1901 * Prove that in the backwards-direction subgraph starting at <this>
1902 * there is no lock matching <mask>:
1903 */
1904static int
1905check_usage_backwards(struct task_struct *curr, struct held_lock *this,
1906 enum lock_usage_bit bit, const char *irqclass)
1907{
1908 int ret;
1909
1910 find_usage_bit = bit;
1911 /* fills in <backwards_match> */
Dave Jonesf82b2172008-08-11 09:30:23 +02001912 ret = find_usage_backwards(hlock_class(this), 0);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001913 if (!ret || ret == 1)
1914 return ret;
1915
1916 return print_irq_inversion_bug(curr, backwards_match, this, 0, irqclass);
1917}
1918
Ingo Molnar3117df02006-12-13 00:34:43 -08001919void print_irqtrace_events(struct task_struct *curr)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001920{
1921 printk("irq event stamp: %u\n", curr->irq_events);
1922 printk("hardirqs last enabled at (%u): ", curr->hardirq_enable_event);
1923 print_ip_sym(curr->hardirq_enable_ip);
1924 printk("hardirqs last disabled at (%u): ", curr->hardirq_disable_event);
1925 print_ip_sym(curr->hardirq_disable_ip);
1926 printk("softirqs last enabled at (%u): ", curr->softirq_enable_event);
1927 print_ip_sym(curr->softirq_enable_ip);
1928 printk("softirqs last disabled at (%u): ", curr->softirq_disable_event);
1929 print_ip_sym(curr->softirq_disable_ip);
1930}
1931
Peter Zijlstra8e182572007-07-19 01:48:54 -07001932static int hardirq_verbose(struct lock_class *class)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001933{
Peter Zijlstra8e182572007-07-19 01:48:54 -07001934#if HARDIRQ_VERBOSE
1935 return class_filter(class);
1936#endif
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001937 return 0;
1938}
1939
Peter Zijlstra8e182572007-07-19 01:48:54 -07001940static int softirq_verbose(struct lock_class *class)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001941{
Peter Zijlstra8e182572007-07-19 01:48:54 -07001942#if SOFTIRQ_VERBOSE
1943 return class_filter(class);
1944#endif
1945 return 0;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001946}
1947
1948#define STRICT_READ_CHECKS 1
1949
Peter Zijlstra8e182572007-07-19 01:48:54 -07001950static int mark_lock_irq(struct task_struct *curr, struct held_lock *this,
1951 enum lock_usage_bit new_bit)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001952{
Peter Zijlstra8e182572007-07-19 01:48:54 -07001953 int ret = 1;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001954
Peter Zijlstra8e182572007-07-19 01:48:54 -07001955 switch(new_bit) {
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001956 case LOCK_USED_IN_HARDIRQ:
1957 if (!valid_state(curr, this, new_bit, LOCK_ENABLED_HARDIRQS))
1958 return 0;
1959 if (!valid_state(curr, this, new_bit,
1960 LOCK_ENABLED_HARDIRQS_READ))
1961 return 0;
1962 /*
1963 * just marked it hardirq-safe, check that this lock
1964 * took no hardirq-unsafe lock in the past:
1965 */
1966 if (!check_usage_forwards(curr, this,
1967 LOCK_ENABLED_HARDIRQS, "hard"))
1968 return 0;
1969#if STRICT_READ_CHECKS
1970 /*
1971 * just marked it hardirq-safe, check that this lock
1972 * took no hardirq-unsafe-read lock in the past:
1973 */
1974 if (!check_usage_forwards(curr, this,
1975 LOCK_ENABLED_HARDIRQS_READ, "hard-read"))
1976 return 0;
1977#endif
Dave Jonesf82b2172008-08-11 09:30:23 +02001978 if (hardirq_verbose(hlock_class(this)))
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001979 ret = 2;
1980 break;
1981 case LOCK_USED_IN_SOFTIRQ:
1982 if (!valid_state(curr, this, new_bit, LOCK_ENABLED_SOFTIRQS))
1983 return 0;
1984 if (!valid_state(curr, this, new_bit,
1985 LOCK_ENABLED_SOFTIRQS_READ))
1986 return 0;
1987 /*
1988 * just marked it softirq-safe, check that this lock
1989 * took no softirq-unsafe lock in the past:
1990 */
1991 if (!check_usage_forwards(curr, this,
1992 LOCK_ENABLED_SOFTIRQS, "soft"))
1993 return 0;
1994#if STRICT_READ_CHECKS
1995 /*
1996 * just marked it softirq-safe, check that this lock
1997 * took no softirq-unsafe-read lock in the past:
1998 */
1999 if (!check_usage_forwards(curr, this,
2000 LOCK_ENABLED_SOFTIRQS_READ, "soft-read"))
2001 return 0;
2002#endif
Dave Jonesf82b2172008-08-11 09:30:23 +02002003 if (softirq_verbose(hlock_class(this)))
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002004 ret = 2;
2005 break;
2006 case LOCK_USED_IN_HARDIRQ_READ:
2007 if (!valid_state(curr, this, new_bit, LOCK_ENABLED_HARDIRQS))
2008 return 0;
2009 /*
2010 * just marked it hardirq-read-safe, check that this lock
2011 * took no hardirq-unsafe lock in the past:
2012 */
2013 if (!check_usage_forwards(curr, this,
2014 LOCK_ENABLED_HARDIRQS, "hard"))
2015 return 0;
Dave Jonesf82b2172008-08-11 09:30:23 +02002016 if (hardirq_verbose(hlock_class(this)))
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002017 ret = 2;
2018 break;
2019 case LOCK_USED_IN_SOFTIRQ_READ:
2020 if (!valid_state(curr, this, new_bit, LOCK_ENABLED_SOFTIRQS))
2021 return 0;
2022 /*
2023 * just marked it softirq-read-safe, check that this lock
2024 * took no softirq-unsafe lock in the past:
2025 */
2026 if (!check_usage_forwards(curr, this,
2027 LOCK_ENABLED_SOFTIRQS, "soft"))
2028 return 0;
Dave Jonesf82b2172008-08-11 09:30:23 +02002029 if (softirq_verbose(hlock_class(this)))
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002030 ret = 2;
2031 break;
2032 case LOCK_ENABLED_HARDIRQS:
2033 if (!valid_state(curr, this, new_bit, LOCK_USED_IN_HARDIRQ))
2034 return 0;
2035 if (!valid_state(curr, this, new_bit,
2036 LOCK_USED_IN_HARDIRQ_READ))
2037 return 0;
2038 /*
2039 * just marked it hardirq-unsafe, check that no hardirq-safe
2040 * lock in the system ever took it in the past:
2041 */
2042 if (!check_usage_backwards(curr, this,
2043 LOCK_USED_IN_HARDIRQ, "hard"))
2044 return 0;
2045#if STRICT_READ_CHECKS
2046 /*
2047 * just marked it hardirq-unsafe, check that no
2048 * hardirq-safe-read lock in the system ever took
2049 * it in the past:
2050 */
2051 if (!check_usage_backwards(curr, this,
2052 LOCK_USED_IN_HARDIRQ_READ, "hard-read"))
2053 return 0;
2054#endif
Dave Jonesf82b2172008-08-11 09:30:23 +02002055 if (hardirq_verbose(hlock_class(this)))
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002056 ret = 2;
2057 break;
2058 case LOCK_ENABLED_SOFTIRQS:
2059 if (!valid_state(curr, this, new_bit, LOCK_USED_IN_SOFTIRQ))
2060 return 0;
2061 if (!valid_state(curr, this, new_bit,
2062 LOCK_USED_IN_SOFTIRQ_READ))
2063 return 0;
2064 /*
2065 * just marked it softirq-unsafe, check that no softirq-safe
2066 * lock in the system ever took it in the past:
2067 */
2068 if (!check_usage_backwards(curr, this,
2069 LOCK_USED_IN_SOFTIRQ, "soft"))
2070 return 0;
2071#if STRICT_READ_CHECKS
2072 /*
2073 * just marked it softirq-unsafe, check that no
2074 * softirq-safe-read lock in the system ever took
2075 * it in the past:
2076 */
2077 if (!check_usage_backwards(curr, this,
2078 LOCK_USED_IN_SOFTIRQ_READ, "soft-read"))
2079 return 0;
2080#endif
Dave Jonesf82b2172008-08-11 09:30:23 +02002081 if (softirq_verbose(hlock_class(this)))
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002082 ret = 2;
2083 break;
2084 case LOCK_ENABLED_HARDIRQS_READ:
2085 if (!valid_state(curr, this, new_bit, LOCK_USED_IN_HARDIRQ))
2086 return 0;
2087#if STRICT_READ_CHECKS
2088 /*
2089 * just marked it hardirq-read-unsafe, check that no
2090 * hardirq-safe lock in the system ever took it in the past:
2091 */
2092 if (!check_usage_backwards(curr, this,
2093 LOCK_USED_IN_HARDIRQ, "hard"))
2094 return 0;
2095#endif
Dave Jonesf82b2172008-08-11 09:30:23 +02002096 if (hardirq_verbose(hlock_class(this)))
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002097 ret = 2;
2098 break;
2099 case LOCK_ENABLED_SOFTIRQS_READ:
2100 if (!valid_state(curr, this, new_bit, LOCK_USED_IN_SOFTIRQ))
2101 return 0;
2102#if STRICT_READ_CHECKS
2103 /*
2104 * just marked it softirq-read-unsafe, check that no
2105 * softirq-safe lock in the system ever took it in the past:
2106 */
2107 if (!check_usage_backwards(curr, this,
2108 LOCK_USED_IN_SOFTIRQ, "soft"))
2109 return 0;
2110#endif
Dave Jonesf82b2172008-08-11 09:30:23 +02002111 if (softirq_verbose(hlock_class(this)))
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002112 ret = 2;
2113 break;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002114 default:
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002115 WARN_ON(1);
Peter Zijlstra8e182572007-07-19 01:48:54 -07002116 break;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002117 }
2118
2119 return ret;
2120}
2121
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002122/*
2123 * Mark all held locks with a usage bit:
2124 */
Steven Rostedt1d09daa2008-05-12 21:20:55 +02002125static int
Jarek Poplawski4ff773bb2007-05-08 00:31:00 -07002126mark_held_locks(struct task_struct *curr, int hardirq)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002127{
2128 enum lock_usage_bit usage_bit;
2129 struct held_lock *hlock;
2130 int i;
2131
2132 for (i = 0; i < curr->lockdep_depth; i++) {
2133 hlock = curr->held_locks + i;
2134
2135 if (hardirq) {
2136 if (hlock->read)
2137 usage_bit = LOCK_ENABLED_HARDIRQS_READ;
2138 else
2139 usage_bit = LOCK_ENABLED_HARDIRQS;
2140 } else {
2141 if (hlock->read)
2142 usage_bit = LOCK_ENABLED_SOFTIRQS_READ;
2143 else
2144 usage_bit = LOCK_ENABLED_SOFTIRQS;
2145 }
Jarek Poplawski4ff773bb2007-05-08 00:31:00 -07002146 if (!mark_lock(curr, hlock, usage_bit))
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002147 return 0;
2148 }
2149
2150 return 1;
2151}
2152
2153/*
2154 * Debugging helper: via this flag we know that we are in
2155 * 'early bootup code', and will warn about any invalid irqs-on event:
2156 */
2157static int early_boot_irqs_enabled;
2158
2159void early_boot_irqs_off(void)
2160{
2161 early_boot_irqs_enabled = 0;
2162}
2163
2164void early_boot_irqs_on(void)
2165{
2166 early_boot_irqs_enabled = 1;
2167}
2168
2169/*
2170 * Hardirqs will be enabled:
2171 */
Heiko Carstens6afe40b2008-10-28 11:14:58 +01002172void trace_hardirqs_on_caller(unsigned long ip)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002173{
2174 struct task_struct *curr = current;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002175
Heiko Carstens6afe40b2008-10-28 11:14:58 +01002176 time_hardirqs_on(CALLER_ADDR0, ip);
Steven Rostedt81d68a92008-05-12 21:20:42 +02002177
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002178 if (unlikely(!debug_locks || current->lockdep_recursion))
2179 return;
2180
2181 if (DEBUG_LOCKS_WARN_ON(unlikely(!early_boot_irqs_enabled)))
2182 return;
2183
2184 if (unlikely(curr->hardirqs_enabled)) {
2185 debug_atomic_inc(&redundant_hardirqs_on);
2186 return;
2187 }
2188 /* we'll do an OFF -> ON transition: */
2189 curr->hardirqs_enabled = 1;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002190
2191 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2192 return;
2193 if (DEBUG_LOCKS_WARN_ON(current->hardirq_context))
2194 return;
2195 /*
2196 * We are going to turn hardirqs on, so set the
2197 * usage bit for all held locks:
2198 */
Jarek Poplawski4ff773bb2007-05-08 00:31:00 -07002199 if (!mark_held_locks(curr, 1))
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002200 return;
2201 /*
2202 * If we have softirqs enabled, then set the usage
2203 * bit for all held locks. (disabled hardirqs prevented
2204 * this bit from being set before)
2205 */
2206 if (curr->softirqs_enabled)
Jarek Poplawski4ff773bb2007-05-08 00:31:00 -07002207 if (!mark_held_locks(curr, 0))
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002208 return;
2209
2210 curr->hardirq_enable_ip = ip;
2211 curr->hardirq_enable_event = ++curr->irq_events;
2212 debug_atomic_inc(&hardirqs_on_events);
2213}
Steven Rostedt81d68a92008-05-12 21:20:42 +02002214EXPORT_SYMBOL(trace_hardirqs_on_caller);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002215
Steven Rostedt1d09daa2008-05-12 21:20:55 +02002216void trace_hardirqs_on(void)
Steven Rostedt81d68a92008-05-12 21:20:42 +02002217{
2218 trace_hardirqs_on_caller(CALLER_ADDR0);
2219}
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002220EXPORT_SYMBOL(trace_hardirqs_on);
2221
2222/*
2223 * Hardirqs were disabled:
2224 */
Heiko Carstens6afe40b2008-10-28 11:14:58 +01002225void trace_hardirqs_off_caller(unsigned long ip)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002226{
2227 struct task_struct *curr = current;
2228
Heiko Carstens6afe40b2008-10-28 11:14:58 +01002229 time_hardirqs_off(CALLER_ADDR0, ip);
Steven Rostedt81d68a92008-05-12 21:20:42 +02002230
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002231 if (unlikely(!debug_locks || current->lockdep_recursion))
2232 return;
2233
2234 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2235 return;
2236
2237 if (curr->hardirqs_enabled) {
2238 /*
2239 * We have done an ON -> OFF transition:
2240 */
2241 curr->hardirqs_enabled = 0;
Heiko Carstens6afe40b2008-10-28 11:14:58 +01002242 curr->hardirq_disable_ip = ip;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002243 curr->hardirq_disable_event = ++curr->irq_events;
2244 debug_atomic_inc(&hardirqs_off_events);
2245 } else
2246 debug_atomic_inc(&redundant_hardirqs_off);
2247}
Steven Rostedt81d68a92008-05-12 21:20:42 +02002248EXPORT_SYMBOL(trace_hardirqs_off_caller);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002249
Steven Rostedt1d09daa2008-05-12 21:20:55 +02002250void trace_hardirqs_off(void)
Steven Rostedt81d68a92008-05-12 21:20:42 +02002251{
2252 trace_hardirqs_off_caller(CALLER_ADDR0);
2253}
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002254EXPORT_SYMBOL(trace_hardirqs_off);
2255
2256/*
2257 * Softirqs will be enabled:
2258 */
2259void trace_softirqs_on(unsigned long ip)
2260{
2261 struct task_struct *curr = current;
2262
2263 if (unlikely(!debug_locks))
2264 return;
2265
2266 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2267 return;
2268
2269 if (curr->softirqs_enabled) {
2270 debug_atomic_inc(&redundant_softirqs_on);
2271 return;
2272 }
2273
2274 /*
2275 * We'll do an OFF -> ON transition:
2276 */
2277 curr->softirqs_enabled = 1;
2278 curr->softirq_enable_ip = ip;
2279 curr->softirq_enable_event = ++curr->irq_events;
2280 debug_atomic_inc(&softirqs_on_events);
2281 /*
2282 * We are going to turn softirqs on, so set the
2283 * usage bit for all held locks, if hardirqs are
2284 * enabled too:
2285 */
2286 if (curr->hardirqs_enabled)
Jarek Poplawski4ff773bb2007-05-08 00:31:00 -07002287 mark_held_locks(curr, 0);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002288}
2289
2290/*
2291 * Softirqs were disabled:
2292 */
2293void trace_softirqs_off(unsigned long ip)
2294{
2295 struct task_struct *curr = current;
2296
2297 if (unlikely(!debug_locks))
2298 return;
2299
2300 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2301 return;
2302
2303 if (curr->softirqs_enabled) {
2304 /*
2305 * We have done an ON -> OFF transition:
2306 */
2307 curr->softirqs_enabled = 0;
2308 curr->softirq_disable_ip = ip;
2309 curr->softirq_disable_event = ++curr->irq_events;
2310 debug_atomic_inc(&softirqs_off_events);
2311 DEBUG_LOCKS_WARN_ON(!softirq_count());
2312 } else
2313 debug_atomic_inc(&redundant_softirqs_off);
2314}
2315
Peter Zijlstra8e182572007-07-19 01:48:54 -07002316static int mark_irqflags(struct task_struct *curr, struct held_lock *hlock)
2317{
2318 /*
2319 * If non-trylock use in a hardirq or softirq context, then
2320 * mark the lock as used in these contexts:
2321 */
2322 if (!hlock->trylock) {
2323 if (hlock->read) {
2324 if (curr->hardirq_context)
2325 if (!mark_lock(curr, hlock,
2326 LOCK_USED_IN_HARDIRQ_READ))
2327 return 0;
2328 if (curr->softirq_context)
2329 if (!mark_lock(curr, hlock,
2330 LOCK_USED_IN_SOFTIRQ_READ))
2331 return 0;
2332 } else {
2333 if (curr->hardirq_context)
2334 if (!mark_lock(curr, hlock, LOCK_USED_IN_HARDIRQ))
2335 return 0;
2336 if (curr->softirq_context)
2337 if (!mark_lock(curr, hlock, LOCK_USED_IN_SOFTIRQ))
2338 return 0;
2339 }
2340 }
2341 if (!hlock->hardirqs_off) {
2342 if (hlock->read) {
2343 if (!mark_lock(curr, hlock,
2344 LOCK_ENABLED_HARDIRQS_READ))
2345 return 0;
2346 if (curr->softirqs_enabled)
2347 if (!mark_lock(curr, hlock,
2348 LOCK_ENABLED_SOFTIRQS_READ))
2349 return 0;
2350 } else {
2351 if (!mark_lock(curr, hlock,
2352 LOCK_ENABLED_HARDIRQS))
2353 return 0;
2354 if (curr->softirqs_enabled)
2355 if (!mark_lock(curr, hlock,
2356 LOCK_ENABLED_SOFTIRQS))
2357 return 0;
2358 }
2359 }
2360
2361 return 1;
2362}
2363
2364static int separate_irq_context(struct task_struct *curr,
2365 struct held_lock *hlock)
2366{
2367 unsigned int depth = curr->lockdep_depth;
2368
2369 /*
2370 * Keep track of points where we cross into an interrupt context:
2371 */
2372 hlock->irq_context = 2*(curr->hardirq_context ? 1 : 0) +
2373 curr->softirq_context;
2374 if (depth) {
2375 struct held_lock *prev_hlock;
2376
2377 prev_hlock = curr->held_locks + depth-1;
2378 /*
2379 * If we cross into another context, reset the
2380 * hash key (this also prevents the checking and the
2381 * adding of the dependency to 'prev'):
2382 */
2383 if (prev_hlock->irq_context != hlock->irq_context)
2384 return 1;
2385 }
2386 return 0;
2387}
2388
2389#else
2390
2391static inline
2392int mark_lock_irq(struct task_struct *curr, struct held_lock *this,
2393 enum lock_usage_bit new_bit)
2394{
2395 WARN_ON(1);
2396 return 1;
2397}
2398
2399static inline int mark_irqflags(struct task_struct *curr,
2400 struct held_lock *hlock)
2401{
2402 return 1;
2403}
2404
2405static inline int separate_irq_context(struct task_struct *curr,
2406 struct held_lock *hlock)
2407{
2408 return 0;
2409}
2410
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002411#endif
2412
2413/*
Peter Zijlstra8e182572007-07-19 01:48:54 -07002414 * Mark a lock with a usage bit, and validate the state transition:
2415 */
Steven Rostedt1d09daa2008-05-12 21:20:55 +02002416static int mark_lock(struct task_struct *curr, struct held_lock *this,
Steven Rostedt0764d232008-05-12 21:20:44 +02002417 enum lock_usage_bit new_bit)
Peter Zijlstra8e182572007-07-19 01:48:54 -07002418{
2419 unsigned int new_mask = 1 << new_bit, ret = 1;
2420
2421 /*
2422 * If already set then do not dirty the cacheline,
2423 * nor do any checks:
2424 */
Dave Jonesf82b2172008-08-11 09:30:23 +02002425 if (likely(hlock_class(this)->usage_mask & new_mask))
Peter Zijlstra8e182572007-07-19 01:48:54 -07002426 return 1;
2427
2428 if (!graph_lock())
2429 return 0;
2430 /*
2431 * Make sure we didnt race:
2432 */
Dave Jonesf82b2172008-08-11 09:30:23 +02002433 if (unlikely(hlock_class(this)->usage_mask & new_mask)) {
Peter Zijlstra8e182572007-07-19 01:48:54 -07002434 graph_unlock();
2435 return 1;
2436 }
2437
Dave Jonesf82b2172008-08-11 09:30:23 +02002438 hlock_class(this)->usage_mask |= new_mask;
Peter Zijlstra8e182572007-07-19 01:48:54 -07002439
Dave Jonesf82b2172008-08-11 09:30:23 +02002440 if (!save_trace(hlock_class(this)->usage_traces + new_bit))
Peter Zijlstra8e182572007-07-19 01:48:54 -07002441 return 0;
2442
2443 switch (new_bit) {
2444 case LOCK_USED_IN_HARDIRQ:
2445 case LOCK_USED_IN_SOFTIRQ:
2446 case LOCK_USED_IN_HARDIRQ_READ:
2447 case LOCK_USED_IN_SOFTIRQ_READ:
2448 case LOCK_ENABLED_HARDIRQS:
2449 case LOCK_ENABLED_SOFTIRQS:
2450 case LOCK_ENABLED_HARDIRQS_READ:
2451 case LOCK_ENABLED_SOFTIRQS_READ:
2452 ret = mark_lock_irq(curr, this, new_bit);
2453 if (!ret)
2454 return 0;
2455 break;
2456 case LOCK_USED:
Peter Zijlstra8e182572007-07-19 01:48:54 -07002457 debug_atomic_dec(&nr_unused_locks);
2458 break;
2459 default:
2460 if (!debug_locks_off_graph_unlock())
2461 return 0;
2462 WARN_ON(1);
2463 return 0;
2464 }
2465
2466 graph_unlock();
2467
2468 /*
2469 * We must printk outside of the graph_lock:
2470 */
2471 if (ret == 2) {
2472 printk("\nmarked lock as {%s}:\n", usage_str[new_bit]);
2473 print_lock(this);
2474 print_irqtrace_events(curr);
2475 dump_stack();
2476 }
2477
2478 return ret;
2479}
2480
2481/*
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002482 * Initialize a lock instance's lock-class mapping info:
2483 */
2484void lockdep_init_map(struct lockdep_map *lock, const char *name,
Peter Zijlstra4dfbb9d2006-10-11 01:45:14 -04002485 struct lock_class_key *key, int subclass)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002486{
2487 if (unlikely(!debug_locks))
2488 return;
2489
2490 if (DEBUG_LOCKS_WARN_ON(!key))
2491 return;
2492 if (DEBUG_LOCKS_WARN_ON(!name))
2493 return;
2494 /*
2495 * Sanity check, the lock-class key must be persistent:
2496 */
2497 if (!static_obj(key)) {
2498 printk("BUG: key %p not in .data!\n", key);
2499 DEBUG_LOCKS_WARN_ON(1);
2500 return;
2501 }
2502 lock->name = name;
2503 lock->key = key;
Ingo Molnard6d897c2006-07-10 04:44:04 -07002504 lock->class_cache = NULL;
Peter Zijlstra96645672007-07-19 01:49:00 -07002505#ifdef CONFIG_LOCK_STAT
2506 lock->cpu = raw_smp_processor_id();
2507#endif
Peter Zijlstra4dfbb9d2006-10-11 01:45:14 -04002508 if (subclass)
2509 register_lock_class(lock, subclass, 1);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002510}
2511
2512EXPORT_SYMBOL_GPL(lockdep_init_map);
2513
2514/*
2515 * This gets called for every mutex_lock*()/spin_lock*() operation.
2516 * We maintain the dependency maps and validate the locking attempt:
2517 */
2518static int __lock_acquire(struct lockdep_map *lock, unsigned int subclass,
2519 int trylock, int read, int check, int hardirqs_off,
Peter Zijlstra7531e2f2008-08-11 09:30:24 +02002520 struct lockdep_map *nest_lock, unsigned long ip)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002521{
2522 struct task_struct *curr = current;
Ingo Molnard6d897c2006-07-10 04:44:04 -07002523 struct lock_class *class = NULL;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002524 struct held_lock *hlock;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002525 unsigned int depth, id;
2526 int chain_head = 0;
2527 u64 chain_key;
2528
Peter Zijlstraf20786f2007-07-19 01:48:56 -07002529 if (!prove_locking)
2530 check = 1;
2531
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002532 if (unlikely(!debug_locks))
2533 return 0;
2534
2535 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2536 return 0;
2537
2538 if (unlikely(subclass >= MAX_LOCKDEP_SUBCLASSES)) {
2539 debug_locks_off();
2540 printk("BUG: MAX_LOCKDEP_SUBCLASSES too low!\n");
2541 printk("turning off the locking correctness validator.\n");
2542 return 0;
2543 }
2544
Ingo Molnard6d897c2006-07-10 04:44:04 -07002545 if (!subclass)
2546 class = lock->class_cache;
2547 /*
2548 * Not cached yet or subclass?
2549 */
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002550 if (unlikely(!class)) {
Peter Zijlstra4dfbb9d2006-10-11 01:45:14 -04002551 class = register_lock_class(lock, subclass, 0);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002552 if (!class)
2553 return 0;
2554 }
2555 debug_atomic_inc((atomic_t *)&class->ops);
2556 if (very_verbose(class)) {
2557 printk("\nacquire class [%p] %s", class->key, class->name);
2558 if (class->name_version > 1)
2559 printk("#%d", class->name_version);
2560 printk("\n");
2561 dump_stack();
2562 }
2563
2564 /*
2565 * Add the lock to the list of currently held locks.
2566 * (we dont increase the depth just yet, up until the
2567 * dependency checks are done)
2568 */
2569 depth = curr->lockdep_depth;
2570 if (DEBUG_LOCKS_WARN_ON(depth >= MAX_LOCK_DEPTH))
2571 return 0;
2572
2573 hlock = curr->held_locks + depth;
Dave Jonesf82b2172008-08-11 09:30:23 +02002574 if (DEBUG_LOCKS_WARN_ON(!class))
2575 return 0;
2576 hlock->class_idx = class - lock_classes + 1;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002577 hlock->acquire_ip = ip;
2578 hlock->instance = lock;
Peter Zijlstra7531e2f2008-08-11 09:30:24 +02002579 hlock->nest_lock = nest_lock;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002580 hlock->trylock = trylock;
2581 hlock->read = read;
2582 hlock->check = check;
Dmitry Baryshkov6951b122008-08-18 04:26:37 +04002583 hlock->hardirqs_off = !!hardirqs_off;
Peter Zijlstraf20786f2007-07-19 01:48:56 -07002584#ifdef CONFIG_LOCK_STAT
2585 hlock->waittime_stamp = 0;
2586 hlock->holdtime_stamp = sched_clock();
2587#endif
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002588
Peter Zijlstra8e182572007-07-19 01:48:54 -07002589 if (check == 2 && !mark_irqflags(curr, hlock))
2590 return 0;
2591
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002592 /* mark it as used: */
Jarek Poplawski4ff773bb2007-05-08 00:31:00 -07002593 if (!mark_lock(curr, hlock, LOCK_USED))
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002594 return 0;
Peter Zijlstra8e182572007-07-19 01:48:54 -07002595
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002596 /*
Gautham R Shenoy17aacfb2007-10-28 20:47:01 +01002597 * Calculate the chain hash: it's the combined hash of all the
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002598 * lock keys along the dependency chain. We save the hash value
2599 * at every step so that we can get the current hash easily
2600 * after unlock. The chain hash is then used to cache dependency
2601 * results.
2602 *
2603 * The 'key ID' is what is the most compact key value to drive
2604 * the hash, not class->key.
2605 */
2606 id = class - lock_classes;
2607 if (DEBUG_LOCKS_WARN_ON(id >= MAX_LOCKDEP_KEYS))
2608 return 0;
2609
2610 chain_key = curr->curr_chain_key;
2611 if (!depth) {
2612 if (DEBUG_LOCKS_WARN_ON(chain_key != 0))
2613 return 0;
2614 chain_head = 1;
2615 }
2616
2617 hlock->prev_chain_key = chain_key;
Peter Zijlstra8e182572007-07-19 01:48:54 -07002618 if (separate_irq_context(curr, hlock)) {
2619 chain_key = 0;
2620 chain_head = 1;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002621 }
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002622 chain_key = iterate_chain_key(chain_key, id);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002623
Gregory Haskins3aa416b2007-10-11 22:11:11 +02002624 if (!validate_chain(curr, lock, hlock, chain_head, chain_key))
Peter Zijlstra8e182572007-07-19 01:48:54 -07002625 return 0;
Jarek Poplawski381a2292007-02-10 01:44:58 -08002626
Gregory Haskins3aa416b2007-10-11 22:11:11 +02002627 curr->curr_chain_key = chain_key;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002628 curr->lockdep_depth++;
2629 check_chain_key(curr);
Jarek Poplawski60e114d2007-02-20 13:58:00 -08002630#ifdef CONFIG_DEBUG_LOCKDEP
2631 if (unlikely(!debug_locks))
2632 return 0;
2633#endif
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002634 if (unlikely(curr->lockdep_depth >= MAX_LOCK_DEPTH)) {
2635 debug_locks_off();
2636 printk("BUG: MAX_LOCK_DEPTH too low!\n");
2637 printk("turning off the locking correctness validator.\n");
2638 return 0;
2639 }
Jarek Poplawski381a2292007-02-10 01:44:58 -08002640
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002641 if (unlikely(curr->lockdep_depth > max_lockdep_depth))
2642 max_lockdep_depth = curr->lockdep_depth;
2643
2644 return 1;
2645}
2646
2647static int
2648print_unlock_inbalance_bug(struct task_struct *curr, struct lockdep_map *lock,
2649 unsigned long ip)
2650{
2651 if (!debug_locks_off())
2652 return 0;
2653 if (debug_locks_silent)
2654 return 0;
2655
2656 printk("\n=====================================\n");
2657 printk( "[ BUG: bad unlock balance detected! ]\n");
2658 printk( "-------------------------------------\n");
2659 printk("%s/%d is trying to release lock (",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07002660 curr->comm, task_pid_nr(curr));
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002661 print_lockdep_cache(lock);
2662 printk(") at:\n");
2663 print_ip_sym(ip);
2664 printk("but there are no more locks to release!\n");
2665 printk("\nother info that might help us debug this:\n");
2666 lockdep_print_held_locks(curr);
2667
2668 printk("\nstack backtrace:\n");
2669 dump_stack();
2670
2671 return 0;
2672}
2673
2674/*
2675 * Common debugging checks for both nested and non-nested unlock:
2676 */
2677static int check_unlock(struct task_struct *curr, struct lockdep_map *lock,
2678 unsigned long ip)
2679{
2680 if (unlikely(!debug_locks))
2681 return 0;
2682 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2683 return 0;
2684
2685 if (curr->lockdep_depth <= 0)
2686 return print_unlock_inbalance_bug(curr, lock, ip);
2687
2688 return 1;
2689}
2690
Peter Zijlstra64aa3482008-08-11 09:30:21 +02002691static int
2692__lock_set_subclass(struct lockdep_map *lock,
2693 unsigned int subclass, unsigned long ip)
2694{
2695 struct task_struct *curr = current;
2696 struct held_lock *hlock, *prev_hlock;
2697 struct lock_class *class;
2698 unsigned int depth;
2699 int i;
2700
2701 depth = curr->lockdep_depth;
2702 if (DEBUG_LOCKS_WARN_ON(!depth))
2703 return 0;
2704
2705 prev_hlock = NULL;
2706 for (i = depth-1; i >= 0; i--) {
2707 hlock = curr->held_locks + i;
2708 /*
2709 * We must not cross into another context:
2710 */
2711 if (prev_hlock && prev_hlock->irq_context != hlock->irq_context)
2712 break;
2713 if (hlock->instance == lock)
2714 goto found_it;
2715 prev_hlock = hlock;
2716 }
2717 return print_unlock_inbalance_bug(curr, lock, ip);
2718
2719found_it:
2720 class = register_lock_class(lock, subclass, 0);
Dave Jonesf82b2172008-08-11 09:30:23 +02002721 hlock->class_idx = class - lock_classes + 1;
Peter Zijlstra64aa3482008-08-11 09:30:21 +02002722
2723 curr->lockdep_depth = i;
2724 curr->curr_chain_key = hlock->prev_chain_key;
2725
2726 for (; i < depth; i++) {
2727 hlock = curr->held_locks + i;
2728 if (!__lock_acquire(hlock->instance,
Dave Jonesf82b2172008-08-11 09:30:23 +02002729 hlock_class(hlock)->subclass, hlock->trylock,
Peter Zijlstra64aa3482008-08-11 09:30:21 +02002730 hlock->read, hlock->check, hlock->hardirqs_off,
Peter Zijlstra7531e2f2008-08-11 09:30:24 +02002731 hlock->nest_lock, hlock->acquire_ip))
Peter Zijlstra64aa3482008-08-11 09:30:21 +02002732 return 0;
2733 }
2734
2735 if (DEBUG_LOCKS_WARN_ON(curr->lockdep_depth != depth))
2736 return 0;
2737 return 1;
2738}
2739
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002740/*
2741 * Remove the lock to the list of currently held locks in a
2742 * potentially non-nested (out of order) manner. This is a
2743 * relatively rare operation, as all the unlock APIs default
2744 * to nested mode (which uses lock_release()):
2745 */
2746static int
2747lock_release_non_nested(struct task_struct *curr,
2748 struct lockdep_map *lock, unsigned long ip)
2749{
2750 struct held_lock *hlock, *prev_hlock;
2751 unsigned int depth;
2752 int i;
2753
2754 /*
2755 * Check whether the lock exists in the current stack
2756 * of held locks:
2757 */
2758 depth = curr->lockdep_depth;
2759 if (DEBUG_LOCKS_WARN_ON(!depth))
2760 return 0;
2761
2762 prev_hlock = NULL;
2763 for (i = depth-1; i >= 0; i--) {
2764 hlock = curr->held_locks + i;
2765 /*
2766 * We must not cross into another context:
2767 */
2768 if (prev_hlock && prev_hlock->irq_context != hlock->irq_context)
2769 break;
2770 if (hlock->instance == lock)
2771 goto found_it;
2772 prev_hlock = hlock;
2773 }
2774 return print_unlock_inbalance_bug(curr, lock, ip);
2775
2776found_it:
Peter Zijlstraf20786f2007-07-19 01:48:56 -07002777 lock_release_holdtime(hlock);
2778
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002779 /*
2780 * We have the right lock to unlock, 'hlock' points to it.
2781 * Now we remove it from the stack, and add back the other
2782 * entries (if any), recalculating the hash along the way:
2783 */
2784 curr->lockdep_depth = i;
2785 curr->curr_chain_key = hlock->prev_chain_key;
2786
2787 for (i++; i < depth; i++) {
2788 hlock = curr->held_locks + i;
2789 if (!__lock_acquire(hlock->instance,
Dave Jonesf82b2172008-08-11 09:30:23 +02002790 hlock_class(hlock)->subclass, hlock->trylock,
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002791 hlock->read, hlock->check, hlock->hardirqs_off,
Peter Zijlstra7531e2f2008-08-11 09:30:24 +02002792 hlock->nest_lock, hlock->acquire_ip))
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002793 return 0;
2794 }
2795
2796 if (DEBUG_LOCKS_WARN_ON(curr->lockdep_depth != depth - 1))
2797 return 0;
2798 return 1;
2799}
2800
2801/*
2802 * Remove the lock to the list of currently held locks - this gets
2803 * called on mutex_unlock()/spin_unlock*() (or on a failed
2804 * mutex_lock_interruptible()). This is done for unlocks that nest
2805 * perfectly. (i.e. the current top of the lock-stack is unlocked)
2806 */
2807static int lock_release_nested(struct task_struct *curr,
2808 struct lockdep_map *lock, unsigned long ip)
2809{
2810 struct held_lock *hlock;
2811 unsigned int depth;
2812
2813 /*
2814 * Pop off the top of the lock stack:
2815 */
2816 depth = curr->lockdep_depth - 1;
2817 hlock = curr->held_locks + depth;
2818
2819 /*
2820 * Is the unlock non-nested:
2821 */
2822 if (hlock->instance != lock)
2823 return lock_release_non_nested(curr, lock, ip);
2824 curr->lockdep_depth--;
2825
2826 if (DEBUG_LOCKS_WARN_ON(!depth && (hlock->prev_chain_key != 0)))
2827 return 0;
2828
2829 curr->curr_chain_key = hlock->prev_chain_key;
2830
Peter Zijlstraf20786f2007-07-19 01:48:56 -07002831 lock_release_holdtime(hlock);
2832
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002833#ifdef CONFIG_DEBUG_LOCKDEP
2834 hlock->prev_chain_key = 0;
Dave Jonesf82b2172008-08-11 09:30:23 +02002835 hlock->class_idx = 0;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002836 hlock->acquire_ip = 0;
2837 hlock->irq_context = 0;
2838#endif
2839 return 1;
2840}
2841
2842/*
2843 * Remove the lock to the list of currently held locks - this gets
2844 * called on mutex_unlock()/spin_unlock*() (or on a failed
2845 * mutex_lock_interruptible()). This is done for unlocks that nest
2846 * perfectly. (i.e. the current top of the lock-stack is unlocked)
2847 */
2848static void
2849__lock_release(struct lockdep_map *lock, int nested, unsigned long ip)
2850{
2851 struct task_struct *curr = current;
2852
2853 if (!check_unlock(curr, lock, ip))
2854 return;
2855
2856 if (nested) {
2857 if (!lock_release_nested(curr, lock, ip))
2858 return;
2859 } else {
2860 if (!lock_release_non_nested(curr, lock, ip))
2861 return;
2862 }
2863
2864 check_chain_key(curr);
2865}
2866
2867/*
2868 * Check whether we follow the irq-flags state precisely:
2869 */
Steven Rostedt1d09daa2008-05-12 21:20:55 +02002870static void check_flags(unsigned long flags)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002871{
Ingo Molnar992860e2008-07-14 10:28:38 +02002872#if defined(CONFIG_PROVE_LOCKING) && defined(CONFIG_DEBUG_LOCKDEP) && \
2873 defined(CONFIG_TRACE_IRQFLAGS)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002874 if (!debug_locks)
2875 return;
2876
Ingo Molnar5f9fa8a2007-12-07 19:02:47 +01002877 if (irqs_disabled_flags(flags)) {
2878 if (DEBUG_LOCKS_WARN_ON(current->hardirqs_enabled)) {
2879 printk("possible reason: unannotated irqs-off.\n");
2880 }
2881 } else {
2882 if (DEBUG_LOCKS_WARN_ON(!current->hardirqs_enabled)) {
2883 printk("possible reason: unannotated irqs-on.\n");
2884 }
2885 }
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002886
2887 /*
2888 * We dont accurately track softirq state in e.g.
2889 * hardirq contexts (such as on 4KSTACKS), so only
2890 * check if not in hardirq contexts:
2891 */
2892 if (!hardirq_count()) {
2893 if (softirq_count())
2894 DEBUG_LOCKS_WARN_ON(current->softirqs_enabled);
2895 else
2896 DEBUG_LOCKS_WARN_ON(!current->softirqs_enabled);
2897 }
2898
2899 if (!debug_locks)
2900 print_irqtrace_events(current);
2901#endif
2902}
2903
Peter Zijlstra64aa3482008-08-11 09:30:21 +02002904void
2905lock_set_subclass(struct lockdep_map *lock,
2906 unsigned int subclass, unsigned long ip)
2907{
2908 unsigned long flags;
2909
2910 if (unlikely(current->lockdep_recursion))
2911 return;
2912
2913 raw_local_irq_save(flags);
2914 current->lockdep_recursion = 1;
2915 check_flags(flags);
2916 if (__lock_set_subclass(lock, subclass, ip))
2917 check_chain_key(current);
2918 current->lockdep_recursion = 0;
2919 raw_local_irq_restore(flags);
2920}
2921
2922EXPORT_SYMBOL_GPL(lock_set_subclass);
2923
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002924/*
2925 * We are not always called with irqs disabled - do that here,
2926 * and also avoid lockdep recursion:
2927 */
Steven Rostedt1d09daa2008-05-12 21:20:55 +02002928void lock_acquire(struct lockdep_map *lock, unsigned int subclass,
Peter Zijlstra7531e2f2008-08-11 09:30:24 +02002929 int trylock, int read, int check,
2930 struct lockdep_map *nest_lock, unsigned long ip)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002931{
2932 unsigned long flags;
2933
2934 if (unlikely(current->lockdep_recursion))
2935 return;
2936
2937 raw_local_irq_save(flags);
2938 check_flags(flags);
2939
2940 current->lockdep_recursion = 1;
2941 __lock_acquire(lock, subclass, trylock, read, check,
Peter Zijlstra7531e2f2008-08-11 09:30:24 +02002942 irqs_disabled_flags(flags), nest_lock, ip);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002943 current->lockdep_recursion = 0;
2944 raw_local_irq_restore(flags);
2945}
2946
2947EXPORT_SYMBOL_GPL(lock_acquire);
2948
Steven Rostedt1d09daa2008-05-12 21:20:55 +02002949void lock_release(struct lockdep_map *lock, int nested,
Steven Rostedt0764d232008-05-12 21:20:44 +02002950 unsigned long ip)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002951{
2952 unsigned long flags;
2953
2954 if (unlikely(current->lockdep_recursion))
2955 return;
2956
2957 raw_local_irq_save(flags);
2958 check_flags(flags);
2959 current->lockdep_recursion = 1;
2960 __lock_release(lock, nested, ip);
2961 current->lockdep_recursion = 0;
2962 raw_local_irq_restore(flags);
2963}
2964
2965EXPORT_SYMBOL_GPL(lock_release);
2966
Peter Zijlstraf20786f2007-07-19 01:48:56 -07002967#ifdef CONFIG_LOCK_STAT
2968static int
2969print_lock_contention_bug(struct task_struct *curr, struct lockdep_map *lock,
2970 unsigned long ip)
2971{
2972 if (!debug_locks_off())
2973 return 0;
2974 if (debug_locks_silent)
2975 return 0;
2976
2977 printk("\n=================================\n");
2978 printk( "[ BUG: bad contention detected! ]\n");
2979 printk( "---------------------------------\n");
2980 printk("%s/%d is trying to contend lock (",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07002981 curr->comm, task_pid_nr(curr));
Peter Zijlstraf20786f2007-07-19 01:48:56 -07002982 print_lockdep_cache(lock);
2983 printk(") at:\n");
2984 print_ip_sym(ip);
2985 printk("but there are no locks held!\n");
2986 printk("\nother info that might help us debug this:\n");
2987 lockdep_print_held_locks(curr);
2988
2989 printk("\nstack backtrace:\n");
2990 dump_stack();
2991
2992 return 0;
2993}
2994
2995static void
2996__lock_contended(struct lockdep_map *lock, unsigned long ip)
2997{
2998 struct task_struct *curr = current;
2999 struct held_lock *hlock, *prev_hlock;
3000 struct lock_class_stats *stats;
3001 unsigned int depth;
3002 int i, point;
3003
3004 depth = curr->lockdep_depth;
3005 if (DEBUG_LOCKS_WARN_ON(!depth))
3006 return;
3007
3008 prev_hlock = NULL;
3009 for (i = depth-1; i >= 0; i--) {
3010 hlock = curr->held_locks + i;
3011 /*
3012 * We must not cross into another context:
3013 */
3014 if (prev_hlock && prev_hlock->irq_context != hlock->irq_context)
3015 break;
3016 if (hlock->instance == lock)
3017 goto found_it;
3018 prev_hlock = hlock;
3019 }
3020 print_lock_contention_bug(curr, lock, ip);
3021 return;
3022
3023found_it:
3024 hlock->waittime_stamp = sched_clock();
3025
Dave Jonesf82b2172008-08-11 09:30:23 +02003026 point = lock_contention_point(hlock_class(hlock), ip);
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003027
Dave Jonesf82b2172008-08-11 09:30:23 +02003028 stats = get_lock_stats(hlock_class(hlock));
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003029 if (point < ARRAY_SIZE(stats->contention_point))
Joe Korty04148b72008-08-25 17:16:23 -04003030 stats->contention_point[point]++;
Peter Zijlstra96645672007-07-19 01:49:00 -07003031 if (lock->cpu != smp_processor_id())
3032 stats->bounces[bounce_contended + !!hlock->read]++;
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003033 put_lock_stats(stats);
3034}
3035
3036static void
3037__lock_acquired(struct lockdep_map *lock)
3038{
3039 struct task_struct *curr = current;
3040 struct held_lock *hlock, *prev_hlock;
3041 struct lock_class_stats *stats;
3042 unsigned int depth;
3043 u64 now;
Peter Zijlstra96645672007-07-19 01:49:00 -07003044 s64 waittime = 0;
3045 int i, cpu;
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003046
3047 depth = curr->lockdep_depth;
3048 if (DEBUG_LOCKS_WARN_ON(!depth))
3049 return;
3050
3051 prev_hlock = NULL;
3052 for (i = depth-1; i >= 0; i--) {
3053 hlock = curr->held_locks + i;
3054 /*
3055 * We must not cross into another context:
3056 */
3057 if (prev_hlock && prev_hlock->irq_context != hlock->irq_context)
3058 break;
3059 if (hlock->instance == lock)
3060 goto found_it;
3061 prev_hlock = hlock;
3062 }
3063 print_lock_contention_bug(curr, lock, _RET_IP_);
3064 return;
3065
3066found_it:
Peter Zijlstra96645672007-07-19 01:49:00 -07003067 cpu = smp_processor_id();
3068 if (hlock->waittime_stamp) {
3069 now = sched_clock();
3070 waittime = now - hlock->waittime_stamp;
3071 hlock->holdtime_stamp = now;
3072 }
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003073
Dave Jonesf82b2172008-08-11 09:30:23 +02003074 stats = get_lock_stats(hlock_class(hlock));
Peter Zijlstra96645672007-07-19 01:49:00 -07003075 if (waittime) {
3076 if (hlock->read)
3077 lock_time_inc(&stats->read_waittime, waittime);
3078 else
3079 lock_time_inc(&stats->write_waittime, waittime);
3080 }
3081 if (lock->cpu != cpu)
3082 stats->bounces[bounce_acquired + !!hlock->read]++;
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003083 put_lock_stats(stats);
Peter Zijlstra96645672007-07-19 01:49:00 -07003084
3085 lock->cpu = cpu;
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003086}
3087
3088void lock_contended(struct lockdep_map *lock, unsigned long ip)
3089{
3090 unsigned long flags;
3091
3092 if (unlikely(!lock_stat))
3093 return;
3094
3095 if (unlikely(current->lockdep_recursion))
3096 return;
3097
3098 raw_local_irq_save(flags);
3099 check_flags(flags);
3100 current->lockdep_recursion = 1;
3101 __lock_contended(lock, ip);
3102 current->lockdep_recursion = 0;
3103 raw_local_irq_restore(flags);
3104}
3105EXPORT_SYMBOL_GPL(lock_contended);
3106
3107void lock_acquired(struct lockdep_map *lock)
3108{
3109 unsigned long flags;
3110
3111 if (unlikely(!lock_stat))
3112 return;
3113
3114 if (unlikely(current->lockdep_recursion))
3115 return;
3116
3117 raw_local_irq_save(flags);
3118 check_flags(flags);
3119 current->lockdep_recursion = 1;
3120 __lock_acquired(lock);
3121 current->lockdep_recursion = 0;
3122 raw_local_irq_restore(flags);
3123}
3124EXPORT_SYMBOL_GPL(lock_acquired);
3125#endif
3126
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003127/*
3128 * Used by the testsuite, sanitize the validator state
3129 * after a simulated failure:
3130 */
3131
3132void lockdep_reset(void)
3133{
3134 unsigned long flags;
Ingo Molnar23d95a02006-12-13 00:34:40 -08003135 int i;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003136
3137 raw_local_irq_save(flags);
3138 current->curr_chain_key = 0;
3139 current->lockdep_depth = 0;
3140 current->lockdep_recursion = 0;
3141 memset(current->held_locks, 0, MAX_LOCK_DEPTH*sizeof(struct held_lock));
3142 nr_hardirq_chains = 0;
3143 nr_softirq_chains = 0;
3144 nr_process_chains = 0;
3145 debug_locks = 1;
Ingo Molnar23d95a02006-12-13 00:34:40 -08003146 for (i = 0; i < CHAINHASH_SIZE; i++)
3147 INIT_LIST_HEAD(chainhash_table + i);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003148 raw_local_irq_restore(flags);
3149}
3150
3151static void zap_class(struct lock_class *class)
3152{
3153 int i;
3154
3155 /*
3156 * Remove all dependencies this lock is
3157 * involved in:
3158 */
3159 for (i = 0; i < nr_list_entries; i++) {
3160 if (list_entries[i].class == class)
3161 list_del_rcu(&list_entries[i].entry);
3162 }
3163 /*
3164 * Unhash the class and remove it from the all_lock_classes list:
3165 */
3166 list_del_rcu(&class->hash_entry);
3167 list_del_rcu(&class->lock_entry);
3168
Rabin Vincent8bfe0292008-08-11 09:30:26 +02003169 class->key = NULL;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003170}
3171
Arjan van de Venfabe8742008-01-24 07:00:45 +01003172static inline int within(const void *addr, void *start, unsigned long size)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003173{
3174 return addr >= start && addr < start + size;
3175}
3176
3177void lockdep_free_key_range(void *start, unsigned long size)
3178{
3179 struct lock_class *class, *next;
3180 struct list_head *head;
3181 unsigned long flags;
3182 int i;
Nick Piggin5a26db52008-01-16 09:51:58 +01003183 int locked;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003184
3185 raw_local_irq_save(flags);
Nick Piggin5a26db52008-01-16 09:51:58 +01003186 locked = graph_lock();
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003187
3188 /*
3189 * Unhash all classes that were created by this module:
3190 */
3191 for (i = 0; i < CLASSHASH_SIZE; i++) {
3192 head = classhash_table + i;
3193 if (list_empty(head))
3194 continue;
Arjan van de Venfabe8742008-01-24 07:00:45 +01003195 list_for_each_entry_safe(class, next, head, hash_entry) {
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003196 if (within(class->key, start, size))
3197 zap_class(class);
Arjan van de Venfabe8742008-01-24 07:00:45 +01003198 else if (within(class->name, start, size))
3199 zap_class(class);
3200 }
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003201 }
3202
Nick Piggin5a26db52008-01-16 09:51:58 +01003203 if (locked)
3204 graph_unlock();
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003205 raw_local_irq_restore(flags);
3206}
3207
3208void lockdep_reset_lock(struct lockdep_map *lock)
3209{
Ingo Molnard6d897c2006-07-10 04:44:04 -07003210 struct lock_class *class, *next;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003211 struct list_head *head;
3212 unsigned long flags;
3213 int i, j;
Nick Piggin5a26db52008-01-16 09:51:58 +01003214 int locked;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003215
3216 raw_local_irq_save(flags);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003217
3218 /*
Ingo Molnard6d897c2006-07-10 04:44:04 -07003219 * Remove all classes this lock might have:
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003220 */
Ingo Molnard6d897c2006-07-10 04:44:04 -07003221 for (j = 0; j < MAX_LOCKDEP_SUBCLASSES; j++) {
3222 /*
3223 * If the class exists we look it up and zap it:
3224 */
3225 class = look_up_lock_class(lock, j);
3226 if (class)
3227 zap_class(class);
3228 }
3229 /*
3230 * Debug check: in the end all mapped classes should
3231 * be gone.
3232 */
Nick Piggin5a26db52008-01-16 09:51:58 +01003233 locked = graph_lock();
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003234 for (i = 0; i < CLASSHASH_SIZE; i++) {
3235 head = classhash_table + i;
3236 if (list_empty(head))
3237 continue;
3238 list_for_each_entry_safe(class, next, head, hash_entry) {
Ingo Molnard6d897c2006-07-10 04:44:04 -07003239 if (unlikely(class == lock->class_cache)) {
Ingo Molnar74c383f2006-12-13 00:34:43 -08003240 if (debug_locks_off_graph_unlock())
3241 WARN_ON(1);
Ingo Molnard6d897c2006-07-10 04:44:04 -07003242 goto out_restore;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003243 }
3244 }
3245 }
Nick Piggin5a26db52008-01-16 09:51:58 +01003246 if (locked)
3247 graph_unlock();
Ingo Molnard6d897c2006-07-10 04:44:04 -07003248
3249out_restore:
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003250 raw_local_irq_restore(flags);
3251}
3252
Sam Ravnborg14999932007-02-28 20:12:31 -08003253void lockdep_init(void)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003254{
3255 int i;
3256
3257 /*
3258 * Some architectures have their own start_kernel()
3259 * code which calls lockdep_init(), while we also
3260 * call lockdep_init() from the start_kernel() itself,
3261 * and we want to initialize the hashes only once:
3262 */
3263 if (lockdep_initialized)
3264 return;
3265
3266 for (i = 0; i < CLASSHASH_SIZE; i++)
3267 INIT_LIST_HEAD(classhash_table + i);
3268
3269 for (i = 0; i < CHAINHASH_SIZE; i++)
3270 INIT_LIST_HEAD(chainhash_table + i);
3271
3272 lockdep_initialized = 1;
3273}
3274
3275void __init lockdep_info(void)
3276{
3277 printk("Lock dependency validator: Copyright (c) 2006 Red Hat, Inc., Ingo Molnar\n");
3278
Li Zefanb0788ca2008-11-21 15:57:32 +08003279 printk("... MAX_LOCKDEP_SUBCLASSES: %lu\n", MAX_LOCKDEP_SUBCLASSES);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003280 printk("... MAX_LOCK_DEPTH: %lu\n", MAX_LOCK_DEPTH);
3281 printk("... MAX_LOCKDEP_KEYS: %lu\n", MAX_LOCKDEP_KEYS);
Li Zefanb0788ca2008-11-21 15:57:32 +08003282 printk("... CLASSHASH_SIZE: %lu\n", CLASSHASH_SIZE);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003283 printk("... MAX_LOCKDEP_ENTRIES: %lu\n", MAX_LOCKDEP_ENTRIES);
3284 printk("... MAX_LOCKDEP_CHAINS: %lu\n", MAX_LOCKDEP_CHAINS);
3285 printk("... CHAINHASH_SIZE: %lu\n", CHAINHASH_SIZE);
3286
3287 printk(" memory used by lock dependency info: %lu kB\n",
3288 (sizeof(struct lock_class) * MAX_LOCKDEP_KEYS +
3289 sizeof(struct list_head) * CLASSHASH_SIZE +
3290 sizeof(struct lock_list) * MAX_LOCKDEP_ENTRIES +
3291 sizeof(struct lock_chain) * MAX_LOCKDEP_CHAINS +
3292 sizeof(struct list_head) * CHAINHASH_SIZE) / 1024);
3293
3294 printk(" per task-struct memory footprint: %lu bytes\n",
3295 sizeof(struct held_lock) * MAX_LOCK_DEPTH);
3296
3297#ifdef CONFIG_DEBUG_LOCKDEP
Johannes Bergc71063c2007-07-19 01:49:02 -07003298 if (lockdep_init_error) {
3299 printk("WARNING: lockdep init error! Arch code didn't call lockdep_init() early enough?\n");
3300 printk("Call stack leading to lockdep invocation was:\n");
3301 print_stack_trace(&lockdep_init_trace, 0);
3302 }
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003303#endif
3304}
3305
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003306static void
3307print_freed_lock_bug(struct task_struct *curr, const void *mem_from,
Arjan van de Ven55794a42006-07-10 04:44:03 -07003308 const void *mem_to, struct held_lock *hlock)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003309{
3310 if (!debug_locks_off())
3311 return;
3312 if (debug_locks_silent)
3313 return;
3314
3315 printk("\n=========================\n");
3316 printk( "[ BUG: held lock freed! ]\n");
3317 printk( "-------------------------\n");
3318 printk("%s/%d is freeing memory %p-%p, with a lock still held there!\n",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07003319 curr->comm, task_pid_nr(curr), mem_from, mem_to-1);
Arjan van de Ven55794a42006-07-10 04:44:03 -07003320 print_lock(hlock);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003321 lockdep_print_held_locks(curr);
3322
3323 printk("\nstack backtrace:\n");
3324 dump_stack();
3325}
3326
Oleg Nesterov54561782007-12-05 15:46:09 +01003327static inline int not_in_range(const void* mem_from, unsigned long mem_len,
3328 const void* lock_from, unsigned long lock_len)
3329{
3330 return lock_from + lock_len <= mem_from ||
3331 mem_from + mem_len <= lock_from;
3332}
3333
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003334/*
3335 * Called when kernel memory is freed (or unmapped), or if a lock
3336 * is destroyed or reinitialized - this code checks whether there is
3337 * any held lock in the memory range of <from> to <to>:
3338 */
3339void debug_check_no_locks_freed(const void *mem_from, unsigned long mem_len)
3340{
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003341 struct task_struct *curr = current;
3342 struct held_lock *hlock;
3343 unsigned long flags;
3344 int i;
3345
3346 if (unlikely(!debug_locks))
3347 return;
3348
3349 local_irq_save(flags);
3350 for (i = 0; i < curr->lockdep_depth; i++) {
3351 hlock = curr->held_locks + i;
3352
Oleg Nesterov54561782007-12-05 15:46:09 +01003353 if (not_in_range(mem_from, mem_len, hlock->instance,
3354 sizeof(*hlock->instance)))
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003355 continue;
3356
Oleg Nesterov54561782007-12-05 15:46:09 +01003357 print_freed_lock_bug(curr, mem_from, mem_from + mem_len, hlock);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003358 break;
3359 }
3360 local_irq_restore(flags);
3361}
Peter Zijlstraed075362006-12-06 20:35:24 -08003362EXPORT_SYMBOL_GPL(debug_check_no_locks_freed);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003363
3364static void print_held_locks_bug(struct task_struct *curr)
3365{
3366 if (!debug_locks_off())
3367 return;
3368 if (debug_locks_silent)
3369 return;
3370
3371 printk("\n=====================================\n");
3372 printk( "[ BUG: lock held at task exit time! ]\n");
3373 printk( "-------------------------------------\n");
3374 printk("%s/%d is exiting with locks still held!\n",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07003375 curr->comm, task_pid_nr(curr));
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003376 lockdep_print_held_locks(curr);
3377
3378 printk("\nstack backtrace:\n");
3379 dump_stack();
3380}
3381
3382void debug_check_no_locks_held(struct task_struct *task)
3383{
3384 if (unlikely(task->lockdep_depth > 0))
3385 print_held_locks_bug(task);
3386}
3387
3388void debug_show_all_locks(void)
3389{
3390 struct task_struct *g, *p;
3391 int count = 10;
3392 int unlock = 1;
3393
Jarek Poplawski9c35dd72007-03-22 00:11:28 -08003394 if (unlikely(!debug_locks)) {
3395 printk("INFO: lockdep is turned off.\n");
3396 return;
3397 }
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003398 printk("\nShowing all locks held in the system:\n");
3399
3400 /*
3401 * Here we try to get the tasklist_lock as hard as possible,
3402 * if not successful after 2 seconds we ignore it (but keep
3403 * trying). This is to enable a debug printout even if a
3404 * tasklist_lock-holding task deadlocks or crashes.
3405 */
3406retry:
3407 if (!read_trylock(&tasklist_lock)) {
3408 if (count == 10)
3409 printk("hm, tasklist_lock locked, retrying... ");
3410 if (count) {
3411 count--;
3412 printk(" #%d", 10-count);
3413 mdelay(200);
3414 goto retry;
3415 }
3416 printk(" ignoring it.\n");
3417 unlock = 0;
qinghuang feng46fec7a2008-10-28 17:24:28 +08003418 } else {
3419 if (count != 10)
3420 printk(KERN_CONT " locked it.\n");
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003421 }
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003422
3423 do_each_thread(g, p) {
Ingo Molnar85684872007-12-05 15:46:09 +01003424 /*
3425 * It's not reliable to print a task's held locks
3426 * if it's not sleeping (or if it's not the current
3427 * task):
3428 */
3429 if (p->state == TASK_RUNNING && p != current)
3430 continue;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003431 if (p->lockdep_depth)
3432 lockdep_print_held_locks(p);
3433 if (!unlock)
3434 if (read_trylock(&tasklist_lock))
3435 unlock = 1;
3436 } while_each_thread(g, p);
3437
3438 printk("\n");
3439 printk("=============================================\n\n");
3440
3441 if (unlock)
3442 read_unlock(&tasklist_lock);
3443}
3444
3445EXPORT_SYMBOL_GPL(debug_show_all_locks);
3446
Ingo Molnar82a1fcb2008-01-25 21:08:02 +01003447/*
3448 * Careful: only use this function if you are sure that
3449 * the task cannot run in parallel!
3450 */
3451void __debug_show_held_locks(struct task_struct *task)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003452{
Jarek Poplawski9c35dd72007-03-22 00:11:28 -08003453 if (unlikely(!debug_locks)) {
3454 printk("INFO: lockdep is turned off.\n");
3455 return;
3456 }
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003457 lockdep_print_held_locks(task);
3458}
Ingo Molnar82a1fcb2008-01-25 21:08:02 +01003459EXPORT_SYMBOL_GPL(__debug_show_held_locks);
3460
3461void debug_show_held_locks(struct task_struct *task)
3462{
3463 __debug_show_held_locks(task);
3464}
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003465
3466EXPORT_SYMBOL_GPL(debug_show_held_locks);
Peter Zijlstrab351d162007-10-11 22:11:12 +02003467
3468void lockdep_sys_exit(void)
3469{
3470 struct task_struct *curr = current;
3471
3472 if (unlikely(curr->lockdep_depth)) {
3473 if (!debug_locks_off())
3474 return;
3475 printk("\n================================================\n");
3476 printk( "[ BUG: lock held when returning to user space! ]\n");
3477 printk( "------------------------------------------------\n");
3478 printk("%s/%d is leaving the kernel with locks still held!\n",
3479 curr->comm, curr->pid);
3480 lockdep_print_held_locks(curr);
3481 }
3482}