blob: a288ae107b50b24101e5ad193b8fdb4c5aa8a44d [file] [log] [blame]
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001/*
2 * kernel/lockdep.c
3 *
4 * Runtime locking correctness validator
5 *
6 * Started by Ingo Molnar:
7 *
Peter Zijlstra4b32d0a2007-07-19 01:48:59 -07008 * Copyright (C) 2006,2007 Red Hat, Inc., Ingo Molnar <mingo@redhat.com>
9 * Copyright (C) 2007 Red Hat, Inc., Peter Zijlstra <pzijlstr@redhat.com>
Ingo Molnarfbb9ce952006-07-03 00:24:50 -070010 *
11 * this code maps all the lock dependencies as they occur in a live kernel
12 * and will warn about the following classes of locking bugs:
13 *
14 * - lock inversion scenarios
15 * - circular lock dependencies
16 * - hardirq/softirq safe/unsafe locking bugs
17 *
18 * Bugs are reported even if the current locking scenario does not cause
19 * any deadlock at this point.
20 *
21 * I.e. if anytime in the past two locks were taken in a different order,
22 * even if it happened for another task, even if those were different
23 * locks (but of the same class as this lock), this code will detect it.
24 *
25 * Thanks to Arjan van de Ven for coming up with the initial idea of
26 * mapping lock dependencies runtime.
27 */
Steven Rostedta5e25882008-12-02 15:34:05 -050028#define DISABLE_BRANCH_PROFILING
Ingo Molnarfbb9ce952006-07-03 00:24:50 -070029#include <linux/mutex.h>
30#include <linux/sched.h>
31#include <linux/delay.h>
32#include <linux/module.h>
33#include <linux/proc_fs.h>
34#include <linux/seq_file.h>
35#include <linux/spinlock.h>
36#include <linux/kallsyms.h>
37#include <linux/interrupt.h>
38#include <linux/stacktrace.h>
39#include <linux/debug_locks.h>
40#include <linux/irqflags.h>
Dave Jones99de0552006-09-29 02:00:10 -070041#include <linux/utsname.h>
Peter Zijlstra4b32d0a2007-07-19 01:48:59 -070042#include <linux/hash.h>
Steven Rostedt81d68a92008-05-12 21:20:42 +020043#include <linux/ftrace.h>
Peter Zijlstrab4b136f2009-01-29 14:50:36 +010044#include <linux/stringify.h>
Ingo Molnarfbb9ce952006-07-03 00:24:50 -070045
46#include <asm/sections.h>
47
48#include "lockdep_internals.h"
49
Peter Zijlstraf20786f2007-07-19 01:48:56 -070050#ifdef CONFIG_PROVE_LOCKING
51int prove_locking = 1;
52module_param(prove_locking, int, 0644);
53#else
54#define prove_locking 0
55#endif
56
57#ifdef CONFIG_LOCK_STAT
58int lock_stat = 1;
59module_param(lock_stat, int, 0644);
60#else
61#define lock_stat 0
62#endif
63
Ingo Molnarfbb9ce952006-07-03 00:24:50 -070064/*
Ingo Molnar74c383f2006-12-13 00:34:43 -080065 * lockdep_lock: protects the lockdep graph, the hashes and the
66 * class/list/hash allocators.
Ingo Molnarfbb9ce952006-07-03 00:24:50 -070067 *
68 * This is one of the rare exceptions where it's justified
69 * to use a raw spinlock - we really dont want the spinlock
Ingo Molnar74c383f2006-12-13 00:34:43 -080070 * code to recurse back into the lockdep code...
Ingo Molnarfbb9ce952006-07-03 00:24:50 -070071 */
Ingo Molnar74c383f2006-12-13 00:34:43 -080072static raw_spinlock_t lockdep_lock = (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED;
73
74static int graph_lock(void)
75{
76 __raw_spin_lock(&lockdep_lock);
77 /*
78 * Make sure that if another CPU detected a bug while
79 * walking the graph we dont change it (while the other
80 * CPU is busy printing out stuff with the graph lock
81 * dropped already)
82 */
83 if (!debug_locks) {
84 __raw_spin_unlock(&lockdep_lock);
85 return 0;
86 }
Steven Rostedtbb065af2008-05-12 21:21:00 +020087 /* prevent any recursions within lockdep from causing deadlocks */
88 current->lockdep_recursion++;
Ingo Molnar74c383f2006-12-13 00:34:43 -080089 return 1;
90}
91
92static inline int graph_unlock(void)
93{
Jarek Poplawski381a2292007-02-10 01:44:58 -080094 if (debug_locks && !__raw_spin_is_locked(&lockdep_lock))
95 return DEBUG_LOCKS_WARN_ON(1);
96
Steven Rostedtbb065af2008-05-12 21:21:00 +020097 current->lockdep_recursion--;
Ingo Molnar74c383f2006-12-13 00:34:43 -080098 __raw_spin_unlock(&lockdep_lock);
99 return 0;
100}
101
102/*
103 * Turn lock debugging off and return with 0 if it was off already,
104 * and also release the graph lock:
105 */
106static inline int debug_locks_off_graph_unlock(void)
107{
108 int ret = debug_locks_off();
109
110 __raw_spin_unlock(&lockdep_lock);
111
112 return ret;
113}
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700114
115static int lockdep_initialized;
116
117unsigned long nr_list_entries;
118static struct lock_list list_entries[MAX_LOCKDEP_ENTRIES];
119
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700120/*
121 * All data structures here are protected by the global debug_lock.
122 *
123 * Mutex key structs only get allocated, once during bootup, and never
124 * get freed - this significantly simplifies the debugging code.
125 */
126unsigned long nr_lock_classes;
127static struct lock_class lock_classes[MAX_LOCKDEP_KEYS];
128
Dave Jonesf82b2172008-08-11 09:30:23 +0200129static inline struct lock_class *hlock_class(struct held_lock *hlock)
130{
131 if (!hlock->class_idx) {
132 DEBUG_LOCKS_WARN_ON(1);
133 return NULL;
134 }
135 return lock_classes + hlock->class_idx - 1;
136}
137
Peter Zijlstraf20786f2007-07-19 01:48:56 -0700138#ifdef CONFIG_LOCK_STAT
139static DEFINE_PER_CPU(struct lock_class_stats[MAX_LOCKDEP_KEYS], lock_stats);
140
Peter Zijlstrac7e78cf2008-10-16 23:17:09 +0200141static int lock_point(unsigned long points[], unsigned long ip)
Peter Zijlstraf20786f2007-07-19 01:48:56 -0700142{
143 int i;
144
Peter Zijlstrac7e78cf2008-10-16 23:17:09 +0200145 for (i = 0; i < LOCKSTAT_POINTS; i++) {
146 if (points[i] == 0) {
147 points[i] = ip;
Peter Zijlstraf20786f2007-07-19 01:48:56 -0700148 break;
149 }
Peter Zijlstrac7e78cf2008-10-16 23:17:09 +0200150 if (points[i] == ip)
Peter Zijlstraf20786f2007-07-19 01:48:56 -0700151 break;
152 }
153
154 return i;
155}
156
157static void lock_time_inc(struct lock_time *lt, s64 time)
158{
159 if (time > lt->max)
160 lt->max = time;
161
162 if (time < lt->min || !lt->min)
163 lt->min = time;
164
165 lt->total += time;
166 lt->nr++;
167}
168
Peter Zijlstrac46261d2007-07-19 01:48:57 -0700169static inline void lock_time_add(struct lock_time *src, struct lock_time *dst)
170{
171 dst->min += src->min;
172 dst->max += src->max;
173 dst->total += src->total;
174 dst->nr += src->nr;
175}
176
177struct lock_class_stats lock_stats(struct lock_class *class)
178{
179 struct lock_class_stats stats;
180 int cpu, i;
181
182 memset(&stats, 0, sizeof(struct lock_class_stats));
183 for_each_possible_cpu(cpu) {
184 struct lock_class_stats *pcs =
185 &per_cpu(lock_stats, cpu)[class - lock_classes];
186
187 for (i = 0; i < ARRAY_SIZE(stats.contention_point); i++)
188 stats.contention_point[i] += pcs->contention_point[i];
189
Peter Zijlstrac7e78cf2008-10-16 23:17:09 +0200190 for (i = 0; i < ARRAY_SIZE(stats.contending_point); i++)
191 stats.contending_point[i] += pcs->contending_point[i];
192
Peter Zijlstrac46261d2007-07-19 01:48:57 -0700193 lock_time_add(&pcs->read_waittime, &stats.read_waittime);
194 lock_time_add(&pcs->write_waittime, &stats.write_waittime);
195
196 lock_time_add(&pcs->read_holdtime, &stats.read_holdtime);
197 lock_time_add(&pcs->write_holdtime, &stats.write_holdtime);
Peter Zijlstra96645672007-07-19 01:49:00 -0700198
199 for (i = 0; i < ARRAY_SIZE(stats.bounces); i++)
200 stats.bounces[i] += pcs->bounces[i];
Peter Zijlstrac46261d2007-07-19 01:48:57 -0700201 }
202
203 return stats;
204}
205
206void clear_lock_stats(struct lock_class *class)
207{
208 int cpu;
209
210 for_each_possible_cpu(cpu) {
211 struct lock_class_stats *cpu_stats =
212 &per_cpu(lock_stats, cpu)[class - lock_classes];
213
214 memset(cpu_stats, 0, sizeof(struct lock_class_stats));
215 }
216 memset(class->contention_point, 0, sizeof(class->contention_point));
Peter Zijlstrac7e78cf2008-10-16 23:17:09 +0200217 memset(class->contending_point, 0, sizeof(class->contending_point));
Peter Zijlstrac46261d2007-07-19 01:48:57 -0700218}
219
Peter Zijlstraf20786f2007-07-19 01:48:56 -0700220static struct lock_class_stats *get_lock_stats(struct lock_class *class)
221{
222 return &get_cpu_var(lock_stats)[class - lock_classes];
223}
224
225static void put_lock_stats(struct lock_class_stats *stats)
226{
227 put_cpu_var(lock_stats);
228}
229
230static void lock_release_holdtime(struct held_lock *hlock)
231{
232 struct lock_class_stats *stats;
233 s64 holdtime;
234
235 if (!lock_stat)
236 return;
237
238 holdtime = sched_clock() - hlock->holdtime_stamp;
239
Dave Jonesf82b2172008-08-11 09:30:23 +0200240 stats = get_lock_stats(hlock_class(hlock));
Peter Zijlstraf20786f2007-07-19 01:48:56 -0700241 if (hlock->read)
242 lock_time_inc(&stats->read_holdtime, holdtime);
243 else
244 lock_time_inc(&stats->write_holdtime, holdtime);
245 put_lock_stats(stats);
246}
247#else
248static inline void lock_release_holdtime(struct held_lock *hlock)
249{
250}
251#endif
252
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700253/*
254 * We keep a global list of all lock classes. The list only grows,
255 * never shrinks. The list is only accessed with the lockdep
256 * spinlock lock held.
257 */
258LIST_HEAD(all_lock_classes);
259
260/*
261 * The lockdep classes are in a hash-table as well, for fast lookup:
262 */
263#define CLASSHASH_BITS (MAX_LOCKDEP_KEYS_BITS - 1)
264#define CLASSHASH_SIZE (1UL << CLASSHASH_BITS)
Peter Zijlstra4b32d0a2007-07-19 01:48:59 -0700265#define __classhashfn(key) hash_long((unsigned long)key, CLASSHASH_BITS)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700266#define classhashentry(key) (classhash_table + __classhashfn((key)))
267
268static struct list_head classhash_table[CLASSHASH_SIZE];
269
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700270/*
271 * We put the lock dependency chains into a hash-table as well, to cache
272 * their existence:
273 */
274#define CHAINHASH_BITS (MAX_LOCKDEP_CHAINS_BITS-1)
275#define CHAINHASH_SIZE (1UL << CHAINHASH_BITS)
Peter Zijlstra4b32d0a2007-07-19 01:48:59 -0700276#define __chainhashfn(chain) hash_long(chain, CHAINHASH_BITS)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700277#define chainhashentry(chain) (chainhash_table + __chainhashfn((chain)))
278
279static struct list_head chainhash_table[CHAINHASH_SIZE];
280
281/*
282 * The hash key of the lock dependency chains is a hash itself too:
283 * it's a hash of all locks taken up to that lock, including that lock.
284 * It's a 64-bit hash, because it's important for the keys to be
285 * unique.
286 */
287#define iterate_chain_key(key1, key2) \
Ingo Molnar03cbc352006-09-29 02:01:46 -0700288 (((key1) << MAX_LOCKDEP_KEYS_BITS) ^ \
289 ((key1) >> (64-MAX_LOCKDEP_KEYS_BITS)) ^ \
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700290 (key2))
291
Steven Rostedt1d09daa2008-05-12 21:20:55 +0200292void lockdep_off(void)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700293{
294 current->lockdep_recursion++;
295}
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700296EXPORT_SYMBOL(lockdep_off);
297
Steven Rostedt1d09daa2008-05-12 21:20:55 +0200298void lockdep_on(void)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700299{
300 current->lockdep_recursion--;
301}
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700302EXPORT_SYMBOL(lockdep_on);
303
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700304/*
305 * Debugging switches:
306 */
307
308#define VERBOSE 0
Ingo Molnar33e94e92006-12-13 00:34:41 -0800309#define VERY_VERBOSE 0
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700310
311#if VERBOSE
312# define HARDIRQ_VERBOSE 1
313# define SOFTIRQ_VERBOSE 1
Nick Piggincf40bd12009-01-21 08:12:39 +0100314# define RECLAIM_VERBOSE 1
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700315#else
316# define HARDIRQ_VERBOSE 0
317# define SOFTIRQ_VERBOSE 0
Nick Piggincf40bd12009-01-21 08:12:39 +0100318# define RECLAIM_VERBOSE 0
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700319#endif
320
Nick Piggincf40bd12009-01-21 08:12:39 +0100321#if VERBOSE || HARDIRQ_VERBOSE || SOFTIRQ_VERBOSE || RECLAIM_VERBOSE
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700322/*
323 * Quick filtering for interesting events:
324 */
325static int class_filter(struct lock_class *class)
326{
Andi Kleenf9829cc2006-07-10 04:44:01 -0700327#if 0
328 /* Example */
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700329 if (class->name_version == 1 &&
Andi Kleenf9829cc2006-07-10 04:44:01 -0700330 !strcmp(class->name, "lockname"))
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700331 return 1;
332 if (class->name_version == 1 &&
Andi Kleenf9829cc2006-07-10 04:44:01 -0700333 !strcmp(class->name, "&struct->lockfield"))
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700334 return 1;
Andi Kleenf9829cc2006-07-10 04:44:01 -0700335#endif
Ingo Molnara6640892006-12-13 00:34:39 -0800336 /* Filter everything else. 1 would be to allow everything else */
337 return 0;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700338}
339#endif
340
341static int verbose(struct lock_class *class)
342{
343#if VERBOSE
344 return class_filter(class);
345#endif
346 return 0;
347}
348
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700349/*
350 * Stack-trace: tightly packed array of stack backtrace
Ingo Molnar74c383f2006-12-13 00:34:43 -0800351 * addresses. Protected by the graph_lock.
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700352 */
353unsigned long nr_stack_trace_entries;
354static unsigned long stack_trace[MAX_STACK_TRACE_ENTRIES];
355
356static int save_trace(struct stack_trace *trace)
357{
358 trace->nr_entries = 0;
359 trace->max_entries = MAX_STACK_TRACE_ENTRIES - nr_stack_trace_entries;
360 trace->entries = stack_trace + nr_stack_trace_entries;
361
Andi Kleen5a1b3992006-09-26 10:52:34 +0200362 trace->skip = 3;
Andi Kleen5a1b3992006-09-26 10:52:34 +0200363
Christoph Hellwigab1b6f02007-05-08 00:23:29 -0700364 save_stack_trace(trace);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700365
366 trace->max_entries = trace->nr_entries;
367
368 nr_stack_trace_entries += trace->nr_entries;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700369
370 if (nr_stack_trace_entries == MAX_STACK_TRACE_ENTRIES) {
Ingo Molnar74c383f2006-12-13 00:34:43 -0800371 if (!debug_locks_off_graph_unlock())
372 return 0;
373
374 printk("BUG: MAX_STACK_TRACE_ENTRIES too low!\n");
375 printk("turning off the locking correctness validator.\n");
376 dump_stack();
377
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700378 return 0;
379 }
380
381 return 1;
382}
383
384unsigned int nr_hardirq_chains;
385unsigned int nr_softirq_chains;
386unsigned int nr_process_chains;
387unsigned int max_lockdep_depth;
388unsigned int max_recursion_depth;
389
David Miller419ca3f2008-07-29 21:45:03 -0700390static unsigned int lockdep_dependency_gen_id;
391
392static bool lockdep_dependency_visit(struct lock_class *source,
393 unsigned int depth)
394{
395 if (!depth)
396 lockdep_dependency_gen_id++;
397 if (source->dep_gen_id == lockdep_dependency_gen_id)
398 return true;
399 source->dep_gen_id = lockdep_dependency_gen_id;
400 return false;
401}
402
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700403#ifdef CONFIG_DEBUG_LOCKDEP
404/*
405 * We cannot printk in early bootup code. Not even early_printk()
406 * might work. So we mark any initialization errors and printk
407 * about it later on, in lockdep_info().
408 */
409static int lockdep_init_error;
Johannes Bergc71063c2007-07-19 01:49:02 -0700410static unsigned long lockdep_init_trace_data[20];
411static struct stack_trace lockdep_init_trace = {
412 .max_entries = ARRAY_SIZE(lockdep_init_trace_data),
413 .entries = lockdep_init_trace_data,
414};
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700415
416/*
417 * Various lockdep statistics:
418 */
419atomic_t chain_lookup_hits;
420atomic_t chain_lookup_misses;
421atomic_t hardirqs_on_events;
422atomic_t hardirqs_off_events;
423atomic_t redundant_hardirqs_on;
424atomic_t redundant_hardirqs_off;
425atomic_t softirqs_on_events;
426atomic_t softirqs_off_events;
427atomic_t redundant_softirqs_on;
428atomic_t redundant_softirqs_off;
429atomic_t nr_unused_locks;
430atomic_t nr_cyclic_checks;
431atomic_t nr_cyclic_check_recursions;
432atomic_t nr_find_usage_forwards_checks;
433atomic_t nr_find_usage_forwards_recursions;
434atomic_t nr_find_usage_backwards_checks;
435atomic_t nr_find_usage_backwards_recursions;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700436#endif
437
438/*
439 * Locking printouts:
440 */
441
Peter Zijlstrafabe9c42009-01-22 14:51:01 +0100442#define __USAGE(__STATE) \
Peter Zijlstrab4b136f2009-01-29 14:50:36 +0100443 [LOCK_USED_IN_##__STATE] = "IN-"__stringify(__STATE)"-W", \
444 [LOCK_ENABLED_##__STATE] = __stringify(__STATE)"-ON-W", \
445 [LOCK_USED_IN_##__STATE##_READ] = "IN-"__stringify(__STATE)"-R",\
446 [LOCK_ENABLED_##__STATE##_READ] = __stringify(__STATE)"-ON-R",
Peter Zijlstrafabe9c42009-01-22 14:51:01 +0100447
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700448static const char *usage_str[] =
449{
Peter Zijlstrafabe9c42009-01-22 14:51:01 +0100450#define LOCKDEP_STATE(__STATE) __USAGE(__STATE)
451#include "lockdep_states.h"
452#undef LOCKDEP_STATE
453 [LOCK_USED] = "INITIAL USE",
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700454};
455
456const char * __get_key_name(struct lockdep_subclass_key *key, char *str)
457{
Alexey Dobriyanffb45122007-05-08 00:28:41 -0700458 return kallsyms_lookup((unsigned long)key, NULL, NULL, NULL, str);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700459}
460
Peter Zijlstra3ff176c2009-01-22 17:40:42 +0100461static inline unsigned long lock_flag(enum lock_usage_bit bit)
462{
463 return 1UL << bit;
464}
465
466static char get_usage_char(struct lock_class *class, enum lock_usage_bit bit)
467{
468 char c = '.';
469
470 if (class->usage_mask & lock_flag(bit + 2))
471 c = '+';
472 if (class->usage_mask & lock_flag(bit)) {
473 c = '-';
474 if (class->usage_mask & lock_flag(bit + 2))
475 c = '?';
476 }
477
478 return c;
479}
480
Peter Zijlstraf510b232009-01-22 17:53:47 +0100481void get_usage_chars(struct lock_class *class, char usage[LOCK_USAGE_CHARS])
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700482{
Peter Zijlstraf510b232009-01-22 17:53:47 +0100483 int i = 0;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700484
Peter Zijlstraf510b232009-01-22 17:53:47 +0100485#define LOCKDEP_STATE(__STATE) \
486 usage[i++] = get_usage_char(class, LOCK_USED_IN_##__STATE); \
487 usage[i++] = get_usage_char(class, LOCK_USED_IN_##__STATE##_READ);
488#include "lockdep_states.h"
489#undef LOCKDEP_STATE
490
491 usage[i] = '\0';
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700492}
493
494static void print_lock_name(struct lock_class *class)
495{
Peter Zijlstraf510b232009-01-22 17:53:47 +0100496 char str[KSYM_NAME_LEN], usage[LOCK_USAGE_CHARS];
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700497 const char *name;
498
Peter Zijlstraf510b232009-01-22 17:53:47 +0100499 get_usage_chars(class, usage);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700500
501 name = class->name;
502 if (!name) {
503 name = __get_key_name(class->key, str);
504 printk(" (%s", name);
505 } else {
506 printk(" (%s", name);
507 if (class->name_version > 1)
508 printk("#%d", class->name_version);
509 if (class->subclass)
510 printk("/%d", class->subclass);
511 }
Peter Zijlstraf510b232009-01-22 17:53:47 +0100512 printk("){%s}", usage);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700513}
514
515static void print_lockdep_cache(struct lockdep_map *lock)
516{
517 const char *name;
Tejun Heo9281ace2007-07-17 04:03:51 -0700518 char str[KSYM_NAME_LEN];
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700519
520 name = lock->name;
521 if (!name)
522 name = __get_key_name(lock->key->subkeys, str);
523
524 printk("%s", name);
525}
526
527static void print_lock(struct held_lock *hlock)
528{
Dave Jonesf82b2172008-08-11 09:30:23 +0200529 print_lock_name(hlock_class(hlock));
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700530 printk(", at: ");
531 print_ip_sym(hlock->acquire_ip);
532}
533
534static void lockdep_print_held_locks(struct task_struct *curr)
535{
536 int i, depth = curr->lockdep_depth;
537
538 if (!depth) {
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -0700539 printk("no locks held by %s/%d.\n", curr->comm, task_pid_nr(curr));
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700540 return;
541 }
542 printk("%d lock%s held by %s/%d:\n",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -0700543 depth, depth > 1 ? "s" : "", curr->comm, task_pid_nr(curr));
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700544
545 for (i = 0; i < depth; i++) {
546 printk(" #%d: ", i);
547 print_lock(curr->held_locks + i);
548 }
549}
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700550
551static void print_lock_class_header(struct lock_class *class, int depth)
552{
553 int bit;
554
Andi Kleenf9829cc2006-07-10 04:44:01 -0700555 printk("%*s->", depth, "");
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700556 print_lock_name(class);
557 printk(" ops: %lu", class->ops);
558 printk(" {\n");
559
560 for (bit = 0; bit < LOCK_USAGE_STATES; bit++) {
561 if (class->usage_mask & (1 << bit)) {
562 int len = depth;
563
Andi Kleenf9829cc2006-07-10 04:44:01 -0700564 len += printk("%*s %s", depth, "", usage_str[bit]);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700565 len += printk(" at:\n");
566 print_stack_trace(class->usage_traces + bit, len);
567 }
568 }
Andi Kleenf9829cc2006-07-10 04:44:01 -0700569 printk("%*s }\n", depth, "");
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700570
Andi Kleenf9829cc2006-07-10 04:44:01 -0700571 printk("%*s ... key at: ",depth,"");
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700572 print_ip_sym((unsigned long)class->key);
573}
574
575/*
576 * printk all lock dependencies starting at <entry>:
577 */
Ingo Molnar7807faf2008-11-25 08:44:24 +0100578static void __used
579print_lock_dependencies(struct lock_class *class, int depth)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700580{
581 struct lock_list *entry;
582
David Miller419ca3f2008-07-29 21:45:03 -0700583 if (lockdep_dependency_visit(class, depth))
584 return;
585
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700586 if (DEBUG_LOCKS_WARN_ON(depth >= 20))
587 return;
588
589 print_lock_class_header(class, depth);
590
591 list_for_each_entry(entry, &class->locks_after, entry) {
Jarek Poplawskib23984d2006-12-06 20:36:23 -0800592 if (DEBUG_LOCKS_WARN_ON(!entry->class))
593 return;
594
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700595 print_lock_dependencies(entry->class, depth + 1);
596
Andi Kleenf9829cc2006-07-10 04:44:01 -0700597 printk("%*s ... acquired at:\n",depth,"");
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700598 print_stack_trace(&entry->trace, 2);
599 printk("\n");
600 }
601}
602
Dave Jones99de0552006-09-29 02:00:10 -0700603static void print_kernel_version(void)
604{
Serge E. Hallyn96b644b2006-10-02 02:18:13 -0700605 printk("%s %.*s\n", init_utsname()->release,
606 (int)strcspn(init_utsname()->version, " "),
607 init_utsname()->version);
Dave Jones99de0552006-09-29 02:00:10 -0700608}
609
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700610static int very_verbose(struct lock_class *class)
611{
612#if VERY_VERBOSE
613 return class_filter(class);
614#endif
615 return 0;
616}
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700617
618/*
619 * Is this the address of a static object:
620 */
621static int static_obj(void *obj)
622{
623 unsigned long start = (unsigned long) &_stext,
624 end = (unsigned long) &_end,
625 addr = (unsigned long) obj;
626#ifdef CONFIG_SMP
627 int i;
628#endif
629
630 /*
631 * static variable?
632 */
633 if ((addr >= start) && (addr < end))
634 return 1;
635
636#ifdef CONFIG_SMP
637 /*
638 * percpu var?
639 */
640 for_each_possible_cpu(i) {
641 start = (unsigned long) &__per_cpu_start + per_cpu_offset(i);
Ingo Molnar1ff56832006-11-17 19:57:22 +0100642 end = (unsigned long) &__per_cpu_start + PERCPU_ENOUGH_ROOM
643 + per_cpu_offset(i);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700644
645 if ((addr >= start) && (addr < end))
646 return 1;
647 }
648#endif
649
650 /*
651 * module var?
652 */
653 return is_module_address(addr);
654}
655
656/*
657 * To make lock name printouts unique, we calculate a unique
658 * class->name_version generation counter:
659 */
660static int count_matching_names(struct lock_class *new_class)
661{
662 struct lock_class *class;
663 int count = 0;
664
665 if (!new_class->name)
666 return 0;
667
668 list_for_each_entry(class, &all_lock_classes, lock_entry) {
669 if (new_class->key - new_class->subclass == class->key)
670 return class->name_version;
671 if (class->name && !strcmp(class->name, new_class->name))
672 count = max(count, class->name_version);
673 }
674
675 return count + 1;
676}
677
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700678/*
679 * Register a lock's class in the hash-table, if the class is not present
680 * yet. Otherwise we look it up. We cache the result in the lock object
681 * itself, so actual lookup of the hash should be once per lock object.
682 */
683static inline struct lock_class *
Ingo Molnard6d897c2006-07-10 04:44:04 -0700684look_up_lock_class(struct lockdep_map *lock, unsigned int subclass)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700685{
686 struct lockdep_subclass_key *key;
687 struct list_head *hash_head;
688 struct lock_class *class;
689
690#ifdef CONFIG_DEBUG_LOCKDEP
691 /*
692 * If the architecture calls into lockdep before initializing
693 * the hashes then we'll warn about it later. (we cannot printk
694 * right now)
695 */
696 if (unlikely(!lockdep_initialized)) {
697 lockdep_init();
698 lockdep_init_error = 1;
Johannes Bergc71063c2007-07-19 01:49:02 -0700699 save_stack_trace(&lockdep_init_trace);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700700 }
701#endif
702
703 /*
704 * Static locks do not have their class-keys yet - for them the key
705 * is the lock object itself:
706 */
707 if (unlikely(!lock->key))
708 lock->key = (void *)lock;
709
710 /*
711 * NOTE: the class-key must be unique. For dynamic locks, a static
712 * lock_class_key variable is passed in through the mutex_init()
713 * (or spin_lock_init()) call - which acts as the key. For static
714 * locks we use the lock object itself as the key.
715 */
Peter Zijlstra4b32d0a2007-07-19 01:48:59 -0700716 BUILD_BUG_ON(sizeof(struct lock_class_key) >
717 sizeof(struct lockdep_map));
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700718
719 key = lock->key->subkeys + subclass;
720
721 hash_head = classhashentry(key);
722
723 /*
724 * We can walk the hash lockfree, because the hash only
725 * grows, and we are careful when adding entries to the end:
726 */
Peter Zijlstra4b32d0a2007-07-19 01:48:59 -0700727 list_for_each_entry(class, hash_head, hash_entry) {
728 if (class->key == key) {
729 WARN_ON_ONCE(class->name != lock->name);
Ingo Molnard6d897c2006-07-10 04:44:04 -0700730 return class;
Peter Zijlstra4b32d0a2007-07-19 01:48:59 -0700731 }
732 }
Ingo Molnard6d897c2006-07-10 04:44:04 -0700733
734 return NULL;
735}
736
737/*
738 * Register a lock's class in the hash-table, if the class is not present
739 * yet. Otherwise we look it up. We cache the result in the lock object
740 * itself, so actual lookup of the hash should be once per lock object.
741 */
742static inline struct lock_class *
Peter Zijlstra4dfbb9d2006-10-11 01:45:14 -0400743register_lock_class(struct lockdep_map *lock, unsigned int subclass, int force)
Ingo Molnard6d897c2006-07-10 04:44:04 -0700744{
745 struct lockdep_subclass_key *key;
746 struct list_head *hash_head;
747 struct lock_class *class;
Ingo Molnar70e45062006-12-06 20:40:50 -0800748 unsigned long flags;
Ingo Molnard6d897c2006-07-10 04:44:04 -0700749
750 class = look_up_lock_class(lock, subclass);
751 if (likely(class))
752 return class;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700753
754 /*
755 * Debug-check: all keys must be persistent!
756 */
757 if (!static_obj(lock->key)) {
758 debug_locks_off();
759 printk("INFO: trying to register non-static key.\n");
760 printk("the code is fine but needs lockdep annotation.\n");
761 printk("turning off the locking correctness validator.\n");
762 dump_stack();
763
764 return NULL;
765 }
766
Ingo Molnard6d897c2006-07-10 04:44:04 -0700767 key = lock->key->subkeys + subclass;
768 hash_head = classhashentry(key);
769
Ingo Molnar70e45062006-12-06 20:40:50 -0800770 raw_local_irq_save(flags);
Ingo Molnar74c383f2006-12-13 00:34:43 -0800771 if (!graph_lock()) {
772 raw_local_irq_restore(flags);
773 return NULL;
774 }
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700775 /*
776 * We have to do the hash-walk again, to avoid races
777 * with another CPU:
778 */
779 list_for_each_entry(class, hash_head, hash_entry)
780 if (class->key == key)
781 goto out_unlock_set;
782 /*
783 * Allocate a new key from the static array, and add it to
784 * the hash:
785 */
786 if (nr_lock_classes >= MAX_LOCKDEP_KEYS) {
Ingo Molnar74c383f2006-12-13 00:34:43 -0800787 if (!debug_locks_off_graph_unlock()) {
788 raw_local_irq_restore(flags);
789 return NULL;
790 }
Ingo Molnar70e45062006-12-06 20:40:50 -0800791 raw_local_irq_restore(flags);
Ingo Molnar74c383f2006-12-13 00:34:43 -0800792
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700793 printk("BUG: MAX_LOCKDEP_KEYS too low!\n");
794 printk("turning off the locking correctness validator.\n");
Peter Zijlstraeedeeab2009-03-18 12:38:47 +0100795 dump_stack();
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700796 return NULL;
797 }
798 class = lock_classes + nr_lock_classes++;
799 debug_atomic_inc(&nr_unused_locks);
800 class->key = key;
801 class->name = lock->name;
802 class->subclass = subclass;
803 INIT_LIST_HEAD(&class->lock_entry);
804 INIT_LIST_HEAD(&class->locks_before);
805 INIT_LIST_HEAD(&class->locks_after);
806 class->name_version = count_matching_names(class);
807 /*
808 * We use RCU's safe list-add method to make
809 * parallel walking of the hash-list safe:
810 */
811 list_add_tail_rcu(&class->hash_entry, hash_head);
Dale Farnsworth14811972008-02-25 23:03:02 +0100812 /*
813 * Add it to the global list of classes:
814 */
815 list_add_tail_rcu(&class->lock_entry, &all_lock_classes);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700816
817 if (verbose(class)) {
Ingo Molnar74c383f2006-12-13 00:34:43 -0800818 graph_unlock();
Ingo Molnar70e45062006-12-06 20:40:50 -0800819 raw_local_irq_restore(flags);
Ingo Molnar74c383f2006-12-13 00:34:43 -0800820
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700821 printk("\nnew class %p: %s", class->key, class->name);
822 if (class->name_version > 1)
823 printk("#%d", class->name_version);
824 printk("\n");
825 dump_stack();
Ingo Molnar74c383f2006-12-13 00:34:43 -0800826
Ingo Molnar70e45062006-12-06 20:40:50 -0800827 raw_local_irq_save(flags);
Ingo Molnar74c383f2006-12-13 00:34:43 -0800828 if (!graph_lock()) {
829 raw_local_irq_restore(flags);
830 return NULL;
831 }
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700832 }
833out_unlock_set:
Ingo Molnar74c383f2006-12-13 00:34:43 -0800834 graph_unlock();
Ingo Molnar70e45062006-12-06 20:40:50 -0800835 raw_local_irq_restore(flags);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700836
Peter Zijlstra4dfbb9d2006-10-11 01:45:14 -0400837 if (!subclass || force)
Ingo Molnard6d897c2006-07-10 04:44:04 -0700838 lock->class_cache = class;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700839
Jarek Poplawski381a2292007-02-10 01:44:58 -0800840 if (DEBUG_LOCKS_WARN_ON(class->subclass != subclass))
841 return NULL;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700842
843 return class;
844}
845
Peter Zijlstraca58abc2007-07-19 01:48:53 -0700846#ifdef CONFIG_PROVE_LOCKING
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700847/*
Peter Zijlstra8e182572007-07-19 01:48:54 -0700848 * Allocate a lockdep entry. (assumes the graph_lock held, returns
849 * with NULL on failure)
850 */
851static struct lock_list *alloc_list_entry(void)
852{
853 if (nr_list_entries >= MAX_LOCKDEP_ENTRIES) {
854 if (!debug_locks_off_graph_unlock())
855 return NULL;
856
857 printk("BUG: MAX_LOCKDEP_ENTRIES too low!\n");
858 printk("turning off the locking correctness validator.\n");
Peter Zijlstraeedeeab2009-03-18 12:38:47 +0100859 dump_stack();
Peter Zijlstra8e182572007-07-19 01:48:54 -0700860 return NULL;
861 }
862 return list_entries + nr_list_entries++;
863}
864
865/*
866 * Add a new dependency to the head of the list:
867 */
868static int add_lock_to_list(struct lock_class *class, struct lock_class *this,
869 struct list_head *head, unsigned long ip, int distance)
870{
871 struct lock_list *entry;
872 /*
873 * Lock not present yet - get a new dependency struct and
874 * add it to the list:
875 */
876 entry = alloc_list_entry();
877 if (!entry)
878 return 0;
879
Peter Zijlstra8e182572007-07-19 01:48:54 -0700880 if (!save_trace(&entry->trace))
881 return 0;
882
Zhu Yi74870172008-08-27 14:33:00 +0800883 entry->class = this;
884 entry->distance = distance;
Peter Zijlstra8e182572007-07-19 01:48:54 -0700885 /*
886 * Since we never remove from the dependency list, the list can
887 * be walked lockless by other CPUs, it's only allocation
888 * that must be protected by the spinlock. But this also means
889 * we must make new entries visible only once writes to the
890 * entry become visible - hence the RCU op:
891 */
892 list_add_tail_rcu(&entry->entry, head);
893
894 return 1;
895}
896
897/*
898 * Recursive, forwards-direction lock-dependency checking, used for
899 * both noncyclic checking and for hardirq-unsafe/softirq-unsafe
900 * checking.
901 *
902 * (to keep the stackframe of the recursive functions small we
903 * use these global variables, and we also mark various helper
904 * functions as noinline.)
905 */
906static struct held_lock *check_source, *check_target;
907
908/*
909 * Print a dependency chain entry (this is only done when a deadlock
910 * has been detected):
911 */
912static noinline int
913print_circular_bug_entry(struct lock_list *target, unsigned int depth)
914{
915 if (debug_locks_silent)
916 return 0;
917 printk("\n-> #%u", depth);
918 print_lock_name(target->class);
919 printk(":\n");
920 print_stack_trace(&target->trace, 6);
921
922 return 0;
923}
924
925/*
926 * When a circular dependency is detected, print the
927 * header first:
928 */
929static noinline int
930print_circular_bug_header(struct lock_list *entry, unsigned int depth)
931{
932 struct task_struct *curr = current;
933
934 if (!debug_locks_off_graph_unlock() || debug_locks_silent)
935 return 0;
936
937 printk("\n=======================================================\n");
938 printk( "[ INFO: possible circular locking dependency detected ]\n");
939 print_kernel_version();
940 printk( "-------------------------------------------------------\n");
941 printk("%s/%d is trying to acquire lock:\n",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -0700942 curr->comm, task_pid_nr(curr));
Peter Zijlstra8e182572007-07-19 01:48:54 -0700943 print_lock(check_source);
944 printk("\nbut task is already holding lock:\n");
945 print_lock(check_target);
946 printk("\nwhich lock already depends on the new lock.\n\n");
947 printk("\nthe existing dependency chain (in reverse order) is:\n");
948
949 print_circular_bug_entry(entry, depth);
950
951 return 0;
952}
953
954static noinline int print_circular_bug_tail(void)
955{
956 struct task_struct *curr = current;
957 struct lock_list this;
958
959 if (debug_locks_silent)
960 return 0;
961
Dave Jonesf82b2172008-08-11 09:30:23 +0200962 this.class = hlock_class(check_source);
Peter Zijlstra8e182572007-07-19 01:48:54 -0700963 if (!save_trace(&this.trace))
964 return 0;
965
966 print_circular_bug_entry(&this, 0);
967
968 printk("\nother info that might help us debug this:\n\n");
969 lockdep_print_held_locks(curr);
970
971 printk("\nstack backtrace:\n");
972 dump_stack();
973
974 return 0;
975}
976
977#define RECURSION_LIMIT 40
978
979static int noinline print_infinite_recursion_bug(void)
980{
981 if (!debug_locks_off_graph_unlock())
982 return 0;
983
984 WARN_ON(1);
985
986 return 0;
987}
988
David Miller419ca3f2008-07-29 21:45:03 -0700989unsigned long __lockdep_count_forward_deps(struct lock_class *class,
990 unsigned int depth)
991{
992 struct lock_list *entry;
993 unsigned long ret = 1;
994
995 if (lockdep_dependency_visit(class, depth))
996 return 0;
997
998 /*
999 * Recurse this class's dependency list:
1000 */
1001 list_for_each_entry(entry, &class->locks_after, entry)
1002 ret += __lockdep_count_forward_deps(entry->class, depth + 1);
1003
1004 return ret;
1005}
1006
1007unsigned long lockdep_count_forward_deps(struct lock_class *class)
1008{
1009 unsigned long ret, flags;
1010
1011 local_irq_save(flags);
1012 __raw_spin_lock(&lockdep_lock);
1013 ret = __lockdep_count_forward_deps(class, 0);
1014 __raw_spin_unlock(&lockdep_lock);
1015 local_irq_restore(flags);
1016
1017 return ret;
1018}
1019
1020unsigned long __lockdep_count_backward_deps(struct lock_class *class,
1021 unsigned int depth)
1022{
1023 struct lock_list *entry;
1024 unsigned long ret = 1;
1025
1026 if (lockdep_dependency_visit(class, depth))
1027 return 0;
1028 /*
1029 * Recurse this class's dependency list:
1030 */
1031 list_for_each_entry(entry, &class->locks_before, entry)
1032 ret += __lockdep_count_backward_deps(entry->class, depth + 1);
1033
1034 return ret;
1035}
1036
1037unsigned long lockdep_count_backward_deps(struct lock_class *class)
1038{
1039 unsigned long ret, flags;
1040
1041 local_irq_save(flags);
1042 __raw_spin_lock(&lockdep_lock);
1043 ret = __lockdep_count_backward_deps(class, 0);
1044 __raw_spin_unlock(&lockdep_lock);
1045 local_irq_restore(flags);
1046
1047 return ret;
1048}
1049
Peter Zijlstra8e182572007-07-19 01:48:54 -07001050/*
1051 * Prove that the dependency graph starting at <entry> can not
1052 * lead to <target>. Print an error and return 0 if it does.
1053 */
1054static noinline int
1055check_noncircular(struct lock_class *source, unsigned int depth)
1056{
1057 struct lock_list *entry;
1058
David Miller419ca3f2008-07-29 21:45:03 -07001059 if (lockdep_dependency_visit(source, depth))
1060 return 1;
1061
Peter Zijlstra8e182572007-07-19 01:48:54 -07001062 debug_atomic_inc(&nr_cyclic_check_recursions);
1063 if (depth > max_recursion_depth)
1064 max_recursion_depth = depth;
1065 if (depth >= RECURSION_LIMIT)
1066 return print_infinite_recursion_bug();
1067 /*
1068 * Check this lock's dependency list:
1069 */
1070 list_for_each_entry(entry, &source->locks_after, entry) {
Dave Jonesf82b2172008-08-11 09:30:23 +02001071 if (entry->class == hlock_class(check_target))
Peter Zijlstra8e182572007-07-19 01:48:54 -07001072 return print_circular_bug_header(entry, depth+1);
1073 debug_atomic_inc(&nr_cyclic_checks);
1074 if (!check_noncircular(entry->class, depth+1))
1075 return print_circular_bug_entry(entry, depth+1);
1076 }
1077 return 1;
1078}
1079
Steven Rostedt81d68a92008-05-12 21:20:42 +02001080#if defined(CONFIG_TRACE_IRQFLAGS) && defined(CONFIG_PROVE_LOCKING)
Peter Zijlstra8e182572007-07-19 01:48:54 -07001081/*
1082 * Forwards and backwards subgraph searching, for the purposes of
1083 * proving that two subgraphs can be connected by a new dependency
1084 * without creating any illegal irq-safe -> irq-unsafe lock dependency.
1085 */
1086static enum lock_usage_bit find_usage_bit;
1087static struct lock_class *forwards_match, *backwards_match;
1088
1089/*
1090 * Find a node in the forwards-direction dependency sub-graph starting
1091 * at <source> that matches <find_usage_bit>.
1092 *
1093 * Return 2 if such a node exists in the subgraph, and put that node
1094 * into <forwards_match>.
1095 *
1096 * Return 1 otherwise and keep <forwards_match> unchanged.
1097 * Return 0 on error.
1098 */
1099static noinline int
1100find_usage_forwards(struct lock_class *source, unsigned int depth)
1101{
1102 struct lock_list *entry;
1103 int ret;
1104
David Miller419ca3f2008-07-29 21:45:03 -07001105 if (lockdep_dependency_visit(source, depth))
1106 return 1;
1107
Peter Zijlstra8e182572007-07-19 01:48:54 -07001108 if (depth > max_recursion_depth)
1109 max_recursion_depth = depth;
1110 if (depth >= RECURSION_LIMIT)
1111 return print_infinite_recursion_bug();
1112
1113 debug_atomic_inc(&nr_find_usage_forwards_checks);
1114 if (source->usage_mask & (1 << find_usage_bit)) {
1115 forwards_match = source;
1116 return 2;
1117 }
1118
1119 /*
1120 * Check this lock's dependency list:
1121 */
1122 list_for_each_entry(entry, &source->locks_after, entry) {
1123 debug_atomic_inc(&nr_find_usage_forwards_recursions);
1124 ret = find_usage_forwards(entry->class, depth+1);
1125 if (ret == 2 || ret == 0)
1126 return ret;
1127 }
1128 return 1;
1129}
1130
1131/*
1132 * Find a node in the backwards-direction dependency sub-graph starting
1133 * at <source> that matches <find_usage_bit>.
1134 *
1135 * Return 2 if such a node exists in the subgraph, and put that node
1136 * into <backwards_match>.
1137 *
1138 * Return 1 otherwise and keep <backwards_match> unchanged.
1139 * Return 0 on error.
1140 */
Steven Rostedt1d09daa2008-05-12 21:20:55 +02001141static noinline int
Peter Zijlstra8e182572007-07-19 01:48:54 -07001142find_usage_backwards(struct lock_class *source, unsigned int depth)
1143{
1144 struct lock_list *entry;
1145 int ret;
1146
David Miller419ca3f2008-07-29 21:45:03 -07001147 if (lockdep_dependency_visit(source, depth))
1148 return 1;
1149
Peter Zijlstra8e182572007-07-19 01:48:54 -07001150 if (!__raw_spin_is_locked(&lockdep_lock))
1151 return DEBUG_LOCKS_WARN_ON(1);
1152
1153 if (depth > max_recursion_depth)
1154 max_recursion_depth = depth;
1155 if (depth >= RECURSION_LIMIT)
1156 return print_infinite_recursion_bug();
1157
1158 debug_atomic_inc(&nr_find_usage_backwards_checks);
1159 if (source->usage_mask & (1 << find_usage_bit)) {
1160 backwards_match = source;
1161 return 2;
1162 }
1163
Dave Jonesf82b2172008-08-11 09:30:23 +02001164 if (!source && debug_locks_off_graph_unlock()) {
1165 WARN_ON(1);
1166 return 0;
1167 }
1168
Peter Zijlstra8e182572007-07-19 01:48:54 -07001169 /*
1170 * Check this lock's dependency list:
1171 */
1172 list_for_each_entry(entry, &source->locks_before, entry) {
1173 debug_atomic_inc(&nr_find_usage_backwards_recursions);
1174 ret = find_usage_backwards(entry->class, depth+1);
1175 if (ret == 2 || ret == 0)
1176 return ret;
1177 }
1178 return 1;
1179}
1180
1181static int
1182print_bad_irq_dependency(struct task_struct *curr,
1183 struct held_lock *prev,
1184 struct held_lock *next,
1185 enum lock_usage_bit bit1,
1186 enum lock_usage_bit bit2,
1187 const char *irqclass)
1188{
1189 if (!debug_locks_off_graph_unlock() || debug_locks_silent)
1190 return 0;
1191
1192 printk("\n======================================================\n");
1193 printk( "[ INFO: %s-safe -> %s-unsafe lock order detected ]\n",
1194 irqclass, irqclass);
1195 print_kernel_version();
1196 printk( "------------------------------------------------------\n");
1197 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 -07001198 curr->comm, task_pid_nr(curr),
Peter Zijlstra8e182572007-07-19 01:48:54 -07001199 curr->hardirq_context, hardirq_count() >> HARDIRQ_SHIFT,
1200 curr->softirq_context, softirq_count() >> SOFTIRQ_SHIFT,
1201 curr->hardirqs_enabled,
1202 curr->softirqs_enabled);
1203 print_lock(next);
1204
1205 printk("\nand this task is already holding:\n");
1206 print_lock(prev);
1207 printk("which would create a new lock dependency:\n");
Dave Jonesf82b2172008-08-11 09:30:23 +02001208 print_lock_name(hlock_class(prev));
Peter Zijlstra8e182572007-07-19 01:48:54 -07001209 printk(" ->");
Dave Jonesf82b2172008-08-11 09:30:23 +02001210 print_lock_name(hlock_class(next));
Peter Zijlstra8e182572007-07-19 01:48:54 -07001211 printk("\n");
1212
1213 printk("\nbut this new dependency connects a %s-irq-safe lock:\n",
1214 irqclass);
1215 print_lock_name(backwards_match);
1216 printk("\n... which became %s-irq-safe at:\n", irqclass);
1217
1218 print_stack_trace(backwards_match->usage_traces + bit1, 1);
1219
1220 printk("\nto a %s-irq-unsafe lock:\n", irqclass);
1221 print_lock_name(forwards_match);
1222 printk("\n... which became %s-irq-unsafe at:\n", irqclass);
1223 printk("...");
1224
1225 print_stack_trace(forwards_match->usage_traces + bit2, 1);
1226
1227 printk("\nother info that might help us debug this:\n\n");
1228 lockdep_print_held_locks(curr);
1229
1230 printk("\nthe %s-irq-safe lock's dependencies:\n", irqclass);
1231 print_lock_dependencies(backwards_match, 0);
1232
1233 printk("\nthe %s-irq-unsafe lock's dependencies:\n", irqclass);
1234 print_lock_dependencies(forwards_match, 0);
1235
1236 printk("\nstack backtrace:\n");
1237 dump_stack();
1238
1239 return 0;
1240}
1241
1242static int
1243check_usage(struct task_struct *curr, struct held_lock *prev,
1244 struct held_lock *next, enum lock_usage_bit bit_backwards,
1245 enum lock_usage_bit bit_forwards, const char *irqclass)
1246{
1247 int ret;
1248
1249 find_usage_bit = bit_backwards;
1250 /* fills in <backwards_match> */
Dave Jonesf82b2172008-08-11 09:30:23 +02001251 ret = find_usage_backwards(hlock_class(prev), 0);
Peter Zijlstra8e182572007-07-19 01:48:54 -07001252 if (!ret || ret == 1)
1253 return ret;
1254
1255 find_usage_bit = bit_forwards;
Dave Jonesf82b2172008-08-11 09:30:23 +02001256 ret = find_usage_forwards(hlock_class(next), 0);
Peter Zijlstra8e182572007-07-19 01:48:54 -07001257 if (!ret || ret == 1)
1258 return ret;
1259 /* ret == 2 */
1260 return print_bad_irq_dependency(curr, prev, next,
1261 bit_backwards, bit_forwards, irqclass);
1262}
1263
Peter Zijlstra4f367d8a2009-01-22 18:10:42 +01001264static const char *state_names[] = {
1265#define LOCKDEP_STATE(__STATE) \
Peter Zijlstrab4b136f2009-01-29 14:50:36 +01001266 __stringify(__STATE),
Peter Zijlstra4f367d8a2009-01-22 18:10:42 +01001267#include "lockdep_states.h"
1268#undef LOCKDEP_STATE
1269};
1270
1271static const char *state_rnames[] = {
1272#define LOCKDEP_STATE(__STATE) \
Peter Zijlstrab4b136f2009-01-29 14:50:36 +01001273 __stringify(__STATE)"-READ",
Peter Zijlstra4f367d8a2009-01-22 18:10:42 +01001274#include "lockdep_states.h"
1275#undef LOCKDEP_STATE
1276};
1277
1278static inline const char *state_name(enum lock_usage_bit bit)
1279{
1280 return (bit & 1) ? state_rnames[bit >> 2] : state_names[bit >> 2];
1281}
1282
1283static int exclusive_bit(int new_bit)
1284{
1285 /*
1286 * USED_IN
1287 * USED_IN_READ
1288 * ENABLED
1289 * ENABLED_READ
1290 *
1291 * bit 0 - write/read
1292 * bit 1 - used_in/enabled
1293 * bit 2+ state
1294 */
1295
1296 int state = new_bit & ~3;
1297 int dir = new_bit & 2;
1298
1299 /*
1300 * keep state, bit flip the direction and strip read.
1301 */
1302 return state | (dir ^ 2);
1303}
1304
1305static int check_irq_usage(struct task_struct *curr, struct held_lock *prev,
1306 struct held_lock *next, enum lock_usage_bit bit)
Peter Zijlstra8e182572007-07-19 01:48:54 -07001307{
1308 /*
1309 * Prove that the new dependency does not connect a hardirq-safe
1310 * lock with a hardirq-unsafe lock - to achieve this we search
1311 * the backwards-subgraph starting at <prev>, and the
1312 * forwards-subgraph starting at <next>:
1313 */
Peter Zijlstra4f367d8a2009-01-22 18:10:42 +01001314 if (!check_usage(curr, prev, next, bit,
1315 exclusive_bit(bit), state_name(bit)))
Peter Zijlstra8e182572007-07-19 01:48:54 -07001316 return 0;
1317
Peter Zijlstra4f367d8a2009-01-22 18:10:42 +01001318 bit++; /* _READ */
1319
Peter Zijlstra8e182572007-07-19 01:48:54 -07001320 /*
1321 * Prove that the new dependency does not connect a hardirq-safe-read
1322 * lock with a hardirq-unsafe lock - to achieve this we search
1323 * the backwards-subgraph starting at <prev>, and the
1324 * forwards-subgraph starting at <next>:
1325 */
Peter Zijlstra4f367d8a2009-01-22 18:10:42 +01001326 if (!check_usage(curr, prev, next, bit,
1327 exclusive_bit(bit), state_name(bit)))
Peter Zijlstra8e182572007-07-19 01:48:54 -07001328 return 0;
1329
Peter Zijlstra4f367d8a2009-01-22 18:10:42 +01001330 return 1;
1331}
Peter Zijlstra8e182572007-07-19 01:48:54 -07001332
Peter Zijlstra4f367d8a2009-01-22 18:10:42 +01001333static int
1334check_prev_add_irq(struct task_struct *curr, struct held_lock *prev,
1335 struct held_lock *next)
1336{
1337#define LOCKDEP_STATE(__STATE) \
1338 if (!check_irq_usage(curr, prev, next, LOCK_USED_IN_##__STATE)) \
Nick Piggincf40bd12009-01-21 08:12:39 +01001339 return 0;
Peter Zijlstra4f367d8a2009-01-22 18:10:42 +01001340#include "lockdep_states.h"
1341#undef LOCKDEP_STATE
Nick Piggincf40bd12009-01-21 08:12:39 +01001342
Peter Zijlstra8e182572007-07-19 01:48:54 -07001343 return 1;
1344}
1345
1346static void inc_chains(void)
1347{
1348 if (current->hardirq_context)
1349 nr_hardirq_chains++;
1350 else {
1351 if (current->softirq_context)
1352 nr_softirq_chains++;
1353 else
1354 nr_process_chains++;
1355 }
1356}
1357
1358#else
1359
1360static inline int
1361check_prev_add_irq(struct task_struct *curr, struct held_lock *prev,
1362 struct held_lock *next)
1363{
1364 return 1;
1365}
1366
1367static inline void inc_chains(void)
1368{
1369 nr_process_chains++;
1370}
1371
1372#endif
1373
1374static int
1375print_deadlock_bug(struct task_struct *curr, struct held_lock *prev,
1376 struct held_lock *next)
1377{
1378 if (!debug_locks_off_graph_unlock() || debug_locks_silent)
1379 return 0;
1380
1381 printk("\n=============================================\n");
1382 printk( "[ INFO: possible recursive locking detected ]\n");
1383 print_kernel_version();
1384 printk( "---------------------------------------------\n");
1385 printk("%s/%d is trying to acquire lock:\n",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07001386 curr->comm, task_pid_nr(curr));
Peter Zijlstra8e182572007-07-19 01:48:54 -07001387 print_lock(next);
1388 printk("\nbut task is already holding lock:\n");
1389 print_lock(prev);
1390
1391 printk("\nother info that might help us debug this:\n");
1392 lockdep_print_held_locks(curr);
1393
1394 printk("\nstack backtrace:\n");
1395 dump_stack();
1396
1397 return 0;
1398}
1399
1400/*
1401 * Check whether we are holding such a class already.
1402 *
1403 * (Note that this has to be done separately, because the graph cannot
1404 * detect such classes of deadlocks.)
1405 *
1406 * Returns: 0 on deadlock detected, 1 on OK, 2 on recursive read
1407 */
1408static int
1409check_deadlock(struct task_struct *curr, struct held_lock *next,
1410 struct lockdep_map *next_instance, int read)
1411{
1412 struct held_lock *prev;
Peter Zijlstra7531e2f2008-08-11 09:30:24 +02001413 struct held_lock *nest = NULL;
Peter Zijlstra8e182572007-07-19 01:48:54 -07001414 int i;
1415
1416 for (i = 0; i < curr->lockdep_depth; i++) {
1417 prev = curr->held_locks + i;
Peter Zijlstra7531e2f2008-08-11 09:30:24 +02001418
1419 if (prev->instance == next->nest_lock)
1420 nest = prev;
1421
Dave Jonesf82b2172008-08-11 09:30:23 +02001422 if (hlock_class(prev) != hlock_class(next))
Peter Zijlstra8e182572007-07-19 01:48:54 -07001423 continue;
Peter Zijlstra7531e2f2008-08-11 09:30:24 +02001424
Peter Zijlstra8e182572007-07-19 01:48:54 -07001425 /*
1426 * Allow read-after-read recursion of the same
1427 * lock class (i.e. read_lock(lock)+read_lock(lock)):
1428 */
1429 if ((read == 2) && prev->read)
1430 return 2;
Peter Zijlstra7531e2f2008-08-11 09:30:24 +02001431
1432 /*
1433 * We're holding the nest_lock, which serializes this lock's
1434 * nesting behaviour.
1435 */
1436 if (nest)
1437 return 2;
1438
Peter Zijlstra8e182572007-07-19 01:48:54 -07001439 return print_deadlock_bug(curr, prev, next);
1440 }
1441 return 1;
1442}
1443
1444/*
1445 * There was a chain-cache miss, and we are about to add a new dependency
1446 * to a previous lock. We recursively validate the following rules:
1447 *
1448 * - would the adding of the <prev> -> <next> dependency create a
1449 * circular dependency in the graph? [== circular deadlock]
1450 *
1451 * - does the new prev->next dependency connect any hardirq-safe lock
1452 * (in the full backwards-subgraph starting at <prev>) with any
1453 * hardirq-unsafe lock (in the full forwards-subgraph starting at
1454 * <next>)? [== illegal lock inversion with hardirq contexts]
1455 *
1456 * - does the new prev->next dependency connect any softirq-safe lock
1457 * (in the full backwards-subgraph starting at <prev>) with any
1458 * softirq-unsafe lock (in the full forwards-subgraph starting at
1459 * <next>)? [== illegal lock inversion with softirq contexts]
1460 *
1461 * any of these scenarios could lead to a deadlock.
1462 *
1463 * Then if all the validations pass, we add the forwards and backwards
1464 * dependency.
1465 */
1466static int
1467check_prev_add(struct task_struct *curr, struct held_lock *prev,
1468 struct held_lock *next, int distance)
1469{
1470 struct lock_list *entry;
1471 int ret;
1472
1473 /*
1474 * Prove that the new <prev> -> <next> dependency would not
1475 * create a circular dependency in the graph. (We do this by
1476 * forward-recursing into the graph starting at <next>, and
1477 * checking whether we can reach <prev>.)
1478 *
1479 * We are using global variables to control the recursion, to
1480 * keep the stackframe size of the recursive functions low:
1481 */
1482 check_source = next;
1483 check_target = prev;
Dave Jonesf82b2172008-08-11 09:30:23 +02001484 if (!(check_noncircular(hlock_class(next), 0)))
Peter Zijlstra8e182572007-07-19 01:48:54 -07001485 return print_circular_bug_tail();
1486
1487 if (!check_prev_add_irq(curr, prev, next))
1488 return 0;
1489
1490 /*
1491 * For recursive read-locks we do all the dependency checks,
1492 * but we dont store read-triggered dependencies (only
1493 * write-triggered dependencies). This ensures that only the
1494 * write-side dependencies matter, and that if for example a
1495 * write-lock never takes any other locks, then the reads are
1496 * equivalent to a NOP.
1497 */
1498 if (next->read == 2 || prev->read == 2)
1499 return 1;
1500 /*
1501 * Is the <prev> -> <next> dependency already present?
1502 *
1503 * (this may occur even though this is a new chain: consider
1504 * e.g. the L1 -> L2 -> L3 -> L4 and the L5 -> L1 -> L2 -> L3
1505 * chains - the second one will be new, but L1 already has
1506 * L2 added to its dependency list, due to the first chain.)
1507 */
Dave Jonesf82b2172008-08-11 09:30:23 +02001508 list_for_each_entry(entry, &hlock_class(prev)->locks_after, entry) {
1509 if (entry->class == hlock_class(next)) {
Peter Zijlstra8e182572007-07-19 01:48:54 -07001510 if (distance == 1)
1511 entry->distance = 1;
1512 return 2;
1513 }
1514 }
1515
1516 /*
1517 * Ok, all validations passed, add the new lock
1518 * to the previous lock's dependency list:
1519 */
Dave Jonesf82b2172008-08-11 09:30:23 +02001520 ret = add_lock_to_list(hlock_class(prev), hlock_class(next),
1521 &hlock_class(prev)->locks_after,
1522 next->acquire_ip, distance);
Peter Zijlstra8e182572007-07-19 01:48:54 -07001523
1524 if (!ret)
1525 return 0;
1526
Dave Jonesf82b2172008-08-11 09:30:23 +02001527 ret = add_lock_to_list(hlock_class(next), hlock_class(prev),
1528 &hlock_class(next)->locks_before,
1529 next->acquire_ip, distance);
Peter Zijlstra8e182572007-07-19 01:48:54 -07001530 if (!ret)
1531 return 0;
1532
1533 /*
1534 * Debugging printouts:
1535 */
Dave Jonesf82b2172008-08-11 09:30:23 +02001536 if (verbose(hlock_class(prev)) || verbose(hlock_class(next))) {
Peter Zijlstra8e182572007-07-19 01:48:54 -07001537 graph_unlock();
1538 printk("\n new dependency: ");
Dave Jonesf82b2172008-08-11 09:30:23 +02001539 print_lock_name(hlock_class(prev));
Peter Zijlstra8e182572007-07-19 01:48:54 -07001540 printk(" => ");
Dave Jonesf82b2172008-08-11 09:30:23 +02001541 print_lock_name(hlock_class(next));
Peter Zijlstra8e182572007-07-19 01:48:54 -07001542 printk("\n");
1543 dump_stack();
1544 return graph_lock();
1545 }
1546 return 1;
1547}
1548
1549/*
1550 * Add the dependency to all directly-previous locks that are 'relevant'.
1551 * The ones that are relevant are (in increasing distance from curr):
1552 * all consecutive trylock entries and the final non-trylock entry - or
1553 * the end of this context's lock-chain - whichever comes first.
1554 */
1555static int
1556check_prevs_add(struct task_struct *curr, struct held_lock *next)
1557{
1558 int depth = curr->lockdep_depth;
1559 struct held_lock *hlock;
1560
1561 /*
1562 * Debugging checks.
1563 *
1564 * Depth must not be zero for a non-head lock:
1565 */
1566 if (!depth)
1567 goto out_bug;
1568 /*
1569 * At least two relevant locks must exist for this
1570 * to be a head:
1571 */
1572 if (curr->held_locks[depth].irq_context !=
1573 curr->held_locks[depth-1].irq_context)
1574 goto out_bug;
1575
1576 for (;;) {
1577 int distance = curr->lockdep_depth - depth + 1;
1578 hlock = curr->held_locks + depth-1;
1579 /*
1580 * Only non-recursive-read entries get new dependencies
1581 * added:
1582 */
1583 if (hlock->read != 2) {
1584 if (!check_prev_add(curr, hlock, next, distance))
1585 return 0;
1586 /*
1587 * Stop after the first non-trylock entry,
1588 * as non-trylock entries have added their
1589 * own direct dependencies already, so this
1590 * lock is connected to them indirectly:
1591 */
1592 if (!hlock->trylock)
1593 break;
1594 }
1595 depth--;
1596 /*
1597 * End of lock-stack?
1598 */
1599 if (!depth)
1600 break;
1601 /*
1602 * Stop the search if we cross into another context:
1603 */
1604 if (curr->held_locks[depth].irq_context !=
1605 curr->held_locks[depth-1].irq_context)
1606 break;
1607 }
1608 return 1;
1609out_bug:
1610 if (!debug_locks_off_graph_unlock())
1611 return 0;
1612
1613 WARN_ON(1);
1614
1615 return 0;
1616}
1617
1618unsigned long nr_lock_chains;
Huang, Ying443cd502008-06-20 16:39:21 +08001619struct lock_chain lock_chains[MAX_LOCKDEP_CHAINS];
Huang, Yingcd1a28e2008-06-23 11:20:54 +08001620int nr_chain_hlocks;
Huang, Ying443cd502008-06-20 16:39:21 +08001621static u16 chain_hlocks[MAX_LOCKDEP_CHAIN_HLOCKS];
1622
1623struct lock_class *lock_chain_get_class(struct lock_chain *chain, int i)
1624{
1625 return lock_classes + chain_hlocks[chain->base + i];
1626}
Peter Zijlstra8e182572007-07-19 01:48:54 -07001627
1628/*
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001629 * Look up a dependency chain. If the key is not present yet then
Jarek Poplawski9e860d02007-05-08 00:30:12 -07001630 * add it and return 1 - in this case the new dependency chain is
1631 * validated. If the key is already hashed, return 0.
1632 * (On return with 1 graph_lock is held.)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001633 */
Huang, Ying443cd502008-06-20 16:39:21 +08001634static inline int lookup_chain_cache(struct task_struct *curr,
1635 struct held_lock *hlock,
1636 u64 chain_key)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001637{
Dave Jonesf82b2172008-08-11 09:30:23 +02001638 struct lock_class *class = hlock_class(hlock);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001639 struct list_head *hash_head = chainhashentry(chain_key);
1640 struct lock_chain *chain;
Huang, Ying443cd502008-06-20 16:39:21 +08001641 struct held_lock *hlock_curr, *hlock_next;
Huang, Yingcd1a28e2008-06-23 11:20:54 +08001642 int i, j, n, cn;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001643
Jarek Poplawski381a2292007-02-10 01:44:58 -08001644 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
1645 return 0;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001646 /*
1647 * We can walk it lock-free, because entries only get added
1648 * to the hash:
1649 */
1650 list_for_each_entry(chain, hash_head, entry) {
1651 if (chain->chain_key == chain_key) {
1652cache_hit:
1653 debug_atomic_inc(&chain_lookup_hits);
Ingo Molnar81fc6852006-12-13 00:34:40 -08001654 if (very_verbose(class))
Andrew Morton755cd902006-12-29 16:49:14 -08001655 printk("\nhash chain already cached, key: "
1656 "%016Lx tail class: [%p] %s\n",
1657 (unsigned long long)chain_key,
1658 class->key, class->name);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001659 return 0;
1660 }
1661 }
Ingo Molnar81fc6852006-12-13 00:34:40 -08001662 if (very_verbose(class))
Andrew Morton755cd902006-12-29 16:49:14 -08001663 printk("\nnew hash chain, key: %016Lx tail class: [%p] %s\n",
1664 (unsigned long long)chain_key, class->key, class->name);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001665 /*
1666 * Allocate a new chain entry from the static array, and add
1667 * it to the hash:
1668 */
Ingo Molnar74c383f2006-12-13 00:34:43 -08001669 if (!graph_lock())
1670 return 0;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001671 /*
1672 * We have to walk the chain again locked - to avoid duplicates:
1673 */
1674 list_for_each_entry(chain, hash_head, entry) {
1675 if (chain->chain_key == chain_key) {
Ingo Molnar74c383f2006-12-13 00:34:43 -08001676 graph_unlock();
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001677 goto cache_hit;
1678 }
1679 }
1680 if (unlikely(nr_lock_chains >= MAX_LOCKDEP_CHAINS)) {
Ingo Molnar74c383f2006-12-13 00:34:43 -08001681 if (!debug_locks_off_graph_unlock())
1682 return 0;
1683
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001684 printk("BUG: MAX_LOCKDEP_CHAINS too low!\n");
1685 printk("turning off the locking correctness validator.\n");
Peter Zijlstraeedeeab2009-03-18 12:38:47 +01001686 dump_stack();
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001687 return 0;
1688 }
1689 chain = lock_chains + nr_lock_chains++;
1690 chain->chain_key = chain_key;
Huang, Ying443cd502008-06-20 16:39:21 +08001691 chain->irq_context = hlock->irq_context;
1692 /* Find the first held_lock of current chain */
1693 hlock_next = hlock;
1694 for (i = curr->lockdep_depth - 1; i >= 0; i--) {
1695 hlock_curr = curr->held_locks + i;
1696 if (hlock_curr->irq_context != hlock_next->irq_context)
1697 break;
1698 hlock_next = hlock;
1699 }
1700 i++;
1701 chain->depth = curr->lockdep_depth + 1 - i;
Huang, Yingcd1a28e2008-06-23 11:20:54 +08001702 cn = nr_chain_hlocks;
1703 while (cn + chain->depth <= MAX_LOCKDEP_CHAIN_HLOCKS) {
1704 n = cmpxchg(&nr_chain_hlocks, cn, cn + chain->depth);
1705 if (n == cn)
1706 break;
1707 cn = n;
1708 }
1709 if (likely(cn + chain->depth <= MAX_LOCKDEP_CHAIN_HLOCKS)) {
1710 chain->base = cn;
Huang, Ying443cd502008-06-20 16:39:21 +08001711 for (j = 0; j < chain->depth - 1; j++, i++) {
Dave Jonesf82b2172008-08-11 09:30:23 +02001712 int lock_id = curr->held_locks[i].class_idx - 1;
Huang, Ying443cd502008-06-20 16:39:21 +08001713 chain_hlocks[chain->base + j] = lock_id;
1714 }
1715 chain_hlocks[chain->base + j] = class - lock_classes;
1716 }
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001717 list_add_tail_rcu(&chain->entry, hash_head);
1718 debug_atomic_inc(&chain_lookup_misses);
Peter Zijlstra8e182572007-07-19 01:48:54 -07001719 inc_chains();
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001720
1721 return 1;
1722}
Peter Zijlstra8e182572007-07-19 01:48:54 -07001723
1724static int validate_chain(struct task_struct *curr, struct lockdep_map *lock,
Johannes Berg4e6045f2007-10-18 23:39:55 -07001725 struct held_lock *hlock, int chain_head, u64 chain_key)
Peter Zijlstra8e182572007-07-19 01:48:54 -07001726{
1727 /*
1728 * Trylock needs to maintain the stack of held locks, but it
1729 * does not add new dependencies, because trylock can be done
1730 * in any order.
1731 *
1732 * We look up the chain_key and do the O(N^2) check and update of
1733 * the dependencies only if this is a new dependency chain.
1734 * (If lookup_chain_cache() returns with 1 it acquires
1735 * graph_lock for us)
1736 */
1737 if (!hlock->trylock && (hlock->check == 2) &&
Huang, Ying443cd502008-06-20 16:39:21 +08001738 lookup_chain_cache(curr, hlock, chain_key)) {
Peter Zijlstra8e182572007-07-19 01:48:54 -07001739 /*
1740 * Check whether last held lock:
1741 *
1742 * - is irq-safe, if this lock is irq-unsafe
1743 * - is softirq-safe, if this lock is hardirq-unsafe
1744 *
1745 * And check whether the new lock's dependency graph
1746 * could lead back to the previous lock.
1747 *
1748 * any of these scenarios could lead to a deadlock. If
1749 * All validations
1750 */
1751 int ret = check_deadlock(curr, hlock, lock, hlock->read);
1752
1753 if (!ret)
1754 return 0;
1755 /*
1756 * Mark recursive read, as we jump over it when
1757 * building dependencies (just like we jump over
1758 * trylock entries):
1759 */
1760 if (ret == 2)
1761 hlock->read = 2;
1762 /*
1763 * Add dependency only if this lock is not the head
1764 * of the chain, and if it's not a secondary read-lock:
1765 */
1766 if (!chain_head && ret != 2)
1767 if (!check_prevs_add(curr, hlock))
1768 return 0;
1769 graph_unlock();
1770 } else
1771 /* after lookup_chain_cache(): */
1772 if (unlikely(!debug_locks))
1773 return 0;
1774
1775 return 1;
1776}
1777#else
1778static inline int validate_chain(struct task_struct *curr,
1779 struct lockdep_map *lock, struct held_lock *hlock,
Gregory Haskins3aa416b2007-10-11 22:11:11 +02001780 int chain_head, u64 chain_key)
Peter Zijlstra8e182572007-07-19 01:48:54 -07001781{
1782 return 1;
1783}
Peter Zijlstraca58abc2007-07-19 01:48:53 -07001784#endif
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001785
1786/*
1787 * We are building curr_chain_key incrementally, so double-check
1788 * it from scratch, to make sure that it's done correctly:
1789 */
Steven Rostedt1d09daa2008-05-12 21:20:55 +02001790static void check_chain_key(struct task_struct *curr)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001791{
1792#ifdef CONFIG_DEBUG_LOCKDEP
1793 struct held_lock *hlock, *prev_hlock = NULL;
1794 unsigned int i, id;
1795 u64 chain_key = 0;
1796
1797 for (i = 0; i < curr->lockdep_depth; i++) {
1798 hlock = curr->held_locks + i;
1799 if (chain_key != hlock->prev_chain_key) {
1800 debug_locks_off();
Arjan van de Ven2df8b1d2008-07-30 12:43:11 -07001801 WARN(1, "hm#1, depth: %u [%u], %016Lx != %016Lx\n",
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001802 curr->lockdep_depth, i,
1803 (unsigned long long)chain_key,
1804 (unsigned long long)hlock->prev_chain_key);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001805 return;
1806 }
Dave Jonesf82b2172008-08-11 09:30:23 +02001807 id = hlock->class_idx - 1;
Jarek Poplawski381a2292007-02-10 01:44:58 -08001808 if (DEBUG_LOCKS_WARN_ON(id >= MAX_LOCKDEP_KEYS))
1809 return;
1810
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001811 if (prev_hlock && (prev_hlock->irq_context !=
1812 hlock->irq_context))
1813 chain_key = 0;
1814 chain_key = iterate_chain_key(chain_key, id);
1815 prev_hlock = hlock;
1816 }
1817 if (chain_key != curr->curr_chain_key) {
1818 debug_locks_off();
Arjan van de Ven2df8b1d2008-07-30 12:43:11 -07001819 WARN(1, "hm#2, depth: %u [%u], %016Lx != %016Lx\n",
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001820 curr->lockdep_depth, i,
1821 (unsigned long long)chain_key,
1822 (unsigned long long)curr->curr_chain_key);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001823 }
1824#endif
1825}
1826
Peter Zijlstra8e182572007-07-19 01:48:54 -07001827static int
1828print_usage_bug(struct task_struct *curr, struct held_lock *this,
1829 enum lock_usage_bit prev_bit, enum lock_usage_bit new_bit)
1830{
1831 if (!debug_locks_off_graph_unlock() || debug_locks_silent)
1832 return 0;
1833
1834 printk("\n=================================\n");
1835 printk( "[ INFO: inconsistent lock state ]\n");
1836 print_kernel_version();
1837 printk( "---------------------------------\n");
1838
1839 printk("inconsistent {%s} -> {%s} usage.\n",
1840 usage_str[prev_bit], usage_str[new_bit]);
1841
1842 printk("%s/%d [HC%u[%lu]:SC%u[%lu]:HE%u:SE%u] takes:\n",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07001843 curr->comm, task_pid_nr(curr),
Peter Zijlstra8e182572007-07-19 01:48:54 -07001844 trace_hardirq_context(curr), hardirq_count() >> HARDIRQ_SHIFT,
1845 trace_softirq_context(curr), softirq_count() >> SOFTIRQ_SHIFT,
1846 trace_hardirqs_enabled(curr),
1847 trace_softirqs_enabled(curr));
1848 print_lock(this);
1849
1850 printk("{%s} state was registered at:\n", usage_str[prev_bit]);
Dave Jonesf82b2172008-08-11 09:30:23 +02001851 print_stack_trace(hlock_class(this)->usage_traces + prev_bit, 1);
Peter Zijlstra8e182572007-07-19 01:48:54 -07001852
1853 print_irqtrace_events(curr);
1854 printk("\nother info that might help us debug this:\n");
1855 lockdep_print_held_locks(curr);
1856
1857 printk("\nstack backtrace:\n");
1858 dump_stack();
1859
1860 return 0;
1861}
1862
1863/*
1864 * Print out an error if an invalid bit is set:
1865 */
1866static inline int
1867valid_state(struct task_struct *curr, struct held_lock *this,
1868 enum lock_usage_bit new_bit, enum lock_usage_bit bad_bit)
1869{
Dave Jonesf82b2172008-08-11 09:30:23 +02001870 if (unlikely(hlock_class(this)->usage_mask & (1 << bad_bit)))
Peter Zijlstra8e182572007-07-19 01:48:54 -07001871 return print_usage_bug(curr, this, bad_bit, new_bit);
1872 return 1;
1873}
1874
1875static int mark_lock(struct task_struct *curr, struct held_lock *this,
1876 enum lock_usage_bit new_bit);
1877
Steven Rostedt81d68a92008-05-12 21:20:42 +02001878#if defined(CONFIG_TRACE_IRQFLAGS) && defined(CONFIG_PROVE_LOCKING)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001879
1880/*
1881 * print irq inversion bug:
1882 */
1883static int
1884print_irq_inversion_bug(struct task_struct *curr, struct lock_class *other,
1885 struct held_lock *this, int forwards,
1886 const char *irqclass)
1887{
Ingo Molnar74c383f2006-12-13 00:34:43 -08001888 if (!debug_locks_off_graph_unlock() || debug_locks_silent)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001889 return 0;
1890
1891 printk("\n=========================================================\n");
1892 printk( "[ INFO: possible irq lock inversion dependency detected ]\n");
Dave Jones99de0552006-09-29 02:00:10 -07001893 print_kernel_version();
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001894 printk( "---------------------------------------------------------\n");
1895 printk("%s/%d just changed the state of lock:\n",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07001896 curr->comm, task_pid_nr(curr));
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001897 print_lock(this);
1898 if (forwards)
Peter Zijlstra26575e22009-03-04 14:53:24 +01001899 printk("but this lock took another, %s-unsafe lock in the past:\n", irqclass);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001900 else
Peter Zijlstra26575e22009-03-04 14:53:24 +01001901 printk("but this lock was taken by another, %s-safe lock in the past:\n", irqclass);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001902 print_lock_name(other);
1903 printk("\n\nand interrupts could create inverse lock ordering between them.\n\n");
1904
1905 printk("\nother info that might help us debug this:\n");
1906 lockdep_print_held_locks(curr);
1907
1908 printk("\nthe first lock's dependencies:\n");
Dave Jonesf82b2172008-08-11 09:30:23 +02001909 print_lock_dependencies(hlock_class(this), 0);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001910
1911 printk("\nthe second lock's dependencies:\n");
1912 print_lock_dependencies(other, 0);
1913
1914 printk("\nstack backtrace:\n");
1915 dump_stack();
1916
1917 return 0;
1918}
1919
1920/*
1921 * Prove that in the forwards-direction subgraph starting at <this>
1922 * there is no lock matching <mask>:
1923 */
1924static int
1925check_usage_forwards(struct task_struct *curr, struct held_lock *this,
1926 enum lock_usage_bit bit, const char *irqclass)
1927{
1928 int ret;
1929
1930 find_usage_bit = bit;
1931 /* fills in <forwards_match> */
Dave Jonesf82b2172008-08-11 09:30:23 +02001932 ret = find_usage_forwards(hlock_class(this), 0);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001933 if (!ret || ret == 1)
1934 return ret;
1935
1936 return print_irq_inversion_bug(curr, forwards_match, this, 1, irqclass);
1937}
1938
1939/*
1940 * Prove that in the backwards-direction subgraph starting at <this>
1941 * there is no lock matching <mask>:
1942 */
1943static int
1944check_usage_backwards(struct task_struct *curr, struct held_lock *this,
1945 enum lock_usage_bit bit, const char *irqclass)
1946{
1947 int ret;
1948
1949 find_usage_bit = bit;
1950 /* fills in <backwards_match> */
Dave Jonesf82b2172008-08-11 09:30:23 +02001951 ret = find_usage_backwards(hlock_class(this), 0);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001952 if (!ret || ret == 1)
1953 return ret;
1954
1955 return print_irq_inversion_bug(curr, backwards_match, this, 0, irqclass);
1956}
1957
Ingo Molnar3117df02006-12-13 00:34:43 -08001958void print_irqtrace_events(struct task_struct *curr)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001959{
1960 printk("irq event stamp: %u\n", curr->irq_events);
1961 printk("hardirqs last enabled at (%u): ", curr->hardirq_enable_event);
1962 print_ip_sym(curr->hardirq_enable_ip);
1963 printk("hardirqs last disabled at (%u): ", curr->hardirq_disable_event);
1964 print_ip_sym(curr->hardirq_disable_ip);
1965 printk("softirqs last enabled at (%u): ", curr->softirq_enable_event);
1966 print_ip_sym(curr->softirq_enable_ip);
1967 printk("softirqs last disabled at (%u): ", curr->softirq_disable_event);
1968 print_ip_sym(curr->softirq_disable_ip);
1969}
1970
Peter Zijlstracd953022009-01-22 16:38:21 +01001971static int HARDIRQ_verbose(struct lock_class *class)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001972{
Peter Zijlstra8e182572007-07-19 01:48:54 -07001973#if HARDIRQ_VERBOSE
1974 return class_filter(class);
1975#endif
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001976 return 0;
1977}
1978
Peter Zijlstracd953022009-01-22 16:38:21 +01001979static int SOFTIRQ_verbose(struct lock_class *class)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001980{
Peter Zijlstra8e182572007-07-19 01:48:54 -07001981#if SOFTIRQ_VERBOSE
1982 return class_filter(class);
1983#endif
1984 return 0;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001985}
1986
Peter Zijlstracd953022009-01-22 16:38:21 +01001987static int RECLAIM_FS_verbose(struct lock_class *class)
Nick Piggincf40bd12009-01-21 08:12:39 +01001988{
1989#if RECLAIM_VERBOSE
1990 return class_filter(class);
1991#endif
1992 return 0;
1993}
1994
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001995#define STRICT_READ_CHECKS 1
1996
Peter Zijlstracd953022009-01-22 16:38:21 +01001997static int (*state_verbose_f[])(struct lock_class *class) = {
1998#define LOCKDEP_STATE(__STATE) \
1999 __STATE##_verbose,
2000#include "lockdep_states.h"
2001#undef LOCKDEP_STATE
2002};
2003
2004static inline int state_verbose(enum lock_usage_bit bit,
2005 struct lock_class *class)
2006{
2007 return state_verbose_f[bit >> 2](class);
2008}
2009
Peter Zijlstra42c50d52009-01-22 16:58:16 +01002010typedef int (*check_usage_f)(struct task_struct *, struct held_lock *,
2011 enum lock_usage_bit bit, const char *name);
2012
Peter Zijlstra6a6904d2009-01-22 16:07:44 +01002013static int
Peter Zijlstra1c21f142009-03-04 13:51:13 +01002014mark_lock_irq(struct task_struct *curr, struct held_lock *this,
2015 enum lock_usage_bit new_bit)
Peter Zijlstra6a6904d2009-01-22 16:07:44 +01002016{
Peter Zijlstraf9892092009-01-22 16:09:59 +01002017 int excl_bit = exclusive_bit(new_bit);
Peter Zijlstra9d3651a2009-01-22 17:18:32 +01002018 int read = new_bit & 1;
Peter Zijlstra42c50d52009-01-22 16:58:16 +01002019 int dir = new_bit & 2;
2020
Peter Zijlstra38aa2712009-01-27 14:53:50 +01002021 /*
2022 * mark USED_IN has to look forwards -- to ensure no dependency
2023 * has ENABLED state, which would allow recursion deadlocks.
2024 *
2025 * mark ENABLED has to look backwards -- to ensure no dependee
2026 * has USED_IN state, which, again, would allow recursion deadlocks.
2027 */
Peter Zijlstra42c50d52009-01-22 16:58:16 +01002028 check_usage_f usage = dir ?
2029 check_usage_backwards : check_usage_forwards;
Peter Zijlstraf9892092009-01-22 16:09:59 +01002030
Peter Zijlstra38aa2712009-01-27 14:53:50 +01002031 /*
2032 * Validate that this particular lock does not have conflicting
2033 * usage states.
2034 */
Peter Zijlstra6a6904d2009-01-22 16:07:44 +01002035 if (!valid_state(curr, this, new_bit, excl_bit))
2036 return 0;
Peter Zijlstra9d3651a2009-01-22 17:18:32 +01002037
Peter Zijlstra38aa2712009-01-27 14:53:50 +01002038 /*
2039 * Validate that the lock dependencies don't have conflicting usage
2040 * states.
2041 */
2042 if ((!read || !dir || STRICT_READ_CHECKS) &&
Peter Zijlstra1c21f142009-03-04 13:51:13 +01002043 !usage(curr, this, excl_bit, state_name(new_bit & ~1)))
Peter Zijlstra6a6904d2009-01-22 16:07:44 +01002044 return 0;
Peter Zijlstra780e8202009-01-22 16:51:29 +01002045
Peter Zijlstra38aa2712009-01-27 14:53:50 +01002046 /*
2047 * Check for read in write conflicts
2048 */
2049 if (!read) {
2050 if (!valid_state(curr, this, new_bit, excl_bit + 1))
2051 return 0;
2052
2053 if (STRICT_READ_CHECKS &&
Peter Zijlstra4f367d8a2009-01-22 18:10:42 +01002054 !usage(curr, this, excl_bit + 1,
2055 state_name(new_bit + 1)))
Peter Zijlstra38aa2712009-01-27 14:53:50 +01002056 return 0;
2057 }
Peter Zijlstra780e8202009-01-22 16:51:29 +01002058
Peter Zijlstracd953022009-01-22 16:38:21 +01002059 if (state_verbose(new_bit, hlock_class(this)))
Peter Zijlstra6a6904d2009-01-22 16:07:44 +01002060 return 2;
2061
2062 return 1;
2063}
2064
Nick Piggincf40bd12009-01-21 08:12:39 +01002065enum mark_type {
Peter Zijlstra36bfb9b2009-01-22 14:12:41 +01002066#define LOCKDEP_STATE(__STATE) __STATE,
2067#include "lockdep_states.h"
2068#undef LOCKDEP_STATE
Nick Piggincf40bd12009-01-21 08:12:39 +01002069};
2070
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002071/*
2072 * Mark all held locks with a usage bit:
2073 */
Steven Rostedt1d09daa2008-05-12 21:20:55 +02002074static int
Nick Piggincf40bd12009-01-21 08:12:39 +01002075mark_held_locks(struct task_struct *curr, enum mark_type mark)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002076{
2077 enum lock_usage_bit usage_bit;
2078 struct held_lock *hlock;
2079 int i;
2080
2081 for (i = 0; i < curr->lockdep_depth; i++) {
2082 hlock = curr->held_locks + i;
2083
Peter Zijlstracf2ad4d2009-01-27 13:58:08 +01002084 usage_bit = 2 + (mark << 2); /* ENABLED */
2085 if (hlock->read)
2086 usage_bit += 1; /* READ */
2087
2088 BUG_ON(usage_bit >= LOCK_USAGE_STATES);
Nick Piggincf40bd12009-01-21 08:12:39 +01002089
Jarek Poplawski4ff773bb2007-05-08 00:31:00 -07002090 if (!mark_lock(curr, hlock, usage_bit))
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002091 return 0;
2092 }
2093
2094 return 1;
2095}
2096
2097/*
2098 * Debugging helper: via this flag we know that we are in
2099 * 'early bootup code', and will warn about any invalid irqs-on event:
2100 */
2101static int early_boot_irqs_enabled;
2102
2103void early_boot_irqs_off(void)
2104{
2105 early_boot_irqs_enabled = 0;
2106}
2107
2108void early_boot_irqs_on(void)
2109{
2110 early_boot_irqs_enabled = 1;
2111}
2112
2113/*
2114 * Hardirqs will be enabled:
2115 */
Heiko Carstens6afe40b2008-10-28 11:14:58 +01002116void trace_hardirqs_on_caller(unsigned long ip)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002117{
2118 struct task_struct *curr = current;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002119
Heiko Carstens6afe40b2008-10-28 11:14:58 +01002120 time_hardirqs_on(CALLER_ADDR0, ip);
Steven Rostedt81d68a92008-05-12 21:20:42 +02002121
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002122 if (unlikely(!debug_locks || current->lockdep_recursion))
2123 return;
2124
2125 if (DEBUG_LOCKS_WARN_ON(unlikely(!early_boot_irqs_enabled)))
2126 return;
2127
2128 if (unlikely(curr->hardirqs_enabled)) {
2129 debug_atomic_inc(&redundant_hardirqs_on);
2130 return;
2131 }
2132 /* we'll do an OFF -> ON transition: */
2133 curr->hardirqs_enabled = 1;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002134
2135 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2136 return;
2137 if (DEBUG_LOCKS_WARN_ON(current->hardirq_context))
2138 return;
2139 /*
2140 * We are going to turn hardirqs on, so set the
2141 * usage bit for all held locks:
2142 */
Nick Piggincf40bd12009-01-21 08:12:39 +01002143 if (!mark_held_locks(curr, HARDIRQ))
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002144 return;
2145 /*
2146 * If we have softirqs enabled, then set the usage
2147 * bit for all held locks. (disabled hardirqs prevented
2148 * this bit from being set before)
2149 */
2150 if (curr->softirqs_enabled)
Nick Piggincf40bd12009-01-21 08:12:39 +01002151 if (!mark_held_locks(curr, SOFTIRQ))
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002152 return;
2153
2154 curr->hardirq_enable_ip = ip;
2155 curr->hardirq_enable_event = ++curr->irq_events;
2156 debug_atomic_inc(&hardirqs_on_events);
2157}
Steven Rostedt81d68a92008-05-12 21:20:42 +02002158EXPORT_SYMBOL(trace_hardirqs_on_caller);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002159
Steven Rostedt1d09daa2008-05-12 21:20:55 +02002160void trace_hardirqs_on(void)
Steven Rostedt81d68a92008-05-12 21:20:42 +02002161{
2162 trace_hardirqs_on_caller(CALLER_ADDR0);
2163}
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002164EXPORT_SYMBOL(trace_hardirqs_on);
2165
2166/*
2167 * Hardirqs were disabled:
2168 */
Heiko Carstens6afe40b2008-10-28 11:14:58 +01002169void trace_hardirqs_off_caller(unsigned long ip)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002170{
2171 struct task_struct *curr = current;
2172
Heiko Carstens6afe40b2008-10-28 11:14:58 +01002173 time_hardirqs_off(CALLER_ADDR0, ip);
Steven Rostedt81d68a92008-05-12 21:20:42 +02002174
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002175 if (unlikely(!debug_locks || current->lockdep_recursion))
2176 return;
2177
2178 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2179 return;
2180
2181 if (curr->hardirqs_enabled) {
2182 /*
2183 * We have done an ON -> OFF transition:
2184 */
2185 curr->hardirqs_enabled = 0;
Heiko Carstens6afe40b2008-10-28 11:14:58 +01002186 curr->hardirq_disable_ip = ip;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002187 curr->hardirq_disable_event = ++curr->irq_events;
2188 debug_atomic_inc(&hardirqs_off_events);
2189 } else
2190 debug_atomic_inc(&redundant_hardirqs_off);
2191}
Steven Rostedt81d68a92008-05-12 21:20:42 +02002192EXPORT_SYMBOL(trace_hardirqs_off_caller);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002193
Steven Rostedt1d09daa2008-05-12 21:20:55 +02002194void trace_hardirqs_off(void)
Steven Rostedt81d68a92008-05-12 21:20:42 +02002195{
2196 trace_hardirqs_off_caller(CALLER_ADDR0);
2197}
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002198EXPORT_SYMBOL(trace_hardirqs_off);
2199
2200/*
2201 * Softirqs will be enabled:
2202 */
2203void trace_softirqs_on(unsigned long ip)
2204{
2205 struct task_struct *curr = current;
2206
2207 if (unlikely(!debug_locks))
2208 return;
2209
2210 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2211 return;
2212
2213 if (curr->softirqs_enabled) {
2214 debug_atomic_inc(&redundant_softirqs_on);
2215 return;
2216 }
2217
2218 /*
2219 * We'll do an OFF -> ON transition:
2220 */
2221 curr->softirqs_enabled = 1;
2222 curr->softirq_enable_ip = ip;
2223 curr->softirq_enable_event = ++curr->irq_events;
2224 debug_atomic_inc(&softirqs_on_events);
2225 /*
2226 * We are going to turn softirqs on, so set the
2227 * usage bit for all held locks, if hardirqs are
2228 * enabled too:
2229 */
2230 if (curr->hardirqs_enabled)
Nick Piggincf40bd12009-01-21 08:12:39 +01002231 mark_held_locks(curr, SOFTIRQ);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002232}
2233
2234/*
2235 * Softirqs were disabled:
2236 */
2237void trace_softirqs_off(unsigned long ip)
2238{
2239 struct task_struct *curr = current;
2240
2241 if (unlikely(!debug_locks))
2242 return;
2243
2244 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2245 return;
2246
2247 if (curr->softirqs_enabled) {
2248 /*
2249 * We have done an ON -> OFF transition:
2250 */
2251 curr->softirqs_enabled = 0;
2252 curr->softirq_disable_ip = ip;
2253 curr->softirq_disable_event = ++curr->irq_events;
2254 debug_atomic_inc(&softirqs_off_events);
2255 DEBUG_LOCKS_WARN_ON(!softirq_count());
2256 } else
2257 debug_atomic_inc(&redundant_softirqs_off);
2258}
2259
Peter Zijlstra2f850182009-03-20 11:13:20 +01002260static void __lockdep_trace_alloc(gfp_t gfp_mask, unsigned long flags)
Nick Piggincf40bd12009-01-21 08:12:39 +01002261{
2262 struct task_struct *curr = current;
2263
2264 if (unlikely(!debug_locks))
2265 return;
2266
2267 /* no reclaim without waiting on it */
2268 if (!(gfp_mask & __GFP_WAIT))
2269 return;
2270
2271 /* this guy won't enter reclaim */
2272 if ((curr->flags & PF_MEMALLOC) && !(gfp_mask & __GFP_NOMEMALLOC))
2273 return;
2274
2275 /* We're only interested __GFP_FS allocations for now */
2276 if (!(gfp_mask & __GFP_FS))
2277 return;
2278
Peter Zijlstra2f850182009-03-20 11:13:20 +01002279 if (DEBUG_LOCKS_WARN_ON(irqs_disabled_flags(flags)))
Nick Piggincf40bd12009-01-21 08:12:39 +01002280 return;
2281
2282 mark_held_locks(curr, RECLAIM_FS);
2283}
2284
Peter Zijlstra2f850182009-03-20 11:13:20 +01002285static void check_flags(unsigned long flags);
2286
2287void lockdep_trace_alloc(gfp_t gfp_mask)
2288{
2289 unsigned long flags;
2290
2291 if (unlikely(current->lockdep_recursion))
2292 return;
2293
2294 raw_local_irq_save(flags);
2295 check_flags(flags);
2296 current->lockdep_recursion = 1;
2297 __lockdep_trace_alloc(gfp_mask, flags);
2298 current->lockdep_recursion = 0;
2299 raw_local_irq_restore(flags);
2300}
2301
Peter Zijlstra8e182572007-07-19 01:48:54 -07002302static int mark_irqflags(struct task_struct *curr, struct held_lock *hlock)
2303{
2304 /*
2305 * If non-trylock use in a hardirq or softirq context, then
2306 * mark the lock as used in these contexts:
2307 */
2308 if (!hlock->trylock) {
2309 if (hlock->read) {
2310 if (curr->hardirq_context)
2311 if (!mark_lock(curr, hlock,
2312 LOCK_USED_IN_HARDIRQ_READ))
2313 return 0;
2314 if (curr->softirq_context)
2315 if (!mark_lock(curr, hlock,
2316 LOCK_USED_IN_SOFTIRQ_READ))
2317 return 0;
2318 } else {
2319 if (curr->hardirq_context)
2320 if (!mark_lock(curr, hlock, LOCK_USED_IN_HARDIRQ))
2321 return 0;
2322 if (curr->softirq_context)
2323 if (!mark_lock(curr, hlock, LOCK_USED_IN_SOFTIRQ))
2324 return 0;
2325 }
2326 }
2327 if (!hlock->hardirqs_off) {
2328 if (hlock->read) {
2329 if (!mark_lock(curr, hlock,
Peter Zijlstra4fc95e82009-01-22 13:10:52 +01002330 LOCK_ENABLED_HARDIRQ_READ))
Peter Zijlstra8e182572007-07-19 01:48:54 -07002331 return 0;
2332 if (curr->softirqs_enabled)
2333 if (!mark_lock(curr, hlock,
Peter Zijlstra4fc95e82009-01-22 13:10:52 +01002334 LOCK_ENABLED_SOFTIRQ_READ))
Peter Zijlstra8e182572007-07-19 01:48:54 -07002335 return 0;
2336 } else {
2337 if (!mark_lock(curr, hlock,
Peter Zijlstra4fc95e82009-01-22 13:10:52 +01002338 LOCK_ENABLED_HARDIRQ))
Peter Zijlstra8e182572007-07-19 01:48:54 -07002339 return 0;
2340 if (curr->softirqs_enabled)
2341 if (!mark_lock(curr, hlock,
Peter Zijlstra4fc95e82009-01-22 13:10:52 +01002342 LOCK_ENABLED_SOFTIRQ))
Peter Zijlstra8e182572007-07-19 01:48:54 -07002343 return 0;
2344 }
2345 }
2346
Nick Piggincf40bd12009-01-21 08:12:39 +01002347 /*
2348 * We reuse the irq context infrastructure more broadly as a general
2349 * context checking code. This tests GFP_FS recursion (a lock taken
2350 * during reclaim for a GFP_FS allocation is held over a GFP_FS
2351 * allocation).
2352 */
2353 if (!hlock->trylock && (curr->lockdep_reclaim_gfp & __GFP_FS)) {
2354 if (hlock->read) {
2355 if (!mark_lock(curr, hlock, LOCK_USED_IN_RECLAIM_FS_READ))
2356 return 0;
2357 } else {
2358 if (!mark_lock(curr, hlock, LOCK_USED_IN_RECLAIM_FS))
2359 return 0;
2360 }
2361 }
2362
Peter Zijlstra8e182572007-07-19 01:48:54 -07002363 return 1;
2364}
2365
2366static int separate_irq_context(struct task_struct *curr,
2367 struct held_lock *hlock)
2368{
2369 unsigned int depth = curr->lockdep_depth;
2370
2371 /*
2372 * Keep track of points where we cross into an interrupt context:
2373 */
2374 hlock->irq_context = 2*(curr->hardirq_context ? 1 : 0) +
2375 curr->softirq_context;
2376 if (depth) {
2377 struct held_lock *prev_hlock;
2378
2379 prev_hlock = curr->held_locks + depth-1;
2380 /*
2381 * If we cross into another context, reset the
2382 * hash key (this also prevents the checking and the
2383 * adding of the dependency to 'prev'):
2384 */
2385 if (prev_hlock->irq_context != hlock->irq_context)
2386 return 1;
2387 }
2388 return 0;
2389}
2390
2391#else
2392
2393static inline
2394int mark_lock_irq(struct task_struct *curr, struct held_lock *this,
2395 enum lock_usage_bit new_bit)
2396{
2397 WARN_ON(1);
2398 return 1;
2399}
2400
2401static inline int mark_irqflags(struct task_struct *curr,
2402 struct held_lock *hlock)
2403{
2404 return 1;
2405}
2406
2407static inline int separate_irq_context(struct task_struct *curr,
2408 struct held_lock *hlock)
2409{
2410 return 0;
2411}
2412
Peter Zijlstra868a23a2009-02-15 00:25:21 +01002413void lockdep_trace_alloc(gfp_t gfp_mask)
2414{
2415}
2416
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002417#endif
2418
2419/*
Peter Zijlstra8e182572007-07-19 01:48:54 -07002420 * Mark a lock with a usage bit, and validate the state transition:
2421 */
Steven Rostedt1d09daa2008-05-12 21:20:55 +02002422static int mark_lock(struct task_struct *curr, struct held_lock *this,
Steven Rostedt0764d232008-05-12 21:20:44 +02002423 enum lock_usage_bit new_bit)
Peter Zijlstra8e182572007-07-19 01:48:54 -07002424{
2425 unsigned int new_mask = 1 << new_bit, ret = 1;
2426
2427 /*
2428 * If already set then do not dirty the cacheline,
2429 * nor do any checks:
2430 */
Dave Jonesf82b2172008-08-11 09:30:23 +02002431 if (likely(hlock_class(this)->usage_mask & new_mask))
Peter Zijlstra8e182572007-07-19 01:48:54 -07002432 return 1;
2433
2434 if (!graph_lock())
2435 return 0;
2436 /*
2437 * Make sure we didnt race:
2438 */
Dave Jonesf82b2172008-08-11 09:30:23 +02002439 if (unlikely(hlock_class(this)->usage_mask & new_mask)) {
Peter Zijlstra8e182572007-07-19 01:48:54 -07002440 graph_unlock();
2441 return 1;
2442 }
2443
Dave Jonesf82b2172008-08-11 09:30:23 +02002444 hlock_class(this)->usage_mask |= new_mask;
Peter Zijlstra8e182572007-07-19 01:48:54 -07002445
Dave Jonesf82b2172008-08-11 09:30:23 +02002446 if (!save_trace(hlock_class(this)->usage_traces + new_bit))
Peter Zijlstra8e182572007-07-19 01:48:54 -07002447 return 0;
2448
2449 switch (new_bit) {
Peter Zijlstra53464172009-01-22 14:15:53 +01002450#define LOCKDEP_STATE(__STATE) \
2451 case LOCK_USED_IN_##__STATE: \
2452 case LOCK_USED_IN_##__STATE##_READ: \
2453 case LOCK_ENABLED_##__STATE: \
2454 case LOCK_ENABLED_##__STATE##_READ:
2455#include "lockdep_states.h"
2456#undef LOCKDEP_STATE
Peter Zijlstra8e182572007-07-19 01:48:54 -07002457 ret = mark_lock_irq(curr, this, new_bit);
2458 if (!ret)
2459 return 0;
2460 break;
2461 case LOCK_USED:
Peter Zijlstra8e182572007-07-19 01:48:54 -07002462 debug_atomic_dec(&nr_unused_locks);
2463 break;
2464 default:
2465 if (!debug_locks_off_graph_unlock())
2466 return 0;
2467 WARN_ON(1);
2468 return 0;
2469 }
2470
2471 graph_unlock();
2472
2473 /*
2474 * We must printk outside of the graph_lock:
2475 */
2476 if (ret == 2) {
2477 printk("\nmarked lock as {%s}:\n", usage_str[new_bit]);
2478 print_lock(this);
2479 print_irqtrace_events(curr);
2480 dump_stack();
2481 }
2482
2483 return ret;
2484}
2485
2486/*
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002487 * Initialize a lock instance's lock-class mapping info:
2488 */
2489void lockdep_init_map(struct lockdep_map *lock, const char *name,
Peter Zijlstra4dfbb9d2006-10-11 01:45:14 -04002490 struct lock_class_key *key, int subclass)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002491{
2492 if (unlikely(!debug_locks))
2493 return;
2494
2495 if (DEBUG_LOCKS_WARN_ON(!key))
2496 return;
2497 if (DEBUG_LOCKS_WARN_ON(!name))
2498 return;
2499 /*
2500 * Sanity check, the lock-class key must be persistent:
2501 */
2502 if (!static_obj(key)) {
2503 printk("BUG: key %p not in .data!\n", key);
2504 DEBUG_LOCKS_WARN_ON(1);
2505 return;
2506 }
2507 lock->name = name;
2508 lock->key = key;
Ingo Molnard6d897c2006-07-10 04:44:04 -07002509 lock->class_cache = NULL;
Peter Zijlstra96645672007-07-19 01:49:00 -07002510#ifdef CONFIG_LOCK_STAT
2511 lock->cpu = raw_smp_processor_id();
2512#endif
Peter Zijlstra4dfbb9d2006-10-11 01:45:14 -04002513 if (subclass)
2514 register_lock_class(lock, subclass, 1);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002515}
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002516EXPORT_SYMBOL_GPL(lockdep_init_map);
2517
2518/*
2519 * This gets called for every mutex_lock*()/spin_lock*() operation.
2520 * We maintain the dependency maps and validate the locking attempt:
2521 */
2522static int __lock_acquire(struct lockdep_map *lock, unsigned int subclass,
2523 int trylock, int read, int check, int hardirqs_off,
Peter Zijlstra7531e2f2008-08-11 09:30:24 +02002524 struct lockdep_map *nest_lock, unsigned long ip)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002525{
2526 struct task_struct *curr = current;
Ingo Molnard6d897c2006-07-10 04:44:04 -07002527 struct lock_class *class = NULL;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002528 struct held_lock *hlock;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002529 unsigned int depth, id;
2530 int chain_head = 0;
2531 u64 chain_key;
2532
Peter Zijlstraf20786f2007-07-19 01:48:56 -07002533 if (!prove_locking)
2534 check = 1;
2535
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002536 if (unlikely(!debug_locks))
2537 return 0;
2538
2539 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2540 return 0;
2541
2542 if (unlikely(subclass >= MAX_LOCKDEP_SUBCLASSES)) {
2543 debug_locks_off();
2544 printk("BUG: MAX_LOCKDEP_SUBCLASSES too low!\n");
2545 printk("turning off the locking correctness validator.\n");
Peter Zijlstraeedeeab2009-03-18 12:38:47 +01002546 dump_stack();
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002547 return 0;
2548 }
2549
Ingo Molnard6d897c2006-07-10 04:44:04 -07002550 if (!subclass)
2551 class = lock->class_cache;
2552 /*
2553 * Not cached yet or subclass?
2554 */
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002555 if (unlikely(!class)) {
Peter Zijlstra4dfbb9d2006-10-11 01:45:14 -04002556 class = register_lock_class(lock, subclass, 0);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002557 if (!class)
2558 return 0;
2559 }
2560 debug_atomic_inc((atomic_t *)&class->ops);
2561 if (very_verbose(class)) {
2562 printk("\nacquire class [%p] %s", class->key, class->name);
2563 if (class->name_version > 1)
2564 printk("#%d", class->name_version);
2565 printk("\n");
2566 dump_stack();
2567 }
2568
2569 /*
2570 * Add the lock to the list of currently held locks.
2571 * (we dont increase the depth just yet, up until the
2572 * dependency checks are done)
2573 */
2574 depth = curr->lockdep_depth;
2575 if (DEBUG_LOCKS_WARN_ON(depth >= MAX_LOCK_DEPTH))
2576 return 0;
2577
2578 hlock = curr->held_locks + depth;
Dave Jonesf82b2172008-08-11 09:30:23 +02002579 if (DEBUG_LOCKS_WARN_ON(!class))
2580 return 0;
2581 hlock->class_idx = class - lock_classes + 1;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002582 hlock->acquire_ip = ip;
2583 hlock->instance = lock;
Peter Zijlstra7531e2f2008-08-11 09:30:24 +02002584 hlock->nest_lock = nest_lock;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002585 hlock->trylock = trylock;
2586 hlock->read = read;
2587 hlock->check = check;
Dmitry Baryshkov6951b122008-08-18 04:26:37 +04002588 hlock->hardirqs_off = !!hardirqs_off;
Peter Zijlstraf20786f2007-07-19 01:48:56 -07002589#ifdef CONFIG_LOCK_STAT
2590 hlock->waittime_stamp = 0;
2591 hlock->holdtime_stamp = sched_clock();
2592#endif
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002593
Peter Zijlstra8e182572007-07-19 01:48:54 -07002594 if (check == 2 && !mark_irqflags(curr, hlock))
2595 return 0;
2596
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002597 /* mark it as used: */
Jarek Poplawski4ff773bb2007-05-08 00:31:00 -07002598 if (!mark_lock(curr, hlock, LOCK_USED))
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002599 return 0;
Peter Zijlstra8e182572007-07-19 01:48:54 -07002600
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002601 /*
Gautham R Shenoy17aacfb2007-10-28 20:47:01 +01002602 * Calculate the chain hash: it's the combined hash of all the
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002603 * lock keys along the dependency chain. We save the hash value
2604 * at every step so that we can get the current hash easily
2605 * after unlock. The chain hash is then used to cache dependency
2606 * results.
2607 *
2608 * The 'key ID' is what is the most compact key value to drive
2609 * the hash, not class->key.
2610 */
2611 id = class - lock_classes;
2612 if (DEBUG_LOCKS_WARN_ON(id >= MAX_LOCKDEP_KEYS))
2613 return 0;
2614
2615 chain_key = curr->curr_chain_key;
2616 if (!depth) {
2617 if (DEBUG_LOCKS_WARN_ON(chain_key != 0))
2618 return 0;
2619 chain_head = 1;
2620 }
2621
2622 hlock->prev_chain_key = chain_key;
Peter Zijlstra8e182572007-07-19 01:48:54 -07002623 if (separate_irq_context(curr, hlock)) {
2624 chain_key = 0;
2625 chain_head = 1;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002626 }
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002627 chain_key = iterate_chain_key(chain_key, id);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002628
Gregory Haskins3aa416b2007-10-11 22:11:11 +02002629 if (!validate_chain(curr, lock, hlock, chain_head, chain_key))
Peter Zijlstra8e182572007-07-19 01:48:54 -07002630 return 0;
Jarek Poplawski381a2292007-02-10 01:44:58 -08002631
Gregory Haskins3aa416b2007-10-11 22:11:11 +02002632 curr->curr_chain_key = chain_key;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002633 curr->lockdep_depth++;
2634 check_chain_key(curr);
Jarek Poplawski60e114d2007-02-20 13:58:00 -08002635#ifdef CONFIG_DEBUG_LOCKDEP
2636 if (unlikely(!debug_locks))
2637 return 0;
2638#endif
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002639 if (unlikely(curr->lockdep_depth >= MAX_LOCK_DEPTH)) {
2640 debug_locks_off();
2641 printk("BUG: MAX_LOCK_DEPTH too low!\n");
2642 printk("turning off the locking correctness validator.\n");
Peter Zijlstraeedeeab2009-03-18 12:38:47 +01002643 dump_stack();
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002644 return 0;
2645 }
Jarek Poplawski381a2292007-02-10 01:44:58 -08002646
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002647 if (unlikely(curr->lockdep_depth > max_lockdep_depth))
2648 max_lockdep_depth = curr->lockdep_depth;
2649
2650 return 1;
2651}
2652
2653static int
2654print_unlock_inbalance_bug(struct task_struct *curr, struct lockdep_map *lock,
2655 unsigned long ip)
2656{
2657 if (!debug_locks_off())
2658 return 0;
2659 if (debug_locks_silent)
2660 return 0;
2661
2662 printk("\n=====================================\n");
2663 printk( "[ BUG: bad unlock balance detected! ]\n");
2664 printk( "-------------------------------------\n");
2665 printk("%s/%d is trying to release lock (",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07002666 curr->comm, task_pid_nr(curr));
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002667 print_lockdep_cache(lock);
2668 printk(") at:\n");
2669 print_ip_sym(ip);
2670 printk("but there are no more locks to release!\n");
2671 printk("\nother info that might help us debug this:\n");
2672 lockdep_print_held_locks(curr);
2673
2674 printk("\nstack backtrace:\n");
2675 dump_stack();
2676
2677 return 0;
2678}
2679
2680/*
2681 * Common debugging checks for both nested and non-nested unlock:
2682 */
2683static int check_unlock(struct task_struct *curr, struct lockdep_map *lock,
2684 unsigned long ip)
2685{
2686 if (unlikely(!debug_locks))
2687 return 0;
2688 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2689 return 0;
2690
2691 if (curr->lockdep_depth <= 0)
2692 return print_unlock_inbalance_bug(curr, lock, ip);
2693
2694 return 1;
2695}
2696
Peter Zijlstra64aa3482008-08-11 09:30:21 +02002697static int
Peter Zijlstra00ef9f72008-12-04 09:00:17 +01002698__lock_set_class(struct lockdep_map *lock, const char *name,
2699 struct lock_class_key *key, unsigned int subclass,
2700 unsigned long ip)
Peter Zijlstra64aa3482008-08-11 09:30:21 +02002701{
2702 struct task_struct *curr = current;
2703 struct held_lock *hlock, *prev_hlock;
2704 struct lock_class *class;
2705 unsigned int depth;
2706 int i;
2707
2708 depth = curr->lockdep_depth;
2709 if (DEBUG_LOCKS_WARN_ON(!depth))
2710 return 0;
2711
2712 prev_hlock = NULL;
2713 for (i = depth-1; i >= 0; i--) {
2714 hlock = curr->held_locks + i;
2715 /*
2716 * We must not cross into another context:
2717 */
2718 if (prev_hlock && prev_hlock->irq_context != hlock->irq_context)
2719 break;
2720 if (hlock->instance == lock)
2721 goto found_it;
2722 prev_hlock = hlock;
2723 }
2724 return print_unlock_inbalance_bug(curr, lock, ip);
2725
2726found_it:
Peter Zijlstra00ef9f72008-12-04 09:00:17 +01002727 lockdep_init_map(lock, name, key, 0);
Peter Zijlstra64aa3482008-08-11 09:30:21 +02002728 class = register_lock_class(lock, subclass, 0);
Dave Jonesf82b2172008-08-11 09:30:23 +02002729 hlock->class_idx = class - lock_classes + 1;
Peter Zijlstra64aa3482008-08-11 09:30:21 +02002730
2731 curr->lockdep_depth = i;
2732 curr->curr_chain_key = hlock->prev_chain_key;
2733
2734 for (; i < depth; i++) {
2735 hlock = curr->held_locks + i;
2736 if (!__lock_acquire(hlock->instance,
Dave Jonesf82b2172008-08-11 09:30:23 +02002737 hlock_class(hlock)->subclass, hlock->trylock,
Peter Zijlstra64aa3482008-08-11 09:30:21 +02002738 hlock->read, hlock->check, hlock->hardirqs_off,
Peter Zijlstra7531e2f2008-08-11 09:30:24 +02002739 hlock->nest_lock, hlock->acquire_ip))
Peter Zijlstra64aa3482008-08-11 09:30:21 +02002740 return 0;
2741 }
2742
2743 if (DEBUG_LOCKS_WARN_ON(curr->lockdep_depth != depth))
2744 return 0;
2745 return 1;
2746}
2747
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002748/*
2749 * Remove the lock to the list of currently held locks in a
2750 * potentially non-nested (out of order) manner. This is a
2751 * relatively rare operation, as all the unlock APIs default
2752 * to nested mode (which uses lock_release()):
2753 */
2754static int
2755lock_release_non_nested(struct task_struct *curr,
2756 struct lockdep_map *lock, unsigned long ip)
2757{
2758 struct held_lock *hlock, *prev_hlock;
2759 unsigned int depth;
2760 int i;
2761
2762 /*
2763 * Check whether the lock exists in the current stack
2764 * of held locks:
2765 */
2766 depth = curr->lockdep_depth;
2767 if (DEBUG_LOCKS_WARN_ON(!depth))
2768 return 0;
2769
2770 prev_hlock = NULL;
2771 for (i = depth-1; i >= 0; i--) {
2772 hlock = curr->held_locks + i;
2773 /*
2774 * We must not cross into another context:
2775 */
2776 if (prev_hlock && prev_hlock->irq_context != hlock->irq_context)
2777 break;
2778 if (hlock->instance == lock)
2779 goto found_it;
2780 prev_hlock = hlock;
2781 }
2782 return print_unlock_inbalance_bug(curr, lock, ip);
2783
2784found_it:
Peter Zijlstraf20786f2007-07-19 01:48:56 -07002785 lock_release_holdtime(hlock);
2786
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002787 /*
2788 * We have the right lock to unlock, 'hlock' points to it.
2789 * Now we remove it from the stack, and add back the other
2790 * entries (if any), recalculating the hash along the way:
2791 */
2792 curr->lockdep_depth = i;
2793 curr->curr_chain_key = hlock->prev_chain_key;
2794
2795 for (i++; i < depth; i++) {
2796 hlock = curr->held_locks + i;
2797 if (!__lock_acquire(hlock->instance,
Dave Jonesf82b2172008-08-11 09:30:23 +02002798 hlock_class(hlock)->subclass, hlock->trylock,
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002799 hlock->read, hlock->check, hlock->hardirqs_off,
Peter Zijlstra7531e2f2008-08-11 09:30:24 +02002800 hlock->nest_lock, hlock->acquire_ip))
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002801 return 0;
2802 }
2803
2804 if (DEBUG_LOCKS_WARN_ON(curr->lockdep_depth != depth - 1))
2805 return 0;
2806 return 1;
2807}
2808
2809/*
2810 * Remove the lock to the list of currently held locks - this gets
2811 * called on mutex_unlock()/spin_unlock*() (or on a failed
2812 * mutex_lock_interruptible()). This is done for unlocks that nest
2813 * perfectly. (i.e. the current top of the lock-stack is unlocked)
2814 */
2815static int lock_release_nested(struct task_struct *curr,
2816 struct lockdep_map *lock, unsigned long ip)
2817{
2818 struct held_lock *hlock;
2819 unsigned int depth;
2820
2821 /*
2822 * Pop off the top of the lock stack:
2823 */
2824 depth = curr->lockdep_depth - 1;
2825 hlock = curr->held_locks + depth;
2826
2827 /*
2828 * Is the unlock non-nested:
2829 */
2830 if (hlock->instance != lock)
2831 return lock_release_non_nested(curr, lock, ip);
2832 curr->lockdep_depth--;
2833
2834 if (DEBUG_LOCKS_WARN_ON(!depth && (hlock->prev_chain_key != 0)))
2835 return 0;
2836
2837 curr->curr_chain_key = hlock->prev_chain_key;
2838
Peter Zijlstraf20786f2007-07-19 01:48:56 -07002839 lock_release_holdtime(hlock);
2840
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002841#ifdef CONFIG_DEBUG_LOCKDEP
2842 hlock->prev_chain_key = 0;
Dave Jonesf82b2172008-08-11 09:30:23 +02002843 hlock->class_idx = 0;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002844 hlock->acquire_ip = 0;
2845 hlock->irq_context = 0;
2846#endif
2847 return 1;
2848}
2849
2850/*
2851 * Remove the lock to the list of currently held locks - this gets
2852 * called on mutex_unlock()/spin_unlock*() (or on a failed
2853 * mutex_lock_interruptible()). This is done for unlocks that nest
2854 * perfectly. (i.e. the current top of the lock-stack is unlocked)
2855 */
2856static void
2857__lock_release(struct lockdep_map *lock, int nested, unsigned long ip)
2858{
2859 struct task_struct *curr = current;
2860
2861 if (!check_unlock(curr, lock, ip))
2862 return;
2863
2864 if (nested) {
2865 if (!lock_release_nested(curr, lock, ip))
2866 return;
2867 } else {
2868 if (!lock_release_non_nested(curr, lock, ip))
2869 return;
2870 }
2871
2872 check_chain_key(curr);
2873}
2874
2875/*
2876 * Check whether we follow the irq-flags state precisely:
2877 */
Steven Rostedt1d09daa2008-05-12 21:20:55 +02002878static void check_flags(unsigned long flags)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002879{
Ingo Molnar992860e2008-07-14 10:28:38 +02002880#if defined(CONFIG_PROVE_LOCKING) && defined(CONFIG_DEBUG_LOCKDEP) && \
2881 defined(CONFIG_TRACE_IRQFLAGS)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002882 if (!debug_locks)
2883 return;
2884
Ingo Molnar5f9fa8a2007-12-07 19:02:47 +01002885 if (irqs_disabled_flags(flags)) {
2886 if (DEBUG_LOCKS_WARN_ON(current->hardirqs_enabled)) {
2887 printk("possible reason: unannotated irqs-off.\n");
2888 }
2889 } else {
2890 if (DEBUG_LOCKS_WARN_ON(!current->hardirqs_enabled)) {
2891 printk("possible reason: unannotated irqs-on.\n");
2892 }
2893 }
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002894
2895 /*
2896 * We dont accurately track softirq state in e.g.
2897 * hardirq contexts (such as on 4KSTACKS), so only
2898 * check if not in hardirq contexts:
2899 */
2900 if (!hardirq_count()) {
2901 if (softirq_count())
2902 DEBUG_LOCKS_WARN_ON(current->softirqs_enabled);
2903 else
2904 DEBUG_LOCKS_WARN_ON(!current->softirqs_enabled);
2905 }
2906
2907 if (!debug_locks)
2908 print_irqtrace_events(current);
2909#endif
2910}
2911
Peter Zijlstra00ef9f72008-12-04 09:00:17 +01002912void lock_set_class(struct lockdep_map *lock, const char *name,
2913 struct lock_class_key *key, unsigned int subclass,
2914 unsigned long ip)
Peter Zijlstra64aa3482008-08-11 09:30:21 +02002915{
2916 unsigned long flags;
2917
2918 if (unlikely(current->lockdep_recursion))
2919 return;
2920
2921 raw_local_irq_save(flags);
2922 current->lockdep_recursion = 1;
2923 check_flags(flags);
Peter Zijlstra00ef9f72008-12-04 09:00:17 +01002924 if (__lock_set_class(lock, name, key, subclass, ip))
Peter Zijlstra64aa3482008-08-11 09:30:21 +02002925 check_chain_key(current);
2926 current->lockdep_recursion = 0;
2927 raw_local_irq_restore(flags);
2928}
Peter Zijlstra00ef9f72008-12-04 09:00:17 +01002929EXPORT_SYMBOL_GPL(lock_set_class);
Peter Zijlstra64aa3482008-08-11 09:30:21 +02002930
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002931/*
2932 * We are not always called with irqs disabled - do that here,
2933 * and also avoid lockdep recursion:
2934 */
Steven Rostedt1d09daa2008-05-12 21:20:55 +02002935void lock_acquire(struct lockdep_map *lock, unsigned int subclass,
Peter Zijlstra7531e2f2008-08-11 09:30:24 +02002936 int trylock, int read, int check,
2937 struct lockdep_map *nest_lock, unsigned long ip)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002938{
2939 unsigned long flags;
2940
2941 if (unlikely(current->lockdep_recursion))
2942 return;
2943
2944 raw_local_irq_save(flags);
2945 check_flags(flags);
2946
2947 current->lockdep_recursion = 1;
2948 __lock_acquire(lock, subclass, trylock, read, check,
Peter Zijlstra7531e2f2008-08-11 09:30:24 +02002949 irqs_disabled_flags(flags), nest_lock, ip);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002950 current->lockdep_recursion = 0;
2951 raw_local_irq_restore(flags);
2952}
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002953EXPORT_SYMBOL_GPL(lock_acquire);
2954
Steven Rostedt1d09daa2008-05-12 21:20:55 +02002955void lock_release(struct lockdep_map *lock, int nested,
Steven Rostedt0764d232008-05-12 21:20:44 +02002956 unsigned long ip)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002957{
2958 unsigned long flags;
2959
2960 if (unlikely(current->lockdep_recursion))
2961 return;
2962
2963 raw_local_irq_save(flags);
2964 check_flags(flags);
2965 current->lockdep_recursion = 1;
2966 __lock_release(lock, nested, ip);
2967 current->lockdep_recursion = 0;
2968 raw_local_irq_restore(flags);
2969}
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002970EXPORT_SYMBOL_GPL(lock_release);
2971
Nick Piggincf40bd12009-01-21 08:12:39 +01002972void lockdep_set_current_reclaim_state(gfp_t gfp_mask)
2973{
2974 current->lockdep_reclaim_gfp = gfp_mask;
2975}
2976
2977void lockdep_clear_current_reclaim_state(void)
2978{
2979 current->lockdep_reclaim_gfp = 0;
2980}
2981
Peter Zijlstraf20786f2007-07-19 01:48:56 -07002982#ifdef CONFIG_LOCK_STAT
2983static int
2984print_lock_contention_bug(struct task_struct *curr, struct lockdep_map *lock,
2985 unsigned long ip)
2986{
2987 if (!debug_locks_off())
2988 return 0;
2989 if (debug_locks_silent)
2990 return 0;
2991
2992 printk("\n=================================\n");
2993 printk( "[ BUG: bad contention detected! ]\n");
2994 printk( "---------------------------------\n");
2995 printk("%s/%d is trying to contend lock (",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07002996 curr->comm, task_pid_nr(curr));
Peter Zijlstraf20786f2007-07-19 01:48:56 -07002997 print_lockdep_cache(lock);
2998 printk(") at:\n");
2999 print_ip_sym(ip);
3000 printk("but there are no locks held!\n");
3001 printk("\nother info that might help us debug this:\n");
3002 lockdep_print_held_locks(curr);
3003
3004 printk("\nstack backtrace:\n");
3005 dump_stack();
3006
3007 return 0;
3008}
3009
3010static void
3011__lock_contended(struct lockdep_map *lock, unsigned long ip)
3012{
3013 struct task_struct *curr = current;
3014 struct held_lock *hlock, *prev_hlock;
3015 struct lock_class_stats *stats;
3016 unsigned int depth;
Peter Zijlstrac7e78cf2008-10-16 23:17:09 +02003017 int i, contention_point, contending_point;
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003018
3019 depth = curr->lockdep_depth;
3020 if (DEBUG_LOCKS_WARN_ON(!depth))
3021 return;
3022
3023 prev_hlock = NULL;
3024 for (i = depth-1; i >= 0; i--) {
3025 hlock = curr->held_locks + i;
3026 /*
3027 * We must not cross into another context:
3028 */
3029 if (prev_hlock && prev_hlock->irq_context != hlock->irq_context)
3030 break;
3031 if (hlock->instance == lock)
3032 goto found_it;
3033 prev_hlock = hlock;
3034 }
3035 print_lock_contention_bug(curr, lock, ip);
3036 return;
3037
3038found_it:
3039 hlock->waittime_stamp = sched_clock();
3040
Peter Zijlstrac7e78cf2008-10-16 23:17:09 +02003041 contention_point = lock_point(hlock_class(hlock)->contention_point, ip);
3042 contending_point = lock_point(hlock_class(hlock)->contending_point,
3043 lock->ip);
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003044
Dave Jonesf82b2172008-08-11 09:30:23 +02003045 stats = get_lock_stats(hlock_class(hlock));
Peter Zijlstrac7e78cf2008-10-16 23:17:09 +02003046 if (contention_point < LOCKSTAT_POINTS)
3047 stats->contention_point[contention_point]++;
3048 if (contending_point < LOCKSTAT_POINTS)
3049 stats->contending_point[contending_point]++;
Peter Zijlstra96645672007-07-19 01:49:00 -07003050 if (lock->cpu != smp_processor_id())
3051 stats->bounces[bounce_contended + !!hlock->read]++;
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003052 put_lock_stats(stats);
3053}
3054
3055static void
Peter Zijlstrac7e78cf2008-10-16 23:17:09 +02003056__lock_acquired(struct lockdep_map *lock, unsigned long ip)
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003057{
3058 struct task_struct *curr = current;
3059 struct held_lock *hlock, *prev_hlock;
3060 struct lock_class_stats *stats;
3061 unsigned int depth;
3062 u64 now;
Peter Zijlstra96645672007-07-19 01:49:00 -07003063 s64 waittime = 0;
3064 int i, cpu;
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003065
3066 depth = curr->lockdep_depth;
3067 if (DEBUG_LOCKS_WARN_ON(!depth))
3068 return;
3069
3070 prev_hlock = NULL;
3071 for (i = depth-1; i >= 0; i--) {
3072 hlock = curr->held_locks + i;
3073 /*
3074 * We must not cross into another context:
3075 */
3076 if (prev_hlock && prev_hlock->irq_context != hlock->irq_context)
3077 break;
3078 if (hlock->instance == lock)
3079 goto found_it;
3080 prev_hlock = hlock;
3081 }
3082 print_lock_contention_bug(curr, lock, _RET_IP_);
3083 return;
3084
3085found_it:
Peter Zijlstra96645672007-07-19 01:49:00 -07003086 cpu = smp_processor_id();
3087 if (hlock->waittime_stamp) {
3088 now = sched_clock();
3089 waittime = now - hlock->waittime_stamp;
3090 hlock->holdtime_stamp = now;
3091 }
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003092
Dave Jonesf82b2172008-08-11 09:30:23 +02003093 stats = get_lock_stats(hlock_class(hlock));
Peter Zijlstra96645672007-07-19 01:49:00 -07003094 if (waittime) {
3095 if (hlock->read)
3096 lock_time_inc(&stats->read_waittime, waittime);
3097 else
3098 lock_time_inc(&stats->write_waittime, waittime);
3099 }
3100 if (lock->cpu != cpu)
3101 stats->bounces[bounce_acquired + !!hlock->read]++;
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003102 put_lock_stats(stats);
Peter Zijlstra96645672007-07-19 01:49:00 -07003103
3104 lock->cpu = cpu;
Peter Zijlstrac7e78cf2008-10-16 23:17:09 +02003105 lock->ip = ip;
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003106}
3107
3108void lock_contended(struct lockdep_map *lock, unsigned long ip)
3109{
3110 unsigned long flags;
3111
3112 if (unlikely(!lock_stat))
3113 return;
3114
3115 if (unlikely(current->lockdep_recursion))
3116 return;
3117
3118 raw_local_irq_save(flags);
3119 check_flags(flags);
3120 current->lockdep_recursion = 1;
3121 __lock_contended(lock, ip);
3122 current->lockdep_recursion = 0;
3123 raw_local_irq_restore(flags);
3124}
3125EXPORT_SYMBOL_GPL(lock_contended);
3126
Peter Zijlstrac7e78cf2008-10-16 23:17:09 +02003127void lock_acquired(struct lockdep_map *lock, unsigned long ip)
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003128{
3129 unsigned long flags;
3130
3131 if (unlikely(!lock_stat))
3132 return;
3133
3134 if (unlikely(current->lockdep_recursion))
3135 return;
3136
3137 raw_local_irq_save(flags);
3138 check_flags(flags);
3139 current->lockdep_recursion = 1;
Peter Zijlstrac7e78cf2008-10-16 23:17:09 +02003140 __lock_acquired(lock, ip);
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003141 current->lockdep_recursion = 0;
3142 raw_local_irq_restore(flags);
3143}
3144EXPORT_SYMBOL_GPL(lock_acquired);
3145#endif
3146
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003147/*
3148 * Used by the testsuite, sanitize the validator state
3149 * after a simulated failure:
3150 */
3151
3152void lockdep_reset(void)
3153{
3154 unsigned long flags;
Ingo Molnar23d95a02006-12-13 00:34:40 -08003155 int i;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003156
3157 raw_local_irq_save(flags);
3158 current->curr_chain_key = 0;
3159 current->lockdep_depth = 0;
3160 current->lockdep_recursion = 0;
3161 memset(current->held_locks, 0, MAX_LOCK_DEPTH*sizeof(struct held_lock));
3162 nr_hardirq_chains = 0;
3163 nr_softirq_chains = 0;
3164 nr_process_chains = 0;
3165 debug_locks = 1;
Ingo Molnar23d95a02006-12-13 00:34:40 -08003166 for (i = 0; i < CHAINHASH_SIZE; i++)
3167 INIT_LIST_HEAD(chainhash_table + i);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003168 raw_local_irq_restore(flags);
3169}
3170
3171static void zap_class(struct lock_class *class)
3172{
3173 int i;
3174
3175 /*
3176 * Remove all dependencies this lock is
3177 * involved in:
3178 */
3179 for (i = 0; i < nr_list_entries; i++) {
3180 if (list_entries[i].class == class)
3181 list_del_rcu(&list_entries[i].entry);
3182 }
3183 /*
3184 * Unhash the class and remove it from the all_lock_classes list:
3185 */
3186 list_del_rcu(&class->hash_entry);
3187 list_del_rcu(&class->lock_entry);
3188
Rabin Vincent8bfe0292008-08-11 09:30:26 +02003189 class->key = NULL;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003190}
3191
Arjan van de Venfabe8742008-01-24 07:00:45 +01003192static inline int within(const void *addr, void *start, unsigned long size)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003193{
3194 return addr >= start && addr < start + size;
3195}
3196
3197void lockdep_free_key_range(void *start, unsigned long size)
3198{
3199 struct lock_class *class, *next;
3200 struct list_head *head;
3201 unsigned long flags;
3202 int i;
Nick Piggin5a26db52008-01-16 09:51:58 +01003203 int locked;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003204
3205 raw_local_irq_save(flags);
Nick Piggin5a26db52008-01-16 09:51:58 +01003206 locked = graph_lock();
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003207
3208 /*
3209 * Unhash all classes that were created by this module:
3210 */
3211 for (i = 0; i < CLASSHASH_SIZE; i++) {
3212 head = classhash_table + i;
3213 if (list_empty(head))
3214 continue;
Arjan van de Venfabe8742008-01-24 07:00:45 +01003215 list_for_each_entry_safe(class, next, head, hash_entry) {
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003216 if (within(class->key, start, size))
3217 zap_class(class);
Arjan van de Venfabe8742008-01-24 07:00:45 +01003218 else if (within(class->name, start, size))
3219 zap_class(class);
3220 }
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003221 }
3222
Nick Piggin5a26db52008-01-16 09:51:58 +01003223 if (locked)
3224 graph_unlock();
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003225 raw_local_irq_restore(flags);
3226}
3227
3228void lockdep_reset_lock(struct lockdep_map *lock)
3229{
Ingo Molnard6d897c2006-07-10 04:44:04 -07003230 struct lock_class *class, *next;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003231 struct list_head *head;
3232 unsigned long flags;
3233 int i, j;
Nick Piggin5a26db52008-01-16 09:51:58 +01003234 int locked;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003235
3236 raw_local_irq_save(flags);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003237
3238 /*
Ingo Molnard6d897c2006-07-10 04:44:04 -07003239 * Remove all classes this lock might have:
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003240 */
Ingo Molnard6d897c2006-07-10 04:44:04 -07003241 for (j = 0; j < MAX_LOCKDEP_SUBCLASSES; j++) {
3242 /*
3243 * If the class exists we look it up and zap it:
3244 */
3245 class = look_up_lock_class(lock, j);
3246 if (class)
3247 zap_class(class);
3248 }
3249 /*
3250 * Debug check: in the end all mapped classes should
3251 * be gone.
3252 */
Nick Piggin5a26db52008-01-16 09:51:58 +01003253 locked = graph_lock();
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003254 for (i = 0; i < CLASSHASH_SIZE; i++) {
3255 head = classhash_table + i;
3256 if (list_empty(head))
3257 continue;
3258 list_for_each_entry_safe(class, next, head, hash_entry) {
Ingo Molnard6d897c2006-07-10 04:44:04 -07003259 if (unlikely(class == lock->class_cache)) {
Ingo Molnar74c383f2006-12-13 00:34:43 -08003260 if (debug_locks_off_graph_unlock())
3261 WARN_ON(1);
Ingo Molnard6d897c2006-07-10 04:44:04 -07003262 goto out_restore;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003263 }
3264 }
3265 }
Nick Piggin5a26db52008-01-16 09:51:58 +01003266 if (locked)
3267 graph_unlock();
Ingo Molnard6d897c2006-07-10 04:44:04 -07003268
3269out_restore:
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003270 raw_local_irq_restore(flags);
3271}
3272
Sam Ravnborg14999932007-02-28 20:12:31 -08003273void lockdep_init(void)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003274{
3275 int i;
3276
3277 /*
3278 * Some architectures have their own start_kernel()
3279 * code which calls lockdep_init(), while we also
3280 * call lockdep_init() from the start_kernel() itself,
3281 * and we want to initialize the hashes only once:
3282 */
3283 if (lockdep_initialized)
3284 return;
3285
3286 for (i = 0; i < CLASSHASH_SIZE; i++)
3287 INIT_LIST_HEAD(classhash_table + i);
3288
3289 for (i = 0; i < CHAINHASH_SIZE; i++)
3290 INIT_LIST_HEAD(chainhash_table + i);
3291
3292 lockdep_initialized = 1;
3293}
3294
3295void __init lockdep_info(void)
3296{
3297 printk("Lock dependency validator: Copyright (c) 2006 Red Hat, Inc., Ingo Molnar\n");
3298
Li Zefanb0788ca2008-11-21 15:57:32 +08003299 printk("... MAX_LOCKDEP_SUBCLASSES: %lu\n", MAX_LOCKDEP_SUBCLASSES);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003300 printk("... MAX_LOCK_DEPTH: %lu\n", MAX_LOCK_DEPTH);
3301 printk("... MAX_LOCKDEP_KEYS: %lu\n", MAX_LOCKDEP_KEYS);
Li Zefanb0788ca2008-11-21 15:57:32 +08003302 printk("... CLASSHASH_SIZE: %lu\n", CLASSHASH_SIZE);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003303 printk("... MAX_LOCKDEP_ENTRIES: %lu\n", MAX_LOCKDEP_ENTRIES);
3304 printk("... MAX_LOCKDEP_CHAINS: %lu\n", MAX_LOCKDEP_CHAINS);
3305 printk("... CHAINHASH_SIZE: %lu\n", CHAINHASH_SIZE);
3306
3307 printk(" memory used by lock dependency info: %lu kB\n",
3308 (sizeof(struct lock_class) * MAX_LOCKDEP_KEYS +
3309 sizeof(struct list_head) * CLASSHASH_SIZE +
3310 sizeof(struct lock_list) * MAX_LOCKDEP_ENTRIES +
3311 sizeof(struct lock_chain) * MAX_LOCKDEP_CHAINS +
3312 sizeof(struct list_head) * CHAINHASH_SIZE) / 1024);
3313
3314 printk(" per task-struct memory footprint: %lu bytes\n",
3315 sizeof(struct held_lock) * MAX_LOCK_DEPTH);
3316
3317#ifdef CONFIG_DEBUG_LOCKDEP
Johannes Bergc71063c2007-07-19 01:49:02 -07003318 if (lockdep_init_error) {
3319 printk("WARNING: lockdep init error! Arch code didn't call lockdep_init() early enough?\n");
3320 printk("Call stack leading to lockdep invocation was:\n");
3321 print_stack_trace(&lockdep_init_trace, 0);
3322 }
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003323#endif
3324}
3325
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003326static void
3327print_freed_lock_bug(struct task_struct *curr, const void *mem_from,
Arjan van de Ven55794a42006-07-10 04:44:03 -07003328 const void *mem_to, struct held_lock *hlock)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003329{
3330 if (!debug_locks_off())
3331 return;
3332 if (debug_locks_silent)
3333 return;
3334
3335 printk("\n=========================\n");
3336 printk( "[ BUG: held lock freed! ]\n");
3337 printk( "-------------------------\n");
3338 printk("%s/%d is freeing memory %p-%p, with a lock still held there!\n",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07003339 curr->comm, task_pid_nr(curr), mem_from, mem_to-1);
Arjan van de Ven55794a42006-07-10 04:44:03 -07003340 print_lock(hlock);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003341 lockdep_print_held_locks(curr);
3342
3343 printk("\nstack backtrace:\n");
3344 dump_stack();
3345}
3346
Oleg Nesterov54561782007-12-05 15:46:09 +01003347static inline int not_in_range(const void* mem_from, unsigned long mem_len,
3348 const void* lock_from, unsigned long lock_len)
3349{
3350 return lock_from + lock_len <= mem_from ||
3351 mem_from + mem_len <= lock_from;
3352}
3353
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003354/*
3355 * Called when kernel memory is freed (or unmapped), or if a lock
3356 * is destroyed or reinitialized - this code checks whether there is
3357 * any held lock in the memory range of <from> to <to>:
3358 */
3359void debug_check_no_locks_freed(const void *mem_from, unsigned long mem_len)
3360{
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003361 struct task_struct *curr = current;
3362 struct held_lock *hlock;
3363 unsigned long flags;
3364 int i;
3365
3366 if (unlikely(!debug_locks))
3367 return;
3368
3369 local_irq_save(flags);
3370 for (i = 0; i < curr->lockdep_depth; i++) {
3371 hlock = curr->held_locks + i;
3372
Oleg Nesterov54561782007-12-05 15:46:09 +01003373 if (not_in_range(mem_from, mem_len, hlock->instance,
3374 sizeof(*hlock->instance)))
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003375 continue;
3376
Oleg Nesterov54561782007-12-05 15:46:09 +01003377 print_freed_lock_bug(curr, mem_from, mem_from + mem_len, hlock);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003378 break;
3379 }
3380 local_irq_restore(flags);
3381}
Peter Zijlstraed075362006-12-06 20:35:24 -08003382EXPORT_SYMBOL_GPL(debug_check_no_locks_freed);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003383
3384static void print_held_locks_bug(struct task_struct *curr)
3385{
3386 if (!debug_locks_off())
3387 return;
3388 if (debug_locks_silent)
3389 return;
3390
3391 printk("\n=====================================\n");
3392 printk( "[ BUG: lock held at task exit time! ]\n");
3393 printk( "-------------------------------------\n");
3394 printk("%s/%d is exiting with locks still held!\n",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07003395 curr->comm, task_pid_nr(curr));
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003396 lockdep_print_held_locks(curr);
3397
3398 printk("\nstack backtrace:\n");
3399 dump_stack();
3400}
3401
3402void debug_check_no_locks_held(struct task_struct *task)
3403{
3404 if (unlikely(task->lockdep_depth > 0))
3405 print_held_locks_bug(task);
3406}
3407
3408void debug_show_all_locks(void)
3409{
3410 struct task_struct *g, *p;
3411 int count = 10;
3412 int unlock = 1;
3413
Jarek Poplawski9c35dd72007-03-22 00:11:28 -08003414 if (unlikely(!debug_locks)) {
3415 printk("INFO: lockdep is turned off.\n");
3416 return;
3417 }
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003418 printk("\nShowing all locks held in the system:\n");
3419
3420 /*
3421 * Here we try to get the tasklist_lock as hard as possible,
3422 * if not successful after 2 seconds we ignore it (but keep
3423 * trying). This is to enable a debug printout even if a
3424 * tasklist_lock-holding task deadlocks or crashes.
3425 */
3426retry:
3427 if (!read_trylock(&tasklist_lock)) {
3428 if (count == 10)
3429 printk("hm, tasklist_lock locked, retrying... ");
3430 if (count) {
3431 count--;
3432 printk(" #%d", 10-count);
3433 mdelay(200);
3434 goto retry;
3435 }
3436 printk(" ignoring it.\n");
3437 unlock = 0;
qinghuang feng46fec7a2008-10-28 17:24:28 +08003438 } else {
3439 if (count != 10)
3440 printk(KERN_CONT " locked it.\n");
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003441 }
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003442
3443 do_each_thread(g, p) {
Ingo Molnar85684872007-12-05 15:46:09 +01003444 /*
3445 * It's not reliable to print a task's held locks
3446 * if it's not sleeping (or if it's not the current
3447 * task):
3448 */
3449 if (p->state == TASK_RUNNING && p != current)
3450 continue;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003451 if (p->lockdep_depth)
3452 lockdep_print_held_locks(p);
3453 if (!unlock)
3454 if (read_trylock(&tasklist_lock))
3455 unlock = 1;
3456 } while_each_thread(g, p);
3457
3458 printk("\n");
3459 printk("=============================================\n\n");
3460
3461 if (unlock)
3462 read_unlock(&tasklist_lock);
3463}
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003464EXPORT_SYMBOL_GPL(debug_show_all_locks);
3465
Ingo Molnar82a1fcb2008-01-25 21:08:02 +01003466/*
3467 * Careful: only use this function if you are sure that
3468 * the task cannot run in parallel!
3469 */
3470void __debug_show_held_locks(struct task_struct *task)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003471{
Jarek Poplawski9c35dd72007-03-22 00:11:28 -08003472 if (unlikely(!debug_locks)) {
3473 printk("INFO: lockdep is turned off.\n");
3474 return;
3475 }
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003476 lockdep_print_held_locks(task);
3477}
Ingo Molnar82a1fcb2008-01-25 21:08:02 +01003478EXPORT_SYMBOL_GPL(__debug_show_held_locks);
3479
3480void debug_show_held_locks(struct task_struct *task)
3481{
3482 __debug_show_held_locks(task);
3483}
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003484EXPORT_SYMBOL_GPL(debug_show_held_locks);
Peter Zijlstrab351d162007-10-11 22:11:12 +02003485
3486void lockdep_sys_exit(void)
3487{
3488 struct task_struct *curr = current;
3489
3490 if (unlikely(curr->lockdep_depth)) {
3491 if (!debug_locks_off())
3492 return;
3493 printk("\n================================================\n");
3494 printk( "[ BUG: lock held when returning to user space! ]\n");
3495 printk( "------------------------------------------------\n");
3496 printk("%s/%d is leaving the kernel with locks still held!\n",
3497 curr->comm, curr->pid);
3498 lockdep_print_held_locks(curr);
3499 }
3500}