Luca's patch ported
[cbs-scheduler.git] / kernel / lockdep.c
blob2859ae3885fc96878a5c53a0ec9b0e0b2cf42288
1 /*
2 * kernel/lockdep.c
4 * Runtime locking correctness validator
6 * Started by Ingo Molnar:
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>
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:
14 * - lock inversion scenarios
15 * - circular lock dependencies
16 * - hardirq/softirq safe/unsafe locking bugs
18 * Bugs are reported even if the current locking scenario does not cause
19 * any deadlock at this point.
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.
25 * Thanks to Arjan van de Ven for coming up with the initial idea of
26 * mapping lock dependencies runtime.
28 #define DISABLE_BRANCH_PROFILING
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>
41 #include <linux/utsname.h>
42 #include <linux/hash.h>
43 #include <linux/ftrace.h>
44 #include <linux/stringify.h>
45 #include <trace/lockdep.h>
47 #include <asm/sections.h>
49 #include "lockdep_internals.h"
51 #ifdef CONFIG_PROVE_LOCKING
52 int prove_locking = 1;
53 module_param(prove_locking, int, 0644);
54 #else
55 #define prove_locking 0
56 #endif
58 #ifdef CONFIG_LOCK_STAT
59 int lock_stat = 1;
60 module_param(lock_stat, int, 0644);
61 #else
62 #define lock_stat 0
63 #endif
66 * lockdep_lock: protects the lockdep graph, the hashes and the
67 * class/list/hash allocators.
69 * This is one of the rare exceptions where it's justified
70 * to use a raw spinlock - we really dont want the spinlock
71 * code to recurse back into the lockdep code...
73 static __raw_spinlock_t lockdep_lock = (__raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED;
75 static int graph_lock(void)
77 __raw_spin_lock(&lockdep_lock);
79 * Make sure that if another CPU detected a bug while
80 * walking the graph we dont change it (while the other
81 * CPU is busy printing out stuff with the graph lock
82 * dropped already)
84 if (!debug_locks) {
85 __raw_spin_unlock(&lockdep_lock);
86 return 0;
88 /* prevent any recursions within lockdep from causing deadlocks */
89 current->lockdep_recursion++;
90 return 1;
93 static inline int graph_unlock(void)
95 if (debug_locks && !__raw_spin_is_locked(&lockdep_lock))
96 return DEBUG_LOCKS_WARN_ON(1);
98 current->lockdep_recursion--;
99 __raw_spin_unlock(&lockdep_lock);
100 return 0;
104 * Turn lock debugging off and return with 0 if it was off already,
105 * and also release the graph lock:
107 static inline int debug_locks_off_graph_unlock(void)
109 int ret = debug_locks_off();
111 __raw_spin_unlock(&lockdep_lock);
113 return ret;
116 static int lockdep_initialized;
118 unsigned long nr_list_entries;
119 static struct lock_list list_entries[MAX_LOCKDEP_ENTRIES];
122 * All data structures here are protected by the global debug_lock.
124 * Mutex key structs only get allocated, once during bootup, and never
125 * get freed - this significantly simplifies the debugging code.
127 unsigned long nr_lock_classes;
128 static struct lock_class lock_classes[MAX_LOCKDEP_KEYS];
130 static inline struct lock_class *hlock_class(struct held_lock *hlock)
132 if (!hlock->class_idx) {
133 DEBUG_LOCKS_WARN_ON(1);
134 return NULL;
136 return lock_classes + hlock->class_idx - 1;
139 #ifdef CONFIG_LOCK_STAT
140 static DEFINE_PER_CPU(struct lock_class_stats[MAX_LOCKDEP_KEYS], lock_stats);
142 static int lock_point(unsigned long points[], unsigned long ip)
144 int i;
146 for (i = 0; i < LOCKSTAT_POINTS; i++) {
147 if (points[i] == 0) {
148 points[i] = ip;
149 break;
151 if (points[i] == ip)
152 break;
155 return i;
158 static void lock_time_inc(struct lock_time *lt, s64 time)
160 if (time > lt->max)
161 lt->max = time;
163 if (time < lt->min || !lt->min)
164 lt->min = time;
166 lt->total += time;
167 lt->nr++;
170 static inline void lock_time_add(struct lock_time *src, struct lock_time *dst)
172 dst->min += src->min;
173 dst->max += src->max;
174 dst->total += src->total;
175 dst->nr += src->nr;
178 struct lock_class_stats lock_stats(struct lock_class *class)
180 struct lock_class_stats stats;
181 int cpu, i;
183 memset(&stats, 0, sizeof(struct lock_class_stats));
184 for_each_possible_cpu(cpu) {
185 struct lock_class_stats *pcs =
186 &per_cpu(lock_stats, cpu)[class - lock_classes];
188 for (i = 0; i < ARRAY_SIZE(stats.contention_point); i++)
189 stats.contention_point[i] += pcs->contention_point[i];
191 for (i = 0; i < ARRAY_SIZE(stats.contending_point); i++)
192 stats.contending_point[i] += pcs->contending_point[i];
194 lock_time_add(&pcs->read_waittime, &stats.read_waittime);
195 lock_time_add(&pcs->write_waittime, &stats.write_waittime);
197 lock_time_add(&pcs->read_holdtime, &stats.read_holdtime);
198 lock_time_add(&pcs->write_holdtime, &stats.write_holdtime);
200 for (i = 0; i < ARRAY_SIZE(stats.bounces); i++)
201 stats.bounces[i] += pcs->bounces[i];
204 return stats;
207 void clear_lock_stats(struct lock_class *class)
209 int cpu;
211 for_each_possible_cpu(cpu) {
212 struct lock_class_stats *cpu_stats =
213 &per_cpu(lock_stats, cpu)[class - lock_classes];
215 memset(cpu_stats, 0, sizeof(struct lock_class_stats));
217 memset(class->contention_point, 0, sizeof(class->contention_point));
218 memset(class->contending_point, 0, sizeof(class->contending_point));
221 static struct lock_class_stats *get_lock_stats(struct lock_class *class)
223 return &get_cpu_var(lock_stats)[class - lock_classes];
226 static void put_lock_stats(struct lock_class_stats *stats)
228 put_cpu_var(lock_stats);
231 static void lock_release_holdtime(struct held_lock *hlock)
233 struct lock_class_stats *stats;
234 s64 holdtime;
236 if (!lock_stat)
237 return;
239 holdtime = sched_clock() - hlock->holdtime_stamp;
241 stats = get_lock_stats(hlock_class(hlock));
242 if (hlock->read)
243 lock_time_inc(&stats->read_holdtime, holdtime);
244 else
245 lock_time_inc(&stats->write_holdtime, holdtime);
246 put_lock_stats(stats);
248 #else
249 static inline void lock_release_holdtime(struct held_lock *hlock)
252 #endif
255 * We keep a global list of all lock classes. The list only grows,
256 * never shrinks. The list is only accessed with the lockdep
257 * spinlock lock held.
259 LIST_HEAD(all_lock_classes);
262 * The lockdep classes are in a hash-table as well, for fast lookup:
264 #define CLASSHASH_BITS (MAX_LOCKDEP_KEYS_BITS - 1)
265 #define CLASSHASH_SIZE (1UL << CLASSHASH_BITS)
266 #define __classhashfn(key) hash_long((unsigned long)key, CLASSHASH_BITS)
267 #define classhashentry(key) (classhash_table + __classhashfn((key)))
269 static struct list_head classhash_table[CLASSHASH_SIZE];
272 * We put the lock dependency chains into a hash-table as well, to cache
273 * their existence:
275 #define CHAINHASH_BITS (MAX_LOCKDEP_CHAINS_BITS-1)
276 #define CHAINHASH_SIZE (1UL << CHAINHASH_BITS)
277 #define __chainhashfn(chain) hash_long(chain, CHAINHASH_BITS)
278 #define chainhashentry(chain) (chainhash_table + __chainhashfn((chain)))
280 static struct list_head chainhash_table[CHAINHASH_SIZE];
283 * The hash key of the lock dependency chains is a hash itself too:
284 * it's a hash of all locks taken up to that lock, including that lock.
285 * It's a 64-bit hash, because it's important for the keys to be
286 * unique.
288 #define iterate_chain_key(key1, key2) \
289 (((key1) << MAX_LOCKDEP_KEYS_BITS) ^ \
290 ((key1) >> (64-MAX_LOCKDEP_KEYS_BITS)) ^ \
291 (key2))
293 void lockdep_off(void)
295 current->lockdep_recursion++;
297 EXPORT_SYMBOL(lockdep_off);
299 void lockdep_on(void)
301 current->lockdep_recursion--;
303 EXPORT_SYMBOL(lockdep_on);
306 * Debugging switches:
309 #define VERBOSE 0
310 #define VERY_VERBOSE 0
312 #if VERBOSE
313 # define HARDIRQ_VERBOSE 1
314 # define SOFTIRQ_VERBOSE 1
315 # define RECLAIM_VERBOSE 1
316 #else
317 # define HARDIRQ_VERBOSE 0
318 # define SOFTIRQ_VERBOSE 0
319 # define RECLAIM_VERBOSE 0
320 #endif
322 #if VERBOSE || HARDIRQ_VERBOSE || SOFTIRQ_VERBOSE || RECLAIM_VERBOSE
324 * Quick filtering for interesting events:
326 static int class_filter(struct lock_class *class)
328 #if 0
329 /* Example */
330 if (class->name_version == 1 &&
331 !strcmp(class->name, "lockname"))
332 return 1;
333 if (class->name_version == 1 &&
334 !strcmp(class->name, "&struct->lockfield"))
335 return 1;
336 #endif
337 /* Filter everything else. 1 would be to allow everything else */
338 return 0;
340 #endif
342 static int verbose(struct lock_class *class)
344 #if VERBOSE
345 return class_filter(class);
346 #endif
347 return 0;
351 * Stack-trace: tightly packed array of stack backtrace
352 * addresses. Protected by the graph_lock.
354 unsigned long nr_stack_trace_entries;
355 static unsigned long stack_trace[MAX_STACK_TRACE_ENTRIES];
357 static int save_trace(struct stack_trace *trace)
359 trace->nr_entries = 0;
360 trace->max_entries = MAX_STACK_TRACE_ENTRIES - nr_stack_trace_entries;
361 trace->entries = stack_trace + nr_stack_trace_entries;
363 trace->skip = 3;
365 save_stack_trace(trace);
367 trace->max_entries = trace->nr_entries;
369 nr_stack_trace_entries += trace->nr_entries;
371 if (nr_stack_trace_entries == MAX_STACK_TRACE_ENTRIES) {
372 if (!debug_locks_off_graph_unlock())
373 return 0;
375 printk("BUG: MAX_STACK_TRACE_ENTRIES too low!\n");
376 printk("turning off the locking correctness validator.\n");
377 dump_stack();
379 return 0;
382 return 1;
385 unsigned int nr_hardirq_chains;
386 unsigned int nr_softirq_chains;
387 unsigned int nr_process_chains;
388 unsigned int max_lockdep_depth;
389 unsigned int max_recursion_depth;
391 static unsigned int lockdep_dependency_gen_id;
393 static bool lockdep_dependency_visit(struct lock_class *source,
394 unsigned int depth)
396 if (!depth)
397 lockdep_dependency_gen_id++;
398 if (source->dep_gen_id == lockdep_dependency_gen_id)
399 return true;
400 source->dep_gen_id = lockdep_dependency_gen_id;
401 return false;
404 #ifdef CONFIG_DEBUG_LOCKDEP
406 * We cannot printk in early bootup code. Not even early_printk()
407 * might work. So we mark any initialization errors and printk
408 * about it later on, in lockdep_info().
410 static int lockdep_init_error;
411 static unsigned long lockdep_init_trace_data[20];
412 static struct stack_trace lockdep_init_trace = {
413 .max_entries = ARRAY_SIZE(lockdep_init_trace_data),
414 .entries = lockdep_init_trace_data,
418 * Various lockdep statistics:
420 atomic_t chain_lookup_hits;
421 atomic_t chain_lookup_misses;
422 atomic_t hardirqs_on_events;
423 atomic_t hardirqs_off_events;
424 atomic_t redundant_hardirqs_on;
425 atomic_t redundant_hardirqs_off;
426 atomic_t softirqs_on_events;
427 atomic_t softirqs_off_events;
428 atomic_t redundant_softirqs_on;
429 atomic_t redundant_softirqs_off;
430 atomic_t nr_unused_locks;
431 atomic_t nr_cyclic_checks;
432 atomic_t nr_cyclic_check_recursions;
433 atomic_t nr_find_usage_forwards_checks;
434 atomic_t nr_find_usage_forwards_recursions;
435 atomic_t nr_find_usage_backwards_checks;
436 atomic_t nr_find_usage_backwards_recursions;
437 #endif
440 * Locking printouts:
443 #define __USAGE(__STATE) \
444 [LOCK_USED_IN_##__STATE] = "IN-"__stringify(__STATE)"-W", \
445 [LOCK_ENABLED_##__STATE] = __stringify(__STATE)"-ON-W", \
446 [LOCK_USED_IN_##__STATE##_READ] = "IN-"__stringify(__STATE)"-R",\
447 [LOCK_ENABLED_##__STATE##_READ] = __stringify(__STATE)"-ON-R",
449 static const char *usage_str[] =
451 #define LOCKDEP_STATE(__STATE) __USAGE(__STATE)
452 #include "lockdep_states.h"
453 #undef LOCKDEP_STATE
454 [LOCK_USED] = "INITIAL USE",
457 const char * __get_key_name(struct lockdep_subclass_key *key, char *str)
459 return kallsyms_lookup((unsigned long)key, NULL, NULL, NULL, str);
462 static inline unsigned long lock_flag(enum lock_usage_bit bit)
464 return 1UL << bit;
467 static char get_usage_char(struct lock_class *class, enum lock_usage_bit bit)
469 char c = '.';
471 if (class->usage_mask & lock_flag(bit + 2))
472 c = '+';
473 if (class->usage_mask & lock_flag(bit)) {
474 c = '-';
475 if (class->usage_mask & lock_flag(bit + 2))
476 c = '?';
479 return c;
482 void get_usage_chars(struct lock_class *class, char usage[LOCK_USAGE_CHARS])
484 int i = 0;
486 #define LOCKDEP_STATE(__STATE) \
487 usage[i++] = get_usage_char(class, LOCK_USED_IN_##__STATE); \
488 usage[i++] = get_usage_char(class, LOCK_USED_IN_##__STATE##_READ);
489 #include "lockdep_states.h"
490 #undef LOCKDEP_STATE
492 usage[i] = '\0';
495 static void print_lock_name(struct lock_class *class)
497 char str[KSYM_NAME_LEN], usage[LOCK_USAGE_CHARS];
498 const char *name;
500 get_usage_chars(class, usage);
502 name = class->name;
503 if (!name) {
504 name = __get_key_name(class->key, str);
505 printk(" (%s", name);
506 } else {
507 printk(" (%s", name);
508 if (class->name_version > 1)
509 printk("#%d", class->name_version);
510 if (class->subclass)
511 printk("/%d", class->subclass);
513 printk("){%s}", usage);
516 static void print_lockdep_cache(struct lockdep_map *lock)
518 const char *name;
519 char str[KSYM_NAME_LEN];
521 name = lock->name;
522 if (!name)
523 name = __get_key_name(lock->key->subkeys, str);
525 printk("%s", name);
528 static void print_lock(struct held_lock *hlock)
530 print_lock_name(hlock_class(hlock));
531 printk(", at: ");
532 print_ip_sym(hlock->acquire_ip);
535 static void lockdep_print_held_locks(struct task_struct *curr)
537 int i, depth = curr->lockdep_depth;
539 if (!depth) {
540 printk("no locks held by %s/%d.\n", curr->comm, task_pid_nr(curr));
541 return;
543 printk("%d lock%s held by %s/%d:\n",
544 depth, depth > 1 ? "s" : "", curr->comm, task_pid_nr(curr));
546 for (i = 0; i < depth; i++) {
547 printk(" #%d: ", i);
548 print_lock(curr->held_locks + i);
552 static void print_lock_class_header(struct lock_class *class, int depth)
554 int bit;
556 printk("%*s->", depth, "");
557 print_lock_name(class);
558 printk(" ops: %lu", class->ops);
559 printk(" {\n");
561 for (bit = 0; bit < LOCK_USAGE_STATES; bit++) {
562 if (class->usage_mask & (1 << bit)) {
563 int len = depth;
565 len += printk("%*s %s", depth, "", usage_str[bit]);
566 len += printk(" at:\n");
567 print_stack_trace(class->usage_traces + bit, len);
570 printk("%*s }\n", depth, "");
572 printk("%*s ... key at: ",depth,"");
573 print_ip_sym((unsigned long)class->key);
577 * printk all lock dependencies starting at <entry>:
579 static void __used
580 print_lock_dependencies(struct lock_class *class, int depth)
582 struct lock_list *entry;
584 if (lockdep_dependency_visit(class, depth))
585 return;
587 if (DEBUG_LOCKS_WARN_ON(depth >= 20))
588 return;
590 print_lock_class_header(class, depth);
592 list_for_each_entry(entry, &class->locks_after, entry) {
593 if (DEBUG_LOCKS_WARN_ON(!entry->class))
594 return;
596 print_lock_dependencies(entry->class, depth + 1);
598 printk("%*s ... acquired at:\n",depth,"");
599 print_stack_trace(&entry->trace, 2);
600 printk("\n");
604 static void print_kernel_version(void)
606 printk("%s %.*s\n", init_utsname()->release,
607 (int)strcspn(init_utsname()->version, " "),
608 init_utsname()->version);
611 static int very_verbose(struct lock_class *class)
613 #if VERY_VERBOSE
614 return class_filter(class);
615 #endif
616 return 0;
620 * Is this the address of a static object:
622 static int static_obj(void *obj)
624 unsigned long start = (unsigned long) &_stext,
625 end = (unsigned long) &_end,
626 addr = (unsigned long) obj;
627 #ifdef CONFIG_SMP
628 int i;
629 #endif
632 * static variable?
634 if ((addr >= start) && (addr < end))
635 return 1;
637 #ifdef CONFIG_SMP
639 * percpu var?
641 for_each_possible_cpu(i) {
642 start = (unsigned long) &__per_cpu_start + per_cpu_offset(i);
643 end = (unsigned long) &__per_cpu_start + PERCPU_ENOUGH_ROOM
644 + per_cpu_offset(i);
646 if ((addr >= start) && (addr < end))
647 return 1;
649 #endif
652 * module var?
654 return is_module_address(addr);
658 * To make lock name printouts unique, we calculate a unique
659 * class->name_version generation counter:
661 static int count_matching_names(struct lock_class *new_class)
663 struct lock_class *class;
664 int count = 0;
666 if (!new_class->name)
667 return 0;
669 list_for_each_entry(class, &all_lock_classes, lock_entry) {
670 if (new_class->key - new_class->subclass == class->key)
671 return class->name_version;
672 if (class->name && !strcmp(class->name, new_class->name))
673 count = max(count, class->name_version);
676 return count + 1;
680 * Register a lock's class in the hash-table, if the class is not present
681 * yet. Otherwise we look it up. We cache the result in the lock object
682 * itself, so actual lookup of the hash should be once per lock object.
684 static inline struct lock_class *
685 look_up_lock_class(struct lockdep_map *lock, unsigned int subclass)
687 struct lockdep_subclass_key *key;
688 struct list_head *hash_head;
689 struct lock_class *class;
691 #ifdef CONFIG_DEBUG_LOCKDEP
693 * If the architecture calls into lockdep before initializing
694 * the hashes then we'll warn about it later. (we cannot printk
695 * right now)
697 if (unlikely(!lockdep_initialized)) {
698 lockdep_init();
699 lockdep_init_error = 1;
700 save_stack_trace(&lockdep_init_trace);
702 #endif
705 * Static locks do not have their class-keys yet - for them the key
706 * is the lock object itself:
708 if (unlikely(!lock->key))
709 lock->key = (void *)lock;
712 * NOTE: the class-key must be unique. For dynamic locks, a static
713 * lock_class_key variable is passed in through the mutex_init()
714 * (or spin_lock_init()) call - which acts as the key. For static
715 * locks we use the lock object itself as the key.
717 BUILD_BUG_ON(sizeof(struct lock_class_key) >
718 sizeof(struct lockdep_map));
720 key = lock->key->subkeys + subclass;
722 hash_head = classhashentry(key);
725 * We can walk the hash lockfree, because the hash only
726 * grows, and we are careful when adding entries to the end:
728 list_for_each_entry(class, hash_head, hash_entry) {
729 if (class->key == key) {
730 WARN_ON_ONCE(class->name != lock->name);
731 return class;
735 return NULL;
739 * Register a lock's class in the hash-table, if the class is not present
740 * yet. Otherwise we look it up. We cache the result in the lock object
741 * itself, so actual lookup of the hash should be once per lock object.
743 static inline struct lock_class *
744 register_lock_class(struct lockdep_map *lock, unsigned int subclass, int force)
746 struct lockdep_subclass_key *key;
747 struct list_head *hash_head;
748 struct lock_class *class;
749 unsigned long flags;
751 class = look_up_lock_class(lock, subclass);
752 if (likely(class))
753 return class;
756 * Debug-check: all keys must be persistent!
758 if (!static_obj(lock->key)) {
759 debug_locks_off();
760 printk("INFO: trying to register non-static key %p.\n", lock->key);
761 printk("the code is fine but needs lockdep annotation.\n");
762 printk("turning off the locking correctness validator.\n");
763 dump_stack();
765 return NULL;
768 key = lock->key->subkeys + subclass;
769 hash_head = classhashentry(key);
771 raw_local_irq_save(flags);
772 if (!graph_lock()) {
773 raw_local_irq_restore(flags);
774 return NULL;
777 * We have to do the hash-walk again, to avoid races
778 * with another CPU:
780 list_for_each_entry(class, hash_head, hash_entry)
781 if (class->key == key)
782 goto out_unlock_set;
784 * Allocate a new key from the static array, and add it to
785 * the hash:
787 if (nr_lock_classes >= MAX_LOCKDEP_KEYS) {
788 if (!debug_locks_off_graph_unlock()) {
789 raw_local_irq_restore(flags);
790 return NULL;
792 raw_local_irq_restore(flags);
794 printk("BUG: MAX_LOCKDEP_KEYS too low!\n");
795 printk("turning off the locking correctness validator.\n");
796 dump_stack();
797 return NULL;
799 class = lock_classes + nr_lock_classes++;
800 debug_atomic_inc(&nr_unused_locks);
801 class->key = key;
802 class->name = lock->name;
803 class->subclass = subclass;
804 INIT_LIST_HEAD(&class->lock_entry);
805 INIT_LIST_HEAD(&class->locks_before);
806 INIT_LIST_HEAD(&class->locks_after);
807 class->name_version = count_matching_names(class);
809 * We use RCU's safe list-add method to make
810 * parallel walking of the hash-list safe:
812 list_add_tail_rcu(&class->hash_entry, hash_head);
814 * Add it to the global list of classes:
816 list_add_tail_rcu(&class->lock_entry, &all_lock_classes);
818 if (verbose(class)) {
819 graph_unlock();
820 raw_local_irq_restore(flags);
822 printk("\nnew class %p: %s", class->key, class->name);
823 if (class->name_version > 1)
824 printk("#%d", class->name_version);
825 printk("\n");
826 dump_stack();
828 raw_local_irq_save(flags);
829 if (!graph_lock()) {
830 raw_local_irq_restore(flags);
831 return NULL;
834 out_unlock_set:
835 graph_unlock();
836 raw_local_irq_restore(flags);
838 if (!subclass || force)
839 lock->class_cache = class;
841 if (DEBUG_LOCKS_WARN_ON(class->subclass != subclass))
842 return NULL;
844 return class;
847 #if defined(CONFIG_PROVE_LOCKING) || defined(CONFIG_TRACE_IRQFLAGS)
849 #define RECURSION_LIMIT 40
851 static int noinline print_infinite_recursion_bug(void)
853 if (!debug_locks_off_graph_unlock())
854 return 0;
856 WARN_ON(1);
858 return 0;
860 #endif /* CONFIG_PROVE_LOCKING || CONFIG_TRACE_IRQFLAGS */
862 #ifdef CONFIG_PROVE_LOCKING
864 * Allocate a lockdep entry. (assumes the graph_lock held, returns
865 * with NULL on failure)
867 static struct lock_list *alloc_list_entry(void)
869 if (nr_list_entries >= MAX_LOCKDEP_ENTRIES) {
870 if (!debug_locks_off_graph_unlock())
871 return NULL;
873 printk("BUG: MAX_LOCKDEP_ENTRIES too low!\n");
874 printk("turning off the locking correctness validator.\n");
875 dump_stack();
876 return NULL;
878 return list_entries + nr_list_entries++;
882 * Add a new dependency to the head of the list:
884 static int add_lock_to_list(struct lock_class *class, struct lock_class *this,
885 struct list_head *head, unsigned long ip, int distance)
887 struct lock_list *entry;
889 * Lock not present yet - get a new dependency struct and
890 * add it to the list:
892 entry = alloc_list_entry();
893 if (!entry)
894 return 0;
896 if (!save_trace(&entry->trace))
897 return 0;
899 entry->class = this;
900 entry->distance = distance;
902 * Since we never remove from the dependency list, the list can
903 * be walked lockless by other CPUs, it's only allocation
904 * that must be protected by the spinlock. But this also means
905 * we must make new entries visible only once writes to the
906 * entry become visible - hence the RCU op:
908 list_add_tail_rcu(&entry->entry, head);
910 return 1;
914 * Recursive, forwards-direction lock-dependency checking, used for
915 * both noncyclic checking and for hardirq-unsafe/softirq-unsafe
916 * checking.
918 * (to keep the stackframe of the recursive functions small we
919 * use these global variables, and we also mark various helper
920 * functions as noinline.)
922 static struct held_lock *check_source, *check_target;
925 * Print a dependency chain entry (this is only done when a deadlock
926 * has been detected):
928 static noinline int
929 print_circular_bug_entry(struct lock_list *target, unsigned int depth)
931 if (debug_locks_silent)
932 return 0;
933 printk("\n-> #%u", depth);
934 print_lock_name(target->class);
935 printk(":\n");
936 print_stack_trace(&target->trace, 6);
938 return 0;
942 * When a circular dependency is detected, print the
943 * header first:
945 static noinline int
946 print_circular_bug_header(struct lock_list *entry, unsigned int depth)
948 struct task_struct *curr = current;
950 if (!debug_locks_off_graph_unlock() || debug_locks_silent)
951 return 0;
953 printk("\n=======================================================\n");
954 printk( "[ INFO: possible circular locking dependency detected ]\n");
955 print_kernel_version();
956 printk( "-------------------------------------------------------\n");
957 printk("%s/%d is trying to acquire lock:\n",
958 curr->comm, task_pid_nr(curr));
959 print_lock(check_source);
960 printk("\nbut task is already holding lock:\n");
961 print_lock(check_target);
962 printk("\nwhich lock already depends on the new lock.\n\n");
963 printk("\nthe existing dependency chain (in reverse order) is:\n");
965 print_circular_bug_entry(entry, depth);
967 return 0;
970 static noinline int print_circular_bug_tail(void)
972 struct task_struct *curr = current;
973 struct lock_list this;
975 if (debug_locks_silent)
976 return 0;
978 this.class = hlock_class(check_source);
979 if (!save_trace(&this.trace))
980 return 0;
982 print_circular_bug_entry(&this, 0);
984 printk("\nother info that might help us debug this:\n\n");
985 lockdep_print_held_locks(curr);
987 printk("\nstack backtrace:\n");
988 dump_stack();
990 return 0;
993 unsigned long __lockdep_count_forward_deps(struct lock_class *class,
994 unsigned int depth)
996 struct lock_list *entry;
997 unsigned long ret = 1;
999 if (lockdep_dependency_visit(class, depth))
1000 return 0;
1003 * Recurse this class's dependency list:
1005 list_for_each_entry(entry, &class->locks_after, entry)
1006 ret += __lockdep_count_forward_deps(entry->class, depth + 1);
1008 return ret;
1011 unsigned long lockdep_count_forward_deps(struct lock_class *class)
1013 unsigned long ret, flags;
1015 local_irq_save(flags);
1016 __raw_spin_lock(&lockdep_lock);
1017 ret = __lockdep_count_forward_deps(class, 0);
1018 __raw_spin_unlock(&lockdep_lock);
1019 local_irq_restore(flags);
1021 return ret;
1024 unsigned long __lockdep_count_backward_deps(struct lock_class *class,
1025 unsigned int depth)
1027 struct lock_list *entry;
1028 unsigned long ret = 1;
1030 if (lockdep_dependency_visit(class, depth))
1031 return 0;
1033 * Recurse this class's dependency list:
1035 list_for_each_entry(entry, &class->locks_before, entry)
1036 ret += __lockdep_count_backward_deps(entry->class, depth + 1);
1038 return ret;
1041 unsigned long lockdep_count_backward_deps(struct lock_class *class)
1043 unsigned long ret, flags;
1045 local_irq_save(flags);
1046 __raw_spin_lock(&lockdep_lock);
1047 ret = __lockdep_count_backward_deps(class, 0);
1048 __raw_spin_unlock(&lockdep_lock);
1049 local_irq_restore(flags);
1051 return ret;
1055 * Prove that the dependency graph starting at <entry> can not
1056 * lead to <target>. Print an error and return 0 if it does.
1058 static noinline int
1059 check_noncircular(struct lock_class *source, unsigned int depth)
1061 struct lock_list *entry;
1063 if (lockdep_dependency_visit(source, depth))
1064 return 1;
1066 debug_atomic_inc(&nr_cyclic_check_recursions);
1067 if (depth > max_recursion_depth)
1068 max_recursion_depth = depth;
1069 if (depth >= RECURSION_LIMIT)
1070 return print_infinite_recursion_bug();
1072 * Check this lock's dependency list:
1074 list_for_each_entry(entry, &source->locks_after, entry) {
1075 if (entry->class == hlock_class(check_target))
1076 return print_circular_bug_header(entry, depth+1);
1077 debug_atomic_inc(&nr_cyclic_checks);
1078 if (!check_noncircular(entry->class, depth+1))
1079 return print_circular_bug_entry(entry, depth+1);
1081 return 1;
1084 #if defined(CONFIG_TRACE_IRQFLAGS) && defined(CONFIG_PROVE_LOCKING)
1086 * Forwards and backwards subgraph searching, for the purposes of
1087 * proving that two subgraphs can be connected by a new dependency
1088 * without creating any illegal irq-safe -> irq-unsafe lock dependency.
1090 static enum lock_usage_bit find_usage_bit;
1091 static struct lock_class *forwards_match, *backwards_match;
1094 * Find a node in the forwards-direction dependency sub-graph starting
1095 * at <source> that matches <find_usage_bit>.
1097 * Return 2 if such a node exists in the subgraph, and put that node
1098 * into <forwards_match>.
1100 * Return 1 otherwise and keep <forwards_match> unchanged.
1101 * Return 0 on error.
1103 static noinline int
1104 find_usage_forwards(struct lock_class *source, unsigned int depth)
1106 struct lock_list *entry;
1107 int ret;
1109 if (lockdep_dependency_visit(source, depth))
1110 return 1;
1112 if (depth > max_recursion_depth)
1113 max_recursion_depth = depth;
1114 if (depth >= RECURSION_LIMIT)
1115 return print_infinite_recursion_bug();
1117 debug_atomic_inc(&nr_find_usage_forwards_checks);
1118 if (source->usage_mask & (1 << find_usage_bit)) {
1119 forwards_match = source;
1120 return 2;
1124 * Check this lock's dependency list:
1126 list_for_each_entry(entry, &source->locks_after, entry) {
1127 debug_atomic_inc(&nr_find_usage_forwards_recursions);
1128 ret = find_usage_forwards(entry->class, depth+1);
1129 if (ret == 2 || ret == 0)
1130 return ret;
1132 return 1;
1136 * Find a node in the backwards-direction dependency sub-graph starting
1137 * at <source> that matches <find_usage_bit>.
1139 * Return 2 if such a node exists in the subgraph, and put that node
1140 * into <backwards_match>.
1142 * Return 1 otherwise and keep <backwards_match> unchanged.
1143 * Return 0 on error.
1145 static noinline int
1146 find_usage_backwards(struct lock_class *source, unsigned int depth)
1148 struct lock_list *entry;
1149 int ret;
1151 if (lockdep_dependency_visit(source, depth))
1152 return 1;
1154 if (!__raw_spin_is_locked(&lockdep_lock))
1155 return DEBUG_LOCKS_WARN_ON(1);
1157 if (depth > max_recursion_depth)
1158 max_recursion_depth = depth;
1159 if (depth >= RECURSION_LIMIT)
1160 return print_infinite_recursion_bug();
1162 debug_atomic_inc(&nr_find_usage_backwards_checks);
1163 if (source->usage_mask & (1 << find_usage_bit)) {
1164 backwards_match = source;
1165 return 2;
1168 if (!source && debug_locks_off_graph_unlock()) {
1169 WARN_ON(1);
1170 return 0;
1174 * Check this lock's dependency list:
1176 list_for_each_entry(entry, &source->locks_before, entry) {
1177 debug_atomic_inc(&nr_find_usage_backwards_recursions);
1178 ret = find_usage_backwards(entry->class, depth+1);
1179 if (ret == 2 || ret == 0)
1180 return ret;
1182 return 1;
1185 #ifdef CONFIG_PROVE_LOCKING
1186 static int
1187 print_bad_irq_dependency(struct task_struct *curr,
1188 struct held_lock *prev,
1189 struct held_lock *next,
1190 enum lock_usage_bit bit1,
1191 enum lock_usage_bit bit2,
1192 const char *irqclass)
1194 if (!debug_locks_off_graph_unlock() || debug_locks_silent)
1195 return 0;
1197 printk("\n======================================================\n");
1198 printk( "[ INFO: %s-safe -> %s-unsafe lock order detected ]\n",
1199 irqclass, irqclass);
1200 print_kernel_version();
1201 printk( "------------------------------------------------------\n");
1202 printk("%s/%d [HC%u[%lu]:SC%u[%lu]:HE%u:SE%u] is trying to acquire:\n",
1203 curr->comm, task_pid_nr(curr),
1204 curr->hardirq_context, hardirq_count() >> HARDIRQ_SHIFT,
1205 curr->softirq_context, softirq_count() >> SOFTIRQ_SHIFT,
1206 curr->hardirqs_enabled,
1207 curr->softirqs_enabled);
1208 print_lock(next);
1210 printk("\nand this task is already holding:\n");
1211 print_lock(prev);
1212 printk("which would create a new lock dependency:\n");
1213 print_lock_name(hlock_class(prev));
1214 printk(" ->");
1215 print_lock_name(hlock_class(next));
1216 printk("\n");
1218 printk("\nbut this new dependency connects a %s-irq-safe lock:\n",
1219 irqclass);
1220 print_lock_name(backwards_match);
1221 printk("\n... which became %s-irq-safe at:\n", irqclass);
1223 print_stack_trace(backwards_match->usage_traces + bit1, 1);
1225 printk("\nto a %s-irq-unsafe lock:\n", irqclass);
1226 print_lock_name(forwards_match);
1227 printk("\n... which became %s-irq-unsafe at:\n", irqclass);
1228 printk("...");
1230 print_stack_trace(forwards_match->usage_traces + bit2, 1);
1232 printk("\nother info that might help us debug this:\n\n");
1233 lockdep_print_held_locks(curr);
1235 printk("\nthe %s-irq-safe lock's dependencies:\n", irqclass);
1236 print_lock_dependencies(backwards_match, 0);
1238 printk("\nthe %s-irq-unsafe lock's dependencies:\n", irqclass);
1239 print_lock_dependencies(forwards_match, 0);
1241 printk("\nstack backtrace:\n");
1242 dump_stack();
1244 return 0;
1246 #endif /* CONFIG_PROVE_LOCKING */
1248 static int
1249 check_usage(struct task_struct *curr, struct held_lock *prev,
1250 struct held_lock *next, enum lock_usage_bit bit_backwards,
1251 enum lock_usage_bit bit_forwards, const char *irqclass)
1253 int ret;
1255 find_usage_bit = bit_backwards;
1256 /* fills in <backwards_match> */
1257 ret = find_usage_backwards(hlock_class(prev), 0);
1258 if (!ret || ret == 1)
1259 return ret;
1261 find_usage_bit = bit_forwards;
1262 ret = find_usage_forwards(hlock_class(next), 0);
1263 if (!ret || ret == 1)
1264 return ret;
1265 /* ret == 2 */
1266 return print_bad_irq_dependency(curr, prev, next,
1267 bit_backwards, bit_forwards, irqclass);
1270 static const char *state_names[] = {
1271 #define LOCKDEP_STATE(__STATE) \
1272 __stringify(__STATE),
1273 #include "lockdep_states.h"
1274 #undef LOCKDEP_STATE
1277 static const char *state_rnames[] = {
1278 #define LOCKDEP_STATE(__STATE) \
1279 __stringify(__STATE)"-READ",
1280 #include "lockdep_states.h"
1281 #undef LOCKDEP_STATE
1284 static inline const char *state_name(enum lock_usage_bit bit)
1286 return (bit & 1) ? state_rnames[bit >> 2] : state_names[bit >> 2];
1289 static int exclusive_bit(int new_bit)
1292 * USED_IN
1293 * USED_IN_READ
1294 * ENABLED
1295 * ENABLED_READ
1297 * bit 0 - write/read
1298 * bit 1 - used_in/enabled
1299 * bit 2+ state
1302 int state = new_bit & ~3;
1303 int dir = new_bit & 2;
1306 * keep state, bit flip the direction and strip read.
1308 return state | (dir ^ 2);
1311 static int check_irq_usage(struct task_struct *curr, struct held_lock *prev,
1312 struct held_lock *next, enum lock_usage_bit bit)
1315 * Prove that the new dependency does not connect a hardirq-safe
1316 * lock with a hardirq-unsafe lock - to achieve this we search
1317 * the backwards-subgraph starting at <prev>, and the
1318 * forwards-subgraph starting at <next>:
1320 if (!check_usage(curr, prev, next, bit,
1321 exclusive_bit(bit), state_name(bit)))
1322 return 0;
1324 bit++; /* _READ */
1327 * Prove that the new dependency does not connect a hardirq-safe-read
1328 * lock with a hardirq-unsafe lock - to achieve this we search
1329 * the backwards-subgraph starting at <prev>, and the
1330 * forwards-subgraph starting at <next>:
1332 if (!check_usage(curr, prev, next, bit,
1333 exclusive_bit(bit), state_name(bit)))
1334 return 0;
1336 return 1;
1339 static int
1340 check_prev_add_irq(struct task_struct *curr, struct held_lock *prev,
1341 struct held_lock *next)
1343 #define LOCKDEP_STATE(__STATE) \
1344 if (!check_irq_usage(curr, prev, next, LOCK_USED_IN_##__STATE)) \
1345 return 0;
1346 #include "lockdep_states.h"
1347 #undef LOCKDEP_STATE
1349 return 1;
1352 static void inc_chains(void)
1354 if (current->hardirq_context)
1355 nr_hardirq_chains++;
1356 else {
1357 if (current->softirq_context)
1358 nr_softirq_chains++;
1359 else
1360 nr_process_chains++;
1364 #else
1366 static inline int
1367 check_prev_add_irq(struct task_struct *curr, struct held_lock *prev,
1368 struct held_lock *next)
1370 return 1;
1373 static inline void inc_chains(void)
1375 nr_process_chains++;
1378 #endif
1380 static int
1381 print_deadlock_bug(struct task_struct *curr, struct held_lock *prev,
1382 struct held_lock *next)
1384 if (!debug_locks_off_graph_unlock() || debug_locks_silent)
1385 return 0;
1387 printk("\n=============================================\n");
1388 printk( "[ INFO: possible recursive locking detected ]\n");
1389 print_kernel_version();
1390 printk( "---------------------------------------------\n");
1391 printk("%s/%d is trying to acquire lock:\n",
1392 curr->comm, task_pid_nr(curr));
1393 print_lock(next);
1394 printk("\nbut task is already holding lock:\n");
1395 print_lock(prev);
1397 printk("\nother info that might help us debug this:\n");
1398 lockdep_print_held_locks(curr);
1400 printk("\nstack backtrace:\n");
1401 dump_stack();
1403 return 0;
1407 * Check whether we are holding such a class already.
1409 * (Note that this has to be done separately, because the graph cannot
1410 * detect such classes of deadlocks.)
1412 * Returns: 0 on deadlock detected, 1 on OK, 2 on recursive read
1414 static int
1415 check_deadlock(struct task_struct *curr, struct held_lock *next,
1416 struct lockdep_map *next_instance, int read)
1418 struct held_lock *prev;
1419 struct held_lock *nest = NULL;
1420 int i;
1422 for (i = 0; i < curr->lockdep_depth; i++) {
1423 prev = curr->held_locks + i;
1425 if (prev->instance == next->nest_lock)
1426 nest = prev;
1428 if (hlock_class(prev) != hlock_class(next))
1429 continue;
1432 * Allow read-after-read recursion of the same
1433 * lock class (i.e. read_lock(lock)+read_lock(lock)):
1435 if ((read == 2) && prev->read)
1436 return 2;
1439 * We're holding the nest_lock, which serializes this lock's
1440 * nesting behaviour.
1442 if (nest)
1443 return 2;
1445 return print_deadlock_bug(curr, prev, next);
1447 return 1;
1451 * There was a chain-cache miss, and we are about to add a new dependency
1452 * to a previous lock. We recursively validate the following rules:
1454 * - would the adding of the <prev> -> <next> dependency create a
1455 * circular dependency in the graph? [== circular deadlock]
1457 * - does the new prev->next dependency connect any hardirq-safe lock
1458 * (in the full backwards-subgraph starting at <prev>) with any
1459 * hardirq-unsafe lock (in the full forwards-subgraph starting at
1460 * <next>)? [== illegal lock inversion with hardirq contexts]
1462 * - does the new prev->next dependency connect any softirq-safe lock
1463 * (in the full backwards-subgraph starting at <prev>) with any
1464 * softirq-unsafe lock (in the full forwards-subgraph starting at
1465 * <next>)? [== illegal lock inversion with softirq contexts]
1467 * any of these scenarios could lead to a deadlock.
1469 * Then if all the validations pass, we add the forwards and backwards
1470 * dependency.
1472 static int
1473 check_prev_add(struct task_struct *curr, struct held_lock *prev,
1474 struct held_lock *next, int distance)
1476 struct lock_list *entry;
1477 int ret;
1480 * Prove that the new <prev> -> <next> dependency would not
1481 * create a circular dependency in the graph. (We do this by
1482 * forward-recursing into the graph starting at <next>, and
1483 * checking whether we can reach <prev>.)
1485 * We are using global variables to control the recursion, to
1486 * keep the stackframe size of the recursive functions low:
1488 check_source = next;
1489 check_target = prev;
1490 if (!(check_noncircular(hlock_class(next), 0)))
1491 return print_circular_bug_tail();
1493 if (!check_prev_add_irq(curr, prev, next))
1494 return 0;
1497 * For recursive read-locks we do all the dependency checks,
1498 * but we dont store read-triggered dependencies (only
1499 * write-triggered dependencies). This ensures that only the
1500 * write-side dependencies matter, and that if for example a
1501 * write-lock never takes any other locks, then the reads are
1502 * equivalent to a NOP.
1504 if (next->read == 2 || prev->read == 2)
1505 return 1;
1507 * Is the <prev> -> <next> dependency already present?
1509 * (this may occur even though this is a new chain: consider
1510 * e.g. the L1 -> L2 -> L3 -> L4 and the L5 -> L1 -> L2 -> L3
1511 * chains - the second one will be new, but L1 already has
1512 * L2 added to its dependency list, due to the first chain.)
1514 list_for_each_entry(entry, &hlock_class(prev)->locks_after, entry) {
1515 if (entry->class == hlock_class(next)) {
1516 if (distance == 1)
1517 entry->distance = 1;
1518 return 2;
1523 * Ok, all validations passed, add the new lock
1524 * to the previous lock's dependency list:
1526 ret = add_lock_to_list(hlock_class(prev), hlock_class(next),
1527 &hlock_class(prev)->locks_after,
1528 next->acquire_ip, distance);
1530 if (!ret)
1531 return 0;
1533 ret = add_lock_to_list(hlock_class(next), hlock_class(prev),
1534 &hlock_class(next)->locks_before,
1535 next->acquire_ip, distance);
1536 if (!ret)
1537 return 0;
1540 * Debugging printouts:
1542 if (verbose(hlock_class(prev)) || verbose(hlock_class(next))) {
1543 graph_unlock();
1544 printk("\n new dependency: ");
1545 print_lock_name(hlock_class(prev));
1546 printk(" => ");
1547 print_lock_name(hlock_class(next));
1548 printk("\n");
1549 dump_stack();
1550 return graph_lock();
1552 return 1;
1556 * Add the dependency to all directly-previous locks that are 'relevant'.
1557 * The ones that are relevant are (in increasing distance from curr):
1558 * all consecutive trylock entries and the final non-trylock entry - or
1559 * the end of this context's lock-chain - whichever comes first.
1561 static int
1562 check_prevs_add(struct task_struct *curr, struct held_lock *next)
1564 int depth = curr->lockdep_depth;
1565 struct held_lock *hlock;
1568 * Debugging checks.
1570 * Depth must not be zero for a non-head lock:
1572 if (!depth)
1573 goto out_bug;
1575 * At least two relevant locks must exist for this
1576 * to be a head:
1578 if (curr->held_locks[depth].irq_context !=
1579 curr->held_locks[depth-1].irq_context)
1580 goto out_bug;
1582 for (;;) {
1583 int distance = curr->lockdep_depth - depth + 1;
1584 hlock = curr->held_locks + depth-1;
1586 * Only non-recursive-read entries get new dependencies
1587 * added:
1589 if (hlock->read != 2) {
1590 if (!check_prev_add(curr, hlock, next, distance))
1591 return 0;
1593 * Stop after the first non-trylock entry,
1594 * as non-trylock entries have added their
1595 * own direct dependencies already, so this
1596 * lock is connected to them indirectly:
1598 if (!hlock->trylock)
1599 break;
1601 depth--;
1603 * End of lock-stack?
1605 if (!depth)
1606 break;
1608 * Stop the search if we cross into another context:
1610 if (curr->held_locks[depth].irq_context !=
1611 curr->held_locks[depth-1].irq_context)
1612 break;
1614 return 1;
1615 out_bug:
1616 if (!debug_locks_off_graph_unlock())
1617 return 0;
1619 WARN_ON(1);
1621 return 0;
1624 unsigned long nr_lock_chains;
1625 struct lock_chain lock_chains[MAX_LOCKDEP_CHAINS];
1626 int nr_chain_hlocks;
1627 static u16 chain_hlocks[MAX_LOCKDEP_CHAIN_HLOCKS];
1629 struct lock_class *lock_chain_get_class(struct lock_chain *chain, int i)
1631 return lock_classes + chain_hlocks[chain->base + i];
1635 * Look up a dependency chain. If the key is not present yet then
1636 * add it and return 1 - in this case the new dependency chain is
1637 * validated. If the key is already hashed, return 0.
1638 * (On return with 1 graph_lock is held.)
1640 static inline int lookup_chain_cache(struct task_struct *curr,
1641 struct held_lock *hlock,
1642 u64 chain_key)
1644 struct lock_class *class = hlock_class(hlock);
1645 struct list_head *hash_head = chainhashentry(chain_key);
1646 struct lock_chain *chain;
1647 struct held_lock *hlock_curr, *hlock_next;
1648 int i, j, n, cn;
1650 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
1651 return 0;
1653 * We can walk it lock-free, because entries only get added
1654 * to the hash:
1656 list_for_each_entry(chain, hash_head, entry) {
1657 if (chain->chain_key == chain_key) {
1658 cache_hit:
1659 debug_atomic_inc(&chain_lookup_hits);
1660 if (very_verbose(class))
1661 printk("\nhash chain already cached, key: "
1662 "%016Lx tail class: [%p] %s\n",
1663 (unsigned long long)chain_key,
1664 class->key, class->name);
1665 return 0;
1668 if (very_verbose(class))
1669 printk("\nnew hash chain, key: %016Lx tail class: [%p] %s\n",
1670 (unsigned long long)chain_key, class->key, class->name);
1672 * Allocate a new chain entry from the static array, and add
1673 * it to the hash:
1675 if (!graph_lock())
1676 return 0;
1678 * We have to walk the chain again locked - to avoid duplicates:
1680 list_for_each_entry(chain, hash_head, entry) {
1681 if (chain->chain_key == chain_key) {
1682 graph_unlock();
1683 goto cache_hit;
1686 if (unlikely(nr_lock_chains >= MAX_LOCKDEP_CHAINS)) {
1687 if (!debug_locks_off_graph_unlock())
1688 return 0;
1690 printk("BUG: MAX_LOCKDEP_CHAINS too low!\n");
1691 printk("turning off the locking correctness validator.\n");
1692 dump_stack();
1693 return 0;
1695 chain = lock_chains + nr_lock_chains++;
1696 chain->chain_key = chain_key;
1697 chain->irq_context = hlock->irq_context;
1698 /* Find the first held_lock of current chain */
1699 hlock_next = hlock;
1700 for (i = curr->lockdep_depth - 1; i >= 0; i--) {
1701 hlock_curr = curr->held_locks + i;
1702 if (hlock_curr->irq_context != hlock_next->irq_context)
1703 break;
1704 hlock_next = hlock;
1706 i++;
1707 chain->depth = curr->lockdep_depth + 1 - i;
1708 cn = nr_chain_hlocks;
1709 while (cn + chain->depth <= MAX_LOCKDEP_CHAIN_HLOCKS) {
1710 n = cmpxchg(&nr_chain_hlocks, cn, cn + chain->depth);
1711 if (n == cn)
1712 break;
1713 cn = n;
1715 if (likely(cn + chain->depth <= MAX_LOCKDEP_CHAIN_HLOCKS)) {
1716 chain->base = cn;
1717 for (j = 0; j < chain->depth - 1; j++, i++) {
1718 int lock_id = curr->held_locks[i].class_idx - 1;
1719 chain_hlocks[chain->base + j] = lock_id;
1721 chain_hlocks[chain->base + j] = class - lock_classes;
1723 list_add_tail_rcu(&chain->entry, hash_head);
1724 debug_atomic_inc(&chain_lookup_misses);
1725 inc_chains();
1727 return 1;
1730 static int validate_chain(struct task_struct *curr, struct lockdep_map *lock,
1731 struct held_lock *hlock, int chain_head, u64 chain_key)
1734 * Trylock needs to maintain the stack of held locks, but it
1735 * does not add new dependencies, because trylock can be done
1736 * in any order.
1738 * We look up the chain_key and do the O(N^2) check and update of
1739 * the dependencies only if this is a new dependency chain.
1740 * (If lookup_chain_cache() returns with 1 it acquires
1741 * graph_lock for us)
1743 if (!hlock->trylock && (hlock->check == 2) &&
1744 lookup_chain_cache(curr, hlock, chain_key)) {
1746 * Check whether last held lock:
1748 * - is irq-safe, if this lock is irq-unsafe
1749 * - is softirq-safe, if this lock is hardirq-unsafe
1751 * And check whether the new lock's dependency graph
1752 * could lead back to the previous lock.
1754 * any of these scenarios could lead to a deadlock. If
1755 * All validations
1757 int ret = check_deadlock(curr, hlock, lock, hlock->read);
1759 if (!ret)
1760 return 0;
1762 * Mark recursive read, as we jump over it when
1763 * building dependencies (just like we jump over
1764 * trylock entries):
1766 if (ret == 2)
1767 hlock->read = 2;
1769 * Add dependency only if this lock is not the head
1770 * of the chain, and if it's not a secondary read-lock:
1772 if (!chain_head && ret != 2)
1773 if (!check_prevs_add(curr, hlock))
1774 return 0;
1775 graph_unlock();
1776 } else
1777 /* after lookup_chain_cache(): */
1778 if (unlikely(!debug_locks))
1779 return 0;
1781 return 1;
1783 #else
1784 static inline int validate_chain(struct task_struct *curr,
1785 struct lockdep_map *lock, struct held_lock *hlock,
1786 int chain_head, u64 chain_key)
1788 return 1;
1790 #endif
1793 * We are building curr_chain_key incrementally, so double-check
1794 * it from scratch, to make sure that it's done correctly:
1796 static void check_chain_key(struct task_struct *curr)
1798 #ifdef CONFIG_DEBUG_LOCKDEP
1799 struct held_lock *hlock, *prev_hlock = NULL;
1800 unsigned int i, id;
1801 u64 chain_key = 0;
1803 for (i = 0; i < curr->lockdep_depth; i++) {
1804 hlock = curr->held_locks + i;
1805 if (chain_key != hlock->prev_chain_key) {
1806 debug_locks_off();
1807 WARN(1, "hm#1, depth: %u [%u], %016Lx != %016Lx\n",
1808 curr->lockdep_depth, i,
1809 (unsigned long long)chain_key,
1810 (unsigned long long)hlock->prev_chain_key);
1811 return;
1813 id = hlock->class_idx - 1;
1814 if (DEBUG_LOCKS_WARN_ON(id >= MAX_LOCKDEP_KEYS))
1815 return;
1817 if (prev_hlock && (prev_hlock->irq_context !=
1818 hlock->irq_context))
1819 chain_key = 0;
1820 chain_key = iterate_chain_key(chain_key, id);
1821 prev_hlock = hlock;
1823 if (chain_key != curr->curr_chain_key) {
1824 debug_locks_off();
1825 WARN(1, "hm#2, depth: %u [%u], %016Lx != %016Lx\n",
1826 curr->lockdep_depth, i,
1827 (unsigned long long)chain_key,
1828 (unsigned long long)curr->curr_chain_key);
1830 #endif
1833 static int
1834 print_usage_bug(struct task_struct *curr, struct held_lock *this,
1835 enum lock_usage_bit prev_bit, enum lock_usage_bit new_bit)
1837 if (!debug_locks_off_graph_unlock() || debug_locks_silent)
1838 return 0;
1840 printk("\n=================================\n");
1841 printk( "[ INFO: inconsistent lock state ]\n");
1842 print_kernel_version();
1843 printk( "---------------------------------\n");
1845 printk("inconsistent {%s} -> {%s} usage.\n",
1846 usage_str[prev_bit], usage_str[new_bit]);
1848 printk("%s/%d [HC%u[%lu]:SC%u[%lu]:HE%u:SE%u] takes:\n",
1849 curr->comm, task_pid_nr(curr),
1850 trace_hardirq_context(curr), hardirq_count() >> HARDIRQ_SHIFT,
1851 trace_softirq_context(curr), softirq_count() >> SOFTIRQ_SHIFT,
1852 trace_hardirqs_enabled(curr),
1853 trace_softirqs_enabled(curr));
1854 print_lock(this);
1856 printk("{%s} state was registered at:\n", usage_str[prev_bit]);
1857 print_stack_trace(hlock_class(this)->usage_traces + prev_bit, 1);
1859 print_irqtrace_events(curr);
1860 printk("\nother info that might help us debug this:\n");
1861 lockdep_print_held_locks(curr);
1863 printk("\nstack backtrace:\n");
1864 dump_stack();
1866 return 0;
1870 * Print out an error if an invalid bit is set:
1872 static inline int
1873 valid_state(struct task_struct *curr, struct held_lock *this,
1874 enum lock_usage_bit new_bit, enum lock_usage_bit bad_bit)
1876 if (unlikely(hlock_class(this)->usage_mask & (1 << bad_bit)))
1877 return print_usage_bug(curr, this, bad_bit, new_bit);
1878 return 1;
1881 static int mark_lock(struct task_struct *curr, struct held_lock *this,
1882 enum lock_usage_bit new_bit);
1884 #if defined(CONFIG_TRACE_IRQFLAGS) && defined(CONFIG_PROVE_LOCKING)
1887 * print irq inversion bug:
1889 static int
1890 print_irq_inversion_bug(struct task_struct *curr, struct lock_class *other,
1891 struct held_lock *this, int forwards,
1892 const char *irqclass)
1894 if (!debug_locks_off_graph_unlock() || debug_locks_silent)
1895 return 0;
1897 printk("\n=========================================================\n");
1898 printk( "[ INFO: possible irq lock inversion dependency detected ]\n");
1899 print_kernel_version();
1900 printk( "---------------------------------------------------------\n");
1901 printk("%s/%d just changed the state of lock:\n",
1902 curr->comm, task_pid_nr(curr));
1903 print_lock(this);
1904 if (forwards)
1905 printk("but this lock took another, %s-unsafe lock in the past:\n", irqclass);
1906 else
1907 printk("but this lock was taken by another, %s-safe lock in the past:\n", irqclass);
1908 print_lock_name(other);
1909 printk("\n\nand interrupts could create inverse lock ordering between them.\n\n");
1911 printk("\nother info that might help us debug this:\n");
1912 lockdep_print_held_locks(curr);
1914 printk("\nthe first lock's dependencies:\n");
1915 print_lock_dependencies(hlock_class(this), 0);
1917 printk("\nthe second lock's dependencies:\n");
1918 print_lock_dependencies(other, 0);
1920 printk("\nstack backtrace:\n");
1921 dump_stack();
1923 return 0;
1927 * Prove that in the forwards-direction subgraph starting at <this>
1928 * there is no lock matching <mask>:
1930 static int
1931 check_usage_forwards(struct task_struct *curr, struct held_lock *this,
1932 enum lock_usage_bit bit, const char *irqclass)
1934 int ret;
1936 find_usage_bit = bit;
1937 /* fills in <forwards_match> */
1938 ret = find_usage_forwards(hlock_class(this), 0);
1939 if (!ret || ret == 1)
1940 return ret;
1942 return print_irq_inversion_bug(curr, forwards_match, this, 1, irqclass);
1946 * Prove that in the backwards-direction subgraph starting at <this>
1947 * there is no lock matching <mask>:
1949 static int
1950 check_usage_backwards(struct task_struct *curr, struct held_lock *this,
1951 enum lock_usage_bit bit, const char *irqclass)
1953 int ret;
1955 find_usage_bit = bit;
1956 /* fills in <backwards_match> */
1957 ret = find_usage_backwards(hlock_class(this), 0);
1958 if (!ret || ret == 1)
1959 return ret;
1961 return print_irq_inversion_bug(curr, backwards_match, this, 0, irqclass);
1964 void print_irqtrace_events(struct task_struct *curr)
1966 printk("irq event stamp: %u\n", curr->irq_events);
1967 printk("hardirqs last enabled at (%u): ", curr->hardirq_enable_event);
1968 print_ip_sym(curr->hardirq_enable_ip);
1969 printk("hardirqs last disabled at (%u): ", curr->hardirq_disable_event);
1970 print_ip_sym(curr->hardirq_disable_ip);
1971 printk("softirqs last enabled at (%u): ", curr->softirq_enable_event);
1972 print_ip_sym(curr->softirq_enable_ip);
1973 printk("softirqs last disabled at (%u): ", curr->softirq_disable_event);
1974 print_ip_sym(curr->softirq_disable_ip);
1977 static int HARDIRQ_verbose(struct lock_class *class)
1979 #if HARDIRQ_VERBOSE
1980 return class_filter(class);
1981 #endif
1982 return 0;
1985 static int SOFTIRQ_verbose(struct lock_class *class)
1987 #if SOFTIRQ_VERBOSE
1988 return class_filter(class);
1989 #endif
1990 return 0;
1993 static int RECLAIM_FS_verbose(struct lock_class *class)
1995 #if RECLAIM_VERBOSE
1996 return class_filter(class);
1997 #endif
1998 return 0;
2001 #define STRICT_READ_CHECKS 1
2003 static int (*state_verbose_f[])(struct lock_class *class) = {
2004 #define LOCKDEP_STATE(__STATE) \
2005 __STATE##_verbose,
2006 #include "lockdep_states.h"
2007 #undef LOCKDEP_STATE
2010 static inline int state_verbose(enum lock_usage_bit bit,
2011 struct lock_class *class)
2013 return state_verbose_f[bit >> 2](class);
2016 typedef int (*check_usage_f)(struct task_struct *, struct held_lock *,
2017 enum lock_usage_bit bit, const char *name);
2019 static int
2020 mark_lock_irq(struct task_struct *curr, struct held_lock *this,
2021 enum lock_usage_bit new_bit)
2023 int excl_bit = exclusive_bit(new_bit);
2024 int read = new_bit & 1;
2025 int dir = new_bit & 2;
2028 * mark USED_IN has to look forwards -- to ensure no dependency
2029 * has ENABLED state, which would allow recursion deadlocks.
2031 * mark ENABLED has to look backwards -- to ensure no dependee
2032 * has USED_IN state, which, again, would allow recursion deadlocks.
2034 check_usage_f usage = dir ?
2035 check_usage_backwards : check_usage_forwards;
2038 * Validate that this particular lock does not have conflicting
2039 * usage states.
2041 if (!valid_state(curr, this, new_bit, excl_bit))
2042 return 0;
2045 * Validate that the lock dependencies don't have conflicting usage
2046 * states.
2048 if ((!read || !dir || STRICT_READ_CHECKS) &&
2049 !usage(curr, this, excl_bit, state_name(new_bit & ~1)))
2050 return 0;
2053 * Check for read in write conflicts
2055 if (!read) {
2056 if (!valid_state(curr, this, new_bit, excl_bit + 1))
2057 return 0;
2059 if (STRICT_READ_CHECKS &&
2060 !usage(curr, this, excl_bit + 1,
2061 state_name(new_bit + 1)))
2062 return 0;
2065 if (state_verbose(new_bit, hlock_class(this)))
2066 return 2;
2068 return 1;
2071 enum mark_type {
2072 #define LOCKDEP_STATE(__STATE) __STATE,
2073 #include "lockdep_states.h"
2074 #undef LOCKDEP_STATE
2078 * Mark all held locks with a usage bit:
2080 static int
2081 mark_held_locks(struct task_struct *curr, enum mark_type mark)
2083 enum lock_usage_bit usage_bit;
2084 struct held_lock *hlock;
2085 int i;
2087 for (i = 0; i < curr->lockdep_depth; i++) {
2088 hlock = curr->held_locks + i;
2090 usage_bit = 2 + (mark << 2); /* ENABLED */
2091 if (hlock->read)
2092 usage_bit += 1; /* READ */
2094 BUG_ON(usage_bit >= LOCK_USAGE_STATES);
2096 if (!mark_lock(curr, hlock, usage_bit))
2097 return 0;
2100 return 1;
2104 * Debugging helper: via this flag we know that we are in
2105 * 'early bootup code', and will warn about any invalid irqs-on event:
2107 static int early_boot_irqs_enabled;
2109 void early_boot_irqs_off(void)
2111 early_boot_irqs_enabled = 0;
2114 void early_boot_irqs_on(void)
2116 early_boot_irqs_enabled = 1;
2120 * Hardirqs will be enabled:
2122 void trace_hardirqs_on_caller(unsigned long ip)
2124 struct task_struct *curr = current;
2126 time_hardirqs_on(CALLER_ADDR0, ip);
2128 if (unlikely(!debug_locks || current->lockdep_recursion))
2129 return;
2131 if (DEBUG_LOCKS_WARN_ON(unlikely(!early_boot_irqs_enabled)))
2132 return;
2134 if (unlikely(curr->hardirqs_enabled)) {
2135 debug_atomic_inc(&redundant_hardirqs_on);
2136 return;
2138 /* we'll do an OFF -> ON transition: */
2139 curr->hardirqs_enabled = 1;
2141 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2142 return;
2143 if (DEBUG_LOCKS_WARN_ON(current->hardirq_context))
2144 return;
2146 * We are going to turn hardirqs on, so set the
2147 * usage bit for all held locks:
2149 if (!mark_held_locks(curr, HARDIRQ))
2150 return;
2152 * If we have softirqs enabled, then set the usage
2153 * bit for all held locks. (disabled hardirqs prevented
2154 * this bit from being set before)
2156 if (curr->softirqs_enabled)
2157 if (!mark_held_locks(curr, SOFTIRQ))
2158 return;
2160 curr->hardirq_enable_ip = ip;
2161 curr->hardirq_enable_event = ++curr->irq_events;
2162 debug_atomic_inc(&hardirqs_on_events);
2164 EXPORT_SYMBOL(trace_hardirqs_on_caller);
2166 void trace_hardirqs_on(void)
2168 trace_hardirqs_on_caller(CALLER_ADDR0);
2170 EXPORT_SYMBOL(trace_hardirqs_on);
2173 * Hardirqs were disabled:
2175 void trace_hardirqs_off_caller(unsigned long ip)
2177 struct task_struct *curr = current;
2179 time_hardirqs_off(CALLER_ADDR0, ip);
2181 if (unlikely(!debug_locks || current->lockdep_recursion))
2182 return;
2184 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2185 return;
2187 if (curr->hardirqs_enabled) {
2189 * We have done an ON -> OFF transition:
2191 curr->hardirqs_enabled = 0;
2192 curr->hardirq_disable_ip = ip;
2193 curr->hardirq_disable_event = ++curr->irq_events;
2194 debug_atomic_inc(&hardirqs_off_events);
2195 } else
2196 debug_atomic_inc(&redundant_hardirqs_off);
2198 EXPORT_SYMBOL(trace_hardirqs_off_caller);
2200 void trace_hardirqs_off(void)
2202 trace_hardirqs_off_caller(CALLER_ADDR0);
2204 EXPORT_SYMBOL(trace_hardirqs_off);
2207 * Softirqs will be enabled:
2209 void trace_softirqs_on(unsigned long ip)
2211 struct task_struct *curr = current;
2213 if (unlikely(!debug_locks))
2214 return;
2216 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2217 return;
2219 if (curr->softirqs_enabled) {
2220 debug_atomic_inc(&redundant_softirqs_on);
2221 return;
2225 * We'll do an OFF -> ON transition:
2227 curr->softirqs_enabled = 1;
2228 curr->softirq_enable_ip = ip;
2229 curr->softirq_enable_event = ++curr->irq_events;
2230 debug_atomic_inc(&softirqs_on_events);
2232 * We are going to turn softirqs on, so set the
2233 * usage bit for all held locks, if hardirqs are
2234 * enabled too:
2236 if (curr->hardirqs_enabled)
2237 mark_held_locks(curr, SOFTIRQ);
2241 * Softirqs were disabled:
2243 void trace_softirqs_off(unsigned long ip)
2245 struct task_struct *curr = current;
2247 if (unlikely(!debug_locks))
2248 return;
2250 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2251 return;
2253 if (curr->softirqs_enabled) {
2255 * We have done an ON -> OFF transition:
2257 curr->softirqs_enabled = 0;
2258 curr->softirq_disable_ip = ip;
2259 curr->softirq_disable_event = ++curr->irq_events;
2260 debug_atomic_inc(&softirqs_off_events);
2261 DEBUG_LOCKS_WARN_ON(!softirq_count());
2262 } else
2263 debug_atomic_inc(&redundant_softirqs_off);
2266 static void __lockdep_trace_alloc(gfp_t gfp_mask, unsigned long flags)
2268 struct task_struct *curr = current;
2270 if (unlikely(!debug_locks))
2271 return;
2273 /* no reclaim without waiting on it */
2274 if (!(gfp_mask & __GFP_WAIT))
2275 return;
2277 /* this guy won't enter reclaim */
2278 if ((curr->flags & PF_MEMALLOC) && !(gfp_mask & __GFP_NOMEMALLOC))
2279 return;
2281 /* We're only interested __GFP_FS allocations for now */
2282 if (!(gfp_mask & __GFP_FS))
2283 return;
2285 if (DEBUG_LOCKS_WARN_ON(irqs_disabled_flags(flags)))
2286 return;
2288 mark_held_locks(curr, RECLAIM_FS);
2291 static void check_flags(unsigned long flags);
2293 void lockdep_trace_alloc(gfp_t gfp_mask)
2295 unsigned long flags;
2297 if (unlikely(current->lockdep_recursion))
2298 return;
2300 raw_local_irq_save(flags);
2301 check_flags(flags);
2302 current->lockdep_recursion = 1;
2303 __lockdep_trace_alloc(gfp_mask, flags);
2304 current->lockdep_recursion = 0;
2305 raw_local_irq_restore(flags);
2308 static int mark_irqflags(struct task_struct *curr, struct held_lock *hlock)
2311 * If non-trylock use in a hardirq or softirq context, then
2312 * mark the lock as used in these contexts:
2314 if (!hlock->trylock) {
2315 if (hlock->read) {
2316 if (curr->hardirq_context)
2317 if (!mark_lock(curr, hlock,
2318 LOCK_USED_IN_HARDIRQ_READ))
2319 return 0;
2320 if (curr->softirq_context)
2321 if (!mark_lock(curr, hlock,
2322 LOCK_USED_IN_SOFTIRQ_READ))
2323 return 0;
2324 } else {
2325 if (curr->hardirq_context)
2326 if (!mark_lock(curr, hlock, LOCK_USED_IN_HARDIRQ))
2327 return 0;
2328 if (curr->softirq_context)
2329 if (!mark_lock(curr, hlock, LOCK_USED_IN_SOFTIRQ))
2330 return 0;
2333 if (!hlock->hardirqs_off) {
2334 if (hlock->read) {
2335 if (!mark_lock(curr, hlock,
2336 LOCK_ENABLED_HARDIRQ_READ))
2337 return 0;
2338 if (curr->softirqs_enabled)
2339 if (!mark_lock(curr, hlock,
2340 LOCK_ENABLED_SOFTIRQ_READ))
2341 return 0;
2342 } else {
2343 if (!mark_lock(curr, hlock,
2344 LOCK_ENABLED_HARDIRQ))
2345 return 0;
2346 if (curr->softirqs_enabled)
2347 if (!mark_lock(curr, hlock,
2348 LOCK_ENABLED_SOFTIRQ))
2349 return 0;
2354 * We reuse the irq context infrastructure more broadly as a general
2355 * context checking code. This tests GFP_FS recursion (a lock taken
2356 * during reclaim for a GFP_FS allocation is held over a GFP_FS
2357 * allocation).
2359 if (!hlock->trylock && (curr->lockdep_reclaim_gfp & __GFP_FS)) {
2360 if (hlock->read) {
2361 if (!mark_lock(curr, hlock, LOCK_USED_IN_RECLAIM_FS_READ))
2362 return 0;
2363 } else {
2364 if (!mark_lock(curr, hlock, LOCK_USED_IN_RECLAIM_FS))
2365 return 0;
2369 return 1;
2372 static int separate_irq_context(struct task_struct *curr,
2373 struct held_lock *hlock)
2375 unsigned int depth = curr->lockdep_depth;
2378 * Keep track of points where we cross into an interrupt context:
2380 hlock->irq_context = 2*(curr->hardirq_context ? 1 : 0) +
2381 curr->softirq_context;
2382 if (depth) {
2383 struct held_lock *prev_hlock;
2385 prev_hlock = curr->held_locks + depth-1;
2387 * If we cross into another context, reset the
2388 * hash key (this also prevents the checking and the
2389 * adding of the dependency to 'prev'):
2391 if (prev_hlock->irq_context != hlock->irq_context)
2392 return 1;
2394 return 0;
2397 #else
2399 static inline
2400 int mark_lock_irq(struct task_struct *curr, struct held_lock *this,
2401 enum lock_usage_bit new_bit)
2403 WARN_ON(1);
2404 return 1;
2407 static inline int mark_irqflags(struct task_struct *curr,
2408 struct held_lock *hlock)
2410 return 1;
2413 static inline int separate_irq_context(struct task_struct *curr,
2414 struct held_lock *hlock)
2416 return 0;
2419 void lockdep_trace_alloc(gfp_t gfp_mask)
2423 #endif
2426 * Mark a lock with a usage bit, and validate the state transition:
2428 static int mark_lock(struct task_struct *curr, struct held_lock *this,
2429 enum lock_usage_bit new_bit)
2431 unsigned int new_mask = 1 << new_bit, ret = 1;
2434 * If already set then do not dirty the cacheline,
2435 * nor do any checks:
2437 if (likely(hlock_class(this)->usage_mask & new_mask))
2438 return 1;
2440 if (!graph_lock())
2441 return 0;
2443 * Make sure we didnt race:
2445 if (unlikely(hlock_class(this)->usage_mask & new_mask)) {
2446 graph_unlock();
2447 return 1;
2450 hlock_class(this)->usage_mask |= new_mask;
2452 if (!save_trace(hlock_class(this)->usage_traces + new_bit))
2453 return 0;
2455 switch (new_bit) {
2456 #define LOCKDEP_STATE(__STATE) \
2457 case LOCK_USED_IN_##__STATE: \
2458 case LOCK_USED_IN_##__STATE##_READ: \
2459 case LOCK_ENABLED_##__STATE: \
2460 case LOCK_ENABLED_##__STATE##_READ:
2461 #include "lockdep_states.h"
2462 #undef LOCKDEP_STATE
2463 ret = mark_lock_irq(curr, this, new_bit);
2464 if (!ret)
2465 return 0;
2466 break;
2467 case LOCK_USED:
2468 debug_atomic_dec(&nr_unused_locks);
2469 break;
2470 default:
2471 if (!debug_locks_off_graph_unlock())
2472 return 0;
2473 WARN_ON(1);
2474 return 0;
2477 graph_unlock();
2480 * We must printk outside of the graph_lock:
2482 if (ret == 2) {
2483 printk("\nmarked lock as {%s}:\n", usage_str[new_bit]);
2484 print_lock(this);
2485 print_irqtrace_events(curr);
2486 dump_stack();
2489 return ret;
2493 * Initialize a lock instance's lock-class mapping info:
2495 void lockdep_init_map(struct lockdep_map *lock, const char *name,
2496 struct lock_class_key *key, int subclass)
2498 if (unlikely(!debug_locks))
2499 return;
2501 if (DEBUG_LOCKS_WARN_ON(!key))
2502 return;
2503 if (DEBUG_LOCKS_WARN_ON(!name))
2504 return;
2506 * Sanity check, the lock-class key must be persistent:
2508 if (!static_obj(key)) {
2509 printk("BUG: key %p not in .data!\n", key);
2510 DEBUG_LOCKS_WARN_ON(1);
2511 return;
2513 lock->name = name;
2514 lock->key = key;
2515 lock->class_cache = NULL;
2516 #ifdef CONFIG_LOCK_STAT
2517 lock->cpu = raw_smp_processor_id();
2518 #endif
2519 if (subclass)
2520 register_lock_class(lock, subclass, 1);
2522 EXPORT_SYMBOL_GPL(lockdep_init_map);
2525 * This gets called for every mutex_lock*()/spin_lock*() operation.
2526 * We maintain the dependency maps and validate the locking attempt:
2528 static int __lock_acquire(struct lockdep_map *lock, unsigned int subclass,
2529 int trylock, int read, int check, int hardirqs_off,
2530 struct lockdep_map *nest_lock, unsigned long ip)
2532 struct task_struct *curr = current;
2533 struct lock_class *class = NULL;
2534 struct held_lock *hlock;
2535 unsigned int depth, id;
2536 int chain_head = 0;
2537 u64 chain_key;
2539 if (!prove_locking)
2540 check = 1;
2542 if (unlikely(!debug_locks))
2543 return 0;
2545 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2546 return 0;
2548 if (unlikely(subclass >= MAX_LOCKDEP_SUBCLASSES)) {
2549 debug_locks_off();
2550 printk("BUG: MAX_LOCKDEP_SUBCLASSES too low!\n");
2551 printk("turning off the locking correctness validator.\n");
2552 dump_stack();
2553 return 0;
2556 if (!subclass)
2557 class = lock->class_cache;
2559 * Not cached yet or subclass?
2561 if (unlikely(!class)) {
2562 class = register_lock_class(lock, subclass, 0);
2563 if (!class)
2564 return 0;
2566 debug_atomic_inc((atomic_t *)&class->ops);
2567 if (very_verbose(class)) {
2568 printk("\nacquire class [%p] %s", class->key, class->name);
2569 if (class->name_version > 1)
2570 printk("#%d", class->name_version);
2571 printk("\n");
2572 dump_stack();
2576 * Add the lock to the list of currently held locks.
2577 * (we dont increase the depth just yet, up until the
2578 * dependency checks are done)
2580 depth = curr->lockdep_depth;
2581 if (DEBUG_LOCKS_WARN_ON(depth >= MAX_LOCK_DEPTH))
2582 return 0;
2584 hlock = curr->held_locks + depth;
2585 if (DEBUG_LOCKS_WARN_ON(!class))
2586 return 0;
2587 hlock->class_idx = class - lock_classes + 1;
2588 hlock->acquire_ip = ip;
2589 hlock->instance = lock;
2590 hlock->nest_lock = nest_lock;
2591 hlock->trylock = trylock;
2592 hlock->read = read;
2593 hlock->check = check;
2594 hlock->hardirqs_off = !!hardirqs_off;
2595 #ifdef CONFIG_LOCK_STAT
2596 hlock->waittime_stamp = 0;
2597 hlock->holdtime_stamp = sched_clock();
2598 #endif
2600 if (check == 2 && !mark_irqflags(curr, hlock))
2601 return 0;
2603 /* mark it as used: */
2604 if (!mark_lock(curr, hlock, LOCK_USED))
2605 return 0;
2608 * Calculate the chain hash: it's the combined hash of all the
2609 * lock keys along the dependency chain. We save the hash value
2610 * at every step so that we can get the current hash easily
2611 * after unlock. The chain hash is then used to cache dependency
2612 * results.
2614 * The 'key ID' is what is the most compact key value to drive
2615 * the hash, not class->key.
2617 id = class - lock_classes;
2618 if (DEBUG_LOCKS_WARN_ON(id >= MAX_LOCKDEP_KEYS))
2619 return 0;
2621 chain_key = curr->curr_chain_key;
2622 if (!depth) {
2623 if (DEBUG_LOCKS_WARN_ON(chain_key != 0))
2624 return 0;
2625 chain_head = 1;
2628 hlock->prev_chain_key = chain_key;
2629 if (separate_irq_context(curr, hlock)) {
2630 chain_key = 0;
2631 chain_head = 1;
2633 chain_key = iterate_chain_key(chain_key, id);
2635 if (!validate_chain(curr, lock, hlock, chain_head, chain_key))
2636 return 0;
2638 curr->curr_chain_key = chain_key;
2639 curr->lockdep_depth++;
2640 check_chain_key(curr);
2641 #ifdef CONFIG_DEBUG_LOCKDEP
2642 if (unlikely(!debug_locks))
2643 return 0;
2644 #endif
2645 if (unlikely(curr->lockdep_depth >= MAX_LOCK_DEPTH)) {
2646 debug_locks_off();
2647 printk("BUG: MAX_LOCK_DEPTH too low!\n");
2648 printk("turning off the locking correctness validator.\n");
2649 dump_stack();
2650 return 0;
2653 if (unlikely(curr->lockdep_depth > max_lockdep_depth))
2654 max_lockdep_depth = curr->lockdep_depth;
2656 return 1;
2659 static int
2660 print_unlock_inbalance_bug(struct task_struct *curr, struct lockdep_map *lock,
2661 unsigned long ip)
2663 if (!debug_locks_off())
2664 return 0;
2665 if (debug_locks_silent)
2666 return 0;
2668 printk("\n=====================================\n");
2669 printk( "[ BUG: bad unlock balance detected! ]\n");
2670 printk( "-------------------------------------\n");
2671 printk("%s/%d is trying to release lock (",
2672 curr->comm, task_pid_nr(curr));
2673 print_lockdep_cache(lock);
2674 printk(") at:\n");
2675 print_ip_sym(ip);
2676 printk("but there are no more locks to release!\n");
2677 printk("\nother info that might help us debug this:\n");
2678 lockdep_print_held_locks(curr);
2680 printk("\nstack backtrace:\n");
2681 dump_stack();
2683 return 0;
2687 * Common debugging checks for both nested and non-nested unlock:
2689 static int check_unlock(struct task_struct *curr, struct lockdep_map *lock,
2690 unsigned long ip)
2692 if (unlikely(!debug_locks))
2693 return 0;
2694 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2695 return 0;
2697 if (curr->lockdep_depth <= 0)
2698 return print_unlock_inbalance_bug(curr, lock, ip);
2700 return 1;
2703 static int
2704 __lock_set_class(struct lockdep_map *lock, const char *name,
2705 struct lock_class_key *key, unsigned int subclass,
2706 unsigned long ip)
2708 struct task_struct *curr = current;
2709 struct held_lock *hlock, *prev_hlock;
2710 struct lock_class *class;
2711 unsigned int depth;
2712 int i;
2714 depth = curr->lockdep_depth;
2715 if (DEBUG_LOCKS_WARN_ON(!depth))
2716 return 0;
2718 prev_hlock = NULL;
2719 for (i = depth-1; i >= 0; i--) {
2720 hlock = curr->held_locks + i;
2722 * We must not cross into another context:
2724 if (prev_hlock && prev_hlock->irq_context != hlock->irq_context)
2725 break;
2726 if (hlock->instance == lock)
2727 goto found_it;
2728 prev_hlock = hlock;
2730 return print_unlock_inbalance_bug(curr, lock, ip);
2732 found_it:
2733 lockdep_init_map(lock, name, key, 0);
2734 class = register_lock_class(lock, subclass, 0);
2735 hlock->class_idx = class - lock_classes + 1;
2737 curr->lockdep_depth = i;
2738 curr->curr_chain_key = hlock->prev_chain_key;
2740 for (; i < depth; i++) {
2741 hlock = curr->held_locks + i;
2742 if (!__lock_acquire(hlock->instance,
2743 hlock_class(hlock)->subclass, hlock->trylock,
2744 hlock->read, hlock->check, hlock->hardirqs_off,
2745 hlock->nest_lock, hlock->acquire_ip))
2746 return 0;
2749 if (DEBUG_LOCKS_WARN_ON(curr->lockdep_depth != depth))
2750 return 0;
2751 return 1;
2755 * Remove the lock to the list of currently held locks in a
2756 * potentially non-nested (out of order) manner. This is a
2757 * relatively rare operation, as all the unlock APIs default
2758 * to nested mode (which uses lock_release()):
2760 static int
2761 lock_release_non_nested(struct task_struct *curr,
2762 struct lockdep_map *lock, unsigned long ip)
2764 struct held_lock *hlock, *prev_hlock;
2765 unsigned int depth;
2766 int i;
2769 * Check whether the lock exists in the current stack
2770 * of held locks:
2772 depth = curr->lockdep_depth;
2773 if (DEBUG_LOCKS_WARN_ON(!depth))
2774 return 0;
2776 prev_hlock = NULL;
2777 for (i = depth-1; i >= 0; i--) {
2778 hlock = curr->held_locks + i;
2780 * We must not cross into another context:
2782 if (prev_hlock && prev_hlock->irq_context != hlock->irq_context)
2783 break;
2784 if (hlock->instance == lock)
2785 goto found_it;
2786 prev_hlock = hlock;
2788 return print_unlock_inbalance_bug(curr, lock, ip);
2790 found_it:
2791 lock_release_holdtime(hlock);
2794 * We have the right lock to unlock, 'hlock' points to it.
2795 * Now we remove it from the stack, and add back the other
2796 * entries (if any), recalculating the hash along the way:
2798 curr->lockdep_depth = i;
2799 curr->curr_chain_key = hlock->prev_chain_key;
2801 for (i++; i < depth; i++) {
2802 hlock = curr->held_locks + i;
2803 if (!__lock_acquire(hlock->instance,
2804 hlock_class(hlock)->subclass, hlock->trylock,
2805 hlock->read, hlock->check, hlock->hardirqs_off,
2806 hlock->nest_lock, hlock->acquire_ip))
2807 return 0;
2810 if (DEBUG_LOCKS_WARN_ON(curr->lockdep_depth != depth - 1))
2811 return 0;
2812 return 1;
2816 * Remove the lock to the list of currently held locks - this gets
2817 * called on mutex_unlock()/spin_unlock*() (or on a failed
2818 * mutex_lock_interruptible()). This is done for unlocks that nest
2819 * perfectly. (i.e. the current top of the lock-stack is unlocked)
2821 static int lock_release_nested(struct task_struct *curr,
2822 struct lockdep_map *lock, unsigned long ip)
2824 struct held_lock *hlock;
2825 unsigned int depth;
2828 * Pop off the top of the lock stack:
2830 depth = curr->lockdep_depth - 1;
2831 hlock = curr->held_locks + depth;
2834 * Is the unlock non-nested:
2836 if (hlock->instance != lock)
2837 return lock_release_non_nested(curr, lock, ip);
2838 curr->lockdep_depth--;
2840 if (DEBUG_LOCKS_WARN_ON(!depth && (hlock->prev_chain_key != 0)))
2841 return 0;
2843 curr->curr_chain_key = hlock->prev_chain_key;
2845 lock_release_holdtime(hlock);
2847 #ifdef CONFIG_DEBUG_LOCKDEP
2848 hlock->prev_chain_key = 0;
2849 hlock->class_idx = 0;
2850 hlock->acquire_ip = 0;
2851 hlock->irq_context = 0;
2852 #endif
2853 return 1;
2857 * Remove the lock to the list of currently held locks - this gets
2858 * called on mutex_unlock()/spin_unlock*() (or on a failed
2859 * mutex_lock_interruptible()). This is done for unlocks that nest
2860 * perfectly. (i.e. the current top of the lock-stack is unlocked)
2862 static void
2863 __lock_release(struct lockdep_map *lock, int nested, unsigned long ip)
2865 struct task_struct *curr = current;
2867 if (!check_unlock(curr, lock, ip))
2868 return;
2870 if (nested) {
2871 if (!lock_release_nested(curr, lock, ip))
2872 return;
2873 } else {
2874 if (!lock_release_non_nested(curr, lock, ip))
2875 return;
2878 check_chain_key(curr);
2882 * Check whether we follow the irq-flags state precisely:
2884 static void check_flags(unsigned long flags)
2886 #if defined(CONFIG_PROVE_LOCKING) && defined(CONFIG_DEBUG_LOCKDEP) && \
2887 defined(CONFIG_TRACE_IRQFLAGS)
2888 if (!debug_locks)
2889 return;
2891 if (irqs_disabled_flags(flags)) {
2892 if (DEBUG_LOCKS_WARN_ON(current->hardirqs_enabled)) {
2893 printk("possible reason: unannotated irqs-off.\n");
2895 } else {
2896 if (DEBUG_LOCKS_WARN_ON(!current->hardirqs_enabled)) {
2897 printk("possible reason: unannotated irqs-on.\n");
2902 * We dont accurately track softirq state in e.g.
2903 * hardirq contexts (such as on 4KSTACKS), so only
2904 * check if not in hardirq contexts:
2906 if (!hardirq_count()) {
2907 if (softirq_count())
2908 DEBUG_LOCKS_WARN_ON(current->softirqs_enabled);
2909 else
2910 DEBUG_LOCKS_WARN_ON(!current->softirqs_enabled);
2913 if (!debug_locks)
2914 print_irqtrace_events(current);
2915 #endif
2918 void lock_set_class(struct lockdep_map *lock, const char *name,
2919 struct lock_class_key *key, unsigned int subclass,
2920 unsigned long ip)
2922 unsigned long flags;
2924 if (unlikely(current->lockdep_recursion))
2925 return;
2927 raw_local_irq_save(flags);
2928 current->lockdep_recursion = 1;
2929 check_flags(flags);
2930 if (__lock_set_class(lock, name, key, subclass, ip))
2931 check_chain_key(current);
2932 current->lockdep_recursion = 0;
2933 raw_local_irq_restore(flags);
2935 EXPORT_SYMBOL_GPL(lock_set_class);
2937 DEFINE_TRACE(lock_acquire);
2940 * We are not always called with irqs disabled - do that here,
2941 * and also avoid lockdep recursion:
2943 void lock_acquire(struct lockdep_map *lock, unsigned int subclass,
2944 int trylock, int read, int check,
2945 struct lockdep_map *nest_lock, unsigned long ip)
2947 unsigned long flags;
2949 trace_lock_acquire(lock, subclass, trylock, read, check, nest_lock, ip);
2951 if (unlikely(current->lockdep_recursion))
2952 return;
2954 raw_local_irq_save(flags);
2955 check_flags(flags);
2957 current->lockdep_recursion = 1;
2958 __lock_acquire(lock, subclass, trylock, read, check,
2959 irqs_disabled_flags(flags), nest_lock, ip);
2960 current->lockdep_recursion = 0;
2961 raw_local_irq_restore(flags);
2963 EXPORT_SYMBOL_GPL(lock_acquire);
2965 DEFINE_TRACE(lock_release);
2967 void lock_release(struct lockdep_map *lock, int nested,
2968 unsigned long ip)
2970 unsigned long flags;
2972 trace_lock_release(lock, nested, ip);
2974 if (unlikely(current->lockdep_recursion))
2975 return;
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);
2984 EXPORT_SYMBOL_GPL(lock_release);
2986 void lockdep_set_current_reclaim_state(gfp_t gfp_mask)
2988 current->lockdep_reclaim_gfp = gfp_mask;
2991 void lockdep_clear_current_reclaim_state(void)
2993 current->lockdep_reclaim_gfp = 0;
2996 #ifdef CONFIG_LOCK_STAT
2997 static int
2998 print_lock_contention_bug(struct task_struct *curr, struct lockdep_map *lock,
2999 unsigned long ip)
3001 if (!debug_locks_off())
3002 return 0;
3003 if (debug_locks_silent)
3004 return 0;
3006 printk("\n=================================\n");
3007 printk( "[ BUG: bad contention detected! ]\n");
3008 printk( "---------------------------------\n");
3009 printk("%s/%d is trying to contend lock (",
3010 curr->comm, task_pid_nr(curr));
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);
3018 printk("\nstack backtrace:\n");
3019 dump_stack();
3021 return 0;
3024 static void
3025 __lock_contended(struct lockdep_map *lock, unsigned long ip)
3027 struct task_struct *curr = current;
3028 struct held_lock *hlock, *prev_hlock;
3029 struct lock_class_stats *stats;
3030 unsigned int depth;
3031 int i, contention_point, contending_point;
3033 depth = curr->lockdep_depth;
3034 if (DEBUG_LOCKS_WARN_ON(!depth))
3035 return;
3037 prev_hlock = NULL;
3038 for (i = depth-1; i >= 0; i--) {
3039 hlock = curr->held_locks + i;
3041 * We must not cross into another context:
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;
3049 print_lock_contention_bug(curr, lock, ip);
3050 return;
3052 found_it:
3053 hlock->waittime_stamp = sched_clock();
3055 contention_point = lock_point(hlock_class(hlock)->contention_point, ip);
3056 contending_point = lock_point(hlock_class(hlock)->contending_point,
3057 lock->ip);
3059 stats = get_lock_stats(hlock_class(hlock));
3060 if (contention_point < LOCKSTAT_POINTS)
3061 stats->contention_point[contention_point]++;
3062 if (contending_point < LOCKSTAT_POINTS)
3063 stats->contending_point[contending_point]++;
3064 if (lock->cpu != smp_processor_id())
3065 stats->bounces[bounce_contended + !!hlock->read]++;
3066 put_lock_stats(stats);
3069 static void
3070 __lock_acquired(struct lockdep_map *lock, unsigned long ip)
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;
3077 s64 waittime = 0;
3078 int i, cpu;
3080 depth = curr->lockdep_depth;
3081 if (DEBUG_LOCKS_WARN_ON(!depth))
3082 return;
3084 prev_hlock = NULL;
3085 for (i = depth-1; i >= 0; i--) {
3086 hlock = curr->held_locks + i;
3088 * We must not cross into another context:
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;
3096 print_lock_contention_bug(curr, lock, _RET_IP_);
3097 return;
3099 found_it:
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;
3107 stats = get_lock_stats(hlock_class(hlock));
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);
3114 if (lock->cpu != cpu)
3115 stats->bounces[bounce_acquired + !!hlock->read]++;
3116 put_lock_stats(stats);
3118 lock->cpu = cpu;
3119 lock->ip = ip;
3122 DEFINE_TRACE(lock_contended);
3124 void lock_contended(struct lockdep_map *lock, unsigned long ip)
3126 unsigned long flags;
3128 trace_lock_contended(lock, ip);
3130 if (unlikely(!lock_stat))
3131 return;
3133 if (unlikely(current->lockdep_recursion))
3134 return;
3136 raw_local_irq_save(flags);
3137 check_flags(flags);
3138 current->lockdep_recursion = 1;
3139 __lock_contended(lock, ip);
3140 current->lockdep_recursion = 0;
3141 raw_local_irq_restore(flags);
3143 EXPORT_SYMBOL_GPL(lock_contended);
3145 DEFINE_TRACE(lock_acquired);
3147 void lock_acquired(struct lockdep_map *lock, unsigned long ip)
3149 unsigned long flags;
3151 trace_lock_acquired(lock, ip);
3153 if (unlikely(!lock_stat))
3154 return;
3156 if (unlikely(current->lockdep_recursion))
3157 return;
3159 raw_local_irq_save(flags);
3160 check_flags(flags);
3161 current->lockdep_recursion = 1;
3162 __lock_acquired(lock, ip);
3163 current->lockdep_recursion = 0;
3164 raw_local_irq_restore(flags);
3166 EXPORT_SYMBOL_GPL(lock_acquired);
3167 #endif
3170 * Used by the testsuite, sanitize the validator state
3171 * after a simulated failure:
3174 void lockdep_reset(void)
3176 unsigned long flags;
3177 int i;
3179 raw_local_irq_save(flags);
3180 current->curr_chain_key = 0;
3181 current->lockdep_depth = 0;
3182 current->lockdep_recursion = 0;
3183 memset(current->held_locks, 0, MAX_LOCK_DEPTH*sizeof(struct held_lock));
3184 nr_hardirq_chains = 0;
3185 nr_softirq_chains = 0;
3186 nr_process_chains = 0;
3187 debug_locks = 1;
3188 for (i = 0; i < CHAINHASH_SIZE; i++)
3189 INIT_LIST_HEAD(chainhash_table + i);
3190 raw_local_irq_restore(flags);
3193 static void zap_class(struct lock_class *class)
3195 int i;
3198 * Remove all dependencies this lock is
3199 * involved in:
3201 for (i = 0; i < nr_list_entries; i++) {
3202 if (list_entries[i].class == class)
3203 list_del_rcu(&list_entries[i].entry);
3206 * Unhash the class and remove it from the all_lock_classes list:
3208 list_del_rcu(&class->hash_entry);
3209 list_del_rcu(&class->lock_entry);
3211 class->key = NULL;
3214 static inline int within(const void *addr, void *start, unsigned long size)
3216 return addr >= start && addr < start + size;
3219 void lockdep_free_key_range(void *start, unsigned long size)
3221 struct lock_class *class, *next;
3222 struct list_head *head;
3223 unsigned long flags;
3224 int i;
3225 int locked;
3227 raw_local_irq_save(flags);
3228 locked = graph_lock();
3231 * Unhash all classes that were created by this module:
3233 for (i = 0; i < CLASSHASH_SIZE; i++) {
3234 head = classhash_table + i;
3235 if (list_empty(head))
3236 continue;
3237 list_for_each_entry_safe(class, next, head, hash_entry) {
3238 if (within(class->key, start, size))
3239 zap_class(class);
3240 else if (within(class->name, start, size))
3241 zap_class(class);
3245 if (locked)
3246 graph_unlock();
3247 raw_local_irq_restore(flags);
3250 void lockdep_reset_lock(struct lockdep_map *lock)
3252 struct lock_class *class, *next;
3253 struct list_head *head;
3254 unsigned long flags;
3255 int i, j;
3256 int locked;
3258 raw_local_irq_save(flags);
3261 * Remove all classes this lock might have:
3263 for (j = 0; j < MAX_LOCKDEP_SUBCLASSES; j++) {
3265 * If the class exists we look it up and zap it:
3267 class = look_up_lock_class(lock, j);
3268 if (class)
3269 zap_class(class);
3272 * Debug check: in the end all mapped classes should
3273 * be gone.
3275 locked = graph_lock();
3276 for (i = 0; i < CLASSHASH_SIZE; i++) {
3277 head = classhash_table + i;
3278 if (list_empty(head))
3279 continue;
3280 list_for_each_entry_safe(class, next, head, hash_entry) {
3281 if (unlikely(class == lock->class_cache)) {
3282 if (debug_locks_off_graph_unlock())
3283 WARN_ON(1);
3284 goto out_restore;
3288 if (locked)
3289 graph_unlock();
3291 out_restore:
3292 raw_local_irq_restore(flags);
3295 void lockdep_init(void)
3297 int i;
3300 * Some architectures have their own start_kernel()
3301 * code which calls lockdep_init(), while we also
3302 * call lockdep_init() from the start_kernel() itself,
3303 * and we want to initialize the hashes only once:
3305 if (lockdep_initialized)
3306 return;
3308 for (i = 0; i < CLASSHASH_SIZE; i++)
3309 INIT_LIST_HEAD(classhash_table + i);
3311 for (i = 0; i < CHAINHASH_SIZE; i++)
3312 INIT_LIST_HEAD(chainhash_table + i);
3314 lockdep_initialized = 1;
3317 void __init lockdep_info(void)
3319 printk("Lock dependency validator: Copyright (c) 2006 Red Hat, Inc., Ingo Molnar\n");
3321 printk("... MAX_LOCKDEP_SUBCLASSES: %lu\n", MAX_LOCKDEP_SUBCLASSES);
3322 printk("... MAX_LOCK_DEPTH: %lu\n", MAX_LOCK_DEPTH);
3323 printk("... MAX_LOCKDEP_KEYS: %lu\n", MAX_LOCKDEP_KEYS);
3324 printk("... CLASSHASH_SIZE: %lu\n", CLASSHASH_SIZE);
3325 printk("... MAX_LOCKDEP_ENTRIES: %lu\n", MAX_LOCKDEP_ENTRIES);
3326 printk("... MAX_LOCKDEP_CHAINS: %lu\n", MAX_LOCKDEP_CHAINS);
3327 printk("... CHAINHASH_SIZE: %lu\n", CHAINHASH_SIZE);
3329 printk(" memory used by lock dependency info: %lu kB\n",
3330 (sizeof(struct lock_class) * MAX_LOCKDEP_KEYS +
3331 sizeof(struct list_head) * CLASSHASH_SIZE +
3332 sizeof(struct lock_list) * MAX_LOCKDEP_ENTRIES +
3333 sizeof(struct lock_chain) * MAX_LOCKDEP_CHAINS +
3334 sizeof(struct list_head) * CHAINHASH_SIZE) / 1024);
3336 printk(" per task-struct memory footprint: %lu bytes\n",
3337 sizeof(struct held_lock) * MAX_LOCK_DEPTH);
3339 #ifdef CONFIG_DEBUG_LOCKDEP
3340 if (lockdep_init_error) {
3341 printk("WARNING: lockdep init error! Arch code didn't call lockdep_init() early enough?\n");
3342 printk("Call stack leading to lockdep invocation was:\n");
3343 print_stack_trace(&lockdep_init_trace, 0);
3345 #endif
3348 static void
3349 print_freed_lock_bug(struct task_struct *curr, const void *mem_from,
3350 const void *mem_to, struct held_lock *hlock)
3352 if (!debug_locks_off())
3353 return;
3354 if (debug_locks_silent)
3355 return;
3357 printk("\n=========================\n");
3358 printk( "[ BUG: held lock freed! ]\n");
3359 printk( "-------------------------\n");
3360 printk("%s/%d is freeing memory %p-%p, with a lock still held there!\n",
3361 curr->comm, task_pid_nr(curr), mem_from, mem_to-1);
3362 print_lock(hlock);
3363 lockdep_print_held_locks(curr);
3365 printk("\nstack backtrace:\n");
3366 dump_stack();
3369 static inline int not_in_range(const void* mem_from, unsigned long mem_len,
3370 const void* lock_from, unsigned long lock_len)
3372 return lock_from + lock_len <= mem_from ||
3373 mem_from + mem_len <= lock_from;
3377 * Called when kernel memory is freed (or unmapped), or if a lock
3378 * is destroyed or reinitialized - this code checks whether there is
3379 * any held lock in the memory range of <from> to <to>:
3381 void debug_check_no_locks_freed(const void *mem_from, unsigned long mem_len)
3383 struct task_struct *curr = current;
3384 struct held_lock *hlock;
3385 unsigned long flags;
3386 int i;
3388 if (unlikely(!debug_locks))
3389 return;
3391 local_irq_save(flags);
3392 for (i = 0; i < curr->lockdep_depth; i++) {
3393 hlock = curr->held_locks + i;
3395 if (not_in_range(mem_from, mem_len, hlock->instance,
3396 sizeof(*hlock->instance)))
3397 continue;
3399 print_freed_lock_bug(curr, mem_from, mem_from + mem_len, hlock);
3400 break;
3402 local_irq_restore(flags);
3404 EXPORT_SYMBOL_GPL(debug_check_no_locks_freed);
3406 static void print_held_locks_bug(struct task_struct *curr)
3408 if (!debug_locks_off())
3409 return;
3410 if (debug_locks_silent)
3411 return;
3413 printk("\n=====================================\n");
3414 printk( "[ BUG: lock held at task exit time! ]\n");
3415 printk( "-------------------------------------\n");
3416 printk("%s/%d is exiting with locks still held!\n",
3417 curr->comm, task_pid_nr(curr));
3418 lockdep_print_held_locks(curr);
3420 printk("\nstack backtrace:\n");
3421 dump_stack();
3424 void debug_check_no_locks_held(struct task_struct *task)
3426 if (unlikely(task->lockdep_depth > 0))
3427 print_held_locks_bug(task);
3430 void debug_show_all_locks(void)
3432 struct task_struct *g, *p;
3433 int count = 10;
3434 int unlock = 1;
3436 if (unlikely(!debug_locks)) {
3437 printk("INFO: lockdep is turned off.\n");
3438 return;
3440 printk("\nShowing all locks held in the system:\n");
3443 * Here we try to get the tasklist_lock as hard as possible,
3444 * if not successful after 2 seconds we ignore it (but keep
3445 * trying). This is to enable a debug printout even if a
3446 * tasklist_lock-holding task deadlocks or crashes.
3448 retry:
3449 if (!read_trylock(&tasklist_lock)) {
3450 if (count == 10)
3451 printk("hm, tasklist_lock locked, retrying... ");
3452 if (count) {
3453 count--;
3454 printk(" #%d", 10-count);
3455 mdelay(200);
3456 goto retry;
3458 printk(" ignoring it.\n");
3459 unlock = 0;
3460 } else {
3461 if (count != 10)
3462 printk(KERN_CONT " locked it.\n");
3465 do_each_thread(g, p) {
3467 * It's not reliable to print a task's held locks
3468 * if it's not sleeping (or if it's not the current
3469 * task):
3471 if (p->state == TASK_RUNNING && p != current)
3472 continue;
3473 if (p->lockdep_depth)
3474 lockdep_print_held_locks(p);
3475 if (!unlock)
3476 if (read_trylock(&tasklist_lock))
3477 unlock = 1;
3478 } while_each_thread(g, p);
3480 printk("\n");
3481 printk("=============================================\n\n");
3483 if (unlock)
3484 read_unlock(&tasklist_lock);
3486 EXPORT_SYMBOL_GPL(debug_show_all_locks);
3489 * Careful: only use this function if you are sure that
3490 * the task cannot run in parallel!
3492 void __debug_show_held_locks(struct task_struct *task)
3494 if (unlikely(!debug_locks)) {
3495 printk("INFO: lockdep is turned off.\n");
3496 return;
3498 lockdep_print_held_locks(task);
3500 EXPORT_SYMBOL_GPL(__debug_show_held_locks);
3502 void debug_show_held_locks(struct task_struct *task)
3504 __debug_show_held_locks(task);
3506 EXPORT_SYMBOL_GPL(debug_show_held_locks);
3508 void lockdep_sys_exit(void)
3510 struct task_struct *curr = current;
3512 if (unlikely(curr->lockdep_depth)) {
3513 if (!debug_locks_off())
3514 return;
3515 printk("\n================================================\n");
3516 printk( "[ BUG: lock held when returning to user space! ]\n");
3517 printk( "------------------------------------------------\n");
3518 printk("%s/%d is leaving the kernel with locks still held!\n",
3519 curr->comm, curr->pid);
3520 lockdep_print_held_locks(curr);