blob: c081fa967c8f32166b329b702bf6ab11887000fe [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>
Peter Zijlstraaf012962009-07-16 15:44:29 +020047
Ingo Molnarfbb9ce952006-07-03 00:24:50 -070048#include <asm/sections.h>
49
50#include "lockdep_internals.h"
51
Steven Rostedta8d154b2009-04-10 09:36:00 -040052#define CREATE_TRACE_POINTS
Frederic Weisbecker67178762009-11-13 10:06:34 +010053#include <trace/events/lock.h>
Steven Rostedta8d154b2009-04-10 09:36:00 -040054
Peter Zijlstraf20786f2007-07-19 01:48:56 -070055#ifdef CONFIG_PROVE_LOCKING
56int prove_locking = 1;
57module_param(prove_locking, int, 0644);
58#else
59#define prove_locking 0
60#endif
61
62#ifdef CONFIG_LOCK_STAT
63int lock_stat = 1;
64module_param(lock_stat, int, 0644);
65#else
66#define lock_stat 0
67#endif
68
Ingo Molnarfbb9ce952006-07-03 00:24:50 -070069/*
Ingo Molnar74c383f2006-12-13 00:34:43 -080070 * lockdep_lock: protects the lockdep graph, the hashes and the
71 * class/list/hash allocators.
Ingo Molnarfbb9ce952006-07-03 00:24:50 -070072 *
73 * This is one of the rare exceptions where it's justified
74 * to use a raw spinlock - we really dont want the spinlock
Ingo Molnar74c383f2006-12-13 00:34:43 -080075 * code to recurse back into the lockdep code...
Ingo Molnarfbb9ce952006-07-03 00:24:50 -070076 */
Thomas Gleixneredc35bd2009-12-03 12:38:57 +010077static arch_spinlock_t lockdep_lock = (arch_spinlock_t)__ARCH_SPIN_LOCK_UNLOCKED;
Ingo Molnar74c383f2006-12-13 00:34:43 -080078
79static int graph_lock(void)
80{
Thomas Gleixner0199c4e2009-12-02 20:01:25 +010081 arch_spin_lock(&lockdep_lock);
Ingo Molnar74c383f2006-12-13 00:34:43 -080082 /*
83 * Make sure that if another CPU detected a bug while
84 * walking the graph we dont change it (while the other
85 * CPU is busy printing out stuff with the graph lock
86 * dropped already)
87 */
88 if (!debug_locks) {
Thomas Gleixner0199c4e2009-12-02 20:01:25 +010089 arch_spin_unlock(&lockdep_lock);
Ingo Molnar74c383f2006-12-13 00:34:43 -080090 return 0;
91 }
Steven Rostedtbb065af2008-05-12 21:21:00 +020092 /* prevent any recursions within lockdep from causing deadlocks */
93 current->lockdep_recursion++;
Ingo Molnar74c383f2006-12-13 00:34:43 -080094 return 1;
95}
96
97static inline int graph_unlock(void)
98{
Peter Zijlstra0119fee2011-09-02 01:30:29 +020099 if (debug_locks && !arch_spin_is_locked(&lockdep_lock)) {
100 /*
101 * The lockdep graph lock isn't locked while we expect it to
102 * be, we're confused now, bye!
103 */
Jarek Poplawski381a2292007-02-10 01:44:58 -0800104 return DEBUG_LOCKS_WARN_ON(1);
Peter Zijlstra0119fee2011-09-02 01:30:29 +0200105 }
Jarek Poplawski381a2292007-02-10 01:44:58 -0800106
Steven Rostedtbb065af2008-05-12 21:21:00 +0200107 current->lockdep_recursion--;
Thomas Gleixner0199c4e2009-12-02 20:01:25 +0100108 arch_spin_unlock(&lockdep_lock);
Ingo Molnar74c383f2006-12-13 00:34:43 -0800109 return 0;
110}
111
112/*
113 * Turn lock debugging off and return with 0 if it was off already,
114 * and also release the graph lock:
115 */
116static inline int debug_locks_off_graph_unlock(void)
117{
118 int ret = debug_locks_off();
119
Thomas Gleixner0199c4e2009-12-02 20:01:25 +0100120 arch_spin_unlock(&lockdep_lock);
Ingo Molnar74c383f2006-12-13 00:34:43 -0800121
122 return ret;
123}
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700124
125static int lockdep_initialized;
126
127unsigned long nr_list_entries;
Peter Zijlstraaf012962009-07-16 15:44:29 +0200128static struct lock_list list_entries[MAX_LOCKDEP_ENTRIES];
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700129
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700130/*
131 * All data structures here are protected by the global debug_lock.
132 *
133 * Mutex key structs only get allocated, once during bootup, and never
134 * get freed - this significantly simplifies the debugging code.
135 */
136unsigned long nr_lock_classes;
137static struct lock_class lock_classes[MAX_LOCKDEP_KEYS];
138
Dave Jonesf82b2172008-08-11 09:30:23 +0200139static inline struct lock_class *hlock_class(struct held_lock *hlock)
140{
141 if (!hlock->class_idx) {
Peter Zijlstra0119fee2011-09-02 01:30:29 +0200142 /*
143 * Someone passed in garbage, we give up.
144 */
Dave Jonesf82b2172008-08-11 09:30:23 +0200145 DEBUG_LOCKS_WARN_ON(1);
146 return NULL;
147 }
148 return lock_classes + hlock->class_idx - 1;
149}
150
Peter Zijlstraf20786f2007-07-19 01:48:56 -0700151#ifdef CONFIG_LOCK_STAT
Tejun Heo1871e522009-10-29 22:34:13 +0900152static DEFINE_PER_CPU(struct lock_class_stats[MAX_LOCKDEP_KEYS],
153 cpu_lock_stats);
Peter Zijlstraf20786f2007-07-19 01:48:56 -0700154
Peter Zijlstra3365e7792009-10-09 10:12:41 +0200155static inline u64 lockstat_clock(void)
156{
Peter Zijlstrac6763292010-05-25 10:48:51 +0200157 return local_clock();
Peter Zijlstra3365e7792009-10-09 10:12:41 +0200158}
159
Peter Zijlstrac7e78cf2008-10-16 23:17:09 +0200160static int lock_point(unsigned long points[], unsigned long ip)
Peter Zijlstraf20786f2007-07-19 01:48:56 -0700161{
162 int i;
163
Peter Zijlstrac7e78cf2008-10-16 23:17:09 +0200164 for (i = 0; i < LOCKSTAT_POINTS; i++) {
165 if (points[i] == 0) {
166 points[i] = ip;
Peter Zijlstraf20786f2007-07-19 01:48:56 -0700167 break;
168 }
Peter Zijlstrac7e78cf2008-10-16 23:17:09 +0200169 if (points[i] == ip)
Peter Zijlstraf20786f2007-07-19 01:48:56 -0700170 break;
171 }
172
173 return i;
174}
175
Peter Zijlstra3365e7792009-10-09 10:12:41 +0200176static void lock_time_inc(struct lock_time *lt, u64 time)
Peter Zijlstraf20786f2007-07-19 01:48:56 -0700177{
178 if (time > lt->max)
179 lt->max = time;
180
Frank Rowand109d71c2009-11-19 13:42:06 -0800181 if (time < lt->min || !lt->nr)
Peter Zijlstraf20786f2007-07-19 01:48:56 -0700182 lt->min = time;
183
184 lt->total += time;
185 lt->nr++;
186}
187
Peter Zijlstrac46261d2007-07-19 01:48:57 -0700188static inline void lock_time_add(struct lock_time *src, struct lock_time *dst)
189{
Frank Rowand109d71c2009-11-19 13:42:06 -0800190 if (!src->nr)
191 return;
192
193 if (src->max > dst->max)
194 dst->max = src->max;
195
196 if (src->min < dst->min || !dst->nr)
197 dst->min = src->min;
198
Peter Zijlstrac46261d2007-07-19 01:48:57 -0700199 dst->total += src->total;
200 dst->nr += src->nr;
201}
202
203struct lock_class_stats lock_stats(struct lock_class *class)
204{
205 struct lock_class_stats stats;
206 int cpu, i;
207
208 memset(&stats, 0, sizeof(struct lock_class_stats));
209 for_each_possible_cpu(cpu) {
210 struct lock_class_stats *pcs =
Tejun Heo1871e522009-10-29 22:34:13 +0900211 &per_cpu(cpu_lock_stats, cpu)[class - lock_classes];
Peter Zijlstrac46261d2007-07-19 01:48:57 -0700212
213 for (i = 0; i < ARRAY_SIZE(stats.contention_point); i++)
214 stats.contention_point[i] += pcs->contention_point[i];
215
Peter Zijlstrac7e78cf2008-10-16 23:17:09 +0200216 for (i = 0; i < ARRAY_SIZE(stats.contending_point); i++)
217 stats.contending_point[i] += pcs->contending_point[i];
218
Peter Zijlstrac46261d2007-07-19 01:48:57 -0700219 lock_time_add(&pcs->read_waittime, &stats.read_waittime);
220 lock_time_add(&pcs->write_waittime, &stats.write_waittime);
221
222 lock_time_add(&pcs->read_holdtime, &stats.read_holdtime);
223 lock_time_add(&pcs->write_holdtime, &stats.write_holdtime);
Peter Zijlstra96645672007-07-19 01:49:00 -0700224
225 for (i = 0; i < ARRAY_SIZE(stats.bounces); i++)
226 stats.bounces[i] += pcs->bounces[i];
Peter Zijlstrac46261d2007-07-19 01:48:57 -0700227 }
228
229 return stats;
230}
231
232void clear_lock_stats(struct lock_class *class)
233{
234 int cpu;
235
236 for_each_possible_cpu(cpu) {
237 struct lock_class_stats *cpu_stats =
Tejun Heo1871e522009-10-29 22:34:13 +0900238 &per_cpu(cpu_lock_stats, cpu)[class - lock_classes];
Peter Zijlstrac46261d2007-07-19 01:48:57 -0700239
240 memset(cpu_stats, 0, sizeof(struct lock_class_stats));
241 }
242 memset(class->contention_point, 0, sizeof(class->contention_point));
Peter Zijlstrac7e78cf2008-10-16 23:17:09 +0200243 memset(class->contending_point, 0, sizeof(class->contending_point));
Peter Zijlstrac46261d2007-07-19 01:48:57 -0700244}
245
Peter Zijlstraf20786f2007-07-19 01:48:56 -0700246static struct lock_class_stats *get_lock_stats(struct lock_class *class)
247{
Tejun Heo1871e522009-10-29 22:34:13 +0900248 return &get_cpu_var(cpu_lock_stats)[class - lock_classes];
Peter Zijlstraf20786f2007-07-19 01:48:56 -0700249}
250
251static void put_lock_stats(struct lock_class_stats *stats)
252{
Tejun Heo1871e522009-10-29 22:34:13 +0900253 put_cpu_var(cpu_lock_stats);
Peter Zijlstraf20786f2007-07-19 01:48:56 -0700254}
255
256static void lock_release_holdtime(struct held_lock *hlock)
257{
258 struct lock_class_stats *stats;
Peter Zijlstra3365e7792009-10-09 10:12:41 +0200259 u64 holdtime;
Peter Zijlstraf20786f2007-07-19 01:48:56 -0700260
261 if (!lock_stat)
262 return;
263
Peter Zijlstra3365e7792009-10-09 10:12:41 +0200264 holdtime = lockstat_clock() - hlock->holdtime_stamp;
Peter Zijlstraf20786f2007-07-19 01:48:56 -0700265
Dave Jonesf82b2172008-08-11 09:30:23 +0200266 stats = get_lock_stats(hlock_class(hlock));
Peter Zijlstraf20786f2007-07-19 01:48:56 -0700267 if (hlock->read)
268 lock_time_inc(&stats->read_holdtime, holdtime);
269 else
270 lock_time_inc(&stats->write_holdtime, holdtime);
271 put_lock_stats(stats);
272}
273#else
274static inline void lock_release_holdtime(struct held_lock *hlock)
275{
276}
277#endif
278
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700279/*
280 * We keep a global list of all lock classes. The list only grows,
281 * never shrinks. The list is only accessed with the lockdep
282 * spinlock lock held.
283 */
284LIST_HEAD(all_lock_classes);
285
286/*
287 * The lockdep classes are in a hash-table as well, for fast lookup:
288 */
289#define CLASSHASH_BITS (MAX_LOCKDEP_KEYS_BITS - 1)
290#define CLASSHASH_SIZE (1UL << CLASSHASH_BITS)
Peter Zijlstra4b32d0a2007-07-19 01:48:59 -0700291#define __classhashfn(key) hash_long((unsigned long)key, CLASSHASH_BITS)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700292#define classhashentry(key) (classhash_table + __classhashfn((key)))
293
294static struct list_head classhash_table[CLASSHASH_SIZE];
295
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700296/*
297 * We put the lock dependency chains into a hash-table as well, to cache
298 * their existence:
299 */
300#define CHAINHASH_BITS (MAX_LOCKDEP_CHAINS_BITS-1)
301#define CHAINHASH_SIZE (1UL << CHAINHASH_BITS)
Peter Zijlstra4b32d0a2007-07-19 01:48:59 -0700302#define __chainhashfn(chain) hash_long(chain, CHAINHASH_BITS)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700303#define chainhashentry(chain) (chainhash_table + __chainhashfn((chain)))
304
305static struct list_head chainhash_table[CHAINHASH_SIZE];
306
307/*
308 * The hash key of the lock dependency chains is a hash itself too:
309 * it's a hash of all locks taken up to that lock, including that lock.
310 * It's a 64-bit hash, because it's important for the keys to be
311 * unique.
312 */
313#define iterate_chain_key(key1, key2) \
Ingo Molnar03cbc352006-09-29 02:01:46 -0700314 (((key1) << MAX_LOCKDEP_KEYS_BITS) ^ \
315 ((key1) >> (64-MAX_LOCKDEP_KEYS_BITS)) ^ \
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700316 (key2))
317
Steven Rostedt1d09daa2008-05-12 21:20:55 +0200318void lockdep_off(void)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700319{
320 current->lockdep_recursion++;
321}
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700322EXPORT_SYMBOL(lockdep_off);
323
Steven Rostedt1d09daa2008-05-12 21:20:55 +0200324void lockdep_on(void)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700325{
326 current->lockdep_recursion--;
327}
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700328EXPORT_SYMBOL(lockdep_on);
329
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700330/*
331 * Debugging switches:
332 */
333
334#define VERBOSE 0
Ingo Molnar33e94e92006-12-13 00:34:41 -0800335#define VERY_VERBOSE 0
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700336
337#if VERBOSE
338# define HARDIRQ_VERBOSE 1
339# define SOFTIRQ_VERBOSE 1
Nick Piggincf40bd12009-01-21 08:12:39 +0100340# define RECLAIM_VERBOSE 1
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700341#else
342# define HARDIRQ_VERBOSE 0
343# define SOFTIRQ_VERBOSE 0
Nick Piggincf40bd12009-01-21 08:12:39 +0100344# define RECLAIM_VERBOSE 0
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700345#endif
346
Nick Piggincf40bd12009-01-21 08:12:39 +0100347#if VERBOSE || HARDIRQ_VERBOSE || SOFTIRQ_VERBOSE || RECLAIM_VERBOSE
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700348/*
349 * Quick filtering for interesting events:
350 */
351static int class_filter(struct lock_class *class)
352{
Andi Kleenf9829cc2006-07-10 04:44:01 -0700353#if 0
354 /* Example */
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700355 if (class->name_version == 1 &&
Andi Kleenf9829cc2006-07-10 04:44:01 -0700356 !strcmp(class->name, "lockname"))
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700357 return 1;
358 if (class->name_version == 1 &&
Andi Kleenf9829cc2006-07-10 04:44:01 -0700359 !strcmp(class->name, "&struct->lockfield"))
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700360 return 1;
Andi Kleenf9829cc2006-07-10 04:44:01 -0700361#endif
Ingo Molnara6640892006-12-13 00:34:39 -0800362 /* Filter everything else. 1 would be to allow everything else */
363 return 0;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700364}
365#endif
366
367static int verbose(struct lock_class *class)
368{
369#if VERBOSE
370 return class_filter(class);
371#endif
372 return 0;
373}
374
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700375/*
376 * Stack-trace: tightly packed array of stack backtrace
Ingo Molnar74c383f2006-12-13 00:34:43 -0800377 * addresses. Protected by the graph_lock.
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700378 */
379unsigned long nr_stack_trace_entries;
380static unsigned long stack_trace[MAX_STACK_TRACE_ENTRIES];
381
382static int save_trace(struct stack_trace *trace)
383{
384 trace->nr_entries = 0;
385 trace->max_entries = MAX_STACK_TRACE_ENTRIES - nr_stack_trace_entries;
386 trace->entries = stack_trace + nr_stack_trace_entries;
387
Andi Kleen5a1b3992006-09-26 10:52:34 +0200388 trace->skip = 3;
Andi Kleen5a1b3992006-09-26 10:52:34 +0200389
Christoph Hellwigab1b6f02007-05-08 00:23:29 -0700390 save_stack_trace(trace);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700391
Peter Zijlstra4f84f432009-07-20 15:27:04 +0200392 /*
393 * Some daft arches put -1 at the end to indicate its a full trace.
394 *
395 * <rant> this is buggy anyway, since it takes a whole extra entry so a
396 * complete trace that maxes out the entries provided will be reported
397 * as incomplete, friggin useless </rant>
398 */
Luck, Tonyea5b41f2009-12-09 14:29:36 -0800399 if (trace->nr_entries != 0 &&
400 trace->entries[trace->nr_entries-1] == ULONG_MAX)
Peter Zijlstra4f84f432009-07-20 15:27:04 +0200401 trace->nr_entries--;
402
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700403 trace->max_entries = trace->nr_entries;
404
405 nr_stack_trace_entries += trace->nr_entries;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700406
Peter Zijlstra4f84f432009-07-20 15:27:04 +0200407 if (nr_stack_trace_entries >= MAX_STACK_TRACE_ENTRIES-1) {
Ingo Molnar74c383f2006-12-13 00:34:43 -0800408 if (!debug_locks_off_graph_unlock())
409 return 0;
410
411 printk("BUG: MAX_STACK_TRACE_ENTRIES too low!\n");
412 printk("turning off the locking correctness validator.\n");
413 dump_stack();
414
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700415 return 0;
416 }
417
418 return 1;
419}
420
421unsigned int nr_hardirq_chains;
422unsigned int nr_softirq_chains;
423unsigned int nr_process_chains;
424unsigned int max_lockdep_depth;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700425
426#ifdef CONFIG_DEBUG_LOCKDEP
427/*
428 * We cannot printk in early bootup code. Not even early_printk()
429 * might work. So we mark any initialization errors and printk
430 * about it later on, in lockdep_info().
431 */
432static int lockdep_init_error;
Johannes Bergc71063c2007-07-19 01:49:02 -0700433static unsigned long lockdep_init_trace_data[20];
434static struct stack_trace lockdep_init_trace = {
435 .max_entries = ARRAY_SIZE(lockdep_init_trace_data),
436 .entries = lockdep_init_trace_data,
437};
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700438
439/*
440 * Various lockdep statistics:
441 */
Frederic Weisbeckerbd6d29c2010-04-06 00:10:17 +0200442DEFINE_PER_CPU(struct lockdep_stats, lockdep_stats);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700443#endif
444
445/*
446 * Locking printouts:
447 */
448
Peter Zijlstrafabe9c42009-01-22 14:51:01 +0100449#define __USAGE(__STATE) \
Peter Zijlstrab4b136f2009-01-29 14:50:36 +0100450 [LOCK_USED_IN_##__STATE] = "IN-"__stringify(__STATE)"-W", \
451 [LOCK_ENABLED_##__STATE] = __stringify(__STATE)"-ON-W", \
452 [LOCK_USED_IN_##__STATE##_READ] = "IN-"__stringify(__STATE)"-R",\
453 [LOCK_ENABLED_##__STATE##_READ] = __stringify(__STATE)"-ON-R",
Peter Zijlstrafabe9c42009-01-22 14:51:01 +0100454
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700455static const char *usage_str[] =
456{
Peter Zijlstrafabe9c42009-01-22 14:51:01 +0100457#define LOCKDEP_STATE(__STATE) __USAGE(__STATE)
458#include "lockdep_states.h"
459#undef LOCKDEP_STATE
460 [LOCK_USED] = "INITIAL USE",
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700461};
462
463const char * __get_key_name(struct lockdep_subclass_key *key, char *str)
464{
Alexey Dobriyanffb45122007-05-08 00:28:41 -0700465 return kallsyms_lookup((unsigned long)key, NULL, NULL, NULL, str);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700466}
467
Peter Zijlstra3ff176c2009-01-22 17:40:42 +0100468static inline unsigned long lock_flag(enum lock_usage_bit bit)
469{
470 return 1UL << bit;
471}
472
473static char get_usage_char(struct lock_class *class, enum lock_usage_bit bit)
474{
475 char c = '.';
476
477 if (class->usage_mask & lock_flag(bit + 2))
478 c = '+';
479 if (class->usage_mask & lock_flag(bit)) {
480 c = '-';
481 if (class->usage_mask & lock_flag(bit + 2))
482 c = '?';
483 }
484
485 return c;
486}
487
Peter Zijlstraf510b232009-01-22 17:53:47 +0100488void get_usage_chars(struct lock_class *class, char usage[LOCK_USAGE_CHARS])
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700489{
Peter Zijlstraf510b232009-01-22 17:53:47 +0100490 int i = 0;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700491
Peter Zijlstraf510b232009-01-22 17:53:47 +0100492#define LOCKDEP_STATE(__STATE) \
493 usage[i++] = get_usage_char(class, LOCK_USED_IN_##__STATE); \
494 usage[i++] = get_usage_char(class, LOCK_USED_IN_##__STATE##_READ);
495#include "lockdep_states.h"
496#undef LOCKDEP_STATE
497
498 usage[i] = '\0';
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700499}
500
Steven Rostedt3003eba2011-04-20 21:41:54 -0400501static int __print_lock_name(struct lock_class *class)
502{
503 char str[KSYM_NAME_LEN];
504 const char *name;
505
506 name = class->name;
507 if (!name)
508 name = __get_key_name(class->key, str);
509
510 return printk("%s", name);
511}
512
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700513static void print_lock_name(struct lock_class *class)
514{
Peter Zijlstraf510b232009-01-22 17:53:47 +0100515 char str[KSYM_NAME_LEN], usage[LOCK_USAGE_CHARS];
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700516 const char *name;
517
Peter Zijlstraf510b232009-01-22 17:53:47 +0100518 get_usage_chars(class, usage);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700519
520 name = class->name;
521 if (!name) {
522 name = __get_key_name(class->key, str);
523 printk(" (%s", name);
524 } else {
525 printk(" (%s", name);
526 if (class->name_version > 1)
527 printk("#%d", class->name_version);
528 if (class->subclass)
529 printk("/%d", class->subclass);
530 }
Peter Zijlstraf510b232009-01-22 17:53:47 +0100531 printk("){%s}", usage);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700532}
533
534static void print_lockdep_cache(struct lockdep_map *lock)
535{
536 const char *name;
Tejun Heo9281ace2007-07-17 04:03:51 -0700537 char str[KSYM_NAME_LEN];
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700538
539 name = lock->name;
540 if (!name)
541 name = __get_key_name(lock->key->subkeys, str);
542
543 printk("%s", name);
544}
545
546static void print_lock(struct held_lock *hlock)
547{
Dave Jonesf82b2172008-08-11 09:30:23 +0200548 print_lock_name(hlock_class(hlock));
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700549 printk(", at: ");
550 print_ip_sym(hlock->acquire_ip);
551}
552
553static void lockdep_print_held_locks(struct task_struct *curr)
554{
555 int i, depth = curr->lockdep_depth;
556
557 if (!depth) {
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -0700558 printk("no locks held by %s/%d.\n", curr->comm, task_pid_nr(curr));
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700559 return;
560 }
561 printk("%d lock%s held by %s/%d:\n",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -0700562 depth, depth > 1 ? "s" : "", curr->comm, task_pid_nr(curr));
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700563
564 for (i = 0; i < depth; i++) {
565 printk(" #%d: ", i);
566 print_lock(curr->held_locks + i);
567 }
568}
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700569
Dave Jones99de0552006-09-29 02:00:10 -0700570static void print_kernel_version(void)
571{
Serge E. Hallyn96b644b2006-10-02 02:18:13 -0700572 printk("%s %.*s\n", init_utsname()->release,
573 (int)strcspn(init_utsname()->version, " "),
574 init_utsname()->version);
Dave Jones99de0552006-09-29 02:00:10 -0700575}
576
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700577static int very_verbose(struct lock_class *class)
578{
579#if VERY_VERBOSE
580 return class_filter(class);
581#endif
582 return 0;
583}
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700584
585/*
586 * Is this the address of a static object:
587 */
588static int static_obj(void *obj)
589{
590 unsigned long start = (unsigned long) &_stext,
591 end = (unsigned long) &_end,
592 addr = (unsigned long) obj;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700593
594 /*
595 * static variable?
596 */
597 if ((addr >= start) && (addr < end))
598 return 1;
599
Mike Frysinger2a9ad182009-09-22 16:44:16 -0700600 if (arch_is_kernel_data(addr))
601 return 1;
602
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700603 /*
Tejun Heo10fad5e2010-03-10 18:57:54 +0900604 * in-kernel percpu var?
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700605 */
Tejun Heo10fad5e2010-03-10 18:57:54 +0900606 if (is_kernel_percpu_address(addr))
607 return 1;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700608
609 /*
Tejun Heo10fad5e2010-03-10 18:57:54 +0900610 * module static or percpu var?
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700611 */
Tejun Heo10fad5e2010-03-10 18:57:54 +0900612 return is_module_address(addr) || is_module_percpu_address(addr);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700613}
614
615/*
616 * To make lock name printouts unique, we calculate a unique
617 * class->name_version generation counter:
618 */
619static int count_matching_names(struct lock_class *new_class)
620{
621 struct lock_class *class;
622 int count = 0;
623
624 if (!new_class->name)
625 return 0;
626
627 list_for_each_entry(class, &all_lock_classes, lock_entry) {
628 if (new_class->key - new_class->subclass == class->key)
629 return class->name_version;
630 if (class->name && !strcmp(class->name, new_class->name))
631 count = max(count, class->name_version);
632 }
633
634 return count + 1;
635}
636
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700637/*
638 * Register a lock's class in the hash-table, if the class is not present
639 * yet. Otherwise we look it up. We cache the result in the lock object
640 * itself, so actual lookup of the hash should be once per lock object.
641 */
642static inline struct lock_class *
Ingo Molnard6d897c2006-07-10 04:44:04 -0700643look_up_lock_class(struct lockdep_map *lock, unsigned int subclass)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700644{
645 struct lockdep_subclass_key *key;
646 struct list_head *hash_head;
647 struct lock_class *class;
648
649#ifdef CONFIG_DEBUG_LOCKDEP
650 /*
651 * If the architecture calls into lockdep before initializing
652 * the hashes then we'll warn about it later. (we cannot printk
653 * right now)
654 */
655 if (unlikely(!lockdep_initialized)) {
656 lockdep_init();
657 lockdep_init_error = 1;
Johannes Bergc71063c2007-07-19 01:49:02 -0700658 save_stack_trace(&lockdep_init_trace);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700659 }
660#endif
661
Hitoshi Mitake4ba053c2010-10-13 17:30:26 +0900662 if (unlikely(subclass >= MAX_LOCKDEP_SUBCLASSES)) {
663 debug_locks_off();
664 printk(KERN_ERR
665 "BUG: looking up invalid subclass: %u\n", subclass);
666 printk(KERN_ERR
667 "turning off the locking correctness validator.\n");
668 dump_stack();
669 return NULL;
670 }
671
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700672 /*
673 * Static locks do not have their class-keys yet - for them the key
674 * is the lock object itself:
675 */
676 if (unlikely(!lock->key))
677 lock->key = (void *)lock;
678
679 /*
680 * NOTE: the class-key must be unique. For dynamic locks, a static
681 * lock_class_key variable is passed in through the mutex_init()
682 * (or spin_lock_init()) call - which acts as the key. For static
683 * locks we use the lock object itself as the key.
684 */
Peter Zijlstra4b32d0a2007-07-19 01:48:59 -0700685 BUILD_BUG_ON(sizeof(struct lock_class_key) >
686 sizeof(struct lockdep_map));
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700687
688 key = lock->key->subkeys + subclass;
689
690 hash_head = classhashentry(key);
691
692 /*
693 * We can walk the hash lockfree, because the hash only
694 * grows, and we are careful when adding entries to the end:
695 */
Peter Zijlstra4b32d0a2007-07-19 01:48:59 -0700696 list_for_each_entry(class, hash_head, hash_entry) {
697 if (class->key == key) {
Peter Zijlstra0119fee2011-09-02 01:30:29 +0200698 /*
699 * Huh! same key, different name? Did someone trample
700 * on some memory? We're most confused.
701 */
Peter Zijlstra4b32d0a2007-07-19 01:48:59 -0700702 WARN_ON_ONCE(class->name != lock->name);
Ingo Molnard6d897c2006-07-10 04:44:04 -0700703 return class;
Peter Zijlstra4b32d0a2007-07-19 01:48:59 -0700704 }
705 }
Ingo Molnard6d897c2006-07-10 04:44:04 -0700706
707 return NULL;
708}
709
710/*
711 * Register a lock's class in the hash-table, if the class is not present
712 * yet. Otherwise we look it up. We cache the result in the lock object
713 * itself, so actual lookup of the hash should be once per lock object.
714 */
715static inline struct lock_class *
Peter Zijlstra4dfbb9d2006-10-11 01:45:14 -0400716register_lock_class(struct lockdep_map *lock, unsigned int subclass, int force)
Ingo Molnard6d897c2006-07-10 04:44:04 -0700717{
718 struct lockdep_subclass_key *key;
719 struct list_head *hash_head;
720 struct lock_class *class;
Ingo Molnar70e45062006-12-06 20:40:50 -0800721 unsigned long flags;
Ingo Molnard6d897c2006-07-10 04:44:04 -0700722
723 class = look_up_lock_class(lock, subclass);
724 if (likely(class))
725 return class;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700726
727 /*
728 * Debug-check: all keys must be persistent!
729 */
730 if (!static_obj(lock->key)) {
731 debug_locks_off();
732 printk("INFO: trying to register non-static key.\n");
733 printk("the code is fine but needs lockdep annotation.\n");
734 printk("turning off the locking correctness validator.\n");
735 dump_stack();
736
737 return NULL;
738 }
739
Ingo Molnard6d897c2006-07-10 04:44:04 -0700740 key = lock->key->subkeys + subclass;
741 hash_head = classhashentry(key);
742
Ingo Molnar70e45062006-12-06 20:40:50 -0800743 raw_local_irq_save(flags);
Ingo Molnar74c383f2006-12-13 00:34:43 -0800744 if (!graph_lock()) {
745 raw_local_irq_restore(flags);
746 return NULL;
747 }
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700748 /*
749 * We have to do the hash-walk again, to avoid races
750 * with another CPU:
751 */
752 list_for_each_entry(class, hash_head, hash_entry)
753 if (class->key == key)
754 goto out_unlock_set;
755 /*
756 * Allocate a new key from the static array, and add it to
757 * the hash:
758 */
759 if (nr_lock_classes >= MAX_LOCKDEP_KEYS) {
Ingo Molnar74c383f2006-12-13 00:34:43 -0800760 if (!debug_locks_off_graph_unlock()) {
761 raw_local_irq_restore(flags);
762 return NULL;
763 }
Ingo Molnar70e45062006-12-06 20:40:50 -0800764 raw_local_irq_restore(flags);
Ingo Molnar74c383f2006-12-13 00:34:43 -0800765
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700766 printk("BUG: MAX_LOCKDEP_KEYS too low!\n");
767 printk("turning off the locking correctness validator.\n");
Peter Zijlstraeedeeab2009-03-18 12:38:47 +0100768 dump_stack();
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700769 return NULL;
770 }
771 class = lock_classes + nr_lock_classes++;
Frederic Weisbeckerbd6d29c2010-04-06 00:10:17 +0200772 debug_atomic_inc(nr_unused_locks);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700773 class->key = key;
774 class->name = lock->name;
775 class->subclass = subclass;
776 INIT_LIST_HEAD(&class->lock_entry);
777 INIT_LIST_HEAD(&class->locks_before);
778 INIT_LIST_HEAD(&class->locks_after);
779 class->name_version = count_matching_names(class);
780 /*
781 * We use RCU's safe list-add method to make
782 * parallel walking of the hash-list safe:
783 */
784 list_add_tail_rcu(&class->hash_entry, hash_head);
Dale Farnsworth14811972008-02-25 23:03:02 +0100785 /*
786 * Add it to the global list of classes:
787 */
788 list_add_tail_rcu(&class->lock_entry, &all_lock_classes);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700789
790 if (verbose(class)) {
Ingo Molnar74c383f2006-12-13 00:34:43 -0800791 graph_unlock();
Ingo Molnar70e45062006-12-06 20:40:50 -0800792 raw_local_irq_restore(flags);
Ingo Molnar74c383f2006-12-13 00:34:43 -0800793
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700794 printk("\nnew class %p: %s", class->key, class->name);
795 if (class->name_version > 1)
796 printk("#%d", class->name_version);
797 printk("\n");
798 dump_stack();
Ingo Molnar74c383f2006-12-13 00:34:43 -0800799
Ingo Molnar70e45062006-12-06 20:40:50 -0800800 raw_local_irq_save(flags);
Ingo Molnar74c383f2006-12-13 00:34:43 -0800801 if (!graph_lock()) {
802 raw_local_irq_restore(flags);
803 return NULL;
804 }
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700805 }
806out_unlock_set:
Ingo Molnar74c383f2006-12-13 00:34:43 -0800807 graph_unlock();
Ingo Molnar70e45062006-12-06 20:40:50 -0800808 raw_local_irq_restore(flags);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700809
Peter Zijlstra4dfbb9d2006-10-11 01:45:14 -0400810 if (!subclass || force)
Hitoshi Mitake62016252010-10-05 18:01:51 +0900811 lock->class_cache[0] = class;
812 else if (subclass < NR_LOCKDEP_CACHING_CLASSES)
813 lock->class_cache[subclass] = class;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700814
Peter Zijlstra0119fee2011-09-02 01:30:29 +0200815 /*
816 * Hash collision, did we smoke some? We found a class with a matching
817 * hash but the subclass -- which is hashed in -- didn't match.
818 */
Jarek Poplawski381a2292007-02-10 01:44:58 -0800819 if (DEBUG_LOCKS_WARN_ON(class->subclass != subclass))
820 return NULL;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700821
822 return class;
823}
824
Peter Zijlstraca58abc2007-07-19 01:48:53 -0700825#ifdef CONFIG_PROVE_LOCKING
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700826/*
Peter Zijlstra8e182572007-07-19 01:48:54 -0700827 * Allocate a lockdep entry. (assumes the graph_lock held, returns
828 * with NULL on failure)
829 */
830static struct lock_list *alloc_list_entry(void)
831{
832 if (nr_list_entries >= MAX_LOCKDEP_ENTRIES) {
833 if (!debug_locks_off_graph_unlock())
834 return NULL;
835
836 printk("BUG: MAX_LOCKDEP_ENTRIES too low!\n");
837 printk("turning off the locking correctness validator.\n");
Peter Zijlstraeedeeab2009-03-18 12:38:47 +0100838 dump_stack();
Peter Zijlstra8e182572007-07-19 01:48:54 -0700839 return NULL;
840 }
841 return list_entries + nr_list_entries++;
842}
843
844/*
845 * Add a new dependency to the head of the list:
846 */
847static int add_lock_to_list(struct lock_class *class, struct lock_class *this,
Yong Zhang4726f2a2010-05-04 14:16:48 +0800848 struct list_head *head, unsigned long ip,
849 int distance, struct stack_trace *trace)
Peter Zijlstra8e182572007-07-19 01:48:54 -0700850{
851 struct lock_list *entry;
852 /*
853 * Lock not present yet - get a new dependency struct and
854 * add it to the list:
855 */
856 entry = alloc_list_entry();
857 if (!entry)
858 return 0;
859
Zhu Yi74870172008-08-27 14:33:00 +0800860 entry->class = this;
861 entry->distance = distance;
Yong Zhang4726f2a2010-05-04 14:16:48 +0800862 entry->trace = *trace;
Peter Zijlstra8e182572007-07-19 01:48:54 -0700863 /*
864 * Since we never remove from the dependency list, the list can
865 * be walked lockless by other CPUs, it's only allocation
866 * that must be protected by the spinlock. But this also means
867 * we must make new entries visible only once writes to the
868 * entry become visible - hence the RCU op:
869 */
870 list_add_tail_rcu(&entry->entry, head);
871
872 return 1;
873}
874
Peter Zijlstra98c33ed2009-07-21 13:19:07 +0200875/*
876 * For good efficiency of modular, we use power of 2
877 */
Peter Zijlstraaf012962009-07-16 15:44:29 +0200878#define MAX_CIRCULAR_QUEUE_SIZE 4096UL
879#define CQ_MASK (MAX_CIRCULAR_QUEUE_SIZE-1)
880
Peter Zijlstra98c33ed2009-07-21 13:19:07 +0200881/*
882 * The circular_queue and helpers is used to implement the
Peter Zijlstraaf012962009-07-16 15:44:29 +0200883 * breadth-first search(BFS)algorithem, by which we can build
884 * the shortest path from the next lock to be acquired to the
885 * previous held lock if there is a circular between them.
Peter Zijlstra98c33ed2009-07-21 13:19:07 +0200886 */
Peter Zijlstraaf012962009-07-16 15:44:29 +0200887struct circular_queue {
888 unsigned long element[MAX_CIRCULAR_QUEUE_SIZE];
889 unsigned int front, rear;
890};
891
892static struct circular_queue lock_cq;
Peter Zijlstraaf012962009-07-16 15:44:29 +0200893
Ming Lei12f3dfd2009-07-16 15:44:29 +0200894unsigned int max_bfs_queue_depth;
Peter Zijlstraaf012962009-07-16 15:44:29 +0200895
Ming Leie351b662009-07-22 22:48:09 +0800896static unsigned int lockdep_dependency_gen_id;
897
Peter Zijlstraaf012962009-07-16 15:44:29 +0200898static inline void __cq_init(struct circular_queue *cq)
899{
900 cq->front = cq->rear = 0;
Ming Leie351b662009-07-22 22:48:09 +0800901 lockdep_dependency_gen_id++;
Peter Zijlstraaf012962009-07-16 15:44:29 +0200902}
903
904static inline int __cq_empty(struct circular_queue *cq)
905{
906 return (cq->front == cq->rear);
907}
908
909static inline int __cq_full(struct circular_queue *cq)
910{
911 return ((cq->rear + 1) & CQ_MASK) == cq->front;
912}
913
914static inline int __cq_enqueue(struct circular_queue *cq, unsigned long elem)
915{
916 if (__cq_full(cq))
917 return -1;
918
919 cq->element[cq->rear] = elem;
920 cq->rear = (cq->rear + 1) & CQ_MASK;
921 return 0;
922}
923
924static inline int __cq_dequeue(struct circular_queue *cq, unsigned long *elem)
925{
926 if (__cq_empty(cq))
927 return -1;
928
929 *elem = cq->element[cq->front];
930 cq->front = (cq->front + 1) & CQ_MASK;
931 return 0;
932}
933
934static inline unsigned int __cq_get_elem_count(struct circular_queue *cq)
935{
936 return (cq->rear - cq->front) & CQ_MASK;
937}
938
939static inline void mark_lock_accessed(struct lock_list *lock,
940 struct lock_list *parent)
941{
942 unsigned long nr;
Peter Zijlstra98c33ed2009-07-21 13:19:07 +0200943
Peter Zijlstraaf012962009-07-16 15:44:29 +0200944 nr = lock - list_entries;
Peter Zijlstra0119fee2011-09-02 01:30:29 +0200945 WARN_ON(nr >= nr_list_entries); /* Out-of-bounds, input fail */
Peter Zijlstraaf012962009-07-16 15:44:29 +0200946 lock->parent = parent;
Ming Leie351b662009-07-22 22:48:09 +0800947 lock->class->dep_gen_id = lockdep_dependency_gen_id;
Peter Zijlstraaf012962009-07-16 15:44:29 +0200948}
949
950static inline unsigned long lock_accessed(struct lock_list *lock)
951{
952 unsigned long nr;
Peter Zijlstra98c33ed2009-07-21 13:19:07 +0200953
Peter Zijlstraaf012962009-07-16 15:44:29 +0200954 nr = lock - list_entries;
Peter Zijlstra0119fee2011-09-02 01:30:29 +0200955 WARN_ON(nr >= nr_list_entries); /* Out-of-bounds, input fail */
Ming Leie351b662009-07-22 22:48:09 +0800956 return lock->class->dep_gen_id == lockdep_dependency_gen_id;
Peter Zijlstraaf012962009-07-16 15:44:29 +0200957}
958
959static inline struct lock_list *get_lock_parent(struct lock_list *child)
960{
961 return child->parent;
962}
963
964static inline int get_lock_depth(struct lock_list *child)
965{
966 int depth = 0;
967 struct lock_list *parent;
968
969 while ((parent = get_lock_parent(child))) {
970 child = parent;
971 depth++;
972 }
973 return depth;
974}
975
Ming Lei9e2d5512009-07-16 15:44:29 +0200976static int __bfs(struct lock_list *source_entry,
Peter Zijlstraaf012962009-07-16 15:44:29 +0200977 void *data,
978 int (*match)(struct lock_list *entry, void *data),
979 struct lock_list **target_entry,
980 int forward)
Ming Leic94aa5c2009-07-16 15:44:29 +0200981{
982 struct lock_list *entry;
Ming Leid588e462009-07-16 15:44:29 +0200983 struct list_head *head;
Ming Leic94aa5c2009-07-16 15:44:29 +0200984 struct circular_queue *cq = &lock_cq;
985 int ret = 1;
986
Ming Lei9e2d5512009-07-16 15:44:29 +0200987 if (match(source_entry, data)) {
Ming Leic94aa5c2009-07-16 15:44:29 +0200988 *target_entry = source_entry;
989 ret = 0;
990 goto exit;
991 }
992
Ming Leid588e462009-07-16 15:44:29 +0200993 if (forward)
994 head = &source_entry->class->locks_after;
995 else
996 head = &source_entry->class->locks_before;
997
998 if (list_empty(head))
999 goto exit;
1000
1001 __cq_init(cq);
Ming Leic94aa5c2009-07-16 15:44:29 +02001002 __cq_enqueue(cq, (unsigned long)source_entry);
1003
1004 while (!__cq_empty(cq)) {
1005 struct lock_list *lock;
Ming Leic94aa5c2009-07-16 15:44:29 +02001006
1007 __cq_dequeue(cq, (unsigned long *)&lock);
1008
1009 if (!lock->class) {
1010 ret = -2;
1011 goto exit;
1012 }
1013
1014 if (forward)
1015 head = &lock->class->locks_after;
1016 else
1017 head = &lock->class->locks_before;
1018
1019 list_for_each_entry(entry, head, entry) {
1020 if (!lock_accessed(entry)) {
Ming Lei12f3dfd2009-07-16 15:44:29 +02001021 unsigned int cq_depth;
Ming Leic94aa5c2009-07-16 15:44:29 +02001022 mark_lock_accessed(entry, lock);
Ming Lei9e2d5512009-07-16 15:44:29 +02001023 if (match(entry, data)) {
Ming Leic94aa5c2009-07-16 15:44:29 +02001024 *target_entry = entry;
1025 ret = 0;
1026 goto exit;
1027 }
1028
1029 if (__cq_enqueue(cq, (unsigned long)entry)) {
1030 ret = -1;
1031 goto exit;
1032 }
Ming Lei12f3dfd2009-07-16 15:44:29 +02001033 cq_depth = __cq_get_elem_count(cq);
1034 if (max_bfs_queue_depth < cq_depth)
1035 max_bfs_queue_depth = cq_depth;
Ming Leic94aa5c2009-07-16 15:44:29 +02001036 }
1037 }
1038 }
1039exit:
1040 return ret;
1041}
1042
Ming Leid7aaba12009-07-16 15:44:29 +02001043static inline int __bfs_forwards(struct lock_list *src_entry,
Ming Lei9e2d5512009-07-16 15:44:29 +02001044 void *data,
1045 int (*match)(struct lock_list *entry, void *data),
1046 struct lock_list **target_entry)
Ming Leic94aa5c2009-07-16 15:44:29 +02001047{
Ming Lei9e2d5512009-07-16 15:44:29 +02001048 return __bfs(src_entry, data, match, target_entry, 1);
Ming Leic94aa5c2009-07-16 15:44:29 +02001049
1050}
1051
Ming Leid7aaba12009-07-16 15:44:29 +02001052static inline int __bfs_backwards(struct lock_list *src_entry,
Ming Lei9e2d5512009-07-16 15:44:29 +02001053 void *data,
1054 int (*match)(struct lock_list *entry, void *data),
1055 struct lock_list **target_entry)
Ming Leic94aa5c2009-07-16 15:44:29 +02001056{
Ming Lei9e2d5512009-07-16 15:44:29 +02001057 return __bfs(src_entry, data, match, target_entry, 0);
Ming Leic94aa5c2009-07-16 15:44:29 +02001058
1059}
1060
Peter Zijlstra8e182572007-07-19 01:48:54 -07001061/*
1062 * Recursive, forwards-direction lock-dependency checking, used for
1063 * both noncyclic checking and for hardirq-unsafe/softirq-unsafe
1064 * checking.
Peter Zijlstra8e182572007-07-19 01:48:54 -07001065 */
Peter Zijlstra8e182572007-07-19 01:48:54 -07001066
1067/*
1068 * Print a dependency chain entry (this is only done when a deadlock
1069 * has been detected):
1070 */
1071static noinline int
Ming Lei24208ca2009-07-16 15:44:29 +02001072print_circular_bug_entry(struct lock_list *target, int depth)
Peter Zijlstra8e182572007-07-19 01:48:54 -07001073{
1074 if (debug_locks_silent)
1075 return 0;
1076 printk("\n-> #%u", depth);
1077 print_lock_name(target->class);
1078 printk(":\n");
1079 print_stack_trace(&target->trace, 6);
1080
1081 return 0;
1082}
1083
Steven Rostedtf4185812011-04-20 21:41:55 -04001084static void
1085print_circular_lock_scenario(struct held_lock *src,
1086 struct held_lock *tgt,
1087 struct lock_list *prt)
1088{
1089 struct lock_class *source = hlock_class(src);
1090 struct lock_class *target = hlock_class(tgt);
1091 struct lock_class *parent = prt->class;
1092
1093 /*
1094 * A direct locking problem where unsafe_class lock is taken
1095 * directly by safe_class lock, then all we need to show
1096 * is the deadlock scenario, as it is obvious that the
1097 * unsafe lock is taken under the safe lock.
1098 *
1099 * But if there is a chain instead, where the safe lock takes
1100 * an intermediate lock (middle_class) where this lock is
1101 * not the same as the safe lock, then the lock chain is
1102 * used to describe the problem. Otherwise we would need
1103 * to show a different CPU case for each link in the chain
1104 * from the safe_class lock to the unsafe_class lock.
1105 */
1106 if (parent != source) {
1107 printk("Chain exists of:\n ");
1108 __print_lock_name(source);
1109 printk(" --> ");
1110 __print_lock_name(parent);
1111 printk(" --> ");
1112 __print_lock_name(target);
1113 printk("\n\n");
1114 }
1115
1116 printk(" Possible unsafe locking scenario:\n\n");
1117 printk(" CPU0 CPU1\n");
1118 printk(" ---- ----\n");
1119 printk(" lock(");
1120 __print_lock_name(target);
1121 printk(");\n");
1122 printk(" lock(");
1123 __print_lock_name(parent);
1124 printk(");\n");
1125 printk(" lock(");
1126 __print_lock_name(target);
1127 printk(");\n");
1128 printk(" lock(");
1129 __print_lock_name(source);
1130 printk(");\n");
1131 printk("\n *** DEADLOCK ***\n\n");
1132}
1133
Peter Zijlstra8e182572007-07-19 01:48:54 -07001134/*
1135 * When a circular dependency is detected, print the
1136 * header first:
1137 */
1138static noinline int
Ming Leidb0002a2009-07-16 15:44:29 +02001139print_circular_bug_header(struct lock_list *entry, unsigned int depth,
1140 struct held_lock *check_src,
1141 struct held_lock *check_tgt)
Peter Zijlstra8e182572007-07-19 01:48:54 -07001142{
1143 struct task_struct *curr = current;
1144
Ming Leic94aa5c2009-07-16 15:44:29 +02001145 if (debug_locks_silent)
Peter Zijlstra8e182572007-07-19 01:48:54 -07001146 return 0;
1147
1148 printk("\n=======================================================\n");
1149 printk( "[ INFO: possible circular locking dependency detected ]\n");
1150 print_kernel_version();
1151 printk( "-------------------------------------------------------\n");
1152 printk("%s/%d is trying to acquire lock:\n",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07001153 curr->comm, task_pid_nr(curr));
Ming Leidb0002a2009-07-16 15:44:29 +02001154 print_lock(check_src);
Peter Zijlstra8e182572007-07-19 01:48:54 -07001155 printk("\nbut task is already holding lock:\n");
Ming Leidb0002a2009-07-16 15:44:29 +02001156 print_lock(check_tgt);
Peter Zijlstra8e182572007-07-19 01:48:54 -07001157 printk("\nwhich lock already depends on the new lock.\n\n");
1158 printk("\nthe existing dependency chain (in reverse order) is:\n");
1159
1160 print_circular_bug_entry(entry, depth);
1161
1162 return 0;
1163}
1164
Ming Lei9e2d5512009-07-16 15:44:29 +02001165static inline int class_equal(struct lock_list *entry, void *data)
1166{
1167 return entry->class == data;
1168}
1169
Ming Leidb0002a2009-07-16 15:44:29 +02001170static noinline int print_circular_bug(struct lock_list *this,
1171 struct lock_list *target,
1172 struct held_lock *check_src,
1173 struct held_lock *check_tgt)
Peter Zijlstra8e182572007-07-19 01:48:54 -07001174{
1175 struct task_struct *curr = current;
Ming Leic94aa5c2009-07-16 15:44:29 +02001176 struct lock_list *parent;
Steven Rostedtf4185812011-04-20 21:41:55 -04001177 struct lock_list *first_parent;
Ming Lei24208ca2009-07-16 15:44:29 +02001178 int depth;
Peter Zijlstra8e182572007-07-19 01:48:54 -07001179
Ming Leic94aa5c2009-07-16 15:44:29 +02001180 if (!debug_locks_off_graph_unlock() || debug_locks_silent)
Peter Zijlstra8e182572007-07-19 01:48:54 -07001181 return 0;
1182
Ming Leidb0002a2009-07-16 15:44:29 +02001183 if (!save_trace(&this->trace))
Peter Zijlstra8e182572007-07-19 01:48:54 -07001184 return 0;
1185
Ming Leic94aa5c2009-07-16 15:44:29 +02001186 depth = get_lock_depth(target);
1187
Ming Leidb0002a2009-07-16 15:44:29 +02001188 print_circular_bug_header(target, depth, check_src, check_tgt);
Ming Leic94aa5c2009-07-16 15:44:29 +02001189
1190 parent = get_lock_parent(target);
Steven Rostedtf4185812011-04-20 21:41:55 -04001191 first_parent = parent;
Ming Leic94aa5c2009-07-16 15:44:29 +02001192
1193 while (parent) {
1194 print_circular_bug_entry(parent, --depth);
1195 parent = get_lock_parent(parent);
1196 }
Peter Zijlstra8e182572007-07-19 01:48:54 -07001197
1198 printk("\nother info that might help us debug this:\n\n");
Steven Rostedtf4185812011-04-20 21:41:55 -04001199 print_circular_lock_scenario(check_src, check_tgt,
1200 first_parent);
1201
Peter Zijlstra8e182572007-07-19 01:48:54 -07001202 lockdep_print_held_locks(curr);
1203
1204 printk("\nstack backtrace:\n");
1205 dump_stack();
1206
1207 return 0;
1208}
1209
Ming Leidb0002a2009-07-16 15:44:29 +02001210static noinline int print_bfs_bug(int ret)
1211{
1212 if (!debug_locks_off_graph_unlock())
1213 return 0;
1214
Peter Zijlstra0119fee2011-09-02 01:30:29 +02001215 /*
1216 * Breadth-first-search failed, graph got corrupted?
1217 */
Ming Leidb0002a2009-07-16 15:44:29 +02001218 WARN(1, "lockdep bfs error:%d\n", ret);
1219
1220 return 0;
1221}
1222
Ming Leief681022009-07-16 15:44:29 +02001223static int noop_count(struct lock_list *entry, void *data)
David Miller419ca3f2008-07-29 21:45:03 -07001224{
Ming Leief681022009-07-16 15:44:29 +02001225 (*(unsigned long *)data)++;
1226 return 0;
David Miller419ca3f2008-07-29 21:45:03 -07001227}
1228
Ming Leief681022009-07-16 15:44:29 +02001229unsigned long __lockdep_count_forward_deps(struct lock_list *this)
1230{
1231 unsigned long count = 0;
1232 struct lock_list *uninitialized_var(target_entry);
1233
1234 __bfs_forwards(this, (void *)&count, noop_count, &target_entry);
1235
1236 return count;
1237}
David Miller419ca3f2008-07-29 21:45:03 -07001238unsigned long lockdep_count_forward_deps(struct lock_class *class)
1239{
1240 unsigned long ret, flags;
Ming Leief681022009-07-16 15:44:29 +02001241 struct lock_list this;
1242
1243 this.parent = NULL;
1244 this.class = class;
David Miller419ca3f2008-07-29 21:45:03 -07001245
1246 local_irq_save(flags);
Thomas Gleixner0199c4e2009-12-02 20:01:25 +01001247 arch_spin_lock(&lockdep_lock);
Ming Leief681022009-07-16 15:44:29 +02001248 ret = __lockdep_count_forward_deps(&this);
Thomas Gleixner0199c4e2009-12-02 20:01:25 +01001249 arch_spin_unlock(&lockdep_lock);
David Miller419ca3f2008-07-29 21:45:03 -07001250 local_irq_restore(flags);
1251
1252 return ret;
1253}
1254
Ming Leief681022009-07-16 15:44:29 +02001255unsigned long __lockdep_count_backward_deps(struct lock_list *this)
David Miller419ca3f2008-07-29 21:45:03 -07001256{
Ming Leief681022009-07-16 15:44:29 +02001257 unsigned long count = 0;
1258 struct lock_list *uninitialized_var(target_entry);
David Miller419ca3f2008-07-29 21:45:03 -07001259
Ming Leief681022009-07-16 15:44:29 +02001260 __bfs_backwards(this, (void *)&count, noop_count, &target_entry);
David Miller419ca3f2008-07-29 21:45:03 -07001261
Ming Leief681022009-07-16 15:44:29 +02001262 return count;
David Miller419ca3f2008-07-29 21:45:03 -07001263}
1264
1265unsigned long lockdep_count_backward_deps(struct lock_class *class)
1266{
1267 unsigned long ret, flags;
Ming Leief681022009-07-16 15:44:29 +02001268 struct lock_list this;
1269
1270 this.parent = NULL;
1271 this.class = class;
David Miller419ca3f2008-07-29 21:45:03 -07001272
1273 local_irq_save(flags);
Thomas Gleixner0199c4e2009-12-02 20:01:25 +01001274 arch_spin_lock(&lockdep_lock);
Ming Leief681022009-07-16 15:44:29 +02001275 ret = __lockdep_count_backward_deps(&this);
Thomas Gleixner0199c4e2009-12-02 20:01:25 +01001276 arch_spin_unlock(&lockdep_lock);
David Miller419ca3f2008-07-29 21:45:03 -07001277 local_irq_restore(flags);
1278
1279 return ret;
1280}
1281
Peter Zijlstra8e182572007-07-19 01:48:54 -07001282/*
1283 * Prove that the dependency graph starting at <entry> can not
1284 * lead to <target>. Print an error and return 0 if it does.
1285 */
1286static noinline int
Ming Leidb0002a2009-07-16 15:44:29 +02001287check_noncircular(struct lock_list *root, struct lock_class *target,
1288 struct lock_list **target_entry)
Peter Zijlstra8e182572007-07-19 01:48:54 -07001289{
Ming Leidb0002a2009-07-16 15:44:29 +02001290 int result;
Peter Zijlstra8e182572007-07-19 01:48:54 -07001291
Frederic Weisbeckerbd6d29c2010-04-06 00:10:17 +02001292 debug_atomic_inc(nr_cyclic_checks);
David Miller419ca3f2008-07-29 21:45:03 -07001293
Ming Leid7aaba12009-07-16 15:44:29 +02001294 result = __bfs_forwards(root, target, class_equal, target_entry);
Ming Leidb0002a2009-07-16 15:44:29 +02001295
1296 return result;
Peter Zijlstra8e182572007-07-19 01:48:54 -07001297}
1298
Steven Rostedt81d68a92008-05-12 21:20:42 +02001299#if defined(CONFIG_TRACE_IRQFLAGS) && defined(CONFIG_PROVE_LOCKING)
Peter Zijlstra8e182572007-07-19 01:48:54 -07001300/*
1301 * Forwards and backwards subgraph searching, for the purposes of
1302 * proving that two subgraphs can be connected by a new dependency
1303 * without creating any illegal irq-safe -> irq-unsafe lock dependency.
1304 */
Peter Zijlstra8e182572007-07-19 01:48:54 -07001305
Ming Leid7aaba12009-07-16 15:44:29 +02001306static inline int usage_match(struct lock_list *entry, void *bit)
1307{
1308 return entry->class->usage_mask & (1 << (enum lock_usage_bit)bit);
1309}
1310
1311
1312
Peter Zijlstra8e182572007-07-19 01:48:54 -07001313/*
1314 * Find a node in the forwards-direction dependency sub-graph starting
Ming Leid7aaba12009-07-16 15:44:29 +02001315 * at @root->class that matches @bit.
Peter Zijlstra8e182572007-07-19 01:48:54 -07001316 *
Ming Leid7aaba12009-07-16 15:44:29 +02001317 * Return 0 if such a node exists in the subgraph, and put that node
1318 * into *@target_entry.
Peter Zijlstra8e182572007-07-19 01:48:54 -07001319 *
Ming Leid7aaba12009-07-16 15:44:29 +02001320 * Return 1 otherwise and keep *@target_entry unchanged.
1321 * Return <0 on error.
Peter Zijlstra8e182572007-07-19 01:48:54 -07001322 */
Ming Leid7aaba12009-07-16 15:44:29 +02001323static int
1324find_usage_forwards(struct lock_list *root, enum lock_usage_bit bit,
1325 struct lock_list **target_entry)
Peter Zijlstra8e182572007-07-19 01:48:54 -07001326{
Ming Leid7aaba12009-07-16 15:44:29 +02001327 int result;
Peter Zijlstra8e182572007-07-19 01:48:54 -07001328
Frederic Weisbeckerbd6d29c2010-04-06 00:10:17 +02001329 debug_atomic_inc(nr_find_usage_forwards_checks);
Peter Zijlstra8e182572007-07-19 01:48:54 -07001330
Ming Leid7aaba12009-07-16 15:44:29 +02001331 result = __bfs_forwards(root, (void *)bit, usage_match, target_entry);
1332
1333 return result;
Peter Zijlstra8e182572007-07-19 01:48:54 -07001334}
1335
1336/*
1337 * Find a node in the backwards-direction dependency sub-graph starting
Ming Leid7aaba12009-07-16 15:44:29 +02001338 * at @root->class that matches @bit.
Peter Zijlstra8e182572007-07-19 01:48:54 -07001339 *
Ming Leid7aaba12009-07-16 15:44:29 +02001340 * Return 0 if such a node exists in the subgraph, and put that node
1341 * into *@target_entry.
Peter Zijlstra8e182572007-07-19 01:48:54 -07001342 *
Ming Leid7aaba12009-07-16 15:44:29 +02001343 * Return 1 otherwise and keep *@target_entry unchanged.
1344 * Return <0 on error.
Peter Zijlstra8e182572007-07-19 01:48:54 -07001345 */
Ming Leid7aaba12009-07-16 15:44:29 +02001346static int
1347find_usage_backwards(struct lock_list *root, enum lock_usage_bit bit,
1348 struct lock_list **target_entry)
Peter Zijlstra8e182572007-07-19 01:48:54 -07001349{
Ming Leid7aaba12009-07-16 15:44:29 +02001350 int result;
Peter Zijlstra8e182572007-07-19 01:48:54 -07001351
Frederic Weisbeckerbd6d29c2010-04-06 00:10:17 +02001352 debug_atomic_inc(nr_find_usage_backwards_checks);
Peter Zijlstra8e182572007-07-19 01:48:54 -07001353
Ming Leid7aaba12009-07-16 15:44:29 +02001354 result = __bfs_backwards(root, (void *)bit, usage_match, target_entry);
Dave Jonesf82b2172008-08-11 09:30:23 +02001355
Ming Leid7aaba12009-07-16 15:44:29 +02001356 return result;
Peter Zijlstra8e182572007-07-19 01:48:54 -07001357}
1358
Peter Zijlstraaf012962009-07-16 15:44:29 +02001359static void print_lock_class_header(struct lock_class *class, int depth)
1360{
1361 int bit;
1362
1363 printk("%*s->", depth, "");
1364 print_lock_name(class);
1365 printk(" ops: %lu", class->ops);
1366 printk(" {\n");
1367
1368 for (bit = 0; bit < LOCK_USAGE_STATES; bit++) {
1369 if (class->usage_mask & (1 << bit)) {
1370 int len = depth;
1371
1372 len += printk("%*s %s", depth, "", usage_str[bit]);
1373 len += printk(" at:\n");
1374 print_stack_trace(class->usage_traces + bit, len);
1375 }
1376 }
1377 printk("%*s }\n", depth, "");
1378
1379 printk("%*s ... key at: ",depth,"");
1380 print_ip_sym((unsigned long)class->key);
1381}
1382
1383/*
1384 * printk the shortest lock dependencies from @start to @end in reverse order:
1385 */
1386static void __used
1387print_shortest_lock_dependencies(struct lock_list *leaf,
1388 struct lock_list *root)
1389{
1390 struct lock_list *entry = leaf;
1391 int depth;
1392
1393 /*compute depth from generated tree by BFS*/
1394 depth = get_lock_depth(leaf);
1395
1396 do {
1397 print_lock_class_header(entry->class, depth);
1398 printk("%*s ... acquired at:\n", depth, "");
1399 print_stack_trace(&entry->trace, 2);
1400 printk("\n");
1401
1402 if (depth == 0 && (entry != root)) {
Steven Rostedt6be8c392011-04-20 21:41:58 -04001403 printk("lockdep:%s bad path found in chain graph\n", __func__);
Peter Zijlstraaf012962009-07-16 15:44:29 +02001404 break;
1405 }
1406
1407 entry = get_lock_parent(entry);
1408 depth--;
1409 } while (entry && (depth >= 0));
1410
1411 return;
1412}
Ming Leid7aaba12009-07-16 15:44:29 +02001413
Steven Rostedt3003eba2011-04-20 21:41:54 -04001414static void
1415print_irq_lock_scenario(struct lock_list *safe_entry,
1416 struct lock_list *unsafe_entry,
Steven Rostedtdad3d742011-04-20 21:41:57 -04001417 struct lock_class *prev_class,
1418 struct lock_class *next_class)
Steven Rostedt3003eba2011-04-20 21:41:54 -04001419{
1420 struct lock_class *safe_class = safe_entry->class;
1421 struct lock_class *unsafe_class = unsafe_entry->class;
Steven Rostedtdad3d742011-04-20 21:41:57 -04001422 struct lock_class *middle_class = prev_class;
Steven Rostedt3003eba2011-04-20 21:41:54 -04001423
1424 if (middle_class == safe_class)
Steven Rostedtdad3d742011-04-20 21:41:57 -04001425 middle_class = next_class;
Steven Rostedt3003eba2011-04-20 21:41:54 -04001426
1427 /*
1428 * A direct locking problem where unsafe_class lock is taken
1429 * directly by safe_class lock, then all we need to show
1430 * is the deadlock scenario, as it is obvious that the
1431 * unsafe lock is taken under the safe lock.
1432 *
1433 * But if there is a chain instead, where the safe lock takes
1434 * an intermediate lock (middle_class) where this lock is
1435 * not the same as the safe lock, then the lock chain is
1436 * used to describe the problem. Otherwise we would need
1437 * to show a different CPU case for each link in the chain
1438 * from the safe_class lock to the unsafe_class lock.
1439 */
1440 if (middle_class != unsafe_class) {
1441 printk("Chain exists of:\n ");
1442 __print_lock_name(safe_class);
1443 printk(" --> ");
1444 __print_lock_name(middle_class);
1445 printk(" --> ");
1446 __print_lock_name(unsafe_class);
1447 printk("\n\n");
1448 }
1449
1450 printk(" Possible interrupt unsafe locking scenario:\n\n");
1451 printk(" CPU0 CPU1\n");
1452 printk(" ---- ----\n");
1453 printk(" lock(");
1454 __print_lock_name(unsafe_class);
1455 printk(");\n");
1456 printk(" local_irq_disable();\n");
1457 printk(" lock(");
1458 __print_lock_name(safe_class);
1459 printk(");\n");
1460 printk(" lock(");
1461 __print_lock_name(middle_class);
1462 printk(");\n");
1463 printk(" <Interrupt>\n");
1464 printk(" lock(");
1465 __print_lock_name(safe_class);
1466 printk(");\n");
1467 printk("\n *** DEADLOCK ***\n\n");
1468}
1469
Peter Zijlstra8e182572007-07-19 01:48:54 -07001470static int
1471print_bad_irq_dependency(struct task_struct *curr,
Ming Lei24208ca2009-07-16 15:44:29 +02001472 struct lock_list *prev_root,
1473 struct lock_list *next_root,
1474 struct lock_list *backwards_entry,
1475 struct lock_list *forwards_entry,
Peter Zijlstra8e182572007-07-19 01:48:54 -07001476 struct held_lock *prev,
1477 struct held_lock *next,
1478 enum lock_usage_bit bit1,
1479 enum lock_usage_bit bit2,
1480 const char *irqclass)
1481{
1482 if (!debug_locks_off_graph_unlock() || debug_locks_silent)
1483 return 0;
1484
1485 printk("\n======================================================\n");
1486 printk( "[ INFO: %s-safe -> %s-unsafe lock order detected ]\n",
1487 irqclass, irqclass);
1488 print_kernel_version();
1489 printk( "------------------------------------------------------\n");
1490 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 -07001491 curr->comm, task_pid_nr(curr),
Peter Zijlstra8e182572007-07-19 01:48:54 -07001492 curr->hardirq_context, hardirq_count() >> HARDIRQ_SHIFT,
1493 curr->softirq_context, softirq_count() >> SOFTIRQ_SHIFT,
1494 curr->hardirqs_enabled,
1495 curr->softirqs_enabled);
1496 print_lock(next);
1497
1498 printk("\nand this task is already holding:\n");
1499 print_lock(prev);
1500 printk("which would create a new lock dependency:\n");
Dave Jonesf82b2172008-08-11 09:30:23 +02001501 print_lock_name(hlock_class(prev));
Peter Zijlstra8e182572007-07-19 01:48:54 -07001502 printk(" ->");
Dave Jonesf82b2172008-08-11 09:30:23 +02001503 print_lock_name(hlock_class(next));
Peter Zijlstra8e182572007-07-19 01:48:54 -07001504 printk("\n");
1505
1506 printk("\nbut this new dependency connects a %s-irq-safe lock:\n",
1507 irqclass);
Ming Lei24208ca2009-07-16 15:44:29 +02001508 print_lock_name(backwards_entry->class);
Peter Zijlstra8e182572007-07-19 01:48:54 -07001509 printk("\n... which became %s-irq-safe at:\n", irqclass);
1510
Ming Lei24208ca2009-07-16 15:44:29 +02001511 print_stack_trace(backwards_entry->class->usage_traces + bit1, 1);
Peter Zijlstra8e182572007-07-19 01:48:54 -07001512
1513 printk("\nto a %s-irq-unsafe lock:\n", irqclass);
Ming Lei24208ca2009-07-16 15:44:29 +02001514 print_lock_name(forwards_entry->class);
Peter Zijlstra8e182572007-07-19 01:48:54 -07001515 printk("\n... which became %s-irq-unsafe at:\n", irqclass);
1516 printk("...");
1517
Ming Lei24208ca2009-07-16 15:44:29 +02001518 print_stack_trace(forwards_entry->class->usage_traces + bit2, 1);
Peter Zijlstra8e182572007-07-19 01:48:54 -07001519
1520 printk("\nother info that might help us debug this:\n\n");
Steven Rostedtdad3d742011-04-20 21:41:57 -04001521 print_irq_lock_scenario(backwards_entry, forwards_entry,
1522 hlock_class(prev), hlock_class(next));
Steven Rostedt3003eba2011-04-20 21:41:54 -04001523
Peter Zijlstra8e182572007-07-19 01:48:54 -07001524 lockdep_print_held_locks(curr);
1525
Ming Lei24208ca2009-07-16 15:44:29 +02001526 printk("\nthe dependencies between %s-irq-safe lock", irqclass);
1527 printk(" and the holding lock:\n");
1528 if (!save_trace(&prev_root->trace))
1529 return 0;
1530 print_shortest_lock_dependencies(backwards_entry, prev_root);
Peter Zijlstra8e182572007-07-19 01:48:54 -07001531
Ming Lei24208ca2009-07-16 15:44:29 +02001532 printk("\nthe dependencies between the lock to be acquired");
1533 printk(" and %s-irq-unsafe lock:\n", irqclass);
1534 if (!save_trace(&next_root->trace))
1535 return 0;
1536 print_shortest_lock_dependencies(forwards_entry, next_root);
Peter Zijlstra8e182572007-07-19 01:48:54 -07001537
1538 printk("\nstack backtrace:\n");
1539 dump_stack();
1540
1541 return 0;
1542}
1543
1544static int
1545check_usage(struct task_struct *curr, struct held_lock *prev,
1546 struct held_lock *next, enum lock_usage_bit bit_backwards,
1547 enum lock_usage_bit bit_forwards, const char *irqclass)
1548{
1549 int ret;
Ming Lei24208ca2009-07-16 15:44:29 +02001550 struct lock_list this, that;
Ming Leid7aaba12009-07-16 15:44:29 +02001551 struct lock_list *uninitialized_var(target_entry);
Ming Lei24208ca2009-07-16 15:44:29 +02001552 struct lock_list *uninitialized_var(target_entry1);
Peter Zijlstra8e182572007-07-19 01:48:54 -07001553
Ming Leid7aaba12009-07-16 15:44:29 +02001554 this.parent = NULL;
Peter Zijlstra8e182572007-07-19 01:48:54 -07001555
Ming Leid7aaba12009-07-16 15:44:29 +02001556 this.class = hlock_class(prev);
1557 ret = find_usage_backwards(&this, bit_backwards, &target_entry);
Peter Zijlstraaf012962009-07-16 15:44:29 +02001558 if (ret < 0)
1559 return print_bfs_bug(ret);
1560 if (ret == 1)
1561 return ret;
Ming Leid7aaba12009-07-16 15:44:29 +02001562
Ming Lei24208ca2009-07-16 15:44:29 +02001563 that.parent = NULL;
1564 that.class = hlock_class(next);
1565 ret = find_usage_forwards(&that, bit_forwards, &target_entry1);
Peter Zijlstraaf012962009-07-16 15:44:29 +02001566 if (ret < 0)
1567 return print_bfs_bug(ret);
1568 if (ret == 1)
1569 return ret;
Ming Leid7aaba12009-07-16 15:44:29 +02001570
Ming Lei24208ca2009-07-16 15:44:29 +02001571 return print_bad_irq_dependency(curr, &this, &that,
1572 target_entry, target_entry1,
1573 prev, next,
Peter Zijlstra8e182572007-07-19 01:48:54 -07001574 bit_backwards, bit_forwards, irqclass);
1575}
1576
Peter Zijlstra4f367d8a2009-01-22 18:10:42 +01001577static const char *state_names[] = {
1578#define LOCKDEP_STATE(__STATE) \
Peter Zijlstrab4b136f2009-01-29 14:50:36 +01001579 __stringify(__STATE),
Peter Zijlstra4f367d8a2009-01-22 18:10:42 +01001580#include "lockdep_states.h"
1581#undef LOCKDEP_STATE
1582};
1583
1584static const char *state_rnames[] = {
1585#define LOCKDEP_STATE(__STATE) \
Peter Zijlstrab4b136f2009-01-29 14:50:36 +01001586 __stringify(__STATE)"-READ",
Peter Zijlstra4f367d8a2009-01-22 18:10:42 +01001587#include "lockdep_states.h"
1588#undef LOCKDEP_STATE
1589};
1590
1591static inline const char *state_name(enum lock_usage_bit bit)
1592{
1593 return (bit & 1) ? state_rnames[bit >> 2] : state_names[bit >> 2];
1594}
1595
1596static int exclusive_bit(int new_bit)
1597{
1598 /*
1599 * USED_IN
1600 * USED_IN_READ
1601 * ENABLED
1602 * ENABLED_READ
1603 *
1604 * bit 0 - write/read
1605 * bit 1 - used_in/enabled
1606 * bit 2+ state
1607 */
1608
1609 int state = new_bit & ~3;
1610 int dir = new_bit & 2;
1611
1612 /*
1613 * keep state, bit flip the direction and strip read.
1614 */
1615 return state | (dir ^ 2);
1616}
1617
1618static int check_irq_usage(struct task_struct *curr, struct held_lock *prev,
1619 struct held_lock *next, enum lock_usage_bit bit)
Peter Zijlstra8e182572007-07-19 01:48:54 -07001620{
1621 /*
1622 * Prove that the new dependency does not connect a hardirq-safe
1623 * lock with a hardirq-unsafe lock - to achieve this we search
1624 * the backwards-subgraph starting at <prev>, and the
1625 * forwards-subgraph starting at <next>:
1626 */
Peter Zijlstra4f367d8a2009-01-22 18:10:42 +01001627 if (!check_usage(curr, prev, next, bit,
1628 exclusive_bit(bit), state_name(bit)))
Peter Zijlstra8e182572007-07-19 01:48:54 -07001629 return 0;
1630
Peter Zijlstra4f367d8a2009-01-22 18:10:42 +01001631 bit++; /* _READ */
1632
Peter Zijlstra8e182572007-07-19 01:48:54 -07001633 /*
1634 * Prove that the new dependency does not connect a hardirq-safe-read
1635 * lock with a hardirq-unsafe lock - to achieve this we search
1636 * the backwards-subgraph starting at <prev>, and the
1637 * forwards-subgraph starting at <next>:
1638 */
Peter Zijlstra4f367d8a2009-01-22 18:10:42 +01001639 if (!check_usage(curr, prev, next, bit,
1640 exclusive_bit(bit), state_name(bit)))
Peter Zijlstra8e182572007-07-19 01:48:54 -07001641 return 0;
1642
Peter Zijlstra4f367d8a2009-01-22 18:10:42 +01001643 return 1;
1644}
Peter Zijlstra8e182572007-07-19 01:48:54 -07001645
Peter Zijlstra4f367d8a2009-01-22 18:10:42 +01001646static int
1647check_prev_add_irq(struct task_struct *curr, struct held_lock *prev,
1648 struct held_lock *next)
1649{
1650#define LOCKDEP_STATE(__STATE) \
1651 if (!check_irq_usage(curr, prev, next, LOCK_USED_IN_##__STATE)) \
Nick Piggincf40bd12009-01-21 08:12:39 +01001652 return 0;
Peter Zijlstra4f367d8a2009-01-22 18:10:42 +01001653#include "lockdep_states.h"
1654#undef LOCKDEP_STATE
Nick Piggincf40bd12009-01-21 08:12:39 +01001655
Peter Zijlstra8e182572007-07-19 01:48:54 -07001656 return 1;
1657}
1658
1659static void inc_chains(void)
1660{
1661 if (current->hardirq_context)
1662 nr_hardirq_chains++;
1663 else {
1664 if (current->softirq_context)
1665 nr_softirq_chains++;
1666 else
1667 nr_process_chains++;
1668 }
1669}
1670
1671#else
1672
1673static inline int
1674check_prev_add_irq(struct task_struct *curr, struct held_lock *prev,
1675 struct held_lock *next)
1676{
1677 return 1;
1678}
1679
1680static inline void inc_chains(void)
1681{
1682 nr_process_chains++;
1683}
1684
1685#endif
1686
Steven Rostedt48702ec2011-04-20 21:41:56 -04001687static void
1688print_deadlock_scenario(struct held_lock *nxt,
1689 struct held_lock *prv)
1690{
1691 struct lock_class *next = hlock_class(nxt);
1692 struct lock_class *prev = hlock_class(prv);
1693
1694 printk(" Possible unsafe locking scenario:\n\n");
1695 printk(" CPU0\n");
1696 printk(" ----\n");
1697 printk(" lock(");
1698 __print_lock_name(prev);
1699 printk(");\n");
1700 printk(" lock(");
1701 __print_lock_name(next);
1702 printk(");\n");
1703 printk("\n *** DEADLOCK ***\n\n");
1704 printk(" May be due to missing lock nesting notation\n\n");
1705}
1706
Peter Zijlstra8e182572007-07-19 01:48:54 -07001707static int
1708print_deadlock_bug(struct task_struct *curr, struct held_lock *prev,
1709 struct held_lock *next)
1710{
1711 if (!debug_locks_off_graph_unlock() || debug_locks_silent)
1712 return 0;
1713
1714 printk("\n=============================================\n");
1715 printk( "[ INFO: possible recursive locking detected ]\n");
1716 print_kernel_version();
1717 printk( "---------------------------------------------\n");
1718 printk("%s/%d is trying to acquire lock:\n",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07001719 curr->comm, task_pid_nr(curr));
Peter Zijlstra8e182572007-07-19 01:48:54 -07001720 print_lock(next);
1721 printk("\nbut task is already holding lock:\n");
1722 print_lock(prev);
1723
1724 printk("\nother info that might help us debug this:\n");
Steven Rostedt48702ec2011-04-20 21:41:56 -04001725 print_deadlock_scenario(next, prev);
Peter Zijlstra8e182572007-07-19 01:48:54 -07001726 lockdep_print_held_locks(curr);
1727
1728 printk("\nstack backtrace:\n");
1729 dump_stack();
1730
1731 return 0;
1732}
1733
1734/*
1735 * Check whether we are holding such a class already.
1736 *
1737 * (Note that this has to be done separately, because the graph cannot
1738 * detect such classes of deadlocks.)
1739 *
1740 * Returns: 0 on deadlock detected, 1 on OK, 2 on recursive read
1741 */
1742static int
1743check_deadlock(struct task_struct *curr, struct held_lock *next,
1744 struct lockdep_map *next_instance, int read)
1745{
1746 struct held_lock *prev;
Peter Zijlstra7531e2f2008-08-11 09:30:24 +02001747 struct held_lock *nest = NULL;
Peter Zijlstra8e182572007-07-19 01:48:54 -07001748 int i;
1749
1750 for (i = 0; i < curr->lockdep_depth; i++) {
1751 prev = curr->held_locks + i;
Peter Zijlstra7531e2f2008-08-11 09:30:24 +02001752
1753 if (prev->instance == next->nest_lock)
1754 nest = prev;
1755
Dave Jonesf82b2172008-08-11 09:30:23 +02001756 if (hlock_class(prev) != hlock_class(next))
Peter Zijlstra8e182572007-07-19 01:48:54 -07001757 continue;
Peter Zijlstra7531e2f2008-08-11 09:30:24 +02001758
Peter Zijlstra8e182572007-07-19 01:48:54 -07001759 /*
1760 * Allow read-after-read recursion of the same
1761 * lock class (i.e. read_lock(lock)+read_lock(lock)):
1762 */
1763 if ((read == 2) && prev->read)
1764 return 2;
Peter Zijlstra7531e2f2008-08-11 09:30:24 +02001765
1766 /*
1767 * We're holding the nest_lock, which serializes this lock's
1768 * nesting behaviour.
1769 */
1770 if (nest)
1771 return 2;
1772
Peter Zijlstra8e182572007-07-19 01:48:54 -07001773 return print_deadlock_bug(curr, prev, next);
1774 }
1775 return 1;
1776}
1777
1778/*
1779 * There was a chain-cache miss, and we are about to add a new dependency
1780 * to a previous lock. We recursively validate the following rules:
1781 *
1782 * - would the adding of the <prev> -> <next> dependency create a
1783 * circular dependency in the graph? [== circular deadlock]
1784 *
1785 * - does the new prev->next dependency connect any hardirq-safe lock
1786 * (in the full backwards-subgraph starting at <prev>) with any
1787 * hardirq-unsafe lock (in the full forwards-subgraph starting at
1788 * <next>)? [== illegal lock inversion with hardirq contexts]
1789 *
1790 * - does the new prev->next dependency connect any softirq-safe lock
1791 * (in the full backwards-subgraph starting at <prev>) with any
1792 * softirq-unsafe lock (in the full forwards-subgraph starting at
1793 * <next>)? [== illegal lock inversion with softirq contexts]
1794 *
1795 * any of these scenarios could lead to a deadlock.
1796 *
1797 * Then if all the validations pass, we add the forwards and backwards
1798 * dependency.
1799 */
1800static int
1801check_prev_add(struct task_struct *curr, struct held_lock *prev,
Yong Zhang4726f2a2010-05-04 14:16:48 +08001802 struct held_lock *next, int distance, int trylock_loop)
Peter Zijlstra8e182572007-07-19 01:48:54 -07001803{
1804 struct lock_list *entry;
1805 int ret;
Ming Leidb0002a2009-07-16 15:44:29 +02001806 struct lock_list this;
1807 struct lock_list *uninitialized_var(target_entry);
Yong Zhang4726f2a2010-05-04 14:16:48 +08001808 /*
1809 * Static variable, serialized by the graph_lock().
1810 *
1811 * We use this static variable to save the stack trace in case
1812 * we call into this function multiple times due to encountering
1813 * trylocks in the held lock stack.
1814 */
1815 static struct stack_trace trace;
Peter Zijlstra8e182572007-07-19 01:48:54 -07001816
1817 /*
1818 * Prove that the new <prev> -> <next> dependency would not
1819 * create a circular dependency in the graph. (We do this by
1820 * forward-recursing into the graph starting at <next>, and
1821 * checking whether we can reach <prev>.)
1822 *
1823 * We are using global variables to control the recursion, to
1824 * keep the stackframe size of the recursive functions low:
1825 */
Ming Leidb0002a2009-07-16 15:44:29 +02001826 this.class = hlock_class(next);
1827 this.parent = NULL;
1828 ret = check_noncircular(&this, hlock_class(prev), &target_entry);
1829 if (unlikely(!ret))
1830 return print_circular_bug(&this, target_entry, next, prev);
1831 else if (unlikely(ret < 0))
1832 return print_bfs_bug(ret);
Ming Leic94aa5c2009-07-16 15:44:29 +02001833
Peter Zijlstra8e182572007-07-19 01:48:54 -07001834 if (!check_prev_add_irq(curr, prev, next))
1835 return 0;
1836
1837 /*
1838 * For recursive read-locks we do all the dependency checks,
1839 * but we dont store read-triggered dependencies (only
1840 * write-triggered dependencies). This ensures that only the
1841 * write-side dependencies matter, and that if for example a
1842 * write-lock never takes any other locks, then the reads are
1843 * equivalent to a NOP.
1844 */
1845 if (next->read == 2 || prev->read == 2)
1846 return 1;
1847 /*
1848 * Is the <prev> -> <next> dependency already present?
1849 *
1850 * (this may occur even though this is a new chain: consider
1851 * e.g. the L1 -> L2 -> L3 -> L4 and the L5 -> L1 -> L2 -> L3
1852 * chains - the second one will be new, but L1 already has
1853 * L2 added to its dependency list, due to the first chain.)
1854 */
Dave Jonesf82b2172008-08-11 09:30:23 +02001855 list_for_each_entry(entry, &hlock_class(prev)->locks_after, entry) {
1856 if (entry->class == hlock_class(next)) {
Peter Zijlstra8e182572007-07-19 01:48:54 -07001857 if (distance == 1)
1858 entry->distance = 1;
1859 return 2;
1860 }
1861 }
1862
Yong Zhang4726f2a2010-05-04 14:16:48 +08001863 if (!trylock_loop && !save_trace(&trace))
1864 return 0;
1865
Peter Zijlstra8e182572007-07-19 01:48:54 -07001866 /*
1867 * Ok, all validations passed, add the new lock
1868 * to the previous lock's dependency list:
1869 */
Dave Jonesf82b2172008-08-11 09:30:23 +02001870 ret = add_lock_to_list(hlock_class(prev), hlock_class(next),
1871 &hlock_class(prev)->locks_after,
Yong Zhang4726f2a2010-05-04 14:16:48 +08001872 next->acquire_ip, distance, &trace);
Peter Zijlstra8e182572007-07-19 01:48:54 -07001873
1874 if (!ret)
1875 return 0;
1876
Dave Jonesf82b2172008-08-11 09:30:23 +02001877 ret = add_lock_to_list(hlock_class(next), hlock_class(prev),
1878 &hlock_class(next)->locks_before,
Yong Zhang4726f2a2010-05-04 14:16:48 +08001879 next->acquire_ip, distance, &trace);
Peter Zijlstra8e182572007-07-19 01:48:54 -07001880 if (!ret)
1881 return 0;
1882
1883 /*
1884 * Debugging printouts:
1885 */
Dave Jonesf82b2172008-08-11 09:30:23 +02001886 if (verbose(hlock_class(prev)) || verbose(hlock_class(next))) {
Peter Zijlstra8e182572007-07-19 01:48:54 -07001887 graph_unlock();
1888 printk("\n new dependency: ");
Dave Jonesf82b2172008-08-11 09:30:23 +02001889 print_lock_name(hlock_class(prev));
Peter Zijlstra8e182572007-07-19 01:48:54 -07001890 printk(" => ");
Dave Jonesf82b2172008-08-11 09:30:23 +02001891 print_lock_name(hlock_class(next));
Peter Zijlstra8e182572007-07-19 01:48:54 -07001892 printk("\n");
1893 dump_stack();
1894 return graph_lock();
1895 }
1896 return 1;
1897}
1898
1899/*
1900 * Add the dependency to all directly-previous locks that are 'relevant'.
1901 * The ones that are relevant are (in increasing distance from curr):
1902 * all consecutive trylock entries and the final non-trylock entry - or
1903 * the end of this context's lock-chain - whichever comes first.
1904 */
1905static int
1906check_prevs_add(struct task_struct *curr, struct held_lock *next)
1907{
1908 int depth = curr->lockdep_depth;
Yong Zhang4726f2a2010-05-04 14:16:48 +08001909 int trylock_loop = 0;
Peter Zijlstra8e182572007-07-19 01:48:54 -07001910 struct held_lock *hlock;
1911
1912 /*
1913 * Debugging checks.
1914 *
1915 * Depth must not be zero for a non-head lock:
1916 */
1917 if (!depth)
1918 goto out_bug;
1919 /*
1920 * At least two relevant locks must exist for this
1921 * to be a head:
1922 */
1923 if (curr->held_locks[depth].irq_context !=
1924 curr->held_locks[depth-1].irq_context)
1925 goto out_bug;
1926
1927 for (;;) {
1928 int distance = curr->lockdep_depth - depth + 1;
1929 hlock = curr->held_locks + depth-1;
1930 /*
1931 * Only non-recursive-read entries get new dependencies
1932 * added:
1933 */
1934 if (hlock->read != 2) {
Yong Zhang4726f2a2010-05-04 14:16:48 +08001935 if (!check_prev_add(curr, hlock, next,
1936 distance, trylock_loop))
Peter Zijlstra8e182572007-07-19 01:48:54 -07001937 return 0;
1938 /*
1939 * Stop after the first non-trylock entry,
1940 * as non-trylock entries have added their
1941 * own direct dependencies already, so this
1942 * lock is connected to them indirectly:
1943 */
1944 if (!hlock->trylock)
1945 break;
1946 }
1947 depth--;
1948 /*
1949 * End of lock-stack?
1950 */
1951 if (!depth)
1952 break;
1953 /*
1954 * Stop the search if we cross into another context:
1955 */
1956 if (curr->held_locks[depth].irq_context !=
1957 curr->held_locks[depth-1].irq_context)
1958 break;
Yong Zhang4726f2a2010-05-04 14:16:48 +08001959 trylock_loop = 1;
Peter Zijlstra8e182572007-07-19 01:48:54 -07001960 }
1961 return 1;
1962out_bug:
1963 if (!debug_locks_off_graph_unlock())
1964 return 0;
1965
Peter Zijlstra0119fee2011-09-02 01:30:29 +02001966 /*
1967 * Clearly we all shouldn't be here, but since we made it we
1968 * can reliable say we messed up our state. See the above two
1969 * gotos for reasons why we could possibly end up here.
1970 */
Peter Zijlstra8e182572007-07-19 01:48:54 -07001971 WARN_ON(1);
1972
1973 return 0;
1974}
1975
1976unsigned long nr_lock_chains;
Huang, Ying443cd502008-06-20 16:39:21 +08001977struct lock_chain lock_chains[MAX_LOCKDEP_CHAINS];
Huang, Yingcd1a28e2008-06-23 11:20:54 +08001978int nr_chain_hlocks;
Huang, Ying443cd502008-06-20 16:39:21 +08001979static u16 chain_hlocks[MAX_LOCKDEP_CHAIN_HLOCKS];
1980
1981struct lock_class *lock_chain_get_class(struct lock_chain *chain, int i)
1982{
1983 return lock_classes + chain_hlocks[chain->base + i];
1984}
Peter Zijlstra8e182572007-07-19 01:48:54 -07001985
1986/*
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001987 * Look up a dependency chain. If the key is not present yet then
Jarek Poplawski9e860d02007-05-08 00:30:12 -07001988 * add it and return 1 - in this case the new dependency chain is
1989 * validated. If the key is already hashed, return 0.
1990 * (On return with 1 graph_lock is held.)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001991 */
Huang, Ying443cd502008-06-20 16:39:21 +08001992static inline int lookup_chain_cache(struct task_struct *curr,
1993 struct held_lock *hlock,
1994 u64 chain_key)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001995{
Dave Jonesf82b2172008-08-11 09:30:23 +02001996 struct lock_class *class = hlock_class(hlock);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001997 struct list_head *hash_head = chainhashentry(chain_key);
1998 struct lock_chain *chain;
Huang, Ying443cd502008-06-20 16:39:21 +08001999 struct held_lock *hlock_curr, *hlock_next;
Steven Rostedte0944ee2011-04-20 21:42:00 -04002000 int i, j;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002001
Peter Zijlstra0119fee2011-09-02 01:30:29 +02002002 /*
2003 * We might need to take the graph lock, ensure we've got IRQs
2004 * disabled to make this an IRQ-safe lock.. for recursion reasons
2005 * lockdep won't complain about its own locking errors.
2006 */
Jarek Poplawski381a2292007-02-10 01:44:58 -08002007 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2008 return 0;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002009 /*
2010 * We can walk it lock-free, because entries only get added
2011 * to the hash:
2012 */
2013 list_for_each_entry(chain, hash_head, entry) {
2014 if (chain->chain_key == chain_key) {
2015cache_hit:
Frederic Weisbeckerbd6d29c2010-04-06 00:10:17 +02002016 debug_atomic_inc(chain_lookup_hits);
Ingo Molnar81fc6852006-12-13 00:34:40 -08002017 if (very_verbose(class))
Andrew Morton755cd902006-12-29 16:49:14 -08002018 printk("\nhash chain already cached, key: "
2019 "%016Lx tail class: [%p] %s\n",
2020 (unsigned long long)chain_key,
2021 class->key, class->name);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002022 return 0;
2023 }
2024 }
Ingo Molnar81fc6852006-12-13 00:34:40 -08002025 if (very_verbose(class))
Andrew Morton755cd902006-12-29 16:49:14 -08002026 printk("\nnew hash chain, key: %016Lx tail class: [%p] %s\n",
2027 (unsigned long long)chain_key, class->key, class->name);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002028 /*
2029 * Allocate a new chain entry from the static array, and add
2030 * it to the hash:
2031 */
Ingo Molnar74c383f2006-12-13 00:34:43 -08002032 if (!graph_lock())
2033 return 0;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002034 /*
2035 * We have to walk the chain again locked - to avoid duplicates:
2036 */
2037 list_for_each_entry(chain, hash_head, entry) {
2038 if (chain->chain_key == chain_key) {
Ingo Molnar74c383f2006-12-13 00:34:43 -08002039 graph_unlock();
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002040 goto cache_hit;
2041 }
2042 }
2043 if (unlikely(nr_lock_chains >= MAX_LOCKDEP_CHAINS)) {
Ingo Molnar74c383f2006-12-13 00:34:43 -08002044 if (!debug_locks_off_graph_unlock())
2045 return 0;
2046
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002047 printk("BUG: MAX_LOCKDEP_CHAINS too low!\n");
2048 printk("turning off the locking correctness validator.\n");
Peter Zijlstraeedeeab2009-03-18 12:38:47 +01002049 dump_stack();
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002050 return 0;
2051 }
2052 chain = lock_chains + nr_lock_chains++;
2053 chain->chain_key = chain_key;
Huang, Ying443cd502008-06-20 16:39:21 +08002054 chain->irq_context = hlock->irq_context;
2055 /* Find the first held_lock of current chain */
2056 hlock_next = hlock;
2057 for (i = curr->lockdep_depth - 1; i >= 0; i--) {
2058 hlock_curr = curr->held_locks + i;
2059 if (hlock_curr->irq_context != hlock_next->irq_context)
2060 break;
2061 hlock_next = hlock;
2062 }
2063 i++;
2064 chain->depth = curr->lockdep_depth + 1 - i;
Steven Rostedte0944ee2011-04-20 21:42:00 -04002065 if (likely(nr_chain_hlocks + chain->depth <= MAX_LOCKDEP_CHAIN_HLOCKS)) {
2066 chain->base = nr_chain_hlocks;
2067 nr_chain_hlocks += chain->depth;
Huang, Ying443cd502008-06-20 16:39:21 +08002068 for (j = 0; j < chain->depth - 1; j++, i++) {
Dave Jonesf82b2172008-08-11 09:30:23 +02002069 int lock_id = curr->held_locks[i].class_idx - 1;
Huang, Ying443cd502008-06-20 16:39:21 +08002070 chain_hlocks[chain->base + j] = lock_id;
2071 }
2072 chain_hlocks[chain->base + j] = class - lock_classes;
2073 }
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002074 list_add_tail_rcu(&chain->entry, hash_head);
Frederic Weisbeckerbd6d29c2010-04-06 00:10:17 +02002075 debug_atomic_inc(chain_lookup_misses);
Peter Zijlstra8e182572007-07-19 01:48:54 -07002076 inc_chains();
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002077
2078 return 1;
2079}
Peter Zijlstra8e182572007-07-19 01:48:54 -07002080
2081static int validate_chain(struct task_struct *curr, struct lockdep_map *lock,
Johannes Berg4e6045f2007-10-18 23:39:55 -07002082 struct held_lock *hlock, int chain_head, u64 chain_key)
Peter Zijlstra8e182572007-07-19 01:48:54 -07002083{
2084 /*
2085 * Trylock needs to maintain the stack of held locks, but it
2086 * does not add new dependencies, because trylock can be done
2087 * in any order.
2088 *
2089 * We look up the chain_key and do the O(N^2) check and update of
2090 * the dependencies only if this is a new dependency chain.
2091 * (If lookup_chain_cache() returns with 1 it acquires
2092 * graph_lock for us)
2093 */
2094 if (!hlock->trylock && (hlock->check == 2) &&
Huang, Ying443cd502008-06-20 16:39:21 +08002095 lookup_chain_cache(curr, hlock, chain_key)) {
Peter Zijlstra8e182572007-07-19 01:48:54 -07002096 /*
2097 * Check whether last held lock:
2098 *
2099 * - is irq-safe, if this lock is irq-unsafe
2100 * - is softirq-safe, if this lock is hardirq-unsafe
2101 *
2102 * And check whether the new lock's dependency graph
2103 * could lead back to the previous lock.
2104 *
2105 * any of these scenarios could lead to a deadlock. If
2106 * All validations
2107 */
2108 int ret = check_deadlock(curr, hlock, lock, hlock->read);
2109
2110 if (!ret)
2111 return 0;
2112 /*
2113 * Mark recursive read, as we jump over it when
2114 * building dependencies (just like we jump over
2115 * trylock entries):
2116 */
2117 if (ret == 2)
2118 hlock->read = 2;
2119 /*
2120 * Add dependency only if this lock is not the head
2121 * of the chain, and if it's not a secondary read-lock:
2122 */
2123 if (!chain_head && ret != 2)
2124 if (!check_prevs_add(curr, hlock))
2125 return 0;
2126 graph_unlock();
2127 } else
2128 /* after lookup_chain_cache(): */
2129 if (unlikely(!debug_locks))
2130 return 0;
2131
2132 return 1;
2133}
2134#else
2135static inline int validate_chain(struct task_struct *curr,
2136 struct lockdep_map *lock, struct held_lock *hlock,
Gregory Haskins3aa416b2007-10-11 22:11:11 +02002137 int chain_head, u64 chain_key)
Peter Zijlstra8e182572007-07-19 01:48:54 -07002138{
2139 return 1;
2140}
Peter Zijlstraca58abc2007-07-19 01:48:53 -07002141#endif
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002142
2143/*
2144 * We are building curr_chain_key incrementally, so double-check
2145 * it from scratch, to make sure that it's done correctly:
2146 */
Steven Rostedt1d09daa2008-05-12 21:20:55 +02002147static void check_chain_key(struct task_struct *curr)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002148{
2149#ifdef CONFIG_DEBUG_LOCKDEP
2150 struct held_lock *hlock, *prev_hlock = NULL;
2151 unsigned int i, id;
2152 u64 chain_key = 0;
2153
2154 for (i = 0; i < curr->lockdep_depth; i++) {
2155 hlock = curr->held_locks + i;
2156 if (chain_key != hlock->prev_chain_key) {
2157 debug_locks_off();
Peter Zijlstra0119fee2011-09-02 01:30:29 +02002158 /*
2159 * We got mighty confused, our chain keys don't match
2160 * with what we expect, someone trample on our task state?
2161 */
Arjan van de Ven2df8b1d2008-07-30 12:43:11 -07002162 WARN(1, "hm#1, depth: %u [%u], %016Lx != %016Lx\n",
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002163 curr->lockdep_depth, i,
2164 (unsigned long long)chain_key,
2165 (unsigned long long)hlock->prev_chain_key);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002166 return;
2167 }
Dave Jonesf82b2172008-08-11 09:30:23 +02002168 id = hlock->class_idx - 1;
Peter Zijlstra0119fee2011-09-02 01:30:29 +02002169 /*
2170 * Whoops ran out of static storage again?
2171 */
Jarek Poplawski381a2292007-02-10 01:44:58 -08002172 if (DEBUG_LOCKS_WARN_ON(id >= MAX_LOCKDEP_KEYS))
2173 return;
2174
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002175 if (prev_hlock && (prev_hlock->irq_context !=
2176 hlock->irq_context))
2177 chain_key = 0;
2178 chain_key = iterate_chain_key(chain_key, id);
2179 prev_hlock = hlock;
2180 }
2181 if (chain_key != curr->curr_chain_key) {
2182 debug_locks_off();
Peter Zijlstra0119fee2011-09-02 01:30:29 +02002183 /*
2184 * More smoking hash instead of calculating it, damn see these
2185 * numbers float.. I bet that a pink elephant stepped on my memory.
2186 */
Arjan van de Ven2df8b1d2008-07-30 12:43:11 -07002187 WARN(1, "hm#2, depth: %u [%u], %016Lx != %016Lx\n",
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002188 curr->lockdep_depth, i,
2189 (unsigned long long)chain_key,
2190 (unsigned long long)curr->curr_chain_key);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002191 }
2192#endif
2193}
2194
Steven Rostedt282b5c22011-04-20 21:41:59 -04002195static void
2196print_usage_bug_scenario(struct held_lock *lock)
2197{
2198 struct lock_class *class = hlock_class(lock);
2199
2200 printk(" Possible unsafe locking scenario:\n\n");
2201 printk(" CPU0\n");
2202 printk(" ----\n");
2203 printk(" lock(");
2204 __print_lock_name(class);
2205 printk(");\n");
2206 printk(" <Interrupt>\n");
2207 printk(" lock(");
2208 __print_lock_name(class);
2209 printk(");\n");
2210 printk("\n *** DEADLOCK ***\n\n");
2211}
2212
Peter Zijlstra8e182572007-07-19 01:48:54 -07002213static int
2214print_usage_bug(struct task_struct *curr, struct held_lock *this,
2215 enum lock_usage_bit prev_bit, enum lock_usage_bit new_bit)
2216{
2217 if (!debug_locks_off_graph_unlock() || debug_locks_silent)
2218 return 0;
2219
2220 printk("\n=================================\n");
2221 printk( "[ INFO: inconsistent lock state ]\n");
2222 print_kernel_version();
2223 printk( "---------------------------------\n");
2224
2225 printk("inconsistent {%s} -> {%s} usage.\n",
2226 usage_str[prev_bit], usage_str[new_bit]);
2227
2228 printk("%s/%d [HC%u[%lu]:SC%u[%lu]:HE%u:SE%u] takes:\n",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07002229 curr->comm, task_pid_nr(curr),
Peter Zijlstra8e182572007-07-19 01:48:54 -07002230 trace_hardirq_context(curr), hardirq_count() >> HARDIRQ_SHIFT,
2231 trace_softirq_context(curr), softirq_count() >> SOFTIRQ_SHIFT,
2232 trace_hardirqs_enabled(curr),
2233 trace_softirqs_enabled(curr));
2234 print_lock(this);
2235
2236 printk("{%s} state was registered at:\n", usage_str[prev_bit]);
Dave Jonesf82b2172008-08-11 09:30:23 +02002237 print_stack_trace(hlock_class(this)->usage_traces + prev_bit, 1);
Peter Zijlstra8e182572007-07-19 01:48:54 -07002238
2239 print_irqtrace_events(curr);
2240 printk("\nother info that might help us debug this:\n");
Steven Rostedt282b5c22011-04-20 21:41:59 -04002241 print_usage_bug_scenario(this);
2242
Peter Zijlstra8e182572007-07-19 01:48:54 -07002243 lockdep_print_held_locks(curr);
2244
2245 printk("\nstack backtrace:\n");
2246 dump_stack();
2247
2248 return 0;
2249}
2250
2251/*
2252 * Print out an error if an invalid bit is set:
2253 */
2254static inline int
2255valid_state(struct task_struct *curr, struct held_lock *this,
2256 enum lock_usage_bit new_bit, enum lock_usage_bit bad_bit)
2257{
Dave Jonesf82b2172008-08-11 09:30:23 +02002258 if (unlikely(hlock_class(this)->usage_mask & (1 << bad_bit)))
Peter Zijlstra8e182572007-07-19 01:48:54 -07002259 return print_usage_bug(curr, this, bad_bit, new_bit);
2260 return 1;
2261}
2262
2263static int mark_lock(struct task_struct *curr, struct held_lock *this,
2264 enum lock_usage_bit new_bit);
2265
Steven Rostedt81d68a92008-05-12 21:20:42 +02002266#if defined(CONFIG_TRACE_IRQFLAGS) && defined(CONFIG_PROVE_LOCKING)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002267
2268/*
2269 * print irq inversion bug:
2270 */
2271static int
Ming Lei24208ca2009-07-16 15:44:29 +02002272print_irq_inversion_bug(struct task_struct *curr,
2273 struct lock_list *root, struct lock_list *other,
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002274 struct held_lock *this, int forwards,
2275 const char *irqclass)
2276{
Steven Rostedtdad3d742011-04-20 21:41:57 -04002277 struct lock_list *entry = other;
2278 struct lock_list *middle = NULL;
2279 int depth;
2280
Ingo Molnar74c383f2006-12-13 00:34:43 -08002281 if (!debug_locks_off_graph_unlock() || debug_locks_silent)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002282 return 0;
2283
2284 printk("\n=========================================================\n");
2285 printk( "[ INFO: possible irq lock inversion dependency detected ]\n");
Dave Jones99de0552006-09-29 02:00:10 -07002286 print_kernel_version();
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002287 printk( "---------------------------------------------------------\n");
2288 printk("%s/%d just changed the state of lock:\n",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07002289 curr->comm, task_pid_nr(curr));
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002290 print_lock(this);
2291 if (forwards)
Peter Zijlstra26575e22009-03-04 14:53:24 +01002292 printk("but this lock took another, %s-unsafe lock in the past:\n", irqclass);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002293 else
Peter Zijlstra26575e22009-03-04 14:53:24 +01002294 printk("but this lock was taken by another, %s-safe lock in the past:\n", irqclass);
Ming Lei24208ca2009-07-16 15:44:29 +02002295 print_lock_name(other->class);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002296 printk("\n\nand interrupts could create inverse lock ordering between them.\n\n");
2297
2298 printk("\nother info that might help us debug this:\n");
Steven Rostedtdad3d742011-04-20 21:41:57 -04002299
2300 /* Find a middle lock (if one exists) */
2301 depth = get_lock_depth(other);
2302 do {
2303 if (depth == 0 && (entry != root)) {
2304 printk("lockdep:%s bad path found in chain graph\n", __func__);
2305 break;
2306 }
2307 middle = entry;
2308 entry = get_lock_parent(entry);
2309 depth--;
2310 } while (entry && entry != root && (depth >= 0));
2311 if (forwards)
2312 print_irq_lock_scenario(root, other,
2313 middle ? middle->class : root->class, other->class);
2314 else
2315 print_irq_lock_scenario(other, root,
2316 middle ? middle->class : other->class, root->class);
2317
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002318 lockdep_print_held_locks(curr);
2319
Ming Lei24208ca2009-07-16 15:44:29 +02002320 printk("\nthe shortest dependencies between 2nd lock and 1st lock:\n");
2321 if (!save_trace(&root->trace))
2322 return 0;
2323 print_shortest_lock_dependencies(other, root);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002324
2325 printk("\nstack backtrace:\n");
2326 dump_stack();
2327
2328 return 0;
2329}
2330
2331/*
2332 * Prove that in the forwards-direction subgraph starting at <this>
2333 * there is no lock matching <mask>:
2334 */
2335static int
2336check_usage_forwards(struct task_struct *curr, struct held_lock *this,
2337 enum lock_usage_bit bit, const char *irqclass)
2338{
2339 int ret;
Ming Leid7aaba12009-07-16 15:44:29 +02002340 struct lock_list root;
2341 struct lock_list *uninitialized_var(target_entry);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002342
Ming Leid7aaba12009-07-16 15:44:29 +02002343 root.parent = NULL;
2344 root.class = hlock_class(this);
2345 ret = find_usage_forwards(&root, bit, &target_entry);
Peter Zijlstraaf012962009-07-16 15:44:29 +02002346 if (ret < 0)
2347 return print_bfs_bug(ret);
2348 if (ret == 1)
2349 return ret;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002350
Ming Lei24208ca2009-07-16 15:44:29 +02002351 return print_irq_inversion_bug(curr, &root, target_entry,
Ming Leid7aaba12009-07-16 15:44:29 +02002352 this, 1, irqclass);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002353}
2354
2355/*
2356 * Prove that in the backwards-direction subgraph starting at <this>
2357 * there is no lock matching <mask>:
2358 */
2359static int
2360check_usage_backwards(struct task_struct *curr, struct held_lock *this,
2361 enum lock_usage_bit bit, const char *irqclass)
2362{
2363 int ret;
Ming Leid7aaba12009-07-16 15:44:29 +02002364 struct lock_list root;
2365 struct lock_list *uninitialized_var(target_entry);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002366
Ming Leid7aaba12009-07-16 15:44:29 +02002367 root.parent = NULL;
2368 root.class = hlock_class(this);
2369 ret = find_usage_backwards(&root, bit, &target_entry);
Peter Zijlstraaf012962009-07-16 15:44:29 +02002370 if (ret < 0)
2371 return print_bfs_bug(ret);
2372 if (ret == 1)
2373 return ret;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002374
Ming Lei24208ca2009-07-16 15:44:29 +02002375 return print_irq_inversion_bug(curr, &root, target_entry,
Oleg Nesterov48d50672010-01-26 19:16:41 +01002376 this, 0, irqclass);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002377}
2378
Ingo Molnar3117df02006-12-13 00:34:43 -08002379void print_irqtrace_events(struct task_struct *curr)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002380{
2381 printk("irq event stamp: %u\n", curr->irq_events);
2382 printk("hardirqs last enabled at (%u): ", curr->hardirq_enable_event);
2383 print_ip_sym(curr->hardirq_enable_ip);
2384 printk("hardirqs last disabled at (%u): ", curr->hardirq_disable_event);
2385 print_ip_sym(curr->hardirq_disable_ip);
2386 printk("softirqs last enabled at (%u): ", curr->softirq_enable_event);
2387 print_ip_sym(curr->softirq_enable_ip);
2388 printk("softirqs last disabled at (%u): ", curr->softirq_disable_event);
2389 print_ip_sym(curr->softirq_disable_ip);
2390}
2391
Peter Zijlstracd953022009-01-22 16:38:21 +01002392static int HARDIRQ_verbose(struct lock_class *class)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002393{
Peter Zijlstra8e182572007-07-19 01:48:54 -07002394#if HARDIRQ_VERBOSE
2395 return class_filter(class);
2396#endif
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002397 return 0;
2398}
2399
Peter Zijlstracd953022009-01-22 16:38:21 +01002400static int SOFTIRQ_verbose(struct lock_class *class)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002401{
Peter Zijlstra8e182572007-07-19 01:48:54 -07002402#if SOFTIRQ_VERBOSE
2403 return class_filter(class);
2404#endif
2405 return 0;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002406}
2407
Peter Zijlstracd953022009-01-22 16:38:21 +01002408static int RECLAIM_FS_verbose(struct lock_class *class)
Nick Piggincf40bd12009-01-21 08:12:39 +01002409{
2410#if RECLAIM_VERBOSE
2411 return class_filter(class);
2412#endif
2413 return 0;
2414}
2415
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002416#define STRICT_READ_CHECKS 1
2417
Peter Zijlstracd953022009-01-22 16:38:21 +01002418static int (*state_verbose_f[])(struct lock_class *class) = {
2419#define LOCKDEP_STATE(__STATE) \
2420 __STATE##_verbose,
2421#include "lockdep_states.h"
2422#undef LOCKDEP_STATE
2423};
2424
2425static inline int state_verbose(enum lock_usage_bit bit,
2426 struct lock_class *class)
2427{
2428 return state_verbose_f[bit >> 2](class);
2429}
2430
Peter Zijlstra42c50d52009-01-22 16:58:16 +01002431typedef int (*check_usage_f)(struct task_struct *, struct held_lock *,
2432 enum lock_usage_bit bit, const char *name);
2433
Peter Zijlstra6a6904d2009-01-22 16:07:44 +01002434static int
Peter Zijlstra1c21f142009-03-04 13:51:13 +01002435mark_lock_irq(struct task_struct *curr, struct held_lock *this,
2436 enum lock_usage_bit new_bit)
Peter Zijlstra6a6904d2009-01-22 16:07:44 +01002437{
Peter Zijlstraf9892092009-01-22 16:09:59 +01002438 int excl_bit = exclusive_bit(new_bit);
Peter Zijlstra9d3651a2009-01-22 17:18:32 +01002439 int read = new_bit & 1;
Peter Zijlstra42c50d52009-01-22 16:58:16 +01002440 int dir = new_bit & 2;
2441
Peter Zijlstra38aa2712009-01-27 14:53:50 +01002442 /*
2443 * mark USED_IN has to look forwards -- to ensure no dependency
2444 * has ENABLED state, which would allow recursion deadlocks.
2445 *
2446 * mark ENABLED has to look backwards -- to ensure no dependee
2447 * has USED_IN state, which, again, would allow recursion deadlocks.
2448 */
Peter Zijlstra42c50d52009-01-22 16:58:16 +01002449 check_usage_f usage = dir ?
2450 check_usage_backwards : check_usage_forwards;
Peter Zijlstraf9892092009-01-22 16:09:59 +01002451
Peter Zijlstra38aa2712009-01-27 14:53:50 +01002452 /*
2453 * Validate that this particular lock does not have conflicting
2454 * usage states.
2455 */
Peter Zijlstra6a6904d2009-01-22 16:07:44 +01002456 if (!valid_state(curr, this, new_bit, excl_bit))
2457 return 0;
Peter Zijlstra9d3651a2009-01-22 17:18:32 +01002458
Peter Zijlstra38aa2712009-01-27 14:53:50 +01002459 /*
2460 * Validate that the lock dependencies don't have conflicting usage
2461 * states.
2462 */
2463 if ((!read || !dir || STRICT_READ_CHECKS) &&
Peter Zijlstra1c21f142009-03-04 13:51:13 +01002464 !usage(curr, this, excl_bit, state_name(new_bit & ~1)))
Peter Zijlstra6a6904d2009-01-22 16:07:44 +01002465 return 0;
Peter Zijlstra780e8202009-01-22 16:51:29 +01002466
Peter Zijlstra38aa2712009-01-27 14:53:50 +01002467 /*
2468 * Check for read in write conflicts
2469 */
2470 if (!read) {
2471 if (!valid_state(curr, this, new_bit, excl_bit + 1))
2472 return 0;
2473
2474 if (STRICT_READ_CHECKS &&
Peter Zijlstra4f367d8a2009-01-22 18:10:42 +01002475 !usage(curr, this, excl_bit + 1,
2476 state_name(new_bit + 1)))
Peter Zijlstra38aa2712009-01-27 14:53:50 +01002477 return 0;
2478 }
Peter Zijlstra780e8202009-01-22 16:51:29 +01002479
Peter Zijlstracd953022009-01-22 16:38:21 +01002480 if (state_verbose(new_bit, hlock_class(this)))
Peter Zijlstra6a6904d2009-01-22 16:07:44 +01002481 return 2;
2482
2483 return 1;
2484}
2485
Nick Piggincf40bd12009-01-21 08:12:39 +01002486enum mark_type {
Peter Zijlstra36bfb9b2009-01-22 14:12:41 +01002487#define LOCKDEP_STATE(__STATE) __STATE,
2488#include "lockdep_states.h"
2489#undef LOCKDEP_STATE
Nick Piggincf40bd12009-01-21 08:12:39 +01002490};
2491
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002492/*
2493 * Mark all held locks with a usage bit:
2494 */
Steven Rostedt1d09daa2008-05-12 21:20:55 +02002495static int
Nick Piggincf40bd12009-01-21 08:12:39 +01002496mark_held_locks(struct task_struct *curr, enum mark_type mark)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002497{
2498 enum lock_usage_bit usage_bit;
2499 struct held_lock *hlock;
2500 int i;
2501
2502 for (i = 0; i < curr->lockdep_depth; i++) {
2503 hlock = curr->held_locks + i;
2504
Peter Zijlstracf2ad4d2009-01-27 13:58:08 +01002505 usage_bit = 2 + (mark << 2); /* ENABLED */
2506 if (hlock->read)
2507 usage_bit += 1; /* READ */
2508
2509 BUG_ON(usage_bit >= LOCK_USAGE_STATES);
Nick Piggincf40bd12009-01-21 08:12:39 +01002510
Peter Zijlstra70a06862011-07-25 12:09:59 +02002511 if (hlock_class(hlock)->key == __lockdep_no_validate__.subkeys)
Peter Zijlstraefbe2ee2011-07-07 11:39:45 +02002512 continue;
2513
Jarek Poplawski4ff773bb2007-05-08 00:31:00 -07002514 if (!mark_lock(curr, hlock, usage_bit))
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002515 return 0;
2516 }
2517
2518 return 1;
2519}
2520
2521/*
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002522 * Hardirqs will be enabled:
2523 */
Peter Zijlstradd4e5d32011-06-21 17:17:27 +02002524static void __trace_hardirqs_on_caller(unsigned long ip)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002525{
2526 struct task_struct *curr = current;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002527
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002528 /* we'll do an OFF -> ON transition: */
2529 curr->hardirqs_enabled = 1;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002530
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002531 /*
2532 * We are going to turn hardirqs on, so set the
2533 * usage bit for all held locks:
2534 */
Nick Piggincf40bd12009-01-21 08:12:39 +01002535 if (!mark_held_locks(curr, HARDIRQ))
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002536 return;
2537 /*
2538 * If we have softirqs enabled, then set the usage
2539 * bit for all held locks. (disabled hardirqs prevented
2540 * this bit from being set before)
2541 */
2542 if (curr->softirqs_enabled)
Nick Piggincf40bd12009-01-21 08:12:39 +01002543 if (!mark_held_locks(curr, SOFTIRQ))
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002544 return;
2545
2546 curr->hardirq_enable_ip = ip;
2547 curr->hardirq_enable_event = ++curr->irq_events;
Frederic Weisbeckerbd6d29c2010-04-06 00:10:17 +02002548 debug_atomic_inc(hardirqs_on_events);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002549}
Peter Zijlstradd4e5d32011-06-21 17:17:27 +02002550
2551void trace_hardirqs_on_caller(unsigned long ip)
2552{
2553 time_hardirqs_on(CALLER_ADDR0, ip);
2554
2555 if (unlikely(!debug_locks || current->lockdep_recursion))
2556 return;
2557
Peter Zijlstra7d36b262011-07-26 13:13:44 +02002558 if (unlikely(current->hardirqs_enabled)) {
2559 /*
2560 * Neither irq nor preemption are disabled here
2561 * so this is racy by nature but losing one hit
2562 * in a stat is not a big deal.
2563 */
2564 __debug_atomic_inc(redundant_hardirqs_on);
2565 return;
2566 }
2567
Peter Zijlstra0119fee2011-09-02 01:30:29 +02002568 /*
2569 * We're enabling irqs and according to our state above irqs weren't
2570 * already enabled, yet we find the hardware thinks they are in fact
2571 * enabled.. someone messed up their IRQ state tracing.
2572 */
Peter Zijlstradd4e5d32011-06-21 17:17:27 +02002573 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2574 return;
2575
Peter Zijlstra0119fee2011-09-02 01:30:29 +02002576 /*
2577 * See the fine text that goes along with this variable definition.
2578 */
Peter Zijlstra7d36b262011-07-26 13:13:44 +02002579 if (DEBUG_LOCKS_WARN_ON(unlikely(early_boot_irqs_disabled)))
2580 return;
2581
Peter Zijlstra0119fee2011-09-02 01:30:29 +02002582 /*
2583 * Can't allow enabling interrupts while in an interrupt handler,
2584 * that's general bad form and such. Recursion, limited stack etc..
2585 */
Peter Zijlstra7d36b262011-07-26 13:13:44 +02002586 if (DEBUG_LOCKS_WARN_ON(current->hardirq_context))
2587 return;
2588
Peter Zijlstradd4e5d32011-06-21 17:17:27 +02002589 current->lockdep_recursion = 1;
2590 __trace_hardirqs_on_caller(ip);
2591 current->lockdep_recursion = 0;
2592}
Steven Rostedt81d68a92008-05-12 21:20:42 +02002593EXPORT_SYMBOL(trace_hardirqs_on_caller);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002594
Steven Rostedt1d09daa2008-05-12 21:20:55 +02002595void trace_hardirqs_on(void)
Steven Rostedt81d68a92008-05-12 21:20:42 +02002596{
2597 trace_hardirqs_on_caller(CALLER_ADDR0);
2598}
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002599EXPORT_SYMBOL(trace_hardirqs_on);
2600
2601/*
2602 * Hardirqs were disabled:
2603 */
Heiko Carstens6afe40b2008-10-28 11:14:58 +01002604void trace_hardirqs_off_caller(unsigned long ip)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002605{
2606 struct task_struct *curr = current;
2607
Heiko Carstens6afe40b2008-10-28 11:14:58 +01002608 time_hardirqs_off(CALLER_ADDR0, ip);
Steven Rostedt81d68a92008-05-12 21:20:42 +02002609
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002610 if (unlikely(!debug_locks || current->lockdep_recursion))
2611 return;
2612
Peter Zijlstra0119fee2011-09-02 01:30:29 +02002613 /*
2614 * So we're supposed to get called after you mask local IRQs, but for
2615 * some reason the hardware doesn't quite think you did a proper job.
2616 */
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002617 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2618 return;
2619
2620 if (curr->hardirqs_enabled) {
2621 /*
2622 * We have done an ON -> OFF transition:
2623 */
2624 curr->hardirqs_enabled = 0;
Heiko Carstens6afe40b2008-10-28 11:14:58 +01002625 curr->hardirq_disable_ip = ip;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002626 curr->hardirq_disable_event = ++curr->irq_events;
Frederic Weisbeckerbd6d29c2010-04-06 00:10:17 +02002627 debug_atomic_inc(hardirqs_off_events);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002628 } else
Frederic Weisbeckerbd6d29c2010-04-06 00:10:17 +02002629 debug_atomic_inc(redundant_hardirqs_off);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002630}
Steven Rostedt81d68a92008-05-12 21:20:42 +02002631EXPORT_SYMBOL(trace_hardirqs_off_caller);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002632
Steven Rostedt1d09daa2008-05-12 21:20:55 +02002633void trace_hardirqs_off(void)
Steven Rostedt81d68a92008-05-12 21:20:42 +02002634{
2635 trace_hardirqs_off_caller(CALLER_ADDR0);
2636}
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002637EXPORT_SYMBOL(trace_hardirqs_off);
2638
2639/*
2640 * Softirqs will be enabled:
2641 */
2642void trace_softirqs_on(unsigned long ip)
2643{
2644 struct task_struct *curr = current;
2645
Peter Zijlstradd4e5d32011-06-21 17:17:27 +02002646 if (unlikely(!debug_locks || current->lockdep_recursion))
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002647 return;
2648
Peter Zijlstra0119fee2011-09-02 01:30:29 +02002649 /*
2650 * We fancy IRQs being disabled here, see softirq.c, avoids
2651 * funny state and nesting things.
2652 */
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002653 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2654 return;
2655
2656 if (curr->softirqs_enabled) {
Frederic Weisbeckerbd6d29c2010-04-06 00:10:17 +02002657 debug_atomic_inc(redundant_softirqs_on);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002658 return;
2659 }
2660
Peter Zijlstradd4e5d32011-06-21 17:17:27 +02002661 current->lockdep_recursion = 1;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002662 /*
2663 * We'll do an OFF -> ON transition:
2664 */
2665 curr->softirqs_enabled = 1;
2666 curr->softirq_enable_ip = ip;
2667 curr->softirq_enable_event = ++curr->irq_events;
Frederic Weisbeckerbd6d29c2010-04-06 00:10:17 +02002668 debug_atomic_inc(softirqs_on_events);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002669 /*
2670 * We are going to turn softirqs on, so set the
2671 * usage bit for all held locks, if hardirqs are
2672 * enabled too:
2673 */
2674 if (curr->hardirqs_enabled)
Nick Piggincf40bd12009-01-21 08:12:39 +01002675 mark_held_locks(curr, SOFTIRQ);
Peter Zijlstradd4e5d32011-06-21 17:17:27 +02002676 current->lockdep_recursion = 0;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002677}
2678
2679/*
2680 * Softirqs were disabled:
2681 */
2682void trace_softirqs_off(unsigned long ip)
2683{
2684 struct task_struct *curr = current;
2685
Peter Zijlstradd4e5d32011-06-21 17:17:27 +02002686 if (unlikely(!debug_locks || current->lockdep_recursion))
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002687 return;
2688
Peter Zijlstra0119fee2011-09-02 01:30:29 +02002689 /*
2690 * We fancy IRQs being disabled here, see softirq.c
2691 */
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002692 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2693 return;
2694
2695 if (curr->softirqs_enabled) {
2696 /*
2697 * We have done an ON -> OFF transition:
2698 */
2699 curr->softirqs_enabled = 0;
2700 curr->softirq_disable_ip = ip;
2701 curr->softirq_disable_event = ++curr->irq_events;
Frederic Weisbeckerbd6d29c2010-04-06 00:10:17 +02002702 debug_atomic_inc(softirqs_off_events);
Peter Zijlstra0119fee2011-09-02 01:30:29 +02002703 /*
2704 * Whoops, we wanted softirqs off, so why aren't they?
2705 */
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002706 DEBUG_LOCKS_WARN_ON(!softirq_count());
2707 } else
Frederic Weisbeckerbd6d29c2010-04-06 00:10:17 +02002708 debug_atomic_inc(redundant_softirqs_off);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002709}
2710
Peter Zijlstra2f850182009-03-20 11:13:20 +01002711static void __lockdep_trace_alloc(gfp_t gfp_mask, unsigned long flags)
Nick Piggincf40bd12009-01-21 08:12:39 +01002712{
2713 struct task_struct *curr = current;
2714
2715 if (unlikely(!debug_locks))
2716 return;
2717
2718 /* no reclaim without waiting on it */
2719 if (!(gfp_mask & __GFP_WAIT))
2720 return;
2721
2722 /* this guy won't enter reclaim */
2723 if ((curr->flags & PF_MEMALLOC) && !(gfp_mask & __GFP_NOMEMALLOC))
2724 return;
2725
2726 /* We're only interested __GFP_FS allocations for now */
2727 if (!(gfp_mask & __GFP_FS))
2728 return;
2729
Peter Zijlstra0119fee2011-09-02 01:30:29 +02002730 /*
2731 * Oi! Can't be having __GFP_FS allocations with IRQs disabled.
2732 */
Peter Zijlstra2f850182009-03-20 11:13:20 +01002733 if (DEBUG_LOCKS_WARN_ON(irqs_disabled_flags(flags)))
Nick Piggincf40bd12009-01-21 08:12:39 +01002734 return;
2735
2736 mark_held_locks(curr, RECLAIM_FS);
2737}
2738
Peter Zijlstra2f850182009-03-20 11:13:20 +01002739static void check_flags(unsigned long flags);
2740
2741void lockdep_trace_alloc(gfp_t gfp_mask)
2742{
2743 unsigned long flags;
2744
2745 if (unlikely(current->lockdep_recursion))
2746 return;
2747
2748 raw_local_irq_save(flags);
2749 check_flags(flags);
2750 current->lockdep_recursion = 1;
2751 __lockdep_trace_alloc(gfp_mask, flags);
2752 current->lockdep_recursion = 0;
2753 raw_local_irq_restore(flags);
2754}
2755
Peter Zijlstra8e182572007-07-19 01:48:54 -07002756static int mark_irqflags(struct task_struct *curr, struct held_lock *hlock)
2757{
2758 /*
2759 * If non-trylock use in a hardirq or softirq context, then
2760 * mark the lock as used in these contexts:
2761 */
2762 if (!hlock->trylock) {
2763 if (hlock->read) {
2764 if (curr->hardirq_context)
2765 if (!mark_lock(curr, hlock,
2766 LOCK_USED_IN_HARDIRQ_READ))
2767 return 0;
2768 if (curr->softirq_context)
2769 if (!mark_lock(curr, hlock,
2770 LOCK_USED_IN_SOFTIRQ_READ))
2771 return 0;
2772 } else {
2773 if (curr->hardirq_context)
2774 if (!mark_lock(curr, hlock, LOCK_USED_IN_HARDIRQ))
2775 return 0;
2776 if (curr->softirq_context)
2777 if (!mark_lock(curr, hlock, LOCK_USED_IN_SOFTIRQ))
2778 return 0;
2779 }
2780 }
2781 if (!hlock->hardirqs_off) {
2782 if (hlock->read) {
2783 if (!mark_lock(curr, hlock,
Peter Zijlstra4fc95e82009-01-22 13:10:52 +01002784 LOCK_ENABLED_HARDIRQ_READ))
Peter Zijlstra8e182572007-07-19 01:48:54 -07002785 return 0;
2786 if (curr->softirqs_enabled)
2787 if (!mark_lock(curr, hlock,
Peter Zijlstra4fc95e82009-01-22 13:10:52 +01002788 LOCK_ENABLED_SOFTIRQ_READ))
Peter Zijlstra8e182572007-07-19 01:48:54 -07002789 return 0;
2790 } else {
2791 if (!mark_lock(curr, hlock,
Peter Zijlstra4fc95e82009-01-22 13:10:52 +01002792 LOCK_ENABLED_HARDIRQ))
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))
Peter Zijlstra8e182572007-07-19 01:48:54 -07002797 return 0;
2798 }
2799 }
2800
Nick Piggincf40bd12009-01-21 08:12:39 +01002801 /*
2802 * We reuse the irq context infrastructure more broadly as a general
2803 * context checking code. This tests GFP_FS recursion (a lock taken
2804 * during reclaim for a GFP_FS allocation is held over a GFP_FS
2805 * allocation).
2806 */
2807 if (!hlock->trylock && (curr->lockdep_reclaim_gfp & __GFP_FS)) {
2808 if (hlock->read) {
2809 if (!mark_lock(curr, hlock, LOCK_USED_IN_RECLAIM_FS_READ))
2810 return 0;
2811 } else {
2812 if (!mark_lock(curr, hlock, LOCK_USED_IN_RECLAIM_FS))
2813 return 0;
2814 }
2815 }
2816
Peter Zijlstra8e182572007-07-19 01:48:54 -07002817 return 1;
2818}
2819
2820static int separate_irq_context(struct task_struct *curr,
2821 struct held_lock *hlock)
2822{
2823 unsigned int depth = curr->lockdep_depth;
2824
2825 /*
2826 * Keep track of points where we cross into an interrupt context:
2827 */
2828 hlock->irq_context = 2*(curr->hardirq_context ? 1 : 0) +
2829 curr->softirq_context;
2830 if (depth) {
2831 struct held_lock *prev_hlock;
2832
2833 prev_hlock = curr->held_locks + depth-1;
2834 /*
2835 * If we cross into another context, reset the
2836 * hash key (this also prevents the checking and the
2837 * adding of the dependency to 'prev'):
2838 */
2839 if (prev_hlock->irq_context != hlock->irq_context)
2840 return 1;
2841 }
2842 return 0;
2843}
2844
Peter Zijlstra0119fee2011-09-02 01:30:29 +02002845#else /* defined(CONFIG_TRACE_IRQFLAGS) && defined(CONFIG_PROVE_LOCKING) */
Peter Zijlstra8e182572007-07-19 01:48:54 -07002846
2847static inline
2848int mark_lock_irq(struct task_struct *curr, struct held_lock *this,
2849 enum lock_usage_bit new_bit)
2850{
Peter Zijlstra0119fee2011-09-02 01:30:29 +02002851 WARN_ON(1); /* Impossible innit? when we don't have TRACE_IRQFLAG */
Peter Zijlstra8e182572007-07-19 01:48:54 -07002852 return 1;
2853}
2854
2855static inline int mark_irqflags(struct task_struct *curr,
2856 struct held_lock *hlock)
2857{
2858 return 1;
2859}
2860
2861static inline int separate_irq_context(struct task_struct *curr,
2862 struct held_lock *hlock)
2863{
2864 return 0;
2865}
2866
Peter Zijlstra868a23a2009-02-15 00:25:21 +01002867void lockdep_trace_alloc(gfp_t gfp_mask)
2868{
2869}
2870
Peter Zijlstra0119fee2011-09-02 01:30:29 +02002871#endif /* defined(CONFIG_TRACE_IRQFLAGS) && defined(CONFIG_PROVE_LOCKING) */
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002872
2873/*
Peter Zijlstra8e182572007-07-19 01:48:54 -07002874 * Mark a lock with a usage bit, and validate the state transition:
2875 */
Steven Rostedt1d09daa2008-05-12 21:20:55 +02002876static int mark_lock(struct task_struct *curr, struct held_lock *this,
Steven Rostedt0764d232008-05-12 21:20:44 +02002877 enum lock_usage_bit new_bit)
Peter Zijlstra8e182572007-07-19 01:48:54 -07002878{
2879 unsigned int new_mask = 1 << new_bit, ret = 1;
2880
2881 /*
2882 * If already set then do not dirty the cacheline,
2883 * nor do any checks:
2884 */
Dave Jonesf82b2172008-08-11 09:30:23 +02002885 if (likely(hlock_class(this)->usage_mask & new_mask))
Peter Zijlstra8e182572007-07-19 01:48:54 -07002886 return 1;
2887
2888 if (!graph_lock())
2889 return 0;
2890 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002891 * Make sure we didn't race:
Peter Zijlstra8e182572007-07-19 01:48:54 -07002892 */
Dave Jonesf82b2172008-08-11 09:30:23 +02002893 if (unlikely(hlock_class(this)->usage_mask & new_mask)) {
Peter Zijlstra8e182572007-07-19 01:48:54 -07002894 graph_unlock();
2895 return 1;
2896 }
2897
Dave Jonesf82b2172008-08-11 09:30:23 +02002898 hlock_class(this)->usage_mask |= new_mask;
Peter Zijlstra8e182572007-07-19 01:48:54 -07002899
Dave Jonesf82b2172008-08-11 09:30:23 +02002900 if (!save_trace(hlock_class(this)->usage_traces + new_bit))
Peter Zijlstra8e182572007-07-19 01:48:54 -07002901 return 0;
2902
2903 switch (new_bit) {
Peter Zijlstra53464172009-01-22 14:15:53 +01002904#define LOCKDEP_STATE(__STATE) \
2905 case LOCK_USED_IN_##__STATE: \
2906 case LOCK_USED_IN_##__STATE##_READ: \
2907 case LOCK_ENABLED_##__STATE: \
2908 case LOCK_ENABLED_##__STATE##_READ:
2909#include "lockdep_states.h"
2910#undef LOCKDEP_STATE
Peter Zijlstra8e182572007-07-19 01:48:54 -07002911 ret = mark_lock_irq(curr, this, new_bit);
2912 if (!ret)
2913 return 0;
2914 break;
2915 case LOCK_USED:
Frederic Weisbeckerbd6d29c2010-04-06 00:10:17 +02002916 debug_atomic_dec(nr_unused_locks);
Peter Zijlstra8e182572007-07-19 01:48:54 -07002917 break;
2918 default:
2919 if (!debug_locks_off_graph_unlock())
2920 return 0;
2921 WARN_ON(1);
2922 return 0;
2923 }
2924
2925 graph_unlock();
2926
2927 /*
2928 * We must printk outside of the graph_lock:
2929 */
2930 if (ret == 2) {
2931 printk("\nmarked lock as {%s}:\n", usage_str[new_bit]);
2932 print_lock(this);
2933 print_irqtrace_events(curr);
2934 dump_stack();
2935 }
2936
2937 return ret;
2938}
2939
2940/*
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002941 * Initialize a lock instance's lock-class mapping info:
2942 */
2943void lockdep_init_map(struct lockdep_map *lock, const char *name,
Peter Zijlstra4dfbb9d2006-10-11 01:45:14 -04002944 struct lock_class_key *key, int subclass)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002945{
Tejun Heof59de892011-07-14 15:19:09 +02002946 memset(lock, 0, sizeof(*lock));
Hitoshi Mitake62016252010-10-05 18:01:51 +09002947
Peter Zijlstrac8a25002009-04-17 09:40:49 +02002948#ifdef CONFIG_LOCK_STAT
2949 lock->cpu = raw_smp_processor_id();
2950#endif
2951
Peter Zijlstra0119fee2011-09-02 01:30:29 +02002952 /*
2953 * Can't be having no nameless bastards around this place!
2954 */
Peter Zijlstrac8a25002009-04-17 09:40:49 +02002955 if (DEBUG_LOCKS_WARN_ON(!name)) {
2956 lock->name = "NULL";
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002957 return;
Peter Zijlstrac8a25002009-04-17 09:40:49 +02002958 }
2959
2960 lock->name = name;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002961
Peter Zijlstra0119fee2011-09-02 01:30:29 +02002962 /*
2963 * No key, no joy, we need to hash something.
2964 */
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002965 if (DEBUG_LOCKS_WARN_ON(!key))
2966 return;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002967 /*
2968 * Sanity check, the lock-class key must be persistent:
2969 */
2970 if (!static_obj(key)) {
2971 printk("BUG: key %p not in .data!\n", key);
Peter Zijlstra0119fee2011-09-02 01:30:29 +02002972 /*
2973 * What it says above ^^^^^, I suggest you read it.
2974 */
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002975 DEBUG_LOCKS_WARN_ON(1);
2976 return;
2977 }
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002978 lock->key = key;
Peter Zijlstrac8a25002009-04-17 09:40:49 +02002979
2980 if (unlikely(!debug_locks))
2981 return;
2982
Peter Zijlstra4dfbb9d2006-10-11 01:45:14 -04002983 if (subclass)
2984 register_lock_class(lock, subclass, 1);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002985}
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002986EXPORT_SYMBOL_GPL(lockdep_init_map);
2987
Peter Zijlstra1704f472010-03-19 01:37:42 +01002988struct lock_class_key __lockdep_no_validate__;
2989
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002990/*
2991 * This gets called for every mutex_lock*()/spin_lock*() operation.
2992 * We maintain the dependency maps and validate the locking attempt:
2993 */
2994static int __lock_acquire(struct lockdep_map *lock, unsigned int subclass,
2995 int trylock, int read, int check, int hardirqs_off,
Peter Zijlstrabb97a912009-07-20 19:15:35 +02002996 struct lockdep_map *nest_lock, unsigned long ip,
2997 int references)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002998{
2999 struct task_struct *curr = current;
Ingo Molnard6d897c2006-07-10 04:44:04 -07003000 struct lock_class *class = NULL;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003001 struct held_lock *hlock;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003002 unsigned int depth, id;
3003 int chain_head = 0;
Peter Zijlstrabb97a912009-07-20 19:15:35 +02003004 int class_idx;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003005 u64 chain_key;
3006
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003007 if (!prove_locking)
3008 check = 1;
3009
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003010 if (unlikely(!debug_locks))
3011 return 0;
3012
Peter Zijlstra0119fee2011-09-02 01:30:29 +02003013 /*
3014 * Lockdep should run with IRQs disabled, otherwise we could
3015 * get an interrupt which would want to take locks, which would
3016 * end up in lockdep and have you got a head-ache already?
3017 */
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003018 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
3019 return 0;
3020
Peter Zijlstra1704f472010-03-19 01:37:42 +01003021 if (lock->key == &__lockdep_no_validate__)
3022 check = 1;
3023
Hitoshi Mitake62016252010-10-05 18:01:51 +09003024 if (subclass < NR_LOCKDEP_CACHING_CLASSES)
3025 class = lock->class_cache[subclass];
Ingo Molnard6d897c2006-07-10 04:44:04 -07003026 /*
Hitoshi Mitake62016252010-10-05 18:01:51 +09003027 * Not cached?
Ingo Molnard6d897c2006-07-10 04:44:04 -07003028 */
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003029 if (unlikely(!class)) {
Peter Zijlstra4dfbb9d2006-10-11 01:45:14 -04003030 class = register_lock_class(lock, subclass, 0);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003031 if (!class)
3032 return 0;
3033 }
Frederic Weisbeckerbd6d29c2010-04-06 00:10:17 +02003034 atomic_inc((atomic_t *)&class->ops);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003035 if (very_verbose(class)) {
3036 printk("\nacquire class [%p] %s", class->key, class->name);
3037 if (class->name_version > 1)
3038 printk("#%d", class->name_version);
3039 printk("\n");
3040 dump_stack();
3041 }
3042
3043 /*
3044 * Add the lock to the list of currently held locks.
3045 * (we dont increase the depth just yet, up until the
3046 * dependency checks are done)
3047 */
3048 depth = curr->lockdep_depth;
Peter Zijlstra0119fee2011-09-02 01:30:29 +02003049 /*
3050 * Ran out of static storage for our per-task lock stack again have we?
3051 */
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003052 if (DEBUG_LOCKS_WARN_ON(depth >= MAX_LOCK_DEPTH))
3053 return 0;
3054
Peter Zijlstrabb97a912009-07-20 19:15:35 +02003055 class_idx = class - lock_classes + 1;
3056
3057 if (depth) {
3058 hlock = curr->held_locks + depth - 1;
3059 if (hlock->class_idx == class_idx && nest_lock) {
3060 if (hlock->references)
3061 hlock->references++;
3062 else
3063 hlock->references = 2;
3064
3065 return 1;
3066 }
3067 }
3068
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003069 hlock = curr->held_locks + depth;
Peter Zijlstra0119fee2011-09-02 01:30:29 +02003070 /*
3071 * Plain impossible, we just registered it and checked it weren't no
3072 * NULL like.. I bet this mushroom I ate was good!
3073 */
Dave Jonesf82b2172008-08-11 09:30:23 +02003074 if (DEBUG_LOCKS_WARN_ON(!class))
3075 return 0;
Peter Zijlstrabb97a912009-07-20 19:15:35 +02003076 hlock->class_idx = class_idx;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003077 hlock->acquire_ip = ip;
3078 hlock->instance = lock;
Peter Zijlstra7531e2f2008-08-11 09:30:24 +02003079 hlock->nest_lock = nest_lock;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003080 hlock->trylock = trylock;
3081 hlock->read = read;
3082 hlock->check = check;
Dmitry Baryshkov6951b122008-08-18 04:26:37 +04003083 hlock->hardirqs_off = !!hardirqs_off;
Peter Zijlstrabb97a912009-07-20 19:15:35 +02003084 hlock->references = references;
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003085#ifdef CONFIG_LOCK_STAT
3086 hlock->waittime_stamp = 0;
Peter Zijlstra3365e7792009-10-09 10:12:41 +02003087 hlock->holdtime_stamp = lockstat_clock();
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003088#endif
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003089
Peter Zijlstra8e182572007-07-19 01:48:54 -07003090 if (check == 2 && !mark_irqflags(curr, hlock))
3091 return 0;
3092
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003093 /* mark it as used: */
Jarek Poplawski4ff773bb2007-05-08 00:31:00 -07003094 if (!mark_lock(curr, hlock, LOCK_USED))
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003095 return 0;
Peter Zijlstra8e182572007-07-19 01:48:54 -07003096
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003097 /*
Gautham R Shenoy17aacfb2007-10-28 20:47:01 +01003098 * Calculate the chain hash: it's the combined hash of all the
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003099 * lock keys along the dependency chain. We save the hash value
3100 * at every step so that we can get the current hash easily
3101 * after unlock. The chain hash is then used to cache dependency
3102 * results.
3103 *
3104 * The 'key ID' is what is the most compact key value to drive
3105 * the hash, not class->key.
3106 */
3107 id = class - lock_classes;
Peter Zijlstra0119fee2011-09-02 01:30:29 +02003108 /*
3109 * Whoops, we did it again.. ran straight out of our static allocation.
3110 */
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003111 if (DEBUG_LOCKS_WARN_ON(id >= MAX_LOCKDEP_KEYS))
3112 return 0;
3113
3114 chain_key = curr->curr_chain_key;
3115 if (!depth) {
Peter Zijlstra0119fee2011-09-02 01:30:29 +02003116 /*
3117 * How can we have a chain hash when we ain't got no keys?!
3118 */
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003119 if (DEBUG_LOCKS_WARN_ON(chain_key != 0))
3120 return 0;
3121 chain_head = 1;
3122 }
3123
3124 hlock->prev_chain_key = chain_key;
Peter Zijlstra8e182572007-07-19 01:48:54 -07003125 if (separate_irq_context(curr, hlock)) {
3126 chain_key = 0;
3127 chain_head = 1;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003128 }
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003129 chain_key = iterate_chain_key(chain_key, id);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003130
Gregory Haskins3aa416b2007-10-11 22:11:11 +02003131 if (!validate_chain(curr, lock, hlock, chain_head, chain_key))
Peter Zijlstra8e182572007-07-19 01:48:54 -07003132 return 0;
Jarek Poplawski381a2292007-02-10 01:44:58 -08003133
Gregory Haskins3aa416b2007-10-11 22:11:11 +02003134 curr->curr_chain_key = chain_key;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003135 curr->lockdep_depth++;
3136 check_chain_key(curr);
Jarek Poplawski60e114d2007-02-20 13:58:00 -08003137#ifdef CONFIG_DEBUG_LOCKDEP
3138 if (unlikely(!debug_locks))
3139 return 0;
3140#endif
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003141 if (unlikely(curr->lockdep_depth >= MAX_LOCK_DEPTH)) {
3142 debug_locks_off();
3143 printk("BUG: MAX_LOCK_DEPTH too low!\n");
3144 printk("turning off the locking correctness validator.\n");
Peter Zijlstraeedeeab2009-03-18 12:38:47 +01003145 dump_stack();
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003146 return 0;
3147 }
Jarek Poplawski381a2292007-02-10 01:44:58 -08003148
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003149 if (unlikely(curr->lockdep_depth > max_lockdep_depth))
3150 max_lockdep_depth = curr->lockdep_depth;
3151
3152 return 1;
3153}
3154
3155static int
3156print_unlock_inbalance_bug(struct task_struct *curr, struct lockdep_map *lock,
3157 unsigned long ip)
3158{
3159 if (!debug_locks_off())
3160 return 0;
3161 if (debug_locks_silent)
3162 return 0;
3163
3164 printk("\n=====================================\n");
3165 printk( "[ BUG: bad unlock balance detected! ]\n");
3166 printk( "-------------------------------------\n");
3167 printk("%s/%d is trying to release lock (",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07003168 curr->comm, task_pid_nr(curr));
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003169 print_lockdep_cache(lock);
3170 printk(") at:\n");
3171 print_ip_sym(ip);
3172 printk("but there are no more locks to release!\n");
3173 printk("\nother info that might help us debug this:\n");
3174 lockdep_print_held_locks(curr);
3175
3176 printk("\nstack backtrace:\n");
3177 dump_stack();
3178
3179 return 0;
3180}
3181
3182/*
3183 * Common debugging checks for both nested and non-nested unlock:
3184 */
3185static int check_unlock(struct task_struct *curr, struct lockdep_map *lock,
3186 unsigned long ip)
3187{
3188 if (unlikely(!debug_locks))
3189 return 0;
Peter Zijlstra0119fee2011-09-02 01:30:29 +02003190 /*
3191 * Lockdep should run with IRQs disabled, recursion, head-ache, etc..
3192 */
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003193 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
3194 return 0;
3195
3196 if (curr->lockdep_depth <= 0)
3197 return print_unlock_inbalance_bug(curr, lock, ip);
3198
3199 return 1;
3200}
3201
Peter Zijlstrabb97a912009-07-20 19:15:35 +02003202static int match_held_lock(struct held_lock *hlock, struct lockdep_map *lock)
3203{
3204 if (hlock->instance == lock)
3205 return 1;
3206
3207 if (hlock->references) {
Hitoshi Mitake62016252010-10-05 18:01:51 +09003208 struct lock_class *class = lock->class_cache[0];
Peter Zijlstrabb97a912009-07-20 19:15:35 +02003209
3210 if (!class)
3211 class = look_up_lock_class(lock, 0);
3212
Peter Zijlstra80e04012011-08-05 14:26:17 +02003213 /*
3214 * If look_up_lock_class() failed to find a class, we're trying
3215 * to test if we hold a lock that has never yet been acquired.
3216 * Clearly if the lock hasn't been acquired _ever_, we're not
3217 * holding it either, so report failure.
3218 */
3219 if (!class)
Peter Zijlstrabb97a912009-07-20 19:15:35 +02003220 return 0;
3221
Peter Zijlstra0119fee2011-09-02 01:30:29 +02003222 /*
3223 * References, but not a lock we're actually ref-counting?
3224 * State got messed up, follow the sites that change ->references
3225 * and try to make sense of it.
3226 */
Peter Zijlstrabb97a912009-07-20 19:15:35 +02003227 if (DEBUG_LOCKS_WARN_ON(!hlock->nest_lock))
3228 return 0;
3229
3230 if (hlock->class_idx == class - lock_classes + 1)
3231 return 1;
3232 }
3233
3234 return 0;
3235}
3236
Peter Zijlstra64aa3482008-08-11 09:30:21 +02003237static int
Peter Zijlstra00ef9f72008-12-04 09:00:17 +01003238__lock_set_class(struct lockdep_map *lock, const char *name,
3239 struct lock_class_key *key, unsigned int subclass,
3240 unsigned long ip)
Peter Zijlstra64aa3482008-08-11 09:30:21 +02003241{
3242 struct task_struct *curr = current;
3243 struct held_lock *hlock, *prev_hlock;
3244 struct lock_class *class;
3245 unsigned int depth;
3246 int i;
3247
3248 depth = curr->lockdep_depth;
Peter Zijlstra0119fee2011-09-02 01:30:29 +02003249 /*
3250 * This function is about (re)setting the class of a held lock,
3251 * yet we're not actually holding any locks. Naughty user!
3252 */
Peter Zijlstra64aa3482008-08-11 09:30:21 +02003253 if (DEBUG_LOCKS_WARN_ON(!depth))
3254 return 0;
3255
3256 prev_hlock = NULL;
3257 for (i = depth-1; i >= 0; i--) {
3258 hlock = curr->held_locks + i;
3259 /*
3260 * We must not cross into another context:
3261 */
3262 if (prev_hlock && prev_hlock->irq_context != hlock->irq_context)
3263 break;
Peter Zijlstrabb97a912009-07-20 19:15:35 +02003264 if (match_held_lock(hlock, lock))
Peter Zijlstra64aa3482008-08-11 09:30:21 +02003265 goto found_it;
3266 prev_hlock = hlock;
3267 }
3268 return print_unlock_inbalance_bug(curr, lock, ip);
3269
3270found_it:
Peter Zijlstra00ef9f72008-12-04 09:00:17 +01003271 lockdep_init_map(lock, name, key, 0);
Peter Zijlstra64aa3482008-08-11 09:30:21 +02003272 class = register_lock_class(lock, subclass, 0);
Dave Jonesf82b2172008-08-11 09:30:23 +02003273 hlock->class_idx = class - lock_classes + 1;
Peter Zijlstra64aa3482008-08-11 09:30:21 +02003274
3275 curr->lockdep_depth = i;
3276 curr->curr_chain_key = hlock->prev_chain_key;
3277
3278 for (; i < depth; i++) {
3279 hlock = curr->held_locks + i;
3280 if (!__lock_acquire(hlock->instance,
Dave Jonesf82b2172008-08-11 09:30:23 +02003281 hlock_class(hlock)->subclass, hlock->trylock,
Peter Zijlstra64aa3482008-08-11 09:30:21 +02003282 hlock->read, hlock->check, hlock->hardirqs_off,
Peter Zijlstrabb97a912009-07-20 19:15:35 +02003283 hlock->nest_lock, hlock->acquire_ip,
3284 hlock->references))
Peter Zijlstra64aa3482008-08-11 09:30:21 +02003285 return 0;
3286 }
3287
Peter Zijlstra0119fee2011-09-02 01:30:29 +02003288 /*
3289 * I took it apart and put it back together again, except now I have
3290 * these 'spare' parts.. where shall I put them.
3291 */
Peter Zijlstra64aa3482008-08-11 09:30:21 +02003292 if (DEBUG_LOCKS_WARN_ON(curr->lockdep_depth != depth))
3293 return 0;
3294 return 1;
3295}
3296
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003297/*
3298 * Remove the lock to the list of currently held locks in a
3299 * potentially non-nested (out of order) manner. This is a
3300 * relatively rare operation, as all the unlock APIs default
3301 * to nested mode (which uses lock_release()):
3302 */
3303static int
3304lock_release_non_nested(struct task_struct *curr,
3305 struct lockdep_map *lock, unsigned long ip)
3306{
3307 struct held_lock *hlock, *prev_hlock;
3308 unsigned int depth;
3309 int i;
3310
3311 /*
3312 * Check whether the lock exists in the current stack
3313 * of held locks:
3314 */
3315 depth = curr->lockdep_depth;
Peter Zijlstra0119fee2011-09-02 01:30:29 +02003316 /*
3317 * So we're all set to release this lock.. wait what lock? We don't
3318 * own any locks, you've been drinking again?
3319 */
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003320 if (DEBUG_LOCKS_WARN_ON(!depth))
3321 return 0;
3322
3323 prev_hlock = NULL;
3324 for (i = depth-1; i >= 0; i--) {
3325 hlock = curr->held_locks + i;
3326 /*
3327 * We must not cross into another context:
3328 */
3329 if (prev_hlock && prev_hlock->irq_context != hlock->irq_context)
3330 break;
Peter Zijlstrabb97a912009-07-20 19:15:35 +02003331 if (match_held_lock(hlock, lock))
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003332 goto found_it;
3333 prev_hlock = hlock;
3334 }
3335 return print_unlock_inbalance_bug(curr, lock, ip);
3336
3337found_it:
Peter Zijlstrabb97a912009-07-20 19:15:35 +02003338 if (hlock->instance == lock)
3339 lock_release_holdtime(hlock);
3340
3341 if (hlock->references) {
3342 hlock->references--;
3343 if (hlock->references) {
3344 /*
3345 * We had, and after removing one, still have
3346 * references, the current lock stack is still
3347 * valid. We're done!
3348 */
3349 return 1;
3350 }
3351 }
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003352
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003353 /*
3354 * We have the right lock to unlock, 'hlock' points to it.
3355 * Now we remove it from the stack, and add back the other
3356 * entries (if any), recalculating the hash along the way:
3357 */
Peter Zijlstrabb97a912009-07-20 19:15:35 +02003358
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003359 curr->lockdep_depth = i;
3360 curr->curr_chain_key = hlock->prev_chain_key;
3361
3362 for (i++; i < depth; i++) {
3363 hlock = curr->held_locks + i;
3364 if (!__lock_acquire(hlock->instance,
Dave Jonesf82b2172008-08-11 09:30:23 +02003365 hlock_class(hlock)->subclass, hlock->trylock,
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003366 hlock->read, hlock->check, hlock->hardirqs_off,
Peter Zijlstrabb97a912009-07-20 19:15:35 +02003367 hlock->nest_lock, hlock->acquire_ip,
3368 hlock->references))
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003369 return 0;
3370 }
3371
Peter Zijlstra0119fee2011-09-02 01:30:29 +02003372 /*
3373 * We had N bottles of beer on the wall, we drank one, but now
3374 * there's not N-1 bottles of beer left on the wall...
3375 */
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003376 if (DEBUG_LOCKS_WARN_ON(curr->lockdep_depth != depth - 1))
3377 return 0;
3378 return 1;
3379}
3380
3381/*
3382 * Remove the lock to the list of currently held locks - this gets
3383 * called on mutex_unlock()/spin_unlock*() (or on a failed
3384 * mutex_lock_interruptible()). This is done for unlocks that nest
3385 * perfectly. (i.e. the current top of the lock-stack is unlocked)
3386 */
3387static int lock_release_nested(struct task_struct *curr,
3388 struct lockdep_map *lock, unsigned long ip)
3389{
3390 struct held_lock *hlock;
3391 unsigned int depth;
3392
3393 /*
3394 * Pop off the top of the lock stack:
3395 */
3396 depth = curr->lockdep_depth - 1;
3397 hlock = curr->held_locks + depth;
3398
3399 /*
3400 * Is the unlock non-nested:
3401 */
Peter Zijlstrabb97a912009-07-20 19:15:35 +02003402 if (hlock->instance != lock || hlock->references)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003403 return lock_release_non_nested(curr, lock, ip);
3404 curr->lockdep_depth--;
3405
Peter Zijlstra0119fee2011-09-02 01:30:29 +02003406 /*
3407 * No more locks, but somehow we've got hash left over, who left it?
3408 */
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003409 if (DEBUG_LOCKS_WARN_ON(!depth && (hlock->prev_chain_key != 0)))
3410 return 0;
3411
3412 curr->curr_chain_key = hlock->prev_chain_key;
3413
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003414 lock_release_holdtime(hlock);
3415
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003416#ifdef CONFIG_DEBUG_LOCKDEP
3417 hlock->prev_chain_key = 0;
Dave Jonesf82b2172008-08-11 09:30:23 +02003418 hlock->class_idx = 0;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003419 hlock->acquire_ip = 0;
3420 hlock->irq_context = 0;
3421#endif
3422 return 1;
3423}
3424
3425/*
3426 * Remove the lock to the list of currently held locks - this gets
3427 * called on mutex_unlock()/spin_unlock*() (or on a failed
3428 * mutex_lock_interruptible()). This is done for unlocks that nest
3429 * perfectly. (i.e. the current top of the lock-stack is unlocked)
3430 */
3431static void
3432__lock_release(struct lockdep_map *lock, int nested, unsigned long ip)
3433{
3434 struct task_struct *curr = current;
3435
3436 if (!check_unlock(curr, lock, ip))
3437 return;
3438
3439 if (nested) {
3440 if (!lock_release_nested(curr, lock, ip))
3441 return;
3442 } else {
3443 if (!lock_release_non_nested(curr, lock, ip))
3444 return;
3445 }
3446
3447 check_chain_key(curr);
3448}
3449
Peter Zijlstraf607c662009-07-20 19:16:29 +02003450static int __lock_is_held(struct lockdep_map *lock)
3451{
3452 struct task_struct *curr = current;
3453 int i;
3454
3455 for (i = 0; i < curr->lockdep_depth; i++) {
Peter Zijlstrabb97a912009-07-20 19:15:35 +02003456 struct held_lock *hlock = curr->held_locks + i;
3457
3458 if (match_held_lock(hlock, lock))
Peter Zijlstraf607c662009-07-20 19:16:29 +02003459 return 1;
3460 }
3461
3462 return 0;
3463}
3464
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003465/*
3466 * Check whether we follow the irq-flags state precisely:
3467 */
Steven Rostedt1d09daa2008-05-12 21:20:55 +02003468static void check_flags(unsigned long flags)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003469{
Ingo Molnar992860e2008-07-14 10:28:38 +02003470#if defined(CONFIG_PROVE_LOCKING) && defined(CONFIG_DEBUG_LOCKDEP) && \
3471 defined(CONFIG_TRACE_IRQFLAGS)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003472 if (!debug_locks)
3473 return;
3474
Ingo Molnar5f9fa8a2007-12-07 19:02:47 +01003475 if (irqs_disabled_flags(flags)) {
3476 if (DEBUG_LOCKS_WARN_ON(current->hardirqs_enabled)) {
3477 printk("possible reason: unannotated irqs-off.\n");
3478 }
3479 } else {
3480 if (DEBUG_LOCKS_WARN_ON(!current->hardirqs_enabled)) {
3481 printk("possible reason: unannotated irqs-on.\n");
3482 }
3483 }
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003484
3485 /*
3486 * We dont accurately track softirq state in e.g.
3487 * hardirq contexts (such as on 4KSTACKS), so only
3488 * check if not in hardirq contexts:
3489 */
3490 if (!hardirq_count()) {
Peter Zijlstra0119fee2011-09-02 01:30:29 +02003491 if (softirq_count()) {
3492 /* like the above, but with softirqs */
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003493 DEBUG_LOCKS_WARN_ON(current->softirqs_enabled);
Peter Zijlstra0119fee2011-09-02 01:30:29 +02003494 } else {
3495 /* lick the above, does it taste good? */
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003496 DEBUG_LOCKS_WARN_ON(!current->softirqs_enabled);
Peter Zijlstra0119fee2011-09-02 01:30:29 +02003497 }
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003498 }
3499
3500 if (!debug_locks)
3501 print_irqtrace_events(current);
3502#endif
3503}
3504
Peter Zijlstra00ef9f72008-12-04 09:00:17 +01003505void lock_set_class(struct lockdep_map *lock, const char *name,
3506 struct lock_class_key *key, unsigned int subclass,
3507 unsigned long ip)
Peter Zijlstra64aa3482008-08-11 09:30:21 +02003508{
3509 unsigned long flags;
3510
3511 if (unlikely(current->lockdep_recursion))
3512 return;
3513
3514 raw_local_irq_save(flags);
3515 current->lockdep_recursion = 1;
3516 check_flags(flags);
Peter Zijlstra00ef9f72008-12-04 09:00:17 +01003517 if (__lock_set_class(lock, name, key, subclass, ip))
Peter Zijlstra64aa3482008-08-11 09:30:21 +02003518 check_chain_key(current);
3519 current->lockdep_recursion = 0;
3520 raw_local_irq_restore(flags);
3521}
Peter Zijlstra00ef9f72008-12-04 09:00:17 +01003522EXPORT_SYMBOL_GPL(lock_set_class);
Peter Zijlstra64aa3482008-08-11 09:30:21 +02003523
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003524/*
3525 * We are not always called with irqs disabled - do that here,
3526 * and also avoid lockdep recursion:
3527 */
Steven Rostedt1d09daa2008-05-12 21:20:55 +02003528void lock_acquire(struct lockdep_map *lock, unsigned int subclass,
Peter Zijlstra7531e2f2008-08-11 09:30:24 +02003529 int trylock, int read, int check,
3530 struct lockdep_map *nest_lock, unsigned long ip)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003531{
3532 unsigned long flags;
3533
3534 if (unlikely(current->lockdep_recursion))
3535 return;
3536
3537 raw_local_irq_save(flags);
3538 check_flags(flags);
3539
3540 current->lockdep_recursion = 1;
Frederic Weisbeckerdb2c4c72010-02-02 23:34:40 +01003541 trace_lock_acquire(lock, subclass, trylock, read, check, nest_lock, ip);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003542 __lock_acquire(lock, subclass, trylock, read, check,
Peter Zijlstrabb97a912009-07-20 19:15:35 +02003543 irqs_disabled_flags(flags), nest_lock, ip, 0);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003544 current->lockdep_recursion = 0;
3545 raw_local_irq_restore(flags);
3546}
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003547EXPORT_SYMBOL_GPL(lock_acquire);
3548
Steven Rostedt1d09daa2008-05-12 21:20:55 +02003549void lock_release(struct lockdep_map *lock, int nested,
Steven Rostedt0764d232008-05-12 21:20:44 +02003550 unsigned long ip)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003551{
3552 unsigned long flags;
3553
3554 if (unlikely(current->lockdep_recursion))
3555 return;
3556
3557 raw_local_irq_save(flags);
3558 check_flags(flags);
3559 current->lockdep_recursion = 1;
Frederic Weisbecker93135432010-05-08 06:24:25 +02003560 trace_lock_release(lock, ip);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003561 __lock_release(lock, nested, ip);
3562 current->lockdep_recursion = 0;
3563 raw_local_irq_restore(flags);
3564}
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003565EXPORT_SYMBOL_GPL(lock_release);
3566
Peter Zijlstraf607c662009-07-20 19:16:29 +02003567int lock_is_held(struct lockdep_map *lock)
3568{
3569 unsigned long flags;
3570 int ret = 0;
3571
3572 if (unlikely(current->lockdep_recursion))
Peter Zijlstraf2513cd2011-06-06 12:32:43 +02003573 return 1; /* avoid false negative lockdep_assert_held() */
Peter Zijlstraf607c662009-07-20 19:16:29 +02003574
3575 raw_local_irq_save(flags);
3576 check_flags(flags);
3577
3578 current->lockdep_recursion = 1;
3579 ret = __lock_is_held(lock);
3580 current->lockdep_recursion = 0;
3581 raw_local_irq_restore(flags);
3582
3583 return ret;
3584}
3585EXPORT_SYMBOL_GPL(lock_is_held);
3586
Nick Piggincf40bd12009-01-21 08:12:39 +01003587void lockdep_set_current_reclaim_state(gfp_t gfp_mask)
3588{
3589 current->lockdep_reclaim_gfp = gfp_mask;
3590}
3591
3592void lockdep_clear_current_reclaim_state(void)
3593{
3594 current->lockdep_reclaim_gfp = 0;
3595}
3596
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003597#ifdef CONFIG_LOCK_STAT
3598static int
3599print_lock_contention_bug(struct task_struct *curr, struct lockdep_map *lock,
3600 unsigned long ip)
3601{
3602 if (!debug_locks_off())
3603 return 0;
3604 if (debug_locks_silent)
3605 return 0;
3606
3607 printk("\n=================================\n");
3608 printk( "[ BUG: bad contention detected! ]\n");
3609 printk( "---------------------------------\n");
3610 printk("%s/%d is trying to contend lock (",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07003611 curr->comm, task_pid_nr(curr));
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003612 print_lockdep_cache(lock);
3613 printk(") at:\n");
3614 print_ip_sym(ip);
3615 printk("but there are no locks held!\n");
3616 printk("\nother info that might help us debug this:\n");
3617 lockdep_print_held_locks(curr);
3618
3619 printk("\nstack backtrace:\n");
3620 dump_stack();
3621
3622 return 0;
3623}
3624
3625static void
3626__lock_contended(struct lockdep_map *lock, unsigned long ip)
3627{
3628 struct task_struct *curr = current;
3629 struct held_lock *hlock, *prev_hlock;
3630 struct lock_class_stats *stats;
3631 unsigned int depth;
Peter Zijlstrac7e78cf2008-10-16 23:17:09 +02003632 int i, contention_point, contending_point;
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003633
3634 depth = curr->lockdep_depth;
Peter Zijlstra0119fee2011-09-02 01:30:29 +02003635 /*
3636 * Whee, we contended on this lock, except it seems we're not
3637 * actually trying to acquire anything much at all..
3638 */
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003639 if (DEBUG_LOCKS_WARN_ON(!depth))
3640 return;
3641
3642 prev_hlock = NULL;
3643 for (i = depth-1; i >= 0; i--) {
3644 hlock = curr->held_locks + i;
3645 /*
3646 * We must not cross into another context:
3647 */
3648 if (prev_hlock && prev_hlock->irq_context != hlock->irq_context)
3649 break;
Peter Zijlstrabb97a912009-07-20 19:15:35 +02003650 if (match_held_lock(hlock, lock))
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003651 goto found_it;
3652 prev_hlock = hlock;
3653 }
3654 print_lock_contention_bug(curr, lock, ip);
3655 return;
3656
3657found_it:
Peter Zijlstrabb97a912009-07-20 19:15:35 +02003658 if (hlock->instance != lock)
3659 return;
3660
Peter Zijlstra3365e7792009-10-09 10:12:41 +02003661 hlock->waittime_stamp = lockstat_clock();
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003662
Peter Zijlstrac7e78cf2008-10-16 23:17:09 +02003663 contention_point = lock_point(hlock_class(hlock)->contention_point, ip);
3664 contending_point = lock_point(hlock_class(hlock)->contending_point,
3665 lock->ip);
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003666
Dave Jonesf82b2172008-08-11 09:30:23 +02003667 stats = get_lock_stats(hlock_class(hlock));
Peter Zijlstrac7e78cf2008-10-16 23:17:09 +02003668 if (contention_point < LOCKSTAT_POINTS)
3669 stats->contention_point[contention_point]++;
3670 if (contending_point < LOCKSTAT_POINTS)
3671 stats->contending_point[contending_point]++;
Peter Zijlstra96645672007-07-19 01:49:00 -07003672 if (lock->cpu != smp_processor_id())
3673 stats->bounces[bounce_contended + !!hlock->read]++;
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003674 put_lock_stats(stats);
3675}
3676
3677static void
Peter Zijlstrac7e78cf2008-10-16 23:17:09 +02003678__lock_acquired(struct lockdep_map *lock, unsigned long ip)
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003679{
3680 struct task_struct *curr = current;
3681 struct held_lock *hlock, *prev_hlock;
3682 struct lock_class_stats *stats;
3683 unsigned int depth;
Peter Zijlstra3365e7792009-10-09 10:12:41 +02003684 u64 now, waittime = 0;
Peter Zijlstra96645672007-07-19 01:49:00 -07003685 int i, cpu;
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003686
3687 depth = curr->lockdep_depth;
Peter Zijlstra0119fee2011-09-02 01:30:29 +02003688 /*
3689 * Yay, we acquired ownership of this lock we didn't try to
3690 * acquire, how the heck did that happen?
3691 */
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003692 if (DEBUG_LOCKS_WARN_ON(!depth))
3693 return;
3694
3695 prev_hlock = NULL;
3696 for (i = depth-1; i >= 0; i--) {
3697 hlock = curr->held_locks + i;
3698 /*
3699 * We must not cross into another context:
3700 */
3701 if (prev_hlock && prev_hlock->irq_context != hlock->irq_context)
3702 break;
Peter Zijlstrabb97a912009-07-20 19:15:35 +02003703 if (match_held_lock(hlock, lock))
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003704 goto found_it;
3705 prev_hlock = hlock;
3706 }
3707 print_lock_contention_bug(curr, lock, _RET_IP_);
3708 return;
3709
3710found_it:
Peter Zijlstrabb97a912009-07-20 19:15:35 +02003711 if (hlock->instance != lock)
3712 return;
3713
Peter Zijlstra96645672007-07-19 01:49:00 -07003714 cpu = smp_processor_id();
3715 if (hlock->waittime_stamp) {
Peter Zijlstra3365e7792009-10-09 10:12:41 +02003716 now = lockstat_clock();
Peter Zijlstra96645672007-07-19 01:49:00 -07003717 waittime = now - hlock->waittime_stamp;
3718 hlock->holdtime_stamp = now;
3719 }
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003720
Frederic Weisbecker883a2a32010-05-08 06:16:11 +02003721 trace_lock_acquired(lock, ip);
Frederic Weisbecker20625012009-04-06 01:49:33 +02003722
Dave Jonesf82b2172008-08-11 09:30:23 +02003723 stats = get_lock_stats(hlock_class(hlock));
Peter Zijlstra96645672007-07-19 01:49:00 -07003724 if (waittime) {
3725 if (hlock->read)
3726 lock_time_inc(&stats->read_waittime, waittime);
3727 else
3728 lock_time_inc(&stats->write_waittime, waittime);
3729 }
3730 if (lock->cpu != cpu)
3731 stats->bounces[bounce_acquired + !!hlock->read]++;
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003732 put_lock_stats(stats);
Peter Zijlstra96645672007-07-19 01:49:00 -07003733
3734 lock->cpu = cpu;
Peter Zijlstrac7e78cf2008-10-16 23:17:09 +02003735 lock->ip = ip;
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003736}
3737
3738void lock_contended(struct lockdep_map *lock, unsigned long ip)
3739{
3740 unsigned long flags;
3741
3742 if (unlikely(!lock_stat))
3743 return;
3744
3745 if (unlikely(current->lockdep_recursion))
3746 return;
3747
3748 raw_local_irq_save(flags);
3749 check_flags(flags);
3750 current->lockdep_recursion = 1;
Frederic Weisbeckerdb2c4c72010-02-02 23:34:40 +01003751 trace_lock_contended(lock, ip);
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003752 __lock_contended(lock, ip);
3753 current->lockdep_recursion = 0;
3754 raw_local_irq_restore(flags);
3755}
3756EXPORT_SYMBOL_GPL(lock_contended);
3757
Peter Zijlstrac7e78cf2008-10-16 23:17:09 +02003758void lock_acquired(struct lockdep_map *lock, unsigned long ip)
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003759{
3760 unsigned long flags;
3761
3762 if (unlikely(!lock_stat))
3763 return;
3764
3765 if (unlikely(current->lockdep_recursion))
3766 return;
3767
3768 raw_local_irq_save(flags);
3769 check_flags(flags);
3770 current->lockdep_recursion = 1;
Peter Zijlstrac7e78cf2008-10-16 23:17:09 +02003771 __lock_acquired(lock, ip);
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003772 current->lockdep_recursion = 0;
3773 raw_local_irq_restore(flags);
3774}
3775EXPORT_SYMBOL_GPL(lock_acquired);
3776#endif
3777
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003778/*
3779 * Used by the testsuite, sanitize the validator state
3780 * after a simulated failure:
3781 */
3782
3783void lockdep_reset(void)
3784{
3785 unsigned long flags;
Ingo Molnar23d95a02006-12-13 00:34:40 -08003786 int i;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003787
3788 raw_local_irq_save(flags);
3789 current->curr_chain_key = 0;
3790 current->lockdep_depth = 0;
3791 current->lockdep_recursion = 0;
3792 memset(current->held_locks, 0, MAX_LOCK_DEPTH*sizeof(struct held_lock));
3793 nr_hardirq_chains = 0;
3794 nr_softirq_chains = 0;
3795 nr_process_chains = 0;
3796 debug_locks = 1;
Ingo Molnar23d95a02006-12-13 00:34:40 -08003797 for (i = 0; i < CHAINHASH_SIZE; i++)
3798 INIT_LIST_HEAD(chainhash_table + i);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003799 raw_local_irq_restore(flags);
3800}
3801
3802static void zap_class(struct lock_class *class)
3803{
3804 int i;
3805
3806 /*
3807 * Remove all dependencies this lock is
3808 * involved in:
3809 */
3810 for (i = 0; i < nr_list_entries; i++) {
3811 if (list_entries[i].class == class)
3812 list_del_rcu(&list_entries[i].entry);
3813 }
3814 /*
3815 * Unhash the class and remove it from the all_lock_classes list:
3816 */
3817 list_del_rcu(&class->hash_entry);
3818 list_del_rcu(&class->lock_entry);
3819
Rabin Vincent8bfe0292008-08-11 09:30:26 +02003820 class->key = NULL;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003821}
3822
Arjan van de Venfabe8742008-01-24 07:00:45 +01003823static inline int within(const void *addr, void *start, unsigned long size)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003824{
3825 return addr >= start && addr < start + size;
3826}
3827
3828void lockdep_free_key_range(void *start, unsigned long size)
3829{
3830 struct lock_class *class, *next;
3831 struct list_head *head;
3832 unsigned long flags;
3833 int i;
Nick Piggin5a26db52008-01-16 09:51:58 +01003834 int locked;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003835
3836 raw_local_irq_save(flags);
Nick Piggin5a26db52008-01-16 09:51:58 +01003837 locked = graph_lock();
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003838
3839 /*
3840 * Unhash all classes that were created by this module:
3841 */
3842 for (i = 0; i < CLASSHASH_SIZE; i++) {
3843 head = classhash_table + i;
3844 if (list_empty(head))
3845 continue;
Arjan van de Venfabe8742008-01-24 07:00:45 +01003846 list_for_each_entry_safe(class, next, head, hash_entry) {
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003847 if (within(class->key, start, size))
3848 zap_class(class);
Arjan van de Venfabe8742008-01-24 07:00:45 +01003849 else if (within(class->name, start, size))
3850 zap_class(class);
3851 }
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003852 }
3853
Nick Piggin5a26db52008-01-16 09:51:58 +01003854 if (locked)
3855 graph_unlock();
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003856 raw_local_irq_restore(flags);
3857}
3858
3859void lockdep_reset_lock(struct lockdep_map *lock)
3860{
Ingo Molnard6d897c2006-07-10 04:44:04 -07003861 struct lock_class *class, *next;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003862 struct list_head *head;
3863 unsigned long flags;
3864 int i, j;
Nick Piggin5a26db52008-01-16 09:51:58 +01003865 int locked;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003866
3867 raw_local_irq_save(flags);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003868
3869 /*
Ingo Molnard6d897c2006-07-10 04:44:04 -07003870 * Remove all classes this lock might have:
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003871 */
Ingo Molnard6d897c2006-07-10 04:44:04 -07003872 for (j = 0; j < MAX_LOCKDEP_SUBCLASSES; j++) {
3873 /*
3874 * If the class exists we look it up and zap it:
3875 */
3876 class = look_up_lock_class(lock, j);
3877 if (class)
3878 zap_class(class);
3879 }
3880 /*
3881 * Debug check: in the end all mapped classes should
3882 * be gone.
3883 */
Nick Piggin5a26db52008-01-16 09:51:58 +01003884 locked = graph_lock();
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003885 for (i = 0; i < CLASSHASH_SIZE; i++) {
3886 head = classhash_table + i;
3887 if (list_empty(head))
3888 continue;
3889 list_for_each_entry_safe(class, next, head, hash_entry) {
Hitoshi Mitake62016252010-10-05 18:01:51 +09003890 int match = 0;
3891
3892 for (j = 0; j < NR_LOCKDEP_CACHING_CLASSES; j++)
3893 match |= class == lock->class_cache[j];
3894
3895 if (unlikely(match)) {
Peter Zijlstra0119fee2011-09-02 01:30:29 +02003896 if (debug_locks_off_graph_unlock()) {
3897 /*
3898 * We all just reset everything, how did it match?
3899 */
Ingo Molnar74c383f2006-12-13 00:34:43 -08003900 WARN_ON(1);
Peter Zijlstra0119fee2011-09-02 01:30:29 +02003901 }
Ingo Molnard6d897c2006-07-10 04:44:04 -07003902 goto out_restore;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003903 }
3904 }
3905 }
Nick Piggin5a26db52008-01-16 09:51:58 +01003906 if (locked)
3907 graph_unlock();
Ingo Molnard6d897c2006-07-10 04:44:04 -07003908
3909out_restore:
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003910 raw_local_irq_restore(flags);
3911}
3912
Sam Ravnborg14999932007-02-28 20:12:31 -08003913void lockdep_init(void)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003914{
3915 int i;
3916
3917 /*
3918 * Some architectures have their own start_kernel()
3919 * code which calls lockdep_init(), while we also
3920 * call lockdep_init() from the start_kernel() itself,
3921 * and we want to initialize the hashes only once:
3922 */
3923 if (lockdep_initialized)
3924 return;
3925
3926 for (i = 0; i < CLASSHASH_SIZE; i++)
3927 INIT_LIST_HEAD(classhash_table + i);
3928
3929 for (i = 0; i < CHAINHASH_SIZE; i++)
3930 INIT_LIST_HEAD(chainhash_table + i);
3931
3932 lockdep_initialized = 1;
3933}
3934
3935void __init lockdep_info(void)
3936{
3937 printk("Lock dependency validator: Copyright (c) 2006 Red Hat, Inc., Ingo Molnar\n");
3938
Li Zefanb0788ca2008-11-21 15:57:32 +08003939 printk("... MAX_LOCKDEP_SUBCLASSES: %lu\n", MAX_LOCKDEP_SUBCLASSES);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003940 printk("... MAX_LOCK_DEPTH: %lu\n", MAX_LOCK_DEPTH);
3941 printk("... MAX_LOCKDEP_KEYS: %lu\n", MAX_LOCKDEP_KEYS);
Li Zefanb0788ca2008-11-21 15:57:32 +08003942 printk("... CLASSHASH_SIZE: %lu\n", CLASSHASH_SIZE);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003943 printk("... MAX_LOCKDEP_ENTRIES: %lu\n", MAX_LOCKDEP_ENTRIES);
3944 printk("... MAX_LOCKDEP_CHAINS: %lu\n", MAX_LOCKDEP_CHAINS);
3945 printk("... CHAINHASH_SIZE: %lu\n", CHAINHASH_SIZE);
3946
3947 printk(" memory used by lock dependency info: %lu kB\n",
3948 (sizeof(struct lock_class) * MAX_LOCKDEP_KEYS +
3949 sizeof(struct list_head) * CLASSHASH_SIZE +
3950 sizeof(struct lock_list) * MAX_LOCKDEP_ENTRIES +
3951 sizeof(struct lock_chain) * MAX_LOCKDEP_CHAINS +
Ming Lei90629202009-08-02 21:43:36 +08003952 sizeof(struct list_head) * CHAINHASH_SIZE
Ming Lei4dd861d2009-07-16 15:44:29 +02003953#ifdef CONFIG_PROVE_LOCKING
Ming Leie351b662009-07-22 22:48:09 +08003954 + sizeof(struct circular_queue)
Ming Lei4dd861d2009-07-16 15:44:29 +02003955#endif
Ming Lei90629202009-08-02 21:43:36 +08003956 ) / 1024
Ming Lei4dd861d2009-07-16 15:44:29 +02003957 );
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003958
3959 printk(" per task-struct memory footprint: %lu bytes\n",
3960 sizeof(struct held_lock) * MAX_LOCK_DEPTH);
3961
3962#ifdef CONFIG_DEBUG_LOCKDEP
Johannes Bergc71063c2007-07-19 01:49:02 -07003963 if (lockdep_init_error) {
3964 printk("WARNING: lockdep init error! Arch code didn't call lockdep_init() early enough?\n");
3965 printk("Call stack leading to lockdep invocation was:\n");
3966 print_stack_trace(&lockdep_init_trace, 0);
3967 }
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003968#endif
3969}
3970
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003971static void
3972print_freed_lock_bug(struct task_struct *curr, const void *mem_from,
Arjan van de Ven55794a42006-07-10 04:44:03 -07003973 const void *mem_to, struct held_lock *hlock)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003974{
3975 if (!debug_locks_off())
3976 return;
3977 if (debug_locks_silent)
3978 return;
3979
3980 printk("\n=========================\n");
3981 printk( "[ BUG: held lock freed! ]\n");
3982 printk( "-------------------------\n");
3983 printk("%s/%d is freeing memory %p-%p, with a lock still held there!\n",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07003984 curr->comm, task_pid_nr(curr), mem_from, mem_to-1);
Arjan van de Ven55794a42006-07-10 04:44:03 -07003985 print_lock(hlock);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003986 lockdep_print_held_locks(curr);
3987
3988 printk("\nstack backtrace:\n");
3989 dump_stack();
3990}
3991
Oleg Nesterov54561782007-12-05 15:46:09 +01003992static inline int not_in_range(const void* mem_from, unsigned long mem_len,
3993 const void* lock_from, unsigned long lock_len)
3994{
3995 return lock_from + lock_len <= mem_from ||
3996 mem_from + mem_len <= lock_from;
3997}
3998
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003999/*
4000 * Called when kernel memory is freed (or unmapped), or if a lock
4001 * is destroyed or reinitialized - this code checks whether there is
4002 * any held lock in the memory range of <from> to <to>:
4003 */
4004void debug_check_no_locks_freed(const void *mem_from, unsigned long mem_len)
4005{
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004006 struct task_struct *curr = current;
4007 struct held_lock *hlock;
4008 unsigned long flags;
4009 int i;
4010
4011 if (unlikely(!debug_locks))
4012 return;
4013
4014 local_irq_save(flags);
4015 for (i = 0; i < curr->lockdep_depth; i++) {
4016 hlock = curr->held_locks + i;
4017
Oleg Nesterov54561782007-12-05 15:46:09 +01004018 if (not_in_range(mem_from, mem_len, hlock->instance,
4019 sizeof(*hlock->instance)))
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004020 continue;
4021
Oleg Nesterov54561782007-12-05 15:46:09 +01004022 print_freed_lock_bug(curr, mem_from, mem_from + mem_len, hlock);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004023 break;
4024 }
4025 local_irq_restore(flags);
4026}
Peter Zijlstraed075362006-12-06 20:35:24 -08004027EXPORT_SYMBOL_GPL(debug_check_no_locks_freed);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004028
4029static void print_held_locks_bug(struct task_struct *curr)
4030{
4031 if (!debug_locks_off())
4032 return;
4033 if (debug_locks_silent)
4034 return;
4035
4036 printk("\n=====================================\n");
4037 printk( "[ BUG: lock held at task exit time! ]\n");
4038 printk( "-------------------------------------\n");
4039 printk("%s/%d is exiting with locks still held!\n",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07004040 curr->comm, task_pid_nr(curr));
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004041 lockdep_print_held_locks(curr);
4042
4043 printk("\nstack backtrace:\n");
4044 dump_stack();
4045}
4046
4047void debug_check_no_locks_held(struct task_struct *task)
4048{
4049 if (unlikely(task->lockdep_depth > 0))
4050 print_held_locks_bug(task);
4051}
4052
4053void debug_show_all_locks(void)
4054{
4055 struct task_struct *g, *p;
4056 int count = 10;
4057 int unlock = 1;
4058
Jarek Poplawski9c35dd72007-03-22 00:11:28 -08004059 if (unlikely(!debug_locks)) {
4060 printk("INFO: lockdep is turned off.\n");
4061 return;
4062 }
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004063 printk("\nShowing all locks held in the system:\n");
4064
4065 /*
4066 * Here we try to get the tasklist_lock as hard as possible,
4067 * if not successful after 2 seconds we ignore it (but keep
4068 * trying). This is to enable a debug printout even if a
4069 * tasklist_lock-holding task deadlocks or crashes.
4070 */
4071retry:
4072 if (!read_trylock(&tasklist_lock)) {
4073 if (count == 10)
4074 printk("hm, tasklist_lock locked, retrying... ");
4075 if (count) {
4076 count--;
4077 printk(" #%d", 10-count);
4078 mdelay(200);
4079 goto retry;
4080 }
4081 printk(" ignoring it.\n");
4082 unlock = 0;
qinghuang feng46fec7a2008-10-28 17:24:28 +08004083 } else {
4084 if (count != 10)
4085 printk(KERN_CONT " locked it.\n");
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004086 }
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004087
4088 do_each_thread(g, p) {
Ingo Molnar85684872007-12-05 15:46:09 +01004089 /*
4090 * It's not reliable to print a task's held locks
4091 * if it's not sleeping (or if it's not the current
4092 * task):
4093 */
4094 if (p->state == TASK_RUNNING && p != current)
4095 continue;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004096 if (p->lockdep_depth)
4097 lockdep_print_held_locks(p);
4098 if (!unlock)
4099 if (read_trylock(&tasklist_lock))
4100 unlock = 1;
4101 } while_each_thread(g, p);
4102
4103 printk("\n");
4104 printk("=============================================\n\n");
4105
4106 if (unlock)
4107 read_unlock(&tasklist_lock);
4108}
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004109EXPORT_SYMBOL_GPL(debug_show_all_locks);
4110
Ingo Molnar82a1fcb2008-01-25 21:08:02 +01004111/*
4112 * Careful: only use this function if you are sure that
4113 * the task cannot run in parallel!
4114 */
John Kacurf1b499f2010-08-05 17:10:53 +02004115void debug_show_held_locks(struct task_struct *task)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004116{
Jarek Poplawski9c35dd72007-03-22 00:11:28 -08004117 if (unlikely(!debug_locks)) {
4118 printk("INFO: lockdep is turned off.\n");
4119 return;
4120 }
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004121 lockdep_print_held_locks(task);
4122}
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004123EXPORT_SYMBOL_GPL(debug_show_held_locks);
Peter Zijlstrab351d162007-10-11 22:11:12 +02004124
4125void lockdep_sys_exit(void)
4126{
4127 struct task_struct *curr = current;
4128
4129 if (unlikely(curr->lockdep_depth)) {
4130 if (!debug_locks_off())
4131 return;
4132 printk("\n================================================\n");
4133 printk( "[ BUG: lock held when returning to user space! ]\n");
4134 printk( "------------------------------------------------\n");
4135 printk("%s/%d is leaving the kernel with locks still held!\n",
4136 curr->comm, curr->pid);
4137 lockdep_print_held_locks(curr);
4138 }
4139}
Paul E. McKenney0632eb32010-02-22 17:04:47 -08004140
4141void lockdep_rcu_dereference(const char *file, const int line)
4142{
4143 struct task_struct *curr = current;
4144
Lai Jiangshan2b3fc352010-04-20 16:23:07 +08004145#ifndef CONFIG_PROVE_RCU_REPEATEDLY
Paul E. McKenney0632eb32010-02-22 17:04:47 -08004146 if (!debug_locks_off())
4147 return;
Lai Jiangshan2b3fc352010-04-20 16:23:07 +08004148#endif /* #ifdef CONFIG_PROVE_RCU_REPEATEDLY */
4149 /* Note: the following can be executed concurrently, so be careful. */
Paul E. McKenney056ba4a2010-02-25 14:06:46 -08004150 printk("\n===================================================\n");
4151 printk( "[ INFO: suspicious rcu_dereference_check() usage. ]\n");
4152 printk( "---------------------------------------------------\n");
Paul E. McKenney0632eb32010-02-22 17:04:47 -08004153 printk("%s:%d invoked rcu_dereference_check() without protection!\n",
4154 file, line);
4155 printk("\nother info that might help us debug this:\n\n");
Paul E. McKenneycc5b83a2010-03-03 07:46:59 -08004156 printk("\nrcu_scheduler_active = %d, debug_locks = %d\n", rcu_scheduler_active, debug_locks);
Paul E. McKenney0632eb32010-02-22 17:04:47 -08004157 lockdep_print_held_locks(curr);
4158 printk("\nstack backtrace:\n");
4159 dump_stack();
4160}
4161EXPORT_SYMBOL_GPL(lockdep_rcu_dereference);