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