blob: f5dcd36d3151a0e3563f786f050795d605d25f99 [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
171 if (time < lt->min || !lt->min)
172 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{
180 dst->min += src->min;
181 dst->max += src->max;
182 dst->total += src->total;
183 dst->nr += src->nr;
184}
185
186struct lock_class_stats lock_stats(struct lock_class *class)
187{
188 struct lock_class_stats stats;
189 int cpu, i;
190
191 memset(&stats, 0, sizeof(struct lock_class_stats));
192 for_each_possible_cpu(cpu) {
193 struct lock_class_stats *pcs =
194 &per_cpu(lock_stats, cpu)[class - lock_classes];
195
196 for (i = 0; i < ARRAY_SIZE(stats.contention_point); i++)
197 stats.contention_point[i] += pcs->contention_point[i];
198
Peter Zijlstrac7e78cf2008-10-16 23:17:09 +0200199 for (i = 0; i < ARRAY_SIZE(stats.contending_point); i++)
200 stats.contending_point[i] += pcs->contending_point[i];
201
Peter Zijlstrac46261d2007-07-19 01:48:57 -0700202 lock_time_add(&pcs->read_waittime, &stats.read_waittime);
203 lock_time_add(&pcs->write_waittime, &stats.write_waittime);
204
205 lock_time_add(&pcs->read_holdtime, &stats.read_holdtime);
206 lock_time_add(&pcs->write_holdtime, &stats.write_holdtime);
Peter Zijlstra96645672007-07-19 01:49:00 -0700207
208 for (i = 0; i < ARRAY_SIZE(stats.bounces); i++)
209 stats.bounces[i] += pcs->bounces[i];
Peter Zijlstrac46261d2007-07-19 01:48:57 -0700210 }
211
212 return stats;
213}
214
215void clear_lock_stats(struct lock_class *class)
216{
217 int cpu;
218
219 for_each_possible_cpu(cpu) {
220 struct lock_class_stats *cpu_stats =
221 &per_cpu(lock_stats, cpu)[class - lock_classes];
222
223 memset(cpu_stats, 0, sizeof(struct lock_class_stats));
224 }
225 memset(class->contention_point, 0, sizeof(class->contention_point));
Peter Zijlstrac7e78cf2008-10-16 23:17:09 +0200226 memset(class->contending_point, 0, sizeof(class->contending_point));
Peter Zijlstrac46261d2007-07-19 01:48:57 -0700227}
228
Peter Zijlstraf20786f2007-07-19 01:48:56 -0700229static struct lock_class_stats *get_lock_stats(struct lock_class *class)
230{
231 return &get_cpu_var(lock_stats)[class - lock_classes];
232}
233
234static void put_lock_stats(struct lock_class_stats *stats)
235{
236 put_cpu_var(lock_stats);
237}
238
239static void lock_release_holdtime(struct held_lock *hlock)
240{
241 struct lock_class_stats *stats;
Peter Zijlstra3365e7792009-10-09 10:12:41 +0200242 u64 holdtime;
Peter Zijlstraf20786f2007-07-19 01:48:56 -0700243
244 if (!lock_stat)
245 return;
246
Peter Zijlstra3365e7792009-10-09 10:12:41 +0200247 holdtime = lockstat_clock() - hlock->holdtime_stamp;
Peter Zijlstraf20786f2007-07-19 01:48:56 -0700248
Dave Jonesf82b2172008-08-11 09:30:23 +0200249 stats = get_lock_stats(hlock_class(hlock));
Peter Zijlstraf20786f2007-07-19 01:48:56 -0700250 if (hlock->read)
251 lock_time_inc(&stats->read_holdtime, holdtime);
252 else
253 lock_time_inc(&stats->write_holdtime, holdtime);
254 put_lock_stats(stats);
255}
256#else
257static inline void lock_release_holdtime(struct held_lock *hlock)
258{
259}
260#endif
261
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700262/*
263 * We keep a global list of all lock classes. The list only grows,
264 * never shrinks. The list is only accessed with the lockdep
265 * spinlock lock held.
266 */
267LIST_HEAD(all_lock_classes);
268
269/*
270 * The lockdep classes are in a hash-table as well, for fast lookup:
271 */
272#define CLASSHASH_BITS (MAX_LOCKDEP_KEYS_BITS - 1)
273#define CLASSHASH_SIZE (1UL << CLASSHASH_BITS)
Peter Zijlstra4b32d0a2007-07-19 01:48:59 -0700274#define __classhashfn(key) hash_long((unsigned long)key, CLASSHASH_BITS)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700275#define classhashentry(key) (classhash_table + __classhashfn((key)))
276
277static struct list_head classhash_table[CLASSHASH_SIZE];
278
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700279/*
280 * We put the lock dependency chains into a hash-table as well, to cache
281 * their existence:
282 */
283#define CHAINHASH_BITS (MAX_LOCKDEP_CHAINS_BITS-1)
284#define CHAINHASH_SIZE (1UL << CHAINHASH_BITS)
Peter Zijlstra4b32d0a2007-07-19 01:48:59 -0700285#define __chainhashfn(chain) hash_long(chain, CHAINHASH_BITS)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700286#define chainhashentry(chain) (chainhash_table + __chainhashfn((chain)))
287
288static struct list_head chainhash_table[CHAINHASH_SIZE];
289
290/*
291 * The hash key of the lock dependency chains is a hash itself too:
292 * it's a hash of all locks taken up to that lock, including that lock.
293 * It's a 64-bit hash, because it's important for the keys to be
294 * unique.
295 */
296#define iterate_chain_key(key1, key2) \
Ingo Molnar03cbc352006-09-29 02:01:46 -0700297 (((key1) << MAX_LOCKDEP_KEYS_BITS) ^ \
298 ((key1) >> (64-MAX_LOCKDEP_KEYS_BITS)) ^ \
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700299 (key2))
300
Steven Rostedt1d09daa2008-05-12 21:20:55 +0200301void lockdep_off(void)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700302{
303 current->lockdep_recursion++;
304}
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700305EXPORT_SYMBOL(lockdep_off);
306
Steven Rostedt1d09daa2008-05-12 21:20:55 +0200307void lockdep_on(void)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700308{
309 current->lockdep_recursion--;
310}
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700311EXPORT_SYMBOL(lockdep_on);
312
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700313/*
314 * Debugging switches:
315 */
316
317#define VERBOSE 0
Ingo Molnar33e94e92006-12-13 00:34:41 -0800318#define VERY_VERBOSE 0
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700319
320#if VERBOSE
321# define HARDIRQ_VERBOSE 1
322# define SOFTIRQ_VERBOSE 1
Nick Piggincf40bd12009-01-21 08:12:39 +0100323# define RECLAIM_VERBOSE 1
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700324#else
325# define HARDIRQ_VERBOSE 0
326# define SOFTIRQ_VERBOSE 0
Nick Piggincf40bd12009-01-21 08:12:39 +0100327# define RECLAIM_VERBOSE 0
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700328#endif
329
Nick Piggincf40bd12009-01-21 08:12:39 +0100330#if VERBOSE || HARDIRQ_VERBOSE || SOFTIRQ_VERBOSE || RECLAIM_VERBOSE
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700331/*
332 * Quick filtering for interesting events:
333 */
334static int class_filter(struct lock_class *class)
335{
Andi Kleenf9829cc2006-07-10 04:44:01 -0700336#if 0
337 /* Example */
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700338 if (class->name_version == 1 &&
Andi Kleenf9829cc2006-07-10 04:44:01 -0700339 !strcmp(class->name, "lockname"))
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700340 return 1;
341 if (class->name_version == 1 &&
Andi Kleenf9829cc2006-07-10 04:44:01 -0700342 !strcmp(class->name, "&struct->lockfield"))
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700343 return 1;
Andi Kleenf9829cc2006-07-10 04:44:01 -0700344#endif
Ingo Molnara6640892006-12-13 00:34:39 -0800345 /* Filter everything else. 1 would be to allow everything else */
346 return 0;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700347}
348#endif
349
350static int verbose(struct lock_class *class)
351{
352#if VERBOSE
353 return class_filter(class);
354#endif
355 return 0;
356}
357
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700358/*
359 * Stack-trace: tightly packed array of stack backtrace
Ingo Molnar74c383f2006-12-13 00:34:43 -0800360 * addresses. Protected by the graph_lock.
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700361 */
362unsigned long nr_stack_trace_entries;
363static unsigned long stack_trace[MAX_STACK_TRACE_ENTRIES];
364
365static int save_trace(struct stack_trace *trace)
366{
367 trace->nr_entries = 0;
368 trace->max_entries = MAX_STACK_TRACE_ENTRIES - nr_stack_trace_entries;
369 trace->entries = stack_trace + nr_stack_trace_entries;
370
Andi Kleen5a1b3992006-09-26 10:52:34 +0200371 trace->skip = 3;
Andi Kleen5a1b3992006-09-26 10:52:34 +0200372
Christoph Hellwigab1b6f02007-05-08 00:23:29 -0700373 save_stack_trace(trace);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700374
Peter Zijlstra4f84f432009-07-20 15:27:04 +0200375 /*
376 * Some daft arches put -1 at the end to indicate its a full trace.
377 *
378 * <rant> this is buggy anyway, since it takes a whole extra entry so a
379 * complete trace that maxes out the entries provided will be reported
380 * as incomplete, friggin useless </rant>
381 */
382 if (trace->entries[trace->nr_entries-1] == ULONG_MAX)
383 trace->nr_entries--;
384
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700385 trace->max_entries = trace->nr_entries;
386
387 nr_stack_trace_entries += trace->nr_entries;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700388
Peter Zijlstra4f84f432009-07-20 15:27:04 +0200389 if (nr_stack_trace_entries >= MAX_STACK_TRACE_ENTRIES-1) {
Ingo Molnar74c383f2006-12-13 00:34:43 -0800390 if (!debug_locks_off_graph_unlock())
391 return 0;
392
393 printk("BUG: MAX_STACK_TRACE_ENTRIES too low!\n");
394 printk("turning off the locking correctness validator.\n");
395 dump_stack();
396
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700397 return 0;
398 }
399
400 return 1;
401}
402
403unsigned int nr_hardirq_chains;
404unsigned int nr_softirq_chains;
405unsigned int nr_process_chains;
406unsigned int max_lockdep_depth;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700407
408#ifdef CONFIG_DEBUG_LOCKDEP
409/*
410 * We cannot printk in early bootup code. Not even early_printk()
411 * might work. So we mark any initialization errors and printk
412 * about it later on, in lockdep_info().
413 */
414static int lockdep_init_error;
Johannes Bergc71063c2007-07-19 01:49:02 -0700415static unsigned long lockdep_init_trace_data[20];
416static struct stack_trace lockdep_init_trace = {
417 .max_entries = ARRAY_SIZE(lockdep_init_trace_data),
418 .entries = lockdep_init_trace_data,
419};
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700420
421/*
422 * Various lockdep statistics:
423 */
424atomic_t chain_lookup_hits;
425atomic_t chain_lookup_misses;
426atomic_t hardirqs_on_events;
427atomic_t hardirqs_off_events;
428atomic_t redundant_hardirqs_on;
429atomic_t redundant_hardirqs_off;
430atomic_t softirqs_on_events;
431atomic_t softirqs_off_events;
432atomic_t redundant_softirqs_on;
433atomic_t redundant_softirqs_off;
434atomic_t nr_unused_locks;
435atomic_t nr_cyclic_checks;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700436atomic_t nr_find_usage_forwards_checks;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700437atomic_t nr_find_usage_backwards_checks;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700438#endif
439
440/*
441 * Locking printouts:
442 */
443
Peter Zijlstrafabe9c42009-01-22 14:51:01 +0100444#define __USAGE(__STATE) \
Peter Zijlstrab4b136f2009-01-29 14:50:36 +0100445 [LOCK_USED_IN_##__STATE] = "IN-"__stringify(__STATE)"-W", \
446 [LOCK_ENABLED_##__STATE] = __stringify(__STATE)"-ON-W", \
447 [LOCK_USED_IN_##__STATE##_READ] = "IN-"__stringify(__STATE)"-R",\
448 [LOCK_ENABLED_##__STATE##_READ] = __stringify(__STATE)"-ON-R",
Peter Zijlstrafabe9c42009-01-22 14:51:01 +0100449
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700450static const char *usage_str[] =
451{
Peter Zijlstrafabe9c42009-01-22 14:51:01 +0100452#define LOCKDEP_STATE(__STATE) __USAGE(__STATE)
453#include "lockdep_states.h"
454#undef LOCKDEP_STATE
455 [LOCK_USED] = "INITIAL USE",
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700456};
457
458const char * __get_key_name(struct lockdep_subclass_key *key, char *str)
459{
Alexey Dobriyanffb45122007-05-08 00:28:41 -0700460 return kallsyms_lookup((unsigned long)key, NULL, NULL, NULL, str);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700461}
462
Peter Zijlstra3ff176c2009-01-22 17:40:42 +0100463static inline unsigned long lock_flag(enum lock_usage_bit bit)
464{
465 return 1UL << bit;
466}
467
468static char get_usage_char(struct lock_class *class, enum lock_usage_bit bit)
469{
470 char c = '.';
471
472 if (class->usage_mask & lock_flag(bit + 2))
473 c = '+';
474 if (class->usage_mask & lock_flag(bit)) {
475 c = '-';
476 if (class->usage_mask & lock_flag(bit + 2))
477 c = '?';
478 }
479
480 return c;
481}
482
Peter Zijlstraf510b232009-01-22 17:53:47 +0100483void get_usage_chars(struct lock_class *class, char usage[LOCK_USAGE_CHARS])
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700484{
Peter Zijlstraf510b232009-01-22 17:53:47 +0100485 int i = 0;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700486
Peter Zijlstraf510b232009-01-22 17:53:47 +0100487#define LOCKDEP_STATE(__STATE) \
488 usage[i++] = get_usage_char(class, LOCK_USED_IN_##__STATE); \
489 usage[i++] = get_usage_char(class, LOCK_USED_IN_##__STATE##_READ);
490#include "lockdep_states.h"
491#undef LOCKDEP_STATE
492
493 usage[i] = '\0';
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700494}
495
496static void print_lock_name(struct lock_class *class)
497{
Peter Zijlstraf510b232009-01-22 17:53:47 +0100498 char str[KSYM_NAME_LEN], usage[LOCK_USAGE_CHARS];
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700499 const char *name;
500
Peter Zijlstraf510b232009-01-22 17:53:47 +0100501 get_usage_chars(class, usage);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700502
503 name = class->name;
504 if (!name) {
505 name = __get_key_name(class->key, str);
506 printk(" (%s", name);
507 } else {
508 printk(" (%s", name);
509 if (class->name_version > 1)
510 printk("#%d", class->name_version);
511 if (class->subclass)
512 printk("/%d", class->subclass);
513 }
Peter Zijlstraf510b232009-01-22 17:53:47 +0100514 printk("){%s}", usage);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700515}
516
517static void print_lockdep_cache(struct lockdep_map *lock)
518{
519 const char *name;
Tejun Heo9281ace2007-07-17 04:03:51 -0700520 char str[KSYM_NAME_LEN];
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700521
522 name = lock->name;
523 if (!name)
524 name = __get_key_name(lock->key->subkeys, str);
525
526 printk("%s", name);
527}
528
529static void print_lock(struct held_lock *hlock)
530{
Dave Jonesf82b2172008-08-11 09:30:23 +0200531 print_lock_name(hlock_class(hlock));
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700532 printk(", at: ");
533 print_ip_sym(hlock->acquire_ip);
534}
535
536static void lockdep_print_held_locks(struct task_struct *curr)
537{
538 int i, depth = curr->lockdep_depth;
539
540 if (!depth) {
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -0700541 printk("no locks held by %s/%d.\n", curr->comm, task_pid_nr(curr));
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700542 return;
543 }
544 printk("%d lock%s held by %s/%d:\n",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -0700545 depth, depth > 1 ? "s" : "", curr->comm, task_pid_nr(curr));
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700546
547 for (i = 0; i < depth; i++) {
548 printk(" #%d: ", i);
549 print_lock(curr->held_locks + i);
550 }
551}
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700552
Dave Jones99de0552006-09-29 02:00:10 -0700553static void print_kernel_version(void)
554{
Serge E. Hallyn96b644b2006-10-02 02:18:13 -0700555 printk("%s %.*s\n", init_utsname()->release,
556 (int)strcspn(init_utsname()->version, " "),
557 init_utsname()->version);
Dave Jones99de0552006-09-29 02:00:10 -0700558}
559
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700560static int very_verbose(struct lock_class *class)
561{
562#if VERY_VERBOSE
563 return class_filter(class);
564#endif
565 return 0;
566}
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700567
568/*
569 * Is this the address of a static object:
570 */
571static int static_obj(void *obj)
572{
573 unsigned long start = (unsigned long) &_stext,
574 end = (unsigned long) &_end,
575 addr = (unsigned long) obj;
576#ifdef CONFIG_SMP
577 int i;
578#endif
579
580 /*
581 * static variable?
582 */
583 if ((addr >= start) && (addr < end))
584 return 1;
585
Mike Frysinger2a9ad182009-09-22 16:44:16 -0700586 if (arch_is_kernel_data(addr))
587 return 1;
588
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700589#ifdef CONFIG_SMP
590 /*
591 * percpu var?
592 */
593 for_each_possible_cpu(i) {
594 start = (unsigned long) &__per_cpu_start + per_cpu_offset(i);
Ingo Molnar1ff56832006-11-17 19:57:22 +0100595 end = (unsigned long) &__per_cpu_start + PERCPU_ENOUGH_ROOM
596 + per_cpu_offset(i);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700597
598 if ((addr >= start) && (addr < end))
599 return 1;
600 }
601#endif
602
603 /*
604 * module var?
605 */
606 return is_module_address(addr);
607}
608
609/*
610 * To make lock name printouts unique, we calculate a unique
611 * class->name_version generation counter:
612 */
613static int count_matching_names(struct lock_class *new_class)
614{
615 struct lock_class *class;
616 int count = 0;
617
618 if (!new_class->name)
619 return 0;
620
621 list_for_each_entry(class, &all_lock_classes, lock_entry) {
622 if (new_class->key - new_class->subclass == class->key)
623 return class->name_version;
624 if (class->name && !strcmp(class->name, new_class->name))
625 count = max(count, class->name_version);
626 }
627
628 return count + 1;
629}
630
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700631/*
632 * Register a lock's class in the hash-table, if the class is not present
633 * yet. Otherwise we look it up. We cache the result in the lock object
634 * itself, so actual lookup of the hash should be once per lock object.
635 */
636static inline struct lock_class *
Ingo Molnard6d897c2006-07-10 04:44:04 -0700637look_up_lock_class(struct lockdep_map *lock, unsigned int subclass)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700638{
639 struct lockdep_subclass_key *key;
640 struct list_head *hash_head;
641 struct lock_class *class;
642
643#ifdef CONFIG_DEBUG_LOCKDEP
644 /*
645 * If the architecture calls into lockdep before initializing
646 * the hashes then we'll warn about it later. (we cannot printk
647 * right now)
648 */
649 if (unlikely(!lockdep_initialized)) {
650 lockdep_init();
651 lockdep_init_error = 1;
Johannes Bergc71063c2007-07-19 01:49:02 -0700652 save_stack_trace(&lockdep_init_trace);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700653 }
654#endif
655
656 /*
657 * Static locks do not have their class-keys yet - for them the key
658 * is the lock object itself:
659 */
660 if (unlikely(!lock->key))
661 lock->key = (void *)lock;
662
663 /*
664 * NOTE: the class-key must be unique. For dynamic locks, a static
665 * lock_class_key variable is passed in through the mutex_init()
666 * (or spin_lock_init()) call - which acts as the key. For static
667 * locks we use the lock object itself as the key.
668 */
Peter Zijlstra4b32d0a2007-07-19 01:48:59 -0700669 BUILD_BUG_ON(sizeof(struct lock_class_key) >
670 sizeof(struct lockdep_map));
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700671
672 key = lock->key->subkeys + subclass;
673
674 hash_head = classhashentry(key);
675
676 /*
677 * We can walk the hash lockfree, because the hash only
678 * grows, and we are careful when adding entries to the end:
679 */
Peter Zijlstra4b32d0a2007-07-19 01:48:59 -0700680 list_for_each_entry(class, hash_head, hash_entry) {
681 if (class->key == key) {
682 WARN_ON_ONCE(class->name != lock->name);
Ingo Molnard6d897c2006-07-10 04:44:04 -0700683 return class;
Peter Zijlstra4b32d0a2007-07-19 01:48:59 -0700684 }
685 }
Ingo Molnard6d897c2006-07-10 04:44:04 -0700686
687 return NULL;
688}
689
690/*
691 * Register a lock's class in the hash-table, if the class is not present
692 * yet. Otherwise we look it up. We cache the result in the lock object
693 * itself, so actual lookup of the hash should be once per lock object.
694 */
695static inline struct lock_class *
Peter Zijlstra4dfbb9d2006-10-11 01:45:14 -0400696register_lock_class(struct lockdep_map *lock, unsigned int subclass, int force)
Ingo Molnard6d897c2006-07-10 04:44:04 -0700697{
698 struct lockdep_subclass_key *key;
699 struct list_head *hash_head;
700 struct lock_class *class;
Ingo Molnar70e45062006-12-06 20:40:50 -0800701 unsigned long flags;
Ingo Molnard6d897c2006-07-10 04:44:04 -0700702
703 class = look_up_lock_class(lock, subclass);
704 if (likely(class))
705 return class;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700706
707 /*
708 * Debug-check: all keys must be persistent!
709 */
710 if (!static_obj(lock->key)) {
711 debug_locks_off();
712 printk("INFO: trying to register non-static key.\n");
713 printk("the code is fine but needs lockdep annotation.\n");
714 printk("turning off the locking correctness validator.\n");
715 dump_stack();
716
717 return NULL;
718 }
719
Ingo Molnard6d897c2006-07-10 04:44:04 -0700720 key = lock->key->subkeys + subclass;
721 hash_head = classhashentry(key);
722
Ingo Molnar70e45062006-12-06 20:40:50 -0800723 raw_local_irq_save(flags);
Ingo Molnar74c383f2006-12-13 00:34:43 -0800724 if (!graph_lock()) {
725 raw_local_irq_restore(flags);
726 return NULL;
727 }
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700728 /*
729 * We have to do the hash-walk again, to avoid races
730 * with another CPU:
731 */
732 list_for_each_entry(class, hash_head, hash_entry)
733 if (class->key == key)
734 goto out_unlock_set;
735 /*
736 * Allocate a new key from the static array, and add it to
737 * the hash:
738 */
739 if (nr_lock_classes >= MAX_LOCKDEP_KEYS) {
Ingo Molnar74c383f2006-12-13 00:34:43 -0800740 if (!debug_locks_off_graph_unlock()) {
741 raw_local_irq_restore(flags);
742 return NULL;
743 }
Ingo Molnar70e45062006-12-06 20:40:50 -0800744 raw_local_irq_restore(flags);
Ingo Molnar74c383f2006-12-13 00:34:43 -0800745
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700746 printk("BUG: MAX_LOCKDEP_KEYS too low!\n");
747 printk("turning off the locking correctness validator.\n");
Peter Zijlstraeedeeab2009-03-18 12:38:47 +0100748 dump_stack();
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700749 return NULL;
750 }
751 class = lock_classes + nr_lock_classes++;
752 debug_atomic_inc(&nr_unused_locks);
753 class->key = key;
754 class->name = lock->name;
755 class->subclass = subclass;
756 INIT_LIST_HEAD(&class->lock_entry);
757 INIT_LIST_HEAD(&class->locks_before);
758 INIT_LIST_HEAD(&class->locks_after);
759 class->name_version = count_matching_names(class);
760 /*
761 * We use RCU's safe list-add method to make
762 * parallel walking of the hash-list safe:
763 */
764 list_add_tail_rcu(&class->hash_entry, hash_head);
Dale Farnsworth14811972008-02-25 23:03:02 +0100765 /*
766 * Add it to the global list of classes:
767 */
768 list_add_tail_rcu(&class->lock_entry, &all_lock_classes);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700769
770 if (verbose(class)) {
Ingo Molnar74c383f2006-12-13 00:34:43 -0800771 graph_unlock();
Ingo Molnar70e45062006-12-06 20:40:50 -0800772 raw_local_irq_restore(flags);
Ingo Molnar74c383f2006-12-13 00:34:43 -0800773
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700774 printk("\nnew class %p: %s", class->key, class->name);
775 if (class->name_version > 1)
776 printk("#%d", class->name_version);
777 printk("\n");
778 dump_stack();
Ingo Molnar74c383f2006-12-13 00:34:43 -0800779
Ingo Molnar70e45062006-12-06 20:40:50 -0800780 raw_local_irq_save(flags);
Ingo Molnar74c383f2006-12-13 00:34:43 -0800781 if (!graph_lock()) {
782 raw_local_irq_restore(flags);
783 return NULL;
784 }
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700785 }
786out_unlock_set:
Ingo Molnar74c383f2006-12-13 00:34:43 -0800787 graph_unlock();
Ingo Molnar70e45062006-12-06 20:40:50 -0800788 raw_local_irq_restore(flags);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700789
Peter Zijlstra4dfbb9d2006-10-11 01:45:14 -0400790 if (!subclass || force)
Ingo Molnard6d897c2006-07-10 04:44:04 -0700791 lock->class_cache = class;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700792
Jarek Poplawski381a2292007-02-10 01:44:58 -0800793 if (DEBUG_LOCKS_WARN_ON(class->subclass != subclass))
794 return NULL;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700795
796 return class;
797}
798
Peter Zijlstraca58abc2007-07-19 01:48:53 -0700799#ifdef CONFIG_PROVE_LOCKING
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700800/*
Peter Zijlstra8e182572007-07-19 01:48:54 -0700801 * Allocate a lockdep entry. (assumes the graph_lock held, returns
802 * with NULL on failure)
803 */
804static struct lock_list *alloc_list_entry(void)
805{
806 if (nr_list_entries >= MAX_LOCKDEP_ENTRIES) {
807 if (!debug_locks_off_graph_unlock())
808 return NULL;
809
810 printk("BUG: MAX_LOCKDEP_ENTRIES too low!\n");
811 printk("turning off the locking correctness validator.\n");
Peter Zijlstraeedeeab2009-03-18 12:38:47 +0100812 dump_stack();
Peter Zijlstra8e182572007-07-19 01:48:54 -0700813 return NULL;
814 }
815 return list_entries + nr_list_entries++;
816}
817
818/*
819 * Add a new dependency to the head of the list:
820 */
821static int add_lock_to_list(struct lock_class *class, struct lock_class *this,
822 struct list_head *head, unsigned long ip, int distance)
823{
824 struct lock_list *entry;
825 /*
826 * Lock not present yet - get a new dependency struct and
827 * add it to the list:
828 */
829 entry = alloc_list_entry();
830 if (!entry)
831 return 0;
832
Peter Zijlstra8e182572007-07-19 01:48:54 -0700833 if (!save_trace(&entry->trace))
834 return 0;
835
Zhu Yi74870172008-08-27 14:33:00 +0800836 entry->class = this;
837 entry->distance = distance;
Peter Zijlstra8e182572007-07-19 01:48:54 -0700838 /*
839 * Since we never remove from the dependency list, the list can
840 * be walked lockless by other CPUs, it's only allocation
841 * that must be protected by the spinlock. But this also means
842 * we must make new entries visible only once writes to the
843 * entry become visible - hence the RCU op:
844 */
845 list_add_tail_rcu(&entry->entry, head);
846
847 return 1;
848}
849
Peter Zijlstra98c33ed2009-07-21 13:19:07 +0200850/*
851 * For good efficiency of modular, we use power of 2
852 */
Peter Zijlstraaf012962009-07-16 15:44:29 +0200853#define MAX_CIRCULAR_QUEUE_SIZE 4096UL
854#define CQ_MASK (MAX_CIRCULAR_QUEUE_SIZE-1)
855
Peter Zijlstra98c33ed2009-07-21 13:19:07 +0200856/*
857 * The circular_queue and helpers is used to implement the
Peter Zijlstraaf012962009-07-16 15:44:29 +0200858 * breadth-first search(BFS)algorithem, by which we can build
859 * the shortest path from the next lock to be acquired to the
860 * previous held lock if there is a circular between them.
Peter Zijlstra98c33ed2009-07-21 13:19:07 +0200861 */
Peter Zijlstraaf012962009-07-16 15:44:29 +0200862struct circular_queue {
863 unsigned long element[MAX_CIRCULAR_QUEUE_SIZE];
864 unsigned int front, rear;
865};
866
867static struct circular_queue lock_cq;
Peter Zijlstraaf012962009-07-16 15:44:29 +0200868
Ming Lei12f3dfd2009-07-16 15:44:29 +0200869unsigned int max_bfs_queue_depth;
Peter Zijlstraaf012962009-07-16 15:44:29 +0200870
Ming Leie351b662009-07-22 22:48:09 +0800871static unsigned int lockdep_dependency_gen_id;
872
Peter Zijlstraaf012962009-07-16 15:44:29 +0200873static inline void __cq_init(struct circular_queue *cq)
874{
875 cq->front = cq->rear = 0;
Ming Leie351b662009-07-22 22:48:09 +0800876 lockdep_dependency_gen_id++;
Peter Zijlstraaf012962009-07-16 15:44:29 +0200877}
878
879static inline int __cq_empty(struct circular_queue *cq)
880{
881 return (cq->front == cq->rear);
882}
883
884static inline int __cq_full(struct circular_queue *cq)
885{
886 return ((cq->rear + 1) & CQ_MASK) == cq->front;
887}
888
889static inline int __cq_enqueue(struct circular_queue *cq, unsigned long elem)
890{
891 if (__cq_full(cq))
892 return -1;
893
894 cq->element[cq->rear] = elem;
895 cq->rear = (cq->rear + 1) & CQ_MASK;
896 return 0;
897}
898
899static inline int __cq_dequeue(struct circular_queue *cq, unsigned long *elem)
900{
901 if (__cq_empty(cq))
902 return -1;
903
904 *elem = cq->element[cq->front];
905 cq->front = (cq->front + 1) & CQ_MASK;
906 return 0;
907}
908
909static inline unsigned int __cq_get_elem_count(struct circular_queue *cq)
910{
911 return (cq->rear - cq->front) & CQ_MASK;
912}
913
914static inline void mark_lock_accessed(struct lock_list *lock,
915 struct lock_list *parent)
916{
917 unsigned long nr;
Peter Zijlstra98c33ed2009-07-21 13:19:07 +0200918
Peter Zijlstraaf012962009-07-16 15:44:29 +0200919 nr = lock - list_entries;
920 WARN_ON(nr >= nr_list_entries);
921 lock->parent = parent;
Ming Leie351b662009-07-22 22:48:09 +0800922 lock->class->dep_gen_id = lockdep_dependency_gen_id;
Peter Zijlstraaf012962009-07-16 15:44:29 +0200923}
924
925static inline unsigned long lock_accessed(struct lock_list *lock)
926{
927 unsigned long nr;
Peter Zijlstra98c33ed2009-07-21 13:19:07 +0200928
Peter Zijlstraaf012962009-07-16 15:44:29 +0200929 nr = lock - list_entries;
930 WARN_ON(nr >= nr_list_entries);
Ming Leie351b662009-07-22 22:48:09 +0800931 return lock->class->dep_gen_id == lockdep_dependency_gen_id;
Peter Zijlstraaf012962009-07-16 15:44:29 +0200932}
933
934static inline struct lock_list *get_lock_parent(struct lock_list *child)
935{
936 return child->parent;
937}
938
939static inline int get_lock_depth(struct lock_list *child)
940{
941 int depth = 0;
942 struct lock_list *parent;
943
944 while ((parent = get_lock_parent(child))) {
945 child = parent;
946 depth++;
947 }
948 return depth;
949}
950
Ming Lei9e2d5512009-07-16 15:44:29 +0200951static int __bfs(struct lock_list *source_entry,
Peter Zijlstraaf012962009-07-16 15:44:29 +0200952 void *data,
953 int (*match)(struct lock_list *entry, void *data),
954 struct lock_list **target_entry,
955 int forward)
Ming Leic94aa5c2009-07-16 15:44:29 +0200956{
957 struct lock_list *entry;
Ming Leid588e462009-07-16 15:44:29 +0200958 struct list_head *head;
Ming Leic94aa5c2009-07-16 15:44:29 +0200959 struct circular_queue *cq = &lock_cq;
960 int ret = 1;
961
Ming Lei9e2d5512009-07-16 15:44:29 +0200962 if (match(source_entry, data)) {
Ming Leic94aa5c2009-07-16 15:44:29 +0200963 *target_entry = source_entry;
964 ret = 0;
965 goto exit;
966 }
967
Ming Leid588e462009-07-16 15:44:29 +0200968 if (forward)
969 head = &source_entry->class->locks_after;
970 else
971 head = &source_entry->class->locks_before;
972
973 if (list_empty(head))
974 goto exit;
975
976 __cq_init(cq);
Ming Leic94aa5c2009-07-16 15:44:29 +0200977 __cq_enqueue(cq, (unsigned long)source_entry);
978
979 while (!__cq_empty(cq)) {
980 struct lock_list *lock;
Ming Leic94aa5c2009-07-16 15:44:29 +0200981
982 __cq_dequeue(cq, (unsigned long *)&lock);
983
984 if (!lock->class) {
985 ret = -2;
986 goto exit;
987 }
988
989 if (forward)
990 head = &lock->class->locks_after;
991 else
992 head = &lock->class->locks_before;
993
994 list_for_each_entry(entry, head, entry) {
995 if (!lock_accessed(entry)) {
Ming Lei12f3dfd2009-07-16 15:44:29 +0200996 unsigned int cq_depth;
Ming Leic94aa5c2009-07-16 15:44:29 +0200997 mark_lock_accessed(entry, lock);
Ming Lei9e2d5512009-07-16 15:44:29 +0200998 if (match(entry, data)) {
Ming Leic94aa5c2009-07-16 15:44:29 +0200999 *target_entry = entry;
1000 ret = 0;
1001 goto exit;
1002 }
1003
1004 if (__cq_enqueue(cq, (unsigned long)entry)) {
1005 ret = -1;
1006 goto exit;
1007 }
Ming Lei12f3dfd2009-07-16 15:44:29 +02001008 cq_depth = __cq_get_elem_count(cq);
1009 if (max_bfs_queue_depth < cq_depth)
1010 max_bfs_queue_depth = cq_depth;
Ming Leic94aa5c2009-07-16 15:44:29 +02001011 }
1012 }
1013 }
1014exit:
1015 return ret;
1016}
1017
Ming Leid7aaba12009-07-16 15:44:29 +02001018static inline int __bfs_forwards(struct lock_list *src_entry,
Ming Lei9e2d5512009-07-16 15:44:29 +02001019 void *data,
1020 int (*match)(struct lock_list *entry, void *data),
1021 struct lock_list **target_entry)
Ming Leic94aa5c2009-07-16 15:44:29 +02001022{
Ming Lei9e2d5512009-07-16 15:44:29 +02001023 return __bfs(src_entry, data, match, target_entry, 1);
Ming Leic94aa5c2009-07-16 15:44:29 +02001024
1025}
1026
Ming Leid7aaba12009-07-16 15:44:29 +02001027static inline int __bfs_backwards(struct lock_list *src_entry,
Ming Lei9e2d5512009-07-16 15:44:29 +02001028 void *data,
1029 int (*match)(struct lock_list *entry, void *data),
1030 struct lock_list **target_entry)
Ming Leic94aa5c2009-07-16 15:44:29 +02001031{
Ming Lei9e2d5512009-07-16 15:44:29 +02001032 return __bfs(src_entry, data, match, target_entry, 0);
Ming Leic94aa5c2009-07-16 15:44:29 +02001033
1034}
1035
Peter Zijlstra8e182572007-07-19 01:48:54 -07001036/*
1037 * Recursive, forwards-direction lock-dependency checking, used for
1038 * both noncyclic checking and for hardirq-unsafe/softirq-unsafe
1039 * checking.
Peter Zijlstra8e182572007-07-19 01:48:54 -07001040 */
Peter Zijlstra8e182572007-07-19 01:48:54 -07001041
1042/*
1043 * Print a dependency chain entry (this is only done when a deadlock
1044 * has been detected):
1045 */
1046static noinline int
Ming Lei24208ca2009-07-16 15:44:29 +02001047print_circular_bug_entry(struct lock_list *target, int depth)
Peter Zijlstra8e182572007-07-19 01:48:54 -07001048{
1049 if (debug_locks_silent)
1050 return 0;
1051 printk("\n-> #%u", depth);
1052 print_lock_name(target->class);
1053 printk(":\n");
1054 print_stack_trace(&target->trace, 6);
1055
1056 return 0;
1057}
1058
1059/*
1060 * When a circular dependency is detected, print the
1061 * header first:
1062 */
1063static noinline int
Ming Leidb0002a2009-07-16 15:44:29 +02001064print_circular_bug_header(struct lock_list *entry, unsigned int depth,
1065 struct held_lock *check_src,
1066 struct held_lock *check_tgt)
Peter Zijlstra8e182572007-07-19 01:48:54 -07001067{
1068 struct task_struct *curr = current;
1069
Ming Leic94aa5c2009-07-16 15:44:29 +02001070 if (debug_locks_silent)
Peter Zijlstra8e182572007-07-19 01:48:54 -07001071 return 0;
1072
1073 printk("\n=======================================================\n");
1074 printk( "[ INFO: possible circular locking dependency detected ]\n");
1075 print_kernel_version();
1076 printk( "-------------------------------------------------------\n");
1077 printk("%s/%d is trying to acquire lock:\n",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07001078 curr->comm, task_pid_nr(curr));
Ming Leidb0002a2009-07-16 15:44:29 +02001079 print_lock(check_src);
Peter Zijlstra8e182572007-07-19 01:48:54 -07001080 printk("\nbut task is already holding lock:\n");
Ming Leidb0002a2009-07-16 15:44:29 +02001081 print_lock(check_tgt);
Peter Zijlstra8e182572007-07-19 01:48:54 -07001082 printk("\nwhich lock already depends on the new lock.\n\n");
1083 printk("\nthe existing dependency chain (in reverse order) is:\n");
1084
1085 print_circular_bug_entry(entry, depth);
1086
1087 return 0;
1088}
1089
Ming Lei9e2d5512009-07-16 15:44:29 +02001090static inline int class_equal(struct lock_list *entry, void *data)
1091{
1092 return entry->class == data;
1093}
1094
Ming Leidb0002a2009-07-16 15:44:29 +02001095static noinline int print_circular_bug(struct lock_list *this,
1096 struct lock_list *target,
1097 struct held_lock *check_src,
1098 struct held_lock *check_tgt)
Peter Zijlstra8e182572007-07-19 01:48:54 -07001099{
1100 struct task_struct *curr = current;
Ming Leic94aa5c2009-07-16 15:44:29 +02001101 struct lock_list *parent;
Ming Lei24208ca2009-07-16 15:44:29 +02001102 int depth;
Peter Zijlstra8e182572007-07-19 01:48:54 -07001103
Ming Leic94aa5c2009-07-16 15:44:29 +02001104 if (!debug_locks_off_graph_unlock() || debug_locks_silent)
Peter Zijlstra8e182572007-07-19 01:48:54 -07001105 return 0;
1106
Ming Leidb0002a2009-07-16 15:44:29 +02001107 if (!save_trace(&this->trace))
Peter Zijlstra8e182572007-07-19 01:48:54 -07001108 return 0;
1109
Ming Leic94aa5c2009-07-16 15:44:29 +02001110 depth = get_lock_depth(target);
1111
Ming Leidb0002a2009-07-16 15:44:29 +02001112 print_circular_bug_header(target, depth, check_src, check_tgt);
Ming Leic94aa5c2009-07-16 15:44:29 +02001113
1114 parent = get_lock_parent(target);
1115
1116 while (parent) {
1117 print_circular_bug_entry(parent, --depth);
1118 parent = get_lock_parent(parent);
1119 }
Peter Zijlstra8e182572007-07-19 01:48:54 -07001120
1121 printk("\nother info that might help us debug this:\n\n");
1122 lockdep_print_held_locks(curr);
1123
1124 printk("\nstack backtrace:\n");
1125 dump_stack();
1126
1127 return 0;
1128}
1129
Ming Leidb0002a2009-07-16 15:44:29 +02001130static noinline int print_bfs_bug(int ret)
1131{
1132 if (!debug_locks_off_graph_unlock())
1133 return 0;
1134
1135 WARN(1, "lockdep bfs error:%d\n", ret);
1136
1137 return 0;
1138}
1139
Ming Leief681022009-07-16 15:44:29 +02001140static int noop_count(struct lock_list *entry, void *data)
David Miller419ca3f2008-07-29 21:45:03 -07001141{
Ming Leief681022009-07-16 15:44:29 +02001142 (*(unsigned long *)data)++;
1143 return 0;
David Miller419ca3f2008-07-29 21:45:03 -07001144}
1145
Ming Leief681022009-07-16 15:44:29 +02001146unsigned long __lockdep_count_forward_deps(struct lock_list *this)
1147{
1148 unsigned long count = 0;
1149 struct lock_list *uninitialized_var(target_entry);
1150
1151 __bfs_forwards(this, (void *)&count, noop_count, &target_entry);
1152
1153 return count;
1154}
David Miller419ca3f2008-07-29 21:45:03 -07001155unsigned long lockdep_count_forward_deps(struct lock_class *class)
1156{
1157 unsigned long ret, flags;
Ming Leief681022009-07-16 15:44:29 +02001158 struct lock_list this;
1159
1160 this.parent = NULL;
1161 this.class = class;
David Miller419ca3f2008-07-29 21:45:03 -07001162
1163 local_irq_save(flags);
1164 __raw_spin_lock(&lockdep_lock);
Ming Leief681022009-07-16 15:44:29 +02001165 ret = __lockdep_count_forward_deps(&this);
David Miller419ca3f2008-07-29 21:45:03 -07001166 __raw_spin_unlock(&lockdep_lock);
1167 local_irq_restore(flags);
1168
1169 return ret;
1170}
1171
Ming Leief681022009-07-16 15:44:29 +02001172unsigned long __lockdep_count_backward_deps(struct lock_list *this)
David Miller419ca3f2008-07-29 21:45:03 -07001173{
Ming Leief681022009-07-16 15:44:29 +02001174 unsigned long count = 0;
1175 struct lock_list *uninitialized_var(target_entry);
David Miller419ca3f2008-07-29 21:45:03 -07001176
Ming Leief681022009-07-16 15:44:29 +02001177 __bfs_backwards(this, (void *)&count, noop_count, &target_entry);
David Miller419ca3f2008-07-29 21:45:03 -07001178
Ming Leief681022009-07-16 15:44:29 +02001179 return count;
David Miller419ca3f2008-07-29 21:45:03 -07001180}
1181
1182unsigned long lockdep_count_backward_deps(struct lock_class *class)
1183{
1184 unsigned long ret, flags;
Ming Leief681022009-07-16 15:44:29 +02001185 struct lock_list this;
1186
1187 this.parent = NULL;
1188 this.class = class;
David Miller419ca3f2008-07-29 21:45:03 -07001189
1190 local_irq_save(flags);
1191 __raw_spin_lock(&lockdep_lock);
Ming Leief681022009-07-16 15:44:29 +02001192 ret = __lockdep_count_backward_deps(&this);
David Miller419ca3f2008-07-29 21:45:03 -07001193 __raw_spin_unlock(&lockdep_lock);
1194 local_irq_restore(flags);
1195
1196 return ret;
1197}
1198
Peter Zijlstra8e182572007-07-19 01:48:54 -07001199/*
1200 * Prove that the dependency graph starting at <entry> can not
1201 * lead to <target>. Print an error and return 0 if it does.
1202 */
1203static noinline int
Ming Leidb0002a2009-07-16 15:44:29 +02001204check_noncircular(struct lock_list *root, struct lock_class *target,
1205 struct lock_list **target_entry)
Peter Zijlstra8e182572007-07-19 01:48:54 -07001206{
Ming Leidb0002a2009-07-16 15:44:29 +02001207 int result;
Peter Zijlstra8e182572007-07-19 01:48:54 -07001208
Ming Leidb0002a2009-07-16 15:44:29 +02001209 debug_atomic_inc(&nr_cyclic_checks);
David Miller419ca3f2008-07-29 21:45:03 -07001210
Ming Leid7aaba12009-07-16 15:44:29 +02001211 result = __bfs_forwards(root, target, class_equal, target_entry);
Ming Leidb0002a2009-07-16 15:44:29 +02001212
1213 return result;
Peter Zijlstra8e182572007-07-19 01:48:54 -07001214}
1215
Steven Rostedt81d68a92008-05-12 21:20:42 +02001216#if defined(CONFIG_TRACE_IRQFLAGS) && defined(CONFIG_PROVE_LOCKING)
Peter Zijlstra8e182572007-07-19 01:48:54 -07001217/*
1218 * Forwards and backwards subgraph searching, for the purposes of
1219 * proving that two subgraphs can be connected by a new dependency
1220 * without creating any illegal irq-safe -> irq-unsafe lock dependency.
1221 */
Peter Zijlstra8e182572007-07-19 01:48:54 -07001222
Ming Leid7aaba12009-07-16 15:44:29 +02001223static inline int usage_match(struct lock_list *entry, void *bit)
1224{
1225 return entry->class->usage_mask & (1 << (enum lock_usage_bit)bit);
1226}
1227
1228
1229
Peter Zijlstra8e182572007-07-19 01:48:54 -07001230/*
1231 * Find a node in the forwards-direction dependency sub-graph starting
Ming Leid7aaba12009-07-16 15:44:29 +02001232 * at @root->class that matches @bit.
Peter Zijlstra8e182572007-07-19 01:48:54 -07001233 *
Ming Leid7aaba12009-07-16 15:44:29 +02001234 * Return 0 if such a node exists in the subgraph, and put that node
1235 * into *@target_entry.
Peter Zijlstra8e182572007-07-19 01:48:54 -07001236 *
Ming Leid7aaba12009-07-16 15:44:29 +02001237 * Return 1 otherwise and keep *@target_entry unchanged.
1238 * Return <0 on error.
Peter Zijlstra8e182572007-07-19 01:48:54 -07001239 */
Ming Leid7aaba12009-07-16 15:44:29 +02001240static int
1241find_usage_forwards(struct lock_list *root, enum lock_usage_bit bit,
1242 struct lock_list **target_entry)
Peter Zijlstra8e182572007-07-19 01:48:54 -07001243{
Ming Leid7aaba12009-07-16 15:44:29 +02001244 int result;
Peter Zijlstra8e182572007-07-19 01:48:54 -07001245
1246 debug_atomic_inc(&nr_find_usage_forwards_checks);
Peter Zijlstra8e182572007-07-19 01:48:54 -07001247
Ming Leid7aaba12009-07-16 15:44:29 +02001248 result = __bfs_forwards(root, (void *)bit, usage_match, target_entry);
1249
1250 return result;
Peter Zijlstra8e182572007-07-19 01:48:54 -07001251}
1252
1253/*
1254 * Find a node in the backwards-direction dependency sub-graph starting
Ming Leid7aaba12009-07-16 15:44:29 +02001255 * at @root->class that matches @bit.
Peter Zijlstra8e182572007-07-19 01:48:54 -07001256 *
Ming Leid7aaba12009-07-16 15:44:29 +02001257 * Return 0 if such a node exists in the subgraph, and put that node
1258 * into *@target_entry.
Peter Zijlstra8e182572007-07-19 01:48:54 -07001259 *
Ming Leid7aaba12009-07-16 15:44:29 +02001260 * Return 1 otherwise and keep *@target_entry unchanged.
1261 * Return <0 on error.
Peter Zijlstra8e182572007-07-19 01:48:54 -07001262 */
Ming Leid7aaba12009-07-16 15:44:29 +02001263static int
1264find_usage_backwards(struct lock_list *root, enum lock_usage_bit bit,
1265 struct lock_list **target_entry)
Peter Zijlstra8e182572007-07-19 01:48:54 -07001266{
Ming Leid7aaba12009-07-16 15:44:29 +02001267 int result;
Peter Zijlstra8e182572007-07-19 01:48:54 -07001268
1269 debug_atomic_inc(&nr_find_usage_backwards_checks);
Peter Zijlstra8e182572007-07-19 01:48:54 -07001270
Ming Leid7aaba12009-07-16 15:44:29 +02001271 result = __bfs_backwards(root, (void *)bit, usage_match, target_entry);
Dave Jonesf82b2172008-08-11 09:30:23 +02001272
Ming Leid7aaba12009-07-16 15:44:29 +02001273 return result;
Peter Zijlstra8e182572007-07-19 01:48:54 -07001274}
1275
Peter Zijlstraaf012962009-07-16 15:44:29 +02001276static void print_lock_class_header(struct lock_class *class, int depth)
1277{
1278 int bit;
1279
1280 printk("%*s->", depth, "");
1281 print_lock_name(class);
1282 printk(" ops: %lu", class->ops);
1283 printk(" {\n");
1284
1285 for (bit = 0; bit < LOCK_USAGE_STATES; bit++) {
1286 if (class->usage_mask & (1 << bit)) {
1287 int len = depth;
1288
1289 len += printk("%*s %s", depth, "", usage_str[bit]);
1290 len += printk(" at:\n");
1291 print_stack_trace(class->usage_traces + bit, len);
1292 }
1293 }
1294 printk("%*s }\n", depth, "");
1295
1296 printk("%*s ... key at: ",depth,"");
1297 print_ip_sym((unsigned long)class->key);
1298}
1299
1300/*
1301 * printk the shortest lock dependencies from @start to @end in reverse order:
1302 */
1303static void __used
1304print_shortest_lock_dependencies(struct lock_list *leaf,
1305 struct lock_list *root)
1306{
1307 struct lock_list *entry = leaf;
1308 int depth;
1309
1310 /*compute depth from generated tree by BFS*/
1311 depth = get_lock_depth(leaf);
1312
1313 do {
1314 print_lock_class_header(entry->class, depth);
1315 printk("%*s ... acquired at:\n", depth, "");
1316 print_stack_trace(&entry->trace, 2);
1317 printk("\n");
1318
1319 if (depth == 0 && (entry != root)) {
1320 printk("lockdep:%s bad BFS generated tree\n", __func__);
1321 break;
1322 }
1323
1324 entry = get_lock_parent(entry);
1325 depth--;
1326 } while (entry && (depth >= 0));
1327
1328 return;
1329}
Ming Leid7aaba12009-07-16 15:44:29 +02001330
Peter Zijlstra8e182572007-07-19 01:48:54 -07001331static int
1332print_bad_irq_dependency(struct task_struct *curr,
Ming Lei24208ca2009-07-16 15:44:29 +02001333 struct lock_list *prev_root,
1334 struct lock_list *next_root,
1335 struct lock_list *backwards_entry,
1336 struct lock_list *forwards_entry,
Peter Zijlstra8e182572007-07-19 01:48:54 -07001337 struct held_lock *prev,
1338 struct held_lock *next,
1339 enum lock_usage_bit bit1,
1340 enum lock_usage_bit bit2,
1341 const char *irqclass)
1342{
1343 if (!debug_locks_off_graph_unlock() || debug_locks_silent)
1344 return 0;
1345
1346 printk("\n======================================================\n");
1347 printk( "[ INFO: %s-safe -> %s-unsafe lock order detected ]\n",
1348 irqclass, irqclass);
1349 print_kernel_version();
1350 printk( "------------------------------------------------------\n");
1351 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 -07001352 curr->comm, task_pid_nr(curr),
Peter Zijlstra8e182572007-07-19 01:48:54 -07001353 curr->hardirq_context, hardirq_count() >> HARDIRQ_SHIFT,
1354 curr->softirq_context, softirq_count() >> SOFTIRQ_SHIFT,
1355 curr->hardirqs_enabled,
1356 curr->softirqs_enabled);
1357 print_lock(next);
1358
1359 printk("\nand this task is already holding:\n");
1360 print_lock(prev);
1361 printk("which would create a new lock dependency:\n");
Dave Jonesf82b2172008-08-11 09:30:23 +02001362 print_lock_name(hlock_class(prev));
Peter Zijlstra8e182572007-07-19 01:48:54 -07001363 printk(" ->");
Dave Jonesf82b2172008-08-11 09:30:23 +02001364 print_lock_name(hlock_class(next));
Peter Zijlstra8e182572007-07-19 01:48:54 -07001365 printk("\n");
1366
1367 printk("\nbut this new dependency connects a %s-irq-safe lock:\n",
1368 irqclass);
Ming Lei24208ca2009-07-16 15:44:29 +02001369 print_lock_name(backwards_entry->class);
Peter Zijlstra8e182572007-07-19 01:48:54 -07001370 printk("\n... which became %s-irq-safe at:\n", irqclass);
1371
Ming Lei24208ca2009-07-16 15:44:29 +02001372 print_stack_trace(backwards_entry->class->usage_traces + bit1, 1);
Peter Zijlstra8e182572007-07-19 01:48:54 -07001373
1374 printk("\nto a %s-irq-unsafe lock:\n", irqclass);
Ming Lei24208ca2009-07-16 15:44:29 +02001375 print_lock_name(forwards_entry->class);
Peter Zijlstra8e182572007-07-19 01:48:54 -07001376 printk("\n... which became %s-irq-unsafe at:\n", irqclass);
1377 printk("...");
1378
Ming Lei24208ca2009-07-16 15:44:29 +02001379 print_stack_trace(forwards_entry->class->usage_traces + bit2, 1);
Peter Zijlstra8e182572007-07-19 01:48:54 -07001380
1381 printk("\nother info that might help us debug this:\n\n");
1382 lockdep_print_held_locks(curr);
1383
Ming Lei24208ca2009-07-16 15:44:29 +02001384 printk("\nthe dependencies between %s-irq-safe lock", irqclass);
1385 printk(" and the holding lock:\n");
1386 if (!save_trace(&prev_root->trace))
1387 return 0;
1388 print_shortest_lock_dependencies(backwards_entry, prev_root);
Peter Zijlstra8e182572007-07-19 01:48:54 -07001389
Ming Lei24208ca2009-07-16 15:44:29 +02001390 printk("\nthe dependencies between the lock to be acquired");
1391 printk(" and %s-irq-unsafe lock:\n", irqclass);
1392 if (!save_trace(&next_root->trace))
1393 return 0;
1394 print_shortest_lock_dependencies(forwards_entry, next_root);
Peter Zijlstra8e182572007-07-19 01:48:54 -07001395
1396 printk("\nstack backtrace:\n");
1397 dump_stack();
1398
1399 return 0;
1400}
1401
1402static int
1403check_usage(struct task_struct *curr, struct held_lock *prev,
1404 struct held_lock *next, enum lock_usage_bit bit_backwards,
1405 enum lock_usage_bit bit_forwards, const char *irqclass)
1406{
1407 int ret;
Ming Lei24208ca2009-07-16 15:44:29 +02001408 struct lock_list this, that;
Ming Leid7aaba12009-07-16 15:44:29 +02001409 struct lock_list *uninitialized_var(target_entry);
Ming Lei24208ca2009-07-16 15:44:29 +02001410 struct lock_list *uninitialized_var(target_entry1);
Peter Zijlstra8e182572007-07-19 01:48:54 -07001411
Ming Leid7aaba12009-07-16 15:44:29 +02001412 this.parent = NULL;
Peter Zijlstra8e182572007-07-19 01:48:54 -07001413
Ming Leid7aaba12009-07-16 15:44:29 +02001414 this.class = hlock_class(prev);
1415 ret = find_usage_backwards(&this, bit_backwards, &target_entry);
Peter Zijlstraaf012962009-07-16 15:44:29 +02001416 if (ret < 0)
1417 return print_bfs_bug(ret);
1418 if (ret == 1)
1419 return ret;
Ming Leid7aaba12009-07-16 15:44:29 +02001420
Ming Lei24208ca2009-07-16 15:44:29 +02001421 that.parent = NULL;
1422 that.class = hlock_class(next);
1423 ret = find_usage_forwards(&that, bit_forwards, &target_entry1);
Peter Zijlstraaf012962009-07-16 15:44:29 +02001424 if (ret < 0)
1425 return print_bfs_bug(ret);
1426 if (ret == 1)
1427 return ret;
Ming Leid7aaba12009-07-16 15:44:29 +02001428
Ming Lei24208ca2009-07-16 15:44:29 +02001429 return print_bad_irq_dependency(curr, &this, &that,
1430 target_entry, target_entry1,
1431 prev, next,
Peter Zijlstra8e182572007-07-19 01:48:54 -07001432 bit_backwards, bit_forwards, irqclass);
1433}
1434
Peter Zijlstra4f367d8a2009-01-22 18:10:42 +01001435static const char *state_names[] = {
1436#define LOCKDEP_STATE(__STATE) \
Peter Zijlstrab4b136f2009-01-29 14:50:36 +01001437 __stringify(__STATE),
Peter Zijlstra4f367d8a2009-01-22 18:10:42 +01001438#include "lockdep_states.h"
1439#undef LOCKDEP_STATE
1440};
1441
1442static const char *state_rnames[] = {
1443#define LOCKDEP_STATE(__STATE) \
Peter Zijlstrab4b136f2009-01-29 14:50:36 +01001444 __stringify(__STATE)"-READ",
Peter Zijlstra4f367d8a2009-01-22 18:10:42 +01001445#include "lockdep_states.h"
1446#undef LOCKDEP_STATE
1447};
1448
1449static inline const char *state_name(enum lock_usage_bit bit)
1450{
1451 return (bit & 1) ? state_rnames[bit >> 2] : state_names[bit >> 2];
1452}
1453
1454static int exclusive_bit(int new_bit)
1455{
1456 /*
1457 * USED_IN
1458 * USED_IN_READ
1459 * ENABLED
1460 * ENABLED_READ
1461 *
1462 * bit 0 - write/read
1463 * bit 1 - used_in/enabled
1464 * bit 2+ state
1465 */
1466
1467 int state = new_bit & ~3;
1468 int dir = new_bit & 2;
1469
1470 /*
1471 * keep state, bit flip the direction and strip read.
1472 */
1473 return state | (dir ^ 2);
1474}
1475
1476static int check_irq_usage(struct task_struct *curr, struct held_lock *prev,
1477 struct held_lock *next, enum lock_usage_bit bit)
Peter Zijlstra8e182572007-07-19 01:48:54 -07001478{
1479 /*
1480 * Prove that the new dependency does not connect a hardirq-safe
1481 * lock with a hardirq-unsafe lock - to achieve this we search
1482 * the backwards-subgraph starting at <prev>, and the
1483 * forwards-subgraph starting at <next>:
1484 */
Peter Zijlstra4f367d8a2009-01-22 18:10:42 +01001485 if (!check_usage(curr, prev, next, bit,
1486 exclusive_bit(bit), state_name(bit)))
Peter Zijlstra8e182572007-07-19 01:48:54 -07001487 return 0;
1488
Peter Zijlstra4f367d8a2009-01-22 18:10:42 +01001489 bit++; /* _READ */
1490
Peter Zijlstra8e182572007-07-19 01:48:54 -07001491 /*
1492 * Prove that the new dependency does not connect a hardirq-safe-read
1493 * lock with a hardirq-unsafe lock - to achieve this we search
1494 * the backwards-subgraph starting at <prev>, and the
1495 * forwards-subgraph starting at <next>:
1496 */
Peter Zijlstra4f367d8a2009-01-22 18:10:42 +01001497 if (!check_usage(curr, prev, next, bit,
1498 exclusive_bit(bit), state_name(bit)))
Peter Zijlstra8e182572007-07-19 01:48:54 -07001499 return 0;
1500
Peter Zijlstra4f367d8a2009-01-22 18:10:42 +01001501 return 1;
1502}
Peter Zijlstra8e182572007-07-19 01:48:54 -07001503
Peter Zijlstra4f367d8a2009-01-22 18:10:42 +01001504static int
1505check_prev_add_irq(struct task_struct *curr, struct held_lock *prev,
1506 struct held_lock *next)
1507{
1508#define LOCKDEP_STATE(__STATE) \
1509 if (!check_irq_usage(curr, prev, next, LOCK_USED_IN_##__STATE)) \
Nick Piggincf40bd12009-01-21 08:12:39 +01001510 return 0;
Peter Zijlstra4f367d8a2009-01-22 18:10:42 +01001511#include "lockdep_states.h"
1512#undef LOCKDEP_STATE
Nick Piggincf40bd12009-01-21 08:12:39 +01001513
Peter Zijlstra8e182572007-07-19 01:48:54 -07001514 return 1;
1515}
1516
1517static void inc_chains(void)
1518{
1519 if (current->hardirq_context)
1520 nr_hardirq_chains++;
1521 else {
1522 if (current->softirq_context)
1523 nr_softirq_chains++;
1524 else
1525 nr_process_chains++;
1526 }
1527}
1528
1529#else
1530
1531static inline int
1532check_prev_add_irq(struct task_struct *curr, struct held_lock *prev,
1533 struct held_lock *next)
1534{
1535 return 1;
1536}
1537
1538static inline void inc_chains(void)
1539{
1540 nr_process_chains++;
1541}
1542
1543#endif
1544
1545static int
1546print_deadlock_bug(struct task_struct *curr, struct held_lock *prev,
1547 struct held_lock *next)
1548{
1549 if (!debug_locks_off_graph_unlock() || debug_locks_silent)
1550 return 0;
1551
1552 printk("\n=============================================\n");
1553 printk( "[ INFO: possible recursive locking detected ]\n");
1554 print_kernel_version();
1555 printk( "---------------------------------------------\n");
1556 printk("%s/%d is trying to acquire lock:\n",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07001557 curr->comm, task_pid_nr(curr));
Peter Zijlstra8e182572007-07-19 01:48:54 -07001558 print_lock(next);
1559 printk("\nbut task is already holding lock:\n");
1560 print_lock(prev);
1561
1562 printk("\nother info that might help us debug this:\n");
1563 lockdep_print_held_locks(curr);
1564
1565 printk("\nstack backtrace:\n");
1566 dump_stack();
1567
1568 return 0;
1569}
1570
1571/*
1572 * Check whether we are holding such a class already.
1573 *
1574 * (Note that this has to be done separately, because the graph cannot
1575 * detect such classes of deadlocks.)
1576 *
1577 * Returns: 0 on deadlock detected, 1 on OK, 2 on recursive read
1578 */
1579static int
1580check_deadlock(struct task_struct *curr, struct held_lock *next,
1581 struct lockdep_map *next_instance, int read)
1582{
1583 struct held_lock *prev;
Peter Zijlstra7531e2f2008-08-11 09:30:24 +02001584 struct held_lock *nest = NULL;
Peter Zijlstra8e182572007-07-19 01:48:54 -07001585 int i;
1586
1587 for (i = 0; i < curr->lockdep_depth; i++) {
1588 prev = curr->held_locks + i;
Peter Zijlstra7531e2f2008-08-11 09:30:24 +02001589
1590 if (prev->instance == next->nest_lock)
1591 nest = prev;
1592
Dave Jonesf82b2172008-08-11 09:30:23 +02001593 if (hlock_class(prev) != hlock_class(next))
Peter Zijlstra8e182572007-07-19 01:48:54 -07001594 continue;
Peter Zijlstra7531e2f2008-08-11 09:30:24 +02001595
Peter Zijlstra8e182572007-07-19 01:48:54 -07001596 /*
1597 * Allow read-after-read recursion of the same
1598 * lock class (i.e. read_lock(lock)+read_lock(lock)):
1599 */
1600 if ((read == 2) && prev->read)
1601 return 2;
Peter Zijlstra7531e2f2008-08-11 09:30:24 +02001602
1603 /*
1604 * We're holding the nest_lock, which serializes this lock's
1605 * nesting behaviour.
1606 */
1607 if (nest)
1608 return 2;
1609
Peter Zijlstra8e182572007-07-19 01:48:54 -07001610 return print_deadlock_bug(curr, prev, next);
1611 }
1612 return 1;
1613}
1614
1615/*
1616 * There was a chain-cache miss, and we are about to add a new dependency
1617 * to a previous lock. We recursively validate the following rules:
1618 *
1619 * - would the adding of the <prev> -> <next> dependency create a
1620 * circular dependency in the graph? [== circular deadlock]
1621 *
1622 * - does the new prev->next dependency connect any hardirq-safe lock
1623 * (in the full backwards-subgraph starting at <prev>) with any
1624 * hardirq-unsafe lock (in the full forwards-subgraph starting at
1625 * <next>)? [== illegal lock inversion with hardirq contexts]
1626 *
1627 * - does the new prev->next dependency connect any softirq-safe lock
1628 * (in the full backwards-subgraph starting at <prev>) with any
1629 * softirq-unsafe lock (in the full forwards-subgraph starting at
1630 * <next>)? [== illegal lock inversion with softirq contexts]
1631 *
1632 * any of these scenarios could lead to a deadlock.
1633 *
1634 * Then if all the validations pass, we add the forwards and backwards
1635 * dependency.
1636 */
1637static int
1638check_prev_add(struct task_struct *curr, struct held_lock *prev,
1639 struct held_lock *next, int distance)
1640{
1641 struct lock_list *entry;
1642 int ret;
Ming Leidb0002a2009-07-16 15:44:29 +02001643 struct lock_list this;
1644 struct lock_list *uninitialized_var(target_entry);
Peter Zijlstra8e182572007-07-19 01:48:54 -07001645
1646 /*
1647 * Prove that the new <prev> -> <next> dependency would not
1648 * create a circular dependency in the graph. (We do this by
1649 * forward-recursing into the graph starting at <next>, and
1650 * checking whether we can reach <prev>.)
1651 *
1652 * We are using global variables to control the recursion, to
1653 * keep the stackframe size of the recursive functions low:
1654 */
Ming Leidb0002a2009-07-16 15:44:29 +02001655 this.class = hlock_class(next);
1656 this.parent = NULL;
1657 ret = check_noncircular(&this, hlock_class(prev), &target_entry);
1658 if (unlikely(!ret))
1659 return print_circular_bug(&this, target_entry, next, prev);
1660 else if (unlikely(ret < 0))
1661 return print_bfs_bug(ret);
Ming Leic94aa5c2009-07-16 15:44:29 +02001662
Peter Zijlstra8e182572007-07-19 01:48:54 -07001663 if (!check_prev_add_irq(curr, prev, next))
1664 return 0;
1665
1666 /*
1667 * For recursive read-locks we do all the dependency checks,
1668 * but we dont store read-triggered dependencies (only
1669 * write-triggered dependencies). This ensures that only the
1670 * write-side dependencies matter, and that if for example a
1671 * write-lock never takes any other locks, then the reads are
1672 * equivalent to a NOP.
1673 */
1674 if (next->read == 2 || prev->read == 2)
1675 return 1;
1676 /*
1677 * Is the <prev> -> <next> dependency already present?
1678 *
1679 * (this may occur even though this is a new chain: consider
1680 * e.g. the L1 -> L2 -> L3 -> L4 and the L5 -> L1 -> L2 -> L3
1681 * chains - the second one will be new, but L1 already has
1682 * L2 added to its dependency list, due to the first chain.)
1683 */
Dave Jonesf82b2172008-08-11 09:30:23 +02001684 list_for_each_entry(entry, &hlock_class(prev)->locks_after, entry) {
1685 if (entry->class == hlock_class(next)) {
Peter Zijlstra8e182572007-07-19 01:48:54 -07001686 if (distance == 1)
1687 entry->distance = 1;
1688 return 2;
1689 }
1690 }
1691
1692 /*
1693 * Ok, all validations passed, add the new lock
1694 * to the previous lock's dependency list:
1695 */
Dave Jonesf82b2172008-08-11 09:30:23 +02001696 ret = add_lock_to_list(hlock_class(prev), hlock_class(next),
1697 &hlock_class(prev)->locks_after,
1698 next->acquire_ip, distance);
Peter Zijlstra8e182572007-07-19 01:48:54 -07001699
1700 if (!ret)
1701 return 0;
1702
Dave Jonesf82b2172008-08-11 09:30:23 +02001703 ret = add_lock_to_list(hlock_class(next), hlock_class(prev),
1704 &hlock_class(next)->locks_before,
1705 next->acquire_ip, distance);
Peter Zijlstra8e182572007-07-19 01:48:54 -07001706 if (!ret)
1707 return 0;
1708
1709 /*
1710 * Debugging printouts:
1711 */
Dave Jonesf82b2172008-08-11 09:30:23 +02001712 if (verbose(hlock_class(prev)) || verbose(hlock_class(next))) {
Peter Zijlstra8e182572007-07-19 01:48:54 -07001713 graph_unlock();
1714 printk("\n new dependency: ");
Dave Jonesf82b2172008-08-11 09:30:23 +02001715 print_lock_name(hlock_class(prev));
Peter Zijlstra8e182572007-07-19 01:48:54 -07001716 printk(" => ");
Dave Jonesf82b2172008-08-11 09:30:23 +02001717 print_lock_name(hlock_class(next));
Peter Zijlstra8e182572007-07-19 01:48:54 -07001718 printk("\n");
1719 dump_stack();
1720 return graph_lock();
1721 }
1722 return 1;
1723}
1724
1725/*
1726 * Add the dependency to all directly-previous locks that are 'relevant'.
1727 * The ones that are relevant are (in increasing distance from curr):
1728 * all consecutive trylock entries and the final non-trylock entry - or
1729 * the end of this context's lock-chain - whichever comes first.
1730 */
1731static int
1732check_prevs_add(struct task_struct *curr, struct held_lock *next)
1733{
1734 int depth = curr->lockdep_depth;
1735 struct held_lock *hlock;
1736
1737 /*
1738 * Debugging checks.
1739 *
1740 * Depth must not be zero for a non-head lock:
1741 */
1742 if (!depth)
1743 goto out_bug;
1744 /*
1745 * At least two relevant locks must exist for this
1746 * to be a head:
1747 */
1748 if (curr->held_locks[depth].irq_context !=
1749 curr->held_locks[depth-1].irq_context)
1750 goto out_bug;
1751
1752 for (;;) {
1753 int distance = curr->lockdep_depth - depth + 1;
1754 hlock = curr->held_locks + depth-1;
1755 /*
1756 * Only non-recursive-read entries get new dependencies
1757 * added:
1758 */
1759 if (hlock->read != 2) {
1760 if (!check_prev_add(curr, hlock, next, distance))
1761 return 0;
1762 /*
1763 * Stop after the first non-trylock entry,
1764 * as non-trylock entries have added their
1765 * own direct dependencies already, so this
1766 * lock is connected to them indirectly:
1767 */
1768 if (!hlock->trylock)
1769 break;
1770 }
1771 depth--;
1772 /*
1773 * End of lock-stack?
1774 */
1775 if (!depth)
1776 break;
1777 /*
1778 * Stop the search if we cross into another context:
1779 */
1780 if (curr->held_locks[depth].irq_context !=
1781 curr->held_locks[depth-1].irq_context)
1782 break;
1783 }
1784 return 1;
1785out_bug:
1786 if (!debug_locks_off_graph_unlock())
1787 return 0;
1788
1789 WARN_ON(1);
1790
1791 return 0;
1792}
1793
1794unsigned long nr_lock_chains;
Huang, Ying443cd502008-06-20 16:39:21 +08001795struct lock_chain lock_chains[MAX_LOCKDEP_CHAINS];
Huang, Yingcd1a28e2008-06-23 11:20:54 +08001796int nr_chain_hlocks;
Huang, Ying443cd502008-06-20 16:39:21 +08001797static u16 chain_hlocks[MAX_LOCKDEP_CHAIN_HLOCKS];
1798
1799struct lock_class *lock_chain_get_class(struct lock_chain *chain, int i)
1800{
1801 return lock_classes + chain_hlocks[chain->base + i];
1802}
Peter Zijlstra8e182572007-07-19 01:48:54 -07001803
1804/*
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001805 * Look up a dependency chain. If the key is not present yet then
Jarek Poplawski9e860d02007-05-08 00:30:12 -07001806 * add it and return 1 - in this case the new dependency chain is
1807 * validated. If the key is already hashed, return 0.
1808 * (On return with 1 graph_lock is held.)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001809 */
Huang, Ying443cd502008-06-20 16:39:21 +08001810static inline int lookup_chain_cache(struct task_struct *curr,
1811 struct held_lock *hlock,
1812 u64 chain_key)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001813{
Dave Jonesf82b2172008-08-11 09:30:23 +02001814 struct lock_class *class = hlock_class(hlock);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001815 struct list_head *hash_head = chainhashentry(chain_key);
1816 struct lock_chain *chain;
Huang, Ying443cd502008-06-20 16:39:21 +08001817 struct held_lock *hlock_curr, *hlock_next;
Huang, Yingcd1a28e2008-06-23 11:20:54 +08001818 int i, j, n, cn;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001819
Jarek Poplawski381a2292007-02-10 01:44:58 -08001820 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
1821 return 0;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001822 /*
1823 * We can walk it lock-free, because entries only get added
1824 * to the hash:
1825 */
1826 list_for_each_entry(chain, hash_head, entry) {
1827 if (chain->chain_key == chain_key) {
1828cache_hit:
1829 debug_atomic_inc(&chain_lookup_hits);
Ingo Molnar81fc6852006-12-13 00:34:40 -08001830 if (very_verbose(class))
Andrew Morton755cd902006-12-29 16:49:14 -08001831 printk("\nhash chain already cached, key: "
1832 "%016Lx tail class: [%p] %s\n",
1833 (unsigned long long)chain_key,
1834 class->key, class->name);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001835 return 0;
1836 }
1837 }
Ingo Molnar81fc6852006-12-13 00:34:40 -08001838 if (very_verbose(class))
Andrew Morton755cd902006-12-29 16:49:14 -08001839 printk("\nnew hash chain, key: %016Lx tail class: [%p] %s\n",
1840 (unsigned long long)chain_key, class->key, class->name);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001841 /*
1842 * Allocate a new chain entry from the static array, and add
1843 * it to the hash:
1844 */
Ingo Molnar74c383f2006-12-13 00:34:43 -08001845 if (!graph_lock())
1846 return 0;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001847 /*
1848 * We have to walk the chain again locked - to avoid duplicates:
1849 */
1850 list_for_each_entry(chain, hash_head, entry) {
1851 if (chain->chain_key == chain_key) {
Ingo Molnar74c383f2006-12-13 00:34:43 -08001852 graph_unlock();
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001853 goto cache_hit;
1854 }
1855 }
1856 if (unlikely(nr_lock_chains >= MAX_LOCKDEP_CHAINS)) {
Ingo Molnar74c383f2006-12-13 00:34:43 -08001857 if (!debug_locks_off_graph_unlock())
1858 return 0;
1859
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001860 printk("BUG: MAX_LOCKDEP_CHAINS too low!\n");
1861 printk("turning off the locking correctness validator.\n");
Peter Zijlstraeedeeab2009-03-18 12:38:47 +01001862 dump_stack();
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001863 return 0;
1864 }
1865 chain = lock_chains + nr_lock_chains++;
1866 chain->chain_key = chain_key;
Huang, Ying443cd502008-06-20 16:39:21 +08001867 chain->irq_context = hlock->irq_context;
1868 /* Find the first held_lock of current chain */
1869 hlock_next = hlock;
1870 for (i = curr->lockdep_depth - 1; i >= 0; i--) {
1871 hlock_curr = curr->held_locks + i;
1872 if (hlock_curr->irq_context != hlock_next->irq_context)
1873 break;
1874 hlock_next = hlock;
1875 }
1876 i++;
1877 chain->depth = curr->lockdep_depth + 1 - i;
Huang, Yingcd1a28e2008-06-23 11:20:54 +08001878 cn = nr_chain_hlocks;
1879 while (cn + chain->depth <= MAX_LOCKDEP_CHAIN_HLOCKS) {
1880 n = cmpxchg(&nr_chain_hlocks, cn, cn + chain->depth);
1881 if (n == cn)
1882 break;
1883 cn = n;
1884 }
1885 if (likely(cn + chain->depth <= MAX_LOCKDEP_CHAIN_HLOCKS)) {
1886 chain->base = cn;
Huang, Ying443cd502008-06-20 16:39:21 +08001887 for (j = 0; j < chain->depth - 1; j++, i++) {
Dave Jonesf82b2172008-08-11 09:30:23 +02001888 int lock_id = curr->held_locks[i].class_idx - 1;
Huang, Ying443cd502008-06-20 16:39:21 +08001889 chain_hlocks[chain->base + j] = lock_id;
1890 }
1891 chain_hlocks[chain->base + j] = class - lock_classes;
1892 }
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001893 list_add_tail_rcu(&chain->entry, hash_head);
1894 debug_atomic_inc(&chain_lookup_misses);
Peter Zijlstra8e182572007-07-19 01:48:54 -07001895 inc_chains();
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001896
1897 return 1;
1898}
Peter Zijlstra8e182572007-07-19 01:48:54 -07001899
1900static int validate_chain(struct task_struct *curr, struct lockdep_map *lock,
Johannes Berg4e6045f2007-10-18 23:39:55 -07001901 struct held_lock *hlock, int chain_head, u64 chain_key)
Peter Zijlstra8e182572007-07-19 01:48:54 -07001902{
1903 /*
1904 * Trylock needs to maintain the stack of held locks, but it
1905 * does not add new dependencies, because trylock can be done
1906 * in any order.
1907 *
1908 * We look up the chain_key and do the O(N^2) check and update of
1909 * the dependencies only if this is a new dependency chain.
1910 * (If lookup_chain_cache() returns with 1 it acquires
1911 * graph_lock for us)
1912 */
1913 if (!hlock->trylock && (hlock->check == 2) &&
Huang, Ying443cd502008-06-20 16:39:21 +08001914 lookup_chain_cache(curr, hlock, chain_key)) {
Peter Zijlstra8e182572007-07-19 01:48:54 -07001915 /*
1916 * Check whether last held lock:
1917 *
1918 * - is irq-safe, if this lock is irq-unsafe
1919 * - is softirq-safe, if this lock is hardirq-unsafe
1920 *
1921 * And check whether the new lock's dependency graph
1922 * could lead back to the previous lock.
1923 *
1924 * any of these scenarios could lead to a deadlock. If
1925 * All validations
1926 */
1927 int ret = check_deadlock(curr, hlock, lock, hlock->read);
1928
1929 if (!ret)
1930 return 0;
1931 /*
1932 * Mark recursive read, as we jump over it when
1933 * building dependencies (just like we jump over
1934 * trylock entries):
1935 */
1936 if (ret == 2)
1937 hlock->read = 2;
1938 /*
1939 * Add dependency only if this lock is not the head
1940 * of the chain, and if it's not a secondary read-lock:
1941 */
1942 if (!chain_head && ret != 2)
1943 if (!check_prevs_add(curr, hlock))
1944 return 0;
1945 graph_unlock();
1946 } else
1947 /* after lookup_chain_cache(): */
1948 if (unlikely(!debug_locks))
1949 return 0;
1950
1951 return 1;
1952}
1953#else
1954static inline int validate_chain(struct task_struct *curr,
1955 struct lockdep_map *lock, struct held_lock *hlock,
Gregory Haskins3aa416b2007-10-11 22:11:11 +02001956 int chain_head, u64 chain_key)
Peter Zijlstra8e182572007-07-19 01:48:54 -07001957{
1958 return 1;
1959}
Peter Zijlstraca58abc2007-07-19 01:48:53 -07001960#endif
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001961
1962/*
1963 * We are building curr_chain_key incrementally, so double-check
1964 * it from scratch, to make sure that it's done correctly:
1965 */
Steven Rostedt1d09daa2008-05-12 21:20:55 +02001966static void check_chain_key(struct task_struct *curr)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001967{
1968#ifdef CONFIG_DEBUG_LOCKDEP
1969 struct held_lock *hlock, *prev_hlock = NULL;
1970 unsigned int i, id;
1971 u64 chain_key = 0;
1972
1973 for (i = 0; i < curr->lockdep_depth; i++) {
1974 hlock = curr->held_locks + i;
1975 if (chain_key != hlock->prev_chain_key) {
1976 debug_locks_off();
Arjan van de Ven2df8b1d2008-07-30 12:43:11 -07001977 WARN(1, "hm#1, depth: %u [%u], %016Lx != %016Lx\n",
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001978 curr->lockdep_depth, i,
1979 (unsigned long long)chain_key,
1980 (unsigned long long)hlock->prev_chain_key);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001981 return;
1982 }
Dave Jonesf82b2172008-08-11 09:30:23 +02001983 id = hlock->class_idx - 1;
Jarek Poplawski381a2292007-02-10 01:44:58 -08001984 if (DEBUG_LOCKS_WARN_ON(id >= MAX_LOCKDEP_KEYS))
1985 return;
1986
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001987 if (prev_hlock && (prev_hlock->irq_context !=
1988 hlock->irq_context))
1989 chain_key = 0;
1990 chain_key = iterate_chain_key(chain_key, id);
1991 prev_hlock = hlock;
1992 }
1993 if (chain_key != curr->curr_chain_key) {
1994 debug_locks_off();
Arjan van de Ven2df8b1d2008-07-30 12:43:11 -07001995 WARN(1, "hm#2, depth: %u [%u], %016Lx != %016Lx\n",
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001996 curr->lockdep_depth, i,
1997 (unsigned long long)chain_key,
1998 (unsigned long long)curr->curr_chain_key);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001999 }
2000#endif
2001}
2002
Peter Zijlstra8e182572007-07-19 01:48:54 -07002003static int
2004print_usage_bug(struct task_struct *curr, struct held_lock *this,
2005 enum lock_usage_bit prev_bit, enum lock_usage_bit new_bit)
2006{
2007 if (!debug_locks_off_graph_unlock() || debug_locks_silent)
2008 return 0;
2009
2010 printk("\n=================================\n");
2011 printk( "[ INFO: inconsistent lock state ]\n");
2012 print_kernel_version();
2013 printk( "---------------------------------\n");
2014
2015 printk("inconsistent {%s} -> {%s} usage.\n",
2016 usage_str[prev_bit], usage_str[new_bit]);
2017
2018 printk("%s/%d [HC%u[%lu]:SC%u[%lu]:HE%u:SE%u] takes:\n",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07002019 curr->comm, task_pid_nr(curr),
Peter Zijlstra8e182572007-07-19 01:48:54 -07002020 trace_hardirq_context(curr), hardirq_count() >> HARDIRQ_SHIFT,
2021 trace_softirq_context(curr), softirq_count() >> SOFTIRQ_SHIFT,
2022 trace_hardirqs_enabled(curr),
2023 trace_softirqs_enabled(curr));
2024 print_lock(this);
2025
2026 printk("{%s} state was registered at:\n", usage_str[prev_bit]);
Dave Jonesf82b2172008-08-11 09:30:23 +02002027 print_stack_trace(hlock_class(this)->usage_traces + prev_bit, 1);
Peter Zijlstra8e182572007-07-19 01:48:54 -07002028
2029 print_irqtrace_events(curr);
2030 printk("\nother info that might help us debug this:\n");
2031 lockdep_print_held_locks(curr);
2032
2033 printk("\nstack backtrace:\n");
2034 dump_stack();
2035
2036 return 0;
2037}
2038
2039/*
2040 * Print out an error if an invalid bit is set:
2041 */
2042static inline int
2043valid_state(struct task_struct *curr, struct held_lock *this,
2044 enum lock_usage_bit new_bit, enum lock_usage_bit bad_bit)
2045{
Dave Jonesf82b2172008-08-11 09:30:23 +02002046 if (unlikely(hlock_class(this)->usage_mask & (1 << bad_bit)))
Peter Zijlstra8e182572007-07-19 01:48:54 -07002047 return print_usage_bug(curr, this, bad_bit, new_bit);
2048 return 1;
2049}
2050
2051static int mark_lock(struct task_struct *curr, struct held_lock *this,
2052 enum lock_usage_bit new_bit);
2053
Steven Rostedt81d68a92008-05-12 21:20:42 +02002054#if defined(CONFIG_TRACE_IRQFLAGS) && defined(CONFIG_PROVE_LOCKING)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002055
2056/*
2057 * print irq inversion bug:
2058 */
2059static int
Ming Lei24208ca2009-07-16 15:44:29 +02002060print_irq_inversion_bug(struct task_struct *curr,
2061 struct lock_list *root, struct lock_list *other,
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002062 struct held_lock *this, int forwards,
2063 const char *irqclass)
2064{
Ingo Molnar74c383f2006-12-13 00:34:43 -08002065 if (!debug_locks_off_graph_unlock() || debug_locks_silent)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002066 return 0;
2067
2068 printk("\n=========================================================\n");
2069 printk( "[ INFO: possible irq lock inversion dependency detected ]\n");
Dave Jones99de0552006-09-29 02:00:10 -07002070 print_kernel_version();
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002071 printk( "---------------------------------------------------------\n");
2072 printk("%s/%d just changed the state of lock:\n",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07002073 curr->comm, task_pid_nr(curr));
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002074 print_lock(this);
2075 if (forwards)
Peter Zijlstra26575e22009-03-04 14:53:24 +01002076 printk("but this lock took another, %s-unsafe lock in the past:\n", irqclass);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002077 else
Peter Zijlstra26575e22009-03-04 14:53:24 +01002078 printk("but this lock was taken by another, %s-safe lock in the past:\n", irqclass);
Ming Lei24208ca2009-07-16 15:44:29 +02002079 print_lock_name(other->class);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002080 printk("\n\nand interrupts could create inverse lock ordering between them.\n\n");
2081
2082 printk("\nother info that might help us debug this:\n");
2083 lockdep_print_held_locks(curr);
2084
Ming Lei24208ca2009-07-16 15:44:29 +02002085 printk("\nthe shortest dependencies between 2nd lock and 1st lock:\n");
2086 if (!save_trace(&root->trace))
2087 return 0;
2088 print_shortest_lock_dependencies(other, root);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002089
2090 printk("\nstack backtrace:\n");
2091 dump_stack();
2092
2093 return 0;
2094}
2095
2096/*
2097 * Prove that in the forwards-direction subgraph starting at <this>
2098 * there is no lock matching <mask>:
2099 */
2100static int
2101check_usage_forwards(struct task_struct *curr, struct held_lock *this,
2102 enum lock_usage_bit bit, const char *irqclass)
2103{
2104 int ret;
Ming Leid7aaba12009-07-16 15:44:29 +02002105 struct lock_list root;
2106 struct lock_list *uninitialized_var(target_entry);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002107
Ming Leid7aaba12009-07-16 15:44:29 +02002108 root.parent = NULL;
2109 root.class = hlock_class(this);
2110 ret = find_usage_forwards(&root, bit, &target_entry);
Peter Zijlstraaf012962009-07-16 15:44:29 +02002111 if (ret < 0)
2112 return print_bfs_bug(ret);
2113 if (ret == 1)
2114 return ret;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002115
Ming Lei24208ca2009-07-16 15:44:29 +02002116 return print_irq_inversion_bug(curr, &root, target_entry,
Ming Leid7aaba12009-07-16 15:44:29 +02002117 this, 1, irqclass);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002118}
2119
2120/*
2121 * Prove that in the backwards-direction subgraph starting at <this>
2122 * there is no lock matching <mask>:
2123 */
2124static int
2125check_usage_backwards(struct task_struct *curr, struct held_lock *this,
2126 enum lock_usage_bit bit, const char *irqclass)
2127{
2128 int ret;
Ming Leid7aaba12009-07-16 15:44:29 +02002129 struct lock_list root;
2130 struct lock_list *uninitialized_var(target_entry);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002131
Ming Leid7aaba12009-07-16 15:44:29 +02002132 root.parent = NULL;
2133 root.class = hlock_class(this);
2134 ret = find_usage_backwards(&root, bit, &target_entry);
Peter Zijlstraaf012962009-07-16 15:44:29 +02002135 if (ret < 0)
2136 return print_bfs_bug(ret);
2137 if (ret == 1)
2138 return ret;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002139
Ming Lei24208ca2009-07-16 15:44:29 +02002140 return print_irq_inversion_bug(curr, &root, target_entry,
Ming Leid7aaba12009-07-16 15:44:29 +02002141 this, 1, irqclass);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002142}
2143
Ingo Molnar3117df02006-12-13 00:34:43 -08002144void print_irqtrace_events(struct task_struct *curr)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002145{
2146 printk("irq event stamp: %u\n", curr->irq_events);
2147 printk("hardirqs last enabled at (%u): ", curr->hardirq_enable_event);
2148 print_ip_sym(curr->hardirq_enable_ip);
2149 printk("hardirqs last disabled at (%u): ", curr->hardirq_disable_event);
2150 print_ip_sym(curr->hardirq_disable_ip);
2151 printk("softirqs last enabled at (%u): ", curr->softirq_enable_event);
2152 print_ip_sym(curr->softirq_enable_ip);
2153 printk("softirqs last disabled at (%u): ", curr->softirq_disable_event);
2154 print_ip_sym(curr->softirq_disable_ip);
2155}
2156
Peter Zijlstracd953022009-01-22 16:38:21 +01002157static int HARDIRQ_verbose(struct lock_class *class)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002158{
Peter Zijlstra8e182572007-07-19 01:48:54 -07002159#if HARDIRQ_VERBOSE
2160 return class_filter(class);
2161#endif
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002162 return 0;
2163}
2164
Peter Zijlstracd953022009-01-22 16:38:21 +01002165static int SOFTIRQ_verbose(struct lock_class *class)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002166{
Peter Zijlstra8e182572007-07-19 01:48:54 -07002167#if SOFTIRQ_VERBOSE
2168 return class_filter(class);
2169#endif
2170 return 0;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002171}
2172
Peter Zijlstracd953022009-01-22 16:38:21 +01002173static int RECLAIM_FS_verbose(struct lock_class *class)
Nick Piggincf40bd12009-01-21 08:12:39 +01002174{
2175#if RECLAIM_VERBOSE
2176 return class_filter(class);
2177#endif
2178 return 0;
2179}
2180
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002181#define STRICT_READ_CHECKS 1
2182
Peter Zijlstracd953022009-01-22 16:38:21 +01002183static int (*state_verbose_f[])(struct lock_class *class) = {
2184#define LOCKDEP_STATE(__STATE) \
2185 __STATE##_verbose,
2186#include "lockdep_states.h"
2187#undef LOCKDEP_STATE
2188};
2189
2190static inline int state_verbose(enum lock_usage_bit bit,
2191 struct lock_class *class)
2192{
2193 return state_verbose_f[bit >> 2](class);
2194}
2195
Peter Zijlstra42c50d52009-01-22 16:58:16 +01002196typedef int (*check_usage_f)(struct task_struct *, struct held_lock *,
2197 enum lock_usage_bit bit, const char *name);
2198
Peter Zijlstra6a6904d2009-01-22 16:07:44 +01002199static int
Peter Zijlstra1c21f142009-03-04 13:51:13 +01002200mark_lock_irq(struct task_struct *curr, struct held_lock *this,
2201 enum lock_usage_bit new_bit)
Peter Zijlstra6a6904d2009-01-22 16:07:44 +01002202{
Peter Zijlstraf9892092009-01-22 16:09:59 +01002203 int excl_bit = exclusive_bit(new_bit);
Peter Zijlstra9d3651a2009-01-22 17:18:32 +01002204 int read = new_bit & 1;
Peter Zijlstra42c50d52009-01-22 16:58:16 +01002205 int dir = new_bit & 2;
2206
Peter Zijlstra38aa2712009-01-27 14:53:50 +01002207 /*
2208 * mark USED_IN has to look forwards -- to ensure no dependency
2209 * has ENABLED state, which would allow recursion deadlocks.
2210 *
2211 * mark ENABLED has to look backwards -- to ensure no dependee
2212 * has USED_IN state, which, again, would allow recursion deadlocks.
2213 */
Peter Zijlstra42c50d52009-01-22 16:58:16 +01002214 check_usage_f usage = dir ?
2215 check_usage_backwards : check_usage_forwards;
Peter Zijlstraf9892092009-01-22 16:09:59 +01002216
Peter Zijlstra38aa2712009-01-27 14:53:50 +01002217 /*
2218 * Validate that this particular lock does not have conflicting
2219 * usage states.
2220 */
Peter Zijlstra6a6904d2009-01-22 16:07:44 +01002221 if (!valid_state(curr, this, new_bit, excl_bit))
2222 return 0;
Peter Zijlstra9d3651a2009-01-22 17:18:32 +01002223
Peter Zijlstra38aa2712009-01-27 14:53:50 +01002224 /*
2225 * Validate that the lock dependencies don't have conflicting usage
2226 * states.
2227 */
2228 if ((!read || !dir || STRICT_READ_CHECKS) &&
Peter Zijlstra1c21f142009-03-04 13:51:13 +01002229 !usage(curr, this, excl_bit, state_name(new_bit & ~1)))
Peter Zijlstra6a6904d2009-01-22 16:07:44 +01002230 return 0;
Peter Zijlstra780e8202009-01-22 16:51:29 +01002231
Peter Zijlstra38aa2712009-01-27 14:53:50 +01002232 /*
2233 * Check for read in write conflicts
2234 */
2235 if (!read) {
2236 if (!valid_state(curr, this, new_bit, excl_bit + 1))
2237 return 0;
2238
2239 if (STRICT_READ_CHECKS &&
Peter Zijlstra4f367d8a2009-01-22 18:10:42 +01002240 !usage(curr, this, excl_bit + 1,
2241 state_name(new_bit + 1)))
Peter Zijlstra38aa2712009-01-27 14:53:50 +01002242 return 0;
2243 }
Peter Zijlstra780e8202009-01-22 16:51:29 +01002244
Peter Zijlstracd953022009-01-22 16:38:21 +01002245 if (state_verbose(new_bit, hlock_class(this)))
Peter Zijlstra6a6904d2009-01-22 16:07:44 +01002246 return 2;
2247
2248 return 1;
2249}
2250
Nick Piggincf40bd12009-01-21 08:12:39 +01002251enum mark_type {
Peter Zijlstra36bfb9b2009-01-22 14:12:41 +01002252#define LOCKDEP_STATE(__STATE) __STATE,
2253#include "lockdep_states.h"
2254#undef LOCKDEP_STATE
Nick Piggincf40bd12009-01-21 08:12:39 +01002255};
2256
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002257/*
2258 * Mark all held locks with a usage bit:
2259 */
Steven Rostedt1d09daa2008-05-12 21:20:55 +02002260static int
Nick Piggincf40bd12009-01-21 08:12:39 +01002261mark_held_locks(struct task_struct *curr, enum mark_type mark)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002262{
2263 enum lock_usage_bit usage_bit;
2264 struct held_lock *hlock;
2265 int i;
2266
2267 for (i = 0; i < curr->lockdep_depth; i++) {
2268 hlock = curr->held_locks + i;
2269
Peter Zijlstracf2ad4d2009-01-27 13:58:08 +01002270 usage_bit = 2 + (mark << 2); /* ENABLED */
2271 if (hlock->read)
2272 usage_bit += 1; /* READ */
2273
2274 BUG_ON(usage_bit >= LOCK_USAGE_STATES);
Nick Piggincf40bd12009-01-21 08:12:39 +01002275
Jarek Poplawski4ff773bb2007-05-08 00:31:00 -07002276 if (!mark_lock(curr, hlock, usage_bit))
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002277 return 0;
2278 }
2279
2280 return 1;
2281}
2282
2283/*
2284 * Debugging helper: via this flag we know that we are in
2285 * 'early bootup code', and will warn about any invalid irqs-on event:
2286 */
2287static int early_boot_irqs_enabled;
2288
2289void early_boot_irqs_off(void)
2290{
2291 early_boot_irqs_enabled = 0;
2292}
2293
2294void early_boot_irqs_on(void)
2295{
2296 early_boot_irqs_enabled = 1;
2297}
2298
2299/*
2300 * Hardirqs will be enabled:
2301 */
Heiko Carstens6afe40b2008-10-28 11:14:58 +01002302void trace_hardirqs_on_caller(unsigned long ip)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002303{
2304 struct task_struct *curr = current;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002305
Heiko Carstens6afe40b2008-10-28 11:14:58 +01002306 time_hardirqs_on(CALLER_ADDR0, ip);
Steven Rostedt81d68a92008-05-12 21:20:42 +02002307
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002308 if (unlikely(!debug_locks || current->lockdep_recursion))
2309 return;
2310
2311 if (DEBUG_LOCKS_WARN_ON(unlikely(!early_boot_irqs_enabled)))
2312 return;
2313
2314 if (unlikely(curr->hardirqs_enabled)) {
2315 debug_atomic_inc(&redundant_hardirqs_on);
2316 return;
2317 }
2318 /* we'll do an OFF -> ON transition: */
2319 curr->hardirqs_enabled = 1;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002320
2321 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2322 return;
2323 if (DEBUG_LOCKS_WARN_ON(current->hardirq_context))
2324 return;
2325 /*
2326 * We are going to turn hardirqs on, so set the
2327 * usage bit for all held locks:
2328 */
Nick Piggincf40bd12009-01-21 08:12:39 +01002329 if (!mark_held_locks(curr, HARDIRQ))
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002330 return;
2331 /*
2332 * If we have softirqs enabled, then set the usage
2333 * bit for all held locks. (disabled hardirqs prevented
2334 * this bit from being set before)
2335 */
2336 if (curr->softirqs_enabled)
Nick Piggincf40bd12009-01-21 08:12:39 +01002337 if (!mark_held_locks(curr, SOFTIRQ))
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002338 return;
2339
2340 curr->hardirq_enable_ip = ip;
2341 curr->hardirq_enable_event = ++curr->irq_events;
2342 debug_atomic_inc(&hardirqs_on_events);
2343}
Steven Rostedt81d68a92008-05-12 21:20:42 +02002344EXPORT_SYMBOL(trace_hardirqs_on_caller);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002345
Steven Rostedt1d09daa2008-05-12 21:20:55 +02002346void trace_hardirqs_on(void)
Steven Rostedt81d68a92008-05-12 21:20:42 +02002347{
2348 trace_hardirqs_on_caller(CALLER_ADDR0);
2349}
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002350EXPORT_SYMBOL(trace_hardirqs_on);
2351
2352/*
2353 * Hardirqs were disabled:
2354 */
Heiko Carstens6afe40b2008-10-28 11:14:58 +01002355void trace_hardirqs_off_caller(unsigned long ip)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002356{
2357 struct task_struct *curr = current;
2358
Heiko Carstens6afe40b2008-10-28 11:14:58 +01002359 time_hardirqs_off(CALLER_ADDR0, ip);
Steven Rostedt81d68a92008-05-12 21:20:42 +02002360
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002361 if (unlikely(!debug_locks || current->lockdep_recursion))
2362 return;
2363
2364 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2365 return;
2366
2367 if (curr->hardirqs_enabled) {
2368 /*
2369 * We have done an ON -> OFF transition:
2370 */
2371 curr->hardirqs_enabled = 0;
Heiko Carstens6afe40b2008-10-28 11:14:58 +01002372 curr->hardirq_disable_ip = ip;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002373 curr->hardirq_disable_event = ++curr->irq_events;
2374 debug_atomic_inc(&hardirqs_off_events);
2375 } else
2376 debug_atomic_inc(&redundant_hardirqs_off);
2377}
Steven Rostedt81d68a92008-05-12 21:20:42 +02002378EXPORT_SYMBOL(trace_hardirqs_off_caller);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002379
Steven Rostedt1d09daa2008-05-12 21:20:55 +02002380void trace_hardirqs_off(void)
Steven Rostedt81d68a92008-05-12 21:20:42 +02002381{
2382 trace_hardirqs_off_caller(CALLER_ADDR0);
2383}
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002384EXPORT_SYMBOL(trace_hardirqs_off);
2385
2386/*
2387 * Softirqs will be enabled:
2388 */
2389void trace_softirqs_on(unsigned long ip)
2390{
2391 struct task_struct *curr = current;
2392
2393 if (unlikely(!debug_locks))
2394 return;
2395
2396 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2397 return;
2398
2399 if (curr->softirqs_enabled) {
2400 debug_atomic_inc(&redundant_softirqs_on);
2401 return;
2402 }
2403
2404 /*
2405 * We'll do an OFF -> ON transition:
2406 */
2407 curr->softirqs_enabled = 1;
2408 curr->softirq_enable_ip = ip;
2409 curr->softirq_enable_event = ++curr->irq_events;
2410 debug_atomic_inc(&softirqs_on_events);
2411 /*
2412 * We are going to turn softirqs on, so set the
2413 * usage bit for all held locks, if hardirqs are
2414 * enabled too:
2415 */
2416 if (curr->hardirqs_enabled)
Nick Piggincf40bd12009-01-21 08:12:39 +01002417 mark_held_locks(curr, SOFTIRQ);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002418}
2419
2420/*
2421 * Softirqs were disabled:
2422 */
2423void trace_softirqs_off(unsigned long ip)
2424{
2425 struct task_struct *curr = current;
2426
2427 if (unlikely(!debug_locks))
2428 return;
2429
2430 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2431 return;
2432
2433 if (curr->softirqs_enabled) {
2434 /*
2435 * We have done an ON -> OFF transition:
2436 */
2437 curr->softirqs_enabled = 0;
2438 curr->softirq_disable_ip = ip;
2439 curr->softirq_disable_event = ++curr->irq_events;
2440 debug_atomic_inc(&softirqs_off_events);
2441 DEBUG_LOCKS_WARN_ON(!softirq_count());
2442 } else
2443 debug_atomic_inc(&redundant_softirqs_off);
2444}
2445
Peter Zijlstra2f850182009-03-20 11:13:20 +01002446static void __lockdep_trace_alloc(gfp_t gfp_mask, unsigned long flags)
Nick Piggincf40bd12009-01-21 08:12:39 +01002447{
2448 struct task_struct *curr = current;
2449
2450 if (unlikely(!debug_locks))
2451 return;
2452
2453 /* no reclaim without waiting on it */
2454 if (!(gfp_mask & __GFP_WAIT))
2455 return;
2456
2457 /* this guy won't enter reclaim */
2458 if ((curr->flags & PF_MEMALLOC) && !(gfp_mask & __GFP_NOMEMALLOC))
2459 return;
2460
2461 /* We're only interested __GFP_FS allocations for now */
2462 if (!(gfp_mask & __GFP_FS))
2463 return;
2464
Peter Zijlstra2f850182009-03-20 11:13:20 +01002465 if (DEBUG_LOCKS_WARN_ON(irqs_disabled_flags(flags)))
Nick Piggincf40bd12009-01-21 08:12:39 +01002466 return;
2467
2468 mark_held_locks(curr, RECLAIM_FS);
2469}
2470
Peter Zijlstra2f850182009-03-20 11:13:20 +01002471static void check_flags(unsigned long flags);
2472
2473void lockdep_trace_alloc(gfp_t gfp_mask)
2474{
2475 unsigned long flags;
2476
2477 if (unlikely(current->lockdep_recursion))
2478 return;
2479
2480 raw_local_irq_save(flags);
2481 check_flags(flags);
2482 current->lockdep_recursion = 1;
2483 __lockdep_trace_alloc(gfp_mask, flags);
2484 current->lockdep_recursion = 0;
2485 raw_local_irq_restore(flags);
2486}
2487
Peter Zijlstra8e182572007-07-19 01:48:54 -07002488static int mark_irqflags(struct task_struct *curr, struct held_lock *hlock)
2489{
2490 /*
2491 * If non-trylock use in a hardirq or softirq context, then
2492 * mark the lock as used in these contexts:
2493 */
2494 if (!hlock->trylock) {
2495 if (hlock->read) {
2496 if (curr->hardirq_context)
2497 if (!mark_lock(curr, hlock,
2498 LOCK_USED_IN_HARDIRQ_READ))
2499 return 0;
2500 if (curr->softirq_context)
2501 if (!mark_lock(curr, hlock,
2502 LOCK_USED_IN_SOFTIRQ_READ))
2503 return 0;
2504 } else {
2505 if (curr->hardirq_context)
2506 if (!mark_lock(curr, hlock, LOCK_USED_IN_HARDIRQ))
2507 return 0;
2508 if (curr->softirq_context)
2509 if (!mark_lock(curr, hlock, LOCK_USED_IN_SOFTIRQ))
2510 return 0;
2511 }
2512 }
2513 if (!hlock->hardirqs_off) {
2514 if (hlock->read) {
2515 if (!mark_lock(curr, hlock,
Peter Zijlstra4fc95e82009-01-22 13:10:52 +01002516 LOCK_ENABLED_HARDIRQ_READ))
Peter Zijlstra8e182572007-07-19 01:48:54 -07002517 return 0;
2518 if (curr->softirqs_enabled)
2519 if (!mark_lock(curr, hlock,
Peter Zijlstra4fc95e82009-01-22 13:10:52 +01002520 LOCK_ENABLED_SOFTIRQ_READ))
Peter Zijlstra8e182572007-07-19 01:48:54 -07002521 return 0;
2522 } else {
2523 if (!mark_lock(curr, hlock,
Peter Zijlstra4fc95e82009-01-22 13:10:52 +01002524 LOCK_ENABLED_HARDIRQ))
Peter Zijlstra8e182572007-07-19 01:48:54 -07002525 return 0;
2526 if (curr->softirqs_enabled)
2527 if (!mark_lock(curr, hlock,
Peter Zijlstra4fc95e82009-01-22 13:10:52 +01002528 LOCK_ENABLED_SOFTIRQ))
Peter Zijlstra8e182572007-07-19 01:48:54 -07002529 return 0;
2530 }
2531 }
2532
Nick Piggincf40bd12009-01-21 08:12:39 +01002533 /*
2534 * We reuse the irq context infrastructure more broadly as a general
2535 * context checking code. This tests GFP_FS recursion (a lock taken
2536 * during reclaim for a GFP_FS allocation is held over a GFP_FS
2537 * allocation).
2538 */
2539 if (!hlock->trylock && (curr->lockdep_reclaim_gfp & __GFP_FS)) {
2540 if (hlock->read) {
2541 if (!mark_lock(curr, hlock, LOCK_USED_IN_RECLAIM_FS_READ))
2542 return 0;
2543 } else {
2544 if (!mark_lock(curr, hlock, LOCK_USED_IN_RECLAIM_FS))
2545 return 0;
2546 }
2547 }
2548
Peter Zijlstra8e182572007-07-19 01:48:54 -07002549 return 1;
2550}
2551
2552static int separate_irq_context(struct task_struct *curr,
2553 struct held_lock *hlock)
2554{
2555 unsigned int depth = curr->lockdep_depth;
2556
2557 /*
2558 * Keep track of points where we cross into an interrupt context:
2559 */
2560 hlock->irq_context = 2*(curr->hardirq_context ? 1 : 0) +
2561 curr->softirq_context;
2562 if (depth) {
2563 struct held_lock *prev_hlock;
2564
2565 prev_hlock = curr->held_locks + depth-1;
2566 /*
2567 * If we cross into another context, reset the
2568 * hash key (this also prevents the checking and the
2569 * adding of the dependency to 'prev'):
2570 */
2571 if (prev_hlock->irq_context != hlock->irq_context)
2572 return 1;
2573 }
2574 return 0;
2575}
2576
2577#else
2578
2579static inline
2580int mark_lock_irq(struct task_struct *curr, struct held_lock *this,
2581 enum lock_usage_bit new_bit)
2582{
2583 WARN_ON(1);
2584 return 1;
2585}
2586
2587static inline int mark_irqflags(struct task_struct *curr,
2588 struct held_lock *hlock)
2589{
2590 return 1;
2591}
2592
2593static inline int separate_irq_context(struct task_struct *curr,
2594 struct held_lock *hlock)
2595{
2596 return 0;
2597}
2598
Peter Zijlstra868a23a2009-02-15 00:25:21 +01002599void lockdep_trace_alloc(gfp_t gfp_mask)
2600{
2601}
2602
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002603#endif
2604
2605/*
Peter Zijlstra8e182572007-07-19 01:48:54 -07002606 * Mark a lock with a usage bit, and validate the state transition:
2607 */
Steven Rostedt1d09daa2008-05-12 21:20:55 +02002608static int mark_lock(struct task_struct *curr, struct held_lock *this,
Steven Rostedt0764d232008-05-12 21:20:44 +02002609 enum lock_usage_bit new_bit)
Peter Zijlstra8e182572007-07-19 01:48:54 -07002610{
2611 unsigned int new_mask = 1 << new_bit, ret = 1;
2612
2613 /*
2614 * If already set then do not dirty the cacheline,
2615 * nor do any checks:
2616 */
Dave Jonesf82b2172008-08-11 09:30:23 +02002617 if (likely(hlock_class(this)->usage_mask & new_mask))
Peter Zijlstra8e182572007-07-19 01:48:54 -07002618 return 1;
2619
2620 if (!graph_lock())
2621 return 0;
2622 /*
2623 * Make sure we didnt race:
2624 */
Dave Jonesf82b2172008-08-11 09:30:23 +02002625 if (unlikely(hlock_class(this)->usage_mask & new_mask)) {
Peter Zijlstra8e182572007-07-19 01:48:54 -07002626 graph_unlock();
2627 return 1;
2628 }
2629
Dave Jonesf82b2172008-08-11 09:30:23 +02002630 hlock_class(this)->usage_mask |= new_mask;
Peter Zijlstra8e182572007-07-19 01:48:54 -07002631
Dave Jonesf82b2172008-08-11 09:30:23 +02002632 if (!save_trace(hlock_class(this)->usage_traces + new_bit))
Peter Zijlstra8e182572007-07-19 01:48:54 -07002633 return 0;
2634
2635 switch (new_bit) {
Peter Zijlstra53464172009-01-22 14:15:53 +01002636#define LOCKDEP_STATE(__STATE) \
2637 case LOCK_USED_IN_##__STATE: \
2638 case LOCK_USED_IN_##__STATE##_READ: \
2639 case LOCK_ENABLED_##__STATE: \
2640 case LOCK_ENABLED_##__STATE##_READ:
2641#include "lockdep_states.h"
2642#undef LOCKDEP_STATE
Peter Zijlstra8e182572007-07-19 01:48:54 -07002643 ret = mark_lock_irq(curr, this, new_bit);
2644 if (!ret)
2645 return 0;
2646 break;
2647 case LOCK_USED:
Peter Zijlstra8e182572007-07-19 01:48:54 -07002648 debug_atomic_dec(&nr_unused_locks);
2649 break;
2650 default:
2651 if (!debug_locks_off_graph_unlock())
2652 return 0;
2653 WARN_ON(1);
2654 return 0;
2655 }
2656
2657 graph_unlock();
2658
2659 /*
2660 * We must printk outside of the graph_lock:
2661 */
2662 if (ret == 2) {
2663 printk("\nmarked lock as {%s}:\n", usage_str[new_bit]);
2664 print_lock(this);
2665 print_irqtrace_events(curr);
2666 dump_stack();
2667 }
2668
2669 return ret;
2670}
2671
2672/*
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002673 * Initialize a lock instance's lock-class mapping info:
2674 */
2675void lockdep_init_map(struct lockdep_map *lock, const char *name,
Peter Zijlstra4dfbb9d2006-10-11 01:45:14 -04002676 struct lock_class_key *key, int subclass)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002677{
Peter Zijlstrac8a25002009-04-17 09:40:49 +02002678 lock->class_cache = NULL;
2679#ifdef CONFIG_LOCK_STAT
2680 lock->cpu = raw_smp_processor_id();
2681#endif
2682
2683 if (DEBUG_LOCKS_WARN_ON(!name)) {
2684 lock->name = "NULL";
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002685 return;
Peter Zijlstrac8a25002009-04-17 09:40:49 +02002686 }
2687
2688 lock->name = name;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002689
2690 if (DEBUG_LOCKS_WARN_ON(!key))
2691 return;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002692 /*
2693 * Sanity check, the lock-class key must be persistent:
2694 */
2695 if (!static_obj(key)) {
2696 printk("BUG: key %p not in .data!\n", key);
2697 DEBUG_LOCKS_WARN_ON(1);
2698 return;
2699 }
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002700 lock->key = key;
Peter Zijlstrac8a25002009-04-17 09:40:49 +02002701
2702 if (unlikely(!debug_locks))
2703 return;
2704
Peter Zijlstra4dfbb9d2006-10-11 01:45:14 -04002705 if (subclass)
2706 register_lock_class(lock, subclass, 1);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002707}
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002708EXPORT_SYMBOL_GPL(lockdep_init_map);
2709
2710/*
2711 * This gets called for every mutex_lock*()/spin_lock*() operation.
2712 * We maintain the dependency maps and validate the locking attempt:
2713 */
2714static int __lock_acquire(struct lockdep_map *lock, unsigned int subclass,
2715 int trylock, int read, int check, int hardirqs_off,
Peter Zijlstrabb97a912009-07-20 19:15:35 +02002716 struct lockdep_map *nest_lock, unsigned long ip,
2717 int references)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002718{
2719 struct task_struct *curr = current;
Ingo Molnard6d897c2006-07-10 04:44:04 -07002720 struct lock_class *class = NULL;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002721 struct held_lock *hlock;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002722 unsigned int depth, id;
2723 int chain_head = 0;
Peter Zijlstrabb97a912009-07-20 19:15:35 +02002724 int class_idx;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002725 u64 chain_key;
2726
Peter Zijlstraf20786f2007-07-19 01:48:56 -07002727 if (!prove_locking)
2728 check = 1;
2729
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002730 if (unlikely(!debug_locks))
2731 return 0;
2732
2733 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2734 return 0;
2735
2736 if (unlikely(subclass >= MAX_LOCKDEP_SUBCLASSES)) {
2737 debug_locks_off();
2738 printk("BUG: MAX_LOCKDEP_SUBCLASSES too low!\n");
2739 printk("turning off the locking correctness validator.\n");
Peter Zijlstraeedeeab2009-03-18 12:38:47 +01002740 dump_stack();
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002741 return 0;
2742 }
2743
Ingo Molnard6d897c2006-07-10 04:44:04 -07002744 if (!subclass)
2745 class = lock->class_cache;
2746 /*
2747 * Not cached yet or subclass?
2748 */
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002749 if (unlikely(!class)) {
Peter Zijlstra4dfbb9d2006-10-11 01:45:14 -04002750 class = register_lock_class(lock, subclass, 0);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002751 if (!class)
2752 return 0;
2753 }
2754 debug_atomic_inc((atomic_t *)&class->ops);
2755 if (very_verbose(class)) {
2756 printk("\nacquire class [%p] %s", class->key, class->name);
2757 if (class->name_version > 1)
2758 printk("#%d", class->name_version);
2759 printk("\n");
2760 dump_stack();
2761 }
2762
2763 /*
2764 * Add the lock to the list of currently held locks.
2765 * (we dont increase the depth just yet, up until the
2766 * dependency checks are done)
2767 */
2768 depth = curr->lockdep_depth;
2769 if (DEBUG_LOCKS_WARN_ON(depth >= MAX_LOCK_DEPTH))
2770 return 0;
2771
Peter Zijlstrabb97a912009-07-20 19:15:35 +02002772 class_idx = class - lock_classes + 1;
2773
2774 if (depth) {
2775 hlock = curr->held_locks + depth - 1;
2776 if (hlock->class_idx == class_idx && nest_lock) {
2777 if (hlock->references)
2778 hlock->references++;
2779 else
2780 hlock->references = 2;
2781
2782 return 1;
2783 }
2784 }
2785
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002786 hlock = curr->held_locks + depth;
Dave Jonesf82b2172008-08-11 09:30:23 +02002787 if (DEBUG_LOCKS_WARN_ON(!class))
2788 return 0;
Peter Zijlstrabb97a912009-07-20 19:15:35 +02002789 hlock->class_idx = class_idx;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002790 hlock->acquire_ip = ip;
2791 hlock->instance = lock;
Peter Zijlstra7531e2f2008-08-11 09:30:24 +02002792 hlock->nest_lock = nest_lock;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002793 hlock->trylock = trylock;
2794 hlock->read = read;
2795 hlock->check = check;
Dmitry Baryshkov6951b122008-08-18 04:26:37 +04002796 hlock->hardirqs_off = !!hardirqs_off;
Peter Zijlstrabb97a912009-07-20 19:15:35 +02002797 hlock->references = references;
Peter Zijlstraf20786f2007-07-19 01:48:56 -07002798#ifdef CONFIG_LOCK_STAT
2799 hlock->waittime_stamp = 0;
Peter Zijlstra3365e7792009-10-09 10:12:41 +02002800 hlock->holdtime_stamp = lockstat_clock();
Peter Zijlstraf20786f2007-07-19 01:48:56 -07002801#endif
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002802
Peter Zijlstra8e182572007-07-19 01:48:54 -07002803 if (check == 2 && !mark_irqflags(curr, hlock))
2804 return 0;
2805
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002806 /* mark it as used: */
Jarek Poplawski4ff773bb2007-05-08 00:31:00 -07002807 if (!mark_lock(curr, hlock, LOCK_USED))
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002808 return 0;
Peter Zijlstra8e182572007-07-19 01:48:54 -07002809
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002810 /*
Gautham R Shenoy17aacfb2007-10-28 20:47:01 +01002811 * Calculate the chain hash: it's the combined hash of all the
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002812 * lock keys along the dependency chain. We save the hash value
2813 * at every step so that we can get the current hash easily
2814 * after unlock. The chain hash is then used to cache dependency
2815 * results.
2816 *
2817 * The 'key ID' is what is the most compact key value to drive
2818 * the hash, not class->key.
2819 */
2820 id = class - lock_classes;
2821 if (DEBUG_LOCKS_WARN_ON(id >= MAX_LOCKDEP_KEYS))
2822 return 0;
2823
2824 chain_key = curr->curr_chain_key;
2825 if (!depth) {
2826 if (DEBUG_LOCKS_WARN_ON(chain_key != 0))
2827 return 0;
2828 chain_head = 1;
2829 }
2830
2831 hlock->prev_chain_key = chain_key;
Peter Zijlstra8e182572007-07-19 01:48:54 -07002832 if (separate_irq_context(curr, hlock)) {
2833 chain_key = 0;
2834 chain_head = 1;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002835 }
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002836 chain_key = iterate_chain_key(chain_key, id);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002837
Gregory Haskins3aa416b2007-10-11 22:11:11 +02002838 if (!validate_chain(curr, lock, hlock, chain_head, chain_key))
Peter Zijlstra8e182572007-07-19 01:48:54 -07002839 return 0;
Jarek Poplawski381a2292007-02-10 01:44:58 -08002840
Gregory Haskins3aa416b2007-10-11 22:11:11 +02002841 curr->curr_chain_key = chain_key;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002842 curr->lockdep_depth++;
2843 check_chain_key(curr);
Jarek Poplawski60e114d2007-02-20 13:58:00 -08002844#ifdef CONFIG_DEBUG_LOCKDEP
2845 if (unlikely(!debug_locks))
2846 return 0;
2847#endif
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002848 if (unlikely(curr->lockdep_depth >= MAX_LOCK_DEPTH)) {
2849 debug_locks_off();
2850 printk("BUG: MAX_LOCK_DEPTH too low!\n");
2851 printk("turning off the locking correctness validator.\n");
Peter Zijlstraeedeeab2009-03-18 12:38:47 +01002852 dump_stack();
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002853 return 0;
2854 }
Jarek Poplawski381a2292007-02-10 01:44:58 -08002855
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002856 if (unlikely(curr->lockdep_depth > max_lockdep_depth))
2857 max_lockdep_depth = curr->lockdep_depth;
2858
2859 return 1;
2860}
2861
2862static int
2863print_unlock_inbalance_bug(struct task_struct *curr, struct lockdep_map *lock,
2864 unsigned long ip)
2865{
2866 if (!debug_locks_off())
2867 return 0;
2868 if (debug_locks_silent)
2869 return 0;
2870
2871 printk("\n=====================================\n");
2872 printk( "[ BUG: bad unlock balance detected! ]\n");
2873 printk( "-------------------------------------\n");
2874 printk("%s/%d is trying to release lock (",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07002875 curr->comm, task_pid_nr(curr));
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002876 print_lockdep_cache(lock);
2877 printk(") at:\n");
2878 print_ip_sym(ip);
2879 printk("but there are no more locks to release!\n");
2880 printk("\nother info that might help us debug this:\n");
2881 lockdep_print_held_locks(curr);
2882
2883 printk("\nstack backtrace:\n");
2884 dump_stack();
2885
2886 return 0;
2887}
2888
2889/*
2890 * Common debugging checks for both nested and non-nested unlock:
2891 */
2892static int check_unlock(struct task_struct *curr, struct lockdep_map *lock,
2893 unsigned long ip)
2894{
2895 if (unlikely(!debug_locks))
2896 return 0;
2897 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2898 return 0;
2899
2900 if (curr->lockdep_depth <= 0)
2901 return print_unlock_inbalance_bug(curr, lock, ip);
2902
2903 return 1;
2904}
2905
Peter Zijlstrabb97a912009-07-20 19:15:35 +02002906static int match_held_lock(struct held_lock *hlock, struct lockdep_map *lock)
2907{
2908 if (hlock->instance == lock)
2909 return 1;
2910
2911 if (hlock->references) {
2912 struct lock_class *class = lock->class_cache;
2913
2914 if (!class)
2915 class = look_up_lock_class(lock, 0);
2916
2917 if (DEBUG_LOCKS_WARN_ON(!class))
2918 return 0;
2919
2920 if (DEBUG_LOCKS_WARN_ON(!hlock->nest_lock))
2921 return 0;
2922
2923 if (hlock->class_idx == class - lock_classes + 1)
2924 return 1;
2925 }
2926
2927 return 0;
2928}
2929
Peter Zijlstra64aa3482008-08-11 09:30:21 +02002930static int
Peter Zijlstra00ef9f72008-12-04 09:00:17 +01002931__lock_set_class(struct lockdep_map *lock, const char *name,
2932 struct lock_class_key *key, unsigned int subclass,
2933 unsigned long ip)
Peter Zijlstra64aa3482008-08-11 09:30:21 +02002934{
2935 struct task_struct *curr = current;
2936 struct held_lock *hlock, *prev_hlock;
2937 struct lock_class *class;
2938 unsigned int depth;
2939 int i;
2940
2941 depth = curr->lockdep_depth;
2942 if (DEBUG_LOCKS_WARN_ON(!depth))
2943 return 0;
2944
2945 prev_hlock = NULL;
2946 for (i = depth-1; i >= 0; i--) {
2947 hlock = curr->held_locks + i;
2948 /*
2949 * We must not cross into another context:
2950 */
2951 if (prev_hlock && prev_hlock->irq_context != hlock->irq_context)
2952 break;
Peter Zijlstrabb97a912009-07-20 19:15:35 +02002953 if (match_held_lock(hlock, lock))
Peter Zijlstra64aa3482008-08-11 09:30:21 +02002954 goto found_it;
2955 prev_hlock = hlock;
2956 }
2957 return print_unlock_inbalance_bug(curr, lock, ip);
2958
2959found_it:
Peter Zijlstra00ef9f72008-12-04 09:00:17 +01002960 lockdep_init_map(lock, name, key, 0);
Peter Zijlstra64aa3482008-08-11 09:30:21 +02002961 class = register_lock_class(lock, subclass, 0);
Dave Jonesf82b2172008-08-11 09:30:23 +02002962 hlock->class_idx = class - lock_classes + 1;
Peter Zijlstra64aa3482008-08-11 09:30:21 +02002963
2964 curr->lockdep_depth = i;
2965 curr->curr_chain_key = hlock->prev_chain_key;
2966
2967 for (; i < depth; i++) {
2968 hlock = curr->held_locks + i;
2969 if (!__lock_acquire(hlock->instance,
Dave Jonesf82b2172008-08-11 09:30:23 +02002970 hlock_class(hlock)->subclass, hlock->trylock,
Peter Zijlstra64aa3482008-08-11 09:30:21 +02002971 hlock->read, hlock->check, hlock->hardirqs_off,
Peter Zijlstrabb97a912009-07-20 19:15:35 +02002972 hlock->nest_lock, hlock->acquire_ip,
2973 hlock->references))
Peter Zijlstra64aa3482008-08-11 09:30:21 +02002974 return 0;
2975 }
2976
2977 if (DEBUG_LOCKS_WARN_ON(curr->lockdep_depth != depth))
2978 return 0;
2979 return 1;
2980}
2981
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002982/*
2983 * Remove the lock to the list of currently held locks in a
2984 * potentially non-nested (out of order) manner. This is a
2985 * relatively rare operation, as all the unlock APIs default
2986 * to nested mode (which uses lock_release()):
2987 */
2988static int
2989lock_release_non_nested(struct task_struct *curr,
2990 struct lockdep_map *lock, unsigned long ip)
2991{
2992 struct held_lock *hlock, *prev_hlock;
2993 unsigned int depth;
2994 int i;
2995
2996 /*
2997 * Check whether the lock exists in the current stack
2998 * of held locks:
2999 */
3000 depth = curr->lockdep_depth;
3001 if (DEBUG_LOCKS_WARN_ON(!depth))
3002 return 0;
3003
3004 prev_hlock = NULL;
3005 for (i = depth-1; i >= 0; i--) {
3006 hlock = curr->held_locks + i;
3007 /*
3008 * We must not cross into another context:
3009 */
3010 if (prev_hlock && prev_hlock->irq_context != hlock->irq_context)
3011 break;
Peter Zijlstrabb97a912009-07-20 19:15:35 +02003012 if (match_held_lock(hlock, lock))
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003013 goto found_it;
3014 prev_hlock = hlock;
3015 }
3016 return print_unlock_inbalance_bug(curr, lock, ip);
3017
3018found_it:
Peter Zijlstrabb97a912009-07-20 19:15:35 +02003019 if (hlock->instance == lock)
3020 lock_release_holdtime(hlock);
3021
3022 if (hlock->references) {
3023 hlock->references--;
3024 if (hlock->references) {
3025 /*
3026 * We had, and after removing one, still have
3027 * references, the current lock stack is still
3028 * valid. We're done!
3029 */
3030 return 1;
3031 }
3032 }
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003033
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003034 /*
3035 * We have the right lock to unlock, 'hlock' points to it.
3036 * Now we remove it from the stack, and add back the other
3037 * entries (if any), recalculating the hash along the way:
3038 */
Peter Zijlstrabb97a912009-07-20 19:15:35 +02003039
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003040 curr->lockdep_depth = i;
3041 curr->curr_chain_key = hlock->prev_chain_key;
3042
3043 for (i++; i < depth; i++) {
3044 hlock = curr->held_locks + i;
3045 if (!__lock_acquire(hlock->instance,
Dave Jonesf82b2172008-08-11 09:30:23 +02003046 hlock_class(hlock)->subclass, hlock->trylock,
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003047 hlock->read, hlock->check, hlock->hardirqs_off,
Peter Zijlstrabb97a912009-07-20 19:15:35 +02003048 hlock->nest_lock, hlock->acquire_ip,
3049 hlock->references))
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003050 return 0;
3051 }
3052
3053 if (DEBUG_LOCKS_WARN_ON(curr->lockdep_depth != depth - 1))
3054 return 0;
3055 return 1;
3056}
3057
3058/*
3059 * Remove the lock to the list of currently held locks - this gets
3060 * called on mutex_unlock()/spin_unlock*() (or on a failed
3061 * mutex_lock_interruptible()). This is done for unlocks that nest
3062 * perfectly. (i.e. the current top of the lock-stack is unlocked)
3063 */
3064static int lock_release_nested(struct task_struct *curr,
3065 struct lockdep_map *lock, unsigned long ip)
3066{
3067 struct held_lock *hlock;
3068 unsigned int depth;
3069
3070 /*
3071 * Pop off the top of the lock stack:
3072 */
3073 depth = curr->lockdep_depth - 1;
3074 hlock = curr->held_locks + depth;
3075
3076 /*
3077 * Is the unlock non-nested:
3078 */
Peter Zijlstrabb97a912009-07-20 19:15:35 +02003079 if (hlock->instance != lock || hlock->references)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003080 return lock_release_non_nested(curr, lock, ip);
3081 curr->lockdep_depth--;
3082
3083 if (DEBUG_LOCKS_WARN_ON(!depth && (hlock->prev_chain_key != 0)))
3084 return 0;
3085
3086 curr->curr_chain_key = hlock->prev_chain_key;
3087
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003088 lock_release_holdtime(hlock);
3089
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003090#ifdef CONFIG_DEBUG_LOCKDEP
3091 hlock->prev_chain_key = 0;
Dave Jonesf82b2172008-08-11 09:30:23 +02003092 hlock->class_idx = 0;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003093 hlock->acquire_ip = 0;
3094 hlock->irq_context = 0;
3095#endif
3096 return 1;
3097}
3098
3099/*
3100 * Remove the lock to the list of currently held locks - this gets
3101 * called on mutex_unlock()/spin_unlock*() (or on a failed
3102 * mutex_lock_interruptible()). This is done for unlocks that nest
3103 * perfectly. (i.e. the current top of the lock-stack is unlocked)
3104 */
3105static void
3106__lock_release(struct lockdep_map *lock, int nested, unsigned long ip)
3107{
3108 struct task_struct *curr = current;
3109
3110 if (!check_unlock(curr, lock, ip))
3111 return;
3112
3113 if (nested) {
3114 if (!lock_release_nested(curr, lock, ip))
3115 return;
3116 } else {
3117 if (!lock_release_non_nested(curr, lock, ip))
3118 return;
3119 }
3120
3121 check_chain_key(curr);
3122}
3123
Peter Zijlstraf607c662009-07-20 19:16:29 +02003124static int __lock_is_held(struct lockdep_map *lock)
3125{
3126 struct task_struct *curr = current;
3127 int i;
3128
3129 for (i = 0; i < curr->lockdep_depth; i++) {
Peter Zijlstrabb97a912009-07-20 19:15:35 +02003130 struct held_lock *hlock = curr->held_locks + i;
3131
3132 if (match_held_lock(hlock, lock))
Peter Zijlstraf607c662009-07-20 19:16:29 +02003133 return 1;
3134 }
3135
3136 return 0;
3137}
3138
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003139/*
3140 * Check whether we follow the irq-flags state precisely:
3141 */
Steven Rostedt1d09daa2008-05-12 21:20:55 +02003142static void check_flags(unsigned long flags)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003143{
Ingo Molnar992860e2008-07-14 10:28:38 +02003144#if defined(CONFIG_PROVE_LOCKING) && defined(CONFIG_DEBUG_LOCKDEP) && \
3145 defined(CONFIG_TRACE_IRQFLAGS)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003146 if (!debug_locks)
3147 return;
3148
Ingo Molnar5f9fa8a2007-12-07 19:02:47 +01003149 if (irqs_disabled_flags(flags)) {
3150 if (DEBUG_LOCKS_WARN_ON(current->hardirqs_enabled)) {
3151 printk("possible reason: unannotated irqs-off.\n");
3152 }
3153 } else {
3154 if (DEBUG_LOCKS_WARN_ON(!current->hardirqs_enabled)) {
3155 printk("possible reason: unannotated irqs-on.\n");
3156 }
3157 }
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003158
3159 /*
3160 * We dont accurately track softirq state in e.g.
3161 * hardirq contexts (such as on 4KSTACKS), so only
3162 * check if not in hardirq contexts:
3163 */
3164 if (!hardirq_count()) {
3165 if (softirq_count())
3166 DEBUG_LOCKS_WARN_ON(current->softirqs_enabled);
3167 else
3168 DEBUG_LOCKS_WARN_ON(!current->softirqs_enabled);
3169 }
3170
3171 if (!debug_locks)
3172 print_irqtrace_events(current);
3173#endif
3174}
3175
Peter Zijlstra00ef9f72008-12-04 09:00:17 +01003176void lock_set_class(struct lockdep_map *lock, const char *name,
3177 struct lock_class_key *key, unsigned int subclass,
3178 unsigned long ip)
Peter Zijlstra64aa3482008-08-11 09:30:21 +02003179{
3180 unsigned long flags;
3181
3182 if (unlikely(current->lockdep_recursion))
3183 return;
3184
3185 raw_local_irq_save(flags);
3186 current->lockdep_recursion = 1;
3187 check_flags(flags);
Peter Zijlstra00ef9f72008-12-04 09:00:17 +01003188 if (__lock_set_class(lock, name, key, subclass, ip))
Peter Zijlstra64aa3482008-08-11 09:30:21 +02003189 check_chain_key(current);
3190 current->lockdep_recursion = 0;
3191 raw_local_irq_restore(flags);
3192}
Peter Zijlstra00ef9f72008-12-04 09:00:17 +01003193EXPORT_SYMBOL_GPL(lock_set_class);
Peter Zijlstra64aa3482008-08-11 09:30:21 +02003194
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003195/*
3196 * We are not always called with irqs disabled - do that here,
3197 * and also avoid lockdep recursion:
3198 */
Steven Rostedt1d09daa2008-05-12 21:20:55 +02003199void lock_acquire(struct lockdep_map *lock, unsigned int subclass,
Peter Zijlstra7531e2f2008-08-11 09:30:24 +02003200 int trylock, int read, int check,
3201 struct lockdep_map *nest_lock, unsigned long ip)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003202{
3203 unsigned long flags;
3204
Peter Zijlstraefed7922009-03-04 12:32:55 +01003205 trace_lock_acquire(lock, subclass, trylock, read, check, nest_lock, ip);
3206
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003207 if (unlikely(current->lockdep_recursion))
3208 return;
3209
3210 raw_local_irq_save(flags);
3211 check_flags(flags);
3212
3213 current->lockdep_recursion = 1;
3214 __lock_acquire(lock, subclass, trylock, read, check,
Peter Zijlstrabb97a912009-07-20 19:15:35 +02003215 irqs_disabled_flags(flags), nest_lock, ip, 0);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003216 current->lockdep_recursion = 0;
3217 raw_local_irq_restore(flags);
3218}
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003219EXPORT_SYMBOL_GPL(lock_acquire);
3220
Steven Rostedt1d09daa2008-05-12 21:20:55 +02003221void lock_release(struct lockdep_map *lock, int nested,
Steven Rostedt0764d232008-05-12 21:20:44 +02003222 unsigned long ip)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003223{
3224 unsigned long flags;
3225
Peter Zijlstraefed7922009-03-04 12:32:55 +01003226 trace_lock_release(lock, nested, ip);
3227
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003228 if (unlikely(current->lockdep_recursion))
3229 return;
3230
3231 raw_local_irq_save(flags);
3232 check_flags(flags);
3233 current->lockdep_recursion = 1;
3234 __lock_release(lock, nested, ip);
3235 current->lockdep_recursion = 0;
3236 raw_local_irq_restore(flags);
3237}
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003238EXPORT_SYMBOL_GPL(lock_release);
3239
Peter Zijlstraf607c662009-07-20 19:16:29 +02003240int lock_is_held(struct lockdep_map *lock)
3241{
3242 unsigned long flags;
3243 int ret = 0;
3244
3245 if (unlikely(current->lockdep_recursion))
3246 return ret;
3247
3248 raw_local_irq_save(flags);
3249 check_flags(flags);
3250
3251 current->lockdep_recursion = 1;
3252 ret = __lock_is_held(lock);
3253 current->lockdep_recursion = 0;
3254 raw_local_irq_restore(flags);
3255
3256 return ret;
3257}
3258EXPORT_SYMBOL_GPL(lock_is_held);
3259
Nick Piggincf40bd12009-01-21 08:12:39 +01003260void lockdep_set_current_reclaim_state(gfp_t gfp_mask)
3261{
3262 current->lockdep_reclaim_gfp = gfp_mask;
3263}
3264
3265void lockdep_clear_current_reclaim_state(void)
3266{
3267 current->lockdep_reclaim_gfp = 0;
3268}
3269
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003270#ifdef CONFIG_LOCK_STAT
3271static int
3272print_lock_contention_bug(struct task_struct *curr, struct lockdep_map *lock,
3273 unsigned long ip)
3274{
3275 if (!debug_locks_off())
3276 return 0;
3277 if (debug_locks_silent)
3278 return 0;
3279
3280 printk("\n=================================\n");
3281 printk( "[ BUG: bad contention detected! ]\n");
3282 printk( "---------------------------------\n");
3283 printk("%s/%d is trying to contend lock (",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07003284 curr->comm, task_pid_nr(curr));
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003285 print_lockdep_cache(lock);
3286 printk(") at:\n");
3287 print_ip_sym(ip);
3288 printk("but there are no locks held!\n");
3289 printk("\nother info that might help us debug this:\n");
3290 lockdep_print_held_locks(curr);
3291
3292 printk("\nstack backtrace:\n");
3293 dump_stack();
3294
3295 return 0;
3296}
3297
3298static void
3299__lock_contended(struct lockdep_map *lock, unsigned long ip)
3300{
3301 struct task_struct *curr = current;
3302 struct held_lock *hlock, *prev_hlock;
3303 struct lock_class_stats *stats;
3304 unsigned int depth;
Peter Zijlstrac7e78cf2008-10-16 23:17:09 +02003305 int i, contention_point, contending_point;
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003306
3307 depth = curr->lockdep_depth;
3308 if (DEBUG_LOCKS_WARN_ON(!depth))
3309 return;
3310
3311 prev_hlock = NULL;
3312 for (i = depth-1; i >= 0; i--) {
3313 hlock = curr->held_locks + i;
3314 /*
3315 * We must not cross into another context:
3316 */
3317 if (prev_hlock && prev_hlock->irq_context != hlock->irq_context)
3318 break;
Peter Zijlstrabb97a912009-07-20 19:15:35 +02003319 if (match_held_lock(hlock, lock))
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003320 goto found_it;
3321 prev_hlock = hlock;
3322 }
3323 print_lock_contention_bug(curr, lock, ip);
3324 return;
3325
3326found_it:
Peter Zijlstrabb97a912009-07-20 19:15:35 +02003327 if (hlock->instance != lock)
3328 return;
3329
Peter Zijlstra3365e7792009-10-09 10:12:41 +02003330 hlock->waittime_stamp = lockstat_clock();
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003331
Peter Zijlstrac7e78cf2008-10-16 23:17:09 +02003332 contention_point = lock_point(hlock_class(hlock)->contention_point, ip);
3333 contending_point = lock_point(hlock_class(hlock)->contending_point,
3334 lock->ip);
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003335
Dave Jonesf82b2172008-08-11 09:30:23 +02003336 stats = get_lock_stats(hlock_class(hlock));
Peter Zijlstrac7e78cf2008-10-16 23:17:09 +02003337 if (contention_point < LOCKSTAT_POINTS)
3338 stats->contention_point[contention_point]++;
3339 if (contending_point < LOCKSTAT_POINTS)
3340 stats->contending_point[contending_point]++;
Peter Zijlstra96645672007-07-19 01:49:00 -07003341 if (lock->cpu != smp_processor_id())
3342 stats->bounces[bounce_contended + !!hlock->read]++;
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003343 put_lock_stats(stats);
3344}
3345
3346static void
Peter Zijlstrac7e78cf2008-10-16 23:17:09 +02003347__lock_acquired(struct lockdep_map *lock, unsigned long ip)
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003348{
3349 struct task_struct *curr = current;
3350 struct held_lock *hlock, *prev_hlock;
3351 struct lock_class_stats *stats;
3352 unsigned int depth;
Peter Zijlstra3365e7792009-10-09 10:12:41 +02003353 u64 now, waittime = 0;
Peter Zijlstra96645672007-07-19 01:49:00 -07003354 int i, cpu;
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003355
3356 depth = curr->lockdep_depth;
3357 if (DEBUG_LOCKS_WARN_ON(!depth))
3358 return;
3359
3360 prev_hlock = NULL;
3361 for (i = depth-1; i >= 0; i--) {
3362 hlock = curr->held_locks + i;
3363 /*
3364 * We must not cross into another context:
3365 */
3366 if (prev_hlock && prev_hlock->irq_context != hlock->irq_context)
3367 break;
Peter Zijlstrabb97a912009-07-20 19:15:35 +02003368 if (match_held_lock(hlock, lock))
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003369 goto found_it;
3370 prev_hlock = hlock;
3371 }
3372 print_lock_contention_bug(curr, lock, _RET_IP_);
3373 return;
3374
3375found_it:
Peter Zijlstrabb97a912009-07-20 19:15:35 +02003376 if (hlock->instance != lock)
3377 return;
3378
Peter Zijlstra96645672007-07-19 01:49:00 -07003379 cpu = smp_processor_id();
3380 if (hlock->waittime_stamp) {
Peter Zijlstra3365e7792009-10-09 10:12:41 +02003381 now = lockstat_clock();
Peter Zijlstra96645672007-07-19 01:49:00 -07003382 waittime = now - hlock->waittime_stamp;
3383 hlock->holdtime_stamp = now;
3384 }
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003385
Frederic Weisbecker20625012009-04-06 01:49:33 +02003386 trace_lock_acquired(lock, ip, waittime);
3387
Dave Jonesf82b2172008-08-11 09:30:23 +02003388 stats = get_lock_stats(hlock_class(hlock));
Peter Zijlstra96645672007-07-19 01:49:00 -07003389 if (waittime) {
3390 if (hlock->read)
3391 lock_time_inc(&stats->read_waittime, waittime);
3392 else
3393 lock_time_inc(&stats->write_waittime, waittime);
3394 }
3395 if (lock->cpu != cpu)
3396 stats->bounces[bounce_acquired + !!hlock->read]++;
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003397 put_lock_stats(stats);
Peter Zijlstra96645672007-07-19 01:49:00 -07003398
3399 lock->cpu = cpu;
Peter Zijlstrac7e78cf2008-10-16 23:17:09 +02003400 lock->ip = ip;
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003401}
3402
3403void lock_contended(struct lockdep_map *lock, unsigned long ip)
3404{
3405 unsigned long flags;
3406
Peter Zijlstraefed7922009-03-04 12:32:55 +01003407 trace_lock_contended(lock, ip);
3408
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003409 if (unlikely(!lock_stat))
3410 return;
3411
3412 if (unlikely(current->lockdep_recursion))
3413 return;
3414
3415 raw_local_irq_save(flags);
3416 check_flags(flags);
3417 current->lockdep_recursion = 1;
3418 __lock_contended(lock, ip);
3419 current->lockdep_recursion = 0;
3420 raw_local_irq_restore(flags);
3421}
3422EXPORT_SYMBOL_GPL(lock_contended);
3423
Peter Zijlstrac7e78cf2008-10-16 23:17:09 +02003424void lock_acquired(struct lockdep_map *lock, unsigned long ip)
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003425{
3426 unsigned long flags;
3427
3428 if (unlikely(!lock_stat))
3429 return;
3430
3431 if (unlikely(current->lockdep_recursion))
3432 return;
3433
3434 raw_local_irq_save(flags);
3435 check_flags(flags);
3436 current->lockdep_recursion = 1;
Peter Zijlstrac7e78cf2008-10-16 23:17:09 +02003437 __lock_acquired(lock, ip);
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003438 current->lockdep_recursion = 0;
3439 raw_local_irq_restore(flags);
3440}
3441EXPORT_SYMBOL_GPL(lock_acquired);
3442#endif
3443
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003444/*
3445 * Used by the testsuite, sanitize the validator state
3446 * after a simulated failure:
3447 */
3448
3449void lockdep_reset(void)
3450{
3451 unsigned long flags;
Ingo Molnar23d95a02006-12-13 00:34:40 -08003452 int i;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003453
3454 raw_local_irq_save(flags);
3455 current->curr_chain_key = 0;
3456 current->lockdep_depth = 0;
3457 current->lockdep_recursion = 0;
3458 memset(current->held_locks, 0, MAX_LOCK_DEPTH*sizeof(struct held_lock));
3459 nr_hardirq_chains = 0;
3460 nr_softirq_chains = 0;
3461 nr_process_chains = 0;
3462 debug_locks = 1;
Ingo Molnar23d95a02006-12-13 00:34:40 -08003463 for (i = 0; i < CHAINHASH_SIZE; i++)
3464 INIT_LIST_HEAD(chainhash_table + i);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003465 raw_local_irq_restore(flags);
3466}
3467
3468static void zap_class(struct lock_class *class)
3469{
3470 int i;
3471
3472 /*
3473 * Remove all dependencies this lock is
3474 * involved in:
3475 */
3476 for (i = 0; i < nr_list_entries; i++) {
3477 if (list_entries[i].class == class)
3478 list_del_rcu(&list_entries[i].entry);
3479 }
3480 /*
3481 * Unhash the class and remove it from the all_lock_classes list:
3482 */
3483 list_del_rcu(&class->hash_entry);
3484 list_del_rcu(&class->lock_entry);
3485
Rabin Vincent8bfe0292008-08-11 09:30:26 +02003486 class->key = NULL;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003487}
3488
Arjan van de Venfabe8742008-01-24 07:00:45 +01003489static inline int within(const void *addr, void *start, unsigned long size)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003490{
3491 return addr >= start && addr < start + size;
3492}
3493
3494void lockdep_free_key_range(void *start, unsigned long size)
3495{
3496 struct lock_class *class, *next;
3497 struct list_head *head;
3498 unsigned long flags;
3499 int i;
Nick Piggin5a26db52008-01-16 09:51:58 +01003500 int locked;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003501
3502 raw_local_irq_save(flags);
Nick Piggin5a26db52008-01-16 09:51:58 +01003503 locked = graph_lock();
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003504
3505 /*
3506 * Unhash all classes that were created by this module:
3507 */
3508 for (i = 0; i < CLASSHASH_SIZE; i++) {
3509 head = classhash_table + i;
3510 if (list_empty(head))
3511 continue;
Arjan van de Venfabe8742008-01-24 07:00:45 +01003512 list_for_each_entry_safe(class, next, head, hash_entry) {
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003513 if (within(class->key, start, size))
3514 zap_class(class);
Arjan van de Venfabe8742008-01-24 07:00:45 +01003515 else if (within(class->name, start, size))
3516 zap_class(class);
3517 }
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003518 }
3519
Nick Piggin5a26db52008-01-16 09:51:58 +01003520 if (locked)
3521 graph_unlock();
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003522 raw_local_irq_restore(flags);
3523}
3524
3525void lockdep_reset_lock(struct lockdep_map *lock)
3526{
Ingo Molnard6d897c2006-07-10 04:44:04 -07003527 struct lock_class *class, *next;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003528 struct list_head *head;
3529 unsigned long flags;
3530 int i, j;
Nick Piggin5a26db52008-01-16 09:51:58 +01003531 int locked;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003532
3533 raw_local_irq_save(flags);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003534
3535 /*
Ingo Molnard6d897c2006-07-10 04:44:04 -07003536 * Remove all classes this lock might have:
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003537 */
Ingo Molnard6d897c2006-07-10 04:44:04 -07003538 for (j = 0; j < MAX_LOCKDEP_SUBCLASSES; j++) {
3539 /*
3540 * If the class exists we look it up and zap it:
3541 */
3542 class = look_up_lock_class(lock, j);
3543 if (class)
3544 zap_class(class);
3545 }
3546 /*
3547 * Debug check: in the end all mapped classes should
3548 * be gone.
3549 */
Nick Piggin5a26db52008-01-16 09:51:58 +01003550 locked = graph_lock();
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003551 for (i = 0; i < CLASSHASH_SIZE; i++) {
3552 head = classhash_table + i;
3553 if (list_empty(head))
3554 continue;
3555 list_for_each_entry_safe(class, next, head, hash_entry) {
Ingo Molnard6d897c2006-07-10 04:44:04 -07003556 if (unlikely(class == lock->class_cache)) {
Ingo Molnar74c383f2006-12-13 00:34:43 -08003557 if (debug_locks_off_graph_unlock())
3558 WARN_ON(1);
Ingo Molnard6d897c2006-07-10 04:44:04 -07003559 goto out_restore;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003560 }
3561 }
3562 }
Nick Piggin5a26db52008-01-16 09:51:58 +01003563 if (locked)
3564 graph_unlock();
Ingo Molnard6d897c2006-07-10 04:44:04 -07003565
3566out_restore:
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003567 raw_local_irq_restore(flags);
3568}
3569
Sam Ravnborg14999932007-02-28 20:12:31 -08003570void lockdep_init(void)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003571{
3572 int i;
3573
3574 /*
3575 * Some architectures have their own start_kernel()
3576 * code which calls lockdep_init(), while we also
3577 * call lockdep_init() from the start_kernel() itself,
3578 * and we want to initialize the hashes only once:
3579 */
3580 if (lockdep_initialized)
3581 return;
3582
3583 for (i = 0; i < CLASSHASH_SIZE; i++)
3584 INIT_LIST_HEAD(classhash_table + i);
3585
3586 for (i = 0; i < CHAINHASH_SIZE; i++)
3587 INIT_LIST_HEAD(chainhash_table + i);
3588
3589 lockdep_initialized = 1;
3590}
3591
3592void __init lockdep_info(void)
3593{
3594 printk("Lock dependency validator: Copyright (c) 2006 Red Hat, Inc., Ingo Molnar\n");
3595
Li Zefanb0788ca2008-11-21 15:57:32 +08003596 printk("... MAX_LOCKDEP_SUBCLASSES: %lu\n", MAX_LOCKDEP_SUBCLASSES);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003597 printk("... MAX_LOCK_DEPTH: %lu\n", MAX_LOCK_DEPTH);
3598 printk("... MAX_LOCKDEP_KEYS: %lu\n", MAX_LOCKDEP_KEYS);
Li Zefanb0788ca2008-11-21 15:57:32 +08003599 printk("... CLASSHASH_SIZE: %lu\n", CLASSHASH_SIZE);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003600 printk("... MAX_LOCKDEP_ENTRIES: %lu\n", MAX_LOCKDEP_ENTRIES);
3601 printk("... MAX_LOCKDEP_CHAINS: %lu\n", MAX_LOCKDEP_CHAINS);
3602 printk("... CHAINHASH_SIZE: %lu\n", CHAINHASH_SIZE);
3603
3604 printk(" memory used by lock dependency info: %lu kB\n",
3605 (sizeof(struct lock_class) * MAX_LOCKDEP_KEYS +
3606 sizeof(struct list_head) * CLASSHASH_SIZE +
3607 sizeof(struct lock_list) * MAX_LOCKDEP_ENTRIES +
3608 sizeof(struct lock_chain) * MAX_LOCKDEP_CHAINS +
Ming Lei90629202009-08-02 21:43:36 +08003609 sizeof(struct list_head) * CHAINHASH_SIZE
Ming Lei4dd861d2009-07-16 15:44:29 +02003610#ifdef CONFIG_PROVE_LOCKING
Ming Leie351b662009-07-22 22:48:09 +08003611 + sizeof(struct circular_queue)
Ming Lei4dd861d2009-07-16 15:44:29 +02003612#endif
Ming Lei90629202009-08-02 21:43:36 +08003613 ) / 1024
Ming Lei4dd861d2009-07-16 15:44:29 +02003614 );
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003615
3616 printk(" per task-struct memory footprint: %lu bytes\n",
3617 sizeof(struct held_lock) * MAX_LOCK_DEPTH);
3618
3619#ifdef CONFIG_DEBUG_LOCKDEP
Johannes Bergc71063c2007-07-19 01:49:02 -07003620 if (lockdep_init_error) {
3621 printk("WARNING: lockdep init error! Arch code didn't call lockdep_init() early enough?\n");
3622 printk("Call stack leading to lockdep invocation was:\n");
3623 print_stack_trace(&lockdep_init_trace, 0);
3624 }
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003625#endif
3626}
3627
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003628static void
3629print_freed_lock_bug(struct task_struct *curr, const void *mem_from,
Arjan van de Ven55794a42006-07-10 04:44:03 -07003630 const void *mem_to, struct held_lock *hlock)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003631{
3632 if (!debug_locks_off())
3633 return;
3634 if (debug_locks_silent)
3635 return;
3636
3637 printk("\n=========================\n");
3638 printk( "[ BUG: held lock freed! ]\n");
3639 printk( "-------------------------\n");
3640 printk("%s/%d is freeing memory %p-%p, with a lock still held there!\n",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07003641 curr->comm, task_pid_nr(curr), mem_from, mem_to-1);
Arjan van de Ven55794a42006-07-10 04:44:03 -07003642 print_lock(hlock);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003643 lockdep_print_held_locks(curr);
3644
3645 printk("\nstack backtrace:\n");
3646 dump_stack();
3647}
3648
Oleg Nesterov54561782007-12-05 15:46:09 +01003649static inline int not_in_range(const void* mem_from, unsigned long mem_len,
3650 const void* lock_from, unsigned long lock_len)
3651{
3652 return lock_from + lock_len <= mem_from ||
3653 mem_from + mem_len <= lock_from;
3654}
3655
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003656/*
3657 * Called when kernel memory is freed (or unmapped), or if a lock
3658 * is destroyed or reinitialized - this code checks whether there is
3659 * any held lock in the memory range of <from> to <to>:
3660 */
3661void debug_check_no_locks_freed(const void *mem_from, unsigned long mem_len)
3662{
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003663 struct task_struct *curr = current;
3664 struct held_lock *hlock;
3665 unsigned long flags;
3666 int i;
3667
3668 if (unlikely(!debug_locks))
3669 return;
3670
3671 local_irq_save(flags);
3672 for (i = 0; i < curr->lockdep_depth; i++) {
3673 hlock = curr->held_locks + i;
3674
Oleg Nesterov54561782007-12-05 15:46:09 +01003675 if (not_in_range(mem_from, mem_len, hlock->instance,
3676 sizeof(*hlock->instance)))
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003677 continue;
3678
Oleg Nesterov54561782007-12-05 15:46:09 +01003679 print_freed_lock_bug(curr, mem_from, mem_from + mem_len, hlock);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003680 break;
3681 }
3682 local_irq_restore(flags);
3683}
Peter Zijlstraed075362006-12-06 20:35:24 -08003684EXPORT_SYMBOL_GPL(debug_check_no_locks_freed);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003685
3686static void print_held_locks_bug(struct task_struct *curr)
3687{
3688 if (!debug_locks_off())
3689 return;
3690 if (debug_locks_silent)
3691 return;
3692
3693 printk("\n=====================================\n");
3694 printk( "[ BUG: lock held at task exit time! ]\n");
3695 printk( "-------------------------------------\n");
3696 printk("%s/%d is exiting with locks still held!\n",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07003697 curr->comm, task_pid_nr(curr));
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003698 lockdep_print_held_locks(curr);
3699
3700 printk("\nstack backtrace:\n");
3701 dump_stack();
3702}
3703
3704void debug_check_no_locks_held(struct task_struct *task)
3705{
3706 if (unlikely(task->lockdep_depth > 0))
3707 print_held_locks_bug(task);
3708}
3709
3710void debug_show_all_locks(void)
3711{
3712 struct task_struct *g, *p;
3713 int count = 10;
3714 int unlock = 1;
3715
Jarek Poplawski9c35dd72007-03-22 00:11:28 -08003716 if (unlikely(!debug_locks)) {
3717 printk("INFO: lockdep is turned off.\n");
3718 return;
3719 }
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003720 printk("\nShowing all locks held in the system:\n");
3721
3722 /*
3723 * Here we try to get the tasklist_lock as hard as possible,
3724 * if not successful after 2 seconds we ignore it (but keep
3725 * trying). This is to enable a debug printout even if a
3726 * tasklist_lock-holding task deadlocks or crashes.
3727 */
3728retry:
3729 if (!read_trylock(&tasklist_lock)) {
3730 if (count == 10)
3731 printk("hm, tasklist_lock locked, retrying... ");
3732 if (count) {
3733 count--;
3734 printk(" #%d", 10-count);
3735 mdelay(200);
3736 goto retry;
3737 }
3738 printk(" ignoring it.\n");
3739 unlock = 0;
qinghuang feng46fec7a2008-10-28 17:24:28 +08003740 } else {
3741 if (count != 10)
3742 printk(KERN_CONT " locked it.\n");
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003743 }
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003744
3745 do_each_thread(g, p) {
Ingo Molnar85684872007-12-05 15:46:09 +01003746 /*
3747 * It's not reliable to print a task's held locks
3748 * if it's not sleeping (or if it's not the current
3749 * task):
3750 */
3751 if (p->state == TASK_RUNNING && p != current)
3752 continue;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003753 if (p->lockdep_depth)
3754 lockdep_print_held_locks(p);
3755 if (!unlock)
3756 if (read_trylock(&tasklist_lock))
3757 unlock = 1;
3758 } while_each_thread(g, p);
3759
3760 printk("\n");
3761 printk("=============================================\n\n");
3762
3763 if (unlock)
3764 read_unlock(&tasklist_lock);
3765}
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003766EXPORT_SYMBOL_GPL(debug_show_all_locks);
3767
Ingo Molnar82a1fcb2008-01-25 21:08:02 +01003768/*
3769 * Careful: only use this function if you are sure that
3770 * the task cannot run in parallel!
3771 */
3772void __debug_show_held_locks(struct task_struct *task)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003773{
Jarek Poplawski9c35dd72007-03-22 00:11:28 -08003774 if (unlikely(!debug_locks)) {
3775 printk("INFO: lockdep is turned off.\n");
3776 return;
3777 }
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003778 lockdep_print_held_locks(task);
3779}
Ingo Molnar82a1fcb2008-01-25 21:08:02 +01003780EXPORT_SYMBOL_GPL(__debug_show_held_locks);
3781
3782void debug_show_held_locks(struct task_struct *task)
3783{
3784 __debug_show_held_locks(task);
3785}
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003786EXPORT_SYMBOL_GPL(debug_show_held_locks);
Peter Zijlstrab351d162007-10-11 22:11:12 +02003787
3788void lockdep_sys_exit(void)
3789{
3790 struct task_struct *curr = current;
3791
3792 if (unlikely(curr->lockdep_depth)) {
3793 if (!debug_locks_off())
3794 return;
3795 printk("\n================================================\n");
3796 printk( "[ BUG: lock held when returning to user space! ]\n");
3797 printk( "------------------------------------------------\n");
3798 printk("%s/%d is leaving the kernel with locks still held!\n",
3799 curr->comm, curr->pid);
3800 lockdep_print_held_locks(curr);
3801 }
3802}