blob: 06b0c3568f0b230a8c6b669d055c8c442a7eb790 [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 */
Steven Rostedta5e25882008-12-02 15:34:05 -050028#define DISABLE_BRANCH_PROFILING
Ingo Molnarfbb9ce952006-07-03 00:24:50 -070029#include <linux/mutex.h>
30#include <linux/sched.h>
31#include <linux/delay.h>
32#include <linux/module.h>
33#include <linux/proc_fs.h>
34#include <linux/seq_file.h>
35#include <linux/spinlock.h>
36#include <linux/kallsyms.h>
37#include <linux/interrupt.h>
38#include <linux/stacktrace.h>
39#include <linux/debug_locks.h>
40#include <linux/irqflags.h>
Dave Jones99de0552006-09-29 02:00:10 -070041#include <linux/utsname.h>
Peter Zijlstra4b32d0a2007-07-19 01:48:59 -070042#include <linux/hash.h>
Steven Rostedt81d68a92008-05-12 21:20:42 +020043#include <linux/ftrace.h>
Ingo Molnarfbb9ce952006-07-03 00:24:50 -070044
45#include <asm/sections.h>
46
47#include "lockdep_internals.h"
48
Peter Zijlstraf20786f2007-07-19 01:48:56 -070049#ifdef CONFIG_PROVE_LOCKING
50int prove_locking = 1;
51module_param(prove_locking, int, 0644);
52#else
53#define prove_locking 0
54#endif
55
56#ifdef CONFIG_LOCK_STAT
57int lock_stat = 1;
58module_param(lock_stat, int, 0644);
59#else
60#define lock_stat 0
61#endif
62
Ingo Molnarfbb9ce952006-07-03 00:24:50 -070063/*
Ingo Molnar74c383f2006-12-13 00:34:43 -080064 * lockdep_lock: protects the lockdep graph, the hashes and the
65 * class/list/hash allocators.
Ingo Molnarfbb9ce952006-07-03 00:24:50 -070066 *
67 * This is one of the rare exceptions where it's justified
68 * to use a raw spinlock - we really dont want the spinlock
Ingo Molnar74c383f2006-12-13 00:34:43 -080069 * code to recurse back into the lockdep code...
Ingo Molnarfbb9ce952006-07-03 00:24:50 -070070 */
Ingo Molnar74c383f2006-12-13 00:34:43 -080071static raw_spinlock_t lockdep_lock = (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED;
72
73static int graph_lock(void)
74{
75 __raw_spin_lock(&lockdep_lock);
76 /*
77 * Make sure that if another CPU detected a bug while
78 * walking the graph we dont change it (while the other
79 * CPU is busy printing out stuff with the graph lock
80 * dropped already)
81 */
82 if (!debug_locks) {
83 __raw_spin_unlock(&lockdep_lock);
84 return 0;
85 }
Steven Rostedtbb065af2008-05-12 21:21:00 +020086 /* prevent any recursions within lockdep from causing deadlocks */
87 current->lockdep_recursion++;
Ingo Molnar74c383f2006-12-13 00:34:43 -080088 return 1;
89}
90
91static inline int graph_unlock(void)
92{
Jarek Poplawski381a2292007-02-10 01:44:58 -080093 if (debug_locks && !__raw_spin_is_locked(&lockdep_lock))
94 return DEBUG_LOCKS_WARN_ON(1);
95
Steven Rostedtbb065af2008-05-12 21:21:00 +020096 current->lockdep_recursion--;
Ingo Molnar74c383f2006-12-13 00:34:43 -080097 __raw_spin_unlock(&lockdep_lock);
98 return 0;
99}
100
101/*
102 * Turn lock debugging off and return with 0 if it was off already,
103 * and also release the graph lock:
104 */
105static inline int debug_locks_off_graph_unlock(void)
106{
107 int ret = debug_locks_off();
108
109 __raw_spin_unlock(&lockdep_lock);
110
111 return ret;
112}
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700113
114static int lockdep_initialized;
115
116unsigned long nr_list_entries;
117static struct lock_list list_entries[MAX_LOCKDEP_ENTRIES];
118
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700119/*
120 * All data structures here are protected by the global debug_lock.
121 *
122 * Mutex key structs only get allocated, once during bootup, and never
123 * get freed - this significantly simplifies the debugging code.
124 */
125unsigned long nr_lock_classes;
126static struct lock_class lock_classes[MAX_LOCKDEP_KEYS];
127
Dave Jonesf82b2172008-08-11 09:30:23 +0200128static inline struct lock_class *hlock_class(struct held_lock *hlock)
129{
130 if (!hlock->class_idx) {
131 DEBUG_LOCKS_WARN_ON(1);
132 return NULL;
133 }
134 return lock_classes + hlock->class_idx - 1;
135}
136
Peter Zijlstraf20786f2007-07-19 01:48:56 -0700137#ifdef CONFIG_LOCK_STAT
138static DEFINE_PER_CPU(struct lock_class_stats[MAX_LOCKDEP_KEYS], lock_stats);
139
Peter Zijlstrac7e78cf2008-10-16 23:17:09 +0200140static int lock_point(unsigned long points[], unsigned long ip)
Peter Zijlstraf20786f2007-07-19 01:48:56 -0700141{
142 int i;
143
Peter Zijlstrac7e78cf2008-10-16 23:17:09 +0200144 for (i = 0; i < LOCKSTAT_POINTS; i++) {
145 if (points[i] == 0) {
146 points[i] = ip;
Peter Zijlstraf20786f2007-07-19 01:48:56 -0700147 break;
148 }
Peter Zijlstrac7e78cf2008-10-16 23:17:09 +0200149 if (points[i] == ip)
Peter Zijlstraf20786f2007-07-19 01:48:56 -0700150 break;
151 }
152
153 return i;
154}
155
156static void lock_time_inc(struct lock_time *lt, s64 time)
157{
158 if (time > lt->max)
159 lt->max = time;
160
161 if (time < lt->min || !lt->min)
162 lt->min = time;
163
164 lt->total += time;
165 lt->nr++;
166}
167
Peter Zijlstrac46261d2007-07-19 01:48:57 -0700168static inline void lock_time_add(struct lock_time *src, struct lock_time *dst)
169{
170 dst->min += src->min;
171 dst->max += src->max;
172 dst->total += src->total;
173 dst->nr += src->nr;
174}
175
176struct lock_class_stats lock_stats(struct lock_class *class)
177{
178 struct lock_class_stats stats;
179 int cpu, i;
180
181 memset(&stats, 0, sizeof(struct lock_class_stats));
182 for_each_possible_cpu(cpu) {
183 struct lock_class_stats *pcs =
184 &per_cpu(lock_stats, cpu)[class - lock_classes];
185
186 for (i = 0; i < ARRAY_SIZE(stats.contention_point); i++)
187 stats.contention_point[i] += pcs->contention_point[i];
188
Peter Zijlstrac7e78cf2008-10-16 23:17:09 +0200189 for (i = 0; i < ARRAY_SIZE(stats.contending_point); i++)
190 stats.contending_point[i] += pcs->contending_point[i];
191
Peter Zijlstrac46261d2007-07-19 01:48:57 -0700192 lock_time_add(&pcs->read_waittime, &stats.read_waittime);
193 lock_time_add(&pcs->write_waittime, &stats.write_waittime);
194
195 lock_time_add(&pcs->read_holdtime, &stats.read_holdtime);
196 lock_time_add(&pcs->write_holdtime, &stats.write_holdtime);
Peter Zijlstra96645672007-07-19 01:49:00 -0700197
198 for (i = 0; i < ARRAY_SIZE(stats.bounces); i++)
199 stats.bounces[i] += pcs->bounces[i];
Peter Zijlstrac46261d2007-07-19 01:48:57 -0700200 }
201
202 return stats;
203}
204
205void clear_lock_stats(struct lock_class *class)
206{
207 int cpu;
208
209 for_each_possible_cpu(cpu) {
210 struct lock_class_stats *cpu_stats =
211 &per_cpu(lock_stats, cpu)[class - lock_classes];
212
213 memset(cpu_stats, 0, sizeof(struct lock_class_stats));
214 }
215 memset(class->contention_point, 0, sizeof(class->contention_point));
Peter Zijlstrac7e78cf2008-10-16 23:17:09 +0200216 memset(class->contending_point, 0, sizeof(class->contending_point));
Peter Zijlstrac46261d2007-07-19 01:48:57 -0700217}
218
Peter Zijlstraf20786f2007-07-19 01:48:56 -0700219static struct lock_class_stats *get_lock_stats(struct lock_class *class)
220{
221 return &get_cpu_var(lock_stats)[class - lock_classes];
222}
223
224static void put_lock_stats(struct lock_class_stats *stats)
225{
226 put_cpu_var(lock_stats);
227}
228
229static void lock_release_holdtime(struct held_lock *hlock)
230{
231 struct lock_class_stats *stats;
232 s64 holdtime;
233
234 if (!lock_stat)
235 return;
236
237 holdtime = sched_clock() - hlock->holdtime_stamp;
238
Dave Jonesf82b2172008-08-11 09:30:23 +0200239 stats = get_lock_stats(hlock_class(hlock));
Peter Zijlstraf20786f2007-07-19 01:48:56 -0700240 if (hlock->read)
241 lock_time_inc(&stats->read_holdtime, holdtime);
242 else
243 lock_time_inc(&stats->write_holdtime, holdtime);
244 put_lock_stats(stats);
245}
246#else
247static inline void lock_release_holdtime(struct held_lock *hlock)
248{
249}
250#endif
251
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700252/*
253 * We keep a global list of all lock classes. The list only grows,
254 * never shrinks. The list is only accessed with the lockdep
255 * spinlock lock held.
256 */
257LIST_HEAD(all_lock_classes);
258
259/*
260 * The lockdep classes are in a hash-table as well, for fast lookup:
261 */
262#define CLASSHASH_BITS (MAX_LOCKDEP_KEYS_BITS - 1)
263#define CLASSHASH_SIZE (1UL << CLASSHASH_BITS)
Peter Zijlstra4b32d0a2007-07-19 01:48:59 -0700264#define __classhashfn(key) hash_long((unsigned long)key, CLASSHASH_BITS)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700265#define classhashentry(key) (classhash_table + __classhashfn((key)))
266
267static struct list_head classhash_table[CLASSHASH_SIZE];
268
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700269/*
270 * We put the lock dependency chains into a hash-table as well, to cache
271 * their existence:
272 */
273#define CHAINHASH_BITS (MAX_LOCKDEP_CHAINS_BITS-1)
274#define CHAINHASH_SIZE (1UL << CHAINHASH_BITS)
Peter Zijlstra4b32d0a2007-07-19 01:48:59 -0700275#define __chainhashfn(chain) hash_long(chain, CHAINHASH_BITS)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700276#define chainhashentry(chain) (chainhash_table + __chainhashfn((chain)))
277
278static struct list_head chainhash_table[CHAINHASH_SIZE];
279
280/*
281 * The hash key of the lock dependency chains is a hash itself too:
282 * it's a hash of all locks taken up to that lock, including that lock.
283 * It's a 64-bit hash, because it's important for the keys to be
284 * unique.
285 */
286#define iterate_chain_key(key1, key2) \
Ingo Molnar03cbc352006-09-29 02:01:46 -0700287 (((key1) << MAX_LOCKDEP_KEYS_BITS) ^ \
288 ((key1) >> (64-MAX_LOCKDEP_KEYS_BITS)) ^ \
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700289 (key2))
290
Steven Rostedt1d09daa2008-05-12 21:20:55 +0200291void lockdep_off(void)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700292{
293 current->lockdep_recursion++;
294}
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700295EXPORT_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}
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700301EXPORT_SYMBOL(lockdep_on);
302
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700303/*
304 * Debugging switches:
305 */
306
307#define VERBOSE 0
Ingo Molnar33e94e92006-12-13 00:34:41 -0800308#define VERY_VERBOSE 0
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700309
310#if VERBOSE
311# define HARDIRQ_VERBOSE 1
312# define SOFTIRQ_VERBOSE 1
313#else
314# define HARDIRQ_VERBOSE 0
315# define SOFTIRQ_VERBOSE 0
316#endif
317
318#if VERBOSE || HARDIRQ_VERBOSE || SOFTIRQ_VERBOSE
319/*
320 * Quick filtering for interesting events:
321 */
322static int class_filter(struct lock_class *class)
323{
Andi Kleenf9829cc2006-07-10 04:44:01 -0700324#if 0
325 /* Example */
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700326 if (class->name_version == 1 &&
Andi Kleenf9829cc2006-07-10 04:44:01 -0700327 !strcmp(class->name, "lockname"))
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700328 return 1;
329 if (class->name_version == 1 &&
Andi Kleenf9829cc2006-07-10 04:44:01 -0700330 !strcmp(class->name, "&struct->lockfield"))
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700331 return 1;
Andi Kleenf9829cc2006-07-10 04:44:01 -0700332#endif
Ingo Molnara6640892006-12-13 00:34:39 -0800333 /* Filter everything else. 1 would be to allow everything else */
334 return 0;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700335}
336#endif
337
338static int verbose(struct lock_class *class)
339{
340#if VERBOSE
341 return class_filter(class);
342#endif
343 return 0;
344}
345
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700346/*
347 * Stack-trace: tightly packed array of stack backtrace
Ingo Molnar74c383f2006-12-13 00:34:43 -0800348 * addresses. Protected by the graph_lock.
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700349 */
350unsigned long nr_stack_trace_entries;
351static unsigned long stack_trace[MAX_STACK_TRACE_ENTRIES];
352
353static int save_trace(struct stack_trace *trace)
354{
355 trace->nr_entries = 0;
356 trace->max_entries = MAX_STACK_TRACE_ENTRIES - nr_stack_trace_entries;
357 trace->entries = stack_trace + nr_stack_trace_entries;
358
Andi Kleen5a1b3992006-09-26 10:52:34 +0200359 trace->skip = 3;
Andi Kleen5a1b3992006-09-26 10:52:34 +0200360
Christoph Hellwigab1b6f02007-05-08 00:23:29 -0700361 save_stack_trace(trace);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700362
363 trace->max_entries = trace->nr_entries;
364
365 nr_stack_trace_entries += trace->nr_entries;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700366
367 if (nr_stack_trace_entries == MAX_STACK_TRACE_ENTRIES) {
Ingo Molnar74c383f2006-12-13 00:34:43 -0800368 if (!debug_locks_off_graph_unlock())
369 return 0;
370
371 printk("BUG: MAX_STACK_TRACE_ENTRIES too low!\n");
372 printk("turning off the locking correctness validator.\n");
373 dump_stack();
374
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700375 return 0;
376 }
377
378 return 1;
379}
380
381unsigned int nr_hardirq_chains;
382unsigned int nr_softirq_chains;
383unsigned int nr_process_chains;
384unsigned int max_lockdep_depth;
385unsigned int max_recursion_depth;
386
David Miller419ca3f2008-07-29 21:45:03 -0700387static unsigned int lockdep_dependency_gen_id;
388
389static bool lockdep_dependency_visit(struct lock_class *source,
390 unsigned int depth)
391{
392 if (!depth)
393 lockdep_dependency_gen_id++;
394 if (source->dep_gen_id == lockdep_dependency_gen_id)
395 return true;
396 source->dep_gen_id = lockdep_dependency_gen_id;
397 return false;
398}
399
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700400#ifdef CONFIG_DEBUG_LOCKDEP
401/*
402 * We cannot printk in early bootup code. Not even early_printk()
403 * might work. So we mark any initialization errors and printk
404 * about it later on, in lockdep_info().
405 */
406static int lockdep_init_error;
Johannes Bergc71063c2007-07-19 01:49:02 -0700407static unsigned long lockdep_init_trace_data[20];
408static struct stack_trace lockdep_init_trace = {
409 .max_entries = ARRAY_SIZE(lockdep_init_trace_data),
410 .entries = lockdep_init_trace_data,
411};
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700412
413/*
414 * Various lockdep statistics:
415 */
416atomic_t chain_lookup_hits;
417atomic_t chain_lookup_misses;
418atomic_t hardirqs_on_events;
419atomic_t hardirqs_off_events;
420atomic_t redundant_hardirqs_on;
421atomic_t redundant_hardirqs_off;
422atomic_t softirqs_on_events;
423atomic_t softirqs_off_events;
424atomic_t redundant_softirqs_on;
425atomic_t redundant_softirqs_off;
426atomic_t nr_unused_locks;
427atomic_t nr_cyclic_checks;
428atomic_t nr_cyclic_check_recursions;
429atomic_t nr_find_usage_forwards_checks;
430atomic_t nr_find_usage_forwards_recursions;
431atomic_t nr_find_usage_backwards_checks;
432atomic_t nr_find_usage_backwards_recursions;
433# define debug_atomic_inc(ptr) atomic_inc(ptr)
434# define debug_atomic_dec(ptr) atomic_dec(ptr)
435# define debug_atomic_read(ptr) atomic_read(ptr)
436#else
437# define debug_atomic_inc(ptr) do { } while (0)
438# define debug_atomic_dec(ptr) do { } while (0)
439# define debug_atomic_read(ptr) 0
440#endif
441
442/*
443 * Locking printouts:
444 */
445
446static const char *usage_str[] =
447{
448 [LOCK_USED] = "initial-use ",
449 [LOCK_USED_IN_HARDIRQ] = "in-hardirq-W",
450 [LOCK_USED_IN_SOFTIRQ] = "in-softirq-W",
451 [LOCK_ENABLED_SOFTIRQS] = "softirq-on-W",
452 [LOCK_ENABLED_HARDIRQS] = "hardirq-on-W",
453 [LOCK_USED_IN_HARDIRQ_READ] = "in-hardirq-R",
454 [LOCK_USED_IN_SOFTIRQ_READ] = "in-softirq-R",
455 [LOCK_ENABLED_SOFTIRQS_READ] = "softirq-on-R",
456 [LOCK_ENABLED_HARDIRQS_READ] = "hardirq-on-R",
457};
458
459const char * __get_key_name(struct lockdep_subclass_key *key, char *str)
460{
Alexey Dobriyanffb45122007-05-08 00:28:41 -0700461 return kallsyms_lookup((unsigned long)key, NULL, NULL, NULL, str);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700462}
463
464void
465get_usage_chars(struct lock_class *class, char *c1, char *c2, char *c3, char *c4)
466{
467 *c1 = '.', *c2 = '.', *c3 = '.', *c4 = '.';
468
469 if (class->usage_mask & LOCKF_USED_IN_HARDIRQ)
470 *c1 = '+';
471 else
472 if (class->usage_mask & LOCKF_ENABLED_HARDIRQS)
473 *c1 = '-';
474
475 if (class->usage_mask & LOCKF_USED_IN_SOFTIRQ)
476 *c2 = '+';
477 else
478 if (class->usage_mask & LOCKF_ENABLED_SOFTIRQS)
479 *c2 = '-';
480
481 if (class->usage_mask & LOCKF_ENABLED_HARDIRQS_READ)
482 *c3 = '-';
483 if (class->usage_mask & LOCKF_USED_IN_HARDIRQ_READ) {
484 *c3 = '+';
485 if (class->usage_mask & LOCKF_ENABLED_HARDIRQS_READ)
486 *c3 = '?';
487 }
488
489 if (class->usage_mask & LOCKF_ENABLED_SOFTIRQS_READ)
490 *c4 = '-';
491 if (class->usage_mask & LOCKF_USED_IN_SOFTIRQ_READ) {
492 *c4 = '+';
493 if (class->usage_mask & LOCKF_ENABLED_SOFTIRQS_READ)
494 *c4 = '?';
495 }
496}
497
498static void print_lock_name(struct lock_class *class)
499{
Tejun Heo9281ace2007-07-17 04:03:51 -0700500 char str[KSYM_NAME_LEN], c1, c2, c3, c4;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700501 const char *name;
502
503 get_usage_chars(class, &c1, &c2, &c3, &c4);
504
505 name = class->name;
506 if (!name) {
507 name = __get_key_name(class->key, str);
508 printk(" (%s", name);
509 } else {
510 printk(" (%s", name);
511 if (class->name_version > 1)
512 printk("#%d", class->name_version);
513 if (class->subclass)
514 printk("/%d", class->subclass);
515 }
516 printk("){%c%c%c%c}", c1, c2, c3, c4);
517}
518
519static void print_lockdep_cache(struct lockdep_map *lock)
520{
521 const char *name;
Tejun Heo9281ace2007-07-17 04:03:51 -0700522 char str[KSYM_NAME_LEN];
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700523
524 name = lock->name;
525 if (!name)
526 name = __get_key_name(lock->key->subkeys, str);
527
528 printk("%s", name);
529}
530
531static void print_lock(struct held_lock *hlock)
532{
Dave Jonesf82b2172008-08-11 09:30:23 +0200533 print_lock_name(hlock_class(hlock));
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700534 printk(", at: ");
535 print_ip_sym(hlock->acquire_ip);
536}
537
538static void lockdep_print_held_locks(struct task_struct *curr)
539{
540 int i, depth = curr->lockdep_depth;
541
542 if (!depth) {
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -0700543 printk("no locks held by %s/%d.\n", curr->comm, task_pid_nr(curr));
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700544 return;
545 }
546 printk("%d lock%s held by %s/%d:\n",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -0700547 depth, depth > 1 ? "s" : "", curr->comm, task_pid_nr(curr));
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700548
549 for (i = 0; i < depth; i++) {
550 printk(" #%d: ", i);
551 print_lock(curr->held_locks + i);
552 }
553}
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700554
555static void print_lock_class_header(struct lock_class *class, int depth)
556{
557 int bit;
558
Andi Kleenf9829cc2006-07-10 04:44:01 -0700559 printk("%*s->", depth, "");
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700560 print_lock_name(class);
561 printk(" ops: %lu", class->ops);
562 printk(" {\n");
563
564 for (bit = 0; bit < LOCK_USAGE_STATES; bit++) {
565 if (class->usage_mask & (1 << bit)) {
566 int len = depth;
567
Andi Kleenf9829cc2006-07-10 04:44:01 -0700568 len += printk("%*s %s", depth, "", usage_str[bit]);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700569 len += printk(" at:\n");
570 print_stack_trace(class->usage_traces + bit, len);
571 }
572 }
Andi Kleenf9829cc2006-07-10 04:44:01 -0700573 printk("%*s }\n", depth, "");
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700574
Andi Kleenf9829cc2006-07-10 04:44:01 -0700575 printk("%*s ... key at: ",depth,"");
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700576 print_ip_sym((unsigned long)class->key);
577}
578
579/*
580 * printk all lock dependencies starting at <entry>:
581 */
Ingo Molnar7807faf2008-11-25 08:44:24 +0100582static void __used
583print_lock_dependencies(struct lock_class *class, int depth)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700584{
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 */
Heiko Carstens6afe40b2008-10-28 11:14:58 +01002176void trace_hardirqs_on_caller(unsigned long ip)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002177{
2178 struct task_struct *curr = current;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002179
Heiko Carstens6afe40b2008-10-28 11:14:58 +01002180 time_hardirqs_on(CALLER_ADDR0, ip);
Steven Rostedt81d68a92008-05-12 21:20:42 +02002181
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002182 if (unlikely(!debug_locks || current->lockdep_recursion))
2183 return;
2184
2185 if (DEBUG_LOCKS_WARN_ON(unlikely(!early_boot_irqs_enabled)))
2186 return;
2187
2188 if (unlikely(curr->hardirqs_enabled)) {
2189 debug_atomic_inc(&redundant_hardirqs_on);
2190 return;
2191 }
2192 /* we'll do an OFF -> ON transition: */
2193 curr->hardirqs_enabled = 1;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002194
2195 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2196 return;
2197 if (DEBUG_LOCKS_WARN_ON(current->hardirq_context))
2198 return;
2199 /*
2200 * We are going to turn hardirqs on, so set the
2201 * usage bit for all held locks:
2202 */
Jarek Poplawski4ff773bb2007-05-08 00:31:00 -07002203 if (!mark_held_locks(curr, 1))
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002204 return;
2205 /*
2206 * If we have softirqs enabled, then set the usage
2207 * bit for all held locks. (disabled hardirqs prevented
2208 * this bit from being set before)
2209 */
2210 if (curr->softirqs_enabled)
Jarek Poplawski4ff773bb2007-05-08 00:31:00 -07002211 if (!mark_held_locks(curr, 0))
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002212 return;
2213
2214 curr->hardirq_enable_ip = ip;
2215 curr->hardirq_enable_event = ++curr->irq_events;
2216 debug_atomic_inc(&hardirqs_on_events);
2217}
Steven Rostedt81d68a92008-05-12 21:20:42 +02002218EXPORT_SYMBOL(trace_hardirqs_on_caller);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002219
Steven Rostedt1d09daa2008-05-12 21:20:55 +02002220void trace_hardirqs_on(void)
Steven Rostedt81d68a92008-05-12 21:20:42 +02002221{
2222 trace_hardirqs_on_caller(CALLER_ADDR0);
2223}
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002224EXPORT_SYMBOL(trace_hardirqs_on);
2225
2226/*
2227 * Hardirqs were disabled:
2228 */
Heiko Carstens6afe40b2008-10-28 11:14:58 +01002229void trace_hardirqs_off_caller(unsigned long ip)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002230{
2231 struct task_struct *curr = current;
2232
Heiko Carstens6afe40b2008-10-28 11:14:58 +01002233 time_hardirqs_off(CALLER_ADDR0, ip);
Steven Rostedt81d68a92008-05-12 21:20:42 +02002234
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002235 if (unlikely(!debug_locks || current->lockdep_recursion))
2236 return;
2237
2238 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2239 return;
2240
2241 if (curr->hardirqs_enabled) {
2242 /*
2243 * We have done an ON -> OFF transition:
2244 */
2245 curr->hardirqs_enabled = 0;
Heiko Carstens6afe40b2008-10-28 11:14:58 +01002246 curr->hardirq_disable_ip = ip;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002247 curr->hardirq_disable_event = ++curr->irq_events;
2248 debug_atomic_inc(&hardirqs_off_events);
2249 } else
2250 debug_atomic_inc(&redundant_hardirqs_off);
2251}
Steven Rostedt81d68a92008-05-12 21:20:42 +02002252EXPORT_SYMBOL(trace_hardirqs_off_caller);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002253
Steven Rostedt1d09daa2008-05-12 21:20:55 +02002254void trace_hardirqs_off(void)
Steven Rostedt81d68a92008-05-12 21:20:42 +02002255{
2256 trace_hardirqs_off_caller(CALLER_ADDR0);
2257}
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002258EXPORT_SYMBOL(trace_hardirqs_off);
2259
2260/*
2261 * Softirqs will be enabled:
2262 */
2263void trace_softirqs_on(unsigned long ip)
2264{
2265 struct task_struct *curr = current;
2266
2267 if (unlikely(!debug_locks))
2268 return;
2269
2270 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2271 return;
2272
2273 if (curr->softirqs_enabled) {
2274 debug_atomic_inc(&redundant_softirqs_on);
2275 return;
2276 }
2277
2278 /*
2279 * We'll do an OFF -> ON transition:
2280 */
2281 curr->softirqs_enabled = 1;
2282 curr->softirq_enable_ip = ip;
2283 curr->softirq_enable_event = ++curr->irq_events;
2284 debug_atomic_inc(&softirqs_on_events);
2285 /*
2286 * We are going to turn softirqs on, so set the
2287 * usage bit for all held locks, if hardirqs are
2288 * enabled too:
2289 */
2290 if (curr->hardirqs_enabled)
Jarek Poplawski4ff773bb2007-05-08 00:31:00 -07002291 mark_held_locks(curr, 0);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002292}
2293
2294/*
2295 * Softirqs were disabled:
2296 */
2297void trace_softirqs_off(unsigned long ip)
2298{
2299 struct task_struct *curr = current;
2300
2301 if (unlikely(!debug_locks))
2302 return;
2303
2304 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2305 return;
2306
2307 if (curr->softirqs_enabled) {
2308 /*
2309 * We have done an ON -> OFF transition:
2310 */
2311 curr->softirqs_enabled = 0;
2312 curr->softirq_disable_ip = ip;
2313 curr->softirq_disable_event = ++curr->irq_events;
2314 debug_atomic_inc(&softirqs_off_events);
2315 DEBUG_LOCKS_WARN_ON(!softirq_count());
2316 } else
2317 debug_atomic_inc(&redundant_softirqs_off);
2318}
2319
Peter Zijlstra8e182572007-07-19 01:48:54 -07002320static int mark_irqflags(struct task_struct *curr, struct held_lock *hlock)
2321{
2322 /*
2323 * If non-trylock use in a hardirq or softirq context, then
2324 * mark the lock as used in these contexts:
2325 */
2326 if (!hlock->trylock) {
2327 if (hlock->read) {
2328 if (curr->hardirq_context)
2329 if (!mark_lock(curr, hlock,
2330 LOCK_USED_IN_HARDIRQ_READ))
2331 return 0;
2332 if (curr->softirq_context)
2333 if (!mark_lock(curr, hlock,
2334 LOCK_USED_IN_SOFTIRQ_READ))
2335 return 0;
2336 } else {
2337 if (curr->hardirq_context)
2338 if (!mark_lock(curr, hlock, LOCK_USED_IN_HARDIRQ))
2339 return 0;
2340 if (curr->softirq_context)
2341 if (!mark_lock(curr, hlock, LOCK_USED_IN_SOFTIRQ))
2342 return 0;
2343 }
2344 }
2345 if (!hlock->hardirqs_off) {
2346 if (hlock->read) {
2347 if (!mark_lock(curr, hlock,
2348 LOCK_ENABLED_HARDIRQS_READ))
2349 return 0;
2350 if (curr->softirqs_enabled)
2351 if (!mark_lock(curr, hlock,
2352 LOCK_ENABLED_SOFTIRQS_READ))
2353 return 0;
2354 } else {
2355 if (!mark_lock(curr, hlock,
2356 LOCK_ENABLED_HARDIRQS))
2357 return 0;
2358 if (curr->softirqs_enabled)
2359 if (!mark_lock(curr, hlock,
2360 LOCK_ENABLED_SOFTIRQS))
2361 return 0;
2362 }
2363 }
2364
2365 return 1;
2366}
2367
2368static int separate_irq_context(struct task_struct *curr,
2369 struct held_lock *hlock)
2370{
2371 unsigned int depth = curr->lockdep_depth;
2372
2373 /*
2374 * Keep track of points where we cross into an interrupt context:
2375 */
2376 hlock->irq_context = 2*(curr->hardirq_context ? 1 : 0) +
2377 curr->softirq_context;
2378 if (depth) {
2379 struct held_lock *prev_hlock;
2380
2381 prev_hlock = curr->held_locks + depth-1;
2382 /*
2383 * If we cross into another context, reset the
2384 * hash key (this also prevents the checking and the
2385 * adding of the dependency to 'prev'):
2386 */
2387 if (prev_hlock->irq_context != hlock->irq_context)
2388 return 1;
2389 }
2390 return 0;
2391}
2392
2393#else
2394
2395static inline
2396int mark_lock_irq(struct task_struct *curr, struct held_lock *this,
2397 enum lock_usage_bit new_bit)
2398{
2399 WARN_ON(1);
2400 return 1;
2401}
2402
2403static inline int mark_irqflags(struct task_struct *curr,
2404 struct held_lock *hlock)
2405{
2406 return 1;
2407}
2408
2409static inline int separate_irq_context(struct task_struct *curr,
2410 struct held_lock *hlock)
2411{
2412 return 0;
2413}
2414
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002415#endif
2416
2417/*
Peter Zijlstra8e182572007-07-19 01:48:54 -07002418 * Mark a lock with a usage bit, and validate the state transition:
2419 */
Steven Rostedt1d09daa2008-05-12 21:20:55 +02002420static int mark_lock(struct task_struct *curr, struct held_lock *this,
Steven Rostedt0764d232008-05-12 21:20:44 +02002421 enum lock_usage_bit new_bit)
Peter Zijlstra8e182572007-07-19 01:48:54 -07002422{
2423 unsigned int new_mask = 1 << new_bit, ret = 1;
2424
2425 /*
2426 * If already set then do not dirty the cacheline,
2427 * nor do any checks:
2428 */
Dave Jonesf82b2172008-08-11 09:30:23 +02002429 if (likely(hlock_class(this)->usage_mask & new_mask))
Peter Zijlstra8e182572007-07-19 01:48:54 -07002430 return 1;
2431
2432 if (!graph_lock())
2433 return 0;
2434 /*
2435 * Make sure we didnt race:
2436 */
Dave Jonesf82b2172008-08-11 09:30:23 +02002437 if (unlikely(hlock_class(this)->usage_mask & new_mask)) {
Peter Zijlstra8e182572007-07-19 01:48:54 -07002438 graph_unlock();
2439 return 1;
2440 }
2441
Dave Jonesf82b2172008-08-11 09:30:23 +02002442 hlock_class(this)->usage_mask |= new_mask;
Peter Zijlstra8e182572007-07-19 01:48:54 -07002443
Dave Jonesf82b2172008-08-11 09:30:23 +02002444 if (!save_trace(hlock_class(this)->usage_traces + new_bit))
Peter Zijlstra8e182572007-07-19 01:48:54 -07002445 return 0;
2446
2447 switch (new_bit) {
2448 case LOCK_USED_IN_HARDIRQ:
2449 case LOCK_USED_IN_SOFTIRQ:
2450 case LOCK_USED_IN_HARDIRQ_READ:
2451 case LOCK_USED_IN_SOFTIRQ_READ:
2452 case LOCK_ENABLED_HARDIRQS:
2453 case LOCK_ENABLED_SOFTIRQS:
2454 case LOCK_ENABLED_HARDIRQS_READ:
2455 case LOCK_ENABLED_SOFTIRQS_READ:
2456 ret = mark_lock_irq(curr, this, new_bit);
2457 if (!ret)
2458 return 0;
2459 break;
2460 case LOCK_USED:
Peter Zijlstra8e182572007-07-19 01:48:54 -07002461 debug_atomic_dec(&nr_unused_locks);
2462 break;
2463 default:
2464 if (!debug_locks_off_graph_unlock())
2465 return 0;
2466 WARN_ON(1);
2467 return 0;
2468 }
2469
2470 graph_unlock();
2471
2472 /*
2473 * We must printk outside of the graph_lock:
2474 */
2475 if (ret == 2) {
2476 printk("\nmarked lock as {%s}:\n", usage_str[new_bit]);
2477 print_lock(this);
2478 print_irqtrace_events(curr);
2479 dump_stack();
2480 }
2481
2482 return ret;
2483}
2484
2485/*
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002486 * Initialize a lock instance's lock-class mapping info:
2487 */
2488void lockdep_init_map(struct lockdep_map *lock, const char *name,
Peter Zijlstra4dfbb9d2006-10-11 01:45:14 -04002489 struct lock_class_key *key, int subclass)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002490{
2491 if (unlikely(!debug_locks))
2492 return;
2493
2494 if (DEBUG_LOCKS_WARN_ON(!key))
2495 return;
2496 if (DEBUG_LOCKS_WARN_ON(!name))
2497 return;
2498 /*
2499 * Sanity check, the lock-class key must be persistent:
2500 */
2501 if (!static_obj(key)) {
2502 printk("BUG: key %p not in .data!\n", key);
2503 DEBUG_LOCKS_WARN_ON(1);
2504 return;
2505 }
2506 lock->name = name;
2507 lock->key = key;
Ingo Molnard6d897c2006-07-10 04:44:04 -07002508 lock->class_cache = NULL;
Peter Zijlstra96645672007-07-19 01:49:00 -07002509#ifdef CONFIG_LOCK_STAT
2510 lock->cpu = raw_smp_processor_id();
2511#endif
Peter Zijlstra4dfbb9d2006-10-11 01:45:14 -04002512 if (subclass)
2513 register_lock_class(lock, subclass, 1);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002514}
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002515EXPORT_SYMBOL_GPL(lockdep_init_map);
2516
2517/*
2518 * This gets called for every mutex_lock*()/spin_lock*() operation.
2519 * We maintain the dependency maps and validate the locking attempt:
2520 */
2521static int __lock_acquire(struct lockdep_map *lock, unsigned int subclass,
2522 int trylock, int read, int check, int hardirqs_off,
Peter Zijlstra7531e2f2008-08-11 09:30:24 +02002523 struct lockdep_map *nest_lock, unsigned long ip)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002524{
2525 struct task_struct *curr = current;
Ingo Molnard6d897c2006-07-10 04:44:04 -07002526 struct lock_class *class = NULL;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002527 struct held_lock *hlock;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002528 unsigned int depth, id;
2529 int chain_head = 0;
2530 u64 chain_key;
2531
Peter Zijlstraf20786f2007-07-19 01:48:56 -07002532 if (!prove_locking)
2533 check = 1;
2534
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002535 if (unlikely(!debug_locks))
2536 return 0;
2537
2538 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2539 return 0;
2540
2541 if (unlikely(subclass >= MAX_LOCKDEP_SUBCLASSES)) {
2542 debug_locks_off();
2543 printk("BUG: MAX_LOCKDEP_SUBCLASSES too low!\n");
2544 printk("turning off the locking correctness validator.\n");
2545 return 0;
2546 }
2547
Ingo Molnard6d897c2006-07-10 04:44:04 -07002548 if (!subclass)
2549 class = lock->class_cache;
2550 /*
2551 * Not cached yet or subclass?
2552 */
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002553 if (unlikely(!class)) {
Peter Zijlstra4dfbb9d2006-10-11 01:45:14 -04002554 class = register_lock_class(lock, subclass, 0);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002555 if (!class)
2556 return 0;
2557 }
2558 debug_atomic_inc((atomic_t *)&class->ops);
2559 if (very_verbose(class)) {
2560 printk("\nacquire class [%p] %s", class->key, class->name);
2561 if (class->name_version > 1)
2562 printk("#%d", class->name_version);
2563 printk("\n");
2564 dump_stack();
2565 }
2566
2567 /*
2568 * Add the lock to the list of currently held locks.
2569 * (we dont increase the depth just yet, up until the
2570 * dependency checks are done)
2571 */
2572 depth = curr->lockdep_depth;
2573 if (DEBUG_LOCKS_WARN_ON(depth >= MAX_LOCK_DEPTH))
2574 return 0;
2575
2576 hlock = curr->held_locks + depth;
Dave Jonesf82b2172008-08-11 09:30:23 +02002577 if (DEBUG_LOCKS_WARN_ON(!class))
2578 return 0;
2579 hlock->class_idx = class - lock_classes + 1;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002580 hlock->acquire_ip = ip;
2581 hlock->instance = lock;
Peter Zijlstra7531e2f2008-08-11 09:30:24 +02002582 hlock->nest_lock = nest_lock;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002583 hlock->trylock = trylock;
2584 hlock->read = read;
2585 hlock->check = check;
Dmitry Baryshkov6951b122008-08-18 04:26:37 +04002586 hlock->hardirqs_off = !!hardirqs_off;
Peter Zijlstraf20786f2007-07-19 01:48:56 -07002587#ifdef CONFIG_LOCK_STAT
2588 hlock->waittime_stamp = 0;
2589 hlock->holdtime_stamp = sched_clock();
2590#endif
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002591
Peter Zijlstra8e182572007-07-19 01:48:54 -07002592 if (check == 2 && !mark_irqflags(curr, hlock))
2593 return 0;
2594
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002595 /* mark it as used: */
Jarek Poplawski4ff773bb2007-05-08 00:31:00 -07002596 if (!mark_lock(curr, hlock, LOCK_USED))
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002597 return 0;
Peter Zijlstra8e182572007-07-19 01:48:54 -07002598
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002599 /*
Gautham R Shenoy17aacfb2007-10-28 20:47:01 +01002600 * Calculate the chain hash: it's the combined hash of all the
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002601 * lock keys along the dependency chain. We save the hash value
2602 * at every step so that we can get the current hash easily
2603 * after unlock. The chain hash is then used to cache dependency
2604 * results.
2605 *
2606 * The 'key ID' is what is the most compact key value to drive
2607 * the hash, not class->key.
2608 */
2609 id = class - lock_classes;
2610 if (DEBUG_LOCKS_WARN_ON(id >= MAX_LOCKDEP_KEYS))
2611 return 0;
2612
2613 chain_key = curr->curr_chain_key;
2614 if (!depth) {
2615 if (DEBUG_LOCKS_WARN_ON(chain_key != 0))
2616 return 0;
2617 chain_head = 1;
2618 }
2619
2620 hlock->prev_chain_key = chain_key;
Peter Zijlstra8e182572007-07-19 01:48:54 -07002621 if (separate_irq_context(curr, hlock)) {
2622 chain_key = 0;
2623 chain_head = 1;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002624 }
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002625 chain_key = iterate_chain_key(chain_key, id);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002626
Gregory Haskins3aa416b2007-10-11 22:11:11 +02002627 if (!validate_chain(curr, lock, hlock, chain_head, chain_key))
Peter Zijlstra8e182572007-07-19 01:48:54 -07002628 return 0;
Jarek Poplawski381a2292007-02-10 01:44:58 -08002629
Gregory Haskins3aa416b2007-10-11 22:11:11 +02002630 curr->curr_chain_key = chain_key;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002631 curr->lockdep_depth++;
2632 check_chain_key(curr);
Jarek Poplawski60e114d2007-02-20 13:58:00 -08002633#ifdef CONFIG_DEBUG_LOCKDEP
2634 if (unlikely(!debug_locks))
2635 return 0;
2636#endif
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002637 if (unlikely(curr->lockdep_depth >= MAX_LOCK_DEPTH)) {
2638 debug_locks_off();
2639 printk("BUG: MAX_LOCK_DEPTH too low!\n");
2640 printk("turning off the locking correctness validator.\n");
2641 return 0;
2642 }
Jarek Poplawski381a2292007-02-10 01:44:58 -08002643
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002644 if (unlikely(curr->lockdep_depth > max_lockdep_depth))
2645 max_lockdep_depth = curr->lockdep_depth;
2646
2647 return 1;
2648}
2649
2650static int
2651print_unlock_inbalance_bug(struct task_struct *curr, struct lockdep_map *lock,
2652 unsigned long ip)
2653{
2654 if (!debug_locks_off())
2655 return 0;
2656 if (debug_locks_silent)
2657 return 0;
2658
2659 printk("\n=====================================\n");
2660 printk( "[ BUG: bad unlock balance detected! ]\n");
2661 printk( "-------------------------------------\n");
2662 printk("%s/%d is trying to release lock (",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07002663 curr->comm, task_pid_nr(curr));
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002664 print_lockdep_cache(lock);
2665 printk(") at:\n");
2666 print_ip_sym(ip);
2667 printk("but there are no more locks to release!\n");
2668 printk("\nother info that might help us debug this:\n");
2669 lockdep_print_held_locks(curr);
2670
2671 printk("\nstack backtrace:\n");
2672 dump_stack();
2673
2674 return 0;
2675}
2676
2677/*
2678 * Common debugging checks for both nested and non-nested unlock:
2679 */
2680static int check_unlock(struct task_struct *curr, struct lockdep_map *lock,
2681 unsigned long ip)
2682{
2683 if (unlikely(!debug_locks))
2684 return 0;
2685 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2686 return 0;
2687
2688 if (curr->lockdep_depth <= 0)
2689 return print_unlock_inbalance_bug(curr, lock, ip);
2690
2691 return 1;
2692}
2693
Peter Zijlstra64aa3482008-08-11 09:30:21 +02002694static int
Peter Zijlstra00ef9f72008-12-04 09:00:17 +01002695__lock_set_class(struct lockdep_map *lock, const char *name,
2696 struct lock_class_key *key, unsigned int subclass,
2697 unsigned long ip)
Peter Zijlstra64aa3482008-08-11 09:30:21 +02002698{
2699 struct task_struct *curr = current;
2700 struct held_lock *hlock, *prev_hlock;
2701 struct lock_class *class;
2702 unsigned int depth;
2703 int i;
2704
2705 depth = curr->lockdep_depth;
2706 if (DEBUG_LOCKS_WARN_ON(!depth))
2707 return 0;
2708
2709 prev_hlock = NULL;
2710 for (i = depth-1; i >= 0; i--) {
2711 hlock = curr->held_locks + i;
2712 /*
2713 * We must not cross into another context:
2714 */
2715 if (prev_hlock && prev_hlock->irq_context != hlock->irq_context)
2716 break;
2717 if (hlock->instance == lock)
2718 goto found_it;
2719 prev_hlock = hlock;
2720 }
2721 return print_unlock_inbalance_bug(curr, lock, ip);
2722
2723found_it:
Peter Zijlstra00ef9f72008-12-04 09:00:17 +01002724 lockdep_init_map(lock, name, key, 0);
Peter Zijlstra64aa3482008-08-11 09:30:21 +02002725 class = register_lock_class(lock, subclass, 0);
Dave Jonesf82b2172008-08-11 09:30:23 +02002726 hlock->class_idx = class - lock_classes + 1;
Peter Zijlstra64aa3482008-08-11 09:30:21 +02002727
2728 curr->lockdep_depth = i;
2729 curr->curr_chain_key = hlock->prev_chain_key;
2730
2731 for (; i < depth; i++) {
2732 hlock = curr->held_locks + i;
2733 if (!__lock_acquire(hlock->instance,
Dave Jonesf82b2172008-08-11 09:30:23 +02002734 hlock_class(hlock)->subclass, hlock->trylock,
Peter Zijlstra64aa3482008-08-11 09:30:21 +02002735 hlock->read, hlock->check, hlock->hardirqs_off,
Peter Zijlstra7531e2f2008-08-11 09:30:24 +02002736 hlock->nest_lock, hlock->acquire_ip))
Peter Zijlstra64aa3482008-08-11 09:30:21 +02002737 return 0;
2738 }
2739
2740 if (DEBUG_LOCKS_WARN_ON(curr->lockdep_depth != depth))
2741 return 0;
2742 return 1;
2743}
2744
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002745/*
2746 * Remove the lock to the list of currently held locks in a
2747 * potentially non-nested (out of order) manner. This is a
2748 * relatively rare operation, as all the unlock APIs default
2749 * to nested mode (which uses lock_release()):
2750 */
2751static int
2752lock_release_non_nested(struct task_struct *curr,
2753 struct lockdep_map *lock, unsigned long ip)
2754{
2755 struct held_lock *hlock, *prev_hlock;
2756 unsigned int depth;
2757 int i;
2758
2759 /*
2760 * Check whether the lock exists in the current stack
2761 * of held locks:
2762 */
2763 depth = curr->lockdep_depth;
2764 if (DEBUG_LOCKS_WARN_ON(!depth))
2765 return 0;
2766
2767 prev_hlock = NULL;
2768 for (i = depth-1; i >= 0; i--) {
2769 hlock = curr->held_locks + i;
2770 /*
2771 * We must not cross into another context:
2772 */
2773 if (prev_hlock && prev_hlock->irq_context != hlock->irq_context)
2774 break;
2775 if (hlock->instance == lock)
2776 goto found_it;
2777 prev_hlock = hlock;
2778 }
2779 return print_unlock_inbalance_bug(curr, lock, ip);
2780
2781found_it:
Peter Zijlstraf20786f2007-07-19 01:48:56 -07002782 lock_release_holdtime(hlock);
2783
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002784 /*
2785 * We have the right lock to unlock, 'hlock' points to it.
2786 * Now we remove it from the stack, and add back the other
2787 * entries (if any), recalculating the hash along the way:
2788 */
2789 curr->lockdep_depth = i;
2790 curr->curr_chain_key = hlock->prev_chain_key;
2791
2792 for (i++; i < depth; i++) {
2793 hlock = curr->held_locks + i;
2794 if (!__lock_acquire(hlock->instance,
Dave Jonesf82b2172008-08-11 09:30:23 +02002795 hlock_class(hlock)->subclass, hlock->trylock,
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002796 hlock->read, hlock->check, hlock->hardirqs_off,
Peter Zijlstra7531e2f2008-08-11 09:30:24 +02002797 hlock->nest_lock, hlock->acquire_ip))
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002798 return 0;
2799 }
2800
2801 if (DEBUG_LOCKS_WARN_ON(curr->lockdep_depth != depth - 1))
2802 return 0;
2803 return 1;
2804}
2805
2806/*
2807 * Remove the lock to the list of currently held locks - this gets
2808 * called on mutex_unlock()/spin_unlock*() (or on a failed
2809 * mutex_lock_interruptible()). This is done for unlocks that nest
2810 * perfectly. (i.e. the current top of the lock-stack is unlocked)
2811 */
2812static int lock_release_nested(struct task_struct *curr,
2813 struct lockdep_map *lock, unsigned long ip)
2814{
2815 struct held_lock *hlock;
2816 unsigned int depth;
2817
2818 /*
2819 * Pop off the top of the lock stack:
2820 */
2821 depth = curr->lockdep_depth - 1;
2822 hlock = curr->held_locks + depth;
2823
2824 /*
2825 * Is the unlock non-nested:
2826 */
2827 if (hlock->instance != lock)
2828 return lock_release_non_nested(curr, lock, ip);
2829 curr->lockdep_depth--;
2830
2831 if (DEBUG_LOCKS_WARN_ON(!depth && (hlock->prev_chain_key != 0)))
2832 return 0;
2833
2834 curr->curr_chain_key = hlock->prev_chain_key;
2835
Peter Zijlstraf20786f2007-07-19 01:48:56 -07002836 lock_release_holdtime(hlock);
2837
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002838#ifdef CONFIG_DEBUG_LOCKDEP
2839 hlock->prev_chain_key = 0;
Dave Jonesf82b2172008-08-11 09:30:23 +02002840 hlock->class_idx = 0;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002841 hlock->acquire_ip = 0;
2842 hlock->irq_context = 0;
2843#endif
2844 return 1;
2845}
2846
2847/*
2848 * Remove the lock to the list of currently held locks - this gets
2849 * called on mutex_unlock()/spin_unlock*() (or on a failed
2850 * mutex_lock_interruptible()). This is done for unlocks that nest
2851 * perfectly. (i.e. the current top of the lock-stack is unlocked)
2852 */
2853static void
2854__lock_release(struct lockdep_map *lock, int nested, unsigned long ip)
2855{
2856 struct task_struct *curr = current;
2857
2858 if (!check_unlock(curr, lock, ip))
2859 return;
2860
2861 if (nested) {
2862 if (!lock_release_nested(curr, lock, ip))
2863 return;
2864 } else {
2865 if (!lock_release_non_nested(curr, lock, ip))
2866 return;
2867 }
2868
2869 check_chain_key(curr);
2870}
2871
2872/*
2873 * Check whether we follow the irq-flags state precisely:
2874 */
Steven Rostedt1d09daa2008-05-12 21:20:55 +02002875static void check_flags(unsigned long flags)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002876{
Ingo Molnar992860e2008-07-14 10:28:38 +02002877#if defined(CONFIG_PROVE_LOCKING) && defined(CONFIG_DEBUG_LOCKDEP) && \
2878 defined(CONFIG_TRACE_IRQFLAGS)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002879 if (!debug_locks)
2880 return;
2881
Ingo Molnar5f9fa8a2007-12-07 19:02:47 +01002882 if (irqs_disabled_flags(flags)) {
2883 if (DEBUG_LOCKS_WARN_ON(current->hardirqs_enabled)) {
2884 printk("possible reason: unannotated irqs-off.\n");
2885 }
2886 } else {
2887 if (DEBUG_LOCKS_WARN_ON(!current->hardirqs_enabled)) {
2888 printk("possible reason: unannotated irqs-on.\n");
2889 }
2890 }
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002891
2892 /*
2893 * We dont accurately track softirq state in e.g.
2894 * hardirq contexts (such as on 4KSTACKS), so only
2895 * check if not in hardirq contexts:
2896 */
2897 if (!hardirq_count()) {
2898 if (softirq_count())
2899 DEBUG_LOCKS_WARN_ON(current->softirqs_enabled);
2900 else
2901 DEBUG_LOCKS_WARN_ON(!current->softirqs_enabled);
2902 }
2903
2904 if (!debug_locks)
2905 print_irqtrace_events(current);
2906#endif
2907}
2908
Peter Zijlstra00ef9f72008-12-04 09:00:17 +01002909void lock_set_class(struct lockdep_map *lock, const char *name,
2910 struct lock_class_key *key, unsigned int subclass,
2911 unsigned long ip)
Peter Zijlstra64aa3482008-08-11 09:30:21 +02002912{
2913 unsigned long flags;
2914
2915 if (unlikely(current->lockdep_recursion))
2916 return;
2917
2918 raw_local_irq_save(flags);
2919 current->lockdep_recursion = 1;
2920 check_flags(flags);
Peter Zijlstra00ef9f72008-12-04 09:00:17 +01002921 if (__lock_set_class(lock, name, key, subclass, ip))
Peter Zijlstra64aa3482008-08-11 09:30:21 +02002922 check_chain_key(current);
2923 current->lockdep_recursion = 0;
2924 raw_local_irq_restore(flags);
2925}
Peter Zijlstra00ef9f72008-12-04 09:00:17 +01002926EXPORT_SYMBOL_GPL(lock_set_class);
Peter Zijlstra64aa3482008-08-11 09:30:21 +02002927
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002928/*
2929 * We are not always called with irqs disabled - do that here,
2930 * and also avoid lockdep recursion:
2931 */
Steven Rostedt1d09daa2008-05-12 21:20:55 +02002932void lock_acquire(struct lockdep_map *lock, unsigned int subclass,
Peter Zijlstra7531e2f2008-08-11 09:30:24 +02002933 int trylock, int read, int check,
2934 struct lockdep_map *nest_lock, unsigned long ip)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002935{
2936 unsigned long flags;
2937
2938 if (unlikely(current->lockdep_recursion))
2939 return;
2940
2941 raw_local_irq_save(flags);
2942 check_flags(flags);
2943
2944 current->lockdep_recursion = 1;
2945 __lock_acquire(lock, subclass, trylock, read, check,
Peter Zijlstra7531e2f2008-08-11 09:30:24 +02002946 irqs_disabled_flags(flags), nest_lock, ip);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002947 current->lockdep_recursion = 0;
2948 raw_local_irq_restore(flags);
2949}
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002950EXPORT_SYMBOL_GPL(lock_acquire);
2951
Steven Rostedt1d09daa2008-05-12 21:20:55 +02002952void lock_release(struct lockdep_map *lock, int nested,
Steven Rostedt0764d232008-05-12 21:20:44 +02002953 unsigned long ip)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002954{
2955 unsigned long flags;
2956
2957 if (unlikely(current->lockdep_recursion))
2958 return;
2959
2960 raw_local_irq_save(flags);
2961 check_flags(flags);
2962 current->lockdep_recursion = 1;
2963 __lock_release(lock, nested, ip);
2964 current->lockdep_recursion = 0;
2965 raw_local_irq_restore(flags);
2966}
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002967EXPORT_SYMBOL_GPL(lock_release);
2968
Peter Zijlstraf20786f2007-07-19 01:48:56 -07002969#ifdef CONFIG_LOCK_STAT
2970static int
2971print_lock_contention_bug(struct task_struct *curr, struct lockdep_map *lock,
2972 unsigned long ip)
2973{
2974 if (!debug_locks_off())
2975 return 0;
2976 if (debug_locks_silent)
2977 return 0;
2978
2979 printk("\n=================================\n");
2980 printk( "[ BUG: bad contention detected! ]\n");
2981 printk( "---------------------------------\n");
2982 printk("%s/%d is trying to contend lock (",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07002983 curr->comm, task_pid_nr(curr));
Peter Zijlstraf20786f2007-07-19 01:48:56 -07002984 print_lockdep_cache(lock);
2985 printk(") at:\n");
2986 print_ip_sym(ip);
2987 printk("but there are no locks held!\n");
2988 printk("\nother info that might help us debug this:\n");
2989 lockdep_print_held_locks(curr);
2990
2991 printk("\nstack backtrace:\n");
2992 dump_stack();
2993
2994 return 0;
2995}
2996
2997static void
2998__lock_contended(struct lockdep_map *lock, unsigned long ip)
2999{
3000 struct task_struct *curr = current;
3001 struct held_lock *hlock, *prev_hlock;
3002 struct lock_class_stats *stats;
3003 unsigned int depth;
Peter Zijlstrac7e78cf2008-10-16 23:17:09 +02003004 int i, contention_point, contending_point;
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003005
3006 depth = curr->lockdep_depth;
3007 if (DEBUG_LOCKS_WARN_ON(!depth))
3008 return;
3009
3010 prev_hlock = NULL;
3011 for (i = depth-1; i >= 0; i--) {
3012 hlock = curr->held_locks + i;
3013 /*
3014 * We must not cross into another context:
3015 */
3016 if (prev_hlock && prev_hlock->irq_context != hlock->irq_context)
3017 break;
3018 if (hlock->instance == lock)
3019 goto found_it;
3020 prev_hlock = hlock;
3021 }
3022 print_lock_contention_bug(curr, lock, ip);
3023 return;
3024
3025found_it:
3026 hlock->waittime_stamp = sched_clock();
3027
Peter Zijlstrac7e78cf2008-10-16 23:17:09 +02003028 contention_point = lock_point(hlock_class(hlock)->contention_point, ip);
3029 contending_point = lock_point(hlock_class(hlock)->contending_point,
3030 lock->ip);
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003031
Dave Jonesf82b2172008-08-11 09:30:23 +02003032 stats = get_lock_stats(hlock_class(hlock));
Peter Zijlstrac7e78cf2008-10-16 23:17:09 +02003033 if (contention_point < LOCKSTAT_POINTS)
3034 stats->contention_point[contention_point]++;
3035 if (contending_point < LOCKSTAT_POINTS)
3036 stats->contending_point[contending_point]++;
Peter Zijlstra96645672007-07-19 01:49:00 -07003037 if (lock->cpu != smp_processor_id())
3038 stats->bounces[bounce_contended + !!hlock->read]++;
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003039 put_lock_stats(stats);
3040}
3041
3042static void
Peter Zijlstrac7e78cf2008-10-16 23:17:09 +02003043__lock_acquired(struct lockdep_map *lock, unsigned long ip)
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003044{
3045 struct task_struct *curr = current;
3046 struct held_lock *hlock, *prev_hlock;
3047 struct lock_class_stats *stats;
3048 unsigned int depth;
3049 u64 now;
Peter Zijlstra96645672007-07-19 01:49:00 -07003050 s64 waittime = 0;
3051 int i, cpu;
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003052
3053 depth = curr->lockdep_depth;
3054 if (DEBUG_LOCKS_WARN_ON(!depth))
3055 return;
3056
3057 prev_hlock = NULL;
3058 for (i = depth-1; i >= 0; i--) {
3059 hlock = curr->held_locks + i;
3060 /*
3061 * We must not cross into another context:
3062 */
3063 if (prev_hlock && prev_hlock->irq_context != hlock->irq_context)
3064 break;
3065 if (hlock->instance == lock)
3066 goto found_it;
3067 prev_hlock = hlock;
3068 }
3069 print_lock_contention_bug(curr, lock, _RET_IP_);
3070 return;
3071
3072found_it:
Peter Zijlstra96645672007-07-19 01:49:00 -07003073 cpu = smp_processor_id();
3074 if (hlock->waittime_stamp) {
3075 now = sched_clock();
3076 waittime = now - hlock->waittime_stamp;
3077 hlock->holdtime_stamp = now;
3078 }
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003079
Dave Jonesf82b2172008-08-11 09:30:23 +02003080 stats = get_lock_stats(hlock_class(hlock));
Peter Zijlstra96645672007-07-19 01:49:00 -07003081 if (waittime) {
3082 if (hlock->read)
3083 lock_time_inc(&stats->read_waittime, waittime);
3084 else
3085 lock_time_inc(&stats->write_waittime, waittime);
3086 }
3087 if (lock->cpu != cpu)
3088 stats->bounces[bounce_acquired + !!hlock->read]++;
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003089 put_lock_stats(stats);
Peter Zijlstra96645672007-07-19 01:49:00 -07003090
3091 lock->cpu = cpu;
Peter Zijlstrac7e78cf2008-10-16 23:17:09 +02003092 lock->ip = ip;
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003093}
3094
3095void lock_contended(struct lockdep_map *lock, unsigned long ip)
3096{
3097 unsigned long flags;
3098
3099 if (unlikely(!lock_stat))
3100 return;
3101
3102 if (unlikely(current->lockdep_recursion))
3103 return;
3104
3105 raw_local_irq_save(flags);
3106 check_flags(flags);
3107 current->lockdep_recursion = 1;
3108 __lock_contended(lock, ip);
3109 current->lockdep_recursion = 0;
3110 raw_local_irq_restore(flags);
3111}
3112EXPORT_SYMBOL_GPL(lock_contended);
3113
Peter Zijlstrac7e78cf2008-10-16 23:17:09 +02003114void lock_acquired(struct lockdep_map *lock, unsigned long ip)
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003115{
3116 unsigned long flags;
3117
3118 if (unlikely(!lock_stat))
3119 return;
3120
3121 if (unlikely(current->lockdep_recursion))
3122 return;
3123
3124 raw_local_irq_save(flags);
3125 check_flags(flags);
3126 current->lockdep_recursion = 1;
Peter Zijlstrac7e78cf2008-10-16 23:17:09 +02003127 __lock_acquired(lock, ip);
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003128 current->lockdep_recursion = 0;
3129 raw_local_irq_restore(flags);
3130}
3131EXPORT_SYMBOL_GPL(lock_acquired);
3132#endif
3133
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003134/*
3135 * Used by the testsuite, sanitize the validator state
3136 * after a simulated failure:
3137 */
3138
3139void lockdep_reset(void)
3140{
3141 unsigned long flags;
Ingo Molnar23d95a02006-12-13 00:34:40 -08003142 int i;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003143
3144 raw_local_irq_save(flags);
3145 current->curr_chain_key = 0;
3146 current->lockdep_depth = 0;
3147 current->lockdep_recursion = 0;
3148 memset(current->held_locks, 0, MAX_LOCK_DEPTH*sizeof(struct held_lock));
3149 nr_hardirq_chains = 0;
3150 nr_softirq_chains = 0;
3151 nr_process_chains = 0;
3152 debug_locks = 1;
Ingo Molnar23d95a02006-12-13 00:34:40 -08003153 for (i = 0; i < CHAINHASH_SIZE; i++)
3154 INIT_LIST_HEAD(chainhash_table + i);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003155 raw_local_irq_restore(flags);
3156}
3157
3158static void zap_class(struct lock_class *class)
3159{
3160 int i;
3161
3162 /*
3163 * Remove all dependencies this lock is
3164 * involved in:
3165 */
3166 for (i = 0; i < nr_list_entries; i++) {
3167 if (list_entries[i].class == class)
3168 list_del_rcu(&list_entries[i].entry);
3169 }
3170 /*
3171 * Unhash the class and remove it from the all_lock_classes list:
3172 */
3173 list_del_rcu(&class->hash_entry);
3174 list_del_rcu(&class->lock_entry);
3175
Rabin Vincent8bfe0292008-08-11 09:30:26 +02003176 class->key = NULL;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003177}
3178
Arjan van de Venfabe8742008-01-24 07:00:45 +01003179static inline int within(const void *addr, void *start, unsigned long size)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003180{
3181 return addr >= start && addr < start + size;
3182}
3183
3184void lockdep_free_key_range(void *start, unsigned long size)
3185{
3186 struct lock_class *class, *next;
3187 struct list_head *head;
3188 unsigned long flags;
3189 int i;
Nick Piggin5a26db52008-01-16 09:51:58 +01003190 int locked;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003191
3192 raw_local_irq_save(flags);
Nick Piggin5a26db52008-01-16 09:51:58 +01003193 locked = graph_lock();
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003194
3195 /*
3196 * Unhash all classes that were created by this module:
3197 */
3198 for (i = 0; i < CLASSHASH_SIZE; i++) {
3199 head = classhash_table + i;
3200 if (list_empty(head))
3201 continue;
Arjan van de Venfabe8742008-01-24 07:00:45 +01003202 list_for_each_entry_safe(class, next, head, hash_entry) {
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003203 if (within(class->key, start, size))
3204 zap_class(class);
Arjan van de Venfabe8742008-01-24 07:00:45 +01003205 else if (within(class->name, start, size))
3206 zap_class(class);
3207 }
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003208 }
3209
Nick Piggin5a26db52008-01-16 09:51:58 +01003210 if (locked)
3211 graph_unlock();
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003212 raw_local_irq_restore(flags);
3213}
3214
3215void lockdep_reset_lock(struct lockdep_map *lock)
3216{
Ingo Molnard6d897c2006-07-10 04:44:04 -07003217 struct lock_class *class, *next;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003218 struct list_head *head;
3219 unsigned long flags;
3220 int i, j;
Nick Piggin5a26db52008-01-16 09:51:58 +01003221 int locked;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003222
3223 raw_local_irq_save(flags);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003224
3225 /*
Ingo Molnard6d897c2006-07-10 04:44:04 -07003226 * Remove all classes this lock might have:
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003227 */
Ingo Molnard6d897c2006-07-10 04:44:04 -07003228 for (j = 0; j < MAX_LOCKDEP_SUBCLASSES; j++) {
3229 /*
3230 * If the class exists we look it up and zap it:
3231 */
3232 class = look_up_lock_class(lock, j);
3233 if (class)
3234 zap_class(class);
3235 }
3236 /*
3237 * Debug check: in the end all mapped classes should
3238 * be gone.
3239 */
Nick Piggin5a26db52008-01-16 09:51:58 +01003240 locked = graph_lock();
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003241 for (i = 0; i < CLASSHASH_SIZE; i++) {
3242 head = classhash_table + i;
3243 if (list_empty(head))
3244 continue;
3245 list_for_each_entry_safe(class, next, head, hash_entry) {
Ingo Molnard6d897c2006-07-10 04:44:04 -07003246 if (unlikely(class == lock->class_cache)) {
Ingo Molnar74c383f2006-12-13 00:34:43 -08003247 if (debug_locks_off_graph_unlock())
3248 WARN_ON(1);
Ingo Molnard6d897c2006-07-10 04:44:04 -07003249 goto out_restore;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003250 }
3251 }
3252 }
Nick Piggin5a26db52008-01-16 09:51:58 +01003253 if (locked)
3254 graph_unlock();
Ingo Molnard6d897c2006-07-10 04:44:04 -07003255
3256out_restore:
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003257 raw_local_irq_restore(flags);
3258}
3259
Sam Ravnborg14999932007-02-28 20:12:31 -08003260void lockdep_init(void)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003261{
3262 int i;
3263
3264 /*
3265 * Some architectures have their own start_kernel()
3266 * code which calls lockdep_init(), while we also
3267 * call lockdep_init() from the start_kernel() itself,
3268 * and we want to initialize the hashes only once:
3269 */
3270 if (lockdep_initialized)
3271 return;
3272
3273 for (i = 0; i < CLASSHASH_SIZE; i++)
3274 INIT_LIST_HEAD(classhash_table + i);
3275
3276 for (i = 0; i < CHAINHASH_SIZE; i++)
3277 INIT_LIST_HEAD(chainhash_table + i);
3278
3279 lockdep_initialized = 1;
3280}
3281
3282void __init lockdep_info(void)
3283{
3284 printk("Lock dependency validator: Copyright (c) 2006 Red Hat, Inc., Ingo Molnar\n");
3285
Li Zefanb0788ca2008-11-21 15:57:32 +08003286 printk("... MAX_LOCKDEP_SUBCLASSES: %lu\n", MAX_LOCKDEP_SUBCLASSES);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003287 printk("... MAX_LOCK_DEPTH: %lu\n", MAX_LOCK_DEPTH);
3288 printk("... MAX_LOCKDEP_KEYS: %lu\n", MAX_LOCKDEP_KEYS);
Li Zefanb0788ca2008-11-21 15:57:32 +08003289 printk("... CLASSHASH_SIZE: %lu\n", CLASSHASH_SIZE);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003290 printk("... MAX_LOCKDEP_ENTRIES: %lu\n", MAX_LOCKDEP_ENTRIES);
3291 printk("... MAX_LOCKDEP_CHAINS: %lu\n", MAX_LOCKDEP_CHAINS);
3292 printk("... CHAINHASH_SIZE: %lu\n", CHAINHASH_SIZE);
3293
3294 printk(" memory used by lock dependency info: %lu kB\n",
3295 (sizeof(struct lock_class) * MAX_LOCKDEP_KEYS +
3296 sizeof(struct list_head) * CLASSHASH_SIZE +
3297 sizeof(struct lock_list) * MAX_LOCKDEP_ENTRIES +
3298 sizeof(struct lock_chain) * MAX_LOCKDEP_CHAINS +
3299 sizeof(struct list_head) * CHAINHASH_SIZE) / 1024);
3300
3301 printk(" per task-struct memory footprint: %lu bytes\n",
3302 sizeof(struct held_lock) * MAX_LOCK_DEPTH);
3303
3304#ifdef CONFIG_DEBUG_LOCKDEP
Johannes Bergc71063c2007-07-19 01:49:02 -07003305 if (lockdep_init_error) {
3306 printk("WARNING: lockdep init error! Arch code didn't call lockdep_init() early enough?\n");
3307 printk("Call stack leading to lockdep invocation was:\n");
3308 print_stack_trace(&lockdep_init_trace, 0);
3309 }
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003310#endif
3311}
3312
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003313static void
3314print_freed_lock_bug(struct task_struct *curr, const void *mem_from,
Arjan van de Ven55794a42006-07-10 04:44:03 -07003315 const void *mem_to, struct held_lock *hlock)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003316{
3317 if (!debug_locks_off())
3318 return;
3319 if (debug_locks_silent)
3320 return;
3321
3322 printk("\n=========================\n");
3323 printk( "[ BUG: held lock freed! ]\n");
3324 printk( "-------------------------\n");
3325 printk("%s/%d is freeing memory %p-%p, with a lock still held there!\n",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07003326 curr->comm, task_pid_nr(curr), mem_from, mem_to-1);
Arjan van de Ven55794a42006-07-10 04:44:03 -07003327 print_lock(hlock);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003328 lockdep_print_held_locks(curr);
3329
3330 printk("\nstack backtrace:\n");
3331 dump_stack();
3332}
3333
Oleg Nesterov54561782007-12-05 15:46:09 +01003334static inline int not_in_range(const void* mem_from, unsigned long mem_len,
3335 const void* lock_from, unsigned long lock_len)
3336{
3337 return lock_from + lock_len <= mem_from ||
3338 mem_from + mem_len <= lock_from;
3339}
3340
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003341/*
3342 * Called when kernel memory is freed (or unmapped), or if a lock
3343 * is destroyed or reinitialized - this code checks whether there is
3344 * any held lock in the memory range of <from> to <to>:
3345 */
3346void debug_check_no_locks_freed(const void *mem_from, unsigned long mem_len)
3347{
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003348 struct task_struct *curr = current;
3349 struct held_lock *hlock;
3350 unsigned long flags;
3351 int i;
3352
3353 if (unlikely(!debug_locks))
3354 return;
3355
3356 local_irq_save(flags);
3357 for (i = 0; i < curr->lockdep_depth; i++) {
3358 hlock = curr->held_locks + i;
3359
Oleg Nesterov54561782007-12-05 15:46:09 +01003360 if (not_in_range(mem_from, mem_len, hlock->instance,
3361 sizeof(*hlock->instance)))
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003362 continue;
3363
Oleg Nesterov54561782007-12-05 15:46:09 +01003364 print_freed_lock_bug(curr, mem_from, mem_from + mem_len, hlock);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003365 break;
3366 }
3367 local_irq_restore(flags);
3368}
Peter Zijlstraed075362006-12-06 20:35:24 -08003369EXPORT_SYMBOL_GPL(debug_check_no_locks_freed);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003370
3371static void print_held_locks_bug(struct task_struct *curr)
3372{
3373 if (!debug_locks_off())
3374 return;
3375 if (debug_locks_silent)
3376 return;
3377
3378 printk("\n=====================================\n");
3379 printk( "[ BUG: lock held at task exit time! ]\n");
3380 printk( "-------------------------------------\n");
3381 printk("%s/%d is exiting with locks still held!\n",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07003382 curr->comm, task_pid_nr(curr));
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003383 lockdep_print_held_locks(curr);
3384
3385 printk("\nstack backtrace:\n");
3386 dump_stack();
3387}
3388
3389void debug_check_no_locks_held(struct task_struct *task)
3390{
3391 if (unlikely(task->lockdep_depth > 0))
3392 print_held_locks_bug(task);
3393}
3394
3395void debug_show_all_locks(void)
3396{
3397 struct task_struct *g, *p;
3398 int count = 10;
3399 int unlock = 1;
3400
Jarek Poplawski9c35dd72007-03-22 00:11:28 -08003401 if (unlikely(!debug_locks)) {
3402 printk("INFO: lockdep is turned off.\n");
3403 return;
3404 }
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003405 printk("\nShowing all locks held in the system:\n");
3406
3407 /*
3408 * Here we try to get the tasklist_lock as hard as possible,
3409 * if not successful after 2 seconds we ignore it (but keep
3410 * trying). This is to enable a debug printout even if a
3411 * tasklist_lock-holding task deadlocks or crashes.
3412 */
3413retry:
3414 if (!read_trylock(&tasklist_lock)) {
3415 if (count == 10)
3416 printk("hm, tasklist_lock locked, retrying... ");
3417 if (count) {
3418 count--;
3419 printk(" #%d", 10-count);
3420 mdelay(200);
3421 goto retry;
3422 }
3423 printk(" ignoring it.\n");
3424 unlock = 0;
qinghuang feng46fec7a2008-10-28 17:24:28 +08003425 } else {
3426 if (count != 10)
3427 printk(KERN_CONT " locked it.\n");
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003428 }
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003429
3430 do_each_thread(g, p) {
Ingo Molnar85684872007-12-05 15:46:09 +01003431 /*
3432 * It's not reliable to print a task's held locks
3433 * if it's not sleeping (or if it's not the current
3434 * task):
3435 */
3436 if (p->state == TASK_RUNNING && p != current)
3437 continue;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003438 if (p->lockdep_depth)
3439 lockdep_print_held_locks(p);
3440 if (!unlock)
3441 if (read_trylock(&tasklist_lock))
3442 unlock = 1;
3443 } while_each_thread(g, p);
3444
3445 printk("\n");
3446 printk("=============================================\n\n");
3447
3448 if (unlock)
3449 read_unlock(&tasklist_lock);
3450}
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003451EXPORT_SYMBOL_GPL(debug_show_all_locks);
3452
Ingo Molnar82a1fcb2008-01-25 21:08:02 +01003453/*
3454 * Careful: only use this function if you are sure that
3455 * the task cannot run in parallel!
3456 */
3457void __debug_show_held_locks(struct task_struct *task)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003458{
Jarek Poplawski9c35dd72007-03-22 00:11:28 -08003459 if (unlikely(!debug_locks)) {
3460 printk("INFO: lockdep is turned off.\n");
3461 return;
3462 }
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003463 lockdep_print_held_locks(task);
3464}
Ingo Molnar82a1fcb2008-01-25 21:08:02 +01003465EXPORT_SYMBOL_GPL(__debug_show_held_locks);
3466
3467void debug_show_held_locks(struct task_struct *task)
3468{
3469 __debug_show_held_locks(task);
3470}
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003471EXPORT_SYMBOL_GPL(debug_show_held_locks);
Peter Zijlstrab351d162007-10-11 22:11:12 +02003472
3473void lockdep_sys_exit(void)
3474{
3475 struct task_struct *curr = current;
3476
3477 if (unlikely(curr->lockdep_depth)) {
3478 if (!debug_locks_off())
3479 return;
3480 printk("\n================================================\n");
3481 printk( "[ BUG: lock held when returning to user space! ]\n");
3482 printk( "------------------------------------------------\n");
3483 printk("%s/%d is leaving the kernel with locks still held!\n",
3484 curr->comm, curr->pid);
3485 lockdep_print_held_locks(curr);
3486 }
3487}