blob: 234a9dccb4befe1bb8cc897784b4a0216faca84b [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
Peter Zijlstrac7e78cf2008-10-16 23:17:09 +0200139static int lock_point(unsigned long points[], unsigned long ip)
Peter Zijlstraf20786f2007-07-19 01:48:56 -0700140{
141 int i;
142
Peter Zijlstrac7e78cf2008-10-16 23:17:09 +0200143 for (i = 0; i < LOCKSTAT_POINTS; i++) {
144 if (points[i] == 0) {
145 points[i] = ip;
Peter Zijlstraf20786f2007-07-19 01:48:56 -0700146 break;
147 }
Peter Zijlstrac7e78cf2008-10-16 23:17:09 +0200148 if (points[i] == ip)
Peter Zijlstraf20786f2007-07-19 01:48:56 -0700149 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
Peter Zijlstrac7e78cf2008-10-16 23:17:09 +0200188 for (i = 0; i < ARRAY_SIZE(stats.contending_point); i++)
189 stats.contending_point[i] += pcs->contending_point[i];
190
Peter Zijlstrac46261d2007-07-19 01:48:57 -0700191 lock_time_add(&pcs->read_waittime, &stats.read_waittime);
192 lock_time_add(&pcs->write_waittime, &stats.write_waittime);
193
194 lock_time_add(&pcs->read_holdtime, &stats.read_holdtime);
195 lock_time_add(&pcs->write_holdtime, &stats.write_holdtime);
Peter Zijlstra96645672007-07-19 01:49:00 -0700196
197 for (i = 0; i < ARRAY_SIZE(stats.bounces); i++)
198 stats.bounces[i] += pcs->bounces[i];
Peter Zijlstrac46261d2007-07-19 01:48:57 -0700199 }
200
201 return stats;
202}
203
204void clear_lock_stats(struct lock_class *class)
205{
206 int cpu;
207
208 for_each_possible_cpu(cpu) {
209 struct lock_class_stats *cpu_stats =
210 &per_cpu(lock_stats, cpu)[class - lock_classes];
211
212 memset(cpu_stats, 0, sizeof(struct lock_class_stats));
213 }
214 memset(class->contention_point, 0, sizeof(class->contention_point));
Peter Zijlstrac7e78cf2008-10-16 23:17:09 +0200215 memset(class->contending_point, 0, sizeof(class->contending_point));
Peter Zijlstrac46261d2007-07-19 01:48:57 -0700216}
217
Peter Zijlstraf20786f2007-07-19 01:48:56 -0700218static struct lock_class_stats *get_lock_stats(struct lock_class *class)
219{
220 return &get_cpu_var(lock_stats)[class - lock_classes];
221}
222
223static void put_lock_stats(struct lock_class_stats *stats)
224{
225 put_cpu_var(lock_stats);
226}
227
228static void lock_release_holdtime(struct held_lock *hlock)
229{
230 struct lock_class_stats *stats;
231 s64 holdtime;
232
233 if (!lock_stat)
234 return;
235
236 holdtime = sched_clock() - hlock->holdtime_stamp;
237
Dave Jonesf82b2172008-08-11 09:30:23 +0200238 stats = get_lock_stats(hlock_class(hlock));
Peter Zijlstraf20786f2007-07-19 01:48:56 -0700239 if (hlock->read)
240 lock_time_inc(&stats->read_holdtime, holdtime);
241 else
242 lock_time_inc(&stats->write_holdtime, holdtime);
243 put_lock_stats(stats);
244}
245#else
246static inline void lock_release_holdtime(struct held_lock *hlock)
247{
248}
249#endif
250
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700251/*
252 * We keep a global list of all lock classes. The list only grows,
253 * never shrinks. The list is only accessed with the lockdep
254 * spinlock lock held.
255 */
256LIST_HEAD(all_lock_classes);
257
258/*
259 * The lockdep classes are in a hash-table as well, for fast lookup:
260 */
261#define CLASSHASH_BITS (MAX_LOCKDEP_KEYS_BITS - 1)
262#define CLASSHASH_SIZE (1UL << CLASSHASH_BITS)
Peter Zijlstra4b32d0a2007-07-19 01:48:59 -0700263#define __classhashfn(key) hash_long((unsigned long)key, CLASSHASH_BITS)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700264#define classhashentry(key) (classhash_table + __classhashfn((key)))
265
266static struct list_head classhash_table[CLASSHASH_SIZE];
267
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700268/*
269 * We put the lock dependency chains into a hash-table as well, to cache
270 * their existence:
271 */
272#define CHAINHASH_BITS (MAX_LOCKDEP_CHAINS_BITS-1)
273#define CHAINHASH_SIZE (1UL << CHAINHASH_BITS)
Peter Zijlstra4b32d0a2007-07-19 01:48:59 -0700274#define __chainhashfn(chain) hash_long(chain, CHAINHASH_BITS)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700275#define chainhashentry(chain) (chainhash_table + __chainhashfn((chain)))
276
277static struct list_head chainhash_table[CHAINHASH_SIZE];
278
279/*
280 * The hash key of the lock dependency chains is a hash itself too:
281 * it's a hash of all locks taken up to that lock, including that lock.
282 * It's a 64-bit hash, because it's important for the keys to be
283 * unique.
284 */
285#define iterate_chain_key(key1, key2) \
Ingo Molnar03cbc352006-09-29 02:01:46 -0700286 (((key1) << MAX_LOCKDEP_KEYS_BITS) ^ \
287 ((key1) >> (64-MAX_LOCKDEP_KEYS_BITS)) ^ \
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700288 (key2))
289
Steven Rostedt1d09daa2008-05-12 21:20:55 +0200290void lockdep_off(void)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700291{
292 current->lockdep_recursion++;
293}
294
295EXPORT_SYMBOL(lockdep_off);
296
Steven Rostedt1d09daa2008-05-12 21:20:55 +0200297void lockdep_on(void)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700298{
299 current->lockdep_recursion--;
300}
301
302EXPORT_SYMBOL(lockdep_on);
303
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700304/*
305 * Debugging switches:
306 */
307
308#define VERBOSE 0
Ingo Molnar33e94e92006-12-13 00:34:41 -0800309#define VERY_VERBOSE 0
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700310
311#if VERBOSE
312# define HARDIRQ_VERBOSE 1
313# define SOFTIRQ_VERBOSE 1
314#else
315# define HARDIRQ_VERBOSE 0
316# define SOFTIRQ_VERBOSE 0
317#endif
318
319#if VERBOSE || HARDIRQ_VERBOSE || SOFTIRQ_VERBOSE
320/*
321 * Quick filtering for interesting events:
322 */
323static int class_filter(struct lock_class *class)
324{
Andi Kleenf9829cc2006-07-10 04:44:01 -0700325#if 0
326 /* Example */
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700327 if (class->name_version == 1 &&
Andi Kleenf9829cc2006-07-10 04:44:01 -0700328 !strcmp(class->name, "lockname"))
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700329 return 1;
330 if (class->name_version == 1 &&
Andi Kleenf9829cc2006-07-10 04:44:01 -0700331 !strcmp(class->name, "&struct->lockfield"))
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700332 return 1;
Andi Kleenf9829cc2006-07-10 04:44:01 -0700333#endif
Ingo Molnara6640892006-12-13 00:34:39 -0800334 /* Filter everything else. 1 would be to allow everything else */
335 return 0;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700336}
337#endif
338
339static int verbose(struct lock_class *class)
340{
341#if VERBOSE
342 return class_filter(class);
343#endif
344 return 0;
345}
346
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700347/*
348 * Stack-trace: tightly packed array of stack backtrace
Ingo Molnar74c383f2006-12-13 00:34:43 -0800349 * addresses. Protected by the graph_lock.
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700350 */
351unsigned long nr_stack_trace_entries;
352static unsigned long stack_trace[MAX_STACK_TRACE_ENTRIES];
353
354static int save_trace(struct stack_trace *trace)
355{
356 trace->nr_entries = 0;
357 trace->max_entries = MAX_STACK_TRACE_ENTRIES - nr_stack_trace_entries;
358 trace->entries = stack_trace + nr_stack_trace_entries;
359
Andi Kleen5a1b3992006-09-26 10:52:34 +0200360 trace->skip = 3;
Andi Kleen5a1b3992006-09-26 10:52:34 +0200361
Christoph Hellwigab1b6f02007-05-08 00:23:29 -0700362 save_stack_trace(trace);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700363
364 trace->max_entries = trace->nr_entries;
365
366 nr_stack_trace_entries += trace->nr_entries;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700367
368 if (nr_stack_trace_entries == MAX_STACK_TRACE_ENTRIES) {
Ingo Molnar74c383f2006-12-13 00:34:43 -0800369 if (!debug_locks_off_graph_unlock())
370 return 0;
371
372 printk("BUG: MAX_STACK_TRACE_ENTRIES too low!\n");
373 printk("turning off the locking correctness validator.\n");
374 dump_stack();
375
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700376 return 0;
377 }
378
379 return 1;
380}
381
382unsigned int nr_hardirq_chains;
383unsigned int nr_softirq_chains;
384unsigned int nr_process_chains;
385unsigned int max_lockdep_depth;
386unsigned int max_recursion_depth;
387
David Miller419ca3f2008-07-29 21:45:03 -0700388static unsigned int lockdep_dependency_gen_id;
389
390static bool lockdep_dependency_visit(struct lock_class *source,
391 unsigned int depth)
392{
393 if (!depth)
394 lockdep_dependency_gen_id++;
395 if (source->dep_gen_id == lockdep_dependency_gen_id)
396 return true;
397 source->dep_gen_id = lockdep_dependency_gen_id;
398 return false;
399}
400
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700401#ifdef CONFIG_DEBUG_LOCKDEP
402/*
403 * We cannot printk in early bootup code. Not even early_printk()
404 * might work. So we mark any initialization errors and printk
405 * about it later on, in lockdep_info().
406 */
407static int lockdep_init_error;
Johannes Bergc71063c2007-07-19 01:49:02 -0700408static unsigned long lockdep_init_trace_data[20];
409static struct stack_trace lockdep_init_trace = {
410 .max_entries = ARRAY_SIZE(lockdep_init_trace_data),
411 .entries = lockdep_init_trace_data,
412};
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700413
414/*
415 * Various lockdep statistics:
416 */
417atomic_t chain_lookup_hits;
418atomic_t chain_lookup_misses;
419atomic_t hardirqs_on_events;
420atomic_t hardirqs_off_events;
421atomic_t redundant_hardirqs_on;
422atomic_t redundant_hardirqs_off;
423atomic_t softirqs_on_events;
424atomic_t softirqs_off_events;
425atomic_t redundant_softirqs_on;
426atomic_t redundant_softirqs_off;
427atomic_t nr_unused_locks;
428atomic_t nr_cyclic_checks;
429atomic_t nr_cyclic_check_recursions;
430atomic_t nr_find_usage_forwards_checks;
431atomic_t nr_find_usage_forwards_recursions;
432atomic_t nr_find_usage_backwards_checks;
433atomic_t nr_find_usage_backwards_recursions;
434# define debug_atomic_inc(ptr) atomic_inc(ptr)
435# define debug_atomic_dec(ptr) atomic_dec(ptr)
436# define debug_atomic_read(ptr) atomic_read(ptr)
437#else
438# define debug_atomic_inc(ptr) do { } while (0)
439# define debug_atomic_dec(ptr) do { } while (0)
440# define debug_atomic_read(ptr) 0
441#endif
442
443/*
444 * Locking printouts:
445 */
446
447static const char *usage_str[] =
448{
449 [LOCK_USED] = "initial-use ",
450 [LOCK_USED_IN_HARDIRQ] = "in-hardirq-W",
451 [LOCK_USED_IN_SOFTIRQ] = "in-softirq-W",
452 [LOCK_ENABLED_SOFTIRQS] = "softirq-on-W",
453 [LOCK_ENABLED_HARDIRQS] = "hardirq-on-W",
454 [LOCK_USED_IN_HARDIRQ_READ] = "in-hardirq-R",
455 [LOCK_USED_IN_SOFTIRQ_READ] = "in-softirq-R",
456 [LOCK_ENABLED_SOFTIRQS_READ] = "softirq-on-R",
457 [LOCK_ENABLED_HARDIRQS_READ] = "hardirq-on-R",
458};
459
460const char * __get_key_name(struct lockdep_subclass_key *key, char *str)
461{
Alexey Dobriyanffb45122007-05-08 00:28:41 -0700462 return kallsyms_lookup((unsigned long)key, NULL, NULL, NULL, str);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700463}
464
465void
466get_usage_chars(struct lock_class *class, char *c1, char *c2, char *c3, char *c4)
467{
468 *c1 = '.', *c2 = '.', *c3 = '.', *c4 = '.';
469
470 if (class->usage_mask & LOCKF_USED_IN_HARDIRQ)
471 *c1 = '+';
472 else
473 if (class->usage_mask & LOCKF_ENABLED_HARDIRQS)
474 *c1 = '-';
475
476 if (class->usage_mask & LOCKF_USED_IN_SOFTIRQ)
477 *c2 = '+';
478 else
479 if (class->usage_mask & LOCKF_ENABLED_SOFTIRQS)
480 *c2 = '-';
481
482 if (class->usage_mask & LOCKF_ENABLED_HARDIRQS_READ)
483 *c3 = '-';
484 if (class->usage_mask & LOCKF_USED_IN_HARDIRQ_READ) {
485 *c3 = '+';
486 if (class->usage_mask & LOCKF_ENABLED_HARDIRQS_READ)
487 *c3 = '?';
488 }
489
490 if (class->usage_mask & LOCKF_ENABLED_SOFTIRQS_READ)
491 *c4 = '-';
492 if (class->usage_mask & LOCKF_USED_IN_SOFTIRQ_READ) {
493 *c4 = '+';
494 if (class->usage_mask & LOCKF_ENABLED_SOFTIRQS_READ)
495 *c4 = '?';
496 }
497}
498
499static void print_lock_name(struct lock_class *class)
500{
Tejun Heo9281ace2007-07-17 04:03:51 -0700501 char str[KSYM_NAME_LEN], c1, c2, c3, c4;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700502 const char *name;
503
504 get_usage_chars(class, &c1, &c2, &c3, &c4);
505
506 name = class->name;
507 if (!name) {
508 name = __get_key_name(class->key, str);
509 printk(" (%s", name);
510 } else {
511 printk(" (%s", name);
512 if (class->name_version > 1)
513 printk("#%d", class->name_version);
514 if (class->subclass)
515 printk("/%d", class->subclass);
516 }
517 printk("){%c%c%c%c}", c1, c2, c3, c4);
518}
519
520static void print_lockdep_cache(struct lockdep_map *lock)
521{
522 const char *name;
Tejun Heo9281ace2007-07-17 04:03:51 -0700523 char str[KSYM_NAME_LEN];
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700524
525 name = lock->name;
526 if (!name)
527 name = __get_key_name(lock->key->subkeys, str);
528
529 printk("%s", name);
530}
531
532static void print_lock(struct held_lock *hlock)
533{
Dave Jonesf82b2172008-08-11 09:30:23 +0200534 print_lock_name(hlock_class(hlock));
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700535 printk(", at: ");
536 print_ip_sym(hlock->acquire_ip);
537}
538
539static void lockdep_print_held_locks(struct task_struct *curr)
540{
541 int i, depth = curr->lockdep_depth;
542
543 if (!depth) {
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -0700544 printk("no locks held by %s/%d.\n", curr->comm, task_pid_nr(curr));
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700545 return;
546 }
547 printk("%d lock%s held by %s/%d:\n",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -0700548 depth, depth > 1 ? "s" : "", curr->comm, task_pid_nr(curr));
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700549
550 for (i = 0; i < depth; i++) {
551 printk(" #%d: ", i);
552 print_lock(curr->held_locks + i);
553 }
554}
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700555
556static void print_lock_class_header(struct lock_class *class, int depth)
557{
558 int bit;
559
Andi Kleenf9829cc2006-07-10 04:44:01 -0700560 printk("%*s->", depth, "");
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700561 print_lock_name(class);
562 printk(" ops: %lu", class->ops);
563 printk(" {\n");
564
565 for (bit = 0; bit < LOCK_USAGE_STATES; bit++) {
566 if (class->usage_mask & (1 << bit)) {
567 int len = depth;
568
Andi Kleenf9829cc2006-07-10 04:44:01 -0700569 len += printk("%*s %s", depth, "", usage_str[bit]);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700570 len += printk(" at:\n");
571 print_stack_trace(class->usage_traces + bit, len);
572 }
573 }
Andi Kleenf9829cc2006-07-10 04:44:01 -0700574 printk("%*s }\n", depth, "");
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700575
Andi Kleenf9829cc2006-07-10 04:44:01 -0700576 printk("%*s ... key at: ",depth,"");
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700577 print_ip_sym((unsigned long)class->key);
578}
579
580/*
581 * printk all lock dependencies starting at <entry>:
582 */
583static void print_lock_dependencies(struct lock_class *class, int depth)
584{
585 struct lock_list *entry;
586
David Miller419ca3f2008-07-29 21:45:03 -0700587 if (lockdep_dependency_visit(class, depth))
588 return;
589
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700590 if (DEBUG_LOCKS_WARN_ON(depth >= 20))
591 return;
592
593 print_lock_class_header(class, depth);
594
595 list_for_each_entry(entry, &class->locks_after, entry) {
Jarek Poplawskib23984d2006-12-06 20:36:23 -0800596 if (DEBUG_LOCKS_WARN_ON(!entry->class))
597 return;
598
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700599 print_lock_dependencies(entry->class, depth + 1);
600
Andi Kleenf9829cc2006-07-10 04:44:01 -0700601 printk("%*s ... acquired at:\n",depth,"");
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700602 print_stack_trace(&entry->trace, 2);
603 printk("\n");
604 }
605}
606
Dave Jones99de0552006-09-29 02:00:10 -0700607static void print_kernel_version(void)
608{
Serge E. Hallyn96b644b2006-10-02 02:18:13 -0700609 printk("%s %.*s\n", init_utsname()->release,
610 (int)strcspn(init_utsname()->version, " "),
611 init_utsname()->version);
Dave Jones99de0552006-09-29 02:00:10 -0700612}
613
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700614static int very_verbose(struct lock_class *class)
615{
616#if VERY_VERBOSE
617 return class_filter(class);
618#endif
619 return 0;
620}
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700621
622/*
623 * Is this the address of a static object:
624 */
625static int static_obj(void *obj)
626{
627 unsigned long start = (unsigned long) &_stext,
628 end = (unsigned long) &_end,
629 addr = (unsigned long) obj;
630#ifdef CONFIG_SMP
631 int i;
632#endif
633
634 /*
635 * static variable?
636 */
637 if ((addr >= start) && (addr < end))
638 return 1;
639
640#ifdef CONFIG_SMP
641 /*
642 * percpu var?
643 */
644 for_each_possible_cpu(i) {
645 start = (unsigned long) &__per_cpu_start + per_cpu_offset(i);
Ingo Molnar1ff56832006-11-17 19:57:22 +0100646 end = (unsigned long) &__per_cpu_start + PERCPU_ENOUGH_ROOM
647 + per_cpu_offset(i);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700648
649 if ((addr >= start) && (addr < end))
650 return 1;
651 }
652#endif
653
654 /*
655 * module var?
656 */
657 return is_module_address(addr);
658}
659
660/*
661 * To make lock name printouts unique, we calculate a unique
662 * class->name_version generation counter:
663 */
664static int count_matching_names(struct lock_class *new_class)
665{
666 struct lock_class *class;
667 int count = 0;
668
669 if (!new_class->name)
670 return 0;
671
672 list_for_each_entry(class, &all_lock_classes, lock_entry) {
673 if (new_class->key - new_class->subclass == class->key)
674 return class->name_version;
675 if (class->name && !strcmp(class->name, new_class->name))
676 count = max(count, class->name_version);
677 }
678
679 return count + 1;
680}
681
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700682/*
683 * Register a lock's class in the hash-table, if the class is not present
684 * yet. Otherwise we look it up. We cache the result in the lock object
685 * itself, so actual lookup of the hash should be once per lock object.
686 */
687static inline struct lock_class *
Ingo Molnard6d897c2006-07-10 04:44:04 -0700688look_up_lock_class(struct lockdep_map *lock, unsigned int subclass)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700689{
690 struct lockdep_subclass_key *key;
691 struct list_head *hash_head;
692 struct lock_class *class;
693
694#ifdef CONFIG_DEBUG_LOCKDEP
695 /*
696 * If the architecture calls into lockdep before initializing
697 * the hashes then we'll warn about it later. (we cannot printk
698 * right now)
699 */
700 if (unlikely(!lockdep_initialized)) {
701 lockdep_init();
702 lockdep_init_error = 1;
Johannes Bergc71063c2007-07-19 01:49:02 -0700703 save_stack_trace(&lockdep_init_trace);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700704 }
705#endif
706
707 /*
708 * Static locks do not have their class-keys yet - for them the key
709 * is the lock object itself:
710 */
711 if (unlikely(!lock->key))
712 lock->key = (void *)lock;
713
714 /*
715 * NOTE: the class-key must be unique. For dynamic locks, a static
716 * lock_class_key variable is passed in through the mutex_init()
717 * (or spin_lock_init()) call - which acts as the key. For static
718 * locks we use the lock object itself as the key.
719 */
Peter Zijlstra4b32d0a2007-07-19 01:48:59 -0700720 BUILD_BUG_ON(sizeof(struct lock_class_key) >
721 sizeof(struct lockdep_map));
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700722
723 key = lock->key->subkeys + subclass;
724
725 hash_head = classhashentry(key);
726
727 /*
728 * We can walk the hash lockfree, because the hash only
729 * grows, and we are careful when adding entries to the end:
730 */
Peter Zijlstra4b32d0a2007-07-19 01:48:59 -0700731 list_for_each_entry(class, hash_head, hash_entry) {
732 if (class->key == key) {
733 WARN_ON_ONCE(class->name != lock->name);
Ingo Molnard6d897c2006-07-10 04:44:04 -0700734 return class;
Peter Zijlstra4b32d0a2007-07-19 01:48:59 -0700735 }
736 }
Ingo Molnard6d897c2006-07-10 04:44:04 -0700737
738 return NULL;
739}
740
741/*
742 * Register a lock's class in the hash-table, if the class is not present
743 * yet. Otherwise we look it up. We cache the result in the lock object
744 * itself, so actual lookup of the hash should be once per lock object.
745 */
746static inline struct lock_class *
Peter Zijlstra4dfbb9d2006-10-11 01:45:14 -0400747register_lock_class(struct lockdep_map *lock, unsigned int subclass, int force)
Ingo Molnard6d897c2006-07-10 04:44:04 -0700748{
749 struct lockdep_subclass_key *key;
750 struct list_head *hash_head;
751 struct lock_class *class;
Ingo Molnar70e45062006-12-06 20:40:50 -0800752 unsigned long flags;
Ingo Molnard6d897c2006-07-10 04:44:04 -0700753
754 class = look_up_lock_class(lock, subclass);
755 if (likely(class))
756 return class;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700757
758 /*
759 * Debug-check: all keys must be persistent!
760 */
761 if (!static_obj(lock->key)) {
762 debug_locks_off();
763 printk("INFO: trying to register non-static key.\n");
764 printk("the code is fine but needs lockdep annotation.\n");
765 printk("turning off the locking correctness validator.\n");
766 dump_stack();
767
768 return NULL;
769 }
770
Ingo Molnard6d897c2006-07-10 04:44:04 -0700771 key = lock->key->subkeys + subclass;
772 hash_head = classhashentry(key);
773
Ingo Molnar70e45062006-12-06 20:40:50 -0800774 raw_local_irq_save(flags);
Ingo Molnar74c383f2006-12-13 00:34:43 -0800775 if (!graph_lock()) {
776 raw_local_irq_restore(flags);
777 return NULL;
778 }
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700779 /*
780 * We have to do the hash-walk again, to avoid races
781 * with another CPU:
782 */
783 list_for_each_entry(class, hash_head, hash_entry)
784 if (class->key == key)
785 goto out_unlock_set;
786 /*
787 * Allocate a new key from the static array, and add it to
788 * the hash:
789 */
790 if (nr_lock_classes >= MAX_LOCKDEP_KEYS) {
Ingo Molnar74c383f2006-12-13 00:34:43 -0800791 if (!debug_locks_off_graph_unlock()) {
792 raw_local_irq_restore(flags);
793 return NULL;
794 }
Ingo Molnar70e45062006-12-06 20:40:50 -0800795 raw_local_irq_restore(flags);
Ingo Molnar74c383f2006-12-13 00:34:43 -0800796
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700797 printk("BUG: MAX_LOCKDEP_KEYS too low!\n");
798 printk("turning off the locking correctness validator.\n");
799 return NULL;
800 }
801 class = lock_classes + nr_lock_classes++;
802 debug_atomic_inc(&nr_unused_locks);
803 class->key = key;
804 class->name = lock->name;
805 class->subclass = subclass;
806 INIT_LIST_HEAD(&class->lock_entry);
807 INIT_LIST_HEAD(&class->locks_before);
808 INIT_LIST_HEAD(&class->locks_after);
809 class->name_version = count_matching_names(class);
810 /*
811 * We use RCU's safe list-add method to make
812 * parallel walking of the hash-list safe:
813 */
814 list_add_tail_rcu(&class->hash_entry, hash_head);
Dale Farnsworth14811972008-02-25 23:03:02 +0100815 /*
816 * Add it to the global list of classes:
817 */
818 list_add_tail_rcu(&class->lock_entry, &all_lock_classes);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700819
820 if (verbose(class)) {
Ingo Molnar74c383f2006-12-13 00:34:43 -0800821 graph_unlock();
Ingo Molnar70e45062006-12-06 20:40:50 -0800822 raw_local_irq_restore(flags);
Ingo Molnar74c383f2006-12-13 00:34:43 -0800823
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700824 printk("\nnew class %p: %s", class->key, class->name);
825 if (class->name_version > 1)
826 printk("#%d", class->name_version);
827 printk("\n");
828 dump_stack();
Ingo Molnar74c383f2006-12-13 00:34:43 -0800829
Ingo Molnar70e45062006-12-06 20:40:50 -0800830 raw_local_irq_save(flags);
Ingo Molnar74c383f2006-12-13 00:34:43 -0800831 if (!graph_lock()) {
832 raw_local_irq_restore(flags);
833 return NULL;
834 }
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700835 }
836out_unlock_set:
Ingo Molnar74c383f2006-12-13 00:34:43 -0800837 graph_unlock();
Ingo Molnar70e45062006-12-06 20:40:50 -0800838 raw_local_irq_restore(flags);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700839
Peter Zijlstra4dfbb9d2006-10-11 01:45:14 -0400840 if (!subclass || force)
Ingo Molnard6d897c2006-07-10 04:44:04 -0700841 lock->class_cache = class;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700842
Jarek Poplawski381a2292007-02-10 01:44:58 -0800843 if (DEBUG_LOCKS_WARN_ON(class->subclass != subclass))
844 return NULL;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700845
846 return class;
847}
848
Peter Zijlstraca58abc2007-07-19 01:48:53 -0700849#ifdef CONFIG_PROVE_LOCKING
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700850/*
Peter Zijlstra8e182572007-07-19 01:48:54 -0700851 * Allocate a lockdep entry. (assumes the graph_lock held, returns
852 * with NULL on failure)
853 */
854static struct lock_list *alloc_list_entry(void)
855{
856 if (nr_list_entries >= MAX_LOCKDEP_ENTRIES) {
857 if (!debug_locks_off_graph_unlock())
858 return NULL;
859
860 printk("BUG: MAX_LOCKDEP_ENTRIES too low!\n");
861 printk("turning off the locking correctness validator.\n");
862 return NULL;
863 }
864 return list_entries + nr_list_entries++;
865}
866
867/*
868 * Add a new dependency to the head of the list:
869 */
870static int add_lock_to_list(struct lock_class *class, struct lock_class *this,
871 struct list_head *head, unsigned long ip, int distance)
872{
873 struct lock_list *entry;
874 /*
875 * Lock not present yet - get a new dependency struct and
876 * add it to the list:
877 */
878 entry = alloc_list_entry();
879 if (!entry)
880 return 0;
881
Peter Zijlstra8e182572007-07-19 01:48:54 -0700882 if (!save_trace(&entry->trace))
883 return 0;
884
Zhu Yi74870172008-08-27 14:33:00 +0800885 entry->class = this;
886 entry->distance = distance;
Peter Zijlstra8e182572007-07-19 01:48:54 -0700887 /*
888 * Since we never remove from the dependency list, the list can
889 * be walked lockless by other CPUs, it's only allocation
890 * that must be protected by the spinlock. But this also means
891 * we must make new entries visible only once writes to the
892 * entry become visible - hence the RCU op:
893 */
894 list_add_tail_rcu(&entry->entry, head);
895
896 return 1;
897}
898
899/*
900 * Recursive, forwards-direction lock-dependency checking, used for
901 * both noncyclic checking and for hardirq-unsafe/softirq-unsafe
902 * checking.
903 *
904 * (to keep the stackframe of the recursive functions small we
905 * use these global variables, and we also mark various helper
906 * functions as noinline.)
907 */
908static struct held_lock *check_source, *check_target;
909
910/*
911 * Print a dependency chain entry (this is only done when a deadlock
912 * has been detected):
913 */
914static noinline int
915print_circular_bug_entry(struct lock_list *target, unsigned int depth)
916{
917 if (debug_locks_silent)
918 return 0;
919 printk("\n-> #%u", depth);
920 print_lock_name(target->class);
921 printk(":\n");
922 print_stack_trace(&target->trace, 6);
923
924 return 0;
925}
926
927/*
928 * When a circular dependency is detected, print the
929 * header first:
930 */
931static noinline int
932print_circular_bug_header(struct lock_list *entry, unsigned int depth)
933{
934 struct task_struct *curr = current;
935
936 if (!debug_locks_off_graph_unlock() || debug_locks_silent)
937 return 0;
938
939 printk("\n=======================================================\n");
940 printk( "[ INFO: possible circular locking dependency detected ]\n");
941 print_kernel_version();
942 printk( "-------------------------------------------------------\n");
943 printk("%s/%d is trying to acquire lock:\n",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -0700944 curr->comm, task_pid_nr(curr));
Peter Zijlstra8e182572007-07-19 01:48:54 -0700945 print_lock(check_source);
946 printk("\nbut task is already holding lock:\n");
947 print_lock(check_target);
948 printk("\nwhich lock already depends on the new lock.\n\n");
949 printk("\nthe existing dependency chain (in reverse order) is:\n");
950
951 print_circular_bug_entry(entry, depth);
952
953 return 0;
954}
955
956static noinline int print_circular_bug_tail(void)
957{
958 struct task_struct *curr = current;
959 struct lock_list this;
960
961 if (debug_locks_silent)
962 return 0;
963
Dave Jonesf82b2172008-08-11 09:30:23 +0200964 this.class = hlock_class(check_source);
Peter Zijlstra8e182572007-07-19 01:48:54 -0700965 if (!save_trace(&this.trace))
966 return 0;
967
968 print_circular_bug_entry(&this, 0);
969
970 printk("\nother info that might help us debug this:\n\n");
971 lockdep_print_held_locks(curr);
972
973 printk("\nstack backtrace:\n");
974 dump_stack();
975
976 return 0;
977}
978
979#define RECURSION_LIMIT 40
980
981static int noinline print_infinite_recursion_bug(void)
982{
983 if (!debug_locks_off_graph_unlock())
984 return 0;
985
986 WARN_ON(1);
987
988 return 0;
989}
990
David Miller419ca3f2008-07-29 21:45:03 -0700991unsigned long __lockdep_count_forward_deps(struct lock_class *class,
992 unsigned int depth)
993{
994 struct lock_list *entry;
995 unsigned long ret = 1;
996
997 if (lockdep_dependency_visit(class, depth))
998 return 0;
999
1000 /*
1001 * Recurse this class's dependency list:
1002 */
1003 list_for_each_entry(entry, &class->locks_after, entry)
1004 ret += __lockdep_count_forward_deps(entry->class, depth + 1);
1005
1006 return ret;
1007}
1008
1009unsigned long lockdep_count_forward_deps(struct lock_class *class)
1010{
1011 unsigned long ret, flags;
1012
1013 local_irq_save(flags);
1014 __raw_spin_lock(&lockdep_lock);
1015 ret = __lockdep_count_forward_deps(class, 0);
1016 __raw_spin_unlock(&lockdep_lock);
1017 local_irq_restore(flags);
1018
1019 return ret;
1020}
1021
1022unsigned long __lockdep_count_backward_deps(struct lock_class *class,
1023 unsigned int depth)
1024{
1025 struct lock_list *entry;
1026 unsigned long ret = 1;
1027
1028 if (lockdep_dependency_visit(class, depth))
1029 return 0;
1030 /*
1031 * Recurse this class's dependency list:
1032 */
1033 list_for_each_entry(entry, &class->locks_before, entry)
1034 ret += __lockdep_count_backward_deps(entry->class, depth + 1);
1035
1036 return ret;
1037}
1038
1039unsigned long lockdep_count_backward_deps(struct lock_class *class)
1040{
1041 unsigned long ret, flags;
1042
1043 local_irq_save(flags);
1044 __raw_spin_lock(&lockdep_lock);
1045 ret = __lockdep_count_backward_deps(class, 0);
1046 __raw_spin_unlock(&lockdep_lock);
1047 local_irq_restore(flags);
1048
1049 return ret;
1050}
1051
Peter Zijlstra8e182572007-07-19 01:48:54 -07001052/*
1053 * Prove that the dependency graph starting at <entry> can not
1054 * lead to <target>. Print an error and return 0 if it does.
1055 */
1056static noinline int
1057check_noncircular(struct lock_class *source, unsigned int depth)
1058{
1059 struct lock_list *entry;
1060
David Miller419ca3f2008-07-29 21:45:03 -07001061 if (lockdep_dependency_visit(source, depth))
1062 return 1;
1063
Peter Zijlstra8e182572007-07-19 01:48:54 -07001064 debug_atomic_inc(&nr_cyclic_check_recursions);
1065 if (depth > max_recursion_depth)
1066 max_recursion_depth = depth;
1067 if (depth >= RECURSION_LIMIT)
1068 return print_infinite_recursion_bug();
1069 /*
1070 * Check this lock's dependency list:
1071 */
1072 list_for_each_entry(entry, &source->locks_after, entry) {
Dave Jonesf82b2172008-08-11 09:30:23 +02001073 if (entry->class == hlock_class(check_target))
Peter Zijlstra8e182572007-07-19 01:48:54 -07001074 return print_circular_bug_header(entry, depth+1);
1075 debug_atomic_inc(&nr_cyclic_checks);
1076 if (!check_noncircular(entry->class, depth+1))
1077 return print_circular_bug_entry(entry, depth+1);
1078 }
1079 return 1;
1080}
1081
Steven Rostedt81d68a92008-05-12 21:20:42 +02001082#if defined(CONFIG_TRACE_IRQFLAGS) && defined(CONFIG_PROVE_LOCKING)
Peter Zijlstra8e182572007-07-19 01:48:54 -07001083/*
1084 * Forwards and backwards subgraph searching, for the purposes of
1085 * proving that two subgraphs can be connected by a new dependency
1086 * without creating any illegal irq-safe -> irq-unsafe lock dependency.
1087 */
1088static enum lock_usage_bit find_usage_bit;
1089static struct lock_class *forwards_match, *backwards_match;
1090
1091/*
1092 * Find a node in the forwards-direction dependency sub-graph starting
1093 * at <source> that matches <find_usage_bit>.
1094 *
1095 * Return 2 if such a node exists in the subgraph, and put that node
1096 * into <forwards_match>.
1097 *
1098 * Return 1 otherwise and keep <forwards_match> unchanged.
1099 * Return 0 on error.
1100 */
1101static noinline int
1102find_usage_forwards(struct lock_class *source, unsigned int depth)
1103{
1104 struct lock_list *entry;
1105 int ret;
1106
David Miller419ca3f2008-07-29 21:45:03 -07001107 if (lockdep_dependency_visit(source, depth))
1108 return 1;
1109
Peter Zijlstra8e182572007-07-19 01:48:54 -07001110 if (depth > max_recursion_depth)
1111 max_recursion_depth = depth;
1112 if (depth >= RECURSION_LIMIT)
1113 return print_infinite_recursion_bug();
1114
1115 debug_atomic_inc(&nr_find_usage_forwards_checks);
1116 if (source->usage_mask & (1 << find_usage_bit)) {
1117 forwards_match = source;
1118 return 2;
1119 }
1120
1121 /*
1122 * Check this lock's dependency list:
1123 */
1124 list_for_each_entry(entry, &source->locks_after, entry) {
1125 debug_atomic_inc(&nr_find_usage_forwards_recursions);
1126 ret = find_usage_forwards(entry->class, depth+1);
1127 if (ret == 2 || ret == 0)
1128 return ret;
1129 }
1130 return 1;
1131}
1132
1133/*
1134 * Find a node in the backwards-direction dependency sub-graph starting
1135 * at <source> that matches <find_usage_bit>.
1136 *
1137 * Return 2 if such a node exists in the subgraph, and put that node
1138 * into <backwards_match>.
1139 *
1140 * Return 1 otherwise and keep <backwards_match> unchanged.
1141 * Return 0 on error.
1142 */
Steven Rostedt1d09daa2008-05-12 21:20:55 +02001143static noinline int
Peter Zijlstra8e182572007-07-19 01:48:54 -07001144find_usage_backwards(struct lock_class *source, unsigned int depth)
1145{
1146 struct lock_list *entry;
1147 int ret;
1148
David Miller419ca3f2008-07-29 21:45:03 -07001149 if (lockdep_dependency_visit(source, depth))
1150 return 1;
1151
Peter Zijlstra8e182572007-07-19 01:48:54 -07001152 if (!__raw_spin_is_locked(&lockdep_lock))
1153 return DEBUG_LOCKS_WARN_ON(1);
1154
1155 if (depth > max_recursion_depth)
1156 max_recursion_depth = depth;
1157 if (depth >= RECURSION_LIMIT)
1158 return print_infinite_recursion_bug();
1159
1160 debug_atomic_inc(&nr_find_usage_backwards_checks);
1161 if (source->usage_mask & (1 << find_usage_bit)) {
1162 backwards_match = source;
1163 return 2;
1164 }
1165
Dave Jonesf82b2172008-08-11 09:30:23 +02001166 if (!source && debug_locks_off_graph_unlock()) {
1167 WARN_ON(1);
1168 return 0;
1169 }
1170
Peter Zijlstra8e182572007-07-19 01:48:54 -07001171 /*
1172 * Check this lock's dependency list:
1173 */
1174 list_for_each_entry(entry, &source->locks_before, entry) {
1175 debug_atomic_inc(&nr_find_usage_backwards_recursions);
1176 ret = find_usage_backwards(entry->class, depth+1);
1177 if (ret == 2 || ret == 0)
1178 return ret;
1179 }
1180 return 1;
1181}
1182
1183static int
1184print_bad_irq_dependency(struct task_struct *curr,
1185 struct held_lock *prev,
1186 struct held_lock *next,
1187 enum lock_usage_bit bit1,
1188 enum lock_usage_bit bit2,
1189 const char *irqclass)
1190{
1191 if (!debug_locks_off_graph_unlock() || debug_locks_silent)
1192 return 0;
1193
1194 printk("\n======================================================\n");
1195 printk( "[ INFO: %s-safe -> %s-unsafe lock order detected ]\n",
1196 irqclass, irqclass);
1197 print_kernel_version();
1198 printk( "------------------------------------------------------\n");
1199 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 -07001200 curr->comm, task_pid_nr(curr),
Peter Zijlstra8e182572007-07-19 01:48:54 -07001201 curr->hardirq_context, hardirq_count() >> HARDIRQ_SHIFT,
1202 curr->softirq_context, softirq_count() >> SOFTIRQ_SHIFT,
1203 curr->hardirqs_enabled,
1204 curr->softirqs_enabled);
1205 print_lock(next);
1206
1207 printk("\nand this task is already holding:\n");
1208 print_lock(prev);
1209 printk("which would create a new lock dependency:\n");
Dave Jonesf82b2172008-08-11 09:30:23 +02001210 print_lock_name(hlock_class(prev));
Peter Zijlstra8e182572007-07-19 01:48:54 -07001211 printk(" ->");
Dave Jonesf82b2172008-08-11 09:30:23 +02001212 print_lock_name(hlock_class(next));
Peter Zijlstra8e182572007-07-19 01:48:54 -07001213 printk("\n");
1214
1215 printk("\nbut this new dependency connects a %s-irq-safe lock:\n",
1216 irqclass);
1217 print_lock_name(backwards_match);
1218 printk("\n... which became %s-irq-safe at:\n", irqclass);
1219
1220 print_stack_trace(backwards_match->usage_traces + bit1, 1);
1221
1222 printk("\nto a %s-irq-unsafe lock:\n", irqclass);
1223 print_lock_name(forwards_match);
1224 printk("\n... which became %s-irq-unsafe at:\n", irqclass);
1225 printk("...");
1226
1227 print_stack_trace(forwards_match->usage_traces + bit2, 1);
1228
1229 printk("\nother info that might help us debug this:\n\n");
1230 lockdep_print_held_locks(curr);
1231
1232 printk("\nthe %s-irq-safe lock's dependencies:\n", irqclass);
1233 print_lock_dependencies(backwards_match, 0);
1234
1235 printk("\nthe %s-irq-unsafe lock's dependencies:\n", irqclass);
1236 print_lock_dependencies(forwards_match, 0);
1237
1238 printk("\nstack backtrace:\n");
1239 dump_stack();
1240
1241 return 0;
1242}
1243
1244static int
1245check_usage(struct task_struct *curr, struct held_lock *prev,
1246 struct held_lock *next, enum lock_usage_bit bit_backwards,
1247 enum lock_usage_bit bit_forwards, const char *irqclass)
1248{
1249 int ret;
1250
1251 find_usage_bit = bit_backwards;
1252 /* fills in <backwards_match> */
Dave Jonesf82b2172008-08-11 09:30:23 +02001253 ret = find_usage_backwards(hlock_class(prev), 0);
Peter Zijlstra8e182572007-07-19 01:48:54 -07001254 if (!ret || ret == 1)
1255 return ret;
1256
1257 find_usage_bit = bit_forwards;
Dave Jonesf82b2172008-08-11 09:30:23 +02001258 ret = find_usage_forwards(hlock_class(next), 0);
Peter Zijlstra8e182572007-07-19 01:48:54 -07001259 if (!ret || ret == 1)
1260 return ret;
1261 /* ret == 2 */
1262 return print_bad_irq_dependency(curr, prev, next,
1263 bit_backwards, bit_forwards, irqclass);
1264}
1265
1266static int
1267check_prev_add_irq(struct task_struct *curr, struct held_lock *prev,
1268 struct held_lock *next)
1269{
1270 /*
1271 * Prove that the new dependency does not connect a hardirq-safe
1272 * lock with a hardirq-unsafe lock - to achieve this we search
1273 * the backwards-subgraph starting at <prev>, and the
1274 * forwards-subgraph starting at <next>:
1275 */
1276 if (!check_usage(curr, prev, next, LOCK_USED_IN_HARDIRQ,
1277 LOCK_ENABLED_HARDIRQS, "hard"))
1278 return 0;
1279
1280 /*
1281 * Prove that the new dependency does not connect a hardirq-safe-read
1282 * lock with a hardirq-unsafe lock - to achieve this we search
1283 * the backwards-subgraph starting at <prev>, and the
1284 * forwards-subgraph starting at <next>:
1285 */
1286 if (!check_usage(curr, prev, next, LOCK_USED_IN_HARDIRQ_READ,
1287 LOCK_ENABLED_HARDIRQS, "hard-read"))
1288 return 0;
1289
1290 /*
1291 * Prove that the new dependency does not connect a softirq-safe
1292 * lock with a softirq-unsafe lock - to achieve this we search
1293 * the backwards-subgraph starting at <prev>, and the
1294 * forwards-subgraph starting at <next>:
1295 */
1296 if (!check_usage(curr, prev, next, LOCK_USED_IN_SOFTIRQ,
1297 LOCK_ENABLED_SOFTIRQS, "soft"))
1298 return 0;
1299 /*
1300 * Prove that the new dependency does not connect a softirq-safe-read
1301 * lock with a softirq-unsafe lock - to achieve this we search
1302 * the backwards-subgraph starting at <prev>, and the
1303 * forwards-subgraph starting at <next>:
1304 */
1305 if (!check_usage(curr, prev, next, LOCK_USED_IN_SOFTIRQ_READ,
1306 LOCK_ENABLED_SOFTIRQS, "soft"))
1307 return 0;
1308
1309 return 1;
1310}
1311
1312static void inc_chains(void)
1313{
1314 if (current->hardirq_context)
1315 nr_hardirq_chains++;
1316 else {
1317 if (current->softirq_context)
1318 nr_softirq_chains++;
1319 else
1320 nr_process_chains++;
1321 }
1322}
1323
1324#else
1325
1326static inline int
1327check_prev_add_irq(struct task_struct *curr, struct held_lock *prev,
1328 struct held_lock *next)
1329{
1330 return 1;
1331}
1332
1333static inline void inc_chains(void)
1334{
1335 nr_process_chains++;
1336}
1337
1338#endif
1339
1340static int
1341print_deadlock_bug(struct task_struct *curr, struct held_lock *prev,
1342 struct held_lock *next)
1343{
1344 if (!debug_locks_off_graph_unlock() || debug_locks_silent)
1345 return 0;
1346
1347 printk("\n=============================================\n");
1348 printk( "[ INFO: possible recursive locking detected ]\n");
1349 print_kernel_version();
1350 printk( "---------------------------------------------\n");
1351 printk("%s/%d is trying to acquire lock:\n",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07001352 curr->comm, task_pid_nr(curr));
Peter Zijlstra8e182572007-07-19 01:48:54 -07001353 print_lock(next);
1354 printk("\nbut task is already holding lock:\n");
1355 print_lock(prev);
1356
1357 printk("\nother info that might help us debug this:\n");
1358 lockdep_print_held_locks(curr);
1359
1360 printk("\nstack backtrace:\n");
1361 dump_stack();
1362
1363 return 0;
1364}
1365
1366/*
1367 * Check whether we are holding such a class already.
1368 *
1369 * (Note that this has to be done separately, because the graph cannot
1370 * detect such classes of deadlocks.)
1371 *
1372 * Returns: 0 on deadlock detected, 1 on OK, 2 on recursive read
1373 */
1374static int
1375check_deadlock(struct task_struct *curr, struct held_lock *next,
1376 struct lockdep_map *next_instance, int read)
1377{
1378 struct held_lock *prev;
Peter Zijlstra7531e2f2008-08-11 09:30:24 +02001379 struct held_lock *nest = NULL;
Peter Zijlstra8e182572007-07-19 01:48:54 -07001380 int i;
1381
1382 for (i = 0; i < curr->lockdep_depth; i++) {
1383 prev = curr->held_locks + i;
Peter Zijlstra7531e2f2008-08-11 09:30:24 +02001384
1385 if (prev->instance == next->nest_lock)
1386 nest = prev;
1387
Dave Jonesf82b2172008-08-11 09:30:23 +02001388 if (hlock_class(prev) != hlock_class(next))
Peter Zijlstra8e182572007-07-19 01:48:54 -07001389 continue;
Peter Zijlstra7531e2f2008-08-11 09:30:24 +02001390
Peter Zijlstra8e182572007-07-19 01:48:54 -07001391 /*
1392 * Allow read-after-read recursion of the same
1393 * lock class (i.e. read_lock(lock)+read_lock(lock)):
1394 */
1395 if ((read == 2) && prev->read)
1396 return 2;
Peter Zijlstra7531e2f2008-08-11 09:30:24 +02001397
1398 /*
1399 * We're holding the nest_lock, which serializes this lock's
1400 * nesting behaviour.
1401 */
1402 if (nest)
1403 return 2;
1404
Peter Zijlstra8e182572007-07-19 01:48:54 -07001405 return print_deadlock_bug(curr, prev, next);
1406 }
1407 return 1;
1408}
1409
1410/*
1411 * There was a chain-cache miss, and we are about to add a new dependency
1412 * to a previous lock. We recursively validate the following rules:
1413 *
1414 * - would the adding of the <prev> -> <next> dependency create a
1415 * circular dependency in the graph? [== circular deadlock]
1416 *
1417 * - does the new prev->next dependency connect any hardirq-safe lock
1418 * (in the full backwards-subgraph starting at <prev>) with any
1419 * hardirq-unsafe lock (in the full forwards-subgraph starting at
1420 * <next>)? [== illegal lock inversion with hardirq contexts]
1421 *
1422 * - does the new prev->next dependency connect any softirq-safe lock
1423 * (in the full backwards-subgraph starting at <prev>) with any
1424 * softirq-unsafe lock (in the full forwards-subgraph starting at
1425 * <next>)? [== illegal lock inversion with softirq contexts]
1426 *
1427 * any of these scenarios could lead to a deadlock.
1428 *
1429 * Then if all the validations pass, we add the forwards and backwards
1430 * dependency.
1431 */
1432static int
1433check_prev_add(struct task_struct *curr, struct held_lock *prev,
1434 struct held_lock *next, int distance)
1435{
1436 struct lock_list *entry;
1437 int ret;
1438
1439 /*
1440 * Prove that the new <prev> -> <next> dependency would not
1441 * create a circular dependency in the graph. (We do this by
1442 * forward-recursing into the graph starting at <next>, and
1443 * checking whether we can reach <prev>.)
1444 *
1445 * We are using global variables to control the recursion, to
1446 * keep the stackframe size of the recursive functions low:
1447 */
1448 check_source = next;
1449 check_target = prev;
Dave Jonesf82b2172008-08-11 09:30:23 +02001450 if (!(check_noncircular(hlock_class(next), 0)))
Peter Zijlstra8e182572007-07-19 01:48:54 -07001451 return print_circular_bug_tail();
1452
1453 if (!check_prev_add_irq(curr, prev, next))
1454 return 0;
1455
1456 /*
1457 * For recursive read-locks we do all the dependency checks,
1458 * but we dont store read-triggered dependencies (only
1459 * write-triggered dependencies). This ensures that only the
1460 * write-side dependencies matter, and that if for example a
1461 * write-lock never takes any other locks, then the reads are
1462 * equivalent to a NOP.
1463 */
1464 if (next->read == 2 || prev->read == 2)
1465 return 1;
1466 /*
1467 * Is the <prev> -> <next> dependency already present?
1468 *
1469 * (this may occur even though this is a new chain: consider
1470 * e.g. the L1 -> L2 -> L3 -> L4 and the L5 -> L1 -> L2 -> L3
1471 * chains - the second one will be new, but L1 already has
1472 * L2 added to its dependency list, due to the first chain.)
1473 */
Dave Jonesf82b2172008-08-11 09:30:23 +02001474 list_for_each_entry(entry, &hlock_class(prev)->locks_after, entry) {
1475 if (entry->class == hlock_class(next)) {
Peter Zijlstra8e182572007-07-19 01:48:54 -07001476 if (distance == 1)
1477 entry->distance = 1;
1478 return 2;
1479 }
1480 }
1481
1482 /*
1483 * Ok, all validations passed, add the new lock
1484 * to the previous lock's dependency list:
1485 */
Dave Jonesf82b2172008-08-11 09:30:23 +02001486 ret = add_lock_to_list(hlock_class(prev), hlock_class(next),
1487 &hlock_class(prev)->locks_after,
1488 next->acquire_ip, distance);
Peter Zijlstra8e182572007-07-19 01:48:54 -07001489
1490 if (!ret)
1491 return 0;
1492
Dave Jonesf82b2172008-08-11 09:30:23 +02001493 ret = add_lock_to_list(hlock_class(next), hlock_class(prev),
1494 &hlock_class(next)->locks_before,
1495 next->acquire_ip, distance);
Peter Zijlstra8e182572007-07-19 01:48:54 -07001496 if (!ret)
1497 return 0;
1498
1499 /*
1500 * Debugging printouts:
1501 */
Dave Jonesf82b2172008-08-11 09:30:23 +02001502 if (verbose(hlock_class(prev)) || verbose(hlock_class(next))) {
Peter Zijlstra8e182572007-07-19 01:48:54 -07001503 graph_unlock();
1504 printk("\n new dependency: ");
Dave Jonesf82b2172008-08-11 09:30:23 +02001505 print_lock_name(hlock_class(prev));
Peter Zijlstra8e182572007-07-19 01:48:54 -07001506 printk(" => ");
Dave Jonesf82b2172008-08-11 09:30:23 +02001507 print_lock_name(hlock_class(next));
Peter Zijlstra8e182572007-07-19 01:48:54 -07001508 printk("\n");
1509 dump_stack();
1510 return graph_lock();
1511 }
1512 return 1;
1513}
1514
1515/*
1516 * Add the dependency to all directly-previous locks that are 'relevant'.
1517 * The ones that are relevant are (in increasing distance from curr):
1518 * all consecutive trylock entries and the final non-trylock entry - or
1519 * the end of this context's lock-chain - whichever comes first.
1520 */
1521static int
1522check_prevs_add(struct task_struct *curr, struct held_lock *next)
1523{
1524 int depth = curr->lockdep_depth;
1525 struct held_lock *hlock;
1526
1527 /*
1528 * Debugging checks.
1529 *
1530 * Depth must not be zero for a non-head lock:
1531 */
1532 if (!depth)
1533 goto out_bug;
1534 /*
1535 * At least two relevant locks must exist for this
1536 * to be a head:
1537 */
1538 if (curr->held_locks[depth].irq_context !=
1539 curr->held_locks[depth-1].irq_context)
1540 goto out_bug;
1541
1542 for (;;) {
1543 int distance = curr->lockdep_depth - depth + 1;
1544 hlock = curr->held_locks + depth-1;
1545 /*
1546 * Only non-recursive-read entries get new dependencies
1547 * added:
1548 */
1549 if (hlock->read != 2) {
1550 if (!check_prev_add(curr, hlock, next, distance))
1551 return 0;
1552 /*
1553 * Stop after the first non-trylock entry,
1554 * as non-trylock entries have added their
1555 * own direct dependencies already, so this
1556 * lock is connected to them indirectly:
1557 */
1558 if (!hlock->trylock)
1559 break;
1560 }
1561 depth--;
1562 /*
1563 * End of lock-stack?
1564 */
1565 if (!depth)
1566 break;
1567 /*
1568 * Stop the search if we cross into another context:
1569 */
1570 if (curr->held_locks[depth].irq_context !=
1571 curr->held_locks[depth-1].irq_context)
1572 break;
1573 }
1574 return 1;
1575out_bug:
1576 if (!debug_locks_off_graph_unlock())
1577 return 0;
1578
1579 WARN_ON(1);
1580
1581 return 0;
1582}
1583
1584unsigned long nr_lock_chains;
Huang, Ying443cd502008-06-20 16:39:21 +08001585struct lock_chain lock_chains[MAX_LOCKDEP_CHAINS];
Huang, Yingcd1a28e2008-06-23 11:20:54 +08001586int nr_chain_hlocks;
Huang, Ying443cd502008-06-20 16:39:21 +08001587static u16 chain_hlocks[MAX_LOCKDEP_CHAIN_HLOCKS];
1588
1589struct lock_class *lock_chain_get_class(struct lock_chain *chain, int i)
1590{
1591 return lock_classes + chain_hlocks[chain->base + i];
1592}
Peter Zijlstra8e182572007-07-19 01:48:54 -07001593
1594/*
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001595 * Look up a dependency chain. If the key is not present yet then
Jarek Poplawski9e860d02007-05-08 00:30:12 -07001596 * add it and return 1 - in this case the new dependency chain is
1597 * validated. If the key is already hashed, return 0.
1598 * (On return with 1 graph_lock is held.)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001599 */
Huang, Ying443cd502008-06-20 16:39:21 +08001600static inline int lookup_chain_cache(struct task_struct *curr,
1601 struct held_lock *hlock,
1602 u64 chain_key)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001603{
Dave Jonesf82b2172008-08-11 09:30:23 +02001604 struct lock_class *class = hlock_class(hlock);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001605 struct list_head *hash_head = chainhashentry(chain_key);
1606 struct lock_chain *chain;
Huang, Ying443cd502008-06-20 16:39:21 +08001607 struct held_lock *hlock_curr, *hlock_next;
Huang, Yingcd1a28e2008-06-23 11:20:54 +08001608 int i, j, n, cn;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001609
Jarek Poplawski381a2292007-02-10 01:44:58 -08001610 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
1611 return 0;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001612 /*
1613 * We can walk it lock-free, because entries only get added
1614 * to the hash:
1615 */
1616 list_for_each_entry(chain, hash_head, entry) {
1617 if (chain->chain_key == chain_key) {
1618cache_hit:
1619 debug_atomic_inc(&chain_lookup_hits);
Ingo Molnar81fc6852006-12-13 00:34:40 -08001620 if (very_verbose(class))
Andrew Morton755cd902006-12-29 16:49:14 -08001621 printk("\nhash chain already cached, key: "
1622 "%016Lx tail class: [%p] %s\n",
1623 (unsigned long long)chain_key,
1624 class->key, class->name);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001625 return 0;
1626 }
1627 }
Ingo Molnar81fc6852006-12-13 00:34:40 -08001628 if (very_verbose(class))
Andrew Morton755cd902006-12-29 16:49:14 -08001629 printk("\nnew hash chain, key: %016Lx tail class: [%p] %s\n",
1630 (unsigned long long)chain_key, class->key, class->name);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001631 /*
1632 * Allocate a new chain entry from the static array, and add
1633 * it to the hash:
1634 */
Ingo Molnar74c383f2006-12-13 00:34:43 -08001635 if (!graph_lock())
1636 return 0;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001637 /*
1638 * We have to walk the chain again locked - to avoid duplicates:
1639 */
1640 list_for_each_entry(chain, hash_head, entry) {
1641 if (chain->chain_key == chain_key) {
Ingo Molnar74c383f2006-12-13 00:34:43 -08001642 graph_unlock();
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001643 goto cache_hit;
1644 }
1645 }
1646 if (unlikely(nr_lock_chains >= MAX_LOCKDEP_CHAINS)) {
Ingo Molnar74c383f2006-12-13 00:34:43 -08001647 if (!debug_locks_off_graph_unlock())
1648 return 0;
1649
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001650 printk("BUG: MAX_LOCKDEP_CHAINS too low!\n");
1651 printk("turning off the locking correctness validator.\n");
1652 return 0;
1653 }
1654 chain = lock_chains + nr_lock_chains++;
1655 chain->chain_key = chain_key;
Huang, Ying443cd502008-06-20 16:39:21 +08001656 chain->irq_context = hlock->irq_context;
1657 /* Find the first held_lock of current chain */
1658 hlock_next = hlock;
1659 for (i = curr->lockdep_depth - 1; i >= 0; i--) {
1660 hlock_curr = curr->held_locks + i;
1661 if (hlock_curr->irq_context != hlock_next->irq_context)
1662 break;
1663 hlock_next = hlock;
1664 }
1665 i++;
1666 chain->depth = curr->lockdep_depth + 1 - i;
Huang, Yingcd1a28e2008-06-23 11:20:54 +08001667 cn = nr_chain_hlocks;
1668 while (cn + chain->depth <= MAX_LOCKDEP_CHAIN_HLOCKS) {
1669 n = cmpxchg(&nr_chain_hlocks, cn, cn + chain->depth);
1670 if (n == cn)
1671 break;
1672 cn = n;
1673 }
1674 if (likely(cn + chain->depth <= MAX_LOCKDEP_CHAIN_HLOCKS)) {
1675 chain->base = cn;
Huang, Ying443cd502008-06-20 16:39:21 +08001676 for (j = 0; j < chain->depth - 1; j++, i++) {
Dave Jonesf82b2172008-08-11 09:30:23 +02001677 int lock_id = curr->held_locks[i].class_idx - 1;
Huang, Ying443cd502008-06-20 16:39:21 +08001678 chain_hlocks[chain->base + j] = lock_id;
1679 }
1680 chain_hlocks[chain->base + j] = class - lock_classes;
1681 }
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001682 list_add_tail_rcu(&chain->entry, hash_head);
1683 debug_atomic_inc(&chain_lookup_misses);
Peter Zijlstra8e182572007-07-19 01:48:54 -07001684 inc_chains();
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001685
1686 return 1;
1687}
Peter Zijlstra8e182572007-07-19 01:48:54 -07001688
1689static int validate_chain(struct task_struct *curr, struct lockdep_map *lock,
Johannes Berg4e6045f2007-10-18 23:39:55 -07001690 struct held_lock *hlock, int chain_head, u64 chain_key)
Peter Zijlstra8e182572007-07-19 01:48:54 -07001691{
1692 /*
1693 * Trylock needs to maintain the stack of held locks, but it
1694 * does not add new dependencies, because trylock can be done
1695 * in any order.
1696 *
1697 * We look up the chain_key and do the O(N^2) check and update of
1698 * the dependencies only if this is a new dependency chain.
1699 * (If lookup_chain_cache() returns with 1 it acquires
1700 * graph_lock for us)
1701 */
1702 if (!hlock->trylock && (hlock->check == 2) &&
Huang, Ying443cd502008-06-20 16:39:21 +08001703 lookup_chain_cache(curr, hlock, chain_key)) {
Peter Zijlstra8e182572007-07-19 01:48:54 -07001704 /*
1705 * Check whether last held lock:
1706 *
1707 * - is irq-safe, if this lock is irq-unsafe
1708 * - is softirq-safe, if this lock is hardirq-unsafe
1709 *
1710 * And check whether the new lock's dependency graph
1711 * could lead back to the previous lock.
1712 *
1713 * any of these scenarios could lead to a deadlock. If
1714 * All validations
1715 */
1716 int ret = check_deadlock(curr, hlock, lock, hlock->read);
1717
1718 if (!ret)
1719 return 0;
1720 /*
1721 * Mark recursive read, as we jump over it when
1722 * building dependencies (just like we jump over
1723 * trylock entries):
1724 */
1725 if (ret == 2)
1726 hlock->read = 2;
1727 /*
1728 * Add dependency only if this lock is not the head
1729 * of the chain, and if it's not a secondary read-lock:
1730 */
1731 if (!chain_head && ret != 2)
1732 if (!check_prevs_add(curr, hlock))
1733 return 0;
1734 graph_unlock();
1735 } else
1736 /* after lookup_chain_cache(): */
1737 if (unlikely(!debug_locks))
1738 return 0;
1739
1740 return 1;
1741}
1742#else
1743static inline int validate_chain(struct task_struct *curr,
1744 struct lockdep_map *lock, struct held_lock *hlock,
Gregory Haskins3aa416b2007-10-11 22:11:11 +02001745 int chain_head, u64 chain_key)
Peter Zijlstra8e182572007-07-19 01:48:54 -07001746{
1747 return 1;
1748}
Peter Zijlstraca58abc2007-07-19 01:48:53 -07001749#endif
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001750
1751/*
1752 * We are building curr_chain_key incrementally, so double-check
1753 * it from scratch, to make sure that it's done correctly:
1754 */
Steven Rostedt1d09daa2008-05-12 21:20:55 +02001755static void check_chain_key(struct task_struct *curr)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001756{
1757#ifdef CONFIG_DEBUG_LOCKDEP
1758 struct held_lock *hlock, *prev_hlock = NULL;
1759 unsigned int i, id;
1760 u64 chain_key = 0;
1761
1762 for (i = 0; i < curr->lockdep_depth; i++) {
1763 hlock = curr->held_locks + i;
1764 if (chain_key != hlock->prev_chain_key) {
1765 debug_locks_off();
Arjan van de Ven2df8b1d2008-07-30 12:43:11 -07001766 WARN(1, "hm#1, depth: %u [%u], %016Lx != %016Lx\n",
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001767 curr->lockdep_depth, i,
1768 (unsigned long long)chain_key,
1769 (unsigned long long)hlock->prev_chain_key);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001770 return;
1771 }
Dave Jonesf82b2172008-08-11 09:30:23 +02001772 id = hlock->class_idx - 1;
Jarek Poplawski381a2292007-02-10 01:44:58 -08001773 if (DEBUG_LOCKS_WARN_ON(id >= MAX_LOCKDEP_KEYS))
1774 return;
1775
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001776 if (prev_hlock && (prev_hlock->irq_context !=
1777 hlock->irq_context))
1778 chain_key = 0;
1779 chain_key = iterate_chain_key(chain_key, id);
1780 prev_hlock = hlock;
1781 }
1782 if (chain_key != curr->curr_chain_key) {
1783 debug_locks_off();
Arjan van de Ven2df8b1d2008-07-30 12:43:11 -07001784 WARN(1, "hm#2, depth: %u [%u], %016Lx != %016Lx\n",
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001785 curr->lockdep_depth, i,
1786 (unsigned long long)chain_key,
1787 (unsigned long long)curr->curr_chain_key);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001788 }
1789#endif
1790}
1791
Peter Zijlstra8e182572007-07-19 01:48:54 -07001792static int
1793print_usage_bug(struct task_struct *curr, struct held_lock *this,
1794 enum lock_usage_bit prev_bit, enum lock_usage_bit new_bit)
1795{
1796 if (!debug_locks_off_graph_unlock() || debug_locks_silent)
1797 return 0;
1798
1799 printk("\n=================================\n");
1800 printk( "[ INFO: inconsistent lock state ]\n");
1801 print_kernel_version();
1802 printk( "---------------------------------\n");
1803
1804 printk("inconsistent {%s} -> {%s} usage.\n",
1805 usage_str[prev_bit], usage_str[new_bit]);
1806
1807 printk("%s/%d [HC%u[%lu]:SC%u[%lu]:HE%u:SE%u] takes:\n",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07001808 curr->comm, task_pid_nr(curr),
Peter Zijlstra8e182572007-07-19 01:48:54 -07001809 trace_hardirq_context(curr), hardirq_count() >> HARDIRQ_SHIFT,
1810 trace_softirq_context(curr), softirq_count() >> SOFTIRQ_SHIFT,
1811 trace_hardirqs_enabled(curr),
1812 trace_softirqs_enabled(curr));
1813 print_lock(this);
1814
1815 printk("{%s} state was registered at:\n", usage_str[prev_bit]);
Dave Jonesf82b2172008-08-11 09:30:23 +02001816 print_stack_trace(hlock_class(this)->usage_traces + prev_bit, 1);
Peter Zijlstra8e182572007-07-19 01:48:54 -07001817
1818 print_irqtrace_events(curr);
1819 printk("\nother info that might help us debug this:\n");
1820 lockdep_print_held_locks(curr);
1821
1822 printk("\nstack backtrace:\n");
1823 dump_stack();
1824
1825 return 0;
1826}
1827
1828/*
1829 * Print out an error if an invalid bit is set:
1830 */
1831static inline int
1832valid_state(struct task_struct *curr, struct held_lock *this,
1833 enum lock_usage_bit new_bit, enum lock_usage_bit bad_bit)
1834{
Dave Jonesf82b2172008-08-11 09:30:23 +02001835 if (unlikely(hlock_class(this)->usage_mask & (1 << bad_bit)))
Peter Zijlstra8e182572007-07-19 01:48:54 -07001836 return print_usage_bug(curr, this, bad_bit, new_bit);
1837 return 1;
1838}
1839
1840static int mark_lock(struct task_struct *curr, struct held_lock *this,
1841 enum lock_usage_bit new_bit);
1842
Steven Rostedt81d68a92008-05-12 21:20:42 +02001843#if defined(CONFIG_TRACE_IRQFLAGS) && defined(CONFIG_PROVE_LOCKING)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001844
1845/*
1846 * print irq inversion bug:
1847 */
1848static int
1849print_irq_inversion_bug(struct task_struct *curr, struct lock_class *other,
1850 struct held_lock *this, int forwards,
1851 const char *irqclass)
1852{
Ingo Molnar74c383f2006-12-13 00:34:43 -08001853 if (!debug_locks_off_graph_unlock() || debug_locks_silent)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001854 return 0;
1855
1856 printk("\n=========================================================\n");
1857 printk( "[ INFO: possible irq lock inversion dependency detected ]\n");
Dave Jones99de0552006-09-29 02:00:10 -07001858 print_kernel_version();
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001859 printk( "---------------------------------------------------------\n");
1860 printk("%s/%d just changed the state of lock:\n",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07001861 curr->comm, task_pid_nr(curr));
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001862 print_lock(this);
1863 if (forwards)
1864 printk("but this lock took another, %s-irq-unsafe lock in the past:\n", irqclass);
1865 else
1866 printk("but this lock was taken by another, %s-irq-safe lock in the past:\n", irqclass);
1867 print_lock_name(other);
1868 printk("\n\nand interrupts could create inverse lock ordering between them.\n\n");
1869
1870 printk("\nother info that might help us debug this:\n");
1871 lockdep_print_held_locks(curr);
1872
1873 printk("\nthe first lock's dependencies:\n");
Dave Jonesf82b2172008-08-11 09:30:23 +02001874 print_lock_dependencies(hlock_class(this), 0);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001875
1876 printk("\nthe second lock's dependencies:\n");
1877 print_lock_dependencies(other, 0);
1878
1879 printk("\nstack backtrace:\n");
1880 dump_stack();
1881
1882 return 0;
1883}
1884
1885/*
1886 * Prove that in the forwards-direction subgraph starting at <this>
1887 * there is no lock matching <mask>:
1888 */
1889static int
1890check_usage_forwards(struct task_struct *curr, struct held_lock *this,
1891 enum lock_usage_bit bit, const char *irqclass)
1892{
1893 int ret;
1894
1895 find_usage_bit = bit;
1896 /* fills in <forwards_match> */
Dave Jonesf82b2172008-08-11 09:30:23 +02001897 ret = find_usage_forwards(hlock_class(this), 0);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001898 if (!ret || ret == 1)
1899 return ret;
1900
1901 return print_irq_inversion_bug(curr, forwards_match, this, 1, irqclass);
1902}
1903
1904/*
1905 * Prove that in the backwards-direction subgraph starting at <this>
1906 * there is no lock matching <mask>:
1907 */
1908static int
1909check_usage_backwards(struct task_struct *curr, struct held_lock *this,
1910 enum lock_usage_bit bit, const char *irqclass)
1911{
1912 int ret;
1913
1914 find_usage_bit = bit;
1915 /* fills in <backwards_match> */
Dave Jonesf82b2172008-08-11 09:30:23 +02001916 ret = find_usage_backwards(hlock_class(this), 0);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001917 if (!ret || ret == 1)
1918 return ret;
1919
1920 return print_irq_inversion_bug(curr, backwards_match, this, 0, irqclass);
1921}
1922
Ingo Molnar3117df02006-12-13 00:34:43 -08001923void print_irqtrace_events(struct task_struct *curr)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001924{
1925 printk("irq event stamp: %u\n", curr->irq_events);
1926 printk("hardirqs last enabled at (%u): ", curr->hardirq_enable_event);
1927 print_ip_sym(curr->hardirq_enable_ip);
1928 printk("hardirqs last disabled at (%u): ", curr->hardirq_disable_event);
1929 print_ip_sym(curr->hardirq_disable_ip);
1930 printk("softirqs last enabled at (%u): ", curr->softirq_enable_event);
1931 print_ip_sym(curr->softirq_enable_ip);
1932 printk("softirqs last disabled at (%u): ", curr->softirq_disable_event);
1933 print_ip_sym(curr->softirq_disable_ip);
1934}
1935
Peter Zijlstra8e182572007-07-19 01:48:54 -07001936static int hardirq_verbose(struct lock_class *class)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001937{
Peter Zijlstra8e182572007-07-19 01:48:54 -07001938#if HARDIRQ_VERBOSE
1939 return class_filter(class);
1940#endif
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001941 return 0;
1942}
1943
Peter Zijlstra8e182572007-07-19 01:48:54 -07001944static int softirq_verbose(struct lock_class *class)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001945{
Peter Zijlstra8e182572007-07-19 01:48:54 -07001946#if SOFTIRQ_VERBOSE
1947 return class_filter(class);
1948#endif
1949 return 0;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001950}
1951
1952#define STRICT_READ_CHECKS 1
1953
Peter Zijlstra8e182572007-07-19 01:48:54 -07001954static int mark_lock_irq(struct task_struct *curr, struct held_lock *this,
1955 enum lock_usage_bit new_bit)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001956{
Peter Zijlstra8e182572007-07-19 01:48:54 -07001957 int ret = 1;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001958
Peter Zijlstra8e182572007-07-19 01:48:54 -07001959 switch(new_bit) {
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001960 case LOCK_USED_IN_HARDIRQ:
1961 if (!valid_state(curr, this, new_bit, LOCK_ENABLED_HARDIRQS))
1962 return 0;
1963 if (!valid_state(curr, this, new_bit,
1964 LOCK_ENABLED_HARDIRQS_READ))
1965 return 0;
1966 /*
1967 * just marked it hardirq-safe, check that this lock
1968 * took no hardirq-unsafe lock in the past:
1969 */
1970 if (!check_usage_forwards(curr, this,
1971 LOCK_ENABLED_HARDIRQS, "hard"))
1972 return 0;
1973#if STRICT_READ_CHECKS
1974 /*
1975 * just marked it hardirq-safe, check that this lock
1976 * took no hardirq-unsafe-read lock in the past:
1977 */
1978 if (!check_usage_forwards(curr, this,
1979 LOCK_ENABLED_HARDIRQS_READ, "hard-read"))
1980 return 0;
1981#endif
Dave Jonesf82b2172008-08-11 09:30:23 +02001982 if (hardirq_verbose(hlock_class(this)))
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001983 ret = 2;
1984 break;
1985 case LOCK_USED_IN_SOFTIRQ:
1986 if (!valid_state(curr, this, new_bit, LOCK_ENABLED_SOFTIRQS))
1987 return 0;
1988 if (!valid_state(curr, this, new_bit,
1989 LOCK_ENABLED_SOFTIRQS_READ))
1990 return 0;
1991 /*
1992 * just marked it softirq-safe, check that this lock
1993 * took no softirq-unsafe lock in the past:
1994 */
1995 if (!check_usage_forwards(curr, this,
1996 LOCK_ENABLED_SOFTIRQS, "soft"))
1997 return 0;
1998#if STRICT_READ_CHECKS
1999 /*
2000 * just marked it softirq-safe, check that this lock
2001 * took no softirq-unsafe-read lock in the past:
2002 */
2003 if (!check_usage_forwards(curr, this,
2004 LOCK_ENABLED_SOFTIRQS_READ, "soft-read"))
2005 return 0;
2006#endif
Dave Jonesf82b2172008-08-11 09:30:23 +02002007 if (softirq_verbose(hlock_class(this)))
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002008 ret = 2;
2009 break;
2010 case LOCK_USED_IN_HARDIRQ_READ:
2011 if (!valid_state(curr, this, new_bit, LOCK_ENABLED_HARDIRQS))
2012 return 0;
2013 /*
2014 * just marked it hardirq-read-safe, check that this lock
2015 * took no hardirq-unsafe lock in the past:
2016 */
2017 if (!check_usage_forwards(curr, this,
2018 LOCK_ENABLED_HARDIRQS, "hard"))
2019 return 0;
Dave Jonesf82b2172008-08-11 09:30:23 +02002020 if (hardirq_verbose(hlock_class(this)))
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002021 ret = 2;
2022 break;
2023 case LOCK_USED_IN_SOFTIRQ_READ:
2024 if (!valid_state(curr, this, new_bit, LOCK_ENABLED_SOFTIRQS))
2025 return 0;
2026 /*
2027 * just marked it softirq-read-safe, check that this lock
2028 * took no softirq-unsafe lock in the past:
2029 */
2030 if (!check_usage_forwards(curr, this,
2031 LOCK_ENABLED_SOFTIRQS, "soft"))
2032 return 0;
Dave Jonesf82b2172008-08-11 09:30:23 +02002033 if (softirq_verbose(hlock_class(this)))
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002034 ret = 2;
2035 break;
2036 case LOCK_ENABLED_HARDIRQS:
2037 if (!valid_state(curr, this, new_bit, LOCK_USED_IN_HARDIRQ))
2038 return 0;
2039 if (!valid_state(curr, this, new_bit,
2040 LOCK_USED_IN_HARDIRQ_READ))
2041 return 0;
2042 /*
2043 * just marked it hardirq-unsafe, check that no hardirq-safe
2044 * lock in the system ever took it in the past:
2045 */
2046 if (!check_usage_backwards(curr, this,
2047 LOCK_USED_IN_HARDIRQ, "hard"))
2048 return 0;
2049#if STRICT_READ_CHECKS
2050 /*
2051 * just marked it hardirq-unsafe, check that no
2052 * hardirq-safe-read lock in the system ever took
2053 * it in the past:
2054 */
2055 if (!check_usage_backwards(curr, this,
2056 LOCK_USED_IN_HARDIRQ_READ, "hard-read"))
2057 return 0;
2058#endif
Dave Jonesf82b2172008-08-11 09:30:23 +02002059 if (hardirq_verbose(hlock_class(this)))
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002060 ret = 2;
2061 break;
2062 case LOCK_ENABLED_SOFTIRQS:
2063 if (!valid_state(curr, this, new_bit, LOCK_USED_IN_SOFTIRQ))
2064 return 0;
2065 if (!valid_state(curr, this, new_bit,
2066 LOCK_USED_IN_SOFTIRQ_READ))
2067 return 0;
2068 /*
2069 * just marked it softirq-unsafe, check that no softirq-safe
2070 * lock in the system ever took it in the past:
2071 */
2072 if (!check_usage_backwards(curr, this,
2073 LOCK_USED_IN_SOFTIRQ, "soft"))
2074 return 0;
2075#if STRICT_READ_CHECKS
2076 /*
2077 * just marked it softirq-unsafe, check that no
2078 * softirq-safe-read lock in the system ever took
2079 * it in the past:
2080 */
2081 if (!check_usage_backwards(curr, this,
2082 LOCK_USED_IN_SOFTIRQ_READ, "soft-read"))
2083 return 0;
2084#endif
Dave Jonesf82b2172008-08-11 09:30:23 +02002085 if (softirq_verbose(hlock_class(this)))
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002086 ret = 2;
2087 break;
2088 case LOCK_ENABLED_HARDIRQS_READ:
2089 if (!valid_state(curr, this, new_bit, LOCK_USED_IN_HARDIRQ))
2090 return 0;
2091#if STRICT_READ_CHECKS
2092 /*
2093 * just marked it hardirq-read-unsafe, check that no
2094 * hardirq-safe lock in the system ever took it in the past:
2095 */
2096 if (!check_usage_backwards(curr, this,
2097 LOCK_USED_IN_HARDIRQ, "hard"))
2098 return 0;
2099#endif
Dave Jonesf82b2172008-08-11 09:30:23 +02002100 if (hardirq_verbose(hlock_class(this)))
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002101 ret = 2;
2102 break;
2103 case LOCK_ENABLED_SOFTIRQS_READ:
2104 if (!valid_state(curr, this, new_bit, LOCK_USED_IN_SOFTIRQ))
2105 return 0;
2106#if STRICT_READ_CHECKS
2107 /*
2108 * just marked it softirq-read-unsafe, check that no
2109 * softirq-safe lock in the system ever took it in the past:
2110 */
2111 if (!check_usage_backwards(curr, this,
2112 LOCK_USED_IN_SOFTIRQ, "soft"))
2113 return 0;
2114#endif
Dave Jonesf82b2172008-08-11 09:30:23 +02002115 if (softirq_verbose(hlock_class(this)))
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002116 ret = 2;
2117 break;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002118 default:
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002119 WARN_ON(1);
Peter Zijlstra8e182572007-07-19 01:48:54 -07002120 break;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002121 }
2122
2123 return ret;
2124}
2125
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002126/*
2127 * Mark all held locks with a usage bit:
2128 */
Steven Rostedt1d09daa2008-05-12 21:20:55 +02002129static int
Jarek Poplawski4ff773bb2007-05-08 00:31:00 -07002130mark_held_locks(struct task_struct *curr, int hardirq)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002131{
2132 enum lock_usage_bit usage_bit;
2133 struct held_lock *hlock;
2134 int i;
2135
2136 for (i = 0; i < curr->lockdep_depth; i++) {
2137 hlock = curr->held_locks + i;
2138
2139 if (hardirq) {
2140 if (hlock->read)
2141 usage_bit = LOCK_ENABLED_HARDIRQS_READ;
2142 else
2143 usage_bit = LOCK_ENABLED_HARDIRQS;
2144 } else {
2145 if (hlock->read)
2146 usage_bit = LOCK_ENABLED_SOFTIRQS_READ;
2147 else
2148 usage_bit = LOCK_ENABLED_SOFTIRQS;
2149 }
Jarek Poplawski4ff773bb2007-05-08 00:31:00 -07002150 if (!mark_lock(curr, hlock, usage_bit))
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002151 return 0;
2152 }
2153
2154 return 1;
2155}
2156
2157/*
2158 * Debugging helper: via this flag we know that we are in
2159 * 'early bootup code', and will warn about any invalid irqs-on event:
2160 */
2161static int early_boot_irqs_enabled;
2162
2163void early_boot_irqs_off(void)
2164{
2165 early_boot_irqs_enabled = 0;
2166}
2167
2168void early_boot_irqs_on(void)
2169{
2170 early_boot_irqs_enabled = 1;
2171}
2172
2173/*
2174 * Hardirqs will be enabled:
2175 */
Steven Rostedt1d09daa2008-05-12 21:20:55 +02002176void trace_hardirqs_on_caller(unsigned long a0)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002177{
2178 struct task_struct *curr = current;
2179 unsigned long ip;
2180
Steven Rostedt81d68a92008-05-12 21:20:42 +02002181 time_hardirqs_on(CALLER_ADDR0, a0);
2182
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002183 if (unlikely(!debug_locks || current->lockdep_recursion))
2184 return;
2185
2186 if (DEBUG_LOCKS_WARN_ON(unlikely(!early_boot_irqs_enabled)))
2187 return;
2188
2189 if (unlikely(curr->hardirqs_enabled)) {
2190 debug_atomic_inc(&redundant_hardirqs_on);
2191 return;
2192 }
2193 /* we'll do an OFF -> ON transition: */
2194 curr->hardirqs_enabled = 1;
2195 ip = (unsigned long) __builtin_return_address(0);
2196
2197 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2198 return;
2199 if (DEBUG_LOCKS_WARN_ON(current->hardirq_context))
2200 return;
2201 /*
2202 * We are going to turn hardirqs on, so set the
2203 * usage bit for all held locks:
2204 */
Jarek Poplawski4ff773bb2007-05-08 00:31:00 -07002205 if (!mark_held_locks(curr, 1))
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002206 return;
2207 /*
2208 * If we have softirqs enabled, then set the usage
2209 * bit for all held locks. (disabled hardirqs prevented
2210 * this bit from being set before)
2211 */
2212 if (curr->softirqs_enabled)
Jarek Poplawski4ff773bb2007-05-08 00:31:00 -07002213 if (!mark_held_locks(curr, 0))
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002214 return;
2215
2216 curr->hardirq_enable_ip = ip;
2217 curr->hardirq_enable_event = ++curr->irq_events;
2218 debug_atomic_inc(&hardirqs_on_events);
2219}
Steven Rostedt81d68a92008-05-12 21:20:42 +02002220EXPORT_SYMBOL(trace_hardirqs_on_caller);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002221
Steven Rostedt1d09daa2008-05-12 21:20:55 +02002222void trace_hardirqs_on(void)
Steven Rostedt81d68a92008-05-12 21:20:42 +02002223{
2224 trace_hardirqs_on_caller(CALLER_ADDR0);
2225}
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002226EXPORT_SYMBOL(trace_hardirqs_on);
2227
2228/*
2229 * Hardirqs were disabled:
2230 */
Steven Rostedt1d09daa2008-05-12 21:20:55 +02002231void trace_hardirqs_off_caller(unsigned long a0)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002232{
2233 struct task_struct *curr = current;
2234
Steven Rostedt81d68a92008-05-12 21:20:42 +02002235 time_hardirqs_off(CALLER_ADDR0, a0);
2236
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002237 if (unlikely(!debug_locks || current->lockdep_recursion))
2238 return;
2239
2240 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2241 return;
2242
2243 if (curr->hardirqs_enabled) {
2244 /*
2245 * We have done an ON -> OFF transition:
2246 */
2247 curr->hardirqs_enabled = 0;
2248 curr->hardirq_disable_ip = _RET_IP_;
2249 curr->hardirq_disable_event = ++curr->irq_events;
2250 debug_atomic_inc(&hardirqs_off_events);
2251 } else
2252 debug_atomic_inc(&redundant_hardirqs_off);
2253}
Steven Rostedt81d68a92008-05-12 21:20:42 +02002254EXPORT_SYMBOL(trace_hardirqs_off_caller);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002255
Steven Rostedt1d09daa2008-05-12 21:20:55 +02002256void trace_hardirqs_off(void)
Steven Rostedt81d68a92008-05-12 21:20:42 +02002257{
2258 trace_hardirqs_off_caller(CALLER_ADDR0);
2259}
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002260EXPORT_SYMBOL(trace_hardirqs_off);
2261
2262/*
2263 * Softirqs will be enabled:
2264 */
2265void trace_softirqs_on(unsigned long ip)
2266{
2267 struct task_struct *curr = current;
2268
2269 if (unlikely(!debug_locks))
2270 return;
2271
2272 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2273 return;
2274
2275 if (curr->softirqs_enabled) {
2276 debug_atomic_inc(&redundant_softirqs_on);
2277 return;
2278 }
2279
2280 /*
2281 * We'll do an OFF -> ON transition:
2282 */
2283 curr->softirqs_enabled = 1;
2284 curr->softirq_enable_ip = ip;
2285 curr->softirq_enable_event = ++curr->irq_events;
2286 debug_atomic_inc(&softirqs_on_events);
2287 /*
2288 * We are going to turn softirqs on, so set the
2289 * usage bit for all held locks, if hardirqs are
2290 * enabled too:
2291 */
2292 if (curr->hardirqs_enabled)
Jarek Poplawski4ff773bb2007-05-08 00:31:00 -07002293 mark_held_locks(curr, 0);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002294}
2295
2296/*
2297 * Softirqs were disabled:
2298 */
2299void trace_softirqs_off(unsigned long ip)
2300{
2301 struct task_struct *curr = current;
2302
2303 if (unlikely(!debug_locks))
2304 return;
2305
2306 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2307 return;
2308
2309 if (curr->softirqs_enabled) {
2310 /*
2311 * We have done an ON -> OFF transition:
2312 */
2313 curr->softirqs_enabled = 0;
2314 curr->softirq_disable_ip = ip;
2315 curr->softirq_disable_event = ++curr->irq_events;
2316 debug_atomic_inc(&softirqs_off_events);
2317 DEBUG_LOCKS_WARN_ON(!softirq_count());
2318 } else
2319 debug_atomic_inc(&redundant_softirqs_off);
2320}
2321
Peter Zijlstra8e182572007-07-19 01:48:54 -07002322static int mark_irqflags(struct task_struct *curr, struct held_lock *hlock)
2323{
2324 /*
2325 * If non-trylock use in a hardirq or softirq context, then
2326 * mark the lock as used in these contexts:
2327 */
2328 if (!hlock->trylock) {
2329 if (hlock->read) {
2330 if (curr->hardirq_context)
2331 if (!mark_lock(curr, hlock,
2332 LOCK_USED_IN_HARDIRQ_READ))
2333 return 0;
2334 if (curr->softirq_context)
2335 if (!mark_lock(curr, hlock,
2336 LOCK_USED_IN_SOFTIRQ_READ))
2337 return 0;
2338 } else {
2339 if (curr->hardirq_context)
2340 if (!mark_lock(curr, hlock, LOCK_USED_IN_HARDIRQ))
2341 return 0;
2342 if (curr->softirq_context)
2343 if (!mark_lock(curr, hlock, LOCK_USED_IN_SOFTIRQ))
2344 return 0;
2345 }
2346 }
2347 if (!hlock->hardirqs_off) {
2348 if (hlock->read) {
2349 if (!mark_lock(curr, hlock,
2350 LOCK_ENABLED_HARDIRQS_READ))
2351 return 0;
2352 if (curr->softirqs_enabled)
2353 if (!mark_lock(curr, hlock,
2354 LOCK_ENABLED_SOFTIRQS_READ))
2355 return 0;
2356 } else {
2357 if (!mark_lock(curr, hlock,
2358 LOCK_ENABLED_HARDIRQS))
2359 return 0;
2360 if (curr->softirqs_enabled)
2361 if (!mark_lock(curr, hlock,
2362 LOCK_ENABLED_SOFTIRQS))
2363 return 0;
2364 }
2365 }
2366
2367 return 1;
2368}
2369
2370static int separate_irq_context(struct task_struct *curr,
2371 struct held_lock *hlock)
2372{
2373 unsigned int depth = curr->lockdep_depth;
2374
2375 /*
2376 * Keep track of points where we cross into an interrupt context:
2377 */
2378 hlock->irq_context = 2*(curr->hardirq_context ? 1 : 0) +
2379 curr->softirq_context;
2380 if (depth) {
2381 struct held_lock *prev_hlock;
2382
2383 prev_hlock = curr->held_locks + depth-1;
2384 /*
2385 * If we cross into another context, reset the
2386 * hash key (this also prevents the checking and the
2387 * adding of the dependency to 'prev'):
2388 */
2389 if (prev_hlock->irq_context != hlock->irq_context)
2390 return 1;
2391 }
2392 return 0;
2393}
2394
2395#else
2396
2397static inline
2398int mark_lock_irq(struct task_struct *curr, struct held_lock *this,
2399 enum lock_usage_bit new_bit)
2400{
2401 WARN_ON(1);
2402 return 1;
2403}
2404
2405static inline int mark_irqflags(struct task_struct *curr,
2406 struct held_lock *hlock)
2407{
2408 return 1;
2409}
2410
2411static inline int separate_irq_context(struct task_struct *curr,
2412 struct held_lock *hlock)
2413{
2414 return 0;
2415}
2416
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002417#endif
2418
2419/*
Peter Zijlstra8e182572007-07-19 01:48:54 -07002420 * Mark a lock with a usage bit, and validate the state transition:
2421 */
Steven Rostedt1d09daa2008-05-12 21:20:55 +02002422static int mark_lock(struct task_struct *curr, struct held_lock *this,
Steven Rostedt0764d232008-05-12 21:20:44 +02002423 enum lock_usage_bit new_bit)
Peter Zijlstra8e182572007-07-19 01:48:54 -07002424{
2425 unsigned int new_mask = 1 << new_bit, ret = 1;
2426
2427 /*
2428 * If already set then do not dirty the cacheline,
2429 * nor do any checks:
2430 */
Dave Jonesf82b2172008-08-11 09:30:23 +02002431 if (likely(hlock_class(this)->usage_mask & new_mask))
Peter Zijlstra8e182572007-07-19 01:48:54 -07002432 return 1;
2433
2434 if (!graph_lock())
2435 return 0;
2436 /*
2437 * Make sure we didnt race:
2438 */
Dave Jonesf82b2172008-08-11 09:30:23 +02002439 if (unlikely(hlock_class(this)->usage_mask & new_mask)) {
Peter Zijlstra8e182572007-07-19 01:48:54 -07002440 graph_unlock();
2441 return 1;
2442 }
2443
Dave Jonesf82b2172008-08-11 09:30:23 +02002444 hlock_class(this)->usage_mask |= new_mask;
Peter Zijlstra8e182572007-07-19 01:48:54 -07002445
Dave Jonesf82b2172008-08-11 09:30:23 +02002446 if (!save_trace(hlock_class(this)->usage_traces + new_bit))
Peter Zijlstra8e182572007-07-19 01:48:54 -07002447 return 0;
2448
2449 switch (new_bit) {
2450 case LOCK_USED_IN_HARDIRQ:
2451 case LOCK_USED_IN_SOFTIRQ:
2452 case LOCK_USED_IN_HARDIRQ_READ:
2453 case LOCK_USED_IN_SOFTIRQ_READ:
2454 case LOCK_ENABLED_HARDIRQS:
2455 case LOCK_ENABLED_SOFTIRQS:
2456 case LOCK_ENABLED_HARDIRQS_READ:
2457 case LOCK_ENABLED_SOFTIRQS_READ:
2458 ret = mark_lock_irq(curr, this, new_bit);
2459 if (!ret)
2460 return 0;
2461 break;
2462 case LOCK_USED:
Peter Zijlstra8e182572007-07-19 01:48:54 -07002463 debug_atomic_dec(&nr_unused_locks);
2464 break;
2465 default:
2466 if (!debug_locks_off_graph_unlock())
2467 return 0;
2468 WARN_ON(1);
2469 return 0;
2470 }
2471
2472 graph_unlock();
2473
2474 /*
2475 * We must printk outside of the graph_lock:
2476 */
2477 if (ret == 2) {
2478 printk("\nmarked lock as {%s}:\n", usage_str[new_bit]);
2479 print_lock(this);
2480 print_irqtrace_events(curr);
2481 dump_stack();
2482 }
2483
2484 return ret;
2485}
2486
2487/*
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002488 * Initialize a lock instance's lock-class mapping info:
2489 */
2490void lockdep_init_map(struct lockdep_map *lock, const char *name,
Peter Zijlstra4dfbb9d2006-10-11 01:45:14 -04002491 struct lock_class_key *key, int subclass)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002492{
2493 if (unlikely(!debug_locks))
2494 return;
2495
2496 if (DEBUG_LOCKS_WARN_ON(!key))
2497 return;
2498 if (DEBUG_LOCKS_WARN_ON(!name))
2499 return;
2500 /*
2501 * Sanity check, the lock-class key must be persistent:
2502 */
2503 if (!static_obj(key)) {
2504 printk("BUG: key %p not in .data!\n", key);
2505 DEBUG_LOCKS_WARN_ON(1);
2506 return;
2507 }
2508 lock->name = name;
2509 lock->key = key;
Ingo Molnard6d897c2006-07-10 04:44:04 -07002510 lock->class_cache = NULL;
Peter Zijlstra96645672007-07-19 01:49:00 -07002511#ifdef CONFIG_LOCK_STAT
2512 lock->cpu = raw_smp_processor_id();
2513#endif
Peter Zijlstra4dfbb9d2006-10-11 01:45:14 -04002514 if (subclass)
2515 register_lock_class(lock, subclass, 1);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002516}
2517
2518EXPORT_SYMBOL_GPL(lockdep_init_map);
2519
2520/*
2521 * This gets called for every mutex_lock*()/spin_lock*() operation.
2522 * We maintain the dependency maps and validate the locking attempt:
2523 */
2524static int __lock_acquire(struct lockdep_map *lock, unsigned int subclass,
2525 int trylock, int read, int check, int hardirqs_off,
Peter Zijlstra7531e2f2008-08-11 09:30:24 +02002526 struct lockdep_map *nest_lock, unsigned long ip)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002527{
2528 struct task_struct *curr = current;
Ingo Molnard6d897c2006-07-10 04:44:04 -07002529 struct lock_class *class = NULL;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002530 struct held_lock *hlock;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002531 unsigned int depth, id;
2532 int chain_head = 0;
2533 u64 chain_key;
2534
Peter Zijlstraf20786f2007-07-19 01:48:56 -07002535 if (!prove_locking)
2536 check = 1;
2537
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002538 if (unlikely(!debug_locks))
2539 return 0;
2540
2541 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2542 return 0;
2543
2544 if (unlikely(subclass >= MAX_LOCKDEP_SUBCLASSES)) {
2545 debug_locks_off();
2546 printk("BUG: MAX_LOCKDEP_SUBCLASSES too low!\n");
2547 printk("turning off the locking correctness validator.\n");
2548 return 0;
2549 }
2550
Ingo Molnard6d897c2006-07-10 04:44:04 -07002551 if (!subclass)
2552 class = lock->class_cache;
2553 /*
2554 * Not cached yet or subclass?
2555 */
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002556 if (unlikely(!class)) {
Peter Zijlstra4dfbb9d2006-10-11 01:45:14 -04002557 class = register_lock_class(lock, subclass, 0);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002558 if (!class)
2559 return 0;
2560 }
2561 debug_atomic_inc((atomic_t *)&class->ops);
2562 if (very_verbose(class)) {
2563 printk("\nacquire class [%p] %s", class->key, class->name);
2564 if (class->name_version > 1)
2565 printk("#%d", class->name_version);
2566 printk("\n");
2567 dump_stack();
2568 }
2569
2570 /*
2571 * Add the lock to the list of currently held locks.
2572 * (we dont increase the depth just yet, up until the
2573 * dependency checks are done)
2574 */
2575 depth = curr->lockdep_depth;
2576 if (DEBUG_LOCKS_WARN_ON(depth >= MAX_LOCK_DEPTH))
2577 return 0;
2578
2579 hlock = curr->held_locks + depth;
Dave Jonesf82b2172008-08-11 09:30:23 +02002580 if (DEBUG_LOCKS_WARN_ON(!class))
2581 return 0;
2582 hlock->class_idx = class - lock_classes + 1;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002583 hlock->acquire_ip = ip;
2584 hlock->instance = lock;
Peter Zijlstra7531e2f2008-08-11 09:30:24 +02002585 hlock->nest_lock = nest_lock;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002586 hlock->trylock = trylock;
2587 hlock->read = read;
2588 hlock->check = check;
Dmitry Baryshkov6951b122008-08-18 04:26:37 +04002589 hlock->hardirqs_off = !!hardirqs_off;
Peter Zijlstraf20786f2007-07-19 01:48:56 -07002590#ifdef CONFIG_LOCK_STAT
2591 hlock->waittime_stamp = 0;
2592 hlock->holdtime_stamp = sched_clock();
2593#endif
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002594
Peter Zijlstra8e182572007-07-19 01:48:54 -07002595 if (check == 2 && !mark_irqflags(curr, hlock))
2596 return 0;
2597
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002598 /* mark it as used: */
Jarek Poplawski4ff773bb2007-05-08 00:31:00 -07002599 if (!mark_lock(curr, hlock, LOCK_USED))
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002600 return 0;
Peter Zijlstra8e182572007-07-19 01:48:54 -07002601
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002602 /*
Gautham R Shenoy17aacfb2007-10-28 20:47:01 +01002603 * Calculate the chain hash: it's the combined hash of all the
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002604 * lock keys along the dependency chain. We save the hash value
2605 * at every step so that we can get the current hash easily
2606 * after unlock. The chain hash is then used to cache dependency
2607 * results.
2608 *
2609 * The 'key ID' is what is the most compact key value to drive
2610 * the hash, not class->key.
2611 */
2612 id = class - lock_classes;
2613 if (DEBUG_LOCKS_WARN_ON(id >= MAX_LOCKDEP_KEYS))
2614 return 0;
2615
2616 chain_key = curr->curr_chain_key;
2617 if (!depth) {
2618 if (DEBUG_LOCKS_WARN_ON(chain_key != 0))
2619 return 0;
2620 chain_head = 1;
2621 }
2622
2623 hlock->prev_chain_key = chain_key;
Peter Zijlstra8e182572007-07-19 01:48:54 -07002624 if (separate_irq_context(curr, hlock)) {
2625 chain_key = 0;
2626 chain_head = 1;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002627 }
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002628 chain_key = iterate_chain_key(chain_key, id);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002629
Gregory Haskins3aa416b2007-10-11 22:11:11 +02002630 if (!validate_chain(curr, lock, hlock, chain_head, chain_key))
Peter Zijlstra8e182572007-07-19 01:48:54 -07002631 return 0;
Jarek Poplawski381a2292007-02-10 01:44:58 -08002632
Gregory Haskins3aa416b2007-10-11 22:11:11 +02002633 curr->curr_chain_key = chain_key;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002634 curr->lockdep_depth++;
2635 check_chain_key(curr);
Jarek Poplawski60e114d2007-02-20 13:58:00 -08002636#ifdef CONFIG_DEBUG_LOCKDEP
2637 if (unlikely(!debug_locks))
2638 return 0;
2639#endif
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002640 if (unlikely(curr->lockdep_depth >= MAX_LOCK_DEPTH)) {
2641 debug_locks_off();
2642 printk("BUG: MAX_LOCK_DEPTH too low!\n");
2643 printk("turning off the locking correctness validator.\n");
2644 return 0;
2645 }
Jarek Poplawski381a2292007-02-10 01:44:58 -08002646
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002647 if (unlikely(curr->lockdep_depth > max_lockdep_depth))
2648 max_lockdep_depth = curr->lockdep_depth;
2649
2650 return 1;
2651}
2652
2653static int
2654print_unlock_inbalance_bug(struct task_struct *curr, struct lockdep_map *lock,
2655 unsigned long ip)
2656{
2657 if (!debug_locks_off())
2658 return 0;
2659 if (debug_locks_silent)
2660 return 0;
2661
2662 printk("\n=====================================\n");
2663 printk( "[ BUG: bad unlock balance detected! ]\n");
2664 printk( "-------------------------------------\n");
2665 printk("%s/%d is trying to release lock (",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07002666 curr->comm, task_pid_nr(curr));
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002667 print_lockdep_cache(lock);
2668 printk(") at:\n");
2669 print_ip_sym(ip);
2670 printk("but there are no more locks to release!\n");
2671 printk("\nother info that might help us debug this:\n");
2672 lockdep_print_held_locks(curr);
2673
2674 printk("\nstack backtrace:\n");
2675 dump_stack();
2676
2677 return 0;
2678}
2679
2680/*
2681 * Common debugging checks for both nested and non-nested unlock:
2682 */
2683static int check_unlock(struct task_struct *curr, struct lockdep_map *lock,
2684 unsigned long ip)
2685{
2686 if (unlikely(!debug_locks))
2687 return 0;
2688 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2689 return 0;
2690
2691 if (curr->lockdep_depth <= 0)
2692 return print_unlock_inbalance_bug(curr, lock, ip);
2693
2694 return 1;
2695}
2696
Peter Zijlstra64aa3482008-08-11 09:30:21 +02002697static int
2698__lock_set_subclass(struct lockdep_map *lock,
2699 unsigned int subclass, unsigned long ip)
2700{
2701 struct task_struct *curr = current;
2702 struct held_lock *hlock, *prev_hlock;
2703 struct lock_class *class;
2704 unsigned int depth;
2705 int i;
2706
2707 depth = curr->lockdep_depth;
2708 if (DEBUG_LOCKS_WARN_ON(!depth))
2709 return 0;
2710
2711 prev_hlock = NULL;
2712 for (i = depth-1; i >= 0; i--) {
2713 hlock = curr->held_locks + i;
2714 /*
2715 * We must not cross into another context:
2716 */
2717 if (prev_hlock && prev_hlock->irq_context != hlock->irq_context)
2718 break;
2719 if (hlock->instance == lock)
2720 goto found_it;
2721 prev_hlock = hlock;
2722 }
2723 return print_unlock_inbalance_bug(curr, lock, ip);
2724
2725found_it:
2726 class = register_lock_class(lock, subclass, 0);
Dave Jonesf82b2172008-08-11 09:30:23 +02002727 hlock->class_idx = class - lock_classes + 1;
Peter Zijlstra64aa3482008-08-11 09:30:21 +02002728
2729 curr->lockdep_depth = i;
2730 curr->curr_chain_key = hlock->prev_chain_key;
2731
2732 for (; i < depth; i++) {
2733 hlock = curr->held_locks + i;
2734 if (!__lock_acquire(hlock->instance,
Dave Jonesf82b2172008-08-11 09:30:23 +02002735 hlock_class(hlock)->subclass, hlock->trylock,
Peter Zijlstra64aa3482008-08-11 09:30:21 +02002736 hlock->read, hlock->check, hlock->hardirqs_off,
Peter Zijlstra7531e2f2008-08-11 09:30:24 +02002737 hlock->nest_lock, hlock->acquire_ip))
Peter Zijlstra64aa3482008-08-11 09:30:21 +02002738 return 0;
2739 }
2740
2741 if (DEBUG_LOCKS_WARN_ON(curr->lockdep_depth != depth))
2742 return 0;
2743 return 1;
2744}
2745
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002746/*
2747 * Remove the lock to the list of currently held locks in a
2748 * potentially non-nested (out of order) manner. This is a
2749 * relatively rare operation, as all the unlock APIs default
2750 * to nested mode (which uses lock_release()):
2751 */
2752static int
2753lock_release_non_nested(struct task_struct *curr,
2754 struct lockdep_map *lock, unsigned long ip)
2755{
2756 struct held_lock *hlock, *prev_hlock;
2757 unsigned int depth;
2758 int i;
2759
2760 /*
2761 * Check whether the lock exists in the current stack
2762 * of held locks:
2763 */
2764 depth = curr->lockdep_depth;
2765 if (DEBUG_LOCKS_WARN_ON(!depth))
2766 return 0;
2767
2768 prev_hlock = NULL;
2769 for (i = depth-1; i >= 0; i--) {
2770 hlock = curr->held_locks + i;
2771 /*
2772 * We must not cross into another context:
2773 */
2774 if (prev_hlock && prev_hlock->irq_context != hlock->irq_context)
2775 break;
2776 if (hlock->instance == lock)
2777 goto found_it;
2778 prev_hlock = hlock;
2779 }
2780 return print_unlock_inbalance_bug(curr, lock, ip);
2781
2782found_it:
Peter Zijlstraf20786f2007-07-19 01:48:56 -07002783 lock_release_holdtime(hlock);
2784
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002785 /*
2786 * We have the right lock to unlock, 'hlock' points to it.
2787 * Now we remove it from the stack, and add back the other
2788 * entries (if any), recalculating the hash along the way:
2789 */
2790 curr->lockdep_depth = i;
2791 curr->curr_chain_key = hlock->prev_chain_key;
2792
2793 for (i++; i < depth; i++) {
2794 hlock = curr->held_locks + i;
2795 if (!__lock_acquire(hlock->instance,
Dave Jonesf82b2172008-08-11 09:30:23 +02002796 hlock_class(hlock)->subclass, hlock->trylock,
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002797 hlock->read, hlock->check, hlock->hardirqs_off,
Peter Zijlstra7531e2f2008-08-11 09:30:24 +02002798 hlock->nest_lock, hlock->acquire_ip))
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002799 return 0;
2800 }
2801
2802 if (DEBUG_LOCKS_WARN_ON(curr->lockdep_depth != depth - 1))
2803 return 0;
2804 return 1;
2805}
2806
2807/*
2808 * Remove the lock to the list of currently held locks - this gets
2809 * called on mutex_unlock()/spin_unlock*() (or on a failed
2810 * mutex_lock_interruptible()). This is done for unlocks that nest
2811 * perfectly. (i.e. the current top of the lock-stack is unlocked)
2812 */
2813static int lock_release_nested(struct task_struct *curr,
2814 struct lockdep_map *lock, unsigned long ip)
2815{
2816 struct held_lock *hlock;
2817 unsigned int depth;
2818
2819 /*
2820 * Pop off the top of the lock stack:
2821 */
2822 depth = curr->lockdep_depth - 1;
2823 hlock = curr->held_locks + depth;
2824
2825 /*
2826 * Is the unlock non-nested:
2827 */
2828 if (hlock->instance != lock)
2829 return lock_release_non_nested(curr, lock, ip);
2830 curr->lockdep_depth--;
2831
2832 if (DEBUG_LOCKS_WARN_ON(!depth && (hlock->prev_chain_key != 0)))
2833 return 0;
2834
2835 curr->curr_chain_key = hlock->prev_chain_key;
2836
Peter Zijlstraf20786f2007-07-19 01:48:56 -07002837 lock_release_holdtime(hlock);
2838
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002839#ifdef CONFIG_DEBUG_LOCKDEP
2840 hlock->prev_chain_key = 0;
Dave Jonesf82b2172008-08-11 09:30:23 +02002841 hlock->class_idx = 0;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002842 hlock->acquire_ip = 0;
2843 hlock->irq_context = 0;
2844#endif
2845 return 1;
2846}
2847
2848/*
2849 * Remove the lock to the list of currently held locks - this gets
2850 * called on mutex_unlock()/spin_unlock*() (or on a failed
2851 * mutex_lock_interruptible()). This is done for unlocks that nest
2852 * perfectly. (i.e. the current top of the lock-stack is unlocked)
2853 */
2854static void
2855__lock_release(struct lockdep_map *lock, int nested, unsigned long ip)
2856{
2857 struct task_struct *curr = current;
2858
2859 if (!check_unlock(curr, lock, ip))
2860 return;
2861
2862 if (nested) {
2863 if (!lock_release_nested(curr, lock, ip))
2864 return;
2865 } else {
2866 if (!lock_release_non_nested(curr, lock, ip))
2867 return;
2868 }
2869
2870 check_chain_key(curr);
2871}
2872
2873/*
2874 * Check whether we follow the irq-flags state precisely:
2875 */
Steven Rostedt1d09daa2008-05-12 21:20:55 +02002876static void check_flags(unsigned long flags)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002877{
Ingo Molnar992860e2008-07-14 10:28:38 +02002878#if defined(CONFIG_PROVE_LOCKING) && defined(CONFIG_DEBUG_LOCKDEP) && \
2879 defined(CONFIG_TRACE_IRQFLAGS)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002880 if (!debug_locks)
2881 return;
2882
Ingo Molnar5f9fa8a2007-12-07 19:02:47 +01002883 if (irqs_disabled_flags(flags)) {
2884 if (DEBUG_LOCKS_WARN_ON(current->hardirqs_enabled)) {
2885 printk("possible reason: unannotated irqs-off.\n");
2886 }
2887 } else {
2888 if (DEBUG_LOCKS_WARN_ON(!current->hardirqs_enabled)) {
2889 printk("possible reason: unannotated irqs-on.\n");
2890 }
2891 }
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002892
2893 /*
2894 * We dont accurately track softirq state in e.g.
2895 * hardirq contexts (such as on 4KSTACKS), so only
2896 * check if not in hardirq contexts:
2897 */
2898 if (!hardirq_count()) {
2899 if (softirq_count())
2900 DEBUG_LOCKS_WARN_ON(current->softirqs_enabled);
2901 else
2902 DEBUG_LOCKS_WARN_ON(!current->softirqs_enabled);
2903 }
2904
2905 if (!debug_locks)
2906 print_irqtrace_events(current);
2907#endif
2908}
2909
Peter Zijlstra64aa3482008-08-11 09:30:21 +02002910void
2911lock_set_subclass(struct lockdep_map *lock,
2912 unsigned int subclass, unsigned long ip)
2913{
2914 unsigned long flags;
2915
2916 if (unlikely(current->lockdep_recursion))
2917 return;
2918
2919 raw_local_irq_save(flags);
2920 current->lockdep_recursion = 1;
2921 check_flags(flags);
2922 if (__lock_set_subclass(lock, subclass, ip))
2923 check_chain_key(current);
2924 current->lockdep_recursion = 0;
2925 raw_local_irq_restore(flags);
2926}
2927
2928EXPORT_SYMBOL_GPL(lock_set_subclass);
2929
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002930/*
2931 * We are not always called with irqs disabled - do that here,
2932 * and also avoid lockdep recursion:
2933 */
Steven Rostedt1d09daa2008-05-12 21:20:55 +02002934void lock_acquire(struct lockdep_map *lock, unsigned int subclass,
Peter Zijlstra7531e2f2008-08-11 09:30:24 +02002935 int trylock, int read, int check,
2936 struct lockdep_map *nest_lock, unsigned long ip)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002937{
2938 unsigned long flags;
2939
2940 if (unlikely(current->lockdep_recursion))
2941 return;
2942
2943 raw_local_irq_save(flags);
2944 check_flags(flags);
2945
2946 current->lockdep_recursion = 1;
2947 __lock_acquire(lock, subclass, trylock, read, check,
Peter Zijlstra7531e2f2008-08-11 09:30:24 +02002948 irqs_disabled_flags(flags), nest_lock, ip);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002949 current->lockdep_recursion = 0;
2950 raw_local_irq_restore(flags);
2951}
2952
2953EXPORT_SYMBOL_GPL(lock_acquire);
2954
Steven Rostedt1d09daa2008-05-12 21:20:55 +02002955void lock_release(struct lockdep_map *lock, int nested,
Steven Rostedt0764d232008-05-12 21:20:44 +02002956 unsigned long ip)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002957{
2958 unsigned long flags;
2959
2960 if (unlikely(current->lockdep_recursion))
2961 return;
2962
2963 raw_local_irq_save(flags);
2964 check_flags(flags);
2965 current->lockdep_recursion = 1;
2966 __lock_release(lock, nested, ip);
2967 current->lockdep_recursion = 0;
2968 raw_local_irq_restore(flags);
2969}
2970
2971EXPORT_SYMBOL_GPL(lock_release);
2972
Peter Zijlstraf20786f2007-07-19 01:48:56 -07002973#ifdef CONFIG_LOCK_STAT
2974static int
2975print_lock_contention_bug(struct task_struct *curr, struct lockdep_map *lock,
2976 unsigned long ip)
2977{
2978 if (!debug_locks_off())
2979 return 0;
2980 if (debug_locks_silent)
2981 return 0;
2982
2983 printk("\n=================================\n");
2984 printk( "[ BUG: bad contention detected! ]\n");
2985 printk( "---------------------------------\n");
2986 printk("%s/%d is trying to contend lock (",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07002987 curr->comm, task_pid_nr(curr));
Peter Zijlstraf20786f2007-07-19 01:48:56 -07002988 print_lockdep_cache(lock);
2989 printk(") at:\n");
2990 print_ip_sym(ip);
2991 printk("but there are no locks held!\n");
2992 printk("\nother info that might help us debug this:\n");
2993 lockdep_print_held_locks(curr);
2994
2995 printk("\nstack backtrace:\n");
2996 dump_stack();
2997
2998 return 0;
2999}
3000
3001static void
3002__lock_contended(struct lockdep_map *lock, unsigned long ip)
3003{
3004 struct task_struct *curr = current;
3005 struct held_lock *hlock, *prev_hlock;
3006 struct lock_class_stats *stats;
3007 unsigned int depth;
Peter Zijlstrac7e78cf2008-10-16 23:17:09 +02003008 int i, contention_point, contending_point;
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003009
3010 depth = curr->lockdep_depth;
3011 if (DEBUG_LOCKS_WARN_ON(!depth))
3012 return;
3013
3014 prev_hlock = NULL;
3015 for (i = depth-1; i >= 0; i--) {
3016 hlock = curr->held_locks + i;
3017 /*
3018 * We must not cross into another context:
3019 */
3020 if (prev_hlock && prev_hlock->irq_context != hlock->irq_context)
3021 break;
3022 if (hlock->instance == lock)
3023 goto found_it;
3024 prev_hlock = hlock;
3025 }
3026 print_lock_contention_bug(curr, lock, ip);
3027 return;
3028
3029found_it:
3030 hlock->waittime_stamp = sched_clock();
3031
Peter Zijlstrac7e78cf2008-10-16 23:17:09 +02003032 contention_point = lock_point(hlock_class(hlock)->contention_point, ip);
3033 contending_point = lock_point(hlock_class(hlock)->contending_point,
3034 lock->ip);
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003035
Dave Jonesf82b2172008-08-11 09:30:23 +02003036 stats = get_lock_stats(hlock_class(hlock));
Peter Zijlstrac7e78cf2008-10-16 23:17:09 +02003037 if (contention_point < LOCKSTAT_POINTS)
3038 stats->contention_point[contention_point]++;
3039 if (contending_point < LOCKSTAT_POINTS)
3040 stats->contending_point[contending_point]++;
Peter Zijlstra96645672007-07-19 01:49:00 -07003041 if (lock->cpu != smp_processor_id())
3042 stats->bounces[bounce_contended + !!hlock->read]++;
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003043 put_lock_stats(stats);
3044}
3045
3046static void
Peter Zijlstrac7e78cf2008-10-16 23:17:09 +02003047__lock_acquired(struct lockdep_map *lock, unsigned long ip)
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003048{
3049 struct task_struct *curr = current;
3050 struct held_lock *hlock, *prev_hlock;
3051 struct lock_class_stats *stats;
3052 unsigned int depth;
3053 u64 now;
Peter Zijlstra96645672007-07-19 01:49:00 -07003054 s64 waittime = 0;
3055 int i, cpu;
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003056
3057 depth = curr->lockdep_depth;
3058 if (DEBUG_LOCKS_WARN_ON(!depth))
3059 return;
3060
3061 prev_hlock = NULL;
3062 for (i = depth-1; i >= 0; i--) {
3063 hlock = curr->held_locks + i;
3064 /*
3065 * We must not cross into another context:
3066 */
3067 if (prev_hlock && prev_hlock->irq_context != hlock->irq_context)
3068 break;
3069 if (hlock->instance == lock)
3070 goto found_it;
3071 prev_hlock = hlock;
3072 }
3073 print_lock_contention_bug(curr, lock, _RET_IP_);
3074 return;
3075
3076found_it:
Peter Zijlstra96645672007-07-19 01:49:00 -07003077 cpu = smp_processor_id();
3078 if (hlock->waittime_stamp) {
3079 now = sched_clock();
3080 waittime = now - hlock->waittime_stamp;
3081 hlock->holdtime_stamp = now;
3082 }
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003083
Dave Jonesf82b2172008-08-11 09:30:23 +02003084 stats = get_lock_stats(hlock_class(hlock));
Peter Zijlstra96645672007-07-19 01:49:00 -07003085 if (waittime) {
3086 if (hlock->read)
3087 lock_time_inc(&stats->read_waittime, waittime);
3088 else
3089 lock_time_inc(&stats->write_waittime, waittime);
3090 }
3091 if (lock->cpu != cpu)
3092 stats->bounces[bounce_acquired + !!hlock->read]++;
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003093 put_lock_stats(stats);
Peter Zijlstra96645672007-07-19 01:49:00 -07003094
3095 lock->cpu = cpu;
Peter Zijlstrac7e78cf2008-10-16 23:17:09 +02003096 lock->ip = ip;
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003097}
3098
3099void lock_contended(struct lockdep_map *lock, unsigned long ip)
3100{
3101 unsigned long flags;
3102
3103 if (unlikely(!lock_stat))
3104 return;
3105
3106 if (unlikely(current->lockdep_recursion))
3107 return;
3108
3109 raw_local_irq_save(flags);
3110 check_flags(flags);
3111 current->lockdep_recursion = 1;
3112 __lock_contended(lock, ip);
3113 current->lockdep_recursion = 0;
3114 raw_local_irq_restore(flags);
3115}
3116EXPORT_SYMBOL_GPL(lock_contended);
3117
Peter Zijlstrac7e78cf2008-10-16 23:17:09 +02003118void lock_acquired(struct lockdep_map *lock, unsigned long ip)
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003119{
3120 unsigned long flags;
3121
3122 if (unlikely(!lock_stat))
3123 return;
3124
3125 if (unlikely(current->lockdep_recursion))
3126 return;
3127
3128 raw_local_irq_save(flags);
3129 check_flags(flags);
3130 current->lockdep_recursion = 1;
Peter Zijlstrac7e78cf2008-10-16 23:17:09 +02003131 __lock_acquired(lock, ip);
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003132 current->lockdep_recursion = 0;
3133 raw_local_irq_restore(flags);
3134}
3135EXPORT_SYMBOL_GPL(lock_acquired);
3136#endif
3137
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003138/*
3139 * Used by the testsuite, sanitize the validator state
3140 * after a simulated failure:
3141 */
3142
3143void lockdep_reset(void)
3144{
3145 unsigned long flags;
Ingo Molnar23d95a02006-12-13 00:34:40 -08003146 int i;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003147
3148 raw_local_irq_save(flags);
3149 current->curr_chain_key = 0;
3150 current->lockdep_depth = 0;
3151 current->lockdep_recursion = 0;
3152 memset(current->held_locks, 0, MAX_LOCK_DEPTH*sizeof(struct held_lock));
3153 nr_hardirq_chains = 0;
3154 nr_softirq_chains = 0;
3155 nr_process_chains = 0;
3156 debug_locks = 1;
Ingo Molnar23d95a02006-12-13 00:34:40 -08003157 for (i = 0; i < CHAINHASH_SIZE; i++)
3158 INIT_LIST_HEAD(chainhash_table + i);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003159 raw_local_irq_restore(flags);
3160}
3161
3162static void zap_class(struct lock_class *class)
3163{
3164 int i;
3165
3166 /*
3167 * Remove all dependencies this lock is
3168 * involved in:
3169 */
3170 for (i = 0; i < nr_list_entries; i++) {
3171 if (list_entries[i].class == class)
3172 list_del_rcu(&list_entries[i].entry);
3173 }
3174 /*
3175 * Unhash the class and remove it from the all_lock_classes list:
3176 */
3177 list_del_rcu(&class->hash_entry);
3178 list_del_rcu(&class->lock_entry);
3179
Rabin Vincent8bfe0292008-08-11 09:30:26 +02003180 class->key = NULL;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003181}
3182
Arjan van de Venfabe8742008-01-24 07:00:45 +01003183static inline int within(const void *addr, void *start, unsigned long size)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003184{
3185 return addr >= start && addr < start + size;
3186}
3187
3188void lockdep_free_key_range(void *start, unsigned long size)
3189{
3190 struct lock_class *class, *next;
3191 struct list_head *head;
3192 unsigned long flags;
3193 int i;
Nick Piggin5a26db52008-01-16 09:51:58 +01003194 int locked;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003195
3196 raw_local_irq_save(flags);
Nick Piggin5a26db52008-01-16 09:51:58 +01003197 locked = graph_lock();
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003198
3199 /*
3200 * Unhash all classes that were created by this module:
3201 */
3202 for (i = 0; i < CLASSHASH_SIZE; i++) {
3203 head = classhash_table + i;
3204 if (list_empty(head))
3205 continue;
Arjan van de Venfabe8742008-01-24 07:00:45 +01003206 list_for_each_entry_safe(class, next, head, hash_entry) {
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003207 if (within(class->key, start, size))
3208 zap_class(class);
Arjan van de Venfabe8742008-01-24 07:00:45 +01003209 else if (within(class->name, start, size))
3210 zap_class(class);
3211 }
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003212 }
3213
Nick Piggin5a26db52008-01-16 09:51:58 +01003214 if (locked)
3215 graph_unlock();
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003216 raw_local_irq_restore(flags);
3217}
3218
3219void lockdep_reset_lock(struct lockdep_map *lock)
3220{
Ingo Molnard6d897c2006-07-10 04:44:04 -07003221 struct lock_class *class, *next;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003222 struct list_head *head;
3223 unsigned long flags;
3224 int i, j;
Nick Piggin5a26db52008-01-16 09:51:58 +01003225 int locked;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003226
3227 raw_local_irq_save(flags);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003228
3229 /*
Ingo Molnard6d897c2006-07-10 04:44:04 -07003230 * Remove all classes this lock might have:
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003231 */
Ingo Molnard6d897c2006-07-10 04:44:04 -07003232 for (j = 0; j < MAX_LOCKDEP_SUBCLASSES; j++) {
3233 /*
3234 * If the class exists we look it up and zap it:
3235 */
3236 class = look_up_lock_class(lock, j);
3237 if (class)
3238 zap_class(class);
3239 }
3240 /*
3241 * Debug check: in the end all mapped classes should
3242 * be gone.
3243 */
Nick Piggin5a26db52008-01-16 09:51:58 +01003244 locked = graph_lock();
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003245 for (i = 0; i < CLASSHASH_SIZE; i++) {
3246 head = classhash_table + i;
3247 if (list_empty(head))
3248 continue;
3249 list_for_each_entry_safe(class, next, head, hash_entry) {
Ingo Molnard6d897c2006-07-10 04:44:04 -07003250 if (unlikely(class == lock->class_cache)) {
Ingo Molnar74c383f2006-12-13 00:34:43 -08003251 if (debug_locks_off_graph_unlock())
3252 WARN_ON(1);
Ingo Molnard6d897c2006-07-10 04:44:04 -07003253 goto out_restore;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003254 }
3255 }
3256 }
Nick Piggin5a26db52008-01-16 09:51:58 +01003257 if (locked)
3258 graph_unlock();
Ingo Molnard6d897c2006-07-10 04:44:04 -07003259
3260out_restore:
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003261 raw_local_irq_restore(flags);
3262}
3263
Sam Ravnborg14999932007-02-28 20:12:31 -08003264void lockdep_init(void)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003265{
3266 int i;
3267
3268 /*
3269 * Some architectures have their own start_kernel()
3270 * code which calls lockdep_init(), while we also
3271 * call lockdep_init() from the start_kernel() itself,
3272 * and we want to initialize the hashes only once:
3273 */
3274 if (lockdep_initialized)
3275 return;
3276
3277 for (i = 0; i < CLASSHASH_SIZE; i++)
3278 INIT_LIST_HEAD(classhash_table + i);
3279
3280 for (i = 0; i < CHAINHASH_SIZE; i++)
3281 INIT_LIST_HEAD(chainhash_table + i);
3282
3283 lockdep_initialized = 1;
3284}
3285
3286void __init lockdep_info(void)
3287{
3288 printk("Lock dependency validator: Copyright (c) 2006 Red Hat, Inc., Ingo Molnar\n");
3289
3290 printk("... MAX_LOCKDEP_SUBCLASSES: %lu\n", MAX_LOCKDEP_SUBCLASSES);
3291 printk("... MAX_LOCK_DEPTH: %lu\n", MAX_LOCK_DEPTH);
3292 printk("... MAX_LOCKDEP_KEYS: %lu\n", MAX_LOCKDEP_KEYS);
3293 printk("... CLASSHASH_SIZE: %lu\n", CLASSHASH_SIZE);
3294 printk("... MAX_LOCKDEP_ENTRIES: %lu\n", MAX_LOCKDEP_ENTRIES);
3295 printk("... MAX_LOCKDEP_CHAINS: %lu\n", MAX_LOCKDEP_CHAINS);
3296 printk("... CHAINHASH_SIZE: %lu\n", CHAINHASH_SIZE);
3297
3298 printk(" memory used by lock dependency info: %lu kB\n",
3299 (sizeof(struct lock_class) * MAX_LOCKDEP_KEYS +
3300 sizeof(struct list_head) * CLASSHASH_SIZE +
3301 sizeof(struct lock_list) * MAX_LOCKDEP_ENTRIES +
3302 sizeof(struct lock_chain) * MAX_LOCKDEP_CHAINS +
3303 sizeof(struct list_head) * CHAINHASH_SIZE) / 1024);
3304
3305 printk(" per task-struct memory footprint: %lu bytes\n",
3306 sizeof(struct held_lock) * MAX_LOCK_DEPTH);
3307
3308#ifdef CONFIG_DEBUG_LOCKDEP
Johannes Bergc71063c2007-07-19 01:49:02 -07003309 if (lockdep_init_error) {
3310 printk("WARNING: lockdep init error! Arch code didn't call lockdep_init() early enough?\n");
3311 printk("Call stack leading to lockdep invocation was:\n");
3312 print_stack_trace(&lockdep_init_trace, 0);
3313 }
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003314#endif
3315}
3316
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003317static void
3318print_freed_lock_bug(struct task_struct *curr, const void *mem_from,
Arjan van de Ven55794a42006-07-10 04:44:03 -07003319 const void *mem_to, struct held_lock *hlock)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003320{
3321 if (!debug_locks_off())
3322 return;
3323 if (debug_locks_silent)
3324 return;
3325
3326 printk("\n=========================\n");
3327 printk( "[ BUG: held lock freed! ]\n");
3328 printk( "-------------------------\n");
3329 printk("%s/%d is freeing memory %p-%p, with a lock still held there!\n",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07003330 curr->comm, task_pid_nr(curr), mem_from, mem_to-1);
Arjan van de Ven55794a42006-07-10 04:44:03 -07003331 print_lock(hlock);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003332 lockdep_print_held_locks(curr);
3333
3334 printk("\nstack backtrace:\n");
3335 dump_stack();
3336}
3337
Oleg Nesterov54561782007-12-05 15:46:09 +01003338static inline int not_in_range(const void* mem_from, unsigned long mem_len,
3339 const void* lock_from, unsigned long lock_len)
3340{
3341 return lock_from + lock_len <= mem_from ||
3342 mem_from + mem_len <= lock_from;
3343}
3344
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003345/*
3346 * Called when kernel memory is freed (or unmapped), or if a lock
3347 * is destroyed or reinitialized - this code checks whether there is
3348 * any held lock in the memory range of <from> to <to>:
3349 */
3350void debug_check_no_locks_freed(const void *mem_from, unsigned long mem_len)
3351{
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003352 struct task_struct *curr = current;
3353 struct held_lock *hlock;
3354 unsigned long flags;
3355 int i;
3356
3357 if (unlikely(!debug_locks))
3358 return;
3359
3360 local_irq_save(flags);
3361 for (i = 0; i < curr->lockdep_depth; i++) {
3362 hlock = curr->held_locks + i;
3363
Oleg Nesterov54561782007-12-05 15:46:09 +01003364 if (not_in_range(mem_from, mem_len, hlock->instance,
3365 sizeof(*hlock->instance)))
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003366 continue;
3367
Oleg Nesterov54561782007-12-05 15:46:09 +01003368 print_freed_lock_bug(curr, mem_from, mem_from + mem_len, hlock);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003369 break;
3370 }
3371 local_irq_restore(flags);
3372}
Peter Zijlstraed075362006-12-06 20:35:24 -08003373EXPORT_SYMBOL_GPL(debug_check_no_locks_freed);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003374
3375static void print_held_locks_bug(struct task_struct *curr)
3376{
3377 if (!debug_locks_off())
3378 return;
3379 if (debug_locks_silent)
3380 return;
3381
3382 printk("\n=====================================\n");
3383 printk( "[ BUG: lock held at task exit time! ]\n");
3384 printk( "-------------------------------------\n");
3385 printk("%s/%d is exiting with locks still held!\n",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07003386 curr->comm, task_pid_nr(curr));
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003387 lockdep_print_held_locks(curr);
3388
3389 printk("\nstack backtrace:\n");
3390 dump_stack();
3391}
3392
3393void debug_check_no_locks_held(struct task_struct *task)
3394{
3395 if (unlikely(task->lockdep_depth > 0))
3396 print_held_locks_bug(task);
3397}
3398
3399void debug_show_all_locks(void)
3400{
3401 struct task_struct *g, *p;
3402 int count = 10;
3403 int unlock = 1;
3404
Jarek Poplawski9c35dd72007-03-22 00:11:28 -08003405 if (unlikely(!debug_locks)) {
3406 printk("INFO: lockdep is turned off.\n");
3407 return;
3408 }
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003409 printk("\nShowing all locks held in the system:\n");
3410
3411 /*
3412 * Here we try to get the tasklist_lock as hard as possible,
3413 * if not successful after 2 seconds we ignore it (but keep
3414 * trying). This is to enable a debug printout even if a
3415 * tasklist_lock-holding task deadlocks or crashes.
3416 */
3417retry:
3418 if (!read_trylock(&tasklist_lock)) {
3419 if (count == 10)
3420 printk("hm, tasklist_lock locked, retrying... ");
3421 if (count) {
3422 count--;
3423 printk(" #%d", 10-count);
3424 mdelay(200);
3425 goto retry;
3426 }
3427 printk(" ignoring it.\n");
3428 unlock = 0;
3429 }
3430 if (count != 10)
3431 printk(" locked it.\n");
3432
3433 do_each_thread(g, p) {
Ingo Molnar85684872007-12-05 15:46:09 +01003434 /*
3435 * It's not reliable to print a task's held locks
3436 * if it's not sleeping (or if it's not the current
3437 * task):
3438 */
3439 if (p->state == TASK_RUNNING && p != current)
3440 continue;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003441 if (p->lockdep_depth)
3442 lockdep_print_held_locks(p);
3443 if (!unlock)
3444 if (read_trylock(&tasklist_lock))
3445 unlock = 1;
3446 } while_each_thread(g, p);
3447
3448 printk("\n");
3449 printk("=============================================\n\n");
3450
3451 if (unlock)
3452 read_unlock(&tasklist_lock);
3453}
3454
3455EXPORT_SYMBOL_GPL(debug_show_all_locks);
3456
Ingo Molnar82a1fcb2008-01-25 21:08:02 +01003457/*
3458 * Careful: only use this function if you are sure that
3459 * the task cannot run in parallel!
3460 */
3461void __debug_show_held_locks(struct task_struct *task)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003462{
Jarek Poplawski9c35dd72007-03-22 00:11:28 -08003463 if (unlikely(!debug_locks)) {
3464 printk("INFO: lockdep is turned off.\n");
3465 return;
3466 }
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003467 lockdep_print_held_locks(task);
3468}
Ingo Molnar82a1fcb2008-01-25 21:08:02 +01003469EXPORT_SYMBOL_GPL(__debug_show_held_locks);
3470
3471void debug_show_held_locks(struct task_struct *task)
3472{
3473 __debug_show_held_locks(task);
3474}
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003475
3476EXPORT_SYMBOL_GPL(debug_show_held_locks);
Peter Zijlstrab351d162007-10-11 22:11:12 +02003477
3478void lockdep_sys_exit(void)
3479{
3480 struct task_struct *curr = current;
3481
3482 if (unlikely(curr->lockdep_depth)) {
3483 if (!debug_locks_off())
3484 return;
3485 printk("\n================================================\n");
3486 printk( "[ BUG: lock held when returning to user space! ]\n");
3487 printk( "------------------------------------------------\n");
3488 printk("%s/%d is leaving the kernel with locks still held!\n",
3489 curr->comm, curr->pid);
3490 lockdep_print_held_locks(curr);
3491 }
3492}