2988 lines · 65.6 KB · C
RawDownload
// SPDX-License-Identifier: GPL-2.0/* * lib/locking-selftest.c * * Testsuite for various locking APIs: spinlocks, rwlocks, * mutexes and rw-semaphores. * * It is checking both false positives and false negatives. * * Started by Ingo Molnar: * *  Copyright (C) 2006 Red Hat, Inc., Ingo Molnar <mingo@redhat.com> */#include <linux/rwsem.h>#include <linux/mutex.h>#include <linux/ww_mutex.h>#include <linux/sched.h>#include <linux/sched/mm.h>#include <linux/delay.h>#include <linux/lockdep.h>#include <linux/spinlock.h>#include <linux/kallsyms.h>#include <linux/interrupt.h>#include <linux/debug_locks.h>#include <linux/irqflags.h>#include <linux/rtmutex.h>#include <linux/local_lock.h> #ifdef CONFIG_PREEMPT_RT# define NON_RT(...)#else# define NON_RT(...)	__VA_ARGS__#endif /* * Change this to 1 if you want to see the failure printouts: */static unsigned int debug_locks_verbose;unsigned int force_read_lock_recursive; static DEFINE_WD_CLASS(ww_lockdep); static int __init setup_debug_locks_verbose(char *str){	get_option(&str, &debug_locks_verbose); 	return 1;} __setup("debug_locks_verbose=", setup_debug_locks_verbose); #define FAILURE		0#define SUCCESS		1 #define LOCKTYPE_SPIN	0x1#define LOCKTYPE_RWLOCK	0x2#define LOCKTYPE_MUTEX	0x4#define LOCKTYPE_RWSEM	0x8#define LOCKTYPE_WW	0x10#define LOCKTYPE_RTMUTEX 0x20#define LOCKTYPE_LL	0x40#define LOCKTYPE_SPECIAL 0x80 static struct ww_acquire_ctx t, t2;static struct ww_mutex o, o2, o3; /* * Normal standalone locks, for the circular and irq-context * dependency tests: */static DEFINE_SPINLOCK(lock_A);static DEFINE_SPINLOCK(lock_B);static DEFINE_SPINLOCK(lock_C);static DEFINE_SPINLOCK(lock_D); static DEFINE_RAW_SPINLOCK(raw_lock_A);static DEFINE_RAW_SPINLOCK(raw_lock_B); static DEFINE_RWLOCK(rwlock_A);static DEFINE_RWLOCK(rwlock_B);static DEFINE_RWLOCK(rwlock_C);static DEFINE_RWLOCK(rwlock_D); static DEFINE_MUTEX(mutex_A);static DEFINE_MUTEX(mutex_B);static DEFINE_MUTEX(mutex_C);static DEFINE_MUTEX(mutex_D); static DECLARE_RWSEM(rwsem_A);static DECLARE_RWSEM(rwsem_B);static DECLARE_RWSEM(rwsem_C);static DECLARE_RWSEM(rwsem_D); #ifdef CONFIG_RT_MUTEXES static DEFINE_RT_MUTEX(rtmutex_A);static DEFINE_RT_MUTEX(rtmutex_B);static DEFINE_RT_MUTEX(rtmutex_C);static DEFINE_RT_MUTEX(rtmutex_D); #endif /* * Locks that we initialize dynamically as well so that * e.g. X1 and X2 becomes two instances of the same class, * but X* and Y* are different classes. We do this so that * we do not trigger a real lockup: */static DEFINE_SPINLOCK(lock_X1);static DEFINE_SPINLOCK(lock_X2);static DEFINE_SPINLOCK(lock_Y1);static DEFINE_SPINLOCK(lock_Y2);static DEFINE_SPINLOCK(lock_Z1);static DEFINE_SPINLOCK(lock_Z2); static DEFINE_RWLOCK(rwlock_X1);static DEFINE_RWLOCK(rwlock_X2);static DEFINE_RWLOCK(rwlock_Y1);static DEFINE_RWLOCK(rwlock_Y2);static DEFINE_RWLOCK(rwlock_Z1);static DEFINE_RWLOCK(rwlock_Z2); static DEFINE_MUTEX(mutex_X1);static DEFINE_MUTEX(mutex_X2);static DEFINE_MUTEX(mutex_Y1);static DEFINE_MUTEX(mutex_Y2);static DEFINE_MUTEX(mutex_Z1);static DEFINE_MUTEX(mutex_Z2); static DECLARE_RWSEM(rwsem_X1);static DECLARE_RWSEM(rwsem_X2);static DECLARE_RWSEM(rwsem_Y1);static DECLARE_RWSEM(rwsem_Y2);static DECLARE_RWSEM(rwsem_Z1);static DECLARE_RWSEM(rwsem_Z2); #ifdef CONFIG_RT_MUTEXES static DEFINE_RT_MUTEX(rtmutex_X1);static DEFINE_RT_MUTEX(rtmutex_X2);static DEFINE_RT_MUTEX(rtmutex_Y1);static DEFINE_RT_MUTEX(rtmutex_Y2);static DEFINE_RT_MUTEX(rtmutex_Z1);static DEFINE_RT_MUTEX(rtmutex_Z2); #endif static DEFINE_PER_CPU(local_lock_t, local_A); /* * non-inlined runtime initializers, to let separate locks share * the same lock-class: */#define INIT_CLASS_FUNC(class) 				\static noinline void					\init_class_##class(spinlock_t *lock, rwlock_t *rwlock, \	struct mutex *mutex, struct rw_semaphore *rwsem)\{							\	spin_lock_init(lock);			\	rwlock_init(rwlock);				\	mutex_init(mutex);				\	init_rwsem(rwsem);				\} INIT_CLASS_FUNC(X)INIT_CLASS_FUNC(Y)INIT_CLASS_FUNC(Z) static void init_shared_classes(void){#ifdef CONFIG_RT_MUTEXES	static struct lock_class_key rt_X, rt_Y, rt_Z; 	__rt_mutex_init(&rtmutex_X1, __func__, &rt_X);	__rt_mutex_init(&rtmutex_X2, __func__, &rt_X);	__rt_mutex_init(&rtmutex_Y1, __func__, &rt_Y);	__rt_mutex_init(&rtmutex_Y2, __func__, &rt_Y);	__rt_mutex_init(&rtmutex_Z1, __func__, &rt_Z);	__rt_mutex_init(&rtmutex_Z2, __func__, &rt_Z);#endif 	init_class_X(&lock_X1, &rwlock_X1, &mutex_X1, &rwsem_X1);	init_class_X(&lock_X2, &rwlock_X2, &mutex_X2, &rwsem_X2); 	init_class_Y(&lock_Y1, &rwlock_Y1, &mutex_Y1, &rwsem_Y1);	init_class_Y(&lock_Y2, &rwlock_Y2, &mutex_Y2, &rwsem_Y2); 	init_class_Z(&lock_Z1, &rwlock_Z1, &mutex_Z1, &rwsem_Z1);	init_class_Z(&lock_Z2, &rwlock_Z2, &mutex_Z2, &rwsem_Z2);} /* * For spinlocks and rwlocks we also do hardirq-safe / softirq-safe tests. * The following functions use a lock from a simulated hardirq/softirq * context, causing the locks to be marked as hardirq-safe/softirq-safe: */ #define HARDIRQ_DISABLE		local_irq_disable#define HARDIRQ_ENABLE		local_irq_enable #define HARDIRQ_ENTER()				\	local_irq_disable();			\	__irq_enter();				\	lockdep_hardirq_threaded();		\	WARN_ON(!in_hardirq()); #define HARDIRQ_EXIT()				\	__irq_exit();				\	local_irq_enable(); #define SOFTIRQ_DISABLE		local_bh_disable#define SOFTIRQ_ENABLE		local_bh_enable #define SOFTIRQ_ENTER()				\		local_bh_disable();		\		local_irq_disable();		\		lockdep_softirq_enter();	\		WARN_ON(!in_softirq()); #define SOFTIRQ_EXIT()				\		lockdep_softirq_exit();		\		local_irq_enable();		\		local_bh_enable(); /* * Shortcuts for lock/unlock API variants, to keep * the testcases compact: */#define L(x)			spin_lock(&lock_##x)#define U(x)			spin_unlock(&lock_##x)#define LU(x)			L(x); U(x)#define SI(x)			spin_lock_init(&lock_##x) #define WL(x)			write_lock(&rwlock_##x)#define WU(x)			write_unlock(&rwlock_##x)#define WLU(x)			WL(x); WU(x) #define RL(x)			read_lock(&rwlock_##x)#define RU(x)			read_unlock(&rwlock_##x)#define RLU(x)			RL(x); RU(x)#define RWI(x)			rwlock_init(&rwlock_##x) #define ML(x)			mutex_lock(&mutex_##x)#define MU(x)			mutex_unlock(&mutex_##x)#define MI(x)			mutex_init(&mutex_##x) #define RTL(x)			rt_mutex_lock(&rtmutex_##x)#define RTU(x)			rt_mutex_unlock(&rtmutex_##x)#define RTI(x)			rt_mutex_init(&rtmutex_##x) #define WSL(x)			down_write(&rwsem_##x)#define WSU(x)			up_write(&rwsem_##x) #define RSL(x)			down_read(&rwsem_##x)#define RSU(x)			up_read(&rwsem_##x)#define RWSI(x)			init_rwsem(&rwsem_##x) #ifndef CONFIG_DEBUG_WW_MUTEX_SLOWPATH#define WWAI(x)			ww_acquire_init(x, &ww_lockdep)#else#define WWAI(x)			do { ww_acquire_init(x, &ww_lockdep); (x)->deadlock_inject_countdown = ~0U; } while (0)#endif#define WWAD(x)			ww_acquire_done(x)#define WWAF(x)			ww_acquire_fini(x) #define WWL(x, c)		ww_mutex_lock(x, c)#define WWT(x)			ww_mutex_trylock(x, NULL)#define WWL1(x)			ww_mutex_lock(x, NULL)#define WWU(x)			ww_mutex_unlock(x)  #define LOCK_UNLOCK_2(x,y)	LOCK(x); LOCK(y); UNLOCK(y); UNLOCK(x) /* * Generate different permutations of the same testcase, using * the same basic lock-dependency/state events: */ #define GENERATE_TESTCASE(name)			\						\static void name(void) { E(); } #define GENERATE_PERMUTATIONS_2_EVENTS(name)	\						\static void name##_12(void) { E1(); E2(); }	\static void name##_21(void) { E2(); E1(); } #define GENERATE_PERMUTATIONS_3_EVENTS(name)		\							\static void name##_123(void) { E1(); E2(); E3(); }	\static void name##_132(void) { E1(); E3(); E2(); }	\static void name##_213(void) { E2(); E1(); E3(); }	\static void name##_231(void) { E2(); E3(); E1(); }	\static void name##_312(void) { E3(); E1(); E2(); }	\static void name##_321(void) { E3(); E2(); E1(); } /* * AA deadlock: */ #define E()					\						\	LOCK(X1);				\	LOCK(X2); /* this one should fail */ /* * 6 testcases: */#include "locking-selftest-spin.h"GENERATE_TESTCASE(AA_spin)#include "locking-selftest-wlock.h"GENERATE_TESTCASE(AA_wlock)#include "locking-selftest-rlock.h"GENERATE_TESTCASE(AA_rlock)#include "locking-selftest-mutex.h"GENERATE_TESTCASE(AA_mutex)#include "locking-selftest-wsem.h"GENERATE_TESTCASE(AA_wsem)#include "locking-selftest-rsem.h"GENERATE_TESTCASE(AA_rsem) #ifdef CONFIG_RT_MUTEXES#include "locking-selftest-rtmutex.h"GENERATE_TESTCASE(AA_rtmutex);#endif #undef E /* * Special-case for read-locking, they are * allowed to recurse on the same lock class: */static void rlock_AA1(void){	RL(X1);	RL(X1); // this one should NOT fail} static void rlock_AA1B(void){	RL(X1);	RL(X2); // this one should NOT fail} static void rsem_AA1(void){	RSL(X1);	RSL(X1); // this one should fail} static void rsem_AA1B(void){	RSL(X1);	RSL(X2); // this one should fail}/* * The mixing of read and write locks is not allowed: */static void rlock_AA2(void){	RL(X1);	WL(X2); // this one should fail} static void rsem_AA2(void){	RSL(X1);	WSL(X2); // this one should fail} static void rlock_AA3(void){	WL(X1);	RL(X2); // this one should fail} static void rsem_AA3(void){	WSL(X1);	RSL(X2); // this one should fail} /* * read_lock(A) * spin_lock(B) *		spin_lock(B) *		write_lock(A) */static void rlock_ABBA1(void){	RL(X1);	L(Y1);	U(Y1);	RU(X1); 	L(Y1);	WL(X1);	WU(X1);	U(Y1); // should fail} static void rwsem_ABBA1(void){	RSL(X1);	ML(Y1);	MU(Y1);	RSU(X1); 	ML(Y1);	WSL(X1);	WSU(X1);	MU(Y1); // should fail} /* * read_lock(A) * spin_lock(B) *		spin_lock(B) *		write_lock(A) * * This test case is aimed at poking whether the chain cache prevents us from * detecting a read-lock/lock-write deadlock: if the chain cache doesn't differ * read/write locks, the following case may happen * * 	{ read_lock(A)->lock(B) dependency exists } * * 	P0: * 	lock(B); * 	read_lock(A); * *	{ Not a deadlock, B -> A is added in the chain cache } * *	P1: *	lock(B); *	write_lock(A); * *	{ B->A found in chain cache, not reported as a deadlock } * */static void rlock_chaincache_ABBA1(void){	RL(X1);	L(Y1);	U(Y1);	RU(X1); 	L(Y1);	RL(X1);	RU(X1);	U(Y1); 	L(Y1);	WL(X1);	WU(X1);	U(Y1); // should fail} /* * read_lock(A) * spin_lock(B) *		spin_lock(B) *		read_lock(A) */static void rlock_ABBA2(void){	RL(X1);	L(Y1);	U(Y1);	RU(X1); 	L(Y1);	RL(X1);	RU(X1);	U(Y1); // should NOT fail} static void rwsem_ABBA2(void){	RSL(X1);	ML(Y1);	MU(Y1);	RSU(X1); 	ML(Y1);	RSL(X1);	RSU(X1);	MU(Y1); // should fail}  /* * write_lock(A) * spin_lock(B) *		spin_lock(B) *		write_lock(A) */static void rlock_ABBA3(void){	WL(X1);	L(Y1);

Lines 1–500 of 2988.