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>
45 #include <asm/sections.h>
47 #include "lockdep_internals.h"
49 #ifdef CONFIG_PROVE_LOCKING
50 int prove_locking = 1;
51 module_param(prove_locking, int, 0644);
53 #define prove_locking 0
56 #ifdef CONFIG_LOCK_STAT
58 module_param(lock_stat, int, 0644);
64 * lockdep_lock: protects the lockdep graph, the hashes and the
65 * class/list/hash allocators.
67 * This is one of the rare exceptions where it's justified
68 * to use a raw spinlock - we really dont want the spinlock
69 * code to recurse back into the lockdep code...
71 static raw_spinlock_t lockdep_lock = (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED;
73 static int graph_lock(void)
75 __raw_spin_lock(&lockdep_lock);
77 * Make sure that if another CPU detected a bug while
78 * walking the graph we dont change it (while the other
79 * CPU is busy printing out stuff with the graph lock
83 __raw_spin_unlock(&lockdep_lock);
86 /* prevent any recursions within lockdep from causing deadlocks */
87 current->lockdep_recursion++;
91 static inline int graph_unlock(void)
93 if (debug_locks && !__raw_spin_is_locked(&lockdep_lock))
94 return DEBUG_LOCKS_WARN_ON(1);
96 current->lockdep_recursion--;
97 __raw_spin_unlock(&lockdep_lock);
102 * Turn lock debugging off and return with 0 if it was off already,
103 * and also release the graph lock:
105 static inline int debug_locks_off_graph_unlock(void)
107 int ret = debug_locks_off();
109 __raw_spin_unlock(&lockdep_lock);
114 static int lockdep_initialized;
116 unsigned long nr_list_entries;
117 static struct lock_list list_entries[MAX_LOCKDEP_ENTRIES];
120 * All data structures here are protected by the global debug_lock.
122 * Mutex key structs only get allocated, once during bootup, and never
123 * get freed - this significantly simplifies the debugging code.
125 unsigned long nr_lock_classes;
126 static struct lock_class lock_classes[MAX_LOCKDEP_KEYS];
128 static inline struct lock_class *hlock_class(struct held_lock *hlock)
130 if (!hlock->class_idx) {
131 DEBUG_LOCKS_WARN_ON(1);
134 return lock_classes + hlock->class_idx - 1;
137 #ifdef CONFIG_LOCK_STAT
138 static DEFINE_PER_CPU(struct lock_class_stats[MAX_LOCKDEP_KEYS], lock_stats);
140 static int lock_point(unsigned long points[], unsigned long ip)
144 for (i = 0; i < LOCKSTAT_POINTS; i++) {
145 if (points[i] == 0) {
156 static void lock_time_inc(struct lock_time *lt, s64 time)
161 if (time < lt->min || !lt->min)
168 static inline void lock_time_add(struct lock_time *src, struct lock_time *dst)
170 dst->min += src->min;
171 dst->max += src->max;
172 dst->total += src->total;
176 struct lock_class_stats lock_stats(struct lock_class *class)
178 struct lock_class_stats stats;
181 memset(&stats, 0, sizeof(struct lock_class_stats));
182 for_each_possible_cpu(cpu) {
183 struct lock_class_stats *pcs =
184 &per_cpu(lock_stats, cpu)[class - lock_classes];
186 for (i = 0; i < ARRAY_SIZE(stats.contention_point); i++)
187 stats.contention_point[i] += pcs->contention_point[i];
189 for (i = 0; i < ARRAY_SIZE(stats.contending_point); i++)
190 stats.contending_point[i] += pcs->contending_point[i];
192 lock_time_add(&pcs->read_waittime, &stats.read_waittime);
193 lock_time_add(&pcs->write_waittime, &stats.write_waittime);
195 lock_time_add(&pcs->read_holdtime, &stats.read_holdtime);
196 lock_time_add(&pcs->write_holdtime, &stats.write_holdtime);
198 for (i = 0; i < ARRAY_SIZE(stats.bounces); i++)
199 stats.bounces[i] += pcs->bounces[i];
205 void clear_lock_stats(struct lock_class *class)
209 for_each_possible_cpu(cpu) {
210 struct lock_class_stats *cpu_stats =
211 &per_cpu(lock_stats, cpu)[class - lock_classes];
213 memset(cpu_stats, 0, sizeof(struct lock_class_stats));
215 memset(class->contention_point, 0, sizeof(class->contention_point));
216 memset(class->contending_point, 0, sizeof(class->contending_point));
219 static struct lock_class_stats *get_lock_stats(struct lock_class *class)
221 return &get_cpu_var(lock_stats)[class - lock_classes];
224 static void put_lock_stats(struct lock_class_stats *stats)
226 put_cpu_var(lock_stats);
229 static void lock_release_holdtime(struct held_lock *hlock)
231 struct lock_class_stats *stats;
237 holdtime = sched_clock() - hlock->holdtime_stamp;
239 stats = get_lock_stats(hlock_class(hlock));
241 lock_time_inc(&stats->read_holdtime, holdtime);
243 lock_time_inc(&stats->write_holdtime, holdtime);
244 put_lock_stats(stats);
247 static inline void lock_release_holdtime(struct held_lock *hlock)
253 * We keep a global list of all lock classes. The list only grows,
254 * never shrinks. The list is only accessed with the lockdep
255 * spinlock lock held.
257 LIST_HEAD(all_lock_classes);
260 * The lockdep classes are in a hash-table as well, for fast lookup:
262 #define CLASSHASH_BITS (MAX_LOCKDEP_KEYS_BITS - 1)
263 #define CLASSHASH_SIZE (1UL << CLASSHASH_BITS)
264 #define __classhashfn(key) hash_long((unsigned long)key, CLASSHASH_BITS)
265 #define classhashentry(key) (classhash_table + __classhashfn((key)))
267 static struct list_head classhash_table[CLASSHASH_SIZE];
270 * We put the lock dependency chains into a hash-table as well, to cache
273 #define CHAINHASH_BITS (MAX_LOCKDEP_CHAINS_BITS-1)
274 #define CHAINHASH_SIZE (1UL << CHAINHASH_BITS)
275 #define __chainhashfn(chain) hash_long(chain, CHAINHASH_BITS)
276 #define chainhashentry(chain) (chainhash_table + __chainhashfn((chain)))
278 static struct list_head chainhash_table[CHAINHASH_SIZE];
281 * The hash key of the lock dependency chains is a hash itself too:
282 * it's a hash of all locks taken up to that lock, including that lock.
283 * It's a 64-bit hash, because it's important for the keys to be
286 #define iterate_chain_key(key1, key2) \
287 (((key1) << MAX_LOCKDEP_KEYS_BITS) ^ \
288 ((key1) >> (64-MAX_LOCKDEP_KEYS_BITS)) ^ \
291 void lockdep_off(void)
293 current->lockdep_recursion++;
295 EXPORT_SYMBOL(lockdep_off);
297 void lockdep_on(void)
299 current->lockdep_recursion--;
301 EXPORT_SYMBOL(lockdep_on);
304 * Debugging switches:
308 #define VERY_VERBOSE 0
311 # define HARDIRQ_VERBOSE 1
312 # define SOFTIRQ_VERBOSE 1
313 # define RECLAIM_VERBOSE 1
315 # define HARDIRQ_VERBOSE 0
316 # define SOFTIRQ_VERBOSE 0
317 # define RECLAIM_VERBOSE 0
320 #if VERBOSE || HARDIRQ_VERBOSE || SOFTIRQ_VERBOSE || RECLAIM_VERBOSE
322 * Quick filtering for interesting events:
324 static int class_filter(struct lock_class *class)
328 if (class->name_version == 1 &&
329 !strcmp(class->name, "lockname"))
331 if (class->name_version == 1 &&
332 !strcmp(class->name, "&struct->lockfield"))
335 /* Filter everything else. 1 would be to allow everything else */
340 static int verbose(struct lock_class *class)
343 return class_filter(class);
349 * Stack-trace: tightly packed array of stack backtrace
350 * addresses. Protected by the graph_lock.
352 unsigned long nr_stack_trace_entries;
353 static unsigned long stack_trace[MAX_STACK_TRACE_ENTRIES];
355 static int save_trace(struct stack_trace *trace)
357 trace->nr_entries = 0;
358 trace->max_entries = MAX_STACK_TRACE_ENTRIES - nr_stack_trace_entries;
359 trace->entries = stack_trace + nr_stack_trace_entries;
363 save_stack_trace(trace);
365 trace->max_entries = trace->nr_entries;
367 nr_stack_trace_entries += trace->nr_entries;
369 if (nr_stack_trace_entries == MAX_STACK_TRACE_ENTRIES) {
370 if (!debug_locks_off_graph_unlock())
373 printk("BUG: MAX_STACK_TRACE_ENTRIES too low!\n");
374 printk("turning off the locking correctness validator.\n");
383 unsigned int nr_hardirq_chains;
384 unsigned int nr_softirq_chains;
385 unsigned int nr_process_chains;
386 unsigned int max_lockdep_depth;
387 unsigned int max_recursion_depth;
389 static unsigned int lockdep_dependency_gen_id;
391 static bool lockdep_dependency_visit(struct lock_class *source,
395 lockdep_dependency_gen_id++;
396 if (source->dep_gen_id == lockdep_dependency_gen_id)
398 source->dep_gen_id = lockdep_dependency_gen_id;
402 #ifdef CONFIG_DEBUG_LOCKDEP
404 * We cannot printk in early bootup code. Not even early_printk()
405 * might work. So we mark any initialization errors and printk
406 * about it later on, in lockdep_info().
408 static int lockdep_init_error;
409 static unsigned long lockdep_init_trace_data[20];
410 static struct stack_trace lockdep_init_trace = {
411 .max_entries = ARRAY_SIZE(lockdep_init_trace_data),
412 .entries = lockdep_init_trace_data,
416 * Various lockdep statistics:
418 atomic_t chain_lookup_hits;
419 atomic_t chain_lookup_misses;
420 atomic_t hardirqs_on_events;
421 atomic_t hardirqs_off_events;
422 atomic_t redundant_hardirqs_on;
423 atomic_t redundant_hardirqs_off;
424 atomic_t softirqs_on_events;
425 atomic_t softirqs_off_events;
426 atomic_t redundant_softirqs_on;
427 atomic_t redundant_softirqs_off;
428 atomic_t nr_unused_locks;
429 atomic_t nr_cyclic_checks;
430 atomic_t nr_cyclic_check_recursions;
431 atomic_t nr_find_usage_forwards_checks;
432 atomic_t nr_find_usage_forwards_recursions;
433 atomic_t nr_find_usage_backwards_checks;
434 atomic_t nr_find_usage_backwards_recursions;
435 # define debug_atomic_inc(ptr) atomic_inc(ptr)
436 # define debug_atomic_dec(ptr) atomic_dec(ptr)
437 # define debug_atomic_read(ptr) atomic_read(ptr)
439 # define debug_atomic_inc(ptr) do { } while (0)
440 # define debug_atomic_dec(ptr) do { } while (0)
441 # define debug_atomic_read(ptr) 0
448 #define __STR(foo) #foo
449 #define STR(foo) __STR(foo)
451 #define __USAGE(__STATE) \
452 [LOCK_USED_IN_##__STATE] = "IN-"STR(__STATE)"-W", \
453 [LOCK_ENABLED_##__STATE] = STR(__STATE)"-ON-W", \
454 [LOCK_USED_IN_##__STATE##_READ] = "IN-"STR(__STATE)"-R", \
455 [LOCK_ENABLED_##__STATE##_READ] = STR(__STATE)"-ON-R",
457 static const char *usage_str[] =
459 #define LOCKDEP_STATE(__STATE) __USAGE(__STATE)
460 #include "lockdep_states.h"
462 [LOCK_USED] = "INITIAL USE",
465 const char * __get_key_name(struct lockdep_subclass_key *key, char *str)
467 return kallsyms_lookup((unsigned long)key, NULL, NULL, NULL, str);
471 get_usage_chars(struct lock_class *class, char *c1, char *c2, char *c3,
472 char *c4, char *c5, char *c6)
474 *c1 = '.', *c2 = '.', *c3 = '.', *c4 = '.', *c5 = '.', *c6 = '.';
476 if (class->usage_mask & LOCKF_USED_IN_HARDIRQ)
479 if (class->usage_mask & LOCKF_ENABLED_HARDIRQ)
482 if (class->usage_mask & LOCKF_USED_IN_SOFTIRQ)
485 if (class->usage_mask & LOCKF_ENABLED_SOFTIRQ)
488 if (class->usage_mask & LOCKF_ENABLED_HARDIRQ_READ)
490 if (class->usage_mask & LOCKF_USED_IN_HARDIRQ_READ) {
492 if (class->usage_mask & LOCKF_ENABLED_HARDIRQ_READ)
496 if (class->usage_mask & LOCKF_ENABLED_SOFTIRQ_READ)
498 if (class->usage_mask & LOCKF_USED_IN_SOFTIRQ_READ) {
500 if (class->usage_mask & LOCKF_ENABLED_SOFTIRQ_READ)
504 if (class->usage_mask & LOCKF_USED_IN_RECLAIM_FS)
507 if (class->usage_mask & LOCKF_ENABLED_RECLAIM_FS)
510 if (class->usage_mask & LOCKF_ENABLED_RECLAIM_FS_READ)
512 if (class->usage_mask & LOCKF_USED_IN_RECLAIM_FS_READ) {
514 if (class->usage_mask & LOCKF_ENABLED_RECLAIM_FS_READ)
520 static void print_lock_name(struct lock_class *class)
522 char str[KSYM_NAME_LEN], c1, c2, c3, c4, c5, c6;
525 get_usage_chars(class, &c1, &c2, &c3, &c4, &c5, &c6);
529 name = __get_key_name(class->key, str);
530 printk(" (%s", name);
532 printk(" (%s", name);
533 if (class->name_version > 1)
534 printk("#%d", class->name_version);
536 printk("/%d", class->subclass);
538 printk("){%c%c%c%c%c%c}", c1, c2, c3, c4, c5, c6);
541 static void print_lockdep_cache(struct lockdep_map *lock)
544 char str[KSYM_NAME_LEN];
548 name = __get_key_name(lock->key->subkeys, str);
553 static void print_lock(struct held_lock *hlock)
555 print_lock_name(hlock_class(hlock));
557 print_ip_sym(hlock->acquire_ip);
560 static void lockdep_print_held_locks(struct task_struct *curr)
562 int i, depth = curr->lockdep_depth;
565 printk("no locks held by %s/%d.\n", curr->comm, task_pid_nr(curr));
568 printk("%d lock%s held by %s/%d:\n",
569 depth, depth > 1 ? "s" : "", curr->comm, task_pid_nr(curr));
571 for (i = 0; i < depth; i++) {
573 print_lock(curr->held_locks + i);
577 static void print_lock_class_header(struct lock_class *class, int depth)
581 printk("%*s->", depth, "");
582 print_lock_name(class);
583 printk(" ops: %lu", class->ops);
586 for (bit = 0; bit < LOCK_USAGE_STATES; bit++) {
587 if (class->usage_mask & (1 << bit)) {
590 len += printk("%*s %s", depth, "", usage_str[bit]);
591 len += printk(" at:\n");
592 print_stack_trace(class->usage_traces + bit, len);
595 printk("%*s }\n", depth, "");
597 printk("%*s ... key at: ",depth,"");
598 print_ip_sym((unsigned long)class->key);
602 * printk all lock dependencies starting at <entry>:
605 print_lock_dependencies(struct lock_class *class, int depth)
607 struct lock_list *entry;
609 if (lockdep_dependency_visit(class, depth))
612 if (DEBUG_LOCKS_WARN_ON(depth >= 20))
615 print_lock_class_header(class, depth);
617 list_for_each_entry(entry, &class->locks_after, entry) {
618 if (DEBUG_LOCKS_WARN_ON(!entry->class))
621 print_lock_dependencies(entry->class, depth + 1);
623 printk("%*s ... acquired at:\n",depth,"");
624 print_stack_trace(&entry->trace, 2);
629 static void print_kernel_version(void)
631 printk("%s %.*s\n", init_utsname()->release,
632 (int)strcspn(init_utsname()->version, " "),
633 init_utsname()->version);
636 static int very_verbose(struct lock_class *class)
639 return class_filter(class);
645 * Is this the address of a static object:
647 static int static_obj(void *obj)
649 unsigned long start = (unsigned long) &_stext,
650 end = (unsigned long) &_end,
651 addr = (unsigned long) obj;
659 if ((addr >= start) && (addr < end))
666 for_each_possible_cpu(i) {
667 start = (unsigned long) &__per_cpu_start + per_cpu_offset(i);
668 end = (unsigned long) &__per_cpu_start + PERCPU_ENOUGH_ROOM
671 if ((addr >= start) && (addr < end))
679 return is_module_address(addr);
683 * To make lock name printouts unique, we calculate a unique
684 * class->name_version generation counter:
686 static int count_matching_names(struct lock_class *new_class)
688 struct lock_class *class;
691 if (!new_class->name)
694 list_for_each_entry(class, &all_lock_classes, lock_entry) {
695 if (new_class->key - new_class->subclass == class->key)
696 return class->name_version;
697 if (class->name && !strcmp(class->name, new_class->name))
698 count = max(count, class->name_version);
705 * Register a lock's class in the hash-table, if the class is not present
706 * yet. Otherwise we look it up. We cache the result in the lock object
707 * itself, so actual lookup of the hash should be once per lock object.
709 static inline struct lock_class *
710 look_up_lock_class(struct lockdep_map *lock, unsigned int subclass)
712 struct lockdep_subclass_key *key;
713 struct list_head *hash_head;
714 struct lock_class *class;
716 #ifdef CONFIG_DEBUG_LOCKDEP
718 * If the architecture calls into lockdep before initializing
719 * the hashes then we'll warn about it later. (we cannot printk
722 if (unlikely(!lockdep_initialized)) {
724 lockdep_init_error = 1;
725 save_stack_trace(&lockdep_init_trace);
730 * Static locks do not have their class-keys yet - for them the key
731 * is the lock object itself:
733 if (unlikely(!lock->key))
734 lock->key = (void *)lock;
737 * NOTE: the class-key must be unique. For dynamic locks, a static
738 * lock_class_key variable is passed in through the mutex_init()
739 * (or spin_lock_init()) call - which acts as the key. For static
740 * locks we use the lock object itself as the key.
742 BUILD_BUG_ON(sizeof(struct lock_class_key) >
743 sizeof(struct lockdep_map));
745 key = lock->key->subkeys + subclass;
747 hash_head = classhashentry(key);
750 * We can walk the hash lockfree, because the hash only
751 * grows, and we are careful when adding entries to the end:
753 list_for_each_entry(class, hash_head, hash_entry) {
754 if (class->key == key) {
755 WARN_ON_ONCE(class->name != lock->name);
764 * Register a lock's class in the hash-table, if the class is not present
765 * yet. Otherwise we look it up. We cache the result in the lock object
766 * itself, so actual lookup of the hash should be once per lock object.
768 static inline struct lock_class *
769 register_lock_class(struct lockdep_map *lock, unsigned int subclass, int force)
771 struct lockdep_subclass_key *key;
772 struct list_head *hash_head;
773 struct lock_class *class;
776 class = look_up_lock_class(lock, subclass);
781 * Debug-check: all keys must be persistent!
783 if (!static_obj(lock->key)) {
785 printk("INFO: trying to register non-static key.\n");
786 printk("the code is fine but needs lockdep annotation.\n");
787 printk("turning off the locking correctness validator.\n");
793 key = lock->key->subkeys + subclass;
794 hash_head = classhashentry(key);
796 raw_local_irq_save(flags);
798 raw_local_irq_restore(flags);
802 * We have to do the hash-walk again, to avoid races
805 list_for_each_entry(class, hash_head, hash_entry)
806 if (class->key == key)
809 * Allocate a new key from the static array, and add it to
812 if (nr_lock_classes >= MAX_LOCKDEP_KEYS) {
813 if (!debug_locks_off_graph_unlock()) {
814 raw_local_irq_restore(flags);
817 raw_local_irq_restore(flags);
819 printk("BUG: MAX_LOCKDEP_KEYS too low!\n");
820 printk("turning off the locking correctness validator.\n");
823 class = lock_classes + nr_lock_classes++;
824 debug_atomic_inc(&nr_unused_locks);
826 class->name = lock->name;
827 class->subclass = subclass;
828 INIT_LIST_HEAD(&class->lock_entry);
829 INIT_LIST_HEAD(&class->locks_before);
830 INIT_LIST_HEAD(&class->locks_after);
831 class->name_version = count_matching_names(class);
833 * We use RCU's safe list-add method to make
834 * parallel walking of the hash-list safe:
836 list_add_tail_rcu(&class->hash_entry, hash_head);
838 * Add it to the global list of classes:
840 list_add_tail_rcu(&class->lock_entry, &all_lock_classes);
842 if (verbose(class)) {
844 raw_local_irq_restore(flags);
846 printk("\nnew class %p: %s", class->key, class->name);
847 if (class->name_version > 1)
848 printk("#%d", class->name_version);
852 raw_local_irq_save(flags);
854 raw_local_irq_restore(flags);
860 raw_local_irq_restore(flags);
862 if (!subclass || force)
863 lock->class_cache = class;
865 if (DEBUG_LOCKS_WARN_ON(class->subclass != subclass))
871 #ifdef CONFIG_PROVE_LOCKING
873 * Allocate a lockdep entry. (assumes the graph_lock held, returns
874 * with NULL on failure)
876 static struct lock_list *alloc_list_entry(void)
878 if (nr_list_entries >= MAX_LOCKDEP_ENTRIES) {
879 if (!debug_locks_off_graph_unlock())
882 printk("BUG: MAX_LOCKDEP_ENTRIES too low!\n");
883 printk("turning off the locking correctness validator.\n");
886 return list_entries + nr_list_entries++;
890 * Add a new dependency to the head of the list:
892 static int add_lock_to_list(struct lock_class *class, struct lock_class *this,
893 struct list_head *head, unsigned long ip, int distance)
895 struct lock_list *entry;
897 * Lock not present yet - get a new dependency struct and
898 * add it to the list:
900 entry = alloc_list_entry();
904 if (!save_trace(&entry->trace))
908 entry->distance = distance;
910 * Since we never remove from the dependency list, the list can
911 * be walked lockless by other CPUs, it's only allocation
912 * that must be protected by the spinlock. But this also means
913 * we must make new entries visible only once writes to the
914 * entry become visible - hence the RCU op:
916 list_add_tail_rcu(&entry->entry, head);
922 * Recursive, forwards-direction lock-dependency checking, used for
923 * both noncyclic checking and for hardirq-unsafe/softirq-unsafe
926 * (to keep the stackframe of the recursive functions small we
927 * use these global variables, and we also mark various helper
928 * functions as noinline.)
930 static struct held_lock *check_source, *check_target;
933 * Print a dependency chain entry (this is only done when a deadlock
934 * has been detected):
937 print_circular_bug_entry(struct lock_list *target, unsigned int depth)
939 if (debug_locks_silent)
941 printk("\n-> #%u", depth);
942 print_lock_name(target->class);
944 print_stack_trace(&target->trace, 6);
950 * When a circular dependency is detected, print the
954 print_circular_bug_header(struct lock_list *entry, unsigned int depth)
956 struct task_struct *curr = current;
958 if (!debug_locks_off_graph_unlock() || debug_locks_silent)
961 printk("\n=======================================================\n");
962 printk( "[ INFO: possible circular locking dependency detected ]\n");
963 print_kernel_version();
964 printk( "-------------------------------------------------------\n");
965 printk("%s/%d is trying to acquire lock:\n",
966 curr->comm, task_pid_nr(curr));
967 print_lock(check_source);
968 printk("\nbut task is already holding lock:\n");
969 print_lock(check_target);
970 printk("\nwhich lock already depends on the new lock.\n\n");
971 printk("\nthe existing dependency chain (in reverse order) is:\n");
973 print_circular_bug_entry(entry, depth);
978 static noinline int print_circular_bug_tail(void)
980 struct task_struct *curr = current;
981 struct lock_list this;
983 if (debug_locks_silent)
986 this.class = hlock_class(check_source);
987 if (!save_trace(&this.trace))
990 print_circular_bug_entry(&this, 0);
992 printk("\nother info that might help us debug this:\n\n");
993 lockdep_print_held_locks(curr);
995 printk("\nstack backtrace:\n");
1001 #define RECURSION_LIMIT 40
1003 static int noinline print_infinite_recursion_bug(void)
1005 if (!debug_locks_off_graph_unlock())
1013 unsigned long __lockdep_count_forward_deps(struct lock_class *class,
1016 struct lock_list *entry;
1017 unsigned long ret = 1;
1019 if (lockdep_dependency_visit(class, depth))
1023 * Recurse this class's dependency list:
1025 list_for_each_entry(entry, &class->locks_after, entry)
1026 ret += __lockdep_count_forward_deps(entry->class, depth + 1);
1031 unsigned long lockdep_count_forward_deps(struct lock_class *class)
1033 unsigned long ret, flags;
1035 local_irq_save(flags);
1036 __raw_spin_lock(&lockdep_lock);
1037 ret = __lockdep_count_forward_deps(class, 0);
1038 __raw_spin_unlock(&lockdep_lock);
1039 local_irq_restore(flags);
1044 unsigned long __lockdep_count_backward_deps(struct lock_class *class,
1047 struct lock_list *entry;
1048 unsigned long ret = 1;
1050 if (lockdep_dependency_visit(class, depth))
1053 * Recurse this class's dependency list:
1055 list_for_each_entry(entry, &class->locks_before, entry)
1056 ret += __lockdep_count_backward_deps(entry->class, depth + 1);
1061 unsigned long lockdep_count_backward_deps(struct lock_class *class)
1063 unsigned long ret, flags;
1065 local_irq_save(flags);
1066 __raw_spin_lock(&lockdep_lock);
1067 ret = __lockdep_count_backward_deps(class, 0);
1068 __raw_spin_unlock(&lockdep_lock);
1069 local_irq_restore(flags);
1075 * Prove that the dependency graph starting at <entry> can not
1076 * lead to <target>. Print an error and return 0 if it does.
1079 check_noncircular(struct lock_class *source, unsigned int depth)
1081 struct lock_list *entry;
1083 if (lockdep_dependency_visit(source, depth))
1086 debug_atomic_inc(&nr_cyclic_check_recursions);
1087 if (depth > max_recursion_depth)
1088 max_recursion_depth = depth;
1089 if (depth >= RECURSION_LIMIT)
1090 return print_infinite_recursion_bug();
1092 * Check this lock's dependency list:
1094 list_for_each_entry(entry, &source->locks_after, entry) {
1095 if (entry->class == hlock_class(check_target))
1096 return print_circular_bug_header(entry, depth+1);
1097 debug_atomic_inc(&nr_cyclic_checks);
1098 if (!check_noncircular(entry->class, depth+1))
1099 return print_circular_bug_entry(entry, depth+1);
1104 #if defined(CONFIG_TRACE_IRQFLAGS) && defined(CONFIG_PROVE_LOCKING)
1106 * Forwards and backwards subgraph searching, for the purposes of
1107 * proving that two subgraphs can be connected by a new dependency
1108 * without creating any illegal irq-safe -> irq-unsafe lock dependency.
1110 static enum lock_usage_bit find_usage_bit;
1111 static struct lock_class *forwards_match, *backwards_match;
1114 * Find a node in the forwards-direction dependency sub-graph starting
1115 * at <source> that matches <find_usage_bit>.
1117 * Return 2 if such a node exists in the subgraph, and put that node
1118 * into <forwards_match>.
1120 * Return 1 otherwise and keep <forwards_match> unchanged.
1121 * Return 0 on error.
1124 find_usage_forwards(struct lock_class *source, unsigned int depth)
1126 struct lock_list *entry;
1129 if (lockdep_dependency_visit(source, depth))
1132 if (depth > max_recursion_depth)
1133 max_recursion_depth = depth;
1134 if (depth >= RECURSION_LIMIT)
1135 return print_infinite_recursion_bug();
1137 debug_atomic_inc(&nr_find_usage_forwards_checks);
1138 if (source->usage_mask & (1 << find_usage_bit)) {
1139 forwards_match = source;
1144 * Check this lock's dependency list:
1146 list_for_each_entry(entry, &source->locks_after, entry) {
1147 debug_atomic_inc(&nr_find_usage_forwards_recursions);
1148 ret = find_usage_forwards(entry->class, depth+1);
1149 if (ret == 2 || ret == 0)
1156 * Find a node in the backwards-direction dependency sub-graph starting
1157 * at <source> that matches <find_usage_bit>.
1159 * Return 2 if such a node exists in the subgraph, and put that node
1160 * into <backwards_match>.
1162 * Return 1 otherwise and keep <backwards_match> unchanged.
1163 * Return 0 on error.
1166 find_usage_backwards(struct lock_class *source, unsigned int depth)
1168 struct lock_list *entry;
1171 if (lockdep_dependency_visit(source, depth))
1174 if (!__raw_spin_is_locked(&lockdep_lock))
1175 return DEBUG_LOCKS_WARN_ON(1);
1177 if (depth > max_recursion_depth)
1178 max_recursion_depth = depth;
1179 if (depth >= RECURSION_LIMIT)
1180 return print_infinite_recursion_bug();
1182 debug_atomic_inc(&nr_find_usage_backwards_checks);
1183 if (source->usage_mask & (1 << find_usage_bit)) {
1184 backwards_match = source;
1188 if (!source && debug_locks_off_graph_unlock()) {
1194 * Check this lock's dependency list:
1196 list_for_each_entry(entry, &source->locks_before, entry) {
1197 debug_atomic_inc(&nr_find_usage_backwards_recursions);
1198 ret = find_usage_backwards(entry->class, depth+1);
1199 if (ret == 2 || ret == 0)
1206 print_bad_irq_dependency(struct task_struct *curr,
1207 struct held_lock *prev,
1208 struct held_lock *next,
1209 enum lock_usage_bit bit1,
1210 enum lock_usage_bit bit2,
1211 const char *irqclass)
1213 if (!debug_locks_off_graph_unlock() || debug_locks_silent)
1216 printk("\n======================================================\n");
1217 printk( "[ INFO: %s-safe -> %s-unsafe lock order detected ]\n",
1218 irqclass, irqclass);
1219 print_kernel_version();
1220 printk( "------------------------------------------------------\n");
1221 printk("%s/%d [HC%u[%lu]:SC%u[%lu]:HE%u:SE%u] is trying to acquire:\n",
1222 curr->comm, task_pid_nr(curr),
1223 curr->hardirq_context, hardirq_count() >> HARDIRQ_SHIFT,
1224 curr->softirq_context, softirq_count() >> SOFTIRQ_SHIFT,
1225 curr->hardirqs_enabled,
1226 curr->softirqs_enabled);
1229 printk("\nand this task is already holding:\n");
1231 printk("which would create a new lock dependency:\n");
1232 print_lock_name(hlock_class(prev));
1234 print_lock_name(hlock_class(next));
1237 printk("\nbut this new dependency connects a %s-irq-safe lock:\n",
1239 print_lock_name(backwards_match);
1240 printk("\n... which became %s-irq-safe at:\n", irqclass);
1242 print_stack_trace(backwards_match->usage_traces + bit1, 1);
1244 printk("\nto a %s-irq-unsafe lock:\n", irqclass);
1245 print_lock_name(forwards_match);
1246 printk("\n... which became %s-irq-unsafe at:\n", irqclass);
1249 print_stack_trace(forwards_match->usage_traces + bit2, 1);
1251 printk("\nother info that might help us debug this:\n\n");
1252 lockdep_print_held_locks(curr);
1254 printk("\nthe %s-irq-safe lock's dependencies:\n", irqclass);
1255 print_lock_dependencies(backwards_match, 0);
1257 printk("\nthe %s-irq-unsafe lock's dependencies:\n", irqclass);
1258 print_lock_dependencies(forwards_match, 0);
1260 printk("\nstack backtrace:\n");
1267 check_usage(struct task_struct *curr, struct held_lock *prev,
1268 struct held_lock *next, enum lock_usage_bit bit_backwards,
1269 enum lock_usage_bit bit_forwards, const char *irqclass)
1273 find_usage_bit = bit_backwards;
1274 /* fills in <backwards_match> */
1275 ret = find_usage_backwards(hlock_class(prev), 0);
1276 if (!ret || ret == 1)
1279 find_usage_bit = bit_forwards;
1280 ret = find_usage_forwards(hlock_class(next), 0);
1281 if (!ret || ret == 1)
1284 return print_bad_irq_dependency(curr, prev, next,
1285 bit_backwards, bit_forwards, irqclass);
1289 check_prev_add_irq(struct task_struct *curr, struct held_lock *prev,
1290 struct held_lock *next)
1293 * Prove that the new dependency does not connect a hardirq-safe
1294 * lock with a hardirq-unsafe lock - to achieve this we search
1295 * the backwards-subgraph starting at <prev>, and the
1296 * forwards-subgraph starting at <next>:
1298 if (!check_usage(curr, prev, next, LOCK_USED_IN_HARDIRQ,
1299 LOCK_ENABLED_HARDIRQ, "hard"))
1303 * Prove that the new dependency does not connect a hardirq-safe-read
1304 * lock with a hardirq-unsafe lock - to achieve this we search
1305 * the backwards-subgraph starting at <prev>, and the
1306 * forwards-subgraph starting at <next>:
1308 if (!check_usage(curr, prev, next, LOCK_USED_IN_HARDIRQ_READ,
1309 LOCK_ENABLED_HARDIRQ, "hard-read"))
1313 * Prove that the new dependency does not connect a softirq-safe
1314 * lock with a softirq-unsafe lock - to achieve this we search
1315 * the backwards-subgraph starting at <prev>, and the
1316 * forwards-subgraph starting at <next>:
1318 if (!check_usage(curr, prev, next, LOCK_USED_IN_SOFTIRQ,
1319 LOCK_ENABLED_SOFTIRQ, "soft"))
1322 * Prove that the new dependency does not connect a softirq-safe-read
1323 * lock with a softirq-unsafe lock - to achieve this we search
1324 * the backwards-subgraph starting at <prev>, and the
1325 * forwards-subgraph starting at <next>:
1327 if (!check_usage(curr, prev, next, LOCK_USED_IN_SOFTIRQ_READ,
1328 LOCK_ENABLED_SOFTIRQ, "soft"))
1332 * Prove that the new dependency does not connect a reclaim-fs-safe
1333 * lock with a reclaim-fs-unsafe lock - to achieve this we search
1334 * the backwards-subgraph starting at <prev>, and the
1335 * forwards-subgraph starting at <next>:
1337 if (!check_usage(curr, prev, next, LOCK_USED_IN_RECLAIM_FS,
1338 LOCK_ENABLED_RECLAIM_FS, "reclaim-fs"))
1342 * Prove that the new dependency does not connect a reclaim-fs-safe-read
1343 * lock with a reclaim-fs-unsafe lock - to achieve this we search
1344 * the backwards-subgraph starting at <prev>, and the
1345 * forwards-subgraph starting at <next>:
1347 if (!check_usage(curr, prev, next, LOCK_USED_IN_RECLAIM_FS_READ,
1348 LOCK_ENABLED_RECLAIM_FS, "reclaim-fs-read"))
1354 static void inc_chains(void)
1356 if (current->hardirq_context)
1357 nr_hardirq_chains++;
1359 if (current->softirq_context)
1360 nr_softirq_chains++;
1362 nr_process_chains++;
1369 check_prev_add_irq(struct task_struct *curr, struct held_lock *prev,
1370 struct held_lock *next)
1375 static inline void inc_chains(void)
1377 nr_process_chains++;
1383 print_deadlock_bug(struct task_struct *curr, struct held_lock *prev,
1384 struct held_lock *next)
1386 if (!debug_locks_off_graph_unlock() || debug_locks_silent)
1389 printk("\n=============================================\n");
1390 printk( "[ INFO: possible recursive locking detected ]\n");
1391 print_kernel_version();
1392 printk( "---------------------------------------------\n");
1393 printk("%s/%d is trying to acquire lock:\n",
1394 curr->comm, task_pid_nr(curr));
1396 printk("\nbut task is already holding lock:\n");
1399 printk("\nother info that might help us debug this:\n");
1400 lockdep_print_held_locks(curr);
1402 printk("\nstack backtrace:\n");
1409 * Check whether we are holding such a class already.
1411 * (Note that this has to be done separately, because the graph cannot
1412 * detect such classes of deadlocks.)
1414 * Returns: 0 on deadlock detected, 1 on OK, 2 on recursive read
1417 check_deadlock(struct task_struct *curr, struct held_lock *next,
1418 struct lockdep_map *next_instance, int read)
1420 struct held_lock *prev;
1421 struct held_lock *nest = NULL;
1424 for (i = 0; i < curr->lockdep_depth; i++) {
1425 prev = curr->held_locks + i;
1427 if (prev->instance == next->nest_lock)
1430 if (hlock_class(prev) != hlock_class(next))
1434 * Allow read-after-read recursion of the same
1435 * lock class (i.e. read_lock(lock)+read_lock(lock)):
1437 if ((read == 2) && prev->read)
1441 * We're holding the nest_lock, which serializes this lock's
1442 * nesting behaviour.
1447 return print_deadlock_bug(curr, prev, next);
1453 * There was a chain-cache miss, and we are about to add a new dependency
1454 * to a previous lock. We recursively validate the following rules:
1456 * - would the adding of the <prev> -> <next> dependency create a
1457 * circular dependency in the graph? [== circular deadlock]
1459 * - does the new prev->next dependency connect any hardirq-safe lock
1460 * (in the full backwards-subgraph starting at <prev>) with any
1461 * hardirq-unsafe lock (in the full forwards-subgraph starting at
1462 * <next>)? [== illegal lock inversion with hardirq contexts]
1464 * - does the new prev->next dependency connect any softirq-safe lock
1465 * (in the full backwards-subgraph starting at <prev>) with any
1466 * softirq-unsafe lock (in the full forwards-subgraph starting at
1467 * <next>)? [== illegal lock inversion with softirq contexts]
1469 * any of these scenarios could lead to a deadlock.
1471 * Then if all the validations pass, we add the forwards and backwards
1475 check_prev_add(struct task_struct *curr, struct held_lock *prev,
1476 struct held_lock *next, int distance)
1478 struct lock_list *entry;
1482 * Prove that the new <prev> -> <next> dependency would not
1483 * create a circular dependency in the graph. (We do this by
1484 * forward-recursing into the graph starting at <next>, and
1485 * checking whether we can reach <prev>.)
1487 * We are using global variables to control the recursion, to
1488 * keep the stackframe size of the recursive functions low:
1490 check_source = next;
1491 check_target = prev;
1492 if (!(check_noncircular(hlock_class(next), 0)))
1493 return print_circular_bug_tail();
1495 if (!check_prev_add_irq(curr, prev, next))
1499 * For recursive read-locks we do all the dependency checks,
1500 * but we dont store read-triggered dependencies (only
1501 * write-triggered dependencies). This ensures that only the
1502 * write-side dependencies matter, and that if for example a
1503 * write-lock never takes any other locks, then the reads are
1504 * equivalent to a NOP.
1506 if (next->read == 2 || prev->read == 2)
1509 * Is the <prev> -> <next> dependency already present?
1511 * (this may occur even though this is a new chain: consider
1512 * e.g. the L1 -> L2 -> L3 -> L4 and the L5 -> L1 -> L2 -> L3
1513 * chains - the second one will be new, but L1 already has
1514 * L2 added to its dependency list, due to the first chain.)
1516 list_for_each_entry(entry, &hlock_class(prev)->locks_after, entry) {
1517 if (entry->class == hlock_class(next)) {
1519 entry->distance = 1;
1525 * Ok, all validations passed, add the new lock
1526 * to the previous lock's dependency list:
1528 ret = add_lock_to_list(hlock_class(prev), hlock_class(next),
1529 &hlock_class(prev)->locks_after,
1530 next->acquire_ip, distance);
1535 ret = add_lock_to_list(hlock_class(next), hlock_class(prev),
1536 &hlock_class(next)->locks_before,
1537 next->acquire_ip, distance);
1542 * Debugging printouts:
1544 if (verbose(hlock_class(prev)) || verbose(hlock_class(next))) {
1546 printk("\n new dependency: ");
1547 print_lock_name(hlock_class(prev));
1549 print_lock_name(hlock_class(next));
1552 return graph_lock();
1558 * Add the dependency to all directly-previous locks that are 'relevant'.
1559 * The ones that are relevant are (in increasing distance from curr):
1560 * all consecutive trylock entries and the final non-trylock entry - or
1561 * the end of this context's lock-chain - whichever comes first.
1564 check_prevs_add(struct task_struct *curr, struct held_lock *next)
1566 int depth = curr->lockdep_depth;
1567 struct held_lock *hlock;
1572 * Depth must not be zero for a non-head lock:
1577 * At least two relevant locks must exist for this
1580 if (curr->held_locks[depth].irq_context !=
1581 curr->held_locks[depth-1].irq_context)
1585 int distance = curr->lockdep_depth - depth + 1;
1586 hlock = curr->held_locks + depth-1;
1588 * Only non-recursive-read entries get new dependencies
1591 if (hlock->read != 2) {
1592 if (!check_prev_add(curr, hlock, next, distance))
1595 * Stop after the first non-trylock entry,
1596 * as non-trylock entries have added their
1597 * own direct dependencies already, so this
1598 * lock is connected to them indirectly:
1600 if (!hlock->trylock)
1605 * End of lock-stack?
1610 * Stop the search if we cross into another context:
1612 if (curr->held_locks[depth].irq_context !=
1613 curr->held_locks[depth-1].irq_context)
1618 if (!debug_locks_off_graph_unlock())
1626 unsigned long nr_lock_chains;
1627 struct lock_chain lock_chains[MAX_LOCKDEP_CHAINS];
1628 int nr_chain_hlocks;
1629 static u16 chain_hlocks[MAX_LOCKDEP_CHAIN_HLOCKS];
1631 struct lock_class *lock_chain_get_class(struct lock_chain *chain, int i)
1633 return lock_classes + chain_hlocks[chain->base + i];
1637 * Look up a dependency chain. If the key is not present yet then
1638 * add it and return 1 - in this case the new dependency chain is
1639 * validated. If the key is already hashed, return 0.
1640 * (On return with 1 graph_lock is held.)
1642 static inline int lookup_chain_cache(struct task_struct *curr,
1643 struct held_lock *hlock,
1646 struct lock_class *class = hlock_class(hlock);
1647 struct list_head *hash_head = chainhashentry(chain_key);
1648 struct lock_chain *chain;
1649 struct held_lock *hlock_curr, *hlock_next;
1652 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
1655 * We can walk it lock-free, because entries only get added
1658 list_for_each_entry(chain, hash_head, entry) {
1659 if (chain->chain_key == chain_key) {
1661 debug_atomic_inc(&chain_lookup_hits);
1662 if (very_verbose(class))
1663 printk("\nhash chain already cached, key: "
1664 "%016Lx tail class: [%p] %s\n",
1665 (unsigned long long)chain_key,
1666 class->key, class->name);
1670 if (very_verbose(class))
1671 printk("\nnew hash chain, key: %016Lx tail class: [%p] %s\n",
1672 (unsigned long long)chain_key, class->key, class->name);
1674 * Allocate a new chain entry from the static array, and add
1680 * We have to walk the chain again locked - to avoid duplicates:
1682 list_for_each_entry(chain, hash_head, entry) {
1683 if (chain->chain_key == chain_key) {
1688 if (unlikely(nr_lock_chains >= MAX_LOCKDEP_CHAINS)) {
1689 if (!debug_locks_off_graph_unlock())
1692 printk("BUG: MAX_LOCKDEP_CHAINS too low!\n");
1693 printk("turning off the locking correctness validator.\n");
1696 chain = lock_chains + nr_lock_chains++;
1697 chain->chain_key = chain_key;
1698 chain->irq_context = hlock->irq_context;
1699 /* Find the first held_lock of current chain */
1701 for (i = curr->lockdep_depth - 1; i >= 0; i--) {
1702 hlock_curr = curr->held_locks + i;
1703 if (hlock_curr->irq_context != hlock_next->irq_context)
1708 chain->depth = curr->lockdep_depth + 1 - i;
1709 cn = nr_chain_hlocks;
1710 while (cn + chain->depth <= MAX_LOCKDEP_CHAIN_HLOCKS) {
1711 n = cmpxchg(&nr_chain_hlocks, cn, cn + chain->depth);
1716 if (likely(cn + chain->depth <= MAX_LOCKDEP_CHAIN_HLOCKS)) {
1718 for (j = 0; j < chain->depth - 1; j++, i++) {
1719 int lock_id = curr->held_locks[i].class_idx - 1;
1720 chain_hlocks[chain->base + j] = lock_id;
1722 chain_hlocks[chain->base + j] = class - lock_classes;
1724 list_add_tail_rcu(&chain->entry, hash_head);
1725 debug_atomic_inc(&chain_lookup_misses);
1731 static int validate_chain(struct task_struct *curr, struct lockdep_map *lock,
1732 struct held_lock *hlock, int chain_head, u64 chain_key)
1735 * Trylock needs to maintain the stack of held locks, but it
1736 * does not add new dependencies, because trylock can be done
1739 * We look up the chain_key and do the O(N^2) check and update of
1740 * the dependencies only if this is a new dependency chain.
1741 * (If lookup_chain_cache() returns with 1 it acquires
1742 * graph_lock for us)
1744 if (!hlock->trylock && (hlock->check == 2) &&
1745 lookup_chain_cache(curr, hlock, chain_key)) {
1747 * Check whether last held lock:
1749 * - is irq-safe, if this lock is irq-unsafe
1750 * - is softirq-safe, if this lock is hardirq-unsafe
1752 * And check whether the new lock's dependency graph
1753 * could lead back to the previous lock.
1755 * any of these scenarios could lead to a deadlock. If
1758 int ret = check_deadlock(curr, hlock, lock, hlock->read);
1763 * Mark recursive read, as we jump over it when
1764 * building dependencies (just like we jump over
1770 * Add dependency only if this lock is not the head
1771 * of the chain, and if it's not a secondary read-lock:
1773 if (!chain_head && ret != 2)
1774 if (!check_prevs_add(curr, hlock))
1778 /* after lookup_chain_cache(): */
1779 if (unlikely(!debug_locks))
1785 static inline int validate_chain(struct task_struct *curr,
1786 struct lockdep_map *lock, struct held_lock *hlock,
1787 int chain_head, u64 chain_key)
1794 * We are building curr_chain_key incrementally, so double-check
1795 * it from scratch, to make sure that it's done correctly:
1797 static void check_chain_key(struct task_struct *curr)
1799 #ifdef CONFIG_DEBUG_LOCKDEP
1800 struct held_lock *hlock, *prev_hlock = NULL;
1804 for (i = 0; i < curr->lockdep_depth; i++) {
1805 hlock = curr->held_locks + i;
1806 if (chain_key != hlock->prev_chain_key) {
1808 WARN(1, "hm#1, depth: %u [%u], %016Lx != %016Lx\n",
1809 curr->lockdep_depth, i,
1810 (unsigned long long)chain_key,
1811 (unsigned long long)hlock->prev_chain_key);
1814 id = hlock->class_idx - 1;
1815 if (DEBUG_LOCKS_WARN_ON(id >= MAX_LOCKDEP_KEYS))
1818 if (prev_hlock && (prev_hlock->irq_context !=
1819 hlock->irq_context))
1821 chain_key = iterate_chain_key(chain_key, id);
1824 if (chain_key != curr->curr_chain_key) {
1826 WARN(1, "hm#2, depth: %u [%u], %016Lx != %016Lx\n",
1827 curr->lockdep_depth, i,
1828 (unsigned long long)chain_key,
1829 (unsigned long long)curr->curr_chain_key);
1835 print_usage_bug(struct task_struct *curr, struct held_lock *this,
1836 enum lock_usage_bit prev_bit, enum lock_usage_bit new_bit)
1838 if (!debug_locks_off_graph_unlock() || debug_locks_silent)
1841 printk("\n=================================\n");
1842 printk( "[ INFO: inconsistent lock state ]\n");
1843 print_kernel_version();
1844 printk( "---------------------------------\n");
1846 printk("inconsistent {%s} -> {%s} usage.\n",
1847 usage_str[prev_bit], usage_str[new_bit]);
1849 printk("%s/%d [HC%u[%lu]:SC%u[%lu]:HE%u:SE%u] takes:\n",
1850 curr->comm, task_pid_nr(curr),
1851 trace_hardirq_context(curr), hardirq_count() >> HARDIRQ_SHIFT,
1852 trace_softirq_context(curr), softirq_count() >> SOFTIRQ_SHIFT,
1853 trace_hardirqs_enabled(curr),
1854 trace_softirqs_enabled(curr));
1857 printk("{%s} state was registered at:\n", usage_str[prev_bit]);
1858 print_stack_trace(hlock_class(this)->usage_traces + prev_bit, 1);
1860 print_irqtrace_events(curr);
1861 printk("\nother info that might help us debug this:\n");
1862 lockdep_print_held_locks(curr);
1864 printk("\nstack backtrace:\n");
1871 * Print out an error if an invalid bit is set:
1874 valid_state(struct task_struct *curr, struct held_lock *this,
1875 enum lock_usage_bit new_bit, enum lock_usage_bit bad_bit)
1877 if (unlikely(hlock_class(this)->usage_mask & (1 << bad_bit)))
1878 return print_usage_bug(curr, this, bad_bit, new_bit);
1882 static int mark_lock(struct task_struct *curr, struct held_lock *this,
1883 enum lock_usage_bit new_bit);
1885 #if defined(CONFIG_TRACE_IRQFLAGS) && defined(CONFIG_PROVE_LOCKING)
1888 * print irq inversion bug:
1891 print_irq_inversion_bug(struct task_struct *curr, struct lock_class *other,
1892 struct held_lock *this, int forwards,
1893 const char *irqclass)
1895 if (!debug_locks_off_graph_unlock() || debug_locks_silent)
1898 printk("\n=========================================================\n");
1899 printk( "[ INFO: possible irq lock inversion dependency detected ]\n");
1900 print_kernel_version();
1901 printk( "---------------------------------------------------------\n");
1902 printk("%s/%d just changed the state of lock:\n",
1903 curr->comm, task_pid_nr(curr));
1906 printk("but this lock took another, %s-irq-unsafe lock in the past:\n", irqclass);
1908 printk("but this lock was taken by another, %s-irq-safe lock in the past:\n", irqclass);
1909 print_lock_name(other);
1910 printk("\n\nand interrupts could create inverse lock ordering between them.\n\n");
1912 printk("\nother info that might help us debug this:\n");
1913 lockdep_print_held_locks(curr);
1915 printk("\nthe first lock's dependencies:\n");
1916 print_lock_dependencies(hlock_class(this), 0);
1918 printk("\nthe second lock's dependencies:\n");
1919 print_lock_dependencies(other, 0);
1921 printk("\nstack backtrace:\n");
1928 * Prove that in the forwards-direction subgraph starting at <this>
1929 * there is no lock matching <mask>:
1932 check_usage_forwards(struct task_struct *curr, struct held_lock *this,
1933 enum lock_usage_bit bit, const char *irqclass)
1937 find_usage_bit = bit;
1938 /* fills in <forwards_match> */
1939 ret = find_usage_forwards(hlock_class(this), 0);
1940 if (!ret || ret == 1)
1943 return print_irq_inversion_bug(curr, forwards_match, this, 1, irqclass);
1947 * Prove that in the backwards-direction subgraph starting at <this>
1948 * there is no lock matching <mask>:
1951 check_usage_backwards(struct task_struct *curr, struct held_lock *this,
1952 enum lock_usage_bit bit, const char *irqclass)
1956 find_usage_bit = bit;
1957 /* fills in <backwards_match> */
1958 ret = find_usage_backwards(hlock_class(this), 0);
1959 if (!ret || ret == 1)
1962 return print_irq_inversion_bug(curr, backwards_match, this, 0, irqclass);
1965 void print_irqtrace_events(struct task_struct *curr)
1967 printk("irq event stamp: %u\n", curr->irq_events);
1968 printk("hardirqs last enabled at (%u): ", curr->hardirq_enable_event);
1969 print_ip_sym(curr->hardirq_enable_ip);
1970 printk("hardirqs last disabled at (%u): ", curr->hardirq_disable_event);
1971 print_ip_sym(curr->hardirq_disable_ip);
1972 printk("softirqs last enabled at (%u): ", curr->softirq_enable_event);
1973 print_ip_sym(curr->softirq_enable_ip);
1974 printk("softirqs last disabled at (%u): ", curr->softirq_disable_event);
1975 print_ip_sym(curr->softirq_disable_ip);
1978 static int HARDIRQ_verbose(struct lock_class *class)
1981 return class_filter(class);
1986 static int SOFTIRQ_verbose(struct lock_class *class)
1989 return class_filter(class);
1994 static int RECLAIM_FS_verbose(struct lock_class *class)
1997 return class_filter(class);
2002 #define STRICT_READ_CHECKS 1
2004 static const char *state_names[] = {
2005 #define LOCKDEP_STATE(__STATE) \
2007 #include "lockdep_states.h"
2008 #undef LOCKDEP_STATE
2011 static inline const char *state_name(enum lock_usage_bit bit)
2013 return state_names[bit >> 2];
2016 static const char *state_rnames[] = {
2017 #define LOCKDEP_STATE(__STATE) \
2018 STR(__STATE)"-READ",
2019 #include "lockdep_states.h"
2020 #undef LOCKDEP_STATE
2023 static inline const char *state_rname(enum lock_usage_bit bit)
2025 return state_rnames[bit >> 2];
2028 static int (*state_verbose_f[])(struct lock_class *class) = {
2029 #define LOCKDEP_STATE(__STATE) \
2031 #include "lockdep_states.h"
2032 #undef LOCKDEP_STATE
2035 static inline int state_verbose(enum lock_usage_bit bit,
2036 struct lock_class *class)
2038 return state_verbose_f[bit >> 2](class);
2041 static int exclusive_bit(int new_bit)
2049 * bit 0 - write/read
2050 * bit 1 - used_in/enabled
2054 int state = new_bit & ~3;
2055 int dir = new_bit & 2;
2057 return state | (dir ^ 2);
2060 typedef int (*check_usage_f)(struct task_struct *, struct held_lock *,
2061 enum lock_usage_bit bit, const char *name);
2064 mark_lock_irq_write(struct task_struct *curr, struct held_lock *this,
2067 const char *name = state_name(new_bit);
2068 const char *rname = state_rname(new_bit);
2070 int excl_bit = exclusive_bit(new_bit);
2071 int dir = new_bit & 2;
2073 check_usage_f usage = dir ?
2074 check_usage_backwards : check_usage_forwards;
2076 if (!valid_state(curr, this, new_bit, excl_bit))
2078 if (!valid_state(curr, this, new_bit, excl_bit + 1))
2082 * just marked it hardirq-safe, check that this lock
2083 * took no hardirq-unsafe lock in the past:
2085 if (!usage(curr, this, excl_bit, name))
2087 #if STRICT_READ_CHECKS
2089 * just marked it hardirq-safe, check that this lock
2090 * took no hardirq-unsafe-read lock in the past:
2092 if (!usage(curr, this, excl_bit + 1, rname))
2095 if (state_verbose(new_bit, hlock_class(this)))
2102 mark_lock_irq_read(struct task_struct *curr, struct held_lock *this,
2105 const char *name = state_name(new_bit);
2106 const char *rname = state_rname(new_bit);
2108 int excl_bit = exclusive_bit(new_bit);
2109 int dir = new_bit & 2;
2111 if (!valid_state(curr, this, new_bit, excl_bit))
2116 * just marked it hardirq-read-safe, check that this lock
2117 * took no hardirq-unsafe lock in the past:
2119 if (!check_usage_forwards(curr, this, excl_bit, name))
2121 } else if (STRICT_READ_CHECKS) {
2123 * just marked it hardirq-read-unsafe, check that no
2124 * hardirq-safe lock in the system ever took it in the past:
2126 if (!check_usage_backwards(curr, this, excl_bit, name))
2130 if (state_verbose(new_bit, hlock_class(this)))
2136 static int mark_lock_irq(struct task_struct *curr, struct held_lock *this,
2137 enum lock_usage_bit new_bit)
2142 case LOCK_USED_IN_HARDIRQ:
2143 case LOCK_USED_IN_SOFTIRQ:
2144 case LOCK_USED_IN_RECLAIM_FS:
2145 case LOCK_ENABLED_HARDIRQ:
2146 case LOCK_ENABLED_SOFTIRQ:
2147 case LOCK_ENABLED_RECLAIM_FS:
2148 return mark_lock_irq_write(curr, this, new_bit);
2150 case LOCK_USED_IN_HARDIRQ_READ:
2151 case LOCK_USED_IN_SOFTIRQ_READ:
2152 case LOCK_USED_IN_RECLAIM_FS_READ:
2153 case LOCK_ENABLED_HARDIRQ_READ:
2154 case LOCK_ENABLED_SOFTIRQ_READ:
2155 case LOCK_ENABLED_RECLAIM_FS_READ:
2156 return mark_lock_irq_read(curr, this, new_bit);
2167 #define LOCKDEP_STATE(__STATE) __STATE,
2168 #include "lockdep_states.h"
2169 #undef LOCKDEP_STATE
2172 #define MARK_HELD_CASE(__STATE) \
2175 usage_bit = LOCK_ENABLED_##__STATE##_READ; \
2177 usage_bit = LOCK_ENABLED_##__STATE; \
2181 * Mark all held locks with a usage bit:
2184 mark_held_locks(struct task_struct *curr, enum mark_type mark)
2186 enum lock_usage_bit usage_bit;
2187 struct held_lock *hlock;
2190 for (i = 0; i < curr->lockdep_depth; i++) {
2191 hlock = curr->held_locks + i;
2194 #define LOCKDEP_STATE(__STATE) MARK_HELD_CASE(__STATE)
2195 #include "lockdep_states.h"
2196 #undef LOCKDEP_STATE
2201 if (!mark_lock(curr, hlock, usage_bit))
2209 * Debugging helper: via this flag we know that we are in
2210 * 'early bootup code', and will warn about any invalid irqs-on event:
2212 static int early_boot_irqs_enabled;
2214 void early_boot_irqs_off(void)
2216 early_boot_irqs_enabled = 0;
2219 void early_boot_irqs_on(void)
2221 early_boot_irqs_enabled = 1;
2225 * Hardirqs will be enabled:
2227 void trace_hardirqs_on_caller(unsigned long ip)
2229 struct task_struct *curr = current;
2231 time_hardirqs_on(CALLER_ADDR0, ip);
2233 if (unlikely(!debug_locks || current->lockdep_recursion))
2236 if (DEBUG_LOCKS_WARN_ON(unlikely(!early_boot_irqs_enabled)))
2239 if (unlikely(curr->hardirqs_enabled)) {
2240 debug_atomic_inc(&redundant_hardirqs_on);
2243 /* we'll do an OFF -> ON transition: */
2244 curr->hardirqs_enabled = 1;
2246 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2248 if (DEBUG_LOCKS_WARN_ON(current->hardirq_context))
2251 * We are going to turn hardirqs on, so set the
2252 * usage bit for all held locks:
2254 if (!mark_held_locks(curr, HARDIRQ))
2257 * If we have softirqs enabled, then set the usage
2258 * bit for all held locks. (disabled hardirqs prevented
2259 * this bit from being set before)
2261 if (curr->softirqs_enabled)
2262 if (!mark_held_locks(curr, SOFTIRQ))
2265 curr->hardirq_enable_ip = ip;
2266 curr->hardirq_enable_event = ++curr->irq_events;
2267 debug_atomic_inc(&hardirqs_on_events);
2269 EXPORT_SYMBOL(trace_hardirqs_on_caller);
2271 void trace_hardirqs_on(void)
2273 trace_hardirqs_on_caller(CALLER_ADDR0);
2275 EXPORT_SYMBOL(trace_hardirqs_on);
2278 * Hardirqs were disabled:
2280 void trace_hardirqs_off_caller(unsigned long ip)
2282 struct task_struct *curr = current;
2284 time_hardirqs_off(CALLER_ADDR0, ip);
2286 if (unlikely(!debug_locks || current->lockdep_recursion))
2289 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2292 if (curr->hardirqs_enabled) {
2294 * We have done an ON -> OFF transition:
2296 curr->hardirqs_enabled = 0;
2297 curr->hardirq_disable_ip = ip;
2298 curr->hardirq_disable_event = ++curr->irq_events;
2299 debug_atomic_inc(&hardirqs_off_events);
2301 debug_atomic_inc(&redundant_hardirqs_off);
2303 EXPORT_SYMBOL(trace_hardirqs_off_caller);
2305 void trace_hardirqs_off(void)
2307 trace_hardirqs_off_caller(CALLER_ADDR0);
2309 EXPORT_SYMBOL(trace_hardirqs_off);
2312 * Softirqs will be enabled:
2314 void trace_softirqs_on(unsigned long ip)
2316 struct task_struct *curr = current;
2318 if (unlikely(!debug_locks))
2321 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2324 if (curr->softirqs_enabled) {
2325 debug_atomic_inc(&redundant_softirqs_on);
2330 * We'll do an OFF -> ON transition:
2332 curr->softirqs_enabled = 1;
2333 curr->softirq_enable_ip = ip;
2334 curr->softirq_enable_event = ++curr->irq_events;
2335 debug_atomic_inc(&softirqs_on_events);
2337 * We are going to turn softirqs on, so set the
2338 * usage bit for all held locks, if hardirqs are
2341 if (curr->hardirqs_enabled)
2342 mark_held_locks(curr, SOFTIRQ);
2346 * Softirqs were disabled:
2348 void trace_softirqs_off(unsigned long ip)
2350 struct task_struct *curr = current;
2352 if (unlikely(!debug_locks))
2355 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2358 if (curr->softirqs_enabled) {
2360 * We have done an ON -> OFF transition:
2362 curr->softirqs_enabled = 0;
2363 curr->softirq_disable_ip = ip;
2364 curr->softirq_disable_event = ++curr->irq_events;
2365 debug_atomic_inc(&softirqs_off_events);
2366 DEBUG_LOCKS_WARN_ON(!softirq_count());
2368 debug_atomic_inc(&redundant_softirqs_off);
2371 void lockdep_trace_alloc(gfp_t gfp_mask)
2373 struct task_struct *curr = current;
2375 if (unlikely(!debug_locks))
2378 /* no reclaim without waiting on it */
2379 if (!(gfp_mask & __GFP_WAIT))
2382 /* this guy won't enter reclaim */
2383 if ((curr->flags & PF_MEMALLOC) && !(gfp_mask & __GFP_NOMEMALLOC))
2386 /* We're only interested __GFP_FS allocations for now */
2387 if (!(gfp_mask & __GFP_FS))
2390 if (DEBUG_LOCKS_WARN_ON(irqs_disabled()))
2393 mark_held_locks(curr, RECLAIM_FS);
2396 static int mark_irqflags(struct task_struct *curr, struct held_lock *hlock)
2399 * If non-trylock use in a hardirq or softirq context, then
2400 * mark the lock as used in these contexts:
2402 if (!hlock->trylock) {
2404 if (curr->hardirq_context)
2405 if (!mark_lock(curr, hlock,
2406 LOCK_USED_IN_HARDIRQ_READ))
2408 if (curr->softirq_context)
2409 if (!mark_lock(curr, hlock,
2410 LOCK_USED_IN_SOFTIRQ_READ))
2413 if (curr->hardirq_context)
2414 if (!mark_lock(curr, hlock, LOCK_USED_IN_HARDIRQ))
2416 if (curr->softirq_context)
2417 if (!mark_lock(curr, hlock, LOCK_USED_IN_SOFTIRQ))
2421 if (!hlock->hardirqs_off) {
2423 if (!mark_lock(curr, hlock,
2424 LOCK_ENABLED_HARDIRQ_READ))
2426 if (curr->softirqs_enabled)
2427 if (!mark_lock(curr, hlock,
2428 LOCK_ENABLED_SOFTIRQ_READ))
2431 if (!mark_lock(curr, hlock,
2432 LOCK_ENABLED_HARDIRQ))
2434 if (curr->softirqs_enabled)
2435 if (!mark_lock(curr, hlock,
2436 LOCK_ENABLED_SOFTIRQ))
2442 * We reuse the irq context infrastructure more broadly as a general
2443 * context checking code. This tests GFP_FS recursion (a lock taken
2444 * during reclaim for a GFP_FS allocation is held over a GFP_FS
2447 if (!hlock->trylock && (curr->lockdep_reclaim_gfp & __GFP_FS)) {
2449 if (!mark_lock(curr, hlock, LOCK_USED_IN_RECLAIM_FS_READ))
2452 if (!mark_lock(curr, hlock, LOCK_USED_IN_RECLAIM_FS))
2460 static int separate_irq_context(struct task_struct *curr,
2461 struct held_lock *hlock)
2463 unsigned int depth = curr->lockdep_depth;
2466 * Keep track of points where we cross into an interrupt context:
2468 hlock->irq_context = 2*(curr->hardirq_context ? 1 : 0) +
2469 curr->softirq_context;
2471 struct held_lock *prev_hlock;
2473 prev_hlock = curr->held_locks + depth-1;
2475 * If we cross into another context, reset the
2476 * hash key (this also prevents the checking and the
2477 * adding of the dependency to 'prev'):
2479 if (prev_hlock->irq_context != hlock->irq_context)
2488 int mark_lock_irq(struct task_struct *curr, struct held_lock *this,
2489 enum lock_usage_bit new_bit)
2495 static inline int mark_irqflags(struct task_struct *curr,
2496 struct held_lock *hlock)
2501 static inline int separate_irq_context(struct task_struct *curr,
2502 struct held_lock *hlock)
2510 * Mark a lock with a usage bit, and validate the state transition:
2512 static int mark_lock(struct task_struct *curr, struct held_lock *this,
2513 enum lock_usage_bit new_bit)
2515 unsigned int new_mask = 1 << new_bit, ret = 1;
2518 * If already set then do not dirty the cacheline,
2519 * nor do any checks:
2521 if (likely(hlock_class(this)->usage_mask & new_mask))
2527 * Make sure we didnt race:
2529 if (unlikely(hlock_class(this)->usage_mask & new_mask)) {
2534 hlock_class(this)->usage_mask |= new_mask;
2536 if (!save_trace(hlock_class(this)->usage_traces + new_bit))
2540 #define LOCKDEP_STATE(__STATE) \
2541 case LOCK_USED_IN_##__STATE: \
2542 case LOCK_USED_IN_##__STATE##_READ: \
2543 case LOCK_ENABLED_##__STATE: \
2544 case LOCK_ENABLED_##__STATE##_READ:
2545 #include "lockdep_states.h"
2546 #undef LOCKDEP_STATE
2547 ret = mark_lock_irq(curr, this, new_bit);
2552 debug_atomic_dec(&nr_unused_locks);
2555 if (!debug_locks_off_graph_unlock())
2564 * We must printk outside of the graph_lock:
2567 printk("\nmarked lock as {%s}:\n", usage_str[new_bit]);
2569 print_irqtrace_events(curr);
2577 * Initialize a lock instance's lock-class mapping info:
2579 void lockdep_init_map(struct lockdep_map *lock, const char *name,
2580 struct lock_class_key *key, int subclass)
2582 if (unlikely(!debug_locks))
2585 if (DEBUG_LOCKS_WARN_ON(!key))
2587 if (DEBUG_LOCKS_WARN_ON(!name))
2590 * Sanity check, the lock-class key must be persistent:
2592 if (!static_obj(key)) {
2593 printk("BUG: key %p not in .data!\n", key);
2594 DEBUG_LOCKS_WARN_ON(1);
2599 lock->class_cache = NULL;
2600 #ifdef CONFIG_LOCK_STAT
2601 lock->cpu = raw_smp_processor_id();
2604 register_lock_class(lock, subclass, 1);
2606 EXPORT_SYMBOL_GPL(lockdep_init_map);
2609 * This gets called for every mutex_lock*()/spin_lock*() operation.
2610 * We maintain the dependency maps and validate the locking attempt:
2612 static int __lock_acquire(struct lockdep_map *lock, unsigned int subclass,
2613 int trylock, int read, int check, int hardirqs_off,
2614 struct lockdep_map *nest_lock, unsigned long ip)
2616 struct task_struct *curr = current;
2617 struct lock_class *class = NULL;
2618 struct held_lock *hlock;
2619 unsigned int depth, id;
2626 if (unlikely(!debug_locks))
2629 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2632 if (unlikely(subclass >= MAX_LOCKDEP_SUBCLASSES)) {
2634 printk("BUG: MAX_LOCKDEP_SUBCLASSES too low!\n");
2635 printk("turning off the locking correctness validator.\n");
2640 class = lock->class_cache;
2642 * Not cached yet or subclass?
2644 if (unlikely(!class)) {
2645 class = register_lock_class(lock, subclass, 0);
2649 debug_atomic_inc((atomic_t *)&class->ops);
2650 if (very_verbose(class)) {
2651 printk("\nacquire class [%p] %s", class->key, class->name);
2652 if (class->name_version > 1)
2653 printk("#%d", class->name_version);
2659 * Add the lock to the list of currently held locks.
2660 * (we dont increase the depth just yet, up until the
2661 * dependency checks are done)
2663 depth = curr->lockdep_depth;
2664 if (DEBUG_LOCKS_WARN_ON(depth >= MAX_LOCK_DEPTH))
2667 hlock = curr->held_locks + depth;
2668 if (DEBUG_LOCKS_WARN_ON(!class))
2670 hlock->class_idx = class - lock_classes + 1;
2671 hlock->acquire_ip = ip;
2672 hlock->instance = lock;
2673 hlock->nest_lock = nest_lock;
2674 hlock->trylock = trylock;
2676 hlock->check = check;
2677 hlock->hardirqs_off = !!hardirqs_off;
2678 #ifdef CONFIG_LOCK_STAT
2679 hlock->waittime_stamp = 0;
2680 hlock->holdtime_stamp = sched_clock();
2683 if (check == 2 && !mark_irqflags(curr, hlock))
2686 /* mark it as used: */
2687 if (!mark_lock(curr, hlock, LOCK_USED))
2691 * Calculate the chain hash: it's the combined hash of all the
2692 * lock keys along the dependency chain. We save the hash value
2693 * at every step so that we can get the current hash easily
2694 * after unlock. The chain hash is then used to cache dependency
2697 * The 'key ID' is what is the most compact key value to drive
2698 * the hash, not class->key.
2700 id = class - lock_classes;
2701 if (DEBUG_LOCKS_WARN_ON(id >= MAX_LOCKDEP_KEYS))
2704 chain_key = curr->curr_chain_key;
2706 if (DEBUG_LOCKS_WARN_ON(chain_key != 0))
2711 hlock->prev_chain_key = chain_key;
2712 if (separate_irq_context(curr, hlock)) {
2716 chain_key = iterate_chain_key(chain_key, id);
2718 if (!validate_chain(curr, lock, hlock, chain_head, chain_key))
2721 curr->curr_chain_key = chain_key;
2722 curr->lockdep_depth++;
2723 check_chain_key(curr);
2724 #ifdef CONFIG_DEBUG_LOCKDEP
2725 if (unlikely(!debug_locks))
2728 if (unlikely(curr->lockdep_depth >= MAX_LOCK_DEPTH)) {
2730 printk("BUG: MAX_LOCK_DEPTH too low!\n");
2731 printk("turning off the locking correctness validator.\n");
2735 if (unlikely(curr->lockdep_depth > max_lockdep_depth))
2736 max_lockdep_depth = curr->lockdep_depth;
2742 print_unlock_inbalance_bug(struct task_struct *curr, struct lockdep_map *lock,
2745 if (!debug_locks_off())
2747 if (debug_locks_silent)
2750 printk("\n=====================================\n");
2751 printk( "[ BUG: bad unlock balance detected! ]\n");
2752 printk( "-------------------------------------\n");
2753 printk("%s/%d is trying to release lock (",
2754 curr->comm, task_pid_nr(curr));
2755 print_lockdep_cache(lock);
2758 printk("but there are no more locks to release!\n");
2759 printk("\nother info that might help us debug this:\n");
2760 lockdep_print_held_locks(curr);
2762 printk("\nstack backtrace:\n");
2769 * Common debugging checks for both nested and non-nested unlock:
2771 static int check_unlock(struct task_struct *curr, struct lockdep_map *lock,
2774 if (unlikely(!debug_locks))
2776 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2779 if (curr->lockdep_depth <= 0)
2780 return print_unlock_inbalance_bug(curr, lock, ip);
2786 __lock_set_class(struct lockdep_map *lock, const char *name,
2787 struct lock_class_key *key, unsigned int subclass,
2790 struct task_struct *curr = current;
2791 struct held_lock *hlock, *prev_hlock;
2792 struct lock_class *class;
2796 depth = curr->lockdep_depth;
2797 if (DEBUG_LOCKS_WARN_ON(!depth))
2801 for (i = depth-1; i >= 0; i--) {
2802 hlock = curr->held_locks + i;
2804 * We must not cross into another context:
2806 if (prev_hlock && prev_hlock->irq_context != hlock->irq_context)
2808 if (hlock->instance == lock)
2812 return print_unlock_inbalance_bug(curr, lock, ip);
2815 lockdep_init_map(lock, name, key, 0);
2816 class = register_lock_class(lock, subclass, 0);
2817 hlock->class_idx = class - lock_classes + 1;
2819 curr->lockdep_depth = i;
2820 curr->curr_chain_key = hlock->prev_chain_key;
2822 for (; i < depth; i++) {
2823 hlock = curr->held_locks + i;
2824 if (!__lock_acquire(hlock->instance,
2825 hlock_class(hlock)->subclass, hlock->trylock,
2826 hlock->read, hlock->check, hlock->hardirqs_off,
2827 hlock->nest_lock, hlock->acquire_ip))
2831 if (DEBUG_LOCKS_WARN_ON(curr->lockdep_depth != depth))
2837 * Remove the lock to the list of currently held locks in a
2838 * potentially non-nested (out of order) manner. This is a
2839 * relatively rare operation, as all the unlock APIs default
2840 * to nested mode (which uses lock_release()):
2843 lock_release_non_nested(struct task_struct *curr,
2844 struct lockdep_map *lock, unsigned long ip)
2846 struct held_lock *hlock, *prev_hlock;
2851 * Check whether the lock exists in the current stack
2854 depth = curr->lockdep_depth;
2855 if (DEBUG_LOCKS_WARN_ON(!depth))
2859 for (i = depth-1; i >= 0; i--) {
2860 hlock = curr->held_locks + i;
2862 * We must not cross into another context:
2864 if (prev_hlock && prev_hlock->irq_context != hlock->irq_context)
2866 if (hlock->instance == lock)
2870 return print_unlock_inbalance_bug(curr, lock, ip);
2873 lock_release_holdtime(hlock);
2876 * We have the right lock to unlock, 'hlock' points to it.
2877 * Now we remove it from the stack, and add back the other
2878 * entries (if any), recalculating the hash along the way:
2880 curr->lockdep_depth = i;
2881 curr->curr_chain_key = hlock->prev_chain_key;
2883 for (i++; i < depth; i++) {
2884 hlock = curr->held_locks + i;
2885 if (!__lock_acquire(hlock->instance,
2886 hlock_class(hlock)->subclass, hlock->trylock,
2887 hlock->read, hlock->check, hlock->hardirqs_off,
2888 hlock->nest_lock, hlock->acquire_ip))
2892 if (DEBUG_LOCKS_WARN_ON(curr->lockdep_depth != depth - 1))
2898 * Remove the lock to the list of currently held locks - this gets
2899 * called on mutex_unlock()/spin_unlock*() (or on a failed
2900 * mutex_lock_interruptible()). This is done for unlocks that nest
2901 * perfectly. (i.e. the current top of the lock-stack is unlocked)
2903 static int lock_release_nested(struct task_struct *curr,
2904 struct lockdep_map *lock, unsigned long ip)
2906 struct held_lock *hlock;
2910 * Pop off the top of the lock stack:
2912 depth = curr->lockdep_depth - 1;
2913 hlock = curr->held_locks + depth;
2916 * Is the unlock non-nested:
2918 if (hlock->instance != lock)
2919 return lock_release_non_nested(curr, lock, ip);
2920 curr->lockdep_depth--;
2922 if (DEBUG_LOCKS_WARN_ON(!depth && (hlock->prev_chain_key != 0)))
2925 curr->curr_chain_key = hlock->prev_chain_key;
2927 lock_release_holdtime(hlock);
2929 #ifdef CONFIG_DEBUG_LOCKDEP
2930 hlock->prev_chain_key = 0;
2931 hlock->class_idx = 0;
2932 hlock->acquire_ip = 0;
2933 hlock->irq_context = 0;
2939 * Remove the lock to the list of currently held locks - this gets
2940 * called on mutex_unlock()/spin_unlock*() (or on a failed
2941 * mutex_lock_interruptible()). This is done for unlocks that nest
2942 * perfectly. (i.e. the current top of the lock-stack is unlocked)
2945 __lock_release(struct lockdep_map *lock, int nested, unsigned long ip)
2947 struct task_struct *curr = current;
2949 if (!check_unlock(curr, lock, ip))
2953 if (!lock_release_nested(curr, lock, ip))
2956 if (!lock_release_non_nested(curr, lock, ip))
2960 check_chain_key(curr);
2964 * Check whether we follow the irq-flags state precisely:
2966 static void check_flags(unsigned long flags)
2968 #if defined(CONFIG_PROVE_LOCKING) && defined(CONFIG_DEBUG_LOCKDEP) && \
2969 defined(CONFIG_TRACE_IRQFLAGS)
2973 if (irqs_disabled_flags(flags)) {
2974 if (DEBUG_LOCKS_WARN_ON(current->hardirqs_enabled)) {
2975 printk("possible reason: unannotated irqs-off.\n");
2978 if (DEBUG_LOCKS_WARN_ON(!current->hardirqs_enabled)) {
2979 printk("possible reason: unannotated irqs-on.\n");
2984 * We dont accurately track softirq state in e.g.
2985 * hardirq contexts (such as on 4KSTACKS), so only
2986 * check if not in hardirq contexts:
2988 if (!hardirq_count()) {
2989 if (softirq_count())
2990 DEBUG_LOCKS_WARN_ON(current->softirqs_enabled);
2992 DEBUG_LOCKS_WARN_ON(!current->softirqs_enabled);
2996 print_irqtrace_events(current);
3000 void lock_set_class(struct lockdep_map *lock, const char *name,
3001 struct lock_class_key *key, unsigned int subclass,
3004 unsigned long flags;
3006 if (unlikely(current->lockdep_recursion))
3009 raw_local_irq_save(flags);
3010 current->lockdep_recursion = 1;
3012 if (__lock_set_class(lock, name, key, subclass, ip))
3013 check_chain_key(current);
3014 current->lockdep_recursion = 0;
3015 raw_local_irq_restore(flags);
3017 EXPORT_SYMBOL_GPL(lock_set_class);
3020 * We are not always called with irqs disabled - do that here,
3021 * and also avoid lockdep recursion:
3023 void lock_acquire(struct lockdep_map *lock, unsigned int subclass,
3024 int trylock, int read, int check,
3025 struct lockdep_map *nest_lock, unsigned long ip)
3027 unsigned long flags;
3029 if (unlikely(current->lockdep_recursion))
3032 raw_local_irq_save(flags);
3035 current->lockdep_recursion = 1;
3036 __lock_acquire(lock, subclass, trylock, read, check,
3037 irqs_disabled_flags(flags), nest_lock, ip);
3038 current->lockdep_recursion = 0;
3039 raw_local_irq_restore(flags);
3041 EXPORT_SYMBOL_GPL(lock_acquire);
3043 void lock_release(struct lockdep_map *lock, int nested,
3046 unsigned long flags;
3048 if (unlikely(current->lockdep_recursion))
3051 raw_local_irq_save(flags);
3053 current->lockdep_recursion = 1;
3054 __lock_release(lock, nested, ip);
3055 current->lockdep_recursion = 0;
3056 raw_local_irq_restore(flags);
3058 EXPORT_SYMBOL_GPL(lock_release);
3060 void lockdep_set_current_reclaim_state(gfp_t gfp_mask)
3062 current->lockdep_reclaim_gfp = gfp_mask;
3065 void lockdep_clear_current_reclaim_state(void)
3067 current->lockdep_reclaim_gfp = 0;
3070 #ifdef CONFIG_LOCK_STAT
3072 print_lock_contention_bug(struct task_struct *curr, struct lockdep_map *lock,
3075 if (!debug_locks_off())
3077 if (debug_locks_silent)
3080 printk("\n=================================\n");
3081 printk( "[ BUG: bad contention detected! ]\n");
3082 printk( "---------------------------------\n");
3083 printk("%s/%d is trying to contend lock (",
3084 curr->comm, task_pid_nr(curr));
3085 print_lockdep_cache(lock);
3088 printk("but there are no locks held!\n");
3089 printk("\nother info that might help us debug this:\n");
3090 lockdep_print_held_locks(curr);
3092 printk("\nstack backtrace:\n");
3099 __lock_contended(struct lockdep_map *lock, unsigned long ip)
3101 struct task_struct *curr = current;
3102 struct held_lock *hlock, *prev_hlock;
3103 struct lock_class_stats *stats;
3105 int i, contention_point, contending_point;
3107 depth = curr->lockdep_depth;
3108 if (DEBUG_LOCKS_WARN_ON(!depth))
3112 for (i = depth-1; i >= 0; i--) {
3113 hlock = curr->held_locks + i;
3115 * We must not cross into another context:
3117 if (prev_hlock && prev_hlock->irq_context != hlock->irq_context)
3119 if (hlock->instance == lock)
3123 print_lock_contention_bug(curr, lock, ip);
3127 hlock->waittime_stamp = sched_clock();
3129 contention_point = lock_point(hlock_class(hlock)->contention_point, ip);
3130 contending_point = lock_point(hlock_class(hlock)->contending_point,
3133 stats = get_lock_stats(hlock_class(hlock));
3134 if (contention_point < LOCKSTAT_POINTS)
3135 stats->contention_point[contention_point]++;
3136 if (contending_point < LOCKSTAT_POINTS)
3137 stats->contending_point[contending_point]++;
3138 if (lock->cpu != smp_processor_id())
3139 stats->bounces[bounce_contended + !!hlock->read]++;
3140 put_lock_stats(stats);
3144 __lock_acquired(struct lockdep_map *lock, unsigned long ip)
3146 struct task_struct *curr = current;
3147 struct held_lock *hlock, *prev_hlock;
3148 struct lock_class_stats *stats;
3154 depth = curr->lockdep_depth;
3155 if (DEBUG_LOCKS_WARN_ON(!depth))
3159 for (i = depth-1; i >= 0; i--) {
3160 hlock = curr->held_locks + i;
3162 * We must not cross into another context:
3164 if (prev_hlock && prev_hlock->irq_context != hlock->irq_context)
3166 if (hlock->instance == lock)
3170 print_lock_contention_bug(curr, lock, _RET_IP_);
3174 cpu = smp_processor_id();
3175 if (hlock->waittime_stamp) {
3176 now = sched_clock();
3177 waittime = now - hlock->waittime_stamp;
3178 hlock->holdtime_stamp = now;
3181 stats = get_lock_stats(hlock_class(hlock));
3184 lock_time_inc(&stats->read_waittime, waittime);
3186 lock_time_inc(&stats->write_waittime, waittime);
3188 if (lock->cpu != cpu)
3189 stats->bounces[bounce_acquired + !!hlock->read]++;
3190 put_lock_stats(stats);
3196 void lock_contended(struct lockdep_map *lock, unsigned long ip)
3198 unsigned long flags;
3200 if (unlikely(!lock_stat))
3203 if (unlikely(current->lockdep_recursion))
3206 raw_local_irq_save(flags);
3208 current->lockdep_recursion = 1;
3209 __lock_contended(lock, ip);
3210 current->lockdep_recursion = 0;
3211 raw_local_irq_restore(flags);
3213 EXPORT_SYMBOL_GPL(lock_contended);
3215 void lock_acquired(struct lockdep_map *lock, unsigned long ip)
3217 unsigned long flags;
3219 if (unlikely(!lock_stat))
3222 if (unlikely(current->lockdep_recursion))
3225 raw_local_irq_save(flags);
3227 current->lockdep_recursion = 1;
3228 __lock_acquired(lock, ip);
3229 current->lockdep_recursion = 0;
3230 raw_local_irq_restore(flags);
3232 EXPORT_SYMBOL_GPL(lock_acquired);
3236 * Used by the testsuite, sanitize the validator state
3237 * after a simulated failure:
3240 void lockdep_reset(void)
3242 unsigned long flags;
3245 raw_local_irq_save(flags);
3246 current->curr_chain_key = 0;
3247 current->lockdep_depth = 0;
3248 current->lockdep_recursion = 0;
3249 memset(current->held_locks, 0, MAX_LOCK_DEPTH*sizeof(struct held_lock));
3250 nr_hardirq_chains = 0;
3251 nr_softirq_chains = 0;
3252 nr_process_chains = 0;
3254 for (i = 0; i < CHAINHASH_SIZE; i++)
3255 INIT_LIST_HEAD(chainhash_table + i);
3256 raw_local_irq_restore(flags);
3259 static void zap_class(struct lock_class *class)
3264 * Remove all dependencies this lock is
3267 for (i = 0; i < nr_list_entries; i++) {
3268 if (list_entries[i].class == class)
3269 list_del_rcu(&list_entries[i].entry);
3272 * Unhash the class and remove it from the all_lock_classes list:
3274 list_del_rcu(&class->hash_entry);
3275 list_del_rcu(&class->lock_entry);
3280 static inline int within(const void *addr, void *start, unsigned long size)
3282 return addr >= start && addr < start + size;
3285 void lockdep_free_key_range(void *start, unsigned long size)
3287 struct lock_class *class, *next;
3288 struct list_head *head;
3289 unsigned long flags;
3293 raw_local_irq_save(flags);
3294 locked = graph_lock();
3297 * Unhash all classes that were created by this module:
3299 for (i = 0; i < CLASSHASH_SIZE; i++) {
3300 head = classhash_table + i;
3301 if (list_empty(head))
3303 list_for_each_entry_safe(class, next, head, hash_entry) {
3304 if (within(class->key, start, size))
3306 else if (within(class->name, start, size))
3313 raw_local_irq_restore(flags);
3316 void lockdep_reset_lock(struct lockdep_map *lock)
3318 struct lock_class *class, *next;
3319 struct list_head *head;
3320 unsigned long flags;
3324 raw_local_irq_save(flags);
3327 * Remove all classes this lock might have:
3329 for (j = 0; j < MAX_LOCKDEP_SUBCLASSES; j++) {
3331 * If the class exists we look it up and zap it:
3333 class = look_up_lock_class(lock, j);
3338 * Debug check: in the end all mapped classes should
3341 locked = graph_lock();
3342 for (i = 0; i < CLASSHASH_SIZE; i++) {
3343 head = classhash_table + i;
3344 if (list_empty(head))
3346 list_for_each_entry_safe(class, next, head, hash_entry) {
3347 if (unlikely(class == lock->class_cache)) {
3348 if (debug_locks_off_graph_unlock())
3358 raw_local_irq_restore(flags);
3361 void lockdep_init(void)
3366 * Some architectures have their own start_kernel()
3367 * code which calls lockdep_init(), while we also
3368 * call lockdep_init() from the start_kernel() itself,
3369 * and we want to initialize the hashes only once:
3371 if (lockdep_initialized)
3374 for (i = 0; i < CLASSHASH_SIZE; i++)
3375 INIT_LIST_HEAD(classhash_table + i);
3377 for (i = 0; i < CHAINHASH_SIZE; i++)
3378 INIT_LIST_HEAD(chainhash_table + i);
3380 lockdep_initialized = 1;
3383 void __init lockdep_info(void)
3385 printk("Lock dependency validator: Copyright (c) 2006 Red Hat, Inc., Ingo Molnar\n");
3387 printk("... MAX_LOCKDEP_SUBCLASSES: %lu\n", MAX_LOCKDEP_SUBCLASSES);
3388 printk("... MAX_LOCK_DEPTH: %lu\n", MAX_LOCK_DEPTH);
3389 printk("... MAX_LOCKDEP_KEYS: %lu\n", MAX_LOCKDEP_KEYS);
3390 printk("... CLASSHASH_SIZE: %lu\n", CLASSHASH_SIZE);
3391 printk("... MAX_LOCKDEP_ENTRIES: %lu\n", MAX_LOCKDEP_ENTRIES);
3392 printk("... MAX_LOCKDEP_CHAINS: %lu\n", MAX_LOCKDEP_CHAINS);
3393 printk("... CHAINHASH_SIZE: %lu\n", CHAINHASH_SIZE);
3395 printk(" memory used by lock dependency info: %lu kB\n",
3396 (sizeof(struct lock_class) * MAX_LOCKDEP_KEYS +
3397 sizeof(struct list_head) * CLASSHASH_SIZE +
3398 sizeof(struct lock_list) * MAX_LOCKDEP_ENTRIES +
3399 sizeof(struct lock_chain) * MAX_LOCKDEP_CHAINS +
3400 sizeof(struct list_head) * CHAINHASH_SIZE) / 1024);
3402 printk(" per task-struct memory footprint: %lu bytes\n",
3403 sizeof(struct held_lock) * MAX_LOCK_DEPTH);
3405 #ifdef CONFIG_DEBUG_LOCKDEP
3406 if (lockdep_init_error) {
3407 printk("WARNING: lockdep init error! Arch code didn't call lockdep_init() early enough?\n");
3408 printk("Call stack leading to lockdep invocation was:\n");
3409 print_stack_trace(&lockdep_init_trace, 0);
3415 print_freed_lock_bug(struct task_struct *curr, const void *mem_from,
3416 const void *mem_to, struct held_lock *hlock)
3418 if (!debug_locks_off())
3420 if (debug_locks_silent)
3423 printk("\n=========================\n");
3424 printk( "[ BUG: held lock freed! ]\n");
3425 printk( "-------------------------\n");
3426 printk("%s/%d is freeing memory %p-%p, with a lock still held there!\n",
3427 curr->comm, task_pid_nr(curr), mem_from, mem_to-1);
3429 lockdep_print_held_locks(curr);
3431 printk("\nstack backtrace:\n");
3435 static inline int not_in_range(const void* mem_from, unsigned long mem_len,
3436 const void* lock_from, unsigned long lock_len)
3438 return lock_from + lock_len <= mem_from ||
3439 mem_from + mem_len <= lock_from;
3443 * Called when kernel memory is freed (or unmapped), or if a lock
3444 * is destroyed or reinitialized - this code checks whether there is
3445 * any held lock in the memory range of <from> to <to>:
3447 void debug_check_no_locks_freed(const void *mem_from, unsigned long mem_len)
3449 struct task_struct *curr = current;
3450 struct held_lock *hlock;
3451 unsigned long flags;
3454 if (unlikely(!debug_locks))
3457 local_irq_save(flags);
3458 for (i = 0; i < curr->lockdep_depth; i++) {
3459 hlock = curr->held_locks + i;
3461 if (not_in_range(mem_from, mem_len, hlock->instance,
3462 sizeof(*hlock->instance)))
3465 print_freed_lock_bug(curr, mem_from, mem_from + mem_len, hlock);
3468 local_irq_restore(flags);
3470 EXPORT_SYMBOL_GPL(debug_check_no_locks_freed);
3472 static void print_held_locks_bug(struct task_struct *curr)
3474 if (!debug_locks_off())
3476 if (debug_locks_silent)
3479 printk("\n=====================================\n");
3480 printk( "[ BUG: lock held at task exit time! ]\n");
3481 printk( "-------------------------------------\n");
3482 printk("%s/%d is exiting with locks still held!\n",
3483 curr->comm, task_pid_nr(curr));
3484 lockdep_print_held_locks(curr);
3486 printk("\nstack backtrace:\n");
3490 void debug_check_no_locks_held(struct task_struct *task)
3492 if (unlikely(task->lockdep_depth > 0))
3493 print_held_locks_bug(task);
3496 void debug_show_all_locks(void)
3498 struct task_struct *g, *p;
3502 if (unlikely(!debug_locks)) {
3503 printk("INFO: lockdep is turned off.\n");
3506 printk("\nShowing all locks held in the system:\n");
3509 * Here we try to get the tasklist_lock as hard as possible,
3510 * if not successful after 2 seconds we ignore it (but keep
3511 * trying). This is to enable a debug printout even if a
3512 * tasklist_lock-holding task deadlocks or crashes.
3515 if (!read_trylock(&tasklist_lock)) {
3517 printk("hm, tasklist_lock locked, retrying... ");
3520 printk(" #%d", 10-count);
3524 printk(" ignoring it.\n");
3528 printk(KERN_CONT " locked it.\n");
3531 do_each_thread(g, p) {
3533 * It's not reliable to print a task's held locks
3534 * if it's not sleeping (or if it's not the current
3537 if (p->state == TASK_RUNNING && p != current)
3539 if (p->lockdep_depth)
3540 lockdep_print_held_locks(p);
3542 if (read_trylock(&tasklist_lock))
3544 } while_each_thread(g, p);
3547 printk("=============================================\n\n");
3550 read_unlock(&tasklist_lock);
3552 EXPORT_SYMBOL_GPL(debug_show_all_locks);
3555 * Careful: only use this function if you are sure that
3556 * the task cannot run in parallel!
3558 void __debug_show_held_locks(struct task_struct *task)
3560 if (unlikely(!debug_locks)) {
3561 printk("INFO: lockdep is turned off.\n");
3564 lockdep_print_held_locks(task);
3566 EXPORT_SYMBOL_GPL(__debug_show_held_locks);
3568 void debug_show_held_locks(struct task_struct *task)
3570 __debug_show_held_locks(task);
3572 EXPORT_SYMBOL_GPL(debug_show_held_locks);
3574 void lockdep_sys_exit(void)
3576 struct task_struct *curr = current;
3578 if (unlikely(curr->lockdep_depth)) {
3579 if (!debug_locks_off())
3581 printk("\n================================================\n");
3582 printk( "[ BUG: lock held when returning to user space! ]\n");
3583 printk( "------------------------------------------------\n");
3584 printk("%s/%d is leaving the kernel with locks still held!\n",
3585 curr->comm, curr->pid);
3586 lockdep_print_held_locks(curr);