blob: 54cc0dc7b3033af33f6d4397be53253c5f0ebb36 [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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090046#include <linux/gfp.h>
Yong Zhangd3d03d42011-11-09 16:04:51 +080047#include <linux/kmemcheck.h>
Peter Zijlstraaf012962009-07-16 15:44:29 +020048
Ingo Molnarfbb9ce952006-07-03 00:24:50 -070049#include <asm/sections.h>
50
51#include "lockdep_internals.h"
52
Steven Rostedta8d154b2009-04-10 09:36:00 -040053#define CREATE_TRACE_POINTS
Frederic Weisbecker67178762009-11-13 10:06:34 +010054#include <trace/events/lock.h>
Steven Rostedta8d154b2009-04-10 09:36:00 -040055
Peter Zijlstraf20786f2007-07-19 01:48:56 -070056#ifdef CONFIG_PROVE_LOCKING
57int prove_locking = 1;
58module_param(prove_locking, int, 0644);
59#else
60#define prove_locking 0
61#endif
62
63#ifdef CONFIG_LOCK_STAT
64int lock_stat = 1;
65module_param(lock_stat, int, 0644);
66#else
67#define lock_stat 0
68#endif
69
Ingo Molnarfbb9ce952006-07-03 00:24:50 -070070/*
Ingo Molnar74c383f2006-12-13 00:34:43 -080071 * lockdep_lock: protects the lockdep graph, the hashes and the
72 * class/list/hash allocators.
Ingo Molnarfbb9ce952006-07-03 00:24:50 -070073 *
74 * This is one of the rare exceptions where it's justified
75 * to use a raw spinlock - we really dont want the spinlock
Ingo Molnar74c383f2006-12-13 00:34:43 -080076 * code to recurse back into the lockdep code...
Ingo Molnarfbb9ce952006-07-03 00:24:50 -070077 */
Thomas Gleixneredc35bd2009-12-03 12:38:57 +010078static arch_spinlock_t lockdep_lock = (arch_spinlock_t)__ARCH_SPIN_LOCK_UNLOCKED;
Ingo Molnar74c383f2006-12-13 00:34:43 -080079
80static int graph_lock(void)
81{
Thomas Gleixner0199c4e2009-12-02 20:01:25 +010082 arch_spin_lock(&lockdep_lock);
Ingo Molnar74c383f2006-12-13 00:34:43 -080083 /*
84 * Make sure that if another CPU detected a bug while
85 * walking the graph we dont change it (while the other
86 * CPU is busy printing out stuff with the graph lock
87 * dropped already)
88 */
89 if (!debug_locks) {
Thomas Gleixner0199c4e2009-12-02 20:01:25 +010090 arch_spin_unlock(&lockdep_lock);
Ingo Molnar74c383f2006-12-13 00:34:43 -080091 return 0;
92 }
Steven Rostedtbb065af2008-05-12 21:21:00 +020093 /* prevent any recursions within lockdep from causing deadlocks */
94 current->lockdep_recursion++;
Ingo Molnar74c383f2006-12-13 00:34:43 -080095 return 1;
96}
97
98static inline int graph_unlock(void)
99{
Peter Zijlstra0119fee2011-09-02 01:30:29 +0200100 if (debug_locks && !arch_spin_is_locked(&lockdep_lock)) {
101 /*
102 * The lockdep graph lock isn't locked while we expect it to
103 * be, we're confused now, bye!
104 */
Jarek Poplawski381a2292007-02-10 01:44:58 -0800105 return DEBUG_LOCKS_WARN_ON(1);
Peter Zijlstra0119fee2011-09-02 01:30:29 +0200106 }
Jarek Poplawski381a2292007-02-10 01:44:58 -0800107
Steven Rostedtbb065af2008-05-12 21:21:00 +0200108 current->lockdep_recursion--;
Thomas Gleixner0199c4e2009-12-02 20:01:25 +0100109 arch_spin_unlock(&lockdep_lock);
Ingo Molnar74c383f2006-12-13 00:34:43 -0800110 return 0;
111}
112
113/*
114 * Turn lock debugging off and return with 0 if it was off already,
115 * and also release the graph lock:
116 */
117static inline int debug_locks_off_graph_unlock(void)
118{
119 int ret = debug_locks_off();
120
Thomas Gleixner0199c4e2009-12-02 20:01:25 +0100121 arch_spin_unlock(&lockdep_lock);
Ingo Molnar74c383f2006-12-13 00:34:43 -0800122
123 return ret;
124}
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700125
126static int lockdep_initialized;
127
128unsigned long nr_list_entries;
Peter Zijlstraaf012962009-07-16 15:44:29 +0200129static struct lock_list list_entries[MAX_LOCKDEP_ENTRIES];
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700130
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700131/*
132 * All data structures here are protected by the global debug_lock.
133 *
134 * Mutex key structs only get allocated, once during bootup, and never
135 * get freed - this significantly simplifies the debugging code.
136 */
137unsigned long nr_lock_classes;
138static struct lock_class lock_classes[MAX_LOCKDEP_KEYS];
139
Dave Jonesf82b2172008-08-11 09:30:23 +0200140static inline struct lock_class *hlock_class(struct held_lock *hlock)
141{
142 if (!hlock->class_idx) {
Peter Zijlstra0119fee2011-09-02 01:30:29 +0200143 /*
144 * Someone passed in garbage, we give up.
145 */
Dave Jonesf82b2172008-08-11 09:30:23 +0200146 DEBUG_LOCKS_WARN_ON(1);
147 return NULL;
148 }
149 return lock_classes + hlock->class_idx - 1;
150}
151
Peter Zijlstraf20786f2007-07-19 01:48:56 -0700152#ifdef CONFIG_LOCK_STAT
Tejun Heo1871e522009-10-29 22:34:13 +0900153static DEFINE_PER_CPU(struct lock_class_stats[MAX_LOCKDEP_KEYS],
154 cpu_lock_stats);
Peter Zijlstraf20786f2007-07-19 01:48:56 -0700155
Peter Zijlstra3365e7792009-10-09 10:12:41 +0200156static inline u64 lockstat_clock(void)
157{
Peter Zijlstrac6763292010-05-25 10:48:51 +0200158 return local_clock();
Peter Zijlstra3365e7792009-10-09 10:12:41 +0200159}
160
Peter Zijlstrac7e78cf2008-10-16 23:17:09 +0200161static int lock_point(unsigned long points[], unsigned long ip)
Peter Zijlstraf20786f2007-07-19 01:48:56 -0700162{
163 int i;
164
Peter Zijlstrac7e78cf2008-10-16 23:17:09 +0200165 for (i = 0; i < LOCKSTAT_POINTS; i++) {
166 if (points[i] == 0) {
167 points[i] = ip;
Peter Zijlstraf20786f2007-07-19 01:48:56 -0700168 break;
169 }
Peter Zijlstrac7e78cf2008-10-16 23:17:09 +0200170 if (points[i] == ip)
Peter Zijlstraf20786f2007-07-19 01:48:56 -0700171 break;
172 }
173
174 return i;
175}
176
Peter Zijlstra3365e7792009-10-09 10:12:41 +0200177static void lock_time_inc(struct lock_time *lt, u64 time)
Peter Zijlstraf20786f2007-07-19 01:48:56 -0700178{
179 if (time > lt->max)
180 lt->max = time;
181
Frank Rowand109d71c2009-11-19 13:42:06 -0800182 if (time < lt->min || !lt->nr)
Peter Zijlstraf20786f2007-07-19 01:48:56 -0700183 lt->min = time;
184
185 lt->total += time;
186 lt->nr++;
187}
188
Peter Zijlstrac46261d2007-07-19 01:48:57 -0700189static inline void lock_time_add(struct lock_time *src, struct lock_time *dst)
190{
Frank Rowand109d71c2009-11-19 13:42:06 -0800191 if (!src->nr)
192 return;
193
194 if (src->max > dst->max)
195 dst->max = src->max;
196
197 if (src->min < dst->min || !dst->nr)
198 dst->min = src->min;
199
Peter Zijlstrac46261d2007-07-19 01:48:57 -0700200 dst->total += src->total;
201 dst->nr += src->nr;
202}
203
204struct lock_class_stats lock_stats(struct lock_class *class)
205{
206 struct lock_class_stats stats;
207 int cpu, i;
208
209 memset(&stats, 0, sizeof(struct lock_class_stats));
210 for_each_possible_cpu(cpu) {
211 struct lock_class_stats *pcs =
Tejun Heo1871e522009-10-29 22:34:13 +0900212 &per_cpu(cpu_lock_stats, cpu)[class - lock_classes];
Peter Zijlstrac46261d2007-07-19 01:48:57 -0700213
214 for (i = 0; i < ARRAY_SIZE(stats.contention_point); i++)
215 stats.contention_point[i] += pcs->contention_point[i];
216
Peter Zijlstrac7e78cf2008-10-16 23:17:09 +0200217 for (i = 0; i < ARRAY_SIZE(stats.contending_point); i++)
218 stats.contending_point[i] += pcs->contending_point[i];
219
Peter Zijlstrac46261d2007-07-19 01:48:57 -0700220 lock_time_add(&pcs->read_waittime, &stats.read_waittime);
221 lock_time_add(&pcs->write_waittime, &stats.write_waittime);
222
223 lock_time_add(&pcs->read_holdtime, &stats.read_holdtime);
224 lock_time_add(&pcs->write_holdtime, &stats.write_holdtime);
Peter Zijlstra96645672007-07-19 01:49:00 -0700225
226 for (i = 0; i < ARRAY_SIZE(stats.bounces); i++)
227 stats.bounces[i] += pcs->bounces[i];
Peter Zijlstrac46261d2007-07-19 01:48:57 -0700228 }
229
230 return stats;
231}
232
233void clear_lock_stats(struct lock_class *class)
234{
235 int cpu;
236
237 for_each_possible_cpu(cpu) {
238 struct lock_class_stats *cpu_stats =
Tejun Heo1871e522009-10-29 22:34:13 +0900239 &per_cpu(cpu_lock_stats, cpu)[class - lock_classes];
Peter Zijlstrac46261d2007-07-19 01:48:57 -0700240
241 memset(cpu_stats, 0, sizeof(struct lock_class_stats));
242 }
243 memset(class->contention_point, 0, sizeof(class->contention_point));
Peter Zijlstrac7e78cf2008-10-16 23:17:09 +0200244 memset(class->contending_point, 0, sizeof(class->contending_point));
Peter Zijlstrac46261d2007-07-19 01:48:57 -0700245}
246
Peter Zijlstraf20786f2007-07-19 01:48:56 -0700247static struct lock_class_stats *get_lock_stats(struct lock_class *class)
248{
Tejun Heo1871e522009-10-29 22:34:13 +0900249 return &get_cpu_var(cpu_lock_stats)[class - lock_classes];
Peter Zijlstraf20786f2007-07-19 01:48:56 -0700250}
251
252static void put_lock_stats(struct lock_class_stats *stats)
253{
Tejun Heo1871e522009-10-29 22:34:13 +0900254 put_cpu_var(cpu_lock_stats);
Peter Zijlstraf20786f2007-07-19 01:48:56 -0700255}
256
257static void lock_release_holdtime(struct held_lock *hlock)
258{
259 struct lock_class_stats *stats;
Peter Zijlstra3365e7792009-10-09 10:12:41 +0200260 u64 holdtime;
Peter Zijlstraf20786f2007-07-19 01:48:56 -0700261
262 if (!lock_stat)
263 return;
264
Peter Zijlstra3365e7792009-10-09 10:12:41 +0200265 holdtime = lockstat_clock() - hlock->holdtime_stamp;
Peter Zijlstraf20786f2007-07-19 01:48:56 -0700266
Dave Jonesf82b2172008-08-11 09:30:23 +0200267 stats = get_lock_stats(hlock_class(hlock));
Peter Zijlstraf20786f2007-07-19 01:48:56 -0700268 if (hlock->read)
269 lock_time_inc(&stats->read_holdtime, holdtime);
270 else
271 lock_time_inc(&stats->write_holdtime, holdtime);
272 put_lock_stats(stats);
273}
274#else
275static inline void lock_release_holdtime(struct held_lock *hlock)
276{
277}
278#endif
279
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700280/*
281 * We keep a global list of all lock classes. The list only grows,
282 * never shrinks. The list is only accessed with the lockdep
283 * spinlock lock held.
284 */
285LIST_HEAD(all_lock_classes);
286
287/*
288 * The lockdep classes are in a hash-table as well, for fast lookup:
289 */
290#define CLASSHASH_BITS (MAX_LOCKDEP_KEYS_BITS - 1)
291#define CLASSHASH_SIZE (1UL << CLASSHASH_BITS)
Peter Zijlstra4b32d0a2007-07-19 01:48:59 -0700292#define __classhashfn(key) hash_long((unsigned long)key, CLASSHASH_BITS)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700293#define classhashentry(key) (classhash_table + __classhashfn((key)))
294
295static struct list_head classhash_table[CLASSHASH_SIZE];
296
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700297/*
298 * We put the lock dependency chains into a hash-table as well, to cache
299 * their existence:
300 */
301#define CHAINHASH_BITS (MAX_LOCKDEP_CHAINS_BITS-1)
302#define CHAINHASH_SIZE (1UL << CHAINHASH_BITS)
Peter Zijlstra4b32d0a2007-07-19 01:48:59 -0700303#define __chainhashfn(chain) hash_long(chain, CHAINHASH_BITS)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700304#define chainhashentry(chain) (chainhash_table + __chainhashfn((chain)))
305
306static struct list_head chainhash_table[CHAINHASH_SIZE];
307
308/*
309 * The hash key of the lock dependency chains is a hash itself too:
310 * it's a hash of all locks taken up to that lock, including that lock.
311 * It's a 64-bit hash, because it's important for the keys to be
312 * unique.
313 */
314#define iterate_chain_key(key1, key2) \
Ingo Molnar03cbc352006-09-29 02:01:46 -0700315 (((key1) << MAX_LOCKDEP_KEYS_BITS) ^ \
316 ((key1) >> (64-MAX_LOCKDEP_KEYS_BITS)) ^ \
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700317 (key2))
318
Steven Rostedt1d09daa2008-05-12 21:20:55 +0200319void lockdep_off(void)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700320{
321 current->lockdep_recursion++;
322}
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700323EXPORT_SYMBOL(lockdep_off);
324
Steven Rostedt1d09daa2008-05-12 21:20:55 +0200325void lockdep_on(void)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700326{
327 current->lockdep_recursion--;
328}
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700329EXPORT_SYMBOL(lockdep_on);
330
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700331/*
332 * Debugging switches:
333 */
334
335#define VERBOSE 0
Ingo Molnar33e94e92006-12-13 00:34:41 -0800336#define VERY_VERBOSE 0
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700337
338#if VERBOSE
339# define HARDIRQ_VERBOSE 1
340# define SOFTIRQ_VERBOSE 1
Nick Piggincf40bd12009-01-21 08:12:39 +0100341# define RECLAIM_VERBOSE 1
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700342#else
343# define HARDIRQ_VERBOSE 0
344# define SOFTIRQ_VERBOSE 0
Nick Piggincf40bd12009-01-21 08:12:39 +0100345# define RECLAIM_VERBOSE 0
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700346#endif
347
Nick Piggincf40bd12009-01-21 08:12:39 +0100348#if VERBOSE || HARDIRQ_VERBOSE || SOFTIRQ_VERBOSE || RECLAIM_VERBOSE
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700349/*
350 * Quick filtering for interesting events:
351 */
352static int class_filter(struct lock_class *class)
353{
Andi Kleenf9829cc2006-07-10 04:44:01 -0700354#if 0
355 /* Example */
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700356 if (class->name_version == 1 &&
Andi Kleenf9829cc2006-07-10 04:44:01 -0700357 !strcmp(class->name, "lockname"))
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700358 return 1;
359 if (class->name_version == 1 &&
Andi Kleenf9829cc2006-07-10 04:44:01 -0700360 !strcmp(class->name, "&struct->lockfield"))
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700361 return 1;
Andi Kleenf9829cc2006-07-10 04:44:01 -0700362#endif
Ingo Molnara6640892006-12-13 00:34:39 -0800363 /* Filter everything else. 1 would be to allow everything else */
364 return 0;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700365}
366#endif
367
368static int verbose(struct lock_class *class)
369{
370#if VERBOSE
371 return class_filter(class);
372#endif
373 return 0;
374}
375
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700376/*
377 * Stack-trace: tightly packed array of stack backtrace
Ingo Molnar74c383f2006-12-13 00:34:43 -0800378 * addresses. Protected by the graph_lock.
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700379 */
380unsigned long nr_stack_trace_entries;
381static unsigned long stack_trace[MAX_STACK_TRACE_ENTRIES];
382
383static int save_trace(struct stack_trace *trace)
384{
385 trace->nr_entries = 0;
386 trace->max_entries = MAX_STACK_TRACE_ENTRIES - nr_stack_trace_entries;
387 trace->entries = stack_trace + nr_stack_trace_entries;
388
Andi Kleen5a1b3992006-09-26 10:52:34 +0200389 trace->skip = 3;
Andi Kleen5a1b3992006-09-26 10:52:34 +0200390
Christoph Hellwigab1b6f02007-05-08 00:23:29 -0700391 save_stack_trace(trace);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700392
Peter Zijlstra4f84f432009-07-20 15:27:04 +0200393 /*
394 * Some daft arches put -1 at the end to indicate its a full trace.
395 *
396 * <rant> this is buggy anyway, since it takes a whole extra entry so a
397 * complete trace that maxes out the entries provided will be reported
398 * as incomplete, friggin useless </rant>
399 */
Luck, Tonyea5b41f2009-12-09 14:29:36 -0800400 if (trace->nr_entries != 0 &&
401 trace->entries[trace->nr_entries-1] == ULONG_MAX)
Peter Zijlstra4f84f432009-07-20 15:27:04 +0200402 trace->nr_entries--;
403
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700404 trace->max_entries = trace->nr_entries;
405
406 nr_stack_trace_entries += trace->nr_entries;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700407
Peter Zijlstra4f84f432009-07-20 15:27:04 +0200408 if (nr_stack_trace_entries >= MAX_STACK_TRACE_ENTRIES-1) {
Ingo Molnar74c383f2006-12-13 00:34:43 -0800409 if (!debug_locks_off_graph_unlock())
410 return 0;
411
412 printk("BUG: MAX_STACK_TRACE_ENTRIES too low!\n");
413 printk("turning off the locking correctness validator.\n");
414 dump_stack();
415
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700416 return 0;
417 }
418
419 return 1;
420}
421
422unsigned int nr_hardirq_chains;
423unsigned int nr_softirq_chains;
424unsigned int nr_process_chains;
425unsigned int max_lockdep_depth;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700426
427#ifdef CONFIG_DEBUG_LOCKDEP
428/*
429 * We cannot printk in early bootup code. Not even early_printk()
430 * might work. So we mark any initialization errors and printk
431 * about it later on, in lockdep_info().
432 */
433static int lockdep_init_error;
Johannes Bergc71063c2007-07-19 01:49:02 -0700434static unsigned long lockdep_init_trace_data[20];
435static struct stack_trace lockdep_init_trace = {
436 .max_entries = ARRAY_SIZE(lockdep_init_trace_data),
437 .entries = lockdep_init_trace_data,
438};
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700439
440/*
441 * Various lockdep statistics:
442 */
Frederic Weisbeckerbd6d29c2010-04-06 00:10:17 +0200443DEFINE_PER_CPU(struct lockdep_stats, lockdep_stats);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700444#endif
445
446/*
447 * Locking printouts:
448 */
449
Peter Zijlstrafabe9c42009-01-22 14:51:01 +0100450#define __USAGE(__STATE) \
Peter Zijlstrab4b136f2009-01-29 14:50:36 +0100451 [LOCK_USED_IN_##__STATE] = "IN-"__stringify(__STATE)"-W", \
452 [LOCK_ENABLED_##__STATE] = __stringify(__STATE)"-ON-W", \
453 [LOCK_USED_IN_##__STATE##_READ] = "IN-"__stringify(__STATE)"-R",\
454 [LOCK_ENABLED_##__STATE##_READ] = __stringify(__STATE)"-ON-R",
Peter Zijlstrafabe9c42009-01-22 14:51:01 +0100455
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700456static const char *usage_str[] =
457{
Peter Zijlstrafabe9c42009-01-22 14:51:01 +0100458#define LOCKDEP_STATE(__STATE) __USAGE(__STATE)
459#include "lockdep_states.h"
460#undef LOCKDEP_STATE
461 [LOCK_USED] = "INITIAL USE",
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700462};
463
464const char * __get_key_name(struct lockdep_subclass_key *key, char *str)
465{
Alexey Dobriyanffb45122007-05-08 00:28:41 -0700466 return kallsyms_lookup((unsigned long)key, NULL, NULL, NULL, str);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700467}
468
Peter Zijlstra3ff176c2009-01-22 17:40:42 +0100469static inline unsigned long lock_flag(enum lock_usage_bit bit)
470{
471 return 1UL << bit;
472}
473
474static char get_usage_char(struct lock_class *class, enum lock_usage_bit bit)
475{
476 char c = '.';
477
478 if (class->usage_mask & lock_flag(bit + 2))
479 c = '+';
480 if (class->usage_mask & lock_flag(bit)) {
481 c = '-';
482 if (class->usage_mask & lock_flag(bit + 2))
483 c = '?';
484 }
485
486 return c;
487}
488
Peter Zijlstraf510b232009-01-22 17:53:47 +0100489void get_usage_chars(struct lock_class *class, char usage[LOCK_USAGE_CHARS])
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700490{
Peter Zijlstraf510b232009-01-22 17:53:47 +0100491 int i = 0;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700492
Peter Zijlstraf510b232009-01-22 17:53:47 +0100493#define LOCKDEP_STATE(__STATE) \
494 usage[i++] = get_usage_char(class, LOCK_USED_IN_##__STATE); \
495 usage[i++] = get_usage_char(class, LOCK_USED_IN_##__STATE##_READ);
496#include "lockdep_states.h"
497#undef LOCKDEP_STATE
498
499 usage[i] = '\0';
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700500}
501
Steven Rostedt3003eba2011-04-20 21:41:54 -0400502static int __print_lock_name(struct lock_class *class)
503{
504 char str[KSYM_NAME_LEN];
505 const char *name;
506
507 name = class->name;
508 if (!name)
509 name = __get_key_name(class->key, str);
510
511 return printk("%s", name);
512}
513
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700514static void print_lock_name(struct lock_class *class)
515{
Peter Zijlstraf510b232009-01-22 17:53:47 +0100516 char str[KSYM_NAME_LEN], usage[LOCK_USAGE_CHARS];
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700517 const char *name;
518
Peter Zijlstraf510b232009-01-22 17:53:47 +0100519 get_usage_chars(class, usage);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700520
521 name = class->name;
522 if (!name) {
523 name = __get_key_name(class->key, str);
524 printk(" (%s", name);
525 } else {
526 printk(" (%s", name);
527 if (class->name_version > 1)
528 printk("#%d", class->name_version);
529 if (class->subclass)
530 printk("/%d", class->subclass);
531 }
Peter Zijlstraf510b232009-01-22 17:53:47 +0100532 printk("){%s}", usage);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700533}
534
535static void print_lockdep_cache(struct lockdep_map *lock)
536{
537 const char *name;
Tejun Heo9281ace2007-07-17 04:03:51 -0700538 char str[KSYM_NAME_LEN];
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700539
540 name = lock->name;
541 if (!name)
542 name = __get_key_name(lock->key->subkeys, str);
543
544 printk("%s", name);
545}
546
547static void print_lock(struct held_lock *hlock)
548{
Dave Jonesf82b2172008-08-11 09:30:23 +0200549 print_lock_name(hlock_class(hlock));
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700550 printk(", at: ");
551 print_ip_sym(hlock->acquire_ip);
552}
553
554static void lockdep_print_held_locks(struct task_struct *curr)
555{
556 int i, depth = curr->lockdep_depth;
557
558 if (!depth) {
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -0700559 printk("no locks held by %s/%d.\n", curr->comm, task_pid_nr(curr));
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700560 return;
561 }
562 printk("%d lock%s held by %s/%d:\n",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -0700563 depth, depth > 1 ? "s" : "", curr->comm, task_pid_nr(curr));
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700564
565 for (i = 0; i < depth; i++) {
566 printk(" #%d: ", i);
567 print_lock(curr->held_locks + i);
568 }
569}
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700570
Ben Hutchingsfbdc4b92011-10-28 04:36:55 +0100571static void print_kernel_ident(void)
Dave Jones99de0552006-09-29 02:00:10 -0700572{
Ben Hutchingsfbdc4b92011-10-28 04:36:55 +0100573 printk("%s %.*s %s\n", init_utsname()->release,
Serge E. Hallyn96b644b2006-10-02 02:18:13 -0700574 (int)strcspn(init_utsname()->version, " "),
Ben Hutchingsfbdc4b92011-10-28 04:36:55 +0100575 init_utsname()->version,
576 print_tainted());
Dave Jones99de0552006-09-29 02:00:10 -0700577}
578
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700579static int very_verbose(struct lock_class *class)
580{
581#if VERY_VERBOSE
582 return class_filter(class);
583#endif
584 return 0;
585}
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700586
587/*
588 * Is this the address of a static object:
589 */
590static int static_obj(void *obj)
591{
592 unsigned long start = (unsigned long) &_stext,
593 end = (unsigned long) &_end,
594 addr = (unsigned long) obj;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700595
596 /*
597 * static variable?
598 */
599 if ((addr >= start) && (addr < end))
600 return 1;
601
Mike Frysinger2a9ad182009-09-22 16:44:16 -0700602 if (arch_is_kernel_data(addr))
603 return 1;
604
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700605 /*
Tejun Heo10fad5e2010-03-10 18:57:54 +0900606 * in-kernel percpu var?
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700607 */
Tejun Heo10fad5e2010-03-10 18:57:54 +0900608 if (is_kernel_percpu_address(addr))
609 return 1;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700610
611 /*
Tejun Heo10fad5e2010-03-10 18:57:54 +0900612 * module static or percpu var?
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700613 */
Tejun Heo10fad5e2010-03-10 18:57:54 +0900614 return is_module_address(addr) || is_module_percpu_address(addr);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700615}
616
617/*
618 * To make lock name printouts unique, we calculate a unique
619 * class->name_version generation counter:
620 */
621static int count_matching_names(struct lock_class *new_class)
622{
623 struct lock_class *class;
624 int count = 0;
625
626 if (!new_class->name)
627 return 0;
628
629 list_for_each_entry(class, &all_lock_classes, lock_entry) {
630 if (new_class->key - new_class->subclass == class->key)
631 return class->name_version;
632 if (class->name && !strcmp(class->name, new_class->name))
633 count = max(count, class->name_version);
634 }
635
636 return count + 1;
637}
638
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700639/*
640 * Register a lock's class in the hash-table, if the class is not present
641 * yet. Otherwise we look it up. We cache the result in the lock object
642 * itself, so actual lookup of the hash should be once per lock object.
643 */
644static inline struct lock_class *
Ingo Molnard6d897c2006-07-10 04:44:04 -0700645look_up_lock_class(struct lockdep_map *lock, unsigned int subclass)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700646{
647 struct lockdep_subclass_key *key;
648 struct list_head *hash_head;
649 struct lock_class *class;
650
651#ifdef CONFIG_DEBUG_LOCKDEP
652 /*
653 * If the architecture calls into lockdep before initializing
654 * the hashes then we'll warn about it later. (we cannot printk
655 * right now)
656 */
657 if (unlikely(!lockdep_initialized)) {
658 lockdep_init();
659 lockdep_init_error = 1;
Johannes Bergc71063c2007-07-19 01:49:02 -0700660 save_stack_trace(&lockdep_init_trace);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700661 }
662#endif
663
Hitoshi Mitake4ba053c2010-10-13 17:30:26 +0900664 if (unlikely(subclass >= MAX_LOCKDEP_SUBCLASSES)) {
665 debug_locks_off();
666 printk(KERN_ERR
667 "BUG: looking up invalid subclass: %u\n", subclass);
668 printk(KERN_ERR
669 "turning off the locking correctness validator.\n");
670 dump_stack();
671 return NULL;
672 }
673
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700674 /*
675 * Static locks do not have their class-keys yet - for them the key
676 * is the lock object itself:
677 */
678 if (unlikely(!lock->key))
679 lock->key = (void *)lock;
680
681 /*
682 * NOTE: the class-key must be unique. For dynamic locks, a static
683 * lock_class_key variable is passed in through the mutex_init()
684 * (or spin_lock_init()) call - which acts as the key. For static
685 * locks we use the lock object itself as the key.
686 */
Peter Zijlstra4b32d0a2007-07-19 01:48:59 -0700687 BUILD_BUG_ON(sizeof(struct lock_class_key) >
688 sizeof(struct lockdep_map));
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700689
690 key = lock->key->subkeys + subclass;
691
692 hash_head = classhashentry(key);
693
694 /*
695 * We can walk the hash lockfree, because the hash only
696 * grows, and we are careful when adding entries to the end:
697 */
Peter Zijlstra4b32d0a2007-07-19 01:48:59 -0700698 list_for_each_entry(class, hash_head, hash_entry) {
699 if (class->key == key) {
Peter Zijlstra0119fee2011-09-02 01:30:29 +0200700 /*
701 * Huh! same key, different name? Did someone trample
702 * on some memory? We're most confused.
703 */
Peter Zijlstra4b32d0a2007-07-19 01:48:59 -0700704 WARN_ON_ONCE(class->name != lock->name);
Ingo Molnard6d897c2006-07-10 04:44:04 -0700705 return class;
Peter Zijlstra4b32d0a2007-07-19 01:48:59 -0700706 }
707 }
Ingo Molnard6d897c2006-07-10 04:44:04 -0700708
709 return NULL;
710}
711
712/*
713 * Register a lock's class in the hash-table, if the class is not present
714 * yet. Otherwise we look it up. We cache the result in the lock object
715 * itself, so actual lookup of the hash should be once per lock object.
716 */
717static inline struct lock_class *
Peter Zijlstra4dfbb9d2006-10-11 01:45:14 -0400718register_lock_class(struct lockdep_map *lock, unsigned int subclass, int force)
Ingo Molnard6d897c2006-07-10 04:44:04 -0700719{
720 struct lockdep_subclass_key *key;
721 struct list_head *hash_head;
722 struct lock_class *class;
Ingo Molnar70e45062006-12-06 20:40:50 -0800723 unsigned long flags;
Ingo Molnard6d897c2006-07-10 04:44:04 -0700724
725 class = look_up_lock_class(lock, subclass);
726 if (likely(class))
Yong Zhang87cdee72011-11-09 16:07:14 +0800727 goto out_set_class_cache;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700728
729 /*
730 * Debug-check: all keys must be persistent!
731 */
732 if (!static_obj(lock->key)) {
733 debug_locks_off();
734 printk("INFO: trying to register non-static key.\n");
735 printk("the code is fine but needs lockdep annotation.\n");
736 printk("turning off the locking correctness validator.\n");
737 dump_stack();
738
739 return NULL;
740 }
741
Ingo Molnard6d897c2006-07-10 04:44:04 -0700742 key = lock->key->subkeys + subclass;
743 hash_head = classhashentry(key);
744
Ingo Molnar70e45062006-12-06 20:40:50 -0800745 raw_local_irq_save(flags);
Ingo Molnar74c383f2006-12-13 00:34:43 -0800746 if (!graph_lock()) {
747 raw_local_irq_restore(flags);
748 return NULL;
749 }
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700750 /*
751 * We have to do the hash-walk again, to avoid races
752 * with another CPU:
753 */
754 list_for_each_entry(class, hash_head, hash_entry)
755 if (class->key == key)
756 goto out_unlock_set;
757 /*
758 * Allocate a new key from the static array, and add it to
759 * the hash:
760 */
761 if (nr_lock_classes >= MAX_LOCKDEP_KEYS) {
Ingo Molnar74c383f2006-12-13 00:34:43 -0800762 if (!debug_locks_off_graph_unlock()) {
763 raw_local_irq_restore(flags);
764 return NULL;
765 }
Ingo Molnar70e45062006-12-06 20:40:50 -0800766 raw_local_irq_restore(flags);
Ingo Molnar74c383f2006-12-13 00:34:43 -0800767
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700768 printk("BUG: MAX_LOCKDEP_KEYS too low!\n");
769 printk("turning off the locking correctness validator.\n");
Peter Zijlstraeedeeab2009-03-18 12:38:47 +0100770 dump_stack();
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700771 return NULL;
772 }
773 class = lock_classes + nr_lock_classes++;
Frederic Weisbeckerbd6d29c2010-04-06 00:10:17 +0200774 debug_atomic_inc(nr_unused_locks);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700775 class->key = key;
776 class->name = lock->name;
777 class->subclass = subclass;
778 INIT_LIST_HEAD(&class->lock_entry);
779 INIT_LIST_HEAD(&class->locks_before);
780 INIT_LIST_HEAD(&class->locks_after);
781 class->name_version = count_matching_names(class);
782 /*
783 * We use RCU's safe list-add method to make
784 * parallel walking of the hash-list safe:
785 */
786 list_add_tail_rcu(&class->hash_entry, hash_head);
Dale Farnsworth14811972008-02-25 23:03:02 +0100787 /*
788 * Add it to the global list of classes:
789 */
790 list_add_tail_rcu(&class->lock_entry, &all_lock_classes);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700791
792 if (verbose(class)) {
Ingo Molnar74c383f2006-12-13 00:34:43 -0800793 graph_unlock();
Ingo Molnar70e45062006-12-06 20:40:50 -0800794 raw_local_irq_restore(flags);
Ingo Molnar74c383f2006-12-13 00:34:43 -0800795
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700796 printk("\nnew class %p: %s", class->key, class->name);
797 if (class->name_version > 1)
798 printk("#%d", class->name_version);
799 printk("\n");
800 dump_stack();
Ingo Molnar74c383f2006-12-13 00:34:43 -0800801
Ingo Molnar70e45062006-12-06 20:40:50 -0800802 raw_local_irq_save(flags);
Ingo Molnar74c383f2006-12-13 00:34:43 -0800803 if (!graph_lock()) {
804 raw_local_irq_restore(flags);
805 return NULL;
806 }
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700807 }
808out_unlock_set:
Ingo Molnar74c383f2006-12-13 00:34:43 -0800809 graph_unlock();
Ingo Molnar70e45062006-12-06 20:40:50 -0800810 raw_local_irq_restore(flags);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700811
Yong Zhang87cdee72011-11-09 16:07:14 +0800812out_set_class_cache:
Peter Zijlstra4dfbb9d2006-10-11 01:45:14 -0400813 if (!subclass || force)
Hitoshi Mitake62016252010-10-05 18:01:51 +0900814 lock->class_cache[0] = class;
815 else if (subclass < NR_LOCKDEP_CACHING_CLASSES)
816 lock->class_cache[subclass] = class;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700817
Peter Zijlstra0119fee2011-09-02 01:30:29 +0200818 /*
819 * Hash collision, did we smoke some? We found a class with a matching
820 * hash but the subclass -- which is hashed in -- didn't match.
821 */
Jarek Poplawski381a2292007-02-10 01:44:58 -0800822 if (DEBUG_LOCKS_WARN_ON(class->subclass != subclass))
823 return NULL;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700824
825 return class;
826}
827
Peter Zijlstraca58abc2007-07-19 01:48:53 -0700828#ifdef CONFIG_PROVE_LOCKING
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700829/*
Peter Zijlstra8e182572007-07-19 01:48:54 -0700830 * Allocate a lockdep entry. (assumes the graph_lock held, returns
831 * with NULL on failure)
832 */
833static struct lock_list *alloc_list_entry(void)
834{
835 if (nr_list_entries >= MAX_LOCKDEP_ENTRIES) {
836 if (!debug_locks_off_graph_unlock())
837 return NULL;
838
839 printk("BUG: MAX_LOCKDEP_ENTRIES too low!\n");
840 printk("turning off the locking correctness validator.\n");
Peter Zijlstraeedeeab2009-03-18 12:38:47 +0100841 dump_stack();
Peter Zijlstra8e182572007-07-19 01:48:54 -0700842 return NULL;
843 }
844 return list_entries + nr_list_entries++;
845}
846
847/*
848 * Add a new dependency to the head of the list:
849 */
850static int add_lock_to_list(struct lock_class *class, struct lock_class *this,
Yong Zhang4726f2a2010-05-04 14:16:48 +0800851 struct list_head *head, unsigned long ip,
852 int distance, struct stack_trace *trace)
Peter Zijlstra8e182572007-07-19 01:48:54 -0700853{
854 struct lock_list *entry;
855 /*
856 * Lock not present yet - get a new dependency struct and
857 * add it to the list:
858 */
859 entry = alloc_list_entry();
860 if (!entry)
861 return 0;
862
Zhu Yi74870172008-08-27 14:33:00 +0800863 entry->class = this;
864 entry->distance = distance;
Yong Zhang4726f2a2010-05-04 14:16:48 +0800865 entry->trace = *trace;
Peter Zijlstra8e182572007-07-19 01:48:54 -0700866 /*
867 * Since we never remove from the dependency list, the list can
868 * be walked lockless by other CPUs, it's only allocation
869 * that must be protected by the spinlock. But this also means
870 * we must make new entries visible only once writes to the
871 * entry become visible - hence the RCU op:
872 */
873 list_add_tail_rcu(&entry->entry, head);
874
875 return 1;
876}
877
Peter Zijlstra98c33ed2009-07-21 13:19:07 +0200878/*
879 * For good efficiency of modular, we use power of 2
880 */
Peter Zijlstraaf012962009-07-16 15:44:29 +0200881#define MAX_CIRCULAR_QUEUE_SIZE 4096UL
882#define CQ_MASK (MAX_CIRCULAR_QUEUE_SIZE-1)
883
Peter Zijlstra98c33ed2009-07-21 13:19:07 +0200884/*
885 * The circular_queue and helpers is used to implement the
Peter Zijlstraaf012962009-07-16 15:44:29 +0200886 * breadth-first search(BFS)algorithem, by which we can build
887 * the shortest path from the next lock to be acquired to the
888 * previous held lock if there is a circular between them.
Peter Zijlstra98c33ed2009-07-21 13:19:07 +0200889 */
Peter Zijlstraaf012962009-07-16 15:44:29 +0200890struct circular_queue {
891 unsigned long element[MAX_CIRCULAR_QUEUE_SIZE];
892 unsigned int front, rear;
893};
894
895static struct circular_queue lock_cq;
Peter Zijlstraaf012962009-07-16 15:44:29 +0200896
Ming Lei12f3dfd2009-07-16 15:44:29 +0200897unsigned int max_bfs_queue_depth;
Peter Zijlstraaf012962009-07-16 15:44:29 +0200898
Ming Leie351b662009-07-22 22:48:09 +0800899static unsigned int lockdep_dependency_gen_id;
900
Peter Zijlstraaf012962009-07-16 15:44:29 +0200901static inline void __cq_init(struct circular_queue *cq)
902{
903 cq->front = cq->rear = 0;
Ming Leie351b662009-07-22 22:48:09 +0800904 lockdep_dependency_gen_id++;
Peter Zijlstraaf012962009-07-16 15:44:29 +0200905}
906
907static inline int __cq_empty(struct circular_queue *cq)
908{
909 return (cq->front == cq->rear);
910}
911
912static inline int __cq_full(struct circular_queue *cq)
913{
914 return ((cq->rear + 1) & CQ_MASK) == cq->front;
915}
916
917static inline int __cq_enqueue(struct circular_queue *cq, unsigned long elem)
918{
919 if (__cq_full(cq))
920 return -1;
921
922 cq->element[cq->rear] = elem;
923 cq->rear = (cq->rear + 1) & CQ_MASK;
924 return 0;
925}
926
927static inline int __cq_dequeue(struct circular_queue *cq, unsigned long *elem)
928{
929 if (__cq_empty(cq))
930 return -1;
931
932 *elem = cq->element[cq->front];
933 cq->front = (cq->front + 1) & CQ_MASK;
934 return 0;
935}
936
937static inline unsigned int __cq_get_elem_count(struct circular_queue *cq)
938{
939 return (cq->rear - cq->front) & CQ_MASK;
940}
941
942static inline void mark_lock_accessed(struct lock_list *lock,
943 struct lock_list *parent)
944{
945 unsigned long nr;
Peter Zijlstra98c33ed2009-07-21 13:19:07 +0200946
Peter Zijlstraaf012962009-07-16 15:44:29 +0200947 nr = lock - list_entries;
Peter Zijlstra0119fee2011-09-02 01:30:29 +0200948 WARN_ON(nr >= nr_list_entries); /* Out-of-bounds, input fail */
Peter Zijlstraaf012962009-07-16 15:44:29 +0200949 lock->parent = parent;
Ming Leie351b662009-07-22 22:48:09 +0800950 lock->class->dep_gen_id = lockdep_dependency_gen_id;
Peter Zijlstraaf012962009-07-16 15:44:29 +0200951}
952
953static inline unsigned long lock_accessed(struct lock_list *lock)
954{
955 unsigned long nr;
Peter Zijlstra98c33ed2009-07-21 13:19:07 +0200956
Peter Zijlstraaf012962009-07-16 15:44:29 +0200957 nr = lock - list_entries;
Peter Zijlstra0119fee2011-09-02 01:30:29 +0200958 WARN_ON(nr >= nr_list_entries); /* Out-of-bounds, input fail */
Ming Leie351b662009-07-22 22:48:09 +0800959 return lock->class->dep_gen_id == lockdep_dependency_gen_id;
Peter Zijlstraaf012962009-07-16 15:44:29 +0200960}
961
962static inline struct lock_list *get_lock_parent(struct lock_list *child)
963{
964 return child->parent;
965}
966
967static inline int get_lock_depth(struct lock_list *child)
968{
969 int depth = 0;
970 struct lock_list *parent;
971
972 while ((parent = get_lock_parent(child))) {
973 child = parent;
974 depth++;
975 }
976 return depth;
977}
978
Ming Lei9e2d5512009-07-16 15:44:29 +0200979static int __bfs(struct lock_list *source_entry,
Peter Zijlstraaf012962009-07-16 15:44:29 +0200980 void *data,
981 int (*match)(struct lock_list *entry, void *data),
982 struct lock_list **target_entry,
983 int forward)
Ming Leic94aa5c2009-07-16 15:44:29 +0200984{
985 struct lock_list *entry;
Ming Leid588e462009-07-16 15:44:29 +0200986 struct list_head *head;
Ming Leic94aa5c2009-07-16 15:44:29 +0200987 struct circular_queue *cq = &lock_cq;
988 int ret = 1;
989
Ming Lei9e2d5512009-07-16 15:44:29 +0200990 if (match(source_entry, data)) {
Ming Leic94aa5c2009-07-16 15:44:29 +0200991 *target_entry = source_entry;
992 ret = 0;
993 goto exit;
994 }
995
Ming Leid588e462009-07-16 15:44:29 +0200996 if (forward)
997 head = &source_entry->class->locks_after;
998 else
999 head = &source_entry->class->locks_before;
1000
1001 if (list_empty(head))
1002 goto exit;
1003
1004 __cq_init(cq);
Ming Leic94aa5c2009-07-16 15:44:29 +02001005 __cq_enqueue(cq, (unsigned long)source_entry);
1006
1007 while (!__cq_empty(cq)) {
1008 struct lock_list *lock;
Ming Leic94aa5c2009-07-16 15:44:29 +02001009
1010 __cq_dequeue(cq, (unsigned long *)&lock);
1011
1012 if (!lock->class) {
1013 ret = -2;
1014 goto exit;
1015 }
1016
1017 if (forward)
1018 head = &lock->class->locks_after;
1019 else
1020 head = &lock->class->locks_before;
1021
1022 list_for_each_entry(entry, head, entry) {
1023 if (!lock_accessed(entry)) {
Ming Lei12f3dfd2009-07-16 15:44:29 +02001024 unsigned int cq_depth;
Ming Leic94aa5c2009-07-16 15:44:29 +02001025 mark_lock_accessed(entry, lock);
Ming Lei9e2d5512009-07-16 15:44:29 +02001026 if (match(entry, data)) {
Ming Leic94aa5c2009-07-16 15:44:29 +02001027 *target_entry = entry;
1028 ret = 0;
1029 goto exit;
1030 }
1031
1032 if (__cq_enqueue(cq, (unsigned long)entry)) {
1033 ret = -1;
1034 goto exit;
1035 }
Ming Lei12f3dfd2009-07-16 15:44:29 +02001036 cq_depth = __cq_get_elem_count(cq);
1037 if (max_bfs_queue_depth < cq_depth)
1038 max_bfs_queue_depth = cq_depth;
Ming Leic94aa5c2009-07-16 15:44:29 +02001039 }
1040 }
1041 }
1042exit:
1043 return ret;
1044}
1045
Ming Leid7aaba12009-07-16 15:44:29 +02001046static inline int __bfs_forwards(struct lock_list *src_entry,
Ming Lei9e2d5512009-07-16 15:44:29 +02001047 void *data,
1048 int (*match)(struct lock_list *entry, void *data),
1049 struct lock_list **target_entry)
Ming Leic94aa5c2009-07-16 15:44:29 +02001050{
Ming Lei9e2d5512009-07-16 15:44:29 +02001051 return __bfs(src_entry, data, match, target_entry, 1);
Ming Leic94aa5c2009-07-16 15:44:29 +02001052
1053}
1054
Ming Leid7aaba12009-07-16 15:44:29 +02001055static inline int __bfs_backwards(struct lock_list *src_entry,
Ming Lei9e2d5512009-07-16 15:44:29 +02001056 void *data,
1057 int (*match)(struct lock_list *entry, void *data),
1058 struct lock_list **target_entry)
Ming Leic94aa5c2009-07-16 15:44:29 +02001059{
Ming Lei9e2d5512009-07-16 15:44:29 +02001060 return __bfs(src_entry, data, match, target_entry, 0);
Ming Leic94aa5c2009-07-16 15:44:29 +02001061
1062}
1063
Peter Zijlstra8e182572007-07-19 01:48:54 -07001064/*
1065 * Recursive, forwards-direction lock-dependency checking, used for
1066 * both noncyclic checking and for hardirq-unsafe/softirq-unsafe
1067 * checking.
Peter Zijlstra8e182572007-07-19 01:48:54 -07001068 */
Peter Zijlstra8e182572007-07-19 01:48:54 -07001069
1070/*
1071 * Print a dependency chain entry (this is only done when a deadlock
1072 * has been detected):
1073 */
1074static noinline int
Ming Lei24208ca2009-07-16 15:44:29 +02001075print_circular_bug_entry(struct lock_list *target, int depth)
Peter Zijlstra8e182572007-07-19 01:48:54 -07001076{
1077 if (debug_locks_silent)
1078 return 0;
1079 printk("\n-> #%u", depth);
1080 print_lock_name(target->class);
1081 printk(":\n");
1082 print_stack_trace(&target->trace, 6);
1083
1084 return 0;
1085}
1086
Steven Rostedtf4185812011-04-20 21:41:55 -04001087static void
1088print_circular_lock_scenario(struct held_lock *src,
1089 struct held_lock *tgt,
1090 struct lock_list *prt)
1091{
1092 struct lock_class *source = hlock_class(src);
1093 struct lock_class *target = hlock_class(tgt);
1094 struct lock_class *parent = prt->class;
1095
1096 /*
1097 * A direct locking problem where unsafe_class lock is taken
1098 * directly by safe_class lock, then all we need to show
1099 * is the deadlock scenario, as it is obvious that the
1100 * unsafe lock is taken under the safe lock.
1101 *
1102 * But if there is a chain instead, where the safe lock takes
1103 * an intermediate lock (middle_class) where this lock is
1104 * not the same as the safe lock, then the lock chain is
1105 * used to describe the problem. Otherwise we would need
1106 * to show a different CPU case for each link in the chain
1107 * from the safe_class lock to the unsafe_class lock.
1108 */
1109 if (parent != source) {
1110 printk("Chain exists of:\n ");
1111 __print_lock_name(source);
1112 printk(" --> ");
1113 __print_lock_name(parent);
1114 printk(" --> ");
1115 __print_lock_name(target);
1116 printk("\n\n");
1117 }
1118
1119 printk(" Possible unsafe locking scenario:\n\n");
1120 printk(" CPU0 CPU1\n");
1121 printk(" ---- ----\n");
1122 printk(" lock(");
1123 __print_lock_name(target);
1124 printk(");\n");
1125 printk(" lock(");
1126 __print_lock_name(parent);
1127 printk(");\n");
1128 printk(" lock(");
1129 __print_lock_name(target);
1130 printk(");\n");
1131 printk(" lock(");
1132 __print_lock_name(source);
1133 printk(");\n");
1134 printk("\n *** DEADLOCK ***\n\n");
1135}
1136
Peter Zijlstra8e182572007-07-19 01:48:54 -07001137/*
1138 * When a circular dependency is detected, print the
1139 * header first:
1140 */
1141static noinline int
Ming Leidb0002a2009-07-16 15:44:29 +02001142print_circular_bug_header(struct lock_list *entry, unsigned int depth,
1143 struct held_lock *check_src,
1144 struct held_lock *check_tgt)
Peter Zijlstra8e182572007-07-19 01:48:54 -07001145{
1146 struct task_struct *curr = current;
1147
Ming Leic94aa5c2009-07-16 15:44:29 +02001148 if (debug_locks_silent)
Peter Zijlstra8e182572007-07-19 01:48:54 -07001149 return 0;
1150
Paul E. McKenneyb3fbab02011-05-24 08:31:09 -07001151 printk("\n");
1152 printk("======================================================\n");
1153 printk("[ INFO: possible circular locking dependency detected ]\n");
Ben Hutchingsfbdc4b92011-10-28 04:36:55 +01001154 print_kernel_ident();
Paul E. McKenneyb3fbab02011-05-24 08:31:09 -07001155 printk("-------------------------------------------------------\n");
Peter Zijlstra8e182572007-07-19 01:48:54 -07001156 printk("%s/%d is trying to acquire lock:\n",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07001157 curr->comm, task_pid_nr(curr));
Ming Leidb0002a2009-07-16 15:44:29 +02001158 print_lock(check_src);
Peter Zijlstra8e182572007-07-19 01:48:54 -07001159 printk("\nbut task is already holding lock:\n");
Ming Leidb0002a2009-07-16 15:44:29 +02001160 print_lock(check_tgt);
Peter Zijlstra8e182572007-07-19 01:48:54 -07001161 printk("\nwhich lock already depends on the new lock.\n\n");
1162 printk("\nthe existing dependency chain (in reverse order) is:\n");
1163
1164 print_circular_bug_entry(entry, depth);
1165
1166 return 0;
1167}
1168
Ming Lei9e2d5512009-07-16 15:44:29 +02001169static inline int class_equal(struct lock_list *entry, void *data)
1170{
1171 return entry->class == data;
1172}
1173
Ming Leidb0002a2009-07-16 15:44:29 +02001174static noinline int print_circular_bug(struct lock_list *this,
1175 struct lock_list *target,
1176 struct held_lock *check_src,
1177 struct held_lock *check_tgt)
Peter Zijlstra8e182572007-07-19 01:48:54 -07001178{
1179 struct task_struct *curr = current;
Ming Leic94aa5c2009-07-16 15:44:29 +02001180 struct lock_list *parent;
Steven Rostedtf4185812011-04-20 21:41:55 -04001181 struct lock_list *first_parent;
Ming Lei24208ca2009-07-16 15:44:29 +02001182 int depth;
Peter Zijlstra8e182572007-07-19 01:48:54 -07001183
Ming Leic94aa5c2009-07-16 15:44:29 +02001184 if (!debug_locks_off_graph_unlock() || debug_locks_silent)
Peter Zijlstra8e182572007-07-19 01:48:54 -07001185 return 0;
1186
Ming Leidb0002a2009-07-16 15:44:29 +02001187 if (!save_trace(&this->trace))
Peter Zijlstra8e182572007-07-19 01:48:54 -07001188 return 0;
1189
Ming Leic94aa5c2009-07-16 15:44:29 +02001190 depth = get_lock_depth(target);
1191
Ming Leidb0002a2009-07-16 15:44:29 +02001192 print_circular_bug_header(target, depth, check_src, check_tgt);
Ming Leic94aa5c2009-07-16 15:44:29 +02001193
1194 parent = get_lock_parent(target);
Steven Rostedtf4185812011-04-20 21:41:55 -04001195 first_parent = parent;
Ming Leic94aa5c2009-07-16 15:44:29 +02001196
1197 while (parent) {
1198 print_circular_bug_entry(parent, --depth);
1199 parent = get_lock_parent(parent);
1200 }
Peter Zijlstra8e182572007-07-19 01:48:54 -07001201
1202 printk("\nother info that might help us debug this:\n\n");
Steven Rostedtf4185812011-04-20 21:41:55 -04001203 print_circular_lock_scenario(check_src, check_tgt,
1204 first_parent);
1205
Peter Zijlstra8e182572007-07-19 01:48:54 -07001206 lockdep_print_held_locks(curr);
1207
1208 printk("\nstack backtrace:\n");
1209 dump_stack();
1210
1211 return 0;
1212}
1213
Ming Leidb0002a2009-07-16 15:44:29 +02001214static noinline int print_bfs_bug(int ret)
1215{
1216 if (!debug_locks_off_graph_unlock())
1217 return 0;
1218
Peter Zijlstra0119fee2011-09-02 01:30:29 +02001219 /*
1220 * Breadth-first-search failed, graph got corrupted?
1221 */
Ming Leidb0002a2009-07-16 15:44:29 +02001222 WARN(1, "lockdep bfs error:%d\n", ret);
1223
1224 return 0;
1225}
1226
Ming Leief681022009-07-16 15:44:29 +02001227static int noop_count(struct lock_list *entry, void *data)
David Miller419ca3f2008-07-29 21:45:03 -07001228{
Ming Leief681022009-07-16 15:44:29 +02001229 (*(unsigned long *)data)++;
1230 return 0;
David Miller419ca3f2008-07-29 21:45:03 -07001231}
1232
Ming Leief681022009-07-16 15:44:29 +02001233unsigned long __lockdep_count_forward_deps(struct lock_list *this)
1234{
1235 unsigned long count = 0;
1236 struct lock_list *uninitialized_var(target_entry);
1237
1238 __bfs_forwards(this, (void *)&count, noop_count, &target_entry);
1239
1240 return count;
1241}
David Miller419ca3f2008-07-29 21:45:03 -07001242unsigned long lockdep_count_forward_deps(struct lock_class *class)
1243{
1244 unsigned long ret, flags;
Ming Leief681022009-07-16 15:44:29 +02001245 struct lock_list this;
1246
1247 this.parent = NULL;
1248 this.class = class;
David Miller419ca3f2008-07-29 21:45:03 -07001249
1250 local_irq_save(flags);
Thomas Gleixner0199c4e2009-12-02 20:01:25 +01001251 arch_spin_lock(&lockdep_lock);
Ming Leief681022009-07-16 15:44:29 +02001252 ret = __lockdep_count_forward_deps(&this);
Thomas Gleixner0199c4e2009-12-02 20:01:25 +01001253 arch_spin_unlock(&lockdep_lock);
David Miller419ca3f2008-07-29 21:45:03 -07001254 local_irq_restore(flags);
1255
1256 return ret;
1257}
1258
Ming Leief681022009-07-16 15:44:29 +02001259unsigned long __lockdep_count_backward_deps(struct lock_list *this)
David Miller419ca3f2008-07-29 21:45:03 -07001260{
Ming Leief681022009-07-16 15:44:29 +02001261 unsigned long count = 0;
1262 struct lock_list *uninitialized_var(target_entry);
David Miller419ca3f2008-07-29 21:45:03 -07001263
Ming Leief681022009-07-16 15:44:29 +02001264 __bfs_backwards(this, (void *)&count, noop_count, &target_entry);
David Miller419ca3f2008-07-29 21:45:03 -07001265
Ming Leief681022009-07-16 15:44:29 +02001266 return count;
David Miller419ca3f2008-07-29 21:45:03 -07001267}
1268
1269unsigned long lockdep_count_backward_deps(struct lock_class *class)
1270{
1271 unsigned long ret, flags;
Ming Leief681022009-07-16 15:44:29 +02001272 struct lock_list this;
1273
1274 this.parent = NULL;
1275 this.class = class;
David Miller419ca3f2008-07-29 21:45:03 -07001276
1277 local_irq_save(flags);
Thomas Gleixner0199c4e2009-12-02 20:01:25 +01001278 arch_spin_lock(&lockdep_lock);
Ming Leief681022009-07-16 15:44:29 +02001279 ret = __lockdep_count_backward_deps(&this);
Thomas Gleixner0199c4e2009-12-02 20:01:25 +01001280 arch_spin_unlock(&lockdep_lock);
David Miller419ca3f2008-07-29 21:45:03 -07001281 local_irq_restore(flags);
1282
1283 return ret;
1284}
1285
Peter Zijlstra8e182572007-07-19 01:48:54 -07001286/*
1287 * Prove that the dependency graph starting at <entry> can not
1288 * lead to <target>. Print an error and return 0 if it does.
1289 */
1290static noinline int
Ming Leidb0002a2009-07-16 15:44:29 +02001291check_noncircular(struct lock_list *root, struct lock_class *target,
1292 struct lock_list **target_entry)
Peter Zijlstra8e182572007-07-19 01:48:54 -07001293{
Ming Leidb0002a2009-07-16 15:44:29 +02001294 int result;
Peter Zijlstra8e182572007-07-19 01:48:54 -07001295
Frederic Weisbeckerbd6d29c2010-04-06 00:10:17 +02001296 debug_atomic_inc(nr_cyclic_checks);
David Miller419ca3f2008-07-29 21:45:03 -07001297
Ming Leid7aaba12009-07-16 15:44:29 +02001298 result = __bfs_forwards(root, target, class_equal, target_entry);
Ming Leidb0002a2009-07-16 15:44:29 +02001299
1300 return result;
Peter Zijlstra8e182572007-07-19 01:48:54 -07001301}
1302
Steven Rostedt81d68a92008-05-12 21:20:42 +02001303#if defined(CONFIG_TRACE_IRQFLAGS) && defined(CONFIG_PROVE_LOCKING)
Peter Zijlstra8e182572007-07-19 01:48:54 -07001304/*
1305 * Forwards and backwards subgraph searching, for the purposes of
1306 * proving that two subgraphs can be connected by a new dependency
1307 * without creating any illegal irq-safe -> irq-unsafe lock dependency.
1308 */
Peter Zijlstra8e182572007-07-19 01:48:54 -07001309
Ming Leid7aaba12009-07-16 15:44:29 +02001310static inline int usage_match(struct lock_list *entry, void *bit)
1311{
1312 return entry->class->usage_mask & (1 << (enum lock_usage_bit)bit);
1313}
1314
1315
1316
Peter Zijlstra8e182572007-07-19 01:48:54 -07001317/*
1318 * Find a node in the forwards-direction dependency sub-graph starting
Ming Leid7aaba12009-07-16 15:44:29 +02001319 * at @root->class that matches @bit.
Peter Zijlstra8e182572007-07-19 01:48:54 -07001320 *
Ming Leid7aaba12009-07-16 15:44:29 +02001321 * Return 0 if such a node exists in the subgraph, and put that node
1322 * into *@target_entry.
Peter Zijlstra8e182572007-07-19 01:48:54 -07001323 *
Ming Leid7aaba12009-07-16 15:44:29 +02001324 * Return 1 otherwise and keep *@target_entry unchanged.
1325 * Return <0 on error.
Peter Zijlstra8e182572007-07-19 01:48:54 -07001326 */
Ming Leid7aaba12009-07-16 15:44:29 +02001327static int
1328find_usage_forwards(struct lock_list *root, enum lock_usage_bit bit,
1329 struct lock_list **target_entry)
Peter Zijlstra8e182572007-07-19 01:48:54 -07001330{
Ming Leid7aaba12009-07-16 15:44:29 +02001331 int result;
Peter Zijlstra8e182572007-07-19 01:48:54 -07001332
Frederic Weisbeckerbd6d29c2010-04-06 00:10:17 +02001333 debug_atomic_inc(nr_find_usage_forwards_checks);
Peter Zijlstra8e182572007-07-19 01:48:54 -07001334
Ming Leid7aaba12009-07-16 15:44:29 +02001335 result = __bfs_forwards(root, (void *)bit, usage_match, target_entry);
1336
1337 return result;
Peter Zijlstra8e182572007-07-19 01:48:54 -07001338}
1339
1340/*
1341 * Find a node in the backwards-direction dependency sub-graph starting
Ming Leid7aaba12009-07-16 15:44:29 +02001342 * at @root->class that matches @bit.
Peter Zijlstra8e182572007-07-19 01:48:54 -07001343 *
Ming Leid7aaba12009-07-16 15:44:29 +02001344 * Return 0 if such a node exists in the subgraph, and put that node
1345 * into *@target_entry.
Peter Zijlstra8e182572007-07-19 01:48:54 -07001346 *
Ming Leid7aaba12009-07-16 15:44:29 +02001347 * Return 1 otherwise and keep *@target_entry unchanged.
1348 * Return <0 on error.
Peter Zijlstra8e182572007-07-19 01:48:54 -07001349 */
Ming Leid7aaba12009-07-16 15:44:29 +02001350static int
1351find_usage_backwards(struct lock_list *root, enum lock_usage_bit bit,
1352 struct lock_list **target_entry)
Peter Zijlstra8e182572007-07-19 01:48:54 -07001353{
Ming Leid7aaba12009-07-16 15:44:29 +02001354 int result;
Peter Zijlstra8e182572007-07-19 01:48:54 -07001355
Frederic Weisbeckerbd6d29c2010-04-06 00:10:17 +02001356 debug_atomic_inc(nr_find_usage_backwards_checks);
Peter Zijlstra8e182572007-07-19 01:48:54 -07001357
Ming Leid7aaba12009-07-16 15:44:29 +02001358 result = __bfs_backwards(root, (void *)bit, usage_match, target_entry);
Dave Jonesf82b2172008-08-11 09:30:23 +02001359
Ming Leid7aaba12009-07-16 15:44:29 +02001360 return result;
Peter Zijlstra8e182572007-07-19 01:48:54 -07001361}
1362
Peter Zijlstraaf012962009-07-16 15:44:29 +02001363static void print_lock_class_header(struct lock_class *class, int depth)
1364{
1365 int bit;
1366
1367 printk("%*s->", depth, "");
1368 print_lock_name(class);
1369 printk(" ops: %lu", class->ops);
1370 printk(" {\n");
1371
1372 for (bit = 0; bit < LOCK_USAGE_STATES; bit++) {
1373 if (class->usage_mask & (1 << bit)) {
1374 int len = depth;
1375
1376 len += printk("%*s %s", depth, "", usage_str[bit]);
1377 len += printk(" at:\n");
1378 print_stack_trace(class->usage_traces + bit, len);
1379 }
1380 }
1381 printk("%*s }\n", depth, "");
1382
1383 printk("%*s ... key at: ",depth,"");
1384 print_ip_sym((unsigned long)class->key);
1385}
1386
1387/*
1388 * printk the shortest lock dependencies from @start to @end in reverse order:
1389 */
1390static void __used
1391print_shortest_lock_dependencies(struct lock_list *leaf,
1392 struct lock_list *root)
1393{
1394 struct lock_list *entry = leaf;
1395 int depth;
1396
1397 /*compute depth from generated tree by BFS*/
1398 depth = get_lock_depth(leaf);
1399
1400 do {
1401 print_lock_class_header(entry->class, depth);
1402 printk("%*s ... acquired at:\n", depth, "");
1403 print_stack_trace(&entry->trace, 2);
1404 printk("\n");
1405
1406 if (depth == 0 && (entry != root)) {
Steven Rostedt6be8c392011-04-20 21:41:58 -04001407 printk("lockdep:%s bad path found in chain graph\n", __func__);
Peter Zijlstraaf012962009-07-16 15:44:29 +02001408 break;
1409 }
1410
1411 entry = get_lock_parent(entry);
1412 depth--;
1413 } while (entry && (depth >= 0));
1414
1415 return;
1416}
Ming Leid7aaba12009-07-16 15:44:29 +02001417
Steven Rostedt3003eba2011-04-20 21:41:54 -04001418static void
1419print_irq_lock_scenario(struct lock_list *safe_entry,
1420 struct lock_list *unsafe_entry,
Steven Rostedtdad3d742011-04-20 21:41:57 -04001421 struct lock_class *prev_class,
1422 struct lock_class *next_class)
Steven Rostedt3003eba2011-04-20 21:41:54 -04001423{
1424 struct lock_class *safe_class = safe_entry->class;
1425 struct lock_class *unsafe_class = unsafe_entry->class;
Steven Rostedtdad3d742011-04-20 21:41:57 -04001426 struct lock_class *middle_class = prev_class;
Steven Rostedt3003eba2011-04-20 21:41:54 -04001427
1428 if (middle_class == safe_class)
Steven Rostedtdad3d742011-04-20 21:41:57 -04001429 middle_class = next_class;
Steven Rostedt3003eba2011-04-20 21:41:54 -04001430
1431 /*
1432 * A direct locking problem where unsafe_class lock is taken
1433 * directly by safe_class lock, then all we need to show
1434 * is the deadlock scenario, as it is obvious that the
1435 * unsafe lock is taken under the safe lock.
1436 *
1437 * But if there is a chain instead, where the safe lock takes
1438 * an intermediate lock (middle_class) where this lock is
1439 * not the same as the safe lock, then the lock chain is
1440 * used to describe the problem. Otherwise we would need
1441 * to show a different CPU case for each link in the chain
1442 * from the safe_class lock to the unsafe_class lock.
1443 */
1444 if (middle_class != unsafe_class) {
1445 printk("Chain exists of:\n ");
1446 __print_lock_name(safe_class);
1447 printk(" --> ");
1448 __print_lock_name(middle_class);
1449 printk(" --> ");
1450 __print_lock_name(unsafe_class);
1451 printk("\n\n");
1452 }
1453
1454 printk(" Possible interrupt unsafe locking scenario:\n\n");
1455 printk(" CPU0 CPU1\n");
1456 printk(" ---- ----\n");
1457 printk(" lock(");
1458 __print_lock_name(unsafe_class);
1459 printk(");\n");
1460 printk(" local_irq_disable();\n");
1461 printk(" lock(");
1462 __print_lock_name(safe_class);
1463 printk(");\n");
1464 printk(" lock(");
1465 __print_lock_name(middle_class);
1466 printk(");\n");
1467 printk(" <Interrupt>\n");
1468 printk(" lock(");
1469 __print_lock_name(safe_class);
1470 printk(");\n");
1471 printk("\n *** DEADLOCK ***\n\n");
1472}
1473
Peter Zijlstra8e182572007-07-19 01:48:54 -07001474static int
1475print_bad_irq_dependency(struct task_struct *curr,
Ming Lei24208ca2009-07-16 15:44:29 +02001476 struct lock_list *prev_root,
1477 struct lock_list *next_root,
1478 struct lock_list *backwards_entry,
1479 struct lock_list *forwards_entry,
Peter Zijlstra8e182572007-07-19 01:48:54 -07001480 struct held_lock *prev,
1481 struct held_lock *next,
1482 enum lock_usage_bit bit1,
1483 enum lock_usage_bit bit2,
1484 const char *irqclass)
1485{
1486 if (!debug_locks_off_graph_unlock() || debug_locks_silent)
1487 return 0;
1488
Paul E. McKenneyb3fbab02011-05-24 08:31:09 -07001489 printk("\n");
1490 printk("======================================================\n");
1491 printk("[ INFO: %s-safe -> %s-unsafe lock order detected ]\n",
Peter Zijlstra8e182572007-07-19 01:48:54 -07001492 irqclass, irqclass);
Ben Hutchingsfbdc4b92011-10-28 04:36:55 +01001493 print_kernel_ident();
Paul E. McKenneyb3fbab02011-05-24 08:31:09 -07001494 printk("------------------------------------------------------\n");
Peter Zijlstra8e182572007-07-19 01:48:54 -07001495 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 -07001496 curr->comm, task_pid_nr(curr),
Peter Zijlstra8e182572007-07-19 01:48:54 -07001497 curr->hardirq_context, hardirq_count() >> HARDIRQ_SHIFT,
1498 curr->softirq_context, softirq_count() >> SOFTIRQ_SHIFT,
1499 curr->hardirqs_enabled,
1500 curr->softirqs_enabled);
1501 print_lock(next);
1502
1503 printk("\nand this task is already holding:\n");
1504 print_lock(prev);
1505 printk("which would create a new lock dependency:\n");
Dave Jonesf82b2172008-08-11 09:30:23 +02001506 print_lock_name(hlock_class(prev));
Peter Zijlstra8e182572007-07-19 01:48:54 -07001507 printk(" ->");
Dave Jonesf82b2172008-08-11 09:30:23 +02001508 print_lock_name(hlock_class(next));
Peter Zijlstra8e182572007-07-19 01:48:54 -07001509 printk("\n");
1510
1511 printk("\nbut this new dependency connects a %s-irq-safe lock:\n",
1512 irqclass);
Ming Lei24208ca2009-07-16 15:44:29 +02001513 print_lock_name(backwards_entry->class);
Peter Zijlstra8e182572007-07-19 01:48:54 -07001514 printk("\n... which became %s-irq-safe at:\n", irqclass);
1515
Ming Lei24208ca2009-07-16 15:44:29 +02001516 print_stack_trace(backwards_entry->class->usage_traces + bit1, 1);
Peter Zijlstra8e182572007-07-19 01:48:54 -07001517
1518 printk("\nto a %s-irq-unsafe lock:\n", irqclass);
Ming Lei24208ca2009-07-16 15:44:29 +02001519 print_lock_name(forwards_entry->class);
Peter Zijlstra8e182572007-07-19 01:48:54 -07001520 printk("\n... which became %s-irq-unsafe at:\n", irqclass);
1521 printk("...");
1522
Ming Lei24208ca2009-07-16 15:44:29 +02001523 print_stack_trace(forwards_entry->class->usage_traces + bit2, 1);
Peter Zijlstra8e182572007-07-19 01:48:54 -07001524
1525 printk("\nother info that might help us debug this:\n\n");
Steven Rostedtdad3d742011-04-20 21:41:57 -04001526 print_irq_lock_scenario(backwards_entry, forwards_entry,
1527 hlock_class(prev), hlock_class(next));
Steven Rostedt3003eba2011-04-20 21:41:54 -04001528
Peter Zijlstra8e182572007-07-19 01:48:54 -07001529 lockdep_print_held_locks(curr);
1530
Ming Lei24208ca2009-07-16 15:44:29 +02001531 printk("\nthe dependencies between %s-irq-safe lock", irqclass);
1532 printk(" and the holding lock:\n");
1533 if (!save_trace(&prev_root->trace))
1534 return 0;
1535 print_shortest_lock_dependencies(backwards_entry, prev_root);
Peter Zijlstra8e182572007-07-19 01:48:54 -07001536
Ming Lei24208ca2009-07-16 15:44:29 +02001537 printk("\nthe dependencies between the lock to be acquired");
1538 printk(" and %s-irq-unsafe lock:\n", irqclass);
1539 if (!save_trace(&next_root->trace))
1540 return 0;
1541 print_shortest_lock_dependencies(forwards_entry, next_root);
Peter Zijlstra8e182572007-07-19 01:48:54 -07001542
1543 printk("\nstack backtrace:\n");
1544 dump_stack();
1545
1546 return 0;
1547}
1548
1549static int
1550check_usage(struct task_struct *curr, struct held_lock *prev,
1551 struct held_lock *next, enum lock_usage_bit bit_backwards,
1552 enum lock_usage_bit bit_forwards, const char *irqclass)
1553{
1554 int ret;
Ming Lei24208ca2009-07-16 15:44:29 +02001555 struct lock_list this, that;
Ming Leid7aaba12009-07-16 15:44:29 +02001556 struct lock_list *uninitialized_var(target_entry);
Ming Lei24208ca2009-07-16 15:44:29 +02001557 struct lock_list *uninitialized_var(target_entry1);
Peter Zijlstra8e182572007-07-19 01:48:54 -07001558
Ming Leid7aaba12009-07-16 15:44:29 +02001559 this.parent = NULL;
Peter Zijlstra8e182572007-07-19 01:48:54 -07001560
Ming Leid7aaba12009-07-16 15:44:29 +02001561 this.class = hlock_class(prev);
1562 ret = find_usage_backwards(&this, bit_backwards, &target_entry);
Peter Zijlstraaf012962009-07-16 15:44:29 +02001563 if (ret < 0)
1564 return print_bfs_bug(ret);
1565 if (ret == 1)
1566 return ret;
Ming Leid7aaba12009-07-16 15:44:29 +02001567
Ming Lei24208ca2009-07-16 15:44:29 +02001568 that.parent = NULL;
1569 that.class = hlock_class(next);
1570 ret = find_usage_forwards(&that, bit_forwards, &target_entry1);
Peter Zijlstraaf012962009-07-16 15:44:29 +02001571 if (ret < 0)
1572 return print_bfs_bug(ret);
1573 if (ret == 1)
1574 return ret;
Ming Leid7aaba12009-07-16 15:44:29 +02001575
Ming Lei24208ca2009-07-16 15:44:29 +02001576 return print_bad_irq_dependency(curr, &this, &that,
1577 target_entry, target_entry1,
1578 prev, next,
Peter Zijlstra8e182572007-07-19 01:48:54 -07001579 bit_backwards, bit_forwards, irqclass);
1580}
1581
Peter Zijlstra4f367d8a2009-01-22 18:10:42 +01001582static const char *state_names[] = {
1583#define LOCKDEP_STATE(__STATE) \
Peter Zijlstrab4b136f2009-01-29 14:50:36 +01001584 __stringify(__STATE),
Peter Zijlstra4f367d8a2009-01-22 18:10:42 +01001585#include "lockdep_states.h"
1586#undef LOCKDEP_STATE
1587};
1588
1589static const char *state_rnames[] = {
1590#define LOCKDEP_STATE(__STATE) \
Peter Zijlstrab4b136f2009-01-29 14:50:36 +01001591 __stringify(__STATE)"-READ",
Peter Zijlstra4f367d8a2009-01-22 18:10:42 +01001592#include "lockdep_states.h"
1593#undef LOCKDEP_STATE
1594};
1595
1596static inline const char *state_name(enum lock_usage_bit bit)
1597{
1598 return (bit & 1) ? state_rnames[bit >> 2] : state_names[bit >> 2];
1599}
1600
1601static int exclusive_bit(int new_bit)
1602{
1603 /*
1604 * USED_IN
1605 * USED_IN_READ
1606 * ENABLED
1607 * ENABLED_READ
1608 *
1609 * bit 0 - write/read
1610 * bit 1 - used_in/enabled
1611 * bit 2+ state
1612 */
1613
1614 int state = new_bit & ~3;
1615 int dir = new_bit & 2;
1616
1617 /*
1618 * keep state, bit flip the direction and strip read.
1619 */
1620 return state | (dir ^ 2);
1621}
1622
1623static int check_irq_usage(struct task_struct *curr, struct held_lock *prev,
1624 struct held_lock *next, enum lock_usage_bit bit)
Peter Zijlstra8e182572007-07-19 01:48:54 -07001625{
1626 /*
1627 * Prove that the new dependency does not connect a hardirq-safe
1628 * lock with a hardirq-unsafe lock - to achieve this we search
1629 * the backwards-subgraph starting at <prev>, and the
1630 * forwards-subgraph starting at <next>:
1631 */
Peter Zijlstra4f367d8a2009-01-22 18:10:42 +01001632 if (!check_usage(curr, prev, next, bit,
1633 exclusive_bit(bit), state_name(bit)))
Peter Zijlstra8e182572007-07-19 01:48:54 -07001634 return 0;
1635
Peter Zijlstra4f367d8a2009-01-22 18:10:42 +01001636 bit++; /* _READ */
1637
Peter Zijlstra8e182572007-07-19 01:48:54 -07001638 /*
1639 * Prove that the new dependency does not connect a hardirq-safe-read
1640 * lock with a hardirq-unsafe lock - to achieve this we search
1641 * the backwards-subgraph starting at <prev>, and the
1642 * forwards-subgraph starting at <next>:
1643 */
Peter Zijlstra4f367d8a2009-01-22 18:10:42 +01001644 if (!check_usage(curr, prev, next, bit,
1645 exclusive_bit(bit), state_name(bit)))
Peter Zijlstra8e182572007-07-19 01:48:54 -07001646 return 0;
1647
Peter Zijlstra4f367d8a2009-01-22 18:10:42 +01001648 return 1;
1649}
Peter Zijlstra8e182572007-07-19 01:48:54 -07001650
Peter Zijlstra4f367d8a2009-01-22 18:10:42 +01001651static int
1652check_prev_add_irq(struct task_struct *curr, struct held_lock *prev,
1653 struct held_lock *next)
1654{
1655#define LOCKDEP_STATE(__STATE) \
1656 if (!check_irq_usage(curr, prev, next, LOCK_USED_IN_##__STATE)) \
Nick Piggincf40bd12009-01-21 08:12:39 +01001657 return 0;
Peter Zijlstra4f367d8a2009-01-22 18:10:42 +01001658#include "lockdep_states.h"
1659#undef LOCKDEP_STATE
Nick Piggincf40bd12009-01-21 08:12:39 +01001660
Peter Zijlstra8e182572007-07-19 01:48:54 -07001661 return 1;
1662}
1663
1664static void inc_chains(void)
1665{
1666 if (current->hardirq_context)
1667 nr_hardirq_chains++;
1668 else {
1669 if (current->softirq_context)
1670 nr_softirq_chains++;
1671 else
1672 nr_process_chains++;
1673 }
1674}
1675
1676#else
1677
1678static inline int
1679check_prev_add_irq(struct task_struct *curr, struct held_lock *prev,
1680 struct held_lock *next)
1681{
1682 return 1;
1683}
1684
1685static inline void inc_chains(void)
1686{
1687 nr_process_chains++;
1688}
1689
1690#endif
1691
Steven Rostedt48702ec2011-04-20 21:41:56 -04001692static void
1693print_deadlock_scenario(struct held_lock *nxt,
1694 struct held_lock *prv)
1695{
1696 struct lock_class *next = hlock_class(nxt);
1697 struct lock_class *prev = hlock_class(prv);
1698
1699 printk(" Possible unsafe locking scenario:\n\n");
1700 printk(" CPU0\n");
1701 printk(" ----\n");
1702 printk(" lock(");
1703 __print_lock_name(prev);
1704 printk(");\n");
1705 printk(" lock(");
1706 __print_lock_name(next);
1707 printk(");\n");
1708 printk("\n *** DEADLOCK ***\n\n");
1709 printk(" May be due to missing lock nesting notation\n\n");
1710}
1711
Peter Zijlstra8e182572007-07-19 01:48:54 -07001712static int
1713print_deadlock_bug(struct task_struct *curr, struct held_lock *prev,
1714 struct held_lock *next)
1715{
1716 if (!debug_locks_off_graph_unlock() || debug_locks_silent)
1717 return 0;
1718
Paul E. McKenneyb3fbab02011-05-24 08:31:09 -07001719 printk("\n");
1720 printk("=============================================\n");
1721 printk("[ INFO: possible recursive locking detected ]\n");
Ben Hutchingsfbdc4b92011-10-28 04:36:55 +01001722 print_kernel_ident();
Paul E. McKenneyb3fbab02011-05-24 08:31:09 -07001723 printk("---------------------------------------------\n");
Peter Zijlstra8e182572007-07-19 01:48:54 -07001724 printk("%s/%d is trying to acquire lock:\n",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07001725 curr->comm, task_pid_nr(curr));
Peter Zijlstra8e182572007-07-19 01:48:54 -07001726 print_lock(next);
1727 printk("\nbut task is already holding lock:\n");
1728 print_lock(prev);
1729
1730 printk("\nother info that might help us debug this:\n");
Steven Rostedt48702ec2011-04-20 21:41:56 -04001731 print_deadlock_scenario(next, prev);
Peter Zijlstra8e182572007-07-19 01:48:54 -07001732 lockdep_print_held_locks(curr);
1733
1734 printk("\nstack backtrace:\n");
1735 dump_stack();
1736
1737 return 0;
1738}
1739
1740/*
1741 * Check whether we are holding such a class already.
1742 *
1743 * (Note that this has to be done separately, because the graph cannot
1744 * detect such classes of deadlocks.)
1745 *
1746 * Returns: 0 on deadlock detected, 1 on OK, 2 on recursive read
1747 */
1748static int
1749check_deadlock(struct task_struct *curr, struct held_lock *next,
1750 struct lockdep_map *next_instance, int read)
1751{
1752 struct held_lock *prev;
Peter Zijlstra7531e2f2008-08-11 09:30:24 +02001753 struct held_lock *nest = NULL;
Peter Zijlstra8e182572007-07-19 01:48:54 -07001754 int i;
1755
1756 for (i = 0; i < curr->lockdep_depth; i++) {
1757 prev = curr->held_locks + i;
Peter Zijlstra7531e2f2008-08-11 09:30:24 +02001758
1759 if (prev->instance == next->nest_lock)
1760 nest = prev;
1761
Dave Jonesf82b2172008-08-11 09:30:23 +02001762 if (hlock_class(prev) != hlock_class(next))
Peter Zijlstra8e182572007-07-19 01:48:54 -07001763 continue;
Peter Zijlstra7531e2f2008-08-11 09:30:24 +02001764
Peter Zijlstra8e182572007-07-19 01:48:54 -07001765 /*
1766 * Allow read-after-read recursion of the same
1767 * lock class (i.e. read_lock(lock)+read_lock(lock)):
1768 */
1769 if ((read == 2) && prev->read)
1770 return 2;
Peter Zijlstra7531e2f2008-08-11 09:30:24 +02001771
1772 /*
1773 * We're holding the nest_lock, which serializes this lock's
1774 * nesting behaviour.
1775 */
1776 if (nest)
1777 return 2;
1778
Peter Zijlstra8e182572007-07-19 01:48:54 -07001779 return print_deadlock_bug(curr, prev, next);
1780 }
1781 return 1;
1782}
1783
1784/*
1785 * There was a chain-cache miss, and we are about to add a new dependency
1786 * to a previous lock. We recursively validate the following rules:
1787 *
1788 * - would the adding of the <prev> -> <next> dependency create a
1789 * circular dependency in the graph? [== circular deadlock]
1790 *
1791 * - does the new prev->next dependency connect any hardirq-safe lock
1792 * (in the full backwards-subgraph starting at <prev>) with any
1793 * hardirq-unsafe lock (in the full forwards-subgraph starting at
1794 * <next>)? [== illegal lock inversion with hardirq contexts]
1795 *
1796 * - does the new prev->next dependency connect any softirq-safe lock
1797 * (in the full backwards-subgraph starting at <prev>) with any
1798 * softirq-unsafe lock (in the full forwards-subgraph starting at
1799 * <next>)? [== illegal lock inversion with softirq contexts]
1800 *
1801 * any of these scenarios could lead to a deadlock.
1802 *
1803 * Then if all the validations pass, we add the forwards and backwards
1804 * dependency.
1805 */
1806static int
1807check_prev_add(struct task_struct *curr, struct held_lock *prev,
Yong Zhang4726f2a2010-05-04 14:16:48 +08001808 struct held_lock *next, int distance, int trylock_loop)
Peter Zijlstra8e182572007-07-19 01:48:54 -07001809{
1810 struct lock_list *entry;
1811 int ret;
Ming Leidb0002a2009-07-16 15:44:29 +02001812 struct lock_list this;
1813 struct lock_list *uninitialized_var(target_entry);
Yong Zhang4726f2a2010-05-04 14:16:48 +08001814 /*
1815 * Static variable, serialized by the graph_lock().
1816 *
1817 * We use this static variable to save the stack trace in case
1818 * we call into this function multiple times due to encountering
1819 * trylocks in the held lock stack.
1820 */
1821 static struct stack_trace trace;
Peter Zijlstra8e182572007-07-19 01:48:54 -07001822
1823 /*
1824 * Prove that the new <prev> -> <next> dependency would not
1825 * create a circular dependency in the graph. (We do this by
1826 * forward-recursing into the graph starting at <next>, and
1827 * checking whether we can reach <prev>.)
1828 *
1829 * We are using global variables to control the recursion, to
1830 * keep the stackframe size of the recursive functions low:
1831 */
Ming Leidb0002a2009-07-16 15:44:29 +02001832 this.class = hlock_class(next);
1833 this.parent = NULL;
1834 ret = check_noncircular(&this, hlock_class(prev), &target_entry);
1835 if (unlikely(!ret))
1836 return print_circular_bug(&this, target_entry, next, prev);
1837 else if (unlikely(ret < 0))
1838 return print_bfs_bug(ret);
Ming Leic94aa5c2009-07-16 15:44:29 +02001839
Peter Zijlstra8e182572007-07-19 01:48:54 -07001840 if (!check_prev_add_irq(curr, prev, next))
1841 return 0;
1842
1843 /*
1844 * For recursive read-locks we do all the dependency checks,
1845 * but we dont store read-triggered dependencies (only
1846 * write-triggered dependencies). This ensures that only the
1847 * write-side dependencies matter, and that if for example a
1848 * write-lock never takes any other locks, then the reads are
1849 * equivalent to a NOP.
1850 */
1851 if (next->read == 2 || prev->read == 2)
1852 return 1;
1853 /*
1854 * Is the <prev> -> <next> dependency already present?
1855 *
1856 * (this may occur even though this is a new chain: consider
1857 * e.g. the L1 -> L2 -> L3 -> L4 and the L5 -> L1 -> L2 -> L3
1858 * chains - the second one will be new, but L1 already has
1859 * L2 added to its dependency list, due to the first chain.)
1860 */
Dave Jonesf82b2172008-08-11 09:30:23 +02001861 list_for_each_entry(entry, &hlock_class(prev)->locks_after, entry) {
1862 if (entry->class == hlock_class(next)) {
Peter Zijlstra8e182572007-07-19 01:48:54 -07001863 if (distance == 1)
1864 entry->distance = 1;
1865 return 2;
1866 }
1867 }
1868
Yong Zhang4726f2a2010-05-04 14:16:48 +08001869 if (!trylock_loop && !save_trace(&trace))
1870 return 0;
1871
Peter Zijlstra8e182572007-07-19 01:48:54 -07001872 /*
1873 * Ok, all validations passed, add the new lock
1874 * to the previous lock's dependency list:
1875 */
Dave Jonesf82b2172008-08-11 09:30:23 +02001876 ret = add_lock_to_list(hlock_class(prev), hlock_class(next),
1877 &hlock_class(prev)->locks_after,
Yong Zhang4726f2a2010-05-04 14:16:48 +08001878 next->acquire_ip, distance, &trace);
Peter Zijlstra8e182572007-07-19 01:48:54 -07001879
1880 if (!ret)
1881 return 0;
1882
Dave Jonesf82b2172008-08-11 09:30:23 +02001883 ret = add_lock_to_list(hlock_class(next), hlock_class(prev),
1884 &hlock_class(next)->locks_before,
Yong Zhang4726f2a2010-05-04 14:16:48 +08001885 next->acquire_ip, distance, &trace);
Peter Zijlstra8e182572007-07-19 01:48:54 -07001886 if (!ret)
1887 return 0;
1888
1889 /*
1890 * Debugging printouts:
1891 */
Dave Jonesf82b2172008-08-11 09:30:23 +02001892 if (verbose(hlock_class(prev)) || verbose(hlock_class(next))) {
Peter Zijlstra8e182572007-07-19 01:48:54 -07001893 graph_unlock();
1894 printk("\n new dependency: ");
Dave Jonesf82b2172008-08-11 09:30:23 +02001895 print_lock_name(hlock_class(prev));
Peter Zijlstra8e182572007-07-19 01:48:54 -07001896 printk(" => ");
Dave Jonesf82b2172008-08-11 09:30:23 +02001897 print_lock_name(hlock_class(next));
Peter Zijlstra8e182572007-07-19 01:48:54 -07001898 printk("\n");
1899 dump_stack();
1900 return graph_lock();
1901 }
1902 return 1;
1903}
1904
1905/*
1906 * Add the dependency to all directly-previous locks that are 'relevant'.
1907 * The ones that are relevant are (in increasing distance from curr):
1908 * all consecutive trylock entries and the final non-trylock entry - or
1909 * the end of this context's lock-chain - whichever comes first.
1910 */
1911static int
1912check_prevs_add(struct task_struct *curr, struct held_lock *next)
1913{
1914 int depth = curr->lockdep_depth;
Yong Zhang4726f2a2010-05-04 14:16:48 +08001915 int trylock_loop = 0;
Peter Zijlstra8e182572007-07-19 01:48:54 -07001916 struct held_lock *hlock;
1917
1918 /*
1919 * Debugging checks.
1920 *
1921 * Depth must not be zero for a non-head lock:
1922 */
1923 if (!depth)
1924 goto out_bug;
1925 /*
1926 * At least two relevant locks must exist for this
1927 * to be a head:
1928 */
1929 if (curr->held_locks[depth].irq_context !=
1930 curr->held_locks[depth-1].irq_context)
1931 goto out_bug;
1932
1933 for (;;) {
1934 int distance = curr->lockdep_depth - depth + 1;
1935 hlock = curr->held_locks + depth-1;
1936 /*
1937 * Only non-recursive-read entries get new dependencies
1938 * added:
1939 */
1940 if (hlock->read != 2) {
Yong Zhang4726f2a2010-05-04 14:16:48 +08001941 if (!check_prev_add(curr, hlock, next,
1942 distance, trylock_loop))
Peter Zijlstra8e182572007-07-19 01:48:54 -07001943 return 0;
1944 /*
1945 * Stop after the first non-trylock entry,
1946 * as non-trylock entries have added their
1947 * own direct dependencies already, so this
1948 * lock is connected to them indirectly:
1949 */
1950 if (!hlock->trylock)
1951 break;
1952 }
1953 depth--;
1954 /*
1955 * End of lock-stack?
1956 */
1957 if (!depth)
1958 break;
1959 /*
1960 * Stop the search if we cross into another context:
1961 */
1962 if (curr->held_locks[depth].irq_context !=
1963 curr->held_locks[depth-1].irq_context)
1964 break;
Yong Zhang4726f2a2010-05-04 14:16:48 +08001965 trylock_loop = 1;
Peter Zijlstra8e182572007-07-19 01:48:54 -07001966 }
1967 return 1;
1968out_bug:
1969 if (!debug_locks_off_graph_unlock())
1970 return 0;
1971
Peter Zijlstra0119fee2011-09-02 01:30:29 +02001972 /*
1973 * Clearly we all shouldn't be here, but since we made it we
1974 * can reliable say we messed up our state. See the above two
1975 * gotos for reasons why we could possibly end up here.
1976 */
Peter Zijlstra8e182572007-07-19 01:48:54 -07001977 WARN_ON(1);
1978
1979 return 0;
1980}
1981
1982unsigned long nr_lock_chains;
Huang, Ying443cd502008-06-20 16:39:21 +08001983struct lock_chain lock_chains[MAX_LOCKDEP_CHAINS];
Huang, Yingcd1a28e2008-06-23 11:20:54 +08001984int nr_chain_hlocks;
Huang, Ying443cd502008-06-20 16:39:21 +08001985static u16 chain_hlocks[MAX_LOCKDEP_CHAIN_HLOCKS];
1986
1987struct lock_class *lock_chain_get_class(struct lock_chain *chain, int i)
1988{
1989 return lock_classes + chain_hlocks[chain->base + i];
1990}
Peter Zijlstra8e182572007-07-19 01:48:54 -07001991
1992/*
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001993 * Look up a dependency chain. If the key is not present yet then
Jarek Poplawski9e860d02007-05-08 00:30:12 -07001994 * add it and return 1 - in this case the new dependency chain is
1995 * validated. If the key is already hashed, return 0.
1996 * (On return with 1 graph_lock is held.)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001997 */
Huang, Ying443cd502008-06-20 16:39:21 +08001998static inline int lookup_chain_cache(struct task_struct *curr,
1999 struct held_lock *hlock,
2000 u64 chain_key)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002001{
Dave Jonesf82b2172008-08-11 09:30:23 +02002002 struct lock_class *class = hlock_class(hlock);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002003 struct list_head *hash_head = chainhashentry(chain_key);
2004 struct lock_chain *chain;
Huang, Ying443cd502008-06-20 16:39:21 +08002005 struct held_lock *hlock_curr, *hlock_next;
Steven Rostedte0944ee2011-04-20 21:42:00 -04002006 int i, j;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002007
Peter Zijlstra0119fee2011-09-02 01:30:29 +02002008 /*
2009 * We might need to take the graph lock, ensure we've got IRQs
2010 * disabled to make this an IRQ-safe lock.. for recursion reasons
2011 * lockdep won't complain about its own locking errors.
2012 */
Jarek Poplawski381a2292007-02-10 01:44:58 -08002013 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2014 return 0;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002015 /*
2016 * We can walk it lock-free, because entries only get added
2017 * to the hash:
2018 */
2019 list_for_each_entry(chain, hash_head, entry) {
2020 if (chain->chain_key == chain_key) {
2021cache_hit:
Frederic Weisbeckerbd6d29c2010-04-06 00:10:17 +02002022 debug_atomic_inc(chain_lookup_hits);
Ingo Molnar81fc6852006-12-13 00:34:40 -08002023 if (very_verbose(class))
Andrew Morton755cd902006-12-29 16:49:14 -08002024 printk("\nhash chain already cached, key: "
2025 "%016Lx tail class: [%p] %s\n",
2026 (unsigned long long)chain_key,
2027 class->key, class->name);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002028 return 0;
2029 }
2030 }
Ingo Molnar81fc6852006-12-13 00:34:40 -08002031 if (very_verbose(class))
Andrew Morton755cd902006-12-29 16:49:14 -08002032 printk("\nnew hash chain, key: %016Lx tail class: [%p] %s\n",
2033 (unsigned long long)chain_key, class->key, class->name);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002034 /*
2035 * Allocate a new chain entry from the static array, and add
2036 * it to the hash:
2037 */
Ingo Molnar74c383f2006-12-13 00:34:43 -08002038 if (!graph_lock())
2039 return 0;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002040 /*
2041 * We have to walk the chain again locked - to avoid duplicates:
2042 */
2043 list_for_each_entry(chain, hash_head, entry) {
2044 if (chain->chain_key == chain_key) {
Ingo Molnar74c383f2006-12-13 00:34:43 -08002045 graph_unlock();
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002046 goto cache_hit;
2047 }
2048 }
2049 if (unlikely(nr_lock_chains >= MAX_LOCKDEP_CHAINS)) {
Ingo Molnar74c383f2006-12-13 00:34:43 -08002050 if (!debug_locks_off_graph_unlock())
2051 return 0;
2052
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002053 printk("BUG: MAX_LOCKDEP_CHAINS too low!\n");
2054 printk("turning off the locking correctness validator.\n");
Peter Zijlstraeedeeab2009-03-18 12:38:47 +01002055 dump_stack();
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002056 return 0;
2057 }
2058 chain = lock_chains + nr_lock_chains++;
2059 chain->chain_key = chain_key;
Huang, Ying443cd502008-06-20 16:39:21 +08002060 chain->irq_context = hlock->irq_context;
2061 /* Find the first held_lock of current chain */
2062 hlock_next = hlock;
2063 for (i = curr->lockdep_depth - 1; i >= 0; i--) {
2064 hlock_curr = curr->held_locks + i;
2065 if (hlock_curr->irq_context != hlock_next->irq_context)
2066 break;
2067 hlock_next = hlock;
2068 }
2069 i++;
2070 chain->depth = curr->lockdep_depth + 1 - i;
Steven Rostedte0944ee2011-04-20 21:42:00 -04002071 if (likely(nr_chain_hlocks + chain->depth <= MAX_LOCKDEP_CHAIN_HLOCKS)) {
2072 chain->base = nr_chain_hlocks;
2073 nr_chain_hlocks += chain->depth;
Huang, Ying443cd502008-06-20 16:39:21 +08002074 for (j = 0; j < chain->depth - 1; j++, i++) {
Dave Jonesf82b2172008-08-11 09:30:23 +02002075 int lock_id = curr->held_locks[i].class_idx - 1;
Huang, Ying443cd502008-06-20 16:39:21 +08002076 chain_hlocks[chain->base + j] = lock_id;
2077 }
2078 chain_hlocks[chain->base + j] = class - lock_classes;
2079 }
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002080 list_add_tail_rcu(&chain->entry, hash_head);
Frederic Weisbeckerbd6d29c2010-04-06 00:10:17 +02002081 debug_atomic_inc(chain_lookup_misses);
Peter Zijlstra8e182572007-07-19 01:48:54 -07002082 inc_chains();
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002083
2084 return 1;
2085}
Peter Zijlstra8e182572007-07-19 01:48:54 -07002086
2087static int validate_chain(struct task_struct *curr, struct lockdep_map *lock,
Johannes Berg4e6045f2007-10-18 23:39:55 -07002088 struct held_lock *hlock, int chain_head, u64 chain_key)
Peter Zijlstra8e182572007-07-19 01:48:54 -07002089{
2090 /*
2091 * Trylock needs to maintain the stack of held locks, but it
2092 * does not add new dependencies, because trylock can be done
2093 * in any order.
2094 *
2095 * We look up the chain_key and do the O(N^2) check and update of
2096 * the dependencies only if this is a new dependency chain.
2097 * (If lookup_chain_cache() returns with 1 it acquires
2098 * graph_lock for us)
2099 */
2100 if (!hlock->trylock && (hlock->check == 2) &&
Huang, Ying443cd502008-06-20 16:39:21 +08002101 lookup_chain_cache(curr, hlock, chain_key)) {
Peter Zijlstra8e182572007-07-19 01:48:54 -07002102 /*
2103 * Check whether last held lock:
2104 *
2105 * - is irq-safe, if this lock is irq-unsafe
2106 * - is softirq-safe, if this lock is hardirq-unsafe
2107 *
2108 * And check whether the new lock's dependency graph
2109 * could lead back to the previous lock.
2110 *
2111 * any of these scenarios could lead to a deadlock. If
2112 * All validations
2113 */
2114 int ret = check_deadlock(curr, hlock, lock, hlock->read);
2115
2116 if (!ret)
2117 return 0;
2118 /*
2119 * Mark recursive read, as we jump over it when
2120 * building dependencies (just like we jump over
2121 * trylock entries):
2122 */
2123 if (ret == 2)
2124 hlock->read = 2;
2125 /*
2126 * Add dependency only if this lock is not the head
2127 * of the chain, and if it's not a secondary read-lock:
2128 */
2129 if (!chain_head && ret != 2)
2130 if (!check_prevs_add(curr, hlock))
2131 return 0;
2132 graph_unlock();
2133 } else
2134 /* after lookup_chain_cache(): */
2135 if (unlikely(!debug_locks))
2136 return 0;
2137
2138 return 1;
2139}
2140#else
2141static inline int validate_chain(struct task_struct *curr,
2142 struct lockdep_map *lock, struct held_lock *hlock,
Gregory Haskins3aa416b2007-10-11 22:11:11 +02002143 int chain_head, u64 chain_key)
Peter Zijlstra8e182572007-07-19 01:48:54 -07002144{
2145 return 1;
2146}
Peter Zijlstraca58abc2007-07-19 01:48:53 -07002147#endif
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002148
2149/*
2150 * We are building curr_chain_key incrementally, so double-check
2151 * it from scratch, to make sure that it's done correctly:
2152 */
Steven Rostedt1d09daa2008-05-12 21:20:55 +02002153static void check_chain_key(struct task_struct *curr)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002154{
2155#ifdef CONFIG_DEBUG_LOCKDEP
2156 struct held_lock *hlock, *prev_hlock = NULL;
2157 unsigned int i, id;
2158 u64 chain_key = 0;
2159
2160 for (i = 0; i < curr->lockdep_depth; i++) {
2161 hlock = curr->held_locks + i;
2162 if (chain_key != hlock->prev_chain_key) {
2163 debug_locks_off();
Peter Zijlstra0119fee2011-09-02 01:30:29 +02002164 /*
2165 * We got mighty confused, our chain keys don't match
2166 * with what we expect, someone trample on our task state?
2167 */
Arjan van de Ven2df8b1d2008-07-30 12:43:11 -07002168 WARN(1, "hm#1, depth: %u [%u], %016Lx != %016Lx\n",
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002169 curr->lockdep_depth, i,
2170 (unsigned long long)chain_key,
2171 (unsigned long long)hlock->prev_chain_key);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002172 return;
2173 }
Dave Jonesf82b2172008-08-11 09:30:23 +02002174 id = hlock->class_idx - 1;
Peter Zijlstra0119fee2011-09-02 01:30:29 +02002175 /*
2176 * Whoops ran out of static storage again?
2177 */
Jarek Poplawski381a2292007-02-10 01:44:58 -08002178 if (DEBUG_LOCKS_WARN_ON(id >= MAX_LOCKDEP_KEYS))
2179 return;
2180
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002181 if (prev_hlock && (prev_hlock->irq_context !=
2182 hlock->irq_context))
2183 chain_key = 0;
2184 chain_key = iterate_chain_key(chain_key, id);
2185 prev_hlock = hlock;
2186 }
2187 if (chain_key != curr->curr_chain_key) {
2188 debug_locks_off();
Peter Zijlstra0119fee2011-09-02 01:30:29 +02002189 /*
2190 * More smoking hash instead of calculating it, damn see these
2191 * numbers float.. I bet that a pink elephant stepped on my memory.
2192 */
Arjan van de Ven2df8b1d2008-07-30 12:43:11 -07002193 WARN(1, "hm#2, depth: %u [%u], %016Lx != %016Lx\n",
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002194 curr->lockdep_depth, i,
2195 (unsigned long long)chain_key,
2196 (unsigned long long)curr->curr_chain_key);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002197 }
2198#endif
2199}
2200
Steven Rostedt282b5c22011-04-20 21:41:59 -04002201static void
2202print_usage_bug_scenario(struct held_lock *lock)
2203{
2204 struct lock_class *class = hlock_class(lock);
2205
2206 printk(" Possible unsafe locking scenario:\n\n");
2207 printk(" CPU0\n");
2208 printk(" ----\n");
2209 printk(" lock(");
2210 __print_lock_name(class);
2211 printk(");\n");
2212 printk(" <Interrupt>\n");
2213 printk(" lock(");
2214 __print_lock_name(class);
2215 printk(");\n");
2216 printk("\n *** DEADLOCK ***\n\n");
2217}
2218
Peter Zijlstra8e182572007-07-19 01:48:54 -07002219static int
2220print_usage_bug(struct task_struct *curr, struct held_lock *this,
2221 enum lock_usage_bit prev_bit, enum lock_usage_bit new_bit)
2222{
2223 if (!debug_locks_off_graph_unlock() || debug_locks_silent)
2224 return 0;
2225
Paul E. McKenneyb3fbab02011-05-24 08:31:09 -07002226 printk("\n");
2227 printk("=================================\n");
2228 printk("[ INFO: inconsistent lock state ]\n");
Ben Hutchingsfbdc4b92011-10-28 04:36:55 +01002229 print_kernel_ident();
Paul E. McKenneyb3fbab02011-05-24 08:31:09 -07002230 printk("---------------------------------\n");
Peter Zijlstra8e182572007-07-19 01:48:54 -07002231
2232 printk("inconsistent {%s} -> {%s} usage.\n",
2233 usage_str[prev_bit], usage_str[new_bit]);
2234
2235 printk("%s/%d [HC%u[%lu]:SC%u[%lu]:HE%u:SE%u] takes:\n",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07002236 curr->comm, task_pid_nr(curr),
Peter Zijlstra8e182572007-07-19 01:48:54 -07002237 trace_hardirq_context(curr), hardirq_count() >> HARDIRQ_SHIFT,
2238 trace_softirq_context(curr), softirq_count() >> SOFTIRQ_SHIFT,
2239 trace_hardirqs_enabled(curr),
2240 trace_softirqs_enabled(curr));
2241 print_lock(this);
2242
2243 printk("{%s} state was registered at:\n", usage_str[prev_bit]);
Dave Jonesf82b2172008-08-11 09:30:23 +02002244 print_stack_trace(hlock_class(this)->usage_traces + prev_bit, 1);
Peter Zijlstra8e182572007-07-19 01:48:54 -07002245
2246 print_irqtrace_events(curr);
2247 printk("\nother info that might help us debug this:\n");
Steven Rostedt282b5c22011-04-20 21:41:59 -04002248 print_usage_bug_scenario(this);
2249
Peter Zijlstra8e182572007-07-19 01:48:54 -07002250 lockdep_print_held_locks(curr);
2251
2252 printk("\nstack backtrace:\n");
2253 dump_stack();
2254
2255 return 0;
2256}
2257
2258/*
2259 * Print out an error if an invalid bit is set:
2260 */
2261static inline int
2262valid_state(struct task_struct *curr, struct held_lock *this,
2263 enum lock_usage_bit new_bit, enum lock_usage_bit bad_bit)
2264{
Dave Jonesf82b2172008-08-11 09:30:23 +02002265 if (unlikely(hlock_class(this)->usage_mask & (1 << bad_bit)))
Peter Zijlstra8e182572007-07-19 01:48:54 -07002266 return print_usage_bug(curr, this, bad_bit, new_bit);
2267 return 1;
2268}
2269
2270static int mark_lock(struct task_struct *curr, struct held_lock *this,
2271 enum lock_usage_bit new_bit);
2272
Steven Rostedt81d68a92008-05-12 21:20:42 +02002273#if defined(CONFIG_TRACE_IRQFLAGS) && defined(CONFIG_PROVE_LOCKING)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002274
2275/*
2276 * print irq inversion bug:
2277 */
2278static int
Ming Lei24208ca2009-07-16 15:44:29 +02002279print_irq_inversion_bug(struct task_struct *curr,
2280 struct lock_list *root, struct lock_list *other,
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002281 struct held_lock *this, int forwards,
2282 const char *irqclass)
2283{
Steven Rostedtdad3d742011-04-20 21:41:57 -04002284 struct lock_list *entry = other;
2285 struct lock_list *middle = NULL;
2286 int depth;
2287
Ingo Molnar74c383f2006-12-13 00:34:43 -08002288 if (!debug_locks_off_graph_unlock() || debug_locks_silent)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002289 return 0;
2290
Paul E. McKenneyb3fbab02011-05-24 08:31:09 -07002291 printk("\n");
2292 printk("=========================================================\n");
2293 printk("[ INFO: possible irq lock inversion dependency detected ]\n");
Ben Hutchingsfbdc4b92011-10-28 04:36:55 +01002294 print_kernel_ident();
Paul E. McKenneyb3fbab02011-05-24 08:31:09 -07002295 printk("---------------------------------------------------------\n");
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002296 printk("%s/%d just changed the state of lock:\n",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07002297 curr->comm, task_pid_nr(curr));
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002298 print_lock(this);
2299 if (forwards)
Peter Zijlstra26575e22009-03-04 14:53:24 +01002300 printk("but this lock took another, %s-unsafe lock in the past:\n", irqclass);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002301 else
Peter Zijlstra26575e22009-03-04 14:53:24 +01002302 printk("but this lock was taken by another, %s-safe lock in the past:\n", irqclass);
Ming Lei24208ca2009-07-16 15:44:29 +02002303 print_lock_name(other->class);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002304 printk("\n\nand interrupts could create inverse lock ordering between them.\n\n");
2305
2306 printk("\nother info that might help us debug this:\n");
Steven Rostedtdad3d742011-04-20 21:41:57 -04002307
2308 /* Find a middle lock (if one exists) */
2309 depth = get_lock_depth(other);
2310 do {
2311 if (depth == 0 && (entry != root)) {
2312 printk("lockdep:%s bad path found in chain graph\n", __func__);
2313 break;
2314 }
2315 middle = entry;
2316 entry = get_lock_parent(entry);
2317 depth--;
2318 } while (entry && entry != root && (depth >= 0));
2319 if (forwards)
2320 print_irq_lock_scenario(root, other,
2321 middle ? middle->class : root->class, other->class);
2322 else
2323 print_irq_lock_scenario(other, root,
2324 middle ? middle->class : other->class, root->class);
2325
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002326 lockdep_print_held_locks(curr);
2327
Ming Lei24208ca2009-07-16 15:44:29 +02002328 printk("\nthe shortest dependencies between 2nd lock and 1st lock:\n");
2329 if (!save_trace(&root->trace))
2330 return 0;
2331 print_shortest_lock_dependencies(other, root);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002332
2333 printk("\nstack backtrace:\n");
2334 dump_stack();
2335
2336 return 0;
2337}
2338
2339/*
2340 * Prove that in the forwards-direction subgraph starting at <this>
2341 * there is no lock matching <mask>:
2342 */
2343static int
2344check_usage_forwards(struct task_struct *curr, struct held_lock *this,
2345 enum lock_usage_bit bit, const char *irqclass)
2346{
2347 int ret;
Ming Leid7aaba12009-07-16 15:44:29 +02002348 struct lock_list root;
2349 struct lock_list *uninitialized_var(target_entry);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002350
Ming Leid7aaba12009-07-16 15:44:29 +02002351 root.parent = NULL;
2352 root.class = hlock_class(this);
2353 ret = find_usage_forwards(&root, bit, &target_entry);
Peter Zijlstraaf012962009-07-16 15:44:29 +02002354 if (ret < 0)
2355 return print_bfs_bug(ret);
2356 if (ret == 1)
2357 return ret;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002358
Ming Lei24208ca2009-07-16 15:44:29 +02002359 return print_irq_inversion_bug(curr, &root, target_entry,
Ming Leid7aaba12009-07-16 15:44:29 +02002360 this, 1, irqclass);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002361}
2362
2363/*
2364 * Prove that in the backwards-direction subgraph starting at <this>
2365 * there is no lock matching <mask>:
2366 */
2367static int
2368check_usage_backwards(struct task_struct *curr, struct held_lock *this,
2369 enum lock_usage_bit bit, const char *irqclass)
2370{
2371 int ret;
Ming Leid7aaba12009-07-16 15:44:29 +02002372 struct lock_list root;
2373 struct lock_list *uninitialized_var(target_entry);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002374
Ming Leid7aaba12009-07-16 15:44:29 +02002375 root.parent = NULL;
2376 root.class = hlock_class(this);
2377 ret = find_usage_backwards(&root, bit, &target_entry);
Peter Zijlstraaf012962009-07-16 15:44:29 +02002378 if (ret < 0)
2379 return print_bfs_bug(ret);
2380 if (ret == 1)
2381 return ret;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002382
Ming Lei24208ca2009-07-16 15:44:29 +02002383 return print_irq_inversion_bug(curr, &root, target_entry,
Oleg Nesterov48d50672010-01-26 19:16:41 +01002384 this, 0, irqclass);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002385}
2386
Ingo Molnar3117df02006-12-13 00:34:43 -08002387void print_irqtrace_events(struct task_struct *curr)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002388{
2389 printk("irq event stamp: %u\n", curr->irq_events);
2390 printk("hardirqs last enabled at (%u): ", curr->hardirq_enable_event);
2391 print_ip_sym(curr->hardirq_enable_ip);
2392 printk("hardirqs last disabled at (%u): ", curr->hardirq_disable_event);
2393 print_ip_sym(curr->hardirq_disable_ip);
2394 printk("softirqs last enabled at (%u): ", curr->softirq_enable_event);
2395 print_ip_sym(curr->softirq_enable_ip);
2396 printk("softirqs last disabled at (%u): ", curr->softirq_disable_event);
2397 print_ip_sym(curr->softirq_disable_ip);
2398}
2399
Peter Zijlstracd953022009-01-22 16:38:21 +01002400static int HARDIRQ_verbose(struct lock_class *class)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002401{
Peter Zijlstra8e182572007-07-19 01:48:54 -07002402#if HARDIRQ_VERBOSE
2403 return class_filter(class);
2404#endif
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002405 return 0;
2406}
2407
Peter Zijlstracd953022009-01-22 16:38:21 +01002408static int SOFTIRQ_verbose(struct lock_class *class)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002409{
Peter Zijlstra8e182572007-07-19 01:48:54 -07002410#if SOFTIRQ_VERBOSE
2411 return class_filter(class);
2412#endif
2413 return 0;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002414}
2415
Peter Zijlstracd953022009-01-22 16:38:21 +01002416static int RECLAIM_FS_verbose(struct lock_class *class)
Nick Piggincf40bd12009-01-21 08:12:39 +01002417{
2418#if RECLAIM_VERBOSE
2419 return class_filter(class);
2420#endif
2421 return 0;
2422}
2423
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002424#define STRICT_READ_CHECKS 1
2425
Peter Zijlstracd953022009-01-22 16:38:21 +01002426static int (*state_verbose_f[])(struct lock_class *class) = {
2427#define LOCKDEP_STATE(__STATE) \
2428 __STATE##_verbose,
2429#include "lockdep_states.h"
2430#undef LOCKDEP_STATE
2431};
2432
2433static inline int state_verbose(enum lock_usage_bit bit,
2434 struct lock_class *class)
2435{
2436 return state_verbose_f[bit >> 2](class);
2437}
2438
Peter Zijlstra42c50d52009-01-22 16:58:16 +01002439typedef int (*check_usage_f)(struct task_struct *, struct held_lock *,
2440 enum lock_usage_bit bit, const char *name);
2441
Peter Zijlstra6a6904d2009-01-22 16:07:44 +01002442static int
Peter Zijlstra1c21f142009-03-04 13:51:13 +01002443mark_lock_irq(struct task_struct *curr, struct held_lock *this,
2444 enum lock_usage_bit new_bit)
Peter Zijlstra6a6904d2009-01-22 16:07:44 +01002445{
Peter Zijlstraf9892092009-01-22 16:09:59 +01002446 int excl_bit = exclusive_bit(new_bit);
Peter Zijlstra9d3651a2009-01-22 17:18:32 +01002447 int read = new_bit & 1;
Peter Zijlstra42c50d52009-01-22 16:58:16 +01002448 int dir = new_bit & 2;
2449
Peter Zijlstra38aa2712009-01-27 14:53:50 +01002450 /*
2451 * mark USED_IN has to look forwards -- to ensure no dependency
2452 * has ENABLED state, which would allow recursion deadlocks.
2453 *
2454 * mark ENABLED has to look backwards -- to ensure no dependee
2455 * has USED_IN state, which, again, would allow recursion deadlocks.
2456 */
Peter Zijlstra42c50d52009-01-22 16:58:16 +01002457 check_usage_f usage = dir ?
2458 check_usage_backwards : check_usage_forwards;
Peter Zijlstraf9892092009-01-22 16:09:59 +01002459
Peter Zijlstra38aa2712009-01-27 14:53:50 +01002460 /*
2461 * Validate that this particular lock does not have conflicting
2462 * usage states.
2463 */
Peter Zijlstra6a6904d2009-01-22 16:07:44 +01002464 if (!valid_state(curr, this, new_bit, excl_bit))
2465 return 0;
Peter Zijlstra9d3651a2009-01-22 17:18:32 +01002466
Peter Zijlstra38aa2712009-01-27 14:53:50 +01002467 /*
2468 * Validate that the lock dependencies don't have conflicting usage
2469 * states.
2470 */
2471 if ((!read || !dir || STRICT_READ_CHECKS) &&
Peter Zijlstra1c21f142009-03-04 13:51:13 +01002472 !usage(curr, this, excl_bit, state_name(new_bit & ~1)))
Peter Zijlstra6a6904d2009-01-22 16:07:44 +01002473 return 0;
Peter Zijlstra780e8202009-01-22 16:51:29 +01002474
Peter Zijlstra38aa2712009-01-27 14:53:50 +01002475 /*
2476 * Check for read in write conflicts
2477 */
2478 if (!read) {
2479 if (!valid_state(curr, this, new_bit, excl_bit + 1))
2480 return 0;
2481
2482 if (STRICT_READ_CHECKS &&
Peter Zijlstra4f367d8a2009-01-22 18:10:42 +01002483 !usage(curr, this, excl_bit + 1,
2484 state_name(new_bit + 1)))
Peter Zijlstra38aa2712009-01-27 14:53:50 +01002485 return 0;
2486 }
Peter Zijlstra780e8202009-01-22 16:51:29 +01002487
Peter Zijlstracd953022009-01-22 16:38:21 +01002488 if (state_verbose(new_bit, hlock_class(this)))
Peter Zijlstra6a6904d2009-01-22 16:07:44 +01002489 return 2;
2490
2491 return 1;
2492}
2493
Nick Piggincf40bd12009-01-21 08:12:39 +01002494enum mark_type {
Peter Zijlstra36bfb9b2009-01-22 14:12:41 +01002495#define LOCKDEP_STATE(__STATE) __STATE,
2496#include "lockdep_states.h"
2497#undef LOCKDEP_STATE
Nick Piggincf40bd12009-01-21 08:12:39 +01002498};
2499
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002500/*
2501 * Mark all held locks with a usage bit:
2502 */
Steven Rostedt1d09daa2008-05-12 21:20:55 +02002503static int
Nick Piggincf40bd12009-01-21 08:12:39 +01002504mark_held_locks(struct task_struct *curr, enum mark_type mark)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002505{
2506 enum lock_usage_bit usage_bit;
2507 struct held_lock *hlock;
2508 int i;
2509
2510 for (i = 0; i < curr->lockdep_depth; i++) {
2511 hlock = curr->held_locks + i;
2512
Peter Zijlstracf2ad4d2009-01-27 13:58:08 +01002513 usage_bit = 2 + (mark << 2); /* ENABLED */
2514 if (hlock->read)
2515 usage_bit += 1; /* READ */
2516
2517 BUG_ON(usage_bit >= LOCK_USAGE_STATES);
Nick Piggincf40bd12009-01-21 08:12:39 +01002518
Peter Zijlstra70a06862011-07-25 12:09:59 +02002519 if (hlock_class(hlock)->key == __lockdep_no_validate__.subkeys)
Peter Zijlstraefbe2ee2011-07-07 11:39:45 +02002520 continue;
2521
Jarek Poplawski4ff773bb2007-05-08 00:31:00 -07002522 if (!mark_lock(curr, hlock, usage_bit))
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002523 return 0;
2524 }
2525
2526 return 1;
2527}
2528
2529/*
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002530 * Hardirqs will be enabled:
2531 */
Peter Zijlstradd4e5d32011-06-21 17:17:27 +02002532static void __trace_hardirqs_on_caller(unsigned long ip)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002533{
2534 struct task_struct *curr = current;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002535
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002536 /* we'll do an OFF -> ON transition: */
2537 curr->hardirqs_enabled = 1;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002538
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002539 /*
2540 * We are going to turn hardirqs on, so set the
2541 * usage bit for all held locks:
2542 */
Nick Piggincf40bd12009-01-21 08:12:39 +01002543 if (!mark_held_locks(curr, HARDIRQ))
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002544 return;
2545 /*
2546 * If we have softirqs enabled, then set the usage
2547 * bit for all held locks. (disabled hardirqs prevented
2548 * this bit from being set before)
2549 */
2550 if (curr->softirqs_enabled)
Nick Piggincf40bd12009-01-21 08:12:39 +01002551 if (!mark_held_locks(curr, SOFTIRQ))
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002552 return;
2553
2554 curr->hardirq_enable_ip = ip;
2555 curr->hardirq_enable_event = ++curr->irq_events;
Frederic Weisbeckerbd6d29c2010-04-06 00:10:17 +02002556 debug_atomic_inc(hardirqs_on_events);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002557}
Peter Zijlstradd4e5d32011-06-21 17:17:27 +02002558
2559void trace_hardirqs_on_caller(unsigned long ip)
2560{
2561 time_hardirqs_on(CALLER_ADDR0, ip);
2562
2563 if (unlikely(!debug_locks || current->lockdep_recursion))
2564 return;
2565
Peter Zijlstra7d36b262011-07-26 13:13:44 +02002566 if (unlikely(current->hardirqs_enabled)) {
2567 /*
2568 * Neither irq nor preemption are disabled here
2569 * so this is racy by nature but losing one hit
2570 * in a stat is not a big deal.
2571 */
2572 __debug_atomic_inc(redundant_hardirqs_on);
2573 return;
2574 }
2575
Peter Zijlstra0119fee2011-09-02 01:30:29 +02002576 /*
2577 * We're enabling irqs and according to our state above irqs weren't
2578 * already enabled, yet we find the hardware thinks they are in fact
2579 * enabled.. someone messed up their IRQ state tracing.
2580 */
Peter Zijlstradd4e5d32011-06-21 17:17:27 +02002581 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2582 return;
2583
Peter Zijlstra0119fee2011-09-02 01:30:29 +02002584 /*
2585 * See the fine text that goes along with this variable definition.
2586 */
Peter Zijlstra7d36b262011-07-26 13:13:44 +02002587 if (DEBUG_LOCKS_WARN_ON(unlikely(early_boot_irqs_disabled)))
2588 return;
2589
Peter Zijlstra0119fee2011-09-02 01:30:29 +02002590 /*
2591 * Can't allow enabling interrupts while in an interrupt handler,
2592 * that's general bad form and such. Recursion, limited stack etc..
2593 */
Peter Zijlstra7d36b262011-07-26 13:13:44 +02002594 if (DEBUG_LOCKS_WARN_ON(current->hardirq_context))
2595 return;
2596
Peter Zijlstradd4e5d32011-06-21 17:17:27 +02002597 current->lockdep_recursion = 1;
2598 __trace_hardirqs_on_caller(ip);
2599 current->lockdep_recursion = 0;
2600}
Steven Rostedt81d68a92008-05-12 21:20:42 +02002601EXPORT_SYMBOL(trace_hardirqs_on_caller);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002602
Steven Rostedt1d09daa2008-05-12 21:20:55 +02002603void trace_hardirqs_on(void)
Steven Rostedt81d68a92008-05-12 21:20:42 +02002604{
2605 trace_hardirqs_on_caller(CALLER_ADDR0);
2606}
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002607EXPORT_SYMBOL(trace_hardirqs_on);
2608
2609/*
2610 * Hardirqs were disabled:
2611 */
Heiko Carstens6afe40b2008-10-28 11:14:58 +01002612void trace_hardirqs_off_caller(unsigned long ip)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002613{
2614 struct task_struct *curr = current;
2615
Heiko Carstens6afe40b2008-10-28 11:14:58 +01002616 time_hardirqs_off(CALLER_ADDR0, ip);
Steven Rostedt81d68a92008-05-12 21:20:42 +02002617
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002618 if (unlikely(!debug_locks || current->lockdep_recursion))
2619 return;
2620
Peter Zijlstra0119fee2011-09-02 01:30:29 +02002621 /*
2622 * So we're supposed to get called after you mask local IRQs, but for
2623 * some reason the hardware doesn't quite think you did a proper job.
2624 */
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002625 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2626 return;
2627
2628 if (curr->hardirqs_enabled) {
2629 /*
2630 * We have done an ON -> OFF transition:
2631 */
2632 curr->hardirqs_enabled = 0;
Heiko Carstens6afe40b2008-10-28 11:14:58 +01002633 curr->hardirq_disable_ip = ip;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002634 curr->hardirq_disable_event = ++curr->irq_events;
Frederic Weisbeckerbd6d29c2010-04-06 00:10:17 +02002635 debug_atomic_inc(hardirqs_off_events);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002636 } else
Frederic Weisbeckerbd6d29c2010-04-06 00:10:17 +02002637 debug_atomic_inc(redundant_hardirqs_off);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002638}
Steven Rostedt81d68a92008-05-12 21:20:42 +02002639EXPORT_SYMBOL(trace_hardirqs_off_caller);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002640
Steven Rostedt1d09daa2008-05-12 21:20:55 +02002641void trace_hardirqs_off(void)
Steven Rostedt81d68a92008-05-12 21:20:42 +02002642{
2643 trace_hardirqs_off_caller(CALLER_ADDR0);
2644}
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002645EXPORT_SYMBOL(trace_hardirqs_off);
2646
2647/*
2648 * Softirqs will be enabled:
2649 */
2650void trace_softirqs_on(unsigned long ip)
2651{
2652 struct task_struct *curr = current;
2653
Peter Zijlstradd4e5d32011-06-21 17:17:27 +02002654 if (unlikely(!debug_locks || current->lockdep_recursion))
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002655 return;
2656
Peter Zijlstra0119fee2011-09-02 01:30:29 +02002657 /*
2658 * We fancy IRQs being disabled here, see softirq.c, avoids
2659 * funny state and nesting things.
2660 */
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002661 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2662 return;
2663
2664 if (curr->softirqs_enabled) {
Frederic Weisbeckerbd6d29c2010-04-06 00:10:17 +02002665 debug_atomic_inc(redundant_softirqs_on);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002666 return;
2667 }
2668
Peter Zijlstradd4e5d32011-06-21 17:17:27 +02002669 current->lockdep_recursion = 1;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002670 /*
2671 * We'll do an OFF -> ON transition:
2672 */
2673 curr->softirqs_enabled = 1;
2674 curr->softirq_enable_ip = ip;
2675 curr->softirq_enable_event = ++curr->irq_events;
Frederic Weisbeckerbd6d29c2010-04-06 00:10:17 +02002676 debug_atomic_inc(softirqs_on_events);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002677 /*
2678 * We are going to turn softirqs on, so set the
2679 * usage bit for all held locks, if hardirqs are
2680 * enabled too:
2681 */
2682 if (curr->hardirqs_enabled)
Nick Piggincf40bd12009-01-21 08:12:39 +01002683 mark_held_locks(curr, SOFTIRQ);
Peter Zijlstradd4e5d32011-06-21 17:17:27 +02002684 current->lockdep_recursion = 0;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002685}
2686
2687/*
2688 * Softirqs were disabled:
2689 */
2690void trace_softirqs_off(unsigned long ip)
2691{
2692 struct task_struct *curr = current;
2693
Peter Zijlstradd4e5d32011-06-21 17:17:27 +02002694 if (unlikely(!debug_locks || current->lockdep_recursion))
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002695 return;
2696
Peter Zijlstra0119fee2011-09-02 01:30:29 +02002697 /*
2698 * We fancy IRQs being disabled here, see softirq.c
2699 */
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002700 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2701 return;
2702
2703 if (curr->softirqs_enabled) {
2704 /*
2705 * We have done an ON -> OFF transition:
2706 */
2707 curr->softirqs_enabled = 0;
2708 curr->softirq_disable_ip = ip;
2709 curr->softirq_disable_event = ++curr->irq_events;
Frederic Weisbeckerbd6d29c2010-04-06 00:10:17 +02002710 debug_atomic_inc(softirqs_off_events);
Peter Zijlstra0119fee2011-09-02 01:30:29 +02002711 /*
2712 * Whoops, we wanted softirqs off, so why aren't they?
2713 */
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002714 DEBUG_LOCKS_WARN_ON(!softirq_count());
2715 } else
Frederic Weisbeckerbd6d29c2010-04-06 00:10:17 +02002716 debug_atomic_inc(redundant_softirqs_off);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002717}
2718
Peter Zijlstra2f850182009-03-20 11:13:20 +01002719static void __lockdep_trace_alloc(gfp_t gfp_mask, unsigned long flags)
Nick Piggincf40bd12009-01-21 08:12:39 +01002720{
2721 struct task_struct *curr = current;
2722
2723 if (unlikely(!debug_locks))
2724 return;
2725
2726 /* no reclaim without waiting on it */
2727 if (!(gfp_mask & __GFP_WAIT))
2728 return;
2729
2730 /* this guy won't enter reclaim */
2731 if ((curr->flags & PF_MEMALLOC) && !(gfp_mask & __GFP_NOMEMALLOC))
2732 return;
2733
2734 /* We're only interested __GFP_FS allocations for now */
2735 if (!(gfp_mask & __GFP_FS))
2736 return;
2737
Peter Zijlstra0119fee2011-09-02 01:30:29 +02002738 /*
2739 * Oi! Can't be having __GFP_FS allocations with IRQs disabled.
2740 */
Peter Zijlstra2f850182009-03-20 11:13:20 +01002741 if (DEBUG_LOCKS_WARN_ON(irqs_disabled_flags(flags)))
Nick Piggincf40bd12009-01-21 08:12:39 +01002742 return;
2743
2744 mark_held_locks(curr, RECLAIM_FS);
2745}
2746
Peter Zijlstra2f850182009-03-20 11:13:20 +01002747static void check_flags(unsigned long flags);
2748
2749void lockdep_trace_alloc(gfp_t gfp_mask)
2750{
2751 unsigned long flags;
2752
2753 if (unlikely(current->lockdep_recursion))
2754 return;
2755
2756 raw_local_irq_save(flags);
2757 check_flags(flags);
2758 current->lockdep_recursion = 1;
2759 __lockdep_trace_alloc(gfp_mask, flags);
2760 current->lockdep_recursion = 0;
2761 raw_local_irq_restore(flags);
2762}
2763
Peter Zijlstra8e182572007-07-19 01:48:54 -07002764static int mark_irqflags(struct task_struct *curr, struct held_lock *hlock)
2765{
2766 /*
2767 * If non-trylock use in a hardirq or softirq context, then
2768 * mark the lock as used in these contexts:
2769 */
2770 if (!hlock->trylock) {
2771 if (hlock->read) {
2772 if (curr->hardirq_context)
2773 if (!mark_lock(curr, hlock,
2774 LOCK_USED_IN_HARDIRQ_READ))
2775 return 0;
2776 if (curr->softirq_context)
2777 if (!mark_lock(curr, hlock,
2778 LOCK_USED_IN_SOFTIRQ_READ))
2779 return 0;
2780 } else {
2781 if (curr->hardirq_context)
2782 if (!mark_lock(curr, hlock, LOCK_USED_IN_HARDIRQ))
2783 return 0;
2784 if (curr->softirq_context)
2785 if (!mark_lock(curr, hlock, LOCK_USED_IN_SOFTIRQ))
2786 return 0;
2787 }
2788 }
2789 if (!hlock->hardirqs_off) {
2790 if (hlock->read) {
2791 if (!mark_lock(curr, hlock,
Peter Zijlstra4fc95e82009-01-22 13:10:52 +01002792 LOCK_ENABLED_HARDIRQ_READ))
Peter Zijlstra8e182572007-07-19 01:48:54 -07002793 return 0;
2794 if (curr->softirqs_enabled)
2795 if (!mark_lock(curr, hlock,
Peter Zijlstra4fc95e82009-01-22 13:10:52 +01002796 LOCK_ENABLED_SOFTIRQ_READ))
Peter Zijlstra8e182572007-07-19 01:48:54 -07002797 return 0;
2798 } else {
2799 if (!mark_lock(curr, hlock,
Peter Zijlstra4fc95e82009-01-22 13:10:52 +01002800 LOCK_ENABLED_HARDIRQ))
Peter Zijlstra8e182572007-07-19 01:48:54 -07002801 return 0;
2802 if (curr->softirqs_enabled)
2803 if (!mark_lock(curr, hlock,
Peter Zijlstra4fc95e82009-01-22 13:10:52 +01002804 LOCK_ENABLED_SOFTIRQ))
Peter Zijlstra8e182572007-07-19 01:48:54 -07002805 return 0;
2806 }
2807 }
2808
Nick Piggincf40bd12009-01-21 08:12:39 +01002809 /*
2810 * We reuse the irq context infrastructure more broadly as a general
2811 * context checking code. This tests GFP_FS recursion (a lock taken
2812 * during reclaim for a GFP_FS allocation is held over a GFP_FS
2813 * allocation).
2814 */
2815 if (!hlock->trylock && (curr->lockdep_reclaim_gfp & __GFP_FS)) {
2816 if (hlock->read) {
2817 if (!mark_lock(curr, hlock, LOCK_USED_IN_RECLAIM_FS_READ))
2818 return 0;
2819 } else {
2820 if (!mark_lock(curr, hlock, LOCK_USED_IN_RECLAIM_FS))
2821 return 0;
2822 }
2823 }
2824
Peter Zijlstra8e182572007-07-19 01:48:54 -07002825 return 1;
2826}
2827
2828static int separate_irq_context(struct task_struct *curr,
2829 struct held_lock *hlock)
2830{
2831 unsigned int depth = curr->lockdep_depth;
2832
2833 /*
2834 * Keep track of points where we cross into an interrupt context:
2835 */
2836 hlock->irq_context = 2*(curr->hardirq_context ? 1 : 0) +
2837 curr->softirq_context;
2838 if (depth) {
2839 struct held_lock *prev_hlock;
2840
2841 prev_hlock = curr->held_locks + depth-1;
2842 /*
2843 * If we cross into another context, reset the
2844 * hash key (this also prevents the checking and the
2845 * adding of the dependency to 'prev'):
2846 */
2847 if (prev_hlock->irq_context != hlock->irq_context)
2848 return 1;
2849 }
2850 return 0;
2851}
2852
Peter Zijlstra0119fee2011-09-02 01:30:29 +02002853#else /* defined(CONFIG_TRACE_IRQFLAGS) && defined(CONFIG_PROVE_LOCKING) */
Peter Zijlstra8e182572007-07-19 01:48:54 -07002854
2855static inline
2856int mark_lock_irq(struct task_struct *curr, struct held_lock *this,
2857 enum lock_usage_bit new_bit)
2858{
Peter Zijlstra0119fee2011-09-02 01:30:29 +02002859 WARN_ON(1); /* Impossible innit? when we don't have TRACE_IRQFLAG */
Peter Zijlstra8e182572007-07-19 01:48:54 -07002860 return 1;
2861}
2862
2863static inline int mark_irqflags(struct task_struct *curr,
2864 struct held_lock *hlock)
2865{
2866 return 1;
2867}
2868
2869static inline int separate_irq_context(struct task_struct *curr,
2870 struct held_lock *hlock)
2871{
2872 return 0;
2873}
2874
Peter Zijlstra868a23a2009-02-15 00:25:21 +01002875void lockdep_trace_alloc(gfp_t gfp_mask)
2876{
2877}
2878
Peter Zijlstra0119fee2011-09-02 01:30:29 +02002879#endif /* defined(CONFIG_TRACE_IRQFLAGS) && defined(CONFIG_PROVE_LOCKING) */
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002880
2881/*
Peter Zijlstra8e182572007-07-19 01:48:54 -07002882 * Mark a lock with a usage bit, and validate the state transition:
2883 */
Steven Rostedt1d09daa2008-05-12 21:20:55 +02002884static int mark_lock(struct task_struct *curr, struct held_lock *this,
Steven Rostedt0764d232008-05-12 21:20:44 +02002885 enum lock_usage_bit new_bit)
Peter Zijlstra8e182572007-07-19 01:48:54 -07002886{
2887 unsigned int new_mask = 1 << new_bit, ret = 1;
2888
2889 /*
2890 * If already set then do not dirty the cacheline,
2891 * nor do any checks:
2892 */
Dave Jonesf82b2172008-08-11 09:30:23 +02002893 if (likely(hlock_class(this)->usage_mask & new_mask))
Peter Zijlstra8e182572007-07-19 01:48:54 -07002894 return 1;
2895
2896 if (!graph_lock())
2897 return 0;
2898 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002899 * Make sure we didn't race:
Peter Zijlstra8e182572007-07-19 01:48:54 -07002900 */
Dave Jonesf82b2172008-08-11 09:30:23 +02002901 if (unlikely(hlock_class(this)->usage_mask & new_mask)) {
Peter Zijlstra8e182572007-07-19 01:48:54 -07002902 graph_unlock();
2903 return 1;
2904 }
2905
Dave Jonesf82b2172008-08-11 09:30:23 +02002906 hlock_class(this)->usage_mask |= new_mask;
Peter Zijlstra8e182572007-07-19 01:48:54 -07002907
Dave Jonesf82b2172008-08-11 09:30:23 +02002908 if (!save_trace(hlock_class(this)->usage_traces + new_bit))
Peter Zijlstra8e182572007-07-19 01:48:54 -07002909 return 0;
2910
2911 switch (new_bit) {
Peter Zijlstra53464172009-01-22 14:15:53 +01002912#define LOCKDEP_STATE(__STATE) \
2913 case LOCK_USED_IN_##__STATE: \
2914 case LOCK_USED_IN_##__STATE##_READ: \
2915 case LOCK_ENABLED_##__STATE: \
2916 case LOCK_ENABLED_##__STATE##_READ:
2917#include "lockdep_states.h"
2918#undef LOCKDEP_STATE
Peter Zijlstra8e182572007-07-19 01:48:54 -07002919 ret = mark_lock_irq(curr, this, new_bit);
2920 if (!ret)
2921 return 0;
2922 break;
2923 case LOCK_USED:
Frederic Weisbeckerbd6d29c2010-04-06 00:10:17 +02002924 debug_atomic_dec(nr_unused_locks);
Peter Zijlstra8e182572007-07-19 01:48:54 -07002925 break;
2926 default:
2927 if (!debug_locks_off_graph_unlock())
2928 return 0;
2929 WARN_ON(1);
2930 return 0;
2931 }
2932
2933 graph_unlock();
2934
2935 /*
2936 * We must printk outside of the graph_lock:
2937 */
2938 if (ret == 2) {
2939 printk("\nmarked lock as {%s}:\n", usage_str[new_bit]);
2940 print_lock(this);
2941 print_irqtrace_events(curr);
2942 dump_stack();
2943 }
2944
2945 return ret;
2946}
2947
2948/*
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002949 * Initialize a lock instance's lock-class mapping info:
2950 */
2951void lockdep_init_map(struct lockdep_map *lock, const char *name,
Peter Zijlstra4dfbb9d2006-10-11 01:45:14 -04002952 struct lock_class_key *key, int subclass)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002953{
Yong Zhangd3d03d42011-11-09 16:04:51 +08002954 int i;
2955
2956 kmemcheck_mark_initialized(lock, sizeof(*lock));
2957
2958 for (i = 0; i < NR_LOCKDEP_CACHING_CLASSES; i++)
2959 lock->class_cache[i] = NULL;
Hitoshi Mitake62016252010-10-05 18:01:51 +09002960
Peter Zijlstrac8a25002009-04-17 09:40:49 +02002961#ifdef CONFIG_LOCK_STAT
2962 lock->cpu = raw_smp_processor_id();
2963#endif
2964
Peter Zijlstra0119fee2011-09-02 01:30:29 +02002965 /*
2966 * Can't be having no nameless bastards around this place!
2967 */
Peter Zijlstrac8a25002009-04-17 09:40:49 +02002968 if (DEBUG_LOCKS_WARN_ON(!name)) {
2969 lock->name = "NULL";
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002970 return;
Peter Zijlstrac8a25002009-04-17 09:40:49 +02002971 }
2972
2973 lock->name = name;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002974
Peter Zijlstra0119fee2011-09-02 01:30:29 +02002975 /*
2976 * No key, no joy, we need to hash something.
2977 */
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002978 if (DEBUG_LOCKS_WARN_ON(!key))
2979 return;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002980 /*
2981 * Sanity check, the lock-class key must be persistent:
2982 */
2983 if (!static_obj(key)) {
2984 printk("BUG: key %p not in .data!\n", key);
Peter Zijlstra0119fee2011-09-02 01:30:29 +02002985 /*
2986 * What it says above ^^^^^, I suggest you read it.
2987 */
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002988 DEBUG_LOCKS_WARN_ON(1);
2989 return;
2990 }
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002991 lock->key = key;
Peter Zijlstrac8a25002009-04-17 09:40:49 +02002992
2993 if (unlikely(!debug_locks))
2994 return;
2995
Peter Zijlstra4dfbb9d2006-10-11 01:45:14 -04002996 if (subclass)
2997 register_lock_class(lock, subclass, 1);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002998}
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002999EXPORT_SYMBOL_GPL(lockdep_init_map);
3000
Peter Zijlstra1704f472010-03-19 01:37:42 +01003001struct lock_class_key __lockdep_no_validate__;
3002
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003003/*
3004 * This gets called for every mutex_lock*()/spin_lock*() operation.
3005 * We maintain the dependency maps and validate the locking attempt:
3006 */
3007static int __lock_acquire(struct lockdep_map *lock, unsigned int subclass,
3008 int trylock, int read, int check, int hardirqs_off,
Peter Zijlstrabb97a912009-07-20 19:15:35 +02003009 struct lockdep_map *nest_lock, unsigned long ip,
3010 int references)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003011{
3012 struct task_struct *curr = current;
Ingo Molnard6d897c2006-07-10 04:44:04 -07003013 struct lock_class *class = NULL;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003014 struct held_lock *hlock;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003015 unsigned int depth, id;
3016 int chain_head = 0;
Peter Zijlstrabb97a912009-07-20 19:15:35 +02003017 int class_idx;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003018 u64 chain_key;
3019
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003020 if (!prove_locking)
3021 check = 1;
3022
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003023 if (unlikely(!debug_locks))
3024 return 0;
3025
Peter Zijlstra0119fee2011-09-02 01:30:29 +02003026 /*
3027 * Lockdep should run with IRQs disabled, otherwise we could
3028 * get an interrupt which would want to take locks, which would
3029 * end up in lockdep and have you got a head-ache already?
3030 */
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003031 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
3032 return 0;
3033
Peter Zijlstra1704f472010-03-19 01:37:42 +01003034 if (lock->key == &__lockdep_no_validate__)
3035 check = 1;
3036
Hitoshi Mitake62016252010-10-05 18:01:51 +09003037 if (subclass < NR_LOCKDEP_CACHING_CLASSES)
3038 class = lock->class_cache[subclass];
Ingo Molnard6d897c2006-07-10 04:44:04 -07003039 /*
Hitoshi Mitake62016252010-10-05 18:01:51 +09003040 * Not cached?
Ingo Molnard6d897c2006-07-10 04:44:04 -07003041 */
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003042 if (unlikely(!class)) {
Peter Zijlstra4dfbb9d2006-10-11 01:45:14 -04003043 class = register_lock_class(lock, subclass, 0);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003044 if (!class)
3045 return 0;
3046 }
Frederic Weisbeckerbd6d29c2010-04-06 00:10:17 +02003047 atomic_inc((atomic_t *)&class->ops);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003048 if (very_verbose(class)) {
3049 printk("\nacquire class [%p] %s", class->key, class->name);
3050 if (class->name_version > 1)
3051 printk("#%d", class->name_version);
3052 printk("\n");
3053 dump_stack();
3054 }
3055
3056 /*
3057 * Add the lock to the list of currently held locks.
3058 * (we dont increase the depth just yet, up until the
3059 * dependency checks are done)
3060 */
3061 depth = curr->lockdep_depth;
Peter Zijlstra0119fee2011-09-02 01:30:29 +02003062 /*
3063 * Ran out of static storage for our per-task lock stack again have we?
3064 */
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003065 if (DEBUG_LOCKS_WARN_ON(depth >= MAX_LOCK_DEPTH))
3066 return 0;
3067
Peter Zijlstrabb97a912009-07-20 19:15:35 +02003068 class_idx = class - lock_classes + 1;
3069
3070 if (depth) {
3071 hlock = curr->held_locks + depth - 1;
3072 if (hlock->class_idx == class_idx && nest_lock) {
3073 if (hlock->references)
3074 hlock->references++;
3075 else
3076 hlock->references = 2;
3077
3078 return 1;
3079 }
3080 }
3081
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003082 hlock = curr->held_locks + depth;
Peter Zijlstra0119fee2011-09-02 01:30:29 +02003083 /*
3084 * Plain impossible, we just registered it and checked it weren't no
3085 * NULL like.. I bet this mushroom I ate was good!
3086 */
Dave Jonesf82b2172008-08-11 09:30:23 +02003087 if (DEBUG_LOCKS_WARN_ON(!class))
3088 return 0;
Peter Zijlstrabb97a912009-07-20 19:15:35 +02003089 hlock->class_idx = class_idx;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003090 hlock->acquire_ip = ip;
3091 hlock->instance = lock;
Peter Zijlstra7531e2f2008-08-11 09:30:24 +02003092 hlock->nest_lock = nest_lock;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003093 hlock->trylock = trylock;
3094 hlock->read = read;
3095 hlock->check = check;
Dmitry Baryshkov6951b122008-08-18 04:26:37 +04003096 hlock->hardirqs_off = !!hardirqs_off;
Peter Zijlstrabb97a912009-07-20 19:15:35 +02003097 hlock->references = references;
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003098#ifdef CONFIG_LOCK_STAT
3099 hlock->waittime_stamp = 0;
Peter Zijlstra3365e7792009-10-09 10:12:41 +02003100 hlock->holdtime_stamp = lockstat_clock();
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003101#endif
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003102
Peter Zijlstra8e182572007-07-19 01:48:54 -07003103 if (check == 2 && !mark_irqflags(curr, hlock))
3104 return 0;
3105
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003106 /* mark it as used: */
Jarek Poplawski4ff773bb2007-05-08 00:31:00 -07003107 if (!mark_lock(curr, hlock, LOCK_USED))
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003108 return 0;
Peter Zijlstra8e182572007-07-19 01:48:54 -07003109
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003110 /*
Gautham R Shenoy17aacfb2007-10-28 20:47:01 +01003111 * Calculate the chain hash: it's the combined hash of all the
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003112 * lock keys along the dependency chain. We save the hash value
3113 * at every step so that we can get the current hash easily
3114 * after unlock. The chain hash is then used to cache dependency
3115 * results.
3116 *
3117 * The 'key ID' is what is the most compact key value to drive
3118 * the hash, not class->key.
3119 */
3120 id = class - lock_classes;
Peter Zijlstra0119fee2011-09-02 01:30:29 +02003121 /*
3122 * Whoops, we did it again.. ran straight out of our static allocation.
3123 */
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003124 if (DEBUG_LOCKS_WARN_ON(id >= MAX_LOCKDEP_KEYS))
3125 return 0;
3126
3127 chain_key = curr->curr_chain_key;
3128 if (!depth) {
Peter Zijlstra0119fee2011-09-02 01:30:29 +02003129 /*
3130 * How can we have a chain hash when we ain't got no keys?!
3131 */
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003132 if (DEBUG_LOCKS_WARN_ON(chain_key != 0))
3133 return 0;
3134 chain_head = 1;
3135 }
3136
3137 hlock->prev_chain_key = chain_key;
Peter Zijlstra8e182572007-07-19 01:48:54 -07003138 if (separate_irq_context(curr, hlock)) {
3139 chain_key = 0;
3140 chain_head = 1;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003141 }
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003142 chain_key = iterate_chain_key(chain_key, id);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003143
Gregory Haskins3aa416b2007-10-11 22:11:11 +02003144 if (!validate_chain(curr, lock, hlock, chain_head, chain_key))
Peter Zijlstra8e182572007-07-19 01:48:54 -07003145 return 0;
Jarek Poplawski381a2292007-02-10 01:44:58 -08003146
Gregory Haskins3aa416b2007-10-11 22:11:11 +02003147 curr->curr_chain_key = chain_key;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003148 curr->lockdep_depth++;
3149 check_chain_key(curr);
Jarek Poplawski60e114d2007-02-20 13:58:00 -08003150#ifdef CONFIG_DEBUG_LOCKDEP
3151 if (unlikely(!debug_locks))
3152 return 0;
3153#endif
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003154 if (unlikely(curr->lockdep_depth >= MAX_LOCK_DEPTH)) {
3155 debug_locks_off();
3156 printk("BUG: MAX_LOCK_DEPTH too low!\n");
3157 printk("turning off the locking correctness validator.\n");
Peter Zijlstraeedeeab2009-03-18 12:38:47 +01003158 dump_stack();
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003159 return 0;
3160 }
Jarek Poplawski381a2292007-02-10 01:44:58 -08003161
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003162 if (unlikely(curr->lockdep_depth > max_lockdep_depth))
3163 max_lockdep_depth = curr->lockdep_depth;
3164
3165 return 1;
3166}
3167
3168static int
3169print_unlock_inbalance_bug(struct task_struct *curr, struct lockdep_map *lock,
3170 unsigned long ip)
3171{
3172 if (!debug_locks_off())
3173 return 0;
3174 if (debug_locks_silent)
3175 return 0;
3176
Paul E. McKenneyb3fbab02011-05-24 08:31:09 -07003177 printk("\n");
3178 printk("=====================================\n");
3179 printk("[ BUG: bad unlock balance detected! ]\n");
Ben Hutchingsfbdc4b92011-10-28 04:36:55 +01003180 print_kernel_ident();
Paul E. McKenneyb3fbab02011-05-24 08:31:09 -07003181 printk("-------------------------------------\n");
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003182 printk("%s/%d is trying to release lock (",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07003183 curr->comm, task_pid_nr(curr));
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003184 print_lockdep_cache(lock);
3185 printk(") at:\n");
3186 print_ip_sym(ip);
3187 printk("but there are no more locks to release!\n");
3188 printk("\nother info that might help us debug this:\n");
3189 lockdep_print_held_locks(curr);
3190
3191 printk("\nstack backtrace:\n");
3192 dump_stack();
3193
3194 return 0;
3195}
3196
3197/*
3198 * Common debugging checks for both nested and non-nested unlock:
3199 */
3200static int check_unlock(struct task_struct *curr, struct lockdep_map *lock,
3201 unsigned long ip)
3202{
3203 if (unlikely(!debug_locks))
3204 return 0;
Peter Zijlstra0119fee2011-09-02 01:30:29 +02003205 /*
3206 * Lockdep should run with IRQs disabled, recursion, head-ache, etc..
3207 */
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003208 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
3209 return 0;
3210
3211 if (curr->lockdep_depth <= 0)
3212 return print_unlock_inbalance_bug(curr, lock, ip);
3213
3214 return 1;
3215}
3216
Peter Zijlstrabb97a912009-07-20 19:15:35 +02003217static int match_held_lock(struct held_lock *hlock, struct lockdep_map *lock)
3218{
3219 if (hlock->instance == lock)
3220 return 1;
3221
3222 if (hlock->references) {
Hitoshi Mitake62016252010-10-05 18:01:51 +09003223 struct lock_class *class = lock->class_cache[0];
Peter Zijlstrabb97a912009-07-20 19:15:35 +02003224
3225 if (!class)
3226 class = look_up_lock_class(lock, 0);
3227
Peter Zijlstra80e04012011-08-05 14:26:17 +02003228 /*
3229 * If look_up_lock_class() failed to find a class, we're trying
3230 * to test if we hold a lock that has never yet been acquired.
3231 * Clearly if the lock hasn't been acquired _ever_, we're not
3232 * holding it either, so report failure.
3233 */
3234 if (!class)
Peter Zijlstrabb97a912009-07-20 19:15:35 +02003235 return 0;
3236
Peter Zijlstra0119fee2011-09-02 01:30:29 +02003237 /*
3238 * References, but not a lock we're actually ref-counting?
3239 * State got messed up, follow the sites that change ->references
3240 * and try to make sense of it.
3241 */
Peter Zijlstrabb97a912009-07-20 19:15:35 +02003242 if (DEBUG_LOCKS_WARN_ON(!hlock->nest_lock))
3243 return 0;
3244
3245 if (hlock->class_idx == class - lock_classes + 1)
3246 return 1;
3247 }
3248
3249 return 0;
3250}
3251
Peter Zijlstra64aa3482008-08-11 09:30:21 +02003252static int
Peter Zijlstra00ef9f72008-12-04 09:00:17 +01003253__lock_set_class(struct lockdep_map *lock, const char *name,
3254 struct lock_class_key *key, unsigned int subclass,
3255 unsigned long ip)
Peter Zijlstra64aa3482008-08-11 09:30:21 +02003256{
3257 struct task_struct *curr = current;
3258 struct held_lock *hlock, *prev_hlock;
3259 struct lock_class *class;
3260 unsigned int depth;
3261 int i;
3262
3263 depth = curr->lockdep_depth;
Peter Zijlstra0119fee2011-09-02 01:30:29 +02003264 /*
3265 * This function is about (re)setting the class of a held lock,
3266 * yet we're not actually holding any locks. Naughty user!
3267 */
Peter Zijlstra64aa3482008-08-11 09:30:21 +02003268 if (DEBUG_LOCKS_WARN_ON(!depth))
3269 return 0;
3270
3271 prev_hlock = NULL;
3272 for (i = depth-1; i >= 0; i--) {
3273 hlock = curr->held_locks + i;
3274 /*
3275 * We must not cross into another context:
3276 */
3277 if (prev_hlock && prev_hlock->irq_context != hlock->irq_context)
3278 break;
Peter Zijlstrabb97a912009-07-20 19:15:35 +02003279 if (match_held_lock(hlock, lock))
Peter Zijlstra64aa3482008-08-11 09:30:21 +02003280 goto found_it;
3281 prev_hlock = hlock;
3282 }
3283 return print_unlock_inbalance_bug(curr, lock, ip);
3284
3285found_it:
Peter Zijlstra00ef9f72008-12-04 09:00:17 +01003286 lockdep_init_map(lock, name, key, 0);
Peter Zijlstra64aa3482008-08-11 09:30:21 +02003287 class = register_lock_class(lock, subclass, 0);
Dave Jonesf82b2172008-08-11 09:30:23 +02003288 hlock->class_idx = class - lock_classes + 1;
Peter Zijlstra64aa3482008-08-11 09:30:21 +02003289
3290 curr->lockdep_depth = i;
3291 curr->curr_chain_key = hlock->prev_chain_key;
3292
3293 for (; i < depth; i++) {
3294 hlock = curr->held_locks + i;
3295 if (!__lock_acquire(hlock->instance,
Dave Jonesf82b2172008-08-11 09:30:23 +02003296 hlock_class(hlock)->subclass, hlock->trylock,
Peter Zijlstra64aa3482008-08-11 09:30:21 +02003297 hlock->read, hlock->check, hlock->hardirqs_off,
Peter Zijlstrabb97a912009-07-20 19:15:35 +02003298 hlock->nest_lock, hlock->acquire_ip,
3299 hlock->references))
Peter Zijlstra64aa3482008-08-11 09:30:21 +02003300 return 0;
3301 }
3302
Peter Zijlstra0119fee2011-09-02 01:30:29 +02003303 /*
3304 * I took it apart and put it back together again, except now I have
3305 * these 'spare' parts.. where shall I put them.
3306 */
Peter Zijlstra64aa3482008-08-11 09:30:21 +02003307 if (DEBUG_LOCKS_WARN_ON(curr->lockdep_depth != depth))
3308 return 0;
3309 return 1;
3310}
3311
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003312/*
3313 * Remove the lock to the list of currently held locks in a
3314 * potentially non-nested (out of order) manner. This is a
3315 * relatively rare operation, as all the unlock APIs default
3316 * to nested mode (which uses lock_release()):
3317 */
3318static int
3319lock_release_non_nested(struct task_struct *curr,
3320 struct lockdep_map *lock, unsigned long ip)
3321{
3322 struct held_lock *hlock, *prev_hlock;
3323 unsigned int depth;
3324 int i;
3325
3326 /*
3327 * Check whether the lock exists in the current stack
3328 * of held locks:
3329 */
3330 depth = curr->lockdep_depth;
Peter Zijlstra0119fee2011-09-02 01:30:29 +02003331 /*
3332 * So we're all set to release this lock.. wait what lock? We don't
3333 * own any locks, you've been drinking again?
3334 */
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003335 if (DEBUG_LOCKS_WARN_ON(!depth))
3336 return 0;
3337
3338 prev_hlock = NULL;
3339 for (i = depth-1; i >= 0; i--) {
3340 hlock = curr->held_locks + i;
3341 /*
3342 * We must not cross into another context:
3343 */
3344 if (prev_hlock && prev_hlock->irq_context != hlock->irq_context)
3345 break;
Peter Zijlstrabb97a912009-07-20 19:15:35 +02003346 if (match_held_lock(hlock, lock))
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003347 goto found_it;
3348 prev_hlock = hlock;
3349 }
3350 return print_unlock_inbalance_bug(curr, lock, ip);
3351
3352found_it:
Peter Zijlstrabb97a912009-07-20 19:15:35 +02003353 if (hlock->instance == lock)
3354 lock_release_holdtime(hlock);
3355
3356 if (hlock->references) {
3357 hlock->references--;
3358 if (hlock->references) {
3359 /*
3360 * We had, and after removing one, still have
3361 * references, the current lock stack is still
3362 * valid. We're done!
3363 */
3364 return 1;
3365 }
3366 }
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003367
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003368 /*
3369 * We have the right lock to unlock, 'hlock' points to it.
3370 * Now we remove it from the stack, and add back the other
3371 * entries (if any), recalculating the hash along the way:
3372 */
Peter Zijlstrabb97a912009-07-20 19:15:35 +02003373
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003374 curr->lockdep_depth = i;
3375 curr->curr_chain_key = hlock->prev_chain_key;
3376
3377 for (i++; i < depth; i++) {
3378 hlock = curr->held_locks + i;
3379 if (!__lock_acquire(hlock->instance,
Dave Jonesf82b2172008-08-11 09:30:23 +02003380 hlock_class(hlock)->subclass, hlock->trylock,
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003381 hlock->read, hlock->check, hlock->hardirqs_off,
Peter Zijlstrabb97a912009-07-20 19:15:35 +02003382 hlock->nest_lock, hlock->acquire_ip,
3383 hlock->references))
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003384 return 0;
3385 }
3386
Peter Zijlstra0119fee2011-09-02 01:30:29 +02003387 /*
3388 * We had N bottles of beer on the wall, we drank one, but now
3389 * there's not N-1 bottles of beer left on the wall...
3390 */
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003391 if (DEBUG_LOCKS_WARN_ON(curr->lockdep_depth != depth - 1))
3392 return 0;
3393 return 1;
3394}
3395
3396/*
3397 * Remove the lock to the list of currently held locks - this gets
3398 * called on mutex_unlock()/spin_unlock*() (or on a failed
3399 * mutex_lock_interruptible()). This is done for unlocks that nest
3400 * perfectly. (i.e. the current top of the lock-stack is unlocked)
3401 */
3402static int lock_release_nested(struct task_struct *curr,
3403 struct lockdep_map *lock, unsigned long ip)
3404{
3405 struct held_lock *hlock;
3406 unsigned int depth;
3407
3408 /*
3409 * Pop off the top of the lock stack:
3410 */
3411 depth = curr->lockdep_depth - 1;
3412 hlock = curr->held_locks + depth;
3413
3414 /*
3415 * Is the unlock non-nested:
3416 */
Peter Zijlstrabb97a912009-07-20 19:15:35 +02003417 if (hlock->instance != lock || hlock->references)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003418 return lock_release_non_nested(curr, lock, ip);
3419 curr->lockdep_depth--;
3420
Peter Zijlstra0119fee2011-09-02 01:30:29 +02003421 /*
3422 * No more locks, but somehow we've got hash left over, who left it?
3423 */
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003424 if (DEBUG_LOCKS_WARN_ON(!depth && (hlock->prev_chain_key != 0)))
3425 return 0;
3426
3427 curr->curr_chain_key = hlock->prev_chain_key;
3428
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003429 lock_release_holdtime(hlock);
3430
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003431#ifdef CONFIG_DEBUG_LOCKDEP
3432 hlock->prev_chain_key = 0;
Dave Jonesf82b2172008-08-11 09:30:23 +02003433 hlock->class_idx = 0;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003434 hlock->acquire_ip = 0;
3435 hlock->irq_context = 0;
3436#endif
3437 return 1;
3438}
3439
3440/*
3441 * Remove the lock to the list of currently held locks - this gets
3442 * called on mutex_unlock()/spin_unlock*() (or on a failed
3443 * mutex_lock_interruptible()). This is done for unlocks that nest
3444 * perfectly. (i.e. the current top of the lock-stack is unlocked)
3445 */
3446static void
3447__lock_release(struct lockdep_map *lock, int nested, unsigned long ip)
3448{
3449 struct task_struct *curr = current;
3450
3451 if (!check_unlock(curr, lock, ip))
3452 return;
3453
3454 if (nested) {
3455 if (!lock_release_nested(curr, lock, ip))
3456 return;
3457 } else {
3458 if (!lock_release_non_nested(curr, lock, ip))
3459 return;
3460 }
3461
3462 check_chain_key(curr);
3463}
3464
Peter Zijlstraf607c662009-07-20 19:16:29 +02003465static int __lock_is_held(struct lockdep_map *lock)
3466{
3467 struct task_struct *curr = current;
3468 int i;
3469
3470 for (i = 0; i < curr->lockdep_depth; i++) {
Peter Zijlstrabb97a912009-07-20 19:15:35 +02003471 struct held_lock *hlock = curr->held_locks + i;
3472
3473 if (match_held_lock(hlock, lock))
Peter Zijlstraf607c662009-07-20 19:16:29 +02003474 return 1;
3475 }
3476
3477 return 0;
3478}
3479
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003480/*
3481 * Check whether we follow the irq-flags state precisely:
3482 */
Steven Rostedt1d09daa2008-05-12 21:20:55 +02003483static void check_flags(unsigned long flags)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003484{
Ingo Molnar992860e2008-07-14 10:28:38 +02003485#if defined(CONFIG_PROVE_LOCKING) && defined(CONFIG_DEBUG_LOCKDEP) && \
3486 defined(CONFIG_TRACE_IRQFLAGS)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003487 if (!debug_locks)
3488 return;
3489
Ingo Molnar5f9fa8a2007-12-07 19:02:47 +01003490 if (irqs_disabled_flags(flags)) {
3491 if (DEBUG_LOCKS_WARN_ON(current->hardirqs_enabled)) {
3492 printk("possible reason: unannotated irqs-off.\n");
3493 }
3494 } else {
3495 if (DEBUG_LOCKS_WARN_ON(!current->hardirqs_enabled)) {
3496 printk("possible reason: unannotated irqs-on.\n");
3497 }
3498 }
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003499
3500 /*
3501 * We dont accurately track softirq state in e.g.
3502 * hardirq contexts (such as on 4KSTACKS), so only
3503 * check if not in hardirq contexts:
3504 */
3505 if (!hardirq_count()) {
Peter Zijlstra0119fee2011-09-02 01:30:29 +02003506 if (softirq_count()) {
3507 /* like the above, but with softirqs */
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003508 DEBUG_LOCKS_WARN_ON(current->softirqs_enabled);
Peter Zijlstra0119fee2011-09-02 01:30:29 +02003509 } else {
3510 /* lick the above, does it taste good? */
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003511 DEBUG_LOCKS_WARN_ON(!current->softirqs_enabled);
Peter Zijlstra0119fee2011-09-02 01:30:29 +02003512 }
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003513 }
3514
3515 if (!debug_locks)
3516 print_irqtrace_events(current);
3517#endif
3518}
3519
Peter Zijlstra00ef9f72008-12-04 09:00:17 +01003520void lock_set_class(struct lockdep_map *lock, const char *name,
3521 struct lock_class_key *key, unsigned int subclass,
3522 unsigned long ip)
Peter Zijlstra64aa3482008-08-11 09:30:21 +02003523{
3524 unsigned long flags;
3525
3526 if (unlikely(current->lockdep_recursion))
3527 return;
3528
3529 raw_local_irq_save(flags);
3530 current->lockdep_recursion = 1;
3531 check_flags(flags);
Peter Zijlstra00ef9f72008-12-04 09:00:17 +01003532 if (__lock_set_class(lock, name, key, subclass, ip))
Peter Zijlstra64aa3482008-08-11 09:30:21 +02003533 check_chain_key(current);
3534 current->lockdep_recursion = 0;
3535 raw_local_irq_restore(flags);
3536}
Peter Zijlstra00ef9f72008-12-04 09:00:17 +01003537EXPORT_SYMBOL_GPL(lock_set_class);
Peter Zijlstra64aa3482008-08-11 09:30:21 +02003538
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003539/*
3540 * We are not always called with irqs disabled - do that here,
3541 * and also avoid lockdep recursion:
3542 */
Steven Rostedt1d09daa2008-05-12 21:20:55 +02003543void lock_acquire(struct lockdep_map *lock, unsigned int subclass,
Peter Zijlstra7531e2f2008-08-11 09:30:24 +02003544 int trylock, int read, int check,
3545 struct lockdep_map *nest_lock, unsigned long ip)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003546{
3547 unsigned long flags;
3548
3549 if (unlikely(current->lockdep_recursion))
3550 return;
3551
3552 raw_local_irq_save(flags);
3553 check_flags(flags);
3554
3555 current->lockdep_recursion = 1;
Frederic Weisbeckerdb2c4c72010-02-02 23:34:40 +01003556 trace_lock_acquire(lock, subclass, trylock, read, check, nest_lock, ip);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003557 __lock_acquire(lock, subclass, trylock, read, check,
Peter Zijlstrabb97a912009-07-20 19:15:35 +02003558 irqs_disabled_flags(flags), nest_lock, ip, 0);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003559 current->lockdep_recursion = 0;
3560 raw_local_irq_restore(flags);
3561}
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003562EXPORT_SYMBOL_GPL(lock_acquire);
3563
Steven Rostedt1d09daa2008-05-12 21:20:55 +02003564void lock_release(struct lockdep_map *lock, int nested,
Steven Rostedt0764d232008-05-12 21:20:44 +02003565 unsigned long ip)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003566{
3567 unsigned long flags;
3568
3569 if (unlikely(current->lockdep_recursion))
3570 return;
3571
3572 raw_local_irq_save(flags);
3573 check_flags(flags);
3574 current->lockdep_recursion = 1;
Frederic Weisbecker93135432010-05-08 06:24:25 +02003575 trace_lock_release(lock, ip);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003576 __lock_release(lock, nested, ip);
3577 current->lockdep_recursion = 0;
3578 raw_local_irq_restore(flags);
3579}
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003580EXPORT_SYMBOL_GPL(lock_release);
3581
Peter Zijlstraf607c662009-07-20 19:16:29 +02003582int lock_is_held(struct lockdep_map *lock)
3583{
3584 unsigned long flags;
3585 int ret = 0;
3586
3587 if (unlikely(current->lockdep_recursion))
Peter Zijlstraf2513cd2011-06-06 12:32:43 +02003588 return 1; /* avoid false negative lockdep_assert_held() */
Peter Zijlstraf607c662009-07-20 19:16:29 +02003589
3590 raw_local_irq_save(flags);
3591 check_flags(flags);
3592
3593 current->lockdep_recursion = 1;
3594 ret = __lock_is_held(lock);
3595 current->lockdep_recursion = 0;
3596 raw_local_irq_restore(flags);
3597
3598 return ret;
3599}
3600EXPORT_SYMBOL_GPL(lock_is_held);
3601
Nick Piggincf40bd12009-01-21 08:12:39 +01003602void lockdep_set_current_reclaim_state(gfp_t gfp_mask)
3603{
3604 current->lockdep_reclaim_gfp = gfp_mask;
3605}
3606
3607void lockdep_clear_current_reclaim_state(void)
3608{
3609 current->lockdep_reclaim_gfp = 0;
3610}
3611
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003612#ifdef CONFIG_LOCK_STAT
3613static int
3614print_lock_contention_bug(struct task_struct *curr, struct lockdep_map *lock,
3615 unsigned long ip)
3616{
3617 if (!debug_locks_off())
3618 return 0;
3619 if (debug_locks_silent)
3620 return 0;
3621
Paul E. McKenneyb3fbab02011-05-24 08:31:09 -07003622 printk("\n");
3623 printk("=================================\n");
3624 printk("[ BUG: bad contention detected! ]\n");
Ben Hutchingsfbdc4b92011-10-28 04:36:55 +01003625 print_kernel_ident();
Paul E. McKenneyb3fbab02011-05-24 08:31:09 -07003626 printk("---------------------------------\n");
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003627 printk("%s/%d is trying to contend lock (",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07003628 curr->comm, task_pid_nr(curr));
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003629 print_lockdep_cache(lock);
3630 printk(") at:\n");
3631 print_ip_sym(ip);
3632 printk("but there are no locks held!\n");
3633 printk("\nother info that might help us debug this:\n");
3634 lockdep_print_held_locks(curr);
3635
3636 printk("\nstack backtrace:\n");
3637 dump_stack();
3638
3639 return 0;
3640}
3641
3642static void
3643__lock_contended(struct lockdep_map *lock, unsigned long ip)
3644{
3645 struct task_struct *curr = current;
3646 struct held_lock *hlock, *prev_hlock;
3647 struct lock_class_stats *stats;
3648 unsigned int depth;
Peter Zijlstrac7e78cf2008-10-16 23:17:09 +02003649 int i, contention_point, contending_point;
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003650
3651 depth = curr->lockdep_depth;
Peter Zijlstra0119fee2011-09-02 01:30:29 +02003652 /*
3653 * Whee, we contended on this lock, except it seems we're not
3654 * actually trying to acquire anything much at all..
3655 */
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003656 if (DEBUG_LOCKS_WARN_ON(!depth))
3657 return;
3658
3659 prev_hlock = NULL;
3660 for (i = depth-1; i >= 0; i--) {
3661 hlock = curr->held_locks + i;
3662 /*
3663 * We must not cross into another context:
3664 */
3665 if (prev_hlock && prev_hlock->irq_context != hlock->irq_context)
3666 break;
Peter Zijlstrabb97a912009-07-20 19:15:35 +02003667 if (match_held_lock(hlock, lock))
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003668 goto found_it;
3669 prev_hlock = hlock;
3670 }
3671 print_lock_contention_bug(curr, lock, ip);
3672 return;
3673
3674found_it:
Peter Zijlstrabb97a912009-07-20 19:15:35 +02003675 if (hlock->instance != lock)
3676 return;
3677
Peter Zijlstra3365e7792009-10-09 10:12:41 +02003678 hlock->waittime_stamp = lockstat_clock();
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003679
Peter Zijlstrac7e78cf2008-10-16 23:17:09 +02003680 contention_point = lock_point(hlock_class(hlock)->contention_point, ip);
3681 contending_point = lock_point(hlock_class(hlock)->contending_point,
3682 lock->ip);
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003683
Dave Jonesf82b2172008-08-11 09:30:23 +02003684 stats = get_lock_stats(hlock_class(hlock));
Peter Zijlstrac7e78cf2008-10-16 23:17:09 +02003685 if (contention_point < LOCKSTAT_POINTS)
3686 stats->contention_point[contention_point]++;
3687 if (contending_point < LOCKSTAT_POINTS)
3688 stats->contending_point[contending_point]++;
Peter Zijlstra96645672007-07-19 01:49:00 -07003689 if (lock->cpu != smp_processor_id())
3690 stats->bounces[bounce_contended + !!hlock->read]++;
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003691 put_lock_stats(stats);
3692}
3693
3694static void
Peter Zijlstrac7e78cf2008-10-16 23:17:09 +02003695__lock_acquired(struct lockdep_map *lock, unsigned long ip)
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003696{
3697 struct task_struct *curr = current;
3698 struct held_lock *hlock, *prev_hlock;
3699 struct lock_class_stats *stats;
3700 unsigned int depth;
Peter Zijlstra3365e7792009-10-09 10:12:41 +02003701 u64 now, waittime = 0;
Peter Zijlstra96645672007-07-19 01:49:00 -07003702 int i, cpu;
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003703
3704 depth = curr->lockdep_depth;
Peter Zijlstra0119fee2011-09-02 01:30:29 +02003705 /*
3706 * Yay, we acquired ownership of this lock we didn't try to
3707 * acquire, how the heck did that happen?
3708 */
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003709 if (DEBUG_LOCKS_WARN_ON(!depth))
3710 return;
3711
3712 prev_hlock = NULL;
3713 for (i = depth-1; i >= 0; i--) {
3714 hlock = curr->held_locks + i;
3715 /*
3716 * We must not cross into another context:
3717 */
3718 if (prev_hlock && prev_hlock->irq_context != hlock->irq_context)
3719 break;
Peter Zijlstrabb97a912009-07-20 19:15:35 +02003720 if (match_held_lock(hlock, lock))
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003721 goto found_it;
3722 prev_hlock = hlock;
3723 }
3724 print_lock_contention_bug(curr, lock, _RET_IP_);
3725 return;
3726
3727found_it:
Peter Zijlstrabb97a912009-07-20 19:15:35 +02003728 if (hlock->instance != lock)
3729 return;
3730
Peter Zijlstra96645672007-07-19 01:49:00 -07003731 cpu = smp_processor_id();
3732 if (hlock->waittime_stamp) {
Peter Zijlstra3365e7792009-10-09 10:12:41 +02003733 now = lockstat_clock();
Peter Zijlstra96645672007-07-19 01:49:00 -07003734 waittime = now - hlock->waittime_stamp;
3735 hlock->holdtime_stamp = now;
3736 }
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003737
Frederic Weisbecker883a2a32010-05-08 06:16:11 +02003738 trace_lock_acquired(lock, ip);
Frederic Weisbecker20625012009-04-06 01:49:33 +02003739
Dave Jonesf82b2172008-08-11 09:30:23 +02003740 stats = get_lock_stats(hlock_class(hlock));
Peter Zijlstra96645672007-07-19 01:49:00 -07003741 if (waittime) {
3742 if (hlock->read)
3743 lock_time_inc(&stats->read_waittime, waittime);
3744 else
3745 lock_time_inc(&stats->write_waittime, waittime);
3746 }
3747 if (lock->cpu != cpu)
3748 stats->bounces[bounce_acquired + !!hlock->read]++;
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003749 put_lock_stats(stats);
Peter Zijlstra96645672007-07-19 01:49:00 -07003750
3751 lock->cpu = cpu;
Peter Zijlstrac7e78cf2008-10-16 23:17:09 +02003752 lock->ip = ip;
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003753}
3754
3755void lock_contended(struct lockdep_map *lock, unsigned long ip)
3756{
3757 unsigned long flags;
3758
3759 if (unlikely(!lock_stat))
3760 return;
3761
3762 if (unlikely(current->lockdep_recursion))
3763 return;
3764
3765 raw_local_irq_save(flags);
3766 check_flags(flags);
3767 current->lockdep_recursion = 1;
Frederic Weisbeckerdb2c4c72010-02-02 23:34:40 +01003768 trace_lock_contended(lock, ip);
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003769 __lock_contended(lock, ip);
3770 current->lockdep_recursion = 0;
3771 raw_local_irq_restore(flags);
3772}
3773EXPORT_SYMBOL_GPL(lock_contended);
3774
Peter Zijlstrac7e78cf2008-10-16 23:17:09 +02003775void lock_acquired(struct lockdep_map *lock, unsigned long ip)
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003776{
3777 unsigned long flags;
3778
3779 if (unlikely(!lock_stat))
3780 return;
3781
3782 if (unlikely(current->lockdep_recursion))
3783 return;
3784
3785 raw_local_irq_save(flags);
3786 check_flags(flags);
3787 current->lockdep_recursion = 1;
Peter Zijlstrac7e78cf2008-10-16 23:17:09 +02003788 __lock_acquired(lock, ip);
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003789 current->lockdep_recursion = 0;
3790 raw_local_irq_restore(flags);
3791}
3792EXPORT_SYMBOL_GPL(lock_acquired);
3793#endif
3794
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003795/*
3796 * Used by the testsuite, sanitize the validator state
3797 * after a simulated failure:
3798 */
3799
3800void lockdep_reset(void)
3801{
3802 unsigned long flags;
Ingo Molnar23d95a02006-12-13 00:34:40 -08003803 int i;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003804
3805 raw_local_irq_save(flags);
3806 current->curr_chain_key = 0;
3807 current->lockdep_depth = 0;
3808 current->lockdep_recursion = 0;
3809 memset(current->held_locks, 0, MAX_LOCK_DEPTH*sizeof(struct held_lock));
3810 nr_hardirq_chains = 0;
3811 nr_softirq_chains = 0;
3812 nr_process_chains = 0;
3813 debug_locks = 1;
Ingo Molnar23d95a02006-12-13 00:34:40 -08003814 for (i = 0; i < CHAINHASH_SIZE; i++)
3815 INIT_LIST_HEAD(chainhash_table + i);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003816 raw_local_irq_restore(flags);
3817}
3818
3819static void zap_class(struct lock_class *class)
3820{
3821 int i;
3822
3823 /*
3824 * Remove all dependencies this lock is
3825 * involved in:
3826 */
3827 for (i = 0; i < nr_list_entries; i++) {
3828 if (list_entries[i].class == class)
3829 list_del_rcu(&list_entries[i].entry);
3830 }
3831 /*
3832 * Unhash the class and remove it from the all_lock_classes list:
3833 */
3834 list_del_rcu(&class->hash_entry);
3835 list_del_rcu(&class->lock_entry);
3836
Rabin Vincent8bfe0292008-08-11 09:30:26 +02003837 class->key = NULL;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003838}
3839
Arjan van de Venfabe8742008-01-24 07:00:45 +01003840static inline int within(const void *addr, void *start, unsigned long size)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003841{
3842 return addr >= start && addr < start + size;
3843}
3844
3845void lockdep_free_key_range(void *start, unsigned long size)
3846{
3847 struct lock_class *class, *next;
3848 struct list_head *head;
3849 unsigned long flags;
3850 int i;
Nick Piggin5a26db52008-01-16 09:51:58 +01003851 int locked;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003852
3853 raw_local_irq_save(flags);
Nick Piggin5a26db52008-01-16 09:51:58 +01003854 locked = graph_lock();
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003855
3856 /*
3857 * Unhash all classes that were created by this module:
3858 */
3859 for (i = 0; i < CLASSHASH_SIZE; i++) {
3860 head = classhash_table + i;
3861 if (list_empty(head))
3862 continue;
Arjan van de Venfabe8742008-01-24 07:00:45 +01003863 list_for_each_entry_safe(class, next, head, hash_entry) {
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003864 if (within(class->key, start, size))
3865 zap_class(class);
Arjan van de Venfabe8742008-01-24 07:00:45 +01003866 else if (within(class->name, start, size))
3867 zap_class(class);
3868 }
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003869 }
3870
Nick Piggin5a26db52008-01-16 09:51:58 +01003871 if (locked)
3872 graph_unlock();
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003873 raw_local_irq_restore(flags);
3874}
3875
3876void lockdep_reset_lock(struct lockdep_map *lock)
3877{
Ingo Molnard6d897c2006-07-10 04:44:04 -07003878 struct lock_class *class, *next;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003879 struct list_head *head;
3880 unsigned long flags;
3881 int i, j;
Nick Piggin5a26db52008-01-16 09:51:58 +01003882 int locked;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003883
3884 raw_local_irq_save(flags);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003885
3886 /*
Ingo Molnard6d897c2006-07-10 04:44:04 -07003887 * Remove all classes this lock might have:
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003888 */
Ingo Molnard6d897c2006-07-10 04:44:04 -07003889 for (j = 0; j < MAX_LOCKDEP_SUBCLASSES; j++) {
3890 /*
3891 * If the class exists we look it up and zap it:
3892 */
3893 class = look_up_lock_class(lock, j);
3894 if (class)
3895 zap_class(class);
3896 }
3897 /*
3898 * Debug check: in the end all mapped classes should
3899 * be gone.
3900 */
Nick Piggin5a26db52008-01-16 09:51:58 +01003901 locked = graph_lock();
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003902 for (i = 0; i < CLASSHASH_SIZE; i++) {
3903 head = classhash_table + i;
3904 if (list_empty(head))
3905 continue;
3906 list_for_each_entry_safe(class, next, head, hash_entry) {
Hitoshi Mitake62016252010-10-05 18:01:51 +09003907 int match = 0;
3908
3909 for (j = 0; j < NR_LOCKDEP_CACHING_CLASSES; j++)
3910 match |= class == lock->class_cache[j];
3911
3912 if (unlikely(match)) {
Peter Zijlstra0119fee2011-09-02 01:30:29 +02003913 if (debug_locks_off_graph_unlock()) {
3914 /*
3915 * We all just reset everything, how did it match?
3916 */
Ingo Molnar74c383f2006-12-13 00:34:43 -08003917 WARN_ON(1);
Peter Zijlstra0119fee2011-09-02 01:30:29 +02003918 }
Ingo Molnard6d897c2006-07-10 04:44:04 -07003919 goto out_restore;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003920 }
3921 }
3922 }
Nick Piggin5a26db52008-01-16 09:51:58 +01003923 if (locked)
3924 graph_unlock();
Ingo Molnard6d897c2006-07-10 04:44:04 -07003925
3926out_restore:
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003927 raw_local_irq_restore(flags);
3928}
3929
Sam Ravnborg14999932007-02-28 20:12:31 -08003930void lockdep_init(void)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003931{
3932 int i;
3933
3934 /*
3935 * Some architectures have their own start_kernel()
3936 * code which calls lockdep_init(), while we also
3937 * call lockdep_init() from the start_kernel() itself,
3938 * and we want to initialize the hashes only once:
3939 */
3940 if (lockdep_initialized)
3941 return;
3942
3943 for (i = 0; i < CLASSHASH_SIZE; i++)
3944 INIT_LIST_HEAD(classhash_table + i);
3945
3946 for (i = 0; i < CHAINHASH_SIZE; i++)
3947 INIT_LIST_HEAD(chainhash_table + i);
3948
3949 lockdep_initialized = 1;
3950}
3951
3952void __init lockdep_info(void)
3953{
3954 printk("Lock dependency validator: Copyright (c) 2006 Red Hat, Inc., Ingo Molnar\n");
3955
Li Zefanb0788ca2008-11-21 15:57:32 +08003956 printk("... MAX_LOCKDEP_SUBCLASSES: %lu\n", MAX_LOCKDEP_SUBCLASSES);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003957 printk("... MAX_LOCK_DEPTH: %lu\n", MAX_LOCK_DEPTH);
3958 printk("... MAX_LOCKDEP_KEYS: %lu\n", MAX_LOCKDEP_KEYS);
Li Zefanb0788ca2008-11-21 15:57:32 +08003959 printk("... CLASSHASH_SIZE: %lu\n", CLASSHASH_SIZE);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003960 printk("... MAX_LOCKDEP_ENTRIES: %lu\n", MAX_LOCKDEP_ENTRIES);
3961 printk("... MAX_LOCKDEP_CHAINS: %lu\n", MAX_LOCKDEP_CHAINS);
3962 printk("... CHAINHASH_SIZE: %lu\n", CHAINHASH_SIZE);
3963
3964 printk(" memory used by lock dependency info: %lu kB\n",
3965 (sizeof(struct lock_class) * MAX_LOCKDEP_KEYS +
3966 sizeof(struct list_head) * CLASSHASH_SIZE +
3967 sizeof(struct lock_list) * MAX_LOCKDEP_ENTRIES +
3968 sizeof(struct lock_chain) * MAX_LOCKDEP_CHAINS +
Ming Lei90629202009-08-02 21:43:36 +08003969 sizeof(struct list_head) * CHAINHASH_SIZE
Ming Lei4dd861d2009-07-16 15:44:29 +02003970#ifdef CONFIG_PROVE_LOCKING
Ming Leie351b662009-07-22 22:48:09 +08003971 + sizeof(struct circular_queue)
Ming Lei4dd861d2009-07-16 15:44:29 +02003972#endif
Ming Lei90629202009-08-02 21:43:36 +08003973 ) / 1024
Ming Lei4dd861d2009-07-16 15:44:29 +02003974 );
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003975
3976 printk(" per task-struct memory footprint: %lu bytes\n",
3977 sizeof(struct held_lock) * MAX_LOCK_DEPTH);
3978
3979#ifdef CONFIG_DEBUG_LOCKDEP
Johannes Bergc71063c2007-07-19 01:49:02 -07003980 if (lockdep_init_error) {
3981 printk("WARNING: lockdep init error! Arch code didn't call lockdep_init() early enough?\n");
3982 printk("Call stack leading to lockdep invocation was:\n");
3983 print_stack_trace(&lockdep_init_trace, 0);
3984 }
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003985#endif
3986}
3987
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003988static void
3989print_freed_lock_bug(struct task_struct *curr, const void *mem_from,
Arjan van de Ven55794a42006-07-10 04:44:03 -07003990 const void *mem_to, struct held_lock *hlock)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003991{
3992 if (!debug_locks_off())
3993 return;
3994 if (debug_locks_silent)
3995 return;
3996
Paul E. McKenneyb3fbab02011-05-24 08:31:09 -07003997 printk("\n");
3998 printk("=========================\n");
3999 printk("[ BUG: held lock freed! ]\n");
Ben Hutchingsfbdc4b92011-10-28 04:36:55 +01004000 print_kernel_ident();
Paul E. McKenneyb3fbab02011-05-24 08:31:09 -07004001 printk("-------------------------\n");
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004002 printk("%s/%d is freeing memory %p-%p, with a lock still held there!\n",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07004003 curr->comm, task_pid_nr(curr), mem_from, mem_to-1);
Arjan van de Ven55794a42006-07-10 04:44:03 -07004004 print_lock(hlock);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004005 lockdep_print_held_locks(curr);
4006
4007 printk("\nstack backtrace:\n");
4008 dump_stack();
4009}
4010
Oleg Nesterov54561782007-12-05 15:46:09 +01004011static inline int not_in_range(const void* mem_from, unsigned long mem_len,
4012 const void* lock_from, unsigned long lock_len)
4013{
4014 return lock_from + lock_len <= mem_from ||
4015 mem_from + mem_len <= lock_from;
4016}
4017
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004018/*
4019 * Called when kernel memory is freed (or unmapped), or if a lock
4020 * is destroyed or reinitialized - this code checks whether there is
4021 * any held lock in the memory range of <from> to <to>:
4022 */
4023void debug_check_no_locks_freed(const void *mem_from, unsigned long mem_len)
4024{
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004025 struct task_struct *curr = current;
4026 struct held_lock *hlock;
4027 unsigned long flags;
4028 int i;
4029
4030 if (unlikely(!debug_locks))
4031 return;
4032
4033 local_irq_save(flags);
4034 for (i = 0; i < curr->lockdep_depth; i++) {
4035 hlock = curr->held_locks + i;
4036
Oleg Nesterov54561782007-12-05 15:46:09 +01004037 if (not_in_range(mem_from, mem_len, hlock->instance,
4038 sizeof(*hlock->instance)))
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004039 continue;
4040
Oleg Nesterov54561782007-12-05 15:46:09 +01004041 print_freed_lock_bug(curr, mem_from, mem_from + mem_len, hlock);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004042 break;
4043 }
4044 local_irq_restore(flags);
4045}
Peter Zijlstraed075362006-12-06 20:35:24 -08004046EXPORT_SYMBOL_GPL(debug_check_no_locks_freed);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004047
4048static void print_held_locks_bug(struct task_struct *curr)
4049{
4050 if (!debug_locks_off())
4051 return;
4052 if (debug_locks_silent)
4053 return;
4054
Paul E. McKenneyb3fbab02011-05-24 08:31:09 -07004055 printk("\n");
4056 printk("=====================================\n");
4057 printk("[ BUG: lock held at task exit time! ]\n");
Ben Hutchingsfbdc4b92011-10-28 04:36:55 +01004058 print_kernel_ident();
Paul E. McKenneyb3fbab02011-05-24 08:31:09 -07004059 printk("-------------------------------------\n");
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004060 printk("%s/%d is exiting with locks still held!\n",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07004061 curr->comm, task_pid_nr(curr));
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004062 lockdep_print_held_locks(curr);
4063
4064 printk("\nstack backtrace:\n");
4065 dump_stack();
4066}
4067
4068void debug_check_no_locks_held(struct task_struct *task)
4069{
4070 if (unlikely(task->lockdep_depth > 0))
4071 print_held_locks_bug(task);
4072}
4073
4074void debug_show_all_locks(void)
4075{
4076 struct task_struct *g, *p;
4077 int count = 10;
4078 int unlock = 1;
4079
Jarek Poplawski9c35dd72007-03-22 00:11:28 -08004080 if (unlikely(!debug_locks)) {
4081 printk("INFO: lockdep is turned off.\n");
4082 return;
4083 }
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004084 printk("\nShowing all locks held in the system:\n");
4085
4086 /*
4087 * Here we try to get the tasklist_lock as hard as possible,
4088 * if not successful after 2 seconds we ignore it (but keep
4089 * trying). This is to enable a debug printout even if a
4090 * tasklist_lock-holding task deadlocks or crashes.
4091 */
4092retry:
4093 if (!read_trylock(&tasklist_lock)) {
4094 if (count == 10)
4095 printk("hm, tasklist_lock locked, retrying... ");
4096 if (count) {
4097 count--;
4098 printk(" #%d", 10-count);
4099 mdelay(200);
4100 goto retry;
4101 }
4102 printk(" ignoring it.\n");
4103 unlock = 0;
qinghuang feng46fec7a2008-10-28 17:24:28 +08004104 } else {
4105 if (count != 10)
4106 printk(KERN_CONT " locked it.\n");
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004107 }
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004108
4109 do_each_thread(g, p) {
Ingo Molnar85684872007-12-05 15:46:09 +01004110 /*
4111 * It's not reliable to print a task's held locks
4112 * if it's not sleeping (or if it's not the current
4113 * task):
4114 */
4115 if (p->state == TASK_RUNNING && p != current)
4116 continue;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004117 if (p->lockdep_depth)
4118 lockdep_print_held_locks(p);
4119 if (!unlock)
4120 if (read_trylock(&tasklist_lock))
4121 unlock = 1;
4122 } while_each_thread(g, p);
4123
4124 printk("\n");
4125 printk("=============================================\n\n");
4126
4127 if (unlock)
4128 read_unlock(&tasklist_lock);
4129}
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004130EXPORT_SYMBOL_GPL(debug_show_all_locks);
4131
Ingo Molnar82a1fcb2008-01-25 21:08:02 +01004132/*
4133 * Careful: only use this function if you are sure that
4134 * the task cannot run in parallel!
4135 */
John Kacurf1b499f2010-08-05 17:10:53 +02004136void debug_show_held_locks(struct task_struct *task)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004137{
Jarek Poplawski9c35dd72007-03-22 00:11:28 -08004138 if (unlikely(!debug_locks)) {
4139 printk("INFO: lockdep is turned off.\n");
4140 return;
4141 }
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004142 lockdep_print_held_locks(task);
4143}
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004144EXPORT_SYMBOL_GPL(debug_show_held_locks);
Peter Zijlstrab351d162007-10-11 22:11:12 +02004145
4146void lockdep_sys_exit(void)
4147{
4148 struct task_struct *curr = current;
4149
4150 if (unlikely(curr->lockdep_depth)) {
4151 if (!debug_locks_off())
4152 return;
Paul E. McKenneyb3fbab02011-05-24 08:31:09 -07004153 printk("\n");
4154 printk("================================================\n");
4155 printk("[ BUG: lock held when returning to user space! ]\n");
Ben Hutchingsfbdc4b92011-10-28 04:36:55 +01004156 print_kernel_ident();
Paul E. McKenneyb3fbab02011-05-24 08:31:09 -07004157 printk("------------------------------------------------\n");
Peter Zijlstrab351d162007-10-11 22:11:12 +02004158 printk("%s/%d is leaving the kernel with locks still held!\n",
4159 curr->comm, curr->pid);
4160 lockdep_print_held_locks(curr);
4161 }
4162}
Paul E. McKenney0632eb32010-02-22 17:04:47 -08004163
Paul E. McKenneyb3fbab02011-05-24 08:31:09 -07004164void lockdep_rcu_suspicious(const char *file, const int line, const char *s)
Paul E. McKenney0632eb32010-02-22 17:04:47 -08004165{
4166 struct task_struct *curr = current;
4167
Lai Jiangshan2b3fc352010-04-20 16:23:07 +08004168#ifndef CONFIG_PROVE_RCU_REPEATEDLY
Paul E. McKenney0632eb32010-02-22 17:04:47 -08004169 if (!debug_locks_off())
4170 return;
Lai Jiangshan2b3fc352010-04-20 16:23:07 +08004171#endif /* #ifdef CONFIG_PROVE_RCU_REPEATEDLY */
4172 /* Note: the following can be executed concurrently, so be careful. */
Paul E. McKenneyb3fbab02011-05-24 08:31:09 -07004173 printk("\n");
4174 printk("===============================\n");
4175 printk("[ INFO: suspicious RCU usage. ]\n");
Ben Hutchingsfbdc4b92011-10-28 04:36:55 +01004176 print_kernel_ident();
Paul E. McKenneyb3fbab02011-05-24 08:31:09 -07004177 printk("-------------------------------\n");
4178 printk("%s:%d %s!\n", file, line, s);
Paul E. McKenney0632eb32010-02-22 17:04:47 -08004179 printk("\nother info that might help us debug this:\n\n");
Paul E. McKenneycc5b83a2010-03-03 07:46:59 -08004180 printk("\nrcu_scheduler_active = %d, debug_locks = %d\n", rcu_scheduler_active, debug_locks);
Paul E. McKenney0632eb32010-02-22 17:04:47 -08004181 lockdep_print_held_locks(curr);
4182 printk("\nstack backtrace:\n");
4183 dump_stack();
4184}
Paul E. McKenneyb3fbab02011-05-24 08:31:09 -07004185EXPORT_SYMBOL_GPL(lockdep_rcu_suspicious);