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