blob: 1cedb00e3e7a9d664165af50c8c1f7d5574d5265 [file] [log] [blame]
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001/*
2 * kernel/lockdep.c
3 *
4 * Runtime locking correctness validator
5 *
6 * Started by Ingo Molnar:
7 *
Peter Zijlstra4b32d0a2007-07-19 01:48:59 -07008 * Copyright (C) 2006,2007 Red Hat, Inc., Ingo Molnar <mingo@redhat.com>
9 * Copyright (C) 2007 Red Hat, Inc., Peter Zijlstra <pzijlstr@redhat.com>
Ingo Molnarfbb9ce952006-07-03 00:24:50 -070010 *
11 * this code maps all the lock dependencies as they occur in a live kernel
12 * and will warn about the following classes of locking bugs:
13 *
14 * - lock inversion scenarios
15 * - circular lock dependencies
16 * - hardirq/softirq safe/unsafe locking bugs
17 *
18 * Bugs are reported even if the current locking scenario does not cause
19 * any deadlock at this point.
20 *
21 * I.e. if anytime in the past two locks were taken in a different order,
22 * even if it happened for another task, even if those were different
23 * locks (but of the same class as this lock), this code will detect it.
24 *
25 * Thanks to Arjan van de Ven for coming up with the initial idea of
26 * mapping lock dependencies runtime.
27 */
Steven Rostedta5e25882008-12-02 15:34:05 -050028#define DISABLE_BRANCH_PROFILING
Ingo Molnarfbb9ce952006-07-03 00:24:50 -070029#include <linux/mutex.h>
30#include <linux/sched.h>
31#include <linux/delay.h>
32#include <linux/module.h>
33#include <linux/proc_fs.h>
34#include <linux/seq_file.h>
35#include <linux/spinlock.h>
36#include <linux/kallsyms.h>
37#include <linux/interrupt.h>
38#include <linux/stacktrace.h>
39#include <linux/debug_locks.h>
40#include <linux/irqflags.h>
Dave Jones99de0552006-09-29 02:00:10 -070041#include <linux/utsname.h>
Peter Zijlstra4b32d0a2007-07-19 01:48:59 -070042#include <linux/hash.h>
Steven Rostedt81d68a92008-05-12 21:20:42 +020043#include <linux/ftrace.h>
Peter Zijlstrab4b136f2009-01-29 14:50:36 +010044#include <linux/stringify.h>
Ming Leid588e462009-07-16 15:44:29 +020045#include <linux/bitops.h>
Peter Zijlstraaf012962009-07-16 15:44:29 +020046
Ingo Molnarfbb9ce952006-07-03 00:24:50 -070047#include <asm/sections.h>
48
49#include "lockdep_internals.h"
50
Steven Rostedta8d154b2009-04-10 09:36:00 -040051#define CREATE_TRACE_POINTS
Steven Rostedtad8d75f2009-04-14 19:39:12 -040052#include <trace/events/lockdep.h>
Steven Rostedta8d154b2009-04-10 09:36:00 -040053
Peter Zijlstraf20786f2007-07-19 01:48:56 -070054#ifdef CONFIG_PROVE_LOCKING
55int prove_locking = 1;
56module_param(prove_locking, int, 0644);
57#else
58#define prove_locking 0
59#endif
60
61#ifdef CONFIG_LOCK_STAT
62int lock_stat = 1;
63module_param(lock_stat, int, 0644);
64#else
65#define lock_stat 0
66#endif
67
Ingo Molnarfbb9ce952006-07-03 00:24:50 -070068/*
Ingo Molnar74c383f2006-12-13 00:34:43 -080069 * lockdep_lock: protects the lockdep graph, the hashes and the
70 * class/list/hash allocators.
Ingo Molnarfbb9ce952006-07-03 00:24:50 -070071 *
72 * This is one of the rare exceptions where it's justified
73 * to use a raw spinlock - we really dont want the spinlock
Ingo Molnar74c383f2006-12-13 00:34:43 -080074 * code to recurse back into the lockdep code...
Ingo Molnarfbb9ce952006-07-03 00:24:50 -070075 */
Ingo Molnar74c383f2006-12-13 00:34:43 -080076static raw_spinlock_t lockdep_lock = (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED;
77
78static int graph_lock(void)
79{
80 __raw_spin_lock(&lockdep_lock);
81 /*
82 * Make sure that if another CPU detected a bug while
83 * walking the graph we dont change it (while the other
84 * CPU is busy printing out stuff with the graph lock
85 * dropped already)
86 */
87 if (!debug_locks) {
88 __raw_spin_unlock(&lockdep_lock);
89 return 0;
90 }
Steven Rostedtbb065af2008-05-12 21:21:00 +020091 /* prevent any recursions within lockdep from causing deadlocks */
92 current->lockdep_recursion++;
Ingo Molnar74c383f2006-12-13 00:34:43 -080093 return 1;
94}
95
96static inline int graph_unlock(void)
97{
Jarek Poplawski381a2292007-02-10 01:44:58 -080098 if (debug_locks && !__raw_spin_is_locked(&lockdep_lock))
99 return DEBUG_LOCKS_WARN_ON(1);
100
Steven Rostedtbb065af2008-05-12 21:21:00 +0200101 current->lockdep_recursion--;
Ingo Molnar74c383f2006-12-13 00:34:43 -0800102 __raw_spin_unlock(&lockdep_lock);
103 return 0;
104}
105
106/*
107 * Turn lock debugging off and return with 0 if it was off already,
108 * and also release the graph lock:
109 */
110static inline int debug_locks_off_graph_unlock(void)
111{
112 int ret = debug_locks_off();
113
114 __raw_spin_unlock(&lockdep_lock);
115
116 return ret;
117}
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700118
119static int lockdep_initialized;
120
121unsigned long nr_list_entries;
Peter Zijlstraaf012962009-07-16 15:44:29 +0200122static struct lock_list list_entries[MAX_LOCKDEP_ENTRIES];
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700123
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700124/*
125 * All data structures here are protected by the global debug_lock.
126 *
127 * Mutex key structs only get allocated, once during bootup, and never
128 * get freed - this significantly simplifies the debugging code.
129 */
130unsigned long nr_lock_classes;
131static struct lock_class lock_classes[MAX_LOCKDEP_KEYS];
132
Dave Jonesf82b2172008-08-11 09:30:23 +0200133static inline struct lock_class *hlock_class(struct held_lock *hlock)
134{
135 if (!hlock->class_idx) {
136 DEBUG_LOCKS_WARN_ON(1);
137 return NULL;
138 }
139 return lock_classes + hlock->class_idx - 1;
140}
141
Peter Zijlstraf20786f2007-07-19 01:48:56 -0700142#ifdef CONFIG_LOCK_STAT
143static DEFINE_PER_CPU(struct lock_class_stats[MAX_LOCKDEP_KEYS], lock_stats);
144
Peter Zijlstrac7e78cf2008-10-16 23:17:09 +0200145static int lock_point(unsigned long points[], unsigned long ip)
Peter Zijlstraf20786f2007-07-19 01:48:56 -0700146{
147 int i;
148
Peter Zijlstrac7e78cf2008-10-16 23:17:09 +0200149 for (i = 0; i < LOCKSTAT_POINTS; i++) {
150 if (points[i] == 0) {
151 points[i] = ip;
Peter Zijlstraf20786f2007-07-19 01:48:56 -0700152 break;
153 }
Peter Zijlstrac7e78cf2008-10-16 23:17:09 +0200154 if (points[i] == ip)
Peter Zijlstraf20786f2007-07-19 01:48:56 -0700155 break;
156 }
157
158 return i;
159}
160
161static void lock_time_inc(struct lock_time *lt, s64 time)
162{
163 if (time > lt->max)
164 lt->max = time;
165
166 if (time < lt->min || !lt->min)
167 lt->min = time;
168
169 lt->total += time;
170 lt->nr++;
171}
172
Peter Zijlstrac46261d2007-07-19 01:48:57 -0700173static inline void lock_time_add(struct lock_time *src, struct lock_time *dst)
174{
175 dst->min += src->min;
176 dst->max += src->max;
177 dst->total += src->total;
178 dst->nr += src->nr;
179}
180
181struct lock_class_stats lock_stats(struct lock_class *class)
182{
183 struct lock_class_stats stats;
184 int cpu, i;
185
186 memset(&stats, 0, sizeof(struct lock_class_stats));
187 for_each_possible_cpu(cpu) {
188 struct lock_class_stats *pcs =
189 &per_cpu(lock_stats, cpu)[class - lock_classes];
190
191 for (i = 0; i < ARRAY_SIZE(stats.contention_point); i++)
192 stats.contention_point[i] += pcs->contention_point[i];
193
Peter Zijlstrac7e78cf2008-10-16 23:17:09 +0200194 for (i = 0; i < ARRAY_SIZE(stats.contending_point); i++)
195 stats.contending_point[i] += pcs->contending_point[i];
196
Peter Zijlstrac46261d2007-07-19 01:48:57 -0700197 lock_time_add(&pcs->read_waittime, &stats.read_waittime);
198 lock_time_add(&pcs->write_waittime, &stats.write_waittime);
199
200 lock_time_add(&pcs->read_holdtime, &stats.read_holdtime);
201 lock_time_add(&pcs->write_holdtime, &stats.write_holdtime);
Peter Zijlstra96645672007-07-19 01:49:00 -0700202
203 for (i = 0; i < ARRAY_SIZE(stats.bounces); i++)
204 stats.bounces[i] += pcs->bounces[i];
Peter Zijlstrac46261d2007-07-19 01:48:57 -0700205 }
206
207 return stats;
208}
209
210void clear_lock_stats(struct lock_class *class)
211{
212 int cpu;
213
214 for_each_possible_cpu(cpu) {
215 struct lock_class_stats *cpu_stats =
216 &per_cpu(lock_stats, cpu)[class - lock_classes];
217
218 memset(cpu_stats, 0, sizeof(struct lock_class_stats));
219 }
220 memset(class->contention_point, 0, sizeof(class->contention_point));
Peter Zijlstrac7e78cf2008-10-16 23:17:09 +0200221 memset(class->contending_point, 0, sizeof(class->contending_point));
Peter Zijlstrac46261d2007-07-19 01:48:57 -0700222}
223
Peter Zijlstraf20786f2007-07-19 01:48:56 -0700224static struct lock_class_stats *get_lock_stats(struct lock_class *class)
225{
226 return &get_cpu_var(lock_stats)[class - lock_classes];
227}
228
229static void put_lock_stats(struct lock_class_stats *stats)
230{
231 put_cpu_var(lock_stats);
232}
233
234static void lock_release_holdtime(struct held_lock *hlock)
235{
236 struct lock_class_stats *stats;
237 s64 holdtime;
238
239 if (!lock_stat)
240 return;
241
242 holdtime = sched_clock() - hlock->holdtime_stamp;
243
Dave Jonesf82b2172008-08-11 09:30:23 +0200244 stats = get_lock_stats(hlock_class(hlock));
Peter Zijlstraf20786f2007-07-19 01:48:56 -0700245 if (hlock->read)
246 lock_time_inc(&stats->read_holdtime, holdtime);
247 else
248 lock_time_inc(&stats->write_holdtime, holdtime);
249 put_lock_stats(stats);
250}
251#else
252static inline void lock_release_holdtime(struct held_lock *hlock)
253{
254}
255#endif
256
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700257/*
258 * We keep a global list of all lock classes. The list only grows,
259 * never shrinks. The list is only accessed with the lockdep
260 * spinlock lock held.
261 */
262LIST_HEAD(all_lock_classes);
263
264/*
265 * The lockdep classes are in a hash-table as well, for fast lookup:
266 */
267#define CLASSHASH_BITS (MAX_LOCKDEP_KEYS_BITS - 1)
268#define CLASSHASH_SIZE (1UL << CLASSHASH_BITS)
Peter Zijlstra4b32d0a2007-07-19 01:48:59 -0700269#define __classhashfn(key) hash_long((unsigned long)key, CLASSHASH_BITS)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700270#define classhashentry(key) (classhash_table + __classhashfn((key)))
271
272static struct list_head classhash_table[CLASSHASH_SIZE];
273
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700274/*
275 * We put the lock dependency chains into a hash-table as well, to cache
276 * their existence:
277 */
278#define CHAINHASH_BITS (MAX_LOCKDEP_CHAINS_BITS-1)
279#define CHAINHASH_SIZE (1UL << CHAINHASH_BITS)
Peter Zijlstra4b32d0a2007-07-19 01:48:59 -0700280#define __chainhashfn(chain) hash_long(chain, CHAINHASH_BITS)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700281#define chainhashentry(chain) (chainhash_table + __chainhashfn((chain)))
282
283static struct list_head chainhash_table[CHAINHASH_SIZE];
284
285/*
286 * The hash key of the lock dependency chains is a hash itself too:
287 * it's a hash of all locks taken up to that lock, including that lock.
288 * It's a 64-bit hash, because it's important for the keys to be
289 * unique.
290 */
291#define iterate_chain_key(key1, key2) \
Ingo Molnar03cbc352006-09-29 02:01:46 -0700292 (((key1) << MAX_LOCKDEP_KEYS_BITS) ^ \
293 ((key1) >> (64-MAX_LOCKDEP_KEYS_BITS)) ^ \
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700294 (key2))
295
Steven Rostedt1d09daa2008-05-12 21:20:55 +0200296void lockdep_off(void)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700297{
298 current->lockdep_recursion++;
299}
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700300EXPORT_SYMBOL(lockdep_off);
301
Steven Rostedt1d09daa2008-05-12 21:20:55 +0200302void lockdep_on(void)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700303{
304 current->lockdep_recursion--;
305}
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700306EXPORT_SYMBOL(lockdep_on);
307
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700308/*
309 * Debugging switches:
310 */
311
312#define VERBOSE 0
Ingo Molnar33e94e92006-12-13 00:34:41 -0800313#define VERY_VERBOSE 0
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700314
315#if VERBOSE
316# define HARDIRQ_VERBOSE 1
317# define SOFTIRQ_VERBOSE 1
Nick Piggincf40bd12009-01-21 08:12:39 +0100318# define RECLAIM_VERBOSE 1
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700319#else
320# define HARDIRQ_VERBOSE 0
321# define SOFTIRQ_VERBOSE 0
Nick Piggincf40bd12009-01-21 08:12:39 +0100322# define RECLAIM_VERBOSE 0
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700323#endif
324
Nick Piggincf40bd12009-01-21 08:12:39 +0100325#if VERBOSE || HARDIRQ_VERBOSE || SOFTIRQ_VERBOSE || RECLAIM_VERBOSE
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700326/*
327 * Quick filtering for interesting events:
328 */
329static int class_filter(struct lock_class *class)
330{
Andi Kleenf9829cc2006-07-10 04:44:01 -0700331#if 0
332 /* Example */
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700333 if (class->name_version == 1 &&
Andi Kleenf9829cc2006-07-10 04:44:01 -0700334 !strcmp(class->name, "lockname"))
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700335 return 1;
336 if (class->name_version == 1 &&
Andi Kleenf9829cc2006-07-10 04:44:01 -0700337 !strcmp(class->name, "&struct->lockfield"))
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700338 return 1;
Andi Kleenf9829cc2006-07-10 04:44:01 -0700339#endif
Ingo Molnara6640892006-12-13 00:34:39 -0800340 /* Filter everything else. 1 would be to allow everything else */
341 return 0;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700342}
343#endif
344
345static int verbose(struct lock_class *class)
346{
347#if VERBOSE
348 return class_filter(class);
349#endif
350 return 0;
351}
352
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700353/*
354 * Stack-trace: tightly packed array of stack backtrace
Ingo Molnar74c383f2006-12-13 00:34:43 -0800355 * addresses. Protected by the graph_lock.
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700356 */
357unsigned long nr_stack_trace_entries;
358static unsigned long stack_trace[MAX_STACK_TRACE_ENTRIES];
359
360static int save_trace(struct stack_trace *trace)
361{
362 trace->nr_entries = 0;
363 trace->max_entries = MAX_STACK_TRACE_ENTRIES - nr_stack_trace_entries;
364 trace->entries = stack_trace + nr_stack_trace_entries;
365
Andi Kleen5a1b3992006-09-26 10:52:34 +0200366 trace->skip = 3;
Andi Kleen5a1b3992006-09-26 10:52:34 +0200367
Christoph Hellwigab1b6f02007-05-08 00:23:29 -0700368 save_stack_trace(trace);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700369
370 trace->max_entries = trace->nr_entries;
371
372 nr_stack_trace_entries += trace->nr_entries;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700373
374 if (nr_stack_trace_entries == MAX_STACK_TRACE_ENTRIES) {
Ingo Molnar74c383f2006-12-13 00:34:43 -0800375 if (!debug_locks_off_graph_unlock())
376 return 0;
377
378 printk("BUG: MAX_STACK_TRACE_ENTRIES too low!\n");
379 printk("turning off the locking correctness validator.\n");
380 dump_stack();
381
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700382 return 0;
383 }
384
385 return 1;
386}
387
388unsigned int nr_hardirq_chains;
389unsigned int nr_softirq_chains;
390unsigned int nr_process_chains;
391unsigned int max_lockdep_depth;
392unsigned int max_recursion_depth;
393
394#ifdef CONFIG_DEBUG_LOCKDEP
395/*
396 * We cannot printk in early bootup code. Not even early_printk()
397 * might work. So we mark any initialization errors and printk
398 * about it later on, in lockdep_info().
399 */
400static int lockdep_init_error;
Johannes Bergc71063c2007-07-19 01:49:02 -0700401static unsigned long lockdep_init_trace_data[20];
402static struct stack_trace lockdep_init_trace = {
403 .max_entries = ARRAY_SIZE(lockdep_init_trace_data),
404 .entries = lockdep_init_trace_data,
405};
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700406
407/*
408 * Various lockdep statistics:
409 */
410atomic_t chain_lookup_hits;
411atomic_t chain_lookup_misses;
412atomic_t hardirqs_on_events;
413atomic_t hardirqs_off_events;
414atomic_t redundant_hardirqs_on;
415atomic_t redundant_hardirqs_off;
416atomic_t softirqs_on_events;
417atomic_t softirqs_off_events;
418atomic_t redundant_softirqs_on;
419atomic_t redundant_softirqs_off;
420atomic_t nr_unused_locks;
421atomic_t nr_cyclic_checks;
422atomic_t nr_cyclic_check_recursions;
423atomic_t nr_find_usage_forwards_checks;
424atomic_t nr_find_usage_forwards_recursions;
425atomic_t nr_find_usage_backwards_checks;
426atomic_t nr_find_usage_backwards_recursions;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700427#endif
428
429/*
430 * Locking printouts:
431 */
432
Peter Zijlstrafabe9c42009-01-22 14:51:01 +0100433#define __USAGE(__STATE) \
Peter Zijlstrab4b136f2009-01-29 14:50:36 +0100434 [LOCK_USED_IN_##__STATE] = "IN-"__stringify(__STATE)"-W", \
435 [LOCK_ENABLED_##__STATE] = __stringify(__STATE)"-ON-W", \
436 [LOCK_USED_IN_##__STATE##_READ] = "IN-"__stringify(__STATE)"-R",\
437 [LOCK_ENABLED_##__STATE##_READ] = __stringify(__STATE)"-ON-R",
Peter Zijlstrafabe9c42009-01-22 14:51:01 +0100438
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700439static const char *usage_str[] =
440{
Peter Zijlstrafabe9c42009-01-22 14:51:01 +0100441#define LOCKDEP_STATE(__STATE) __USAGE(__STATE)
442#include "lockdep_states.h"
443#undef LOCKDEP_STATE
444 [LOCK_USED] = "INITIAL USE",
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700445};
446
447const char * __get_key_name(struct lockdep_subclass_key *key, char *str)
448{
Alexey Dobriyanffb45122007-05-08 00:28:41 -0700449 return kallsyms_lookup((unsigned long)key, NULL, NULL, NULL, str);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700450}
451
Peter Zijlstra3ff176c2009-01-22 17:40:42 +0100452static inline unsigned long lock_flag(enum lock_usage_bit bit)
453{
454 return 1UL << bit;
455}
456
457static char get_usage_char(struct lock_class *class, enum lock_usage_bit bit)
458{
459 char c = '.';
460
461 if (class->usage_mask & lock_flag(bit + 2))
462 c = '+';
463 if (class->usage_mask & lock_flag(bit)) {
464 c = '-';
465 if (class->usage_mask & lock_flag(bit + 2))
466 c = '?';
467 }
468
469 return c;
470}
471
Peter Zijlstraf510b232009-01-22 17:53:47 +0100472void get_usage_chars(struct lock_class *class, char usage[LOCK_USAGE_CHARS])
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700473{
Peter Zijlstraf510b232009-01-22 17:53:47 +0100474 int i = 0;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700475
Peter Zijlstraf510b232009-01-22 17:53:47 +0100476#define LOCKDEP_STATE(__STATE) \
477 usage[i++] = get_usage_char(class, LOCK_USED_IN_##__STATE); \
478 usage[i++] = get_usage_char(class, LOCK_USED_IN_##__STATE##_READ);
479#include "lockdep_states.h"
480#undef LOCKDEP_STATE
481
482 usage[i] = '\0';
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700483}
484
485static void print_lock_name(struct lock_class *class)
486{
Peter Zijlstraf510b232009-01-22 17:53:47 +0100487 char str[KSYM_NAME_LEN], usage[LOCK_USAGE_CHARS];
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700488 const char *name;
489
Peter Zijlstraf510b232009-01-22 17:53:47 +0100490 get_usage_chars(class, usage);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700491
492 name = class->name;
493 if (!name) {
494 name = __get_key_name(class->key, str);
495 printk(" (%s", name);
496 } else {
497 printk(" (%s", name);
498 if (class->name_version > 1)
499 printk("#%d", class->name_version);
500 if (class->subclass)
501 printk("/%d", class->subclass);
502 }
Peter Zijlstraf510b232009-01-22 17:53:47 +0100503 printk("){%s}", usage);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700504}
505
506static void print_lockdep_cache(struct lockdep_map *lock)
507{
508 const char *name;
Tejun Heo9281ace2007-07-17 04:03:51 -0700509 char str[KSYM_NAME_LEN];
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700510
511 name = lock->name;
512 if (!name)
513 name = __get_key_name(lock->key->subkeys, str);
514
515 printk("%s", name);
516}
517
518static void print_lock(struct held_lock *hlock)
519{
Dave Jonesf82b2172008-08-11 09:30:23 +0200520 print_lock_name(hlock_class(hlock));
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700521 printk(", at: ");
522 print_ip_sym(hlock->acquire_ip);
523}
524
525static void lockdep_print_held_locks(struct task_struct *curr)
526{
527 int i, depth = curr->lockdep_depth;
528
529 if (!depth) {
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -0700530 printk("no locks held by %s/%d.\n", curr->comm, task_pid_nr(curr));
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700531 return;
532 }
533 printk("%d lock%s held by %s/%d:\n",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -0700534 depth, depth > 1 ? "s" : "", curr->comm, task_pid_nr(curr));
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700535
536 for (i = 0; i < depth; i++) {
537 printk(" #%d: ", i);
538 print_lock(curr->held_locks + i);
539 }
540}
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700541
Dave Jones99de0552006-09-29 02:00:10 -0700542static void print_kernel_version(void)
543{
Serge E. Hallyn96b644b2006-10-02 02:18:13 -0700544 printk("%s %.*s\n", init_utsname()->release,
545 (int)strcspn(init_utsname()->version, " "),
546 init_utsname()->version);
Dave Jones99de0552006-09-29 02:00:10 -0700547}
548
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700549static int very_verbose(struct lock_class *class)
550{
551#if VERY_VERBOSE
552 return class_filter(class);
553#endif
554 return 0;
555}
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700556
557/*
558 * Is this the address of a static object:
559 */
560static int static_obj(void *obj)
561{
562 unsigned long start = (unsigned long) &_stext,
563 end = (unsigned long) &_end,
564 addr = (unsigned long) obj;
565#ifdef CONFIG_SMP
566 int i;
567#endif
568
569 /*
570 * static variable?
571 */
572 if ((addr >= start) && (addr < end))
573 return 1;
574
575#ifdef CONFIG_SMP
576 /*
577 * percpu var?
578 */
579 for_each_possible_cpu(i) {
580 start = (unsigned long) &__per_cpu_start + per_cpu_offset(i);
Ingo Molnar1ff56832006-11-17 19:57:22 +0100581 end = (unsigned long) &__per_cpu_start + PERCPU_ENOUGH_ROOM
582 + per_cpu_offset(i);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700583
584 if ((addr >= start) && (addr < end))
585 return 1;
586 }
587#endif
588
589 /*
590 * module var?
591 */
592 return is_module_address(addr);
593}
594
595/*
596 * To make lock name printouts unique, we calculate a unique
597 * class->name_version generation counter:
598 */
599static int count_matching_names(struct lock_class *new_class)
600{
601 struct lock_class *class;
602 int count = 0;
603
604 if (!new_class->name)
605 return 0;
606
607 list_for_each_entry(class, &all_lock_classes, lock_entry) {
608 if (new_class->key - new_class->subclass == class->key)
609 return class->name_version;
610 if (class->name && !strcmp(class->name, new_class->name))
611 count = max(count, class->name_version);
612 }
613
614 return count + 1;
615}
616
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700617/*
618 * Register a lock's class in the hash-table, if the class is not present
619 * yet. Otherwise we look it up. We cache the result in the lock object
620 * itself, so actual lookup of the hash should be once per lock object.
621 */
622static inline struct lock_class *
Ingo Molnard6d897c2006-07-10 04:44:04 -0700623look_up_lock_class(struct lockdep_map *lock, unsigned int subclass)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700624{
625 struct lockdep_subclass_key *key;
626 struct list_head *hash_head;
627 struct lock_class *class;
628
629#ifdef CONFIG_DEBUG_LOCKDEP
630 /*
631 * If the architecture calls into lockdep before initializing
632 * the hashes then we'll warn about it later. (we cannot printk
633 * right now)
634 */
635 if (unlikely(!lockdep_initialized)) {
636 lockdep_init();
637 lockdep_init_error = 1;
Johannes Bergc71063c2007-07-19 01:49:02 -0700638 save_stack_trace(&lockdep_init_trace);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700639 }
640#endif
641
642 /*
643 * Static locks do not have their class-keys yet - for them the key
644 * is the lock object itself:
645 */
646 if (unlikely(!lock->key))
647 lock->key = (void *)lock;
648
649 /*
650 * NOTE: the class-key must be unique. For dynamic locks, a static
651 * lock_class_key variable is passed in through the mutex_init()
652 * (or spin_lock_init()) call - which acts as the key. For static
653 * locks we use the lock object itself as the key.
654 */
Peter Zijlstra4b32d0a2007-07-19 01:48:59 -0700655 BUILD_BUG_ON(sizeof(struct lock_class_key) >
656 sizeof(struct lockdep_map));
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700657
658 key = lock->key->subkeys + subclass;
659
660 hash_head = classhashentry(key);
661
662 /*
663 * We can walk the hash lockfree, because the hash only
664 * grows, and we are careful when adding entries to the end:
665 */
Peter Zijlstra4b32d0a2007-07-19 01:48:59 -0700666 list_for_each_entry(class, hash_head, hash_entry) {
667 if (class->key == key) {
668 WARN_ON_ONCE(class->name != lock->name);
Ingo Molnard6d897c2006-07-10 04:44:04 -0700669 return class;
Peter Zijlstra4b32d0a2007-07-19 01:48:59 -0700670 }
671 }
Ingo Molnard6d897c2006-07-10 04:44:04 -0700672
673 return NULL;
674}
675
676/*
677 * Register a lock's class in the hash-table, if the class is not present
678 * yet. Otherwise we look it up. We cache the result in the lock object
679 * itself, so actual lookup of the hash should be once per lock object.
680 */
681static inline struct lock_class *
Peter Zijlstra4dfbb9d2006-10-11 01:45:14 -0400682register_lock_class(struct lockdep_map *lock, unsigned int subclass, int force)
Ingo Molnard6d897c2006-07-10 04:44:04 -0700683{
684 struct lockdep_subclass_key *key;
685 struct list_head *hash_head;
686 struct lock_class *class;
Ingo Molnar70e45062006-12-06 20:40:50 -0800687 unsigned long flags;
Ingo Molnard6d897c2006-07-10 04:44:04 -0700688
689 class = look_up_lock_class(lock, subclass);
690 if (likely(class))
691 return class;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700692
693 /*
694 * Debug-check: all keys must be persistent!
695 */
696 if (!static_obj(lock->key)) {
697 debug_locks_off();
698 printk("INFO: trying to register non-static key.\n");
699 printk("the code is fine but needs lockdep annotation.\n");
700 printk("turning off the locking correctness validator.\n");
701 dump_stack();
702
703 return NULL;
704 }
705
Ingo Molnard6d897c2006-07-10 04:44:04 -0700706 key = lock->key->subkeys + subclass;
707 hash_head = classhashentry(key);
708
Ingo Molnar70e45062006-12-06 20:40:50 -0800709 raw_local_irq_save(flags);
Ingo Molnar74c383f2006-12-13 00:34:43 -0800710 if (!graph_lock()) {
711 raw_local_irq_restore(flags);
712 return NULL;
713 }
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700714 /*
715 * We have to do the hash-walk again, to avoid races
716 * with another CPU:
717 */
718 list_for_each_entry(class, hash_head, hash_entry)
719 if (class->key == key)
720 goto out_unlock_set;
721 /*
722 * Allocate a new key from the static array, and add it to
723 * the hash:
724 */
725 if (nr_lock_classes >= MAX_LOCKDEP_KEYS) {
Ingo Molnar74c383f2006-12-13 00:34:43 -0800726 if (!debug_locks_off_graph_unlock()) {
727 raw_local_irq_restore(flags);
728 return NULL;
729 }
Ingo Molnar70e45062006-12-06 20:40:50 -0800730 raw_local_irq_restore(flags);
Ingo Molnar74c383f2006-12-13 00:34:43 -0800731
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700732 printk("BUG: MAX_LOCKDEP_KEYS too low!\n");
733 printk("turning off the locking correctness validator.\n");
Peter Zijlstraeedeeab2009-03-18 12:38:47 +0100734 dump_stack();
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700735 return NULL;
736 }
737 class = lock_classes + nr_lock_classes++;
738 debug_atomic_inc(&nr_unused_locks);
739 class->key = key;
740 class->name = lock->name;
741 class->subclass = subclass;
742 INIT_LIST_HEAD(&class->lock_entry);
743 INIT_LIST_HEAD(&class->locks_before);
744 INIT_LIST_HEAD(&class->locks_after);
745 class->name_version = count_matching_names(class);
746 /*
747 * We use RCU's safe list-add method to make
748 * parallel walking of the hash-list safe:
749 */
750 list_add_tail_rcu(&class->hash_entry, hash_head);
Dale Farnsworth14811972008-02-25 23:03:02 +0100751 /*
752 * Add it to the global list of classes:
753 */
754 list_add_tail_rcu(&class->lock_entry, &all_lock_classes);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700755
756 if (verbose(class)) {
Ingo Molnar74c383f2006-12-13 00:34:43 -0800757 graph_unlock();
Ingo Molnar70e45062006-12-06 20:40:50 -0800758 raw_local_irq_restore(flags);
Ingo Molnar74c383f2006-12-13 00:34:43 -0800759
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700760 printk("\nnew class %p: %s", class->key, class->name);
761 if (class->name_version > 1)
762 printk("#%d", class->name_version);
763 printk("\n");
764 dump_stack();
Ingo Molnar74c383f2006-12-13 00:34:43 -0800765
Ingo Molnar70e45062006-12-06 20:40:50 -0800766 raw_local_irq_save(flags);
Ingo Molnar74c383f2006-12-13 00:34:43 -0800767 if (!graph_lock()) {
768 raw_local_irq_restore(flags);
769 return NULL;
770 }
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700771 }
772out_unlock_set:
Ingo Molnar74c383f2006-12-13 00:34:43 -0800773 graph_unlock();
Ingo Molnar70e45062006-12-06 20:40:50 -0800774 raw_local_irq_restore(flags);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700775
Peter Zijlstra4dfbb9d2006-10-11 01:45:14 -0400776 if (!subclass || force)
Ingo Molnard6d897c2006-07-10 04:44:04 -0700777 lock->class_cache = class;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700778
Jarek Poplawski381a2292007-02-10 01:44:58 -0800779 if (DEBUG_LOCKS_WARN_ON(class->subclass != subclass))
780 return NULL;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700781
782 return class;
783}
784
Peter Zijlstraca58abc2007-07-19 01:48:53 -0700785#ifdef CONFIG_PROVE_LOCKING
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700786/*
Peter Zijlstra8e182572007-07-19 01:48:54 -0700787 * Allocate a lockdep entry. (assumes the graph_lock held, returns
788 * with NULL on failure)
789 */
790static struct lock_list *alloc_list_entry(void)
791{
792 if (nr_list_entries >= MAX_LOCKDEP_ENTRIES) {
793 if (!debug_locks_off_graph_unlock())
794 return NULL;
795
796 printk("BUG: MAX_LOCKDEP_ENTRIES too low!\n");
797 printk("turning off the locking correctness validator.\n");
Peter Zijlstraeedeeab2009-03-18 12:38:47 +0100798 dump_stack();
Peter Zijlstra8e182572007-07-19 01:48:54 -0700799 return NULL;
800 }
801 return list_entries + nr_list_entries++;
802}
803
804/*
805 * Add a new dependency to the head of the list:
806 */
807static int add_lock_to_list(struct lock_class *class, struct lock_class *this,
808 struct list_head *head, unsigned long ip, int distance)
809{
810 struct lock_list *entry;
811 /*
812 * Lock not present yet - get a new dependency struct and
813 * add it to the list:
814 */
815 entry = alloc_list_entry();
816 if (!entry)
817 return 0;
818
Peter Zijlstra8e182572007-07-19 01:48:54 -0700819 if (!save_trace(&entry->trace))
820 return 0;
821
Zhu Yi74870172008-08-27 14:33:00 +0800822 entry->class = this;
823 entry->distance = distance;
Peter Zijlstra8e182572007-07-19 01:48:54 -0700824 /*
825 * Since we never remove from the dependency list, the list can
826 * be walked lockless by other CPUs, it's only allocation
827 * that must be protected by the spinlock. But this also means
828 * we must make new entries visible only once writes to the
829 * entry become visible - hence the RCU op:
830 */
831 list_add_tail_rcu(&entry->entry, head);
832
833 return 1;
834}
835
Peter Zijlstraaf012962009-07-16 15:44:29 +0200836/*For good efficiency of modular, we use power of 2*/
837#define MAX_CIRCULAR_QUEUE_SIZE 4096UL
838#define CQ_MASK (MAX_CIRCULAR_QUEUE_SIZE-1)
839
840/* The circular_queue and helpers is used to implement the
841 * breadth-first search(BFS)algorithem, by which we can build
842 * the shortest path from the next lock to be acquired to the
843 * previous held lock if there is a circular between them.
844 * */
845struct circular_queue {
846 unsigned long element[MAX_CIRCULAR_QUEUE_SIZE];
847 unsigned int front, rear;
848};
849
850static struct circular_queue lock_cq;
851static unsigned long bfs_accessed[BITS_TO_LONGS(MAX_LOCKDEP_ENTRIES)];
852
Ming Lei12f3dfd2009-07-16 15:44:29 +0200853unsigned int max_bfs_queue_depth;
Peter Zijlstraaf012962009-07-16 15:44:29 +0200854
855static inline void __cq_init(struct circular_queue *cq)
856{
857 cq->front = cq->rear = 0;
858 bitmap_zero(bfs_accessed, MAX_LOCKDEP_ENTRIES);
859}
860
861static inline int __cq_empty(struct circular_queue *cq)
862{
863 return (cq->front == cq->rear);
864}
865
866static inline int __cq_full(struct circular_queue *cq)
867{
868 return ((cq->rear + 1) & CQ_MASK) == cq->front;
869}
870
871static inline int __cq_enqueue(struct circular_queue *cq, unsigned long elem)
872{
873 if (__cq_full(cq))
874 return -1;
875
876 cq->element[cq->rear] = elem;
877 cq->rear = (cq->rear + 1) & CQ_MASK;
878 return 0;
879}
880
881static inline int __cq_dequeue(struct circular_queue *cq, unsigned long *elem)
882{
883 if (__cq_empty(cq))
884 return -1;
885
886 *elem = cq->element[cq->front];
887 cq->front = (cq->front + 1) & CQ_MASK;
888 return 0;
889}
890
891static inline unsigned int __cq_get_elem_count(struct circular_queue *cq)
892{
893 return (cq->rear - cq->front) & CQ_MASK;
894}
895
896static inline void mark_lock_accessed(struct lock_list *lock,
897 struct lock_list *parent)
898{
899 unsigned long nr;
900 nr = lock - list_entries;
901 WARN_ON(nr >= nr_list_entries);
902 lock->parent = parent;
903 set_bit(nr, bfs_accessed);
904}
905
906static inline unsigned long lock_accessed(struct lock_list *lock)
907{
908 unsigned long nr;
909 nr = lock - list_entries;
910 WARN_ON(nr >= nr_list_entries);
911 return test_bit(nr, bfs_accessed);
912}
913
914static inline struct lock_list *get_lock_parent(struct lock_list *child)
915{
916 return child->parent;
917}
918
919static inline int get_lock_depth(struct lock_list *child)
920{
921 int depth = 0;
922 struct lock_list *parent;
923
924 while ((parent = get_lock_parent(child))) {
925 child = parent;
926 depth++;
927 }
928 return depth;
929}
930
Ming Lei9e2d5512009-07-16 15:44:29 +0200931static int __bfs(struct lock_list *source_entry,
Peter Zijlstraaf012962009-07-16 15:44:29 +0200932 void *data,
933 int (*match)(struct lock_list *entry, void *data),
934 struct lock_list **target_entry,
935 int forward)
Ming Leic94aa5c2009-07-16 15:44:29 +0200936{
937 struct lock_list *entry;
Ming Leid588e462009-07-16 15:44:29 +0200938 struct list_head *head;
Ming Leic94aa5c2009-07-16 15:44:29 +0200939 struct circular_queue *cq = &lock_cq;
940 int ret = 1;
941
Ming Lei9e2d5512009-07-16 15:44:29 +0200942 if (match(source_entry, data)) {
Ming Leic94aa5c2009-07-16 15:44:29 +0200943 *target_entry = source_entry;
944 ret = 0;
945 goto exit;
946 }
947
Ming Leid588e462009-07-16 15:44:29 +0200948 if (forward)
949 head = &source_entry->class->locks_after;
950 else
951 head = &source_entry->class->locks_before;
952
953 if (list_empty(head))
954 goto exit;
955
956 __cq_init(cq);
Ming Leic94aa5c2009-07-16 15:44:29 +0200957 __cq_enqueue(cq, (unsigned long)source_entry);
958
959 while (!__cq_empty(cq)) {
960 struct lock_list *lock;
Ming Leic94aa5c2009-07-16 15:44:29 +0200961
962 __cq_dequeue(cq, (unsigned long *)&lock);
963
964 if (!lock->class) {
965 ret = -2;
966 goto exit;
967 }
968
969 if (forward)
970 head = &lock->class->locks_after;
971 else
972 head = &lock->class->locks_before;
973
974 list_for_each_entry(entry, head, entry) {
975 if (!lock_accessed(entry)) {
Ming Lei12f3dfd2009-07-16 15:44:29 +0200976 unsigned int cq_depth;
Ming Leic94aa5c2009-07-16 15:44:29 +0200977 mark_lock_accessed(entry, lock);
Ming Lei9e2d5512009-07-16 15:44:29 +0200978 if (match(entry, data)) {
Ming Leic94aa5c2009-07-16 15:44:29 +0200979 *target_entry = entry;
980 ret = 0;
981 goto exit;
982 }
983
984 if (__cq_enqueue(cq, (unsigned long)entry)) {
985 ret = -1;
986 goto exit;
987 }
Ming Lei12f3dfd2009-07-16 15:44:29 +0200988 cq_depth = __cq_get_elem_count(cq);
989 if (max_bfs_queue_depth < cq_depth)
990 max_bfs_queue_depth = cq_depth;
Ming Leic94aa5c2009-07-16 15:44:29 +0200991 }
992 }
993 }
994exit:
995 return ret;
996}
997
Ming Leid7aaba12009-07-16 15:44:29 +0200998static inline int __bfs_forwards(struct lock_list *src_entry,
Ming Lei9e2d5512009-07-16 15:44:29 +0200999 void *data,
1000 int (*match)(struct lock_list *entry, void *data),
1001 struct lock_list **target_entry)
Ming Leic94aa5c2009-07-16 15:44:29 +02001002{
Ming Lei9e2d5512009-07-16 15:44:29 +02001003 return __bfs(src_entry, data, match, target_entry, 1);
Ming Leic94aa5c2009-07-16 15:44:29 +02001004
1005}
1006
Ming Leid7aaba12009-07-16 15:44:29 +02001007static inline int __bfs_backwards(struct lock_list *src_entry,
Ming Lei9e2d5512009-07-16 15:44:29 +02001008 void *data,
1009 int (*match)(struct lock_list *entry, void *data),
1010 struct lock_list **target_entry)
Ming Leic94aa5c2009-07-16 15:44:29 +02001011{
Ming Lei9e2d5512009-07-16 15:44:29 +02001012 return __bfs(src_entry, data, match, target_entry, 0);
Ming Leic94aa5c2009-07-16 15:44:29 +02001013
1014}
1015
Peter Zijlstra8e182572007-07-19 01:48:54 -07001016/*
1017 * Recursive, forwards-direction lock-dependency checking, used for
1018 * both noncyclic checking and for hardirq-unsafe/softirq-unsafe
1019 * checking.
Peter Zijlstra8e182572007-07-19 01:48:54 -07001020 */
Peter Zijlstra8e182572007-07-19 01:48:54 -07001021
1022/*
1023 * Print a dependency chain entry (this is only done when a deadlock
1024 * has been detected):
1025 */
1026static noinline int
Ming Lei24208ca2009-07-16 15:44:29 +02001027print_circular_bug_entry(struct lock_list *target, int depth)
Peter Zijlstra8e182572007-07-19 01:48:54 -07001028{
1029 if (debug_locks_silent)
1030 return 0;
1031 printk("\n-> #%u", depth);
1032 print_lock_name(target->class);
1033 printk(":\n");
1034 print_stack_trace(&target->trace, 6);
1035
1036 return 0;
1037}
1038
1039/*
1040 * When a circular dependency is detected, print the
1041 * header first:
1042 */
1043static noinline int
Ming Leidb0002a2009-07-16 15:44:29 +02001044print_circular_bug_header(struct lock_list *entry, unsigned int depth,
1045 struct held_lock *check_src,
1046 struct held_lock *check_tgt)
Peter Zijlstra8e182572007-07-19 01:48:54 -07001047{
1048 struct task_struct *curr = current;
1049
Ming Leic94aa5c2009-07-16 15:44:29 +02001050 if (debug_locks_silent)
Peter Zijlstra8e182572007-07-19 01:48:54 -07001051 return 0;
1052
1053 printk("\n=======================================================\n");
1054 printk( "[ INFO: possible circular locking dependency detected ]\n");
1055 print_kernel_version();
1056 printk( "-------------------------------------------------------\n");
1057 printk("%s/%d is trying to acquire lock:\n",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07001058 curr->comm, task_pid_nr(curr));
Ming Leidb0002a2009-07-16 15:44:29 +02001059 print_lock(check_src);
Peter Zijlstra8e182572007-07-19 01:48:54 -07001060 printk("\nbut task is already holding lock:\n");
Ming Leidb0002a2009-07-16 15:44:29 +02001061 print_lock(check_tgt);
Peter Zijlstra8e182572007-07-19 01:48:54 -07001062 printk("\nwhich lock already depends on the new lock.\n\n");
1063 printk("\nthe existing dependency chain (in reverse order) is:\n");
1064
1065 print_circular_bug_entry(entry, depth);
1066
1067 return 0;
1068}
1069
Ming Lei9e2d5512009-07-16 15:44:29 +02001070static inline int class_equal(struct lock_list *entry, void *data)
1071{
1072 return entry->class == data;
1073}
1074
Ming Leidb0002a2009-07-16 15:44:29 +02001075static noinline int print_circular_bug(struct lock_list *this,
1076 struct lock_list *target,
1077 struct held_lock *check_src,
1078 struct held_lock *check_tgt)
Peter Zijlstra8e182572007-07-19 01:48:54 -07001079{
1080 struct task_struct *curr = current;
Ming Leic94aa5c2009-07-16 15:44:29 +02001081 struct lock_list *parent;
Ming Lei24208ca2009-07-16 15:44:29 +02001082 int depth;
Peter Zijlstra8e182572007-07-19 01:48:54 -07001083
Ming Leic94aa5c2009-07-16 15:44:29 +02001084 if (!debug_locks_off_graph_unlock() || debug_locks_silent)
Peter Zijlstra8e182572007-07-19 01:48:54 -07001085 return 0;
1086
Ming Leidb0002a2009-07-16 15:44:29 +02001087 if (!save_trace(&this->trace))
Peter Zijlstra8e182572007-07-19 01:48:54 -07001088 return 0;
1089
Ming Leic94aa5c2009-07-16 15:44:29 +02001090 depth = get_lock_depth(target);
1091
Ming Leidb0002a2009-07-16 15:44:29 +02001092 print_circular_bug_header(target, depth, check_src, check_tgt);
Ming Leic94aa5c2009-07-16 15:44:29 +02001093
1094 parent = get_lock_parent(target);
1095
1096 while (parent) {
1097 print_circular_bug_entry(parent, --depth);
1098 parent = get_lock_parent(parent);
1099 }
Peter Zijlstra8e182572007-07-19 01:48:54 -07001100
1101 printk("\nother info that might help us debug this:\n\n");
1102 lockdep_print_held_locks(curr);
1103
1104 printk("\nstack backtrace:\n");
1105 dump_stack();
1106
1107 return 0;
1108}
1109
Ming Leidb0002a2009-07-16 15:44:29 +02001110static noinline int print_bfs_bug(int ret)
1111{
1112 if (!debug_locks_off_graph_unlock())
1113 return 0;
1114
1115 WARN(1, "lockdep bfs error:%d\n", ret);
1116
1117 return 0;
1118}
1119
Ming Leief681022009-07-16 15:44:29 +02001120static int noop_count(struct lock_list *entry, void *data)
David Miller419ca3f2008-07-29 21:45:03 -07001121{
Ming Leief681022009-07-16 15:44:29 +02001122 (*(unsigned long *)data)++;
1123 return 0;
David Miller419ca3f2008-07-29 21:45:03 -07001124}
1125
Ming Leief681022009-07-16 15:44:29 +02001126unsigned long __lockdep_count_forward_deps(struct lock_list *this)
1127{
1128 unsigned long count = 0;
1129 struct lock_list *uninitialized_var(target_entry);
1130
1131 __bfs_forwards(this, (void *)&count, noop_count, &target_entry);
1132
1133 return count;
1134}
David Miller419ca3f2008-07-29 21:45:03 -07001135unsigned long lockdep_count_forward_deps(struct lock_class *class)
1136{
1137 unsigned long ret, flags;
Ming Leief681022009-07-16 15:44:29 +02001138 struct lock_list this;
1139
1140 this.parent = NULL;
1141 this.class = class;
David Miller419ca3f2008-07-29 21:45:03 -07001142
1143 local_irq_save(flags);
1144 __raw_spin_lock(&lockdep_lock);
Ming Leief681022009-07-16 15:44:29 +02001145 ret = __lockdep_count_forward_deps(&this);
David Miller419ca3f2008-07-29 21:45:03 -07001146 __raw_spin_unlock(&lockdep_lock);
1147 local_irq_restore(flags);
1148
1149 return ret;
1150}
1151
Ming Leief681022009-07-16 15:44:29 +02001152unsigned long __lockdep_count_backward_deps(struct lock_list *this)
David Miller419ca3f2008-07-29 21:45:03 -07001153{
Ming Leief681022009-07-16 15:44:29 +02001154 unsigned long count = 0;
1155 struct lock_list *uninitialized_var(target_entry);
David Miller419ca3f2008-07-29 21:45:03 -07001156
Ming Leief681022009-07-16 15:44:29 +02001157 __bfs_backwards(this, (void *)&count, noop_count, &target_entry);
David Miller419ca3f2008-07-29 21:45:03 -07001158
Ming Leief681022009-07-16 15:44:29 +02001159 return count;
David Miller419ca3f2008-07-29 21:45:03 -07001160}
1161
1162unsigned long lockdep_count_backward_deps(struct lock_class *class)
1163{
1164 unsigned long ret, flags;
Ming Leief681022009-07-16 15:44:29 +02001165 struct lock_list this;
1166
1167 this.parent = NULL;
1168 this.class = class;
David Miller419ca3f2008-07-29 21:45:03 -07001169
1170 local_irq_save(flags);
1171 __raw_spin_lock(&lockdep_lock);
Ming Leief681022009-07-16 15:44:29 +02001172 ret = __lockdep_count_backward_deps(&this);
David Miller419ca3f2008-07-29 21:45:03 -07001173 __raw_spin_unlock(&lockdep_lock);
1174 local_irq_restore(flags);
1175
1176 return ret;
1177}
1178
Peter Zijlstra8e182572007-07-19 01:48:54 -07001179/*
1180 * Prove that the dependency graph starting at <entry> can not
1181 * lead to <target>. Print an error and return 0 if it does.
1182 */
1183static noinline int
Ming Leidb0002a2009-07-16 15:44:29 +02001184check_noncircular(struct lock_list *root, struct lock_class *target,
1185 struct lock_list **target_entry)
Peter Zijlstra8e182572007-07-19 01:48:54 -07001186{
Ming Leidb0002a2009-07-16 15:44:29 +02001187 int result;
Peter Zijlstra8e182572007-07-19 01:48:54 -07001188
Ming Leidb0002a2009-07-16 15:44:29 +02001189 debug_atomic_inc(&nr_cyclic_checks);
David Miller419ca3f2008-07-29 21:45:03 -07001190
Ming Leid7aaba12009-07-16 15:44:29 +02001191 result = __bfs_forwards(root, target, class_equal, target_entry);
Ming Leidb0002a2009-07-16 15:44:29 +02001192
1193 return result;
Peter Zijlstra8e182572007-07-19 01:48:54 -07001194}
1195
Steven Rostedt81d68a92008-05-12 21:20:42 +02001196#if defined(CONFIG_TRACE_IRQFLAGS) && defined(CONFIG_PROVE_LOCKING)
Peter Zijlstra8e182572007-07-19 01:48:54 -07001197/*
1198 * Forwards and backwards subgraph searching, for the purposes of
1199 * proving that two subgraphs can be connected by a new dependency
1200 * without creating any illegal irq-safe -> irq-unsafe lock dependency.
1201 */
Peter Zijlstra8e182572007-07-19 01:48:54 -07001202
Ming Leid7aaba12009-07-16 15:44:29 +02001203static inline int usage_match(struct lock_list *entry, void *bit)
1204{
1205 return entry->class->usage_mask & (1 << (enum lock_usage_bit)bit);
1206}
1207
1208
1209
Peter Zijlstra8e182572007-07-19 01:48:54 -07001210/*
1211 * Find a node in the forwards-direction dependency sub-graph starting
Ming Leid7aaba12009-07-16 15:44:29 +02001212 * at @root->class that matches @bit.
Peter Zijlstra8e182572007-07-19 01:48:54 -07001213 *
Ming Leid7aaba12009-07-16 15:44:29 +02001214 * Return 0 if such a node exists in the subgraph, and put that node
1215 * into *@target_entry.
Peter Zijlstra8e182572007-07-19 01:48:54 -07001216 *
Ming Leid7aaba12009-07-16 15:44:29 +02001217 * Return 1 otherwise and keep *@target_entry unchanged.
1218 * Return <0 on error.
Peter Zijlstra8e182572007-07-19 01:48:54 -07001219 */
Ming Leid7aaba12009-07-16 15:44:29 +02001220static int
1221find_usage_forwards(struct lock_list *root, enum lock_usage_bit bit,
1222 struct lock_list **target_entry)
Peter Zijlstra8e182572007-07-19 01:48:54 -07001223{
Ming Leid7aaba12009-07-16 15:44:29 +02001224 int result;
Peter Zijlstra8e182572007-07-19 01:48:54 -07001225
1226 debug_atomic_inc(&nr_find_usage_forwards_checks);
Peter Zijlstra8e182572007-07-19 01:48:54 -07001227
Ming Leid7aaba12009-07-16 15:44:29 +02001228 result = __bfs_forwards(root, (void *)bit, usage_match, target_entry);
1229
1230 return result;
Peter Zijlstra8e182572007-07-19 01:48:54 -07001231}
1232
1233/*
1234 * Find a node in the backwards-direction dependency sub-graph starting
Ming Leid7aaba12009-07-16 15:44:29 +02001235 * at @root->class that matches @bit.
Peter Zijlstra8e182572007-07-19 01:48:54 -07001236 *
Ming Leid7aaba12009-07-16 15:44:29 +02001237 * Return 0 if such a node exists in the subgraph, and put that node
1238 * into *@target_entry.
Peter Zijlstra8e182572007-07-19 01:48:54 -07001239 *
Ming Leid7aaba12009-07-16 15:44:29 +02001240 * Return 1 otherwise and keep *@target_entry unchanged.
1241 * Return <0 on error.
Peter Zijlstra8e182572007-07-19 01:48:54 -07001242 */
Ming Leid7aaba12009-07-16 15:44:29 +02001243static int
1244find_usage_backwards(struct lock_list *root, enum lock_usage_bit bit,
1245 struct lock_list **target_entry)
Peter Zijlstra8e182572007-07-19 01:48:54 -07001246{
Ming Leid7aaba12009-07-16 15:44:29 +02001247 int result;
Peter Zijlstra8e182572007-07-19 01:48:54 -07001248
1249 debug_atomic_inc(&nr_find_usage_backwards_checks);
Peter Zijlstra8e182572007-07-19 01:48:54 -07001250
Ming Leid7aaba12009-07-16 15:44:29 +02001251 result = __bfs_backwards(root, (void *)bit, usage_match, target_entry);
Dave Jonesf82b2172008-08-11 09:30:23 +02001252
Ming Leid7aaba12009-07-16 15:44:29 +02001253 return result;
Peter Zijlstra8e182572007-07-19 01:48:54 -07001254}
1255
Peter Zijlstraaf012962009-07-16 15:44:29 +02001256static void print_lock_class_header(struct lock_class *class, int depth)
1257{
1258 int bit;
1259
1260 printk("%*s->", depth, "");
1261 print_lock_name(class);
1262 printk(" ops: %lu", class->ops);
1263 printk(" {\n");
1264
1265 for (bit = 0; bit < LOCK_USAGE_STATES; bit++) {
1266 if (class->usage_mask & (1 << bit)) {
1267 int len = depth;
1268
1269 len += printk("%*s %s", depth, "", usage_str[bit]);
1270 len += printk(" at:\n");
1271 print_stack_trace(class->usage_traces + bit, len);
1272 }
1273 }
1274 printk("%*s }\n", depth, "");
1275
1276 printk("%*s ... key at: ",depth,"");
1277 print_ip_sym((unsigned long)class->key);
1278}
1279
1280/*
1281 * printk the shortest lock dependencies from @start to @end in reverse order:
1282 */
1283static void __used
1284print_shortest_lock_dependencies(struct lock_list *leaf,
1285 struct lock_list *root)
1286{
1287 struct lock_list *entry = leaf;
1288 int depth;
1289
1290 /*compute depth from generated tree by BFS*/
1291 depth = get_lock_depth(leaf);
1292
1293 do {
1294 print_lock_class_header(entry->class, depth);
1295 printk("%*s ... acquired at:\n", depth, "");
1296 print_stack_trace(&entry->trace, 2);
1297 printk("\n");
1298
1299 if (depth == 0 && (entry != root)) {
1300 printk("lockdep:%s bad BFS generated tree\n", __func__);
1301 break;
1302 }
1303
1304 entry = get_lock_parent(entry);
1305 depth--;
1306 } while (entry && (depth >= 0));
1307
1308 return;
1309}
Ming Leid7aaba12009-07-16 15:44:29 +02001310
Peter Zijlstra8e182572007-07-19 01:48:54 -07001311static int
1312print_bad_irq_dependency(struct task_struct *curr,
Ming Lei24208ca2009-07-16 15:44:29 +02001313 struct lock_list *prev_root,
1314 struct lock_list *next_root,
1315 struct lock_list *backwards_entry,
1316 struct lock_list *forwards_entry,
Peter Zijlstra8e182572007-07-19 01:48:54 -07001317 struct held_lock *prev,
1318 struct held_lock *next,
1319 enum lock_usage_bit bit1,
1320 enum lock_usage_bit bit2,
1321 const char *irqclass)
1322{
1323 if (!debug_locks_off_graph_unlock() || debug_locks_silent)
1324 return 0;
1325
1326 printk("\n======================================================\n");
1327 printk( "[ INFO: %s-safe -> %s-unsafe lock order detected ]\n",
1328 irqclass, irqclass);
1329 print_kernel_version();
1330 printk( "------------------------------------------------------\n");
1331 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 -07001332 curr->comm, task_pid_nr(curr),
Peter Zijlstra8e182572007-07-19 01:48:54 -07001333 curr->hardirq_context, hardirq_count() >> HARDIRQ_SHIFT,
1334 curr->softirq_context, softirq_count() >> SOFTIRQ_SHIFT,
1335 curr->hardirqs_enabled,
1336 curr->softirqs_enabled);
1337 print_lock(next);
1338
1339 printk("\nand this task is already holding:\n");
1340 print_lock(prev);
1341 printk("which would create a new lock dependency:\n");
Dave Jonesf82b2172008-08-11 09:30:23 +02001342 print_lock_name(hlock_class(prev));
Peter Zijlstra8e182572007-07-19 01:48:54 -07001343 printk(" ->");
Dave Jonesf82b2172008-08-11 09:30:23 +02001344 print_lock_name(hlock_class(next));
Peter Zijlstra8e182572007-07-19 01:48:54 -07001345 printk("\n");
1346
1347 printk("\nbut this new dependency connects a %s-irq-safe lock:\n",
1348 irqclass);
Ming Lei24208ca2009-07-16 15:44:29 +02001349 print_lock_name(backwards_entry->class);
Peter Zijlstra8e182572007-07-19 01:48:54 -07001350 printk("\n... which became %s-irq-safe at:\n", irqclass);
1351
Ming Lei24208ca2009-07-16 15:44:29 +02001352 print_stack_trace(backwards_entry->class->usage_traces + bit1, 1);
Peter Zijlstra8e182572007-07-19 01:48:54 -07001353
1354 printk("\nto a %s-irq-unsafe lock:\n", irqclass);
Ming Lei24208ca2009-07-16 15:44:29 +02001355 print_lock_name(forwards_entry->class);
Peter Zijlstra8e182572007-07-19 01:48:54 -07001356 printk("\n... which became %s-irq-unsafe at:\n", irqclass);
1357 printk("...");
1358
Ming Lei24208ca2009-07-16 15:44:29 +02001359 print_stack_trace(forwards_entry->class->usage_traces + bit2, 1);
Peter Zijlstra8e182572007-07-19 01:48:54 -07001360
1361 printk("\nother info that might help us debug this:\n\n");
1362 lockdep_print_held_locks(curr);
1363
Ming Lei24208ca2009-07-16 15:44:29 +02001364 printk("\nthe dependencies between %s-irq-safe lock", irqclass);
1365 printk(" and the holding lock:\n");
1366 if (!save_trace(&prev_root->trace))
1367 return 0;
1368 print_shortest_lock_dependencies(backwards_entry, prev_root);
Peter Zijlstra8e182572007-07-19 01:48:54 -07001369
Ming Lei24208ca2009-07-16 15:44:29 +02001370 printk("\nthe dependencies between the lock to be acquired");
1371 printk(" and %s-irq-unsafe lock:\n", irqclass);
1372 if (!save_trace(&next_root->trace))
1373 return 0;
1374 print_shortest_lock_dependencies(forwards_entry, next_root);
Peter Zijlstra8e182572007-07-19 01:48:54 -07001375
1376 printk("\nstack backtrace:\n");
1377 dump_stack();
1378
1379 return 0;
1380}
1381
1382static int
1383check_usage(struct task_struct *curr, struct held_lock *prev,
1384 struct held_lock *next, enum lock_usage_bit bit_backwards,
1385 enum lock_usage_bit bit_forwards, const char *irqclass)
1386{
1387 int ret;
Ming Lei24208ca2009-07-16 15:44:29 +02001388 struct lock_list this, that;
Ming Leid7aaba12009-07-16 15:44:29 +02001389 struct lock_list *uninitialized_var(target_entry);
Ming Lei24208ca2009-07-16 15:44:29 +02001390 struct lock_list *uninitialized_var(target_entry1);
Peter Zijlstra8e182572007-07-19 01:48:54 -07001391
Ming Leid7aaba12009-07-16 15:44:29 +02001392 this.parent = NULL;
Peter Zijlstra8e182572007-07-19 01:48:54 -07001393
Ming Leid7aaba12009-07-16 15:44:29 +02001394 this.class = hlock_class(prev);
1395 ret = find_usage_backwards(&this, bit_backwards, &target_entry);
Peter Zijlstraaf012962009-07-16 15:44:29 +02001396 if (ret < 0)
1397 return print_bfs_bug(ret);
1398 if (ret == 1)
1399 return ret;
Ming Leid7aaba12009-07-16 15:44:29 +02001400
Ming Lei24208ca2009-07-16 15:44:29 +02001401 that.parent = NULL;
1402 that.class = hlock_class(next);
1403 ret = find_usage_forwards(&that, bit_forwards, &target_entry1);
Peter Zijlstraaf012962009-07-16 15:44:29 +02001404 if (ret < 0)
1405 return print_bfs_bug(ret);
1406 if (ret == 1)
1407 return ret;
Ming Leid7aaba12009-07-16 15:44:29 +02001408
Ming Lei24208ca2009-07-16 15:44:29 +02001409 return print_bad_irq_dependency(curr, &this, &that,
1410 target_entry, target_entry1,
1411 prev, next,
Peter Zijlstra8e182572007-07-19 01:48:54 -07001412 bit_backwards, bit_forwards, irqclass);
1413}
1414
Peter Zijlstra4f367d8a2009-01-22 18:10:42 +01001415static const char *state_names[] = {
1416#define LOCKDEP_STATE(__STATE) \
Peter Zijlstrab4b136f2009-01-29 14:50:36 +01001417 __stringify(__STATE),
Peter Zijlstra4f367d8a2009-01-22 18:10:42 +01001418#include "lockdep_states.h"
1419#undef LOCKDEP_STATE
1420};
1421
1422static const char *state_rnames[] = {
1423#define LOCKDEP_STATE(__STATE) \
Peter Zijlstrab4b136f2009-01-29 14:50:36 +01001424 __stringify(__STATE)"-READ",
Peter Zijlstra4f367d8a2009-01-22 18:10:42 +01001425#include "lockdep_states.h"
1426#undef LOCKDEP_STATE
1427};
1428
1429static inline const char *state_name(enum lock_usage_bit bit)
1430{
1431 return (bit & 1) ? state_rnames[bit >> 2] : state_names[bit >> 2];
1432}
1433
1434static int exclusive_bit(int new_bit)
1435{
1436 /*
1437 * USED_IN
1438 * USED_IN_READ
1439 * ENABLED
1440 * ENABLED_READ
1441 *
1442 * bit 0 - write/read
1443 * bit 1 - used_in/enabled
1444 * bit 2+ state
1445 */
1446
1447 int state = new_bit & ~3;
1448 int dir = new_bit & 2;
1449
1450 /*
1451 * keep state, bit flip the direction and strip read.
1452 */
1453 return state | (dir ^ 2);
1454}
1455
1456static int check_irq_usage(struct task_struct *curr, struct held_lock *prev,
1457 struct held_lock *next, enum lock_usage_bit bit)
Peter Zijlstra8e182572007-07-19 01:48:54 -07001458{
1459 /*
1460 * Prove that the new dependency does not connect a hardirq-safe
1461 * lock with a hardirq-unsafe lock - to achieve this we search
1462 * the backwards-subgraph starting at <prev>, and the
1463 * forwards-subgraph starting at <next>:
1464 */
Peter Zijlstra4f367d8a2009-01-22 18:10:42 +01001465 if (!check_usage(curr, prev, next, bit,
1466 exclusive_bit(bit), state_name(bit)))
Peter Zijlstra8e182572007-07-19 01:48:54 -07001467 return 0;
1468
Peter Zijlstra4f367d8a2009-01-22 18:10:42 +01001469 bit++; /* _READ */
1470
Peter Zijlstra8e182572007-07-19 01:48:54 -07001471 /*
1472 * Prove that the new dependency does not connect a hardirq-safe-read
1473 * lock with a hardirq-unsafe lock - to achieve this we search
1474 * the backwards-subgraph starting at <prev>, and the
1475 * forwards-subgraph starting at <next>:
1476 */
Peter Zijlstra4f367d8a2009-01-22 18:10:42 +01001477 if (!check_usage(curr, prev, next, bit,
1478 exclusive_bit(bit), state_name(bit)))
Peter Zijlstra8e182572007-07-19 01:48:54 -07001479 return 0;
1480
Peter Zijlstra4f367d8a2009-01-22 18:10:42 +01001481 return 1;
1482}
Peter Zijlstra8e182572007-07-19 01:48:54 -07001483
Peter Zijlstra4f367d8a2009-01-22 18:10:42 +01001484static int
1485check_prev_add_irq(struct task_struct *curr, struct held_lock *prev,
1486 struct held_lock *next)
1487{
1488#define LOCKDEP_STATE(__STATE) \
1489 if (!check_irq_usage(curr, prev, next, LOCK_USED_IN_##__STATE)) \
Nick Piggincf40bd12009-01-21 08:12:39 +01001490 return 0;
Peter Zijlstra4f367d8a2009-01-22 18:10:42 +01001491#include "lockdep_states.h"
1492#undef LOCKDEP_STATE
Nick Piggincf40bd12009-01-21 08:12:39 +01001493
Peter Zijlstra8e182572007-07-19 01:48:54 -07001494 return 1;
1495}
1496
1497static void inc_chains(void)
1498{
1499 if (current->hardirq_context)
1500 nr_hardirq_chains++;
1501 else {
1502 if (current->softirq_context)
1503 nr_softirq_chains++;
1504 else
1505 nr_process_chains++;
1506 }
1507}
1508
1509#else
1510
1511static inline int
1512check_prev_add_irq(struct task_struct *curr, struct held_lock *prev,
1513 struct held_lock *next)
1514{
1515 return 1;
1516}
1517
1518static inline void inc_chains(void)
1519{
1520 nr_process_chains++;
1521}
1522
1523#endif
1524
1525static int
1526print_deadlock_bug(struct task_struct *curr, struct held_lock *prev,
1527 struct held_lock *next)
1528{
1529 if (!debug_locks_off_graph_unlock() || debug_locks_silent)
1530 return 0;
1531
1532 printk("\n=============================================\n");
1533 printk( "[ INFO: possible recursive locking detected ]\n");
1534 print_kernel_version();
1535 printk( "---------------------------------------------\n");
1536 printk("%s/%d is trying to acquire lock:\n",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07001537 curr->comm, task_pid_nr(curr));
Peter Zijlstra8e182572007-07-19 01:48:54 -07001538 print_lock(next);
1539 printk("\nbut task is already holding lock:\n");
1540 print_lock(prev);
1541
1542 printk("\nother info that might help us debug this:\n");
1543 lockdep_print_held_locks(curr);
1544
1545 printk("\nstack backtrace:\n");
1546 dump_stack();
1547
1548 return 0;
1549}
1550
1551/*
1552 * Check whether we are holding such a class already.
1553 *
1554 * (Note that this has to be done separately, because the graph cannot
1555 * detect such classes of deadlocks.)
1556 *
1557 * Returns: 0 on deadlock detected, 1 on OK, 2 on recursive read
1558 */
1559static int
1560check_deadlock(struct task_struct *curr, struct held_lock *next,
1561 struct lockdep_map *next_instance, int read)
1562{
1563 struct held_lock *prev;
Peter Zijlstra7531e2f2008-08-11 09:30:24 +02001564 struct held_lock *nest = NULL;
Peter Zijlstra8e182572007-07-19 01:48:54 -07001565 int i;
1566
1567 for (i = 0; i < curr->lockdep_depth; i++) {
1568 prev = curr->held_locks + i;
Peter Zijlstra7531e2f2008-08-11 09:30:24 +02001569
1570 if (prev->instance == next->nest_lock)
1571 nest = prev;
1572
Dave Jonesf82b2172008-08-11 09:30:23 +02001573 if (hlock_class(prev) != hlock_class(next))
Peter Zijlstra8e182572007-07-19 01:48:54 -07001574 continue;
Peter Zijlstra7531e2f2008-08-11 09:30:24 +02001575
Peter Zijlstra8e182572007-07-19 01:48:54 -07001576 /*
1577 * Allow read-after-read recursion of the same
1578 * lock class (i.e. read_lock(lock)+read_lock(lock)):
1579 */
1580 if ((read == 2) && prev->read)
1581 return 2;
Peter Zijlstra7531e2f2008-08-11 09:30:24 +02001582
1583 /*
1584 * We're holding the nest_lock, which serializes this lock's
1585 * nesting behaviour.
1586 */
1587 if (nest)
1588 return 2;
1589
Peter Zijlstra8e182572007-07-19 01:48:54 -07001590 return print_deadlock_bug(curr, prev, next);
1591 }
1592 return 1;
1593}
1594
1595/*
1596 * There was a chain-cache miss, and we are about to add a new dependency
1597 * to a previous lock. We recursively validate the following rules:
1598 *
1599 * - would the adding of the <prev> -> <next> dependency create a
1600 * circular dependency in the graph? [== circular deadlock]
1601 *
1602 * - does the new prev->next dependency connect any hardirq-safe lock
1603 * (in the full backwards-subgraph starting at <prev>) with any
1604 * hardirq-unsafe lock (in the full forwards-subgraph starting at
1605 * <next>)? [== illegal lock inversion with hardirq contexts]
1606 *
1607 * - does the new prev->next dependency connect any softirq-safe lock
1608 * (in the full backwards-subgraph starting at <prev>) with any
1609 * softirq-unsafe lock (in the full forwards-subgraph starting at
1610 * <next>)? [== illegal lock inversion with softirq contexts]
1611 *
1612 * any of these scenarios could lead to a deadlock.
1613 *
1614 * Then if all the validations pass, we add the forwards and backwards
1615 * dependency.
1616 */
1617static int
1618check_prev_add(struct task_struct *curr, struct held_lock *prev,
1619 struct held_lock *next, int distance)
1620{
1621 struct lock_list *entry;
1622 int ret;
Ming Leidb0002a2009-07-16 15:44:29 +02001623 struct lock_list this;
1624 struct lock_list *uninitialized_var(target_entry);
Peter Zijlstra8e182572007-07-19 01:48:54 -07001625
1626 /*
1627 * Prove that the new <prev> -> <next> dependency would not
1628 * create a circular dependency in the graph. (We do this by
1629 * forward-recursing into the graph starting at <next>, and
1630 * checking whether we can reach <prev>.)
1631 *
1632 * We are using global variables to control the recursion, to
1633 * keep the stackframe size of the recursive functions low:
1634 */
Ming Leidb0002a2009-07-16 15:44:29 +02001635 this.class = hlock_class(next);
1636 this.parent = NULL;
1637 ret = check_noncircular(&this, hlock_class(prev), &target_entry);
1638 if (unlikely(!ret))
1639 return print_circular_bug(&this, target_entry, next, prev);
1640 else if (unlikely(ret < 0))
1641 return print_bfs_bug(ret);
Ming Leic94aa5c2009-07-16 15:44:29 +02001642
Peter Zijlstra8e182572007-07-19 01:48:54 -07001643 if (!check_prev_add_irq(curr, prev, next))
1644 return 0;
1645
1646 /*
1647 * For recursive read-locks we do all the dependency checks,
1648 * but we dont store read-triggered dependencies (only
1649 * write-triggered dependencies). This ensures that only the
1650 * write-side dependencies matter, and that if for example a
1651 * write-lock never takes any other locks, then the reads are
1652 * equivalent to a NOP.
1653 */
1654 if (next->read == 2 || prev->read == 2)
1655 return 1;
1656 /*
1657 * Is the <prev> -> <next> dependency already present?
1658 *
1659 * (this may occur even though this is a new chain: consider
1660 * e.g. the L1 -> L2 -> L3 -> L4 and the L5 -> L1 -> L2 -> L3
1661 * chains - the second one will be new, but L1 already has
1662 * L2 added to its dependency list, due to the first chain.)
1663 */
Dave Jonesf82b2172008-08-11 09:30:23 +02001664 list_for_each_entry(entry, &hlock_class(prev)->locks_after, entry) {
1665 if (entry->class == hlock_class(next)) {
Peter Zijlstra8e182572007-07-19 01:48:54 -07001666 if (distance == 1)
1667 entry->distance = 1;
1668 return 2;
1669 }
1670 }
1671
1672 /*
1673 * Ok, all validations passed, add the new lock
1674 * to the previous lock's dependency list:
1675 */
Dave Jonesf82b2172008-08-11 09:30:23 +02001676 ret = add_lock_to_list(hlock_class(prev), hlock_class(next),
1677 &hlock_class(prev)->locks_after,
1678 next->acquire_ip, distance);
Peter Zijlstra8e182572007-07-19 01:48:54 -07001679
1680 if (!ret)
1681 return 0;
1682
Dave Jonesf82b2172008-08-11 09:30:23 +02001683 ret = add_lock_to_list(hlock_class(next), hlock_class(prev),
1684 &hlock_class(next)->locks_before,
1685 next->acquire_ip, distance);
Peter Zijlstra8e182572007-07-19 01:48:54 -07001686 if (!ret)
1687 return 0;
1688
1689 /*
1690 * Debugging printouts:
1691 */
Dave Jonesf82b2172008-08-11 09:30:23 +02001692 if (verbose(hlock_class(prev)) || verbose(hlock_class(next))) {
Peter Zijlstra8e182572007-07-19 01:48:54 -07001693 graph_unlock();
1694 printk("\n new dependency: ");
Dave Jonesf82b2172008-08-11 09:30:23 +02001695 print_lock_name(hlock_class(prev));
Peter Zijlstra8e182572007-07-19 01:48:54 -07001696 printk(" => ");
Dave Jonesf82b2172008-08-11 09:30:23 +02001697 print_lock_name(hlock_class(next));
Peter Zijlstra8e182572007-07-19 01:48:54 -07001698 printk("\n");
1699 dump_stack();
1700 return graph_lock();
1701 }
1702 return 1;
1703}
1704
1705/*
1706 * Add the dependency to all directly-previous locks that are 'relevant'.
1707 * The ones that are relevant are (in increasing distance from curr):
1708 * all consecutive trylock entries and the final non-trylock entry - or
1709 * the end of this context's lock-chain - whichever comes first.
1710 */
1711static int
1712check_prevs_add(struct task_struct *curr, struct held_lock *next)
1713{
1714 int depth = curr->lockdep_depth;
1715 struct held_lock *hlock;
1716
1717 /*
1718 * Debugging checks.
1719 *
1720 * Depth must not be zero for a non-head lock:
1721 */
1722 if (!depth)
1723 goto out_bug;
1724 /*
1725 * At least two relevant locks must exist for this
1726 * to be a head:
1727 */
1728 if (curr->held_locks[depth].irq_context !=
1729 curr->held_locks[depth-1].irq_context)
1730 goto out_bug;
1731
1732 for (;;) {
1733 int distance = curr->lockdep_depth - depth + 1;
1734 hlock = curr->held_locks + depth-1;
1735 /*
1736 * Only non-recursive-read entries get new dependencies
1737 * added:
1738 */
1739 if (hlock->read != 2) {
1740 if (!check_prev_add(curr, hlock, next, distance))
1741 return 0;
1742 /*
1743 * Stop after the first non-trylock entry,
1744 * as non-trylock entries have added their
1745 * own direct dependencies already, so this
1746 * lock is connected to them indirectly:
1747 */
1748 if (!hlock->trylock)
1749 break;
1750 }
1751 depth--;
1752 /*
1753 * End of lock-stack?
1754 */
1755 if (!depth)
1756 break;
1757 /*
1758 * Stop the search if we cross into another context:
1759 */
1760 if (curr->held_locks[depth].irq_context !=
1761 curr->held_locks[depth-1].irq_context)
1762 break;
1763 }
1764 return 1;
1765out_bug:
1766 if (!debug_locks_off_graph_unlock())
1767 return 0;
1768
1769 WARN_ON(1);
1770
1771 return 0;
1772}
1773
1774unsigned long nr_lock_chains;
Huang, Ying443cd502008-06-20 16:39:21 +08001775struct lock_chain lock_chains[MAX_LOCKDEP_CHAINS];
Huang, Yingcd1a28e2008-06-23 11:20:54 +08001776int nr_chain_hlocks;
Huang, Ying443cd502008-06-20 16:39:21 +08001777static u16 chain_hlocks[MAX_LOCKDEP_CHAIN_HLOCKS];
1778
1779struct lock_class *lock_chain_get_class(struct lock_chain *chain, int i)
1780{
1781 return lock_classes + chain_hlocks[chain->base + i];
1782}
Peter Zijlstra8e182572007-07-19 01:48:54 -07001783
1784/*
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001785 * Look up a dependency chain. If the key is not present yet then
Jarek Poplawski9e860d02007-05-08 00:30:12 -07001786 * add it and return 1 - in this case the new dependency chain is
1787 * validated. If the key is already hashed, return 0.
1788 * (On return with 1 graph_lock is held.)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001789 */
Huang, Ying443cd502008-06-20 16:39:21 +08001790static inline int lookup_chain_cache(struct task_struct *curr,
1791 struct held_lock *hlock,
1792 u64 chain_key)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001793{
Dave Jonesf82b2172008-08-11 09:30:23 +02001794 struct lock_class *class = hlock_class(hlock);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001795 struct list_head *hash_head = chainhashentry(chain_key);
1796 struct lock_chain *chain;
Huang, Ying443cd502008-06-20 16:39:21 +08001797 struct held_lock *hlock_curr, *hlock_next;
Huang, Yingcd1a28e2008-06-23 11:20:54 +08001798 int i, j, n, cn;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001799
Jarek Poplawski381a2292007-02-10 01:44:58 -08001800 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
1801 return 0;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001802 /*
1803 * We can walk it lock-free, because entries only get added
1804 * to the hash:
1805 */
1806 list_for_each_entry(chain, hash_head, entry) {
1807 if (chain->chain_key == chain_key) {
1808cache_hit:
1809 debug_atomic_inc(&chain_lookup_hits);
Ingo Molnar81fc6852006-12-13 00:34:40 -08001810 if (very_verbose(class))
Andrew Morton755cd902006-12-29 16:49:14 -08001811 printk("\nhash chain already cached, key: "
1812 "%016Lx tail class: [%p] %s\n",
1813 (unsigned long long)chain_key,
1814 class->key, class->name);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001815 return 0;
1816 }
1817 }
Ingo Molnar81fc6852006-12-13 00:34:40 -08001818 if (very_verbose(class))
Andrew Morton755cd902006-12-29 16:49:14 -08001819 printk("\nnew hash chain, key: %016Lx tail class: [%p] %s\n",
1820 (unsigned long long)chain_key, class->key, class->name);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001821 /*
1822 * Allocate a new chain entry from the static array, and add
1823 * it to the hash:
1824 */
Ingo Molnar74c383f2006-12-13 00:34:43 -08001825 if (!graph_lock())
1826 return 0;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001827 /*
1828 * We have to walk the chain again locked - to avoid duplicates:
1829 */
1830 list_for_each_entry(chain, hash_head, entry) {
1831 if (chain->chain_key == chain_key) {
Ingo Molnar74c383f2006-12-13 00:34:43 -08001832 graph_unlock();
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001833 goto cache_hit;
1834 }
1835 }
1836 if (unlikely(nr_lock_chains >= MAX_LOCKDEP_CHAINS)) {
Ingo Molnar74c383f2006-12-13 00:34:43 -08001837 if (!debug_locks_off_graph_unlock())
1838 return 0;
1839
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001840 printk("BUG: MAX_LOCKDEP_CHAINS too low!\n");
1841 printk("turning off the locking correctness validator.\n");
Peter Zijlstraeedeeab2009-03-18 12:38:47 +01001842 dump_stack();
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001843 return 0;
1844 }
1845 chain = lock_chains + nr_lock_chains++;
1846 chain->chain_key = chain_key;
Huang, Ying443cd502008-06-20 16:39:21 +08001847 chain->irq_context = hlock->irq_context;
1848 /* Find the first held_lock of current chain */
1849 hlock_next = hlock;
1850 for (i = curr->lockdep_depth - 1; i >= 0; i--) {
1851 hlock_curr = curr->held_locks + i;
1852 if (hlock_curr->irq_context != hlock_next->irq_context)
1853 break;
1854 hlock_next = hlock;
1855 }
1856 i++;
1857 chain->depth = curr->lockdep_depth + 1 - i;
Huang, Yingcd1a28e2008-06-23 11:20:54 +08001858 cn = nr_chain_hlocks;
1859 while (cn + chain->depth <= MAX_LOCKDEP_CHAIN_HLOCKS) {
1860 n = cmpxchg(&nr_chain_hlocks, cn, cn + chain->depth);
1861 if (n == cn)
1862 break;
1863 cn = n;
1864 }
1865 if (likely(cn + chain->depth <= MAX_LOCKDEP_CHAIN_HLOCKS)) {
1866 chain->base = cn;
Huang, Ying443cd502008-06-20 16:39:21 +08001867 for (j = 0; j < chain->depth - 1; j++, i++) {
Dave Jonesf82b2172008-08-11 09:30:23 +02001868 int lock_id = curr->held_locks[i].class_idx - 1;
Huang, Ying443cd502008-06-20 16:39:21 +08001869 chain_hlocks[chain->base + j] = lock_id;
1870 }
1871 chain_hlocks[chain->base + j] = class - lock_classes;
1872 }
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001873 list_add_tail_rcu(&chain->entry, hash_head);
1874 debug_atomic_inc(&chain_lookup_misses);
Peter Zijlstra8e182572007-07-19 01:48:54 -07001875 inc_chains();
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001876
1877 return 1;
1878}
Peter Zijlstra8e182572007-07-19 01:48:54 -07001879
1880static int validate_chain(struct task_struct *curr, struct lockdep_map *lock,
Johannes Berg4e6045f2007-10-18 23:39:55 -07001881 struct held_lock *hlock, int chain_head, u64 chain_key)
Peter Zijlstra8e182572007-07-19 01:48:54 -07001882{
1883 /*
1884 * Trylock needs to maintain the stack of held locks, but it
1885 * does not add new dependencies, because trylock can be done
1886 * in any order.
1887 *
1888 * We look up the chain_key and do the O(N^2) check and update of
1889 * the dependencies only if this is a new dependency chain.
1890 * (If lookup_chain_cache() returns with 1 it acquires
1891 * graph_lock for us)
1892 */
1893 if (!hlock->trylock && (hlock->check == 2) &&
Huang, Ying443cd502008-06-20 16:39:21 +08001894 lookup_chain_cache(curr, hlock, chain_key)) {
Peter Zijlstra8e182572007-07-19 01:48:54 -07001895 /*
1896 * Check whether last held lock:
1897 *
1898 * - is irq-safe, if this lock is irq-unsafe
1899 * - is softirq-safe, if this lock is hardirq-unsafe
1900 *
1901 * And check whether the new lock's dependency graph
1902 * could lead back to the previous lock.
1903 *
1904 * any of these scenarios could lead to a deadlock. If
1905 * All validations
1906 */
1907 int ret = check_deadlock(curr, hlock, lock, hlock->read);
1908
1909 if (!ret)
1910 return 0;
1911 /*
1912 * Mark recursive read, as we jump over it when
1913 * building dependencies (just like we jump over
1914 * trylock entries):
1915 */
1916 if (ret == 2)
1917 hlock->read = 2;
1918 /*
1919 * Add dependency only if this lock is not the head
1920 * of the chain, and if it's not a secondary read-lock:
1921 */
1922 if (!chain_head && ret != 2)
1923 if (!check_prevs_add(curr, hlock))
1924 return 0;
1925 graph_unlock();
1926 } else
1927 /* after lookup_chain_cache(): */
1928 if (unlikely(!debug_locks))
1929 return 0;
1930
1931 return 1;
1932}
1933#else
1934static inline int validate_chain(struct task_struct *curr,
1935 struct lockdep_map *lock, struct held_lock *hlock,
Gregory Haskins3aa416b2007-10-11 22:11:11 +02001936 int chain_head, u64 chain_key)
Peter Zijlstra8e182572007-07-19 01:48:54 -07001937{
1938 return 1;
1939}
Peter Zijlstraca58abc2007-07-19 01:48:53 -07001940#endif
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001941
1942/*
1943 * We are building curr_chain_key incrementally, so double-check
1944 * it from scratch, to make sure that it's done correctly:
1945 */
Steven Rostedt1d09daa2008-05-12 21:20:55 +02001946static void check_chain_key(struct task_struct *curr)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001947{
1948#ifdef CONFIG_DEBUG_LOCKDEP
1949 struct held_lock *hlock, *prev_hlock = NULL;
1950 unsigned int i, id;
1951 u64 chain_key = 0;
1952
1953 for (i = 0; i < curr->lockdep_depth; i++) {
1954 hlock = curr->held_locks + i;
1955 if (chain_key != hlock->prev_chain_key) {
1956 debug_locks_off();
Arjan van de Ven2df8b1d2008-07-30 12:43:11 -07001957 WARN(1, "hm#1, depth: %u [%u], %016Lx != %016Lx\n",
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001958 curr->lockdep_depth, i,
1959 (unsigned long long)chain_key,
1960 (unsigned long long)hlock->prev_chain_key);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001961 return;
1962 }
Dave Jonesf82b2172008-08-11 09:30:23 +02001963 id = hlock->class_idx - 1;
Jarek Poplawski381a2292007-02-10 01:44:58 -08001964 if (DEBUG_LOCKS_WARN_ON(id >= MAX_LOCKDEP_KEYS))
1965 return;
1966
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001967 if (prev_hlock && (prev_hlock->irq_context !=
1968 hlock->irq_context))
1969 chain_key = 0;
1970 chain_key = iterate_chain_key(chain_key, id);
1971 prev_hlock = hlock;
1972 }
1973 if (chain_key != curr->curr_chain_key) {
1974 debug_locks_off();
Arjan van de Ven2df8b1d2008-07-30 12:43:11 -07001975 WARN(1, "hm#2, depth: %u [%u], %016Lx != %016Lx\n",
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001976 curr->lockdep_depth, i,
1977 (unsigned long long)chain_key,
1978 (unsigned long long)curr->curr_chain_key);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001979 }
1980#endif
1981}
1982
Peter Zijlstra8e182572007-07-19 01:48:54 -07001983static int
1984print_usage_bug(struct task_struct *curr, struct held_lock *this,
1985 enum lock_usage_bit prev_bit, enum lock_usage_bit new_bit)
1986{
1987 if (!debug_locks_off_graph_unlock() || debug_locks_silent)
1988 return 0;
1989
1990 printk("\n=================================\n");
1991 printk( "[ INFO: inconsistent lock state ]\n");
1992 print_kernel_version();
1993 printk( "---------------------------------\n");
1994
1995 printk("inconsistent {%s} -> {%s} usage.\n",
1996 usage_str[prev_bit], usage_str[new_bit]);
1997
1998 printk("%s/%d [HC%u[%lu]:SC%u[%lu]:HE%u:SE%u] takes:\n",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07001999 curr->comm, task_pid_nr(curr),
Peter Zijlstra8e182572007-07-19 01:48:54 -07002000 trace_hardirq_context(curr), hardirq_count() >> HARDIRQ_SHIFT,
2001 trace_softirq_context(curr), softirq_count() >> SOFTIRQ_SHIFT,
2002 trace_hardirqs_enabled(curr),
2003 trace_softirqs_enabled(curr));
2004 print_lock(this);
2005
2006 printk("{%s} state was registered at:\n", usage_str[prev_bit]);
Dave Jonesf82b2172008-08-11 09:30:23 +02002007 print_stack_trace(hlock_class(this)->usage_traces + prev_bit, 1);
Peter Zijlstra8e182572007-07-19 01:48:54 -07002008
2009 print_irqtrace_events(curr);
2010 printk("\nother info that might help us debug this:\n");
2011 lockdep_print_held_locks(curr);
2012
2013 printk("\nstack backtrace:\n");
2014 dump_stack();
2015
2016 return 0;
2017}
2018
2019/*
2020 * Print out an error if an invalid bit is set:
2021 */
2022static inline int
2023valid_state(struct task_struct *curr, struct held_lock *this,
2024 enum lock_usage_bit new_bit, enum lock_usage_bit bad_bit)
2025{
Dave Jonesf82b2172008-08-11 09:30:23 +02002026 if (unlikely(hlock_class(this)->usage_mask & (1 << bad_bit)))
Peter Zijlstra8e182572007-07-19 01:48:54 -07002027 return print_usage_bug(curr, this, bad_bit, new_bit);
2028 return 1;
2029}
2030
2031static int mark_lock(struct task_struct *curr, struct held_lock *this,
2032 enum lock_usage_bit new_bit);
2033
Steven Rostedt81d68a92008-05-12 21:20:42 +02002034#if defined(CONFIG_TRACE_IRQFLAGS) && defined(CONFIG_PROVE_LOCKING)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002035
2036/*
2037 * print irq inversion bug:
2038 */
2039static int
Ming Lei24208ca2009-07-16 15:44:29 +02002040print_irq_inversion_bug(struct task_struct *curr,
2041 struct lock_list *root, struct lock_list *other,
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002042 struct held_lock *this, int forwards,
2043 const char *irqclass)
2044{
Ingo Molnar74c383f2006-12-13 00:34:43 -08002045 if (!debug_locks_off_graph_unlock() || debug_locks_silent)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002046 return 0;
2047
2048 printk("\n=========================================================\n");
2049 printk( "[ INFO: possible irq lock inversion dependency detected ]\n");
Dave Jones99de0552006-09-29 02:00:10 -07002050 print_kernel_version();
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002051 printk( "---------------------------------------------------------\n");
2052 printk("%s/%d just changed the state of lock:\n",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07002053 curr->comm, task_pid_nr(curr));
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002054 print_lock(this);
2055 if (forwards)
Peter Zijlstra26575e22009-03-04 14:53:24 +01002056 printk("but this lock took another, %s-unsafe lock in the past:\n", irqclass);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002057 else
Peter Zijlstra26575e22009-03-04 14:53:24 +01002058 printk("but this lock was taken by another, %s-safe lock in the past:\n", irqclass);
Ming Lei24208ca2009-07-16 15:44:29 +02002059 print_lock_name(other->class);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002060 printk("\n\nand interrupts could create inverse lock ordering between them.\n\n");
2061
2062 printk("\nother info that might help us debug this:\n");
2063 lockdep_print_held_locks(curr);
2064
Ming Lei24208ca2009-07-16 15:44:29 +02002065 printk("\nthe shortest dependencies between 2nd lock and 1st lock:\n");
2066 if (!save_trace(&root->trace))
2067 return 0;
2068 print_shortest_lock_dependencies(other, root);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002069
2070 printk("\nstack backtrace:\n");
2071 dump_stack();
2072
2073 return 0;
2074}
2075
2076/*
2077 * Prove that in the forwards-direction subgraph starting at <this>
2078 * there is no lock matching <mask>:
2079 */
2080static int
2081check_usage_forwards(struct task_struct *curr, struct held_lock *this,
2082 enum lock_usage_bit bit, const char *irqclass)
2083{
2084 int ret;
Ming Leid7aaba12009-07-16 15:44:29 +02002085 struct lock_list root;
2086 struct lock_list *uninitialized_var(target_entry);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002087
Ming Leid7aaba12009-07-16 15:44:29 +02002088 root.parent = NULL;
2089 root.class = hlock_class(this);
2090 ret = find_usage_forwards(&root, bit, &target_entry);
Peter Zijlstraaf012962009-07-16 15:44:29 +02002091 if (ret < 0)
2092 return print_bfs_bug(ret);
2093 if (ret == 1)
2094 return ret;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002095
Ming Lei24208ca2009-07-16 15:44:29 +02002096 return print_irq_inversion_bug(curr, &root, target_entry,
Ming Leid7aaba12009-07-16 15:44:29 +02002097 this, 1, irqclass);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002098}
2099
2100/*
2101 * Prove that in the backwards-direction subgraph starting at <this>
2102 * there is no lock matching <mask>:
2103 */
2104static int
2105check_usage_backwards(struct task_struct *curr, struct held_lock *this,
2106 enum lock_usage_bit bit, const char *irqclass)
2107{
2108 int ret;
Ming Leid7aaba12009-07-16 15:44:29 +02002109 struct lock_list root;
2110 struct lock_list *uninitialized_var(target_entry);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002111
Ming Leid7aaba12009-07-16 15:44:29 +02002112 root.parent = NULL;
2113 root.class = hlock_class(this);
2114 ret = find_usage_backwards(&root, bit, &target_entry);
Peter Zijlstraaf012962009-07-16 15:44:29 +02002115 if (ret < 0)
2116 return print_bfs_bug(ret);
2117 if (ret == 1)
2118 return ret;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002119
Ming Lei24208ca2009-07-16 15:44:29 +02002120 return print_irq_inversion_bug(curr, &root, target_entry,
Ming Leid7aaba12009-07-16 15:44:29 +02002121 this, 1, irqclass);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002122}
2123
Ingo Molnar3117df02006-12-13 00:34:43 -08002124void print_irqtrace_events(struct task_struct *curr)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002125{
2126 printk("irq event stamp: %u\n", curr->irq_events);
2127 printk("hardirqs last enabled at (%u): ", curr->hardirq_enable_event);
2128 print_ip_sym(curr->hardirq_enable_ip);
2129 printk("hardirqs last disabled at (%u): ", curr->hardirq_disable_event);
2130 print_ip_sym(curr->hardirq_disable_ip);
2131 printk("softirqs last enabled at (%u): ", curr->softirq_enable_event);
2132 print_ip_sym(curr->softirq_enable_ip);
2133 printk("softirqs last disabled at (%u): ", curr->softirq_disable_event);
2134 print_ip_sym(curr->softirq_disable_ip);
2135}
2136
Peter Zijlstracd953022009-01-22 16:38:21 +01002137static int HARDIRQ_verbose(struct lock_class *class)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002138{
Peter Zijlstra8e182572007-07-19 01:48:54 -07002139#if HARDIRQ_VERBOSE
2140 return class_filter(class);
2141#endif
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002142 return 0;
2143}
2144
Peter Zijlstracd953022009-01-22 16:38:21 +01002145static int SOFTIRQ_verbose(struct lock_class *class)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002146{
Peter Zijlstra8e182572007-07-19 01:48:54 -07002147#if SOFTIRQ_VERBOSE
2148 return class_filter(class);
2149#endif
2150 return 0;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002151}
2152
Peter Zijlstracd953022009-01-22 16:38:21 +01002153static int RECLAIM_FS_verbose(struct lock_class *class)
Nick Piggincf40bd12009-01-21 08:12:39 +01002154{
2155#if RECLAIM_VERBOSE
2156 return class_filter(class);
2157#endif
2158 return 0;
2159}
2160
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002161#define STRICT_READ_CHECKS 1
2162
Peter Zijlstracd953022009-01-22 16:38:21 +01002163static int (*state_verbose_f[])(struct lock_class *class) = {
2164#define LOCKDEP_STATE(__STATE) \
2165 __STATE##_verbose,
2166#include "lockdep_states.h"
2167#undef LOCKDEP_STATE
2168};
2169
2170static inline int state_verbose(enum lock_usage_bit bit,
2171 struct lock_class *class)
2172{
2173 return state_verbose_f[bit >> 2](class);
2174}
2175
Peter Zijlstra42c50d52009-01-22 16:58:16 +01002176typedef int (*check_usage_f)(struct task_struct *, struct held_lock *,
2177 enum lock_usage_bit bit, const char *name);
2178
Peter Zijlstra6a6904d2009-01-22 16:07:44 +01002179static int
Peter Zijlstra1c21f142009-03-04 13:51:13 +01002180mark_lock_irq(struct task_struct *curr, struct held_lock *this,
2181 enum lock_usage_bit new_bit)
Peter Zijlstra6a6904d2009-01-22 16:07:44 +01002182{
Peter Zijlstraf9892092009-01-22 16:09:59 +01002183 int excl_bit = exclusive_bit(new_bit);
Peter Zijlstra9d3651a2009-01-22 17:18:32 +01002184 int read = new_bit & 1;
Peter Zijlstra42c50d52009-01-22 16:58:16 +01002185 int dir = new_bit & 2;
2186
Peter Zijlstra38aa2712009-01-27 14:53:50 +01002187 /*
2188 * mark USED_IN has to look forwards -- to ensure no dependency
2189 * has ENABLED state, which would allow recursion deadlocks.
2190 *
2191 * mark ENABLED has to look backwards -- to ensure no dependee
2192 * has USED_IN state, which, again, would allow recursion deadlocks.
2193 */
Peter Zijlstra42c50d52009-01-22 16:58:16 +01002194 check_usage_f usage = dir ?
2195 check_usage_backwards : check_usage_forwards;
Peter Zijlstraf9892092009-01-22 16:09:59 +01002196
Peter Zijlstra38aa2712009-01-27 14:53:50 +01002197 /*
2198 * Validate that this particular lock does not have conflicting
2199 * usage states.
2200 */
Peter Zijlstra6a6904d2009-01-22 16:07:44 +01002201 if (!valid_state(curr, this, new_bit, excl_bit))
2202 return 0;
Peter Zijlstra9d3651a2009-01-22 17:18:32 +01002203
Peter Zijlstra38aa2712009-01-27 14:53:50 +01002204 /*
2205 * Validate that the lock dependencies don't have conflicting usage
2206 * states.
2207 */
2208 if ((!read || !dir || STRICT_READ_CHECKS) &&
Peter Zijlstra1c21f142009-03-04 13:51:13 +01002209 !usage(curr, this, excl_bit, state_name(new_bit & ~1)))
Peter Zijlstra6a6904d2009-01-22 16:07:44 +01002210 return 0;
Peter Zijlstra780e8202009-01-22 16:51:29 +01002211
Peter Zijlstra38aa2712009-01-27 14:53:50 +01002212 /*
2213 * Check for read in write conflicts
2214 */
2215 if (!read) {
2216 if (!valid_state(curr, this, new_bit, excl_bit + 1))
2217 return 0;
2218
2219 if (STRICT_READ_CHECKS &&
Peter Zijlstra4f367d8a2009-01-22 18:10:42 +01002220 !usage(curr, this, excl_bit + 1,
2221 state_name(new_bit + 1)))
Peter Zijlstra38aa2712009-01-27 14:53:50 +01002222 return 0;
2223 }
Peter Zijlstra780e8202009-01-22 16:51:29 +01002224
Peter Zijlstracd953022009-01-22 16:38:21 +01002225 if (state_verbose(new_bit, hlock_class(this)))
Peter Zijlstra6a6904d2009-01-22 16:07:44 +01002226 return 2;
2227
2228 return 1;
2229}
2230
Nick Piggincf40bd12009-01-21 08:12:39 +01002231enum mark_type {
Peter Zijlstra36bfb9b2009-01-22 14:12:41 +01002232#define LOCKDEP_STATE(__STATE) __STATE,
2233#include "lockdep_states.h"
2234#undef LOCKDEP_STATE
Nick Piggincf40bd12009-01-21 08:12:39 +01002235};
2236
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002237/*
2238 * Mark all held locks with a usage bit:
2239 */
Steven Rostedt1d09daa2008-05-12 21:20:55 +02002240static int
Nick Piggincf40bd12009-01-21 08:12:39 +01002241mark_held_locks(struct task_struct *curr, enum mark_type mark)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002242{
2243 enum lock_usage_bit usage_bit;
2244 struct held_lock *hlock;
2245 int i;
2246
2247 for (i = 0; i < curr->lockdep_depth; i++) {
2248 hlock = curr->held_locks + i;
2249
Peter Zijlstracf2ad4d2009-01-27 13:58:08 +01002250 usage_bit = 2 + (mark << 2); /* ENABLED */
2251 if (hlock->read)
2252 usage_bit += 1; /* READ */
2253
2254 BUG_ON(usage_bit >= LOCK_USAGE_STATES);
Nick Piggincf40bd12009-01-21 08:12:39 +01002255
Jarek Poplawski4ff773bb2007-05-08 00:31:00 -07002256 if (!mark_lock(curr, hlock, usage_bit))
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002257 return 0;
2258 }
2259
2260 return 1;
2261}
2262
2263/*
2264 * Debugging helper: via this flag we know that we are in
2265 * 'early bootup code', and will warn about any invalid irqs-on event:
2266 */
2267static int early_boot_irqs_enabled;
2268
2269void early_boot_irqs_off(void)
2270{
2271 early_boot_irqs_enabled = 0;
2272}
2273
2274void early_boot_irqs_on(void)
2275{
2276 early_boot_irqs_enabled = 1;
2277}
2278
2279/*
2280 * Hardirqs will be enabled:
2281 */
Heiko Carstens6afe40b2008-10-28 11:14:58 +01002282void trace_hardirqs_on_caller(unsigned long ip)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002283{
2284 struct task_struct *curr = current;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002285
Heiko Carstens6afe40b2008-10-28 11:14:58 +01002286 time_hardirqs_on(CALLER_ADDR0, ip);
Steven Rostedt81d68a92008-05-12 21:20:42 +02002287
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002288 if (unlikely(!debug_locks || current->lockdep_recursion))
2289 return;
2290
2291 if (DEBUG_LOCKS_WARN_ON(unlikely(!early_boot_irqs_enabled)))
2292 return;
2293
2294 if (unlikely(curr->hardirqs_enabled)) {
2295 debug_atomic_inc(&redundant_hardirqs_on);
2296 return;
2297 }
2298 /* we'll do an OFF -> ON transition: */
2299 curr->hardirqs_enabled = 1;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002300
2301 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2302 return;
2303 if (DEBUG_LOCKS_WARN_ON(current->hardirq_context))
2304 return;
2305 /*
2306 * We are going to turn hardirqs on, so set the
2307 * usage bit for all held locks:
2308 */
Nick Piggincf40bd12009-01-21 08:12:39 +01002309 if (!mark_held_locks(curr, HARDIRQ))
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002310 return;
2311 /*
2312 * If we have softirqs enabled, then set the usage
2313 * bit for all held locks. (disabled hardirqs prevented
2314 * this bit from being set before)
2315 */
2316 if (curr->softirqs_enabled)
Nick Piggincf40bd12009-01-21 08:12:39 +01002317 if (!mark_held_locks(curr, SOFTIRQ))
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002318 return;
2319
2320 curr->hardirq_enable_ip = ip;
2321 curr->hardirq_enable_event = ++curr->irq_events;
2322 debug_atomic_inc(&hardirqs_on_events);
2323}
Steven Rostedt81d68a92008-05-12 21:20:42 +02002324EXPORT_SYMBOL(trace_hardirqs_on_caller);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002325
Steven Rostedt1d09daa2008-05-12 21:20:55 +02002326void trace_hardirqs_on(void)
Steven Rostedt81d68a92008-05-12 21:20:42 +02002327{
2328 trace_hardirqs_on_caller(CALLER_ADDR0);
2329}
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002330EXPORT_SYMBOL(trace_hardirqs_on);
2331
2332/*
2333 * Hardirqs were disabled:
2334 */
Heiko Carstens6afe40b2008-10-28 11:14:58 +01002335void trace_hardirqs_off_caller(unsigned long ip)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002336{
2337 struct task_struct *curr = current;
2338
Heiko Carstens6afe40b2008-10-28 11:14:58 +01002339 time_hardirqs_off(CALLER_ADDR0, ip);
Steven Rostedt81d68a92008-05-12 21:20:42 +02002340
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002341 if (unlikely(!debug_locks || current->lockdep_recursion))
2342 return;
2343
2344 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2345 return;
2346
2347 if (curr->hardirqs_enabled) {
2348 /*
2349 * We have done an ON -> OFF transition:
2350 */
2351 curr->hardirqs_enabled = 0;
Heiko Carstens6afe40b2008-10-28 11:14:58 +01002352 curr->hardirq_disable_ip = ip;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002353 curr->hardirq_disable_event = ++curr->irq_events;
2354 debug_atomic_inc(&hardirqs_off_events);
2355 } else
2356 debug_atomic_inc(&redundant_hardirqs_off);
2357}
Steven Rostedt81d68a92008-05-12 21:20:42 +02002358EXPORT_SYMBOL(trace_hardirqs_off_caller);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002359
Steven Rostedt1d09daa2008-05-12 21:20:55 +02002360void trace_hardirqs_off(void)
Steven Rostedt81d68a92008-05-12 21:20:42 +02002361{
2362 trace_hardirqs_off_caller(CALLER_ADDR0);
2363}
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002364EXPORT_SYMBOL(trace_hardirqs_off);
2365
2366/*
2367 * Softirqs will be enabled:
2368 */
2369void trace_softirqs_on(unsigned long ip)
2370{
2371 struct task_struct *curr = current;
2372
2373 if (unlikely(!debug_locks))
2374 return;
2375
2376 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2377 return;
2378
2379 if (curr->softirqs_enabled) {
2380 debug_atomic_inc(&redundant_softirqs_on);
2381 return;
2382 }
2383
2384 /*
2385 * We'll do an OFF -> ON transition:
2386 */
2387 curr->softirqs_enabled = 1;
2388 curr->softirq_enable_ip = ip;
2389 curr->softirq_enable_event = ++curr->irq_events;
2390 debug_atomic_inc(&softirqs_on_events);
2391 /*
2392 * We are going to turn softirqs on, so set the
2393 * usage bit for all held locks, if hardirqs are
2394 * enabled too:
2395 */
2396 if (curr->hardirqs_enabled)
Nick Piggincf40bd12009-01-21 08:12:39 +01002397 mark_held_locks(curr, SOFTIRQ);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002398}
2399
2400/*
2401 * Softirqs were disabled:
2402 */
2403void trace_softirqs_off(unsigned long ip)
2404{
2405 struct task_struct *curr = current;
2406
2407 if (unlikely(!debug_locks))
2408 return;
2409
2410 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2411 return;
2412
2413 if (curr->softirqs_enabled) {
2414 /*
2415 * We have done an ON -> OFF transition:
2416 */
2417 curr->softirqs_enabled = 0;
2418 curr->softirq_disable_ip = ip;
2419 curr->softirq_disable_event = ++curr->irq_events;
2420 debug_atomic_inc(&softirqs_off_events);
2421 DEBUG_LOCKS_WARN_ON(!softirq_count());
2422 } else
2423 debug_atomic_inc(&redundant_softirqs_off);
2424}
2425
Peter Zijlstra2f850182009-03-20 11:13:20 +01002426static void __lockdep_trace_alloc(gfp_t gfp_mask, unsigned long flags)
Nick Piggincf40bd12009-01-21 08:12:39 +01002427{
2428 struct task_struct *curr = current;
2429
2430 if (unlikely(!debug_locks))
2431 return;
2432
2433 /* no reclaim without waiting on it */
2434 if (!(gfp_mask & __GFP_WAIT))
2435 return;
2436
2437 /* this guy won't enter reclaim */
2438 if ((curr->flags & PF_MEMALLOC) && !(gfp_mask & __GFP_NOMEMALLOC))
2439 return;
2440
2441 /* We're only interested __GFP_FS allocations for now */
2442 if (!(gfp_mask & __GFP_FS))
2443 return;
2444
Peter Zijlstra2f850182009-03-20 11:13:20 +01002445 if (DEBUG_LOCKS_WARN_ON(irqs_disabled_flags(flags)))
Nick Piggincf40bd12009-01-21 08:12:39 +01002446 return;
2447
2448 mark_held_locks(curr, RECLAIM_FS);
2449}
2450
Peter Zijlstra2f850182009-03-20 11:13:20 +01002451static void check_flags(unsigned long flags);
2452
2453void lockdep_trace_alloc(gfp_t gfp_mask)
2454{
2455 unsigned long flags;
2456
2457 if (unlikely(current->lockdep_recursion))
2458 return;
2459
2460 raw_local_irq_save(flags);
2461 check_flags(flags);
2462 current->lockdep_recursion = 1;
2463 __lockdep_trace_alloc(gfp_mask, flags);
2464 current->lockdep_recursion = 0;
2465 raw_local_irq_restore(flags);
2466}
2467
Peter Zijlstra8e182572007-07-19 01:48:54 -07002468static int mark_irqflags(struct task_struct *curr, struct held_lock *hlock)
2469{
2470 /*
2471 * If non-trylock use in a hardirq or softirq context, then
2472 * mark the lock as used in these contexts:
2473 */
2474 if (!hlock->trylock) {
2475 if (hlock->read) {
2476 if (curr->hardirq_context)
2477 if (!mark_lock(curr, hlock,
2478 LOCK_USED_IN_HARDIRQ_READ))
2479 return 0;
2480 if (curr->softirq_context)
2481 if (!mark_lock(curr, hlock,
2482 LOCK_USED_IN_SOFTIRQ_READ))
2483 return 0;
2484 } else {
2485 if (curr->hardirq_context)
2486 if (!mark_lock(curr, hlock, LOCK_USED_IN_HARDIRQ))
2487 return 0;
2488 if (curr->softirq_context)
2489 if (!mark_lock(curr, hlock, LOCK_USED_IN_SOFTIRQ))
2490 return 0;
2491 }
2492 }
2493 if (!hlock->hardirqs_off) {
2494 if (hlock->read) {
2495 if (!mark_lock(curr, hlock,
Peter Zijlstra4fc95e82009-01-22 13:10:52 +01002496 LOCK_ENABLED_HARDIRQ_READ))
Peter Zijlstra8e182572007-07-19 01:48:54 -07002497 return 0;
2498 if (curr->softirqs_enabled)
2499 if (!mark_lock(curr, hlock,
Peter Zijlstra4fc95e82009-01-22 13:10:52 +01002500 LOCK_ENABLED_SOFTIRQ_READ))
Peter Zijlstra8e182572007-07-19 01:48:54 -07002501 return 0;
2502 } else {
2503 if (!mark_lock(curr, hlock,
Peter Zijlstra4fc95e82009-01-22 13:10:52 +01002504 LOCK_ENABLED_HARDIRQ))
Peter Zijlstra8e182572007-07-19 01:48:54 -07002505 return 0;
2506 if (curr->softirqs_enabled)
2507 if (!mark_lock(curr, hlock,
Peter Zijlstra4fc95e82009-01-22 13:10:52 +01002508 LOCK_ENABLED_SOFTIRQ))
Peter Zijlstra8e182572007-07-19 01:48:54 -07002509 return 0;
2510 }
2511 }
2512
Nick Piggincf40bd12009-01-21 08:12:39 +01002513 /*
2514 * We reuse the irq context infrastructure more broadly as a general
2515 * context checking code. This tests GFP_FS recursion (a lock taken
2516 * during reclaim for a GFP_FS allocation is held over a GFP_FS
2517 * allocation).
2518 */
2519 if (!hlock->trylock && (curr->lockdep_reclaim_gfp & __GFP_FS)) {
2520 if (hlock->read) {
2521 if (!mark_lock(curr, hlock, LOCK_USED_IN_RECLAIM_FS_READ))
2522 return 0;
2523 } else {
2524 if (!mark_lock(curr, hlock, LOCK_USED_IN_RECLAIM_FS))
2525 return 0;
2526 }
2527 }
2528
Peter Zijlstra8e182572007-07-19 01:48:54 -07002529 return 1;
2530}
2531
2532static int separate_irq_context(struct task_struct *curr,
2533 struct held_lock *hlock)
2534{
2535 unsigned int depth = curr->lockdep_depth;
2536
2537 /*
2538 * Keep track of points where we cross into an interrupt context:
2539 */
2540 hlock->irq_context = 2*(curr->hardirq_context ? 1 : 0) +
2541 curr->softirq_context;
2542 if (depth) {
2543 struct held_lock *prev_hlock;
2544
2545 prev_hlock = curr->held_locks + depth-1;
2546 /*
2547 * If we cross into another context, reset the
2548 * hash key (this also prevents the checking and the
2549 * adding of the dependency to 'prev'):
2550 */
2551 if (prev_hlock->irq_context != hlock->irq_context)
2552 return 1;
2553 }
2554 return 0;
2555}
2556
2557#else
2558
2559static inline
2560int mark_lock_irq(struct task_struct *curr, struct held_lock *this,
2561 enum lock_usage_bit new_bit)
2562{
2563 WARN_ON(1);
2564 return 1;
2565}
2566
2567static inline int mark_irqflags(struct task_struct *curr,
2568 struct held_lock *hlock)
2569{
2570 return 1;
2571}
2572
2573static inline int separate_irq_context(struct task_struct *curr,
2574 struct held_lock *hlock)
2575{
2576 return 0;
2577}
2578
Peter Zijlstra868a23a2009-02-15 00:25:21 +01002579void lockdep_trace_alloc(gfp_t gfp_mask)
2580{
2581}
2582
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002583#endif
2584
2585/*
Peter Zijlstra8e182572007-07-19 01:48:54 -07002586 * Mark a lock with a usage bit, and validate the state transition:
2587 */
Steven Rostedt1d09daa2008-05-12 21:20:55 +02002588static int mark_lock(struct task_struct *curr, struct held_lock *this,
Steven Rostedt0764d232008-05-12 21:20:44 +02002589 enum lock_usage_bit new_bit)
Peter Zijlstra8e182572007-07-19 01:48:54 -07002590{
2591 unsigned int new_mask = 1 << new_bit, ret = 1;
2592
2593 /*
2594 * If already set then do not dirty the cacheline,
2595 * nor do any checks:
2596 */
Dave Jonesf82b2172008-08-11 09:30:23 +02002597 if (likely(hlock_class(this)->usage_mask & new_mask))
Peter Zijlstra8e182572007-07-19 01:48:54 -07002598 return 1;
2599
2600 if (!graph_lock())
2601 return 0;
2602 /*
2603 * Make sure we didnt race:
2604 */
Dave Jonesf82b2172008-08-11 09:30:23 +02002605 if (unlikely(hlock_class(this)->usage_mask & new_mask)) {
Peter Zijlstra8e182572007-07-19 01:48:54 -07002606 graph_unlock();
2607 return 1;
2608 }
2609
Dave Jonesf82b2172008-08-11 09:30:23 +02002610 hlock_class(this)->usage_mask |= new_mask;
Peter Zijlstra8e182572007-07-19 01:48:54 -07002611
Dave Jonesf82b2172008-08-11 09:30:23 +02002612 if (!save_trace(hlock_class(this)->usage_traces + new_bit))
Peter Zijlstra8e182572007-07-19 01:48:54 -07002613 return 0;
2614
2615 switch (new_bit) {
Peter Zijlstra53464172009-01-22 14:15:53 +01002616#define LOCKDEP_STATE(__STATE) \
2617 case LOCK_USED_IN_##__STATE: \
2618 case LOCK_USED_IN_##__STATE##_READ: \
2619 case LOCK_ENABLED_##__STATE: \
2620 case LOCK_ENABLED_##__STATE##_READ:
2621#include "lockdep_states.h"
2622#undef LOCKDEP_STATE
Peter Zijlstra8e182572007-07-19 01:48:54 -07002623 ret = mark_lock_irq(curr, this, new_bit);
2624 if (!ret)
2625 return 0;
2626 break;
2627 case LOCK_USED:
Peter Zijlstra8e182572007-07-19 01:48:54 -07002628 debug_atomic_dec(&nr_unused_locks);
2629 break;
2630 default:
2631 if (!debug_locks_off_graph_unlock())
2632 return 0;
2633 WARN_ON(1);
2634 return 0;
2635 }
2636
2637 graph_unlock();
2638
2639 /*
2640 * We must printk outside of the graph_lock:
2641 */
2642 if (ret == 2) {
2643 printk("\nmarked lock as {%s}:\n", usage_str[new_bit]);
2644 print_lock(this);
2645 print_irqtrace_events(curr);
2646 dump_stack();
2647 }
2648
2649 return ret;
2650}
2651
2652/*
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002653 * Initialize a lock instance's lock-class mapping info:
2654 */
2655void lockdep_init_map(struct lockdep_map *lock, const char *name,
Peter Zijlstra4dfbb9d2006-10-11 01:45:14 -04002656 struct lock_class_key *key, int subclass)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002657{
Peter Zijlstrac8a25002009-04-17 09:40:49 +02002658 lock->class_cache = NULL;
2659#ifdef CONFIG_LOCK_STAT
2660 lock->cpu = raw_smp_processor_id();
2661#endif
2662
2663 if (DEBUG_LOCKS_WARN_ON(!name)) {
2664 lock->name = "NULL";
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002665 return;
Peter Zijlstrac8a25002009-04-17 09:40:49 +02002666 }
2667
2668 lock->name = name;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002669
2670 if (DEBUG_LOCKS_WARN_ON(!key))
2671 return;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002672 /*
2673 * Sanity check, the lock-class key must be persistent:
2674 */
2675 if (!static_obj(key)) {
2676 printk("BUG: key %p not in .data!\n", key);
2677 DEBUG_LOCKS_WARN_ON(1);
2678 return;
2679 }
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002680 lock->key = key;
Peter Zijlstrac8a25002009-04-17 09:40:49 +02002681
2682 if (unlikely(!debug_locks))
2683 return;
2684
Peter Zijlstra4dfbb9d2006-10-11 01:45:14 -04002685 if (subclass)
2686 register_lock_class(lock, subclass, 1);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002687}
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002688EXPORT_SYMBOL_GPL(lockdep_init_map);
2689
2690/*
2691 * This gets called for every mutex_lock*()/spin_lock*() operation.
2692 * We maintain the dependency maps and validate the locking attempt:
2693 */
2694static int __lock_acquire(struct lockdep_map *lock, unsigned int subclass,
2695 int trylock, int read, int check, int hardirqs_off,
Peter Zijlstra7531e2f2008-08-11 09:30:24 +02002696 struct lockdep_map *nest_lock, unsigned long ip)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002697{
2698 struct task_struct *curr = current;
Ingo Molnard6d897c2006-07-10 04:44:04 -07002699 struct lock_class *class = NULL;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002700 struct held_lock *hlock;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002701 unsigned int depth, id;
2702 int chain_head = 0;
2703 u64 chain_key;
2704
Peter Zijlstraf20786f2007-07-19 01:48:56 -07002705 if (!prove_locking)
2706 check = 1;
2707
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002708 if (unlikely(!debug_locks))
2709 return 0;
2710
2711 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2712 return 0;
2713
2714 if (unlikely(subclass >= MAX_LOCKDEP_SUBCLASSES)) {
2715 debug_locks_off();
2716 printk("BUG: MAX_LOCKDEP_SUBCLASSES too low!\n");
2717 printk("turning off the locking correctness validator.\n");
Peter Zijlstraeedeeab2009-03-18 12:38:47 +01002718 dump_stack();
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002719 return 0;
2720 }
2721
Ingo Molnard6d897c2006-07-10 04:44:04 -07002722 if (!subclass)
2723 class = lock->class_cache;
2724 /*
2725 * Not cached yet or subclass?
2726 */
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002727 if (unlikely(!class)) {
Peter Zijlstra4dfbb9d2006-10-11 01:45:14 -04002728 class = register_lock_class(lock, subclass, 0);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002729 if (!class)
2730 return 0;
2731 }
2732 debug_atomic_inc((atomic_t *)&class->ops);
2733 if (very_verbose(class)) {
2734 printk("\nacquire class [%p] %s", class->key, class->name);
2735 if (class->name_version > 1)
2736 printk("#%d", class->name_version);
2737 printk("\n");
2738 dump_stack();
2739 }
2740
2741 /*
2742 * Add the lock to the list of currently held locks.
2743 * (we dont increase the depth just yet, up until the
2744 * dependency checks are done)
2745 */
2746 depth = curr->lockdep_depth;
2747 if (DEBUG_LOCKS_WARN_ON(depth >= MAX_LOCK_DEPTH))
2748 return 0;
2749
2750 hlock = curr->held_locks + depth;
Dave Jonesf82b2172008-08-11 09:30:23 +02002751 if (DEBUG_LOCKS_WARN_ON(!class))
2752 return 0;
2753 hlock->class_idx = class - lock_classes + 1;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002754 hlock->acquire_ip = ip;
2755 hlock->instance = lock;
Peter Zijlstra7531e2f2008-08-11 09:30:24 +02002756 hlock->nest_lock = nest_lock;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002757 hlock->trylock = trylock;
2758 hlock->read = read;
2759 hlock->check = check;
Dmitry Baryshkov6951b122008-08-18 04:26:37 +04002760 hlock->hardirqs_off = !!hardirqs_off;
Peter Zijlstraf20786f2007-07-19 01:48:56 -07002761#ifdef CONFIG_LOCK_STAT
2762 hlock->waittime_stamp = 0;
2763 hlock->holdtime_stamp = sched_clock();
2764#endif
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002765
Peter Zijlstra8e182572007-07-19 01:48:54 -07002766 if (check == 2 && !mark_irqflags(curr, hlock))
2767 return 0;
2768
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002769 /* mark it as used: */
Jarek Poplawski4ff773bb2007-05-08 00:31:00 -07002770 if (!mark_lock(curr, hlock, LOCK_USED))
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002771 return 0;
Peter Zijlstra8e182572007-07-19 01:48:54 -07002772
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002773 /*
Gautham R Shenoy17aacfb2007-10-28 20:47:01 +01002774 * Calculate the chain hash: it's the combined hash of all the
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002775 * lock keys along the dependency chain. We save the hash value
2776 * at every step so that we can get the current hash easily
2777 * after unlock. The chain hash is then used to cache dependency
2778 * results.
2779 *
2780 * The 'key ID' is what is the most compact key value to drive
2781 * the hash, not class->key.
2782 */
2783 id = class - lock_classes;
2784 if (DEBUG_LOCKS_WARN_ON(id >= MAX_LOCKDEP_KEYS))
2785 return 0;
2786
2787 chain_key = curr->curr_chain_key;
2788 if (!depth) {
2789 if (DEBUG_LOCKS_WARN_ON(chain_key != 0))
2790 return 0;
2791 chain_head = 1;
2792 }
2793
2794 hlock->prev_chain_key = chain_key;
Peter Zijlstra8e182572007-07-19 01:48:54 -07002795 if (separate_irq_context(curr, hlock)) {
2796 chain_key = 0;
2797 chain_head = 1;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002798 }
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002799 chain_key = iterate_chain_key(chain_key, id);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002800
Gregory Haskins3aa416b2007-10-11 22:11:11 +02002801 if (!validate_chain(curr, lock, hlock, chain_head, chain_key))
Peter Zijlstra8e182572007-07-19 01:48:54 -07002802 return 0;
Jarek Poplawski381a2292007-02-10 01:44:58 -08002803
Gregory Haskins3aa416b2007-10-11 22:11:11 +02002804 curr->curr_chain_key = chain_key;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002805 curr->lockdep_depth++;
2806 check_chain_key(curr);
Jarek Poplawski60e114d2007-02-20 13:58:00 -08002807#ifdef CONFIG_DEBUG_LOCKDEP
2808 if (unlikely(!debug_locks))
2809 return 0;
2810#endif
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002811 if (unlikely(curr->lockdep_depth >= MAX_LOCK_DEPTH)) {
2812 debug_locks_off();
2813 printk("BUG: MAX_LOCK_DEPTH too low!\n");
2814 printk("turning off the locking correctness validator.\n");
Peter Zijlstraeedeeab2009-03-18 12:38:47 +01002815 dump_stack();
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002816 return 0;
2817 }
Jarek Poplawski381a2292007-02-10 01:44:58 -08002818
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002819 if (unlikely(curr->lockdep_depth > max_lockdep_depth))
2820 max_lockdep_depth = curr->lockdep_depth;
2821
2822 return 1;
2823}
2824
2825static int
2826print_unlock_inbalance_bug(struct task_struct *curr, struct lockdep_map *lock,
2827 unsigned long ip)
2828{
2829 if (!debug_locks_off())
2830 return 0;
2831 if (debug_locks_silent)
2832 return 0;
2833
2834 printk("\n=====================================\n");
2835 printk( "[ BUG: bad unlock balance detected! ]\n");
2836 printk( "-------------------------------------\n");
2837 printk("%s/%d is trying to release lock (",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07002838 curr->comm, task_pid_nr(curr));
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002839 print_lockdep_cache(lock);
2840 printk(") at:\n");
2841 print_ip_sym(ip);
2842 printk("but there are no more locks to release!\n");
2843 printk("\nother info that might help us debug this:\n");
2844 lockdep_print_held_locks(curr);
2845
2846 printk("\nstack backtrace:\n");
2847 dump_stack();
2848
2849 return 0;
2850}
2851
2852/*
2853 * Common debugging checks for both nested and non-nested unlock:
2854 */
2855static int check_unlock(struct task_struct *curr, struct lockdep_map *lock,
2856 unsigned long ip)
2857{
2858 if (unlikely(!debug_locks))
2859 return 0;
2860 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2861 return 0;
2862
2863 if (curr->lockdep_depth <= 0)
2864 return print_unlock_inbalance_bug(curr, lock, ip);
2865
2866 return 1;
2867}
2868
Peter Zijlstra64aa3482008-08-11 09:30:21 +02002869static int
Peter Zijlstra00ef9f72008-12-04 09:00:17 +01002870__lock_set_class(struct lockdep_map *lock, const char *name,
2871 struct lock_class_key *key, unsigned int subclass,
2872 unsigned long ip)
Peter Zijlstra64aa3482008-08-11 09:30:21 +02002873{
2874 struct task_struct *curr = current;
2875 struct held_lock *hlock, *prev_hlock;
2876 struct lock_class *class;
2877 unsigned int depth;
2878 int i;
2879
2880 depth = curr->lockdep_depth;
2881 if (DEBUG_LOCKS_WARN_ON(!depth))
2882 return 0;
2883
2884 prev_hlock = NULL;
2885 for (i = depth-1; i >= 0; i--) {
2886 hlock = curr->held_locks + i;
2887 /*
2888 * We must not cross into another context:
2889 */
2890 if (prev_hlock && prev_hlock->irq_context != hlock->irq_context)
2891 break;
2892 if (hlock->instance == lock)
2893 goto found_it;
2894 prev_hlock = hlock;
2895 }
2896 return print_unlock_inbalance_bug(curr, lock, ip);
2897
2898found_it:
Peter Zijlstra00ef9f72008-12-04 09:00:17 +01002899 lockdep_init_map(lock, name, key, 0);
Peter Zijlstra64aa3482008-08-11 09:30:21 +02002900 class = register_lock_class(lock, subclass, 0);
Dave Jonesf82b2172008-08-11 09:30:23 +02002901 hlock->class_idx = class - lock_classes + 1;
Peter Zijlstra64aa3482008-08-11 09:30:21 +02002902
2903 curr->lockdep_depth = i;
2904 curr->curr_chain_key = hlock->prev_chain_key;
2905
2906 for (; i < depth; i++) {
2907 hlock = curr->held_locks + i;
2908 if (!__lock_acquire(hlock->instance,
Dave Jonesf82b2172008-08-11 09:30:23 +02002909 hlock_class(hlock)->subclass, hlock->trylock,
Peter Zijlstra64aa3482008-08-11 09:30:21 +02002910 hlock->read, hlock->check, hlock->hardirqs_off,
Peter Zijlstra7531e2f2008-08-11 09:30:24 +02002911 hlock->nest_lock, hlock->acquire_ip))
Peter Zijlstra64aa3482008-08-11 09:30:21 +02002912 return 0;
2913 }
2914
2915 if (DEBUG_LOCKS_WARN_ON(curr->lockdep_depth != depth))
2916 return 0;
2917 return 1;
2918}
2919
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002920/*
2921 * Remove the lock to the list of currently held locks in a
2922 * potentially non-nested (out of order) manner. This is a
2923 * relatively rare operation, as all the unlock APIs default
2924 * to nested mode (which uses lock_release()):
2925 */
2926static int
2927lock_release_non_nested(struct task_struct *curr,
2928 struct lockdep_map *lock, unsigned long ip)
2929{
2930 struct held_lock *hlock, *prev_hlock;
2931 unsigned int depth;
2932 int i;
2933
2934 /*
2935 * Check whether the lock exists in the current stack
2936 * of held locks:
2937 */
2938 depth = curr->lockdep_depth;
2939 if (DEBUG_LOCKS_WARN_ON(!depth))
2940 return 0;
2941
2942 prev_hlock = NULL;
2943 for (i = depth-1; i >= 0; i--) {
2944 hlock = curr->held_locks + i;
2945 /*
2946 * We must not cross into another context:
2947 */
2948 if (prev_hlock && prev_hlock->irq_context != hlock->irq_context)
2949 break;
2950 if (hlock->instance == lock)
2951 goto found_it;
2952 prev_hlock = hlock;
2953 }
2954 return print_unlock_inbalance_bug(curr, lock, ip);
2955
2956found_it:
Peter Zijlstraf20786f2007-07-19 01:48:56 -07002957 lock_release_holdtime(hlock);
2958
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002959 /*
2960 * We have the right lock to unlock, 'hlock' points to it.
2961 * Now we remove it from the stack, and add back the other
2962 * entries (if any), recalculating the hash along the way:
2963 */
2964 curr->lockdep_depth = i;
2965 curr->curr_chain_key = hlock->prev_chain_key;
2966
2967 for (i++; i < depth; i++) {
2968 hlock = curr->held_locks + i;
2969 if (!__lock_acquire(hlock->instance,
Dave Jonesf82b2172008-08-11 09:30:23 +02002970 hlock_class(hlock)->subclass, hlock->trylock,
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002971 hlock->read, hlock->check, hlock->hardirqs_off,
Peter Zijlstra7531e2f2008-08-11 09:30:24 +02002972 hlock->nest_lock, hlock->acquire_ip))
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002973 return 0;
2974 }
2975
2976 if (DEBUG_LOCKS_WARN_ON(curr->lockdep_depth != depth - 1))
2977 return 0;
2978 return 1;
2979}
2980
2981/*
2982 * Remove the lock to the list of currently held locks - this gets
2983 * called on mutex_unlock()/spin_unlock*() (or on a failed
2984 * mutex_lock_interruptible()). This is done for unlocks that nest
2985 * perfectly. (i.e. the current top of the lock-stack is unlocked)
2986 */
2987static int lock_release_nested(struct task_struct *curr,
2988 struct lockdep_map *lock, unsigned long ip)
2989{
2990 struct held_lock *hlock;
2991 unsigned int depth;
2992
2993 /*
2994 * Pop off the top of the lock stack:
2995 */
2996 depth = curr->lockdep_depth - 1;
2997 hlock = curr->held_locks + depth;
2998
2999 /*
3000 * Is the unlock non-nested:
3001 */
3002 if (hlock->instance != lock)
3003 return lock_release_non_nested(curr, lock, ip);
3004 curr->lockdep_depth--;
3005
3006 if (DEBUG_LOCKS_WARN_ON(!depth && (hlock->prev_chain_key != 0)))
3007 return 0;
3008
3009 curr->curr_chain_key = hlock->prev_chain_key;
3010
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003011 lock_release_holdtime(hlock);
3012
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003013#ifdef CONFIG_DEBUG_LOCKDEP
3014 hlock->prev_chain_key = 0;
Dave Jonesf82b2172008-08-11 09:30:23 +02003015 hlock->class_idx = 0;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003016 hlock->acquire_ip = 0;
3017 hlock->irq_context = 0;
3018#endif
3019 return 1;
3020}
3021
3022/*
3023 * Remove the lock to the list of currently held locks - this gets
3024 * called on mutex_unlock()/spin_unlock*() (or on a failed
3025 * mutex_lock_interruptible()). This is done for unlocks that nest
3026 * perfectly. (i.e. the current top of the lock-stack is unlocked)
3027 */
3028static void
3029__lock_release(struct lockdep_map *lock, int nested, unsigned long ip)
3030{
3031 struct task_struct *curr = current;
3032
3033 if (!check_unlock(curr, lock, ip))
3034 return;
3035
3036 if (nested) {
3037 if (!lock_release_nested(curr, lock, ip))
3038 return;
3039 } else {
3040 if (!lock_release_non_nested(curr, lock, ip))
3041 return;
3042 }
3043
3044 check_chain_key(curr);
3045}
3046
3047/*
3048 * Check whether we follow the irq-flags state precisely:
3049 */
Steven Rostedt1d09daa2008-05-12 21:20:55 +02003050static void check_flags(unsigned long flags)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003051{
Ingo Molnar992860e2008-07-14 10:28:38 +02003052#if defined(CONFIG_PROVE_LOCKING) && defined(CONFIG_DEBUG_LOCKDEP) && \
3053 defined(CONFIG_TRACE_IRQFLAGS)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003054 if (!debug_locks)
3055 return;
3056
Ingo Molnar5f9fa8a2007-12-07 19:02:47 +01003057 if (irqs_disabled_flags(flags)) {
3058 if (DEBUG_LOCKS_WARN_ON(current->hardirqs_enabled)) {
3059 printk("possible reason: unannotated irqs-off.\n");
3060 }
3061 } else {
3062 if (DEBUG_LOCKS_WARN_ON(!current->hardirqs_enabled)) {
3063 printk("possible reason: unannotated irqs-on.\n");
3064 }
3065 }
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003066
3067 /*
3068 * We dont accurately track softirq state in e.g.
3069 * hardirq contexts (such as on 4KSTACKS), so only
3070 * check if not in hardirq contexts:
3071 */
3072 if (!hardirq_count()) {
3073 if (softirq_count())
3074 DEBUG_LOCKS_WARN_ON(current->softirqs_enabled);
3075 else
3076 DEBUG_LOCKS_WARN_ON(!current->softirqs_enabled);
3077 }
3078
3079 if (!debug_locks)
3080 print_irqtrace_events(current);
3081#endif
3082}
3083
Peter Zijlstra00ef9f72008-12-04 09:00:17 +01003084void lock_set_class(struct lockdep_map *lock, const char *name,
3085 struct lock_class_key *key, unsigned int subclass,
3086 unsigned long ip)
Peter Zijlstra64aa3482008-08-11 09:30:21 +02003087{
3088 unsigned long flags;
3089
3090 if (unlikely(current->lockdep_recursion))
3091 return;
3092
3093 raw_local_irq_save(flags);
3094 current->lockdep_recursion = 1;
3095 check_flags(flags);
Peter Zijlstra00ef9f72008-12-04 09:00:17 +01003096 if (__lock_set_class(lock, name, key, subclass, ip))
Peter Zijlstra64aa3482008-08-11 09:30:21 +02003097 check_chain_key(current);
3098 current->lockdep_recursion = 0;
3099 raw_local_irq_restore(flags);
3100}
Peter Zijlstra00ef9f72008-12-04 09:00:17 +01003101EXPORT_SYMBOL_GPL(lock_set_class);
Peter Zijlstra64aa3482008-08-11 09:30:21 +02003102
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003103/*
3104 * We are not always called with irqs disabled - do that here,
3105 * and also avoid lockdep recursion:
3106 */
Steven Rostedt1d09daa2008-05-12 21:20:55 +02003107void lock_acquire(struct lockdep_map *lock, unsigned int subclass,
Peter Zijlstra7531e2f2008-08-11 09:30:24 +02003108 int trylock, int read, int check,
3109 struct lockdep_map *nest_lock, unsigned long ip)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003110{
3111 unsigned long flags;
3112
Peter Zijlstraefed7922009-03-04 12:32:55 +01003113 trace_lock_acquire(lock, subclass, trylock, read, check, nest_lock, ip);
3114
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003115 if (unlikely(current->lockdep_recursion))
3116 return;
3117
3118 raw_local_irq_save(flags);
3119 check_flags(flags);
3120
3121 current->lockdep_recursion = 1;
3122 __lock_acquire(lock, subclass, trylock, read, check,
Peter Zijlstra7531e2f2008-08-11 09:30:24 +02003123 irqs_disabled_flags(flags), nest_lock, ip);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003124 current->lockdep_recursion = 0;
3125 raw_local_irq_restore(flags);
3126}
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003127EXPORT_SYMBOL_GPL(lock_acquire);
3128
Steven Rostedt1d09daa2008-05-12 21:20:55 +02003129void lock_release(struct lockdep_map *lock, int nested,
Steven Rostedt0764d232008-05-12 21:20:44 +02003130 unsigned long ip)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003131{
3132 unsigned long flags;
3133
Peter Zijlstraefed7922009-03-04 12:32:55 +01003134 trace_lock_release(lock, nested, ip);
3135
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003136 if (unlikely(current->lockdep_recursion))
3137 return;
3138
3139 raw_local_irq_save(flags);
3140 check_flags(flags);
3141 current->lockdep_recursion = 1;
3142 __lock_release(lock, nested, ip);
3143 current->lockdep_recursion = 0;
3144 raw_local_irq_restore(flags);
3145}
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003146EXPORT_SYMBOL_GPL(lock_release);
3147
Nick Piggincf40bd12009-01-21 08:12:39 +01003148void lockdep_set_current_reclaim_state(gfp_t gfp_mask)
3149{
3150 current->lockdep_reclaim_gfp = gfp_mask;
3151}
3152
3153void lockdep_clear_current_reclaim_state(void)
3154{
3155 current->lockdep_reclaim_gfp = 0;
3156}
3157
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003158#ifdef CONFIG_LOCK_STAT
3159static int
3160print_lock_contention_bug(struct task_struct *curr, struct lockdep_map *lock,
3161 unsigned long ip)
3162{
3163 if (!debug_locks_off())
3164 return 0;
3165 if (debug_locks_silent)
3166 return 0;
3167
3168 printk("\n=================================\n");
3169 printk( "[ BUG: bad contention detected! ]\n");
3170 printk( "---------------------------------\n");
3171 printk("%s/%d is trying to contend lock (",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07003172 curr->comm, task_pid_nr(curr));
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003173 print_lockdep_cache(lock);
3174 printk(") at:\n");
3175 print_ip_sym(ip);
3176 printk("but there are no locks held!\n");
3177 printk("\nother info that might help us debug this:\n");
3178 lockdep_print_held_locks(curr);
3179
3180 printk("\nstack backtrace:\n");
3181 dump_stack();
3182
3183 return 0;
3184}
3185
3186static void
3187__lock_contended(struct lockdep_map *lock, unsigned long ip)
3188{
3189 struct task_struct *curr = current;
3190 struct held_lock *hlock, *prev_hlock;
3191 struct lock_class_stats *stats;
3192 unsigned int depth;
Peter Zijlstrac7e78cf2008-10-16 23:17:09 +02003193 int i, contention_point, contending_point;
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003194
3195 depth = curr->lockdep_depth;
3196 if (DEBUG_LOCKS_WARN_ON(!depth))
3197 return;
3198
3199 prev_hlock = NULL;
3200 for (i = depth-1; i >= 0; i--) {
3201 hlock = curr->held_locks + i;
3202 /*
3203 * We must not cross into another context:
3204 */
3205 if (prev_hlock && prev_hlock->irq_context != hlock->irq_context)
3206 break;
3207 if (hlock->instance == lock)
3208 goto found_it;
3209 prev_hlock = hlock;
3210 }
3211 print_lock_contention_bug(curr, lock, ip);
3212 return;
3213
3214found_it:
3215 hlock->waittime_stamp = sched_clock();
3216
Peter Zijlstrac7e78cf2008-10-16 23:17:09 +02003217 contention_point = lock_point(hlock_class(hlock)->contention_point, ip);
3218 contending_point = lock_point(hlock_class(hlock)->contending_point,
3219 lock->ip);
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003220
Dave Jonesf82b2172008-08-11 09:30:23 +02003221 stats = get_lock_stats(hlock_class(hlock));
Peter Zijlstrac7e78cf2008-10-16 23:17:09 +02003222 if (contention_point < LOCKSTAT_POINTS)
3223 stats->contention_point[contention_point]++;
3224 if (contending_point < LOCKSTAT_POINTS)
3225 stats->contending_point[contending_point]++;
Peter Zijlstra96645672007-07-19 01:49:00 -07003226 if (lock->cpu != smp_processor_id())
3227 stats->bounces[bounce_contended + !!hlock->read]++;
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003228 put_lock_stats(stats);
3229}
3230
3231static void
Peter Zijlstrac7e78cf2008-10-16 23:17:09 +02003232__lock_acquired(struct lockdep_map *lock, unsigned long ip)
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003233{
3234 struct task_struct *curr = current;
3235 struct held_lock *hlock, *prev_hlock;
3236 struct lock_class_stats *stats;
3237 unsigned int depth;
3238 u64 now;
Peter Zijlstra96645672007-07-19 01:49:00 -07003239 s64 waittime = 0;
3240 int i, cpu;
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003241
3242 depth = curr->lockdep_depth;
3243 if (DEBUG_LOCKS_WARN_ON(!depth))
3244 return;
3245
3246 prev_hlock = NULL;
3247 for (i = depth-1; i >= 0; i--) {
3248 hlock = curr->held_locks + i;
3249 /*
3250 * We must not cross into another context:
3251 */
3252 if (prev_hlock && prev_hlock->irq_context != hlock->irq_context)
3253 break;
3254 if (hlock->instance == lock)
3255 goto found_it;
3256 prev_hlock = hlock;
3257 }
3258 print_lock_contention_bug(curr, lock, _RET_IP_);
3259 return;
3260
3261found_it:
Peter Zijlstra96645672007-07-19 01:49:00 -07003262 cpu = smp_processor_id();
3263 if (hlock->waittime_stamp) {
3264 now = sched_clock();
3265 waittime = now - hlock->waittime_stamp;
3266 hlock->holdtime_stamp = now;
3267 }
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003268
Frederic Weisbecker20625012009-04-06 01:49:33 +02003269 trace_lock_acquired(lock, ip, waittime);
3270
Dave Jonesf82b2172008-08-11 09:30:23 +02003271 stats = get_lock_stats(hlock_class(hlock));
Peter Zijlstra96645672007-07-19 01:49:00 -07003272 if (waittime) {
3273 if (hlock->read)
3274 lock_time_inc(&stats->read_waittime, waittime);
3275 else
3276 lock_time_inc(&stats->write_waittime, waittime);
3277 }
3278 if (lock->cpu != cpu)
3279 stats->bounces[bounce_acquired + !!hlock->read]++;
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003280 put_lock_stats(stats);
Peter Zijlstra96645672007-07-19 01:49:00 -07003281
3282 lock->cpu = cpu;
Peter Zijlstrac7e78cf2008-10-16 23:17:09 +02003283 lock->ip = ip;
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003284}
3285
3286void lock_contended(struct lockdep_map *lock, unsigned long ip)
3287{
3288 unsigned long flags;
3289
Peter Zijlstraefed7922009-03-04 12:32:55 +01003290 trace_lock_contended(lock, ip);
3291
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003292 if (unlikely(!lock_stat))
3293 return;
3294
3295 if (unlikely(current->lockdep_recursion))
3296 return;
3297
3298 raw_local_irq_save(flags);
3299 check_flags(flags);
3300 current->lockdep_recursion = 1;
3301 __lock_contended(lock, ip);
3302 current->lockdep_recursion = 0;
3303 raw_local_irq_restore(flags);
3304}
3305EXPORT_SYMBOL_GPL(lock_contended);
3306
Peter Zijlstrac7e78cf2008-10-16 23:17:09 +02003307void lock_acquired(struct lockdep_map *lock, unsigned long ip)
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003308{
3309 unsigned long flags;
3310
3311 if (unlikely(!lock_stat))
3312 return;
3313
3314 if (unlikely(current->lockdep_recursion))
3315 return;
3316
3317 raw_local_irq_save(flags);
3318 check_flags(flags);
3319 current->lockdep_recursion = 1;
Peter Zijlstrac7e78cf2008-10-16 23:17:09 +02003320 __lock_acquired(lock, ip);
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003321 current->lockdep_recursion = 0;
3322 raw_local_irq_restore(flags);
3323}
3324EXPORT_SYMBOL_GPL(lock_acquired);
3325#endif
3326
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003327/*
3328 * Used by the testsuite, sanitize the validator state
3329 * after a simulated failure:
3330 */
3331
3332void lockdep_reset(void)
3333{
3334 unsigned long flags;
Ingo Molnar23d95a02006-12-13 00:34:40 -08003335 int i;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003336
3337 raw_local_irq_save(flags);
3338 current->curr_chain_key = 0;
3339 current->lockdep_depth = 0;
3340 current->lockdep_recursion = 0;
3341 memset(current->held_locks, 0, MAX_LOCK_DEPTH*sizeof(struct held_lock));
3342 nr_hardirq_chains = 0;
3343 nr_softirq_chains = 0;
3344 nr_process_chains = 0;
3345 debug_locks = 1;
Ingo Molnar23d95a02006-12-13 00:34:40 -08003346 for (i = 0; i < CHAINHASH_SIZE; i++)
3347 INIT_LIST_HEAD(chainhash_table + i);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003348 raw_local_irq_restore(flags);
3349}
3350
3351static void zap_class(struct lock_class *class)
3352{
3353 int i;
3354
3355 /*
3356 * Remove all dependencies this lock is
3357 * involved in:
3358 */
3359 for (i = 0; i < nr_list_entries; i++) {
3360 if (list_entries[i].class == class)
3361 list_del_rcu(&list_entries[i].entry);
3362 }
3363 /*
3364 * Unhash the class and remove it from the all_lock_classes list:
3365 */
3366 list_del_rcu(&class->hash_entry);
3367 list_del_rcu(&class->lock_entry);
3368
Rabin Vincent8bfe0292008-08-11 09:30:26 +02003369 class->key = NULL;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003370}
3371
Arjan van de Venfabe8742008-01-24 07:00:45 +01003372static inline int within(const void *addr, void *start, unsigned long size)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003373{
3374 return addr >= start && addr < start + size;
3375}
3376
3377void lockdep_free_key_range(void *start, unsigned long size)
3378{
3379 struct lock_class *class, *next;
3380 struct list_head *head;
3381 unsigned long flags;
3382 int i;
Nick Piggin5a26db52008-01-16 09:51:58 +01003383 int locked;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003384
3385 raw_local_irq_save(flags);
Nick Piggin5a26db52008-01-16 09:51:58 +01003386 locked = graph_lock();
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003387
3388 /*
3389 * Unhash all classes that were created by this module:
3390 */
3391 for (i = 0; i < CLASSHASH_SIZE; i++) {
3392 head = classhash_table + i;
3393 if (list_empty(head))
3394 continue;
Arjan van de Venfabe8742008-01-24 07:00:45 +01003395 list_for_each_entry_safe(class, next, head, hash_entry) {
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003396 if (within(class->key, start, size))
3397 zap_class(class);
Arjan van de Venfabe8742008-01-24 07:00:45 +01003398 else if (within(class->name, start, size))
3399 zap_class(class);
3400 }
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003401 }
3402
Nick Piggin5a26db52008-01-16 09:51:58 +01003403 if (locked)
3404 graph_unlock();
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003405 raw_local_irq_restore(flags);
3406}
3407
3408void lockdep_reset_lock(struct lockdep_map *lock)
3409{
Ingo Molnard6d897c2006-07-10 04:44:04 -07003410 struct lock_class *class, *next;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003411 struct list_head *head;
3412 unsigned long flags;
3413 int i, j;
Nick Piggin5a26db52008-01-16 09:51:58 +01003414 int locked;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003415
3416 raw_local_irq_save(flags);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003417
3418 /*
Ingo Molnard6d897c2006-07-10 04:44:04 -07003419 * Remove all classes this lock might have:
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003420 */
Ingo Molnard6d897c2006-07-10 04:44:04 -07003421 for (j = 0; j < MAX_LOCKDEP_SUBCLASSES; j++) {
3422 /*
3423 * If the class exists we look it up and zap it:
3424 */
3425 class = look_up_lock_class(lock, j);
3426 if (class)
3427 zap_class(class);
3428 }
3429 /*
3430 * Debug check: in the end all mapped classes should
3431 * be gone.
3432 */
Nick Piggin5a26db52008-01-16 09:51:58 +01003433 locked = graph_lock();
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003434 for (i = 0; i < CLASSHASH_SIZE; i++) {
3435 head = classhash_table + i;
3436 if (list_empty(head))
3437 continue;
3438 list_for_each_entry_safe(class, next, head, hash_entry) {
Ingo Molnard6d897c2006-07-10 04:44:04 -07003439 if (unlikely(class == lock->class_cache)) {
Ingo Molnar74c383f2006-12-13 00:34:43 -08003440 if (debug_locks_off_graph_unlock())
3441 WARN_ON(1);
Ingo Molnard6d897c2006-07-10 04:44:04 -07003442 goto out_restore;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003443 }
3444 }
3445 }
Nick Piggin5a26db52008-01-16 09:51:58 +01003446 if (locked)
3447 graph_unlock();
Ingo Molnard6d897c2006-07-10 04:44:04 -07003448
3449out_restore:
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003450 raw_local_irq_restore(flags);
3451}
3452
Sam Ravnborg14999932007-02-28 20:12:31 -08003453void lockdep_init(void)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003454{
3455 int i;
3456
3457 /*
3458 * Some architectures have their own start_kernel()
3459 * code which calls lockdep_init(), while we also
3460 * call lockdep_init() from the start_kernel() itself,
3461 * and we want to initialize the hashes only once:
3462 */
3463 if (lockdep_initialized)
3464 return;
3465
3466 for (i = 0; i < CLASSHASH_SIZE; i++)
3467 INIT_LIST_HEAD(classhash_table + i);
3468
3469 for (i = 0; i < CHAINHASH_SIZE; i++)
3470 INIT_LIST_HEAD(chainhash_table + i);
3471
3472 lockdep_initialized = 1;
3473}
3474
3475void __init lockdep_info(void)
3476{
3477 printk("Lock dependency validator: Copyright (c) 2006 Red Hat, Inc., Ingo Molnar\n");
3478
Li Zefanb0788ca2008-11-21 15:57:32 +08003479 printk("... MAX_LOCKDEP_SUBCLASSES: %lu\n", MAX_LOCKDEP_SUBCLASSES);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003480 printk("... MAX_LOCK_DEPTH: %lu\n", MAX_LOCK_DEPTH);
3481 printk("... MAX_LOCKDEP_KEYS: %lu\n", MAX_LOCKDEP_KEYS);
Li Zefanb0788ca2008-11-21 15:57:32 +08003482 printk("... CLASSHASH_SIZE: %lu\n", CLASSHASH_SIZE);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003483 printk("... MAX_LOCKDEP_ENTRIES: %lu\n", MAX_LOCKDEP_ENTRIES);
3484 printk("... MAX_LOCKDEP_CHAINS: %lu\n", MAX_LOCKDEP_CHAINS);
3485 printk("... CHAINHASH_SIZE: %lu\n", CHAINHASH_SIZE);
3486
3487 printk(" memory used by lock dependency info: %lu kB\n",
3488 (sizeof(struct lock_class) * MAX_LOCKDEP_KEYS +
3489 sizeof(struct list_head) * CLASSHASH_SIZE +
3490 sizeof(struct lock_list) * MAX_LOCKDEP_ENTRIES +
3491 sizeof(struct lock_chain) * MAX_LOCKDEP_CHAINS +
Ming Lei4dd861d2009-07-16 15:44:29 +02003492 sizeof(struct list_head) * CHAINHASH_SIZE) / 1024
3493#ifdef CONFIG_PROVE_LOCKING
3494 + sizeof(struct circular_queue) + sizeof(bfs_accessed)
3495#endif
3496 );
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003497
3498 printk(" per task-struct memory footprint: %lu bytes\n",
3499 sizeof(struct held_lock) * MAX_LOCK_DEPTH);
3500
3501#ifdef CONFIG_DEBUG_LOCKDEP
Johannes Bergc71063c2007-07-19 01:49:02 -07003502 if (lockdep_init_error) {
3503 printk("WARNING: lockdep init error! Arch code didn't call lockdep_init() early enough?\n");
3504 printk("Call stack leading to lockdep invocation was:\n");
3505 print_stack_trace(&lockdep_init_trace, 0);
3506 }
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003507#endif
3508}
3509
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003510static void
3511print_freed_lock_bug(struct task_struct *curr, const void *mem_from,
Arjan van de Ven55794a42006-07-10 04:44:03 -07003512 const void *mem_to, struct held_lock *hlock)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003513{
3514 if (!debug_locks_off())
3515 return;
3516 if (debug_locks_silent)
3517 return;
3518
3519 printk("\n=========================\n");
3520 printk( "[ BUG: held lock freed! ]\n");
3521 printk( "-------------------------\n");
3522 printk("%s/%d is freeing memory %p-%p, with a lock still held there!\n",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07003523 curr->comm, task_pid_nr(curr), mem_from, mem_to-1);
Arjan van de Ven55794a42006-07-10 04:44:03 -07003524 print_lock(hlock);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003525 lockdep_print_held_locks(curr);
3526
3527 printk("\nstack backtrace:\n");
3528 dump_stack();
3529}
3530
Oleg Nesterov54561782007-12-05 15:46:09 +01003531static inline int not_in_range(const void* mem_from, unsigned long mem_len,
3532 const void* lock_from, unsigned long lock_len)
3533{
3534 return lock_from + lock_len <= mem_from ||
3535 mem_from + mem_len <= lock_from;
3536}
3537
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003538/*
3539 * Called when kernel memory is freed (or unmapped), or if a lock
3540 * is destroyed or reinitialized - this code checks whether there is
3541 * any held lock in the memory range of <from> to <to>:
3542 */
3543void debug_check_no_locks_freed(const void *mem_from, unsigned long mem_len)
3544{
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003545 struct task_struct *curr = current;
3546 struct held_lock *hlock;
3547 unsigned long flags;
3548 int i;
3549
3550 if (unlikely(!debug_locks))
3551 return;
3552
3553 local_irq_save(flags);
3554 for (i = 0; i < curr->lockdep_depth; i++) {
3555 hlock = curr->held_locks + i;
3556
Oleg Nesterov54561782007-12-05 15:46:09 +01003557 if (not_in_range(mem_from, mem_len, hlock->instance,
3558 sizeof(*hlock->instance)))
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003559 continue;
3560
Oleg Nesterov54561782007-12-05 15:46:09 +01003561 print_freed_lock_bug(curr, mem_from, mem_from + mem_len, hlock);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003562 break;
3563 }
3564 local_irq_restore(flags);
3565}
Peter Zijlstraed075362006-12-06 20:35:24 -08003566EXPORT_SYMBOL_GPL(debug_check_no_locks_freed);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003567
3568static void print_held_locks_bug(struct task_struct *curr)
3569{
3570 if (!debug_locks_off())
3571 return;
3572 if (debug_locks_silent)
3573 return;
3574
3575 printk("\n=====================================\n");
3576 printk( "[ BUG: lock held at task exit time! ]\n");
3577 printk( "-------------------------------------\n");
3578 printk("%s/%d is exiting with locks still held!\n",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07003579 curr->comm, task_pid_nr(curr));
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003580 lockdep_print_held_locks(curr);
3581
3582 printk("\nstack backtrace:\n");
3583 dump_stack();
3584}
3585
3586void debug_check_no_locks_held(struct task_struct *task)
3587{
3588 if (unlikely(task->lockdep_depth > 0))
3589 print_held_locks_bug(task);
3590}
3591
3592void debug_show_all_locks(void)
3593{
3594 struct task_struct *g, *p;
3595 int count = 10;
3596 int unlock = 1;
3597
Jarek Poplawski9c35dd72007-03-22 00:11:28 -08003598 if (unlikely(!debug_locks)) {
3599 printk("INFO: lockdep is turned off.\n");
3600 return;
3601 }
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003602 printk("\nShowing all locks held in the system:\n");
3603
3604 /*
3605 * Here we try to get the tasklist_lock as hard as possible,
3606 * if not successful after 2 seconds we ignore it (but keep
3607 * trying). This is to enable a debug printout even if a
3608 * tasklist_lock-holding task deadlocks or crashes.
3609 */
3610retry:
3611 if (!read_trylock(&tasklist_lock)) {
3612 if (count == 10)
3613 printk("hm, tasklist_lock locked, retrying... ");
3614 if (count) {
3615 count--;
3616 printk(" #%d", 10-count);
3617 mdelay(200);
3618 goto retry;
3619 }
3620 printk(" ignoring it.\n");
3621 unlock = 0;
qinghuang feng46fec7a2008-10-28 17:24:28 +08003622 } else {
3623 if (count != 10)
3624 printk(KERN_CONT " locked it.\n");
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003625 }
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003626
3627 do_each_thread(g, p) {
Ingo Molnar85684872007-12-05 15:46:09 +01003628 /*
3629 * It's not reliable to print a task's held locks
3630 * if it's not sleeping (or if it's not the current
3631 * task):
3632 */
3633 if (p->state == TASK_RUNNING && p != current)
3634 continue;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003635 if (p->lockdep_depth)
3636 lockdep_print_held_locks(p);
3637 if (!unlock)
3638 if (read_trylock(&tasklist_lock))
3639 unlock = 1;
3640 } while_each_thread(g, p);
3641
3642 printk("\n");
3643 printk("=============================================\n\n");
3644
3645 if (unlock)
3646 read_unlock(&tasklist_lock);
3647}
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003648EXPORT_SYMBOL_GPL(debug_show_all_locks);
3649
Ingo Molnar82a1fcb2008-01-25 21:08:02 +01003650/*
3651 * Careful: only use this function if you are sure that
3652 * the task cannot run in parallel!
3653 */
3654void __debug_show_held_locks(struct task_struct *task)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003655{
Jarek Poplawski9c35dd72007-03-22 00:11:28 -08003656 if (unlikely(!debug_locks)) {
3657 printk("INFO: lockdep is turned off.\n");
3658 return;
3659 }
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003660 lockdep_print_held_locks(task);
3661}
Ingo Molnar82a1fcb2008-01-25 21:08:02 +01003662EXPORT_SYMBOL_GPL(__debug_show_held_locks);
3663
3664void debug_show_held_locks(struct task_struct *task)
3665{
3666 __debug_show_held_locks(task);
3667}
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003668EXPORT_SYMBOL_GPL(debug_show_held_locks);
Peter Zijlstrab351d162007-10-11 22:11:12 +02003669
3670void lockdep_sys_exit(void)
3671{
3672 struct task_struct *curr = current;
3673
3674 if (unlikely(curr->lockdep_depth)) {
3675 if (!debug_locks_off())
3676 return;
3677 printk("\n================================================\n");
3678 printk( "[ BUG: lock held when returning to user space! ]\n");
3679 printk( "------------------------------------------------\n");
3680 printk("%s/%d is leaving the kernel with locks still held!\n",
3681 curr->comm, curr->pid);
3682 lockdep_print_held_locks(curr);
3683 }
3684}