blob: 0b863c83a774b876ffcfb250f8dacfe64c68945d [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>
Ingo Molnarfbb9ce952006-07-03 00:24:50 -070044
45#include <asm/sections.h>
46
47#include "lockdep_internals.h"
48
Peter Zijlstraf20786f2007-07-19 01:48:56 -070049#ifdef CONFIG_PROVE_LOCKING
50int prove_locking = 1;
51module_param(prove_locking, int, 0644);
52#else
53#define prove_locking 0
54#endif
55
56#ifdef CONFIG_LOCK_STAT
57int lock_stat = 1;
58module_param(lock_stat, int, 0644);
59#else
60#define lock_stat 0
61#endif
62
Ingo Molnarfbb9ce952006-07-03 00:24:50 -070063/*
Ingo Molnar74c383f2006-12-13 00:34:43 -080064 * lockdep_lock: protects the lockdep graph, the hashes and the
65 * class/list/hash allocators.
Ingo Molnarfbb9ce952006-07-03 00:24:50 -070066 *
67 * This is one of the rare exceptions where it's justified
68 * to use a raw spinlock - we really dont want the spinlock
Ingo Molnar74c383f2006-12-13 00:34:43 -080069 * code to recurse back into the lockdep code...
Ingo Molnarfbb9ce952006-07-03 00:24:50 -070070 */
Ingo Molnar74c383f2006-12-13 00:34:43 -080071static raw_spinlock_t lockdep_lock = (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED;
72
73static int graph_lock(void)
74{
75 __raw_spin_lock(&lockdep_lock);
76 /*
77 * Make sure that if another CPU detected a bug while
78 * walking the graph we dont change it (while the other
79 * CPU is busy printing out stuff with the graph lock
80 * dropped already)
81 */
82 if (!debug_locks) {
83 __raw_spin_unlock(&lockdep_lock);
84 return 0;
85 }
Steven Rostedtbb065af2008-05-12 21:21:00 +020086 /* prevent any recursions within lockdep from causing deadlocks */
87 current->lockdep_recursion++;
Ingo Molnar74c383f2006-12-13 00:34:43 -080088 return 1;
89}
90
91static inline int graph_unlock(void)
92{
Jarek Poplawski381a2292007-02-10 01:44:58 -080093 if (debug_locks && !__raw_spin_is_locked(&lockdep_lock))
94 return DEBUG_LOCKS_WARN_ON(1);
95
Steven Rostedtbb065af2008-05-12 21:21:00 +020096 current->lockdep_recursion--;
Ingo Molnar74c383f2006-12-13 00:34:43 -080097 __raw_spin_unlock(&lockdep_lock);
98 return 0;
99}
100
101/*
102 * Turn lock debugging off and return with 0 if it was off already,
103 * and also release the graph lock:
104 */
105static inline int debug_locks_off_graph_unlock(void)
106{
107 int ret = debug_locks_off();
108
109 __raw_spin_unlock(&lockdep_lock);
110
111 return ret;
112}
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700113
114static int lockdep_initialized;
115
116unsigned long nr_list_entries;
117static struct lock_list list_entries[MAX_LOCKDEP_ENTRIES];
118
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700119/*
120 * All data structures here are protected by the global debug_lock.
121 *
122 * Mutex key structs only get allocated, once during bootup, and never
123 * get freed - this significantly simplifies the debugging code.
124 */
125unsigned long nr_lock_classes;
126static struct lock_class lock_classes[MAX_LOCKDEP_KEYS];
127
Dave Jonesf82b2172008-08-11 09:30:23 +0200128static inline struct lock_class *hlock_class(struct held_lock *hlock)
129{
130 if (!hlock->class_idx) {
131 DEBUG_LOCKS_WARN_ON(1);
132 return NULL;
133 }
134 return lock_classes + hlock->class_idx - 1;
135}
136
Peter Zijlstraf20786f2007-07-19 01:48:56 -0700137#ifdef CONFIG_LOCK_STAT
138static DEFINE_PER_CPU(struct lock_class_stats[MAX_LOCKDEP_KEYS], lock_stats);
139
Peter Zijlstrac7e78cf2008-10-16 23:17:09 +0200140static int lock_point(unsigned long points[], unsigned long ip)
Peter Zijlstraf20786f2007-07-19 01:48:56 -0700141{
142 int i;
143
Peter Zijlstrac7e78cf2008-10-16 23:17:09 +0200144 for (i = 0; i < LOCKSTAT_POINTS; i++) {
145 if (points[i] == 0) {
146 points[i] = ip;
Peter Zijlstraf20786f2007-07-19 01:48:56 -0700147 break;
148 }
Peter Zijlstrac7e78cf2008-10-16 23:17:09 +0200149 if (points[i] == ip)
Peter Zijlstraf20786f2007-07-19 01:48:56 -0700150 break;
151 }
152
153 return i;
154}
155
156static void lock_time_inc(struct lock_time *lt, s64 time)
157{
158 if (time > lt->max)
159 lt->max = time;
160
161 if (time < lt->min || !lt->min)
162 lt->min = time;
163
164 lt->total += time;
165 lt->nr++;
166}
167
Peter Zijlstrac46261d2007-07-19 01:48:57 -0700168static inline void lock_time_add(struct lock_time *src, struct lock_time *dst)
169{
170 dst->min += src->min;
171 dst->max += src->max;
172 dst->total += src->total;
173 dst->nr += src->nr;
174}
175
176struct lock_class_stats lock_stats(struct lock_class *class)
177{
178 struct lock_class_stats stats;
179 int cpu, i;
180
181 memset(&stats, 0, sizeof(struct lock_class_stats));
182 for_each_possible_cpu(cpu) {
183 struct lock_class_stats *pcs =
184 &per_cpu(lock_stats, cpu)[class - lock_classes];
185
186 for (i = 0; i < ARRAY_SIZE(stats.contention_point); i++)
187 stats.contention_point[i] += pcs->contention_point[i];
188
Peter Zijlstrac7e78cf2008-10-16 23:17:09 +0200189 for (i = 0; i < ARRAY_SIZE(stats.contending_point); i++)
190 stats.contending_point[i] += pcs->contending_point[i];
191
Peter Zijlstrac46261d2007-07-19 01:48:57 -0700192 lock_time_add(&pcs->read_waittime, &stats.read_waittime);
193 lock_time_add(&pcs->write_waittime, &stats.write_waittime);
194
195 lock_time_add(&pcs->read_holdtime, &stats.read_holdtime);
196 lock_time_add(&pcs->write_holdtime, &stats.write_holdtime);
Peter Zijlstra96645672007-07-19 01:49:00 -0700197
198 for (i = 0; i < ARRAY_SIZE(stats.bounces); i++)
199 stats.bounces[i] += pcs->bounces[i];
Peter Zijlstrac46261d2007-07-19 01:48:57 -0700200 }
201
202 return stats;
203}
204
205void clear_lock_stats(struct lock_class *class)
206{
207 int cpu;
208
209 for_each_possible_cpu(cpu) {
210 struct lock_class_stats *cpu_stats =
211 &per_cpu(lock_stats, cpu)[class - lock_classes];
212
213 memset(cpu_stats, 0, sizeof(struct lock_class_stats));
214 }
215 memset(class->contention_point, 0, sizeof(class->contention_point));
Peter Zijlstrac7e78cf2008-10-16 23:17:09 +0200216 memset(class->contending_point, 0, sizeof(class->contending_point));
Peter Zijlstrac46261d2007-07-19 01:48:57 -0700217}
218
Peter Zijlstraf20786f2007-07-19 01:48:56 -0700219static struct lock_class_stats *get_lock_stats(struct lock_class *class)
220{
221 return &get_cpu_var(lock_stats)[class - lock_classes];
222}
223
224static void put_lock_stats(struct lock_class_stats *stats)
225{
226 put_cpu_var(lock_stats);
227}
228
229static void lock_release_holdtime(struct held_lock *hlock)
230{
231 struct lock_class_stats *stats;
232 s64 holdtime;
233
234 if (!lock_stat)
235 return;
236
237 holdtime = sched_clock() - hlock->holdtime_stamp;
238
Dave Jonesf82b2172008-08-11 09:30:23 +0200239 stats = get_lock_stats(hlock_class(hlock));
Peter Zijlstraf20786f2007-07-19 01:48:56 -0700240 if (hlock->read)
241 lock_time_inc(&stats->read_holdtime, holdtime);
242 else
243 lock_time_inc(&stats->write_holdtime, holdtime);
244 put_lock_stats(stats);
245}
246#else
247static inline void lock_release_holdtime(struct held_lock *hlock)
248{
249}
250#endif
251
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700252/*
253 * We keep a global list of all lock classes. The list only grows,
254 * never shrinks. The list is only accessed with the lockdep
255 * spinlock lock held.
256 */
257LIST_HEAD(all_lock_classes);
258
259/*
260 * The lockdep classes are in a hash-table as well, for fast lookup:
261 */
262#define CLASSHASH_BITS (MAX_LOCKDEP_KEYS_BITS - 1)
263#define CLASSHASH_SIZE (1UL << CLASSHASH_BITS)
Peter Zijlstra4b32d0a2007-07-19 01:48:59 -0700264#define __classhashfn(key) hash_long((unsigned long)key, CLASSHASH_BITS)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700265#define classhashentry(key) (classhash_table + __classhashfn((key)))
266
267static struct list_head classhash_table[CLASSHASH_SIZE];
268
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700269/*
270 * We put the lock dependency chains into a hash-table as well, to cache
271 * their existence:
272 */
273#define CHAINHASH_BITS (MAX_LOCKDEP_CHAINS_BITS-1)
274#define CHAINHASH_SIZE (1UL << CHAINHASH_BITS)
Peter Zijlstra4b32d0a2007-07-19 01:48:59 -0700275#define __chainhashfn(chain) hash_long(chain, CHAINHASH_BITS)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700276#define chainhashentry(chain) (chainhash_table + __chainhashfn((chain)))
277
278static struct list_head chainhash_table[CHAINHASH_SIZE];
279
280/*
281 * The hash key of the lock dependency chains is a hash itself too:
282 * it's a hash of all locks taken up to that lock, including that lock.
283 * It's a 64-bit hash, because it's important for the keys to be
284 * unique.
285 */
286#define iterate_chain_key(key1, key2) \
Ingo Molnar03cbc352006-09-29 02:01:46 -0700287 (((key1) << MAX_LOCKDEP_KEYS_BITS) ^ \
288 ((key1) >> (64-MAX_LOCKDEP_KEYS_BITS)) ^ \
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700289 (key2))
290
Steven Rostedt1d09daa2008-05-12 21:20:55 +0200291void lockdep_off(void)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700292{
293 current->lockdep_recursion++;
294}
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700295EXPORT_SYMBOL(lockdep_off);
296
Steven Rostedt1d09daa2008-05-12 21:20:55 +0200297void lockdep_on(void)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700298{
299 current->lockdep_recursion--;
300}
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700301EXPORT_SYMBOL(lockdep_on);
302
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700303/*
304 * Debugging switches:
305 */
306
307#define VERBOSE 0
Ingo Molnar33e94e92006-12-13 00:34:41 -0800308#define VERY_VERBOSE 0
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700309
310#if VERBOSE
311# define HARDIRQ_VERBOSE 1
312# define SOFTIRQ_VERBOSE 1
Nick Piggincf40bd12009-01-21 08:12:39 +0100313# define RECLAIM_VERBOSE 1
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700314#else
315# define HARDIRQ_VERBOSE 0
316# define SOFTIRQ_VERBOSE 0
Nick Piggincf40bd12009-01-21 08:12:39 +0100317# define RECLAIM_VERBOSE 0
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700318#endif
319
Nick Piggincf40bd12009-01-21 08:12:39 +0100320#if VERBOSE || HARDIRQ_VERBOSE || SOFTIRQ_VERBOSE || RECLAIM_VERBOSE
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700321/*
322 * Quick filtering for interesting events:
323 */
324static int class_filter(struct lock_class *class)
325{
Andi Kleenf9829cc2006-07-10 04:44:01 -0700326#if 0
327 /* Example */
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700328 if (class->name_version == 1 &&
Andi Kleenf9829cc2006-07-10 04:44:01 -0700329 !strcmp(class->name, "lockname"))
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700330 return 1;
331 if (class->name_version == 1 &&
Andi Kleenf9829cc2006-07-10 04:44:01 -0700332 !strcmp(class->name, "&struct->lockfield"))
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700333 return 1;
Andi Kleenf9829cc2006-07-10 04:44:01 -0700334#endif
Ingo Molnara6640892006-12-13 00:34:39 -0800335 /* Filter everything else. 1 would be to allow everything else */
336 return 0;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700337}
338#endif
339
340static int verbose(struct lock_class *class)
341{
342#if VERBOSE
343 return class_filter(class);
344#endif
345 return 0;
346}
347
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700348/*
349 * Stack-trace: tightly packed array of stack backtrace
Ingo Molnar74c383f2006-12-13 00:34:43 -0800350 * addresses. Protected by the graph_lock.
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700351 */
352unsigned long nr_stack_trace_entries;
353static unsigned long stack_trace[MAX_STACK_TRACE_ENTRIES];
354
355static int save_trace(struct stack_trace *trace)
356{
357 trace->nr_entries = 0;
358 trace->max_entries = MAX_STACK_TRACE_ENTRIES - nr_stack_trace_entries;
359 trace->entries = stack_trace + nr_stack_trace_entries;
360
Andi Kleen5a1b3992006-09-26 10:52:34 +0200361 trace->skip = 3;
Andi Kleen5a1b3992006-09-26 10:52:34 +0200362
Christoph Hellwigab1b6f02007-05-08 00:23:29 -0700363 save_stack_trace(trace);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700364
365 trace->max_entries = trace->nr_entries;
366
367 nr_stack_trace_entries += trace->nr_entries;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700368
369 if (nr_stack_trace_entries == MAX_STACK_TRACE_ENTRIES) {
Ingo Molnar74c383f2006-12-13 00:34:43 -0800370 if (!debug_locks_off_graph_unlock())
371 return 0;
372
373 printk("BUG: MAX_STACK_TRACE_ENTRIES too low!\n");
374 printk("turning off the locking correctness validator.\n");
375 dump_stack();
376
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700377 return 0;
378 }
379
380 return 1;
381}
382
383unsigned int nr_hardirq_chains;
384unsigned int nr_softirq_chains;
385unsigned int nr_process_chains;
386unsigned int max_lockdep_depth;
387unsigned int max_recursion_depth;
388
David Miller419ca3f2008-07-29 21:45:03 -0700389static unsigned int lockdep_dependency_gen_id;
390
391static bool lockdep_dependency_visit(struct lock_class *source,
392 unsigned int depth)
393{
394 if (!depth)
395 lockdep_dependency_gen_id++;
396 if (source->dep_gen_id == lockdep_dependency_gen_id)
397 return true;
398 source->dep_gen_id = lockdep_dependency_gen_id;
399 return false;
400}
401
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700402#ifdef CONFIG_DEBUG_LOCKDEP
403/*
404 * We cannot printk in early bootup code. Not even early_printk()
405 * might work. So we mark any initialization errors and printk
406 * about it later on, in lockdep_info().
407 */
408static int lockdep_init_error;
Johannes Bergc71063c2007-07-19 01:49:02 -0700409static unsigned long lockdep_init_trace_data[20];
410static struct stack_trace lockdep_init_trace = {
411 .max_entries = ARRAY_SIZE(lockdep_init_trace_data),
412 .entries = lockdep_init_trace_data,
413};
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700414
415/*
416 * Various lockdep statistics:
417 */
418atomic_t chain_lookup_hits;
419atomic_t chain_lookup_misses;
420atomic_t hardirqs_on_events;
421atomic_t hardirqs_off_events;
422atomic_t redundant_hardirqs_on;
423atomic_t redundant_hardirqs_off;
424atomic_t softirqs_on_events;
425atomic_t softirqs_off_events;
426atomic_t redundant_softirqs_on;
427atomic_t redundant_softirqs_off;
428atomic_t nr_unused_locks;
429atomic_t nr_cyclic_checks;
430atomic_t nr_cyclic_check_recursions;
431atomic_t nr_find_usage_forwards_checks;
432atomic_t nr_find_usage_forwards_recursions;
433atomic_t nr_find_usage_backwards_checks;
434atomic_t nr_find_usage_backwards_recursions;
435# define debug_atomic_inc(ptr) atomic_inc(ptr)
436# define debug_atomic_dec(ptr) atomic_dec(ptr)
437# define debug_atomic_read(ptr) atomic_read(ptr)
438#else
439# define debug_atomic_inc(ptr) do { } while (0)
440# define debug_atomic_dec(ptr) do { } while (0)
441# define debug_atomic_read(ptr) 0
442#endif
443
444/*
445 * Locking printouts:
446 */
447
Peter Zijlstrafabe9c42009-01-22 14:51:01 +0100448#define __STR(foo) #foo
449#define STR(foo) __STR(foo)
450
451#define __USAGE(__STATE) \
452 [LOCK_USED_IN_##__STATE] = "IN-"STR(__STATE)"-W", \
453 [LOCK_ENABLED_##__STATE] = STR(__STATE)"-ON-W", \
454 [LOCK_USED_IN_##__STATE##_READ] = "IN-"STR(__STATE)"-R", \
455 [LOCK_ENABLED_##__STATE##_READ] = STR(__STATE)"-ON-R",
456
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700457static const char *usage_str[] =
458{
Peter Zijlstrafabe9c42009-01-22 14:51:01 +0100459#define LOCKDEP_STATE(__STATE) __USAGE(__STATE)
460#include "lockdep_states.h"
461#undef LOCKDEP_STATE
462 [LOCK_USED] = "INITIAL USE",
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700463};
464
465const char * __get_key_name(struct lockdep_subclass_key *key, char *str)
466{
Alexey Dobriyanffb45122007-05-08 00:28:41 -0700467 return kallsyms_lookup((unsigned long)key, NULL, NULL, NULL, str);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700468}
469
470void
Nick Piggincf40bd12009-01-21 08:12:39 +0100471get_usage_chars(struct lock_class *class, char *c1, char *c2, char *c3,
472 char *c4, char *c5, char *c6)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700473{
Nick Piggincf40bd12009-01-21 08:12:39 +0100474 *c1 = '.', *c2 = '.', *c3 = '.', *c4 = '.', *c5 = '.', *c6 = '.';
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700475
476 if (class->usage_mask & LOCKF_USED_IN_HARDIRQ)
477 *c1 = '+';
478 else
Peter Zijlstra4fc95e82009-01-22 13:10:52 +0100479 if (class->usage_mask & LOCKF_ENABLED_HARDIRQ)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700480 *c1 = '-';
481
482 if (class->usage_mask & LOCKF_USED_IN_SOFTIRQ)
483 *c2 = '+';
484 else
Peter Zijlstra4fc95e82009-01-22 13:10:52 +0100485 if (class->usage_mask & LOCKF_ENABLED_SOFTIRQ)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700486 *c2 = '-';
487
Peter Zijlstra4fc95e82009-01-22 13:10:52 +0100488 if (class->usage_mask & LOCKF_ENABLED_HARDIRQ_READ)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700489 *c3 = '-';
490 if (class->usage_mask & LOCKF_USED_IN_HARDIRQ_READ) {
491 *c3 = '+';
Peter Zijlstra4fc95e82009-01-22 13:10:52 +0100492 if (class->usage_mask & LOCKF_ENABLED_HARDIRQ_READ)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700493 *c3 = '?';
494 }
495
Peter Zijlstra4fc95e82009-01-22 13:10:52 +0100496 if (class->usage_mask & LOCKF_ENABLED_SOFTIRQ_READ)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700497 *c4 = '-';
498 if (class->usage_mask & LOCKF_USED_IN_SOFTIRQ_READ) {
499 *c4 = '+';
Peter Zijlstra4fc95e82009-01-22 13:10:52 +0100500 if (class->usage_mask & LOCKF_ENABLED_SOFTIRQ_READ)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700501 *c4 = '?';
502 }
Nick Piggincf40bd12009-01-21 08:12:39 +0100503
504 if (class->usage_mask & LOCKF_USED_IN_RECLAIM_FS)
505 *c5 = '+';
506 else
Peter Zijlstraa652d702009-01-22 13:13:11 +0100507 if (class->usage_mask & LOCKF_ENABLED_RECLAIM_FS)
Nick Piggincf40bd12009-01-21 08:12:39 +0100508 *c5 = '-';
509
Peter Zijlstraa652d702009-01-22 13:13:11 +0100510 if (class->usage_mask & LOCKF_ENABLED_RECLAIM_FS_READ)
Nick Piggincf40bd12009-01-21 08:12:39 +0100511 *c6 = '-';
512 if (class->usage_mask & LOCKF_USED_IN_RECLAIM_FS_READ) {
513 *c6 = '+';
Peter Zijlstraa652d702009-01-22 13:13:11 +0100514 if (class->usage_mask & LOCKF_ENABLED_RECLAIM_FS_READ)
Nick Piggincf40bd12009-01-21 08:12:39 +0100515 *c6 = '?';
516 }
517
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700518}
519
520static void print_lock_name(struct lock_class *class)
521{
Nick Piggincf40bd12009-01-21 08:12:39 +0100522 char str[KSYM_NAME_LEN], c1, c2, c3, c4, c5, c6;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700523 const char *name;
524
Nick Piggincf40bd12009-01-21 08:12:39 +0100525 get_usage_chars(class, &c1, &c2, &c3, &c4, &c5, &c6);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700526
527 name = class->name;
528 if (!name) {
529 name = __get_key_name(class->key, str);
530 printk(" (%s", name);
531 } else {
532 printk(" (%s", name);
533 if (class->name_version > 1)
534 printk("#%d", class->name_version);
535 if (class->subclass)
536 printk("/%d", class->subclass);
537 }
Nick Piggincf40bd12009-01-21 08:12:39 +0100538 printk("){%c%c%c%c%c%c}", c1, c2, c3, c4, c5, c6);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700539}
540
541static void print_lockdep_cache(struct lockdep_map *lock)
542{
543 const char *name;
Tejun Heo9281ace2007-07-17 04:03:51 -0700544 char str[KSYM_NAME_LEN];
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700545
546 name = lock->name;
547 if (!name)
548 name = __get_key_name(lock->key->subkeys, str);
549
550 printk("%s", name);
551}
552
553static void print_lock(struct held_lock *hlock)
554{
Dave Jonesf82b2172008-08-11 09:30:23 +0200555 print_lock_name(hlock_class(hlock));
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700556 printk(", at: ");
557 print_ip_sym(hlock->acquire_ip);
558}
559
560static void lockdep_print_held_locks(struct task_struct *curr)
561{
562 int i, depth = curr->lockdep_depth;
563
564 if (!depth) {
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -0700565 printk("no locks held by %s/%d.\n", curr->comm, task_pid_nr(curr));
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700566 return;
567 }
568 printk("%d lock%s held by %s/%d:\n",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -0700569 depth, depth > 1 ? "s" : "", curr->comm, task_pid_nr(curr));
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700570
571 for (i = 0; i < depth; i++) {
572 printk(" #%d: ", i);
573 print_lock(curr->held_locks + i);
574 }
575}
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700576
577static void print_lock_class_header(struct lock_class *class, int depth)
578{
579 int bit;
580
Andi Kleenf9829cc2006-07-10 04:44:01 -0700581 printk("%*s->", depth, "");
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700582 print_lock_name(class);
583 printk(" ops: %lu", class->ops);
584 printk(" {\n");
585
586 for (bit = 0; bit < LOCK_USAGE_STATES; bit++) {
587 if (class->usage_mask & (1 << bit)) {
588 int len = depth;
589
Andi Kleenf9829cc2006-07-10 04:44:01 -0700590 len += printk("%*s %s", depth, "", usage_str[bit]);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700591 len += printk(" at:\n");
592 print_stack_trace(class->usage_traces + bit, len);
593 }
594 }
Andi Kleenf9829cc2006-07-10 04:44:01 -0700595 printk("%*s }\n", depth, "");
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700596
Andi Kleenf9829cc2006-07-10 04:44:01 -0700597 printk("%*s ... key at: ",depth,"");
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700598 print_ip_sym((unsigned long)class->key);
599}
600
601/*
602 * printk all lock dependencies starting at <entry>:
603 */
Ingo Molnar7807faf2008-11-25 08:44:24 +0100604static void __used
605print_lock_dependencies(struct lock_class *class, int depth)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700606{
607 struct lock_list *entry;
608
David Miller419ca3f2008-07-29 21:45:03 -0700609 if (lockdep_dependency_visit(class, depth))
610 return;
611
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700612 if (DEBUG_LOCKS_WARN_ON(depth >= 20))
613 return;
614
615 print_lock_class_header(class, depth);
616
617 list_for_each_entry(entry, &class->locks_after, entry) {
Jarek Poplawskib23984d2006-12-06 20:36:23 -0800618 if (DEBUG_LOCKS_WARN_ON(!entry->class))
619 return;
620
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700621 print_lock_dependencies(entry->class, depth + 1);
622
Andi Kleenf9829cc2006-07-10 04:44:01 -0700623 printk("%*s ... acquired at:\n",depth,"");
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700624 print_stack_trace(&entry->trace, 2);
625 printk("\n");
626 }
627}
628
Dave Jones99de0552006-09-29 02:00:10 -0700629static void print_kernel_version(void)
630{
Serge E. Hallyn96b644b2006-10-02 02:18:13 -0700631 printk("%s %.*s\n", init_utsname()->release,
632 (int)strcspn(init_utsname()->version, " "),
633 init_utsname()->version);
Dave Jones99de0552006-09-29 02:00:10 -0700634}
635
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700636static int very_verbose(struct lock_class *class)
637{
638#if VERY_VERBOSE
639 return class_filter(class);
640#endif
641 return 0;
642}
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700643
644/*
645 * Is this the address of a static object:
646 */
647static int static_obj(void *obj)
648{
649 unsigned long start = (unsigned long) &_stext,
650 end = (unsigned long) &_end,
651 addr = (unsigned long) obj;
652#ifdef CONFIG_SMP
653 int i;
654#endif
655
656 /*
657 * static variable?
658 */
659 if ((addr >= start) && (addr < end))
660 return 1;
661
662#ifdef CONFIG_SMP
663 /*
664 * percpu var?
665 */
666 for_each_possible_cpu(i) {
667 start = (unsigned long) &__per_cpu_start + per_cpu_offset(i);
Ingo Molnar1ff56832006-11-17 19:57:22 +0100668 end = (unsigned long) &__per_cpu_start + PERCPU_ENOUGH_ROOM
669 + per_cpu_offset(i);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700670
671 if ((addr >= start) && (addr < end))
672 return 1;
673 }
674#endif
675
676 /*
677 * module var?
678 */
679 return is_module_address(addr);
680}
681
682/*
683 * To make lock name printouts unique, we calculate a unique
684 * class->name_version generation counter:
685 */
686static int count_matching_names(struct lock_class *new_class)
687{
688 struct lock_class *class;
689 int count = 0;
690
691 if (!new_class->name)
692 return 0;
693
694 list_for_each_entry(class, &all_lock_classes, lock_entry) {
695 if (new_class->key - new_class->subclass == class->key)
696 return class->name_version;
697 if (class->name && !strcmp(class->name, new_class->name))
698 count = max(count, class->name_version);
699 }
700
701 return count + 1;
702}
703
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700704/*
705 * Register a lock's class in the hash-table, if the class is not present
706 * yet. Otherwise we look it up. We cache the result in the lock object
707 * itself, so actual lookup of the hash should be once per lock object.
708 */
709static inline struct lock_class *
Ingo Molnard6d897c2006-07-10 04:44:04 -0700710look_up_lock_class(struct lockdep_map *lock, unsigned int subclass)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700711{
712 struct lockdep_subclass_key *key;
713 struct list_head *hash_head;
714 struct lock_class *class;
715
716#ifdef CONFIG_DEBUG_LOCKDEP
717 /*
718 * If the architecture calls into lockdep before initializing
719 * the hashes then we'll warn about it later. (we cannot printk
720 * right now)
721 */
722 if (unlikely(!lockdep_initialized)) {
723 lockdep_init();
724 lockdep_init_error = 1;
Johannes Bergc71063c2007-07-19 01:49:02 -0700725 save_stack_trace(&lockdep_init_trace);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700726 }
727#endif
728
729 /*
730 * Static locks do not have their class-keys yet - for them the key
731 * is the lock object itself:
732 */
733 if (unlikely(!lock->key))
734 lock->key = (void *)lock;
735
736 /*
737 * NOTE: the class-key must be unique. For dynamic locks, a static
738 * lock_class_key variable is passed in through the mutex_init()
739 * (or spin_lock_init()) call - which acts as the key. For static
740 * locks we use the lock object itself as the key.
741 */
Peter Zijlstra4b32d0a2007-07-19 01:48:59 -0700742 BUILD_BUG_ON(sizeof(struct lock_class_key) >
743 sizeof(struct lockdep_map));
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700744
745 key = lock->key->subkeys + subclass;
746
747 hash_head = classhashentry(key);
748
749 /*
750 * We can walk the hash lockfree, because the hash only
751 * grows, and we are careful when adding entries to the end:
752 */
Peter Zijlstra4b32d0a2007-07-19 01:48:59 -0700753 list_for_each_entry(class, hash_head, hash_entry) {
754 if (class->key == key) {
755 WARN_ON_ONCE(class->name != lock->name);
Ingo Molnard6d897c2006-07-10 04:44:04 -0700756 return class;
Peter Zijlstra4b32d0a2007-07-19 01:48:59 -0700757 }
758 }
Ingo Molnard6d897c2006-07-10 04:44:04 -0700759
760 return NULL;
761}
762
763/*
764 * Register a lock's class in the hash-table, if the class is not present
765 * yet. Otherwise we look it up. We cache the result in the lock object
766 * itself, so actual lookup of the hash should be once per lock object.
767 */
768static inline struct lock_class *
Peter Zijlstra4dfbb9d2006-10-11 01:45:14 -0400769register_lock_class(struct lockdep_map *lock, unsigned int subclass, int force)
Ingo Molnard6d897c2006-07-10 04:44:04 -0700770{
771 struct lockdep_subclass_key *key;
772 struct list_head *hash_head;
773 struct lock_class *class;
Ingo Molnar70e45062006-12-06 20:40:50 -0800774 unsigned long flags;
Ingo Molnard6d897c2006-07-10 04:44:04 -0700775
776 class = look_up_lock_class(lock, subclass);
777 if (likely(class))
778 return class;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700779
780 /*
781 * Debug-check: all keys must be persistent!
782 */
783 if (!static_obj(lock->key)) {
784 debug_locks_off();
785 printk("INFO: trying to register non-static key.\n");
786 printk("the code is fine but needs lockdep annotation.\n");
787 printk("turning off the locking correctness validator.\n");
788 dump_stack();
789
790 return NULL;
791 }
792
Ingo Molnard6d897c2006-07-10 04:44:04 -0700793 key = lock->key->subkeys + subclass;
794 hash_head = classhashentry(key);
795
Ingo Molnar70e45062006-12-06 20:40:50 -0800796 raw_local_irq_save(flags);
Ingo Molnar74c383f2006-12-13 00:34:43 -0800797 if (!graph_lock()) {
798 raw_local_irq_restore(flags);
799 return NULL;
800 }
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700801 /*
802 * We have to do the hash-walk again, to avoid races
803 * with another CPU:
804 */
805 list_for_each_entry(class, hash_head, hash_entry)
806 if (class->key == key)
807 goto out_unlock_set;
808 /*
809 * Allocate a new key from the static array, and add it to
810 * the hash:
811 */
812 if (nr_lock_classes >= MAX_LOCKDEP_KEYS) {
Ingo Molnar74c383f2006-12-13 00:34:43 -0800813 if (!debug_locks_off_graph_unlock()) {
814 raw_local_irq_restore(flags);
815 return NULL;
816 }
Ingo Molnar70e45062006-12-06 20:40:50 -0800817 raw_local_irq_restore(flags);
Ingo Molnar74c383f2006-12-13 00:34:43 -0800818
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700819 printk("BUG: MAX_LOCKDEP_KEYS too low!\n");
820 printk("turning off the locking correctness validator.\n");
821 return NULL;
822 }
823 class = lock_classes + nr_lock_classes++;
824 debug_atomic_inc(&nr_unused_locks);
825 class->key = key;
826 class->name = lock->name;
827 class->subclass = subclass;
828 INIT_LIST_HEAD(&class->lock_entry);
829 INIT_LIST_HEAD(&class->locks_before);
830 INIT_LIST_HEAD(&class->locks_after);
831 class->name_version = count_matching_names(class);
832 /*
833 * We use RCU's safe list-add method to make
834 * parallel walking of the hash-list safe:
835 */
836 list_add_tail_rcu(&class->hash_entry, hash_head);
Dale Farnsworth14811972008-02-25 23:03:02 +0100837 /*
838 * Add it to the global list of classes:
839 */
840 list_add_tail_rcu(&class->lock_entry, &all_lock_classes);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700841
842 if (verbose(class)) {
Ingo Molnar74c383f2006-12-13 00:34:43 -0800843 graph_unlock();
Ingo Molnar70e45062006-12-06 20:40:50 -0800844 raw_local_irq_restore(flags);
Ingo Molnar74c383f2006-12-13 00:34:43 -0800845
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700846 printk("\nnew class %p: %s", class->key, class->name);
847 if (class->name_version > 1)
848 printk("#%d", class->name_version);
849 printk("\n");
850 dump_stack();
Ingo Molnar74c383f2006-12-13 00:34:43 -0800851
Ingo Molnar70e45062006-12-06 20:40:50 -0800852 raw_local_irq_save(flags);
Ingo Molnar74c383f2006-12-13 00:34:43 -0800853 if (!graph_lock()) {
854 raw_local_irq_restore(flags);
855 return NULL;
856 }
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700857 }
858out_unlock_set:
Ingo Molnar74c383f2006-12-13 00:34:43 -0800859 graph_unlock();
Ingo Molnar70e45062006-12-06 20:40:50 -0800860 raw_local_irq_restore(flags);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700861
Peter Zijlstra4dfbb9d2006-10-11 01:45:14 -0400862 if (!subclass || force)
Ingo Molnard6d897c2006-07-10 04:44:04 -0700863 lock->class_cache = class;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700864
Jarek Poplawski381a2292007-02-10 01:44:58 -0800865 if (DEBUG_LOCKS_WARN_ON(class->subclass != subclass))
866 return NULL;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700867
868 return class;
869}
870
Peter Zijlstraca58abc2007-07-19 01:48:53 -0700871#ifdef CONFIG_PROVE_LOCKING
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700872/*
Peter Zijlstra8e182572007-07-19 01:48:54 -0700873 * Allocate a lockdep entry. (assumes the graph_lock held, returns
874 * with NULL on failure)
875 */
876static struct lock_list *alloc_list_entry(void)
877{
878 if (nr_list_entries >= MAX_LOCKDEP_ENTRIES) {
879 if (!debug_locks_off_graph_unlock())
880 return NULL;
881
882 printk("BUG: MAX_LOCKDEP_ENTRIES too low!\n");
883 printk("turning off the locking correctness validator.\n");
884 return NULL;
885 }
886 return list_entries + nr_list_entries++;
887}
888
889/*
890 * Add a new dependency to the head of the list:
891 */
892static int add_lock_to_list(struct lock_class *class, struct lock_class *this,
893 struct list_head *head, unsigned long ip, int distance)
894{
895 struct lock_list *entry;
896 /*
897 * Lock not present yet - get a new dependency struct and
898 * add it to the list:
899 */
900 entry = alloc_list_entry();
901 if (!entry)
902 return 0;
903
Peter Zijlstra8e182572007-07-19 01:48:54 -0700904 if (!save_trace(&entry->trace))
905 return 0;
906
Zhu Yi74870172008-08-27 14:33:00 +0800907 entry->class = this;
908 entry->distance = distance;
Peter Zijlstra8e182572007-07-19 01:48:54 -0700909 /*
910 * Since we never remove from the dependency list, the list can
911 * be walked lockless by other CPUs, it's only allocation
912 * that must be protected by the spinlock. But this also means
913 * we must make new entries visible only once writes to the
914 * entry become visible - hence the RCU op:
915 */
916 list_add_tail_rcu(&entry->entry, head);
917
918 return 1;
919}
920
921/*
922 * Recursive, forwards-direction lock-dependency checking, used for
923 * both noncyclic checking and for hardirq-unsafe/softirq-unsafe
924 * checking.
925 *
926 * (to keep the stackframe of the recursive functions small we
927 * use these global variables, and we also mark various helper
928 * functions as noinline.)
929 */
930static struct held_lock *check_source, *check_target;
931
932/*
933 * Print a dependency chain entry (this is only done when a deadlock
934 * has been detected):
935 */
936static noinline int
937print_circular_bug_entry(struct lock_list *target, unsigned int depth)
938{
939 if (debug_locks_silent)
940 return 0;
941 printk("\n-> #%u", depth);
942 print_lock_name(target->class);
943 printk(":\n");
944 print_stack_trace(&target->trace, 6);
945
946 return 0;
947}
948
949/*
950 * When a circular dependency is detected, print the
951 * header first:
952 */
953static noinline int
954print_circular_bug_header(struct lock_list *entry, unsigned int depth)
955{
956 struct task_struct *curr = current;
957
958 if (!debug_locks_off_graph_unlock() || debug_locks_silent)
959 return 0;
960
961 printk("\n=======================================================\n");
962 printk( "[ INFO: possible circular locking dependency detected ]\n");
963 print_kernel_version();
964 printk( "-------------------------------------------------------\n");
965 printk("%s/%d is trying to acquire lock:\n",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -0700966 curr->comm, task_pid_nr(curr));
Peter Zijlstra8e182572007-07-19 01:48:54 -0700967 print_lock(check_source);
968 printk("\nbut task is already holding lock:\n");
969 print_lock(check_target);
970 printk("\nwhich lock already depends on the new lock.\n\n");
971 printk("\nthe existing dependency chain (in reverse order) is:\n");
972
973 print_circular_bug_entry(entry, depth);
974
975 return 0;
976}
977
978static noinline int print_circular_bug_tail(void)
979{
980 struct task_struct *curr = current;
981 struct lock_list this;
982
983 if (debug_locks_silent)
984 return 0;
985
Dave Jonesf82b2172008-08-11 09:30:23 +0200986 this.class = hlock_class(check_source);
Peter Zijlstra8e182572007-07-19 01:48:54 -0700987 if (!save_trace(&this.trace))
988 return 0;
989
990 print_circular_bug_entry(&this, 0);
991
992 printk("\nother info that might help us debug this:\n\n");
993 lockdep_print_held_locks(curr);
994
995 printk("\nstack backtrace:\n");
996 dump_stack();
997
998 return 0;
999}
1000
1001#define RECURSION_LIMIT 40
1002
1003static int noinline print_infinite_recursion_bug(void)
1004{
1005 if (!debug_locks_off_graph_unlock())
1006 return 0;
1007
1008 WARN_ON(1);
1009
1010 return 0;
1011}
1012
David Miller419ca3f2008-07-29 21:45:03 -07001013unsigned long __lockdep_count_forward_deps(struct lock_class *class,
1014 unsigned int depth)
1015{
1016 struct lock_list *entry;
1017 unsigned long ret = 1;
1018
1019 if (lockdep_dependency_visit(class, depth))
1020 return 0;
1021
1022 /*
1023 * Recurse this class's dependency list:
1024 */
1025 list_for_each_entry(entry, &class->locks_after, entry)
1026 ret += __lockdep_count_forward_deps(entry->class, depth + 1);
1027
1028 return ret;
1029}
1030
1031unsigned long lockdep_count_forward_deps(struct lock_class *class)
1032{
1033 unsigned long ret, flags;
1034
1035 local_irq_save(flags);
1036 __raw_spin_lock(&lockdep_lock);
1037 ret = __lockdep_count_forward_deps(class, 0);
1038 __raw_spin_unlock(&lockdep_lock);
1039 local_irq_restore(flags);
1040
1041 return ret;
1042}
1043
1044unsigned long __lockdep_count_backward_deps(struct lock_class *class,
1045 unsigned int depth)
1046{
1047 struct lock_list *entry;
1048 unsigned long ret = 1;
1049
1050 if (lockdep_dependency_visit(class, depth))
1051 return 0;
1052 /*
1053 * Recurse this class's dependency list:
1054 */
1055 list_for_each_entry(entry, &class->locks_before, entry)
1056 ret += __lockdep_count_backward_deps(entry->class, depth + 1);
1057
1058 return ret;
1059}
1060
1061unsigned long lockdep_count_backward_deps(struct lock_class *class)
1062{
1063 unsigned long ret, flags;
1064
1065 local_irq_save(flags);
1066 __raw_spin_lock(&lockdep_lock);
1067 ret = __lockdep_count_backward_deps(class, 0);
1068 __raw_spin_unlock(&lockdep_lock);
1069 local_irq_restore(flags);
1070
1071 return ret;
1072}
1073
Peter Zijlstra8e182572007-07-19 01:48:54 -07001074/*
1075 * Prove that the dependency graph starting at <entry> can not
1076 * lead to <target>. Print an error and return 0 if it does.
1077 */
1078static noinline int
1079check_noncircular(struct lock_class *source, unsigned int depth)
1080{
1081 struct lock_list *entry;
1082
David Miller419ca3f2008-07-29 21:45:03 -07001083 if (lockdep_dependency_visit(source, depth))
1084 return 1;
1085
Peter Zijlstra8e182572007-07-19 01:48:54 -07001086 debug_atomic_inc(&nr_cyclic_check_recursions);
1087 if (depth > max_recursion_depth)
1088 max_recursion_depth = depth;
1089 if (depth >= RECURSION_LIMIT)
1090 return print_infinite_recursion_bug();
1091 /*
1092 * Check this lock's dependency list:
1093 */
1094 list_for_each_entry(entry, &source->locks_after, entry) {
Dave Jonesf82b2172008-08-11 09:30:23 +02001095 if (entry->class == hlock_class(check_target))
Peter Zijlstra8e182572007-07-19 01:48:54 -07001096 return print_circular_bug_header(entry, depth+1);
1097 debug_atomic_inc(&nr_cyclic_checks);
1098 if (!check_noncircular(entry->class, depth+1))
1099 return print_circular_bug_entry(entry, depth+1);
1100 }
1101 return 1;
1102}
1103
Steven Rostedt81d68a92008-05-12 21:20:42 +02001104#if defined(CONFIG_TRACE_IRQFLAGS) && defined(CONFIG_PROVE_LOCKING)
Peter Zijlstra8e182572007-07-19 01:48:54 -07001105/*
1106 * Forwards and backwards subgraph searching, for the purposes of
1107 * proving that two subgraphs can be connected by a new dependency
1108 * without creating any illegal irq-safe -> irq-unsafe lock dependency.
1109 */
1110static enum lock_usage_bit find_usage_bit;
1111static struct lock_class *forwards_match, *backwards_match;
1112
1113/*
1114 * Find a node in the forwards-direction dependency sub-graph starting
1115 * at <source> that matches <find_usage_bit>.
1116 *
1117 * Return 2 if such a node exists in the subgraph, and put that node
1118 * into <forwards_match>.
1119 *
1120 * Return 1 otherwise and keep <forwards_match> unchanged.
1121 * Return 0 on error.
1122 */
1123static noinline int
1124find_usage_forwards(struct lock_class *source, unsigned int depth)
1125{
1126 struct lock_list *entry;
1127 int ret;
1128
David Miller419ca3f2008-07-29 21:45:03 -07001129 if (lockdep_dependency_visit(source, depth))
1130 return 1;
1131
Peter Zijlstra8e182572007-07-19 01:48:54 -07001132 if (depth > max_recursion_depth)
1133 max_recursion_depth = depth;
1134 if (depth >= RECURSION_LIMIT)
1135 return print_infinite_recursion_bug();
1136
1137 debug_atomic_inc(&nr_find_usage_forwards_checks);
1138 if (source->usage_mask & (1 << find_usage_bit)) {
1139 forwards_match = source;
1140 return 2;
1141 }
1142
1143 /*
1144 * Check this lock's dependency list:
1145 */
1146 list_for_each_entry(entry, &source->locks_after, entry) {
1147 debug_atomic_inc(&nr_find_usage_forwards_recursions);
1148 ret = find_usage_forwards(entry->class, depth+1);
1149 if (ret == 2 || ret == 0)
1150 return ret;
1151 }
1152 return 1;
1153}
1154
1155/*
1156 * Find a node in the backwards-direction dependency sub-graph starting
1157 * at <source> that matches <find_usage_bit>.
1158 *
1159 * Return 2 if such a node exists in the subgraph, and put that node
1160 * into <backwards_match>.
1161 *
1162 * Return 1 otherwise and keep <backwards_match> unchanged.
1163 * Return 0 on error.
1164 */
Steven Rostedt1d09daa2008-05-12 21:20:55 +02001165static noinline int
Peter Zijlstra8e182572007-07-19 01:48:54 -07001166find_usage_backwards(struct lock_class *source, unsigned int depth)
1167{
1168 struct lock_list *entry;
1169 int ret;
1170
David Miller419ca3f2008-07-29 21:45:03 -07001171 if (lockdep_dependency_visit(source, depth))
1172 return 1;
1173
Peter Zijlstra8e182572007-07-19 01:48:54 -07001174 if (!__raw_spin_is_locked(&lockdep_lock))
1175 return DEBUG_LOCKS_WARN_ON(1);
1176
1177 if (depth > max_recursion_depth)
1178 max_recursion_depth = depth;
1179 if (depth >= RECURSION_LIMIT)
1180 return print_infinite_recursion_bug();
1181
1182 debug_atomic_inc(&nr_find_usage_backwards_checks);
1183 if (source->usage_mask & (1 << find_usage_bit)) {
1184 backwards_match = source;
1185 return 2;
1186 }
1187
Dave Jonesf82b2172008-08-11 09:30:23 +02001188 if (!source && debug_locks_off_graph_unlock()) {
1189 WARN_ON(1);
1190 return 0;
1191 }
1192
Peter Zijlstra8e182572007-07-19 01:48:54 -07001193 /*
1194 * Check this lock's dependency list:
1195 */
1196 list_for_each_entry(entry, &source->locks_before, entry) {
1197 debug_atomic_inc(&nr_find_usage_backwards_recursions);
1198 ret = find_usage_backwards(entry->class, depth+1);
1199 if (ret == 2 || ret == 0)
1200 return ret;
1201 }
1202 return 1;
1203}
1204
1205static int
1206print_bad_irq_dependency(struct task_struct *curr,
1207 struct held_lock *prev,
1208 struct held_lock *next,
1209 enum lock_usage_bit bit1,
1210 enum lock_usage_bit bit2,
1211 const char *irqclass)
1212{
1213 if (!debug_locks_off_graph_unlock() || debug_locks_silent)
1214 return 0;
1215
1216 printk("\n======================================================\n");
1217 printk( "[ INFO: %s-safe -> %s-unsafe lock order detected ]\n",
1218 irqclass, irqclass);
1219 print_kernel_version();
1220 printk( "------------------------------------------------------\n");
1221 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 -07001222 curr->comm, task_pid_nr(curr),
Peter Zijlstra8e182572007-07-19 01:48:54 -07001223 curr->hardirq_context, hardirq_count() >> HARDIRQ_SHIFT,
1224 curr->softirq_context, softirq_count() >> SOFTIRQ_SHIFT,
1225 curr->hardirqs_enabled,
1226 curr->softirqs_enabled);
1227 print_lock(next);
1228
1229 printk("\nand this task is already holding:\n");
1230 print_lock(prev);
1231 printk("which would create a new lock dependency:\n");
Dave Jonesf82b2172008-08-11 09:30:23 +02001232 print_lock_name(hlock_class(prev));
Peter Zijlstra8e182572007-07-19 01:48:54 -07001233 printk(" ->");
Dave Jonesf82b2172008-08-11 09:30:23 +02001234 print_lock_name(hlock_class(next));
Peter Zijlstra8e182572007-07-19 01:48:54 -07001235 printk("\n");
1236
1237 printk("\nbut this new dependency connects a %s-irq-safe lock:\n",
1238 irqclass);
1239 print_lock_name(backwards_match);
1240 printk("\n... which became %s-irq-safe at:\n", irqclass);
1241
1242 print_stack_trace(backwards_match->usage_traces + bit1, 1);
1243
1244 printk("\nto a %s-irq-unsafe lock:\n", irqclass);
1245 print_lock_name(forwards_match);
1246 printk("\n... which became %s-irq-unsafe at:\n", irqclass);
1247 printk("...");
1248
1249 print_stack_trace(forwards_match->usage_traces + bit2, 1);
1250
1251 printk("\nother info that might help us debug this:\n\n");
1252 lockdep_print_held_locks(curr);
1253
1254 printk("\nthe %s-irq-safe lock's dependencies:\n", irqclass);
1255 print_lock_dependencies(backwards_match, 0);
1256
1257 printk("\nthe %s-irq-unsafe lock's dependencies:\n", irqclass);
1258 print_lock_dependencies(forwards_match, 0);
1259
1260 printk("\nstack backtrace:\n");
1261 dump_stack();
1262
1263 return 0;
1264}
1265
1266static int
1267check_usage(struct task_struct *curr, struct held_lock *prev,
1268 struct held_lock *next, enum lock_usage_bit bit_backwards,
1269 enum lock_usage_bit bit_forwards, const char *irqclass)
1270{
1271 int ret;
1272
1273 find_usage_bit = bit_backwards;
1274 /* fills in <backwards_match> */
Dave Jonesf82b2172008-08-11 09:30:23 +02001275 ret = find_usage_backwards(hlock_class(prev), 0);
Peter Zijlstra8e182572007-07-19 01:48:54 -07001276 if (!ret || ret == 1)
1277 return ret;
1278
1279 find_usage_bit = bit_forwards;
Dave Jonesf82b2172008-08-11 09:30:23 +02001280 ret = find_usage_forwards(hlock_class(next), 0);
Peter Zijlstra8e182572007-07-19 01:48:54 -07001281 if (!ret || ret == 1)
1282 return ret;
1283 /* ret == 2 */
1284 return print_bad_irq_dependency(curr, prev, next,
1285 bit_backwards, bit_forwards, irqclass);
1286}
1287
1288static int
1289check_prev_add_irq(struct task_struct *curr, struct held_lock *prev,
1290 struct held_lock *next)
1291{
1292 /*
1293 * Prove that the new dependency does not connect a hardirq-safe
1294 * lock with a hardirq-unsafe lock - to achieve this we search
1295 * the backwards-subgraph starting at <prev>, and the
1296 * forwards-subgraph starting at <next>:
1297 */
1298 if (!check_usage(curr, prev, next, LOCK_USED_IN_HARDIRQ,
Peter Zijlstra4fc95e82009-01-22 13:10:52 +01001299 LOCK_ENABLED_HARDIRQ, "hard"))
Peter Zijlstra8e182572007-07-19 01:48:54 -07001300 return 0;
1301
1302 /*
1303 * Prove that the new dependency does not connect a hardirq-safe-read
1304 * lock with a hardirq-unsafe lock - to achieve this we search
1305 * the backwards-subgraph starting at <prev>, and the
1306 * forwards-subgraph starting at <next>:
1307 */
1308 if (!check_usage(curr, prev, next, LOCK_USED_IN_HARDIRQ_READ,
Peter Zijlstra4fc95e82009-01-22 13:10:52 +01001309 LOCK_ENABLED_HARDIRQ, "hard-read"))
Peter Zijlstra8e182572007-07-19 01:48:54 -07001310 return 0;
1311
1312 /*
1313 * Prove that the new dependency does not connect a softirq-safe
1314 * lock with a softirq-unsafe lock - to achieve this we search
1315 * the backwards-subgraph starting at <prev>, and the
1316 * forwards-subgraph starting at <next>:
1317 */
1318 if (!check_usage(curr, prev, next, LOCK_USED_IN_SOFTIRQ,
Peter Zijlstra4fc95e82009-01-22 13:10:52 +01001319 LOCK_ENABLED_SOFTIRQ, "soft"))
Peter Zijlstra8e182572007-07-19 01:48:54 -07001320 return 0;
1321 /*
1322 * Prove that the new dependency does not connect a softirq-safe-read
1323 * lock with a softirq-unsafe lock - to achieve this we search
1324 * the backwards-subgraph starting at <prev>, and the
1325 * forwards-subgraph starting at <next>:
1326 */
1327 if (!check_usage(curr, prev, next, LOCK_USED_IN_SOFTIRQ_READ,
Peter Zijlstra4fc95e82009-01-22 13:10:52 +01001328 LOCK_ENABLED_SOFTIRQ, "soft"))
Peter Zijlstra8e182572007-07-19 01:48:54 -07001329 return 0;
1330
Nick Piggincf40bd12009-01-21 08:12:39 +01001331 /*
1332 * Prove that the new dependency does not connect a reclaim-fs-safe
1333 * lock with a reclaim-fs-unsafe lock - to achieve this we search
1334 * the backwards-subgraph starting at <prev>, and the
1335 * forwards-subgraph starting at <next>:
1336 */
1337 if (!check_usage(curr, prev, next, LOCK_USED_IN_RECLAIM_FS,
Peter Zijlstraa652d702009-01-22 13:13:11 +01001338 LOCK_ENABLED_RECLAIM_FS, "reclaim-fs"))
Nick Piggincf40bd12009-01-21 08:12:39 +01001339 return 0;
1340
1341 /*
1342 * Prove that the new dependency does not connect a reclaim-fs-safe-read
1343 * lock with a reclaim-fs-unsafe lock - to achieve this we search
1344 * the backwards-subgraph starting at <prev>, and the
1345 * forwards-subgraph starting at <next>:
1346 */
1347 if (!check_usage(curr, prev, next, LOCK_USED_IN_RECLAIM_FS_READ,
Peter Zijlstraa652d702009-01-22 13:13:11 +01001348 LOCK_ENABLED_RECLAIM_FS, "reclaim-fs-read"))
Nick Piggincf40bd12009-01-21 08:12:39 +01001349 return 0;
1350
Peter Zijlstra8e182572007-07-19 01:48:54 -07001351 return 1;
1352}
1353
1354static void inc_chains(void)
1355{
1356 if (current->hardirq_context)
1357 nr_hardirq_chains++;
1358 else {
1359 if (current->softirq_context)
1360 nr_softirq_chains++;
1361 else
1362 nr_process_chains++;
1363 }
1364}
1365
1366#else
1367
1368static inline int
1369check_prev_add_irq(struct task_struct *curr, struct held_lock *prev,
1370 struct held_lock *next)
1371{
1372 return 1;
1373}
1374
1375static inline void inc_chains(void)
1376{
1377 nr_process_chains++;
1378}
1379
1380#endif
1381
1382static int
1383print_deadlock_bug(struct task_struct *curr, struct held_lock *prev,
1384 struct held_lock *next)
1385{
1386 if (!debug_locks_off_graph_unlock() || debug_locks_silent)
1387 return 0;
1388
1389 printk("\n=============================================\n");
1390 printk( "[ INFO: possible recursive locking detected ]\n");
1391 print_kernel_version();
1392 printk( "---------------------------------------------\n");
1393 printk("%s/%d is trying to acquire lock:\n",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07001394 curr->comm, task_pid_nr(curr));
Peter Zijlstra8e182572007-07-19 01:48:54 -07001395 print_lock(next);
1396 printk("\nbut task is already holding lock:\n");
1397 print_lock(prev);
1398
1399 printk("\nother info that might help us debug this:\n");
1400 lockdep_print_held_locks(curr);
1401
1402 printk("\nstack backtrace:\n");
1403 dump_stack();
1404
1405 return 0;
1406}
1407
1408/*
1409 * Check whether we are holding such a class already.
1410 *
1411 * (Note that this has to be done separately, because the graph cannot
1412 * detect such classes of deadlocks.)
1413 *
1414 * Returns: 0 on deadlock detected, 1 on OK, 2 on recursive read
1415 */
1416static int
1417check_deadlock(struct task_struct *curr, struct held_lock *next,
1418 struct lockdep_map *next_instance, int read)
1419{
1420 struct held_lock *prev;
Peter Zijlstra7531e2f2008-08-11 09:30:24 +02001421 struct held_lock *nest = NULL;
Peter Zijlstra8e182572007-07-19 01:48:54 -07001422 int i;
1423
1424 for (i = 0; i < curr->lockdep_depth; i++) {
1425 prev = curr->held_locks + i;
Peter Zijlstra7531e2f2008-08-11 09:30:24 +02001426
1427 if (prev->instance == next->nest_lock)
1428 nest = prev;
1429
Dave Jonesf82b2172008-08-11 09:30:23 +02001430 if (hlock_class(prev) != hlock_class(next))
Peter Zijlstra8e182572007-07-19 01:48:54 -07001431 continue;
Peter Zijlstra7531e2f2008-08-11 09:30:24 +02001432
Peter Zijlstra8e182572007-07-19 01:48:54 -07001433 /*
1434 * Allow read-after-read recursion of the same
1435 * lock class (i.e. read_lock(lock)+read_lock(lock)):
1436 */
1437 if ((read == 2) && prev->read)
1438 return 2;
Peter Zijlstra7531e2f2008-08-11 09:30:24 +02001439
1440 /*
1441 * We're holding the nest_lock, which serializes this lock's
1442 * nesting behaviour.
1443 */
1444 if (nest)
1445 return 2;
1446
Peter Zijlstra8e182572007-07-19 01:48:54 -07001447 return print_deadlock_bug(curr, prev, next);
1448 }
1449 return 1;
1450}
1451
1452/*
1453 * There was a chain-cache miss, and we are about to add a new dependency
1454 * to a previous lock. We recursively validate the following rules:
1455 *
1456 * - would the adding of the <prev> -> <next> dependency create a
1457 * circular dependency in the graph? [== circular deadlock]
1458 *
1459 * - does the new prev->next dependency connect any hardirq-safe lock
1460 * (in the full backwards-subgraph starting at <prev>) with any
1461 * hardirq-unsafe lock (in the full forwards-subgraph starting at
1462 * <next>)? [== illegal lock inversion with hardirq contexts]
1463 *
1464 * - does the new prev->next dependency connect any softirq-safe lock
1465 * (in the full backwards-subgraph starting at <prev>) with any
1466 * softirq-unsafe lock (in the full forwards-subgraph starting at
1467 * <next>)? [== illegal lock inversion with softirq contexts]
1468 *
1469 * any of these scenarios could lead to a deadlock.
1470 *
1471 * Then if all the validations pass, we add the forwards and backwards
1472 * dependency.
1473 */
1474static int
1475check_prev_add(struct task_struct *curr, struct held_lock *prev,
1476 struct held_lock *next, int distance)
1477{
1478 struct lock_list *entry;
1479 int ret;
1480
1481 /*
1482 * Prove that the new <prev> -> <next> dependency would not
1483 * create a circular dependency in the graph. (We do this by
1484 * forward-recursing into the graph starting at <next>, and
1485 * checking whether we can reach <prev>.)
1486 *
1487 * We are using global variables to control the recursion, to
1488 * keep the stackframe size of the recursive functions low:
1489 */
1490 check_source = next;
1491 check_target = prev;
Dave Jonesf82b2172008-08-11 09:30:23 +02001492 if (!(check_noncircular(hlock_class(next), 0)))
Peter Zijlstra8e182572007-07-19 01:48:54 -07001493 return print_circular_bug_tail();
1494
1495 if (!check_prev_add_irq(curr, prev, next))
1496 return 0;
1497
1498 /*
1499 * For recursive read-locks we do all the dependency checks,
1500 * but we dont store read-triggered dependencies (only
1501 * write-triggered dependencies). This ensures that only the
1502 * write-side dependencies matter, and that if for example a
1503 * write-lock never takes any other locks, then the reads are
1504 * equivalent to a NOP.
1505 */
1506 if (next->read == 2 || prev->read == 2)
1507 return 1;
1508 /*
1509 * Is the <prev> -> <next> dependency already present?
1510 *
1511 * (this may occur even though this is a new chain: consider
1512 * e.g. the L1 -> L2 -> L3 -> L4 and the L5 -> L1 -> L2 -> L3
1513 * chains - the second one will be new, but L1 already has
1514 * L2 added to its dependency list, due to the first chain.)
1515 */
Dave Jonesf82b2172008-08-11 09:30:23 +02001516 list_for_each_entry(entry, &hlock_class(prev)->locks_after, entry) {
1517 if (entry->class == hlock_class(next)) {
Peter Zijlstra8e182572007-07-19 01:48:54 -07001518 if (distance == 1)
1519 entry->distance = 1;
1520 return 2;
1521 }
1522 }
1523
1524 /*
1525 * Ok, all validations passed, add the new lock
1526 * to the previous lock's dependency list:
1527 */
Dave Jonesf82b2172008-08-11 09:30:23 +02001528 ret = add_lock_to_list(hlock_class(prev), hlock_class(next),
1529 &hlock_class(prev)->locks_after,
1530 next->acquire_ip, distance);
Peter Zijlstra8e182572007-07-19 01:48:54 -07001531
1532 if (!ret)
1533 return 0;
1534
Dave Jonesf82b2172008-08-11 09:30:23 +02001535 ret = add_lock_to_list(hlock_class(next), hlock_class(prev),
1536 &hlock_class(next)->locks_before,
1537 next->acquire_ip, distance);
Peter Zijlstra8e182572007-07-19 01:48:54 -07001538 if (!ret)
1539 return 0;
1540
1541 /*
1542 * Debugging printouts:
1543 */
Dave Jonesf82b2172008-08-11 09:30:23 +02001544 if (verbose(hlock_class(prev)) || verbose(hlock_class(next))) {
Peter Zijlstra8e182572007-07-19 01:48:54 -07001545 graph_unlock();
1546 printk("\n new dependency: ");
Dave Jonesf82b2172008-08-11 09:30:23 +02001547 print_lock_name(hlock_class(prev));
Peter Zijlstra8e182572007-07-19 01:48:54 -07001548 printk(" => ");
Dave Jonesf82b2172008-08-11 09:30:23 +02001549 print_lock_name(hlock_class(next));
Peter Zijlstra8e182572007-07-19 01:48:54 -07001550 printk("\n");
1551 dump_stack();
1552 return graph_lock();
1553 }
1554 return 1;
1555}
1556
1557/*
1558 * Add the dependency to all directly-previous locks that are 'relevant'.
1559 * The ones that are relevant are (in increasing distance from curr):
1560 * all consecutive trylock entries and the final non-trylock entry - or
1561 * the end of this context's lock-chain - whichever comes first.
1562 */
1563static int
1564check_prevs_add(struct task_struct *curr, struct held_lock *next)
1565{
1566 int depth = curr->lockdep_depth;
1567 struct held_lock *hlock;
1568
1569 /*
1570 * Debugging checks.
1571 *
1572 * Depth must not be zero for a non-head lock:
1573 */
1574 if (!depth)
1575 goto out_bug;
1576 /*
1577 * At least two relevant locks must exist for this
1578 * to be a head:
1579 */
1580 if (curr->held_locks[depth].irq_context !=
1581 curr->held_locks[depth-1].irq_context)
1582 goto out_bug;
1583
1584 for (;;) {
1585 int distance = curr->lockdep_depth - depth + 1;
1586 hlock = curr->held_locks + depth-1;
1587 /*
1588 * Only non-recursive-read entries get new dependencies
1589 * added:
1590 */
1591 if (hlock->read != 2) {
1592 if (!check_prev_add(curr, hlock, next, distance))
1593 return 0;
1594 /*
1595 * Stop after the first non-trylock entry,
1596 * as non-trylock entries have added their
1597 * own direct dependencies already, so this
1598 * lock is connected to them indirectly:
1599 */
1600 if (!hlock->trylock)
1601 break;
1602 }
1603 depth--;
1604 /*
1605 * End of lock-stack?
1606 */
1607 if (!depth)
1608 break;
1609 /*
1610 * Stop the search if we cross into another context:
1611 */
1612 if (curr->held_locks[depth].irq_context !=
1613 curr->held_locks[depth-1].irq_context)
1614 break;
1615 }
1616 return 1;
1617out_bug:
1618 if (!debug_locks_off_graph_unlock())
1619 return 0;
1620
1621 WARN_ON(1);
1622
1623 return 0;
1624}
1625
1626unsigned long nr_lock_chains;
Huang, Ying443cd502008-06-20 16:39:21 +08001627struct lock_chain lock_chains[MAX_LOCKDEP_CHAINS];
Huang, Yingcd1a28e2008-06-23 11:20:54 +08001628int nr_chain_hlocks;
Huang, Ying443cd502008-06-20 16:39:21 +08001629static u16 chain_hlocks[MAX_LOCKDEP_CHAIN_HLOCKS];
1630
1631struct lock_class *lock_chain_get_class(struct lock_chain *chain, int i)
1632{
1633 return lock_classes + chain_hlocks[chain->base + i];
1634}
Peter Zijlstra8e182572007-07-19 01:48:54 -07001635
1636/*
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001637 * Look up a dependency chain. If the key is not present yet then
Jarek Poplawski9e860d02007-05-08 00:30:12 -07001638 * add it and return 1 - in this case the new dependency chain is
1639 * validated. If the key is already hashed, return 0.
1640 * (On return with 1 graph_lock is held.)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001641 */
Huang, Ying443cd502008-06-20 16:39:21 +08001642static inline int lookup_chain_cache(struct task_struct *curr,
1643 struct held_lock *hlock,
1644 u64 chain_key)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001645{
Dave Jonesf82b2172008-08-11 09:30:23 +02001646 struct lock_class *class = hlock_class(hlock);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001647 struct list_head *hash_head = chainhashentry(chain_key);
1648 struct lock_chain *chain;
Huang, Ying443cd502008-06-20 16:39:21 +08001649 struct held_lock *hlock_curr, *hlock_next;
Huang, Yingcd1a28e2008-06-23 11:20:54 +08001650 int i, j, n, cn;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001651
Jarek Poplawski381a2292007-02-10 01:44:58 -08001652 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
1653 return 0;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001654 /*
1655 * We can walk it lock-free, because entries only get added
1656 * to the hash:
1657 */
1658 list_for_each_entry(chain, hash_head, entry) {
1659 if (chain->chain_key == chain_key) {
1660cache_hit:
1661 debug_atomic_inc(&chain_lookup_hits);
Ingo Molnar81fc6852006-12-13 00:34:40 -08001662 if (very_verbose(class))
Andrew Morton755cd902006-12-29 16:49:14 -08001663 printk("\nhash chain already cached, key: "
1664 "%016Lx tail class: [%p] %s\n",
1665 (unsigned long long)chain_key,
1666 class->key, class->name);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001667 return 0;
1668 }
1669 }
Ingo Molnar81fc6852006-12-13 00:34:40 -08001670 if (very_verbose(class))
Andrew Morton755cd902006-12-29 16:49:14 -08001671 printk("\nnew hash chain, key: %016Lx tail class: [%p] %s\n",
1672 (unsigned long long)chain_key, class->key, class->name);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001673 /*
1674 * Allocate a new chain entry from the static array, and add
1675 * it to the hash:
1676 */
Ingo Molnar74c383f2006-12-13 00:34:43 -08001677 if (!graph_lock())
1678 return 0;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001679 /*
1680 * We have to walk the chain again locked - to avoid duplicates:
1681 */
1682 list_for_each_entry(chain, hash_head, entry) {
1683 if (chain->chain_key == chain_key) {
Ingo Molnar74c383f2006-12-13 00:34:43 -08001684 graph_unlock();
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001685 goto cache_hit;
1686 }
1687 }
1688 if (unlikely(nr_lock_chains >= MAX_LOCKDEP_CHAINS)) {
Ingo Molnar74c383f2006-12-13 00:34:43 -08001689 if (!debug_locks_off_graph_unlock())
1690 return 0;
1691
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001692 printk("BUG: MAX_LOCKDEP_CHAINS too low!\n");
1693 printk("turning off the locking correctness validator.\n");
1694 return 0;
1695 }
1696 chain = lock_chains + nr_lock_chains++;
1697 chain->chain_key = chain_key;
Huang, Ying443cd502008-06-20 16:39:21 +08001698 chain->irq_context = hlock->irq_context;
1699 /* Find the first held_lock of current chain */
1700 hlock_next = hlock;
1701 for (i = curr->lockdep_depth - 1; i >= 0; i--) {
1702 hlock_curr = curr->held_locks + i;
1703 if (hlock_curr->irq_context != hlock_next->irq_context)
1704 break;
1705 hlock_next = hlock;
1706 }
1707 i++;
1708 chain->depth = curr->lockdep_depth + 1 - i;
Huang, Yingcd1a28e2008-06-23 11:20:54 +08001709 cn = nr_chain_hlocks;
1710 while (cn + chain->depth <= MAX_LOCKDEP_CHAIN_HLOCKS) {
1711 n = cmpxchg(&nr_chain_hlocks, cn, cn + chain->depth);
1712 if (n == cn)
1713 break;
1714 cn = n;
1715 }
1716 if (likely(cn + chain->depth <= MAX_LOCKDEP_CHAIN_HLOCKS)) {
1717 chain->base = cn;
Huang, Ying443cd502008-06-20 16:39:21 +08001718 for (j = 0; j < chain->depth - 1; j++, i++) {
Dave Jonesf82b2172008-08-11 09:30:23 +02001719 int lock_id = curr->held_locks[i].class_idx - 1;
Huang, Ying443cd502008-06-20 16:39:21 +08001720 chain_hlocks[chain->base + j] = lock_id;
1721 }
1722 chain_hlocks[chain->base + j] = class - lock_classes;
1723 }
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001724 list_add_tail_rcu(&chain->entry, hash_head);
1725 debug_atomic_inc(&chain_lookup_misses);
Peter Zijlstra8e182572007-07-19 01:48:54 -07001726 inc_chains();
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001727
1728 return 1;
1729}
Peter Zijlstra8e182572007-07-19 01:48:54 -07001730
1731static int validate_chain(struct task_struct *curr, struct lockdep_map *lock,
Johannes Berg4e6045f2007-10-18 23:39:55 -07001732 struct held_lock *hlock, int chain_head, u64 chain_key)
Peter Zijlstra8e182572007-07-19 01:48:54 -07001733{
1734 /*
1735 * Trylock needs to maintain the stack of held locks, but it
1736 * does not add new dependencies, because trylock can be done
1737 * in any order.
1738 *
1739 * We look up the chain_key and do the O(N^2) check and update of
1740 * the dependencies only if this is a new dependency chain.
1741 * (If lookup_chain_cache() returns with 1 it acquires
1742 * graph_lock for us)
1743 */
1744 if (!hlock->trylock && (hlock->check == 2) &&
Huang, Ying443cd502008-06-20 16:39:21 +08001745 lookup_chain_cache(curr, hlock, chain_key)) {
Peter Zijlstra8e182572007-07-19 01:48:54 -07001746 /*
1747 * Check whether last held lock:
1748 *
1749 * - is irq-safe, if this lock is irq-unsafe
1750 * - is softirq-safe, if this lock is hardirq-unsafe
1751 *
1752 * And check whether the new lock's dependency graph
1753 * could lead back to the previous lock.
1754 *
1755 * any of these scenarios could lead to a deadlock. If
1756 * All validations
1757 */
1758 int ret = check_deadlock(curr, hlock, lock, hlock->read);
1759
1760 if (!ret)
1761 return 0;
1762 /*
1763 * Mark recursive read, as we jump over it when
1764 * building dependencies (just like we jump over
1765 * trylock entries):
1766 */
1767 if (ret == 2)
1768 hlock->read = 2;
1769 /*
1770 * Add dependency only if this lock is not the head
1771 * of the chain, and if it's not a secondary read-lock:
1772 */
1773 if (!chain_head && ret != 2)
1774 if (!check_prevs_add(curr, hlock))
1775 return 0;
1776 graph_unlock();
1777 } else
1778 /* after lookup_chain_cache(): */
1779 if (unlikely(!debug_locks))
1780 return 0;
1781
1782 return 1;
1783}
1784#else
1785static inline int validate_chain(struct task_struct *curr,
1786 struct lockdep_map *lock, struct held_lock *hlock,
Gregory Haskins3aa416b2007-10-11 22:11:11 +02001787 int chain_head, u64 chain_key)
Peter Zijlstra8e182572007-07-19 01:48:54 -07001788{
1789 return 1;
1790}
Peter Zijlstraca58abc2007-07-19 01:48:53 -07001791#endif
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001792
1793/*
1794 * We are building curr_chain_key incrementally, so double-check
1795 * it from scratch, to make sure that it's done correctly:
1796 */
Steven Rostedt1d09daa2008-05-12 21:20:55 +02001797static void check_chain_key(struct task_struct *curr)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001798{
1799#ifdef CONFIG_DEBUG_LOCKDEP
1800 struct held_lock *hlock, *prev_hlock = NULL;
1801 unsigned int i, id;
1802 u64 chain_key = 0;
1803
1804 for (i = 0; i < curr->lockdep_depth; i++) {
1805 hlock = curr->held_locks + i;
1806 if (chain_key != hlock->prev_chain_key) {
1807 debug_locks_off();
Arjan van de Ven2df8b1d2008-07-30 12:43:11 -07001808 WARN(1, "hm#1, depth: %u [%u], %016Lx != %016Lx\n",
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001809 curr->lockdep_depth, i,
1810 (unsigned long long)chain_key,
1811 (unsigned long long)hlock->prev_chain_key);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001812 return;
1813 }
Dave Jonesf82b2172008-08-11 09:30:23 +02001814 id = hlock->class_idx - 1;
Jarek Poplawski381a2292007-02-10 01:44:58 -08001815 if (DEBUG_LOCKS_WARN_ON(id >= MAX_LOCKDEP_KEYS))
1816 return;
1817
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001818 if (prev_hlock && (prev_hlock->irq_context !=
1819 hlock->irq_context))
1820 chain_key = 0;
1821 chain_key = iterate_chain_key(chain_key, id);
1822 prev_hlock = hlock;
1823 }
1824 if (chain_key != curr->curr_chain_key) {
1825 debug_locks_off();
Arjan van de Ven2df8b1d2008-07-30 12:43:11 -07001826 WARN(1, "hm#2, depth: %u [%u], %016Lx != %016Lx\n",
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001827 curr->lockdep_depth, i,
1828 (unsigned long long)chain_key,
1829 (unsigned long long)curr->curr_chain_key);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001830 }
1831#endif
1832}
1833
Peter Zijlstra8e182572007-07-19 01:48:54 -07001834static int
1835print_usage_bug(struct task_struct *curr, struct held_lock *this,
1836 enum lock_usage_bit prev_bit, enum lock_usage_bit new_bit)
1837{
1838 if (!debug_locks_off_graph_unlock() || debug_locks_silent)
1839 return 0;
1840
1841 printk("\n=================================\n");
1842 printk( "[ INFO: inconsistent lock state ]\n");
1843 print_kernel_version();
1844 printk( "---------------------------------\n");
1845
1846 printk("inconsistent {%s} -> {%s} usage.\n",
1847 usage_str[prev_bit], usage_str[new_bit]);
1848
1849 printk("%s/%d [HC%u[%lu]:SC%u[%lu]:HE%u:SE%u] takes:\n",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07001850 curr->comm, task_pid_nr(curr),
Peter Zijlstra8e182572007-07-19 01:48:54 -07001851 trace_hardirq_context(curr), hardirq_count() >> HARDIRQ_SHIFT,
1852 trace_softirq_context(curr), softirq_count() >> SOFTIRQ_SHIFT,
1853 trace_hardirqs_enabled(curr),
1854 trace_softirqs_enabled(curr));
1855 print_lock(this);
1856
1857 printk("{%s} state was registered at:\n", usage_str[prev_bit]);
Dave Jonesf82b2172008-08-11 09:30:23 +02001858 print_stack_trace(hlock_class(this)->usage_traces + prev_bit, 1);
Peter Zijlstra8e182572007-07-19 01:48:54 -07001859
1860 print_irqtrace_events(curr);
1861 printk("\nother info that might help us debug this:\n");
1862 lockdep_print_held_locks(curr);
1863
1864 printk("\nstack backtrace:\n");
1865 dump_stack();
1866
1867 return 0;
1868}
1869
1870/*
1871 * Print out an error if an invalid bit is set:
1872 */
1873static inline int
1874valid_state(struct task_struct *curr, struct held_lock *this,
1875 enum lock_usage_bit new_bit, enum lock_usage_bit bad_bit)
1876{
Dave Jonesf82b2172008-08-11 09:30:23 +02001877 if (unlikely(hlock_class(this)->usage_mask & (1 << bad_bit)))
Peter Zijlstra8e182572007-07-19 01:48:54 -07001878 return print_usage_bug(curr, this, bad_bit, new_bit);
1879 return 1;
1880}
1881
1882static int mark_lock(struct task_struct *curr, struct held_lock *this,
1883 enum lock_usage_bit new_bit);
1884
Steven Rostedt81d68a92008-05-12 21:20:42 +02001885#if defined(CONFIG_TRACE_IRQFLAGS) && defined(CONFIG_PROVE_LOCKING)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001886
1887/*
1888 * print irq inversion bug:
1889 */
1890static int
1891print_irq_inversion_bug(struct task_struct *curr, struct lock_class *other,
1892 struct held_lock *this, int forwards,
1893 const char *irqclass)
1894{
Ingo Molnar74c383f2006-12-13 00:34:43 -08001895 if (!debug_locks_off_graph_unlock() || debug_locks_silent)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001896 return 0;
1897
1898 printk("\n=========================================================\n");
1899 printk( "[ INFO: possible irq lock inversion dependency detected ]\n");
Dave Jones99de0552006-09-29 02:00:10 -07001900 print_kernel_version();
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001901 printk( "---------------------------------------------------------\n");
1902 printk("%s/%d just changed the state of lock:\n",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07001903 curr->comm, task_pid_nr(curr));
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001904 print_lock(this);
1905 if (forwards)
1906 printk("but this lock took another, %s-irq-unsafe lock in the past:\n", irqclass);
1907 else
1908 printk("but this lock was taken by another, %s-irq-safe lock in the past:\n", irqclass);
1909 print_lock_name(other);
1910 printk("\n\nand interrupts could create inverse lock ordering between them.\n\n");
1911
1912 printk("\nother info that might help us debug this:\n");
1913 lockdep_print_held_locks(curr);
1914
1915 printk("\nthe first lock's dependencies:\n");
Dave Jonesf82b2172008-08-11 09:30:23 +02001916 print_lock_dependencies(hlock_class(this), 0);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001917
1918 printk("\nthe second lock's dependencies:\n");
1919 print_lock_dependencies(other, 0);
1920
1921 printk("\nstack backtrace:\n");
1922 dump_stack();
1923
1924 return 0;
1925}
1926
1927/*
1928 * Prove that in the forwards-direction subgraph starting at <this>
1929 * there is no lock matching <mask>:
1930 */
1931static int
1932check_usage_forwards(struct task_struct *curr, struct held_lock *this,
1933 enum lock_usage_bit bit, const char *irqclass)
1934{
1935 int ret;
1936
1937 find_usage_bit = bit;
1938 /* fills in <forwards_match> */
Dave Jonesf82b2172008-08-11 09:30:23 +02001939 ret = find_usage_forwards(hlock_class(this), 0);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001940 if (!ret || ret == 1)
1941 return ret;
1942
1943 return print_irq_inversion_bug(curr, forwards_match, this, 1, irqclass);
1944}
1945
1946/*
1947 * Prove that in the backwards-direction subgraph starting at <this>
1948 * there is no lock matching <mask>:
1949 */
1950static int
1951check_usage_backwards(struct task_struct *curr, struct held_lock *this,
1952 enum lock_usage_bit bit, const char *irqclass)
1953{
1954 int ret;
1955
1956 find_usage_bit = bit;
1957 /* fills in <backwards_match> */
Dave Jonesf82b2172008-08-11 09:30:23 +02001958 ret = find_usage_backwards(hlock_class(this), 0);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001959 if (!ret || ret == 1)
1960 return ret;
1961
1962 return print_irq_inversion_bug(curr, backwards_match, this, 0, irqclass);
1963}
1964
Ingo Molnar3117df02006-12-13 00:34:43 -08001965void print_irqtrace_events(struct task_struct *curr)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001966{
1967 printk("irq event stamp: %u\n", curr->irq_events);
1968 printk("hardirqs last enabled at (%u): ", curr->hardirq_enable_event);
1969 print_ip_sym(curr->hardirq_enable_ip);
1970 printk("hardirqs last disabled at (%u): ", curr->hardirq_disable_event);
1971 print_ip_sym(curr->hardirq_disable_ip);
1972 printk("softirqs last enabled at (%u): ", curr->softirq_enable_event);
1973 print_ip_sym(curr->softirq_enable_ip);
1974 printk("softirqs last disabled at (%u): ", curr->softirq_disable_event);
1975 print_ip_sym(curr->softirq_disable_ip);
1976}
1977
Peter Zijlstra8e182572007-07-19 01:48:54 -07001978static int hardirq_verbose(struct lock_class *class)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001979{
Peter Zijlstra8e182572007-07-19 01:48:54 -07001980#if HARDIRQ_VERBOSE
1981 return class_filter(class);
1982#endif
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001983 return 0;
1984}
1985
Peter Zijlstra8e182572007-07-19 01:48:54 -07001986static int softirq_verbose(struct lock_class *class)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001987{
Peter Zijlstra8e182572007-07-19 01:48:54 -07001988#if SOFTIRQ_VERBOSE
1989 return class_filter(class);
1990#endif
1991 return 0;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001992}
1993
Nick Piggincf40bd12009-01-21 08:12:39 +01001994static int reclaim_verbose(struct lock_class *class)
1995{
1996#if RECLAIM_VERBOSE
1997 return class_filter(class);
1998#endif
1999 return 0;
2000}
2001
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002002#define STRICT_READ_CHECKS 1
2003
Peter Zijlstra6a6904d2009-01-22 16:07:44 +01002004static int
2005mark_lock_irq_used_in(struct task_struct *curr, struct held_lock *this,
2006 int new_bit, int excl_bit,
2007 const char *name, const char *rname,
2008 int (*verbose)(struct lock_class *class))
2009{
2010 if (!valid_state(curr, this, new_bit, excl_bit))
2011 return 0;
2012 if (!valid_state(curr, this, new_bit, excl_bit + 1))
2013 return 0;
2014 /*
2015 * just marked it hardirq-safe, check that this lock
2016 * took no hardirq-unsafe lock in the past:
2017 */
2018 if (!check_usage_forwards(curr, this, excl_bit, name))
2019 return 0;
2020#if STRICT_READ_CHECKS
2021 /*
2022 * just marked it hardirq-safe, check that this lock
2023 * took no hardirq-unsafe-read lock in the past:
2024 */
2025 if (!check_usage_forwards(curr, this, excl_bit + 1, rname))
2026 return 0;
2027#endif
2028 if (verbose(hlock_class(this)))
2029 return 2;
2030
2031 return 1;
2032}
2033
2034static int
2035mark_lock_irq_used_in_read(struct task_struct *curr, struct held_lock *this,
2036 int new_bit, int excl_bit,
2037 const char *name, const char *rname,
2038 int (*verbose)(struct lock_class *class))
2039{
2040 if (!valid_state(curr, this, new_bit, excl_bit))
2041 return 0;
2042 /*
2043 * just marked it hardirq-read-safe, check that this lock
2044 * took no hardirq-unsafe lock in the past:
2045 */
2046 if (!check_usage_forwards(curr, this, excl_bit, name))
2047 return 0;
2048 if (verbose(hlock_class(this)))
2049 return 2;
2050
2051 return 1;
2052}
2053
2054static int
2055mark_lock_irq_enabled(struct task_struct *curr, struct held_lock *this,
2056 int new_bit, int excl_bit,
2057 const char *name, const char *rname,
2058 int (*verbose)(struct lock_class *class))
2059{
2060 if (!valid_state(curr, this, new_bit, excl_bit))
2061 return 0;
2062 if (!valid_state(curr, this, new_bit, excl_bit + 1))
2063 return 0;
2064 /*
2065 * just marked it hardirq-unsafe, check that no hardirq-safe
2066 * lock in the system ever took it in the past:
2067 */
2068 if (!check_usage_backwards(curr, this, excl_bit, name))
2069 return 0;
2070#if STRICT_READ_CHECKS
2071 /*
2072 * just marked it hardirq-unsafe, check that no
2073 * hardirq-safe-read lock in the system ever took
2074 * it in the past:
2075 */
2076 if (!check_usage_backwards(curr, this, excl_bit + 1, rname))
2077 return 0;
2078#endif
2079 if (verbose(hlock_class(this)))
2080 return 2;
2081
2082 return 1;
2083}
2084
2085static int
2086mark_lock_irq_enabled_read(struct task_struct *curr, struct held_lock *this,
2087 int new_bit, int excl_bit,
2088 const char *name, const char *rname,
2089 int (*verbose)(struct lock_class *class))
2090{
2091 if (!valid_state(curr, this, new_bit, excl_bit))
2092 return 0;
2093#if STRICT_READ_CHECKS
2094 /*
2095 * just marked it hardirq-read-unsafe, check that no
2096 * hardirq-safe lock in the system ever took it in the past:
2097 */
2098 if (!check_usage_backwards(curr, this, excl_bit, name))
2099 return 0;
2100#endif
2101 if (verbose(hlock_class(this)))
2102 return 2;
2103
2104 return 1;
2105}
2106
Peter Zijlstra8e182572007-07-19 01:48:54 -07002107static int mark_lock_irq(struct task_struct *curr, struct held_lock *this,
2108 enum lock_usage_bit new_bit)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002109{
Peter Zijlstra8e182572007-07-19 01:48:54 -07002110 int ret = 1;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002111
Peter Zijlstra8e182572007-07-19 01:48:54 -07002112 switch(new_bit) {
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002113 case LOCK_USED_IN_HARDIRQ:
Peter Zijlstra6a6904d2009-01-22 16:07:44 +01002114 return mark_lock_irq_used_in(curr, this, new_bit,
2115 LOCK_ENABLED_HARDIRQ,
2116 "hard", "hard-read", hardirq_verbose);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002117 case LOCK_USED_IN_SOFTIRQ:
Peter Zijlstra6a6904d2009-01-22 16:07:44 +01002118 return mark_lock_irq_used_in(curr, this, new_bit,
2119 LOCK_ENABLED_SOFTIRQ,
2120 "soft", "soft-read", softirq_verbose);
Nick Piggincf40bd12009-01-21 08:12:39 +01002121 case LOCK_USED_IN_RECLAIM_FS:
Peter Zijlstra6a6904d2009-01-22 16:07:44 +01002122 return mark_lock_irq_used_in(curr, this, new_bit,
2123 LOCK_ENABLED_RECLAIM_FS,
2124 "reclaim-fs", "reclaim-fs-read",
2125 reclaim_verbose);
2126
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002127 case LOCK_USED_IN_HARDIRQ_READ:
Peter Zijlstra6a6904d2009-01-22 16:07:44 +01002128 return mark_lock_irq_used_in_read(curr, this, new_bit,
2129 LOCK_ENABLED_HARDIRQ,
2130 "hard", "hard-read", hardirq_verbose);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002131 case LOCK_USED_IN_SOFTIRQ_READ:
Peter Zijlstra6a6904d2009-01-22 16:07:44 +01002132 return mark_lock_irq_used_in_read(curr, this, new_bit,
2133 LOCK_ENABLED_SOFTIRQ,
2134 "soft", "soft-read", softirq_verbose);
Nick Piggincf40bd12009-01-21 08:12:39 +01002135 case LOCK_USED_IN_RECLAIM_FS_READ:
Peter Zijlstra6a6904d2009-01-22 16:07:44 +01002136 return mark_lock_irq_used_in_read(curr, this, new_bit,
2137 LOCK_ENABLED_RECLAIM_FS,
2138 "reclaim-fs", "reclaim-fs-read",
2139 reclaim_verbose);
2140
Peter Zijlstra4fc95e82009-01-22 13:10:52 +01002141 case LOCK_ENABLED_HARDIRQ:
Peter Zijlstra6a6904d2009-01-22 16:07:44 +01002142 return mark_lock_irq_enabled(curr, this, new_bit,
2143 LOCK_USED_IN_HARDIRQ,
2144 "hard", "hard-read", hardirq_verbose);
Peter Zijlstra4fc95e82009-01-22 13:10:52 +01002145 case LOCK_ENABLED_SOFTIRQ:
Peter Zijlstra6a6904d2009-01-22 16:07:44 +01002146 return mark_lock_irq_enabled(curr, this, new_bit,
2147 LOCK_USED_IN_SOFTIRQ,
2148 "soft", "soft-read", softirq_verbose);
Peter Zijlstraa652d702009-01-22 13:13:11 +01002149 case LOCK_ENABLED_RECLAIM_FS:
Peter Zijlstra6a6904d2009-01-22 16:07:44 +01002150 return mark_lock_irq_enabled(curr, this, new_bit,
2151 LOCK_USED_IN_RECLAIM_FS,
2152 "reclaim-fs", "reclaim-fs-read",
2153 reclaim_verbose);
2154
Peter Zijlstra4fc95e82009-01-22 13:10:52 +01002155 case LOCK_ENABLED_HARDIRQ_READ:
Peter Zijlstra6a6904d2009-01-22 16:07:44 +01002156 return mark_lock_irq_enabled_read(curr, this, new_bit,
2157 LOCK_USED_IN_HARDIRQ,
2158 "hard", "hard-read", hardirq_verbose);
Peter Zijlstra4fc95e82009-01-22 13:10:52 +01002159 case LOCK_ENABLED_SOFTIRQ_READ:
Peter Zijlstra6a6904d2009-01-22 16:07:44 +01002160 return mark_lock_irq_enabled_read(curr, this, new_bit,
2161 LOCK_USED_IN_SOFTIRQ,
2162 "soft", "soft-read", softirq_verbose);
Peter Zijlstraa652d702009-01-22 13:13:11 +01002163 case LOCK_ENABLED_RECLAIM_FS_READ:
Peter Zijlstra6a6904d2009-01-22 16:07:44 +01002164 return mark_lock_irq_enabled_read(curr, this, new_bit,
2165 LOCK_USED_IN_RECLAIM_FS,
2166 "reclaim-fs", "reclaim-fs-read",
2167 reclaim_verbose);
2168
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002169 default:
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002170 WARN_ON(1);
Peter Zijlstra8e182572007-07-19 01:48:54 -07002171 break;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002172 }
2173
2174 return ret;
2175}
2176
Nick Piggincf40bd12009-01-21 08:12:39 +01002177enum mark_type {
Peter Zijlstra36bfb9b2009-01-22 14:12:41 +01002178#define LOCKDEP_STATE(__STATE) __STATE,
2179#include "lockdep_states.h"
2180#undef LOCKDEP_STATE
Nick Piggincf40bd12009-01-21 08:12:39 +01002181};
2182
Peter Zijlstra36bfb9b2009-01-22 14:12:41 +01002183#define MARK_HELD_CASE(__STATE) \
2184 case __STATE: \
2185 if (hlock->read) \
2186 usage_bit = LOCK_ENABLED_##__STATE##_READ; \
2187 else \
2188 usage_bit = LOCK_ENABLED_##__STATE; \
2189 break;
2190
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002191/*
2192 * Mark all held locks with a usage bit:
2193 */
Steven Rostedt1d09daa2008-05-12 21:20:55 +02002194static int
Nick Piggincf40bd12009-01-21 08:12:39 +01002195mark_held_locks(struct task_struct *curr, enum mark_type mark)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002196{
2197 enum lock_usage_bit usage_bit;
2198 struct held_lock *hlock;
2199 int i;
2200
2201 for (i = 0; i < curr->lockdep_depth; i++) {
2202 hlock = curr->held_locks + i;
2203
Nick Piggincf40bd12009-01-21 08:12:39 +01002204 switch (mark) {
Peter Zijlstra36bfb9b2009-01-22 14:12:41 +01002205#define LOCKDEP_STATE(__STATE) MARK_HELD_CASE(__STATE)
2206#include "lockdep_states.h"
2207#undef LOCKDEP_STATE
Nick Piggincf40bd12009-01-21 08:12:39 +01002208 default:
2209 BUG();
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002210 }
Nick Piggincf40bd12009-01-21 08:12:39 +01002211
Jarek Poplawski4ff773bb2007-05-08 00:31:00 -07002212 if (!mark_lock(curr, hlock, usage_bit))
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002213 return 0;
2214 }
2215
2216 return 1;
2217}
2218
2219/*
2220 * Debugging helper: via this flag we know that we are in
2221 * 'early bootup code', and will warn about any invalid irqs-on event:
2222 */
2223static int early_boot_irqs_enabled;
2224
2225void early_boot_irqs_off(void)
2226{
2227 early_boot_irqs_enabled = 0;
2228}
2229
2230void early_boot_irqs_on(void)
2231{
2232 early_boot_irqs_enabled = 1;
2233}
2234
2235/*
2236 * Hardirqs will be enabled:
2237 */
Heiko Carstens6afe40b2008-10-28 11:14:58 +01002238void trace_hardirqs_on_caller(unsigned long ip)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002239{
2240 struct task_struct *curr = current;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002241
Heiko Carstens6afe40b2008-10-28 11:14:58 +01002242 time_hardirqs_on(CALLER_ADDR0, ip);
Steven Rostedt81d68a92008-05-12 21:20:42 +02002243
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002244 if (unlikely(!debug_locks || current->lockdep_recursion))
2245 return;
2246
2247 if (DEBUG_LOCKS_WARN_ON(unlikely(!early_boot_irqs_enabled)))
2248 return;
2249
2250 if (unlikely(curr->hardirqs_enabled)) {
2251 debug_atomic_inc(&redundant_hardirqs_on);
2252 return;
2253 }
2254 /* we'll do an OFF -> ON transition: */
2255 curr->hardirqs_enabled = 1;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002256
2257 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2258 return;
2259 if (DEBUG_LOCKS_WARN_ON(current->hardirq_context))
2260 return;
2261 /*
2262 * We are going to turn hardirqs on, so set the
2263 * usage bit for all held locks:
2264 */
Nick Piggincf40bd12009-01-21 08:12:39 +01002265 if (!mark_held_locks(curr, HARDIRQ))
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002266 return;
2267 /*
2268 * If we have softirqs enabled, then set the usage
2269 * bit for all held locks. (disabled hardirqs prevented
2270 * this bit from being set before)
2271 */
2272 if (curr->softirqs_enabled)
Nick Piggincf40bd12009-01-21 08:12:39 +01002273 if (!mark_held_locks(curr, SOFTIRQ))
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002274 return;
2275
2276 curr->hardirq_enable_ip = ip;
2277 curr->hardirq_enable_event = ++curr->irq_events;
2278 debug_atomic_inc(&hardirqs_on_events);
2279}
Steven Rostedt81d68a92008-05-12 21:20:42 +02002280EXPORT_SYMBOL(trace_hardirqs_on_caller);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002281
Steven Rostedt1d09daa2008-05-12 21:20:55 +02002282void trace_hardirqs_on(void)
Steven Rostedt81d68a92008-05-12 21:20:42 +02002283{
2284 trace_hardirqs_on_caller(CALLER_ADDR0);
2285}
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002286EXPORT_SYMBOL(trace_hardirqs_on);
2287
2288/*
2289 * Hardirqs were disabled:
2290 */
Heiko Carstens6afe40b2008-10-28 11:14:58 +01002291void trace_hardirqs_off_caller(unsigned long ip)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002292{
2293 struct task_struct *curr = current;
2294
Heiko Carstens6afe40b2008-10-28 11:14:58 +01002295 time_hardirqs_off(CALLER_ADDR0, ip);
Steven Rostedt81d68a92008-05-12 21:20:42 +02002296
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002297 if (unlikely(!debug_locks || current->lockdep_recursion))
2298 return;
2299
2300 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2301 return;
2302
2303 if (curr->hardirqs_enabled) {
2304 /*
2305 * We have done an ON -> OFF transition:
2306 */
2307 curr->hardirqs_enabled = 0;
Heiko Carstens6afe40b2008-10-28 11:14:58 +01002308 curr->hardirq_disable_ip = ip;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002309 curr->hardirq_disable_event = ++curr->irq_events;
2310 debug_atomic_inc(&hardirqs_off_events);
2311 } else
2312 debug_atomic_inc(&redundant_hardirqs_off);
2313}
Steven Rostedt81d68a92008-05-12 21:20:42 +02002314EXPORT_SYMBOL(trace_hardirqs_off_caller);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002315
Steven Rostedt1d09daa2008-05-12 21:20:55 +02002316void trace_hardirqs_off(void)
Steven Rostedt81d68a92008-05-12 21:20:42 +02002317{
2318 trace_hardirqs_off_caller(CALLER_ADDR0);
2319}
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002320EXPORT_SYMBOL(trace_hardirqs_off);
2321
2322/*
2323 * Softirqs will be enabled:
2324 */
2325void trace_softirqs_on(unsigned long ip)
2326{
2327 struct task_struct *curr = current;
2328
2329 if (unlikely(!debug_locks))
2330 return;
2331
2332 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2333 return;
2334
2335 if (curr->softirqs_enabled) {
2336 debug_atomic_inc(&redundant_softirqs_on);
2337 return;
2338 }
2339
2340 /*
2341 * We'll do an OFF -> ON transition:
2342 */
2343 curr->softirqs_enabled = 1;
2344 curr->softirq_enable_ip = ip;
2345 curr->softirq_enable_event = ++curr->irq_events;
2346 debug_atomic_inc(&softirqs_on_events);
2347 /*
2348 * We are going to turn softirqs on, so set the
2349 * usage bit for all held locks, if hardirqs are
2350 * enabled too:
2351 */
2352 if (curr->hardirqs_enabled)
Nick Piggincf40bd12009-01-21 08:12:39 +01002353 mark_held_locks(curr, SOFTIRQ);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002354}
2355
2356/*
2357 * Softirqs were disabled:
2358 */
2359void trace_softirqs_off(unsigned long ip)
2360{
2361 struct task_struct *curr = current;
2362
2363 if (unlikely(!debug_locks))
2364 return;
2365
2366 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2367 return;
2368
2369 if (curr->softirqs_enabled) {
2370 /*
2371 * We have done an ON -> OFF transition:
2372 */
2373 curr->softirqs_enabled = 0;
2374 curr->softirq_disable_ip = ip;
2375 curr->softirq_disable_event = ++curr->irq_events;
2376 debug_atomic_inc(&softirqs_off_events);
2377 DEBUG_LOCKS_WARN_ON(!softirq_count());
2378 } else
2379 debug_atomic_inc(&redundant_softirqs_off);
2380}
2381
Nick Piggincf40bd12009-01-21 08:12:39 +01002382void lockdep_trace_alloc(gfp_t gfp_mask)
2383{
2384 struct task_struct *curr = current;
2385
2386 if (unlikely(!debug_locks))
2387 return;
2388
2389 /* no reclaim without waiting on it */
2390 if (!(gfp_mask & __GFP_WAIT))
2391 return;
2392
2393 /* this guy won't enter reclaim */
2394 if ((curr->flags & PF_MEMALLOC) && !(gfp_mask & __GFP_NOMEMALLOC))
2395 return;
2396
2397 /* We're only interested __GFP_FS allocations for now */
2398 if (!(gfp_mask & __GFP_FS))
2399 return;
2400
2401 if (DEBUG_LOCKS_WARN_ON(irqs_disabled()))
2402 return;
2403
2404 mark_held_locks(curr, RECLAIM_FS);
2405}
2406
Peter Zijlstra8e182572007-07-19 01:48:54 -07002407static int mark_irqflags(struct task_struct *curr, struct held_lock *hlock)
2408{
2409 /*
2410 * If non-trylock use in a hardirq or softirq context, then
2411 * mark the lock as used in these contexts:
2412 */
2413 if (!hlock->trylock) {
2414 if (hlock->read) {
2415 if (curr->hardirq_context)
2416 if (!mark_lock(curr, hlock,
2417 LOCK_USED_IN_HARDIRQ_READ))
2418 return 0;
2419 if (curr->softirq_context)
2420 if (!mark_lock(curr, hlock,
2421 LOCK_USED_IN_SOFTIRQ_READ))
2422 return 0;
2423 } else {
2424 if (curr->hardirq_context)
2425 if (!mark_lock(curr, hlock, LOCK_USED_IN_HARDIRQ))
2426 return 0;
2427 if (curr->softirq_context)
2428 if (!mark_lock(curr, hlock, LOCK_USED_IN_SOFTIRQ))
2429 return 0;
2430 }
2431 }
2432 if (!hlock->hardirqs_off) {
2433 if (hlock->read) {
2434 if (!mark_lock(curr, hlock,
Peter Zijlstra4fc95e82009-01-22 13:10:52 +01002435 LOCK_ENABLED_HARDIRQ_READ))
Peter Zijlstra8e182572007-07-19 01:48:54 -07002436 return 0;
2437 if (curr->softirqs_enabled)
2438 if (!mark_lock(curr, hlock,
Peter Zijlstra4fc95e82009-01-22 13:10:52 +01002439 LOCK_ENABLED_SOFTIRQ_READ))
Peter Zijlstra8e182572007-07-19 01:48:54 -07002440 return 0;
2441 } else {
2442 if (!mark_lock(curr, hlock,
Peter Zijlstra4fc95e82009-01-22 13:10:52 +01002443 LOCK_ENABLED_HARDIRQ))
Peter Zijlstra8e182572007-07-19 01:48:54 -07002444 return 0;
2445 if (curr->softirqs_enabled)
2446 if (!mark_lock(curr, hlock,
Peter Zijlstra4fc95e82009-01-22 13:10:52 +01002447 LOCK_ENABLED_SOFTIRQ))
Peter Zijlstra8e182572007-07-19 01:48:54 -07002448 return 0;
2449 }
2450 }
2451
Nick Piggincf40bd12009-01-21 08:12:39 +01002452 /*
2453 * We reuse the irq context infrastructure more broadly as a general
2454 * context checking code. This tests GFP_FS recursion (a lock taken
2455 * during reclaim for a GFP_FS allocation is held over a GFP_FS
2456 * allocation).
2457 */
2458 if (!hlock->trylock && (curr->lockdep_reclaim_gfp & __GFP_FS)) {
2459 if (hlock->read) {
2460 if (!mark_lock(curr, hlock, LOCK_USED_IN_RECLAIM_FS_READ))
2461 return 0;
2462 } else {
2463 if (!mark_lock(curr, hlock, LOCK_USED_IN_RECLAIM_FS))
2464 return 0;
2465 }
2466 }
2467
Peter Zijlstra8e182572007-07-19 01:48:54 -07002468 return 1;
2469}
2470
2471static int separate_irq_context(struct task_struct *curr,
2472 struct held_lock *hlock)
2473{
2474 unsigned int depth = curr->lockdep_depth;
2475
2476 /*
2477 * Keep track of points where we cross into an interrupt context:
2478 */
2479 hlock->irq_context = 2*(curr->hardirq_context ? 1 : 0) +
2480 curr->softirq_context;
2481 if (depth) {
2482 struct held_lock *prev_hlock;
2483
2484 prev_hlock = curr->held_locks + depth-1;
2485 /*
2486 * If we cross into another context, reset the
2487 * hash key (this also prevents the checking and the
2488 * adding of the dependency to 'prev'):
2489 */
2490 if (prev_hlock->irq_context != hlock->irq_context)
2491 return 1;
2492 }
2493 return 0;
2494}
2495
2496#else
2497
2498static inline
2499int mark_lock_irq(struct task_struct *curr, struct held_lock *this,
2500 enum lock_usage_bit new_bit)
2501{
2502 WARN_ON(1);
2503 return 1;
2504}
2505
2506static inline int mark_irqflags(struct task_struct *curr,
2507 struct held_lock *hlock)
2508{
2509 return 1;
2510}
2511
2512static inline int separate_irq_context(struct task_struct *curr,
2513 struct held_lock *hlock)
2514{
2515 return 0;
2516}
2517
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002518#endif
2519
2520/*
Peter Zijlstra8e182572007-07-19 01:48:54 -07002521 * Mark a lock with a usage bit, and validate the state transition:
2522 */
Steven Rostedt1d09daa2008-05-12 21:20:55 +02002523static int mark_lock(struct task_struct *curr, struct held_lock *this,
Steven Rostedt0764d232008-05-12 21:20:44 +02002524 enum lock_usage_bit new_bit)
Peter Zijlstra8e182572007-07-19 01:48:54 -07002525{
2526 unsigned int new_mask = 1 << new_bit, ret = 1;
2527
2528 /*
2529 * If already set then do not dirty the cacheline,
2530 * nor do any checks:
2531 */
Dave Jonesf82b2172008-08-11 09:30:23 +02002532 if (likely(hlock_class(this)->usage_mask & new_mask))
Peter Zijlstra8e182572007-07-19 01:48:54 -07002533 return 1;
2534
2535 if (!graph_lock())
2536 return 0;
2537 /*
2538 * Make sure we didnt race:
2539 */
Dave Jonesf82b2172008-08-11 09:30:23 +02002540 if (unlikely(hlock_class(this)->usage_mask & new_mask)) {
Peter Zijlstra8e182572007-07-19 01:48:54 -07002541 graph_unlock();
2542 return 1;
2543 }
2544
Dave Jonesf82b2172008-08-11 09:30:23 +02002545 hlock_class(this)->usage_mask |= new_mask;
Peter Zijlstra8e182572007-07-19 01:48:54 -07002546
Dave Jonesf82b2172008-08-11 09:30:23 +02002547 if (!save_trace(hlock_class(this)->usage_traces + new_bit))
Peter Zijlstra8e182572007-07-19 01:48:54 -07002548 return 0;
2549
2550 switch (new_bit) {
Peter Zijlstra53464172009-01-22 14:15:53 +01002551#define LOCKDEP_STATE(__STATE) \
2552 case LOCK_USED_IN_##__STATE: \
2553 case LOCK_USED_IN_##__STATE##_READ: \
2554 case LOCK_ENABLED_##__STATE: \
2555 case LOCK_ENABLED_##__STATE##_READ:
2556#include "lockdep_states.h"
2557#undef LOCKDEP_STATE
Peter Zijlstra8e182572007-07-19 01:48:54 -07002558 ret = mark_lock_irq(curr, this, new_bit);
2559 if (!ret)
2560 return 0;
2561 break;
2562 case LOCK_USED:
Peter Zijlstra8e182572007-07-19 01:48:54 -07002563 debug_atomic_dec(&nr_unused_locks);
2564 break;
2565 default:
2566 if (!debug_locks_off_graph_unlock())
2567 return 0;
2568 WARN_ON(1);
2569 return 0;
2570 }
2571
2572 graph_unlock();
2573
2574 /*
2575 * We must printk outside of the graph_lock:
2576 */
2577 if (ret == 2) {
2578 printk("\nmarked lock as {%s}:\n", usage_str[new_bit]);
2579 print_lock(this);
2580 print_irqtrace_events(curr);
2581 dump_stack();
2582 }
2583
2584 return ret;
2585}
2586
2587/*
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002588 * Initialize a lock instance's lock-class mapping info:
2589 */
2590void lockdep_init_map(struct lockdep_map *lock, const char *name,
Peter Zijlstra4dfbb9d2006-10-11 01:45:14 -04002591 struct lock_class_key *key, int subclass)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002592{
2593 if (unlikely(!debug_locks))
2594 return;
2595
2596 if (DEBUG_LOCKS_WARN_ON(!key))
2597 return;
2598 if (DEBUG_LOCKS_WARN_ON(!name))
2599 return;
2600 /*
2601 * Sanity check, the lock-class key must be persistent:
2602 */
2603 if (!static_obj(key)) {
2604 printk("BUG: key %p not in .data!\n", key);
2605 DEBUG_LOCKS_WARN_ON(1);
2606 return;
2607 }
2608 lock->name = name;
2609 lock->key = key;
Ingo Molnard6d897c2006-07-10 04:44:04 -07002610 lock->class_cache = NULL;
Peter Zijlstra96645672007-07-19 01:49:00 -07002611#ifdef CONFIG_LOCK_STAT
2612 lock->cpu = raw_smp_processor_id();
2613#endif
Peter Zijlstra4dfbb9d2006-10-11 01:45:14 -04002614 if (subclass)
2615 register_lock_class(lock, subclass, 1);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002616}
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002617EXPORT_SYMBOL_GPL(lockdep_init_map);
2618
2619/*
2620 * This gets called for every mutex_lock*()/spin_lock*() operation.
2621 * We maintain the dependency maps and validate the locking attempt:
2622 */
2623static int __lock_acquire(struct lockdep_map *lock, unsigned int subclass,
2624 int trylock, int read, int check, int hardirqs_off,
Peter Zijlstra7531e2f2008-08-11 09:30:24 +02002625 struct lockdep_map *nest_lock, unsigned long ip)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002626{
2627 struct task_struct *curr = current;
Ingo Molnard6d897c2006-07-10 04:44:04 -07002628 struct lock_class *class = NULL;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002629 struct held_lock *hlock;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002630 unsigned int depth, id;
2631 int chain_head = 0;
2632 u64 chain_key;
2633
Peter Zijlstraf20786f2007-07-19 01:48:56 -07002634 if (!prove_locking)
2635 check = 1;
2636
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002637 if (unlikely(!debug_locks))
2638 return 0;
2639
2640 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2641 return 0;
2642
2643 if (unlikely(subclass >= MAX_LOCKDEP_SUBCLASSES)) {
2644 debug_locks_off();
2645 printk("BUG: MAX_LOCKDEP_SUBCLASSES too low!\n");
2646 printk("turning off the locking correctness validator.\n");
2647 return 0;
2648 }
2649
Ingo Molnard6d897c2006-07-10 04:44:04 -07002650 if (!subclass)
2651 class = lock->class_cache;
2652 /*
2653 * Not cached yet or subclass?
2654 */
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002655 if (unlikely(!class)) {
Peter Zijlstra4dfbb9d2006-10-11 01:45:14 -04002656 class = register_lock_class(lock, subclass, 0);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002657 if (!class)
2658 return 0;
2659 }
2660 debug_atomic_inc((atomic_t *)&class->ops);
2661 if (very_verbose(class)) {
2662 printk("\nacquire class [%p] %s", class->key, class->name);
2663 if (class->name_version > 1)
2664 printk("#%d", class->name_version);
2665 printk("\n");
2666 dump_stack();
2667 }
2668
2669 /*
2670 * Add the lock to the list of currently held locks.
2671 * (we dont increase the depth just yet, up until the
2672 * dependency checks are done)
2673 */
2674 depth = curr->lockdep_depth;
2675 if (DEBUG_LOCKS_WARN_ON(depth >= MAX_LOCK_DEPTH))
2676 return 0;
2677
2678 hlock = curr->held_locks + depth;
Dave Jonesf82b2172008-08-11 09:30:23 +02002679 if (DEBUG_LOCKS_WARN_ON(!class))
2680 return 0;
2681 hlock->class_idx = class - lock_classes + 1;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002682 hlock->acquire_ip = ip;
2683 hlock->instance = lock;
Peter Zijlstra7531e2f2008-08-11 09:30:24 +02002684 hlock->nest_lock = nest_lock;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002685 hlock->trylock = trylock;
2686 hlock->read = read;
2687 hlock->check = check;
Dmitry Baryshkov6951b122008-08-18 04:26:37 +04002688 hlock->hardirqs_off = !!hardirqs_off;
Peter Zijlstraf20786f2007-07-19 01:48:56 -07002689#ifdef CONFIG_LOCK_STAT
2690 hlock->waittime_stamp = 0;
2691 hlock->holdtime_stamp = sched_clock();
2692#endif
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002693
Peter Zijlstra8e182572007-07-19 01:48:54 -07002694 if (check == 2 && !mark_irqflags(curr, hlock))
2695 return 0;
2696
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002697 /* mark it as used: */
Jarek Poplawski4ff773bb2007-05-08 00:31:00 -07002698 if (!mark_lock(curr, hlock, LOCK_USED))
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002699 return 0;
Peter Zijlstra8e182572007-07-19 01:48:54 -07002700
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002701 /*
Gautham R Shenoy17aacfb2007-10-28 20:47:01 +01002702 * Calculate the chain hash: it's the combined hash of all the
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002703 * lock keys along the dependency chain. We save the hash value
2704 * at every step so that we can get the current hash easily
2705 * after unlock. The chain hash is then used to cache dependency
2706 * results.
2707 *
2708 * The 'key ID' is what is the most compact key value to drive
2709 * the hash, not class->key.
2710 */
2711 id = class - lock_classes;
2712 if (DEBUG_LOCKS_WARN_ON(id >= MAX_LOCKDEP_KEYS))
2713 return 0;
2714
2715 chain_key = curr->curr_chain_key;
2716 if (!depth) {
2717 if (DEBUG_LOCKS_WARN_ON(chain_key != 0))
2718 return 0;
2719 chain_head = 1;
2720 }
2721
2722 hlock->prev_chain_key = chain_key;
Peter Zijlstra8e182572007-07-19 01:48:54 -07002723 if (separate_irq_context(curr, hlock)) {
2724 chain_key = 0;
2725 chain_head = 1;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002726 }
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002727 chain_key = iterate_chain_key(chain_key, id);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002728
Gregory Haskins3aa416b2007-10-11 22:11:11 +02002729 if (!validate_chain(curr, lock, hlock, chain_head, chain_key))
Peter Zijlstra8e182572007-07-19 01:48:54 -07002730 return 0;
Jarek Poplawski381a2292007-02-10 01:44:58 -08002731
Gregory Haskins3aa416b2007-10-11 22:11:11 +02002732 curr->curr_chain_key = chain_key;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002733 curr->lockdep_depth++;
2734 check_chain_key(curr);
Jarek Poplawski60e114d2007-02-20 13:58:00 -08002735#ifdef CONFIG_DEBUG_LOCKDEP
2736 if (unlikely(!debug_locks))
2737 return 0;
2738#endif
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002739 if (unlikely(curr->lockdep_depth >= MAX_LOCK_DEPTH)) {
2740 debug_locks_off();
2741 printk("BUG: MAX_LOCK_DEPTH too low!\n");
2742 printk("turning off the locking correctness validator.\n");
2743 return 0;
2744 }
Jarek Poplawski381a2292007-02-10 01:44:58 -08002745
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002746 if (unlikely(curr->lockdep_depth > max_lockdep_depth))
2747 max_lockdep_depth = curr->lockdep_depth;
2748
2749 return 1;
2750}
2751
2752static int
2753print_unlock_inbalance_bug(struct task_struct *curr, struct lockdep_map *lock,
2754 unsigned long ip)
2755{
2756 if (!debug_locks_off())
2757 return 0;
2758 if (debug_locks_silent)
2759 return 0;
2760
2761 printk("\n=====================================\n");
2762 printk( "[ BUG: bad unlock balance detected! ]\n");
2763 printk( "-------------------------------------\n");
2764 printk("%s/%d is trying to release lock (",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07002765 curr->comm, task_pid_nr(curr));
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002766 print_lockdep_cache(lock);
2767 printk(") at:\n");
2768 print_ip_sym(ip);
2769 printk("but there are no more locks to release!\n");
2770 printk("\nother info that might help us debug this:\n");
2771 lockdep_print_held_locks(curr);
2772
2773 printk("\nstack backtrace:\n");
2774 dump_stack();
2775
2776 return 0;
2777}
2778
2779/*
2780 * Common debugging checks for both nested and non-nested unlock:
2781 */
2782static int check_unlock(struct task_struct *curr, struct lockdep_map *lock,
2783 unsigned long ip)
2784{
2785 if (unlikely(!debug_locks))
2786 return 0;
2787 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2788 return 0;
2789
2790 if (curr->lockdep_depth <= 0)
2791 return print_unlock_inbalance_bug(curr, lock, ip);
2792
2793 return 1;
2794}
2795
Peter Zijlstra64aa3482008-08-11 09:30:21 +02002796static int
Peter Zijlstra00ef9f72008-12-04 09:00:17 +01002797__lock_set_class(struct lockdep_map *lock, const char *name,
2798 struct lock_class_key *key, unsigned int subclass,
2799 unsigned long ip)
Peter Zijlstra64aa3482008-08-11 09:30:21 +02002800{
2801 struct task_struct *curr = current;
2802 struct held_lock *hlock, *prev_hlock;
2803 struct lock_class *class;
2804 unsigned int depth;
2805 int i;
2806
2807 depth = curr->lockdep_depth;
2808 if (DEBUG_LOCKS_WARN_ON(!depth))
2809 return 0;
2810
2811 prev_hlock = NULL;
2812 for (i = depth-1; i >= 0; i--) {
2813 hlock = curr->held_locks + i;
2814 /*
2815 * We must not cross into another context:
2816 */
2817 if (prev_hlock && prev_hlock->irq_context != hlock->irq_context)
2818 break;
2819 if (hlock->instance == lock)
2820 goto found_it;
2821 prev_hlock = hlock;
2822 }
2823 return print_unlock_inbalance_bug(curr, lock, ip);
2824
2825found_it:
Peter Zijlstra00ef9f72008-12-04 09:00:17 +01002826 lockdep_init_map(lock, name, key, 0);
Peter Zijlstra64aa3482008-08-11 09:30:21 +02002827 class = register_lock_class(lock, subclass, 0);
Dave Jonesf82b2172008-08-11 09:30:23 +02002828 hlock->class_idx = class - lock_classes + 1;
Peter Zijlstra64aa3482008-08-11 09:30:21 +02002829
2830 curr->lockdep_depth = i;
2831 curr->curr_chain_key = hlock->prev_chain_key;
2832
2833 for (; i < depth; i++) {
2834 hlock = curr->held_locks + i;
2835 if (!__lock_acquire(hlock->instance,
Dave Jonesf82b2172008-08-11 09:30:23 +02002836 hlock_class(hlock)->subclass, hlock->trylock,
Peter Zijlstra64aa3482008-08-11 09:30:21 +02002837 hlock->read, hlock->check, hlock->hardirqs_off,
Peter Zijlstra7531e2f2008-08-11 09:30:24 +02002838 hlock->nest_lock, hlock->acquire_ip))
Peter Zijlstra64aa3482008-08-11 09:30:21 +02002839 return 0;
2840 }
2841
2842 if (DEBUG_LOCKS_WARN_ON(curr->lockdep_depth != depth))
2843 return 0;
2844 return 1;
2845}
2846
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002847/*
2848 * Remove the lock to the list of currently held locks in a
2849 * potentially non-nested (out of order) manner. This is a
2850 * relatively rare operation, as all the unlock APIs default
2851 * to nested mode (which uses lock_release()):
2852 */
2853static int
2854lock_release_non_nested(struct task_struct *curr,
2855 struct lockdep_map *lock, unsigned long ip)
2856{
2857 struct held_lock *hlock, *prev_hlock;
2858 unsigned int depth;
2859 int i;
2860
2861 /*
2862 * Check whether the lock exists in the current stack
2863 * of held locks:
2864 */
2865 depth = curr->lockdep_depth;
2866 if (DEBUG_LOCKS_WARN_ON(!depth))
2867 return 0;
2868
2869 prev_hlock = NULL;
2870 for (i = depth-1; i >= 0; i--) {
2871 hlock = curr->held_locks + i;
2872 /*
2873 * We must not cross into another context:
2874 */
2875 if (prev_hlock && prev_hlock->irq_context != hlock->irq_context)
2876 break;
2877 if (hlock->instance == lock)
2878 goto found_it;
2879 prev_hlock = hlock;
2880 }
2881 return print_unlock_inbalance_bug(curr, lock, ip);
2882
2883found_it:
Peter Zijlstraf20786f2007-07-19 01:48:56 -07002884 lock_release_holdtime(hlock);
2885
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002886 /*
2887 * We have the right lock to unlock, 'hlock' points to it.
2888 * Now we remove it from the stack, and add back the other
2889 * entries (if any), recalculating the hash along the way:
2890 */
2891 curr->lockdep_depth = i;
2892 curr->curr_chain_key = hlock->prev_chain_key;
2893
2894 for (i++; i < depth; i++) {
2895 hlock = curr->held_locks + i;
2896 if (!__lock_acquire(hlock->instance,
Dave Jonesf82b2172008-08-11 09:30:23 +02002897 hlock_class(hlock)->subclass, hlock->trylock,
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002898 hlock->read, hlock->check, hlock->hardirqs_off,
Peter Zijlstra7531e2f2008-08-11 09:30:24 +02002899 hlock->nest_lock, hlock->acquire_ip))
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002900 return 0;
2901 }
2902
2903 if (DEBUG_LOCKS_WARN_ON(curr->lockdep_depth != depth - 1))
2904 return 0;
2905 return 1;
2906}
2907
2908/*
2909 * Remove the lock to the list of currently held locks - this gets
2910 * called on mutex_unlock()/spin_unlock*() (or on a failed
2911 * mutex_lock_interruptible()). This is done for unlocks that nest
2912 * perfectly. (i.e. the current top of the lock-stack is unlocked)
2913 */
2914static int lock_release_nested(struct task_struct *curr,
2915 struct lockdep_map *lock, unsigned long ip)
2916{
2917 struct held_lock *hlock;
2918 unsigned int depth;
2919
2920 /*
2921 * Pop off the top of the lock stack:
2922 */
2923 depth = curr->lockdep_depth - 1;
2924 hlock = curr->held_locks + depth;
2925
2926 /*
2927 * Is the unlock non-nested:
2928 */
2929 if (hlock->instance != lock)
2930 return lock_release_non_nested(curr, lock, ip);
2931 curr->lockdep_depth--;
2932
2933 if (DEBUG_LOCKS_WARN_ON(!depth && (hlock->prev_chain_key != 0)))
2934 return 0;
2935
2936 curr->curr_chain_key = hlock->prev_chain_key;
2937
Peter Zijlstraf20786f2007-07-19 01:48:56 -07002938 lock_release_holdtime(hlock);
2939
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002940#ifdef CONFIG_DEBUG_LOCKDEP
2941 hlock->prev_chain_key = 0;
Dave Jonesf82b2172008-08-11 09:30:23 +02002942 hlock->class_idx = 0;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002943 hlock->acquire_ip = 0;
2944 hlock->irq_context = 0;
2945#endif
2946 return 1;
2947}
2948
2949/*
2950 * Remove the lock to the list of currently held locks - this gets
2951 * called on mutex_unlock()/spin_unlock*() (or on a failed
2952 * mutex_lock_interruptible()). This is done for unlocks that nest
2953 * perfectly. (i.e. the current top of the lock-stack is unlocked)
2954 */
2955static void
2956__lock_release(struct lockdep_map *lock, int nested, unsigned long ip)
2957{
2958 struct task_struct *curr = current;
2959
2960 if (!check_unlock(curr, lock, ip))
2961 return;
2962
2963 if (nested) {
2964 if (!lock_release_nested(curr, lock, ip))
2965 return;
2966 } else {
2967 if (!lock_release_non_nested(curr, lock, ip))
2968 return;
2969 }
2970
2971 check_chain_key(curr);
2972}
2973
2974/*
2975 * Check whether we follow the irq-flags state precisely:
2976 */
Steven Rostedt1d09daa2008-05-12 21:20:55 +02002977static void check_flags(unsigned long flags)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002978{
Ingo Molnar992860e2008-07-14 10:28:38 +02002979#if defined(CONFIG_PROVE_LOCKING) && defined(CONFIG_DEBUG_LOCKDEP) && \
2980 defined(CONFIG_TRACE_IRQFLAGS)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002981 if (!debug_locks)
2982 return;
2983
Ingo Molnar5f9fa8a2007-12-07 19:02:47 +01002984 if (irqs_disabled_flags(flags)) {
2985 if (DEBUG_LOCKS_WARN_ON(current->hardirqs_enabled)) {
2986 printk("possible reason: unannotated irqs-off.\n");
2987 }
2988 } else {
2989 if (DEBUG_LOCKS_WARN_ON(!current->hardirqs_enabled)) {
2990 printk("possible reason: unannotated irqs-on.\n");
2991 }
2992 }
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002993
2994 /*
2995 * We dont accurately track softirq state in e.g.
2996 * hardirq contexts (such as on 4KSTACKS), so only
2997 * check if not in hardirq contexts:
2998 */
2999 if (!hardirq_count()) {
3000 if (softirq_count())
3001 DEBUG_LOCKS_WARN_ON(current->softirqs_enabled);
3002 else
3003 DEBUG_LOCKS_WARN_ON(!current->softirqs_enabled);
3004 }
3005
3006 if (!debug_locks)
3007 print_irqtrace_events(current);
3008#endif
3009}
3010
Peter Zijlstra00ef9f72008-12-04 09:00:17 +01003011void lock_set_class(struct lockdep_map *lock, const char *name,
3012 struct lock_class_key *key, unsigned int subclass,
3013 unsigned long ip)
Peter Zijlstra64aa3482008-08-11 09:30:21 +02003014{
3015 unsigned long flags;
3016
3017 if (unlikely(current->lockdep_recursion))
3018 return;
3019
3020 raw_local_irq_save(flags);
3021 current->lockdep_recursion = 1;
3022 check_flags(flags);
Peter Zijlstra00ef9f72008-12-04 09:00:17 +01003023 if (__lock_set_class(lock, name, key, subclass, ip))
Peter Zijlstra64aa3482008-08-11 09:30:21 +02003024 check_chain_key(current);
3025 current->lockdep_recursion = 0;
3026 raw_local_irq_restore(flags);
3027}
Peter Zijlstra00ef9f72008-12-04 09:00:17 +01003028EXPORT_SYMBOL_GPL(lock_set_class);
Peter Zijlstra64aa3482008-08-11 09:30:21 +02003029
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003030/*
3031 * We are not always called with irqs disabled - do that here,
3032 * and also avoid lockdep recursion:
3033 */
Steven Rostedt1d09daa2008-05-12 21:20:55 +02003034void lock_acquire(struct lockdep_map *lock, unsigned int subclass,
Peter Zijlstra7531e2f2008-08-11 09:30:24 +02003035 int trylock, int read, int check,
3036 struct lockdep_map *nest_lock, unsigned long ip)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003037{
3038 unsigned long flags;
3039
3040 if (unlikely(current->lockdep_recursion))
3041 return;
3042
3043 raw_local_irq_save(flags);
3044 check_flags(flags);
3045
3046 current->lockdep_recursion = 1;
3047 __lock_acquire(lock, subclass, trylock, read, check,
Peter Zijlstra7531e2f2008-08-11 09:30:24 +02003048 irqs_disabled_flags(flags), nest_lock, ip);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003049 current->lockdep_recursion = 0;
3050 raw_local_irq_restore(flags);
3051}
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003052EXPORT_SYMBOL_GPL(lock_acquire);
3053
Steven Rostedt1d09daa2008-05-12 21:20:55 +02003054void lock_release(struct lockdep_map *lock, int nested,
Steven Rostedt0764d232008-05-12 21:20:44 +02003055 unsigned long ip)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003056{
3057 unsigned long flags;
3058
3059 if (unlikely(current->lockdep_recursion))
3060 return;
3061
3062 raw_local_irq_save(flags);
3063 check_flags(flags);
3064 current->lockdep_recursion = 1;
3065 __lock_release(lock, nested, ip);
3066 current->lockdep_recursion = 0;
3067 raw_local_irq_restore(flags);
3068}
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003069EXPORT_SYMBOL_GPL(lock_release);
3070
Nick Piggincf40bd12009-01-21 08:12:39 +01003071void lockdep_set_current_reclaim_state(gfp_t gfp_mask)
3072{
3073 current->lockdep_reclaim_gfp = gfp_mask;
3074}
3075
3076void lockdep_clear_current_reclaim_state(void)
3077{
3078 current->lockdep_reclaim_gfp = 0;
3079}
3080
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003081#ifdef CONFIG_LOCK_STAT
3082static int
3083print_lock_contention_bug(struct task_struct *curr, struct lockdep_map *lock,
3084 unsigned long ip)
3085{
3086 if (!debug_locks_off())
3087 return 0;
3088 if (debug_locks_silent)
3089 return 0;
3090
3091 printk("\n=================================\n");
3092 printk( "[ BUG: bad contention detected! ]\n");
3093 printk( "---------------------------------\n");
3094 printk("%s/%d is trying to contend lock (",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07003095 curr->comm, task_pid_nr(curr));
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003096 print_lockdep_cache(lock);
3097 printk(") at:\n");
3098 print_ip_sym(ip);
3099 printk("but there are no locks held!\n");
3100 printk("\nother info that might help us debug this:\n");
3101 lockdep_print_held_locks(curr);
3102
3103 printk("\nstack backtrace:\n");
3104 dump_stack();
3105
3106 return 0;
3107}
3108
3109static void
3110__lock_contended(struct lockdep_map *lock, unsigned long ip)
3111{
3112 struct task_struct *curr = current;
3113 struct held_lock *hlock, *prev_hlock;
3114 struct lock_class_stats *stats;
3115 unsigned int depth;
Peter Zijlstrac7e78cf2008-10-16 23:17:09 +02003116 int i, contention_point, contending_point;
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003117
3118 depth = curr->lockdep_depth;
3119 if (DEBUG_LOCKS_WARN_ON(!depth))
3120 return;
3121
3122 prev_hlock = NULL;
3123 for (i = depth-1; i >= 0; i--) {
3124 hlock = curr->held_locks + i;
3125 /*
3126 * We must not cross into another context:
3127 */
3128 if (prev_hlock && prev_hlock->irq_context != hlock->irq_context)
3129 break;
3130 if (hlock->instance == lock)
3131 goto found_it;
3132 prev_hlock = hlock;
3133 }
3134 print_lock_contention_bug(curr, lock, ip);
3135 return;
3136
3137found_it:
3138 hlock->waittime_stamp = sched_clock();
3139
Peter Zijlstrac7e78cf2008-10-16 23:17:09 +02003140 contention_point = lock_point(hlock_class(hlock)->contention_point, ip);
3141 contending_point = lock_point(hlock_class(hlock)->contending_point,
3142 lock->ip);
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003143
Dave Jonesf82b2172008-08-11 09:30:23 +02003144 stats = get_lock_stats(hlock_class(hlock));
Peter Zijlstrac7e78cf2008-10-16 23:17:09 +02003145 if (contention_point < LOCKSTAT_POINTS)
3146 stats->contention_point[contention_point]++;
3147 if (contending_point < LOCKSTAT_POINTS)
3148 stats->contending_point[contending_point]++;
Peter Zijlstra96645672007-07-19 01:49:00 -07003149 if (lock->cpu != smp_processor_id())
3150 stats->bounces[bounce_contended + !!hlock->read]++;
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003151 put_lock_stats(stats);
3152}
3153
3154static void
Peter Zijlstrac7e78cf2008-10-16 23:17:09 +02003155__lock_acquired(struct lockdep_map *lock, unsigned long ip)
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003156{
3157 struct task_struct *curr = current;
3158 struct held_lock *hlock, *prev_hlock;
3159 struct lock_class_stats *stats;
3160 unsigned int depth;
3161 u64 now;
Peter Zijlstra96645672007-07-19 01:49:00 -07003162 s64 waittime = 0;
3163 int i, cpu;
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003164
3165 depth = curr->lockdep_depth;
3166 if (DEBUG_LOCKS_WARN_ON(!depth))
3167 return;
3168
3169 prev_hlock = NULL;
3170 for (i = depth-1; i >= 0; i--) {
3171 hlock = curr->held_locks + i;
3172 /*
3173 * We must not cross into another context:
3174 */
3175 if (prev_hlock && prev_hlock->irq_context != hlock->irq_context)
3176 break;
3177 if (hlock->instance == lock)
3178 goto found_it;
3179 prev_hlock = hlock;
3180 }
3181 print_lock_contention_bug(curr, lock, _RET_IP_);
3182 return;
3183
3184found_it:
Peter Zijlstra96645672007-07-19 01:49:00 -07003185 cpu = smp_processor_id();
3186 if (hlock->waittime_stamp) {
3187 now = sched_clock();
3188 waittime = now - hlock->waittime_stamp;
3189 hlock->holdtime_stamp = now;
3190 }
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003191
Dave Jonesf82b2172008-08-11 09:30:23 +02003192 stats = get_lock_stats(hlock_class(hlock));
Peter Zijlstra96645672007-07-19 01:49:00 -07003193 if (waittime) {
3194 if (hlock->read)
3195 lock_time_inc(&stats->read_waittime, waittime);
3196 else
3197 lock_time_inc(&stats->write_waittime, waittime);
3198 }
3199 if (lock->cpu != cpu)
3200 stats->bounces[bounce_acquired + !!hlock->read]++;
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003201 put_lock_stats(stats);
Peter Zijlstra96645672007-07-19 01:49:00 -07003202
3203 lock->cpu = cpu;
Peter Zijlstrac7e78cf2008-10-16 23:17:09 +02003204 lock->ip = ip;
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003205}
3206
3207void lock_contended(struct lockdep_map *lock, unsigned long ip)
3208{
3209 unsigned long flags;
3210
3211 if (unlikely(!lock_stat))
3212 return;
3213
3214 if (unlikely(current->lockdep_recursion))
3215 return;
3216
3217 raw_local_irq_save(flags);
3218 check_flags(flags);
3219 current->lockdep_recursion = 1;
3220 __lock_contended(lock, ip);
3221 current->lockdep_recursion = 0;
3222 raw_local_irq_restore(flags);
3223}
3224EXPORT_SYMBOL_GPL(lock_contended);
3225
Peter Zijlstrac7e78cf2008-10-16 23:17:09 +02003226void lock_acquired(struct lockdep_map *lock, unsigned long ip)
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003227{
3228 unsigned long flags;
3229
3230 if (unlikely(!lock_stat))
3231 return;
3232
3233 if (unlikely(current->lockdep_recursion))
3234 return;
3235
3236 raw_local_irq_save(flags);
3237 check_flags(flags);
3238 current->lockdep_recursion = 1;
Peter Zijlstrac7e78cf2008-10-16 23:17:09 +02003239 __lock_acquired(lock, ip);
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003240 current->lockdep_recursion = 0;
3241 raw_local_irq_restore(flags);
3242}
3243EXPORT_SYMBOL_GPL(lock_acquired);
3244#endif
3245
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003246/*
3247 * Used by the testsuite, sanitize the validator state
3248 * after a simulated failure:
3249 */
3250
3251void lockdep_reset(void)
3252{
3253 unsigned long flags;
Ingo Molnar23d95a02006-12-13 00:34:40 -08003254 int i;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003255
3256 raw_local_irq_save(flags);
3257 current->curr_chain_key = 0;
3258 current->lockdep_depth = 0;
3259 current->lockdep_recursion = 0;
3260 memset(current->held_locks, 0, MAX_LOCK_DEPTH*sizeof(struct held_lock));
3261 nr_hardirq_chains = 0;
3262 nr_softirq_chains = 0;
3263 nr_process_chains = 0;
3264 debug_locks = 1;
Ingo Molnar23d95a02006-12-13 00:34:40 -08003265 for (i = 0; i < CHAINHASH_SIZE; i++)
3266 INIT_LIST_HEAD(chainhash_table + i);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003267 raw_local_irq_restore(flags);
3268}
3269
3270static void zap_class(struct lock_class *class)
3271{
3272 int i;
3273
3274 /*
3275 * Remove all dependencies this lock is
3276 * involved in:
3277 */
3278 for (i = 0; i < nr_list_entries; i++) {
3279 if (list_entries[i].class == class)
3280 list_del_rcu(&list_entries[i].entry);
3281 }
3282 /*
3283 * Unhash the class and remove it from the all_lock_classes list:
3284 */
3285 list_del_rcu(&class->hash_entry);
3286 list_del_rcu(&class->lock_entry);
3287
Rabin Vincent8bfe0292008-08-11 09:30:26 +02003288 class->key = NULL;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003289}
3290
Arjan van de Venfabe8742008-01-24 07:00:45 +01003291static inline int within(const void *addr, void *start, unsigned long size)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003292{
3293 return addr >= start && addr < start + size;
3294}
3295
3296void lockdep_free_key_range(void *start, unsigned long size)
3297{
3298 struct lock_class *class, *next;
3299 struct list_head *head;
3300 unsigned long flags;
3301 int i;
Nick Piggin5a26db52008-01-16 09:51:58 +01003302 int locked;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003303
3304 raw_local_irq_save(flags);
Nick Piggin5a26db52008-01-16 09:51:58 +01003305 locked = graph_lock();
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003306
3307 /*
3308 * Unhash all classes that were created by this module:
3309 */
3310 for (i = 0; i < CLASSHASH_SIZE; i++) {
3311 head = classhash_table + i;
3312 if (list_empty(head))
3313 continue;
Arjan van de Venfabe8742008-01-24 07:00:45 +01003314 list_for_each_entry_safe(class, next, head, hash_entry) {
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003315 if (within(class->key, start, size))
3316 zap_class(class);
Arjan van de Venfabe8742008-01-24 07:00:45 +01003317 else if (within(class->name, start, size))
3318 zap_class(class);
3319 }
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003320 }
3321
Nick Piggin5a26db52008-01-16 09:51:58 +01003322 if (locked)
3323 graph_unlock();
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003324 raw_local_irq_restore(flags);
3325}
3326
3327void lockdep_reset_lock(struct lockdep_map *lock)
3328{
Ingo Molnard6d897c2006-07-10 04:44:04 -07003329 struct lock_class *class, *next;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003330 struct list_head *head;
3331 unsigned long flags;
3332 int i, j;
Nick Piggin5a26db52008-01-16 09:51:58 +01003333 int locked;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003334
3335 raw_local_irq_save(flags);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003336
3337 /*
Ingo Molnard6d897c2006-07-10 04:44:04 -07003338 * Remove all classes this lock might have:
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003339 */
Ingo Molnard6d897c2006-07-10 04:44:04 -07003340 for (j = 0; j < MAX_LOCKDEP_SUBCLASSES; j++) {
3341 /*
3342 * If the class exists we look it up and zap it:
3343 */
3344 class = look_up_lock_class(lock, j);
3345 if (class)
3346 zap_class(class);
3347 }
3348 /*
3349 * Debug check: in the end all mapped classes should
3350 * be gone.
3351 */
Nick Piggin5a26db52008-01-16 09:51:58 +01003352 locked = graph_lock();
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003353 for (i = 0; i < CLASSHASH_SIZE; i++) {
3354 head = classhash_table + i;
3355 if (list_empty(head))
3356 continue;
3357 list_for_each_entry_safe(class, next, head, hash_entry) {
Ingo Molnard6d897c2006-07-10 04:44:04 -07003358 if (unlikely(class == lock->class_cache)) {
Ingo Molnar74c383f2006-12-13 00:34:43 -08003359 if (debug_locks_off_graph_unlock())
3360 WARN_ON(1);
Ingo Molnard6d897c2006-07-10 04:44:04 -07003361 goto out_restore;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003362 }
3363 }
3364 }
Nick Piggin5a26db52008-01-16 09:51:58 +01003365 if (locked)
3366 graph_unlock();
Ingo Molnard6d897c2006-07-10 04:44:04 -07003367
3368out_restore:
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003369 raw_local_irq_restore(flags);
3370}
3371
Sam Ravnborg14999932007-02-28 20:12:31 -08003372void lockdep_init(void)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003373{
3374 int i;
3375
3376 /*
3377 * Some architectures have their own start_kernel()
3378 * code which calls lockdep_init(), while we also
3379 * call lockdep_init() from the start_kernel() itself,
3380 * and we want to initialize the hashes only once:
3381 */
3382 if (lockdep_initialized)
3383 return;
3384
3385 for (i = 0; i < CLASSHASH_SIZE; i++)
3386 INIT_LIST_HEAD(classhash_table + i);
3387
3388 for (i = 0; i < CHAINHASH_SIZE; i++)
3389 INIT_LIST_HEAD(chainhash_table + i);
3390
3391 lockdep_initialized = 1;
3392}
3393
3394void __init lockdep_info(void)
3395{
3396 printk("Lock dependency validator: Copyright (c) 2006 Red Hat, Inc., Ingo Molnar\n");
3397
Li Zefanb0788ca2008-11-21 15:57:32 +08003398 printk("... MAX_LOCKDEP_SUBCLASSES: %lu\n", MAX_LOCKDEP_SUBCLASSES);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003399 printk("... MAX_LOCK_DEPTH: %lu\n", MAX_LOCK_DEPTH);
3400 printk("... MAX_LOCKDEP_KEYS: %lu\n", MAX_LOCKDEP_KEYS);
Li Zefanb0788ca2008-11-21 15:57:32 +08003401 printk("... CLASSHASH_SIZE: %lu\n", CLASSHASH_SIZE);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003402 printk("... MAX_LOCKDEP_ENTRIES: %lu\n", MAX_LOCKDEP_ENTRIES);
3403 printk("... MAX_LOCKDEP_CHAINS: %lu\n", MAX_LOCKDEP_CHAINS);
3404 printk("... CHAINHASH_SIZE: %lu\n", CHAINHASH_SIZE);
3405
3406 printk(" memory used by lock dependency info: %lu kB\n",
3407 (sizeof(struct lock_class) * MAX_LOCKDEP_KEYS +
3408 sizeof(struct list_head) * CLASSHASH_SIZE +
3409 sizeof(struct lock_list) * MAX_LOCKDEP_ENTRIES +
3410 sizeof(struct lock_chain) * MAX_LOCKDEP_CHAINS +
3411 sizeof(struct list_head) * CHAINHASH_SIZE) / 1024);
3412
3413 printk(" per task-struct memory footprint: %lu bytes\n",
3414 sizeof(struct held_lock) * MAX_LOCK_DEPTH);
3415
3416#ifdef CONFIG_DEBUG_LOCKDEP
Johannes Bergc71063c2007-07-19 01:49:02 -07003417 if (lockdep_init_error) {
3418 printk("WARNING: lockdep init error! Arch code didn't call lockdep_init() early enough?\n");
3419 printk("Call stack leading to lockdep invocation was:\n");
3420 print_stack_trace(&lockdep_init_trace, 0);
3421 }
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003422#endif
3423}
3424
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003425static void
3426print_freed_lock_bug(struct task_struct *curr, const void *mem_from,
Arjan van de Ven55794a42006-07-10 04:44:03 -07003427 const void *mem_to, struct held_lock *hlock)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003428{
3429 if (!debug_locks_off())
3430 return;
3431 if (debug_locks_silent)
3432 return;
3433
3434 printk("\n=========================\n");
3435 printk( "[ BUG: held lock freed! ]\n");
3436 printk( "-------------------------\n");
3437 printk("%s/%d is freeing memory %p-%p, with a lock still held there!\n",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07003438 curr->comm, task_pid_nr(curr), mem_from, mem_to-1);
Arjan van de Ven55794a42006-07-10 04:44:03 -07003439 print_lock(hlock);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003440 lockdep_print_held_locks(curr);
3441
3442 printk("\nstack backtrace:\n");
3443 dump_stack();
3444}
3445
Oleg Nesterov54561782007-12-05 15:46:09 +01003446static inline int not_in_range(const void* mem_from, unsigned long mem_len,
3447 const void* lock_from, unsigned long lock_len)
3448{
3449 return lock_from + lock_len <= mem_from ||
3450 mem_from + mem_len <= lock_from;
3451}
3452
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003453/*
3454 * Called when kernel memory is freed (or unmapped), or if a lock
3455 * is destroyed or reinitialized - this code checks whether there is
3456 * any held lock in the memory range of <from> to <to>:
3457 */
3458void debug_check_no_locks_freed(const void *mem_from, unsigned long mem_len)
3459{
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003460 struct task_struct *curr = current;
3461 struct held_lock *hlock;
3462 unsigned long flags;
3463 int i;
3464
3465 if (unlikely(!debug_locks))
3466 return;
3467
3468 local_irq_save(flags);
3469 for (i = 0; i < curr->lockdep_depth; i++) {
3470 hlock = curr->held_locks + i;
3471
Oleg Nesterov54561782007-12-05 15:46:09 +01003472 if (not_in_range(mem_from, mem_len, hlock->instance,
3473 sizeof(*hlock->instance)))
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003474 continue;
3475
Oleg Nesterov54561782007-12-05 15:46:09 +01003476 print_freed_lock_bug(curr, mem_from, mem_from + mem_len, hlock);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003477 break;
3478 }
3479 local_irq_restore(flags);
3480}
Peter Zijlstraed075362006-12-06 20:35:24 -08003481EXPORT_SYMBOL_GPL(debug_check_no_locks_freed);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003482
3483static void print_held_locks_bug(struct task_struct *curr)
3484{
3485 if (!debug_locks_off())
3486 return;
3487 if (debug_locks_silent)
3488 return;
3489
3490 printk("\n=====================================\n");
3491 printk( "[ BUG: lock held at task exit time! ]\n");
3492 printk( "-------------------------------------\n");
3493 printk("%s/%d is exiting with locks still held!\n",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07003494 curr->comm, task_pid_nr(curr));
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003495 lockdep_print_held_locks(curr);
3496
3497 printk("\nstack backtrace:\n");
3498 dump_stack();
3499}
3500
3501void debug_check_no_locks_held(struct task_struct *task)
3502{
3503 if (unlikely(task->lockdep_depth > 0))
3504 print_held_locks_bug(task);
3505}
3506
3507void debug_show_all_locks(void)
3508{
3509 struct task_struct *g, *p;
3510 int count = 10;
3511 int unlock = 1;
3512
Jarek Poplawski9c35dd72007-03-22 00:11:28 -08003513 if (unlikely(!debug_locks)) {
3514 printk("INFO: lockdep is turned off.\n");
3515 return;
3516 }
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003517 printk("\nShowing all locks held in the system:\n");
3518
3519 /*
3520 * Here we try to get the tasklist_lock as hard as possible,
3521 * if not successful after 2 seconds we ignore it (but keep
3522 * trying). This is to enable a debug printout even if a
3523 * tasklist_lock-holding task deadlocks or crashes.
3524 */
3525retry:
3526 if (!read_trylock(&tasklist_lock)) {
3527 if (count == 10)
3528 printk("hm, tasklist_lock locked, retrying... ");
3529 if (count) {
3530 count--;
3531 printk(" #%d", 10-count);
3532 mdelay(200);
3533 goto retry;
3534 }
3535 printk(" ignoring it.\n");
3536 unlock = 0;
qinghuang feng46fec7a2008-10-28 17:24:28 +08003537 } else {
3538 if (count != 10)
3539 printk(KERN_CONT " locked it.\n");
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003540 }
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003541
3542 do_each_thread(g, p) {
Ingo Molnar85684872007-12-05 15:46:09 +01003543 /*
3544 * It's not reliable to print a task's held locks
3545 * if it's not sleeping (or if it's not the current
3546 * task):
3547 */
3548 if (p->state == TASK_RUNNING && p != current)
3549 continue;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003550 if (p->lockdep_depth)
3551 lockdep_print_held_locks(p);
3552 if (!unlock)
3553 if (read_trylock(&tasklist_lock))
3554 unlock = 1;
3555 } while_each_thread(g, p);
3556
3557 printk("\n");
3558 printk("=============================================\n\n");
3559
3560 if (unlock)
3561 read_unlock(&tasklist_lock);
3562}
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003563EXPORT_SYMBOL_GPL(debug_show_all_locks);
3564
Ingo Molnar82a1fcb2008-01-25 21:08:02 +01003565/*
3566 * Careful: only use this function if you are sure that
3567 * the task cannot run in parallel!
3568 */
3569void __debug_show_held_locks(struct task_struct *task)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003570{
Jarek Poplawski9c35dd72007-03-22 00:11:28 -08003571 if (unlikely(!debug_locks)) {
3572 printk("INFO: lockdep is turned off.\n");
3573 return;
3574 }
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003575 lockdep_print_held_locks(task);
3576}
Ingo Molnar82a1fcb2008-01-25 21:08:02 +01003577EXPORT_SYMBOL_GPL(__debug_show_held_locks);
3578
3579void debug_show_held_locks(struct task_struct *task)
3580{
3581 __debug_show_held_locks(task);
3582}
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003583EXPORT_SYMBOL_GPL(debug_show_held_locks);
Peter Zijlstrab351d162007-10-11 22:11:12 +02003584
3585void lockdep_sys_exit(void)
3586{
3587 struct task_struct *curr = current;
3588
3589 if (unlikely(curr->lockdep_depth)) {
3590 if (!debug_locks_off())
3591 return;
3592 printk("\n================================================\n");
3593 printk( "[ BUG: lock held when returning to user space! ]\n");
3594 printk( "------------------------------------------------\n");
3595 printk("%s/%d is leaving the kernel with locks still held!\n",
3596 curr->comm, curr->pid);
3597 lockdep_print_held_locks(curr);
3598 }
3599}