blob: 71b567f5281340c7d2ea77b6097a86e41b583f1d [file] [log] [blame]
Ingo Molnarfbb9ce92006-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 Molnarfbb9ce92006-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 Molnarfbb9ce92006-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 Molnarfbb9ce92006-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 Molnarfbb9ce92006-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 Molnarfbb9ce92006-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 Molnarfbb9ce92006-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 Molnarfbb9ce92006-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 Molnarfbb9ce92006-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 Molnarfbb9ce92006-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 Molnarfbb9ce92006-07-03 00:24:50 -0700267#define classhashentry(key) (classhash_table + __classhashfn((key)))
268
269static struct list_head classhash_table[CLASSHASH_SIZE];
270
Ingo Molnarfbb9ce92006-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 Molnarfbb9ce92006-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 Molnarfbb9ce92006-07-03 00:24:50 -0700291 (key2))
292
Steven Rostedt1d09daa2008-05-12 21:20:55 +0200293void lockdep_off(void)
Ingo Molnarfbb9ce92006-07-03 00:24:50 -0700294{
295 current->lockdep_recursion++;
296}
Ingo Molnarfbb9ce92006-07-03 00:24:50 -0700297EXPORT_SYMBOL(lockdep_off);
298
Steven Rostedt1d09daa2008-05-12 21:20:55 +0200299void lockdep_on(void)
Ingo Molnarfbb9ce92006-07-03 00:24:50 -0700300{
301 current->lockdep_recursion--;
302}
Ingo Molnarfbb9ce92006-07-03 00:24:50 -0700303EXPORT_SYMBOL(lockdep_on);
304
Ingo Molnarfbb9ce92006-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 Molnarfbb9ce92006-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 Molnarfbb9ce92006-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 Molnarfbb9ce92006-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 Molnarfbb9ce92006-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 Molnarfbb9ce92006-07-03 00:24:50 -0700330 if (class->name_version == 1 &&
Andi Kleenf9829cc2006-07-10 04:44:01 -0700331 !strcmp(class->name, "lockname"))
Ingo Molnarfbb9ce92006-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 Molnarfbb9ce92006-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 Molnarfbb9ce92006-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 Molnarfbb9ce92006-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 Molnarfbb9ce92006-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 Molnarfbb9ce92006-07-03 00:24:50 -0700366
367 trace->max_entries = trace->nr_entries;
368
369 nr_stack_trace_entries += trace->nr_entries;
Ingo Molnarfbb9ce92006-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 Molnarfbb9ce92006-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 Molnarfbb9ce92006-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 Molnarfbb9ce92006-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 Molnarfbb9ce92006-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 Molnarfbb9ce92006-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 Molnarfbb9ce92006-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 Molnarfbb9ce92006-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 Molnarfbb9ce92006-07-03 00:24:50 -0700483{
Peter Zijlstraf510b232009-01-22 17:53:47 +0100484 int i = 0;
Ingo Molnarfbb9ce92006-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 Molnarfbb9ce92006-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 Molnarfbb9ce92006-07-03 00:24:50 -0700498 const char *name;
499
Peter Zijlstraf510b232009-01-22 17:53:47 +0100500 get_usage_chars(class, usage);
Ingo Molnarfbb9ce92006-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 Molnarfbb9ce92006-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 Molnarfbb9ce92006-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 Molnarfbb9ce92006-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 Molnarfbb9ce92006-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 Molnarfbb9ce92006-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 Molnarfbb9ce92006-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 Molnarfbb9ce92006-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 Molnarfbb9ce92006-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 Molnarfbb9ce92006-07-03 00:24:50 -0700571
Andi Kleenf9829cc2006-07-10 04:44:01 -0700572 printk("%*s ... key at: ",depth,"");
Ingo Molnarfbb9ce92006-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 Molnarfbb9ce92006-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 Molnarfbb9ce92006-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 Molnarfbb9ce92006-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 Molnarfbb9ce92006-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 Molnarfbb9ce92006-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 Molnarfbb9ce92006-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 Molnarfbb9ce92006-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 Molnarfbb9ce92006-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 Molnarfbb9ce92006-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 Molnarfbb9ce92006-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 Molnarfbb9ce92006-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 Molnarfbb9ce92006-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 Molnarfbb9ce92006-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 Molnarfbb9ce92006-07-03 00:24:50 -0700794 printk("BUG: MAX_LOCKDEP_KEYS too low!\n");
795 printk("turning off the locking correctness validator.\n");
796 return NULL;
797 }
798 class = lock_classes + nr_lock_classes++;
799 debug_atomic_inc(&nr_unused_locks);
800 class->key = key;
801 class->name = lock->name;
802 class->subclass = subclass;
803 INIT_LIST_HEAD(&class->lock_entry);
804 INIT_LIST_HEAD(&class->locks_before);
805 INIT_LIST_HEAD(&class->locks_after);
806 class->name_version = count_matching_names(class);
807 /*
808 * We use RCU's safe list-add method to make
809 * parallel walking of the hash-list safe:
810 */
811 list_add_tail_rcu(&class->hash_entry, hash_head);
Dale Farnsworth14811972008-02-25 23:03:02 +0100812 /*
813 * Add it to the global list of classes:
814 */
815 list_add_tail_rcu(&class->lock_entry, &all_lock_classes);
Ingo Molnarfbb9ce92006-07-03 00:24:50 -0700816
817 if (verbose(class)) {
Ingo Molnar74c383f2006-12-13 00:34:43 -0800818 graph_unlock();
Ingo Molnar70e45062006-12-06 20:40:50 -0800819 raw_local_irq_restore(flags);
Ingo Molnar74c383f2006-12-13 00:34:43 -0800820
Ingo Molnarfbb9ce92006-07-03 00:24:50 -0700821 printk("\nnew class %p: %s", class->key, class->name);
822 if (class->name_version > 1)
823 printk("#%d", class->name_version);
824 printk("\n");
825 dump_stack();
Ingo Molnar74c383f2006-12-13 00:34:43 -0800826
Ingo Molnar70e45062006-12-06 20:40:50 -0800827 raw_local_irq_save(flags);
Ingo Molnar74c383f2006-12-13 00:34:43 -0800828 if (!graph_lock()) {
829 raw_local_irq_restore(flags);
830 return NULL;
831 }
Ingo Molnarfbb9ce92006-07-03 00:24:50 -0700832 }
833out_unlock_set:
Ingo Molnar74c383f2006-12-13 00:34:43 -0800834 graph_unlock();
Ingo Molnar70e45062006-12-06 20:40:50 -0800835 raw_local_irq_restore(flags);
Ingo Molnarfbb9ce92006-07-03 00:24:50 -0700836
Peter Zijlstra4dfbb9d2006-10-11 01:45:14 -0400837 if (!subclass || force)
Ingo Molnard6d897c2006-07-10 04:44:04 -0700838 lock->class_cache = class;
Ingo Molnarfbb9ce92006-07-03 00:24:50 -0700839
Jarek Poplawski381a2292007-02-10 01:44:58 -0800840 if (DEBUG_LOCKS_WARN_ON(class->subclass != subclass))
841 return NULL;
Ingo Molnarfbb9ce92006-07-03 00:24:50 -0700842
843 return class;
844}
845
Peter Zijlstraca58abc2007-07-19 01:48:53 -0700846#ifdef CONFIG_PROVE_LOCKING
Ingo Molnarfbb9ce92006-07-03 00:24:50 -0700847/*
Peter Zijlstra8e182572007-07-19 01:48:54 -0700848 * Allocate a lockdep entry. (assumes the graph_lock held, returns
849 * with NULL on failure)
850 */
851static struct lock_list *alloc_list_entry(void)
852{
853 if (nr_list_entries >= MAX_LOCKDEP_ENTRIES) {
854 if (!debug_locks_off_graph_unlock())
855 return NULL;
856
857 printk("BUG: MAX_LOCKDEP_ENTRIES too low!\n");
858 printk("turning off the locking correctness validator.\n");
859 return NULL;
860 }
861 return list_entries + nr_list_entries++;
862}
863
864/*
865 * Add a new dependency to the head of the list:
866 */
867static int add_lock_to_list(struct lock_class *class, struct lock_class *this,
868 struct list_head *head, unsigned long ip, int distance)
869{
870 struct lock_list *entry;
871 /*
872 * Lock not present yet - get a new dependency struct and
873 * add it to the list:
874 */
875 entry = alloc_list_entry();
876 if (!entry)
877 return 0;
878
Peter Zijlstra8e182572007-07-19 01:48:54 -0700879 if (!save_trace(&entry->trace))
880 return 0;
881
Zhu Yi74870172008-08-27 14:33:00 +0800882 entry->class = this;
883 entry->distance = distance;
Peter Zijlstra8e182572007-07-19 01:48:54 -0700884 /*
885 * Since we never remove from the dependency list, the list can
886 * be walked lockless by other CPUs, it's only allocation
887 * that must be protected by the spinlock. But this also means
888 * we must make new entries visible only once writes to the
889 * entry become visible - hence the RCU op:
890 */
891 list_add_tail_rcu(&entry->entry, head);
892
893 return 1;
894}
895
896/*
897 * Recursive, forwards-direction lock-dependency checking, used for
898 * both noncyclic checking and for hardirq-unsafe/softirq-unsafe
899 * checking.
900 *
901 * (to keep the stackframe of the recursive functions small we
902 * use these global variables, and we also mark various helper
903 * functions as noinline.)
904 */
905static struct held_lock *check_source, *check_target;
906
907/*
908 * Print a dependency chain entry (this is only done when a deadlock
909 * has been detected):
910 */
911static noinline int
912print_circular_bug_entry(struct lock_list *target, unsigned int depth)
913{
914 if (debug_locks_silent)
915 return 0;
916 printk("\n-> #%u", depth);
917 print_lock_name(target->class);
918 printk(":\n");
919 print_stack_trace(&target->trace, 6);
920
921 return 0;
922}
923
924/*
925 * When a circular dependency is detected, print the
926 * header first:
927 */
928static noinline int
929print_circular_bug_header(struct lock_list *entry, unsigned int depth)
930{
931 struct task_struct *curr = current;
932
933 if (!debug_locks_off_graph_unlock() || debug_locks_silent)
934 return 0;
935
936 printk("\n=======================================================\n");
937 printk( "[ INFO: possible circular locking dependency detected ]\n");
938 print_kernel_version();
939 printk( "-------------------------------------------------------\n");
940 printk("%s/%d is trying to acquire lock:\n",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -0700941 curr->comm, task_pid_nr(curr));
Peter Zijlstra8e182572007-07-19 01:48:54 -0700942 print_lock(check_source);
943 printk("\nbut task is already holding lock:\n");
944 print_lock(check_target);
945 printk("\nwhich lock already depends on the new lock.\n\n");
946 printk("\nthe existing dependency chain (in reverse order) is:\n");
947
948 print_circular_bug_entry(entry, depth);
949
950 return 0;
951}
952
953static noinline int print_circular_bug_tail(void)
954{
955 struct task_struct *curr = current;
956 struct lock_list this;
957
958 if (debug_locks_silent)
959 return 0;
960
Dave Jonesf82b2172008-08-11 09:30:23 +0200961 this.class = hlock_class(check_source);
Peter Zijlstra8e182572007-07-19 01:48:54 -0700962 if (!save_trace(&this.trace))
963 return 0;
964
965 print_circular_bug_entry(&this, 0);
966
967 printk("\nother info that might help us debug this:\n\n");
968 lockdep_print_held_locks(curr);
969
970 printk("\nstack backtrace:\n");
971 dump_stack();
972
973 return 0;
974}
975
976#define RECURSION_LIMIT 40
977
978static int noinline print_infinite_recursion_bug(void)
979{
980 if (!debug_locks_off_graph_unlock())
981 return 0;
982
983 WARN_ON(1);
984
985 return 0;
986}
987
David Miller419ca3f2008-07-29 21:45:03 -0700988unsigned long __lockdep_count_forward_deps(struct lock_class *class,
989 unsigned int depth)
990{
991 struct lock_list *entry;
992 unsigned long ret = 1;
993
994 if (lockdep_dependency_visit(class, depth))
995 return 0;
996
997 /*
998 * Recurse this class's dependency list:
999 */
1000 list_for_each_entry(entry, &class->locks_after, entry)
1001 ret += __lockdep_count_forward_deps(entry->class, depth + 1);
1002
1003 return ret;
1004}
1005
1006unsigned long lockdep_count_forward_deps(struct lock_class *class)
1007{
1008 unsigned long ret, flags;
1009
1010 local_irq_save(flags);
1011 __raw_spin_lock(&lockdep_lock);
1012 ret = __lockdep_count_forward_deps(class, 0);
1013 __raw_spin_unlock(&lockdep_lock);
1014 local_irq_restore(flags);
1015
1016 return ret;
1017}
1018
1019unsigned long __lockdep_count_backward_deps(struct lock_class *class,
1020 unsigned int depth)
1021{
1022 struct lock_list *entry;
1023 unsigned long ret = 1;
1024
1025 if (lockdep_dependency_visit(class, depth))
1026 return 0;
1027 /*
1028 * Recurse this class's dependency list:
1029 */
1030 list_for_each_entry(entry, &class->locks_before, entry)
1031 ret += __lockdep_count_backward_deps(entry->class, depth + 1);
1032
1033 return ret;
1034}
1035
1036unsigned long lockdep_count_backward_deps(struct lock_class *class)
1037{
1038 unsigned long ret, flags;
1039
1040 local_irq_save(flags);
1041 __raw_spin_lock(&lockdep_lock);
1042 ret = __lockdep_count_backward_deps(class, 0);
1043 __raw_spin_unlock(&lockdep_lock);
1044 local_irq_restore(flags);
1045
1046 return ret;
1047}
1048
Peter Zijlstra8e182572007-07-19 01:48:54 -07001049/*
1050 * Prove that the dependency graph starting at <entry> can not
1051 * lead to <target>. Print an error and return 0 if it does.
1052 */
1053static noinline int
1054check_noncircular(struct lock_class *source, unsigned int depth)
1055{
1056 struct lock_list *entry;
1057
David Miller419ca3f2008-07-29 21:45:03 -07001058 if (lockdep_dependency_visit(source, depth))
1059 return 1;
1060
Peter Zijlstra8e182572007-07-19 01:48:54 -07001061 debug_atomic_inc(&nr_cyclic_check_recursions);
1062 if (depth > max_recursion_depth)
1063 max_recursion_depth = depth;
1064 if (depth >= RECURSION_LIMIT)
1065 return print_infinite_recursion_bug();
1066 /*
1067 * Check this lock's dependency list:
1068 */
1069 list_for_each_entry(entry, &source->locks_after, entry) {
Dave Jonesf82b2172008-08-11 09:30:23 +02001070 if (entry->class == hlock_class(check_target))
Peter Zijlstra8e182572007-07-19 01:48:54 -07001071 return print_circular_bug_header(entry, depth+1);
1072 debug_atomic_inc(&nr_cyclic_checks);
1073 if (!check_noncircular(entry->class, depth+1))
1074 return print_circular_bug_entry(entry, depth+1);
1075 }
1076 return 1;
1077}
1078
Steven Rostedt81d68a92008-05-12 21:20:42 +02001079#if defined(CONFIG_TRACE_IRQFLAGS) && defined(CONFIG_PROVE_LOCKING)
Peter Zijlstra8e182572007-07-19 01:48:54 -07001080/*
1081 * Forwards and backwards subgraph searching, for the purposes of
1082 * proving that two subgraphs can be connected by a new dependency
1083 * without creating any illegal irq-safe -> irq-unsafe lock dependency.
1084 */
1085static enum lock_usage_bit find_usage_bit;
1086static struct lock_class *forwards_match, *backwards_match;
1087
1088/*
1089 * Find a node in the forwards-direction dependency sub-graph starting
1090 * at <source> that matches <find_usage_bit>.
1091 *
1092 * Return 2 if such a node exists in the subgraph, and put that node
1093 * into <forwards_match>.
1094 *
1095 * Return 1 otherwise and keep <forwards_match> unchanged.
1096 * Return 0 on error.
1097 */
1098static noinline int
1099find_usage_forwards(struct lock_class *source, unsigned int depth)
1100{
1101 struct lock_list *entry;
1102 int ret;
1103
David Miller419ca3f2008-07-29 21:45:03 -07001104 if (lockdep_dependency_visit(source, depth))
1105 return 1;
1106
Peter Zijlstra8e182572007-07-19 01:48:54 -07001107 if (depth > max_recursion_depth)
1108 max_recursion_depth = depth;
1109 if (depth >= RECURSION_LIMIT)
1110 return print_infinite_recursion_bug();
1111
1112 debug_atomic_inc(&nr_find_usage_forwards_checks);
1113 if (source->usage_mask & (1 << find_usage_bit)) {
1114 forwards_match = source;
1115 return 2;
1116 }
1117
1118 /*
1119 * Check this lock's dependency list:
1120 */
1121 list_for_each_entry(entry, &source->locks_after, entry) {
1122 debug_atomic_inc(&nr_find_usage_forwards_recursions);
1123 ret = find_usage_forwards(entry->class, depth+1);
1124 if (ret == 2 || ret == 0)
1125 return ret;
1126 }
1127 return 1;
1128}
1129
1130/*
1131 * Find a node in the backwards-direction dependency sub-graph starting
1132 * at <source> that matches <find_usage_bit>.
1133 *
1134 * Return 2 if such a node exists in the subgraph, and put that node
1135 * into <backwards_match>.
1136 *
1137 * Return 1 otherwise and keep <backwards_match> unchanged.
1138 * Return 0 on error.
1139 */
Steven Rostedt1d09daa2008-05-12 21:20:55 +02001140static noinline int
Peter Zijlstra8e182572007-07-19 01:48:54 -07001141find_usage_backwards(struct lock_class *source, unsigned int depth)
1142{
1143 struct lock_list *entry;
1144 int ret;
1145
David Miller419ca3f2008-07-29 21:45:03 -07001146 if (lockdep_dependency_visit(source, depth))
1147 return 1;
1148
Peter Zijlstra8e182572007-07-19 01:48:54 -07001149 if (!__raw_spin_is_locked(&lockdep_lock))
1150 return DEBUG_LOCKS_WARN_ON(1);
1151
1152 if (depth > max_recursion_depth)
1153 max_recursion_depth = depth;
1154 if (depth >= RECURSION_LIMIT)
1155 return print_infinite_recursion_bug();
1156
1157 debug_atomic_inc(&nr_find_usage_backwards_checks);
1158 if (source->usage_mask & (1 << find_usage_bit)) {
1159 backwards_match = source;
1160 return 2;
1161 }
1162
Dave Jonesf82b2172008-08-11 09:30:23 +02001163 if (!source && debug_locks_off_graph_unlock()) {
1164 WARN_ON(1);
1165 return 0;
1166 }
1167
Peter Zijlstra8e182572007-07-19 01:48:54 -07001168 /*
1169 * Check this lock's dependency list:
1170 */
1171 list_for_each_entry(entry, &source->locks_before, entry) {
1172 debug_atomic_inc(&nr_find_usage_backwards_recursions);
1173 ret = find_usage_backwards(entry->class, depth+1);
1174 if (ret == 2 || ret == 0)
1175 return ret;
1176 }
1177 return 1;
1178}
1179
1180static int
1181print_bad_irq_dependency(struct task_struct *curr,
1182 struct held_lock *prev,
1183 struct held_lock *next,
1184 enum lock_usage_bit bit1,
1185 enum lock_usage_bit bit2,
1186 const char *irqclass)
1187{
1188 if (!debug_locks_off_graph_unlock() || debug_locks_silent)
1189 return 0;
1190
1191 printk("\n======================================================\n");
1192 printk( "[ INFO: %s-safe -> %s-unsafe lock order detected ]\n",
1193 irqclass, irqclass);
1194 print_kernel_version();
1195 printk( "------------------------------------------------------\n");
1196 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 -07001197 curr->comm, task_pid_nr(curr),
Peter Zijlstra8e182572007-07-19 01:48:54 -07001198 curr->hardirq_context, hardirq_count() >> HARDIRQ_SHIFT,
1199 curr->softirq_context, softirq_count() >> SOFTIRQ_SHIFT,
1200 curr->hardirqs_enabled,
1201 curr->softirqs_enabled);
1202 print_lock(next);
1203
1204 printk("\nand this task is already holding:\n");
1205 print_lock(prev);
1206 printk("which would create a new lock dependency:\n");
Dave Jonesf82b2172008-08-11 09:30:23 +02001207 print_lock_name(hlock_class(prev));
Peter Zijlstra8e182572007-07-19 01:48:54 -07001208 printk(" ->");
Dave Jonesf82b2172008-08-11 09:30:23 +02001209 print_lock_name(hlock_class(next));
Peter Zijlstra8e182572007-07-19 01:48:54 -07001210 printk("\n");
1211
1212 printk("\nbut this new dependency connects a %s-irq-safe lock:\n",
1213 irqclass);
1214 print_lock_name(backwards_match);
1215 printk("\n... which became %s-irq-safe at:\n", irqclass);
1216
1217 print_stack_trace(backwards_match->usage_traces + bit1, 1);
1218
1219 printk("\nto a %s-irq-unsafe lock:\n", irqclass);
1220 print_lock_name(forwards_match);
1221 printk("\n... which became %s-irq-unsafe at:\n", irqclass);
1222 printk("...");
1223
1224 print_stack_trace(forwards_match->usage_traces + bit2, 1);
1225
1226 printk("\nother info that might help us debug this:\n\n");
1227 lockdep_print_held_locks(curr);
1228
1229 printk("\nthe %s-irq-safe lock's dependencies:\n", irqclass);
1230 print_lock_dependencies(backwards_match, 0);
1231
1232 printk("\nthe %s-irq-unsafe lock's dependencies:\n", irqclass);
1233 print_lock_dependencies(forwards_match, 0);
1234
1235 printk("\nstack backtrace:\n");
1236 dump_stack();
1237
1238 return 0;
1239}
1240
1241static int
1242check_usage(struct task_struct *curr, struct held_lock *prev,
1243 struct held_lock *next, enum lock_usage_bit bit_backwards,
1244 enum lock_usage_bit bit_forwards, const char *irqclass)
1245{
1246 int ret;
1247
1248 find_usage_bit = bit_backwards;
1249 /* fills in <backwards_match> */
Dave Jonesf82b2172008-08-11 09:30:23 +02001250 ret = find_usage_backwards(hlock_class(prev), 0);
Peter Zijlstra8e182572007-07-19 01:48:54 -07001251 if (!ret || ret == 1)
1252 return ret;
1253
1254 find_usage_bit = bit_forwards;
Dave Jonesf82b2172008-08-11 09:30:23 +02001255 ret = find_usage_forwards(hlock_class(next), 0);
Peter Zijlstra8e182572007-07-19 01:48:54 -07001256 if (!ret || ret == 1)
1257 return ret;
1258 /* ret == 2 */
1259 return print_bad_irq_dependency(curr, prev, next,
1260 bit_backwards, bit_forwards, irqclass);
1261}
1262
Peter Zijlstra4f367d82009-01-22 18:10:42 +01001263static const char *state_names[] = {
1264#define LOCKDEP_STATE(__STATE) \
Peter Zijlstrab4b136f2009-01-29 14:50:36 +01001265 __stringify(__STATE),
Peter Zijlstra4f367d82009-01-22 18:10:42 +01001266#include "lockdep_states.h"
1267#undef LOCKDEP_STATE
1268};
1269
1270static const char *state_rnames[] = {
1271#define LOCKDEP_STATE(__STATE) \
Peter Zijlstrab4b136f2009-01-29 14:50:36 +01001272 __stringify(__STATE)"-READ",
Peter Zijlstra4f367d82009-01-22 18:10:42 +01001273#include "lockdep_states.h"
1274#undef LOCKDEP_STATE
1275};
1276
1277static inline const char *state_name(enum lock_usage_bit bit)
1278{
1279 return (bit & 1) ? state_rnames[bit >> 2] : state_names[bit >> 2];
1280}
1281
1282static int exclusive_bit(int new_bit)
1283{
1284 /*
1285 * USED_IN
1286 * USED_IN_READ
1287 * ENABLED
1288 * ENABLED_READ
1289 *
1290 * bit 0 - write/read
1291 * bit 1 - used_in/enabled
1292 * bit 2+ state
1293 */
1294
1295 int state = new_bit & ~3;
1296 int dir = new_bit & 2;
1297
1298 /*
1299 * keep state, bit flip the direction and strip read.
1300 */
1301 return state | (dir ^ 2);
1302}
1303
1304static int check_irq_usage(struct task_struct *curr, struct held_lock *prev,
1305 struct held_lock *next, enum lock_usage_bit bit)
Peter Zijlstra8e182572007-07-19 01:48:54 -07001306{
1307 /*
1308 * Prove that the new dependency does not connect a hardirq-safe
1309 * lock with a hardirq-unsafe lock - to achieve this we search
1310 * the backwards-subgraph starting at <prev>, and the
1311 * forwards-subgraph starting at <next>:
1312 */
Peter Zijlstra4f367d82009-01-22 18:10:42 +01001313 if (!check_usage(curr, prev, next, bit,
1314 exclusive_bit(bit), state_name(bit)))
Peter Zijlstra8e182572007-07-19 01:48:54 -07001315 return 0;
1316
Peter Zijlstra4f367d82009-01-22 18:10:42 +01001317 bit++; /* _READ */
1318
Peter Zijlstra8e182572007-07-19 01:48:54 -07001319 /*
1320 * Prove that the new dependency does not connect a hardirq-safe-read
1321 * lock with a hardirq-unsafe lock - to achieve this we search
1322 * the backwards-subgraph starting at <prev>, and the
1323 * forwards-subgraph starting at <next>:
1324 */
Peter Zijlstra4f367d82009-01-22 18:10:42 +01001325 if (!check_usage(curr, prev, next, bit,
1326 exclusive_bit(bit), state_name(bit)))
Peter Zijlstra8e182572007-07-19 01:48:54 -07001327 return 0;
1328
Peter Zijlstra4f367d82009-01-22 18:10:42 +01001329 return 1;
1330}
Peter Zijlstra8e182572007-07-19 01:48:54 -07001331
Peter Zijlstra4f367d82009-01-22 18:10:42 +01001332static int
1333check_prev_add_irq(struct task_struct *curr, struct held_lock *prev,
1334 struct held_lock *next)
1335{
1336#define LOCKDEP_STATE(__STATE) \
1337 if (!check_irq_usage(curr, prev, next, LOCK_USED_IN_##__STATE)) \
Nick Piggincf40bd12009-01-21 08:12:39 +01001338 return 0;
Peter Zijlstra4f367d82009-01-22 18:10:42 +01001339#include "lockdep_states.h"
1340#undef LOCKDEP_STATE
Nick Piggincf40bd12009-01-21 08:12:39 +01001341
Peter Zijlstra8e182572007-07-19 01:48:54 -07001342 return 1;
1343}
1344
1345static void inc_chains(void)
1346{
1347 if (current->hardirq_context)
1348 nr_hardirq_chains++;
1349 else {
1350 if (current->softirq_context)
1351 nr_softirq_chains++;
1352 else
1353 nr_process_chains++;
1354 }
1355}
1356
1357#else
1358
1359static inline int
1360check_prev_add_irq(struct task_struct *curr, struct held_lock *prev,
1361 struct held_lock *next)
1362{
1363 return 1;
1364}
1365
1366static inline void inc_chains(void)
1367{
1368 nr_process_chains++;
1369}
1370
1371#endif
1372
1373static int
1374print_deadlock_bug(struct task_struct *curr, struct held_lock *prev,
1375 struct held_lock *next)
1376{
1377 if (!debug_locks_off_graph_unlock() || debug_locks_silent)
1378 return 0;
1379
1380 printk("\n=============================================\n");
1381 printk( "[ INFO: possible recursive locking detected ]\n");
1382 print_kernel_version();
1383 printk( "---------------------------------------------\n");
1384 printk("%s/%d is trying to acquire lock:\n",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07001385 curr->comm, task_pid_nr(curr));
Peter Zijlstra8e182572007-07-19 01:48:54 -07001386 print_lock(next);
1387 printk("\nbut task is already holding lock:\n");
1388 print_lock(prev);
1389
1390 printk("\nother info that might help us debug this:\n");
1391 lockdep_print_held_locks(curr);
1392
1393 printk("\nstack backtrace:\n");
1394 dump_stack();
1395
1396 return 0;
1397}
1398
1399/*
1400 * Check whether we are holding such a class already.
1401 *
1402 * (Note that this has to be done separately, because the graph cannot
1403 * detect such classes of deadlocks.)
1404 *
1405 * Returns: 0 on deadlock detected, 1 on OK, 2 on recursive read
1406 */
1407static int
1408check_deadlock(struct task_struct *curr, struct held_lock *next,
1409 struct lockdep_map *next_instance, int read)
1410{
1411 struct held_lock *prev;
Peter Zijlstra7531e2f2008-08-11 09:30:24 +02001412 struct held_lock *nest = NULL;
Peter Zijlstra8e182572007-07-19 01:48:54 -07001413 int i;
1414
1415 for (i = 0; i < curr->lockdep_depth; i++) {
1416 prev = curr->held_locks + i;
Peter Zijlstra7531e2f2008-08-11 09:30:24 +02001417
1418 if (prev->instance == next->nest_lock)
1419 nest = prev;
1420
Dave Jonesf82b2172008-08-11 09:30:23 +02001421 if (hlock_class(prev) != hlock_class(next))
Peter Zijlstra8e182572007-07-19 01:48:54 -07001422 continue;
Peter Zijlstra7531e2f2008-08-11 09:30:24 +02001423
Peter Zijlstra8e182572007-07-19 01:48:54 -07001424 /*
1425 * Allow read-after-read recursion of the same
1426 * lock class (i.e. read_lock(lock)+read_lock(lock)):
1427 */
1428 if ((read == 2) && prev->read)
1429 return 2;
Peter Zijlstra7531e2f2008-08-11 09:30:24 +02001430
1431 /*
1432 * We're holding the nest_lock, which serializes this lock's
1433 * nesting behaviour.
1434 */
1435 if (nest)
1436 return 2;
1437
Peter Zijlstra8e182572007-07-19 01:48:54 -07001438 return print_deadlock_bug(curr, prev, next);
1439 }
1440 return 1;
1441}
1442
1443/*
1444 * There was a chain-cache miss, and we are about to add a new dependency
1445 * to a previous lock. We recursively validate the following rules:
1446 *
1447 * - would the adding of the <prev> -> <next> dependency create a
1448 * circular dependency in the graph? [== circular deadlock]
1449 *
1450 * - does the new prev->next dependency connect any hardirq-safe lock
1451 * (in the full backwards-subgraph starting at <prev>) with any
1452 * hardirq-unsafe lock (in the full forwards-subgraph starting at
1453 * <next>)? [== illegal lock inversion with hardirq contexts]
1454 *
1455 * - does the new prev->next dependency connect any softirq-safe lock
1456 * (in the full backwards-subgraph starting at <prev>) with any
1457 * softirq-unsafe lock (in the full forwards-subgraph starting at
1458 * <next>)? [== illegal lock inversion with softirq contexts]
1459 *
1460 * any of these scenarios could lead to a deadlock.
1461 *
1462 * Then if all the validations pass, we add the forwards and backwards
1463 * dependency.
1464 */
1465static int
1466check_prev_add(struct task_struct *curr, struct held_lock *prev,
1467 struct held_lock *next, int distance)
1468{
1469 struct lock_list *entry;
1470 int ret;
1471
1472 /*
1473 * Prove that the new <prev> -> <next> dependency would not
1474 * create a circular dependency in the graph. (We do this by
1475 * forward-recursing into the graph starting at <next>, and
1476 * checking whether we can reach <prev>.)
1477 *
1478 * We are using global variables to control the recursion, to
1479 * keep the stackframe size of the recursive functions low:
1480 */
1481 check_source = next;
1482 check_target = prev;
Dave Jonesf82b2172008-08-11 09:30:23 +02001483 if (!(check_noncircular(hlock_class(next), 0)))
Peter Zijlstra8e182572007-07-19 01:48:54 -07001484 return print_circular_bug_tail();
1485
1486 if (!check_prev_add_irq(curr, prev, next))
1487 return 0;
1488
1489 /*
1490 * For recursive read-locks we do all the dependency checks,
1491 * but we dont store read-triggered dependencies (only
1492 * write-triggered dependencies). This ensures that only the
1493 * write-side dependencies matter, and that if for example a
1494 * write-lock never takes any other locks, then the reads are
1495 * equivalent to a NOP.
1496 */
1497 if (next->read == 2 || prev->read == 2)
1498 return 1;
1499 /*
1500 * Is the <prev> -> <next> dependency already present?
1501 *
1502 * (this may occur even though this is a new chain: consider
1503 * e.g. the L1 -> L2 -> L3 -> L4 and the L5 -> L1 -> L2 -> L3
1504 * chains - the second one will be new, but L1 already has
1505 * L2 added to its dependency list, due to the first chain.)
1506 */
Dave Jonesf82b2172008-08-11 09:30:23 +02001507 list_for_each_entry(entry, &hlock_class(prev)->locks_after, entry) {
1508 if (entry->class == hlock_class(next)) {
Peter Zijlstra8e182572007-07-19 01:48:54 -07001509 if (distance == 1)
1510 entry->distance = 1;
1511 return 2;
1512 }
1513 }
1514
1515 /*
1516 * Ok, all validations passed, add the new lock
1517 * to the previous lock's dependency list:
1518 */
Dave Jonesf82b2172008-08-11 09:30:23 +02001519 ret = add_lock_to_list(hlock_class(prev), hlock_class(next),
1520 &hlock_class(prev)->locks_after,
1521 next->acquire_ip, distance);
Peter Zijlstra8e182572007-07-19 01:48:54 -07001522
1523 if (!ret)
1524 return 0;
1525
Dave Jonesf82b2172008-08-11 09:30:23 +02001526 ret = add_lock_to_list(hlock_class(next), hlock_class(prev),
1527 &hlock_class(next)->locks_before,
1528 next->acquire_ip, distance);
Peter Zijlstra8e182572007-07-19 01:48:54 -07001529 if (!ret)
1530 return 0;
1531
1532 /*
1533 * Debugging printouts:
1534 */
Dave Jonesf82b2172008-08-11 09:30:23 +02001535 if (verbose(hlock_class(prev)) || verbose(hlock_class(next))) {
Peter Zijlstra8e182572007-07-19 01:48:54 -07001536 graph_unlock();
1537 printk("\n new dependency: ");
Dave Jonesf82b2172008-08-11 09:30:23 +02001538 print_lock_name(hlock_class(prev));
Peter Zijlstra8e182572007-07-19 01:48:54 -07001539 printk(" => ");
Dave Jonesf82b2172008-08-11 09:30:23 +02001540 print_lock_name(hlock_class(next));
Peter Zijlstra8e182572007-07-19 01:48:54 -07001541 printk("\n");
1542 dump_stack();
1543 return graph_lock();
1544 }
1545 return 1;
1546}
1547
1548/*
1549 * Add the dependency to all directly-previous locks that are 'relevant'.
1550 * The ones that are relevant are (in increasing distance from curr):
1551 * all consecutive trylock entries and the final non-trylock entry - or
1552 * the end of this context's lock-chain - whichever comes first.
1553 */
1554static int
1555check_prevs_add(struct task_struct *curr, struct held_lock *next)
1556{
1557 int depth = curr->lockdep_depth;
1558 struct held_lock *hlock;
1559
1560 /*
1561 * Debugging checks.
1562 *
1563 * Depth must not be zero for a non-head lock:
1564 */
1565 if (!depth)
1566 goto out_bug;
1567 /*
1568 * At least two relevant locks must exist for this
1569 * to be a head:
1570 */
1571 if (curr->held_locks[depth].irq_context !=
1572 curr->held_locks[depth-1].irq_context)
1573 goto out_bug;
1574
1575 for (;;) {
1576 int distance = curr->lockdep_depth - depth + 1;
1577 hlock = curr->held_locks + depth-1;
1578 /*
1579 * Only non-recursive-read entries get new dependencies
1580 * added:
1581 */
1582 if (hlock->read != 2) {
1583 if (!check_prev_add(curr, hlock, next, distance))
1584 return 0;
1585 /*
1586 * Stop after the first non-trylock entry,
1587 * as non-trylock entries have added their
1588 * own direct dependencies already, so this
1589 * lock is connected to them indirectly:
1590 */
1591 if (!hlock->trylock)
1592 break;
1593 }
1594 depth--;
1595 /*
1596 * End of lock-stack?
1597 */
1598 if (!depth)
1599 break;
1600 /*
1601 * Stop the search if we cross into another context:
1602 */
1603 if (curr->held_locks[depth].irq_context !=
1604 curr->held_locks[depth-1].irq_context)
1605 break;
1606 }
1607 return 1;
1608out_bug:
1609 if (!debug_locks_off_graph_unlock())
1610 return 0;
1611
1612 WARN_ON(1);
1613
1614 return 0;
1615}
1616
1617unsigned long nr_lock_chains;
Huang, Ying443cd502008-06-20 16:39:21 +08001618struct lock_chain lock_chains[MAX_LOCKDEP_CHAINS];
Huang, Yingcd1a28e2008-06-23 11:20:54 +08001619int nr_chain_hlocks;
Huang, Ying443cd502008-06-20 16:39:21 +08001620static u16 chain_hlocks[MAX_LOCKDEP_CHAIN_HLOCKS];
1621
1622struct lock_class *lock_chain_get_class(struct lock_chain *chain, int i)
1623{
1624 return lock_classes + chain_hlocks[chain->base + i];
1625}
Peter Zijlstra8e182572007-07-19 01:48:54 -07001626
1627/*
Ingo Molnarfbb9ce92006-07-03 00:24:50 -07001628 * Look up a dependency chain. If the key is not present yet then
Jarek Poplawski9e860d02007-05-08 00:30:12 -07001629 * add it and return 1 - in this case the new dependency chain is
1630 * validated. If the key is already hashed, return 0.
1631 * (On return with 1 graph_lock is held.)
Ingo Molnarfbb9ce92006-07-03 00:24:50 -07001632 */
Huang, Ying443cd502008-06-20 16:39:21 +08001633static inline int lookup_chain_cache(struct task_struct *curr,
1634 struct held_lock *hlock,
1635 u64 chain_key)
Ingo Molnarfbb9ce92006-07-03 00:24:50 -07001636{
Dave Jonesf82b2172008-08-11 09:30:23 +02001637 struct lock_class *class = hlock_class(hlock);
Ingo Molnarfbb9ce92006-07-03 00:24:50 -07001638 struct list_head *hash_head = chainhashentry(chain_key);
1639 struct lock_chain *chain;
Huang, Ying443cd502008-06-20 16:39:21 +08001640 struct held_lock *hlock_curr, *hlock_next;
Huang, Yingcd1a28e2008-06-23 11:20:54 +08001641 int i, j, n, cn;
Ingo Molnarfbb9ce92006-07-03 00:24:50 -07001642
Jarek Poplawski381a2292007-02-10 01:44:58 -08001643 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
1644 return 0;
Ingo Molnarfbb9ce92006-07-03 00:24:50 -07001645 /*
1646 * We can walk it lock-free, because entries only get added
1647 * to the hash:
1648 */
1649 list_for_each_entry(chain, hash_head, entry) {
1650 if (chain->chain_key == chain_key) {
1651cache_hit:
1652 debug_atomic_inc(&chain_lookup_hits);
Ingo Molnar81fc6852006-12-13 00:34:40 -08001653 if (very_verbose(class))
Andrew Morton755cd902006-12-29 16:49:14 -08001654 printk("\nhash chain already cached, key: "
1655 "%016Lx tail class: [%p] %s\n",
1656 (unsigned long long)chain_key,
1657 class->key, class->name);
Ingo Molnarfbb9ce92006-07-03 00:24:50 -07001658 return 0;
1659 }
1660 }
Ingo Molnar81fc6852006-12-13 00:34:40 -08001661 if (very_verbose(class))
Andrew Morton755cd902006-12-29 16:49:14 -08001662 printk("\nnew hash chain, key: %016Lx tail class: [%p] %s\n",
1663 (unsigned long long)chain_key, class->key, class->name);
Ingo Molnarfbb9ce92006-07-03 00:24:50 -07001664 /*
1665 * Allocate a new chain entry from the static array, and add
1666 * it to the hash:
1667 */
Ingo Molnar74c383f2006-12-13 00:34:43 -08001668 if (!graph_lock())
1669 return 0;
Ingo Molnarfbb9ce92006-07-03 00:24:50 -07001670 /*
1671 * We have to walk the chain again locked - to avoid duplicates:
1672 */
1673 list_for_each_entry(chain, hash_head, entry) {
1674 if (chain->chain_key == chain_key) {
Ingo Molnar74c383f2006-12-13 00:34:43 -08001675 graph_unlock();
Ingo Molnarfbb9ce92006-07-03 00:24:50 -07001676 goto cache_hit;
1677 }
1678 }
1679 if (unlikely(nr_lock_chains >= MAX_LOCKDEP_CHAINS)) {
Ingo Molnar74c383f2006-12-13 00:34:43 -08001680 if (!debug_locks_off_graph_unlock())
1681 return 0;
1682
Ingo Molnarfbb9ce92006-07-03 00:24:50 -07001683 printk("BUG: MAX_LOCKDEP_CHAINS too low!\n");
1684 printk("turning off the locking correctness validator.\n");
1685 return 0;
1686 }
1687 chain = lock_chains + nr_lock_chains++;
1688 chain->chain_key = chain_key;
Huang, Ying443cd502008-06-20 16:39:21 +08001689 chain->irq_context = hlock->irq_context;
1690 /* Find the first held_lock of current chain */
1691 hlock_next = hlock;
1692 for (i = curr->lockdep_depth - 1; i >= 0; i--) {
1693 hlock_curr = curr->held_locks + i;
1694 if (hlock_curr->irq_context != hlock_next->irq_context)
1695 break;
1696 hlock_next = hlock;
1697 }
1698 i++;
1699 chain->depth = curr->lockdep_depth + 1 - i;
Huang, Yingcd1a28e2008-06-23 11:20:54 +08001700 cn = nr_chain_hlocks;
1701 while (cn + chain->depth <= MAX_LOCKDEP_CHAIN_HLOCKS) {
1702 n = cmpxchg(&nr_chain_hlocks, cn, cn + chain->depth);
1703 if (n == cn)
1704 break;
1705 cn = n;
1706 }
1707 if (likely(cn + chain->depth <= MAX_LOCKDEP_CHAIN_HLOCKS)) {
1708 chain->base = cn;
Huang, Ying443cd502008-06-20 16:39:21 +08001709 for (j = 0; j < chain->depth - 1; j++, i++) {
Dave Jonesf82b2172008-08-11 09:30:23 +02001710 int lock_id = curr->held_locks[i].class_idx - 1;
Huang, Ying443cd502008-06-20 16:39:21 +08001711 chain_hlocks[chain->base + j] = lock_id;
1712 }
1713 chain_hlocks[chain->base + j] = class - lock_classes;
1714 }
Ingo Molnarfbb9ce92006-07-03 00:24:50 -07001715 list_add_tail_rcu(&chain->entry, hash_head);
1716 debug_atomic_inc(&chain_lookup_misses);
Peter Zijlstra8e182572007-07-19 01:48:54 -07001717 inc_chains();
Ingo Molnarfbb9ce92006-07-03 00:24:50 -07001718
1719 return 1;
1720}
Peter Zijlstra8e182572007-07-19 01:48:54 -07001721
1722static int validate_chain(struct task_struct *curr, struct lockdep_map *lock,
Johannes Berg4e6045f2007-10-18 23:39:55 -07001723 struct held_lock *hlock, int chain_head, u64 chain_key)
Peter Zijlstra8e182572007-07-19 01:48:54 -07001724{
1725 /*
1726 * Trylock needs to maintain the stack of held locks, but it
1727 * does not add new dependencies, because trylock can be done
1728 * in any order.
1729 *
1730 * We look up the chain_key and do the O(N^2) check and update of
1731 * the dependencies only if this is a new dependency chain.
1732 * (If lookup_chain_cache() returns with 1 it acquires
1733 * graph_lock for us)
1734 */
1735 if (!hlock->trylock && (hlock->check == 2) &&
Huang, Ying443cd502008-06-20 16:39:21 +08001736 lookup_chain_cache(curr, hlock, chain_key)) {
Peter Zijlstra8e182572007-07-19 01:48:54 -07001737 /*
1738 * Check whether last held lock:
1739 *
1740 * - is irq-safe, if this lock is irq-unsafe
1741 * - is softirq-safe, if this lock is hardirq-unsafe
1742 *
1743 * And check whether the new lock's dependency graph
1744 * could lead back to the previous lock.
1745 *
1746 * any of these scenarios could lead to a deadlock. If
1747 * All validations
1748 */
1749 int ret = check_deadlock(curr, hlock, lock, hlock->read);
1750
1751 if (!ret)
1752 return 0;
1753 /*
1754 * Mark recursive read, as we jump over it when
1755 * building dependencies (just like we jump over
1756 * trylock entries):
1757 */
1758 if (ret == 2)
1759 hlock->read = 2;
1760 /*
1761 * Add dependency only if this lock is not the head
1762 * of the chain, and if it's not a secondary read-lock:
1763 */
1764 if (!chain_head && ret != 2)
1765 if (!check_prevs_add(curr, hlock))
1766 return 0;
1767 graph_unlock();
1768 } else
1769 /* after lookup_chain_cache(): */
1770 if (unlikely(!debug_locks))
1771 return 0;
1772
1773 return 1;
1774}
1775#else
1776static inline int validate_chain(struct task_struct *curr,
1777 struct lockdep_map *lock, struct held_lock *hlock,
Gregory Haskins3aa416b2007-10-11 22:11:11 +02001778 int chain_head, u64 chain_key)
Peter Zijlstra8e182572007-07-19 01:48:54 -07001779{
1780 return 1;
1781}
Peter Zijlstraca58abc2007-07-19 01:48:53 -07001782#endif
Ingo Molnarfbb9ce92006-07-03 00:24:50 -07001783
1784/*
1785 * We are building curr_chain_key incrementally, so double-check
1786 * it from scratch, to make sure that it's done correctly:
1787 */
Steven Rostedt1d09daa2008-05-12 21:20:55 +02001788static void check_chain_key(struct task_struct *curr)
Ingo Molnarfbb9ce92006-07-03 00:24:50 -07001789{
1790#ifdef CONFIG_DEBUG_LOCKDEP
1791 struct held_lock *hlock, *prev_hlock = NULL;
1792 unsigned int i, id;
1793 u64 chain_key = 0;
1794
1795 for (i = 0; i < curr->lockdep_depth; i++) {
1796 hlock = curr->held_locks + i;
1797 if (chain_key != hlock->prev_chain_key) {
1798 debug_locks_off();
Arjan van de Ven2df8b1d2008-07-30 12:43:11 -07001799 WARN(1, "hm#1, depth: %u [%u], %016Lx != %016Lx\n",
Ingo Molnarfbb9ce92006-07-03 00:24:50 -07001800 curr->lockdep_depth, i,
1801 (unsigned long long)chain_key,
1802 (unsigned long long)hlock->prev_chain_key);
Ingo Molnarfbb9ce92006-07-03 00:24:50 -07001803 return;
1804 }
Dave Jonesf82b2172008-08-11 09:30:23 +02001805 id = hlock->class_idx - 1;
Jarek Poplawski381a2292007-02-10 01:44:58 -08001806 if (DEBUG_LOCKS_WARN_ON(id >= MAX_LOCKDEP_KEYS))
1807 return;
1808
Ingo Molnarfbb9ce92006-07-03 00:24:50 -07001809 if (prev_hlock && (prev_hlock->irq_context !=
1810 hlock->irq_context))
1811 chain_key = 0;
1812 chain_key = iterate_chain_key(chain_key, id);
1813 prev_hlock = hlock;
1814 }
1815 if (chain_key != curr->curr_chain_key) {
1816 debug_locks_off();
Arjan van de Ven2df8b1d2008-07-30 12:43:11 -07001817 WARN(1, "hm#2, depth: %u [%u], %016Lx != %016Lx\n",
Ingo Molnarfbb9ce92006-07-03 00:24:50 -07001818 curr->lockdep_depth, i,
1819 (unsigned long long)chain_key,
1820 (unsigned long long)curr->curr_chain_key);
Ingo Molnarfbb9ce92006-07-03 00:24:50 -07001821 }
1822#endif
1823}
1824
Peter Zijlstra8e182572007-07-19 01:48:54 -07001825static int
1826print_usage_bug(struct task_struct *curr, struct held_lock *this,
1827 enum lock_usage_bit prev_bit, enum lock_usage_bit new_bit)
1828{
1829 if (!debug_locks_off_graph_unlock() || debug_locks_silent)
1830 return 0;
1831
1832 printk("\n=================================\n");
1833 printk( "[ INFO: inconsistent lock state ]\n");
1834 print_kernel_version();
1835 printk( "---------------------------------\n");
1836
1837 printk("inconsistent {%s} -> {%s} usage.\n",
1838 usage_str[prev_bit], usage_str[new_bit]);
1839
1840 printk("%s/%d [HC%u[%lu]:SC%u[%lu]:HE%u:SE%u] takes:\n",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07001841 curr->comm, task_pid_nr(curr),
Peter Zijlstra8e182572007-07-19 01:48:54 -07001842 trace_hardirq_context(curr), hardirq_count() >> HARDIRQ_SHIFT,
1843 trace_softirq_context(curr), softirq_count() >> SOFTIRQ_SHIFT,
1844 trace_hardirqs_enabled(curr),
1845 trace_softirqs_enabled(curr));
1846 print_lock(this);
1847
1848 printk("{%s} state was registered at:\n", usage_str[prev_bit]);
Dave Jonesf82b2172008-08-11 09:30:23 +02001849 print_stack_trace(hlock_class(this)->usage_traces + prev_bit, 1);
Peter Zijlstra8e182572007-07-19 01:48:54 -07001850
1851 print_irqtrace_events(curr);
1852 printk("\nother info that might help us debug this:\n");
1853 lockdep_print_held_locks(curr);
1854
1855 printk("\nstack backtrace:\n");
1856 dump_stack();
1857
1858 return 0;
1859}
1860
1861/*
1862 * Print out an error if an invalid bit is set:
1863 */
1864static inline int
1865valid_state(struct task_struct *curr, struct held_lock *this,
1866 enum lock_usage_bit new_bit, enum lock_usage_bit bad_bit)
1867{
Dave Jonesf82b2172008-08-11 09:30:23 +02001868 if (unlikely(hlock_class(this)->usage_mask & (1 << bad_bit)))
Peter Zijlstra8e182572007-07-19 01:48:54 -07001869 return print_usage_bug(curr, this, bad_bit, new_bit);
1870 return 1;
1871}
1872
1873static int mark_lock(struct task_struct *curr, struct held_lock *this,
1874 enum lock_usage_bit new_bit);
1875
Steven Rostedt81d68a92008-05-12 21:20:42 +02001876#if defined(CONFIG_TRACE_IRQFLAGS) && defined(CONFIG_PROVE_LOCKING)
Ingo Molnarfbb9ce92006-07-03 00:24:50 -07001877
1878/*
1879 * print irq inversion bug:
1880 */
1881static int
1882print_irq_inversion_bug(struct task_struct *curr, struct lock_class *other,
1883 struct held_lock *this, int forwards,
1884 const char *irqclass)
1885{
Ingo Molnar74c383f2006-12-13 00:34:43 -08001886 if (!debug_locks_off_graph_unlock() || debug_locks_silent)
Ingo Molnarfbb9ce92006-07-03 00:24:50 -07001887 return 0;
1888
1889 printk("\n=========================================================\n");
1890 printk( "[ INFO: possible irq lock inversion dependency detected ]\n");
Dave Jones99de0552006-09-29 02:00:10 -07001891 print_kernel_version();
Ingo Molnarfbb9ce92006-07-03 00:24:50 -07001892 printk( "---------------------------------------------------------\n");
1893 printk("%s/%d just changed the state of lock:\n",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07001894 curr->comm, task_pid_nr(curr));
Ingo Molnarfbb9ce92006-07-03 00:24:50 -07001895 print_lock(this);
1896 if (forwards)
Peter Zijlstra26575e22009-03-04 14:53:24 +01001897 printk("but this lock took another, %s-unsafe lock in the past:\n", irqclass);
Ingo Molnarfbb9ce92006-07-03 00:24:50 -07001898 else
Peter Zijlstra26575e22009-03-04 14:53:24 +01001899 printk("but this lock was taken by another, %s-safe lock in the past:\n", irqclass);
Ingo Molnarfbb9ce92006-07-03 00:24:50 -07001900 print_lock_name(other);
1901 printk("\n\nand interrupts could create inverse lock ordering between them.\n\n");
1902
1903 printk("\nother info that might help us debug this:\n");
1904 lockdep_print_held_locks(curr);
1905
1906 printk("\nthe first lock's dependencies:\n");
Dave Jonesf82b2172008-08-11 09:30:23 +02001907 print_lock_dependencies(hlock_class(this), 0);
Ingo Molnarfbb9ce92006-07-03 00:24:50 -07001908
1909 printk("\nthe second lock's dependencies:\n");
1910 print_lock_dependencies(other, 0);
1911
1912 printk("\nstack backtrace:\n");
1913 dump_stack();
1914
1915 return 0;
1916}
1917
1918/*
1919 * Prove that in the forwards-direction subgraph starting at <this>
1920 * there is no lock matching <mask>:
1921 */
1922static int
1923check_usage_forwards(struct task_struct *curr, struct held_lock *this,
1924 enum lock_usage_bit bit, const char *irqclass)
1925{
1926 int ret;
1927
1928 find_usage_bit = bit;
1929 /* fills in <forwards_match> */
Dave Jonesf82b2172008-08-11 09:30:23 +02001930 ret = find_usage_forwards(hlock_class(this), 0);
Ingo Molnarfbb9ce92006-07-03 00:24:50 -07001931 if (!ret || ret == 1)
1932 return ret;
1933
1934 return print_irq_inversion_bug(curr, forwards_match, this, 1, irqclass);
1935}
1936
1937/*
1938 * Prove that in the backwards-direction subgraph starting at <this>
1939 * there is no lock matching <mask>:
1940 */
1941static int
1942check_usage_backwards(struct task_struct *curr, struct held_lock *this,
1943 enum lock_usage_bit bit, const char *irqclass)
1944{
1945 int ret;
1946
1947 find_usage_bit = bit;
1948 /* fills in <backwards_match> */
Dave Jonesf82b2172008-08-11 09:30:23 +02001949 ret = find_usage_backwards(hlock_class(this), 0);
Ingo Molnarfbb9ce92006-07-03 00:24:50 -07001950 if (!ret || ret == 1)
1951 return ret;
1952
1953 return print_irq_inversion_bug(curr, backwards_match, this, 0, irqclass);
1954}
1955
Ingo Molnar3117df02006-12-13 00:34:43 -08001956void print_irqtrace_events(struct task_struct *curr)
Ingo Molnarfbb9ce92006-07-03 00:24:50 -07001957{
1958 printk("irq event stamp: %u\n", curr->irq_events);
1959 printk("hardirqs last enabled at (%u): ", curr->hardirq_enable_event);
1960 print_ip_sym(curr->hardirq_enable_ip);
1961 printk("hardirqs last disabled at (%u): ", curr->hardirq_disable_event);
1962 print_ip_sym(curr->hardirq_disable_ip);
1963 printk("softirqs last enabled at (%u): ", curr->softirq_enable_event);
1964 print_ip_sym(curr->softirq_enable_ip);
1965 printk("softirqs last disabled at (%u): ", curr->softirq_disable_event);
1966 print_ip_sym(curr->softirq_disable_ip);
1967}
1968
Peter Zijlstracd953022009-01-22 16:38:21 +01001969static int HARDIRQ_verbose(struct lock_class *class)
Ingo Molnarfbb9ce92006-07-03 00:24:50 -07001970{
Peter Zijlstra8e182572007-07-19 01:48:54 -07001971#if HARDIRQ_VERBOSE
1972 return class_filter(class);
1973#endif
Ingo Molnarfbb9ce92006-07-03 00:24:50 -07001974 return 0;
1975}
1976
Peter Zijlstracd953022009-01-22 16:38:21 +01001977static int SOFTIRQ_verbose(struct lock_class *class)
Ingo Molnarfbb9ce92006-07-03 00:24:50 -07001978{
Peter Zijlstra8e182572007-07-19 01:48:54 -07001979#if SOFTIRQ_VERBOSE
1980 return class_filter(class);
1981#endif
1982 return 0;
Ingo Molnarfbb9ce92006-07-03 00:24:50 -07001983}
1984
Peter Zijlstracd953022009-01-22 16:38:21 +01001985static int RECLAIM_FS_verbose(struct lock_class *class)
Nick Piggincf40bd12009-01-21 08:12:39 +01001986{
1987#if RECLAIM_VERBOSE
1988 return class_filter(class);
1989#endif
1990 return 0;
1991}
1992
Ingo Molnarfbb9ce92006-07-03 00:24:50 -07001993#define STRICT_READ_CHECKS 1
1994
Peter Zijlstracd953022009-01-22 16:38:21 +01001995static int (*state_verbose_f[])(struct lock_class *class) = {
1996#define LOCKDEP_STATE(__STATE) \
1997 __STATE##_verbose,
1998#include "lockdep_states.h"
1999#undef LOCKDEP_STATE
2000};
2001
2002static inline int state_verbose(enum lock_usage_bit bit,
2003 struct lock_class *class)
2004{
2005 return state_verbose_f[bit >> 2](class);
2006}
2007
Peter Zijlstra42c50d52009-01-22 16:58:16 +01002008typedef int (*check_usage_f)(struct task_struct *, struct held_lock *,
2009 enum lock_usage_bit bit, const char *name);
2010
Peter Zijlstra6a6904d2009-01-22 16:07:44 +01002011static int
Peter Zijlstra1c21f142009-03-04 13:51:13 +01002012mark_lock_irq(struct task_struct *curr, struct held_lock *this,
2013 enum lock_usage_bit new_bit)
Peter Zijlstra6a6904d2009-01-22 16:07:44 +01002014{
Peter Zijlstraf9892092009-01-22 16:09:59 +01002015 int excl_bit = exclusive_bit(new_bit);
Peter Zijlstra9d3651a2009-01-22 17:18:32 +01002016 int read = new_bit & 1;
Peter Zijlstra42c50d52009-01-22 16:58:16 +01002017 int dir = new_bit & 2;
2018
Peter Zijlstra38aa2712009-01-27 14:53:50 +01002019 /*
2020 * mark USED_IN has to look forwards -- to ensure no dependency
2021 * has ENABLED state, which would allow recursion deadlocks.
2022 *
2023 * mark ENABLED has to look backwards -- to ensure no dependee
2024 * has USED_IN state, which, again, would allow recursion deadlocks.
2025 */
Peter Zijlstra42c50d52009-01-22 16:58:16 +01002026 check_usage_f usage = dir ?
2027 check_usage_backwards : check_usage_forwards;
Peter Zijlstraf9892092009-01-22 16:09:59 +01002028
Peter Zijlstra38aa2712009-01-27 14:53:50 +01002029 /*
2030 * Validate that this particular lock does not have conflicting
2031 * usage states.
2032 */
Peter Zijlstra6a6904d2009-01-22 16:07:44 +01002033 if (!valid_state(curr, this, new_bit, excl_bit))
2034 return 0;
Peter Zijlstra9d3651a2009-01-22 17:18:32 +01002035
Peter Zijlstra38aa2712009-01-27 14:53:50 +01002036 /*
2037 * Validate that the lock dependencies don't have conflicting usage
2038 * states.
2039 */
2040 if ((!read || !dir || STRICT_READ_CHECKS) &&
Peter Zijlstra1c21f142009-03-04 13:51:13 +01002041 !usage(curr, this, excl_bit, state_name(new_bit & ~1)))
Peter Zijlstra6a6904d2009-01-22 16:07:44 +01002042 return 0;
Peter Zijlstra780e8202009-01-22 16:51:29 +01002043
Peter Zijlstra38aa2712009-01-27 14:53:50 +01002044 /*
2045 * Check for read in write conflicts
2046 */
2047 if (!read) {
2048 if (!valid_state(curr, this, new_bit, excl_bit + 1))
2049 return 0;
2050
2051 if (STRICT_READ_CHECKS &&
Peter Zijlstra4f367d82009-01-22 18:10:42 +01002052 !usage(curr, this, excl_bit + 1,
2053 state_name(new_bit + 1)))
Peter Zijlstra38aa2712009-01-27 14:53:50 +01002054 return 0;
2055 }
Peter Zijlstra780e8202009-01-22 16:51:29 +01002056
Peter Zijlstracd953022009-01-22 16:38:21 +01002057 if (state_verbose(new_bit, hlock_class(this)))
Peter Zijlstra6a6904d2009-01-22 16:07:44 +01002058 return 2;
2059
2060 return 1;
2061}
2062
Nick Piggincf40bd12009-01-21 08:12:39 +01002063enum mark_type {
Peter Zijlstra36bfb9b2009-01-22 14:12:41 +01002064#define LOCKDEP_STATE(__STATE) __STATE,
2065#include "lockdep_states.h"
2066#undef LOCKDEP_STATE
Nick Piggincf40bd12009-01-21 08:12:39 +01002067};
2068
Ingo Molnarfbb9ce92006-07-03 00:24:50 -07002069/*
2070 * Mark all held locks with a usage bit:
2071 */
Steven Rostedt1d09daa2008-05-12 21:20:55 +02002072static int
Nick Piggincf40bd12009-01-21 08:12:39 +01002073mark_held_locks(struct task_struct *curr, enum mark_type mark)
Ingo Molnarfbb9ce92006-07-03 00:24:50 -07002074{
2075 enum lock_usage_bit usage_bit;
2076 struct held_lock *hlock;
2077 int i;
2078
2079 for (i = 0; i < curr->lockdep_depth; i++) {
2080 hlock = curr->held_locks + i;
2081
Peter Zijlstracf2ad4d2009-01-27 13:58:08 +01002082 usage_bit = 2 + (mark << 2); /* ENABLED */
2083 if (hlock->read)
2084 usage_bit += 1; /* READ */
2085
2086 BUG_ON(usage_bit >= LOCK_USAGE_STATES);
Nick Piggincf40bd12009-01-21 08:12:39 +01002087
Jarek Poplawski4ff773bb2007-05-08 00:31:00 -07002088 if (!mark_lock(curr, hlock, usage_bit))
Ingo Molnarfbb9ce92006-07-03 00:24:50 -07002089 return 0;
2090 }
2091
2092 return 1;
2093}
2094
2095/*
2096 * Debugging helper: via this flag we know that we are in
2097 * 'early bootup code', and will warn about any invalid irqs-on event:
2098 */
2099static int early_boot_irqs_enabled;
2100
2101void early_boot_irqs_off(void)
2102{
2103 early_boot_irqs_enabled = 0;
2104}
2105
2106void early_boot_irqs_on(void)
2107{
2108 early_boot_irqs_enabled = 1;
2109}
2110
2111/*
2112 * Hardirqs will be enabled:
2113 */
Heiko Carstens6afe40b2008-10-28 11:14:58 +01002114void trace_hardirqs_on_caller(unsigned long ip)
Ingo Molnarfbb9ce92006-07-03 00:24:50 -07002115{
2116 struct task_struct *curr = current;
Ingo Molnarfbb9ce92006-07-03 00:24:50 -07002117
Heiko Carstens6afe40b2008-10-28 11:14:58 +01002118 time_hardirqs_on(CALLER_ADDR0, ip);
Steven Rostedt81d68a92008-05-12 21:20:42 +02002119
Ingo Molnarfbb9ce92006-07-03 00:24:50 -07002120 if (unlikely(!debug_locks || current->lockdep_recursion))
2121 return;
2122
2123 if (DEBUG_LOCKS_WARN_ON(unlikely(!early_boot_irqs_enabled)))
2124 return;
2125
2126 if (unlikely(curr->hardirqs_enabled)) {
2127 debug_atomic_inc(&redundant_hardirqs_on);
2128 return;
2129 }
2130 /* we'll do an OFF -> ON transition: */
2131 curr->hardirqs_enabled = 1;
Ingo Molnarfbb9ce92006-07-03 00:24:50 -07002132
2133 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2134 return;
2135 if (DEBUG_LOCKS_WARN_ON(current->hardirq_context))
2136 return;
2137 /*
2138 * We are going to turn hardirqs on, so set the
2139 * usage bit for all held locks:
2140 */
Nick Piggincf40bd12009-01-21 08:12:39 +01002141 if (!mark_held_locks(curr, HARDIRQ))
Ingo Molnarfbb9ce92006-07-03 00:24:50 -07002142 return;
2143 /*
2144 * If we have softirqs enabled, then set the usage
2145 * bit for all held locks. (disabled hardirqs prevented
2146 * this bit from being set before)
2147 */
2148 if (curr->softirqs_enabled)
Nick Piggincf40bd12009-01-21 08:12:39 +01002149 if (!mark_held_locks(curr, SOFTIRQ))
Ingo Molnarfbb9ce92006-07-03 00:24:50 -07002150 return;
2151
2152 curr->hardirq_enable_ip = ip;
2153 curr->hardirq_enable_event = ++curr->irq_events;
2154 debug_atomic_inc(&hardirqs_on_events);
2155}
Steven Rostedt81d68a92008-05-12 21:20:42 +02002156EXPORT_SYMBOL(trace_hardirqs_on_caller);
Ingo Molnarfbb9ce92006-07-03 00:24:50 -07002157
Steven Rostedt1d09daa2008-05-12 21:20:55 +02002158void trace_hardirqs_on(void)
Steven Rostedt81d68a92008-05-12 21:20:42 +02002159{
2160 trace_hardirqs_on_caller(CALLER_ADDR0);
2161}
Ingo Molnarfbb9ce92006-07-03 00:24:50 -07002162EXPORT_SYMBOL(trace_hardirqs_on);
2163
2164/*
2165 * Hardirqs were disabled:
2166 */
Heiko Carstens6afe40b2008-10-28 11:14:58 +01002167void trace_hardirqs_off_caller(unsigned long ip)
Ingo Molnarfbb9ce92006-07-03 00:24:50 -07002168{
2169 struct task_struct *curr = current;
2170
Heiko Carstens6afe40b2008-10-28 11:14:58 +01002171 time_hardirqs_off(CALLER_ADDR0, ip);
Steven Rostedt81d68a92008-05-12 21:20:42 +02002172
Ingo Molnarfbb9ce92006-07-03 00:24:50 -07002173 if (unlikely(!debug_locks || current->lockdep_recursion))
2174 return;
2175
2176 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2177 return;
2178
2179 if (curr->hardirqs_enabled) {
2180 /*
2181 * We have done an ON -> OFF transition:
2182 */
2183 curr->hardirqs_enabled = 0;
Heiko Carstens6afe40b2008-10-28 11:14:58 +01002184 curr->hardirq_disable_ip = ip;
Ingo Molnarfbb9ce92006-07-03 00:24:50 -07002185 curr->hardirq_disable_event = ++curr->irq_events;
2186 debug_atomic_inc(&hardirqs_off_events);
2187 } else
2188 debug_atomic_inc(&redundant_hardirqs_off);
2189}
Steven Rostedt81d68a92008-05-12 21:20:42 +02002190EXPORT_SYMBOL(trace_hardirqs_off_caller);
Ingo Molnarfbb9ce92006-07-03 00:24:50 -07002191
Steven Rostedt1d09daa2008-05-12 21:20:55 +02002192void trace_hardirqs_off(void)
Steven Rostedt81d68a92008-05-12 21:20:42 +02002193{
2194 trace_hardirqs_off_caller(CALLER_ADDR0);
2195}
Ingo Molnarfbb9ce92006-07-03 00:24:50 -07002196EXPORT_SYMBOL(trace_hardirqs_off);
2197
2198/*
2199 * Softirqs will be enabled:
2200 */
2201void trace_softirqs_on(unsigned long ip)
2202{
2203 struct task_struct *curr = current;
2204
2205 if (unlikely(!debug_locks))
2206 return;
2207
2208 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2209 return;
2210
2211 if (curr->softirqs_enabled) {
2212 debug_atomic_inc(&redundant_softirqs_on);
2213 return;
2214 }
2215
2216 /*
2217 * We'll do an OFF -> ON transition:
2218 */
2219 curr->softirqs_enabled = 1;
2220 curr->softirq_enable_ip = ip;
2221 curr->softirq_enable_event = ++curr->irq_events;
2222 debug_atomic_inc(&softirqs_on_events);
2223 /*
2224 * We are going to turn softirqs on, so set the
2225 * usage bit for all held locks, if hardirqs are
2226 * enabled too:
2227 */
2228 if (curr->hardirqs_enabled)
Nick Piggincf40bd12009-01-21 08:12:39 +01002229 mark_held_locks(curr, SOFTIRQ);
Ingo Molnarfbb9ce92006-07-03 00:24:50 -07002230}
2231
2232/*
2233 * Softirqs were disabled:
2234 */
2235void trace_softirqs_off(unsigned long ip)
2236{
2237 struct task_struct *curr = current;
2238
2239 if (unlikely(!debug_locks))
2240 return;
2241
2242 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2243 return;
2244
2245 if (curr->softirqs_enabled) {
2246 /*
2247 * We have done an ON -> OFF transition:
2248 */
2249 curr->softirqs_enabled = 0;
2250 curr->softirq_disable_ip = ip;
2251 curr->softirq_disable_event = ++curr->irq_events;
2252 debug_atomic_inc(&softirqs_off_events);
2253 DEBUG_LOCKS_WARN_ON(!softirq_count());
2254 } else
2255 debug_atomic_inc(&redundant_softirqs_off);
2256}
2257
Nick Piggincf40bd12009-01-21 08:12:39 +01002258void lockdep_trace_alloc(gfp_t gfp_mask)
2259{
2260 struct task_struct *curr = current;
2261
2262 if (unlikely(!debug_locks))
2263 return;
2264
2265 /* no reclaim without waiting on it */
2266 if (!(gfp_mask & __GFP_WAIT))
2267 return;
2268
2269 /* this guy won't enter reclaim */
2270 if ((curr->flags & PF_MEMALLOC) && !(gfp_mask & __GFP_NOMEMALLOC))
2271 return;
2272
2273 /* We're only interested __GFP_FS allocations for now */
2274 if (!(gfp_mask & __GFP_FS))
2275 return;
2276
2277 if (DEBUG_LOCKS_WARN_ON(irqs_disabled()))
2278 return;
2279
2280 mark_held_locks(curr, RECLAIM_FS);
2281}
2282
Peter Zijlstra8e182572007-07-19 01:48:54 -07002283static int mark_irqflags(struct task_struct *curr, struct held_lock *hlock)
2284{
2285 /*
2286 * If non-trylock use in a hardirq or softirq context, then
2287 * mark the lock as used in these contexts:
2288 */
2289 if (!hlock->trylock) {
2290 if (hlock->read) {
2291 if (curr->hardirq_context)
2292 if (!mark_lock(curr, hlock,
2293 LOCK_USED_IN_HARDIRQ_READ))
2294 return 0;
2295 if (curr->softirq_context)
2296 if (!mark_lock(curr, hlock,
2297 LOCK_USED_IN_SOFTIRQ_READ))
2298 return 0;
2299 } else {
2300 if (curr->hardirq_context)
2301 if (!mark_lock(curr, hlock, LOCK_USED_IN_HARDIRQ))
2302 return 0;
2303 if (curr->softirq_context)
2304 if (!mark_lock(curr, hlock, LOCK_USED_IN_SOFTIRQ))
2305 return 0;
2306 }
2307 }
2308 if (!hlock->hardirqs_off) {
2309 if (hlock->read) {
2310 if (!mark_lock(curr, hlock,
Peter Zijlstra4fc95e82009-01-22 13:10:52 +01002311 LOCK_ENABLED_HARDIRQ_READ))
Peter Zijlstra8e182572007-07-19 01:48:54 -07002312 return 0;
2313 if (curr->softirqs_enabled)
2314 if (!mark_lock(curr, hlock,
Peter Zijlstra4fc95e82009-01-22 13:10:52 +01002315 LOCK_ENABLED_SOFTIRQ_READ))
Peter Zijlstra8e182572007-07-19 01:48:54 -07002316 return 0;
2317 } else {
2318 if (!mark_lock(curr, hlock,
Peter Zijlstra4fc95e82009-01-22 13:10:52 +01002319 LOCK_ENABLED_HARDIRQ))
Peter Zijlstra8e182572007-07-19 01:48:54 -07002320 return 0;
2321 if (curr->softirqs_enabled)
2322 if (!mark_lock(curr, hlock,
Peter Zijlstra4fc95e82009-01-22 13:10:52 +01002323 LOCK_ENABLED_SOFTIRQ))
Peter Zijlstra8e182572007-07-19 01:48:54 -07002324 return 0;
2325 }
2326 }
2327
Nick Piggincf40bd12009-01-21 08:12:39 +01002328 /*
2329 * We reuse the irq context infrastructure more broadly as a general
2330 * context checking code. This tests GFP_FS recursion (a lock taken
2331 * during reclaim for a GFP_FS allocation is held over a GFP_FS
2332 * allocation).
2333 */
2334 if (!hlock->trylock && (curr->lockdep_reclaim_gfp & __GFP_FS)) {
2335 if (hlock->read) {
2336 if (!mark_lock(curr, hlock, LOCK_USED_IN_RECLAIM_FS_READ))
2337 return 0;
2338 } else {
2339 if (!mark_lock(curr, hlock, LOCK_USED_IN_RECLAIM_FS))
2340 return 0;
2341 }
2342 }
2343
Peter Zijlstra8e182572007-07-19 01:48:54 -07002344 return 1;
2345}
2346
2347static int separate_irq_context(struct task_struct *curr,
2348 struct held_lock *hlock)
2349{
2350 unsigned int depth = curr->lockdep_depth;
2351
2352 /*
2353 * Keep track of points where we cross into an interrupt context:
2354 */
2355 hlock->irq_context = 2*(curr->hardirq_context ? 1 : 0) +
2356 curr->softirq_context;
2357 if (depth) {
2358 struct held_lock *prev_hlock;
2359
2360 prev_hlock = curr->held_locks + depth-1;
2361 /*
2362 * If we cross into another context, reset the
2363 * hash key (this also prevents the checking and the
2364 * adding of the dependency to 'prev'):
2365 */
2366 if (prev_hlock->irq_context != hlock->irq_context)
2367 return 1;
2368 }
2369 return 0;
2370}
2371
2372#else
2373
2374static inline
2375int mark_lock_irq(struct task_struct *curr, struct held_lock *this,
2376 enum lock_usage_bit new_bit)
2377{
2378 WARN_ON(1);
2379 return 1;
2380}
2381
2382static inline int mark_irqflags(struct task_struct *curr,
2383 struct held_lock *hlock)
2384{
2385 return 1;
2386}
2387
2388static inline int separate_irq_context(struct task_struct *curr,
2389 struct held_lock *hlock)
2390{
2391 return 0;
2392}
2393
Peter Zijlstra868a23a2009-02-15 00:25:21 +01002394void lockdep_trace_alloc(gfp_t gfp_mask)
2395{
2396}
2397
Ingo Molnarfbb9ce92006-07-03 00:24:50 -07002398#endif
2399
2400/*
Peter Zijlstra8e182572007-07-19 01:48:54 -07002401 * Mark a lock with a usage bit, and validate the state transition:
2402 */
Steven Rostedt1d09daa2008-05-12 21:20:55 +02002403static int mark_lock(struct task_struct *curr, struct held_lock *this,
Steven Rostedt0764d232008-05-12 21:20:44 +02002404 enum lock_usage_bit new_bit)
Peter Zijlstra8e182572007-07-19 01:48:54 -07002405{
2406 unsigned int new_mask = 1 << new_bit, ret = 1;
2407
2408 /*
2409 * If already set then do not dirty the cacheline,
2410 * nor do any checks:
2411 */
Dave Jonesf82b2172008-08-11 09:30:23 +02002412 if (likely(hlock_class(this)->usage_mask & new_mask))
Peter Zijlstra8e182572007-07-19 01:48:54 -07002413 return 1;
2414
2415 if (!graph_lock())
2416 return 0;
2417 /*
2418 * Make sure we didnt race:
2419 */
Dave Jonesf82b2172008-08-11 09:30:23 +02002420 if (unlikely(hlock_class(this)->usage_mask & new_mask)) {
Peter Zijlstra8e182572007-07-19 01:48:54 -07002421 graph_unlock();
2422 return 1;
2423 }
2424
Dave Jonesf82b2172008-08-11 09:30:23 +02002425 hlock_class(this)->usage_mask |= new_mask;
Peter Zijlstra8e182572007-07-19 01:48:54 -07002426
Dave Jonesf82b2172008-08-11 09:30:23 +02002427 if (!save_trace(hlock_class(this)->usage_traces + new_bit))
Peter Zijlstra8e182572007-07-19 01:48:54 -07002428 return 0;
2429
2430 switch (new_bit) {
Peter Zijlstra53464172009-01-22 14:15:53 +01002431#define LOCKDEP_STATE(__STATE) \
2432 case LOCK_USED_IN_##__STATE: \
2433 case LOCK_USED_IN_##__STATE##_READ: \
2434 case LOCK_ENABLED_##__STATE: \
2435 case LOCK_ENABLED_##__STATE##_READ:
2436#include "lockdep_states.h"
2437#undef LOCKDEP_STATE
Peter Zijlstra8e182572007-07-19 01:48:54 -07002438 ret = mark_lock_irq(curr, this, new_bit);
2439 if (!ret)
2440 return 0;
2441 break;
2442 case LOCK_USED:
Peter Zijlstra8e182572007-07-19 01:48:54 -07002443 debug_atomic_dec(&nr_unused_locks);
2444 break;
2445 default:
2446 if (!debug_locks_off_graph_unlock())
2447 return 0;
2448 WARN_ON(1);
2449 return 0;
2450 }
2451
2452 graph_unlock();
2453
2454 /*
2455 * We must printk outside of the graph_lock:
2456 */
2457 if (ret == 2) {
2458 printk("\nmarked lock as {%s}:\n", usage_str[new_bit]);
2459 print_lock(this);
2460 print_irqtrace_events(curr);
2461 dump_stack();
2462 }
2463
2464 return ret;
2465}
2466
2467/*
Ingo Molnarfbb9ce92006-07-03 00:24:50 -07002468 * Initialize a lock instance's lock-class mapping info:
2469 */
2470void lockdep_init_map(struct lockdep_map *lock, const char *name,
Peter Zijlstra4dfbb9d2006-10-11 01:45:14 -04002471 struct lock_class_key *key, int subclass)
Ingo Molnarfbb9ce92006-07-03 00:24:50 -07002472{
2473 if (unlikely(!debug_locks))
2474 return;
2475
2476 if (DEBUG_LOCKS_WARN_ON(!key))
2477 return;
2478 if (DEBUG_LOCKS_WARN_ON(!name))
2479 return;
2480 /*
2481 * Sanity check, the lock-class key must be persistent:
2482 */
2483 if (!static_obj(key)) {
2484 printk("BUG: key %p not in .data!\n", key);
2485 DEBUG_LOCKS_WARN_ON(1);
2486 return;
2487 }
2488 lock->name = name;
2489 lock->key = key;
Ingo Molnard6d897c2006-07-10 04:44:04 -07002490 lock->class_cache = NULL;
Peter Zijlstra96645672007-07-19 01:49:00 -07002491#ifdef CONFIG_LOCK_STAT
2492 lock->cpu = raw_smp_processor_id();
2493#endif
Peter Zijlstra4dfbb9d2006-10-11 01:45:14 -04002494 if (subclass)
2495 register_lock_class(lock, subclass, 1);
Ingo Molnarfbb9ce92006-07-03 00:24:50 -07002496}
Ingo Molnarfbb9ce92006-07-03 00:24:50 -07002497EXPORT_SYMBOL_GPL(lockdep_init_map);
2498
2499/*
2500 * This gets called for every mutex_lock*()/spin_lock*() operation.
2501 * We maintain the dependency maps and validate the locking attempt:
2502 */
2503static int __lock_acquire(struct lockdep_map *lock, unsigned int subclass,
2504 int trylock, int read, int check, int hardirqs_off,
Peter Zijlstra7531e2f2008-08-11 09:30:24 +02002505 struct lockdep_map *nest_lock, unsigned long ip)
Ingo Molnarfbb9ce92006-07-03 00:24:50 -07002506{
2507 struct task_struct *curr = current;
Ingo Molnard6d897c2006-07-10 04:44:04 -07002508 struct lock_class *class = NULL;
Ingo Molnarfbb9ce92006-07-03 00:24:50 -07002509 struct held_lock *hlock;
Ingo Molnarfbb9ce92006-07-03 00:24:50 -07002510 unsigned int depth, id;
2511 int chain_head = 0;
2512 u64 chain_key;
2513
Peter Zijlstraf20786f2007-07-19 01:48:56 -07002514 if (!prove_locking)
2515 check = 1;
2516
Ingo Molnarfbb9ce92006-07-03 00:24:50 -07002517 if (unlikely(!debug_locks))
2518 return 0;
2519
2520 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2521 return 0;
2522
2523 if (unlikely(subclass >= MAX_LOCKDEP_SUBCLASSES)) {
2524 debug_locks_off();
2525 printk("BUG: MAX_LOCKDEP_SUBCLASSES too low!\n");
2526 printk("turning off the locking correctness validator.\n");
2527 return 0;
2528 }
2529
Ingo Molnard6d897c2006-07-10 04:44:04 -07002530 if (!subclass)
2531 class = lock->class_cache;
2532 /*
2533 * Not cached yet or subclass?
2534 */
Ingo Molnarfbb9ce92006-07-03 00:24:50 -07002535 if (unlikely(!class)) {
Peter Zijlstra4dfbb9d2006-10-11 01:45:14 -04002536 class = register_lock_class(lock, subclass, 0);
Ingo Molnarfbb9ce92006-07-03 00:24:50 -07002537 if (!class)
2538 return 0;
2539 }
2540 debug_atomic_inc((atomic_t *)&class->ops);
2541 if (very_verbose(class)) {
2542 printk("\nacquire class [%p] %s", class->key, class->name);
2543 if (class->name_version > 1)
2544 printk("#%d", class->name_version);
2545 printk("\n");
2546 dump_stack();
2547 }
2548
2549 /*
2550 * Add the lock to the list of currently held locks.
2551 * (we dont increase the depth just yet, up until the
2552 * dependency checks are done)
2553 */
2554 depth = curr->lockdep_depth;
2555 if (DEBUG_LOCKS_WARN_ON(depth >= MAX_LOCK_DEPTH))
2556 return 0;
2557
2558 hlock = curr->held_locks + depth;
Dave Jonesf82b2172008-08-11 09:30:23 +02002559 if (DEBUG_LOCKS_WARN_ON(!class))
2560 return 0;
2561 hlock->class_idx = class - lock_classes + 1;
Ingo Molnarfbb9ce92006-07-03 00:24:50 -07002562 hlock->acquire_ip = ip;
2563 hlock->instance = lock;
Peter Zijlstra7531e2f2008-08-11 09:30:24 +02002564 hlock->nest_lock = nest_lock;
Ingo Molnarfbb9ce92006-07-03 00:24:50 -07002565 hlock->trylock = trylock;
2566 hlock->read = read;
2567 hlock->check = check;
Dmitry Baryshkov6951b122008-08-18 04:26:37 +04002568 hlock->hardirqs_off = !!hardirqs_off;
Peter Zijlstraf20786f2007-07-19 01:48:56 -07002569#ifdef CONFIG_LOCK_STAT
2570 hlock->waittime_stamp = 0;
2571 hlock->holdtime_stamp = sched_clock();
2572#endif
Ingo Molnarfbb9ce92006-07-03 00:24:50 -07002573
Peter Zijlstra8e182572007-07-19 01:48:54 -07002574 if (check == 2 && !mark_irqflags(curr, hlock))
2575 return 0;
2576
Ingo Molnarfbb9ce92006-07-03 00:24:50 -07002577 /* mark it as used: */
Jarek Poplawski4ff773bb2007-05-08 00:31:00 -07002578 if (!mark_lock(curr, hlock, LOCK_USED))
Ingo Molnarfbb9ce92006-07-03 00:24:50 -07002579 return 0;
Peter Zijlstra8e182572007-07-19 01:48:54 -07002580
Ingo Molnarfbb9ce92006-07-03 00:24:50 -07002581 /*
Gautham R Shenoy17aacfb2007-10-28 20:47:01 +01002582 * Calculate the chain hash: it's the combined hash of all the
Ingo Molnarfbb9ce92006-07-03 00:24:50 -07002583 * lock keys along the dependency chain. We save the hash value
2584 * at every step so that we can get the current hash easily
2585 * after unlock. The chain hash is then used to cache dependency
2586 * results.
2587 *
2588 * The 'key ID' is what is the most compact key value to drive
2589 * the hash, not class->key.
2590 */
2591 id = class - lock_classes;
2592 if (DEBUG_LOCKS_WARN_ON(id >= MAX_LOCKDEP_KEYS))
2593 return 0;
2594
2595 chain_key = curr->curr_chain_key;
2596 if (!depth) {
2597 if (DEBUG_LOCKS_WARN_ON(chain_key != 0))
2598 return 0;
2599 chain_head = 1;
2600 }
2601
2602 hlock->prev_chain_key = chain_key;
Peter Zijlstra8e182572007-07-19 01:48:54 -07002603 if (separate_irq_context(curr, hlock)) {
2604 chain_key = 0;
2605 chain_head = 1;
Ingo Molnarfbb9ce92006-07-03 00:24:50 -07002606 }
Ingo Molnarfbb9ce92006-07-03 00:24:50 -07002607 chain_key = iterate_chain_key(chain_key, id);
Ingo Molnarfbb9ce92006-07-03 00:24:50 -07002608
Gregory Haskins3aa416b2007-10-11 22:11:11 +02002609 if (!validate_chain(curr, lock, hlock, chain_head, chain_key))
Peter Zijlstra8e182572007-07-19 01:48:54 -07002610 return 0;
Jarek Poplawski381a2292007-02-10 01:44:58 -08002611
Gregory Haskins3aa416b2007-10-11 22:11:11 +02002612 curr->curr_chain_key = chain_key;
Ingo Molnarfbb9ce92006-07-03 00:24:50 -07002613 curr->lockdep_depth++;
2614 check_chain_key(curr);
Jarek Poplawski60e114d2007-02-20 13:58:00 -08002615#ifdef CONFIG_DEBUG_LOCKDEP
2616 if (unlikely(!debug_locks))
2617 return 0;
2618#endif
Ingo Molnarfbb9ce92006-07-03 00:24:50 -07002619 if (unlikely(curr->lockdep_depth >= MAX_LOCK_DEPTH)) {
2620 debug_locks_off();
2621 printk("BUG: MAX_LOCK_DEPTH too low!\n");
2622 printk("turning off the locking correctness validator.\n");
2623 return 0;
2624 }
Jarek Poplawski381a2292007-02-10 01:44:58 -08002625
Ingo Molnarfbb9ce92006-07-03 00:24:50 -07002626 if (unlikely(curr->lockdep_depth > max_lockdep_depth))
2627 max_lockdep_depth = curr->lockdep_depth;
2628
2629 return 1;
2630}
2631
2632static int
2633print_unlock_inbalance_bug(struct task_struct *curr, struct lockdep_map *lock,
2634 unsigned long ip)
2635{
2636 if (!debug_locks_off())
2637 return 0;
2638 if (debug_locks_silent)
2639 return 0;
2640
2641 printk("\n=====================================\n");
2642 printk( "[ BUG: bad unlock balance detected! ]\n");
2643 printk( "-------------------------------------\n");
2644 printk("%s/%d is trying to release lock (",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07002645 curr->comm, task_pid_nr(curr));
Ingo Molnarfbb9ce92006-07-03 00:24:50 -07002646 print_lockdep_cache(lock);
2647 printk(") at:\n");
2648 print_ip_sym(ip);
2649 printk("but there are no more locks to release!\n");
2650 printk("\nother info that might help us debug this:\n");
2651 lockdep_print_held_locks(curr);
2652
2653 printk("\nstack backtrace:\n");
2654 dump_stack();
2655
2656 return 0;
2657}
2658
2659/*
2660 * Common debugging checks for both nested and non-nested unlock:
2661 */
2662static int check_unlock(struct task_struct *curr, struct lockdep_map *lock,
2663 unsigned long ip)
2664{
2665 if (unlikely(!debug_locks))
2666 return 0;
2667 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2668 return 0;
2669
2670 if (curr->lockdep_depth <= 0)
2671 return print_unlock_inbalance_bug(curr, lock, ip);
2672
2673 return 1;
2674}
2675
Peter Zijlstra64aa3482008-08-11 09:30:21 +02002676static int
Peter Zijlstra00ef9f72008-12-04 09:00:17 +01002677__lock_set_class(struct lockdep_map *lock, const char *name,
2678 struct lock_class_key *key, unsigned int subclass,
2679 unsigned long ip)
Peter Zijlstra64aa3482008-08-11 09:30:21 +02002680{
2681 struct task_struct *curr = current;
2682 struct held_lock *hlock, *prev_hlock;
2683 struct lock_class *class;
2684 unsigned int depth;
2685 int i;
2686
2687 depth = curr->lockdep_depth;
2688 if (DEBUG_LOCKS_WARN_ON(!depth))
2689 return 0;
2690
2691 prev_hlock = NULL;
2692 for (i = depth-1; i >= 0; i--) {
2693 hlock = curr->held_locks + i;
2694 /*
2695 * We must not cross into another context:
2696 */
2697 if (prev_hlock && prev_hlock->irq_context != hlock->irq_context)
2698 break;
2699 if (hlock->instance == lock)
2700 goto found_it;
2701 prev_hlock = hlock;
2702 }
2703 return print_unlock_inbalance_bug(curr, lock, ip);
2704
2705found_it:
Peter Zijlstra00ef9f72008-12-04 09:00:17 +01002706 lockdep_init_map(lock, name, key, 0);
Peter Zijlstra64aa3482008-08-11 09:30:21 +02002707 class = register_lock_class(lock, subclass, 0);
Dave Jonesf82b2172008-08-11 09:30:23 +02002708 hlock->class_idx = class - lock_classes + 1;
Peter Zijlstra64aa3482008-08-11 09:30:21 +02002709
2710 curr->lockdep_depth = i;
2711 curr->curr_chain_key = hlock->prev_chain_key;
2712
2713 for (; i < depth; i++) {
2714 hlock = curr->held_locks + i;
2715 if (!__lock_acquire(hlock->instance,
Dave Jonesf82b2172008-08-11 09:30:23 +02002716 hlock_class(hlock)->subclass, hlock->trylock,
Peter Zijlstra64aa3482008-08-11 09:30:21 +02002717 hlock->read, hlock->check, hlock->hardirqs_off,
Peter Zijlstra7531e2f2008-08-11 09:30:24 +02002718 hlock->nest_lock, hlock->acquire_ip))
Peter Zijlstra64aa3482008-08-11 09:30:21 +02002719 return 0;
2720 }
2721
2722 if (DEBUG_LOCKS_WARN_ON(curr->lockdep_depth != depth))
2723 return 0;
2724 return 1;
2725}
2726
Ingo Molnarfbb9ce92006-07-03 00:24:50 -07002727/*
2728 * Remove the lock to the list of currently held locks in a
2729 * potentially non-nested (out of order) manner. This is a
2730 * relatively rare operation, as all the unlock APIs default
2731 * to nested mode (which uses lock_release()):
2732 */
2733static int
2734lock_release_non_nested(struct task_struct *curr,
2735 struct lockdep_map *lock, unsigned long ip)
2736{
2737 struct held_lock *hlock, *prev_hlock;
2738 unsigned int depth;
2739 int i;
2740
2741 /*
2742 * Check whether the lock exists in the current stack
2743 * of held locks:
2744 */
2745 depth = curr->lockdep_depth;
2746 if (DEBUG_LOCKS_WARN_ON(!depth))
2747 return 0;
2748
2749 prev_hlock = NULL;
2750 for (i = depth-1; i >= 0; i--) {
2751 hlock = curr->held_locks + i;
2752 /*
2753 * We must not cross into another context:
2754 */
2755 if (prev_hlock && prev_hlock->irq_context != hlock->irq_context)
2756 break;
2757 if (hlock->instance == lock)
2758 goto found_it;
2759 prev_hlock = hlock;
2760 }
2761 return print_unlock_inbalance_bug(curr, lock, ip);
2762
2763found_it:
Peter Zijlstraf20786f2007-07-19 01:48:56 -07002764 lock_release_holdtime(hlock);
2765
Ingo Molnarfbb9ce92006-07-03 00:24:50 -07002766 /*
2767 * We have the right lock to unlock, 'hlock' points to it.
2768 * Now we remove it from the stack, and add back the other
2769 * entries (if any), recalculating the hash along the way:
2770 */
2771 curr->lockdep_depth = i;
2772 curr->curr_chain_key = hlock->prev_chain_key;
2773
2774 for (i++; i < depth; i++) {
2775 hlock = curr->held_locks + i;
2776 if (!__lock_acquire(hlock->instance,
Dave Jonesf82b2172008-08-11 09:30:23 +02002777 hlock_class(hlock)->subclass, hlock->trylock,
Ingo Molnarfbb9ce92006-07-03 00:24:50 -07002778 hlock->read, hlock->check, hlock->hardirqs_off,
Peter Zijlstra7531e2f2008-08-11 09:30:24 +02002779 hlock->nest_lock, hlock->acquire_ip))
Ingo Molnarfbb9ce92006-07-03 00:24:50 -07002780 return 0;
2781 }
2782
2783 if (DEBUG_LOCKS_WARN_ON(curr->lockdep_depth != depth - 1))
2784 return 0;
2785 return 1;
2786}
2787
2788/*
2789 * Remove the lock to the list of currently held locks - this gets
2790 * called on mutex_unlock()/spin_unlock*() (or on a failed
2791 * mutex_lock_interruptible()). This is done for unlocks that nest
2792 * perfectly. (i.e. the current top of the lock-stack is unlocked)
2793 */
2794static int lock_release_nested(struct task_struct *curr,
2795 struct lockdep_map *lock, unsigned long ip)
2796{
2797 struct held_lock *hlock;
2798 unsigned int depth;
2799
2800 /*
2801 * Pop off the top of the lock stack:
2802 */
2803 depth = curr->lockdep_depth - 1;
2804 hlock = curr->held_locks + depth;
2805
2806 /*
2807 * Is the unlock non-nested:
2808 */
2809 if (hlock->instance != lock)
2810 return lock_release_non_nested(curr, lock, ip);
2811 curr->lockdep_depth--;
2812
2813 if (DEBUG_LOCKS_WARN_ON(!depth && (hlock->prev_chain_key != 0)))
2814 return 0;
2815
2816 curr->curr_chain_key = hlock->prev_chain_key;
2817
Peter Zijlstraf20786f2007-07-19 01:48:56 -07002818 lock_release_holdtime(hlock);
2819
Ingo Molnarfbb9ce92006-07-03 00:24:50 -07002820#ifdef CONFIG_DEBUG_LOCKDEP
2821 hlock->prev_chain_key = 0;
Dave Jonesf82b2172008-08-11 09:30:23 +02002822 hlock->class_idx = 0;
Ingo Molnarfbb9ce92006-07-03 00:24:50 -07002823 hlock->acquire_ip = 0;
2824 hlock->irq_context = 0;
2825#endif
2826 return 1;
2827}
2828
2829/*
2830 * Remove the lock to the list of currently held locks - this gets
2831 * called on mutex_unlock()/spin_unlock*() (or on a failed
2832 * mutex_lock_interruptible()). This is done for unlocks that nest
2833 * perfectly. (i.e. the current top of the lock-stack is unlocked)
2834 */
2835static void
2836__lock_release(struct lockdep_map *lock, int nested, unsigned long ip)
2837{
2838 struct task_struct *curr = current;
2839
2840 if (!check_unlock(curr, lock, ip))
2841 return;
2842
2843 if (nested) {
2844 if (!lock_release_nested(curr, lock, ip))
2845 return;
2846 } else {
2847 if (!lock_release_non_nested(curr, lock, ip))
2848 return;
2849 }
2850
2851 check_chain_key(curr);
2852}
2853
2854/*
2855 * Check whether we follow the irq-flags state precisely:
2856 */
Steven Rostedt1d09daa2008-05-12 21:20:55 +02002857static void check_flags(unsigned long flags)
Ingo Molnarfbb9ce92006-07-03 00:24:50 -07002858{
Ingo Molnar992860e2008-07-14 10:28:38 +02002859#if defined(CONFIG_PROVE_LOCKING) && defined(CONFIG_DEBUG_LOCKDEP) && \
2860 defined(CONFIG_TRACE_IRQFLAGS)
Ingo Molnarfbb9ce92006-07-03 00:24:50 -07002861 if (!debug_locks)
2862 return;
2863
Ingo Molnar5f9fa8a2007-12-07 19:02:47 +01002864 if (irqs_disabled_flags(flags)) {
2865 if (DEBUG_LOCKS_WARN_ON(current->hardirqs_enabled)) {
2866 printk("possible reason: unannotated irqs-off.\n");
2867 }
2868 } else {
2869 if (DEBUG_LOCKS_WARN_ON(!current->hardirqs_enabled)) {
2870 printk("possible reason: unannotated irqs-on.\n");
2871 }
2872 }
Ingo Molnarfbb9ce92006-07-03 00:24:50 -07002873
2874 /*
2875 * We dont accurately track softirq state in e.g.
2876 * hardirq contexts (such as on 4KSTACKS), so only
2877 * check if not in hardirq contexts:
2878 */
2879 if (!hardirq_count()) {
2880 if (softirq_count())
2881 DEBUG_LOCKS_WARN_ON(current->softirqs_enabled);
2882 else
2883 DEBUG_LOCKS_WARN_ON(!current->softirqs_enabled);
2884 }
2885
2886 if (!debug_locks)
2887 print_irqtrace_events(current);
2888#endif
2889}
2890
Peter Zijlstra00ef9f72008-12-04 09:00:17 +01002891void lock_set_class(struct lockdep_map *lock, const char *name,
2892 struct lock_class_key *key, unsigned int subclass,
2893 unsigned long ip)
Peter Zijlstra64aa3482008-08-11 09:30:21 +02002894{
2895 unsigned long flags;
2896
2897 if (unlikely(current->lockdep_recursion))
2898 return;
2899
2900 raw_local_irq_save(flags);
2901 current->lockdep_recursion = 1;
2902 check_flags(flags);
Peter Zijlstra00ef9f72008-12-04 09:00:17 +01002903 if (__lock_set_class(lock, name, key, subclass, ip))
Peter Zijlstra64aa3482008-08-11 09:30:21 +02002904 check_chain_key(current);
2905 current->lockdep_recursion = 0;
2906 raw_local_irq_restore(flags);
2907}
Peter Zijlstra00ef9f72008-12-04 09:00:17 +01002908EXPORT_SYMBOL_GPL(lock_set_class);
Peter Zijlstra64aa3482008-08-11 09:30:21 +02002909
Peter Zijlstraefed7922009-03-04 12:32:55 +01002910DEFINE_TRACE(lock_acquire);
2911
Ingo Molnarfbb9ce92006-07-03 00:24:50 -07002912/*
2913 * We are not always called with irqs disabled - do that here,
2914 * and also avoid lockdep recursion:
2915 */
Steven Rostedt1d09daa2008-05-12 21:20:55 +02002916void lock_acquire(struct lockdep_map *lock, unsigned int subclass,
Peter Zijlstra7531e2f2008-08-11 09:30:24 +02002917 int trylock, int read, int check,
2918 struct lockdep_map *nest_lock, unsigned long ip)
Ingo Molnarfbb9ce92006-07-03 00:24:50 -07002919{
2920 unsigned long flags;
2921
Peter Zijlstraefed7922009-03-04 12:32:55 +01002922 trace_lock_acquire(lock, subclass, trylock, read, check, nest_lock, ip);
2923
Ingo Molnarfbb9ce92006-07-03 00:24:50 -07002924 if (unlikely(current->lockdep_recursion))
2925 return;
2926
2927 raw_local_irq_save(flags);
2928 check_flags(flags);
2929
2930 current->lockdep_recursion = 1;
2931 __lock_acquire(lock, subclass, trylock, read, check,
Peter Zijlstra7531e2f2008-08-11 09:30:24 +02002932 irqs_disabled_flags(flags), nest_lock, ip);
Ingo Molnarfbb9ce92006-07-03 00:24:50 -07002933 current->lockdep_recursion = 0;
2934 raw_local_irq_restore(flags);
2935}
Ingo Molnarfbb9ce92006-07-03 00:24:50 -07002936EXPORT_SYMBOL_GPL(lock_acquire);
2937
Peter Zijlstraefed7922009-03-04 12:32:55 +01002938DEFINE_TRACE(lock_release);
2939
Steven Rostedt1d09daa2008-05-12 21:20:55 +02002940void lock_release(struct lockdep_map *lock, int nested,
Steven Rostedt0764d232008-05-12 21:20:44 +02002941 unsigned long ip)
Ingo Molnarfbb9ce92006-07-03 00:24:50 -07002942{
2943 unsigned long flags;
2944
Peter Zijlstraefed7922009-03-04 12:32:55 +01002945 trace_lock_release(lock, nested, ip);
2946
Ingo Molnarfbb9ce92006-07-03 00:24:50 -07002947 if (unlikely(current->lockdep_recursion))
2948 return;
2949
2950 raw_local_irq_save(flags);
2951 check_flags(flags);
2952 current->lockdep_recursion = 1;
2953 __lock_release(lock, nested, ip);
2954 current->lockdep_recursion = 0;
2955 raw_local_irq_restore(flags);
2956}
Ingo Molnarfbb9ce92006-07-03 00:24:50 -07002957EXPORT_SYMBOL_GPL(lock_release);
2958
Nick Piggincf40bd12009-01-21 08:12:39 +01002959void lockdep_set_current_reclaim_state(gfp_t gfp_mask)
2960{
2961 current->lockdep_reclaim_gfp = gfp_mask;
2962}
2963
2964void lockdep_clear_current_reclaim_state(void)
2965{
2966 current->lockdep_reclaim_gfp = 0;
2967}
2968
Peter Zijlstraf20786f2007-07-19 01:48:56 -07002969#ifdef CONFIG_LOCK_STAT
2970static int
2971print_lock_contention_bug(struct task_struct *curr, struct lockdep_map *lock,
2972 unsigned long ip)
2973{
2974 if (!debug_locks_off())
2975 return 0;
2976 if (debug_locks_silent)
2977 return 0;
2978
2979 printk("\n=================================\n");
2980 printk( "[ BUG: bad contention detected! ]\n");
2981 printk( "---------------------------------\n");
2982 printk("%s/%d is trying to contend lock (",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07002983 curr->comm, task_pid_nr(curr));
Peter Zijlstraf20786f2007-07-19 01:48:56 -07002984 print_lockdep_cache(lock);
2985 printk(") at:\n");
2986 print_ip_sym(ip);
2987 printk("but there are no locks held!\n");
2988 printk("\nother info that might help us debug this:\n");
2989 lockdep_print_held_locks(curr);
2990
2991 printk("\nstack backtrace:\n");
2992 dump_stack();
2993
2994 return 0;
2995}
2996
2997static void
2998__lock_contended(struct lockdep_map *lock, unsigned long ip)
2999{
3000 struct task_struct *curr = current;
3001 struct held_lock *hlock, *prev_hlock;
3002 struct lock_class_stats *stats;
3003 unsigned int depth;
Peter Zijlstrac7e78cf2008-10-16 23:17:09 +02003004 int i, contention_point, contending_point;
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003005
3006 depth = curr->lockdep_depth;
3007 if (DEBUG_LOCKS_WARN_ON(!depth))
3008 return;
3009
3010 prev_hlock = NULL;
3011 for (i = depth-1; i >= 0; i--) {
3012 hlock = curr->held_locks + i;
3013 /*
3014 * We must not cross into another context:
3015 */
3016 if (prev_hlock && prev_hlock->irq_context != hlock->irq_context)
3017 break;
3018 if (hlock->instance == lock)
3019 goto found_it;
3020 prev_hlock = hlock;
3021 }
3022 print_lock_contention_bug(curr, lock, ip);
3023 return;
3024
3025found_it:
3026 hlock->waittime_stamp = sched_clock();
3027
Peter Zijlstrac7e78cf2008-10-16 23:17:09 +02003028 contention_point = lock_point(hlock_class(hlock)->contention_point, ip);
3029 contending_point = lock_point(hlock_class(hlock)->contending_point,
3030 lock->ip);
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003031
Dave Jonesf82b2172008-08-11 09:30:23 +02003032 stats = get_lock_stats(hlock_class(hlock));
Peter Zijlstrac7e78cf2008-10-16 23:17:09 +02003033 if (contention_point < LOCKSTAT_POINTS)
3034 stats->contention_point[contention_point]++;
3035 if (contending_point < LOCKSTAT_POINTS)
3036 stats->contending_point[contending_point]++;
Peter Zijlstra96645672007-07-19 01:49:00 -07003037 if (lock->cpu != smp_processor_id())
3038 stats->bounces[bounce_contended + !!hlock->read]++;
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003039 put_lock_stats(stats);
3040}
3041
3042static void
Peter Zijlstrac7e78cf2008-10-16 23:17:09 +02003043__lock_acquired(struct lockdep_map *lock, unsigned long ip)
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003044{
3045 struct task_struct *curr = current;
3046 struct held_lock *hlock, *prev_hlock;
3047 struct lock_class_stats *stats;
3048 unsigned int depth;
3049 u64 now;
Peter Zijlstra96645672007-07-19 01:49:00 -07003050 s64 waittime = 0;
3051 int i, cpu;
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003052
3053 depth = curr->lockdep_depth;
3054 if (DEBUG_LOCKS_WARN_ON(!depth))
3055 return;
3056
3057 prev_hlock = NULL;
3058 for (i = depth-1; i >= 0; i--) {
3059 hlock = curr->held_locks + i;
3060 /*
3061 * We must not cross into another context:
3062 */
3063 if (prev_hlock && prev_hlock->irq_context != hlock->irq_context)
3064 break;
3065 if (hlock->instance == lock)
3066 goto found_it;
3067 prev_hlock = hlock;
3068 }
3069 print_lock_contention_bug(curr, lock, _RET_IP_);
3070 return;
3071
3072found_it:
Peter Zijlstra96645672007-07-19 01:49:00 -07003073 cpu = smp_processor_id();
3074 if (hlock->waittime_stamp) {
3075 now = sched_clock();
3076 waittime = now - hlock->waittime_stamp;
3077 hlock->holdtime_stamp = now;
3078 }
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003079
Dave Jonesf82b2172008-08-11 09:30:23 +02003080 stats = get_lock_stats(hlock_class(hlock));
Peter Zijlstra96645672007-07-19 01:49:00 -07003081 if (waittime) {
3082 if (hlock->read)
3083 lock_time_inc(&stats->read_waittime, waittime);
3084 else
3085 lock_time_inc(&stats->write_waittime, waittime);
3086 }
3087 if (lock->cpu != cpu)
3088 stats->bounces[bounce_acquired + !!hlock->read]++;
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003089 put_lock_stats(stats);
Peter Zijlstra96645672007-07-19 01:49:00 -07003090
3091 lock->cpu = cpu;
Peter Zijlstrac7e78cf2008-10-16 23:17:09 +02003092 lock->ip = ip;
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003093}
3094
Peter Zijlstraefed7922009-03-04 12:32:55 +01003095DEFINE_TRACE(lock_contended);
3096
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003097void lock_contended(struct lockdep_map *lock, unsigned long ip)
3098{
3099 unsigned long flags;
3100
Peter Zijlstraefed7922009-03-04 12:32:55 +01003101 trace_lock_contended(lock, ip);
3102
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003103 if (unlikely(!lock_stat))
3104 return;
3105
3106 if (unlikely(current->lockdep_recursion))
3107 return;
3108
3109 raw_local_irq_save(flags);
3110 check_flags(flags);
3111 current->lockdep_recursion = 1;
3112 __lock_contended(lock, ip);
3113 current->lockdep_recursion = 0;
3114 raw_local_irq_restore(flags);
3115}
3116EXPORT_SYMBOL_GPL(lock_contended);
3117
Peter Zijlstraefed7922009-03-04 12:32:55 +01003118DEFINE_TRACE(lock_acquired);
3119
Peter Zijlstrac7e78cf2008-10-16 23:17:09 +02003120void lock_acquired(struct lockdep_map *lock, unsigned long ip)
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003121{
3122 unsigned long flags;
3123
Peter Zijlstraefed7922009-03-04 12:32:55 +01003124 trace_lock_acquired(lock, ip);
3125
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003126 if (unlikely(!lock_stat))
3127 return;
3128
3129 if (unlikely(current->lockdep_recursion))
3130 return;
3131
3132 raw_local_irq_save(flags);
3133 check_flags(flags);
3134 current->lockdep_recursion = 1;
Peter Zijlstrac7e78cf2008-10-16 23:17:09 +02003135 __lock_acquired(lock, ip);
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003136 current->lockdep_recursion = 0;
3137 raw_local_irq_restore(flags);
3138}
3139EXPORT_SYMBOL_GPL(lock_acquired);
3140#endif
3141
Ingo Molnarfbb9ce92006-07-03 00:24:50 -07003142/*
3143 * Used by the testsuite, sanitize the validator state
3144 * after a simulated failure:
3145 */
3146
3147void lockdep_reset(void)
3148{
3149 unsigned long flags;
Ingo Molnar23d95a02006-12-13 00:34:40 -08003150 int i;
Ingo Molnarfbb9ce92006-07-03 00:24:50 -07003151
3152 raw_local_irq_save(flags);
3153 current->curr_chain_key = 0;
3154 current->lockdep_depth = 0;
3155 current->lockdep_recursion = 0;
3156 memset(current->held_locks, 0, MAX_LOCK_DEPTH*sizeof(struct held_lock));
3157 nr_hardirq_chains = 0;
3158 nr_softirq_chains = 0;
3159 nr_process_chains = 0;
3160 debug_locks = 1;
Ingo Molnar23d95a02006-12-13 00:34:40 -08003161 for (i = 0; i < CHAINHASH_SIZE; i++)
3162 INIT_LIST_HEAD(chainhash_table + i);
Ingo Molnarfbb9ce92006-07-03 00:24:50 -07003163 raw_local_irq_restore(flags);
3164}
3165
3166static void zap_class(struct lock_class *class)
3167{
3168 int i;
3169
3170 /*
3171 * Remove all dependencies this lock is
3172 * involved in:
3173 */
3174 for (i = 0; i < nr_list_entries; i++) {
3175 if (list_entries[i].class == class)
3176 list_del_rcu(&list_entries[i].entry);
3177 }
3178 /*
3179 * Unhash the class and remove it from the all_lock_classes list:
3180 */
3181 list_del_rcu(&class->hash_entry);
3182 list_del_rcu(&class->lock_entry);
3183
Rabin Vincent8bfe0292008-08-11 09:30:26 +02003184 class->key = NULL;
Ingo Molnarfbb9ce92006-07-03 00:24:50 -07003185}
3186
Arjan van de Venfabe8742008-01-24 07:00:45 +01003187static inline int within(const void *addr, void *start, unsigned long size)
Ingo Molnarfbb9ce92006-07-03 00:24:50 -07003188{
3189 return addr >= start && addr < start + size;
3190}
3191
3192void lockdep_free_key_range(void *start, unsigned long size)
3193{
3194 struct lock_class *class, *next;
3195 struct list_head *head;
3196 unsigned long flags;
3197 int i;
Nick Piggin5a26db52008-01-16 09:51:58 +01003198 int locked;
Ingo Molnarfbb9ce92006-07-03 00:24:50 -07003199
3200 raw_local_irq_save(flags);
Nick Piggin5a26db52008-01-16 09:51:58 +01003201 locked = graph_lock();
Ingo Molnarfbb9ce92006-07-03 00:24:50 -07003202
3203 /*
3204 * Unhash all classes that were created by this module:
3205 */
3206 for (i = 0; i < CLASSHASH_SIZE; i++) {
3207 head = classhash_table + i;
3208 if (list_empty(head))
3209 continue;
Arjan van de Venfabe8742008-01-24 07:00:45 +01003210 list_for_each_entry_safe(class, next, head, hash_entry) {
Ingo Molnarfbb9ce92006-07-03 00:24:50 -07003211 if (within(class->key, start, size))
3212 zap_class(class);
Arjan van de Venfabe8742008-01-24 07:00:45 +01003213 else if (within(class->name, start, size))
3214 zap_class(class);
3215 }
Ingo Molnarfbb9ce92006-07-03 00:24:50 -07003216 }
3217
Nick Piggin5a26db52008-01-16 09:51:58 +01003218 if (locked)
3219 graph_unlock();
Ingo Molnarfbb9ce92006-07-03 00:24:50 -07003220 raw_local_irq_restore(flags);
3221}
3222
3223void lockdep_reset_lock(struct lockdep_map *lock)
3224{
Ingo Molnard6d897c2006-07-10 04:44:04 -07003225 struct lock_class *class, *next;
Ingo Molnarfbb9ce92006-07-03 00:24:50 -07003226 struct list_head *head;
3227 unsigned long flags;
3228 int i, j;
Nick Piggin5a26db52008-01-16 09:51:58 +01003229 int locked;
Ingo Molnarfbb9ce92006-07-03 00:24:50 -07003230
3231 raw_local_irq_save(flags);
Ingo Molnarfbb9ce92006-07-03 00:24:50 -07003232
3233 /*
Ingo Molnard6d897c2006-07-10 04:44:04 -07003234 * Remove all classes this lock might have:
Ingo Molnarfbb9ce92006-07-03 00:24:50 -07003235 */
Ingo Molnard6d897c2006-07-10 04:44:04 -07003236 for (j = 0; j < MAX_LOCKDEP_SUBCLASSES; j++) {
3237 /*
3238 * If the class exists we look it up and zap it:
3239 */
3240 class = look_up_lock_class(lock, j);
3241 if (class)
3242 zap_class(class);
3243 }
3244 /*
3245 * Debug check: in the end all mapped classes should
3246 * be gone.
3247 */
Nick Piggin5a26db52008-01-16 09:51:58 +01003248 locked = graph_lock();
Ingo Molnarfbb9ce92006-07-03 00:24:50 -07003249 for (i = 0; i < CLASSHASH_SIZE; i++) {
3250 head = classhash_table + i;
3251 if (list_empty(head))
3252 continue;
3253 list_for_each_entry_safe(class, next, head, hash_entry) {
Ingo Molnard6d897c2006-07-10 04:44:04 -07003254 if (unlikely(class == lock->class_cache)) {
Ingo Molnar74c383f2006-12-13 00:34:43 -08003255 if (debug_locks_off_graph_unlock())
3256 WARN_ON(1);
Ingo Molnard6d897c2006-07-10 04:44:04 -07003257 goto out_restore;
Ingo Molnarfbb9ce92006-07-03 00:24:50 -07003258 }
3259 }
3260 }
Nick Piggin5a26db52008-01-16 09:51:58 +01003261 if (locked)
3262 graph_unlock();
Ingo Molnard6d897c2006-07-10 04:44:04 -07003263
3264out_restore:
Ingo Molnarfbb9ce92006-07-03 00:24:50 -07003265 raw_local_irq_restore(flags);
3266}
3267
Sam Ravnborg14999932007-02-28 20:12:31 -08003268void lockdep_init(void)
Ingo Molnarfbb9ce92006-07-03 00:24:50 -07003269{
3270 int i;
3271
3272 /*
3273 * Some architectures have their own start_kernel()
3274 * code which calls lockdep_init(), while we also
3275 * call lockdep_init() from the start_kernel() itself,
3276 * and we want to initialize the hashes only once:
3277 */
3278 if (lockdep_initialized)
3279 return;
3280
3281 for (i = 0; i < CLASSHASH_SIZE; i++)
3282 INIT_LIST_HEAD(classhash_table + i);
3283
3284 for (i = 0; i < CHAINHASH_SIZE; i++)
3285 INIT_LIST_HEAD(chainhash_table + i);
3286
3287 lockdep_initialized = 1;
3288}
3289
3290void __init lockdep_info(void)
3291{
3292 printk("Lock dependency validator: Copyright (c) 2006 Red Hat, Inc., Ingo Molnar\n");
3293
Li Zefanb0788ca2008-11-21 15:57:32 +08003294 printk("... MAX_LOCKDEP_SUBCLASSES: %lu\n", MAX_LOCKDEP_SUBCLASSES);
Ingo Molnarfbb9ce92006-07-03 00:24:50 -07003295 printk("... MAX_LOCK_DEPTH: %lu\n", MAX_LOCK_DEPTH);
3296 printk("... MAX_LOCKDEP_KEYS: %lu\n", MAX_LOCKDEP_KEYS);
Li Zefanb0788ca2008-11-21 15:57:32 +08003297 printk("... CLASSHASH_SIZE: %lu\n", CLASSHASH_SIZE);
Ingo Molnarfbb9ce92006-07-03 00:24:50 -07003298 printk("... MAX_LOCKDEP_ENTRIES: %lu\n", MAX_LOCKDEP_ENTRIES);
3299 printk("... MAX_LOCKDEP_CHAINS: %lu\n", MAX_LOCKDEP_CHAINS);
3300 printk("... CHAINHASH_SIZE: %lu\n", CHAINHASH_SIZE);
3301
3302 printk(" memory used by lock dependency info: %lu kB\n",
3303 (sizeof(struct lock_class) * MAX_LOCKDEP_KEYS +
3304 sizeof(struct list_head) * CLASSHASH_SIZE +
3305 sizeof(struct lock_list) * MAX_LOCKDEP_ENTRIES +
3306 sizeof(struct lock_chain) * MAX_LOCKDEP_CHAINS +
3307 sizeof(struct list_head) * CHAINHASH_SIZE) / 1024);
3308
3309 printk(" per task-struct memory footprint: %lu bytes\n",
3310 sizeof(struct held_lock) * MAX_LOCK_DEPTH);
3311
3312#ifdef CONFIG_DEBUG_LOCKDEP
Johannes Bergc71063c2007-07-19 01:49:02 -07003313 if (lockdep_init_error) {
3314 printk("WARNING: lockdep init error! Arch code didn't call lockdep_init() early enough?\n");
3315 printk("Call stack leading to lockdep invocation was:\n");
3316 print_stack_trace(&lockdep_init_trace, 0);
3317 }
Ingo Molnarfbb9ce92006-07-03 00:24:50 -07003318#endif
3319}
3320
Ingo Molnarfbb9ce92006-07-03 00:24:50 -07003321static void
3322print_freed_lock_bug(struct task_struct *curr, const void *mem_from,
Arjan van de Ven55794a42006-07-10 04:44:03 -07003323 const void *mem_to, struct held_lock *hlock)
Ingo Molnarfbb9ce92006-07-03 00:24:50 -07003324{
3325 if (!debug_locks_off())
3326 return;
3327 if (debug_locks_silent)
3328 return;
3329
3330 printk("\n=========================\n");
3331 printk( "[ BUG: held lock freed! ]\n");
3332 printk( "-------------------------\n");
3333 printk("%s/%d is freeing memory %p-%p, with a lock still held there!\n",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07003334 curr->comm, task_pid_nr(curr), mem_from, mem_to-1);
Arjan van de Ven55794a42006-07-10 04:44:03 -07003335 print_lock(hlock);
Ingo Molnarfbb9ce92006-07-03 00:24:50 -07003336 lockdep_print_held_locks(curr);
3337
3338 printk("\nstack backtrace:\n");
3339 dump_stack();
3340}
3341
Oleg Nesterov54561782007-12-05 15:46:09 +01003342static inline int not_in_range(const void* mem_from, unsigned long mem_len,
3343 const void* lock_from, unsigned long lock_len)
3344{
3345 return lock_from + lock_len <= mem_from ||
3346 mem_from + mem_len <= lock_from;
3347}
3348
Ingo Molnarfbb9ce92006-07-03 00:24:50 -07003349/*
3350 * Called when kernel memory is freed (or unmapped), or if a lock
3351 * is destroyed or reinitialized - this code checks whether there is
3352 * any held lock in the memory range of <from> to <to>:
3353 */
3354void debug_check_no_locks_freed(const void *mem_from, unsigned long mem_len)
3355{
Ingo Molnarfbb9ce92006-07-03 00:24:50 -07003356 struct task_struct *curr = current;
3357 struct held_lock *hlock;
3358 unsigned long flags;
3359 int i;
3360
3361 if (unlikely(!debug_locks))
3362 return;
3363
3364 local_irq_save(flags);
3365 for (i = 0; i < curr->lockdep_depth; i++) {
3366 hlock = curr->held_locks + i;
3367
Oleg Nesterov54561782007-12-05 15:46:09 +01003368 if (not_in_range(mem_from, mem_len, hlock->instance,
3369 sizeof(*hlock->instance)))
Ingo Molnarfbb9ce92006-07-03 00:24:50 -07003370 continue;
3371
Oleg Nesterov54561782007-12-05 15:46:09 +01003372 print_freed_lock_bug(curr, mem_from, mem_from + mem_len, hlock);
Ingo Molnarfbb9ce92006-07-03 00:24:50 -07003373 break;
3374 }
3375 local_irq_restore(flags);
3376}
Peter Zijlstraed075362006-12-06 20:35:24 -08003377EXPORT_SYMBOL_GPL(debug_check_no_locks_freed);
Ingo Molnarfbb9ce92006-07-03 00:24:50 -07003378
3379static void print_held_locks_bug(struct task_struct *curr)
3380{
3381 if (!debug_locks_off())
3382 return;
3383 if (debug_locks_silent)
3384 return;
3385
3386 printk("\n=====================================\n");
3387 printk( "[ BUG: lock held at task exit time! ]\n");
3388 printk( "-------------------------------------\n");
3389 printk("%s/%d is exiting with locks still held!\n",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07003390 curr->comm, task_pid_nr(curr));
Ingo Molnarfbb9ce92006-07-03 00:24:50 -07003391 lockdep_print_held_locks(curr);
3392
3393 printk("\nstack backtrace:\n");
3394 dump_stack();
3395}
3396
3397void debug_check_no_locks_held(struct task_struct *task)
3398{
3399 if (unlikely(task->lockdep_depth > 0))
3400 print_held_locks_bug(task);
3401}
3402
3403void debug_show_all_locks(void)
3404{
3405 struct task_struct *g, *p;
3406 int count = 10;
3407 int unlock = 1;
3408
Jarek Poplawski9c35dd72007-03-22 00:11:28 -08003409 if (unlikely(!debug_locks)) {
3410 printk("INFO: lockdep is turned off.\n");
3411 return;
3412 }
Ingo Molnarfbb9ce92006-07-03 00:24:50 -07003413 printk("\nShowing all locks held in the system:\n");
3414
3415 /*
3416 * Here we try to get the tasklist_lock as hard as possible,
3417 * if not successful after 2 seconds we ignore it (but keep
3418 * trying). This is to enable a debug printout even if a
3419 * tasklist_lock-holding task deadlocks or crashes.
3420 */
3421retry:
3422 if (!read_trylock(&tasklist_lock)) {
3423 if (count == 10)
3424 printk("hm, tasklist_lock locked, retrying... ");
3425 if (count) {
3426 count--;
3427 printk(" #%d", 10-count);
3428 mdelay(200);
3429 goto retry;
3430 }
3431 printk(" ignoring it.\n");
3432 unlock = 0;
qinghuang feng46fec7a2008-10-28 17:24:28 +08003433 } else {
3434 if (count != 10)
3435 printk(KERN_CONT " locked it.\n");
Ingo Molnarfbb9ce92006-07-03 00:24:50 -07003436 }
Ingo Molnarfbb9ce92006-07-03 00:24:50 -07003437
3438 do_each_thread(g, p) {
Ingo Molnar85684872007-12-05 15:46:09 +01003439 /*
3440 * It's not reliable to print a task's held locks
3441 * if it's not sleeping (or if it's not the current
3442 * task):
3443 */
3444 if (p->state == TASK_RUNNING && p != current)
3445 continue;
Ingo Molnarfbb9ce92006-07-03 00:24:50 -07003446 if (p->lockdep_depth)
3447 lockdep_print_held_locks(p);
3448 if (!unlock)
3449 if (read_trylock(&tasklist_lock))
3450 unlock = 1;
3451 } while_each_thread(g, p);
3452
3453 printk("\n");
3454 printk("=============================================\n\n");
3455
3456 if (unlock)
3457 read_unlock(&tasklist_lock);
3458}
Ingo Molnarfbb9ce92006-07-03 00:24:50 -07003459EXPORT_SYMBOL_GPL(debug_show_all_locks);
3460
Ingo Molnar82a1fcb2008-01-25 21:08:02 +01003461/*
3462 * Careful: only use this function if you are sure that
3463 * the task cannot run in parallel!
3464 */
3465void __debug_show_held_locks(struct task_struct *task)
Ingo Molnarfbb9ce92006-07-03 00:24:50 -07003466{
Jarek Poplawski9c35dd72007-03-22 00:11:28 -08003467 if (unlikely(!debug_locks)) {
3468 printk("INFO: lockdep is turned off.\n");
3469 return;
3470 }
Ingo Molnarfbb9ce92006-07-03 00:24:50 -07003471 lockdep_print_held_locks(task);
3472}
Ingo Molnar82a1fcb2008-01-25 21:08:02 +01003473EXPORT_SYMBOL_GPL(__debug_show_held_locks);
3474
3475void debug_show_held_locks(struct task_struct *task)
3476{
3477 __debug_show_held_locks(task);
3478}
Ingo Molnarfbb9ce92006-07-03 00:24:50 -07003479EXPORT_SYMBOL_GPL(debug_show_held_locks);
Peter Zijlstrab351d162007-10-11 22:11:12 +02003480
3481void lockdep_sys_exit(void)
3482{
3483 struct task_struct *curr = current;
3484
3485 if (unlikely(curr->lockdep_depth)) {
3486 if (!debug_locks_off())
3487 return;
3488 printk("\n================================================\n");
3489 printk( "[ BUG: lock held when returning to user space! ]\n");
3490 printk( "------------------------------------------------\n");
3491 printk("%s/%d is leaving the kernel with locks still held!\n",
3492 curr->comm, curr->pid);
3493 lockdep_print_held_locks(curr);
3494 }
3495}