blob: c4582a6ea953476509ebeb30c11ce47813a1066f [file] [log] [blame]
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001/*
2 * kernel/lockdep.c
3 *
4 * Runtime locking correctness validator
5 *
6 * Started by Ingo Molnar:
7 *
Peter Zijlstra4b32d0a2007-07-19 01:48:59 -07008 * Copyright (C) 2006,2007 Red Hat, Inc., Ingo Molnar <mingo@redhat.com>
9 * Copyright (C) 2007 Red Hat, Inc., Peter Zijlstra <pzijlstr@redhat.com>
Ingo Molnarfbb9ce952006-07-03 00:24:50 -070010 *
11 * this code maps all the lock dependencies as they occur in a live kernel
12 * and will warn about the following classes of locking bugs:
13 *
14 * - lock inversion scenarios
15 * - circular lock dependencies
16 * - hardirq/softirq safe/unsafe locking bugs
17 *
18 * Bugs are reported even if the current locking scenario does not cause
19 * any deadlock at this point.
20 *
21 * I.e. if anytime in the past two locks were taken in a different order,
22 * even if it happened for another task, even if those were different
23 * locks (but of the same class as this lock), this code will detect it.
24 *
25 * Thanks to Arjan van de Ven for coming up with the initial idea of
26 * mapping lock dependencies runtime.
27 */
Steven Rostedta5e25882008-12-02 15:34:05 -050028#define DISABLE_BRANCH_PROFILING
Ingo Molnarfbb9ce952006-07-03 00:24:50 -070029#include <linux/mutex.h>
30#include <linux/sched.h>
31#include <linux/delay.h>
32#include <linux/module.h>
33#include <linux/proc_fs.h>
34#include <linux/seq_file.h>
35#include <linux/spinlock.h>
36#include <linux/kallsyms.h>
37#include <linux/interrupt.h>
38#include <linux/stacktrace.h>
39#include <linux/debug_locks.h>
40#include <linux/irqflags.h>
Dave Jones99de0552006-09-29 02:00:10 -070041#include <linux/utsname.h>
Peter Zijlstra4b32d0a2007-07-19 01:48:59 -070042#include <linux/hash.h>
Steven Rostedt81d68a92008-05-12 21:20:42 +020043#include <linux/ftrace.h>
Peter Zijlstrab4b136f2009-01-29 14:50:36 +010044#include <linux/stringify.h>
Peter Zijlstraefed7922009-03-04 12:32:55 +010045#include <trace/lockdep.h>
Ingo Molnarfbb9ce952006-07-03 00:24:50 -070046
47#include <asm/sections.h>
48
49#include "lockdep_internals.h"
50
Peter Zijlstraf20786f2007-07-19 01:48:56 -070051#ifdef CONFIG_PROVE_LOCKING
52int prove_locking = 1;
53module_param(prove_locking, int, 0644);
54#else
55#define prove_locking 0
56#endif
57
58#ifdef CONFIG_LOCK_STAT
59int lock_stat = 1;
60module_param(lock_stat, int, 0644);
61#else
62#define lock_stat 0
63#endif
64
Ingo Molnarfbb9ce952006-07-03 00:24:50 -070065/*
Ingo Molnar74c383f2006-12-13 00:34:43 -080066 * lockdep_lock: protects the lockdep graph, the hashes and the
67 * class/list/hash allocators.
Ingo Molnarfbb9ce952006-07-03 00:24:50 -070068 *
69 * This is one of the rare exceptions where it's justified
70 * to use a raw spinlock - we really dont want the spinlock
Ingo Molnar74c383f2006-12-13 00:34:43 -080071 * code to recurse back into the lockdep code...
Ingo Molnarfbb9ce952006-07-03 00:24:50 -070072 */
Ingo Molnar74c383f2006-12-13 00:34:43 -080073static raw_spinlock_t lockdep_lock = (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED;
74
75static int graph_lock(void)
76{
77 __raw_spin_lock(&lockdep_lock);
78 /*
79 * Make sure that if another CPU detected a bug while
80 * walking the graph we dont change it (while the other
81 * CPU is busy printing out stuff with the graph lock
82 * dropped already)
83 */
84 if (!debug_locks) {
85 __raw_spin_unlock(&lockdep_lock);
86 return 0;
87 }
Steven Rostedtbb065af2008-05-12 21:21:00 +020088 /* prevent any recursions within lockdep from causing deadlocks */
89 current->lockdep_recursion++;
Ingo Molnar74c383f2006-12-13 00:34:43 -080090 return 1;
91}
92
93static inline int graph_unlock(void)
94{
Jarek Poplawski381a2292007-02-10 01:44:58 -080095 if (debug_locks && !__raw_spin_is_locked(&lockdep_lock))
96 return DEBUG_LOCKS_WARN_ON(1);
97
Steven Rostedtbb065af2008-05-12 21:21:00 +020098 current->lockdep_recursion--;
Ingo Molnar74c383f2006-12-13 00:34:43 -080099 __raw_spin_unlock(&lockdep_lock);
100 return 0;
101}
102
103/*
104 * Turn lock debugging off and return with 0 if it was off already,
105 * and also release the graph lock:
106 */
107static inline int debug_locks_off_graph_unlock(void)
108{
109 int ret = debug_locks_off();
110
111 __raw_spin_unlock(&lockdep_lock);
112
113 return ret;
114}
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700115
116static int lockdep_initialized;
117
118unsigned long nr_list_entries;
119static struct lock_list list_entries[MAX_LOCKDEP_ENTRIES];
120
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700121/*
122 * All data structures here are protected by the global debug_lock.
123 *
124 * Mutex key structs only get allocated, once during bootup, and never
125 * get freed - this significantly simplifies the debugging code.
126 */
127unsigned long nr_lock_classes;
128static struct lock_class lock_classes[MAX_LOCKDEP_KEYS];
129
Dave Jonesf82b2172008-08-11 09:30:23 +0200130static inline struct lock_class *hlock_class(struct held_lock *hlock)
131{
132 if (!hlock->class_idx) {
133 DEBUG_LOCKS_WARN_ON(1);
134 return NULL;
135 }
136 return lock_classes + hlock->class_idx - 1;
137}
138
Peter Zijlstraf20786f2007-07-19 01:48:56 -0700139#ifdef CONFIG_LOCK_STAT
140static DEFINE_PER_CPU(struct lock_class_stats[MAX_LOCKDEP_KEYS], lock_stats);
141
Peter Zijlstrac7e78cf2008-10-16 23:17:09 +0200142static int lock_point(unsigned long points[], unsigned long ip)
Peter Zijlstraf20786f2007-07-19 01:48:56 -0700143{
144 int i;
145
Peter Zijlstrac7e78cf2008-10-16 23:17:09 +0200146 for (i = 0; i < LOCKSTAT_POINTS; i++) {
147 if (points[i] == 0) {
148 points[i] = ip;
Peter Zijlstraf20786f2007-07-19 01:48:56 -0700149 break;
150 }
Peter Zijlstrac7e78cf2008-10-16 23:17:09 +0200151 if (points[i] == ip)
Peter Zijlstraf20786f2007-07-19 01:48:56 -0700152 break;
153 }
154
155 return i;
156}
157
158static void lock_time_inc(struct lock_time *lt, s64 time)
159{
160 if (time > lt->max)
161 lt->max = time;
162
163 if (time < lt->min || !lt->min)
164 lt->min = time;
165
166 lt->total += time;
167 lt->nr++;
168}
169
Peter Zijlstrac46261d2007-07-19 01:48:57 -0700170static inline void lock_time_add(struct lock_time *src, struct lock_time *dst)
171{
172 dst->min += src->min;
173 dst->max += src->max;
174 dst->total += src->total;
175 dst->nr += src->nr;
176}
177
178struct lock_class_stats lock_stats(struct lock_class *class)
179{
180 struct lock_class_stats stats;
181 int cpu, i;
182
183 memset(&stats, 0, sizeof(struct lock_class_stats));
184 for_each_possible_cpu(cpu) {
185 struct lock_class_stats *pcs =
186 &per_cpu(lock_stats, cpu)[class - lock_classes];
187
188 for (i = 0; i < ARRAY_SIZE(stats.contention_point); i++)
189 stats.contention_point[i] += pcs->contention_point[i];
190
Peter Zijlstrac7e78cf2008-10-16 23:17:09 +0200191 for (i = 0; i < ARRAY_SIZE(stats.contending_point); i++)
192 stats.contending_point[i] += pcs->contending_point[i];
193
Peter Zijlstrac46261d2007-07-19 01:48:57 -0700194 lock_time_add(&pcs->read_waittime, &stats.read_waittime);
195 lock_time_add(&pcs->write_waittime, &stats.write_waittime);
196
197 lock_time_add(&pcs->read_holdtime, &stats.read_holdtime);
198 lock_time_add(&pcs->write_holdtime, &stats.write_holdtime);
Peter Zijlstra96645672007-07-19 01:49:00 -0700199
200 for (i = 0; i < ARRAY_SIZE(stats.bounces); i++)
201 stats.bounces[i] += pcs->bounces[i];
Peter Zijlstrac46261d2007-07-19 01:48:57 -0700202 }
203
204 return stats;
205}
206
207void clear_lock_stats(struct lock_class *class)
208{
209 int cpu;
210
211 for_each_possible_cpu(cpu) {
212 struct lock_class_stats *cpu_stats =
213 &per_cpu(lock_stats, cpu)[class - lock_classes];
214
215 memset(cpu_stats, 0, sizeof(struct lock_class_stats));
216 }
217 memset(class->contention_point, 0, sizeof(class->contention_point));
Peter Zijlstrac7e78cf2008-10-16 23:17:09 +0200218 memset(class->contending_point, 0, sizeof(class->contending_point));
Peter Zijlstrac46261d2007-07-19 01:48:57 -0700219}
220
Peter Zijlstraf20786f2007-07-19 01:48:56 -0700221static struct lock_class_stats *get_lock_stats(struct lock_class *class)
222{
223 return &get_cpu_var(lock_stats)[class - lock_classes];
224}
225
226static void put_lock_stats(struct lock_class_stats *stats)
227{
228 put_cpu_var(lock_stats);
229}
230
231static void lock_release_holdtime(struct held_lock *hlock)
232{
233 struct lock_class_stats *stats;
234 s64 holdtime;
235
236 if (!lock_stat)
237 return;
238
239 holdtime = sched_clock() - hlock->holdtime_stamp;
240
Dave Jonesf82b2172008-08-11 09:30:23 +0200241 stats = get_lock_stats(hlock_class(hlock));
Peter Zijlstraf20786f2007-07-19 01:48:56 -0700242 if (hlock->read)
243 lock_time_inc(&stats->read_holdtime, holdtime);
244 else
245 lock_time_inc(&stats->write_holdtime, holdtime);
246 put_lock_stats(stats);
247}
248#else
249static inline void lock_release_holdtime(struct held_lock *hlock)
250{
251}
252#endif
253
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700254/*
255 * We keep a global list of all lock classes. The list only grows,
256 * never shrinks. The list is only accessed with the lockdep
257 * spinlock lock held.
258 */
259LIST_HEAD(all_lock_classes);
260
261/*
262 * The lockdep classes are in a hash-table as well, for fast lookup:
263 */
264#define CLASSHASH_BITS (MAX_LOCKDEP_KEYS_BITS - 1)
265#define CLASSHASH_SIZE (1UL << CLASSHASH_BITS)
Peter Zijlstra4b32d0a2007-07-19 01:48:59 -0700266#define __classhashfn(key) hash_long((unsigned long)key, CLASSHASH_BITS)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700267#define classhashentry(key) (classhash_table + __classhashfn((key)))
268
269static struct list_head classhash_table[CLASSHASH_SIZE];
270
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700271/*
272 * We put the lock dependency chains into a hash-table as well, to cache
273 * their existence:
274 */
275#define CHAINHASH_BITS (MAX_LOCKDEP_CHAINS_BITS-1)
276#define CHAINHASH_SIZE (1UL << CHAINHASH_BITS)
Peter Zijlstra4b32d0a2007-07-19 01:48:59 -0700277#define __chainhashfn(chain) hash_long(chain, CHAINHASH_BITS)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700278#define chainhashentry(chain) (chainhash_table + __chainhashfn((chain)))
279
280static struct list_head chainhash_table[CHAINHASH_SIZE];
281
282/*
283 * The hash key of the lock dependency chains is a hash itself too:
284 * it's a hash of all locks taken up to that lock, including that lock.
285 * It's a 64-bit hash, because it's important for the keys to be
286 * unique.
287 */
288#define iterate_chain_key(key1, key2) \
Ingo Molnar03cbc352006-09-29 02:01:46 -0700289 (((key1) << MAX_LOCKDEP_KEYS_BITS) ^ \
290 ((key1) >> (64-MAX_LOCKDEP_KEYS_BITS)) ^ \
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700291 (key2))
292
Steven Rostedt1d09daa2008-05-12 21:20:55 +0200293void lockdep_off(void)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700294{
295 current->lockdep_recursion++;
296}
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700297EXPORT_SYMBOL(lockdep_off);
298
Steven Rostedt1d09daa2008-05-12 21:20:55 +0200299void lockdep_on(void)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700300{
301 current->lockdep_recursion--;
302}
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700303EXPORT_SYMBOL(lockdep_on);
304
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700305/*
306 * Debugging switches:
307 */
308
309#define VERBOSE 0
Ingo Molnar33e94e92006-12-13 00:34:41 -0800310#define VERY_VERBOSE 0
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700311
312#if VERBOSE
313# define HARDIRQ_VERBOSE 1
314# define SOFTIRQ_VERBOSE 1
Nick Piggincf40bd12009-01-21 08:12:39 +0100315# define RECLAIM_VERBOSE 1
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700316#else
317# define HARDIRQ_VERBOSE 0
318# define SOFTIRQ_VERBOSE 0
Nick Piggincf40bd12009-01-21 08:12:39 +0100319# define RECLAIM_VERBOSE 0
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700320#endif
321
Nick Piggincf40bd12009-01-21 08:12:39 +0100322#if VERBOSE || HARDIRQ_VERBOSE || SOFTIRQ_VERBOSE || RECLAIM_VERBOSE
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700323/*
324 * Quick filtering for interesting events:
325 */
326static int class_filter(struct lock_class *class)
327{
Andi Kleenf9829cc2006-07-10 04:44:01 -0700328#if 0
329 /* Example */
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700330 if (class->name_version == 1 &&
Andi Kleenf9829cc2006-07-10 04:44:01 -0700331 !strcmp(class->name, "lockname"))
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700332 return 1;
333 if (class->name_version == 1 &&
Andi Kleenf9829cc2006-07-10 04:44:01 -0700334 !strcmp(class->name, "&struct->lockfield"))
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700335 return 1;
Andi Kleenf9829cc2006-07-10 04:44:01 -0700336#endif
Ingo Molnara6640892006-12-13 00:34:39 -0800337 /* Filter everything else. 1 would be to allow everything else */
338 return 0;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700339}
340#endif
341
342static int verbose(struct lock_class *class)
343{
344#if VERBOSE
345 return class_filter(class);
346#endif
347 return 0;
348}
349
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700350/*
351 * Stack-trace: tightly packed array of stack backtrace
Ingo Molnar74c383f2006-12-13 00:34:43 -0800352 * addresses. Protected by the graph_lock.
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700353 */
354unsigned long nr_stack_trace_entries;
355static unsigned long stack_trace[MAX_STACK_TRACE_ENTRIES];
356
357static int save_trace(struct stack_trace *trace)
358{
359 trace->nr_entries = 0;
360 trace->max_entries = MAX_STACK_TRACE_ENTRIES - nr_stack_trace_entries;
361 trace->entries = stack_trace + nr_stack_trace_entries;
362
Andi Kleen5a1b3992006-09-26 10:52:34 +0200363 trace->skip = 3;
Andi Kleen5a1b3992006-09-26 10:52:34 +0200364
Christoph Hellwigab1b6f02007-05-08 00:23:29 -0700365 save_stack_trace(trace);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700366
367 trace->max_entries = trace->nr_entries;
368
369 nr_stack_trace_entries += trace->nr_entries;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700370
371 if (nr_stack_trace_entries == MAX_STACK_TRACE_ENTRIES) {
Ingo Molnar74c383f2006-12-13 00:34:43 -0800372 if (!debug_locks_off_graph_unlock())
373 return 0;
374
375 printk("BUG: MAX_STACK_TRACE_ENTRIES too low!\n");
376 printk("turning off the locking correctness validator.\n");
377 dump_stack();
378
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700379 return 0;
380 }
381
382 return 1;
383}
384
385unsigned int nr_hardirq_chains;
386unsigned int nr_softirq_chains;
387unsigned int nr_process_chains;
388unsigned int max_lockdep_depth;
389unsigned int max_recursion_depth;
390
David Miller419ca3f2008-07-29 21:45:03 -0700391static unsigned int lockdep_dependency_gen_id;
392
393static bool lockdep_dependency_visit(struct lock_class *source,
394 unsigned int depth)
395{
396 if (!depth)
397 lockdep_dependency_gen_id++;
398 if (source->dep_gen_id == lockdep_dependency_gen_id)
399 return true;
400 source->dep_gen_id = lockdep_dependency_gen_id;
401 return false;
402}
403
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700404#ifdef CONFIG_DEBUG_LOCKDEP
405/*
406 * We cannot printk in early bootup code. Not even early_printk()
407 * might work. So we mark any initialization errors and printk
408 * about it later on, in lockdep_info().
409 */
410static int lockdep_init_error;
Johannes Bergc71063c2007-07-19 01:49:02 -0700411static unsigned long lockdep_init_trace_data[20];
412static struct stack_trace lockdep_init_trace = {
413 .max_entries = ARRAY_SIZE(lockdep_init_trace_data),
414 .entries = lockdep_init_trace_data,
415};
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700416
417/*
418 * Various lockdep statistics:
419 */
420atomic_t chain_lookup_hits;
421atomic_t chain_lookup_misses;
422atomic_t hardirqs_on_events;
423atomic_t hardirqs_off_events;
424atomic_t redundant_hardirqs_on;
425atomic_t redundant_hardirqs_off;
426atomic_t softirqs_on_events;
427atomic_t softirqs_off_events;
428atomic_t redundant_softirqs_on;
429atomic_t redundant_softirqs_off;
430atomic_t nr_unused_locks;
431atomic_t nr_cyclic_checks;
432atomic_t nr_cyclic_check_recursions;
433atomic_t nr_find_usage_forwards_checks;
434atomic_t nr_find_usage_forwards_recursions;
435atomic_t nr_find_usage_backwards_checks;
436atomic_t nr_find_usage_backwards_recursions;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700437#endif
438
439/*
440 * Locking printouts:
441 */
442
Peter Zijlstrafabe9c42009-01-22 14:51:01 +0100443#define __USAGE(__STATE) \
Peter Zijlstrab4b136f2009-01-29 14:50:36 +0100444 [LOCK_USED_IN_##__STATE] = "IN-"__stringify(__STATE)"-W", \
445 [LOCK_ENABLED_##__STATE] = __stringify(__STATE)"-ON-W", \
446 [LOCK_USED_IN_##__STATE##_READ] = "IN-"__stringify(__STATE)"-R",\
447 [LOCK_ENABLED_##__STATE##_READ] = __stringify(__STATE)"-ON-R",
Peter Zijlstrafabe9c42009-01-22 14:51:01 +0100448
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700449static const char *usage_str[] =
450{
Peter Zijlstrafabe9c42009-01-22 14:51:01 +0100451#define LOCKDEP_STATE(__STATE) __USAGE(__STATE)
452#include "lockdep_states.h"
453#undef LOCKDEP_STATE
454 [LOCK_USED] = "INITIAL USE",
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700455};
456
457const char * __get_key_name(struct lockdep_subclass_key *key, char *str)
458{
Alexey Dobriyanffb45122007-05-08 00:28:41 -0700459 return kallsyms_lookup((unsigned long)key, NULL, NULL, NULL, str);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700460}
461
Peter Zijlstra3ff176c2009-01-22 17:40:42 +0100462static inline unsigned long lock_flag(enum lock_usage_bit bit)
463{
464 return 1UL << bit;
465}
466
467static char get_usage_char(struct lock_class *class, enum lock_usage_bit bit)
468{
469 char c = '.';
470
471 if (class->usage_mask & lock_flag(bit + 2))
472 c = '+';
473 if (class->usage_mask & lock_flag(bit)) {
474 c = '-';
475 if (class->usage_mask & lock_flag(bit + 2))
476 c = '?';
477 }
478
479 return c;
480}
481
Peter Zijlstraf510b232009-01-22 17:53:47 +0100482void get_usage_chars(struct lock_class *class, char usage[LOCK_USAGE_CHARS])
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700483{
Peter Zijlstraf510b232009-01-22 17:53:47 +0100484 int i = 0;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700485
Peter Zijlstraf510b232009-01-22 17:53:47 +0100486#define LOCKDEP_STATE(__STATE) \
487 usage[i++] = get_usage_char(class, LOCK_USED_IN_##__STATE); \
488 usage[i++] = get_usage_char(class, LOCK_USED_IN_##__STATE##_READ);
489#include "lockdep_states.h"
490#undef LOCKDEP_STATE
491
492 usage[i] = '\0';
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700493}
494
495static void print_lock_name(struct lock_class *class)
496{
Peter Zijlstraf510b232009-01-22 17:53:47 +0100497 char str[KSYM_NAME_LEN], usage[LOCK_USAGE_CHARS];
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700498 const char *name;
499
Peter Zijlstraf510b232009-01-22 17:53:47 +0100500 get_usage_chars(class, usage);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700501
502 name = class->name;
503 if (!name) {
504 name = __get_key_name(class->key, str);
505 printk(" (%s", name);
506 } else {
507 printk(" (%s", name);
508 if (class->name_version > 1)
509 printk("#%d", class->name_version);
510 if (class->subclass)
511 printk("/%d", class->subclass);
512 }
Peter Zijlstraf510b232009-01-22 17:53:47 +0100513 printk("){%s}", usage);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700514}
515
516static void print_lockdep_cache(struct lockdep_map *lock)
517{
518 const char *name;
Tejun Heo9281ace2007-07-17 04:03:51 -0700519 char str[KSYM_NAME_LEN];
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700520
521 name = lock->name;
522 if (!name)
523 name = __get_key_name(lock->key->subkeys, str);
524
525 printk("%s", name);
526}
527
528static void print_lock(struct held_lock *hlock)
529{
Dave Jonesf82b2172008-08-11 09:30:23 +0200530 print_lock_name(hlock_class(hlock));
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700531 printk(", at: ");
532 print_ip_sym(hlock->acquire_ip);
533}
534
535static void lockdep_print_held_locks(struct task_struct *curr)
536{
537 int i, depth = curr->lockdep_depth;
538
539 if (!depth) {
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -0700540 printk("no locks held by %s/%d.\n", curr->comm, task_pid_nr(curr));
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700541 return;
542 }
543 printk("%d lock%s held by %s/%d:\n",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -0700544 depth, depth > 1 ? "s" : "", curr->comm, task_pid_nr(curr));
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700545
546 for (i = 0; i < depth; i++) {
547 printk(" #%d: ", i);
548 print_lock(curr->held_locks + i);
549 }
550}
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700551
552static void print_lock_class_header(struct lock_class *class, int depth)
553{
554 int bit;
555
Andi Kleenf9829cc2006-07-10 04:44:01 -0700556 printk("%*s->", depth, "");
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700557 print_lock_name(class);
558 printk(" ops: %lu", class->ops);
559 printk(" {\n");
560
561 for (bit = 0; bit < LOCK_USAGE_STATES; bit++) {
562 if (class->usage_mask & (1 << bit)) {
563 int len = depth;
564
Andi Kleenf9829cc2006-07-10 04:44:01 -0700565 len += printk("%*s %s", depth, "", usage_str[bit]);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700566 len += printk(" at:\n");
567 print_stack_trace(class->usage_traces + bit, len);
568 }
569 }
Andi Kleenf9829cc2006-07-10 04:44:01 -0700570 printk("%*s }\n", depth, "");
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700571
Andi Kleenf9829cc2006-07-10 04:44:01 -0700572 printk("%*s ... key at: ",depth,"");
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700573 print_ip_sym((unsigned long)class->key);
574}
575
576/*
577 * printk all lock dependencies starting at <entry>:
578 */
Ingo Molnar7807faf2008-11-25 08:44:24 +0100579static void __used
580print_lock_dependencies(struct lock_class *class, int depth)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700581{
582 struct lock_list *entry;
583
David Miller419ca3f2008-07-29 21:45:03 -0700584 if (lockdep_dependency_visit(class, depth))
585 return;
586
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700587 if (DEBUG_LOCKS_WARN_ON(depth >= 20))
588 return;
589
590 print_lock_class_header(class, depth);
591
592 list_for_each_entry(entry, &class->locks_after, entry) {
Jarek Poplawskib23984d2006-12-06 20:36:23 -0800593 if (DEBUG_LOCKS_WARN_ON(!entry->class))
594 return;
595
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700596 print_lock_dependencies(entry->class, depth + 1);
597
Andi Kleenf9829cc2006-07-10 04:44:01 -0700598 printk("%*s ... acquired at:\n",depth,"");
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700599 print_stack_trace(&entry->trace, 2);
600 printk("\n");
601 }
602}
603
Dave Jones99de0552006-09-29 02:00:10 -0700604static void print_kernel_version(void)
605{
Serge E. Hallyn96b644b2006-10-02 02:18:13 -0700606 printk("%s %.*s\n", init_utsname()->release,
607 (int)strcspn(init_utsname()->version, " "),
608 init_utsname()->version);
Dave Jones99de0552006-09-29 02:00:10 -0700609}
610
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700611static int very_verbose(struct lock_class *class)
612{
613#if VERY_VERBOSE
614 return class_filter(class);
615#endif
616 return 0;
617}
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700618
619/*
620 * Is this the address of a static object:
621 */
622static int static_obj(void *obj)
623{
624 unsigned long start = (unsigned long) &_stext,
625 end = (unsigned long) &_end,
626 addr = (unsigned long) obj;
627#ifdef CONFIG_SMP
628 int i;
629#endif
630
631 /*
632 * static variable?
633 */
634 if ((addr >= start) && (addr < end))
635 return 1;
636
637#ifdef CONFIG_SMP
638 /*
639 * percpu var?
640 */
641 for_each_possible_cpu(i) {
642 start = (unsigned long) &__per_cpu_start + per_cpu_offset(i);
Ingo Molnar1ff56832006-11-17 19:57:22 +0100643 end = (unsigned long) &__per_cpu_start + PERCPU_ENOUGH_ROOM
644 + per_cpu_offset(i);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700645
646 if ((addr >= start) && (addr < end))
647 return 1;
648 }
649#endif
650
651 /*
652 * module var?
653 */
654 return is_module_address(addr);
655}
656
657/*
658 * To make lock name printouts unique, we calculate a unique
659 * class->name_version generation counter:
660 */
661static int count_matching_names(struct lock_class *new_class)
662{
663 struct lock_class *class;
664 int count = 0;
665
666 if (!new_class->name)
667 return 0;
668
669 list_for_each_entry(class, &all_lock_classes, lock_entry) {
670 if (new_class->key - new_class->subclass == class->key)
671 return class->name_version;
672 if (class->name && !strcmp(class->name, new_class->name))
673 count = max(count, class->name_version);
674 }
675
676 return count + 1;
677}
678
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700679/*
680 * Register a lock's class in the hash-table, if the class is not present
681 * yet. Otherwise we look it up. We cache the result in the lock object
682 * itself, so actual lookup of the hash should be once per lock object.
683 */
684static inline struct lock_class *
Ingo Molnard6d897c2006-07-10 04:44:04 -0700685look_up_lock_class(struct lockdep_map *lock, unsigned int subclass)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700686{
687 struct lockdep_subclass_key *key;
688 struct list_head *hash_head;
689 struct lock_class *class;
690
691#ifdef CONFIG_DEBUG_LOCKDEP
692 /*
693 * If the architecture calls into lockdep before initializing
694 * the hashes then we'll warn about it later. (we cannot printk
695 * right now)
696 */
697 if (unlikely(!lockdep_initialized)) {
698 lockdep_init();
699 lockdep_init_error = 1;
Johannes Bergc71063c2007-07-19 01:49:02 -0700700 save_stack_trace(&lockdep_init_trace);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700701 }
702#endif
703
704 /*
705 * Static locks do not have their class-keys yet - for them the key
706 * is the lock object itself:
707 */
708 if (unlikely(!lock->key))
709 lock->key = (void *)lock;
710
711 /*
712 * NOTE: the class-key must be unique. For dynamic locks, a static
713 * lock_class_key variable is passed in through the mutex_init()
714 * (or spin_lock_init()) call - which acts as the key. For static
715 * locks we use the lock object itself as the key.
716 */
Peter Zijlstra4b32d0a2007-07-19 01:48:59 -0700717 BUILD_BUG_ON(sizeof(struct lock_class_key) >
718 sizeof(struct lockdep_map));
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700719
720 key = lock->key->subkeys + subclass;
721
722 hash_head = classhashentry(key);
723
724 /*
725 * We can walk the hash lockfree, because the hash only
726 * grows, and we are careful when adding entries to the end:
727 */
Peter Zijlstra4b32d0a2007-07-19 01:48:59 -0700728 list_for_each_entry(class, hash_head, hash_entry) {
729 if (class->key == key) {
730 WARN_ON_ONCE(class->name != lock->name);
Ingo Molnard6d897c2006-07-10 04:44:04 -0700731 return class;
Peter Zijlstra4b32d0a2007-07-19 01:48:59 -0700732 }
733 }
Ingo Molnard6d897c2006-07-10 04:44:04 -0700734
735 return NULL;
736}
737
738/*
739 * Register a lock's class in the hash-table, if the class is not present
740 * yet. Otherwise we look it up. We cache the result in the lock object
741 * itself, so actual lookup of the hash should be once per lock object.
742 */
743static inline struct lock_class *
Peter Zijlstra4dfbb9d2006-10-11 01:45:14 -0400744register_lock_class(struct lockdep_map *lock, unsigned int subclass, int force)
Ingo Molnard6d897c2006-07-10 04:44:04 -0700745{
746 struct lockdep_subclass_key *key;
747 struct list_head *hash_head;
748 struct lock_class *class;
Ingo Molnar70e45062006-12-06 20:40:50 -0800749 unsigned long flags;
Ingo Molnard6d897c2006-07-10 04:44:04 -0700750
751 class = look_up_lock_class(lock, subclass);
752 if (likely(class))
753 return class;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700754
755 /*
756 * Debug-check: all keys must be persistent!
757 */
758 if (!static_obj(lock->key)) {
759 debug_locks_off();
760 printk("INFO: trying to register non-static key.\n");
761 printk("the code is fine but needs lockdep annotation.\n");
762 printk("turning off the locking correctness validator.\n");
763 dump_stack();
764
765 return NULL;
766 }
767
Ingo Molnard6d897c2006-07-10 04:44:04 -0700768 key = lock->key->subkeys + subclass;
769 hash_head = classhashentry(key);
770
Ingo Molnar70e45062006-12-06 20:40:50 -0800771 raw_local_irq_save(flags);
Ingo Molnar74c383f2006-12-13 00:34:43 -0800772 if (!graph_lock()) {
773 raw_local_irq_restore(flags);
774 return NULL;
775 }
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700776 /*
777 * We have to do the hash-walk again, to avoid races
778 * with another CPU:
779 */
780 list_for_each_entry(class, hash_head, hash_entry)
781 if (class->key == key)
782 goto out_unlock_set;
783 /*
784 * Allocate a new key from the static array, and add it to
785 * the hash:
786 */
787 if (nr_lock_classes >= MAX_LOCKDEP_KEYS) {
Ingo Molnar74c383f2006-12-13 00:34:43 -0800788 if (!debug_locks_off_graph_unlock()) {
789 raw_local_irq_restore(flags);
790 return NULL;
791 }
Ingo Molnar70e45062006-12-06 20:40:50 -0800792 raw_local_irq_restore(flags);
Ingo Molnar74c383f2006-12-13 00:34:43 -0800793
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700794 printk("BUG: MAX_LOCKDEP_KEYS too low!\n");
795 printk("turning off the locking correctness validator.\n");
Peter Zijlstraeedeeab2009-03-18 12:38:47 +0100796 dump_stack();
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700797 return NULL;
798 }
799 class = lock_classes + nr_lock_classes++;
800 debug_atomic_inc(&nr_unused_locks);
801 class->key = key;
802 class->name = lock->name;
803 class->subclass = subclass;
804 INIT_LIST_HEAD(&class->lock_entry);
805 INIT_LIST_HEAD(&class->locks_before);
806 INIT_LIST_HEAD(&class->locks_after);
807 class->name_version = count_matching_names(class);
808 /*
809 * We use RCU's safe list-add method to make
810 * parallel walking of the hash-list safe:
811 */
812 list_add_tail_rcu(&class->hash_entry, hash_head);
Dale Farnsworth14811972008-02-25 23:03:02 +0100813 /*
814 * Add it to the global list of classes:
815 */
816 list_add_tail_rcu(&class->lock_entry, &all_lock_classes);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700817
818 if (verbose(class)) {
Ingo Molnar74c383f2006-12-13 00:34:43 -0800819 graph_unlock();
Ingo Molnar70e45062006-12-06 20:40:50 -0800820 raw_local_irq_restore(flags);
Ingo Molnar74c383f2006-12-13 00:34:43 -0800821
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700822 printk("\nnew class %p: %s", class->key, class->name);
823 if (class->name_version > 1)
824 printk("#%d", class->name_version);
825 printk("\n");
826 dump_stack();
Ingo Molnar74c383f2006-12-13 00:34:43 -0800827
Ingo Molnar70e45062006-12-06 20:40:50 -0800828 raw_local_irq_save(flags);
Ingo Molnar74c383f2006-12-13 00:34:43 -0800829 if (!graph_lock()) {
830 raw_local_irq_restore(flags);
831 return NULL;
832 }
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700833 }
834out_unlock_set:
Ingo Molnar74c383f2006-12-13 00:34:43 -0800835 graph_unlock();
Ingo Molnar70e45062006-12-06 20:40:50 -0800836 raw_local_irq_restore(flags);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700837
Peter Zijlstra4dfbb9d2006-10-11 01:45:14 -0400838 if (!subclass || force)
Ingo Molnard6d897c2006-07-10 04:44:04 -0700839 lock->class_cache = class;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700840
Jarek Poplawski381a2292007-02-10 01:44:58 -0800841 if (DEBUG_LOCKS_WARN_ON(class->subclass != subclass))
842 return NULL;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700843
844 return class;
845}
846
Peter Zijlstraca58abc2007-07-19 01:48:53 -0700847#ifdef CONFIG_PROVE_LOCKING
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700848/*
Peter Zijlstra8e182572007-07-19 01:48:54 -0700849 * Allocate a lockdep entry. (assumes the graph_lock held, returns
850 * with NULL on failure)
851 */
852static struct lock_list *alloc_list_entry(void)
853{
854 if (nr_list_entries >= MAX_LOCKDEP_ENTRIES) {
855 if (!debug_locks_off_graph_unlock())
856 return NULL;
857
858 printk("BUG: MAX_LOCKDEP_ENTRIES too low!\n");
859 printk("turning off the locking correctness validator.\n");
Peter Zijlstraeedeeab2009-03-18 12:38:47 +0100860 dump_stack();
Peter Zijlstra8e182572007-07-19 01:48:54 -0700861 return NULL;
862 }
863 return list_entries + nr_list_entries++;
864}
865
866/*
867 * Add a new dependency to the head of the list:
868 */
869static int add_lock_to_list(struct lock_class *class, struct lock_class *this,
870 struct list_head *head, unsigned long ip, int distance)
871{
872 struct lock_list *entry;
873 /*
874 * Lock not present yet - get a new dependency struct and
875 * add it to the list:
876 */
877 entry = alloc_list_entry();
878 if (!entry)
879 return 0;
880
Peter Zijlstra8e182572007-07-19 01:48:54 -0700881 if (!save_trace(&entry->trace))
882 return 0;
883
Zhu Yi74870172008-08-27 14:33:00 +0800884 entry->class = this;
885 entry->distance = distance;
Peter Zijlstra8e182572007-07-19 01:48:54 -0700886 /*
887 * Since we never remove from the dependency list, the list can
888 * be walked lockless by other CPUs, it's only allocation
889 * that must be protected by the spinlock. But this also means
890 * we must make new entries visible only once writes to the
891 * entry become visible - hence the RCU op:
892 */
893 list_add_tail_rcu(&entry->entry, head);
894
895 return 1;
896}
897
898/*
899 * Recursive, forwards-direction lock-dependency checking, used for
900 * both noncyclic checking and for hardirq-unsafe/softirq-unsafe
901 * checking.
902 *
903 * (to keep the stackframe of the recursive functions small we
904 * use these global variables, and we also mark various helper
905 * functions as noinline.)
906 */
907static struct held_lock *check_source, *check_target;
908
909/*
910 * Print a dependency chain entry (this is only done when a deadlock
911 * has been detected):
912 */
913static noinline int
914print_circular_bug_entry(struct lock_list *target, unsigned int depth)
915{
916 if (debug_locks_silent)
917 return 0;
918 printk("\n-> #%u", depth);
919 print_lock_name(target->class);
920 printk(":\n");
921 print_stack_trace(&target->trace, 6);
922
923 return 0;
924}
925
926/*
927 * When a circular dependency is detected, print the
928 * header first:
929 */
930static noinline int
931print_circular_bug_header(struct lock_list *entry, unsigned int depth)
932{
933 struct task_struct *curr = current;
934
935 if (!debug_locks_off_graph_unlock() || debug_locks_silent)
936 return 0;
937
938 printk("\n=======================================================\n");
939 printk( "[ INFO: possible circular locking dependency detected ]\n");
940 print_kernel_version();
941 printk( "-------------------------------------------------------\n");
942 printk("%s/%d is trying to acquire lock:\n",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -0700943 curr->comm, task_pid_nr(curr));
Peter Zijlstra8e182572007-07-19 01:48:54 -0700944 print_lock(check_source);
945 printk("\nbut task is already holding lock:\n");
946 print_lock(check_target);
947 printk("\nwhich lock already depends on the new lock.\n\n");
948 printk("\nthe existing dependency chain (in reverse order) is:\n");
949
950 print_circular_bug_entry(entry, depth);
951
952 return 0;
953}
954
955static noinline int print_circular_bug_tail(void)
956{
957 struct task_struct *curr = current;
958 struct lock_list this;
959
960 if (debug_locks_silent)
961 return 0;
962
Dave Jonesf82b2172008-08-11 09:30:23 +0200963 this.class = hlock_class(check_source);
Peter Zijlstra8e182572007-07-19 01:48:54 -0700964 if (!save_trace(&this.trace))
965 return 0;
966
967 print_circular_bug_entry(&this, 0);
968
969 printk("\nother info that might help us debug this:\n\n");
970 lockdep_print_held_locks(curr);
971
972 printk("\nstack backtrace:\n");
973 dump_stack();
974
975 return 0;
976}
977
978#define RECURSION_LIMIT 40
979
980static int noinline print_infinite_recursion_bug(void)
981{
982 if (!debug_locks_off_graph_unlock())
983 return 0;
984
985 WARN_ON(1);
986
987 return 0;
988}
989
David Miller419ca3f2008-07-29 21:45:03 -0700990unsigned long __lockdep_count_forward_deps(struct lock_class *class,
991 unsigned int depth)
992{
993 struct lock_list *entry;
994 unsigned long ret = 1;
995
996 if (lockdep_dependency_visit(class, depth))
997 return 0;
998
999 /*
1000 * Recurse this class's dependency list:
1001 */
1002 list_for_each_entry(entry, &class->locks_after, entry)
1003 ret += __lockdep_count_forward_deps(entry->class, depth + 1);
1004
1005 return ret;
1006}
1007
1008unsigned long lockdep_count_forward_deps(struct lock_class *class)
1009{
1010 unsigned long ret, flags;
1011
1012 local_irq_save(flags);
1013 __raw_spin_lock(&lockdep_lock);
1014 ret = __lockdep_count_forward_deps(class, 0);
1015 __raw_spin_unlock(&lockdep_lock);
1016 local_irq_restore(flags);
1017
1018 return ret;
1019}
1020
1021unsigned long __lockdep_count_backward_deps(struct lock_class *class,
1022 unsigned int depth)
1023{
1024 struct lock_list *entry;
1025 unsigned long ret = 1;
1026
1027 if (lockdep_dependency_visit(class, depth))
1028 return 0;
1029 /*
1030 * Recurse this class's dependency list:
1031 */
1032 list_for_each_entry(entry, &class->locks_before, entry)
1033 ret += __lockdep_count_backward_deps(entry->class, depth + 1);
1034
1035 return ret;
1036}
1037
1038unsigned long lockdep_count_backward_deps(struct lock_class *class)
1039{
1040 unsigned long ret, flags;
1041
1042 local_irq_save(flags);
1043 __raw_spin_lock(&lockdep_lock);
1044 ret = __lockdep_count_backward_deps(class, 0);
1045 __raw_spin_unlock(&lockdep_lock);
1046 local_irq_restore(flags);
1047
1048 return ret;
1049}
1050
Peter Zijlstra8e182572007-07-19 01:48:54 -07001051/*
1052 * Prove that the dependency graph starting at <entry> can not
1053 * lead to <target>. Print an error and return 0 if it does.
1054 */
1055static noinline int
1056check_noncircular(struct lock_class *source, unsigned int depth)
1057{
1058 struct lock_list *entry;
1059
David Miller419ca3f2008-07-29 21:45:03 -07001060 if (lockdep_dependency_visit(source, depth))
1061 return 1;
1062
Peter Zijlstra8e182572007-07-19 01:48:54 -07001063 debug_atomic_inc(&nr_cyclic_check_recursions);
1064 if (depth > max_recursion_depth)
1065 max_recursion_depth = depth;
1066 if (depth >= RECURSION_LIMIT)
1067 return print_infinite_recursion_bug();
1068 /*
1069 * Check this lock's dependency list:
1070 */
1071 list_for_each_entry(entry, &source->locks_after, entry) {
Dave Jonesf82b2172008-08-11 09:30:23 +02001072 if (entry->class == hlock_class(check_target))
Peter Zijlstra8e182572007-07-19 01:48:54 -07001073 return print_circular_bug_header(entry, depth+1);
1074 debug_atomic_inc(&nr_cyclic_checks);
1075 if (!check_noncircular(entry->class, depth+1))
1076 return print_circular_bug_entry(entry, depth+1);
1077 }
1078 return 1;
1079}
1080
Steven Rostedt81d68a92008-05-12 21:20:42 +02001081#if defined(CONFIG_TRACE_IRQFLAGS) && defined(CONFIG_PROVE_LOCKING)
Peter Zijlstra8e182572007-07-19 01:48:54 -07001082/*
1083 * Forwards and backwards subgraph searching, for the purposes of
1084 * proving that two subgraphs can be connected by a new dependency
1085 * without creating any illegal irq-safe -> irq-unsafe lock dependency.
1086 */
1087static enum lock_usage_bit find_usage_bit;
1088static struct lock_class *forwards_match, *backwards_match;
1089
1090/*
1091 * Find a node in the forwards-direction dependency sub-graph starting
1092 * at <source> that matches <find_usage_bit>.
1093 *
1094 * Return 2 if such a node exists in the subgraph, and put that node
1095 * into <forwards_match>.
1096 *
1097 * Return 1 otherwise and keep <forwards_match> unchanged.
1098 * Return 0 on error.
1099 */
1100static noinline int
1101find_usage_forwards(struct lock_class *source, unsigned int depth)
1102{
1103 struct lock_list *entry;
1104 int ret;
1105
David Miller419ca3f2008-07-29 21:45:03 -07001106 if (lockdep_dependency_visit(source, depth))
1107 return 1;
1108
Peter Zijlstra8e182572007-07-19 01:48:54 -07001109 if (depth > max_recursion_depth)
1110 max_recursion_depth = depth;
1111 if (depth >= RECURSION_LIMIT)
1112 return print_infinite_recursion_bug();
1113
1114 debug_atomic_inc(&nr_find_usage_forwards_checks);
1115 if (source->usage_mask & (1 << find_usage_bit)) {
1116 forwards_match = source;
1117 return 2;
1118 }
1119
1120 /*
1121 * Check this lock's dependency list:
1122 */
1123 list_for_each_entry(entry, &source->locks_after, entry) {
1124 debug_atomic_inc(&nr_find_usage_forwards_recursions);
1125 ret = find_usage_forwards(entry->class, depth+1);
1126 if (ret == 2 || ret == 0)
1127 return ret;
1128 }
1129 return 1;
1130}
1131
1132/*
1133 * Find a node in the backwards-direction dependency sub-graph starting
1134 * at <source> that matches <find_usage_bit>.
1135 *
1136 * Return 2 if such a node exists in the subgraph, and put that node
1137 * into <backwards_match>.
1138 *
1139 * Return 1 otherwise and keep <backwards_match> unchanged.
1140 * Return 0 on error.
1141 */
Steven Rostedt1d09daa2008-05-12 21:20:55 +02001142static noinline int
Peter Zijlstra8e182572007-07-19 01:48:54 -07001143find_usage_backwards(struct lock_class *source, unsigned int depth)
1144{
1145 struct lock_list *entry;
1146 int ret;
1147
David Miller419ca3f2008-07-29 21:45:03 -07001148 if (lockdep_dependency_visit(source, depth))
1149 return 1;
1150
Peter Zijlstra8e182572007-07-19 01:48:54 -07001151 if (!__raw_spin_is_locked(&lockdep_lock))
1152 return DEBUG_LOCKS_WARN_ON(1);
1153
1154 if (depth > max_recursion_depth)
1155 max_recursion_depth = depth;
1156 if (depth >= RECURSION_LIMIT)
1157 return print_infinite_recursion_bug();
1158
1159 debug_atomic_inc(&nr_find_usage_backwards_checks);
1160 if (source->usage_mask & (1 << find_usage_bit)) {
1161 backwards_match = source;
1162 return 2;
1163 }
1164
Dave Jonesf82b2172008-08-11 09:30:23 +02001165 if (!source && debug_locks_off_graph_unlock()) {
1166 WARN_ON(1);
1167 return 0;
1168 }
1169
Peter Zijlstra8e182572007-07-19 01:48:54 -07001170 /*
1171 * Check this lock's dependency list:
1172 */
1173 list_for_each_entry(entry, &source->locks_before, entry) {
1174 debug_atomic_inc(&nr_find_usage_backwards_recursions);
1175 ret = find_usage_backwards(entry->class, depth+1);
1176 if (ret == 2 || ret == 0)
1177 return ret;
1178 }
1179 return 1;
1180}
1181
1182static int
1183print_bad_irq_dependency(struct task_struct *curr,
1184 struct held_lock *prev,
1185 struct held_lock *next,
1186 enum lock_usage_bit bit1,
1187 enum lock_usage_bit bit2,
1188 const char *irqclass)
1189{
1190 if (!debug_locks_off_graph_unlock() || debug_locks_silent)
1191 return 0;
1192
1193 printk("\n======================================================\n");
1194 printk( "[ INFO: %s-safe -> %s-unsafe lock order detected ]\n",
1195 irqclass, irqclass);
1196 print_kernel_version();
1197 printk( "------------------------------------------------------\n");
1198 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 -07001199 curr->comm, task_pid_nr(curr),
Peter Zijlstra8e182572007-07-19 01:48:54 -07001200 curr->hardirq_context, hardirq_count() >> HARDIRQ_SHIFT,
1201 curr->softirq_context, softirq_count() >> SOFTIRQ_SHIFT,
1202 curr->hardirqs_enabled,
1203 curr->softirqs_enabled);
1204 print_lock(next);
1205
1206 printk("\nand this task is already holding:\n");
1207 print_lock(prev);
1208 printk("which would create a new lock dependency:\n");
Dave Jonesf82b2172008-08-11 09:30:23 +02001209 print_lock_name(hlock_class(prev));
Peter Zijlstra8e182572007-07-19 01:48:54 -07001210 printk(" ->");
Dave Jonesf82b2172008-08-11 09:30:23 +02001211 print_lock_name(hlock_class(next));
Peter Zijlstra8e182572007-07-19 01:48:54 -07001212 printk("\n");
1213
1214 printk("\nbut this new dependency connects a %s-irq-safe lock:\n",
1215 irqclass);
1216 print_lock_name(backwards_match);
1217 printk("\n... which became %s-irq-safe at:\n", irqclass);
1218
1219 print_stack_trace(backwards_match->usage_traces + bit1, 1);
1220
1221 printk("\nto a %s-irq-unsafe lock:\n", irqclass);
1222 print_lock_name(forwards_match);
1223 printk("\n... which became %s-irq-unsafe at:\n", irqclass);
1224 printk("...");
1225
1226 print_stack_trace(forwards_match->usage_traces + bit2, 1);
1227
1228 printk("\nother info that might help us debug this:\n\n");
1229 lockdep_print_held_locks(curr);
1230
1231 printk("\nthe %s-irq-safe lock's dependencies:\n", irqclass);
1232 print_lock_dependencies(backwards_match, 0);
1233
1234 printk("\nthe %s-irq-unsafe lock's dependencies:\n", irqclass);
1235 print_lock_dependencies(forwards_match, 0);
1236
1237 printk("\nstack backtrace:\n");
1238 dump_stack();
1239
1240 return 0;
1241}
1242
1243static int
1244check_usage(struct task_struct *curr, struct held_lock *prev,
1245 struct held_lock *next, enum lock_usage_bit bit_backwards,
1246 enum lock_usage_bit bit_forwards, const char *irqclass)
1247{
1248 int ret;
1249
1250 find_usage_bit = bit_backwards;
1251 /* fills in <backwards_match> */
Dave Jonesf82b2172008-08-11 09:30:23 +02001252 ret = find_usage_backwards(hlock_class(prev), 0);
Peter Zijlstra8e182572007-07-19 01:48:54 -07001253 if (!ret || ret == 1)
1254 return ret;
1255
1256 find_usage_bit = bit_forwards;
Dave Jonesf82b2172008-08-11 09:30:23 +02001257 ret = find_usage_forwards(hlock_class(next), 0);
Peter Zijlstra8e182572007-07-19 01:48:54 -07001258 if (!ret || ret == 1)
1259 return ret;
1260 /* ret == 2 */
1261 return print_bad_irq_dependency(curr, prev, next,
1262 bit_backwards, bit_forwards, irqclass);
1263}
1264
Peter Zijlstra4f367d8a2009-01-22 18:10:42 +01001265static const char *state_names[] = {
1266#define LOCKDEP_STATE(__STATE) \
Peter Zijlstrab4b136f2009-01-29 14:50:36 +01001267 __stringify(__STATE),
Peter Zijlstra4f367d8a2009-01-22 18:10:42 +01001268#include "lockdep_states.h"
1269#undef LOCKDEP_STATE
1270};
1271
1272static const char *state_rnames[] = {
1273#define LOCKDEP_STATE(__STATE) \
Peter Zijlstrab4b136f2009-01-29 14:50:36 +01001274 __stringify(__STATE)"-READ",
Peter Zijlstra4f367d8a2009-01-22 18:10:42 +01001275#include "lockdep_states.h"
1276#undef LOCKDEP_STATE
1277};
1278
1279static inline const char *state_name(enum lock_usage_bit bit)
1280{
1281 return (bit & 1) ? state_rnames[bit >> 2] : state_names[bit >> 2];
1282}
1283
1284static int exclusive_bit(int new_bit)
1285{
1286 /*
1287 * USED_IN
1288 * USED_IN_READ
1289 * ENABLED
1290 * ENABLED_READ
1291 *
1292 * bit 0 - write/read
1293 * bit 1 - used_in/enabled
1294 * bit 2+ state
1295 */
1296
1297 int state = new_bit & ~3;
1298 int dir = new_bit & 2;
1299
1300 /*
1301 * keep state, bit flip the direction and strip read.
1302 */
1303 return state | (dir ^ 2);
1304}
1305
1306static int check_irq_usage(struct task_struct *curr, struct held_lock *prev,
1307 struct held_lock *next, enum lock_usage_bit bit)
Peter Zijlstra8e182572007-07-19 01:48:54 -07001308{
1309 /*
1310 * Prove that the new dependency does not connect a hardirq-safe
1311 * lock with a hardirq-unsafe lock - to achieve this we search
1312 * the backwards-subgraph starting at <prev>, and the
1313 * forwards-subgraph starting at <next>:
1314 */
Peter Zijlstra4f367d8a2009-01-22 18:10:42 +01001315 if (!check_usage(curr, prev, next, bit,
1316 exclusive_bit(bit), state_name(bit)))
Peter Zijlstra8e182572007-07-19 01:48:54 -07001317 return 0;
1318
Peter Zijlstra4f367d8a2009-01-22 18:10:42 +01001319 bit++; /* _READ */
1320
Peter Zijlstra8e182572007-07-19 01:48:54 -07001321 /*
1322 * Prove that the new dependency does not connect a hardirq-safe-read
1323 * lock with a hardirq-unsafe lock - to achieve this we search
1324 * the backwards-subgraph starting at <prev>, and the
1325 * forwards-subgraph starting at <next>:
1326 */
Peter Zijlstra4f367d8a2009-01-22 18:10:42 +01001327 if (!check_usage(curr, prev, next, bit,
1328 exclusive_bit(bit), state_name(bit)))
Peter Zijlstra8e182572007-07-19 01:48:54 -07001329 return 0;
1330
Peter Zijlstra4f367d8a2009-01-22 18:10:42 +01001331 return 1;
1332}
Peter Zijlstra8e182572007-07-19 01:48:54 -07001333
Peter Zijlstra4f367d8a2009-01-22 18:10:42 +01001334static int
1335check_prev_add_irq(struct task_struct *curr, struct held_lock *prev,
1336 struct held_lock *next)
1337{
1338#define LOCKDEP_STATE(__STATE) \
1339 if (!check_irq_usage(curr, prev, next, LOCK_USED_IN_##__STATE)) \
Nick Piggincf40bd12009-01-21 08:12:39 +01001340 return 0;
Peter Zijlstra4f367d8a2009-01-22 18:10:42 +01001341#include "lockdep_states.h"
1342#undef LOCKDEP_STATE
Nick Piggincf40bd12009-01-21 08:12:39 +01001343
Peter Zijlstra8e182572007-07-19 01:48:54 -07001344 return 1;
1345}
1346
1347static void inc_chains(void)
1348{
1349 if (current->hardirq_context)
1350 nr_hardirq_chains++;
1351 else {
1352 if (current->softirq_context)
1353 nr_softirq_chains++;
1354 else
1355 nr_process_chains++;
1356 }
1357}
1358
1359#else
1360
1361static inline int
1362check_prev_add_irq(struct task_struct *curr, struct held_lock *prev,
1363 struct held_lock *next)
1364{
1365 return 1;
1366}
1367
1368static inline void inc_chains(void)
1369{
1370 nr_process_chains++;
1371}
1372
1373#endif
1374
1375static int
1376print_deadlock_bug(struct task_struct *curr, struct held_lock *prev,
1377 struct held_lock *next)
1378{
1379 if (!debug_locks_off_graph_unlock() || debug_locks_silent)
1380 return 0;
1381
1382 printk("\n=============================================\n");
1383 printk( "[ INFO: possible recursive locking detected ]\n");
1384 print_kernel_version();
1385 printk( "---------------------------------------------\n");
1386 printk("%s/%d is trying to acquire lock:\n",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07001387 curr->comm, task_pid_nr(curr));
Peter Zijlstra8e182572007-07-19 01:48:54 -07001388 print_lock(next);
1389 printk("\nbut task is already holding lock:\n");
1390 print_lock(prev);
1391
1392 printk("\nother info that might help us debug this:\n");
1393 lockdep_print_held_locks(curr);
1394
1395 printk("\nstack backtrace:\n");
1396 dump_stack();
1397
1398 return 0;
1399}
1400
1401/*
1402 * Check whether we are holding such a class already.
1403 *
1404 * (Note that this has to be done separately, because the graph cannot
1405 * detect such classes of deadlocks.)
1406 *
1407 * Returns: 0 on deadlock detected, 1 on OK, 2 on recursive read
1408 */
1409static int
1410check_deadlock(struct task_struct *curr, struct held_lock *next,
1411 struct lockdep_map *next_instance, int read)
1412{
1413 struct held_lock *prev;
Peter Zijlstra7531e2f2008-08-11 09:30:24 +02001414 struct held_lock *nest = NULL;
Peter Zijlstra8e182572007-07-19 01:48:54 -07001415 int i;
1416
1417 for (i = 0; i < curr->lockdep_depth; i++) {
1418 prev = curr->held_locks + i;
Peter Zijlstra7531e2f2008-08-11 09:30:24 +02001419
1420 if (prev->instance == next->nest_lock)
1421 nest = prev;
1422
Dave Jonesf82b2172008-08-11 09:30:23 +02001423 if (hlock_class(prev) != hlock_class(next))
Peter Zijlstra8e182572007-07-19 01:48:54 -07001424 continue;
Peter Zijlstra7531e2f2008-08-11 09:30:24 +02001425
Peter Zijlstra8e182572007-07-19 01:48:54 -07001426 /*
1427 * Allow read-after-read recursion of the same
1428 * lock class (i.e. read_lock(lock)+read_lock(lock)):
1429 */
1430 if ((read == 2) && prev->read)
1431 return 2;
Peter Zijlstra7531e2f2008-08-11 09:30:24 +02001432
1433 /*
1434 * We're holding the nest_lock, which serializes this lock's
1435 * nesting behaviour.
1436 */
1437 if (nest)
1438 return 2;
1439
Peter Zijlstra8e182572007-07-19 01:48:54 -07001440 return print_deadlock_bug(curr, prev, next);
1441 }
1442 return 1;
1443}
1444
1445/*
1446 * There was a chain-cache miss, and we are about to add a new dependency
1447 * to a previous lock. We recursively validate the following rules:
1448 *
1449 * - would the adding of the <prev> -> <next> dependency create a
1450 * circular dependency in the graph? [== circular deadlock]
1451 *
1452 * - does the new prev->next dependency connect any hardirq-safe lock
1453 * (in the full backwards-subgraph starting at <prev>) with any
1454 * hardirq-unsafe lock (in the full forwards-subgraph starting at
1455 * <next>)? [== illegal lock inversion with hardirq contexts]
1456 *
1457 * - does the new prev->next dependency connect any softirq-safe lock
1458 * (in the full backwards-subgraph starting at <prev>) with any
1459 * softirq-unsafe lock (in the full forwards-subgraph starting at
1460 * <next>)? [== illegal lock inversion with softirq contexts]
1461 *
1462 * any of these scenarios could lead to a deadlock.
1463 *
1464 * Then if all the validations pass, we add the forwards and backwards
1465 * dependency.
1466 */
1467static int
1468check_prev_add(struct task_struct *curr, struct held_lock *prev,
1469 struct held_lock *next, int distance)
1470{
1471 struct lock_list *entry;
1472 int ret;
1473
1474 /*
1475 * Prove that the new <prev> -> <next> dependency would not
1476 * create a circular dependency in the graph. (We do this by
1477 * forward-recursing into the graph starting at <next>, and
1478 * checking whether we can reach <prev>.)
1479 *
1480 * We are using global variables to control the recursion, to
1481 * keep the stackframe size of the recursive functions low:
1482 */
1483 check_source = next;
1484 check_target = prev;
Dave Jonesf82b2172008-08-11 09:30:23 +02001485 if (!(check_noncircular(hlock_class(next), 0)))
Peter Zijlstra8e182572007-07-19 01:48:54 -07001486 return print_circular_bug_tail();
1487
1488 if (!check_prev_add_irq(curr, prev, next))
1489 return 0;
1490
1491 /*
1492 * For recursive read-locks we do all the dependency checks,
1493 * but we dont store read-triggered dependencies (only
1494 * write-triggered dependencies). This ensures that only the
1495 * write-side dependencies matter, and that if for example a
1496 * write-lock never takes any other locks, then the reads are
1497 * equivalent to a NOP.
1498 */
1499 if (next->read == 2 || prev->read == 2)
1500 return 1;
1501 /*
1502 * Is the <prev> -> <next> dependency already present?
1503 *
1504 * (this may occur even though this is a new chain: consider
1505 * e.g. the L1 -> L2 -> L3 -> L4 and the L5 -> L1 -> L2 -> L3
1506 * chains - the second one will be new, but L1 already has
1507 * L2 added to its dependency list, due to the first chain.)
1508 */
Dave Jonesf82b2172008-08-11 09:30:23 +02001509 list_for_each_entry(entry, &hlock_class(prev)->locks_after, entry) {
1510 if (entry->class == hlock_class(next)) {
Peter Zijlstra8e182572007-07-19 01:48:54 -07001511 if (distance == 1)
1512 entry->distance = 1;
1513 return 2;
1514 }
1515 }
1516
1517 /*
1518 * Ok, all validations passed, add the new lock
1519 * to the previous lock's dependency list:
1520 */
Dave Jonesf82b2172008-08-11 09:30:23 +02001521 ret = add_lock_to_list(hlock_class(prev), hlock_class(next),
1522 &hlock_class(prev)->locks_after,
1523 next->acquire_ip, distance);
Peter Zijlstra8e182572007-07-19 01:48:54 -07001524
1525 if (!ret)
1526 return 0;
1527
Dave Jonesf82b2172008-08-11 09:30:23 +02001528 ret = add_lock_to_list(hlock_class(next), hlock_class(prev),
1529 &hlock_class(next)->locks_before,
1530 next->acquire_ip, distance);
Peter Zijlstra8e182572007-07-19 01:48:54 -07001531 if (!ret)
1532 return 0;
1533
1534 /*
1535 * Debugging printouts:
1536 */
Dave Jonesf82b2172008-08-11 09:30:23 +02001537 if (verbose(hlock_class(prev)) || verbose(hlock_class(next))) {
Peter Zijlstra8e182572007-07-19 01:48:54 -07001538 graph_unlock();
1539 printk("\n new dependency: ");
Dave Jonesf82b2172008-08-11 09:30:23 +02001540 print_lock_name(hlock_class(prev));
Peter Zijlstra8e182572007-07-19 01:48:54 -07001541 printk(" => ");
Dave Jonesf82b2172008-08-11 09:30:23 +02001542 print_lock_name(hlock_class(next));
Peter Zijlstra8e182572007-07-19 01:48:54 -07001543 printk("\n");
1544 dump_stack();
1545 return graph_lock();
1546 }
1547 return 1;
1548}
1549
1550/*
1551 * Add the dependency to all directly-previous locks that are 'relevant'.
1552 * The ones that are relevant are (in increasing distance from curr):
1553 * all consecutive trylock entries and the final non-trylock entry - or
1554 * the end of this context's lock-chain - whichever comes first.
1555 */
1556static int
1557check_prevs_add(struct task_struct *curr, struct held_lock *next)
1558{
1559 int depth = curr->lockdep_depth;
1560 struct held_lock *hlock;
1561
1562 /*
1563 * Debugging checks.
1564 *
1565 * Depth must not be zero for a non-head lock:
1566 */
1567 if (!depth)
1568 goto out_bug;
1569 /*
1570 * At least two relevant locks must exist for this
1571 * to be a head:
1572 */
1573 if (curr->held_locks[depth].irq_context !=
1574 curr->held_locks[depth-1].irq_context)
1575 goto out_bug;
1576
1577 for (;;) {
1578 int distance = curr->lockdep_depth - depth + 1;
1579 hlock = curr->held_locks + depth-1;
1580 /*
1581 * Only non-recursive-read entries get new dependencies
1582 * added:
1583 */
1584 if (hlock->read != 2) {
1585 if (!check_prev_add(curr, hlock, next, distance))
1586 return 0;
1587 /*
1588 * Stop after the first non-trylock entry,
1589 * as non-trylock entries have added their
1590 * own direct dependencies already, so this
1591 * lock is connected to them indirectly:
1592 */
1593 if (!hlock->trylock)
1594 break;
1595 }
1596 depth--;
1597 /*
1598 * End of lock-stack?
1599 */
1600 if (!depth)
1601 break;
1602 /*
1603 * Stop the search if we cross into another context:
1604 */
1605 if (curr->held_locks[depth].irq_context !=
1606 curr->held_locks[depth-1].irq_context)
1607 break;
1608 }
1609 return 1;
1610out_bug:
1611 if (!debug_locks_off_graph_unlock())
1612 return 0;
1613
1614 WARN_ON(1);
1615
1616 return 0;
1617}
1618
1619unsigned long nr_lock_chains;
Huang, Ying443cd502008-06-20 16:39:21 +08001620struct lock_chain lock_chains[MAX_LOCKDEP_CHAINS];
Huang, Yingcd1a28e2008-06-23 11:20:54 +08001621int nr_chain_hlocks;
Huang, Ying443cd502008-06-20 16:39:21 +08001622static u16 chain_hlocks[MAX_LOCKDEP_CHAIN_HLOCKS];
1623
1624struct lock_class *lock_chain_get_class(struct lock_chain *chain, int i)
1625{
1626 return lock_classes + chain_hlocks[chain->base + i];
1627}
Peter Zijlstra8e182572007-07-19 01:48:54 -07001628
1629/*
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001630 * Look up a dependency chain. If the key is not present yet then
Jarek Poplawski9e860d02007-05-08 00:30:12 -07001631 * add it and return 1 - in this case the new dependency chain is
1632 * validated. If the key is already hashed, return 0.
1633 * (On return with 1 graph_lock is held.)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001634 */
Huang, Ying443cd502008-06-20 16:39:21 +08001635static inline int lookup_chain_cache(struct task_struct *curr,
1636 struct held_lock *hlock,
1637 u64 chain_key)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001638{
Dave Jonesf82b2172008-08-11 09:30:23 +02001639 struct lock_class *class = hlock_class(hlock);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001640 struct list_head *hash_head = chainhashentry(chain_key);
1641 struct lock_chain *chain;
Huang, Ying443cd502008-06-20 16:39:21 +08001642 struct held_lock *hlock_curr, *hlock_next;
Huang, Yingcd1a28e2008-06-23 11:20:54 +08001643 int i, j, n, cn;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001644
Jarek Poplawski381a2292007-02-10 01:44:58 -08001645 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
1646 return 0;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001647 /*
1648 * We can walk it lock-free, because entries only get added
1649 * to the hash:
1650 */
1651 list_for_each_entry(chain, hash_head, entry) {
1652 if (chain->chain_key == chain_key) {
1653cache_hit:
1654 debug_atomic_inc(&chain_lookup_hits);
Ingo Molnar81fc6852006-12-13 00:34:40 -08001655 if (very_verbose(class))
Andrew Morton755cd902006-12-29 16:49:14 -08001656 printk("\nhash chain already cached, key: "
1657 "%016Lx tail class: [%p] %s\n",
1658 (unsigned long long)chain_key,
1659 class->key, class->name);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001660 return 0;
1661 }
1662 }
Ingo Molnar81fc6852006-12-13 00:34:40 -08001663 if (very_verbose(class))
Andrew Morton755cd902006-12-29 16:49:14 -08001664 printk("\nnew hash chain, key: %016Lx tail class: [%p] %s\n",
1665 (unsigned long long)chain_key, class->key, class->name);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001666 /*
1667 * Allocate a new chain entry from the static array, and add
1668 * it to the hash:
1669 */
Ingo Molnar74c383f2006-12-13 00:34:43 -08001670 if (!graph_lock())
1671 return 0;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001672 /*
1673 * We have to walk the chain again locked - to avoid duplicates:
1674 */
1675 list_for_each_entry(chain, hash_head, entry) {
1676 if (chain->chain_key == chain_key) {
Ingo Molnar74c383f2006-12-13 00:34:43 -08001677 graph_unlock();
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001678 goto cache_hit;
1679 }
1680 }
1681 if (unlikely(nr_lock_chains >= MAX_LOCKDEP_CHAINS)) {
Ingo Molnar74c383f2006-12-13 00:34:43 -08001682 if (!debug_locks_off_graph_unlock())
1683 return 0;
1684
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001685 printk("BUG: MAX_LOCKDEP_CHAINS too low!\n");
1686 printk("turning off the locking correctness validator.\n");
Peter Zijlstraeedeeab2009-03-18 12:38:47 +01001687 dump_stack();
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001688 return 0;
1689 }
1690 chain = lock_chains + nr_lock_chains++;
1691 chain->chain_key = chain_key;
Huang, Ying443cd502008-06-20 16:39:21 +08001692 chain->irq_context = hlock->irq_context;
1693 /* Find the first held_lock of current chain */
1694 hlock_next = hlock;
1695 for (i = curr->lockdep_depth - 1; i >= 0; i--) {
1696 hlock_curr = curr->held_locks + i;
1697 if (hlock_curr->irq_context != hlock_next->irq_context)
1698 break;
1699 hlock_next = hlock;
1700 }
1701 i++;
1702 chain->depth = curr->lockdep_depth + 1 - i;
Huang, Yingcd1a28e2008-06-23 11:20:54 +08001703 cn = nr_chain_hlocks;
1704 while (cn + chain->depth <= MAX_LOCKDEP_CHAIN_HLOCKS) {
1705 n = cmpxchg(&nr_chain_hlocks, cn, cn + chain->depth);
1706 if (n == cn)
1707 break;
1708 cn = n;
1709 }
1710 if (likely(cn + chain->depth <= MAX_LOCKDEP_CHAIN_HLOCKS)) {
1711 chain->base = cn;
Huang, Ying443cd502008-06-20 16:39:21 +08001712 for (j = 0; j < chain->depth - 1; j++, i++) {
Dave Jonesf82b2172008-08-11 09:30:23 +02001713 int lock_id = curr->held_locks[i].class_idx - 1;
Huang, Ying443cd502008-06-20 16:39:21 +08001714 chain_hlocks[chain->base + j] = lock_id;
1715 }
1716 chain_hlocks[chain->base + j] = class - lock_classes;
1717 }
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001718 list_add_tail_rcu(&chain->entry, hash_head);
1719 debug_atomic_inc(&chain_lookup_misses);
Peter Zijlstra8e182572007-07-19 01:48:54 -07001720 inc_chains();
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001721
1722 return 1;
1723}
Peter Zijlstra8e182572007-07-19 01:48:54 -07001724
1725static int validate_chain(struct task_struct *curr, struct lockdep_map *lock,
Johannes Berg4e6045f2007-10-18 23:39:55 -07001726 struct held_lock *hlock, int chain_head, u64 chain_key)
Peter Zijlstra8e182572007-07-19 01:48:54 -07001727{
1728 /*
1729 * Trylock needs to maintain the stack of held locks, but it
1730 * does not add new dependencies, because trylock can be done
1731 * in any order.
1732 *
1733 * We look up the chain_key and do the O(N^2) check and update of
1734 * the dependencies only if this is a new dependency chain.
1735 * (If lookup_chain_cache() returns with 1 it acquires
1736 * graph_lock for us)
1737 */
1738 if (!hlock->trylock && (hlock->check == 2) &&
Huang, Ying443cd502008-06-20 16:39:21 +08001739 lookup_chain_cache(curr, hlock, chain_key)) {
Peter Zijlstra8e182572007-07-19 01:48:54 -07001740 /*
1741 * Check whether last held lock:
1742 *
1743 * - is irq-safe, if this lock is irq-unsafe
1744 * - is softirq-safe, if this lock is hardirq-unsafe
1745 *
1746 * And check whether the new lock's dependency graph
1747 * could lead back to the previous lock.
1748 *
1749 * any of these scenarios could lead to a deadlock. If
1750 * All validations
1751 */
1752 int ret = check_deadlock(curr, hlock, lock, hlock->read);
1753
1754 if (!ret)
1755 return 0;
1756 /*
1757 * Mark recursive read, as we jump over it when
1758 * building dependencies (just like we jump over
1759 * trylock entries):
1760 */
1761 if (ret == 2)
1762 hlock->read = 2;
1763 /*
1764 * Add dependency only if this lock is not the head
1765 * of the chain, and if it's not a secondary read-lock:
1766 */
1767 if (!chain_head && ret != 2)
1768 if (!check_prevs_add(curr, hlock))
1769 return 0;
1770 graph_unlock();
1771 } else
1772 /* after lookup_chain_cache(): */
1773 if (unlikely(!debug_locks))
1774 return 0;
1775
1776 return 1;
1777}
1778#else
1779static inline int validate_chain(struct task_struct *curr,
1780 struct lockdep_map *lock, struct held_lock *hlock,
Gregory Haskins3aa416b2007-10-11 22:11:11 +02001781 int chain_head, u64 chain_key)
Peter Zijlstra8e182572007-07-19 01:48:54 -07001782{
1783 return 1;
1784}
Peter Zijlstraca58abc2007-07-19 01:48:53 -07001785#endif
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001786
1787/*
1788 * We are building curr_chain_key incrementally, so double-check
1789 * it from scratch, to make sure that it's done correctly:
1790 */
Steven Rostedt1d09daa2008-05-12 21:20:55 +02001791static void check_chain_key(struct task_struct *curr)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001792{
1793#ifdef CONFIG_DEBUG_LOCKDEP
1794 struct held_lock *hlock, *prev_hlock = NULL;
1795 unsigned int i, id;
1796 u64 chain_key = 0;
1797
1798 for (i = 0; i < curr->lockdep_depth; i++) {
1799 hlock = curr->held_locks + i;
1800 if (chain_key != hlock->prev_chain_key) {
1801 debug_locks_off();
Arjan van de Ven2df8b1d2008-07-30 12:43:11 -07001802 WARN(1, "hm#1, depth: %u [%u], %016Lx != %016Lx\n",
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001803 curr->lockdep_depth, i,
1804 (unsigned long long)chain_key,
1805 (unsigned long long)hlock->prev_chain_key);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001806 return;
1807 }
Dave Jonesf82b2172008-08-11 09:30:23 +02001808 id = hlock->class_idx - 1;
Jarek Poplawski381a2292007-02-10 01:44:58 -08001809 if (DEBUG_LOCKS_WARN_ON(id >= MAX_LOCKDEP_KEYS))
1810 return;
1811
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001812 if (prev_hlock && (prev_hlock->irq_context !=
1813 hlock->irq_context))
1814 chain_key = 0;
1815 chain_key = iterate_chain_key(chain_key, id);
1816 prev_hlock = hlock;
1817 }
1818 if (chain_key != curr->curr_chain_key) {
1819 debug_locks_off();
Arjan van de Ven2df8b1d2008-07-30 12:43:11 -07001820 WARN(1, "hm#2, depth: %u [%u], %016Lx != %016Lx\n",
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001821 curr->lockdep_depth, i,
1822 (unsigned long long)chain_key,
1823 (unsigned long long)curr->curr_chain_key);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001824 }
1825#endif
1826}
1827
Peter Zijlstra8e182572007-07-19 01:48:54 -07001828static int
1829print_usage_bug(struct task_struct *curr, struct held_lock *this,
1830 enum lock_usage_bit prev_bit, enum lock_usage_bit new_bit)
1831{
1832 if (!debug_locks_off_graph_unlock() || debug_locks_silent)
1833 return 0;
1834
1835 printk("\n=================================\n");
1836 printk( "[ INFO: inconsistent lock state ]\n");
1837 print_kernel_version();
1838 printk( "---------------------------------\n");
1839
1840 printk("inconsistent {%s} -> {%s} usage.\n",
1841 usage_str[prev_bit], usage_str[new_bit]);
1842
1843 printk("%s/%d [HC%u[%lu]:SC%u[%lu]:HE%u:SE%u] takes:\n",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07001844 curr->comm, task_pid_nr(curr),
Peter Zijlstra8e182572007-07-19 01:48:54 -07001845 trace_hardirq_context(curr), hardirq_count() >> HARDIRQ_SHIFT,
1846 trace_softirq_context(curr), softirq_count() >> SOFTIRQ_SHIFT,
1847 trace_hardirqs_enabled(curr),
1848 trace_softirqs_enabled(curr));
1849 print_lock(this);
1850
1851 printk("{%s} state was registered at:\n", usage_str[prev_bit]);
Dave Jonesf82b2172008-08-11 09:30:23 +02001852 print_stack_trace(hlock_class(this)->usage_traces + prev_bit, 1);
Peter Zijlstra8e182572007-07-19 01:48:54 -07001853
1854 print_irqtrace_events(curr);
1855 printk("\nother info that might help us debug this:\n");
1856 lockdep_print_held_locks(curr);
1857
1858 printk("\nstack backtrace:\n");
1859 dump_stack();
1860
1861 return 0;
1862}
1863
1864/*
1865 * Print out an error if an invalid bit is set:
1866 */
1867static inline int
1868valid_state(struct task_struct *curr, struct held_lock *this,
1869 enum lock_usage_bit new_bit, enum lock_usage_bit bad_bit)
1870{
Dave Jonesf82b2172008-08-11 09:30:23 +02001871 if (unlikely(hlock_class(this)->usage_mask & (1 << bad_bit)))
Peter Zijlstra8e182572007-07-19 01:48:54 -07001872 return print_usage_bug(curr, this, bad_bit, new_bit);
1873 return 1;
1874}
1875
1876static int mark_lock(struct task_struct *curr, struct held_lock *this,
1877 enum lock_usage_bit new_bit);
1878
Steven Rostedt81d68a92008-05-12 21:20:42 +02001879#if defined(CONFIG_TRACE_IRQFLAGS) && defined(CONFIG_PROVE_LOCKING)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001880
1881/*
1882 * print irq inversion bug:
1883 */
1884static int
1885print_irq_inversion_bug(struct task_struct *curr, struct lock_class *other,
1886 struct held_lock *this, int forwards,
1887 const char *irqclass)
1888{
Ingo Molnar74c383f2006-12-13 00:34:43 -08001889 if (!debug_locks_off_graph_unlock() || debug_locks_silent)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001890 return 0;
1891
1892 printk("\n=========================================================\n");
1893 printk( "[ INFO: possible irq lock inversion dependency detected ]\n");
Dave Jones99de0552006-09-29 02:00:10 -07001894 print_kernel_version();
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001895 printk( "---------------------------------------------------------\n");
1896 printk("%s/%d just changed the state of lock:\n",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07001897 curr->comm, task_pid_nr(curr));
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001898 print_lock(this);
1899 if (forwards)
Peter Zijlstra26575e22009-03-04 14:53:24 +01001900 printk("but this lock took another, %s-unsafe lock in the past:\n", irqclass);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001901 else
Peter Zijlstra26575e22009-03-04 14:53:24 +01001902 printk("but this lock was taken by another, %s-safe lock in the past:\n", irqclass);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001903 print_lock_name(other);
1904 printk("\n\nand interrupts could create inverse lock ordering between them.\n\n");
1905
1906 printk("\nother info that might help us debug this:\n");
1907 lockdep_print_held_locks(curr);
1908
1909 printk("\nthe first lock's dependencies:\n");
Dave Jonesf82b2172008-08-11 09:30:23 +02001910 print_lock_dependencies(hlock_class(this), 0);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001911
1912 printk("\nthe second lock's dependencies:\n");
1913 print_lock_dependencies(other, 0);
1914
1915 printk("\nstack backtrace:\n");
1916 dump_stack();
1917
1918 return 0;
1919}
1920
1921/*
1922 * Prove that in the forwards-direction subgraph starting at <this>
1923 * there is no lock matching <mask>:
1924 */
1925static int
1926check_usage_forwards(struct task_struct *curr, struct held_lock *this,
1927 enum lock_usage_bit bit, const char *irqclass)
1928{
1929 int ret;
1930
1931 find_usage_bit = bit;
1932 /* fills in <forwards_match> */
Dave Jonesf82b2172008-08-11 09:30:23 +02001933 ret = find_usage_forwards(hlock_class(this), 0);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001934 if (!ret || ret == 1)
1935 return ret;
1936
1937 return print_irq_inversion_bug(curr, forwards_match, this, 1, irqclass);
1938}
1939
1940/*
1941 * Prove that in the backwards-direction subgraph starting at <this>
1942 * there is no lock matching <mask>:
1943 */
1944static int
1945check_usage_backwards(struct task_struct *curr, struct held_lock *this,
1946 enum lock_usage_bit bit, const char *irqclass)
1947{
1948 int ret;
1949
1950 find_usage_bit = bit;
1951 /* fills in <backwards_match> */
Dave Jonesf82b2172008-08-11 09:30:23 +02001952 ret = find_usage_backwards(hlock_class(this), 0);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001953 if (!ret || ret == 1)
1954 return ret;
1955
1956 return print_irq_inversion_bug(curr, backwards_match, this, 0, irqclass);
1957}
1958
Ingo Molnar3117df02006-12-13 00:34:43 -08001959void print_irqtrace_events(struct task_struct *curr)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001960{
1961 printk("irq event stamp: %u\n", curr->irq_events);
1962 printk("hardirqs last enabled at (%u): ", curr->hardirq_enable_event);
1963 print_ip_sym(curr->hardirq_enable_ip);
1964 printk("hardirqs last disabled at (%u): ", curr->hardirq_disable_event);
1965 print_ip_sym(curr->hardirq_disable_ip);
1966 printk("softirqs last enabled at (%u): ", curr->softirq_enable_event);
1967 print_ip_sym(curr->softirq_enable_ip);
1968 printk("softirqs last disabled at (%u): ", curr->softirq_disable_event);
1969 print_ip_sym(curr->softirq_disable_ip);
1970}
1971
Peter Zijlstracd953022009-01-22 16:38:21 +01001972static int HARDIRQ_verbose(struct lock_class *class)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001973{
Peter Zijlstra8e182572007-07-19 01:48:54 -07001974#if HARDIRQ_VERBOSE
1975 return class_filter(class);
1976#endif
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001977 return 0;
1978}
1979
Peter Zijlstracd953022009-01-22 16:38:21 +01001980static int SOFTIRQ_verbose(struct lock_class *class)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001981{
Peter Zijlstra8e182572007-07-19 01:48:54 -07001982#if SOFTIRQ_VERBOSE
1983 return class_filter(class);
1984#endif
1985 return 0;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001986}
1987
Peter Zijlstracd953022009-01-22 16:38:21 +01001988static int RECLAIM_FS_verbose(struct lock_class *class)
Nick Piggincf40bd12009-01-21 08:12:39 +01001989{
1990#if RECLAIM_VERBOSE
1991 return class_filter(class);
1992#endif
1993 return 0;
1994}
1995
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001996#define STRICT_READ_CHECKS 1
1997
Peter Zijlstracd953022009-01-22 16:38:21 +01001998static int (*state_verbose_f[])(struct lock_class *class) = {
1999#define LOCKDEP_STATE(__STATE) \
2000 __STATE##_verbose,
2001#include "lockdep_states.h"
2002#undef LOCKDEP_STATE
2003};
2004
2005static inline int state_verbose(enum lock_usage_bit bit,
2006 struct lock_class *class)
2007{
2008 return state_verbose_f[bit >> 2](class);
2009}
2010
Peter Zijlstra42c50d52009-01-22 16:58:16 +01002011typedef int (*check_usage_f)(struct task_struct *, struct held_lock *,
2012 enum lock_usage_bit bit, const char *name);
2013
Peter Zijlstra6a6904d2009-01-22 16:07:44 +01002014static int
Peter Zijlstra1c21f142009-03-04 13:51:13 +01002015mark_lock_irq(struct task_struct *curr, struct held_lock *this,
2016 enum lock_usage_bit new_bit)
Peter Zijlstra6a6904d2009-01-22 16:07:44 +01002017{
Peter Zijlstraf9892092009-01-22 16:09:59 +01002018 int excl_bit = exclusive_bit(new_bit);
Peter Zijlstra9d3651a2009-01-22 17:18:32 +01002019 int read = new_bit & 1;
Peter Zijlstra42c50d52009-01-22 16:58:16 +01002020 int dir = new_bit & 2;
2021
Peter Zijlstra38aa2712009-01-27 14:53:50 +01002022 /*
2023 * mark USED_IN has to look forwards -- to ensure no dependency
2024 * has ENABLED state, which would allow recursion deadlocks.
2025 *
2026 * mark ENABLED has to look backwards -- to ensure no dependee
2027 * has USED_IN state, which, again, would allow recursion deadlocks.
2028 */
Peter Zijlstra42c50d52009-01-22 16:58:16 +01002029 check_usage_f usage = dir ?
2030 check_usage_backwards : check_usage_forwards;
Peter Zijlstraf9892092009-01-22 16:09:59 +01002031
Peter Zijlstra38aa2712009-01-27 14:53:50 +01002032 /*
2033 * Validate that this particular lock does not have conflicting
2034 * usage states.
2035 */
Peter Zijlstra6a6904d2009-01-22 16:07:44 +01002036 if (!valid_state(curr, this, new_bit, excl_bit))
2037 return 0;
Peter Zijlstra9d3651a2009-01-22 17:18:32 +01002038
Peter Zijlstra38aa2712009-01-27 14:53:50 +01002039 /*
2040 * Validate that the lock dependencies don't have conflicting usage
2041 * states.
2042 */
2043 if ((!read || !dir || STRICT_READ_CHECKS) &&
Peter Zijlstra1c21f142009-03-04 13:51:13 +01002044 !usage(curr, this, excl_bit, state_name(new_bit & ~1)))
Peter Zijlstra6a6904d2009-01-22 16:07:44 +01002045 return 0;
Peter Zijlstra780e8202009-01-22 16:51:29 +01002046
Peter Zijlstra38aa2712009-01-27 14:53:50 +01002047 /*
2048 * Check for read in write conflicts
2049 */
2050 if (!read) {
2051 if (!valid_state(curr, this, new_bit, excl_bit + 1))
2052 return 0;
2053
2054 if (STRICT_READ_CHECKS &&
Peter Zijlstra4f367d8a2009-01-22 18:10:42 +01002055 !usage(curr, this, excl_bit + 1,
2056 state_name(new_bit + 1)))
Peter Zijlstra38aa2712009-01-27 14:53:50 +01002057 return 0;
2058 }
Peter Zijlstra780e8202009-01-22 16:51:29 +01002059
Peter Zijlstracd953022009-01-22 16:38:21 +01002060 if (state_verbose(new_bit, hlock_class(this)))
Peter Zijlstra6a6904d2009-01-22 16:07:44 +01002061 return 2;
2062
2063 return 1;
2064}
2065
Nick Piggincf40bd12009-01-21 08:12:39 +01002066enum mark_type {
Peter Zijlstra36bfb9b2009-01-22 14:12:41 +01002067#define LOCKDEP_STATE(__STATE) __STATE,
2068#include "lockdep_states.h"
2069#undef LOCKDEP_STATE
Nick Piggincf40bd12009-01-21 08:12:39 +01002070};
2071
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002072/*
2073 * Mark all held locks with a usage bit:
2074 */
Steven Rostedt1d09daa2008-05-12 21:20:55 +02002075static int
Nick Piggincf40bd12009-01-21 08:12:39 +01002076mark_held_locks(struct task_struct *curr, enum mark_type mark)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002077{
2078 enum lock_usage_bit usage_bit;
2079 struct held_lock *hlock;
2080 int i;
2081
2082 for (i = 0; i < curr->lockdep_depth; i++) {
2083 hlock = curr->held_locks + i;
2084
Peter Zijlstracf2ad4d2009-01-27 13:58:08 +01002085 usage_bit = 2 + (mark << 2); /* ENABLED */
2086 if (hlock->read)
2087 usage_bit += 1; /* READ */
2088
2089 BUG_ON(usage_bit >= LOCK_USAGE_STATES);
Nick Piggincf40bd12009-01-21 08:12:39 +01002090
Jarek Poplawski4ff773bb2007-05-08 00:31:00 -07002091 if (!mark_lock(curr, hlock, usage_bit))
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002092 return 0;
2093 }
2094
2095 return 1;
2096}
2097
2098/*
2099 * Debugging helper: via this flag we know that we are in
2100 * 'early bootup code', and will warn about any invalid irqs-on event:
2101 */
2102static int early_boot_irqs_enabled;
2103
2104void early_boot_irqs_off(void)
2105{
2106 early_boot_irqs_enabled = 0;
2107}
2108
2109void early_boot_irqs_on(void)
2110{
2111 early_boot_irqs_enabled = 1;
2112}
2113
2114/*
2115 * Hardirqs will be enabled:
2116 */
Heiko Carstens6afe40b2008-10-28 11:14:58 +01002117void trace_hardirqs_on_caller(unsigned long ip)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002118{
2119 struct task_struct *curr = current;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002120
Heiko Carstens6afe40b2008-10-28 11:14:58 +01002121 time_hardirqs_on(CALLER_ADDR0, ip);
Steven Rostedt81d68a92008-05-12 21:20:42 +02002122
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002123 if (unlikely(!debug_locks || current->lockdep_recursion))
2124 return;
2125
2126 if (DEBUG_LOCKS_WARN_ON(unlikely(!early_boot_irqs_enabled)))
2127 return;
2128
2129 if (unlikely(curr->hardirqs_enabled)) {
2130 debug_atomic_inc(&redundant_hardirqs_on);
2131 return;
2132 }
2133 /* we'll do an OFF -> ON transition: */
2134 curr->hardirqs_enabled = 1;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002135
2136 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2137 return;
2138 if (DEBUG_LOCKS_WARN_ON(current->hardirq_context))
2139 return;
2140 /*
2141 * We are going to turn hardirqs on, so set the
2142 * usage bit for all held locks:
2143 */
Nick Piggincf40bd12009-01-21 08:12:39 +01002144 if (!mark_held_locks(curr, HARDIRQ))
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002145 return;
2146 /*
2147 * If we have softirqs enabled, then set the usage
2148 * bit for all held locks. (disabled hardirqs prevented
2149 * this bit from being set before)
2150 */
2151 if (curr->softirqs_enabled)
Nick Piggincf40bd12009-01-21 08:12:39 +01002152 if (!mark_held_locks(curr, SOFTIRQ))
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002153 return;
2154
2155 curr->hardirq_enable_ip = ip;
2156 curr->hardirq_enable_event = ++curr->irq_events;
2157 debug_atomic_inc(&hardirqs_on_events);
2158}
Steven Rostedt81d68a92008-05-12 21:20:42 +02002159EXPORT_SYMBOL(trace_hardirqs_on_caller);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002160
Steven Rostedt1d09daa2008-05-12 21:20:55 +02002161void trace_hardirqs_on(void)
Steven Rostedt81d68a92008-05-12 21:20:42 +02002162{
2163 trace_hardirqs_on_caller(CALLER_ADDR0);
2164}
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002165EXPORT_SYMBOL(trace_hardirqs_on);
2166
2167/*
2168 * Hardirqs were disabled:
2169 */
Heiko Carstens6afe40b2008-10-28 11:14:58 +01002170void trace_hardirqs_off_caller(unsigned long ip)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002171{
2172 struct task_struct *curr = current;
2173
Heiko Carstens6afe40b2008-10-28 11:14:58 +01002174 time_hardirqs_off(CALLER_ADDR0, ip);
Steven Rostedt81d68a92008-05-12 21:20:42 +02002175
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002176 if (unlikely(!debug_locks || current->lockdep_recursion))
2177 return;
2178
2179 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2180 return;
2181
2182 if (curr->hardirqs_enabled) {
2183 /*
2184 * We have done an ON -> OFF transition:
2185 */
2186 curr->hardirqs_enabled = 0;
Heiko Carstens6afe40b2008-10-28 11:14:58 +01002187 curr->hardirq_disable_ip = ip;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002188 curr->hardirq_disable_event = ++curr->irq_events;
2189 debug_atomic_inc(&hardirqs_off_events);
2190 } else
2191 debug_atomic_inc(&redundant_hardirqs_off);
2192}
Steven Rostedt81d68a92008-05-12 21:20:42 +02002193EXPORT_SYMBOL(trace_hardirqs_off_caller);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002194
Steven Rostedt1d09daa2008-05-12 21:20:55 +02002195void trace_hardirqs_off(void)
Steven Rostedt81d68a92008-05-12 21:20:42 +02002196{
2197 trace_hardirqs_off_caller(CALLER_ADDR0);
2198}
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002199EXPORT_SYMBOL(trace_hardirqs_off);
2200
2201/*
2202 * Softirqs will be enabled:
2203 */
2204void trace_softirqs_on(unsigned long ip)
2205{
2206 struct task_struct *curr = current;
2207
2208 if (unlikely(!debug_locks))
2209 return;
2210
2211 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2212 return;
2213
2214 if (curr->softirqs_enabled) {
2215 debug_atomic_inc(&redundant_softirqs_on);
2216 return;
2217 }
2218
2219 /*
2220 * We'll do an OFF -> ON transition:
2221 */
2222 curr->softirqs_enabled = 1;
2223 curr->softirq_enable_ip = ip;
2224 curr->softirq_enable_event = ++curr->irq_events;
2225 debug_atomic_inc(&softirqs_on_events);
2226 /*
2227 * We are going to turn softirqs on, so set the
2228 * usage bit for all held locks, if hardirqs are
2229 * enabled too:
2230 */
2231 if (curr->hardirqs_enabled)
Nick Piggincf40bd12009-01-21 08:12:39 +01002232 mark_held_locks(curr, SOFTIRQ);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002233}
2234
2235/*
2236 * Softirqs were disabled:
2237 */
2238void trace_softirqs_off(unsigned long ip)
2239{
2240 struct task_struct *curr = current;
2241
2242 if (unlikely(!debug_locks))
2243 return;
2244
2245 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2246 return;
2247
2248 if (curr->softirqs_enabled) {
2249 /*
2250 * We have done an ON -> OFF transition:
2251 */
2252 curr->softirqs_enabled = 0;
2253 curr->softirq_disable_ip = ip;
2254 curr->softirq_disable_event = ++curr->irq_events;
2255 debug_atomic_inc(&softirqs_off_events);
2256 DEBUG_LOCKS_WARN_ON(!softirq_count());
2257 } else
2258 debug_atomic_inc(&redundant_softirqs_off);
2259}
2260
Peter Zijlstra2f850182009-03-20 11:13:20 +01002261static void __lockdep_trace_alloc(gfp_t gfp_mask, unsigned long flags)
Nick Piggincf40bd12009-01-21 08:12:39 +01002262{
2263 struct task_struct *curr = current;
2264
2265 if (unlikely(!debug_locks))
2266 return;
2267
2268 /* no reclaim without waiting on it */
2269 if (!(gfp_mask & __GFP_WAIT))
2270 return;
2271
2272 /* this guy won't enter reclaim */
2273 if ((curr->flags & PF_MEMALLOC) && !(gfp_mask & __GFP_NOMEMALLOC))
2274 return;
2275
2276 /* We're only interested __GFP_FS allocations for now */
2277 if (!(gfp_mask & __GFP_FS))
2278 return;
2279
Peter Zijlstra2f850182009-03-20 11:13:20 +01002280 if (DEBUG_LOCKS_WARN_ON(irqs_disabled_flags(flags)))
Nick Piggincf40bd12009-01-21 08:12:39 +01002281 return;
2282
2283 mark_held_locks(curr, RECLAIM_FS);
2284}
2285
Peter Zijlstra2f850182009-03-20 11:13:20 +01002286static void check_flags(unsigned long flags);
2287
2288void lockdep_trace_alloc(gfp_t gfp_mask)
2289{
2290 unsigned long flags;
2291
2292 if (unlikely(current->lockdep_recursion))
2293 return;
2294
2295 raw_local_irq_save(flags);
2296 check_flags(flags);
2297 current->lockdep_recursion = 1;
2298 __lockdep_trace_alloc(gfp_mask, flags);
2299 current->lockdep_recursion = 0;
2300 raw_local_irq_restore(flags);
2301}
2302
Peter Zijlstra8e182572007-07-19 01:48:54 -07002303static int mark_irqflags(struct task_struct *curr, struct held_lock *hlock)
2304{
2305 /*
2306 * If non-trylock use in a hardirq or softirq context, then
2307 * mark the lock as used in these contexts:
2308 */
2309 if (!hlock->trylock) {
2310 if (hlock->read) {
2311 if (curr->hardirq_context)
2312 if (!mark_lock(curr, hlock,
2313 LOCK_USED_IN_HARDIRQ_READ))
2314 return 0;
2315 if (curr->softirq_context)
2316 if (!mark_lock(curr, hlock,
2317 LOCK_USED_IN_SOFTIRQ_READ))
2318 return 0;
2319 } else {
2320 if (curr->hardirq_context)
2321 if (!mark_lock(curr, hlock, LOCK_USED_IN_HARDIRQ))
2322 return 0;
2323 if (curr->softirq_context)
2324 if (!mark_lock(curr, hlock, LOCK_USED_IN_SOFTIRQ))
2325 return 0;
2326 }
2327 }
2328 if (!hlock->hardirqs_off) {
2329 if (hlock->read) {
2330 if (!mark_lock(curr, hlock,
Peter Zijlstra4fc95e82009-01-22 13:10:52 +01002331 LOCK_ENABLED_HARDIRQ_READ))
Peter Zijlstra8e182572007-07-19 01:48:54 -07002332 return 0;
2333 if (curr->softirqs_enabled)
2334 if (!mark_lock(curr, hlock,
Peter Zijlstra4fc95e82009-01-22 13:10:52 +01002335 LOCK_ENABLED_SOFTIRQ_READ))
Peter Zijlstra8e182572007-07-19 01:48:54 -07002336 return 0;
2337 } else {
2338 if (!mark_lock(curr, hlock,
Peter Zijlstra4fc95e82009-01-22 13:10:52 +01002339 LOCK_ENABLED_HARDIRQ))
Peter Zijlstra8e182572007-07-19 01:48:54 -07002340 return 0;
2341 if (curr->softirqs_enabled)
2342 if (!mark_lock(curr, hlock,
Peter Zijlstra4fc95e82009-01-22 13:10:52 +01002343 LOCK_ENABLED_SOFTIRQ))
Peter Zijlstra8e182572007-07-19 01:48:54 -07002344 return 0;
2345 }
2346 }
2347
Nick Piggincf40bd12009-01-21 08:12:39 +01002348 /*
2349 * We reuse the irq context infrastructure more broadly as a general
2350 * context checking code. This tests GFP_FS recursion (a lock taken
2351 * during reclaim for a GFP_FS allocation is held over a GFP_FS
2352 * allocation).
2353 */
2354 if (!hlock->trylock && (curr->lockdep_reclaim_gfp & __GFP_FS)) {
2355 if (hlock->read) {
2356 if (!mark_lock(curr, hlock, LOCK_USED_IN_RECLAIM_FS_READ))
2357 return 0;
2358 } else {
2359 if (!mark_lock(curr, hlock, LOCK_USED_IN_RECLAIM_FS))
2360 return 0;
2361 }
2362 }
2363
Peter Zijlstra8e182572007-07-19 01:48:54 -07002364 return 1;
2365}
2366
2367static int separate_irq_context(struct task_struct *curr,
2368 struct held_lock *hlock)
2369{
2370 unsigned int depth = curr->lockdep_depth;
2371
2372 /*
2373 * Keep track of points where we cross into an interrupt context:
2374 */
2375 hlock->irq_context = 2*(curr->hardirq_context ? 1 : 0) +
2376 curr->softirq_context;
2377 if (depth) {
2378 struct held_lock *prev_hlock;
2379
2380 prev_hlock = curr->held_locks + depth-1;
2381 /*
2382 * If we cross into another context, reset the
2383 * hash key (this also prevents the checking and the
2384 * adding of the dependency to 'prev'):
2385 */
2386 if (prev_hlock->irq_context != hlock->irq_context)
2387 return 1;
2388 }
2389 return 0;
2390}
2391
2392#else
2393
2394static inline
2395int mark_lock_irq(struct task_struct *curr, struct held_lock *this,
2396 enum lock_usage_bit new_bit)
2397{
2398 WARN_ON(1);
2399 return 1;
2400}
2401
2402static inline int mark_irqflags(struct task_struct *curr,
2403 struct held_lock *hlock)
2404{
2405 return 1;
2406}
2407
2408static inline int separate_irq_context(struct task_struct *curr,
2409 struct held_lock *hlock)
2410{
2411 return 0;
2412}
2413
Peter Zijlstra868a23a2009-02-15 00:25:21 +01002414void lockdep_trace_alloc(gfp_t gfp_mask)
2415{
2416}
2417
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002418#endif
2419
2420/*
Peter Zijlstra8e182572007-07-19 01:48:54 -07002421 * Mark a lock with a usage bit, and validate the state transition:
2422 */
Steven Rostedt1d09daa2008-05-12 21:20:55 +02002423static int mark_lock(struct task_struct *curr, struct held_lock *this,
Steven Rostedt0764d232008-05-12 21:20:44 +02002424 enum lock_usage_bit new_bit)
Peter Zijlstra8e182572007-07-19 01:48:54 -07002425{
2426 unsigned int new_mask = 1 << new_bit, ret = 1;
2427
2428 /*
2429 * If already set then do not dirty the cacheline,
2430 * nor do any checks:
2431 */
Dave Jonesf82b2172008-08-11 09:30:23 +02002432 if (likely(hlock_class(this)->usage_mask & new_mask))
Peter Zijlstra8e182572007-07-19 01:48:54 -07002433 return 1;
2434
2435 if (!graph_lock())
2436 return 0;
2437 /*
2438 * Make sure we didnt race:
2439 */
Dave Jonesf82b2172008-08-11 09:30:23 +02002440 if (unlikely(hlock_class(this)->usage_mask & new_mask)) {
Peter Zijlstra8e182572007-07-19 01:48:54 -07002441 graph_unlock();
2442 return 1;
2443 }
2444
Dave Jonesf82b2172008-08-11 09:30:23 +02002445 hlock_class(this)->usage_mask |= new_mask;
Peter Zijlstra8e182572007-07-19 01:48:54 -07002446
Dave Jonesf82b2172008-08-11 09:30:23 +02002447 if (!save_trace(hlock_class(this)->usage_traces + new_bit))
Peter Zijlstra8e182572007-07-19 01:48:54 -07002448 return 0;
2449
2450 switch (new_bit) {
Peter Zijlstra53464172009-01-22 14:15:53 +01002451#define LOCKDEP_STATE(__STATE) \
2452 case LOCK_USED_IN_##__STATE: \
2453 case LOCK_USED_IN_##__STATE##_READ: \
2454 case LOCK_ENABLED_##__STATE: \
2455 case LOCK_ENABLED_##__STATE##_READ:
2456#include "lockdep_states.h"
2457#undef LOCKDEP_STATE
Peter Zijlstra8e182572007-07-19 01:48:54 -07002458 ret = mark_lock_irq(curr, this, new_bit);
2459 if (!ret)
2460 return 0;
2461 break;
2462 case LOCK_USED:
Peter Zijlstra8e182572007-07-19 01:48:54 -07002463 debug_atomic_dec(&nr_unused_locks);
2464 break;
2465 default:
2466 if (!debug_locks_off_graph_unlock())
2467 return 0;
2468 WARN_ON(1);
2469 return 0;
2470 }
2471
2472 graph_unlock();
2473
2474 /*
2475 * We must printk outside of the graph_lock:
2476 */
2477 if (ret == 2) {
2478 printk("\nmarked lock as {%s}:\n", usage_str[new_bit]);
2479 print_lock(this);
2480 print_irqtrace_events(curr);
2481 dump_stack();
2482 }
2483
2484 return ret;
2485}
2486
2487/*
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002488 * Initialize a lock instance's lock-class mapping info:
2489 */
2490void lockdep_init_map(struct lockdep_map *lock, const char *name,
Peter Zijlstra4dfbb9d2006-10-11 01:45:14 -04002491 struct lock_class_key *key, int subclass)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002492{
2493 if (unlikely(!debug_locks))
2494 return;
2495
2496 if (DEBUG_LOCKS_WARN_ON(!key))
2497 return;
2498 if (DEBUG_LOCKS_WARN_ON(!name))
2499 return;
2500 /*
2501 * Sanity check, the lock-class key must be persistent:
2502 */
2503 if (!static_obj(key)) {
2504 printk("BUG: key %p not in .data!\n", key);
2505 DEBUG_LOCKS_WARN_ON(1);
2506 return;
2507 }
2508 lock->name = name;
2509 lock->key = key;
Ingo Molnard6d897c2006-07-10 04:44:04 -07002510 lock->class_cache = NULL;
Peter Zijlstra96645672007-07-19 01:49:00 -07002511#ifdef CONFIG_LOCK_STAT
2512 lock->cpu = raw_smp_processor_id();
2513#endif
Peter Zijlstra4dfbb9d2006-10-11 01:45:14 -04002514 if (subclass)
2515 register_lock_class(lock, subclass, 1);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002516}
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002517EXPORT_SYMBOL_GPL(lockdep_init_map);
2518
2519/*
2520 * This gets called for every mutex_lock*()/spin_lock*() operation.
2521 * We maintain the dependency maps and validate the locking attempt:
2522 */
2523static int __lock_acquire(struct lockdep_map *lock, unsigned int subclass,
2524 int trylock, int read, int check, int hardirqs_off,
Peter Zijlstra7531e2f2008-08-11 09:30:24 +02002525 struct lockdep_map *nest_lock, unsigned long ip)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002526{
2527 struct task_struct *curr = current;
Ingo Molnard6d897c2006-07-10 04:44:04 -07002528 struct lock_class *class = NULL;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002529 struct held_lock *hlock;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002530 unsigned int depth, id;
2531 int chain_head = 0;
2532 u64 chain_key;
2533
Peter Zijlstraf20786f2007-07-19 01:48:56 -07002534 if (!prove_locking)
2535 check = 1;
2536
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002537 if (unlikely(!debug_locks))
2538 return 0;
2539
2540 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2541 return 0;
2542
2543 if (unlikely(subclass >= MAX_LOCKDEP_SUBCLASSES)) {
2544 debug_locks_off();
2545 printk("BUG: MAX_LOCKDEP_SUBCLASSES too low!\n");
2546 printk("turning off the locking correctness validator.\n");
Peter Zijlstraeedeeab2009-03-18 12:38:47 +01002547 dump_stack();
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002548 return 0;
2549 }
2550
Ingo Molnard6d897c2006-07-10 04:44:04 -07002551 if (!subclass)
2552 class = lock->class_cache;
2553 /*
2554 * Not cached yet or subclass?
2555 */
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002556 if (unlikely(!class)) {
Peter Zijlstra4dfbb9d2006-10-11 01:45:14 -04002557 class = register_lock_class(lock, subclass, 0);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002558 if (!class)
2559 return 0;
2560 }
2561 debug_atomic_inc((atomic_t *)&class->ops);
2562 if (very_verbose(class)) {
2563 printk("\nacquire class [%p] %s", class->key, class->name);
2564 if (class->name_version > 1)
2565 printk("#%d", class->name_version);
2566 printk("\n");
2567 dump_stack();
2568 }
2569
2570 /*
2571 * Add the lock to the list of currently held locks.
2572 * (we dont increase the depth just yet, up until the
2573 * dependency checks are done)
2574 */
2575 depth = curr->lockdep_depth;
2576 if (DEBUG_LOCKS_WARN_ON(depth >= MAX_LOCK_DEPTH))
2577 return 0;
2578
2579 hlock = curr->held_locks + depth;
Dave Jonesf82b2172008-08-11 09:30:23 +02002580 if (DEBUG_LOCKS_WARN_ON(!class))
2581 return 0;
2582 hlock->class_idx = class - lock_classes + 1;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002583 hlock->acquire_ip = ip;
2584 hlock->instance = lock;
Peter Zijlstra7531e2f2008-08-11 09:30:24 +02002585 hlock->nest_lock = nest_lock;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002586 hlock->trylock = trylock;
2587 hlock->read = read;
2588 hlock->check = check;
Dmitry Baryshkov6951b122008-08-18 04:26:37 +04002589 hlock->hardirqs_off = !!hardirqs_off;
Peter Zijlstraf20786f2007-07-19 01:48:56 -07002590#ifdef CONFIG_LOCK_STAT
2591 hlock->waittime_stamp = 0;
2592 hlock->holdtime_stamp = sched_clock();
2593#endif
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002594
Peter Zijlstra8e182572007-07-19 01:48:54 -07002595 if (check == 2 && !mark_irqflags(curr, hlock))
2596 return 0;
2597
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002598 /* mark it as used: */
Jarek Poplawski4ff773bb2007-05-08 00:31:00 -07002599 if (!mark_lock(curr, hlock, LOCK_USED))
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002600 return 0;
Peter Zijlstra8e182572007-07-19 01:48:54 -07002601
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002602 /*
Gautham R Shenoy17aacfb2007-10-28 20:47:01 +01002603 * Calculate the chain hash: it's the combined hash of all the
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002604 * lock keys along the dependency chain. We save the hash value
2605 * at every step so that we can get the current hash easily
2606 * after unlock. The chain hash is then used to cache dependency
2607 * results.
2608 *
2609 * The 'key ID' is what is the most compact key value to drive
2610 * the hash, not class->key.
2611 */
2612 id = class - lock_classes;
2613 if (DEBUG_LOCKS_WARN_ON(id >= MAX_LOCKDEP_KEYS))
2614 return 0;
2615
2616 chain_key = curr->curr_chain_key;
2617 if (!depth) {
2618 if (DEBUG_LOCKS_WARN_ON(chain_key != 0))
2619 return 0;
2620 chain_head = 1;
2621 }
2622
2623 hlock->prev_chain_key = chain_key;
Peter Zijlstra8e182572007-07-19 01:48:54 -07002624 if (separate_irq_context(curr, hlock)) {
2625 chain_key = 0;
2626 chain_head = 1;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002627 }
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002628 chain_key = iterate_chain_key(chain_key, id);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002629
Gregory Haskins3aa416b2007-10-11 22:11:11 +02002630 if (!validate_chain(curr, lock, hlock, chain_head, chain_key))
Peter Zijlstra8e182572007-07-19 01:48:54 -07002631 return 0;
Jarek Poplawski381a2292007-02-10 01:44:58 -08002632
Gregory Haskins3aa416b2007-10-11 22:11:11 +02002633 curr->curr_chain_key = chain_key;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002634 curr->lockdep_depth++;
2635 check_chain_key(curr);
Jarek Poplawski60e114d2007-02-20 13:58:00 -08002636#ifdef CONFIG_DEBUG_LOCKDEP
2637 if (unlikely(!debug_locks))
2638 return 0;
2639#endif
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002640 if (unlikely(curr->lockdep_depth >= MAX_LOCK_DEPTH)) {
2641 debug_locks_off();
2642 printk("BUG: MAX_LOCK_DEPTH too low!\n");
2643 printk("turning off the locking correctness validator.\n");
Peter Zijlstraeedeeab2009-03-18 12:38:47 +01002644 dump_stack();
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002645 return 0;
2646 }
Jarek Poplawski381a2292007-02-10 01:44:58 -08002647
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002648 if (unlikely(curr->lockdep_depth > max_lockdep_depth))
2649 max_lockdep_depth = curr->lockdep_depth;
2650
2651 return 1;
2652}
2653
2654static int
2655print_unlock_inbalance_bug(struct task_struct *curr, struct lockdep_map *lock,
2656 unsigned long ip)
2657{
2658 if (!debug_locks_off())
2659 return 0;
2660 if (debug_locks_silent)
2661 return 0;
2662
2663 printk("\n=====================================\n");
2664 printk( "[ BUG: bad unlock balance detected! ]\n");
2665 printk( "-------------------------------------\n");
2666 printk("%s/%d is trying to release lock (",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07002667 curr->comm, task_pid_nr(curr));
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002668 print_lockdep_cache(lock);
2669 printk(") at:\n");
2670 print_ip_sym(ip);
2671 printk("but there are no more locks to release!\n");
2672 printk("\nother info that might help us debug this:\n");
2673 lockdep_print_held_locks(curr);
2674
2675 printk("\nstack backtrace:\n");
2676 dump_stack();
2677
2678 return 0;
2679}
2680
2681/*
2682 * Common debugging checks for both nested and non-nested unlock:
2683 */
2684static int check_unlock(struct task_struct *curr, struct lockdep_map *lock,
2685 unsigned long ip)
2686{
2687 if (unlikely(!debug_locks))
2688 return 0;
2689 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2690 return 0;
2691
2692 if (curr->lockdep_depth <= 0)
2693 return print_unlock_inbalance_bug(curr, lock, ip);
2694
2695 return 1;
2696}
2697
Peter Zijlstra64aa3482008-08-11 09:30:21 +02002698static int
Peter Zijlstra00ef9f72008-12-04 09:00:17 +01002699__lock_set_class(struct lockdep_map *lock, const char *name,
2700 struct lock_class_key *key, unsigned int subclass,
2701 unsigned long ip)
Peter Zijlstra64aa3482008-08-11 09:30:21 +02002702{
2703 struct task_struct *curr = current;
2704 struct held_lock *hlock, *prev_hlock;
2705 struct lock_class *class;
2706 unsigned int depth;
2707 int i;
2708
2709 depth = curr->lockdep_depth;
2710 if (DEBUG_LOCKS_WARN_ON(!depth))
2711 return 0;
2712
2713 prev_hlock = NULL;
2714 for (i = depth-1; i >= 0; i--) {
2715 hlock = curr->held_locks + i;
2716 /*
2717 * We must not cross into another context:
2718 */
2719 if (prev_hlock && prev_hlock->irq_context != hlock->irq_context)
2720 break;
2721 if (hlock->instance == lock)
2722 goto found_it;
2723 prev_hlock = hlock;
2724 }
2725 return print_unlock_inbalance_bug(curr, lock, ip);
2726
2727found_it:
Peter Zijlstra00ef9f72008-12-04 09:00:17 +01002728 lockdep_init_map(lock, name, key, 0);
Peter Zijlstra64aa3482008-08-11 09:30:21 +02002729 class = register_lock_class(lock, subclass, 0);
Dave Jonesf82b2172008-08-11 09:30:23 +02002730 hlock->class_idx = class - lock_classes + 1;
Peter Zijlstra64aa3482008-08-11 09:30:21 +02002731
2732 curr->lockdep_depth = i;
2733 curr->curr_chain_key = hlock->prev_chain_key;
2734
2735 for (; i < depth; i++) {
2736 hlock = curr->held_locks + i;
2737 if (!__lock_acquire(hlock->instance,
Dave Jonesf82b2172008-08-11 09:30:23 +02002738 hlock_class(hlock)->subclass, hlock->trylock,
Peter Zijlstra64aa3482008-08-11 09:30:21 +02002739 hlock->read, hlock->check, hlock->hardirqs_off,
Peter Zijlstra7531e2f2008-08-11 09:30:24 +02002740 hlock->nest_lock, hlock->acquire_ip))
Peter Zijlstra64aa3482008-08-11 09:30:21 +02002741 return 0;
2742 }
2743
2744 if (DEBUG_LOCKS_WARN_ON(curr->lockdep_depth != depth))
2745 return 0;
2746 return 1;
2747}
2748
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002749/*
2750 * Remove the lock to the list of currently held locks in a
2751 * potentially non-nested (out of order) manner. This is a
2752 * relatively rare operation, as all the unlock APIs default
2753 * to nested mode (which uses lock_release()):
2754 */
2755static int
2756lock_release_non_nested(struct task_struct *curr,
2757 struct lockdep_map *lock, unsigned long ip)
2758{
2759 struct held_lock *hlock, *prev_hlock;
2760 unsigned int depth;
2761 int i;
2762
2763 /*
2764 * Check whether the lock exists in the current stack
2765 * of held locks:
2766 */
2767 depth = curr->lockdep_depth;
2768 if (DEBUG_LOCKS_WARN_ON(!depth))
2769 return 0;
2770
2771 prev_hlock = NULL;
2772 for (i = depth-1; i >= 0; i--) {
2773 hlock = curr->held_locks + i;
2774 /*
2775 * We must not cross into another context:
2776 */
2777 if (prev_hlock && prev_hlock->irq_context != hlock->irq_context)
2778 break;
2779 if (hlock->instance == lock)
2780 goto found_it;
2781 prev_hlock = hlock;
2782 }
2783 return print_unlock_inbalance_bug(curr, lock, ip);
2784
2785found_it:
Peter Zijlstraf20786f2007-07-19 01:48:56 -07002786 lock_release_holdtime(hlock);
2787
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002788 /*
2789 * We have the right lock to unlock, 'hlock' points to it.
2790 * Now we remove it from the stack, and add back the other
2791 * entries (if any), recalculating the hash along the way:
2792 */
2793 curr->lockdep_depth = i;
2794 curr->curr_chain_key = hlock->prev_chain_key;
2795
2796 for (i++; i < depth; i++) {
2797 hlock = curr->held_locks + i;
2798 if (!__lock_acquire(hlock->instance,
Dave Jonesf82b2172008-08-11 09:30:23 +02002799 hlock_class(hlock)->subclass, hlock->trylock,
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002800 hlock->read, hlock->check, hlock->hardirqs_off,
Peter Zijlstra7531e2f2008-08-11 09:30:24 +02002801 hlock->nest_lock, hlock->acquire_ip))
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002802 return 0;
2803 }
2804
2805 if (DEBUG_LOCKS_WARN_ON(curr->lockdep_depth != depth - 1))
2806 return 0;
2807 return 1;
2808}
2809
2810/*
2811 * Remove the lock to the list of currently held locks - this gets
2812 * called on mutex_unlock()/spin_unlock*() (or on a failed
2813 * mutex_lock_interruptible()). This is done for unlocks that nest
2814 * perfectly. (i.e. the current top of the lock-stack is unlocked)
2815 */
2816static int lock_release_nested(struct task_struct *curr,
2817 struct lockdep_map *lock, unsigned long ip)
2818{
2819 struct held_lock *hlock;
2820 unsigned int depth;
2821
2822 /*
2823 * Pop off the top of the lock stack:
2824 */
2825 depth = curr->lockdep_depth - 1;
2826 hlock = curr->held_locks + depth;
2827
2828 /*
2829 * Is the unlock non-nested:
2830 */
2831 if (hlock->instance != lock)
2832 return lock_release_non_nested(curr, lock, ip);
2833 curr->lockdep_depth--;
2834
2835 if (DEBUG_LOCKS_WARN_ON(!depth && (hlock->prev_chain_key != 0)))
2836 return 0;
2837
2838 curr->curr_chain_key = hlock->prev_chain_key;
2839
Peter Zijlstraf20786f2007-07-19 01:48:56 -07002840 lock_release_holdtime(hlock);
2841
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002842#ifdef CONFIG_DEBUG_LOCKDEP
2843 hlock->prev_chain_key = 0;
Dave Jonesf82b2172008-08-11 09:30:23 +02002844 hlock->class_idx = 0;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002845 hlock->acquire_ip = 0;
2846 hlock->irq_context = 0;
2847#endif
2848 return 1;
2849}
2850
2851/*
2852 * Remove the lock to the list of currently held locks - this gets
2853 * called on mutex_unlock()/spin_unlock*() (or on a failed
2854 * mutex_lock_interruptible()). This is done for unlocks that nest
2855 * perfectly. (i.e. the current top of the lock-stack is unlocked)
2856 */
2857static void
2858__lock_release(struct lockdep_map *lock, int nested, unsigned long ip)
2859{
2860 struct task_struct *curr = current;
2861
2862 if (!check_unlock(curr, lock, ip))
2863 return;
2864
2865 if (nested) {
2866 if (!lock_release_nested(curr, lock, ip))
2867 return;
2868 } else {
2869 if (!lock_release_non_nested(curr, lock, ip))
2870 return;
2871 }
2872
2873 check_chain_key(curr);
2874}
2875
2876/*
2877 * Check whether we follow the irq-flags state precisely:
2878 */
Steven Rostedt1d09daa2008-05-12 21:20:55 +02002879static void check_flags(unsigned long flags)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002880{
Ingo Molnar992860e2008-07-14 10:28:38 +02002881#if defined(CONFIG_PROVE_LOCKING) && defined(CONFIG_DEBUG_LOCKDEP) && \
2882 defined(CONFIG_TRACE_IRQFLAGS)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002883 if (!debug_locks)
2884 return;
2885
Ingo Molnar5f9fa8a2007-12-07 19:02:47 +01002886 if (irqs_disabled_flags(flags)) {
2887 if (DEBUG_LOCKS_WARN_ON(current->hardirqs_enabled)) {
2888 printk("possible reason: unannotated irqs-off.\n");
2889 }
2890 } else {
2891 if (DEBUG_LOCKS_WARN_ON(!current->hardirqs_enabled)) {
2892 printk("possible reason: unannotated irqs-on.\n");
2893 }
2894 }
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002895
2896 /*
2897 * We dont accurately track softirq state in e.g.
2898 * hardirq contexts (such as on 4KSTACKS), so only
2899 * check if not in hardirq contexts:
2900 */
2901 if (!hardirq_count()) {
2902 if (softirq_count())
2903 DEBUG_LOCKS_WARN_ON(current->softirqs_enabled);
2904 else
2905 DEBUG_LOCKS_WARN_ON(!current->softirqs_enabled);
2906 }
2907
2908 if (!debug_locks)
2909 print_irqtrace_events(current);
2910#endif
2911}
2912
Peter Zijlstra00ef9f72008-12-04 09:00:17 +01002913void lock_set_class(struct lockdep_map *lock, const char *name,
2914 struct lock_class_key *key, unsigned int subclass,
2915 unsigned long ip)
Peter Zijlstra64aa3482008-08-11 09:30:21 +02002916{
2917 unsigned long flags;
2918
2919 if (unlikely(current->lockdep_recursion))
2920 return;
2921
2922 raw_local_irq_save(flags);
2923 current->lockdep_recursion = 1;
2924 check_flags(flags);
Peter Zijlstra00ef9f72008-12-04 09:00:17 +01002925 if (__lock_set_class(lock, name, key, subclass, ip))
Peter Zijlstra64aa3482008-08-11 09:30:21 +02002926 check_chain_key(current);
2927 current->lockdep_recursion = 0;
2928 raw_local_irq_restore(flags);
2929}
Peter Zijlstra00ef9f72008-12-04 09:00:17 +01002930EXPORT_SYMBOL_GPL(lock_set_class);
Peter Zijlstra64aa3482008-08-11 09:30:21 +02002931
Peter Zijlstraefed7922009-03-04 12:32:55 +01002932DEFINE_TRACE(lock_acquire);
2933
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002934/*
2935 * We are not always called with irqs disabled - do that here,
2936 * and also avoid lockdep recursion:
2937 */
Steven Rostedt1d09daa2008-05-12 21:20:55 +02002938void lock_acquire(struct lockdep_map *lock, unsigned int subclass,
Peter Zijlstra7531e2f2008-08-11 09:30:24 +02002939 int trylock, int read, int check,
2940 struct lockdep_map *nest_lock, unsigned long ip)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002941{
2942 unsigned long flags;
2943
Peter Zijlstraefed7922009-03-04 12:32:55 +01002944 trace_lock_acquire(lock, subclass, trylock, read, check, nest_lock, ip);
2945
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002946 if (unlikely(current->lockdep_recursion))
2947 return;
2948
2949 raw_local_irq_save(flags);
2950 check_flags(flags);
2951
2952 current->lockdep_recursion = 1;
2953 __lock_acquire(lock, subclass, trylock, read, check,
Peter Zijlstra7531e2f2008-08-11 09:30:24 +02002954 irqs_disabled_flags(flags), nest_lock, ip);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002955 current->lockdep_recursion = 0;
2956 raw_local_irq_restore(flags);
2957}
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002958EXPORT_SYMBOL_GPL(lock_acquire);
2959
Peter Zijlstraefed7922009-03-04 12:32:55 +01002960DEFINE_TRACE(lock_release);
2961
Steven Rostedt1d09daa2008-05-12 21:20:55 +02002962void lock_release(struct lockdep_map *lock, int nested,
Steven Rostedt0764d232008-05-12 21:20:44 +02002963 unsigned long ip)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002964{
2965 unsigned long flags;
2966
Peter Zijlstraefed7922009-03-04 12:32:55 +01002967 trace_lock_release(lock, nested, ip);
2968
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002969 if (unlikely(current->lockdep_recursion))
2970 return;
2971
2972 raw_local_irq_save(flags);
2973 check_flags(flags);
2974 current->lockdep_recursion = 1;
2975 __lock_release(lock, nested, ip);
2976 current->lockdep_recursion = 0;
2977 raw_local_irq_restore(flags);
2978}
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002979EXPORT_SYMBOL_GPL(lock_release);
2980
Nick Piggincf40bd12009-01-21 08:12:39 +01002981void lockdep_set_current_reclaim_state(gfp_t gfp_mask)
2982{
2983 current->lockdep_reclaim_gfp = gfp_mask;
2984}
2985
2986void lockdep_clear_current_reclaim_state(void)
2987{
2988 current->lockdep_reclaim_gfp = 0;
2989}
2990
Peter Zijlstraf20786f2007-07-19 01:48:56 -07002991#ifdef CONFIG_LOCK_STAT
2992static int
2993print_lock_contention_bug(struct task_struct *curr, struct lockdep_map *lock,
2994 unsigned long ip)
2995{
2996 if (!debug_locks_off())
2997 return 0;
2998 if (debug_locks_silent)
2999 return 0;
3000
3001 printk("\n=================================\n");
3002 printk( "[ BUG: bad contention detected! ]\n");
3003 printk( "---------------------------------\n");
3004 printk("%s/%d is trying to contend lock (",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07003005 curr->comm, task_pid_nr(curr));
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003006 print_lockdep_cache(lock);
3007 printk(") at:\n");
3008 print_ip_sym(ip);
3009 printk("but there are no locks held!\n");
3010 printk("\nother info that might help us debug this:\n");
3011 lockdep_print_held_locks(curr);
3012
3013 printk("\nstack backtrace:\n");
3014 dump_stack();
3015
3016 return 0;
3017}
3018
3019static void
3020__lock_contended(struct lockdep_map *lock, unsigned long ip)
3021{
3022 struct task_struct *curr = current;
3023 struct held_lock *hlock, *prev_hlock;
3024 struct lock_class_stats *stats;
3025 unsigned int depth;
Peter Zijlstrac7e78cf2008-10-16 23:17:09 +02003026 int i, contention_point, contending_point;
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003027
3028 depth = curr->lockdep_depth;
3029 if (DEBUG_LOCKS_WARN_ON(!depth))
3030 return;
3031
3032 prev_hlock = NULL;
3033 for (i = depth-1; i >= 0; i--) {
3034 hlock = curr->held_locks + i;
3035 /*
3036 * We must not cross into another context:
3037 */
3038 if (prev_hlock && prev_hlock->irq_context != hlock->irq_context)
3039 break;
3040 if (hlock->instance == lock)
3041 goto found_it;
3042 prev_hlock = hlock;
3043 }
3044 print_lock_contention_bug(curr, lock, ip);
3045 return;
3046
3047found_it:
3048 hlock->waittime_stamp = sched_clock();
3049
Peter Zijlstrac7e78cf2008-10-16 23:17:09 +02003050 contention_point = lock_point(hlock_class(hlock)->contention_point, ip);
3051 contending_point = lock_point(hlock_class(hlock)->contending_point,
3052 lock->ip);
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003053
Dave Jonesf82b2172008-08-11 09:30:23 +02003054 stats = get_lock_stats(hlock_class(hlock));
Peter Zijlstrac7e78cf2008-10-16 23:17:09 +02003055 if (contention_point < LOCKSTAT_POINTS)
3056 stats->contention_point[contention_point]++;
3057 if (contending_point < LOCKSTAT_POINTS)
3058 stats->contending_point[contending_point]++;
Peter Zijlstra96645672007-07-19 01:49:00 -07003059 if (lock->cpu != smp_processor_id())
3060 stats->bounces[bounce_contended + !!hlock->read]++;
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003061 put_lock_stats(stats);
3062}
3063
Frederic Weisbecker20625012009-04-06 01:49:33 +02003064DEFINE_TRACE(lock_acquired);
3065
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003066static void
Peter Zijlstrac7e78cf2008-10-16 23:17:09 +02003067__lock_acquired(struct lockdep_map *lock, unsigned long ip)
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003068{
3069 struct task_struct *curr = current;
3070 struct held_lock *hlock, *prev_hlock;
3071 struct lock_class_stats *stats;
3072 unsigned int depth;
3073 u64 now;
Peter Zijlstra96645672007-07-19 01:49:00 -07003074 s64 waittime = 0;
3075 int i, cpu;
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003076
3077 depth = curr->lockdep_depth;
3078 if (DEBUG_LOCKS_WARN_ON(!depth))
3079 return;
3080
3081 prev_hlock = NULL;
3082 for (i = depth-1; i >= 0; i--) {
3083 hlock = curr->held_locks + i;
3084 /*
3085 * We must not cross into another context:
3086 */
3087 if (prev_hlock && prev_hlock->irq_context != hlock->irq_context)
3088 break;
3089 if (hlock->instance == lock)
3090 goto found_it;
3091 prev_hlock = hlock;
3092 }
3093 print_lock_contention_bug(curr, lock, _RET_IP_);
3094 return;
3095
3096found_it:
Peter Zijlstra96645672007-07-19 01:49:00 -07003097 cpu = smp_processor_id();
3098 if (hlock->waittime_stamp) {
3099 now = sched_clock();
3100 waittime = now - hlock->waittime_stamp;
3101 hlock->holdtime_stamp = now;
3102 }
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003103
Frederic Weisbecker20625012009-04-06 01:49:33 +02003104 trace_lock_acquired(lock, ip, waittime);
3105
Dave Jonesf82b2172008-08-11 09:30:23 +02003106 stats = get_lock_stats(hlock_class(hlock));
Peter Zijlstra96645672007-07-19 01:49:00 -07003107 if (waittime) {
3108 if (hlock->read)
3109 lock_time_inc(&stats->read_waittime, waittime);
3110 else
3111 lock_time_inc(&stats->write_waittime, waittime);
3112 }
3113 if (lock->cpu != cpu)
3114 stats->bounces[bounce_acquired + !!hlock->read]++;
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003115 put_lock_stats(stats);
Peter Zijlstra96645672007-07-19 01:49:00 -07003116
3117 lock->cpu = cpu;
Peter Zijlstrac7e78cf2008-10-16 23:17:09 +02003118 lock->ip = ip;
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003119}
3120
Peter Zijlstraefed7922009-03-04 12:32:55 +01003121DEFINE_TRACE(lock_contended);
3122
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003123void lock_contended(struct lockdep_map *lock, unsigned long ip)
3124{
3125 unsigned long flags;
3126
Peter Zijlstraefed7922009-03-04 12:32:55 +01003127 trace_lock_contended(lock, ip);
3128
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003129 if (unlikely(!lock_stat))
3130 return;
3131
3132 if (unlikely(current->lockdep_recursion))
3133 return;
3134
3135 raw_local_irq_save(flags);
3136 check_flags(flags);
3137 current->lockdep_recursion = 1;
3138 __lock_contended(lock, ip);
3139 current->lockdep_recursion = 0;
3140 raw_local_irq_restore(flags);
3141}
3142EXPORT_SYMBOL_GPL(lock_contended);
3143
Peter Zijlstrac7e78cf2008-10-16 23:17:09 +02003144void lock_acquired(struct lockdep_map *lock, unsigned long ip)
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003145{
3146 unsigned long flags;
3147
3148 if (unlikely(!lock_stat))
3149 return;
3150
3151 if (unlikely(current->lockdep_recursion))
3152 return;
3153
3154 raw_local_irq_save(flags);
3155 check_flags(flags);
3156 current->lockdep_recursion = 1;
Peter Zijlstrac7e78cf2008-10-16 23:17:09 +02003157 __lock_acquired(lock, ip);
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003158 current->lockdep_recursion = 0;
3159 raw_local_irq_restore(flags);
3160}
3161EXPORT_SYMBOL_GPL(lock_acquired);
3162#endif
3163
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003164/*
3165 * Used by the testsuite, sanitize the validator state
3166 * after a simulated failure:
3167 */
3168
3169void lockdep_reset(void)
3170{
3171 unsigned long flags;
Ingo Molnar23d95a02006-12-13 00:34:40 -08003172 int i;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003173
3174 raw_local_irq_save(flags);
3175 current->curr_chain_key = 0;
3176 current->lockdep_depth = 0;
3177 current->lockdep_recursion = 0;
3178 memset(current->held_locks, 0, MAX_LOCK_DEPTH*sizeof(struct held_lock));
3179 nr_hardirq_chains = 0;
3180 nr_softirq_chains = 0;
3181 nr_process_chains = 0;
3182 debug_locks = 1;
Ingo Molnar23d95a02006-12-13 00:34:40 -08003183 for (i = 0; i < CHAINHASH_SIZE; i++)
3184 INIT_LIST_HEAD(chainhash_table + i);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003185 raw_local_irq_restore(flags);
3186}
3187
3188static void zap_class(struct lock_class *class)
3189{
3190 int i;
3191
3192 /*
3193 * Remove all dependencies this lock is
3194 * involved in:
3195 */
3196 for (i = 0; i < nr_list_entries; i++) {
3197 if (list_entries[i].class == class)
3198 list_del_rcu(&list_entries[i].entry);
3199 }
3200 /*
3201 * Unhash the class and remove it from the all_lock_classes list:
3202 */
3203 list_del_rcu(&class->hash_entry);
3204 list_del_rcu(&class->lock_entry);
3205
Rabin Vincent8bfe0292008-08-11 09:30:26 +02003206 class->key = NULL;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003207}
3208
Arjan van de Venfabe8742008-01-24 07:00:45 +01003209static inline int within(const void *addr, void *start, unsigned long size)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003210{
3211 return addr >= start && addr < start + size;
3212}
3213
3214void lockdep_free_key_range(void *start, unsigned long size)
3215{
3216 struct lock_class *class, *next;
3217 struct list_head *head;
3218 unsigned long flags;
3219 int i;
Nick Piggin5a26db52008-01-16 09:51:58 +01003220 int locked;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003221
3222 raw_local_irq_save(flags);
Nick Piggin5a26db52008-01-16 09:51:58 +01003223 locked = graph_lock();
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003224
3225 /*
3226 * Unhash all classes that were created by this module:
3227 */
3228 for (i = 0; i < CLASSHASH_SIZE; i++) {
3229 head = classhash_table + i;
3230 if (list_empty(head))
3231 continue;
Arjan van de Venfabe8742008-01-24 07:00:45 +01003232 list_for_each_entry_safe(class, next, head, hash_entry) {
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003233 if (within(class->key, start, size))
3234 zap_class(class);
Arjan van de Venfabe8742008-01-24 07:00:45 +01003235 else if (within(class->name, start, size))
3236 zap_class(class);
3237 }
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003238 }
3239
Nick Piggin5a26db52008-01-16 09:51:58 +01003240 if (locked)
3241 graph_unlock();
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003242 raw_local_irq_restore(flags);
3243}
3244
3245void lockdep_reset_lock(struct lockdep_map *lock)
3246{
Ingo Molnard6d897c2006-07-10 04:44:04 -07003247 struct lock_class *class, *next;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003248 struct list_head *head;
3249 unsigned long flags;
3250 int i, j;
Nick Piggin5a26db52008-01-16 09:51:58 +01003251 int locked;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003252
3253 raw_local_irq_save(flags);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003254
3255 /*
Ingo Molnard6d897c2006-07-10 04:44:04 -07003256 * Remove all classes this lock might have:
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003257 */
Ingo Molnard6d897c2006-07-10 04:44:04 -07003258 for (j = 0; j < MAX_LOCKDEP_SUBCLASSES; j++) {
3259 /*
3260 * If the class exists we look it up and zap it:
3261 */
3262 class = look_up_lock_class(lock, j);
3263 if (class)
3264 zap_class(class);
3265 }
3266 /*
3267 * Debug check: in the end all mapped classes should
3268 * be gone.
3269 */
Nick Piggin5a26db52008-01-16 09:51:58 +01003270 locked = graph_lock();
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003271 for (i = 0; i < CLASSHASH_SIZE; i++) {
3272 head = classhash_table + i;
3273 if (list_empty(head))
3274 continue;
3275 list_for_each_entry_safe(class, next, head, hash_entry) {
Ingo Molnard6d897c2006-07-10 04:44:04 -07003276 if (unlikely(class == lock->class_cache)) {
Ingo Molnar74c383f2006-12-13 00:34:43 -08003277 if (debug_locks_off_graph_unlock())
3278 WARN_ON(1);
Ingo Molnard6d897c2006-07-10 04:44:04 -07003279 goto out_restore;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003280 }
3281 }
3282 }
Nick Piggin5a26db52008-01-16 09:51:58 +01003283 if (locked)
3284 graph_unlock();
Ingo Molnard6d897c2006-07-10 04:44:04 -07003285
3286out_restore:
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003287 raw_local_irq_restore(flags);
3288}
3289
Sam Ravnborg14999932007-02-28 20:12:31 -08003290void lockdep_init(void)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003291{
3292 int i;
3293
3294 /*
3295 * Some architectures have their own start_kernel()
3296 * code which calls lockdep_init(), while we also
3297 * call lockdep_init() from the start_kernel() itself,
3298 * and we want to initialize the hashes only once:
3299 */
3300 if (lockdep_initialized)
3301 return;
3302
3303 for (i = 0; i < CLASSHASH_SIZE; i++)
3304 INIT_LIST_HEAD(classhash_table + i);
3305
3306 for (i = 0; i < CHAINHASH_SIZE; i++)
3307 INIT_LIST_HEAD(chainhash_table + i);
3308
3309 lockdep_initialized = 1;
3310}
3311
3312void __init lockdep_info(void)
3313{
3314 printk("Lock dependency validator: Copyright (c) 2006 Red Hat, Inc., Ingo Molnar\n");
3315
Li Zefanb0788ca2008-11-21 15:57:32 +08003316 printk("... MAX_LOCKDEP_SUBCLASSES: %lu\n", MAX_LOCKDEP_SUBCLASSES);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003317 printk("... MAX_LOCK_DEPTH: %lu\n", MAX_LOCK_DEPTH);
3318 printk("... MAX_LOCKDEP_KEYS: %lu\n", MAX_LOCKDEP_KEYS);
Li Zefanb0788ca2008-11-21 15:57:32 +08003319 printk("... CLASSHASH_SIZE: %lu\n", CLASSHASH_SIZE);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003320 printk("... MAX_LOCKDEP_ENTRIES: %lu\n", MAX_LOCKDEP_ENTRIES);
3321 printk("... MAX_LOCKDEP_CHAINS: %lu\n", MAX_LOCKDEP_CHAINS);
3322 printk("... CHAINHASH_SIZE: %lu\n", CHAINHASH_SIZE);
3323
3324 printk(" memory used by lock dependency info: %lu kB\n",
3325 (sizeof(struct lock_class) * MAX_LOCKDEP_KEYS +
3326 sizeof(struct list_head) * CLASSHASH_SIZE +
3327 sizeof(struct lock_list) * MAX_LOCKDEP_ENTRIES +
3328 sizeof(struct lock_chain) * MAX_LOCKDEP_CHAINS +
3329 sizeof(struct list_head) * CHAINHASH_SIZE) / 1024);
3330
3331 printk(" per task-struct memory footprint: %lu bytes\n",
3332 sizeof(struct held_lock) * MAX_LOCK_DEPTH);
3333
3334#ifdef CONFIG_DEBUG_LOCKDEP
Johannes Bergc71063c2007-07-19 01:49:02 -07003335 if (lockdep_init_error) {
3336 printk("WARNING: lockdep init error! Arch code didn't call lockdep_init() early enough?\n");
3337 printk("Call stack leading to lockdep invocation was:\n");
3338 print_stack_trace(&lockdep_init_trace, 0);
3339 }
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003340#endif
3341}
3342
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003343static void
3344print_freed_lock_bug(struct task_struct *curr, const void *mem_from,
Arjan van de Ven55794a42006-07-10 04:44:03 -07003345 const void *mem_to, struct held_lock *hlock)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003346{
3347 if (!debug_locks_off())
3348 return;
3349 if (debug_locks_silent)
3350 return;
3351
3352 printk("\n=========================\n");
3353 printk( "[ BUG: held lock freed! ]\n");
3354 printk( "-------------------------\n");
3355 printk("%s/%d is freeing memory %p-%p, with a lock still held there!\n",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07003356 curr->comm, task_pid_nr(curr), mem_from, mem_to-1);
Arjan van de Ven55794a42006-07-10 04:44:03 -07003357 print_lock(hlock);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003358 lockdep_print_held_locks(curr);
3359
3360 printk("\nstack backtrace:\n");
3361 dump_stack();
3362}
3363
Oleg Nesterov54561782007-12-05 15:46:09 +01003364static inline int not_in_range(const void* mem_from, unsigned long mem_len,
3365 const void* lock_from, unsigned long lock_len)
3366{
3367 return lock_from + lock_len <= mem_from ||
3368 mem_from + mem_len <= lock_from;
3369}
3370
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003371/*
3372 * Called when kernel memory is freed (or unmapped), or if a lock
3373 * is destroyed or reinitialized - this code checks whether there is
3374 * any held lock in the memory range of <from> to <to>:
3375 */
3376void debug_check_no_locks_freed(const void *mem_from, unsigned long mem_len)
3377{
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003378 struct task_struct *curr = current;
3379 struct held_lock *hlock;
3380 unsigned long flags;
3381 int i;
3382
3383 if (unlikely(!debug_locks))
3384 return;
3385
3386 local_irq_save(flags);
3387 for (i = 0; i < curr->lockdep_depth; i++) {
3388 hlock = curr->held_locks + i;
3389
Oleg Nesterov54561782007-12-05 15:46:09 +01003390 if (not_in_range(mem_from, mem_len, hlock->instance,
3391 sizeof(*hlock->instance)))
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003392 continue;
3393
Oleg Nesterov54561782007-12-05 15:46:09 +01003394 print_freed_lock_bug(curr, mem_from, mem_from + mem_len, hlock);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003395 break;
3396 }
3397 local_irq_restore(flags);
3398}
Peter Zijlstraed075362006-12-06 20:35:24 -08003399EXPORT_SYMBOL_GPL(debug_check_no_locks_freed);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003400
3401static void print_held_locks_bug(struct task_struct *curr)
3402{
3403 if (!debug_locks_off())
3404 return;
3405 if (debug_locks_silent)
3406 return;
3407
3408 printk("\n=====================================\n");
3409 printk( "[ BUG: lock held at task exit time! ]\n");
3410 printk( "-------------------------------------\n");
3411 printk("%s/%d is exiting with locks still held!\n",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07003412 curr->comm, task_pid_nr(curr));
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003413 lockdep_print_held_locks(curr);
3414
3415 printk("\nstack backtrace:\n");
3416 dump_stack();
3417}
3418
3419void debug_check_no_locks_held(struct task_struct *task)
3420{
3421 if (unlikely(task->lockdep_depth > 0))
3422 print_held_locks_bug(task);
3423}
3424
3425void debug_show_all_locks(void)
3426{
3427 struct task_struct *g, *p;
3428 int count = 10;
3429 int unlock = 1;
3430
Jarek Poplawski9c35dd72007-03-22 00:11:28 -08003431 if (unlikely(!debug_locks)) {
3432 printk("INFO: lockdep is turned off.\n");
3433 return;
3434 }
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003435 printk("\nShowing all locks held in the system:\n");
3436
3437 /*
3438 * Here we try to get the tasklist_lock as hard as possible,
3439 * if not successful after 2 seconds we ignore it (but keep
3440 * trying). This is to enable a debug printout even if a
3441 * tasklist_lock-holding task deadlocks or crashes.
3442 */
3443retry:
3444 if (!read_trylock(&tasklist_lock)) {
3445 if (count == 10)
3446 printk("hm, tasklist_lock locked, retrying... ");
3447 if (count) {
3448 count--;
3449 printk(" #%d", 10-count);
3450 mdelay(200);
3451 goto retry;
3452 }
3453 printk(" ignoring it.\n");
3454 unlock = 0;
qinghuang feng46fec7a2008-10-28 17:24:28 +08003455 } else {
3456 if (count != 10)
3457 printk(KERN_CONT " locked it.\n");
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003458 }
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003459
3460 do_each_thread(g, p) {
Ingo Molnar85684872007-12-05 15:46:09 +01003461 /*
3462 * It's not reliable to print a task's held locks
3463 * if it's not sleeping (or if it's not the current
3464 * task):
3465 */
3466 if (p->state == TASK_RUNNING && p != current)
3467 continue;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003468 if (p->lockdep_depth)
3469 lockdep_print_held_locks(p);
3470 if (!unlock)
3471 if (read_trylock(&tasklist_lock))
3472 unlock = 1;
3473 } while_each_thread(g, p);
3474
3475 printk("\n");
3476 printk("=============================================\n\n");
3477
3478 if (unlock)
3479 read_unlock(&tasklist_lock);
3480}
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003481EXPORT_SYMBOL_GPL(debug_show_all_locks);
3482
Ingo Molnar82a1fcb2008-01-25 21:08:02 +01003483/*
3484 * Careful: only use this function if you are sure that
3485 * the task cannot run in parallel!
3486 */
3487void __debug_show_held_locks(struct task_struct *task)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003488{
Jarek Poplawski9c35dd72007-03-22 00:11:28 -08003489 if (unlikely(!debug_locks)) {
3490 printk("INFO: lockdep is turned off.\n");
3491 return;
3492 }
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003493 lockdep_print_held_locks(task);
3494}
Ingo Molnar82a1fcb2008-01-25 21:08:02 +01003495EXPORT_SYMBOL_GPL(__debug_show_held_locks);
3496
3497void debug_show_held_locks(struct task_struct *task)
3498{
3499 __debug_show_held_locks(task);
3500}
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003501EXPORT_SYMBOL_GPL(debug_show_held_locks);
Peter Zijlstrab351d162007-10-11 22:11:12 +02003502
3503void lockdep_sys_exit(void)
3504{
3505 struct task_struct *curr = current;
3506
3507 if (unlikely(curr->lockdep_depth)) {
3508 if (!debug_locks_off())
3509 return;
3510 printk("\n================================================\n");
3511 printk( "[ BUG: lock held when returning to user space! ]\n");
3512 printk( "------------------------------------------------\n");
3513 printk("%s/%d is leaving the kernel with locks still held!\n",
3514 curr->comm, curr->pid);
3515 lockdep_print_held_locks(curr);
3516 }
3517}