]> bbs.cooldavid.org Git - net-next-2.6.git/blame - kernel/lockdep.c
lockdep: simplify the mark_lock_irq() helpers
[net-next-2.6.git] / kernel / lockdep.c
CommitLineData
fbb9ce95
IM
1/*
2 * kernel/lockdep.c
3 *
4 * Runtime locking correctness validator
5 *
6 * Started by Ingo Molnar:
7 *
4b32d0a4
PZ
8 * Copyright (C) 2006,2007 Red Hat, Inc., Ingo Molnar <mingo@redhat.com>
9 * Copyright (C) 2007 Red Hat, Inc., Peter Zijlstra <pzijlstr@redhat.com>
fbb9ce95
IM
10 *
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 */
a5e25883 28#define DISABLE_BRANCH_PROFILING
fbb9ce95
IM
29#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>
99de055a 41#include <linux/utsname.h>
4b32d0a4 42#include <linux/hash.h>
81d68a96 43#include <linux/ftrace.h>
fbb9ce95
IM
44
45#include <asm/sections.h>
46
47#include "lockdep_internals.h"
48
f20786ff
PZ
49#ifdef CONFIG_PROVE_LOCKING
50int prove_locking = 1;
51module_param(prove_locking, int, 0644);
52#else
53#define prove_locking 0
54#endif
55
56#ifdef CONFIG_LOCK_STAT
57int lock_stat = 1;
58module_param(lock_stat, int, 0644);
59#else
60#define lock_stat 0
61#endif
62
fbb9ce95 63/*
74c383f1
IM
64 * lockdep_lock: protects the lockdep graph, the hashes and the
65 * class/list/hash allocators.
fbb9ce95
IM
66 *
67 * This is one of the rare exceptions where it's justified
68 * to use a raw spinlock - we really dont want the spinlock
74c383f1 69 * code to recurse back into the lockdep code...
fbb9ce95 70 */
74c383f1
IM
71static raw_spinlock_t lockdep_lock = (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED;
72
73static int graph_lock(void)
74{
75 __raw_spin_lock(&lockdep_lock);
76 /*
77 * Make sure that if another CPU detected a bug while
78 * walking the graph we dont change it (while the other
79 * CPU is busy printing out stuff with the graph lock
80 * dropped already)
81 */
82 if (!debug_locks) {
83 __raw_spin_unlock(&lockdep_lock);
84 return 0;
85 }
bb065afb
SR
86 /* prevent any recursions within lockdep from causing deadlocks */
87 current->lockdep_recursion++;
74c383f1
IM
88 return 1;
89}
90
91static inline int graph_unlock(void)
92{
381a2292
JP
93 if (debug_locks && !__raw_spin_is_locked(&lockdep_lock))
94 return DEBUG_LOCKS_WARN_ON(1);
95
bb065afb 96 current->lockdep_recursion--;
74c383f1
IM
97 __raw_spin_unlock(&lockdep_lock);
98 return 0;
99}
100
101/*
102 * Turn lock debugging off and return with 0 if it was off already,
103 * and also release the graph lock:
104 */
105static inline int debug_locks_off_graph_unlock(void)
106{
107 int ret = debug_locks_off();
108
109 __raw_spin_unlock(&lockdep_lock);
110
111 return ret;
112}
fbb9ce95
IM
113
114static int lockdep_initialized;
115
116unsigned long nr_list_entries;
117static struct lock_list list_entries[MAX_LOCKDEP_ENTRIES];
118
fbb9ce95
IM
119/*
120 * All data structures here are protected by the global debug_lock.
121 *
122 * Mutex key structs only get allocated, once during bootup, and never
123 * get freed - this significantly simplifies the debugging code.
124 */
125unsigned long nr_lock_classes;
126static struct lock_class lock_classes[MAX_LOCKDEP_KEYS];
127
f82b217e
DJ
128static inline struct lock_class *hlock_class(struct held_lock *hlock)
129{
130 if (!hlock->class_idx) {
131 DEBUG_LOCKS_WARN_ON(1);
132 return NULL;
133 }
134 return lock_classes + hlock->class_idx - 1;
135}
136
f20786ff
PZ
137#ifdef CONFIG_LOCK_STAT
138static DEFINE_PER_CPU(struct lock_class_stats[MAX_LOCKDEP_KEYS], lock_stats);
139
c7e78cff 140static int lock_point(unsigned long points[], unsigned long ip)
f20786ff
PZ
141{
142 int i;
143
c7e78cff
PZ
144 for (i = 0; i < LOCKSTAT_POINTS; i++) {
145 if (points[i] == 0) {
146 points[i] = ip;
f20786ff
PZ
147 break;
148 }
c7e78cff 149 if (points[i] == ip)
f20786ff
PZ
150 break;
151 }
152
153 return i;
154}
155
156static void lock_time_inc(struct lock_time *lt, s64 time)
157{
158 if (time > lt->max)
159 lt->max = time;
160
161 if (time < lt->min || !lt->min)
162 lt->min = time;
163
164 lt->total += time;
165 lt->nr++;
166}
167
c46261de
PZ
168static inline void lock_time_add(struct lock_time *src, struct lock_time *dst)
169{
170 dst->min += src->min;
171 dst->max += src->max;
172 dst->total += src->total;
173 dst->nr += src->nr;
174}
175
176struct lock_class_stats lock_stats(struct lock_class *class)
177{
178 struct lock_class_stats stats;
179 int cpu, i;
180
181 memset(&stats, 0, sizeof(struct lock_class_stats));
182 for_each_possible_cpu(cpu) {
183 struct lock_class_stats *pcs =
184 &per_cpu(lock_stats, cpu)[class - lock_classes];
185
186 for (i = 0; i < ARRAY_SIZE(stats.contention_point); i++)
187 stats.contention_point[i] += pcs->contention_point[i];
188
c7e78cff
PZ
189 for (i = 0; i < ARRAY_SIZE(stats.contending_point); i++)
190 stats.contending_point[i] += pcs->contending_point[i];
191
c46261de
PZ
192 lock_time_add(&pcs->read_waittime, &stats.read_waittime);
193 lock_time_add(&pcs->write_waittime, &stats.write_waittime);
194
195 lock_time_add(&pcs->read_holdtime, &stats.read_holdtime);
196 lock_time_add(&pcs->write_holdtime, &stats.write_holdtime);
96645678
PZ
197
198 for (i = 0; i < ARRAY_SIZE(stats.bounces); i++)
199 stats.bounces[i] += pcs->bounces[i];
c46261de
PZ
200 }
201
202 return stats;
203}
204
205void clear_lock_stats(struct lock_class *class)
206{
207 int cpu;
208
209 for_each_possible_cpu(cpu) {
210 struct lock_class_stats *cpu_stats =
211 &per_cpu(lock_stats, cpu)[class - lock_classes];
212
213 memset(cpu_stats, 0, sizeof(struct lock_class_stats));
214 }
215 memset(class->contention_point, 0, sizeof(class->contention_point));
c7e78cff 216 memset(class->contending_point, 0, sizeof(class->contending_point));
c46261de
PZ
217}
218
f20786ff
PZ
219static struct lock_class_stats *get_lock_stats(struct lock_class *class)
220{
221 return &get_cpu_var(lock_stats)[class - lock_classes];
222}
223
224static void put_lock_stats(struct lock_class_stats *stats)
225{
226 put_cpu_var(lock_stats);
227}
228
229static void lock_release_holdtime(struct held_lock *hlock)
230{
231 struct lock_class_stats *stats;
232 s64 holdtime;
233
234 if (!lock_stat)
235 return;
236
237 holdtime = sched_clock() - hlock->holdtime_stamp;
238
f82b217e 239 stats = get_lock_stats(hlock_class(hlock));
f20786ff
PZ
240 if (hlock->read)
241 lock_time_inc(&stats->read_holdtime, holdtime);
242 else
243 lock_time_inc(&stats->write_holdtime, holdtime);
244 put_lock_stats(stats);
245}
246#else
247static inline void lock_release_holdtime(struct held_lock *hlock)
248{
249}
250#endif
251
fbb9ce95
IM
252/*
253 * We keep a global list of all lock classes. The list only grows,
254 * never shrinks. The list is only accessed with the lockdep
255 * spinlock lock held.
256 */
257LIST_HEAD(all_lock_classes);
258
259/*
260 * The lockdep classes are in a hash-table as well, for fast lookup:
261 */
262#define CLASSHASH_BITS (MAX_LOCKDEP_KEYS_BITS - 1)
263#define CLASSHASH_SIZE (1UL << CLASSHASH_BITS)
4b32d0a4 264#define __classhashfn(key) hash_long((unsigned long)key, CLASSHASH_BITS)
fbb9ce95
IM
265#define classhashentry(key) (classhash_table + __classhashfn((key)))
266
267static struct list_head classhash_table[CLASSHASH_SIZE];
268
fbb9ce95
IM
269/*
270 * We put the lock dependency chains into a hash-table as well, to cache
271 * their existence:
272 */
273#define CHAINHASH_BITS (MAX_LOCKDEP_CHAINS_BITS-1)
274#define CHAINHASH_SIZE (1UL << CHAINHASH_BITS)
4b32d0a4 275#define __chainhashfn(chain) hash_long(chain, CHAINHASH_BITS)
fbb9ce95
IM
276#define chainhashentry(chain) (chainhash_table + __chainhashfn((chain)))
277
278static struct list_head chainhash_table[CHAINHASH_SIZE];
279
280/*
281 * The hash key of the lock dependency chains is a hash itself too:
282 * it's a hash of all locks taken up to that lock, including that lock.
283 * It's a 64-bit hash, because it's important for the keys to be
284 * unique.
285 */
286#define iterate_chain_key(key1, key2) \
03cbc358
IM
287 (((key1) << MAX_LOCKDEP_KEYS_BITS) ^ \
288 ((key1) >> (64-MAX_LOCKDEP_KEYS_BITS)) ^ \
fbb9ce95
IM
289 (key2))
290
1d09daa5 291void lockdep_off(void)
fbb9ce95
IM
292{
293 current->lockdep_recursion++;
294}
fbb9ce95
IM
295EXPORT_SYMBOL(lockdep_off);
296
1d09daa5 297void lockdep_on(void)
fbb9ce95
IM
298{
299 current->lockdep_recursion--;
300}
fbb9ce95
IM
301EXPORT_SYMBOL(lockdep_on);
302
fbb9ce95
IM
303/*
304 * Debugging switches:
305 */
306
307#define VERBOSE 0
33e94e96 308#define VERY_VERBOSE 0
fbb9ce95
IM
309
310#if VERBOSE
311# define HARDIRQ_VERBOSE 1
312# define SOFTIRQ_VERBOSE 1
cf40bd16 313# define RECLAIM_VERBOSE 1
fbb9ce95
IM
314#else
315# define HARDIRQ_VERBOSE 0
316# define SOFTIRQ_VERBOSE 0
cf40bd16 317# define RECLAIM_VERBOSE 0
fbb9ce95
IM
318#endif
319
cf40bd16 320#if VERBOSE || HARDIRQ_VERBOSE || SOFTIRQ_VERBOSE || RECLAIM_VERBOSE
fbb9ce95
IM
321/*
322 * Quick filtering for interesting events:
323 */
324static int class_filter(struct lock_class *class)
325{
f9829cce
AK
326#if 0
327 /* Example */
fbb9ce95 328 if (class->name_version == 1 &&
f9829cce 329 !strcmp(class->name, "lockname"))
fbb9ce95
IM
330 return 1;
331 if (class->name_version == 1 &&
f9829cce 332 !strcmp(class->name, "&struct->lockfield"))
fbb9ce95 333 return 1;
f9829cce 334#endif
a6640897
IM
335 /* Filter everything else. 1 would be to allow everything else */
336 return 0;
fbb9ce95
IM
337}
338#endif
339
340static int verbose(struct lock_class *class)
341{
342#if VERBOSE
343 return class_filter(class);
344#endif
345 return 0;
346}
347
fbb9ce95
IM
348/*
349 * Stack-trace: tightly packed array of stack backtrace
74c383f1 350 * addresses. Protected by the graph_lock.
fbb9ce95
IM
351 */
352unsigned long nr_stack_trace_entries;
353static unsigned long stack_trace[MAX_STACK_TRACE_ENTRIES];
354
355static int save_trace(struct stack_trace *trace)
356{
357 trace->nr_entries = 0;
358 trace->max_entries = MAX_STACK_TRACE_ENTRIES - nr_stack_trace_entries;
359 trace->entries = stack_trace + nr_stack_trace_entries;
360
5a1b3999 361 trace->skip = 3;
5a1b3999 362
ab1b6f03 363 save_stack_trace(trace);
fbb9ce95
IM
364
365 trace->max_entries = trace->nr_entries;
366
367 nr_stack_trace_entries += trace->nr_entries;
fbb9ce95
IM
368
369 if (nr_stack_trace_entries == MAX_STACK_TRACE_ENTRIES) {
74c383f1
IM
370 if (!debug_locks_off_graph_unlock())
371 return 0;
372
373 printk("BUG: MAX_STACK_TRACE_ENTRIES too low!\n");
374 printk("turning off the locking correctness validator.\n");
375 dump_stack();
376
fbb9ce95
IM
377 return 0;
378 }
379
380 return 1;
381}
382
383unsigned int nr_hardirq_chains;
384unsigned int nr_softirq_chains;
385unsigned int nr_process_chains;
386unsigned int max_lockdep_depth;
387unsigned int max_recursion_depth;
388
419ca3f1
DM
389static unsigned int lockdep_dependency_gen_id;
390
391static bool lockdep_dependency_visit(struct lock_class *source,
392 unsigned int depth)
393{
394 if (!depth)
395 lockdep_dependency_gen_id++;
396 if (source->dep_gen_id == lockdep_dependency_gen_id)
397 return true;
398 source->dep_gen_id = lockdep_dependency_gen_id;
399 return false;
400}
401
fbb9ce95
IM
402#ifdef CONFIG_DEBUG_LOCKDEP
403/*
404 * We cannot printk in early bootup code. Not even early_printk()
405 * might work. So we mark any initialization errors and printk
406 * about it later on, in lockdep_info().
407 */
408static int lockdep_init_error;
c71063c9
JB
409static unsigned long lockdep_init_trace_data[20];
410static struct stack_trace lockdep_init_trace = {
411 .max_entries = ARRAY_SIZE(lockdep_init_trace_data),
412 .entries = lockdep_init_trace_data,
413};
fbb9ce95
IM
414
415/*
416 * Various lockdep statistics:
417 */
418atomic_t chain_lookup_hits;
419atomic_t chain_lookup_misses;
420atomic_t hardirqs_on_events;
421atomic_t hardirqs_off_events;
422atomic_t redundant_hardirqs_on;
423atomic_t redundant_hardirqs_off;
424atomic_t softirqs_on_events;
425atomic_t softirqs_off_events;
426atomic_t redundant_softirqs_on;
427atomic_t redundant_softirqs_off;
428atomic_t nr_unused_locks;
429atomic_t nr_cyclic_checks;
430atomic_t nr_cyclic_check_recursions;
431atomic_t nr_find_usage_forwards_checks;
432atomic_t nr_find_usage_forwards_recursions;
433atomic_t nr_find_usage_backwards_checks;
434atomic_t nr_find_usage_backwards_recursions;
435# define debug_atomic_inc(ptr) atomic_inc(ptr)
436# define debug_atomic_dec(ptr) atomic_dec(ptr)
437# define debug_atomic_read(ptr) atomic_read(ptr)
438#else
439# define debug_atomic_inc(ptr) do { } while (0)
440# define debug_atomic_dec(ptr) do { } while (0)
441# define debug_atomic_read(ptr) 0
442#endif
443
444/*
445 * Locking printouts:
446 */
447
fabe9c42
PZ
448#define __STR(foo) #foo
449#define STR(foo) __STR(foo)
450
451#define __USAGE(__STATE) \
452 [LOCK_USED_IN_##__STATE] = "IN-"STR(__STATE)"-W", \
453 [LOCK_ENABLED_##__STATE] = STR(__STATE)"-ON-W", \
454 [LOCK_USED_IN_##__STATE##_READ] = "IN-"STR(__STATE)"-R", \
455 [LOCK_ENABLED_##__STATE##_READ] = STR(__STATE)"-ON-R",
456
fbb9ce95
IM
457static const char *usage_str[] =
458{
fabe9c42
PZ
459#define LOCKDEP_STATE(__STATE) __USAGE(__STATE)
460#include "lockdep_states.h"
461#undef LOCKDEP_STATE
462 [LOCK_USED] = "INITIAL USE",
fbb9ce95
IM
463};
464
465const char * __get_key_name(struct lockdep_subclass_key *key, char *str)
466{
ffb45122 467 return kallsyms_lookup((unsigned long)key, NULL, NULL, NULL, str);
fbb9ce95
IM
468}
469
470void
cf40bd16
NP
471get_usage_chars(struct lock_class *class, char *c1, char *c2, char *c3,
472 char *c4, char *c5, char *c6)
fbb9ce95 473{
cf40bd16 474 *c1 = '.', *c2 = '.', *c3 = '.', *c4 = '.', *c5 = '.', *c6 = '.';
fbb9ce95
IM
475
476 if (class->usage_mask & LOCKF_USED_IN_HARDIRQ)
477 *c1 = '+';
478 else
4fc95e86 479 if (class->usage_mask & LOCKF_ENABLED_HARDIRQ)
fbb9ce95
IM
480 *c1 = '-';
481
482 if (class->usage_mask & LOCKF_USED_IN_SOFTIRQ)
483 *c2 = '+';
484 else
4fc95e86 485 if (class->usage_mask & LOCKF_ENABLED_SOFTIRQ)
fbb9ce95
IM
486 *c2 = '-';
487
4fc95e86 488 if (class->usage_mask & LOCKF_ENABLED_HARDIRQ_READ)
fbb9ce95
IM
489 *c3 = '-';
490 if (class->usage_mask & LOCKF_USED_IN_HARDIRQ_READ) {
491 *c3 = '+';
4fc95e86 492 if (class->usage_mask & LOCKF_ENABLED_HARDIRQ_READ)
fbb9ce95
IM
493 *c3 = '?';
494 }
495
4fc95e86 496 if (class->usage_mask & LOCKF_ENABLED_SOFTIRQ_READ)
fbb9ce95
IM
497 *c4 = '-';
498 if (class->usage_mask & LOCKF_USED_IN_SOFTIRQ_READ) {
499 *c4 = '+';
4fc95e86 500 if (class->usage_mask & LOCKF_ENABLED_SOFTIRQ_READ)
fbb9ce95
IM
501 *c4 = '?';
502 }
cf40bd16
NP
503
504 if (class->usage_mask & LOCKF_USED_IN_RECLAIM_FS)
505 *c5 = '+';
506 else
a652d708 507 if (class->usage_mask & LOCKF_ENABLED_RECLAIM_FS)
cf40bd16
NP
508 *c5 = '-';
509
a652d708 510 if (class->usage_mask & LOCKF_ENABLED_RECLAIM_FS_READ)
cf40bd16
NP
511 *c6 = '-';
512 if (class->usage_mask & LOCKF_USED_IN_RECLAIM_FS_READ) {
513 *c6 = '+';
a652d708 514 if (class->usage_mask & LOCKF_ENABLED_RECLAIM_FS_READ)
cf40bd16
NP
515 *c6 = '?';
516 }
517
fbb9ce95
IM
518}
519
520static void print_lock_name(struct lock_class *class)
521{
cf40bd16 522 char str[KSYM_NAME_LEN], c1, c2, c3, c4, c5, c6;
fbb9ce95
IM
523 const char *name;
524
cf40bd16 525 get_usage_chars(class, &c1, &c2, &c3, &c4, &c5, &c6);
fbb9ce95
IM
526
527 name = class->name;
528 if (!name) {
529 name = __get_key_name(class->key, str);
530 printk(" (%s", name);
531 } else {
532 printk(" (%s", name);
533 if (class->name_version > 1)
534 printk("#%d", class->name_version);
535 if (class->subclass)
536 printk("/%d", class->subclass);
537 }
cf40bd16 538 printk("){%c%c%c%c%c%c}", c1, c2, c3, c4, c5, c6);
fbb9ce95
IM
539}
540
541static void print_lockdep_cache(struct lockdep_map *lock)
542{
543 const char *name;
9281acea 544 char str[KSYM_NAME_LEN];
fbb9ce95
IM
545
546 name = lock->name;
547 if (!name)
548 name = __get_key_name(lock->key->subkeys, str);
549
550 printk("%s", name);
551}
552
553static void print_lock(struct held_lock *hlock)
554{
f82b217e 555 print_lock_name(hlock_class(hlock));
fbb9ce95
IM
556 printk(", at: ");
557 print_ip_sym(hlock->acquire_ip);
558}
559
560static void lockdep_print_held_locks(struct task_struct *curr)
561{
562 int i, depth = curr->lockdep_depth;
563
564 if (!depth) {
ba25f9dc 565 printk("no locks held by %s/%d.\n", curr->comm, task_pid_nr(curr));
fbb9ce95
IM
566 return;
567 }
568 printk("%d lock%s held by %s/%d:\n",
ba25f9dc 569 depth, depth > 1 ? "s" : "", curr->comm, task_pid_nr(curr));
fbb9ce95
IM
570
571 for (i = 0; i < depth; i++) {
572 printk(" #%d: ", i);
573 print_lock(curr->held_locks + i);
574 }
575}
fbb9ce95
IM
576
577static void print_lock_class_header(struct lock_class *class, int depth)
578{
579 int bit;
580
f9829cce 581 printk("%*s->", depth, "");
fbb9ce95
IM
582 print_lock_name(class);
583 printk(" ops: %lu", class->ops);
584 printk(" {\n");
585
586 for (bit = 0; bit < LOCK_USAGE_STATES; bit++) {
587 if (class->usage_mask & (1 << bit)) {
588 int len = depth;
589
f9829cce 590 len += printk("%*s %s", depth, "", usage_str[bit]);
fbb9ce95
IM
591 len += printk(" at:\n");
592 print_stack_trace(class->usage_traces + bit, len);
593 }
594 }
f9829cce 595 printk("%*s }\n", depth, "");
fbb9ce95 596
f9829cce 597 printk("%*s ... key at: ",depth,"");
fbb9ce95
IM
598 print_ip_sym((unsigned long)class->key);
599}
600
601/*
602 * printk all lock dependencies starting at <entry>:
603 */
7807fafa
IM
604static void __used
605print_lock_dependencies(struct lock_class *class, int depth)
fbb9ce95
IM
606{
607 struct lock_list *entry;
608
419ca3f1
DM
609 if (lockdep_dependency_visit(class, depth))
610 return;
611
fbb9ce95
IM
612 if (DEBUG_LOCKS_WARN_ON(depth >= 20))
613 return;
614
615 print_lock_class_header(class, depth);
616
617 list_for_each_entry(entry, &class->locks_after, entry) {
b23984d0
JP
618 if (DEBUG_LOCKS_WARN_ON(!entry->class))
619 return;
620
fbb9ce95
IM
621 print_lock_dependencies(entry->class, depth + 1);
622
f9829cce 623 printk("%*s ... acquired at:\n",depth,"");
fbb9ce95
IM
624 print_stack_trace(&entry->trace, 2);
625 printk("\n");
626 }
627}
628
8e18257d
PZ
629static void print_kernel_version(void)
630{
631 printk("%s %.*s\n", init_utsname()->release,
632 (int)strcspn(init_utsname()->version, " "),
633 init_utsname()->version);
634}
635
636static int very_verbose(struct lock_class *class)
637{
638#if VERY_VERBOSE
639 return class_filter(class);
640#endif
641 return 0;
642}
643
fbb9ce95 644/*
8e18257d 645 * Is this the address of a static object:
fbb9ce95 646 */
8e18257d 647static int static_obj(void *obj)
fbb9ce95 648{
8e18257d
PZ
649 unsigned long start = (unsigned long) &_stext,
650 end = (unsigned long) &_end,
651 addr = (unsigned long) obj;
652#ifdef CONFIG_SMP
653 int i;
654#endif
655
fbb9ce95 656 /*
8e18257d 657 * static variable?
fbb9ce95 658 */
8e18257d
PZ
659 if ((addr >= start) && (addr < end))
660 return 1;
fbb9ce95 661
8e18257d 662#ifdef CONFIG_SMP
fbb9ce95 663 /*
8e18257d 664 * percpu var?
fbb9ce95 665 */
8e18257d
PZ
666 for_each_possible_cpu(i) {
667 start = (unsigned long) &__per_cpu_start + per_cpu_offset(i);
668 end = (unsigned long) &__per_cpu_start + PERCPU_ENOUGH_ROOM
669 + per_cpu_offset(i);
fbb9ce95 670
8e18257d
PZ
671 if ((addr >= start) && (addr < end))
672 return 1;
673 }
ca58abcb 674#endif
fbb9ce95 675
8e18257d
PZ
676 /*
677 * module var?
678 */
679 return is_module_address(addr);
99de055a
DJ
680}
681
fbb9ce95 682/*
8e18257d
PZ
683 * To make lock name printouts unique, we calculate a unique
684 * class->name_version generation counter:
fbb9ce95 685 */
8e18257d 686static int count_matching_names(struct lock_class *new_class)
fbb9ce95 687{
8e18257d
PZ
688 struct lock_class *class;
689 int count = 0;
fbb9ce95 690
8e18257d 691 if (!new_class->name)
fbb9ce95
IM
692 return 0;
693
8e18257d
PZ
694 list_for_each_entry(class, &all_lock_classes, lock_entry) {
695 if (new_class->key - new_class->subclass == class->key)
696 return class->name_version;
697 if (class->name && !strcmp(class->name, new_class->name))
698 count = max(count, class->name_version);
699 }
fbb9ce95 700
8e18257d 701 return count + 1;
fbb9ce95
IM
702}
703
8e18257d
PZ
704/*
705 * Register a lock's class in the hash-table, if the class is not present
706 * yet. Otherwise we look it up. We cache the result in the lock object
707 * itself, so actual lookup of the hash should be once per lock object.
708 */
709static inline struct lock_class *
710look_up_lock_class(struct lockdep_map *lock, unsigned int subclass)
fbb9ce95 711{
8e18257d
PZ
712 struct lockdep_subclass_key *key;
713 struct list_head *hash_head;
714 struct lock_class *class;
fbb9ce95 715
8e18257d
PZ
716#ifdef CONFIG_DEBUG_LOCKDEP
717 /*
718 * If the architecture calls into lockdep before initializing
719 * the hashes then we'll warn about it later. (we cannot printk
720 * right now)
721 */
722 if (unlikely(!lockdep_initialized)) {
723 lockdep_init();
724 lockdep_init_error = 1;
c71063c9 725 save_stack_trace(&lockdep_init_trace);
8e18257d
PZ
726 }
727#endif
fbb9ce95 728
8e18257d
PZ
729 /*
730 * Static locks do not have their class-keys yet - for them the key
731 * is the lock object itself:
732 */
733 if (unlikely(!lock->key))
734 lock->key = (void *)lock;
fbb9ce95 735
8e18257d
PZ
736 /*
737 * NOTE: the class-key must be unique. For dynamic locks, a static
738 * lock_class_key variable is passed in through the mutex_init()
739 * (or spin_lock_init()) call - which acts as the key. For static
740 * locks we use the lock object itself as the key.
741 */
4b32d0a4
PZ
742 BUILD_BUG_ON(sizeof(struct lock_class_key) >
743 sizeof(struct lockdep_map));
fbb9ce95 744
8e18257d 745 key = lock->key->subkeys + subclass;
ca268c69 746
8e18257d 747 hash_head = classhashentry(key);
74c383f1 748
8e18257d
PZ
749 /*
750 * We can walk the hash lockfree, because the hash only
751 * grows, and we are careful when adding entries to the end:
752 */
4b32d0a4
PZ
753 list_for_each_entry(class, hash_head, hash_entry) {
754 if (class->key == key) {
755 WARN_ON_ONCE(class->name != lock->name);
8e18257d 756 return class;
4b32d0a4
PZ
757 }
758 }
fbb9ce95 759
8e18257d 760 return NULL;
fbb9ce95
IM
761}
762
763/*
8e18257d
PZ
764 * Register a lock's class in the hash-table, if the class is not present
765 * yet. Otherwise we look it up. We cache the result in the lock object
766 * itself, so actual lookup of the hash should be once per lock object.
fbb9ce95 767 */
8e18257d
PZ
768static inline struct lock_class *
769register_lock_class(struct lockdep_map *lock, unsigned int subclass, int force)
fbb9ce95 770{
8e18257d
PZ
771 struct lockdep_subclass_key *key;
772 struct list_head *hash_head;
773 struct lock_class *class;
774 unsigned long flags;
775
776 class = look_up_lock_class(lock, subclass);
777 if (likely(class))
778 return class;
779
780 /*
781 * Debug-check: all keys must be persistent!
782 */
783 if (!static_obj(lock->key)) {
784 debug_locks_off();
785 printk("INFO: trying to register non-static key.\n");
786 printk("the code is fine but needs lockdep annotation.\n");
787 printk("turning off the locking correctness validator.\n");
788 dump_stack();
789
790 return NULL;
791 }
792
793 key = lock->key->subkeys + subclass;
794 hash_head = classhashentry(key);
795
796 raw_local_irq_save(flags);
797 if (!graph_lock()) {
798 raw_local_irq_restore(flags);
799 return NULL;
800 }
801 /*
802 * We have to do the hash-walk again, to avoid races
803 * with another CPU:
804 */
805 list_for_each_entry(class, hash_head, hash_entry)
806 if (class->key == key)
807 goto out_unlock_set;
808 /*
809 * Allocate a new key from the static array, and add it to
810 * the hash:
811 */
812 if (nr_lock_classes >= MAX_LOCKDEP_KEYS) {
813 if (!debug_locks_off_graph_unlock()) {
814 raw_local_irq_restore(flags);
815 return NULL;
816 }
817 raw_local_irq_restore(flags);
818
819 printk("BUG: MAX_LOCKDEP_KEYS too low!\n");
820 printk("turning off the locking correctness validator.\n");
821 return NULL;
822 }
823 class = lock_classes + nr_lock_classes++;
824 debug_atomic_inc(&nr_unused_locks);
825 class->key = key;
826 class->name = lock->name;
827 class->subclass = subclass;
828 INIT_LIST_HEAD(&class->lock_entry);
829 INIT_LIST_HEAD(&class->locks_before);
830 INIT_LIST_HEAD(&class->locks_after);
831 class->name_version = count_matching_names(class);
832 /*
833 * We use RCU's safe list-add method to make
834 * parallel walking of the hash-list safe:
835 */
836 list_add_tail_rcu(&class->hash_entry, hash_head);
1481197b
DF
837 /*
838 * Add it to the global list of classes:
839 */
840 list_add_tail_rcu(&class->lock_entry, &all_lock_classes);
8e18257d
PZ
841
842 if (verbose(class)) {
843 graph_unlock();
844 raw_local_irq_restore(flags);
845
846 printk("\nnew class %p: %s", class->key, class->name);
847 if (class->name_version > 1)
848 printk("#%d", class->name_version);
849 printk("\n");
850 dump_stack();
851
852 raw_local_irq_save(flags);
853 if (!graph_lock()) {
854 raw_local_irq_restore(flags);
855 return NULL;
856 }
857 }
858out_unlock_set:
859 graph_unlock();
860 raw_local_irq_restore(flags);
861
862 if (!subclass || force)
863 lock->class_cache = class;
864
865 if (DEBUG_LOCKS_WARN_ON(class->subclass != subclass))
866 return NULL;
867
868 return class;
869}
870
871#ifdef CONFIG_PROVE_LOCKING
872/*
873 * Allocate a lockdep entry. (assumes the graph_lock held, returns
874 * with NULL on failure)
875 */
876static struct lock_list *alloc_list_entry(void)
877{
878 if (nr_list_entries >= MAX_LOCKDEP_ENTRIES) {
879 if (!debug_locks_off_graph_unlock())
880 return NULL;
881
882 printk("BUG: MAX_LOCKDEP_ENTRIES too low!\n");
883 printk("turning off the locking correctness validator.\n");
884 return NULL;
885 }
886 return list_entries + nr_list_entries++;
887}
888
889/*
890 * Add a new dependency to the head of the list:
891 */
892static int add_lock_to_list(struct lock_class *class, struct lock_class *this,
893 struct list_head *head, unsigned long ip, int distance)
894{
895 struct lock_list *entry;
896 /*
897 * Lock not present yet - get a new dependency struct and
898 * add it to the list:
899 */
900 entry = alloc_list_entry();
901 if (!entry)
902 return 0;
903
8e18257d
PZ
904 if (!save_trace(&entry->trace))
905 return 0;
906
74870172
ZY
907 entry->class = this;
908 entry->distance = distance;
8e18257d
PZ
909 /*
910 * Since we never remove from the dependency list, the list can
911 * be walked lockless by other CPUs, it's only allocation
912 * that must be protected by the spinlock. But this also means
913 * we must make new entries visible only once writes to the
914 * entry become visible - hence the RCU op:
915 */
916 list_add_tail_rcu(&entry->entry, head);
917
918 return 1;
919}
920
921/*
922 * Recursive, forwards-direction lock-dependency checking, used for
923 * both noncyclic checking and for hardirq-unsafe/softirq-unsafe
924 * checking.
925 *
926 * (to keep the stackframe of the recursive functions small we
927 * use these global variables, and we also mark various helper
928 * functions as noinline.)
929 */
930static struct held_lock *check_source, *check_target;
931
932/*
933 * Print a dependency chain entry (this is only done when a deadlock
934 * has been detected):
935 */
936static noinline int
937print_circular_bug_entry(struct lock_list *target, unsigned int depth)
938{
939 if (debug_locks_silent)
940 return 0;
941 printk("\n-> #%u", depth);
942 print_lock_name(target->class);
943 printk(":\n");
944 print_stack_trace(&target->trace, 6);
945
946 return 0;
947}
948
949/*
950 * When a circular dependency is detected, print the
951 * header first:
952 */
953static noinline int
954print_circular_bug_header(struct lock_list *entry, unsigned int depth)
955{
956 struct task_struct *curr = current;
957
958 if (!debug_locks_off_graph_unlock() || debug_locks_silent)
959 return 0;
960
961 printk("\n=======================================================\n");
962 printk( "[ INFO: possible circular locking dependency detected ]\n");
963 print_kernel_version();
964 printk( "-------------------------------------------------------\n");
965 printk("%s/%d is trying to acquire lock:\n",
ba25f9dc 966 curr->comm, task_pid_nr(curr));
8e18257d
PZ
967 print_lock(check_source);
968 printk("\nbut task is already holding lock:\n");
969 print_lock(check_target);
970 printk("\nwhich lock already depends on the new lock.\n\n");
971 printk("\nthe existing dependency chain (in reverse order) is:\n");
972
973 print_circular_bug_entry(entry, depth);
974
975 return 0;
976}
977
978static noinline int print_circular_bug_tail(void)
979{
980 struct task_struct *curr = current;
981 struct lock_list this;
982
983 if (debug_locks_silent)
984 return 0;
985
f82b217e 986 this.class = hlock_class(check_source);
8e18257d
PZ
987 if (!save_trace(&this.trace))
988 return 0;
989
990 print_circular_bug_entry(&this, 0);
991
992 printk("\nother info that might help us debug this:\n\n");
993 lockdep_print_held_locks(curr);
994
995 printk("\nstack backtrace:\n");
996 dump_stack();
997
998 return 0;
999}
1000
1001#define RECURSION_LIMIT 40
1002
1003static int noinline print_infinite_recursion_bug(void)
1004{
1005 if (!debug_locks_off_graph_unlock())
1006 return 0;
1007
1008 WARN_ON(1);
1009
1010 return 0;
1011}
1012
419ca3f1
DM
1013unsigned long __lockdep_count_forward_deps(struct lock_class *class,
1014 unsigned int depth)
1015{
1016 struct lock_list *entry;
1017 unsigned long ret = 1;
1018
1019 if (lockdep_dependency_visit(class, depth))
1020 return 0;
1021
1022 /*
1023 * Recurse this class's dependency list:
1024 */
1025 list_for_each_entry(entry, &class->locks_after, entry)
1026 ret += __lockdep_count_forward_deps(entry->class, depth + 1);
1027
1028 return ret;
1029}
1030
1031unsigned long lockdep_count_forward_deps(struct lock_class *class)
1032{
1033 unsigned long ret, flags;
1034
1035 local_irq_save(flags);
1036 __raw_spin_lock(&lockdep_lock);
1037 ret = __lockdep_count_forward_deps(class, 0);
1038 __raw_spin_unlock(&lockdep_lock);
1039 local_irq_restore(flags);
1040
1041 return ret;
1042}
1043
1044unsigned long __lockdep_count_backward_deps(struct lock_class *class,
1045 unsigned int depth)
1046{
1047 struct lock_list *entry;
1048 unsigned long ret = 1;
1049
1050 if (lockdep_dependency_visit(class, depth))
1051 return 0;
1052 /*
1053 * Recurse this class's dependency list:
1054 */
1055 list_for_each_entry(entry, &class->locks_before, entry)
1056 ret += __lockdep_count_backward_deps(entry->class, depth + 1);
1057
1058 return ret;
1059}
1060
1061unsigned long lockdep_count_backward_deps(struct lock_class *class)
1062{
1063 unsigned long ret, flags;
1064
1065 local_irq_save(flags);
1066 __raw_spin_lock(&lockdep_lock);
1067 ret = __lockdep_count_backward_deps(class, 0);
1068 __raw_spin_unlock(&lockdep_lock);
1069 local_irq_restore(flags);
1070
1071 return ret;
1072}
1073
8e18257d
PZ
1074/*
1075 * Prove that the dependency graph starting at <entry> can not
1076 * lead to <target>. Print an error and return 0 if it does.
1077 */
1078static noinline int
1079check_noncircular(struct lock_class *source, unsigned int depth)
1080{
1081 struct lock_list *entry;
1082
419ca3f1
DM
1083 if (lockdep_dependency_visit(source, depth))
1084 return 1;
1085
8e18257d
PZ
1086 debug_atomic_inc(&nr_cyclic_check_recursions);
1087 if (depth > max_recursion_depth)
fbb9ce95 1088 max_recursion_depth = depth;
ca268c69 1089 if (depth >= RECURSION_LIMIT)
fbb9ce95
IM
1090 return print_infinite_recursion_bug();
1091 /*
1092 * Check this lock's dependency list:
1093 */
1094 list_for_each_entry(entry, &source->locks_after, entry) {
f82b217e 1095 if (entry->class == hlock_class(check_target))
fbb9ce95
IM
1096 return print_circular_bug_header(entry, depth+1);
1097 debug_atomic_inc(&nr_cyclic_checks);
1098 if (!check_noncircular(entry->class, depth+1))
1099 return print_circular_bug_entry(entry, depth+1);
1100 }
1101 return 1;
1102}
1103
81d68a96 1104#if defined(CONFIG_TRACE_IRQFLAGS) && defined(CONFIG_PROVE_LOCKING)
fbb9ce95
IM
1105/*
1106 * Forwards and backwards subgraph searching, for the purposes of
1107 * proving that two subgraphs can be connected by a new dependency
1108 * without creating any illegal irq-safe -> irq-unsafe lock dependency.
1109 */
1110static enum lock_usage_bit find_usage_bit;
1111static struct lock_class *forwards_match, *backwards_match;
1112
1113/*
1114 * Find a node in the forwards-direction dependency sub-graph starting
1115 * at <source> that matches <find_usage_bit>.
1116 *
1117 * Return 2 if such a node exists in the subgraph, and put that node
1118 * into <forwards_match>.
1119 *
1120 * Return 1 otherwise and keep <forwards_match> unchanged.
1121 * Return 0 on error.
1122 */
1123static noinline int
1124find_usage_forwards(struct lock_class *source, unsigned int depth)
1125{
1126 struct lock_list *entry;
1127 int ret;
1128
419ca3f1
DM
1129 if (lockdep_dependency_visit(source, depth))
1130 return 1;
1131
fbb9ce95
IM
1132 if (depth > max_recursion_depth)
1133 max_recursion_depth = depth;
ca268c69 1134 if (depth >= RECURSION_LIMIT)
fbb9ce95
IM
1135 return print_infinite_recursion_bug();
1136
1137 debug_atomic_inc(&nr_find_usage_forwards_checks);
1138 if (source->usage_mask & (1 << find_usage_bit)) {
1139 forwards_match = source;
1140 return 2;
1141 }
1142
1143 /*
1144 * Check this lock's dependency list:
1145 */
1146 list_for_each_entry(entry, &source->locks_after, entry) {
1147 debug_atomic_inc(&nr_find_usage_forwards_recursions);
1148 ret = find_usage_forwards(entry->class, depth+1);
1149 if (ret == 2 || ret == 0)
1150 return ret;
1151 }
1152 return 1;
1153}
1154
1155/*
1156 * Find a node in the backwards-direction dependency sub-graph starting
1157 * at <source> that matches <find_usage_bit>.
1158 *
1159 * Return 2 if such a node exists in the subgraph, and put that node
1160 * into <backwards_match>.
1161 *
1162 * Return 1 otherwise and keep <backwards_match> unchanged.
1163 * Return 0 on error.
1164 */
1d09daa5 1165static noinline int
fbb9ce95
IM
1166find_usage_backwards(struct lock_class *source, unsigned int depth)
1167{
1168 struct lock_list *entry;
1169 int ret;
1170
419ca3f1
DM
1171 if (lockdep_dependency_visit(source, depth))
1172 return 1;
1173
381a2292
JP
1174 if (!__raw_spin_is_locked(&lockdep_lock))
1175 return DEBUG_LOCKS_WARN_ON(1);
1176
fbb9ce95
IM
1177 if (depth > max_recursion_depth)
1178 max_recursion_depth = depth;
ca268c69 1179 if (depth >= RECURSION_LIMIT)
fbb9ce95
IM
1180 return print_infinite_recursion_bug();
1181
1182 debug_atomic_inc(&nr_find_usage_backwards_checks);
1183 if (source->usage_mask & (1 << find_usage_bit)) {
1184 backwards_match = source;
1185 return 2;
1186 }
1187
f82b217e
DJ
1188 if (!source && debug_locks_off_graph_unlock()) {
1189 WARN_ON(1);
1190 return 0;
1191 }
1192
fbb9ce95
IM
1193 /*
1194 * Check this lock's dependency list:
1195 */
1196 list_for_each_entry(entry, &source->locks_before, entry) {
1197 debug_atomic_inc(&nr_find_usage_backwards_recursions);
1198 ret = find_usage_backwards(entry->class, depth+1);
1199 if (ret == 2 || ret == 0)
1200 return ret;
1201 }
1202 return 1;
1203}
1204
1205static int
1206print_bad_irq_dependency(struct task_struct *curr,
1207 struct held_lock *prev,
1208 struct held_lock *next,
1209 enum lock_usage_bit bit1,
1210 enum lock_usage_bit bit2,
1211 const char *irqclass)
1212{
74c383f1 1213 if (!debug_locks_off_graph_unlock() || debug_locks_silent)
fbb9ce95
IM
1214 return 0;
1215
1216 printk("\n======================================================\n");
1217 printk( "[ INFO: %s-safe -> %s-unsafe lock order detected ]\n",
1218 irqclass, irqclass);
99de055a 1219 print_kernel_version();
fbb9ce95
IM
1220 printk( "------------------------------------------------------\n");
1221 printk("%s/%d [HC%u[%lu]:SC%u[%lu]:HE%u:SE%u] is trying to acquire:\n",
ba25f9dc 1222 curr->comm, task_pid_nr(curr),
fbb9ce95
IM
1223 curr->hardirq_context, hardirq_count() >> HARDIRQ_SHIFT,
1224 curr->softirq_context, softirq_count() >> SOFTIRQ_SHIFT,
1225 curr->hardirqs_enabled,
1226 curr->softirqs_enabled);
1227 print_lock(next);
1228
1229 printk("\nand this task is already holding:\n");
1230 print_lock(prev);
1231 printk("which would create a new lock dependency:\n");
f82b217e 1232 print_lock_name(hlock_class(prev));
fbb9ce95 1233 printk(" ->");
f82b217e 1234 print_lock_name(hlock_class(next));
fbb9ce95
IM
1235 printk("\n");
1236
1237 printk("\nbut this new dependency connects a %s-irq-safe lock:\n",
1238 irqclass);
1239 print_lock_name(backwards_match);
1240 printk("\n... which became %s-irq-safe at:\n", irqclass);
1241
1242 print_stack_trace(backwards_match->usage_traces + bit1, 1);
1243
1244 printk("\nto a %s-irq-unsafe lock:\n", irqclass);
1245 print_lock_name(forwards_match);
1246 printk("\n... which became %s-irq-unsafe at:\n", irqclass);
1247 printk("...");
1248
1249 print_stack_trace(forwards_match->usage_traces + bit2, 1);
1250
1251 printk("\nother info that might help us debug this:\n\n");
1252 lockdep_print_held_locks(curr);
1253
1254 printk("\nthe %s-irq-safe lock's dependencies:\n", irqclass);
1255 print_lock_dependencies(backwards_match, 0);
1256
1257 printk("\nthe %s-irq-unsafe lock's dependencies:\n", irqclass);
1258 print_lock_dependencies(forwards_match, 0);
1259
1260 printk("\nstack backtrace:\n");
1261 dump_stack();
1262
1263 return 0;
1264}
1265
1266static int
1267check_usage(struct task_struct *curr, struct held_lock *prev,
1268 struct held_lock *next, enum lock_usage_bit bit_backwards,
1269 enum lock_usage_bit bit_forwards, const char *irqclass)
1270{
1271 int ret;
1272
1273 find_usage_bit = bit_backwards;
1274 /* fills in <backwards_match> */
f82b217e 1275 ret = find_usage_backwards(hlock_class(prev), 0);
fbb9ce95
IM
1276 if (!ret || ret == 1)
1277 return ret;
1278
1279 find_usage_bit = bit_forwards;
f82b217e 1280 ret = find_usage_forwards(hlock_class(next), 0);
fbb9ce95
IM
1281 if (!ret || ret == 1)
1282 return ret;
1283 /* ret == 2 */
1284 return print_bad_irq_dependency(curr, prev, next,
1285 bit_backwards, bit_forwards, irqclass);
1286}
1287
8e18257d
PZ
1288static int
1289check_prev_add_irq(struct task_struct *curr, struct held_lock *prev,
1290 struct held_lock *next)
1291{
1292 /*
1293 * Prove that the new dependency does not connect a hardirq-safe
1294 * lock with a hardirq-unsafe lock - to achieve this we search
1295 * the backwards-subgraph starting at <prev>, and the
1296 * forwards-subgraph starting at <next>:
1297 */
1298 if (!check_usage(curr, prev, next, LOCK_USED_IN_HARDIRQ,
4fc95e86 1299 LOCK_ENABLED_HARDIRQ, "hard"))
8e18257d
PZ
1300 return 0;
1301
1302 /*
1303 * Prove that the new dependency does not connect a hardirq-safe-read
1304 * lock with a hardirq-unsafe lock - to achieve this we search
1305 * the backwards-subgraph starting at <prev>, and the
1306 * forwards-subgraph starting at <next>:
1307 */
1308 if (!check_usage(curr, prev, next, LOCK_USED_IN_HARDIRQ_READ,
4fc95e86 1309 LOCK_ENABLED_HARDIRQ, "hard-read"))
8e18257d
PZ
1310 return 0;
1311
1312 /*
1313 * Prove that the new dependency does not connect a softirq-safe
1314 * lock with a softirq-unsafe lock - to achieve this we search
1315 * the backwards-subgraph starting at <prev>, and the
1316 * forwards-subgraph starting at <next>:
1317 */
1318 if (!check_usage(curr, prev, next, LOCK_USED_IN_SOFTIRQ,
4fc95e86 1319 LOCK_ENABLED_SOFTIRQ, "soft"))
8e18257d
PZ
1320 return 0;
1321 /*
1322 * Prove that the new dependency does not connect a softirq-safe-read
1323 * lock with a softirq-unsafe lock - to achieve this we search
1324 * the backwards-subgraph starting at <prev>, and the
1325 * forwards-subgraph starting at <next>:
1326 */
1327 if (!check_usage(curr, prev, next, LOCK_USED_IN_SOFTIRQ_READ,
4fc95e86 1328 LOCK_ENABLED_SOFTIRQ, "soft"))
8e18257d
PZ
1329 return 0;
1330
cf40bd16
NP
1331 /*
1332 * Prove that the new dependency does not connect a reclaim-fs-safe
1333 * lock with a reclaim-fs-unsafe lock - to achieve this we search
1334 * the backwards-subgraph starting at <prev>, and the
1335 * forwards-subgraph starting at <next>:
1336 */
1337 if (!check_usage(curr, prev, next, LOCK_USED_IN_RECLAIM_FS,
a652d708 1338 LOCK_ENABLED_RECLAIM_FS, "reclaim-fs"))
cf40bd16
NP
1339 return 0;
1340
1341 /*
1342 * Prove that the new dependency does not connect a reclaim-fs-safe-read
1343 * lock with a reclaim-fs-unsafe lock - to achieve this we search
1344 * the backwards-subgraph starting at <prev>, and the
1345 * forwards-subgraph starting at <next>:
1346 */
1347 if (!check_usage(curr, prev, next, LOCK_USED_IN_RECLAIM_FS_READ,
a652d708 1348 LOCK_ENABLED_RECLAIM_FS, "reclaim-fs-read"))
cf40bd16
NP
1349 return 0;
1350
8e18257d
PZ
1351 return 1;
1352}
1353
1354static void inc_chains(void)
1355{
1356 if (current->hardirq_context)
1357 nr_hardirq_chains++;
1358 else {
1359 if (current->softirq_context)
1360 nr_softirq_chains++;
1361 else
1362 nr_process_chains++;
1363 }
1364}
1365
1366#else
1367
1368static inline int
1369check_prev_add_irq(struct task_struct *curr, struct held_lock *prev,
1370 struct held_lock *next)
1371{
1372 return 1;
1373}
1374
1375static inline void inc_chains(void)
1376{
1377 nr_process_chains++;
1378}
1379
fbb9ce95
IM
1380#endif
1381
1382static int
1383print_deadlock_bug(struct task_struct *curr, struct held_lock *prev,
1384 struct held_lock *next)
1385{
74c383f1 1386 if (!debug_locks_off_graph_unlock() || debug_locks_silent)
fbb9ce95
IM
1387 return 0;
1388
1389 printk("\n=============================================\n");
1390 printk( "[ INFO: possible recursive locking detected ]\n");
99de055a 1391 print_kernel_version();
fbb9ce95
IM
1392 printk( "---------------------------------------------\n");
1393 printk("%s/%d is trying to acquire lock:\n",
ba25f9dc 1394 curr->comm, task_pid_nr(curr));
fbb9ce95
IM
1395 print_lock(next);
1396 printk("\nbut task is already holding lock:\n");
1397 print_lock(prev);
1398
1399 printk("\nother info that might help us debug this:\n");
1400 lockdep_print_held_locks(curr);
1401
1402 printk("\nstack backtrace:\n");
1403 dump_stack();
1404
1405 return 0;
1406}
1407
1408/*
1409 * Check whether we are holding such a class already.
1410 *
1411 * (Note that this has to be done separately, because the graph cannot
1412 * detect such classes of deadlocks.)
1413 *
1414 * Returns: 0 on deadlock detected, 1 on OK, 2 on recursive read
1415 */
1416static int
1417check_deadlock(struct task_struct *curr, struct held_lock *next,
1418 struct lockdep_map *next_instance, int read)
1419{
1420 struct held_lock *prev;
7531e2f3 1421 struct held_lock *nest = NULL;
fbb9ce95
IM
1422 int i;
1423
1424 for (i = 0; i < curr->lockdep_depth; i++) {
1425 prev = curr->held_locks + i;
7531e2f3
PZ
1426
1427 if (prev->instance == next->nest_lock)
1428 nest = prev;
1429
f82b217e 1430 if (hlock_class(prev) != hlock_class(next))
fbb9ce95 1431 continue;
7531e2f3 1432
fbb9ce95
IM
1433 /*
1434 * Allow read-after-read recursion of the same
6c9076ec 1435 * lock class (i.e. read_lock(lock)+read_lock(lock)):
fbb9ce95 1436 */
6c9076ec 1437 if ((read == 2) && prev->read)
fbb9ce95 1438 return 2;
7531e2f3
PZ
1439
1440 /*
1441 * We're holding the nest_lock, which serializes this lock's
1442 * nesting behaviour.
1443 */
1444 if (nest)
1445 return 2;
1446
fbb9ce95
IM
1447 return print_deadlock_bug(curr, prev, next);
1448 }
1449 return 1;
1450}
1451
1452/*
1453 * There was a chain-cache miss, and we are about to add a new dependency
1454 * to a previous lock. We recursively validate the following rules:
1455 *
1456 * - would the adding of the <prev> -> <next> dependency create a
1457 * circular dependency in the graph? [== circular deadlock]
1458 *
1459 * - does the new prev->next dependency connect any hardirq-safe lock
1460 * (in the full backwards-subgraph starting at <prev>) with any
1461 * hardirq-unsafe lock (in the full forwards-subgraph starting at
1462 * <next>)? [== illegal lock inversion with hardirq contexts]
1463 *
1464 * - does the new prev->next dependency connect any softirq-safe lock
1465 * (in the full backwards-subgraph starting at <prev>) with any
1466 * softirq-unsafe lock (in the full forwards-subgraph starting at
1467 * <next>)? [== illegal lock inversion with softirq contexts]
1468 *
1469 * any of these scenarios could lead to a deadlock.
1470 *
1471 * Then if all the validations pass, we add the forwards and backwards
1472 * dependency.
1473 */
1474static int
1475check_prev_add(struct task_struct *curr, struct held_lock *prev,
068135e6 1476 struct held_lock *next, int distance)
fbb9ce95
IM
1477{
1478 struct lock_list *entry;
1479 int ret;
1480
1481 /*
1482 * Prove that the new <prev> -> <next> dependency would not
1483 * create a circular dependency in the graph. (We do this by
1484 * forward-recursing into the graph starting at <next>, and
1485 * checking whether we can reach <prev>.)
1486 *
1487 * We are using global variables to control the recursion, to
1488 * keep the stackframe size of the recursive functions low:
1489 */
1490 check_source = next;
1491 check_target = prev;
f82b217e 1492 if (!(check_noncircular(hlock_class(next), 0)))
fbb9ce95
IM
1493 return print_circular_bug_tail();
1494
8e18257d 1495 if (!check_prev_add_irq(curr, prev, next))
fbb9ce95
IM
1496 return 0;
1497
fbb9ce95
IM
1498 /*
1499 * For recursive read-locks we do all the dependency checks,
1500 * but we dont store read-triggered dependencies (only
1501 * write-triggered dependencies). This ensures that only the
1502 * write-side dependencies matter, and that if for example a
1503 * write-lock never takes any other locks, then the reads are
1504 * equivalent to a NOP.
1505 */
1506 if (next->read == 2 || prev->read == 2)
1507 return 1;
1508 /*
1509 * Is the <prev> -> <next> dependency already present?
1510 *
1511 * (this may occur even though this is a new chain: consider
1512 * e.g. the L1 -> L2 -> L3 -> L4 and the L5 -> L1 -> L2 -> L3
1513 * chains - the second one will be new, but L1 already has
1514 * L2 added to its dependency list, due to the first chain.)
1515 */
f82b217e
DJ
1516 list_for_each_entry(entry, &hlock_class(prev)->locks_after, entry) {
1517 if (entry->class == hlock_class(next)) {
068135e6
JB
1518 if (distance == 1)
1519 entry->distance = 1;
fbb9ce95 1520 return 2;
068135e6 1521 }
fbb9ce95
IM
1522 }
1523
1524 /*
1525 * Ok, all validations passed, add the new lock
1526 * to the previous lock's dependency list:
1527 */
f82b217e
DJ
1528 ret = add_lock_to_list(hlock_class(prev), hlock_class(next),
1529 &hlock_class(prev)->locks_after,
1530 next->acquire_ip, distance);
068135e6 1531
fbb9ce95
IM
1532 if (!ret)
1533 return 0;
910b1b2e 1534
f82b217e
DJ
1535 ret = add_lock_to_list(hlock_class(next), hlock_class(prev),
1536 &hlock_class(next)->locks_before,
1537 next->acquire_ip, distance);
910b1b2e
JP
1538 if (!ret)
1539 return 0;
fbb9ce95
IM
1540
1541 /*
8e18257d
PZ
1542 * Debugging printouts:
1543 */
f82b217e 1544 if (verbose(hlock_class(prev)) || verbose(hlock_class(next))) {
8e18257d
PZ
1545 graph_unlock();
1546 printk("\n new dependency: ");
f82b217e 1547 print_lock_name(hlock_class(prev));
8e18257d 1548 printk(" => ");
f82b217e 1549 print_lock_name(hlock_class(next));
8e18257d 1550 printk("\n");
fbb9ce95 1551 dump_stack();
8e18257d 1552 return graph_lock();
fbb9ce95 1553 }
8e18257d
PZ
1554 return 1;
1555}
fbb9ce95 1556
8e18257d
PZ
1557/*
1558 * Add the dependency to all directly-previous locks that are 'relevant'.
1559 * The ones that are relevant are (in increasing distance from curr):
1560 * all consecutive trylock entries and the final non-trylock entry - or
1561 * the end of this context's lock-chain - whichever comes first.
1562 */
1563static int
1564check_prevs_add(struct task_struct *curr, struct held_lock *next)
1565{
1566 int depth = curr->lockdep_depth;
1567 struct held_lock *hlock;
d6d897ce 1568
fbb9ce95 1569 /*
8e18257d
PZ
1570 * Debugging checks.
1571 *
1572 * Depth must not be zero for a non-head lock:
fbb9ce95 1573 */
8e18257d
PZ
1574 if (!depth)
1575 goto out_bug;
fbb9ce95 1576 /*
8e18257d
PZ
1577 * At least two relevant locks must exist for this
1578 * to be a head:
fbb9ce95 1579 */
8e18257d
PZ
1580 if (curr->held_locks[depth].irq_context !=
1581 curr->held_locks[depth-1].irq_context)
1582 goto out_bug;
74c383f1 1583
8e18257d
PZ
1584 for (;;) {
1585 int distance = curr->lockdep_depth - depth + 1;
1586 hlock = curr->held_locks + depth-1;
1587 /*
1588 * Only non-recursive-read entries get new dependencies
1589 * added:
1590 */
1591 if (hlock->read != 2) {
1592 if (!check_prev_add(curr, hlock, next, distance))
1593 return 0;
1594 /*
1595 * Stop after the first non-trylock entry,
1596 * as non-trylock entries have added their
1597 * own direct dependencies already, so this
1598 * lock is connected to them indirectly:
1599 */
1600 if (!hlock->trylock)
1601 break;
74c383f1 1602 }
8e18257d
PZ
1603 depth--;
1604 /*
1605 * End of lock-stack?
1606 */
1607 if (!depth)
1608 break;
1609 /*
1610 * Stop the search if we cross into another context:
1611 */
1612 if (curr->held_locks[depth].irq_context !=
1613 curr->held_locks[depth-1].irq_context)
1614 break;
fbb9ce95 1615 }
8e18257d
PZ
1616 return 1;
1617out_bug:
1618 if (!debug_locks_off_graph_unlock())
1619 return 0;
fbb9ce95 1620
8e18257d 1621 WARN_ON(1);
fbb9ce95 1622
8e18257d 1623 return 0;
fbb9ce95
IM
1624}
1625
8e18257d 1626unsigned long nr_lock_chains;
443cd507 1627struct lock_chain lock_chains[MAX_LOCKDEP_CHAINS];
cd1a28e8 1628int nr_chain_hlocks;
443cd507
HY
1629static u16 chain_hlocks[MAX_LOCKDEP_CHAIN_HLOCKS];
1630
1631struct lock_class *lock_chain_get_class(struct lock_chain *chain, int i)
1632{
1633 return lock_classes + chain_hlocks[chain->base + i];
1634}
8e18257d 1635
fbb9ce95
IM
1636/*
1637 * Look up a dependency chain. If the key is not present yet then
9e860d00
JP
1638 * add it and return 1 - in this case the new dependency chain is
1639 * validated. If the key is already hashed, return 0.
1640 * (On return with 1 graph_lock is held.)
fbb9ce95 1641 */
443cd507
HY
1642static inline int lookup_chain_cache(struct task_struct *curr,
1643 struct held_lock *hlock,
1644 u64 chain_key)
fbb9ce95 1645{
f82b217e 1646 struct lock_class *class = hlock_class(hlock);
fbb9ce95
IM
1647 struct list_head *hash_head = chainhashentry(chain_key);
1648 struct lock_chain *chain;
443cd507 1649 struct held_lock *hlock_curr, *hlock_next;
cd1a28e8 1650 int i, j, n, cn;
fbb9ce95 1651
381a2292
JP
1652 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
1653 return 0;
fbb9ce95
IM
1654 /*
1655 * We can walk it lock-free, because entries only get added
1656 * to the hash:
1657 */
1658 list_for_each_entry(chain, hash_head, entry) {
1659 if (chain->chain_key == chain_key) {
1660cache_hit:
1661 debug_atomic_inc(&chain_lookup_hits);
81fc685a 1662 if (very_verbose(class))
755cd900
AM
1663 printk("\nhash chain already cached, key: "
1664 "%016Lx tail class: [%p] %s\n",
1665 (unsigned long long)chain_key,
1666 class->key, class->name);
fbb9ce95
IM
1667 return 0;
1668 }
1669 }
81fc685a 1670 if (very_verbose(class))
755cd900
AM
1671 printk("\nnew hash chain, key: %016Lx tail class: [%p] %s\n",
1672 (unsigned long long)chain_key, class->key, class->name);
fbb9ce95
IM
1673 /*
1674 * Allocate a new chain entry from the static array, and add
1675 * it to the hash:
1676 */
74c383f1
IM
1677 if (!graph_lock())
1678 return 0;
fbb9ce95
IM
1679 /*
1680 * We have to walk the chain again locked - to avoid duplicates:
1681 */
1682 list_for_each_entry(chain, hash_head, entry) {
1683 if (chain->chain_key == chain_key) {
74c383f1 1684 graph_unlock();
fbb9ce95
IM
1685 goto cache_hit;
1686 }
1687 }
1688 if (unlikely(nr_lock_chains >= MAX_LOCKDEP_CHAINS)) {
74c383f1
IM
1689 if (!debug_locks_off_graph_unlock())
1690 return 0;
1691
fbb9ce95
IM
1692 printk("BUG: MAX_LOCKDEP_CHAINS too low!\n");
1693 printk("turning off the locking correctness validator.\n");
1694 return 0;
1695 }
1696 chain = lock_chains + nr_lock_chains++;
1697 chain->chain_key = chain_key;
443cd507
HY
1698 chain->irq_context = hlock->irq_context;
1699 /* Find the first held_lock of current chain */
1700 hlock_next = hlock;
1701 for (i = curr->lockdep_depth - 1; i >= 0; i--) {
1702 hlock_curr = curr->held_locks + i;
1703 if (hlock_curr->irq_context != hlock_next->irq_context)
1704 break;
1705 hlock_next = hlock;
1706 }
1707 i++;
1708 chain->depth = curr->lockdep_depth + 1 - i;
cd1a28e8
HY
1709 cn = nr_chain_hlocks;
1710 while (cn + chain->depth <= MAX_LOCKDEP_CHAIN_HLOCKS) {
1711 n = cmpxchg(&nr_chain_hlocks, cn, cn + chain->depth);
1712 if (n == cn)
1713 break;
1714 cn = n;
1715 }
1716 if (likely(cn + chain->depth <= MAX_LOCKDEP_CHAIN_HLOCKS)) {
1717 chain->base = cn;
443cd507 1718 for (j = 0; j < chain->depth - 1; j++, i++) {
f82b217e 1719 int lock_id = curr->held_locks[i].class_idx - 1;
443cd507
HY
1720 chain_hlocks[chain->base + j] = lock_id;
1721 }
1722 chain_hlocks[chain->base + j] = class - lock_classes;
1723 }
fbb9ce95
IM
1724 list_add_tail_rcu(&chain->entry, hash_head);
1725 debug_atomic_inc(&chain_lookup_misses);
8e18257d
PZ
1726 inc_chains();
1727
1728 return 1;
1729}
1730
1731static int validate_chain(struct task_struct *curr, struct lockdep_map *lock,
4e6045f1 1732 struct held_lock *hlock, int chain_head, u64 chain_key)
8e18257d
PZ
1733{
1734 /*
1735 * Trylock needs to maintain the stack of held locks, but it
1736 * does not add new dependencies, because trylock can be done
1737 * in any order.
1738 *
1739 * We look up the chain_key and do the O(N^2) check and update of
1740 * the dependencies only if this is a new dependency chain.
1741 * (If lookup_chain_cache() returns with 1 it acquires
1742 * graph_lock for us)
1743 */
1744 if (!hlock->trylock && (hlock->check == 2) &&
443cd507 1745 lookup_chain_cache(curr, hlock, chain_key)) {
8e18257d
PZ
1746 /*
1747 * Check whether last held lock:
1748 *
1749 * - is irq-safe, if this lock is irq-unsafe
1750 * - is softirq-safe, if this lock is hardirq-unsafe
1751 *
1752 * And check whether the new lock's dependency graph
1753 * could lead back to the previous lock.
1754 *
1755 * any of these scenarios could lead to a deadlock. If
1756 * All validations
1757 */
1758 int ret = check_deadlock(curr, hlock, lock, hlock->read);
1759
1760 if (!ret)
1761 return 0;
1762 /*
1763 * Mark recursive read, as we jump over it when
1764 * building dependencies (just like we jump over
1765 * trylock entries):
1766 */
1767 if (ret == 2)
1768 hlock->read = 2;
1769 /*
1770 * Add dependency only if this lock is not the head
1771 * of the chain, and if it's not a secondary read-lock:
1772 */
1773 if (!chain_head && ret != 2)
1774 if (!check_prevs_add(curr, hlock))
1775 return 0;
1776 graph_unlock();
1777 } else
1778 /* after lookup_chain_cache(): */
1779 if (unlikely(!debug_locks))
1780 return 0;
fbb9ce95
IM
1781
1782 return 1;
1783}
8e18257d
PZ
1784#else
1785static inline int validate_chain(struct task_struct *curr,
1786 struct lockdep_map *lock, struct held_lock *hlock,
3aa416b0 1787 int chain_head, u64 chain_key)
8e18257d
PZ
1788{
1789 return 1;
1790}
ca58abcb 1791#endif
fbb9ce95
IM
1792
1793/*
1794 * We are building curr_chain_key incrementally, so double-check
1795 * it from scratch, to make sure that it's done correctly:
1796 */
1d09daa5 1797static void check_chain_key(struct task_struct *curr)
fbb9ce95
IM
1798{
1799#ifdef CONFIG_DEBUG_LOCKDEP
1800 struct held_lock *hlock, *prev_hlock = NULL;
1801 unsigned int i, id;
1802 u64 chain_key = 0;
1803
1804 for (i = 0; i < curr->lockdep_depth; i++) {
1805 hlock = curr->held_locks + i;
1806 if (chain_key != hlock->prev_chain_key) {
1807 debug_locks_off();
2df8b1d6 1808 WARN(1, "hm#1, depth: %u [%u], %016Lx != %016Lx\n",
fbb9ce95
IM
1809 curr->lockdep_depth, i,
1810 (unsigned long long)chain_key,
1811 (unsigned long long)hlock->prev_chain_key);
fbb9ce95
IM
1812 return;
1813 }
f82b217e 1814 id = hlock->class_idx - 1;
381a2292
JP
1815 if (DEBUG_LOCKS_WARN_ON(id >= MAX_LOCKDEP_KEYS))
1816 return;
1817
fbb9ce95
IM
1818 if (prev_hlock && (prev_hlock->irq_context !=
1819 hlock->irq_context))
1820 chain_key = 0;
1821 chain_key = iterate_chain_key(chain_key, id);
1822 prev_hlock = hlock;
1823 }
1824 if (chain_key != curr->curr_chain_key) {
1825 debug_locks_off();
2df8b1d6 1826 WARN(1, "hm#2, depth: %u [%u], %016Lx != %016Lx\n",
fbb9ce95
IM
1827 curr->lockdep_depth, i,
1828 (unsigned long long)chain_key,
1829 (unsigned long long)curr->curr_chain_key);
fbb9ce95
IM
1830 }
1831#endif
1832}
1833
8e18257d
PZ
1834static int
1835print_usage_bug(struct task_struct *curr, struct held_lock *this,
1836 enum lock_usage_bit prev_bit, enum lock_usage_bit new_bit)
1837{
1838 if (!debug_locks_off_graph_unlock() || debug_locks_silent)
1839 return 0;
1840
1841 printk("\n=================================\n");
1842 printk( "[ INFO: inconsistent lock state ]\n");
1843 print_kernel_version();
1844 printk( "---------------------------------\n");
1845
1846 printk("inconsistent {%s} -> {%s} usage.\n",
1847 usage_str[prev_bit], usage_str[new_bit]);
1848
1849 printk("%s/%d [HC%u[%lu]:SC%u[%lu]:HE%u:SE%u] takes:\n",
ba25f9dc 1850 curr->comm, task_pid_nr(curr),
8e18257d
PZ
1851 trace_hardirq_context(curr), hardirq_count() >> HARDIRQ_SHIFT,
1852 trace_softirq_context(curr), softirq_count() >> SOFTIRQ_SHIFT,
1853 trace_hardirqs_enabled(curr),
1854 trace_softirqs_enabled(curr));
1855 print_lock(this);
1856
1857 printk("{%s} state was registered at:\n", usage_str[prev_bit]);
f82b217e 1858 print_stack_trace(hlock_class(this)->usage_traces + prev_bit, 1);
8e18257d
PZ
1859
1860 print_irqtrace_events(curr);
1861 printk("\nother info that might help us debug this:\n");
1862 lockdep_print_held_locks(curr);
1863
1864 printk("\nstack backtrace:\n");
1865 dump_stack();
1866
1867 return 0;
1868}
1869
1870/*
1871 * Print out an error if an invalid bit is set:
1872 */
1873static inline int
1874valid_state(struct task_struct *curr, struct held_lock *this,
1875 enum lock_usage_bit new_bit, enum lock_usage_bit bad_bit)
1876{
f82b217e 1877 if (unlikely(hlock_class(this)->usage_mask & (1 << bad_bit)))
8e18257d
PZ
1878 return print_usage_bug(curr, this, bad_bit, new_bit);
1879 return 1;
1880}
1881
1882static int mark_lock(struct task_struct *curr, struct held_lock *this,
1883 enum lock_usage_bit new_bit);
1884
81d68a96 1885#if defined(CONFIG_TRACE_IRQFLAGS) && defined(CONFIG_PROVE_LOCKING)
fbb9ce95
IM
1886
1887/*
1888 * print irq inversion bug:
1889 */
1890static int
1891print_irq_inversion_bug(struct task_struct *curr, struct lock_class *other,
1892 struct held_lock *this, int forwards,
1893 const char *irqclass)
1894{
74c383f1 1895 if (!debug_locks_off_graph_unlock() || debug_locks_silent)
fbb9ce95
IM
1896 return 0;
1897
1898 printk("\n=========================================================\n");
1899 printk( "[ INFO: possible irq lock inversion dependency detected ]\n");
99de055a 1900 print_kernel_version();
fbb9ce95
IM
1901 printk( "---------------------------------------------------------\n");
1902 printk("%s/%d just changed the state of lock:\n",
ba25f9dc 1903 curr->comm, task_pid_nr(curr));
fbb9ce95
IM
1904 print_lock(this);
1905 if (forwards)
1906 printk("but this lock took another, %s-irq-unsafe lock in the past:\n", irqclass);
1907 else
1908 printk("but this lock was taken by another, %s-irq-safe lock in the past:\n", irqclass);
1909 print_lock_name(other);
1910 printk("\n\nand interrupts could create inverse lock ordering between them.\n\n");
1911
1912 printk("\nother info that might help us debug this:\n");
1913 lockdep_print_held_locks(curr);
1914
1915 printk("\nthe first lock's dependencies:\n");
f82b217e 1916 print_lock_dependencies(hlock_class(this), 0);
fbb9ce95
IM
1917
1918 printk("\nthe second lock's dependencies:\n");
1919 print_lock_dependencies(other, 0);
1920
1921 printk("\nstack backtrace:\n");
1922 dump_stack();
1923
1924 return 0;
1925}
1926
1927/*
1928 * Prove that in the forwards-direction subgraph starting at <this>
1929 * there is no lock matching <mask>:
1930 */
1931static int
1932check_usage_forwards(struct task_struct *curr, struct held_lock *this,
1933 enum lock_usage_bit bit, const char *irqclass)
1934{
1935 int ret;
1936
1937 find_usage_bit = bit;
1938 /* fills in <forwards_match> */
f82b217e 1939 ret = find_usage_forwards(hlock_class(this), 0);
fbb9ce95
IM
1940 if (!ret || ret == 1)
1941 return ret;
1942
1943 return print_irq_inversion_bug(curr, forwards_match, this, 1, irqclass);
1944}
1945
1946/*
1947 * Prove that in the backwards-direction subgraph starting at <this>
1948 * there is no lock matching <mask>:
1949 */
1950static int
1951check_usage_backwards(struct task_struct *curr, struct held_lock *this,
1952 enum lock_usage_bit bit, const char *irqclass)
1953{
1954 int ret;
1955
1956 find_usage_bit = bit;
1957 /* fills in <backwards_match> */
f82b217e 1958 ret = find_usage_backwards(hlock_class(this), 0);
fbb9ce95
IM
1959 if (!ret || ret == 1)
1960 return ret;
1961
1962 return print_irq_inversion_bug(curr, backwards_match, this, 0, irqclass);
1963}
1964
3117df04 1965void print_irqtrace_events(struct task_struct *curr)
fbb9ce95
IM
1966{
1967 printk("irq event stamp: %u\n", curr->irq_events);
1968 printk("hardirqs last enabled at (%u): ", curr->hardirq_enable_event);
1969 print_ip_sym(curr->hardirq_enable_ip);
1970 printk("hardirqs last disabled at (%u): ", curr->hardirq_disable_event);
1971 print_ip_sym(curr->hardirq_disable_ip);
1972 printk("softirqs last enabled at (%u): ", curr->softirq_enable_event);
1973 print_ip_sym(curr->softirq_enable_ip);
1974 printk("softirqs last disabled at (%u): ", curr->softirq_disable_event);
1975 print_ip_sym(curr->softirq_disable_ip);
1976}
1977
8e18257d 1978static int hardirq_verbose(struct lock_class *class)
fbb9ce95 1979{
8e18257d
PZ
1980#if HARDIRQ_VERBOSE
1981 return class_filter(class);
1982#endif
fbb9ce95
IM
1983 return 0;
1984}
1985
8e18257d 1986static int softirq_verbose(struct lock_class *class)
fbb9ce95 1987{
8e18257d
PZ
1988#if SOFTIRQ_VERBOSE
1989 return class_filter(class);
1990#endif
1991 return 0;
fbb9ce95
IM
1992}
1993
cf40bd16
NP
1994static int reclaim_verbose(struct lock_class *class)
1995{
1996#if RECLAIM_VERBOSE
1997 return class_filter(class);
1998#endif
1999 return 0;
2000}
2001
fbb9ce95
IM
2002#define STRICT_READ_CHECKS 1
2003
604de3b5
PZ
2004static const char *state_names[] = {
2005#define LOCKDEP_STATE(__STATE) \
2006 STR(__STATE),
2007#include "lockdep_states.h"
2008#undef LOCKDEP_STATE
2009};
2010
2011static inline const char *state_name(enum lock_usage_bit bit)
2012{
2013 return state_names[bit >> 2];
2014}
2015
2016static const char *state_rnames[] = {
2017#define LOCKDEP_STATE(__STATE) \
2018 STR(__STATE)"-READ",
2019#include "lockdep_states.h"
2020#undef LOCKDEP_STATE
2021};
2022
2023static inline const char *state_rname(enum lock_usage_bit bit)
2024{
2025 return state_rnames[bit >> 2];
2026}
2027
6a6904d3
PZ
2028static int
2029mark_lock_irq_used_in(struct task_struct *curr, struct held_lock *this,
2030 int new_bit, int excl_bit,
6a6904d3
PZ
2031 int (*verbose)(struct lock_class *class))
2032{
604de3b5
PZ
2033 const char *name = state_name(new_bit);
2034 const char *rname = state_rname(new_bit);
2035
6a6904d3
PZ
2036 if (!valid_state(curr, this, new_bit, excl_bit))
2037 return 0;
2038 if (!valid_state(curr, this, new_bit, excl_bit + 1))
2039 return 0;
2040 /*
2041 * just marked it hardirq-safe, check that this lock
2042 * took no hardirq-unsafe lock in the past:
2043 */
2044 if (!check_usage_forwards(curr, this, excl_bit, name))
2045 return 0;
2046#if STRICT_READ_CHECKS
2047 /*
2048 * just marked it hardirq-safe, check that this lock
2049 * took no hardirq-unsafe-read lock in the past:
2050 */
2051 if (!check_usage_forwards(curr, this, excl_bit + 1, rname))
2052 return 0;
2053#endif
2054 if (verbose(hlock_class(this)))
2055 return 2;
2056
2057 return 1;
2058}
2059
2060static int
2061mark_lock_irq_used_in_read(struct task_struct *curr, struct held_lock *this,
2062 int new_bit, int excl_bit,
6a6904d3
PZ
2063 int (*verbose)(struct lock_class *class))
2064{
604de3b5
PZ
2065 const char *name = state_name(new_bit);
2066 const char *rname = state_rname(new_bit);
2067
6a6904d3
PZ
2068 if (!valid_state(curr, this, new_bit, excl_bit))
2069 return 0;
2070 /*
2071 * just marked it hardirq-read-safe, check that this lock
2072 * took no hardirq-unsafe lock in the past:
2073 */
2074 if (!check_usage_forwards(curr, this, excl_bit, name))
2075 return 0;
2076 if (verbose(hlock_class(this)))
2077 return 2;
2078
2079 return 1;
2080}
2081
2082static int
2083mark_lock_irq_enabled(struct task_struct *curr, struct held_lock *this,
2084 int new_bit, int excl_bit,
6a6904d3
PZ
2085 int (*verbose)(struct lock_class *class))
2086{
604de3b5
PZ
2087 const char *name = state_name(new_bit);
2088 const char *rname = state_rname(new_bit);
2089
6a6904d3
PZ
2090 if (!valid_state(curr, this, new_bit, excl_bit))
2091 return 0;
2092 if (!valid_state(curr, this, new_bit, excl_bit + 1))
2093 return 0;
2094 /*
2095 * just marked it hardirq-unsafe, check that no hardirq-safe
2096 * lock in the system ever took it in the past:
2097 */
2098 if (!check_usage_backwards(curr, this, excl_bit, name))
2099 return 0;
2100#if STRICT_READ_CHECKS
2101 /*
2102 * just marked it hardirq-unsafe, check that no
2103 * hardirq-safe-read lock in the system ever took
2104 * it in the past:
2105 */
2106 if (!check_usage_backwards(curr, this, excl_bit + 1, rname))
2107 return 0;
2108#endif
2109 if (verbose(hlock_class(this)))
2110 return 2;
2111
2112 return 1;
2113}
2114
2115static int
2116mark_lock_irq_enabled_read(struct task_struct *curr, struct held_lock *this,
2117 int new_bit, int excl_bit,
6a6904d3
PZ
2118 int (*verbose)(struct lock_class *class))
2119{
604de3b5
PZ
2120 const char *name = state_name(new_bit);
2121 const char *rname = state_rname(new_bit);
2122
6a6904d3
PZ
2123 if (!valid_state(curr, this, new_bit, excl_bit))
2124 return 0;
2125#if STRICT_READ_CHECKS
2126 /*
2127 * just marked it hardirq-read-unsafe, check that no
2128 * hardirq-safe lock in the system ever took it in the past:
2129 */
2130 if (!check_usage_backwards(curr, this, excl_bit, name))
2131 return 0;
2132#endif
2133 if (verbose(hlock_class(this)))
2134 return 2;
2135
2136 return 1;
2137}
2138
8e18257d
PZ
2139static int mark_lock_irq(struct task_struct *curr, struct held_lock *this,
2140 enum lock_usage_bit new_bit)
fbb9ce95 2141{
8e18257d 2142 int ret = 1;
fbb9ce95 2143
8e18257d 2144 switch(new_bit) {
fbb9ce95 2145 case LOCK_USED_IN_HARDIRQ:
6a6904d3
PZ
2146 return mark_lock_irq_used_in(curr, this, new_bit,
2147 LOCK_ENABLED_HARDIRQ,
604de3b5 2148 hardirq_verbose);
fbb9ce95 2149 case LOCK_USED_IN_SOFTIRQ:
6a6904d3
PZ
2150 return mark_lock_irq_used_in(curr, this, new_bit,
2151 LOCK_ENABLED_SOFTIRQ,
604de3b5 2152 softirq_verbose);
cf40bd16 2153 case LOCK_USED_IN_RECLAIM_FS:
6a6904d3
PZ
2154 return mark_lock_irq_used_in(curr, this, new_bit,
2155 LOCK_ENABLED_RECLAIM_FS,
6a6904d3
PZ
2156 reclaim_verbose);
2157
fbb9ce95 2158 case LOCK_USED_IN_HARDIRQ_READ:
6a6904d3
PZ
2159 return mark_lock_irq_used_in_read(curr, this, new_bit,
2160 LOCK_ENABLED_HARDIRQ,
604de3b5 2161 hardirq_verbose);
fbb9ce95 2162 case LOCK_USED_IN_SOFTIRQ_READ:
6a6904d3
PZ
2163 return mark_lock_irq_used_in_read(curr, this, new_bit,
2164 LOCK_ENABLED_SOFTIRQ,
604de3b5 2165 softirq_verbose);
cf40bd16 2166 case LOCK_USED_IN_RECLAIM_FS_READ:
6a6904d3
PZ
2167 return mark_lock_irq_used_in_read(curr, this, new_bit,
2168 LOCK_ENABLED_RECLAIM_FS,
6a6904d3
PZ
2169 reclaim_verbose);
2170
4fc95e86 2171 case LOCK_ENABLED_HARDIRQ:
6a6904d3
PZ
2172 return mark_lock_irq_enabled(curr, this, new_bit,
2173 LOCK_USED_IN_HARDIRQ,
604de3b5 2174 hardirq_verbose);
4fc95e86 2175 case LOCK_ENABLED_SOFTIRQ:
6a6904d3
PZ
2176 return mark_lock_irq_enabled(curr, this, new_bit,
2177 LOCK_USED_IN_SOFTIRQ,
604de3b5 2178 softirq_verbose);
a652d708 2179 case LOCK_ENABLED_RECLAIM_FS:
6a6904d3
PZ
2180 return mark_lock_irq_enabled(curr, this, new_bit,
2181 LOCK_USED_IN_RECLAIM_FS,
6a6904d3
PZ
2182 reclaim_verbose);
2183
4fc95e86 2184 case LOCK_ENABLED_HARDIRQ_READ:
6a6904d3
PZ
2185 return mark_lock_irq_enabled_read(curr, this, new_bit,
2186 LOCK_USED_IN_HARDIRQ,
604de3b5 2187 hardirq_verbose);
4fc95e86 2188 case LOCK_ENABLED_SOFTIRQ_READ:
6a6904d3
PZ
2189 return mark_lock_irq_enabled_read(curr, this, new_bit,
2190 LOCK_USED_IN_SOFTIRQ,
604de3b5 2191 softirq_verbose);
a652d708 2192 case LOCK_ENABLED_RECLAIM_FS_READ:
6a6904d3
PZ
2193 return mark_lock_irq_enabled_read(curr, this, new_bit,
2194 LOCK_USED_IN_RECLAIM_FS,
6a6904d3
PZ
2195 reclaim_verbose);
2196
fbb9ce95 2197 default:
fbb9ce95 2198 WARN_ON(1);
8e18257d 2199 break;
fbb9ce95
IM
2200 }
2201
2202 return ret;
2203}
2204
cf40bd16 2205enum mark_type {
36bfb9bb
PZ
2206#define LOCKDEP_STATE(__STATE) __STATE,
2207#include "lockdep_states.h"
2208#undef LOCKDEP_STATE
cf40bd16
NP
2209};
2210
36bfb9bb
PZ
2211#define MARK_HELD_CASE(__STATE) \
2212 case __STATE: \
2213 if (hlock->read) \
2214 usage_bit = LOCK_ENABLED_##__STATE##_READ; \
2215 else \
2216 usage_bit = LOCK_ENABLED_##__STATE; \
2217 break;
2218
fbb9ce95
IM
2219/*
2220 * Mark all held locks with a usage bit:
2221 */
1d09daa5 2222static int
cf40bd16 2223mark_held_locks(struct task_struct *curr, enum mark_type mark)
fbb9ce95
IM
2224{
2225 enum lock_usage_bit usage_bit;
2226 struct held_lock *hlock;
2227 int i;
2228
2229 for (i = 0; i < curr->lockdep_depth; i++) {
2230 hlock = curr->held_locks + i;
2231
cf40bd16 2232 switch (mark) {
36bfb9bb
PZ
2233#define LOCKDEP_STATE(__STATE) MARK_HELD_CASE(__STATE)
2234#include "lockdep_states.h"
2235#undef LOCKDEP_STATE
cf40bd16
NP
2236 default:
2237 BUG();
fbb9ce95 2238 }
cf40bd16 2239
4ff773bb 2240 if (!mark_lock(curr, hlock, usage_bit))
fbb9ce95
IM
2241 return 0;
2242 }
2243
2244 return 1;
2245}
2246
2247/*
2248 * Debugging helper: via this flag we know that we are in
2249 * 'early bootup code', and will warn about any invalid irqs-on event:
2250 */
2251static int early_boot_irqs_enabled;
2252
2253void early_boot_irqs_off(void)
2254{
2255 early_boot_irqs_enabled = 0;
2256}
2257
2258void early_boot_irqs_on(void)
2259{
2260 early_boot_irqs_enabled = 1;
2261}
2262
2263/*
2264 * Hardirqs will be enabled:
2265 */
6afe40b4 2266void trace_hardirqs_on_caller(unsigned long ip)
fbb9ce95
IM
2267{
2268 struct task_struct *curr = current;
fbb9ce95 2269
6afe40b4 2270 time_hardirqs_on(CALLER_ADDR0, ip);
81d68a96 2271
fbb9ce95
IM
2272 if (unlikely(!debug_locks || current->lockdep_recursion))
2273 return;
2274
2275 if (DEBUG_LOCKS_WARN_ON(unlikely(!early_boot_irqs_enabled)))
2276 return;
2277
2278 if (unlikely(curr->hardirqs_enabled)) {
2279 debug_atomic_inc(&redundant_hardirqs_on);
2280 return;
2281 }
2282 /* we'll do an OFF -> ON transition: */
2283 curr->hardirqs_enabled = 1;
fbb9ce95
IM
2284
2285 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2286 return;
2287 if (DEBUG_LOCKS_WARN_ON(current->hardirq_context))
2288 return;
2289 /*
2290 * We are going to turn hardirqs on, so set the
2291 * usage bit for all held locks:
2292 */
cf40bd16 2293 if (!mark_held_locks(curr, HARDIRQ))
fbb9ce95
IM
2294 return;
2295 /*
2296 * If we have softirqs enabled, then set the usage
2297 * bit for all held locks. (disabled hardirqs prevented
2298 * this bit from being set before)
2299 */
2300 if (curr->softirqs_enabled)
cf40bd16 2301 if (!mark_held_locks(curr, SOFTIRQ))
fbb9ce95
IM
2302 return;
2303
8e18257d
PZ
2304 curr->hardirq_enable_ip = ip;
2305 curr->hardirq_enable_event = ++curr->irq_events;
2306 debug_atomic_inc(&hardirqs_on_events);
2307}
81d68a96 2308EXPORT_SYMBOL(trace_hardirqs_on_caller);
8e18257d 2309
1d09daa5 2310void trace_hardirqs_on(void)
81d68a96
SR
2311{
2312 trace_hardirqs_on_caller(CALLER_ADDR0);
2313}
8e18257d
PZ
2314EXPORT_SYMBOL(trace_hardirqs_on);
2315
2316/*
2317 * Hardirqs were disabled:
2318 */
6afe40b4 2319void trace_hardirqs_off_caller(unsigned long ip)
8e18257d
PZ
2320{
2321 struct task_struct *curr = current;
2322
6afe40b4 2323 time_hardirqs_off(CALLER_ADDR0, ip);
81d68a96 2324
8e18257d
PZ
2325 if (unlikely(!debug_locks || current->lockdep_recursion))
2326 return;
2327
2328 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2329 return;
2330
2331 if (curr->hardirqs_enabled) {
2332 /*
2333 * We have done an ON -> OFF transition:
2334 */
2335 curr->hardirqs_enabled = 0;
6afe40b4 2336 curr->hardirq_disable_ip = ip;
8e18257d
PZ
2337 curr->hardirq_disable_event = ++curr->irq_events;
2338 debug_atomic_inc(&hardirqs_off_events);
2339 } else
2340 debug_atomic_inc(&redundant_hardirqs_off);
2341}
81d68a96 2342EXPORT_SYMBOL(trace_hardirqs_off_caller);
8e18257d 2343
1d09daa5 2344void trace_hardirqs_off(void)
81d68a96
SR
2345{
2346 trace_hardirqs_off_caller(CALLER_ADDR0);
2347}
8e18257d
PZ
2348EXPORT_SYMBOL(trace_hardirqs_off);
2349
2350/*
2351 * Softirqs will be enabled:
2352 */
2353void trace_softirqs_on(unsigned long ip)
2354{
2355 struct task_struct *curr = current;
2356
2357 if (unlikely(!debug_locks))
2358 return;
2359
2360 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2361 return;
2362
2363 if (curr->softirqs_enabled) {
2364 debug_atomic_inc(&redundant_softirqs_on);
2365 return;
2366 }
2367
2368 /*
2369 * We'll do an OFF -> ON transition:
2370 */
2371 curr->softirqs_enabled = 1;
2372 curr->softirq_enable_ip = ip;
2373 curr->softirq_enable_event = ++curr->irq_events;
2374 debug_atomic_inc(&softirqs_on_events);
2375 /*
2376 * We are going to turn softirqs on, so set the
2377 * usage bit for all held locks, if hardirqs are
2378 * enabled too:
2379 */
2380 if (curr->hardirqs_enabled)
cf40bd16 2381 mark_held_locks(curr, SOFTIRQ);
8e18257d
PZ
2382}
2383
2384/*
2385 * Softirqs were disabled:
2386 */
2387void trace_softirqs_off(unsigned long ip)
2388{
2389 struct task_struct *curr = current;
2390
2391 if (unlikely(!debug_locks))
2392 return;
2393
2394 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2395 return;
2396
2397 if (curr->softirqs_enabled) {
2398 /*
2399 * We have done an ON -> OFF transition:
2400 */
2401 curr->softirqs_enabled = 0;
2402 curr->softirq_disable_ip = ip;
2403 curr->softirq_disable_event = ++curr->irq_events;
2404 debug_atomic_inc(&softirqs_off_events);
2405 DEBUG_LOCKS_WARN_ON(!softirq_count());
2406 } else
2407 debug_atomic_inc(&redundant_softirqs_off);
2408}
2409
cf40bd16
NP
2410void lockdep_trace_alloc(gfp_t gfp_mask)
2411{
2412 struct task_struct *curr = current;
2413
2414 if (unlikely(!debug_locks))
2415 return;
2416
2417 /* no reclaim without waiting on it */
2418 if (!(gfp_mask & __GFP_WAIT))
2419 return;
2420
2421 /* this guy won't enter reclaim */
2422 if ((curr->flags & PF_MEMALLOC) && !(gfp_mask & __GFP_NOMEMALLOC))
2423 return;
2424
2425 /* We're only interested __GFP_FS allocations for now */
2426 if (!(gfp_mask & __GFP_FS))
2427 return;
2428
2429 if (DEBUG_LOCKS_WARN_ON(irqs_disabled()))
2430 return;
2431
2432 mark_held_locks(curr, RECLAIM_FS);
2433}
2434
8e18257d
PZ
2435static int mark_irqflags(struct task_struct *curr, struct held_lock *hlock)
2436{
2437 /*
2438 * If non-trylock use in a hardirq or softirq context, then
2439 * mark the lock as used in these contexts:
2440 */
2441 if (!hlock->trylock) {
2442 if (hlock->read) {
2443 if (curr->hardirq_context)
2444 if (!mark_lock(curr, hlock,
2445 LOCK_USED_IN_HARDIRQ_READ))
2446 return 0;
2447 if (curr->softirq_context)
2448 if (!mark_lock(curr, hlock,
2449 LOCK_USED_IN_SOFTIRQ_READ))
2450 return 0;
2451 } else {
2452 if (curr->hardirq_context)
2453 if (!mark_lock(curr, hlock, LOCK_USED_IN_HARDIRQ))
2454 return 0;
2455 if (curr->softirq_context)
2456 if (!mark_lock(curr, hlock, LOCK_USED_IN_SOFTIRQ))
2457 return 0;
2458 }
2459 }
2460 if (!hlock->hardirqs_off) {
2461 if (hlock->read) {
2462 if (!mark_lock(curr, hlock,
4fc95e86 2463 LOCK_ENABLED_HARDIRQ_READ))
8e18257d
PZ
2464 return 0;
2465 if (curr->softirqs_enabled)
2466 if (!mark_lock(curr, hlock,
4fc95e86 2467 LOCK_ENABLED_SOFTIRQ_READ))
8e18257d
PZ
2468 return 0;
2469 } else {
2470 if (!mark_lock(curr, hlock,
4fc95e86 2471 LOCK_ENABLED_HARDIRQ))
8e18257d
PZ
2472 return 0;
2473 if (curr->softirqs_enabled)
2474 if (!mark_lock(curr, hlock,
4fc95e86 2475 LOCK_ENABLED_SOFTIRQ))
8e18257d
PZ
2476 return 0;
2477 }
2478 }
2479
cf40bd16
NP
2480 /*
2481 * We reuse the irq context infrastructure more broadly as a general
2482 * context checking code. This tests GFP_FS recursion (a lock taken
2483 * during reclaim for a GFP_FS allocation is held over a GFP_FS
2484 * allocation).
2485 */
2486 if (!hlock->trylock && (curr->lockdep_reclaim_gfp & __GFP_FS)) {
2487 if (hlock->read) {
2488 if (!mark_lock(curr, hlock, LOCK_USED_IN_RECLAIM_FS_READ))
2489 return 0;
2490 } else {
2491 if (!mark_lock(curr, hlock, LOCK_USED_IN_RECLAIM_FS))
2492 return 0;
2493 }
2494 }
2495
8e18257d
PZ
2496 return 1;
2497}
2498
2499static int separate_irq_context(struct task_struct *curr,
2500 struct held_lock *hlock)
2501{
2502 unsigned int depth = curr->lockdep_depth;
2503
2504 /*
2505 * Keep track of points where we cross into an interrupt context:
2506 */
2507 hlock->irq_context = 2*(curr->hardirq_context ? 1 : 0) +
2508 curr->softirq_context;
2509 if (depth) {
2510 struct held_lock *prev_hlock;
2511
2512 prev_hlock = curr->held_locks + depth-1;
2513 /*
2514 * If we cross into another context, reset the
2515 * hash key (this also prevents the checking and the
2516 * adding of the dependency to 'prev'):
2517 */
2518 if (prev_hlock->irq_context != hlock->irq_context)
2519 return 1;
2520 }
2521 return 0;
fbb9ce95
IM
2522}
2523
8e18257d 2524#else
fbb9ce95 2525
8e18257d
PZ
2526static inline
2527int mark_lock_irq(struct task_struct *curr, struct held_lock *this,
2528 enum lock_usage_bit new_bit)
fbb9ce95 2529{
8e18257d
PZ
2530 WARN_ON(1);
2531 return 1;
2532}
fbb9ce95 2533
8e18257d
PZ
2534static inline int mark_irqflags(struct task_struct *curr,
2535 struct held_lock *hlock)
2536{
2537 return 1;
2538}
fbb9ce95 2539
8e18257d
PZ
2540static inline int separate_irq_context(struct task_struct *curr,
2541 struct held_lock *hlock)
2542{
2543 return 0;
fbb9ce95
IM
2544}
2545
8e18257d 2546#endif
fbb9ce95
IM
2547
2548/*
8e18257d 2549 * Mark a lock with a usage bit, and validate the state transition:
fbb9ce95 2550 */
1d09daa5 2551static int mark_lock(struct task_struct *curr, struct held_lock *this,
0764d23c 2552 enum lock_usage_bit new_bit)
fbb9ce95 2553{
8e18257d 2554 unsigned int new_mask = 1 << new_bit, ret = 1;
fbb9ce95
IM
2555
2556 /*
8e18257d
PZ
2557 * If already set then do not dirty the cacheline,
2558 * nor do any checks:
fbb9ce95 2559 */
f82b217e 2560 if (likely(hlock_class(this)->usage_mask & new_mask))
8e18257d
PZ
2561 return 1;
2562
2563 if (!graph_lock())
2564 return 0;
fbb9ce95 2565 /*
8e18257d 2566 * Make sure we didnt race:
fbb9ce95 2567 */
f82b217e 2568 if (unlikely(hlock_class(this)->usage_mask & new_mask)) {
8e18257d
PZ
2569 graph_unlock();
2570 return 1;
2571 }
fbb9ce95 2572
f82b217e 2573 hlock_class(this)->usage_mask |= new_mask;
fbb9ce95 2574
f82b217e 2575 if (!save_trace(hlock_class(this)->usage_traces + new_bit))
8e18257d 2576 return 0;
fbb9ce95 2577
8e18257d 2578 switch (new_bit) {
5346417e
PZ
2579#define LOCKDEP_STATE(__STATE) \
2580 case LOCK_USED_IN_##__STATE: \
2581 case LOCK_USED_IN_##__STATE##_READ: \
2582 case LOCK_ENABLED_##__STATE: \
2583 case LOCK_ENABLED_##__STATE##_READ:
2584#include "lockdep_states.h"
2585#undef LOCKDEP_STATE
8e18257d
PZ
2586 ret = mark_lock_irq(curr, this, new_bit);
2587 if (!ret)
2588 return 0;
2589 break;
2590 case LOCK_USED:
8e18257d
PZ
2591 debug_atomic_dec(&nr_unused_locks);
2592 break;
2593 default:
2594 if (!debug_locks_off_graph_unlock())
2595 return 0;
2596 WARN_ON(1);
2597 return 0;
2598 }
fbb9ce95 2599
8e18257d
PZ
2600 graph_unlock();
2601
2602 /*
2603 * We must printk outside of the graph_lock:
2604 */
2605 if (ret == 2) {
2606 printk("\nmarked lock as {%s}:\n", usage_str[new_bit]);
2607 print_lock(this);
2608 print_irqtrace_events(curr);
2609 dump_stack();
2610 }
2611
2612 return ret;
2613}
fbb9ce95
IM
2614
2615/*
2616 * Initialize a lock instance's lock-class mapping info:
2617 */
2618void lockdep_init_map(struct lockdep_map *lock, const char *name,
4dfbb9d8 2619 struct lock_class_key *key, int subclass)
fbb9ce95
IM
2620{
2621 if (unlikely(!debug_locks))
2622 return;
2623
2624 if (DEBUG_LOCKS_WARN_ON(!key))
2625 return;
2626 if (DEBUG_LOCKS_WARN_ON(!name))
2627 return;
2628 /*
2629 * Sanity check, the lock-class key must be persistent:
2630 */
2631 if (!static_obj(key)) {
2632 printk("BUG: key %p not in .data!\n", key);
2633 DEBUG_LOCKS_WARN_ON(1);
2634 return;
2635 }
2636 lock->name = name;
2637 lock->key = key;
d6d897ce 2638 lock->class_cache = NULL;
96645678
PZ
2639#ifdef CONFIG_LOCK_STAT
2640 lock->cpu = raw_smp_processor_id();
2641#endif
4dfbb9d8
PZ
2642 if (subclass)
2643 register_lock_class(lock, subclass, 1);
fbb9ce95 2644}
fbb9ce95
IM
2645EXPORT_SYMBOL_GPL(lockdep_init_map);
2646
2647/*
2648 * This gets called for every mutex_lock*()/spin_lock*() operation.
2649 * We maintain the dependency maps and validate the locking attempt:
2650 */
2651static int __lock_acquire(struct lockdep_map *lock, unsigned int subclass,
2652 int trylock, int read, int check, int hardirqs_off,
7531e2f3 2653 struct lockdep_map *nest_lock, unsigned long ip)
fbb9ce95
IM
2654{
2655 struct task_struct *curr = current;
d6d897ce 2656 struct lock_class *class = NULL;
fbb9ce95 2657 struct held_lock *hlock;
fbb9ce95
IM
2658 unsigned int depth, id;
2659 int chain_head = 0;
2660 u64 chain_key;
2661
f20786ff
PZ
2662 if (!prove_locking)
2663 check = 1;
2664
fbb9ce95
IM
2665 if (unlikely(!debug_locks))
2666 return 0;
2667
2668 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2669 return 0;
2670
2671 if (unlikely(subclass >= MAX_LOCKDEP_SUBCLASSES)) {
2672 debug_locks_off();
2673 printk("BUG: MAX_LOCKDEP_SUBCLASSES too low!\n");
2674 printk("turning off the locking correctness validator.\n");
2675 return 0;
2676 }
2677
d6d897ce
IM
2678 if (!subclass)
2679 class = lock->class_cache;
2680 /*
2681 * Not cached yet or subclass?
2682 */
fbb9ce95 2683 if (unlikely(!class)) {
4dfbb9d8 2684 class = register_lock_class(lock, subclass, 0);
fbb9ce95
IM
2685 if (!class)
2686 return 0;
2687 }
2688 debug_atomic_inc((atomic_t *)&class->ops);
2689 if (very_verbose(class)) {
2690 printk("\nacquire class [%p] %s", class->key, class->name);
2691 if (class->name_version > 1)
2692 printk("#%d", class->name_version);
2693 printk("\n");
2694 dump_stack();
2695 }
2696
2697 /*
2698 * Add the lock to the list of currently held locks.
2699 * (we dont increase the depth just yet, up until the
2700 * dependency checks are done)
2701 */
2702 depth = curr->lockdep_depth;
2703 if (DEBUG_LOCKS_WARN_ON(depth >= MAX_LOCK_DEPTH))
2704 return 0;
2705
2706 hlock = curr->held_locks + depth;
f82b217e
DJ
2707 if (DEBUG_LOCKS_WARN_ON(!class))
2708 return 0;
2709 hlock->class_idx = class - lock_classes + 1;
fbb9ce95
IM
2710 hlock->acquire_ip = ip;
2711 hlock->instance = lock;
7531e2f3 2712 hlock->nest_lock = nest_lock;
fbb9ce95
IM
2713 hlock->trylock = trylock;
2714 hlock->read = read;
2715 hlock->check = check;
6951b12a 2716 hlock->hardirqs_off = !!hardirqs_off;
f20786ff
PZ
2717#ifdef CONFIG_LOCK_STAT
2718 hlock->waittime_stamp = 0;
2719 hlock->holdtime_stamp = sched_clock();
2720#endif
fbb9ce95 2721
8e18257d
PZ
2722 if (check == 2 && !mark_irqflags(curr, hlock))
2723 return 0;
2724
fbb9ce95 2725 /* mark it as used: */
4ff773bb 2726 if (!mark_lock(curr, hlock, LOCK_USED))
fbb9ce95 2727 return 0;
8e18257d 2728
fbb9ce95 2729 /*
17aacfb9 2730 * Calculate the chain hash: it's the combined hash of all the
fbb9ce95
IM
2731 * lock keys along the dependency chain. We save the hash value
2732 * at every step so that we can get the current hash easily
2733 * after unlock. The chain hash is then used to cache dependency
2734 * results.
2735 *
2736 * The 'key ID' is what is the most compact key value to drive
2737 * the hash, not class->key.
2738 */
2739 id = class - lock_classes;
2740 if (DEBUG_LOCKS_WARN_ON(id >= MAX_LOCKDEP_KEYS))
2741 return 0;
2742
2743 chain_key = curr->curr_chain_key;
2744 if (!depth) {
2745 if (DEBUG_LOCKS_WARN_ON(chain_key != 0))
2746 return 0;
2747 chain_head = 1;
2748 }
2749
2750 hlock->prev_chain_key = chain_key;
8e18257d
PZ
2751 if (separate_irq_context(curr, hlock)) {
2752 chain_key = 0;
2753 chain_head = 1;
fbb9ce95 2754 }
fbb9ce95 2755 chain_key = iterate_chain_key(chain_key, id);
fbb9ce95 2756
3aa416b0 2757 if (!validate_chain(curr, lock, hlock, chain_head, chain_key))
8e18257d 2758 return 0;
381a2292 2759
3aa416b0 2760 curr->curr_chain_key = chain_key;
fbb9ce95
IM
2761 curr->lockdep_depth++;
2762 check_chain_key(curr);
60e114d1
JP
2763#ifdef CONFIG_DEBUG_LOCKDEP
2764 if (unlikely(!debug_locks))
2765 return 0;
2766#endif
fbb9ce95
IM
2767 if (unlikely(curr->lockdep_depth >= MAX_LOCK_DEPTH)) {
2768 debug_locks_off();
2769 printk("BUG: MAX_LOCK_DEPTH too low!\n");
2770 printk("turning off the locking correctness validator.\n");
2771 return 0;
2772 }
381a2292 2773
fbb9ce95
IM
2774 if (unlikely(curr->lockdep_depth > max_lockdep_depth))
2775 max_lockdep_depth = curr->lockdep_depth;
2776
2777 return 1;
2778}
2779
2780static int
2781print_unlock_inbalance_bug(struct task_struct *curr, struct lockdep_map *lock,
2782 unsigned long ip)
2783{
2784 if (!debug_locks_off())
2785 return 0;
2786 if (debug_locks_silent)
2787 return 0;
2788
2789 printk("\n=====================================\n");
2790 printk( "[ BUG: bad unlock balance detected! ]\n");
2791 printk( "-------------------------------------\n");
2792 printk("%s/%d is trying to release lock (",
ba25f9dc 2793 curr->comm, task_pid_nr(curr));
fbb9ce95
IM
2794 print_lockdep_cache(lock);
2795 printk(") at:\n");
2796 print_ip_sym(ip);
2797 printk("but there are no more locks to release!\n");
2798 printk("\nother info that might help us debug this:\n");
2799 lockdep_print_held_locks(curr);
2800
2801 printk("\nstack backtrace:\n");
2802 dump_stack();
2803
2804 return 0;
2805}
2806
2807/*
2808 * Common debugging checks for both nested and non-nested unlock:
2809 */
2810static int check_unlock(struct task_struct *curr, struct lockdep_map *lock,
2811 unsigned long ip)
2812{
2813 if (unlikely(!debug_locks))
2814 return 0;
2815 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2816 return 0;
2817
2818 if (curr->lockdep_depth <= 0)
2819 return print_unlock_inbalance_bug(curr, lock, ip);
2820
2821 return 1;
2822}
2823
64aa348e 2824static int
00ef9f73
PZ
2825__lock_set_class(struct lockdep_map *lock, const char *name,
2826 struct lock_class_key *key, unsigned int subclass,
2827 unsigned long ip)
64aa348e
PZ
2828{
2829 struct task_struct *curr = current;
2830 struct held_lock *hlock, *prev_hlock;
2831 struct lock_class *class;
2832 unsigned int depth;
2833 int i;
2834
2835 depth = curr->lockdep_depth;
2836 if (DEBUG_LOCKS_WARN_ON(!depth))
2837 return 0;
2838
2839 prev_hlock = NULL;
2840 for (i = depth-1; i >= 0; i--) {
2841 hlock = curr->held_locks + i;
2842 /*
2843 * We must not cross into another context:
2844 */
2845 if (prev_hlock && prev_hlock->irq_context != hlock->irq_context)
2846 break;
2847 if (hlock->instance == lock)
2848 goto found_it;
2849 prev_hlock = hlock;
2850 }
2851 return print_unlock_inbalance_bug(curr, lock, ip);
2852
2853found_it:
00ef9f73 2854 lockdep_init_map(lock, name, key, 0);
64aa348e 2855 class = register_lock_class(lock, subclass, 0);
f82b217e 2856 hlock->class_idx = class - lock_classes + 1;
64aa348e
PZ
2857
2858 curr->lockdep_depth = i;
2859 curr->curr_chain_key = hlock->prev_chain_key;
2860
2861 for (; i < depth; i++) {
2862 hlock = curr->held_locks + i;
2863 if (!__lock_acquire(hlock->instance,
f82b217e 2864 hlock_class(hlock)->subclass, hlock->trylock,
64aa348e 2865 hlock->read, hlock->check, hlock->hardirqs_off,
7531e2f3 2866 hlock->nest_lock, hlock->acquire_ip))
64aa348e
PZ
2867 return 0;
2868 }
2869
2870 if (DEBUG_LOCKS_WARN_ON(curr->lockdep_depth != depth))
2871 return 0;
2872 return 1;
2873}
2874
fbb9ce95
IM
2875/*
2876 * Remove the lock to the list of currently held locks in a
2877 * potentially non-nested (out of order) manner. This is a
2878 * relatively rare operation, as all the unlock APIs default
2879 * to nested mode (which uses lock_release()):
2880 */
2881static int
2882lock_release_non_nested(struct task_struct *curr,
2883 struct lockdep_map *lock, unsigned long ip)
2884{
2885 struct held_lock *hlock, *prev_hlock;
2886 unsigned int depth;
2887 int i;
2888
2889 /*
2890 * Check whether the lock exists in the current stack
2891 * of held locks:
2892 */
2893 depth = curr->lockdep_depth;
2894 if (DEBUG_LOCKS_WARN_ON(!depth))
2895 return 0;
2896
2897 prev_hlock = NULL;
2898 for (i = depth-1; i >= 0; i--) {
2899 hlock = curr->held_locks + i;
2900 /*
2901 * We must not cross into another context:
2902 */
2903 if (prev_hlock && prev_hlock->irq_context != hlock->irq_context)
2904 break;
2905 if (hlock->instance == lock)
2906 goto found_it;
2907 prev_hlock = hlock;
2908 }
2909 return print_unlock_inbalance_bug(curr, lock, ip);
2910
2911found_it:
f20786ff
PZ
2912 lock_release_holdtime(hlock);
2913
fbb9ce95
IM
2914 /*
2915 * We have the right lock to unlock, 'hlock' points to it.
2916 * Now we remove it from the stack, and add back the other
2917 * entries (if any), recalculating the hash along the way:
2918 */
2919 curr->lockdep_depth = i;
2920 curr->curr_chain_key = hlock->prev_chain_key;
2921
2922 for (i++; i < depth; i++) {
2923 hlock = curr->held_locks + i;
2924 if (!__lock_acquire(hlock->instance,
f82b217e 2925 hlock_class(hlock)->subclass, hlock->trylock,
fbb9ce95 2926 hlock->read, hlock->check, hlock->hardirqs_off,
7531e2f3 2927 hlock->nest_lock, hlock->acquire_ip))
fbb9ce95
IM
2928 return 0;
2929 }
2930
2931 if (DEBUG_LOCKS_WARN_ON(curr->lockdep_depth != depth - 1))
2932 return 0;
2933 return 1;
2934}
2935
2936/*
2937 * Remove the lock to the list of currently held locks - this gets
2938 * called on mutex_unlock()/spin_unlock*() (or on a failed
2939 * mutex_lock_interruptible()). This is done for unlocks that nest
2940 * perfectly. (i.e. the current top of the lock-stack is unlocked)
2941 */
2942static int lock_release_nested(struct task_struct *curr,
2943 struct lockdep_map *lock, unsigned long ip)
2944{
2945 struct held_lock *hlock;
2946 unsigned int depth;
2947
2948 /*
2949 * Pop off the top of the lock stack:
2950 */
2951 depth = curr->lockdep_depth - 1;
2952 hlock = curr->held_locks + depth;
2953
2954 /*
2955 * Is the unlock non-nested:
2956 */
2957 if (hlock->instance != lock)
2958 return lock_release_non_nested(curr, lock, ip);
2959 curr->lockdep_depth--;
2960
2961 if (DEBUG_LOCKS_WARN_ON(!depth && (hlock->prev_chain_key != 0)))
2962 return 0;
2963
2964 curr->curr_chain_key = hlock->prev_chain_key;
2965
f20786ff
PZ
2966 lock_release_holdtime(hlock);
2967
fbb9ce95
IM
2968#ifdef CONFIG_DEBUG_LOCKDEP
2969 hlock->prev_chain_key = 0;
f82b217e 2970 hlock->class_idx = 0;
fbb9ce95
IM
2971 hlock->acquire_ip = 0;
2972 hlock->irq_context = 0;
2973#endif
2974 return 1;
2975}
2976
2977/*
2978 * Remove the lock to the list of currently held locks - this gets
2979 * called on mutex_unlock()/spin_unlock*() (or on a failed
2980 * mutex_lock_interruptible()). This is done for unlocks that nest
2981 * perfectly. (i.e. the current top of the lock-stack is unlocked)
2982 */
2983static void
2984__lock_release(struct lockdep_map *lock, int nested, unsigned long ip)
2985{
2986 struct task_struct *curr = current;
2987
2988 if (!check_unlock(curr, lock, ip))
2989 return;
2990
2991 if (nested) {
2992 if (!lock_release_nested(curr, lock, ip))
2993 return;
2994 } else {
2995 if (!lock_release_non_nested(curr, lock, ip))
2996 return;
2997 }
2998
2999 check_chain_key(curr);
3000}
3001
3002/*
3003 * Check whether we follow the irq-flags state precisely:
3004 */
1d09daa5 3005static void check_flags(unsigned long flags)
fbb9ce95 3006{
992860e9
IM
3007#if defined(CONFIG_PROVE_LOCKING) && defined(CONFIG_DEBUG_LOCKDEP) && \
3008 defined(CONFIG_TRACE_IRQFLAGS)
fbb9ce95
IM
3009 if (!debug_locks)
3010 return;
3011
5f9fa8a6
IM
3012 if (irqs_disabled_flags(flags)) {
3013 if (DEBUG_LOCKS_WARN_ON(current->hardirqs_enabled)) {
3014 printk("possible reason: unannotated irqs-off.\n");
3015 }
3016 } else {
3017 if (DEBUG_LOCKS_WARN_ON(!current->hardirqs_enabled)) {
3018 printk("possible reason: unannotated irqs-on.\n");
3019 }
3020 }
fbb9ce95
IM
3021
3022 /*
3023 * We dont accurately track softirq state in e.g.
3024 * hardirq contexts (such as on 4KSTACKS), so only
3025 * check if not in hardirq contexts:
3026 */
3027 if (!hardirq_count()) {
3028 if (softirq_count())
3029 DEBUG_LOCKS_WARN_ON(current->softirqs_enabled);
3030 else
3031 DEBUG_LOCKS_WARN_ON(!current->softirqs_enabled);
3032 }
3033
3034 if (!debug_locks)
3035 print_irqtrace_events(current);
3036#endif
3037}
3038
00ef9f73
PZ
3039void lock_set_class(struct lockdep_map *lock, const char *name,
3040 struct lock_class_key *key, unsigned int subclass,
3041 unsigned long ip)
64aa348e
PZ
3042{
3043 unsigned long flags;
3044
3045 if (unlikely(current->lockdep_recursion))
3046 return;
3047
3048 raw_local_irq_save(flags);
3049 current->lockdep_recursion = 1;
3050 check_flags(flags);
00ef9f73 3051 if (__lock_set_class(lock, name, key, subclass, ip))
64aa348e
PZ
3052 check_chain_key(current);
3053 current->lockdep_recursion = 0;
3054 raw_local_irq_restore(flags);
3055}
00ef9f73 3056EXPORT_SYMBOL_GPL(lock_set_class);
64aa348e 3057
fbb9ce95
IM
3058/*
3059 * We are not always called with irqs disabled - do that here,
3060 * and also avoid lockdep recursion:
3061 */
1d09daa5 3062void lock_acquire(struct lockdep_map *lock, unsigned int subclass,
7531e2f3
PZ
3063 int trylock, int read, int check,
3064 struct lockdep_map *nest_lock, unsigned long ip)
fbb9ce95
IM
3065{
3066 unsigned long flags;
3067
3068 if (unlikely(current->lockdep_recursion))
3069 return;
3070
3071 raw_local_irq_save(flags);
3072 check_flags(flags);
3073
3074 current->lockdep_recursion = 1;
3075 __lock_acquire(lock, subclass, trylock, read, check,
7531e2f3 3076 irqs_disabled_flags(flags), nest_lock, ip);
fbb9ce95
IM
3077 current->lockdep_recursion = 0;
3078 raw_local_irq_restore(flags);
3079}
fbb9ce95
IM
3080EXPORT_SYMBOL_GPL(lock_acquire);
3081
1d09daa5 3082void lock_release(struct lockdep_map *lock, int nested,
0764d23c 3083 unsigned long ip)
fbb9ce95
IM
3084{
3085 unsigned long flags;
3086
3087 if (unlikely(current->lockdep_recursion))
3088 return;
3089
3090 raw_local_irq_save(flags);
3091 check_flags(flags);
3092 current->lockdep_recursion = 1;
3093 __lock_release(lock, nested, ip);
3094 current->lockdep_recursion = 0;
3095 raw_local_irq_restore(flags);
3096}
fbb9ce95
IM
3097EXPORT_SYMBOL_GPL(lock_release);
3098
cf40bd16
NP
3099void lockdep_set_current_reclaim_state(gfp_t gfp_mask)
3100{
3101 current->lockdep_reclaim_gfp = gfp_mask;
3102}
3103
3104void lockdep_clear_current_reclaim_state(void)
3105{
3106 current->lockdep_reclaim_gfp = 0;
3107}
3108
f20786ff
PZ
3109#ifdef CONFIG_LOCK_STAT
3110static int
3111print_lock_contention_bug(struct task_struct *curr, struct lockdep_map *lock,
3112 unsigned long ip)
3113{
3114 if (!debug_locks_off())
3115 return 0;
3116 if (debug_locks_silent)
3117 return 0;
3118
3119 printk("\n=================================\n");
3120 printk( "[ BUG: bad contention detected! ]\n");
3121 printk( "---------------------------------\n");
3122 printk("%s/%d is trying to contend lock (",
ba25f9dc 3123 curr->comm, task_pid_nr(curr));
f20786ff
PZ
3124 print_lockdep_cache(lock);
3125 printk(") at:\n");
3126 print_ip_sym(ip);
3127 printk("but there are no locks held!\n");
3128 printk("\nother info that might help us debug this:\n");
3129 lockdep_print_held_locks(curr);
3130
3131 printk("\nstack backtrace:\n");
3132 dump_stack();
3133
3134 return 0;
3135}
3136
3137static void
3138__lock_contended(struct lockdep_map *lock, unsigned long ip)
3139{
3140 struct task_struct *curr = current;
3141 struct held_lock *hlock, *prev_hlock;
3142 struct lock_class_stats *stats;
3143 unsigned int depth;
c7e78cff 3144 int i, contention_point, contending_point;
f20786ff
PZ
3145
3146 depth = curr->lockdep_depth;
3147 if (DEBUG_LOCKS_WARN_ON(!depth))
3148 return;
3149
3150 prev_hlock = NULL;
3151 for (i = depth-1; i >= 0; i--) {
3152 hlock = curr->held_locks + i;
3153 /*
3154 * We must not cross into another context:
3155 */
3156 if (prev_hlock && prev_hlock->irq_context != hlock->irq_context)
3157 break;
3158 if (hlock->instance == lock)
3159 goto found_it;
3160 prev_hlock = hlock;
3161 }
3162 print_lock_contention_bug(curr, lock, ip);
3163 return;
3164
3165found_it:
3166 hlock->waittime_stamp = sched_clock();
3167
c7e78cff
PZ
3168 contention_point = lock_point(hlock_class(hlock)->contention_point, ip);
3169 contending_point = lock_point(hlock_class(hlock)->contending_point,
3170 lock->ip);
f20786ff 3171
f82b217e 3172 stats = get_lock_stats(hlock_class(hlock));
c7e78cff
PZ
3173 if (contention_point < LOCKSTAT_POINTS)
3174 stats->contention_point[contention_point]++;
3175 if (contending_point < LOCKSTAT_POINTS)
3176 stats->contending_point[contending_point]++;
96645678
PZ
3177 if (lock->cpu != smp_processor_id())
3178 stats->bounces[bounce_contended + !!hlock->read]++;
f20786ff
PZ
3179 put_lock_stats(stats);
3180}
3181
3182static void
c7e78cff 3183__lock_acquired(struct lockdep_map *lock, unsigned long ip)
f20786ff
PZ
3184{
3185 struct task_struct *curr = current;
3186 struct held_lock *hlock, *prev_hlock;
3187 struct lock_class_stats *stats;
3188 unsigned int depth;
3189 u64 now;
96645678
PZ
3190 s64 waittime = 0;
3191 int i, cpu;
f20786ff
PZ
3192
3193 depth = curr->lockdep_depth;
3194 if (DEBUG_LOCKS_WARN_ON(!depth))
3195 return;
3196
3197 prev_hlock = NULL;
3198 for (i = depth-1; i >= 0; i--) {
3199 hlock = curr->held_locks + i;
3200 /*
3201 * We must not cross into another context:
3202 */
3203 if (prev_hlock && prev_hlock->irq_context != hlock->irq_context)
3204 break;
3205 if (hlock->instance == lock)
3206 goto found_it;
3207 prev_hlock = hlock;
3208 }
3209 print_lock_contention_bug(curr, lock, _RET_IP_);
3210 return;
3211
3212found_it:
96645678
PZ
3213 cpu = smp_processor_id();
3214 if (hlock->waittime_stamp) {
3215 now = sched_clock();
3216 waittime = now - hlock->waittime_stamp;
3217 hlock->holdtime_stamp = now;
3218 }
f20786ff 3219
f82b217e 3220 stats = get_lock_stats(hlock_class(hlock));
96645678
PZ
3221 if (waittime) {
3222 if (hlock->read)
3223 lock_time_inc(&stats->read_waittime, waittime);
3224 else
3225 lock_time_inc(&stats->write_waittime, waittime);
3226 }
3227 if (lock->cpu != cpu)
3228 stats->bounces[bounce_acquired + !!hlock->read]++;
f20786ff 3229 put_lock_stats(stats);
96645678
PZ
3230
3231 lock->cpu = cpu;
c7e78cff 3232 lock->ip = ip;
f20786ff
PZ
3233}
3234
3235void lock_contended(struct lockdep_map *lock, unsigned long ip)
3236{
3237 unsigned long flags;
3238
3239 if (unlikely(!lock_stat))
3240 return;
3241
3242 if (unlikely(current->lockdep_recursion))
3243 return;
3244
3245 raw_local_irq_save(flags);
3246 check_flags(flags);
3247 current->lockdep_recursion = 1;
3248 __lock_contended(lock, ip);
3249 current->lockdep_recursion = 0;
3250 raw_local_irq_restore(flags);
3251}
3252EXPORT_SYMBOL_GPL(lock_contended);
3253
c7e78cff 3254void lock_acquired(struct lockdep_map *lock, unsigned long ip)
f20786ff
PZ
3255{
3256 unsigned long flags;
3257
3258 if (unlikely(!lock_stat))
3259 return;
3260
3261 if (unlikely(current->lockdep_recursion))
3262 return;
3263
3264 raw_local_irq_save(flags);
3265 check_flags(flags);
3266 current->lockdep_recursion = 1;
c7e78cff 3267 __lock_acquired(lock, ip);
f20786ff
PZ
3268 current->lockdep_recursion = 0;
3269 raw_local_irq_restore(flags);
3270}
3271EXPORT_SYMBOL_GPL(lock_acquired);
3272#endif
3273
fbb9ce95
IM
3274/*
3275 * Used by the testsuite, sanitize the validator state
3276 * after a simulated failure:
3277 */
3278
3279void lockdep_reset(void)
3280{
3281 unsigned long flags;
23d95a03 3282 int i;
fbb9ce95
IM
3283
3284 raw_local_irq_save(flags);
3285 current->curr_chain_key = 0;
3286 current->lockdep_depth = 0;
3287 current->lockdep_recursion = 0;
3288 memset(current->held_locks, 0, MAX_LOCK_DEPTH*sizeof(struct held_lock));
3289 nr_hardirq_chains = 0;
3290 nr_softirq_chains = 0;
3291 nr_process_chains = 0;
3292 debug_locks = 1;
23d95a03
IM
3293 for (i = 0; i < CHAINHASH_SIZE; i++)
3294 INIT_LIST_HEAD(chainhash_table + i);
fbb9ce95
IM
3295 raw_local_irq_restore(flags);
3296}
3297
3298static void zap_class(struct lock_class *class)
3299{
3300 int i;
3301
3302 /*
3303 * Remove all dependencies this lock is
3304 * involved in:
3305 */
3306 for (i = 0; i < nr_list_entries; i++) {
3307 if (list_entries[i].class == class)
3308 list_del_rcu(&list_entries[i].entry);
3309 }
3310 /*
3311 * Unhash the class and remove it from the all_lock_classes list:
3312 */
3313 list_del_rcu(&class->hash_entry);
3314 list_del_rcu(&class->lock_entry);
3315
8bfe0298 3316 class->key = NULL;
fbb9ce95
IM
3317}
3318
fabe874a 3319static inline int within(const void *addr, void *start, unsigned long size)
fbb9ce95
IM
3320{
3321 return addr >= start && addr < start + size;
3322}
3323
3324void lockdep_free_key_range(void *start, unsigned long size)
3325{
3326 struct lock_class *class, *next;
3327 struct list_head *head;
3328 unsigned long flags;
3329 int i;
5a26db5b 3330 int locked;
fbb9ce95
IM
3331
3332 raw_local_irq_save(flags);
5a26db5b 3333 locked = graph_lock();
fbb9ce95
IM
3334
3335 /*
3336 * Unhash all classes that were created by this module:
3337 */
3338 for (i = 0; i < CLASSHASH_SIZE; i++) {
3339 head = classhash_table + i;
3340 if (list_empty(head))
3341 continue;
fabe874a 3342 list_for_each_entry_safe(class, next, head, hash_entry) {
fbb9ce95
IM
3343 if (within(class->key, start, size))
3344 zap_class(class);
fabe874a
AV
3345 else if (within(class->name, start, size))
3346 zap_class(class);
3347 }
fbb9ce95
IM
3348 }
3349
5a26db5b
NP
3350 if (locked)
3351 graph_unlock();
fbb9ce95
IM
3352 raw_local_irq_restore(flags);
3353}
3354
3355void lockdep_reset_lock(struct lockdep_map *lock)
3356{
d6d897ce 3357 struct lock_class *class, *next;
fbb9ce95
IM
3358 struct list_head *head;
3359 unsigned long flags;
3360 int i, j;
5a26db5b 3361 int locked;
fbb9ce95
IM
3362
3363 raw_local_irq_save(flags);
fbb9ce95
IM
3364
3365 /*
d6d897ce
IM
3366 * Remove all classes this lock might have:
3367 */
3368 for (j = 0; j < MAX_LOCKDEP_SUBCLASSES; j++) {
3369 /*
3370 * If the class exists we look it up and zap it:
3371 */
3372 class = look_up_lock_class(lock, j);
3373 if (class)
3374 zap_class(class);
3375 }
3376 /*
3377 * Debug check: in the end all mapped classes should
3378 * be gone.
fbb9ce95 3379 */
5a26db5b 3380 locked = graph_lock();
fbb9ce95
IM
3381 for (i = 0; i < CLASSHASH_SIZE; i++) {
3382 head = classhash_table + i;
3383 if (list_empty(head))
3384 continue;
3385 list_for_each_entry_safe(class, next, head, hash_entry) {
d6d897ce 3386 if (unlikely(class == lock->class_cache)) {
74c383f1
IM
3387 if (debug_locks_off_graph_unlock())
3388 WARN_ON(1);
d6d897ce 3389 goto out_restore;
fbb9ce95
IM
3390 }
3391 }
3392 }
5a26db5b
NP
3393 if (locked)
3394 graph_unlock();
d6d897ce
IM
3395
3396out_restore:
fbb9ce95
IM
3397 raw_local_irq_restore(flags);
3398}
3399
1499993c 3400void lockdep_init(void)
fbb9ce95
IM
3401{
3402 int i;
3403
3404 /*
3405 * Some architectures have their own start_kernel()
3406 * code which calls lockdep_init(), while we also
3407 * call lockdep_init() from the start_kernel() itself,
3408 * and we want to initialize the hashes only once:
3409 */
3410 if (lockdep_initialized)
3411 return;
3412
3413 for (i = 0; i < CLASSHASH_SIZE; i++)
3414 INIT_LIST_HEAD(classhash_table + i);
3415
3416 for (i = 0; i < CHAINHASH_SIZE; i++)
3417 INIT_LIST_HEAD(chainhash_table + i);
3418
3419 lockdep_initialized = 1;
3420}
3421
3422void __init lockdep_info(void)
3423{
3424 printk("Lock dependency validator: Copyright (c) 2006 Red Hat, Inc., Ingo Molnar\n");
3425
b0788caf 3426 printk("... MAX_LOCKDEP_SUBCLASSES: %lu\n", MAX_LOCKDEP_SUBCLASSES);
fbb9ce95
IM
3427 printk("... MAX_LOCK_DEPTH: %lu\n", MAX_LOCK_DEPTH);
3428 printk("... MAX_LOCKDEP_KEYS: %lu\n", MAX_LOCKDEP_KEYS);
b0788caf 3429 printk("... CLASSHASH_SIZE: %lu\n", CLASSHASH_SIZE);
fbb9ce95
IM
3430 printk("... MAX_LOCKDEP_ENTRIES: %lu\n", MAX_LOCKDEP_ENTRIES);
3431 printk("... MAX_LOCKDEP_CHAINS: %lu\n", MAX_LOCKDEP_CHAINS);
3432 printk("... CHAINHASH_SIZE: %lu\n", CHAINHASH_SIZE);
3433
3434 printk(" memory used by lock dependency info: %lu kB\n",
3435 (sizeof(struct lock_class) * MAX_LOCKDEP_KEYS +
3436 sizeof(struct list_head) * CLASSHASH_SIZE +
3437 sizeof(struct lock_list) * MAX_LOCKDEP_ENTRIES +
3438 sizeof(struct lock_chain) * MAX_LOCKDEP_CHAINS +
3439 sizeof(struct list_head) * CHAINHASH_SIZE) / 1024);
3440
3441 printk(" per task-struct memory footprint: %lu bytes\n",
3442 sizeof(struct held_lock) * MAX_LOCK_DEPTH);
3443
3444#ifdef CONFIG_DEBUG_LOCKDEP
c71063c9
JB
3445 if (lockdep_init_error) {
3446 printk("WARNING: lockdep init error! Arch code didn't call lockdep_init() early enough?\n");
3447 printk("Call stack leading to lockdep invocation was:\n");
3448 print_stack_trace(&lockdep_init_trace, 0);
3449 }
fbb9ce95
IM
3450#endif
3451}
3452
fbb9ce95
IM
3453static void
3454print_freed_lock_bug(struct task_struct *curr, const void *mem_from,
55794a41 3455 const void *mem_to, struct held_lock *hlock)
fbb9ce95
IM
3456{
3457 if (!debug_locks_off())
3458 return;
3459 if (debug_locks_silent)
3460 return;
3461
3462 printk("\n=========================\n");
3463 printk( "[ BUG: held lock freed! ]\n");
3464 printk( "-------------------------\n");
3465 printk("%s/%d is freeing memory %p-%p, with a lock still held there!\n",
ba25f9dc 3466 curr->comm, task_pid_nr(curr), mem_from, mem_to-1);
55794a41 3467 print_lock(hlock);
fbb9ce95
IM
3468 lockdep_print_held_locks(curr);
3469
3470 printk("\nstack backtrace:\n");
3471 dump_stack();
3472}
3473
54561783
ON
3474static inline int not_in_range(const void* mem_from, unsigned long mem_len,
3475 const void* lock_from, unsigned long lock_len)
3476{
3477 return lock_from + lock_len <= mem_from ||
3478 mem_from + mem_len <= lock_from;
3479}
3480
fbb9ce95
IM
3481/*
3482 * Called when kernel memory is freed (or unmapped), or if a lock
3483 * is destroyed or reinitialized - this code checks whether there is
3484 * any held lock in the memory range of <from> to <to>:
3485 */
3486void debug_check_no_locks_freed(const void *mem_from, unsigned long mem_len)
3487{
fbb9ce95
IM
3488 struct task_struct *curr = current;
3489 struct held_lock *hlock;
3490 unsigned long flags;
3491 int i;
3492
3493 if (unlikely(!debug_locks))
3494 return;
3495
3496 local_irq_save(flags);
3497 for (i = 0; i < curr->lockdep_depth; i++) {
3498 hlock = curr->held_locks + i;
3499
54561783
ON
3500 if (not_in_range(mem_from, mem_len, hlock->instance,
3501 sizeof(*hlock->instance)))
fbb9ce95
IM
3502 continue;
3503
54561783 3504 print_freed_lock_bug(curr, mem_from, mem_from + mem_len, hlock);
fbb9ce95
IM
3505 break;
3506 }
3507 local_irq_restore(flags);
3508}
ed07536e 3509EXPORT_SYMBOL_GPL(debug_check_no_locks_freed);
fbb9ce95
IM
3510
3511static void print_held_locks_bug(struct task_struct *curr)
3512{
3513 if (!debug_locks_off())
3514 return;
3515 if (debug_locks_silent)
3516 return;
3517
3518 printk("\n=====================================\n");
3519 printk( "[ BUG: lock held at task exit time! ]\n");
3520 printk( "-------------------------------------\n");
3521 printk("%s/%d is exiting with locks still held!\n",
ba25f9dc 3522 curr->comm, task_pid_nr(curr));
fbb9ce95
IM
3523 lockdep_print_held_locks(curr);
3524
3525 printk("\nstack backtrace:\n");
3526 dump_stack();
3527}
3528
3529void debug_check_no_locks_held(struct task_struct *task)
3530{
3531 if (unlikely(task->lockdep_depth > 0))
3532 print_held_locks_bug(task);
3533}
3534
3535void debug_show_all_locks(void)
3536{
3537 struct task_struct *g, *p;
3538 int count = 10;
3539 int unlock = 1;
3540
9c35dd7f
JP
3541 if (unlikely(!debug_locks)) {
3542 printk("INFO: lockdep is turned off.\n");
3543 return;
3544 }
fbb9ce95
IM
3545 printk("\nShowing all locks held in the system:\n");
3546
3547 /*
3548 * Here we try to get the tasklist_lock as hard as possible,
3549 * if not successful after 2 seconds we ignore it (but keep
3550 * trying). This is to enable a debug printout even if a
3551 * tasklist_lock-holding task deadlocks or crashes.
3552 */
3553retry:
3554 if (!read_trylock(&tasklist_lock)) {
3555 if (count == 10)
3556 printk("hm, tasklist_lock locked, retrying... ");
3557 if (count) {
3558 count--;
3559 printk(" #%d", 10-count);
3560 mdelay(200);
3561 goto retry;
3562 }
3563 printk(" ignoring it.\n");
3564 unlock = 0;
46fec7ac 3565 } else {
3566 if (count != 10)
3567 printk(KERN_CONT " locked it.\n");
fbb9ce95 3568 }
fbb9ce95
IM
3569
3570 do_each_thread(g, p) {
85684873
IM
3571 /*
3572 * It's not reliable to print a task's held locks
3573 * if it's not sleeping (or if it's not the current
3574 * task):
3575 */
3576 if (p->state == TASK_RUNNING && p != current)
3577 continue;
fbb9ce95
IM
3578 if (p->lockdep_depth)
3579 lockdep_print_held_locks(p);
3580 if (!unlock)
3581 if (read_trylock(&tasklist_lock))
3582 unlock = 1;
3583 } while_each_thread(g, p);
3584
3585 printk("\n");
3586 printk("=============================================\n\n");
3587
3588 if (unlock)
3589 read_unlock(&tasklist_lock);
3590}
fbb9ce95
IM
3591EXPORT_SYMBOL_GPL(debug_show_all_locks);
3592
82a1fcb9
IM
3593/*
3594 * Careful: only use this function if you are sure that
3595 * the task cannot run in parallel!
3596 */
3597void __debug_show_held_locks(struct task_struct *task)
fbb9ce95 3598{
9c35dd7f
JP
3599 if (unlikely(!debug_locks)) {
3600 printk("INFO: lockdep is turned off.\n");
3601 return;
3602 }
fbb9ce95
IM
3603 lockdep_print_held_locks(task);
3604}
82a1fcb9
IM
3605EXPORT_SYMBOL_GPL(__debug_show_held_locks);
3606
3607void debug_show_held_locks(struct task_struct *task)
3608{
3609 __debug_show_held_locks(task);
3610}
fbb9ce95 3611EXPORT_SYMBOL_GPL(debug_show_held_locks);
b351d164
PZ
3612
3613void lockdep_sys_exit(void)
3614{
3615 struct task_struct *curr = current;
3616
3617 if (unlikely(curr->lockdep_depth)) {
3618 if (!debug_locks_off())
3619 return;
3620 printk("\n================================================\n");
3621 printk( "[ BUG: lock held when returning to user space! ]\n");
3622 printk( "------------------------------------------------\n");
3623 printk("%s/%d is leaving the kernel with locks still held!\n",
3624 curr->comm, curr->pid);
3625 lockdep_print_held_locks(curr);
3626 }
3627}