blob: 367f72452ff4abbe0d036d5b2b5b85601ac39963 [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{
149 return cpu_clock(smp_processor_id());
150}
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 */
434atomic_t chain_lookup_hits;
435atomic_t chain_lookup_misses;
436atomic_t hardirqs_on_events;
437atomic_t hardirqs_off_events;
438atomic_t redundant_hardirqs_on;
439atomic_t redundant_hardirqs_off;
440atomic_t softirqs_on_events;
441atomic_t softirqs_off_events;
442atomic_t redundant_softirqs_on;
443atomic_t redundant_softirqs_off;
444atomic_t nr_unused_locks;
445atomic_t nr_cyclic_checks;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700446atomic_t nr_find_usage_forwards_checks;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700447atomic_t nr_find_usage_backwards_checks;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700448#endif
449
450/*
451 * Locking printouts:
452 */
453
Peter Zijlstrafabe9c42009-01-22 14:51:01 +0100454#define __USAGE(__STATE) \
Peter Zijlstrab4b136f2009-01-29 14:50:36 +0100455 [LOCK_USED_IN_##__STATE] = "IN-"__stringify(__STATE)"-W", \
456 [LOCK_ENABLED_##__STATE] = __stringify(__STATE)"-ON-W", \
457 [LOCK_USED_IN_##__STATE##_READ] = "IN-"__stringify(__STATE)"-R",\
458 [LOCK_ENABLED_##__STATE##_READ] = __stringify(__STATE)"-ON-R",
Peter Zijlstrafabe9c42009-01-22 14:51:01 +0100459
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700460static const char *usage_str[] =
461{
Peter Zijlstrafabe9c42009-01-22 14:51:01 +0100462#define LOCKDEP_STATE(__STATE) __USAGE(__STATE)
463#include "lockdep_states.h"
464#undef LOCKDEP_STATE
465 [LOCK_USED] = "INITIAL USE",
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700466};
467
468const char * __get_key_name(struct lockdep_subclass_key *key, char *str)
469{
Alexey Dobriyanffb45122007-05-08 00:28:41 -0700470 return kallsyms_lookup((unsigned long)key, NULL, NULL, NULL, str);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700471}
472
Peter Zijlstra3ff176c2009-01-22 17:40:42 +0100473static inline unsigned long lock_flag(enum lock_usage_bit bit)
474{
475 return 1UL << bit;
476}
477
478static char get_usage_char(struct lock_class *class, enum lock_usage_bit bit)
479{
480 char c = '.';
481
482 if (class->usage_mask & lock_flag(bit + 2))
483 c = '+';
484 if (class->usage_mask & lock_flag(bit)) {
485 c = '-';
486 if (class->usage_mask & lock_flag(bit + 2))
487 c = '?';
488 }
489
490 return c;
491}
492
Peter Zijlstraf510b232009-01-22 17:53:47 +0100493void get_usage_chars(struct lock_class *class, char usage[LOCK_USAGE_CHARS])
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700494{
Peter Zijlstraf510b232009-01-22 17:53:47 +0100495 int i = 0;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700496
Peter Zijlstraf510b232009-01-22 17:53:47 +0100497#define LOCKDEP_STATE(__STATE) \
498 usage[i++] = get_usage_char(class, LOCK_USED_IN_##__STATE); \
499 usage[i++] = get_usage_char(class, LOCK_USED_IN_##__STATE##_READ);
500#include "lockdep_states.h"
501#undef LOCKDEP_STATE
502
503 usage[i] = '\0';
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700504}
505
506static void print_lock_name(struct lock_class *class)
507{
Peter Zijlstraf510b232009-01-22 17:53:47 +0100508 char str[KSYM_NAME_LEN], usage[LOCK_USAGE_CHARS];
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700509 const char *name;
510
Peter Zijlstraf510b232009-01-22 17:53:47 +0100511 get_usage_chars(class, usage);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700512
513 name = class->name;
514 if (!name) {
515 name = __get_key_name(class->key, str);
516 printk(" (%s", name);
517 } else {
518 printk(" (%s", name);
519 if (class->name_version > 1)
520 printk("#%d", class->name_version);
521 if (class->subclass)
522 printk("/%d", class->subclass);
523 }
Peter Zijlstraf510b232009-01-22 17:53:47 +0100524 printk("){%s}", usage);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700525}
526
527static void print_lockdep_cache(struct lockdep_map *lock)
528{
529 const char *name;
Tejun Heo9281ace2007-07-17 04:03:51 -0700530 char str[KSYM_NAME_LEN];
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700531
532 name = lock->name;
533 if (!name)
534 name = __get_key_name(lock->key->subkeys, str);
535
536 printk("%s", name);
537}
538
539static void print_lock(struct held_lock *hlock)
540{
Dave Jonesf82b2172008-08-11 09:30:23 +0200541 print_lock_name(hlock_class(hlock));
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700542 printk(", at: ");
543 print_ip_sym(hlock->acquire_ip);
544}
545
546static void lockdep_print_held_locks(struct task_struct *curr)
547{
548 int i, depth = curr->lockdep_depth;
549
550 if (!depth) {
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -0700551 printk("no locks held by %s/%d.\n", curr->comm, task_pid_nr(curr));
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700552 return;
553 }
554 printk("%d lock%s held by %s/%d:\n",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -0700555 depth, depth > 1 ? "s" : "", curr->comm, task_pid_nr(curr));
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700556
557 for (i = 0; i < depth; i++) {
558 printk(" #%d: ", i);
559 print_lock(curr->held_locks + i);
560 }
561}
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700562
Dave Jones99de0552006-09-29 02:00:10 -0700563static void print_kernel_version(void)
564{
Serge E. Hallyn96b644b2006-10-02 02:18:13 -0700565 printk("%s %.*s\n", init_utsname()->release,
566 (int)strcspn(init_utsname()->version, " "),
567 init_utsname()->version);
Dave Jones99de0552006-09-29 02:00:10 -0700568}
569
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700570static int very_verbose(struct lock_class *class)
571{
572#if VERY_VERBOSE
573 return class_filter(class);
574#endif
575 return 0;
576}
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700577
578/*
579 * Is this the address of a static object:
580 */
581static int static_obj(void *obj)
582{
583 unsigned long start = (unsigned long) &_stext,
584 end = (unsigned long) &_end,
585 addr = (unsigned long) obj;
586#ifdef CONFIG_SMP
587 int i;
588#endif
589
590 /*
591 * static variable?
592 */
593 if ((addr >= start) && (addr < end))
594 return 1;
595
Mike Frysinger2a9ad182009-09-22 16:44:16 -0700596 if (arch_is_kernel_data(addr))
597 return 1;
598
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700599#ifdef CONFIG_SMP
600 /*
601 * percpu var?
602 */
603 for_each_possible_cpu(i) {
604 start = (unsigned long) &__per_cpu_start + per_cpu_offset(i);
Ingo Molnar1ff56832006-11-17 19:57:22 +0100605 end = (unsigned long) &__per_cpu_start + PERCPU_ENOUGH_ROOM
606 + per_cpu_offset(i);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700607
608 if ((addr >= start) && (addr < end))
609 return 1;
610 }
611#endif
612
613 /*
614 * module var?
615 */
616 return is_module_address(addr);
617}
618
619/*
620 * To make lock name printouts unique, we calculate a unique
621 * class->name_version generation counter:
622 */
623static int count_matching_names(struct lock_class *new_class)
624{
625 struct lock_class *class;
626 int count = 0;
627
628 if (!new_class->name)
629 return 0;
630
631 list_for_each_entry(class, &all_lock_classes, lock_entry) {
632 if (new_class->key - new_class->subclass == class->key)
633 return class->name_version;
634 if (class->name && !strcmp(class->name, new_class->name))
635 count = max(count, class->name_version);
636 }
637
638 return count + 1;
639}
640
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700641/*
642 * Register a lock's class in the hash-table, if the class is not present
643 * yet. Otherwise we look it up. We cache the result in the lock object
644 * itself, so actual lookup of the hash should be once per lock object.
645 */
646static inline struct lock_class *
Ingo Molnard6d897c2006-07-10 04:44:04 -0700647look_up_lock_class(struct lockdep_map *lock, unsigned int subclass)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700648{
649 struct lockdep_subclass_key *key;
650 struct list_head *hash_head;
651 struct lock_class *class;
652
653#ifdef CONFIG_DEBUG_LOCKDEP
654 /*
655 * If the architecture calls into lockdep before initializing
656 * the hashes then we'll warn about it later. (we cannot printk
657 * right now)
658 */
659 if (unlikely(!lockdep_initialized)) {
660 lockdep_init();
661 lockdep_init_error = 1;
Johannes Bergc71063c2007-07-19 01:49:02 -0700662 save_stack_trace(&lockdep_init_trace);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700663 }
664#endif
665
666 /*
667 * Static locks do not have their class-keys yet - for them the key
668 * is the lock object itself:
669 */
670 if (unlikely(!lock->key))
671 lock->key = (void *)lock;
672
673 /*
674 * NOTE: the class-key must be unique. For dynamic locks, a static
675 * lock_class_key variable is passed in through the mutex_init()
676 * (or spin_lock_init()) call - which acts as the key. For static
677 * locks we use the lock object itself as the key.
678 */
Peter Zijlstra4b32d0a2007-07-19 01:48:59 -0700679 BUILD_BUG_ON(sizeof(struct lock_class_key) >
680 sizeof(struct lockdep_map));
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700681
682 key = lock->key->subkeys + subclass;
683
684 hash_head = classhashentry(key);
685
686 /*
687 * We can walk the hash lockfree, because the hash only
688 * grows, and we are careful when adding entries to the end:
689 */
Peter Zijlstra4b32d0a2007-07-19 01:48:59 -0700690 list_for_each_entry(class, hash_head, hash_entry) {
691 if (class->key == key) {
692 WARN_ON_ONCE(class->name != lock->name);
Ingo Molnard6d897c2006-07-10 04:44:04 -0700693 return class;
Peter Zijlstra4b32d0a2007-07-19 01:48:59 -0700694 }
695 }
Ingo Molnard6d897c2006-07-10 04:44:04 -0700696
697 return NULL;
698}
699
700/*
701 * Register a lock's class in the hash-table, if the class is not present
702 * yet. Otherwise we look it up. We cache the result in the lock object
703 * itself, so actual lookup of the hash should be once per lock object.
704 */
705static inline struct lock_class *
Peter Zijlstra4dfbb9d2006-10-11 01:45:14 -0400706register_lock_class(struct lockdep_map *lock, unsigned int subclass, int force)
Ingo Molnard6d897c2006-07-10 04:44:04 -0700707{
708 struct lockdep_subclass_key *key;
709 struct list_head *hash_head;
710 struct lock_class *class;
Ingo Molnar70e45062006-12-06 20:40:50 -0800711 unsigned long flags;
Ingo Molnard6d897c2006-07-10 04:44:04 -0700712
713 class = look_up_lock_class(lock, subclass);
714 if (likely(class))
715 return class;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700716
717 /*
718 * Debug-check: all keys must be persistent!
719 */
720 if (!static_obj(lock->key)) {
721 debug_locks_off();
722 printk("INFO: trying to register non-static key.\n");
723 printk("the code is fine but needs lockdep annotation.\n");
724 printk("turning off the locking correctness validator.\n");
725 dump_stack();
726
727 return NULL;
728 }
729
Ingo Molnard6d897c2006-07-10 04:44:04 -0700730 key = lock->key->subkeys + subclass;
731 hash_head = classhashentry(key);
732
Ingo Molnar70e45062006-12-06 20:40:50 -0800733 raw_local_irq_save(flags);
Ingo Molnar74c383f2006-12-13 00:34:43 -0800734 if (!graph_lock()) {
735 raw_local_irq_restore(flags);
736 return NULL;
737 }
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700738 /*
739 * We have to do the hash-walk again, to avoid races
740 * with another CPU:
741 */
742 list_for_each_entry(class, hash_head, hash_entry)
743 if (class->key == key)
744 goto out_unlock_set;
745 /*
746 * Allocate a new key from the static array, and add it to
747 * the hash:
748 */
749 if (nr_lock_classes >= MAX_LOCKDEP_KEYS) {
Ingo Molnar74c383f2006-12-13 00:34:43 -0800750 if (!debug_locks_off_graph_unlock()) {
751 raw_local_irq_restore(flags);
752 return NULL;
753 }
Ingo Molnar70e45062006-12-06 20:40:50 -0800754 raw_local_irq_restore(flags);
Ingo Molnar74c383f2006-12-13 00:34:43 -0800755
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700756 printk("BUG: MAX_LOCKDEP_KEYS too low!\n");
757 printk("turning off the locking correctness validator.\n");
Peter Zijlstraeedeeab2009-03-18 12:38:47 +0100758 dump_stack();
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700759 return NULL;
760 }
761 class = lock_classes + nr_lock_classes++;
762 debug_atomic_inc(&nr_unused_locks);
763 class->key = key;
764 class->name = lock->name;
765 class->subclass = subclass;
766 INIT_LIST_HEAD(&class->lock_entry);
767 INIT_LIST_HEAD(&class->locks_before);
768 INIT_LIST_HEAD(&class->locks_after);
769 class->name_version = count_matching_names(class);
770 /*
771 * We use RCU's safe list-add method to make
772 * parallel walking of the hash-list safe:
773 */
774 list_add_tail_rcu(&class->hash_entry, hash_head);
Dale Farnsworth14811972008-02-25 23:03:02 +0100775 /*
776 * Add it to the global list of classes:
777 */
778 list_add_tail_rcu(&class->lock_entry, &all_lock_classes);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700779
780 if (verbose(class)) {
Ingo Molnar74c383f2006-12-13 00:34:43 -0800781 graph_unlock();
Ingo Molnar70e45062006-12-06 20:40:50 -0800782 raw_local_irq_restore(flags);
Ingo Molnar74c383f2006-12-13 00:34:43 -0800783
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700784 printk("\nnew class %p: %s", class->key, class->name);
785 if (class->name_version > 1)
786 printk("#%d", class->name_version);
787 printk("\n");
788 dump_stack();
Ingo Molnar74c383f2006-12-13 00:34:43 -0800789
Ingo Molnar70e45062006-12-06 20:40:50 -0800790 raw_local_irq_save(flags);
Ingo Molnar74c383f2006-12-13 00:34:43 -0800791 if (!graph_lock()) {
792 raw_local_irq_restore(flags);
793 return NULL;
794 }
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700795 }
796out_unlock_set:
Ingo Molnar74c383f2006-12-13 00:34:43 -0800797 graph_unlock();
Ingo Molnar70e45062006-12-06 20:40:50 -0800798 raw_local_irq_restore(flags);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700799
Peter Zijlstra4dfbb9d2006-10-11 01:45:14 -0400800 if (!subclass || force)
Ingo Molnard6d897c2006-07-10 04:44:04 -0700801 lock->class_cache = class;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700802
Jarek Poplawski381a2292007-02-10 01:44:58 -0800803 if (DEBUG_LOCKS_WARN_ON(class->subclass != subclass))
804 return NULL;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700805
806 return class;
807}
808
Peter Zijlstraca58abc2007-07-19 01:48:53 -0700809#ifdef CONFIG_PROVE_LOCKING
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700810/*
Peter Zijlstra8e182572007-07-19 01:48:54 -0700811 * Allocate a lockdep entry. (assumes the graph_lock held, returns
812 * with NULL on failure)
813 */
814static struct lock_list *alloc_list_entry(void)
815{
816 if (nr_list_entries >= MAX_LOCKDEP_ENTRIES) {
817 if (!debug_locks_off_graph_unlock())
818 return NULL;
819
820 printk("BUG: MAX_LOCKDEP_ENTRIES too low!\n");
821 printk("turning off the locking correctness validator.\n");
Peter Zijlstraeedeeab2009-03-18 12:38:47 +0100822 dump_stack();
Peter Zijlstra8e182572007-07-19 01:48:54 -0700823 return NULL;
824 }
825 return list_entries + nr_list_entries++;
826}
827
828/*
829 * Add a new dependency to the head of the list:
830 */
831static int add_lock_to_list(struct lock_class *class, struct lock_class *this,
832 struct list_head *head, unsigned long ip, int distance)
833{
834 struct lock_list *entry;
835 /*
836 * Lock not present yet - get a new dependency struct and
837 * add it to the list:
838 */
839 entry = alloc_list_entry();
840 if (!entry)
841 return 0;
842
Peter Zijlstra8e182572007-07-19 01:48:54 -0700843 if (!save_trace(&entry->trace))
844 return 0;
845
Zhu Yi74870172008-08-27 14:33:00 +0800846 entry->class = this;
847 entry->distance = distance;
Peter Zijlstra8e182572007-07-19 01:48:54 -0700848 /*
849 * Since we never remove from the dependency list, the list can
850 * be walked lockless by other CPUs, it's only allocation
851 * that must be protected by the spinlock. But this also means
852 * we must make new entries visible only once writes to the
853 * entry become visible - hence the RCU op:
854 */
855 list_add_tail_rcu(&entry->entry, head);
856
857 return 1;
858}
859
Peter Zijlstra98c33ed2009-07-21 13:19:07 +0200860/*
861 * For good efficiency of modular, we use power of 2
862 */
Peter Zijlstraaf012962009-07-16 15:44:29 +0200863#define MAX_CIRCULAR_QUEUE_SIZE 4096UL
864#define CQ_MASK (MAX_CIRCULAR_QUEUE_SIZE-1)
865
Peter Zijlstra98c33ed2009-07-21 13:19:07 +0200866/*
867 * The circular_queue and helpers is used to implement the
Peter Zijlstraaf012962009-07-16 15:44:29 +0200868 * breadth-first search(BFS)algorithem, by which we can build
869 * the shortest path from the next lock to be acquired to the
870 * previous held lock if there is a circular between them.
Peter Zijlstra98c33ed2009-07-21 13:19:07 +0200871 */
Peter Zijlstraaf012962009-07-16 15:44:29 +0200872struct circular_queue {
873 unsigned long element[MAX_CIRCULAR_QUEUE_SIZE];
874 unsigned int front, rear;
875};
876
877static struct circular_queue lock_cq;
Peter Zijlstraaf012962009-07-16 15:44:29 +0200878
Ming Lei12f3dfd2009-07-16 15:44:29 +0200879unsigned int max_bfs_queue_depth;
Peter Zijlstraaf012962009-07-16 15:44:29 +0200880
Ming Leie351b662009-07-22 22:48:09 +0800881static unsigned int lockdep_dependency_gen_id;
882
Peter Zijlstraaf012962009-07-16 15:44:29 +0200883static inline void __cq_init(struct circular_queue *cq)
884{
885 cq->front = cq->rear = 0;
Ming Leie351b662009-07-22 22:48:09 +0800886 lockdep_dependency_gen_id++;
Peter Zijlstraaf012962009-07-16 15:44:29 +0200887}
888
889static inline int __cq_empty(struct circular_queue *cq)
890{
891 return (cq->front == cq->rear);
892}
893
894static inline int __cq_full(struct circular_queue *cq)
895{
896 return ((cq->rear + 1) & CQ_MASK) == cq->front;
897}
898
899static inline int __cq_enqueue(struct circular_queue *cq, unsigned long elem)
900{
901 if (__cq_full(cq))
902 return -1;
903
904 cq->element[cq->rear] = elem;
905 cq->rear = (cq->rear + 1) & CQ_MASK;
906 return 0;
907}
908
909static inline int __cq_dequeue(struct circular_queue *cq, unsigned long *elem)
910{
911 if (__cq_empty(cq))
912 return -1;
913
914 *elem = cq->element[cq->front];
915 cq->front = (cq->front + 1) & CQ_MASK;
916 return 0;
917}
918
919static inline unsigned int __cq_get_elem_count(struct circular_queue *cq)
920{
921 return (cq->rear - cq->front) & CQ_MASK;
922}
923
924static inline void mark_lock_accessed(struct lock_list *lock,
925 struct lock_list *parent)
926{
927 unsigned long nr;
Peter Zijlstra98c33ed2009-07-21 13:19:07 +0200928
Peter Zijlstraaf012962009-07-16 15:44:29 +0200929 nr = lock - list_entries;
930 WARN_ON(nr >= nr_list_entries);
931 lock->parent = parent;
Ming Leie351b662009-07-22 22:48:09 +0800932 lock->class->dep_gen_id = lockdep_dependency_gen_id;
Peter Zijlstraaf012962009-07-16 15:44:29 +0200933}
934
935static inline unsigned long lock_accessed(struct lock_list *lock)
936{
937 unsigned long nr;
Peter Zijlstra98c33ed2009-07-21 13:19:07 +0200938
Peter Zijlstraaf012962009-07-16 15:44:29 +0200939 nr = lock - list_entries;
940 WARN_ON(nr >= nr_list_entries);
Ming Leie351b662009-07-22 22:48:09 +0800941 return lock->class->dep_gen_id == lockdep_dependency_gen_id;
Peter Zijlstraaf012962009-07-16 15:44:29 +0200942}
943
944static inline struct lock_list *get_lock_parent(struct lock_list *child)
945{
946 return child->parent;
947}
948
949static inline int get_lock_depth(struct lock_list *child)
950{
951 int depth = 0;
952 struct lock_list *parent;
953
954 while ((parent = get_lock_parent(child))) {
955 child = parent;
956 depth++;
957 }
958 return depth;
959}
960
Ming Lei9e2d5512009-07-16 15:44:29 +0200961static int __bfs(struct lock_list *source_entry,
Peter Zijlstraaf012962009-07-16 15:44:29 +0200962 void *data,
963 int (*match)(struct lock_list *entry, void *data),
964 struct lock_list **target_entry,
965 int forward)
Ming Leic94aa5c2009-07-16 15:44:29 +0200966{
967 struct lock_list *entry;
Ming Leid588e462009-07-16 15:44:29 +0200968 struct list_head *head;
Ming Leic94aa5c2009-07-16 15:44:29 +0200969 struct circular_queue *cq = &lock_cq;
970 int ret = 1;
971
Ming Lei9e2d5512009-07-16 15:44:29 +0200972 if (match(source_entry, data)) {
Ming Leic94aa5c2009-07-16 15:44:29 +0200973 *target_entry = source_entry;
974 ret = 0;
975 goto exit;
976 }
977
Ming Leid588e462009-07-16 15:44:29 +0200978 if (forward)
979 head = &source_entry->class->locks_after;
980 else
981 head = &source_entry->class->locks_before;
982
983 if (list_empty(head))
984 goto exit;
985
986 __cq_init(cq);
Ming Leic94aa5c2009-07-16 15:44:29 +0200987 __cq_enqueue(cq, (unsigned long)source_entry);
988
989 while (!__cq_empty(cq)) {
990 struct lock_list *lock;
Ming Leic94aa5c2009-07-16 15:44:29 +0200991
992 __cq_dequeue(cq, (unsigned long *)&lock);
993
994 if (!lock->class) {
995 ret = -2;
996 goto exit;
997 }
998
999 if (forward)
1000 head = &lock->class->locks_after;
1001 else
1002 head = &lock->class->locks_before;
1003
1004 list_for_each_entry(entry, head, entry) {
1005 if (!lock_accessed(entry)) {
Ming Lei12f3dfd2009-07-16 15:44:29 +02001006 unsigned int cq_depth;
Ming Leic94aa5c2009-07-16 15:44:29 +02001007 mark_lock_accessed(entry, lock);
Ming Lei9e2d5512009-07-16 15:44:29 +02001008 if (match(entry, data)) {
Ming Leic94aa5c2009-07-16 15:44:29 +02001009 *target_entry = entry;
1010 ret = 0;
1011 goto exit;
1012 }
1013
1014 if (__cq_enqueue(cq, (unsigned long)entry)) {
1015 ret = -1;
1016 goto exit;
1017 }
Ming Lei12f3dfd2009-07-16 15:44:29 +02001018 cq_depth = __cq_get_elem_count(cq);
1019 if (max_bfs_queue_depth < cq_depth)
1020 max_bfs_queue_depth = cq_depth;
Ming Leic94aa5c2009-07-16 15:44:29 +02001021 }
1022 }
1023 }
1024exit:
1025 return ret;
1026}
1027
Ming Leid7aaba12009-07-16 15:44:29 +02001028static inline int __bfs_forwards(struct lock_list *src_entry,
Ming Lei9e2d5512009-07-16 15:44:29 +02001029 void *data,
1030 int (*match)(struct lock_list *entry, void *data),
1031 struct lock_list **target_entry)
Ming Leic94aa5c2009-07-16 15:44:29 +02001032{
Ming Lei9e2d5512009-07-16 15:44:29 +02001033 return __bfs(src_entry, data, match, target_entry, 1);
Ming Leic94aa5c2009-07-16 15:44:29 +02001034
1035}
1036
Ming Leid7aaba12009-07-16 15:44:29 +02001037static inline int __bfs_backwards(struct lock_list *src_entry,
Ming Lei9e2d5512009-07-16 15:44:29 +02001038 void *data,
1039 int (*match)(struct lock_list *entry, void *data),
1040 struct lock_list **target_entry)
Ming Leic94aa5c2009-07-16 15:44:29 +02001041{
Ming Lei9e2d5512009-07-16 15:44:29 +02001042 return __bfs(src_entry, data, match, target_entry, 0);
Ming Leic94aa5c2009-07-16 15:44:29 +02001043
1044}
1045
Peter Zijlstra8e182572007-07-19 01:48:54 -07001046/*
1047 * Recursive, forwards-direction lock-dependency checking, used for
1048 * both noncyclic checking and for hardirq-unsafe/softirq-unsafe
1049 * checking.
Peter Zijlstra8e182572007-07-19 01:48:54 -07001050 */
Peter Zijlstra8e182572007-07-19 01:48:54 -07001051
1052/*
1053 * Print a dependency chain entry (this is only done when a deadlock
1054 * has been detected):
1055 */
1056static noinline int
Ming Lei24208ca2009-07-16 15:44:29 +02001057print_circular_bug_entry(struct lock_list *target, int depth)
Peter Zijlstra8e182572007-07-19 01:48:54 -07001058{
1059 if (debug_locks_silent)
1060 return 0;
1061 printk("\n-> #%u", depth);
1062 print_lock_name(target->class);
1063 printk(":\n");
1064 print_stack_trace(&target->trace, 6);
1065
1066 return 0;
1067}
1068
1069/*
1070 * When a circular dependency is detected, print the
1071 * header first:
1072 */
1073static noinline int
Ming Leidb0002a2009-07-16 15:44:29 +02001074print_circular_bug_header(struct lock_list *entry, unsigned int depth,
1075 struct held_lock *check_src,
1076 struct held_lock *check_tgt)
Peter Zijlstra8e182572007-07-19 01:48:54 -07001077{
1078 struct task_struct *curr = current;
1079
Ming Leic94aa5c2009-07-16 15:44:29 +02001080 if (debug_locks_silent)
Peter Zijlstra8e182572007-07-19 01:48:54 -07001081 return 0;
1082
1083 printk("\n=======================================================\n");
1084 printk( "[ INFO: possible circular locking dependency detected ]\n");
1085 print_kernel_version();
1086 printk( "-------------------------------------------------------\n");
1087 printk("%s/%d is trying to acquire lock:\n",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07001088 curr->comm, task_pid_nr(curr));
Ming Leidb0002a2009-07-16 15:44:29 +02001089 print_lock(check_src);
Peter Zijlstra8e182572007-07-19 01:48:54 -07001090 printk("\nbut task is already holding lock:\n");
Ming Leidb0002a2009-07-16 15:44:29 +02001091 print_lock(check_tgt);
Peter Zijlstra8e182572007-07-19 01:48:54 -07001092 printk("\nwhich lock already depends on the new lock.\n\n");
1093 printk("\nthe existing dependency chain (in reverse order) is:\n");
1094
1095 print_circular_bug_entry(entry, depth);
1096
1097 return 0;
1098}
1099
Ming Lei9e2d5512009-07-16 15:44:29 +02001100static inline int class_equal(struct lock_list *entry, void *data)
1101{
1102 return entry->class == data;
1103}
1104
Ming Leidb0002a2009-07-16 15:44:29 +02001105static noinline int print_circular_bug(struct lock_list *this,
1106 struct lock_list *target,
1107 struct held_lock *check_src,
1108 struct held_lock *check_tgt)
Peter Zijlstra8e182572007-07-19 01:48:54 -07001109{
1110 struct task_struct *curr = current;
Ming Leic94aa5c2009-07-16 15:44:29 +02001111 struct lock_list *parent;
Ming Lei24208ca2009-07-16 15:44:29 +02001112 int depth;
Peter Zijlstra8e182572007-07-19 01:48:54 -07001113
Ming Leic94aa5c2009-07-16 15:44:29 +02001114 if (!debug_locks_off_graph_unlock() || debug_locks_silent)
Peter Zijlstra8e182572007-07-19 01:48:54 -07001115 return 0;
1116
Ming Leidb0002a2009-07-16 15:44:29 +02001117 if (!save_trace(&this->trace))
Peter Zijlstra8e182572007-07-19 01:48:54 -07001118 return 0;
1119
Ming Leic94aa5c2009-07-16 15:44:29 +02001120 depth = get_lock_depth(target);
1121
Ming Leidb0002a2009-07-16 15:44:29 +02001122 print_circular_bug_header(target, depth, check_src, check_tgt);
Ming Leic94aa5c2009-07-16 15:44:29 +02001123
1124 parent = get_lock_parent(target);
1125
1126 while (parent) {
1127 print_circular_bug_entry(parent, --depth);
1128 parent = get_lock_parent(parent);
1129 }
Peter Zijlstra8e182572007-07-19 01:48:54 -07001130
1131 printk("\nother info that might help us debug this:\n\n");
1132 lockdep_print_held_locks(curr);
1133
1134 printk("\nstack backtrace:\n");
1135 dump_stack();
1136
1137 return 0;
1138}
1139
Ming Leidb0002a2009-07-16 15:44:29 +02001140static noinline int print_bfs_bug(int ret)
1141{
1142 if (!debug_locks_off_graph_unlock())
1143 return 0;
1144
1145 WARN(1, "lockdep bfs error:%d\n", ret);
1146
1147 return 0;
1148}
1149
Ming Leief681022009-07-16 15:44:29 +02001150static int noop_count(struct lock_list *entry, void *data)
David Miller419ca3f2008-07-29 21:45:03 -07001151{
Ming Leief681022009-07-16 15:44:29 +02001152 (*(unsigned long *)data)++;
1153 return 0;
David Miller419ca3f2008-07-29 21:45:03 -07001154}
1155
Ming Leief681022009-07-16 15:44:29 +02001156unsigned long __lockdep_count_forward_deps(struct lock_list *this)
1157{
1158 unsigned long count = 0;
1159 struct lock_list *uninitialized_var(target_entry);
1160
1161 __bfs_forwards(this, (void *)&count, noop_count, &target_entry);
1162
1163 return count;
1164}
David Miller419ca3f2008-07-29 21:45:03 -07001165unsigned long lockdep_count_forward_deps(struct lock_class *class)
1166{
1167 unsigned long ret, flags;
Ming Leief681022009-07-16 15:44:29 +02001168 struct lock_list this;
1169
1170 this.parent = NULL;
1171 this.class = class;
David Miller419ca3f2008-07-29 21:45:03 -07001172
1173 local_irq_save(flags);
Thomas Gleixner0199c4e2009-12-02 20:01:25 +01001174 arch_spin_lock(&lockdep_lock);
Ming Leief681022009-07-16 15:44:29 +02001175 ret = __lockdep_count_forward_deps(&this);
Thomas Gleixner0199c4e2009-12-02 20:01:25 +01001176 arch_spin_unlock(&lockdep_lock);
David Miller419ca3f2008-07-29 21:45:03 -07001177 local_irq_restore(flags);
1178
1179 return ret;
1180}
1181
Ming Leief681022009-07-16 15:44:29 +02001182unsigned long __lockdep_count_backward_deps(struct lock_list *this)
David Miller419ca3f2008-07-29 21:45:03 -07001183{
Ming Leief681022009-07-16 15:44:29 +02001184 unsigned long count = 0;
1185 struct lock_list *uninitialized_var(target_entry);
David Miller419ca3f2008-07-29 21:45:03 -07001186
Ming Leief681022009-07-16 15:44:29 +02001187 __bfs_backwards(this, (void *)&count, noop_count, &target_entry);
David Miller419ca3f2008-07-29 21:45:03 -07001188
Ming Leief681022009-07-16 15:44:29 +02001189 return count;
David Miller419ca3f2008-07-29 21:45:03 -07001190}
1191
1192unsigned long lockdep_count_backward_deps(struct lock_class *class)
1193{
1194 unsigned long ret, flags;
Ming Leief681022009-07-16 15:44:29 +02001195 struct lock_list this;
1196
1197 this.parent = NULL;
1198 this.class = class;
David Miller419ca3f2008-07-29 21:45:03 -07001199
1200 local_irq_save(flags);
Thomas Gleixner0199c4e2009-12-02 20:01:25 +01001201 arch_spin_lock(&lockdep_lock);
Ming Leief681022009-07-16 15:44:29 +02001202 ret = __lockdep_count_backward_deps(&this);
Thomas Gleixner0199c4e2009-12-02 20:01:25 +01001203 arch_spin_unlock(&lockdep_lock);
David Miller419ca3f2008-07-29 21:45:03 -07001204 local_irq_restore(flags);
1205
1206 return ret;
1207}
1208
Peter Zijlstra8e182572007-07-19 01:48:54 -07001209/*
1210 * Prove that the dependency graph starting at <entry> can not
1211 * lead to <target>. Print an error and return 0 if it does.
1212 */
1213static noinline int
Ming Leidb0002a2009-07-16 15:44:29 +02001214check_noncircular(struct lock_list *root, struct lock_class *target,
1215 struct lock_list **target_entry)
Peter Zijlstra8e182572007-07-19 01:48:54 -07001216{
Ming Leidb0002a2009-07-16 15:44:29 +02001217 int result;
Peter Zijlstra8e182572007-07-19 01:48:54 -07001218
Ming Leidb0002a2009-07-16 15:44:29 +02001219 debug_atomic_inc(&nr_cyclic_checks);
David Miller419ca3f2008-07-29 21:45:03 -07001220
Ming Leid7aaba12009-07-16 15:44:29 +02001221 result = __bfs_forwards(root, target, class_equal, target_entry);
Ming Leidb0002a2009-07-16 15:44:29 +02001222
1223 return result;
Peter Zijlstra8e182572007-07-19 01:48:54 -07001224}
1225
Steven Rostedt81d68a92008-05-12 21:20:42 +02001226#if defined(CONFIG_TRACE_IRQFLAGS) && defined(CONFIG_PROVE_LOCKING)
Peter Zijlstra8e182572007-07-19 01:48:54 -07001227/*
1228 * Forwards and backwards subgraph searching, for the purposes of
1229 * proving that two subgraphs can be connected by a new dependency
1230 * without creating any illegal irq-safe -> irq-unsafe lock dependency.
1231 */
Peter Zijlstra8e182572007-07-19 01:48:54 -07001232
Ming Leid7aaba12009-07-16 15:44:29 +02001233static inline int usage_match(struct lock_list *entry, void *bit)
1234{
1235 return entry->class->usage_mask & (1 << (enum lock_usage_bit)bit);
1236}
1237
1238
1239
Peter Zijlstra8e182572007-07-19 01:48:54 -07001240/*
1241 * Find a node in the forwards-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_forwards(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
1256 debug_atomic_inc(&nr_find_usage_forwards_checks);
Peter Zijlstra8e182572007-07-19 01:48:54 -07001257
Ming Leid7aaba12009-07-16 15:44:29 +02001258 result = __bfs_forwards(root, (void *)bit, usage_match, target_entry);
1259
1260 return result;
Peter Zijlstra8e182572007-07-19 01:48:54 -07001261}
1262
1263/*
1264 * Find a node in the backwards-direction dependency sub-graph starting
Ming Leid7aaba12009-07-16 15:44:29 +02001265 * at @root->class that matches @bit.
Peter Zijlstra8e182572007-07-19 01:48:54 -07001266 *
Ming Leid7aaba12009-07-16 15:44:29 +02001267 * Return 0 if such a node exists in the subgraph, and put that node
1268 * into *@target_entry.
Peter Zijlstra8e182572007-07-19 01:48:54 -07001269 *
Ming Leid7aaba12009-07-16 15:44:29 +02001270 * Return 1 otherwise and keep *@target_entry unchanged.
1271 * Return <0 on error.
Peter Zijlstra8e182572007-07-19 01:48:54 -07001272 */
Ming Leid7aaba12009-07-16 15:44:29 +02001273static int
1274find_usage_backwards(struct lock_list *root, enum lock_usage_bit bit,
1275 struct lock_list **target_entry)
Peter Zijlstra8e182572007-07-19 01:48:54 -07001276{
Ming Leid7aaba12009-07-16 15:44:29 +02001277 int result;
Peter Zijlstra8e182572007-07-19 01:48:54 -07001278
1279 debug_atomic_inc(&nr_find_usage_backwards_checks);
Peter Zijlstra8e182572007-07-19 01:48:54 -07001280
Ming Leid7aaba12009-07-16 15:44:29 +02001281 result = __bfs_backwards(root, (void *)bit, usage_match, target_entry);
Dave Jonesf82b2172008-08-11 09:30:23 +02001282
Ming Leid7aaba12009-07-16 15:44:29 +02001283 return result;
Peter Zijlstra8e182572007-07-19 01:48:54 -07001284}
1285
Peter Zijlstraaf012962009-07-16 15:44:29 +02001286static void print_lock_class_header(struct lock_class *class, int depth)
1287{
1288 int bit;
1289
1290 printk("%*s->", depth, "");
1291 print_lock_name(class);
1292 printk(" ops: %lu", class->ops);
1293 printk(" {\n");
1294
1295 for (bit = 0; bit < LOCK_USAGE_STATES; bit++) {
1296 if (class->usage_mask & (1 << bit)) {
1297 int len = depth;
1298
1299 len += printk("%*s %s", depth, "", usage_str[bit]);
1300 len += printk(" at:\n");
1301 print_stack_trace(class->usage_traces + bit, len);
1302 }
1303 }
1304 printk("%*s }\n", depth, "");
1305
1306 printk("%*s ... key at: ",depth,"");
1307 print_ip_sym((unsigned long)class->key);
1308}
1309
1310/*
1311 * printk the shortest lock dependencies from @start to @end in reverse order:
1312 */
1313static void __used
1314print_shortest_lock_dependencies(struct lock_list *leaf,
1315 struct lock_list *root)
1316{
1317 struct lock_list *entry = leaf;
1318 int depth;
1319
1320 /*compute depth from generated tree by BFS*/
1321 depth = get_lock_depth(leaf);
1322
1323 do {
1324 print_lock_class_header(entry->class, depth);
1325 printk("%*s ... acquired at:\n", depth, "");
1326 print_stack_trace(&entry->trace, 2);
1327 printk("\n");
1328
1329 if (depth == 0 && (entry != root)) {
1330 printk("lockdep:%s bad BFS generated tree\n", __func__);
1331 break;
1332 }
1333
1334 entry = get_lock_parent(entry);
1335 depth--;
1336 } while (entry && (depth >= 0));
1337
1338 return;
1339}
Ming Leid7aaba12009-07-16 15:44:29 +02001340
Peter Zijlstra8e182572007-07-19 01:48:54 -07001341static int
1342print_bad_irq_dependency(struct task_struct *curr,
Ming Lei24208ca2009-07-16 15:44:29 +02001343 struct lock_list *prev_root,
1344 struct lock_list *next_root,
1345 struct lock_list *backwards_entry,
1346 struct lock_list *forwards_entry,
Peter Zijlstra8e182572007-07-19 01:48:54 -07001347 struct held_lock *prev,
1348 struct held_lock *next,
1349 enum lock_usage_bit bit1,
1350 enum lock_usage_bit bit2,
1351 const char *irqclass)
1352{
1353 if (!debug_locks_off_graph_unlock() || debug_locks_silent)
1354 return 0;
1355
1356 printk("\n======================================================\n");
1357 printk( "[ INFO: %s-safe -> %s-unsafe lock order detected ]\n",
1358 irqclass, irqclass);
1359 print_kernel_version();
1360 printk( "------------------------------------------------------\n");
1361 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 -07001362 curr->comm, task_pid_nr(curr),
Peter Zijlstra8e182572007-07-19 01:48:54 -07001363 curr->hardirq_context, hardirq_count() >> HARDIRQ_SHIFT,
1364 curr->softirq_context, softirq_count() >> SOFTIRQ_SHIFT,
1365 curr->hardirqs_enabled,
1366 curr->softirqs_enabled);
1367 print_lock(next);
1368
1369 printk("\nand this task is already holding:\n");
1370 print_lock(prev);
1371 printk("which would create a new lock dependency:\n");
Dave Jonesf82b2172008-08-11 09:30:23 +02001372 print_lock_name(hlock_class(prev));
Peter Zijlstra8e182572007-07-19 01:48:54 -07001373 printk(" ->");
Dave Jonesf82b2172008-08-11 09:30:23 +02001374 print_lock_name(hlock_class(next));
Peter Zijlstra8e182572007-07-19 01:48:54 -07001375 printk("\n");
1376
1377 printk("\nbut this new dependency connects a %s-irq-safe lock:\n",
1378 irqclass);
Ming Lei24208ca2009-07-16 15:44:29 +02001379 print_lock_name(backwards_entry->class);
Peter Zijlstra8e182572007-07-19 01:48:54 -07001380 printk("\n... which became %s-irq-safe at:\n", irqclass);
1381
Ming Lei24208ca2009-07-16 15:44:29 +02001382 print_stack_trace(backwards_entry->class->usage_traces + bit1, 1);
Peter Zijlstra8e182572007-07-19 01:48:54 -07001383
1384 printk("\nto a %s-irq-unsafe lock:\n", irqclass);
Ming Lei24208ca2009-07-16 15:44:29 +02001385 print_lock_name(forwards_entry->class);
Peter Zijlstra8e182572007-07-19 01:48:54 -07001386 printk("\n... which became %s-irq-unsafe at:\n", irqclass);
1387 printk("...");
1388
Ming Lei24208ca2009-07-16 15:44:29 +02001389 print_stack_trace(forwards_entry->class->usage_traces + bit2, 1);
Peter Zijlstra8e182572007-07-19 01:48:54 -07001390
1391 printk("\nother info that might help us debug this:\n\n");
1392 lockdep_print_held_locks(curr);
1393
Ming Lei24208ca2009-07-16 15:44:29 +02001394 printk("\nthe dependencies between %s-irq-safe lock", irqclass);
1395 printk(" and the holding lock:\n");
1396 if (!save_trace(&prev_root->trace))
1397 return 0;
1398 print_shortest_lock_dependencies(backwards_entry, prev_root);
Peter Zijlstra8e182572007-07-19 01:48:54 -07001399
Ming Lei24208ca2009-07-16 15:44:29 +02001400 printk("\nthe dependencies between the lock to be acquired");
1401 printk(" and %s-irq-unsafe lock:\n", irqclass);
1402 if (!save_trace(&next_root->trace))
1403 return 0;
1404 print_shortest_lock_dependencies(forwards_entry, next_root);
Peter Zijlstra8e182572007-07-19 01:48:54 -07001405
1406 printk("\nstack backtrace:\n");
1407 dump_stack();
1408
1409 return 0;
1410}
1411
1412static int
1413check_usage(struct task_struct *curr, struct held_lock *prev,
1414 struct held_lock *next, enum lock_usage_bit bit_backwards,
1415 enum lock_usage_bit bit_forwards, const char *irqclass)
1416{
1417 int ret;
Ming Lei24208ca2009-07-16 15:44:29 +02001418 struct lock_list this, that;
Ming Leid7aaba12009-07-16 15:44:29 +02001419 struct lock_list *uninitialized_var(target_entry);
Ming Lei24208ca2009-07-16 15:44:29 +02001420 struct lock_list *uninitialized_var(target_entry1);
Peter Zijlstra8e182572007-07-19 01:48:54 -07001421
Ming Leid7aaba12009-07-16 15:44:29 +02001422 this.parent = NULL;
Peter Zijlstra8e182572007-07-19 01:48:54 -07001423
Ming Leid7aaba12009-07-16 15:44:29 +02001424 this.class = hlock_class(prev);
1425 ret = find_usage_backwards(&this, bit_backwards, &target_entry);
Peter Zijlstraaf012962009-07-16 15:44:29 +02001426 if (ret < 0)
1427 return print_bfs_bug(ret);
1428 if (ret == 1)
1429 return ret;
Ming Leid7aaba12009-07-16 15:44:29 +02001430
Ming Lei24208ca2009-07-16 15:44:29 +02001431 that.parent = NULL;
1432 that.class = hlock_class(next);
1433 ret = find_usage_forwards(&that, bit_forwards, &target_entry1);
Peter Zijlstraaf012962009-07-16 15:44:29 +02001434 if (ret < 0)
1435 return print_bfs_bug(ret);
1436 if (ret == 1)
1437 return ret;
Ming Leid7aaba12009-07-16 15:44:29 +02001438
Ming Lei24208ca2009-07-16 15:44:29 +02001439 return print_bad_irq_dependency(curr, &this, &that,
1440 target_entry, target_entry1,
1441 prev, next,
Peter Zijlstra8e182572007-07-19 01:48:54 -07001442 bit_backwards, bit_forwards, irqclass);
1443}
1444
Peter Zijlstra4f367d82009-01-22 18:10:42 +01001445static const char *state_names[] = {
1446#define LOCKDEP_STATE(__STATE) \
Peter Zijlstrab4b136f2009-01-29 14:50:36 +01001447 __stringify(__STATE),
Peter Zijlstra4f367d82009-01-22 18:10:42 +01001448#include "lockdep_states.h"
1449#undef LOCKDEP_STATE
1450};
1451
1452static const char *state_rnames[] = {
1453#define LOCKDEP_STATE(__STATE) \
Peter Zijlstrab4b136f2009-01-29 14:50:36 +01001454 __stringify(__STATE)"-READ",
Peter Zijlstra4f367d82009-01-22 18:10:42 +01001455#include "lockdep_states.h"
1456#undef LOCKDEP_STATE
1457};
1458
1459static inline const char *state_name(enum lock_usage_bit bit)
1460{
1461 return (bit & 1) ? state_rnames[bit >> 2] : state_names[bit >> 2];
1462}
1463
1464static int exclusive_bit(int new_bit)
1465{
1466 /*
1467 * USED_IN
1468 * USED_IN_READ
1469 * ENABLED
1470 * ENABLED_READ
1471 *
1472 * bit 0 - write/read
1473 * bit 1 - used_in/enabled
1474 * bit 2+ state
1475 */
1476
1477 int state = new_bit & ~3;
1478 int dir = new_bit & 2;
1479
1480 /*
1481 * keep state, bit flip the direction and strip read.
1482 */
1483 return state | (dir ^ 2);
1484}
1485
1486static int check_irq_usage(struct task_struct *curr, struct held_lock *prev,
1487 struct held_lock *next, enum lock_usage_bit bit)
Peter Zijlstra8e182572007-07-19 01:48:54 -07001488{
1489 /*
1490 * Prove that the new dependency does not connect a hardirq-safe
1491 * lock with a hardirq-unsafe lock - to achieve this we search
1492 * the backwards-subgraph starting at <prev>, and the
1493 * forwards-subgraph starting at <next>:
1494 */
Peter Zijlstra4f367d82009-01-22 18:10:42 +01001495 if (!check_usage(curr, prev, next, bit,
1496 exclusive_bit(bit), state_name(bit)))
Peter Zijlstra8e182572007-07-19 01:48:54 -07001497 return 0;
1498
Peter Zijlstra4f367d82009-01-22 18:10:42 +01001499 bit++; /* _READ */
1500
Peter Zijlstra8e182572007-07-19 01:48:54 -07001501 /*
1502 * Prove that the new dependency does not connect a hardirq-safe-read
1503 * lock with a hardirq-unsafe lock - to achieve this we search
1504 * the backwards-subgraph starting at <prev>, and the
1505 * forwards-subgraph starting at <next>:
1506 */
Peter Zijlstra4f367d82009-01-22 18:10:42 +01001507 if (!check_usage(curr, prev, next, bit,
1508 exclusive_bit(bit), state_name(bit)))
Peter Zijlstra8e182572007-07-19 01:48:54 -07001509 return 0;
1510
Peter Zijlstra4f367d82009-01-22 18:10:42 +01001511 return 1;
1512}
Peter Zijlstra8e182572007-07-19 01:48:54 -07001513
Peter Zijlstra4f367d82009-01-22 18:10:42 +01001514static int
1515check_prev_add_irq(struct task_struct *curr, struct held_lock *prev,
1516 struct held_lock *next)
1517{
1518#define LOCKDEP_STATE(__STATE) \
1519 if (!check_irq_usage(curr, prev, next, LOCK_USED_IN_##__STATE)) \
Nick Piggincf40bd12009-01-21 08:12:39 +01001520 return 0;
Peter Zijlstra4f367d82009-01-22 18:10:42 +01001521#include "lockdep_states.h"
1522#undef LOCKDEP_STATE
Nick Piggincf40bd12009-01-21 08:12:39 +01001523
Peter Zijlstra8e182572007-07-19 01:48:54 -07001524 return 1;
1525}
1526
1527static void inc_chains(void)
1528{
1529 if (current->hardirq_context)
1530 nr_hardirq_chains++;
1531 else {
1532 if (current->softirq_context)
1533 nr_softirq_chains++;
1534 else
1535 nr_process_chains++;
1536 }
1537}
1538
1539#else
1540
1541static inline int
1542check_prev_add_irq(struct task_struct *curr, struct held_lock *prev,
1543 struct held_lock *next)
1544{
1545 return 1;
1546}
1547
1548static inline void inc_chains(void)
1549{
1550 nr_process_chains++;
1551}
1552
1553#endif
1554
1555static int
1556print_deadlock_bug(struct task_struct *curr, struct held_lock *prev,
1557 struct held_lock *next)
1558{
1559 if (!debug_locks_off_graph_unlock() || debug_locks_silent)
1560 return 0;
1561
1562 printk("\n=============================================\n");
1563 printk( "[ INFO: possible recursive locking detected ]\n");
1564 print_kernel_version();
1565 printk( "---------------------------------------------\n");
1566 printk("%s/%d is trying to acquire lock:\n",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07001567 curr->comm, task_pid_nr(curr));
Peter Zijlstra8e182572007-07-19 01:48:54 -07001568 print_lock(next);
1569 printk("\nbut task is already holding lock:\n");
1570 print_lock(prev);
1571
1572 printk("\nother info that might help us debug this:\n");
1573 lockdep_print_held_locks(curr);
1574
1575 printk("\nstack backtrace:\n");
1576 dump_stack();
1577
1578 return 0;
1579}
1580
1581/*
1582 * Check whether we are holding such a class already.
1583 *
1584 * (Note that this has to be done separately, because the graph cannot
1585 * detect such classes of deadlocks.)
1586 *
1587 * Returns: 0 on deadlock detected, 1 on OK, 2 on recursive read
1588 */
1589static int
1590check_deadlock(struct task_struct *curr, struct held_lock *next,
1591 struct lockdep_map *next_instance, int read)
1592{
1593 struct held_lock *prev;
Peter Zijlstra7531e2f2008-08-11 09:30:24 +02001594 struct held_lock *nest = NULL;
Peter Zijlstra8e182572007-07-19 01:48:54 -07001595 int i;
1596
1597 for (i = 0; i < curr->lockdep_depth; i++) {
1598 prev = curr->held_locks + i;
Peter Zijlstra7531e2f2008-08-11 09:30:24 +02001599
1600 if (prev->instance == next->nest_lock)
1601 nest = prev;
1602
Dave Jonesf82b2172008-08-11 09:30:23 +02001603 if (hlock_class(prev) != hlock_class(next))
Peter Zijlstra8e182572007-07-19 01:48:54 -07001604 continue;
Peter Zijlstra7531e2f2008-08-11 09:30:24 +02001605
Peter Zijlstra8e182572007-07-19 01:48:54 -07001606 /*
1607 * Allow read-after-read recursion of the same
1608 * lock class (i.e. read_lock(lock)+read_lock(lock)):
1609 */
1610 if ((read == 2) && prev->read)
1611 return 2;
Peter Zijlstra7531e2f2008-08-11 09:30:24 +02001612
1613 /*
1614 * We're holding the nest_lock, which serializes this lock's
1615 * nesting behaviour.
1616 */
1617 if (nest)
1618 return 2;
1619
Peter Zijlstra8e182572007-07-19 01:48:54 -07001620 return print_deadlock_bug(curr, prev, next);
1621 }
1622 return 1;
1623}
1624
1625/*
1626 * There was a chain-cache miss, and we are about to add a new dependency
1627 * to a previous lock. We recursively validate the following rules:
1628 *
1629 * - would the adding of the <prev> -> <next> dependency create a
1630 * circular dependency in the graph? [== circular deadlock]
1631 *
1632 * - does the new prev->next dependency connect any hardirq-safe lock
1633 * (in the full backwards-subgraph starting at <prev>) with any
1634 * hardirq-unsafe lock (in the full forwards-subgraph starting at
1635 * <next>)? [== illegal lock inversion with hardirq contexts]
1636 *
1637 * - does the new prev->next dependency connect any softirq-safe lock
1638 * (in the full backwards-subgraph starting at <prev>) with any
1639 * softirq-unsafe lock (in the full forwards-subgraph starting at
1640 * <next>)? [== illegal lock inversion with softirq contexts]
1641 *
1642 * any of these scenarios could lead to a deadlock.
1643 *
1644 * Then if all the validations pass, we add the forwards and backwards
1645 * dependency.
1646 */
1647static int
1648check_prev_add(struct task_struct *curr, struct held_lock *prev,
1649 struct held_lock *next, int distance)
1650{
1651 struct lock_list *entry;
1652 int ret;
Ming Leidb0002a2009-07-16 15:44:29 +02001653 struct lock_list this;
1654 struct lock_list *uninitialized_var(target_entry);
Peter Zijlstra8e182572007-07-19 01:48:54 -07001655
1656 /*
1657 * Prove that the new <prev> -> <next> dependency would not
1658 * create a circular dependency in the graph. (We do this by
1659 * forward-recursing into the graph starting at <next>, and
1660 * checking whether we can reach <prev>.)
1661 *
1662 * We are using global variables to control the recursion, to
1663 * keep the stackframe size of the recursive functions low:
1664 */
Ming Leidb0002a2009-07-16 15:44:29 +02001665 this.class = hlock_class(next);
1666 this.parent = NULL;
1667 ret = check_noncircular(&this, hlock_class(prev), &target_entry);
1668 if (unlikely(!ret))
1669 return print_circular_bug(&this, target_entry, next, prev);
1670 else if (unlikely(ret < 0))
1671 return print_bfs_bug(ret);
Ming Leic94aa5c2009-07-16 15:44:29 +02001672
Peter Zijlstra8e182572007-07-19 01:48:54 -07001673 if (!check_prev_add_irq(curr, prev, next))
1674 return 0;
1675
1676 /*
1677 * For recursive read-locks we do all the dependency checks,
1678 * but we dont store read-triggered dependencies (only
1679 * write-triggered dependencies). This ensures that only the
1680 * write-side dependencies matter, and that if for example a
1681 * write-lock never takes any other locks, then the reads are
1682 * equivalent to a NOP.
1683 */
1684 if (next->read == 2 || prev->read == 2)
1685 return 1;
1686 /*
1687 * Is the <prev> -> <next> dependency already present?
1688 *
1689 * (this may occur even though this is a new chain: consider
1690 * e.g. the L1 -> L2 -> L3 -> L4 and the L5 -> L1 -> L2 -> L3
1691 * chains - the second one will be new, but L1 already has
1692 * L2 added to its dependency list, due to the first chain.)
1693 */
Dave Jonesf82b2172008-08-11 09:30:23 +02001694 list_for_each_entry(entry, &hlock_class(prev)->locks_after, entry) {
1695 if (entry->class == hlock_class(next)) {
Peter Zijlstra8e182572007-07-19 01:48:54 -07001696 if (distance == 1)
1697 entry->distance = 1;
1698 return 2;
1699 }
1700 }
1701
1702 /*
1703 * Ok, all validations passed, add the new lock
1704 * to the previous lock's dependency list:
1705 */
Dave Jonesf82b2172008-08-11 09:30:23 +02001706 ret = add_lock_to_list(hlock_class(prev), hlock_class(next),
1707 &hlock_class(prev)->locks_after,
1708 next->acquire_ip, distance);
Peter Zijlstra8e182572007-07-19 01:48:54 -07001709
1710 if (!ret)
1711 return 0;
1712
Dave Jonesf82b2172008-08-11 09:30:23 +02001713 ret = add_lock_to_list(hlock_class(next), hlock_class(prev),
1714 &hlock_class(next)->locks_before,
1715 next->acquire_ip, distance);
Peter Zijlstra8e182572007-07-19 01:48:54 -07001716 if (!ret)
1717 return 0;
1718
1719 /*
1720 * Debugging printouts:
1721 */
Dave Jonesf82b2172008-08-11 09:30:23 +02001722 if (verbose(hlock_class(prev)) || verbose(hlock_class(next))) {
Peter Zijlstra8e182572007-07-19 01:48:54 -07001723 graph_unlock();
1724 printk("\n new dependency: ");
Dave Jonesf82b2172008-08-11 09:30:23 +02001725 print_lock_name(hlock_class(prev));
Peter Zijlstra8e182572007-07-19 01:48:54 -07001726 printk(" => ");
Dave Jonesf82b2172008-08-11 09:30:23 +02001727 print_lock_name(hlock_class(next));
Peter Zijlstra8e182572007-07-19 01:48:54 -07001728 printk("\n");
1729 dump_stack();
1730 return graph_lock();
1731 }
1732 return 1;
1733}
1734
1735/*
1736 * Add the dependency to all directly-previous locks that are 'relevant'.
1737 * The ones that are relevant are (in increasing distance from curr):
1738 * all consecutive trylock entries and the final non-trylock entry - or
1739 * the end of this context's lock-chain - whichever comes first.
1740 */
1741static int
1742check_prevs_add(struct task_struct *curr, struct held_lock *next)
1743{
1744 int depth = curr->lockdep_depth;
1745 struct held_lock *hlock;
1746
1747 /*
1748 * Debugging checks.
1749 *
1750 * Depth must not be zero for a non-head lock:
1751 */
1752 if (!depth)
1753 goto out_bug;
1754 /*
1755 * At least two relevant locks must exist for this
1756 * to be a head:
1757 */
1758 if (curr->held_locks[depth].irq_context !=
1759 curr->held_locks[depth-1].irq_context)
1760 goto out_bug;
1761
1762 for (;;) {
1763 int distance = curr->lockdep_depth - depth + 1;
1764 hlock = curr->held_locks + depth-1;
1765 /*
1766 * Only non-recursive-read entries get new dependencies
1767 * added:
1768 */
1769 if (hlock->read != 2) {
1770 if (!check_prev_add(curr, hlock, next, distance))
1771 return 0;
1772 /*
1773 * Stop after the first non-trylock entry,
1774 * as non-trylock entries have added their
1775 * own direct dependencies already, so this
1776 * lock is connected to them indirectly:
1777 */
1778 if (!hlock->trylock)
1779 break;
1780 }
1781 depth--;
1782 /*
1783 * End of lock-stack?
1784 */
1785 if (!depth)
1786 break;
1787 /*
1788 * Stop the search if we cross into another context:
1789 */
1790 if (curr->held_locks[depth].irq_context !=
1791 curr->held_locks[depth-1].irq_context)
1792 break;
1793 }
1794 return 1;
1795out_bug:
1796 if (!debug_locks_off_graph_unlock())
1797 return 0;
1798
1799 WARN_ON(1);
1800
1801 return 0;
1802}
1803
1804unsigned long nr_lock_chains;
Huang, Ying443cd502008-06-20 16:39:21 +08001805struct lock_chain lock_chains[MAX_LOCKDEP_CHAINS];
Huang, Yingcd1a28e2008-06-23 11:20:54 +08001806int nr_chain_hlocks;
Huang, Ying443cd502008-06-20 16:39:21 +08001807static u16 chain_hlocks[MAX_LOCKDEP_CHAIN_HLOCKS];
1808
1809struct lock_class *lock_chain_get_class(struct lock_chain *chain, int i)
1810{
1811 return lock_classes + chain_hlocks[chain->base + i];
1812}
Peter Zijlstra8e182572007-07-19 01:48:54 -07001813
1814/*
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001815 * Look up a dependency chain. If the key is not present yet then
Jarek Poplawski9e860d02007-05-08 00:30:12 -07001816 * add it and return 1 - in this case the new dependency chain is
1817 * validated. If the key is already hashed, return 0.
1818 * (On return with 1 graph_lock is held.)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001819 */
Huang, Ying443cd502008-06-20 16:39:21 +08001820static inline int lookup_chain_cache(struct task_struct *curr,
1821 struct held_lock *hlock,
1822 u64 chain_key)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001823{
Dave Jonesf82b2172008-08-11 09:30:23 +02001824 struct lock_class *class = hlock_class(hlock);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001825 struct list_head *hash_head = chainhashentry(chain_key);
1826 struct lock_chain *chain;
Huang, Ying443cd502008-06-20 16:39:21 +08001827 struct held_lock *hlock_curr, *hlock_next;
Huang, Yingcd1a28e2008-06-23 11:20:54 +08001828 int i, j, n, cn;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001829
Jarek Poplawski381a2292007-02-10 01:44:58 -08001830 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
1831 return 0;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001832 /*
1833 * We can walk it lock-free, because entries only get added
1834 * to the hash:
1835 */
1836 list_for_each_entry(chain, hash_head, entry) {
1837 if (chain->chain_key == chain_key) {
1838cache_hit:
1839 debug_atomic_inc(&chain_lookup_hits);
Ingo Molnar81fc6852006-12-13 00:34:40 -08001840 if (very_verbose(class))
Andrew Morton755cd902006-12-29 16:49:14 -08001841 printk("\nhash chain already cached, key: "
1842 "%016Lx tail class: [%p] %s\n",
1843 (unsigned long long)chain_key,
1844 class->key, class->name);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001845 return 0;
1846 }
1847 }
Ingo Molnar81fc6852006-12-13 00:34:40 -08001848 if (very_verbose(class))
Andrew Morton755cd902006-12-29 16:49:14 -08001849 printk("\nnew hash chain, key: %016Lx tail class: [%p] %s\n",
1850 (unsigned long long)chain_key, class->key, class->name);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001851 /*
1852 * Allocate a new chain entry from the static array, and add
1853 * it to the hash:
1854 */
Ingo Molnar74c383f2006-12-13 00:34:43 -08001855 if (!graph_lock())
1856 return 0;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001857 /*
1858 * We have to walk the chain again locked - to avoid duplicates:
1859 */
1860 list_for_each_entry(chain, hash_head, entry) {
1861 if (chain->chain_key == chain_key) {
Ingo Molnar74c383f2006-12-13 00:34:43 -08001862 graph_unlock();
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001863 goto cache_hit;
1864 }
1865 }
1866 if (unlikely(nr_lock_chains >= MAX_LOCKDEP_CHAINS)) {
Ingo Molnar74c383f2006-12-13 00:34:43 -08001867 if (!debug_locks_off_graph_unlock())
1868 return 0;
1869
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001870 printk("BUG: MAX_LOCKDEP_CHAINS too low!\n");
1871 printk("turning off the locking correctness validator.\n");
Peter Zijlstraeedeeab2009-03-18 12:38:47 +01001872 dump_stack();
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001873 return 0;
1874 }
1875 chain = lock_chains + nr_lock_chains++;
1876 chain->chain_key = chain_key;
Huang, Ying443cd502008-06-20 16:39:21 +08001877 chain->irq_context = hlock->irq_context;
1878 /* Find the first held_lock of current chain */
1879 hlock_next = hlock;
1880 for (i = curr->lockdep_depth - 1; i >= 0; i--) {
1881 hlock_curr = curr->held_locks + i;
1882 if (hlock_curr->irq_context != hlock_next->irq_context)
1883 break;
1884 hlock_next = hlock;
1885 }
1886 i++;
1887 chain->depth = curr->lockdep_depth + 1 - i;
Huang, Yingcd1a28e2008-06-23 11:20:54 +08001888 cn = nr_chain_hlocks;
1889 while (cn + chain->depth <= MAX_LOCKDEP_CHAIN_HLOCKS) {
1890 n = cmpxchg(&nr_chain_hlocks, cn, cn + chain->depth);
1891 if (n == cn)
1892 break;
1893 cn = n;
1894 }
1895 if (likely(cn + chain->depth <= MAX_LOCKDEP_CHAIN_HLOCKS)) {
1896 chain->base = cn;
Huang, Ying443cd502008-06-20 16:39:21 +08001897 for (j = 0; j < chain->depth - 1; j++, i++) {
Dave Jonesf82b2172008-08-11 09:30:23 +02001898 int lock_id = curr->held_locks[i].class_idx - 1;
Huang, Ying443cd502008-06-20 16:39:21 +08001899 chain_hlocks[chain->base + j] = lock_id;
1900 }
1901 chain_hlocks[chain->base + j] = class - lock_classes;
1902 }
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001903 list_add_tail_rcu(&chain->entry, hash_head);
1904 debug_atomic_inc(&chain_lookup_misses);
Peter Zijlstra8e182572007-07-19 01:48:54 -07001905 inc_chains();
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001906
1907 return 1;
1908}
Peter Zijlstra8e182572007-07-19 01:48:54 -07001909
1910static int validate_chain(struct task_struct *curr, struct lockdep_map *lock,
Johannes Berg4e6045f2007-10-18 23:39:55 -07001911 struct held_lock *hlock, int chain_head, u64 chain_key)
Peter Zijlstra8e182572007-07-19 01:48:54 -07001912{
1913 /*
1914 * Trylock needs to maintain the stack of held locks, but it
1915 * does not add new dependencies, because trylock can be done
1916 * in any order.
1917 *
1918 * We look up the chain_key and do the O(N^2) check and update of
1919 * the dependencies only if this is a new dependency chain.
1920 * (If lookup_chain_cache() returns with 1 it acquires
1921 * graph_lock for us)
1922 */
1923 if (!hlock->trylock && (hlock->check == 2) &&
Huang, Ying443cd502008-06-20 16:39:21 +08001924 lookup_chain_cache(curr, hlock, chain_key)) {
Peter Zijlstra8e182572007-07-19 01:48:54 -07001925 /*
1926 * Check whether last held lock:
1927 *
1928 * - is irq-safe, if this lock is irq-unsafe
1929 * - is softirq-safe, if this lock is hardirq-unsafe
1930 *
1931 * And check whether the new lock's dependency graph
1932 * could lead back to the previous lock.
1933 *
1934 * any of these scenarios could lead to a deadlock. If
1935 * All validations
1936 */
1937 int ret = check_deadlock(curr, hlock, lock, hlock->read);
1938
1939 if (!ret)
1940 return 0;
1941 /*
1942 * Mark recursive read, as we jump over it when
1943 * building dependencies (just like we jump over
1944 * trylock entries):
1945 */
1946 if (ret == 2)
1947 hlock->read = 2;
1948 /*
1949 * Add dependency only if this lock is not the head
1950 * of the chain, and if it's not a secondary read-lock:
1951 */
1952 if (!chain_head && ret != 2)
1953 if (!check_prevs_add(curr, hlock))
1954 return 0;
1955 graph_unlock();
1956 } else
1957 /* after lookup_chain_cache(): */
1958 if (unlikely(!debug_locks))
1959 return 0;
1960
1961 return 1;
1962}
1963#else
1964static inline int validate_chain(struct task_struct *curr,
1965 struct lockdep_map *lock, struct held_lock *hlock,
Gregory Haskins3aa416b2007-10-11 22:11:11 +02001966 int chain_head, u64 chain_key)
Peter Zijlstra8e182572007-07-19 01:48:54 -07001967{
1968 return 1;
1969}
Peter Zijlstraca58abc2007-07-19 01:48:53 -07001970#endif
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001971
1972/*
1973 * We are building curr_chain_key incrementally, so double-check
1974 * it from scratch, to make sure that it's done correctly:
1975 */
Steven Rostedt1d09daa2008-05-12 21:20:55 +02001976static void check_chain_key(struct task_struct *curr)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001977{
1978#ifdef CONFIG_DEBUG_LOCKDEP
1979 struct held_lock *hlock, *prev_hlock = NULL;
1980 unsigned int i, id;
1981 u64 chain_key = 0;
1982
1983 for (i = 0; i < curr->lockdep_depth; i++) {
1984 hlock = curr->held_locks + i;
1985 if (chain_key != hlock->prev_chain_key) {
1986 debug_locks_off();
Arjan van de Ven2df8b1d2008-07-30 12:43:11 -07001987 WARN(1, "hm#1, depth: %u [%u], %016Lx != %016Lx\n",
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001988 curr->lockdep_depth, i,
1989 (unsigned long long)chain_key,
1990 (unsigned long long)hlock->prev_chain_key);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001991 return;
1992 }
Dave Jonesf82b2172008-08-11 09:30:23 +02001993 id = hlock->class_idx - 1;
Jarek Poplawski381a2292007-02-10 01:44:58 -08001994 if (DEBUG_LOCKS_WARN_ON(id >= MAX_LOCKDEP_KEYS))
1995 return;
1996
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001997 if (prev_hlock && (prev_hlock->irq_context !=
1998 hlock->irq_context))
1999 chain_key = 0;
2000 chain_key = iterate_chain_key(chain_key, id);
2001 prev_hlock = hlock;
2002 }
2003 if (chain_key != curr->curr_chain_key) {
2004 debug_locks_off();
Arjan van de Ven2df8b1d2008-07-30 12:43:11 -07002005 WARN(1, "hm#2, depth: %u [%u], %016Lx != %016Lx\n",
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002006 curr->lockdep_depth, i,
2007 (unsigned long long)chain_key,
2008 (unsigned long long)curr->curr_chain_key);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002009 }
2010#endif
2011}
2012
Peter Zijlstra8e182572007-07-19 01:48:54 -07002013static int
2014print_usage_bug(struct task_struct *curr, struct held_lock *this,
2015 enum lock_usage_bit prev_bit, enum lock_usage_bit new_bit)
2016{
2017 if (!debug_locks_off_graph_unlock() || debug_locks_silent)
2018 return 0;
2019
2020 printk("\n=================================\n");
2021 printk( "[ INFO: inconsistent lock state ]\n");
2022 print_kernel_version();
2023 printk( "---------------------------------\n");
2024
2025 printk("inconsistent {%s} -> {%s} usage.\n",
2026 usage_str[prev_bit], usage_str[new_bit]);
2027
2028 printk("%s/%d [HC%u[%lu]:SC%u[%lu]:HE%u:SE%u] takes:\n",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07002029 curr->comm, task_pid_nr(curr),
Peter Zijlstra8e182572007-07-19 01:48:54 -07002030 trace_hardirq_context(curr), hardirq_count() >> HARDIRQ_SHIFT,
2031 trace_softirq_context(curr), softirq_count() >> SOFTIRQ_SHIFT,
2032 trace_hardirqs_enabled(curr),
2033 trace_softirqs_enabled(curr));
2034 print_lock(this);
2035
2036 printk("{%s} state was registered at:\n", usage_str[prev_bit]);
Dave Jonesf82b2172008-08-11 09:30:23 +02002037 print_stack_trace(hlock_class(this)->usage_traces + prev_bit, 1);
Peter Zijlstra8e182572007-07-19 01:48:54 -07002038
2039 print_irqtrace_events(curr);
2040 printk("\nother info that might help us debug this:\n");
2041 lockdep_print_held_locks(curr);
2042
2043 printk("\nstack backtrace:\n");
2044 dump_stack();
2045
2046 return 0;
2047}
2048
2049/*
2050 * Print out an error if an invalid bit is set:
2051 */
2052static inline int
2053valid_state(struct task_struct *curr, struct held_lock *this,
2054 enum lock_usage_bit new_bit, enum lock_usage_bit bad_bit)
2055{
Dave Jonesf82b2172008-08-11 09:30:23 +02002056 if (unlikely(hlock_class(this)->usage_mask & (1 << bad_bit)))
Peter Zijlstra8e182572007-07-19 01:48:54 -07002057 return print_usage_bug(curr, this, bad_bit, new_bit);
2058 return 1;
2059}
2060
2061static int mark_lock(struct task_struct *curr, struct held_lock *this,
2062 enum lock_usage_bit new_bit);
2063
Steven Rostedt81d68a92008-05-12 21:20:42 +02002064#if defined(CONFIG_TRACE_IRQFLAGS) && defined(CONFIG_PROVE_LOCKING)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002065
2066/*
2067 * print irq inversion bug:
2068 */
2069static int
Ming Lei24208ca2009-07-16 15:44:29 +02002070print_irq_inversion_bug(struct task_struct *curr,
2071 struct lock_list *root, struct lock_list *other,
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002072 struct held_lock *this, int forwards,
2073 const char *irqclass)
2074{
Ingo Molnar74c383f2006-12-13 00:34:43 -08002075 if (!debug_locks_off_graph_unlock() || debug_locks_silent)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002076 return 0;
2077
2078 printk("\n=========================================================\n");
2079 printk( "[ INFO: possible irq lock inversion dependency detected ]\n");
Dave Jones99de0552006-09-29 02:00:10 -07002080 print_kernel_version();
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002081 printk( "---------------------------------------------------------\n");
2082 printk("%s/%d just changed the state of lock:\n",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07002083 curr->comm, task_pid_nr(curr));
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002084 print_lock(this);
2085 if (forwards)
Peter Zijlstra26575e22009-03-04 14:53:24 +01002086 printk("but this lock took another, %s-unsafe lock in the past:\n", irqclass);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002087 else
Peter Zijlstra26575e22009-03-04 14:53:24 +01002088 printk("but this lock was taken by another, %s-safe lock in the past:\n", irqclass);
Ming Lei24208ca2009-07-16 15:44:29 +02002089 print_lock_name(other->class);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002090 printk("\n\nand interrupts could create inverse lock ordering between them.\n\n");
2091
2092 printk("\nother info that might help us debug this:\n");
2093 lockdep_print_held_locks(curr);
2094
Ming Lei24208ca2009-07-16 15:44:29 +02002095 printk("\nthe shortest dependencies between 2nd lock and 1st lock:\n");
2096 if (!save_trace(&root->trace))
2097 return 0;
2098 print_shortest_lock_dependencies(other, root);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002099
2100 printk("\nstack backtrace:\n");
2101 dump_stack();
2102
2103 return 0;
2104}
2105
2106/*
2107 * Prove that in the forwards-direction subgraph starting at <this>
2108 * there is no lock matching <mask>:
2109 */
2110static int
2111check_usage_forwards(struct task_struct *curr, struct held_lock *this,
2112 enum lock_usage_bit bit, const char *irqclass)
2113{
2114 int ret;
Ming Leid7aaba12009-07-16 15:44:29 +02002115 struct lock_list root;
2116 struct lock_list *uninitialized_var(target_entry);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002117
Ming Leid7aaba12009-07-16 15:44:29 +02002118 root.parent = NULL;
2119 root.class = hlock_class(this);
2120 ret = find_usage_forwards(&root, bit, &target_entry);
Peter Zijlstraaf012962009-07-16 15:44:29 +02002121 if (ret < 0)
2122 return print_bfs_bug(ret);
2123 if (ret == 1)
2124 return ret;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002125
Ming Lei24208ca2009-07-16 15:44:29 +02002126 return print_irq_inversion_bug(curr, &root, target_entry,
Ming Leid7aaba12009-07-16 15:44:29 +02002127 this, 1, irqclass);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002128}
2129
2130/*
2131 * Prove that in the backwards-direction subgraph starting at <this>
2132 * there is no lock matching <mask>:
2133 */
2134static int
2135check_usage_backwards(struct task_struct *curr, struct held_lock *this,
2136 enum lock_usage_bit bit, const char *irqclass)
2137{
2138 int ret;
Ming Leid7aaba12009-07-16 15:44:29 +02002139 struct lock_list root;
2140 struct lock_list *uninitialized_var(target_entry);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002141
Ming Leid7aaba12009-07-16 15:44:29 +02002142 root.parent = NULL;
2143 root.class = hlock_class(this);
2144 ret = find_usage_backwards(&root, bit, &target_entry);
Peter Zijlstraaf012962009-07-16 15:44:29 +02002145 if (ret < 0)
2146 return print_bfs_bug(ret);
2147 if (ret == 1)
2148 return ret;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002149
Ming Lei24208ca2009-07-16 15:44:29 +02002150 return print_irq_inversion_bug(curr, &root, target_entry,
Oleg Nesterov48d50672010-01-26 19:16:41 +01002151 this, 0, irqclass);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002152}
2153
Ingo Molnar3117df02006-12-13 00:34:43 -08002154void print_irqtrace_events(struct task_struct *curr)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002155{
2156 printk("irq event stamp: %u\n", curr->irq_events);
2157 printk("hardirqs last enabled at (%u): ", curr->hardirq_enable_event);
2158 print_ip_sym(curr->hardirq_enable_ip);
2159 printk("hardirqs last disabled at (%u): ", curr->hardirq_disable_event);
2160 print_ip_sym(curr->hardirq_disable_ip);
2161 printk("softirqs last enabled at (%u): ", curr->softirq_enable_event);
2162 print_ip_sym(curr->softirq_enable_ip);
2163 printk("softirqs last disabled at (%u): ", curr->softirq_disable_event);
2164 print_ip_sym(curr->softirq_disable_ip);
2165}
2166
Peter Zijlstracd953022009-01-22 16:38:21 +01002167static int HARDIRQ_verbose(struct lock_class *class)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002168{
Peter Zijlstra8e182572007-07-19 01:48:54 -07002169#if HARDIRQ_VERBOSE
2170 return class_filter(class);
2171#endif
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002172 return 0;
2173}
2174
Peter Zijlstracd953022009-01-22 16:38:21 +01002175static int SOFTIRQ_verbose(struct lock_class *class)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002176{
Peter Zijlstra8e182572007-07-19 01:48:54 -07002177#if SOFTIRQ_VERBOSE
2178 return class_filter(class);
2179#endif
2180 return 0;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002181}
2182
Peter Zijlstracd953022009-01-22 16:38:21 +01002183static int RECLAIM_FS_verbose(struct lock_class *class)
Nick Piggincf40bd12009-01-21 08:12:39 +01002184{
2185#if RECLAIM_VERBOSE
2186 return class_filter(class);
2187#endif
2188 return 0;
2189}
2190
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002191#define STRICT_READ_CHECKS 1
2192
Peter Zijlstracd953022009-01-22 16:38:21 +01002193static int (*state_verbose_f[])(struct lock_class *class) = {
2194#define LOCKDEP_STATE(__STATE) \
2195 __STATE##_verbose,
2196#include "lockdep_states.h"
2197#undef LOCKDEP_STATE
2198};
2199
2200static inline int state_verbose(enum lock_usage_bit bit,
2201 struct lock_class *class)
2202{
2203 return state_verbose_f[bit >> 2](class);
2204}
2205
Peter Zijlstra42c50d52009-01-22 16:58:16 +01002206typedef int (*check_usage_f)(struct task_struct *, struct held_lock *,
2207 enum lock_usage_bit bit, const char *name);
2208
Peter Zijlstra6a6904d2009-01-22 16:07:44 +01002209static int
Peter Zijlstra1c21f142009-03-04 13:51:13 +01002210mark_lock_irq(struct task_struct *curr, struct held_lock *this,
2211 enum lock_usage_bit new_bit)
Peter Zijlstra6a6904d2009-01-22 16:07:44 +01002212{
Peter Zijlstraf9892092009-01-22 16:09:59 +01002213 int excl_bit = exclusive_bit(new_bit);
Peter Zijlstra9d3651a2009-01-22 17:18:32 +01002214 int read = new_bit & 1;
Peter Zijlstra42c50d52009-01-22 16:58:16 +01002215 int dir = new_bit & 2;
2216
Peter Zijlstra38aa2712009-01-27 14:53:50 +01002217 /*
2218 * mark USED_IN has to look forwards -- to ensure no dependency
2219 * has ENABLED state, which would allow recursion deadlocks.
2220 *
2221 * mark ENABLED has to look backwards -- to ensure no dependee
2222 * has USED_IN state, which, again, would allow recursion deadlocks.
2223 */
Peter Zijlstra42c50d52009-01-22 16:58:16 +01002224 check_usage_f usage = dir ?
2225 check_usage_backwards : check_usage_forwards;
Peter Zijlstraf9892092009-01-22 16:09:59 +01002226
Peter Zijlstra38aa2712009-01-27 14:53:50 +01002227 /*
2228 * Validate that this particular lock does not have conflicting
2229 * usage states.
2230 */
Peter Zijlstra6a6904d2009-01-22 16:07:44 +01002231 if (!valid_state(curr, this, new_bit, excl_bit))
2232 return 0;
Peter Zijlstra9d3651a2009-01-22 17:18:32 +01002233
Peter Zijlstra38aa2712009-01-27 14:53:50 +01002234 /*
2235 * Validate that the lock dependencies don't have conflicting usage
2236 * states.
2237 */
2238 if ((!read || !dir || STRICT_READ_CHECKS) &&
Peter Zijlstra1c21f142009-03-04 13:51:13 +01002239 !usage(curr, this, excl_bit, state_name(new_bit & ~1)))
Peter Zijlstra6a6904d2009-01-22 16:07:44 +01002240 return 0;
Peter Zijlstra780e8202009-01-22 16:51:29 +01002241
Peter Zijlstra38aa2712009-01-27 14:53:50 +01002242 /*
2243 * Check for read in write conflicts
2244 */
2245 if (!read) {
2246 if (!valid_state(curr, this, new_bit, excl_bit + 1))
2247 return 0;
2248
2249 if (STRICT_READ_CHECKS &&
Peter Zijlstra4f367d82009-01-22 18:10:42 +01002250 !usage(curr, this, excl_bit + 1,
2251 state_name(new_bit + 1)))
Peter Zijlstra38aa2712009-01-27 14:53:50 +01002252 return 0;
2253 }
Peter Zijlstra780e8202009-01-22 16:51:29 +01002254
Peter Zijlstracd953022009-01-22 16:38:21 +01002255 if (state_verbose(new_bit, hlock_class(this)))
Peter Zijlstra6a6904d2009-01-22 16:07:44 +01002256 return 2;
2257
2258 return 1;
2259}
2260
Nick Piggincf40bd12009-01-21 08:12:39 +01002261enum mark_type {
Peter Zijlstra36bfb9b2009-01-22 14:12:41 +01002262#define LOCKDEP_STATE(__STATE) __STATE,
2263#include "lockdep_states.h"
2264#undef LOCKDEP_STATE
Nick Piggincf40bd12009-01-21 08:12:39 +01002265};
2266
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002267/*
2268 * Mark all held locks with a usage bit:
2269 */
Steven Rostedt1d09daa2008-05-12 21:20:55 +02002270static int
Nick Piggincf40bd12009-01-21 08:12:39 +01002271mark_held_locks(struct task_struct *curr, enum mark_type mark)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002272{
2273 enum lock_usage_bit usage_bit;
2274 struct held_lock *hlock;
2275 int i;
2276
2277 for (i = 0; i < curr->lockdep_depth; i++) {
2278 hlock = curr->held_locks + i;
2279
Peter Zijlstracf2ad4d2009-01-27 13:58:08 +01002280 usage_bit = 2 + (mark << 2); /* ENABLED */
2281 if (hlock->read)
2282 usage_bit += 1; /* READ */
2283
2284 BUG_ON(usage_bit >= LOCK_USAGE_STATES);
Nick Piggincf40bd12009-01-21 08:12:39 +01002285
Jarek Poplawski4ff773bb2007-05-08 00:31:00 -07002286 if (!mark_lock(curr, hlock, usage_bit))
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002287 return 0;
2288 }
2289
2290 return 1;
2291}
2292
2293/*
2294 * Debugging helper: via this flag we know that we are in
2295 * 'early bootup code', and will warn about any invalid irqs-on event:
2296 */
2297static int early_boot_irqs_enabled;
2298
2299void early_boot_irqs_off(void)
2300{
2301 early_boot_irqs_enabled = 0;
2302}
2303
2304void early_boot_irqs_on(void)
2305{
2306 early_boot_irqs_enabled = 1;
2307}
2308
2309/*
2310 * Hardirqs will be enabled:
2311 */
Heiko Carstens6afe40b2008-10-28 11:14:58 +01002312void trace_hardirqs_on_caller(unsigned long ip)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002313{
2314 struct task_struct *curr = current;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002315
Heiko Carstens6afe40b2008-10-28 11:14:58 +01002316 time_hardirqs_on(CALLER_ADDR0, ip);
Steven Rostedt81d68a92008-05-12 21:20:42 +02002317
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002318 if (unlikely(!debug_locks || current->lockdep_recursion))
2319 return;
2320
2321 if (DEBUG_LOCKS_WARN_ON(unlikely(!early_boot_irqs_enabled)))
2322 return;
2323
2324 if (unlikely(curr->hardirqs_enabled)) {
2325 debug_atomic_inc(&redundant_hardirqs_on);
2326 return;
2327 }
2328 /* we'll do an OFF -> ON transition: */
2329 curr->hardirqs_enabled = 1;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002330
2331 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2332 return;
2333 if (DEBUG_LOCKS_WARN_ON(current->hardirq_context))
2334 return;
2335 /*
2336 * We are going to turn hardirqs on, so set the
2337 * usage bit for all held locks:
2338 */
Nick Piggincf40bd12009-01-21 08:12:39 +01002339 if (!mark_held_locks(curr, HARDIRQ))
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002340 return;
2341 /*
2342 * If we have softirqs enabled, then set the usage
2343 * bit for all held locks. (disabled hardirqs prevented
2344 * this bit from being set before)
2345 */
2346 if (curr->softirqs_enabled)
Nick Piggincf40bd12009-01-21 08:12:39 +01002347 if (!mark_held_locks(curr, SOFTIRQ))
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002348 return;
2349
2350 curr->hardirq_enable_ip = ip;
2351 curr->hardirq_enable_event = ++curr->irq_events;
2352 debug_atomic_inc(&hardirqs_on_events);
2353}
Steven Rostedt81d68a92008-05-12 21:20:42 +02002354EXPORT_SYMBOL(trace_hardirqs_on_caller);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002355
Steven Rostedt1d09daa2008-05-12 21:20:55 +02002356void trace_hardirqs_on(void)
Steven Rostedt81d68a92008-05-12 21:20:42 +02002357{
2358 trace_hardirqs_on_caller(CALLER_ADDR0);
2359}
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002360EXPORT_SYMBOL(trace_hardirqs_on);
2361
2362/*
2363 * Hardirqs were disabled:
2364 */
Heiko Carstens6afe40b2008-10-28 11:14:58 +01002365void trace_hardirqs_off_caller(unsigned long ip)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002366{
2367 struct task_struct *curr = current;
2368
Heiko Carstens6afe40b2008-10-28 11:14:58 +01002369 time_hardirqs_off(CALLER_ADDR0, ip);
Steven Rostedt81d68a92008-05-12 21:20:42 +02002370
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002371 if (unlikely(!debug_locks || current->lockdep_recursion))
2372 return;
2373
2374 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2375 return;
2376
2377 if (curr->hardirqs_enabled) {
2378 /*
2379 * We have done an ON -> OFF transition:
2380 */
2381 curr->hardirqs_enabled = 0;
Heiko Carstens6afe40b2008-10-28 11:14:58 +01002382 curr->hardirq_disable_ip = ip;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002383 curr->hardirq_disable_event = ++curr->irq_events;
2384 debug_atomic_inc(&hardirqs_off_events);
2385 } else
2386 debug_atomic_inc(&redundant_hardirqs_off);
2387}
Steven Rostedt81d68a92008-05-12 21:20:42 +02002388EXPORT_SYMBOL(trace_hardirqs_off_caller);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002389
Steven Rostedt1d09daa2008-05-12 21:20:55 +02002390void trace_hardirqs_off(void)
Steven Rostedt81d68a92008-05-12 21:20:42 +02002391{
2392 trace_hardirqs_off_caller(CALLER_ADDR0);
2393}
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002394EXPORT_SYMBOL(trace_hardirqs_off);
2395
2396/*
2397 * Softirqs will be enabled:
2398 */
2399void trace_softirqs_on(unsigned long ip)
2400{
2401 struct task_struct *curr = current;
2402
2403 if (unlikely(!debug_locks))
2404 return;
2405
2406 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2407 return;
2408
2409 if (curr->softirqs_enabled) {
2410 debug_atomic_inc(&redundant_softirqs_on);
2411 return;
2412 }
2413
2414 /*
2415 * We'll do an OFF -> ON transition:
2416 */
2417 curr->softirqs_enabled = 1;
2418 curr->softirq_enable_ip = ip;
2419 curr->softirq_enable_event = ++curr->irq_events;
2420 debug_atomic_inc(&softirqs_on_events);
2421 /*
2422 * We are going to turn softirqs on, so set the
2423 * usage bit for all held locks, if hardirqs are
2424 * enabled too:
2425 */
2426 if (curr->hardirqs_enabled)
Nick Piggincf40bd12009-01-21 08:12:39 +01002427 mark_held_locks(curr, SOFTIRQ);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002428}
2429
2430/*
2431 * Softirqs were disabled:
2432 */
2433void trace_softirqs_off(unsigned long ip)
2434{
2435 struct task_struct *curr = current;
2436
2437 if (unlikely(!debug_locks))
2438 return;
2439
2440 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2441 return;
2442
2443 if (curr->softirqs_enabled) {
2444 /*
2445 * We have done an ON -> OFF transition:
2446 */
2447 curr->softirqs_enabled = 0;
2448 curr->softirq_disable_ip = ip;
2449 curr->softirq_disable_event = ++curr->irq_events;
2450 debug_atomic_inc(&softirqs_off_events);
2451 DEBUG_LOCKS_WARN_ON(!softirq_count());
2452 } else
2453 debug_atomic_inc(&redundant_softirqs_off);
2454}
2455
Peter Zijlstra2f850182009-03-20 11:13:20 +01002456static void __lockdep_trace_alloc(gfp_t gfp_mask, unsigned long flags)
Nick Piggincf40bd12009-01-21 08:12:39 +01002457{
2458 struct task_struct *curr = current;
2459
2460 if (unlikely(!debug_locks))
2461 return;
2462
2463 /* no reclaim without waiting on it */
2464 if (!(gfp_mask & __GFP_WAIT))
2465 return;
2466
2467 /* this guy won't enter reclaim */
2468 if ((curr->flags & PF_MEMALLOC) && !(gfp_mask & __GFP_NOMEMALLOC))
2469 return;
2470
2471 /* We're only interested __GFP_FS allocations for now */
2472 if (!(gfp_mask & __GFP_FS))
2473 return;
2474
Peter Zijlstra2f850182009-03-20 11:13:20 +01002475 if (DEBUG_LOCKS_WARN_ON(irqs_disabled_flags(flags)))
Nick Piggincf40bd12009-01-21 08:12:39 +01002476 return;
2477
2478 mark_held_locks(curr, RECLAIM_FS);
2479}
2480
Peter Zijlstra2f850182009-03-20 11:13:20 +01002481static void check_flags(unsigned long flags);
2482
2483void lockdep_trace_alloc(gfp_t gfp_mask)
2484{
2485 unsigned long flags;
2486
2487 if (unlikely(current->lockdep_recursion))
2488 return;
2489
2490 raw_local_irq_save(flags);
2491 check_flags(flags);
2492 current->lockdep_recursion = 1;
2493 __lockdep_trace_alloc(gfp_mask, flags);
2494 current->lockdep_recursion = 0;
2495 raw_local_irq_restore(flags);
2496}
2497
Peter Zijlstra8e182572007-07-19 01:48:54 -07002498static int mark_irqflags(struct task_struct *curr, struct held_lock *hlock)
2499{
2500 /*
2501 * If non-trylock use in a hardirq or softirq context, then
2502 * mark the lock as used in these contexts:
2503 */
2504 if (!hlock->trylock) {
2505 if (hlock->read) {
2506 if (curr->hardirq_context)
2507 if (!mark_lock(curr, hlock,
2508 LOCK_USED_IN_HARDIRQ_READ))
2509 return 0;
2510 if (curr->softirq_context)
2511 if (!mark_lock(curr, hlock,
2512 LOCK_USED_IN_SOFTIRQ_READ))
2513 return 0;
2514 } else {
2515 if (curr->hardirq_context)
2516 if (!mark_lock(curr, hlock, LOCK_USED_IN_HARDIRQ))
2517 return 0;
2518 if (curr->softirq_context)
2519 if (!mark_lock(curr, hlock, LOCK_USED_IN_SOFTIRQ))
2520 return 0;
2521 }
2522 }
2523 if (!hlock->hardirqs_off) {
2524 if (hlock->read) {
2525 if (!mark_lock(curr, hlock,
Peter Zijlstra4fc95e82009-01-22 13:10:52 +01002526 LOCK_ENABLED_HARDIRQ_READ))
Peter Zijlstra8e182572007-07-19 01:48:54 -07002527 return 0;
2528 if (curr->softirqs_enabled)
2529 if (!mark_lock(curr, hlock,
Peter Zijlstra4fc95e82009-01-22 13:10:52 +01002530 LOCK_ENABLED_SOFTIRQ_READ))
Peter Zijlstra8e182572007-07-19 01:48:54 -07002531 return 0;
2532 } else {
2533 if (!mark_lock(curr, hlock,
Peter Zijlstra4fc95e82009-01-22 13:10:52 +01002534 LOCK_ENABLED_HARDIRQ))
Peter Zijlstra8e182572007-07-19 01:48:54 -07002535 return 0;
2536 if (curr->softirqs_enabled)
2537 if (!mark_lock(curr, hlock,
Peter Zijlstra4fc95e82009-01-22 13:10:52 +01002538 LOCK_ENABLED_SOFTIRQ))
Peter Zijlstra8e182572007-07-19 01:48:54 -07002539 return 0;
2540 }
2541 }
2542
Nick Piggincf40bd12009-01-21 08:12:39 +01002543 /*
2544 * We reuse the irq context infrastructure more broadly as a general
2545 * context checking code. This tests GFP_FS recursion (a lock taken
2546 * during reclaim for a GFP_FS allocation is held over a GFP_FS
2547 * allocation).
2548 */
2549 if (!hlock->trylock && (curr->lockdep_reclaim_gfp & __GFP_FS)) {
2550 if (hlock->read) {
2551 if (!mark_lock(curr, hlock, LOCK_USED_IN_RECLAIM_FS_READ))
2552 return 0;
2553 } else {
2554 if (!mark_lock(curr, hlock, LOCK_USED_IN_RECLAIM_FS))
2555 return 0;
2556 }
2557 }
2558
Peter Zijlstra8e182572007-07-19 01:48:54 -07002559 return 1;
2560}
2561
2562static int separate_irq_context(struct task_struct *curr,
2563 struct held_lock *hlock)
2564{
2565 unsigned int depth = curr->lockdep_depth;
2566
2567 /*
2568 * Keep track of points where we cross into an interrupt context:
2569 */
2570 hlock->irq_context = 2*(curr->hardirq_context ? 1 : 0) +
2571 curr->softirq_context;
2572 if (depth) {
2573 struct held_lock *prev_hlock;
2574
2575 prev_hlock = curr->held_locks + depth-1;
2576 /*
2577 * If we cross into another context, reset the
2578 * hash key (this also prevents the checking and the
2579 * adding of the dependency to 'prev'):
2580 */
2581 if (prev_hlock->irq_context != hlock->irq_context)
2582 return 1;
2583 }
2584 return 0;
2585}
2586
2587#else
2588
2589static inline
2590int mark_lock_irq(struct task_struct *curr, struct held_lock *this,
2591 enum lock_usage_bit new_bit)
2592{
2593 WARN_ON(1);
2594 return 1;
2595}
2596
2597static inline int mark_irqflags(struct task_struct *curr,
2598 struct held_lock *hlock)
2599{
2600 return 1;
2601}
2602
2603static inline int separate_irq_context(struct task_struct *curr,
2604 struct held_lock *hlock)
2605{
2606 return 0;
2607}
2608
Peter Zijlstra868a23a2009-02-15 00:25:21 +01002609void lockdep_trace_alloc(gfp_t gfp_mask)
2610{
2611}
2612
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002613#endif
2614
2615/*
Peter Zijlstra8e182572007-07-19 01:48:54 -07002616 * Mark a lock with a usage bit, and validate the state transition:
2617 */
Steven Rostedt1d09daa2008-05-12 21:20:55 +02002618static int mark_lock(struct task_struct *curr, struct held_lock *this,
Steven Rostedt0764d232008-05-12 21:20:44 +02002619 enum lock_usage_bit new_bit)
Peter Zijlstra8e182572007-07-19 01:48:54 -07002620{
2621 unsigned int new_mask = 1 << new_bit, ret = 1;
2622
2623 /*
2624 * If already set then do not dirty the cacheline,
2625 * nor do any checks:
2626 */
Dave Jonesf82b2172008-08-11 09:30:23 +02002627 if (likely(hlock_class(this)->usage_mask & new_mask))
Peter Zijlstra8e182572007-07-19 01:48:54 -07002628 return 1;
2629
2630 if (!graph_lock())
2631 return 0;
2632 /*
2633 * Make sure we didnt race:
2634 */
Dave Jonesf82b2172008-08-11 09:30:23 +02002635 if (unlikely(hlock_class(this)->usage_mask & new_mask)) {
Peter Zijlstra8e182572007-07-19 01:48:54 -07002636 graph_unlock();
2637 return 1;
2638 }
2639
Dave Jonesf82b2172008-08-11 09:30:23 +02002640 hlock_class(this)->usage_mask |= new_mask;
Peter Zijlstra8e182572007-07-19 01:48:54 -07002641
Dave Jonesf82b2172008-08-11 09:30:23 +02002642 if (!save_trace(hlock_class(this)->usage_traces + new_bit))
Peter Zijlstra8e182572007-07-19 01:48:54 -07002643 return 0;
2644
2645 switch (new_bit) {
Peter Zijlstra53464172009-01-22 14:15:53 +01002646#define LOCKDEP_STATE(__STATE) \
2647 case LOCK_USED_IN_##__STATE: \
2648 case LOCK_USED_IN_##__STATE##_READ: \
2649 case LOCK_ENABLED_##__STATE: \
2650 case LOCK_ENABLED_##__STATE##_READ:
2651#include "lockdep_states.h"
2652#undef LOCKDEP_STATE
Peter Zijlstra8e182572007-07-19 01:48:54 -07002653 ret = mark_lock_irq(curr, this, new_bit);
2654 if (!ret)
2655 return 0;
2656 break;
2657 case LOCK_USED:
Peter Zijlstra8e182572007-07-19 01:48:54 -07002658 debug_atomic_dec(&nr_unused_locks);
2659 break;
2660 default:
2661 if (!debug_locks_off_graph_unlock())
2662 return 0;
2663 WARN_ON(1);
2664 return 0;
2665 }
2666
2667 graph_unlock();
2668
2669 /*
2670 * We must printk outside of the graph_lock:
2671 */
2672 if (ret == 2) {
2673 printk("\nmarked lock as {%s}:\n", usage_str[new_bit]);
2674 print_lock(this);
2675 print_irqtrace_events(curr);
2676 dump_stack();
2677 }
2678
2679 return ret;
2680}
2681
2682/*
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002683 * Initialize a lock instance's lock-class mapping info:
2684 */
2685void lockdep_init_map(struct lockdep_map *lock, const char *name,
Peter Zijlstra4dfbb9d2006-10-11 01:45:14 -04002686 struct lock_class_key *key, int subclass)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002687{
Peter Zijlstrac8a25002009-04-17 09:40:49 +02002688 lock->class_cache = NULL;
2689#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
2720/*
2721 * This gets called for every mutex_lock*()/spin_lock*() operation.
2722 * We maintain the dependency maps and validate the locking attempt:
2723 */
2724static int __lock_acquire(struct lockdep_map *lock, unsigned int subclass,
2725 int trylock, int read, int check, int hardirqs_off,
Peter Zijlstrabb97a912009-07-20 19:15:35 +02002726 struct lockdep_map *nest_lock, unsigned long ip,
2727 int references)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002728{
2729 struct task_struct *curr = current;
Ingo Molnard6d897c2006-07-10 04:44:04 -07002730 struct lock_class *class = NULL;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002731 struct held_lock *hlock;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002732 unsigned int depth, id;
2733 int chain_head = 0;
Peter Zijlstrabb97a912009-07-20 19:15:35 +02002734 int class_idx;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002735 u64 chain_key;
2736
Peter Zijlstraf20786f2007-07-19 01:48:56 -07002737 if (!prove_locking)
2738 check = 1;
2739
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002740 if (unlikely(!debug_locks))
2741 return 0;
2742
2743 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2744 return 0;
2745
2746 if (unlikely(subclass >= MAX_LOCKDEP_SUBCLASSES)) {
2747 debug_locks_off();
2748 printk("BUG: MAX_LOCKDEP_SUBCLASSES too low!\n");
2749 printk("turning off the locking correctness validator.\n");
Peter Zijlstraeedeeab2009-03-18 12:38:47 +01002750 dump_stack();
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002751 return 0;
2752 }
2753
Ingo Molnard6d897c2006-07-10 04:44:04 -07002754 if (!subclass)
2755 class = lock->class_cache;
2756 /*
2757 * Not cached yet or subclass?
2758 */
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002759 if (unlikely(!class)) {
Peter Zijlstra4dfbb9d2006-10-11 01:45:14 -04002760 class = register_lock_class(lock, subclass, 0);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002761 if (!class)
2762 return 0;
2763 }
2764 debug_atomic_inc((atomic_t *)&class->ops);
2765 if (very_verbose(class)) {
2766 printk("\nacquire class [%p] %s", class->key, class->name);
2767 if (class->name_version > 1)
2768 printk("#%d", class->name_version);
2769 printk("\n");
2770 dump_stack();
2771 }
2772
2773 /*
2774 * Add the lock to the list of currently held locks.
2775 * (we dont increase the depth just yet, up until the
2776 * dependency checks are done)
2777 */
2778 depth = curr->lockdep_depth;
2779 if (DEBUG_LOCKS_WARN_ON(depth >= MAX_LOCK_DEPTH))
2780 return 0;
2781
Peter Zijlstrabb97a912009-07-20 19:15:35 +02002782 class_idx = class - lock_classes + 1;
2783
2784 if (depth) {
2785 hlock = curr->held_locks + depth - 1;
2786 if (hlock->class_idx == class_idx && nest_lock) {
2787 if (hlock->references)
2788 hlock->references++;
2789 else
2790 hlock->references = 2;
2791
2792 return 1;
2793 }
2794 }
2795
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002796 hlock = curr->held_locks + depth;
Dave Jonesf82b2172008-08-11 09:30:23 +02002797 if (DEBUG_LOCKS_WARN_ON(!class))
2798 return 0;
Peter Zijlstrabb97a912009-07-20 19:15:35 +02002799 hlock->class_idx = class_idx;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002800 hlock->acquire_ip = ip;
2801 hlock->instance = lock;
Peter Zijlstra7531e2f2008-08-11 09:30:24 +02002802 hlock->nest_lock = nest_lock;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002803 hlock->trylock = trylock;
2804 hlock->read = read;
2805 hlock->check = check;
Dmitry Baryshkov6951b122008-08-18 04:26:37 +04002806 hlock->hardirqs_off = !!hardirqs_off;
Peter Zijlstrabb97a912009-07-20 19:15:35 +02002807 hlock->references = references;
Peter Zijlstraf20786f2007-07-19 01:48:56 -07002808#ifdef CONFIG_LOCK_STAT
2809 hlock->waittime_stamp = 0;
Peter Zijlstra3365e7792009-10-09 10:12:41 +02002810 hlock->holdtime_stamp = lockstat_clock();
Peter Zijlstraf20786f2007-07-19 01:48:56 -07002811#endif
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002812
Peter Zijlstra8e182572007-07-19 01:48:54 -07002813 if (check == 2 && !mark_irqflags(curr, hlock))
2814 return 0;
2815
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002816 /* mark it as used: */
Jarek Poplawski4ff773bb2007-05-08 00:31:00 -07002817 if (!mark_lock(curr, hlock, LOCK_USED))
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002818 return 0;
Peter Zijlstra8e182572007-07-19 01:48:54 -07002819
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002820 /*
Gautham R Shenoy17aacfb2007-10-28 20:47:01 +01002821 * Calculate the chain hash: it's the combined hash of all the
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002822 * lock keys along the dependency chain. We save the hash value
2823 * at every step so that we can get the current hash easily
2824 * after unlock. The chain hash is then used to cache dependency
2825 * results.
2826 *
2827 * The 'key ID' is what is the most compact key value to drive
2828 * the hash, not class->key.
2829 */
2830 id = class - lock_classes;
2831 if (DEBUG_LOCKS_WARN_ON(id >= MAX_LOCKDEP_KEYS))
2832 return 0;
2833
2834 chain_key = curr->curr_chain_key;
2835 if (!depth) {
2836 if (DEBUG_LOCKS_WARN_ON(chain_key != 0))
2837 return 0;
2838 chain_head = 1;
2839 }
2840
2841 hlock->prev_chain_key = chain_key;
Peter Zijlstra8e182572007-07-19 01:48:54 -07002842 if (separate_irq_context(curr, hlock)) {
2843 chain_key = 0;
2844 chain_head = 1;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002845 }
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002846 chain_key = iterate_chain_key(chain_key, id);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002847
Gregory Haskins3aa416b2007-10-11 22:11:11 +02002848 if (!validate_chain(curr, lock, hlock, chain_head, chain_key))
Peter Zijlstra8e182572007-07-19 01:48:54 -07002849 return 0;
Jarek Poplawski381a2292007-02-10 01:44:58 -08002850
Gregory Haskins3aa416b2007-10-11 22:11:11 +02002851 curr->curr_chain_key = chain_key;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002852 curr->lockdep_depth++;
2853 check_chain_key(curr);
Jarek Poplawski60e114d2007-02-20 13:58:00 -08002854#ifdef CONFIG_DEBUG_LOCKDEP
2855 if (unlikely(!debug_locks))
2856 return 0;
2857#endif
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002858 if (unlikely(curr->lockdep_depth >= MAX_LOCK_DEPTH)) {
2859 debug_locks_off();
2860 printk("BUG: MAX_LOCK_DEPTH too low!\n");
2861 printk("turning off the locking correctness validator.\n");
Peter Zijlstraeedeeab2009-03-18 12:38:47 +01002862 dump_stack();
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002863 return 0;
2864 }
Jarek Poplawski381a2292007-02-10 01:44:58 -08002865
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002866 if (unlikely(curr->lockdep_depth > max_lockdep_depth))
2867 max_lockdep_depth = curr->lockdep_depth;
2868
2869 return 1;
2870}
2871
2872static int
2873print_unlock_inbalance_bug(struct task_struct *curr, struct lockdep_map *lock,
2874 unsigned long ip)
2875{
2876 if (!debug_locks_off())
2877 return 0;
2878 if (debug_locks_silent)
2879 return 0;
2880
2881 printk("\n=====================================\n");
2882 printk( "[ BUG: bad unlock balance detected! ]\n");
2883 printk( "-------------------------------------\n");
2884 printk("%s/%d is trying to release lock (",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07002885 curr->comm, task_pid_nr(curr));
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002886 print_lockdep_cache(lock);
2887 printk(") at:\n");
2888 print_ip_sym(ip);
2889 printk("but there are no more locks to release!\n");
2890 printk("\nother info that might help us debug this:\n");
2891 lockdep_print_held_locks(curr);
2892
2893 printk("\nstack backtrace:\n");
2894 dump_stack();
2895
2896 return 0;
2897}
2898
2899/*
2900 * Common debugging checks for both nested and non-nested unlock:
2901 */
2902static int check_unlock(struct task_struct *curr, struct lockdep_map *lock,
2903 unsigned long ip)
2904{
2905 if (unlikely(!debug_locks))
2906 return 0;
2907 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2908 return 0;
2909
2910 if (curr->lockdep_depth <= 0)
2911 return print_unlock_inbalance_bug(curr, lock, ip);
2912
2913 return 1;
2914}
2915
Peter Zijlstrabb97a912009-07-20 19:15:35 +02002916static int match_held_lock(struct held_lock *hlock, struct lockdep_map *lock)
2917{
2918 if (hlock->instance == lock)
2919 return 1;
2920
2921 if (hlock->references) {
2922 struct lock_class *class = lock->class_cache;
2923
2924 if (!class)
2925 class = look_up_lock_class(lock, 0);
2926
2927 if (DEBUG_LOCKS_WARN_ON(!class))
2928 return 0;
2929
2930 if (DEBUG_LOCKS_WARN_ON(!hlock->nest_lock))
2931 return 0;
2932
2933 if (hlock->class_idx == class - lock_classes + 1)
2934 return 1;
2935 }
2936
2937 return 0;
2938}
2939
Peter Zijlstra64aa3482008-08-11 09:30:21 +02002940static int
Peter Zijlstra00ef9f72008-12-04 09:00:17 +01002941__lock_set_class(struct lockdep_map *lock, const char *name,
2942 struct lock_class_key *key, unsigned int subclass,
2943 unsigned long ip)
Peter Zijlstra64aa3482008-08-11 09:30:21 +02002944{
2945 struct task_struct *curr = current;
2946 struct held_lock *hlock, *prev_hlock;
2947 struct lock_class *class;
2948 unsigned int depth;
2949 int i;
2950
2951 depth = curr->lockdep_depth;
2952 if (DEBUG_LOCKS_WARN_ON(!depth))
2953 return 0;
2954
2955 prev_hlock = NULL;
2956 for (i = depth-1; i >= 0; i--) {
2957 hlock = curr->held_locks + i;
2958 /*
2959 * We must not cross into another context:
2960 */
2961 if (prev_hlock && prev_hlock->irq_context != hlock->irq_context)
2962 break;
Peter Zijlstrabb97a912009-07-20 19:15:35 +02002963 if (match_held_lock(hlock, lock))
Peter Zijlstra64aa3482008-08-11 09:30:21 +02002964 goto found_it;
2965 prev_hlock = hlock;
2966 }
2967 return print_unlock_inbalance_bug(curr, lock, ip);
2968
2969found_it:
Peter Zijlstra00ef9f72008-12-04 09:00:17 +01002970 lockdep_init_map(lock, name, key, 0);
Peter Zijlstra64aa3482008-08-11 09:30:21 +02002971 class = register_lock_class(lock, subclass, 0);
Dave Jonesf82b2172008-08-11 09:30:23 +02002972 hlock->class_idx = class - lock_classes + 1;
Peter Zijlstra64aa3482008-08-11 09:30:21 +02002973
2974 curr->lockdep_depth = i;
2975 curr->curr_chain_key = hlock->prev_chain_key;
2976
2977 for (; i < depth; i++) {
2978 hlock = curr->held_locks + i;
2979 if (!__lock_acquire(hlock->instance,
Dave Jonesf82b2172008-08-11 09:30:23 +02002980 hlock_class(hlock)->subclass, hlock->trylock,
Peter Zijlstra64aa3482008-08-11 09:30:21 +02002981 hlock->read, hlock->check, hlock->hardirqs_off,
Peter Zijlstrabb97a912009-07-20 19:15:35 +02002982 hlock->nest_lock, hlock->acquire_ip,
2983 hlock->references))
Peter Zijlstra64aa3482008-08-11 09:30:21 +02002984 return 0;
2985 }
2986
2987 if (DEBUG_LOCKS_WARN_ON(curr->lockdep_depth != depth))
2988 return 0;
2989 return 1;
2990}
2991
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002992/*
2993 * Remove the lock to the list of currently held locks in a
2994 * potentially non-nested (out of order) manner. This is a
2995 * relatively rare operation, as all the unlock APIs default
2996 * to nested mode (which uses lock_release()):
2997 */
2998static int
2999lock_release_non_nested(struct task_struct *curr,
3000 struct lockdep_map *lock, unsigned long ip)
3001{
3002 struct held_lock *hlock, *prev_hlock;
3003 unsigned int depth;
3004 int i;
3005
3006 /*
3007 * Check whether the lock exists in the current stack
3008 * of held locks:
3009 */
3010 depth = curr->lockdep_depth;
3011 if (DEBUG_LOCKS_WARN_ON(!depth))
3012 return 0;
3013
3014 prev_hlock = NULL;
3015 for (i = depth-1; i >= 0; i--) {
3016 hlock = curr->held_locks + i;
3017 /*
3018 * We must not cross into another context:
3019 */
3020 if (prev_hlock && prev_hlock->irq_context != hlock->irq_context)
3021 break;
Peter Zijlstrabb97a912009-07-20 19:15:35 +02003022 if (match_held_lock(hlock, lock))
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003023 goto found_it;
3024 prev_hlock = hlock;
3025 }
3026 return print_unlock_inbalance_bug(curr, lock, ip);
3027
3028found_it:
Peter Zijlstrabb97a912009-07-20 19:15:35 +02003029 if (hlock->instance == lock)
3030 lock_release_holdtime(hlock);
3031
3032 if (hlock->references) {
3033 hlock->references--;
3034 if (hlock->references) {
3035 /*
3036 * We had, and after removing one, still have
3037 * references, the current lock stack is still
3038 * valid. We're done!
3039 */
3040 return 1;
3041 }
3042 }
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003043
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003044 /*
3045 * We have the right lock to unlock, 'hlock' points to it.
3046 * Now we remove it from the stack, and add back the other
3047 * entries (if any), recalculating the hash along the way:
3048 */
Peter Zijlstrabb97a912009-07-20 19:15:35 +02003049
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003050 curr->lockdep_depth = i;
3051 curr->curr_chain_key = hlock->prev_chain_key;
3052
3053 for (i++; i < depth; i++) {
3054 hlock = curr->held_locks + i;
3055 if (!__lock_acquire(hlock->instance,
Dave Jonesf82b2172008-08-11 09:30:23 +02003056 hlock_class(hlock)->subclass, hlock->trylock,
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003057 hlock->read, hlock->check, hlock->hardirqs_off,
Peter Zijlstrabb97a912009-07-20 19:15:35 +02003058 hlock->nest_lock, hlock->acquire_ip,
3059 hlock->references))
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003060 return 0;
3061 }
3062
3063 if (DEBUG_LOCKS_WARN_ON(curr->lockdep_depth != depth - 1))
3064 return 0;
3065 return 1;
3066}
3067
3068/*
3069 * Remove the lock to the list of currently held locks - this gets
3070 * called on mutex_unlock()/spin_unlock*() (or on a failed
3071 * mutex_lock_interruptible()). This is done for unlocks that nest
3072 * perfectly. (i.e. the current top of the lock-stack is unlocked)
3073 */
3074static int lock_release_nested(struct task_struct *curr,
3075 struct lockdep_map *lock, unsigned long ip)
3076{
3077 struct held_lock *hlock;
3078 unsigned int depth;
3079
3080 /*
3081 * Pop off the top of the lock stack:
3082 */
3083 depth = curr->lockdep_depth - 1;
3084 hlock = curr->held_locks + depth;
3085
3086 /*
3087 * Is the unlock non-nested:
3088 */
Peter Zijlstrabb97a912009-07-20 19:15:35 +02003089 if (hlock->instance != lock || hlock->references)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003090 return lock_release_non_nested(curr, lock, ip);
3091 curr->lockdep_depth--;
3092
3093 if (DEBUG_LOCKS_WARN_ON(!depth && (hlock->prev_chain_key != 0)))
3094 return 0;
3095
3096 curr->curr_chain_key = hlock->prev_chain_key;
3097
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003098 lock_release_holdtime(hlock);
3099
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003100#ifdef CONFIG_DEBUG_LOCKDEP
3101 hlock->prev_chain_key = 0;
Dave Jonesf82b2172008-08-11 09:30:23 +02003102 hlock->class_idx = 0;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003103 hlock->acquire_ip = 0;
3104 hlock->irq_context = 0;
3105#endif
3106 return 1;
3107}
3108
3109/*
3110 * Remove the lock to the list of currently held locks - this gets
3111 * called on mutex_unlock()/spin_unlock*() (or on a failed
3112 * mutex_lock_interruptible()). This is done for unlocks that nest
3113 * perfectly. (i.e. the current top of the lock-stack is unlocked)
3114 */
3115static void
3116__lock_release(struct lockdep_map *lock, int nested, unsigned long ip)
3117{
3118 struct task_struct *curr = current;
3119
3120 if (!check_unlock(curr, lock, ip))
3121 return;
3122
3123 if (nested) {
3124 if (!lock_release_nested(curr, lock, ip))
3125 return;
3126 } else {
3127 if (!lock_release_non_nested(curr, lock, ip))
3128 return;
3129 }
3130
3131 check_chain_key(curr);
3132}
3133
Peter Zijlstraf607c662009-07-20 19:16:29 +02003134static int __lock_is_held(struct lockdep_map *lock)
3135{
3136 struct task_struct *curr = current;
3137 int i;
3138
3139 for (i = 0; i < curr->lockdep_depth; i++) {
Peter Zijlstrabb97a912009-07-20 19:15:35 +02003140 struct held_lock *hlock = curr->held_locks + i;
3141
3142 if (match_held_lock(hlock, lock))
Peter Zijlstraf607c662009-07-20 19:16:29 +02003143 return 1;
3144 }
3145
3146 return 0;
3147}
3148
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003149/*
3150 * Check whether we follow the irq-flags state precisely:
3151 */
Steven Rostedt1d09daa2008-05-12 21:20:55 +02003152static void check_flags(unsigned long flags)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003153{
Ingo Molnar992860e2008-07-14 10:28:38 +02003154#if defined(CONFIG_PROVE_LOCKING) && defined(CONFIG_DEBUG_LOCKDEP) && \
3155 defined(CONFIG_TRACE_IRQFLAGS)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003156 if (!debug_locks)
3157 return;
3158
Ingo Molnar5f9fa8a2007-12-07 19:02:47 +01003159 if (irqs_disabled_flags(flags)) {
3160 if (DEBUG_LOCKS_WARN_ON(current->hardirqs_enabled)) {
3161 printk("possible reason: unannotated irqs-off.\n");
3162 }
3163 } else {
3164 if (DEBUG_LOCKS_WARN_ON(!current->hardirqs_enabled)) {
3165 printk("possible reason: unannotated irqs-on.\n");
3166 }
3167 }
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003168
3169 /*
3170 * We dont accurately track softirq state in e.g.
3171 * hardirq contexts (such as on 4KSTACKS), so only
3172 * check if not in hardirq contexts:
3173 */
3174 if (!hardirq_count()) {
3175 if (softirq_count())
3176 DEBUG_LOCKS_WARN_ON(current->softirqs_enabled);
3177 else
3178 DEBUG_LOCKS_WARN_ON(!current->softirqs_enabled);
3179 }
3180
3181 if (!debug_locks)
3182 print_irqtrace_events(current);
3183#endif
3184}
3185
Peter Zijlstra00ef9f72008-12-04 09:00:17 +01003186void lock_set_class(struct lockdep_map *lock, const char *name,
3187 struct lock_class_key *key, unsigned int subclass,
3188 unsigned long ip)
Peter Zijlstra64aa3482008-08-11 09:30:21 +02003189{
3190 unsigned long flags;
3191
3192 if (unlikely(current->lockdep_recursion))
3193 return;
3194
3195 raw_local_irq_save(flags);
3196 current->lockdep_recursion = 1;
3197 check_flags(flags);
Peter Zijlstra00ef9f72008-12-04 09:00:17 +01003198 if (__lock_set_class(lock, name, key, subclass, ip))
Peter Zijlstra64aa3482008-08-11 09:30:21 +02003199 check_chain_key(current);
3200 current->lockdep_recursion = 0;
3201 raw_local_irq_restore(flags);
3202}
Peter Zijlstra00ef9f72008-12-04 09:00:17 +01003203EXPORT_SYMBOL_GPL(lock_set_class);
Peter Zijlstra64aa3482008-08-11 09:30:21 +02003204
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003205/*
3206 * We are not always called with irqs disabled - do that here,
3207 * and also avoid lockdep recursion:
3208 */
Steven Rostedt1d09daa2008-05-12 21:20:55 +02003209void lock_acquire(struct lockdep_map *lock, unsigned int subclass,
Peter Zijlstra7531e2f2008-08-11 09:30:24 +02003210 int trylock, int read, int check,
3211 struct lockdep_map *nest_lock, unsigned long ip)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003212{
3213 unsigned long flags;
3214
3215 if (unlikely(current->lockdep_recursion))
3216 return;
3217
3218 raw_local_irq_save(flags);
3219 check_flags(flags);
3220
3221 current->lockdep_recursion = 1;
Frederic Weisbeckerdb2c4c72010-02-02 23:34:40 +01003222 trace_lock_acquire(lock, subclass, trylock, read, check, nest_lock, ip);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003223 __lock_acquire(lock, subclass, trylock, read, check,
Peter Zijlstrabb97a912009-07-20 19:15:35 +02003224 irqs_disabled_flags(flags), nest_lock, ip, 0);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003225 current->lockdep_recursion = 0;
3226 raw_local_irq_restore(flags);
3227}
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003228EXPORT_SYMBOL_GPL(lock_acquire);
3229
Steven Rostedt1d09daa2008-05-12 21:20:55 +02003230void lock_release(struct lockdep_map *lock, int nested,
Steven Rostedt0764d232008-05-12 21:20:44 +02003231 unsigned long ip)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003232{
3233 unsigned long flags;
3234
3235 if (unlikely(current->lockdep_recursion))
3236 return;
3237
3238 raw_local_irq_save(flags);
3239 check_flags(flags);
3240 current->lockdep_recursion = 1;
Frederic Weisbeckerdb2c4c72010-02-02 23:34:40 +01003241 trace_lock_release(lock, nested, ip);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003242 __lock_release(lock, nested, ip);
3243 current->lockdep_recursion = 0;
3244 raw_local_irq_restore(flags);
3245}
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003246EXPORT_SYMBOL_GPL(lock_release);
3247
Peter Zijlstraf607c662009-07-20 19:16:29 +02003248int lock_is_held(struct lockdep_map *lock)
3249{
3250 unsigned long flags;
3251 int ret = 0;
3252
3253 if (unlikely(current->lockdep_recursion))
3254 return ret;
3255
3256 raw_local_irq_save(flags);
3257 check_flags(flags);
3258
3259 current->lockdep_recursion = 1;
3260 ret = __lock_is_held(lock);
3261 current->lockdep_recursion = 0;
3262 raw_local_irq_restore(flags);
3263
3264 return ret;
3265}
3266EXPORT_SYMBOL_GPL(lock_is_held);
3267
Nick Piggincf40bd12009-01-21 08:12:39 +01003268void lockdep_set_current_reclaim_state(gfp_t gfp_mask)
3269{
3270 current->lockdep_reclaim_gfp = gfp_mask;
3271}
3272
3273void lockdep_clear_current_reclaim_state(void)
3274{
3275 current->lockdep_reclaim_gfp = 0;
3276}
3277
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003278#ifdef CONFIG_LOCK_STAT
3279static int
3280print_lock_contention_bug(struct task_struct *curr, struct lockdep_map *lock,
3281 unsigned long ip)
3282{
3283 if (!debug_locks_off())
3284 return 0;
3285 if (debug_locks_silent)
3286 return 0;
3287
3288 printk("\n=================================\n");
3289 printk( "[ BUG: bad contention detected! ]\n");
3290 printk( "---------------------------------\n");
3291 printk("%s/%d is trying to contend lock (",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07003292 curr->comm, task_pid_nr(curr));
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003293 print_lockdep_cache(lock);
3294 printk(") at:\n");
3295 print_ip_sym(ip);
3296 printk("but there are no locks held!\n");
3297 printk("\nother info that might help us debug this:\n");
3298 lockdep_print_held_locks(curr);
3299
3300 printk("\nstack backtrace:\n");
3301 dump_stack();
3302
3303 return 0;
3304}
3305
3306static void
3307__lock_contended(struct lockdep_map *lock, unsigned long ip)
3308{
3309 struct task_struct *curr = current;
3310 struct held_lock *hlock, *prev_hlock;
3311 struct lock_class_stats *stats;
3312 unsigned int depth;
Peter Zijlstrac7e78cf2008-10-16 23:17:09 +02003313 int i, contention_point, contending_point;
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003314
3315 depth = curr->lockdep_depth;
3316 if (DEBUG_LOCKS_WARN_ON(!depth))
3317 return;
3318
3319 prev_hlock = NULL;
3320 for (i = depth-1; i >= 0; i--) {
3321 hlock = curr->held_locks + i;
3322 /*
3323 * We must not cross into another context:
3324 */
3325 if (prev_hlock && prev_hlock->irq_context != hlock->irq_context)
3326 break;
Peter Zijlstrabb97a912009-07-20 19:15:35 +02003327 if (match_held_lock(hlock, lock))
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003328 goto found_it;
3329 prev_hlock = hlock;
3330 }
3331 print_lock_contention_bug(curr, lock, ip);
3332 return;
3333
3334found_it:
Peter Zijlstrabb97a912009-07-20 19:15:35 +02003335 if (hlock->instance != lock)
3336 return;
3337
Peter Zijlstra3365e7792009-10-09 10:12:41 +02003338 hlock->waittime_stamp = lockstat_clock();
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003339
Peter Zijlstrac7e78cf2008-10-16 23:17:09 +02003340 contention_point = lock_point(hlock_class(hlock)->contention_point, ip);
3341 contending_point = lock_point(hlock_class(hlock)->contending_point,
3342 lock->ip);
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003343
Dave Jonesf82b2172008-08-11 09:30:23 +02003344 stats = get_lock_stats(hlock_class(hlock));
Peter Zijlstrac7e78cf2008-10-16 23:17:09 +02003345 if (contention_point < LOCKSTAT_POINTS)
3346 stats->contention_point[contention_point]++;
3347 if (contending_point < LOCKSTAT_POINTS)
3348 stats->contending_point[contending_point]++;
Peter Zijlstra96645672007-07-19 01:49:00 -07003349 if (lock->cpu != smp_processor_id())
3350 stats->bounces[bounce_contended + !!hlock->read]++;
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003351 put_lock_stats(stats);
3352}
3353
3354static void
Peter Zijlstrac7e78cf2008-10-16 23:17:09 +02003355__lock_acquired(struct lockdep_map *lock, unsigned long ip)
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003356{
3357 struct task_struct *curr = current;
3358 struct held_lock *hlock, *prev_hlock;
3359 struct lock_class_stats *stats;
3360 unsigned int depth;
Peter Zijlstra3365e7792009-10-09 10:12:41 +02003361 u64 now, waittime = 0;
Peter Zijlstra96645672007-07-19 01:49:00 -07003362 int i, cpu;
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003363
3364 depth = curr->lockdep_depth;
3365 if (DEBUG_LOCKS_WARN_ON(!depth))
3366 return;
3367
3368 prev_hlock = NULL;
3369 for (i = depth-1; i >= 0; i--) {
3370 hlock = curr->held_locks + i;
3371 /*
3372 * We must not cross into another context:
3373 */
3374 if (prev_hlock && prev_hlock->irq_context != hlock->irq_context)
3375 break;
Peter Zijlstrabb97a912009-07-20 19:15:35 +02003376 if (match_held_lock(hlock, lock))
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003377 goto found_it;
3378 prev_hlock = hlock;
3379 }
3380 print_lock_contention_bug(curr, lock, _RET_IP_);
3381 return;
3382
3383found_it:
Peter Zijlstrabb97a912009-07-20 19:15:35 +02003384 if (hlock->instance != lock)
3385 return;
3386
Peter Zijlstra96645672007-07-19 01:49:00 -07003387 cpu = smp_processor_id();
3388 if (hlock->waittime_stamp) {
Peter Zijlstra3365e7792009-10-09 10:12:41 +02003389 now = lockstat_clock();
Peter Zijlstra96645672007-07-19 01:49:00 -07003390 waittime = now - hlock->waittime_stamp;
3391 hlock->holdtime_stamp = now;
3392 }
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003393
Frederic Weisbecker20625012009-04-06 01:49:33 +02003394 trace_lock_acquired(lock, ip, waittime);
3395
Dave Jonesf82b2172008-08-11 09:30:23 +02003396 stats = get_lock_stats(hlock_class(hlock));
Peter Zijlstra96645672007-07-19 01:49:00 -07003397 if (waittime) {
3398 if (hlock->read)
3399 lock_time_inc(&stats->read_waittime, waittime);
3400 else
3401 lock_time_inc(&stats->write_waittime, waittime);
3402 }
3403 if (lock->cpu != cpu)
3404 stats->bounces[bounce_acquired + !!hlock->read]++;
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003405 put_lock_stats(stats);
Peter Zijlstra96645672007-07-19 01:49:00 -07003406
3407 lock->cpu = cpu;
Peter Zijlstrac7e78cf2008-10-16 23:17:09 +02003408 lock->ip = ip;
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003409}
3410
3411void lock_contended(struct lockdep_map *lock, unsigned long ip)
3412{
3413 unsigned long flags;
3414
3415 if (unlikely(!lock_stat))
3416 return;
3417
3418 if (unlikely(current->lockdep_recursion))
3419 return;
3420
3421 raw_local_irq_save(flags);
3422 check_flags(flags);
3423 current->lockdep_recursion = 1;
Frederic Weisbeckerdb2c4c72010-02-02 23:34:40 +01003424 trace_lock_contended(lock, ip);
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003425 __lock_contended(lock, ip);
3426 current->lockdep_recursion = 0;
3427 raw_local_irq_restore(flags);
3428}
3429EXPORT_SYMBOL_GPL(lock_contended);
3430
Peter Zijlstrac7e78cf2008-10-16 23:17:09 +02003431void lock_acquired(struct lockdep_map *lock, unsigned long ip)
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003432{
3433 unsigned long flags;
3434
3435 if (unlikely(!lock_stat))
3436 return;
3437
3438 if (unlikely(current->lockdep_recursion))
3439 return;
3440
3441 raw_local_irq_save(flags);
3442 check_flags(flags);
3443 current->lockdep_recursion = 1;
Peter Zijlstrac7e78cf2008-10-16 23:17:09 +02003444 __lock_acquired(lock, ip);
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003445 current->lockdep_recursion = 0;
3446 raw_local_irq_restore(flags);
3447}
3448EXPORT_SYMBOL_GPL(lock_acquired);
3449#endif
3450
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003451/*
3452 * Used by the testsuite, sanitize the validator state
3453 * after a simulated failure:
3454 */
3455
3456void lockdep_reset(void)
3457{
3458 unsigned long flags;
Ingo Molnar23d95a02006-12-13 00:34:40 -08003459 int i;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003460
3461 raw_local_irq_save(flags);
3462 current->curr_chain_key = 0;
3463 current->lockdep_depth = 0;
3464 current->lockdep_recursion = 0;
3465 memset(current->held_locks, 0, MAX_LOCK_DEPTH*sizeof(struct held_lock));
3466 nr_hardirq_chains = 0;
3467 nr_softirq_chains = 0;
3468 nr_process_chains = 0;
3469 debug_locks = 1;
Ingo Molnar23d95a02006-12-13 00:34:40 -08003470 for (i = 0; i < CHAINHASH_SIZE; i++)
3471 INIT_LIST_HEAD(chainhash_table + i);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003472 raw_local_irq_restore(flags);
3473}
3474
3475static void zap_class(struct lock_class *class)
3476{
3477 int i;
3478
3479 /*
3480 * Remove all dependencies this lock is
3481 * involved in:
3482 */
3483 for (i = 0; i < nr_list_entries; i++) {
3484 if (list_entries[i].class == class)
3485 list_del_rcu(&list_entries[i].entry);
3486 }
3487 /*
3488 * Unhash the class and remove it from the all_lock_classes list:
3489 */
3490 list_del_rcu(&class->hash_entry);
3491 list_del_rcu(&class->lock_entry);
3492
Rabin Vincent8bfe0292008-08-11 09:30:26 +02003493 class->key = NULL;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003494}
3495
Arjan van de Venfabe8742008-01-24 07:00:45 +01003496static inline int within(const void *addr, void *start, unsigned long size)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003497{
3498 return addr >= start && addr < start + size;
3499}
3500
3501void lockdep_free_key_range(void *start, unsigned long size)
3502{
3503 struct lock_class *class, *next;
3504 struct list_head *head;
3505 unsigned long flags;
3506 int i;
Nick Piggin5a26db52008-01-16 09:51:58 +01003507 int locked;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003508
3509 raw_local_irq_save(flags);
Nick Piggin5a26db52008-01-16 09:51:58 +01003510 locked = graph_lock();
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003511
3512 /*
3513 * Unhash all classes that were created by this module:
3514 */
3515 for (i = 0; i < CLASSHASH_SIZE; i++) {
3516 head = classhash_table + i;
3517 if (list_empty(head))
3518 continue;
Arjan van de Venfabe8742008-01-24 07:00:45 +01003519 list_for_each_entry_safe(class, next, head, hash_entry) {
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003520 if (within(class->key, start, size))
3521 zap_class(class);
Arjan van de Venfabe8742008-01-24 07:00:45 +01003522 else if (within(class->name, start, size))
3523 zap_class(class);
3524 }
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003525 }
3526
Nick Piggin5a26db52008-01-16 09:51:58 +01003527 if (locked)
3528 graph_unlock();
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003529 raw_local_irq_restore(flags);
3530}
3531
3532void lockdep_reset_lock(struct lockdep_map *lock)
3533{
Ingo Molnard6d897c2006-07-10 04:44:04 -07003534 struct lock_class *class, *next;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003535 struct list_head *head;
3536 unsigned long flags;
3537 int i, j;
Nick Piggin5a26db52008-01-16 09:51:58 +01003538 int locked;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003539
3540 raw_local_irq_save(flags);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003541
3542 /*
Ingo Molnard6d897c2006-07-10 04:44:04 -07003543 * Remove all classes this lock might have:
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003544 */
Ingo Molnard6d897c2006-07-10 04:44:04 -07003545 for (j = 0; j < MAX_LOCKDEP_SUBCLASSES; j++) {
3546 /*
3547 * If the class exists we look it up and zap it:
3548 */
3549 class = look_up_lock_class(lock, j);
3550 if (class)
3551 zap_class(class);
3552 }
3553 /*
3554 * Debug check: in the end all mapped classes should
3555 * be gone.
3556 */
Nick Piggin5a26db52008-01-16 09:51:58 +01003557 locked = graph_lock();
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003558 for (i = 0; i < CLASSHASH_SIZE; i++) {
3559 head = classhash_table + i;
3560 if (list_empty(head))
3561 continue;
3562 list_for_each_entry_safe(class, next, head, hash_entry) {
Ingo Molnard6d897c2006-07-10 04:44:04 -07003563 if (unlikely(class == lock->class_cache)) {
Ingo Molnar74c383f2006-12-13 00:34:43 -08003564 if (debug_locks_off_graph_unlock())
3565 WARN_ON(1);
Ingo Molnard6d897c2006-07-10 04:44:04 -07003566 goto out_restore;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003567 }
3568 }
3569 }
Nick Piggin5a26db52008-01-16 09:51:58 +01003570 if (locked)
3571 graph_unlock();
Ingo Molnard6d897c2006-07-10 04:44:04 -07003572
3573out_restore:
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003574 raw_local_irq_restore(flags);
3575}
3576
Sam Ravnborg14999932007-02-28 20:12:31 -08003577void lockdep_init(void)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003578{
3579 int i;
3580
3581 /*
3582 * Some architectures have their own start_kernel()
3583 * code which calls lockdep_init(), while we also
3584 * call lockdep_init() from the start_kernel() itself,
3585 * and we want to initialize the hashes only once:
3586 */
3587 if (lockdep_initialized)
3588 return;
3589
3590 for (i = 0; i < CLASSHASH_SIZE; i++)
3591 INIT_LIST_HEAD(classhash_table + i);
3592
3593 for (i = 0; i < CHAINHASH_SIZE; i++)
3594 INIT_LIST_HEAD(chainhash_table + i);
3595
3596 lockdep_initialized = 1;
3597}
3598
3599void __init lockdep_info(void)
3600{
3601 printk("Lock dependency validator: Copyright (c) 2006 Red Hat, Inc., Ingo Molnar\n");
3602
Li Zefanb0788ca2008-11-21 15:57:32 +08003603 printk("... MAX_LOCKDEP_SUBCLASSES: %lu\n", MAX_LOCKDEP_SUBCLASSES);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003604 printk("... MAX_LOCK_DEPTH: %lu\n", MAX_LOCK_DEPTH);
3605 printk("... MAX_LOCKDEP_KEYS: %lu\n", MAX_LOCKDEP_KEYS);
Li Zefanb0788ca2008-11-21 15:57:32 +08003606 printk("... CLASSHASH_SIZE: %lu\n", CLASSHASH_SIZE);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003607 printk("... MAX_LOCKDEP_ENTRIES: %lu\n", MAX_LOCKDEP_ENTRIES);
3608 printk("... MAX_LOCKDEP_CHAINS: %lu\n", MAX_LOCKDEP_CHAINS);
3609 printk("... CHAINHASH_SIZE: %lu\n", CHAINHASH_SIZE);
3610
3611 printk(" memory used by lock dependency info: %lu kB\n",
3612 (sizeof(struct lock_class) * MAX_LOCKDEP_KEYS +
3613 sizeof(struct list_head) * CLASSHASH_SIZE +
3614 sizeof(struct lock_list) * MAX_LOCKDEP_ENTRIES +
3615 sizeof(struct lock_chain) * MAX_LOCKDEP_CHAINS +
Ming Lei90629202009-08-02 21:43:36 +08003616 sizeof(struct list_head) * CHAINHASH_SIZE
Ming Lei4dd861d2009-07-16 15:44:29 +02003617#ifdef CONFIG_PROVE_LOCKING
Ming Leie351b662009-07-22 22:48:09 +08003618 + sizeof(struct circular_queue)
Ming Lei4dd861d2009-07-16 15:44:29 +02003619#endif
Ming Lei90629202009-08-02 21:43:36 +08003620 ) / 1024
Ming Lei4dd861d2009-07-16 15:44:29 +02003621 );
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003622
3623 printk(" per task-struct memory footprint: %lu bytes\n",
3624 sizeof(struct held_lock) * MAX_LOCK_DEPTH);
3625
3626#ifdef CONFIG_DEBUG_LOCKDEP
Johannes Bergc71063c2007-07-19 01:49:02 -07003627 if (lockdep_init_error) {
3628 printk("WARNING: lockdep init error! Arch code didn't call lockdep_init() early enough?\n");
3629 printk("Call stack leading to lockdep invocation was:\n");
3630 print_stack_trace(&lockdep_init_trace, 0);
3631 }
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003632#endif
3633}
3634
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003635static void
3636print_freed_lock_bug(struct task_struct *curr, const void *mem_from,
Arjan van de Ven55794a42006-07-10 04:44:03 -07003637 const void *mem_to, struct held_lock *hlock)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003638{
3639 if (!debug_locks_off())
3640 return;
3641 if (debug_locks_silent)
3642 return;
3643
3644 printk("\n=========================\n");
3645 printk( "[ BUG: held lock freed! ]\n");
3646 printk( "-------------------------\n");
3647 printk("%s/%d is freeing memory %p-%p, with a lock still held there!\n",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07003648 curr->comm, task_pid_nr(curr), mem_from, mem_to-1);
Arjan van de Ven55794a42006-07-10 04:44:03 -07003649 print_lock(hlock);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003650 lockdep_print_held_locks(curr);
3651
3652 printk("\nstack backtrace:\n");
3653 dump_stack();
3654}
3655
Oleg Nesterov54561782007-12-05 15:46:09 +01003656static inline int not_in_range(const void* mem_from, unsigned long mem_len,
3657 const void* lock_from, unsigned long lock_len)
3658{
3659 return lock_from + lock_len <= mem_from ||
3660 mem_from + mem_len <= lock_from;
3661}
3662
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003663/*
3664 * Called when kernel memory is freed (or unmapped), or if a lock
3665 * is destroyed or reinitialized - this code checks whether there is
3666 * any held lock in the memory range of <from> to <to>:
3667 */
3668void debug_check_no_locks_freed(const void *mem_from, unsigned long mem_len)
3669{
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003670 struct task_struct *curr = current;
3671 struct held_lock *hlock;
3672 unsigned long flags;
3673 int i;
3674
3675 if (unlikely(!debug_locks))
3676 return;
3677
3678 local_irq_save(flags);
3679 for (i = 0; i < curr->lockdep_depth; i++) {
3680 hlock = curr->held_locks + i;
3681
Oleg Nesterov54561782007-12-05 15:46:09 +01003682 if (not_in_range(mem_from, mem_len, hlock->instance,
3683 sizeof(*hlock->instance)))
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003684 continue;
3685
Oleg Nesterov54561782007-12-05 15:46:09 +01003686 print_freed_lock_bug(curr, mem_from, mem_from + mem_len, hlock);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003687 break;
3688 }
3689 local_irq_restore(flags);
3690}
Peter Zijlstraed075362006-12-06 20:35:24 -08003691EXPORT_SYMBOL_GPL(debug_check_no_locks_freed);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003692
3693static void print_held_locks_bug(struct task_struct *curr)
3694{
3695 if (!debug_locks_off())
3696 return;
3697 if (debug_locks_silent)
3698 return;
3699
3700 printk("\n=====================================\n");
3701 printk( "[ BUG: lock held at task exit time! ]\n");
3702 printk( "-------------------------------------\n");
3703 printk("%s/%d is exiting with locks still held!\n",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07003704 curr->comm, task_pid_nr(curr));
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003705 lockdep_print_held_locks(curr);
3706
3707 printk("\nstack backtrace:\n");
3708 dump_stack();
3709}
3710
3711void debug_check_no_locks_held(struct task_struct *task)
3712{
3713 if (unlikely(task->lockdep_depth > 0))
3714 print_held_locks_bug(task);
3715}
3716
3717void debug_show_all_locks(void)
3718{
3719 struct task_struct *g, *p;
3720 int count = 10;
3721 int unlock = 1;
3722
Jarek Poplawski9c35dd72007-03-22 00:11:28 -08003723 if (unlikely(!debug_locks)) {
3724 printk("INFO: lockdep is turned off.\n");
3725 return;
3726 }
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003727 printk("\nShowing all locks held in the system:\n");
3728
3729 /*
3730 * Here we try to get the tasklist_lock as hard as possible,
3731 * if not successful after 2 seconds we ignore it (but keep
3732 * trying). This is to enable a debug printout even if a
3733 * tasklist_lock-holding task deadlocks or crashes.
3734 */
3735retry:
3736 if (!read_trylock(&tasklist_lock)) {
3737 if (count == 10)
3738 printk("hm, tasklist_lock locked, retrying... ");
3739 if (count) {
3740 count--;
3741 printk(" #%d", 10-count);
3742 mdelay(200);
3743 goto retry;
3744 }
3745 printk(" ignoring it.\n");
3746 unlock = 0;
qinghuang feng46fec7a2008-10-28 17:24:28 +08003747 } else {
3748 if (count != 10)
3749 printk(KERN_CONT " locked it.\n");
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003750 }
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003751
3752 do_each_thread(g, p) {
Ingo Molnar85684872007-12-05 15:46:09 +01003753 /*
3754 * It's not reliable to print a task's held locks
3755 * if it's not sleeping (or if it's not the current
3756 * task):
3757 */
3758 if (p->state == TASK_RUNNING && p != current)
3759 continue;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003760 if (p->lockdep_depth)
3761 lockdep_print_held_locks(p);
3762 if (!unlock)
3763 if (read_trylock(&tasklist_lock))
3764 unlock = 1;
3765 } while_each_thread(g, p);
3766
3767 printk("\n");
3768 printk("=============================================\n\n");
3769
3770 if (unlock)
3771 read_unlock(&tasklist_lock);
3772}
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003773EXPORT_SYMBOL_GPL(debug_show_all_locks);
3774
Ingo Molnar82a1fcb2008-01-25 21:08:02 +01003775/*
3776 * Careful: only use this function if you are sure that
3777 * the task cannot run in parallel!
3778 */
3779void __debug_show_held_locks(struct task_struct *task)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003780{
Jarek Poplawski9c35dd72007-03-22 00:11:28 -08003781 if (unlikely(!debug_locks)) {
3782 printk("INFO: lockdep is turned off.\n");
3783 return;
3784 }
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003785 lockdep_print_held_locks(task);
3786}
Ingo Molnar82a1fcb2008-01-25 21:08:02 +01003787EXPORT_SYMBOL_GPL(__debug_show_held_locks);
3788
3789void debug_show_held_locks(struct task_struct *task)
3790{
3791 __debug_show_held_locks(task);
3792}
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003793EXPORT_SYMBOL_GPL(debug_show_held_locks);
Peter Zijlstrab351d162007-10-11 22:11:12 +02003794
3795void lockdep_sys_exit(void)
3796{
3797 struct task_struct *curr = current;
3798
3799 if (unlikely(curr->lockdep_depth)) {
3800 if (!debug_locks_off())
3801 return;
3802 printk("\n================================================\n");
3803 printk( "[ BUG: lock held when returning to user space! ]\n");
3804 printk( "------------------------------------------------\n");
3805 printk("%s/%d is leaving the kernel with locks still held!\n",
3806 curr->comm, curr->pid);
3807 lockdep_print_held_locks(curr);
3808 }
3809}
Paul E. McKenney0632eb32010-02-22 17:04:47 -08003810
3811void lockdep_rcu_dereference(const char *file, const int line)
3812{
3813 struct task_struct *curr = current;
3814
3815 if (!debug_locks_off())
3816 return;
Paul E. McKenney056ba4a2010-02-25 14:06:46 -08003817 printk("\n===================================================\n");
3818 printk( "[ INFO: suspicious rcu_dereference_check() usage. ]\n");
3819 printk( "---------------------------------------------------\n");
Paul E. McKenney0632eb32010-02-22 17:04:47 -08003820 printk("%s:%d invoked rcu_dereference_check() without protection!\n",
3821 file, line);
3822 printk("\nother info that might help us debug this:\n\n");
Paul E. McKenneycc5b83a2010-03-03 07:46:59 -08003823 printk("\nrcu_scheduler_active = %d, debug_locks = %d\n", rcu_scheduler_active, debug_locks);
Paul E. McKenney0632eb32010-02-22 17:04:47 -08003824 lockdep_print_held_locks(curr);
3825 printk("\nstack backtrace:\n");
3826 dump_stack();
3827}
3828EXPORT_SYMBOL_GPL(lockdep_rcu_dereference);