Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    3)  * kernel/lockdep.c
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    4)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    5)  * Runtime locking correctness validator
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    6)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    7)  * Started by Ingo Molnar:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    8)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    9)  *  Copyright (C) 2006,2007 Red Hat, Inc., Ingo Molnar <mingo@redhat.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   10)  *  Copyright (C) 2007 Red Hat, Inc., Peter Zijlstra
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   11)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   12)  * this code maps all the lock dependencies as they occur in a live kernel
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   13)  * and will warn about the following classes of locking bugs:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   14)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   15)  * - lock inversion scenarios
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   16)  * - circular lock dependencies
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   17)  * - hardirq/softirq safe/unsafe locking bugs
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   18)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   19)  * Bugs are reported even if the current locking scenario does not cause
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   20)  * any deadlock at this point.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   21)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   22)  * I.e. if anytime in the past two locks were taken in a different order,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   23)  * even if it happened for another task, even if those were different
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   24)  * locks (but of the same class as this lock), this code will detect it.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   25)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   26)  * Thanks to Arjan van de Ven for coming up with the initial idea of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   27)  * mapping lock dependencies runtime.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   28)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   29) #define DISABLE_BRANCH_PROFILING
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   30) #include <linux/mutex.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   31) #include <linux/sched.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   32) #include <linux/sched/clock.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   33) #include <linux/sched/task.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   34) #include <linux/sched/mm.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   35) #include <linux/delay.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   36) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   37) #include <linux/proc_fs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   38) #include <linux/seq_file.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   39) #include <linux/spinlock.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   40) #include <linux/kallsyms.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   41) #include <linux/interrupt.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   42) #include <linux/stacktrace.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   43) #include <linux/debug_locks.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   44) #include <linux/irqflags.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   45) #include <linux/utsname.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   46) #include <linux/hash.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   47) #include <linux/ftrace.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   48) #include <linux/stringify.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   49) #include <linux/bitmap.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   50) #include <linux/bitops.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   51) #include <linux/gfp.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   52) #include <linux/random.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   53) #include <linux/jhash.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   54) #include <linux/nmi.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   55) #include <linux/rcupdate.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   56) #include <linux/kprobes.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   57) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   58) #include <asm/sections.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   59) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   60) #include "lockdep_internals.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   61) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   62) #define CREATE_TRACE_POINTS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   63) #include <trace/events/lock.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   64) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   65) #ifdef CONFIG_PROVE_LOCKING
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   66) int prove_locking = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   67) module_param(prove_locking, int, 0644);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   68) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   69) #define prove_locking 0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   70) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   71) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   72) #ifdef CONFIG_LOCK_STAT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   73) int lock_stat = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   74) module_param(lock_stat, int, 0644);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   75) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   76) #define lock_stat 0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   77) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   78) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   79) DEFINE_PER_CPU(unsigned int, lockdep_recursion);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   80) EXPORT_PER_CPU_SYMBOL_GPL(lockdep_recursion);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   81) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   82) static __always_inline bool lockdep_enabled(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   83) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   84) 	if (!debug_locks)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   85) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   86) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   87) 	if (this_cpu_read(lockdep_recursion))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   88) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   89) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   90) 	if (current->lockdep_recursion)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   91) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   92) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   93) 	return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   94) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   95) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   96) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   97)  * lockdep_lock: protects the lockdep graph, the hashes and the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   98)  *               class/list/hash allocators.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   99)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  100)  * This is one of the rare exceptions where it's justified
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  101)  * to use a raw spinlock - we really dont want the spinlock
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  102)  * code to recurse back into the lockdep code...
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  103)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  104) static arch_spinlock_t __lock = (arch_spinlock_t)__ARCH_SPIN_LOCK_UNLOCKED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  105) static struct task_struct *__owner;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  106) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  107) static inline void lockdep_lock(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  108) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  109) 	DEBUG_LOCKS_WARN_ON(!irqs_disabled());
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  110) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  111) 	__this_cpu_inc(lockdep_recursion);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  112) 	arch_spin_lock(&__lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  113) 	__owner = current;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  114) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  115) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  116) static inline void lockdep_unlock(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  117) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  118) 	DEBUG_LOCKS_WARN_ON(!irqs_disabled());
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  119) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  120) 	if (debug_locks && DEBUG_LOCKS_WARN_ON(__owner != current))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  121) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  122) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  123) 	__owner = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  124) 	arch_spin_unlock(&__lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  125) 	__this_cpu_dec(lockdep_recursion);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  126) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  127) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  128) static inline bool lockdep_assert_locked(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  129) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  130) 	return DEBUG_LOCKS_WARN_ON(__owner != current);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  131) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  132) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  133) static struct task_struct *lockdep_selftest_task_struct;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  134) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  135) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  136) static int graph_lock(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  137) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  138) 	lockdep_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  139) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  140) 	 * Make sure that if another CPU detected a bug while
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  141) 	 * walking the graph we dont change it (while the other
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  142) 	 * CPU is busy printing out stuff with the graph lock
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  143) 	 * dropped already)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  144) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  145) 	if (!debug_locks) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  146) 		lockdep_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  147) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  148) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  149) 	return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  150) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  151) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  152) static inline void graph_unlock(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  153) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  154) 	lockdep_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  155) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  156) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  157) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  158)  * Turn lock debugging off and return with 0 if it was off already,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  159)  * and also release the graph lock:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  160)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  161) static inline int debug_locks_off_graph_unlock(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  162) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  163) 	int ret = debug_locks_off();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  164) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  165) 	lockdep_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  166) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  167) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  168) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  169) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  170) unsigned long nr_list_entries;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  171) static struct lock_list list_entries[MAX_LOCKDEP_ENTRIES];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  172) static DECLARE_BITMAP(list_entries_in_use, MAX_LOCKDEP_ENTRIES);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  173) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  174) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  175)  * All data structures here are protected by the global debug_lock.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  176)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  177)  * nr_lock_classes is the number of elements of lock_classes[] that is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  178)  * in use.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  179)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  180) #define KEYHASH_BITS		(MAX_LOCKDEP_KEYS_BITS - 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  181) #define KEYHASH_SIZE		(1UL << KEYHASH_BITS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  182) static struct hlist_head lock_keys_hash[KEYHASH_SIZE];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  183) unsigned long nr_lock_classes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  184) unsigned long nr_zapped_classes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  185) unsigned long max_lock_class_idx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  186) struct lock_class lock_classes[MAX_LOCKDEP_KEYS];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  187) DECLARE_BITMAP(lock_classes_in_use, MAX_LOCKDEP_KEYS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  188) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  189) static inline struct lock_class *hlock_class(struct held_lock *hlock)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  190) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  191) 	unsigned int class_idx = hlock->class_idx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  192) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  193) 	/* Don't re-read hlock->class_idx, can't use READ_ONCE() on bitfield */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  194) 	barrier();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  195) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  196) 	if (!test_bit(class_idx, lock_classes_in_use)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  197) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  198) 		 * Someone passed in garbage, we give up.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  199) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  200) 		DEBUG_LOCKS_WARN_ON(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  201) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  202) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  203) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  204) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  205) 	 * At this point, if the passed hlock->class_idx is still garbage,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  206) 	 * we just have to live with it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  207) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  208) 	return lock_classes + class_idx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  209) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  210) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  211) #ifdef CONFIG_LOCK_STAT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  212) static DEFINE_PER_CPU(struct lock_class_stats[MAX_LOCKDEP_KEYS], cpu_lock_stats);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  213) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  214) static inline u64 lockstat_clock(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  215) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  216) 	return local_clock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  217) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  218) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  219) static int lock_point(unsigned long points[], unsigned long ip)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  220) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  221) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  222) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  223) 	for (i = 0; i < LOCKSTAT_POINTS; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  224) 		if (points[i] == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  225) 			points[i] = ip;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  226) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  227) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  228) 		if (points[i] == ip)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  229) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  230) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  231) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  232) 	return i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  233) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  234) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  235) static void lock_time_inc(struct lock_time *lt, u64 time)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  236) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  237) 	if (time > lt->max)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  238) 		lt->max = time;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  239) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  240) 	if (time < lt->min || !lt->nr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  241) 		lt->min = time;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  242) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  243) 	lt->total += time;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  244) 	lt->nr++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  245) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  246) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  247) static inline void lock_time_add(struct lock_time *src, struct lock_time *dst)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  248) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  249) 	if (!src->nr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  250) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  251) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  252) 	if (src->max > dst->max)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  253) 		dst->max = src->max;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  254) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  255) 	if (src->min < dst->min || !dst->nr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  256) 		dst->min = src->min;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  257) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  258) 	dst->total += src->total;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  259) 	dst->nr += src->nr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  260) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  261) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  262) struct lock_class_stats lock_stats(struct lock_class *class)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  263) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  264) 	struct lock_class_stats stats;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  265) 	int cpu, i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  266) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  267) 	memset(&stats, 0, sizeof(struct lock_class_stats));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  268) 	for_each_possible_cpu(cpu) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  269) 		struct lock_class_stats *pcs =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  270) 			&per_cpu(cpu_lock_stats, cpu)[class - lock_classes];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  271) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  272) 		for (i = 0; i < ARRAY_SIZE(stats.contention_point); i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  273) 			stats.contention_point[i] += pcs->contention_point[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  274) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  275) 		for (i = 0; i < ARRAY_SIZE(stats.contending_point); i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  276) 			stats.contending_point[i] += pcs->contending_point[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  277) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  278) 		lock_time_add(&pcs->read_waittime, &stats.read_waittime);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  279) 		lock_time_add(&pcs->write_waittime, &stats.write_waittime);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  280) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  281) 		lock_time_add(&pcs->read_holdtime, &stats.read_holdtime);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  282) 		lock_time_add(&pcs->write_holdtime, &stats.write_holdtime);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  283) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  284) 		for (i = 0; i < ARRAY_SIZE(stats.bounces); i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  285) 			stats.bounces[i] += pcs->bounces[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  286) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  287) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  288) 	return stats;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  289) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  290) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  291) void clear_lock_stats(struct lock_class *class)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  292) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  293) 	int cpu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  294) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  295) 	for_each_possible_cpu(cpu) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  296) 		struct lock_class_stats *cpu_stats =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  297) 			&per_cpu(cpu_lock_stats, cpu)[class - lock_classes];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  298) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  299) 		memset(cpu_stats, 0, sizeof(struct lock_class_stats));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  300) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  301) 	memset(class->contention_point, 0, sizeof(class->contention_point));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  302) 	memset(class->contending_point, 0, sizeof(class->contending_point));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  303) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  304) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  305) static struct lock_class_stats *get_lock_stats(struct lock_class *class)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  306) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  307) 	return &this_cpu_ptr(cpu_lock_stats)[class - lock_classes];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  308) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  309) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  310) static void lock_release_holdtime(struct held_lock *hlock)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  311) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  312) 	struct lock_class_stats *stats;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  313) 	u64 holdtime;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  314) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  315) 	if (!lock_stat)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  316) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  317) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  318) 	holdtime = lockstat_clock() - hlock->holdtime_stamp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  319) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  320) 	stats = get_lock_stats(hlock_class(hlock));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  321) 	if (hlock->read)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  322) 		lock_time_inc(&stats->read_holdtime, holdtime);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  323) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  324) 		lock_time_inc(&stats->write_holdtime, holdtime);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  325) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  326) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  327) static inline void lock_release_holdtime(struct held_lock *hlock)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  328) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  329) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  330) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  331) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  332) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  333)  * We keep a global list of all lock classes. The list is only accessed with
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  334)  * the lockdep spinlock lock held. free_lock_classes is a list with free
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  335)  * elements. These elements are linked together by the lock_entry member in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  336)  * struct lock_class.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  337)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  338) static LIST_HEAD(all_lock_classes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  339) static LIST_HEAD(free_lock_classes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  340) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  341) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  342)  * struct pending_free - information about data structures about to be freed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  343)  * @zapped: Head of a list with struct lock_class elements.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  344)  * @lock_chains_being_freed: Bitmap that indicates which lock_chains[] elements
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  345)  *	are about to be freed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  346)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  347) struct pending_free {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  348) 	struct list_head zapped;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  349) 	DECLARE_BITMAP(lock_chains_being_freed, MAX_LOCKDEP_CHAINS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  350) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  351) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  352) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  353)  * struct delayed_free - data structures used for delayed freeing
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  354)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  355)  * A data structure for delayed freeing of data structures that may be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  356)  * accessed by RCU readers at the time these were freed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  357)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  358)  * @rcu_head:  Used to schedule an RCU callback for freeing data structures.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  359)  * @index:     Index of @pf to which freed data structures are added.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  360)  * @scheduled: Whether or not an RCU callback has been scheduled.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  361)  * @pf:        Array with information about data structures about to be freed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  362)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  363) static struct delayed_free {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  364) 	struct rcu_head		rcu_head;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  365) 	int			index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  366) 	int			scheduled;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  367) 	struct pending_free	pf[2];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  368) } delayed_free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  369) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  370) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  371)  * The lockdep classes are in a hash-table as well, for fast lookup:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  372)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  373) #define CLASSHASH_BITS		(MAX_LOCKDEP_KEYS_BITS - 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  374) #define CLASSHASH_SIZE		(1UL << CLASSHASH_BITS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  375) #define __classhashfn(key)	hash_long((unsigned long)key, CLASSHASH_BITS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  376) #define classhashentry(key)	(classhash_table + __classhashfn((key)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  377) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  378) static struct hlist_head classhash_table[CLASSHASH_SIZE];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  379) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  380) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  381)  * We put the lock dependency chains into a hash-table as well, to cache
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  382)  * their existence:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  383)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  384) #define CHAINHASH_BITS		(MAX_LOCKDEP_CHAINS_BITS-1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  385) #define CHAINHASH_SIZE		(1UL << CHAINHASH_BITS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  386) #define __chainhashfn(chain)	hash_long(chain, CHAINHASH_BITS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  387) #define chainhashentry(chain)	(chainhash_table + __chainhashfn((chain)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  388) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  389) static struct hlist_head chainhash_table[CHAINHASH_SIZE];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  390) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  391) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  392)  * the id of held_lock
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  393)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  394) static inline u16 hlock_id(struct held_lock *hlock)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  395) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  396) 	BUILD_BUG_ON(MAX_LOCKDEP_KEYS_BITS + 2 > 16);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  397) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  398) 	return (hlock->class_idx | (hlock->read << MAX_LOCKDEP_KEYS_BITS));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  399) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  400) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  401) static inline unsigned int chain_hlock_class_idx(u16 hlock_id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  402) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  403) 	return hlock_id & (MAX_LOCKDEP_KEYS - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  404) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  405) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  406) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  407)  * The hash key of the lock dependency chains is a hash itself too:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  408)  * it's a hash of all locks taken up to that lock, including that lock.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  409)  * It's a 64-bit hash, because it's important for the keys to be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  410)  * unique.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  411)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  412) static inline u64 iterate_chain_key(u64 key, u32 idx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  413) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  414) 	u32 k0 = key, k1 = key >> 32;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  415) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  416) 	__jhash_mix(idx, k0, k1); /* Macro that modifies arguments! */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  417) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  418) 	return k0 | (u64)k1 << 32;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  419) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  420) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  421) void lockdep_init_task(struct task_struct *task)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  422) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  423) 	task->lockdep_depth = 0; /* no locks held yet */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  424) 	task->curr_chain_key = INITIAL_CHAIN_KEY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  425) 	task->lockdep_recursion = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  426) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  427) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  428) static __always_inline void lockdep_recursion_inc(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  429) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  430) 	__this_cpu_inc(lockdep_recursion);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  431) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  432) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  433) static __always_inline void lockdep_recursion_finish(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  434) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  435) 	if (WARN_ON_ONCE(__this_cpu_dec_return(lockdep_recursion)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  436) 		__this_cpu_write(lockdep_recursion, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  437) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  438) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  439) void lockdep_set_selftest_task(struct task_struct *task)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  440) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  441) 	lockdep_selftest_task_struct = task;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  442) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  443) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  444) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  445)  * Debugging switches:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  446)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  447) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  448) #define VERBOSE			0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  449) #define VERY_VERBOSE		0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  450) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  451) #if VERBOSE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  452) # define HARDIRQ_VERBOSE	1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  453) # define SOFTIRQ_VERBOSE	1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  454) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  455) # define HARDIRQ_VERBOSE	0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  456) # define SOFTIRQ_VERBOSE	0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  457) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  458) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  459) #if VERBOSE || HARDIRQ_VERBOSE || SOFTIRQ_VERBOSE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  460) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  461)  * Quick filtering for interesting events:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  462)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  463) static int class_filter(struct lock_class *class)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  464) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  465) #if 0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  466) 	/* Example */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  467) 	if (class->name_version == 1 &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  468) 			!strcmp(class->name, "lockname"))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  469) 		return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  470) 	if (class->name_version == 1 &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  471) 			!strcmp(class->name, "&struct->lockfield"))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  472) 		return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  473) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  474) 	/* Filter everything else. 1 would be to allow everything else */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  475) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  476) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  477) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  478) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  479) static int verbose(struct lock_class *class)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  480) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  481) #if VERBOSE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  482) 	return class_filter(class);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  483) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  484) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  485) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  486) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  487) static void print_lockdep_off(const char *bug_msg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  488) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  489) 	printk(KERN_DEBUG "%s\n", bug_msg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  490) 	printk(KERN_DEBUG "turning off the locking correctness validator.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  491) #ifdef CONFIG_LOCK_STAT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  492) 	printk(KERN_DEBUG "Please attach the output of /proc/lock_stat to the bug report\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  493) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  494) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  495) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  496) unsigned long nr_stack_trace_entries;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  497) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  498) #ifdef CONFIG_PROVE_LOCKING
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  499) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  500)  * struct lock_trace - single stack backtrace
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  501)  * @hash_entry:	Entry in a stack_trace_hash[] list.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  502)  * @hash:	jhash() of @entries.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  503)  * @nr_entries:	Number of entries in @entries.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  504)  * @entries:	Actual stack backtrace.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  505)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  506) struct lock_trace {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  507) 	struct hlist_node	hash_entry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  508) 	u32			hash;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  509) 	u32			nr_entries;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  510) 	unsigned long		entries[] __aligned(sizeof(unsigned long));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  511) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  512) #define LOCK_TRACE_SIZE_IN_LONGS				\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  513) 	(sizeof(struct lock_trace) / sizeof(unsigned long))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  514) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  515)  * Stack-trace: sequence of lock_trace structures. Protected by the graph_lock.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  516)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  517) static unsigned long stack_trace[MAX_STACK_TRACE_ENTRIES];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  518) static struct hlist_head stack_trace_hash[STACK_TRACE_HASH_SIZE];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  519) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  520) static bool traces_identical(struct lock_trace *t1, struct lock_trace *t2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  521) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  522) 	return t1->hash == t2->hash && t1->nr_entries == t2->nr_entries &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  523) 		memcmp(t1->entries, t2->entries,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  524) 		       t1->nr_entries * sizeof(t1->entries[0])) == 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  525) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  526) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  527) static struct lock_trace *save_trace(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  528) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  529) 	struct lock_trace *trace, *t2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  530) 	struct hlist_head *hash_head;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  531) 	u32 hash;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  532) 	int max_entries;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  533) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  534) 	BUILD_BUG_ON_NOT_POWER_OF_2(STACK_TRACE_HASH_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  535) 	BUILD_BUG_ON(LOCK_TRACE_SIZE_IN_LONGS >= MAX_STACK_TRACE_ENTRIES);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  536) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  537) 	trace = (struct lock_trace *)(stack_trace + nr_stack_trace_entries);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  538) 	max_entries = MAX_STACK_TRACE_ENTRIES - nr_stack_trace_entries -
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  539) 		LOCK_TRACE_SIZE_IN_LONGS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  540) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  541) 	if (max_entries <= 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  542) 		if (!debug_locks_off_graph_unlock())
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  543) 			return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  544) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  545) 		print_lockdep_off("BUG: MAX_STACK_TRACE_ENTRIES too low!");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  546) 		dump_stack();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  547) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  548) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  549) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  550) 	trace->nr_entries = stack_trace_save(trace->entries, max_entries, 3);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  551) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  552) 	hash = jhash(trace->entries, trace->nr_entries *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  553) 		     sizeof(trace->entries[0]), 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  554) 	trace->hash = hash;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  555) 	hash_head = stack_trace_hash + (hash & (STACK_TRACE_HASH_SIZE - 1));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  556) 	hlist_for_each_entry(t2, hash_head, hash_entry) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  557) 		if (traces_identical(trace, t2))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  558) 			return t2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  559) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  560) 	nr_stack_trace_entries += LOCK_TRACE_SIZE_IN_LONGS + trace->nr_entries;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  561) 	hlist_add_head(&trace->hash_entry, hash_head);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  562) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  563) 	return trace;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  564) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  565) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  566) /* Return the number of stack traces in the stack_trace[] array. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  567) u64 lockdep_stack_trace_count(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  568) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  569) 	struct lock_trace *trace;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  570) 	u64 c = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  571) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  572) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  573) 	for (i = 0; i < ARRAY_SIZE(stack_trace_hash); i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  574) 		hlist_for_each_entry(trace, &stack_trace_hash[i], hash_entry) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  575) 			c++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  576) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  577) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  578) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  579) 	return c;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  580) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  581) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  582) /* Return the number of stack hash chains that have at least one stack trace. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  583) u64 lockdep_stack_hash_count(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  584) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  585) 	u64 c = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  586) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  587) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  588) 	for (i = 0; i < ARRAY_SIZE(stack_trace_hash); i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  589) 		if (!hlist_empty(&stack_trace_hash[i]))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  590) 			c++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  591) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  592) 	return c;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  593) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  594) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  595) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  596) unsigned int nr_hardirq_chains;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  597) unsigned int nr_softirq_chains;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  598) unsigned int nr_process_chains;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  599) unsigned int max_lockdep_depth;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  600) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  601) #ifdef CONFIG_DEBUG_LOCKDEP
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  602) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  603)  * Various lockdep statistics:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  604)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  605) DEFINE_PER_CPU(struct lockdep_stats, lockdep_stats);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  606) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  607) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  608) #ifdef CONFIG_PROVE_LOCKING
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  609) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  610)  * Locking printouts:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  611)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  612) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  613) #define __USAGE(__STATE)						\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  614) 	[LOCK_USED_IN_##__STATE] = "IN-"__stringify(__STATE)"-W",	\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  615) 	[LOCK_ENABLED_##__STATE] = __stringify(__STATE)"-ON-W",		\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  616) 	[LOCK_USED_IN_##__STATE##_READ] = "IN-"__stringify(__STATE)"-R",\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  617) 	[LOCK_ENABLED_##__STATE##_READ] = __stringify(__STATE)"-ON-R",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  618) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  619) static const char *usage_str[] =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  620) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  621) #define LOCKDEP_STATE(__STATE) __USAGE(__STATE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  622) #include "lockdep_states.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  623) #undef LOCKDEP_STATE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  624) 	[LOCK_USED] = "INITIAL USE",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  625) 	[LOCK_USED_READ] = "INITIAL READ USE",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  626) 	/* abused as string storage for verify_lock_unused() */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  627) 	[LOCK_USAGE_STATES] = "IN-NMI",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  628) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  629) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  630) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  631) const char *__get_key_name(const struct lockdep_subclass_key *key, char *str)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  632) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  633) 	return kallsyms_lookup((unsigned long)key, NULL, NULL, NULL, str);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  634) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  635) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  636) static inline unsigned long lock_flag(enum lock_usage_bit bit)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  637) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  638) 	return 1UL << bit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  639) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  640) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  641) static char get_usage_char(struct lock_class *class, enum lock_usage_bit bit)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  642) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  643) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  644) 	 * The usage character defaults to '.' (i.e., irqs disabled and not in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  645) 	 * irq context), which is the safest usage category.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  646) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  647) 	char c = '.';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  648) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  649) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  650) 	 * The order of the following usage checks matters, which will
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  651) 	 * result in the outcome character as follows:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  652) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  653) 	 * - '+': irq is enabled and not in irq context
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  654) 	 * - '-': in irq context and irq is disabled
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  655) 	 * - '?': in irq context and irq is enabled
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  656) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  657) 	if (class->usage_mask & lock_flag(bit + LOCK_USAGE_DIR_MASK)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  658) 		c = '+';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  659) 		if (class->usage_mask & lock_flag(bit))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  660) 			c = '?';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  661) 	} else if (class->usage_mask & lock_flag(bit))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  662) 		c = '-';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  663) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  664) 	return c;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  665) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  666) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  667) void get_usage_chars(struct lock_class *class, char usage[LOCK_USAGE_CHARS])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  668) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  669) 	int i = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  670) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  671) #define LOCKDEP_STATE(__STATE) 						\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  672) 	usage[i++] = get_usage_char(class, LOCK_USED_IN_##__STATE);	\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  673) 	usage[i++] = get_usage_char(class, LOCK_USED_IN_##__STATE##_READ);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  674) #include "lockdep_states.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  675) #undef LOCKDEP_STATE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  676) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  677) 	usage[i] = '\0';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  678) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  679) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  680) static void __print_lock_name(struct lock_class *class)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  681) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  682) 	char str[KSYM_NAME_LEN];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  683) 	const char *name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  684) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  685) 	name = class->name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  686) 	if (!name) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  687) 		name = __get_key_name(class->key, str);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  688) 		printk(KERN_CONT "%s", name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  689) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  690) 		printk(KERN_CONT "%s", name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  691) 		if (class->name_version > 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  692) 			printk(KERN_CONT "#%d", class->name_version);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  693) 		if (class->subclass)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  694) 			printk(KERN_CONT "/%d", class->subclass);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  695) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  696) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  697) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  698) static void print_lock_name(struct lock_class *class)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  699) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  700) 	char usage[LOCK_USAGE_CHARS];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  701) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  702) 	get_usage_chars(class, usage);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  703) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  704) 	printk(KERN_CONT " (");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  705) 	__print_lock_name(class);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  706) 	printk(KERN_CONT "){%s}-{%d:%d}", usage,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  707) 			class->wait_type_outer ?: class->wait_type_inner,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  708) 			class->wait_type_inner);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  709) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  710) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  711) static void print_lockdep_cache(struct lockdep_map *lock)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  712) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  713) 	const char *name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  714) 	char str[KSYM_NAME_LEN];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  715) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  716) 	name = lock->name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  717) 	if (!name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  718) 		name = __get_key_name(lock->key->subkeys, str);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  719) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  720) 	printk(KERN_CONT "%s", name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  721) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  722) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  723) static void print_lock(struct held_lock *hlock)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  724) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  725) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  726) 	 * We can be called locklessly through debug_show_all_locks() so be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  727) 	 * extra careful, the hlock might have been released and cleared.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  728) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  729) 	 * If this indeed happens, lets pretend it does not hurt to continue
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  730) 	 * to print the lock unless the hlock class_idx does not point to a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  731) 	 * registered class. The rationale here is: since we don't attempt
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  732) 	 * to distinguish whether we are in this situation, if it just
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  733) 	 * happened we can't count on class_idx to tell either.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  734) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  735) 	struct lock_class *lock = hlock_class(hlock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  736) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  737) 	if (!lock) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  738) 		printk(KERN_CONT "<RELEASED>\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  739) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  740) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  741) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  742) 	printk(KERN_CONT "%px", hlock->instance);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  743) 	print_lock_name(lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  744) 	printk(KERN_CONT ", at: %pS\n", (void *)hlock->acquire_ip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  745) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  746) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  747) static void lockdep_print_held_locks(struct task_struct *p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  748) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  749) 	int i, depth = READ_ONCE(p->lockdep_depth);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  750) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  751) 	if (!depth)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  752) 		printk("no locks held by %s/%d.\n", p->comm, task_pid_nr(p));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  753) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  754) 		printk("%d lock%s held by %s/%d:\n", depth,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  755) 		       depth > 1 ? "s" : "", p->comm, task_pid_nr(p));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  756) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  757) 	 * It's not reliable to print a task's held locks if it's not sleeping
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  758) 	 * and it's not the current task.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  759) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  760) 	if (p->state == TASK_RUNNING && p != current)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  761) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  762) 	for (i = 0; i < depth; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  763) 		printk(" #%d: ", i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  764) 		print_lock(p->held_locks + i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  765) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  766) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  767) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  768) static void print_kernel_ident(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  769) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  770) 	printk("%s %.*s %s\n", init_utsname()->release,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  771) 		(int)strcspn(init_utsname()->version, " "),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  772) 		init_utsname()->version,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  773) 		print_tainted());
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  774) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  775) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  776) static int very_verbose(struct lock_class *class)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  777) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  778) #if VERY_VERBOSE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  779) 	return class_filter(class);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  780) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  781) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  782) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  783) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  784) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  785)  * Is this the address of a static object:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  786)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  787) #ifdef __KERNEL__
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  788) static int static_obj(const void *obj)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  789) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  790) 	unsigned long start = (unsigned long) &_stext,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  791) 		      end   = (unsigned long) &_end,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  792) 		      addr  = (unsigned long) obj;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  793) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  794) 	if (arch_is_kernel_initmem_freed(addr))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  795) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  796) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  797) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  798) 	 * static variable?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  799) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  800) 	if ((addr >= start) && (addr < end))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  801) 		return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  802) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  803) 	if (arch_is_kernel_data(addr))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  804) 		return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  805) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  806) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  807) 	 * in-kernel percpu var?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  808) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  809) 	if (is_kernel_percpu_address(addr))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  810) 		return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  811) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  812) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  813) 	 * module static or percpu var?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  814) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  815) 	return is_module_address(addr) || is_module_percpu_address(addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  816) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  817) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  818) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  819) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  820)  * To make lock name printouts unique, we calculate a unique
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  821)  * class->name_version generation counter. The caller must hold the graph
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  822)  * lock.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  823)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  824) static int count_matching_names(struct lock_class *new_class)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  825) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  826) 	struct lock_class *class;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  827) 	int count = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  828) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  829) 	if (!new_class->name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  830) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  831) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  832) 	list_for_each_entry(class, &all_lock_classes, lock_entry) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  833) 		if (new_class->key - new_class->subclass == class->key)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  834) 			return class->name_version;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  835) 		if (class->name && !strcmp(class->name, new_class->name))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  836) 			count = max(count, class->name_version);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  837) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  838) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  839) 	return count + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  840) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  841) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  842) /* used from NMI context -- must be lockless */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  843) static noinstr struct lock_class *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  844) look_up_lock_class(const struct lockdep_map *lock, unsigned int subclass)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  845) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  846) 	struct lockdep_subclass_key *key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  847) 	struct hlist_head *hash_head;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  848) 	struct lock_class *class;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  849) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  850) 	if (unlikely(subclass >= MAX_LOCKDEP_SUBCLASSES)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  851) 		instrumentation_begin();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  852) 		debug_locks_off();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  853) 		printk(KERN_ERR
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  854) 			"BUG: looking up invalid subclass: %u\n", subclass);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  855) 		printk(KERN_ERR
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  856) 			"turning off the locking correctness validator.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  857) 		dump_stack();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  858) 		instrumentation_end();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  859) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  860) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  861) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  862) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  863) 	 * If it is not initialised then it has never been locked,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  864) 	 * so it won't be present in the hash table.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  865) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  866) 	if (unlikely(!lock->key))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  867) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  868) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  869) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  870) 	 * NOTE: the class-key must be unique. For dynamic locks, a static
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  871) 	 * lock_class_key variable is passed in through the mutex_init()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  872) 	 * (or spin_lock_init()) call - which acts as the key. For static
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  873) 	 * locks we use the lock object itself as the key.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  874) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  875) 	BUILD_BUG_ON(sizeof(struct lock_class_key) >
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  876) 			sizeof(struct lockdep_map));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  877) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  878) 	key = lock->key->subkeys + subclass;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  879) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  880) 	hash_head = classhashentry(key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  881) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  882) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  883) 	 * We do an RCU walk of the hash, see lockdep_free_key_range().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  884) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  885) 	if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  886) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  887) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  888) 	hlist_for_each_entry_rcu_notrace(class, hash_head, hash_entry) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  889) 		if (class->key == key) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  890) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  891) 			 * Huh! same key, different name? Did someone trample
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  892) 			 * on some memory? We're most confused.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  893) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  894) 			WARN_ON_ONCE(class->name != lock->name &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  895) 				     lock->key != &__lockdep_no_validate__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  896) 			return class;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  897) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  898) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  899) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  900) 	return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  901) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  902) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  903) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  904)  * Static locks do not have their class-keys yet - for them the key is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  905)  * the lock object itself. If the lock is in the per cpu area, the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  906)  * canonical address of the lock (per cpu offset removed) is used.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  907)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  908) static bool assign_lock_key(struct lockdep_map *lock)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  909) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  910) 	unsigned long can_addr, addr = (unsigned long)lock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  911) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  912) #ifdef __KERNEL__
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  913) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  914) 	 * lockdep_free_key_range() assumes that struct lock_class_key
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  915) 	 * objects do not overlap. Since we use the address of lock
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  916) 	 * objects as class key for static objects, check whether the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  917) 	 * size of lock_class_key objects does not exceed the size of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  918) 	 * the smallest lock object.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  919) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  920) 	BUILD_BUG_ON(sizeof(struct lock_class_key) > sizeof(raw_spinlock_t));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  921) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  922) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  923) 	if (__is_kernel_percpu_address(addr, &can_addr))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  924) 		lock->key = (void *)can_addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  925) 	else if (__is_module_percpu_address(addr, &can_addr))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  926) 		lock->key = (void *)can_addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  927) 	else if (static_obj(lock))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  928) 		lock->key = (void *)lock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  929) 	else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  930) 		/* Debug-check: all keys must be persistent! */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  931) 		debug_locks_off();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  932) 		pr_err("INFO: trying to register non-static key.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  933) 		pr_err("The code is fine but needs lockdep annotation, or maybe\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  934) 		pr_err("you didn't initialize this object before use?\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  935) 		pr_err("turning off the locking correctness validator.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  936) 		dump_stack();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  937) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  938) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  939) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  940) 	return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  941) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  942) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  943) #ifdef CONFIG_DEBUG_LOCKDEP
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  944) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  945) /* Check whether element @e occurs in list @h */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  946) static bool in_list(struct list_head *e, struct list_head *h)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  947) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  948) 	struct list_head *f;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  949) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  950) 	list_for_each(f, h) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  951) 		if (e == f)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  952) 			return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  953) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  954) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  955) 	return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  956) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  957) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  958) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  959)  * Check whether entry @e occurs in any of the locks_after or locks_before
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  960)  * lists.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  961)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  962) static bool in_any_class_list(struct list_head *e)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  963) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  964) 	struct lock_class *class;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  965) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  966) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  967) 	for (i = 0; i < ARRAY_SIZE(lock_classes); i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  968) 		class = &lock_classes[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  969) 		if (in_list(e, &class->locks_after) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  970) 		    in_list(e, &class->locks_before))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  971) 			return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  972) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  973) 	return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  974) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  975) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  976) static bool class_lock_list_valid(struct lock_class *c, struct list_head *h)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  977) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  978) 	struct lock_list *e;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  979) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  980) 	list_for_each_entry(e, h, entry) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  981) 		if (e->links_to != c) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  982) 			printk(KERN_INFO "class %s: mismatch for lock entry %ld; class %s <> %s",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  983) 			       c->name ? : "(?)",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  984) 			       (unsigned long)(e - list_entries),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  985) 			       e->links_to && e->links_to->name ?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  986) 			       e->links_to->name : "(?)",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  987) 			       e->class && e->class->name ? e->class->name :
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  988) 			       "(?)");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  989) 			return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  990) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  991) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  992) 	return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  993) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  994) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  995) #ifdef CONFIG_PROVE_LOCKING
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  996) static u16 chain_hlocks[MAX_LOCKDEP_CHAIN_HLOCKS];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  997) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  998) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  999) static bool check_lock_chain_key(struct lock_chain *chain)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1000) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1001) #ifdef CONFIG_PROVE_LOCKING
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1002) 	u64 chain_key = INITIAL_CHAIN_KEY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1003) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1004) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1005) 	for (i = chain->base; i < chain->base + chain->depth; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1006) 		chain_key = iterate_chain_key(chain_key, chain_hlocks[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1007) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1008) 	 * The 'unsigned long long' casts avoid that a compiler warning
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1009) 	 * is reported when building tools/lib/lockdep.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1010) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1011) 	if (chain->chain_key != chain_key) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1012) 		printk(KERN_INFO "chain %lld: key %#llx <> %#llx\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1013) 		       (unsigned long long)(chain - lock_chains),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1014) 		       (unsigned long long)chain->chain_key,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1015) 		       (unsigned long long)chain_key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1016) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1017) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1018) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1019) 	return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1020) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1021) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1022) static bool in_any_zapped_class_list(struct lock_class *class)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1023) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1024) 	struct pending_free *pf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1025) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1026) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1027) 	for (i = 0, pf = delayed_free.pf; i < ARRAY_SIZE(delayed_free.pf); i++, pf++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1028) 		if (in_list(&class->lock_entry, &pf->zapped))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1029) 			return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1030) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1031) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1032) 	return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1033) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1034) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1035) static bool __check_data_structures(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1036) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1037) 	struct lock_class *class;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1038) 	struct lock_chain *chain;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1039) 	struct hlist_head *head;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1040) 	struct lock_list *e;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1041) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1042) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1043) 	/* Check whether all classes occur in a lock list. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1044) 	for (i = 0; i < ARRAY_SIZE(lock_classes); i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1045) 		class = &lock_classes[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1046) 		if (!in_list(&class->lock_entry, &all_lock_classes) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1047) 		    !in_list(&class->lock_entry, &free_lock_classes) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1048) 		    !in_any_zapped_class_list(class)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1049) 			printk(KERN_INFO "class %px/%s is not in any class list\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1050) 			       class, class->name ? : "(?)");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1051) 			return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1052) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1053) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1054) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1055) 	/* Check whether all classes have valid lock lists. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1056) 	for (i = 0; i < ARRAY_SIZE(lock_classes); i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1057) 		class = &lock_classes[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1058) 		if (!class_lock_list_valid(class, &class->locks_before))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1059) 			return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1060) 		if (!class_lock_list_valid(class, &class->locks_after))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1061) 			return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1062) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1063) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1064) 	/* Check the chain_key of all lock chains. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1065) 	for (i = 0; i < ARRAY_SIZE(chainhash_table); i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1066) 		head = chainhash_table + i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1067) 		hlist_for_each_entry_rcu(chain, head, entry) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1068) 			if (!check_lock_chain_key(chain))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1069) 				return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1070) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1071) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1072) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1073) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1074) 	 * Check whether all list entries that are in use occur in a class
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1075) 	 * lock list.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1076) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1077) 	for_each_set_bit(i, list_entries_in_use, ARRAY_SIZE(list_entries)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1078) 		e = list_entries + i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1079) 		if (!in_any_class_list(&e->entry)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1080) 			printk(KERN_INFO "list entry %d is not in any class list; class %s <> %s\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1081) 			       (unsigned int)(e - list_entries),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1082) 			       e->class->name ? : "(?)",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1083) 			       e->links_to->name ? : "(?)");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1084) 			return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1085) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1086) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1087) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1088) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1089) 	 * Check whether all list entries that are not in use do not occur in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1090) 	 * a class lock list.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1091) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1092) 	for_each_clear_bit(i, list_entries_in_use, ARRAY_SIZE(list_entries)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1093) 		e = list_entries + i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1094) 		if (in_any_class_list(&e->entry)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1095) 			printk(KERN_INFO "list entry %d occurs in a class list; class %s <> %s\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1096) 			       (unsigned int)(e - list_entries),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1097) 			       e->class && e->class->name ? e->class->name :
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1098) 			       "(?)",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1099) 			       e->links_to && e->links_to->name ?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1100) 			       e->links_to->name : "(?)");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1101) 			return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1102) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1103) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1104) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1105) 	return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1106) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1107) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1108) int check_consistency = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1109) module_param(check_consistency, int, 0644);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1110) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1111) static void check_data_structures(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1112) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1113) 	static bool once = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1114) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1115) 	if (check_consistency && !once) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1116) 		if (!__check_data_structures()) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1117) 			once = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1118) 			WARN_ON(once);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1119) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1120) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1121) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1122) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1123) #else /* CONFIG_DEBUG_LOCKDEP */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1124) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1125) static inline void check_data_structures(void) { }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1126) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1127) #endif /* CONFIG_DEBUG_LOCKDEP */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1128) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1129) static void init_chain_block_buckets(void);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1130) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1131) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1132)  * Initialize the lock_classes[] array elements, the free_lock_classes list
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1133)  * and also the delayed_free structure.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1134)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1135) static void init_data_structures_once(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1136) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1137) 	static bool __read_mostly ds_initialized, rcu_head_initialized;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1138) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1139) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1140) 	if (likely(rcu_head_initialized))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1141) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1142) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1143) 	if (system_state >= SYSTEM_SCHEDULING) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1144) 		init_rcu_head(&delayed_free.rcu_head);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1145) 		rcu_head_initialized = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1146) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1147) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1148) 	if (ds_initialized)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1149) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1150) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1151) 	ds_initialized = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1152) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1153) 	INIT_LIST_HEAD(&delayed_free.pf[0].zapped);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1154) 	INIT_LIST_HEAD(&delayed_free.pf[1].zapped);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1155) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1156) 	for (i = 0; i < ARRAY_SIZE(lock_classes); i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1157) 		list_add_tail(&lock_classes[i].lock_entry, &free_lock_classes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1158) 		INIT_LIST_HEAD(&lock_classes[i].locks_after);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1159) 		INIT_LIST_HEAD(&lock_classes[i].locks_before);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1160) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1161) 	init_chain_block_buckets();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1162) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1163) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1164) static inline struct hlist_head *keyhashentry(const struct lock_class_key *key)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1165) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1166) 	unsigned long hash = hash_long((uintptr_t)key, KEYHASH_BITS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1167) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1168) 	return lock_keys_hash + hash;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1169) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1170) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1171) /* Register a dynamically allocated key. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1172) void lockdep_register_key(struct lock_class_key *key)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1173) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1174) 	struct hlist_head *hash_head;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1175) 	struct lock_class_key *k;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1176) 	unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1177) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1178) 	if (WARN_ON_ONCE(static_obj(key)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1179) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1180) 	hash_head = keyhashentry(key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1181) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1182) 	raw_local_irq_save(flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1183) 	if (!graph_lock())
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1184) 		goto restore_irqs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1185) 	hlist_for_each_entry_rcu(k, hash_head, hash_entry) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1186) 		if (WARN_ON_ONCE(k == key))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1187) 			goto out_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1188) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1189) 	hlist_add_head_rcu(&key->hash_entry, hash_head);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1190) out_unlock:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1191) 	graph_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1192) restore_irqs:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1193) 	raw_local_irq_restore(flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1194) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1195) EXPORT_SYMBOL_GPL(lockdep_register_key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1196) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1197) /* Check whether a key has been registered as a dynamic key. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1198) static bool is_dynamic_key(const struct lock_class_key *key)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1199) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1200) 	struct hlist_head *hash_head;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1201) 	struct lock_class_key *k;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1202) 	bool found = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1203) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1204) 	if (WARN_ON_ONCE(static_obj(key)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1205) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1206) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1207) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1208) 	 * If lock debugging is disabled lock_keys_hash[] may contain
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1209) 	 * pointers to memory that has already been freed. Avoid triggering
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1210) 	 * a use-after-free in that case by returning early.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1211) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1212) 	if (!debug_locks)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1213) 		return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1214) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1215) 	hash_head = keyhashentry(key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1216) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1217) 	rcu_read_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1218) 	hlist_for_each_entry_rcu(k, hash_head, hash_entry) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1219) 		if (k == key) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1220) 			found = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1221) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1222) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1223) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1224) 	rcu_read_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1225) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1226) 	return found;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1227) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1228) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1229) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1230)  * Register a lock's class in the hash-table, if the class is not present
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1231)  * yet. Otherwise we look it up. We cache the result in the lock object
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1232)  * itself, so actual lookup of the hash should be once per lock object.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1233)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1234) static struct lock_class *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1235) register_lock_class(struct lockdep_map *lock, unsigned int subclass, int force)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1236) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1237) 	struct lockdep_subclass_key *key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1238) 	struct hlist_head *hash_head;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1239) 	struct lock_class *class;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1240) 	int idx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1241) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1242) 	DEBUG_LOCKS_WARN_ON(!irqs_disabled());
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1243) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1244) 	class = look_up_lock_class(lock, subclass);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1245) 	if (likely(class))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1246) 		goto out_set_class_cache;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1247) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1248) 	if (!lock->key) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1249) 		if (!assign_lock_key(lock))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1250) 			return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1251) 	} else if (!static_obj(lock->key) && !is_dynamic_key(lock->key)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1252) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1253) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1254) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1255) 	key = lock->key->subkeys + subclass;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1256) 	hash_head = classhashentry(key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1257) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1258) 	if (!graph_lock()) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1259) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1260) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1261) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1262) 	 * We have to do the hash-walk again, to avoid races
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1263) 	 * with another CPU:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1264) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1265) 	hlist_for_each_entry_rcu(class, hash_head, hash_entry) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1266) 		if (class->key == key)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1267) 			goto out_unlock_set;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1268) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1269) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1270) 	init_data_structures_once();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1271) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1272) 	/* Allocate a new lock class and add it to the hash. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1273) 	class = list_first_entry_or_null(&free_lock_classes, typeof(*class),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1274) 					 lock_entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1275) 	if (!class) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1276) 		if (!debug_locks_off_graph_unlock()) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1277) 			return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1278) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1279) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1280) 		print_lockdep_off("BUG: MAX_LOCKDEP_KEYS too low!");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1281) 		dump_stack();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1282) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1283) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1284) 	nr_lock_classes++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1285) 	__set_bit(class - lock_classes, lock_classes_in_use);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1286) 	debug_atomic_inc(nr_unused_locks);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1287) 	class->key = key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1288) 	class->name = lock->name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1289) 	class->subclass = subclass;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1290) 	WARN_ON_ONCE(!list_empty(&class->locks_before));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1291) 	WARN_ON_ONCE(!list_empty(&class->locks_after));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1292) 	class->name_version = count_matching_names(class);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1293) 	class->wait_type_inner = lock->wait_type_inner;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1294) 	class->wait_type_outer = lock->wait_type_outer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1295) 	class->lock_type = lock->lock_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1296) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1297) 	 * We use RCU's safe list-add method to make
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1298) 	 * parallel walking of the hash-list safe:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1299) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1300) 	hlist_add_head_rcu(&class->hash_entry, hash_head);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1301) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1302) 	 * Remove the class from the free list and add it to the global list
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1303) 	 * of classes.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1304) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1305) 	list_move_tail(&class->lock_entry, &all_lock_classes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1306) 	idx = class - lock_classes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1307) 	if (idx > max_lock_class_idx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1308) 		max_lock_class_idx = idx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1309) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1310) 	if (verbose(class)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1311) 		graph_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1312) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1313) 		printk("\nnew class %px: %s", class->key, class->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1314) 		if (class->name_version > 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1315) 			printk(KERN_CONT "#%d", class->name_version);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1316) 		printk(KERN_CONT "\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1317) 		dump_stack();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1318) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1319) 		if (!graph_lock()) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1320) 			return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1321) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1322) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1323) out_unlock_set:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1324) 	graph_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1325) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1326) out_set_class_cache:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1327) 	if (!subclass || force)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1328) 		lock->class_cache[0] = class;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1329) 	else if (subclass < NR_LOCKDEP_CACHING_CLASSES)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1330) 		lock->class_cache[subclass] = class;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1331) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1332) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1333) 	 * Hash collision, did we smoke some? We found a class with a matching
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1334) 	 * hash but the subclass -- which is hashed in -- didn't match.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1335) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1336) 	if (DEBUG_LOCKS_WARN_ON(class->subclass != subclass))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1337) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1338) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1339) 	return class;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1340) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1341) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1342) #ifdef CONFIG_PROVE_LOCKING
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1343) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1344)  * Allocate a lockdep entry. (assumes the graph_lock held, returns
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1345)  * with NULL on failure)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1346)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1347) static struct lock_list *alloc_list_entry(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1348) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1349) 	int idx = find_first_zero_bit(list_entries_in_use,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1350) 				      ARRAY_SIZE(list_entries));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1351) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1352) 	if (idx >= ARRAY_SIZE(list_entries)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1353) 		if (!debug_locks_off_graph_unlock())
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1354) 			return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1355) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1356) 		print_lockdep_off("BUG: MAX_LOCKDEP_ENTRIES too low!");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1357) 		dump_stack();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1358) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1359) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1360) 	nr_list_entries++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1361) 	__set_bit(idx, list_entries_in_use);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1362) 	return list_entries + idx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1363) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1364) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1365) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1366)  * Add a new dependency to the head of the list:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1367)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1368) static int add_lock_to_list(struct lock_class *this,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1369) 			    struct lock_class *links_to, struct list_head *head,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1370) 			    unsigned long ip, u16 distance, u8 dep,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1371) 			    const struct lock_trace *trace)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1372) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1373) 	struct lock_list *entry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1374) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1375) 	 * Lock not present yet - get a new dependency struct and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1376) 	 * add it to the list:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1377) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1378) 	entry = alloc_list_entry();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1379) 	if (!entry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1380) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1381) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1382) 	entry->class = this;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1383) 	entry->links_to = links_to;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1384) 	entry->dep = dep;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1385) 	entry->distance = distance;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1386) 	entry->trace = trace;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1387) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1388) 	 * Both allocation and removal are done under the graph lock; but
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1389) 	 * iteration is under RCU-sched; see look_up_lock_class() and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1390) 	 * lockdep_free_key_range().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1391) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1392) 	list_add_tail_rcu(&entry->entry, head);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1393) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1394) 	return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1395) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1396) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1397) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1398)  * For good efficiency of modular, we use power of 2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1399)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1400) #define MAX_CIRCULAR_QUEUE_SIZE		4096UL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1401) #define CQ_MASK				(MAX_CIRCULAR_QUEUE_SIZE-1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1402) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1403) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1404)  * The circular_queue and helpers are used to implement graph
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1405)  * breadth-first search (BFS) algorithm, by which we can determine
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1406)  * whether there is a path from a lock to another. In deadlock checks,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1407)  * a path from the next lock to be acquired to a previous held lock
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1408)  * indicates that adding the <prev> -> <next> lock dependency will
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1409)  * produce a circle in the graph. Breadth-first search instead of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1410)  * depth-first search is used in order to find the shortest (circular)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1411)  * path.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1412)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1413) struct circular_queue {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1414) 	struct lock_list *element[MAX_CIRCULAR_QUEUE_SIZE];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1415) 	unsigned int  front, rear;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1416) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1417) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1418) static struct circular_queue lock_cq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1419) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1420) unsigned int max_bfs_queue_depth;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1421) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1422) static unsigned int lockdep_dependency_gen_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1423) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1424) static inline void __cq_init(struct circular_queue *cq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1425) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1426) 	cq->front = cq->rear = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1427) 	lockdep_dependency_gen_id++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1428) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1429) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1430) static inline int __cq_empty(struct circular_queue *cq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1431) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1432) 	return (cq->front == cq->rear);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1433) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1434) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1435) static inline int __cq_full(struct circular_queue *cq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1436) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1437) 	return ((cq->rear + 1) & CQ_MASK) == cq->front;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1438) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1439) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1440) static inline int __cq_enqueue(struct circular_queue *cq, struct lock_list *elem)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1441) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1442) 	if (__cq_full(cq))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1443) 		return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1444) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1445) 	cq->element[cq->rear] = elem;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1446) 	cq->rear = (cq->rear + 1) & CQ_MASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1447) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1448) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1449) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1450) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1451)  * Dequeue an element from the circular_queue, return a lock_list if
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1452)  * the queue is not empty, or NULL if otherwise.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1453)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1454) static inline struct lock_list * __cq_dequeue(struct circular_queue *cq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1455) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1456) 	struct lock_list * lock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1457) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1458) 	if (__cq_empty(cq))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1459) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1460) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1461) 	lock = cq->element[cq->front];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1462) 	cq->front = (cq->front + 1) & CQ_MASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1463) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1464) 	return lock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1465) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1466) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1467) static inline unsigned int  __cq_get_elem_count(struct circular_queue *cq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1468) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1469) 	return (cq->rear - cq->front) & CQ_MASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1470) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1471) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1472) static inline void mark_lock_accessed(struct lock_list *lock)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1473) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1474) 	lock->class->dep_gen_id = lockdep_dependency_gen_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1475) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1476) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1477) static inline void visit_lock_entry(struct lock_list *lock,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1478) 				    struct lock_list *parent)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1479) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1480) 	lock->parent = parent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1481) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1482) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1483) static inline unsigned long lock_accessed(struct lock_list *lock)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1484) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1485) 	return lock->class->dep_gen_id == lockdep_dependency_gen_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1486) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1487) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1488) static inline struct lock_list *get_lock_parent(struct lock_list *child)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1489) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1490) 	return child->parent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1491) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1492) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1493) static inline int get_lock_depth(struct lock_list *child)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1494) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1495) 	int depth = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1496) 	struct lock_list *parent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1497) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1498) 	while ((parent = get_lock_parent(child))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1499) 		child = parent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1500) 		depth++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1501) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1502) 	return depth;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1503) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1504) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1505) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1506)  * Return the forward or backward dependency list.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1507)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1508)  * @lock:   the lock_list to get its class's dependency list
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1509)  * @offset: the offset to struct lock_class to determine whether it is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1510)  *          locks_after or locks_before
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1511)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1512) static inline struct list_head *get_dep_list(struct lock_list *lock, int offset)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1513) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1514) 	void *lock_class = lock->class;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1515) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1516) 	return lock_class + offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1517) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1518) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1519)  * Return values of a bfs search:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1520)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1521)  * BFS_E* indicates an error
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1522)  * BFS_R* indicates a result (match or not)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1523)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1524)  * BFS_EINVALIDNODE: Find a invalid node in the graph.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1525)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1526)  * BFS_EQUEUEFULL: The queue is full while doing the bfs.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1527)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1528)  * BFS_RMATCH: Find the matched node in the graph, and put that node into
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1529)  *             *@target_entry.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1530)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1531)  * BFS_RNOMATCH: Haven't found the matched node and keep *@target_entry
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1532)  *               _unchanged_.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1533)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1534) enum bfs_result {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1535) 	BFS_EINVALIDNODE = -2,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1536) 	BFS_EQUEUEFULL = -1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1537) 	BFS_RMATCH = 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1538) 	BFS_RNOMATCH = 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1539) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1540) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1541) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1542)  * bfs_result < 0 means error
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1543)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1544) static inline bool bfs_error(enum bfs_result res)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1545) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1546) 	return res < 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1547) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1548) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1549) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1550)  * DEP_*_BIT in lock_list::dep
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1551)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1552)  * For dependency @prev -> @next:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1553)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1554)  *   SR: @prev is shared reader (->read != 0) and @next is recursive reader
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1555)  *       (->read == 2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1556)  *   ER: @prev is exclusive locker (->read == 0) and @next is recursive reader
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1557)  *   SN: @prev is shared reader and @next is non-recursive locker (->read != 2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1558)  *   EN: @prev is exclusive locker and @next is non-recursive locker
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1559)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1560)  * Note that we define the value of DEP_*_BITs so that:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1561)  *   bit0 is prev->read == 0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1562)  *   bit1 is next->read != 2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1563)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1564) #define DEP_SR_BIT (0 + (0 << 1)) /* 0 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1565) #define DEP_ER_BIT (1 + (0 << 1)) /* 1 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1566) #define DEP_SN_BIT (0 + (1 << 1)) /* 2 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1567) #define DEP_EN_BIT (1 + (1 << 1)) /* 3 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1568) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1569) #define DEP_SR_MASK (1U << (DEP_SR_BIT))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1570) #define DEP_ER_MASK (1U << (DEP_ER_BIT))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1571) #define DEP_SN_MASK (1U << (DEP_SN_BIT))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1572) #define DEP_EN_MASK (1U << (DEP_EN_BIT))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1573) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1574) static inline unsigned int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1575) __calc_dep_bit(struct held_lock *prev, struct held_lock *next)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1576) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1577) 	return (prev->read == 0) + ((next->read != 2) << 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1578) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1579) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1580) static inline u8 calc_dep(struct held_lock *prev, struct held_lock *next)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1581) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1582) 	return 1U << __calc_dep_bit(prev, next);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1583) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1584) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1585) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1586)  * calculate the dep_bit for backwards edges. We care about whether @prev is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1587)  * shared and whether @next is recursive.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1588)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1589) static inline unsigned int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1590) __calc_dep_bitb(struct held_lock *prev, struct held_lock *next)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1591) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1592) 	return (next->read != 2) + ((prev->read == 0) << 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1593) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1594) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1595) static inline u8 calc_depb(struct held_lock *prev, struct held_lock *next)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1596) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1597) 	return 1U << __calc_dep_bitb(prev, next);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1598) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1599) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1600) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1601)  * Initialize a lock_list entry @lock belonging to @class as the root for a BFS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1602)  * search.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1603)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1604) static inline void __bfs_init_root(struct lock_list *lock,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1605) 				   struct lock_class *class)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1606) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1607) 	lock->class = class;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1608) 	lock->parent = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1609) 	lock->only_xr = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1610) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1611) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1612) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1613)  * Initialize a lock_list entry @lock based on a lock acquisition @hlock as the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1614)  * root for a BFS search.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1615)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1616)  * ->only_xr of the initial lock node is set to @hlock->read == 2, to make sure
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1617)  * that <prev> -> @hlock and @hlock -> <whatever __bfs() found> is not -(*R)->
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1618)  * and -(S*)->.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1619)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1620) static inline void bfs_init_root(struct lock_list *lock,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1621) 				 struct held_lock *hlock)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1622) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1623) 	__bfs_init_root(lock, hlock_class(hlock));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1624) 	lock->only_xr = (hlock->read == 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1625) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1626) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1627) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1628)  * Similar to bfs_init_root() but initialize the root for backwards BFS.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1629)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1630)  * ->only_xr of the initial lock node is set to @hlock->read != 0, to make sure
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1631)  * that <next> -> @hlock and @hlock -> <whatever backwards BFS found> is not
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1632)  * -(*S)-> and -(R*)-> (reverse order of -(*R)-> and -(S*)->).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1633)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1634) static inline void bfs_init_rootb(struct lock_list *lock,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1635) 				  struct held_lock *hlock)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1636) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1637) 	__bfs_init_root(lock, hlock_class(hlock));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1638) 	lock->only_xr = (hlock->read != 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1639) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1640) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1641) static inline struct lock_list *__bfs_next(struct lock_list *lock, int offset)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1642) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1643) 	if (!lock || !lock->parent)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1644) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1645) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1646) 	return list_next_or_null_rcu(get_dep_list(lock->parent, offset),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1647) 				     &lock->entry, struct lock_list, entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1648) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1649) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1650) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1651)  * Breadth-First Search to find a strong path in the dependency graph.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1652)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1653)  * @source_entry: the source of the path we are searching for.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1654)  * @data: data used for the second parameter of @match function
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1655)  * @match: match function for the search
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1656)  * @target_entry: pointer to the target of a matched path
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1657)  * @offset: the offset to struct lock_class to determine whether it is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1658)  *          locks_after or locks_before
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1659)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1660)  * We may have multiple edges (considering different kinds of dependencies,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1661)  * e.g. ER and SN) between two nodes in the dependency graph. But
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1662)  * only the strong dependency path in the graph is relevant to deadlocks. A
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1663)  * strong dependency path is a dependency path that doesn't have two adjacent
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1664)  * dependencies as -(*R)-> -(S*)->, please see:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1665)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1666)  *         Documentation/locking/lockdep-design.rst
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1667)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1668)  * for more explanation of the definition of strong dependency paths
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1669)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1670)  * In __bfs(), we only traverse in the strong dependency path:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1671)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1672)  *     In lock_list::only_xr, we record whether the previous dependency only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1673)  *     has -(*R)-> in the search, and if it does (prev only has -(*R)->), we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1674)  *     filter out any -(S*)-> in the current dependency and after that, the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1675)  *     ->only_xr is set according to whether we only have -(*R)-> left.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1676)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1677) static enum bfs_result __bfs(struct lock_list *source_entry,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1678) 			     void *data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1679) 			     bool (*match)(struct lock_list *entry, void *data),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1680) 			     struct lock_list **target_entry,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1681) 			     int offset)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1682) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1683) 	struct circular_queue *cq = &lock_cq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1684) 	struct lock_list *lock = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1685) 	struct lock_list *entry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1686) 	struct list_head *head;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1687) 	unsigned int cq_depth;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1688) 	bool first;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1689) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1690) 	lockdep_assert_locked();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1691) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1692) 	__cq_init(cq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1693) 	__cq_enqueue(cq, source_entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1694) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1695) 	while ((lock = __bfs_next(lock, offset)) || (lock = __cq_dequeue(cq))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1696) 		if (!lock->class)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1697) 			return BFS_EINVALIDNODE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1698) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1699) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1700) 		 * Step 1: check whether we already finish on this one.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1701) 		 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1702) 		 * If we have visited all the dependencies from this @lock to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1703) 		 * others (iow, if we have visited all lock_list entries in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1704) 		 * @lock->class->locks_{after,before}) we skip, otherwise go
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1705) 		 * and visit all the dependencies in the list and mark this
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1706) 		 * list accessed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1707) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1708) 		if (lock_accessed(lock))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1709) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1710) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1711) 			mark_lock_accessed(lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1712) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1713) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1714) 		 * Step 2: check whether prev dependency and this form a strong
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1715) 		 *         dependency path.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1716) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1717) 		if (lock->parent) { /* Parent exists, check prev dependency */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1718) 			u8 dep = lock->dep;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1719) 			bool prev_only_xr = lock->parent->only_xr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1720) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1721) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1722) 			 * Mask out all -(S*)-> if we only have *R in previous
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1723) 			 * step, because -(*R)-> -(S*)-> don't make up a strong
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1724) 			 * dependency.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1725) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1726) 			if (prev_only_xr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1727) 				dep &= ~(DEP_SR_MASK | DEP_SN_MASK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1728) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1729) 			/* If nothing left, we skip */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1730) 			if (!dep)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1731) 				continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1732) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1733) 			/* If there are only -(*R)-> left, set that for the next step */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1734) 			lock->only_xr = !(dep & (DEP_SN_MASK | DEP_EN_MASK));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1735) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1736) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1737) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1738) 		 * Step 3: we haven't visited this and there is a strong
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1739) 		 *         dependency path to this, so check with @match.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1740) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1741) 		if (match(lock, data)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1742) 			*target_entry = lock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1743) 			return BFS_RMATCH;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1744) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1745) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1746) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1747) 		 * Step 4: if not match, expand the path by adding the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1748) 		 *         forward or backwards dependencis in the search
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1749) 		 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1750) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1751) 		first = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1752) 		head = get_dep_list(lock, offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1753) 		list_for_each_entry_rcu(entry, head, entry) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1754) 			visit_lock_entry(entry, lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1755) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1756) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1757) 			 * Note we only enqueue the first of the list into the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1758) 			 * queue, because we can always find a sibling
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1759) 			 * dependency from one (see __bfs_next()), as a result
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1760) 			 * the space of queue is saved.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1761) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1762) 			if (!first)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1763) 				continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1764) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1765) 			first = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1766) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1767) 			if (__cq_enqueue(cq, entry))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1768) 				return BFS_EQUEUEFULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1769) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1770) 			cq_depth = __cq_get_elem_count(cq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1771) 			if (max_bfs_queue_depth < cq_depth)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1772) 				max_bfs_queue_depth = cq_depth;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1773) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1774) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1775) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1776) 	return BFS_RNOMATCH;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1777) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1778) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1779) static inline enum bfs_result
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1780) __bfs_forwards(struct lock_list *src_entry,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1781) 	       void *data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1782) 	       bool (*match)(struct lock_list *entry, void *data),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1783) 	       struct lock_list **target_entry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1784) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1785) 	return __bfs(src_entry, data, match, target_entry,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1786) 		     offsetof(struct lock_class, locks_after));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1787) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1788) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1789) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1790) static inline enum bfs_result
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1791) __bfs_backwards(struct lock_list *src_entry,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1792) 		void *data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1793) 		bool (*match)(struct lock_list *entry, void *data),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1794) 		struct lock_list **target_entry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1795) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1796) 	return __bfs(src_entry, data, match, target_entry,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1797) 		     offsetof(struct lock_class, locks_before));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1798) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1799) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1800) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1801) static void print_lock_trace(const struct lock_trace *trace,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1802) 			     unsigned int spaces)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1803) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1804) 	stack_trace_print(trace->entries, trace->nr_entries, spaces);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1805) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1806) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1807) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1808)  * Print a dependency chain entry (this is only done when a deadlock
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1809)  * has been detected):
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1810)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1811) static noinline void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1812) print_circular_bug_entry(struct lock_list *target, int depth)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1813) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1814) 	if (debug_locks_silent)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1815) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1816) 	printk("\n-> #%u", depth);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1817) 	print_lock_name(target->class);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1818) 	printk(KERN_CONT ":\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1819) 	print_lock_trace(target->trace, 6);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1820) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1821) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1822) static void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1823) print_circular_lock_scenario(struct held_lock *src,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1824) 			     struct held_lock *tgt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1825) 			     struct lock_list *prt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1826) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1827) 	struct lock_class *source = hlock_class(src);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1828) 	struct lock_class *target = hlock_class(tgt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1829) 	struct lock_class *parent = prt->class;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1830) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1831) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1832) 	 * A direct locking problem where unsafe_class lock is taken
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1833) 	 * directly by safe_class lock, then all we need to show
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1834) 	 * is the deadlock scenario, as it is obvious that the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1835) 	 * unsafe lock is taken under the safe lock.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1836) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1837) 	 * But if there is a chain instead, where the safe lock takes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1838) 	 * an intermediate lock (middle_class) where this lock is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1839) 	 * not the same as the safe lock, then the lock chain is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1840) 	 * used to describe the problem. Otherwise we would need
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1841) 	 * to show a different CPU case for each link in the chain
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1842) 	 * from the safe_class lock to the unsafe_class lock.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1843) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1844) 	if (parent != source) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1845) 		printk("Chain exists of:\n  ");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1846) 		__print_lock_name(source);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1847) 		printk(KERN_CONT " --> ");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1848) 		__print_lock_name(parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1849) 		printk(KERN_CONT " --> ");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1850) 		__print_lock_name(target);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1851) 		printk(KERN_CONT "\n\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1852) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1853) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1854) 	printk(" Possible unsafe locking scenario:\n\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1855) 	printk("       CPU0                    CPU1\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1856) 	printk("       ----                    ----\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1857) 	printk("  lock(");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1858) 	__print_lock_name(target);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1859) 	printk(KERN_CONT ");\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1860) 	printk("                               lock(");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1861) 	__print_lock_name(parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1862) 	printk(KERN_CONT ");\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1863) 	printk("                               lock(");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1864) 	__print_lock_name(target);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1865) 	printk(KERN_CONT ");\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1866) 	printk("  lock(");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1867) 	__print_lock_name(source);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1868) 	printk(KERN_CONT ");\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1869) 	printk("\n *** DEADLOCK ***\n\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1870) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1871) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1872) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1873)  * When a circular dependency is detected, print the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1874)  * header first:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1875)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1876) static noinline void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1877) print_circular_bug_header(struct lock_list *entry, unsigned int depth,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1878) 			struct held_lock *check_src,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1879) 			struct held_lock *check_tgt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1880) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1881) 	struct task_struct *curr = current;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1882) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1883) 	if (debug_locks_silent)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1884) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1885) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1886) 	pr_warn("\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1887) 	pr_warn("======================================================\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1888) 	pr_warn("WARNING: possible circular locking dependency detected\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1889) 	print_kernel_ident();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1890) 	pr_warn("------------------------------------------------------\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1891) 	pr_warn("%s/%d is trying to acquire lock:\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1892) 		curr->comm, task_pid_nr(curr));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1893) 	print_lock(check_src);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1894) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1895) 	pr_warn("\nbut task is already holding lock:\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1896) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1897) 	print_lock(check_tgt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1898) 	pr_warn("\nwhich lock already depends on the new lock.\n\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1899) 	pr_warn("\nthe existing dependency chain (in reverse order) is:\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1900) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1901) 	print_circular_bug_entry(entry, depth);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1902) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1903) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1904) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1905)  * We are about to add A -> B into the dependency graph, and in __bfs() a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1906)  * strong dependency path A -> .. -> B is found: hlock_class equals
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1907)  * entry->class.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1908)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1909)  * If A -> .. -> B can replace A -> B in any __bfs() search (means the former
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1910)  * is _stronger_ than or equal to the latter), we consider A -> B as redundant.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1911)  * For example if A -> .. -> B is -(EN)-> (i.e. A -(E*)-> .. -(*N)-> B), and A
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1912)  * -> B is -(ER)-> or -(EN)->, then we don't need to add A -> B into the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1913)  * dependency graph, as any strong path ..-> A -> B ->.. we can get with
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1914)  * having dependency A -> B, we could already get a equivalent path ..-> A ->
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1915)  * .. -> B -> .. with A -> .. -> B. Therefore A -> B is reduntant.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1916)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1917)  * We need to make sure both the start and the end of A -> .. -> B is not
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1918)  * weaker than A -> B. For the start part, please see the comment in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1919)  * check_redundant(). For the end part, we need:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1920)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1921)  * Either
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1922)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1923)  *     a) A -> B is -(*R)-> (everything is not weaker than that)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1924)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1925)  * or
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1926)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1927)  *     b) A -> .. -> B is -(*N)-> (nothing is stronger than this)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1928)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1929)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1930) static inline bool hlock_equal(struct lock_list *entry, void *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1931) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1932) 	struct held_lock *hlock = (struct held_lock *)data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1933) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1934) 	return hlock_class(hlock) == entry->class && /* Found A -> .. -> B */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1935) 	       (hlock->read == 2 ||  /* A -> B is -(*R)-> */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1936) 		!entry->only_xr); /* A -> .. -> B is -(*N)-> */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1937) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1938) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1939) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1940)  * We are about to add B -> A into the dependency graph, and in __bfs() a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1941)  * strong dependency path A -> .. -> B is found: hlock_class equals
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1942)  * entry->class.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1943)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1944)  * We will have a deadlock case (conflict) if A -> .. -> B -> A is a strong
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1945)  * dependency cycle, that means:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1946)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1947)  * Either
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1948)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1949)  *     a) B -> A is -(E*)->
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1950)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1951)  * or
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1952)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1953)  *     b) A -> .. -> B is -(*N)-> (i.e. A -> .. -(*N)-> B)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1954)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1955)  * as then we don't have -(*R)-> -(S*)-> in the cycle.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1956)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1957) static inline bool hlock_conflict(struct lock_list *entry, void *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1958) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1959) 	struct held_lock *hlock = (struct held_lock *)data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1960) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1961) 	return hlock_class(hlock) == entry->class && /* Found A -> .. -> B */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1962) 	       (hlock->read == 0 || /* B -> A is -(E*)-> */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1963) 		!entry->only_xr); /* A -> .. -> B is -(*N)-> */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1964) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1965) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1966) static noinline void print_circular_bug(struct lock_list *this,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1967) 				struct lock_list *target,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1968) 				struct held_lock *check_src,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1969) 				struct held_lock *check_tgt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1970) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1971) 	struct task_struct *curr = current;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1972) 	struct lock_list *parent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1973) 	struct lock_list *first_parent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1974) 	int depth;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1975) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1976) 	if (!debug_locks_off_graph_unlock() || debug_locks_silent)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1977) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1978) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1979) 	this->trace = save_trace();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1980) 	if (!this->trace)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1981) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1982) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1983) 	depth = get_lock_depth(target);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1984) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1985) 	print_circular_bug_header(target, depth, check_src, check_tgt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1986) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1987) 	parent = get_lock_parent(target);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1988) 	first_parent = parent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1989) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1990) 	while (parent) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1991) 		print_circular_bug_entry(parent, --depth);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1992) 		parent = get_lock_parent(parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1993) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1994) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1995) 	printk("\nother info that might help us debug this:\n\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1996) 	print_circular_lock_scenario(check_src, check_tgt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1997) 				     first_parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1998) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1999) 	lockdep_print_held_locks(curr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2000) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2001) 	printk("\nstack backtrace:\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2002) 	dump_stack();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2003) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2004) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2005) static noinline void print_bfs_bug(int ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2006) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2007) 	if (!debug_locks_off_graph_unlock())
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2008) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2009) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2010) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2011) 	 * Breadth-first-search failed, graph got corrupted?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2012) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2013) 	WARN(1, "lockdep bfs error:%d\n", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2014) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2015) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2016) static bool noop_count(struct lock_list *entry, void *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2017) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2018) 	(*(unsigned long *)data)++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2019) 	return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2020) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2021) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2022) static unsigned long __lockdep_count_forward_deps(struct lock_list *this)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2023) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2024) 	unsigned long  count = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2025) 	struct lock_list *target_entry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2026) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2027) 	__bfs_forwards(this, (void *)&count, noop_count, &target_entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2028) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2029) 	return count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2030) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2031) unsigned long lockdep_count_forward_deps(struct lock_class *class)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2032) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2033) 	unsigned long ret, flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2034) 	struct lock_list this;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2035) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2036) 	__bfs_init_root(&this, class);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2037) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2038) 	raw_local_irq_save(flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2039) 	lockdep_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2040) 	ret = __lockdep_count_forward_deps(&this);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2041) 	lockdep_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2042) 	raw_local_irq_restore(flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2043) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2044) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2045) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2046) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2047) static unsigned long __lockdep_count_backward_deps(struct lock_list *this)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2048) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2049) 	unsigned long  count = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2050) 	struct lock_list *target_entry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2051) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2052) 	__bfs_backwards(this, (void *)&count, noop_count, &target_entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2053) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2054) 	return count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2055) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2056) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2057) unsigned long lockdep_count_backward_deps(struct lock_class *class)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2058) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2059) 	unsigned long ret, flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2060) 	struct lock_list this;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2061) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2062) 	__bfs_init_root(&this, class);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2063) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2064) 	raw_local_irq_save(flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2065) 	lockdep_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2066) 	ret = __lockdep_count_backward_deps(&this);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2067) 	lockdep_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2068) 	raw_local_irq_restore(flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2069) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2070) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2071) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2072) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2073) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2074)  * Check that the dependency graph starting at <src> can lead to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2075)  * <target> or not.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2076)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2077) static noinline enum bfs_result
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2078) check_path(struct held_lock *target, struct lock_list *src_entry,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2079) 	   bool (*match)(struct lock_list *entry, void *data),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2080) 	   struct lock_list **target_entry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2081) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2082) 	enum bfs_result ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2083) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2084) 	ret = __bfs_forwards(src_entry, target, match, target_entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2085) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2086) 	if (unlikely(bfs_error(ret)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2087) 		print_bfs_bug(ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2088) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2089) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2090) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2091) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2092) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2093)  * Prove that the dependency graph starting at <src> can not
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2094)  * lead to <target>. If it can, there is a circle when adding
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2095)  * <target> -> <src> dependency.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2096)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2097)  * Print an error and return BFS_RMATCH if it does.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2098)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2099) static noinline enum bfs_result
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2100) check_noncircular(struct held_lock *src, struct held_lock *target,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2101) 		  struct lock_trace **const trace)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2102) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2103) 	enum bfs_result ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2104) 	struct lock_list *target_entry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2105) 	struct lock_list src_entry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2106) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2107) 	bfs_init_root(&src_entry, src);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2108) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2109) 	debug_atomic_inc(nr_cyclic_checks);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2110) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2111) 	ret = check_path(target, &src_entry, hlock_conflict, &target_entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2112) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2113) 	if (unlikely(ret == BFS_RMATCH)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2114) 		if (!*trace) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2115) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2116) 			 * If save_trace fails here, the printing might
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2117) 			 * trigger a WARN but because of the !nr_entries it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2118) 			 * should not do bad things.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2119) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2120) 			*trace = save_trace();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2121) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2122) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2123) 		print_circular_bug(&src_entry, target_entry, src, target);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2124) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2125) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2126) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2127) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2128) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2129) #ifdef CONFIG_LOCKDEP_SMALL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2130) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2131)  * Check that the dependency graph starting at <src> can lead to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2132)  * <target> or not. If it can, <src> -> <target> dependency is already
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2133)  * in the graph.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2134)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2135)  * Return BFS_RMATCH if it does, or BFS_RMATCH if it does not, return BFS_E* if
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2136)  * any error appears in the bfs search.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2137)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2138) static noinline enum bfs_result
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2139) check_redundant(struct held_lock *src, struct held_lock *target)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2140) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2141) 	enum bfs_result ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2142) 	struct lock_list *target_entry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2143) 	struct lock_list src_entry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2144) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2145) 	bfs_init_root(&src_entry, src);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2146) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2147) 	 * Special setup for check_redundant().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2148) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2149) 	 * To report redundant, we need to find a strong dependency path that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2150) 	 * is equal to or stronger than <src> -> <target>. So if <src> is E,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2151) 	 * we need to let __bfs() only search for a path starting at a -(E*)->,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2152) 	 * we achieve this by setting the initial node's ->only_xr to true in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2153) 	 * that case. And if <prev> is S, we set initial ->only_xr to false
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2154) 	 * because both -(S*)-> (equal) and -(E*)-> (stronger) are redundant.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2155) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2156) 	src_entry.only_xr = src->read == 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2157) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2158) 	debug_atomic_inc(nr_redundant_checks);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2159) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2160) 	ret = check_path(target, &src_entry, hlock_equal, &target_entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2161) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2162) 	if (ret == BFS_RMATCH)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2163) 		debug_atomic_inc(nr_redundant);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2164) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2165) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2166) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2167) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2168) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2169) #ifdef CONFIG_TRACE_IRQFLAGS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2170) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2171) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2172)  * Forwards and backwards subgraph searching, for the purposes of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2173)  * proving that two subgraphs can be connected by a new dependency
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2174)  * without creating any illegal irq-safe -> irq-unsafe lock dependency.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2175)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2176)  * A irq safe->unsafe deadlock happens with the following conditions:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2177)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2178)  * 1) We have a strong dependency path A -> ... -> B
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2179)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2180)  * 2) and we have ENABLED_IRQ usage of B and USED_IN_IRQ usage of A, therefore
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2181)  *    irq can create a new dependency B -> A (consider the case that a holder
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2182)  *    of B gets interrupted by an irq whose handler will try to acquire A).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2183)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2184)  * 3) the dependency circle A -> ... -> B -> A we get from 1) and 2) is a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2185)  *    strong circle:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2186)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2187)  *      For the usage bits of B:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2188)  *        a) if A -> B is -(*N)->, then B -> A could be any type, so any
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2189)  *           ENABLED_IRQ usage suffices.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2190)  *        b) if A -> B is -(*R)->, then B -> A must be -(E*)->, so only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2191)  *           ENABLED_IRQ_*_READ usage suffices.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2192)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2193)  *      For the usage bits of A:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2194)  *        c) if A -> B is -(E*)->, then B -> A could be any type, so any
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2195)  *           USED_IN_IRQ usage suffices.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2196)  *        d) if A -> B is -(S*)->, then B -> A must be -(*N)->, so only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2197)  *           USED_IN_IRQ_*_READ usage suffices.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2198)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2199) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2200) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2201)  * There is a strong dependency path in the dependency graph: A -> B, and now
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2202)  * we need to decide which usage bit of A should be accumulated to detect
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2203)  * safe->unsafe bugs.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2204)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2205)  * Note that usage_accumulate() is used in backwards search, so ->only_xr
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2206)  * stands for whether A -> B only has -(S*)-> (in this case ->only_xr is true).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2207)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2208)  * As above, if only_xr is false, which means A -> B has -(E*)-> dependency
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2209)  * path, any usage of A should be considered. Otherwise, we should only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2210)  * consider _READ usage.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2211)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2212) static inline bool usage_accumulate(struct lock_list *entry, void *mask)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2213) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2214) 	if (!entry->only_xr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2215) 		*(unsigned long *)mask |= entry->class->usage_mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2216) 	else /* Mask out _READ usage bits */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2217) 		*(unsigned long *)mask |= (entry->class->usage_mask & LOCKF_IRQ);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2218) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2219) 	return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2220) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2221) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2222) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2223)  * There is a strong dependency path in the dependency graph: A -> B, and now
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2224)  * we need to decide which usage bit of B conflicts with the usage bits of A,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2225)  * i.e. which usage bit of B may introduce safe->unsafe deadlocks.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2226)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2227)  * As above, if only_xr is false, which means A -> B has -(*N)-> dependency
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2228)  * path, any usage of B should be considered. Otherwise, we should only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2229)  * consider _READ usage.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2230)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2231) static inline bool usage_match(struct lock_list *entry, void *mask)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2232) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2233) 	if (!entry->only_xr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2234) 		return !!(entry->class->usage_mask & *(unsigned long *)mask);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2235) 	else /* Mask out _READ usage bits */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2236) 		return !!((entry->class->usage_mask & LOCKF_IRQ) & *(unsigned long *)mask);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2237) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2238) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2239) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2240)  * Find a node in the forwards-direction dependency sub-graph starting
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2241)  * at @root->class that matches @bit.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2242)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2243)  * Return BFS_MATCH if such a node exists in the subgraph, and put that node
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2244)  * into *@target_entry.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2245)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2246) static enum bfs_result
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2247) find_usage_forwards(struct lock_list *root, unsigned long usage_mask,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2248) 			struct lock_list **target_entry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2249) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2250) 	enum bfs_result result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2251) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2252) 	debug_atomic_inc(nr_find_usage_forwards_checks);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2253) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2254) 	result = __bfs_forwards(root, &usage_mask, usage_match, target_entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2255) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2256) 	return result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2257) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2258) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2259) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2260)  * Find a node in the backwards-direction dependency sub-graph starting
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2261)  * at @root->class that matches @bit.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2262)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2263) static enum bfs_result
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2264) find_usage_backwards(struct lock_list *root, unsigned long usage_mask,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2265) 			struct lock_list **target_entry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2266) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2267) 	enum bfs_result result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2268) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2269) 	debug_atomic_inc(nr_find_usage_backwards_checks);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2270) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2271) 	result = __bfs_backwards(root, &usage_mask, usage_match, target_entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2272) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2273) 	return result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2274) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2275) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2276) static void print_lock_class_header(struct lock_class *class, int depth)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2277) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2278) 	int bit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2279) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2280) 	printk("%*s->", depth, "");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2281) 	print_lock_name(class);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2282) #ifdef CONFIG_DEBUG_LOCKDEP
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2283) 	printk(KERN_CONT " ops: %lu", debug_class_ops_read(class));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2284) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2285) 	printk(KERN_CONT " {\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2286) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2287) 	for (bit = 0; bit < LOCK_TRACE_STATES; bit++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2288) 		if (class->usage_mask & (1 << bit)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2289) 			int len = depth;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2290) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2291) 			len += printk("%*s   %s", depth, "", usage_str[bit]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2292) 			len += printk(KERN_CONT " at:\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2293) 			print_lock_trace(class->usage_traces[bit], len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2294) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2295) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2296) 	printk("%*s }\n", depth, "");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2297) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2298) 	printk("%*s ... key      at: [<%px>] %pS\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2299) 		depth, "", class->key, class->key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2300) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2301) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2302) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2303)  * Dependency path printing:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2304)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2305)  * After BFS we get a lock dependency path (linked via ->parent of lock_list),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2306)  * printing out each lock in the dependency path will help on understanding how
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2307)  * the deadlock could happen. Here are some details about dependency path
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2308)  * printing:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2309)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2310)  * 1)	A lock_list can be either forwards or backwards for a lock dependency,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2311)  * 	for a lock dependency A -> B, there are two lock_lists:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2312)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2313)  * 	a)	lock_list in the ->locks_after list of A, whose ->class is B and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2314)  * 		->links_to is A. In this case, we can say the lock_list is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2315)  * 		"A -> B" (forwards case).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2316)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2317)  * 	b)	lock_list in the ->locks_before list of B, whose ->class is A
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2318)  * 		and ->links_to is B. In this case, we can say the lock_list is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2319)  * 		"B <- A" (bacwards case).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2320)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2321)  * 	The ->trace of both a) and b) point to the call trace where B was
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2322)  * 	acquired with A held.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2323)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2324)  * 2)	A "helper" lock_list is introduced during BFS, this lock_list doesn't
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2325)  * 	represent a certain lock dependency, it only provides an initial entry
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2326)  * 	for BFS. For example, BFS may introduce a "helper" lock_list whose
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2327)  * 	->class is A, as a result BFS will search all dependencies starting with
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2328)  * 	A, e.g. A -> B or A -> C.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2329)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2330)  * 	The notation of a forwards helper lock_list is like "-> A", which means
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2331)  * 	we should search the forwards dependencies starting with "A", e.g A -> B
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2332)  * 	or A -> C.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2333)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2334)  * 	The notation of a bacwards helper lock_list is like "<- B", which means
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2335)  * 	we should search the backwards dependencies ending with "B", e.g.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2336)  * 	B <- A or B <- C.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2337)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2338) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2339) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2340)  * printk the shortest lock dependencies from @root to @leaf in reverse order.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2341)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2342)  * We have a lock dependency path as follow:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2343)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2344)  *    @root                                                                 @leaf
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2345)  *      |                                                                     |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2346)  *      V                                                                     V
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2347)  *	          ->parent                                   ->parent
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2348)  * | lock_list | <--------- | lock_list | ... | lock_list  | <--------- | lock_list |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2349)  * |    -> L1  |            | L1 -> L2  | ... |Ln-2 -> Ln-1|            | Ln-1 -> Ln|
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2350)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2351)  * , so it's natural that we start from @leaf and print every ->class and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2352)  * ->trace until we reach the @root.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2353)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2354) static void __used
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2355) print_shortest_lock_dependencies(struct lock_list *leaf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2356) 				 struct lock_list *root)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2357) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2358) 	struct lock_list *entry = leaf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2359) 	int depth;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2360) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2361) 	/*compute depth from generated tree by BFS*/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2362) 	depth = get_lock_depth(leaf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2363) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2364) 	do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2365) 		print_lock_class_header(entry->class, depth);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2366) 		printk("%*s ... acquired at:\n", depth, "");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2367) 		print_lock_trace(entry->trace, 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2368) 		printk("\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2369) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2370) 		if (depth == 0 && (entry != root)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2371) 			printk("lockdep:%s bad path found in chain graph\n", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2372) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2373) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2374) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2375) 		entry = get_lock_parent(entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2376) 		depth--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2377) 	} while (entry && (depth >= 0));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2378) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2379) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2380) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2381)  * printk the shortest lock dependencies from @leaf to @root.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2382)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2383)  * We have a lock dependency path (from a backwards search) as follow:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2384)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2385)  *    @leaf                                                                 @root
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2386)  *      |                                                                     |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2387)  *      V                                                                     V
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2388)  *	          ->parent                                   ->parent
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2389)  * | lock_list | ---------> | lock_list | ... | lock_list  | ---------> | lock_list |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2390)  * | L2 <- L1  |            | L3 <- L2  | ... | Ln <- Ln-1 |            |    <- Ln  |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2391)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2392)  * , so when we iterate from @leaf to @root, we actually print the lock
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2393)  * dependency path L1 -> L2 -> .. -> Ln in the non-reverse order.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2394)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2395)  * Another thing to notice here is that ->class of L2 <- L1 is L1, while the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2396)  * ->trace of L2 <- L1 is the call trace of L2, in fact we don't have the call
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2397)  * trace of L1 in the dependency path, which is alright, because most of the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2398)  * time we can figure out where L1 is held from the call trace of L2.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2399)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2400) static void __used
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2401) print_shortest_lock_dependencies_backwards(struct lock_list *leaf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2402) 					   struct lock_list *root)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2403) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2404) 	struct lock_list *entry = leaf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2405) 	const struct lock_trace *trace = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2406) 	int depth;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2407) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2408) 	/*compute depth from generated tree by BFS*/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2409) 	depth = get_lock_depth(leaf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2410) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2411) 	do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2412) 		print_lock_class_header(entry->class, depth);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2413) 		if (trace) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2414) 			printk("%*s ... acquired at:\n", depth, "");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2415) 			print_lock_trace(trace, 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2416) 			printk("\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2417) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2418) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2419) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2420) 		 * Record the pointer to the trace for the next lock_list
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2421) 		 * entry, see the comments for the function.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2422) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2423) 		trace = entry->trace;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2424) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2425) 		if (depth == 0 && (entry != root)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2426) 			printk("lockdep:%s bad path found in chain graph\n", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2427) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2428) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2429) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2430) 		entry = get_lock_parent(entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2431) 		depth--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2432) 	} while (entry && (depth >= 0));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2433) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2434) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2435) static void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2436) print_irq_lock_scenario(struct lock_list *safe_entry,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2437) 			struct lock_list *unsafe_entry,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2438) 			struct lock_class *prev_class,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2439) 			struct lock_class *next_class)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2440) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2441) 	struct lock_class *safe_class = safe_entry->class;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2442) 	struct lock_class *unsafe_class = unsafe_entry->class;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2443) 	struct lock_class *middle_class = prev_class;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2444) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2445) 	if (middle_class == safe_class)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2446) 		middle_class = next_class;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2447) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2448) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2449) 	 * A direct locking problem where unsafe_class lock is taken
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2450) 	 * directly by safe_class lock, then all we need to show
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2451) 	 * is the deadlock scenario, as it is obvious that the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2452) 	 * unsafe lock is taken under the safe lock.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2453) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2454) 	 * But if there is a chain instead, where the safe lock takes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2455) 	 * an intermediate lock (middle_class) where this lock is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2456) 	 * not the same as the safe lock, then the lock chain is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2457) 	 * used to describe the problem. Otherwise we would need
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2458) 	 * to show a different CPU case for each link in the chain
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2459) 	 * from the safe_class lock to the unsafe_class lock.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2460) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2461) 	if (middle_class != unsafe_class) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2462) 		printk("Chain exists of:\n  ");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2463) 		__print_lock_name(safe_class);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2464) 		printk(KERN_CONT " --> ");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2465) 		__print_lock_name(middle_class);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2466) 		printk(KERN_CONT " --> ");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2467) 		__print_lock_name(unsafe_class);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2468) 		printk(KERN_CONT "\n\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2469) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2470) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2471) 	printk(" Possible interrupt unsafe locking scenario:\n\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2472) 	printk("       CPU0                    CPU1\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2473) 	printk("       ----                    ----\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2474) 	printk("  lock(");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2475) 	__print_lock_name(unsafe_class);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2476) 	printk(KERN_CONT ");\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2477) 	printk("                               local_irq_disable();\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2478) 	printk("                               lock(");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2479) 	__print_lock_name(safe_class);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2480) 	printk(KERN_CONT ");\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2481) 	printk("                               lock(");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2482) 	__print_lock_name(middle_class);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2483) 	printk(KERN_CONT ");\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2484) 	printk("  <Interrupt>\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2485) 	printk("    lock(");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2486) 	__print_lock_name(safe_class);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2487) 	printk(KERN_CONT ");\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2488) 	printk("\n *** DEADLOCK ***\n\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2489) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2490) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2491) static void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2492) print_bad_irq_dependency(struct task_struct *curr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2493) 			 struct lock_list *prev_root,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2494) 			 struct lock_list *next_root,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2495) 			 struct lock_list *backwards_entry,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2496) 			 struct lock_list *forwards_entry,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2497) 			 struct held_lock *prev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2498) 			 struct held_lock *next,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2499) 			 enum lock_usage_bit bit1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2500) 			 enum lock_usage_bit bit2,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2501) 			 const char *irqclass)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2502) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2503) 	if (!debug_locks_off_graph_unlock() || debug_locks_silent)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2504) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2505) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2506) 	pr_warn("\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2507) 	pr_warn("=====================================================\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2508) 	pr_warn("WARNING: %s-safe -> %s-unsafe lock order detected\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2509) 		irqclass, irqclass);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2510) 	print_kernel_ident();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2511) 	pr_warn("-----------------------------------------------------\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2512) 	pr_warn("%s/%d [HC%u[%lu]:SC%u[%lu]:HE%u:SE%u] is trying to acquire:\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2513) 		curr->comm, task_pid_nr(curr),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2514) 		lockdep_hardirq_context(), hardirq_count() >> HARDIRQ_SHIFT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2515) 		curr->softirq_context, softirq_count() >> SOFTIRQ_SHIFT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2516) 		lockdep_hardirqs_enabled(),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2517) 		curr->softirqs_enabled);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2518) 	print_lock(next);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2519) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2520) 	pr_warn("\nand this task is already holding:\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2521) 	print_lock(prev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2522) 	pr_warn("which would create a new lock dependency:\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2523) 	print_lock_name(hlock_class(prev));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2524) 	pr_cont(" ->");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2525) 	print_lock_name(hlock_class(next));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2526) 	pr_cont("\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2527) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2528) 	pr_warn("\nbut this new dependency connects a %s-irq-safe lock:\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2529) 		irqclass);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2530) 	print_lock_name(backwards_entry->class);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2531) 	pr_warn("\n... which became %s-irq-safe at:\n", irqclass);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2532) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2533) 	print_lock_trace(backwards_entry->class->usage_traces[bit1], 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2534) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2535) 	pr_warn("\nto a %s-irq-unsafe lock:\n", irqclass);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2536) 	print_lock_name(forwards_entry->class);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2537) 	pr_warn("\n... which became %s-irq-unsafe at:\n", irqclass);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2538) 	pr_warn("...");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2539) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2540) 	print_lock_trace(forwards_entry->class->usage_traces[bit2], 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2541) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2542) 	pr_warn("\nother info that might help us debug this:\n\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2543) 	print_irq_lock_scenario(backwards_entry, forwards_entry,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2544) 				hlock_class(prev), hlock_class(next));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2545) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2546) 	lockdep_print_held_locks(curr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2547) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2548) 	pr_warn("\nthe dependencies between %s-irq-safe lock and the holding lock:\n", irqclass);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2549) 	prev_root->trace = save_trace();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2550) 	if (!prev_root->trace)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2551) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2552) 	print_shortest_lock_dependencies_backwards(backwards_entry, prev_root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2553) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2554) 	pr_warn("\nthe dependencies between the lock to be acquired");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2555) 	pr_warn(" and %s-irq-unsafe lock:\n", irqclass);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2556) 	next_root->trace = save_trace();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2557) 	if (!next_root->trace)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2558) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2559) 	print_shortest_lock_dependencies(forwards_entry, next_root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2560) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2561) 	pr_warn("\nstack backtrace:\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2562) 	dump_stack();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2563) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2564) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2565) static const char *state_names[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2566) #define LOCKDEP_STATE(__STATE) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2567) 	__stringify(__STATE),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2568) #include "lockdep_states.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2569) #undef LOCKDEP_STATE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2570) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2571) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2572) static const char *state_rnames[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2573) #define LOCKDEP_STATE(__STATE) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2574) 	__stringify(__STATE)"-READ",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2575) #include "lockdep_states.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2576) #undef LOCKDEP_STATE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2577) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2578) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2579) static inline const char *state_name(enum lock_usage_bit bit)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2580) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2581) 	if (bit & LOCK_USAGE_READ_MASK)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2582) 		return state_rnames[bit >> LOCK_USAGE_DIR_MASK];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2583) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2584) 		return state_names[bit >> LOCK_USAGE_DIR_MASK];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2585) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2586) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2587) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2588)  * The bit number is encoded like:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2589)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2590)  *  bit0: 0 exclusive, 1 read lock
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2591)  *  bit1: 0 used in irq, 1 irq enabled
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2592)  *  bit2-n: state
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2593)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2594) static int exclusive_bit(int new_bit)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2595) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2596) 	int state = new_bit & LOCK_USAGE_STATE_MASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2597) 	int dir = new_bit & LOCK_USAGE_DIR_MASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2598) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2599) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2600) 	 * keep state, bit flip the direction and strip read.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2601) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2602) 	return state | (dir ^ LOCK_USAGE_DIR_MASK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2603) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2604) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2605) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2606)  * Observe that when given a bitmask where each bitnr is encoded as above, a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2607)  * right shift of the mask transforms the individual bitnrs as -1 and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2608)  * conversely, a left shift transforms into +1 for the individual bitnrs.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2609)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2610)  * So for all bits whose number have LOCK_ENABLED_* set (bitnr1 == 1), we can
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2611)  * create the mask with those bit numbers using LOCK_USED_IN_* (bitnr1 == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2612)  * instead by subtracting the bit number by 2, or shifting the mask right by 2.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2613)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2614)  * Similarly, bitnr1 == 0 becomes bitnr1 == 1 by adding 2, or shifting left 2.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2615)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2616)  * So split the mask (note that LOCKF_ENABLED_IRQ_ALL|LOCKF_USED_IN_IRQ_ALL is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2617)  * all bits set) and recompose with bitnr1 flipped.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2618)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2619) static unsigned long invert_dir_mask(unsigned long mask)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2620) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2621) 	unsigned long excl = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2622) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2623) 	/* Invert dir */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2624) 	excl |= (mask & LOCKF_ENABLED_IRQ_ALL) >> LOCK_USAGE_DIR_MASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2625) 	excl |= (mask & LOCKF_USED_IN_IRQ_ALL) << LOCK_USAGE_DIR_MASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2626) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2627) 	return excl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2628) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2629) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2630) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2631)  * Note that a LOCK_ENABLED_IRQ_*_READ usage and a LOCK_USED_IN_IRQ_*_READ
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2632)  * usage may cause deadlock too, for example:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2633)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2634)  * P1				P2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2635)  * <irq disabled>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2636)  * write_lock(l1);		<irq enabled>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2637)  *				read_lock(l2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2638)  * write_lock(l2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2639)  * 				<in irq>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2640)  * 				read_lock(l1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2641)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2642)  * , in above case, l1 will be marked as LOCK_USED_IN_IRQ_HARDIRQ_READ and l2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2643)  * will marked as LOCK_ENABLE_IRQ_HARDIRQ_READ, and this is a possible
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2644)  * deadlock.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2645)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2646)  * In fact, all of the following cases may cause deadlocks:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2647)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2648)  * 	 LOCK_USED_IN_IRQ_* -> LOCK_ENABLED_IRQ_*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2649)  * 	 LOCK_USED_IN_IRQ_*_READ -> LOCK_ENABLED_IRQ_*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2650)  * 	 LOCK_USED_IN_IRQ_* -> LOCK_ENABLED_IRQ_*_READ
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2651)  * 	 LOCK_USED_IN_IRQ_*_READ -> LOCK_ENABLED_IRQ_*_READ
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2652)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2653)  * As a result, to calculate the "exclusive mask", first we invert the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2654)  * direction (USED_IN/ENABLED) of the original mask, and 1) for all bits with
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2655)  * bitnr0 set (LOCK_*_READ), add those with bitnr0 cleared (LOCK_*). 2) for all
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2656)  * bits with bitnr0 cleared (LOCK_*_READ), add those with bitnr0 set (LOCK_*).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2657)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2658) static unsigned long exclusive_mask(unsigned long mask)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2659) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2660) 	unsigned long excl = invert_dir_mask(mask);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2661) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2662) 	excl |= (excl & LOCKF_IRQ_READ) >> LOCK_USAGE_READ_MASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2663) 	excl |= (excl & LOCKF_IRQ) << LOCK_USAGE_READ_MASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2664) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2665) 	return excl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2666) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2667) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2668) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2669)  * Retrieve the _possible_ original mask to which @mask is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2670)  * exclusive. Ie: this is the opposite of exclusive_mask().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2671)  * Note that 2 possible original bits can match an exclusive
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2672)  * bit: one has LOCK_USAGE_READ_MASK set, the other has it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2673)  * cleared. So both are returned for each exclusive bit.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2674)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2675) static unsigned long original_mask(unsigned long mask)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2676) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2677) 	unsigned long excl = invert_dir_mask(mask);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2678) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2679) 	/* Include read in existing usages */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2680) 	excl |= (excl & LOCKF_IRQ_READ) >> LOCK_USAGE_READ_MASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2681) 	excl |= (excl & LOCKF_IRQ) << LOCK_USAGE_READ_MASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2682) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2683) 	return excl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2684) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2685) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2686) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2687)  * Find the first pair of bit match between an original
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2688)  * usage mask and an exclusive usage mask.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2689)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2690) static int find_exclusive_match(unsigned long mask,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2691) 				unsigned long excl_mask,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2692) 				enum lock_usage_bit *bitp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2693) 				enum lock_usage_bit *excl_bitp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2694) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2695) 	int bit, excl, excl_read;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2696) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2697) 	for_each_set_bit(bit, &mask, LOCK_USED) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2698) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2699) 		 * exclusive_bit() strips the read bit, however,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2700) 		 * LOCK_ENABLED_IRQ_*_READ may cause deadlocks too, so we need
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2701) 		 * to search excl | LOCK_USAGE_READ_MASK as well.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2702) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2703) 		excl = exclusive_bit(bit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2704) 		excl_read = excl | LOCK_USAGE_READ_MASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2705) 		if (excl_mask & lock_flag(excl)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2706) 			*bitp = bit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2707) 			*excl_bitp = excl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2708) 			return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2709) 		} else if (excl_mask & lock_flag(excl_read)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2710) 			*bitp = bit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2711) 			*excl_bitp = excl_read;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2712) 			return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2713) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2714) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2715) 	return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2716) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2717) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2718) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2719)  * Prove that the new dependency does not connect a hardirq-safe(-read)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2720)  * lock with a hardirq-unsafe lock - to achieve this we search
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2721)  * the backwards-subgraph starting at <prev>, and the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2722)  * forwards-subgraph starting at <next>:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2723)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2724) static int check_irq_usage(struct task_struct *curr, struct held_lock *prev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2725) 			   struct held_lock *next)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2726) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2727) 	unsigned long usage_mask = 0, forward_mask, backward_mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2728) 	enum lock_usage_bit forward_bit = 0, backward_bit = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2729) 	struct lock_list *target_entry1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2730) 	struct lock_list *target_entry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2731) 	struct lock_list this, that;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2732) 	enum bfs_result ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2733) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2734) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2735) 	 * Step 1: gather all hard/soft IRQs usages backward in an
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2736) 	 * accumulated usage mask.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2737) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2738) 	bfs_init_rootb(&this, prev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2739) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2740) 	ret = __bfs_backwards(&this, &usage_mask, usage_accumulate, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2741) 	if (bfs_error(ret)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2742) 		print_bfs_bug(ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2743) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2744) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2745) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2746) 	usage_mask &= LOCKF_USED_IN_IRQ_ALL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2747) 	if (!usage_mask)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2748) 		return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2749) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2750) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2751) 	 * Step 2: find exclusive uses forward that match the previous
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2752) 	 * backward accumulated mask.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2753) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2754) 	forward_mask = exclusive_mask(usage_mask);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2755) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2756) 	bfs_init_root(&that, next);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2757) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2758) 	ret = find_usage_forwards(&that, forward_mask, &target_entry1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2759) 	if (bfs_error(ret)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2760) 		print_bfs_bug(ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2761) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2762) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2763) 	if (ret == BFS_RNOMATCH)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2764) 		return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2765) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2766) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2767) 	 * Step 3: we found a bad match! Now retrieve a lock from the backward
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2768) 	 * list whose usage mask matches the exclusive usage mask from the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2769) 	 * lock found on the forward list.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2770) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2771) 	 * Note, we should only keep the LOCKF_ENABLED_IRQ_ALL bits, considering
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2772) 	 * the follow case:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2773) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2774) 	 * When trying to add A -> B to the graph, we find that there is a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2775) 	 * hardirq-safe L, that L -> ... -> A, and another hardirq-unsafe M,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2776) 	 * that B -> ... -> M. However M is **softirq-safe**, if we use exact
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2777) 	 * invert bits of M's usage_mask, we will find another lock N that is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2778) 	 * **softirq-unsafe** and N -> ... -> A, however N -> .. -> M will not
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2779) 	 * cause a inversion deadlock.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2780) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2781) 	backward_mask = original_mask(target_entry1->class->usage_mask & LOCKF_ENABLED_IRQ_ALL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2782) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2783) 	ret = find_usage_backwards(&this, backward_mask, &target_entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2784) 	if (bfs_error(ret)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2785) 		print_bfs_bug(ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2786) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2787) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2788) 	if (DEBUG_LOCKS_WARN_ON(ret == BFS_RNOMATCH))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2789) 		return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2790) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2791) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2792) 	 * Step 4: narrow down to a pair of incompatible usage bits
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2793) 	 * and report it.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2794) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2795) 	ret = find_exclusive_match(target_entry->class->usage_mask,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2796) 				   target_entry1->class->usage_mask,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2797) 				   &backward_bit, &forward_bit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2798) 	if (DEBUG_LOCKS_WARN_ON(ret == -1))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2799) 		return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2800) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2801) 	print_bad_irq_dependency(curr, &this, &that,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2802) 				 target_entry, target_entry1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2803) 				 prev, next,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2804) 				 backward_bit, forward_bit,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2805) 				 state_name(backward_bit));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2806) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2807) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2808) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2809) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2810) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2811) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2812) static inline int check_irq_usage(struct task_struct *curr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2813) 				  struct held_lock *prev, struct held_lock *next)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2814) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2815) 	return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2816) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2817) #endif /* CONFIG_TRACE_IRQFLAGS */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2818) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2819) static void inc_chains(int irq_context)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2820) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2821) 	if (irq_context & LOCK_CHAIN_HARDIRQ_CONTEXT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2822) 		nr_hardirq_chains++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2823) 	else if (irq_context & LOCK_CHAIN_SOFTIRQ_CONTEXT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2824) 		nr_softirq_chains++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2825) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2826) 		nr_process_chains++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2827) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2828) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2829) static void dec_chains(int irq_context)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2830) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2831) 	if (irq_context & LOCK_CHAIN_HARDIRQ_CONTEXT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2832) 		nr_hardirq_chains--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2833) 	else if (irq_context & LOCK_CHAIN_SOFTIRQ_CONTEXT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2834) 		nr_softirq_chains--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2835) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2836) 		nr_process_chains--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2837) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2838) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2839) static void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2840) print_deadlock_scenario(struct held_lock *nxt, struct held_lock *prv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2841) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2842) 	struct lock_class *next = hlock_class(nxt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2843) 	struct lock_class *prev = hlock_class(prv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2844) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2845) 	printk(" Possible unsafe locking scenario:\n\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2846) 	printk("       CPU0\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2847) 	printk("       ----\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2848) 	printk("  lock(");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2849) 	__print_lock_name(prev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2850) 	printk(KERN_CONT ");\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2851) 	printk("  lock(");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2852) 	__print_lock_name(next);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2853) 	printk(KERN_CONT ");\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2854) 	printk("\n *** DEADLOCK ***\n\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2855) 	printk(" May be due to missing lock nesting notation\n\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2856) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2857) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2858) static void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2859) print_deadlock_bug(struct task_struct *curr, struct held_lock *prev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2860) 		   struct held_lock *next)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2861) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2862) 	if (!debug_locks_off_graph_unlock() || debug_locks_silent)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2863) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2864) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2865) 	pr_warn("\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2866) 	pr_warn("============================================\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2867) 	pr_warn("WARNING: possible recursive locking detected\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2868) 	print_kernel_ident();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2869) 	pr_warn("--------------------------------------------\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2870) 	pr_warn("%s/%d is trying to acquire lock:\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2871) 		curr->comm, task_pid_nr(curr));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2872) 	print_lock(next);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2873) 	pr_warn("\nbut task is already holding lock:\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2874) 	print_lock(prev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2875) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2876) 	pr_warn("\nother info that might help us debug this:\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2877) 	print_deadlock_scenario(next, prev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2878) 	lockdep_print_held_locks(curr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2879) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2880) 	pr_warn("\nstack backtrace:\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2881) 	dump_stack();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2882) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2883) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2884) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2885)  * Check whether we are holding such a class already.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2886)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2887)  * (Note that this has to be done separately, because the graph cannot
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2888)  * detect such classes of deadlocks.)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2889)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2890)  * Returns: 0 on deadlock detected, 1 on OK, 2 if another lock with the same
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2891)  * lock class is held but nest_lock is also held, i.e. we rely on the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2892)  * nest_lock to avoid the deadlock.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2893)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2894) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2895) check_deadlock(struct task_struct *curr, struct held_lock *next)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2896) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2897) 	struct held_lock *prev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2898) 	struct held_lock *nest = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2899) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2900) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2901) 	for (i = 0; i < curr->lockdep_depth; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2902) 		prev = curr->held_locks + i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2903) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2904) 		if (prev->instance == next->nest_lock)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2905) 			nest = prev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2906) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2907) 		if (hlock_class(prev) != hlock_class(next))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2908) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2909) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2910) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2911) 		 * Allow read-after-read recursion of the same
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2912) 		 * lock class (i.e. read_lock(lock)+read_lock(lock)):
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2913) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2914) 		if ((next->read == 2) && prev->read)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2915) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2916) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2917) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2918) 		 * We're holding the nest_lock, which serializes this lock's
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2919) 		 * nesting behaviour.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2920) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2921) 		if (nest)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2922) 			return 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2923) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2924) 		print_deadlock_bug(curr, prev, next);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2925) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2926) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2927) 	return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2928) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2929) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2930) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2931)  * There was a chain-cache miss, and we are about to add a new dependency
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2932)  * to a previous lock. We validate the following rules:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2933)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2934)  *  - would the adding of the <prev> -> <next> dependency create a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2935)  *    circular dependency in the graph? [== circular deadlock]
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2936)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2937)  *  - does the new prev->next dependency connect any hardirq-safe lock
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2938)  *    (in the full backwards-subgraph starting at <prev>) with any
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2939)  *    hardirq-unsafe lock (in the full forwards-subgraph starting at
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2940)  *    <next>)? [== illegal lock inversion with hardirq contexts]
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2941)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2942)  *  - does the new prev->next dependency connect any softirq-safe lock
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2943)  *    (in the full backwards-subgraph starting at <prev>) with any
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2944)  *    softirq-unsafe lock (in the full forwards-subgraph starting at
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2945)  *    <next>)? [== illegal lock inversion with softirq contexts]
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2946)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2947)  * any of these scenarios could lead to a deadlock.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2948)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2949)  * Then if all the validations pass, we add the forwards and backwards
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2950)  * dependency.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2951)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2952) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2953) check_prev_add(struct task_struct *curr, struct held_lock *prev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2954) 	       struct held_lock *next, u16 distance,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2955) 	       struct lock_trace **const trace)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2956) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2957) 	struct lock_list *entry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2958) 	enum bfs_result ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2959) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2960) 	if (!hlock_class(prev)->key || !hlock_class(next)->key) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2961) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2962) 		 * The warning statements below may trigger a use-after-free
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2963) 		 * of the class name. It is better to trigger a use-after free
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2964) 		 * and to have the class name most of the time instead of not
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2965) 		 * having the class name available.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2966) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2967) 		WARN_ONCE(!debug_locks_silent && !hlock_class(prev)->key,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2968) 			  "Detected use-after-free of lock class %px/%s\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2969) 			  hlock_class(prev),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2970) 			  hlock_class(prev)->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2971) 		WARN_ONCE(!debug_locks_silent && !hlock_class(next)->key,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2972) 			  "Detected use-after-free of lock class %px/%s\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2973) 			  hlock_class(next),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2974) 			  hlock_class(next)->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2975) 		return 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2976) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2977) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2978) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2979) 	 * Prove that the new <prev> -> <next> dependency would not
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2980) 	 * create a circular dependency in the graph. (We do this by
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2981) 	 * a breadth-first search into the graph starting at <next>,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2982) 	 * and check whether we can reach <prev>.)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2983) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2984) 	 * The search is limited by the size of the circular queue (i.e.,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2985) 	 * MAX_CIRCULAR_QUEUE_SIZE) which keeps track of a breadth of nodes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2986) 	 * in the graph whose neighbours are to be checked.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2987) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2988) 	ret = check_noncircular(next, prev, trace);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2989) 	if (unlikely(bfs_error(ret) || ret == BFS_RMATCH))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2990) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2991) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2992) 	if (!check_irq_usage(curr, prev, next))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2993) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2994) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2995) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2996) 	 * Is the <prev> -> <next> dependency already present?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2997) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2998) 	 * (this may occur even though this is a new chain: consider
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2999) 	 *  e.g. the L1 -> L2 -> L3 -> L4 and the L5 -> L1 -> L2 -> L3
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3000) 	 *  chains - the second one will be new, but L1 already has
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3001) 	 *  L2 added to its dependency list, due to the first chain.)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3002) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3003) 	list_for_each_entry(entry, &hlock_class(prev)->locks_after, entry) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3004) 		if (entry->class == hlock_class(next)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3005) 			if (distance == 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3006) 				entry->distance = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3007) 			entry->dep |= calc_dep(prev, next);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3008) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3009) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3010) 			 * Also, update the reverse dependency in @next's
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3011) 			 * ->locks_before list.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3012) 			 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3013) 			 *  Here we reuse @entry as the cursor, which is fine
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3014) 			 *  because we won't go to the next iteration of the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3015) 			 *  outer loop:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3016) 			 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3017) 			 *  For normal cases, we return in the inner loop.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3018) 			 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3019) 			 *  If we fail to return, we have inconsistency, i.e.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3020) 			 *  <prev>::locks_after contains <next> while
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3021) 			 *  <next>::locks_before doesn't contain <prev>. In
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3022) 			 *  that case, we return after the inner and indicate
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3023) 			 *  something is wrong.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3024) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3025) 			list_for_each_entry(entry, &hlock_class(next)->locks_before, entry) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3026) 				if (entry->class == hlock_class(prev)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3027) 					if (distance == 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3028) 						entry->distance = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3029) 					entry->dep |= calc_depb(prev, next);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3030) 					return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3031) 				}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3032) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3033) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3034) 			/* <prev> is not found in <next>::locks_before */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3035) 			return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3036) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3037) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3038) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3039) #ifdef CONFIG_LOCKDEP_SMALL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3040) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3041) 	 * Is the <prev> -> <next> link redundant?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3042) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3043) 	ret = check_redundant(prev, next);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3044) 	if (bfs_error(ret))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3045) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3046) 	else if (ret == BFS_RMATCH)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3047) 		return 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3048) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3049) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3050) 	if (!*trace) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3051) 		*trace = save_trace();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3052) 		if (!*trace)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3053) 			return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3054) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3055) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3056) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3057) 	 * Ok, all validations passed, add the new lock
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3058) 	 * to the previous lock's dependency list:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3059) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3060) 	ret = add_lock_to_list(hlock_class(next), hlock_class(prev),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3061) 			       &hlock_class(prev)->locks_after,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3062) 			       next->acquire_ip, distance,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3063) 			       calc_dep(prev, next),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3064) 			       *trace);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3065) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3066) 	if (!ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3067) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3068) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3069) 	ret = add_lock_to_list(hlock_class(prev), hlock_class(next),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3070) 			       &hlock_class(next)->locks_before,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3071) 			       next->acquire_ip, distance,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3072) 			       calc_depb(prev, next),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3073) 			       *trace);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3074) 	if (!ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3075) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3076) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3077) 	return 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3078) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3079) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3080) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3081)  * Add the dependency to all directly-previous locks that are 'relevant'.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3082)  * The ones that are relevant are (in increasing distance from curr):
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3083)  * all consecutive trylock entries and the final non-trylock entry - or
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3084)  * the end of this context's lock-chain - whichever comes first.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3085)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3086) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3087) check_prevs_add(struct task_struct *curr, struct held_lock *next)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3088) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3089) 	struct lock_trace *trace = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3090) 	int depth = curr->lockdep_depth;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3091) 	struct held_lock *hlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3092) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3093) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3094) 	 * Debugging checks.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3095) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3096) 	 * Depth must not be zero for a non-head lock:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3097) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3098) 	if (!depth)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3099) 		goto out_bug;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3100) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3101) 	 * At least two relevant locks must exist for this
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3102) 	 * to be a head:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3103) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3104) 	if (curr->held_locks[depth].irq_context !=
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3105) 			curr->held_locks[depth-1].irq_context)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3106) 		goto out_bug;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3107) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3108) 	for (;;) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3109) 		u16 distance = curr->lockdep_depth - depth + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3110) 		hlock = curr->held_locks + depth - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3111) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3112) 		if (hlock->check) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3113) 			int ret = check_prev_add(curr, hlock, next, distance, &trace);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3114) 			if (!ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3115) 				return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3116) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3117) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3118) 			 * Stop after the first non-trylock entry,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3119) 			 * as non-trylock entries have added their
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3120) 			 * own direct dependencies already, so this
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3121) 			 * lock is connected to them indirectly:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3122) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3123) 			if (!hlock->trylock)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3124) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3125) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3126) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3127) 		depth--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3128) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3129) 		 * End of lock-stack?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3130) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3131) 		if (!depth)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3132) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3133) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3134) 		 * Stop the search if we cross into another context:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3135) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3136) 		if (curr->held_locks[depth].irq_context !=
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3137) 				curr->held_locks[depth-1].irq_context)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3138) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3139) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3140) 	return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3141) out_bug:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3142) 	if (!debug_locks_off_graph_unlock())
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3143) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3144) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3145) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3146) 	 * Clearly we all shouldn't be here, but since we made it we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3147) 	 * can reliable say we messed up our state. See the above two
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3148) 	 * gotos for reasons why we could possibly end up here.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3149) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3150) 	WARN_ON(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3151) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3152) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3153) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3154) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3155) struct lock_chain lock_chains[MAX_LOCKDEP_CHAINS];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3156) static DECLARE_BITMAP(lock_chains_in_use, MAX_LOCKDEP_CHAINS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3157) static u16 chain_hlocks[MAX_LOCKDEP_CHAIN_HLOCKS];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3158) unsigned long nr_zapped_lock_chains;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3159) unsigned int nr_free_chain_hlocks;	/* Free chain_hlocks in buckets */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3160) unsigned int nr_lost_chain_hlocks;	/* Lost chain_hlocks */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3161) unsigned int nr_large_chain_blocks;	/* size > MAX_CHAIN_BUCKETS */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3162) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3163) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3164)  * The first 2 chain_hlocks entries in the chain block in the bucket
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3165)  * list contains the following meta data:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3166)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3167)  *   entry[0]:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3168)  *     Bit    15 - always set to 1 (it is not a class index)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3169)  *     Bits 0-14 - upper 15 bits of the next block index
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3170)  *   entry[1]    - lower 16 bits of next block index
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3171)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3172)  * A next block index of all 1 bits means it is the end of the list.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3173)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3174)  * On the unsized bucket (bucket-0), the 3rd and 4th entries contain
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3175)  * the chain block size:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3176)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3177)  *   entry[2] - upper 16 bits of the chain block size
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3178)  *   entry[3] - lower 16 bits of the chain block size
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3179)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3180) #define MAX_CHAIN_BUCKETS	16
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3181) #define CHAIN_BLK_FLAG		(1U << 15)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3182) #define CHAIN_BLK_LIST_END	0xFFFFU
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3183) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3184) static int chain_block_buckets[MAX_CHAIN_BUCKETS];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3185) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3186) static inline int size_to_bucket(int size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3187) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3188) 	if (size > MAX_CHAIN_BUCKETS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3189) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3190) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3191) 	return size - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3192) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3193) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3194) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3195)  * Iterate all the chain blocks in a bucket.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3196)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3197) #define for_each_chain_block(bucket, prev, curr)		\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3198) 	for ((prev) = -1, (curr) = chain_block_buckets[bucket];	\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3199) 	     (curr) >= 0;					\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3200) 	     (prev) = (curr), (curr) = chain_block_next(curr))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3201) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3202) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3203)  * next block or -1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3204)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3205) static inline int chain_block_next(int offset)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3206) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3207) 	int next = chain_hlocks[offset];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3208) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3209) 	WARN_ON_ONCE(!(next & CHAIN_BLK_FLAG));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3210) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3211) 	if (next == CHAIN_BLK_LIST_END)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3212) 		return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3213) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3214) 	next &= ~CHAIN_BLK_FLAG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3215) 	next <<= 16;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3216) 	next |= chain_hlocks[offset + 1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3217) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3218) 	return next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3219) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3220) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3221) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3222)  * bucket-0 only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3223)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3224) static inline int chain_block_size(int offset)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3225) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3226) 	return (chain_hlocks[offset + 2] << 16) | chain_hlocks[offset + 3];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3227) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3228) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3229) static inline void init_chain_block(int offset, int next, int bucket, int size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3230) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3231) 	chain_hlocks[offset] = (next >> 16) | CHAIN_BLK_FLAG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3232) 	chain_hlocks[offset + 1] = (u16)next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3233) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3234) 	if (size && !bucket) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3235) 		chain_hlocks[offset + 2] = size >> 16;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3236) 		chain_hlocks[offset + 3] = (u16)size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3237) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3238) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3239) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3240) static inline void add_chain_block(int offset, int size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3241) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3242) 	int bucket = size_to_bucket(size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3243) 	int next = chain_block_buckets[bucket];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3244) 	int prev, curr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3245) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3246) 	if (unlikely(size < 2)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3247) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3248) 		 * We can't store single entries on the freelist. Leak them.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3249) 		 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3250) 		 * One possible way out would be to uniquely mark them, other
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3251) 		 * than with CHAIN_BLK_FLAG, such that we can recover them when
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3252) 		 * the block before it is re-added.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3253) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3254) 		if (size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3255) 			nr_lost_chain_hlocks++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3256) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3257) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3258) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3259) 	nr_free_chain_hlocks += size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3260) 	if (!bucket) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3261) 		nr_large_chain_blocks++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3262) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3263) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3264) 		 * Variable sized, sort large to small.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3265) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3266) 		for_each_chain_block(0, prev, curr) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3267) 			if (size >= chain_block_size(curr))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3268) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3269) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3270) 		init_chain_block(offset, curr, 0, size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3271) 		if (prev < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3272) 			chain_block_buckets[0] = offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3273) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3274) 			init_chain_block(prev, offset, 0, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3275) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3276) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3277) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3278) 	 * Fixed size, add to head.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3279) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3280) 	init_chain_block(offset, next, bucket, size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3281) 	chain_block_buckets[bucket] = offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3282) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3283) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3284) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3285)  * Only the first block in the list can be deleted.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3286)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3287)  * For the variable size bucket[0], the first block (the largest one) is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3288)  * returned, broken up and put back into the pool. So if a chain block of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3289)  * length > MAX_CHAIN_BUCKETS is ever used and zapped, it will just be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3290)  * queued up after the primordial chain block and never be used until the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3291)  * hlock entries in the primordial chain block is almost used up. That
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3292)  * causes fragmentation and reduce allocation efficiency. That can be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3293)  * monitored by looking at the "large chain blocks" number in lockdep_stats.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3294)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3295) static inline void del_chain_block(int bucket, int size, int next)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3296) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3297) 	nr_free_chain_hlocks -= size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3298) 	chain_block_buckets[bucket] = next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3299) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3300) 	if (!bucket)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3301) 		nr_large_chain_blocks--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3302) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3303) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3304) static void init_chain_block_buckets(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3305) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3306) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3307) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3308) 	for (i = 0; i < MAX_CHAIN_BUCKETS; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3309) 		chain_block_buckets[i] = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3310) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3311) 	add_chain_block(0, ARRAY_SIZE(chain_hlocks));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3312) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3313) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3314) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3315)  * Return offset of a chain block of the right size or -1 if not found.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3316)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3317)  * Fairly simple worst-fit allocator with the addition of a number of size
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3318)  * specific free lists.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3319)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3320) static int alloc_chain_hlocks(int req)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3321) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3322) 	int bucket, curr, size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3323) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3324) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3325) 	 * We rely on the MSB to act as an escape bit to denote freelist
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3326) 	 * pointers. Make sure this bit isn't set in 'normal' class_idx usage.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3327) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3328) 	BUILD_BUG_ON((MAX_LOCKDEP_KEYS-1) & CHAIN_BLK_FLAG);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3329) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3330) 	init_data_structures_once();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3331) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3332) 	if (nr_free_chain_hlocks < req)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3333) 		return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3334) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3335) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3336) 	 * We require a minimum of 2 (u16) entries to encode a freelist
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3337) 	 * 'pointer'.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3338) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3339) 	req = max(req, 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3340) 	bucket = size_to_bucket(req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3341) 	curr = chain_block_buckets[bucket];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3342) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3343) 	if (bucket) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3344) 		if (curr >= 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3345) 			del_chain_block(bucket, req, chain_block_next(curr));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3346) 			return curr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3347) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3348) 		/* Try bucket 0 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3349) 		curr = chain_block_buckets[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3350) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3351) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3352) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3353) 	 * The variable sized freelist is sorted by size; the first entry is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3354) 	 * the largest. Use it if it fits.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3355) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3356) 	if (curr >= 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3357) 		size = chain_block_size(curr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3358) 		if (likely(size >= req)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3359) 			del_chain_block(0, size, chain_block_next(curr));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3360) 			add_chain_block(curr + req, size - req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3361) 			return curr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3362) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3363) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3364) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3365) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3366) 	 * Last resort, split a block in a larger sized bucket.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3367) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3368) 	for (size = MAX_CHAIN_BUCKETS; size > req; size--) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3369) 		bucket = size_to_bucket(size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3370) 		curr = chain_block_buckets[bucket];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3371) 		if (curr < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3372) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3373) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3374) 		del_chain_block(bucket, size, chain_block_next(curr));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3375) 		add_chain_block(curr + req, size - req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3376) 		return curr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3377) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3378) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3379) 	return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3380) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3381) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3382) static inline void free_chain_hlocks(int base, int size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3383) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3384) 	add_chain_block(base, max(size, 2));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3385) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3386) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3387) struct lock_class *lock_chain_get_class(struct lock_chain *chain, int i)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3388) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3389) 	u16 chain_hlock = chain_hlocks[chain->base + i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3390) 	unsigned int class_idx = chain_hlock_class_idx(chain_hlock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3391) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3392) 	return lock_classes + class_idx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3393) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3394) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3395) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3396)  * Returns the index of the first held_lock of the current chain
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3397)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3398) static inline int get_first_held_lock(struct task_struct *curr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3399) 					struct held_lock *hlock)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3400) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3401) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3402) 	struct held_lock *hlock_curr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3403) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3404) 	for (i = curr->lockdep_depth - 1; i >= 0; i--) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3405) 		hlock_curr = curr->held_locks + i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3406) 		if (hlock_curr->irq_context != hlock->irq_context)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3407) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3408) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3409) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3410) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3411) 	return ++i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3412) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3413) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3414) #ifdef CONFIG_DEBUG_LOCKDEP
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3415) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3416)  * Returns the next chain_key iteration
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3417)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3418) static u64 print_chain_key_iteration(u16 hlock_id, u64 chain_key)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3419) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3420) 	u64 new_chain_key = iterate_chain_key(chain_key, hlock_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3421) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3422) 	printk(" hlock_id:%d -> chain_key:%016Lx",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3423) 		(unsigned int)hlock_id,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3424) 		(unsigned long long)new_chain_key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3425) 	return new_chain_key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3426) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3427) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3428) static void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3429) print_chain_keys_held_locks(struct task_struct *curr, struct held_lock *hlock_next)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3430) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3431) 	struct held_lock *hlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3432) 	u64 chain_key = INITIAL_CHAIN_KEY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3433) 	int depth = curr->lockdep_depth;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3434) 	int i = get_first_held_lock(curr, hlock_next);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3435) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3436) 	printk("depth: %u (irq_context %u)\n", depth - i + 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3437) 		hlock_next->irq_context);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3438) 	for (; i < depth; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3439) 		hlock = curr->held_locks + i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3440) 		chain_key = print_chain_key_iteration(hlock_id(hlock), chain_key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3441) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3442) 		print_lock(hlock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3443) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3444) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3445) 	print_chain_key_iteration(hlock_id(hlock_next), chain_key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3446) 	print_lock(hlock_next);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3447) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3448) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3449) static void print_chain_keys_chain(struct lock_chain *chain)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3450) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3451) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3452) 	u64 chain_key = INITIAL_CHAIN_KEY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3453) 	u16 hlock_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3454) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3455) 	printk("depth: %u\n", chain->depth);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3456) 	for (i = 0; i < chain->depth; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3457) 		hlock_id = chain_hlocks[chain->base + i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3458) 		chain_key = print_chain_key_iteration(hlock_id, chain_key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3459) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3460) 		print_lock_name(lock_classes + chain_hlock_class_idx(hlock_id));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3461) 		printk("\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3462) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3463) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3464) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3465) static void print_collision(struct task_struct *curr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3466) 			struct held_lock *hlock_next,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3467) 			struct lock_chain *chain)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3468) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3469) 	pr_warn("\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3470) 	pr_warn("============================\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3471) 	pr_warn("WARNING: chain_key collision\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3472) 	print_kernel_ident();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3473) 	pr_warn("----------------------------\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3474) 	pr_warn("%s/%d: ", current->comm, task_pid_nr(current));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3475) 	pr_warn("Hash chain already cached but the contents don't match!\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3476) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3477) 	pr_warn("Held locks:");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3478) 	print_chain_keys_held_locks(curr, hlock_next);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3479) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3480) 	pr_warn("Locks in cached chain:");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3481) 	print_chain_keys_chain(chain);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3482) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3483) 	pr_warn("\nstack backtrace:\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3484) 	dump_stack();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3485) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3486) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3487) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3488) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3489)  * Checks whether the chain and the current held locks are consistent
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3490)  * in depth and also in content. If they are not it most likely means
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3491)  * that there was a collision during the calculation of the chain_key.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3492)  * Returns: 0 not passed, 1 passed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3493)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3494) static int check_no_collision(struct task_struct *curr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3495) 			struct held_lock *hlock,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3496) 			struct lock_chain *chain)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3497) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3498) #ifdef CONFIG_DEBUG_LOCKDEP
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3499) 	int i, j, id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3500) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3501) 	i = get_first_held_lock(curr, hlock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3502) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3503) 	if (DEBUG_LOCKS_WARN_ON(chain->depth != curr->lockdep_depth - (i - 1))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3504) 		print_collision(curr, hlock, chain);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3505) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3506) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3507) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3508) 	for (j = 0; j < chain->depth - 1; j++, i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3509) 		id = hlock_id(&curr->held_locks[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3510) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3511) 		if (DEBUG_LOCKS_WARN_ON(chain_hlocks[chain->base + j] != id)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3512) 			print_collision(curr, hlock, chain);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3513) 			return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3514) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3515) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3516) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3517) 	return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3518) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3519) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3520) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3521)  * Given an index that is >= -1, return the index of the next lock chain.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3522)  * Return -2 if there is no next lock chain.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3523)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3524) long lockdep_next_lockchain(long i)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3525) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3526) 	i = find_next_bit(lock_chains_in_use, ARRAY_SIZE(lock_chains), i + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3527) 	return i < ARRAY_SIZE(lock_chains) ? i : -2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3528) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3529) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3530) unsigned long lock_chain_count(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3531) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3532) 	return bitmap_weight(lock_chains_in_use, ARRAY_SIZE(lock_chains));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3533) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3534) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3535) /* Must be called with the graph lock held. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3536) static struct lock_chain *alloc_lock_chain(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3537) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3538) 	int idx = find_first_zero_bit(lock_chains_in_use,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3539) 				      ARRAY_SIZE(lock_chains));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3540) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3541) 	if (unlikely(idx >= ARRAY_SIZE(lock_chains)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3542) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3543) 	__set_bit(idx, lock_chains_in_use);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3544) 	return lock_chains + idx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3545) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3546) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3547) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3548)  * Adds a dependency chain into chain hashtable. And must be called with
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3549)  * graph_lock held.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3550)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3551)  * Return 0 if fail, and graph_lock is released.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3552)  * Return 1 if succeed, with graph_lock held.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3553)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3554) static inline int add_chain_cache(struct task_struct *curr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3555) 				  struct held_lock *hlock,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3556) 				  u64 chain_key)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3557) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3558) 	struct hlist_head *hash_head = chainhashentry(chain_key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3559) 	struct lock_chain *chain;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3560) 	int i, j;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3561) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3562) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3563) 	 * The caller must hold the graph lock, ensure we've got IRQs
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3564) 	 * disabled to make this an IRQ-safe lock.. for recursion reasons
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3565) 	 * lockdep won't complain about its own locking errors.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3566) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3567) 	if (lockdep_assert_locked())
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3568) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3569) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3570) 	chain = alloc_lock_chain();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3571) 	if (!chain) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3572) 		if (!debug_locks_off_graph_unlock())
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3573) 			return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3574) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3575) 		print_lockdep_off("BUG: MAX_LOCKDEP_CHAINS too low!");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3576) 		dump_stack();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3577) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3578) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3579) 	chain->chain_key = chain_key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3580) 	chain->irq_context = hlock->irq_context;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3581) 	i = get_first_held_lock(curr, hlock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3582) 	chain->depth = curr->lockdep_depth + 1 - i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3583) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3584) 	BUILD_BUG_ON((1UL << 24) <= ARRAY_SIZE(chain_hlocks));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3585) 	BUILD_BUG_ON((1UL << 6)  <= ARRAY_SIZE(curr->held_locks));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3586) 	BUILD_BUG_ON((1UL << 8*sizeof(chain_hlocks[0])) <= ARRAY_SIZE(lock_classes));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3587) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3588) 	j = alloc_chain_hlocks(chain->depth);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3589) 	if (j < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3590) 		if (!debug_locks_off_graph_unlock())
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3591) 			return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3592) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3593) 		print_lockdep_off("BUG: MAX_LOCKDEP_CHAIN_HLOCKS too low!");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3594) 		dump_stack();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3595) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3596) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3597) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3598) 	chain->base = j;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3599) 	for (j = 0; j < chain->depth - 1; j++, i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3600) 		int lock_id = hlock_id(curr->held_locks + i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3601) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3602) 		chain_hlocks[chain->base + j] = lock_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3603) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3604) 	chain_hlocks[chain->base + j] = hlock_id(hlock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3605) 	hlist_add_head_rcu(&chain->entry, hash_head);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3606) 	debug_atomic_inc(chain_lookup_misses);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3607) 	inc_chains(chain->irq_context);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3608) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3609) 	return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3610) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3611) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3612) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3613)  * Look up a dependency chain. Must be called with either the graph lock or
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3614)  * the RCU read lock held.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3615)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3616) static inline struct lock_chain *lookup_chain_cache(u64 chain_key)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3617) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3618) 	struct hlist_head *hash_head = chainhashentry(chain_key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3619) 	struct lock_chain *chain;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3620) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3621) 	hlist_for_each_entry_rcu(chain, hash_head, entry) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3622) 		if (READ_ONCE(chain->chain_key) == chain_key) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3623) 			debug_atomic_inc(chain_lookup_hits);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3624) 			return chain;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3625) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3626) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3627) 	return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3628) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3629) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3630) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3631)  * If the key is not present yet in dependency chain cache then
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3632)  * add it and return 1 - in this case the new dependency chain is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3633)  * validated. If the key is already hashed, return 0.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3634)  * (On return with 1 graph_lock is held.)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3635)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3636) static inline int lookup_chain_cache_add(struct task_struct *curr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3637) 					 struct held_lock *hlock,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3638) 					 u64 chain_key)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3639) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3640) 	struct lock_class *class = hlock_class(hlock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3641) 	struct lock_chain *chain = lookup_chain_cache(chain_key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3642) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3643) 	if (chain) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3644) cache_hit:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3645) 		if (!check_no_collision(curr, hlock, chain))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3646) 			return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3647) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3648) 		if (very_verbose(class)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3649) 			printk("\nhash chain already cached, key: "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3650) 					"%016Lx tail class: [%px] %s\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3651) 					(unsigned long long)chain_key,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3652) 					class->key, class->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3653) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3654) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3655) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3656) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3657) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3658) 	if (very_verbose(class)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3659) 		printk("\nnew hash chain, key: %016Lx tail class: [%px] %s\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3660) 			(unsigned long long)chain_key, class->key, class->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3661) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3662) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3663) 	if (!graph_lock())
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3664) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3665) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3666) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3667) 	 * We have to walk the chain again locked - to avoid duplicates:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3668) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3669) 	chain = lookup_chain_cache(chain_key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3670) 	if (chain) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3671) 		graph_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3672) 		goto cache_hit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3673) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3674) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3675) 	if (!add_chain_cache(curr, hlock, chain_key))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3676) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3677) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3678) 	return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3679) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3680) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3681) static int validate_chain(struct task_struct *curr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3682) 			  struct held_lock *hlock,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3683) 			  int chain_head, u64 chain_key)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3684) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3685) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3686) 	 * Trylock needs to maintain the stack of held locks, but it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3687) 	 * does not add new dependencies, because trylock can be done
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3688) 	 * in any order.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3689) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3690) 	 * We look up the chain_key and do the O(N^2) check and update of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3691) 	 * the dependencies only if this is a new dependency chain.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3692) 	 * (If lookup_chain_cache_add() return with 1 it acquires
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3693) 	 * graph_lock for us)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3694) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3695) 	if (!hlock->trylock && hlock->check &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3696) 	    lookup_chain_cache_add(curr, hlock, chain_key)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3697) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3698) 		 * Check whether last held lock:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3699) 		 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3700) 		 * - is irq-safe, if this lock is irq-unsafe
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3701) 		 * - is softirq-safe, if this lock is hardirq-unsafe
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3702) 		 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3703) 		 * And check whether the new lock's dependency graph
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3704) 		 * could lead back to the previous lock:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3705) 		 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3706) 		 * - within the current held-lock stack
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3707) 		 * - across our accumulated lock dependency records
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3708) 		 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3709) 		 * any of these scenarios could lead to a deadlock.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3710) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3711) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3712) 		 * The simple case: does the current hold the same lock
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3713) 		 * already?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3714) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3715) 		int ret = check_deadlock(curr, hlock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3716) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3717) 		if (!ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3718) 			return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3719) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3720) 		 * Add dependency only if this lock is not the head
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3721) 		 * of the chain, and if the new lock introduces no more
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3722) 		 * lock dependency (because we already hold a lock with the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3723) 		 * same lock class) nor deadlock (because the nest_lock
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3724) 		 * serializes nesting locks), see the comments for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3725) 		 * check_deadlock().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3726) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3727) 		if (!chain_head && ret != 2) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3728) 			if (!check_prevs_add(curr, hlock))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3729) 				return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3730) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3731) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3732) 		graph_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3733) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3734) 		/* after lookup_chain_cache_add(): */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3735) 		if (unlikely(!debug_locks))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3736) 			return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3737) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3738) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3739) 	return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3740) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3741) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3742) static inline int validate_chain(struct task_struct *curr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3743) 				 struct held_lock *hlock,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3744) 				 int chain_head, u64 chain_key)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3745) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3746) 	return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3747) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3748) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3749) static void init_chain_block_buckets(void)	{ }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3750) #endif /* CONFIG_PROVE_LOCKING */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3751) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3752) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3753)  * We are building curr_chain_key incrementally, so double-check
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3754)  * it from scratch, to make sure that it's done correctly:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3755)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3756) static void check_chain_key(struct task_struct *curr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3757) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3758) #ifdef CONFIG_DEBUG_LOCKDEP
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3759) 	struct held_lock *hlock, *prev_hlock = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3760) 	unsigned int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3761) 	u64 chain_key = INITIAL_CHAIN_KEY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3762) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3763) 	for (i = 0; i < curr->lockdep_depth; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3764) 		hlock = curr->held_locks + i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3765) 		if (chain_key != hlock->prev_chain_key) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3766) 			debug_locks_off();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3767) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3768) 			 * We got mighty confused, our chain keys don't match
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3769) 			 * with what we expect, someone trample on our task state?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3770) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3771) 			WARN(1, "hm#1, depth: %u [%u], %016Lx != %016Lx\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3772) 				curr->lockdep_depth, i,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3773) 				(unsigned long long)chain_key,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3774) 				(unsigned long long)hlock->prev_chain_key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3775) 			return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3776) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3777) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3778) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3779) 		 * hlock->class_idx can't go beyond MAX_LOCKDEP_KEYS, but is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3780) 		 * it registered lock class index?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3781) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3782) 		if (DEBUG_LOCKS_WARN_ON(!test_bit(hlock->class_idx, lock_classes_in_use)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3783) 			return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3784) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3785) 		if (prev_hlock && (prev_hlock->irq_context !=
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3786) 							hlock->irq_context))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3787) 			chain_key = INITIAL_CHAIN_KEY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3788) 		chain_key = iterate_chain_key(chain_key, hlock_id(hlock));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3789) 		prev_hlock = hlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3790) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3791) 	if (chain_key != curr->curr_chain_key) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3792) 		debug_locks_off();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3793) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3794) 		 * More smoking hash instead of calculating it, damn see these
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3795) 		 * numbers float.. I bet that a pink elephant stepped on my memory.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3796) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3797) 		WARN(1, "hm#2, depth: %u [%u], %016Lx != %016Lx\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3798) 			curr->lockdep_depth, i,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3799) 			(unsigned long long)chain_key,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3800) 			(unsigned long long)curr->curr_chain_key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3801) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3802) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3803) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3804) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3805) #ifdef CONFIG_PROVE_LOCKING
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3806) static int mark_lock(struct task_struct *curr, struct held_lock *this,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3807) 		     enum lock_usage_bit new_bit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3808) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3809) static void print_usage_bug_scenario(struct held_lock *lock)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3810) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3811) 	struct lock_class *class = hlock_class(lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3812) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3813) 	printk(" Possible unsafe locking scenario:\n\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3814) 	printk("       CPU0\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3815) 	printk("       ----\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3816) 	printk("  lock(");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3817) 	__print_lock_name(class);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3818) 	printk(KERN_CONT ");\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3819) 	printk("  <Interrupt>\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3820) 	printk("    lock(");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3821) 	__print_lock_name(class);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3822) 	printk(KERN_CONT ");\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3823) 	printk("\n *** DEADLOCK ***\n\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3824) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3825) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3826) static void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3827) print_usage_bug(struct task_struct *curr, struct held_lock *this,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3828) 		enum lock_usage_bit prev_bit, enum lock_usage_bit new_bit)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3829) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3830) 	if (!debug_locks_off() || debug_locks_silent)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3831) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3832) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3833) 	pr_warn("\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3834) 	pr_warn("================================\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3835) 	pr_warn("WARNING: inconsistent lock state\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3836) 	print_kernel_ident();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3837) 	pr_warn("--------------------------------\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3838) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3839) 	pr_warn("inconsistent {%s} -> {%s} usage.\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3840) 		usage_str[prev_bit], usage_str[new_bit]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3841) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3842) 	pr_warn("%s/%d [HC%u[%lu]:SC%u[%lu]:HE%u:SE%u] takes:\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3843) 		curr->comm, task_pid_nr(curr),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3844) 		lockdep_hardirq_context(), hardirq_count() >> HARDIRQ_SHIFT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3845) 		lockdep_softirq_context(curr), softirq_count() >> SOFTIRQ_SHIFT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3846) 		lockdep_hardirqs_enabled(),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3847) 		lockdep_softirqs_enabled(curr));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3848) 	print_lock(this);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3849) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3850) 	pr_warn("{%s} state was registered at:\n", usage_str[prev_bit]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3851) 	print_lock_trace(hlock_class(this)->usage_traces[prev_bit], 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3852) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3853) 	print_irqtrace_events(curr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3854) 	pr_warn("\nother info that might help us debug this:\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3855) 	print_usage_bug_scenario(this);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3856) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3857) 	lockdep_print_held_locks(curr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3858) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3859) 	pr_warn("\nstack backtrace:\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3860) 	dump_stack();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3861) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3862) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3863) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3864)  * Print out an error if an invalid bit is set:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3865)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3866) static inline int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3867) valid_state(struct task_struct *curr, struct held_lock *this,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3868) 	    enum lock_usage_bit new_bit, enum lock_usage_bit bad_bit)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3869) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3870) 	if (unlikely(hlock_class(this)->usage_mask & (1 << bad_bit))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3871) 		graph_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3872) 		print_usage_bug(curr, this, bad_bit, new_bit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3873) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3874) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3875) 	return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3876) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3877) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3878) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3879) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3880)  * print irq inversion bug:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3881)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3882) static void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3883) print_irq_inversion_bug(struct task_struct *curr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3884) 			struct lock_list *root, struct lock_list *other,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3885) 			struct held_lock *this, int forwards,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3886) 			const char *irqclass)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3887) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3888) 	struct lock_list *entry = other;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3889) 	struct lock_list *middle = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3890) 	int depth;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3891) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3892) 	if (!debug_locks_off_graph_unlock() || debug_locks_silent)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3893) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3894) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3895) 	pr_warn("\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3896) 	pr_warn("========================================================\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3897) 	pr_warn("WARNING: possible irq lock inversion dependency detected\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3898) 	print_kernel_ident();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3899) 	pr_warn("--------------------------------------------------------\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3900) 	pr_warn("%s/%d just changed the state of lock:\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3901) 		curr->comm, task_pid_nr(curr));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3902) 	print_lock(this);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3903) 	if (forwards)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3904) 		pr_warn("but this lock took another, %s-unsafe lock in the past:\n", irqclass);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3905) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3906) 		pr_warn("but this lock was taken by another, %s-safe lock in the past:\n", irqclass);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3907) 	print_lock_name(other->class);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3908) 	pr_warn("\n\nand interrupts could create inverse lock ordering between them.\n\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3909) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3910) 	pr_warn("\nother info that might help us debug this:\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3911) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3912) 	/* Find a middle lock (if one exists) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3913) 	depth = get_lock_depth(other);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3914) 	do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3915) 		if (depth == 0 && (entry != root)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3916) 			pr_warn("lockdep:%s bad path found in chain graph\n", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3917) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3918) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3919) 		middle = entry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3920) 		entry = get_lock_parent(entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3921) 		depth--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3922) 	} while (entry && entry != root && (depth >= 0));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3923) 	if (forwards)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3924) 		print_irq_lock_scenario(root, other,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3925) 			middle ? middle->class : root->class, other->class);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3926) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3927) 		print_irq_lock_scenario(other, root,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3928) 			middle ? middle->class : other->class, root->class);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3929) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3930) 	lockdep_print_held_locks(curr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3931) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3932) 	pr_warn("\nthe shortest dependencies between 2nd lock and 1st lock:\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3933) 	root->trace = save_trace();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3934) 	if (!root->trace)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3935) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3936) 	print_shortest_lock_dependencies(other, root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3937) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3938) 	pr_warn("\nstack backtrace:\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3939) 	dump_stack();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3940) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3941) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3942) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3943)  * Prove that in the forwards-direction subgraph starting at <this>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3944)  * there is no lock matching <mask>:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3945)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3946) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3947) check_usage_forwards(struct task_struct *curr, struct held_lock *this,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3948) 		     enum lock_usage_bit bit)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3949) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3950) 	enum bfs_result ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3951) 	struct lock_list root;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3952) 	struct lock_list *target_entry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3953) 	enum lock_usage_bit read_bit = bit + LOCK_USAGE_READ_MASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3954) 	unsigned usage_mask = lock_flag(bit) | lock_flag(read_bit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3955) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3956) 	bfs_init_root(&root, this);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3957) 	ret = find_usage_forwards(&root, usage_mask, &target_entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3958) 	if (bfs_error(ret)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3959) 		print_bfs_bug(ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3960) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3961) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3962) 	if (ret == BFS_RNOMATCH)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3963) 		return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3964) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3965) 	/* Check whether write or read usage is the match */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3966) 	if (target_entry->class->usage_mask & lock_flag(bit)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3967) 		print_irq_inversion_bug(curr, &root, target_entry,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3968) 					this, 1, state_name(bit));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3969) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3970) 		print_irq_inversion_bug(curr, &root, target_entry,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3971) 					this, 1, state_name(read_bit));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3972) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3973) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3974) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3975) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3976) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3977) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3978)  * Prove that in the backwards-direction subgraph starting at <this>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3979)  * there is no lock matching <mask>:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3980)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3981) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3982) check_usage_backwards(struct task_struct *curr, struct held_lock *this,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3983) 		      enum lock_usage_bit bit)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3984) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3985) 	enum bfs_result ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3986) 	struct lock_list root;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3987) 	struct lock_list *target_entry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3988) 	enum lock_usage_bit read_bit = bit + LOCK_USAGE_READ_MASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3989) 	unsigned usage_mask = lock_flag(bit) | lock_flag(read_bit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3990) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3991) 	bfs_init_rootb(&root, this);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3992) 	ret = find_usage_backwards(&root, usage_mask, &target_entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3993) 	if (bfs_error(ret)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3994) 		print_bfs_bug(ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3995) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3996) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3997) 	if (ret == BFS_RNOMATCH)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3998) 		return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3999) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4000) 	/* Check whether write or read usage is the match */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4001) 	if (target_entry->class->usage_mask & lock_flag(bit)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4002) 		print_irq_inversion_bug(curr, &root, target_entry,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4003) 					this, 0, state_name(bit));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4004) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4005) 		print_irq_inversion_bug(curr, &root, target_entry,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4006) 					this, 0, state_name(read_bit));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4007) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4008) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4009) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4010) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4011) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4012) void print_irqtrace_events(struct task_struct *curr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4013) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4014) 	const struct irqtrace_events *trace = &curr->irqtrace;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4015) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4016) 	printk("irq event stamp: %u\n", trace->irq_events);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4017) 	printk("hardirqs last  enabled at (%u): [<%px>] %pS\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4018) 		trace->hardirq_enable_event, (void *)trace->hardirq_enable_ip,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4019) 		(void *)trace->hardirq_enable_ip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4020) 	printk("hardirqs last disabled at (%u): [<%px>] %pS\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4021) 		trace->hardirq_disable_event, (void *)trace->hardirq_disable_ip,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4022) 		(void *)trace->hardirq_disable_ip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4023) 	printk("softirqs last  enabled at (%u): [<%px>] %pS\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4024) 		trace->softirq_enable_event, (void *)trace->softirq_enable_ip,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4025) 		(void *)trace->softirq_enable_ip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4026) 	printk("softirqs last disabled at (%u): [<%px>] %pS\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4027) 		trace->softirq_disable_event, (void *)trace->softirq_disable_ip,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4028) 		(void *)trace->softirq_disable_ip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4029) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4030) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4031) static int HARDIRQ_verbose(struct lock_class *class)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4032) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4033) #if HARDIRQ_VERBOSE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4034) 	return class_filter(class);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4035) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4036) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4037) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4038) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4039) static int SOFTIRQ_verbose(struct lock_class *class)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4040) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4041) #if SOFTIRQ_VERBOSE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4042) 	return class_filter(class);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4043) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4044) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4045) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4046) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4047) static int (*state_verbose_f[])(struct lock_class *class) = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4048) #define LOCKDEP_STATE(__STATE) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4049) 	__STATE##_verbose,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4050) #include "lockdep_states.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4051) #undef LOCKDEP_STATE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4052) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4053) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4054) static inline int state_verbose(enum lock_usage_bit bit,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4055) 				struct lock_class *class)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4056) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4057) 	return state_verbose_f[bit >> LOCK_USAGE_DIR_MASK](class);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4058) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4059) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4060) typedef int (*check_usage_f)(struct task_struct *, struct held_lock *,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4061) 			     enum lock_usage_bit bit, const char *name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4062) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4063) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4064) mark_lock_irq(struct task_struct *curr, struct held_lock *this,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4065) 		enum lock_usage_bit new_bit)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4066) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4067) 	int excl_bit = exclusive_bit(new_bit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4068) 	int read = new_bit & LOCK_USAGE_READ_MASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4069) 	int dir = new_bit & LOCK_USAGE_DIR_MASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4070) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4071) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4072) 	 * Validate that this particular lock does not have conflicting
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4073) 	 * usage states.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4074) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4075) 	if (!valid_state(curr, this, new_bit, excl_bit))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4076) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4077) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4078) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4079) 	 * Check for read in write conflicts
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4080) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4081) 	if (!read && !valid_state(curr, this, new_bit,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4082) 				  excl_bit + LOCK_USAGE_READ_MASK))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4083) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4084) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4085) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4086) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4087) 	 * Validate that the lock dependencies don't have conflicting usage
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4088) 	 * states.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4089) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4090) 	if (dir) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4091) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4092) 		 * mark ENABLED has to look backwards -- to ensure no dependee
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4093) 		 * has USED_IN state, which, again, would allow  recursion deadlocks.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4094) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4095) 		if (!check_usage_backwards(curr, this, excl_bit))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4096) 			return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4097) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4098) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4099) 		 * mark USED_IN has to look forwards -- to ensure no dependency
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4100) 		 * has ENABLED state, which would allow recursion deadlocks.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4101) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4102) 		if (!check_usage_forwards(curr, this, excl_bit))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4103) 			return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4104) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4105) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4106) 	if (state_verbose(new_bit, hlock_class(this)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4107) 		return 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4108) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4109) 	return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4110) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4111) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4112) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4113)  * Mark all held locks with a usage bit:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4114)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4115) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4116) mark_held_locks(struct task_struct *curr, enum lock_usage_bit base_bit)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4117) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4118) 	struct held_lock *hlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4119) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4120) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4121) 	for (i = 0; i < curr->lockdep_depth; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4122) 		enum lock_usage_bit hlock_bit = base_bit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4123) 		hlock = curr->held_locks + i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4124) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4125) 		if (hlock->read)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4126) 			hlock_bit += LOCK_USAGE_READ_MASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4127) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4128) 		BUG_ON(hlock_bit >= LOCK_USAGE_STATES);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4129) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4130) 		if (!hlock->check)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4131) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4132) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4133) 		if (!mark_lock(curr, hlock, hlock_bit))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4134) 			return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4135) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4136) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4137) 	return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4138) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4139) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4140) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4141)  * Hardirqs will be enabled:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4142)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4143) static void __trace_hardirqs_on_caller(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4144) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4145) 	struct task_struct *curr = current;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4146) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4147) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4148) 	 * We are going to turn hardirqs on, so set the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4149) 	 * usage bit for all held locks:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4150) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4151) 	if (!mark_held_locks(curr, LOCK_ENABLED_HARDIRQ))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4152) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4153) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4154) 	 * If we have softirqs enabled, then set the usage
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4155) 	 * bit for all held locks. (disabled hardirqs prevented
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4156) 	 * this bit from being set before)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4157) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4158) 	if (curr->softirqs_enabled)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4159) 		mark_held_locks(curr, LOCK_ENABLED_SOFTIRQ);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4160) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4161) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4162) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4163)  * lockdep_hardirqs_on_prepare - Prepare for enabling interrupts
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4164)  * @ip:		Caller address
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4165)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4166)  * Invoked before a possible transition to RCU idle from exit to user or
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4167)  * guest mode. This ensures that all RCU operations are done before RCU
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4168)  * stops watching. After the RCU transition lockdep_hardirqs_on() has to be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4169)  * invoked to set the final state.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4170)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4171) void lockdep_hardirqs_on_prepare(unsigned long ip)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4172) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4173) 	if (unlikely(!debug_locks))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4174) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4175) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4176) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4177) 	 * NMIs do not (and cannot) track lock dependencies, nothing to do.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4178) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4179) 	if (unlikely(in_nmi()))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4180) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4181) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4182) 	if (unlikely(this_cpu_read(lockdep_recursion)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4183) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4184) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4185) 	if (unlikely(lockdep_hardirqs_enabled())) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4186) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4187) 		 * Neither irq nor preemption are disabled here
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4188) 		 * so this is racy by nature but losing one hit
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4189) 		 * in a stat is not a big deal.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4190) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4191) 		__debug_atomic_inc(redundant_hardirqs_on);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4192) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4193) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4194) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4195) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4196) 	 * We're enabling irqs and according to our state above irqs weren't
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4197) 	 * already enabled, yet we find the hardware thinks they are in fact
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4198) 	 * enabled.. someone messed up their IRQ state tracing.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4199) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4200) 	if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4201) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4202) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4203) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4204) 	 * See the fine text that goes along with this variable definition.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4205) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4206) 	if (DEBUG_LOCKS_WARN_ON(early_boot_irqs_disabled))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4207) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4208) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4209) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4210) 	 * Can't allow enabling interrupts while in an interrupt handler,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4211) 	 * that's general bad form and such. Recursion, limited stack etc..
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4212) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4213) 	if (DEBUG_LOCKS_WARN_ON(lockdep_hardirq_context()))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4214) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4215) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4216) 	current->hardirq_chain_key = current->curr_chain_key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4217) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4218) 	lockdep_recursion_inc();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4219) 	__trace_hardirqs_on_caller();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4220) 	lockdep_recursion_finish();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4221) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4222) EXPORT_SYMBOL_GPL(lockdep_hardirqs_on_prepare);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4223) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4224) void noinstr lockdep_hardirqs_on(unsigned long ip)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4225) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4226) 	struct irqtrace_events *trace = &current->irqtrace;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4227) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4228) 	if (unlikely(!debug_locks))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4229) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4230) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4231) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4232) 	 * NMIs can happen in the middle of local_irq_{en,dis}able() where the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4233) 	 * tracking state and hardware state are out of sync.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4234) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4235) 	 * NMIs must save lockdep_hardirqs_enabled() to restore IRQ state from,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4236) 	 * and not rely on hardware state like normal interrupts.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4237) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4238) 	if (unlikely(in_nmi())) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4239) 		if (!IS_ENABLED(CONFIG_TRACE_IRQFLAGS_NMI))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4240) 			return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4241) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4242) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4243) 		 * Skip:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4244) 		 *  - recursion check, because NMI can hit lockdep;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4245) 		 *  - hardware state check, because above;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4246) 		 *  - chain_key check, see lockdep_hardirqs_on_prepare().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4247) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4248) 		goto skip_checks;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4249) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4250) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4251) 	if (unlikely(this_cpu_read(lockdep_recursion)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4252) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4253) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4254) 	if (lockdep_hardirqs_enabled()) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4255) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4256) 		 * Neither irq nor preemption are disabled here
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4257) 		 * so this is racy by nature but losing one hit
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4258) 		 * in a stat is not a big deal.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4259) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4260) 		__debug_atomic_inc(redundant_hardirqs_on);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4261) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4262) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4263) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4264) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4265) 	 * We're enabling irqs and according to our state above irqs weren't
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4266) 	 * already enabled, yet we find the hardware thinks they are in fact
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4267) 	 * enabled.. someone messed up their IRQ state tracing.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4268) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4269) 	if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4270) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4271) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4272) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4273) 	 * Ensure the lock stack remained unchanged between
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4274) 	 * lockdep_hardirqs_on_prepare() and lockdep_hardirqs_on().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4275) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4276) 	DEBUG_LOCKS_WARN_ON(current->hardirq_chain_key !=
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4277) 			    current->curr_chain_key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4278) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4279) skip_checks:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4280) 	/* we'll do an OFF -> ON transition: */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4281) 	__this_cpu_write(hardirqs_enabled, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4282) 	trace->hardirq_enable_ip = ip;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4283) 	trace->hardirq_enable_event = ++trace->irq_events;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4284) 	debug_atomic_inc(hardirqs_on_events);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4285) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4286) EXPORT_SYMBOL_GPL(lockdep_hardirqs_on);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4287) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4288) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4289)  * Hardirqs were disabled:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4290)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4291) void noinstr lockdep_hardirqs_off(unsigned long ip)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4292) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4293) 	if (unlikely(!debug_locks))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4294) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4295) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4296) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4297) 	 * Matching lockdep_hardirqs_on(), allow NMIs in the middle of lockdep;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4298) 	 * they will restore the software state. This ensures the software
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4299) 	 * state is consistent inside NMIs as well.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4300) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4301) 	if (in_nmi()) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4302) 		if (!IS_ENABLED(CONFIG_TRACE_IRQFLAGS_NMI))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4303) 			return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4304) 	} else if (__this_cpu_read(lockdep_recursion))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4305) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4306) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4307) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4308) 	 * So we're supposed to get called after you mask local IRQs, but for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4309) 	 * some reason the hardware doesn't quite think you did a proper job.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4310) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4311) 	if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4312) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4313) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4314) 	if (lockdep_hardirqs_enabled()) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4315) 		struct irqtrace_events *trace = &current->irqtrace;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4316) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4317) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4318) 		 * We have done an ON -> OFF transition:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4319) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4320) 		__this_cpu_write(hardirqs_enabled, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4321) 		trace->hardirq_disable_ip = ip;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4322) 		trace->hardirq_disable_event = ++trace->irq_events;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4323) 		debug_atomic_inc(hardirqs_off_events);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4324) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4325) 		debug_atomic_inc(redundant_hardirqs_off);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4326) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4327) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4328) EXPORT_SYMBOL_GPL(lockdep_hardirqs_off);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4329) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4330) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4331)  * Softirqs will be enabled:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4332)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4333) void lockdep_softirqs_on(unsigned long ip)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4334) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4335) 	struct irqtrace_events *trace = &current->irqtrace;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4336) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4337) 	if (unlikely(!lockdep_enabled()))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4338) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4339) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4340) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4341) 	 * We fancy IRQs being disabled here, see softirq.c, avoids
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4342) 	 * funny state and nesting things.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4343) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4344) 	if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4345) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4346) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4347) 	if (current->softirqs_enabled) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4348) 		debug_atomic_inc(redundant_softirqs_on);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4349) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4350) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4351) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4352) 	lockdep_recursion_inc();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4353) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4354) 	 * We'll do an OFF -> ON transition:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4355) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4356) 	current->softirqs_enabled = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4357) 	trace->softirq_enable_ip = ip;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4358) 	trace->softirq_enable_event = ++trace->irq_events;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4359) 	debug_atomic_inc(softirqs_on_events);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4360) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4361) 	 * We are going to turn softirqs on, so set the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4362) 	 * usage bit for all held locks, if hardirqs are
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4363) 	 * enabled too:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4364) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4365) 	if (lockdep_hardirqs_enabled())
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4366) 		mark_held_locks(current, LOCK_ENABLED_SOFTIRQ);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4367) 	lockdep_recursion_finish();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4368) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4369) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4370) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4371)  * Softirqs were disabled:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4372)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4373) void lockdep_softirqs_off(unsigned long ip)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4374) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4375) 	if (unlikely(!lockdep_enabled()))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4376) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4377) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4378) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4379) 	 * We fancy IRQs being disabled here, see softirq.c
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4380) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4381) 	if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4382) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4383) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4384) 	if (current->softirqs_enabled) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4385) 		struct irqtrace_events *trace = &current->irqtrace;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4386) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4387) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4388) 		 * We have done an ON -> OFF transition:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4389) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4390) 		current->softirqs_enabled = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4391) 		trace->softirq_disable_ip = ip;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4392) 		trace->softirq_disable_event = ++trace->irq_events;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4393) 		debug_atomic_inc(softirqs_off_events);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4394) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4395) 		 * Whoops, we wanted softirqs off, so why aren't they?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4396) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4397) 		DEBUG_LOCKS_WARN_ON(!softirq_count());
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4398) 	} else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4399) 		debug_atomic_inc(redundant_softirqs_off);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4400) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4401) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4402) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4403) mark_usage(struct task_struct *curr, struct held_lock *hlock, int check)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4404) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4405) 	if (!check)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4406) 		goto lock_used;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4407) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4408) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4409) 	 * If non-trylock use in a hardirq or softirq context, then
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4410) 	 * mark the lock as used in these contexts:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4411) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4412) 	if (!hlock->trylock) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4413) 		if (hlock->read) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4414) 			if (lockdep_hardirq_context())
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4415) 				if (!mark_lock(curr, hlock,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4416) 						LOCK_USED_IN_HARDIRQ_READ))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4417) 					return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4418) 			if (curr->softirq_context)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4419) 				if (!mark_lock(curr, hlock,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4420) 						LOCK_USED_IN_SOFTIRQ_READ))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4421) 					return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4422) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4423) 			if (lockdep_hardirq_context())
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4424) 				if (!mark_lock(curr, hlock, LOCK_USED_IN_HARDIRQ))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4425) 					return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4426) 			if (curr->softirq_context)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4427) 				if (!mark_lock(curr, hlock, LOCK_USED_IN_SOFTIRQ))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4428) 					return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4429) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4430) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4431) 	if (!hlock->hardirqs_off) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4432) 		if (hlock->read) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4433) 			if (!mark_lock(curr, hlock,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4434) 					LOCK_ENABLED_HARDIRQ_READ))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4435) 				return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4436) 			if (curr->softirqs_enabled)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4437) 				if (!mark_lock(curr, hlock,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4438) 						LOCK_ENABLED_SOFTIRQ_READ))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4439) 					return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4440) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4441) 			if (!mark_lock(curr, hlock,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4442) 					LOCK_ENABLED_HARDIRQ))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4443) 				return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4444) 			if (curr->softirqs_enabled)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4445) 				if (!mark_lock(curr, hlock,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4446) 						LOCK_ENABLED_SOFTIRQ))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4447) 					return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4448) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4449) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4450) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4451) lock_used:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4452) 	/* mark it as used: */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4453) 	if (!mark_lock(curr, hlock, LOCK_USED))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4454) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4455) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4456) 	return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4457) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4458) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4459) static inline unsigned int task_irq_context(struct task_struct *task)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4460) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4461) 	return LOCK_CHAIN_HARDIRQ_CONTEXT * !!lockdep_hardirq_context() +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4462) 	       LOCK_CHAIN_SOFTIRQ_CONTEXT * !!task->softirq_context;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4463) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4464) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4465) static int separate_irq_context(struct task_struct *curr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4466) 		struct held_lock *hlock)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4467) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4468) 	unsigned int depth = curr->lockdep_depth;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4469) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4470) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4471) 	 * Keep track of points where we cross into an interrupt context:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4472) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4473) 	if (depth) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4474) 		struct held_lock *prev_hlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4475) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4476) 		prev_hlock = curr->held_locks + depth-1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4477) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4478) 		 * If we cross into another context, reset the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4479) 		 * hash key (this also prevents the checking and the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4480) 		 * adding of the dependency to 'prev'):
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4481) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4482) 		if (prev_hlock->irq_context != hlock->irq_context)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4483) 			return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4484) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4485) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4486) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4487) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4488) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4489)  * Mark a lock with a usage bit, and validate the state transition:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4490)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4491) static int mark_lock(struct task_struct *curr, struct held_lock *this,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4492) 			     enum lock_usage_bit new_bit)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4493) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4494) 	unsigned int new_mask, ret = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4495) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4496) 	if (new_bit >= LOCK_USAGE_STATES) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4497) 		DEBUG_LOCKS_WARN_ON(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4498) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4499) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4500) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4501) 	if (new_bit == LOCK_USED && this->read)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4502) 		new_bit = LOCK_USED_READ;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4503) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4504) 	new_mask = 1 << new_bit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4505) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4506) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4507) 	 * If already set then do not dirty the cacheline,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4508) 	 * nor do any checks:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4509) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4510) 	if (likely(hlock_class(this)->usage_mask & new_mask))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4511) 		return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4512) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4513) 	if (!graph_lock())
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4514) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4515) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4516) 	 * Make sure we didn't race:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4517) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4518) 	if (unlikely(hlock_class(this)->usage_mask & new_mask))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4519) 		goto unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4520) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4521) 	if (!hlock_class(this)->usage_mask)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4522) 		debug_atomic_dec(nr_unused_locks);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4523) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4524) 	hlock_class(this)->usage_mask |= new_mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4525) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4526) 	if (new_bit < LOCK_TRACE_STATES) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4527) 		if (!(hlock_class(this)->usage_traces[new_bit] = save_trace()))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4528) 			return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4529) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4530) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4531) 	if (new_bit < LOCK_USED) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4532) 		ret = mark_lock_irq(curr, this, new_bit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4533) 		if (!ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4534) 			return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4535) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4536) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4537) unlock:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4538) 	graph_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4539) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4540) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4541) 	 * We must printk outside of the graph_lock:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4542) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4543) 	if (ret == 2) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4544) 		printk("\nmarked lock as {%s}:\n", usage_str[new_bit]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4545) 		print_lock(this);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4546) 		print_irqtrace_events(curr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4547) 		dump_stack();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4548) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4549) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4550) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4551) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4552) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4553) static inline short task_wait_context(struct task_struct *curr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4554) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4555) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4556) 	 * Set appropriate wait type for the context; for IRQs we have to take
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4557) 	 * into account force_irqthread as that is implied by PREEMPT_RT.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4558) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4559) 	if (lockdep_hardirq_context()) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4560) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4561) 		 * Check if force_irqthreads will run us threaded.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4562) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4563) 		if (curr->hardirq_threaded || curr->irq_config)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4564) 			return LD_WAIT_CONFIG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4565) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4566) 		return LD_WAIT_SPIN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4567) 	} else if (curr->softirq_context) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4568) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4569) 		 * Softirqs are always threaded.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4570) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4571) 		return LD_WAIT_CONFIG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4572) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4573) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4574) 	return LD_WAIT_MAX;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4575) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4576) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4577) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4578) print_lock_invalid_wait_context(struct task_struct *curr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4579) 				struct held_lock *hlock)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4580) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4581) 	short curr_inner;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4582) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4583) 	if (!debug_locks_off())
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4584) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4585) 	if (debug_locks_silent)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4586) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4587) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4588) 	pr_warn("\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4589) 	pr_warn("=============================\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4590) 	pr_warn("[ BUG: Invalid wait context ]\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4591) 	print_kernel_ident();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4592) 	pr_warn("-----------------------------\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4593) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4594) 	pr_warn("%s/%d is trying to lock:\n", curr->comm, task_pid_nr(curr));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4595) 	print_lock(hlock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4596) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4597) 	pr_warn("other info that might help us debug this:\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4598) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4599) 	curr_inner = task_wait_context(curr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4600) 	pr_warn("context-{%d:%d}\n", curr_inner, curr_inner);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4601) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4602) 	lockdep_print_held_locks(curr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4603) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4604) 	pr_warn("stack backtrace:\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4605) 	dump_stack();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4606) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4607) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4608) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4609) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4610) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4611)  * Verify the wait_type context.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4612)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4613)  * This check validates we takes locks in the right wait-type order; that is it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4614)  * ensures that we do not take mutexes inside spinlocks and do not attempt to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4615)  * acquire spinlocks inside raw_spinlocks and the sort.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4616)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4617)  * The entire thing is slightly more complex because of RCU, RCU is a lock that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4618)  * can be taken from (pretty much) any context but also has constraints.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4619)  * However when taken in a stricter environment the RCU lock does not loosen
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4620)  * the constraints.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4621)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4622)  * Therefore we must look for the strictest environment in the lock stack and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4623)  * compare that to the lock we're trying to acquire.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4624)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4625) static int check_wait_context(struct task_struct *curr, struct held_lock *next)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4626) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4627) 	u8 next_inner = hlock_class(next)->wait_type_inner;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4628) 	u8 next_outer = hlock_class(next)->wait_type_outer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4629) 	u8 curr_inner;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4630) 	int depth;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4631) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4632) 	if (!next_inner || next->trylock)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4633) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4634) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4635) 	if (!next_outer)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4636) 		next_outer = next_inner;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4637) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4638) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4639) 	 * Find start of current irq_context..
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4640) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4641) 	for (depth = curr->lockdep_depth - 1; depth >= 0; depth--) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4642) 		struct held_lock *prev = curr->held_locks + depth;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4643) 		if (prev->irq_context != next->irq_context)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4644) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4645) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4646) 	depth++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4647) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4648) 	curr_inner = task_wait_context(curr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4649) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4650) 	for (; depth < curr->lockdep_depth; depth++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4651) 		struct held_lock *prev = curr->held_locks + depth;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4652) 		u8 prev_inner = hlock_class(prev)->wait_type_inner;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4653) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4654) 		if (prev_inner) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4655) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4656) 			 * We can have a bigger inner than a previous one
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4657) 			 * when outer is smaller than inner, as with RCU.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4658) 			 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4659) 			 * Also due to trylocks.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4660) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4661) 			curr_inner = min(curr_inner, prev_inner);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4662) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4663) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4664) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4665) 	if (next_outer > curr_inner)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4666) 		return print_lock_invalid_wait_context(curr, next);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4667) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4668) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4669) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4670) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4671) #else /* CONFIG_PROVE_LOCKING */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4672) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4673) static inline int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4674) mark_usage(struct task_struct *curr, struct held_lock *hlock, int check)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4675) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4676) 	return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4677) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4678) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4679) static inline unsigned int task_irq_context(struct task_struct *task)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4680) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4681) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4682) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4683) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4684) static inline int separate_irq_context(struct task_struct *curr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4685) 		struct held_lock *hlock)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4686) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4687) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4688) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4689) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4690) static inline int check_wait_context(struct task_struct *curr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4691) 				     struct held_lock *next)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4692) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4693) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4694) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4695) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4696) #endif /* CONFIG_PROVE_LOCKING */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4697) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4698) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4699)  * Initialize a lock instance's lock-class mapping info:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4700)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4701) void lockdep_init_map_type(struct lockdep_map *lock, const char *name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4702) 			    struct lock_class_key *key, int subclass,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4703) 			    u8 inner, u8 outer, u8 lock_type)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4704) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4705) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4706) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4707) 	for (i = 0; i < NR_LOCKDEP_CACHING_CLASSES; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4708) 		lock->class_cache[i] = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4709) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4710) #ifdef CONFIG_LOCK_STAT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4711) 	lock->cpu = raw_smp_processor_id();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4712) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4713) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4714) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4715) 	 * Can't be having no nameless bastards around this place!
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4716) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4717) 	if (DEBUG_LOCKS_WARN_ON(!name)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4718) 		lock->name = "NULL";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4719) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4720) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4721) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4722) 	lock->name = name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4723) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4724) 	lock->wait_type_outer = outer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4725) 	lock->wait_type_inner = inner;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4726) 	lock->lock_type = lock_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4727) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4728) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4729) 	 * No key, no joy, we need to hash something.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4730) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4731) 	if (DEBUG_LOCKS_WARN_ON(!key))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4732) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4733) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4734) 	 * Sanity check, the lock-class key must either have been allocated
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4735) 	 * statically or must have been registered as a dynamic key.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4736) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4737) 	if (!static_obj(key) && !is_dynamic_key(key)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4738) 		if (debug_locks)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4739) 			printk(KERN_ERR "BUG: key %px has not been registered!\n", key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4740) 		DEBUG_LOCKS_WARN_ON(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4741) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4742) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4743) 	lock->key = key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4744) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4745) 	if (unlikely(!debug_locks))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4746) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4747) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4748) 	if (subclass) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4749) 		unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4750) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4751) 		if (DEBUG_LOCKS_WARN_ON(!lockdep_enabled()))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4752) 			return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4753) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4754) 		raw_local_irq_save(flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4755) 		lockdep_recursion_inc();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4756) 		register_lock_class(lock, subclass, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4757) 		lockdep_recursion_finish();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4758) 		raw_local_irq_restore(flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4759) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4760) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4761) EXPORT_SYMBOL_GPL(lockdep_init_map_type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4762) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4763) struct lock_class_key __lockdep_no_validate__;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4764) EXPORT_SYMBOL_GPL(__lockdep_no_validate__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4765) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4766) static void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4767) print_lock_nested_lock_not_held(struct task_struct *curr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4768) 				struct held_lock *hlock,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4769) 				unsigned long ip)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4770) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4771) 	if (!debug_locks_off())
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4772) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4773) 	if (debug_locks_silent)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4774) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4775) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4776) 	pr_warn("\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4777) 	pr_warn("==================================\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4778) 	pr_warn("WARNING: Nested lock was not taken\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4779) 	print_kernel_ident();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4780) 	pr_warn("----------------------------------\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4781) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4782) 	pr_warn("%s/%d is trying to lock:\n", curr->comm, task_pid_nr(curr));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4783) 	print_lock(hlock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4784) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4785) 	pr_warn("\nbut this task is not holding:\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4786) 	pr_warn("%s\n", hlock->nest_lock->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4787) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4788) 	pr_warn("\nstack backtrace:\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4789) 	dump_stack();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4790) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4791) 	pr_warn("\nother info that might help us debug this:\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4792) 	lockdep_print_held_locks(curr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4793) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4794) 	pr_warn("\nstack backtrace:\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4795) 	dump_stack();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4796) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4797) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4798) static int __lock_is_held(const struct lockdep_map *lock, int read);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4799) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4800) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4801)  * This gets called for every mutex_lock*()/spin_lock*() operation.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4802)  * We maintain the dependency maps and validate the locking attempt:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4803)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4804)  * The callers must make sure that IRQs are disabled before calling it,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4805)  * otherwise we could get an interrupt which would want to take locks,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4806)  * which would end up in lockdep again.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4807)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4808) static int __lock_acquire(struct lockdep_map *lock, unsigned int subclass,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4809) 			  int trylock, int read, int check, int hardirqs_off,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4810) 			  struct lockdep_map *nest_lock, unsigned long ip,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4811) 			  int references, int pin_count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4812) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4813) 	struct task_struct *curr = current;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4814) 	struct lock_class *class = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4815) 	struct held_lock *hlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4816) 	unsigned int depth;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4817) 	int chain_head = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4818) 	int class_idx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4819) 	u64 chain_key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4820) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4821) 	if (unlikely(!debug_locks))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4822) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4823) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4824) 	if (!prove_locking || lock->key == &__lockdep_no_validate__)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4825) 		check = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4826) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4827) 	if (subclass < NR_LOCKDEP_CACHING_CLASSES)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4828) 		class = lock->class_cache[subclass];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4829) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4830) 	 * Not cached?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4831) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4832) 	if (unlikely(!class)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4833) 		class = register_lock_class(lock, subclass, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4834) 		if (!class)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4835) 			return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4836) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4837) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4838) 	debug_class_ops_inc(class);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4839) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4840) 	if (very_verbose(class)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4841) 		printk("\nacquire class [%px] %s", class->key, class->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4842) 		if (class->name_version > 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4843) 			printk(KERN_CONT "#%d", class->name_version);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4844) 		printk(KERN_CONT "\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4845) 		dump_stack();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4846) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4847) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4848) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4849) 	 * Add the lock to the list of currently held locks.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4850) 	 * (we dont increase the depth just yet, up until the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4851) 	 * dependency checks are done)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4852) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4853) 	depth = curr->lockdep_depth;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4854) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4855) 	 * Ran out of static storage for our per-task lock stack again have we?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4856) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4857) 	if (DEBUG_LOCKS_WARN_ON(depth >= MAX_LOCK_DEPTH))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4858) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4859) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4860) 	class_idx = class - lock_classes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4861) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4862) 	if (depth) { /* we're holding locks */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4863) 		hlock = curr->held_locks + depth - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4864) 		if (hlock->class_idx == class_idx && nest_lock) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4865) 			if (!references)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4866) 				references++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4867) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4868) 			if (!hlock->references)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4869) 				hlock->references++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4870) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4871) 			hlock->references += references;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4872) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4873) 			/* Overflow */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4874) 			if (DEBUG_LOCKS_WARN_ON(hlock->references < references))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4875) 				return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4876) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4877) 			return 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4878) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4879) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4880) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4881) 	hlock = curr->held_locks + depth;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4882) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4883) 	 * Plain impossible, we just registered it and checked it weren't no
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4884) 	 * NULL like.. I bet this mushroom I ate was good!
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4885) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4886) 	if (DEBUG_LOCKS_WARN_ON(!class))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4887) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4888) 	hlock->class_idx = class_idx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4889) 	hlock->acquire_ip = ip;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4890) 	hlock->instance = lock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4891) 	hlock->nest_lock = nest_lock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4892) 	hlock->irq_context = task_irq_context(curr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4893) 	hlock->trylock = trylock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4894) 	hlock->read = read;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4895) 	hlock->check = check;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4896) 	hlock->hardirqs_off = !!hardirqs_off;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4897) 	hlock->references = references;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4898) #ifdef CONFIG_LOCK_STAT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4899) 	hlock->waittime_stamp = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4900) 	hlock->holdtime_stamp = lockstat_clock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4901) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4902) 	hlock->pin_count = pin_count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4903) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4904) 	if (check_wait_context(curr, hlock))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4905) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4906) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4907) 	/* Initialize the lock usage bit */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4908) 	if (!mark_usage(curr, hlock, check))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4909) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4910) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4911) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4912) 	 * Calculate the chain hash: it's the combined hash of all the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4913) 	 * lock keys along the dependency chain. We save the hash value
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4914) 	 * at every step so that we can get the current hash easily
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4915) 	 * after unlock. The chain hash is then used to cache dependency
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4916) 	 * results.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4917) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4918) 	 * The 'key ID' is what is the most compact key value to drive
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4919) 	 * the hash, not class->key.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4920) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4921) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4922) 	 * Whoops, we did it again.. class_idx is invalid.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4923) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4924) 	if (DEBUG_LOCKS_WARN_ON(!test_bit(class_idx, lock_classes_in_use)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4925) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4926) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4927) 	chain_key = curr->curr_chain_key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4928) 	if (!depth) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4929) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4930) 		 * How can we have a chain hash when we ain't got no keys?!
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4931) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4932) 		if (DEBUG_LOCKS_WARN_ON(chain_key != INITIAL_CHAIN_KEY))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4933) 			return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4934) 		chain_head = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4935) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4936) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4937) 	hlock->prev_chain_key = chain_key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4938) 	if (separate_irq_context(curr, hlock)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4939) 		chain_key = INITIAL_CHAIN_KEY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4940) 		chain_head = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4941) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4942) 	chain_key = iterate_chain_key(chain_key, hlock_id(hlock));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4943) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4944) 	if (nest_lock && !__lock_is_held(nest_lock, -1)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4945) 		print_lock_nested_lock_not_held(curr, hlock, ip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4946) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4947) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4948) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4949) 	if (!debug_locks_silent) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4950) 		WARN_ON_ONCE(depth && !hlock_class(hlock - 1)->key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4951) 		WARN_ON_ONCE(!hlock_class(hlock)->key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4952) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4953) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4954) 	if (!validate_chain(curr, hlock, chain_head, chain_key))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4955) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4956) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4957) 	curr->curr_chain_key = chain_key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4958) 	curr->lockdep_depth++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4959) 	check_chain_key(curr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4960) #ifdef CONFIG_DEBUG_LOCKDEP
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4961) 	if (unlikely(!debug_locks))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4962) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4963) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4964) 	if (unlikely(curr->lockdep_depth >= MAX_LOCK_DEPTH)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4965) 		debug_locks_off();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4966) 		print_lockdep_off("BUG: MAX_LOCK_DEPTH too low!");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4967) 		printk(KERN_DEBUG "depth: %i  max: %lu!\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4968) 		       curr->lockdep_depth, MAX_LOCK_DEPTH);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4969) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4970) 		lockdep_print_held_locks(current);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4971) 		debug_show_all_locks();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4972) 		dump_stack();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4973) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4974) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4975) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4976) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4977) 	if (unlikely(curr->lockdep_depth > max_lockdep_depth))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4978) 		max_lockdep_depth = curr->lockdep_depth;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4979) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4980) 	return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4981) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4982) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4983) static void print_unlock_imbalance_bug(struct task_struct *curr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4984) 				       struct lockdep_map *lock,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4985) 				       unsigned long ip)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4986) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4987) 	if (!debug_locks_off())
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4988) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4989) 	if (debug_locks_silent)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4990) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4991) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4992) 	pr_warn("\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4993) 	pr_warn("=====================================\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4994) 	pr_warn("WARNING: bad unlock balance detected!\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4995) 	print_kernel_ident();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4996) 	pr_warn("-------------------------------------\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4997) 	pr_warn("%s/%d is trying to release lock (",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4998) 		curr->comm, task_pid_nr(curr));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4999) 	print_lockdep_cache(lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5000) 	pr_cont(") at:\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5001) 	print_ip_sym(KERN_WARNING, ip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5002) 	pr_warn("but there are no more locks to release!\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5003) 	pr_warn("\nother info that might help us debug this:\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5004) 	lockdep_print_held_locks(curr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5005) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5006) 	pr_warn("\nstack backtrace:\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5007) 	dump_stack();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5008) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5009) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5010) static noinstr int match_held_lock(const struct held_lock *hlock,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5011) 				   const struct lockdep_map *lock)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5012) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5013) 	if (hlock->instance == lock)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5014) 		return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5015) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5016) 	if (hlock->references) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5017) 		const struct lock_class *class = lock->class_cache[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5018) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5019) 		if (!class)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5020) 			class = look_up_lock_class(lock, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5021) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5022) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5023) 		 * If look_up_lock_class() failed to find a class, we're trying
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5024) 		 * to test if we hold a lock that has never yet been acquired.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5025) 		 * Clearly if the lock hasn't been acquired _ever_, we're not
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5026) 		 * holding it either, so report failure.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5027) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5028) 		if (!class)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5029) 			return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5030) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5031) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5032) 		 * References, but not a lock we're actually ref-counting?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5033) 		 * State got messed up, follow the sites that change ->references
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5034) 		 * and try to make sense of it.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5035) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5036) 		if (DEBUG_LOCKS_WARN_ON(!hlock->nest_lock))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5037) 			return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5038) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5039) 		if (hlock->class_idx == class - lock_classes)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5040) 			return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5041) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5042) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5043) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5044) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5045) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5046) /* @depth must not be zero */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5047) static struct held_lock *find_held_lock(struct task_struct *curr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5048) 					struct lockdep_map *lock,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5049) 					unsigned int depth, int *idx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5050) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5051) 	struct held_lock *ret, *hlock, *prev_hlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5052) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5053) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5054) 	i = depth - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5055) 	hlock = curr->held_locks + i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5056) 	ret = hlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5057) 	if (match_held_lock(hlock, lock))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5058) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5059) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5060) 	ret = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5061) 	for (i--, prev_hlock = hlock--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5062) 	     i >= 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5063) 	     i--, prev_hlock = hlock--) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5064) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5065) 		 * We must not cross into another context:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5066) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5067) 		if (prev_hlock->irq_context != hlock->irq_context) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5068) 			ret = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5069) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5070) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5071) 		if (match_held_lock(hlock, lock)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5072) 			ret = hlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5073) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5074) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5075) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5076) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5077) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5078) 	*idx = i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5079) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5080) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5081) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5082) static int reacquire_held_locks(struct task_struct *curr, unsigned int depth,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5083) 				int idx, unsigned int *merged)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5084) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5085) 	struct held_lock *hlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5086) 	int first_idx = idx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5087) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5088) 	if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5089) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5090) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5091) 	for (hlock = curr->held_locks + idx; idx < depth; idx++, hlock++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5092) 		switch (__lock_acquire(hlock->instance,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5093) 				    hlock_class(hlock)->subclass,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5094) 				    hlock->trylock,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5095) 				    hlock->read, hlock->check,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5096) 				    hlock->hardirqs_off,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5097) 				    hlock->nest_lock, hlock->acquire_ip,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5098) 				    hlock->references, hlock->pin_count)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5099) 		case 0:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5100) 			return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5101) 		case 1:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5102) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5103) 		case 2:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5104) 			*merged += (idx == first_idx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5105) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5106) 		default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5107) 			WARN_ON(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5108) 			return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5109) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5110) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5111) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5112) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5113) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5114) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5115) __lock_set_class(struct lockdep_map *lock, const char *name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5116) 		 struct lock_class_key *key, unsigned int subclass,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5117) 		 unsigned long ip)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5118) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5119) 	struct task_struct *curr = current;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5120) 	unsigned int depth, merged = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5121) 	struct held_lock *hlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5122) 	struct lock_class *class;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5123) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5124) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5125) 	if (unlikely(!debug_locks))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5126) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5127) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5128) 	depth = curr->lockdep_depth;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5129) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5130) 	 * This function is about (re)setting the class of a held lock,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5131) 	 * yet we're not actually holding any locks. Naughty user!
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5132) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5133) 	if (DEBUG_LOCKS_WARN_ON(!depth))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5134) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5135) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5136) 	hlock = find_held_lock(curr, lock, depth, &i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5137) 	if (!hlock) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5138) 		print_unlock_imbalance_bug(curr, lock, ip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5139) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5140) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5141) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5142) 	lockdep_init_map_waits(lock, name, key, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5143) 			       lock->wait_type_inner,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5144) 			       lock->wait_type_outer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5145) 	class = register_lock_class(lock, subclass, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5146) 	hlock->class_idx = class - lock_classes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5147) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5148) 	curr->lockdep_depth = i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5149) 	curr->curr_chain_key = hlock->prev_chain_key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5150) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5151) 	if (reacquire_held_locks(curr, depth, i, &merged))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5152) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5153) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5154) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5155) 	 * I took it apart and put it back together again, except now I have
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5156) 	 * these 'spare' parts.. where shall I put them.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5157) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5158) 	if (DEBUG_LOCKS_WARN_ON(curr->lockdep_depth != depth - merged))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5159) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5160) 	return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5161) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5162) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5163) static int __lock_downgrade(struct lockdep_map *lock, unsigned long ip)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5164) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5165) 	struct task_struct *curr = current;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5166) 	unsigned int depth, merged = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5167) 	struct held_lock *hlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5168) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5169) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5170) 	if (unlikely(!debug_locks))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5171) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5172) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5173) 	depth = curr->lockdep_depth;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5174) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5175) 	 * This function is about (re)setting the class of a held lock,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5176) 	 * yet we're not actually holding any locks. Naughty user!
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5177) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5178) 	if (DEBUG_LOCKS_WARN_ON(!depth))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5179) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5180) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5181) 	hlock = find_held_lock(curr, lock, depth, &i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5182) 	if (!hlock) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5183) 		print_unlock_imbalance_bug(curr, lock, ip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5184) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5185) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5186) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5187) 	curr->lockdep_depth = i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5188) 	curr->curr_chain_key = hlock->prev_chain_key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5189) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5190) 	WARN(hlock->read, "downgrading a read lock");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5191) 	hlock->read = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5192) 	hlock->acquire_ip = ip;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5193) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5194) 	if (reacquire_held_locks(curr, depth, i, &merged))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5195) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5196) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5197) 	/* Merging can't happen with unchanged classes.. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5198) 	if (DEBUG_LOCKS_WARN_ON(merged))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5199) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5200) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5201) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5202) 	 * I took it apart and put it back together again, except now I have
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5203) 	 * these 'spare' parts.. where shall I put them.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5204) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5205) 	if (DEBUG_LOCKS_WARN_ON(curr->lockdep_depth != depth))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5206) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5207) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5208) 	return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5209) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5210) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5211) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5212)  * Remove the lock from the list of currently held locks - this gets
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5213)  * called on mutex_unlock()/spin_unlock*() (or on a failed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5214)  * mutex_lock_interruptible()).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5215)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5216) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5217) __lock_release(struct lockdep_map *lock, unsigned long ip)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5218) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5219) 	struct task_struct *curr = current;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5220) 	unsigned int depth, merged = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5221) 	struct held_lock *hlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5222) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5223) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5224) 	if (unlikely(!debug_locks))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5225) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5226) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5227) 	depth = curr->lockdep_depth;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5228) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5229) 	 * So we're all set to release this lock.. wait what lock? We don't
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5230) 	 * own any locks, you've been drinking again?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5231) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5232) 	if (depth <= 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5233) 		print_unlock_imbalance_bug(curr, lock, ip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5234) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5235) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5236) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5237) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5238) 	 * Check whether the lock exists in the current stack
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5239) 	 * of held locks:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5240) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5241) 	hlock = find_held_lock(curr, lock, depth, &i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5242) 	if (!hlock) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5243) 		print_unlock_imbalance_bug(curr, lock, ip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5244) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5245) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5246) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5247) 	if (hlock->instance == lock)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5248) 		lock_release_holdtime(hlock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5249) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5250) 	WARN(hlock->pin_count, "releasing a pinned lock\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5251) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5252) 	if (hlock->references) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5253) 		hlock->references--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5254) 		if (hlock->references) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5255) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5256) 			 * We had, and after removing one, still have
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5257) 			 * references, the current lock stack is still
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5258) 			 * valid. We're done!
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5259) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5260) 			return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5261) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5262) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5263) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5264) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5265) 	 * We have the right lock to unlock, 'hlock' points to it.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5266) 	 * Now we remove it from the stack, and add back the other
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5267) 	 * entries (if any), recalculating the hash along the way:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5268) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5269) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5270) 	curr->lockdep_depth = i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5271) 	curr->curr_chain_key = hlock->prev_chain_key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5272) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5273) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5274) 	 * The most likely case is when the unlock is on the innermost
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5275) 	 * lock. In this case, we are done!
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5276) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5277) 	if (i == depth-1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5278) 		return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5279) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5280) 	if (reacquire_held_locks(curr, depth, i + 1, &merged))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5281) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5282) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5283) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5284) 	 * We had N bottles of beer on the wall, we drank one, but now
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5285) 	 * there's not N-1 bottles of beer left on the wall...
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5286) 	 * Pouring two of the bottles together is acceptable.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5287) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5288) 	DEBUG_LOCKS_WARN_ON(curr->lockdep_depth != depth - merged);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5289) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5290) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5291) 	 * Since reacquire_held_locks() would have called check_chain_key()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5292) 	 * indirectly via __lock_acquire(), we don't need to do it again
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5293) 	 * on return.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5294) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5295) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5296) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5297) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5298) static __always_inline
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5299) int __lock_is_held(const struct lockdep_map *lock, int read)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5300) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5301) 	struct task_struct *curr = current;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5302) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5303) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5304) 	for (i = 0; i < curr->lockdep_depth; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5305) 		struct held_lock *hlock = curr->held_locks + i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5306) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5307) 		if (match_held_lock(hlock, lock)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5308) 			if (read == -1 || !!hlock->read == read)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5309) 				return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5310) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5311) 			return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5312) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5313) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5314) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5315) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5316) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5317) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5318) static struct pin_cookie __lock_pin_lock(struct lockdep_map *lock)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5319) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5320) 	struct pin_cookie cookie = NIL_COOKIE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5321) 	struct task_struct *curr = current;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5322) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5323) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5324) 	if (unlikely(!debug_locks))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5325) 		return cookie;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5326) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5327) 	for (i = 0; i < curr->lockdep_depth; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5328) 		struct held_lock *hlock = curr->held_locks + i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5329) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5330) 		if (match_held_lock(hlock, lock)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5331) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5332) 			 * Grab 16bits of randomness; this is sufficient to not
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5333) 			 * be guessable and still allows some pin nesting in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5334) 			 * our u32 pin_count.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5335) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5336) 			cookie.val = 1 + (prandom_u32() >> 16);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5337) 			hlock->pin_count += cookie.val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5338) 			return cookie;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5339) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5340) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5341) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5342) 	WARN(1, "pinning an unheld lock\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5343) 	return cookie;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5344) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5345) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5346) static void __lock_repin_lock(struct lockdep_map *lock, struct pin_cookie cookie)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5347) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5348) 	struct task_struct *curr = current;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5349) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5350) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5351) 	if (unlikely(!debug_locks))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5352) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5353) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5354) 	for (i = 0; i < curr->lockdep_depth; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5355) 		struct held_lock *hlock = curr->held_locks + i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5356) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5357) 		if (match_held_lock(hlock, lock)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5358) 			hlock->pin_count += cookie.val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5359) 			return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5360) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5361) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5362) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5363) 	WARN(1, "pinning an unheld lock\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5364) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5365) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5366) static void __lock_unpin_lock(struct lockdep_map *lock, struct pin_cookie cookie)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5367) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5368) 	struct task_struct *curr = current;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5369) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5370) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5371) 	if (unlikely(!debug_locks))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5372) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5373) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5374) 	for (i = 0; i < curr->lockdep_depth; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5375) 		struct held_lock *hlock = curr->held_locks + i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5376) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5377) 		if (match_held_lock(hlock, lock)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5378) 			if (WARN(!hlock->pin_count, "unpinning an unpinned lock\n"))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5379) 				return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5380) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5381) 			hlock->pin_count -= cookie.val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5382) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5383) 			if (WARN((int)hlock->pin_count < 0, "pin count corrupted\n"))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5384) 				hlock->pin_count = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5385) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5386) 			return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5387) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5388) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5389) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5390) 	WARN(1, "unpinning an unheld lock\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5391) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5392) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5393) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5394)  * Check whether we follow the irq-flags state precisely:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5395)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5396) static noinstr void check_flags(unsigned long flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5397) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5398) #if defined(CONFIG_PROVE_LOCKING) && defined(CONFIG_DEBUG_LOCKDEP)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5399) 	if (!debug_locks)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5400) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5401) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5402) 	/* Get the warning out..  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5403) 	instrumentation_begin();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5404) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5405) 	if (irqs_disabled_flags(flags)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5406) 		if (DEBUG_LOCKS_WARN_ON(lockdep_hardirqs_enabled())) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5407) 			printk("possible reason: unannotated irqs-off.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5408) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5409) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5410) 		if (DEBUG_LOCKS_WARN_ON(!lockdep_hardirqs_enabled())) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5411) 			printk("possible reason: unannotated irqs-on.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5412) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5413) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5414) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5415) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5416) 	 * We dont accurately track softirq state in e.g.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5417) 	 * hardirq contexts (such as on 4KSTACKS), so only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5418) 	 * check if not in hardirq contexts:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5419) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5420) 	if (!hardirq_count()) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5421) 		if (softirq_count()) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5422) 			/* like the above, but with softirqs */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5423) 			DEBUG_LOCKS_WARN_ON(current->softirqs_enabled);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5424) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5425) 			/* lick the above, does it taste good? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5426) 			DEBUG_LOCKS_WARN_ON(!current->softirqs_enabled);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5427) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5428) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5429) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5430) 	if (!debug_locks)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5431) 		print_irqtrace_events(current);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5432) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5433) 	instrumentation_end();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5434) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5435) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5436) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5437) void lock_set_class(struct lockdep_map *lock, const char *name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5438) 		    struct lock_class_key *key, unsigned int subclass,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5439) 		    unsigned long ip)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5440) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5441) 	unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5442) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5443) 	if (unlikely(!lockdep_enabled()))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5444) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5445) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5446) 	raw_local_irq_save(flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5447) 	lockdep_recursion_inc();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5448) 	check_flags(flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5449) 	if (__lock_set_class(lock, name, key, subclass, ip))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5450) 		check_chain_key(current);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5451) 	lockdep_recursion_finish();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5452) 	raw_local_irq_restore(flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5453) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5454) EXPORT_SYMBOL_GPL(lock_set_class);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5455) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5456) void lock_downgrade(struct lockdep_map *lock, unsigned long ip)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5457) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5458) 	unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5459) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5460) 	if (unlikely(!lockdep_enabled()))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5461) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5462) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5463) 	raw_local_irq_save(flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5464) 	lockdep_recursion_inc();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5465) 	check_flags(flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5466) 	if (__lock_downgrade(lock, ip))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5467) 		check_chain_key(current);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5468) 	lockdep_recursion_finish();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5469) 	raw_local_irq_restore(flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5470) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5471) EXPORT_SYMBOL_GPL(lock_downgrade);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5472) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5473) /* NMI context !!! */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5474) static void verify_lock_unused(struct lockdep_map *lock, struct held_lock *hlock, int subclass)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5475) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5476) #ifdef CONFIG_PROVE_LOCKING
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5477) 	struct lock_class *class = look_up_lock_class(lock, subclass);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5478) 	unsigned long mask = LOCKF_USED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5479) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5480) 	/* if it doesn't have a class (yet), it certainly hasn't been used yet */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5481) 	if (!class)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5482) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5483) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5484) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5485) 	 * READ locks only conflict with USED, such that if we only ever use
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5486) 	 * READ locks, there is no deadlock possible -- RCU.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5487) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5488) 	if (!hlock->read)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5489) 		mask |= LOCKF_USED_READ;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5490) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5491) 	if (!(class->usage_mask & mask))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5492) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5493) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5494) 	hlock->class_idx = class - lock_classes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5495) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5496) 	print_usage_bug(current, hlock, LOCK_USED, LOCK_USAGE_STATES);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5497) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5498) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5499) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5500) static bool lockdep_nmi(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5501) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5502) 	if (raw_cpu_read(lockdep_recursion))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5503) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5504) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5505) 	if (!in_nmi())
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5506) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5507) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5508) 	return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5509) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5510) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5511) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5512)  * read_lock() is recursive if:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5513)  * 1. We force lockdep think this way in selftests or
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5514)  * 2. The implementation is not queued read/write lock or
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5515)  * 3. The locker is at an in_interrupt() context.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5516)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5517) bool read_lock_is_recursive(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5518) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5519) 	return force_read_lock_recursive ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5520) 	       !IS_ENABLED(CONFIG_QUEUED_RWLOCKS) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5521) 	       in_interrupt();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5522) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5523) EXPORT_SYMBOL_GPL(read_lock_is_recursive);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5524) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5525) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5526)  * We are not always called with irqs disabled - do that here,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5527)  * and also avoid lockdep recursion:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5528)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5529) void lock_acquire(struct lockdep_map *lock, unsigned int subclass,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5530) 			  int trylock, int read, int check,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5531) 			  struct lockdep_map *nest_lock, unsigned long ip)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5532) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5533) 	unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5534) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5535) 	trace_lock_acquire(lock, subclass, trylock, read, check, nest_lock, ip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5536) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5537) 	if (!debug_locks)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5538) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5539) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5540) 	if (unlikely(!lockdep_enabled())) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5541) 		/* XXX allow trylock from NMI ?!? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5542) 		if (lockdep_nmi() && !trylock) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5543) 			struct held_lock hlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5544) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5545) 			hlock.acquire_ip = ip;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5546) 			hlock.instance = lock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5547) 			hlock.nest_lock = nest_lock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5548) 			hlock.irq_context = 2; // XXX
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5549) 			hlock.trylock = trylock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5550) 			hlock.read = read;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5551) 			hlock.check = check;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5552) 			hlock.hardirqs_off = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5553) 			hlock.references = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5554) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5555) 			verify_lock_unused(lock, &hlock, subclass);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5556) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5557) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5558) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5559) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5560) 	raw_local_irq_save(flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5561) 	check_flags(flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5562) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5563) 	lockdep_recursion_inc();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5564) 	__lock_acquire(lock, subclass, trylock, read, check,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5565) 		       irqs_disabled_flags(flags), nest_lock, ip, 0, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5566) 	lockdep_recursion_finish();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5567) 	raw_local_irq_restore(flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5568) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5569) EXPORT_SYMBOL_GPL(lock_acquire);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5570) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5571) void lock_release(struct lockdep_map *lock, unsigned long ip)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5572) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5573) 	unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5574) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5575) 	trace_lock_release(lock, ip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5576) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5577) 	if (unlikely(!lockdep_enabled()))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5578) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5579) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5580) 	raw_local_irq_save(flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5581) 	check_flags(flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5582) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5583) 	lockdep_recursion_inc();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5584) 	if (__lock_release(lock, ip))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5585) 		check_chain_key(current);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5586) 	lockdep_recursion_finish();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5587) 	raw_local_irq_restore(flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5588) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5589) EXPORT_SYMBOL_GPL(lock_release);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5590) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5591) noinstr int lock_is_held_type(const struct lockdep_map *lock, int read)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5592) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5593) 	unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5594) 	int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5595) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5596) 	if (unlikely(!lockdep_enabled()))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5597) 		return 1; /* avoid false negative lockdep_assert_held() */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5598) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5599) 	raw_local_irq_save(flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5600) 	check_flags(flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5601) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5602) 	lockdep_recursion_inc();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5603) 	ret = __lock_is_held(lock, read);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5604) 	lockdep_recursion_finish();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5605) 	raw_local_irq_restore(flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5606) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5607) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5608) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5609) EXPORT_SYMBOL_GPL(lock_is_held_type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5610) NOKPROBE_SYMBOL(lock_is_held_type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5611) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5612) struct pin_cookie lock_pin_lock(struct lockdep_map *lock)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5613) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5614) 	struct pin_cookie cookie = NIL_COOKIE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5615) 	unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5616) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5617) 	if (unlikely(!lockdep_enabled()))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5618) 		return cookie;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5619) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5620) 	raw_local_irq_save(flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5621) 	check_flags(flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5622) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5623) 	lockdep_recursion_inc();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5624) 	cookie = __lock_pin_lock(lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5625) 	lockdep_recursion_finish();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5626) 	raw_local_irq_restore(flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5627) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5628) 	return cookie;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5629) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5630) EXPORT_SYMBOL_GPL(lock_pin_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5631) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5632) void lock_repin_lock(struct lockdep_map *lock, struct pin_cookie cookie)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5633) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5634) 	unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5635) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5636) 	if (unlikely(!lockdep_enabled()))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5637) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5638) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5639) 	raw_local_irq_save(flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5640) 	check_flags(flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5641) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5642) 	lockdep_recursion_inc();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5643) 	__lock_repin_lock(lock, cookie);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5644) 	lockdep_recursion_finish();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5645) 	raw_local_irq_restore(flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5646) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5647) EXPORT_SYMBOL_GPL(lock_repin_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5648) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5649) void lock_unpin_lock(struct lockdep_map *lock, struct pin_cookie cookie)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5650) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5651) 	unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5652) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5653) 	if (unlikely(!lockdep_enabled()))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5654) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5655) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5656) 	raw_local_irq_save(flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5657) 	check_flags(flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5658) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5659) 	lockdep_recursion_inc();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5660) 	__lock_unpin_lock(lock, cookie);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5661) 	lockdep_recursion_finish();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5662) 	raw_local_irq_restore(flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5663) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5664) EXPORT_SYMBOL_GPL(lock_unpin_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5665) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5666) #ifdef CONFIG_LOCK_STAT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5667) static void print_lock_contention_bug(struct task_struct *curr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5668) 				      struct lockdep_map *lock,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5669) 				      unsigned long ip)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5670) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5671) 	if (!debug_locks_off())
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5672) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5673) 	if (debug_locks_silent)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5674) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5675) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5676) 	pr_warn("\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5677) 	pr_warn("=================================\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5678) 	pr_warn("WARNING: bad contention detected!\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5679) 	print_kernel_ident();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5680) 	pr_warn("---------------------------------\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5681) 	pr_warn("%s/%d is trying to contend lock (",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5682) 		curr->comm, task_pid_nr(curr));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5683) 	print_lockdep_cache(lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5684) 	pr_cont(") at:\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5685) 	print_ip_sym(KERN_WARNING, ip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5686) 	pr_warn("but there are no locks held!\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5687) 	pr_warn("\nother info that might help us debug this:\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5688) 	lockdep_print_held_locks(curr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5689) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5690) 	pr_warn("\nstack backtrace:\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5691) 	dump_stack();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5692) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5693) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5694) static void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5695) __lock_contended(struct lockdep_map *lock, unsigned long ip)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5696) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5697) 	struct task_struct *curr = current;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5698) 	struct held_lock *hlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5699) 	struct lock_class_stats *stats;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5700) 	unsigned int depth;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5701) 	int i, contention_point, contending_point;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5702) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5703) 	depth = curr->lockdep_depth;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5704) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5705) 	 * Whee, we contended on this lock, except it seems we're not
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5706) 	 * actually trying to acquire anything much at all..
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5707) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5708) 	if (DEBUG_LOCKS_WARN_ON(!depth))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5709) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5710) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5711) 	hlock = find_held_lock(curr, lock, depth, &i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5712) 	if (!hlock) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5713) 		print_lock_contention_bug(curr, lock, ip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5714) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5715) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5716) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5717) 	if (hlock->instance != lock)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5718) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5719) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5720) 	hlock->waittime_stamp = lockstat_clock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5721) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5722) 	contention_point = lock_point(hlock_class(hlock)->contention_point, ip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5723) 	contending_point = lock_point(hlock_class(hlock)->contending_point,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5724) 				      lock->ip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5725) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5726) 	stats = get_lock_stats(hlock_class(hlock));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5727) 	if (contention_point < LOCKSTAT_POINTS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5728) 		stats->contention_point[contention_point]++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5729) 	if (contending_point < LOCKSTAT_POINTS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5730) 		stats->contending_point[contending_point]++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5731) 	if (lock->cpu != smp_processor_id())
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5732) 		stats->bounces[bounce_contended + !!hlock->read]++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5733) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5734) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5735) static void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5736) __lock_acquired(struct lockdep_map *lock, unsigned long ip)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5737) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5738) 	struct task_struct *curr = current;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5739) 	struct held_lock *hlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5740) 	struct lock_class_stats *stats;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5741) 	unsigned int depth;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5742) 	u64 now, waittime = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5743) 	int i, cpu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5744) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5745) 	depth = curr->lockdep_depth;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5746) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5747) 	 * Yay, we acquired ownership of this lock we didn't try to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5748) 	 * acquire, how the heck did that happen?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5749) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5750) 	if (DEBUG_LOCKS_WARN_ON(!depth))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5751) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5752) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5753) 	hlock = find_held_lock(curr, lock, depth, &i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5754) 	if (!hlock) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5755) 		print_lock_contention_bug(curr, lock, _RET_IP_);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5756) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5757) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5758) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5759) 	if (hlock->instance != lock)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5760) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5761) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5762) 	cpu = smp_processor_id();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5763) 	if (hlock->waittime_stamp) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5764) 		now = lockstat_clock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5765) 		waittime = now - hlock->waittime_stamp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5766) 		hlock->holdtime_stamp = now;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5767) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5768) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5769) 	stats = get_lock_stats(hlock_class(hlock));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5770) 	if (waittime) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5771) 		if (hlock->read)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5772) 			lock_time_inc(&stats->read_waittime, waittime);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5773) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5774) 			lock_time_inc(&stats->write_waittime, waittime);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5775) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5776) 	if (lock->cpu != cpu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5777) 		stats->bounces[bounce_acquired + !!hlock->read]++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5778) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5779) 	lock->cpu = cpu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5780) 	lock->ip = ip;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5781) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5782) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5783) void lock_contended(struct lockdep_map *lock, unsigned long ip)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5784) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5785) 	unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5786) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5787) 	trace_lock_contended(lock, ip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5788) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5789) 	if (unlikely(!lock_stat || !lockdep_enabled()))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5790) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5791) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5792) 	raw_local_irq_save(flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5793) 	check_flags(flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5794) 	lockdep_recursion_inc();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5795) 	__lock_contended(lock, ip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5796) 	lockdep_recursion_finish();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5797) 	raw_local_irq_restore(flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5798) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5799) EXPORT_SYMBOL_GPL(lock_contended);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5800) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5801) void lock_acquired(struct lockdep_map *lock, unsigned long ip)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5802) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5803) 	unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5804) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5805) 	trace_lock_acquired(lock, ip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5806) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5807) 	if (unlikely(!lock_stat || !lockdep_enabled()))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5808) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5809) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5810) 	raw_local_irq_save(flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5811) 	check_flags(flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5812) 	lockdep_recursion_inc();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5813) 	__lock_acquired(lock, ip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5814) 	lockdep_recursion_finish();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5815) 	raw_local_irq_restore(flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5816) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5817) EXPORT_SYMBOL_GPL(lock_acquired);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5818) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5819) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5820) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5821)  * Used by the testsuite, sanitize the validator state
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5822)  * after a simulated failure:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5823)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5824) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5825) void lockdep_reset(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5826) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5827) 	unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5828) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5829) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5830) 	raw_local_irq_save(flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5831) 	lockdep_init_task(current);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5832) 	memset(current->held_locks, 0, MAX_LOCK_DEPTH*sizeof(struct held_lock));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5833) 	nr_hardirq_chains = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5834) 	nr_softirq_chains = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5835) 	nr_process_chains = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5836) 	debug_locks = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5837) 	for (i = 0; i < CHAINHASH_SIZE; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5838) 		INIT_HLIST_HEAD(chainhash_table + i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5839) 	raw_local_irq_restore(flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5840) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5841) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5842) /* Remove a class from a lock chain. Must be called with the graph lock held. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5843) static void remove_class_from_lock_chain(struct pending_free *pf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5844) 					 struct lock_chain *chain,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5845) 					 struct lock_class *class)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5846) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5847) #ifdef CONFIG_PROVE_LOCKING
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5848) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5849) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5850) 	for (i = chain->base; i < chain->base + chain->depth; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5851) 		if (chain_hlock_class_idx(chain_hlocks[i]) != class - lock_classes)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5852) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5853) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5854) 		 * Each lock class occurs at most once in a lock chain so once
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5855) 		 * we found a match we can break out of this loop.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5856) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5857) 		goto free_lock_chain;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5858) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5859) 	/* Since the chain has not been modified, return. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5860) 	return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5861) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5862) free_lock_chain:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5863) 	free_chain_hlocks(chain->base, chain->depth);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5864) 	/* Overwrite the chain key for concurrent RCU readers. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5865) 	WRITE_ONCE(chain->chain_key, INITIAL_CHAIN_KEY);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5866) 	dec_chains(chain->irq_context);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5867) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5868) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5869) 	 * Note: calling hlist_del_rcu() from inside a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5870) 	 * hlist_for_each_entry_rcu() loop is safe.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5871) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5872) 	hlist_del_rcu(&chain->entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5873) 	__set_bit(chain - lock_chains, pf->lock_chains_being_freed);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5874) 	nr_zapped_lock_chains++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5875) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5876) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5877) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5878) /* Must be called with the graph lock held. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5879) static void remove_class_from_lock_chains(struct pending_free *pf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5880) 					  struct lock_class *class)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5881) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5882) 	struct lock_chain *chain;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5883) 	struct hlist_head *head;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5884) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5885) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5886) 	for (i = 0; i < ARRAY_SIZE(chainhash_table); i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5887) 		head = chainhash_table + i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5888) 		hlist_for_each_entry_rcu(chain, head, entry) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5889) 			remove_class_from_lock_chain(pf, chain, class);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5890) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5891) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5892) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5893) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5894) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5895)  * Remove all references to a lock class. The caller must hold the graph lock.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5896)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5897) static void zap_class(struct pending_free *pf, struct lock_class *class)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5898) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5899) 	struct lock_list *entry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5900) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5901) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5902) 	WARN_ON_ONCE(!class->key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5903) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5904) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5905) 	 * Remove all dependencies this lock is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5906) 	 * involved in:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5907) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5908) 	for_each_set_bit(i, list_entries_in_use, ARRAY_SIZE(list_entries)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5909) 		entry = list_entries + i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5910) 		if (entry->class != class && entry->links_to != class)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5911) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5912) 		__clear_bit(i, list_entries_in_use);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5913) 		nr_list_entries--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5914) 		list_del_rcu(&entry->entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5915) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5916) 	if (list_empty(&class->locks_after) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5917) 	    list_empty(&class->locks_before)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5918) 		list_move_tail(&class->lock_entry, &pf->zapped);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5919) 		hlist_del_rcu(&class->hash_entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5920) 		WRITE_ONCE(class->key, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5921) 		WRITE_ONCE(class->name, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5922) 		nr_lock_classes--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5923) 		__clear_bit(class - lock_classes, lock_classes_in_use);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5924) 		if (class - lock_classes == max_lock_class_idx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5925) 			max_lock_class_idx--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5926) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5927) 		WARN_ONCE(true, "%s() failed for class %s\n", __func__,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5928) 			  class->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5929) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5930) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5931) 	remove_class_from_lock_chains(pf, class);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5932) 	nr_zapped_classes++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5933) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5934) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5935) static void reinit_class(struct lock_class *class)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5936) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5937) 	void *const p = class;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5938) 	const unsigned int offset = offsetof(struct lock_class, key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5939) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5940) 	WARN_ON_ONCE(!class->lock_entry.next);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5941) 	WARN_ON_ONCE(!list_empty(&class->locks_after));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5942) 	WARN_ON_ONCE(!list_empty(&class->locks_before));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5943) 	memset(p + offset, 0, sizeof(*class) - offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5944) 	WARN_ON_ONCE(!class->lock_entry.next);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5945) 	WARN_ON_ONCE(!list_empty(&class->locks_after));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5946) 	WARN_ON_ONCE(!list_empty(&class->locks_before));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5947) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5948) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5949) static inline int within(const void *addr, void *start, unsigned long size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5950) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5951) 	return addr >= start && addr < start + size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5952) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5953) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5954) static bool inside_selftest(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5955) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5956) 	return current == lockdep_selftest_task_struct;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5957) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5958) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5959) /* The caller must hold the graph lock. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5960) static struct pending_free *get_pending_free(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5961) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5962) 	return delayed_free.pf + delayed_free.index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5963) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5964) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5965) static void free_zapped_rcu(struct rcu_head *cb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5966) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5967) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5968)  * Schedule an RCU callback if no RCU callback is pending. Must be called with
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5969)  * the graph lock held.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5970)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5971) static void call_rcu_zapped(struct pending_free *pf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5972) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5973) 	WARN_ON_ONCE(inside_selftest());
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5974) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5975) 	if (list_empty(&pf->zapped))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5976) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5977) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5978) 	if (delayed_free.scheduled)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5979) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5980) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5981) 	delayed_free.scheduled = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5982) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5983) 	WARN_ON_ONCE(delayed_free.pf + delayed_free.index != pf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5984) 	delayed_free.index ^= 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5985) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5986) 	call_rcu(&delayed_free.rcu_head, free_zapped_rcu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5987) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5988) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5989) /* The caller must hold the graph lock. May be called from RCU context. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5990) static void __free_zapped_classes(struct pending_free *pf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5991) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5992) 	struct lock_class *class;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5993) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5994) 	check_data_structures();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5995) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5996) 	list_for_each_entry(class, &pf->zapped, lock_entry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5997) 		reinit_class(class);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5998) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5999) 	list_splice_init(&pf->zapped, &free_lock_classes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6000) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6001) #ifdef CONFIG_PROVE_LOCKING
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6002) 	bitmap_andnot(lock_chains_in_use, lock_chains_in_use,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6003) 		      pf->lock_chains_being_freed, ARRAY_SIZE(lock_chains));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6004) 	bitmap_clear(pf->lock_chains_being_freed, 0, ARRAY_SIZE(lock_chains));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6005) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6006) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6007) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6008) static void free_zapped_rcu(struct rcu_head *ch)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6009) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6010) 	struct pending_free *pf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6011) 	unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6012) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6013) 	if (WARN_ON_ONCE(ch != &delayed_free.rcu_head))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6014) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6015) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6016) 	raw_local_irq_save(flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6017) 	lockdep_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6018) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6019) 	/* closed head */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6020) 	pf = delayed_free.pf + (delayed_free.index ^ 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6021) 	__free_zapped_classes(pf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6022) 	delayed_free.scheduled = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6023) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6024) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6025) 	 * If there's anything on the open list, close and start a new callback.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6026) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6027) 	call_rcu_zapped(delayed_free.pf + delayed_free.index);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6028) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6029) 	lockdep_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6030) 	raw_local_irq_restore(flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6031) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6032) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6033) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6034)  * Remove all lock classes from the class hash table and from the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6035)  * all_lock_classes list whose key or name is in the address range [start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6036)  * start + size). Move these lock classes to the zapped_classes list. Must
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6037)  * be called with the graph lock held.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6038)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6039) static void __lockdep_free_key_range(struct pending_free *pf, void *start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6040) 				     unsigned long size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6041) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6042) 	struct lock_class *class;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6043) 	struct hlist_head *head;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6044) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6045) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6046) 	/* Unhash all classes that were created by a module. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6047) 	for (i = 0; i < CLASSHASH_SIZE; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6048) 		head = classhash_table + i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6049) 		hlist_for_each_entry_rcu(class, head, hash_entry) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6050) 			if (!within(class->key, start, size) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6051) 			    !within(class->name, start, size))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6052) 				continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6053) 			zap_class(pf, class);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6054) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6055) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6056) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6057) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6058) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6059)  * Used in module.c to remove lock classes from memory that is going to be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6060)  * freed; and possibly re-used by other modules.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6061)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6062)  * We will have had one synchronize_rcu() before getting here, so we're
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6063)  * guaranteed nobody will look up these exact classes -- they're properly dead
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6064)  * but still allocated.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6065)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6066) static void lockdep_free_key_range_reg(void *start, unsigned long size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6067) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6068) 	struct pending_free *pf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6069) 	unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6070) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6071) 	init_data_structures_once();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6072) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6073) 	raw_local_irq_save(flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6074) 	lockdep_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6075) 	pf = get_pending_free();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6076) 	__lockdep_free_key_range(pf, start, size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6077) 	call_rcu_zapped(pf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6078) 	lockdep_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6079) 	raw_local_irq_restore(flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6080) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6081) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6082) 	 * Wait for any possible iterators from look_up_lock_class() to pass
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6083) 	 * before continuing to free the memory they refer to.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6084) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6085) 	synchronize_rcu();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6086) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6087) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6088) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6089)  * Free all lockdep keys in the range [start, start+size). Does not sleep.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6090)  * Ignores debug_locks. Must only be used by the lockdep selftests.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6091)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6092) static void lockdep_free_key_range_imm(void *start, unsigned long size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6093) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6094) 	struct pending_free *pf = delayed_free.pf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6095) 	unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6096) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6097) 	init_data_structures_once();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6098) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6099) 	raw_local_irq_save(flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6100) 	lockdep_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6101) 	__lockdep_free_key_range(pf, start, size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6102) 	__free_zapped_classes(pf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6103) 	lockdep_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6104) 	raw_local_irq_restore(flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6105) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6106) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6107) void lockdep_free_key_range(void *start, unsigned long size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6108) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6109) 	init_data_structures_once();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6110) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6111) 	if (inside_selftest())
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6112) 		lockdep_free_key_range_imm(start, size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6113) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6114) 		lockdep_free_key_range_reg(start, size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6115) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6116) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6117) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6118)  * Check whether any element of the @lock->class_cache[] array refers to a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6119)  * registered lock class. The caller must hold either the graph lock or the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6120)  * RCU read lock.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6121)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6122) static bool lock_class_cache_is_registered(struct lockdep_map *lock)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6123) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6124) 	struct lock_class *class;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6125) 	struct hlist_head *head;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6126) 	int i, j;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6127) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6128) 	for (i = 0; i < CLASSHASH_SIZE; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6129) 		head = classhash_table + i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6130) 		hlist_for_each_entry_rcu(class, head, hash_entry) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6131) 			for (j = 0; j < NR_LOCKDEP_CACHING_CLASSES; j++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6132) 				if (lock->class_cache[j] == class)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6133) 					return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6134) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6135) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6136) 	return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6137) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6138) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6139) /* The caller must hold the graph lock. Does not sleep. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6140) static void __lockdep_reset_lock(struct pending_free *pf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6141) 				 struct lockdep_map *lock)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6142) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6143) 	struct lock_class *class;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6144) 	int j;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6145) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6146) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6147) 	 * Remove all classes this lock might have:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6148) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6149) 	for (j = 0; j < MAX_LOCKDEP_SUBCLASSES; j++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6150) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6151) 		 * If the class exists we look it up and zap it:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6152) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6153) 		class = look_up_lock_class(lock, j);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6154) 		if (class)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6155) 			zap_class(pf, class);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6156) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6157) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6158) 	 * Debug check: in the end all mapped classes should
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6159) 	 * be gone.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6160) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6161) 	if (WARN_ON_ONCE(lock_class_cache_is_registered(lock)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6162) 		debug_locks_off();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6163) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6164) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6165) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6166)  * Remove all information lockdep has about a lock if debug_locks == 1. Free
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6167)  * released data structures from RCU context.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6168)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6169) static void lockdep_reset_lock_reg(struct lockdep_map *lock)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6170) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6171) 	struct pending_free *pf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6172) 	unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6173) 	int locked;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6174) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6175) 	raw_local_irq_save(flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6176) 	locked = graph_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6177) 	if (!locked)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6178) 		goto out_irq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6179) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6180) 	pf = get_pending_free();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6181) 	__lockdep_reset_lock(pf, lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6182) 	call_rcu_zapped(pf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6183) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6184) 	graph_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6185) out_irq:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6186) 	raw_local_irq_restore(flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6187) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6188) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6189) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6190)  * Reset a lock. Does not sleep. Ignores debug_locks. Must only be used by the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6191)  * lockdep selftests.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6192)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6193) static void lockdep_reset_lock_imm(struct lockdep_map *lock)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6194) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6195) 	struct pending_free *pf = delayed_free.pf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6196) 	unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6197) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6198) 	raw_local_irq_save(flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6199) 	lockdep_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6200) 	__lockdep_reset_lock(pf, lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6201) 	__free_zapped_classes(pf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6202) 	lockdep_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6203) 	raw_local_irq_restore(flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6204) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6205) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6206) void lockdep_reset_lock(struct lockdep_map *lock)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6207) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6208) 	init_data_structures_once();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6209) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6210) 	if (inside_selftest())
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6211) 		lockdep_reset_lock_imm(lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6212) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6213) 		lockdep_reset_lock_reg(lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6214) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6215) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6216) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6217)  * Unregister a dynamically allocated key.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6218)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6219)  * Unlike lockdep_register_key(), a search is always done to find a matching
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6220)  * key irrespective of debug_locks to avoid potential invalid access to freed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6221)  * memory in lock_class entry.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6222)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6223) void lockdep_unregister_key(struct lock_class_key *key)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6224) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6225) 	struct hlist_head *hash_head = keyhashentry(key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6226) 	struct lock_class_key *k;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6227) 	struct pending_free *pf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6228) 	unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6229) 	bool found = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6230) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6231) 	might_sleep();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6232) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6233) 	if (WARN_ON_ONCE(static_obj(key)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6234) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6235) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6236) 	raw_local_irq_save(flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6237) 	lockdep_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6238) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6239) 	hlist_for_each_entry_rcu(k, hash_head, hash_entry) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6240) 		if (k == key) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6241) 			hlist_del_rcu(&k->hash_entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6242) 			found = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6243) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6244) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6245) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6246) 	WARN_ON_ONCE(!found && debug_locks);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6247) 	if (found) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6248) 		pf = get_pending_free();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6249) 		__lockdep_free_key_range(pf, key, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6250) 		call_rcu_zapped(pf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6251) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6252) 	lockdep_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6253) 	raw_local_irq_restore(flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6254) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6255) 	/* Wait until is_dynamic_key() has finished accessing k->hash_entry. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6256) 	synchronize_rcu();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6257) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6258) EXPORT_SYMBOL_GPL(lockdep_unregister_key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6259) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6260) void __init lockdep_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6261) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6262) 	printk("Lock dependency validator: Copyright (c) 2006 Red Hat, Inc., Ingo Molnar\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6263) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6264) 	printk("... MAX_LOCKDEP_SUBCLASSES:  %lu\n", MAX_LOCKDEP_SUBCLASSES);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6265) 	printk("... MAX_LOCK_DEPTH:          %lu\n", MAX_LOCK_DEPTH);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6266) 	printk("... MAX_LOCKDEP_KEYS:        %lu\n", MAX_LOCKDEP_KEYS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6267) 	printk("... CLASSHASH_SIZE:          %lu\n", CLASSHASH_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6268) 	printk("... MAX_LOCKDEP_ENTRIES:     %lu\n", MAX_LOCKDEP_ENTRIES);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6269) 	printk("... MAX_LOCKDEP_CHAINS:      %lu\n", MAX_LOCKDEP_CHAINS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6270) 	printk("... CHAINHASH_SIZE:          %lu\n", CHAINHASH_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6271) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6272) 	printk(" memory used by lock dependency info: %zu kB\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6273) 	       (sizeof(lock_classes) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6274) 		sizeof(lock_classes_in_use) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6275) 		sizeof(classhash_table) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6276) 		sizeof(list_entries) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6277) 		sizeof(list_entries_in_use) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6278) 		sizeof(chainhash_table) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6279) 		sizeof(delayed_free)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6280) #ifdef CONFIG_PROVE_LOCKING
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6281) 		+ sizeof(lock_cq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6282) 		+ sizeof(lock_chains)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6283) 		+ sizeof(lock_chains_in_use)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6284) 		+ sizeof(chain_hlocks)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6285) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6286) 		) / 1024
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6287) 		);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6288) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6289) #if defined(CONFIG_TRACE_IRQFLAGS) && defined(CONFIG_PROVE_LOCKING)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6290) 	printk(" memory used for stack traces: %zu kB\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6291) 	       (sizeof(stack_trace) + sizeof(stack_trace_hash)) / 1024
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6292) 	       );
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6293) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6294) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6295) 	printk(" per task-struct memory footprint: %zu bytes\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6296) 	       sizeof(((struct task_struct *)NULL)->held_locks));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6297) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6298) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6299) static void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6300) print_freed_lock_bug(struct task_struct *curr, const void *mem_from,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6301) 		     const void *mem_to, struct held_lock *hlock)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6302) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6303) 	if (!debug_locks_off())
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6304) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6305) 	if (debug_locks_silent)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6306) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6307) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6308) 	pr_warn("\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6309) 	pr_warn("=========================\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6310) 	pr_warn("WARNING: held lock freed!\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6311) 	print_kernel_ident();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6312) 	pr_warn("-------------------------\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6313) 	pr_warn("%s/%d is freeing memory %px-%px, with a lock still held there!\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6314) 		curr->comm, task_pid_nr(curr), mem_from, mem_to-1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6315) 	print_lock(hlock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6316) 	lockdep_print_held_locks(curr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6317) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6318) 	pr_warn("\nstack backtrace:\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6319) 	dump_stack();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6320) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6321) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6322) static inline int not_in_range(const void* mem_from, unsigned long mem_len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6323) 				const void* lock_from, unsigned long lock_len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6324) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6325) 	return lock_from + lock_len <= mem_from ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6326) 		mem_from + mem_len <= lock_from;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6327) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6328) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6329) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6330)  * Called when kernel memory is freed (or unmapped), or if a lock
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6331)  * is destroyed or reinitialized - this code checks whether there is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6332)  * any held lock in the memory range of <from> to <to>:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6333)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6334) void debug_check_no_locks_freed(const void *mem_from, unsigned long mem_len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6335) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6336) 	struct task_struct *curr = current;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6337) 	struct held_lock *hlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6338) 	unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6339) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6340) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6341) 	if (unlikely(!debug_locks))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6342) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6343) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6344) 	raw_local_irq_save(flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6345) 	for (i = 0; i < curr->lockdep_depth; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6346) 		hlock = curr->held_locks + i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6347) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6348) 		if (not_in_range(mem_from, mem_len, hlock->instance,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6349) 					sizeof(*hlock->instance)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6350) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6351) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6352) 		print_freed_lock_bug(curr, mem_from, mem_from + mem_len, hlock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6353) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6354) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6355) 	raw_local_irq_restore(flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6356) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6357) EXPORT_SYMBOL_GPL(debug_check_no_locks_freed);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6358) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6359) static void print_held_locks_bug(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6360) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6361) 	if (!debug_locks_off())
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6362) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6363) 	if (debug_locks_silent)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6364) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6365) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6366) 	pr_warn("\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6367) 	pr_warn("====================================\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6368) 	pr_warn("WARNING: %s/%d still has locks held!\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6369) 	       current->comm, task_pid_nr(current));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6370) 	print_kernel_ident();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6371) 	pr_warn("------------------------------------\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6372) 	lockdep_print_held_locks(current);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6373) 	pr_warn("\nstack backtrace:\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6374) 	dump_stack();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6375) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6376) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6377) void debug_check_no_locks_held(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6378) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6379) 	if (unlikely(current->lockdep_depth > 0))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6380) 		print_held_locks_bug();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6381) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6382) EXPORT_SYMBOL_GPL(debug_check_no_locks_held);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6383) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6384) #ifdef __KERNEL__
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6385) void debug_show_all_locks(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6386) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6387) 	struct task_struct *g, *p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6388) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6389) 	if (unlikely(!debug_locks)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6390) 		pr_warn("INFO: lockdep is turned off.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6391) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6392) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6393) 	pr_warn("\nShowing all locks held in the system:\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6394) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6395) 	rcu_read_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6396) 	for_each_process_thread(g, p) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6397) 		if (!p->lockdep_depth)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6398) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6399) 		lockdep_print_held_locks(p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6400) 		touch_nmi_watchdog();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6401) 		touch_all_softlockup_watchdogs();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6402) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6403) 	rcu_read_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6404) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6405) 	pr_warn("\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6406) 	pr_warn("=============================================\n\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6407) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6408) EXPORT_SYMBOL_GPL(debug_show_all_locks);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6409) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6410) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6411) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6412)  * Careful: only use this function if you are sure that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6413)  * the task cannot run in parallel!
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6414)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6415) void debug_show_held_locks(struct task_struct *task)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6416) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6417) 	if (unlikely(!debug_locks)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6418) 		printk("INFO: lockdep is turned off.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6419) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6420) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6421) 	lockdep_print_held_locks(task);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6422) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6423) EXPORT_SYMBOL_GPL(debug_show_held_locks);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6424) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6425) asmlinkage __visible void lockdep_sys_exit(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6426) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6427) 	struct task_struct *curr = current;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6428) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6429) 	if (unlikely(curr->lockdep_depth)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6430) 		if (!debug_locks_off())
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6431) 			return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6432) 		pr_warn("\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6433) 		pr_warn("================================================\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6434) 		pr_warn("WARNING: lock held when returning to user space!\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6435) 		print_kernel_ident();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6436) 		pr_warn("------------------------------------------------\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6437) 		pr_warn("%s/%d is leaving the kernel with locks still held!\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6438) 				curr->comm, curr->pid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6439) 		lockdep_print_held_locks(curr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6440) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6441) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6442) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6443) 	 * The lock history for each syscall should be independent. So wipe the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6444) 	 * slate clean on return to userspace.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6445) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6446) 	lockdep_invariant_state(false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6447) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6448) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6449) void lockdep_rcu_suspicious(const char *file, const int line, const char *s)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6450) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6451) 	struct task_struct *curr = current;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6452) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6453) 	/* Note: the following can be executed concurrently, so be careful. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6454) 	pr_warn("\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6455) 	pr_warn("=============================\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6456) 	pr_warn("WARNING: suspicious RCU usage\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6457) 	print_kernel_ident();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6458) 	pr_warn("-----------------------------\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6459) 	pr_warn("%s:%d %s!\n", file, line, s);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6460) 	pr_warn("\nother info that might help us debug this:\n\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6461) 	pr_warn("\n%srcu_scheduler_active = %d, debug_locks = %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6462) 	       !rcu_lockdep_current_cpu_online()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6463) 			? "RCU used illegally from offline CPU!\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6464) 			: "",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6465) 	       rcu_scheduler_active, debug_locks);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6466) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6467) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6468) 	 * If a CPU is in the RCU-free window in idle (ie: in the section
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6469) 	 * between rcu_idle_enter() and rcu_idle_exit(), then RCU
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6470) 	 * considers that CPU to be in an "extended quiescent state",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6471) 	 * which means that RCU will be completely ignoring that CPU.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6472) 	 * Therefore, rcu_read_lock() and friends have absolutely no
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6473) 	 * effect on a CPU running in that state. In other words, even if
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6474) 	 * such an RCU-idle CPU has called rcu_read_lock(), RCU might well
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6475) 	 * delete data structures out from under it.  RCU really has no
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6476) 	 * choice here: we need to keep an RCU-free window in idle where
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6477) 	 * the CPU may possibly enter into low power mode. This way we can
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6478) 	 * notice an extended quiescent state to other CPUs that started a grace
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6479) 	 * period. Otherwise we would delay any grace period as long as we run
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6480) 	 * in the idle task.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6481) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6482) 	 * So complain bitterly if someone does call rcu_read_lock(),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6483) 	 * rcu_read_lock_bh() and so on from extended quiescent states.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6484) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6485) 	if (!rcu_is_watching())
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6486) 		pr_warn("RCU used illegally from extended quiescent state!\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6487) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6488) 	lockdep_print_held_locks(curr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6489) 	pr_warn("\nstack backtrace:\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6490) 	dump_stack();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6491) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6492) EXPORT_SYMBOL_GPL(lockdep_rcu_suspicious);