blob: bc4d32871f9a3ebdcea24b5c73d18a2ce50a28fd [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{
Thomas Gleixner0199c4e2009-12-02 20:01:25 +010099 if (debug_locks && !arch_spin_is_locked(&lockdep_lock))
Jarek Poplawski381a2292007-02-10 01:44:58 -0800100 return DEBUG_LOCKS_WARN_ON(1);
101
Steven Rostedtbb065af2008-05-12 21:21:00 +0200102 current->lockdep_recursion--;
Thomas Gleixner0199c4e2009-12-02 20:01:25 +0100103 arch_spin_unlock(&lockdep_lock);
Ingo Molnar74c383f2006-12-13 00:34:43 -0800104 return 0;
105}
106
107/*
108 * Turn lock debugging off and return with 0 if it was off already,
109 * and also release the graph lock:
110 */
111static inline int debug_locks_off_graph_unlock(void)
112{
113 int ret = debug_locks_off();
114
Thomas Gleixner0199c4e2009-12-02 20:01:25 +0100115 arch_spin_unlock(&lockdep_lock);
Ingo Molnar74c383f2006-12-13 00:34:43 -0800116
117 return ret;
118}
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700119
120static int lockdep_initialized;
121
122unsigned long nr_list_entries;
Peter Zijlstraaf012962009-07-16 15:44:29 +0200123static struct lock_list list_entries[MAX_LOCKDEP_ENTRIES];
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700124
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700125/*
126 * All data structures here are protected by the global debug_lock.
127 *
128 * Mutex key structs only get allocated, once during bootup, and never
129 * get freed - this significantly simplifies the debugging code.
130 */
131unsigned long nr_lock_classes;
132static struct lock_class lock_classes[MAX_LOCKDEP_KEYS];
133
Dave Jonesf82b2172008-08-11 09:30:23 +0200134static inline struct lock_class *hlock_class(struct held_lock *hlock)
135{
136 if (!hlock->class_idx) {
137 DEBUG_LOCKS_WARN_ON(1);
138 return NULL;
139 }
140 return lock_classes + hlock->class_idx - 1;
141}
142
Peter Zijlstraf20786f2007-07-19 01:48:56 -0700143#ifdef CONFIG_LOCK_STAT
Tejun Heo1871e522009-10-29 22:34:13 +0900144static DEFINE_PER_CPU(struct lock_class_stats[MAX_LOCKDEP_KEYS],
145 cpu_lock_stats);
Peter Zijlstraf20786f2007-07-19 01:48:56 -0700146
Peter Zijlstra3365e7792009-10-09 10:12:41 +0200147static inline u64 lockstat_clock(void)
148{
Peter Zijlstrac6763292010-05-25 10:48:51 +0200149 return local_clock();
Peter Zijlstra3365e7792009-10-09 10:12:41 +0200150}
151
Peter Zijlstrac7e78cf2008-10-16 23:17:09 +0200152static int lock_point(unsigned long points[], unsigned long ip)
Peter Zijlstraf20786f2007-07-19 01:48:56 -0700153{
154 int i;
155
Peter Zijlstrac7e78cf2008-10-16 23:17:09 +0200156 for (i = 0; i < LOCKSTAT_POINTS; i++) {
157 if (points[i] == 0) {
158 points[i] = ip;
Peter Zijlstraf20786f2007-07-19 01:48:56 -0700159 break;
160 }
Peter Zijlstrac7e78cf2008-10-16 23:17:09 +0200161 if (points[i] == ip)
Peter Zijlstraf20786f2007-07-19 01:48:56 -0700162 break;
163 }
164
165 return i;
166}
167
Peter Zijlstra3365e7792009-10-09 10:12:41 +0200168static void lock_time_inc(struct lock_time *lt, u64 time)
Peter Zijlstraf20786f2007-07-19 01:48:56 -0700169{
170 if (time > lt->max)
171 lt->max = time;
172
Frank Rowand109d71c2009-11-19 13:42:06 -0800173 if (time < lt->min || !lt->nr)
Peter Zijlstraf20786f2007-07-19 01:48:56 -0700174 lt->min = time;
175
176 lt->total += time;
177 lt->nr++;
178}
179
Peter Zijlstrac46261d2007-07-19 01:48:57 -0700180static inline void lock_time_add(struct lock_time *src, struct lock_time *dst)
181{
Frank Rowand109d71c2009-11-19 13:42:06 -0800182 if (!src->nr)
183 return;
184
185 if (src->max > dst->max)
186 dst->max = src->max;
187
188 if (src->min < dst->min || !dst->nr)
189 dst->min = src->min;
190
Peter Zijlstrac46261d2007-07-19 01:48:57 -0700191 dst->total += src->total;
192 dst->nr += src->nr;
193}
194
195struct lock_class_stats lock_stats(struct lock_class *class)
196{
197 struct lock_class_stats stats;
198 int cpu, i;
199
200 memset(&stats, 0, sizeof(struct lock_class_stats));
201 for_each_possible_cpu(cpu) {
202 struct lock_class_stats *pcs =
Tejun Heo1871e522009-10-29 22:34:13 +0900203 &per_cpu(cpu_lock_stats, cpu)[class - lock_classes];
Peter Zijlstrac46261d2007-07-19 01:48:57 -0700204
205 for (i = 0; i < ARRAY_SIZE(stats.contention_point); i++)
206 stats.contention_point[i] += pcs->contention_point[i];
207
Peter Zijlstrac7e78cf2008-10-16 23:17:09 +0200208 for (i = 0; i < ARRAY_SIZE(stats.contending_point); i++)
209 stats.contending_point[i] += pcs->contending_point[i];
210
Peter Zijlstrac46261d2007-07-19 01:48:57 -0700211 lock_time_add(&pcs->read_waittime, &stats.read_waittime);
212 lock_time_add(&pcs->write_waittime, &stats.write_waittime);
213
214 lock_time_add(&pcs->read_holdtime, &stats.read_holdtime);
215 lock_time_add(&pcs->write_holdtime, &stats.write_holdtime);
Peter Zijlstra96645672007-07-19 01:49:00 -0700216
217 for (i = 0; i < ARRAY_SIZE(stats.bounces); i++)
218 stats.bounces[i] += pcs->bounces[i];
Peter Zijlstrac46261d2007-07-19 01:48:57 -0700219 }
220
221 return stats;
222}
223
224void clear_lock_stats(struct lock_class *class)
225{
226 int cpu;
227
228 for_each_possible_cpu(cpu) {
229 struct lock_class_stats *cpu_stats =
Tejun Heo1871e522009-10-29 22:34:13 +0900230 &per_cpu(cpu_lock_stats, cpu)[class - lock_classes];
Peter Zijlstrac46261d2007-07-19 01:48:57 -0700231
232 memset(cpu_stats, 0, sizeof(struct lock_class_stats));
233 }
234 memset(class->contention_point, 0, sizeof(class->contention_point));
Peter Zijlstrac7e78cf2008-10-16 23:17:09 +0200235 memset(class->contending_point, 0, sizeof(class->contending_point));
Peter Zijlstrac46261d2007-07-19 01:48:57 -0700236}
237
Peter Zijlstraf20786f2007-07-19 01:48:56 -0700238static struct lock_class_stats *get_lock_stats(struct lock_class *class)
239{
Tejun Heo1871e522009-10-29 22:34:13 +0900240 return &get_cpu_var(cpu_lock_stats)[class - lock_classes];
Peter Zijlstraf20786f2007-07-19 01:48:56 -0700241}
242
243static void put_lock_stats(struct lock_class_stats *stats)
244{
Tejun Heo1871e522009-10-29 22:34:13 +0900245 put_cpu_var(cpu_lock_stats);
Peter Zijlstraf20786f2007-07-19 01:48:56 -0700246}
247
248static void lock_release_holdtime(struct held_lock *hlock)
249{
250 struct lock_class_stats *stats;
Peter Zijlstra3365e7792009-10-09 10:12:41 +0200251 u64 holdtime;
Peter Zijlstraf20786f2007-07-19 01:48:56 -0700252
253 if (!lock_stat)
254 return;
255
Peter Zijlstra3365e7792009-10-09 10:12:41 +0200256 holdtime = lockstat_clock() - hlock->holdtime_stamp;
Peter Zijlstraf20786f2007-07-19 01:48:56 -0700257
Dave Jonesf82b2172008-08-11 09:30:23 +0200258 stats = get_lock_stats(hlock_class(hlock));
Peter Zijlstraf20786f2007-07-19 01:48:56 -0700259 if (hlock->read)
260 lock_time_inc(&stats->read_holdtime, holdtime);
261 else
262 lock_time_inc(&stats->write_holdtime, holdtime);
263 put_lock_stats(stats);
264}
265#else
266static inline void lock_release_holdtime(struct held_lock *hlock)
267{
268}
269#endif
270
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700271/*
272 * We keep a global list of all lock classes. The list only grows,
273 * never shrinks. The list is only accessed with the lockdep
274 * spinlock lock held.
275 */
276LIST_HEAD(all_lock_classes);
277
278/*
279 * The lockdep classes are in a hash-table as well, for fast lookup:
280 */
281#define CLASSHASH_BITS (MAX_LOCKDEP_KEYS_BITS - 1)
282#define CLASSHASH_SIZE (1UL << CLASSHASH_BITS)
Peter Zijlstra4b32d0a2007-07-19 01:48:59 -0700283#define __classhashfn(key) hash_long((unsigned long)key, CLASSHASH_BITS)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700284#define classhashentry(key) (classhash_table + __classhashfn((key)))
285
286static struct list_head classhash_table[CLASSHASH_SIZE];
287
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700288/*
289 * We put the lock dependency chains into a hash-table as well, to cache
290 * their existence:
291 */
292#define CHAINHASH_BITS (MAX_LOCKDEP_CHAINS_BITS-1)
293#define CHAINHASH_SIZE (1UL << CHAINHASH_BITS)
Peter Zijlstra4b32d0a2007-07-19 01:48:59 -0700294#define __chainhashfn(chain) hash_long(chain, CHAINHASH_BITS)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700295#define chainhashentry(chain) (chainhash_table + __chainhashfn((chain)))
296
297static struct list_head chainhash_table[CHAINHASH_SIZE];
298
299/*
300 * The hash key of the lock dependency chains is a hash itself too:
301 * it's a hash of all locks taken up to that lock, including that lock.
302 * It's a 64-bit hash, because it's important for the keys to be
303 * unique.
304 */
305#define iterate_chain_key(key1, key2) \
Ingo Molnar03cbc352006-09-29 02:01:46 -0700306 (((key1) << MAX_LOCKDEP_KEYS_BITS) ^ \
307 ((key1) >> (64-MAX_LOCKDEP_KEYS_BITS)) ^ \
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700308 (key2))
309
Steven Rostedt1d09daa2008-05-12 21:20:55 +0200310void lockdep_off(void)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700311{
312 current->lockdep_recursion++;
313}
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700314EXPORT_SYMBOL(lockdep_off);
315
Steven Rostedt1d09daa2008-05-12 21:20:55 +0200316void lockdep_on(void)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700317{
318 current->lockdep_recursion--;
319}
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700320EXPORT_SYMBOL(lockdep_on);
321
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700322/*
323 * Debugging switches:
324 */
325
326#define VERBOSE 0
Ingo Molnar33e94e92006-12-13 00:34:41 -0800327#define VERY_VERBOSE 0
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700328
329#if VERBOSE
330# define HARDIRQ_VERBOSE 1
331# define SOFTIRQ_VERBOSE 1
Nick Piggincf40bd12009-01-21 08:12:39 +0100332# define RECLAIM_VERBOSE 1
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700333#else
334# define HARDIRQ_VERBOSE 0
335# define SOFTIRQ_VERBOSE 0
Nick Piggincf40bd12009-01-21 08:12:39 +0100336# define RECLAIM_VERBOSE 0
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700337#endif
338
Nick Piggincf40bd12009-01-21 08:12:39 +0100339#if VERBOSE || HARDIRQ_VERBOSE || SOFTIRQ_VERBOSE || RECLAIM_VERBOSE
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700340/*
341 * Quick filtering for interesting events:
342 */
343static int class_filter(struct lock_class *class)
344{
Andi Kleenf9829cc2006-07-10 04:44:01 -0700345#if 0
346 /* Example */
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700347 if (class->name_version == 1 &&
Andi Kleenf9829cc2006-07-10 04:44:01 -0700348 !strcmp(class->name, "lockname"))
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700349 return 1;
350 if (class->name_version == 1 &&
Andi Kleenf9829cc2006-07-10 04:44:01 -0700351 !strcmp(class->name, "&struct->lockfield"))
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700352 return 1;
Andi Kleenf9829cc2006-07-10 04:44:01 -0700353#endif
Ingo Molnara6640892006-12-13 00:34:39 -0800354 /* Filter everything else. 1 would be to allow everything else */
355 return 0;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700356}
357#endif
358
359static int verbose(struct lock_class *class)
360{
361#if VERBOSE
362 return class_filter(class);
363#endif
364 return 0;
365}
366
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700367/*
368 * Stack-trace: tightly packed array of stack backtrace
Ingo Molnar74c383f2006-12-13 00:34:43 -0800369 * addresses. Protected by the graph_lock.
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700370 */
371unsigned long nr_stack_trace_entries;
372static unsigned long stack_trace[MAX_STACK_TRACE_ENTRIES];
373
374static int save_trace(struct stack_trace *trace)
375{
376 trace->nr_entries = 0;
377 trace->max_entries = MAX_STACK_TRACE_ENTRIES - nr_stack_trace_entries;
378 trace->entries = stack_trace + nr_stack_trace_entries;
379
Andi Kleen5a1b3992006-09-26 10:52:34 +0200380 trace->skip = 3;
Andi Kleen5a1b3992006-09-26 10:52:34 +0200381
Christoph Hellwigab1b6f02007-05-08 00:23:29 -0700382 save_stack_trace(trace);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700383
Peter Zijlstra4f84f432009-07-20 15:27:04 +0200384 /*
385 * Some daft arches put -1 at the end to indicate its a full trace.
386 *
387 * <rant> this is buggy anyway, since it takes a whole extra entry so a
388 * complete trace that maxes out the entries provided will be reported
389 * as incomplete, friggin useless </rant>
390 */
Luck, Tonyea5b41f2009-12-09 14:29:36 -0800391 if (trace->nr_entries != 0 &&
392 trace->entries[trace->nr_entries-1] == ULONG_MAX)
Peter Zijlstra4f84f432009-07-20 15:27:04 +0200393 trace->nr_entries--;
394
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700395 trace->max_entries = trace->nr_entries;
396
397 nr_stack_trace_entries += trace->nr_entries;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700398
Peter Zijlstra4f84f432009-07-20 15:27:04 +0200399 if (nr_stack_trace_entries >= MAX_STACK_TRACE_ENTRIES-1) {
Ingo Molnar74c383f2006-12-13 00:34:43 -0800400 if (!debug_locks_off_graph_unlock())
401 return 0;
402
403 printk("BUG: MAX_STACK_TRACE_ENTRIES too low!\n");
404 printk("turning off the locking correctness validator.\n");
405 dump_stack();
406
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700407 return 0;
408 }
409
410 return 1;
411}
412
413unsigned int nr_hardirq_chains;
414unsigned int nr_softirq_chains;
415unsigned int nr_process_chains;
416unsigned int max_lockdep_depth;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700417
418#ifdef CONFIG_DEBUG_LOCKDEP
419/*
420 * We cannot printk in early bootup code. Not even early_printk()
421 * might work. So we mark any initialization errors and printk
422 * about it later on, in lockdep_info().
423 */
424static int lockdep_init_error;
Johannes Bergc71063c2007-07-19 01:49:02 -0700425static unsigned long lockdep_init_trace_data[20];
426static struct stack_trace lockdep_init_trace = {
427 .max_entries = ARRAY_SIZE(lockdep_init_trace_data),
428 .entries = lockdep_init_trace_data,
429};
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700430
431/*
432 * Various lockdep statistics:
433 */
Frederic Weisbeckerbd6d29c2010-04-06 00:10:17 +0200434DEFINE_PER_CPU(struct lockdep_stats, lockdep_stats);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700435#endif
436
437/*
438 * Locking printouts:
439 */
440
Peter Zijlstrafabe9c42009-01-22 14:51:01 +0100441#define __USAGE(__STATE) \
Peter Zijlstrab4b136f2009-01-29 14:50:36 +0100442 [LOCK_USED_IN_##__STATE] = "IN-"__stringify(__STATE)"-W", \
443 [LOCK_ENABLED_##__STATE] = __stringify(__STATE)"-ON-W", \
444 [LOCK_USED_IN_##__STATE##_READ] = "IN-"__stringify(__STATE)"-R",\
445 [LOCK_ENABLED_##__STATE##_READ] = __stringify(__STATE)"-ON-R",
Peter Zijlstrafabe9c42009-01-22 14:51:01 +0100446
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700447static const char *usage_str[] =
448{
Peter Zijlstrafabe9c42009-01-22 14:51:01 +0100449#define LOCKDEP_STATE(__STATE) __USAGE(__STATE)
450#include "lockdep_states.h"
451#undef LOCKDEP_STATE
452 [LOCK_USED] = "INITIAL USE",
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700453};
454
455const char * __get_key_name(struct lockdep_subclass_key *key, char *str)
456{
Alexey Dobriyanffb45122007-05-08 00:28:41 -0700457 return kallsyms_lookup((unsigned long)key, NULL, NULL, NULL, str);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700458}
459
Peter Zijlstra3ff176c2009-01-22 17:40:42 +0100460static inline unsigned long lock_flag(enum lock_usage_bit bit)
461{
462 return 1UL << bit;
463}
464
465static char get_usage_char(struct lock_class *class, enum lock_usage_bit bit)
466{
467 char c = '.';
468
469 if (class->usage_mask & lock_flag(bit + 2))
470 c = '+';
471 if (class->usage_mask & lock_flag(bit)) {
472 c = '-';
473 if (class->usage_mask & lock_flag(bit + 2))
474 c = '?';
475 }
476
477 return c;
478}
479
Peter Zijlstraf510b232009-01-22 17:53:47 +0100480void get_usage_chars(struct lock_class *class, char usage[LOCK_USAGE_CHARS])
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700481{
Peter Zijlstraf510b232009-01-22 17:53:47 +0100482 int i = 0;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700483
Peter Zijlstraf510b232009-01-22 17:53:47 +0100484#define LOCKDEP_STATE(__STATE) \
485 usage[i++] = get_usage_char(class, LOCK_USED_IN_##__STATE); \
486 usage[i++] = get_usage_char(class, LOCK_USED_IN_##__STATE##_READ);
487#include "lockdep_states.h"
488#undef LOCKDEP_STATE
489
490 usage[i] = '\0';
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700491}
492
493static void print_lock_name(struct lock_class *class)
494{
Peter Zijlstraf510b232009-01-22 17:53:47 +0100495 char str[KSYM_NAME_LEN], usage[LOCK_USAGE_CHARS];
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700496 const char *name;
497
Peter Zijlstraf510b232009-01-22 17:53:47 +0100498 get_usage_chars(class, usage);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700499
500 name = class->name;
501 if (!name) {
502 name = __get_key_name(class->key, str);
503 printk(" (%s", name);
504 } else {
505 printk(" (%s", name);
506 if (class->name_version > 1)
507 printk("#%d", class->name_version);
508 if (class->subclass)
509 printk("/%d", class->subclass);
510 }
Peter Zijlstraf510b232009-01-22 17:53:47 +0100511 printk("){%s}", usage);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700512}
513
514static void print_lockdep_cache(struct lockdep_map *lock)
515{
516 const char *name;
Tejun Heo9281ace2007-07-17 04:03:51 -0700517 char str[KSYM_NAME_LEN];
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700518
519 name = lock->name;
520 if (!name)
521 name = __get_key_name(lock->key->subkeys, str);
522
523 printk("%s", name);
524}
525
526static void print_lock(struct held_lock *hlock)
527{
Dave Jonesf82b2172008-08-11 09:30:23 +0200528 print_lock_name(hlock_class(hlock));
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700529 printk(", at: ");
530 print_ip_sym(hlock->acquire_ip);
531}
532
533static void lockdep_print_held_locks(struct task_struct *curr)
534{
535 int i, depth = curr->lockdep_depth;
536
537 if (!depth) {
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -0700538 printk("no locks held by %s/%d.\n", curr->comm, task_pid_nr(curr));
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700539 return;
540 }
541 printk("%d lock%s held by %s/%d:\n",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -0700542 depth, depth > 1 ? "s" : "", curr->comm, task_pid_nr(curr));
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700543
544 for (i = 0; i < depth; i++) {
545 printk(" #%d: ", i);
546 print_lock(curr->held_locks + i);
547 }
548}
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700549
Dave Jones99de0552006-09-29 02:00:10 -0700550static void print_kernel_version(void)
551{
Serge E. Hallyn96b644b2006-10-02 02:18:13 -0700552 printk("%s %.*s\n", init_utsname()->release,
553 (int)strcspn(init_utsname()->version, " "),
554 init_utsname()->version);
Dave Jones99de0552006-09-29 02:00:10 -0700555}
556
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700557static int very_verbose(struct lock_class *class)
558{
559#if VERY_VERBOSE
560 return class_filter(class);
561#endif
562 return 0;
563}
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700564
565/*
566 * Is this the address of a static object:
567 */
568static int static_obj(void *obj)
569{
570 unsigned long start = (unsigned long) &_stext,
571 end = (unsigned long) &_end,
572 addr = (unsigned long) obj;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700573
574 /*
575 * static variable?
576 */
577 if ((addr >= start) && (addr < end))
578 return 1;
579
Mike Frysinger2a9ad182009-09-22 16:44:16 -0700580 if (arch_is_kernel_data(addr))
581 return 1;
582
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700583 /*
Tejun Heo10fad5e2010-03-10 18:57:54 +0900584 * in-kernel percpu var?
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700585 */
Tejun Heo10fad5e2010-03-10 18:57:54 +0900586 if (is_kernel_percpu_address(addr))
587 return 1;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700588
589 /*
Tejun Heo10fad5e2010-03-10 18:57:54 +0900590 * module static or percpu var?
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700591 */
Tejun Heo10fad5e2010-03-10 18:57:54 +0900592 return is_module_address(addr) || is_module_percpu_address(addr);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700593}
594
595/*
596 * To make lock name printouts unique, we calculate a unique
597 * class->name_version generation counter:
598 */
599static int count_matching_names(struct lock_class *new_class)
600{
601 struct lock_class *class;
602 int count = 0;
603
604 if (!new_class->name)
605 return 0;
606
607 list_for_each_entry(class, &all_lock_classes, lock_entry) {
608 if (new_class->key - new_class->subclass == class->key)
609 return class->name_version;
610 if (class->name && !strcmp(class->name, new_class->name))
611 count = max(count, class->name_version);
612 }
613
614 return count + 1;
615}
616
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700617/*
618 * Register a lock's class in the hash-table, if the class is not present
619 * yet. Otherwise we look it up. We cache the result in the lock object
620 * itself, so actual lookup of the hash should be once per lock object.
621 */
622static inline struct lock_class *
Ingo Molnard6d897c2006-07-10 04:44:04 -0700623look_up_lock_class(struct lockdep_map *lock, unsigned int subclass)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700624{
625 struct lockdep_subclass_key *key;
626 struct list_head *hash_head;
627 struct lock_class *class;
628
629#ifdef CONFIG_DEBUG_LOCKDEP
630 /*
631 * If the architecture calls into lockdep before initializing
632 * the hashes then we'll warn about it later. (we cannot printk
633 * right now)
634 */
635 if (unlikely(!lockdep_initialized)) {
636 lockdep_init();
637 lockdep_init_error = 1;
Johannes Bergc71063c2007-07-19 01:49:02 -0700638 save_stack_trace(&lockdep_init_trace);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700639 }
640#endif
641
642 /*
643 * Static locks do not have their class-keys yet - for them the key
644 * is the lock object itself:
645 */
646 if (unlikely(!lock->key))
647 lock->key = (void *)lock;
648
649 /*
650 * NOTE: the class-key must be unique. For dynamic locks, a static
651 * lock_class_key variable is passed in through the mutex_init()
652 * (or spin_lock_init()) call - which acts as the key. For static
653 * locks we use the lock object itself as the key.
654 */
Peter Zijlstra4b32d0a2007-07-19 01:48:59 -0700655 BUILD_BUG_ON(sizeof(struct lock_class_key) >
656 sizeof(struct lockdep_map));
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700657
658 key = lock->key->subkeys + subclass;
659
660 hash_head = classhashentry(key);
661
662 /*
663 * We can walk the hash lockfree, because the hash only
664 * grows, and we are careful when adding entries to the end:
665 */
Peter Zijlstra4b32d0a2007-07-19 01:48:59 -0700666 list_for_each_entry(class, hash_head, hash_entry) {
667 if (class->key == key) {
668 WARN_ON_ONCE(class->name != lock->name);
Ingo Molnard6d897c2006-07-10 04:44:04 -0700669 return class;
Peter Zijlstra4b32d0a2007-07-19 01:48:59 -0700670 }
671 }
Ingo Molnard6d897c2006-07-10 04:44:04 -0700672
673 return NULL;
674}
675
676/*
677 * Register a lock's class in the hash-table, if the class is not present
678 * yet. Otherwise we look it up. We cache the result in the lock object
679 * itself, so actual lookup of the hash should be once per lock object.
680 */
681static inline struct lock_class *
Peter Zijlstra4dfbb9d2006-10-11 01:45:14 -0400682register_lock_class(struct lockdep_map *lock, unsigned int subclass, int force)
Ingo Molnard6d897c2006-07-10 04:44:04 -0700683{
684 struct lockdep_subclass_key *key;
685 struct list_head *hash_head;
686 struct lock_class *class;
Ingo Molnar70e45062006-12-06 20:40:50 -0800687 unsigned long flags;
Ingo Molnard6d897c2006-07-10 04:44:04 -0700688
689 class = look_up_lock_class(lock, subclass);
690 if (likely(class))
691 return class;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700692
693 /*
694 * Debug-check: all keys must be persistent!
695 */
696 if (!static_obj(lock->key)) {
697 debug_locks_off();
698 printk("INFO: trying to register non-static key.\n");
699 printk("the code is fine but needs lockdep annotation.\n");
700 printk("turning off the locking correctness validator.\n");
701 dump_stack();
702
703 return NULL;
704 }
705
Ingo Molnard6d897c2006-07-10 04:44:04 -0700706 key = lock->key->subkeys + subclass;
707 hash_head = classhashentry(key);
708
Ingo Molnar70e45062006-12-06 20:40:50 -0800709 raw_local_irq_save(flags);
Ingo Molnar74c383f2006-12-13 00:34:43 -0800710 if (!graph_lock()) {
711 raw_local_irq_restore(flags);
712 return NULL;
713 }
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700714 /*
715 * We have to do the hash-walk again, to avoid races
716 * with another CPU:
717 */
718 list_for_each_entry(class, hash_head, hash_entry)
719 if (class->key == key)
720 goto out_unlock_set;
721 /*
722 * Allocate a new key from the static array, and add it to
723 * the hash:
724 */
725 if (nr_lock_classes >= MAX_LOCKDEP_KEYS) {
Ingo Molnar74c383f2006-12-13 00:34:43 -0800726 if (!debug_locks_off_graph_unlock()) {
727 raw_local_irq_restore(flags);
728 return NULL;
729 }
Ingo Molnar70e45062006-12-06 20:40:50 -0800730 raw_local_irq_restore(flags);
Ingo Molnar74c383f2006-12-13 00:34:43 -0800731
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700732 printk("BUG: MAX_LOCKDEP_KEYS too low!\n");
733 printk("turning off the locking correctness validator.\n");
Peter Zijlstraeedeeab2009-03-18 12:38:47 +0100734 dump_stack();
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700735 return NULL;
736 }
737 class = lock_classes + nr_lock_classes++;
Frederic Weisbeckerbd6d29c2010-04-06 00:10:17 +0200738 debug_atomic_inc(nr_unused_locks);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700739 class->key = key;
740 class->name = lock->name;
741 class->subclass = subclass;
742 INIT_LIST_HEAD(&class->lock_entry);
743 INIT_LIST_HEAD(&class->locks_before);
744 INIT_LIST_HEAD(&class->locks_after);
745 class->name_version = count_matching_names(class);
746 /*
747 * We use RCU's safe list-add method to make
748 * parallel walking of the hash-list safe:
749 */
750 list_add_tail_rcu(&class->hash_entry, hash_head);
Dale Farnsworth14811972008-02-25 23:03:02 +0100751 /*
752 * Add it to the global list of classes:
753 */
754 list_add_tail_rcu(&class->lock_entry, &all_lock_classes);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700755
756 if (verbose(class)) {
Ingo Molnar74c383f2006-12-13 00:34:43 -0800757 graph_unlock();
Ingo Molnar70e45062006-12-06 20:40:50 -0800758 raw_local_irq_restore(flags);
Ingo Molnar74c383f2006-12-13 00:34:43 -0800759
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700760 printk("\nnew class %p: %s", class->key, class->name);
761 if (class->name_version > 1)
762 printk("#%d", class->name_version);
763 printk("\n");
764 dump_stack();
Ingo Molnar74c383f2006-12-13 00:34:43 -0800765
Ingo Molnar70e45062006-12-06 20:40:50 -0800766 raw_local_irq_save(flags);
Ingo Molnar74c383f2006-12-13 00:34:43 -0800767 if (!graph_lock()) {
768 raw_local_irq_restore(flags);
769 return NULL;
770 }
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700771 }
772out_unlock_set:
Ingo Molnar74c383f2006-12-13 00:34:43 -0800773 graph_unlock();
Ingo Molnar70e45062006-12-06 20:40:50 -0800774 raw_local_irq_restore(flags);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700775
Peter Zijlstra4dfbb9d2006-10-11 01:45:14 -0400776 if (!subclass || force)
Hitoshi Mitake62016252010-10-05 18:01:51 +0900777 lock->class_cache[0] = class;
778 else if (subclass < NR_LOCKDEP_CACHING_CLASSES)
779 lock->class_cache[subclass] = class;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700780
Jarek Poplawski381a2292007-02-10 01:44:58 -0800781 if (DEBUG_LOCKS_WARN_ON(class->subclass != subclass))
782 return NULL;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700783
784 return class;
785}
786
Peter Zijlstraca58abc2007-07-19 01:48:53 -0700787#ifdef CONFIG_PROVE_LOCKING
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700788/*
Peter Zijlstra8e182572007-07-19 01:48:54 -0700789 * Allocate a lockdep entry. (assumes the graph_lock held, returns
790 * with NULL on failure)
791 */
792static struct lock_list *alloc_list_entry(void)
793{
794 if (nr_list_entries >= MAX_LOCKDEP_ENTRIES) {
795 if (!debug_locks_off_graph_unlock())
796 return NULL;
797
798 printk("BUG: MAX_LOCKDEP_ENTRIES too low!\n");
799 printk("turning off the locking correctness validator.\n");
Peter Zijlstraeedeeab2009-03-18 12:38:47 +0100800 dump_stack();
Peter Zijlstra8e182572007-07-19 01:48:54 -0700801 return NULL;
802 }
803 return list_entries + nr_list_entries++;
804}
805
806/*
807 * Add a new dependency to the head of the list:
808 */
809static int add_lock_to_list(struct lock_class *class, struct lock_class *this,
Yong Zhang4726f2a2010-05-04 14:16:48 +0800810 struct list_head *head, unsigned long ip,
811 int distance, struct stack_trace *trace)
Peter Zijlstra8e182572007-07-19 01:48:54 -0700812{
813 struct lock_list *entry;
814 /*
815 * Lock not present yet - get a new dependency struct and
816 * add it to the list:
817 */
818 entry = alloc_list_entry();
819 if (!entry)
820 return 0;
821
Zhu Yi74870172008-08-27 14:33:00 +0800822 entry->class = this;
823 entry->distance = distance;
Yong Zhang4726f2a2010-05-04 14:16:48 +0800824 entry->trace = *trace;
Peter Zijlstra8e182572007-07-19 01:48:54 -0700825 /*
826 * Since we never remove from the dependency list, the list can
827 * be walked lockless by other CPUs, it's only allocation
828 * that must be protected by the spinlock. But this also means
829 * we must make new entries visible only once writes to the
830 * entry become visible - hence the RCU op:
831 */
832 list_add_tail_rcu(&entry->entry, head);
833
834 return 1;
835}
836
Peter Zijlstra98c33ed2009-07-21 13:19:07 +0200837/*
838 * For good efficiency of modular, we use power of 2
839 */
Peter Zijlstraaf012962009-07-16 15:44:29 +0200840#define MAX_CIRCULAR_QUEUE_SIZE 4096UL
841#define CQ_MASK (MAX_CIRCULAR_QUEUE_SIZE-1)
842
Peter Zijlstra98c33ed2009-07-21 13:19:07 +0200843/*
844 * The circular_queue and helpers is used to implement the
Peter Zijlstraaf012962009-07-16 15:44:29 +0200845 * breadth-first search(BFS)algorithem, by which we can build
846 * the shortest path from the next lock to be acquired to the
847 * previous held lock if there is a circular between them.
Peter Zijlstra98c33ed2009-07-21 13:19:07 +0200848 */
Peter Zijlstraaf012962009-07-16 15:44:29 +0200849struct circular_queue {
850 unsigned long element[MAX_CIRCULAR_QUEUE_SIZE];
851 unsigned int front, rear;
852};
853
854static struct circular_queue lock_cq;
Peter Zijlstraaf012962009-07-16 15:44:29 +0200855
Ming Lei12f3dfd2009-07-16 15:44:29 +0200856unsigned int max_bfs_queue_depth;
Peter Zijlstraaf012962009-07-16 15:44:29 +0200857
Ming Leie351b662009-07-22 22:48:09 +0800858static unsigned int lockdep_dependency_gen_id;
859
Peter Zijlstraaf012962009-07-16 15:44:29 +0200860static inline void __cq_init(struct circular_queue *cq)
861{
862 cq->front = cq->rear = 0;
Ming Leie351b662009-07-22 22:48:09 +0800863 lockdep_dependency_gen_id++;
Peter Zijlstraaf012962009-07-16 15:44:29 +0200864}
865
866static inline int __cq_empty(struct circular_queue *cq)
867{
868 return (cq->front == cq->rear);
869}
870
871static inline int __cq_full(struct circular_queue *cq)
872{
873 return ((cq->rear + 1) & CQ_MASK) == cq->front;
874}
875
876static inline int __cq_enqueue(struct circular_queue *cq, unsigned long elem)
877{
878 if (__cq_full(cq))
879 return -1;
880
881 cq->element[cq->rear] = elem;
882 cq->rear = (cq->rear + 1) & CQ_MASK;
883 return 0;
884}
885
886static inline int __cq_dequeue(struct circular_queue *cq, unsigned long *elem)
887{
888 if (__cq_empty(cq))
889 return -1;
890
891 *elem = cq->element[cq->front];
892 cq->front = (cq->front + 1) & CQ_MASK;
893 return 0;
894}
895
896static inline unsigned int __cq_get_elem_count(struct circular_queue *cq)
897{
898 return (cq->rear - cq->front) & CQ_MASK;
899}
900
901static inline void mark_lock_accessed(struct lock_list *lock,
902 struct lock_list *parent)
903{
904 unsigned long nr;
Peter Zijlstra98c33ed2009-07-21 13:19:07 +0200905
Peter Zijlstraaf012962009-07-16 15:44:29 +0200906 nr = lock - list_entries;
907 WARN_ON(nr >= nr_list_entries);
908 lock->parent = parent;
Ming Leie351b662009-07-22 22:48:09 +0800909 lock->class->dep_gen_id = lockdep_dependency_gen_id;
Peter Zijlstraaf012962009-07-16 15:44:29 +0200910}
911
912static inline unsigned long lock_accessed(struct lock_list *lock)
913{
914 unsigned long nr;
Peter Zijlstra98c33ed2009-07-21 13:19:07 +0200915
Peter Zijlstraaf012962009-07-16 15:44:29 +0200916 nr = lock - list_entries;
917 WARN_ON(nr >= nr_list_entries);
Ming Leie351b662009-07-22 22:48:09 +0800918 return lock->class->dep_gen_id == lockdep_dependency_gen_id;
Peter Zijlstraaf012962009-07-16 15:44:29 +0200919}
920
921static inline struct lock_list *get_lock_parent(struct lock_list *child)
922{
923 return child->parent;
924}
925
926static inline int get_lock_depth(struct lock_list *child)
927{
928 int depth = 0;
929 struct lock_list *parent;
930
931 while ((parent = get_lock_parent(child))) {
932 child = parent;
933 depth++;
934 }
935 return depth;
936}
937
Ming Lei9e2d5512009-07-16 15:44:29 +0200938static int __bfs(struct lock_list *source_entry,
Peter Zijlstraaf012962009-07-16 15:44:29 +0200939 void *data,
940 int (*match)(struct lock_list *entry, void *data),
941 struct lock_list **target_entry,
942 int forward)
Ming Leic94aa5c2009-07-16 15:44:29 +0200943{
944 struct lock_list *entry;
Ming Leid588e462009-07-16 15:44:29 +0200945 struct list_head *head;
Ming Leic94aa5c2009-07-16 15:44:29 +0200946 struct circular_queue *cq = &lock_cq;
947 int ret = 1;
948
Ming Lei9e2d5512009-07-16 15:44:29 +0200949 if (match(source_entry, data)) {
Ming Leic94aa5c2009-07-16 15:44:29 +0200950 *target_entry = source_entry;
951 ret = 0;
952 goto exit;
953 }
954
Ming Leid588e462009-07-16 15:44:29 +0200955 if (forward)
956 head = &source_entry->class->locks_after;
957 else
958 head = &source_entry->class->locks_before;
959
960 if (list_empty(head))
961 goto exit;
962
963 __cq_init(cq);
Ming Leic94aa5c2009-07-16 15:44:29 +0200964 __cq_enqueue(cq, (unsigned long)source_entry);
965
966 while (!__cq_empty(cq)) {
967 struct lock_list *lock;
Ming Leic94aa5c2009-07-16 15:44:29 +0200968
969 __cq_dequeue(cq, (unsigned long *)&lock);
970
971 if (!lock->class) {
972 ret = -2;
973 goto exit;
974 }
975
976 if (forward)
977 head = &lock->class->locks_after;
978 else
979 head = &lock->class->locks_before;
980
981 list_for_each_entry(entry, head, entry) {
982 if (!lock_accessed(entry)) {
Ming Lei12f3dfd2009-07-16 15:44:29 +0200983 unsigned int cq_depth;
Ming Leic94aa5c2009-07-16 15:44:29 +0200984 mark_lock_accessed(entry, lock);
Ming Lei9e2d5512009-07-16 15:44:29 +0200985 if (match(entry, data)) {
Ming Leic94aa5c2009-07-16 15:44:29 +0200986 *target_entry = entry;
987 ret = 0;
988 goto exit;
989 }
990
991 if (__cq_enqueue(cq, (unsigned long)entry)) {
992 ret = -1;
993 goto exit;
994 }
Ming Lei12f3dfd2009-07-16 15:44:29 +0200995 cq_depth = __cq_get_elem_count(cq);
996 if (max_bfs_queue_depth < cq_depth)
997 max_bfs_queue_depth = cq_depth;
Ming Leic94aa5c2009-07-16 15:44:29 +0200998 }
999 }
1000 }
1001exit:
1002 return ret;
1003}
1004
Ming Leid7aaba12009-07-16 15:44:29 +02001005static inline int __bfs_forwards(struct lock_list *src_entry,
Ming Lei9e2d5512009-07-16 15:44:29 +02001006 void *data,
1007 int (*match)(struct lock_list *entry, void *data),
1008 struct lock_list **target_entry)
Ming Leic94aa5c2009-07-16 15:44:29 +02001009{
Ming Lei9e2d5512009-07-16 15:44:29 +02001010 return __bfs(src_entry, data, match, target_entry, 1);
Ming Leic94aa5c2009-07-16 15:44:29 +02001011
1012}
1013
Ming Leid7aaba12009-07-16 15:44:29 +02001014static inline int __bfs_backwards(struct lock_list *src_entry,
Ming Lei9e2d5512009-07-16 15:44:29 +02001015 void *data,
1016 int (*match)(struct lock_list *entry, void *data),
1017 struct lock_list **target_entry)
Ming Leic94aa5c2009-07-16 15:44:29 +02001018{
Ming Lei9e2d5512009-07-16 15:44:29 +02001019 return __bfs(src_entry, data, match, target_entry, 0);
Ming Leic94aa5c2009-07-16 15:44:29 +02001020
1021}
1022
Peter Zijlstra8e182572007-07-19 01:48:54 -07001023/*
1024 * Recursive, forwards-direction lock-dependency checking, used for
1025 * both noncyclic checking and for hardirq-unsafe/softirq-unsafe
1026 * checking.
Peter Zijlstra8e182572007-07-19 01:48:54 -07001027 */
Peter Zijlstra8e182572007-07-19 01:48:54 -07001028
1029/*
1030 * Print a dependency chain entry (this is only done when a deadlock
1031 * has been detected):
1032 */
1033static noinline int
Ming Lei24208ca2009-07-16 15:44:29 +02001034print_circular_bug_entry(struct lock_list *target, int depth)
Peter Zijlstra8e182572007-07-19 01:48:54 -07001035{
1036 if (debug_locks_silent)
1037 return 0;
1038 printk("\n-> #%u", depth);
1039 print_lock_name(target->class);
1040 printk(":\n");
1041 print_stack_trace(&target->trace, 6);
1042
1043 return 0;
1044}
1045
1046/*
1047 * When a circular dependency is detected, print the
1048 * header first:
1049 */
1050static noinline int
Ming Leidb0002a2009-07-16 15:44:29 +02001051print_circular_bug_header(struct lock_list *entry, unsigned int depth,
1052 struct held_lock *check_src,
1053 struct held_lock *check_tgt)
Peter Zijlstra8e182572007-07-19 01:48:54 -07001054{
1055 struct task_struct *curr = current;
1056
Ming Leic94aa5c2009-07-16 15:44:29 +02001057 if (debug_locks_silent)
Peter Zijlstra8e182572007-07-19 01:48:54 -07001058 return 0;
1059
1060 printk("\n=======================================================\n");
1061 printk( "[ INFO: possible circular locking dependency detected ]\n");
1062 print_kernel_version();
1063 printk( "-------------------------------------------------------\n");
1064 printk("%s/%d is trying to acquire lock:\n",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07001065 curr->comm, task_pid_nr(curr));
Ming Leidb0002a2009-07-16 15:44:29 +02001066 print_lock(check_src);
Peter Zijlstra8e182572007-07-19 01:48:54 -07001067 printk("\nbut task is already holding lock:\n");
Ming Leidb0002a2009-07-16 15:44:29 +02001068 print_lock(check_tgt);
Peter Zijlstra8e182572007-07-19 01:48:54 -07001069 printk("\nwhich lock already depends on the new lock.\n\n");
1070 printk("\nthe existing dependency chain (in reverse order) is:\n");
1071
1072 print_circular_bug_entry(entry, depth);
1073
1074 return 0;
1075}
1076
Ming Lei9e2d5512009-07-16 15:44:29 +02001077static inline int class_equal(struct lock_list *entry, void *data)
1078{
1079 return entry->class == data;
1080}
1081
Ming Leidb0002a2009-07-16 15:44:29 +02001082static noinline int print_circular_bug(struct lock_list *this,
1083 struct lock_list *target,
1084 struct held_lock *check_src,
1085 struct held_lock *check_tgt)
Peter Zijlstra8e182572007-07-19 01:48:54 -07001086{
1087 struct task_struct *curr = current;
Ming Leic94aa5c2009-07-16 15:44:29 +02001088 struct lock_list *parent;
Ming Lei24208ca2009-07-16 15:44:29 +02001089 int depth;
Peter Zijlstra8e182572007-07-19 01:48:54 -07001090
Ming Leic94aa5c2009-07-16 15:44:29 +02001091 if (!debug_locks_off_graph_unlock() || debug_locks_silent)
Peter Zijlstra8e182572007-07-19 01:48:54 -07001092 return 0;
1093
Ming Leidb0002a2009-07-16 15:44:29 +02001094 if (!save_trace(&this->trace))
Peter Zijlstra8e182572007-07-19 01:48:54 -07001095 return 0;
1096
Ming Leic94aa5c2009-07-16 15:44:29 +02001097 depth = get_lock_depth(target);
1098
Ming Leidb0002a2009-07-16 15:44:29 +02001099 print_circular_bug_header(target, depth, check_src, check_tgt);
Ming Leic94aa5c2009-07-16 15:44:29 +02001100
1101 parent = get_lock_parent(target);
1102
1103 while (parent) {
1104 print_circular_bug_entry(parent, --depth);
1105 parent = get_lock_parent(parent);
1106 }
Peter Zijlstra8e182572007-07-19 01:48:54 -07001107
1108 printk("\nother info that might help us debug this:\n\n");
1109 lockdep_print_held_locks(curr);
1110
1111 printk("\nstack backtrace:\n");
1112 dump_stack();
1113
1114 return 0;
1115}
1116
Ming Leidb0002a2009-07-16 15:44:29 +02001117static noinline int print_bfs_bug(int ret)
1118{
1119 if (!debug_locks_off_graph_unlock())
1120 return 0;
1121
1122 WARN(1, "lockdep bfs error:%d\n", ret);
1123
1124 return 0;
1125}
1126
Ming Leief681022009-07-16 15:44:29 +02001127static int noop_count(struct lock_list *entry, void *data)
David Miller419ca3f2008-07-29 21:45:03 -07001128{
Ming Leief681022009-07-16 15:44:29 +02001129 (*(unsigned long *)data)++;
1130 return 0;
David Miller419ca3f2008-07-29 21:45:03 -07001131}
1132
Ming Leief681022009-07-16 15:44:29 +02001133unsigned long __lockdep_count_forward_deps(struct lock_list *this)
1134{
1135 unsigned long count = 0;
1136 struct lock_list *uninitialized_var(target_entry);
1137
1138 __bfs_forwards(this, (void *)&count, noop_count, &target_entry);
1139
1140 return count;
1141}
David Miller419ca3f2008-07-29 21:45:03 -07001142unsigned long lockdep_count_forward_deps(struct lock_class *class)
1143{
1144 unsigned long ret, flags;
Ming Leief681022009-07-16 15:44:29 +02001145 struct lock_list this;
1146
1147 this.parent = NULL;
1148 this.class = class;
David Miller419ca3f2008-07-29 21:45:03 -07001149
1150 local_irq_save(flags);
Thomas Gleixner0199c4e2009-12-02 20:01:25 +01001151 arch_spin_lock(&lockdep_lock);
Ming Leief681022009-07-16 15:44:29 +02001152 ret = __lockdep_count_forward_deps(&this);
Thomas Gleixner0199c4e2009-12-02 20:01:25 +01001153 arch_spin_unlock(&lockdep_lock);
David Miller419ca3f2008-07-29 21:45:03 -07001154 local_irq_restore(flags);
1155
1156 return ret;
1157}
1158
Ming Leief681022009-07-16 15:44:29 +02001159unsigned long __lockdep_count_backward_deps(struct lock_list *this)
David Miller419ca3f2008-07-29 21:45:03 -07001160{
Ming Leief681022009-07-16 15:44:29 +02001161 unsigned long count = 0;
1162 struct lock_list *uninitialized_var(target_entry);
David Miller419ca3f2008-07-29 21:45:03 -07001163
Ming Leief681022009-07-16 15:44:29 +02001164 __bfs_backwards(this, (void *)&count, noop_count, &target_entry);
David Miller419ca3f2008-07-29 21:45:03 -07001165
Ming Leief681022009-07-16 15:44:29 +02001166 return count;
David Miller419ca3f2008-07-29 21:45:03 -07001167}
1168
1169unsigned long lockdep_count_backward_deps(struct lock_class *class)
1170{
1171 unsigned long ret, flags;
Ming Leief681022009-07-16 15:44:29 +02001172 struct lock_list this;
1173
1174 this.parent = NULL;
1175 this.class = class;
David Miller419ca3f2008-07-29 21:45:03 -07001176
1177 local_irq_save(flags);
Thomas Gleixner0199c4e2009-12-02 20:01:25 +01001178 arch_spin_lock(&lockdep_lock);
Ming Leief681022009-07-16 15:44:29 +02001179 ret = __lockdep_count_backward_deps(&this);
Thomas Gleixner0199c4e2009-12-02 20:01:25 +01001180 arch_spin_unlock(&lockdep_lock);
David Miller419ca3f2008-07-29 21:45:03 -07001181 local_irq_restore(flags);
1182
1183 return ret;
1184}
1185
Peter Zijlstra8e182572007-07-19 01:48:54 -07001186/*
1187 * Prove that the dependency graph starting at <entry> can not
1188 * lead to <target>. Print an error and return 0 if it does.
1189 */
1190static noinline int
Ming Leidb0002a2009-07-16 15:44:29 +02001191check_noncircular(struct lock_list *root, struct lock_class *target,
1192 struct lock_list **target_entry)
Peter Zijlstra8e182572007-07-19 01:48:54 -07001193{
Ming Leidb0002a2009-07-16 15:44:29 +02001194 int result;
Peter Zijlstra8e182572007-07-19 01:48:54 -07001195
Frederic Weisbeckerbd6d29c2010-04-06 00:10:17 +02001196 debug_atomic_inc(nr_cyclic_checks);
David Miller419ca3f2008-07-29 21:45:03 -07001197
Ming Leid7aaba12009-07-16 15:44:29 +02001198 result = __bfs_forwards(root, target, class_equal, target_entry);
Ming Leidb0002a2009-07-16 15:44:29 +02001199
1200 return result;
Peter Zijlstra8e182572007-07-19 01:48:54 -07001201}
1202
Steven Rostedt81d68a92008-05-12 21:20:42 +02001203#if defined(CONFIG_TRACE_IRQFLAGS) && defined(CONFIG_PROVE_LOCKING)
Peter Zijlstra8e182572007-07-19 01:48:54 -07001204/*
1205 * Forwards and backwards subgraph searching, for the purposes of
1206 * proving that two subgraphs can be connected by a new dependency
1207 * without creating any illegal irq-safe -> irq-unsafe lock dependency.
1208 */
Peter Zijlstra8e182572007-07-19 01:48:54 -07001209
Ming Leid7aaba12009-07-16 15:44:29 +02001210static inline int usage_match(struct lock_list *entry, void *bit)
1211{
1212 return entry->class->usage_mask & (1 << (enum lock_usage_bit)bit);
1213}
1214
1215
1216
Peter Zijlstra8e182572007-07-19 01:48:54 -07001217/*
1218 * Find a node in the forwards-direction dependency sub-graph starting
Ming Leid7aaba12009-07-16 15:44:29 +02001219 * at @root->class that matches @bit.
Peter Zijlstra8e182572007-07-19 01:48:54 -07001220 *
Ming Leid7aaba12009-07-16 15:44:29 +02001221 * Return 0 if such a node exists in the subgraph, and put that node
1222 * into *@target_entry.
Peter Zijlstra8e182572007-07-19 01:48:54 -07001223 *
Ming Leid7aaba12009-07-16 15:44:29 +02001224 * Return 1 otherwise and keep *@target_entry unchanged.
1225 * Return <0 on error.
Peter Zijlstra8e182572007-07-19 01:48:54 -07001226 */
Ming Leid7aaba12009-07-16 15:44:29 +02001227static int
1228find_usage_forwards(struct lock_list *root, enum lock_usage_bit bit,
1229 struct lock_list **target_entry)
Peter Zijlstra8e182572007-07-19 01:48:54 -07001230{
Ming Leid7aaba12009-07-16 15:44:29 +02001231 int result;
Peter Zijlstra8e182572007-07-19 01:48:54 -07001232
Frederic Weisbeckerbd6d29c2010-04-06 00:10:17 +02001233 debug_atomic_inc(nr_find_usage_forwards_checks);
Peter Zijlstra8e182572007-07-19 01:48:54 -07001234
Ming Leid7aaba12009-07-16 15:44:29 +02001235 result = __bfs_forwards(root, (void *)bit, usage_match, target_entry);
1236
1237 return result;
Peter Zijlstra8e182572007-07-19 01:48:54 -07001238}
1239
1240/*
1241 * Find a node in the backwards-direction dependency sub-graph starting
Ming Leid7aaba12009-07-16 15:44:29 +02001242 * at @root->class that matches @bit.
Peter Zijlstra8e182572007-07-19 01:48:54 -07001243 *
Ming Leid7aaba12009-07-16 15:44:29 +02001244 * Return 0 if such a node exists in the subgraph, and put that node
1245 * into *@target_entry.
Peter Zijlstra8e182572007-07-19 01:48:54 -07001246 *
Ming Leid7aaba12009-07-16 15:44:29 +02001247 * Return 1 otherwise and keep *@target_entry unchanged.
1248 * Return <0 on error.
Peter Zijlstra8e182572007-07-19 01:48:54 -07001249 */
Ming Leid7aaba12009-07-16 15:44:29 +02001250static int
1251find_usage_backwards(struct lock_list *root, enum lock_usage_bit bit,
1252 struct lock_list **target_entry)
Peter Zijlstra8e182572007-07-19 01:48:54 -07001253{
Ming Leid7aaba12009-07-16 15:44:29 +02001254 int result;
Peter Zijlstra8e182572007-07-19 01:48:54 -07001255
Frederic Weisbeckerbd6d29c2010-04-06 00:10:17 +02001256 debug_atomic_inc(nr_find_usage_backwards_checks);
Peter Zijlstra8e182572007-07-19 01:48:54 -07001257
Ming Leid7aaba12009-07-16 15:44:29 +02001258 result = __bfs_backwards(root, (void *)bit, usage_match, target_entry);
Dave Jonesf82b2172008-08-11 09:30:23 +02001259
Ming Leid7aaba12009-07-16 15:44:29 +02001260 return result;
Peter Zijlstra8e182572007-07-19 01:48:54 -07001261}
1262
Peter Zijlstraaf012962009-07-16 15:44:29 +02001263static void print_lock_class_header(struct lock_class *class, int depth)
1264{
1265 int bit;
1266
1267 printk("%*s->", depth, "");
1268 print_lock_name(class);
1269 printk(" ops: %lu", class->ops);
1270 printk(" {\n");
1271
1272 for (bit = 0; bit < LOCK_USAGE_STATES; bit++) {
1273 if (class->usage_mask & (1 << bit)) {
1274 int len = depth;
1275
1276 len += printk("%*s %s", depth, "", usage_str[bit]);
1277 len += printk(" at:\n");
1278 print_stack_trace(class->usage_traces + bit, len);
1279 }
1280 }
1281 printk("%*s }\n", depth, "");
1282
1283 printk("%*s ... key at: ",depth,"");
1284 print_ip_sym((unsigned long)class->key);
1285}
1286
1287/*
1288 * printk the shortest lock dependencies from @start to @end in reverse order:
1289 */
1290static void __used
1291print_shortest_lock_dependencies(struct lock_list *leaf,
1292 struct lock_list *root)
1293{
1294 struct lock_list *entry = leaf;
1295 int depth;
1296
1297 /*compute depth from generated tree by BFS*/
1298 depth = get_lock_depth(leaf);
1299
1300 do {
1301 print_lock_class_header(entry->class, depth);
1302 printk("%*s ... acquired at:\n", depth, "");
1303 print_stack_trace(&entry->trace, 2);
1304 printk("\n");
1305
1306 if (depth == 0 && (entry != root)) {
1307 printk("lockdep:%s bad BFS generated tree\n", __func__);
1308 break;
1309 }
1310
1311 entry = get_lock_parent(entry);
1312 depth--;
1313 } while (entry && (depth >= 0));
1314
1315 return;
1316}
Ming Leid7aaba12009-07-16 15:44:29 +02001317
Peter Zijlstra8e182572007-07-19 01:48:54 -07001318static int
1319print_bad_irq_dependency(struct task_struct *curr,
Ming Lei24208ca2009-07-16 15:44:29 +02001320 struct lock_list *prev_root,
1321 struct lock_list *next_root,
1322 struct lock_list *backwards_entry,
1323 struct lock_list *forwards_entry,
Peter Zijlstra8e182572007-07-19 01:48:54 -07001324 struct held_lock *prev,
1325 struct held_lock *next,
1326 enum lock_usage_bit bit1,
1327 enum lock_usage_bit bit2,
1328 const char *irqclass)
1329{
1330 if (!debug_locks_off_graph_unlock() || debug_locks_silent)
1331 return 0;
1332
1333 printk("\n======================================================\n");
1334 printk( "[ INFO: %s-safe -> %s-unsafe lock order detected ]\n",
1335 irqclass, irqclass);
1336 print_kernel_version();
1337 printk( "------------------------------------------------------\n");
1338 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 -07001339 curr->comm, task_pid_nr(curr),
Peter Zijlstra8e182572007-07-19 01:48:54 -07001340 curr->hardirq_context, hardirq_count() >> HARDIRQ_SHIFT,
1341 curr->softirq_context, softirq_count() >> SOFTIRQ_SHIFT,
1342 curr->hardirqs_enabled,
1343 curr->softirqs_enabled);
1344 print_lock(next);
1345
1346 printk("\nand this task is already holding:\n");
1347 print_lock(prev);
1348 printk("which would create a new lock dependency:\n");
Dave Jonesf82b2172008-08-11 09:30:23 +02001349 print_lock_name(hlock_class(prev));
Peter Zijlstra8e182572007-07-19 01:48:54 -07001350 printk(" ->");
Dave Jonesf82b2172008-08-11 09:30:23 +02001351 print_lock_name(hlock_class(next));
Peter Zijlstra8e182572007-07-19 01:48:54 -07001352 printk("\n");
1353
1354 printk("\nbut this new dependency connects a %s-irq-safe lock:\n",
1355 irqclass);
Ming Lei24208ca2009-07-16 15:44:29 +02001356 print_lock_name(backwards_entry->class);
Peter Zijlstra8e182572007-07-19 01:48:54 -07001357 printk("\n... which became %s-irq-safe at:\n", irqclass);
1358
Ming Lei24208ca2009-07-16 15:44:29 +02001359 print_stack_trace(backwards_entry->class->usage_traces + bit1, 1);
Peter Zijlstra8e182572007-07-19 01:48:54 -07001360
1361 printk("\nto a %s-irq-unsafe lock:\n", irqclass);
Ming Lei24208ca2009-07-16 15:44:29 +02001362 print_lock_name(forwards_entry->class);
Peter Zijlstra8e182572007-07-19 01:48:54 -07001363 printk("\n... which became %s-irq-unsafe at:\n", irqclass);
1364 printk("...");
1365
Ming Lei24208ca2009-07-16 15:44:29 +02001366 print_stack_trace(forwards_entry->class->usage_traces + bit2, 1);
Peter Zijlstra8e182572007-07-19 01:48:54 -07001367
1368 printk("\nother info that might help us debug this:\n\n");
1369 lockdep_print_held_locks(curr);
1370
Ming Lei24208ca2009-07-16 15:44:29 +02001371 printk("\nthe dependencies between %s-irq-safe lock", irqclass);
1372 printk(" and the holding lock:\n");
1373 if (!save_trace(&prev_root->trace))
1374 return 0;
1375 print_shortest_lock_dependencies(backwards_entry, prev_root);
Peter Zijlstra8e182572007-07-19 01:48:54 -07001376
Ming Lei24208ca2009-07-16 15:44:29 +02001377 printk("\nthe dependencies between the lock to be acquired");
1378 printk(" and %s-irq-unsafe lock:\n", irqclass);
1379 if (!save_trace(&next_root->trace))
1380 return 0;
1381 print_shortest_lock_dependencies(forwards_entry, next_root);
Peter Zijlstra8e182572007-07-19 01:48:54 -07001382
1383 printk("\nstack backtrace:\n");
1384 dump_stack();
1385
1386 return 0;
1387}
1388
1389static int
1390check_usage(struct task_struct *curr, struct held_lock *prev,
1391 struct held_lock *next, enum lock_usage_bit bit_backwards,
1392 enum lock_usage_bit bit_forwards, const char *irqclass)
1393{
1394 int ret;
Ming Lei24208ca2009-07-16 15:44:29 +02001395 struct lock_list this, that;
Ming Leid7aaba12009-07-16 15:44:29 +02001396 struct lock_list *uninitialized_var(target_entry);
Ming Lei24208ca2009-07-16 15:44:29 +02001397 struct lock_list *uninitialized_var(target_entry1);
Peter Zijlstra8e182572007-07-19 01:48:54 -07001398
Ming Leid7aaba12009-07-16 15:44:29 +02001399 this.parent = NULL;
Peter Zijlstra8e182572007-07-19 01:48:54 -07001400
Ming Leid7aaba12009-07-16 15:44:29 +02001401 this.class = hlock_class(prev);
1402 ret = find_usage_backwards(&this, bit_backwards, &target_entry);
Peter Zijlstraaf012962009-07-16 15:44:29 +02001403 if (ret < 0)
1404 return print_bfs_bug(ret);
1405 if (ret == 1)
1406 return ret;
Ming Leid7aaba12009-07-16 15:44:29 +02001407
Ming Lei24208ca2009-07-16 15:44:29 +02001408 that.parent = NULL;
1409 that.class = hlock_class(next);
1410 ret = find_usage_forwards(&that, bit_forwards, &target_entry1);
Peter Zijlstraaf012962009-07-16 15:44:29 +02001411 if (ret < 0)
1412 return print_bfs_bug(ret);
1413 if (ret == 1)
1414 return ret;
Ming Leid7aaba12009-07-16 15:44:29 +02001415
Ming Lei24208ca2009-07-16 15:44:29 +02001416 return print_bad_irq_dependency(curr, &this, &that,
1417 target_entry, target_entry1,
1418 prev, next,
Peter Zijlstra8e182572007-07-19 01:48:54 -07001419 bit_backwards, bit_forwards, irqclass);
1420}
1421
Peter Zijlstra4f367d8a2009-01-22 18:10:42 +01001422static const char *state_names[] = {
1423#define LOCKDEP_STATE(__STATE) \
Peter Zijlstrab4b136f2009-01-29 14:50:36 +01001424 __stringify(__STATE),
Peter Zijlstra4f367d8a2009-01-22 18:10:42 +01001425#include "lockdep_states.h"
1426#undef LOCKDEP_STATE
1427};
1428
1429static const char *state_rnames[] = {
1430#define LOCKDEP_STATE(__STATE) \
Peter Zijlstrab4b136f2009-01-29 14:50:36 +01001431 __stringify(__STATE)"-READ",
Peter Zijlstra4f367d8a2009-01-22 18:10:42 +01001432#include "lockdep_states.h"
1433#undef LOCKDEP_STATE
1434};
1435
1436static inline const char *state_name(enum lock_usage_bit bit)
1437{
1438 return (bit & 1) ? state_rnames[bit >> 2] : state_names[bit >> 2];
1439}
1440
1441static int exclusive_bit(int new_bit)
1442{
1443 /*
1444 * USED_IN
1445 * USED_IN_READ
1446 * ENABLED
1447 * ENABLED_READ
1448 *
1449 * bit 0 - write/read
1450 * bit 1 - used_in/enabled
1451 * bit 2+ state
1452 */
1453
1454 int state = new_bit & ~3;
1455 int dir = new_bit & 2;
1456
1457 /*
1458 * keep state, bit flip the direction and strip read.
1459 */
1460 return state | (dir ^ 2);
1461}
1462
1463static int check_irq_usage(struct task_struct *curr, struct held_lock *prev,
1464 struct held_lock *next, enum lock_usage_bit bit)
Peter Zijlstra8e182572007-07-19 01:48:54 -07001465{
1466 /*
1467 * Prove that the new dependency does not connect a hardirq-safe
1468 * lock with a hardirq-unsafe lock - to achieve this we search
1469 * the backwards-subgraph starting at <prev>, and the
1470 * forwards-subgraph starting at <next>:
1471 */
Peter Zijlstra4f367d8a2009-01-22 18:10:42 +01001472 if (!check_usage(curr, prev, next, bit,
1473 exclusive_bit(bit), state_name(bit)))
Peter Zijlstra8e182572007-07-19 01:48:54 -07001474 return 0;
1475
Peter Zijlstra4f367d8a2009-01-22 18:10:42 +01001476 bit++; /* _READ */
1477
Peter Zijlstra8e182572007-07-19 01:48:54 -07001478 /*
1479 * Prove that the new dependency does not connect a hardirq-safe-read
1480 * lock with a hardirq-unsafe lock - to achieve this we search
1481 * the backwards-subgraph starting at <prev>, and the
1482 * forwards-subgraph starting at <next>:
1483 */
Peter Zijlstra4f367d8a2009-01-22 18:10:42 +01001484 if (!check_usage(curr, prev, next, bit,
1485 exclusive_bit(bit), state_name(bit)))
Peter Zijlstra8e182572007-07-19 01:48:54 -07001486 return 0;
1487
Peter Zijlstra4f367d8a2009-01-22 18:10:42 +01001488 return 1;
1489}
Peter Zijlstra8e182572007-07-19 01:48:54 -07001490
Peter Zijlstra4f367d8a2009-01-22 18:10:42 +01001491static int
1492check_prev_add_irq(struct task_struct *curr, struct held_lock *prev,
1493 struct held_lock *next)
1494{
1495#define LOCKDEP_STATE(__STATE) \
1496 if (!check_irq_usage(curr, prev, next, LOCK_USED_IN_##__STATE)) \
Nick Piggincf40bd12009-01-21 08:12:39 +01001497 return 0;
Peter Zijlstra4f367d8a2009-01-22 18:10:42 +01001498#include "lockdep_states.h"
1499#undef LOCKDEP_STATE
Nick Piggincf40bd12009-01-21 08:12:39 +01001500
Peter Zijlstra8e182572007-07-19 01:48:54 -07001501 return 1;
1502}
1503
1504static void inc_chains(void)
1505{
1506 if (current->hardirq_context)
1507 nr_hardirq_chains++;
1508 else {
1509 if (current->softirq_context)
1510 nr_softirq_chains++;
1511 else
1512 nr_process_chains++;
1513 }
1514}
1515
1516#else
1517
1518static inline int
1519check_prev_add_irq(struct task_struct *curr, struct held_lock *prev,
1520 struct held_lock *next)
1521{
1522 return 1;
1523}
1524
1525static inline void inc_chains(void)
1526{
1527 nr_process_chains++;
1528}
1529
1530#endif
1531
1532static int
1533print_deadlock_bug(struct task_struct *curr, struct held_lock *prev,
1534 struct held_lock *next)
1535{
1536 if (!debug_locks_off_graph_unlock() || debug_locks_silent)
1537 return 0;
1538
1539 printk("\n=============================================\n");
1540 printk( "[ INFO: possible recursive locking detected ]\n");
1541 print_kernel_version();
1542 printk( "---------------------------------------------\n");
1543 printk("%s/%d is trying to acquire lock:\n",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07001544 curr->comm, task_pid_nr(curr));
Peter Zijlstra8e182572007-07-19 01:48:54 -07001545 print_lock(next);
1546 printk("\nbut task is already holding lock:\n");
1547 print_lock(prev);
1548
1549 printk("\nother info that might help us debug this:\n");
1550 lockdep_print_held_locks(curr);
1551
1552 printk("\nstack backtrace:\n");
1553 dump_stack();
1554
1555 return 0;
1556}
1557
1558/*
1559 * Check whether we are holding such a class already.
1560 *
1561 * (Note that this has to be done separately, because the graph cannot
1562 * detect such classes of deadlocks.)
1563 *
1564 * Returns: 0 on deadlock detected, 1 on OK, 2 on recursive read
1565 */
1566static int
1567check_deadlock(struct task_struct *curr, struct held_lock *next,
1568 struct lockdep_map *next_instance, int read)
1569{
1570 struct held_lock *prev;
Peter Zijlstra7531e2f2008-08-11 09:30:24 +02001571 struct held_lock *nest = NULL;
Peter Zijlstra8e182572007-07-19 01:48:54 -07001572 int i;
1573
1574 for (i = 0; i < curr->lockdep_depth; i++) {
1575 prev = curr->held_locks + i;
Peter Zijlstra7531e2f2008-08-11 09:30:24 +02001576
1577 if (prev->instance == next->nest_lock)
1578 nest = prev;
1579
Dave Jonesf82b2172008-08-11 09:30:23 +02001580 if (hlock_class(prev) != hlock_class(next))
Peter Zijlstra8e182572007-07-19 01:48:54 -07001581 continue;
Peter Zijlstra7531e2f2008-08-11 09:30:24 +02001582
Peter Zijlstra8e182572007-07-19 01:48:54 -07001583 /*
1584 * Allow read-after-read recursion of the same
1585 * lock class (i.e. read_lock(lock)+read_lock(lock)):
1586 */
1587 if ((read == 2) && prev->read)
1588 return 2;
Peter Zijlstra7531e2f2008-08-11 09:30:24 +02001589
1590 /*
1591 * We're holding the nest_lock, which serializes this lock's
1592 * nesting behaviour.
1593 */
1594 if (nest)
1595 return 2;
1596
Peter Zijlstra8e182572007-07-19 01:48:54 -07001597 return print_deadlock_bug(curr, prev, next);
1598 }
1599 return 1;
1600}
1601
1602/*
1603 * There was a chain-cache miss, and we are about to add a new dependency
1604 * to a previous lock. We recursively validate the following rules:
1605 *
1606 * - would the adding of the <prev> -> <next> dependency create a
1607 * circular dependency in the graph? [== circular deadlock]
1608 *
1609 * - does the new prev->next dependency connect any hardirq-safe lock
1610 * (in the full backwards-subgraph starting at <prev>) with any
1611 * hardirq-unsafe lock (in the full forwards-subgraph starting at
1612 * <next>)? [== illegal lock inversion with hardirq contexts]
1613 *
1614 * - does the new prev->next dependency connect any softirq-safe lock
1615 * (in the full backwards-subgraph starting at <prev>) with any
1616 * softirq-unsafe lock (in the full forwards-subgraph starting at
1617 * <next>)? [== illegal lock inversion with softirq contexts]
1618 *
1619 * any of these scenarios could lead to a deadlock.
1620 *
1621 * Then if all the validations pass, we add the forwards and backwards
1622 * dependency.
1623 */
1624static int
1625check_prev_add(struct task_struct *curr, struct held_lock *prev,
Yong Zhang4726f2a2010-05-04 14:16:48 +08001626 struct held_lock *next, int distance, int trylock_loop)
Peter Zijlstra8e182572007-07-19 01:48:54 -07001627{
1628 struct lock_list *entry;
1629 int ret;
Ming Leidb0002a2009-07-16 15:44:29 +02001630 struct lock_list this;
1631 struct lock_list *uninitialized_var(target_entry);
Yong Zhang4726f2a2010-05-04 14:16:48 +08001632 /*
1633 * Static variable, serialized by the graph_lock().
1634 *
1635 * We use this static variable to save the stack trace in case
1636 * we call into this function multiple times due to encountering
1637 * trylocks in the held lock stack.
1638 */
1639 static struct stack_trace trace;
Peter Zijlstra8e182572007-07-19 01:48:54 -07001640
1641 /*
1642 * Prove that the new <prev> -> <next> dependency would not
1643 * create a circular dependency in the graph. (We do this by
1644 * forward-recursing into the graph starting at <next>, and
1645 * checking whether we can reach <prev>.)
1646 *
1647 * We are using global variables to control the recursion, to
1648 * keep the stackframe size of the recursive functions low:
1649 */
Ming Leidb0002a2009-07-16 15:44:29 +02001650 this.class = hlock_class(next);
1651 this.parent = NULL;
1652 ret = check_noncircular(&this, hlock_class(prev), &target_entry);
1653 if (unlikely(!ret))
1654 return print_circular_bug(&this, target_entry, next, prev);
1655 else if (unlikely(ret < 0))
1656 return print_bfs_bug(ret);
Ming Leic94aa5c2009-07-16 15:44:29 +02001657
Peter Zijlstra8e182572007-07-19 01:48:54 -07001658 if (!check_prev_add_irq(curr, prev, next))
1659 return 0;
1660
1661 /*
1662 * For recursive read-locks we do all the dependency checks,
1663 * but we dont store read-triggered dependencies (only
1664 * write-triggered dependencies). This ensures that only the
1665 * write-side dependencies matter, and that if for example a
1666 * write-lock never takes any other locks, then the reads are
1667 * equivalent to a NOP.
1668 */
1669 if (next->read == 2 || prev->read == 2)
1670 return 1;
1671 /*
1672 * Is the <prev> -> <next> dependency already present?
1673 *
1674 * (this may occur even though this is a new chain: consider
1675 * e.g. the L1 -> L2 -> L3 -> L4 and the L5 -> L1 -> L2 -> L3
1676 * chains - the second one will be new, but L1 already has
1677 * L2 added to its dependency list, due to the first chain.)
1678 */
Dave Jonesf82b2172008-08-11 09:30:23 +02001679 list_for_each_entry(entry, &hlock_class(prev)->locks_after, entry) {
1680 if (entry->class == hlock_class(next)) {
Peter Zijlstra8e182572007-07-19 01:48:54 -07001681 if (distance == 1)
1682 entry->distance = 1;
1683 return 2;
1684 }
1685 }
1686
Yong Zhang4726f2a2010-05-04 14:16:48 +08001687 if (!trylock_loop && !save_trace(&trace))
1688 return 0;
1689
Peter Zijlstra8e182572007-07-19 01:48:54 -07001690 /*
1691 * Ok, all validations passed, add the new lock
1692 * to the previous lock's dependency list:
1693 */
Dave Jonesf82b2172008-08-11 09:30:23 +02001694 ret = add_lock_to_list(hlock_class(prev), hlock_class(next),
1695 &hlock_class(prev)->locks_after,
Yong Zhang4726f2a2010-05-04 14:16:48 +08001696 next->acquire_ip, distance, &trace);
Peter Zijlstra8e182572007-07-19 01:48:54 -07001697
1698 if (!ret)
1699 return 0;
1700
Dave Jonesf82b2172008-08-11 09:30:23 +02001701 ret = add_lock_to_list(hlock_class(next), hlock_class(prev),
1702 &hlock_class(next)->locks_before,
Yong Zhang4726f2a2010-05-04 14:16:48 +08001703 next->acquire_ip, distance, &trace);
Peter Zijlstra8e182572007-07-19 01:48:54 -07001704 if (!ret)
1705 return 0;
1706
1707 /*
1708 * Debugging printouts:
1709 */
Dave Jonesf82b2172008-08-11 09:30:23 +02001710 if (verbose(hlock_class(prev)) || verbose(hlock_class(next))) {
Peter Zijlstra8e182572007-07-19 01:48:54 -07001711 graph_unlock();
1712 printk("\n new dependency: ");
Dave Jonesf82b2172008-08-11 09:30:23 +02001713 print_lock_name(hlock_class(prev));
Peter Zijlstra8e182572007-07-19 01:48:54 -07001714 printk(" => ");
Dave Jonesf82b2172008-08-11 09:30:23 +02001715 print_lock_name(hlock_class(next));
Peter Zijlstra8e182572007-07-19 01:48:54 -07001716 printk("\n");
1717 dump_stack();
1718 return graph_lock();
1719 }
1720 return 1;
1721}
1722
1723/*
1724 * Add the dependency to all directly-previous locks that are 'relevant'.
1725 * The ones that are relevant are (in increasing distance from curr):
1726 * all consecutive trylock entries and the final non-trylock entry - or
1727 * the end of this context's lock-chain - whichever comes first.
1728 */
1729static int
1730check_prevs_add(struct task_struct *curr, struct held_lock *next)
1731{
1732 int depth = curr->lockdep_depth;
Yong Zhang4726f2a2010-05-04 14:16:48 +08001733 int trylock_loop = 0;
Peter Zijlstra8e182572007-07-19 01:48:54 -07001734 struct held_lock *hlock;
1735
1736 /*
1737 * Debugging checks.
1738 *
1739 * Depth must not be zero for a non-head lock:
1740 */
1741 if (!depth)
1742 goto out_bug;
1743 /*
1744 * At least two relevant locks must exist for this
1745 * to be a head:
1746 */
1747 if (curr->held_locks[depth].irq_context !=
1748 curr->held_locks[depth-1].irq_context)
1749 goto out_bug;
1750
1751 for (;;) {
1752 int distance = curr->lockdep_depth - depth + 1;
1753 hlock = curr->held_locks + depth-1;
1754 /*
1755 * Only non-recursive-read entries get new dependencies
1756 * added:
1757 */
1758 if (hlock->read != 2) {
Yong Zhang4726f2a2010-05-04 14:16:48 +08001759 if (!check_prev_add(curr, hlock, next,
1760 distance, trylock_loop))
Peter Zijlstra8e182572007-07-19 01:48:54 -07001761 return 0;
1762 /*
1763 * Stop after the first non-trylock entry,
1764 * as non-trylock entries have added their
1765 * own direct dependencies already, so this
1766 * lock is connected to them indirectly:
1767 */
1768 if (!hlock->trylock)
1769 break;
1770 }
1771 depth--;
1772 /*
1773 * End of lock-stack?
1774 */
1775 if (!depth)
1776 break;
1777 /*
1778 * Stop the search if we cross into another context:
1779 */
1780 if (curr->held_locks[depth].irq_context !=
1781 curr->held_locks[depth-1].irq_context)
1782 break;
Yong Zhang4726f2a2010-05-04 14:16:48 +08001783 trylock_loop = 1;
Peter Zijlstra8e182572007-07-19 01:48:54 -07001784 }
1785 return 1;
1786out_bug:
1787 if (!debug_locks_off_graph_unlock())
1788 return 0;
1789
1790 WARN_ON(1);
1791
1792 return 0;
1793}
1794
1795unsigned long nr_lock_chains;
Huang, Ying443cd502008-06-20 16:39:21 +08001796struct lock_chain lock_chains[MAX_LOCKDEP_CHAINS];
Huang, Yingcd1a28e2008-06-23 11:20:54 +08001797int nr_chain_hlocks;
Huang, Ying443cd502008-06-20 16:39:21 +08001798static u16 chain_hlocks[MAX_LOCKDEP_CHAIN_HLOCKS];
1799
1800struct lock_class *lock_chain_get_class(struct lock_chain *chain, int i)
1801{
1802 return lock_classes + chain_hlocks[chain->base + i];
1803}
Peter Zijlstra8e182572007-07-19 01:48:54 -07001804
1805/*
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001806 * Look up a dependency chain. If the key is not present yet then
Jarek Poplawski9e860d02007-05-08 00:30:12 -07001807 * add it and return 1 - in this case the new dependency chain is
1808 * validated. If the key is already hashed, return 0.
1809 * (On return with 1 graph_lock is held.)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001810 */
Huang, Ying443cd502008-06-20 16:39:21 +08001811static inline int lookup_chain_cache(struct task_struct *curr,
1812 struct held_lock *hlock,
1813 u64 chain_key)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001814{
Dave Jonesf82b2172008-08-11 09:30:23 +02001815 struct lock_class *class = hlock_class(hlock);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001816 struct list_head *hash_head = chainhashentry(chain_key);
1817 struct lock_chain *chain;
Huang, Ying443cd502008-06-20 16:39:21 +08001818 struct held_lock *hlock_curr, *hlock_next;
Huang, Yingcd1a28e2008-06-23 11:20:54 +08001819 int i, j, n, cn;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001820
Jarek Poplawski381a2292007-02-10 01:44:58 -08001821 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
1822 return 0;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001823 /*
1824 * We can walk it lock-free, because entries only get added
1825 * to the hash:
1826 */
1827 list_for_each_entry(chain, hash_head, entry) {
1828 if (chain->chain_key == chain_key) {
1829cache_hit:
Frederic Weisbeckerbd6d29c2010-04-06 00:10:17 +02001830 debug_atomic_inc(chain_lookup_hits);
Ingo Molnar81fc6852006-12-13 00:34:40 -08001831 if (very_verbose(class))
Andrew Morton755cd902006-12-29 16:49:14 -08001832 printk("\nhash chain already cached, key: "
1833 "%016Lx tail class: [%p] %s\n",
1834 (unsigned long long)chain_key,
1835 class->key, class->name);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001836 return 0;
1837 }
1838 }
Ingo Molnar81fc6852006-12-13 00:34:40 -08001839 if (very_verbose(class))
Andrew Morton755cd902006-12-29 16:49:14 -08001840 printk("\nnew hash chain, key: %016Lx tail class: [%p] %s\n",
1841 (unsigned long long)chain_key, class->key, class->name);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001842 /*
1843 * Allocate a new chain entry from the static array, and add
1844 * it to the hash:
1845 */
Ingo Molnar74c383f2006-12-13 00:34:43 -08001846 if (!graph_lock())
1847 return 0;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001848 /*
1849 * We have to walk the chain again locked - to avoid duplicates:
1850 */
1851 list_for_each_entry(chain, hash_head, entry) {
1852 if (chain->chain_key == chain_key) {
Ingo Molnar74c383f2006-12-13 00:34:43 -08001853 graph_unlock();
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001854 goto cache_hit;
1855 }
1856 }
1857 if (unlikely(nr_lock_chains >= MAX_LOCKDEP_CHAINS)) {
Ingo Molnar74c383f2006-12-13 00:34:43 -08001858 if (!debug_locks_off_graph_unlock())
1859 return 0;
1860
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001861 printk("BUG: MAX_LOCKDEP_CHAINS too low!\n");
1862 printk("turning off the locking correctness validator.\n");
Peter Zijlstraeedeeab2009-03-18 12:38:47 +01001863 dump_stack();
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001864 return 0;
1865 }
1866 chain = lock_chains + nr_lock_chains++;
1867 chain->chain_key = chain_key;
Huang, Ying443cd502008-06-20 16:39:21 +08001868 chain->irq_context = hlock->irq_context;
1869 /* Find the first held_lock of current chain */
1870 hlock_next = hlock;
1871 for (i = curr->lockdep_depth - 1; i >= 0; i--) {
1872 hlock_curr = curr->held_locks + i;
1873 if (hlock_curr->irq_context != hlock_next->irq_context)
1874 break;
1875 hlock_next = hlock;
1876 }
1877 i++;
1878 chain->depth = curr->lockdep_depth + 1 - i;
Huang, Yingcd1a28e2008-06-23 11:20:54 +08001879 cn = nr_chain_hlocks;
1880 while (cn + chain->depth <= MAX_LOCKDEP_CHAIN_HLOCKS) {
1881 n = cmpxchg(&nr_chain_hlocks, cn, cn + chain->depth);
1882 if (n == cn)
1883 break;
1884 cn = n;
1885 }
1886 if (likely(cn + chain->depth <= MAX_LOCKDEP_CHAIN_HLOCKS)) {
1887 chain->base = cn;
Huang, Ying443cd502008-06-20 16:39:21 +08001888 for (j = 0; j < chain->depth - 1; j++, i++) {
Dave Jonesf82b2172008-08-11 09:30:23 +02001889 int lock_id = curr->held_locks[i].class_idx - 1;
Huang, Ying443cd502008-06-20 16:39:21 +08001890 chain_hlocks[chain->base + j] = lock_id;
1891 }
1892 chain_hlocks[chain->base + j] = class - lock_classes;
1893 }
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001894 list_add_tail_rcu(&chain->entry, hash_head);
Frederic Weisbeckerbd6d29c2010-04-06 00:10:17 +02001895 debug_atomic_inc(chain_lookup_misses);
Peter Zijlstra8e182572007-07-19 01:48:54 -07001896 inc_chains();
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001897
1898 return 1;
1899}
Peter Zijlstra8e182572007-07-19 01:48:54 -07001900
1901static int validate_chain(struct task_struct *curr, struct lockdep_map *lock,
Johannes Berg4e6045f2007-10-18 23:39:55 -07001902 struct held_lock *hlock, int chain_head, u64 chain_key)
Peter Zijlstra8e182572007-07-19 01:48:54 -07001903{
1904 /*
1905 * Trylock needs to maintain the stack of held locks, but it
1906 * does not add new dependencies, because trylock can be done
1907 * in any order.
1908 *
1909 * We look up the chain_key and do the O(N^2) check and update of
1910 * the dependencies only if this is a new dependency chain.
1911 * (If lookup_chain_cache() returns with 1 it acquires
1912 * graph_lock for us)
1913 */
1914 if (!hlock->trylock && (hlock->check == 2) &&
Huang, Ying443cd502008-06-20 16:39:21 +08001915 lookup_chain_cache(curr, hlock, chain_key)) {
Peter Zijlstra8e182572007-07-19 01:48:54 -07001916 /*
1917 * Check whether last held lock:
1918 *
1919 * - is irq-safe, if this lock is irq-unsafe
1920 * - is softirq-safe, if this lock is hardirq-unsafe
1921 *
1922 * And check whether the new lock's dependency graph
1923 * could lead back to the previous lock.
1924 *
1925 * any of these scenarios could lead to a deadlock. If
1926 * All validations
1927 */
1928 int ret = check_deadlock(curr, hlock, lock, hlock->read);
1929
1930 if (!ret)
1931 return 0;
1932 /*
1933 * Mark recursive read, as we jump over it when
1934 * building dependencies (just like we jump over
1935 * trylock entries):
1936 */
1937 if (ret == 2)
1938 hlock->read = 2;
1939 /*
1940 * Add dependency only if this lock is not the head
1941 * of the chain, and if it's not a secondary read-lock:
1942 */
1943 if (!chain_head && ret != 2)
1944 if (!check_prevs_add(curr, hlock))
1945 return 0;
1946 graph_unlock();
1947 } else
1948 /* after lookup_chain_cache(): */
1949 if (unlikely(!debug_locks))
1950 return 0;
1951
1952 return 1;
1953}
1954#else
1955static inline int validate_chain(struct task_struct *curr,
1956 struct lockdep_map *lock, struct held_lock *hlock,
Gregory Haskins3aa416b2007-10-11 22:11:11 +02001957 int chain_head, u64 chain_key)
Peter Zijlstra8e182572007-07-19 01:48:54 -07001958{
1959 return 1;
1960}
Peter Zijlstraca58abc2007-07-19 01:48:53 -07001961#endif
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001962
1963/*
1964 * We are building curr_chain_key incrementally, so double-check
1965 * it from scratch, to make sure that it's done correctly:
1966 */
Steven Rostedt1d09daa2008-05-12 21:20:55 +02001967static void check_chain_key(struct task_struct *curr)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001968{
1969#ifdef CONFIG_DEBUG_LOCKDEP
1970 struct held_lock *hlock, *prev_hlock = NULL;
1971 unsigned int i, id;
1972 u64 chain_key = 0;
1973
1974 for (i = 0; i < curr->lockdep_depth; i++) {
1975 hlock = curr->held_locks + i;
1976 if (chain_key != hlock->prev_chain_key) {
1977 debug_locks_off();
Arjan van de Ven2df8b1d2008-07-30 12:43:11 -07001978 WARN(1, "hm#1, depth: %u [%u], %016Lx != %016Lx\n",
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001979 curr->lockdep_depth, i,
1980 (unsigned long long)chain_key,
1981 (unsigned long long)hlock->prev_chain_key);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001982 return;
1983 }
Dave Jonesf82b2172008-08-11 09:30:23 +02001984 id = hlock->class_idx - 1;
Jarek Poplawski381a2292007-02-10 01:44:58 -08001985 if (DEBUG_LOCKS_WARN_ON(id >= MAX_LOCKDEP_KEYS))
1986 return;
1987
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001988 if (prev_hlock && (prev_hlock->irq_context !=
1989 hlock->irq_context))
1990 chain_key = 0;
1991 chain_key = iterate_chain_key(chain_key, id);
1992 prev_hlock = hlock;
1993 }
1994 if (chain_key != curr->curr_chain_key) {
1995 debug_locks_off();
Arjan van de Ven2df8b1d2008-07-30 12:43:11 -07001996 WARN(1, "hm#2, depth: %u [%u], %016Lx != %016Lx\n",
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001997 curr->lockdep_depth, i,
1998 (unsigned long long)chain_key,
1999 (unsigned long long)curr->curr_chain_key);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002000 }
2001#endif
2002}
2003
Peter Zijlstra8e182572007-07-19 01:48:54 -07002004static int
2005print_usage_bug(struct task_struct *curr, struct held_lock *this,
2006 enum lock_usage_bit prev_bit, enum lock_usage_bit new_bit)
2007{
2008 if (!debug_locks_off_graph_unlock() || debug_locks_silent)
2009 return 0;
2010
2011 printk("\n=================================\n");
2012 printk( "[ INFO: inconsistent lock state ]\n");
2013 print_kernel_version();
2014 printk( "---------------------------------\n");
2015
2016 printk("inconsistent {%s} -> {%s} usage.\n",
2017 usage_str[prev_bit], usage_str[new_bit]);
2018
2019 printk("%s/%d [HC%u[%lu]:SC%u[%lu]:HE%u:SE%u] takes:\n",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07002020 curr->comm, task_pid_nr(curr),
Peter Zijlstra8e182572007-07-19 01:48:54 -07002021 trace_hardirq_context(curr), hardirq_count() >> HARDIRQ_SHIFT,
2022 trace_softirq_context(curr), softirq_count() >> SOFTIRQ_SHIFT,
2023 trace_hardirqs_enabled(curr),
2024 trace_softirqs_enabled(curr));
2025 print_lock(this);
2026
2027 printk("{%s} state was registered at:\n", usage_str[prev_bit]);
Dave Jonesf82b2172008-08-11 09:30:23 +02002028 print_stack_trace(hlock_class(this)->usage_traces + prev_bit, 1);
Peter Zijlstra8e182572007-07-19 01:48:54 -07002029
2030 print_irqtrace_events(curr);
2031 printk("\nother info that might help us debug this:\n");
2032 lockdep_print_held_locks(curr);
2033
2034 printk("\nstack backtrace:\n");
2035 dump_stack();
2036
2037 return 0;
2038}
2039
2040/*
2041 * Print out an error if an invalid bit is set:
2042 */
2043static inline int
2044valid_state(struct task_struct *curr, struct held_lock *this,
2045 enum lock_usage_bit new_bit, enum lock_usage_bit bad_bit)
2046{
Dave Jonesf82b2172008-08-11 09:30:23 +02002047 if (unlikely(hlock_class(this)->usage_mask & (1 << bad_bit)))
Peter Zijlstra8e182572007-07-19 01:48:54 -07002048 return print_usage_bug(curr, this, bad_bit, new_bit);
2049 return 1;
2050}
2051
2052static int mark_lock(struct task_struct *curr, struct held_lock *this,
2053 enum lock_usage_bit new_bit);
2054
Steven Rostedt81d68a92008-05-12 21:20:42 +02002055#if defined(CONFIG_TRACE_IRQFLAGS) && defined(CONFIG_PROVE_LOCKING)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002056
2057/*
2058 * print irq inversion bug:
2059 */
2060static int
Ming Lei24208ca2009-07-16 15:44:29 +02002061print_irq_inversion_bug(struct task_struct *curr,
2062 struct lock_list *root, struct lock_list *other,
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002063 struct held_lock *this, int forwards,
2064 const char *irqclass)
2065{
Ingo Molnar74c383f2006-12-13 00:34:43 -08002066 if (!debug_locks_off_graph_unlock() || debug_locks_silent)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002067 return 0;
2068
2069 printk("\n=========================================================\n");
2070 printk( "[ INFO: possible irq lock inversion dependency detected ]\n");
Dave Jones99de0552006-09-29 02:00:10 -07002071 print_kernel_version();
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002072 printk( "---------------------------------------------------------\n");
2073 printk("%s/%d just changed the state of lock:\n",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07002074 curr->comm, task_pid_nr(curr));
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002075 print_lock(this);
2076 if (forwards)
Peter Zijlstra26575e22009-03-04 14:53:24 +01002077 printk("but this lock took another, %s-unsafe lock in the past:\n", irqclass);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002078 else
Peter Zijlstra26575e22009-03-04 14:53:24 +01002079 printk("but this lock was taken by another, %s-safe lock in the past:\n", irqclass);
Ming Lei24208ca2009-07-16 15:44:29 +02002080 print_lock_name(other->class);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002081 printk("\n\nand interrupts could create inverse lock ordering between them.\n\n");
2082
2083 printk("\nother info that might help us debug this:\n");
2084 lockdep_print_held_locks(curr);
2085
Ming Lei24208ca2009-07-16 15:44:29 +02002086 printk("\nthe shortest dependencies between 2nd lock and 1st lock:\n");
2087 if (!save_trace(&root->trace))
2088 return 0;
2089 print_shortest_lock_dependencies(other, root);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002090
2091 printk("\nstack backtrace:\n");
2092 dump_stack();
2093
2094 return 0;
2095}
2096
2097/*
2098 * Prove that in the forwards-direction subgraph starting at <this>
2099 * there is no lock matching <mask>:
2100 */
2101static int
2102check_usage_forwards(struct task_struct *curr, struct held_lock *this,
2103 enum lock_usage_bit bit, const char *irqclass)
2104{
2105 int ret;
Ming Leid7aaba12009-07-16 15:44:29 +02002106 struct lock_list root;
2107 struct lock_list *uninitialized_var(target_entry);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002108
Ming Leid7aaba12009-07-16 15:44:29 +02002109 root.parent = NULL;
2110 root.class = hlock_class(this);
2111 ret = find_usage_forwards(&root, bit, &target_entry);
Peter Zijlstraaf012962009-07-16 15:44:29 +02002112 if (ret < 0)
2113 return print_bfs_bug(ret);
2114 if (ret == 1)
2115 return ret;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002116
Ming Lei24208ca2009-07-16 15:44:29 +02002117 return print_irq_inversion_bug(curr, &root, target_entry,
Ming Leid7aaba12009-07-16 15:44:29 +02002118 this, 1, irqclass);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002119}
2120
2121/*
2122 * Prove that in the backwards-direction subgraph starting at <this>
2123 * there is no lock matching <mask>:
2124 */
2125static int
2126check_usage_backwards(struct task_struct *curr, struct held_lock *this,
2127 enum lock_usage_bit bit, const char *irqclass)
2128{
2129 int ret;
Ming Leid7aaba12009-07-16 15:44:29 +02002130 struct lock_list root;
2131 struct lock_list *uninitialized_var(target_entry);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002132
Ming Leid7aaba12009-07-16 15:44:29 +02002133 root.parent = NULL;
2134 root.class = hlock_class(this);
2135 ret = find_usage_backwards(&root, bit, &target_entry);
Peter Zijlstraaf012962009-07-16 15:44:29 +02002136 if (ret < 0)
2137 return print_bfs_bug(ret);
2138 if (ret == 1)
2139 return ret;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002140
Ming Lei24208ca2009-07-16 15:44:29 +02002141 return print_irq_inversion_bug(curr, &root, target_entry,
Oleg Nesterov48d50672010-01-26 19:16:41 +01002142 this, 0, irqclass);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002143}
2144
Ingo Molnar3117df02006-12-13 00:34:43 -08002145void print_irqtrace_events(struct task_struct *curr)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002146{
2147 printk("irq event stamp: %u\n", curr->irq_events);
2148 printk("hardirqs last enabled at (%u): ", curr->hardirq_enable_event);
2149 print_ip_sym(curr->hardirq_enable_ip);
2150 printk("hardirqs last disabled at (%u): ", curr->hardirq_disable_event);
2151 print_ip_sym(curr->hardirq_disable_ip);
2152 printk("softirqs last enabled at (%u): ", curr->softirq_enable_event);
2153 print_ip_sym(curr->softirq_enable_ip);
2154 printk("softirqs last disabled at (%u): ", curr->softirq_disable_event);
2155 print_ip_sym(curr->softirq_disable_ip);
2156}
2157
Peter Zijlstracd953022009-01-22 16:38:21 +01002158static int HARDIRQ_verbose(struct lock_class *class)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002159{
Peter Zijlstra8e182572007-07-19 01:48:54 -07002160#if HARDIRQ_VERBOSE
2161 return class_filter(class);
2162#endif
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002163 return 0;
2164}
2165
Peter Zijlstracd953022009-01-22 16:38:21 +01002166static int SOFTIRQ_verbose(struct lock_class *class)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002167{
Peter Zijlstra8e182572007-07-19 01:48:54 -07002168#if SOFTIRQ_VERBOSE
2169 return class_filter(class);
2170#endif
2171 return 0;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002172}
2173
Peter Zijlstracd953022009-01-22 16:38:21 +01002174static int RECLAIM_FS_verbose(struct lock_class *class)
Nick Piggincf40bd12009-01-21 08:12:39 +01002175{
2176#if RECLAIM_VERBOSE
2177 return class_filter(class);
2178#endif
2179 return 0;
2180}
2181
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002182#define STRICT_READ_CHECKS 1
2183
Peter Zijlstracd953022009-01-22 16:38:21 +01002184static int (*state_verbose_f[])(struct lock_class *class) = {
2185#define LOCKDEP_STATE(__STATE) \
2186 __STATE##_verbose,
2187#include "lockdep_states.h"
2188#undef LOCKDEP_STATE
2189};
2190
2191static inline int state_verbose(enum lock_usage_bit bit,
2192 struct lock_class *class)
2193{
2194 return state_verbose_f[bit >> 2](class);
2195}
2196
Peter Zijlstra42c50d52009-01-22 16:58:16 +01002197typedef int (*check_usage_f)(struct task_struct *, struct held_lock *,
2198 enum lock_usage_bit bit, const char *name);
2199
Peter Zijlstra6a6904d2009-01-22 16:07:44 +01002200static int
Peter Zijlstra1c21f142009-03-04 13:51:13 +01002201mark_lock_irq(struct task_struct *curr, struct held_lock *this,
2202 enum lock_usage_bit new_bit)
Peter Zijlstra6a6904d2009-01-22 16:07:44 +01002203{
Peter Zijlstraf9892092009-01-22 16:09:59 +01002204 int excl_bit = exclusive_bit(new_bit);
Peter Zijlstra9d3651a2009-01-22 17:18:32 +01002205 int read = new_bit & 1;
Peter Zijlstra42c50d52009-01-22 16:58:16 +01002206 int dir = new_bit & 2;
2207
Peter Zijlstra38aa2712009-01-27 14:53:50 +01002208 /*
2209 * mark USED_IN has to look forwards -- to ensure no dependency
2210 * has ENABLED state, which would allow recursion deadlocks.
2211 *
2212 * mark ENABLED has to look backwards -- to ensure no dependee
2213 * has USED_IN state, which, again, would allow recursion deadlocks.
2214 */
Peter Zijlstra42c50d52009-01-22 16:58:16 +01002215 check_usage_f usage = dir ?
2216 check_usage_backwards : check_usage_forwards;
Peter Zijlstraf9892092009-01-22 16:09:59 +01002217
Peter Zijlstra38aa2712009-01-27 14:53:50 +01002218 /*
2219 * Validate that this particular lock does not have conflicting
2220 * usage states.
2221 */
Peter Zijlstra6a6904d2009-01-22 16:07:44 +01002222 if (!valid_state(curr, this, new_bit, excl_bit))
2223 return 0;
Peter Zijlstra9d3651a2009-01-22 17:18:32 +01002224
Peter Zijlstra38aa2712009-01-27 14:53:50 +01002225 /*
2226 * Validate that the lock dependencies don't have conflicting usage
2227 * states.
2228 */
2229 if ((!read || !dir || STRICT_READ_CHECKS) &&
Peter Zijlstra1c21f142009-03-04 13:51:13 +01002230 !usage(curr, this, excl_bit, state_name(new_bit & ~1)))
Peter Zijlstra6a6904d2009-01-22 16:07:44 +01002231 return 0;
Peter Zijlstra780e8202009-01-22 16:51:29 +01002232
Peter Zijlstra38aa2712009-01-27 14:53:50 +01002233 /*
2234 * Check for read in write conflicts
2235 */
2236 if (!read) {
2237 if (!valid_state(curr, this, new_bit, excl_bit + 1))
2238 return 0;
2239
2240 if (STRICT_READ_CHECKS &&
Peter Zijlstra4f367d8a2009-01-22 18:10:42 +01002241 !usage(curr, this, excl_bit + 1,
2242 state_name(new_bit + 1)))
Peter Zijlstra38aa2712009-01-27 14:53:50 +01002243 return 0;
2244 }
Peter Zijlstra780e8202009-01-22 16:51:29 +01002245
Peter Zijlstracd953022009-01-22 16:38:21 +01002246 if (state_verbose(new_bit, hlock_class(this)))
Peter Zijlstra6a6904d2009-01-22 16:07:44 +01002247 return 2;
2248
2249 return 1;
2250}
2251
Nick Piggincf40bd12009-01-21 08:12:39 +01002252enum mark_type {
Peter Zijlstra36bfb9b2009-01-22 14:12:41 +01002253#define LOCKDEP_STATE(__STATE) __STATE,
2254#include "lockdep_states.h"
2255#undef LOCKDEP_STATE
Nick Piggincf40bd12009-01-21 08:12:39 +01002256};
2257
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002258/*
2259 * Mark all held locks with a usage bit:
2260 */
Steven Rostedt1d09daa2008-05-12 21:20:55 +02002261static int
Nick Piggincf40bd12009-01-21 08:12:39 +01002262mark_held_locks(struct task_struct *curr, enum mark_type mark)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002263{
2264 enum lock_usage_bit usage_bit;
2265 struct held_lock *hlock;
2266 int i;
2267
2268 for (i = 0; i < curr->lockdep_depth; i++) {
2269 hlock = curr->held_locks + i;
2270
Peter Zijlstracf2ad4d2009-01-27 13:58:08 +01002271 usage_bit = 2 + (mark << 2); /* ENABLED */
2272 if (hlock->read)
2273 usage_bit += 1; /* READ */
2274
2275 BUG_ON(usage_bit >= LOCK_USAGE_STATES);
Nick Piggincf40bd12009-01-21 08:12:39 +01002276
Jarek Poplawski4ff773bb2007-05-08 00:31:00 -07002277 if (!mark_lock(curr, hlock, usage_bit))
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002278 return 0;
2279 }
2280
2281 return 1;
2282}
2283
2284/*
2285 * Debugging helper: via this flag we know that we are in
2286 * 'early bootup code', and will warn about any invalid irqs-on event:
2287 */
2288static int early_boot_irqs_enabled;
2289
2290void early_boot_irqs_off(void)
2291{
2292 early_boot_irqs_enabled = 0;
2293}
2294
2295void early_boot_irqs_on(void)
2296{
2297 early_boot_irqs_enabled = 1;
2298}
2299
2300/*
2301 * Hardirqs will be enabled:
2302 */
Heiko Carstens6afe40b2008-10-28 11:14:58 +01002303void trace_hardirqs_on_caller(unsigned long ip)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002304{
2305 struct task_struct *curr = current;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002306
Heiko Carstens6afe40b2008-10-28 11:14:58 +01002307 time_hardirqs_on(CALLER_ADDR0, ip);
Steven Rostedt81d68a92008-05-12 21:20:42 +02002308
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002309 if (unlikely(!debug_locks || current->lockdep_recursion))
2310 return;
2311
2312 if (DEBUG_LOCKS_WARN_ON(unlikely(!early_boot_irqs_enabled)))
2313 return;
2314
2315 if (unlikely(curr->hardirqs_enabled)) {
Frederic Weisbecker8795d772010-04-15 23:10:42 +02002316 /*
2317 * Neither irq nor preemption are disabled here
2318 * so this is racy by nature but loosing one hit
2319 * in a stat is not a big deal.
2320 */
Frederic Weisbeckerba697f42010-05-04 04:47:25 +02002321 __debug_atomic_inc(redundant_hardirqs_on);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002322 return;
2323 }
2324 /* we'll do an OFF -> ON transition: */
2325 curr->hardirqs_enabled = 1;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002326
2327 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2328 return;
2329 if (DEBUG_LOCKS_WARN_ON(current->hardirq_context))
2330 return;
2331 /*
2332 * We are going to turn hardirqs on, so set the
2333 * usage bit for all held locks:
2334 */
Nick Piggincf40bd12009-01-21 08:12:39 +01002335 if (!mark_held_locks(curr, HARDIRQ))
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002336 return;
2337 /*
2338 * If we have softirqs enabled, then set the usage
2339 * bit for all held locks. (disabled hardirqs prevented
2340 * this bit from being set before)
2341 */
2342 if (curr->softirqs_enabled)
Nick Piggincf40bd12009-01-21 08:12:39 +01002343 if (!mark_held_locks(curr, SOFTIRQ))
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002344 return;
2345
2346 curr->hardirq_enable_ip = ip;
2347 curr->hardirq_enable_event = ++curr->irq_events;
Frederic Weisbeckerbd6d29c2010-04-06 00:10:17 +02002348 debug_atomic_inc(hardirqs_on_events);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002349}
Steven Rostedt81d68a92008-05-12 21:20:42 +02002350EXPORT_SYMBOL(trace_hardirqs_on_caller);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002351
Steven Rostedt1d09daa2008-05-12 21:20:55 +02002352void trace_hardirqs_on(void)
Steven Rostedt81d68a92008-05-12 21:20:42 +02002353{
2354 trace_hardirqs_on_caller(CALLER_ADDR0);
2355}
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002356EXPORT_SYMBOL(trace_hardirqs_on);
2357
2358/*
2359 * Hardirqs were disabled:
2360 */
Heiko Carstens6afe40b2008-10-28 11:14:58 +01002361void trace_hardirqs_off_caller(unsigned long ip)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002362{
2363 struct task_struct *curr = current;
2364
Heiko Carstens6afe40b2008-10-28 11:14:58 +01002365 time_hardirqs_off(CALLER_ADDR0, ip);
Steven Rostedt81d68a92008-05-12 21:20:42 +02002366
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002367 if (unlikely(!debug_locks || current->lockdep_recursion))
2368 return;
2369
2370 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2371 return;
2372
2373 if (curr->hardirqs_enabled) {
2374 /*
2375 * We have done an ON -> OFF transition:
2376 */
2377 curr->hardirqs_enabled = 0;
Heiko Carstens6afe40b2008-10-28 11:14:58 +01002378 curr->hardirq_disable_ip = ip;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002379 curr->hardirq_disable_event = ++curr->irq_events;
Frederic Weisbeckerbd6d29c2010-04-06 00:10:17 +02002380 debug_atomic_inc(hardirqs_off_events);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002381 } else
Frederic Weisbeckerbd6d29c2010-04-06 00:10:17 +02002382 debug_atomic_inc(redundant_hardirqs_off);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002383}
Steven Rostedt81d68a92008-05-12 21:20:42 +02002384EXPORT_SYMBOL(trace_hardirqs_off_caller);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002385
Steven Rostedt1d09daa2008-05-12 21:20:55 +02002386void trace_hardirqs_off(void)
Steven Rostedt81d68a92008-05-12 21:20:42 +02002387{
2388 trace_hardirqs_off_caller(CALLER_ADDR0);
2389}
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002390EXPORT_SYMBOL(trace_hardirqs_off);
2391
2392/*
2393 * Softirqs will be enabled:
2394 */
2395void trace_softirqs_on(unsigned long ip)
2396{
2397 struct task_struct *curr = current;
2398
2399 if (unlikely(!debug_locks))
2400 return;
2401
2402 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2403 return;
2404
2405 if (curr->softirqs_enabled) {
Frederic Weisbeckerbd6d29c2010-04-06 00:10:17 +02002406 debug_atomic_inc(redundant_softirqs_on);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002407 return;
2408 }
2409
2410 /*
2411 * We'll do an OFF -> ON transition:
2412 */
2413 curr->softirqs_enabled = 1;
2414 curr->softirq_enable_ip = ip;
2415 curr->softirq_enable_event = ++curr->irq_events;
Frederic Weisbeckerbd6d29c2010-04-06 00:10:17 +02002416 debug_atomic_inc(softirqs_on_events);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002417 /*
2418 * We are going to turn softirqs on, so set the
2419 * usage bit for all held locks, if hardirqs are
2420 * enabled too:
2421 */
2422 if (curr->hardirqs_enabled)
Nick Piggincf40bd12009-01-21 08:12:39 +01002423 mark_held_locks(curr, SOFTIRQ);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002424}
2425
2426/*
2427 * Softirqs were disabled:
2428 */
2429void trace_softirqs_off(unsigned long ip)
2430{
2431 struct task_struct *curr = current;
2432
2433 if (unlikely(!debug_locks))
2434 return;
2435
2436 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2437 return;
2438
2439 if (curr->softirqs_enabled) {
2440 /*
2441 * We have done an ON -> OFF transition:
2442 */
2443 curr->softirqs_enabled = 0;
2444 curr->softirq_disable_ip = ip;
2445 curr->softirq_disable_event = ++curr->irq_events;
Frederic Weisbeckerbd6d29c2010-04-06 00:10:17 +02002446 debug_atomic_inc(softirqs_off_events);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002447 DEBUG_LOCKS_WARN_ON(!softirq_count());
2448 } else
Frederic Weisbeckerbd6d29c2010-04-06 00:10:17 +02002449 debug_atomic_inc(redundant_softirqs_off);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002450}
2451
Peter Zijlstra2f850182009-03-20 11:13:20 +01002452static void __lockdep_trace_alloc(gfp_t gfp_mask, unsigned long flags)
Nick Piggincf40bd12009-01-21 08:12:39 +01002453{
2454 struct task_struct *curr = current;
2455
2456 if (unlikely(!debug_locks))
2457 return;
2458
2459 /* no reclaim without waiting on it */
2460 if (!(gfp_mask & __GFP_WAIT))
2461 return;
2462
2463 /* this guy won't enter reclaim */
2464 if ((curr->flags & PF_MEMALLOC) && !(gfp_mask & __GFP_NOMEMALLOC))
2465 return;
2466
2467 /* We're only interested __GFP_FS allocations for now */
2468 if (!(gfp_mask & __GFP_FS))
2469 return;
2470
Peter Zijlstra2f850182009-03-20 11:13:20 +01002471 if (DEBUG_LOCKS_WARN_ON(irqs_disabled_flags(flags)))
Nick Piggincf40bd12009-01-21 08:12:39 +01002472 return;
2473
2474 mark_held_locks(curr, RECLAIM_FS);
2475}
2476
Peter Zijlstra2f850182009-03-20 11:13:20 +01002477static void check_flags(unsigned long flags);
2478
2479void lockdep_trace_alloc(gfp_t gfp_mask)
2480{
2481 unsigned long flags;
2482
2483 if (unlikely(current->lockdep_recursion))
2484 return;
2485
2486 raw_local_irq_save(flags);
2487 check_flags(flags);
2488 current->lockdep_recursion = 1;
2489 __lockdep_trace_alloc(gfp_mask, flags);
2490 current->lockdep_recursion = 0;
2491 raw_local_irq_restore(flags);
2492}
2493
Peter Zijlstra8e182572007-07-19 01:48:54 -07002494static int mark_irqflags(struct task_struct *curr, struct held_lock *hlock)
2495{
2496 /*
2497 * If non-trylock use in a hardirq or softirq context, then
2498 * mark the lock as used in these contexts:
2499 */
2500 if (!hlock->trylock) {
2501 if (hlock->read) {
2502 if (curr->hardirq_context)
2503 if (!mark_lock(curr, hlock,
2504 LOCK_USED_IN_HARDIRQ_READ))
2505 return 0;
2506 if (curr->softirq_context)
2507 if (!mark_lock(curr, hlock,
2508 LOCK_USED_IN_SOFTIRQ_READ))
2509 return 0;
2510 } else {
2511 if (curr->hardirq_context)
2512 if (!mark_lock(curr, hlock, LOCK_USED_IN_HARDIRQ))
2513 return 0;
2514 if (curr->softirq_context)
2515 if (!mark_lock(curr, hlock, LOCK_USED_IN_SOFTIRQ))
2516 return 0;
2517 }
2518 }
2519 if (!hlock->hardirqs_off) {
2520 if (hlock->read) {
2521 if (!mark_lock(curr, hlock,
Peter Zijlstra4fc95e82009-01-22 13:10:52 +01002522 LOCK_ENABLED_HARDIRQ_READ))
Peter Zijlstra8e182572007-07-19 01:48:54 -07002523 return 0;
2524 if (curr->softirqs_enabled)
2525 if (!mark_lock(curr, hlock,
Peter Zijlstra4fc95e82009-01-22 13:10:52 +01002526 LOCK_ENABLED_SOFTIRQ_READ))
Peter Zijlstra8e182572007-07-19 01:48:54 -07002527 return 0;
2528 } else {
2529 if (!mark_lock(curr, hlock,
Peter Zijlstra4fc95e82009-01-22 13:10:52 +01002530 LOCK_ENABLED_HARDIRQ))
Peter Zijlstra8e182572007-07-19 01:48:54 -07002531 return 0;
2532 if (curr->softirqs_enabled)
2533 if (!mark_lock(curr, hlock,
Peter Zijlstra4fc95e82009-01-22 13:10:52 +01002534 LOCK_ENABLED_SOFTIRQ))
Peter Zijlstra8e182572007-07-19 01:48:54 -07002535 return 0;
2536 }
2537 }
2538
Nick Piggincf40bd12009-01-21 08:12:39 +01002539 /*
2540 * We reuse the irq context infrastructure more broadly as a general
2541 * context checking code. This tests GFP_FS recursion (a lock taken
2542 * during reclaim for a GFP_FS allocation is held over a GFP_FS
2543 * allocation).
2544 */
2545 if (!hlock->trylock && (curr->lockdep_reclaim_gfp & __GFP_FS)) {
2546 if (hlock->read) {
2547 if (!mark_lock(curr, hlock, LOCK_USED_IN_RECLAIM_FS_READ))
2548 return 0;
2549 } else {
2550 if (!mark_lock(curr, hlock, LOCK_USED_IN_RECLAIM_FS))
2551 return 0;
2552 }
2553 }
2554
Peter Zijlstra8e182572007-07-19 01:48:54 -07002555 return 1;
2556}
2557
2558static int separate_irq_context(struct task_struct *curr,
2559 struct held_lock *hlock)
2560{
2561 unsigned int depth = curr->lockdep_depth;
2562
2563 /*
2564 * Keep track of points where we cross into an interrupt context:
2565 */
2566 hlock->irq_context = 2*(curr->hardirq_context ? 1 : 0) +
2567 curr->softirq_context;
2568 if (depth) {
2569 struct held_lock *prev_hlock;
2570
2571 prev_hlock = curr->held_locks + depth-1;
2572 /*
2573 * If we cross into another context, reset the
2574 * hash key (this also prevents the checking and the
2575 * adding of the dependency to 'prev'):
2576 */
2577 if (prev_hlock->irq_context != hlock->irq_context)
2578 return 1;
2579 }
2580 return 0;
2581}
2582
2583#else
2584
2585static inline
2586int mark_lock_irq(struct task_struct *curr, struct held_lock *this,
2587 enum lock_usage_bit new_bit)
2588{
2589 WARN_ON(1);
2590 return 1;
2591}
2592
2593static inline int mark_irqflags(struct task_struct *curr,
2594 struct held_lock *hlock)
2595{
2596 return 1;
2597}
2598
2599static inline int separate_irq_context(struct task_struct *curr,
2600 struct held_lock *hlock)
2601{
2602 return 0;
2603}
2604
Peter Zijlstra868a23a2009-02-15 00:25:21 +01002605void lockdep_trace_alloc(gfp_t gfp_mask)
2606{
2607}
2608
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002609#endif
2610
2611/*
Peter Zijlstra8e182572007-07-19 01:48:54 -07002612 * Mark a lock with a usage bit, and validate the state transition:
2613 */
Steven Rostedt1d09daa2008-05-12 21:20:55 +02002614static int mark_lock(struct task_struct *curr, struct held_lock *this,
Steven Rostedt0764d232008-05-12 21:20:44 +02002615 enum lock_usage_bit new_bit)
Peter Zijlstra8e182572007-07-19 01:48:54 -07002616{
2617 unsigned int new_mask = 1 << new_bit, ret = 1;
2618
2619 /*
2620 * If already set then do not dirty the cacheline,
2621 * nor do any checks:
2622 */
Dave Jonesf82b2172008-08-11 09:30:23 +02002623 if (likely(hlock_class(this)->usage_mask & new_mask))
Peter Zijlstra8e182572007-07-19 01:48:54 -07002624 return 1;
2625
2626 if (!graph_lock())
2627 return 0;
2628 /*
2629 * Make sure we didnt race:
2630 */
Dave Jonesf82b2172008-08-11 09:30:23 +02002631 if (unlikely(hlock_class(this)->usage_mask & new_mask)) {
Peter Zijlstra8e182572007-07-19 01:48:54 -07002632 graph_unlock();
2633 return 1;
2634 }
2635
Dave Jonesf82b2172008-08-11 09:30:23 +02002636 hlock_class(this)->usage_mask |= new_mask;
Peter Zijlstra8e182572007-07-19 01:48:54 -07002637
Dave Jonesf82b2172008-08-11 09:30:23 +02002638 if (!save_trace(hlock_class(this)->usage_traces + new_bit))
Peter Zijlstra8e182572007-07-19 01:48:54 -07002639 return 0;
2640
2641 switch (new_bit) {
Peter Zijlstra53464172009-01-22 14:15:53 +01002642#define LOCKDEP_STATE(__STATE) \
2643 case LOCK_USED_IN_##__STATE: \
2644 case LOCK_USED_IN_##__STATE##_READ: \
2645 case LOCK_ENABLED_##__STATE: \
2646 case LOCK_ENABLED_##__STATE##_READ:
2647#include "lockdep_states.h"
2648#undef LOCKDEP_STATE
Peter Zijlstra8e182572007-07-19 01:48:54 -07002649 ret = mark_lock_irq(curr, this, new_bit);
2650 if (!ret)
2651 return 0;
2652 break;
2653 case LOCK_USED:
Frederic Weisbeckerbd6d29c2010-04-06 00:10:17 +02002654 debug_atomic_dec(nr_unused_locks);
Peter Zijlstra8e182572007-07-19 01:48:54 -07002655 break;
2656 default:
2657 if (!debug_locks_off_graph_unlock())
2658 return 0;
2659 WARN_ON(1);
2660 return 0;
2661 }
2662
2663 graph_unlock();
2664
2665 /*
2666 * We must printk outside of the graph_lock:
2667 */
2668 if (ret == 2) {
2669 printk("\nmarked lock as {%s}:\n", usage_str[new_bit]);
2670 print_lock(this);
2671 print_irqtrace_events(curr);
2672 dump_stack();
2673 }
2674
2675 return ret;
2676}
2677
2678/*
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002679 * Initialize a lock instance's lock-class mapping info:
2680 */
2681void lockdep_init_map(struct lockdep_map *lock, const char *name,
Peter Zijlstra4dfbb9d2006-10-11 01:45:14 -04002682 struct lock_class_key *key, int subclass)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002683{
Hitoshi Mitake62016252010-10-05 18:01:51 +09002684 int i;
2685
2686 for (i = 0; i < NR_LOCKDEP_CACHING_CLASSES; i++)
2687 lock->class_cache[i] = NULL;
2688
Peter Zijlstrac8a25002009-04-17 09:40:49 +02002689#ifdef CONFIG_LOCK_STAT
2690 lock->cpu = raw_smp_processor_id();
2691#endif
2692
2693 if (DEBUG_LOCKS_WARN_ON(!name)) {
2694 lock->name = "NULL";
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002695 return;
Peter Zijlstrac8a25002009-04-17 09:40:49 +02002696 }
2697
2698 lock->name = name;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002699
2700 if (DEBUG_LOCKS_WARN_ON(!key))
2701 return;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002702 /*
2703 * Sanity check, the lock-class key must be persistent:
2704 */
2705 if (!static_obj(key)) {
2706 printk("BUG: key %p not in .data!\n", key);
2707 DEBUG_LOCKS_WARN_ON(1);
2708 return;
2709 }
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002710 lock->key = key;
Peter Zijlstrac8a25002009-04-17 09:40:49 +02002711
2712 if (unlikely(!debug_locks))
2713 return;
2714
Peter Zijlstra4dfbb9d2006-10-11 01:45:14 -04002715 if (subclass)
2716 register_lock_class(lock, subclass, 1);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002717}
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002718EXPORT_SYMBOL_GPL(lockdep_init_map);
2719
Peter Zijlstra1704f472010-03-19 01:37:42 +01002720struct lock_class_key __lockdep_no_validate__;
2721
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002722/*
2723 * This gets called for every mutex_lock*()/spin_lock*() operation.
2724 * We maintain the dependency maps and validate the locking attempt:
2725 */
2726static int __lock_acquire(struct lockdep_map *lock, unsigned int subclass,
2727 int trylock, int read, int check, int hardirqs_off,
Peter Zijlstrabb97a912009-07-20 19:15:35 +02002728 struct lockdep_map *nest_lock, unsigned long ip,
2729 int references)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002730{
2731 struct task_struct *curr = current;
Ingo Molnard6d897c2006-07-10 04:44:04 -07002732 struct lock_class *class = NULL;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002733 struct held_lock *hlock;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002734 unsigned int depth, id;
2735 int chain_head = 0;
Peter Zijlstrabb97a912009-07-20 19:15:35 +02002736 int class_idx;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002737 u64 chain_key;
2738
Peter Zijlstraf20786f2007-07-19 01:48:56 -07002739 if (!prove_locking)
2740 check = 1;
2741
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002742 if (unlikely(!debug_locks))
2743 return 0;
2744
2745 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2746 return 0;
2747
2748 if (unlikely(subclass >= MAX_LOCKDEP_SUBCLASSES)) {
2749 debug_locks_off();
2750 printk("BUG: MAX_LOCKDEP_SUBCLASSES too low!\n");
2751 printk("turning off the locking correctness validator.\n");
Peter Zijlstraeedeeab2009-03-18 12:38:47 +01002752 dump_stack();
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002753 return 0;
2754 }
2755
Peter Zijlstra1704f472010-03-19 01:37:42 +01002756 if (lock->key == &__lockdep_no_validate__)
2757 check = 1;
2758
Hitoshi Mitake62016252010-10-05 18:01:51 +09002759 if (subclass < NR_LOCKDEP_CACHING_CLASSES)
2760 class = lock->class_cache[subclass];
Ingo Molnard6d897c2006-07-10 04:44:04 -07002761 /*
Hitoshi Mitake62016252010-10-05 18:01:51 +09002762 * Not cached?
Ingo Molnard6d897c2006-07-10 04:44:04 -07002763 */
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002764 if (unlikely(!class)) {
Peter Zijlstra4dfbb9d2006-10-11 01:45:14 -04002765 class = register_lock_class(lock, subclass, 0);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002766 if (!class)
2767 return 0;
2768 }
Frederic Weisbeckerbd6d29c2010-04-06 00:10:17 +02002769 atomic_inc((atomic_t *)&class->ops);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002770 if (very_verbose(class)) {
2771 printk("\nacquire class [%p] %s", class->key, class->name);
2772 if (class->name_version > 1)
2773 printk("#%d", class->name_version);
2774 printk("\n");
2775 dump_stack();
2776 }
2777
2778 /*
2779 * Add the lock to the list of currently held locks.
2780 * (we dont increase the depth just yet, up until the
2781 * dependency checks are done)
2782 */
2783 depth = curr->lockdep_depth;
2784 if (DEBUG_LOCKS_WARN_ON(depth >= MAX_LOCK_DEPTH))
2785 return 0;
2786
Peter Zijlstrabb97a912009-07-20 19:15:35 +02002787 class_idx = class - lock_classes + 1;
2788
2789 if (depth) {
2790 hlock = curr->held_locks + depth - 1;
2791 if (hlock->class_idx == class_idx && nest_lock) {
2792 if (hlock->references)
2793 hlock->references++;
2794 else
2795 hlock->references = 2;
2796
2797 return 1;
2798 }
2799 }
2800
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002801 hlock = curr->held_locks + depth;
Dave Jonesf82b2172008-08-11 09:30:23 +02002802 if (DEBUG_LOCKS_WARN_ON(!class))
2803 return 0;
Peter Zijlstrabb97a912009-07-20 19:15:35 +02002804 hlock->class_idx = class_idx;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002805 hlock->acquire_ip = ip;
2806 hlock->instance = lock;
Peter Zijlstra7531e2f2008-08-11 09:30:24 +02002807 hlock->nest_lock = nest_lock;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002808 hlock->trylock = trylock;
2809 hlock->read = read;
2810 hlock->check = check;
Dmitry Baryshkov6951b122008-08-18 04:26:37 +04002811 hlock->hardirqs_off = !!hardirqs_off;
Peter Zijlstrabb97a912009-07-20 19:15:35 +02002812 hlock->references = references;
Peter Zijlstraf20786f2007-07-19 01:48:56 -07002813#ifdef CONFIG_LOCK_STAT
2814 hlock->waittime_stamp = 0;
Peter Zijlstra3365e7792009-10-09 10:12:41 +02002815 hlock->holdtime_stamp = lockstat_clock();
Peter Zijlstraf20786f2007-07-19 01:48:56 -07002816#endif
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002817
Peter Zijlstra8e182572007-07-19 01:48:54 -07002818 if (check == 2 && !mark_irqflags(curr, hlock))
2819 return 0;
2820
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002821 /* mark it as used: */
Jarek Poplawski4ff773bb2007-05-08 00:31:00 -07002822 if (!mark_lock(curr, hlock, LOCK_USED))
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002823 return 0;
Peter Zijlstra8e182572007-07-19 01:48:54 -07002824
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002825 /*
Gautham R Shenoy17aacfb2007-10-28 20:47:01 +01002826 * Calculate the chain hash: it's the combined hash of all the
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002827 * lock keys along the dependency chain. We save the hash value
2828 * at every step so that we can get the current hash easily
2829 * after unlock. The chain hash is then used to cache dependency
2830 * results.
2831 *
2832 * The 'key ID' is what is the most compact key value to drive
2833 * the hash, not class->key.
2834 */
2835 id = class - lock_classes;
2836 if (DEBUG_LOCKS_WARN_ON(id >= MAX_LOCKDEP_KEYS))
2837 return 0;
2838
2839 chain_key = curr->curr_chain_key;
2840 if (!depth) {
2841 if (DEBUG_LOCKS_WARN_ON(chain_key != 0))
2842 return 0;
2843 chain_head = 1;
2844 }
2845
2846 hlock->prev_chain_key = chain_key;
Peter Zijlstra8e182572007-07-19 01:48:54 -07002847 if (separate_irq_context(curr, hlock)) {
2848 chain_key = 0;
2849 chain_head = 1;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002850 }
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002851 chain_key = iterate_chain_key(chain_key, id);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002852
Gregory Haskins3aa416b2007-10-11 22:11:11 +02002853 if (!validate_chain(curr, lock, hlock, chain_head, chain_key))
Peter Zijlstra8e182572007-07-19 01:48:54 -07002854 return 0;
Jarek Poplawski381a2292007-02-10 01:44:58 -08002855
Gregory Haskins3aa416b2007-10-11 22:11:11 +02002856 curr->curr_chain_key = chain_key;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002857 curr->lockdep_depth++;
2858 check_chain_key(curr);
Jarek Poplawski60e114d2007-02-20 13:58:00 -08002859#ifdef CONFIG_DEBUG_LOCKDEP
2860 if (unlikely(!debug_locks))
2861 return 0;
2862#endif
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002863 if (unlikely(curr->lockdep_depth >= MAX_LOCK_DEPTH)) {
2864 debug_locks_off();
2865 printk("BUG: MAX_LOCK_DEPTH too low!\n");
2866 printk("turning off the locking correctness validator.\n");
Peter Zijlstraeedeeab2009-03-18 12:38:47 +01002867 dump_stack();
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002868 return 0;
2869 }
Jarek Poplawski381a2292007-02-10 01:44:58 -08002870
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002871 if (unlikely(curr->lockdep_depth > max_lockdep_depth))
2872 max_lockdep_depth = curr->lockdep_depth;
2873
2874 return 1;
2875}
2876
2877static int
2878print_unlock_inbalance_bug(struct task_struct *curr, struct lockdep_map *lock,
2879 unsigned long ip)
2880{
2881 if (!debug_locks_off())
2882 return 0;
2883 if (debug_locks_silent)
2884 return 0;
2885
2886 printk("\n=====================================\n");
2887 printk( "[ BUG: bad unlock balance detected! ]\n");
2888 printk( "-------------------------------------\n");
2889 printk("%s/%d is trying to release lock (",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07002890 curr->comm, task_pid_nr(curr));
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002891 print_lockdep_cache(lock);
2892 printk(") at:\n");
2893 print_ip_sym(ip);
2894 printk("but there are no more locks to release!\n");
2895 printk("\nother info that might help us debug this:\n");
2896 lockdep_print_held_locks(curr);
2897
2898 printk("\nstack backtrace:\n");
2899 dump_stack();
2900
2901 return 0;
2902}
2903
2904/*
2905 * Common debugging checks for both nested and non-nested unlock:
2906 */
2907static int check_unlock(struct task_struct *curr, struct lockdep_map *lock,
2908 unsigned long ip)
2909{
2910 if (unlikely(!debug_locks))
2911 return 0;
2912 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2913 return 0;
2914
2915 if (curr->lockdep_depth <= 0)
2916 return print_unlock_inbalance_bug(curr, lock, ip);
2917
2918 return 1;
2919}
2920
Peter Zijlstrabb97a912009-07-20 19:15:35 +02002921static int match_held_lock(struct held_lock *hlock, struct lockdep_map *lock)
2922{
2923 if (hlock->instance == lock)
2924 return 1;
2925
2926 if (hlock->references) {
Hitoshi Mitake62016252010-10-05 18:01:51 +09002927 struct lock_class *class = lock->class_cache[0];
Peter Zijlstrabb97a912009-07-20 19:15:35 +02002928
2929 if (!class)
2930 class = look_up_lock_class(lock, 0);
2931
2932 if (DEBUG_LOCKS_WARN_ON(!class))
2933 return 0;
2934
2935 if (DEBUG_LOCKS_WARN_ON(!hlock->nest_lock))
2936 return 0;
2937
2938 if (hlock->class_idx == class - lock_classes + 1)
2939 return 1;
2940 }
2941
2942 return 0;
2943}
2944
Peter Zijlstra64aa3482008-08-11 09:30:21 +02002945static int
Peter Zijlstra00ef9f72008-12-04 09:00:17 +01002946__lock_set_class(struct lockdep_map *lock, const char *name,
2947 struct lock_class_key *key, unsigned int subclass,
2948 unsigned long ip)
Peter Zijlstra64aa3482008-08-11 09:30:21 +02002949{
2950 struct task_struct *curr = current;
2951 struct held_lock *hlock, *prev_hlock;
2952 struct lock_class *class;
2953 unsigned int depth;
2954 int i;
2955
2956 depth = curr->lockdep_depth;
2957 if (DEBUG_LOCKS_WARN_ON(!depth))
2958 return 0;
2959
2960 prev_hlock = NULL;
2961 for (i = depth-1; i >= 0; i--) {
2962 hlock = curr->held_locks + i;
2963 /*
2964 * We must not cross into another context:
2965 */
2966 if (prev_hlock && prev_hlock->irq_context != hlock->irq_context)
2967 break;
Peter Zijlstrabb97a912009-07-20 19:15:35 +02002968 if (match_held_lock(hlock, lock))
Peter Zijlstra64aa3482008-08-11 09:30:21 +02002969 goto found_it;
2970 prev_hlock = hlock;
2971 }
2972 return print_unlock_inbalance_bug(curr, lock, ip);
2973
2974found_it:
Peter Zijlstra00ef9f72008-12-04 09:00:17 +01002975 lockdep_init_map(lock, name, key, 0);
Peter Zijlstra64aa3482008-08-11 09:30:21 +02002976 class = register_lock_class(lock, subclass, 0);
Dave Jonesf82b2172008-08-11 09:30:23 +02002977 hlock->class_idx = class - lock_classes + 1;
Peter Zijlstra64aa3482008-08-11 09:30:21 +02002978
2979 curr->lockdep_depth = i;
2980 curr->curr_chain_key = hlock->prev_chain_key;
2981
2982 for (; i < depth; i++) {
2983 hlock = curr->held_locks + i;
2984 if (!__lock_acquire(hlock->instance,
Dave Jonesf82b2172008-08-11 09:30:23 +02002985 hlock_class(hlock)->subclass, hlock->trylock,
Peter Zijlstra64aa3482008-08-11 09:30:21 +02002986 hlock->read, hlock->check, hlock->hardirqs_off,
Peter Zijlstrabb97a912009-07-20 19:15:35 +02002987 hlock->nest_lock, hlock->acquire_ip,
2988 hlock->references))
Peter Zijlstra64aa3482008-08-11 09:30:21 +02002989 return 0;
2990 }
2991
2992 if (DEBUG_LOCKS_WARN_ON(curr->lockdep_depth != depth))
2993 return 0;
2994 return 1;
2995}
2996
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002997/*
2998 * Remove the lock to the list of currently held locks in a
2999 * potentially non-nested (out of order) manner. This is a
3000 * relatively rare operation, as all the unlock APIs default
3001 * to nested mode (which uses lock_release()):
3002 */
3003static int
3004lock_release_non_nested(struct task_struct *curr,
3005 struct lockdep_map *lock, unsigned long ip)
3006{
3007 struct held_lock *hlock, *prev_hlock;
3008 unsigned int depth;
3009 int i;
3010
3011 /*
3012 * Check whether the lock exists in the current stack
3013 * of held locks:
3014 */
3015 depth = curr->lockdep_depth;
3016 if (DEBUG_LOCKS_WARN_ON(!depth))
3017 return 0;
3018
3019 prev_hlock = NULL;
3020 for (i = depth-1; i >= 0; i--) {
3021 hlock = curr->held_locks + i;
3022 /*
3023 * We must not cross into another context:
3024 */
3025 if (prev_hlock && prev_hlock->irq_context != hlock->irq_context)
3026 break;
Peter Zijlstrabb97a912009-07-20 19:15:35 +02003027 if (match_held_lock(hlock, lock))
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003028 goto found_it;
3029 prev_hlock = hlock;
3030 }
3031 return print_unlock_inbalance_bug(curr, lock, ip);
3032
3033found_it:
Peter Zijlstrabb97a912009-07-20 19:15:35 +02003034 if (hlock->instance == lock)
3035 lock_release_holdtime(hlock);
3036
3037 if (hlock->references) {
3038 hlock->references--;
3039 if (hlock->references) {
3040 /*
3041 * We had, and after removing one, still have
3042 * references, the current lock stack is still
3043 * valid. We're done!
3044 */
3045 return 1;
3046 }
3047 }
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003048
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003049 /*
3050 * We have the right lock to unlock, 'hlock' points to it.
3051 * Now we remove it from the stack, and add back the other
3052 * entries (if any), recalculating the hash along the way:
3053 */
Peter Zijlstrabb97a912009-07-20 19:15:35 +02003054
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003055 curr->lockdep_depth = i;
3056 curr->curr_chain_key = hlock->prev_chain_key;
3057
3058 for (i++; i < depth; i++) {
3059 hlock = curr->held_locks + i;
3060 if (!__lock_acquire(hlock->instance,
Dave Jonesf82b2172008-08-11 09:30:23 +02003061 hlock_class(hlock)->subclass, hlock->trylock,
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003062 hlock->read, hlock->check, hlock->hardirqs_off,
Peter Zijlstrabb97a912009-07-20 19:15:35 +02003063 hlock->nest_lock, hlock->acquire_ip,
3064 hlock->references))
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003065 return 0;
3066 }
3067
3068 if (DEBUG_LOCKS_WARN_ON(curr->lockdep_depth != depth - 1))
3069 return 0;
3070 return 1;
3071}
3072
3073/*
3074 * Remove the lock to the list of currently held locks - this gets
3075 * called on mutex_unlock()/spin_unlock*() (or on a failed
3076 * mutex_lock_interruptible()). This is done for unlocks that nest
3077 * perfectly. (i.e. the current top of the lock-stack is unlocked)
3078 */
3079static int lock_release_nested(struct task_struct *curr,
3080 struct lockdep_map *lock, unsigned long ip)
3081{
3082 struct held_lock *hlock;
3083 unsigned int depth;
3084
3085 /*
3086 * Pop off the top of the lock stack:
3087 */
3088 depth = curr->lockdep_depth - 1;
3089 hlock = curr->held_locks + depth;
3090
3091 /*
3092 * Is the unlock non-nested:
3093 */
Peter Zijlstrabb97a912009-07-20 19:15:35 +02003094 if (hlock->instance != lock || hlock->references)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003095 return lock_release_non_nested(curr, lock, ip);
3096 curr->lockdep_depth--;
3097
3098 if (DEBUG_LOCKS_WARN_ON(!depth && (hlock->prev_chain_key != 0)))
3099 return 0;
3100
3101 curr->curr_chain_key = hlock->prev_chain_key;
3102
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003103 lock_release_holdtime(hlock);
3104
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003105#ifdef CONFIG_DEBUG_LOCKDEP
3106 hlock->prev_chain_key = 0;
Dave Jonesf82b2172008-08-11 09:30:23 +02003107 hlock->class_idx = 0;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003108 hlock->acquire_ip = 0;
3109 hlock->irq_context = 0;
3110#endif
3111 return 1;
3112}
3113
3114/*
3115 * Remove the lock to the list of currently held locks - this gets
3116 * called on mutex_unlock()/spin_unlock*() (or on a failed
3117 * mutex_lock_interruptible()). This is done for unlocks that nest
3118 * perfectly. (i.e. the current top of the lock-stack is unlocked)
3119 */
3120static void
3121__lock_release(struct lockdep_map *lock, int nested, unsigned long ip)
3122{
3123 struct task_struct *curr = current;
3124
3125 if (!check_unlock(curr, lock, ip))
3126 return;
3127
3128 if (nested) {
3129 if (!lock_release_nested(curr, lock, ip))
3130 return;
3131 } else {
3132 if (!lock_release_non_nested(curr, lock, ip))
3133 return;
3134 }
3135
3136 check_chain_key(curr);
3137}
3138
Peter Zijlstraf607c662009-07-20 19:16:29 +02003139static int __lock_is_held(struct lockdep_map *lock)
3140{
3141 struct task_struct *curr = current;
3142 int i;
3143
3144 for (i = 0; i < curr->lockdep_depth; i++) {
Peter Zijlstrabb97a912009-07-20 19:15:35 +02003145 struct held_lock *hlock = curr->held_locks + i;
3146
3147 if (match_held_lock(hlock, lock))
Peter Zijlstraf607c662009-07-20 19:16:29 +02003148 return 1;
3149 }
3150
3151 return 0;
3152}
3153
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003154/*
3155 * Check whether we follow the irq-flags state precisely:
3156 */
Steven Rostedt1d09daa2008-05-12 21:20:55 +02003157static void check_flags(unsigned long flags)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003158{
Ingo Molnar992860e2008-07-14 10:28:38 +02003159#if defined(CONFIG_PROVE_LOCKING) && defined(CONFIG_DEBUG_LOCKDEP) && \
3160 defined(CONFIG_TRACE_IRQFLAGS)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003161 if (!debug_locks)
3162 return;
3163
Ingo Molnar5f9fa8a2007-12-07 19:02:47 +01003164 if (irqs_disabled_flags(flags)) {
3165 if (DEBUG_LOCKS_WARN_ON(current->hardirqs_enabled)) {
3166 printk("possible reason: unannotated irqs-off.\n");
3167 }
3168 } else {
3169 if (DEBUG_LOCKS_WARN_ON(!current->hardirqs_enabled)) {
3170 printk("possible reason: unannotated irqs-on.\n");
3171 }
3172 }
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003173
3174 /*
3175 * We dont accurately track softirq state in e.g.
3176 * hardirq contexts (such as on 4KSTACKS), so only
3177 * check if not in hardirq contexts:
3178 */
3179 if (!hardirq_count()) {
3180 if (softirq_count())
3181 DEBUG_LOCKS_WARN_ON(current->softirqs_enabled);
3182 else
3183 DEBUG_LOCKS_WARN_ON(!current->softirqs_enabled);
3184 }
3185
3186 if (!debug_locks)
3187 print_irqtrace_events(current);
3188#endif
3189}
3190
Peter Zijlstra00ef9f72008-12-04 09:00:17 +01003191void lock_set_class(struct lockdep_map *lock, const char *name,
3192 struct lock_class_key *key, unsigned int subclass,
3193 unsigned long ip)
Peter Zijlstra64aa3482008-08-11 09:30:21 +02003194{
3195 unsigned long flags;
3196
3197 if (unlikely(current->lockdep_recursion))
3198 return;
3199
3200 raw_local_irq_save(flags);
3201 current->lockdep_recursion = 1;
3202 check_flags(flags);
Peter Zijlstra00ef9f72008-12-04 09:00:17 +01003203 if (__lock_set_class(lock, name, key, subclass, ip))
Peter Zijlstra64aa3482008-08-11 09:30:21 +02003204 check_chain_key(current);
3205 current->lockdep_recursion = 0;
3206 raw_local_irq_restore(flags);
3207}
Peter Zijlstra00ef9f72008-12-04 09:00:17 +01003208EXPORT_SYMBOL_GPL(lock_set_class);
Peter Zijlstra64aa3482008-08-11 09:30:21 +02003209
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003210/*
3211 * We are not always called with irqs disabled - do that here,
3212 * and also avoid lockdep recursion:
3213 */
Steven Rostedt1d09daa2008-05-12 21:20:55 +02003214void lock_acquire(struct lockdep_map *lock, unsigned int subclass,
Peter Zijlstra7531e2f2008-08-11 09:30:24 +02003215 int trylock, int read, int check,
3216 struct lockdep_map *nest_lock, unsigned long ip)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003217{
3218 unsigned long flags;
3219
3220 if (unlikely(current->lockdep_recursion))
3221 return;
3222
3223 raw_local_irq_save(flags);
3224 check_flags(flags);
3225
3226 current->lockdep_recursion = 1;
Frederic Weisbeckerdb2c4c72010-02-02 23:34:40 +01003227 trace_lock_acquire(lock, subclass, trylock, read, check, nest_lock, ip);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003228 __lock_acquire(lock, subclass, trylock, read, check,
Peter Zijlstrabb97a912009-07-20 19:15:35 +02003229 irqs_disabled_flags(flags), nest_lock, ip, 0);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003230 current->lockdep_recursion = 0;
3231 raw_local_irq_restore(flags);
3232}
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003233EXPORT_SYMBOL_GPL(lock_acquire);
3234
Steven Rostedt1d09daa2008-05-12 21:20:55 +02003235void lock_release(struct lockdep_map *lock, int nested,
Steven Rostedt0764d232008-05-12 21:20:44 +02003236 unsigned long ip)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003237{
3238 unsigned long flags;
3239
3240 if (unlikely(current->lockdep_recursion))
3241 return;
3242
3243 raw_local_irq_save(flags);
3244 check_flags(flags);
3245 current->lockdep_recursion = 1;
Frederic Weisbecker93135432010-05-08 06:24:25 +02003246 trace_lock_release(lock, ip);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003247 __lock_release(lock, nested, ip);
3248 current->lockdep_recursion = 0;
3249 raw_local_irq_restore(flags);
3250}
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003251EXPORT_SYMBOL_GPL(lock_release);
3252
Peter Zijlstraf607c662009-07-20 19:16:29 +02003253int lock_is_held(struct lockdep_map *lock)
3254{
3255 unsigned long flags;
3256 int ret = 0;
3257
3258 if (unlikely(current->lockdep_recursion))
3259 return ret;
3260
3261 raw_local_irq_save(flags);
3262 check_flags(flags);
3263
3264 current->lockdep_recursion = 1;
3265 ret = __lock_is_held(lock);
3266 current->lockdep_recursion = 0;
3267 raw_local_irq_restore(flags);
3268
3269 return ret;
3270}
3271EXPORT_SYMBOL_GPL(lock_is_held);
3272
Nick Piggincf40bd12009-01-21 08:12:39 +01003273void lockdep_set_current_reclaim_state(gfp_t gfp_mask)
3274{
3275 current->lockdep_reclaim_gfp = gfp_mask;
3276}
3277
3278void lockdep_clear_current_reclaim_state(void)
3279{
3280 current->lockdep_reclaim_gfp = 0;
3281}
3282
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003283#ifdef CONFIG_LOCK_STAT
3284static int
3285print_lock_contention_bug(struct task_struct *curr, struct lockdep_map *lock,
3286 unsigned long ip)
3287{
3288 if (!debug_locks_off())
3289 return 0;
3290 if (debug_locks_silent)
3291 return 0;
3292
3293 printk("\n=================================\n");
3294 printk( "[ BUG: bad contention detected! ]\n");
3295 printk( "---------------------------------\n");
3296 printk("%s/%d is trying to contend lock (",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07003297 curr->comm, task_pid_nr(curr));
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003298 print_lockdep_cache(lock);
3299 printk(") at:\n");
3300 print_ip_sym(ip);
3301 printk("but there are no locks held!\n");
3302 printk("\nother info that might help us debug this:\n");
3303 lockdep_print_held_locks(curr);
3304
3305 printk("\nstack backtrace:\n");
3306 dump_stack();
3307
3308 return 0;
3309}
3310
3311static void
3312__lock_contended(struct lockdep_map *lock, unsigned long ip)
3313{
3314 struct task_struct *curr = current;
3315 struct held_lock *hlock, *prev_hlock;
3316 struct lock_class_stats *stats;
3317 unsigned int depth;
Peter Zijlstrac7e78cf2008-10-16 23:17:09 +02003318 int i, contention_point, contending_point;
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003319
3320 depth = curr->lockdep_depth;
3321 if (DEBUG_LOCKS_WARN_ON(!depth))
3322 return;
3323
3324 prev_hlock = NULL;
3325 for (i = depth-1; i >= 0; i--) {
3326 hlock = curr->held_locks + i;
3327 /*
3328 * We must not cross into another context:
3329 */
3330 if (prev_hlock && prev_hlock->irq_context != hlock->irq_context)
3331 break;
Peter Zijlstrabb97a912009-07-20 19:15:35 +02003332 if (match_held_lock(hlock, lock))
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003333 goto found_it;
3334 prev_hlock = hlock;
3335 }
3336 print_lock_contention_bug(curr, lock, ip);
3337 return;
3338
3339found_it:
Peter Zijlstrabb97a912009-07-20 19:15:35 +02003340 if (hlock->instance != lock)
3341 return;
3342
Peter Zijlstra3365e7792009-10-09 10:12:41 +02003343 hlock->waittime_stamp = lockstat_clock();
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003344
Peter Zijlstrac7e78cf2008-10-16 23:17:09 +02003345 contention_point = lock_point(hlock_class(hlock)->contention_point, ip);
3346 contending_point = lock_point(hlock_class(hlock)->contending_point,
3347 lock->ip);
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003348
Dave Jonesf82b2172008-08-11 09:30:23 +02003349 stats = get_lock_stats(hlock_class(hlock));
Peter Zijlstrac7e78cf2008-10-16 23:17:09 +02003350 if (contention_point < LOCKSTAT_POINTS)
3351 stats->contention_point[contention_point]++;
3352 if (contending_point < LOCKSTAT_POINTS)
3353 stats->contending_point[contending_point]++;
Peter Zijlstra96645672007-07-19 01:49:00 -07003354 if (lock->cpu != smp_processor_id())
3355 stats->bounces[bounce_contended + !!hlock->read]++;
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003356 put_lock_stats(stats);
3357}
3358
3359static void
Peter Zijlstrac7e78cf2008-10-16 23:17:09 +02003360__lock_acquired(struct lockdep_map *lock, unsigned long ip)
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003361{
3362 struct task_struct *curr = current;
3363 struct held_lock *hlock, *prev_hlock;
3364 struct lock_class_stats *stats;
3365 unsigned int depth;
Peter Zijlstra3365e7792009-10-09 10:12:41 +02003366 u64 now, waittime = 0;
Peter Zijlstra96645672007-07-19 01:49:00 -07003367 int i, cpu;
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003368
3369 depth = curr->lockdep_depth;
3370 if (DEBUG_LOCKS_WARN_ON(!depth))
3371 return;
3372
3373 prev_hlock = NULL;
3374 for (i = depth-1; i >= 0; i--) {
3375 hlock = curr->held_locks + i;
3376 /*
3377 * We must not cross into another context:
3378 */
3379 if (prev_hlock && prev_hlock->irq_context != hlock->irq_context)
3380 break;
Peter Zijlstrabb97a912009-07-20 19:15:35 +02003381 if (match_held_lock(hlock, lock))
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003382 goto found_it;
3383 prev_hlock = hlock;
3384 }
3385 print_lock_contention_bug(curr, lock, _RET_IP_);
3386 return;
3387
3388found_it:
Peter Zijlstrabb97a912009-07-20 19:15:35 +02003389 if (hlock->instance != lock)
3390 return;
3391
Peter Zijlstra96645672007-07-19 01:49:00 -07003392 cpu = smp_processor_id();
3393 if (hlock->waittime_stamp) {
Peter Zijlstra3365e7792009-10-09 10:12:41 +02003394 now = lockstat_clock();
Peter Zijlstra96645672007-07-19 01:49:00 -07003395 waittime = now - hlock->waittime_stamp;
3396 hlock->holdtime_stamp = now;
3397 }
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003398
Frederic Weisbecker883a2a32010-05-08 06:16:11 +02003399 trace_lock_acquired(lock, ip);
Frederic Weisbecker20625012009-04-06 01:49:33 +02003400
Dave Jonesf82b2172008-08-11 09:30:23 +02003401 stats = get_lock_stats(hlock_class(hlock));
Peter Zijlstra96645672007-07-19 01:49:00 -07003402 if (waittime) {
3403 if (hlock->read)
3404 lock_time_inc(&stats->read_waittime, waittime);
3405 else
3406 lock_time_inc(&stats->write_waittime, waittime);
3407 }
3408 if (lock->cpu != cpu)
3409 stats->bounces[bounce_acquired + !!hlock->read]++;
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003410 put_lock_stats(stats);
Peter Zijlstra96645672007-07-19 01:49:00 -07003411
3412 lock->cpu = cpu;
Peter Zijlstrac7e78cf2008-10-16 23:17:09 +02003413 lock->ip = ip;
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003414}
3415
3416void lock_contended(struct lockdep_map *lock, unsigned long ip)
3417{
3418 unsigned long flags;
3419
3420 if (unlikely(!lock_stat))
3421 return;
3422
3423 if (unlikely(current->lockdep_recursion))
3424 return;
3425
3426 raw_local_irq_save(flags);
3427 check_flags(flags);
3428 current->lockdep_recursion = 1;
Frederic Weisbeckerdb2c4c72010-02-02 23:34:40 +01003429 trace_lock_contended(lock, ip);
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003430 __lock_contended(lock, ip);
3431 current->lockdep_recursion = 0;
3432 raw_local_irq_restore(flags);
3433}
3434EXPORT_SYMBOL_GPL(lock_contended);
3435
Peter Zijlstrac7e78cf2008-10-16 23:17:09 +02003436void lock_acquired(struct lockdep_map *lock, unsigned long ip)
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003437{
3438 unsigned long flags;
3439
3440 if (unlikely(!lock_stat))
3441 return;
3442
3443 if (unlikely(current->lockdep_recursion))
3444 return;
3445
3446 raw_local_irq_save(flags);
3447 check_flags(flags);
3448 current->lockdep_recursion = 1;
Peter Zijlstrac7e78cf2008-10-16 23:17:09 +02003449 __lock_acquired(lock, ip);
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003450 current->lockdep_recursion = 0;
3451 raw_local_irq_restore(flags);
3452}
3453EXPORT_SYMBOL_GPL(lock_acquired);
3454#endif
3455
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003456/*
3457 * Used by the testsuite, sanitize the validator state
3458 * after a simulated failure:
3459 */
3460
3461void lockdep_reset(void)
3462{
3463 unsigned long flags;
Ingo Molnar23d95a02006-12-13 00:34:40 -08003464 int i;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003465
3466 raw_local_irq_save(flags);
3467 current->curr_chain_key = 0;
3468 current->lockdep_depth = 0;
3469 current->lockdep_recursion = 0;
3470 memset(current->held_locks, 0, MAX_LOCK_DEPTH*sizeof(struct held_lock));
3471 nr_hardirq_chains = 0;
3472 nr_softirq_chains = 0;
3473 nr_process_chains = 0;
3474 debug_locks = 1;
Ingo Molnar23d95a02006-12-13 00:34:40 -08003475 for (i = 0; i < CHAINHASH_SIZE; i++)
3476 INIT_LIST_HEAD(chainhash_table + i);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003477 raw_local_irq_restore(flags);
3478}
3479
3480static void zap_class(struct lock_class *class)
3481{
3482 int i;
3483
3484 /*
3485 * Remove all dependencies this lock is
3486 * involved in:
3487 */
3488 for (i = 0; i < nr_list_entries; i++) {
3489 if (list_entries[i].class == class)
3490 list_del_rcu(&list_entries[i].entry);
3491 }
3492 /*
3493 * Unhash the class and remove it from the all_lock_classes list:
3494 */
3495 list_del_rcu(&class->hash_entry);
3496 list_del_rcu(&class->lock_entry);
3497
Rabin Vincent8bfe0292008-08-11 09:30:26 +02003498 class->key = NULL;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003499}
3500
Arjan van de Venfabe8742008-01-24 07:00:45 +01003501static inline int within(const void *addr, void *start, unsigned long size)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003502{
3503 return addr >= start && addr < start + size;
3504}
3505
3506void lockdep_free_key_range(void *start, unsigned long size)
3507{
3508 struct lock_class *class, *next;
3509 struct list_head *head;
3510 unsigned long flags;
3511 int i;
Nick Piggin5a26db52008-01-16 09:51:58 +01003512 int locked;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003513
3514 raw_local_irq_save(flags);
Nick Piggin5a26db52008-01-16 09:51:58 +01003515 locked = graph_lock();
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003516
3517 /*
3518 * Unhash all classes that were created by this module:
3519 */
3520 for (i = 0; i < CLASSHASH_SIZE; i++) {
3521 head = classhash_table + i;
3522 if (list_empty(head))
3523 continue;
Arjan van de Venfabe8742008-01-24 07:00:45 +01003524 list_for_each_entry_safe(class, next, head, hash_entry) {
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003525 if (within(class->key, start, size))
3526 zap_class(class);
Arjan van de Venfabe8742008-01-24 07:00:45 +01003527 else if (within(class->name, start, size))
3528 zap_class(class);
3529 }
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003530 }
3531
Nick Piggin5a26db52008-01-16 09:51:58 +01003532 if (locked)
3533 graph_unlock();
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003534 raw_local_irq_restore(flags);
3535}
3536
3537void lockdep_reset_lock(struct lockdep_map *lock)
3538{
Ingo Molnard6d897c2006-07-10 04:44:04 -07003539 struct lock_class *class, *next;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003540 struct list_head *head;
3541 unsigned long flags;
3542 int i, j;
Nick Piggin5a26db52008-01-16 09:51:58 +01003543 int locked;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003544
3545 raw_local_irq_save(flags);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003546
3547 /*
Ingo Molnard6d897c2006-07-10 04:44:04 -07003548 * Remove all classes this lock might have:
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003549 */
Ingo Molnard6d897c2006-07-10 04:44:04 -07003550 for (j = 0; j < MAX_LOCKDEP_SUBCLASSES; j++) {
3551 /*
3552 * If the class exists we look it up and zap it:
3553 */
3554 class = look_up_lock_class(lock, j);
3555 if (class)
3556 zap_class(class);
3557 }
3558 /*
3559 * Debug check: in the end all mapped classes should
3560 * be gone.
3561 */
Nick Piggin5a26db52008-01-16 09:51:58 +01003562 locked = graph_lock();
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003563 for (i = 0; i < CLASSHASH_SIZE; i++) {
3564 head = classhash_table + i;
3565 if (list_empty(head))
3566 continue;
3567 list_for_each_entry_safe(class, next, head, hash_entry) {
Hitoshi Mitake62016252010-10-05 18:01:51 +09003568 int match = 0;
3569
3570 for (j = 0; j < NR_LOCKDEP_CACHING_CLASSES; j++)
3571 match |= class == lock->class_cache[j];
3572
3573 if (unlikely(match)) {
Ingo Molnar74c383f2006-12-13 00:34:43 -08003574 if (debug_locks_off_graph_unlock())
3575 WARN_ON(1);
Ingo Molnard6d897c2006-07-10 04:44:04 -07003576 goto out_restore;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003577 }
3578 }
3579 }
Nick Piggin5a26db52008-01-16 09:51:58 +01003580 if (locked)
3581 graph_unlock();
Ingo Molnard6d897c2006-07-10 04:44:04 -07003582
3583out_restore:
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003584 raw_local_irq_restore(flags);
3585}
3586
Sam Ravnborg14999932007-02-28 20:12:31 -08003587void lockdep_init(void)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003588{
3589 int i;
3590
3591 /*
3592 * Some architectures have their own start_kernel()
3593 * code which calls lockdep_init(), while we also
3594 * call lockdep_init() from the start_kernel() itself,
3595 * and we want to initialize the hashes only once:
3596 */
3597 if (lockdep_initialized)
3598 return;
3599
3600 for (i = 0; i < CLASSHASH_SIZE; i++)
3601 INIT_LIST_HEAD(classhash_table + i);
3602
3603 for (i = 0; i < CHAINHASH_SIZE; i++)
3604 INIT_LIST_HEAD(chainhash_table + i);
3605
3606 lockdep_initialized = 1;
3607}
3608
3609void __init lockdep_info(void)
3610{
3611 printk("Lock dependency validator: Copyright (c) 2006 Red Hat, Inc., Ingo Molnar\n");
3612
Li Zefanb0788ca2008-11-21 15:57:32 +08003613 printk("... MAX_LOCKDEP_SUBCLASSES: %lu\n", MAX_LOCKDEP_SUBCLASSES);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003614 printk("... MAX_LOCK_DEPTH: %lu\n", MAX_LOCK_DEPTH);
3615 printk("... MAX_LOCKDEP_KEYS: %lu\n", MAX_LOCKDEP_KEYS);
Li Zefanb0788ca2008-11-21 15:57:32 +08003616 printk("... CLASSHASH_SIZE: %lu\n", CLASSHASH_SIZE);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003617 printk("... MAX_LOCKDEP_ENTRIES: %lu\n", MAX_LOCKDEP_ENTRIES);
3618 printk("... MAX_LOCKDEP_CHAINS: %lu\n", MAX_LOCKDEP_CHAINS);
3619 printk("... CHAINHASH_SIZE: %lu\n", CHAINHASH_SIZE);
3620
3621 printk(" memory used by lock dependency info: %lu kB\n",
3622 (sizeof(struct lock_class) * MAX_LOCKDEP_KEYS +
3623 sizeof(struct list_head) * CLASSHASH_SIZE +
3624 sizeof(struct lock_list) * MAX_LOCKDEP_ENTRIES +
3625 sizeof(struct lock_chain) * MAX_LOCKDEP_CHAINS +
Ming Lei90629202009-08-02 21:43:36 +08003626 sizeof(struct list_head) * CHAINHASH_SIZE
Ming Lei4dd861d2009-07-16 15:44:29 +02003627#ifdef CONFIG_PROVE_LOCKING
Ming Leie351b662009-07-22 22:48:09 +08003628 + sizeof(struct circular_queue)
Ming Lei4dd861d2009-07-16 15:44:29 +02003629#endif
Ming Lei90629202009-08-02 21:43:36 +08003630 ) / 1024
Ming Lei4dd861d2009-07-16 15:44:29 +02003631 );
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003632
3633 printk(" per task-struct memory footprint: %lu bytes\n",
3634 sizeof(struct held_lock) * MAX_LOCK_DEPTH);
3635
3636#ifdef CONFIG_DEBUG_LOCKDEP
Johannes Bergc71063c2007-07-19 01:49:02 -07003637 if (lockdep_init_error) {
3638 printk("WARNING: lockdep init error! Arch code didn't call lockdep_init() early enough?\n");
3639 printk("Call stack leading to lockdep invocation was:\n");
3640 print_stack_trace(&lockdep_init_trace, 0);
3641 }
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003642#endif
3643}
3644
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003645static void
3646print_freed_lock_bug(struct task_struct *curr, const void *mem_from,
Arjan van de Ven55794a42006-07-10 04:44:03 -07003647 const void *mem_to, struct held_lock *hlock)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003648{
3649 if (!debug_locks_off())
3650 return;
3651 if (debug_locks_silent)
3652 return;
3653
3654 printk("\n=========================\n");
3655 printk( "[ BUG: held lock freed! ]\n");
3656 printk( "-------------------------\n");
3657 printk("%s/%d is freeing memory %p-%p, with a lock still held there!\n",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07003658 curr->comm, task_pid_nr(curr), mem_from, mem_to-1);
Arjan van de Ven55794a42006-07-10 04:44:03 -07003659 print_lock(hlock);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003660 lockdep_print_held_locks(curr);
3661
3662 printk("\nstack backtrace:\n");
3663 dump_stack();
3664}
3665
Oleg Nesterov54561782007-12-05 15:46:09 +01003666static inline int not_in_range(const void* mem_from, unsigned long mem_len,
3667 const void* lock_from, unsigned long lock_len)
3668{
3669 return lock_from + lock_len <= mem_from ||
3670 mem_from + mem_len <= lock_from;
3671}
3672
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003673/*
3674 * Called when kernel memory is freed (or unmapped), or if a lock
3675 * is destroyed or reinitialized - this code checks whether there is
3676 * any held lock in the memory range of <from> to <to>:
3677 */
3678void debug_check_no_locks_freed(const void *mem_from, unsigned long mem_len)
3679{
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003680 struct task_struct *curr = current;
3681 struct held_lock *hlock;
3682 unsigned long flags;
3683 int i;
3684
3685 if (unlikely(!debug_locks))
3686 return;
3687
3688 local_irq_save(flags);
3689 for (i = 0; i < curr->lockdep_depth; i++) {
3690 hlock = curr->held_locks + i;
3691
Oleg Nesterov54561782007-12-05 15:46:09 +01003692 if (not_in_range(mem_from, mem_len, hlock->instance,
3693 sizeof(*hlock->instance)))
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003694 continue;
3695
Oleg Nesterov54561782007-12-05 15:46:09 +01003696 print_freed_lock_bug(curr, mem_from, mem_from + mem_len, hlock);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003697 break;
3698 }
3699 local_irq_restore(flags);
3700}
Peter Zijlstraed075362006-12-06 20:35:24 -08003701EXPORT_SYMBOL_GPL(debug_check_no_locks_freed);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003702
3703static void print_held_locks_bug(struct task_struct *curr)
3704{
3705 if (!debug_locks_off())
3706 return;
3707 if (debug_locks_silent)
3708 return;
3709
3710 printk("\n=====================================\n");
3711 printk( "[ BUG: lock held at task exit time! ]\n");
3712 printk( "-------------------------------------\n");
3713 printk("%s/%d is exiting with locks still held!\n",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07003714 curr->comm, task_pid_nr(curr));
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003715 lockdep_print_held_locks(curr);
3716
3717 printk("\nstack backtrace:\n");
3718 dump_stack();
3719}
3720
3721void debug_check_no_locks_held(struct task_struct *task)
3722{
3723 if (unlikely(task->lockdep_depth > 0))
3724 print_held_locks_bug(task);
3725}
3726
3727void debug_show_all_locks(void)
3728{
3729 struct task_struct *g, *p;
3730 int count = 10;
3731 int unlock = 1;
3732
Jarek Poplawski9c35dd72007-03-22 00:11:28 -08003733 if (unlikely(!debug_locks)) {
3734 printk("INFO: lockdep is turned off.\n");
3735 return;
3736 }
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003737 printk("\nShowing all locks held in the system:\n");
3738
3739 /*
3740 * Here we try to get the tasklist_lock as hard as possible,
3741 * if not successful after 2 seconds we ignore it (but keep
3742 * trying). This is to enable a debug printout even if a
3743 * tasklist_lock-holding task deadlocks or crashes.
3744 */
3745retry:
3746 if (!read_trylock(&tasklist_lock)) {
3747 if (count == 10)
3748 printk("hm, tasklist_lock locked, retrying... ");
3749 if (count) {
3750 count--;
3751 printk(" #%d", 10-count);
3752 mdelay(200);
3753 goto retry;
3754 }
3755 printk(" ignoring it.\n");
3756 unlock = 0;
qinghuang feng46fec7a2008-10-28 17:24:28 +08003757 } else {
3758 if (count != 10)
3759 printk(KERN_CONT " locked it.\n");
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003760 }
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003761
3762 do_each_thread(g, p) {
Ingo Molnar85684872007-12-05 15:46:09 +01003763 /*
3764 * It's not reliable to print a task's held locks
3765 * if it's not sleeping (or if it's not the current
3766 * task):
3767 */
3768 if (p->state == TASK_RUNNING && p != current)
3769 continue;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003770 if (p->lockdep_depth)
3771 lockdep_print_held_locks(p);
3772 if (!unlock)
3773 if (read_trylock(&tasklist_lock))
3774 unlock = 1;
3775 } while_each_thread(g, p);
3776
3777 printk("\n");
3778 printk("=============================================\n\n");
3779
3780 if (unlock)
3781 read_unlock(&tasklist_lock);
3782}
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003783EXPORT_SYMBOL_GPL(debug_show_all_locks);
3784
Ingo Molnar82a1fcb2008-01-25 21:08:02 +01003785/*
3786 * Careful: only use this function if you are sure that
3787 * the task cannot run in parallel!
3788 */
John Kacurf1b499f2010-08-05 17:10:53 +02003789void debug_show_held_locks(struct task_struct *task)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003790{
Jarek Poplawski9c35dd72007-03-22 00:11:28 -08003791 if (unlikely(!debug_locks)) {
3792 printk("INFO: lockdep is turned off.\n");
3793 return;
3794 }
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003795 lockdep_print_held_locks(task);
3796}
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003797EXPORT_SYMBOL_GPL(debug_show_held_locks);
Peter Zijlstrab351d162007-10-11 22:11:12 +02003798
3799void lockdep_sys_exit(void)
3800{
3801 struct task_struct *curr = current;
3802
3803 if (unlikely(curr->lockdep_depth)) {
3804 if (!debug_locks_off())
3805 return;
3806 printk("\n================================================\n");
3807 printk( "[ BUG: lock held when returning to user space! ]\n");
3808 printk( "------------------------------------------------\n");
3809 printk("%s/%d is leaving the kernel with locks still held!\n",
3810 curr->comm, curr->pid);
3811 lockdep_print_held_locks(curr);
3812 }
3813}
Paul E. McKenney0632eb32010-02-22 17:04:47 -08003814
3815void lockdep_rcu_dereference(const char *file, const int line)
3816{
3817 struct task_struct *curr = current;
3818
Lai Jiangshan2b3fc352010-04-20 16:23:07 +08003819#ifndef CONFIG_PROVE_RCU_REPEATEDLY
Paul E. McKenney0632eb32010-02-22 17:04:47 -08003820 if (!debug_locks_off())
3821 return;
Lai Jiangshan2b3fc352010-04-20 16:23:07 +08003822#endif /* #ifdef CONFIG_PROVE_RCU_REPEATEDLY */
3823 /* Note: the following can be executed concurrently, so be careful. */
Paul E. McKenney056ba4a2010-02-25 14:06:46 -08003824 printk("\n===================================================\n");
3825 printk( "[ INFO: suspicious rcu_dereference_check() usage. ]\n");
3826 printk( "---------------------------------------------------\n");
Paul E. McKenney0632eb32010-02-22 17:04:47 -08003827 printk("%s:%d invoked rcu_dereference_check() without protection!\n",
3828 file, line);
3829 printk("\nother info that might help us debug this:\n\n");
Paul E. McKenneycc5b83a2010-03-03 07:46:59 -08003830 printk("\nrcu_scheduler_active = %d, debug_locks = %d\n", rcu_scheduler_active, debug_locks);
Paul E. McKenney0632eb32010-02-22 17:04:47 -08003831 lockdep_print_held_locks(curr);
3832 printk("\nstack backtrace:\n");
3833 dump_stack();
3834}
3835EXPORT_SYMBOL_GPL(lockdep_rcu_dereference);