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
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/sched/clock.h>
32 #include <linux/sched/task.h>
33 #include <linux/sched/mm.h>
34 #include <linux/delay.h>
35 #include <linux/module.h>
36 #include <linux/proc_fs.h>
37 #include <linux/seq_file.h>
38 #include <linux/spinlock.h>
39 #include <linux/kallsyms.h>
40 #include <linux/interrupt.h>
41 #include <linux/stacktrace.h>
42 #include <linux/debug_locks.h>
43 #include <linux/irqflags.h>
44 #include <linux/utsname.h>
45 #include <linux/hash.h>
46 #include <linux/ftrace.h>
47 #include <linux/stringify.h>
48 #include <linux/bitmap.h>
49 #include <linux/bitops.h>
50 #include <linux/gfp.h>
51 #include <linux/random.h>
52 #include <linux/jhash.h>
53 #include <linux/nmi.h>
54 #include <linux/rcupdate.h>
56 #include <asm/sections.h>
58 #include "lockdep_internals.h"
60 #define CREATE_TRACE_POINTS
61 #include <trace/events/lock.h>
63 #ifdef CONFIG_PROVE_LOCKING
64 int prove_locking = 1;
65 module_param(prove_locking, int, 0644);
67 #define prove_locking 0
70 #ifdef CONFIG_LOCK_STAT
72 module_param(lock_stat, int, 0644);
78 * lockdep_lock: protects the lockdep graph, the hashes and the
79 * class/list/hash allocators.
81 * This is one of the rare exceptions where it's justified
82 * to use a raw spinlock - we really dont want the spinlock
83 * code to recurse back into the lockdep code...
85 static arch_spinlock_t lockdep_lock = (arch_spinlock_t)__ARCH_SPIN_LOCK_UNLOCKED;
86 static struct task_struct *lockdep_selftest_task_struct;
88 static int graph_lock(void)
90 arch_spin_lock(&lockdep_lock);
92 * Make sure that if another CPU detected a bug while
93 * walking the graph we dont change it (while the other
94 * CPU is busy printing out stuff with the graph lock
98 arch_spin_unlock(&lockdep_lock);
101 /* prevent any recursions within lockdep from causing deadlocks */
102 current->lockdep_recursion++;
106 static inline int graph_unlock(void)
108 if (debug_locks && !arch_spin_is_locked(&lockdep_lock)) {
110 * The lockdep graph lock isn't locked while we expect it to
111 * be, we're confused now, bye!
113 return DEBUG_LOCKS_WARN_ON(1);
116 current->lockdep_recursion--;
117 arch_spin_unlock(&lockdep_lock);
122 * Turn lock debugging off and return with 0 if it was off already,
123 * and also release the graph lock:
125 static inline int debug_locks_off_graph_unlock(void)
127 int ret = debug_locks_off();
129 arch_spin_unlock(&lockdep_lock);
134 unsigned long nr_list_entries;
135 static struct lock_list list_entries[MAX_LOCKDEP_ENTRIES];
136 static DECLARE_BITMAP(list_entries_in_use, MAX_LOCKDEP_ENTRIES);
139 * All data structures here are protected by the global debug_lock.
141 * nr_lock_classes is the number of elements of lock_classes[] that is
144 #define KEYHASH_BITS (MAX_LOCKDEP_KEYS_BITS - 1)
145 #define KEYHASH_SIZE (1UL << KEYHASH_BITS)
146 static struct hlist_head lock_keys_hash[KEYHASH_SIZE];
147 unsigned long nr_lock_classes;
148 #ifndef CONFIG_DEBUG_LOCKDEP
151 struct lock_class lock_classes[MAX_LOCKDEP_KEYS];
153 static inline struct lock_class *hlock_class(struct held_lock *hlock)
155 if (!hlock->class_idx) {
157 * Someone passed in garbage, we give up.
159 DEBUG_LOCKS_WARN_ON(1);
162 return lock_classes + hlock->class_idx - 1;
165 #ifdef CONFIG_LOCK_STAT
166 static DEFINE_PER_CPU(struct lock_class_stats[MAX_LOCKDEP_KEYS], cpu_lock_stats);
168 static inline u64 lockstat_clock(void)
170 return local_clock();
173 static int lock_point(unsigned long points[], unsigned long ip)
177 for (i = 0; i < LOCKSTAT_POINTS; i++) {
178 if (points[i] == 0) {
189 static void lock_time_inc(struct lock_time *lt, u64 time)
194 if (time < lt->min || !lt->nr)
201 static inline void lock_time_add(struct lock_time *src, struct lock_time *dst)
206 if (src->max > dst->max)
209 if (src->min < dst->min || !dst->nr)
212 dst->total += src->total;
216 struct lock_class_stats lock_stats(struct lock_class *class)
218 struct lock_class_stats stats;
221 memset(&stats, 0, sizeof(struct lock_class_stats));
222 for_each_possible_cpu(cpu) {
223 struct lock_class_stats *pcs =
224 &per_cpu(cpu_lock_stats, cpu)[class - lock_classes];
226 for (i = 0; i < ARRAY_SIZE(stats.contention_point); i++)
227 stats.contention_point[i] += pcs->contention_point[i];
229 for (i = 0; i < ARRAY_SIZE(stats.contending_point); i++)
230 stats.contending_point[i] += pcs->contending_point[i];
232 lock_time_add(&pcs->read_waittime, &stats.read_waittime);
233 lock_time_add(&pcs->write_waittime, &stats.write_waittime);
235 lock_time_add(&pcs->read_holdtime, &stats.read_holdtime);
236 lock_time_add(&pcs->write_holdtime, &stats.write_holdtime);
238 for (i = 0; i < ARRAY_SIZE(stats.bounces); i++)
239 stats.bounces[i] += pcs->bounces[i];
245 void clear_lock_stats(struct lock_class *class)
249 for_each_possible_cpu(cpu) {
250 struct lock_class_stats *cpu_stats =
251 &per_cpu(cpu_lock_stats, cpu)[class - lock_classes];
253 memset(cpu_stats, 0, sizeof(struct lock_class_stats));
255 memset(class->contention_point, 0, sizeof(class->contention_point));
256 memset(class->contending_point, 0, sizeof(class->contending_point));
259 static struct lock_class_stats *get_lock_stats(struct lock_class *class)
261 return &this_cpu_ptr(cpu_lock_stats)[class - lock_classes];
264 static void lock_release_holdtime(struct held_lock *hlock)
266 struct lock_class_stats *stats;
272 holdtime = lockstat_clock() - hlock->holdtime_stamp;
274 stats = get_lock_stats(hlock_class(hlock));
276 lock_time_inc(&stats->read_holdtime, holdtime);
278 lock_time_inc(&stats->write_holdtime, holdtime);
281 static inline void lock_release_holdtime(struct held_lock *hlock)
287 * We keep a global list of all lock classes. The list is only accessed with
288 * the lockdep spinlock lock held. free_lock_classes is a list with free
289 * elements. These elements are linked together by the lock_entry member in
292 LIST_HEAD(all_lock_classes);
293 static LIST_HEAD(free_lock_classes);
296 * struct pending_free - information about data structures about to be freed
297 * @zapped: Head of a list with struct lock_class elements.
298 * @lock_chains_being_freed: Bitmap that indicates which lock_chains[] elements
299 * are about to be freed.
301 struct pending_free {
302 struct list_head zapped;
303 DECLARE_BITMAP(lock_chains_being_freed, MAX_LOCKDEP_CHAINS);
307 * struct delayed_free - data structures used for delayed freeing
309 * A data structure for delayed freeing of data structures that may be
310 * accessed by RCU readers at the time these were freed.
312 * @rcu_head: Used to schedule an RCU callback for freeing data structures.
313 * @index: Index of @pf to which freed data structures are added.
314 * @scheduled: Whether or not an RCU callback has been scheduled.
315 * @pf: Array with information about data structures about to be freed.
317 static struct delayed_free {
318 struct rcu_head rcu_head;
321 struct pending_free pf[2];
325 * The lockdep classes are in a hash-table as well, for fast lookup:
327 #define CLASSHASH_BITS (MAX_LOCKDEP_KEYS_BITS - 1)
328 #define CLASSHASH_SIZE (1UL << CLASSHASH_BITS)
329 #define __classhashfn(key) hash_long((unsigned long)key, CLASSHASH_BITS)
330 #define classhashentry(key) (classhash_table + __classhashfn((key)))
332 static struct hlist_head classhash_table[CLASSHASH_SIZE];
335 * We put the lock dependency chains into a hash-table as well, to cache
338 #define CHAINHASH_BITS (MAX_LOCKDEP_CHAINS_BITS-1)
339 #define CHAINHASH_SIZE (1UL << CHAINHASH_BITS)
340 #define __chainhashfn(chain) hash_long(chain, CHAINHASH_BITS)
341 #define chainhashentry(chain) (chainhash_table + __chainhashfn((chain)))
343 static struct hlist_head chainhash_table[CHAINHASH_SIZE];
346 * The hash key of the lock dependency chains is a hash itself too:
347 * it's a hash of all locks taken up to that lock, including that lock.
348 * It's a 64-bit hash, because it's important for the keys to be
351 static inline u64 iterate_chain_key(u64 key, u32 idx)
353 u32 k0 = key, k1 = key >> 32;
355 __jhash_mix(idx, k0, k1); /* Macro that modifies arguments! */
357 return k0 | (u64)k1 << 32;
360 void lockdep_off(void)
362 current->lockdep_recursion++;
364 EXPORT_SYMBOL(lockdep_off);
366 void lockdep_on(void)
368 current->lockdep_recursion--;
370 EXPORT_SYMBOL(lockdep_on);
372 void lockdep_set_selftest_task(struct task_struct *task)
374 lockdep_selftest_task_struct = task;
378 * Debugging switches:
382 #define VERY_VERBOSE 0
385 # define HARDIRQ_VERBOSE 1
386 # define SOFTIRQ_VERBOSE 1
388 # define HARDIRQ_VERBOSE 0
389 # define SOFTIRQ_VERBOSE 0
392 #if VERBOSE || HARDIRQ_VERBOSE || SOFTIRQ_VERBOSE
394 * Quick filtering for interesting events:
396 static int class_filter(struct lock_class *class)
400 if (class->name_version == 1 &&
401 !strcmp(class->name, "lockname"))
403 if (class->name_version == 1 &&
404 !strcmp(class->name, "&struct->lockfield"))
407 /* Filter everything else. 1 would be to allow everything else */
412 static int verbose(struct lock_class *class)
415 return class_filter(class);
421 * Stack-trace: tightly packed array of stack backtrace
422 * addresses. Protected by the graph_lock.
424 unsigned long nr_stack_trace_entries;
425 static unsigned long stack_trace[MAX_STACK_TRACE_ENTRIES];
427 static void print_lockdep_off(const char *bug_msg)
429 printk(KERN_DEBUG "%s\n", bug_msg);
430 printk(KERN_DEBUG "turning off the locking correctness validator.\n");
431 #ifdef CONFIG_LOCK_STAT
432 printk(KERN_DEBUG "Please attach the output of /proc/lock_stat to the bug report\n");
436 static int save_trace(struct stack_trace *trace)
438 trace->nr_entries = 0;
439 trace->max_entries = MAX_STACK_TRACE_ENTRIES - nr_stack_trace_entries;
440 trace->entries = stack_trace + nr_stack_trace_entries;
444 save_stack_trace(trace);
447 * Some daft arches put -1 at the end to indicate its a full trace.
449 * <rant> this is buggy anyway, since it takes a whole extra entry so a
450 * complete trace that maxes out the entries provided will be reported
451 * as incomplete, friggin useless </rant>
453 if (trace->nr_entries != 0 &&
454 trace->entries[trace->nr_entries-1] == ULONG_MAX)
457 trace->max_entries = trace->nr_entries;
459 nr_stack_trace_entries += trace->nr_entries;
461 if (nr_stack_trace_entries >= MAX_STACK_TRACE_ENTRIES-1) {
462 if (!debug_locks_off_graph_unlock())
465 print_lockdep_off("BUG: MAX_STACK_TRACE_ENTRIES too low!");
474 unsigned int nr_hardirq_chains;
475 unsigned int nr_softirq_chains;
476 unsigned int nr_process_chains;
477 unsigned int max_lockdep_depth;
479 #ifdef CONFIG_DEBUG_LOCKDEP
481 * Various lockdep statistics:
483 DEFINE_PER_CPU(struct lockdep_stats, lockdep_stats);
490 #define __USAGE(__STATE) \
491 [LOCK_USED_IN_##__STATE] = "IN-"__stringify(__STATE)"-W", \
492 [LOCK_ENABLED_##__STATE] = __stringify(__STATE)"-ON-W", \
493 [LOCK_USED_IN_##__STATE##_READ] = "IN-"__stringify(__STATE)"-R",\
494 [LOCK_ENABLED_##__STATE##_READ] = __stringify(__STATE)"-ON-R",
496 static const char *usage_str[] =
498 #define LOCKDEP_STATE(__STATE) __USAGE(__STATE)
499 #include "lockdep_states.h"
501 [LOCK_USED] = "INITIAL USE",
504 const char * __get_key_name(struct lockdep_subclass_key *key, char *str)
506 return kallsyms_lookup((unsigned long)key, NULL, NULL, NULL, str);
509 static inline unsigned long lock_flag(enum lock_usage_bit bit)
514 static char get_usage_char(struct lock_class *class, enum lock_usage_bit bit)
518 if (class->usage_mask & lock_flag(bit + 2))
520 if (class->usage_mask & lock_flag(bit)) {
522 if (class->usage_mask & lock_flag(bit + 2))
529 void get_usage_chars(struct lock_class *class, char usage[LOCK_USAGE_CHARS])
533 #define LOCKDEP_STATE(__STATE) \
534 usage[i++] = get_usage_char(class, LOCK_USED_IN_##__STATE); \
535 usage[i++] = get_usage_char(class, LOCK_USED_IN_##__STATE##_READ);
536 #include "lockdep_states.h"
542 static void __print_lock_name(struct lock_class *class)
544 char str[KSYM_NAME_LEN];
549 name = __get_key_name(class->key, str);
550 printk(KERN_CONT "%s", name);
552 printk(KERN_CONT "%s", name);
553 if (class->name_version > 1)
554 printk(KERN_CONT "#%d", class->name_version);
556 printk(KERN_CONT "/%d", class->subclass);
560 static void print_lock_name(struct lock_class *class)
562 char usage[LOCK_USAGE_CHARS];
564 get_usage_chars(class, usage);
566 printk(KERN_CONT " (");
567 __print_lock_name(class);
568 printk(KERN_CONT "){%s}", usage);
571 static void print_lockdep_cache(struct lockdep_map *lock)
574 char str[KSYM_NAME_LEN];
578 name = __get_key_name(lock->key->subkeys, str);
580 printk(KERN_CONT "%s", name);
583 static void print_lock(struct held_lock *hlock)
586 * We can be called locklessly through debug_show_all_locks() so be
587 * extra careful, the hlock might have been released and cleared.
589 unsigned int class_idx = hlock->class_idx;
591 /* Don't re-read hlock->class_idx, can't use READ_ONCE() on bitfields: */
594 if (!class_idx || (class_idx - 1) >= MAX_LOCKDEP_KEYS) {
595 printk(KERN_CONT "<RELEASED>\n");
599 printk(KERN_CONT "%p", hlock->instance);
600 print_lock_name(lock_classes + class_idx - 1);
601 printk(KERN_CONT ", at: %pS\n", (void *)hlock->acquire_ip);
604 static void lockdep_print_held_locks(struct task_struct *p)
606 int i, depth = READ_ONCE(p->lockdep_depth);
609 printk("no locks held by %s/%d.\n", p->comm, task_pid_nr(p));
611 printk("%d lock%s held by %s/%d:\n", depth,
612 depth > 1 ? "s" : "", p->comm, task_pid_nr(p));
614 * It's not reliable to print a task's held locks if it's not sleeping
615 * and it's not the current task.
617 if (p->state == TASK_RUNNING && p != current)
619 for (i = 0; i < depth; i++) {
621 print_lock(p->held_locks + i);
625 static void print_kernel_ident(void)
627 printk("%s %.*s %s\n", init_utsname()->release,
628 (int)strcspn(init_utsname()->version, " "),
629 init_utsname()->version,
633 static int very_verbose(struct lock_class *class)
636 return class_filter(class);
642 * Is this the address of a static object:
645 static int static_obj(const void *obj)
647 unsigned long start = (unsigned long) &_stext,
648 end = (unsigned long) &_end,
649 addr = (unsigned long) obj;
654 if ((addr >= start) && (addr < end))
657 if (arch_is_kernel_data(addr))
661 * in-kernel percpu var?
663 if (is_kernel_percpu_address(addr))
667 * module static or percpu var?
669 return is_module_address(addr) || is_module_percpu_address(addr);
674 * To make lock name printouts unique, we calculate a unique
675 * class->name_version generation counter. The caller must hold the graph
678 static int count_matching_names(struct lock_class *new_class)
680 struct lock_class *class;
683 if (!new_class->name)
686 list_for_each_entry(class, &all_lock_classes, lock_entry) {
687 if (new_class->key - new_class->subclass == class->key)
688 return class->name_version;
689 if (class->name && !strcmp(class->name, new_class->name))
690 count = max(count, class->name_version);
696 static inline struct lock_class *
697 look_up_lock_class(const struct lockdep_map *lock, unsigned int subclass)
699 struct lockdep_subclass_key *key;
700 struct hlist_head *hash_head;
701 struct lock_class *class;
703 if (unlikely(subclass >= MAX_LOCKDEP_SUBCLASSES)) {
706 "BUG: looking up invalid subclass: %u\n", subclass);
708 "turning off the locking correctness validator.\n");
714 * If it is not initialised then it has never been locked,
715 * so it won't be present in the hash table.
717 if (unlikely(!lock->key))
721 * NOTE: the class-key must be unique. For dynamic locks, a static
722 * lock_class_key variable is passed in through the mutex_init()
723 * (or spin_lock_init()) call - which acts as the key. For static
724 * locks we use the lock object itself as the key.
726 BUILD_BUG_ON(sizeof(struct lock_class_key) >
727 sizeof(struct lockdep_map));
729 key = lock->key->subkeys + subclass;
731 hash_head = classhashentry(key);
734 * We do an RCU walk of the hash, see lockdep_free_key_range().
736 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
739 hlist_for_each_entry_rcu(class, hash_head, hash_entry) {
740 if (class->key == key) {
742 * Huh! same key, different name? Did someone trample
743 * on some memory? We're most confused.
745 WARN_ON_ONCE(class->name != lock->name);
754 * Static locks do not have their class-keys yet - for them the key is
755 * the lock object itself. If the lock is in the per cpu area, the
756 * canonical address of the lock (per cpu offset removed) is used.
758 static bool assign_lock_key(struct lockdep_map *lock)
760 unsigned long can_addr, addr = (unsigned long)lock;
764 * lockdep_free_key_range() assumes that struct lock_class_key
765 * objects do not overlap. Since we use the address of lock
766 * objects as class key for static objects, check whether the
767 * size of lock_class_key objects does not exceed the size of
768 * the smallest lock object.
770 BUILD_BUG_ON(sizeof(struct lock_class_key) > sizeof(raw_spinlock_t));
773 if (__is_kernel_percpu_address(addr, &can_addr))
774 lock->key = (void *)can_addr;
775 else if (__is_module_percpu_address(addr, &can_addr))
776 lock->key = (void *)can_addr;
777 else if (static_obj(lock))
778 lock->key = (void *)lock;
780 /* Debug-check: all keys must be persistent! */
782 pr_err("INFO: trying to register non-static key.\n");
783 pr_err("the code is fine but needs lockdep annotation.\n");
784 pr_err("turning off the locking correctness validator.\n");
792 #ifdef CONFIG_DEBUG_LOCKDEP
794 /* Check whether element @e occurs in list @h */
795 static bool in_list(struct list_head *e, struct list_head *h)
799 list_for_each(f, h) {
808 * Check whether entry @e occurs in any of the locks_after or locks_before
811 static bool in_any_class_list(struct list_head *e)
813 struct lock_class *class;
816 for (i = 0; i < ARRAY_SIZE(lock_classes); i++) {
817 class = &lock_classes[i];
818 if (in_list(e, &class->locks_after) ||
819 in_list(e, &class->locks_before))
825 static bool class_lock_list_valid(struct lock_class *c, struct list_head *h)
829 list_for_each_entry(e, h, entry) {
830 if (e->links_to != c) {
831 printk(KERN_INFO "class %s: mismatch for lock entry %ld; class %s <> %s",
833 (unsigned long)(e - list_entries),
834 e->links_to && e->links_to->name ?
835 e->links_to->name : "(?)",
836 e->class && e->class->name ? e->class->name :
844 static u16 chain_hlocks[];
846 static bool check_lock_chain_key(struct lock_chain *chain)
848 #ifdef CONFIG_PROVE_LOCKING
852 for (i = chain->base; i < chain->base + chain->depth; i++)
853 chain_key = iterate_chain_key(chain_key, chain_hlocks[i] + 1);
855 * The 'unsigned long long' casts avoid that a compiler warning
856 * is reported when building tools/lib/lockdep.
858 if (chain->chain_key != chain_key) {
859 printk(KERN_INFO "chain %lld: key %#llx <> %#llx\n",
860 (unsigned long long)(chain - lock_chains),
861 (unsigned long long)chain->chain_key,
862 (unsigned long long)chain_key);
869 static bool in_any_zapped_class_list(struct lock_class *class)
871 struct pending_free *pf;
874 for (i = 0, pf = delayed_free.pf; i < ARRAY_SIZE(delayed_free.pf); i++, pf++) {
875 if (in_list(&class->lock_entry, &pf->zapped))
882 static bool __check_data_structures(void)
884 struct lock_class *class;
885 struct lock_chain *chain;
886 struct hlist_head *head;
890 /* Check whether all classes occur in a lock list. */
891 for (i = 0; i < ARRAY_SIZE(lock_classes); i++) {
892 class = &lock_classes[i];
893 if (!in_list(&class->lock_entry, &all_lock_classes) &&
894 !in_list(&class->lock_entry, &free_lock_classes) &&
895 !in_any_zapped_class_list(class)) {
896 printk(KERN_INFO "class %px/%s is not in any class list\n",
897 class, class->name ? : "(?)");
902 /* Check whether all classes have valid lock lists. */
903 for (i = 0; i < ARRAY_SIZE(lock_classes); i++) {
904 class = &lock_classes[i];
905 if (!class_lock_list_valid(class, &class->locks_before))
907 if (!class_lock_list_valid(class, &class->locks_after))
911 /* Check the chain_key of all lock chains. */
912 for (i = 0; i < ARRAY_SIZE(chainhash_table); i++) {
913 head = chainhash_table + i;
914 hlist_for_each_entry_rcu(chain, head, entry) {
915 if (!check_lock_chain_key(chain))
921 * Check whether all list entries that are in use occur in a class
924 for_each_set_bit(i, list_entries_in_use, ARRAY_SIZE(list_entries)) {
925 e = list_entries + i;
926 if (!in_any_class_list(&e->entry)) {
927 printk(KERN_INFO "list entry %d is not in any class list; class %s <> %s\n",
928 (unsigned int)(e - list_entries),
929 e->class->name ? : "(?)",
930 e->links_to->name ? : "(?)");
936 * Check whether all list entries that are not in use do not occur in
939 for_each_clear_bit(i, list_entries_in_use, ARRAY_SIZE(list_entries)) {
940 e = list_entries + i;
941 if (in_any_class_list(&e->entry)) {
942 printk(KERN_INFO "list entry %d occurs in a class list; class %s <> %s\n",
943 (unsigned int)(e - list_entries),
944 e->class && e->class->name ? e->class->name :
946 e->links_to && e->links_to->name ?
947 e->links_to->name : "(?)");
955 int check_consistency = 0;
956 module_param(check_consistency, int, 0644);
958 static void check_data_structures(void)
960 static bool once = false;
962 if (check_consistency && !once) {
963 if (!__check_data_structures()) {
970 #else /* CONFIG_DEBUG_LOCKDEP */
972 static inline void check_data_structures(void) { }
974 #endif /* CONFIG_DEBUG_LOCKDEP */
977 * Initialize the lock_classes[] array elements, the free_lock_classes list
978 * and also the delayed_free structure.
980 static void init_data_structures_once(void)
982 static bool initialization_happened;
985 if (likely(initialization_happened))
988 initialization_happened = true;
990 init_rcu_head(&delayed_free.rcu_head);
991 INIT_LIST_HEAD(&delayed_free.pf[0].zapped);
992 INIT_LIST_HEAD(&delayed_free.pf[1].zapped);
994 for (i = 0; i < ARRAY_SIZE(lock_classes); i++) {
995 list_add_tail(&lock_classes[i].lock_entry, &free_lock_classes);
996 INIT_LIST_HEAD(&lock_classes[i].locks_after);
997 INIT_LIST_HEAD(&lock_classes[i].locks_before);
1001 static inline struct hlist_head *keyhashentry(const struct lock_class_key *key)
1003 unsigned long hash = hash_long((uintptr_t)key, KEYHASH_BITS);
1005 return lock_keys_hash + hash;
1008 /* Register a dynamically allocated key. */
1009 void lockdep_register_key(struct lock_class_key *key)
1011 struct hlist_head *hash_head;
1012 struct lock_class_key *k;
1013 unsigned long flags;
1015 if (WARN_ON_ONCE(static_obj(key)))
1017 hash_head = keyhashentry(key);
1019 raw_local_irq_save(flags);
1022 hlist_for_each_entry_rcu(k, hash_head, hash_entry) {
1023 if (WARN_ON_ONCE(k == key))
1026 hlist_add_head_rcu(&key->hash_entry, hash_head);
1030 raw_local_irq_restore(flags);
1032 EXPORT_SYMBOL_GPL(lockdep_register_key);
1034 /* Check whether a key has been registered as a dynamic key. */
1035 static bool is_dynamic_key(const struct lock_class_key *key)
1037 struct hlist_head *hash_head;
1038 struct lock_class_key *k;
1041 if (WARN_ON_ONCE(static_obj(key)))
1045 * If lock debugging is disabled lock_keys_hash[] may contain
1046 * pointers to memory that has already been freed. Avoid triggering
1047 * a use-after-free in that case by returning early.
1052 hash_head = keyhashentry(key);
1055 hlist_for_each_entry_rcu(k, hash_head, hash_entry) {
1067 * Register a lock's class in the hash-table, if the class is not present
1068 * yet. Otherwise we look it up. We cache the result in the lock object
1069 * itself, so actual lookup of the hash should be once per lock object.
1071 static struct lock_class *
1072 register_lock_class(struct lockdep_map *lock, unsigned int subclass, int force)
1074 struct lockdep_subclass_key *key;
1075 struct hlist_head *hash_head;
1076 struct lock_class *class;
1078 DEBUG_LOCKS_WARN_ON(!irqs_disabled());
1080 class = look_up_lock_class(lock, subclass);
1082 goto out_set_class_cache;
1085 if (!assign_lock_key(lock))
1087 } else if (!static_obj(lock->key) && !is_dynamic_key(lock->key)) {
1091 key = lock->key->subkeys + subclass;
1092 hash_head = classhashentry(key);
1094 if (!graph_lock()) {
1098 * We have to do the hash-walk again, to avoid races
1101 hlist_for_each_entry_rcu(class, hash_head, hash_entry) {
1102 if (class->key == key)
1103 goto out_unlock_set;
1106 init_data_structures_once();
1108 /* Allocate a new lock class and add it to the hash. */
1109 class = list_first_entry_or_null(&free_lock_classes, typeof(*class),
1112 if (!debug_locks_off_graph_unlock()) {
1116 print_lockdep_off("BUG: MAX_LOCKDEP_KEYS too low!");
1121 debug_atomic_inc(nr_unused_locks);
1123 class->name = lock->name;
1124 class->subclass = subclass;
1125 WARN_ON_ONCE(!list_empty(&class->locks_before));
1126 WARN_ON_ONCE(!list_empty(&class->locks_after));
1127 class->name_version = count_matching_names(class);
1129 * We use RCU's safe list-add method to make
1130 * parallel walking of the hash-list safe:
1132 hlist_add_head_rcu(&class->hash_entry, hash_head);
1134 * Remove the class from the free list and add it to the global list
1137 list_move_tail(&class->lock_entry, &all_lock_classes);
1139 if (verbose(class)) {
1142 printk("\nnew class %px: %s", class->key, class->name);
1143 if (class->name_version > 1)
1144 printk(KERN_CONT "#%d", class->name_version);
1145 printk(KERN_CONT "\n");
1148 if (!graph_lock()) {
1155 out_set_class_cache:
1156 if (!subclass || force)
1157 lock->class_cache[0] = class;
1158 else if (subclass < NR_LOCKDEP_CACHING_CLASSES)
1159 lock->class_cache[subclass] = class;
1162 * Hash collision, did we smoke some? We found a class with a matching
1163 * hash but the subclass -- which is hashed in -- didn't match.
1165 if (DEBUG_LOCKS_WARN_ON(class->subclass != subclass))
1171 #ifdef CONFIG_PROVE_LOCKING
1173 * Allocate a lockdep entry. (assumes the graph_lock held, returns
1174 * with NULL on failure)
1176 static struct lock_list *alloc_list_entry(void)
1178 int idx = find_first_zero_bit(list_entries_in_use,
1179 ARRAY_SIZE(list_entries));
1181 if (idx >= ARRAY_SIZE(list_entries)) {
1182 if (!debug_locks_off_graph_unlock())
1185 print_lockdep_off("BUG: MAX_LOCKDEP_ENTRIES too low!");
1190 __set_bit(idx, list_entries_in_use);
1191 return list_entries + idx;
1195 * Add a new dependency to the head of the list:
1197 static int add_lock_to_list(struct lock_class *this,
1198 struct lock_class *links_to, struct list_head *head,
1199 unsigned long ip, int distance,
1200 struct stack_trace *trace)
1202 struct lock_list *entry;
1204 * Lock not present yet - get a new dependency struct and
1205 * add it to the list:
1207 entry = alloc_list_entry();
1211 entry->class = this;
1212 entry->links_to = links_to;
1213 entry->distance = distance;
1214 entry->trace = *trace;
1216 * Both allocation and removal are done under the graph lock; but
1217 * iteration is under RCU-sched; see look_up_lock_class() and
1218 * lockdep_free_key_range().
1220 list_add_tail_rcu(&entry->entry, head);
1226 * For good efficiency of modular, we use power of 2
1228 #define MAX_CIRCULAR_QUEUE_SIZE 4096UL
1229 #define CQ_MASK (MAX_CIRCULAR_QUEUE_SIZE-1)
1232 * The circular_queue and helpers is used to implement the
1233 * breadth-first search(BFS)algorithem, by which we can build
1234 * the shortest path from the next lock to be acquired to the
1235 * previous held lock if there is a circular between them.
1237 struct circular_queue {
1238 unsigned long element[MAX_CIRCULAR_QUEUE_SIZE];
1239 unsigned int front, rear;
1242 static struct circular_queue lock_cq;
1244 unsigned int max_bfs_queue_depth;
1246 static unsigned int lockdep_dependency_gen_id;
1248 static inline void __cq_init(struct circular_queue *cq)
1250 cq->front = cq->rear = 0;
1251 lockdep_dependency_gen_id++;
1254 static inline int __cq_empty(struct circular_queue *cq)
1256 return (cq->front == cq->rear);
1259 static inline int __cq_full(struct circular_queue *cq)
1261 return ((cq->rear + 1) & CQ_MASK) == cq->front;
1264 static inline int __cq_enqueue(struct circular_queue *cq, unsigned long elem)
1269 cq->element[cq->rear] = elem;
1270 cq->rear = (cq->rear + 1) & CQ_MASK;
1274 static inline int __cq_dequeue(struct circular_queue *cq, unsigned long *elem)
1279 *elem = cq->element[cq->front];
1280 cq->front = (cq->front + 1) & CQ_MASK;
1284 static inline unsigned int __cq_get_elem_count(struct circular_queue *cq)
1286 return (cq->rear - cq->front) & CQ_MASK;
1289 static inline void mark_lock_accessed(struct lock_list *lock,
1290 struct lock_list *parent)
1294 nr = lock - list_entries;
1295 WARN_ON(nr >= ARRAY_SIZE(list_entries)); /* Out-of-bounds, input fail */
1296 lock->parent = parent;
1297 lock->class->dep_gen_id = lockdep_dependency_gen_id;
1300 static inline unsigned long lock_accessed(struct lock_list *lock)
1304 nr = lock - list_entries;
1305 WARN_ON(nr >= ARRAY_SIZE(list_entries)); /* Out-of-bounds, input fail */
1306 return lock->class->dep_gen_id == lockdep_dependency_gen_id;
1309 static inline struct lock_list *get_lock_parent(struct lock_list *child)
1311 return child->parent;
1314 static inline int get_lock_depth(struct lock_list *child)
1317 struct lock_list *parent;
1319 while ((parent = get_lock_parent(child))) {
1326 static int __bfs(struct lock_list *source_entry,
1328 int (*match)(struct lock_list *entry, void *data),
1329 struct lock_list **target_entry,
1332 struct lock_list *entry;
1333 struct list_head *head;
1334 struct circular_queue *cq = &lock_cq;
1337 if (match(source_entry, data)) {
1338 *target_entry = source_entry;
1344 head = &source_entry->class->locks_after;
1346 head = &source_entry->class->locks_before;
1348 if (list_empty(head))
1352 __cq_enqueue(cq, (unsigned long)source_entry);
1354 while (!__cq_empty(cq)) {
1355 struct lock_list *lock;
1357 __cq_dequeue(cq, (unsigned long *)&lock);
1365 head = &lock->class->locks_after;
1367 head = &lock->class->locks_before;
1369 DEBUG_LOCKS_WARN_ON(!irqs_disabled());
1371 list_for_each_entry_rcu(entry, head, entry) {
1372 if (!lock_accessed(entry)) {
1373 unsigned int cq_depth;
1374 mark_lock_accessed(entry, lock);
1375 if (match(entry, data)) {
1376 *target_entry = entry;
1381 if (__cq_enqueue(cq, (unsigned long)entry)) {
1385 cq_depth = __cq_get_elem_count(cq);
1386 if (max_bfs_queue_depth < cq_depth)
1387 max_bfs_queue_depth = cq_depth;
1395 static inline int __bfs_forwards(struct lock_list *src_entry,
1397 int (*match)(struct lock_list *entry, void *data),
1398 struct lock_list **target_entry)
1400 return __bfs(src_entry, data, match, target_entry, 1);
1404 static inline int __bfs_backwards(struct lock_list *src_entry,
1406 int (*match)(struct lock_list *entry, void *data),
1407 struct lock_list **target_entry)
1409 return __bfs(src_entry, data, match, target_entry, 0);
1414 * Recursive, forwards-direction lock-dependency checking, used for
1415 * both noncyclic checking and for hardirq-unsafe/softirq-unsafe
1420 * Print a dependency chain entry (this is only done when a deadlock
1421 * has been detected):
1424 print_circular_bug_entry(struct lock_list *target, int depth)
1426 if (debug_locks_silent)
1428 printk("\n-> #%u", depth);
1429 print_lock_name(target->class);
1430 printk(KERN_CONT ":\n");
1431 print_stack_trace(&target->trace, 6);
1437 print_circular_lock_scenario(struct held_lock *src,
1438 struct held_lock *tgt,
1439 struct lock_list *prt)
1441 struct lock_class *source = hlock_class(src);
1442 struct lock_class *target = hlock_class(tgt);
1443 struct lock_class *parent = prt->class;
1446 * A direct locking problem where unsafe_class lock is taken
1447 * directly by safe_class lock, then all we need to show
1448 * is the deadlock scenario, as it is obvious that the
1449 * unsafe lock is taken under the safe lock.
1451 * But if there is a chain instead, where the safe lock takes
1452 * an intermediate lock (middle_class) where this lock is
1453 * not the same as the safe lock, then the lock chain is
1454 * used to describe the problem. Otherwise we would need
1455 * to show a different CPU case for each link in the chain
1456 * from the safe_class lock to the unsafe_class lock.
1458 if (parent != source) {
1459 printk("Chain exists of:\n ");
1460 __print_lock_name(source);
1461 printk(KERN_CONT " --> ");
1462 __print_lock_name(parent);
1463 printk(KERN_CONT " --> ");
1464 __print_lock_name(target);
1465 printk(KERN_CONT "\n\n");
1468 printk(" Possible unsafe locking scenario:\n\n");
1469 printk(" CPU0 CPU1\n");
1470 printk(" ---- ----\n");
1472 __print_lock_name(target);
1473 printk(KERN_CONT ");\n");
1475 __print_lock_name(parent);
1476 printk(KERN_CONT ");\n");
1478 __print_lock_name(target);
1479 printk(KERN_CONT ");\n");
1481 __print_lock_name(source);
1482 printk(KERN_CONT ");\n");
1483 printk("\n *** DEADLOCK ***\n\n");
1487 * When a circular dependency is detected, print the
1491 print_circular_bug_header(struct lock_list *entry, unsigned int depth,
1492 struct held_lock *check_src,
1493 struct held_lock *check_tgt)
1495 struct task_struct *curr = current;
1497 if (debug_locks_silent)
1501 pr_warn("======================================================\n");
1502 pr_warn("WARNING: possible circular locking dependency detected\n");
1503 print_kernel_ident();
1504 pr_warn("------------------------------------------------------\n");
1505 pr_warn("%s/%d is trying to acquire lock:\n",
1506 curr->comm, task_pid_nr(curr));
1507 print_lock(check_src);
1509 pr_warn("\nbut task is already holding lock:\n");
1511 print_lock(check_tgt);
1512 pr_warn("\nwhich lock already depends on the new lock.\n\n");
1513 pr_warn("\nthe existing dependency chain (in reverse order) is:\n");
1515 print_circular_bug_entry(entry, depth);
1520 static inline int class_equal(struct lock_list *entry, void *data)
1522 return entry->class == data;
1525 static noinline int print_circular_bug(struct lock_list *this,
1526 struct lock_list *target,
1527 struct held_lock *check_src,
1528 struct held_lock *check_tgt,
1529 struct stack_trace *trace)
1531 struct task_struct *curr = current;
1532 struct lock_list *parent;
1533 struct lock_list *first_parent;
1536 if (!debug_locks_off_graph_unlock() || debug_locks_silent)
1539 if (!save_trace(&this->trace))
1542 depth = get_lock_depth(target);
1544 print_circular_bug_header(target, depth, check_src, check_tgt);
1546 parent = get_lock_parent(target);
1547 first_parent = parent;
1550 print_circular_bug_entry(parent, --depth);
1551 parent = get_lock_parent(parent);
1554 printk("\nother info that might help us debug this:\n\n");
1555 print_circular_lock_scenario(check_src, check_tgt,
1558 lockdep_print_held_locks(curr);
1560 printk("\nstack backtrace:\n");
1566 static noinline int print_bfs_bug(int ret)
1568 if (!debug_locks_off_graph_unlock())
1572 * Breadth-first-search failed, graph got corrupted?
1574 WARN(1, "lockdep bfs error:%d\n", ret);
1579 static int noop_count(struct lock_list *entry, void *data)
1581 (*(unsigned long *)data)++;
1585 static unsigned long __lockdep_count_forward_deps(struct lock_list *this)
1587 unsigned long count = 0;
1588 struct lock_list *uninitialized_var(target_entry);
1590 __bfs_forwards(this, (void *)&count, noop_count, &target_entry);
1594 unsigned long lockdep_count_forward_deps(struct lock_class *class)
1596 unsigned long ret, flags;
1597 struct lock_list this;
1602 raw_local_irq_save(flags);
1603 arch_spin_lock(&lockdep_lock);
1604 ret = __lockdep_count_forward_deps(&this);
1605 arch_spin_unlock(&lockdep_lock);
1606 raw_local_irq_restore(flags);
1611 static unsigned long __lockdep_count_backward_deps(struct lock_list *this)
1613 unsigned long count = 0;
1614 struct lock_list *uninitialized_var(target_entry);
1616 __bfs_backwards(this, (void *)&count, noop_count, &target_entry);
1621 unsigned long lockdep_count_backward_deps(struct lock_class *class)
1623 unsigned long ret, flags;
1624 struct lock_list this;
1629 raw_local_irq_save(flags);
1630 arch_spin_lock(&lockdep_lock);
1631 ret = __lockdep_count_backward_deps(&this);
1632 arch_spin_unlock(&lockdep_lock);
1633 raw_local_irq_restore(flags);
1639 * Prove that the dependency graph starting at <entry> can not
1640 * lead to <target>. Print an error and return 0 if it does.
1643 check_noncircular(struct lock_list *root, struct lock_class *target,
1644 struct lock_list **target_entry)
1648 debug_atomic_inc(nr_cyclic_checks);
1650 result = __bfs_forwards(root, target, class_equal, target_entry);
1656 check_redundant(struct lock_list *root, struct lock_class *target,
1657 struct lock_list **target_entry)
1661 debug_atomic_inc(nr_redundant_checks);
1663 result = __bfs_forwards(root, target, class_equal, target_entry);
1668 #if defined(CONFIG_TRACE_IRQFLAGS) && defined(CONFIG_PROVE_LOCKING)
1670 * Forwards and backwards subgraph searching, for the purposes of
1671 * proving that two subgraphs can be connected by a new dependency
1672 * without creating any illegal irq-safe -> irq-unsafe lock dependency.
1675 static inline int usage_match(struct lock_list *entry, void *bit)
1677 return entry->class->usage_mask & (1 << (enum lock_usage_bit)bit);
1683 * Find a node in the forwards-direction dependency sub-graph starting
1684 * at @root->class that matches @bit.
1686 * Return 0 if such a node exists in the subgraph, and put that node
1687 * into *@target_entry.
1689 * Return 1 otherwise and keep *@target_entry unchanged.
1690 * Return <0 on error.
1693 find_usage_forwards(struct lock_list *root, enum lock_usage_bit bit,
1694 struct lock_list **target_entry)
1698 debug_atomic_inc(nr_find_usage_forwards_checks);
1700 result = __bfs_forwards(root, (void *)bit, usage_match, target_entry);
1706 * Find a node in the backwards-direction dependency sub-graph starting
1707 * at @root->class that matches @bit.
1709 * Return 0 if such a node exists in the subgraph, and put that node
1710 * into *@target_entry.
1712 * Return 1 otherwise and keep *@target_entry unchanged.
1713 * Return <0 on error.
1716 find_usage_backwards(struct lock_list *root, enum lock_usage_bit bit,
1717 struct lock_list **target_entry)
1721 debug_atomic_inc(nr_find_usage_backwards_checks);
1723 result = __bfs_backwards(root, (void *)bit, usage_match, target_entry);
1728 static void print_lock_class_header(struct lock_class *class, int depth)
1732 printk("%*s->", depth, "");
1733 print_lock_name(class);
1734 #ifdef CONFIG_DEBUG_LOCKDEP
1735 printk(KERN_CONT " ops: %lu", debug_class_ops_read(class));
1737 printk(KERN_CONT " {\n");
1739 for (bit = 0; bit < LOCK_USAGE_STATES; bit++) {
1740 if (class->usage_mask & (1 << bit)) {
1743 len += printk("%*s %s", depth, "", usage_str[bit]);
1744 len += printk(KERN_CONT " at:\n");
1745 print_stack_trace(class->usage_traces + bit, len);
1748 printk("%*s }\n", depth, "");
1750 printk("%*s ... key at: [<%px>] %pS\n",
1751 depth, "", class->key, class->key);
1755 * printk the shortest lock dependencies from @start to @end in reverse order:
1758 print_shortest_lock_dependencies(struct lock_list *leaf,
1759 struct lock_list *root)
1761 struct lock_list *entry = leaf;
1764 /*compute depth from generated tree by BFS*/
1765 depth = get_lock_depth(leaf);
1768 print_lock_class_header(entry->class, depth);
1769 printk("%*s ... acquired at:\n", depth, "");
1770 print_stack_trace(&entry->trace, 2);
1773 if (depth == 0 && (entry != root)) {
1774 printk("lockdep:%s bad path found in chain graph\n", __func__);
1778 entry = get_lock_parent(entry);
1780 } while (entry && (depth >= 0));
1786 print_irq_lock_scenario(struct lock_list *safe_entry,
1787 struct lock_list *unsafe_entry,
1788 struct lock_class *prev_class,
1789 struct lock_class *next_class)
1791 struct lock_class *safe_class = safe_entry->class;
1792 struct lock_class *unsafe_class = unsafe_entry->class;
1793 struct lock_class *middle_class = prev_class;
1795 if (middle_class == safe_class)
1796 middle_class = next_class;
1799 * A direct locking problem where unsafe_class lock is taken
1800 * directly by safe_class lock, then all we need to show
1801 * is the deadlock scenario, as it is obvious that the
1802 * unsafe lock is taken under the safe lock.
1804 * But if there is a chain instead, where the safe lock takes
1805 * an intermediate lock (middle_class) where this lock is
1806 * not the same as the safe lock, then the lock chain is
1807 * used to describe the problem. Otherwise we would need
1808 * to show a different CPU case for each link in the chain
1809 * from the safe_class lock to the unsafe_class lock.
1811 if (middle_class != unsafe_class) {
1812 printk("Chain exists of:\n ");
1813 __print_lock_name(safe_class);
1814 printk(KERN_CONT " --> ");
1815 __print_lock_name(middle_class);
1816 printk(KERN_CONT " --> ");
1817 __print_lock_name(unsafe_class);
1818 printk(KERN_CONT "\n\n");
1821 printk(" Possible interrupt unsafe locking scenario:\n\n");
1822 printk(" CPU0 CPU1\n");
1823 printk(" ---- ----\n");
1825 __print_lock_name(unsafe_class);
1826 printk(KERN_CONT ");\n");
1827 printk(" local_irq_disable();\n");
1829 __print_lock_name(safe_class);
1830 printk(KERN_CONT ");\n");
1832 __print_lock_name(middle_class);
1833 printk(KERN_CONT ");\n");
1834 printk(" <Interrupt>\n");
1836 __print_lock_name(safe_class);
1837 printk(KERN_CONT ");\n");
1838 printk("\n *** DEADLOCK ***\n\n");
1842 print_bad_irq_dependency(struct task_struct *curr,
1843 struct lock_list *prev_root,
1844 struct lock_list *next_root,
1845 struct lock_list *backwards_entry,
1846 struct lock_list *forwards_entry,
1847 struct held_lock *prev,
1848 struct held_lock *next,
1849 enum lock_usage_bit bit1,
1850 enum lock_usage_bit bit2,
1851 const char *irqclass)
1853 if (!debug_locks_off_graph_unlock() || debug_locks_silent)
1857 pr_warn("=====================================================\n");
1858 pr_warn("WARNING: %s-safe -> %s-unsafe lock order detected\n",
1859 irqclass, irqclass);
1860 print_kernel_ident();
1861 pr_warn("-----------------------------------------------------\n");
1862 pr_warn("%s/%d [HC%u[%lu]:SC%u[%lu]:HE%u:SE%u] is trying to acquire:\n",
1863 curr->comm, task_pid_nr(curr),
1864 curr->hardirq_context, hardirq_count() >> HARDIRQ_SHIFT,
1865 curr->softirq_context, softirq_count() >> SOFTIRQ_SHIFT,
1866 curr->hardirqs_enabled,
1867 curr->softirqs_enabled);
1870 pr_warn("\nand this task is already holding:\n");
1872 pr_warn("which would create a new lock dependency:\n");
1873 print_lock_name(hlock_class(prev));
1875 print_lock_name(hlock_class(next));
1878 pr_warn("\nbut this new dependency connects a %s-irq-safe lock:\n",
1880 print_lock_name(backwards_entry->class);
1881 pr_warn("\n... which became %s-irq-safe at:\n", irqclass);
1883 print_stack_trace(backwards_entry->class->usage_traces + bit1, 1);
1885 pr_warn("\nto a %s-irq-unsafe lock:\n", irqclass);
1886 print_lock_name(forwards_entry->class);
1887 pr_warn("\n... which became %s-irq-unsafe at:\n", irqclass);
1890 print_stack_trace(forwards_entry->class->usage_traces + bit2, 1);
1892 pr_warn("\nother info that might help us debug this:\n\n");
1893 print_irq_lock_scenario(backwards_entry, forwards_entry,
1894 hlock_class(prev), hlock_class(next));
1896 lockdep_print_held_locks(curr);
1898 pr_warn("\nthe dependencies between %s-irq-safe lock and the holding lock:\n", irqclass);
1899 if (!save_trace(&prev_root->trace))
1901 print_shortest_lock_dependencies(backwards_entry, prev_root);
1903 pr_warn("\nthe dependencies between the lock to be acquired");
1904 pr_warn(" and %s-irq-unsafe lock:\n", irqclass);
1905 if (!save_trace(&next_root->trace))
1907 print_shortest_lock_dependencies(forwards_entry, next_root);
1909 pr_warn("\nstack backtrace:\n");
1916 check_usage(struct task_struct *curr, struct held_lock *prev,
1917 struct held_lock *next, enum lock_usage_bit bit_backwards,
1918 enum lock_usage_bit bit_forwards, const char *irqclass)
1921 struct lock_list this, that;
1922 struct lock_list *uninitialized_var(target_entry);
1923 struct lock_list *uninitialized_var(target_entry1);
1927 this.class = hlock_class(prev);
1928 ret = find_usage_backwards(&this, bit_backwards, &target_entry);
1930 return print_bfs_bug(ret);
1935 that.class = hlock_class(next);
1936 ret = find_usage_forwards(&that, bit_forwards, &target_entry1);
1938 return print_bfs_bug(ret);
1942 return print_bad_irq_dependency(curr, &this, &that,
1943 target_entry, target_entry1,
1945 bit_backwards, bit_forwards, irqclass);
1948 static const char *state_names[] = {
1949 #define LOCKDEP_STATE(__STATE) \
1950 __stringify(__STATE),
1951 #include "lockdep_states.h"
1952 #undef LOCKDEP_STATE
1955 static const char *state_rnames[] = {
1956 #define LOCKDEP_STATE(__STATE) \
1957 __stringify(__STATE)"-READ",
1958 #include "lockdep_states.h"
1959 #undef LOCKDEP_STATE
1962 static inline const char *state_name(enum lock_usage_bit bit)
1964 return (bit & LOCK_USAGE_READ_MASK) ? state_rnames[bit >> 2] : state_names[bit >> 2];
1967 static int exclusive_bit(int new_bit)
1969 int state = new_bit & LOCK_USAGE_STATE_MASK;
1970 int dir = new_bit & LOCK_USAGE_DIR_MASK;
1973 * keep state, bit flip the direction and strip read.
1975 return state | (dir ^ LOCK_USAGE_DIR_MASK);
1978 static int check_irq_usage(struct task_struct *curr, struct held_lock *prev,
1979 struct held_lock *next, enum lock_usage_bit bit)
1982 * Prove that the new dependency does not connect a hardirq-safe
1983 * lock with a hardirq-unsafe lock - to achieve this we search
1984 * the backwards-subgraph starting at <prev>, and the
1985 * forwards-subgraph starting at <next>:
1987 if (!check_usage(curr, prev, next, bit,
1988 exclusive_bit(bit), state_name(bit)))
1994 * Prove that the new dependency does not connect a hardirq-safe-read
1995 * lock with a hardirq-unsafe lock - to achieve this we search
1996 * the backwards-subgraph starting at <prev>, and the
1997 * forwards-subgraph starting at <next>:
1999 if (!check_usage(curr, prev, next, bit,
2000 exclusive_bit(bit), state_name(bit)))
2007 check_prev_add_irq(struct task_struct *curr, struct held_lock *prev,
2008 struct held_lock *next)
2010 #define LOCKDEP_STATE(__STATE) \
2011 if (!check_irq_usage(curr, prev, next, LOCK_USED_IN_##__STATE)) \
2013 #include "lockdep_states.h"
2014 #undef LOCKDEP_STATE
2019 static void inc_chains(void)
2021 if (current->hardirq_context)
2022 nr_hardirq_chains++;
2024 if (current->softirq_context)
2025 nr_softirq_chains++;
2027 nr_process_chains++;
2034 check_prev_add_irq(struct task_struct *curr, struct held_lock *prev,
2035 struct held_lock *next)
2040 static inline void inc_chains(void)
2042 nr_process_chains++;
2048 print_deadlock_scenario(struct held_lock *nxt,
2049 struct held_lock *prv)
2051 struct lock_class *next = hlock_class(nxt);
2052 struct lock_class *prev = hlock_class(prv);
2054 printk(" Possible unsafe locking scenario:\n\n");
2058 __print_lock_name(prev);
2059 printk(KERN_CONT ");\n");
2061 __print_lock_name(next);
2062 printk(KERN_CONT ");\n");
2063 printk("\n *** DEADLOCK ***\n\n");
2064 printk(" May be due to missing lock nesting notation\n\n");
2068 print_deadlock_bug(struct task_struct *curr, struct held_lock *prev,
2069 struct held_lock *next)
2071 if (!debug_locks_off_graph_unlock() || debug_locks_silent)
2075 pr_warn("============================================\n");
2076 pr_warn("WARNING: possible recursive locking detected\n");
2077 print_kernel_ident();
2078 pr_warn("--------------------------------------------\n");
2079 pr_warn("%s/%d is trying to acquire lock:\n",
2080 curr->comm, task_pid_nr(curr));
2082 pr_warn("\nbut task is already holding lock:\n");
2085 pr_warn("\nother info that might help us debug this:\n");
2086 print_deadlock_scenario(next, prev);
2087 lockdep_print_held_locks(curr);
2089 pr_warn("\nstack backtrace:\n");
2096 * Check whether we are holding such a class already.
2098 * (Note that this has to be done separately, because the graph cannot
2099 * detect such classes of deadlocks.)
2101 * Returns: 0 on deadlock detected, 1 on OK, 2 on recursive read
2104 check_deadlock(struct task_struct *curr, struct held_lock *next,
2105 struct lockdep_map *next_instance, int read)
2107 struct held_lock *prev;
2108 struct held_lock *nest = NULL;
2111 for (i = 0; i < curr->lockdep_depth; i++) {
2112 prev = curr->held_locks + i;
2114 if (prev->instance == next->nest_lock)
2117 if (hlock_class(prev) != hlock_class(next))
2121 * Allow read-after-read recursion of the same
2122 * lock class (i.e. read_lock(lock)+read_lock(lock)):
2124 if ((read == 2) && prev->read)
2128 * We're holding the nest_lock, which serializes this lock's
2129 * nesting behaviour.
2134 return print_deadlock_bug(curr, prev, next);
2140 * There was a chain-cache miss, and we are about to add a new dependency
2141 * to a previous lock. We recursively validate the following rules:
2143 * - would the adding of the <prev> -> <next> dependency create a
2144 * circular dependency in the graph? [== circular deadlock]
2146 * - does the new prev->next dependency connect any hardirq-safe lock
2147 * (in the full backwards-subgraph starting at <prev>) with any
2148 * hardirq-unsafe lock (in the full forwards-subgraph starting at
2149 * <next>)? [== illegal lock inversion with hardirq contexts]
2151 * - does the new prev->next dependency connect any softirq-safe lock
2152 * (in the full backwards-subgraph starting at <prev>) with any
2153 * softirq-unsafe lock (in the full forwards-subgraph starting at
2154 * <next>)? [== illegal lock inversion with softirq contexts]
2156 * any of these scenarios could lead to a deadlock.
2158 * Then if all the validations pass, we add the forwards and backwards
2162 check_prev_add(struct task_struct *curr, struct held_lock *prev,
2163 struct held_lock *next, int distance, struct stack_trace *trace,
2164 int (*save)(struct stack_trace *trace))
2166 struct lock_list *uninitialized_var(target_entry);
2167 struct lock_list *entry;
2168 struct lock_list this;
2171 if (!hlock_class(prev)->key || !hlock_class(next)->key) {
2173 * The warning statements below may trigger a use-after-free
2174 * of the class name. It is better to trigger a use-after free
2175 * and to have the class name most of the time instead of not
2176 * having the class name available.
2178 WARN_ONCE(!debug_locks_silent && !hlock_class(prev)->key,
2179 "Detected use-after-free of lock class %px/%s\n",
2181 hlock_class(prev)->name);
2182 WARN_ONCE(!debug_locks_silent && !hlock_class(next)->key,
2183 "Detected use-after-free of lock class %px/%s\n",
2185 hlock_class(next)->name);
2190 * Prove that the new <prev> -> <next> dependency would not
2191 * create a circular dependency in the graph. (We do this by
2192 * forward-recursing into the graph starting at <next>, and
2193 * checking whether we can reach <prev>.)
2195 * We are using global variables to control the recursion, to
2196 * keep the stackframe size of the recursive functions low:
2198 this.class = hlock_class(next);
2200 ret = check_noncircular(&this, hlock_class(prev), &target_entry);
2201 if (unlikely(!ret)) {
2202 if (!trace->entries) {
2204 * If @save fails here, the printing might trigger
2205 * a WARN but because of the !nr_entries it should
2206 * not do bad things.
2210 return print_circular_bug(&this, target_entry, next, prev, trace);
2212 else if (unlikely(ret < 0))
2213 return print_bfs_bug(ret);
2215 if (!check_prev_add_irq(curr, prev, next))
2219 * For recursive read-locks we do all the dependency checks,
2220 * but we dont store read-triggered dependencies (only
2221 * write-triggered dependencies). This ensures that only the
2222 * write-side dependencies matter, and that if for example a
2223 * write-lock never takes any other locks, then the reads are
2224 * equivalent to a NOP.
2226 if (next->read == 2 || prev->read == 2)
2229 * Is the <prev> -> <next> dependency already present?
2231 * (this may occur even though this is a new chain: consider
2232 * e.g. the L1 -> L2 -> L3 -> L4 and the L5 -> L1 -> L2 -> L3
2233 * chains - the second one will be new, but L1 already has
2234 * L2 added to its dependency list, due to the first chain.)
2236 list_for_each_entry(entry, &hlock_class(prev)->locks_after, entry) {
2237 if (entry->class == hlock_class(next)) {
2239 entry->distance = 1;
2245 * Is the <prev> -> <next> link redundant?
2247 this.class = hlock_class(prev);
2249 ret = check_redundant(&this, hlock_class(next), &target_entry);
2251 debug_atomic_inc(nr_redundant);
2255 return print_bfs_bug(ret);
2258 if (!trace->entries && !save(trace))
2262 * Ok, all validations passed, add the new lock
2263 * to the previous lock's dependency list:
2265 ret = add_lock_to_list(hlock_class(next), hlock_class(prev),
2266 &hlock_class(prev)->locks_after,
2267 next->acquire_ip, distance, trace);
2272 ret = add_lock_to_list(hlock_class(prev), hlock_class(next),
2273 &hlock_class(next)->locks_before,
2274 next->acquire_ip, distance, trace);
2282 * Add the dependency to all directly-previous locks that are 'relevant'.
2283 * The ones that are relevant are (in increasing distance from curr):
2284 * all consecutive trylock entries and the final non-trylock entry - or
2285 * the end of this context's lock-chain - whichever comes first.
2288 check_prevs_add(struct task_struct *curr, struct held_lock *next)
2290 int depth = curr->lockdep_depth;
2291 struct held_lock *hlock;
2292 struct stack_trace trace = {
2302 * Depth must not be zero for a non-head lock:
2307 * At least two relevant locks must exist for this
2310 if (curr->held_locks[depth].irq_context !=
2311 curr->held_locks[depth-1].irq_context)
2315 int distance = curr->lockdep_depth - depth + 1;
2316 hlock = curr->held_locks + depth - 1;
2319 * Only non-recursive-read entries get new dependencies
2322 if (hlock->read != 2 && hlock->check) {
2323 int ret = check_prev_add(curr, hlock, next, distance, &trace, save_trace);
2328 * Stop after the first non-trylock entry,
2329 * as non-trylock entries have added their
2330 * own direct dependencies already, so this
2331 * lock is connected to them indirectly:
2333 if (!hlock->trylock)
2339 * End of lock-stack?
2344 * Stop the search if we cross into another context:
2346 if (curr->held_locks[depth].irq_context !=
2347 curr->held_locks[depth-1].irq_context)
2352 if (!debug_locks_off_graph_unlock())
2356 * Clearly we all shouldn't be here, but since we made it we
2357 * can reliable say we messed up our state. See the above two
2358 * gotos for reasons why we could possibly end up here.
2365 struct lock_chain lock_chains[MAX_LOCKDEP_CHAINS];
2366 static DECLARE_BITMAP(lock_chains_in_use, MAX_LOCKDEP_CHAINS);
2367 int nr_chain_hlocks;
2368 static u16 chain_hlocks[MAX_LOCKDEP_CHAIN_HLOCKS];
2370 struct lock_class *lock_chain_get_class(struct lock_chain *chain, int i)
2372 return lock_classes + chain_hlocks[chain->base + i];
2376 * Returns the index of the first held_lock of the current chain
2378 static inline int get_first_held_lock(struct task_struct *curr,
2379 struct held_lock *hlock)
2382 struct held_lock *hlock_curr;
2384 for (i = curr->lockdep_depth - 1; i >= 0; i--) {
2385 hlock_curr = curr->held_locks + i;
2386 if (hlock_curr->irq_context != hlock->irq_context)
2394 #ifdef CONFIG_DEBUG_LOCKDEP
2396 * Returns the next chain_key iteration
2398 static u64 print_chain_key_iteration(int class_idx, u64 chain_key)
2400 u64 new_chain_key = iterate_chain_key(chain_key, class_idx);
2402 printk(" class_idx:%d -> chain_key:%016Lx",
2404 (unsigned long long)new_chain_key);
2405 return new_chain_key;
2409 print_chain_keys_held_locks(struct task_struct *curr, struct held_lock *hlock_next)
2411 struct held_lock *hlock;
2413 int depth = curr->lockdep_depth;
2416 printk("depth: %u\n", depth + 1);
2417 for (i = get_first_held_lock(curr, hlock_next); i < depth; i++) {
2418 hlock = curr->held_locks + i;
2419 chain_key = print_chain_key_iteration(hlock->class_idx, chain_key);
2424 print_chain_key_iteration(hlock_next->class_idx, chain_key);
2425 print_lock(hlock_next);
2428 static void print_chain_keys_chain(struct lock_chain *chain)
2434 printk("depth: %u\n", chain->depth);
2435 for (i = 0; i < chain->depth; i++) {
2436 class_id = chain_hlocks[chain->base + i];
2437 chain_key = print_chain_key_iteration(class_id + 1, chain_key);
2439 print_lock_name(lock_classes + class_id);
2444 static void print_collision(struct task_struct *curr,
2445 struct held_lock *hlock_next,
2446 struct lock_chain *chain)
2449 pr_warn("============================\n");
2450 pr_warn("WARNING: chain_key collision\n");
2451 print_kernel_ident();
2452 pr_warn("----------------------------\n");
2453 pr_warn("%s/%d: ", current->comm, task_pid_nr(current));
2454 pr_warn("Hash chain already cached but the contents don't match!\n");
2456 pr_warn("Held locks:");
2457 print_chain_keys_held_locks(curr, hlock_next);
2459 pr_warn("Locks in cached chain:");
2460 print_chain_keys_chain(chain);
2462 pr_warn("\nstack backtrace:\n");
2468 * Checks whether the chain and the current held locks are consistent
2469 * in depth and also in content. If they are not it most likely means
2470 * that there was a collision during the calculation of the chain_key.
2471 * Returns: 0 not passed, 1 passed
2473 static int check_no_collision(struct task_struct *curr,
2474 struct held_lock *hlock,
2475 struct lock_chain *chain)
2477 #ifdef CONFIG_DEBUG_LOCKDEP
2480 i = get_first_held_lock(curr, hlock);
2482 if (DEBUG_LOCKS_WARN_ON(chain->depth != curr->lockdep_depth - (i - 1))) {
2483 print_collision(curr, hlock, chain);
2487 for (j = 0; j < chain->depth - 1; j++, i++) {
2488 id = curr->held_locks[i].class_idx - 1;
2490 if (DEBUG_LOCKS_WARN_ON(chain_hlocks[chain->base + j] != id)) {
2491 print_collision(curr, hlock, chain);
2500 * Given an index that is >= -1, return the index of the next lock chain.
2501 * Return -2 if there is no next lock chain.
2503 long lockdep_next_lockchain(long i)
2505 i = find_next_bit(lock_chains_in_use, ARRAY_SIZE(lock_chains), i + 1);
2506 return i < ARRAY_SIZE(lock_chains) ? i : -2;
2509 unsigned long lock_chain_count(void)
2511 return bitmap_weight(lock_chains_in_use, ARRAY_SIZE(lock_chains));
2514 /* Must be called with the graph lock held. */
2515 static struct lock_chain *alloc_lock_chain(void)
2517 int idx = find_first_zero_bit(lock_chains_in_use,
2518 ARRAY_SIZE(lock_chains));
2520 if (unlikely(idx >= ARRAY_SIZE(lock_chains)))
2522 __set_bit(idx, lock_chains_in_use);
2523 return lock_chains + idx;
2527 * Adds a dependency chain into chain hashtable. And must be called with
2530 * Return 0 if fail, and graph_lock is released.
2531 * Return 1 if succeed, with graph_lock held.
2533 static inline int add_chain_cache(struct task_struct *curr,
2534 struct held_lock *hlock,
2537 struct lock_class *class = hlock_class(hlock);
2538 struct hlist_head *hash_head = chainhashentry(chain_key);
2539 struct lock_chain *chain;
2543 * The caller must hold the graph lock, ensure we've got IRQs
2544 * disabled to make this an IRQ-safe lock.. for recursion reasons
2545 * lockdep won't complain about its own locking errors.
2547 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2550 chain = alloc_lock_chain();
2552 if (!debug_locks_off_graph_unlock())
2555 print_lockdep_off("BUG: MAX_LOCKDEP_CHAINS too low!");
2559 chain->chain_key = chain_key;
2560 chain->irq_context = hlock->irq_context;
2561 i = get_first_held_lock(curr, hlock);
2562 chain->depth = curr->lockdep_depth + 1 - i;
2564 BUILD_BUG_ON((1UL << 24) <= ARRAY_SIZE(chain_hlocks));
2565 BUILD_BUG_ON((1UL << 6) <= ARRAY_SIZE(curr->held_locks));
2566 BUILD_BUG_ON((1UL << 8*sizeof(chain_hlocks[0])) <= ARRAY_SIZE(lock_classes));
2568 if (likely(nr_chain_hlocks + chain->depth <= MAX_LOCKDEP_CHAIN_HLOCKS)) {
2569 chain->base = nr_chain_hlocks;
2570 for (j = 0; j < chain->depth - 1; j++, i++) {
2571 int lock_id = curr->held_locks[i].class_idx - 1;
2572 chain_hlocks[chain->base + j] = lock_id;
2574 chain_hlocks[chain->base + j] = class - lock_classes;
2575 nr_chain_hlocks += chain->depth;
2577 if (!debug_locks_off_graph_unlock())
2580 print_lockdep_off("BUG: MAX_LOCKDEP_CHAIN_HLOCKS too low!");
2585 hlist_add_head_rcu(&chain->entry, hash_head);
2586 debug_atomic_inc(chain_lookup_misses);
2593 * Look up a dependency chain. Must be called with either the graph lock or
2594 * the RCU read lock held.
2596 static inline struct lock_chain *lookup_chain_cache(u64 chain_key)
2598 struct hlist_head *hash_head = chainhashentry(chain_key);
2599 struct lock_chain *chain;
2601 hlist_for_each_entry_rcu(chain, hash_head, entry) {
2602 if (READ_ONCE(chain->chain_key) == chain_key) {
2603 debug_atomic_inc(chain_lookup_hits);
2611 * If the key is not present yet in dependency chain cache then
2612 * add it and return 1 - in this case the new dependency chain is
2613 * validated. If the key is already hashed, return 0.
2614 * (On return with 1 graph_lock is held.)
2616 static inline int lookup_chain_cache_add(struct task_struct *curr,
2617 struct held_lock *hlock,
2620 struct lock_class *class = hlock_class(hlock);
2621 struct lock_chain *chain = lookup_chain_cache(chain_key);
2625 if (!check_no_collision(curr, hlock, chain))
2628 if (very_verbose(class)) {
2629 printk("\nhash chain already cached, key: "
2630 "%016Lx tail class: [%px] %s\n",
2631 (unsigned long long)chain_key,
2632 class->key, class->name);
2638 if (very_verbose(class)) {
2639 printk("\nnew hash chain, key: %016Lx tail class: [%px] %s\n",
2640 (unsigned long long)chain_key, class->key, class->name);
2647 * We have to walk the chain again locked - to avoid duplicates:
2649 chain = lookup_chain_cache(chain_key);
2655 if (!add_chain_cache(curr, hlock, chain_key))
2661 static int validate_chain(struct task_struct *curr, struct lockdep_map *lock,
2662 struct held_lock *hlock, int chain_head, u64 chain_key)
2665 * Trylock needs to maintain the stack of held locks, but it
2666 * does not add new dependencies, because trylock can be done
2669 * We look up the chain_key and do the O(N^2) check and update of
2670 * the dependencies only if this is a new dependency chain.
2671 * (If lookup_chain_cache_add() return with 1 it acquires
2672 * graph_lock for us)
2674 if (!hlock->trylock && hlock->check &&
2675 lookup_chain_cache_add(curr, hlock, chain_key)) {
2677 * Check whether last held lock:
2679 * - is irq-safe, if this lock is irq-unsafe
2680 * - is softirq-safe, if this lock is hardirq-unsafe
2682 * And check whether the new lock's dependency graph
2683 * could lead back to the previous lock.
2685 * any of these scenarios could lead to a deadlock. If
2688 int ret = check_deadlock(curr, hlock, lock, hlock->read);
2693 * Mark recursive read, as we jump over it when
2694 * building dependencies (just like we jump over
2700 * Add dependency only if this lock is not the head
2701 * of the chain, and if it's not a secondary read-lock:
2703 if (!chain_head && ret != 2) {
2704 if (!check_prevs_add(curr, hlock))
2710 /* after lookup_chain_cache_add(): */
2711 if (unlikely(!debug_locks))
2718 static inline int validate_chain(struct task_struct *curr,
2719 struct lockdep_map *lock, struct held_lock *hlock,
2720 int chain_head, u64 chain_key)
2727 * We are building curr_chain_key incrementally, so double-check
2728 * it from scratch, to make sure that it's done correctly:
2730 static void check_chain_key(struct task_struct *curr)
2732 #ifdef CONFIG_DEBUG_LOCKDEP
2733 struct held_lock *hlock, *prev_hlock = NULL;
2737 for (i = 0; i < curr->lockdep_depth; i++) {
2738 hlock = curr->held_locks + i;
2739 if (chain_key != hlock->prev_chain_key) {
2742 * We got mighty confused, our chain keys don't match
2743 * with what we expect, someone trample on our task state?
2745 WARN(1, "hm#1, depth: %u [%u], %016Lx != %016Lx\n",
2746 curr->lockdep_depth, i,
2747 (unsigned long long)chain_key,
2748 (unsigned long long)hlock->prev_chain_key);
2752 * Whoops ran out of static storage again?
2754 if (DEBUG_LOCKS_WARN_ON(hlock->class_idx > MAX_LOCKDEP_KEYS))
2757 if (prev_hlock && (prev_hlock->irq_context !=
2758 hlock->irq_context))
2760 chain_key = iterate_chain_key(chain_key, hlock->class_idx);
2763 if (chain_key != curr->curr_chain_key) {
2766 * More smoking hash instead of calculating it, damn see these
2767 * numbers float.. I bet that a pink elephant stepped on my memory.
2769 WARN(1, "hm#2, depth: %u [%u], %016Lx != %016Lx\n",
2770 curr->lockdep_depth, i,
2771 (unsigned long long)chain_key,
2772 (unsigned long long)curr->curr_chain_key);
2778 print_usage_bug_scenario(struct held_lock *lock)
2780 struct lock_class *class = hlock_class(lock);
2782 printk(" Possible unsafe locking scenario:\n\n");
2786 __print_lock_name(class);
2787 printk(KERN_CONT ");\n");
2788 printk(" <Interrupt>\n");
2790 __print_lock_name(class);
2791 printk(KERN_CONT ");\n");
2792 printk("\n *** DEADLOCK ***\n\n");
2796 print_usage_bug(struct task_struct *curr, struct held_lock *this,
2797 enum lock_usage_bit prev_bit, enum lock_usage_bit new_bit)
2799 if (!debug_locks_off_graph_unlock() || debug_locks_silent)
2803 pr_warn("================================\n");
2804 pr_warn("WARNING: inconsistent lock state\n");
2805 print_kernel_ident();
2806 pr_warn("--------------------------------\n");
2808 pr_warn("inconsistent {%s} -> {%s} usage.\n",
2809 usage_str[prev_bit], usage_str[new_bit]);
2811 pr_warn("%s/%d [HC%u[%lu]:SC%u[%lu]:HE%u:SE%u] takes:\n",
2812 curr->comm, task_pid_nr(curr),
2813 trace_hardirq_context(curr), hardirq_count() >> HARDIRQ_SHIFT,
2814 trace_softirq_context(curr), softirq_count() >> SOFTIRQ_SHIFT,
2815 trace_hardirqs_enabled(curr),
2816 trace_softirqs_enabled(curr));
2819 pr_warn("{%s} state was registered at:\n", usage_str[prev_bit]);
2820 print_stack_trace(hlock_class(this)->usage_traces + prev_bit, 1);
2822 print_irqtrace_events(curr);
2823 pr_warn("\nother info that might help us debug this:\n");
2824 print_usage_bug_scenario(this);
2826 lockdep_print_held_locks(curr);
2828 pr_warn("\nstack backtrace:\n");
2835 * Print out an error if an invalid bit is set:
2838 valid_state(struct task_struct *curr, struct held_lock *this,
2839 enum lock_usage_bit new_bit, enum lock_usage_bit bad_bit)
2841 if (unlikely(hlock_class(this)->usage_mask & (1 << bad_bit)))
2842 return print_usage_bug(curr, this, bad_bit, new_bit);
2846 static int mark_lock(struct task_struct *curr, struct held_lock *this,
2847 enum lock_usage_bit new_bit);
2849 #if defined(CONFIG_TRACE_IRQFLAGS) && defined(CONFIG_PROVE_LOCKING)
2852 * print irq inversion bug:
2855 print_irq_inversion_bug(struct task_struct *curr,
2856 struct lock_list *root, struct lock_list *other,
2857 struct held_lock *this, int forwards,
2858 const char *irqclass)
2860 struct lock_list *entry = other;
2861 struct lock_list *middle = NULL;
2864 if (!debug_locks_off_graph_unlock() || debug_locks_silent)
2868 pr_warn("========================================================\n");
2869 pr_warn("WARNING: possible irq lock inversion dependency detected\n");
2870 print_kernel_ident();
2871 pr_warn("--------------------------------------------------------\n");
2872 pr_warn("%s/%d just changed the state of lock:\n",
2873 curr->comm, task_pid_nr(curr));
2876 pr_warn("but this lock took another, %s-unsafe lock in the past:\n", irqclass);
2878 pr_warn("but this lock was taken by another, %s-safe lock in the past:\n", irqclass);
2879 print_lock_name(other->class);
2880 pr_warn("\n\nand interrupts could create inverse lock ordering between them.\n\n");
2882 pr_warn("\nother info that might help us debug this:\n");
2884 /* Find a middle lock (if one exists) */
2885 depth = get_lock_depth(other);
2887 if (depth == 0 && (entry != root)) {
2888 pr_warn("lockdep:%s bad path found in chain graph\n", __func__);
2892 entry = get_lock_parent(entry);
2894 } while (entry && entry != root && (depth >= 0));
2896 print_irq_lock_scenario(root, other,
2897 middle ? middle->class : root->class, other->class);
2899 print_irq_lock_scenario(other, root,
2900 middle ? middle->class : other->class, root->class);
2902 lockdep_print_held_locks(curr);
2904 pr_warn("\nthe shortest dependencies between 2nd lock and 1st lock:\n");
2905 if (!save_trace(&root->trace))
2907 print_shortest_lock_dependencies(other, root);
2909 pr_warn("\nstack backtrace:\n");
2916 * Prove that in the forwards-direction subgraph starting at <this>
2917 * there is no lock matching <mask>:
2920 check_usage_forwards(struct task_struct *curr, struct held_lock *this,
2921 enum lock_usage_bit bit, const char *irqclass)
2924 struct lock_list root;
2925 struct lock_list *uninitialized_var(target_entry);
2928 root.class = hlock_class(this);
2929 ret = find_usage_forwards(&root, bit, &target_entry);
2931 return print_bfs_bug(ret);
2935 return print_irq_inversion_bug(curr, &root, target_entry,
2940 * Prove that in the backwards-direction subgraph starting at <this>
2941 * there is no lock matching <mask>:
2944 check_usage_backwards(struct task_struct *curr, struct held_lock *this,
2945 enum lock_usage_bit bit, const char *irqclass)
2948 struct lock_list root;
2949 struct lock_list *uninitialized_var(target_entry);
2952 root.class = hlock_class(this);
2953 ret = find_usage_backwards(&root, bit, &target_entry);
2955 return print_bfs_bug(ret);
2959 return print_irq_inversion_bug(curr, &root, target_entry,
2963 void print_irqtrace_events(struct task_struct *curr)
2965 printk("irq event stamp: %u\n", curr->irq_events);
2966 printk("hardirqs last enabled at (%u): [<%px>] %pS\n",
2967 curr->hardirq_enable_event, (void *)curr->hardirq_enable_ip,
2968 (void *)curr->hardirq_enable_ip);
2969 printk("hardirqs last disabled at (%u): [<%px>] %pS\n",
2970 curr->hardirq_disable_event, (void *)curr->hardirq_disable_ip,
2971 (void *)curr->hardirq_disable_ip);
2972 printk("softirqs last enabled at (%u): [<%px>] %pS\n",
2973 curr->softirq_enable_event, (void *)curr->softirq_enable_ip,
2974 (void *)curr->softirq_enable_ip);
2975 printk("softirqs last disabled at (%u): [<%px>] %pS\n",
2976 curr->softirq_disable_event, (void *)curr->softirq_disable_ip,
2977 (void *)curr->softirq_disable_ip);
2980 static int HARDIRQ_verbose(struct lock_class *class)
2983 return class_filter(class);
2988 static int SOFTIRQ_verbose(struct lock_class *class)
2991 return class_filter(class);
2996 #define STRICT_READ_CHECKS 1
2998 static int (*state_verbose_f[])(struct lock_class *class) = {
2999 #define LOCKDEP_STATE(__STATE) \
3001 #include "lockdep_states.h"
3002 #undef LOCKDEP_STATE
3005 static inline int state_verbose(enum lock_usage_bit bit,
3006 struct lock_class *class)
3008 return state_verbose_f[bit >> 2](class);
3011 typedef int (*check_usage_f)(struct task_struct *, struct held_lock *,
3012 enum lock_usage_bit bit, const char *name);
3015 mark_lock_irq(struct task_struct *curr, struct held_lock *this,
3016 enum lock_usage_bit new_bit)
3018 int excl_bit = exclusive_bit(new_bit);
3019 int read = new_bit & LOCK_USAGE_READ_MASK;
3020 int dir = new_bit & LOCK_USAGE_DIR_MASK;
3023 * mark USED_IN has to look forwards -- to ensure no dependency
3024 * has ENABLED state, which would allow recursion deadlocks.
3026 * mark ENABLED has to look backwards -- to ensure no dependee
3027 * has USED_IN state, which, again, would allow recursion deadlocks.
3029 check_usage_f usage = dir ?
3030 check_usage_backwards : check_usage_forwards;
3033 * Validate that this particular lock does not have conflicting
3036 if (!valid_state(curr, this, new_bit, excl_bit))
3040 * Validate that the lock dependencies don't have conflicting usage
3043 if ((!read || !dir || STRICT_READ_CHECKS) &&
3044 !usage(curr, this, excl_bit, state_name(new_bit & ~LOCK_USAGE_READ_MASK)))
3048 * Check for read in write conflicts
3051 if (!valid_state(curr, this, new_bit, excl_bit + LOCK_USAGE_READ_MASK))
3054 if (STRICT_READ_CHECKS &&
3055 !usage(curr, this, excl_bit + LOCK_USAGE_READ_MASK,
3056 state_name(new_bit + LOCK_USAGE_READ_MASK)))
3060 if (state_verbose(new_bit, hlock_class(this)))
3067 * Mark all held locks with a usage bit:
3070 mark_held_locks(struct task_struct *curr, enum lock_usage_bit base_bit)
3072 struct held_lock *hlock;
3075 for (i = 0; i < curr->lockdep_depth; i++) {
3076 enum lock_usage_bit hlock_bit = base_bit;
3077 hlock = curr->held_locks + i;
3080 hlock_bit += LOCK_USAGE_READ_MASK;
3082 BUG_ON(hlock_bit >= LOCK_USAGE_STATES);
3087 if (!mark_lock(curr, hlock, hlock_bit))
3095 * Hardirqs will be enabled:
3097 static void __trace_hardirqs_on_caller(unsigned long ip)
3099 struct task_struct *curr = current;
3101 /* we'll do an OFF -> ON transition: */
3102 curr->hardirqs_enabled = 1;
3105 * We are going to turn hardirqs on, so set the
3106 * usage bit for all held locks:
3108 if (!mark_held_locks(curr, LOCK_ENABLED_HARDIRQ))
3111 * If we have softirqs enabled, then set the usage
3112 * bit for all held locks. (disabled hardirqs prevented
3113 * this bit from being set before)
3115 if (curr->softirqs_enabled)
3116 if (!mark_held_locks(curr, LOCK_ENABLED_SOFTIRQ))
3119 curr->hardirq_enable_ip = ip;
3120 curr->hardirq_enable_event = ++curr->irq_events;
3121 debug_atomic_inc(hardirqs_on_events);
3124 void lockdep_hardirqs_on(unsigned long ip)
3126 if (unlikely(!debug_locks || current->lockdep_recursion))
3129 if (unlikely(current->hardirqs_enabled)) {
3131 * Neither irq nor preemption are disabled here
3132 * so this is racy by nature but losing one hit
3133 * in a stat is not a big deal.
3135 __debug_atomic_inc(redundant_hardirqs_on);
3140 * We're enabling irqs and according to our state above irqs weren't
3141 * already enabled, yet we find the hardware thinks they are in fact
3142 * enabled.. someone messed up their IRQ state tracing.
3144 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
3148 * See the fine text that goes along with this variable definition.
3150 if (DEBUG_LOCKS_WARN_ON(unlikely(early_boot_irqs_disabled)))
3154 * Can't allow enabling interrupts while in an interrupt handler,
3155 * that's general bad form and such. Recursion, limited stack etc..
3157 if (DEBUG_LOCKS_WARN_ON(current->hardirq_context))
3160 current->lockdep_recursion = 1;
3161 __trace_hardirqs_on_caller(ip);
3162 current->lockdep_recursion = 0;
3166 * Hardirqs were disabled:
3168 void lockdep_hardirqs_off(unsigned long ip)
3170 struct task_struct *curr = current;
3172 if (unlikely(!debug_locks || current->lockdep_recursion))
3176 * So we're supposed to get called after you mask local IRQs, but for
3177 * some reason the hardware doesn't quite think you did a proper job.
3179 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
3182 if (curr->hardirqs_enabled) {
3184 * We have done an ON -> OFF transition:
3186 curr->hardirqs_enabled = 0;
3187 curr->hardirq_disable_ip = ip;
3188 curr->hardirq_disable_event = ++curr->irq_events;
3189 debug_atomic_inc(hardirqs_off_events);
3191 debug_atomic_inc(redundant_hardirqs_off);
3195 * Softirqs will be enabled:
3197 void trace_softirqs_on(unsigned long ip)
3199 struct task_struct *curr = current;
3201 if (unlikely(!debug_locks || current->lockdep_recursion))
3205 * We fancy IRQs being disabled here, see softirq.c, avoids
3206 * funny state and nesting things.
3208 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
3211 if (curr->softirqs_enabled) {
3212 debug_atomic_inc(redundant_softirqs_on);
3216 current->lockdep_recursion = 1;
3218 * We'll do an OFF -> ON transition:
3220 curr->softirqs_enabled = 1;
3221 curr->softirq_enable_ip = ip;
3222 curr->softirq_enable_event = ++curr->irq_events;
3223 debug_atomic_inc(softirqs_on_events);
3225 * We are going to turn softirqs on, so set the
3226 * usage bit for all held locks, if hardirqs are
3229 if (curr->hardirqs_enabled)
3230 mark_held_locks(curr, LOCK_ENABLED_SOFTIRQ);
3231 current->lockdep_recursion = 0;
3235 * Softirqs were disabled:
3237 void trace_softirqs_off(unsigned long ip)
3239 struct task_struct *curr = current;
3241 if (unlikely(!debug_locks || current->lockdep_recursion))
3245 * We fancy IRQs being disabled here, see softirq.c
3247 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
3250 if (curr->softirqs_enabled) {
3252 * We have done an ON -> OFF transition:
3254 curr->softirqs_enabled = 0;
3255 curr->softirq_disable_ip = ip;
3256 curr->softirq_disable_event = ++curr->irq_events;
3257 debug_atomic_inc(softirqs_off_events);
3259 * Whoops, we wanted softirqs off, so why aren't they?
3261 DEBUG_LOCKS_WARN_ON(!softirq_count());
3263 debug_atomic_inc(redundant_softirqs_off);
3266 static int mark_irqflags(struct task_struct *curr, struct held_lock *hlock)
3269 * If non-trylock use in a hardirq or softirq context, then
3270 * mark the lock as used in these contexts:
3272 if (!hlock->trylock) {
3274 if (curr->hardirq_context)
3275 if (!mark_lock(curr, hlock,
3276 LOCK_USED_IN_HARDIRQ_READ))
3278 if (curr->softirq_context)
3279 if (!mark_lock(curr, hlock,
3280 LOCK_USED_IN_SOFTIRQ_READ))
3283 if (curr->hardirq_context)
3284 if (!mark_lock(curr, hlock, LOCK_USED_IN_HARDIRQ))
3286 if (curr->softirq_context)
3287 if (!mark_lock(curr, hlock, LOCK_USED_IN_SOFTIRQ))
3291 if (!hlock->hardirqs_off) {
3293 if (!mark_lock(curr, hlock,
3294 LOCK_ENABLED_HARDIRQ_READ))
3296 if (curr->softirqs_enabled)
3297 if (!mark_lock(curr, hlock,
3298 LOCK_ENABLED_SOFTIRQ_READ))
3301 if (!mark_lock(curr, hlock,
3302 LOCK_ENABLED_HARDIRQ))
3304 if (curr->softirqs_enabled)
3305 if (!mark_lock(curr, hlock,
3306 LOCK_ENABLED_SOFTIRQ))
3314 static inline unsigned int task_irq_context(struct task_struct *task)
3316 return 2 * !!task->hardirq_context + !!task->softirq_context;
3319 static int separate_irq_context(struct task_struct *curr,
3320 struct held_lock *hlock)
3322 unsigned int depth = curr->lockdep_depth;
3325 * Keep track of points where we cross into an interrupt context:
3328 struct held_lock *prev_hlock;
3330 prev_hlock = curr->held_locks + depth-1;
3332 * If we cross into another context, reset the
3333 * hash key (this also prevents the checking and the
3334 * adding of the dependency to 'prev'):
3336 if (prev_hlock->irq_context != hlock->irq_context)
3342 #else /* defined(CONFIG_TRACE_IRQFLAGS) && defined(CONFIG_PROVE_LOCKING) */
3345 int mark_lock_irq(struct task_struct *curr, struct held_lock *this,
3346 enum lock_usage_bit new_bit)
3348 WARN_ON(1); /* Impossible innit? when we don't have TRACE_IRQFLAG */
3352 static inline int mark_irqflags(struct task_struct *curr,
3353 struct held_lock *hlock)
3358 static inline unsigned int task_irq_context(struct task_struct *task)
3363 static inline int separate_irq_context(struct task_struct *curr,
3364 struct held_lock *hlock)
3369 #endif /* defined(CONFIG_TRACE_IRQFLAGS) && defined(CONFIG_PROVE_LOCKING) */
3372 * Mark a lock with a usage bit, and validate the state transition:
3374 static int mark_lock(struct task_struct *curr, struct held_lock *this,
3375 enum lock_usage_bit new_bit)
3377 unsigned int new_mask = 1 << new_bit, ret = 1;
3380 * If already set then do not dirty the cacheline,
3381 * nor do any checks:
3383 if (likely(hlock_class(this)->usage_mask & new_mask))
3389 * Make sure we didn't race:
3391 if (unlikely(hlock_class(this)->usage_mask & new_mask)) {
3396 hlock_class(this)->usage_mask |= new_mask;
3398 if (!save_trace(hlock_class(this)->usage_traces + new_bit))
3402 #define LOCKDEP_STATE(__STATE) \
3403 case LOCK_USED_IN_##__STATE: \
3404 case LOCK_USED_IN_##__STATE##_READ: \
3405 case LOCK_ENABLED_##__STATE: \
3406 case LOCK_ENABLED_##__STATE##_READ:
3407 #include "lockdep_states.h"
3408 #undef LOCKDEP_STATE
3409 ret = mark_lock_irq(curr, this, new_bit);
3414 debug_atomic_dec(nr_unused_locks);
3417 if (!debug_locks_off_graph_unlock())
3426 * We must printk outside of the graph_lock:
3429 printk("\nmarked lock as {%s}:\n", usage_str[new_bit]);
3431 print_irqtrace_events(curr);
3439 * Initialize a lock instance's lock-class mapping info:
3441 void lockdep_init_map(struct lockdep_map *lock, const char *name,
3442 struct lock_class_key *key, int subclass)
3446 for (i = 0; i < NR_LOCKDEP_CACHING_CLASSES; i++)
3447 lock->class_cache[i] = NULL;
3449 #ifdef CONFIG_LOCK_STAT
3450 lock->cpu = raw_smp_processor_id();
3454 * Can't be having no nameless bastards around this place!
3456 if (DEBUG_LOCKS_WARN_ON(!name)) {
3457 lock->name = "NULL";
3464 * No key, no joy, we need to hash something.
3466 if (DEBUG_LOCKS_WARN_ON(!key))
3469 * Sanity check, the lock-class key must either have been allocated
3470 * statically or must have been registered as a dynamic key.
3472 if (!static_obj(key) && !is_dynamic_key(key)) {
3474 printk(KERN_ERR "BUG: key %px has not been registered!\n", key);
3475 DEBUG_LOCKS_WARN_ON(1);
3480 if (unlikely(!debug_locks))
3484 unsigned long flags;
3486 if (DEBUG_LOCKS_WARN_ON(current->lockdep_recursion))
3489 raw_local_irq_save(flags);
3490 current->lockdep_recursion = 1;
3491 register_lock_class(lock, subclass, 1);
3492 current->lockdep_recursion = 0;
3493 raw_local_irq_restore(flags);
3496 EXPORT_SYMBOL_GPL(lockdep_init_map);
3498 struct lock_class_key __lockdep_no_validate__;
3499 EXPORT_SYMBOL_GPL(__lockdep_no_validate__);
3502 print_lock_nested_lock_not_held(struct task_struct *curr,
3503 struct held_lock *hlock,
3506 if (!debug_locks_off())
3508 if (debug_locks_silent)
3512 pr_warn("==================================\n");
3513 pr_warn("WARNING: Nested lock was not taken\n");
3514 print_kernel_ident();
3515 pr_warn("----------------------------------\n");
3517 pr_warn("%s/%d is trying to lock:\n", curr->comm, task_pid_nr(curr));
3520 pr_warn("\nbut this task is not holding:\n");
3521 pr_warn("%s\n", hlock->nest_lock->name);