1 // SPDX-License-Identifier: GPL-2.0-only
5 * Runtime locking correctness validator
7 * Started by Ingo Molnar:
9 * Copyright (C) 2006,2007 Red Hat, Inc., Ingo Molnar <mingo@redhat.com>
10 * Copyright (C) 2007 Red Hat, Inc., Peter Zijlstra
12 * this code maps all the lock dependencies as they occur in a live kernel
13 * and will warn about the following classes of locking bugs:
15 * - lock inversion scenarios
16 * - circular lock dependencies
17 * - hardirq/softirq safe/unsafe locking bugs
19 * Bugs are reported even if the current locking scenario does not cause
20 * any deadlock at this point.
22 * I.e. if anytime in the past two locks were taken in a different order,
23 * even if it happened for another task, even if those were different
24 * locks (but of the same class as this lock), this code will detect it.
26 * Thanks to Arjan van de Ven for coming up with the initial idea of
27 * mapping lock dependencies runtime.
29 #define DISABLE_BRANCH_PROFILING
30 #include <linux/mutex.h>
31 #include <linux/sched.h>
32 #include <linux/sched/clock.h>
33 #include <linux/sched/task.h>
34 #include <linux/sched/mm.h>
35 #include <linux/delay.h>
36 #include <linux/module.h>
37 #include <linux/proc_fs.h>
38 #include <linux/seq_file.h>
39 #include <linux/spinlock.h>
40 #include <linux/kallsyms.h>
41 #include <linux/interrupt.h>
42 #include <linux/stacktrace.h>
43 #include <linux/debug_locks.h>
44 #include <linux/irqflags.h>
45 #include <linux/utsname.h>
46 #include <linux/hash.h>
47 #include <linux/ftrace.h>
48 #include <linux/stringify.h>
49 #include <linux/bitmap.h>
50 #include <linux/bitops.h>
51 #include <linux/gfp.h>
52 #include <linux/random.h>
53 #include <linux/jhash.h>
54 #include <linux/nmi.h>
55 #include <linux/rcupdate.h>
56 #include <linux/kprobes.h>
57 #include <linux/lockdep.h>
59 #include <asm/sections.h>
61 #include "lockdep_internals.h"
63 #include <trace/events/lock.h>
65 #ifdef CONFIG_PROVE_LOCKING
66 static int prove_locking = 1;
67 module_param(prove_locking, int, 0644);
69 #define prove_locking 0
72 #ifdef CONFIG_LOCK_STAT
73 static int lock_stat = 1;
74 module_param(lock_stat, int, 0644);
80 static struct ctl_table kern_lockdep_table[] = {
81 #ifdef CONFIG_PROVE_LOCKING
83 .procname = "prove_locking",
84 .data = &prove_locking,
85 .maxlen = sizeof(int),
87 .proc_handler = proc_dointvec,
89 #endif /* CONFIG_PROVE_LOCKING */
90 #ifdef CONFIG_LOCK_STAT
92 .procname = "lock_stat",
94 .maxlen = sizeof(int),
96 .proc_handler = proc_dointvec,
98 #endif /* CONFIG_LOCK_STAT */
102 static __init int kernel_lockdep_sysctls_init(void)
104 register_sysctl_init("kernel", kern_lockdep_table);
107 late_initcall(kernel_lockdep_sysctls_init);
108 #endif /* CONFIG_SYSCTL */
110 DEFINE_PER_CPU(unsigned int, lockdep_recursion);
111 EXPORT_PER_CPU_SYMBOL_GPL(lockdep_recursion);
113 static __always_inline bool lockdep_enabled(void)
118 if (this_cpu_read(lockdep_recursion))
121 if (current->lockdep_recursion)
128 * lockdep_lock: protects the lockdep graph, the hashes and the
129 * class/list/hash allocators.
131 * This is one of the rare exceptions where it's justified
132 * to use a raw spinlock - we really dont want the spinlock
133 * code to recurse back into the lockdep code...
135 static arch_spinlock_t __lock = (arch_spinlock_t)__ARCH_SPIN_LOCK_UNLOCKED;
136 static struct task_struct *__owner;
138 static inline void lockdep_lock(void)
140 DEBUG_LOCKS_WARN_ON(!irqs_disabled());
142 __this_cpu_inc(lockdep_recursion);
143 arch_spin_lock(&__lock);
147 static inline void lockdep_unlock(void)
149 DEBUG_LOCKS_WARN_ON(!irqs_disabled());
151 if (debug_locks && DEBUG_LOCKS_WARN_ON(__owner != current))
155 arch_spin_unlock(&__lock);
156 __this_cpu_dec(lockdep_recursion);
159 static inline bool lockdep_assert_locked(void)
161 return DEBUG_LOCKS_WARN_ON(__owner != current);
164 static struct task_struct *lockdep_selftest_task_struct;
167 static int graph_lock(void)
171 * Make sure that if another CPU detected a bug while
172 * walking the graph we dont change it (while the other
173 * CPU is busy printing out stuff with the graph lock
183 static inline void graph_unlock(void)
189 * Turn lock debugging off and return with 0 if it was off already,
190 * and also release the graph lock:
192 static inline int debug_locks_off_graph_unlock(void)
194 int ret = debug_locks_off();
201 unsigned long nr_list_entries;
202 static struct lock_list list_entries[MAX_LOCKDEP_ENTRIES];
203 static DECLARE_BITMAP(list_entries_in_use, MAX_LOCKDEP_ENTRIES);
206 * All data structures here are protected by the global debug_lock.
208 * nr_lock_classes is the number of elements of lock_classes[] that is
211 #define KEYHASH_BITS (MAX_LOCKDEP_KEYS_BITS - 1)
212 #define KEYHASH_SIZE (1UL << KEYHASH_BITS)
213 static struct hlist_head lock_keys_hash[KEYHASH_SIZE];
214 unsigned long nr_lock_classes;
215 unsigned long nr_zapped_classes;
216 unsigned long max_lock_class_idx;
217 struct lock_class lock_classes[MAX_LOCKDEP_KEYS];
218 DECLARE_BITMAP(lock_classes_in_use, MAX_LOCKDEP_KEYS);
220 static inline struct lock_class *hlock_class(struct held_lock *hlock)
222 unsigned int class_idx = hlock->class_idx;
224 /* Don't re-read hlock->class_idx, can't use READ_ONCE() on bitfield */
227 if (!test_bit(class_idx, lock_classes_in_use)) {
229 * Someone passed in garbage, we give up.
231 DEBUG_LOCKS_WARN_ON(1);
236 * At this point, if the passed hlock->class_idx is still garbage,
237 * we just have to live with it
239 return lock_classes + class_idx;
242 #ifdef CONFIG_LOCK_STAT
243 static DEFINE_PER_CPU(struct lock_class_stats[MAX_LOCKDEP_KEYS], cpu_lock_stats);
245 static inline u64 lockstat_clock(void)
247 return local_clock();
250 static int lock_point(unsigned long points[], unsigned long ip)
254 for (i = 0; i < LOCKSTAT_POINTS; i++) {
255 if (points[i] == 0) {
266 static void lock_time_inc(struct lock_time *lt, u64 time)
271 if (time < lt->min || !lt->nr)
278 static inline void lock_time_add(struct lock_time *src, struct lock_time *dst)
283 if (src->max > dst->max)
286 if (src->min < dst->min || !dst->nr)
289 dst->total += src->total;
293 struct lock_class_stats lock_stats(struct lock_class *class)
295 struct lock_class_stats stats;
298 memset(&stats, 0, sizeof(struct lock_class_stats));
299 for_each_possible_cpu(cpu) {
300 struct lock_class_stats *pcs =
301 &per_cpu(cpu_lock_stats, cpu)[class - lock_classes];
303 for (i = 0; i < ARRAY_SIZE(stats.contention_point); i++)
304 stats.contention_point[i] += pcs->contention_point[i];
306 for (i = 0; i < ARRAY_SIZE(stats.contending_point); i++)
307 stats.contending_point[i] += pcs->contending_point[i];
309 lock_time_add(&pcs->read_waittime, &stats.read_waittime);
310 lock_time_add(&pcs->write_waittime, &stats.write_waittime);
312 lock_time_add(&pcs->read_holdtime, &stats.read_holdtime);
313 lock_time_add(&pcs->write_holdtime, &stats.write_holdtime);
315 for (i = 0; i < ARRAY_SIZE(stats.bounces); i++)
316 stats.bounces[i] += pcs->bounces[i];
322 void clear_lock_stats(struct lock_class *class)
326 for_each_possible_cpu(cpu) {
327 struct lock_class_stats *cpu_stats =
328 &per_cpu(cpu_lock_stats, cpu)[class - lock_classes];
330 memset(cpu_stats, 0, sizeof(struct lock_class_stats));
332 memset(class->contention_point, 0, sizeof(class->contention_point));
333 memset(class->contending_point, 0, sizeof(class->contending_point));
336 static struct lock_class_stats *get_lock_stats(struct lock_class *class)
338 return &this_cpu_ptr(cpu_lock_stats)[class - lock_classes];
341 static void lock_release_holdtime(struct held_lock *hlock)
343 struct lock_class_stats *stats;
349 holdtime = lockstat_clock() - hlock->holdtime_stamp;
351 stats = get_lock_stats(hlock_class(hlock));
353 lock_time_inc(&stats->read_holdtime, holdtime);
355 lock_time_inc(&stats->write_holdtime, holdtime);
358 static inline void lock_release_holdtime(struct held_lock *hlock)
364 * We keep a global list of all lock classes. The list is only accessed with
365 * the lockdep spinlock lock held. free_lock_classes is a list with free
366 * elements. These elements are linked together by the lock_entry member in
369 static LIST_HEAD(all_lock_classes);
370 static LIST_HEAD(free_lock_classes);
373 * struct pending_free - information about data structures about to be freed
374 * @zapped: Head of a list with struct lock_class elements.
375 * @lock_chains_being_freed: Bitmap that indicates which lock_chains[] elements
376 * are about to be freed.
378 struct pending_free {
379 struct list_head zapped;
380 DECLARE_BITMAP(lock_chains_being_freed, MAX_LOCKDEP_CHAINS);
384 * struct delayed_free - data structures used for delayed freeing
386 * A data structure for delayed freeing of data structures that may be
387 * accessed by RCU readers at the time these were freed.
389 * @rcu_head: Used to schedule an RCU callback for freeing data structures.
390 * @index: Index of @pf to which freed data structures are added.
391 * @scheduled: Whether or not an RCU callback has been scheduled.
392 * @pf: Array with information about data structures about to be freed.
394 static struct delayed_free {
395 struct rcu_head rcu_head;
398 struct pending_free pf[2];
402 * The lockdep classes are in a hash-table as well, for fast lookup:
404 #define CLASSHASH_BITS (MAX_LOCKDEP_KEYS_BITS - 1)
405 #define CLASSHASH_SIZE (1UL << CLASSHASH_BITS)
406 #define __classhashfn(key) hash_long((unsigned long)key, CLASSHASH_BITS)
407 #define classhashentry(key) (classhash_table + __classhashfn((key)))
409 static struct hlist_head classhash_table[CLASSHASH_SIZE];
412 * We put the lock dependency chains into a hash-table as well, to cache
415 #define CHAINHASH_BITS (MAX_LOCKDEP_CHAINS_BITS-1)
416 #define CHAINHASH_SIZE (1UL << CHAINHASH_BITS)
417 #define __chainhashfn(chain) hash_long(chain, CHAINHASH_BITS)
418 #define chainhashentry(chain) (chainhash_table + __chainhashfn((chain)))
420 static struct hlist_head chainhash_table[CHAINHASH_SIZE];
423 * the id of held_lock
425 static inline u16 hlock_id(struct held_lock *hlock)
427 BUILD_BUG_ON(MAX_LOCKDEP_KEYS_BITS + 2 > 16);
429 return (hlock->class_idx | (hlock->read << MAX_LOCKDEP_KEYS_BITS));
432 static inline unsigned int chain_hlock_class_idx(u16 hlock_id)
434 return hlock_id & (MAX_LOCKDEP_KEYS - 1);
438 * The hash key of the lock dependency chains is a hash itself too:
439 * it's a hash of all locks taken up to that lock, including that lock.
440 * It's a 64-bit hash, because it's important for the keys to be
443 static inline u64 iterate_chain_key(u64 key, u32 idx)
445 u32 k0 = key, k1 = key >> 32;
447 __jhash_mix(idx, k0, k1); /* Macro that modifies arguments! */
449 return k0 | (u64)k1 << 32;
452 void lockdep_init_task(struct task_struct *task)
454 task->lockdep_depth = 0; /* no locks held yet */
455 task->curr_chain_key = INITIAL_CHAIN_KEY;
456 task->lockdep_recursion = 0;
459 static __always_inline void lockdep_recursion_inc(void)
461 __this_cpu_inc(lockdep_recursion);
464 static __always_inline void lockdep_recursion_finish(void)
466 if (WARN_ON_ONCE(__this_cpu_dec_return(lockdep_recursion)))
467 __this_cpu_write(lockdep_recursion, 0);
470 void lockdep_set_selftest_task(struct task_struct *task)
472 lockdep_selftest_task_struct = task;
476 * Debugging switches:
480 #define VERY_VERBOSE 0
483 # define HARDIRQ_VERBOSE 1
484 # define SOFTIRQ_VERBOSE 1
486 # define HARDIRQ_VERBOSE 0
487 # define SOFTIRQ_VERBOSE 0
490 #if VERBOSE || HARDIRQ_VERBOSE || SOFTIRQ_VERBOSE
492 * Quick filtering for interesting events:
494 static int class_filter(struct lock_class *class)
498 if (class->name_version == 1 &&
499 !strcmp(class->name, "lockname"))
501 if (class->name_version == 1 &&
502 !strcmp(class->name, "&struct->lockfield"))
505 /* Filter everything else. 1 would be to allow everything else */
510 static int verbose(struct lock_class *class)
513 return class_filter(class);
518 static void print_lockdep_off(const char *bug_msg)
520 printk(KERN_DEBUG "%s\n", bug_msg);
521 printk(KERN_DEBUG "turning off the locking correctness validator.\n");
522 #ifdef CONFIG_LOCK_STAT
523 printk(KERN_DEBUG "Please attach the output of /proc/lock_stat to the bug report\n");
527 unsigned long nr_stack_trace_entries;
529 #ifdef CONFIG_PROVE_LOCKING
531 * struct lock_trace - single stack backtrace
532 * @hash_entry: Entry in a stack_trace_hash[] list.
533 * @hash: jhash() of @entries.
534 * @nr_entries: Number of entries in @entries.
535 * @entries: Actual stack backtrace.
538 struct hlist_node hash_entry;
541 unsigned long entries[] __aligned(sizeof(unsigned long));
543 #define LOCK_TRACE_SIZE_IN_LONGS \
544 (sizeof(struct lock_trace) / sizeof(unsigned long))
546 * Stack-trace: sequence of lock_trace structures. Protected by the graph_lock.
548 static unsigned long stack_trace[MAX_STACK_TRACE_ENTRIES];
549 static struct hlist_head stack_trace_hash[STACK_TRACE_HASH_SIZE];
551 static bool traces_identical(struct lock_trace *t1, struct lock_trace *t2)
553 return t1->hash == t2->hash && t1->nr_entries == t2->nr_entries &&
554 memcmp(t1->entries, t2->entries,
555 t1->nr_entries * sizeof(t1->entries[0])) == 0;
558 static struct lock_trace *save_trace(void)
560 struct lock_trace *trace, *t2;
561 struct hlist_head *hash_head;
565 BUILD_BUG_ON_NOT_POWER_OF_2(STACK_TRACE_HASH_SIZE);
566 BUILD_BUG_ON(LOCK_TRACE_SIZE_IN_LONGS >= MAX_STACK_TRACE_ENTRIES);
568 trace = (struct lock_trace *)(stack_trace + nr_stack_trace_entries);
569 max_entries = MAX_STACK_TRACE_ENTRIES - nr_stack_trace_entries -
570 LOCK_TRACE_SIZE_IN_LONGS;
572 if (max_entries <= 0) {
573 if (!debug_locks_off_graph_unlock())
576 print_lockdep_off("BUG: MAX_STACK_TRACE_ENTRIES too low!");
581 trace->nr_entries = stack_trace_save(trace->entries, max_entries, 3);
583 hash = jhash(trace->entries, trace->nr_entries *
584 sizeof(trace->entries[0]), 0);
586 hash_head = stack_trace_hash + (hash & (STACK_TRACE_HASH_SIZE - 1));
587 hlist_for_each_entry(t2, hash_head, hash_entry) {
588 if (traces_identical(trace, t2))
591 nr_stack_trace_entries += LOCK_TRACE_SIZE_IN_LONGS + trace->nr_entries;
592 hlist_add_head(&trace->hash_entry, hash_head);
597 /* Return the number of stack traces in the stack_trace[] array. */
598 u64 lockdep_stack_trace_count(void)
600 struct lock_trace *trace;
604 for (i = 0; i < ARRAY_SIZE(stack_trace_hash); i++) {
605 hlist_for_each_entry(trace, &stack_trace_hash[i], hash_entry) {
613 /* Return the number of stack hash chains that have at least one stack trace. */
614 u64 lockdep_stack_hash_count(void)
619 for (i = 0; i < ARRAY_SIZE(stack_trace_hash); i++)
620 if (!hlist_empty(&stack_trace_hash[i]))
627 unsigned int nr_hardirq_chains;
628 unsigned int nr_softirq_chains;
629 unsigned int nr_process_chains;
630 unsigned int max_lockdep_depth;
632 #ifdef CONFIG_DEBUG_LOCKDEP
634 * Various lockdep statistics:
636 DEFINE_PER_CPU(struct lockdep_stats, lockdep_stats);
639 #ifdef CONFIG_PROVE_LOCKING
644 #define __USAGE(__STATE) \
645 [LOCK_USED_IN_##__STATE] = "IN-"__stringify(__STATE)"-W", \
646 [LOCK_ENABLED_##__STATE] = __stringify(__STATE)"-ON-W", \
647 [LOCK_USED_IN_##__STATE##_READ] = "IN-"__stringify(__STATE)"-R",\
648 [LOCK_ENABLED_##__STATE##_READ] = __stringify(__STATE)"-ON-R",
650 static const char *usage_str[] =
652 #define LOCKDEP_STATE(__STATE) __USAGE(__STATE)
653 #include "lockdep_states.h"
655 [LOCK_USED] = "INITIAL USE",
656 [LOCK_USED_READ] = "INITIAL READ USE",
657 /* abused as string storage for verify_lock_unused() */
658 [LOCK_USAGE_STATES] = "IN-NMI",
662 const char *__get_key_name(const struct lockdep_subclass_key *key, char *str)
664 return kallsyms_lookup((unsigned long)key, NULL, NULL, NULL, str);
667 static inline unsigned long lock_flag(enum lock_usage_bit bit)
672 static char get_usage_char(struct lock_class *class, enum lock_usage_bit bit)
675 * The usage character defaults to '.' (i.e., irqs disabled and not in
676 * irq context), which is the safest usage category.
681 * The order of the following usage checks matters, which will
682 * result in the outcome character as follows:
684 * - '+': irq is enabled and not in irq context
685 * - '-': in irq context and irq is disabled
686 * - '?': in irq context and irq is enabled
688 if (class->usage_mask & lock_flag(bit + LOCK_USAGE_DIR_MASK)) {
690 if (class->usage_mask & lock_flag(bit))
692 } else if (class->usage_mask & lock_flag(bit))
698 void get_usage_chars(struct lock_class *class, char usage[LOCK_USAGE_CHARS])
702 #define LOCKDEP_STATE(__STATE) \
703 usage[i++] = get_usage_char(class, LOCK_USED_IN_##__STATE); \
704 usage[i++] = get_usage_char(class, LOCK_USED_IN_##__STATE##_READ);
705 #include "lockdep_states.h"
711 static void __print_lock_name(struct lock_class *class)
713 char str[KSYM_NAME_LEN];
718 name = __get_key_name(class->key, str);
719 printk(KERN_CONT "%s", name);
721 printk(KERN_CONT "%s", name);
722 if (class->name_version > 1)
723 printk(KERN_CONT "#%d", class->name_version);
725 printk(KERN_CONT "/%d", class->subclass);
729 static void print_lock_name(struct lock_class *class)
731 char usage[LOCK_USAGE_CHARS];
733 get_usage_chars(class, usage);
735 printk(KERN_CONT " (");
736 __print_lock_name(class);
737 printk(KERN_CONT "){%s}-{%d:%d}", usage,
738 class->wait_type_outer ?: class->wait_type_inner,
739 class->wait_type_inner);
742 static void print_lockdep_cache(struct lockdep_map *lock)
745 char str[KSYM_NAME_LEN];
749 name = __get_key_name(lock->key->subkeys, str);
751 printk(KERN_CONT "%s", name);
754 static void print_lock(struct held_lock *hlock)
757 * We can be called locklessly through debug_show_all_locks() so be
758 * extra careful, the hlock might have been released and cleared.
760 * If this indeed happens, lets pretend it does not hurt to continue
761 * to print the lock unless the hlock class_idx does not point to a
762 * registered class. The rationale here is: since we don't attempt
763 * to distinguish whether we are in this situation, if it just
764 * happened we can't count on class_idx to tell either.
766 struct lock_class *lock = hlock_class(hlock);
769 printk(KERN_CONT "<RELEASED>\n");
773 printk(KERN_CONT "%px", hlock->instance);
774 print_lock_name(lock);
775 printk(KERN_CONT ", at: %pS\n", (void *)hlock->acquire_ip);
778 static void lockdep_print_held_locks(struct task_struct *p)
780 int i, depth = READ_ONCE(p->lockdep_depth);
783 printk("no locks held by %s/%d.\n", p->comm, task_pid_nr(p));
785 printk("%d lock%s held by %s/%d:\n", depth,
786 depth > 1 ? "s" : "", p->comm, task_pid_nr(p));
788 * It's not reliable to print a task's held locks if it's not sleeping
789 * and it's not the current task.
791 if (p != current && task_is_running(p))
793 for (i = 0; i < depth; i++) {
795 print_lock(p->held_locks + i);
799 static void print_kernel_ident(void)
801 printk("%s %.*s %s\n", init_utsname()->release,
802 (int)strcspn(init_utsname()->version, " "),
803 init_utsname()->version,
807 static int very_verbose(struct lock_class *class)
810 return class_filter(class);
816 * Is this the address of a static object:
820 * Check if an address is part of freed initmem. After initmem is freed,
821 * memory can be allocated from it, and such allocations would then have
822 * addresses within the range [_stext, _end].
824 #ifndef arch_is_kernel_initmem_freed
825 static int arch_is_kernel_initmem_freed(unsigned long addr)
827 if (system_state < SYSTEM_FREEING_INITMEM)
830 return init_section_contains((void *)addr, 1);
834 static int static_obj(const void *obj)
836 unsigned long start = (unsigned long) &_stext,
837 end = (unsigned long) &_end,
838 addr = (unsigned long) obj;
840 if (arch_is_kernel_initmem_freed(addr))
846 if ((addr >= start) && (addr < end))
850 * in-kernel percpu var?
852 if (is_kernel_percpu_address(addr))
856 * module static or percpu var?
858 return is_module_address(addr) || is_module_percpu_address(addr);
863 * To make lock name printouts unique, we calculate a unique
864 * class->name_version generation counter. The caller must hold the graph
867 static int count_matching_names(struct lock_class *new_class)
869 struct lock_class *class;
872 if (!new_class->name)
875 list_for_each_entry(class, &all_lock_classes, lock_entry) {
876 if (new_class->key - new_class->subclass == class->key)
877 return class->name_version;
878 if (class->name && !strcmp(class->name, new_class->name))
879 count = max(count, class->name_version);
885 /* used from NMI context -- must be lockless */
886 static noinstr struct lock_class *
887 look_up_lock_class(const struct lockdep_map *lock, unsigned int subclass)
889 struct lockdep_subclass_key *key;
890 struct hlist_head *hash_head;
891 struct lock_class *class;
893 if (unlikely(subclass >= MAX_LOCKDEP_SUBCLASSES)) {
894 instrumentation_begin();
897 "BUG: looking up invalid subclass: %u\n", subclass);
899 "turning off the locking correctness validator.\n");
901 instrumentation_end();
906 * If it is not initialised then it has never been locked,
907 * so it won't be present in the hash table.
909 if (unlikely(!lock->key))
913 * NOTE: the class-key must be unique. For dynamic locks, a static
914 * lock_class_key variable is passed in through the mutex_init()
915 * (or spin_lock_init()) call - which acts as the key. For static
916 * locks we use the lock object itself as the key.
918 BUILD_BUG_ON(sizeof(struct lock_class_key) >
919 sizeof(struct lockdep_map));
921 key = lock->key->subkeys + subclass;
923 hash_head = classhashentry(key);
926 * We do an RCU walk of the hash, see lockdep_free_key_range().
928 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
931 hlist_for_each_entry_rcu_notrace(class, hash_head, hash_entry) {
932 if (class->key == key) {
934 * Huh! same key, different name? Did someone trample
935 * on some memory? We're most confused.
937 WARN_ON_ONCE(class->name != lock->name &&
938 lock->key != &__lockdep_no_validate__);
947 * Static locks do not have their class-keys yet - for them the key is
948 * the lock object itself. If the lock is in the per cpu area, the
949 * canonical address of the lock (per cpu offset removed) is used.
951 static bool assign_lock_key(struct lockdep_map *lock)
953 unsigned long can_addr, addr = (unsigned long)lock;
957 * lockdep_free_key_range() assumes that struct lock_class_key
958 * objects do not overlap. Since we use the address of lock
959 * objects as class key for static objects, check whether the
960 * size of lock_class_key objects does not exceed the size of
961 * the smallest lock object.
963 BUILD_BUG_ON(sizeof(struct lock_class_key) > sizeof(raw_spinlock_t));
966 if (__is_kernel_percpu_address(addr, &can_addr))
967 lock->key = (void *)can_addr;
968 else if (__is_module_percpu_address(addr, &can_addr))
969 lock->key = (void *)can_addr;
970 else if (static_obj(lock))
971 lock->key = (void *)lock;
973 /* Debug-check: all keys must be persistent! */
975 pr_err("INFO: trying to register non-static key.\n");
976 pr_err("The code is fine but needs lockdep annotation, or maybe\n");
977 pr_err("you didn't initialize this object before use?\n");
978 pr_err("turning off the locking correctness validator.\n");
986 #ifdef CONFIG_DEBUG_LOCKDEP
988 /* Check whether element @e occurs in list @h */
989 static bool in_list(struct list_head *e, struct list_head *h)
993 list_for_each(f, h) {
1002 * Check whether entry @e occurs in any of the locks_after or locks_before
1005 static bool in_any_class_list(struct list_head *e)
1007 struct lock_class *class;
1010 for (i = 0; i < ARRAY_SIZE(lock_classes); i++) {
1011 class = &lock_classes[i];
1012 if (in_list(e, &class->locks_after) ||
1013 in_list(e, &class->locks_before))
1019 static bool class_lock_list_valid(struct lock_class *c, struct list_head *h)
1021 struct lock_list *e;
1023 list_for_each_entry(e, h, entry) {
1024 if (e->links_to != c) {
1025 printk(KERN_INFO "class %s: mismatch for lock entry %ld; class %s <> %s",
1027 (unsigned long)(e - list_entries),
1028 e->links_to && e->links_to->name ?
1029 e->links_to->name : "(?)",
1030 e->class && e->class->name ? e->class->name :
1038 #ifdef CONFIG_PROVE_LOCKING
1039 static u16 chain_hlocks[MAX_LOCKDEP_CHAIN_HLOCKS];
1042 static bool check_lock_chain_key(struct lock_chain *chain)
1044 #ifdef CONFIG_PROVE_LOCKING
1045 u64 chain_key = INITIAL_CHAIN_KEY;
1048 for (i = chain->base; i < chain->base + chain->depth; i++)
1049 chain_key = iterate_chain_key(chain_key, chain_hlocks[i]);
1051 * The 'unsigned long long' casts avoid that a compiler warning
1052 * is reported when building tools/lib/lockdep.
1054 if (chain->chain_key != chain_key) {
1055 printk(KERN_INFO "chain %lld: key %#llx <> %#llx\n",
1056 (unsigned long long)(chain - lock_chains),
1057 (unsigned long long)chain->chain_key,
1058 (unsigned long long)chain_key);
1065 static bool in_any_zapped_class_list(struct lock_class *class)
1067 struct pending_free *pf;
1070 for (i = 0, pf = delayed_free.pf; i < ARRAY_SIZE(delayed_free.pf); i++, pf++) {
1071 if (in_list(&class->lock_entry, &pf->zapped))
1078 static bool __check_data_structures(void)
1080 struct lock_class *class;
1081 struct lock_chain *chain;
1082 struct hlist_head *head;
1083 struct lock_list *e;
1086 /* Check whether all classes occur in a lock list. */
1087 for (i = 0; i < ARRAY_SIZE(lock_classes); i++) {
1088 class = &lock_classes[i];
1089 if (!in_list(&class->lock_entry, &all_lock_classes) &&
1090 !in_list(&class->lock_entry, &free_lock_classes) &&
1091 !in_any_zapped_class_list(class)) {
1092 printk(KERN_INFO "class %px/%s is not in any class list\n",
1093 class, class->name ? : "(?)");
1098 /* Check whether all classes have valid lock lists. */
1099 for (i = 0; i < ARRAY_SIZE(lock_classes); i++) {
1100 class = &lock_classes[i];
1101 if (!class_lock_list_valid(class, &class->locks_before))
1103 if (!class_lock_list_valid(class, &class->locks_after))
1107 /* Check the chain_key of all lock chains. */
1108 for (i = 0; i < ARRAY_SIZE(chainhash_table); i++) {
1109 head = chainhash_table + i;
1110 hlist_for_each_entry_rcu(chain, head, entry) {
1111 if (!check_lock_chain_key(chain))
1117 * Check whether all list entries that are in use occur in a class
1120 for_each_set_bit(i, list_entries_in_use, ARRAY_SIZE(list_entries)) {
1121 e = list_entries + i;
1122 if (!in_any_class_list(&e->entry)) {
1123 printk(KERN_INFO "list entry %d is not in any class list; class %s <> %s\n",
1124 (unsigned int)(e - list_entries),
1125 e->class->name ? : "(?)",
1126 e->links_to->name ? : "(?)");
1132 * Check whether all list entries that are not in use do not occur in
1133 * a class lock list.
1135 for_each_clear_bit(i, list_entries_in_use, ARRAY_SIZE(list_entries)) {
1136 e = list_entries + i;
1137 if (in_any_class_list(&e->entry)) {
1138 printk(KERN_INFO "list entry %d occurs in a class list; class %s <> %s\n",
1139 (unsigned int)(e - list_entries),
1140 e->class && e->class->name ? e->class->name :
1142 e->links_to && e->links_to->name ?
1143 e->links_to->name : "(?)");
1151 int check_consistency = 0;
1152 module_param(check_consistency, int, 0644);
1154 static void check_data_structures(void)
1156 static bool once = false;
1158 if (check_consistency && !once) {
1159 if (!__check_data_structures()) {
1166 #else /* CONFIG_DEBUG_LOCKDEP */
1168 static inline void check_data_structures(void) { }
1170 #endif /* CONFIG_DEBUG_LOCKDEP */
1172 static void init_chain_block_buckets(void);
1175 * Initialize the lock_classes[] array elements, the free_lock_classes list
1176 * and also the delayed_free structure.
1178 static void init_data_structures_once(void)
1180 static bool __read_mostly ds_initialized, rcu_head_initialized;
1183 if (likely(rcu_head_initialized))
1186 if (system_state >= SYSTEM_SCHEDULING) {
1187 init_rcu_head(&delayed_free.rcu_head);
1188 rcu_head_initialized = true;
1194 ds_initialized = true;
1196 INIT_LIST_HEAD(&delayed_free.pf[0].zapped);
1197 INIT_LIST_HEAD(&delayed_free.pf[1].zapped);
1199 for (i = 0; i < ARRAY_SIZE(lock_classes); i++) {
1200 list_add_tail(&lock_classes[i].lock_entry, &free_lock_classes);
1201 INIT_LIST_HEAD(&lock_classes[i].locks_after);
1202 INIT_LIST_HEAD(&lock_classes[i].locks_before);
1204 init_chain_block_buckets();
1207 static inline struct hlist_head *keyhashentry(const struct lock_class_key *key)
1209 unsigned long hash = hash_long((uintptr_t)key, KEYHASH_BITS);
1211 return lock_keys_hash + hash;
1214 /* Register a dynamically allocated key. */
1215 void lockdep_register_key(struct lock_class_key *key)
1217 struct hlist_head *hash_head;
1218 struct lock_class_key *k;
1219 unsigned long flags;
1221 if (WARN_ON_ONCE(static_obj(key)))
1223 hash_head = keyhashentry(key);
1225 raw_local_irq_save(flags);
1228 hlist_for_each_entry_rcu(k, hash_head, hash_entry) {
1229 if (WARN_ON_ONCE(k == key))
1232 hlist_add_head_rcu(&key->hash_entry, hash_head);
1236 raw_local_irq_restore(flags);
1238 EXPORT_SYMBOL_GPL(lockdep_register_key);
1240 /* Check whether a key has been registered as a dynamic key. */
1241 static bool is_dynamic_key(const struct lock_class_key *key)
1243 struct hlist_head *hash_head;
1244 struct lock_class_key *k;
1247 if (WARN_ON_ONCE(static_obj(key)))
1251 * If lock debugging is disabled lock_keys_hash[] may contain
1252 * pointers to memory that has already been freed. Avoid triggering
1253 * a use-after-free in that case by returning early.
1258 hash_head = keyhashentry(key);
1261 hlist_for_each_entry_rcu(k, hash_head, hash_entry) {
1273 * Register a lock's class in the hash-table, if the class is not present
1274 * yet. Otherwise we look it up. We cache the result in the lock object
1275 * itself, so actual lookup of the hash should be once per lock object.
1277 static struct lock_class *
1278 register_lock_class(struct lockdep_map *lock, unsigned int subclass, int force)
1280 struct lockdep_subclass_key *key;
1281 struct hlist_head *hash_head;
1282 struct lock_class *class;
1285 DEBUG_LOCKS_WARN_ON(!irqs_disabled());
1287 class = look_up_lock_class(lock, subclass);
1289 goto out_set_class_cache;
1292 if (!assign_lock_key(lock))
1294 } else if (!static_obj(lock->key) && !is_dynamic_key(lock->key)) {
1298 key = lock->key->subkeys + subclass;
1299 hash_head = classhashentry(key);
1301 if (!graph_lock()) {
1305 * We have to do the hash-walk again, to avoid races
1308 hlist_for_each_entry_rcu(class, hash_head, hash_entry) {
1309 if (class->key == key)
1310 goto out_unlock_set;
1313 init_data_structures_once();
1315 /* Allocate a new lock class and add it to the hash. */
1316 class = list_first_entry_or_null(&free_lock_classes, typeof(*class),
1319 if (!debug_locks_off_graph_unlock()) {
1323 print_lockdep_off("BUG: MAX_LOCKDEP_KEYS too low!");
1328 __set_bit(class - lock_classes, lock_classes_in_use);
1329 debug_atomic_inc(nr_unused_locks);
1331 class->name = lock->name;
1332 class->subclass = subclass;
1333 WARN_ON_ONCE(!list_empty(&class->locks_before));
1334 WARN_ON_ONCE(!list_empty(&class->locks_after));
1335 class->name_version = count_matching_names(class);
1336 class->wait_type_inner = lock->wait_type_inner;
1337 class->wait_type_outer = lock->wait_type_outer;
1338 class->lock_type = lock->lock_type;
1340 * We use RCU's safe list-add method to make
1341 * parallel walking of the hash-list safe:
1343 hlist_add_head_rcu(&class->hash_entry, hash_head);
1345 * Remove the class from the free list and add it to the global list
1348 list_move_tail(&class->lock_entry, &all_lock_classes);
1349 idx = class - lock_classes;
1350 if (idx > max_lock_class_idx)
1351 max_lock_class_idx = idx;
1353 if (verbose(class)) {
1356 printk("\nnew class %px: %s", class->key, class->name);
1357 if (class->name_version > 1)
1358 printk(KERN_CONT "#%d", class->name_version);
1359 printk(KERN_CONT "\n");
1362 if (!graph_lock()) {
1369 out_set_class_cache:
1370 if (!subclass || force)
1371 lock->class_cache[0] = class;
1372 else if (subclass < NR_LOCKDEP_CACHING_CLASSES)
1373 lock->class_cache[subclass] = class;
1376 * Hash collision, did we smoke some? We found a class with a matching
1377 * hash but the subclass -- which is hashed in -- didn't match.
1379 if (DEBUG_LOCKS_WARN_ON(class->subclass != subclass))
1385 #ifdef CONFIG_PROVE_LOCKING
1387 * Allocate a lockdep entry. (assumes the graph_lock held, returns
1388 * with NULL on failure)
1390 static struct lock_list *alloc_list_entry(void)
1392 int idx = find_first_zero_bit(list_entries_in_use,
1393 ARRAY_SIZE(list_entries));
1395 if (idx >= ARRAY_SIZE(list_entries)) {
1396 if (!debug_locks_off_graph_unlock())
1399 print_lockdep_off("BUG: MAX_LOCKDEP_ENTRIES too low!");
1404 __set_bit(idx, list_entries_in_use);
1405 return list_entries + idx;
1409 * Add a new dependency to the head of the list:
1411 static int add_lock_to_list(struct lock_class *this,
1412 struct lock_class *links_to, struct list_head *head,
1413 u16 distance, u8 dep,
1414 const struct lock_trace *trace)
1416 struct lock_list *entry;
1418 * Lock not present yet - get a new dependency struct and
1419 * add it to the list:
1421 entry = alloc_list_entry();
1425 entry->class = this;
1426 entry->links_to = links_to;
1428 entry->distance = distance;
1429 entry->trace = trace;
1431 * Both allocation and removal are done under the graph lock; but
1432 * iteration is under RCU-sched; see look_up_lock_class() and
1433 * lockdep_free_key_range().
1435 list_add_tail_rcu(&entry->entry, head);
1441 * For good efficiency of modular, we use power of 2
1443 #define MAX_CIRCULAR_QUEUE_SIZE (1UL << CONFIG_LOCKDEP_CIRCULAR_QUEUE_BITS)
1444 #define CQ_MASK (MAX_CIRCULAR_QUEUE_SIZE-1)
1447 * The circular_queue and helpers are used to implement graph
1448 * breadth-first search (BFS) algorithm, by which we can determine
1449 * whether there is a path from a lock to another. In deadlock checks,
1450 * a path from the next lock to be acquired to a previous held lock
1451 * indicates that adding the <prev> -> <next> lock dependency will
1452 * produce a circle in the graph. Breadth-first search instead of
1453 * depth-first search is used in order to find the shortest (circular)
1456 struct circular_queue {
1457 struct lock_list *element[MAX_CIRCULAR_QUEUE_SIZE];
1458 unsigned int front, rear;
1461 static struct circular_queue lock_cq;
1463 unsigned int max_bfs_queue_depth;
1465 static unsigned int lockdep_dependency_gen_id;
1467 static inline void __cq_init(struct circular_queue *cq)
1469 cq->front = cq->rear = 0;
1470 lockdep_dependency_gen_id++;
1473 static inline int __cq_empty(struct circular_queue *cq)
1475 return (cq->front == cq->rear);
1478 static inline int __cq_full(struct circular_queue *cq)
1480 return ((cq->rear + 1) & CQ_MASK) == cq->front;
1483 static inline int __cq_enqueue(struct circular_queue *cq, struct lock_list *elem)
1488 cq->element[cq->rear] = elem;
1489 cq->rear = (cq->rear + 1) & CQ_MASK;
1494 * Dequeue an element from the circular_queue, return a lock_list if
1495 * the queue is not empty, or NULL if otherwise.
1497 static inline struct lock_list * __cq_dequeue(struct circular_queue *cq)
1499 struct lock_list * lock;
1504 lock = cq->element[cq->front];
1505 cq->front = (cq->front + 1) & CQ_MASK;
1510 static inline unsigned int __cq_get_elem_count(struct circular_queue *cq)
1512 return (cq->rear - cq->front) & CQ_MASK;
1515 static inline void mark_lock_accessed(struct lock_list *lock)
1517 lock->class->dep_gen_id = lockdep_dependency_gen_id;
1520 static inline void visit_lock_entry(struct lock_list *lock,
1521 struct lock_list *parent)
1523 lock->parent = parent;
1526 static inline unsigned long lock_accessed(struct lock_list *lock)
1528 return lock->class->dep_gen_id == lockdep_dependency_gen_id;
1531 static inline struct lock_list *get_lock_parent(struct lock_list *child)
1533 return child->parent;
1536 static inline int get_lock_depth(struct lock_list *child)
1539 struct lock_list *parent;
1541 while ((parent = get_lock_parent(child))) {
1549 * Return the forward or backward dependency list.
1551 * @lock: the lock_list to get its class's dependency list
1552 * @offset: the offset to struct lock_class to determine whether it is
1553 * locks_after or locks_before
1555 static inline struct list_head *get_dep_list(struct lock_list *lock, int offset)
1557 void *lock_class = lock->class;
1559 return lock_class + offset;
1562 * Return values of a bfs search:
1564 * BFS_E* indicates an error
1565 * BFS_R* indicates a result (match or not)
1567 * BFS_EINVALIDNODE: Find a invalid node in the graph.
1569 * BFS_EQUEUEFULL: The queue is full while doing the bfs.
1571 * BFS_RMATCH: Find the matched node in the graph, and put that node into
1574 * BFS_RNOMATCH: Haven't found the matched node and keep *@target_entry
1578 BFS_EINVALIDNODE = -2,
1579 BFS_EQUEUEFULL = -1,
1585 * bfs_result < 0 means error
1587 static inline bool bfs_error(enum bfs_result res)
1593 * DEP_*_BIT in lock_list::dep
1595 * For dependency @prev -> @next:
1597 * SR: @prev is shared reader (->read != 0) and @next is recursive reader
1599 * ER: @prev is exclusive locker (->read == 0) and @next is recursive reader
1600 * SN: @prev is shared reader and @next is non-recursive locker (->read != 2)
1601 * EN: @prev is exclusive locker and @next is non-recursive locker
1603 * Note that we define the value of DEP_*_BITs so that:
1604 * bit0 is prev->read == 0
1605 * bit1 is next->read != 2
1607 #define DEP_SR_BIT (0 + (0 << 1)) /* 0 */
1608 #define DEP_ER_BIT (1 + (0 << 1)) /* 1 */
1609 #define DEP_SN_BIT (0 + (1 << 1)) /* 2 */
1610 #define DEP_EN_BIT (1 + (1 << 1)) /* 3 */
1612 #define DEP_SR_MASK (1U << (DEP_SR_BIT))
1613 #define DEP_ER_MASK (1U << (DEP_ER_BIT))
1614 #define DEP_SN_MASK (1U << (DEP_SN_BIT))
1615 #define DEP_EN_MASK (1U << (DEP_EN_BIT))
1617 static inline unsigned int
1618 __calc_dep_bit(struct held_lock *prev, struct held_lock *next)
1620 return (prev->read == 0) + ((next->read != 2) << 1);
1623 static inline u8 calc_dep(struct held_lock *prev, struct held_lock *next)
1625 return 1U << __calc_dep_bit(prev, next);
1629 * calculate the dep_bit for backwards edges. We care about whether @prev is
1630 * shared and whether @next is recursive.
1632 static inline unsigned int
1633 __calc_dep_bitb(struct held_lock *prev, struct held_lock *next)
1635 return (next->read != 2) + ((prev->read == 0) << 1);
1638 static inline u8 calc_depb(struct held_lock *prev, struct held_lock *next)
1640 return 1U << __calc_dep_bitb(prev, next);
1644 * Initialize a lock_list entry @lock belonging to @class as the root for a BFS
1647 static inline void __bfs_init_root(struct lock_list *lock,
1648 struct lock_class *class)
1650 lock->class = class;
1651 lock->parent = NULL;
1656 * Initialize a lock_list entry @lock based on a lock acquisition @hlock as the
1657 * root for a BFS search.
1659 * ->only_xr of the initial lock node is set to @hlock->read == 2, to make sure
1660 * that <prev> -> @hlock and @hlock -> <whatever __bfs() found> is not -(*R)->
1663 static inline void bfs_init_root(struct lock_list *lock,
1664 struct held_lock *hlock)
1666 __bfs_init_root(lock, hlock_class(hlock));
1667 lock->only_xr = (hlock->read == 2);
1671 * Similar to bfs_init_root() but initialize the root for backwards BFS.
1673 * ->only_xr of the initial lock node is set to @hlock->read != 0, to make sure
1674 * that <next> -> @hlock and @hlock -> <whatever backwards BFS found> is not
1675 * -(*S)-> and -(R*)-> (reverse order of -(*R)-> and -(S*)->).
1677 static inline void bfs_init_rootb(struct lock_list *lock,
1678 struct held_lock *hlock)
1680 __bfs_init_root(lock, hlock_class(hlock));
1681 lock->only_xr = (hlock->read != 0);
1684 static inline struct lock_list *__bfs_next(struct lock_list *lock, int offset)
1686 if (!lock || !lock->parent)
1689 return list_next_or_null_rcu(get_dep_list(lock->parent, offset),
1690 &lock->entry, struct lock_list, entry);
1694 * Breadth-First Search to find a strong path in the dependency graph.
1696 * @source_entry: the source of the path we are searching for.
1697 * @data: data used for the second parameter of @match function
1698 * @match: match function for the search
1699 * @target_entry: pointer to the target of a matched path
1700 * @offset: the offset to struct lock_class to determine whether it is
1701 * locks_after or locks_before
1703 * We may have multiple edges (considering different kinds of dependencies,
1704 * e.g. ER and SN) between two nodes in the dependency graph. But
1705 * only the strong dependency path in the graph is relevant to deadlocks. A
1706 * strong dependency path is a dependency path that doesn't have two adjacent
1707 * dependencies as -(*R)-> -(S*)->, please see:
1709 * Documentation/locking/lockdep-design.rst
1711 * for more explanation of the definition of strong dependency paths
1713 * In __bfs(), we only traverse in the strong dependency path:
1715 * In lock_list::only_xr, we record whether the previous dependency only
1716 * has -(*R)-> in the search, and if it does (prev only has -(*R)->), we
1717 * filter out any -(S*)-> in the current dependency and after that, the
1718 * ->only_xr is set according to whether we only have -(*R)-> left.
1720 static enum bfs_result __bfs(struct lock_list *source_entry,
1722 bool (*match)(struct lock_list *entry, void *data),
1723 bool (*skip)(struct lock_list *entry, void *data),
1724 struct lock_list **target_entry,
1727 struct circular_queue *cq = &lock_cq;
1728 struct lock_list *lock = NULL;
1729 struct lock_list *entry;
1730 struct list_head *head;
1731 unsigned int cq_depth;
1734 lockdep_assert_locked();
1737 __cq_enqueue(cq, source_entry);
1739 while ((lock = __bfs_next(lock, offset)) || (lock = __cq_dequeue(cq))) {
1741 return BFS_EINVALIDNODE;
1744 * Step 1: check whether we already finish on this one.
1746 * If we have visited all the dependencies from this @lock to
1747 * others (iow, if we have visited all lock_list entries in
1748 * @lock->class->locks_{after,before}) we skip, otherwise go
1749 * and visit all the dependencies in the list and mark this
1752 if (lock_accessed(lock))
1755 mark_lock_accessed(lock);
1758 * Step 2: check whether prev dependency and this form a strong
1761 if (lock->parent) { /* Parent exists, check prev dependency */
1763 bool prev_only_xr = lock->parent->only_xr;
1766 * Mask out all -(S*)-> if we only have *R in previous
1767 * step, because -(*R)-> -(S*)-> don't make up a strong
1771 dep &= ~(DEP_SR_MASK | DEP_SN_MASK);
1773 /* If nothing left, we skip */
1777 /* If there are only -(*R)-> left, set that for the next step */
1778 lock->only_xr = !(dep & (DEP_SN_MASK | DEP_EN_MASK));
1782 * Step 3: we haven't visited this and there is a strong
1783 * dependency path to this, so check with @match.
1784 * If @skip is provide and returns true, we skip this
1785 * lock (and any path this lock is in).
1787 if (skip && skip(lock, data))
1790 if (match(lock, data)) {
1791 *target_entry = lock;
1796 * Step 4: if not match, expand the path by adding the
1797 * forward or backwards dependencies in the search
1801 head = get_dep_list(lock, offset);
1802 list_for_each_entry_rcu(entry, head, entry) {
1803 visit_lock_entry(entry, lock);
1806 * Note we only enqueue the first of the list into the
1807 * queue, because we can always find a sibling
1808 * dependency from one (see __bfs_next()), as a result
1809 * the space of queue is saved.
1816 if (__cq_enqueue(cq, entry))
1817 return BFS_EQUEUEFULL;
1819 cq_depth = __cq_get_elem_count(cq);
1820 if (max_bfs_queue_depth < cq_depth)
1821 max_bfs_queue_depth = cq_depth;
1825 return BFS_RNOMATCH;
1828 static inline enum bfs_result
1829 __bfs_forwards(struct lock_list *src_entry,
1831 bool (*match)(struct lock_list *entry, void *data),
1832 bool (*skip)(struct lock_list *entry, void *data),
1833 struct lock_list **target_entry)
1835 return __bfs(src_entry, data, match, skip, target_entry,
1836 offsetof(struct lock_class, locks_after));
1840 static inline enum bfs_result
1841 __bfs_backwards(struct lock_list *src_entry,
1843 bool (*match)(struct lock_list *entry, void *data),
1844 bool (*skip)(struct lock_list *entry, void *data),
1845 struct lock_list **target_entry)
1847 return __bfs(src_entry, data, match, skip, target_entry,
1848 offsetof(struct lock_class, locks_before));
1852 static void print_lock_trace(const struct lock_trace *trace,
1853 unsigned int spaces)
1855 stack_trace_print(trace->entries, trace->nr_entries, spaces);
1859 * Print a dependency chain entry (this is only done when a deadlock
1860 * has been detected):
1862 static noinline void
1863 print_circular_bug_entry(struct lock_list *target, int depth)
1865 if (debug_locks_silent)
1867 printk("\n-> #%u", depth);
1868 print_lock_name(target->class);
1869 printk(KERN_CONT ":\n");
1870 print_lock_trace(target->trace, 6);
1874 print_circular_lock_scenario(struct held_lock *src,
1875 struct held_lock *tgt,
1876 struct lock_list *prt)
1878 struct lock_class *source = hlock_class(src);
1879 struct lock_class *target = hlock_class(tgt);
1880 struct lock_class *parent = prt->class;
1883 * A direct locking problem where unsafe_class lock is taken
1884 * directly by safe_class lock, then all we need to show
1885 * is the deadlock scenario, as it is obvious that the
1886 * unsafe lock is taken under the safe lock.
1888 * But if there is a chain instead, where the safe lock takes
1889 * an intermediate lock (middle_class) where this lock is
1890 * not the same as the safe lock, then the lock chain is
1891 * used to describe the problem. Otherwise we would need
1892 * to show a different CPU case for each link in the chain
1893 * from the safe_class lock to the unsafe_class lock.
1895 if (parent != source) {
1896 printk("Chain exists of:\n ");
1897 __print_lock_name(source);
1898 printk(KERN_CONT " --> ");
1899 __print_lock_name(parent);
1900 printk(KERN_CONT " --> ");
1901 __print_lock_name(target);
1902 printk(KERN_CONT "\n\n");
1905 printk(" Possible unsafe locking scenario:\n\n");
1906 printk(" CPU0 CPU1\n");
1907 printk(" ---- ----\n");
1909 __print_lock_name(target);
1910 printk(KERN_CONT ");\n");
1912 __print_lock_name(parent);
1913 printk(KERN_CONT ");\n");
1915 __print_lock_name(target);
1916 printk(KERN_CONT ");\n");
1918 __print_lock_name(source);
1919 printk(KERN_CONT ");\n");
1920 printk("\n *** DEADLOCK ***\n\n");
1924 * When a circular dependency is detected, print the
1927 static noinline void
1928 print_circular_bug_header(struct lock_list *entry, unsigned int depth,
1929 struct held_lock *check_src,
1930 struct held_lock *check_tgt)
1932 struct task_struct *curr = current;
1934 if (debug_locks_silent)
1938 pr_warn("======================================================\n");
1939 pr_warn("WARNING: possible circular locking dependency detected\n");
1940 print_kernel_ident();
1941 pr_warn("------------------------------------------------------\n");
1942 pr_warn("%s/%d is trying to acquire lock:\n",
1943 curr->comm, task_pid_nr(curr));
1944 print_lock(check_src);
1946 pr_warn("\nbut task is already holding lock:\n");
1948 print_lock(check_tgt);
1949 pr_warn("\nwhich lock already depends on the new lock.\n\n");
1950 pr_warn("\nthe existing dependency chain (in reverse order) is:\n");
1952 print_circular_bug_entry(entry, depth);
1956 * We are about to add A -> B into the dependency graph, and in __bfs() a
1957 * strong dependency path A -> .. -> B is found: hlock_class equals
1960 * If A -> .. -> B can replace A -> B in any __bfs() search (means the former
1961 * is _stronger_ than or equal to the latter), we consider A -> B as redundant.
1962 * For example if A -> .. -> B is -(EN)-> (i.e. A -(E*)-> .. -(*N)-> B), and A
1963 * -> B is -(ER)-> or -(EN)->, then we don't need to add A -> B into the
1964 * dependency graph, as any strong path ..-> A -> B ->.. we can get with
1965 * having dependency A -> B, we could already get a equivalent path ..-> A ->
1966 * .. -> B -> .. with A -> .. -> B. Therefore A -> B is redundant.
1968 * We need to make sure both the start and the end of A -> .. -> B is not
1969 * weaker than A -> B. For the start part, please see the comment in
1970 * check_redundant(). For the end part, we need:
1974 * a) A -> B is -(*R)-> (everything is not weaker than that)
1978 * b) A -> .. -> B is -(*N)-> (nothing is stronger than this)
1981 static inline bool hlock_equal(struct lock_list *entry, void *data)
1983 struct held_lock *hlock = (struct held_lock *)data;
1985 return hlock_class(hlock) == entry->class && /* Found A -> .. -> B */
1986 (hlock->read == 2 || /* A -> B is -(*R)-> */
1987 !entry->only_xr); /* A -> .. -> B is -(*N)-> */
1991 * We are about to add B -> A into the dependency graph, and in __bfs() a
1992 * strong dependency path A -> .. -> B is found: hlock_class equals
1995 * We will have a deadlock case (conflict) if A -> .. -> B -> A is a strong
1996 * dependency cycle, that means:
2000 * a) B -> A is -(E*)->
2004 * b) A -> .. -> B is -(*N)-> (i.e. A -> .. -(*N)-> B)
2006 * as then we don't have -(*R)-> -(S*)-> in the cycle.
2008 static inline bool hlock_conflict(struct lock_list *entry, void *data)
2010 struct held_lock *hlock = (struct held_lock *)data;
2012 return hlock_class(hlock) == entry->class && /* Found A -> .. -> B */
2013 (hlock->read == 0 || /* B -> A is -(E*)-> */
2014 !entry->only_xr); /* A -> .. -> B is -(*N)-> */
2017 static noinline void print_circular_bug(struct lock_list *this,
2018 struct lock_list *target,
2019 struct held_lock *check_src,
2020 struct held_lock *check_tgt)
2022 struct task_struct *curr = current;
2023 struct lock_list *parent;
2024 struct lock_list *first_parent;
2027 if (!debug_locks_off_graph_unlock() || debug_locks_silent)
2030 this->trace = save_trace();
2034 depth = get_lock_depth(target);
2036 print_circular_bug_header(target, depth, check_src, check_tgt);
2038 parent = get_lock_parent(target);
2039 first_parent = parent;
2042 print_circular_bug_entry(parent, --depth);
2043 parent = get_lock_parent(parent);
2046 printk("\nother info that might help us debug this:\n\n");
2047 print_circular_lock_scenario(check_src, check_tgt,
2050 lockdep_print_held_locks(curr);
2052 printk("\nstack backtrace:\n");
2056 static noinline void print_bfs_bug(int ret)
2058 if (!debug_locks_off_graph_unlock())
2062 * Breadth-first-search failed, graph got corrupted?
2064 WARN(1, "lockdep bfs error:%d\n", ret);
2067 static bool noop_count(struct lock_list *entry, void *data)
2069 (*(unsigned long *)data)++;
2073 static unsigned long __lockdep_count_forward_deps(struct lock_list *this)
2075 unsigned long count = 0;
2076 struct lock_list *target_entry;
2078 __bfs_forwards(this, (void *)&count, noop_count, NULL, &target_entry);
2082 unsigned long lockdep_count_forward_deps(struct lock_class *class)
2084 unsigned long ret, flags;
2085 struct lock_list this;
2087 __bfs_init_root(&this, class);
2089 raw_local_irq_save(flags);
2091 ret = __lockdep_count_forward_deps(&this);
2093 raw_local_irq_restore(flags);
2098 static unsigned long __lockdep_count_backward_deps(struct lock_list *this)
2100 unsigned long count = 0;
2101 struct lock_list *target_entry;
2103 __bfs_backwards(this, (void *)&count, noop_count, NULL, &target_entry);
2108 unsigned long lockdep_count_backward_deps(struct lock_class *class)
2110 unsigned long ret, flags;
2111 struct lock_list this;
2113 __bfs_init_root(&this, class);
2115 raw_local_irq_save(flags);
2117 ret = __lockdep_count_backward_deps(&this);
2119 raw_local_irq_restore(flags);
2125 * Check that the dependency graph starting at <src> can lead to
2128 static noinline enum bfs_result
2129 check_path(struct held_lock *target, struct lock_list *src_entry,
2130 bool (*match)(struct lock_list *entry, void *data),
2131 bool (*skip)(struct lock_list *entry, void *data),
2132 struct lock_list **target_entry)
2134 enum bfs_result ret;
2136 ret = __bfs_forwards(src_entry, target, match, skip, target_entry);
2138 if (unlikely(bfs_error(ret)))
2145 * Prove that the dependency graph starting at <src> can not
2146 * lead to <target>. If it can, there is a circle when adding
2147 * <target> -> <src> dependency.
2149 * Print an error and return BFS_RMATCH if it does.
2151 static noinline enum bfs_result
2152 check_noncircular(struct held_lock *src, struct held_lock *target,
2153 struct lock_trace **const trace)
2155 enum bfs_result ret;
2156 struct lock_list *target_entry;
2157 struct lock_list src_entry;
2159 bfs_init_root(&src_entry, src);
2161 debug_atomic_inc(nr_cyclic_checks);
2163 ret = check_path(target, &src_entry, hlock_conflict, NULL, &target_entry);
2165 if (unlikely(ret == BFS_RMATCH)) {
2168 * If save_trace fails here, the printing might
2169 * trigger a WARN but because of the !nr_entries it
2170 * should not do bad things.
2172 *trace = save_trace();
2175 print_circular_bug(&src_entry, target_entry, src, target);
2181 #ifdef CONFIG_TRACE_IRQFLAGS
2184 * Forwards and backwards subgraph searching, for the purposes of
2185 * proving that two subgraphs can be connected by a new dependency
2186 * without creating any illegal irq-safe -> irq-unsafe lock dependency.
2188 * A irq safe->unsafe deadlock happens with the following conditions:
2190 * 1) We have a strong dependency path A -> ... -> B
2192 * 2) and we have ENABLED_IRQ usage of B and USED_IN_IRQ usage of A, therefore
2193 * irq can create a new dependency B -> A (consider the case that a holder
2194 * of B gets interrupted by an irq whose handler will try to acquire A).
2196 * 3) the dependency circle A -> ... -> B -> A we get from 1) and 2) is a
2199 * For the usage bits of B:
2200 * a) if A -> B is -(*N)->, then B -> A could be any type, so any
2201 * ENABLED_IRQ usage suffices.
2202 * b) if A -> B is -(*R)->, then B -> A must be -(E*)->, so only
2203 * ENABLED_IRQ_*_READ usage suffices.
2205 * For the usage bits of A:
2206 * c) if A -> B is -(E*)->, then B -> A could be any type, so any
2207 * USED_IN_IRQ usage suffices.
2208 * d) if A -> B is -(S*)->, then B -> A must be -(*N)->, so only
2209 * USED_IN_IRQ_*_READ usage suffices.
2213 * There is a strong dependency path in the dependency graph: A -> B, and now
2214 * we need to decide which usage bit of A should be accumulated to detect
2215 * safe->unsafe bugs.
2217 * Note that usage_accumulate() is used in backwards search, so ->only_xr
2218 * stands for whether A -> B only has -(S*)-> (in this case ->only_xr is true).
2220 * As above, if only_xr is false, which means A -> B has -(E*)-> dependency
2221 * path, any usage of A should be considered. Otherwise, we should only
2222 * consider _READ usage.
2224 static inline bool usage_accumulate(struct lock_list *entry, void *mask)
2226 if (!entry->only_xr)
2227 *(unsigned long *)mask |= entry->class->usage_mask;
2228 else /* Mask out _READ usage bits */
2229 *(unsigned long *)mask |= (entry->class->usage_mask & LOCKF_IRQ);
2235 * There is a strong dependency path in the dependency graph: A -> B, and now
2236 * we need to decide which usage bit of B conflicts with the usage bits of A,
2237 * i.e. which usage bit of B may introduce safe->unsafe deadlocks.
2239 * As above, if only_xr is false, which means A -> B has -(*N)-> dependency
2240 * path, any usage of B should be considered. Otherwise, we should only
2241 * consider _READ usage.
2243 static inline bool usage_match(struct lock_list *entry, void *mask)
2245 if (!entry->only_xr)
2246 return !!(entry->class->usage_mask & *(unsigned long *)mask);
2247 else /* Mask out _READ usage bits */
2248 return !!((entry->class->usage_mask & LOCKF_IRQ) & *(unsigned long *)mask);
2251 static inline bool usage_skip(struct lock_list *entry, void *mask)
2254 * Skip local_lock() for irq inversion detection.
2256 * For !RT, local_lock() is not a real lock, so it won't carry any
2259 * For RT, an irq inversion happens when we have lock A and B, and on
2260 * some CPU we can have:
2266 * where lock(B) cannot sleep, and we have a dependency B -> ... -> A.
2268 * Now we prove local_lock() cannot exist in that dependency. First we
2269 * have the observation for any lock chain L1 -> ... -> Ln, for any
2270 * 1 <= i <= n, Li.inner_wait_type <= L1.inner_wait_type, otherwise
2271 * wait context check will complain. And since B is not a sleep lock,
2272 * therefore B.inner_wait_type >= 2, and since the inner_wait_type of
2273 * local_lock() is 3, which is greater than 2, therefore there is no
2274 * way the local_lock() exists in the dependency B -> ... -> A.
2276 * As a result, we will skip local_lock(), when we search for irq
2279 if (entry->class->lock_type == LD_LOCK_PERCPU) {
2280 if (DEBUG_LOCKS_WARN_ON(entry->class->wait_type_inner < LD_WAIT_CONFIG))
2290 * Find a node in the forwards-direction dependency sub-graph starting
2291 * at @root->class that matches @bit.
2293 * Return BFS_MATCH if such a node exists in the subgraph, and put that node
2294 * into *@target_entry.
2296 static enum bfs_result
2297 find_usage_forwards(struct lock_list *root, unsigned long usage_mask,
2298 struct lock_list **target_entry)
2300 enum bfs_result result;
2302 debug_atomic_inc(nr_find_usage_forwards_checks);
2304 result = __bfs_forwards(root, &usage_mask, usage_match, usage_skip, target_entry);
2310 * Find a node in the backwards-direction dependency sub-graph starting
2311 * at @root->class that matches @bit.
2313 static enum bfs_result
2314 find_usage_backwards(struct lock_list *root, unsigned long usage_mask,
2315 struct lock_list **target_entry)
2317 enum bfs_result result;
2319 debug_atomic_inc(nr_find_usage_backwards_checks);
2321 result = __bfs_backwards(root, &usage_mask, usage_match, usage_skip, target_entry);
2326 static void print_lock_class_header(struct lock_class *class, int depth)
2330 printk("%*s->", depth, "");
2331 print_lock_name(class);
2332 #ifdef CONFIG_DEBUG_LOCKDEP
2333 printk(KERN_CONT " ops: %lu", debug_class_ops_read(class));
2335 printk(KERN_CONT " {\n");
2337 for (bit = 0; bit < LOCK_TRACE_STATES; bit++) {
2338 if (class->usage_mask & (1 << bit)) {
2341 len += printk("%*s %s", depth, "", usage_str[bit]);
2342 len += printk(KERN_CONT " at:\n");
2343 print_lock_trace(class->usage_traces[bit], len);
2346 printk("%*s }\n", depth, "");
2348 printk("%*s ... key at: [<%px>] %pS\n",
2349 depth, "", class->key, class->key);
2353 * Dependency path printing:
2355 * After BFS we get a lock dependency path (linked via ->parent of lock_list),
2356 * printing out each lock in the dependency path will help on understanding how
2357 * the deadlock could happen. Here are some details about dependency path
2360 * 1) A lock_list can be either forwards or backwards for a lock dependency,
2361 * for a lock dependency A -> B, there are two lock_lists:
2363 * a) lock_list in the ->locks_after list of A, whose ->class is B and
2364 * ->links_to is A. In this case, we can say the lock_list is
2365 * "A -> B" (forwards case).
2367 * b) lock_list in the ->locks_before list of B, whose ->class is A
2368 * and ->links_to is B. In this case, we can say the lock_list is
2369 * "B <- A" (bacwards case).
2371 * The ->trace of both a) and b) point to the call trace where B was
2372 * acquired with A held.
2374 * 2) A "helper" lock_list is introduced during BFS, this lock_list doesn't
2375 * represent a certain lock dependency, it only provides an initial entry
2376 * for BFS. For example, BFS may introduce a "helper" lock_list whose
2377 * ->class is A, as a result BFS will search all dependencies starting with
2378 * A, e.g. A -> B or A -> C.
2380 * The notation of a forwards helper lock_list is like "-> A", which means
2381 * we should search the forwards dependencies starting with "A", e.g A -> B
2384 * The notation of a bacwards helper lock_list is like "<- B", which means
2385 * we should search the backwards dependencies ending with "B", e.g.
2390 * printk the shortest lock dependencies from @root to @leaf in reverse order.
2392 * We have a lock dependency path as follow:
2398 * | lock_list | <--------- | lock_list | ... | lock_list | <--------- | lock_list |
2399 * | -> L1 | | L1 -> L2 | ... |Ln-2 -> Ln-1| | Ln-1 -> Ln|
2401 * , so it's natural that we start from @leaf and print every ->class and
2402 * ->trace until we reach the @root.
2405 print_shortest_lock_dependencies(struct lock_list *leaf,
2406 struct lock_list *root)
2408 struct lock_list *entry = leaf;
2411 /*compute depth from generated tree by BFS*/
2412 depth = get_lock_depth(leaf);
2415 print_lock_class_header(entry->class, depth);
2416 printk("%*s ... acquired at:\n", depth, "");
2417 print_lock_trace(entry->trace, 2);
2420 if (depth == 0 && (entry != root)) {
2421 printk("lockdep:%s bad path found in chain graph\n", __func__);
2425 entry = get_lock_parent(entry);
2427 } while (entry && (depth >= 0));
2431 * printk the shortest lock dependencies from @leaf to @root.
2433 * We have a lock dependency path (from a backwards search) as follow:
2439 * | lock_list | ---------> | lock_list | ... | lock_list | ---------> | lock_list |
2440 * | L2 <- L1 | | L3 <- L2 | ... | Ln <- Ln-1 | | <- Ln |
2442 * , so when we iterate from @leaf to @root, we actually print the lock
2443 * dependency path L1 -> L2 -> .. -> Ln in the non-reverse order.
2445 * Another thing to notice here is that ->class of L2 <- L1 is L1, while the
2446 * ->trace of L2 <- L1 is the call trace of L2, in fact we don't have the call
2447 * trace of L1 in the dependency path, which is alright, because most of the
2448 * time we can figure out where L1 is held from the call trace of L2.
2451 print_shortest_lock_dependencies_backwards(struct lock_list *leaf,
2452 struct lock_list *root)
2454 struct lock_list *entry = leaf;
2455 const struct lock_trace *trace = NULL;
2458 /*compute depth from generated tree by BFS*/
2459 depth = get_lock_depth(leaf);
2462 print_lock_class_header(entry->class, depth);
2464 printk("%*s ... acquired at:\n", depth, "");
2465 print_lock_trace(trace, 2);
2470 * Record the pointer to the trace for the next lock_list
2471 * entry, see the comments for the function.
2473 trace = entry->trace;
2475 if (depth == 0 && (entry != root)) {
2476 printk("lockdep:%s bad path found in chain graph\n", __func__);
2480 entry = get_lock_parent(entry);
2482 } while (entry && (depth >= 0));
2486 print_irq_lock_scenario(struct lock_list *safe_entry,
2487 struct lock_list *unsafe_entry,
2488 struct lock_class *prev_class,
2489 struct lock_class *next_class)
2491 struct lock_class *safe_class = safe_entry->class;
2492 struct lock_class *unsafe_class = unsafe_entry->class;
2493 struct lock_class *middle_class = prev_class;
2495 if (middle_class == safe_class)
2496 middle_class = next_class;
2499 * A direct locking problem where unsafe_class lock is taken
2500 * directly by safe_class lock, then all we need to show
2501 * is the deadlock scenario, as it is obvious that the
2502 * unsafe lock is taken under the safe lock.
2504 * But if there is a chain instead, where the safe lock takes
2505 * an intermediate lock (middle_class) where this lock is
2506 * not the same as the safe lock, then the lock chain is
2507 * used to describe the problem. Otherwise we would need
2508 * to show a different CPU case for each link in the chain
2509 * from the safe_class lock to the unsafe_class lock.
2511 if (middle_class != unsafe_class) {
2512 printk("Chain exists of:\n ");
2513 __print_lock_name(safe_class);
2514 printk(KERN_CONT " --> ");
2515 __print_lock_name(middle_class);
2516 printk(KERN_CONT " --> ");
2517 __print_lock_name(unsafe_class);
2518 printk(KERN_CONT "\n\n");
2521 printk(" Possible interrupt unsafe locking scenario:\n\n");
2522 printk(" CPU0 CPU1\n");
2523 printk(" ---- ----\n");
2525 __print_lock_name(unsafe_class);
2526 printk(KERN_CONT ");\n");
2527 printk(" local_irq_disable();\n");
2529 __print_lock_name(safe_class);
2530 printk(KERN_CONT ");\n");
2532 __print_lock_name(middle_class);
2533 printk(KERN_CONT ");\n");
2534 printk(" <Interrupt>\n");
2536 __print_lock_name(safe_class);
2537 printk(KERN_CONT ");\n");
2538 printk("\n *** DEADLOCK ***\n\n");
2542 print_bad_irq_dependency(struct task_struct *curr,
2543 struct lock_list *prev_root,
2544 struct lock_list *next_root,
2545 struct lock_list *backwards_entry,
2546 struct lock_list *forwards_entry,
2547 struct held_lock *prev,
2548 struct held_lock *next,
2549 enum lock_usage_bit bit1,
2550 enum lock_usage_bit bit2,
2551 const char *irqclass)
2553 if (!debug_locks_off_graph_unlock() || debug_locks_silent)
2557 pr_warn("=====================================================\n");
2558 pr_warn("WARNING: %s-safe -> %s-unsafe lock order detected\n",
2559 irqclass, irqclass);
2560 print_kernel_ident();
2561 pr_warn("-----------------------------------------------------\n");
2562 pr_warn("%s/%d [HC%u[%lu]:SC%u[%lu]:HE%u:SE%u] is trying to acquire:\n",
2563 curr->comm, task_pid_nr(curr),
2564 lockdep_hardirq_context(), hardirq_count() >> HARDIRQ_SHIFT,
2565 curr->softirq_context, softirq_count() >> SOFTIRQ_SHIFT,
2566 lockdep_hardirqs_enabled(),
2567 curr->softirqs_enabled);
2570 pr_warn("\nand this task is already holding:\n");
2572 pr_warn("which would create a new lock dependency:\n");
2573 print_lock_name(hlock_class(prev));
2575 print_lock_name(hlock_class(next));
2578 pr_warn("\nbut this new dependency connects a %s-irq-safe lock:\n",
2580 print_lock_name(backwards_entry->class);
2581 pr_warn("\n... which became %s-irq-safe at:\n", irqclass);
2583 print_lock_trace(backwards_entry->class->usage_traces[bit1], 1);
2585 pr_warn("\nto a %s-irq-unsafe lock:\n", irqclass);
2586 print_lock_name(forwards_entry->class);
2587 pr_warn("\n... which became %s-irq-unsafe at:\n", irqclass);
2590 print_lock_trace(forwards_entry->class->usage_traces[bit2], 1);
2592 pr_warn("\nother info that might help us debug this:\n\n");
2593 print_irq_lock_scenario(backwards_entry, forwards_entry,
2594 hlock_class(prev), hlock_class(next));
2596 lockdep_print_held_locks(curr);
2598 pr_warn("\nthe dependencies between %s-irq-safe lock and the holding lock:\n", irqclass);
2599 print_shortest_lock_dependencies_backwards(backwards_entry, prev_root);
2601 pr_warn("\nthe dependencies between the lock to be acquired");
2602 pr_warn(" and %s-irq-unsafe lock:\n", irqclass);
2603 next_root->trace = save_trace();
2604 if (!next_root->trace)
2606 print_shortest_lock_dependencies(forwards_entry, next_root);
2608 pr_warn("\nstack backtrace:\n");
2612 static const char *state_names[] = {
2613 #define LOCKDEP_STATE(__STATE) \
2614 __stringify(__STATE),
2615 #include "lockdep_states.h"
2616 #undef LOCKDEP_STATE
2619 static const char *state_rnames[] = {
2620 #define LOCKDEP_STATE(__STATE) \
2621 __stringify(__STATE)"-READ",
2622 #include "lockdep_states.h"
2623 #undef LOCKDEP_STATE
2626 static inline const char *state_name(enum lock_usage_bit bit)
2628 if (bit & LOCK_USAGE_READ_MASK)
2629 return state_rnames[bit >> LOCK_USAGE_DIR_MASK];
2631 return state_names[bit >> LOCK_USAGE_DIR_MASK];
2635 * The bit number is encoded like:
2637 * bit0: 0 exclusive, 1 read lock
2638 * bit1: 0 used in irq, 1 irq enabled
2641 static int exclusive_bit(int new_bit)
2643 int state = new_bit & LOCK_USAGE_STATE_MASK;
2644 int dir = new_bit & LOCK_USAGE_DIR_MASK;
2647 * keep state, bit flip the direction and strip read.
2649 return state | (dir ^ LOCK_USAGE_DIR_MASK);
2653 * Observe that when given a bitmask where each bitnr is encoded as above, a
2654 * right shift of the mask transforms the individual bitnrs as -1 and
2655 * conversely, a left shift transforms into +1 for the individual bitnrs.
2657 * So for all bits whose number have LOCK_ENABLED_* set (bitnr1 == 1), we can
2658 * create the mask with those bit numbers using LOCK_USED_IN_* (bitnr1 == 0)
2659 * instead by subtracting the bit number by 2, or shifting the mask right by 2.
2661 * Similarly, bitnr1 == 0 becomes bitnr1 == 1 by adding 2, or shifting left 2.
2663 * So split the mask (note that LOCKF_ENABLED_IRQ_ALL|LOCKF_USED_IN_IRQ_ALL is
2664 * all bits set) and recompose with bitnr1 flipped.
2666 static unsigned long invert_dir_mask(unsigned long mask)
2668 unsigned long excl = 0;
2671 excl |= (mask & LOCKF_ENABLED_IRQ_ALL) >> LOCK_USAGE_DIR_MASK;
2672 excl |= (mask & LOCKF_USED_IN_IRQ_ALL) << LOCK_USAGE_DIR_MASK;
2678 * Note that a LOCK_ENABLED_IRQ_*_READ usage and a LOCK_USED_IN_IRQ_*_READ
2679 * usage may cause deadlock too, for example:
2683 * write_lock(l1); <irq enabled>
2689 * , in above case, l1 will be marked as LOCK_USED_IN_IRQ_HARDIRQ_READ and l2
2690 * will marked as LOCK_ENABLE_IRQ_HARDIRQ_READ, and this is a possible
2693 * In fact, all of the following cases may cause deadlocks:
2695 * LOCK_USED_IN_IRQ_* -> LOCK_ENABLED_IRQ_*
2696 * LOCK_USED_IN_IRQ_*_READ -> LOCK_ENABLED_IRQ_*
2697 * LOCK_USED_IN_IRQ_* -> LOCK_ENABLED_IRQ_*_READ
2698 * LOCK_USED_IN_IRQ_*_READ -> LOCK_ENABLED_IRQ_*_READ
2700 * As a result, to calculate the "exclusive mask", first we invert the
2701 * direction (USED_IN/ENABLED) of the original mask, and 1) for all bits with
2702 * bitnr0 set (LOCK_*_READ), add those with bitnr0 cleared (LOCK_*). 2) for all
2703 * bits with bitnr0 cleared (LOCK_*_READ), add those with bitnr0 set (LOCK_*).
2705 static unsigned long exclusive_mask(unsigned long mask)
2707 unsigned long excl = invert_dir_mask(mask);
2709 excl |= (excl & LOCKF_IRQ_READ) >> LOCK_USAGE_READ_MASK;
2710 excl |= (excl & LOCKF_IRQ) << LOCK_USAGE_READ_MASK;
2716 * Retrieve the _possible_ original mask to which @mask is
2717 * exclusive. Ie: this is the opposite of exclusive_mask().
2718 * Note that 2 possible original bits can match an exclusive
2719 * bit: one has LOCK_USAGE_READ_MASK set, the other has it
2720 * cleared. So both are returned for each exclusive bit.
2722 static unsigned long original_mask(unsigned long mask)
2724 unsigned long excl = invert_dir_mask(mask);
2726 /* Include read in existing usages */
2727 excl |= (excl & LOCKF_IRQ_READ) >> LOCK_USAGE_READ_MASK;
2728 excl |= (excl & LOCKF_IRQ) << LOCK_USAGE_READ_MASK;
2734 * Find the first pair of bit match between an original
2735 * usage mask and an exclusive usage mask.
2737 static int find_exclusive_match(unsigned long mask,
2738 unsigned long excl_mask,
2739 enum lock_usage_bit *bitp,
2740 enum lock_usage_bit *excl_bitp)
2742 int bit, excl, excl_read;
2744 for_each_set_bit(bit, &mask, LOCK_USED) {
2746 * exclusive_bit() strips the read bit, however,
2747 * LOCK_ENABLED_IRQ_*_READ may cause deadlocks too, so we need
2748 * to search excl | LOCK_USAGE_READ_MASK as well.
2750 excl = exclusive_bit(bit);
2751 excl_read = excl | LOCK_USAGE_READ_MASK;
2752 if (excl_mask & lock_flag(excl)) {
2756 } else if (excl_mask & lock_flag(excl_read)) {
2758 *excl_bitp = excl_read;
2766 * Prove that the new dependency does not connect a hardirq-safe(-read)
2767 * lock with a hardirq-unsafe lock - to achieve this we search
2768 * the backwards-subgraph starting at <prev>, and the
2769 * forwards-subgraph starting at <next>:
2771 static int check_irq_usage(struct task_struct *curr, struct held_lock *prev,
2772 struct held_lock *next)
2774 unsigned long usage_mask = 0, forward_mask, backward_mask;
2775 enum lock_usage_bit forward_bit = 0, backward_bit = 0;
2776 struct lock_list *target_entry1;
2777 struct lock_list *target_entry;
2778 struct lock_list this, that;
2779 enum bfs_result ret;
2782 * Step 1: gather all hard/soft IRQs usages backward in an
2783 * accumulated usage mask.
2785 bfs_init_rootb(&this, prev);
2787 ret = __bfs_backwards(&this, &usage_mask, usage_accumulate, usage_skip, NULL);
2788 if (bfs_error(ret)) {
2793 usage_mask &= LOCKF_USED_IN_IRQ_ALL;
2798 * Step 2: find exclusive uses forward that match the previous
2799 * backward accumulated mask.
2801 forward_mask = exclusive_mask(usage_mask);
2803 bfs_init_root(&that, next);
2805 ret = find_usage_forwards(&that, forward_mask, &target_entry1);
2806 if (bfs_error(ret)) {
2810 if (ret == BFS_RNOMATCH)
2814 * Step 3: we found a bad match! Now retrieve a lock from the backward
2815 * list whose usage mask matches the exclusive usage mask from the
2816 * lock found on the forward list.
2818 * Note, we should only keep the LOCKF_ENABLED_IRQ_ALL bits, considering
2821 * When trying to add A -> B to the graph, we find that there is a
2822 * hardirq-safe L, that L -> ... -> A, and another hardirq-unsafe M,
2823 * that B -> ... -> M. However M is **softirq-safe**, if we use exact
2824 * invert bits of M's usage_mask, we will find another lock N that is
2825 * **softirq-unsafe** and N -> ... -> A, however N -> .. -> M will not
2826 * cause a inversion deadlock.
2828 backward_mask = original_mask(target_entry1->class->usage_mask & LOCKF_ENABLED_IRQ_ALL);
2830 ret = find_usage_backwards(&this, backward_mask, &target_entry);
2831 if (bfs_error(ret)) {
2835 if (DEBUG_LOCKS_WARN_ON(ret == BFS_RNOMATCH))
2839 * Step 4: narrow down to a pair of incompatible usage bits
2842 ret = find_exclusive_match(target_entry->class->usage_mask,
2843 target_entry1->class->usage_mask,
2844 &backward_bit, &forward_bit);
2845 if (DEBUG_LOCKS_WARN_ON(ret == -1))
2848 print_bad_irq_dependency(curr, &this, &that,
2849 target_entry, target_entry1,
2851 backward_bit, forward_bit,
2852 state_name(backward_bit));
2859 static inline int check_irq_usage(struct task_struct *curr,
2860 struct held_lock *prev, struct held_lock *next)
2865 static inline bool usage_skip(struct lock_list *entry, void *mask)
2870 #endif /* CONFIG_TRACE_IRQFLAGS */
2872 #ifdef CONFIG_LOCKDEP_SMALL
2874 * Check that the dependency graph starting at <src> can lead to
2875 * <target> or not. If it can, <src> -> <target> dependency is already
2878 * Return BFS_RMATCH if it does, or BFS_RNOMATCH if it does not, return BFS_E* if
2879 * any error appears in the bfs search.
2881 static noinline enum bfs_result
2882 check_redundant(struct held_lock *src, struct held_lock *target)
2884 enum bfs_result ret;
2885 struct lock_list *target_entry;
2886 struct lock_list src_entry;
2888 bfs_init_root(&src_entry, src);
2890 * Special setup for check_redundant().
2892 * To report redundant, we need to find a strong dependency path that
2893 * is equal to or stronger than <src> -> <target>. So if <src> is E,
2894 * we need to let __bfs() only search for a path starting at a -(E*)->,
2895 * we achieve this by setting the initial node's ->only_xr to true in
2896 * that case. And if <prev> is S, we set initial ->only_xr to false
2897 * because both -(S*)-> (equal) and -(E*)-> (stronger) are redundant.
2899 src_entry.only_xr = src->read == 0;
2901 debug_atomic_inc(nr_redundant_checks);
2904 * Note: we skip local_lock() for redundant check, because as the
2905 * comment in usage_skip(), A -> local_lock() -> B and A -> B are not
2908 ret = check_path(target, &src_entry, hlock_equal, usage_skip, &target_entry);
2910 if (ret == BFS_RMATCH)
2911 debug_atomic_inc(nr_redundant);
2918 static inline enum bfs_result
2919 check_redundant(struct held_lock *src, struct held_lock *target)
2921 return BFS_RNOMATCH;
2926 static void inc_chains(int irq_context)
2928 if (irq_context & LOCK_CHAIN_HARDIRQ_CONTEXT)
2929 nr_hardirq_chains++;
2930 else if (irq_context & LOCK_CHAIN_SOFTIRQ_CONTEXT)
2931 nr_softirq_chains++;
2933 nr_process_chains++;
2936 static void dec_chains(int irq_context)
2938 if (irq_context & LOCK_CHAIN_HARDIRQ_CONTEXT)
2939 nr_hardirq_chains--;
2940 else if (irq_context & LOCK_CHAIN_SOFTIRQ_CONTEXT)
2941 nr_softirq_chains--;
2943 nr_process_chains--;
2947 print_deadlock_scenario(struct held_lock *nxt, struct held_lock *prv)
2949 struct lock_class *next = hlock_class(nxt);
2950 struct lock_class *prev = hlock_class(prv);
2952 printk(" Possible unsafe locking scenario:\n\n");
2956 __print_lock_name(prev);
2957 printk(KERN_CONT ");\n");
2959 __print_lock_name(next);
2960 printk(KERN_CONT ");\n");
2961 printk("\n *** DEADLOCK ***\n\n");
2962 printk(" May be due to missing lock nesting notation\n\n");
2966 print_deadlock_bug(struct task_struct *curr, struct held_lock *prev,
2967 struct held_lock *next)
2969 if (!debug_locks_off_graph_unlock() || debug_locks_silent)
2973 pr_warn("============================================\n");
2974 pr_warn("WARNING: possible recursive locking detected\n");
2975 print_kernel_ident();
2976 pr_warn("--------------------------------------------\n");
2977 pr_warn("%s/%d is trying to acquire lock:\n",
2978 curr->comm, task_pid_nr(curr));
2980 pr_warn("\nbut task is already holding lock:\n");
2983 pr_warn("\nother info that might help us debug this:\n");
2984 print_deadlock_scenario(next, prev);
2985 lockdep_print_held_locks(curr);
2987 pr_warn("\nstack backtrace:\n");
2992 * Check whether we are holding such a class already.
2994 * (Note that this has to be done separately, because the graph cannot
2995 * detect such classes of deadlocks.)
2997 * Returns: 0 on deadlock detected, 1 on OK, 2 if another lock with the same
2998 * lock class is held but nest_lock is also held, i.e. we rely on the
2999 * nest_lock to avoid the deadlock.
3002 check_deadlock(struct task_struct *curr, struct held_lock *next)
3004 struct held_lock *prev;
3005 struct held_lock *nest = NULL;
3008 for (i = 0; i < curr->lockdep_depth; i++) {
3009 prev = curr->held_locks + i;
3011 if (prev->instance == next->nest_lock)
3014 if (hlock_class(prev) != hlock_class(next))
3018 * Allow read-after-read recursion of the same
3019 * lock class (i.e. read_lock(lock)+read_lock(lock)):
3021 if ((next->read == 2) && prev->read)
3025 * We're holding the nest_lock, which serializes this lock's
3026 * nesting behaviour.
3031 print_deadlock_bug(curr, prev, next);
3038 * There was a chain-cache miss, and we are about to add a new dependency
3039 * to a previous lock. We validate the following rules:
3041 * - would the adding of the <prev> -> <next> dependency create a
3042 * circular dependency in the graph? [== circular deadlock]
3044 * - does the new prev->next dependency connect any hardirq-safe lock
3045 * (in the full backwards-subgraph starting at <prev>) with any
3046 * hardirq-unsafe lock (in the full forwards-subgraph starting at
3047 * <next>)? [== illegal lock inversion with hardirq contexts]
3049 * - does the new prev->next dependency connect any softirq-safe lock
3050 * (in the full backwards-subgraph starting at <prev>) with any
3051 * softirq-unsafe lock (in the full forwards-subgraph starting at
3052 * <next>)? [== illegal lock inversion with softirq contexts]
3054 * any of these scenarios could lead to a deadlock.
3056 * Then if all the validations pass, we add the forwards and backwards
3060 check_prev_add(struct task_struct *curr, struct held_lock *prev,
3061 struct held_lock *next, u16 distance,
3062 struct lock_trace **const trace)
3064 struct lock_list *entry;
3065 enum bfs_result ret;
3067 if (!hlock_class(prev)->key || !hlock_class(next)->key) {
3069 * The warning statements below may trigger a use-after-free
3070 * of the class name. It is better to trigger a use-after free
3071 * and to have the class name most of the time instead of not
3072 * having the class name available.
3074 WARN_ONCE(!debug_locks_silent && !hlock_class(prev)->key,
3075 "Detected use-after-free of lock class %px/%s\n",
3077 hlock_class(prev)->name);
3078 WARN_ONCE(!debug_locks_silent && !hlock_class(next)->key,
3079 "Detected use-after-free of lock class %px/%s\n",
3081 hlock_class(next)->name);
3086 * Prove that the new <prev> -> <next> dependency would not
3087 * create a circular dependency in the graph. (We do this by
3088 * a breadth-first search into the graph starting at <next>,
3089 * and check whether we can reach <prev>.)
3091 * The search is limited by the size of the circular queue (i.e.,
3092 * MAX_CIRCULAR_QUEUE_SIZE) which keeps track of a breadth of nodes
3093 * in the graph whose neighbours are to be checked.
3095 ret = check_noncircular(next, prev, trace);
3096 if (unlikely(bfs_error(ret) || ret == BFS_RMATCH))
3099 if (!check_irq_usage(curr, prev, next))
3103 * Is the <prev> -> <next> dependency already present?
3105 * (this may occur even though this is a new chain: consider
3106 * e.g. the L1 -> L2 -> L3 -> L4 and the L5 -> L1 -> L2 -> L3
3107 * chains - the second one will be new, but L1 already has
3108 * L2 added to its dependency list, due to the first chain.)
3110 list_for_each_entry(entry, &hlock_class(prev)->locks_after, entry) {
3111 if (entry->class == hlock_class(next)) {
3113 entry->distance = 1;
3114 entry->dep |= calc_dep(prev, next);
3117 * Also, update the reverse dependency in @next's
3118 * ->locks_before list.
3120 * Here we reuse @entry as the cursor, which is fine
3121 * because we won't go to the next iteration of the
3124 * For normal cases, we return in the inner loop.
3126 * If we fail to return, we have inconsistency, i.e.
3127 * <prev>::locks_after contains <next> while
3128 * <next>::locks_before doesn't contain <prev>. In
3129 * that case, we return after the inner and indicate
3130 * something is wrong.
3132 list_for_each_entry(entry, &hlock_class(next)->locks_before, entry) {
3133 if (entry->class == hlock_class(prev)) {
3135 entry->distance = 1;
3136 entry->dep |= calc_depb(prev, next);
3141 /* <prev> is not found in <next>::locks_before */
3147 * Is the <prev> -> <next> link redundant?
3149 ret = check_redundant(prev, next);
3152 else if (ret == BFS_RMATCH)
3156 *trace = save_trace();
3162 * Ok, all validations passed, add the new lock
3163 * to the previous lock's dependency list:
3165 ret = add_lock_to_list(hlock_class(next), hlock_class(prev),
3166 &hlock_class(prev)->locks_after, distance,
3167 calc_dep(prev, next), *trace);
3172 ret = add_lock_to_list(hlock_class(prev), hlock_class(next),
3173 &hlock_class(next)->locks_before, distance,
3174 calc_depb(prev, next), *trace);
3182 * Add the dependency to all directly-previous locks that are 'relevant'.
3183 * The ones that are relevant are (in increasing distance from curr):
3184 * all consecutive trylock entries and the final non-trylock entry - or
3185 * the end of this context's lock-chain - whichever comes first.
3188 check_prevs_add(struct task_struct *curr, struct held_lock *next)
3190 struct lock_trace *trace = NULL;
3191 int depth = curr->lockdep_depth;
3192 struct held_lock *hlock;
3197 * Depth must not be zero for a non-head lock:
3202 * At least two relevant locks must exist for this
3205 if (curr->held_locks[depth].irq_context !=
3206 curr->held_locks[depth-1].irq_context)
3210 u16 distance = curr->lockdep_depth - depth + 1;
3211 hlock = curr->held_locks + depth - 1;
3214 int ret = check_prev_add(curr, hlock, next, distance, &trace);
3219 * Stop after the first non-trylock entry,
3220 * as non-trylock entries have added their
3221 * own direct dependencies already, so this
3222 * lock is connected to them indirectly:
3224 if (!hlock->trylock)
3230 * End of lock-stack?
3235 * Stop the search if we cross into another context:
3237 if (curr->held_locks[depth].irq_context !=
3238 curr->held_locks[depth-1].irq_context)
3243 if (!debug_locks_off_graph_unlock())
3247 * Clearly we all shouldn't be here, but since we made it we
3248 * can reliable say we messed up our state. See the above two
3249 * gotos for reasons why we could possibly end up here.
3256 struct lock_chain lock_chains[MAX_LOCKDEP_CHAINS];
3257 static DECLARE_BITMAP(lock_chains_in_use, MAX_LOCKDEP_CHAINS);
3258 static u16 chain_hlocks[MAX_LOCKDEP_CHAIN_HLOCKS];
3259 unsigned long nr_zapped_lock_chains;
3260 unsigned int nr_free_chain_hlocks; /* Free chain_hlocks in buckets */
3261 unsigned int nr_lost_chain_hlocks; /* Lost chain_hlocks */
3262 unsigned int nr_large_chain_blocks; /* size > MAX_CHAIN_BUCKETS */
3265 * The first 2 chain_hlocks entries in the chain block in the bucket
3266 * list contains the following meta data:
3269 * Bit 15 - always set to 1 (it is not a class index)
3270 * Bits 0-14 - upper 15 bits of the next block index
3271 * entry[1] - lower 16 bits of next block index
3273 * A next block index of all 1 bits means it is the end of the list.
3275 * On the unsized bucket (bucket-0), the 3rd and 4th entries contain
3276 * the chain block size:
3278 * entry[2] - upper 16 bits of the chain block size
3279 * entry[3] - lower 16 bits of the chain block size
3281 #define MAX_CHAIN_BUCKETS 16
3282 #define CHAIN_BLK_FLAG (1U << 15)
3283 #define CHAIN_BLK_LIST_END 0xFFFFU
3285 static int chain_block_buckets[MAX_CHAIN_BUCKETS];
3287 static inline int size_to_bucket(int size)
3289 if (size > MAX_CHAIN_BUCKETS)
3296 * Iterate all the chain blocks in a bucket.
3298 #define for_each_chain_block(bucket, prev, curr) \
3299 for ((prev) = -1, (curr) = chain_block_buckets[bucket]; \
3301 (prev) = (curr), (curr) = chain_block_next(curr))
3306 static inline int chain_block_next(int offset)
3308 int next = chain_hlocks[offset];
3310 WARN_ON_ONCE(!(next & CHAIN_BLK_FLAG));
3312 if (next == CHAIN_BLK_LIST_END)
3315 next &= ~CHAIN_BLK_FLAG;
3317 next |= chain_hlocks[offset + 1];
3325 static inline int chain_block_size(int offset)
3327 return (chain_hlocks[offset + 2] << 16) | chain_hlocks[offset + 3];
3330 static inline void init_chain_block(int offset, int next, int bucket, int size)
3332 chain_hlocks[offset] = (next >> 16) | CHAIN_BLK_FLAG;
3333 chain_hlocks[offset + 1] = (u16)next;
3335 if (size && !bucket) {
3336 chain_hlocks[offset + 2] = size >> 16;
3337 chain_hlocks[offset + 3] = (u16)size;
3341 static inline void add_chain_block(int offset, int size)
3343 int bucket = size_to_bucket(size);
3344 int next = chain_block_buckets[bucket];
3347 if (unlikely(size < 2)) {
3349 * We can't store single entries on the freelist. Leak them.
3351 * One possible way out would be to uniquely mark them, other
3352 * than with CHAIN_BLK_FLAG, such that we can recover them when
3353 * the block before it is re-added.
3356 nr_lost_chain_hlocks++;
3360 nr_free_chain_hlocks += size;
3362 nr_large_chain_blocks++;
3365 * Variable sized, sort large to small.
3367 for_each_chain_block(0, prev, curr) {
3368 if (size >= chain_block_size(curr))
3371 init_chain_block(offset, curr, 0, size);
3373 chain_block_buckets[0] = offset;
3375 init_chain_block(prev, offset, 0, 0);
3379 * Fixed size, add to head.
3381 init_chain_block(offset, next, bucket, size);
3382 chain_block_buckets[bucket] = offset;
3386 * Only the first block in the list can be deleted.
3388 * For the variable size bucket[0], the first block (the largest one) is
3389 * returned, broken up and put back into the pool. So if a chain block of
3390 * length > MAX_CHAIN_BUCKETS is ever used and zapped, it will just be
3391 * queued up after the primordial chain block and never be used until the
3392 * hlock entries in the primordial chain block is almost used up. That
3393 * causes fragmentation and reduce allocation efficiency. That can be
3394 * monitored by looking at the "large chain blocks" number in lockdep_stats.
3396 static inline void del_chain_block(int bucket, int size, int next)
3398 nr_free_chain_hlocks -= size;
3399 chain_block_buckets[bucket] = next;
3402 nr_large_chain_blocks--;
3405 static void init_chain_block_buckets(void)
3409 for (i = 0; i < MAX_CHAIN_BUCKETS; i++)
3410 chain_block_buckets[i] = -1;
3412 add_chain_block(0, ARRAY_SIZE(chain_hlocks));
3416 * Return offset of a chain block of the right size or -1 if not found.
3418 * Fairly simple worst-fit allocator with the addition of a number of size
3419 * specific free lists.
3421 static int alloc_chain_hlocks(int req)
3423 int bucket, curr, size;
3426 * We rely on the MSB to act as an escape bit to denote freelist
3427 * pointers. Make sure this bit isn't set in 'normal' class_idx usage.
3429 BUILD_BUG_ON((MAX_LOCKDEP_KEYS-1) & CHAIN_BLK_FLAG);
3431 init_data_structures_once();
3433 if (nr_free_chain_hlocks < req)
3437 * We require a minimum of 2 (u16) entries to encode a freelist
3441 bucket = size_to_bucket(req);
3442 curr = chain_block_buckets[bucket];
3446 del_chain_block(bucket, req, chain_block_next(curr));
3450 curr = chain_block_buckets[0];
3454 * The variable sized freelist is sorted by size; the first entry is
3455 * the largest. Use it if it fits.
3458 size = chain_block_size(curr);
3459 if (likely(size >= req)) {
3460 del_chain_block(0, size, chain_block_next(curr));
3461 add_chain_block(curr + req, size - req);
3467 * Last resort, split a block in a larger sized bucket.
3469 for (size = MAX_CHAIN_BUCKETS; size > req; size--) {
3470 bucket = size_to_bucket(size);
3471 curr = chain_block_buckets[bucket];
3475 del_chain_block(bucket, size, chain_block_next(curr));
3476 add_chain_block(curr + req, size - req);
3483 static inline void free_chain_hlocks(int base, int size)
3485 add_chain_block(base, max(size, 2));
3488 struct lock_class *lock_chain_get_class(struct lock_chain *chain, int i)
3490 u16 chain_hlock = chain_hlocks[chain->base + i];
3491 unsigned int class_idx = chain_hlock_class_idx(chain_hlock);
3493 return lock_classes + class_idx;
3497 * Returns the index of the first held_lock of the current chain
3499 static inline int get_first_held_lock(struct task_struct *curr,
3500 struct held_lock *hlock)
3503 struct held_lock *hlock_curr;
3505 for (i = curr->lockdep_depth - 1; i >= 0; i--) {
3506 hlock_curr = curr->held_locks + i;
3507 if (hlock_curr->irq_context != hlock->irq_context)
3515 #ifdef CONFIG_DEBUG_LOCKDEP
3517 * Returns the next chain_key iteration
3519 static u64 print_chain_key_iteration(u16 hlock_id, u64 chain_key)
3521 u64 new_chain_key = iterate_chain_key(chain_key, hlock_id);
3523 printk(" hlock_id:%d -> chain_key:%016Lx",
3524 (unsigned int)hlock_id,
3525 (unsigned long long)new_chain_key);
3526 return new_chain_key;
3530 print_chain_keys_held_locks(struct task_struct *curr, struct held_lock *hlock_next)
3532 struct held_lock *hlock;
3533 u64 chain_key = INITIAL_CHAIN_KEY;
3534 int depth = curr->lockdep_depth;
3535 int i = get_first_held_lock(curr, hlock_next);
3537 printk("depth: %u (irq_context %u)\n", depth - i + 1,
3538 hlock_next->irq_context);
3539 for (; i < depth; i++) {
3540 hlock = curr->held_locks + i;
3541 chain_key = print_chain_key_iteration(hlock_id(hlock), chain_key);
3546 print_chain_key_iteration(hlock_id(hlock_next), chain_key);
3547 print_lock(hlock_next);
3550 static void print_chain_keys_chain(struct lock_chain *chain)
3553 u64 chain_key = INITIAL_CHAIN_KEY;
3556 printk("depth: %u\n", chain->depth);
3557 for (i = 0; i < chain->depth; i++) {
3558 hlock_id = chain_hlocks[chain->base + i];
3559 chain_key = print_chain_key_iteration(hlock_id, chain_key);
3561 print_lock_name(lock_classes + chain_hlock_class_idx(hlock_id));
3566 static void print_collision(struct task_struct *curr,
3567 struct held_lock *hlock_next,
3568 struct lock_chain *chain)
3571 pr_warn("============================\n");
3572 pr_warn("WARNING: chain_key collision\n");
3573 print_kernel_ident();
3574 pr_warn("----------------------------\n");
3575 pr_warn("%s/%d: ", current->comm, task_pid_nr(current));
3576 pr_warn("Hash chain already cached but the contents don't match!\n");
3578 pr_warn("Held locks:");
3579 print_chain_keys_held_locks(curr, hlock_next);
3581 pr_warn("Locks in cached chain:");
3582 print_chain_keys_chain(chain);
3584 pr_warn("\nstack backtrace:\n");
3590 * Checks whether the chain and the current held locks are consistent
3591 * in depth and also in content. If they are not it most likely means
3592 * that there was a collision during the calculation of the chain_key.
3593 * Returns: 0 not passed, 1 passed
3595 static int check_no_collision(struct task_struct *curr,
3596 struct held_lock *hlock,
3597 struct lock_chain *chain)
3599 #ifdef CONFIG_DEBUG_LOCKDEP
3602 i = get_first_held_lock(curr, hlock);
3604 if (DEBUG_LOCKS_WARN_ON(chain->depth != curr->lockdep_depth - (i - 1))) {
3605 print_collision(curr, hlock, chain);
3609 for (j = 0; j < chain->depth - 1; j++, i++) {
3610 id = hlock_id(&curr->held_locks[i]);
3612 if (DEBUG_LOCKS_WARN_ON(chain_hlocks[chain->base + j] != id)) {
3613 print_collision(curr, hlock, chain);
3622 * Given an index that is >= -1, return the index of the next lock chain.
3623 * Return -2 if there is no next lock chain.
3625 long lockdep_next_lockchain(long i)
3627 i = find_next_bit(lock_chains_in_use, ARRAY_SIZE(lock_chains), i + 1);
3628 return i < ARRAY_SIZE(lock_chains) ? i : -2;
3631 unsigned long lock_chain_count(void)
3633 return bitmap_weight(lock_chains_in_use, ARRAY_SIZE(lock_chains));
3636 /* Must be called with the graph lock held. */
3637 static struct lock_chain *alloc_lock_chain(void)
3639 int idx = find_first_zero_bit(lock_chains_in_use,
3640 ARRAY_SIZE(lock_chains));
3642 if (unlikely(idx >= ARRAY_SIZE(lock_chains)))
3644 __set_bit(idx, lock_chains_in_use);
3645 return lock_chains + idx;
3649 * Adds a dependency chain into chain hashtable. And must be called with
3652 * Return 0 if fail, and graph_lock is released.
3653 * Return 1 if succeed, with graph_lock held.
3655 static inline int add_chain_cache(struct task_struct *curr,
3656 struct held_lock *hlock,
3659 struct hlist_head *hash_head = chainhashentry(chain_key);
3660 struct lock_chain *chain;
3664 * The caller must hold the graph lock, ensure we've got IRQs
3665 * disabled to make this an IRQ-safe lock.. for recursion reasons
3666 * lockdep won't complain about its own locking errors.
3668 if (lockdep_assert_locked())
3671 chain = alloc_lock_chain();
3673 if (!debug_locks_off_graph_unlock())
3676 print_lockdep_off("BUG: MAX_LOCKDEP_CHAINS too low!");
3680 chain->chain_key = chain_key;
3681 chain->irq_context = hlock->irq_context;
3682 i = get_first_held_lock(curr, hlock);
3683 chain->depth = curr->lockdep_depth + 1 - i;
3685 BUILD_BUG_ON((1UL << 24) <= ARRAY_SIZE(chain_hlocks));
3686 BUILD_BUG_ON((1UL << 6) <= ARRAY_SIZE(curr->held_locks));
3687 BUILD_BUG_ON((1UL << 8*sizeof(chain_hlocks[0])) <= ARRAY_SIZE(lock_classes));
3689 j = alloc_chain_hlocks(chain->depth);
3691 if (!debug_locks_off_graph_unlock())
3694 print_lockdep_off("BUG: MAX_LOCKDEP_CHAIN_HLOCKS too low!");
3700 for (j = 0; j < chain->depth - 1; j++, i++) {
3701 int lock_id = hlock_id(curr->held_locks + i);
3703 chain_hlocks[chain->base + j] = lock_id;
3705 chain_hlocks[chain->base + j] = hlock_id(hlock);
3706 hlist_add_head_rcu(&chain->entry, hash_head);
3707 debug_atomic_inc(chain_lookup_misses);
3708 inc_chains(chain->irq_context);
3714 * Look up a dependency chain. Must be called with either the graph lock or
3715 * the RCU read lock held.
3717 static inline struct lock_chain *lookup_chain_cache(u64 chain_key)
3719 struct hlist_head *hash_head = chainhashentry(chain_key);
3720 struct lock_chain *chain;
3722 hlist_for_each_entry_rcu(chain, hash_head, entry) {
3723 if (READ_ONCE(chain->chain_key) == chain_key) {
3724 debug_atomic_inc(chain_lookup_hits);
3732 * If the key is not present yet in dependency chain cache then
3733 * add it and return 1 - in this case the new dependency chain is
3734 * validated. If the key is already hashed, return 0.
3735 * (On return with 1 graph_lock is held.)
3737 static inline int lookup_chain_cache_add(struct task_struct *curr,
3738 struct held_lock *hlock,
3741 struct lock_class *class = hlock_class(hlock);
3742 struct lock_chain *chain = lookup_chain_cache(chain_key);
3746 if (!check_no_collision(curr, hlock, chain))
3749 if (very_verbose(class)) {
3750 printk("\nhash chain already cached, key: "
3751 "%016Lx tail class: [%px] %s\n",
3752 (unsigned long long)chain_key,
3753 class->key, class->name);
3759 if (very_verbose(class)) {
3760 printk("\nnew hash chain, key: %016Lx tail class: [%px] %s\n",
3761 (unsigned long long)chain_key, class->key, class->name);
3768 * We have to walk the chain again locked - to avoid duplicates:
3770 chain = lookup_chain_cache(chain_key);
3776 if (!add_chain_cache(curr, hlock, chain_key))
3782 static int validate_chain(struct task_struct *curr,
3783 struct held_lock *hlock,
3784 int chain_head, u64 chain_key)
3787 * Trylock needs to maintain the stack of held locks, but it
3788 * does not add new dependencies, because trylock can be done
3791 * We look up the chain_key and do the O(N^2) check and update of
3792 * the dependencies only if this is a new dependency chain.
3793 * (If lookup_chain_cache_add() return with 1 it acquires
3794 * graph_lock for us)
3796 if (!hlock->trylock && hlock->check &&
3797 lookup_chain_cache_add(curr, hlock, chain_key)) {
3799 * Check whether last held lock:
3801 * - is irq-safe, if this lock is irq-unsafe
3802 * - is softirq-safe, if this lock is hardirq-unsafe
3804 * And check whether the new lock's dependency graph
3805 * could lead back to the previous lock:
3807 * - within the current held-lock stack
3808 * - across our accumulated lock dependency records
3810 * any of these scenarios could lead to a deadlock.
3813 * The simple case: does the current hold the same lock
3816 int ret = check_deadlock(curr, hlock);
3821 * Add dependency only if this lock is not the head
3822 * of the chain, and if the new lock introduces no more
3823 * lock dependency (because we already hold a lock with the
3824 * same lock class) nor deadlock (because the nest_lock
3825 * serializes nesting locks), see the comments for
3828 if (!chain_head && ret != 2) {
3829 if (!check_prevs_add(curr, hlock))
3835 /* after lookup_chain_cache_add(): */
3836 if (unlikely(!debug_locks))
3843 static inline int validate_chain(struct task_struct *curr,
3844 struct held_lock *hlock,
3845 int chain_head, u64 chain_key)
3850 static void init_chain_block_buckets(void) { }
3851 #endif /* CONFIG_PROVE_LOCKING */
3854 * We are building curr_chain_key incrementally, so double-check
3855 * it from scratch, to make sure that it's done correctly:
3857 static void check_chain_key(struct task_struct *curr)
3859 #ifdef CONFIG_DEBUG_LOCKDEP
3860 struct held_lock *hlock, *prev_hlock = NULL;
3862 u64 chain_key = INITIAL_CHAIN_KEY;
3864 for (i = 0; i < curr->lockdep_depth; i++) {
3865 hlock = curr->held_locks + i;
3866 if (chain_key != hlock->prev_chain_key) {
3869 * We got mighty confused, our chain keys don't match
3870 * with what we expect, someone trample on our task state?
3872 WARN(1, "hm#1, depth: %u [%u], %016Lx != %016Lx\n",
3873 curr->lockdep_depth, i,
3874 (unsigned long long)chain_key,
3875 (unsigned long long)hlock->prev_chain_key);
3880 * hlock->class_idx can't go beyond MAX_LOCKDEP_KEYS, but is
3881 * it registered lock class index?
3883 if (DEBUG_LOCKS_WARN_ON(!test_bit(hlock->class_idx, lock_classes_in_use)))
3886 if (prev_hlock && (prev_hlock->irq_context !=
3887 hlock->irq_context))
3888 chain_key = INITIAL_CHAIN_KEY;
3889 chain_key = iterate_chain_key(chain_key, hlock_id(hlock));
3892 if (chain_key != curr->curr_chain_key) {
3895 * More smoking hash instead of calculating it, damn see these
3896 * numbers float.. I bet that a pink elephant stepped on my memory.
3898 WARN(1, "hm#2, depth: %u [%u], %016Lx != %016Lx\n",
3899 curr->lockdep_depth, i,
3900 (unsigned long long)chain_key,
3901 (unsigned long long)curr->curr_chain_key);
3906 #ifdef CONFIG_PROVE_LOCKING
3907 static int mark_lock(struct task_struct *curr, struct held_lock *this,
3908 enum lock_usage_bit new_bit);
3910 static void print_usage_bug_scenario(struct held_lock *lock)
3912 struct lock_class *class = hlock_class(lock);
3914 printk(" Possible unsafe locking scenario:\n\n");
3918 __print_lock_name(class);
3919 printk(KERN_CONT ");\n");
3920 printk(" <Interrupt>\n");
3922 __print_lock_name(class);
3923 printk(KERN_CONT ");\n");
3924 printk("\n *** DEADLOCK ***\n\n");
3928 print_usage_bug(struct task_struct *curr, struct held_lock *this,
3929 enum lock_usage_bit prev_bit, enum lock_usage_bit new_bit)
3931 if (!debug_locks_off() || debug_locks_silent)
3935 pr_warn("================================\n");
3936 pr_warn("WARNING: inconsistent lock state\n");
3937 print_kernel_ident();
3938 pr_warn("--------------------------------\n");
3940 pr_warn("inconsistent {%s} -> {%s} usage.\n",
3941 usage_str[prev_bit], usage_str[new_bit]);
3943 pr_warn("%s/%d [HC%u[%lu]:SC%u[%lu]:HE%u:SE%u] takes:\n",
3944 curr->comm, task_pid_nr(curr),
3945 lockdep_hardirq_context(), hardirq_count() >> HARDIRQ_SHIFT,
3946 lockdep_softirq_context(curr), softirq_count() >> SOFTIRQ_SHIFT,
3947 lockdep_hardirqs_enabled(),
3948 lockdep_softirqs_enabled(curr));
3951 pr_warn("{%s} state was registered at:\n", usage_str[prev_bit]);
3952 print_lock_trace(hlock_class(this)->usage_traces[prev_bit], 1);
3954 print_irqtrace_events(curr);
3955 pr_warn("\nother info that might help us debug this:\n");
3956 print_usage_bug_scenario(this);
3958 lockdep_print_held_locks(curr);
3960 pr_warn("\nstack backtrace:\n");
3965 * Print out an error if an invalid bit is set:
3968 valid_state(struct task_struct *curr, struct held_lock *this,
3969 enum lock_usage_bit new_bit, enum lock_usage_bit bad_bit)
3971 if (unlikely(hlock_class(this)->usage_mask & (1 << bad_bit))) {
3973 print_usage_bug(curr, this, bad_bit, new_bit);
3981 * print irq inversion bug:
3984 print_irq_inversion_bug(struct task_struct *curr,
3985 struct lock_list *root, struct lock_list *other,
3986 struct held_lock *this, int forwards,
3987 const char *irqclass)
3989 struct lock_list *entry = other;
3990 struct lock_list *middle = NULL;
3993 if (!debug_locks_off_graph_unlock() || debug_locks_silent)
3997 pr_warn("========================================================\n");
3998 pr_warn("WARNING: possible irq lock inversion dependency detected\n");
3999 print_kernel_ident();
4000 pr_warn("--------------------------------------------------------\n");
4001 pr_warn("%s/%d just changed the state of lock:\n",
4002 curr->comm, task_pid_nr(curr));
4005 pr_warn("but this lock took another, %s-unsafe lock in the past:\n", irqclass);
4007 pr_warn("but this lock was taken by another, %s-safe lock in the past:\n", irqclass);
4008 print_lock_name(other->class);
4009 pr_warn("\n\nand interrupts could create inverse lock ordering between them.\n\n");
4011 pr_warn("\nother info that might help us debug this:\n");
4013 /* Find a middle lock (if one exists) */
4014 depth = get_lock_depth(other);
4016 if (depth == 0 && (entry != root)) {
4017 pr_warn("lockdep:%s bad path found in chain graph\n", __func__);
4021 entry = get_lock_parent(entry);
4023 } while (entry && entry != root && (depth >= 0));
4025 print_irq_lock_scenario(root, other,
4026 middle ? middle->class : root->class, other->class);
4028 print_irq_lock_scenario(other, root,
4029 middle ? middle->class : other->class, root->class);
4031 lockdep_print_held_locks(curr);
4033 pr_warn("\nthe shortest dependencies between 2nd lock and 1st lock:\n");
4034 root->trace = save_trace();
4037 print_shortest_lock_dependencies(other, root);
4039 pr_warn("\nstack backtrace:\n");
4044 * Prove that in the forwards-direction subgraph starting at <this>
4045 * there is no lock matching <mask>:
4048 check_usage_forwards(struct task_struct *curr, struct held_lock *this,
4049 enum lock_usage_bit bit)
4051 enum bfs_result ret;
4052 struct lock_list root;
4053 struct lock_list *target_entry;
4054 enum lock_usage_bit read_bit = bit + LOCK_USAGE_READ_MASK;
4055 unsigned usage_mask = lock_flag(bit) | lock_flag(read_bit);
4057 bfs_init_root(&root, this);
4058 ret = find_usage_forwards(&root, usage_mask, &target_entry);
4059 if (bfs_error(ret)) {
4063 if (ret == BFS_RNOMATCH)
4066 /* Check whether write or read usage is the match */
4067 if (target_entry->class->usage_mask & lock_flag(bit)) {
4068 print_irq_inversion_bug(curr, &root, target_entry,
4069 this, 1, state_name(bit));
4071 print_irq_inversion_bug(curr, &root, target_entry,
4072 this, 1, state_name(read_bit));
4079 * Prove that in the backwards-direction subgraph starting at <this>
4080 * there is no lock matching <mask>:
4083 check_usage_backwards(struct task_struct *curr, struct held_lock *this,
4084 enum lock_usage_bit bit)
4086 enum bfs_result ret;
4087 struct lock_list root;
4088 struct lock_list *target_entry;
4089 enum lock_usage_bit read_bit = bit + LOCK_USAGE_READ_MASK;
4090 unsigned usage_mask = lock_flag(bit) | lock_flag(read_bit);
4092 bfs_init_rootb(&root, this);
4093 ret = find_usage_backwards(&root, usage_mask, &target_entry);
4094 if (bfs_error(ret)) {
4098 if (ret == BFS_RNOMATCH)
4101 /* Check whether write or read usage is the match */
4102 if (target_entry->class->usage_mask & lock_flag(bit)) {
4103 print_irq_inversion_bug(curr, &root, target_entry,
4104 this, 0, state_name(bit));
4106 print_irq_inversion_bug(curr, &root, target_entry,
4107 this, 0, state_name(read_bit));
4113 void print_irqtrace_events(struct task_struct *curr)
4115 const struct irqtrace_events *trace = &curr->irqtrace;
4117 printk("irq event stamp: %u\n", trace->irq_events);
4118 printk("hardirqs last enabled at (%u): [<%px>] %pS\n",
4119 trace->hardirq_enable_event, (void *)trace->hardirq_enable_ip,
4120 (void *)trace->hardirq_enable_ip);
4121 printk("hardirqs last disabled at (%u): [<%px>] %pS\n",
4122 trace->hardirq_disable_event, (void *)trace->hardirq_disable_ip,
4123 (void *)trace->hardirq_disable_ip);
4124 printk("softirqs last enabled at (%u): [<%px>] %pS\n",
4125 trace->softirq_enable_event, (void *)trace->softirq_enable_ip,
4126 (void *)trace->softirq_enable_ip);
4127 printk("softirqs last disabled at (%u): [<%px>] %pS\n",
4128 trace->softirq_disable_event, (void *)trace->softirq_disable_ip,
4129 (void *)trace->softirq_disable_ip);
4132 static int HARDIRQ_verbose(struct lock_class *class)
4135 return class_filter(class);
4140 static int SOFTIRQ_verbose(struct lock_class *class)
4143 return class_filter(class);
4148 static int (*state_verbose_f[])(struct lock_class *class) = {
4149 #define LOCKDEP_STATE(__STATE) \
4151 #include "lockdep_states.h"
4152 #undef LOCKDEP_STATE
4155 static inline int state_verbose(enum lock_usage_bit bit,
4156 struct lock_class *class)
4158 return state_verbose_f[bit >> LOCK_USAGE_DIR_MASK](class);
4161 typedef int (*check_usage_f)(struct task_struct *, struct held_lock *,
4162 enum lock_usage_bit bit, const char *name);
4165 mark_lock_irq(struct task_struct *curr, struct held_lock *this,
4166 enum lock_usage_bit new_bit)
4168 int excl_bit = exclusive_bit(new_bit);
4169 int read = new_bit & LOCK_USAGE_READ_MASK;
4170 int dir = new_bit & LOCK_USAGE_DIR_MASK;
4173 * Validate that this particular lock does not have conflicting
4176 if (!valid_state(curr, this, new_bit, excl_bit))
4180 * Check for read in write conflicts
4182 if (!read && !valid_state(curr, this, new_bit,
4183 excl_bit + LOCK_USAGE_READ_MASK))
4188 * Validate that the lock dependencies don't have conflicting usage
4193 * mark ENABLED has to look backwards -- to ensure no dependee
4194 * has USED_IN state, which, again, would allow recursion deadlocks.
4196 if (!check_usage_backwards(curr, this, excl_bit))
4200 * mark USED_IN has to look forwards -- to ensure no dependency
4201 * has ENABLED state, which would allow recursion deadlocks.
4203 if (!check_usage_forwards(curr, this, excl_bit))
4207 if (state_verbose(new_bit, hlock_class(this)))
4214 * Mark all held locks with a usage bit:
4217 mark_held_locks(struct task_struct *curr, enum lock_usage_bit base_bit)
4219 struct held_lock *hlock;
4222 for (i = 0; i < curr->lockdep_depth; i++) {
4223 enum lock_usage_bit hlock_bit = base_bit;
4224 hlock = curr->held_locks + i;
4227 hlock_bit += LOCK_USAGE_READ_MASK;
4229 BUG_ON(hlock_bit >= LOCK_USAGE_STATES);
4234 if (!mark_lock(curr, hlock, hlock_bit))
4242 * Hardirqs will be enabled:
4244 static void __trace_hardirqs_on_caller(void)
4246 struct task_struct *curr = current;
4249 * We are going to turn hardirqs on, so set the
4250 * usage bit for all held locks:
4252 if (!mark_held_locks(curr, LOCK_ENABLED_HARDIRQ))
4255 * If we have softirqs enabled, then set the usage
4256 * bit for all held locks. (disabled hardirqs prevented
4257 * this bit from being set before)
4259 if (curr->softirqs_enabled)
4260 mark_held_locks(curr, LOCK_ENABLED_SOFTIRQ);
4264 * lockdep_hardirqs_on_prepare - Prepare for enabling interrupts
4266 * Invoked before a possible transition to RCU idle from exit to user or
4267 * guest mode. This ensures that all RCU operations are done before RCU
4268 * stops watching. After the RCU transition lockdep_hardirqs_on() has to be
4269 * invoked to set the final state.
4271 void lockdep_hardirqs_on_prepare(void)
4273 if (unlikely(!debug_locks))
4277 * NMIs do not (and cannot) track lock dependencies, nothing to do.
4279 if (unlikely(in_nmi()))
4282 if (unlikely(this_cpu_read(lockdep_recursion)))
4285 if (unlikely(lockdep_hardirqs_enabled())) {
4287 * Neither irq nor preemption are disabled here
4288 * so this is racy by nature but losing one hit
4289 * in a stat is not a big deal.
4291 __debug_atomic_inc(redundant_hardirqs_on);
4296 * We're enabling irqs and according to our state above irqs weren't
4297 * already enabled, yet we find the hardware thinks they are in fact
4298 * enabled.. someone messed up their IRQ state tracing.
4300 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
4304 * See the fine text that goes along with this variable definition.
4306 if (DEBUG_LOCKS_WARN_ON(early_boot_irqs_disabled))
4310 * Can't allow enabling interrupts while in an interrupt handler,
4311 * that's general bad form and such. Recursion, limited stack etc..
4313 if (DEBUG_LOCKS_WARN_ON(lockdep_hardirq_context()))
4316 current->hardirq_chain_key = current->curr_chain_key;
4318 lockdep_recursion_inc();
4319 __trace_hardirqs_on_caller();
4320 lockdep_recursion_finish();
4322 EXPORT_SYMBOL_GPL(lockdep_hardirqs_on_prepare);
4324 void noinstr lockdep_hardirqs_on(unsigned long ip)
4326 struct irqtrace_events *trace = ¤t->irqtrace;
4328 if (unlikely(!debug_locks))
4332 * NMIs can happen in the middle of local_irq_{en,dis}able() where the
4333 * tracking state and hardware state are out of sync.
4335 * NMIs must save lockdep_hardirqs_enabled() to restore IRQ state from,
4336 * and not rely on hardware state like normal interrupts.
4338 if (unlikely(in_nmi())) {
4339 if (!IS_ENABLED(CONFIG_TRACE_IRQFLAGS_NMI))
4344 * - recursion check, because NMI can hit lockdep;
4345 * - hardware state check, because above;
4346 * - chain_key check, see lockdep_hardirqs_on_prepare().
4351 if (unlikely(this_cpu_read(lockdep_recursion)))
4354 if (lockdep_hardirqs_enabled()) {
4356 * Neither irq nor preemption are disabled here
4357 * so this is racy by nature but losing one hit
4358 * in a stat is not a big deal.
4360 __debug_atomic_inc(redundant_hardirqs_on);
4365 * We're enabling irqs and according to our state above irqs weren't
4366 * already enabled, yet we find the hardware thinks they are in fact
4367 * enabled.. someone messed up their IRQ state tracing.
4369 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
4373 * Ensure the lock stack remained unchanged between
4374 * lockdep_hardirqs_on_prepare() and lockdep_hardirqs_on().
4376 DEBUG_LOCKS_WARN_ON(current->hardirq_chain_key !=
4377 current->curr_chain_key);
4380 /* we'll do an OFF -> ON transition: */
4381 __this_cpu_write(hardirqs_enabled, 1);
4382 trace->hardirq_enable_ip = ip;
4383 trace->hardirq_enable_event = ++trace->irq_events;
4384 debug_atomic_inc(hardirqs_on_events);
4386 EXPORT_SYMBOL_GPL(lockdep_hardirqs_on);
4389 * Hardirqs were disabled:
4391 void noinstr lockdep_hardirqs_off(unsigned long ip)
4393 if (unlikely(!debug_locks))
4397 * Matching lockdep_hardirqs_on(), allow NMIs in the middle of lockdep;
4398 * they will restore the software state. This ensures the software
4399 * state is consistent inside NMIs as well.
4402 if (!IS_ENABLED(CONFIG_TRACE_IRQFLAGS_NMI))
4404 } else if (__this_cpu_read(lockdep_recursion))
4408 * So we're supposed to get called after you mask local IRQs, but for
4409 * some reason the hardware doesn't quite think you did a proper job.
4411 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
4414 if (lockdep_hardirqs_enabled()) {
4415 struct irqtrace_events *trace = ¤t->irqtrace;
4418 * We have done an ON -> OFF transition:
4420 __this_cpu_write(hardirqs_enabled, 0);
4421 trace->hardirq_disable_ip = ip;
4422 trace->hardirq_disable_event = ++trace->irq_events;
4423 debug_atomic_inc(hardirqs_off_events);
4425 debug_atomic_inc(redundant_hardirqs_off);
4428 EXPORT_SYMBOL_GPL(lockdep_hardirqs_off);
4431 * Softirqs will be enabled:
4433 void lockdep_softirqs_on(unsigned long ip)
4435 struct irqtrace_events *trace = ¤t->irqtrace;
4437 if (unlikely(!lockdep_enabled()))
4441 * We fancy IRQs being disabled here, see softirq.c, avoids
4442 * funny state and nesting things.
4444 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
4447 if (current->softirqs_enabled) {
4448 debug_atomic_inc(redundant_softirqs_on);
4452 lockdep_recursion_inc();
4454 * We'll do an OFF -> ON transition:
4456 current->softirqs_enabled = 1;
4457 trace->softirq_enable_ip = ip;
4458 trace->softirq_enable_event = ++trace->irq_events;
4459 debug_atomic_inc(softirqs_on_events);
4461 * We are going to turn softirqs on, so set the
4462 * usage bit for all held locks, if hardirqs are
4465 if (lockdep_hardirqs_enabled())
4466 mark_held_locks(current, LOCK_ENABLED_SOFTIRQ);
4467 lockdep_recursion_finish();
4471 * Softirqs were disabled:
4473 void lockdep_softirqs_off(unsigned long ip)
4475 if (unlikely(!lockdep_enabled()))
4479 * We fancy IRQs being disabled here, see softirq.c
4481 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
4484 if (current->softirqs_enabled) {
4485 struct irqtrace_events *trace = ¤t->irqtrace;
4488 * We have done an ON -> OFF transition:
4490 current->softirqs_enabled = 0;
4491 trace->softirq_disable_ip = ip;
4492 trace->softirq_disable_event = ++trace->irq_events;
4493 debug_atomic_inc(softirqs_off_events);
4495 * Whoops, we wanted softirqs off, so why aren't they?
4497 DEBUG_LOCKS_WARN_ON(!softirq_count());
4499 debug_atomic_inc(redundant_softirqs_off);
4503 mark_usage(struct task_struct *curr, struct held_lock *hlock, int check)
4509 * If non-trylock use in a hardirq or softirq context, then
4510 * mark the lock as used in these contexts:
4512 if (!hlock->trylock) {
4514 if (lockdep_hardirq_context())
4515 if (!mark_lock(curr, hlock,
4516 LOCK_USED_IN_HARDIRQ_READ))
4518 if (curr->softirq_context)
4519 if (!mark_lock(curr, hlock,
4520 LOCK_USED_IN_SOFTIRQ_READ))
4523 if (lockdep_hardirq_context())
4524 if (!mark_lock(curr, hlock, LOCK_USED_IN_HARDIRQ))
4526 if (curr->softirq_context)
4527 if (!mark_lock(curr, hlock, LOCK_USED_IN_SOFTIRQ))
4531 if (!hlock->hardirqs_off) {
4533 if (!mark_lock(curr, hlock,
4534 LOCK_ENABLED_HARDIRQ_READ))
4536 if (curr->softirqs_enabled)
4537 if (!mark_lock(curr, hlock,
4538 LOCK_ENABLED_SOFTIRQ_READ))
4541 if (!mark_lock(curr, hlock,
4542 LOCK_ENABLED_HARDIRQ))
4544 if (curr->softirqs_enabled)
4545 if (!mark_lock(curr, hlock,
4546 LOCK_ENABLED_SOFTIRQ))
4552 /* mark it as used: */
4553 if (!mark_lock(curr, hlock, LOCK_USED))
4559 static inline unsigned int task_irq_context(struct task_struct *task)
4561 return LOCK_CHAIN_HARDIRQ_CONTEXT * !!lockdep_hardirq_context() +
4562 LOCK_CHAIN_SOFTIRQ_CONTEXT * !!task->softirq_context;
4565 static int separate_irq_context(struct task_struct *curr,
4566 struct held_lock *hlock)
4568 unsigned int depth = curr->lockdep_depth;
4571 * Keep track of points where we cross into an interrupt context:
4574 struct held_lock *prev_hlock;
4576 prev_hlock = curr->held_locks + depth-1;
4578 * If we cross into another context, reset the
4579 * hash key (this also prevents the checking and the
4580 * adding of the dependency to 'prev'):
4582 if (prev_hlock->irq_context != hlock->irq_context)
4589 * Mark a lock with a usage bit, and validate the state transition:
4591 static int mark_lock(struct task_struct *curr, struct held_lock *this,
4592 enum lock_usage_bit new_bit)
4594 unsigned int new_mask, ret = 1;
4596 if (new_bit >= LOCK_USAGE_STATES) {
4597 DEBUG_LOCKS_WARN_ON(1);
4601 if (new_bit == LOCK_USED && this->read)
4602 new_bit = LOCK_USED_READ;
4604 new_mask = 1 << new_bit;
4607 * If already set then do not dirty the cacheline,
4608 * nor do any checks:
4610 if (likely(hlock_class(this)->usage_mask & new_mask))
4616 * Make sure we didn't race:
4618 if (unlikely(hlock_class(this)->usage_mask & new_mask))
4621 if (!hlock_class(this)->usage_mask)
4622 debug_atomic_dec(nr_unused_locks);
4624 hlock_class(this)->usage_mask |= new_mask;
4626 if (new_bit < LOCK_TRACE_STATES) {
4627 if (!(hlock_class(this)->usage_traces[new_bit] = save_trace()))
4631 if (new_bit < LOCK_USED) {
4632 ret = mark_lock_irq(curr, this, new_bit);
4641 * We must printk outside of the graph_lock:
4644 printk("\nmarked lock as {%s}:\n", usage_str[new_bit]);
4646 print_irqtrace_events(curr);
4653 static inline short task_wait_context(struct task_struct *curr)
4656 * Set appropriate wait type for the context; for IRQs we have to take
4657 * into account force_irqthread as that is implied by PREEMPT_RT.
4659 if (lockdep_hardirq_context()) {
4661 * Check if force_irqthreads will run us threaded.
4663 if (curr->hardirq_threaded || curr->irq_config)
4664 return LD_WAIT_CONFIG;
4666 return LD_WAIT_SPIN;
4667 } else if (curr->softirq_context) {
4669 * Softirqs are always threaded.
4671 return LD_WAIT_CONFIG;
4678 print_lock_invalid_wait_context(struct task_struct *curr,
4679 struct held_lock *hlock)
4683 if (!debug_locks_off())
4685 if (debug_locks_silent)
4689 pr_warn("=============================\n");
4690 pr_warn("[ BUG: Invalid wait context ]\n");
4691 print_kernel_ident();
4692 pr_warn("-----------------------------\n");
4694 pr_warn("%s/%d is trying to lock:\n", curr->comm, task_pid_nr(curr));
4697 pr_warn("other info that might help us debug this:\n");
4699 curr_inner = task_wait_context(curr);
4700 pr_warn("context-{%d:%d}\n", curr_inner, curr_inner);
4702 lockdep_print_held_locks(curr);
4704 pr_warn("stack backtrace:\n");
4711 * Verify the wait_type context.
4713 * This check validates we take locks in the right wait-type order; that is it
4714 * ensures that we do not take mutexes inside spinlocks and do not attempt to
4715 * acquire spinlocks inside raw_spinlocks and the sort.
4717 * The entire thing is slightly more complex because of RCU, RCU is a lock that
4718 * can be taken from (pretty much) any context but also has constraints.
4719 * However when taken in a stricter environment the RCU lock does not loosen
4722 * Therefore we must look for the strictest environment in the lock stack and
4723 * compare that to the lock we're trying to acquire.
4725 static int check_wait_context(struct task_struct *curr, struct held_lock *next)
4727 u8 next_inner = hlock_class(next)->wait_type_inner;
4728 u8 next_outer = hlock_class(next)->wait_type_outer;
4732 if (!next_inner || next->trylock)
4736 next_outer = next_inner;
4739 * Find start of current irq_context..
4741 for (depth = curr->lockdep_depth - 1; depth >= 0; depth--) {
4742 struct held_lock *prev = curr->held_locks + depth;
4743 if (prev->irq_context != next->irq_context)
4748 curr_inner = task_wait_context(curr);
4750 for (; depth < curr->lockdep_depth; depth++) {
4751 struct held_lock *prev = curr->held_locks + depth;
4752 u8 prev_inner = hlock_class(prev)->wait_type_inner;
4756 * We can have a bigger inner than a previous one
4757 * when outer is smaller than inner, as with RCU.
4759 * Also due to trylocks.
4761 curr_inner = min(curr_inner, prev_inner);
4765 if (next_outer > curr_inner)
4766 return print_lock_invalid_wait_context(curr, next);
4771 #else /* CONFIG_PROVE_LOCKING */
4774 mark_usage(struct task_struct *curr, struct held_lock *hlock, int check)
4779 static inline unsigned int task_irq_context(struct task_struct *task)
4784 static inline int separate_irq_context(struct task_struct *curr,
4785 struct held_lock *hlock)
4790 static inline int check_wait_context(struct task_struct *curr,
4791 struct held_lock *next)
4796 #endif /* CONFIG_PROVE_LOCKING */
4799 * Initialize a lock instance's lock-class mapping info:
4801 void lockdep_init_map_type(struct lockdep_map *lock, const char *name,
4802 struct lock_class_key *key, int subclass,
4803 u8 inner, u8 outer, u8 lock_type)
4807 for (i = 0; i < NR_LOCKDEP_CACHING_CLASSES; i++)
4808 lock->class_cache[i] = NULL;
4810 #ifdef CONFIG_LOCK_STAT
4811 lock->cpu = raw_smp_processor_id();
4815 * Can't be having no nameless bastards around this place!
4817 if (DEBUG_LOCKS_WARN_ON(!name)) {
4818 lock->name = "NULL";
4824 lock->wait_type_outer = outer;
4825 lock->wait_type_inner = inner;
4826 lock->lock_type = lock_type;
4829 * No key, no joy, we need to hash something.
4831 if (DEBUG_LOCKS_WARN_ON(!key))
4834 * Sanity check, the lock-class key must either have been allocated
4835 * statically or must have been registered as a dynamic key.
4837 if (!static_obj(key) && !is_dynamic_key(key)) {
4839 printk(KERN_ERR "BUG: key %px has not been registered!\n", key);
4840 DEBUG_LOCKS_WARN_ON(1);
4845 if (unlikely(!debug_locks))
4849 unsigned long flags;
4851 if (DEBUG_LOCKS_WARN_ON(!lockdep_enabled()))
4854 raw_local_irq_save(flags);
4855 lockdep_recursion_inc();
4856 register_lock_class(lock, subclass, 1);
4857 lockdep_recursion_finish();
4858 raw_local_irq_restore(flags);
4861 EXPORT_SYMBOL_GPL(lockdep_init_map_type);
4863 struct lock_class_key __lockdep_no_validate__;
4864 EXPORT_SYMBOL_GPL(__lockdep_no_validate__);
4867 print_lock_nested_lock_not_held(struct task_struct *curr,
4868 struct held_lock *hlock)
4870 if (!debug_locks_off())
4872 if (debug_locks_silent)
4876 pr_warn("==================================\n");
4877 pr_warn("WARNING: Nested lock was not taken\n");
4878 print_kernel_ident();
4879 pr_warn("----------------------------------\n");
4881 pr_warn("%s/%d is trying to lock:\n", curr->comm, task_pid_nr(curr));
4884 pr_warn("\nbut this task is not holding:\n");
4885 pr_warn("%s\n", hlock->nest_lock->name);
4887 pr_warn("\nstack backtrace:\n");
4890 pr_warn("\nother info that might help us debug this:\n");
4891 lockdep_print_held_locks(curr);
4893 pr_warn("\nstack backtrace:\n");
4897 static int __lock_is_held(const struct lockdep_map *lock, int read);
4900 * This gets called for every mutex_lock*()/spin_lock*() operation.
4901 * We maintain the dependency maps and validate the locking attempt:
4903 * The callers must make sure that IRQs are disabled before calling it,
4904 * otherwise we could get an interrupt which would want to take locks,
4905 * which would end up in lockdep again.
4907 static int __lock_acquire(struct lockdep_map *lock, unsigned int subclass,
4908 int trylock, int read, int check, int hardirqs_off,
4909 struct lockdep_map *nest_lock, unsigned long ip,
4910 int references, int pin_count)
4912 struct task_struct *curr = current;
4913 struct lock_class *class = NULL;
4914 struct held_lock *hlock;
4920 if (unlikely(!debug_locks))
4923 if (!prove_locking || lock->key == &__lockdep_no_validate__)
4926 if (subclass < NR_LOCKDEP_CACHING_CLASSES)
4927 class = lock->class_cache[subclass];
4931 if (unlikely(!class)) {
4932 class = register_lock_class(lock, subclass, 0);
4937 debug_class_ops_inc(class);
4939 if (very_verbose(class)) {
4940 printk("\nacquire class [%px] %s", class->key, class->name);
4941 if (class->name_version > 1)
4942 printk(KERN_CONT "#%d", class->name_version);
4943 printk(KERN_CONT "\n");
4948 * Add the lock to the list of currently held locks.
4949 * (we dont increase the depth just yet, up until the
4950 * dependency checks are done)
4952 depth = curr->lockdep_depth;
4954 * Ran out of static storage for our per-task lock stack again have we?
4956 if (DEBUG_LOCKS_WARN_ON(depth >= MAX_LOCK_DEPTH))
4959 class_idx = class - lock_classes;
4961 if (depth) { /* we're holding locks */
4962 hlock = curr->held_locks + depth - 1;
4963 if (hlock->class_idx == class_idx && nest_lock) {
4967 if (!hlock->references)
4968 hlock->references++;
4970 hlock->references += references;
4973 if (DEBUG_LOCKS_WARN_ON(hlock->references < references))
4980 hlock = curr->held_locks + depth;
4982 * Plain impossible, we just registered it and checked it weren't no
4983 * NULL like.. I bet this mushroom I ate was good!
4985 if (DEBUG_LOCKS_WARN_ON(!class))
4987 hlock->class_idx = class_idx;
4988 hlock->acquire_ip = ip;
4989 hlock->instance = lock;
4990 hlock->nest_lock = nest_lock;
4991 hlock->irq_context = task_irq_context(curr);
4992 hlock->trylock = trylock;
4994 hlock->check = check;
4995 hlock->hardirqs_off = !!hardirqs_off;
4996 hlock->references = references;
4997 #ifdef CONFIG_LOCK_STAT
4998 hlock->waittime_stamp = 0;
4999 hlock->holdtime_stamp = lockstat_clock();
5001 hlock->pin_count = pin_count;
5003 if (check_wait_context(curr, hlock))
5006 /* Initialize the lock usage bit */
5007 if (!mark_usage(curr, hlock, check))
5011 * Calculate the chain hash: it's the combined hash of all the
5012 * lock keys along the dependency chain. We save the hash value
5013 * at every step so that we can get the current hash easily
5014 * after unlock. The chain hash is then used to cache dependency
5017 * The 'key ID' is what is the most compact key value to drive
5018 * the hash, not class->key.
5021 * Whoops, we did it again.. class_idx is invalid.
5023 if (DEBUG_LOCKS_WARN_ON(!test_bit(class_idx, lock_classes_in_use)))
5026 chain_key = curr->curr_chain_key;
5029 * How can we have a chain hash when we ain't got no keys?!
5031 if (DEBUG_LOCKS_WARN_ON(chain_key != INITIAL_CHAIN_KEY))
5036 hlock->prev_chain_key = chain_key;
5037 if (separate_irq_context(curr, hlock)) {
5038 chain_key = INITIAL_CHAIN_KEY;
5041 chain_key = iterate_chain_key(chain_key, hlock_id(hlock));
5043 if (nest_lock && !__lock_is_held(nest_lock, -1)) {
5044 print_lock_nested_lock_not_held(curr, hlock);
5048 if (!debug_locks_silent) {
5049 WARN_ON_ONCE(depth && !hlock_class(hlock - 1)->key);
5050 WARN_ON_ONCE(!hlock_class(hlock)->key);
5053 if (!validate_chain(curr, hlock, chain_head, chain_key))
5056 curr->curr_chain_key = chain_key;
5057 curr->lockdep_depth++;
5058 check_chain_key(curr);
5059 #ifdef CONFIG_DEBUG_LOCKDEP
5060 if (unlikely(!debug_locks))
5063 if (unlikely(curr->lockdep_depth >= MAX_LOCK_DEPTH)) {
5065 print_lockdep_off("BUG: MAX_LOCK_DEPTH too low!");
5066 printk(KERN_DEBUG "depth: %i max: %lu!\n",
5067 curr->lockdep_depth, MAX_LOCK_DEPTH);
5069 lockdep_print_held_locks(current);
5070 debug_show_all_locks();
5076 if (unlikely(curr->lockdep_depth > max_lockdep_depth))
5077 max_lockdep_depth = curr->lockdep_depth;
5082 static void print_unlock_imbalance_bug(struct task_struct *curr,
5083 struct lockdep_map *lock,
5086 if (!debug_locks_off())
5088 if (debug_locks_silent)
5092 pr_warn("=====================================\n");
5093 pr_warn("WARNING: bad unlock balance detected!\n");
5094 print_kernel_ident();
5095 pr_warn("-------------------------------------\n");
5096 pr_warn("%s/%d is trying to release lock (",
5097 curr->comm, task_pid_nr(curr));
5098 print_lockdep_cache(lock);
5100 print_ip_sym(KERN_WARNING, ip);
5101 pr_warn("but there are no more locks to release!\n");
5102 pr_warn("\nother info that might help us debug this:\n");
5103 lockdep_print_held_locks(curr);
5105 pr_warn("\nstack backtrace:\n");
5109 static noinstr int match_held_lock(const struct held_lock *hlock,
5110 const struct lockdep_map *lock)
5112 if (hlock->instance == lock)
5115 if (hlock->references) {
5116 const struct lock_class *class = lock->class_cache[0];
5119 class = look_up_lock_class(lock, 0);
5122 * If look_up_lock_class() failed to find a class, we're trying
5123 * to test if we hold a lock that has never yet been acquired.
5124 * Clearly if the lock hasn't been acquired _ever_, we're not
5125 * holding it either, so report failure.
5131 * References, but not a lock we're actually ref-counting?
5132 * State got messed up, follow the sites that change ->references
5133 * and try to make sense of it.
5135 if (DEBUG_LOCKS_WARN_ON(!hlock->nest_lock))
5138 if (hlock->class_idx == class - lock_classes)
5145 /* @depth must not be zero */
5146 static struct held_lock *find_held_lock(struct task_struct *curr,
5147 struct lockdep_map *lock,
5148 unsigned int depth, int *idx)
5150 struct held_lock *ret, *hlock, *prev_hlock;
5154 hlock = curr->held_locks + i;
5156 if (match_held_lock(hlock, lock))
5160 for (i--, prev_hlock = hlock--;
5162 i--, prev_hlock = hlock--) {
5164 * We must not cross into another context:
5166 if (prev_hlock->irq_context != hlock->irq_context) {
5170 if (match_held_lock(hlock, lock)) {
5181 static int reacquire_held_locks(struct task_struct *curr, unsigned int depth,
5182 int idx, unsigned int *merged)
5184 struct held_lock *hlock;
5185 int first_idx = idx;
5187 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
5190 for (hlock = curr->held_locks + idx; idx < depth; idx++, hlock++) {
5191 switch (__lock_acquire(hlock->instance,
5192 hlock_class(hlock)->subclass,
5194 hlock->read, hlock->check,
5195 hlock->hardirqs_off,
5196 hlock->nest_lock, hlock->acquire_ip,
5197 hlock->references, hlock->pin_count)) {
5203 *merged += (idx == first_idx);
5214 __lock_set_class(struct lockdep_map *lock, const char *name,
5215 struct lock_class_key *key, unsigned int subclass,
5218 struct task_struct *curr = current;
5219 unsigned int depth, merged = 0;
5220 struct held_lock *hlock;
5221 struct lock_class *class;
5224 if (unlikely(!debug_locks))
5227 depth = curr->lockdep_depth;
5229 * This function is about (re)setting the class of a held lock,
5230 * yet we're not actually holding any locks. Naughty user!
5232 if (DEBUG_LOCKS_WARN_ON(!depth))
5235 hlock = find_held_lock(curr, lock, depth, &i);
5237 print_unlock_imbalance_bug(curr, lock, ip);
5241 lockdep_init_map_waits(lock, name, key, 0,
5242 lock->wait_type_inner,
5243 lock->wait_type_outer);
5244 class = register_lock_class(lock, subclass, 0);
5245 hlock->class_idx = class - lock_classes;
5247 curr->lockdep_depth = i;
5248 curr->curr_chain_key = hlock->prev_chain_key;
5250 if (reacquire_held_locks(curr, depth, i, &merged))
5254 * I took it apart and put it back together again, except now I have
5255 * these 'spare' parts.. where shall I put them.
5257 if (DEBUG_LOCKS_WARN_ON(curr->lockdep_depth != depth - merged))
5262 static int __lock_downgrade(struct lockdep_map *lock, unsigned long ip)
5264 struct task_struct *curr = current;
5265 unsigned int depth, merged = 0;
5266 struct held_lock *hlock;
5269 if (unlikely(!debug_locks))
5272 depth = curr->lockdep_depth;
5274 * This function is about (re)setting the class of a held lock,
5275 * yet we're not actually holding any locks. Naughty user!
5277 if (DEBUG_LOCKS_WARN_ON(!depth))
5280 hlock = find_held_lock(curr, lock, depth, &i);
5282 print_unlock_imbalance_bug(curr, lock, ip);
5286 curr->lockdep_depth = i;
5287 curr->curr_chain_key = hlock->prev_chain_key;
5289 WARN(hlock->read, "downgrading a read lock");
5291 hlock->acquire_ip = ip;
5293 if (reacquire_held_locks(curr, depth, i, &merged))
5296 /* Merging can't happen with unchanged classes.. */
5297 if (DEBUG_LOCKS_WARN_ON(merged))
5301 * I took it apart and put it back together again, except now I have
5302 * these 'spare' parts.. where shall I put them.
5304 if (DEBUG_LOCKS_WARN_ON(curr->lockdep_depth != depth))
5311 * Remove the lock from the list of currently held locks - this gets
5312 * called on mutex_unlock()/spin_unlock*() (or on a failed
5313 * mutex_lock_interruptible()).
5316 __lock_release(struct lockdep_map *lock, unsigned long ip)
5318 struct task_struct *curr = current;
5319 unsigned int depth, merged = 1;
5320 struct held_lock *hlock;
5323 if (unlikely(!debug_locks))
5326 depth = curr->lockdep_depth;
5328 * So we're all set to release this lock.. wait what lock? We don't
5329 * own any locks, you've been drinking again?
5332 print_unlock_imbalance_bug(curr, lock, ip);
5337 * Check whether the lock exists in the current stack
5340 hlock = find_held_lock(curr, lock, depth, &i);
5342 print_unlock_imbalance_bug(curr, lock, ip);
5346 if (hlock->instance == lock)
5347 lock_release_holdtime(hlock);
5349 WARN(hlock->pin_count, "releasing a pinned lock\n");
5351 if (hlock->references) {
5352 hlock->references--;
5353 if (hlock->references) {
5355 * We had, and after removing one, still have
5356 * references, the current lock stack is still
5357 * valid. We're done!
5364 * We have the right lock to unlock, 'hlock' points to it.
5365 * Now we remove it from the stack, and add back the other
5366 * entries (if any), recalculating the hash along the way:
5369 curr->lockdep_depth = i;
5370 curr->curr_chain_key = hlock->prev_chain_key;
5373 * The most likely case is when the unlock is on the innermost
5374 * lock. In this case, we are done!
5379 if (reacquire_held_locks(curr, depth, i + 1, &merged))
5383 * We had N bottles of beer on the wall, we drank one, but now
5384 * there's not N-1 bottles of beer left on the wall...
5385 * Pouring two of the bottles together is acceptable.
5387 DEBUG_LOCKS_WARN_ON(curr->lockdep_depth != depth - merged);
5390 * Since reacquire_held_locks() would have called check_chain_key()
5391 * indirectly via __lock_acquire(), we don't need to do it again
5397 static __always_inline
5398 int __lock_is_held(const struct lockdep_map *lock, int read)
5400 struct task_struct *curr = current;
5403 for (i = 0; i < curr->lockdep_depth; i++) {
5404 struct held_lock *hlock = curr->held_locks + i;
5406 if (match_held_lock(hlock, lock)) {
5407 if (read == -1 || !!hlock->read == read)
5408 return LOCK_STATE_HELD;
5410 return LOCK_STATE_NOT_HELD;
5414 return LOCK_STATE_NOT_HELD;
5417 static struct pin_cookie __lock_pin_lock(struct lockdep_map *lock)
5419 struct pin_cookie cookie = NIL_COOKIE;
5420 struct task_struct *curr = current;
5423 if (unlikely(!debug_locks))
5426 for (i = 0; i < curr->lockdep_depth; i++) {
5427 struct held_lock *hlock = curr->held_locks + i;
5429 if (match_held_lock(hlock, lock)) {
5431 * Grab 16bits of randomness; this is sufficient to not
5432 * be guessable and still allows some pin nesting in
5433 * our u32 pin_count.
5435 cookie.val = 1 + (prandom_u32() >> 16);
5436 hlock->pin_count += cookie.val;
5441 WARN(1, "pinning an unheld lock\n");
5445 static void __lock_repin_lock(struct lockdep_map *lock, struct pin_cookie cookie)
5447 struct task_struct *curr = current;
5450 if (unlikely(!debug_locks))
5453 for (i = 0; i < curr->lockdep_depth; i++) {
5454 struct held_lock *hlock = curr->held_locks + i;
5456 if (match_held_lock(hlock, lock)) {
5457 hlock->pin_count += cookie.val;
5462 WARN(1, "pinning an unheld lock\n");
5465 static void __lock_unpin_lock(struct lockdep_map *lock, struct pin_cookie cookie)
5467 struct task_struct *curr = current;
5470 if (unlikely(!debug_locks))
5473 for (i = 0; i < curr->lockdep_depth; i++) {
5474 struct held_lock *hlock = curr->held_locks + i;
5476 if (match_held_lock(hlock, lock)) {
5477 if (WARN(!hlock->pin_count, "unpinning an unpinned lock\n"))
5480 hlock->pin_count -= cookie.val;
5482 if (WARN((int)hlock->pin_count < 0, "pin count corrupted\n"))
5483 hlock->pin_count = 0;
5489 WARN(1, "unpinning an unheld lock\n");
5493 * Check whether we follow the irq-flags state precisely:
5495 static noinstr void check_flags(unsigned long flags)
5497 #if defined(CONFIG_PROVE_LOCKING) && defined(CONFIG_DEBUG_LOCKDEP)
5501 /* Get the warning out.. */
5502 instrumentation_begin();
5504 if (irqs_disabled_flags(flags)) {
5505 if (DEBUG_LOCKS_WARN_ON(lockdep_hardirqs_enabled())) {
5506 printk("possible reason: unannotated irqs-off.\n");
5509 if (DEBUG_LOCKS_WARN_ON(!lockdep_hardirqs_enabled())) {
5510 printk("possible reason: unannotated irqs-on.\n");
5514 #ifndef CONFIG_PREEMPT_RT
5516 * We dont accurately track softirq state in e.g.
5517 * hardirq contexts (such as on 4KSTACKS), so only
5518 * check if not in hardirq contexts:
5520 if (!hardirq_count()) {
5521 if (softirq_count()) {
5522 /* like the above, but with softirqs */
5523 DEBUG_LOCKS_WARN_ON(current->softirqs_enabled);
5525 /* lick the above, does it taste good? */
5526 DEBUG_LOCKS_WARN_ON(!current->softirqs_enabled);
5532 print_irqtrace_events(current);
5534 instrumentation_end();
5538 void lock_set_class(struct lockdep_map *lock, const char *name,
5539 struct lock_class_key *key, unsigned int subclass,
5542 unsigned long flags;
5544 if (unlikely(!lockdep_enabled()))
5547 raw_local_irq_save(flags);
5548 lockdep_recursion_inc();
5550 if (__lock_set_class(lock, name, key, subclass, ip))
5551 check_chain_key(current);
5552 lockdep_recursion_finish();
5553 raw_local_irq_restore(flags);
5555 EXPORT_SYMBOL_GPL(lock_set_class);
5557 void lock_downgrade(struct lockdep_map *lock, unsigned long ip)
5559 unsigned long flags;
5561 if (unlikely(!lockdep_enabled()))
5564 raw_local_irq_save(flags);
5565 lockdep_recursion_inc();
5567 if (__lock_downgrade(lock, ip))
5568 check_chain_key(current);
5569 lockdep_recursion_finish();
5570 raw_local_irq_restore(flags);
5572 EXPORT_SYMBOL_GPL(lock_downgrade);
5574 /* NMI context !!! */
5575 static void verify_lock_unused(struct lockdep_map *lock, struct held_lock *hlock, int subclass)
5577 #ifdef CONFIG_PROVE_LOCKING
5578 struct lock_class *class = look_up_lock_class(lock, subclass);
5579 unsigned long mask = LOCKF_USED;
5581 /* if it doesn't have a class (yet), it certainly hasn't been used yet */
5586 * READ locks only conflict with USED, such that if we only ever use
5587 * READ locks, there is no deadlock possible -- RCU.
5590 mask |= LOCKF_USED_READ;
5592 if (!(class->usage_mask & mask))
5595 hlock->class_idx = class - lock_classes;
5597 print_usage_bug(current, hlock, LOCK_USED, LOCK_USAGE_STATES);
5601 static bool lockdep_nmi(void)
5603 if (raw_cpu_read(lockdep_recursion))
5613 * read_lock() is recursive if:
5614 * 1. We force lockdep think this way in selftests or
5615 * 2. The implementation is not queued read/write lock or
5616 * 3. The locker is at an in_interrupt() context.
5618 bool read_lock_is_recursive(void)
5620 return force_read_lock_recursive ||
5621 !IS_ENABLED(CONFIG_QUEUED_RWLOCKS) ||
5624 EXPORT_SYMBOL_GPL(read_lock_is_recursive);
5627 * We are not always called with irqs disabled - do that here,
5628 * and also avoid lockdep recursion:
5630 void lock_acquire(struct lockdep_map *lock, unsigned int subclass,
5631 int trylock, int read, int check,
5632 struct lockdep_map *nest_lock, unsigned long ip)
5634 unsigned long flags;
5636 trace_lock_acquire(lock, subclass, trylock, read, check, nest_lock, ip);
5641 if (unlikely(!lockdep_enabled())) {
5642 /* XXX allow trylock from NMI ?!? */
5643 if (lockdep_nmi() && !trylock) {
5644 struct held_lock hlock;
5646 hlock.acquire_ip = ip;
5647 hlock.instance = lock;
5648 hlock.nest_lock = nest_lock;
5649 hlock.irq_context = 2; // XXX
5650 hlock.trylock = trylock;
5652 hlock.check = check;
5653 hlock.hardirqs_off = true;
5654 hlock.references = 0;
5656 verify_lock_unused(lock, &hlock, subclass);
5661 raw_local_irq_save(flags);
5664 lockdep_recursion_inc();
5665 __lock_acquire(lock, subclass, trylock, read, check,
5666 irqs_disabled_flags(flags), nest_lock, ip, 0, 0);
5667 lockdep_recursion_finish();
5668 raw_local_irq_restore(flags);
5670 EXPORT_SYMBOL_GPL(lock_acquire);
5672 void lock_release(struct lockdep_map *lock, unsigned long ip)
5674 unsigned long flags;
5676 trace_lock_release(lock, ip);
5678 if (unlikely(!lockdep_enabled()))
5681 raw_local_irq_save(flags);
5684 lockdep_recursion_inc();
5685 if (__lock_release(lock, ip))
5686 check_chain_key(current);
5687 lockdep_recursion_finish();
5688 raw_local_irq_restore(flags);
5690 EXPORT_SYMBOL_GPL(lock_release);
5692 noinstr int lock_is_held_type(const struct lockdep_map *lock, int read)
5694 unsigned long flags;
5695 int ret = LOCK_STATE_NOT_HELD;
5698 * Avoid false negative lockdep_assert_held() and
5699 * lockdep_assert_not_held().
5701 if (unlikely(!lockdep_enabled()))
5702 return LOCK_STATE_UNKNOWN;
5704 raw_local_irq_save(flags);
5707 lockdep_recursion_inc();
5708 ret = __lock_is_held(lock, read);
5709 lockdep_recursion_finish();
5710 raw_local_irq_restore(flags);
5714 EXPORT_SYMBOL_GPL(lock_is_held_type);
5715 NOKPROBE_SYMBOL(lock_is_held_type);
5717 struct pin_cookie lock_pin_lock(struct lockdep_map *lock)
5719 struct pin_cookie cookie = NIL_COOKIE;
5720 unsigned long flags;
5722 if (unlikely(!lockdep_enabled()))
5725 raw_local_irq_save(flags);
5728 lockdep_recursion_inc();
5729 cookie = __lock_pin_lock(lock);
5730 lockdep_recursion_finish();
5731 raw_local_irq_restore(flags);
5735 EXPORT_SYMBOL_GPL(lock_pin_lock);
5737 void lock_repin_lock(struct lockdep_map *lock, struct pin_cookie cookie)
5739 unsigned long flags;
5741 if (unlikely(!lockdep_enabled()))
5744 raw_local_irq_save(flags);
5747 lockdep_recursion_inc();
5748 __lock_repin_lock(lock, cookie);
5749 lockdep_recursion_finish();
5750 raw_local_irq_restore(flags);
5752 EXPORT_SYMBOL_GPL(lock_repin_lock);
5754 void lock_unpin_lock(struct lockdep_map *lock, struct pin_cookie cookie)
5756 unsigned long flags;
5758 if (unlikely(!lockdep_enabled()))
5761 raw_local_irq_save(flags);
5764 lockdep_recursion_inc();
5765 __lock_unpin_lock(lock, cookie);
5766 lockdep_recursion_finish();
5767 raw_local_irq_restore(flags);
5769 EXPORT_SYMBOL_GPL(lock_unpin_lock);
5771 #ifdef CONFIG_LOCK_STAT
5772 static void print_lock_contention_bug(struct task_struct *curr,
5773 struct lockdep_map *lock,
5776 if (!debug_locks_off())
5778 if (debug_locks_silent)
5782 pr_warn("=================================\n");
5783 pr_warn("WARNING: bad contention detected!\n");
5784 print_kernel_ident();
5785 pr_warn("---------------------------------\n");
5786 pr_warn("%s/%d is trying to contend lock (",
5787 curr->comm, task_pid_nr(curr));
5788 print_lockdep_cache(lock);
5790 print_ip_sym(KERN_WARNING, ip);
5791 pr_warn("but there are no locks held!\n");
5792 pr_warn("\nother info that might help us debug this:\n");
5793 lockdep_print_held_locks(curr);
5795 pr_warn("\nstack backtrace:\n");
5800 __lock_contended(struct lockdep_map *lock, unsigned long ip)
5802 struct task_struct *curr = current;
5803 struct held_lock *hlock;
5804 struct lock_class_stats *stats;
5806 int i, contention_point, contending_point;
5808 depth = curr->lockdep_depth;
5810 * Whee, we contended on this lock, except it seems we're not
5811 * actually trying to acquire anything much at all..
5813 if (DEBUG_LOCKS_WARN_ON(!depth))
5816 hlock = find_held_lock(curr, lock, depth, &i);
5818 print_lock_contention_bug(curr, lock, ip);
5822 if (hlock->instance != lock)
5825 hlock->waittime_stamp = lockstat_clock();
5827 contention_point = lock_point(hlock_class(hlock)->contention_point, ip);
5828 contending_point = lock_point(hlock_class(hlock)->contending_point,
5831 stats = get_lock_stats(hlock_class(hlock));
5832 if (contention_point < LOCKSTAT_POINTS)
5833 stats->contention_point[contention_point]++;
5834 if (contending_point < LOCKSTAT_POINTS)
5835 stats->contending_point[contending_point]++;
5836 if (lock->cpu != smp_processor_id())
5837 stats->bounces[bounce_contended + !!hlock->read]++;
5841 __lock_acquired(struct lockdep_map *lock, unsigned long ip)
5843 struct task_struct *curr = current;
5844 struct held_lock *hlock;
5845 struct lock_class_stats *stats;
5847 u64 now, waittime = 0;
5850 depth = curr->lockdep_depth;
5852 * Yay, we acquired ownership of this lock we didn't try to
5853 * acquire, how the heck did that happen?
5855 if (DEBUG_LOCKS_WARN_ON(!depth))
5858 hlock = find_held_lock(curr, lock, depth, &i);
5860 print_lock_contention_bug(curr, lock, _RET_IP_);
5864 if (hlock->instance != lock)
5867 cpu = smp_processor_id();
5868 if (hlock->waittime_stamp) {
5869 now = lockstat_clock();
5870 waittime = now - hlock->waittime_stamp;
5871 hlock->holdtime_stamp = now;
5874 stats = get_lock_stats(hlock_class(hlock));
5877 lock_time_inc(&stats->read_waittime, waittime);
5879 lock_time_inc(&stats->write_waittime, waittime);
5881 if (lock->cpu != cpu)
5882 stats->bounces[bounce_acquired + !!hlock->read]++;
5888 void lock_contended(struct lockdep_map *lock, unsigned long ip)
5890 unsigned long flags;
5892 trace_lock_contended(lock, ip);
5894 if (unlikely(!lock_stat || !lockdep_enabled()))
5897 raw_local_irq_save(flags);
5899 lockdep_recursion_inc();
5900 __lock_contended(lock, ip);
5901 lockdep_recursion_finish();
5902 raw_local_irq_restore(flags);
5904 EXPORT_SYMBOL_GPL(lock_contended);
5906 void lock_acquired(struct lockdep_map *lock, unsigned long ip)
5908 unsigned long flags;
5910 trace_lock_acquired(lock, ip);
5912 if (unlikely(!lock_stat || !lockdep_enabled()))
5915 raw_local_irq_save(flags);
5917 lockdep_recursion_inc();
5918 __lock_acquired(lock, ip);
5919 lockdep_recursion_finish();
5920 raw_local_irq_restore(flags);
5922 EXPORT_SYMBOL_GPL(lock_acquired);
5926 * Used by the testsuite, sanitize the validator state
5927 * after a simulated failure:
5930 void lockdep_reset(void)
5932 unsigned long flags;
5935 raw_local_irq_save(flags);
5936 lockdep_init_task(current);
5937 memset(current->held_locks, 0, MAX_LOCK_DEPTH*sizeof(struct held_lock));
5938 nr_hardirq_chains = 0;
5939 nr_softirq_chains = 0;
5940 nr_process_chains = 0;
5942 for (i = 0; i < CHAINHASH_SIZE; i++)
5943 INIT_HLIST_HEAD(chainhash_table + i);
5944 raw_local_irq_restore(flags);
5947 /* Remove a class from a lock chain. Must be called with the graph lock held. */
5948 static void remove_class_from_lock_chain(struct pending_free *pf,
5949 struct lock_chain *chain,
5950 struct lock_class *class)
5952 #ifdef CONFIG_PROVE_LOCKING
5955 for (i = chain->base; i < chain->base + chain->depth; i++) {
5956 if (chain_hlock_class_idx(chain_hlocks[i]) != class - lock_classes)
5959 * Each lock class occurs at most once in a lock chain so once
5960 * we found a match we can break out of this loop.
5962 goto free_lock_chain;
5964 /* Since the chain has not been modified, return. */
5968 free_chain_hlocks(chain->base, chain->depth);
5969 /* Overwrite the chain key for concurrent RCU readers. */
5970 WRITE_ONCE(chain->chain_key, INITIAL_CHAIN_KEY);
5971 dec_chains(chain->irq_context);
5974 * Note: calling hlist_del_rcu() from inside a
5975 * hlist_for_each_entry_rcu() loop is safe.
5977 hlist_del_rcu(&chain->entry);
5978 __set_bit(chain - lock_chains, pf->lock_chains_being_freed);
5979 nr_zapped_lock_chains++;
5983 /* Must be called with the graph lock held. */
5984 static void remove_class_from_lock_chains(struct pending_free *pf,
5985 struct lock_class *class)
5987 struct lock_chain *chain;
5988 struct hlist_head *head;
5991 for (i = 0; i < ARRAY_SIZE(chainhash_table); i++) {
5992 head = chainhash_table + i;
5993 hlist_for_each_entry_rcu(chain, head, entry) {
5994 remove_class_from_lock_chain(pf, chain, class);
6000 * Remove all references to a lock class. The caller must hold the graph lock.
6002 static void zap_class(struct pending_free *pf, struct lock_class *class)
6004 struct lock_list *entry;
6007 WARN_ON_ONCE(!class->key);
6010 * Remove all dependencies this lock is
6013 for_each_set_bit(i, list_entries_in_use, ARRAY_SIZE(list_entries)) {
6014 entry = list_entries + i;
6015 if (entry->class != class && entry->links_to != class)
6017 __clear_bit(i, list_entries_in_use);
6019 list_del_rcu(&entry->entry);
6021 if (list_empty(&class->locks_after) &&
6022 list_empty(&class->locks_before)) {
6023 list_move_tail(&class->lock_entry, &pf->zapped);
6024 hlist_del_rcu(&class->hash_entry);
6025 WRITE_ONCE(class->key, NULL);
6026 WRITE_ONCE(class->name, NULL);
6028 __clear_bit(class - lock_classes, lock_classes_in_use);
6029 if (class - lock_classes == max_lock_class_idx)
6030 max_lock_class_idx--;
6032 WARN_ONCE(true, "%s() failed for class %s\n", __func__,
6036 remove_class_from_lock_chains(pf, class);
6037 nr_zapped_classes++;
6040 static void reinit_class(struct lock_class *class)
6042 WARN_ON_ONCE(!class->lock_entry.next);
6043 WARN_ON_ONCE(!list_empty(&class->locks_after));
6044 WARN_ON_ONCE(!list_empty(&class->locks_before));
6045 memset_startat(class, 0, key);
6046 WARN_ON_ONCE(!class->lock_entry.next);
6047 WARN_ON_ONCE(!list_empty(&class->locks_after));
6048 WARN_ON_ONCE(!list_empty(&class->locks_before));
6051 static inline int within(const void *addr, void *start, unsigned long size)
6053 return addr >= start && addr < start + size;
6056 static bool inside_selftest(void)
6058 return current == lockdep_selftest_task_struct;
6061 /* The caller must hold the graph lock. */
6062 static struct pending_free *get_pending_free(void)
6064 return delayed_free.pf + delayed_free.index;
6067 static void free_zapped_rcu(struct rcu_head *cb);
6070 * Schedule an RCU callback if no RCU callback is pending. Must be called with
6071 * the graph lock held.
6073 static void call_rcu_zapped(struct pending_free *pf)
6075 WARN_ON_ONCE(inside_selftest());
6077 if (list_empty(&pf->zapped))
6080 if (delayed_free.scheduled)
6083 delayed_free.scheduled = true;
6085 WARN_ON_ONCE(delayed_free.pf + delayed_free.index != pf);
6086 delayed_free.index ^= 1;
6088 call_rcu(&delayed_free.rcu_head, free_zapped_rcu);
6091 /* The caller must hold the graph lock. May be called from RCU context. */
6092 static void __free_zapped_classes(struct pending_free *pf)
6094 struct lock_class *class;
6096 check_data_structures();
6098 list_for_each_entry(class, &pf->zapped, lock_entry)
6099 reinit_class(class);
6101 list_splice_init(&pf->zapped, &free_lock_classes);
6103 #ifdef CONFIG_PROVE_LOCKING
6104 bitmap_andnot(lock_chains_in_use, lock_chains_in_use,
6105 pf->lock_chains_being_freed, ARRAY_SIZE(lock_chains));
6106 bitmap_clear(pf->lock_chains_being_freed, 0, ARRAY_SIZE(lock_chains));
6110 static void free_zapped_rcu(struct rcu_head *ch)
6112 struct pending_free *pf;
6113 unsigned long flags;
6115 if (WARN_ON_ONCE(ch != &delayed_free.rcu_head))
6118 raw_local_irq_save(flags);
6122 pf = delayed_free.pf + (delayed_free.index ^ 1);
6123 __free_zapped_classes(pf);
6124 delayed_free.scheduled = false;
6127 * If there's anything on the open list, close and start a new callback.
6129 call_rcu_zapped(delayed_free.pf + delayed_free.index);
6132 raw_local_irq_restore(flags);
6136 * Remove all lock classes from the class hash table and from the
6137 * all_lock_classes list whose key or name is in the address range [start,
6138 * start + size). Move these lock classes to the zapped_classes list. Must
6139 * be called with the graph lock held.
6141 static void __lockdep_free_key_range(struct pending_free *pf, void *start,
6144 struct lock_class *class;
6145 struct hlist_head *head;
6148 /* Unhash all classes that were created by a module. */
6149 for (i = 0; i < CLASSHASH_SIZE; i++) {
6150 head = classhash_table + i;
6151 hlist_for_each_entry_rcu(class, head, hash_entry) {
6152 if (!within(class->key, start, size) &&
6153 !within(class->name, start, size))
6155 zap_class(pf, class);
6161 * Used in module.c to remove lock classes from memory that is going to be
6162 * freed; and possibly re-used by other modules.
6164 * We will have had one synchronize_rcu() before getting here, so we're
6165 * guaranteed nobody will look up these exact classes -- they're properly dead
6166 * but still allocated.
6168 static void lockdep_free_key_range_reg(void *start, unsigned long size)
6170 struct pending_free *pf;
6171 unsigned long flags;
6173 init_data_structures_once();
6175 raw_local_irq_save(flags);
6177 pf = get_pending_free();
6178 __lockdep_free_key_range(pf, start, size);
6179 call_rcu_zapped(pf);
6181 raw_local_irq_restore(flags);
6184 * Wait for any possible iterators from look_up_lock_class() to pass
6185 * before continuing to free the memory they refer to.
6191 * Free all lockdep keys in the range [start, start+size). Does not sleep.
6192 * Ignores debug_locks. Must only be used by the lockdep selftests.
6194 static void lockdep_free_key_range_imm(void *start, unsigned long size)
6196 struct pending_free *pf = delayed_free.pf;
6197 unsigned long flags;
6199 init_data_structures_once();
6201 raw_local_irq_save(flags);
6203 __lockdep_free_key_range(pf, start, size);
6204 __free_zapped_classes(pf);
6206 raw_local_irq_restore(flags);
6209 void lockdep_free_key_range(void *start, unsigned long size)
6211 init_data_structures_once();
6213 if (inside_selftest())
6214 lockdep_free_key_range_imm(start, size);
6216 lockdep_free_key_range_reg(start, size);
6220 * Check whether any element of the @lock->class_cache[] array refers to a
6221 * registered lock class. The caller must hold either the graph lock or the
6224 static bool lock_class_cache_is_registered(struct lockdep_map *lock)
6226 struct lock_class *class;
6227 struct hlist_head *head;
6230 for (i = 0; i < CLASSHASH_SIZE; i++) {
6231 head = classhash_table + i;
6232 hlist_for_each_entry_rcu(class, head, hash_entry) {
6233 for (j = 0; j < NR_LOCKDEP_CACHING_CLASSES; j++)
6234 if (lock->class_cache[j] == class)
6241 /* The caller must hold the graph lock. Does not sleep. */
6242 static void __lockdep_reset_lock(struct pending_free *pf,
6243 struct lockdep_map *lock)
6245 struct lock_class *class;
6249 * Remove all classes this lock might have:
6251 for (j = 0; j < MAX_LOCKDEP_SUBCLASSES; j++) {
6253 * If the class exists we look it up and zap it:
6255 class = look_up_lock_class(lock, j);
6257 zap_class(pf, class);
6260 * Debug check: in the end all mapped classes should
6263 if (WARN_ON_ONCE(lock_class_cache_is_registered(lock)))
6268 * Remove all information lockdep has about a lock if debug_locks == 1. Free
6269 * released data structures from RCU context.
6271 static void lockdep_reset_lock_reg(struct lockdep_map *lock)
6273 struct pending_free *pf;
6274 unsigned long flags;
6277 raw_local_irq_save(flags);
6278 locked = graph_lock();
6282 pf = get_pending_free();
6283 __lockdep_reset_lock(pf, lock);
6284 call_rcu_zapped(pf);
6288 raw_local_irq_restore(flags);
6292 * Reset a lock. Does not sleep. Ignores debug_locks. Must only be used by the
6293 * lockdep selftests.
6295 static void lockdep_reset_lock_imm(struct lockdep_map *lock)
6297 struct pending_free *pf = delayed_free.pf;
6298 unsigned long flags;
6300 raw_local_irq_save(flags);
6302 __lockdep_reset_lock(pf, lock);
6303 __free_zapped_classes(pf);
6305 raw_local_irq_restore(flags);
6308 void lockdep_reset_lock(struct lockdep_map *lock)
6310 init_data_structures_once();
6312 if (inside_selftest())
6313 lockdep_reset_lock_imm(lock);
6315 lockdep_reset_lock_reg(lock);
6319 * Unregister a dynamically allocated key.
6321 * Unlike lockdep_register_key(), a search is always done to find a matching
6322 * key irrespective of debug_locks to avoid potential invalid access to freed
6323 * memory in lock_class entry.
6325 void lockdep_unregister_key(struct lock_class_key *key)
6327 struct hlist_head *hash_head = keyhashentry(key);
6328 struct lock_class_key *k;
6329 struct pending_free *pf;
6330 unsigned long flags;
6335 if (WARN_ON_ONCE(static_obj(key)))
6338 raw_local_irq_save(flags);
6341 hlist_for_each_entry_rcu(k, hash_head, hash_entry) {
6343 hlist_del_rcu(&k->hash_entry);
6348 WARN_ON_ONCE(!found && debug_locks);
6350 pf = get_pending_free();
6351 __lockdep_free_key_range(pf, key, 1);
6352 call_rcu_zapped(pf);
6355 raw_local_irq_restore(flags);
6357 /* Wait until is_dynamic_key() has finished accessing k->hash_entry. */
6360 EXPORT_SYMBOL_GPL(lockdep_unregister_key);
6362 void __init lockdep_init(void)
6364 printk("Lock dependency validator: Copyright (c) 2006 Red Hat, Inc., Ingo Molnar\n");
6366 printk("... MAX_LOCKDEP_SUBCLASSES: %lu\n", MAX_LOCKDEP_SUBCLASSES);
6367 printk("... MAX_LOCK_DEPTH: %lu\n", MAX_LOCK_DEPTH);
6368 printk("... MAX_LOCKDEP_KEYS: %lu\n", MAX_LOCKDEP_KEYS);
6369 printk("... CLASSHASH_SIZE: %lu\n", CLASSHASH_SIZE);
6370 printk("... MAX_LOCKDEP_ENTRIES: %lu\n", MAX_LOCKDEP_ENTRIES);
6371 printk("... MAX_LOCKDEP_CHAINS: %lu\n", MAX_LOCKDEP_CHAINS);
6372 printk("... CHAINHASH_SIZE: %lu\n", CHAINHASH_SIZE);
6374 printk(" memory used by lock dependency info: %zu kB\n",
6375 (sizeof(lock_classes) +
6376 sizeof(lock_classes_in_use) +
6377 sizeof(classhash_table) +
6378 sizeof(list_entries) +
6379 sizeof(list_entries_in_use) +
6380 sizeof(chainhash_table) +
6381 sizeof(delayed_free)
6382 #ifdef CONFIG_PROVE_LOCKING
6384 + sizeof(lock_chains)
6385 + sizeof(lock_chains_in_use)
6386 + sizeof(chain_hlocks)
6391 #if defined(CONFIG_TRACE_IRQFLAGS) && defined(CONFIG_PROVE_LOCKING)
6392 printk(" memory used for stack traces: %zu kB\n",
6393 (sizeof(stack_trace) + sizeof(stack_trace_hash)) / 1024
6397 printk(" per task-struct memory footprint: %zu bytes\n",
6398 sizeof(((struct task_struct *)NULL)->held_locks));
6402 print_freed_lock_bug(struct task_struct *curr, const void *mem_from,
6403 const void *mem_to, struct held_lock *hlock)
6405 if (!debug_locks_off())
6407 if (debug_locks_silent)
6411 pr_warn("=========================\n");
6412 pr_warn("WARNING: held lock freed!\n");
6413 print_kernel_ident();
6414 pr_warn("-------------------------\n");
6415 pr_warn("%s/%d is freeing memory %px-%px, with a lock still held there!\n",
6416 curr->comm, task_pid_nr(curr), mem_from, mem_to-1);
6418 lockdep_print_held_locks(curr);
6420 pr_warn("\nstack backtrace:\n");
6424 static inline int not_in_range(const void* mem_from, unsigned long mem_len,
6425 const void* lock_from, unsigned long lock_len)
6427 return lock_from + lock_len <= mem_from ||
6428 mem_from + mem_len <= lock_from;
6432 * Called when kernel memory is freed (or unmapped), or if a lock
6433 * is destroyed or reinitialized - this code checks whether there is
6434 * any held lock in the memory range of <from> to <to>:
6436 void debug_check_no_locks_freed(const void *mem_from, unsigned long mem_len)
6438 struct task_struct *curr = current;
6439 struct held_lock *hlock;
6440 unsigned long flags;
6443 if (unlikely(!debug_locks))
6446 raw_local_irq_save(flags);
6447 for (i = 0; i < curr->lockdep_depth; i++) {
6448 hlock = curr->held_locks + i;
6450 if (not_in_range(mem_from, mem_len, hlock->instance,
6451 sizeof(*hlock->instance)))
6454 print_freed_lock_bug(curr, mem_from, mem_from + mem_len, hlock);
6457 raw_local_irq_restore(flags);
6459 EXPORT_SYMBOL_GPL(debug_check_no_locks_freed);
6461 static void print_held_locks_bug(void)
6463 if (!debug_locks_off())
6465 if (debug_locks_silent)
6469 pr_warn("====================================\n");
6470 pr_warn("WARNING: %s/%d still has locks held!\n",
6471 current->comm, task_pid_nr(current));
6472 print_kernel_ident();
6473 pr_warn("------------------------------------\n");
6474 lockdep_print_held_locks(current);
6475 pr_warn("\nstack backtrace:\n");
6479 void debug_check_no_locks_held(void)
6481 if (unlikely(current->lockdep_depth > 0))
6482 print_held_locks_bug();
6484 EXPORT_SYMBOL_GPL(debug_check_no_locks_held);
6487 void debug_show_all_locks(void)
6489 struct task_struct *g, *p;
6491 if (unlikely(!debug_locks)) {
6492 pr_warn("INFO: lockdep is turned off.\n");
6495 pr_warn("\nShowing all locks held in the system:\n");
6498 for_each_process_thread(g, p) {
6499 if (!p->lockdep_depth)
6501 lockdep_print_held_locks(p);
6502 touch_nmi_watchdog();
6503 touch_all_softlockup_watchdogs();
6508 pr_warn("=============================================\n\n");
6510 EXPORT_SYMBOL_GPL(debug_show_all_locks);
6514 * Careful: only use this function if you are sure that
6515 * the task cannot run in parallel!
6517 void debug_show_held_locks(struct task_struct *task)
6519 if (unlikely(!debug_locks)) {
6520 printk("INFO: lockdep is turned off.\n");
6523 lockdep_print_held_locks(task);
6525 EXPORT_SYMBOL_GPL(debug_show_held_locks);
6527 asmlinkage __visible void lockdep_sys_exit(void)
6529 struct task_struct *curr = current;
6531 if (unlikely(curr->lockdep_depth)) {
6532 if (!debug_locks_off())
6535 pr_warn("================================================\n");
6536 pr_warn("WARNING: lock held when returning to user space!\n");
6537 print_kernel_ident();
6538 pr_warn("------------------------------------------------\n");
6539 pr_warn("%s/%d is leaving the kernel with locks still held!\n",
6540 curr->comm, curr->pid);
6541 lockdep_print_held_locks(curr);
6545 * The lock history for each syscall should be independent. So wipe the
6546 * slate clean on return to userspace.
6548 lockdep_invariant_state(false);
6551 void lockdep_rcu_suspicious(const char *file, const int line, const char *s)
6553 struct task_struct *curr = current;
6554 int dl = READ_ONCE(debug_locks);
6556 /* Note: the following can be executed concurrently, so be careful. */
6558 pr_warn("=============================\n");
6559 pr_warn("WARNING: suspicious RCU usage\n");
6560 print_kernel_ident();
6561 pr_warn("-----------------------------\n");
6562 pr_warn("%s:%d %s!\n", file, line, s);
6563 pr_warn("\nother info that might help us debug this:\n\n");
6564 pr_warn("\n%srcu_scheduler_active = %d, debug_locks = %d\n%s",
6565 !rcu_lockdep_current_cpu_online()
6566 ? "RCU used illegally from offline CPU!\n"
6568 rcu_scheduler_active, dl,
6569 dl ? "" : "Possible false positive due to lockdep disabling via debug_locks = 0\n");
6572 * If a CPU is in the RCU-free window in idle (ie: in the section
6573 * between rcu_idle_enter() and rcu_idle_exit(), then RCU
6574 * considers that CPU to be in an "extended quiescent state",
6575 * which means that RCU will be completely ignoring that CPU.
6576 * Therefore, rcu_read_lock() and friends have absolutely no
6577 * effect on a CPU running in that state. In other words, even if
6578 * such an RCU-idle CPU has called rcu_read_lock(), RCU might well
6579 * delete data structures out from under it. RCU really has no
6580 * choice here: we need to keep an RCU-free window in idle where
6581 * the CPU may possibly enter into low power mode. This way we can
6582 * notice an extended quiescent state to other CPUs that started a grace
6583 * period. Otherwise we would delay any grace period as long as we run
6586 * So complain bitterly if someone does call rcu_read_lock(),
6587 * rcu_read_lock_bh() and so on from extended quiescent states.
6589 if (!rcu_is_watching())
6590 pr_warn("RCU used illegally from extended quiescent state!\n");
6592 lockdep_print_held_locks(curr);
6593 pr_warn("\nstack backtrace:\n");
6596 EXPORT_SYMBOL_GPL(lockdep_rcu_suspicious);