blob: 4fa6eeb4e8a7f0c6111a0cb5d67fa6f9671bad5b [file] [log] [blame]
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001/*
2 * kernel/lockdep.c
3 *
4 * Runtime locking correctness validator
5 *
6 * Started by Ingo Molnar:
7 *
Peter Zijlstra4b32d0a2007-07-19 01:48:59 -07008 * Copyright (C) 2006,2007 Red Hat, Inc., Ingo Molnar <mingo@redhat.com>
9 * Copyright (C) 2007 Red Hat, Inc., Peter Zijlstra <pzijlstr@redhat.com>
Ingo Molnarfbb9ce952006-07-03 00:24:50 -070010 *
11 * this code maps all the lock dependencies as they occur in a live kernel
12 * and will warn about the following classes of locking bugs:
13 *
14 * - lock inversion scenarios
15 * - circular lock dependencies
16 * - hardirq/softirq safe/unsafe locking bugs
17 *
18 * Bugs are reported even if the current locking scenario does not cause
19 * any deadlock at this point.
20 *
21 * I.e. if anytime in the past two locks were taken in a different order,
22 * even if it happened for another task, even if those were different
23 * locks (but of the same class as this lock), this code will detect it.
24 *
25 * Thanks to Arjan van de Ven for coming up with the initial idea of
26 * mapping lock dependencies runtime.
27 */
28#include <linux/mutex.h>
29#include <linux/sched.h>
30#include <linux/delay.h>
31#include <linux/module.h>
32#include <linux/proc_fs.h>
33#include <linux/seq_file.h>
34#include <linux/spinlock.h>
35#include <linux/kallsyms.h>
36#include <linux/interrupt.h>
37#include <linux/stacktrace.h>
38#include <linux/debug_locks.h>
39#include <linux/irqflags.h>
Dave Jones99de0552006-09-29 02:00:10 -070040#include <linux/utsname.h>
Peter Zijlstra4b32d0a2007-07-19 01:48:59 -070041#include <linux/hash.h>
Steven Rostedt81d68a92008-05-12 21:20:42 +020042#include <linux/ftrace.h>
Ingo Molnarfbb9ce952006-07-03 00:24:50 -070043
44#include <asm/sections.h>
45
46#include "lockdep_internals.h"
47
Peter Zijlstraf20786f2007-07-19 01:48:56 -070048#ifdef CONFIG_PROVE_LOCKING
49int prove_locking = 1;
50module_param(prove_locking, int, 0644);
51#else
52#define prove_locking 0
53#endif
54
55#ifdef CONFIG_LOCK_STAT
56int lock_stat = 1;
57module_param(lock_stat, int, 0644);
58#else
59#define lock_stat 0
60#endif
61
Ingo Molnarfbb9ce952006-07-03 00:24:50 -070062/*
Ingo Molnar74c383f2006-12-13 00:34:43 -080063 * lockdep_lock: protects the lockdep graph, the hashes and the
64 * class/list/hash allocators.
Ingo Molnarfbb9ce952006-07-03 00:24:50 -070065 *
66 * This is one of the rare exceptions where it's justified
67 * to use a raw spinlock - we really dont want the spinlock
Ingo Molnar74c383f2006-12-13 00:34:43 -080068 * code to recurse back into the lockdep code...
Ingo Molnarfbb9ce952006-07-03 00:24:50 -070069 */
Ingo Molnar74c383f2006-12-13 00:34:43 -080070static raw_spinlock_t lockdep_lock = (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED;
71
72static int graph_lock(void)
73{
74 __raw_spin_lock(&lockdep_lock);
75 /*
76 * Make sure that if another CPU detected a bug while
77 * walking the graph we dont change it (while the other
78 * CPU is busy printing out stuff with the graph lock
79 * dropped already)
80 */
81 if (!debug_locks) {
82 __raw_spin_unlock(&lockdep_lock);
83 return 0;
84 }
Steven Rostedtbb065af2008-05-12 21:21:00 +020085 /* prevent any recursions within lockdep from causing deadlocks */
86 current->lockdep_recursion++;
Ingo Molnar74c383f2006-12-13 00:34:43 -080087 return 1;
88}
89
90static inline int graph_unlock(void)
91{
Jarek Poplawski381a2292007-02-10 01:44:58 -080092 if (debug_locks && !__raw_spin_is_locked(&lockdep_lock))
93 return DEBUG_LOCKS_WARN_ON(1);
94
Steven Rostedtbb065af2008-05-12 21:21:00 +020095 current->lockdep_recursion--;
Ingo Molnar74c383f2006-12-13 00:34:43 -080096 __raw_spin_unlock(&lockdep_lock);
97 return 0;
98}
99
100/*
101 * Turn lock debugging off and return with 0 if it was off already,
102 * and also release the graph lock:
103 */
104static inline int debug_locks_off_graph_unlock(void)
105{
106 int ret = debug_locks_off();
107
108 __raw_spin_unlock(&lockdep_lock);
109
110 return ret;
111}
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700112
113static int lockdep_initialized;
114
115unsigned long nr_list_entries;
116static struct lock_list list_entries[MAX_LOCKDEP_ENTRIES];
117
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700118/*
119 * All data structures here are protected by the global debug_lock.
120 *
121 * Mutex key structs only get allocated, once during bootup, and never
122 * get freed - this significantly simplifies the debugging code.
123 */
124unsigned long nr_lock_classes;
125static struct lock_class lock_classes[MAX_LOCKDEP_KEYS];
126
Dave Jonesf82b2172008-08-11 09:30:23 +0200127static inline struct lock_class *hlock_class(struct held_lock *hlock)
128{
129 if (!hlock->class_idx) {
130 DEBUG_LOCKS_WARN_ON(1);
131 return NULL;
132 }
133 return lock_classes + hlock->class_idx - 1;
134}
135
Peter Zijlstraf20786f2007-07-19 01:48:56 -0700136#ifdef CONFIG_LOCK_STAT
137static DEFINE_PER_CPU(struct lock_class_stats[MAX_LOCKDEP_KEYS], lock_stats);
138
Peter Zijlstrac7e78cf2008-10-16 23:17:09 +0200139static int lock_point(unsigned long points[], unsigned long ip)
Peter Zijlstraf20786f2007-07-19 01:48:56 -0700140{
141 int i;
142
Peter Zijlstrac7e78cf2008-10-16 23:17:09 +0200143 for (i = 0; i < LOCKSTAT_POINTS; i++) {
144 if (points[i] == 0) {
145 points[i] = ip;
Peter Zijlstraf20786f2007-07-19 01:48:56 -0700146 break;
147 }
Peter Zijlstrac7e78cf2008-10-16 23:17:09 +0200148 if (points[i] == ip)
Peter Zijlstraf20786f2007-07-19 01:48:56 -0700149 break;
150 }
151
152 return i;
153}
154
155static void lock_time_inc(struct lock_time *lt, s64 time)
156{
157 if (time > lt->max)
158 lt->max = time;
159
160 if (time < lt->min || !lt->min)
161 lt->min = time;
162
163 lt->total += time;
164 lt->nr++;
165}
166
Peter Zijlstrac46261d2007-07-19 01:48:57 -0700167static inline void lock_time_add(struct lock_time *src, struct lock_time *dst)
168{
169 dst->min += src->min;
170 dst->max += src->max;
171 dst->total += src->total;
172 dst->nr += src->nr;
173}
174
175struct lock_class_stats lock_stats(struct lock_class *class)
176{
177 struct lock_class_stats stats;
178 int cpu, i;
179
180 memset(&stats, 0, sizeof(struct lock_class_stats));
181 for_each_possible_cpu(cpu) {
182 struct lock_class_stats *pcs =
183 &per_cpu(lock_stats, cpu)[class - lock_classes];
184
185 for (i = 0; i < ARRAY_SIZE(stats.contention_point); i++)
186 stats.contention_point[i] += pcs->contention_point[i];
187
Peter Zijlstrac7e78cf2008-10-16 23:17:09 +0200188 for (i = 0; i < ARRAY_SIZE(stats.contending_point); i++)
189 stats.contending_point[i] += pcs->contending_point[i];
190
Peter Zijlstrac46261d2007-07-19 01:48:57 -0700191 lock_time_add(&pcs->read_waittime, &stats.read_waittime);
192 lock_time_add(&pcs->write_waittime, &stats.write_waittime);
193
194 lock_time_add(&pcs->read_holdtime, &stats.read_holdtime);
195 lock_time_add(&pcs->write_holdtime, &stats.write_holdtime);
Peter Zijlstra96645672007-07-19 01:49:00 -0700196
197 for (i = 0; i < ARRAY_SIZE(stats.bounces); i++)
198 stats.bounces[i] += pcs->bounces[i];
Peter Zijlstrac46261d2007-07-19 01:48:57 -0700199 }
200
201 return stats;
202}
203
204void clear_lock_stats(struct lock_class *class)
205{
206 int cpu;
207
208 for_each_possible_cpu(cpu) {
209 struct lock_class_stats *cpu_stats =
210 &per_cpu(lock_stats, cpu)[class - lock_classes];
211
212 memset(cpu_stats, 0, sizeof(struct lock_class_stats));
213 }
214 memset(class->contention_point, 0, sizeof(class->contention_point));
Peter Zijlstrac7e78cf2008-10-16 23:17:09 +0200215 memset(class->contending_point, 0, sizeof(class->contending_point));
Peter Zijlstrac46261d2007-07-19 01:48:57 -0700216}
217
Peter Zijlstraf20786f2007-07-19 01:48:56 -0700218static struct lock_class_stats *get_lock_stats(struct lock_class *class)
219{
220 return &get_cpu_var(lock_stats)[class - lock_classes];
221}
222
223static void put_lock_stats(struct lock_class_stats *stats)
224{
225 put_cpu_var(lock_stats);
226}
227
228static void lock_release_holdtime(struct held_lock *hlock)
229{
230 struct lock_class_stats *stats;
231 s64 holdtime;
232
233 if (!lock_stat)
234 return;
235
236 holdtime = sched_clock() - hlock->holdtime_stamp;
237
Dave Jonesf82b2172008-08-11 09:30:23 +0200238 stats = get_lock_stats(hlock_class(hlock));
Peter Zijlstraf20786f2007-07-19 01:48:56 -0700239 if (hlock->read)
240 lock_time_inc(&stats->read_holdtime, holdtime);
241 else
242 lock_time_inc(&stats->write_holdtime, holdtime);
243 put_lock_stats(stats);
244}
245#else
246static inline void lock_release_holdtime(struct held_lock *hlock)
247{
248}
249#endif
250
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700251/*
252 * We keep a global list of all lock classes. The list only grows,
253 * never shrinks. The list is only accessed with the lockdep
254 * spinlock lock held.
255 */
256LIST_HEAD(all_lock_classes);
257
258/*
259 * The lockdep classes are in a hash-table as well, for fast lookup:
260 */
261#define CLASSHASH_BITS (MAX_LOCKDEP_KEYS_BITS - 1)
262#define CLASSHASH_SIZE (1UL << CLASSHASH_BITS)
Peter Zijlstra4b32d0a2007-07-19 01:48:59 -0700263#define __classhashfn(key) hash_long((unsigned long)key, CLASSHASH_BITS)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700264#define classhashentry(key) (classhash_table + __classhashfn((key)))
265
266static struct list_head classhash_table[CLASSHASH_SIZE];
267
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700268/*
269 * We put the lock dependency chains into a hash-table as well, to cache
270 * their existence:
271 */
272#define CHAINHASH_BITS (MAX_LOCKDEP_CHAINS_BITS-1)
273#define CHAINHASH_SIZE (1UL << CHAINHASH_BITS)
Peter Zijlstra4b32d0a2007-07-19 01:48:59 -0700274#define __chainhashfn(chain) hash_long(chain, CHAINHASH_BITS)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700275#define chainhashentry(chain) (chainhash_table + __chainhashfn((chain)))
276
277static struct list_head chainhash_table[CHAINHASH_SIZE];
278
279/*
280 * The hash key of the lock dependency chains is a hash itself too:
281 * it's a hash of all locks taken up to that lock, including that lock.
282 * It's a 64-bit hash, because it's important for the keys to be
283 * unique.
284 */
285#define iterate_chain_key(key1, key2) \
Ingo Molnar03cbc352006-09-29 02:01:46 -0700286 (((key1) << MAX_LOCKDEP_KEYS_BITS) ^ \
287 ((key1) >> (64-MAX_LOCKDEP_KEYS_BITS)) ^ \
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700288 (key2))
289
Steven Rostedt1d09daa2008-05-12 21:20:55 +0200290void lockdep_off(void)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700291{
292 current->lockdep_recursion++;
293}
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700294EXPORT_SYMBOL(lockdep_off);
295
Steven Rostedt1d09daa2008-05-12 21:20:55 +0200296void lockdep_on(void)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700297{
298 current->lockdep_recursion--;
299}
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700300EXPORT_SYMBOL(lockdep_on);
301
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700302/*
303 * Debugging switches:
304 */
305
306#define VERBOSE 0
Ingo Molnar33e94e92006-12-13 00:34:41 -0800307#define VERY_VERBOSE 0
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700308
309#if VERBOSE
310# define HARDIRQ_VERBOSE 1
311# define SOFTIRQ_VERBOSE 1
312#else
313# define HARDIRQ_VERBOSE 0
314# define SOFTIRQ_VERBOSE 0
315#endif
316
317#if VERBOSE || HARDIRQ_VERBOSE || SOFTIRQ_VERBOSE
318/*
319 * Quick filtering for interesting events:
320 */
321static int class_filter(struct lock_class *class)
322{
Andi Kleenf9829cc2006-07-10 04:44:01 -0700323#if 0
324 /* Example */
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700325 if (class->name_version == 1 &&
Andi Kleenf9829cc2006-07-10 04:44:01 -0700326 !strcmp(class->name, "lockname"))
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700327 return 1;
328 if (class->name_version == 1 &&
Andi Kleenf9829cc2006-07-10 04:44:01 -0700329 !strcmp(class->name, "&struct->lockfield"))
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700330 return 1;
Andi Kleenf9829cc2006-07-10 04:44:01 -0700331#endif
Ingo Molnara6640892006-12-13 00:34:39 -0800332 /* Filter everything else. 1 would be to allow everything else */
333 return 0;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700334}
335#endif
336
337static int verbose(struct lock_class *class)
338{
339#if VERBOSE
340 return class_filter(class);
341#endif
342 return 0;
343}
344
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700345/*
346 * Stack-trace: tightly packed array of stack backtrace
Ingo Molnar74c383f2006-12-13 00:34:43 -0800347 * addresses. Protected by the graph_lock.
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700348 */
349unsigned long nr_stack_trace_entries;
350static unsigned long stack_trace[MAX_STACK_TRACE_ENTRIES];
351
352static int save_trace(struct stack_trace *trace)
353{
354 trace->nr_entries = 0;
355 trace->max_entries = MAX_STACK_TRACE_ENTRIES - nr_stack_trace_entries;
356 trace->entries = stack_trace + nr_stack_trace_entries;
357
Andi Kleen5a1b3992006-09-26 10:52:34 +0200358 trace->skip = 3;
Andi Kleen5a1b3992006-09-26 10:52:34 +0200359
Christoph Hellwigab1b6f02007-05-08 00:23:29 -0700360 save_stack_trace(trace);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700361
362 trace->max_entries = trace->nr_entries;
363
364 nr_stack_trace_entries += trace->nr_entries;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700365
366 if (nr_stack_trace_entries == MAX_STACK_TRACE_ENTRIES) {
Ingo Molnar74c383f2006-12-13 00:34:43 -0800367 if (!debug_locks_off_graph_unlock())
368 return 0;
369
370 printk("BUG: MAX_STACK_TRACE_ENTRIES too low!\n");
371 printk("turning off the locking correctness validator.\n");
372 dump_stack();
373
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700374 return 0;
375 }
376
377 return 1;
378}
379
380unsigned int nr_hardirq_chains;
381unsigned int nr_softirq_chains;
382unsigned int nr_process_chains;
383unsigned int max_lockdep_depth;
384unsigned int max_recursion_depth;
385
David Miller419ca3f2008-07-29 21:45:03 -0700386static unsigned int lockdep_dependency_gen_id;
387
388static bool lockdep_dependency_visit(struct lock_class *source,
389 unsigned int depth)
390{
391 if (!depth)
392 lockdep_dependency_gen_id++;
393 if (source->dep_gen_id == lockdep_dependency_gen_id)
394 return true;
395 source->dep_gen_id = lockdep_dependency_gen_id;
396 return false;
397}
398
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700399#ifdef CONFIG_DEBUG_LOCKDEP
400/*
401 * We cannot printk in early bootup code. Not even early_printk()
402 * might work. So we mark any initialization errors and printk
403 * about it later on, in lockdep_info().
404 */
405static int lockdep_init_error;
Johannes Bergc71063c2007-07-19 01:49:02 -0700406static unsigned long lockdep_init_trace_data[20];
407static struct stack_trace lockdep_init_trace = {
408 .max_entries = ARRAY_SIZE(lockdep_init_trace_data),
409 .entries = lockdep_init_trace_data,
410};
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700411
412/*
413 * Various lockdep statistics:
414 */
415atomic_t chain_lookup_hits;
416atomic_t chain_lookup_misses;
417atomic_t hardirqs_on_events;
418atomic_t hardirqs_off_events;
419atomic_t redundant_hardirqs_on;
420atomic_t redundant_hardirqs_off;
421atomic_t softirqs_on_events;
422atomic_t softirqs_off_events;
423atomic_t redundant_softirqs_on;
424atomic_t redundant_softirqs_off;
425atomic_t nr_unused_locks;
426atomic_t nr_cyclic_checks;
427atomic_t nr_cyclic_check_recursions;
428atomic_t nr_find_usage_forwards_checks;
429atomic_t nr_find_usage_forwards_recursions;
430atomic_t nr_find_usage_backwards_checks;
431atomic_t nr_find_usage_backwards_recursions;
432# define debug_atomic_inc(ptr) atomic_inc(ptr)
433# define debug_atomic_dec(ptr) atomic_dec(ptr)
434# define debug_atomic_read(ptr) atomic_read(ptr)
435#else
436# define debug_atomic_inc(ptr) do { } while (0)
437# define debug_atomic_dec(ptr) do { } while (0)
438# define debug_atomic_read(ptr) 0
439#endif
440
441/*
442 * Locking printouts:
443 */
444
445static const char *usage_str[] =
446{
447 [LOCK_USED] = "initial-use ",
448 [LOCK_USED_IN_HARDIRQ] = "in-hardirq-W",
449 [LOCK_USED_IN_SOFTIRQ] = "in-softirq-W",
450 [LOCK_ENABLED_SOFTIRQS] = "softirq-on-W",
451 [LOCK_ENABLED_HARDIRQS] = "hardirq-on-W",
452 [LOCK_USED_IN_HARDIRQ_READ] = "in-hardirq-R",
453 [LOCK_USED_IN_SOFTIRQ_READ] = "in-softirq-R",
454 [LOCK_ENABLED_SOFTIRQS_READ] = "softirq-on-R",
455 [LOCK_ENABLED_HARDIRQS_READ] = "hardirq-on-R",
456};
457
458const char * __get_key_name(struct lockdep_subclass_key *key, char *str)
459{
Alexey Dobriyanffb45122007-05-08 00:28:41 -0700460 return kallsyms_lookup((unsigned long)key, NULL, NULL, NULL, str);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700461}
462
463void
464get_usage_chars(struct lock_class *class, char *c1, char *c2, char *c3, char *c4)
465{
466 *c1 = '.', *c2 = '.', *c3 = '.', *c4 = '.';
467
468 if (class->usage_mask & LOCKF_USED_IN_HARDIRQ)
469 *c1 = '+';
470 else
471 if (class->usage_mask & LOCKF_ENABLED_HARDIRQS)
472 *c1 = '-';
473
474 if (class->usage_mask & LOCKF_USED_IN_SOFTIRQ)
475 *c2 = '+';
476 else
477 if (class->usage_mask & LOCKF_ENABLED_SOFTIRQS)
478 *c2 = '-';
479
480 if (class->usage_mask & LOCKF_ENABLED_HARDIRQS_READ)
481 *c3 = '-';
482 if (class->usage_mask & LOCKF_USED_IN_HARDIRQ_READ) {
483 *c3 = '+';
484 if (class->usage_mask & LOCKF_ENABLED_HARDIRQS_READ)
485 *c3 = '?';
486 }
487
488 if (class->usage_mask & LOCKF_ENABLED_SOFTIRQS_READ)
489 *c4 = '-';
490 if (class->usage_mask & LOCKF_USED_IN_SOFTIRQ_READ) {
491 *c4 = '+';
492 if (class->usage_mask & LOCKF_ENABLED_SOFTIRQS_READ)
493 *c4 = '?';
494 }
495}
496
497static void print_lock_name(struct lock_class *class)
498{
Tejun Heo9281ace2007-07-17 04:03:51 -0700499 char str[KSYM_NAME_LEN], c1, c2, c3, c4;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700500 const char *name;
501
502 get_usage_chars(class, &c1, &c2, &c3, &c4);
503
504 name = class->name;
505 if (!name) {
506 name = __get_key_name(class->key, str);
507 printk(" (%s", name);
508 } else {
509 printk(" (%s", name);
510 if (class->name_version > 1)
511 printk("#%d", class->name_version);
512 if (class->subclass)
513 printk("/%d", class->subclass);
514 }
515 printk("){%c%c%c%c}", c1, c2, c3, c4);
516}
517
518static void print_lockdep_cache(struct lockdep_map *lock)
519{
520 const char *name;
Tejun Heo9281ace2007-07-17 04:03:51 -0700521 char str[KSYM_NAME_LEN];
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700522
523 name = lock->name;
524 if (!name)
525 name = __get_key_name(lock->key->subkeys, str);
526
527 printk("%s", name);
528}
529
530static void print_lock(struct held_lock *hlock)
531{
Dave Jonesf82b2172008-08-11 09:30:23 +0200532 print_lock_name(hlock_class(hlock));
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700533 printk(", at: ");
534 print_ip_sym(hlock->acquire_ip);
535}
536
537static void lockdep_print_held_locks(struct task_struct *curr)
538{
539 int i, depth = curr->lockdep_depth;
540
541 if (!depth) {
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -0700542 printk("no locks held by %s/%d.\n", curr->comm, task_pid_nr(curr));
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700543 return;
544 }
545 printk("%d lock%s held by %s/%d:\n",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -0700546 depth, depth > 1 ? "s" : "", curr->comm, task_pid_nr(curr));
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700547
548 for (i = 0; i < depth; i++) {
549 printk(" #%d: ", i);
550 print_lock(curr->held_locks + i);
551 }
552}
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700553
554static void print_lock_class_header(struct lock_class *class, int depth)
555{
556 int bit;
557
Andi Kleenf9829cc2006-07-10 04:44:01 -0700558 printk("%*s->", depth, "");
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700559 print_lock_name(class);
560 printk(" ops: %lu", class->ops);
561 printk(" {\n");
562
563 for (bit = 0; bit < LOCK_USAGE_STATES; bit++) {
564 if (class->usage_mask & (1 << bit)) {
565 int len = depth;
566
Andi Kleenf9829cc2006-07-10 04:44:01 -0700567 len += printk("%*s %s", depth, "", usage_str[bit]);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700568 len += printk(" at:\n");
569 print_stack_trace(class->usage_traces + bit, len);
570 }
571 }
Andi Kleenf9829cc2006-07-10 04:44:01 -0700572 printk("%*s }\n", depth, "");
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700573
Andi Kleenf9829cc2006-07-10 04:44:01 -0700574 printk("%*s ... key at: ",depth,"");
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700575 print_ip_sym((unsigned long)class->key);
576}
577
578/*
579 * printk all lock dependencies starting at <entry>:
580 */
Ingo Molnar7807faf2008-11-25 08:44:24 +0100581static void __used
582print_lock_dependencies(struct lock_class *class, int depth)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700583{
584 struct lock_list *entry;
585
David Miller419ca3f2008-07-29 21:45:03 -0700586 if (lockdep_dependency_visit(class, depth))
587 return;
588
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700589 if (DEBUG_LOCKS_WARN_ON(depth >= 20))
590 return;
591
592 print_lock_class_header(class, depth);
593
594 list_for_each_entry(entry, &class->locks_after, entry) {
Jarek Poplawskib23984d2006-12-06 20:36:23 -0800595 if (DEBUG_LOCKS_WARN_ON(!entry->class))
596 return;
597
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700598 print_lock_dependencies(entry->class, depth + 1);
599
Andi Kleenf9829cc2006-07-10 04:44:01 -0700600 printk("%*s ... acquired at:\n",depth,"");
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700601 print_stack_trace(&entry->trace, 2);
602 printk("\n");
603 }
604}
605
Dave Jones99de0552006-09-29 02:00:10 -0700606static void print_kernel_version(void)
607{
Serge E. Hallyn96b644b2006-10-02 02:18:13 -0700608 printk("%s %.*s\n", init_utsname()->release,
609 (int)strcspn(init_utsname()->version, " "),
610 init_utsname()->version);
Dave Jones99de0552006-09-29 02:00:10 -0700611}
612
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700613static int very_verbose(struct lock_class *class)
614{
615#if VERY_VERBOSE
616 return class_filter(class);
617#endif
618 return 0;
619}
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700620
621/*
622 * Is this the address of a static object:
623 */
624static int static_obj(void *obj)
625{
626 unsigned long start = (unsigned long) &_stext,
627 end = (unsigned long) &_end,
628 addr = (unsigned long) obj;
629#ifdef CONFIG_SMP
630 int i;
631#endif
632
633 /*
634 * static variable?
635 */
636 if ((addr >= start) && (addr < end))
637 return 1;
638
639#ifdef CONFIG_SMP
640 /*
641 * percpu var?
642 */
643 for_each_possible_cpu(i) {
644 start = (unsigned long) &__per_cpu_start + per_cpu_offset(i);
Ingo Molnar1ff56832006-11-17 19:57:22 +0100645 end = (unsigned long) &__per_cpu_start + PERCPU_ENOUGH_ROOM
646 + per_cpu_offset(i);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700647
648 if ((addr >= start) && (addr < end))
649 return 1;
650 }
651#endif
652
653 /*
654 * module var?
655 */
656 return is_module_address(addr);
657}
658
659/*
660 * To make lock name printouts unique, we calculate a unique
661 * class->name_version generation counter:
662 */
663static int count_matching_names(struct lock_class *new_class)
664{
665 struct lock_class *class;
666 int count = 0;
667
668 if (!new_class->name)
669 return 0;
670
671 list_for_each_entry(class, &all_lock_classes, lock_entry) {
672 if (new_class->key - new_class->subclass == class->key)
673 return class->name_version;
674 if (class->name && !strcmp(class->name, new_class->name))
675 count = max(count, class->name_version);
676 }
677
678 return count + 1;
679}
680
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700681/*
682 * Register a lock's class in the hash-table, if the class is not present
683 * yet. Otherwise we look it up. We cache the result in the lock object
684 * itself, so actual lookup of the hash should be once per lock object.
685 */
686static inline struct lock_class *
Ingo Molnard6d897c2006-07-10 04:44:04 -0700687look_up_lock_class(struct lockdep_map *lock, unsigned int subclass)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700688{
689 struct lockdep_subclass_key *key;
690 struct list_head *hash_head;
691 struct lock_class *class;
692
693#ifdef CONFIG_DEBUG_LOCKDEP
694 /*
695 * If the architecture calls into lockdep before initializing
696 * the hashes then we'll warn about it later. (we cannot printk
697 * right now)
698 */
699 if (unlikely(!lockdep_initialized)) {
700 lockdep_init();
701 lockdep_init_error = 1;
Johannes Bergc71063c2007-07-19 01:49:02 -0700702 save_stack_trace(&lockdep_init_trace);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700703 }
704#endif
705
706 /*
707 * Static locks do not have their class-keys yet - for them the key
708 * is the lock object itself:
709 */
710 if (unlikely(!lock->key))
711 lock->key = (void *)lock;
712
713 /*
714 * NOTE: the class-key must be unique. For dynamic locks, a static
715 * lock_class_key variable is passed in through the mutex_init()
716 * (or spin_lock_init()) call - which acts as the key. For static
717 * locks we use the lock object itself as the key.
718 */
Peter Zijlstra4b32d0a2007-07-19 01:48:59 -0700719 BUILD_BUG_ON(sizeof(struct lock_class_key) >
720 sizeof(struct lockdep_map));
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700721
722 key = lock->key->subkeys + subclass;
723
724 hash_head = classhashentry(key);
725
726 /*
727 * We can walk the hash lockfree, because the hash only
728 * grows, and we are careful when adding entries to the end:
729 */
Peter Zijlstra4b32d0a2007-07-19 01:48:59 -0700730 list_for_each_entry(class, hash_head, hash_entry) {
731 if (class->key == key) {
732 WARN_ON_ONCE(class->name != lock->name);
Ingo Molnard6d897c2006-07-10 04:44:04 -0700733 return class;
Peter Zijlstra4b32d0a2007-07-19 01:48:59 -0700734 }
735 }
Ingo Molnard6d897c2006-07-10 04:44:04 -0700736
737 return NULL;
738}
739
740/*
741 * Register a lock's class in the hash-table, if the class is not present
742 * yet. Otherwise we look it up. We cache the result in the lock object
743 * itself, so actual lookup of the hash should be once per lock object.
744 */
745static inline struct lock_class *
Peter Zijlstra4dfbb9d2006-10-11 01:45:14 -0400746register_lock_class(struct lockdep_map *lock, unsigned int subclass, int force)
Ingo Molnard6d897c2006-07-10 04:44:04 -0700747{
748 struct lockdep_subclass_key *key;
749 struct list_head *hash_head;
750 struct lock_class *class;
Ingo Molnar70e45062006-12-06 20:40:50 -0800751 unsigned long flags;
Ingo Molnard6d897c2006-07-10 04:44:04 -0700752
753 class = look_up_lock_class(lock, subclass);
754 if (likely(class))
755 return class;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700756
757 /*
758 * Debug-check: all keys must be persistent!
759 */
760 if (!static_obj(lock->key)) {
761 debug_locks_off();
762 printk("INFO: trying to register non-static key.\n");
763 printk("the code is fine but needs lockdep annotation.\n");
764 printk("turning off the locking correctness validator.\n");
765 dump_stack();
766
767 return NULL;
768 }
769
Ingo Molnard6d897c2006-07-10 04:44:04 -0700770 key = lock->key->subkeys + subclass;
771 hash_head = classhashentry(key);
772
Ingo Molnar70e45062006-12-06 20:40:50 -0800773 raw_local_irq_save(flags);
Ingo Molnar74c383f2006-12-13 00:34:43 -0800774 if (!graph_lock()) {
775 raw_local_irq_restore(flags);
776 return NULL;
777 }
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700778 /*
779 * We have to do the hash-walk again, to avoid races
780 * with another CPU:
781 */
782 list_for_each_entry(class, hash_head, hash_entry)
783 if (class->key == key)
784 goto out_unlock_set;
785 /*
786 * Allocate a new key from the static array, and add it to
787 * the hash:
788 */
789 if (nr_lock_classes >= MAX_LOCKDEP_KEYS) {
Ingo Molnar74c383f2006-12-13 00:34:43 -0800790 if (!debug_locks_off_graph_unlock()) {
791 raw_local_irq_restore(flags);
792 return NULL;
793 }
Ingo Molnar70e45062006-12-06 20:40:50 -0800794 raw_local_irq_restore(flags);
Ingo Molnar74c383f2006-12-13 00:34:43 -0800795
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700796 printk("BUG: MAX_LOCKDEP_KEYS too low!\n");
797 printk("turning off the locking correctness validator.\n");
798 return NULL;
799 }
800 class = lock_classes + nr_lock_classes++;
801 debug_atomic_inc(&nr_unused_locks);
802 class->key = key;
803 class->name = lock->name;
804 class->subclass = subclass;
805 INIT_LIST_HEAD(&class->lock_entry);
806 INIT_LIST_HEAD(&class->locks_before);
807 INIT_LIST_HEAD(&class->locks_after);
808 class->name_version = count_matching_names(class);
809 /*
810 * We use RCU's safe list-add method to make
811 * parallel walking of the hash-list safe:
812 */
813 list_add_tail_rcu(&class->hash_entry, hash_head);
Dale Farnsworth14811972008-02-25 23:03:02 +0100814 /*
815 * Add it to the global list of classes:
816 */
817 list_add_tail_rcu(&class->lock_entry, &all_lock_classes);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700818
819 if (verbose(class)) {
Ingo Molnar74c383f2006-12-13 00:34:43 -0800820 graph_unlock();
Ingo Molnar70e45062006-12-06 20:40:50 -0800821 raw_local_irq_restore(flags);
Ingo Molnar74c383f2006-12-13 00:34:43 -0800822
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700823 printk("\nnew class %p: %s", class->key, class->name);
824 if (class->name_version > 1)
825 printk("#%d", class->name_version);
826 printk("\n");
827 dump_stack();
Ingo Molnar74c383f2006-12-13 00:34:43 -0800828
Ingo Molnar70e45062006-12-06 20:40:50 -0800829 raw_local_irq_save(flags);
Ingo Molnar74c383f2006-12-13 00:34:43 -0800830 if (!graph_lock()) {
831 raw_local_irq_restore(flags);
832 return NULL;
833 }
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700834 }
835out_unlock_set:
Ingo Molnar74c383f2006-12-13 00:34:43 -0800836 graph_unlock();
Ingo Molnar70e45062006-12-06 20:40:50 -0800837 raw_local_irq_restore(flags);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700838
Peter Zijlstra4dfbb9d2006-10-11 01:45:14 -0400839 if (!subclass || force)
Ingo Molnard6d897c2006-07-10 04:44:04 -0700840 lock->class_cache = class;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700841
Jarek Poplawski381a2292007-02-10 01:44:58 -0800842 if (DEBUG_LOCKS_WARN_ON(class->subclass != subclass))
843 return NULL;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700844
845 return class;
846}
847
Peter Zijlstraca58abc2007-07-19 01:48:53 -0700848#ifdef CONFIG_PROVE_LOCKING
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700849/*
Peter Zijlstra8e182572007-07-19 01:48:54 -0700850 * Allocate a lockdep entry. (assumes the graph_lock held, returns
851 * with NULL on failure)
852 */
853static struct lock_list *alloc_list_entry(void)
854{
855 if (nr_list_entries >= MAX_LOCKDEP_ENTRIES) {
856 if (!debug_locks_off_graph_unlock())
857 return NULL;
858
859 printk("BUG: MAX_LOCKDEP_ENTRIES too low!\n");
860 printk("turning off the locking correctness validator.\n");
861 return NULL;
862 }
863 return list_entries + nr_list_entries++;
864}
865
866/*
867 * Add a new dependency to the head of the list:
868 */
869static int add_lock_to_list(struct lock_class *class, struct lock_class *this,
870 struct list_head *head, unsigned long ip, int distance)
871{
872 struct lock_list *entry;
873 /*
874 * Lock not present yet - get a new dependency struct and
875 * add it to the list:
876 */
877 entry = alloc_list_entry();
878 if (!entry)
879 return 0;
880
Peter Zijlstra8e182572007-07-19 01:48:54 -0700881 if (!save_trace(&entry->trace))
882 return 0;
883
Zhu Yi74870172008-08-27 14:33:00 +0800884 entry->class = this;
885 entry->distance = distance;
Peter Zijlstra8e182572007-07-19 01:48:54 -0700886 /*
887 * Since we never remove from the dependency list, the list can
888 * be walked lockless by other CPUs, it's only allocation
889 * that must be protected by the spinlock. But this also means
890 * we must make new entries visible only once writes to the
891 * entry become visible - hence the RCU op:
892 */
893 list_add_tail_rcu(&entry->entry, head);
894
895 return 1;
896}
897
898/*
899 * Recursive, forwards-direction lock-dependency checking, used for
900 * both noncyclic checking and for hardirq-unsafe/softirq-unsafe
901 * checking.
902 *
903 * (to keep the stackframe of the recursive functions small we
904 * use these global variables, and we also mark various helper
905 * functions as noinline.)
906 */
907static struct held_lock *check_source, *check_target;
908
909/*
910 * Print a dependency chain entry (this is only done when a deadlock
911 * has been detected):
912 */
913static noinline int
914print_circular_bug_entry(struct lock_list *target, unsigned int depth)
915{
916 if (debug_locks_silent)
917 return 0;
918 printk("\n-> #%u", depth);
919 print_lock_name(target->class);
920 printk(":\n");
921 print_stack_trace(&target->trace, 6);
922
923 return 0;
924}
925
926/*
927 * When a circular dependency is detected, print the
928 * header first:
929 */
930static noinline int
931print_circular_bug_header(struct lock_list *entry, unsigned int depth)
932{
933 struct task_struct *curr = current;
934
935 if (!debug_locks_off_graph_unlock() || debug_locks_silent)
936 return 0;
937
938 printk("\n=======================================================\n");
939 printk( "[ INFO: possible circular locking dependency detected ]\n");
940 print_kernel_version();
941 printk( "-------------------------------------------------------\n");
942 printk("%s/%d is trying to acquire lock:\n",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -0700943 curr->comm, task_pid_nr(curr));
Peter Zijlstra8e182572007-07-19 01:48:54 -0700944 print_lock(check_source);
945 printk("\nbut task is already holding lock:\n");
946 print_lock(check_target);
947 printk("\nwhich lock already depends on the new lock.\n\n");
948 printk("\nthe existing dependency chain (in reverse order) is:\n");
949
950 print_circular_bug_entry(entry, depth);
951
952 return 0;
953}
954
955static noinline int print_circular_bug_tail(void)
956{
957 struct task_struct *curr = current;
958 struct lock_list this;
959
960 if (debug_locks_silent)
961 return 0;
962
Dave Jonesf82b2172008-08-11 09:30:23 +0200963 this.class = hlock_class(check_source);
Peter Zijlstra8e182572007-07-19 01:48:54 -0700964 if (!save_trace(&this.trace))
965 return 0;
966
967 print_circular_bug_entry(&this, 0);
968
969 printk("\nother info that might help us debug this:\n\n");
970 lockdep_print_held_locks(curr);
971
972 printk("\nstack backtrace:\n");
973 dump_stack();
974
975 return 0;
976}
977
978#define RECURSION_LIMIT 40
979
980static int noinline print_infinite_recursion_bug(void)
981{
982 if (!debug_locks_off_graph_unlock())
983 return 0;
984
985 WARN_ON(1);
986
987 return 0;
988}
989
David Miller419ca3f2008-07-29 21:45:03 -0700990unsigned long __lockdep_count_forward_deps(struct lock_class *class,
991 unsigned int depth)
992{
993 struct lock_list *entry;
994 unsigned long ret = 1;
995
996 if (lockdep_dependency_visit(class, depth))
997 return 0;
998
999 /*
1000 * Recurse this class's dependency list:
1001 */
1002 list_for_each_entry(entry, &class->locks_after, entry)
1003 ret += __lockdep_count_forward_deps(entry->class, depth + 1);
1004
1005 return ret;
1006}
1007
1008unsigned long lockdep_count_forward_deps(struct lock_class *class)
1009{
1010 unsigned long ret, flags;
1011
1012 local_irq_save(flags);
1013 __raw_spin_lock(&lockdep_lock);
1014 ret = __lockdep_count_forward_deps(class, 0);
1015 __raw_spin_unlock(&lockdep_lock);
1016 local_irq_restore(flags);
1017
1018 return ret;
1019}
1020
1021unsigned long __lockdep_count_backward_deps(struct lock_class *class,
1022 unsigned int depth)
1023{
1024 struct lock_list *entry;
1025 unsigned long ret = 1;
1026
1027 if (lockdep_dependency_visit(class, depth))
1028 return 0;
1029 /*
1030 * Recurse this class's dependency list:
1031 */
1032 list_for_each_entry(entry, &class->locks_before, entry)
1033 ret += __lockdep_count_backward_deps(entry->class, depth + 1);
1034
1035 return ret;
1036}
1037
1038unsigned long lockdep_count_backward_deps(struct lock_class *class)
1039{
1040 unsigned long ret, flags;
1041
1042 local_irq_save(flags);
1043 __raw_spin_lock(&lockdep_lock);
1044 ret = __lockdep_count_backward_deps(class, 0);
1045 __raw_spin_unlock(&lockdep_lock);
1046 local_irq_restore(flags);
1047
1048 return ret;
1049}
1050
Peter Zijlstra8e182572007-07-19 01:48:54 -07001051/*
1052 * Prove that the dependency graph starting at <entry> can not
1053 * lead to <target>. Print an error and return 0 if it does.
1054 */
1055static noinline int
1056check_noncircular(struct lock_class *source, unsigned int depth)
1057{
1058 struct lock_list *entry;
1059
David Miller419ca3f2008-07-29 21:45:03 -07001060 if (lockdep_dependency_visit(source, depth))
1061 return 1;
1062
Peter Zijlstra8e182572007-07-19 01:48:54 -07001063 debug_atomic_inc(&nr_cyclic_check_recursions);
1064 if (depth > max_recursion_depth)
1065 max_recursion_depth = depth;
1066 if (depth >= RECURSION_LIMIT)
1067 return print_infinite_recursion_bug();
1068 /*
1069 * Check this lock's dependency list:
1070 */
1071 list_for_each_entry(entry, &source->locks_after, entry) {
Dave Jonesf82b2172008-08-11 09:30:23 +02001072 if (entry->class == hlock_class(check_target))
Peter Zijlstra8e182572007-07-19 01:48:54 -07001073 return print_circular_bug_header(entry, depth+1);
1074 debug_atomic_inc(&nr_cyclic_checks);
1075 if (!check_noncircular(entry->class, depth+1))
1076 return print_circular_bug_entry(entry, depth+1);
1077 }
1078 return 1;
1079}
1080
Steven Rostedt81d68a92008-05-12 21:20:42 +02001081#if defined(CONFIG_TRACE_IRQFLAGS) && defined(CONFIG_PROVE_LOCKING)
Peter Zijlstra8e182572007-07-19 01:48:54 -07001082/*
1083 * Forwards and backwards subgraph searching, for the purposes of
1084 * proving that two subgraphs can be connected by a new dependency
1085 * without creating any illegal irq-safe -> irq-unsafe lock dependency.
1086 */
1087static enum lock_usage_bit find_usage_bit;
1088static struct lock_class *forwards_match, *backwards_match;
1089
1090/*
1091 * Find a node in the forwards-direction dependency sub-graph starting
1092 * at <source> that matches <find_usage_bit>.
1093 *
1094 * Return 2 if such a node exists in the subgraph, and put that node
1095 * into <forwards_match>.
1096 *
1097 * Return 1 otherwise and keep <forwards_match> unchanged.
1098 * Return 0 on error.
1099 */
1100static noinline int
1101find_usage_forwards(struct lock_class *source, unsigned int depth)
1102{
1103 struct lock_list *entry;
1104 int ret;
1105
David Miller419ca3f2008-07-29 21:45:03 -07001106 if (lockdep_dependency_visit(source, depth))
1107 return 1;
1108
Peter Zijlstra8e182572007-07-19 01:48:54 -07001109 if (depth > max_recursion_depth)
1110 max_recursion_depth = depth;
1111 if (depth >= RECURSION_LIMIT)
1112 return print_infinite_recursion_bug();
1113
1114 debug_atomic_inc(&nr_find_usage_forwards_checks);
1115 if (source->usage_mask & (1 << find_usage_bit)) {
1116 forwards_match = source;
1117 return 2;
1118 }
1119
1120 /*
1121 * Check this lock's dependency list:
1122 */
1123 list_for_each_entry(entry, &source->locks_after, entry) {
1124 debug_atomic_inc(&nr_find_usage_forwards_recursions);
1125 ret = find_usage_forwards(entry->class, depth+1);
1126 if (ret == 2 || ret == 0)
1127 return ret;
1128 }
1129 return 1;
1130}
1131
1132/*
1133 * Find a node in the backwards-direction dependency sub-graph starting
1134 * at <source> that matches <find_usage_bit>.
1135 *
1136 * Return 2 if such a node exists in the subgraph, and put that node
1137 * into <backwards_match>.
1138 *
1139 * Return 1 otherwise and keep <backwards_match> unchanged.
1140 * Return 0 on error.
1141 */
Steven Rostedt1d09daa2008-05-12 21:20:55 +02001142static noinline int
Peter Zijlstra8e182572007-07-19 01:48:54 -07001143find_usage_backwards(struct lock_class *source, unsigned int depth)
1144{
1145 struct lock_list *entry;
1146 int ret;
1147
David Miller419ca3f2008-07-29 21:45:03 -07001148 if (lockdep_dependency_visit(source, depth))
1149 return 1;
1150
Peter Zijlstra8e182572007-07-19 01:48:54 -07001151 if (!__raw_spin_is_locked(&lockdep_lock))
1152 return DEBUG_LOCKS_WARN_ON(1);
1153
1154 if (depth > max_recursion_depth)
1155 max_recursion_depth = depth;
1156 if (depth >= RECURSION_LIMIT)
1157 return print_infinite_recursion_bug();
1158
1159 debug_atomic_inc(&nr_find_usage_backwards_checks);
1160 if (source->usage_mask & (1 << find_usage_bit)) {
1161 backwards_match = source;
1162 return 2;
1163 }
1164
Dave Jonesf82b2172008-08-11 09:30:23 +02001165 if (!source && debug_locks_off_graph_unlock()) {
1166 WARN_ON(1);
1167 return 0;
1168 }
1169
Peter Zijlstra8e182572007-07-19 01:48:54 -07001170 /*
1171 * Check this lock's dependency list:
1172 */
1173 list_for_each_entry(entry, &source->locks_before, entry) {
1174 debug_atomic_inc(&nr_find_usage_backwards_recursions);
1175 ret = find_usage_backwards(entry->class, depth+1);
1176 if (ret == 2 || ret == 0)
1177 return ret;
1178 }
1179 return 1;
1180}
1181
1182static int
1183print_bad_irq_dependency(struct task_struct *curr,
1184 struct held_lock *prev,
1185 struct held_lock *next,
1186 enum lock_usage_bit bit1,
1187 enum lock_usage_bit bit2,
1188 const char *irqclass)
1189{
1190 if (!debug_locks_off_graph_unlock() || debug_locks_silent)
1191 return 0;
1192
1193 printk("\n======================================================\n");
1194 printk( "[ INFO: %s-safe -> %s-unsafe lock order detected ]\n",
1195 irqclass, irqclass);
1196 print_kernel_version();
1197 printk( "------------------------------------------------------\n");
1198 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 -07001199 curr->comm, task_pid_nr(curr),
Peter Zijlstra8e182572007-07-19 01:48:54 -07001200 curr->hardirq_context, hardirq_count() >> HARDIRQ_SHIFT,
1201 curr->softirq_context, softirq_count() >> SOFTIRQ_SHIFT,
1202 curr->hardirqs_enabled,
1203 curr->softirqs_enabled);
1204 print_lock(next);
1205
1206 printk("\nand this task is already holding:\n");
1207 print_lock(prev);
1208 printk("which would create a new lock dependency:\n");
Dave Jonesf82b2172008-08-11 09:30:23 +02001209 print_lock_name(hlock_class(prev));
Peter Zijlstra8e182572007-07-19 01:48:54 -07001210 printk(" ->");
Dave Jonesf82b2172008-08-11 09:30:23 +02001211 print_lock_name(hlock_class(next));
Peter Zijlstra8e182572007-07-19 01:48:54 -07001212 printk("\n");
1213
1214 printk("\nbut this new dependency connects a %s-irq-safe lock:\n",
1215 irqclass);
1216 print_lock_name(backwards_match);
1217 printk("\n... which became %s-irq-safe at:\n", irqclass);
1218
1219 print_stack_trace(backwards_match->usage_traces + bit1, 1);
1220
1221 printk("\nto a %s-irq-unsafe lock:\n", irqclass);
1222 print_lock_name(forwards_match);
1223 printk("\n... which became %s-irq-unsafe at:\n", irqclass);
1224 printk("...");
1225
1226 print_stack_trace(forwards_match->usage_traces + bit2, 1);
1227
1228 printk("\nother info that might help us debug this:\n\n");
1229 lockdep_print_held_locks(curr);
1230
1231 printk("\nthe %s-irq-safe lock's dependencies:\n", irqclass);
1232 print_lock_dependencies(backwards_match, 0);
1233
1234 printk("\nthe %s-irq-unsafe lock's dependencies:\n", irqclass);
1235 print_lock_dependencies(forwards_match, 0);
1236
1237 printk("\nstack backtrace:\n");
1238 dump_stack();
1239
1240 return 0;
1241}
1242
1243static int
1244check_usage(struct task_struct *curr, struct held_lock *prev,
1245 struct held_lock *next, enum lock_usage_bit bit_backwards,
1246 enum lock_usage_bit bit_forwards, const char *irqclass)
1247{
1248 int ret;
1249
1250 find_usage_bit = bit_backwards;
1251 /* fills in <backwards_match> */
Dave Jonesf82b2172008-08-11 09:30:23 +02001252 ret = find_usage_backwards(hlock_class(prev), 0);
Peter Zijlstra8e182572007-07-19 01:48:54 -07001253 if (!ret || ret == 1)
1254 return ret;
1255
1256 find_usage_bit = bit_forwards;
Dave Jonesf82b2172008-08-11 09:30:23 +02001257 ret = find_usage_forwards(hlock_class(next), 0);
Peter Zijlstra8e182572007-07-19 01:48:54 -07001258 if (!ret || ret == 1)
1259 return ret;
1260 /* ret == 2 */
1261 return print_bad_irq_dependency(curr, prev, next,
1262 bit_backwards, bit_forwards, irqclass);
1263}
1264
1265static int
1266check_prev_add_irq(struct task_struct *curr, struct held_lock *prev,
1267 struct held_lock *next)
1268{
1269 /*
1270 * Prove that the new dependency does not connect a hardirq-safe
1271 * lock with a hardirq-unsafe lock - to achieve this we search
1272 * the backwards-subgraph starting at <prev>, and the
1273 * forwards-subgraph starting at <next>:
1274 */
1275 if (!check_usage(curr, prev, next, LOCK_USED_IN_HARDIRQ,
1276 LOCK_ENABLED_HARDIRQS, "hard"))
1277 return 0;
1278
1279 /*
1280 * Prove that the new dependency does not connect a hardirq-safe-read
1281 * lock with a hardirq-unsafe lock - to achieve this we search
1282 * the backwards-subgraph starting at <prev>, and the
1283 * forwards-subgraph starting at <next>:
1284 */
1285 if (!check_usage(curr, prev, next, LOCK_USED_IN_HARDIRQ_READ,
1286 LOCK_ENABLED_HARDIRQS, "hard-read"))
1287 return 0;
1288
1289 /*
1290 * Prove that the new dependency does not connect a softirq-safe
1291 * lock with a softirq-unsafe lock - to achieve this we search
1292 * the backwards-subgraph starting at <prev>, and the
1293 * forwards-subgraph starting at <next>:
1294 */
1295 if (!check_usage(curr, prev, next, LOCK_USED_IN_SOFTIRQ,
1296 LOCK_ENABLED_SOFTIRQS, "soft"))
1297 return 0;
1298 /*
1299 * Prove that the new dependency does not connect a softirq-safe-read
1300 * lock with a softirq-unsafe lock - to achieve this we search
1301 * the backwards-subgraph starting at <prev>, and the
1302 * forwards-subgraph starting at <next>:
1303 */
1304 if (!check_usage(curr, prev, next, LOCK_USED_IN_SOFTIRQ_READ,
1305 LOCK_ENABLED_SOFTIRQS, "soft"))
1306 return 0;
1307
1308 return 1;
1309}
1310
1311static void inc_chains(void)
1312{
1313 if (current->hardirq_context)
1314 nr_hardirq_chains++;
1315 else {
1316 if (current->softirq_context)
1317 nr_softirq_chains++;
1318 else
1319 nr_process_chains++;
1320 }
1321}
1322
1323#else
1324
1325static inline int
1326check_prev_add_irq(struct task_struct *curr, struct held_lock *prev,
1327 struct held_lock *next)
1328{
1329 return 1;
1330}
1331
1332static inline void inc_chains(void)
1333{
1334 nr_process_chains++;
1335}
1336
1337#endif
1338
1339static int
1340print_deadlock_bug(struct task_struct *curr, struct held_lock *prev,
1341 struct held_lock *next)
1342{
1343 if (!debug_locks_off_graph_unlock() || debug_locks_silent)
1344 return 0;
1345
1346 printk("\n=============================================\n");
1347 printk( "[ INFO: possible recursive locking detected ]\n");
1348 print_kernel_version();
1349 printk( "---------------------------------------------\n");
1350 printk("%s/%d is trying to acquire lock:\n",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07001351 curr->comm, task_pid_nr(curr));
Peter Zijlstra8e182572007-07-19 01:48:54 -07001352 print_lock(next);
1353 printk("\nbut task is already holding lock:\n");
1354 print_lock(prev);
1355
1356 printk("\nother info that might help us debug this:\n");
1357 lockdep_print_held_locks(curr);
1358
1359 printk("\nstack backtrace:\n");
1360 dump_stack();
1361
1362 return 0;
1363}
1364
1365/*
1366 * Check whether we are holding such a class already.
1367 *
1368 * (Note that this has to be done separately, because the graph cannot
1369 * detect such classes of deadlocks.)
1370 *
1371 * Returns: 0 on deadlock detected, 1 on OK, 2 on recursive read
1372 */
1373static int
1374check_deadlock(struct task_struct *curr, struct held_lock *next,
1375 struct lockdep_map *next_instance, int read)
1376{
1377 struct held_lock *prev;
Peter Zijlstra7531e2f2008-08-11 09:30:24 +02001378 struct held_lock *nest = NULL;
Peter Zijlstra8e182572007-07-19 01:48:54 -07001379 int i;
1380
1381 for (i = 0; i < curr->lockdep_depth; i++) {
1382 prev = curr->held_locks + i;
Peter Zijlstra7531e2f2008-08-11 09:30:24 +02001383
1384 if (prev->instance == next->nest_lock)
1385 nest = prev;
1386
Dave Jonesf82b2172008-08-11 09:30:23 +02001387 if (hlock_class(prev) != hlock_class(next))
Peter Zijlstra8e182572007-07-19 01:48:54 -07001388 continue;
Peter Zijlstra7531e2f2008-08-11 09:30:24 +02001389
Peter Zijlstra8e182572007-07-19 01:48:54 -07001390 /*
1391 * Allow read-after-read recursion of the same
1392 * lock class (i.e. read_lock(lock)+read_lock(lock)):
1393 */
1394 if ((read == 2) && prev->read)
1395 return 2;
Peter Zijlstra7531e2f2008-08-11 09:30:24 +02001396
1397 /*
1398 * We're holding the nest_lock, which serializes this lock's
1399 * nesting behaviour.
1400 */
1401 if (nest)
1402 return 2;
1403
Peter Zijlstra8e182572007-07-19 01:48:54 -07001404 return print_deadlock_bug(curr, prev, next);
1405 }
1406 return 1;
1407}
1408
1409/*
1410 * There was a chain-cache miss, and we are about to add a new dependency
1411 * to a previous lock. We recursively validate the following rules:
1412 *
1413 * - would the adding of the <prev> -> <next> dependency create a
1414 * circular dependency in the graph? [== circular deadlock]
1415 *
1416 * - does the new prev->next dependency connect any hardirq-safe lock
1417 * (in the full backwards-subgraph starting at <prev>) with any
1418 * hardirq-unsafe lock (in the full forwards-subgraph starting at
1419 * <next>)? [== illegal lock inversion with hardirq contexts]
1420 *
1421 * - does the new prev->next dependency connect any softirq-safe lock
1422 * (in the full backwards-subgraph starting at <prev>) with any
1423 * softirq-unsafe lock (in the full forwards-subgraph starting at
1424 * <next>)? [== illegal lock inversion with softirq contexts]
1425 *
1426 * any of these scenarios could lead to a deadlock.
1427 *
1428 * Then if all the validations pass, we add the forwards and backwards
1429 * dependency.
1430 */
1431static int
1432check_prev_add(struct task_struct *curr, struct held_lock *prev,
1433 struct held_lock *next, int distance)
1434{
1435 struct lock_list *entry;
1436 int ret;
1437
1438 /*
1439 * Prove that the new <prev> -> <next> dependency would not
1440 * create a circular dependency in the graph. (We do this by
1441 * forward-recursing into the graph starting at <next>, and
1442 * checking whether we can reach <prev>.)
1443 *
1444 * We are using global variables to control the recursion, to
1445 * keep the stackframe size of the recursive functions low:
1446 */
1447 check_source = next;
1448 check_target = prev;
Dave Jonesf82b2172008-08-11 09:30:23 +02001449 if (!(check_noncircular(hlock_class(next), 0)))
Peter Zijlstra8e182572007-07-19 01:48:54 -07001450 return print_circular_bug_tail();
1451
1452 if (!check_prev_add_irq(curr, prev, next))
1453 return 0;
1454
1455 /*
1456 * For recursive read-locks we do all the dependency checks,
1457 * but we dont store read-triggered dependencies (only
1458 * write-triggered dependencies). This ensures that only the
1459 * write-side dependencies matter, and that if for example a
1460 * write-lock never takes any other locks, then the reads are
1461 * equivalent to a NOP.
1462 */
1463 if (next->read == 2 || prev->read == 2)
1464 return 1;
1465 /*
1466 * Is the <prev> -> <next> dependency already present?
1467 *
1468 * (this may occur even though this is a new chain: consider
1469 * e.g. the L1 -> L2 -> L3 -> L4 and the L5 -> L1 -> L2 -> L3
1470 * chains - the second one will be new, but L1 already has
1471 * L2 added to its dependency list, due to the first chain.)
1472 */
Dave Jonesf82b2172008-08-11 09:30:23 +02001473 list_for_each_entry(entry, &hlock_class(prev)->locks_after, entry) {
1474 if (entry->class == hlock_class(next)) {
Peter Zijlstra8e182572007-07-19 01:48:54 -07001475 if (distance == 1)
1476 entry->distance = 1;
1477 return 2;
1478 }
1479 }
1480
1481 /*
1482 * Ok, all validations passed, add the new lock
1483 * to the previous lock's dependency list:
1484 */
Dave Jonesf82b2172008-08-11 09:30:23 +02001485 ret = add_lock_to_list(hlock_class(prev), hlock_class(next),
1486 &hlock_class(prev)->locks_after,
1487 next->acquire_ip, distance);
Peter Zijlstra8e182572007-07-19 01:48:54 -07001488
1489 if (!ret)
1490 return 0;
1491
Dave Jonesf82b2172008-08-11 09:30:23 +02001492 ret = add_lock_to_list(hlock_class(next), hlock_class(prev),
1493 &hlock_class(next)->locks_before,
1494 next->acquire_ip, distance);
Peter Zijlstra8e182572007-07-19 01:48:54 -07001495 if (!ret)
1496 return 0;
1497
1498 /*
1499 * Debugging printouts:
1500 */
Dave Jonesf82b2172008-08-11 09:30:23 +02001501 if (verbose(hlock_class(prev)) || verbose(hlock_class(next))) {
Peter Zijlstra8e182572007-07-19 01:48:54 -07001502 graph_unlock();
1503 printk("\n new dependency: ");
Dave Jonesf82b2172008-08-11 09:30:23 +02001504 print_lock_name(hlock_class(prev));
Peter Zijlstra8e182572007-07-19 01:48:54 -07001505 printk(" => ");
Dave Jonesf82b2172008-08-11 09:30:23 +02001506 print_lock_name(hlock_class(next));
Peter Zijlstra8e182572007-07-19 01:48:54 -07001507 printk("\n");
1508 dump_stack();
1509 return graph_lock();
1510 }
1511 return 1;
1512}
1513
1514/*
1515 * Add the dependency to all directly-previous locks that are 'relevant'.
1516 * The ones that are relevant are (in increasing distance from curr):
1517 * all consecutive trylock entries and the final non-trylock entry - or
1518 * the end of this context's lock-chain - whichever comes first.
1519 */
1520static int
1521check_prevs_add(struct task_struct *curr, struct held_lock *next)
1522{
1523 int depth = curr->lockdep_depth;
1524 struct held_lock *hlock;
1525
1526 /*
1527 * Debugging checks.
1528 *
1529 * Depth must not be zero for a non-head lock:
1530 */
1531 if (!depth)
1532 goto out_bug;
1533 /*
1534 * At least two relevant locks must exist for this
1535 * to be a head:
1536 */
1537 if (curr->held_locks[depth].irq_context !=
1538 curr->held_locks[depth-1].irq_context)
1539 goto out_bug;
1540
1541 for (;;) {
1542 int distance = curr->lockdep_depth - depth + 1;
1543 hlock = curr->held_locks + depth-1;
1544 /*
1545 * Only non-recursive-read entries get new dependencies
1546 * added:
1547 */
1548 if (hlock->read != 2) {
1549 if (!check_prev_add(curr, hlock, next, distance))
1550 return 0;
1551 /*
1552 * Stop after the first non-trylock entry,
1553 * as non-trylock entries have added their
1554 * own direct dependencies already, so this
1555 * lock is connected to them indirectly:
1556 */
1557 if (!hlock->trylock)
1558 break;
1559 }
1560 depth--;
1561 /*
1562 * End of lock-stack?
1563 */
1564 if (!depth)
1565 break;
1566 /*
1567 * Stop the search if we cross into another context:
1568 */
1569 if (curr->held_locks[depth].irq_context !=
1570 curr->held_locks[depth-1].irq_context)
1571 break;
1572 }
1573 return 1;
1574out_bug:
1575 if (!debug_locks_off_graph_unlock())
1576 return 0;
1577
1578 WARN_ON(1);
1579
1580 return 0;
1581}
1582
1583unsigned long nr_lock_chains;
Huang, Ying443cd502008-06-20 16:39:21 +08001584struct lock_chain lock_chains[MAX_LOCKDEP_CHAINS];
Huang, Yingcd1a28e2008-06-23 11:20:54 +08001585int nr_chain_hlocks;
Huang, Ying443cd502008-06-20 16:39:21 +08001586static u16 chain_hlocks[MAX_LOCKDEP_CHAIN_HLOCKS];
1587
1588struct lock_class *lock_chain_get_class(struct lock_chain *chain, int i)
1589{
1590 return lock_classes + chain_hlocks[chain->base + i];
1591}
Peter Zijlstra8e182572007-07-19 01:48:54 -07001592
1593/*
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001594 * Look up a dependency chain. If the key is not present yet then
Jarek Poplawski9e860d02007-05-08 00:30:12 -07001595 * add it and return 1 - in this case the new dependency chain is
1596 * validated. If the key is already hashed, return 0.
1597 * (On return with 1 graph_lock is held.)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001598 */
Huang, Ying443cd502008-06-20 16:39:21 +08001599static inline int lookup_chain_cache(struct task_struct *curr,
1600 struct held_lock *hlock,
1601 u64 chain_key)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001602{
Dave Jonesf82b2172008-08-11 09:30:23 +02001603 struct lock_class *class = hlock_class(hlock);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001604 struct list_head *hash_head = chainhashentry(chain_key);
1605 struct lock_chain *chain;
Huang, Ying443cd502008-06-20 16:39:21 +08001606 struct held_lock *hlock_curr, *hlock_next;
Huang, Yingcd1a28e2008-06-23 11:20:54 +08001607 int i, j, n, cn;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001608
Jarek Poplawski381a2292007-02-10 01:44:58 -08001609 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
1610 return 0;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001611 /*
1612 * We can walk it lock-free, because entries only get added
1613 * to the hash:
1614 */
1615 list_for_each_entry(chain, hash_head, entry) {
1616 if (chain->chain_key == chain_key) {
1617cache_hit:
1618 debug_atomic_inc(&chain_lookup_hits);
Ingo Molnar81fc6852006-12-13 00:34:40 -08001619 if (very_verbose(class))
Andrew Morton755cd902006-12-29 16:49:14 -08001620 printk("\nhash chain already cached, key: "
1621 "%016Lx tail class: [%p] %s\n",
1622 (unsigned long long)chain_key,
1623 class->key, class->name);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001624 return 0;
1625 }
1626 }
Ingo Molnar81fc6852006-12-13 00:34:40 -08001627 if (very_verbose(class))
Andrew Morton755cd902006-12-29 16:49:14 -08001628 printk("\nnew hash chain, key: %016Lx tail class: [%p] %s\n",
1629 (unsigned long long)chain_key, class->key, class->name);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001630 /*
1631 * Allocate a new chain entry from the static array, and add
1632 * it to the hash:
1633 */
Ingo Molnar74c383f2006-12-13 00:34:43 -08001634 if (!graph_lock())
1635 return 0;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001636 /*
1637 * We have to walk the chain again locked - to avoid duplicates:
1638 */
1639 list_for_each_entry(chain, hash_head, entry) {
1640 if (chain->chain_key == chain_key) {
Ingo Molnar74c383f2006-12-13 00:34:43 -08001641 graph_unlock();
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001642 goto cache_hit;
1643 }
1644 }
1645 if (unlikely(nr_lock_chains >= MAX_LOCKDEP_CHAINS)) {
Ingo Molnar74c383f2006-12-13 00:34:43 -08001646 if (!debug_locks_off_graph_unlock())
1647 return 0;
1648
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001649 printk("BUG: MAX_LOCKDEP_CHAINS too low!\n");
1650 printk("turning off the locking correctness validator.\n");
1651 return 0;
1652 }
1653 chain = lock_chains + nr_lock_chains++;
1654 chain->chain_key = chain_key;
Huang, Ying443cd502008-06-20 16:39:21 +08001655 chain->irq_context = hlock->irq_context;
1656 /* Find the first held_lock of current chain */
1657 hlock_next = hlock;
1658 for (i = curr->lockdep_depth - 1; i >= 0; i--) {
1659 hlock_curr = curr->held_locks + i;
1660 if (hlock_curr->irq_context != hlock_next->irq_context)
1661 break;
1662 hlock_next = hlock;
1663 }
1664 i++;
1665 chain->depth = curr->lockdep_depth + 1 - i;
Huang, Yingcd1a28e2008-06-23 11:20:54 +08001666 cn = nr_chain_hlocks;
1667 while (cn + chain->depth <= MAX_LOCKDEP_CHAIN_HLOCKS) {
1668 n = cmpxchg(&nr_chain_hlocks, cn, cn + chain->depth);
1669 if (n == cn)
1670 break;
1671 cn = n;
1672 }
1673 if (likely(cn + chain->depth <= MAX_LOCKDEP_CHAIN_HLOCKS)) {
1674 chain->base = cn;
Huang, Ying443cd502008-06-20 16:39:21 +08001675 for (j = 0; j < chain->depth - 1; j++, i++) {
Dave Jonesf82b2172008-08-11 09:30:23 +02001676 int lock_id = curr->held_locks[i].class_idx - 1;
Huang, Ying443cd502008-06-20 16:39:21 +08001677 chain_hlocks[chain->base + j] = lock_id;
1678 }
1679 chain_hlocks[chain->base + j] = class - lock_classes;
1680 }
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001681 list_add_tail_rcu(&chain->entry, hash_head);
1682 debug_atomic_inc(&chain_lookup_misses);
Peter Zijlstra8e182572007-07-19 01:48:54 -07001683 inc_chains();
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001684
1685 return 1;
1686}
Peter Zijlstra8e182572007-07-19 01:48:54 -07001687
1688static int validate_chain(struct task_struct *curr, struct lockdep_map *lock,
Johannes Berg4e6045f2007-10-18 23:39:55 -07001689 struct held_lock *hlock, int chain_head, u64 chain_key)
Peter Zijlstra8e182572007-07-19 01:48:54 -07001690{
1691 /*
1692 * Trylock needs to maintain the stack of held locks, but it
1693 * does not add new dependencies, because trylock can be done
1694 * in any order.
1695 *
1696 * We look up the chain_key and do the O(N^2) check and update of
1697 * the dependencies only if this is a new dependency chain.
1698 * (If lookup_chain_cache() returns with 1 it acquires
1699 * graph_lock for us)
1700 */
1701 if (!hlock->trylock && (hlock->check == 2) &&
Huang, Ying443cd502008-06-20 16:39:21 +08001702 lookup_chain_cache(curr, hlock, chain_key)) {
Peter Zijlstra8e182572007-07-19 01:48:54 -07001703 /*
1704 * Check whether last held lock:
1705 *
1706 * - is irq-safe, if this lock is irq-unsafe
1707 * - is softirq-safe, if this lock is hardirq-unsafe
1708 *
1709 * And check whether the new lock's dependency graph
1710 * could lead back to the previous lock.
1711 *
1712 * any of these scenarios could lead to a deadlock. If
1713 * All validations
1714 */
1715 int ret = check_deadlock(curr, hlock, lock, hlock->read);
1716
1717 if (!ret)
1718 return 0;
1719 /*
1720 * Mark recursive read, as we jump over it when
1721 * building dependencies (just like we jump over
1722 * trylock entries):
1723 */
1724 if (ret == 2)
1725 hlock->read = 2;
1726 /*
1727 * Add dependency only if this lock is not the head
1728 * of the chain, and if it's not a secondary read-lock:
1729 */
1730 if (!chain_head && ret != 2)
1731 if (!check_prevs_add(curr, hlock))
1732 return 0;
1733 graph_unlock();
1734 } else
1735 /* after lookup_chain_cache(): */
1736 if (unlikely(!debug_locks))
1737 return 0;
1738
1739 return 1;
1740}
1741#else
1742static inline int validate_chain(struct task_struct *curr,
1743 struct lockdep_map *lock, struct held_lock *hlock,
Gregory Haskins3aa416b2007-10-11 22:11:11 +02001744 int chain_head, u64 chain_key)
Peter Zijlstra8e182572007-07-19 01:48:54 -07001745{
1746 return 1;
1747}
Peter Zijlstraca58abc2007-07-19 01:48:53 -07001748#endif
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001749
1750/*
1751 * We are building curr_chain_key incrementally, so double-check
1752 * it from scratch, to make sure that it's done correctly:
1753 */
Steven Rostedt1d09daa2008-05-12 21:20:55 +02001754static void check_chain_key(struct task_struct *curr)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001755{
1756#ifdef CONFIG_DEBUG_LOCKDEP
1757 struct held_lock *hlock, *prev_hlock = NULL;
1758 unsigned int i, id;
1759 u64 chain_key = 0;
1760
1761 for (i = 0; i < curr->lockdep_depth; i++) {
1762 hlock = curr->held_locks + i;
1763 if (chain_key != hlock->prev_chain_key) {
1764 debug_locks_off();
Arjan van de Ven2df8b1d2008-07-30 12:43:11 -07001765 WARN(1, "hm#1, depth: %u [%u], %016Lx != %016Lx\n",
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001766 curr->lockdep_depth, i,
1767 (unsigned long long)chain_key,
1768 (unsigned long long)hlock->prev_chain_key);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001769 return;
1770 }
Dave Jonesf82b2172008-08-11 09:30:23 +02001771 id = hlock->class_idx - 1;
Jarek Poplawski381a2292007-02-10 01:44:58 -08001772 if (DEBUG_LOCKS_WARN_ON(id >= MAX_LOCKDEP_KEYS))
1773 return;
1774
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001775 if (prev_hlock && (prev_hlock->irq_context !=
1776 hlock->irq_context))
1777 chain_key = 0;
1778 chain_key = iterate_chain_key(chain_key, id);
1779 prev_hlock = hlock;
1780 }
1781 if (chain_key != curr->curr_chain_key) {
1782 debug_locks_off();
Arjan van de Ven2df8b1d2008-07-30 12:43:11 -07001783 WARN(1, "hm#2, depth: %u [%u], %016Lx != %016Lx\n",
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001784 curr->lockdep_depth, i,
1785 (unsigned long long)chain_key,
1786 (unsigned long long)curr->curr_chain_key);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001787 }
1788#endif
1789}
1790
Peter Zijlstra8e182572007-07-19 01:48:54 -07001791static int
1792print_usage_bug(struct task_struct *curr, struct held_lock *this,
1793 enum lock_usage_bit prev_bit, enum lock_usage_bit new_bit)
1794{
1795 if (!debug_locks_off_graph_unlock() || debug_locks_silent)
1796 return 0;
1797
1798 printk("\n=================================\n");
1799 printk( "[ INFO: inconsistent lock state ]\n");
1800 print_kernel_version();
1801 printk( "---------------------------------\n");
1802
1803 printk("inconsistent {%s} -> {%s} usage.\n",
1804 usage_str[prev_bit], usage_str[new_bit]);
1805
1806 printk("%s/%d [HC%u[%lu]:SC%u[%lu]:HE%u:SE%u] takes:\n",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07001807 curr->comm, task_pid_nr(curr),
Peter Zijlstra8e182572007-07-19 01:48:54 -07001808 trace_hardirq_context(curr), hardirq_count() >> HARDIRQ_SHIFT,
1809 trace_softirq_context(curr), softirq_count() >> SOFTIRQ_SHIFT,
1810 trace_hardirqs_enabled(curr),
1811 trace_softirqs_enabled(curr));
1812 print_lock(this);
1813
1814 printk("{%s} state was registered at:\n", usage_str[prev_bit]);
Dave Jonesf82b2172008-08-11 09:30:23 +02001815 print_stack_trace(hlock_class(this)->usage_traces + prev_bit, 1);
Peter Zijlstra8e182572007-07-19 01:48:54 -07001816
1817 print_irqtrace_events(curr);
1818 printk("\nother info that might help us debug this:\n");
1819 lockdep_print_held_locks(curr);
1820
1821 printk("\nstack backtrace:\n");
1822 dump_stack();
1823
1824 return 0;
1825}
1826
1827/*
1828 * Print out an error if an invalid bit is set:
1829 */
1830static inline int
1831valid_state(struct task_struct *curr, struct held_lock *this,
1832 enum lock_usage_bit new_bit, enum lock_usage_bit bad_bit)
1833{
Dave Jonesf82b2172008-08-11 09:30:23 +02001834 if (unlikely(hlock_class(this)->usage_mask & (1 << bad_bit)))
Peter Zijlstra8e182572007-07-19 01:48:54 -07001835 return print_usage_bug(curr, this, bad_bit, new_bit);
1836 return 1;
1837}
1838
1839static int mark_lock(struct task_struct *curr, struct held_lock *this,
1840 enum lock_usage_bit new_bit);
1841
Steven Rostedt81d68a92008-05-12 21:20:42 +02001842#if defined(CONFIG_TRACE_IRQFLAGS) && defined(CONFIG_PROVE_LOCKING)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001843
1844/*
1845 * print irq inversion bug:
1846 */
1847static int
1848print_irq_inversion_bug(struct task_struct *curr, struct lock_class *other,
1849 struct held_lock *this, int forwards,
1850 const char *irqclass)
1851{
Ingo Molnar74c383f2006-12-13 00:34:43 -08001852 if (!debug_locks_off_graph_unlock() || debug_locks_silent)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001853 return 0;
1854
1855 printk("\n=========================================================\n");
1856 printk( "[ INFO: possible irq lock inversion dependency detected ]\n");
Dave Jones99de0552006-09-29 02:00:10 -07001857 print_kernel_version();
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001858 printk( "---------------------------------------------------------\n");
1859 printk("%s/%d just changed the state of lock:\n",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07001860 curr->comm, task_pid_nr(curr));
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001861 print_lock(this);
1862 if (forwards)
1863 printk("but this lock took another, %s-irq-unsafe lock in the past:\n", irqclass);
1864 else
1865 printk("but this lock was taken by another, %s-irq-safe lock in the past:\n", irqclass);
1866 print_lock_name(other);
1867 printk("\n\nand interrupts could create inverse lock ordering between them.\n\n");
1868
1869 printk("\nother info that might help us debug this:\n");
1870 lockdep_print_held_locks(curr);
1871
1872 printk("\nthe first lock's dependencies:\n");
Dave Jonesf82b2172008-08-11 09:30:23 +02001873 print_lock_dependencies(hlock_class(this), 0);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001874
1875 printk("\nthe second lock's dependencies:\n");
1876 print_lock_dependencies(other, 0);
1877
1878 printk("\nstack backtrace:\n");
1879 dump_stack();
1880
1881 return 0;
1882}
1883
1884/*
1885 * Prove that in the forwards-direction subgraph starting at <this>
1886 * there is no lock matching <mask>:
1887 */
1888static int
1889check_usage_forwards(struct task_struct *curr, struct held_lock *this,
1890 enum lock_usage_bit bit, const char *irqclass)
1891{
1892 int ret;
1893
1894 find_usage_bit = bit;
1895 /* fills in <forwards_match> */
Dave Jonesf82b2172008-08-11 09:30:23 +02001896 ret = find_usage_forwards(hlock_class(this), 0);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001897 if (!ret || ret == 1)
1898 return ret;
1899
1900 return print_irq_inversion_bug(curr, forwards_match, this, 1, irqclass);
1901}
1902
1903/*
1904 * Prove that in the backwards-direction subgraph starting at <this>
1905 * there is no lock matching <mask>:
1906 */
1907static int
1908check_usage_backwards(struct task_struct *curr, struct held_lock *this,
1909 enum lock_usage_bit bit, const char *irqclass)
1910{
1911 int ret;
1912
1913 find_usage_bit = bit;
1914 /* fills in <backwards_match> */
Dave Jonesf82b2172008-08-11 09:30:23 +02001915 ret = find_usage_backwards(hlock_class(this), 0);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001916 if (!ret || ret == 1)
1917 return ret;
1918
1919 return print_irq_inversion_bug(curr, backwards_match, this, 0, irqclass);
1920}
1921
Ingo Molnar3117df02006-12-13 00:34:43 -08001922void print_irqtrace_events(struct task_struct *curr)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001923{
1924 printk("irq event stamp: %u\n", curr->irq_events);
1925 printk("hardirqs last enabled at (%u): ", curr->hardirq_enable_event);
1926 print_ip_sym(curr->hardirq_enable_ip);
1927 printk("hardirqs last disabled at (%u): ", curr->hardirq_disable_event);
1928 print_ip_sym(curr->hardirq_disable_ip);
1929 printk("softirqs last enabled at (%u): ", curr->softirq_enable_event);
1930 print_ip_sym(curr->softirq_enable_ip);
1931 printk("softirqs last disabled at (%u): ", curr->softirq_disable_event);
1932 print_ip_sym(curr->softirq_disable_ip);
1933}
1934
Peter Zijlstra8e182572007-07-19 01:48:54 -07001935static int hardirq_verbose(struct lock_class *class)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001936{
Peter Zijlstra8e182572007-07-19 01:48:54 -07001937#if HARDIRQ_VERBOSE
1938 return class_filter(class);
1939#endif
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001940 return 0;
1941}
1942
Peter Zijlstra8e182572007-07-19 01:48:54 -07001943static int softirq_verbose(struct lock_class *class)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001944{
Peter Zijlstra8e182572007-07-19 01:48:54 -07001945#if SOFTIRQ_VERBOSE
1946 return class_filter(class);
1947#endif
1948 return 0;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001949}
1950
1951#define STRICT_READ_CHECKS 1
1952
Peter Zijlstra8e182572007-07-19 01:48:54 -07001953static int mark_lock_irq(struct task_struct *curr, struct held_lock *this,
1954 enum lock_usage_bit new_bit)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001955{
Peter Zijlstra8e182572007-07-19 01:48:54 -07001956 int ret = 1;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001957
Peter Zijlstra8e182572007-07-19 01:48:54 -07001958 switch(new_bit) {
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001959 case LOCK_USED_IN_HARDIRQ:
1960 if (!valid_state(curr, this, new_bit, LOCK_ENABLED_HARDIRQS))
1961 return 0;
1962 if (!valid_state(curr, this, new_bit,
1963 LOCK_ENABLED_HARDIRQS_READ))
1964 return 0;
1965 /*
1966 * just marked it hardirq-safe, check that this lock
1967 * took no hardirq-unsafe lock in the past:
1968 */
1969 if (!check_usage_forwards(curr, this,
1970 LOCK_ENABLED_HARDIRQS, "hard"))
1971 return 0;
1972#if STRICT_READ_CHECKS
1973 /*
1974 * just marked it hardirq-safe, check that this lock
1975 * took no hardirq-unsafe-read lock in the past:
1976 */
1977 if (!check_usage_forwards(curr, this,
1978 LOCK_ENABLED_HARDIRQS_READ, "hard-read"))
1979 return 0;
1980#endif
Dave Jonesf82b2172008-08-11 09:30:23 +02001981 if (hardirq_verbose(hlock_class(this)))
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001982 ret = 2;
1983 break;
1984 case LOCK_USED_IN_SOFTIRQ:
1985 if (!valid_state(curr, this, new_bit, LOCK_ENABLED_SOFTIRQS))
1986 return 0;
1987 if (!valid_state(curr, this, new_bit,
1988 LOCK_ENABLED_SOFTIRQS_READ))
1989 return 0;
1990 /*
1991 * just marked it softirq-safe, check that this lock
1992 * took no softirq-unsafe lock in the past:
1993 */
1994 if (!check_usage_forwards(curr, this,
1995 LOCK_ENABLED_SOFTIRQS, "soft"))
1996 return 0;
1997#if STRICT_READ_CHECKS
1998 /*
1999 * just marked it softirq-safe, check that this lock
2000 * took no softirq-unsafe-read lock in the past:
2001 */
2002 if (!check_usage_forwards(curr, this,
2003 LOCK_ENABLED_SOFTIRQS_READ, "soft-read"))
2004 return 0;
2005#endif
Dave Jonesf82b2172008-08-11 09:30:23 +02002006 if (softirq_verbose(hlock_class(this)))
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002007 ret = 2;
2008 break;
2009 case LOCK_USED_IN_HARDIRQ_READ:
2010 if (!valid_state(curr, this, new_bit, LOCK_ENABLED_HARDIRQS))
2011 return 0;
2012 /*
2013 * just marked it hardirq-read-safe, check that this lock
2014 * took no hardirq-unsafe lock in the past:
2015 */
2016 if (!check_usage_forwards(curr, this,
2017 LOCK_ENABLED_HARDIRQS, "hard"))
2018 return 0;
Dave Jonesf82b2172008-08-11 09:30:23 +02002019 if (hardirq_verbose(hlock_class(this)))
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002020 ret = 2;
2021 break;
2022 case LOCK_USED_IN_SOFTIRQ_READ:
2023 if (!valid_state(curr, this, new_bit, LOCK_ENABLED_SOFTIRQS))
2024 return 0;
2025 /*
2026 * just marked it softirq-read-safe, check that this lock
2027 * took no softirq-unsafe lock in the past:
2028 */
2029 if (!check_usage_forwards(curr, this,
2030 LOCK_ENABLED_SOFTIRQS, "soft"))
2031 return 0;
Dave Jonesf82b2172008-08-11 09:30:23 +02002032 if (softirq_verbose(hlock_class(this)))
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002033 ret = 2;
2034 break;
2035 case LOCK_ENABLED_HARDIRQS:
2036 if (!valid_state(curr, this, new_bit, LOCK_USED_IN_HARDIRQ))
2037 return 0;
2038 if (!valid_state(curr, this, new_bit,
2039 LOCK_USED_IN_HARDIRQ_READ))
2040 return 0;
2041 /*
2042 * just marked it hardirq-unsafe, check that no hardirq-safe
2043 * lock in the system ever took it in the past:
2044 */
2045 if (!check_usage_backwards(curr, this,
2046 LOCK_USED_IN_HARDIRQ, "hard"))
2047 return 0;
2048#if STRICT_READ_CHECKS
2049 /*
2050 * just marked it hardirq-unsafe, check that no
2051 * hardirq-safe-read lock in the system ever took
2052 * it in the past:
2053 */
2054 if (!check_usage_backwards(curr, this,
2055 LOCK_USED_IN_HARDIRQ_READ, "hard-read"))
2056 return 0;
2057#endif
Dave Jonesf82b2172008-08-11 09:30:23 +02002058 if (hardirq_verbose(hlock_class(this)))
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002059 ret = 2;
2060 break;
2061 case LOCK_ENABLED_SOFTIRQS:
2062 if (!valid_state(curr, this, new_bit, LOCK_USED_IN_SOFTIRQ))
2063 return 0;
2064 if (!valid_state(curr, this, new_bit,
2065 LOCK_USED_IN_SOFTIRQ_READ))
2066 return 0;
2067 /*
2068 * just marked it softirq-unsafe, check that no softirq-safe
2069 * lock in the system ever took it in the past:
2070 */
2071 if (!check_usage_backwards(curr, this,
2072 LOCK_USED_IN_SOFTIRQ, "soft"))
2073 return 0;
2074#if STRICT_READ_CHECKS
2075 /*
2076 * just marked it softirq-unsafe, check that no
2077 * softirq-safe-read lock in the system ever took
2078 * it in the past:
2079 */
2080 if (!check_usage_backwards(curr, this,
2081 LOCK_USED_IN_SOFTIRQ_READ, "soft-read"))
2082 return 0;
2083#endif
Dave Jonesf82b2172008-08-11 09:30:23 +02002084 if (softirq_verbose(hlock_class(this)))
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002085 ret = 2;
2086 break;
2087 case LOCK_ENABLED_HARDIRQS_READ:
2088 if (!valid_state(curr, this, new_bit, LOCK_USED_IN_HARDIRQ))
2089 return 0;
2090#if STRICT_READ_CHECKS
2091 /*
2092 * just marked it hardirq-read-unsafe, check that no
2093 * hardirq-safe lock in the system ever took it in the past:
2094 */
2095 if (!check_usage_backwards(curr, this,
2096 LOCK_USED_IN_HARDIRQ, "hard"))
2097 return 0;
2098#endif
Dave Jonesf82b2172008-08-11 09:30:23 +02002099 if (hardirq_verbose(hlock_class(this)))
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002100 ret = 2;
2101 break;
2102 case LOCK_ENABLED_SOFTIRQS_READ:
2103 if (!valid_state(curr, this, new_bit, LOCK_USED_IN_SOFTIRQ))
2104 return 0;
2105#if STRICT_READ_CHECKS
2106 /*
2107 * just marked it softirq-read-unsafe, check that no
2108 * softirq-safe lock in the system ever took it in the past:
2109 */
2110 if (!check_usage_backwards(curr, this,
2111 LOCK_USED_IN_SOFTIRQ, "soft"))
2112 return 0;
2113#endif
Dave Jonesf82b2172008-08-11 09:30:23 +02002114 if (softirq_verbose(hlock_class(this)))
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002115 ret = 2;
2116 break;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002117 default:
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002118 WARN_ON(1);
Peter Zijlstra8e182572007-07-19 01:48:54 -07002119 break;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002120 }
2121
2122 return ret;
2123}
2124
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002125/*
2126 * Mark all held locks with a usage bit:
2127 */
Steven Rostedt1d09daa2008-05-12 21:20:55 +02002128static int
Jarek Poplawski4ff773bb2007-05-08 00:31:00 -07002129mark_held_locks(struct task_struct *curr, int hardirq)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002130{
2131 enum lock_usage_bit usage_bit;
2132 struct held_lock *hlock;
2133 int i;
2134
2135 for (i = 0; i < curr->lockdep_depth; i++) {
2136 hlock = curr->held_locks + i;
2137
2138 if (hardirq) {
2139 if (hlock->read)
2140 usage_bit = LOCK_ENABLED_HARDIRQS_READ;
2141 else
2142 usage_bit = LOCK_ENABLED_HARDIRQS;
2143 } else {
2144 if (hlock->read)
2145 usage_bit = LOCK_ENABLED_SOFTIRQS_READ;
2146 else
2147 usage_bit = LOCK_ENABLED_SOFTIRQS;
2148 }
Jarek Poplawski4ff773bb2007-05-08 00:31:00 -07002149 if (!mark_lock(curr, hlock, usage_bit))
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002150 return 0;
2151 }
2152
2153 return 1;
2154}
2155
2156/*
2157 * Debugging helper: via this flag we know that we are in
2158 * 'early bootup code', and will warn about any invalid irqs-on event:
2159 */
2160static int early_boot_irqs_enabled;
2161
2162void early_boot_irqs_off(void)
2163{
2164 early_boot_irqs_enabled = 0;
2165}
2166
2167void early_boot_irqs_on(void)
2168{
2169 early_boot_irqs_enabled = 1;
2170}
2171
2172/*
2173 * Hardirqs will be enabled:
2174 */
Heiko Carstens6afe40b2008-10-28 11:14:58 +01002175void trace_hardirqs_on_caller(unsigned long ip)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002176{
2177 struct task_struct *curr = current;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002178
Heiko Carstens6afe40b2008-10-28 11:14:58 +01002179 time_hardirqs_on(CALLER_ADDR0, ip);
Steven Rostedt81d68a92008-05-12 21:20:42 +02002180
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002181 if (unlikely(!debug_locks || current->lockdep_recursion))
2182 return;
2183
2184 if (DEBUG_LOCKS_WARN_ON(unlikely(!early_boot_irqs_enabled)))
2185 return;
2186
2187 if (unlikely(curr->hardirqs_enabled)) {
2188 debug_atomic_inc(&redundant_hardirqs_on);
2189 return;
2190 }
2191 /* we'll do an OFF -> ON transition: */
2192 curr->hardirqs_enabled = 1;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002193
2194 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2195 return;
2196 if (DEBUG_LOCKS_WARN_ON(current->hardirq_context))
2197 return;
2198 /*
2199 * We are going to turn hardirqs on, so set the
2200 * usage bit for all held locks:
2201 */
Jarek Poplawski4ff773bb2007-05-08 00:31:00 -07002202 if (!mark_held_locks(curr, 1))
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002203 return;
2204 /*
2205 * If we have softirqs enabled, then set the usage
2206 * bit for all held locks. (disabled hardirqs prevented
2207 * this bit from being set before)
2208 */
2209 if (curr->softirqs_enabled)
Jarek Poplawski4ff773bb2007-05-08 00:31:00 -07002210 if (!mark_held_locks(curr, 0))
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002211 return;
2212
2213 curr->hardirq_enable_ip = ip;
2214 curr->hardirq_enable_event = ++curr->irq_events;
2215 debug_atomic_inc(&hardirqs_on_events);
2216}
Steven Rostedt81d68a92008-05-12 21:20:42 +02002217EXPORT_SYMBOL(trace_hardirqs_on_caller);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002218
Steven Rostedt1d09daa2008-05-12 21:20:55 +02002219void trace_hardirqs_on(void)
Steven Rostedt81d68a92008-05-12 21:20:42 +02002220{
2221 trace_hardirqs_on_caller(CALLER_ADDR0);
2222}
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002223EXPORT_SYMBOL(trace_hardirqs_on);
2224
2225/*
2226 * Hardirqs were disabled:
2227 */
Heiko Carstens6afe40b2008-10-28 11:14:58 +01002228void trace_hardirqs_off_caller(unsigned long ip)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002229{
2230 struct task_struct *curr = current;
2231
Heiko Carstens6afe40b2008-10-28 11:14:58 +01002232 time_hardirqs_off(CALLER_ADDR0, ip);
Steven Rostedt81d68a92008-05-12 21:20:42 +02002233
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002234 if (unlikely(!debug_locks || current->lockdep_recursion))
2235 return;
2236
2237 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2238 return;
2239
2240 if (curr->hardirqs_enabled) {
2241 /*
2242 * We have done an ON -> OFF transition:
2243 */
2244 curr->hardirqs_enabled = 0;
Heiko Carstens6afe40b2008-10-28 11:14:58 +01002245 curr->hardirq_disable_ip = ip;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002246 curr->hardirq_disable_event = ++curr->irq_events;
2247 debug_atomic_inc(&hardirqs_off_events);
2248 } else
2249 debug_atomic_inc(&redundant_hardirqs_off);
2250}
Steven Rostedt81d68a92008-05-12 21:20:42 +02002251EXPORT_SYMBOL(trace_hardirqs_off_caller);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002252
Steven Rostedt1d09daa2008-05-12 21:20:55 +02002253void trace_hardirqs_off(void)
Steven Rostedt81d68a92008-05-12 21:20:42 +02002254{
2255 trace_hardirqs_off_caller(CALLER_ADDR0);
2256}
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002257EXPORT_SYMBOL(trace_hardirqs_off);
2258
2259/*
2260 * Softirqs will be enabled:
2261 */
2262void trace_softirqs_on(unsigned long ip)
2263{
2264 struct task_struct *curr = current;
2265
2266 if (unlikely(!debug_locks))
2267 return;
2268
2269 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2270 return;
2271
2272 if (curr->softirqs_enabled) {
2273 debug_atomic_inc(&redundant_softirqs_on);
2274 return;
2275 }
2276
2277 /*
2278 * We'll do an OFF -> ON transition:
2279 */
2280 curr->softirqs_enabled = 1;
2281 curr->softirq_enable_ip = ip;
2282 curr->softirq_enable_event = ++curr->irq_events;
2283 debug_atomic_inc(&softirqs_on_events);
2284 /*
2285 * We are going to turn softirqs on, so set the
2286 * usage bit for all held locks, if hardirqs are
2287 * enabled too:
2288 */
2289 if (curr->hardirqs_enabled)
Jarek Poplawski4ff773bb2007-05-08 00:31:00 -07002290 mark_held_locks(curr, 0);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002291}
2292
2293/*
2294 * Softirqs were disabled:
2295 */
2296void trace_softirqs_off(unsigned long ip)
2297{
2298 struct task_struct *curr = current;
2299
2300 if (unlikely(!debug_locks))
2301 return;
2302
2303 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2304 return;
2305
2306 if (curr->softirqs_enabled) {
2307 /*
2308 * We have done an ON -> OFF transition:
2309 */
2310 curr->softirqs_enabled = 0;
2311 curr->softirq_disable_ip = ip;
2312 curr->softirq_disable_event = ++curr->irq_events;
2313 debug_atomic_inc(&softirqs_off_events);
2314 DEBUG_LOCKS_WARN_ON(!softirq_count());
2315 } else
2316 debug_atomic_inc(&redundant_softirqs_off);
2317}
2318
Peter Zijlstra8e182572007-07-19 01:48:54 -07002319static int mark_irqflags(struct task_struct *curr, struct held_lock *hlock)
2320{
2321 /*
2322 * If non-trylock use in a hardirq or softirq context, then
2323 * mark the lock as used in these contexts:
2324 */
2325 if (!hlock->trylock) {
2326 if (hlock->read) {
2327 if (curr->hardirq_context)
2328 if (!mark_lock(curr, hlock,
2329 LOCK_USED_IN_HARDIRQ_READ))
2330 return 0;
2331 if (curr->softirq_context)
2332 if (!mark_lock(curr, hlock,
2333 LOCK_USED_IN_SOFTIRQ_READ))
2334 return 0;
2335 } else {
2336 if (curr->hardirq_context)
2337 if (!mark_lock(curr, hlock, LOCK_USED_IN_HARDIRQ))
2338 return 0;
2339 if (curr->softirq_context)
2340 if (!mark_lock(curr, hlock, LOCK_USED_IN_SOFTIRQ))
2341 return 0;
2342 }
2343 }
2344 if (!hlock->hardirqs_off) {
2345 if (hlock->read) {
2346 if (!mark_lock(curr, hlock,
2347 LOCK_ENABLED_HARDIRQS_READ))
2348 return 0;
2349 if (curr->softirqs_enabled)
2350 if (!mark_lock(curr, hlock,
2351 LOCK_ENABLED_SOFTIRQS_READ))
2352 return 0;
2353 } else {
2354 if (!mark_lock(curr, hlock,
2355 LOCK_ENABLED_HARDIRQS))
2356 return 0;
2357 if (curr->softirqs_enabled)
2358 if (!mark_lock(curr, hlock,
2359 LOCK_ENABLED_SOFTIRQS))
2360 return 0;
2361 }
2362 }
2363
2364 return 1;
2365}
2366
2367static int separate_irq_context(struct task_struct *curr,
2368 struct held_lock *hlock)
2369{
2370 unsigned int depth = curr->lockdep_depth;
2371
2372 /*
2373 * Keep track of points where we cross into an interrupt context:
2374 */
2375 hlock->irq_context = 2*(curr->hardirq_context ? 1 : 0) +
2376 curr->softirq_context;
2377 if (depth) {
2378 struct held_lock *prev_hlock;
2379
2380 prev_hlock = curr->held_locks + depth-1;
2381 /*
2382 * If we cross into another context, reset the
2383 * hash key (this also prevents the checking and the
2384 * adding of the dependency to 'prev'):
2385 */
2386 if (prev_hlock->irq_context != hlock->irq_context)
2387 return 1;
2388 }
2389 return 0;
2390}
2391
2392#else
2393
2394static inline
2395int mark_lock_irq(struct task_struct *curr, struct held_lock *this,
2396 enum lock_usage_bit new_bit)
2397{
2398 WARN_ON(1);
2399 return 1;
2400}
2401
2402static inline int mark_irqflags(struct task_struct *curr,
2403 struct held_lock *hlock)
2404{
2405 return 1;
2406}
2407
2408static inline int separate_irq_context(struct task_struct *curr,
2409 struct held_lock *hlock)
2410{
2411 return 0;
2412}
2413
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002414#endif
2415
2416/*
Peter Zijlstra8e182572007-07-19 01:48:54 -07002417 * Mark a lock with a usage bit, and validate the state transition:
2418 */
Steven Rostedt1d09daa2008-05-12 21:20:55 +02002419static int mark_lock(struct task_struct *curr, struct held_lock *this,
Steven Rostedt0764d232008-05-12 21:20:44 +02002420 enum lock_usage_bit new_bit)
Peter Zijlstra8e182572007-07-19 01:48:54 -07002421{
2422 unsigned int new_mask = 1 << new_bit, ret = 1;
2423
2424 /*
2425 * If already set then do not dirty the cacheline,
2426 * nor do any checks:
2427 */
Dave Jonesf82b2172008-08-11 09:30:23 +02002428 if (likely(hlock_class(this)->usage_mask & new_mask))
Peter Zijlstra8e182572007-07-19 01:48:54 -07002429 return 1;
2430
2431 if (!graph_lock())
2432 return 0;
2433 /*
2434 * Make sure we didnt race:
2435 */
Dave Jonesf82b2172008-08-11 09:30:23 +02002436 if (unlikely(hlock_class(this)->usage_mask & new_mask)) {
Peter Zijlstra8e182572007-07-19 01:48:54 -07002437 graph_unlock();
2438 return 1;
2439 }
2440
Dave Jonesf82b2172008-08-11 09:30:23 +02002441 hlock_class(this)->usage_mask |= new_mask;
Peter Zijlstra8e182572007-07-19 01:48:54 -07002442
Dave Jonesf82b2172008-08-11 09:30:23 +02002443 if (!save_trace(hlock_class(this)->usage_traces + new_bit))
Peter Zijlstra8e182572007-07-19 01:48:54 -07002444 return 0;
2445
2446 switch (new_bit) {
2447 case LOCK_USED_IN_HARDIRQ:
2448 case LOCK_USED_IN_SOFTIRQ:
2449 case LOCK_USED_IN_HARDIRQ_READ:
2450 case LOCK_USED_IN_SOFTIRQ_READ:
2451 case LOCK_ENABLED_HARDIRQS:
2452 case LOCK_ENABLED_SOFTIRQS:
2453 case LOCK_ENABLED_HARDIRQS_READ:
2454 case LOCK_ENABLED_SOFTIRQS_READ:
2455 ret = mark_lock_irq(curr, this, new_bit);
2456 if (!ret)
2457 return 0;
2458 break;
2459 case LOCK_USED:
Peter Zijlstra8e182572007-07-19 01:48:54 -07002460 debug_atomic_dec(&nr_unused_locks);
2461 break;
2462 default:
2463 if (!debug_locks_off_graph_unlock())
2464 return 0;
2465 WARN_ON(1);
2466 return 0;
2467 }
2468
2469 graph_unlock();
2470
2471 /*
2472 * We must printk outside of the graph_lock:
2473 */
2474 if (ret == 2) {
2475 printk("\nmarked lock as {%s}:\n", usage_str[new_bit]);
2476 print_lock(this);
2477 print_irqtrace_events(curr);
2478 dump_stack();
2479 }
2480
2481 return ret;
2482}
2483
2484/*
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002485 * Initialize a lock instance's lock-class mapping info:
2486 */
2487void lockdep_init_map(struct lockdep_map *lock, const char *name,
Peter Zijlstra4dfbb9d2006-10-11 01:45:14 -04002488 struct lock_class_key *key, int subclass)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002489{
2490 if (unlikely(!debug_locks))
2491 return;
2492
2493 if (DEBUG_LOCKS_WARN_ON(!key))
2494 return;
2495 if (DEBUG_LOCKS_WARN_ON(!name))
2496 return;
2497 /*
2498 * Sanity check, the lock-class key must be persistent:
2499 */
2500 if (!static_obj(key)) {
2501 printk("BUG: key %p not in .data!\n", key);
2502 DEBUG_LOCKS_WARN_ON(1);
2503 return;
2504 }
2505 lock->name = name;
2506 lock->key = key;
Ingo Molnard6d897c2006-07-10 04:44:04 -07002507 lock->class_cache = NULL;
Peter Zijlstra96645672007-07-19 01:49:00 -07002508#ifdef CONFIG_LOCK_STAT
2509 lock->cpu = raw_smp_processor_id();
2510#endif
Peter Zijlstra4dfbb9d2006-10-11 01:45:14 -04002511 if (subclass)
2512 register_lock_class(lock, subclass, 1);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002513}
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002514EXPORT_SYMBOL_GPL(lockdep_init_map);
2515
2516/*
2517 * This gets called for every mutex_lock*()/spin_lock*() operation.
2518 * We maintain the dependency maps and validate the locking attempt:
2519 */
2520static int __lock_acquire(struct lockdep_map *lock, unsigned int subclass,
2521 int trylock, int read, int check, int hardirqs_off,
Peter Zijlstra7531e2f2008-08-11 09:30:24 +02002522 struct lockdep_map *nest_lock, unsigned long ip)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002523{
2524 struct task_struct *curr = current;
Ingo Molnard6d897c2006-07-10 04:44:04 -07002525 struct lock_class *class = NULL;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002526 struct held_lock *hlock;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002527 unsigned int depth, id;
2528 int chain_head = 0;
2529 u64 chain_key;
2530
Peter Zijlstraf20786f2007-07-19 01:48:56 -07002531 if (!prove_locking)
2532 check = 1;
2533
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002534 if (unlikely(!debug_locks))
2535 return 0;
2536
2537 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2538 return 0;
2539
2540 if (unlikely(subclass >= MAX_LOCKDEP_SUBCLASSES)) {
2541 debug_locks_off();
2542 printk("BUG: MAX_LOCKDEP_SUBCLASSES too low!\n");
2543 printk("turning off the locking correctness validator.\n");
2544 return 0;
2545 }
2546
Ingo Molnard6d897c2006-07-10 04:44:04 -07002547 if (!subclass)
2548 class = lock->class_cache;
2549 /*
2550 * Not cached yet or subclass?
2551 */
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002552 if (unlikely(!class)) {
Peter Zijlstra4dfbb9d2006-10-11 01:45:14 -04002553 class = register_lock_class(lock, subclass, 0);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002554 if (!class)
2555 return 0;
2556 }
2557 debug_atomic_inc((atomic_t *)&class->ops);
2558 if (very_verbose(class)) {
2559 printk("\nacquire class [%p] %s", class->key, class->name);
2560 if (class->name_version > 1)
2561 printk("#%d", class->name_version);
2562 printk("\n");
2563 dump_stack();
2564 }
2565
2566 /*
2567 * Add the lock to the list of currently held locks.
2568 * (we dont increase the depth just yet, up until the
2569 * dependency checks are done)
2570 */
2571 depth = curr->lockdep_depth;
2572 if (DEBUG_LOCKS_WARN_ON(depth >= MAX_LOCK_DEPTH))
2573 return 0;
2574
2575 hlock = curr->held_locks + depth;
Dave Jonesf82b2172008-08-11 09:30:23 +02002576 if (DEBUG_LOCKS_WARN_ON(!class))
2577 return 0;
2578 hlock->class_idx = class - lock_classes + 1;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002579 hlock->acquire_ip = ip;
2580 hlock->instance = lock;
Peter Zijlstra7531e2f2008-08-11 09:30:24 +02002581 hlock->nest_lock = nest_lock;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002582 hlock->trylock = trylock;
2583 hlock->read = read;
2584 hlock->check = check;
Dmitry Baryshkov6951b122008-08-18 04:26:37 +04002585 hlock->hardirqs_off = !!hardirqs_off;
Peter Zijlstraf20786f2007-07-19 01:48:56 -07002586#ifdef CONFIG_LOCK_STAT
2587 hlock->waittime_stamp = 0;
2588 hlock->holdtime_stamp = sched_clock();
2589#endif
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002590
Peter Zijlstra8e182572007-07-19 01:48:54 -07002591 if (check == 2 && !mark_irqflags(curr, hlock))
2592 return 0;
2593
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002594 /* mark it as used: */
Jarek Poplawski4ff773bb2007-05-08 00:31:00 -07002595 if (!mark_lock(curr, hlock, LOCK_USED))
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002596 return 0;
Peter Zijlstra8e182572007-07-19 01:48:54 -07002597
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002598 /*
Gautham R Shenoy17aacfb2007-10-28 20:47:01 +01002599 * Calculate the chain hash: it's the combined hash of all the
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002600 * lock keys along the dependency chain. We save the hash value
2601 * at every step so that we can get the current hash easily
2602 * after unlock. The chain hash is then used to cache dependency
2603 * results.
2604 *
2605 * The 'key ID' is what is the most compact key value to drive
2606 * the hash, not class->key.
2607 */
2608 id = class - lock_classes;
2609 if (DEBUG_LOCKS_WARN_ON(id >= MAX_LOCKDEP_KEYS))
2610 return 0;
2611
2612 chain_key = curr->curr_chain_key;
2613 if (!depth) {
2614 if (DEBUG_LOCKS_WARN_ON(chain_key != 0))
2615 return 0;
2616 chain_head = 1;
2617 }
2618
2619 hlock->prev_chain_key = chain_key;
Peter Zijlstra8e182572007-07-19 01:48:54 -07002620 if (separate_irq_context(curr, hlock)) {
2621 chain_key = 0;
2622 chain_head = 1;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002623 }
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002624 chain_key = iterate_chain_key(chain_key, id);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002625
Gregory Haskins3aa416b2007-10-11 22:11:11 +02002626 if (!validate_chain(curr, lock, hlock, chain_head, chain_key))
Peter Zijlstra8e182572007-07-19 01:48:54 -07002627 return 0;
Jarek Poplawski381a2292007-02-10 01:44:58 -08002628
Gregory Haskins3aa416b2007-10-11 22:11:11 +02002629 curr->curr_chain_key = chain_key;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002630 curr->lockdep_depth++;
2631 check_chain_key(curr);
Jarek Poplawski60e114d2007-02-20 13:58:00 -08002632#ifdef CONFIG_DEBUG_LOCKDEP
2633 if (unlikely(!debug_locks))
2634 return 0;
2635#endif
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002636 if (unlikely(curr->lockdep_depth >= MAX_LOCK_DEPTH)) {
2637 debug_locks_off();
2638 printk("BUG: MAX_LOCK_DEPTH too low!\n");
2639 printk("turning off the locking correctness validator.\n");
2640 return 0;
2641 }
Jarek Poplawski381a2292007-02-10 01:44:58 -08002642
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002643 if (unlikely(curr->lockdep_depth > max_lockdep_depth))
2644 max_lockdep_depth = curr->lockdep_depth;
2645
2646 return 1;
2647}
2648
2649static int
2650print_unlock_inbalance_bug(struct task_struct *curr, struct lockdep_map *lock,
2651 unsigned long ip)
2652{
2653 if (!debug_locks_off())
2654 return 0;
2655 if (debug_locks_silent)
2656 return 0;
2657
2658 printk("\n=====================================\n");
2659 printk( "[ BUG: bad unlock balance detected! ]\n");
2660 printk( "-------------------------------------\n");
2661 printk("%s/%d is trying to release lock (",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07002662 curr->comm, task_pid_nr(curr));
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002663 print_lockdep_cache(lock);
2664 printk(") at:\n");
2665 print_ip_sym(ip);
2666 printk("but there are no more locks to release!\n");
2667 printk("\nother info that might help us debug this:\n");
2668 lockdep_print_held_locks(curr);
2669
2670 printk("\nstack backtrace:\n");
2671 dump_stack();
2672
2673 return 0;
2674}
2675
2676/*
2677 * Common debugging checks for both nested and non-nested unlock:
2678 */
2679static int check_unlock(struct task_struct *curr, struct lockdep_map *lock,
2680 unsigned long ip)
2681{
2682 if (unlikely(!debug_locks))
2683 return 0;
2684 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2685 return 0;
2686
2687 if (curr->lockdep_depth <= 0)
2688 return print_unlock_inbalance_bug(curr, lock, ip);
2689
2690 return 1;
2691}
2692
Peter Zijlstra64aa3482008-08-11 09:30:21 +02002693static int
Peter Zijlstra00ef9f72008-12-04 09:00:17 +01002694__lock_set_class(struct lockdep_map *lock, const char *name,
2695 struct lock_class_key *key, unsigned int subclass,
2696 unsigned long ip)
Peter Zijlstra64aa3482008-08-11 09:30:21 +02002697{
2698 struct task_struct *curr = current;
2699 struct held_lock *hlock, *prev_hlock;
2700 struct lock_class *class;
2701 unsigned int depth;
2702 int i;
2703
2704 depth = curr->lockdep_depth;
2705 if (DEBUG_LOCKS_WARN_ON(!depth))
2706 return 0;
2707
2708 prev_hlock = NULL;
2709 for (i = depth-1; i >= 0; i--) {
2710 hlock = curr->held_locks + i;
2711 /*
2712 * We must not cross into another context:
2713 */
2714 if (prev_hlock && prev_hlock->irq_context != hlock->irq_context)
2715 break;
2716 if (hlock->instance == lock)
2717 goto found_it;
2718 prev_hlock = hlock;
2719 }
2720 return print_unlock_inbalance_bug(curr, lock, ip);
2721
2722found_it:
Peter Zijlstra00ef9f72008-12-04 09:00:17 +01002723 lockdep_init_map(lock, name, key, 0);
Peter Zijlstra64aa3482008-08-11 09:30:21 +02002724 class = register_lock_class(lock, subclass, 0);
Dave Jonesf82b2172008-08-11 09:30:23 +02002725 hlock->class_idx = class - lock_classes + 1;
Peter Zijlstra64aa3482008-08-11 09:30:21 +02002726
2727 curr->lockdep_depth = i;
2728 curr->curr_chain_key = hlock->prev_chain_key;
2729
2730 for (; i < depth; i++) {
2731 hlock = curr->held_locks + i;
2732 if (!__lock_acquire(hlock->instance,
Dave Jonesf82b2172008-08-11 09:30:23 +02002733 hlock_class(hlock)->subclass, hlock->trylock,
Peter Zijlstra64aa3482008-08-11 09:30:21 +02002734 hlock->read, hlock->check, hlock->hardirqs_off,
Peter Zijlstra7531e2f2008-08-11 09:30:24 +02002735 hlock->nest_lock, hlock->acquire_ip))
Peter Zijlstra64aa3482008-08-11 09:30:21 +02002736 return 0;
2737 }
2738
2739 if (DEBUG_LOCKS_WARN_ON(curr->lockdep_depth != depth))
2740 return 0;
2741 return 1;
2742}
2743
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002744/*
2745 * Remove the lock to the list of currently held locks in a
2746 * potentially non-nested (out of order) manner. This is a
2747 * relatively rare operation, as all the unlock APIs default
2748 * to nested mode (which uses lock_release()):
2749 */
2750static int
2751lock_release_non_nested(struct task_struct *curr,
2752 struct lockdep_map *lock, unsigned long ip)
2753{
2754 struct held_lock *hlock, *prev_hlock;
2755 unsigned int depth;
2756 int i;
2757
2758 /*
2759 * Check whether the lock exists in the current stack
2760 * of held locks:
2761 */
2762 depth = curr->lockdep_depth;
2763 if (DEBUG_LOCKS_WARN_ON(!depth))
2764 return 0;
2765
2766 prev_hlock = NULL;
2767 for (i = depth-1; i >= 0; i--) {
2768 hlock = curr->held_locks + i;
2769 /*
2770 * We must not cross into another context:
2771 */
2772 if (prev_hlock && prev_hlock->irq_context != hlock->irq_context)
2773 break;
2774 if (hlock->instance == lock)
2775 goto found_it;
2776 prev_hlock = hlock;
2777 }
2778 return print_unlock_inbalance_bug(curr, lock, ip);
2779
2780found_it:
Peter Zijlstraf20786f2007-07-19 01:48:56 -07002781 lock_release_holdtime(hlock);
2782
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002783 /*
2784 * We have the right lock to unlock, 'hlock' points to it.
2785 * Now we remove it from the stack, and add back the other
2786 * entries (if any), recalculating the hash along the way:
2787 */
2788 curr->lockdep_depth = i;
2789 curr->curr_chain_key = hlock->prev_chain_key;
2790
2791 for (i++; i < depth; i++) {
2792 hlock = curr->held_locks + i;
2793 if (!__lock_acquire(hlock->instance,
Dave Jonesf82b2172008-08-11 09:30:23 +02002794 hlock_class(hlock)->subclass, hlock->trylock,
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002795 hlock->read, hlock->check, hlock->hardirqs_off,
Peter Zijlstra7531e2f2008-08-11 09:30:24 +02002796 hlock->nest_lock, hlock->acquire_ip))
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002797 return 0;
2798 }
2799
2800 if (DEBUG_LOCKS_WARN_ON(curr->lockdep_depth != depth - 1))
2801 return 0;
2802 return 1;
2803}
2804
2805/*
2806 * Remove the lock to the list of currently held locks - this gets
2807 * called on mutex_unlock()/spin_unlock*() (or on a failed
2808 * mutex_lock_interruptible()). This is done for unlocks that nest
2809 * perfectly. (i.e. the current top of the lock-stack is unlocked)
2810 */
2811static int lock_release_nested(struct task_struct *curr,
2812 struct lockdep_map *lock, unsigned long ip)
2813{
2814 struct held_lock *hlock;
2815 unsigned int depth;
2816
2817 /*
2818 * Pop off the top of the lock stack:
2819 */
2820 depth = curr->lockdep_depth - 1;
2821 hlock = curr->held_locks + depth;
2822
2823 /*
2824 * Is the unlock non-nested:
2825 */
2826 if (hlock->instance != lock)
2827 return lock_release_non_nested(curr, lock, ip);
2828 curr->lockdep_depth--;
2829
2830 if (DEBUG_LOCKS_WARN_ON(!depth && (hlock->prev_chain_key != 0)))
2831 return 0;
2832
2833 curr->curr_chain_key = hlock->prev_chain_key;
2834
Peter Zijlstraf20786f2007-07-19 01:48:56 -07002835 lock_release_holdtime(hlock);
2836
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002837#ifdef CONFIG_DEBUG_LOCKDEP
2838 hlock->prev_chain_key = 0;
Dave Jonesf82b2172008-08-11 09:30:23 +02002839 hlock->class_idx = 0;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002840 hlock->acquire_ip = 0;
2841 hlock->irq_context = 0;
2842#endif
2843 return 1;
2844}
2845
2846/*
2847 * Remove the lock to the list of currently held locks - this gets
2848 * called on mutex_unlock()/spin_unlock*() (or on a failed
2849 * mutex_lock_interruptible()). This is done for unlocks that nest
2850 * perfectly. (i.e. the current top of the lock-stack is unlocked)
2851 */
2852static void
2853__lock_release(struct lockdep_map *lock, int nested, unsigned long ip)
2854{
2855 struct task_struct *curr = current;
2856
2857 if (!check_unlock(curr, lock, ip))
2858 return;
2859
2860 if (nested) {
2861 if (!lock_release_nested(curr, lock, ip))
2862 return;
2863 } else {
2864 if (!lock_release_non_nested(curr, lock, ip))
2865 return;
2866 }
2867
2868 check_chain_key(curr);
2869}
2870
2871/*
2872 * Check whether we follow the irq-flags state precisely:
2873 */
Steven Rostedt1d09daa2008-05-12 21:20:55 +02002874static void check_flags(unsigned long flags)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002875{
Ingo Molnar992860e2008-07-14 10:28:38 +02002876#if defined(CONFIG_PROVE_LOCKING) && defined(CONFIG_DEBUG_LOCKDEP) && \
2877 defined(CONFIG_TRACE_IRQFLAGS)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002878 if (!debug_locks)
2879 return;
2880
Ingo Molnar5f9fa8a2007-12-07 19:02:47 +01002881 if (irqs_disabled_flags(flags)) {
2882 if (DEBUG_LOCKS_WARN_ON(current->hardirqs_enabled)) {
2883 printk("possible reason: unannotated irqs-off.\n");
2884 }
2885 } else {
2886 if (DEBUG_LOCKS_WARN_ON(!current->hardirqs_enabled)) {
2887 printk("possible reason: unannotated irqs-on.\n");
2888 }
2889 }
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002890
2891 /*
2892 * We dont accurately track softirq state in e.g.
2893 * hardirq contexts (such as on 4KSTACKS), so only
2894 * check if not in hardirq contexts:
2895 */
2896 if (!hardirq_count()) {
2897 if (softirq_count())
2898 DEBUG_LOCKS_WARN_ON(current->softirqs_enabled);
2899 else
2900 DEBUG_LOCKS_WARN_ON(!current->softirqs_enabled);
2901 }
2902
2903 if (!debug_locks)
2904 print_irqtrace_events(current);
2905#endif
2906}
2907
Peter Zijlstra00ef9f72008-12-04 09:00:17 +01002908void lock_set_class(struct lockdep_map *lock, const char *name,
2909 struct lock_class_key *key, unsigned int subclass,
2910 unsigned long ip)
Peter Zijlstra64aa3482008-08-11 09:30:21 +02002911{
2912 unsigned long flags;
2913
2914 if (unlikely(current->lockdep_recursion))
2915 return;
2916
2917 raw_local_irq_save(flags);
2918 current->lockdep_recursion = 1;
2919 check_flags(flags);
Peter Zijlstra00ef9f72008-12-04 09:00:17 +01002920 if (__lock_set_class(lock, name, key, subclass, ip))
Peter Zijlstra64aa3482008-08-11 09:30:21 +02002921 check_chain_key(current);
2922 current->lockdep_recursion = 0;
2923 raw_local_irq_restore(flags);
2924}
Peter Zijlstra00ef9f72008-12-04 09:00:17 +01002925EXPORT_SYMBOL_GPL(lock_set_class);
Peter Zijlstra64aa3482008-08-11 09:30:21 +02002926
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002927/*
2928 * We are not always called with irqs disabled - do that here,
2929 * and also avoid lockdep recursion:
2930 */
Steven Rostedt1d09daa2008-05-12 21:20:55 +02002931void lock_acquire(struct lockdep_map *lock, unsigned int subclass,
Peter Zijlstra7531e2f2008-08-11 09:30:24 +02002932 int trylock, int read, int check,
2933 struct lockdep_map *nest_lock, unsigned long ip)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002934{
2935 unsigned long flags;
2936
2937 if (unlikely(current->lockdep_recursion))
2938 return;
2939
2940 raw_local_irq_save(flags);
2941 check_flags(flags);
2942
2943 current->lockdep_recursion = 1;
2944 __lock_acquire(lock, subclass, trylock, read, check,
Peter Zijlstra7531e2f2008-08-11 09:30:24 +02002945 irqs_disabled_flags(flags), nest_lock, ip);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002946 current->lockdep_recursion = 0;
2947 raw_local_irq_restore(flags);
2948}
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002949EXPORT_SYMBOL_GPL(lock_acquire);
2950
Steven Rostedt1d09daa2008-05-12 21:20:55 +02002951void lock_release(struct lockdep_map *lock, int nested,
Steven Rostedt0764d232008-05-12 21:20:44 +02002952 unsigned long ip)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002953{
2954 unsigned long flags;
2955
2956 if (unlikely(current->lockdep_recursion))
2957 return;
2958
2959 raw_local_irq_save(flags);
2960 check_flags(flags);
2961 current->lockdep_recursion = 1;
2962 __lock_release(lock, nested, ip);
2963 current->lockdep_recursion = 0;
2964 raw_local_irq_restore(flags);
2965}
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002966EXPORT_SYMBOL_GPL(lock_release);
2967
Peter Zijlstraf20786f2007-07-19 01:48:56 -07002968#ifdef CONFIG_LOCK_STAT
2969static int
2970print_lock_contention_bug(struct task_struct *curr, struct lockdep_map *lock,
2971 unsigned long ip)
2972{
2973 if (!debug_locks_off())
2974 return 0;
2975 if (debug_locks_silent)
2976 return 0;
2977
2978 printk("\n=================================\n");
2979 printk( "[ BUG: bad contention detected! ]\n");
2980 printk( "---------------------------------\n");
2981 printk("%s/%d is trying to contend lock (",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07002982 curr->comm, task_pid_nr(curr));
Peter Zijlstraf20786f2007-07-19 01:48:56 -07002983 print_lockdep_cache(lock);
2984 printk(") at:\n");
2985 print_ip_sym(ip);
2986 printk("but there are no locks held!\n");
2987 printk("\nother info that might help us debug this:\n");
2988 lockdep_print_held_locks(curr);
2989
2990 printk("\nstack backtrace:\n");
2991 dump_stack();
2992
2993 return 0;
2994}
2995
2996static void
2997__lock_contended(struct lockdep_map *lock, unsigned long ip)
2998{
2999 struct task_struct *curr = current;
3000 struct held_lock *hlock, *prev_hlock;
3001 struct lock_class_stats *stats;
3002 unsigned int depth;
Peter Zijlstrac7e78cf2008-10-16 23:17:09 +02003003 int i, contention_point, contending_point;
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003004
3005 depth = curr->lockdep_depth;
3006 if (DEBUG_LOCKS_WARN_ON(!depth))
3007 return;
3008
3009 prev_hlock = NULL;
3010 for (i = depth-1; i >= 0; i--) {
3011 hlock = curr->held_locks + i;
3012 /*
3013 * We must not cross into another context:
3014 */
3015 if (prev_hlock && prev_hlock->irq_context != hlock->irq_context)
3016 break;
3017 if (hlock->instance == lock)
3018 goto found_it;
3019 prev_hlock = hlock;
3020 }
3021 print_lock_contention_bug(curr, lock, ip);
3022 return;
3023
3024found_it:
3025 hlock->waittime_stamp = sched_clock();
3026
Peter Zijlstrac7e78cf2008-10-16 23:17:09 +02003027 contention_point = lock_point(hlock_class(hlock)->contention_point, ip);
3028 contending_point = lock_point(hlock_class(hlock)->contending_point,
3029 lock->ip);
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003030
Dave Jonesf82b2172008-08-11 09:30:23 +02003031 stats = get_lock_stats(hlock_class(hlock));
Peter Zijlstrac7e78cf2008-10-16 23:17:09 +02003032 if (contention_point < LOCKSTAT_POINTS)
3033 stats->contention_point[contention_point]++;
3034 if (contending_point < LOCKSTAT_POINTS)
3035 stats->contending_point[contending_point]++;
Peter Zijlstra96645672007-07-19 01:49:00 -07003036 if (lock->cpu != smp_processor_id())
3037 stats->bounces[bounce_contended + !!hlock->read]++;
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003038 put_lock_stats(stats);
3039}
3040
3041static void
Peter Zijlstrac7e78cf2008-10-16 23:17:09 +02003042__lock_acquired(struct lockdep_map *lock, unsigned long ip)
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003043{
3044 struct task_struct *curr = current;
3045 struct held_lock *hlock, *prev_hlock;
3046 struct lock_class_stats *stats;
3047 unsigned int depth;
3048 u64 now;
Peter Zijlstra96645672007-07-19 01:49:00 -07003049 s64 waittime = 0;
3050 int i, cpu;
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003051
3052 depth = curr->lockdep_depth;
3053 if (DEBUG_LOCKS_WARN_ON(!depth))
3054 return;
3055
3056 prev_hlock = NULL;
3057 for (i = depth-1; i >= 0; i--) {
3058 hlock = curr->held_locks + i;
3059 /*
3060 * We must not cross into another context:
3061 */
3062 if (prev_hlock && prev_hlock->irq_context != hlock->irq_context)
3063 break;
3064 if (hlock->instance == lock)
3065 goto found_it;
3066 prev_hlock = hlock;
3067 }
3068 print_lock_contention_bug(curr, lock, _RET_IP_);
3069 return;
3070
3071found_it:
Peter Zijlstra96645672007-07-19 01:49:00 -07003072 cpu = smp_processor_id();
3073 if (hlock->waittime_stamp) {
3074 now = sched_clock();
3075 waittime = now - hlock->waittime_stamp;
3076 hlock->holdtime_stamp = now;
3077 }
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003078
Dave Jonesf82b2172008-08-11 09:30:23 +02003079 stats = get_lock_stats(hlock_class(hlock));
Peter Zijlstra96645672007-07-19 01:49:00 -07003080 if (waittime) {
3081 if (hlock->read)
3082 lock_time_inc(&stats->read_waittime, waittime);
3083 else
3084 lock_time_inc(&stats->write_waittime, waittime);
3085 }
3086 if (lock->cpu != cpu)
3087 stats->bounces[bounce_acquired + !!hlock->read]++;
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003088 put_lock_stats(stats);
Peter Zijlstra96645672007-07-19 01:49:00 -07003089
3090 lock->cpu = cpu;
Peter Zijlstrac7e78cf2008-10-16 23:17:09 +02003091 lock->ip = ip;
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003092}
3093
3094void lock_contended(struct lockdep_map *lock, unsigned long ip)
3095{
3096 unsigned long flags;
3097
3098 if (unlikely(!lock_stat))
3099 return;
3100
3101 if (unlikely(current->lockdep_recursion))
3102 return;
3103
3104 raw_local_irq_save(flags);
3105 check_flags(flags);
3106 current->lockdep_recursion = 1;
3107 __lock_contended(lock, ip);
3108 current->lockdep_recursion = 0;
3109 raw_local_irq_restore(flags);
3110}
3111EXPORT_SYMBOL_GPL(lock_contended);
3112
Peter Zijlstrac7e78cf2008-10-16 23:17:09 +02003113void lock_acquired(struct lockdep_map *lock, unsigned long ip)
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003114{
3115 unsigned long flags;
3116
3117 if (unlikely(!lock_stat))
3118 return;
3119
3120 if (unlikely(current->lockdep_recursion))
3121 return;
3122
3123 raw_local_irq_save(flags);
3124 check_flags(flags);
3125 current->lockdep_recursion = 1;
Peter Zijlstrac7e78cf2008-10-16 23:17:09 +02003126 __lock_acquired(lock, ip);
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003127 current->lockdep_recursion = 0;
3128 raw_local_irq_restore(flags);
3129}
3130EXPORT_SYMBOL_GPL(lock_acquired);
3131#endif
3132
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003133/*
3134 * Used by the testsuite, sanitize the validator state
3135 * after a simulated failure:
3136 */
3137
3138void lockdep_reset(void)
3139{
3140 unsigned long flags;
Ingo Molnar23d95a02006-12-13 00:34:40 -08003141 int i;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003142
3143 raw_local_irq_save(flags);
3144 current->curr_chain_key = 0;
3145 current->lockdep_depth = 0;
3146 current->lockdep_recursion = 0;
3147 memset(current->held_locks, 0, MAX_LOCK_DEPTH*sizeof(struct held_lock));
3148 nr_hardirq_chains = 0;
3149 nr_softirq_chains = 0;
3150 nr_process_chains = 0;
3151 debug_locks = 1;
Ingo Molnar23d95a02006-12-13 00:34:40 -08003152 for (i = 0; i < CHAINHASH_SIZE; i++)
3153 INIT_LIST_HEAD(chainhash_table + i);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003154 raw_local_irq_restore(flags);
3155}
3156
3157static void zap_class(struct lock_class *class)
3158{
3159 int i;
3160
3161 /*
3162 * Remove all dependencies this lock is
3163 * involved in:
3164 */
3165 for (i = 0; i < nr_list_entries; i++) {
3166 if (list_entries[i].class == class)
3167 list_del_rcu(&list_entries[i].entry);
3168 }
3169 /*
3170 * Unhash the class and remove it from the all_lock_classes list:
3171 */
3172 list_del_rcu(&class->hash_entry);
3173 list_del_rcu(&class->lock_entry);
3174
Rabin Vincent8bfe0292008-08-11 09:30:26 +02003175 class->key = NULL;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003176}
3177
Arjan van de Venfabe8742008-01-24 07:00:45 +01003178static inline int within(const void *addr, void *start, unsigned long size)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003179{
3180 return addr >= start && addr < start + size;
3181}
3182
3183void lockdep_free_key_range(void *start, unsigned long size)
3184{
3185 struct lock_class *class, *next;
3186 struct list_head *head;
3187 unsigned long flags;
3188 int i;
Nick Piggin5a26db52008-01-16 09:51:58 +01003189 int locked;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003190
3191 raw_local_irq_save(flags);
Nick Piggin5a26db52008-01-16 09:51:58 +01003192 locked = graph_lock();
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003193
3194 /*
3195 * Unhash all classes that were created by this module:
3196 */
3197 for (i = 0; i < CLASSHASH_SIZE; i++) {
3198 head = classhash_table + i;
3199 if (list_empty(head))
3200 continue;
Arjan van de Venfabe8742008-01-24 07:00:45 +01003201 list_for_each_entry_safe(class, next, head, hash_entry) {
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003202 if (within(class->key, start, size))
3203 zap_class(class);
Arjan van de Venfabe8742008-01-24 07:00:45 +01003204 else if (within(class->name, start, size))
3205 zap_class(class);
3206 }
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003207 }
3208
Nick Piggin5a26db52008-01-16 09:51:58 +01003209 if (locked)
3210 graph_unlock();
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003211 raw_local_irq_restore(flags);
3212}
3213
3214void lockdep_reset_lock(struct lockdep_map *lock)
3215{
Ingo Molnard6d897c2006-07-10 04:44:04 -07003216 struct lock_class *class, *next;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003217 struct list_head *head;
3218 unsigned long flags;
3219 int i, j;
Nick Piggin5a26db52008-01-16 09:51:58 +01003220 int locked;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003221
3222 raw_local_irq_save(flags);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003223
3224 /*
Ingo Molnard6d897c2006-07-10 04:44:04 -07003225 * Remove all classes this lock might have:
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003226 */
Ingo Molnard6d897c2006-07-10 04:44:04 -07003227 for (j = 0; j < MAX_LOCKDEP_SUBCLASSES; j++) {
3228 /*
3229 * If the class exists we look it up and zap it:
3230 */
3231 class = look_up_lock_class(lock, j);
3232 if (class)
3233 zap_class(class);
3234 }
3235 /*
3236 * Debug check: in the end all mapped classes should
3237 * be gone.
3238 */
Nick Piggin5a26db52008-01-16 09:51:58 +01003239 locked = graph_lock();
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003240 for (i = 0; i < CLASSHASH_SIZE; i++) {
3241 head = classhash_table + i;
3242 if (list_empty(head))
3243 continue;
3244 list_for_each_entry_safe(class, next, head, hash_entry) {
Ingo Molnard6d897c2006-07-10 04:44:04 -07003245 if (unlikely(class == lock->class_cache)) {
Ingo Molnar74c383f2006-12-13 00:34:43 -08003246 if (debug_locks_off_graph_unlock())
3247 WARN_ON(1);
Ingo Molnard6d897c2006-07-10 04:44:04 -07003248 goto out_restore;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003249 }
3250 }
3251 }
Nick Piggin5a26db52008-01-16 09:51:58 +01003252 if (locked)
3253 graph_unlock();
Ingo Molnard6d897c2006-07-10 04:44:04 -07003254
3255out_restore:
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003256 raw_local_irq_restore(flags);
3257}
3258
Sam Ravnborg14999932007-02-28 20:12:31 -08003259void lockdep_init(void)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003260{
3261 int i;
3262
3263 /*
3264 * Some architectures have their own start_kernel()
3265 * code which calls lockdep_init(), while we also
3266 * call lockdep_init() from the start_kernel() itself,
3267 * and we want to initialize the hashes only once:
3268 */
3269 if (lockdep_initialized)
3270 return;
3271
3272 for (i = 0; i < CLASSHASH_SIZE; i++)
3273 INIT_LIST_HEAD(classhash_table + i);
3274
3275 for (i = 0; i < CHAINHASH_SIZE; i++)
3276 INIT_LIST_HEAD(chainhash_table + i);
3277
3278 lockdep_initialized = 1;
3279}
3280
3281void __init lockdep_info(void)
3282{
3283 printk("Lock dependency validator: Copyright (c) 2006 Red Hat, Inc., Ingo Molnar\n");
3284
Li Zefanb0788ca2008-11-21 15:57:32 +08003285 printk("... MAX_LOCKDEP_SUBCLASSES: %lu\n", MAX_LOCKDEP_SUBCLASSES);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003286 printk("... MAX_LOCK_DEPTH: %lu\n", MAX_LOCK_DEPTH);
3287 printk("... MAX_LOCKDEP_KEYS: %lu\n", MAX_LOCKDEP_KEYS);
Li Zefanb0788ca2008-11-21 15:57:32 +08003288 printk("... CLASSHASH_SIZE: %lu\n", CLASSHASH_SIZE);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003289 printk("... MAX_LOCKDEP_ENTRIES: %lu\n", MAX_LOCKDEP_ENTRIES);
3290 printk("... MAX_LOCKDEP_CHAINS: %lu\n", MAX_LOCKDEP_CHAINS);
3291 printk("... CHAINHASH_SIZE: %lu\n", CHAINHASH_SIZE);
3292
3293 printk(" memory used by lock dependency info: %lu kB\n",
3294 (sizeof(struct lock_class) * MAX_LOCKDEP_KEYS +
3295 sizeof(struct list_head) * CLASSHASH_SIZE +
3296 sizeof(struct lock_list) * MAX_LOCKDEP_ENTRIES +
3297 sizeof(struct lock_chain) * MAX_LOCKDEP_CHAINS +
3298 sizeof(struct list_head) * CHAINHASH_SIZE) / 1024);
3299
3300 printk(" per task-struct memory footprint: %lu bytes\n",
3301 sizeof(struct held_lock) * MAX_LOCK_DEPTH);
3302
3303#ifdef CONFIG_DEBUG_LOCKDEP
Johannes Bergc71063c2007-07-19 01:49:02 -07003304 if (lockdep_init_error) {
3305 printk("WARNING: lockdep init error! Arch code didn't call lockdep_init() early enough?\n");
3306 printk("Call stack leading to lockdep invocation was:\n");
3307 print_stack_trace(&lockdep_init_trace, 0);
3308 }
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003309#endif
3310}
3311
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003312static void
3313print_freed_lock_bug(struct task_struct *curr, const void *mem_from,
Arjan van de Ven55794a42006-07-10 04:44:03 -07003314 const void *mem_to, struct held_lock *hlock)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003315{
3316 if (!debug_locks_off())
3317 return;
3318 if (debug_locks_silent)
3319 return;
3320
3321 printk("\n=========================\n");
3322 printk( "[ BUG: held lock freed! ]\n");
3323 printk( "-------------------------\n");
3324 printk("%s/%d is freeing memory %p-%p, with a lock still held there!\n",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07003325 curr->comm, task_pid_nr(curr), mem_from, mem_to-1);
Arjan van de Ven55794a42006-07-10 04:44:03 -07003326 print_lock(hlock);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003327 lockdep_print_held_locks(curr);
3328
3329 printk("\nstack backtrace:\n");
3330 dump_stack();
3331}
3332
Oleg Nesterov54561782007-12-05 15:46:09 +01003333static inline int not_in_range(const void* mem_from, unsigned long mem_len,
3334 const void* lock_from, unsigned long lock_len)
3335{
3336 return lock_from + lock_len <= mem_from ||
3337 mem_from + mem_len <= lock_from;
3338}
3339
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003340/*
3341 * Called when kernel memory is freed (or unmapped), or if a lock
3342 * is destroyed or reinitialized - this code checks whether there is
3343 * any held lock in the memory range of <from> to <to>:
3344 */
3345void debug_check_no_locks_freed(const void *mem_from, unsigned long mem_len)
3346{
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003347 struct task_struct *curr = current;
3348 struct held_lock *hlock;
3349 unsigned long flags;
3350 int i;
3351
3352 if (unlikely(!debug_locks))
3353 return;
3354
3355 local_irq_save(flags);
3356 for (i = 0; i < curr->lockdep_depth; i++) {
3357 hlock = curr->held_locks + i;
3358
Oleg Nesterov54561782007-12-05 15:46:09 +01003359 if (not_in_range(mem_from, mem_len, hlock->instance,
3360 sizeof(*hlock->instance)))
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003361 continue;
3362
Oleg Nesterov54561782007-12-05 15:46:09 +01003363 print_freed_lock_bug(curr, mem_from, mem_from + mem_len, hlock);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003364 break;
3365 }
3366 local_irq_restore(flags);
3367}
Peter Zijlstraed075362006-12-06 20:35:24 -08003368EXPORT_SYMBOL_GPL(debug_check_no_locks_freed);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003369
3370static void print_held_locks_bug(struct task_struct *curr)
3371{
3372 if (!debug_locks_off())
3373 return;
3374 if (debug_locks_silent)
3375 return;
3376
3377 printk("\n=====================================\n");
3378 printk( "[ BUG: lock held at task exit time! ]\n");
3379 printk( "-------------------------------------\n");
3380 printk("%s/%d is exiting with locks still held!\n",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07003381 curr->comm, task_pid_nr(curr));
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003382 lockdep_print_held_locks(curr);
3383
3384 printk("\nstack backtrace:\n");
3385 dump_stack();
3386}
3387
3388void debug_check_no_locks_held(struct task_struct *task)
3389{
3390 if (unlikely(task->lockdep_depth > 0))
3391 print_held_locks_bug(task);
3392}
3393
3394void debug_show_all_locks(void)
3395{
3396 struct task_struct *g, *p;
3397 int count = 10;
3398 int unlock = 1;
3399
Jarek Poplawski9c35dd72007-03-22 00:11:28 -08003400 if (unlikely(!debug_locks)) {
3401 printk("INFO: lockdep is turned off.\n");
3402 return;
3403 }
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003404 printk("\nShowing all locks held in the system:\n");
3405
3406 /*
3407 * Here we try to get the tasklist_lock as hard as possible,
3408 * if not successful after 2 seconds we ignore it (but keep
3409 * trying). This is to enable a debug printout even if a
3410 * tasklist_lock-holding task deadlocks or crashes.
3411 */
3412retry:
3413 if (!read_trylock(&tasklist_lock)) {
3414 if (count == 10)
3415 printk("hm, tasklist_lock locked, retrying... ");
3416 if (count) {
3417 count--;
3418 printk(" #%d", 10-count);
3419 mdelay(200);
3420 goto retry;
3421 }
3422 printk(" ignoring it.\n");
3423 unlock = 0;
qinghuang feng46fec7a2008-10-28 17:24:28 +08003424 } else {
3425 if (count != 10)
3426 printk(KERN_CONT " locked it.\n");
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003427 }
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003428
3429 do_each_thread(g, p) {
Ingo Molnar85684872007-12-05 15:46:09 +01003430 /*
3431 * It's not reliable to print a task's held locks
3432 * if it's not sleeping (or if it's not the current
3433 * task):
3434 */
3435 if (p->state == TASK_RUNNING && p != current)
3436 continue;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003437 if (p->lockdep_depth)
3438 lockdep_print_held_locks(p);
3439 if (!unlock)
3440 if (read_trylock(&tasklist_lock))
3441 unlock = 1;
3442 } while_each_thread(g, p);
3443
3444 printk("\n");
3445 printk("=============================================\n\n");
3446
3447 if (unlock)
3448 read_unlock(&tasklist_lock);
3449}
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003450EXPORT_SYMBOL_GPL(debug_show_all_locks);
3451
Ingo Molnar82a1fcb2008-01-25 21:08:02 +01003452/*
3453 * Careful: only use this function if you are sure that
3454 * the task cannot run in parallel!
3455 */
3456void __debug_show_held_locks(struct task_struct *task)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003457{
Jarek Poplawski9c35dd72007-03-22 00:11:28 -08003458 if (unlikely(!debug_locks)) {
3459 printk("INFO: lockdep is turned off.\n");
3460 return;
3461 }
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003462 lockdep_print_held_locks(task);
3463}
Ingo Molnar82a1fcb2008-01-25 21:08:02 +01003464EXPORT_SYMBOL_GPL(__debug_show_held_locks);
3465
3466void debug_show_held_locks(struct task_struct *task)
3467{
3468 __debug_show_held_locks(task);
3469}
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003470EXPORT_SYMBOL_GPL(debug_show_held_locks);
Peter Zijlstrab351d162007-10-11 22:11:12 +02003471
3472void lockdep_sys_exit(void)
3473{
3474 struct task_struct *curr = current;
3475
3476 if (unlikely(curr->lockdep_depth)) {
3477 if (!debug_locks_off())
3478 return;
3479 printk("\n================================================\n");
3480 printk( "[ BUG: lock held when returning to user space! ]\n");
3481 printk( "------------------------------------------------\n");
3482 printk("%s/%d is leaving the kernel with locks still held!\n",
3483 curr->comm, curr->pid);
3484 lockdep_print_held_locks(curr);
3485 }
3486}