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>
46 #include <asm/sections.h>
48 #include "lockdep_internals.h"
50 #ifdef CONFIG_PROVE_LOCKING
51 int prove_locking = 1;
52 module_param(prove_locking, int, 0644);
54 #define prove_locking 0
57 #ifdef CONFIG_LOCK_STAT
59 module_param(lock_stat, int, 0644);
65 * lockdep_lock: protects the lockdep graph, the hashes and the
66 * class/list/hash allocators.
68 * This is one of the rare exceptions where it's justified
69 * to use a raw spinlock - we really dont want the spinlock
70 * code to recurse back into the lockdep code...
72 static raw_spinlock_t lockdep_lock = (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED;
74 static int graph_lock(void)
76 __raw_spin_lock(&lockdep_lock);
78 * Make sure that if another CPU detected a bug while
79 * walking the graph we dont change it (while the other
80 * CPU is busy printing out stuff with the graph lock
84 __raw_spin_unlock(&lockdep_lock);
87 /* prevent any recursions within lockdep from causing deadlocks */
88 current->lockdep_recursion++;
92 static inline int graph_unlock(void)
94 if (debug_locks && !__raw_spin_is_locked(&lockdep_lock))
95 return DEBUG_LOCKS_WARN_ON(1);
97 current->lockdep_recursion--;
98 __raw_spin_unlock(&lockdep_lock);
103 * Turn lock debugging off and return with 0 if it was off already,
104 * and also release the graph lock:
106 static inline int debug_locks_off_graph_unlock(void)
108 int ret = debug_locks_off();
110 __raw_spin_unlock(&lockdep_lock);
115 static int lockdep_initialized;
117 unsigned long nr_list_entries;
118 static struct lock_list list_entries[MAX_LOCKDEP_ENTRIES];
121 * All data structures here are protected by the global debug_lock.
123 * Mutex key structs only get allocated, once during bootup, and never
124 * get freed - this significantly simplifies the debugging code.
126 unsigned long nr_lock_classes;
127 static struct lock_class lock_classes[MAX_LOCKDEP_KEYS];
129 static inline struct lock_class *hlock_class(struct held_lock *hlock)
131 if (!hlock->class_idx) {
132 DEBUG_LOCKS_WARN_ON(1);
135 return lock_classes + hlock->class_idx - 1;
138 #ifdef CONFIG_LOCK_STAT
139 static DEFINE_PER_CPU(struct lock_class_stats[MAX_LOCKDEP_KEYS], lock_stats);
141 static int lock_point(unsigned long points[], unsigned long ip)
145 for (i = 0; i < LOCKSTAT_POINTS; i++) {
146 if (points[i] == 0) {
157 static void lock_time_inc(struct lock_time *lt, s64 time)
162 if (time < lt->min || !lt->min)
169 static inline void lock_time_add(struct lock_time *src, struct lock_time *dst)
171 dst->min += src->min;
172 dst->max += src->max;
173 dst->total += src->total;
177 struct lock_class_stats lock_stats(struct lock_class *class)
179 struct lock_class_stats stats;
182 memset(&stats, 0, sizeof(struct lock_class_stats));
183 for_each_possible_cpu(cpu) {
184 struct lock_class_stats *pcs =
185 &per_cpu(lock_stats, cpu)[class - lock_classes];
187 for (i = 0; i < ARRAY_SIZE(stats.contention_point); i++)
188 stats.contention_point[i] += pcs->contention_point[i];
190 for (i = 0; i < ARRAY_SIZE(stats.contending_point); i++)
191 stats.contending_point[i] += pcs->contending_point[i];
193 lock_time_add(&pcs->read_waittime, &stats.read_waittime);
194 lock_time_add(&pcs->write_waittime, &stats.write_waittime);
196 lock_time_add(&pcs->read_holdtime, &stats.read_holdtime);
197 lock_time_add(&pcs->write_holdtime, &stats.write_holdtime);
199 for (i = 0; i < ARRAY_SIZE(stats.bounces); i++)
200 stats.bounces[i] += pcs->bounces[i];
206 void clear_lock_stats(struct lock_class *class)
210 for_each_possible_cpu(cpu) {
211 struct lock_class_stats *cpu_stats =
212 &per_cpu(lock_stats, cpu)[class - lock_classes];
214 memset(cpu_stats, 0, sizeof(struct lock_class_stats));
216 memset(class->contention_point, 0, sizeof(class->contention_point));
217 memset(class->contending_point, 0, sizeof(class->contending_point));
220 static struct lock_class_stats *get_lock_stats(struct lock_class *class)
222 return &get_cpu_var(lock_stats)[class - lock_classes];
225 static void put_lock_stats(struct lock_class_stats *stats)
227 put_cpu_var(lock_stats);
230 static void lock_release_holdtime(struct held_lock *hlock)
232 struct lock_class_stats *stats;
238 holdtime = sched_clock() - hlock->holdtime_stamp;
240 stats = get_lock_stats(hlock_class(hlock));
242 lock_time_inc(&stats->read_holdtime, holdtime);
244 lock_time_inc(&stats->write_holdtime, holdtime);
245 put_lock_stats(stats);
248 static inline void lock_release_holdtime(struct held_lock *hlock)
254 * We keep a global list of all lock classes. The list only grows,
255 * never shrinks. The list is only accessed with the lockdep
256 * spinlock lock held.
258 LIST_HEAD(all_lock_classes);
261 * The lockdep classes are in a hash-table as well, for fast lookup:
263 #define CLASSHASH_BITS (MAX_LOCKDEP_KEYS_BITS - 1)
264 #define CLASSHASH_SIZE (1UL << CLASSHASH_BITS)
265 #define __classhashfn(key) hash_long((unsigned long)key, CLASSHASH_BITS)
266 #define classhashentry(key) (classhash_table + __classhashfn((key)))
268 static struct list_head classhash_table[CLASSHASH_SIZE];
271 * We put the lock dependency chains into a hash-table as well, to cache
274 #define CHAINHASH_BITS (MAX_LOCKDEP_CHAINS_BITS-1)
275 #define CHAINHASH_SIZE (1UL << CHAINHASH_BITS)
276 #define __chainhashfn(chain) hash_long(chain, CHAINHASH_BITS)
277 #define chainhashentry(chain) (chainhash_table + __chainhashfn((chain)))
279 static struct list_head chainhash_table[CHAINHASH_SIZE];
282 * The hash key of the lock dependency chains is a hash itself too:
283 * it's a hash of all locks taken up to that lock, including that lock.
284 * It's a 64-bit hash, because it's important for the keys to be
287 #define iterate_chain_key(key1, key2) \
288 (((key1) << MAX_LOCKDEP_KEYS_BITS) ^ \
289 ((key1) >> (64-MAX_LOCKDEP_KEYS_BITS)) ^ \
292 void lockdep_off(void)
294 current->lockdep_recursion++;
296 EXPORT_SYMBOL(lockdep_off);
298 void lockdep_on(void)
300 current->lockdep_recursion--;
302 EXPORT_SYMBOL(lockdep_on);
305 * Debugging switches:
309 #define VERY_VERBOSE 0
312 # define HARDIRQ_VERBOSE 1
313 # define SOFTIRQ_VERBOSE 1
314 # define RECLAIM_VERBOSE 1
316 # define HARDIRQ_VERBOSE 0
317 # define SOFTIRQ_VERBOSE 0
318 # define RECLAIM_VERBOSE 0
321 #if VERBOSE || HARDIRQ_VERBOSE || SOFTIRQ_VERBOSE || RECLAIM_VERBOSE
323 * Quick filtering for interesting events:
325 static int class_filter(struct lock_class *class)
329 if (class->name_version == 1 &&
330 !strcmp(class->name, "lockname"))
332 if (class->name_version == 1 &&
333 !strcmp(class->name, "&struct->lockfield"))
336 /* Filter everything else. 1 would be to allow everything else */
341 static int verbose(struct lock_class *class)
344 return class_filter(class);
350 * Stack-trace: tightly packed array of stack backtrace
351 * addresses. Protected by the graph_lock.
353 unsigned long nr_stack_trace_entries;
354 static unsigned long stack_trace[MAX_STACK_TRACE_ENTRIES];
356 static int save_trace(struct stack_trace *trace)
358 trace->nr_entries = 0;
359 trace->max_entries = MAX_STACK_TRACE_ENTRIES - nr_stack_trace_entries;
360 trace->entries = stack_trace + nr_stack_trace_entries;
364 save_stack_trace(trace);
366 trace->max_entries = trace->nr_entries;
368 nr_stack_trace_entries += trace->nr_entries;
370 if (nr_stack_trace_entries == MAX_STACK_TRACE_ENTRIES) {
371 if (!debug_locks_off_graph_unlock())
374 printk("BUG: MAX_STACK_TRACE_ENTRIES too low!\n");
375 printk("turning off the locking correctness validator.\n");
384 unsigned int nr_hardirq_chains;
385 unsigned int nr_softirq_chains;
386 unsigned int nr_process_chains;
387 unsigned int max_lockdep_depth;
388 unsigned int max_recursion_depth;
390 static unsigned int lockdep_dependency_gen_id;
392 static bool lockdep_dependency_visit(struct lock_class *source,
396 lockdep_dependency_gen_id++;
397 if (source->dep_gen_id == lockdep_dependency_gen_id)
399 source->dep_gen_id = lockdep_dependency_gen_id;
403 #ifdef CONFIG_DEBUG_LOCKDEP
405 * We cannot printk in early bootup code. Not even early_printk()
406 * might work. So we mark any initialization errors and printk
407 * about it later on, in lockdep_info().
409 static int lockdep_init_error;
410 static unsigned long lockdep_init_trace_data[20];
411 static struct stack_trace lockdep_init_trace = {
412 .max_entries = ARRAY_SIZE(lockdep_init_trace_data),
413 .entries = lockdep_init_trace_data,
417 * Various lockdep statistics:
419 atomic_t chain_lookup_hits;
420 atomic_t chain_lookup_misses;
421 atomic_t hardirqs_on_events;
422 atomic_t hardirqs_off_events;
423 atomic_t redundant_hardirqs_on;
424 atomic_t redundant_hardirqs_off;
425 atomic_t softirqs_on_events;
426 atomic_t softirqs_off_events;
427 atomic_t redundant_softirqs_on;
428 atomic_t redundant_softirqs_off;
429 atomic_t nr_unused_locks;
430 atomic_t nr_cyclic_checks;
431 atomic_t nr_cyclic_check_recursions;
432 atomic_t nr_find_usage_forwards_checks;
433 atomic_t nr_find_usage_forwards_recursions;
434 atomic_t nr_find_usage_backwards_checks;
435 atomic_t nr_find_usage_backwards_recursions;
442 #define __USAGE(__STATE) \
443 [LOCK_USED_IN_##__STATE] = "IN-"__stringify(__STATE)"-W", \
444 [LOCK_ENABLED_##__STATE] = __stringify(__STATE)"-ON-W", \
445 [LOCK_USED_IN_##__STATE##_READ] = "IN-"__stringify(__STATE)"-R",\
446 [LOCK_ENABLED_##__STATE##_READ] = __stringify(__STATE)"-ON-R",
448 static const char *usage_str[] =
450 #define LOCKDEP_STATE(__STATE) __USAGE(__STATE)
451 #include "lockdep_states.h"
453 [LOCK_USED] = "INITIAL USE",
456 const char * __get_key_name(struct lockdep_subclass_key *key, char *str)
458 return kallsyms_lookup((unsigned long)key, NULL, NULL, NULL, str);
461 static inline unsigned long lock_flag(enum lock_usage_bit bit)
466 static char get_usage_char(struct lock_class *class, enum lock_usage_bit bit)
470 if (class->usage_mask & lock_flag(bit + 2))
472 if (class->usage_mask & lock_flag(bit)) {
474 if (class->usage_mask & lock_flag(bit + 2))
481 void get_usage_chars(struct lock_class *class, char usage[LOCK_USAGE_CHARS])
485 #define LOCKDEP_STATE(__STATE) \
486 usage[i++] = get_usage_char(class, LOCK_USED_IN_##__STATE); \
487 usage[i++] = get_usage_char(class, LOCK_USED_IN_##__STATE##_READ);
488 #include "lockdep_states.h"
494 static void print_lock_name(struct lock_class *class)
496 char str[KSYM_NAME_LEN], usage[LOCK_USAGE_CHARS];
499 get_usage_chars(class, usage);
503 name = __get_key_name(class->key, str);
504 printk(" (%s", name);
506 printk(" (%s", name);
507 if (class->name_version > 1)
508 printk("#%d", class->name_version);
510 printk("/%d", class->subclass);
512 printk("){%s}", usage);
515 static void print_lockdep_cache(struct lockdep_map *lock)
518 char str[KSYM_NAME_LEN];
522 name = __get_key_name(lock->key->subkeys, str);
527 static void print_lock(struct held_lock *hlock)
529 print_lock_name(hlock_class(hlock));
531 print_ip_sym(hlock->acquire_ip);
534 static void lockdep_print_held_locks(struct task_struct *curr)
536 int i, depth = curr->lockdep_depth;
539 printk("no locks held by %s/%d.\n", curr->comm, task_pid_nr(curr));
542 printk("%d lock%s held by %s/%d:\n",
543 depth, depth > 1 ? "s" : "", curr->comm, task_pid_nr(curr));
545 for (i = 0; i < depth; i++) {
547 print_lock(curr->held_locks + i);
551 static void print_lock_class_header(struct lock_class *class, int depth)
555 printk("%*s->", depth, "");
556 print_lock_name(class);
557 printk(" ops: %lu", class->ops);
560 for (bit = 0; bit < LOCK_USAGE_STATES; bit++) {
561 if (class->usage_mask & (1 << bit)) {
564 len += printk("%*s %s", depth, "", usage_str[bit]);
565 len += printk(" at:\n");
566 print_stack_trace(class->usage_traces + bit, len);
569 printk("%*s }\n", depth, "");
571 printk("%*s ... key at: ",depth,"");
572 print_ip_sym((unsigned long)class->key);
576 * printk all lock dependencies starting at <entry>:
579 print_lock_dependencies(struct lock_class *class, int depth)
581 struct lock_list *entry;
583 if (lockdep_dependency_visit(class, depth))
586 if (DEBUG_LOCKS_WARN_ON(depth >= 20))
589 print_lock_class_header(class, depth);
591 list_for_each_entry(entry, &class->locks_after, entry) {
592 if (DEBUG_LOCKS_WARN_ON(!entry->class))
595 print_lock_dependencies(entry->class, depth + 1);
597 printk("%*s ... acquired at:\n",depth,"");
598 print_stack_trace(&entry->trace, 2);
603 static void print_kernel_version(void)
605 printk("%s %.*s\n", init_utsname()->release,
606 (int)strcspn(init_utsname()->version, " "),
607 init_utsname()->version);
610 static int very_verbose(struct lock_class *class)
613 return class_filter(class);
619 * Is this the address of a static object:
621 static int static_obj(void *obj)
623 unsigned long start = (unsigned long) &_stext,
624 end = (unsigned long) &_end,
625 addr = (unsigned long) obj;
633 if ((addr >= start) && (addr < end))
640 for_each_possible_cpu(i) {
641 start = (unsigned long) &__per_cpu_start + per_cpu_offset(i);
642 end = (unsigned long) &__per_cpu_start + PERCPU_ENOUGH_ROOM
645 if ((addr >= start) && (addr < end))
653 return is_module_address(addr);
657 * To make lock name printouts unique, we calculate a unique
658 * class->name_version generation counter:
660 static int count_matching_names(struct lock_class *new_class)
662 struct lock_class *class;
665 if (!new_class->name)
668 list_for_each_entry(class, &all_lock_classes, lock_entry) {
669 if (new_class->key - new_class->subclass == class->key)
670 return class->name_version;
671 if (class->name && !strcmp(class->name, new_class->name))
672 count = max(count, class->name_version);
679 * Register a lock's class in the hash-table, if the class is not present
680 * yet. Otherwise we look it up. We cache the result in the lock object
681 * itself, so actual lookup of the hash should be once per lock object.
683 static inline struct lock_class *
684 look_up_lock_class(struct lockdep_map *lock, unsigned int subclass)
686 struct lockdep_subclass_key *key;
687 struct list_head *hash_head;
688 struct lock_class *class;
690 #ifdef CONFIG_DEBUG_LOCKDEP
692 * If the architecture calls into lockdep before initializing
693 * the hashes then we'll warn about it later. (we cannot printk
696 if (unlikely(!lockdep_initialized)) {
698 lockdep_init_error = 1;
699 save_stack_trace(&lockdep_init_trace);
704 * Static locks do not have their class-keys yet - for them the key
705 * is the lock object itself:
707 if (unlikely(!lock->key))
708 lock->key = (void *)lock;
711 * NOTE: the class-key must be unique. For dynamic locks, a static
712 * lock_class_key variable is passed in through the mutex_init()
713 * (or spin_lock_init()) call - which acts as the key. For static
714 * locks we use the lock object itself as the key.
716 BUILD_BUG_ON(sizeof(struct lock_class_key) >
717 sizeof(struct lockdep_map));
719 key = lock->key->subkeys + subclass;
721 hash_head = classhashentry(key);
724 * We can walk the hash lockfree, because the hash only
725 * grows, and we are careful when adding entries to the end:
727 list_for_each_entry(class, hash_head, hash_entry) {
728 if (class->key == key) {
729 WARN_ON_ONCE(class->name != lock->name);
738 * Register a lock's class in the hash-table, if the class is not present
739 * yet. Otherwise we look it up. We cache the result in the lock object
740 * itself, so actual lookup of the hash should be once per lock object.
742 static inline struct lock_class *
743 register_lock_class(struct lockdep_map *lock, unsigned int subclass, int force)
745 struct lockdep_subclass_key *key;
746 struct list_head *hash_head;
747 struct lock_class *class;
750 class = look_up_lock_class(lock, subclass);
755 * Debug-check: all keys must be persistent!
757 if (!static_obj(lock->key)) {
759 printk("INFO: trying to register non-static key.\n");
760 printk("the code is fine but needs lockdep annotation.\n");
761 printk("turning off the locking correctness validator.\n");
767 key = lock->key->subkeys + subclass;
768 hash_head = classhashentry(key);
770 raw_local_irq_save(flags);
772 raw_local_irq_restore(flags);
776 * We have to do the hash-walk again, to avoid races
779 list_for_each_entry(class, hash_head, hash_entry)
780 if (class->key == key)
783 * Allocate a new key from the static array, and add it to
786 if (nr_lock_classes >= MAX_LOCKDEP_KEYS) {
787 if (!debug_locks_off_graph_unlock()) {
788 raw_local_irq_restore(flags);
791 raw_local_irq_restore(flags);
793 printk("BUG: MAX_LOCKDEP_KEYS too low!\n");
794 printk("turning off the locking correctness validator.\n");
797 class = lock_classes + nr_lock_classes++;
798 debug_atomic_inc(&nr_unused_locks);
800 class->name = lock->name;
801 class->subclass = subclass;
802 INIT_LIST_HEAD(&class->lock_entry);
803 INIT_LIST_HEAD(&class->locks_before);
804 INIT_LIST_HEAD(&class->locks_after);
805 class->name_version = count_matching_names(class);
807 * We use RCU's safe list-add method to make
808 * parallel walking of the hash-list safe:
810 list_add_tail_rcu(&class->hash_entry, hash_head);
812 * Add it to the global list of classes:
814 list_add_tail_rcu(&class->lock_entry, &all_lock_classes);
816 if (verbose(class)) {
818 raw_local_irq_restore(flags);
820 printk("\nnew class %p: %s", class->key, class->name);
821 if (class->name_version > 1)
822 printk("#%d", class->name_version);
826 raw_local_irq_save(flags);
828 raw_local_irq_restore(flags);
834 raw_local_irq_restore(flags);
836 if (!subclass || force)
837 lock->class_cache = class;
839 if (DEBUG_LOCKS_WARN_ON(class->subclass != subclass))
845 #ifdef CONFIG_PROVE_LOCKING
847 * Allocate a lockdep entry. (assumes the graph_lock held, returns
848 * with NULL on failure)
850 static struct lock_list *alloc_list_entry(void)
852 if (nr_list_entries >= MAX_LOCKDEP_ENTRIES) {
853 if (!debug_locks_off_graph_unlock())
856 printk("BUG: MAX_LOCKDEP_ENTRIES too low!\n");
857 printk("turning off the locking correctness validator.\n");
860 return list_entries + nr_list_entries++;
864 * Add a new dependency to the head of the list:
866 static int add_lock_to_list(struct lock_class *class, struct lock_class *this,
867 struct list_head *head, unsigned long ip, int distance)
869 struct lock_list *entry;
871 * Lock not present yet - get a new dependency struct and
872 * add it to the list:
874 entry = alloc_list_entry();
878 if (!save_trace(&entry->trace))
882 entry->distance = distance;
884 * Since we never remove from the dependency list, the list can
885 * be walked lockless by other CPUs, it's only allocation
886 * that must be protected by the spinlock. But this also means
887 * we must make new entries visible only once writes to the
888 * entry become visible - hence the RCU op:
890 list_add_tail_rcu(&entry->entry, head);
896 * Recursive, forwards-direction lock-dependency checking, used for
897 * both noncyclic checking and for hardirq-unsafe/softirq-unsafe
900 * (to keep the stackframe of the recursive functions small we
901 * use these global variables, and we also mark various helper
902 * functions as noinline.)
904 static struct held_lock *check_source, *check_target;
907 * Print a dependency chain entry (this is only done when a deadlock
908 * has been detected):
911 print_circular_bug_entry(struct lock_list *target, unsigned int depth)
913 if (debug_locks_silent)
915 printk("\n-> #%u", depth);
916 print_lock_name(target->class);
918 print_stack_trace(&target->trace, 6);
924 * When a circular dependency is detected, print the
928 print_circular_bug_header(struct lock_list *entry, unsigned int depth)
930 struct task_struct *curr = current;
932 if (!debug_locks_off_graph_unlock() || debug_locks_silent)
935 printk("\n=======================================================\n");
936 printk( "[ INFO: possible circular locking dependency detected ]\n");
937 print_kernel_version();
938 printk( "-------------------------------------------------------\n");
939 printk("%s/%d is trying to acquire lock:\n",
940 curr->comm, task_pid_nr(curr));
941 print_lock(check_source);
942 printk("\nbut task is already holding lock:\n");
943 print_lock(check_target);
944 printk("\nwhich lock already depends on the new lock.\n\n");
945 printk("\nthe existing dependency chain (in reverse order) is:\n");
947 print_circular_bug_entry(entry, depth);
952 static noinline int print_circular_bug_tail(void)
954 struct task_struct *curr = current;
955 struct lock_list this;
957 if (debug_locks_silent)
960 this.class = hlock_class(check_source);
961 if (!save_trace(&this.trace))
964 print_circular_bug_entry(&this, 0);
966 printk("\nother info that might help us debug this:\n\n");
967 lockdep_print_held_locks(curr);
969 printk("\nstack backtrace:\n");
975 #define RECURSION_LIMIT 40
977 static int noinline print_infinite_recursion_bug(void)
979 if (!debug_locks_off_graph_unlock())
987 unsigned long __lockdep_count_forward_deps(struct lock_class *class,
990 struct lock_list *entry;
991 unsigned long ret = 1;
993 if (lockdep_dependency_visit(class, depth))
997 * Recurse this class's dependency list:
999 list_for_each_entry(entry, &class->locks_after, entry)
1000 ret += __lockdep_count_forward_deps(entry->class, depth + 1);
1005 unsigned long lockdep_count_forward_deps(struct lock_class *class)
1007 unsigned long ret, flags;
1009 local_irq_save(flags);
1010 __raw_spin_lock(&lockdep_lock);
1011 ret = __lockdep_count_forward_deps(class, 0);
1012 __raw_spin_unlock(&lockdep_lock);
1013 local_irq_restore(flags);
1018 unsigned long __lockdep_count_backward_deps(struct lock_class *class,
1021 struct lock_list *entry;
1022 unsigned long ret = 1;
1024 if (lockdep_dependency_visit(class, depth))
1027 * Recurse this class's dependency list:
1029 list_for_each_entry(entry, &class->locks_before, entry)
1030 ret += __lockdep_count_backward_deps(entry->class, depth + 1);
1035 unsigned long lockdep_count_backward_deps(struct lock_class *class)
1037 unsigned long ret, flags;
1039 local_irq_save(flags);
1040 __raw_spin_lock(&lockdep_lock);
1041 ret = __lockdep_count_backward_deps(class, 0);
1042 __raw_spin_unlock(&lockdep_lock);
1043 local_irq_restore(flags);
1049 * Prove that the dependency graph starting at <entry> can not
1050 * lead to <target>. Print an error and return 0 if it does.
1053 check_noncircular(struct lock_class *source, unsigned int depth)
1055 struct lock_list *entry;
1057 if (lockdep_dependency_visit(source, depth))
1060 debug_atomic_inc(&nr_cyclic_check_recursions);
1061 if (depth > max_recursion_depth)
1062 max_recursion_depth = depth;
1063 if (depth >= RECURSION_LIMIT)
1064 return print_infinite_recursion_bug();
1066 * Check this lock's dependency list:
1068 list_for_each_entry(entry, &source->locks_after, entry) {
1069 if (entry->class == hlock_class(check_target))
1070 return print_circular_bug_header(entry, depth+1);
1071 debug_atomic_inc(&nr_cyclic_checks);
1072 if (!check_noncircular(entry->class, depth+1))
1073 return print_circular_bug_entry(entry, depth+1);
1078 #if defined(CONFIG_TRACE_IRQFLAGS) && defined(CONFIG_PROVE_LOCKING)
1080 * Forwards and backwards subgraph searching, for the purposes of
1081 * proving that two subgraphs can be connected by a new dependency
1082 * without creating any illegal irq-safe -> irq-unsafe lock dependency.
1084 static enum lock_usage_bit find_usage_bit;
1085 static struct lock_class *forwards_match, *backwards_match;
1088 * Find a node in the forwards-direction dependency sub-graph starting
1089 * at <source> that matches <find_usage_bit>.
1091 * Return 2 if such a node exists in the subgraph, and put that node
1092 * into <forwards_match>.
1094 * Return 1 otherwise and keep <forwards_match> unchanged.
1095 * Return 0 on error.
1098 find_usage_forwards(struct lock_class *source, unsigned int depth)
1100 struct lock_list *entry;
1103 if (lockdep_dependency_visit(source, depth))
1106 if (depth > max_recursion_depth)
1107 max_recursion_depth = depth;
1108 if (depth >= RECURSION_LIMIT)
1109 return print_infinite_recursion_bug();
1111 debug_atomic_inc(&nr_find_usage_forwards_checks);
1112 if (source->usage_mask & (1 << find_usage_bit)) {
1113 forwards_match = source;
1118 * Check this lock's dependency list:
1120 list_for_each_entry(entry, &source->locks_after, entry) {
1121 debug_atomic_inc(&nr_find_usage_forwards_recursions);
1122 ret = find_usage_forwards(entry->class, depth+1);
1123 if (ret == 2 || ret == 0)
1130 * Find a node in the backwards-direction dependency sub-graph starting
1131 * at <source> that matches <find_usage_bit>.
1133 * Return 2 if such a node exists in the subgraph, and put that node
1134 * into <backwards_match>.
1136 * Return 1 otherwise and keep <backwards_match> unchanged.
1137 * Return 0 on error.
1140 find_usage_backwards(struct lock_class *source, unsigned int depth)
1142 struct lock_list *entry;
1145 if (lockdep_dependency_visit(source, depth))
1148 if (!__raw_spin_is_locked(&lockdep_lock))
1149 return DEBUG_LOCKS_WARN_ON(1);
1151 if (depth > max_recursion_depth)
1152 max_recursion_depth = depth;
1153 if (depth >= RECURSION_LIMIT)
1154 return print_infinite_recursion_bug();
1156 debug_atomic_inc(&nr_find_usage_backwards_checks);
1157 if (source->usage_mask & (1 << find_usage_bit)) {
1158 backwards_match = source;
1162 if (!source && debug_locks_off_graph_unlock()) {
1168 * Check this lock's dependency list:
1170 list_for_each_entry(entry, &source->locks_before, entry) {
1171 debug_atomic_inc(&nr_find_usage_backwards_recursions);
1172 ret = find_usage_backwards(entry->class, depth+1);
1173 if (ret == 2 || ret == 0)
1180 print_bad_irq_dependency(struct task_struct *curr,
1181 struct held_lock *prev,
1182 struct held_lock *next,
1183 enum lock_usage_bit bit1,
1184 enum lock_usage_bit bit2,
1185 const char *irqclass)
1187 if (!debug_locks_off_graph_unlock() || debug_locks_silent)
1190 printk("\n======================================================\n");
1191 printk( "[ INFO: %s-safe -> %s-unsafe lock order detected ]\n",
1192 irqclass, irqclass);
1193 print_kernel_version();
1194 printk( "------------------------------------------------------\n");
1195 printk("%s/%d [HC%u[%lu]:SC%u[%lu]:HE%u:SE%u] is trying to acquire:\n",
1196 curr->comm, task_pid_nr(curr),
1197 curr->hardirq_context, hardirq_count() >> HARDIRQ_SHIFT,
1198 curr->softirq_context, softirq_count() >> SOFTIRQ_SHIFT,
1199 curr->hardirqs_enabled,
1200 curr->softirqs_enabled);
1203 printk("\nand this task is already holding:\n");
1205 printk("which would create a new lock dependency:\n");
1206 print_lock_name(hlock_class(prev));
1208 print_lock_name(hlock_class(next));
1211 printk("\nbut this new dependency connects a %s-irq-safe lock:\n",
1213 print_lock_name(backwards_match);
1214 printk("\n... which became %s-irq-safe at:\n", irqclass);
1216 print_stack_trace(backwards_match->usage_traces + bit1, 1);
1218 printk("\nto a %s-irq-unsafe lock:\n", irqclass);
1219 print_lock_name(forwards_match);
1220 printk("\n... which became %s-irq-unsafe at:\n", irqclass);
1223 print_stack_trace(forwards_match->usage_traces + bit2, 1);
1225 printk("\nother info that might help us debug this:\n\n");
1226 lockdep_print_held_locks(curr);
1228 printk("\nthe %s-irq-safe lock's dependencies:\n", irqclass);
1229 print_lock_dependencies(backwards_match, 0);
1231 printk("\nthe %s-irq-unsafe lock's dependencies:\n", irqclass);
1232 print_lock_dependencies(forwards_match, 0);
1234 printk("\nstack backtrace:\n");
1241 check_usage(struct task_struct *curr, struct held_lock *prev,
1242 struct held_lock *next, enum lock_usage_bit bit_backwards,
1243 enum lock_usage_bit bit_forwards, const char *irqclass)
1247 find_usage_bit = bit_backwards;
1248 /* fills in <backwards_match> */
1249 ret = find_usage_backwards(hlock_class(prev), 0);
1250 if (!ret || ret == 1)
1253 find_usage_bit = bit_forwards;
1254 ret = find_usage_forwards(hlock_class(next), 0);
1255 if (!ret || ret == 1)
1258 return print_bad_irq_dependency(curr, prev, next,
1259 bit_backwards, bit_forwards, irqclass);
1262 static const char *state_names[] = {
1263 #define LOCKDEP_STATE(__STATE) \
1264 __stringify(__STATE),
1265 #include "lockdep_states.h"
1266 #undef LOCKDEP_STATE
1269 static const char *state_rnames[] = {
1270 #define LOCKDEP_STATE(__STATE) \
1271 __stringify(__STATE)"-READ",
1272 #include "lockdep_states.h"
1273 #undef LOCKDEP_STATE
1276 static inline const char *state_name(enum lock_usage_bit bit)
1278 return (bit & 1) ? state_rnames[bit >> 2] : state_names[bit >> 2];
1281 static int exclusive_bit(int new_bit)
1289 * bit 0 - write/read
1290 * bit 1 - used_in/enabled
1294 int state = new_bit & ~3;
1295 int dir = new_bit & 2;
1298 * keep state, bit flip the direction and strip read.
1300 return state | (dir ^ 2);
1303 static int check_irq_usage(struct task_struct *curr, struct held_lock *prev,
1304 struct held_lock *next, enum lock_usage_bit bit)
1307 * Prove that the new dependency does not connect a hardirq-safe
1308 * lock with a hardirq-unsafe lock - to achieve this we search
1309 * the backwards-subgraph starting at <prev>, and the
1310 * forwards-subgraph starting at <next>:
1312 if (!check_usage(curr, prev, next, bit,
1313 exclusive_bit(bit), state_name(bit)))
1319 * Prove that the new dependency does not connect a hardirq-safe-read
1320 * lock with a hardirq-unsafe lock - to achieve this we search
1321 * the backwards-subgraph starting at <prev>, and the
1322 * forwards-subgraph starting at <next>:
1324 if (!check_usage(curr, prev, next, bit,
1325 exclusive_bit(bit), state_name(bit)))
1332 check_prev_add_irq(struct task_struct *curr, struct held_lock *prev,
1333 struct held_lock *next)
1335 #define LOCKDEP_STATE(__STATE) \
1336 if (!check_irq_usage(curr, prev, next, LOCK_USED_IN_##__STATE)) \
1338 #include "lockdep_states.h"
1339 #undef LOCKDEP_STATE
1344 static void inc_chains(void)
1346 if (current->hardirq_context)
1347 nr_hardirq_chains++;
1349 if (current->softirq_context)
1350 nr_softirq_chains++;
1352 nr_process_chains++;
1359 check_prev_add_irq(struct task_struct *curr, struct held_lock *prev,
1360 struct held_lock *next)
1365 static inline void inc_chains(void)
1367 nr_process_chains++;
1373 print_deadlock_bug(struct task_struct *curr, struct held_lock *prev,
1374 struct held_lock *next)
1376 if (!debug_locks_off_graph_unlock() || debug_locks_silent)
1379 printk("\n=============================================\n");
1380 printk( "[ INFO: possible recursive locking detected ]\n");
1381 print_kernel_version();
1382 printk( "---------------------------------------------\n");
1383 printk("%s/%d is trying to acquire lock:\n",
1384 curr->comm, task_pid_nr(curr));
1386 printk("\nbut task is already holding lock:\n");
1389 printk("\nother info that might help us debug this:\n");
1390 lockdep_print_held_locks(curr);
1392 printk("\nstack backtrace:\n");
1399 * Check whether we are holding such a class already.
1401 * (Note that this has to be done separately, because the graph cannot
1402 * detect such classes of deadlocks.)
1404 * Returns: 0 on deadlock detected, 1 on OK, 2 on recursive read
1407 check_deadlock(struct task_struct *curr, struct held_lock *next,
1408 struct lockdep_map *next_instance, int read)
1410 struct held_lock *prev;
1411 struct held_lock *nest = NULL;
1414 for (i = 0; i < curr->lockdep_depth; i++) {
1415 prev = curr->held_locks + i;
1417 if (prev->instance == next->nest_lock)
1420 if (hlock_class(prev) != hlock_class(next))
1424 * Allow read-after-read recursion of the same
1425 * lock class (i.e. read_lock(lock)+read_lock(lock)):
1427 if ((read == 2) && prev->read)
1431 * We're holding the nest_lock, which serializes this lock's
1432 * nesting behaviour.
1437 return print_deadlock_bug(curr, prev, next);
1443 * There was a chain-cache miss, and we are about to add a new dependency
1444 * to a previous lock. We recursively validate the following rules:
1446 * - would the adding of the <prev> -> <next> dependency create a
1447 * circular dependency in the graph? [== circular deadlock]
1449 * - does the new prev->next dependency connect any hardirq-safe lock
1450 * (in the full backwards-subgraph starting at <prev>) with any
1451 * hardirq-unsafe lock (in the full forwards-subgraph starting at
1452 * <next>)? [== illegal lock inversion with hardirq contexts]
1454 * - does the new prev->next dependency connect any softirq-safe lock
1455 * (in the full backwards-subgraph starting at <prev>) with any
1456 * softirq-unsafe lock (in the full forwards-subgraph starting at
1457 * <next>)? [== illegal lock inversion with softirq contexts]
1459 * any of these scenarios could lead to a deadlock.
1461 * Then if all the validations pass, we add the forwards and backwards
1465 check_prev_add(struct task_struct *curr, struct held_lock *prev,
1466 struct held_lock *next, int distance)
1468 struct lock_list *entry;
1472 * Prove that the new <prev> -> <next> dependency would not
1473 * create a circular dependency in the graph. (We do this by
1474 * forward-recursing into the graph starting at <next>, and
1475 * checking whether we can reach <prev>.)
1477 * We are using global variables to control the recursion, to
1478 * keep the stackframe size of the recursive functions low:
1480 check_source = next;
1481 check_target = prev;
1482 if (!(check_noncircular(hlock_class(next), 0)))
1483 return print_circular_bug_tail();
1485 if (!check_prev_add_irq(curr, prev, next))
1489 * For recursive read-locks we do all the dependency checks,
1490 * but we dont store read-triggered dependencies (only
1491 * write-triggered dependencies). This ensures that only the
1492 * write-side dependencies matter, and that if for example a
1493 * write-lock never takes any other locks, then the reads are
1494 * equivalent to a NOP.
1496 if (next->read == 2 || prev->read == 2)
1499 * Is the <prev> -> <next> dependency already present?
1501 * (this may occur even though this is a new chain: consider
1502 * e.g. the L1 -> L2 -> L3 -> L4 and the L5 -> L1 -> L2 -> L3
1503 * chains - the second one will be new, but L1 already has
1504 * L2 added to its dependency list, due to the first chain.)
1506 list_for_each_entry(entry, &hlock_class(prev)->locks_after, entry) {
1507 if (entry->class == hlock_class(next)) {
1509 entry->distance = 1;
1515 * Ok, all validations passed, add the new lock
1516 * to the previous lock's dependency list:
1518 ret = add_lock_to_list(hlock_class(prev), hlock_class(next),
1519 &hlock_class(prev)->locks_after,
1520 next->acquire_ip, distance);
1525 ret = add_lock_to_list(hlock_class(next), hlock_class(prev),
1526 &hlock_class(next)->locks_before,
1527 next->acquire_ip, distance);
1532 * Debugging printouts:
1534 if (verbose(hlock_class(prev)) || verbose(hlock_class(next))) {
1536 printk("\n new dependency: ");
1537 print_lock_name(hlock_class(prev));
1539 print_lock_name(hlock_class(next));
1542 return graph_lock();
1548 * Add the dependency to all directly-previous locks that are 'relevant'.
1549 * The ones that are relevant are (in increasing distance from curr):
1550 * all consecutive trylock entries and the final non-trylock entry - or
1551 * the end of this context's lock-chain - whichever comes first.
1554 check_prevs_add(struct task_struct *curr, struct held_lock *next)
1556 int depth = curr->lockdep_depth;
1557 struct held_lock *hlock;
1562 * Depth must not be zero for a non-head lock:
1567 * At least two relevant locks must exist for this
1570 if (curr->held_locks[depth].irq_context !=
1571 curr->held_locks[depth-1].irq_context)
1575 int distance = curr->lockdep_depth - depth + 1;
1576 hlock = curr->held_locks + depth-1;
1578 * Only non-recursive-read entries get new dependencies
1581 if (hlock->read != 2) {
1582 if (!check_prev_add(curr, hlock, next, distance))
1585 * Stop after the first non-trylock entry,
1586 * as non-trylock entries have added their
1587 * own direct dependencies already, so this
1588 * lock is connected to them indirectly:
1590 if (!hlock->trylock)
1595 * End of lock-stack?
1600 * Stop the search if we cross into another context:
1602 if (curr->held_locks[depth].irq_context !=
1603 curr->held_locks[depth-1].irq_context)
1608 if (!debug_locks_off_graph_unlock())
1616 unsigned long nr_lock_chains;
1617 struct lock_chain lock_chains[MAX_LOCKDEP_CHAINS];
1618 int nr_chain_hlocks;
1619 static u16 chain_hlocks[MAX_LOCKDEP_CHAIN_HLOCKS];
1621 struct lock_class *lock_chain_get_class(struct lock_chain *chain, int i)
1623 return lock_classes + chain_hlocks[chain->base + i];
1627 * Look up a dependency chain. If the key is not present yet then
1628 * add it and return 1 - in this case the new dependency chain is
1629 * validated. If the key is already hashed, return 0.
1630 * (On return with 1 graph_lock is held.)
1632 static inline int lookup_chain_cache(struct task_struct *curr,
1633 struct held_lock *hlock,
1636 struct lock_class *class = hlock_class(hlock);
1637 struct list_head *hash_head = chainhashentry(chain_key);
1638 struct lock_chain *chain;
1639 struct held_lock *hlock_curr, *hlock_next;
1642 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
1645 * We can walk it lock-free, because entries only get added
1648 list_for_each_entry(chain, hash_head, entry) {
1649 if (chain->chain_key == chain_key) {
1651 debug_atomic_inc(&chain_lookup_hits);
1652 if (very_verbose(class))
1653 printk("\nhash chain already cached, key: "
1654 "%016Lx tail class: [%p] %s\n",
1655 (unsigned long long)chain_key,
1656 class->key, class->name);
1660 if (very_verbose(class))
1661 printk("\nnew hash chain, key: %016Lx tail class: [%p] %s\n",
1662 (unsigned long long)chain_key, class->key, class->name);
1664 * Allocate a new chain entry from the static array, and add
1670 * We have to walk the chain again locked - to avoid duplicates:
1672 list_for_each_entry(chain, hash_head, entry) {
1673 if (chain->chain_key == chain_key) {
1678 if (unlikely(nr_lock_chains >= MAX_LOCKDEP_CHAINS)) {
1679 if (!debug_locks_off_graph_unlock())
1682 printk("BUG: MAX_LOCKDEP_CHAINS too low!\n");
1683 printk("turning off the locking correctness validator.\n");
1686 chain = lock_chains + nr_lock_chains++;
1687 chain->chain_key = chain_key;
1688 chain->irq_context = hlock->irq_context;
1689 /* Find the first held_lock of current chain */
1691 for (i = curr->lockdep_depth - 1; i >= 0; i--) {
1692 hlock_curr = curr->held_locks + i;
1693 if (hlock_curr->irq_context != hlock_next->irq_context)
1698 chain->depth = curr->lockdep_depth + 1 - i;
1699 cn = nr_chain_hlocks;
1700 while (cn + chain->depth <= MAX_LOCKDEP_CHAIN_HLOCKS) {
1701 n = cmpxchg(&nr_chain_hlocks, cn, cn + chain->depth);
1706 if (likely(cn + chain->depth <= MAX_LOCKDEP_CHAIN_HLOCKS)) {
1708 for (j = 0; j < chain->depth - 1; j++, i++) {
1709 int lock_id = curr->held_locks[i].class_idx - 1;
1710 chain_hlocks[chain->base + j] = lock_id;
1712 chain_hlocks[chain->base + j] = class - lock_classes;
1714 list_add_tail_rcu(&chain->entry, hash_head);
1715 debug_atomic_inc(&chain_lookup_misses);
1721 static int validate_chain(struct task_struct *curr, struct lockdep_map *lock,
1722 struct held_lock *hlock, int chain_head, u64 chain_key)
1725 * Trylock needs to maintain the stack of held locks, but it
1726 * does not add new dependencies, because trylock can be done
1729 * We look up the chain_key and do the O(N^2) check and update of
1730 * the dependencies only if this is a new dependency chain.
1731 * (If lookup_chain_cache() returns with 1 it acquires
1732 * graph_lock for us)
1734 if (!hlock->trylock && (hlock->check == 2) &&
1735 lookup_chain_cache(curr, hlock, chain_key)) {
1737 * Check whether last held lock:
1739 * - is irq-safe, if this lock is irq-unsafe
1740 * - is softirq-safe, if this lock is hardirq-unsafe
1742 * And check whether the new lock's dependency graph
1743 * could lead back to the previous lock.
1745 * any of these scenarios could lead to a deadlock. If
1748 int ret = check_deadlock(curr, hlock, lock, hlock->read);
1753 * Mark recursive read, as we jump over it when
1754 * building dependencies (just like we jump over
1760 * Add dependency only if this lock is not the head
1761 * of the chain, and if it's not a secondary read-lock:
1763 if (!chain_head && ret != 2)
1764 if (!check_prevs_add(curr, hlock))
1768 /* after lookup_chain_cache(): */
1769 if (unlikely(!debug_locks))
1775 static inline int validate_chain(struct task_struct *curr,
1776 struct lockdep_map *lock, struct held_lock *hlock,
1777 int chain_head, u64 chain_key)
1784 * We are building curr_chain_key incrementally, so double-check
1785 * it from scratch, to make sure that it's done correctly:
1787 static void check_chain_key(struct task_struct *curr)
1789 #ifdef CONFIG_DEBUG_LOCKDEP
1790 struct held_lock *hlock, *prev_hlock = NULL;
1794 for (i = 0; i < curr->lockdep_depth; i++) {
1795 hlock = curr->held_locks + i;
1796 if (chain_key != hlock->prev_chain_key) {
1798 WARN(1, "hm#1, depth: %u [%u], %016Lx != %016Lx\n",
1799 curr->lockdep_depth, i,
1800 (unsigned long long)chain_key,
1801 (unsigned long long)hlock->prev_chain_key);
1804 id = hlock->class_idx - 1;
1805 if (DEBUG_LOCKS_WARN_ON(id >= MAX_LOCKDEP_KEYS))
1808 if (prev_hlock && (prev_hlock->irq_context !=
1809 hlock->irq_context))
1811 chain_key = iterate_chain_key(chain_key, id);
1814 if (chain_key != curr->curr_chain_key) {
1816 WARN(1, "hm#2, depth: %u [%u], %016Lx != %016Lx\n",
1817 curr->lockdep_depth, i,
1818 (unsigned long long)chain_key,
1819 (unsigned long long)curr->curr_chain_key);
1825 print_usage_bug(struct task_struct *curr, struct held_lock *this,
1826 enum lock_usage_bit prev_bit, enum lock_usage_bit new_bit)
1828 if (!debug_locks_off_graph_unlock() || debug_locks_silent)
1831 printk("\n=================================\n");
1832 printk( "[ INFO: inconsistent lock state ]\n");
1833 print_kernel_version();
1834 printk( "---------------------------------\n");
1836 printk("inconsistent {%s} -> {%s} usage.\n",
1837 usage_str[prev_bit], usage_str[new_bit]);
1839 printk("%s/%d [HC%u[%lu]:SC%u[%lu]:HE%u:SE%u] takes:\n",
1840 curr->comm, task_pid_nr(curr),
1841 trace_hardirq_context(curr), hardirq_count() >> HARDIRQ_SHIFT,
1842 trace_softirq_context(curr), softirq_count() >> SOFTIRQ_SHIFT,
1843 trace_hardirqs_enabled(curr),
1844 trace_softirqs_enabled(curr));
1847 printk("{%s} state was registered at:\n", usage_str[prev_bit]);
1848 print_stack_trace(hlock_class(this)->usage_traces + prev_bit, 1);
1850 print_irqtrace_events(curr);
1851 printk("\nother info that might help us debug this:\n");
1852 lockdep_print_held_locks(curr);
1854 printk("\nstack backtrace:\n");
1861 * Print out an error if an invalid bit is set:
1864 valid_state(struct task_struct *curr, struct held_lock *this,
1865 enum lock_usage_bit new_bit, enum lock_usage_bit bad_bit)
1867 if (unlikely(hlock_class(this)->usage_mask & (1 << bad_bit)))
1868 return print_usage_bug(curr, this, bad_bit, new_bit);
1872 static int mark_lock(struct task_struct *curr, struct held_lock *this,
1873 enum lock_usage_bit new_bit);
1875 #if defined(CONFIG_TRACE_IRQFLAGS) && defined(CONFIG_PROVE_LOCKING)
1878 * print irq inversion bug:
1881 print_irq_inversion_bug(struct task_struct *curr, struct lock_class *other,
1882 struct held_lock *this, int forwards,
1883 const char *irqclass)
1885 if (!debug_locks_off_graph_unlock() || debug_locks_silent)
1888 printk("\n=========================================================\n");
1889 printk( "[ INFO: possible irq lock inversion dependency detected ]\n");
1890 print_kernel_version();
1891 printk( "---------------------------------------------------------\n");
1892 printk("%s/%d just changed the state of lock:\n",
1893 curr->comm, task_pid_nr(curr));
1896 printk("but this lock took another, %s-unsafe lock in the past:\n", irqclass);
1898 printk("but this lock was taken by another, %s-safe lock in the past:\n", irqclass);
1899 print_lock_name(other);
1900 printk("\n\nand interrupts could create inverse lock ordering between them.\n\n");
1902 printk("\nother info that might help us debug this:\n");
1903 lockdep_print_held_locks(curr);
1905 printk("\nthe first lock's dependencies:\n");
1906 print_lock_dependencies(hlock_class(this), 0);
1908 printk("\nthe second lock's dependencies:\n");
1909 print_lock_dependencies(other, 0);
1911 printk("\nstack backtrace:\n");
1918 * Prove that in the forwards-direction subgraph starting at <this>
1919 * there is no lock matching <mask>:
1922 check_usage_forwards(struct task_struct *curr, struct held_lock *this,
1923 enum lock_usage_bit bit, const char *irqclass)
1927 find_usage_bit = bit;
1928 /* fills in <forwards_match> */
1929 ret = find_usage_forwards(hlock_class(this), 0);
1930 if (!ret || ret == 1)
1933 return print_irq_inversion_bug(curr, forwards_match, this, 1, irqclass);
1937 * Prove that in the backwards-direction subgraph starting at <this>
1938 * there is no lock matching <mask>:
1941 check_usage_backwards(struct task_struct *curr, struct held_lock *this,
1942 enum lock_usage_bit bit, const char *irqclass)
1946 find_usage_bit = bit;
1947 /* fills in <backwards_match> */
1948 ret = find_usage_backwards(hlock_class(this), 0);
1949 if (!ret || ret == 1)
1952 return print_irq_inversion_bug(curr, backwards_match, this, 0, irqclass);
1955 void print_irqtrace_events(struct task_struct *curr)
1957 printk("irq event stamp: %u\n", curr->irq_events);
1958 printk("hardirqs last enabled at (%u): ", curr->hardirq_enable_event);
1959 print_ip_sym(curr->hardirq_enable_ip);
1960 printk("hardirqs last disabled at (%u): ", curr->hardirq_disable_event);
1961 print_ip_sym(curr->hardirq_disable_ip);
1962 printk("softirqs last enabled at (%u): ", curr->softirq_enable_event);
1963 print_ip_sym(curr->softirq_enable_ip);
1964 printk("softirqs last disabled at (%u): ", curr->softirq_disable_event);
1965 print_ip_sym(curr->softirq_disable_ip);
1968 static int HARDIRQ_verbose(struct lock_class *class)
1971 return class_filter(class);
1976 static int SOFTIRQ_verbose(struct lock_class *class)
1979 return class_filter(class);
1984 static int RECLAIM_FS_verbose(struct lock_class *class)
1987 return class_filter(class);
1992 #define STRICT_READ_CHECKS 1
1994 static int (*state_verbose_f[])(struct lock_class *class) = {
1995 #define LOCKDEP_STATE(__STATE) \
1997 #include "lockdep_states.h"
1998 #undef LOCKDEP_STATE
2001 static inline int state_verbose(enum lock_usage_bit bit,
2002 struct lock_class *class)
2004 return state_verbose_f[bit >> 2](class);
2007 typedef int (*check_usage_f)(struct task_struct *, struct held_lock *,
2008 enum lock_usage_bit bit, const char *name);
2011 mark_lock_irq(struct task_struct *curr, struct held_lock *this,
2012 enum lock_usage_bit new_bit)
2014 int excl_bit = exclusive_bit(new_bit);
2015 int read = new_bit & 1;
2016 int dir = new_bit & 2;
2019 * mark USED_IN has to look forwards -- to ensure no dependency
2020 * has ENABLED state, which would allow recursion deadlocks.
2022 * mark ENABLED has to look backwards -- to ensure no dependee
2023 * has USED_IN state, which, again, would allow recursion deadlocks.
2025 check_usage_f usage = dir ?
2026 check_usage_backwards : check_usage_forwards;
2029 * Validate that this particular lock does not have conflicting
2032 if (!valid_state(curr, this, new_bit, excl_bit))
2036 * Validate that the lock dependencies don't have conflicting usage
2039 if ((!read || !dir || STRICT_READ_CHECKS) &&
2040 !usage(curr, this, excl_bit, state_name(new_bit & ~1)))
2044 * Check for read in write conflicts
2047 if (!valid_state(curr, this, new_bit, excl_bit + 1))
2050 if (STRICT_READ_CHECKS &&
2051 !usage(curr, this, excl_bit + 1,
2052 state_name(new_bit + 1)))
2056 if (state_verbose(new_bit, hlock_class(this)))
2063 #define LOCKDEP_STATE(__STATE) __STATE,
2064 #include "lockdep_states.h"
2065 #undef LOCKDEP_STATE
2069 * Mark all held locks with a usage bit:
2072 mark_held_locks(struct task_struct *curr, enum mark_type mark)
2074 enum lock_usage_bit usage_bit;
2075 struct held_lock *hlock;
2078 for (i = 0; i < curr->lockdep_depth; i++) {
2079 hlock = curr->held_locks + i;
2081 usage_bit = 2 + (mark << 2); /* ENABLED */
2083 usage_bit += 1; /* READ */
2085 BUG_ON(usage_bit >= LOCK_USAGE_STATES);
2087 if (!mark_lock(curr, hlock, usage_bit))
2095 * Debugging helper: via this flag we know that we are in
2096 * 'early bootup code', and will warn about any invalid irqs-on event:
2098 static int early_boot_irqs_enabled;
2100 void early_boot_irqs_off(void)
2102 early_boot_irqs_enabled = 0;
2105 void early_boot_irqs_on(void)
2107 early_boot_irqs_enabled = 1;
2111 * Hardirqs will be enabled:
2113 void trace_hardirqs_on_caller(unsigned long ip)
2115 struct task_struct *curr = current;
2117 time_hardirqs_on(CALLER_ADDR0, ip);
2119 if (unlikely(!debug_locks || current->lockdep_recursion))
2122 if (DEBUG_LOCKS_WARN_ON(unlikely(!early_boot_irqs_enabled)))
2125 if (unlikely(curr->hardirqs_enabled)) {
2126 debug_atomic_inc(&redundant_hardirqs_on);
2129 /* we'll do an OFF -> ON transition: */
2130 curr->hardirqs_enabled = 1;
2132 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2134 if (DEBUG_LOCKS_WARN_ON(current->hardirq_context))
2137 * We are going to turn hardirqs on, so set the
2138 * usage bit for all held locks:
2140 if (!mark_held_locks(curr, HARDIRQ))
2143 * If we have softirqs enabled, then set the usage
2144 * bit for all held locks. (disabled hardirqs prevented
2145 * this bit from being set before)
2147 if (curr->softirqs_enabled)
2148 if (!mark_held_locks(curr, SOFTIRQ))
2151 curr->hardirq_enable_ip = ip;
2152 curr->hardirq_enable_event = ++curr->irq_events;
2153 debug_atomic_inc(&hardirqs_on_events);
2155 EXPORT_SYMBOL(trace_hardirqs_on_caller);
2157 void trace_hardirqs_on(void)
2159 trace_hardirqs_on_caller(CALLER_ADDR0);
2161 EXPORT_SYMBOL(trace_hardirqs_on);
2164 * Hardirqs were disabled:
2166 void trace_hardirqs_off_caller(unsigned long ip)
2168 struct task_struct *curr = current;
2170 time_hardirqs_off(CALLER_ADDR0, ip);
2172 if (unlikely(!debug_locks || current->lockdep_recursion))
2175 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2178 if (curr->hardirqs_enabled) {
2180 * We have done an ON -> OFF transition:
2182 curr->hardirqs_enabled = 0;
2183 curr->hardirq_disable_ip = ip;
2184 curr->hardirq_disable_event = ++curr->irq_events;
2185 debug_atomic_inc(&hardirqs_off_events);
2187 debug_atomic_inc(&redundant_hardirqs_off);
2189 EXPORT_SYMBOL(trace_hardirqs_off_caller);
2191 void trace_hardirqs_off(void)
2193 trace_hardirqs_off_caller(CALLER_ADDR0);
2195 EXPORT_SYMBOL(trace_hardirqs_off);
2198 * Softirqs will be enabled:
2200 void trace_softirqs_on(unsigned long ip)
2202 struct task_struct *curr = current;
2204 if (unlikely(!debug_locks))
2207 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2210 if (curr->softirqs_enabled) {
2211 debug_atomic_inc(&redundant_softirqs_on);
2216 * We'll do an OFF -> ON transition:
2218 curr->softirqs_enabled = 1;
2219 curr->softirq_enable_ip = ip;
2220 curr->softirq_enable_event = ++curr->irq_events;
2221 debug_atomic_inc(&softirqs_on_events);
2223 * We are going to turn softirqs on, so set the
2224 * usage bit for all held locks, if hardirqs are
2227 if (curr->hardirqs_enabled)
2228 mark_held_locks(curr, SOFTIRQ);
2232 * Softirqs were disabled:
2234 void trace_softirqs_off(unsigned long ip)
2236 struct task_struct *curr = current;
2238 if (unlikely(!debug_locks))
2241 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2244 if (curr->softirqs_enabled) {
2246 * We have done an ON -> OFF transition:
2248 curr->softirqs_enabled = 0;
2249 curr->softirq_disable_ip = ip;
2250 curr->softirq_disable_event = ++curr->irq_events;
2251 debug_atomic_inc(&softirqs_off_events);
2252 DEBUG_LOCKS_WARN_ON(!softirq_count());
2254 debug_atomic_inc(&redundant_softirqs_off);
2257 void lockdep_trace_alloc(gfp_t gfp_mask)
2259 struct task_struct *curr = current;
2261 if (unlikely(!debug_locks))
2264 /* no reclaim without waiting on it */
2265 if (!(gfp_mask & __GFP_WAIT))
2268 /* this guy won't enter reclaim */
2269 if ((curr->flags & PF_MEMALLOC) && !(gfp_mask & __GFP_NOMEMALLOC))
2272 /* We're only interested __GFP_FS allocations for now */
2273 if (!(gfp_mask & __GFP_FS))
2276 if (DEBUG_LOCKS_WARN_ON(irqs_disabled()))
2279 mark_held_locks(curr, RECLAIM_FS);
2282 static int mark_irqflags(struct task_struct *curr, struct held_lock *hlock)
2285 * If non-trylock use in a hardirq or softirq context, then
2286 * mark the lock as used in these contexts:
2288 if (!hlock->trylock) {
2290 if (curr->hardirq_context)
2291 if (!mark_lock(curr, hlock,
2292 LOCK_USED_IN_HARDIRQ_READ))
2294 if (curr->softirq_context)
2295 if (!mark_lock(curr, hlock,
2296 LOCK_USED_IN_SOFTIRQ_READ))
2299 if (curr->hardirq_context)
2300 if (!mark_lock(curr, hlock, LOCK_USED_IN_HARDIRQ))
2302 if (curr->softirq_context)
2303 if (!mark_lock(curr, hlock, LOCK_USED_IN_SOFTIRQ))
2307 if (!hlock->hardirqs_off) {
2309 if (!mark_lock(curr, hlock,
2310 LOCK_ENABLED_HARDIRQ_READ))
2312 if (curr->softirqs_enabled)
2313 if (!mark_lock(curr, hlock,
2314 LOCK_ENABLED_SOFTIRQ_READ))
2317 if (!mark_lock(curr, hlock,
2318 LOCK_ENABLED_HARDIRQ))
2320 if (curr->softirqs_enabled)
2321 if (!mark_lock(curr, hlock,
2322 LOCK_ENABLED_SOFTIRQ))
2328 * We reuse the irq context infrastructure more broadly as a general
2329 * context checking code. This tests GFP_FS recursion (a lock taken
2330 * during reclaim for a GFP_FS allocation is held over a GFP_FS
2333 if (!hlock->trylock && (curr->lockdep_reclaim_gfp & __GFP_FS)) {
2335 if (!mark_lock(curr, hlock, LOCK_USED_IN_RECLAIM_FS_READ))
2338 if (!mark_lock(curr, hlock, LOCK_USED_IN_RECLAIM_FS))
2346 static int separate_irq_context(struct task_struct *curr,
2347 struct held_lock *hlock)
2349 unsigned int depth = curr->lockdep_depth;
2352 * Keep track of points where we cross into an interrupt context:
2354 hlock->irq_context = 2*(curr->hardirq_context ? 1 : 0) +
2355 curr->softirq_context;
2357 struct held_lock *prev_hlock;
2359 prev_hlock = curr->held_locks + depth-1;
2361 * If we cross into another context, reset the
2362 * hash key (this also prevents the checking and the
2363 * adding of the dependency to 'prev'):
2365 if (prev_hlock->irq_context != hlock->irq_context)
2374 int mark_lock_irq(struct task_struct *curr, struct held_lock *this,
2375 enum lock_usage_bit new_bit)
2381 static inline int mark_irqflags(struct task_struct *curr,
2382 struct held_lock *hlock)
2387 static inline int separate_irq_context(struct task_struct *curr,
2388 struct held_lock *hlock)
2393 void lockdep_trace_alloc(gfp_t gfp_mask)
2400 * Mark a lock with a usage bit, and validate the state transition:
2402 static int mark_lock(struct task_struct *curr, struct held_lock *this,
2403 enum lock_usage_bit new_bit)
2405 unsigned int new_mask = 1 << new_bit, ret = 1;
2408 * If already set then do not dirty the cacheline,
2409 * nor do any checks:
2411 if (likely(hlock_class(this)->usage_mask & new_mask))
2417 * Make sure we didnt race:
2419 if (unlikely(hlock_class(this)->usage_mask & new_mask)) {
2424 hlock_class(this)->usage_mask |= new_mask;
2426 if (!save_trace(hlock_class(this)->usage_traces + new_bit))
2430 #define LOCKDEP_STATE(__STATE) \
2431 case LOCK_USED_IN_##__STATE: \
2432 case LOCK_USED_IN_##__STATE##_READ: \
2433 case LOCK_ENABLED_##__STATE: \
2434 case LOCK_ENABLED_##__STATE##_READ:
2435 #include "lockdep_states.h"
2436 #undef LOCKDEP_STATE
2437 ret = mark_lock_irq(curr, this, new_bit);
2442 debug_atomic_dec(&nr_unused_locks);
2445 if (!debug_locks_off_graph_unlock())
2454 * We must printk outside of the graph_lock:
2457 printk("\nmarked lock as {%s}:\n", usage_str[new_bit]);
2459 print_irqtrace_events(curr);
2467 * Initialize a lock instance's lock-class mapping info:
2469 void lockdep_init_map(struct lockdep_map *lock, const char *name,
2470 struct lock_class_key *key, int subclass)
2472 if (unlikely(!debug_locks))
2475 if (DEBUG_LOCKS_WARN_ON(!key))
2477 if (DEBUG_LOCKS_WARN_ON(!name))
2480 * Sanity check, the lock-class key must be persistent:
2482 if (!static_obj(key)) {
2483 printk("BUG: key %p not in .data!\n", key);
2484 DEBUG_LOCKS_WARN_ON(1);
2489 lock->class_cache = NULL;
2490 #ifdef CONFIG_LOCK_STAT
2491 lock->cpu = raw_smp_processor_id();
2494 register_lock_class(lock, subclass, 1);
2496 EXPORT_SYMBOL_GPL(lockdep_init_map);
2499 * This gets called for every mutex_lock*()/spin_lock*() operation.
2500 * We maintain the dependency maps and validate the locking attempt:
2502 static int __lock_acquire(struct lockdep_map *lock, unsigned int subclass,
2503 int trylock, int read, int check, int hardirqs_off,
2504 struct lockdep_map *nest_lock, unsigned long ip)
2506 struct task_struct *curr = current;
2507 struct lock_class *class = NULL;
2508 struct held_lock *hlock;
2509 unsigned int depth, id;
2516 if (unlikely(!debug_locks))
2519 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2522 if (unlikely(subclass >= MAX_LOCKDEP_SUBCLASSES)) {
2524 printk("BUG: MAX_LOCKDEP_SUBCLASSES too low!\n");
2525 printk("turning off the locking correctness validator.\n");
2530 class = lock->class_cache;
2532 * Not cached yet or subclass?
2534 if (unlikely(!class)) {
2535 class = register_lock_class(lock, subclass, 0);
2539 debug_atomic_inc((atomic_t *)&class->ops);
2540 if (very_verbose(class)) {
2541 printk("\nacquire class [%p] %s", class->key, class->name);
2542 if (class->name_version > 1)
2543 printk("#%d", class->name_version);
2549 * Add the lock to the list of currently held locks.
2550 * (we dont increase the depth just yet, up until the
2551 * dependency checks are done)
2553 depth = curr->lockdep_depth;
2554 if (DEBUG_LOCKS_WARN_ON(depth >= MAX_LOCK_DEPTH))
2557 hlock = curr->held_locks + depth;
2558 if (DEBUG_LOCKS_WARN_ON(!class))
2560 hlock->class_idx = class - lock_classes + 1;
2561 hlock->acquire_ip = ip;
2562 hlock->instance = lock;
2563 hlock->nest_lock = nest_lock;
2564 hlock->trylock = trylock;
2566 hlock->check = check;
2567 hlock->hardirqs_off = !!hardirqs_off;
2568 #ifdef CONFIG_LOCK_STAT
2569 hlock->waittime_stamp = 0;
2570 hlock->holdtime_stamp = sched_clock();
2573 if (check == 2 && !mark_irqflags(curr, hlock))
2576 /* mark it as used: */
2577 if (!mark_lock(curr, hlock, LOCK_USED))
2581 * Calculate the chain hash: it's the combined hash of all the
2582 * lock keys along the dependency chain. We save the hash value
2583 * at every step so that we can get the current hash easily
2584 * after unlock. The chain hash is then used to cache dependency
2587 * The 'key ID' is what is the most compact key value to drive
2588 * the hash, not class->key.
2590 id = class - lock_classes;
2591 if (DEBUG_LOCKS_WARN_ON(id >= MAX_LOCKDEP_KEYS))
2594 chain_key = curr->curr_chain_key;
2596 if (DEBUG_LOCKS_WARN_ON(chain_key != 0))
2601 hlock->prev_chain_key = chain_key;
2602 if (separate_irq_context(curr, hlock)) {
2606 chain_key = iterate_chain_key(chain_key, id);
2608 if (!validate_chain(curr, lock, hlock, chain_head, chain_key))
2611 curr->curr_chain_key = chain_key;
2612 curr->lockdep_depth++;
2613 check_chain_key(curr);
2614 #ifdef CONFIG_DEBUG_LOCKDEP
2615 if (unlikely(!debug_locks))
2618 if (unlikely(curr->lockdep_depth >= MAX_LOCK_DEPTH)) {
2620 printk("BUG: MAX_LOCK_DEPTH too low!\n");
2621 printk("turning off the locking correctness validator.\n");
2625 if (unlikely(curr->lockdep_depth > max_lockdep_depth))
2626 max_lockdep_depth = curr->lockdep_depth;
2632 print_unlock_inbalance_bug(struct task_struct *curr, struct lockdep_map *lock,
2635 if (!debug_locks_off())
2637 if (debug_locks_silent)
2640 printk("\n=====================================\n");
2641 printk( "[ BUG: bad unlock balance detected! ]\n");
2642 printk( "-------------------------------------\n");
2643 printk("%s/%d is trying to release lock (",
2644 curr->comm, task_pid_nr(curr));
2645 print_lockdep_cache(lock);
2648 printk("but there are no more locks to release!\n");
2649 printk("\nother info that might help us debug this:\n");
2650 lockdep_print_held_locks(curr);
2652 printk("\nstack backtrace:\n");
2659 * Common debugging checks for both nested and non-nested unlock:
2661 static int check_unlock(struct task_struct *curr, struct lockdep_map *lock,
2664 if (unlikely(!debug_locks))
2666 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2669 if (curr->lockdep_depth <= 0)
2670 return print_unlock_inbalance_bug(curr, lock, ip);
2676 __lock_set_class(struct lockdep_map *lock, const char *name,
2677 struct lock_class_key *key, unsigned int subclass,
2680 struct task_struct *curr = current;
2681 struct held_lock *hlock, *prev_hlock;
2682 struct lock_class *class;
2686 depth = curr->lockdep_depth;
2687 if (DEBUG_LOCKS_WARN_ON(!depth))
2691 for (i = depth-1; i >= 0; i--) {
2692 hlock = curr->held_locks + i;
2694 * We must not cross into another context:
2696 if (prev_hlock && prev_hlock->irq_context != hlock->irq_context)
2698 if (hlock->instance == lock)
2702 return print_unlock_inbalance_bug(curr, lock, ip);
2705 lockdep_init_map(lock, name, key, 0);
2706 class = register_lock_class(lock, subclass, 0);
2707 hlock->class_idx = class - lock_classes + 1;
2709 curr->lockdep_depth = i;
2710 curr->curr_chain_key = hlock->prev_chain_key;
2712 for (; i < depth; i++) {
2713 hlock = curr->held_locks + i;
2714 if (!__lock_acquire(hlock->instance,
2715 hlock_class(hlock)->subclass, hlock->trylock,
2716 hlock->read, hlock->check, hlock->hardirqs_off,
2717 hlock->nest_lock, hlock->acquire_ip))
2721 if (DEBUG_LOCKS_WARN_ON(curr->lockdep_depth != depth))
2727 * Remove the lock to the list of currently held locks in a
2728 * potentially non-nested (out of order) manner. This is a
2729 * relatively rare operation, as all the unlock APIs default
2730 * to nested mode (which uses lock_release()):
2733 lock_release_non_nested(struct task_struct *curr,
2734 struct lockdep_map *lock, unsigned long ip)
2736 struct held_lock *hlock, *prev_hlock;
2741 * Check whether the lock exists in the current stack
2744 depth = curr->lockdep_depth;
2745 if (DEBUG_LOCKS_WARN_ON(!depth))
2749 for (i = depth-1; i >= 0; i--) {
2750 hlock = curr->held_locks + i;
2752 * We must not cross into another context:
2754 if (prev_hlock && prev_hlock->irq_context != hlock->irq_context)
2756 if (hlock->instance == lock)
2760 return print_unlock_inbalance_bug(curr, lock, ip);
2763 lock_release_holdtime(hlock);
2766 * We have the right lock to unlock, 'hlock' points to it.
2767 * Now we remove it from the stack, and add back the other
2768 * entries (if any), recalculating the hash along the way:
2770 curr->lockdep_depth = i;
2771 curr->curr_chain_key = hlock->prev_chain_key;
2773 for (i++; i < depth; i++) {
2774 hlock = curr->held_locks + i;
2775 if (!__lock_acquire(hlock->instance,
2776 hlock_class(hlock)->subclass, hlock->trylock,
2777 hlock->read, hlock->check, hlock->hardirqs_off,
2778 hlock->nest_lock, hlock->acquire_ip))
2782 if (DEBUG_LOCKS_WARN_ON(curr->lockdep_depth != depth - 1))
2788 * Remove the lock to the list of currently held locks - this gets
2789 * called on mutex_unlock()/spin_unlock*() (or on a failed
2790 * mutex_lock_interruptible()). This is done for unlocks that nest
2791 * perfectly. (i.e. the current top of the lock-stack is unlocked)
2793 static int lock_release_nested(struct task_struct *curr,
2794 struct lockdep_map *lock, unsigned long ip)
2796 struct held_lock *hlock;
2800 * Pop off the top of the lock stack:
2802 depth = curr->lockdep_depth - 1;
2803 hlock = curr->held_locks + depth;
2806 * Is the unlock non-nested:
2808 if (hlock->instance != lock)
2809 return lock_release_non_nested(curr, lock, ip);
2810 curr->lockdep_depth--;
2812 if (DEBUG_LOCKS_WARN_ON(!depth && (hlock->prev_chain_key != 0)))
2815 curr->curr_chain_key = hlock->prev_chain_key;
2817 lock_release_holdtime(hlock);
2819 #ifdef CONFIG_DEBUG_LOCKDEP
2820 hlock->prev_chain_key = 0;
2821 hlock->class_idx = 0;
2822 hlock->acquire_ip = 0;
2823 hlock->irq_context = 0;
2829 * Remove the lock to the list of currently held locks - this gets
2830 * called on mutex_unlock()/spin_unlock*() (or on a failed
2831 * mutex_lock_interruptible()). This is done for unlocks that nest
2832 * perfectly. (i.e. the current top of the lock-stack is unlocked)
2835 __lock_release(struct lockdep_map *lock, int nested, unsigned long ip)
2837 struct task_struct *curr = current;
2839 if (!check_unlock(curr, lock, ip))
2843 if (!lock_release_nested(curr, lock, ip))
2846 if (!lock_release_non_nested(curr, lock, ip))
2850 check_chain_key(curr);
2854 * Check whether we follow the irq-flags state precisely:
2856 static void check_flags(unsigned long flags)
2858 #if defined(CONFIG_PROVE_LOCKING) && defined(CONFIG_DEBUG_LOCKDEP) && \
2859 defined(CONFIG_TRACE_IRQFLAGS)
2863 if (irqs_disabled_flags(flags)) {
2864 if (DEBUG_LOCKS_WARN_ON(current->hardirqs_enabled)) {
2865 printk("possible reason: unannotated irqs-off.\n");
2868 if (DEBUG_LOCKS_WARN_ON(!current->hardirqs_enabled)) {
2869 printk("possible reason: unannotated irqs-on.\n");
2874 * We dont accurately track softirq state in e.g.
2875 * hardirq contexts (such as on 4KSTACKS), so only
2876 * check if not in hardirq contexts:
2878 if (!hardirq_count()) {
2879 if (softirq_count())
2880 DEBUG_LOCKS_WARN_ON(current->softirqs_enabled);
2882 DEBUG_LOCKS_WARN_ON(!current->softirqs_enabled);
2886 print_irqtrace_events(current);
2890 void lock_set_class(struct lockdep_map *lock, const char *name,
2891 struct lock_class_key *key, unsigned int subclass,
2894 unsigned long flags;
2896 if (unlikely(current->lockdep_recursion))
2899 raw_local_irq_save(flags);
2900 current->lockdep_recursion = 1;
2902 if (__lock_set_class(lock, name, key, subclass, ip))
2903 check_chain_key(current);
2904 current->lockdep_recursion = 0;
2905 raw_local_irq_restore(flags);
2907 EXPORT_SYMBOL_GPL(lock_set_class);
2910 * We are not always called with irqs disabled - do that here,
2911 * and also avoid lockdep recursion:
2913 void lock_acquire(struct lockdep_map *lock, unsigned int subclass,
2914 int trylock, int read, int check,
2915 struct lockdep_map *nest_lock, unsigned long ip)
2917 unsigned long flags;
2919 if (unlikely(current->lockdep_recursion))
2922 raw_local_irq_save(flags);
2925 current->lockdep_recursion = 1;
2926 __lock_acquire(lock, subclass, trylock, read, check,
2927 irqs_disabled_flags(flags), nest_lock, ip);
2928 current->lockdep_recursion = 0;
2929 raw_local_irq_restore(flags);
2931 EXPORT_SYMBOL_GPL(lock_acquire);
2933 void lock_release(struct lockdep_map *lock, int nested,
2936 unsigned long flags;
2938 if (unlikely(current->lockdep_recursion))
2941 raw_local_irq_save(flags);
2943 current->lockdep_recursion = 1;
2944 __lock_release(lock, nested, ip);
2945 current->lockdep_recursion = 0;
2946 raw_local_irq_restore(flags);
2948 EXPORT_SYMBOL_GPL(lock_release);
2950 void lockdep_set_current_reclaim_state(gfp_t gfp_mask)
2952 current->lockdep_reclaim_gfp = gfp_mask;
2955 void lockdep_clear_current_reclaim_state(void)
2957 current->lockdep_reclaim_gfp = 0;
2960 #ifdef CONFIG_LOCK_STAT
2962 print_lock_contention_bug(struct task_struct *curr, struct lockdep_map *lock,
2965 if (!debug_locks_off())
2967 if (debug_locks_silent)
2970 printk("\n=================================\n");
2971 printk( "[ BUG: bad contention detected! ]\n");
2972 printk( "---------------------------------\n");
2973 printk("%s/%d is trying to contend lock (",
2974 curr->comm, task_pid_nr(curr));
2975 print_lockdep_cache(lock);
2978 printk("but there are no locks held!\n");
2979 printk("\nother info that might help us debug this:\n");
2980 lockdep_print_held_locks(curr);
2982 printk("\nstack backtrace:\n");
2989 __lock_contended(struct lockdep_map *lock, unsigned long ip)
2991 struct task_struct *curr = current;
2992 struct held_lock *hlock, *prev_hlock;
2993 struct lock_class_stats *stats;
2995 int i, contention_point, contending_point;
2997 depth = curr->lockdep_depth;
2998 if (DEBUG_LOCKS_WARN_ON(!depth))
3002 for (i = depth-1; i >= 0; i--) {
3003 hlock = curr->held_locks + i;
3005 * We must not cross into another context:
3007 if (prev_hlock && prev_hlock->irq_context != hlock->irq_context)
3009 if (hlock->instance == lock)
3013 print_lock_contention_bug(curr, lock, ip);
3017 hlock->waittime_stamp = sched_clock();
3019 contention_point = lock_point(hlock_class(hlock)->contention_point, ip);
3020 contending_point = lock_point(hlock_class(hlock)->contending_point,
3023 stats = get_lock_stats(hlock_class(hlock));
3024 if (contention_point < LOCKSTAT_POINTS)
3025 stats->contention_point[contention_point]++;
3026 if (contending_point < LOCKSTAT_POINTS)
3027 stats->contending_point[contending_point]++;
3028 if (lock->cpu != smp_processor_id())
3029 stats->bounces[bounce_contended + !!hlock->read]++;
3030 put_lock_stats(stats);
3034 __lock_acquired(struct lockdep_map *lock, unsigned long ip)
3036 struct task_struct *curr = current;
3037 struct held_lock *hlock, *prev_hlock;
3038 struct lock_class_stats *stats;
3044 depth = curr->lockdep_depth;
3045 if (DEBUG_LOCKS_WARN_ON(!depth))
3049 for (i = depth-1; i >= 0; i--) {
3050 hlock = curr->held_locks + i;
3052 * We must not cross into another context:
3054 if (prev_hlock && prev_hlock->irq_context != hlock->irq_context)
3056 if (hlock->instance == lock)
3060 print_lock_contention_bug(curr, lock, _RET_IP_);
3064 cpu = smp_processor_id();
3065 if (hlock->waittime_stamp) {
3066 now = sched_clock();
3067 waittime = now - hlock->waittime_stamp;
3068 hlock->holdtime_stamp = now;
3071 stats = get_lock_stats(hlock_class(hlock));
3074 lock_time_inc(&stats->read_waittime, waittime);
3076 lock_time_inc(&stats->write_waittime, waittime);
3078 if (lock->cpu != cpu)
3079 stats->bounces[bounce_acquired + !!hlock->read]++;
3080 put_lock_stats(stats);
3086 void lock_contended(struct lockdep_map *lock, unsigned long ip)
3088 unsigned long flags;
3090 if (unlikely(!lock_stat))
3093 if (unlikely(current->lockdep_recursion))
3096 raw_local_irq_save(flags);
3098 current->lockdep_recursion = 1;
3099 __lock_contended(lock, ip);
3100 current->lockdep_recursion = 0;
3101 raw_local_irq_restore(flags);
3103 EXPORT_SYMBOL_GPL(lock_contended);
3105 void lock_acquired(struct lockdep_map *lock, unsigned long ip)
3107 unsigned long flags;
3109 if (unlikely(!lock_stat))
3112 if (unlikely(current->lockdep_recursion))
3115 raw_local_irq_save(flags);
3117 current->lockdep_recursion = 1;
3118 __lock_acquired(lock, ip);
3119 current->lockdep_recursion = 0;
3120 raw_local_irq_restore(flags);
3122 EXPORT_SYMBOL_GPL(lock_acquired);
3126 * Used by the testsuite, sanitize the validator state
3127 * after a simulated failure:
3130 void lockdep_reset(void)
3132 unsigned long flags;
3135 raw_local_irq_save(flags);
3136 current->curr_chain_key = 0;
3137 current->lockdep_depth = 0;
3138 current->lockdep_recursion = 0;
3139 memset(current->held_locks, 0, MAX_LOCK_DEPTH*sizeof(struct held_lock));
3140 nr_hardirq_chains = 0;
3141 nr_softirq_chains = 0;
3142 nr_process_chains = 0;
3144 for (i = 0; i < CHAINHASH_SIZE; i++)
3145 INIT_LIST_HEAD(chainhash_table + i);
3146 raw_local_irq_restore(flags);
3149 static void zap_class(struct lock_class *class)
3154 * Remove all dependencies this lock is
3157 for (i = 0; i < nr_list_entries; i++) {
3158 if (list_entries[i].class == class)
3159 list_del_rcu(&list_entries[i].entry);
3162 * Unhash the class and remove it from the all_lock_classes list:
3164 list_del_rcu(&class->hash_entry);
3165 list_del_rcu(&class->lock_entry);
3170 static inline int within(const void *addr, void *start, unsigned long size)
3172 return addr >= start && addr < start + size;
3175 void lockdep_free_key_range(void *start, unsigned long size)
3177 struct lock_class *class, *next;
3178 struct list_head *head;
3179 unsigned long flags;
3183 raw_local_irq_save(flags);
3184 locked = graph_lock();
3187 * Unhash all classes that were created by this module:
3189 for (i = 0; i < CLASSHASH_SIZE; i++) {
3190 head = classhash_table + i;
3191 if (list_empty(head))
3193 list_for_each_entry_safe(class, next, head, hash_entry) {
3194 if (within(class->key, start, size))
3196 else if (within(class->name, start, size))
3203 raw_local_irq_restore(flags);
3206 void lockdep_reset_lock(struct lockdep_map *lock)
3208 struct lock_class *class, *next;
3209 struct list_head *head;
3210 unsigned long flags;
3214 raw_local_irq_save(flags);
3217 * Remove all classes this lock might have:
3219 for (j = 0; j < MAX_LOCKDEP_SUBCLASSES; j++) {
3221 * If the class exists we look it up and zap it:
3223 class = look_up_lock_class(lock, j);
3228 * Debug check: in the end all mapped classes should
3231 locked = graph_lock();
3232 for (i = 0; i < CLASSHASH_SIZE; i++) {
3233 head = classhash_table + i;
3234 if (list_empty(head))
3236 list_for_each_entry_safe(class, next, head, hash_entry) {
3237 if (unlikely(class == lock->class_cache)) {
3238 if (debug_locks_off_graph_unlock())
3248 raw_local_irq_restore(flags);
3251 void lockdep_init(void)
3256 * Some architectures have their own start_kernel()
3257 * code which calls lockdep_init(), while we also
3258 * call lockdep_init() from the start_kernel() itself,
3259 * and we want to initialize the hashes only once:
3261 if (lockdep_initialized)
3264 for (i = 0; i < CLASSHASH_SIZE; i++)
3265 INIT_LIST_HEAD(classhash_table + i);
3267 for (i = 0; i < CHAINHASH_SIZE; i++)
3268 INIT_LIST_HEAD(chainhash_table + i);
3270 lockdep_initialized = 1;
3273 void __init lockdep_info(void)
3275 printk("Lock dependency validator: Copyright (c) 2006 Red Hat, Inc., Ingo Molnar\n");
3277 printk("... MAX_LOCKDEP_SUBCLASSES: %lu\n", MAX_LOCKDEP_SUBCLASSES);
3278 printk("... MAX_LOCK_DEPTH: %lu\n", MAX_LOCK_DEPTH);
3279 printk("... MAX_LOCKDEP_KEYS: %lu\n", MAX_LOCKDEP_KEYS);
3280 printk("... CLASSHASH_SIZE: %lu\n", CLASSHASH_SIZE);
3281 printk("... MAX_LOCKDEP_ENTRIES: %lu\n", MAX_LOCKDEP_ENTRIES);
3282 printk("... MAX_LOCKDEP_CHAINS: %lu\n", MAX_LOCKDEP_CHAINS);
3283 printk("... CHAINHASH_SIZE: %lu\n", CHAINHASH_SIZE);
3285 printk(" memory used by lock dependency info: %lu kB\n",
3286 (sizeof(struct lock_class) * MAX_LOCKDEP_KEYS +
3287 sizeof(struct list_head) * CLASSHASH_SIZE +
3288 sizeof(struct lock_list) * MAX_LOCKDEP_ENTRIES +
3289 sizeof(struct lock_chain) * MAX_LOCKDEP_CHAINS +
3290 sizeof(struct list_head) * CHAINHASH_SIZE) / 1024);
3292 printk(" per task-struct memory footprint: %lu bytes\n",
3293 sizeof(struct held_lock) * MAX_LOCK_DEPTH);
3295 #ifdef CONFIG_DEBUG_LOCKDEP
3296 if (lockdep_init_error) {
3297 printk("WARNING: lockdep init error! Arch code didn't call lockdep_init() early enough?\n");
3298 printk("Call stack leading to lockdep invocation was:\n");
3299 print_stack_trace(&lockdep_init_trace, 0);
3305 print_freed_lock_bug(struct task_struct *curr, const void *mem_from,
3306 const void *mem_to, struct held_lock *hlock)
3308 if (!debug_locks_off())
3310 if (debug_locks_silent)
3313 printk("\n=========================\n");
3314 printk( "[ BUG: held lock freed! ]\n");
3315 printk( "-------------------------\n");
3316 printk("%s/%d is freeing memory %p-%p, with a lock still held there!\n",
3317 curr->comm, task_pid_nr(curr), mem_from, mem_to-1);
3319 lockdep_print_held_locks(curr);
3321 printk("\nstack backtrace:\n");
3325 static inline int not_in_range(const void* mem_from, unsigned long mem_len,
3326 const void* lock_from, unsigned long lock_len)
3328 return lock_from + lock_len <= mem_from ||
3329 mem_from + mem_len <= lock_from;
3333 * Called when kernel memory is freed (or unmapped), or if a lock
3334 * is destroyed or reinitialized - this code checks whether there is
3335 * any held lock in the memory range of <from> to <to>:
3337 void debug_check_no_locks_freed(const void *mem_from, unsigned long mem_len)
3339 struct task_struct *curr = current;
3340 struct held_lock *hlock;
3341 unsigned long flags;
3344 if (unlikely(!debug_locks))
3347 local_irq_save(flags);
3348 for (i = 0; i < curr->lockdep_depth; i++) {
3349 hlock = curr->held_locks + i;
3351 if (not_in_range(mem_from, mem_len, hlock->instance,
3352 sizeof(*hlock->instance)))
3355 print_freed_lock_bug(curr, mem_from, mem_from + mem_len, hlock);
3358 local_irq_restore(flags);
3360 EXPORT_SYMBOL_GPL(debug_check_no_locks_freed);
3362 static void print_held_locks_bug(struct task_struct *curr)
3364 if (!debug_locks_off())
3366 if (debug_locks_silent)
3369 printk("\n=====================================\n");
3370 printk( "[ BUG: lock held at task exit time! ]\n");
3371 printk( "-------------------------------------\n");
3372 printk("%s/%d is exiting with locks still held!\n",
3373 curr->comm, task_pid_nr(curr));
3374 lockdep_print_held_locks(curr);
3376 printk("\nstack backtrace:\n");
3380 void debug_check_no_locks_held(struct task_struct *task)
3382 if (unlikely(task->lockdep_depth > 0))
3383 print_held_locks_bug(task);
3386 void debug_show_all_locks(void)
3388 struct task_struct *g, *p;
3392 if (unlikely(!debug_locks)) {
3393 printk("INFO: lockdep is turned off.\n");
3396 printk("\nShowing all locks held in the system:\n");
3399 * Here we try to get the tasklist_lock as hard as possible,
3400 * if not successful after 2 seconds we ignore it (but keep
3401 * trying). This is to enable a debug printout even if a
3402 * tasklist_lock-holding task deadlocks or crashes.
3405 if (!read_trylock(&tasklist_lock)) {
3407 printk("hm, tasklist_lock locked, retrying... ");
3410 printk(" #%d", 10-count);
3414 printk(" ignoring it.\n");
3418 printk(KERN_CONT " locked it.\n");
3421 do_each_thread(g, p) {
3423 * It's not reliable to print a task's held locks
3424 * if it's not sleeping (or if it's not the current
3427 if (p->state == TASK_RUNNING && p != current)
3429 if (p->lockdep_depth)
3430 lockdep_print_held_locks(p);
3432 if (read_trylock(&tasklist_lock))
3434 } while_each_thread(g, p);
3437 printk("=============================================\n\n");
3440 read_unlock(&tasklist_lock);
3442 EXPORT_SYMBOL_GPL(debug_show_all_locks);
3445 * Careful: only use this function if you are sure that
3446 * the task cannot run in parallel!
3448 void __debug_show_held_locks(struct task_struct *task)
3450 if (unlikely(!debug_locks)) {
3451 printk("INFO: lockdep is turned off.\n");
3454 lockdep_print_held_locks(task);
3456 EXPORT_SYMBOL_GPL(__debug_show_held_locks);
3458 void debug_show_held_locks(struct task_struct *task)
3460 __debug_show_held_locks(task);
3462 EXPORT_SYMBOL_GPL(debug_show_held_locks);
3464 void lockdep_sys_exit(void)
3466 struct task_struct *curr = current;
3468 if (unlikely(curr->lockdep_depth)) {
3469 if (!debug_locks_off())
3471 printk("\n================================================\n");
3472 printk( "[ BUG: lock held when returning to user space! ]\n");
3473 printk( "------------------------------------------------\n");
3474 printk("%s/%d is leaving the kernel with locks still held!\n",
3475 curr->comm, curr->pid);
3476 lockdep_print_held_locks(curr);