blob: 981cd4854281458cc78e2e4730ee93df075287f4 [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 Heo9281acea2007-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 Molnar70e450672006-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 Molnar70e450672006-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 Molnar70e450672006-12-06 20:40:50 -0800791 raw_local_irq_restore(flags);
Ingo Molnar74c383f2006-12-13 00:34:43 -0800792
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700793 printk("BUG: MAX_LOCKDEP_KEYS too low!\n");
794 printk("turning off the locking correctness validator.\n");
795 return NULL;
796 }
797 class = lock_classes + nr_lock_classes++;
798 debug_atomic_inc(&nr_unused_locks);
799 class->key = key;
800 class->name = lock->name;
801 class->subclass = subclass;
802 INIT_LIST_HEAD(&class->lock_entry);
803 INIT_LIST_HEAD(&class->locks_before);
804 INIT_LIST_HEAD(&class->locks_after);
805 class->name_version = count_matching_names(class);
806 /*
807 * We use RCU's safe list-add method to make
808 * parallel walking of the hash-list safe:
809 */
810 list_add_tail_rcu(&class->hash_entry, hash_head);
Dale Farnsworth14811972008-02-25 23:03:02 +0100811 /*
812 * Add it to the global list of classes:
813 */
814 list_add_tail_rcu(&class->lock_entry, &all_lock_classes);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700815
816 if (verbose(class)) {
Ingo Molnar74c383f2006-12-13 00:34:43 -0800817 graph_unlock();
Ingo Molnar70e450672006-12-06 20:40:50 -0800818 raw_local_irq_restore(flags);
Ingo Molnar74c383f2006-12-13 00:34:43 -0800819
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700820 printk("\nnew class %p: %s", class->key, class->name);
821 if (class->name_version > 1)
822 printk("#%d", class->name_version);
823 printk("\n");
824 dump_stack();
Ingo Molnar74c383f2006-12-13 00:34:43 -0800825
Ingo Molnar70e450672006-12-06 20:40:50 -0800826 raw_local_irq_save(flags);
Ingo Molnar74c383f2006-12-13 00:34:43 -0800827 if (!graph_lock()) {
828 raw_local_irq_restore(flags);
829 return NULL;
830 }
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700831 }
832out_unlock_set:
Ingo Molnar74c383f2006-12-13 00:34:43 -0800833 graph_unlock();
Ingo Molnar70e450672006-12-06 20:40:50 -0800834 raw_local_irq_restore(flags);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700835
Peter Zijlstra4dfbb9d2006-10-11 01:45:14 -0400836 if (!subclass || force)
Ingo Molnard6d897c2006-07-10 04:44:04 -0700837 lock->class_cache = class;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700838
Jarek Poplawski381a2292007-02-10 01:44:58 -0800839 if (DEBUG_LOCKS_WARN_ON(class->subclass != subclass))
840 return NULL;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700841
842 return class;
843}
844
Peter Zijlstraca58abc2007-07-19 01:48:53 -0700845#ifdef CONFIG_PROVE_LOCKING
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700846/*
Peter Zijlstra8e182572007-07-19 01:48:54 -0700847 * Allocate a lockdep entry. (assumes the graph_lock held, returns
848 * with NULL on failure)
849 */
850static struct lock_list *alloc_list_entry(void)
851{
852 if (nr_list_entries >= MAX_LOCKDEP_ENTRIES) {
853 if (!debug_locks_off_graph_unlock())
854 return NULL;
855
856 printk("BUG: MAX_LOCKDEP_ENTRIES too low!\n");
857 printk("turning off the locking correctness validator.\n");
858 return NULL;
859 }
860 return list_entries + nr_list_entries++;
861}
862
863/*
864 * Add a new dependency to the head of the list:
865 */
866static int add_lock_to_list(struct lock_class *class, struct lock_class *this,
867 struct list_head *head, unsigned long ip, int distance)
868{
869 struct lock_list *entry;
870 /*
871 * Lock not present yet - get a new dependency struct and
872 * add it to the list:
873 */
874 entry = alloc_list_entry();
875 if (!entry)
876 return 0;
877
Peter Zijlstra8e182572007-07-19 01:48:54 -0700878 if (!save_trace(&entry->trace))
879 return 0;
880
Zhu Yi74870172008-08-27 14:33:00 +0800881 entry->class = this;
882 entry->distance = distance;
Peter Zijlstra8e182572007-07-19 01:48:54 -0700883 /*
884 * Since we never remove from the dependency list, the list can
885 * be walked lockless by other CPUs, it's only allocation
886 * that must be protected by the spinlock. But this also means
887 * we must make new entries visible only once writes to the
888 * entry become visible - hence the RCU op:
889 */
890 list_add_tail_rcu(&entry->entry, head);
891
892 return 1;
893}
894
895/*
896 * Recursive, forwards-direction lock-dependency checking, used for
897 * both noncyclic checking and for hardirq-unsafe/softirq-unsafe
898 * checking.
899 *
900 * (to keep the stackframe of the recursive functions small we
901 * use these global variables, and we also mark various helper
902 * functions as noinline.)
903 */
904static struct held_lock *check_source, *check_target;
905
906/*
907 * Print a dependency chain entry (this is only done when a deadlock
908 * has been detected):
909 */
910static noinline int
911print_circular_bug_entry(struct lock_list *target, unsigned int depth)
912{
913 if (debug_locks_silent)
914 return 0;
915 printk("\n-> #%u", depth);
916 print_lock_name(target->class);
917 printk(":\n");
918 print_stack_trace(&target->trace, 6);
919
920 return 0;
921}
922
923/*
924 * When a circular dependency is detected, print the
925 * header first:
926 */
927static noinline int
928print_circular_bug_header(struct lock_list *entry, unsigned int depth)
929{
930 struct task_struct *curr = current;
931
932 if (!debug_locks_off_graph_unlock() || debug_locks_silent)
933 return 0;
934
935 printk("\n=======================================================\n");
936 printk( "[ INFO: possible circular locking dependency detected ]\n");
937 print_kernel_version();
938 printk( "-------------------------------------------------------\n");
939 printk("%s/%d is trying to acquire lock:\n",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -0700940 curr->comm, task_pid_nr(curr));
Peter Zijlstra8e182572007-07-19 01:48:54 -0700941 print_lock(check_source);
942 printk("\nbut task is already holding lock:\n");
943 print_lock(check_target);
944 printk("\nwhich lock already depends on the new lock.\n\n");
945 printk("\nthe existing dependency chain (in reverse order) is:\n");
946
947 print_circular_bug_entry(entry, depth);
948
949 return 0;
950}
951
952static noinline int print_circular_bug_tail(void)
953{
954 struct task_struct *curr = current;
955 struct lock_list this;
956
957 if (debug_locks_silent)
958 return 0;
959
Dave Jonesf82b2172008-08-11 09:30:23 +0200960 this.class = hlock_class(check_source);
Peter Zijlstra8e182572007-07-19 01:48:54 -0700961 if (!save_trace(&this.trace))
962 return 0;
963
964 print_circular_bug_entry(&this, 0);
965
966 printk("\nother info that might help us debug this:\n\n");
967 lockdep_print_held_locks(curr);
968
969 printk("\nstack backtrace:\n");
970 dump_stack();
971
972 return 0;
973}
974
975#define RECURSION_LIMIT 40
976
977static int noinline print_infinite_recursion_bug(void)
978{
979 if (!debug_locks_off_graph_unlock())
980 return 0;
981
982 WARN_ON(1);
983
984 return 0;
985}
986
David Miller419ca3f2008-07-29 21:45:03 -0700987unsigned long __lockdep_count_forward_deps(struct lock_class *class,
988 unsigned int depth)
989{
990 struct lock_list *entry;
991 unsigned long ret = 1;
992
993 if (lockdep_dependency_visit(class, depth))
994 return 0;
995
996 /*
997 * Recurse this class's dependency list:
998 */
999 list_for_each_entry(entry, &class->locks_after, entry)
1000 ret += __lockdep_count_forward_deps(entry->class, depth + 1);
1001
1002 return ret;
1003}
1004
1005unsigned long lockdep_count_forward_deps(struct lock_class *class)
1006{
1007 unsigned long ret, flags;
1008
1009 local_irq_save(flags);
1010 __raw_spin_lock(&lockdep_lock);
1011 ret = __lockdep_count_forward_deps(class, 0);
1012 __raw_spin_unlock(&lockdep_lock);
1013 local_irq_restore(flags);
1014
1015 return ret;
1016}
1017
1018unsigned long __lockdep_count_backward_deps(struct lock_class *class,
1019 unsigned int depth)
1020{
1021 struct lock_list *entry;
1022 unsigned long ret = 1;
1023
1024 if (lockdep_dependency_visit(class, depth))
1025 return 0;
1026 /*
1027 * Recurse this class's dependency list:
1028 */
1029 list_for_each_entry(entry, &class->locks_before, entry)
1030 ret += __lockdep_count_backward_deps(entry->class, depth + 1);
1031
1032 return ret;
1033}
1034
1035unsigned long lockdep_count_backward_deps(struct lock_class *class)
1036{
1037 unsigned long ret, flags;
1038
1039 local_irq_save(flags);
1040 __raw_spin_lock(&lockdep_lock);
1041 ret = __lockdep_count_backward_deps(class, 0);
1042 __raw_spin_unlock(&lockdep_lock);
1043 local_irq_restore(flags);
1044
1045 return ret;
1046}
1047
Peter Zijlstra8e182572007-07-19 01:48:54 -07001048/*
1049 * Prove that the dependency graph starting at <entry> can not
1050 * lead to <target>. Print an error and return 0 if it does.
1051 */
1052static noinline int
1053check_noncircular(struct lock_class *source, unsigned int depth)
1054{
1055 struct lock_list *entry;
1056
David Miller419ca3f2008-07-29 21:45:03 -07001057 if (lockdep_dependency_visit(source, depth))
1058 return 1;
1059
Peter Zijlstra8e182572007-07-19 01:48:54 -07001060 debug_atomic_inc(&nr_cyclic_check_recursions);
1061 if (depth > max_recursion_depth)
1062 max_recursion_depth = depth;
1063 if (depth >= RECURSION_LIMIT)
1064 return print_infinite_recursion_bug();
1065 /*
1066 * Check this lock's dependency list:
1067 */
1068 list_for_each_entry(entry, &source->locks_after, entry) {
Dave Jonesf82b2172008-08-11 09:30:23 +02001069 if (entry->class == hlock_class(check_target))
Peter Zijlstra8e182572007-07-19 01:48:54 -07001070 return print_circular_bug_header(entry, depth+1);
1071 debug_atomic_inc(&nr_cyclic_checks);
1072 if (!check_noncircular(entry->class, depth+1))
1073 return print_circular_bug_entry(entry, depth+1);
1074 }
1075 return 1;
1076}
1077
Steven Rostedt81d68a92008-05-12 21:20:42 +02001078#if defined(CONFIG_TRACE_IRQFLAGS) && defined(CONFIG_PROVE_LOCKING)
Peter Zijlstra8e182572007-07-19 01:48:54 -07001079/*
1080 * Forwards and backwards subgraph searching, for the purposes of
1081 * proving that two subgraphs can be connected by a new dependency
1082 * without creating any illegal irq-safe -> irq-unsafe lock dependency.
1083 */
1084static enum lock_usage_bit find_usage_bit;
1085static struct lock_class *forwards_match, *backwards_match;
1086
1087/*
1088 * Find a node in the forwards-direction dependency sub-graph starting
1089 * at <source> that matches <find_usage_bit>.
1090 *
1091 * Return 2 if such a node exists in the subgraph, and put that node
1092 * into <forwards_match>.
1093 *
1094 * Return 1 otherwise and keep <forwards_match> unchanged.
1095 * Return 0 on error.
1096 */
1097static noinline int
1098find_usage_forwards(struct lock_class *source, unsigned int depth)
1099{
1100 struct lock_list *entry;
1101 int ret;
1102
David Miller419ca3f2008-07-29 21:45:03 -07001103 if (lockdep_dependency_visit(source, depth))
1104 return 1;
1105
Peter Zijlstra8e182572007-07-19 01:48:54 -07001106 if (depth > max_recursion_depth)
1107 max_recursion_depth = depth;
1108 if (depth >= RECURSION_LIMIT)
1109 return print_infinite_recursion_bug();
1110
1111 debug_atomic_inc(&nr_find_usage_forwards_checks);
1112 if (source->usage_mask & (1 << find_usage_bit)) {
1113 forwards_match = source;
1114 return 2;
1115 }
1116
1117 /*
1118 * Check this lock's dependency list:
1119 */
1120 list_for_each_entry(entry, &source->locks_after, entry) {
1121 debug_atomic_inc(&nr_find_usage_forwards_recursions);
1122 ret = find_usage_forwards(entry->class, depth+1);
1123 if (ret == 2 || ret == 0)
1124 return ret;
1125 }
1126 return 1;
1127}
1128
1129/*
1130 * Find a node in the backwards-direction dependency sub-graph starting
1131 * at <source> that matches <find_usage_bit>.
1132 *
1133 * Return 2 if such a node exists in the subgraph, and put that node
1134 * into <backwards_match>.
1135 *
1136 * Return 1 otherwise and keep <backwards_match> unchanged.
1137 * Return 0 on error.
1138 */
Steven Rostedt1d09daa2008-05-12 21:20:55 +02001139static noinline int
Peter Zijlstra8e182572007-07-19 01:48:54 -07001140find_usage_backwards(struct lock_class *source, unsigned int depth)
1141{
1142 struct lock_list *entry;
1143 int ret;
1144
David Miller419ca3f2008-07-29 21:45:03 -07001145 if (lockdep_dependency_visit(source, depth))
1146 return 1;
1147
Peter Zijlstra8e182572007-07-19 01:48:54 -07001148 if (!__raw_spin_is_locked(&lockdep_lock))
1149 return DEBUG_LOCKS_WARN_ON(1);
1150
1151 if (depth > max_recursion_depth)
1152 max_recursion_depth = depth;
1153 if (depth >= RECURSION_LIMIT)
1154 return print_infinite_recursion_bug();
1155
1156 debug_atomic_inc(&nr_find_usage_backwards_checks);
1157 if (source->usage_mask & (1 << find_usage_bit)) {
1158 backwards_match = source;
1159 return 2;
1160 }
1161
Dave Jonesf82b2172008-08-11 09:30:23 +02001162 if (!source && debug_locks_off_graph_unlock()) {
1163 WARN_ON(1);
1164 return 0;
1165 }
1166
Peter Zijlstra8e182572007-07-19 01:48:54 -07001167 /*
1168 * Check this lock's dependency list:
1169 */
1170 list_for_each_entry(entry, &source->locks_before, entry) {
1171 debug_atomic_inc(&nr_find_usage_backwards_recursions);
1172 ret = find_usage_backwards(entry->class, depth+1);
1173 if (ret == 2 || ret == 0)
1174 return ret;
1175 }
1176 return 1;
1177}
1178
1179static int
1180print_bad_irq_dependency(struct task_struct *curr,
1181 struct held_lock *prev,
1182 struct held_lock *next,
1183 enum lock_usage_bit bit1,
1184 enum lock_usage_bit bit2,
1185 const char *irqclass)
1186{
1187 if (!debug_locks_off_graph_unlock() || debug_locks_silent)
1188 return 0;
1189
1190 printk("\n======================================================\n");
1191 printk( "[ INFO: %s-safe -> %s-unsafe lock order detected ]\n",
1192 irqclass, irqclass);
1193 print_kernel_version();
1194 printk( "------------------------------------------------------\n");
1195 printk("%s/%d [HC%u[%lu]:SC%u[%lu]:HE%u:SE%u] is trying to acquire:\n",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07001196 curr->comm, task_pid_nr(curr),
Peter Zijlstra8e182572007-07-19 01:48:54 -07001197 curr->hardirq_context, hardirq_count() >> HARDIRQ_SHIFT,
1198 curr->softirq_context, softirq_count() >> SOFTIRQ_SHIFT,
1199 curr->hardirqs_enabled,
1200 curr->softirqs_enabled);
1201 print_lock(next);
1202
1203 printk("\nand this task is already holding:\n");
1204 print_lock(prev);
1205 printk("which would create a new lock dependency:\n");
Dave Jonesf82b2172008-08-11 09:30:23 +02001206 print_lock_name(hlock_class(prev));
Peter Zijlstra8e182572007-07-19 01:48:54 -07001207 printk(" ->");
Dave Jonesf82b2172008-08-11 09:30:23 +02001208 print_lock_name(hlock_class(next));
Peter Zijlstra8e182572007-07-19 01:48:54 -07001209 printk("\n");
1210
1211 printk("\nbut this new dependency connects a %s-irq-safe lock:\n",
1212 irqclass);
1213 print_lock_name(backwards_match);
1214 printk("\n... which became %s-irq-safe at:\n", irqclass);
1215
1216 print_stack_trace(backwards_match->usage_traces + bit1, 1);
1217
1218 printk("\nto a %s-irq-unsafe lock:\n", irqclass);
1219 print_lock_name(forwards_match);
1220 printk("\n... which became %s-irq-unsafe at:\n", irqclass);
1221 printk("...");
1222
1223 print_stack_trace(forwards_match->usage_traces + bit2, 1);
1224
1225 printk("\nother info that might help us debug this:\n\n");
1226 lockdep_print_held_locks(curr);
1227
1228 printk("\nthe %s-irq-safe lock's dependencies:\n", irqclass);
1229 print_lock_dependencies(backwards_match, 0);
1230
1231 printk("\nthe %s-irq-unsafe lock's dependencies:\n", irqclass);
1232 print_lock_dependencies(forwards_match, 0);
1233
1234 printk("\nstack backtrace:\n");
1235 dump_stack();
1236
1237 return 0;
1238}
1239
1240static int
1241check_usage(struct task_struct *curr, struct held_lock *prev,
1242 struct held_lock *next, enum lock_usage_bit bit_backwards,
1243 enum lock_usage_bit bit_forwards, const char *irqclass)
1244{
1245 int ret;
1246
1247 find_usage_bit = bit_backwards;
1248 /* fills in <backwards_match> */
Dave Jonesf82b2172008-08-11 09:30:23 +02001249 ret = find_usage_backwards(hlock_class(prev), 0);
Peter Zijlstra8e182572007-07-19 01:48:54 -07001250 if (!ret || ret == 1)
1251 return ret;
1252
1253 find_usage_bit = bit_forwards;
Dave Jonesf82b2172008-08-11 09:30:23 +02001254 ret = find_usage_forwards(hlock_class(next), 0);
Peter Zijlstra8e182572007-07-19 01:48:54 -07001255 if (!ret || ret == 1)
1256 return ret;
1257 /* ret == 2 */
1258 return print_bad_irq_dependency(curr, prev, next,
1259 bit_backwards, bit_forwards, irqclass);
1260}
1261
Peter Zijlstra4f367d8a2009-01-22 18:10:42 +01001262static const char *state_names[] = {
1263#define LOCKDEP_STATE(__STATE) \
Peter Zijlstrab4b136f2009-01-29 14:50:36 +01001264 __stringify(__STATE),
Peter Zijlstra4f367d8a2009-01-22 18:10:42 +01001265#include "lockdep_states.h"
1266#undef LOCKDEP_STATE
1267};
1268
1269static const char *state_rnames[] = {
1270#define LOCKDEP_STATE(__STATE) \
Peter Zijlstrab4b136f2009-01-29 14:50:36 +01001271 __stringify(__STATE)"-READ",
Peter Zijlstra4f367d8a2009-01-22 18:10:42 +01001272#include "lockdep_states.h"
1273#undef LOCKDEP_STATE
1274};
1275
1276static inline const char *state_name(enum lock_usage_bit bit)
1277{
1278 return (bit & 1) ? state_rnames[bit >> 2] : state_names[bit >> 2];
1279}
1280
1281static int exclusive_bit(int new_bit)
1282{
1283 /*
1284 * USED_IN
1285 * USED_IN_READ
1286 * ENABLED
1287 * ENABLED_READ
1288 *
1289 * bit 0 - write/read
1290 * bit 1 - used_in/enabled
1291 * bit 2+ state
1292 */
1293
1294 int state = new_bit & ~3;
1295 int dir = new_bit & 2;
1296
1297 /*
1298 * keep state, bit flip the direction and strip read.
1299 */
1300 return state | (dir ^ 2);
1301}
1302
1303static int check_irq_usage(struct task_struct *curr, struct held_lock *prev,
1304 struct held_lock *next, enum lock_usage_bit bit)
Peter Zijlstra8e182572007-07-19 01:48:54 -07001305{
1306 /*
1307 * Prove that the new dependency does not connect a hardirq-safe
1308 * lock with a hardirq-unsafe lock - to achieve this we search
1309 * the backwards-subgraph starting at <prev>, and the
1310 * forwards-subgraph starting at <next>:
1311 */
Peter Zijlstra4f367d8a2009-01-22 18:10:42 +01001312 if (!check_usage(curr, prev, next, bit,
1313 exclusive_bit(bit), state_name(bit)))
Peter Zijlstra8e182572007-07-19 01:48:54 -07001314 return 0;
1315
Peter Zijlstra4f367d8a2009-01-22 18:10:42 +01001316 bit++; /* _READ */
1317
Peter Zijlstra8e182572007-07-19 01:48:54 -07001318 /*
1319 * Prove that the new dependency does not connect a hardirq-safe-read
1320 * lock with a hardirq-unsafe lock - to achieve this we search
1321 * the backwards-subgraph starting at <prev>, and the
1322 * forwards-subgraph starting at <next>:
1323 */
Peter Zijlstra4f367d8a2009-01-22 18:10:42 +01001324 if (!check_usage(curr, prev, next, bit,
1325 exclusive_bit(bit), state_name(bit)))
Peter Zijlstra8e182572007-07-19 01:48:54 -07001326 return 0;
1327
Peter Zijlstra4f367d8a2009-01-22 18:10:42 +01001328 return 1;
1329}
Peter Zijlstra8e182572007-07-19 01:48:54 -07001330
Peter Zijlstra4f367d8a2009-01-22 18:10:42 +01001331static int
1332check_prev_add_irq(struct task_struct *curr, struct held_lock *prev,
1333 struct held_lock *next)
1334{
1335#define LOCKDEP_STATE(__STATE) \
1336 if (!check_irq_usage(curr, prev, next, LOCK_USED_IN_##__STATE)) \
Nick Piggincf40bd12009-01-21 08:12:39 +01001337 return 0;
Peter Zijlstra4f367d8a2009-01-22 18:10:42 +01001338#include "lockdep_states.h"
1339#undef LOCKDEP_STATE
Nick Piggincf40bd12009-01-21 08:12:39 +01001340
Peter Zijlstra8e182572007-07-19 01:48:54 -07001341 return 1;
1342}
1343
1344static void inc_chains(void)
1345{
1346 if (current->hardirq_context)
1347 nr_hardirq_chains++;
1348 else {
1349 if (current->softirq_context)
1350 nr_softirq_chains++;
1351 else
1352 nr_process_chains++;
1353 }
1354}
1355
1356#else
1357
1358static inline int
1359check_prev_add_irq(struct task_struct *curr, struct held_lock *prev,
1360 struct held_lock *next)
1361{
1362 return 1;
1363}
1364
1365static inline void inc_chains(void)
1366{
1367 nr_process_chains++;
1368}
1369
1370#endif
1371
1372static int
1373print_deadlock_bug(struct task_struct *curr, struct held_lock *prev,
1374 struct held_lock *next)
1375{
1376 if (!debug_locks_off_graph_unlock() || debug_locks_silent)
1377 return 0;
1378
1379 printk("\n=============================================\n");
1380 printk( "[ INFO: possible recursive locking detected ]\n");
1381 print_kernel_version();
1382 printk( "---------------------------------------------\n");
1383 printk("%s/%d is trying to acquire lock:\n",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07001384 curr->comm, task_pid_nr(curr));
Peter Zijlstra8e182572007-07-19 01:48:54 -07001385 print_lock(next);
1386 printk("\nbut task is already holding lock:\n");
1387 print_lock(prev);
1388
1389 printk("\nother info that might help us debug this:\n");
1390 lockdep_print_held_locks(curr);
1391
1392 printk("\nstack backtrace:\n");
1393 dump_stack();
1394
1395 return 0;
1396}
1397
1398/*
1399 * Check whether we are holding such a class already.
1400 *
1401 * (Note that this has to be done separately, because the graph cannot
1402 * detect such classes of deadlocks.)
1403 *
1404 * Returns: 0 on deadlock detected, 1 on OK, 2 on recursive read
1405 */
1406static int
1407check_deadlock(struct task_struct *curr, struct held_lock *next,
1408 struct lockdep_map *next_instance, int read)
1409{
1410 struct held_lock *prev;
Peter Zijlstra7531e2f2008-08-11 09:30:24 +02001411 struct held_lock *nest = NULL;
Peter Zijlstra8e182572007-07-19 01:48:54 -07001412 int i;
1413
1414 for (i = 0; i < curr->lockdep_depth; i++) {
1415 prev = curr->held_locks + i;
Peter Zijlstra7531e2f2008-08-11 09:30:24 +02001416
1417 if (prev->instance == next->nest_lock)
1418 nest = prev;
1419
Dave Jonesf82b2172008-08-11 09:30:23 +02001420 if (hlock_class(prev) != hlock_class(next))
Peter Zijlstra8e182572007-07-19 01:48:54 -07001421 continue;
Peter Zijlstra7531e2f2008-08-11 09:30:24 +02001422
Peter Zijlstra8e182572007-07-19 01:48:54 -07001423 /*
1424 * Allow read-after-read recursion of the same
1425 * lock class (i.e. read_lock(lock)+read_lock(lock)):
1426 */
1427 if ((read == 2) && prev->read)
1428 return 2;
Peter Zijlstra7531e2f2008-08-11 09:30:24 +02001429
1430 /*
1431 * We're holding the nest_lock, which serializes this lock's
1432 * nesting behaviour.
1433 */
1434 if (nest)
1435 return 2;
1436
Peter Zijlstra8e182572007-07-19 01:48:54 -07001437 return print_deadlock_bug(curr, prev, next);
1438 }
1439 return 1;
1440}
1441
1442/*
1443 * There was a chain-cache miss, and we are about to add a new dependency
1444 * to a previous lock. We recursively validate the following rules:
1445 *
1446 * - would the adding of the <prev> -> <next> dependency create a
1447 * circular dependency in the graph? [== circular deadlock]
1448 *
1449 * - does the new prev->next dependency connect any hardirq-safe lock
1450 * (in the full backwards-subgraph starting at <prev>) with any
1451 * hardirq-unsafe lock (in the full forwards-subgraph starting at
1452 * <next>)? [== illegal lock inversion with hardirq contexts]
1453 *
1454 * - does the new prev->next dependency connect any softirq-safe lock
1455 * (in the full backwards-subgraph starting at <prev>) with any
1456 * softirq-unsafe lock (in the full forwards-subgraph starting at
1457 * <next>)? [== illegal lock inversion with softirq contexts]
1458 *
1459 * any of these scenarios could lead to a deadlock.
1460 *
1461 * Then if all the validations pass, we add the forwards and backwards
1462 * dependency.
1463 */
1464static int
1465check_prev_add(struct task_struct *curr, struct held_lock *prev,
1466 struct held_lock *next, int distance)
1467{
1468 struct lock_list *entry;
1469 int ret;
1470
1471 /*
1472 * Prove that the new <prev> -> <next> dependency would not
1473 * create a circular dependency in the graph. (We do this by
1474 * forward-recursing into the graph starting at <next>, and
1475 * checking whether we can reach <prev>.)
1476 *
1477 * We are using global variables to control the recursion, to
1478 * keep the stackframe size of the recursive functions low:
1479 */
1480 check_source = next;
1481 check_target = prev;
Dave Jonesf82b2172008-08-11 09:30:23 +02001482 if (!(check_noncircular(hlock_class(next), 0)))
Peter Zijlstra8e182572007-07-19 01:48:54 -07001483 return print_circular_bug_tail();
1484
1485 if (!check_prev_add_irq(curr, prev, next))
1486 return 0;
1487
1488 /*
1489 * For recursive read-locks we do all the dependency checks,
1490 * but we dont store read-triggered dependencies (only
1491 * write-triggered dependencies). This ensures that only the
1492 * write-side dependencies matter, and that if for example a
1493 * write-lock never takes any other locks, then the reads are
1494 * equivalent to a NOP.
1495 */
1496 if (next->read == 2 || prev->read == 2)
1497 return 1;
1498 /*
1499 * Is the <prev> -> <next> dependency already present?
1500 *
1501 * (this may occur even though this is a new chain: consider
1502 * e.g. the L1 -> L2 -> L3 -> L4 and the L5 -> L1 -> L2 -> L3
1503 * chains - the second one will be new, but L1 already has
1504 * L2 added to its dependency list, due to the first chain.)
1505 */
Dave Jonesf82b2172008-08-11 09:30:23 +02001506 list_for_each_entry(entry, &hlock_class(prev)->locks_after, entry) {
1507 if (entry->class == hlock_class(next)) {
Peter Zijlstra8e182572007-07-19 01:48:54 -07001508 if (distance == 1)
1509 entry->distance = 1;
1510 return 2;
1511 }
1512 }
1513
1514 /*
1515 * Ok, all validations passed, add the new lock
1516 * to the previous lock's dependency list:
1517 */
Dave Jonesf82b2172008-08-11 09:30:23 +02001518 ret = add_lock_to_list(hlock_class(prev), hlock_class(next),
1519 &hlock_class(prev)->locks_after,
1520 next->acquire_ip, distance);
Peter Zijlstra8e182572007-07-19 01:48:54 -07001521
1522 if (!ret)
1523 return 0;
1524
Dave Jonesf82b2172008-08-11 09:30:23 +02001525 ret = add_lock_to_list(hlock_class(next), hlock_class(prev),
1526 &hlock_class(next)->locks_before,
1527 next->acquire_ip, distance);
Peter Zijlstra8e182572007-07-19 01:48:54 -07001528 if (!ret)
1529 return 0;
1530
1531 /*
1532 * Debugging printouts:
1533 */
Dave Jonesf82b2172008-08-11 09:30:23 +02001534 if (verbose(hlock_class(prev)) || verbose(hlock_class(next))) {
Peter Zijlstra8e182572007-07-19 01:48:54 -07001535 graph_unlock();
1536 printk("\n new dependency: ");
Dave Jonesf82b2172008-08-11 09:30:23 +02001537 print_lock_name(hlock_class(prev));
Peter Zijlstra8e182572007-07-19 01:48:54 -07001538 printk(" => ");
Dave Jonesf82b2172008-08-11 09:30:23 +02001539 print_lock_name(hlock_class(next));
Peter Zijlstra8e182572007-07-19 01:48:54 -07001540 printk("\n");
1541 dump_stack();
1542 return graph_lock();
1543 }
1544 return 1;
1545}
1546
1547/*
1548 * Add the dependency to all directly-previous locks that are 'relevant'.
1549 * The ones that are relevant are (in increasing distance from curr):
1550 * all consecutive trylock entries and the final non-trylock entry - or
1551 * the end of this context's lock-chain - whichever comes first.
1552 */
1553static int
1554check_prevs_add(struct task_struct *curr, struct held_lock *next)
1555{
1556 int depth = curr->lockdep_depth;
1557 struct held_lock *hlock;
1558
1559 /*
1560 * Debugging checks.
1561 *
1562 * Depth must not be zero for a non-head lock:
1563 */
1564 if (!depth)
1565 goto out_bug;
1566 /*
1567 * At least two relevant locks must exist for this
1568 * to be a head:
1569 */
1570 if (curr->held_locks[depth].irq_context !=
1571 curr->held_locks[depth-1].irq_context)
1572 goto out_bug;
1573
1574 for (;;) {
1575 int distance = curr->lockdep_depth - depth + 1;
1576 hlock = curr->held_locks + depth-1;
1577 /*
1578 * Only non-recursive-read entries get new dependencies
1579 * added:
1580 */
1581 if (hlock->read != 2) {
1582 if (!check_prev_add(curr, hlock, next, distance))
1583 return 0;
1584 /*
1585 * Stop after the first non-trylock entry,
1586 * as non-trylock entries have added their
1587 * own direct dependencies already, so this
1588 * lock is connected to them indirectly:
1589 */
1590 if (!hlock->trylock)
1591 break;
1592 }
1593 depth--;
1594 /*
1595 * End of lock-stack?
1596 */
1597 if (!depth)
1598 break;
1599 /*
1600 * Stop the search if we cross into another context:
1601 */
1602 if (curr->held_locks[depth].irq_context !=
1603 curr->held_locks[depth-1].irq_context)
1604 break;
1605 }
1606 return 1;
1607out_bug:
1608 if (!debug_locks_off_graph_unlock())
1609 return 0;
1610
1611 WARN_ON(1);
1612
1613 return 0;
1614}
1615
1616unsigned long nr_lock_chains;
Huang, Ying443cd502008-06-20 16:39:21 +08001617struct lock_chain lock_chains[MAX_LOCKDEP_CHAINS];
Huang, Yingcd1a28e2008-06-23 11:20:54 +08001618int nr_chain_hlocks;
Huang, Ying443cd502008-06-20 16:39:21 +08001619static u16 chain_hlocks[MAX_LOCKDEP_CHAIN_HLOCKS];
1620
1621struct lock_class *lock_chain_get_class(struct lock_chain *chain, int i)
1622{
1623 return lock_classes + chain_hlocks[chain->base + i];
1624}
Peter Zijlstra8e182572007-07-19 01:48:54 -07001625
1626/*
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001627 * Look up a dependency chain. If the key is not present yet then
Jarek Poplawski9e860d02007-05-08 00:30:12 -07001628 * add it and return 1 - in this case the new dependency chain is
1629 * validated. If the key is already hashed, return 0.
1630 * (On return with 1 graph_lock is held.)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001631 */
Huang, Ying443cd502008-06-20 16:39:21 +08001632static inline int lookup_chain_cache(struct task_struct *curr,
1633 struct held_lock *hlock,
1634 u64 chain_key)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001635{
Dave Jonesf82b2172008-08-11 09:30:23 +02001636 struct lock_class *class = hlock_class(hlock);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001637 struct list_head *hash_head = chainhashentry(chain_key);
1638 struct lock_chain *chain;
Huang, Ying443cd502008-06-20 16:39:21 +08001639 struct held_lock *hlock_curr, *hlock_next;
Huang, Yingcd1a28e2008-06-23 11:20:54 +08001640 int i, j, n, cn;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001641
Jarek Poplawski381a2292007-02-10 01:44:58 -08001642 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
1643 return 0;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001644 /*
1645 * We can walk it lock-free, because entries only get added
1646 * to the hash:
1647 */
1648 list_for_each_entry(chain, hash_head, entry) {
1649 if (chain->chain_key == chain_key) {
1650cache_hit:
1651 debug_atomic_inc(&chain_lookup_hits);
Ingo Molnar81fc6852006-12-13 00:34:40 -08001652 if (very_verbose(class))
Andrew Morton755cd902006-12-29 16:49:14 -08001653 printk("\nhash chain already cached, key: "
1654 "%016Lx tail class: [%p] %s\n",
1655 (unsigned long long)chain_key,
1656 class->key, class->name);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001657 return 0;
1658 }
1659 }
Ingo Molnar81fc6852006-12-13 00:34:40 -08001660 if (very_verbose(class))
Andrew Morton755cd902006-12-29 16:49:14 -08001661 printk("\nnew hash chain, key: %016Lx tail class: [%p] %s\n",
1662 (unsigned long long)chain_key, class->key, class->name);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001663 /*
1664 * Allocate a new chain entry from the static array, and add
1665 * it to the hash:
1666 */
Ingo Molnar74c383f2006-12-13 00:34:43 -08001667 if (!graph_lock())
1668 return 0;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001669 /*
1670 * We have to walk the chain again locked - to avoid duplicates:
1671 */
1672 list_for_each_entry(chain, hash_head, entry) {
1673 if (chain->chain_key == chain_key) {
Ingo Molnar74c383f2006-12-13 00:34:43 -08001674 graph_unlock();
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001675 goto cache_hit;
1676 }
1677 }
1678 if (unlikely(nr_lock_chains >= MAX_LOCKDEP_CHAINS)) {
Ingo Molnar74c383f2006-12-13 00:34:43 -08001679 if (!debug_locks_off_graph_unlock())
1680 return 0;
1681
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001682 printk("BUG: MAX_LOCKDEP_CHAINS too low!\n");
1683 printk("turning off the locking correctness validator.\n");
1684 return 0;
1685 }
1686 chain = lock_chains + nr_lock_chains++;
1687 chain->chain_key = chain_key;
Huang, Ying443cd502008-06-20 16:39:21 +08001688 chain->irq_context = hlock->irq_context;
1689 /* Find the first held_lock of current chain */
1690 hlock_next = hlock;
1691 for (i = curr->lockdep_depth - 1; i >= 0; i--) {
1692 hlock_curr = curr->held_locks + i;
1693 if (hlock_curr->irq_context != hlock_next->irq_context)
1694 break;
1695 hlock_next = hlock;
1696 }
1697 i++;
1698 chain->depth = curr->lockdep_depth + 1 - i;
Huang, Yingcd1a28e2008-06-23 11:20:54 +08001699 cn = nr_chain_hlocks;
1700 while (cn + chain->depth <= MAX_LOCKDEP_CHAIN_HLOCKS) {
1701 n = cmpxchg(&nr_chain_hlocks, cn, cn + chain->depth);
1702 if (n == cn)
1703 break;
1704 cn = n;
1705 }
1706 if (likely(cn + chain->depth <= MAX_LOCKDEP_CHAIN_HLOCKS)) {
1707 chain->base = cn;
Huang, Ying443cd502008-06-20 16:39:21 +08001708 for (j = 0; j < chain->depth - 1; j++, i++) {
Dave Jonesf82b2172008-08-11 09:30:23 +02001709 int lock_id = curr->held_locks[i].class_idx - 1;
Huang, Ying443cd502008-06-20 16:39:21 +08001710 chain_hlocks[chain->base + j] = lock_id;
1711 }
1712 chain_hlocks[chain->base + j] = class - lock_classes;
1713 }
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001714 list_add_tail_rcu(&chain->entry, hash_head);
1715 debug_atomic_inc(&chain_lookup_misses);
Peter Zijlstra8e182572007-07-19 01:48:54 -07001716 inc_chains();
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001717
1718 return 1;
1719}
Peter Zijlstra8e182572007-07-19 01:48:54 -07001720
1721static int validate_chain(struct task_struct *curr, struct lockdep_map *lock,
Johannes Berg4e6045f2007-10-18 23:39:55 -07001722 struct held_lock *hlock, int chain_head, u64 chain_key)
Peter Zijlstra8e182572007-07-19 01:48:54 -07001723{
1724 /*
1725 * Trylock needs to maintain the stack of held locks, but it
1726 * does not add new dependencies, because trylock can be done
1727 * in any order.
1728 *
1729 * We look up the chain_key and do the O(N^2) check and update of
1730 * the dependencies only if this is a new dependency chain.
1731 * (If lookup_chain_cache() returns with 1 it acquires
1732 * graph_lock for us)
1733 */
1734 if (!hlock->trylock && (hlock->check == 2) &&
Huang, Ying443cd502008-06-20 16:39:21 +08001735 lookup_chain_cache(curr, hlock, chain_key)) {
Peter Zijlstra8e182572007-07-19 01:48:54 -07001736 /*
1737 * Check whether last held lock:
1738 *
1739 * - is irq-safe, if this lock is irq-unsafe
1740 * - is softirq-safe, if this lock is hardirq-unsafe
1741 *
1742 * And check whether the new lock's dependency graph
1743 * could lead back to the previous lock.
1744 *
1745 * any of these scenarios could lead to a deadlock. If
1746 * All validations
1747 */
1748 int ret = check_deadlock(curr, hlock, lock, hlock->read);
1749
1750 if (!ret)
1751 return 0;
1752 /*
1753 * Mark recursive read, as we jump over it when
1754 * building dependencies (just like we jump over
1755 * trylock entries):
1756 */
1757 if (ret == 2)
1758 hlock->read = 2;
1759 /*
1760 * Add dependency only if this lock is not the head
1761 * of the chain, and if it's not a secondary read-lock:
1762 */
1763 if (!chain_head && ret != 2)
1764 if (!check_prevs_add(curr, hlock))
1765 return 0;
1766 graph_unlock();
1767 } else
1768 /* after lookup_chain_cache(): */
1769 if (unlikely(!debug_locks))
1770 return 0;
1771
1772 return 1;
1773}
1774#else
1775static inline int validate_chain(struct task_struct *curr,
1776 struct lockdep_map *lock, struct held_lock *hlock,
Gregory Haskins3aa416b2007-10-11 22:11:11 +02001777 int chain_head, u64 chain_key)
Peter Zijlstra8e182572007-07-19 01:48:54 -07001778{
1779 return 1;
1780}
Peter Zijlstraca58abc2007-07-19 01:48:53 -07001781#endif
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001782
1783/*
1784 * We are building curr_chain_key incrementally, so double-check
1785 * it from scratch, to make sure that it's done correctly:
1786 */
Steven Rostedt1d09daa2008-05-12 21:20:55 +02001787static void check_chain_key(struct task_struct *curr)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001788{
1789#ifdef CONFIG_DEBUG_LOCKDEP
1790 struct held_lock *hlock, *prev_hlock = NULL;
1791 unsigned int i, id;
1792 u64 chain_key = 0;
1793
1794 for (i = 0; i < curr->lockdep_depth; i++) {
1795 hlock = curr->held_locks + i;
1796 if (chain_key != hlock->prev_chain_key) {
1797 debug_locks_off();
Arjan van de Ven2df8b1d2008-07-30 12:43:11 -07001798 WARN(1, "hm#1, depth: %u [%u], %016Lx != %016Lx\n",
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001799 curr->lockdep_depth, i,
1800 (unsigned long long)chain_key,
1801 (unsigned long long)hlock->prev_chain_key);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001802 return;
1803 }
Dave Jonesf82b2172008-08-11 09:30:23 +02001804 id = hlock->class_idx - 1;
Jarek Poplawski381a2292007-02-10 01:44:58 -08001805 if (DEBUG_LOCKS_WARN_ON(id >= MAX_LOCKDEP_KEYS))
1806 return;
1807
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001808 if (prev_hlock && (prev_hlock->irq_context !=
1809 hlock->irq_context))
1810 chain_key = 0;
1811 chain_key = iterate_chain_key(chain_key, id);
1812 prev_hlock = hlock;
1813 }
1814 if (chain_key != curr->curr_chain_key) {
1815 debug_locks_off();
Arjan van de Ven2df8b1d2008-07-30 12:43:11 -07001816 WARN(1, "hm#2, depth: %u [%u], %016Lx != %016Lx\n",
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001817 curr->lockdep_depth, i,
1818 (unsigned long long)chain_key,
1819 (unsigned long long)curr->curr_chain_key);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001820 }
1821#endif
1822}
1823
Peter Zijlstra8e182572007-07-19 01:48:54 -07001824static int
1825print_usage_bug(struct task_struct *curr, struct held_lock *this,
1826 enum lock_usage_bit prev_bit, enum lock_usage_bit new_bit)
1827{
1828 if (!debug_locks_off_graph_unlock() || debug_locks_silent)
1829 return 0;
1830
1831 printk("\n=================================\n");
1832 printk( "[ INFO: inconsistent lock state ]\n");
1833 print_kernel_version();
1834 printk( "---------------------------------\n");
1835
1836 printk("inconsistent {%s} -> {%s} usage.\n",
1837 usage_str[prev_bit], usage_str[new_bit]);
1838
1839 printk("%s/%d [HC%u[%lu]:SC%u[%lu]:HE%u:SE%u] takes:\n",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07001840 curr->comm, task_pid_nr(curr),
Peter Zijlstra8e182572007-07-19 01:48:54 -07001841 trace_hardirq_context(curr), hardirq_count() >> HARDIRQ_SHIFT,
1842 trace_softirq_context(curr), softirq_count() >> SOFTIRQ_SHIFT,
1843 trace_hardirqs_enabled(curr),
1844 trace_softirqs_enabled(curr));
1845 print_lock(this);
1846
1847 printk("{%s} state was registered at:\n", usage_str[prev_bit]);
Dave Jonesf82b2172008-08-11 09:30:23 +02001848 print_stack_trace(hlock_class(this)->usage_traces + prev_bit, 1);
Peter Zijlstra8e182572007-07-19 01:48:54 -07001849
1850 print_irqtrace_events(curr);
1851 printk("\nother info that might help us debug this:\n");
1852 lockdep_print_held_locks(curr);
1853
1854 printk("\nstack backtrace:\n");
1855 dump_stack();
1856
1857 return 0;
1858}
1859
1860/*
1861 * Print out an error if an invalid bit is set:
1862 */
1863static inline int
1864valid_state(struct task_struct *curr, struct held_lock *this,
1865 enum lock_usage_bit new_bit, enum lock_usage_bit bad_bit)
1866{
Dave Jonesf82b2172008-08-11 09:30:23 +02001867 if (unlikely(hlock_class(this)->usage_mask & (1 << bad_bit)))
Peter Zijlstra8e182572007-07-19 01:48:54 -07001868 return print_usage_bug(curr, this, bad_bit, new_bit);
1869 return 1;
1870}
1871
1872static int mark_lock(struct task_struct *curr, struct held_lock *this,
1873 enum lock_usage_bit new_bit);
1874
Steven Rostedt81d68a92008-05-12 21:20:42 +02001875#if defined(CONFIG_TRACE_IRQFLAGS) && defined(CONFIG_PROVE_LOCKING)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001876
1877/*
1878 * print irq inversion bug:
1879 */
1880static int
1881print_irq_inversion_bug(struct task_struct *curr, struct lock_class *other,
1882 struct held_lock *this, int forwards,
1883 const char *irqclass)
1884{
Ingo Molnar74c383f2006-12-13 00:34:43 -08001885 if (!debug_locks_off_graph_unlock() || debug_locks_silent)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001886 return 0;
1887
1888 printk("\n=========================================================\n");
1889 printk( "[ INFO: possible irq lock inversion dependency detected ]\n");
Dave Jones99de0552006-09-29 02:00:10 -07001890 print_kernel_version();
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001891 printk( "---------------------------------------------------------\n");
1892 printk("%s/%d just changed the state of lock:\n",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07001893 curr->comm, task_pid_nr(curr));
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001894 print_lock(this);
1895 if (forwards)
Peter Zijlstra26575e22009-03-04 14:53:24 +01001896 printk("but this lock took another, %s-unsafe lock in the past:\n", irqclass);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001897 else
Peter Zijlstra26575e22009-03-04 14:53:24 +01001898 printk("but this lock was taken by another, %s-safe lock in the past:\n", irqclass);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001899 print_lock_name(other);
1900 printk("\n\nand interrupts could create inverse lock ordering between them.\n\n");
1901
1902 printk("\nother info that might help us debug this:\n");
1903 lockdep_print_held_locks(curr);
1904
1905 printk("\nthe first lock's dependencies:\n");
Dave Jonesf82b2172008-08-11 09:30:23 +02001906 print_lock_dependencies(hlock_class(this), 0);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001907
1908 printk("\nthe second lock's dependencies:\n");
1909 print_lock_dependencies(other, 0);
1910
1911 printk("\nstack backtrace:\n");
1912 dump_stack();
1913
1914 return 0;
1915}
1916
1917/*
1918 * Prove that in the forwards-direction subgraph starting at <this>
1919 * there is no lock matching <mask>:
1920 */
1921static int
1922check_usage_forwards(struct task_struct *curr, struct held_lock *this,
1923 enum lock_usage_bit bit, const char *irqclass)
1924{
1925 int ret;
1926
1927 find_usage_bit = bit;
1928 /* fills in <forwards_match> */
Dave Jonesf82b2172008-08-11 09:30:23 +02001929 ret = find_usage_forwards(hlock_class(this), 0);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001930 if (!ret || ret == 1)
1931 return ret;
1932
1933 return print_irq_inversion_bug(curr, forwards_match, this, 1, irqclass);
1934}
1935
1936/*
1937 * Prove that in the backwards-direction subgraph starting at <this>
1938 * there is no lock matching <mask>:
1939 */
1940static int
1941check_usage_backwards(struct task_struct *curr, struct held_lock *this,
1942 enum lock_usage_bit bit, const char *irqclass)
1943{
1944 int ret;
1945
1946 find_usage_bit = bit;
1947 /* fills in <backwards_match> */
Dave Jonesf82b2172008-08-11 09:30:23 +02001948 ret = find_usage_backwards(hlock_class(this), 0);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001949 if (!ret || ret == 1)
1950 return ret;
1951
1952 return print_irq_inversion_bug(curr, backwards_match, this, 0, irqclass);
1953}
1954
Ingo Molnar3117df02006-12-13 00:34:43 -08001955void print_irqtrace_events(struct task_struct *curr)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001956{
1957 printk("irq event stamp: %u\n", curr->irq_events);
1958 printk("hardirqs last enabled at (%u): ", curr->hardirq_enable_event);
1959 print_ip_sym(curr->hardirq_enable_ip);
1960 printk("hardirqs last disabled at (%u): ", curr->hardirq_disable_event);
1961 print_ip_sym(curr->hardirq_disable_ip);
1962 printk("softirqs last enabled at (%u): ", curr->softirq_enable_event);
1963 print_ip_sym(curr->softirq_enable_ip);
1964 printk("softirqs last disabled at (%u): ", curr->softirq_disable_event);
1965 print_ip_sym(curr->softirq_disable_ip);
1966}
1967
Peter Zijlstracd953022009-01-22 16:38:21 +01001968static int HARDIRQ_verbose(struct lock_class *class)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001969{
Peter Zijlstra8e182572007-07-19 01:48:54 -07001970#if HARDIRQ_VERBOSE
1971 return class_filter(class);
1972#endif
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001973 return 0;
1974}
1975
Peter Zijlstracd953022009-01-22 16:38:21 +01001976static int SOFTIRQ_verbose(struct lock_class *class)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001977{
Peter Zijlstra8e182572007-07-19 01:48:54 -07001978#if SOFTIRQ_VERBOSE
1979 return class_filter(class);
1980#endif
1981 return 0;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001982}
1983
Peter Zijlstracd953022009-01-22 16:38:21 +01001984static int RECLAIM_FS_verbose(struct lock_class *class)
Nick Piggincf40bd12009-01-21 08:12:39 +01001985{
1986#if RECLAIM_VERBOSE
1987 return class_filter(class);
1988#endif
1989 return 0;
1990}
1991
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001992#define STRICT_READ_CHECKS 1
1993
Peter Zijlstracd953022009-01-22 16:38:21 +01001994static int (*state_verbose_f[])(struct lock_class *class) = {
1995#define LOCKDEP_STATE(__STATE) \
1996 __STATE##_verbose,
1997#include "lockdep_states.h"
1998#undef LOCKDEP_STATE
1999};
2000
2001static inline int state_verbose(enum lock_usage_bit bit,
2002 struct lock_class *class)
2003{
2004 return state_verbose_f[bit >> 2](class);
2005}
2006
Peter Zijlstra42c50d52009-01-22 16:58:16 +01002007typedef int (*check_usage_f)(struct task_struct *, struct held_lock *,
2008 enum lock_usage_bit bit, const char *name);
2009
Peter Zijlstra6a6904d2009-01-22 16:07:44 +01002010static int
Peter Zijlstra1c21f142009-03-04 13:51:13 +01002011mark_lock_irq(struct task_struct *curr, struct held_lock *this,
2012 enum lock_usage_bit new_bit)
Peter Zijlstra6a6904d2009-01-22 16:07:44 +01002013{
Peter Zijlstraf9892092009-01-22 16:09:59 +01002014 int excl_bit = exclusive_bit(new_bit);
Peter Zijlstra9d3651a2009-01-22 17:18:32 +01002015 int read = new_bit & 1;
Peter Zijlstra42c50d52009-01-22 16:58:16 +01002016 int dir = new_bit & 2;
2017
Peter Zijlstra38aa2712009-01-27 14:53:50 +01002018 /*
2019 * mark USED_IN has to look forwards -- to ensure no dependency
2020 * has ENABLED state, which would allow recursion deadlocks.
2021 *
2022 * mark ENABLED has to look backwards -- to ensure no dependee
2023 * has USED_IN state, which, again, would allow recursion deadlocks.
2024 */
Peter Zijlstra42c50d52009-01-22 16:58:16 +01002025 check_usage_f usage = dir ?
2026 check_usage_backwards : check_usage_forwards;
Peter Zijlstraf9892092009-01-22 16:09:59 +01002027
Peter Zijlstra38aa2712009-01-27 14:53:50 +01002028 /*
2029 * Validate that this particular lock does not have conflicting
2030 * usage states.
2031 */
Peter Zijlstra6a6904d2009-01-22 16:07:44 +01002032 if (!valid_state(curr, this, new_bit, excl_bit))
2033 return 0;
Peter Zijlstra9d3651a2009-01-22 17:18:32 +01002034
Peter Zijlstra38aa2712009-01-27 14:53:50 +01002035 /*
2036 * Validate that the lock dependencies don't have conflicting usage
2037 * states.
2038 */
2039 if ((!read || !dir || STRICT_READ_CHECKS) &&
Peter Zijlstra1c21f142009-03-04 13:51:13 +01002040 !usage(curr, this, excl_bit, state_name(new_bit & ~1)))
Peter Zijlstra6a6904d2009-01-22 16:07:44 +01002041 return 0;
Peter Zijlstra780e8202009-01-22 16:51:29 +01002042
Peter Zijlstra38aa2712009-01-27 14:53:50 +01002043 /*
2044 * Check for read in write conflicts
2045 */
2046 if (!read) {
2047 if (!valid_state(curr, this, new_bit, excl_bit + 1))
2048 return 0;
2049
2050 if (STRICT_READ_CHECKS &&
Peter Zijlstra4f367d8a2009-01-22 18:10:42 +01002051 !usage(curr, this, excl_bit + 1,
2052 state_name(new_bit + 1)))
Peter Zijlstra38aa2712009-01-27 14:53:50 +01002053 return 0;
2054 }
Peter Zijlstra780e8202009-01-22 16:51:29 +01002055
Peter Zijlstracd953022009-01-22 16:38:21 +01002056 if (state_verbose(new_bit, hlock_class(this)))
Peter Zijlstra6a6904d2009-01-22 16:07:44 +01002057 return 2;
2058
2059 return 1;
2060}
2061
Nick Piggincf40bd12009-01-21 08:12:39 +01002062enum mark_type {
Peter Zijlstra36bfb9b2009-01-22 14:12:41 +01002063#define LOCKDEP_STATE(__STATE) __STATE,
2064#include "lockdep_states.h"
2065#undef LOCKDEP_STATE
Nick Piggincf40bd12009-01-21 08:12:39 +01002066};
2067
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002068/*
2069 * Mark all held locks with a usage bit:
2070 */
Steven Rostedt1d09daa2008-05-12 21:20:55 +02002071static int
Nick Piggincf40bd12009-01-21 08:12:39 +01002072mark_held_locks(struct task_struct *curr, enum mark_type mark)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002073{
2074 enum lock_usage_bit usage_bit;
2075 struct held_lock *hlock;
2076 int i;
2077
2078 for (i = 0; i < curr->lockdep_depth; i++) {
2079 hlock = curr->held_locks + i;
2080
Peter Zijlstracf2ad4d2009-01-27 13:58:08 +01002081 usage_bit = 2 + (mark << 2); /* ENABLED */
2082 if (hlock->read)
2083 usage_bit += 1; /* READ */
2084
2085 BUG_ON(usage_bit >= LOCK_USAGE_STATES);
Nick Piggincf40bd12009-01-21 08:12:39 +01002086
Jarek Poplawski4ff773bb2007-05-08 00:31:00 -07002087 if (!mark_lock(curr, hlock, usage_bit))
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002088 return 0;
2089 }
2090
2091 return 1;
2092}
2093
2094/*
2095 * Debugging helper: via this flag we know that we are in
2096 * 'early bootup code', and will warn about any invalid irqs-on event:
2097 */
2098static int early_boot_irqs_enabled;
2099
2100void early_boot_irqs_off(void)
2101{
2102 early_boot_irqs_enabled = 0;
2103}
2104
2105void early_boot_irqs_on(void)
2106{
2107 early_boot_irqs_enabled = 1;
2108}
2109
2110/*
2111 * Hardirqs will be enabled:
2112 */
Heiko Carstens6afe40b2008-10-28 11:14:58 +01002113void trace_hardirqs_on_caller(unsigned long ip)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002114{
2115 struct task_struct *curr = current;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002116
Heiko Carstens6afe40b2008-10-28 11:14:58 +01002117 time_hardirqs_on(CALLER_ADDR0, ip);
Steven Rostedt81d68a92008-05-12 21:20:42 +02002118
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002119 if (unlikely(!debug_locks || current->lockdep_recursion))
2120 return;
2121
2122 if (DEBUG_LOCKS_WARN_ON(unlikely(!early_boot_irqs_enabled)))
2123 return;
2124
2125 if (unlikely(curr->hardirqs_enabled)) {
2126 debug_atomic_inc(&redundant_hardirqs_on);
2127 return;
2128 }
2129 /* we'll do an OFF -> ON transition: */
2130 curr->hardirqs_enabled = 1;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002131
2132 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2133 return;
2134 if (DEBUG_LOCKS_WARN_ON(current->hardirq_context))
2135 return;
2136 /*
2137 * We are going to turn hardirqs on, so set the
2138 * usage bit for all held locks:
2139 */
Nick Piggincf40bd12009-01-21 08:12:39 +01002140 if (!mark_held_locks(curr, HARDIRQ))
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002141 return;
2142 /*
2143 * If we have softirqs enabled, then set the usage
2144 * bit for all held locks. (disabled hardirqs prevented
2145 * this bit from being set before)
2146 */
2147 if (curr->softirqs_enabled)
Nick Piggincf40bd12009-01-21 08:12:39 +01002148 if (!mark_held_locks(curr, SOFTIRQ))
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002149 return;
2150
2151 curr->hardirq_enable_ip = ip;
2152 curr->hardirq_enable_event = ++curr->irq_events;
2153 debug_atomic_inc(&hardirqs_on_events);
2154}
Steven Rostedt81d68a92008-05-12 21:20:42 +02002155EXPORT_SYMBOL(trace_hardirqs_on_caller);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002156
Steven Rostedt1d09daa2008-05-12 21:20:55 +02002157void trace_hardirqs_on(void)
Steven Rostedt81d68a92008-05-12 21:20:42 +02002158{
2159 trace_hardirqs_on_caller(CALLER_ADDR0);
2160}
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002161EXPORT_SYMBOL(trace_hardirqs_on);
2162
2163/*
2164 * Hardirqs were disabled:
2165 */
Heiko Carstens6afe40b2008-10-28 11:14:58 +01002166void trace_hardirqs_off_caller(unsigned long ip)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002167{
2168 struct task_struct *curr = current;
2169
Heiko Carstens6afe40b2008-10-28 11:14:58 +01002170 time_hardirqs_off(CALLER_ADDR0, ip);
Steven Rostedt81d68a92008-05-12 21:20:42 +02002171
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002172 if (unlikely(!debug_locks || current->lockdep_recursion))
2173 return;
2174
2175 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2176 return;
2177
2178 if (curr->hardirqs_enabled) {
2179 /*
2180 * We have done an ON -> OFF transition:
2181 */
2182 curr->hardirqs_enabled = 0;
Heiko Carstens6afe40b2008-10-28 11:14:58 +01002183 curr->hardirq_disable_ip = ip;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002184 curr->hardirq_disable_event = ++curr->irq_events;
2185 debug_atomic_inc(&hardirqs_off_events);
2186 } else
2187 debug_atomic_inc(&redundant_hardirqs_off);
2188}
Steven Rostedt81d68a92008-05-12 21:20:42 +02002189EXPORT_SYMBOL(trace_hardirqs_off_caller);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002190
Steven Rostedt1d09daa2008-05-12 21:20:55 +02002191void trace_hardirqs_off(void)
Steven Rostedt81d68a92008-05-12 21:20:42 +02002192{
2193 trace_hardirqs_off_caller(CALLER_ADDR0);
2194}
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002195EXPORT_SYMBOL(trace_hardirqs_off);
2196
2197/*
2198 * Softirqs will be enabled:
2199 */
2200void trace_softirqs_on(unsigned long ip)
2201{
2202 struct task_struct *curr = current;
2203
2204 if (unlikely(!debug_locks))
2205 return;
2206
2207 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2208 return;
2209
2210 if (curr->softirqs_enabled) {
2211 debug_atomic_inc(&redundant_softirqs_on);
2212 return;
2213 }
2214
2215 /*
2216 * We'll do an OFF -> ON transition:
2217 */
2218 curr->softirqs_enabled = 1;
2219 curr->softirq_enable_ip = ip;
2220 curr->softirq_enable_event = ++curr->irq_events;
2221 debug_atomic_inc(&softirqs_on_events);
2222 /*
2223 * We are going to turn softirqs on, so set the
2224 * usage bit for all held locks, if hardirqs are
2225 * enabled too:
2226 */
2227 if (curr->hardirqs_enabled)
Nick Piggincf40bd12009-01-21 08:12:39 +01002228 mark_held_locks(curr, SOFTIRQ);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002229}
2230
2231/*
2232 * Softirqs were disabled:
2233 */
2234void trace_softirqs_off(unsigned long ip)
2235{
2236 struct task_struct *curr = current;
2237
2238 if (unlikely(!debug_locks))
2239 return;
2240
2241 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2242 return;
2243
2244 if (curr->softirqs_enabled) {
2245 /*
2246 * We have done an ON -> OFF transition:
2247 */
2248 curr->softirqs_enabled = 0;
2249 curr->softirq_disable_ip = ip;
2250 curr->softirq_disable_event = ++curr->irq_events;
2251 debug_atomic_inc(&softirqs_off_events);
2252 DEBUG_LOCKS_WARN_ON(!softirq_count());
2253 } else
2254 debug_atomic_inc(&redundant_softirqs_off);
2255}
2256
Peter Zijlstra2f850182009-03-20 11:13:20 +01002257static void __lockdep_trace_alloc(gfp_t gfp_mask, unsigned long flags)
Nick Piggincf40bd12009-01-21 08:12:39 +01002258{
2259 struct task_struct *curr = current;
2260
2261 if (unlikely(!debug_locks))
2262 return;
2263
2264 /* no reclaim without waiting on it */
2265 if (!(gfp_mask & __GFP_WAIT))
2266 return;
2267
2268 /* this guy won't enter reclaim */
2269 if ((curr->flags & PF_MEMALLOC) && !(gfp_mask & __GFP_NOMEMALLOC))
2270 return;
2271
2272 /* We're only interested __GFP_FS allocations for now */
2273 if (!(gfp_mask & __GFP_FS))
2274 return;
2275
Peter Zijlstra2f850182009-03-20 11:13:20 +01002276 if (DEBUG_LOCKS_WARN_ON(irqs_disabled_flags(flags)))
Nick Piggincf40bd12009-01-21 08:12:39 +01002277 return;
2278
2279 mark_held_locks(curr, RECLAIM_FS);
2280}
2281
Peter Zijlstra2f850182009-03-20 11:13:20 +01002282static void check_flags(unsigned long flags);
2283
2284void lockdep_trace_alloc(gfp_t gfp_mask)
2285{
2286 unsigned long flags;
2287
2288 if (unlikely(current->lockdep_recursion))
2289 return;
2290
2291 raw_local_irq_save(flags);
2292 check_flags(flags);
2293 current->lockdep_recursion = 1;
2294 __lockdep_trace_alloc(gfp_mask, flags);
2295 current->lockdep_recursion = 0;
2296 raw_local_irq_restore(flags);
2297}
2298
Peter Zijlstra8e182572007-07-19 01:48:54 -07002299static int mark_irqflags(struct task_struct *curr, struct held_lock *hlock)
2300{
2301 /*
2302 * If non-trylock use in a hardirq or softirq context, then
2303 * mark the lock as used in these contexts:
2304 */
2305 if (!hlock->trylock) {
2306 if (hlock->read) {
2307 if (curr->hardirq_context)
2308 if (!mark_lock(curr, hlock,
2309 LOCK_USED_IN_HARDIRQ_READ))
2310 return 0;
2311 if (curr->softirq_context)
2312 if (!mark_lock(curr, hlock,
2313 LOCK_USED_IN_SOFTIRQ_READ))
2314 return 0;
2315 } else {
2316 if (curr->hardirq_context)
2317 if (!mark_lock(curr, hlock, LOCK_USED_IN_HARDIRQ))
2318 return 0;
2319 if (curr->softirq_context)
2320 if (!mark_lock(curr, hlock, LOCK_USED_IN_SOFTIRQ))
2321 return 0;
2322 }
2323 }
2324 if (!hlock->hardirqs_off) {
2325 if (hlock->read) {
2326 if (!mark_lock(curr, hlock,
Peter Zijlstra4fc95e82009-01-22 13:10:52 +01002327 LOCK_ENABLED_HARDIRQ_READ))
Peter Zijlstra8e182572007-07-19 01:48:54 -07002328 return 0;
2329 if (curr->softirqs_enabled)
2330 if (!mark_lock(curr, hlock,
Peter Zijlstra4fc95e82009-01-22 13:10:52 +01002331 LOCK_ENABLED_SOFTIRQ_READ))
Peter Zijlstra8e182572007-07-19 01:48:54 -07002332 return 0;
2333 } else {
2334 if (!mark_lock(curr, hlock,
Peter Zijlstra4fc95e82009-01-22 13:10:52 +01002335 LOCK_ENABLED_HARDIRQ))
Peter Zijlstra8e182572007-07-19 01:48:54 -07002336 return 0;
2337 if (curr->softirqs_enabled)
2338 if (!mark_lock(curr, hlock,
Peter Zijlstra4fc95e82009-01-22 13:10:52 +01002339 LOCK_ENABLED_SOFTIRQ))
Peter Zijlstra8e182572007-07-19 01:48:54 -07002340 return 0;
2341 }
2342 }
2343
Nick Piggincf40bd12009-01-21 08:12:39 +01002344 /*
2345 * We reuse the irq context infrastructure more broadly as a general
2346 * context checking code. This tests GFP_FS recursion (a lock taken
2347 * during reclaim for a GFP_FS allocation is held over a GFP_FS
2348 * allocation).
2349 */
2350 if (!hlock->trylock && (curr->lockdep_reclaim_gfp & __GFP_FS)) {
2351 if (hlock->read) {
2352 if (!mark_lock(curr, hlock, LOCK_USED_IN_RECLAIM_FS_READ))
2353 return 0;
2354 } else {
2355 if (!mark_lock(curr, hlock, LOCK_USED_IN_RECLAIM_FS))
2356 return 0;
2357 }
2358 }
2359
Peter Zijlstra8e182572007-07-19 01:48:54 -07002360 return 1;
2361}
2362
2363static int separate_irq_context(struct task_struct *curr,
2364 struct held_lock *hlock)
2365{
2366 unsigned int depth = curr->lockdep_depth;
2367
2368 /*
2369 * Keep track of points where we cross into an interrupt context:
2370 */
2371 hlock->irq_context = 2*(curr->hardirq_context ? 1 : 0) +
2372 curr->softirq_context;
2373 if (depth) {
2374 struct held_lock *prev_hlock;
2375
2376 prev_hlock = curr->held_locks + depth-1;
2377 /*
2378 * If we cross into another context, reset the
2379 * hash key (this also prevents the checking and the
2380 * adding of the dependency to 'prev'):
2381 */
2382 if (prev_hlock->irq_context != hlock->irq_context)
2383 return 1;
2384 }
2385 return 0;
2386}
2387
2388#else
2389
2390static inline
2391int mark_lock_irq(struct task_struct *curr, struct held_lock *this,
2392 enum lock_usage_bit new_bit)
2393{
2394 WARN_ON(1);
2395 return 1;
2396}
2397
2398static inline int mark_irqflags(struct task_struct *curr,
2399 struct held_lock *hlock)
2400{
2401 return 1;
2402}
2403
2404static inline int separate_irq_context(struct task_struct *curr,
2405 struct held_lock *hlock)
2406{
2407 return 0;
2408}
2409
Peter Zijlstra868a23a2009-02-15 00:25:21 +01002410void lockdep_trace_alloc(gfp_t gfp_mask)
2411{
2412}
2413
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002414#endif
2415
2416/*
Peter Zijlstra8e182572007-07-19 01:48:54 -07002417 * Mark a lock with a usage bit, and validate the state transition:
2418 */
Steven Rostedt1d09daa2008-05-12 21:20:55 +02002419static int mark_lock(struct task_struct *curr, struct held_lock *this,
Steven Rostedt0764d232008-05-12 21:20:44 +02002420 enum lock_usage_bit new_bit)
Peter Zijlstra8e182572007-07-19 01:48:54 -07002421{
2422 unsigned int new_mask = 1 << new_bit, ret = 1;
2423
2424 /*
2425 * If already set then do not dirty the cacheline,
2426 * nor do any checks:
2427 */
Dave Jonesf82b2172008-08-11 09:30:23 +02002428 if (likely(hlock_class(this)->usage_mask & new_mask))
Peter Zijlstra8e182572007-07-19 01:48:54 -07002429 return 1;
2430
2431 if (!graph_lock())
2432 return 0;
2433 /*
2434 * Make sure we didnt race:
2435 */
Dave Jonesf82b2172008-08-11 09:30:23 +02002436 if (unlikely(hlock_class(this)->usage_mask & new_mask)) {
Peter Zijlstra8e182572007-07-19 01:48:54 -07002437 graph_unlock();
2438 return 1;
2439 }
2440
Dave Jonesf82b2172008-08-11 09:30:23 +02002441 hlock_class(this)->usage_mask |= new_mask;
Peter Zijlstra8e182572007-07-19 01:48:54 -07002442
Dave Jonesf82b2172008-08-11 09:30:23 +02002443 if (!save_trace(hlock_class(this)->usage_traces + new_bit))
Peter Zijlstra8e182572007-07-19 01:48:54 -07002444 return 0;
2445
2446 switch (new_bit) {
Peter Zijlstra53464172009-01-22 14:15:53 +01002447#define LOCKDEP_STATE(__STATE) \
2448 case LOCK_USED_IN_##__STATE: \
2449 case LOCK_USED_IN_##__STATE##_READ: \
2450 case LOCK_ENABLED_##__STATE: \
2451 case LOCK_ENABLED_##__STATE##_READ:
2452#include "lockdep_states.h"
2453#undef LOCKDEP_STATE
Peter Zijlstra8e182572007-07-19 01:48:54 -07002454 ret = mark_lock_irq(curr, this, new_bit);
2455 if (!ret)
2456 return 0;
2457 break;
2458 case LOCK_USED:
Peter Zijlstra8e182572007-07-19 01:48:54 -07002459 debug_atomic_dec(&nr_unused_locks);
2460 break;
2461 default:
2462 if (!debug_locks_off_graph_unlock())
2463 return 0;
2464 WARN_ON(1);
2465 return 0;
2466 }
2467
2468 graph_unlock();
2469
2470 /*
2471 * We must printk outside of the graph_lock:
2472 */
2473 if (ret == 2) {
2474 printk("\nmarked lock as {%s}:\n", usage_str[new_bit]);
2475 print_lock(this);
2476 print_irqtrace_events(curr);
2477 dump_stack();
2478 }
2479
2480 return ret;
2481}
2482
2483/*
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002484 * Initialize a lock instance's lock-class mapping info:
2485 */
2486void lockdep_init_map(struct lockdep_map *lock, const char *name,
Peter Zijlstra4dfbb9d2006-10-11 01:45:14 -04002487 struct lock_class_key *key, int subclass)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002488{
2489 if (unlikely(!debug_locks))
2490 return;
2491
2492 if (DEBUG_LOCKS_WARN_ON(!key))
2493 return;
2494 if (DEBUG_LOCKS_WARN_ON(!name))
2495 return;
2496 /*
2497 * Sanity check, the lock-class key must be persistent:
2498 */
2499 if (!static_obj(key)) {
2500 printk("BUG: key %p not in .data!\n", key);
2501 DEBUG_LOCKS_WARN_ON(1);
2502 return;
2503 }
2504 lock->name = name;
2505 lock->key = key;
Ingo Molnard6d897c2006-07-10 04:44:04 -07002506 lock->class_cache = NULL;
Peter Zijlstra96645672007-07-19 01:49:00 -07002507#ifdef CONFIG_LOCK_STAT
2508 lock->cpu = raw_smp_processor_id();
2509#endif
Peter Zijlstra4dfbb9d2006-10-11 01:45:14 -04002510 if (subclass)
2511 register_lock_class(lock, subclass, 1);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002512}
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002513EXPORT_SYMBOL_GPL(lockdep_init_map);
2514
2515/*
2516 * This gets called for every mutex_lock*()/spin_lock*() operation.
2517 * We maintain the dependency maps and validate the locking attempt:
2518 */
2519static int __lock_acquire(struct lockdep_map *lock, unsigned int subclass,
2520 int trylock, int read, int check, int hardirqs_off,
Peter Zijlstra7531e2f2008-08-11 09:30:24 +02002521 struct lockdep_map *nest_lock, unsigned long ip)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002522{
2523 struct task_struct *curr = current;
Ingo Molnard6d897c2006-07-10 04:44:04 -07002524 struct lock_class *class = NULL;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002525 struct held_lock *hlock;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002526 unsigned int depth, id;
2527 int chain_head = 0;
2528 u64 chain_key;
2529
Peter Zijlstraf20786f2007-07-19 01:48:56 -07002530 if (!prove_locking)
2531 check = 1;
2532
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002533 if (unlikely(!debug_locks))
2534 return 0;
2535
2536 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2537 return 0;
2538
2539 if (unlikely(subclass >= MAX_LOCKDEP_SUBCLASSES)) {
2540 debug_locks_off();
2541 printk("BUG: MAX_LOCKDEP_SUBCLASSES too low!\n");
2542 printk("turning off the locking correctness validator.\n");
2543 return 0;
2544 }
2545
Ingo Molnard6d897c2006-07-10 04:44:04 -07002546 if (!subclass)
2547 class = lock->class_cache;
2548 /*
2549 * Not cached yet or subclass?
2550 */
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002551 if (unlikely(!class)) {
Peter Zijlstra4dfbb9d2006-10-11 01:45:14 -04002552 class = register_lock_class(lock, subclass, 0);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002553 if (!class)
2554 return 0;
2555 }
2556 debug_atomic_inc((atomic_t *)&class->ops);
2557 if (very_verbose(class)) {
2558 printk("\nacquire class [%p] %s", class->key, class->name);
2559 if (class->name_version > 1)
2560 printk("#%d", class->name_version);
2561 printk("\n");
2562 dump_stack();
2563 }
2564
2565 /*
2566 * Add the lock to the list of currently held locks.
2567 * (we dont increase the depth just yet, up until the
2568 * dependency checks are done)
2569 */
2570 depth = curr->lockdep_depth;
2571 if (DEBUG_LOCKS_WARN_ON(depth >= MAX_LOCK_DEPTH))
2572 return 0;
2573
2574 hlock = curr->held_locks + depth;
Dave Jonesf82b2172008-08-11 09:30:23 +02002575 if (DEBUG_LOCKS_WARN_ON(!class))
2576 return 0;
2577 hlock->class_idx = class - lock_classes + 1;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002578 hlock->acquire_ip = ip;
2579 hlock->instance = lock;
Peter Zijlstra7531e2f2008-08-11 09:30:24 +02002580 hlock->nest_lock = nest_lock;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002581 hlock->trylock = trylock;
2582 hlock->read = read;
2583 hlock->check = check;
Dmitry Baryshkov6951b122008-08-18 04:26:37 +04002584 hlock->hardirqs_off = !!hardirqs_off;
Peter Zijlstraf20786f2007-07-19 01:48:56 -07002585#ifdef CONFIG_LOCK_STAT
2586 hlock->waittime_stamp = 0;
2587 hlock->holdtime_stamp = sched_clock();
2588#endif
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002589
Peter Zijlstra8e182572007-07-19 01:48:54 -07002590 if (check == 2 && !mark_irqflags(curr, hlock))
2591 return 0;
2592
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002593 /* mark it as used: */
Jarek Poplawski4ff773bb2007-05-08 00:31:00 -07002594 if (!mark_lock(curr, hlock, LOCK_USED))
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002595 return 0;
Peter Zijlstra8e182572007-07-19 01:48:54 -07002596
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002597 /*
Gautham R Shenoy17aacfb92007-10-28 20:47:01 +01002598 * Calculate the chain hash: it's the combined hash of all the
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002599 * lock keys along the dependency chain. We save the hash value
2600 * at every step so that we can get the current hash easily
2601 * after unlock. The chain hash is then used to cache dependency
2602 * results.
2603 *
2604 * The 'key ID' is what is the most compact key value to drive
2605 * the hash, not class->key.
2606 */
2607 id = class - lock_classes;
2608 if (DEBUG_LOCKS_WARN_ON(id >= MAX_LOCKDEP_KEYS))
2609 return 0;
2610
2611 chain_key = curr->curr_chain_key;
2612 if (!depth) {
2613 if (DEBUG_LOCKS_WARN_ON(chain_key != 0))
2614 return 0;
2615 chain_head = 1;
2616 }
2617
2618 hlock->prev_chain_key = chain_key;
Peter Zijlstra8e182572007-07-19 01:48:54 -07002619 if (separate_irq_context(curr, hlock)) {
2620 chain_key = 0;
2621 chain_head = 1;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002622 }
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002623 chain_key = iterate_chain_key(chain_key, id);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002624
Gregory Haskins3aa416b2007-10-11 22:11:11 +02002625 if (!validate_chain(curr, lock, hlock, chain_head, chain_key))
Peter Zijlstra8e182572007-07-19 01:48:54 -07002626 return 0;
Jarek Poplawski381a2292007-02-10 01:44:58 -08002627
Gregory Haskins3aa416b2007-10-11 22:11:11 +02002628 curr->curr_chain_key = chain_key;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002629 curr->lockdep_depth++;
2630 check_chain_key(curr);
Jarek Poplawski60e114d2007-02-20 13:58:00 -08002631#ifdef CONFIG_DEBUG_LOCKDEP
2632 if (unlikely(!debug_locks))
2633 return 0;
2634#endif
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002635 if (unlikely(curr->lockdep_depth >= MAX_LOCK_DEPTH)) {
2636 debug_locks_off();
2637 printk("BUG: MAX_LOCK_DEPTH too low!\n");
2638 printk("turning off the locking correctness validator.\n");
2639 return 0;
2640 }
Jarek Poplawski381a2292007-02-10 01:44:58 -08002641
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002642 if (unlikely(curr->lockdep_depth > max_lockdep_depth))
2643 max_lockdep_depth = curr->lockdep_depth;
2644
2645 return 1;
2646}
2647
2648static int
2649print_unlock_inbalance_bug(struct task_struct *curr, struct lockdep_map *lock,
2650 unsigned long ip)
2651{
2652 if (!debug_locks_off())
2653 return 0;
2654 if (debug_locks_silent)
2655 return 0;
2656
2657 printk("\n=====================================\n");
2658 printk( "[ BUG: bad unlock balance detected! ]\n");
2659 printk( "-------------------------------------\n");
2660 printk("%s/%d is trying to release lock (",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07002661 curr->comm, task_pid_nr(curr));
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002662 print_lockdep_cache(lock);
2663 printk(") at:\n");
2664 print_ip_sym(ip);
2665 printk("but there are no more locks to release!\n");
2666 printk("\nother info that might help us debug this:\n");
2667 lockdep_print_held_locks(curr);
2668
2669 printk("\nstack backtrace:\n");
2670 dump_stack();
2671
2672 return 0;
2673}
2674
2675/*
2676 * Common debugging checks for both nested and non-nested unlock:
2677 */
2678static int check_unlock(struct task_struct *curr, struct lockdep_map *lock,
2679 unsigned long ip)
2680{
2681 if (unlikely(!debug_locks))
2682 return 0;
2683 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2684 return 0;
2685
2686 if (curr->lockdep_depth <= 0)
2687 return print_unlock_inbalance_bug(curr, lock, ip);
2688
2689 return 1;
2690}
2691
Peter Zijlstra64aa3482008-08-11 09:30:21 +02002692static int
Peter Zijlstra00ef9f72008-12-04 09:00:17 +01002693__lock_set_class(struct lockdep_map *lock, const char *name,
2694 struct lock_class_key *key, unsigned int subclass,
2695 unsigned long ip)
Peter Zijlstra64aa3482008-08-11 09:30:21 +02002696{
2697 struct task_struct *curr = current;
2698 struct held_lock *hlock, *prev_hlock;
2699 struct lock_class *class;
2700 unsigned int depth;
2701 int i;
2702
2703 depth = curr->lockdep_depth;
2704 if (DEBUG_LOCKS_WARN_ON(!depth))
2705 return 0;
2706
2707 prev_hlock = NULL;
2708 for (i = depth-1; i >= 0; i--) {
2709 hlock = curr->held_locks + i;
2710 /*
2711 * We must not cross into another context:
2712 */
2713 if (prev_hlock && prev_hlock->irq_context != hlock->irq_context)
2714 break;
2715 if (hlock->instance == lock)
2716 goto found_it;
2717 prev_hlock = hlock;
2718 }
2719 return print_unlock_inbalance_bug(curr, lock, ip);
2720
2721found_it:
Peter Zijlstra00ef9f72008-12-04 09:00:17 +01002722 lockdep_init_map(lock, name, key, 0);
Peter Zijlstra64aa3482008-08-11 09:30:21 +02002723 class = register_lock_class(lock, subclass, 0);
Dave Jonesf82b2172008-08-11 09:30:23 +02002724 hlock->class_idx = class - lock_classes + 1;
Peter Zijlstra64aa3482008-08-11 09:30:21 +02002725
2726 curr->lockdep_depth = i;
2727 curr->curr_chain_key = hlock->prev_chain_key;
2728
2729 for (; i < depth; i++) {
2730 hlock = curr->held_locks + i;
2731 if (!__lock_acquire(hlock->instance,
Dave Jonesf82b2172008-08-11 09:30:23 +02002732 hlock_class(hlock)->subclass, hlock->trylock,
Peter Zijlstra64aa3482008-08-11 09:30:21 +02002733 hlock->read, hlock->check, hlock->hardirqs_off,
Peter Zijlstra7531e2f2008-08-11 09:30:24 +02002734 hlock->nest_lock, hlock->acquire_ip))
Peter Zijlstra64aa3482008-08-11 09:30:21 +02002735 return 0;
2736 }
2737
2738 if (DEBUG_LOCKS_WARN_ON(curr->lockdep_depth != depth))
2739 return 0;
2740 return 1;
2741}
2742
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002743/*
2744 * Remove the lock to the list of currently held locks in a
2745 * potentially non-nested (out of order) manner. This is a
2746 * relatively rare operation, as all the unlock APIs default
2747 * to nested mode (which uses lock_release()):
2748 */
2749static int
2750lock_release_non_nested(struct task_struct *curr,
2751 struct lockdep_map *lock, unsigned long ip)
2752{
2753 struct held_lock *hlock, *prev_hlock;
2754 unsigned int depth;
2755 int i;
2756
2757 /*
2758 * Check whether the lock exists in the current stack
2759 * of held locks:
2760 */
2761 depth = curr->lockdep_depth;
2762 if (DEBUG_LOCKS_WARN_ON(!depth))
2763 return 0;
2764
2765 prev_hlock = NULL;
2766 for (i = depth-1; i >= 0; i--) {
2767 hlock = curr->held_locks + i;
2768 /*
2769 * We must not cross into another context:
2770 */
2771 if (prev_hlock && prev_hlock->irq_context != hlock->irq_context)
2772 break;
2773 if (hlock->instance == lock)
2774 goto found_it;
2775 prev_hlock = hlock;
2776 }
2777 return print_unlock_inbalance_bug(curr, lock, ip);
2778
2779found_it:
Peter Zijlstraf20786f2007-07-19 01:48:56 -07002780 lock_release_holdtime(hlock);
2781
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002782 /*
2783 * We have the right lock to unlock, 'hlock' points to it.
2784 * Now we remove it from the stack, and add back the other
2785 * entries (if any), recalculating the hash along the way:
2786 */
2787 curr->lockdep_depth = i;
2788 curr->curr_chain_key = hlock->prev_chain_key;
2789
2790 for (i++; i < depth; i++) {
2791 hlock = curr->held_locks + i;
2792 if (!__lock_acquire(hlock->instance,
Dave Jonesf82b2172008-08-11 09:30:23 +02002793 hlock_class(hlock)->subclass, hlock->trylock,
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002794 hlock->read, hlock->check, hlock->hardirqs_off,
Peter Zijlstra7531e2f2008-08-11 09:30:24 +02002795 hlock->nest_lock, hlock->acquire_ip))
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002796 return 0;
2797 }
2798
2799 if (DEBUG_LOCKS_WARN_ON(curr->lockdep_depth != depth - 1))
2800 return 0;
2801 return 1;
2802}
2803
2804/*
2805 * Remove the lock to the list of currently held locks - this gets
2806 * called on mutex_unlock()/spin_unlock*() (or on a failed
2807 * mutex_lock_interruptible()). This is done for unlocks that nest
2808 * perfectly. (i.e. the current top of the lock-stack is unlocked)
2809 */
2810static int lock_release_nested(struct task_struct *curr,
2811 struct lockdep_map *lock, unsigned long ip)
2812{
2813 struct held_lock *hlock;
2814 unsigned int depth;
2815
2816 /*
2817 * Pop off the top of the lock stack:
2818 */
2819 depth = curr->lockdep_depth - 1;
2820 hlock = curr->held_locks + depth;
2821
2822 /*
2823 * Is the unlock non-nested:
2824 */
2825 if (hlock->instance != lock)
2826 return lock_release_non_nested(curr, lock, ip);
2827 curr->lockdep_depth--;
2828
2829 if (DEBUG_LOCKS_WARN_ON(!depth && (hlock->prev_chain_key != 0)))
2830 return 0;
2831
2832 curr->curr_chain_key = hlock->prev_chain_key;
2833
Peter Zijlstraf20786f2007-07-19 01:48:56 -07002834 lock_release_holdtime(hlock);
2835
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002836#ifdef CONFIG_DEBUG_LOCKDEP
2837 hlock->prev_chain_key = 0;
Dave Jonesf82b2172008-08-11 09:30:23 +02002838 hlock->class_idx = 0;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002839 hlock->acquire_ip = 0;
2840 hlock->irq_context = 0;
2841#endif
2842 return 1;
2843}
2844
2845/*
2846 * Remove the lock to the list of currently held locks - this gets
2847 * called on mutex_unlock()/spin_unlock*() (or on a failed
2848 * mutex_lock_interruptible()). This is done for unlocks that nest
2849 * perfectly. (i.e. the current top of the lock-stack is unlocked)
2850 */
2851static void
2852__lock_release(struct lockdep_map *lock, int nested, unsigned long ip)
2853{
2854 struct task_struct *curr = current;
2855
2856 if (!check_unlock(curr, lock, ip))
2857 return;
2858
2859 if (nested) {
2860 if (!lock_release_nested(curr, lock, ip))
2861 return;
2862 } else {
2863 if (!lock_release_non_nested(curr, lock, ip))
2864 return;
2865 }
2866
2867 check_chain_key(curr);
2868}
2869
2870/*
2871 * Check whether we follow the irq-flags state precisely:
2872 */
Steven Rostedt1d09daa2008-05-12 21:20:55 +02002873static void check_flags(unsigned long flags)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002874{
Ingo Molnar992860e2008-07-14 10:28:38 +02002875#if defined(CONFIG_PROVE_LOCKING) && defined(CONFIG_DEBUG_LOCKDEP) && \
2876 defined(CONFIG_TRACE_IRQFLAGS)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002877 if (!debug_locks)
2878 return;
2879
Ingo Molnar5f9fa8a62007-12-07 19:02:47 +01002880 if (irqs_disabled_flags(flags)) {
2881 if (DEBUG_LOCKS_WARN_ON(current->hardirqs_enabled)) {
2882 printk("possible reason: unannotated irqs-off.\n");
2883 }
2884 } else {
2885 if (DEBUG_LOCKS_WARN_ON(!current->hardirqs_enabled)) {
2886 printk("possible reason: unannotated irqs-on.\n");
2887 }
2888 }
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002889
2890 /*
2891 * We dont accurately track softirq state in e.g.
2892 * hardirq contexts (such as on 4KSTACKS), so only
2893 * check if not in hardirq contexts:
2894 */
2895 if (!hardirq_count()) {
2896 if (softirq_count())
2897 DEBUG_LOCKS_WARN_ON(current->softirqs_enabled);
2898 else
2899 DEBUG_LOCKS_WARN_ON(!current->softirqs_enabled);
2900 }
2901
2902 if (!debug_locks)
2903 print_irqtrace_events(current);
2904#endif
2905}
2906
Peter Zijlstra00ef9f72008-12-04 09:00:17 +01002907void lock_set_class(struct lockdep_map *lock, const char *name,
2908 struct lock_class_key *key, unsigned int subclass,
2909 unsigned long ip)
Peter Zijlstra64aa3482008-08-11 09:30:21 +02002910{
2911 unsigned long flags;
2912
2913 if (unlikely(current->lockdep_recursion))
2914 return;
2915
2916 raw_local_irq_save(flags);
2917 current->lockdep_recursion = 1;
2918 check_flags(flags);
Peter Zijlstra00ef9f72008-12-04 09:00:17 +01002919 if (__lock_set_class(lock, name, key, subclass, ip))
Peter Zijlstra64aa3482008-08-11 09:30:21 +02002920 check_chain_key(current);
2921 current->lockdep_recursion = 0;
2922 raw_local_irq_restore(flags);
2923}
Peter Zijlstra00ef9f72008-12-04 09:00:17 +01002924EXPORT_SYMBOL_GPL(lock_set_class);
Peter Zijlstra64aa3482008-08-11 09:30:21 +02002925
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002926/*
2927 * We are not always called with irqs disabled - do that here,
2928 * and also avoid lockdep recursion:
2929 */
Steven Rostedt1d09daa2008-05-12 21:20:55 +02002930void lock_acquire(struct lockdep_map *lock, unsigned int subclass,
Peter Zijlstra7531e2f2008-08-11 09:30:24 +02002931 int trylock, int read, int check,
2932 struct lockdep_map *nest_lock, unsigned long ip)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002933{
2934 unsigned long flags;
2935
2936 if (unlikely(current->lockdep_recursion))
2937 return;
2938
2939 raw_local_irq_save(flags);
2940 check_flags(flags);
2941
2942 current->lockdep_recursion = 1;
2943 __lock_acquire(lock, subclass, trylock, read, check,
Peter Zijlstra7531e2f2008-08-11 09:30:24 +02002944 irqs_disabled_flags(flags), nest_lock, ip);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002945 current->lockdep_recursion = 0;
2946 raw_local_irq_restore(flags);
2947}
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002948EXPORT_SYMBOL_GPL(lock_acquire);
2949
Steven Rostedt1d09daa2008-05-12 21:20:55 +02002950void lock_release(struct lockdep_map *lock, int nested,
Steven Rostedt0764d232008-05-12 21:20:44 +02002951 unsigned long ip)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002952{
2953 unsigned long flags;
2954
2955 if (unlikely(current->lockdep_recursion))
2956 return;
2957
2958 raw_local_irq_save(flags);
2959 check_flags(flags);
2960 current->lockdep_recursion = 1;
2961 __lock_release(lock, nested, ip);
2962 current->lockdep_recursion = 0;
2963 raw_local_irq_restore(flags);
2964}
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002965EXPORT_SYMBOL_GPL(lock_release);
2966
Nick Piggincf40bd12009-01-21 08:12:39 +01002967void lockdep_set_current_reclaim_state(gfp_t gfp_mask)
2968{
2969 current->lockdep_reclaim_gfp = gfp_mask;
2970}
2971
2972void lockdep_clear_current_reclaim_state(void)
2973{
2974 current->lockdep_reclaim_gfp = 0;
2975}
2976
Peter Zijlstraf20786f2007-07-19 01:48:56 -07002977#ifdef CONFIG_LOCK_STAT
2978static int
2979print_lock_contention_bug(struct task_struct *curr, struct lockdep_map *lock,
2980 unsigned long ip)
2981{
2982 if (!debug_locks_off())
2983 return 0;
2984 if (debug_locks_silent)
2985 return 0;
2986
2987 printk("\n=================================\n");
2988 printk( "[ BUG: bad contention detected! ]\n");
2989 printk( "---------------------------------\n");
2990 printk("%s/%d is trying to contend lock (",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07002991 curr->comm, task_pid_nr(curr));
Peter Zijlstraf20786f2007-07-19 01:48:56 -07002992 print_lockdep_cache(lock);
2993 printk(") at:\n");
2994 print_ip_sym(ip);
2995 printk("but there are no locks held!\n");
2996 printk("\nother info that might help us debug this:\n");
2997 lockdep_print_held_locks(curr);
2998
2999 printk("\nstack backtrace:\n");
3000 dump_stack();
3001
3002 return 0;
3003}
3004
3005static void
3006__lock_contended(struct lockdep_map *lock, unsigned long ip)
3007{
3008 struct task_struct *curr = current;
3009 struct held_lock *hlock, *prev_hlock;
3010 struct lock_class_stats *stats;
3011 unsigned int depth;
Peter Zijlstrac7e78cf2008-10-16 23:17:09 +02003012 int i, contention_point, contending_point;
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003013
3014 depth = curr->lockdep_depth;
3015 if (DEBUG_LOCKS_WARN_ON(!depth))
3016 return;
3017
3018 prev_hlock = NULL;
3019 for (i = depth-1; i >= 0; i--) {
3020 hlock = curr->held_locks + i;
3021 /*
3022 * We must not cross into another context:
3023 */
3024 if (prev_hlock && prev_hlock->irq_context != hlock->irq_context)
3025 break;
3026 if (hlock->instance == lock)
3027 goto found_it;
3028 prev_hlock = hlock;
3029 }
3030 print_lock_contention_bug(curr, lock, ip);
3031 return;
3032
3033found_it:
3034 hlock->waittime_stamp = sched_clock();
3035
Peter Zijlstrac7e78cf2008-10-16 23:17:09 +02003036 contention_point = lock_point(hlock_class(hlock)->contention_point, ip);
3037 contending_point = lock_point(hlock_class(hlock)->contending_point,
3038 lock->ip);
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003039
Dave Jonesf82b2172008-08-11 09:30:23 +02003040 stats = get_lock_stats(hlock_class(hlock));
Peter Zijlstrac7e78cf2008-10-16 23:17:09 +02003041 if (contention_point < LOCKSTAT_POINTS)
3042 stats->contention_point[contention_point]++;
3043 if (contending_point < LOCKSTAT_POINTS)
3044 stats->contending_point[contending_point]++;
Peter Zijlstra96645672007-07-19 01:49:00 -07003045 if (lock->cpu != smp_processor_id())
3046 stats->bounces[bounce_contended + !!hlock->read]++;
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003047 put_lock_stats(stats);
3048}
3049
3050static void
Peter Zijlstrac7e78cf2008-10-16 23:17:09 +02003051__lock_acquired(struct lockdep_map *lock, unsigned long ip)
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003052{
3053 struct task_struct *curr = current;
3054 struct held_lock *hlock, *prev_hlock;
3055 struct lock_class_stats *stats;
3056 unsigned int depth;
3057 u64 now;
Peter Zijlstra96645672007-07-19 01:49:00 -07003058 s64 waittime = 0;
3059 int i, cpu;
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003060
3061 depth = curr->lockdep_depth;
3062 if (DEBUG_LOCKS_WARN_ON(!depth))
3063 return;
3064
3065 prev_hlock = NULL;
3066 for (i = depth-1; i >= 0; i--) {
3067 hlock = curr->held_locks + i;
3068 /*
3069 * We must not cross into another context:
3070 */
3071 if (prev_hlock && prev_hlock->irq_context != hlock->irq_context)
3072 break;
3073 if (hlock->instance == lock)
3074 goto found_it;
3075 prev_hlock = hlock;
3076 }
3077 print_lock_contention_bug(curr, lock, _RET_IP_);
3078 return;
3079
3080found_it:
Peter Zijlstra96645672007-07-19 01:49:00 -07003081 cpu = smp_processor_id();
3082 if (hlock->waittime_stamp) {
3083 now = sched_clock();
3084 waittime = now - hlock->waittime_stamp;
3085 hlock->holdtime_stamp = now;
3086 }
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003087
Dave Jonesf82b2172008-08-11 09:30:23 +02003088 stats = get_lock_stats(hlock_class(hlock));
Peter Zijlstra96645672007-07-19 01:49:00 -07003089 if (waittime) {
3090 if (hlock->read)
3091 lock_time_inc(&stats->read_waittime, waittime);
3092 else
3093 lock_time_inc(&stats->write_waittime, waittime);
3094 }
3095 if (lock->cpu != cpu)
3096 stats->bounces[bounce_acquired + !!hlock->read]++;
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003097 put_lock_stats(stats);
Peter Zijlstra96645672007-07-19 01:49:00 -07003098
3099 lock->cpu = cpu;
Peter Zijlstrac7e78cf2008-10-16 23:17:09 +02003100 lock->ip = ip;
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003101}
3102
3103void lock_contended(struct lockdep_map *lock, unsigned long ip)
3104{
3105 unsigned long flags;
3106
3107 if (unlikely(!lock_stat))
3108 return;
3109
3110 if (unlikely(current->lockdep_recursion))
3111 return;
3112
3113 raw_local_irq_save(flags);
3114 check_flags(flags);
3115 current->lockdep_recursion = 1;
3116 __lock_contended(lock, ip);
3117 current->lockdep_recursion = 0;
3118 raw_local_irq_restore(flags);
3119}
3120EXPORT_SYMBOL_GPL(lock_contended);
3121
Peter Zijlstrac7e78cf2008-10-16 23:17:09 +02003122void lock_acquired(struct lockdep_map *lock, unsigned long ip)
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003123{
3124 unsigned long flags;
3125
3126 if (unlikely(!lock_stat))
3127 return;
3128
3129 if (unlikely(current->lockdep_recursion))
3130 return;
3131
3132 raw_local_irq_save(flags);
3133 check_flags(flags);
3134 current->lockdep_recursion = 1;
Peter Zijlstrac7e78cf2008-10-16 23:17:09 +02003135 __lock_acquired(lock, ip);
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003136 current->lockdep_recursion = 0;
3137 raw_local_irq_restore(flags);
3138}
3139EXPORT_SYMBOL_GPL(lock_acquired);
3140#endif
3141
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003142/*
3143 * Used by the testsuite, sanitize the validator state
3144 * after a simulated failure:
3145 */
3146
3147void lockdep_reset(void)
3148{
3149 unsigned long flags;
Ingo Molnar23d95a02006-12-13 00:34:40 -08003150 int i;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003151
3152 raw_local_irq_save(flags);
3153 current->curr_chain_key = 0;
3154 current->lockdep_depth = 0;
3155 current->lockdep_recursion = 0;
3156 memset(current->held_locks, 0, MAX_LOCK_DEPTH*sizeof(struct held_lock));
3157 nr_hardirq_chains = 0;
3158 nr_softirq_chains = 0;
3159 nr_process_chains = 0;
3160 debug_locks = 1;
Ingo Molnar23d95a02006-12-13 00:34:40 -08003161 for (i = 0; i < CHAINHASH_SIZE; i++)
3162 INIT_LIST_HEAD(chainhash_table + i);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003163 raw_local_irq_restore(flags);
3164}
3165
3166static void zap_class(struct lock_class *class)
3167{
3168 int i;
3169
3170 /*
3171 * Remove all dependencies this lock is
3172 * involved in:
3173 */
3174 for (i = 0; i < nr_list_entries; i++) {
3175 if (list_entries[i].class == class)
3176 list_del_rcu(&list_entries[i].entry);
3177 }
3178 /*
3179 * Unhash the class and remove it from the all_lock_classes list:
3180 */
3181 list_del_rcu(&class->hash_entry);
3182 list_del_rcu(&class->lock_entry);
3183
Rabin Vincent8bfe0292008-08-11 09:30:26 +02003184 class->key = NULL;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003185}
3186
Arjan van de Venfabe8742008-01-24 07:00:45 +01003187static inline int within(const void *addr, void *start, unsigned long size)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003188{
3189 return addr >= start && addr < start + size;
3190}
3191
3192void lockdep_free_key_range(void *start, unsigned long size)
3193{
3194 struct lock_class *class, *next;
3195 struct list_head *head;
3196 unsigned long flags;
3197 int i;
Nick Piggin5a26db52008-01-16 09:51:58 +01003198 int locked;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003199
3200 raw_local_irq_save(flags);
Nick Piggin5a26db52008-01-16 09:51:58 +01003201 locked = graph_lock();
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003202
3203 /*
3204 * Unhash all classes that were created by this module:
3205 */
3206 for (i = 0; i < CLASSHASH_SIZE; i++) {
3207 head = classhash_table + i;
3208 if (list_empty(head))
3209 continue;
Arjan van de Venfabe8742008-01-24 07:00:45 +01003210 list_for_each_entry_safe(class, next, head, hash_entry) {
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003211 if (within(class->key, start, size))
3212 zap_class(class);
Arjan van de Venfabe8742008-01-24 07:00:45 +01003213 else if (within(class->name, start, size))
3214 zap_class(class);
3215 }
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003216 }
3217
Nick Piggin5a26db52008-01-16 09:51:58 +01003218 if (locked)
3219 graph_unlock();
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003220 raw_local_irq_restore(flags);
3221}
3222
3223void lockdep_reset_lock(struct lockdep_map *lock)
3224{
Ingo Molnard6d897c2006-07-10 04:44:04 -07003225 struct lock_class *class, *next;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003226 struct list_head *head;
3227 unsigned long flags;
3228 int i, j;
Nick Piggin5a26db52008-01-16 09:51:58 +01003229 int locked;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003230
3231 raw_local_irq_save(flags);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003232
3233 /*
Ingo Molnard6d897c2006-07-10 04:44:04 -07003234 * Remove all classes this lock might have:
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003235 */
Ingo Molnard6d897c2006-07-10 04:44:04 -07003236 for (j = 0; j < MAX_LOCKDEP_SUBCLASSES; j++) {
3237 /*
3238 * If the class exists we look it up and zap it:
3239 */
3240 class = look_up_lock_class(lock, j);
3241 if (class)
3242 zap_class(class);
3243 }
3244 /*
3245 * Debug check: in the end all mapped classes should
3246 * be gone.
3247 */
Nick Piggin5a26db52008-01-16 09:51:58 +01003248 locked = graph_lock();
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003249 for (i = 0; i < CLASSHASH_SIZE; i++) {
3250 head = classhash_table + i;
3251 if (list_empty(head))
3252 continue;
3253 list_for_each_entry_safe(class, next, head, hash_entry) {
Ingo Molnard6d897c2006-07-10 04:44:04 -07003254 if (unlikely(class == lock->class_cache)) {
Ingo Molnar74c383f2006-12-13 00:34:43 -08003255 if (debug_locks_off_graph_unlock())
3256 WARN_ON(1);
Ingo Molnard6d897c2006-07-10 04:44:04 -07003257 goto out_restore;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003258 }
3259 }
3260 }
Nick Piggin5a26db52008-01-16 09:51:58 +01003261 if (locked)
3262 graph_unlock();
Ingo Molnard6d897c2006-07-10 04:44:04 -07003263
3264out_restore:
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003265 raw_local_irq_restore(flags);
3266}
3267
Sam Ravnborg14999932007-02-28 20:12:31 -08003268void lockdep_init(void)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003269{
3270 int i;
3271
3272 /*
3273 * Some architectures have their own start_kernel()
3274 * code which calls lockdep_init(), while we also
3275 * call lockdep_init() from the start_kernel() itself,
3276 * and we want to initialize the hashes only once:
3277 */
3278 if (lockdep_initialized)
3279 return;
3280
3281 for (i = 0; i < CLASSHASH_SIZE; i++)
3282 INIT_LIST_HEAD(classhash_table + i);
3283
3284 for (i = 0; i < CHAINHASH_SIZE; i++)
3285 INIT_LIST_HEAD(chainhash_table + i);
3286
3287 lockdep_initialized = 1;
3288}
3289
3290void __init lockdep_info(void)
3291{
3292 printk("Lock dependency validator: Copyright (c) 2006 Red Hat, Inc., Ingo Molnar\n");
3293
Li Zefanb0788ca2008-11-21 15:57:32 +08003294 printk("... MAX_LOCKDEP_SUBCLASSES: %lu\n", MAX_LOCKDEP_SUBCLASSES);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003295 printk("... MAX_LOCK_DEPTH: %lu\n", MAX_LOCK_DEPTH);
3296 printk("... MAX_LOCKDEP_KEYS: %lu\n", MAX_LOCKDEP_KEYS);
Li Zefanb0788ca2008-11-21 15:57:32 +08003297 printk("... CLASSHASH_SIZE: %lu\n", CLASSHASH_SIZE);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003298 printk("... MAX_LOCKDEP_ENTRIES: %lu\n", MAX_LOCKDEP_ENTRIES);
3299 printk("... MAX_LOCKDEP_CHAINS: %lu\n", MAX_LOCKDEP_CHAINS);
3300 printk("... CHAINHASH_SIZE: %lu\n", CHAINHASH_SIZE);
3301
3302 printk(" memory used by lock dependency info: %lu kB\n",
3303 (sizeof(struct lock_class) * MAX_LOCKDEP_KEYS +
3304 sizeof(struct list_head) * CLASSHASH_SIZE +
3305 sizeof(struct lock_list) * MAX_LOCKDEP_ENTRIES +
3306 sizeof(struct lock_chain) * MAX_LOCKDEP_CHAINS +
3307 sizeof(struct list_head) * CHAINHASH_SIZE) / 1024);
3308
3309 printk(" per task-struct memory footprint: %lu bytes\n",
3310 sizeof(struct held_lock) * MAX_LOCK_DEPTH);
3311
3312#ifdef CONFIG_DEBUG_LOCKDEP
Johannes Bergc71063c2007-07-19 01:49:02 -07003313 if (lockdep_init_error) {
3314 printk("WARNING: lockdep init error! Arch code didn't call lockdep_init() early enough?\n");
3315 printk("Call stack leading to lockdep invocation was:\n");
3316 print_stack_trace(&lockdep_init_trace, 0);
3317 }
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003318#endif
3319}
3320
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003321static void
3322print_freed_lock_bug(struct task_struct *curr, const void *mem_from,
Arjan van de Ven55794a42006-07-10 04:44:03 -07003323 const void *mem_to, struct held_lock *hlock)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003324{
3325 if (!debug_locks_off())
3326 return;
3327 if (debug_locks_silent)
3328 return;
3329
3330 printk("\n=========================\n");
3331 printk( "[ BUG: held lock freed! ]\n");
3332 printk( "-------------------------\n");
3333 printk("%s/%d is freeing memory %p-%p, with a lock still held there!\n",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07003334 curr->comm, task_pid_nr(curr), mem_from, mem_to-1);
Arjan van de Ven55794a42006-07-10 04:44:03 -07003335 print_lock(hlock);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003336 lockdep_print_held_locks(curr);
3337
3338 printk("\nstack backtrace:\n");
3339 dump_stack();
3340}
3341
Oleg Nesterov54561782007-12-05 15:46:09 +01003342static inline int not_in_range(const void* mem_from, unsigned long mem_len,
3343 const void* lock_from, unsigned long lock_len)
3344{
3345 return lock_from + lock_len <= mem_from ||
3346 mem_from + mem_len <= lock_from;
3347}
3348
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003349/*
3350 * Called when kernel memory is freed (or unmapped), or if a lock
3351 * is destroyed or reinitialized - this code checks whether there is
3352 * any held lock in the memory range of <from> to <to>:
3353 */
3354void debug_check_no_locks_freed(const void *mem_from, unsigned long mem_len)
3355{
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003356 struct task_struct *curr = current;
3357 struct held_lock *hlock;
3358 unsigned long flags;
3359 int i;
3360
3361 if (unlikely(!debug_locks))
3362 return;
3363
3364 local_irq_save(flags);
3365 for (i = 0; i < curr->lockdep_depth; i++) {
3366 hlock = curr->held_locks + i;
3367
Oleg Nesterov54561782007-12-05 15:46:09 +01003368 if (not_in_range(mem_from, mem_len, hlock->instance,
3369 sizeof(*hlock->instance)))
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003370 continue;
3371
Oleg Nesterov54561782007-12-05 15:46:09 +01003372 print_freed_lock_bug(curr, mem_from, mem_from + mem_len, hlock);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003373 break;
3374 }
3375 local_irq_restore(flags);
3376}
Peter Zijlstraed075362006-12-06 20:35:24 -08003377EXPORT_SYMBOL_GPL(debug_check_no_locks_freed);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003378
3379static void print_held_locks_bug(struct task_struct *curr)
3380{
3381 if (!debug_locks_off())
3382 return;
3383 if (debug_locks_silent)
3384 return;
3385
3386 printk("\n=====================================\n");
3387 printk( "[ BUG: lock held at task exit time! ]\n");
3388 printk( "-------------------------------------\n");
3389 printk("%s/%d is exiting with locks still held!\n",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07003390 curr->comm, task_pid_nr(curr));
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003391 lockdep_print_held_locks(curr);
3392
3393 printk("\nstack backtrace:\n");
3394 dump_stack();
3395}
3396
3397void debug_check_no_locks_held(struct task_struct *task)
3398{
3399 if (unlikely(task->lockdep_depth > 0))
3400 print_held_locks_bug(task);
3401}
3402
3403void debug_show_all_locks(void)
3404{
3405 struct task_struct *g, *p;
3406 int count = 10;
3407 int unlock = 1;
3408
Jarek Poplawski9c35dd72007-03-22 00:11:28 -08003409 if (unlikely(!debug_locks)) {
3410 printk("INFO: lockdep is turned off.\n");
3411 return;
3412 }
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003413 printk("\nShowing all locks held in the system:\n");
3414
3415 /*
3416 * Here we try to get the tasklist_lock as hard as possible,
3417 * if not successful after 2 seconds we ignore it (but keep
3418 * trying). This is to enable a debug printout even if a
3419 * tasklist_lock-holding task deadlocks or crashes.
3420 */
3421retry:
3422 if (!read_trylock(&tasklist_lock)) {
3423 if (count == 10)
3424 printk("hm, tasklist_lock locked, retrying... ");
3425 if (count) {
3426 count--;
3427 printk(" #%d", 10-count);
3428 mdelay(200);
3429 goto retry;
3430 }
3431 printk(" ignoring it.\n");
3432 unlock = 0;
qinghuang feng46fec7a2008-10-28 17:24:28 +08003433 } else {
3434 if (count != 10)
3435 printk(KERN_CONT " locked it.\n");
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003436 }
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003437
3438 do_each_thread(g, p) {
Ingo Molnar85684872007-12-05 15:46:09 +01003439 /*
3440 * It's not reliable to print a task's held locks
3441 * if it's not sleeping (or if it's not the current
3442 * task):
3443 */
3444 if (p->state == TASK_RUNNING && p != current)
3445 continue;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003446 if (p->lockdep_depth)
3447 lockdep_print_held_locks(p);
3448 if (!unlock)
3449 if (read_trylock(&tasklist_lock))
3450 unlock = 1;
3451 } while_each_thread(g, p);
3452
3453 printk("\n");
3454 printk("=============================================\n\n");
3455
3456 if (unlock)
3457 read_unlock(&tasklist_lock);
3458}
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003459EXPORT_SYMBOL_GPL(debug_show_all_locks);
3460
Ingo Molnar82a1fcb2008-01-25 21:08:02 +01003461/*
3462 * Careful: only use this function if you are sure that
3463 * the task cannot run in parallel!
3464 */
3465void __debug_show_held_locks(struct task_struct *task)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003466{
Jarek Poplawski9c35dd72007-03-22 00:11:28 -08003467 if (unlikely(!debug_locks)) {
3468 printk("INFO: lockdep is turned off.\n");
3469 return;
3470 }
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003471 lockdep_print_held_locks(task);
3472}
Ingo Molnar82a1fcb2008-01-25 21:08:02 +01003473EXPORT_SYMBOL_GPL(__debug_show_held_locks);
3474
3475void debug_show_held_locks(struct task_struct *task)
3476{
3477 __debug_show_held_locks(task);
3478}
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003479EXPORT_SYMBOL_GPL(debug_show_held_locks);
Peter Zijlstrab351d162007-10-11 22:11:12 +02003480
3481void lockdep_sys_exit(void)
3482{
3483 struct task_struct *curr = current;
3484
3485 if (unlikely(curr->lockdep_depth)) {
3486 if (!debug_locks_off())
3487 return;
3488 printk("\n================================================\n");
3489 printk( "[ BUG: lock held when returning to user space! ]\n");
3490 printk( "------------------------------------------------\n");
3491 printk("%s/%d is leaving the kernel with locks still held!\n",
3492 curr->comm, curr->pid);
3493 lockdep_print_held_locks(curr);
3494 }
3495}