blob: 7a3ae56b3a7f6625c7d248982a665a92bf66b174 [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>
Ming Leid588e462009-07-16 15:44:29 +020045#include <linux/bitops.h>
Peter Zijlstraaf012962009-07-16 15:44:29 +020046
Ingo Molnarfbb9ce952006-07-03 00:24:50 -070047#include <asm/sections.h>
48
49#include "lockdep_internals.h"
50
Steven Rostedta8d154b2009-04-10 09:36:00 -040051#define CREATE_TRACE_POINTS
Frederic Weisbecker67178762009-11-13 10:06:34 +010052#include <trace/events/lock.h>
Steven Rostedta8d154b2009-04-10 09:36:00 -040053
Peter Zijlstraf20786f2007-07-19 01:48:56 -070054#ifdef CONFIG_PROVE_LOCKING
55int prove_locking = 1;
56module_param(prove_locking, int, 0644);
57#else
58#define prove_locking 0
59#endif
60
61#ifdef CONFIG_LOCK_STAT
62int lock_stat = 1;
63module_param(lock_stat, int, 0644);
64#else
65#define lock_stat 0
66#endif
67
Ingo Molnarfbb9ce952006-07-03 00:24:50 -070068/*
Ingo Molnar74c383f2006-12-13 00:34:43 -080069 * lockdep_lock: protects the lockdep graph, the hashes and the
70 * class/list/hash allocators.
Ingo Molnarfbb9ce952006-07-03 00:24:50 -070071 *
72 * This is one of the rare exceptions where it's justified
73 * to use a raw spinlock - we really dont want the spinlock
Ingo Molnar74c383f2006-12-13 00:34:43 -080074 * code to recurse back into the lockdep code...
Ingo Molnarfbb9ce952006-07-03 00:24:50 -070075 */
Ingo Molnar74c383f2006-12-13 00:34:43 -080076static raw_spinlock_t lockdep_lock = (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED;
77
78static int graph_lock(void)
79{
80 __raw_spin_lock(&lockdep_lock);
81 /*
82 * Make sure that if another CPU detected a bug while
83 * walking the graph we dont change it (while the other
84 * CPU is busy printing out stuff with the graph lock
85 * dropped already)
86 */
87 if (!debug_locks) {
88 __raw_spin_unlock(&lockdep_lock);
89 return 0;
90 }
Steven Rostedtbb065af2008-05-12 21:21:00 +020091 /* prevent any recursions within lockdep from causing deadlocks */
92 current->lockdep_recursion++;
Ingo Molnar74c383f2006-12-13 00:34:43 -080093 return 1;
94}
95
96static inline int graph_unlock(void)
97{
Jarek Poplawski381a2292007-02-10 01:44:58 -080098 if (debug_locks && !__raw_spin_is_locked(&lockdep_lock))
99 return DEBUG_LOCKS_WARN_ON(1);
100
Steven Rostedtbb065af2008-05-12 21:21:00 +0200101 current->lockdep_recursion--;
Ingo Molnar74c383f2006-12-13 00:34:43 -0800102 __raw_spin_unlock(&lockdep_lock);
103 return 0;
104}
105
106/*
107 * Turn lock debugging off and return with 0 if it was off already,
108 * and also release the graph lock:
109 */
110static inline int debug_locks_off_graph_unlock(void)
111{
112 int ret = debug_locks_off();
113
114 __raw_spin_unlock(&lockdep_lock);
115
116 return ret;
117}
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700118
119static int lockdep_initialized;
120
121unsigned long nr_list_entries;
Peter Zijlstraaf012962009-07-16 15:44:29 +0200122static struct lock_list list_entries[MAX_LOCKDEP_ENTRIES];
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700123
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700124/*
125 * All data structures here are protected by the global debug_lock.
126 *
127 * Mutex key structs only get allocated, once during bootup, and never
128 * get freed - this significantly simplifies the debugging code.
129 */
130unsigned long nr_lock_classes;
131static struct lock_class lock_classes[MAX_LOCKDEP_KEYS];
132
Dave Jonesf82b2172008-08-11 09:30:23 +0200133static inline struct lock_class *hlock_class(struct held_lock *hlock)
134{
135 if (!hlock->class_idx) {
136 DEBUG_LOCKS_WARN_ON(1);
137 return NULL;
138 }
139 return lock_classes + hlock->class_idx - 1;
140}
141
Peter Zijlstraf20786f2007-07-19 01:48:56 -0700142#ifdef CONFIG_LOCK_STAT
143static DEFINE_PER_CPU(struct lock_class_stats[MAX_LOCKDEP_KEYS], lock_stats);
144
Peter Zijlstra3365e7792009-10-09 10:12:41 +0200145static inline u64 lockstat_clock(void)
146{
147 return cpu_clock(smp_processor_id());
148}
149
Peter Zijlstrac7e78cf2008-10-16 23:17:09 +0200150static int lock_point(unsigned long points[], unsigned long ip)
Peter Zijlstraf20786f2007-07-19 01:48:56 -0700151{
152 int i;
153
Peter Zijlstrac7e78cf2008-10-16 23:17:09 +0200154 for (i = 0; i < LOCKSTAT_POINTS; i++) {
155 if (points[i] == 0) {
156 points[i] = ip;
Peter Zijlstraf20786f2007-07-19 01:48:56 -0700157 break;
158 }
Peter Zijlstrac7e78cf2008-10-16 23:17:09 +0200159 if (points[i] == ip)
Peter Zijlstraf20786f2007-07-19 01:48:56 -0700160 break;
161 }
162
163 return i;
164}
165
Peter Zijlstra3365e7792009-10-09 10:12:41 +0200166static void lock_time_inc(struct lock_time *lt, u64 time)
Peter Zijlstraf20786f2007-07-19 01:48:56 -0700167{
168 if (time > lt->max)
169 lt->max = time;
170
Frank Rowand109d71c2009-11-19 13:42:06 -0800171 if (time < lt->min || !lt->nr)
Peter Zijlstraf20786f2007-07-19 01:48:56 -0700172 lt->min = time;
173
174 lt->total += time;
175 lt->nr++;
176}
177
Peter Zijlstrac46261d2007-07-19 01:48:57 -0700178static inline void lock_time_add(struct lock_time *src, struct lock_time *dst)
179{
Frank Rowand109d71c2009-11-19 13:42:06 -0800180 if (!src->nr)
181 return;
182
183 if (src->max > dst->max)
184 dst->max = src->max;
185
186 if (src->min < dst->min || !dst->nr)
187 dst->min = src->min;
188
Peter Zijlstrac46261d2007-07-19 01:48:57 -0700189 dst->total += src->total;
190 dst->nr += src->nr;
191}
192
193struct lock_class_stats lock_stats(struct lock_class *class)
194{
195 struct lock_class_stats stats;
196 int cpu, i;
197
198 memset(&stats, 0, sizeof(struct lock_class_stats));
199 for_each_possible_cpu(cpu) {
200 struct lock_class_stats *pcs =
201 &per_cpu(lock_stats, cpu)[class - lock_classes];
202
203 for (i = 0; i < ARRAY_SIZE(stats.contention_point); i++)
204 stats.contention_point[i] += pcs->contention_point[i];
205
Peter Zijlstrac7e78cf2008-10-16 23:17:09 +0200206 for (i = 0; i < ARRAY_SIZE(stats.contending_point); i++)
207 stats.contending_point[i] += pcs->contending_point[i];
208
Peter Zijlstrac46261d2007-07-19 01:48:57 -0700209 lock_time_add(&pcs->read_waittime, &stats.read_waittime);
210 lock_time_add(&pcs->write_waittime, &stats.write_waittime);
211
212 lock_time_add(&pcs->read_holdtime, &stats.read_holdtime);
213 lock_time_add(&pcs->write_holdtime, &stats.write_holdtime);
Peter Zijlstra96645672007-07-19 01:49:00 -0700214
215 for (i = 0; i < ARRAY_SIZE(stats.bounces); i++)
216 stats.bounces[i] += pcs->bounces[i];
Peter Zijlstrac46261d2007-07-19 01:48:57 -0700217 }
218
219 return stats;
220}
221
222void clear_lock_stats(struct lock_class *class)
223{
224 int cpu;
225
226 for_each_possible_cpu(cpu) {
227 struct lock_class_stats *cpu_stats =
228 &per_cpu(lock_stats, cpu)[class - lock_classes];
229
230 memset(cpu_stats, 0, sizeof(struct lock_class_stats));
231 }
232 memset(class->contention_point, 0, sizeof(class->contention_point));
Peter Zijlstrac7e78cf2008-10-16 23:17:09 +0200233 memset(class->contending_point, 0, sizeof(class->contending_point));
Peter Zijlstrac46261d2007-07-19 01:48:57 -0700234}
235
Peter Zijlstraf20786f2007-07-19 01:48:56 -0700236static struct lock_class_stats *get_lock_stats(struct lock_class *class)
237{
238 return &get_cpu_var(lock_stats)[class - lock_classes];
239}
240
241static void put_lock_stats(struct lock_class_stats *stats)
242{
243 put_cpu_var(lock_stats);
244}
245
246static void lock_release_holdtime(struct held_lock *hlock)
247{
248 struct lock_class_stats *stats;
Peter Zijlstra3365e7792009-10-09 10:12:41 +0200249 u64 holdtime;
Peter Zijlstraf20786f2007-07-19 01:48:56 -0700250
251 if (!lock_stat)
252 return;
253
Peter Zijlstra3365e7792009-10-09 10:12:41 +0200254 holdtime = lockstat_clock() - hlock->holdtime_stamp;
Peter Zijlstraf20786f2007-07-19 01:48:56 -0700255
Dave Jonesf82b2172008-08-11 09:30:23 +0200256 stats = get_lock_stats(hlock_class(hlock));
Peter Zijlstraf20786f2007-07-19 01:48:56 -0700257 if (hlock->read)
258 lock_time_inc(&stats->read_holdtime, holdtime);
259 else
260 lock_time_inc(&stats->write_holdtime, holdtime);
261 put_lock_stats(stats);
262}
263#else
264static inline void lock_release_holdtime(struct held_lock *hlock)
265{
266}
267#endif
268
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700269/*
270 * We keep a global list of all lock classes. The list only grows,
271 * never shrinks. The list is only accessed with the lockdep
272 * spinlock lock held.
273 */
274LIST_HEAD(all_lock_classes);
275
276/*
277 * The lockdep classes are in a hash-table as well, for fast lookup:
278 */
279#define CLASSHASH_BITS (MAX_LOCKDEP_KEYS_BITS - 1)
280#define CLASSHASH_SIZE (1UL << CLASSHASH_BITS)
Peter Zijlstra4b32d0a2007-07-19 01:48:59 -0700281#define __classhashfn(key) hash_long((unsigned long)key, CLASSHASH_BITS)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700282#define classhashentry(key) (classhash_table + __classhashfn((key)))
283
284static struct list_head classhash_table[CLASSHASH_SIZE];
285
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700286/*
287 * We put the lock dependency chains into a hash-table as well, to cache
288 * their existence:
289 */
290#define CHAINHASH_BITS (MAX_LOCKDEP_CHAINS_BITS-1)
291#define CHAINHASH_SIZE (1UL << CHAINHASH_BITS)
Peter Zijlstra4b32d0a2007-07-19 01:48:59 -0700292#define __chainhashfn(chain) hash_long(chain, CHAINHASH_BITS)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700293#define chainhashentry(chain) (chainhash_table + __chainhashfn((chain)))
294
295static struct list_head chainhash_table[CHAINHASH_SIZE];
296
297/*
298 * The hash key of the lock dependency chains is a hash itself too:
299 * it's a hash of all locks taken up to that lock, including that lock.
300 * It's a 64-bit hash, because it's important for the keys to be
301 * unique.
302 */
303#define iterate_chain_key(key1, key2) \
Ingo Molnar03cbc352006-09-29 02:01:46 -0700304 (((key1) << MAX_LOCKDEP_KEYS_BITS) ^ \
305 ((key1) >> (64-MAX_LOCKDEP_KEYS_BITS)) ^ \
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700306 (key2))
307
Steven Rostedt1d09daa2008-05-12 21:20:55 +0200308void lockdep_off(void)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700309{
310 current->lockdep_recursion++;
311}
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700312EXPORT_SYMBOL(lockdep_off);
313
Steven Rostedt1d09daa2008-05-12 21:20:55 +0200314void lockdep_on(void)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700315{
316 current->lockdep_recursion--;
317}
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700318EXPORT_SYMBOL(lockdep_on);
319
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700320/*
321 * Debugging switches:
322 */
323
324#define VERBOSE 0
Ingo Molnar33e94e92006-12-13 00:34:41 -0800325#define VERY_VERBOSE 0
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700326
327#if VERBOSE
328# define HARDIRQ_VERBOSE 1
329# define SOFTIRQ_VERBOSE 1
Nick Piggincf40bd12009-01-21 08:12:39 +0100330# define RECLAIM_VERBOSE 1
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700331#else
332# define HARDIRQ_VERBOSE 0
333# define SOFTIRQ_VERBOSE 0
Nick Piggincf40bd12009-01-21 08:12:39 +0100334# define RECLAIM_VERBOSE 0
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700335#endif
336
Nick Piggincf40bd12009-01-21 08:12:39 +0100337#if VERBOSE || HARDIRQ_VERBOSE || SOFTIRQ_VERBOSE || RECLAIM_VERBOSE
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700338/*
339 * Quick filtering for interesting events:
340 */
341static int class_filter(struct lock_class *class)
342{
Andi Kleenf9829cc2006-07-10 04:44:01 -0700343#if 0
344 /* Example */
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700345 if (class->name_version == 1 &&
Andi Kleenf9829cc2006-07-10 04:44:01 -0700346 !strcmp(class->name, "lockname"))
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700347 return 1;
348 if (class->name_version == 1 &&
Andi Kleenf9829cc2006-07-10 04:44:01 -0700349 !strcmp(class->name, "&struct->lockfield"))
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700350 return 1;
Andi Kleenf9829cc2006-07-10 04:44:01 -0700351#endif
Ingo Molnara6640892006-12-13 00:34:39 -0800352 /* Filter everything else. 1 would be to allow everything else */
353 return 0;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700354}
355#endif
356
357static int verbose(struct lock_class *class)
358{
359#if VERBOSE
360 return class_filter(class);
361#endif
362 return 0;
363}
364
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700365/*
366 * Stack-trace: tightly packed array of stack backtrace
Ingo Molnar74c383f2006-12-13 00:34:43 -0800367 * addresses. Protected by the graph_lock.
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700368 */
369unsigned long nr_stack_trace_entries;
370static unsigned long stack_trace[MAX_STACK_TRACE_ENTRIES];
371
372static int save_trace(struct stack_trace *trace)
373{
374 trace->nr_entries = 0;
375 trace->max_entries = MAX_STACK_TRACE_ENTRIES - nr_stack_trace_entries;
376 trace->entries = stack_trace + nr_stack_trace_entries;
377
Andi Kleen5a1b3992006-09-26 10:52:34 +0200378 trace->skip = 3;
Andi Kleen5a1b3992006-09-26 10:52:34 +0200379
Christoph Hellwigab1b6f02007-05-08 00:23:29 -0700380 save_stack_trace(trace);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700381
Peter Zijlstra4f84f432009-07-20 15:27:04 +0200382 /*
383 * Some daft arches put -1 at the end to indicate its a full trace.
384 *
385 * <rant> this is buggy anyway, since it takes a whole extra entry so a
386 * complete trace that maxes out the entries provided will be reported
387 * as incomplete, friggin useless </rant>
388 */
389 if (trace->entries[trace->nr_entries-1] == ULONG_MAX)
390 trace->nr_entries--;
391
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700392 trace->max_entries = trace->nr_entries;
393
394 nr_stack_trace_entries += trace->nr_entries;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700395
Peter Zijlstra4f84f432009-07-20 15:27:04 +0200396 if (nr_stack_trace_entries >= MAX_STACK_TRACE_ENTRIES-1) {
Ingo Molnar74c383f2006-12-13 00:34:43 -0800397 if (!debug_locks_off_graph_unlock())
398 return 0;
399
400 printk("BUG: MAX_STACK_TRACE_ENTRIES too low!\n");
401 printk("turning off the locking correctness validator.\n");
402 dump_stack();
403
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700404 return 0;
405 }
406
407 return 1;
408}
409
410unsigned int nr_hardirq_chains;
411unsigned int nr_softirq_chains;
412unsigned int nr_process_chains;
413unsigned int max_lockdep_depth;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700414
415#ifdef CONFIG_DEBUG_LOCKDEP
416/*
417 * We cannot printk in early bootup code. Not even early_printk()
418 * might work. So we mark any initialization errors and printk
419 * about it later on, in lockdep_info().
420 */
421static int lockdep_init_error;
Johannes Bergc71063c2007-07-19 01:49:02 -0700422static unsigned long lockdep_init_trace_data[20];
423static struct stack_trace lockdep_init_trace = {
424 .max_entries = ARRAY_SIZE(lockdep_init_trace_data),
425 .entries = lockdep_init_trace_data,
426};
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700427
428/*
429 * Various lockdep statistics:
430 */
431atomic_t chain_lookup_hits;
432atomic_t chain_lookup_misses;
433atomic_t hardirqs_on_events;
434atomic_t hardirqs_off_events;
435atomic_t redundant_hardirqs_on;
436atomic_t redundant_hardirqs_off;
437atomic_t softirqs_on_events;
438atomic_t softirqs_off_events;
439atomic_t redundant_softirqs_on;
440atomic_t redundant_softirqs_off;
441atomic_t nr_unused_locks;
442atomic_t nr_cyclic_checks;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700443atomic_t nr_find_usage_forwards_checks;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700444atomic_t nr_find_usage_backwards_checks;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700445#endif
446
447/*
448 * Locking printouts:
449 */
450
Peter Zijlstrafabe9c42009-01-22 14:51:01 +0100451#define __USAGE(__STATE) \
Peter Zijlstrab4b136f2009-01-29 14:50:36 +0100452 [LOCK_USED_IN_##__STATE] = "IN-"__stringify(__STATE)"-W", \
453 [LOCK_ENABLED_##__STATE] = __stringify(__STATE)"-ON-W", \
454 [LOCK_USED_IN_##__STATE##_READ] = "IN-"__stringify(__STATE)"-R",\
455 [LOCK_ENABLED_##__STATE##_READ] = __stringify(__STATE)"-ON-R",
Peter Zijlstrafabe9c42009-01-22 14:51:01 +0100456
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700457static const char *usage_str[] =
458{
Peter Zijlstrafabe9c42009-01-22 14:51:01 +0100459#define LOCKDEP_STATE(__STATE) __USAGE(__STATE)
460#include "lockdep_states.h"
461#undef LOCKDEP_STATE
462 [LOCK_USED] = "INITIAL USE",
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700463};
464
465const char * __get_key_name(struct lockdep_subclass_key *key, char *str)
466{
Alexey Dobriyanffb45122007-05-08 00:28:41 -0700467 return kallsyms_lookup((unsigned long)key, NULL, NULL, NULL, str);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700468}
469
Peter Zijlstra3ff176c2009-01-22 17:40:42 +0100470static inline unsigned long lock_flag(enum lock_usage_bit bit)
471{
472 return 1UL << bit;
473}
474
475static char get_usage_char(struct lock_class *class, enum lock_usage_bit bit)
476{
477 char c = '.';
478
479 if (class->usage_mask & lock_flag(bit + 2))
480 c = '+';
481 if (class->usage_mask & lock_flag(bit)) {
482 c = '-';
483 if (class->usage_mask & lock_flag(bit + 2))
484 c = '?';
485 }
486
487 return c;
488}
489
Peter Zijlstraf510b232009-01-22 17:53:47 +0100490void get_usage_chars(struct lock_class *class, char usage[LOCK_USAGE_CHARS])
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700491{
Peter Zijlstraf510b232009-01-22 17:53:47 +0100492 int i = 0;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700493
Peter Zijlstraf510b232009-01-22 17:53:47 +0100494#define LOCKDEP_STATE(__STATE) \
495 usage[i++] = get_usage_char(class, LOCK_USED_IN_##__STATE); \
496 usage[i++] = get_usage_char(class, LOCK_USED_IN_##__STATE##_READ);
497#include "lockdep_states.h"
498#undef LOCKDEP_STATE
499
500 usage[i] = '\0';
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700501}
502
503static void print_lock_name(struct lock_class *class)
504{
Peter Zijlstraf510b232009-01-22 17:53:47 +0100505 char str[KSYM_NAME_LEN], usage[LOCK_USAGE_CHARS];
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700506 const char *name;
507
Peter Zijlstraf510b232009-01-22 17:53:47 +0100508 get_usage_chars(class, usage);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700509
510 name = class->name;
511 if (!name) {
512 name = __get_key_name(class->key, str);
513 printk(" (%s", name);
514 } else {
515 printk(" (%s", name);
516 if (class->name_version > 1)
517 printk("#%d", class->name_version);
518 if (class->subclass)
519 printk("/%d", class->subclass);
520 }
Peter Zijlstraf510b232009-01-22 17:53:47 +0100521 printk("){%s}", usage);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700522}
523
524static void print_lockdep_cache(struct lockdep_map *lock)
525{
526 const char *name;
Tejun Heo9281ace2007-07-17 04:03:51 -0700527 char str[KSYM_NAME_LEN];
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700528
529 name = lock->name;
530 if (!name)
531 name = __get_key_name(lock->key->subkeys, str);
532
533 printk("%s", name);
534}
535
536static void print_lock(struct held_lock *hlock)
537{
Dave Jonesf82b2172008-08-11 09:30:23 +0200538 print_lock_name(hlock_class(hlock));
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700539 printk(", at: ");
540 print_ip_sym(hlock->acquire_ip);
541}
542
543static void lockdep_print_held_locks(struct task_struct *curr)
544{
545 int i, depth = curr->lockdep_depth;
546
547 if (!depth) {
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -0700548 printk("no locks held by %s/%d.\n", curr->comm, task_pid_nr(curr));
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700549 return;
550 }
551 printk("%d lock%s held by %s/%d:\n",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -0700552 depth, depth > 1 ? "s" : "", curr->comm, task_pid_nr(curr));
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700553
554 for (i = 0; i < depth; i++) {
555 printk(" #%d: ", i);
556 print_lock(curr->held_locks + i);
557 }
558}
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700559
Dave Jones99de0552006-09-29 02:00:10 -0700560static void print_kernel_version(void)
561{
Serge E. Hallyn96b644b2006-10-02 02:18:13 -0700562 printk("%s %.*s\n", init_utsname()->release,
563 (int)strcspn(init_utsname()->version, " "),
564 init_utsname()->version);
Dave Jones99de0552006-09-29 02:00:10 -0700565}
566
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700567static int very_verbose(struct lock_class *class)
568{
569#if VERY_VERBOSE
570 return class_filter(class);
571#endif
572 return 0;
573}
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700574
575/*
576 * Is this the address of a static object:
577 */
578static int static_obj(void *obj)
579{
580 unsigned long start = (unsigned long) &_stext,
581 end = (unsigned long) &_end,
582 addr = (unsigned long) obj;
583#ifdef CONFIG_SMP
584 int i;
585#endif
586
587 /*
588 * static variable?
589 */
590 if ((addr >= start) && (addr < end))
591 return 1;
592
Mike Frysinger2a9ad182009-09-22 16:44:16 -0700593 if (arch_is_kernel_data(addr))
594 return 1;
595
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700596#ifdef CONFIG_SMP
597 /*
598 * percpu var?
599 */
600 for_each_possible_cpu(i) {
601 start = (unsigned long) &__per_cpu_start + per_cpu_offset(i);
Ingo Molnar1ff56832006-11-17 19:57:22 +0100602 end = (unsigned long) &__per_cpu_start + PERCPU_ENOUGH_ROOM
603 + per_cpu_offset(i);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700604
605 if ((addr >= start) && (addr < end))
606 return 1;
607 }
608#endif
609
610 /*
611 * module var?
612 */
613 return is_module_address(addr);
614}
615
616/*
617 * To make lock name printouts unique, we calculate a unique
618 * class->name_version generation counter:
619 */
620static int count_matching_names(struct lock_class *new_class)
621{
622 struct lock_class *class;
623 int count = 0;
624
625 if (!new_class->name)
626 return 0;
627
628 list_for_each_entry(class, &all_lock_classes, lock_entry) {
629 if (new_class->key - new_class->subclass == class->key)
630 return class->name_version;
631 if (class->name && !strcmp(class->name, new_class->name))
632 count = max(count, class->name_version);
633 }
634
635 return count + 1;
636}
637
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700638/*
639 * Register a lock's class in the hash-table, if the class is not present
640 * yet. Otherwise we look it up. We cache the result in the lock object
641 * itself, so actual lookup of the hash should be once per lock object.
642 */
643static inline struct lock_class *
Ingo Molnard6d897c2006-07-10 04:44:04 -0700644look_up_lock_class(struct lockdep_map *lock, unsigned int subclass)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700645{
646 struct lockdep_subclass_key *key;
647 struct list_head *hash_head;
648 struct lock_class *class;
649
650#ifdef CONFIG_DEBUG_LOCKDEP
651 /*
652 * If the architecture calls into lockdep before initializing
653 * the hashes then we'll warn about it later. (we cannot printk
654 * right now)
655 */
656 if (unlikely(!lockdep_initialized)) {
657 lockdep_init();
658 lockdep_init_error = 1;
Johannes Bergc71063c2007-07-19 01:49:02 -0700659 save_stack_trace(&lockdep_init_trace);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700660 }
661#endif
662
663 /*
664 * Static locks do not have their class-keys yet - for them the key
665 * is the lock object itself:
666 */
667 if (unlikely(!lock->key))
668 lock->key = (void *)lock;
669
670 /*
671 * NOTE: the class-key must be unique. For dynamic locks, a static
672 * lock_class_key variable is passed in through the mutex_init()
673 * (or spin_lock_init()) call - which acts as the key. For static
674 * locks we use the lock object itself as the key.
675 */
Peter Zijlstra4b32d0a2007-07-19 01:48:59 -0700676 BUILD_BUG_ON(sizeof(struct lock_class_key) >
677 sizeof(struct lockdep_map));
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700678
679 key = lock->key->subkeys + subclass;
680
681 hash_head = classhashentry(key);
682
683 /*
684 * We can walk the hash lockfree, because the hash only
685 * grows, and we are careful when adding entries to the end:
686 */
Peter Zijlstra4b32d0a2007-07-19 01:48:59 -0700687 list_for_each_entry(class, hash_head, hash_entry) {
688 if (class->key == key) {
689 WARN_ON_ONCE(class->name != lock->name);
Ingo Molnard6d897c2006-07-10 04:44:04 -0700690 return class;
Peter Zijlstra4b32d0a2007-07-19 01:48:59 -0700691 }
692 }
Ingo Molnard6d897c2006-07-10 04:44:04 -0700693
694 return NULL;
695}
696
697/*
698 * Register a lock's class in the hash-table, if the class is not present
699 * yet. Otherwise we look it up. We cache the result in the lock object
700 * itself, so actual lookup of the hash should be once per lock object.
701 */
702static inline struct lock_class *
Peter Zijlstra4dfbb9d2006-10-11 01:45:14 -0400703register_lock_class(struct lockdep_map *lock, unsigned int subclass, int force)
Ingo Molnard6d897c2006-07-10 04:44:04 -0700704{
705 struct lockdep_subclass_key *key;
706 struct list_head *hash_head;
707 struct lock_class *class;
Ingo Molnar70e45062006-12-06 20:40:50 -0800708 unsigned long flags;
Ingo Molnard6d897c2006-07-10 04:44:04 -0700709
710 class = look_up_lock_class(lock, subclass);
711 if (likely(class))
712 return class;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700713
714 /*
715 * Debug-check: all keys must be persistent!
716 */
717 if (!static_obj(lock->key)) {
718 debug_locks_off();
719 printk("INFO: trying to register non-static key.\n");
720 printk("the code is fine but needs lockdep annotation.\n");
721 printk("turning off the locking correctness validator.\n");
722 dump_stack();
723
724 return NULL;
725 }
726
Ingo Molnard6d897c2006-07-10 04:44:04 -0700727 key = lock->key->subkeys + subclass;
728 hash_head = classhashentry(key);
729
Ingo Molnar70e45062006-12-06 20:40:50 -0800730 raw_local_irq_save(flags);
Ingo Molnar74c383f2006-12-13 00:34:43 -0800731 if (!graph_lock()) {
732 raw_local_irq_restore(flags);
733 return NULL;
734 }
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700735 /*
736 * We have to do the hash-walk again, to avoid races
737 * with another CPU:
738 */
739 list_for_each_entry(class, hash_head, hash_entry)
740 if (class->key == key)
741 goto out_unlock_set;
742 /*
743 * Allocate a new key from the static array, and add it to
744 * the hash:
745 */
746 if (nr_lock_classes >= MAX_LOCKDEP_KEYS) {
Ingo Molnar74c383f2006-12-13 00:34:43 -0800747 if (!debug_locks_off_graph_unlock()) {
748 raw_local_irq_restore(flags);
749 return NULL;
750 }
Ingo Molnar70e45062006-12-06 20:40:50 -0800751 raw_local_irq_restore(flags);
Ingo Molnar74c383f2006-12-13 00:34:43 -0800752
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700753 printk("BUG: MAX_LOCKDEP_KEYS too low!\n");
754 printk("turning off the locking correctness validator.\n");
Peter Zijlstraeedeeab2009-03-18 12:38:47 +0100755 dump_stack();
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700756 return NULL;
757 }
758 class = lock_classes + nr_lock_classes++;
759 debug_atomic_inc(&nr_unused_locks);
760 class->key = key;
761 class->name = lock->name;
762 class->subclass = subclass;
763 INIT_LIST_HEAD(&class->lock_entry);
764 INIT_LIST_HEAD(&class->locks_before);
765 INIT_LIST_HEAD(&class->locks_after);
766 class->name_version = count_matching_names(class);
767 /*
768 * We use RCU's safe list-add method to make
769 * parallel walking of the hash-list safe:
770 */
771 list_add_tail_rcu(&class->hash_entry, hash_head);
Dale Farnsworth14811972008-02-25 23:03:02 +0100772 /*
773 * Add it to the global list of classes:
774 */
775 list_add_tail_rcu(&class->lock_entry, &all_lock_classes);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700776
777 if (verbose(class)) {
Ingo Molnar74c383f2006-12-13 00:34:43 -0800778 graph_unlock();
Ingo Molnar70e45062006-12-06 20:40:50 -0800779 raw_local_irq_restore(flags);
Ingo Molnar74c383f2006-12-13 00:34:43 -0800780
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700781 printk("\nnew class %p: %s", class->key, class->name);
782 if (class->name_version > 1)
783 printk("#%d", class->name_version);
784 printk("\n");
785 dump_stack();
Ingo Molnar74c383f2006-12-13 00:34:43 -0800786
Ingo Molnar70e45062006-12-06 20:40:50 -0800787 raw_local_irq_save(flags);
Ingo Molnar74c383f2006-12-13 00:34:43 -0800788 if (!graph_lock()) {
789 raw_local_irq_restore(flags);
790 return NULL;
791 }
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700792 }
793out_unlock_set:
Ingo Molnar74c383f2006-12-13 00:34:43 -0800794 graph_unlock();
Ingo Molnar70e45062006-12-06 20:40:50 -0800795 raw_local_irq_restore(flags);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700796
Peter Zijlstra4dfbb9d2006-10-11 01:45:14 -0400797 if (!subclass || force)
Ingo Molnard6d897c2006-07-10 04:44:04 -0700798 lock->class_cache = class;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700799
Jarek Poplawski381a2292007-02-10 01:44:58 -0800800 if (DEBUG_LOCKS_WARN_ON(class->subclass != subclass))
801 return NULL;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700802
803 return class;
804}
805
Peter Zijlstraca58abc2007-07-19 01:48:53 -0700806#ifdef CONFIG_PROVE_LOCKING
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700807/*
Peter Zijlstra8e182572007-07-19 01:48:54 -0700808 * Allocate a lockdep entry. (assumes the graph_lock held, returns
809 * with NULL on failure)
810 */
811static struct lock_list *alloc_list_entry(void)
812{
813 if (nr_list_entries >= MAX_LOCKDEP_ENTRIES) {
814 if (!debug_locks_off_graph_unlock())
815 return NULL;
816
817 printk("BUG: MAX_LOCKDEP_ENTRIES too low!\n");
818 printk("turning off the locking correctness validator.\n");
Peter Zijlstraeedeeab2009-03-18 12:38:47 +0100819 dump_stack();
Peter Zijlstra8e182572007-07-19 01:48:54 -0700820 return NULL;
821 }
822 return list_entries + nr_list_entries++;
823}
824
825/*
826 * Add a new dependency to the head of the list:
827 */
828static int add_lock_to_list(struct lock_class *class, struct lock_class *this,
829 struct list_head *head, unsigned long ip, int distance)
830{
831 struct lock_list *entry;
832 /*
833 * Lock not present yet - get a new dependency struct and
834 * add it to the list:
835 */
836 entry = alloc_list_entry();
837 if (!entry)
838 return 0;
839
Peter Zijlstra8e182572007-07-19 01:48:54 -0700840 if (!save_trace(&entry->trace))
841 return 0;
842
Zhu Yi74870172008-08-27 14:33:00 +0800843 entry->class = this;
844 entry->distance = distance;
Peter Zijlstra8e182572007-07-19 01:48:54 -0700845 /*
846 * Since we never remove from the dependency list, the list can
847 * be walked lockless by other CPUs, it's only allocation
848 * that must be protected by the spinlock. But this also means
849 * we must make new entries visible only once writes to the
850 * entry become visible - hence the RCU op:
851 */
852 list_add_tail_rcu(&entry->entry, head);
853
854 return 1;
855}
856
Peter Zijlstra98c33ed2009-07-21 13:19:07 +0200857/*
858 * For good efficiency of modular, we use power of 2
859 */
Peter Zijlstraaf012962009-07-16 15:44:29 +0200860#define MAX_CIRCULAR_QUEUE_SIZE 4096UL
861#define CQ_MASK (MAX_CIRCULAR_QUEUE_SIZE-1)
862
Peter Zijlstra98c33ed2009-07-21 13:19:07 +0200863/*
864 * The circular_queue and helpers is used to implement the
Peter Zijlstraaf012962009-07-16 15:44:29 +0200865 * breadth-first search(BFS)algorithem, by which we can build
866 * the shortest path from the next lock to be acquired to the
867 * previous held lock if there is a circular between them.
Peter Zijlstra98c33ed2009-07-21 13:19:07 +0200868 */
Peter Zijlstraaf012962009-07-16 15:44:29 +0200869struct circular_queue {
870 unsigned long element[MAX_CIRCULAR_QUEUE_SIZE];
871 unsigned int front, rear;
872};
873
874static struct circular_queue lock_cq;
Peter Zijlstraaf012962009-07-16 15:44:29 +0200875
Ming Lei12f3dfd2009-07-16 15:44:29 +0200876unsigned int max_bfs_queue_depth;
Peter Zijlstraaf012962009-07-16 15:44:29 +0200877
Ming Leie351b662009-07-22 22:48:09 +0800878static unsigned int lockdep_dependency_gen_id;
879
Peter Zijlstraaf012962009-07-16 15:44:29 +0200880static inline void __cq_init(struct circular_queue *cq)
881{
882 cq->front = cq->rear = 0;
Ming Leie351b662009-07-22 22:48:09 +0800883 lockdep_dependency_gen_id++;
Peter Zijlstraaf012962009-07-16 15:44:29 +0200884}
885
886static inline int __cq_empty(struct circular_queue *cq)
887{
888 return (cq->front == cq->rear);
889}
890
891static inline int __cq_full(struct circular_queue *cq)
892{
893 return ((cq->rear + 1) & CQ_MASK) == cq->front;
894}
895
896static inline int __cq_enqueue(struct circular_queue *cq, unsigned long elem)
897{
898 if (__cq_full(cq))
899 return -1;
900
901 cq->element[cq->rear] = elem;
902 cq->rear = (cq->rear + 1) & CQ_MASK;
903 return 0;
904}
905
906static inline int __cq_dequeue(struct circular_queue *cq, unsigned long *elem)
907{
908 if (__cq_empty(cq))
909 return -1;
910
911 *elem = cq->element[cq->front];
912 cq->front = (cq->front + 1) & CQ_MASK;
913 return 0;
914}
915
916static inline unsigned int __cq_get_elem_count(struct circular_queue *cq)
917{
918 return (cq->rear - cq->front) & CQ_MASK;
919}
920
921static inline void mark_lock_accessed(struct lock_list *lock,
922 struct lock_list *parent)
923{
924 unsigned long nr;
Peter Zijlstra98c33ed2009-07-21 13:19:07 +0200925
Peter Zijlstraaf012962009-07-16 15:44:29 +0200926 nr = lock - list_entries;
927 WARN_ON(nr >= nr_list_entries);
928 lock->parent = parent;
Ming Leie351b662009-07-22 22:48:09 +0800929 lock->class->dep_gen_id = lockdep_dependency_gen_id;
Peter Zijlstraaf012962009-07-16 15:44:29 +0200930}
931
932static inline unsigned long lock_accessed(struct lock_list *lock)
933{
934 unsigned long nr;
Peter Zijlstra98c33ed2009-07-21 13:19:07 +0200935
Peter Zijlstraaf012962009-07-16 15:44:29 +0200936 nr = lock - list_entries;
937 WARN_ON(nr >= nr_list_entries);
Ming Leie351b662009-07-22 22:48:09 +0800938 return lock->class->dep_gen_id == lockdep_dependency_gen_id;
Peter Zijlstraaf012962009-07-16 15:44:29 +0200939}
940
941static inline struct lock_list *get_lock_parent(struct lock_list *child)
942{
943 return child->parent;
944}
945
946static inline int get_lock_depth(struct lock_list *child)
947{
948 int depth = 0;
949 struct lock_list *parent;
950
951 while ((parent = get_lock_parent(child))) {
952 child = parent;
953 depth++;
954 }
955 return depth;
956}
957
Ming Lei9e2d5512009-07-16 15:44:29 +0200958static int __bfs(struct lock_list *source_entry,
Peter Zijlstraaf012962009-07-16 15:44:29 +0200959 void *data,
960 int (*match)(struct lock_list *entry, void *data),
961 struct lock_list **target_entry,
962 int forward)
Ming Leic94aa5c2009-07-16 15:44:29 +0200963{
964 struct lock_list *entry;
Ming Leid588e462009-07-16 15:44:29 +0200965 struct list_head *head;
Ming Leic94aa5c2009-07-16 15:44:29 +0200966 struct circular_queue *cq = &lock_cq;
967 int ret = 1;
968
Ming Lei9e2d5512009-07-16 15:44:29 +0200969 if (match(source_entry, data)) {
Ming Leic94aa5c2009-07-16 15:44:29 +0200970 *target_entry = source_entry;
971 ret = 0;
972 goto exit;
973 }
974
Ming Leid588e462009-07-16 15:44:29 +0200975 if (forward)
976 head = &source_entry->class->locks_after;
977 else
978 head = &source_entry->class->locks_before;
979
980 if (list_empty(head))
981 goto exit;
982
983 __cq_init(cq);
Ming Leic94aa5c2009-07-16 15:44:29 +0200984 __cq_enqueue(cq, (unsigned long)source_entry);
985
986 while (!__cq_empty(cq)) {
987 struct lock_list *lock;
Ming Leic94aa5c2009-07-16 15:44:29 +0200988
989 __cq_dequeue(cq, (unsigned long *)&lock);
990
991 if (!lock->class) {
992 ret = -2;
993 goto exit;
994 }
995
996 if (forward)
997 head = &lock->class->locks_after;
998 else
999 head = &lock->class->locks_before;
1000
1001 list_for_each_entry(entry, head, entry) {
1002 if (!lock_accessed(entry)) {
Ming Lei12f3dfd2009-07-16 15:44:29 +02001003 unsigned int cq_depth;
Ming Leic94aa5c2009-07-16 15:44:29 +02001004 mark_lock_accessed(entry, lock);
Ming Lei9e2d5512009-07-16 15:44:29 +02001005 if (match(entry, data)) {
Ming Leic94aa5c2009-07-16 15:44:29 +02001006 *target_entry = entry;
1007 ret = 0;
1008 goto exit;
1009 }
1010
1011 if (__cq_enqueue(cq, (unsigned long)entry)) {
1012 ret = -1;
1013 goto exit;
1014 }
Ming Lei12f3dfd2009-07-16 15:44:29 +02001015 cq_depth = __cq_get_elem_count(cq);
1016 if (max_bfs_queue_depth < cq_depth)
1017 max_bfs_queue_depth = cq_depth;
Ming Leic94aa5c2009-07-16 15:44:29 +02001018 }
1019 }
1020 }
1021exit:
1022 return ret;
1023}
1024
Ming Leid7aaba12009-07-16 15:44:29 +02001025static inline int __bfs_forwards(struct lock_list *src_entry,
Ming Lei9e2d5512009-07-16 15:44:29 +02001026 void *data,
1027 int (*match)(struct lock_list *entry, void *data),
1028 struct lock_list **target_entry)
Ming Leic94aa5c2009-07-16 15:44:29 +02001029{
Ming Lei9e2d5512009-07-16 15:44:29 +02001030 return __bfs(src_entry, data, match, target_entry, 1);
Ming Leic94aa5c2009-07-16 15:44:29 +02001031
1032}
1033
Ming Leid7aaba12009-07-16 15:44:29 +02001034static inline int __bfs_backwards(struct lock_list *src_entry,
Ming Lei9e2d5512009-07-16 15:44:29 +02001035 void *data,
1036 int (*match)(struct lock_list *entry, void *data),
1037 struct lock_list **target_entry)
Ming Leic94aa5c2009-07-16 15:44:29 +02001038{
Ming Lei9e2d5512009-07-16 15:44:29 +02001039 return __bfs(src_entry, data, match, target_entry, 0);
Ming Leic94aa5c2009-07-16 15:44:29 +02001040
1041}
1042
Peter Zijlstra8e182572007-07-19 01:48:54 -07001043/*
1044 * Recursive, forwards-direction lock-dependency checking, used for
1045 * both noncyclic checking and for hardirq-unsafe/softirq-unsafe
1046 * checking.
Peter Zijlstra8e182572007-07-19 01:48:54 -07001047 */
Peter Zijlstra8e182572007-07-19 01:48:54 -07001048
1049/*
1050 * Print a dependency chain entry (this is only done when a deadlock
1051 * has been detected):
1052 */
1053static noinline int
Ming Lei24208ca2009-07-16 15:44:29 +02001054print_circular_bug_entry(struct lock_list *target, int depth)
Peter Zijlstra8e182572007-07-19 01:48:54 -07001055{
1056 if (debug_locks_silent)
1057 return 0;
1058 printk("\n-> #%u", depth);
1059 print_lock_name(target->class);
1060 printk(":\n");
1061 print_stack_trace(&target->trace, 6);
1062
1063 return 0;
1064}
1065
1066/*
1067 * When a circular dependency is detected, print the
1068 * header first:
1069 */
1070static noinline int
Ming Leidb0002a2009-07-16 15:44:29 +02001071print_circular_bug_header(struct lock_list *entry, unsigned int depth,
1072 struct held_lock *check_src,
1073 struct held_lock *check_tgt)
Peter Zijlstra8e182572007-07-19 01:48:54 -07001074{
1075 struct task_struct *curr = current;
1076
Ming Leic94aa5c2009-07-16 15:44:29 +02001077 if (debug_locks_silent)
Peter Zijlstra8e182572007-07-19 01:48:54 -07001078 return 0;
1079
1080 printk("\n=======================================================\n");
1081 printk( "[ INFO: possible circular locking dependency detected ]\n");
1082 print_kernel_version();
1083 printk( "-------------------------------------------------------\n");
1084 printk("%s/%d is trying to acquire lock:\n",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07001085 curr->comm, task_pid_nr(curr));
Ming Leidb0002a2009-07-16 15:44:29 +02001086 print_lock(check_src);
Peter Zijlstra8e182572007-07-19 01:48:54 -07001087 printk("\nbut task is already holding lock:\n");
Ming Leidb0002a2009-07-16 15:44:29 +02001088 print_lock(check_tgt);
Peter Zijlstra8e182572007-07-19 01:48:54 -07001089 printk("\nwhich lock already depends on the new lock.\n\n");
1090 printk("\nthe existing dependency chain (in reverse order) is:\n");
1091
1092 print_circular_bug_entry(entry, depth);
1093
1094 return 0;
1095}
1096
Ming Lei9e2d5512009-07-16 15:44:29 +02001097static inline int class_equal(struct lock_list *entry, void *data)
1098{
1099 return entry->class == data;
1100}
1101
Ming Leidb0002a2009-07-16 15:44:29 +02001102static noinline int print_circular_bug(struct lock_list *this,
1103 struct lock_list *target,
1104 struct held_lock *check_src,
1105 struct held_lock *check_tgt)
Peter Zijlstra8e182572007-07-19 01:48:54 -07001106{
1107 struct task_struct *curr = current;
Ming Leic94aa5c2009-07-16 15:44:29 +02001108 struct lock_list *parent;
Ming Lei24208ca2009-07-16 15:44:29 +02001109 int depth;
Peter Zijlstra8e182572007-07-19 01:48:54 -07001110
Ming Leic94aa5c2009-07-16 15:44:29 +02001111 if (!debug_locks_off_graph_unlock() || debug_locks_silent)
Peter Zijlstra8e182572007-07-19 01:48:54 -07001112 return 0;
1113
Ming Leidb0002a2009-07-16 15:44:29 +02001114 if (!save_trace(&this->trace))
Peter Zijlstra8e182572007-07-19 01:48:54 -07001115 return 0;
1116
Ming Leic94aa5c2009-07-16 15:44:29 +02001117 depth = get_lock_depth(target);
1118
Ming Leidb0002a2009-07-16 15:44:29 +02001119 print_circular_bug_header(target, depth, check_src, check_tgt);
Ming Leic94aa5c2009-07-16 15:44:29 +02001120
1121 parent = get_lock_parent(target);
1122
1123 while (parent) {
1124 print_circular_bug_entry(parent, --depth);
1125 parent = get_lock_parent(parent);
1126 }
Peter Zijlstra8e182572007-07-19 01:48:54 -07001127
1128 printk("\nother info that might help us debug this:\n\n");
1129 lockdep_print_held_locks(curr);
1130
1131 printk("\nstack backtrace:\n");
1132 dump_stack();
1133
1134 return 0;
1135}
1136
Ming Leidb0002a2009-07-16 15:44:29 +02001137static noinline int print_bfs_bug(int ret)
1138{
1139 if (!debug_locks_off_graph_unlock())
1140 return 0;
1141
1142 WARN(1, "lockdep bfs error:%d\n", ret);
1143
1144 return 0;
1145}
1146
Ming Leief681022009-07-16 15:44:29 +02001147static int noop_count(struct lock_list *entry, void *data)
David Miller419ca3f2008-07-29 21:45:03 -07001148{
Ming Leief681022009-07-16 15:44:29 +02001149 (*(unsigned long *)data)++;
1150 return 0;
David Miller419ca3f2008-07-29 21:45:03 -07001151}
1152
Ming Leief681022009-07-16 15:44:29 +02001153unsigned long __lockdep_count_forward_deps(struct lock_list *this)
1154{
1155 unsigned long count = 0;
1156 struct lock_list *uninitialized_var(target_entry);
1157
1158 __bfs_forwards(this, (void *)&count, noop_count, &target_entry);
1159
1160 return count;
1161}
David Miller419ca3f2008-07-29 21:45:03 -07001162unsigned long lockdep_count_forward_deps(struct lock_class *class)
1163{
1164 unsigned long ret, flags;
Ming Leief681022009-07-16 15:44:29 +02001165 struct lock_list this;
1166
1167 this.parent = NULL;
1168 this.class = class;
David Miller419ca3f2008-07-29 21:45:03 -07001169
1170 local_irq_save(flags);
1171 __raw_spin_lock(&lockdep_lock);
Ming Leief681022009-07-16 15:44:29 +02001172 ret = __lockdep_count_forward_deps(&this);
David Miller419ca3f2008-07-29 21:45:03 -07001173 __raw_spin_unlock(&lockdep_lock);
1174 local_irq_restore(flags);
1175
1176 return ret;
1177}
1178
Ming Leief681022009-07-16 15:44:29 +02001179unsigned long __lockdep_count_backward_deps(struct lock_list *this)
David Miller419ca3f2008-07-29 21:45:03 -07001180{
Ming Leief681022009-07-16 15:44:29 +02001181 unsigned long count = 0;
1182 struct lock_list *uninitialized_var(target_entry);
David Miller419ca3f2008-07-29 21:45:03 -07001183
Ming Leief681022009-07-16 15:44:29 +02001184 __bfs_backwards(this, (void *)&count, noop_count, &target_entry);
David Miller419ca3f2008-07-29 21:45:03 -07001185
Ming Leief681022009-07-16 15:44:29 +02001186 return count;
David Miller419ca3f2008-07-29 21:45:03 -07001187}
1188
1189unsigned long lockdep_count_backward_deps(struct lock_class *class)
1190{
1191 unsigned long ret, flags;
Ming Leief681022009-07-16 15:44:29 +02001192 struct lock_list this;
1193
1194 this.parent = NULL;
1195 this.class = class;
David Miller419ca3f2008-07-29 21:45:03 -07001196
1197 local_irq_save(flags);
1198 __raw_spin_lock(&lockdep_lock);
Ming Leief681022009-07-16 15:44:29 +02001199 ret = __lockdep_count_backward_deps(&this);
David Miller419ca3f2008-07-29 21:45:03 -07001200 __raw_spin_unlock(&lockdep_lock);
1201 local_irq_restore(flags);
1202
1203 return ret;
1204}
1205
Peter Zijlstra8e182572007-07-19 01:48:54 -07001206/*
1207 * Prove that the dependency graph starting at <entry> can not
1208 * lead to <target>. Print an error and return 0 if it does.
1209 */
1210static noinline int
Ming Leidb0002a2009-07-16 15:44:29 +02001211check_noncircular(struct lock_list *root, struct lock_class *target,
1212 struct lock_list **target_entry)
Peter Zijlstra8e182572007-07-19 01:48:54 -07001213{
Ming Leidb0002a2009-07-16 15:44:29 +02001214 int result;
Peter Zijlstra8e182572007-07-19 01:48:54 -07001215
Ming Leidb0002a2009-07-16 15:44:29 +02001216 debug_atomic_inc(&nr_cyclic_checks);
David Miller419ca3f2008-07-29 21:45:03 -07001217
Ming Leid7aaba12009-07-16 15:44:29 +02001218 result = __bfs_forwards(root, target, class_equal, target_entry);
Ming Leidb0002a2009-07-16 15:44:29 +02001219
1220 return result;
Peter Zijlstra8e182572007-07-19 01:48:54 -07001221}
1222
Steven Rostedt81d68a92008-05-12 21:20:42 +02001223#if defined(CONFIG_TRACE_IRQFLAGS) && defined(CONFIG_PROVE_LOCKING)
Peter Zijlstra8e182572007-07-19 01:48:54 -07001224/*
1225 * Forwards and backwards subgraph searching, for the purposes of
1226 * proving that two subgraphs can be connected by a new dependency
1227 * without creating any illegal irq-safe -> irq-unsafe lock dependency.
1228 */
Peter Zijlstra8e182572007-07-19 01:48:54 -07001229
Ming Leid7aaba12009-07-16 15:44:29 +02001230static inline int usage_match(struct lock_list *entry, void *bit)
1231{
1232 return entry->class->usage_mask & (1 << (enum lock_usage_bit)bit);
1233}
1234
1235
1236
Peter Zijlstra8e182572007-07-19 01:48:54 -07001237/*
1238 * Find a node in the forwards-direction dependency sub-graph starting
Ming Leid7aaba12009-07-16 15:44:29 +02001239 * at @root->class that matches @bit.
Peter Zijlstra8e182572007-07-19 01:48:54 -07001240 *
Ming Leid7aaba12009-07-16 15:44:29 +02001241 * Return 0 if such a node exists in the subgraph, and put that node
1242 * into *@target_entry.
Peter Zijlstra8e182572007-07-19 01:48:54 -07001243 *
Ming Leid7aaba12009-07-16 15:44:29 +02001244 * Return 1 otherwise and keep *@target_entry unchanged.
1245 * Return <0 on error.
Peter Zijlstra8e182572007-07-19 01:48:54 -07001246 */
Ming Leid7aaba12009-07-16 15:44:29 +02001247static int
1248find_usage_forwards(struct lock_list *root, enum lock_usage_bit bit,
1249 struct lock_list **target_entry)
Peter Zijlstra8e182572007-07-19 01:48:54 -07001250{
Ming Leid7aaba12009-07-16 15:44:29 +02001251 int result;
Peter Zijlstra8e182572007-07-19 01:48:54 -07001252
1253 debug_atomic_inc(&nr_find_usage_forwards_checks);
Peter Zijlstra8e182572007-07-19 01:48:54 -07001254
Ming Leid7aaba12009-07-16 15:44:29 +02001255 result = __bfs_forwards(root, (void *)bit, usage_match, target_entry);
1256
1257 return result;
Peter Zijlstra8e182572007-07-19 01:48:54 -07001258}
1259
1260/*
1261 * Find a node in the backwards-direction dependency sub-graph starting
Ming Leid7aaba12009-07-16 15:44:29 +02001262 * at @root->class that matches @bit.
Peter Zijlstra8e182572007-07-19 01:48:54 -07001263 *
Ming Leid7aaba12009-07-16 15:44:29 +02001264 * Return 0 if such a node exists in the subgraph, and put that node
1265 * into *@target_entry.
Peter Zijlstra8e182572007-07-19 01:48:54 -07001266 *
Ming Leid7aaba12009-07-16 15:44:29 +02001267 * Return 1 otherwise and keep *@target_entry unchanged.
1268 * Return <0 on error.
Peter Zijlstra8e182572007-07-19 01:48:54 -07001269 */
Ming Leid7aaba12009-07-16 15:44:29 +02001270static int
1271find_usage_backwards(struct lock_list *root, enum lock_usage_bit bit,
1272 struct lock_list **target_entry)
Peter Zijlstra8e182572007-07-19 01:48:54 -07001273{
Ming Leid7aaba12009-07-16 15:44:29 +02001274 int result;
Peter Zijlstra8e182572007-07-19 01:48:54 -07001275
1276 debug_atomic_inc(&nr_find_usage_backwards_checks);
Peter Zijlstra8e182572007-07-19 01:48:54 -07001277
Ming Leid7aaba12009-07-16 15:44:29 +02001278 result = __bfs_backwards(root, (void *)bit, usage_match, target_entry);
Dave Jonesf82b2172008-08-11 09:30:23 +02001279
Ming Leid7aaba12009-07-16 15:44:29 +02001280 return result;
Peter Zijlstra8e182572007-07-19 01:48:54 -07001281}
1282
Peter Zijlstraaf012962009-07-16 15:44:29 +02001283static void print_lock_class_header(struct lock_class *class, int depth)
1284{
1285 int bit;
1286
1287 printk("%*s->", depth, "");
1288 print_lock_name(class);
1289 printk(" ops: %lu", class->ops);
1290 printk(" {\n");
1291
1292 for (bit = 0; bit < LOCK_USAGE_STATES; bit++) {
1293 if (class->usage_mask & (1 << bit)) {
1294 int len = depth;
1295
1296 len += printk("%*s %s", depth, "", usage_str[bit]);
1297 len += printk(" at:\n");
1298 print_stack_trace(class->usage_traces + bit, len);
1299 }
1300 }
1301 printk("%*s }\n", depth, "");
1302
1303 printk("%*s ... key at: ",depth,"");
1304 print_ip_sym((unsigned long)class->key);
1305}
1306
1307/*
1308 * printk the shortest lock dependencies from @start to @end in reverse order:
1309 */
1310static void __used
1311print_shortest_lock_dependencies(struct lock_list *leaf,
1312 struct lock_list *root)
1313{
1314 struct lock_list *entry = leaf;
1315 int depth;
1316
1317 /*compute depth from generated tree by BFS*/
1318 depth = get_lock_depth(leaf);
1319
1320 do {
1321 print_lock_class_header(entry->class, depth);
1322 printk("%*s ... acquired at:\n", depth, "");
1323 print_stack_trace(&entry->trace, 2);
1324 printk("\n");
1325
1326 if (depth == 0 && (entry != root)) {
1327 printk("lockdep:%s bad BFS generated tree\n", __func__);
1328 break;
1329 }
1330
1331 entry = get_lock_parent(entry);
1332 depth--;
1333 } while (entry && (depth >= 0));
1334
1335 return;
1336}
Ming Leid7aaba12009-07-16 15:44:29 +02001337
Peter Zijlstra8e182572007-07-19 01:48:54 -07001338static int
1339print_bad_irq_dependency(struct task_struct *curr,
Ming Lei24208ca2009-07-16 15:44:29 +02001340 struct lock_list *prev_root,
1341 struct lock_list *next_root,
1342 struct lock_list *backwards_entry,
1343 struct lock_list *forwards_entry,
Peter Zijlstra8e182572007-07-19 01:48:54 -07001344 struct held_lock *prev,
1345 struct held_lock *next,
1346 enum lock_usage_bit bit1,
1347 enum lock_usage_bit bit2,
1348 const char *irqclass)
1349{
1350 if (!debug_locks_off_graph_unlock() || debug_locks_silent)
1351 return 0;
1352
1353 printk("\n======================================================\n");
1354 printk( "[ INFO: %s-safe -> %s-unsafe lock order detected ]\n",
1355 irqclass, irqclass);
1356 print_kernel_version();
1357 printk( "------------------------------------------------------\n");
1358 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 -07001359 curr->comm, task_pid_nr(curr),
Peter Zijlstra8e182572007-07-19 01:48:54 -07001360 curr->hardirq_context, hardirq_count() >> HARDIRQ_SHIFT,
1361 curr->softirq_context, softirq_count() >> SOFTIRQ_SHIFT,
1362 curr->hardirqs_enabled,
1363 curr->softirqs_enabled);
1364 print_lock(next);
1365
1366 printk("\nand this task is already holding:\n");
1367 print_lock(prev);
1368 printk("which would create a new lock dependency:\n");
Dave Jonesf82b2172008-08-11 09:30:23 +02001369 print_lock_name(hlock_class(prev));
Peter Zijlstra8e182572007-07-19 01:48:54 -07001370 printk(" ->");
Dave Jonesf82b2172008-08-11 09:30:23 +02001371 print_lock_name(hlock_class(next));
Peter Zijlstra8e182572007-07-19 01:48:54 -07001372 printk("\n");
1373
1374 printk("\nbut this new dependency connects a %s-irq-safe lock:\n",
1375 irqclass);
Ming Lei24208ca2009-07-16 15:44:29 +02001376 print_lock_name(backwards_entry->class);
Peter Zijlstra8e182572007-07-19 01:48:54 -07001377 printk("\n... which became %s-irq-safe at:\n", irqclass);
1378
Ming Lei24208ca2009-07-16 15:44:29 +02001379 print_stack_trace(backwards_entry->class->usage_traces + bit1, 1);
Peter Zijlstra8e182572007-07-19 01:48:54 -07001380
1381 printk("\nto a %s-irq-unsafe lock:\n", irqclass);
Ming Lei24208ca2009-07-16 15:44:29 +02001382 print_lock_name(forwards_entry->class);
Peter Zijlstra8e182572007-07-19 01:48:54 -07001383 printk("\n... which became %s-irq-unsafe at:\n", irqclass);
1384 printk("...");
1385
Ming Lei24208ca2009-07-16 15:44:29 +02001386 print_stack_trace(forwards_entry->class->usage_traces + bit2, 1);
Peter Zijlstra8e182572007-07-19 01:48:54 -07001387
1388 printk("\nother info that might help us debug this:\n\n");
1389 lockdep_print_held_locks(curr);
1390
Ming Lei24208ca2009-07-16 15:44:29 +02001391 printk("\nthe dependencies between %s-irq-safe lock", irqclass);
1392 printk(" and the holding lock:\n");
1393 if (!save_trace(&prev_root->trace))
1394 return 0;
1395 print_shortest_lock_dependencies(backwards_entry, prev_root);
Peter Zijlstra8e182572007-07-19 01:48:54 -07001396
Ming Lei24208ca2009-07-16 15:44:29 +02001397 printk("\nthe dependencies between the lock to be acquired");
1398 printk(" and %s-irq-unsafe lock:\n", irqclass);
1399 if (!save_trace(&next_root->trace))
1400 return 0;
1401 print_shortest_lock_dependencies(forwards_entry, next_root);
Peter Zijlstra8e182572007-07-19 01:48:54 -07001402
1403 printk("\nstack backtrace:\n");
1404 dump_stack();
1405
1406 return 0;
1407}
1408
1409static int
1410check_usage(struct task_struct *curr, struct held_lock *prev,
1411 struct held_lock *next, enum lock_usage_bit bit_backwards,
1412 enum lock_usage_bit bit_forwards, const char *irqclass)
1413{
1414 int ret;
Ming Lei24208ca2009-07-16 15:44:29 +02001415 struct lock_list this, that;
Ming Leid7aaba12009-07-16 15:44:29 +02001416 struct lock_list *uninitialized_var(target_entry);
Ming Lei24208ca2009-07-16 15:44:29 +02001417 struct lock_list *uninitialized_var(target_entry1);
Peter Zijlstra8e182572007-07-19 01:48:54 -07001418
Ming Leid7aaba12009-07-16 15:44:29 +02001419 this.parent = NULL;
Peter Zijlstra8e182572007-07-19 01:48:54 -07001420
Ming Leid7aaba12009-07-16 15:44:29 +02001421 this.class = hlock_class(prev);
1422 ret = find_usage_backwards(&this, bit_backwards, &target_entry);
Peter Zijlstraaf012962009-07-16 15:44:29 +02001423 if (ret < 0)
1424 return print_bfs_bug(ret);
1425 if (ret == 1)
1426 return ret;
Ming Leid7aaba12009-07-16 15:44:29 +02001427
Ming Lei24208ca2009-07-16 15:44:29 +02001428 that.parent = NULL;
1429 that.class = hlock_class(next);
1430 ret = find_usage_forwards(&that, bit_forwards, &target_entry1);
Peter Zijlstraaf012962009-07-16 15:44:29 +02001431 if (ret < 0)
1432 return print_bfs_bug(ret);
1433 if (ret == 1)
1434 return ret;
Ming Leid7aaba12009-07-16 15:44:29 +02001435
Ming Lei24208ca2009-07-16 15:44:29 +02001436 return print_bad_irq_dependency(curr, &this, &that,
1437 target_entry, target_entry1,
1438 prev, next,
Peter Zijlstra8e182572007-07-19 01:48:54 -07001439 bit_backwards, bit_forwards, irqclass);
1440}
1441
Peter Zijlstra4f367d8a2009-01-22 18:10:42 +01001442static const char *state_names[] = {
1443#define LOCKDEP_STATE(__STATE) \
Peter Zijlstrab4b136f2009-01-29 14:50:36 +01001444 __stringify(__STATE),
Peter Zijlstra4f367d8a2009-01-22 18:10:42 +01001445#include "lockdep_states.h"
1446#undef LOCKDEP_STATE
1447};
1448
1449static const char *state_rnames[] = {
1450#define LOCKDEP_STATE(__STATE) \
Peter Zijlstrab4b136f2009-01-29 14:50:36 +01001451 __stringify(__STATE)"-READ",
Peter Zijlstra4f367d8a2009-01-22 18:10:42 +01001452#include "lockdep_states.h"
1453#undef LOCKDEP_STATE
1454};
1455
1456static inline const char *state_name(enum lock_usage_bit bit)
1457{
1458 return (bit & 1) ? state_rnames[bit >> 2] : state_names[bit >> 2];
1459}
1460
1461static int exclusive_bit(int new_bit)
1462{
1463 /*
1464 * USED_IN
1465 * USED_IN_READ
1466 * ENABLED
1467 * ENABLED_READ
1468 *
1469 * bit 0 - write/read
1470 * bit 1 - used_in/enabled
1471 * bit 2+ state
1472 */
1473
1474 int state = new_bit & ~3;
1475 int dir = new_bit & 2;
1476
1477 /*
1478 * keep state, bit flip the direction and strip read.
1479 */
1480 return state | (dir ^ 2);
1481}
1482
1483static int check_irq_usage(struct task_struct *curr, struct held_lock *prev,
1484 struct held_lock *next, enum lock_usage_bit bit)
Peter Zijlstra8e182572007-07-19 01:48:54 -07001485{
1486 /*
1487 * Prove that the new dependency does not connect a hardirq-safe
1488 * lock with a hardirq-unsafe lock - to achieve this we search
1489 * the backwards-subgraph starting at <prev>, and the
1490 * forwards-subgraph starting at <next>:
1491 */
Peter Zijlstra4f367d8a2009-01-22 18:10:42 +01001492 if (!check_usage(curr, prev, next, bit,
1493 exclusive_bit(bit), state_name(bit)))
Peter Zijlstra8e182572007-07-19 01:48:54 -07001494 return 0;
1495
Peter Zijlstra4f367d8a2009-01-22 18:10:42 +01001496 bit++; /* _READ */
1497
Peter Zijlstra8e182572007-07-19 01:48:54 -07001498 /*
1499 * Prove that the new dependency does not connect a hardirq-safe-read
1500 * lock with a hardirq-unsafe lock - to achieve this we search
1501 * the backwards-subgraph starting at <prev>, and the
1502 * forwards-subgraph starting at <next>:
1503 */
Peter Zijlstra4f367d8a2009-01-22 18:10:42 +01001504 if (!check_usage(curr, prev, next, bit,
1505 exclusive_bit(bit), state_name(bit)))
Peter Zijlstra8e182572007-07-19 01:48:54 -07001506 return 0;
1507
Peter Zijlstra4f367d8a2009-01-22 18:10:42 +01001508 return 1;
1509}
Peter Zijlstra8e182572007-07-19 01:48:54 -07001510
Peter Zijlstra4f367d8a2009-01-22 18:10:42 +01001511static int
1512check_prev_add_irq(struct task_struct *curr, struct held_lock *prev,
1513 struct held_lock *next)
1514{
1515#define LOCKDEP_STATE(__STATE) \
1516 if (!check_irq_usage(curr, prev, next, LOCK_USED_IN_##__STATE)) \
Nick Piggincf40bd12009-01-21 08:12:39 +01001517 return 0;
Peter Zijlstra4f367d8a2009-01-22 18:10:42 +01001518#include "lockdep_states.h"
1519#undef LOCKDEP_STATE
Nick Piggincf40bd12009-01-21 08:12:39 +01001520
Peter Zijlstra8e182572007-07-19 01:48:54 -07001521 return 1;
1522}
1523
1524static void inc_chains(void)
1525{
1526 if (current->hardirq_context)
1527 nr_hardirq_chains++;
1528 else {
1529 if (current->softirq_context)
1530 nr_softirq_chains++;
1531 else
1532 nr_process_chains++;
1533 }
1534}
1535
1536#else
1537
1538static inline int
1539check_prev_add_irq(struct task_struct *curr, struct held_lock *prev,
1540 struct held_lock *next)
1541{
1542 return 1;
1543}
1544
1545static inline void inc_chains(void)
1546{
1547 nr_process_chains++;
1548}
1549
1550#endif
1551
1552static int
1553print_deadlock_bug(struct task_struct *curr, struct held_lock *prev,
1554 struct held_lock *next)
1555{
1556 if (!debug_locks_off_graph_unlock() || debug_locks_silent)
1557 return 0;
1558
1559 printk("\n=============================================\n");
1560 printk( "[ INFO: possible recursive locking detected ]\n");
1561 print_kernel_version();
1562 printk( "---------------------------------------------\n");
1563 printk("%s/%d is trying to acquire lock:\n",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07001564 curr->comm, task_pid_nr(curr));
Peter Zijlstra8e182572007-07-19 01:48:54 -07001565 print_lock(next);
1566 printk("\nbut task is already holding lock:\n");
1567 print_lock(prev);
1568
1569 printk("\nother info that might help us debug this:\n");
1570 lockdep_print_held_locks(curr);
1571
1572 printk("\nstack backtrace:\n");
1573 dump_stack();
1574
1575 return 0;
1576}
1577
1578/*
1579 * Check whether we are holding such a class already.
1580 *
1581 * (Note that this has to be done separately, because the graph cannot
1582 * detect such classes of deadlocks.)
1583 *
1584 * Returns: 0 on deadlock detected, 1 on OK, 2 on recursive read
1585 */
1586static int
1587check_deadlock(struct task_struct *curr, struct held_lock *next,
1588 struct lockdep_map *next_instance, int read)
1589{
1590 struct held_lock *prev;
Peter Zijlstra7531e2f2008-08-11 09:30:24 +02001591 struct held_lock *nest = NULL;
Peter Zijlstra8e182572007-07-19 01:48:54 -07001592 int i;
1593
1594 for (i = 0; i < curr->lockdep_depth; i++) {
1595 prev = curr->held_locks + i;
Peter Zijlstra7531e2f2008-08-11 09:30:24 +02001596
1597 if (prev->instance == next->nest_lock)
1598 nest = prev;
1599
Dave Jonesf82b2172008-08-11 09:30:23 +02001600 if (hlock_class(prev) != hlock_class(next))
Peter Zijlstra8e182572007-07-19 01:48:54 -07001601 continue;
Peter Zijlstra7531e2f2008-08-11 09:30:24 +02001602
Peter Zijlstra8e182572007-07-19 01:48:54 -07001603 /*
1604 * Allow read-after-read recursion of the same
1605 * lock class (i.e. read_lock(lock)+read_lock(lock)):
1606 */
1607 if ((read == 2) && prev->read)
1608 return 2;
Peter Zijlstra7531e2f2008-08-11 09:30:24 +02001609
1610 /*
1611 * We're holding the nest_lock, which serializes this lock's
1612 * nesting behaviour.
1613 */
1614 if (nest)
1615 return 2;
1616
Peter Zijlstra8e182572007-07-19 01:48:54 -07001617 return print_deadlock_bug(curr, prev, next);
1618 }
1619 return 1;
1620}
1621
1622/*
1623 * There was a chain-cache miss, and we are about to add a new dependency
1624 * to a previous lock. We recursively validate the following rules:
1625 *
1626 * - would the adding of the <prev> -> <next> dependency create a
1627 * circular dependency in the graph? [== circular deadlock]
1628 *
1629 * - does the new prev->next dependency connect any hardirq-safe lock
1630 * (in the full backwards-subgraph starting at <prev>) with any
1631 * hardirq-unsafe lock (in the full forwards-subgraph starting at
1632 * <next>)? [== illegal lock inversion with hardirq contexts]
1633 *
1634 * - does the new prev->next dependency connect any softirq-safe lock
1635 * (in the full backwards-subgraph starting at <prev>) with any
1636 * softirq-unsafe lock (in the full forwards-subgraph starting at
1637 * <next>)? [== illegal lock inversion with softirq contexts]
1638 *
1639 * any of these scenarios could lead to a deadlock.
1640 *
1641 * Then if all the validations pass, we add the forwards and backwards
1642 * dependency.
1643 */
1644static int
1645check_prev_add(struct task_struct *curr, struct held_lock *prev,
1646 struct held_lock *next, int distance)
1647{
1648 struct lock_list *entry;
1649 int ret;
Ming Leidb0002a2009-07-16 15:44:29 +02001650 struct lock_list this;
1651 struct lock_list *uninitialized_var(target_entry);
Peter Zijlstra8e182572007-07-19 01:48:54 -07001652
1653 /*
1654 * Prove that the new <prev> -> <next> dependency would not
1655 * create a circular dependency in the graph. (We do this by
1656 * forward-recursing into the graph starting at <next>, and
1657 * checking whether we can reach <prev>.)
1658 *
1659 * We are using global variables to control the recursion, to
1660 * keep the stackframe size of the recursive functions low:
1661 */
Ming Leidb0002a2009-07-16 15:44:29 +02001662 this.class = hlock_class(next);
1663 this.parent = NULL;
1664 ret = check_noncircular(&this, hlock_class(prev), &target_entry);
1665 if (unlikely(!ret))
1666 return print_circular_bug(&this, target_entry, next, prev);
1667 else if (unlikely(ret < 0))
1668 return print_bfs_bug(ret);
Ming Leic94aa5c2009-07-16 15:44:29 +02001669
Peter Zijlstra8e182572007-07-19 01:48:54 -07001670 if (!check_prev_add_irq(curr, prev, next))
1671 return 0;
1672
1673 /*
1674 * For recursive read-locks we do all the dependency checks,
1675 * but we dont store read-triggered dependencies (only
1676 * write-triggered dependencies). This ensures that only the
1677 * write-side dependencies matter, and that if for example a
1678 * write-lock never takes any other locks, then the reads are
1679 * equivalent to a NOP.
1680 */
1681 if (next->read == 2 || prev->read == 2)
1682 return 1;
1683 /*
1684 * Is the <prev> -> <next> dependency already present?
1685 *
1686 * (this may occur even though this is a new chain: consider
1687 * e.g. the L1 -> L2 -> L3 -> L4 and the L5 -> L1 -> L2 -> L3
1688 * chains - the second one will be new, but L1 already has
1689 * L2 added to its dependency list, due to the first chain.)
1690 */
Dave Jonesf82b2172008-08-11 09:30:23 +02001691 list_for_each_entry(entry, &hlock_class(prev)->locks_after, entry) {
1692 if (entry->class == hlock_class(next)) {
Peter Zijlstra8e182572007-07-19 01:48:54 -07001693 if (distance == 1)
1694 entry->distance = 1;
1695 return 2;
1696 }
1697 }
1698
1699 /*
1700 * Ok, all validations passed, add the new lock
1701 * to the previous lock's dependency list:
1702 */
Dave Jonesf82b2172008-08-11 09:30:23 +02001703 ret = add_lock_to_list(hlock_class(prev), hlock_class(next),
1704 &hlock_class(prev)->locks_after,
1705 next->acquire_ip, distance);
Peter Zijlstra8e182572007-07-19 01:48:54 -07001706
1707 if (!ret)
1708 return 0;
1709
Dave Jonesf82b2172008-08-11 09:30:23 +02001710 ret = add_lock_to_list(hlock_class(next), hlock_class(prev),
1711 &hlock_class(next)->locks_before,
1712 next->acquire_ip, distance);
Peter Zijlstra8e182572007-07-19 01:48:54 -07001713 if (!ret)
1714 return 0;
1715
1716 /*
1717 * Debugging printouts:
1718 */
Dave Jonesf82b2172008-08-11 09:30:23 +02001719 if (verbose(hlock_class(prev)) || verbose(hlock_class(next))) {
Peter Zijlstra8e182572007-07-19 01:48:54 -07001720 graph_unlock();
1721 printk("\n new dependency: ");
Dave Jonesf82b2172008-08-11 09:30:23 +02001722 print_lock_name(hlock_class(prev));
Peter Zijlstra8e182572007-07-19 01:48:54 -07001723 printk(" => ");
Dave Jonesf82b2172008-08-11 09:30:23 +02001724 print_lock_name(hlock_class(next));
Peter Zijlstra8e182572007-07-19 01:48:54 -07001725 printk("\n");
1726 dump_stack();
1727 return graph_lock();
1728 }
1729 return 1;
1730}
1731
1732/*
1733 * Add the dependency to all directly-previous locks that are 'relevant'.
1734 * The ones that are relevant are (in increasing distance from curr):
1735 * all consecutive trylock entries and the final non-trylock entry - or
1736 * the end of this context's lock-chain - whichever comes first.
1737 */
1738static int
1739check_prevs_add(struct task_struct *curr, struct held_lock *next)
1740{
1741 int depth = curr->lockdep_depth;
1742 struct held_lock *hlock;
1743
1744 /*
1745 * Debugging checks.
1746 *
1747 * Depth must not be zero for a non-head lock:
1748 */
1749 if (!depth)
1750 goto out_bug;
1751 /*
1752 * At least two relevant locks must exist for this
1753 * to be a head:
1754 */
1755 if (curr->held_locks[depth].irq_context !=
1756 curr->held_locks[depth-1].irq_context)
1757 goto out_bug;
1758
1759 for (;;) {
1760 int distance = curr->lockdep_depth - depth + 1;
1761 hlock = curr->held_locks + depth-1;
1762 /*
1763 * Only non-recursive-read entries get new dependencies
1764 * added:
1765 */
1766 if (hlock->read != 2) {
1767 if (!check_prev_add(curr, hlock, next, distance))
1768 return 0;
1769 /*
1770 * Stop after the first non-trylock entry,
1771 * as non-trylock entries have added their
1772 * own direct dependencies already, so this
1773 * lock is connected to them indirectly:
1774 */
1775 if (!hlock->trylock)
1776 break;
1777 }
1778 depth--;
1779 /*
1780 * End of lock-stack?
1781 */
1782 if (!depth)
1783 break;
1784 /*
1785 * Stop the search if we cross into another context:
1786 */
1787 if (curr->held_locks[depth].irq_context !=
1788 curr->held_locks[depth-1].irq_context)
1789 break;
1790 }
1791 return 1;
1792out_bug:
1793 if (!debug_locks_off_graph_unlock())
1794 return 0;
1795
1796 WARN_ON(1);
1797
1798 return 0;
1799}
1800
1801unsigned long nr_lock_chains;
Huang, Ying443cd502008-06-20 16:39:21 +08001802struct lock_chain lock_chains[MAX_LOCKDEP_CHAINS];
Huang, Yingcd1a28e2008-06-23 11:20:54 +08001803int nr_chain_hlocks;
Huang, Ying443cd502008-06-20 16:39:21 +08001804static u16 chain_hlocks[MAX_LOCKDEP_CHAIN_HLOCKS];
1805
1806struct lock_class *lock_chain_get_class(struct lock_chain *chain, int i)
1807{
1808 return lock_classes + chain_hlocks[chain->base + i];
1809}
Peter Zijlstra8e182572007-07-19 01:48:54 -07001810
1811/*
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001812 * Look up a dependency chain. If the key is not present yet then
Jarek Poplawski9e860d02007-05-08 00:30:12 -07001813 * add it and return 1 - in this case the new dependency chain is
1814 * validated. If the key is already hashed, return 0.
1815 * (On return with 1 graph_lock is held.)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001816 */
Huang, Ying443cd502008-06-20 16:39:21 +08001817static inline int lookup_chain_cache(struct task_struct *curr,
1818 struct held_lock *hlock,
1819 u64 chain_key)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001820{
Dave Jonesf82b2172008-08-11 09:30:23 +02001821 struct lock_class *class = hlock_class(hlock);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001822 struct list_head *hash_head = chainhashentry(chain_key);
1823 struct lock_chain *chain;
Huang, Ying443cd502008-06-20 16:39:21 +08001824 struct held_lock *hlock_curr, *hlock_next;
Huang, Yingcd1a28e2008-06-23 11:20:54 +08001825 int i, j, n, cn;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001826
Jarek Poplawski381a2292007-02-10 01:44:58 -08001827 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
1828 return 0;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001829 /*
1830 * We can walk it lock-free, because entries only get added
1831 * to the hash:
1832 */
1833 list_for_each_entry(chain, hash_head, entry) {
1834 if (chain->chain_key == chain_key) {
1835cache_hit:
1836 debug_atomic_inc(&chain_lookup_hits);
Ingo Molnar81fc6852006-12-13 00:34:40 -08001837 if (very_verbose(class))
Andrew Morton755cd902006-12-29 16:49:14 -08001838 printk("\nhash chain already cached, key: "
1839 "%016Lx tail class: [%p] %s\n",
1840 (unsigned long long)chain_key,
1841 class->key, class->name);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001842 return 0;
1843 }
1844 }
Ingo Molnar81fc6852006-12-13 00:34:40 -08001845 if (very_verbose(class))
Andrew Morton755cd902006-12-29 16:49:14 -08001846 printk("\nnew hash chain, key: %016Lx tail class: [%p] %s\n",
1847 (unsigned long long)chain_key, class->key, class->name);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001848 /*
1849 * Allocate a new chain entry from the static array, and add
1850 * it to the hash:
1851 */
Ingo Molnar74c383f2006-12-13 00:34:43 -08001852 if (!graph_lock())
1853 return 0;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001854 /*
1855 * We have to walk the chain again locked - to avoid duplicates:
1856 */
1857 list_for_each_entry(chain, hash_head, entry) {
1858 if (chain->chain_key == chain_key) {
Ingo Molnar74c383f2006-12-13 00:34:43 -08001859 graph_unlock();
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001860 goto cache_hit;
1861 }
1862 }
1863 if (unlikely(nr_lock_chains >= MAX_LOCKDEP_CHAINS)) {
Ingo Molnar74c383f2006-12-13 00:34:43 -08001864 if (!debug_locks_off_graph_unlock())
1865 return 0;
1866
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001867 printk("BUG: MAX_LOCKDEP_CHAINS too low!\n");
1868 printk("turning off the locking correctness validator.\n");
Peter Zijlstraeedeeab2009-03-18 12:38:47 +01001869 dump_stack();
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001870 return 0;
1871 }
1872 chain = lock_chains + nr_lock_chains++;
1873 chain->chain_key = chain_key;
Huang, Ying443cd502008-06-20 16:39:21 +08001874 chain->irq_context = hlock->irq_context;
1875 /* Find the first held_lock of current chain */
1876 hlock_next = hlock;
1877 for (i = curr->lockdep_depth - 1; i >= 0; i--) {
1878 hlock_curr = curr->held_locks + i;
1879 if (hlock_curr->irq_context != hlock_next->irq_context)
1880 break;
1881 hlock_next = hlock;
1882 }
1883 i++;
1884 chain->depth = curr->lockdep_depth + 1 - i;
Huang, Yingcd1a28e2008-06-23 11:20:54 +08001885 cn = nr_chain_hlocks;
1886 while (cn + chain->depth <= MAX_LOCKDEP_CHAIN_HLOCKS) {
1887 n = cmpxchg(&nr_chain_hlocks, cn, cn + chain->depth);
1888 if (n == cn)
1889 break;
1890 cn = n;
1891 }
1892 if (likely(cn + chain->depth <= MAX_LOCKDEP_CHAIN_HLOCKS)) {
1893 chain->base = cn;
Huang, Ying443cd502008-06-20 16:39:21 +08001894 for (j = 0; j < chain->depth - 1; j++, i++) {
Dave Jonesf82b2172008-08-11 09:30:23 +02001895 int lock_id = curr->held_locks[i].class_idx - 1;
Huang, Ying443cd502008-06-20 16:39:21 +08001896 chain_hlocks[chain->base + j] = lock_id;
1897 }
1898 chain_hlocks[chain->base + j] = class - lock_classes;
1899 }
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001900 list_add_tail_rcu(&chain->entry, hash_head);
1901 debug_atomic_inc(&chain_lookup_misses);
Peter Zijlstra8e182572007-07-19 01:48:54 -07001902 inc_chains();
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001903
1904 return 1;
1905}
Peter Zijlstra8e182572007-07-19 01:48:54 -07001906
1907static int validate_chain(struct task_struct *curr, struct lockdep_map *lock,
Johannes Berg4e6045f2007-10-18 23:39:55 -07001908 struct held_lock *hlock, int chain_head, u64 chain_key)
Peter Zijlstra8e182572007-07-19 01:48:54 -07001909{
1910 /*
1911 * Trylock needs to maintain the stack of held locks, but it
1912 * does not add new dependencies, because trylock can be done
1913 * in any order.
1914 *
1915 * We look up the chain_key and do the O(N^2) check and update of
1916 * the dependencies only if this is a new dependency chain.
1917 * (If lookup_chain_cache() returns with 1 it acquires
1918 * graph_lock for us)
1919 */
1920 if (!hlock->trylock && (hlock->check == 2) &&
Huang, Ying443cd502008-06-20 16:39:21 +08001921 lookup_chain_cache(curr, hlock, chain_key)) {
Peter Zijlstra8e182572007-07-19 01:48:54 -07001922 /*
1923 * Check whether last held lock:
1924 *
1925 * - is irq-safe, if this lock is irq-unsafe
1926 * - is softirq-safe, if this lock is hardirq-unsafe
1927 *
1928 * And check whether the new lock's dependency graph
1929 * could lead back to the previous lock.
1930 *
1931 * any of these scenarios could lead to a deadlock. If
1932 * All validations
1933 */
1934 int ret = check_deadlock(curr, hlock, lock, hlock->read);
1935
1936 if (!ret)
1937 return 0;
1938 /*
1939 * Mark recursive read, as we jump over it when
1940 * building dependencies (just like we jump over
1941 * trylock entries):
1942 */
1943 if (ret == 2)
1944 hlock->read = 2;
1945 /*
1946 * Add dependency only if this lock is not the head
1947 * of the chain, and if it's not a secondary read-lock:
1948 */
1949 if (!chain_head && ret != 2)
1950 if (!check_prevs_add(curr, hlock))
1951 return 0;
1952 graph_unlock();
1953 } else
1954 /* after lookup_chain_cache(): */
1955 if (unlikely(!debug_locks))
1956 return 0;
1957
1958 return 1;
1959}
1960#else
1961static inline int validate_chain(struct task_struct *curr,
1962 struct lockdep_map *lock, struct held_lock *hlock,
Gregory Haskins3aa416b2007-10-11 22:11:11 +02001963 int chain_head, u64 chain_key)
Peter Zijlstra8e182572007-07-19 01:48:54 -07001964{
1965 return 1;
1966}
Peter Zijlstraca58abc2007-07-19 01:48:53 -07001967#endif
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001968
1969/*
1970 * We are building curr_chain_key incrementally, so double-check
1971 * it from scratch, to make sure that it's done correctly:
1972 */
Steven Rostedt1d09daa2008-05-12 21:20:55 +02001973static void check_chain_key(struct task_struct *curr)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001974{
1975#ifdef CONFIG_DEBUG_LOCKDEP
1976 struct held_lock *hlock, *prev_hlock = NULL;
1977 unsigned int i, id;
1978 u64 chain_key = 0;
1979
1980 for (i = 0; i < curr->lockdep_depth; i++) {
1981 hlock = curr->held_locks + i;
1982 if (chain_key != hlock->prev_chain_key) {
1983 debug_locks_off();
Arjan van de Ven2df8b1d2008-07-30 12:43:11 -07001984 WARN(1, "hm#1, depth: %u [%u], %016Lx != %016Lx\n",
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001985 curr->lockdep_depth, i,
1986 (unsigned long long)chain_key,
1987 (unsigned long long)hlock->prev_chain_key);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001988 return;
1989 }
Dave Jonesf82b2172008-08-11 09:30:23 +02001990 id = hlock->class_idx - 1;
Jarek Poplawski381a2292007-02-10 01:44:58 -08001991 if (DEBUG_LOCKS_WARN_ON(id >= MAX_LOCKDEP_KEYS))
1992 return;
1993
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001994 if (prev_hlock && (prev_hlock->irq_context !=
1995 hlock->irq_context))
1996 chain_key = 0;
1997 chain_key = iterate_chain_key(chain_key, id);
1998 prev_hlock = hlock;
1999 }
2000 if (chain_key != curr->curr_chain_key) {
2001 debug_locks_off();
Arjan van de Ven2df8b1d2008-07-30 12:43:11 -07002002 WARN(1, "hm#2, depth: %u [%u], %016Lx != %016Lx\n",
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002003 curr->lockdep_depth, i,
2004 (unsigned long long)chain_key,
2005 (unsigned long long)curr->curr_chain_key);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002006 }
2007#endif
2008}
2009
Peter Zijlstra8e182572007-07-19 01:48:54 -07002010static int
2011print_usage_bug(struct task_struct *curr, struct held_lock *this,
2012 enum lock_usage_bit prev_bit, enum lock_usage_bit new_bit)
2013{
2014 if (!debug_locks_off_graph_unlock() || debug_locks_silent)
2015 return 0;
2016
2017 printk("\n=================================\n");
2018 printk( "[ INFO: inconsistent lock state ]\n");
2019 print_kernel_version();
2020 printk( "---------------------------------\n");
2021
2022 printk("inconsistent {%s} -> {%s} usage.\n",
2023 usage_str[prev_bit], usage_str[new_bit]);
2024
2025 printk("%s/%d [HC%u[%lu]:SC%u[%lu]:HE%u:SE%u] takes:\n",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07002026 curr->comm, task_pid_nr(curr),
Peter Zijlstra8e182572007-07-19 01:48:54 -07002027 trace_hardirq_context(curr), hardirq_count() >> HARDIRQ_SHIFT,
2028 trace_softirq_context(curr), softirq_count() >> SOFTIRQ_SHIFT,
2029 trace_hardirqs_enabled(curr),
2030 trace_softirqs_enabled(curr));
2031 print_lock(this);
2032
2033 printk("{%s} state was registered at:\n", usage_str[prev_bit]);
Dave Jonesf82b2172008-08-11 09:30:23 +02002034 print_stack_trace(hlock_class(this)->usage_traces + prev_bit, 1);
Peter Zijlstra8e182572007-07-19 01:48:54 -07002035
2036 print_irqtrace_events(curr);
2037 printk("\nother info that might help us debug this:\n");
2038 lockdep_print_held_locks(curr);
2039
2040 printk("\nstack backtrace:\n");
2041 dump_stack();
2042
2043 return 0;
2044}
2045
2046/*
2047 * Print out an error if an invalid bit is set:
2048 */
2049static inline int
2050valid_state(struct task_struct *curr, struct held_lock *this,
2051 enum lock_usage_bit new_bit, enum lock_usage_bit bad_bit)
2052{
Dave Jonesf82b2172008-08-11 09:30:23 +02002053 if (unlikely(hlock_class(this)->usage_mask & (1 << bad_bit)))
Peter Zijlstra8e182572007-07-19 01:48:54 -07002054 return print_usage_bug(curr, this, bad_bit, new_bit);
2055 return 1;
2056}
2057
2058static int mark_lock(struct task_struct *curr, struct held_lock *this,
2059 enum lock_usage_bit new_bit);
2060
Steven Rostedt81d68a92008-05-12 21:20:42 +02002061#if defined(CONFIG_TRACE_IRQFLAGS) && defined(CONFIG_PROVE_LOCKING)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002062
2063/*
2064 * print irq inversion bug:
2065 */
2066static int
Ming Lei24208ca2009-07-16 15:44:29 +02002067print_irq_inversion_bug(struct task_struct *curr,
2068 struct lock_list *root, struct lock_list *other,
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002069 struct held_lock *this, int forwards,
2070 const char *irqclass)
2071{
Ingo Molnar74c383f2006-12-13 00:34:43 -08002072 if (!debug_locks_off_graph_unlock() || debug_locks_silent)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002073 return 0;
2074
2075 printk("\n=========================================================\n");
2076 printk( "[ INFO: possible irq lock inversion dependency detected ]\n");
Dave Jones99de0552006-09-29 02:00:10 -07002077 print_kernel_version();
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002078 printk( "---------------------------------------------------------\n");
2079 printk("%s/%d just changed the state of lock:\n",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07002080 curr->comm, task_pid_nr(curr));
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002081 print_lock(this);
2082 if (forwards)
Peter Zijlstra26575e22009-03-04 14:53:24 +01002083 printk("but this lock took another, %s-unsafe lock in the past:\n", irqclass);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002084 else
Peter Zijlstra26575e22009-03-04 14:53:24 +01002085 printk("but this lock was taken by another, %s-safe lock in the past:\n", irqclass);
Ming Lei24208ca2009-07-16 15:44:29 +02002086 print_lock_name(other->class);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002087 printk("\n\nand interrupts could create inverse lock ordering between them.\n\n");
2088
2089 printk("\nother info that might help us debug this:\n");
2090 lockdep_print_held_locks(curr);
2091
Ming Lei24208ca2009-07-16 15:44:29 +02002092 printk("\nthe shortest dependencies between 2nd lock and 1st lock:\n");
2093 if (!save_trace(&root->trace))
2094 return 0;
2095 print_shortest_lock_dependencies(other, root);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002096
2097 printk("\nstack backtrace:\n");
2098 dump_stack();
2099
2100 return 0;
2101}
2102
2103/*
2104 * Prove that in the forwards-direction subgraph starting at <this>
2105 * there is no lock matching <mask>:
2106 */
2107static int
2108check_usage_forwards(struct task_struct *curr, struct held_lock *this,
2109 enum lock_usage_bit bit, const char *irqclass)
2110{
2111 int ret;
Ming Leid7aaba12009-07-16 15:44:29 +02002112 struct lock_list root;
2113 struct lock_list *uninitialized_var(target_entry);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002114
Ming Leid7aaba12009-07-16 15:44:29 +02002115 root.parent = NULL;
2116 root.class = hlock_class(this);
2117 ret = find_usage_forwards(&root, bit, &target_entry);
Peter Zijlstraaf012962009-07-16 15:44:29 +02002118 if (ret < 0)
2119 return print_bfs_bug(ret);
2120 if (ret == 1)
2121 return ret;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002122
Ming Lei24208ca2009-07-16 15:44:29 +02002123 return print_irq_inversion_bug(curr, &root, target_entry,
Ming Leid7aaba12009-07-16 15:44:29 +02002124 this, 1, irqclass);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002125}
2126
2127/*
2128 * Prove that in the backwards-direction subgraph starting at <this>
2129 * there is no lock matching <mask>:
2130 */
2131static int
2132check_usage_backwards(struct task_struct *curr, struct held_lock *this,
2133 enum lock_usage_bit bit, const char *irqclass)
2134{
2135 int ret;
Ming Leid7aaba12009-07-16 15:44:29 +02002136 struct lock_list root;
2137 struct lock_list *uninitialized_var(target_entry);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002138
Ming Leid7aaba12009-07-16 15:44:29 +02002139 root.parent = NULL;
2140 root.class = hlock_class(this);
2141 ret = find_usage_backwards(&root, bit, &target_entry);
Peter Zijlstraaf012962009-07-16 15:44:29 +02002142 if (ret < 0)
2143 return print_bfs_bug(ret);
2144 if (ret == 1)
2145 return ret;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002146
Ming Lei24208ca2009-07-16 15:44:29 +02002147 return print_irq_inversion_bug(curr, &root, target_entry,
Ming Leid7aaba12009-07-16 15:44:29 +02002148 this, 1, irqclass);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002149}
2150
Ingo Molnar3117df02006-12-13 00:34:43 -08002151void print_irqtrace_events(struct task_struct *curr)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002152{
2153 printk("irq event stamp: %u\n", curr->irq_events);
2154 printk("hardirqs last enabled at (%u): ", curr->hardirq_enable_event);
2155 print_ip_sym(curr->hardirq_enable_ip);
2156 printk("hardirqs last disabled at (%u): ", curr->hardirq_disable_event);
2157 print_ip_sym(curr->hardirq_disable_ip);
2158 printk("softirqs last enabled at (%u): ", curr->softirq_enable_event);
2159 print_ip_sym(curr->softirq_enable_ip);
2160 printk("softirqs last disabled at (%u): ", curr->softirq_disable_event);
2161 print_ip_sym(curr->softirq_disable_ip);
2162}
2163
Peter Zijlstracd953022009-01-22 16:38:21 +01002164static int HARDIRQ_verbose(struct lock_class *class)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002165{
Peter Zijlstra8e182572007-07-19 01:48:54 -07002166#if HARDIRQ_VERBOSE
2167 return class_filter(class);
2168#endif
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002169 return 0;
2170}
2171
Peter Zijlstracd953022009-01-22 16:38:21 +01002172static int SOFTIRQ_verbose(struct lock_class *class)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002173{
Peter Zijlstra8e182572007-07-19 01:48:54 -07002174#if SOFTIRQ_VERBOSE
2175 return class_filter(class);
2176#endif
2177 return 0;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002178}
2179
Peter Zijlstracd953022009-01-22 16:38:21 +01002180static int RECLAIM_FS_verbose(struct lock_class *class)
Nick Piggincf40bd12009-01-21 08:12:39 +01002181{
2182#if RECLAIM_VERBOSE
2183 return class_filter(class);
2184#endif
2185 return 0;
2186}
2187
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002188#define STRICT_READ_CHECKS 1
2189
Peter Zijlstracd953022009-01-22 16:38:21 +01002190static int (*state_verbose_f[])(struct lock_class *class) = {
2191#define LOCKDEP_STATE(__STATE) \
2192 __STATE##_verbose,
2193#include "lockdep_states.h"
2194#undef LOCKDEP_STATE
2195};
2196
2197static inline int state_verbose(enum lock_usage_bit bit,
2198 struct lock_class *class)
2199{
2200 return state_verbose_f[bit >> 2](class);
2201}
2202
Peter Zijlstra42c50d52009-01-22 16:58:16 +01002203typedef int (*check_usage_f)(struct task_struct *, struct held_lock *,
2204 enum lock_usage_bit bit, const char *name);
2205
Peter Zijlstra6a6904d2009-01-22 16:07:44 +01002206static int
Peter Zijlstra1c21f142009-03-04 13:51:13 +01002207mark_lock_irq(struct task_struct *curr, struct held_lock *this,
2208 enum lock_usage_bit new_bit)
Peter Zijlstra6a6904d2009-01-22 16:07:44 +01002209{
Peter Zijlstraf9892092009-01-22 16:09:59 +01002210 int excl_bit = exclusive_bit(new_bit);
Peter Zijlstra9d3651a2009-01-22 17:18:32 +01002211 int read = new_bit & 1;
Peter Zijlstra42c50d52009-01-22 16:58:16 +01002212 int dir = new_bit & 2;
2213
Peter Zijlstra38aa2712009-01-27 14:53:50 +01002214 /*
2215 * mark USED_IN has to look forwards -- to ensure no dependency
2216 * has ENABLED state, which would allow recursion deadlocks.
2217 *
2218 * mark ENABLED has to look backwards -- to ensure no dependee
2219 * has USED_IN state, which, again, would allow recursion deadlocks.
2220 */
Peter Zijlstra42c50d52009-01-22 16:58:16 +01002221 check_usage_f usage = dir ?
2222 check_usage_backwards : check_usage_forwards;
Peter Zijlstraf9892092009-01-22 16:09:59 +01002223
Peter Zijlstra38aa2712009-01-27 14:53:50 +01002224 /*
2225 * Validate that this particular lock does not have conflicting
2226 * usage states.
2227 */
Peter Zijlstra6a6904d2009-01-22 16:07:44 +01002228 if (!valid_state(curr, this, new_bit, excl_bit))
2229 return 0;
Peter Zijlstra9d3651a2009-01-22 17:18:32 +01002230
Peter Zijlstra38aa2712009-01-27 14:53:50 +01002231 /*
2232 * Validate that the lock dependencies don't have conflicting usage
2233 * states.
2234 */
2235 if ((!read || !dir || STRICT_READ_CHECKS) &&
Peter Zijlstra1c21f142009-03-04 13:51:13 +01002236 !usage(curr, this, excl_bit, state_name(new_bit & ~1)))
Peter Zijlstra6a6904d2009-01-22 16:07:44 +01002237 return 0;
Peter Zijlstra780e8202009-01-22 16:51:29 +01002238
Peter Zijlstra38aa2712009-01-27 14:53:50 +01002239 /*
2240 * Check for read in write conflicts
2241 */
2242 if (!read) {
2243 if (!valid_state(curr, this, new_bit, excl_bit + 1))
2244 return 0;
2245
2246 if (STRICT_READ_CHECKS &&
Peter Zijlstra4f367d8a2009-01-22 18:10:42 +01002247 !usage(curr, this, excl_bit + 1,
2248 state_name(new_bit + 1)))
Peter Zijlstra38aa2712009-01-27 14:53:50 +01002249 return 0;
2250 }
Peter Zijlstra780e8202009-01-22 16:51:29 +01002251
Peter Zijlstracd953022009-01-22 16:38:21 +01002252 if (state_verbose(new_bit, hlock_class(this)))
Peter Zijlstra6a6904d2009-01-22 16:07:44 +01002253 return 2;
2254
2255 return 1;
2256}
2257
Nick Piggincf40bd12009-01-21 08:12:39 +01002258enum mark_type {
Peter Zijlstra36bfb9b2009-01-22 14:12:41 +01002259#define LOCKDEP_STATE(__STATE) __STATE,
2260#include "lockdep_states.h"
2261#undef LOCKDEP_STATE
Nick Piggincf40bd12009-01-21 08:12:39 +01002262};
2263
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002264/*
2265 * Mark all held locks with a usage bit:
2266 */
Steven Rostedt1d09daa2008-05-12 21:20:55 +02002267static int
Nick Piggincf40bd12009-01-21 08:12:39 +01002268mark_held_locks(struct task_struct *curr, enum mark_type mark)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002269{
2270 enum lock_usage_bit usage_bit;
2271 struct held_lock *hlock;
2272 int i;
2273
2274 for (i = 0; i < curr->lockdep_depth; i++) {
2275 hlock = curr->held_locks + i;
2276
Peter Zijlstracf2ad4d2009-01-27 13:58:08 +01002277 usage_bit = 2 + (mark << 2); /* ENABLED */
2278 if (hlock->read)
2279 usage_bit += 1; /* READ */
2280
2281 BUG_ON(usage_bit >= LOCK_USAGE_STATES);
Nick Piggincf40bd12009-01-21 08:12:39 +01002282
Jarek Poplawski4ff773bb2007-05-08 00:31:00 -07002283 if (!mark_lock(curr, hlock, usage_bit))
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002284 return 0;
2285 }
2286
2287 return 1;
2288}
2289
2290/*
2291 * Debugging helper: via this flag we know that we are in
2292 * 'early bootup code', and will warn about any invalid irqs-on event:
2293 */
2294static int early_boot_irqs_enabled;
2295
2296void early_boot_irqs_off(void)
2297{
2298 early_boot_irqs_enabled = 0;
2299}
2300
2301void early_boot_irqs_on(void)
2302{
2303 early_boot_irqs_enabled = 1;
2304}
2305
2306/*
2307 * Hardirqs will be enabled:
2308 */
Heiko Carstens6afe40b2008-10-28 11:14:58 +01002309void trace_hardirqs_on_caller(unsigned long ip)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002310{
2311 struct task_struct *curr = current;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002312
Heiko Carstens6afe40b2008-10-28 11:14:58 +01002313 time_hardirqs_on(CALLER_ADDR0, ip);
Steven Rostedt81d68a92008-05-12 21:20:42 +02002314
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002315 if (unlikely(!debug_locks || current->lockdep_recursion))
2316 return;
2317
2318 if (DEBUG_LOCKS_WARN_ON(unlikely(!early_boot_irqs_enabled)))
2319 return;
2320
2321 if (unlikely(curr->hardirqs_enabled)) {
2322 debug_atomic_inc(&redundant_hardirqs_on);
2323 return;
2324 }
2325 /* we'll do an OFF -> ON transition: */
2326 curr->hardirqs_enabled = 1;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002327
2328 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2329 return;
2330 if (DEBUG_LOCKS_WARN_ON(current->hardirq_context))
2331 return;
2332 /*
2333 * We are going to turn hardirqs on, so set the
2334 * usage bit for all held locks:
2335 */
Nick Piggincf40bd12009-01-21 08:12:39 +01002336 if (!mark_held_locks(curr, HARDIRQ))
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002337 return;
2338 /*
2339 * If we have softirqs enabled, then set the usage
2340 * bit for all held locks. (disabled hardirqs prevented
2341 * this bit from being set before)
2342 */
2343 if (curr->softirqs_enabled)
Nick Piggincf40bd12009-01-21 08:12:39 +01002344 if (!mark_held_locks(curr, SOFTIRQ))
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002345 return;
2346
2347 curr->hardirq_enable_ip = ip;
2348 curr->hardirq_enable_event = ++curr->irq_events;
2349 debug_atomic_inc(&hardirqs_on_events);
2350}
Steven Rostedt81d68a92008-05-12 21:20:42 +02002351EXPORT_SYMBOL(trace_hardirqs_on_caller);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002352
Steven Rostedt1d09daa2008-05-12 21:20:55 +02002353void trace_hardirqs_on(void)
Steven Rostedt81d68a92008-05-12 21:20:42 +02002354{
2355 trace_hardirqs_on_caller(CALLER_ADDR0);
2356}
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002357EXPORT_SYMBOL(trace_hardirqs_on);
2358
2359/*
2360 * Hardirqs were disabled:
2361 */
Heiko Carstens6afe40b2008-10-28 11:14:58 +01002362void trace_hardirqs_off_caller(unsigned long ip)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002363{
2364 struct task_struct *curr = current;
2365
Heiko Carstens6afe40b2008-10-28 11:14:58 +01002366 time_hardirqs_off(CALLER_ADDR0, ip);
Steven Rostedt81d68a92008-05-12 21:20:42 +02002367
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002368 if (unlikely(!debug_locks || current->lockdep_recursion))
2369 return;
2370
2371 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2372 return;
2373
2374 if (curr->hardirqs_enabled) {
2375 /*
2376 * We have done an ON -> OFF transition:
2377 */
2378 curr->hardirqs_enabled = 0;
Heiko Carstens6afe40b2008-10-28 11:14:58 +01002379 curr->hardirq_disable_ip = ip;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002380 curr->hardirq_disable_event = ++curr->irq_events;
2381 debug_atomic_inc(&hardirqs_off_events);
2382 } else
2383 debug_atomic_inc(&redundant_hardirqs_off);
2384}
Steven Rostedt81d68a92008-05-12 21:20:42 +02002385EXPORT_SYMBOL(trace_hardirqs_off_caller);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002386
Steven Rostedt1d09daa2008-05-12 21:20:55 +02002387void trace_hardirqs_off(void)
Steven Rostedt81d68a92008-05-12 21:20:42 +02002388{
2389 trace_hardirqs_off_caller(CALLER_ADDR0);
2390}
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002391EXPORT_SYMBOL(trace_hardirqs_off);
2392
2393/*
2394 * Softirqs will be enabled:
2395 */
2396void trace_softirqs_on(unsigned long ip)
2397{
2398 struct task_struct *curr = current;
2399
2400 if (unlikely(!debug_locks))
2401 return;
2402
2403 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2404 return;
2405
2406 if (curr->softirqs_enabled) {
2407 debug_atomic_inc(&redundant_softirqs_on);
2408 return;
2409 }
2410
2411 /*
2412 * We'll do an OFF -> ON transition:
2413 */
2414 curr->softirqs_enabled = 1;
2415 curr->softirq_enable_ip = ip;
2416 curr->softirq_enable_event = ++curr->irq_events;
2417 debug_atomic_inc(&softirqs_on_events);
2418 /*
2419 * We are going to turn softirqs on, so set the
2420 * usage bit for all held locks, if hardirqs are
2421 * enabled too:
2422 */
2423 if (curr->hardirqs_enabled)
Nick Piggincf40bd12009-01-21 08:12:39 +01002424 mark_held_locks(curr, SOFTIRQ);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002425}
2426
2427/*
2428 * Softirqs were disabled:
2429 */
2430void trace_softirqs_off(unsigned long ip)
2431{
2432 struct task_struct *curr = current;
2433
2434 if (unlikely(!debug_locks))
2435 return;
2436
2437 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2438 return;
2439
2440 if (curr->softirqs_enabled) {
2441 /*
2442 * We have done an ON -> OFF transition:
2443 */
2444 curr->softirqs_enabled = 0;
2445 curr->softirq_disable_ip = ip;
2446 curr->softirq_disable_event = ++curr->irq_events;
2447 debug_atomic_inc(&softirqs_off_events);
2448 DEBUG_LOCKS_WARN_ON(!softirq_count());
2449 } else
2450 debug_atomic_inc(&redundant_softirqs_off);
2451}
2452
Peter Zijlstra2f850182009-03-20 11:13:20 +01002453static void __lockdep_trace_alloc(gfp_t gfp_mask, unsigned long flags)
Nick Piggincf40bd12009-01-21 08:12:39 +01002454{
2455 struct task_struct *curr = current;
2456
2457 if (unlikely(!debug_locks))
2458 return;
2459
2460 /* no reclaim without waiting on it */
2461 if (!(gfp_mask & __GFP_WAIT))
2462 return;
2463
2464 /* this guy won't enter reclaim */
2465 if ((curr->flags & PF_MEMALLOC) && !(gfp_mask & __GFP_NOMEMALLOC))
2466 return;
2467
2468 /* We're only interested __GFP_FS allocations for now */
2469 if (!(gfp_mask & __GFP_FS))
2470 return;
2471
Peter Zijlstra2f850182009-03-20 11:13:20 +01002472 if (DEBUG_LOCKS_WARN_ON(irqs_disabled_flags(flags)))
Nick Piggincf40bd12009-01-21 08:12:39 +01002473 return;
2474
2475 mark_held_locks(curr, RECLAIM_FS);
2476}
2477
Peter Zijlstra2f850182009-03-20 11:13:20 +01002478static void check_flags(unsigned long flags);
2479
2480void lockdep_trace_alloc(gfp_t gfp_mask)
2481{
2482 unsigned long flags;
2483
2484 if (unlikely(current->lockdep_recursion))
2485 return;
2486
2487 raw_local_irq_save(flags);
2488 check_flags(flags);
2489 current->lockdep_recursion = 1;
2490 __lockdep_trace_alloc(gfp_mask, flags);
2491 current->lockdep_recursion = 0;
2492 raw_local_irq_restore(flags);
2493}
2494
Peter Zijlstra8e182572007-07-19 01:48:54 -07002495static int mark_irqflags(struct task_struct *curr, struct held_lock *hlock)
2496{
2497 /*
2498 * If non-trylock use in a hardirq or softirq context, then
2499 * mark the lock as used in these contexts:
2500 */
2501 if (!hlock->trylock) {
2502 if (hlock->read) {
2503 if (curr->hardirq_context)
2504 if (!mark_lock(curr, hlock,
2505 LOCK_USED_IN_HARDIRQ_READ))
2506 return 0;
2507 if (curr->softirq_context)
2508 if (!mark_lock(curr, hlock,
2509 LOCK_USED_IN_SOFTIRQ_READ))
2510 return 0;
2511 } else {
2512 if (curr->hardirq_context)
2513 if (!mark_lock(curr, hlock, LOCK_USED_IN_HARDIRQ))
2514 return 0;
2515 if (curr->softirq_context)
2516 if (!mark_lock(curr, hlock, LOCK_USED_IN_SOFTIRQ))
2517 return 0;
2518 }
2519 }
2520 if (!hlock->hardirqs_off) {
2521 if (hlock->read) {
2522 if (!mark_lock(curr, hlock,
Peter Zijlstra4fc95e82009-01-22 13:10:52 +01002523 LOCK_ENABLED_HARDIRQ_READ))
Peter Zijlstra8e182572007-07-19 01:48:54 -07002524 return 0;
2525 if (curr->softirqs_enabled)
2526 if (!mark_lock(curr, hlock,
Peter Zijlstra4fc95e82009-01-22 13:10:52 +01002527 LOCK_ENABLED_SOFTIRQ_READ))
Peter Zijlstra8e182572007-07-19 01:48:54 -07002528 return 0;
2529 } else {
2530 if (!mark_lock(curr, hlock,
Peter Zijlstra4fc95e82009-01-22 13:10:52 +01002531 LOCK_ENABLED_HARDIRQ))
Peter Zijlstra8e182572007-07-19 01:48:54 -07002532 return 0;
2533 if (curr->softirqs_enabled)
2534 if (!mark_lock(curr, hlock,
Peter Zijlstra4fc95e82009-01-22 13:10:52 +01002535 LOCK_ENABLED_SOFTIRQ))
Peter Zijlstra8e182572007-07-19 01:48:54 -07002536 return 0;
2537 }
2538 }
2539
Nick Piggincf40bd12009-01-21 08:12:39 +01002540 /*
2541 * We reuse the irq context infrastructure more broadly as a general
2542 * context checking code. This tests GFP_FS recursion (a lock taken
2543 * during reclaim for a GFP_FS allocation is held over a GFP_FS
2544 * allocation).
2545 */
2546 if (!hlock->trylock && (curr->lockdep_reclaim_gfp & __GFP_FS)) {
2547 if (hlock->read) {
2548 if (!mark_lock(curr, hlock, LOCK_USED_IN_RECLAIM_FS_READ))
2549 return 0;
2550 } else {
2551 if (!mark_lock(curr, hlock, LOCK_USED_IN_RECLAIM_FS))
2552 return 0;
2553 }
2554 }
2555
Peter Zijlstra8e182572007-07-19 01:48:54 -07002556 return 1;
2557}
2558
2559static int separate_irq_context(struct task_struct *curr,
2560 struct held_lock *hlock)
2561{
2562 unsigned int depth = curr->lockdep_depth;
2563
2564 /*
2565 * Keep track of points where we cross into an interrupt context:
2566 */
2567 hlock->irq_context = 2*(curr->hardirq_context ? 1 : 0) +
2568 curr->softirq_context;
2569 if (depth) {
2570 struct held_lock *prev_hlock;
2571
2572 prev_hlock = curr->held_locks + depth-1;
2573 /*
2574 * If we cross into another context, reset the
2575 * hash key (this also prevents the checking and the
2576 * adding of the dependency to 'prev'):
2577 */
2578 if (prev_hlock->irq_context != hlock->irq_context)
2579 return 1;
2580 }
2581 return 0;
2582}
2583
2584#else
2585
2586static inline
2587int mark_lock_irq(struct task_struct *curr, struct held_lock *this,
2588 enum lock_usage_bit new_bit)
2589{
2590 WARN_ON(1);
2591 return 1;
2592}
2593
2594static inline int mark_irqflags(struct task_struct *curr,
2595 struct held_lock *hlock)
2596{
2597 return 1;
2598}
2599
2600static inline int separate_irq_context(struct task_struct *curr,
2601 struct held_lock *hlock)
2602{
2603 return 0;
2604}
2605
Peter Zijlstra868a23a2009-02-15 00:25:21 +01002606void lockdep_trace_alloc(gfp_t gfp_mask)
2607{
2608}
2609
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002610#endif
2611
2612/*
Peter Zijlstra8e182572007-07-19 01:48:54 -07002613 * Mark a lock with a usage bit, and validate the state transition:
2614 */
Steven Rostedt1d09daa2008-05-12 21:20:55 +02002615static int mark_lock(struct task_struct *curr, struct held_lock *this,
Steven Rostedt0764d232008-05-12 21:20:44 +02002616 enum lock_usage_bit new_bit)
Peter Zijlstra8e182572007-07-19 01:48:54 -07002617{
2618 unsigned int new_mask = 1 << new_bit, ret = 1;
2619
2620 /*
2621 * If already set then do not dirty the cacheline,
2622 * nor do any checks:
2623 */
Dave Jonesf82b2172008-08-11 09:30:23 +02002624 if (likely(hlock_class(this)->usage_mask & new_mask))
Peter Zijlstra8e182572007-07-19 01:48:54 -07002625 return 1;
2626
2627 if (!graph_lock())
2628 return 0;
2629 /*
2630 * Make sure we didnt race:
2631 */
Dave Jonesf82b2172008-08-11 09:30:23 +02002632 if (unlikely(hlock_class(this)->usage_mask & new_mask)) {
Peter Zijlstra8e182572007-07-19 01:48:54 -07002633 graph_unlock();
2634 return 1;
2635 }
2636
Dave Jonesf82b2172008-08-11 09:30:23 +02002637 hlock_class(this)->usage_mask |= new_mask;
Peter Zijlstra8e182572007-07-19 01:48:54 -07002638
Dave Jonesf82b2172008-08-11 09:30:23 +02002639 if (!save_trace(hlock_class(this)->usage_traces + new_bit))
Peter Zijlstra8e182572007-07-19 01:48:54 -07002640 return 0;
2641
2642 switch (new_bit) {
Peter Zijlstra53464172009-01-22 14:15:53 +01002643#define LOCKDEP_STATE(__STATE) \
2644 case LOCK_USED_IN_##__STATE: \
2645 case LOCK_USED_IN_##__STATE##_READ: \
2646 case LOCK_ENABLED_##__STATE: \
2647 case LOCK_ENABLED_##__STATE##_READ:
2648#include "lockdep_states.h"
2649#undef LOCKDEP_STATE
Peter Zijlstra8e182572007-07-19 01:48:54 -07002650 ret = mark_lock_irq(curr, this, new_bit);
2651 if (!ret)
2652 return 0;
2653 break;
2654 case LOCK_USED:
Peter Zijlstra8e182572007-07-19 01:48:54 -07002655 debug_atomic_dec(&nr_unused_locks);
2656 break;
2657 default:
2658 if (!debug_locks_off_graph_unlock())
2659 return 0;
2660 WARN_ON(1);
2661 return 0;
2662 }
2663
2664 graph_unlock();
2665
2666 /*
2667 * We must printk outside of the graph_lock:
2668 */
2669 if (ret == 2) {
2670 printk("\nmarked lock as {%s}:\n", usage_str[new_bit]);
2671 print_lock(this);
2672 print_irqtrace_events(curr);
2673 dump_stack();
2674 }
2675
2676 return ret;
2677}
2678
2679/*
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002680 * Initialize a lock instance's lock-class mapping info:
2681 */
2682void lockdep_init_map(struct lockdep_map *lock, const char *name,
Peter Zijlstra4dfbb9d2006-10-11 01:45:14 -04002683 struct lock_class_key *key, int subclass)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002684{
Peter Zijlstrac8a25002009-04-17 09:40:49 +02002685 lock->class_cache = NULL;
2686#ifdef CONFIG_LOCK_STAT
2687 lock->cpu = raw_smp_processor_id();
2688#endif
2689
2690 if (DEBUG_LOCKS_WARN_ON(!name)) {
2691 lock->name = "NULL";
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002692 return;
Peter Zijlstrac8a25002009-04-17 09:40:49 +02002693 }
2694
2695 lock->name = name;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002696
2697 if (DEBUG_LOCKS_WARN_ON(!key))
2698 return;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002699 /*
2700 * Sanity check, the lock-class key must be persistent:
2701 */
2702 if (!static_obj(key)) {
2703 printk("BUG: key %p not in .data!\n", key);
2704 DEBUG_LOCKS_WARN_ON(1);
2705 return;
2706 }
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002707 lock->key = key;
Peter Zijlstrac8a25002009-04-17 09:40:49 +02002708
2709 if (unlikely(!debug_locks))
2710 return;
2711
Peter Zijlstra4dfbb9d2006-10-11 01:45:14 -04002712 if (subclass)
2713 register_lock_class(lock, subclass, 1);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002714}
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002715EXPORT_SYMBOL_GPL(lockdep_init_map);
2716
2717/*
2718 * This gets called for every mutex_lock*()/spin_lock*() operation.
2719 * We maintain the dependency maps and validate the locking attempt:
2720 */
2721static int __lock_acquire(struct lockdep_map *lock, unsigned int subclass,
2722 int trylock, int read, int check, int hardirqs_off,
Peter Zijlstrabb97a912009-07-20 19:15:35 +02002723 struct lockdep_map *nest_lock, unsigned long ip,
2724 int references)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002725{
2726 struct task_struct *curr = current;
Ingo Molnard6d897c2006-07-10 04:44:04 -07002727 struct lock_class *class = NULL;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002728 struct held_lock *hlock;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002729 unsigned int depth, id;
2730 int chain_head = 0;
Peter Zijlstrabb97a912009-07-20 19:15:35 +02002731 int class_idx;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002732 u64 chain_key;
2733
Peter Zijlstraf20786f2007-07-19 01:48:56 -07002734 if (!prove_locking)
2735 check = 1;
2736
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002737 if (unlikely(!debug_locks))
2738 return 0;
2739
2740 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2741 return 0;
2742
2743 if (unlikely(subclass >= MAX_LOCKDEP_SUBCLASSES)) {
2744 debug_locks_off();
2745 printk("BUG: MAX_LOCKDEP_SUBCLASSES too low!\n");
2746 printk("turning off the locking correctness validator.\n");
Peter Zijlstraeedeeab2009-03-18 12:38:47 +01002747 dump_stack();
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002748 return 0;
2749 }
2750
Ingo Molnard6d897c2006-07-10 04:44:04 -07002751 if (!subclass)
2752 class = lock->class_cache;
2753 /*
2754 * Not cached yet or subclass?
2755 */
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002756 if (unlikely(!class)) {
Peter Zijlstra4dfbb9d2006-10-11 01:45:14 -04002757 class = register_lock_class(lock, subclass, 0);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002758 if (!class)
2759 return 0;
2760 }
2761 debug_atomic_inc((atomic_t *)&class->ops);
2762 if (very_verbose(class)) {
2763 printk("\nacquire class [%p] %s", class->key, class->name);
2764 if (class->name_version > 1)
2765 printk("#%d", class->name_version);
2766 printk("\n");
2767 dump_stack();
2768 }
2769
2770 /*
2771 * Add the lock to the list of currently held locks.
2772 * (we dont increase the depth just yet, up until the
2773 * dependency checks are done)
2774 */
2775 depth = curr->lockdep_depth;
2776 if (DEBUG_LOCKS_WARN_ON(depth >= MAX_LOCK_DEPTH))
2777 return 0;
2778
Peter Zijlstrabb97a912009-07-20 19:15:35 +02002779 class_idx = class - lock_classes + 1;
2780
2781 if (depth) {
2782 hlock = curr->held_locks + depth - 1;
2783 if (hlock->class_idx == class_idx && nest_lock) {
2784 if (hlock->references)
2785 hlock->references++;
2786 else
2787 hlock->references = 2;
2788
2789 return 1;
2790 }
2791 }
2792
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002793 hlock = curr->held_locks + depth;
Dave Jonesf82b2172008-08-11 09:30:23 +02002794 if (DEBUG_LOCKS_WARN_ON(!class))
2795 return 0;
Peter Zijlstrabb97a912009-07-20 19:15:35 +02002796 hlock->class_idx = class_idx;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002797 hlock->acquire_ip = ip;
2798 hlock->instance = lock;
Peter Zijlstra7531e2f2008-08-11 09:30:24 +02002799 hlock->nest_lock = nest_lock;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002800 hlock->trylock = trylock;
2801 hlock->read = read;
2802 hlock->check = check;
Dmitry Baryshkov6951b122008-08-18 04:26:37 +04002803 hlock->hardirqs_off = !!hardirqs_off;
Peter Zijlstrabb97a912009-07-20 19:15:35 +02002804 hlock->references = references;
Peter Zijlstraf20786f2007-07-19 01:48:56 -07002805#ifdef CONFIG_LOCK_STAT
2806 hlock->waittime_stamp = 0;
Peter Zijlstra3365e7792009-10-09 10:12:41 +02002807 hlock->holdtime_stamp = lockstat_clock();
Peter Zijlstraf20786f2007-07-19 01:48:56 -07002808#endif
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002809
Peter Zijlstra8e182572007-07-19 01:48:54 -07002810 if (check == 2 && !mark_irqflags(curr, hlock))
2811 return 0;
2812
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002813 /* mark it as used: */
Jarek Poplawski4ff773bb2007-05-08 00:31:00 -07002814 if (!mark_lock(curr, hlock, LOCK_USED))
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002815 return 0;
Peter Zijlstra8e182572007-07-19 01:48:54 -07002816
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002817 /*
Gautham R Shenoy17aacfb2007-10-28 20:47:01 +01002818 * Calculate the chain hash: it's the combined hash of all the
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002819 * lock keys along the dependency chain. We save the hash value
2820 * at every step so that we can get the current hash easily
2821 * after unlock. The chain hash is then used to cache dependency
2822 * results.
2823 *
2824 * The 'key ID' is what is the most compact key value to drive
2825 * the hash, not class->key.
2826 */
2827 id = class - lock_classes;
2828 if (DEBUG_LOCKS_WARN_ON(id >= MAX_LOCKDEP_KEYS))
2829 return 0;
2830
2831 chain_key = curr->curr_chain_key;
2832 if (!depth) {
2833 if (DEBUG_LOCKS_WARN_ON(chain_key != 0))
2834 return 0;
2835 chain_head = 1;
2836 }
2837
2838 hlock->prev_chain_key = chain_key;
Peter Zijlstra8e182572007-07-19 01:48:54 -07002839 if (separate_irq_context(curr, hlock)) {
2840 chain_key = 0;
2841 chain_head = 1;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002842 }
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002843 chain_key = iterate_chain_key(chain_key, id);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002844
Gregory Haskins3aa416b2007-10-11 22:11:11 +02002845 if (!validate_chain(curr, lock, hlock, chain_head, chain_key))
Peter Zijlstra8e182572007-07-19 01:48:54 -07002846 return 0;
Jarek Poplawski381a2292007-02-10 01:44:58 -08002847
Gregory Haskins3aa416b2007-10-11 22:11:11 +02002848 curr->curr_chain_key = chain_key;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002849 curr->lockdep_depth++;
2850 check_chain_key(curr);
Jarek Poplawski60e114d2007-02-20 13:58:00 -08002851#ifdef CONFIG_DEBUG_LOCKDEP
2852 if (unlikely(!debug_locks))
2853 return 0;
2854#endif
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002855 if (unlikely(curr->lockdep_depth >= MAX_LOCK_DEPTH)) {
2856 debug_locks_off();
2857 printk("BUG: MAX_LOCK_DEPTH too low!\n");
2858 printk("turning off the locking correctness validator.\n");
Peter Zijlstraeedeeab2009-03-18 12:38:47 +01002859 dump_stack();
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002860 return 0;
2861 }
Jarek Poplawski381a2292007-02-10 01:44:58 -08002862
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002863 if (unlikely(curr->lockdep_depth > max_lockdep_depth))
2864 max_lockdep_depth = curr->lockdep_depth;
2865
2866 return 1;
2867}
2868
2869static int
2870print_unlock_inbalance_bug(struct task_struct *curr, struct lockdep_map *lock,
2871 unsigned long ip)
2872{
2873 if (!debug_locks_off())
2874 return 0;
2875 if (debug_locks_silent)
2876 return 0;
2877
2878 printk("\n=====================================\n");
2879 printk( "[ BUG: bad unlock balance detected! ]\n");
2880 printk( "-------------------------------------\n");
2881 printk("%s/%d is trying to release lock (",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07002882 curr->comm, task_pid_nr(curr));
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002883 print_lockdep_cache(lock);
2884 printk(") at:\n");
2885 print_ip_sym(ip);
2886 printk("but there are no more locks to release!\n");
2887 printk("\nother info that might help us debug this:\n");
2888 lockdep_print_held_locks(curr);
2889
2890 printk("\nstack backtrace:\n");
2891 dump_stack();
2892
2893 return 0;
2894}
2895
2896/*
2897 * Common debugging checks for both nested and non-nested unlock:
2898 */
2899static int check_unlock(struct task_struct *curr, struct lockdep_map *lock,
2900 unsigned long ip)
2901{
2902 if (unlikely(!debug_locks))
2903 return 0;
2904 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2905 return 0;
2906
2907 if (curr->lockdep_depth <= 0)
2908 return print_unlock_inbalance_bug(curr, lock, ip);
2909
2910 return 1;
2911}
2912
Peter Zijlstrabb97a912009-07-20 19:15:35 +02002913static int match_held_lock(struct held_lock *hlock, struct lockdep_map *lock)
2914{
2915 if (hlock->instance == lock)
2916 return 1;
2917
2918 if (hlock->references) {
2919 struct lock_class *class = lock->class_cache;
2920
2921 if (!class)
2922 class = look_up_lock_class(lock, 0);
2923
2924 if (DEBUG_LOCKS_WARN_ON(!class))
2925 return 0;
2926
2927 if (DEBUG_LOCKS_WARN_ON(!hlock->nest_lock))
2928 return 0;
2929
2930 if (hlock->class_idx == class - lock_classes + 1)
2931 return 1;
2932 }
2933
2934 return 0;
2935}
2936
Peter Zijlstra64aa3482008-08-11 09:30:21 +02002937static int
Peter Zijlstra00ef9f72008-12-04 09:00:17 +01002938__lock_set_class(struct lockdep_map *lock, const char *name,
2939 struct lock_class_key *key, unsigned int subclass,
2940 unsigned long ip)
Peter Zijlstra64aa3482008-08-11 09:30:21 +02002941{
2942 struct task_struct *curr = current;
2943 struct held_lock *hlock, *prev_hlock;
2944 struct lock_class *class;
2945 unsigned int depth;
2946 int i;
2947
2948 depth = curr->lockdep_depth;
2949 if (DEBUG_LOCKS_WARN_ON(!depth))
2950 return 0;
2951
2952 prev_hlock = NULL;
2953 for (i = depth-1; i >= 0; i--) {
2954 hlock = curr->held_locks + i;
2955 /*
2956 * We must not cross into another context:
2957 */
2958 if (prev_hlock && prev_hlock->irq_context != hlock->irq_context)
2959 break;
Peter Zijlstrabb97a912009-07-20 19:15:35 +02002960 if (match_held_lock(hlock, lock))
Peter Zijlstra64aa3482008-08-11 09:30:21 +02002961 goto found_it;
2962 prev_hlock = hlock;
2963 }
2964 return print_unlock_inbalance_bug(curr, lock, ip);
2965
2966found_it:
Peter Zijlstra00ef9f72008-12-04 09:00:17 +01002967 lockdep_init_map(lock, name, key, 0);
Peter Zijlstra64aa3482008-08-11 09:30:21 +02002968 class = register_lock_class(lock, subclass, 0);
Dave Jonesf82b2172008-08-11 09:30:23 +02002969 hlock->class_idx = class - lock_classes + 1;
Peter Zijlstra64aa3482008-08-11 09:30:21 +02002970
2971 curr->lockdep_depth = i;
2972 curr->curr_chain_key = hlock->prev_chain_key;
2973
2974 for (; i < depth; i++) {
2975 hlock = curr->held_locks + i;
2976 if (!__lock_acquire(hlock->instance,
Dave Jonesf82b2172008-08-11 09:30:23 +02002977 hlock_class(hlock)->subclass, hlock->trylock,
Peter Zijlstra64aa3482008-08-11 09:30:21 +02002978 hlock->read, hlock->check, hlock->hardirqs_off,
Peter Zijlstrabb97a912009-07-20 19:15:35 +02002979 hlock->nest_lock, hlock->acquire_ip,
2980 hlock->references))
Peter Zijlstra64aa3482008-08-11 09:30:21 +02002981 return 0;
2982 }
2983
2984 if (DEBUG_LOCKS_WARN_ON(curr->lockdep_depth != depth))
2985 return 0;
2986 return 1;
2987}
2988
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002989/*
2990 * Remove the lock to the list of currently held locks in a
2991 * potentially non-nested (out of order) manner. This is a
2992 * relatively rare operation, as all the unlock APIs default
2993 * to nested mode (which uses lock_release()):
2994 */
2995static int
2996lock_release_non_nested(struct task_struct *curr,
2997 struct lockdep_map *lock, unsigned long ip)
2998{
2999 struct held_lock *hlock, *prev_hlock;
3000 unsigned int depth;
3001 int i;
3002
3003 /*
3004 * Check whether the lock exists in the current stack
3005 * of held locks:
3006 */
3007 depth = curr->lockdep_depth;
3008 if (DEBUG_LOCKS_WARN_ON(!depth))
3009 return 0;
3010
3011 prev_hlock = NULL;
3012 for (i = depth-1; i >= 0; i--) {
3013 hlock = curr->held_locks + i;
3014 /*
3015 * We must not cross into another context:
3016 */
3017 if (prev_hlock && prev_hlock->irq_context != hlock->irq_context)
3018 break;
Peter Zijlstrabb97a912009-07-20 19:15:35 +02003019 if (match_held_lock(hlock, lock))
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003020 goto found_it;
3021 prev_hlock = hlock;
3022 }
3023 return print_unlock_inbalance_bug(curr, lock, ip);
3024
3025found_it:
Peter Zijlstrabb97a912009-07-20 19:15:35 +02003026 if (hlock->instance == lock)
3027 lock_release_holdtime(hlock);
3028
3029 if (hlock->references) {
3030 hlock->references--;
3031 if (hlock->references) {
3032 /*
3033 * We had, and after removing one, still have
3034 * references, the current lock stack is still
3035 * valid. We're done!
3036 */
3037 return 1;
3038 }
3039 }
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003040
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003041 /*
3042 * We have the right lock to unlock, 'hlock' points to it.
3043 * Now we remove it from the stack, and add back the other
3044 * entries (if any), recalculating the hash along the way:
3045 */
Peter Zijlstrabb97a912009-07-20 19:15:35 +02003046
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003047 curr->lockdep_depth = i;
3048 curr->curr_chain_key = hlock->prev_chain_key;
3049
3050 for (i++; i < depth; i++) {
3051 hlock = curr->held_locks + i;
3052 if (!__lock_acquire(hlock->instance,
Dave Jonesf82b2172008-08-11 09:30:23 +02003053 hlock_class(hlock)->subclass, hlock->trylock,
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003054 hlock->read, hlock->check, hlock->hardirqs_off,
Peter Zijlstrabb97a912009-07-20 19:15:35 +02003055 hlock->nest_lock, hlock->acquire_ip,
3056 hlock->references))
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003057 return 0;
3058 }
3059
3060 if (DEBUG_LOCKS_WARN_ON(curr->lockdep_depth != depth - 1))
3061 return 0;
3062 return 1;
3063}
3064
3065/*
3066 * Remove the lock to the list of currently held locks - this gets
3067 * called on mutex_unlock()/spin_unlock*() (or on a failed
3068 * mutex_lock_interruptible()). This is done for unlocks that nest
3069 * perfectly. (i.e. the current top of the lock-stack is unlocked)
3070 */
3071static int lock_release_nested(struct task_struct *curr,
3072 struct lockdep_map *lock, unsigned long ip)
3073{
3074 struct held_lock *hlock;
3075 unsigned int depth;
3076
3077 /*
3078 * Pop off the top of the lock stack:
3079 */
3080 depth = curr->lockdep_depth - 1;
3081 hlock = curr->held_locks + depth;
3082
3083 /*
3084 * Is the unlock non-nested:
3085 */
Peter Zijlstrabb97a912009-07-20 19:15:35 +02003086 if (hlock->instance != lock || hlock->references)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003087 return lock_release_non_nested(curr, lock, ip);
3088 curr->lockdep_depth--;
3089
3090 if (DEBUG_LOCKS_WARN_ON(!depth && (hlock->prev_chain_key != 0)))
3091 return 0;
3092
3093 curr->curr_chain_key = hlock->prev_chain_key;
3094
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003095 lock_release_holdtime(hlock);
3096
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003097#ifdef CONFIG_DEBUG_LOCKDEP
3098 hlock->prev_chain_key = 0;
Dave Jonesf82b2172008-08-11 09:30:23 +02003099 hlock->class_idx = 0;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003100 hlock->acquire_ip = 0;
3101 hlock->irq_context = 0;
3102#endif
3103 return 1;
3104}
3105
3106/*
3107 * Remove the lock to the list of currently held locks - this gets
3108 * called on mutex_unlock()/spin_unlock*() (or on a failed
3109 * mutex_lock_interruptible()). This is done for unlocks that nest
3110 * perfectly. (i.e. the current top of the lock-stack is unlocked)
3111 */
3112static void
3113__lock_release(struct lockdep_map *lock, int nested, unsigned long ip)
3114{
3115 struct task_struct *curr = current;
3116
3117 if (!check_unlock(curr, lock, ip))
3118 return;
3119
3120 if (nested) {
3121 if (!lock_release_nested(curr, lock, ip))
3122 return;
3123 } else {
3124 if (!lock_release_non_nested(curr, lock, ip))
3125 return;
3126 }
3127
3128 check_chain_key(curr);
3129}
3130
Peter Zijlstraf607c662009-07-20 19:16:29 +02003131static int __lock_is_held(struct lockdep_map *lock)
3132{
3133 struct task_struct *curr = current;
3134 int i;
3135
3136 for (i = 0; i < curr->lockdep_depth; i++) {
Peter Zijlstrabb97a912009-07-20 19:15:35 +02003137 struct held_lock *hlock = curr->held_locks + i;
3138
3139 if (match_held_lock(hlock, lock))
Peter Zijlstraf607c662009-07-20 19:16:29 +02003140 return 1;
3141 }
3142
3143 return 0;
3144}
3145
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003146/*
3147 * Check whether we follow the irq-flags state precisely:
3148 */
Steven Rostedt1d09daa2008-05-12 21:20:55 +02003149static void check_flags(unsigned long flags)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003150{
Ingo Molnar992860e2008-07-14 10:28:38 +02003151#if defined(CONFIG_PROVE_LOCKING) && defined(CONFIG_DEBUG_LOCKDEP) && \
3152 defined(CONFIG_TRACE_IRQFLAGS)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003153 if (!debug_locks)
3154 return;
3155
Ingo Molnar5f9fa8a2007-12-07 19:02:47 +01003156 if (irqs_disabled_flags(flags)) {
3157 if (DEBUG_LOCKS_WARN_ON(current->hardirqs_enabled)) {
3158 printk("possible reason: unannotated irqs-off.\n");
3159 }
3160 } else {
3161 if (DEBUG_LOCKS_WARN_ON(!current->hardirqs_enabled)) {
3162 printk("possible reason: unannotated irqs-on.\n");
3163 }
3164 }
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003165
3166 /*
3167 * We dont accurately track softirq state in e.g.
3168 * hardirq contexts (such as on 4KSTACKS), so only
3169 * check if not in hardirq contexts:
3170 */
3171 if (!hardirq_count()) {
3172 if (softirq_count())
3173 DEBUG_LOCKS_WARN_ON(current->softirqs_enabled);
3174 else
3175 DEBUG_LOCKS_WARN_ON(!current->softirqs_enabled);
3176 }
3177
3178 if (!debug_locks)
3179 print_irqtrace_events(current);
3180#endif
3181}
3182
Peter Zijlstra00ef9f72008-12-04 09:00:17 +01003183void lock_set_class(struct lockdep_map *lock, const char *name,
3184 struct lock_class_key *key, unsigned int subclass,
3185 unsigned long ip)
Peter Zijlstra64aa3482008-08-11 09:30:21 +02003186{
3187 unsigned long flags;
3188
3189 if (unlikely(current->lockdep_recursion))
3190 return;
3191
3192 raw_local_irq_save(flags);
3193 current->lockdep_recursion = 1;
3194 check_flags(flags);
Peter Zijlstra00ef9f72008-12-04 09:00:17 +01003195 if (__lock_set_class(lock, name, key, subclass, ip))
Peter Zijlstra64aa3482008-08-11 09:30:21 +02003196 check_chain_key(current);
3197 current->lockdep_recursion = 0;
3198 raw_local_irq_restore(flags);
3199}
Peter Zijlstra00ef9f72008-12-04 09:00:17 +01003200EXPORT_SYMBOL_GPL(lock_set_class);
Peter Zijlstra64aa3482008-08-11 09:30:21 +02003201
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003202/*
3203 * We are not always called with irqs disabled - do that here,
3204 * and also avoid lockdep recursion:
3205 */
Steven Rostedt1d09daa2008-05-12 21:20:55 +02003206void lock_acquire(struct lockdep_map *lock, unsigned int subclass,
Peter Zijlstra7531e2f2008-08-11 09:30:24 +02003207 int trylock, int read, int check,
3208 struct lockdep_map *nest_lock, unsigned long ip)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003209{
3210 unsigned long flags;
3211
Peter Zijlstraefed7922009-03-04 12:32:55 +01003212 trace_lock_acquire(lock, subclass, trylock, read, check, nest_lock, ip);
3213
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003214 if (unlikely(current->lockdep_recursion))
3215 return;
3216
3217 raw_local_irq_save(flags);
3218 check_flags(flags);
3219
3220 current->lockdep_recursion = 1;
3221 __lock_acquire(lock, subclass, trylock, read, check,
Peter Zijlstrabb97a912009-07-20 19:15:35 +02003222 irqs_disabled_flags(flags), nest_lock, ip, 0);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003223 current->lockdep_recursion = 0;
3224 raw_local_irq_restore(flags);
3225}
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003226EXPORT_SYMBOL_GPL(lock_acquire);
3227
Steven Rostedt1d09daa2008-05-12 21:20:55 +02003228void lock_release(struct lockdep_map *lock, int nested,
Steven Rostedt0764d232008-05-12 21:20:44 +02003229 unsigned long ip)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003230{
3231 unsigned long flags;
3232
Peter Zijlstraefed7922009-03-04 12:32:55 +01003233 trace_lock_release(lock, nested, ip);
3234
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003235 if (unlikely(current->lockdep_recursion))
3236 return;
3237
3238 raw_local_irq_save(flags);
3239 check_flags(flags);
3240 current->lockdep_recursion = 1;
3241 __lock_release(lock, nested, ip);
3242 current->lockdep_recursion = 0;
3243 raw_local_irq_restore(flags);
3244}
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003245EXPORT_SYMBOL_GPL(lock_release);
3246
Peter Zijlstraf607c662009-07-20 19:16:29 +02003247int lock_is_held(struct lockdep_map *lock)
3248{
3249 unsigned long flags;
3250 int ret = 0;
3251
3252 if (unlikely(current->lockdep_recursion))
3253 return ret;
3254
3255 raw_local_irq_save(flags);
3256 check_flags(flags);
3257
3258 current->lockdep_recursion = 1;
3259 ret = __lock_is_held(lock);
3260 current->lockdep_recursion = 0;
3261 raw_local_irq_restore(flags);
3262
3263 return ret;
3264}
3265EXPORT_SYMBOL_GPL(lock_is_held);
3266
Nick Piggincf40bd12009-01-21 08:12:39 +01003267void lockdep_set_current_reclaim_state(gfp_t gfp_mask)
3268{
3269 current->lockdep_reclaim_gfp = gfp_mask;
3270}
3271
3272void lockdep_clear_current_reclaim_state(void)
3273{
3274 current->lockdep_reclaim_gfp = 0;
3275}
3276
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003277#ifdef CONFIG_LOCK_STAT
3278static int
3279print_lock_contention_bug(struct task_struct *curr, struct lockdep_map *lock,
3280 unsigned long ip)
3281{
3282 if (!debug_locks_off())
3283 return 0;
3284 if (debug_locks_silent)
3285 return 0;
3286
3287 printk("\n=================================\n");
3288 printk( "[ BUG: bad contention detected! ]\n");
3289 printk( "---------------------------------\n");
3290 printk("%s/%d is trying to contend lock (",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07003291 curr->comm, task_pid_nr(curr));
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003292 print_lockdep_cache(lock);
3293 printk(") at:\n");
3294 print_ip_sym(ip);
3295 printk("but there are no locks held!\n");
3296 printk("\nother info that might help us debug this:\n");
3297 lockdep_print_held_locks(curr);
3298
3299 printk("\nstack backtrace:\n");
3300 dump_stack();
3301
3302 return 0;
3303}
3304
3305static void
3306__lock_contended(struct lockdep_map *lock, unsigned long ip)
3307{
3308 struct task_struct *curr = current;
3309 struct held_lock *hlock, *prev_hlock;
3310 struct lock_class_stats *stats;
3311 unsigned int depth;
Peter Zijlstrac7e78cf2008-10-16 23:17:09 +02003312 int i, contention_point, contending_point;
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003313
3314 depth = curr->lockdep_depth;
3315 if (DEBUG_LOCKS_WARN_ON(!depth))
3316 return;
3317
3318 prev_hlock = NULL;
3319 for (i = depth-1; i >= 0; i--) {
3320 hlock = curr->held_locks + i;
3321 /*
3322 * We must not cross into another context:
3323 */
3324 if (prev_hlock && prev_hlock->irq_context != hlock->irq_context)
3325 break;
Peter Zijlstrabb97a912009-07-20 19:15:35 +02003326 if (match_held_lock(hlock, lock))
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003327 goto found_it;
3328 prev_hlock = hlock;
3329 }
3330 print_lock_contention_bug(curr, lock, ip);
3331 return;
3332
3333found_it:
Peter Zijlstrabb97a912009-07-20 19:15:35 +02003334 if (hlock->instance != lock)
3335 return;
3336
Peter Zijlstra3365e7792009-10-09 10:12:41 +02003337 hlock->waittime_stamp = lockstat_clock();
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003338
Peter Zijlstrac7e78cf2008-10-16 23:17:09 +02003339 contention_point = lock_point(hlock_class(hlock)->contention_point, ip);
3340 contending_point = lock_point(hlock_class(hlock)->contending_point,
3341 lock->ip);
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003342
Dave Jonesf82b2172008-08-11 09:30:23 +02003343 stats = get_lock_stats(hlock_class(hlock));
Peter Zijlstrac7e78cf2008-10-16 23:17:09 +02003344 if (contention_point < LOCKSTAT_POINTS)
3345 stats->contention_point[contention_point]++;
3346 if (contending_point < LOCKSTAT_POINTS)
3347 stats->contending_point[contending_point]++;
Peter Zijlstra96645672007-07-19 01:49:00 -07003348 if (lock->cpu != smp_processor_id())
3349 stats->bounces[bounce_contended + !!hlock->read]++;
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003350 put_lock_stats(stats);
3351}
3352
3353static void
Peter Zijlstrac7e78cf2008-10-16 23:17:09 +02003354__lock_acquired(struct lockdep_map *lock, unsigned long ip)
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003355{
3356 struct task_struct *curr = current;
3357 struct held_lock *hlock, *prev_hlock;
3358 struct lock_class_stats *stats;
3359 unsigned int depth;
Peter Zijlstra3365e7792009-10-09 10:12:41 +02003360 u64 now, waittime = 0;
Peter Zijlstra96645672007-07-19 01:49:00 -07003361 int i, cpu;
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003362
3363 depth = curr->lockdep_depth;
3364 if (DEBUG_LOCKS_WARN_ON(!depth))
3365 return;
3366
3367 prev_hlock = NULL;
3368 for (i = depth-1; i >= 0; i--) {
3369 hlock = curr->held_locks + i;
3370 /*
3371 * We must not cross into another context:
3372 */
3373 if (prev_hlock && prev_hlock->irq_context != hlock->irq_context)
3374 break;
Peter Zijlstrabb97a912009-07-20 19:15:35 +02003375 if (match_held_lock(hlock, lock))
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003376 goto found_it;
3377 prev_hlock = hlock;
3378 }
3379 print_lock_contention_bug(curr, lock, _RET_IP_);
3380 return;
3381
3382found_it:
Peter Zijlstrabb97a912009-07-20 19:15:35 +02003383 if (hlock->instance != lock)
3384 return;
3385
Peter Zijlstra96645672007-07-19 01:49:00 -07003386 cpu = smp_processor_id();
3387 if (hlock->waittime_stamp) {
Peter Zijlstra3365e7792009-10-09 10:12:41 +02003388 now = lockstat_clock();
Peter Zijlstra96645672007-07-19 01:49:00 -07003389 waittime = now - hlock->waittime_stamp;
3390 hlock->holdtime_stamp = now;
3391 }
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003392
Frederic Weisbecker20625012009-04-06 01:49:33 +02003393 trace_lock_acquired(lock, ip, waittime);
3394
Dave Jonesf82b2172008-08-11 09:30:23 +02003395 stats = get_lock_stats(hlock_class(hlock));
Peter Zijlstra96645672007-07-19 01:49:00 -07003396 if (waittime) {
3397 if (hlock->read)
3398 lock_time_inc(&stats->read_waittime, waittime);
3399 else
3400 lock_time_inc(&stats->write_waittime, waittime);
3401 }
3402 if (lock->cpu != cpu)
3403 stats->bounces[bounce_acquired + !!hlock->read]++;
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003404 put_lock_stats(stats);
Peter Zijlstra96645672007-07-19 01:49:00 -07003405
3406 lock->cpu = cpu;
Peter Zijlstrac7e78cf2008-10-16 23:17:09 +02003407 lock->ip = ip;
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003408}
3409
3410void lock_contended(struct lockdep_map *lock, unsigned long ip)
3411{
3412 unsigned long flags;
3413
Peter Zijlstraefed7922009-03-04 12:32:55 +01003414 trace_lock_contended(lock, ip);
3415
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003416 if (unlikely(!lock_stat))
3417 return;
3418
3419 if (unlikely(current->lockdep_recursion))
3420 return;
3421
3422 raw_local_irq_save(flags);
3423 check_flags(flags);
3424 current->lockdep_recursion = 1;
3425 __lock_contended(lock, ip);
3426 current->lockdep_recursion = 0;
3427 raw_local_irq_restore(flags);
3428}
3429EXPORT_SYMBOL_GPL(lock_contended);
3430
Peter Zijlstrac7e78cf2008-10-16 23:17:09 +02003431void lock_acquired(struct lockdep_map *lock, unsigned long ip)
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003432{
3433 unsigned long flags;
3434
3435 if (unlikely(!lock_stat))
3436 return;
3437
3438 if (unlikely(current->lockdep_recursion))
3439 return;
3440
3441 raw_local_irq_save(flags);
3442 check_flags(flags);
3443 current->lockdep_recursion = 1;
Peter Zijlstrac7e78cf2008-10-16 23:17:09 +02003444 __lock_acquired(lock, ip);
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003445 current->lockdep_recursion = 0;
3446 raw_local_irq_restore(flags);
3447}
3448EXPORT_SYMBOL_GPL(lock_acquired);
3449#endif
3450
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003451/*
3452 * Used by the testsuite, sanitize the validator state
3453 * after a simulated failure:
3454 */
3455
3456void lockdep_reset(void)
3457{
3458 unsigned long flags;
Ingo Molnar23d95a02006-12-13 00:34:40 -08003459 int i;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003460
3461 raw_local_irq_save(flags);
3462 current->curr_chain_key = 0;
3463 current->lockdep_depth = 0;
3464 current->lockdep_recursion = 0;
3465 memset(current->held_locks, 0, MAX_LOCK_DEPTH*sizeof(struct held_lock));
3466 nr_hardirq_chains = 0;
3467 nr_softirq_chains = 0;
3468 nr_process_chains = 0;
3469 debug_locks = 1;
Ingo Molnar23d95a02006-12-13 00:34:40 -08003470 for (i = 0; i < CHAINHASH_SIZE; i++)
3471 INIT_LIST_HEAD(chainhash_table + i);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003472 raw_local_irq_restore(flags);
3473}
3474
3475static void zap_class(struct lock_class *class)
3476{
3477 int i;
3478
3479 /*
3480 * Remove all dependencies this lock is
3481 * involved in:
3482 */
3483 for (i = 0; i < nr_list_entries; i++) {
3484 if (list_entries[i].class == class)
3485 list_del_rcu(&list_entries[i].entry);
3486 }
3487 /*
3488 * Unhash the class and remove it from the all_lock_classes list:
3489 */
3490 list_del_rcu(&class->hash_entry);
3491 list_del_rcu(&class->lock_entry);
3492
Rabin Vincent8bfe0292008-08-11 09:30:26 +02003493 class->key = NULL;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003494}
3495
Arjan van de Venfabe8742008-01-24 07:00:45 +01003496static inline int within(const void *addr, void *start, unsigned long size)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003497{
3498 return addr >= start && addr < start + size;
3499}
3500
3501void lockdep_free_key_range(void *start, unsigned long size)
3502{
3503 struct lock_class *class, *next;
3504 struct list_head *head;
3505 unsigned long flags;
3506 int i;
Nick Piggin5a26db52008-01-16 09:51:58 +01003507 int locked;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003508
3509 raw_local_irq_save(flags);
Nick Piggin5a26db52008-01-16 09:51:58 +01003510 locked = graph_lock();
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003511
3512 /*
3513 * Unhash all classes that were created by this module:
3514 */
3515 for (i = 0; i < CLASSHASH_SIZE; i++) {
3516 head = classhash_table + i;
3517 if (list_empty(head))
3518 continue;
Arjan van de Venfabe8742008-01-24 07:00:45 +01003519 list_for_each_entry_safe(class, next, head, hash_entry) {
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003520 if (within(class->key, start, size))
3521 zap_class(class);
Arjan van de Venfabe8742008-01-24 07:00:45 +01003522 else if (within(class->name, start, size))
3523 zap_class(class);
3524 }
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003525 }
3526
Nick Piggin5a26db52008-01-16 09:51:58 +01003527 if (locked)
3528 graph_unlock();
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003529 raw_local_irq_restore(flags);
3530}
3531
3532void lockdep_reset_lock(struct lockdep_map *lock)
3533{
Ingo Molnard6d897c2006-07-10 04:44:04 -07003534 struct lock_class *class, *next;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003535 struct list_head *head;
3536 unsigned long flags;
3537 int i, j;
Nick Piggin5a26db52008-01-16 09:51:58 +01003538 int locked;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003539
3540 raw_local_irq_save(flags);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003541
3542 /*
Ingo Molnard6d897c2006-07-10 04:44:04 -07003543 * Remove all classes this lock might have:
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003544 */
Ingo Molnard6d897c2006-07-10 04:44:04 -07003545 for (j = 0; j < MAX_LOCKDEP_SUBCLASSES; j++) {
3546 /*
3547 * If the class exists we look it up and zap it:
3548 */
3549 class = look_up_lock_class(lock, j);
3550 if (class)
3551 zap_class(class);
3552 }
3553 /*
3554 * Debug check: in the end all mapped classes should
3555 * be gone.
3556 */
Nick Piggin5a26db52008-01-16 09:51:58 +01003557 locked = graph_lock();
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003558 for (i = 0; i < CLASSHASH_SIZE; i++) {
3559 head = classhash_table + i;
3560 if (list_empty(head))
3561 continue;
3562 list_for_each_entry_safe(class, next, head, hash_entry) {
Ingo Molnard6d897c2006-07-10 04:44:04 -07003563 if (unlikely(class == lock->class_cache)) {
Ingo Molnar74c383f2006-12-13 00:34:43 -08003564 if (debug_locks_off_graph_unlock())
3565 WARN_ON(1);
Ingo Molnard6d897c2006-07-10 04:44:04 -07003566 goto out_restore;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003567 }
3568 }
3569 }
Nick Piggin5a26db52008-01-16 09:51:58 +01003570 if (locked)
3571 graph_unlock();
Ingo Molnard6d897c2006-07-10 04:44:04 -07003572
3573out_restore:
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003574 raw_local_irq_restore(flags);
3575}
3576
Sam Ravnborg14999932007-02-28 20:12:31 -08003577void lockdep_init(void)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003578{
3579 int i;
3580
3581 /*
3582 * Some architectures have their own start_kernel()
3583 * code which calls lockdep_init(), while we also
3584 * call lockdep_init() from the start_kernel() itself,
3585 * and we want to initialize the hashes only once:
3586 */
3587 if (lockdep_initialized)
3588 return;
3589
3590 for (i = 0; i < CLASSHASH_SIZE; i++)
3591 INIT_LIST_HEAD(classhash_table + i);
3592
3593 for (i = 0; i < CHAINHASH_SIZE; i++)
3594 INIT_LIST_HEAD(chainhash_table + i);
3595
3596 lockdep_initialized = 1;
3597}
3598
3599void __init lockdep_info(void)
3600{
3601 printk("Lock dependency validator: Copyright (c) 2006 Red Hat, Inc., Ingo Molnar\n");
3602
Li Zefanb0788ca2008-11-21 15:57:32 +08003603 printk("... MAX_LOCKDEP_SUBCLASSES: %lu\n", MAX_LOCKDEP_SUBCLASSES);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003604 printk("... MAX_LOCK_DEPTH: %lu\n", MAX_LOCK_DEPTH);
3605 printk("... MAX_LOCKDEP_KEYS: %lu\n", MAX_LOCKDEP_KEYS);
Li Zefanb0788ca2008-11-21 15:57:32 +08003606 printk("... CLASSHASH_SIZE: %lu\n", CLASSHASH_SIZE);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003607 printk("... MAX_LOCKDEP_ENTRIES: %lu\n", MAX_LOCKDEP_ENTRIES);
3608 printk("... MAX_LOCKDEP_CHAINS: %lu\n", MAX_LOCKDEP_CHAINS);
3609 printk("... CHAINHASH_SIZE: %lu\n", CHAINHASH_SIZE);
3610
3611 printk(" memory used by lock dependency info: %lu kB\n",
3612 (sizeof(struct lock_class) * MAX_LOCKDEP_KEYS +
3613 sizeof(struct list_head) * CLASSHASH_SIZE +
3614 sizeof(struct lock_list) * MAX_LOCKDEP_ENTRIES +
3615 sizeof(struct lock_chain) * MAX_LOCKDEP_CHAINS +
Ming Lei90629202009-08-02 21:43:36 +08003616 sizeof(struct list_head) * CHAINHASH_SIZE
Ming Lei4dd861d2009-07-16 15:44:29 +02003617#ifdef CONFIG_PROVE_LOCKING
Ming Leie351b662009-07-22 22:48:09 +08003618 + sizeof(struct circular_queue)
Ming Lei4dd861d2009-07-16 15:44:29 +02003619#endif
Ming Lei90629202009-08-02 21:43:36 +08003620 ) / 1024
Ming Lei4dd861d2009-07-16 15:44:29 +02003621 );
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003622
3623 printk(" per task-struct memory footprint: %lu bytes\n",
3624 sizeof(struct held_lock) * MAX_LOCK_DEPTH);
3625
3626#ifdef CONFIG_DEBUG_LOCKDEP
Johannes Bergc71063c2007-07-19 01:49:02 -07003627 if (lockdep_init_error) {
3628 printk("WARNING: lockdep init error! Arch code didn't call lockdep_init() early enough?\n");
3629 printk("Call stack leading to lockdep invocation was:\n");
3630 print_stack_trace(&lockdep_init_trace, 0);
3631 }
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003632#endif
3633}
3634
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003635static void
3636print_freed_lock_bug(struct task_struct *curr, const void *mem_from,
Arjan van de Ven55794a42006-07-10 04:44:03 -07003637 const void *mem_to, struct held_lock *hlock)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003638{
3639 if (!debug_locks_off())
3640 return;
3641 if (debug_locks_silent)
3642 return;
3643
3644 printk("\n=========================\n");
3645 printk( "[ BUG: held lock freed! ]\n");
3646 printk( "-------------------------\n");
3647 printk("%s/%d is freeing memory %p-%p, with a lock still held there!\n",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07003648 curr->comm, task_pid_nr(curr), mem_from, mem_to-1);
Arjan van de Ven55794a42006-07-10 04:44:03 -07003649 print_lock(hlock);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003650 lockdep_print_held_locks(curr);
3651
3652 printk("\nstack backtrace:\n");
3653 dump_stack();
3654}
3655
Oleg Nesterov54561782007-12-05 15:46:09 +01003656static inline int not_in_range(const void* mem_from, unsigned long mem_len,
3657 const void* lock_from, unsigned long lock_len)
3658{
3659 return lock_from + lock_len <= mem_from ||
3660 mem_from + mem_len <= lock_from;
3661}
3662
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003663/*
3664 * Called when kernel memory is freed (or unmapped), or if a lock
3665 * is destroyed or reinitialized - this code checks whether there is
3666 * any held lock in the memory range of <from> to <to>:
3667 */
3668void debug_check_no_locks_freed(const void *mem_from, unsigned long mem_len)
3669{
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003670 struct task_struct *curr = current;
3671 struct held_lock *hlock;
3672 unsigned long flags;
3673 int i;
3674
3675 if (unlikely(!debug_locks))
3676 return;
3677
3678 local_irq_save(flags);
3679 for (i = 0; i < curr->lockdep_depth; i++) {
3680 hlock = curr->held_locks + i;
3681
Oleg Nesterov54561782007-12-05 15:46:09 +01003682 if (not_in_range(mem_from, mem_len, hlock->instance,
3683 sizeof(*hlock->instance)))
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003684 continue;
3685
Oleg Nesterov54561782007-12-05 15:46:09 +01003686 print_freed_lock_bug(curr, mem_from, mem_from + mem_len, hlock);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003687 break;
3688 }
3689 local_irq_restore(flags);
3690}
Peter Zijlstraed075362006-12-06 20:35:24 -08003691EXPORT_SYMBOL_GPL(debug_check_no_locks_freed);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003692
3693static void print_held_locks_bug(struct task_struct *curr)
3694{
3695 if (!debug_locks_off())
3696 return;
3697 if (debug_locks_silent)
3698 return;
3699
3700 printk("\n=====================================\n");
3701 printk( "[ BUG: lock held at task exit time! ]\n");
3702 printk( "-------------------------------------\n");
3703 printk("%s/%d is exiting with locks still held!\n",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07003704 curr->comm, task_pid_nr(curr));
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003705 lockdep_print_held_locks(curr);
3706
3707 printk("\nstack backtrace:\n");
3708 dump_stack();
3709}
3710
3711void debug_check_no_locks_held(struct task_struct *task)
3712{
3713 if (unlikely(task->lockdep_depth > 0))
3714 print_held_locks_bug(task);
3715}
3716
3717void debug_show_all_locks(void)
3718{
3719 struct task_struct *g, *p;
3720 int count = 10;
3721 int unlock = 1;
3722
Jarek Poplawski9c35dd72007-03-22 00:11:28 -08003723 if (unlikely(!debug_locks)) {
3724 printk("INFO: lockdep is turned off.\n");
3725 return;
3726 }
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003727 printk("\nShowing all locks held in the system:\n");
3728
3729 /*
3730 * Here we try to get the tasklist_lock as hard as possible,
3731 * if not successful after 2 seconds we ignore it (but keep
3732 * trying). This is to enable a debug printout even if a
3733 * tasklist_lock-holding task deadlocks or crashes.
3734 */
3735retry:
3736 if (!read_trylock(&tasklist_lock)) {
3737 if (count == 10)
3738 printk("hm, tasklist_lock locked, retrying... ");
3739 if (count) {
3740 count--;
3741 printk(" #%d", 10-count);
3742 mdelay(200);
3743 goto retry;
3744 }
3745 printk(" ignoring it.\n");
3746 unlock = 0;
qinghuang feng46fec7a2008-10-28 17:24:28 +08003747 } else {
3748 if (count != 10)
3749 printk(KERN_CONT " locked it.\n");
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003750 }
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003751
3752 do_each_thread(g, p) {
Ingo Molnar85684872007-12-05 15:46:09 +01003753 /*
3754 * It's not reliable to print a task's held locks
3755 * if it's not sleeping (or if it's not the current
3756 * task):
3757 */
3758 if (p->state == TASK_RUNNING && p != current)
3759 continue;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003760 if (p->lockdep_depth)
3761 lockdep_print_held_locks(p);
3762 if (!unlock)
3763 if (read_trylock(&tasklist_lock))
3764 unlock = 1;
3765 } while_each_thread(g, p);
3766
3767 printk("\n");
3768 printk("=============================================\n\n");
3769
3770 if (unlock)
3771 read_unlock(&tasklist_lock);
3772}
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003773EXPORT_SYMBOL_GPL(debug_show_all_locks);
3774
Ingo Molnar82a1fcb2008-01-25 21:08:02 +01003775/*
3776 * Careful: only use this function if you are sure that
3777 * the task cannot run in parallel!
3778 */
3779void __debug_show_held_locks(struct task_struct *task)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003780{
Jarek Poplawski9c35dd72007-03-22 00:11:28 -08003781 if (unlikely(!debug_locks)) {
3782 printk("INFO: lockdep is turned off.\n");
3783 return;
3784 }
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003785 lockdep_print_held_locks(task);
3786}
Ingo Molnar82a1fcb2008-01-25 21:08:02 +01003787EXPORT_SYMBOL_GPL(__debug_show_held_locks);
3788
3789void debug_show_held_locks(struct task_struct *task)
3790{
3791 __debug_show_held_locks(task);
3792}
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003793EXPORT_SYMBOL_GPL(debug_show_held_locks);
Peter Zijlstrab351d162007-10-11 22:11:12 +02003794
3795void lockdep_sys_exit(void)
3796{
3797 struct task_struct *curr = current;
3798
3799 if (unlikely(curr->lockdep_depth)) {
3800 if (!debug_locks_off())
3801 return;
3802 printk("\n================================================\n");
3803 printk( "[ BUG: lock held when returning to user space! ]\n");
3804 printk( "------------------------------------------------\n");
3805 printk("%s/%d is leaving the kernel with locks still held!\n",
3806 curr->comm, curr->pid);
3807 lockdep_print_held_locks(curr);
3808 }
3809}