xref: /linux-6.15/kernel/locking/locktorture.c (revision a6884f6f)
15a4eb3cbSPaul E. McKenney // SPDX-License-Identifier: GPL-2.0+
20af3fe1eSPaul E. McKenney /*
30af3fe1eSPaul E. McKenney  * Module-based torture test facility for locking
40af3fe1eSPaul E. McKenney  *
50af3fe1eSPaul E. McKenney  * Copyright (C) IBM Corporation, 2014
60af3fe1eSPaul E. McKenney  *
75a4eb3cbSPaul E. McKenney  * Authors: Paul E. McKenney <[email protected]>
8095777c4SDavidlohr Bueso  *          Davidlohr Bueso <[email protected]>
90af3fe1eSPaul E. McKenney  *	Based on kernel/rcu/torture.c.
100af3fe1eSPaul E. McKenney  */
1160500037SPaul E. McKenney 
1260500037SPaul E. McKenney #define pr_fmt(fmt) fmt
1360500037SPaul E. McKenney 
140af3fe1eSPaul E. McKenney #include <linux/kernel.h>
150af3fe1eSPaul E. McKenney #include <linux/module.h>
160af3fe1eSPaul E. McKenney #include <linux/kthread.h>
17095777c4SDavidlohr Bueso #include <linux/sched/rt.h>
180af3fe1eSPaul E. McKenney #include <linux/spinlock.h>
1942ddc75dSDavidlohr Bueso #include <linux/mutex.h>
20c98fed9fSDavidlohr Bueso #include <linux/rwsem.h>
210af3fe1eSPaul E. McKenney #include <linux/smp.h>
220af3fe1eSPaul E. McKenney #include <linux/interrupt.h>
230af3fe1eSPaul E. McKenney #include <linux/sched.h>
24ae7e81c0SIngo Molnar #include <uapi/linux/sched/types.h>
25037741a6SIngo Molnar #include <linux/rtmutex.h>
260af3fe1eSPaul E. McKenney #include <linux/atomic.h>
270af3fe1eSPaul E. McKenney #include <linux/moduleparam.h>
280af3fe1eSPaul E. McKenney #include <linux/delay.h>
290af3fe1eSPaul E. McKenney #include <linux/slab.h>
300af3fe1eSPaul E. McKenney #include <linux/torture.h>
316b74fa0aSPaul E. McKenney #include <linux/reboot.h>
320af3fe1eSPaul E. McKenney 
336a081bacSJeff Johnson MODULE_DESCRIPTION("torture test facility for locking");
340af3fe1eSPaul E. McKenney MODULE_LICENSE("GPL");
355a4eb3cbSPaul E. McKenney MODULE_AUTHOR("Paul E. McKenney <[email protected]>");
360af3fe1eSPaul E. McKenney 
37e3bdaefbSPaul E. McKenney torture_param(int, acq_writer_lim, 0, "Write_acquisition time limit (jiffies).");
387f993623SPaul E. McKenney torture_param(int, call_rcu_chains, 0, "Self-propagate call_rcu() chains during test (0=disable).");
39f8619c30SPaul E. McKenney torture_param(int, long_hold, 100, "Do occasional long hold of lock (ms), 0=disable");
4031742a56SPaul E. McKenney torture_param(int, nested_locks, 0, "Number of nested locks (max = 8)");
4131742a56SPaul E. McKenney torture_param(int, nreaders_stress, -1, "Number of read-locking stress-test threads");
4231742a56SPaul E. McKenney torture_param(int, nwriters_stress, -1, "Number of write-locking stress-test threads");
430af3fe1eSPaul E. McKenney torture_param(int, onoff_holdoff, 0, "Time after boot before CPU hotplugs (s)");
44f8619c30SPaul E. McKenney torture_param(int, onoff_interval, 0, "Time between CPU hotplugs (s), 0=disable");
4531742a56SPaul E. McKenney torture_param(int, rt_boost, 2,
4631742a56SPaul E. McKenney 		   "Do periodic rt-boost. 0=Disable, 1=Only for rt_mutex, 2=For all lock types.");
4731742a56SPaul E. McKenney torture_param(int, rt_boost_factor, 50, "A factor determining how often rt-boost happens.");
48f8619c30SPaul E. McKenney torture_param(int, shuffle_interval, 3, "Number of jiffies between shuffles, 0=disable");
490af3fe1eSPaul E. McKenney torture_param(int, shutdown_secs, 0, "Shutdown time (j), <= zero to disable.");
50f8619c30SPaul E. McKenney torture_param(int, stat_interval, 60, "Number of seconds between stats printk()s");
510af3fe1eSPaul E. McKenney torture_param(int, stutter, 5, "Number of jiffies to run/halt test, 0=disable");
52f8619c30SPaul E. McKenney torture_param(int, verbose, 1, "Enable verbose debugging printk()s");
5300c24c9cSPaul E. McKenney torture_param(int, writer_fifo, 0, "Run writers at sched_set_fifo() priority");
54b6334320SJohn Stultz /* Going much higher trips "BUG: MAX_LOCKDEP_CHAIN_HLOCKS too low!" errors */
55b6334320SJohn Stultz #define MAX_NESTED_LOCKS 8
560af3fe1eSPaul E. McKenney 
575d65cf6aSZqiang static char *torture_type = IS_ENABLED(CONFIG_PREEMPT_RT) ? "raw_spin_lock" : "spin_lock";
580af3fe1eSPaul E. McKenney module_param(torture_type, charp, 0444);
590af3fe1eSPaul E. McKenney MODULE_PARM_DESC(torture_type,
6042ddc75dSDavidlohr Bueso 		 "Type of lock to torture (spin_lock, spin_lock_irq, mutex_lock, ...)");
610af3fe1eSPaul E. McKenney 
622273799cSPaul E. McKenney static cpumask_var_t bind_readers; // Bind the readers to the specified set of CPUs.
632273799cSPaul E. McKenney static cpumask_var_t bind_writers; // Bind the writers to the specified set of CPUs.
6473e34124SPaul E. McKenney 
6573e34124SPaul E. McKenney // Parse a cpumask kernel parameter.  If there are more users later on,
6673e34124SPaul E. McKenney // this might need to got to a more central location.
param_set_cpumask(const char * val,const struct kernel_param * kp)6773e34124SPaul E. McKenney static int param_set_cpumask(const char *val, const struct kernel_param *kp)
6873e34124SPaul E. McKenney {
6973e34124SPaul E. McKenney 	cpumask_var_t *cm_bind = kp->arg;
7073e34124SPaul E. McKenney 	int ret;
7173e34124SPaul E. McKenney 	char *s;
7273e34124SPaul E. McKenney 
7373e34124SPaul E. McKenney 	if (!alloc_cpumask_var(cm_bind, GFP_KERNEL)) {
7473e34124SPaul E. McKenney 		s = "Out of memory";
7573e34124SPaul E. McKenney 		ret = -ENOMEM;
7673e34124SPaul E. McKenney 		goto out_err;
7773e34124SPaul E. McKenney 	}
7873e34124SPaul E. McKenney 	ret = cpulist_parse(val, *cm_bind);
7973e34124SPaul E. McKenney 	if (!ret)
8073e34124SPaul E. McKenney 		return ret;
8173e34124SPaul E. McKenney 	s = "Bad CPU range";
8273e34124SPaul E. McKenney out_err:
8373e34124SPaul E. McKenney 	pr_warn("%s: %s, all CPUs set\n", kp->name, s);
8473e34124SPaul E. McKenney 	cpumask_setall(*cm_bind);
8573e34124SPaul E. McKenney 	return ret;
8673e34124SPaul E. McKenney }
8773e34124SPaul E. McKenney 
8873e34124SPaul E. McKenney // Output a cpumask kernel parameter.
param_get_cpumask(char * buffer,const struct kernel_param * kp)8973e34124SPaul E. McKenney static int param_get_cpumask(char *buffer, const struct kernel_param *kp)
9073e34124SPaul E. McKenney {
9173e34124SPaul E. McKenney 	cpumask_var_t *cm_bind = kp->arg;
9273e34124SPaul E. McKenney 
9373e34124SPaul E. McKenney 	return sprintf(buffer, "%*pbl", cpumask_pr_args(*cm_bind));
9473e34124SPaul E. McKenney }
9573e34124SPaul E. McKenney 
cpumask_nonempty(cpumask_var_t mask)9673e34124SPaul E. McKenney static bool cpumask_nonempty(cpumask_var_t mask)
9773e34124SPaul E. McKenney {
9873e34124SPaul E. McKenney 	return cpumask_available(mask) && !cpumask_empty(mask);
9973e34124SPaul E. McKenney }
10073e34124SPaul E. McKenney 
10173e34124SPaul E. McKenney static const struct kernel_param_ops lt_bind_ops = {
10273e34124SPaul E. McKenney 	.set = param_set_cpumask,
10373e34124SPaul E. McKenney 	.get = param_get_cpumask,
10473e34124SPaul E. McKenney };
10573e34124SPaul E. McKenney 
1062273799cSPaul E. McKenney module_param_cb(bind_readers, &lt_bind_ops, &bind_readers, 0644);
1072273799cSPaul E. McKenney module_param_cb(bind_writers, &lt_bind_ops, &bind_writers, 0644);
10873e34124SPaul E. McKenney 
1090203b485SPaul E. McKenney long torture_sched_setaffinity(pid_t pid, const struct cpumask *in_mask, bool dowarn);
11073e34124SPaul E. McKenney 
1110af3fe1eSPaul E. McKenney static struct task_struct *stats_task;
1120af3fe1eSPaul E. McKenney static struct task_struct **writer_tasks;
1134f6332c1SDavidlohr Bueso static struct task_struct **reader_tasks;
1140af3fe1eSPaul E. McKenney 
1150af3fe1eSPaul E. McKenney static bool lock_is_write_held;
116af5f6e27SPaul E. McKenney static atomic_t lock_is_read_held;
1173480d677SPaul E. McKenney static unsigned long last_lock_release;
1180af3fe1eSPaul E. McKenney 
1191e6757a9SDavidlohr Bueso struct lock_stress_stats {
1201e6757a9SDavidlohr Bueso 	long n_lock_fail;
1211e6757a9SDavidlohr Bueso 	long n_lock_acquired;
1220af3fe1eSPaul E. McKenney };
1230af3fe1eSPaul E. McKenney 
1247f993623SPaul E. McKenney struct call_rcu_chain {
1257f993623SPaul E. McKenney 	struct rcu_head crc_rh;
1267f993623SPaul E. McKenney 	bool crc_stop;
1277f993623SPaul E. McKenney };
12869dcbbd8SPaul E. McKenney struct call_rcu_chain *call_rcu_chain_list;
1297f993623SPaul E. McKenney 
1300af3fe1eSPaul E. McKenney /* Forward reference. */
1310af3fe1eSPaul E. McKenney static void lock_torture_cleanup(void);
1320af3fe1eSPaul E. McKenney 
1330af3fe1eSPaul E. McKenney /*
1340af3fe1eSPaul E. McKenney  * Operations vector for selecting different types of tests.
1350af3fe1eSPaul E. McKenney  */
1360af3fe1eSPaul E. McKenney struct lock_torture_ops {
1370af3fe1eSPaul E. McKenney 	void (*init)(void);
1380d720287SHou Tao 	void (*exit)(void);
139b6334320SJohn Stultz 	int (*nested_lock)(int tid, u32 lockset);
140aa3a5f31SWaiman Long 	int (*writelock)(int tid);
1410af3fe1eSPaul E. McKenney 	void (*write_delay)(struct torture_random_state *trsp);
142095777c4SDavidlohr Bueso 	void (*task_boost)(struct torture_random_state *trsp);
143aa3a5f31SWaiman Long 	void (*writeunlock)(int tid);
144b6334320SJohn Stultz 	void (*nested_unlock)(int tid, u32 lockset);
145aa3a5f31SWaiman Long 	int (*readlock)(int tid);
1464f6332c1SDavidlohr Bueso 	void (*read_delay)(struct torture_random_state *trsp);
147aa3a5f31SWaiman Long 	void (*readunlock)(int tid);
148095777c4SDavidlohr Bueso 
149095777c4SDavidlohr Bueso 	unsigned long flags; /* for irq spinlocks */
1500af3fe1eSPaul E. McKenney 	const char *name;
1510af3fe1eSPaul E. McKenney };
1520af3fe1eSPaul E. McKenney 
153630952c2SDavidlohr Bueso struct lock_torture_cxt {
154630952c2SDavidlohr Bueso 	int nrealwriters_stress;
155630952c2SDavidlohr Bueso 	int nrealreaders_stress;
156630952c2SDavidlohr Bueso 	bool debug_lock;
1570d720287SHou Tao 	bool init_called;
158630952c2SDavidlohr Bueso 	atomic_t n_lock_torture_errors;
159630952c2SDavidlohr Bueso 	struct lock_torture_ops *cur_ops;
160630952c2SDavidlohr Bueso 	struct lock_stress_stats *lwsa; /* writer statistics */
161630952c2SDavidlohr Bueso 	struct lock_stress_stats *lrsa; /* reader statistics */
162630952c2SDavidlohr Bueso };
1630d720287SHou Tao static struct lock_torture_cxt cxt = { 0, 0, false, false,
164630952c2SDavidlohr Bueso 				       ATOMIC_INIT(0),
165630952c2SDavidlohr Bueso 				       NULL, NULL};
1660af3fe1eSPaul E. McKenney /*
1670af3fe1eSPaul E. McKenney  * Definitions for lock torture testing.
1680af3fe1eSPaul E. McKenney  */
1690af3fe1eSPaul E. McKenney 
torture_lock_busted_write_lock(int tid __maybe_unused)170aa3a5f31SWaiman Long static int torture_lock_busted_write_lock(int tid __maybe_unused)
171e086481bSPaul E. McKenney {
172e086481bSPaul E. McKenney 	return 0;  /* BUGGY, do not use in real life!!! */
173e086481bSPaul E. McKenney }
174e086481bSPaul E. McKenney 
torture_lock_busted_write_delay(struct torture_random_state * trsp)175e086481bSPaul E. McKenney static void torture_lock_busted_write_delay(struct torture_random_state *trsp)
176e086481bSPaul E. McKenney {
177e086481bSPaul E. McKenney 	/* We want a long delay occasionally to force massive contention.  */
17800c24c9cSPaul E. McKenney 	if (long_hold && !(torture_random(trsp) % (cxt.nrealwriters_stress * 2000 * long_hold)))
17900c24c9cSPaul E. McKenney 		mdelay(long_hold);
180630952c2SDavidlohr Bueso 	if (!(torture_random(trsp) % (cxt.nrealwriters_stress * 20000)))
181cc1321c9SPaul E. McKenney 		torture_preempt_schedule();  /* Allow test to be preempted. */
182e086481bSPaul E. McKenney }
183e086481bSPaul E. McKenney 
torture_lock_busted_write_unlock(int tid __maybe_unused)184aa3a5f31SWaiman Long static void torture_lock_busted_write_unlock(int tid __maybe_unused)
185e086481bSPaul E. McKenney {
186e086481bSPaul E. McKenney 	  /* BUGGY, do not use in real life!!! */
187e086481bSPaul E. McKenney }
188e086481bSPaul E. McKenney 
__torture_rt_boost(struct torture_random_state * trsp)189e01f3a1aSJoel Fernandes (Google) static void __torture_rt_boost(struct torture_random_state *trsp)
190095777c4SDavidlohr Bueso {
191c24501b2SJoel Fernandes (Google) 	const unsigned int factor = rt_boost_factor;
192e01f3a1aSJoel Fernandes (Google) 
193e01f3a1aSJoel Fernandes (Google) 	if (!rt_task(current)) {
194e01f3a1aSJoel Fernandes (Google) 		/*
195c24501b2SJoel Fernandes (Google) 		 * Boost priority once every rt_boost_factor operations. When
196c24501b2SJoel Fernandes (Google) 		 * the task tries to take the lock, the rtmutex it will account
197e01f3a1aSJoel Fernandes (Google) 		 * for the new priority, and do any corresponding pi-dance.
198e01f3a1aSJoel Fernandes (Google) 		 */
199e01f3a1aSJoel Fernandes (Google) 		if (trsp && !(torture_random(trsp) %
200e01f3a1aSJoel Fernandes (Google) 			      (cxt.nrealwriters_stress * factor))) {
201e01f3a1aSJoel Fernandes (Google) 			sched_set_fifo(current);
202e01f3a1aSJoel Fernandes (Google) 		} else /* common case, do nothing */
203e01f3a1aSJoel Fernandes (Google) 			return;
204e01f3a1aSJoel Fernandes (Google) 	} else {
205e01f3a1aSJoel Fernandes (Google) 		/*
206c24501b2SJoel Fernandes (Google) 		 * The task will remain boosted for another 10 * rt_boost_factor
207c24501b2SJoel Fernandes (Google) 		 * operations, then restored back to its original prio, and so
208c24501b2SJoel Fernandes (Google) 		 * forth.
209e01f3a1aSJoel Fernandes (Google) 		 *
210e01f3a1aSJoel Fernandes (Google) 		 * When @trsp is nil, we want to force-reset the task for
211e01f3a1aSJoel Fernandes (Google) 		 * stopping the kthread.
212e01f3a1aSJoel Fernandes (Google) 		 */
213e01f3a1aSJoel Fernandes (Google) 		if (!trsp || !(torture_random(trsp) %
214e01f3a1aSJoel Fernandes (Google) 			       (cxt.nrealwriters_stress * factor * 2))) {
215e01f3a1aSJoel Fernandes (Google) 			sched_set_normal(current, 0);
216e01f3a1aSJoel Fernandes (Google) 		} else /* common case, do nothing */
217e01f3a1aSJoel Fernandes (Google) 			return;
218e01f3a1aSJoel Fernandes (Google) 	}
219e01f3a1aSJoel Fernandes (Google) }
220e01f3a1aSJoel Fernandes (Google) 
torture_rt_boost(struct torture_random_state * trsp)221e01f3a1aSJoel Fernandes (Google) static void torture_rt_boost(struct torture_random_state *trsp)
222e01f3a1aSJoel Fernandes (Google) {
223e01f3a1aSJoel Fernandes (Google) 	if (rt_boost != 2)
224e01f3a1aSJoel Fernandes (Google) 		return;
225e01f3a1aSJoel Fernandes (Google) 
226e01f3a1aSJoel Fernandes (Google) 	__torture_rt_boost(trsp);
227095777c4SDavidlohr Bueso }
228095777c4SDavidlohr Bueso 
229e086481bSPaul E. McKenney static struct lock_torture_ops lock_busted_ops = {
230e086481bSPaul E. McKenney 	.writelock	= torture_lock_busted_write_lock,
231e086481bSPaul E. McKenney 	.write_delay	= torture_lock_busted_write_delay,
232e01f3a1aSJoel Fernandes (Google) 	.task_boost     = torture_rt_boost,
233e086481bSPaul E. McKenney 	.writeunlock	= torture_lock_busted_write_unlock,
2344f6332c1SDavidlohr Bueso 	.readlock       = NULL,
2354f6332c1SDavidlohr Bueso 	.read_delay     = NULL,
2364f6332c1SDavidlohr Bueso 	.readunlock     = NULL,
237e086481bSPaul E. McKenney 	.name		= "lock_busted"
238e086481bSPaul E. McKenney };
239e086481bSPaul E. McKenney 
2400af3fe1eSPaul E. McKenney static DEFINE_SPINLOCK(torture_spinlock);
2410af3fe1eSPaul E. McKenney 
torture_spin_lock_write_lock(int tid __maybe_unused)242aa3a5f31SWaiman Long static int torture_spin_lock_write_lock(int tid __maybe_unused)
243aa3a5f31SWaiman Long __acquires(torture_spinlock)
2440af3fe1eSPaul E. McKenney {
2450af3fe1eSPaul E. McKenney 	spin_lock(&torture_spinlock);
2460af3fe1eSPaul E. McKenney 	return 0;
2470af3fe1eSPaul E. McKenney }
2480af3fe1eSPaul E. McKenney 
torture_spin_lock_write_delay(struct torture_random_state * trsp)2490af3fe1eSPaul E. McKenney static void torture_spin_lock_write_delay(struct torture_random_state *trsp)
2500af3fe1eSPaul E. McKenney {
2510af3fe1eSPaul E. McKenney 	const unsigned long shortdelay_us = 2;
252f8619c30SPaul E. McKenney 	unsigned long j;
2530af3fe1eSPaul E. McKenney 
2540af3fe1eSPaul E. McKenney 	/* We want a short delay mostly to emulate likely code, and
2550af3fe1eSPaul E. McKenney 	 * we want a long delay occasionally to force massive contention.
2560af3fe1eSPaul E. McKenney 	 */
25700c24c9cSPaul E. McKenney 	if (long_hold && !(torture_random(trsp) % (cxt.nrealwriters_stress * 2000 * long_hold))) {
258f8619c30SPaul E. McKenney 		j = jiffies;
25900c24c9cSPaul E. McKenney 		mdelay(long_hold);
260f8619c30SPaul E. McKenney 		pr_alert("%s: delay = %lu jiffies.\n", __func__, jiffies - j);
261f8619c30SPaul E. McKenney 	}
262f8619c30SPaul E. McKenney 	if (!(torture_random(trsp) % (cxt.nrealwriters_stress * 200 * shortdelay_us)))
2630af3fe1eSPaul E. McKenney 		udelay(shortdelay_us);
264630952c2SDavidlohr Bueso 	if (!(torture_random(trsp) % (cxt.nrealwriters_stress * 20000)))
265cc1321c9SPaul E. McKenney 		torture_preempt_schedule();  /* Allow test to be preempted. */
2660af3fe1eSPaul E. McKenney }
2670af3fe1eSPaul E. McKenney 
torture_spin_lock_write_unlock(int tid __maybe_unused)268aa3a5f31SWaiman Long static void torture_spin_lock_write_unlock(int tid __maybe_unused)
269aa3a5f31SWaiman Long __releases(torture_spinlock)
2700af3fe1eSPaul E. McKenney {
2710af3fe1eSPaul E. McKenney 	spin_unlock(&torture_spinlock);
2720af3fe1eSPaul E. McKenney }
2730af3fe1eSPaul E. McKenney 
2740af3fe1eSPaul E. McKenney static struct lock_torture_ops spin_lock_ops = {
2750af3fe1eSPaul E. McKenney 	.writelock	= torture_spin_lock_write_lock,
2760af3fe1eSPaul E. McKenney 	.write_delay	= torture_spin_lock_write_delay,
277e01f3a1aSJoel Fernandes (Google) 	.task_boost     = torture_rt_boost,
2780af3fe1eSPaul E. McKenney 	.writeunlock	= torture_spin_lock_write_unlock,
2794f6332c1SDavidlohr Bueso 	.readlock       = NULL,
2804f6332c1SDavidlohr Bueso 	.read_delay     = NULL,
2814f6332c1SDavidlohr Bueso 	.readunlock     = NULL,
2820af3fe1eSPaul E. McKenney 	.name		= "spin_lock"
2830af3fe1eSPaul E. McKenney };
2840af3fe1eSPaul E. McKenney 
torture_spin_lock_write_lock_irq(int tid __maybe_unused)285aa3a5f31SWaiman Long static int torture_spin_lock_write_lock_irq(int tid __maybe_unused)
286219f800fSDavidlohr Bueso __acquires(torture_spinlock)
2870af3fe1eSPaul E. McKenney {
2880af3fe1eSPaul E. McKenney 	unsigned long flags;
2890af3fe1eSPaul E. McKenney 
2900af3fe1eSPaul E. McKenney 	spin_lock_irqsave(&torture_spinlock, flags);
291630952c2SDavidlohr Bueso 	cxt.cur_ops->flags = flags;
2920af3fe1eSPaul E. McKenney 	return 0;
2930af3fe1eSPaul E. McKenney }
2940af3fe1eSPaul E. McKenney 
torture_lock_spin_write_unlock_irq(int tid __maybe_unused)295aa3a5f31SWaiman Long static void torture_lock_spin_write_unlock_irq(int tid __maybe_unused)
2960af3fe1eSPaul E. McKenney __releases(torture_spinlock)
2970af3fe1eSPaul E. McKenney {
298630952c2SDavidlohr Bueso 	spin_unlock_irqrestore(&torture_spinlock, cxt.cur_ops->flags);
2990af3fe1eSPaul E. McKenney }
3000af3fe1eSPaul E. McKenney 
3010af3fe1eSPaul E. McKenney static struct lock_torture_ops spin_lock_irq_ops = {
3020af3fe1eSPaul E. McKenney 	.writelock	= torture_spin_lock_write_lock_irq,
3030af3fe1eSPaul E. McKenney 	.write_delay	= torture_spin_lock_write_delay,
304e01f3a1aSJoel Fernandes (Google) 	.task_boost     = torture_rt_boost,
3050af3fe1eSPaul E. McKenney 	.writeunlock	= torture_lock_spin_write_unlock_irq,
3064f6332c1SDavidlohr Bueso 	.readlock       = NULL,
3074f6332c1SDavidlohr Bueso 	.read_delay     = NULL,
3084f6332c1SDavidlohr Bueso 	.readunlock     = NULL,
3090af3fe1eSPaul E. McKenney 	.name		= "spin_lock_irq"
3100af3fe1eSPaul E. McKenney };
3110af3fe1eSPaul E. McKenney 
3125d65cf6aSZqiang static DEFINE_RAW_SPINLOCK(torture_raw_spinlock);
3135d65cf6aSZqiang 
torture_raw_spin_lock_write_lock(int tid __maybe_unused)3145d65cf6aSZqiang static int torture_raw_spin_lock_write_lock(int tid __maybe_unused)
3155d65cf6aSZqiang __acquires(torture_raw_spinlock)
3165d65cf6aSZqiang {
3175d65cf6aSZqiang 	raw_spin_lock(&torture_raw_spinlock);
3185d65cf6aSZqiang 	return 0;
3195d65cf6aSZqiang }
3205d65cf6aSZqiang 
torture_raw_spin_lock_write_unlock(int tid __maybe_unused)3215d65cf6aSZqiang static void torture_raw_spin_lock_write_unlock(int tid __maybe_unused)
3225d65cf6aSZqiang __releases(torture_raw_spinlock)
3235d65cf6aSZqiang {
3245d65cf6aSZqiang 	raw_spin_unlock(&torture_raw_spinlock);
3255d65cf6aSZqiang }
3265d65cf6aSZqiang 
3275d65cf6aSZqiang static struct lock_torture_ops raw_spin_lock_ops = {
3285d65cf6aSZqiang 	.writelock	= torture_raw_spin_lock_write_lock,
3295d65cf6aSZqiang 	.write_delay	= torture_spin_lock_write_delay,
3305d65cf6aSZqiang 	.task_boost	= torture_rt_boost,
3315d65cf6aSZqiang 	.writeunlock	= torture_raw_spin_lock_write_unlock,
3325d65cf6aSZqiang 	.readlock	= NULL,
3335d65cf6aSZqiang 	.read_delay	= NULL,
3345d65cf6aSZqiang 	.readunlock	= NULL,
3355d65cf6aSZqiang 	.name		= "raw_spin_lock"
3365d65cf6aSZqiang };
3375d65cf6aSZqiang 
torture_raw_spin_lock_write_lock_irq(int tid __maybe_unused)3385d65cf6aSZqiang static int torture_raw_spin_lock_write_lock_irq(int tid __maybe_unused)
3395d65cf6aSZqiang __acquires(torture_raw_spinlock)
3405d65cf6aSZqiang {
3415d65cf6aSZqiang 	unsigned long flags;
3425d65cf6aSZqiang 
3435d65cf6aSZqiang 	raw_spin_lock_irqsave(&torture_raw_spinlock, flags);
3445d65cf6aSZqiang 	cxt.cur_ops->flags = flags;
3455d65cf6aSZqiang 	return 0;
3465d65cf6aSZqiang }
3475d65cf6aSZqiang 
torture_raw_spin_lock_write_unlock_irq(int tid __maybe_unused)3485d65cf6aSZqiang static void torture_raw_spin_lock_write_unlock_irq(int tid __maybe_unused)
3495d65cf6aSZqiang __releases(torture_raw_spinlock)
3505d65cf6aSZqiang {
3515d65cf6aSZqiang 	raw_spin_unlock_irqrestore(&torture_raw_spinlock, cxt.cur_ops->flags);
3525d65cf6aSZqiang }
3535d65cf6aSZqiang 
3545d65cf6aSZqiang static struct lock_torture_ops raw_spin_lock_irq_ops = {
3555d65cf6aSZqiang 	.writelock	= torture_raw_spin_lock_write_lock_irq,
3565d65cf6aSZqiang 	.write_delay	= torture_spin_lock_write_delay,
3575d65cf6aSZqiang 	.task_boost	= torture_rt_boost,
3585d65cf6aSZqiang 	.writeunlock	= torture_raw_spin_lock_write_unlock_irq,
3595d65cf6aSZqiang 	.readlock	= NULL,
3605d65cf6aSZqiang 	.read_delay	= NULL,
3615d65cf6aSZqiang 	.readunlock	= NULL,
3625d65cf6aSZqiang 	.name		= "raw_spin_lock_irq"
3635d65cf6aSZqiang };
3645d65cf6aSZqiang 
365*a6884f6fSKumar Kartikeya Dwivedi #ifdef CONFIG_BPF_SYSCALL
366*a6884f6fSKumar Kartikeya Dwivedi 
367*a6884f6fSKumar Kartikeya Dwivedi #include <asm/rqspinlock.h>
368*a6884f6fSKumar Kartikeya Dwivedi static rqspinlock_t rqspinlock;
369*a6884f6fSKumar Kartikeya Dwivedi 
torture_raw_res_spin_write_lock(int tid __maybe_unused)370*a6884f6fSKumar Kartikeya Dwivedi static int torture_raw_res_spin_write_lock(int tid __maybe_unused)
371*a6884f6fSKumar Kartikeya Dwivedi {
372*a6884f6fSKumar Kartikeya Dwivedi 	raw_res_spin_lock(&rqspinlock);
373*a6884f6fSKumar Kartikeya Dwivedi 	return 0;
374*a6884f6fSKumar Kartikeya Dwivedi }
375*a6884f6fSKumar Kartikeya Dwivedi 
torture_raw_res_spin_write_unlock(int tid __maybe_unused)376*a6884f6fSKumar Kartikeya Dwivedi static void torture_raw_res_spin_write_unlock(int tid __maybe_unused)
377*a6884f6fSKumar Kartikeya Dwivedi {
378*a6884f6fSKumar Kartikeya Dwivedi 	raw_res_spin_unlock(&rqspinlock);
379*a6884f6fSKumar Kartikeya Dwivedi }
380*a6884f6fSKumar Kartikeya Dwivedi 
381*a6884f6fSKumar Kartikeya Dwivedi static struct lock_torture_ops raw_res_spin_lock_ops = {
382*a6884f6fSKumar Kartikeya Dwivedi 	.writelock	= torture_raw_res_spin_write_lock,
383*a6884f6fSKumar Kartikeya Dwivedi 	.write_delay	= torture_spin_lock_write_delay,
384*a6884f6fSKumar Kartikeya Dwivedi 	.task_boost     = torture_rt_boost,
385*a6884f6fSKumar Kartikeya Dwivedi 	.writeunlock	= torture_raw_res_spin_write_unlock,
386*a6884f6fSKumar Kartikeya Dwivedi 	.readlock       = NULL,
387*a6884f6fSKumar Kartikeya Dwivedi 	.read_delay     = NULL,
388*a6884f6fSKumar Kartikeya Dwivedi 	.readunlock     = NULL,
389*a6884f6fSKumar Kartikeya Dwivedi 	.name		= "raw_res_spin_lock"
390*a6884f6fSKumar Kartikeya Dwivedi };
391*a6884f6fSKumar Kartikeya Dwivedi 
torture_raw_res_spin_write_lock_irq(int tid __maybe_unused)392*a6884f6fSKumar Kartikeya Dwivedi static int torture_raw_res_spin_write_lock_irq(int tid __maybe_unused)
393*a6884f6fSKumar Kartikeya Dwivedi {
394*a6884f6fSKumar Kartikeya Dwivedi 	unsigned long flags;
395*a6884f6fSKumar Kartikeya Dwivedi 
396*a6884f6fSKumar Kartikeya Dwivedi 	raw_res_spin_lock_irqsave(&rqspinlock, flags);
397*a6884f6fSKumar Kartikeya Dwivedi 	cxt.cur_ops->flags = flags;
398*a6884f6fSKumar Kartikeya Dwivedi 	return 0;
399*a6884f6fSKumar Kartikeya Dwivedi }
400*a6884f6fSKumar Kartikeya Dwivedi 
torture_raw_res_spin_write_unlock_irq(int tid __maybe_unused)401*a6884f6fSKumar Kartikeya Dwivedi static void torture_raw_res_spin_write_unlock_irq(int tid __maybe_unused)
402*a6884f6fSKumar Kartikeya Dwivedi {
403*a6884f6fSKumar Kartikeya Dwivedi 	raw_res_spin_unlock_irqrestore(&rqspinlock, cxt.cur_ops->flags);
404*a6884f6fSKumar Kartikeya Dwivedi }
405*a6884f6fSKumar Kartikeya Dwivedi 
406*a6884f6fSKumar Kartikeya Dwivedi static struct lock_torture_ops raw_res_spin_lock_irq_ops = {
407*a6884f6fSKumar Kartikeya Dwivedi 	.writelock	= torture_raw_res_spin_write_lock_irq,
408*a6884f6fSKumar Kartikeya Dwivedi 	.write_delay	= torture_spin_lock_write_delay,
409*a6884f6fSKumar Kartikeya Dwivedi 	.task_boost     = torture_rt_boost,
410*a6884f6fSKumar Kartikeya Dwivedi 	.writeunlock	= torture_raw_res_spin_write_unlock_irq,
411*a6884f6fSKumar Kartikeya Dwivedi 	.readlock       = NULL,
412*a6884f6fSKumar Kartikeya Dwivedi 	.read_delay     = NULL,
413*a6884f6fSKumar Kartikeya Dwivedi 	.readunlock     = NULL,
414*a6884f6fSKumar Kartikeya Dwivedi 	.name		= "raw_res_spin_lock_irq"
415*a6884f6fSKumar Kartikeya Dwivedi };
416*a6884f6fSKumar Kartikeya Dwivedi 
417*a6884f6fSKumar Kartikeya Dwivedi #endif
418*a6884f6fSKumar Kartikeya Dwivedi 
419e34191faSDavidlohr Bueso static DEFINE_RWLOCK(torture_rwlock);
420e34191faSDavidlohr Bueso 
torture_rwlock_write_lock(int tid __maybe_unused)421aa3a5f31SWaiman Long static int torture_rwlock_write_lock(int tid __maybe_unused)
422aa3a5f31SWaiman Long __acquires(torture_rwlock)
423e34191faSDavidlohr Bueso {
424e34191faSDavidlohr Bueso 	write_lock(&torture_rwlock);
425e34191faSDavidlohr Bueso 	return 0;
426e34191faSDavidlohr Bueso }
427e34191faSDavidlohr Bueso 
torture_rwlock_write_delay(struct torture_random_state * trsp)428e34191faSDavidlohr Bueso static void torture_rwlock_write_delay(struct torture_random_state *trsp)
429e34191faSDavidlohr Bueso {
430e34191faSDavidlohr Bueso 	const unsigned long shortdelay_us = 2;
431e34191faSDavidlohr Bueso 
432e34191faSDavidlohr Bueso 	/* We want a short delay mostly to emulate likely code, and
433e34191faSDavidlohr Bueso 	 * we want a long delay occasionally to force massive contention.
434e34191faSDavidlohr Bueso 	 */
43500c24c9cSPaul E. McKenney 	if (long_hold && !(torture_random(trsp) % (cxt.nrealwriters_stress * 2000 * long_hold)))
43600c24c9cSPaul E. McKenney 		mdelay(long_hold);
437e34191faSDavidlohr Bueso 	else
438e34191faSDavidlohr Bueso 		udelay(shortdelay_us);
439e34191faSDavidlohr Bueso }
440e34191faSDavidlohr Bueso 
torture_rwlock_write_unlock(int tid __maybe_unused)441aa3a5f31SWaiman Long static void torture_rwlock_write_unlock(int tid __maybe_unused)
442aa3a5f31SWaiman Long __releases(torture_rwlock)
443e34191faSDavidlohr Bueso {
444e34191faSDavidlohr Bueso 	write_unlock(&torture_rwlock);
445e34191faSDavidlohr Bueso }
446e34191faSDavidlohr Bueso 
torture_rwlock_read_lock(int tid __maybe_unused)447aa3a5f31SWaiman Long static int torture_rwlock_read_lock(int tid __maybe_unused)
448aa3a5f31SWaiman Long __acquires(torture_rwlock)
449e34191faSDavidlohr Bueso {
450e34191faSDavidlohr Bueso 	read_lock(&torture_rwlock);
451e34191faSDavidlohr Bueso 	return 0;
452e34191faSDavidlohr Bueso }
453e34191faSDavidlohr Bueso 
torture_rwlock_read_delay(struct torture_random_state * trsp)454e34191faSDavidlohr Bueso static void torture_rwlock_read_delay(struct torture_random_state *trsp)
455e34191faSDavidlohr Bueso {
456e34191faSDavidlohr Bueso 	const unsigned long shortdelay_us = 10;
457e34191faSDavidlohr Bueso 
458e34191faSDavidlohr Bueso 	/* We want a short delay mostly to emulate likely code, and
459e34191faSDavidlohr Bueso 	 * we want a long delay occasionally to force massive contention.
460e34191faSDavidlohr Bueso 	 */
46100c24c9cSPaul E. McKenney 	if (long_hold && !(torture_random(trsp) % (cxt.nrealreaders_stress * 2000 * long_hold)))
46200c24c9cSPaul E. McKenney 		mdelay(long_hold);
463e34191faSDavidlohr Bueso 	else
464e34191faSDavidlohr Bueso 		udelay(shortdelay_us);
465e34191faSDavidlohr Bueso }
466e34191faSDavidlohr Bueso 
torture_rwlock_read_unlock(int tid __maybe_unused)467aa3a5f31SWaiman Long static void torture_rwlock_read_unlock(int tid __maybe_unused)
468aa3a5f31SWaiman Long __releases(torture_rwlock)
469e34191faSDavidlohr Bueso {
470e34191faSDavidlohr Bueso 	read_unlock(&torture_rwlock);
471e34191faSDavidlohr Bueso }
472e34191faSDavidlohr Bueso 
473e34191faSDavidlohr Bueso static struct lock_torture_ops rw_lock_ops = {
474e34191faSDavidlohr Bueso 	.writelock	= torture_rwlock_write_lock,
475e34191faSDavidlohr Bueso 	.write_delay	= torture_rwlock_write_delay,
476e01f3a1aSJoel Fernandes (Google) 	.task_boost     = torture_rt_boost,
477e34191faSDavidlohr Bueso 	.writeunlock	= torture_rwlock_write_unlock,
478e34191faSDavidlohr Bueso 	.readlock       = torture_rwlock_read_lock,
479e34191faSDavidlohr Bueso 	.read_delay     = torture_rwlock_read_delay,
480e34191faSDavidlohr Bueso 	.readunlock     = torture_rwlock_read_unlock,
481e34191faSDavidlohr Bueso 	.name		= "rw_lock"
482e34191faSDavidlohr Bueso };
483e34191faSDavidlohr Bueso 
torture_rwlock_write_lock_irq(int tid __maybe_unused)484aa3a5f31SWaiman Long static int torture_rwlock_write_lock_irq(int tid __maybe_unused)
485aa3a5f31SWaiman Long __acquires(torture_rwlock)
486e34191faSDavidlohr Bueso {
487e34191faSDavidlohr Bueso 	unsigned long flags;
488e34191faSDavidlohr Bueso 
489e34191faSDavidlohr Bueso 	write_lock_irqsave(&torture_rwlock, flags);
490e34191faSDavidlohr Bueso 	cxt.cur_ops->flags = flags;
491e34191faSDavidlohr Bueso 	return 0;
492e34191faSDavidlohr Bueso }
493e34191faSDavidlohr Bueso 
torture_rwlock_write_unlock_irq(int tid __maybe_unused)494aa3a5f31SWaiman Long static void torture_rwlock_write_unlock_irq(int tid __maybe_unused)
495e34191faSDavidlohr Bueso __releases(torture_rwlock)
496e34191faSDavidlohr Bueso {
497e34191faSDavidlohr Bueso 	write_unlock_irqrestore(&torture_rwlock, cxt.cur_ops->flags);
498e34191faSDavidlohr Bueso }
499e34191faSDavidlohr Bueso 
torture_rwlock_read_lock_irq(int tid __maybe_unused)500aa3a5f31SWaiman Long static int torture_rwlock_read_lock_irq(int tid __maybe_unused)
501aa3a5f31SWaiman Long __acquires(torture_rwlock)
502e34191faSDavidlohr Bueso {
503e34191faSDavidlohr Bueso 	unsigned long flags;
504e34191faSDavidlohr Bueso 
505e34191faSDavidlohr Bueso 	read_lock_irqsave(&torture_rwlock, flags);
506e34191faSDavidlohr Bueso 	cxt.cur_ops->flags = flags;
507e34191faSDavidlohr Bueso 	return 0;
508e34191faSDavidlohr Bueso }
509e34191faSDavidlohr Bueso 
torture_rwlock_read_unlock_irq(int tid __maybe_unused)510aa3a5f31SWaiman Long static void torture_rwlock_read_unlock_irq(int tid __maybe_unused)
511e34191faSDavidlohr Bueso __releases(torture_rwlock)
512e34191faSDavidlohr Bueso {
513f548d99eSAlexey Kodanev 	read_unlock_irqrestore(&torture_rwlock, cxt.cur_ops->flags);
514e34191faSDavidlohr Bueso }
515e34191faSDavidlohr Bueso 
516e34191faSDavidlohr Bueso static struct lock_torture_ops rw_lock_irq_ops = {
517e34191faSDavidlohr Bueso 	.writelock	= torture_rwlock_write_lock_irq,
518e34191faSDavidlohr Bueso 	.write_delay	= torture_rwlock_write_delay,
519e01f3a1aSJoel Fernandes (Google) 	.task_boost     = torture_rt_boost,
520e34191faSDavidlohr Bueso 	.writeunlock	= torture_rwlock_write_unlock_irq,
521e34191faSDavidlohr Bueso 	.readlock       = torture_rwlock_read_lock_irq,
522e34191faSDavidlohr Bueso 	.read_delay     = torture_rwlock_read_delay,
523e34191faSDavidlohr Bueso 	.readunlock     = torture_rwlock_read_unlock_irq,
524e34191faSDavidlohr Bueso 	.name		= "rw_lock_irq"
525e34191faSDavidlohr Bueso };
526e34191faSDavidlohr Bueso 
52742ddc75dSDavidlohr Bueso static DEFINE_MUTEX(torture_mutex);
5283e5aeaf5SJohn Stultz static struct mutex torture_nested_mutexes[MAX_NESTED_LOCKS];
5293e5aeaf5SJohn Stultz static struct lock_class_key nested_mutex_keys[MAX_NESTED_LOCKS];
5303e5aeaf5SJohn Stultz 
torture_mutex_init(void)5313e5aeaf5SJohn Stultz static void torture_mutex_init(void)
5323e5aeaf5SJohn Stultz {
5333e5aeaf5SJohn Stultz 	int i;
5343e5aeaf5SJohn Stultz 
5353e5aeaf5SJohn Stultz 	for (i = 0; i < MAX_NESTED_LOCKS; i++)
5363e5aeaf5SJohn Stultz 		__mutex_init(&torture_nested_mutexes[i], __func__,
5373e5aeaf5SJohn Stultz 			     &nested_mutex_keys[i]);
5383e5aeaf5SJohn Stultz }
5393e5aeaf5SJohn Stultz 
torture_mutex_nested_lock(int tid __maybe_unused,u32 lockset)5403e5aeaf5SJohn Stultz static int torture_mutex_nested_lock(int tid __maybe_unused,
5413e5aeaf5SJohn Stultz 				     u32 lockset)
5423e5aeaf5SJohn Stultz {
5433e5aeaf5SJohn Stultz 	int i;
5443e5aeaf5SJohn Stultz 
5453e5aeaf5SJohn Stultz 	for (i = 0; i < nested_locks; i++)
5463e5aeaf5SJohn Stultz 		if (lockset & (1 << i))
5473e5aeaf5SJohn Stultz 			mutex_lock(&torture_nested_mutexes[i]);
5483e5aeaf5SJohn Stultz 	return 0;
5493e5aeaf5SJohn Stultz }
55042ddc75dSDavidlohr Bueso 
torture_mutex_lock(int tid __maybe_unused)551aa3a5f31SWaiman Long static int torture_mutex_lock(int tid __maybe_unused)
552aa3a5f31SWaiman Long __acquires(torture_mutex)
55342ddc75dSDavidlohr Bueso {
55442ddc75dSDavidlohr Bueso 	mutex_lock(&torture_mutex);
55542ddc75dSDavidlohr Bueso 	return 0;
55642ddc75dSDavidlohr Bueso }
55742ddc75dSDavidlohr Bueso 
torture_mutex_delay(struct torture_random_state * trsp)55842ddc75dSDavidlohr Bueso static void torture_mutex_delay(struct torture_random_state *trsp)
55942ddc75dSDavidlohr Bueso {
56042ddc75dSDavidlohr Bueso 	/* We want a long delay occasionally to force massive contention.  */
56100c24c9cSPaul E. McKenney 	if (long_hold && !(torture_random(trsp) % (cxt.nrealwriters_stress * 2000 * long_hold)))
56200c24c9cSPaul E. McKenney 		mdelay(long_hold * 5);
563630952c2SDavidlohr Bueso 	if (!(torture_random(trsp) % (cxt.nrealwriters_stress * 20000)))
564cc1321c9SPaul E. McKenney 		torture_preempt_schedule();  /* Allow test to be preempted. */
56542ddc75dSDavidlohr Bueso }
56642ddc75dSDavidlohr Bueso 
torture_mutex_unlock(int tid __maybe_unused)567aa3a5f31SWaiman Long static void torture_mutex_unlock(int tid __maybe_unused)
568aa3a5f31SWaiman Long __releases(torture_mutex)
56942ddc75dSDavidlohr Bueso {
57042ddc75dSDavidlohr Bueso 	mutex_unlock(&torture_mutex);
57142ddc75dSDavidlohr Bueso }
57242ddc75dSDavidlohr Bueso 
torture_mutex_nested_unlock(int tid __maybe_unused,u32 lockset)5733e5aeaf5SJohn Stultz static void torture_mutex_nested_unlock(int tid __maybe_unused,
5743e5aeaf5SJohn Stultz 					u32 lockset)
5753e5aeaf5SJohn Stultz {
5763e5aeaf5SJohn Stultz 	int i;
5773e5aeaf5SJohn Stultz 
5783e5aeaf5SJohn Stultz 	for (i = nested_locks - 1; i >= 0; i--)
5793e5aeaf5SJohn Stultz 		if (lockset & (1 << i))
5803e5aeaf5SJohn Stultz 			mutex_unlock(&torture_nested_mutexes[i]);
5813e5aeaf5SJohn Stultz }
5823e5aeaf5SJohn Stultz 
58342ddc75dSDavidlohr Bueso static struct lock_torture_ops mutex_lock_ops = {
5843e5aeaf5SJohn Stultz 	.init		= torture_mutex_init,
5853e5aeaf5SJohn Stultz 	.nested_lock	= torture_mutex_nested_lock,
58642ddc75dSDavidlohr Bueso 	.writelock	= torture_mutex_lock,
58742ddc75dSDavidlohr Bueso 	.write_delay	= torture_mutex_delay,
588e01f3a1aSJoel Fernandes (Google) 	.task_boost     = torture_rt_boost,
58942ddc75dSDavidlohr Bueso 	.writeunlock	= torture_mutex_unlock,
5903e5aeaf5SJohn Stultz 	.nested_unlock	= torture_mutex_nested_unlock,
5914f6332c1SDavidlohr Bueso 	.readlock       = NULL,
5924f6332c1SDavidlohr Bueso 	.read_delay     = NULL,
5934f6332c1SDavidlohr Bueso 	.readunlock     = NULL,
59442ddc75dSDavidlohr Bueso 	.name		= "mutex_lock"
59542ddc75dSDavidlohr Bueso };
59642ddc75dSDavidlohr Bueso 
5970186a6cbSChris Wilson #include <linux/ww_mutex.h>
5982ea55bbbSWaiman Long /*
5992ea55bbbSWaiman Long  * The torture ww_mutexes should belong to the same lock class as
6002ea55bbbSWaiman Long  * torture_ww_class to avoid lockdep problem. The ww_mutex_init()
6012ea55bbbSWaiman Long  * function is called for initialization to ensure that.
6022ea55bbbSWaiman Long  */
60308295b3bSThomas Hellstrom static DEFINE_WD_CLASS(torture_ww_class);
6042ea55bbbSWaiman Long static struct ww_mutex torture_ww_mutex_0, torture_ww_mutex_1, torture_ww_mutex_2;
6058c52cca0SWaiman Long static struct ww_acquire_ctx *ww_acquire_ctxs;
6062ea55bbbSWaiman Long 
torture_ww_mutex_init(void)6072ea55bbbSWaiman Long static void torture_ww_mutex_init(void)
6082ea55bbbSWaiman Long {
6092ea55bbbSWaiman Long 	ww_mutex_init(&torture_ww_mutex_0, &torture_ww_class);
6102ea55bbbSWaiman Long 	ww_mutex_init(&torture_ww_mutex_1, &torture_ww_class);
6112ea55bbbSWaiman Long 	ww_mutex_init(&torture_ww_mutex_2, &torture_ww_class);
6128c52cca0SWaiman Long 
6138c52cca0SWaiman Long 	ww_acquire_ctxs = kmalloc_array(cxt.nrealwriters_stress,
6148c52cca0SWaiman Long 					sizeof(*ww_acquire_ctxs),
6158c52cca0SWaiman Long 					GFP_KERNEL);
6168c52cca0SWaiman Long 	if (!ww_acquire_ctxs)
6178c52cca0SWaiman Long 		VERBOSE_TOROUT_STRING("ww_acquire_ctx: Out of memory");
6182ea55bbbSWaiman Long }
6190186a6cbSChris Wilson 
torture_ww_mutex_exit(void)6208c52cca0SWaiman Long static void torture_ww_mutex_exit(void)
6218c52cca0SWaiman Long {
6228c52cca0SWaiman Long 	kfree(ww_acquire_ctxs);
6238c52cca0SWaiman Long }
6248c52cca0SWaiman Long 
torture_ww_mutex_lock(int tid)6258c52cca0SWaiman Long static int torture_ww_mutex_lock(int tid)
6260186a6cbSChris Wilson __acquires(torture_ww_mutex_0)
6270186a6cbSChris Wilson __acquires(torture_ww_mutex_1)
6280186a6cbSChris Wilson __acquires(torture_ww_mutex_2)
6290186a6cbSChris Wilson {
6300186a6cbSChris Wilson 	LIST_HEAD(list);
6310186a6cbSChris Wilson 	struct reorder_lock {
6320186a6cbSChris Wilson 		struct list_head link;
6330186a6cbSChris Wilson 		struct ww_mutex *lock;
6340186a6cbSChris Wilson 	} locks[3], *ll, *ln;
6358c52cca0SWaiman Long 	struct ww_acquire_ctx *ctx = &ww_acquire_ctxs[tid];
6360186a6cbSChris Wilson 
6370186a6cbSChris Wilson 	locks[0].lock = &torture_ww_mutex_0;
6380186a6cbSChris Wilson 	list_add(&locks[0].link, &list);
6390186a6cbSChris Wilson 
6400186a6cbSChris Wilson 	locks[1].lock = &torture_ww_mutex_1;
6410186a6cbSChris Wilson 	list_add(&locks[1].link, &list);
6420186a6cbSChris Wilson 
6430186a6cbSChris Wilson 	locks[2].lock = &torture_ww_mutex_2;
6440186a6cbSChris Wilson 	list_add(&locks[2].link, &list);
6450186a6cbSChris Wilson 
6468c52cca0SWaiman Long 	ww_acquire_init(ctx, &torture_ww_class);
6470186a6cbSChris Wilson 
6480186a6cbSChris Wilson 	list_for_each_entry(ll, &list, link) {
6490186a6cbSChris Wilson 		int err;
6500186a6cbSChris Wilson 
6518c52cca0SWaiman Long 		err = ww_mutex_lock(ll->lock, ctx);
6520186a6cbSChris Wilson 		if (!err)
6530186a6cbSChris Wilson 			continue;
6540186a6cbSChris Wilson 
6550186a6cbSChris Wilson 		ln = ll;
6560186a6cbSChris Wilson 		list_for_each_entry_continue_reverse(ln, &list, link)
6570186a6cbSChris Wilson 			ww_mutex_unlock(ln->lock);
6580186a6cbSChris Wilson 
6590186a6cbSChris Wilson 		if (err != -EDEADLK)
6600186a6cbSChris Wilson 			return err;
6610186a6cbSChris Wilson 
6628c52cca0SWaiman Long 		ww_mutex_lock_slow(ll->lock, ctx);
6630186a6cbSChris Wilson 		list_move(&ll->link, &list);
6640186a6cbSChris Wilson 	}
6650186a6cbSChris Wilson 
6660186a6cbSChris Wilson 	return 0;
6670186a6cbSChris Wilson }
6680186a6cbSChris Wilson 
torture_ww_mutex_unlock(int tid)6698c52cca0SWaiman Long static void torture_ww_mutex_unlock(int tid)
6700186a6cbSChris Wilson __releases(torture_ww_mutex_0)
6710186a6cbSChris Wilson __releases(torture_ww_mutex_1)
6720186a6cbSChris Wilson __releases(torture_ww_mutex_2)
6730186a6cbSChris Wilson {
6748c52cca0SWaiman Long 	struct ww_acquire_ctx *ctx = &ww_acquire_ctxs[tid];
6758c52cca0SWaiman Long 
6760186a6cbSChris Wilson 	ww_mutex_unlock(&torture_ww_mutex_0);
6770186a6cbSChris Wilson 	ww_mutex_unlock(&torture_ww_mutex_1);
6780186a6cbSChris Wilson 	ww_mutex_unlock(&torture_ww_mutex_2);
6798c52cca0SWaiman Long 	ww_acquire_fini(ctx);
6800186a6cbSChris Wilson }
6810186a6cbSChris Wilson 
6820186a6cbSChris Wilson static struct lock_torture_ops ww_mutex_lock_ops = {
6832ea55bbbSWaiman Long 	.init		= torture_ww_mutex_init,
6848c52cca0SWaiman Long 	.exit		= torture_ww_mutex_exit,
6850186a6cbSChris Wilson 	.writelock	= torture_ww_mutex_lock,
6860186a6cbSChris Wilson 	.write_delay	= torture_mutex_delay,
687e01f3a1aSJoel Fernandes (Google) 	.task_boost     = torture_rt_boost,
6880186a6cbSChris Wilson 	.writeunlock	= torture_ww_mutex_unlock,
6890186a6cbSChris Wilson 	.readlock       = NULL,
6900186a6cbSChris Wilson 	.read_delay     = NULL,
6910186a6cbSChris Wilson 	.readunlock     = NULL,
6920186a6cbSChris Wilson 	.name		= "ww_mutex_lock"
6930186a6cbSChris Wilson };
6940186a6cbSChris Wilson 
695095777c4SDavidlohr Bueso #ifdef CONFIG_RT_MUTEXES
696095777c4SDavidlohr Bueso static DEFINE_RT_MUTEX(torture_rtmutex);
697ae4823e4SJohn Stultz static struct rt_mutex torture_nested_rtmutexes[MAX_NESTED_LOCKS];
698ae4823e4SJohn Stultz static struct lock_class_key nested_rtmutex_keys[MAX_NESTED_LOCKS];
699ae4823e4SJohn Stultz 
torture_rtmutex_init(void)700ae4823e4SJohn Stultz static void torture_rtmutex_init(void)
701ae4823e4SJohn Stultz {
702ae4823e4SJohn Stultz 	int i;
703ae4823e4SJohn Stultz 
704ae4823e4SJohn Stultz 	for (i = 0; i < MAX_NESTED_LOCKS; i++)
705ae4823e4SJohn Stultz 		__rt_mutex_init(&torture_nested_rtmutexes[i], __func__,
706ae4823e4SJohn Stultz 				&nested_rtmutex_keys[i]);
707ae4823e4SJohn Stultz }
708ae4823e4SJohn Stultz 
torture_rtmutex_nested_lock(int tid __maybe_unused,u32 lockset)709ae4823e4SJohn Stultz static int torture_rtmutex_nested_lock(int tid __maybe_unused,
710ae4823e4SJohn Stultz 				       u32 lockset)
711ae4823e4SJohn Stultz {
712ae4823e4SJohn Stultz 	int i;
713ae4823e4SJohn Stultz 
714ae4823e4SJohn Stultz 	for (i = 0; i < nested_locks; i++)
715ae4823e4SJohn Stultz 		if (lockset & (1 << i))
716ae4823e4SJohn Stultz 			rt_mutex_lock(&torture_nested_rtmutexes[i]);
717ae4823e4SJohn Stultz 	return 0;
718ae4823e4SJohn Stultz }
719095777c4SDavidlohr Bueso 
torture_rtmutex_lock(int tid __maybe_unused)720aa3a5f31SWaiman Long static int torture_rtmutex_lock(int tid __maybe_unused)
721aa3a5f31SWaiman Long __acquires(torture_rtmutex)
722095777c4SDavidlohr Bueso {
723095777c4SDavidlohr Bueso 	rt_mutex_lock(&torture_rtmutex);
724095777c4SDavidlohr Bueso 	return 0;
725095777c4SDavidlohr Bueso }
726095777c4SDavidlohr Bueso 
torture_rtmutex_delay(struct torture_random_state * trsp)727095777c4SDavidlohr Bueso static void torture_rtmutex_delay(struct torture_random_state *trsp)
728095777c4SDavidlohr Bueso {
729095777c4SDavidlohr Bueso 	const unsigned long shortdelay_us = 2;
730095777c4SDavidlohr Bueso 
731095777c4SDavidlohr Bueso 	/*
732095777c4SDavidlohr Bueso 	 * We want a short delay mostly to emulate likely code, and
733095777c4SDavidlohr Bueso 	 * we want a long delay occasionally to force massive contention.
734095777c4SDavidlohr Bueso 	 */
73500c24c9cSPaul E. McKenney 	if (long_hold && !(torture_random(trsp) % (cxt.nrealwriters_stress * 2000 * long_hold)))
73600c24c9cSPaul E. McKenney 		mdelay(long_hold);
737095777c4SDavidlohr Bueso 	if (!(torture_random(trsp) %
738f8619c30SPaul E. McKenney 	      (cxt.nrealwriters_stress * 200 * shortdelay_us)))
739095777c4SDavidlohr Bueso 		udelay(shortdelay_us);
740095777c4SDavidlohr Bueso 	if (!(torture_random(trsp) % (cxt.nrealwriters_stress * 20000)))
741cc1321c9SPaul E. McKenney 		torture_preempt_schedule();  /* Allow test to be preempted. */
742095777c4SDavidlohr Bueso }
743095777c4SDavidlohr Bueso 
torture_rtmutex_unlock(int tid __maybe_unused)744aa3a5f31SWaiman Long static void torture_rtmutex_unlock(int tid __maybe_unused)
745aa3a5f31SWaiman Long __releases(torture_rtmutex)
746095777c4SDavidlohr Bueso {
747095777c4SDavidlohr Bueso 	rt_mutex_unlock(&torture_rtmutex);
748095777c4SDavidlohr Bueso }
749095777c4SDavidlohr Bueso 
torture_rt_boost_rtmutex(struct torture_random_state * trsp)750e01f3a1aSJoel Fernandes (Google) static void torture_rt_boost_rtmutex(struct torture_random_state *trsp)
751e01f3a1aSJoel Fernandes (Google) {
752e01f3a1aSJoel Fernandes (Google) 	if (!rt_boost)
753e01f3a1aSJoel Fernandes (Google) 		return;
754e01f3a1aSJoel Fernandes (Google) 
755e01f3a1aSJoel Fernandes (Google) 	__torture_rt_boost(trsp);
756e01f3a1aSJoel Fernandes (Google) }
757e01f3a1aSJoel Fernandes (Google) 
torture_rtmutex_nested_unlock(int tid __maybe_unused,u32 lockset)758ae4823e4SJohn Stultz static void torture_rtmutex_nested_unlock(int tid __maybe_unused,
759ae4823e4SJohn Stultz 					  u32 lockset)
760ae4823e4SJohn Stultz {
761ae4823e4SJohn Stultz 	int i;
762ae4823e4SJohn Stultz 
763ae4823e4SJohn Stultz 	for (i = nested_locks - 1; i >= 0; i--)
764ae4823e4SJohn Stultz 		if (lockset & (1 << i))
765ae4823e4SJohn Stultz 			rt_mutex_unlock(&torture_nested_rtmutexes[i]);
766ae4823e4SJohn Stultz }
767ae4823e4SJohn Stultz 
768095777c4SDavidlohr Bueso static struct lock_torture_ops rtmutex_lock_ops = {
769ae4823e4SJohn Stultz 	.init		= torture_rtmutex_init,
770ae4823e4SJohn Stultz 	.nested_lock	= torture_rtmutex_nested_lock,
771095777c4SDavidlohr Bueso 	.writelock	= torture_rtmutex_lock,
772095777c4SDavidlohr Bueso 	.write_delay	= torture_rtmutex_delay,
773e01f3a1aSJoel Fernandes (Google) 	.task_boost     = torture_rt_boost_rtmutex,
774095777c4SDavidlohr Bueso 	.writeunlock	= torture_rtmutex_unlock,
775ae4823e4SJohn Stultz 	.nested_unlock	= torture_rtmutex_nested_unlock,
776095777c4SDavidlohr Bueso 	.readlock       = NULL,
777095777c4SDavidlohr Bueso 	.read_delay     = NULL,
778095777c4SDavidlohr Bueso 	.readunlock     = NULL,
779095777c4SDavidlohr Bueso 	.name		= "rtmutex_lock"
780095777c4SDavidlohr Bueso };
781095777c4SDavidlohr Bueso #endif
782095777c4SDavidlohr Bueso 
7834a3b427fSDavidlohr Bueso static DECLARE_RWSEM(torture_rwsem);
torture_rwsem_down_write(int tid __maybe_unused)784aa3a5f31SWaiman Long static int torture_rwsem_down_write(int tid __maybe_unused)
785aa3a5f31SWaiman Long __acquires(torture_rwsem)
7864a3b427fSDavidlohr Bueso {
7874a3b427fSDavidlohr Bueso 	down_write(&torture_rwsem);
7884a3b427fSDavidlohr Bueso 	return 0;
7894a3b427fSDavidlohr Bueso }
7904a3b427fSDavidlohr Bueso 
torture_rwsem_write_delay(struct torture_random_state * trsp)7914a3b427fSDavidlohr Bueso static void torture_rwsem_write_delay(struct torture_random_state *trsp)
7924a3b427fSDavidlohr Bueso {
7934a3b427fSDavidlohr Bueso 	/* We want a long delay occasionally to force massive contention.  */
79400c24c9cSPaul E. McKenney 	if (long_hold && !(torture_random(trsp) % (cxt.nrealwriters_stress * 2000 * long_hold)))
79500c24c9cSPaul E. McKenney 		mdelay(long_hold * 10);
796630952c2SDavidlohr Bueso 	if (!(torture_random(trsp) % (cxt.nrealwriters_stress * 20000)))
797cc1321c9SPaul E. McKenney 		torture_preempt_schedule();  /* Allow test to be preempted. */
7984a3b427fSDavidlohr Bueso }
7994a3b427fSDavidlohr Bueso 
torture_rwsem_up_write(int tid __maybe_unused)800aa3a5f31SWaiman Long static void torture_rwsem_up_write(int tid __maybe_unused)
801aa3a5f31SWaiman Long __releases(torture_rwsem)
8024a3b427fSDavidlohr Bueso {
8034a3b427fSDavidlohr Bueso 	up_write(&torture_rwsem);
8044a3b427fSDavidlohr Bueso }
8054a3b427fSDavidlohr Bueso 
torture_rwsem_down_read(int tid __maybe_unused)806aa3a5f31SWaiman Long static int torture_rwsem_down_read(int tid __maybe_unused)
807aa3a5f31SWaiman Long __acquires(torture_rwsem)
8084a3b427fSDavidlohr Bueso {
8094a3b427fSDavidlohr Bueso 	down_read(&torture_rwsem);
8104a3b427fSDavidlohr Bueso 	return 0;
8114a3b427fSDavidlohr Bueso }
8124a3b427fSDavidlohr Bueso 
torture_rwsem_read_delay(struct torture_random_state * trsp)8134a3b427fSDavidlohr Bueso static void torture_rwsem_read_delay(struct torture_random_state *trsp)
8144a3b427fSDavidlohr Bueso {
8154a3b427fSDavidlohr Bueso 	/* We want a long delay occasionally to force massive contention.  */
81600c24c9cSPaul E. McKenney 	if (long_hold && !(torture_random(trsp) % (cxt.nrealreaders_stress * 2000 * long_hold)))
81700c24c9cSPaul E. McKenney 		mdelay(long_hold * 2);
8184a3b427fSDavidlohr Bueso 	else
81900c24c9cSPaul E. McKenney 		mdelay(long_hold / 2);
820630952c2SDavidlohr Bueso 	if (!(torture_random(trsp) % (cxt.nrealreaders_stress * 20000)))
821cc1321c9SPaul E. McKenney 		torture_preempt_schedule();  /* Allow test to be preempted. */
8224a3b427fSDavidlohr Bueso }
8234a3b427fSDavidlohr Bueso 
torture_rwsem_up_read(int tid __maybe_unused)824aa3a5f31SWaiman Long static void torture_rwsem_up_read(int tid __maybe_unused)
825aa3a5f31SWaiman Long __releases(torture_rwsem)
8264a3b427fSDavidlohr Bueso {
8274a3b427fSDavidlohr Bueso 	up_read(&torture_rwsem);
8284a3b427fSDavidlohr Bueso }
8294a3b427fSDavidlohr Bueso 
8304a3b427fSDavidlohr Bueso static struct lock_torture_ops rwsem_lock_ops = {
8314a3b427fSDavidlohr Bueso 	.writelock	= torture_rwsem_down_write,
8324a3b427fSDavidlohr Bueso 	.write_delay	= torture_rwsem_write_delay,
833e01f3a1aSJoel Fernandes (Google) 	.task_boost     = torture_rt_boost,
8344a3b427fSDavidlohr Bueso 	.writeunlock	= torture_rwsem_up_write,
8354a3b427fSDavidlohr Bueso 	.readlock       = torture_rwsem_down_read,
8364a3b427fSDavidlohr Bueso 	.read_delay     = torture_rwsem_read_delay,
8374a3b427fSDavidlohr Bueso 	.readunlock     = torture_rwsem_up_read,
8384a3b427fSDavidlohr Bueso 	.name		= "rwsem_lock"
8394a3b427fSDavidlohr Bueso };
8404a3b427fSDavidlohr Bueso 
841617783ddSPaul E. McKenney #include <linux/percpu-rwsem.h>
842617783ddSPaul E. McKenney static struct percpu_rw_semaphore pcpu_rwsem;
843617783ddSPaul E. McKenney 
torture_percpu_rwsem_init(void)844d49bed9aSWei Yongjun static void torture_percpu_rwsem_init(void)
845617783ddSPaul E. McKenney {
846617783ddSPaul E. McKenney 	BUG_ON(percpu_init_rwsem(&pcpu_rwsem));
847617783ddSPaul E. McKenney }
848617783ddSPaul E. McKenney 
torture_percpu_rwsem_exit(void)8490d720287SHou Tao static void torture_percpu_rwsem_exit(void)
8500d720287SHou Tao {
8510d720287SHou Tao 	percpu_free_rwsem(&pcpu_rwsem);
8520d720287SHou Tao }
8530d720287SHou Tao 
torture_percpu_rwsem_down_write(int tid __maybe_unused)854aa3a5f31SWaiman Long static int torture_percpu_rwsem_down_write(int tid __maybe_unused)
855aa3a5f31SWaiman Long __acquires(pcpu_rwsem)
856617783ddSPaul E. McKenney {
857617783ddSPaul E. McKenney 	percpu_down_write(&pcpu_rwsem);
858617783ddSPaul E. McKenney 	return 0;
859617783ddSPaul E. McKenney }
860617783ddSPaul E. McKenney 
torture_percpu_rwsem_up_write(int tid __maybe_unused)861aa3a5f31SWaiman Long static void torture_percpu_rwsem_up_write(int tid __maybe_unused)
862aa3a5f31SWaiman Long __releases(pcpu_rwsem)
863617783ddSPaul E. McKenney {
864617783ddSPaul E. McKenney 	percpu_up_write(&pcpu_rwsem);
865617783ddSPaul E. McKenney }
866617783ddSPaul E. McKenney 
torture_percpu_rwsem_down_read(int tid __maybe_unused)867aa3a5f31SWaiman Long static int torture_percpu_rwsem_down_read(int tid __maybe_unused)
868aa3a5f31SWaiman Long __acquires(pcpu_rwsem)
869617783ddSPaul E. McKenney {
870617783ddSPaul E. McKenney 	percpu_down_read(&pcpu_rwsem);
871617783ddSPaul E. McKenney 	return 0;
872617783ddSPaul E. McKenney }
873617783ddSPaul E. McKenney 
torture_percpu_rwsem_up_read(int tid __maybe_unused)874aa3a5f31SWaiman Long static void torture_percpu_rwsem_up_read(int tid __maybe_unused)
875aa3a5f31SWaiman Long __releases(pcpu_rwsem)
876617783ddSPaul E. McKenney {
877617783ddSPaul E. McKenney 	percpu_up_read(&pcpu_rwsem);
878617783ddSPaul E. McKenney }
879617783ddSPaul E. McKenney 
880617783ddSPaul E. McKenney static struct lock_torture_ops percpu_rwsem_lock_ops = {
881617783ddSPaul E. McKenney 	.init		= torture_percpu_rwsem_init,
8820d720287SHou Tao 	.exit		= torture_percpu_rwsem_exit,
883617783ddSPaul E. McKenney 	.writelock	= torture_percpu_rwsem_down_write,
884617783ddSPaul E. McKenney 	.write_delay	= torture_rwsem_write_delay,
885e01f3a1aSJoel Fernandes (Google) 	.task_boost     = torture_rt_boost,
886617783ddSPaul E. McKenney 	.writeunlock	= torture_percpu_rwsem_up_write,
887617783ddSPaul E. McKenney 	.readlock       = torture_percpu_rwsem_down_read,
888617783ddSPaul E. McKenney 	.read_delay     = torture_rwsem_read_delay,
889617783ddSPaul E. McKenney 	.readunlock     = torture_percpu_rwsem_up_read,
890617783ddSPaul E. McKenney 	.name		= "percpu_rwsem_lock"
891617783ddSPaul E. McKenney };
892617783ddSPaul E. McKenney 
8930af3fe1eSPaul E. McKenney /*
8940af3fe1eSPaul E. McKenney  * Lock torture writer kthread.  Repeatedly acquires and releases
8950af3fe1eSPaul E. McKenney  * the lock, checking for duplicate acquisitions.
8960af3fe1eSPaul E. McKenney  */
lock_torture_writer(void * arg)8970af3fe1eSPaul E. McKenney static int lock_torture_writer(void *arg)
8980af3fe1eSPaul E. McKenney {
899e3bdaefbSPaul E. McKenney 	unsigned long j;
900e3bdaefbSPaul E. McKenney 	unsigned long j1;
901b6334320SJohn Stultz 	u32 lockset_mask;
902e3bdaefbSPaul E. McKenney 	struct lock_stress_stats *lwsp = arg;
903e3bdaefbSPaul E. McKenney 	DEFINE_TORTURE_RANDOM(rand);
90445bcf0bdSJohn Stultz 	bool skip_main_lock;
905e3bdaefbSPaul E. McKenney 	int tid = lwsp - cxt.lwsa;
9060af3fe1eSPaul E. McKenney 
9070af3fe1eSPaul E. McKenney 	VERBOSE_TOROUT_STRING("lock_torture_writer task started");
9085d248bb3SDietmar Eggemann 	if (!rt_task(current))
9098698a745SDongsheng Yang 		set_user_nice(current, MAX_NICE);
9100af3fe1eSPaul E. McKenney 
9110af3fe1eSPaul E. McKenney 	do {
912da601c63SPaul E. McKenney 		if ((torture_random(&rand) & 0xfffff) == 0)
9130af3fe1eSPaul E. McKenney 			schedule_timeout_uninterruptible(1);
914a1229491SDavidlohr Bueso 
915b6334320SJohn Stultz 		lockset_mask = torture_random(&rand);
91645bcf0bdSJohn Stultz 		/*
91745bcf0bdSJohn Stultz 		 * When using nested_locks, we want to occasionally
91845bcf0bdSJohn Stultz 		 * skip the main lock so we can avoid always serializing
91945bcf0bdSJohn Stultz 		 * the lock chains on that central lock. By skipping the
92045bcf0bdSJohn Stultz 		 * main lock occasionally, we can create different
92145bcf0bdSJohn Stultz 		 * contention patterns (allowing for multiple disjoint
92245bcf0bdSJohn Stultz 		 * blocked trees)
92345bcf0bdSJohn Stultz 		 */
92445bcf0bdSJohn Stultz 		skip_main_lock = (nested_locks &&
92545bcf0bdSJohn Stultz 				 !(torture_random(&rand) % 100));
92645bcf0bdSJohn Stultz 
927095777c4SDavidlohr Bueso 		cxt.cur_ops->task_boost(&rand);
928b6334320SJohn Stultz 		if (cxt.cur_ops->nested_lock)
929b6334320SJohn Stultz 			cxt.cur_ops->nested_lock(tid, lockset_mask);
93045bcf0bdSJohn Stultz 
93145bcf0bdSJohn Stultz 		if (!skip_main_lock) {
932e3bdaefbSPaul E. McKenney 			if (acq_writer_lim > 0)
933e3bdaefbSPaul E. McKenney 				j = jiffies;
934aa3a5f31SWaiman Long 			cxt.cur_ops->writelock(tid);
9350af3fe1eSPaul E. McKenney 			if (WARN_ON_ONCE(lock_is_write_held))
9361e6757a9SDavidlohr Bueso 				lwsp->n_lock_fail++;
937d02c6b52SZou Wei 			lock_is_write_held = true;
938af5f6e27SPaul E. McKenney 			if (WARN_ON_ONCE(atomic_read(&lock_is_read_held)))
939a1229491SDavidlohr Bueso 				lwsp->n_lock_fail++; /* rare, but... */
940e3bdaefbSPaul E. McKenney 			if (acq_writer_lim > 0) {
941e3bdaefbSPaul E. McKenney 				j1 = jiffies;
942e3bdaefbSPaul E. McKenney 				WARN_ONCE(time_after(j1, j + acq_writer_lim),
943e3bdaefbSPaul E. McKenney 					  "%s: Lock acquisition took %lu jiffies.\n",
944e3bdaefbSPaul E. McKenney 					  __func__, j1 - j);
945e3bdaefbSPaul E. McKenney 			}
9461e6757a9SDavidlohr Bueso 			lwsp->n_lock_acquired++;
94784cee9e7SPaul E. McKenney 
948f8619c30SPaul E. McKenney 			cxt.cur_ops->write_delay(&rand);
94984cee9e7SPaul E. McKenney 
950d02c6b52SZou Wei 			lock_is_write_held = false;
9513480d677SPaul E. McKenney 			WRITE_ONCE(last_lock_release, jiffies);
952aa3a5f31SWaiman Long 			cxt.cur_ops->writeunlock(tid);
95345bcf0bdSJohn Stultz 		}
954b6334320SJohn Stultz 		if (cxt.cur_ops->nested_unlock)
955b6334320SJohn Stultz 			cxt.cur_ops->nested_unlock(tid, lockset_mask);
956a1229491SDavidlohr Bueso 
9570af3fe1eSPaul E. McKenney 		stutter_wait("lock_torture_writer");
9580af3fe1eSPaul E. McKenney 	} while (!torture_must_stop());
959095777c4SDavidlohr Bueso 
960095777c4SDavidlohr Bueso 	cxt.cur_ops->task_boost(NULL); /* reset prio */
9610af3fe1eSPaul E. McKenney 	torture_kthread_stopping("lock_torture_writer");
9620af3fe1eSPaul E. McKenney 	return 0;
9630af3fe1eSPaul E. McKenney }
9640af3fe1eSPaul E. McKenney 
9650af3fe1eSPaul E. McKenney /*
9664f6332c1SDavidlohr Bueso  * Lock torture reader kthread.  Repeatedly acquires and releases
9674f6332c1SDavidlohr Bueso  * the reader lock.
9684f6332c1SDavidlohr Bueso  */
lock_torture_reader(void * arg)9694f6332c1SDavidlohr Bueso static int lock_torture_reader(void *arg)
9704f6332c1SDavidlohr Bueso {
9714f6332c1SDavidlohr Bueso 	struct lock_stress_stats *lrsp = arg;
972aa3a5f31SWaiman Long 	int tid = lrsp - cxt.lrsa;
973c0e1472dSPaul E. McKenney 	DEFINE_TORTURE_RANDOM(rand);
9744f6332c1SDavidlohr Bueso 
9754f6332c1SDavidlohr Bueso 	VERBOSE_TOROUT_STRING("lock_torture_reader task started");
9764f6332c1SDavidlohr Bueso 	set_user_nice(current, MAX_NICE);
9774f6332c1SDavidlohr Bueso 
9784f6332c1SDavidlohr Bueso 	do {
9794f6332c1SDavidlohr Bueso 		if ((torture_random(&rand) & 0xfffff) == 0)
9804f6332c1SDavidlohr Bueso 			schedule_timeout_uninterruptible(1);
981a1229491SDavidlohr Bueso 
982aa3a5f31SWaiman Long 		cxt.cur_ops->readlock(tid);
983af5f6e27SPaul E. McKenney 		atomic_inc(&lock_is_read_held);
984a1229491SDavidlohr Bueso 		if (WARN_ON_ONCE(lock_is_write_held))
985a1229491SDavidlohr Bueso 			lrsp->n_lock_fail++; /* rare, but... */
986a1229491SDavidlohr Bueso 
9874f6332c1SDavidlohr Bueso 		lrsp->n_lock_acquired++;
988630952c2SDavidlohr Bueso 		cxt.cur_ops->read_delay(&rand);
989af5f6e27SPaul E. McKenney 		atomic_dec(&lock_is_read_held);
990aa3a5f31SWaiman Long 		cxt.cur_ops->readunlock(tid);
991a1229491SDavidlohr Bueso 
9924f6332c1SDavidlohr Bueso 		stutter_wait("lock_torture_reader");
9934f6332c1SDavidlohr Bueso 	} while (!torture_must_stop());
9944f6332c1SDavidlohr Bueso 	torture_kthread_stopping("lock_torture_reader");
9954f6332c1SDavidlohr Bueso 	return 0;
9964f6332c1SDavidlohr Bueso }
9974f6332c1SDavidlohr Bueso 
9984f6332c1SDavidlohr Bueso /*
9990af3fe1eSPaul E. McKenney  * Create an lock-torture-statistics message in the specified buffer.
10000af3fe1eSPaul E. McKenney  */
__torture_print_stats(char * page,struct lock_stress_stats * statp,bool write)10014f6332c1SDavidlohr Bueso static void __torture_print_stats(char *page,
10024f6332c1SDavidlohr Bueso 				  struct lock_stress_stats *statp, bool write)
10030af3fe1eSPaul E. McKenney {
10045b237d65SPaul E. McKenney 	long cur;
1005d02c6b52SZou Wei 	bool fail = false;
10064f6332c1SDavidlohr Bueso 	int i, n_stress;
10075b237d65SPaul E. McKenney 	long max = 0, min = statp ? data_race(statp[0].n_lock_acquired) : 0;
10080af3fe1eSPaul E. McKenney 	long long sum = 0;
10090af3fe1eSPaul E. McKenney 
1010630952c2SDavidlohr Bueso 	n_stress = write ? cxt.nrealwriters_stress : cxt.nrealreaders_stress;
10114f6332c1SDavidlohr Bueso 	for (i = 0; i < n_stress; i++) {
10125b237d65SPaul E. McKenney 		if (data_race(statp[i].n_lock_fail))
10130af3fe1eSPaul E. McKenney 			fail = true;
10145b237d65SPaul E. McKenney 		cur = data_race(statp[i].n_lock_acquired);
10155b237d65SPaul E. McKenney 		sum += cur;
10165b237d65SPaul E. McKenney 		if (max < cur)
10175b237d65SPaul E. McKenney 			max = cur;
10185b237d65SPaul E. McKenney 		if (min > cur)
10195b237d65SPaul E. McKenney 			min = cur;
10200af3fe1eSPaul E. McKenney 	}
10210af3fe1eSPaul E. McKenney 	page += sprintf(page,
10224f6332c1SDavidlohr Bueso 			"%s:  Total: %lld  Max/Min: %ld/%ld %s  Fail: %d %s\n",
10234f6332c1SDavidlohr Bueso 			write ? "Writes" : "Reads ",
102428e09a2eSPaul E. McKenney 			sum, max, min,
102528e09a2eSPaul E. McKenney 			!onoff_interval && max / 2 > min ? "???" : "",
10260af3fe1eSPaul E. McKenney 			fail, fail ? "!!!" : "");
10270af3fe1eSPaul E. McKenney 	if (fail)
1028630952c2SDavidlohr Bueso 		atomic_inc(&cxt.n_lock_torture_errors);
10290af3fe1eSPaul E. McKenney }
10300af3fe1eSPaul E. McKenney 
10310af3fe1eSPaul E. McKenney /*
10320af3fe1eSPaul E. McKenney  * Print torture statistics.  Caller must ensure that there is only one
10330af3fe1eSPaul E. McKenney  * call to this function at a given time!!!  This is normally accomplished
10340af3fe1eSPaul E. McKenney  * by relying on the module system to only have one copy of the module
10350af3fe1eSPaul E. McKenney  * loaded, and then by giving the lock_torture_stats kthread full control
10360af3fe1eSPaul E. McKenney  * (or the init/cleanup functions when lock_torture_stats thread is not
10370af3fe1eSPaul E. McKenney  * running).
10380af3fe1eSPaul E. McKenney  */
lock_torture_stats_print(void)10390af3fe1eSPaul E. McKenney static void lock_torture_stats_print(void)
10400af3fe1eSPaul E. McKenney {
1041630952c2SDavidlohr Bueso 	int size = cxt.nrealwriters_stress * 200 + 8192;
10420af3fe1eSPaul E. McKenney 	char *buf;
10430af3fe1eSPaul E. McKenney 
1044630952c2SDavidlohr Bueso 	if (cxt.cur_ops->readlock)
1045630952c2SDavidlohr Bueso 		size += cxt.nrealreaders_stress * 200 + 8192;
10464f6332c1SDavidlohr Bueso 
10470af3fe1eSPaul E. McKenney 	buf = kmalloc(size, GFP_KERNEL);
10480af3fe1eSPaul E. McKenney 	if (!buf) {
10490af3fe1eSPaul E. McKenney 		pr_err("lock_torture_stats_print: Out of memory, need: %d",
10500af3fe1eSPaul E. McKenney 		       size);
10510af3fe1eSPaul E. McKenney 		return;
10520af3fe1eSPaul E. McKenney 	}
10534f6332c1SDavidlohr Bueso 
1054630952c2SDavidlohr Bueso 	__torture_print_stats(buf, cxt.lwsa, true);
10550af3fe1eSPaul E. McKenney 	pr_alert("%s", buf);
10560af3fe1eSPaul E. McKenney 	kfree(buf);
10574f6332c1SDavidlohr Bueso 
1058630952c2SDavidlohr Bueso 	if (cxt.cur_ops->readlock) {
10594f6332c1SDavidlohr Bueso 		buf = kmalloc(size, GFP_KERNEL);
10604f6332c1SDavidlohr Bueso 		if (!buf) {
10614f6332c1SDavidlohr Bueso 			pr_err("lock_torture_stats_print: Out of memory, need: %d",
10624f6332c1SDavidlohr Bueso 			       size);
10634f6332c1SDavidlohr Bueso 			return;
10644f6332c1SDavidlohr Bueso 		}
10654f6332c1SDavidlohr Bueso 
1066630952c2SDavidlohr Bueso 		__torture_print_stats(buf, cxt.lrsa, false);
10674f6332c1SDavidlohr Bueso 		pr_alert("%s", buf);
10684f6332c1SDavidlohr Bueso 		kfree(buf);
10694f6332c1SDavidlohr Bueso 	}
10700af3fe1eSPaul E. McKenney }
10710af3fe1eSPaul E. McKenney 
10720af3fe1eSPaul E. McKenney /*
10730af3fe1eSPaul E. McKenney  * Periodically prints torture statistics, if periodic statistics printing
10740af3fe1eSPaul E. McKenney  * was specified via the stat_interval module parameter.
10750af3fe1eSPaul E. McKenney  *
10760af3fe1eSPaul E. McKenney  * No need to worry about fullstop here, since this one doesn't reference
10770af3fe1eSPaul E. McKenney  * volatile state or register callbacks.
10780af3fe1eSPaul E. McKenney  */
lock_torture_stats(void * arg)10790af3fe1eSPaul E. McKenney static int lock_torture_stats(void *arg)
10800af3fe1eSPaul E. McKenney {
10810af3fe1eSPaul E. McKenney 	VERBOSE_TOROUT_STRING("lock_torture_stats task started");
10820af3fe1eSPaul E. McKenney 	do {
10830af3fe1eSPaul E. McKenney 		schedule_timeout_interruptible(stat_interval * HZ);
10840af3fe1eSPaul E. McKenney 		lock_torture_stats_print();
10850af3fe1eSPaul E. McKenney 		torture_shutdown_absorb("lock_torture_stats");
10860af3fe1eSPaul E. McKenney 	} while (!torture_must_stop());
10870af3fe1eSPaul E. McKenney 	torture_kthread_stopping("lock_torture_stats");
10880af3fe1eSPaul E. McKenney 	return 0;
10890af3fe1eSPaul E. McKenney }
10900af3fe1eSPaul E. McKenney 
109173e34124SPaul E. McKenney 
10920af3fe1eSPaul E. McKenney static inline void
lock_torture_print_module_parms(struct lock_torture_ops * cur_ops,const char * tag)10930af3fe1eSPaul E. McKenney lock_torture_print_module_parms(struct lock_torture_ops *cur_ops,
10940af3fe1eSPaul E. McKenney 				const char *tag)
10950af3fe1eSPaul E. McKenney {
109673e34124SPaul E. McKenney 	static cpumask_t cpumask_all;
10972273799cSPaul E. McKenney 	cpumask_t *rcmp = cpumask_nonempty(bind_readers) ? bind_readers : &cpumask_all;
10982273799cSPaul E. McKenney 	cpumask_t *wcmp = cpumask_nonempty(bind_writers) ? bind_writers : &cpumask_all;
109973e34124SPaul E. McKenney 
110073e34124SPaul E. McKenney 	cpumask_setall(&cpumask_all);
11010af3fe1eSPaul E. McKenney 	pr_alert("%s" TORTURE_FLAG
11022273799cSPaul E. McKenney 		 "--- %s%s: acq_writer_lim=%d bind_readers=%*pbl bind_writers=%*pbl call_rcu_chains=%d long_hold=%d nested_locks=%d nreaders_stress=%d nwriters_stress=%d onoff_holdoff=%d onoff_interval=%d rt_boost=%d rt_boost_factor=%d shuffle_interval=%d shutdown_secs=%d stat_interval=%d stutter=%d verbose=%d writer_fifo=%d\n",
1103630952c2SDavidlohr Bueso 		 torture_type, tag, cxt.debug_lock ? " [debug]": "",
11042273799cSPaul E. McKenney 		 acq_writer_lim, cpumask_pr_args(rcmp), cpumask_pr_args(wcmp),
11052273799cSPaul E. McKenney 		 call_rcu_chains, long_hold, nested_locks, cxt.nrealreaders_stress,
110600c24c9cSPaul E. McKenney 		 cxt.nrealwriters_stress, onoff_holdoff, onoff_interval, rt_boost,
110700c24c9cSPaul E. McKenney 		 rt_boost_factor, shuffle_interval, shutdown_secs, stat_interval, stutter,
11082273799cSPaul E. McKenney 		 verbose, writer_fifo);
11090af3fe1eSPaul E. McKenney }
11100af3fe1eSPaul E. McKenney 
11117f993623SPaul E. McKenney // If requested, maintain call_rcu() chains to keep a grace period always
11127f993623SPaul E. McKenney // in flight.  These increase the probability of getting an RCU CPU stall
11137f993623SPaul E. McKenney // warning and associated diagnostics when a locking primitive stalls.
11147f993623SPaul E. McKenney 
call_rcu_chain_cb(struct rcu_head * rhp)11157f993623SPaul E. McKenney static void call_rcu_chain_cb(struct rcu_head *rhp)
11167f993623SPaul E. McKenney {
11177f993623SPaul E. McKenney 	struct call_rcu_chain *crcp = container_of(rhp, struct call_rcu_chain, crc_rh);
11187f993623SPaul E. McKenney 
11197f993623SPaul E. McKenney 	if (!smp_load_acquire(&crcp->crc_stop)) {
11207f993623SPaul E. McKenney 		(void)start_poll_synchronize_rcu(); // Start one grace period...
11217f993623SPaul E. McKenney 		call_rcu(&crcp->crc_rh, call_rcu_chain_cb); // ... and later start another.
11227f993623SPaul E. McKenney 	}
11237f993623SPaul E. McKenney }
11247f993623SPaul E. McKenney 
11257f993623SPaul E. McKenney // Start the requested number of call_rcu() chains.
call_rcu_chain_init(void)11267f993623SPaul E. McKenney static int call_rcu_chain_init(void)
11277f993623SPaul E. McKenney {
11287f993623SPaul E. McKenney 	int i;
11297f993623SPaul E. McKenney 
11307f993623SPaul E. McKenney 	if (call_rcu_chains <= 0)
11317f993623SPaul E. McKenney 		return 0;
113269dcbbd8SPaul E. McKenney 	call_rcu_chain_list = kcalloc(call_rcu_chains, sizeof(*call_rcu_chain_list), GFP_KERNEL);
113369dcbbd8SPaul E. McKenney 	if (!call_rcu_chain_list)
11347f993623SPaul E. McKenney 		return -ENOMEM;
11357f993623SPaul E. McKenney 	for (i = 0; i < call_rcu_chains; i++) {
113669dcbbd8SPaul E. McKenney 		call_rcu_chain_list[i].crc_stop = false;
113769dcbbd8SPaul E. McKenney 		call_rcu(&call_rcu_chain_list[i].crc_rh, call_rcu_chain_cb);
11387f993623SPaul E. McKenney 	}
11397f993623SPaul E. McKenney 	return 0;
11407f993623SPaul E. McKenney }
11417f993623SPaul E. McKenney 
11427f993623SPaul E. McKenney // Stop all of the call_rcu() chains.
call_rcu_chain_cleanup(void)11437f993623SPaul E. McKenney static void call_rcu_chain_cleanup(void)
11447f993623SPaul E. McKenney {
11457f993623SPaul E. McKenney 	int i;
11467f993623SPaul E. McKenney 
114769dcbbd8SPaul E. McKenney 	if (!call_rcu_chain_list)
11487f993623SPaul E. McKenney 		return;
11497f993623SPaul E. McKenney 	for (i = 0; i < call_rcu_chains; i++)
115069dcbbd8SPaul E. McKenney 		smp_store_release(&call_rcu_chain_list[i].crc_stop, true);
11517f993623SPaul E. McKenney 	rcu_barrier();
115269dcbbd8SPaul E. McKenney 	kfree(call_rcu_chain_list);
115369dcbbd8SPaul E. McKenney 	call_rcu_chain_list = NULL;
11547f993623SPaul E. McKenney }
11557f993623SPaul E. McKenney 
lock_torture_cleanup(void)11560af3fe1eSPaul E. McKenney static void lock_torture_cleanup(void)
11570af3fe1eSPaul E. McKenney {
11580af3fe1eSPaul E. McKenney 	int i;
11590af3fe1eSPaul E. McKenney 
1160d36a7a0dSDavidlohr Bueso 	if (torture_cleanup_begin())
11610af3fe1eSPaul E. McKenney 		return;
11620af3fe1eSPaul E. McKenney 
1163c1c33b92SDavidlohr Bueso 	/*
1164c1c33b92SDavidlohr Bueso 	 * Indicates early cleanup, meaning that the test has not run,
11650d720287SHou Tao 	 * such as when passing bogus args when loading the module.
11660d720287SHou Tao 	 * However cxt->cur_ops.init() may have been invoked, so beside
11670d720287SHou Tao 	 * perform the underlying torture-specific cleanups, cur_ops.exit()
11680d720287SHou Tao 	 * will be invoked if needed.
1169c1c33b92SDavidlohr Bueso 	 */
11702ce77d16SDavidlohr Bueso 	if (!cxt.lwsa && !cxt.lrsa)
1171c1c33b92SDavidlohr Bueso 		goto end;
1172c1c33b92SDavidlohr Bueso 
11730af3fe1eSPaul E. McKenney 	if (writer_tasks) {
1174630952c2SDavidlohr Bueso 		for (i = 0; i < cxt.nrealwriters_stress; i++)
11755d248bb3SDietmar Eggemann 			torture_stop_kthread(lock_torture_writer, writer_tasks[i]);
11760af3fe1eSPaul E. McKenney 		kfree(writer_tasks);
11770af3fe1eSPaul E. McKenney 		writer_tasks = NULL;
11780af3fe1eSPaul E. McKenney 	}
11790af3fe1eSPaul E. McKenney 
11804f6332c1SDavidlohr Bueso 	if (reader_tasks) {
1181630952c2SDavidlohr Bueso 		for (i = 0; i < cxt.nrealreaders_stress; i++)
11824f6332c1SDavidlohr Bueso 			torture_stop_kthread(lock_torture_reader,
11834f6332c1SDavidlohr Bueso 					     reader_tasks[i]);
11844f6332c1SDavidlohr Bueso 		kfree(reader_tasks);
11854f6332c1SDavidlohr Bueso 		reader_tasks = NULL;
11864f6332c1SDavidlohr Bueso 	}
11874f6332c1SDavidlohr Bueso 
11880af3fe1eSPaul E. McKenney 	torture_stop_kthread(lock_torture_stats, stats_task);
11890af3fe1eSPaul E. McKenney 	lock_torture_stats_print();  /* -After- the stats thread is stopped! */
11900af3fe1eSPaul E. McKenney 
1191630952c2SDavidlohr Bueso 	if (atomic_read(&cxt.n_lock_torture_errors))
1192630952c2SDavidlohr Bueso 		lock_torture_print_module_parms(cxt.cur_ops,
11930af3fe1eSPaul E. McKenney 						"End of test: FAILURE");
11940af3fe1eSPaul E. McKenney 	else if (torture_onoff_failures())
1195630952c2SDavidlohr Bueso 		lock_torture_print_module_parms(cxt.cur_ops,
11960af3fe1eSPaul E. McKenney 						"End of test: LOCK_HOTPLUG");
11970af3fe1eSPaul E. McKenney 	else
1198630952c2SDavidlohr Bueso 		lock_torture_print_module_parms(cxt.cur_ops,
11990af3fe1eSPaul E. McKenney 						"End of test: SUCCESS");
1200f4dbba59SYang Shi 
1201f4dbba59SYang Shi 	kfree(cxt.lwsa);
1202a9d6938dSPaul E. McKenney 	cxt.lwsa = NULL;
1203f4dbba59SYang Shi 	kfree(cxt.lrsa);
1204a9d6938dSPaul E. McKenney 	cxt.lrsa = NULL;
1205f4dbba59SYang Shi 
12067f993623SPaul E. McKenney 	call_rcu_chain_cleanup();
12077f993623SPaul E. McKenney 
1208c1c33b92SDavidlohr Bueso end:
12090d720287SHou Tao 	if (cxt.init_called) {
12100d720287SHou Tao 		if (cxt.cur_ops->exit)
12110d720287SHou Tao 			cxt.cur_ops->exit();
12120d720287SHou Tao 		cxt.init_called = false;
12130d720287SHou Tao 	}
1214d36a7a0dSDavidlohr Bueso 	torture_cleanup_end();
12150af3fe1eSPaul E. McKenney }
12160af3fe1eSPaul E. McKenney 
lock_torture_init(void)12170af3fe1eSPaul E. McKenney static int __init lock_torture_init(void)
12180af3fe1eSPaul E. McKenney {
12194f6332c1SDavidlohr Bueso 	int i, j;
12200af3fe1eSPaul E. McKenney 	int firsterr = 0;
12210af3fe1eSPaul E. McKenney 	static struct lock_torture_ops *torture_ops[] = {
1222e34191faSDavidlohr Bueso 		&lock_busted_ops,
1223e34191faSDavidlohr Bueso 		&spin_lock_ops, &spin_lock_irq_ops,
12245d65cf6aSZqiang 		&raw_spin_lock_ops, &raw_spin_lock_irq_ops,
1225*a6884f6fSKumar Kartikeya Dwivedi #ifdef CONFIG_BPF_SYSCALL
1226*a6884f6fSKumar Kartikeya Dwivedi 		&raw_res_spin_lock_ops, &raw_res_spin_lock_irq_ops,
1227*a6884f6fSKumar Kartikeya Dwivedi #endif
1228e34191faSDavidlohr Bueso 		&rw_lock_ops, &rw_lock_irq_ops,
1229e34191faSDavidlohr Bueso 		&mutex_lock_ops,
12300186a6cbSChris Wilson 		&ww_mutex_lock_ops,
1231095777c4SDavidlohr Bueso #ifdef CONFIG_RT_MUTEXES
1232095777c4SDavidlohr Bueso 		&rtmutex_lock_ops,
1233095777c4SDavidlohr Bueso #endif
1234e34191faSDavidlohr Bueso 		&rwsem_lock_ops,
1235617783ddSPaul E. McKenney 		&percpu_rwsem_lock_ops,
12360af3fe1eSPaul E. McKenney 	};
12370af3fe1eSPaul E. McKenney 
1238a2f2577dSPaul E. McKenney 	if (!torture_init_begin(torture_type, verbose))
12395228084eSPaul E. McKenney 		return -EBUSY;
12400af3fe1eSPaul E. McKenney 
12410af3fe1eSPaul E. McKenney 	/* Process args and tell the world that the torturer is on the job. */
12420af3fe1eSPaul E. McKenney 	for (i = 0; i < ARRAY_SIZE(torture_ops); i++) {
1243630952c2SDavidlohr Bueso 		cxt.cur_ops = torture_ops[i];
1244630952c2SDavidlohr Bueso 		if (strcmp(torture_type, cxt.cur_ops->name) == 0)
12450af3fe1eSPaul E. McKenney 			break;
12460af3fe1eSPaul E. McKenney 	}
12470af3fe1eSPaul E. McKenney 	if (i == ARRAY_SIZE(torture_ops)) {
12480af3fe1eSPaul E. McKenney 		pr_alert("lock-torture: invalid torture type: \"%s\"\n",
12490af3fe1eSPaul E. McKenney 			 torture_type);
12500af3fe1eSPaul E. McKenney 		pr_alert("lock-torture types:");
12510af3fe1eSPaul E. McKenney 		for (i = 0; i < ARRAY_SIZE(torture_ops); i++)
12520af3fe1eSPaul E. McKenney 			pr_alert(" %s", torture_ops[i]->name);
12530af3fe1eSPaul E. McKenney 		pr_alert("\n");
1254a36a9961SPaul E. McKenney 		firsterr = -EINVAL;
1255a36a9961SPaul E. McKenney 		goto unwind;
12560af3fe1eSPaul E. McKenney 	}
12572ce77d16SDavidlohr Bueso 
1258e5ace37dSHou Tao 	if (nwriters_stress == 0 &&
1259e5ace37dSHou Tao 	    (!cxt.cur_ops->readlock || nreaders_stress == 0)) {
12602ce77d16SDavidlohr Bueso 		pr_alert("lock-torture: must run at least one locking thread\n");
12612ce77d16SDavidlohr Bueso 		firsterr = -EINVAL;
12622ce77d16SDavidlohr Bueso 		goto unwind;
12632ce77d16SDavidlohr Bueso 	}
12642ce77d16SDavidlohr Bueso 
12650af3fe1eSPaul E. McKenney 	if (nwriters_stress >= 0)
1266630952c2SDavidlohr Bueso 		cxt.nrealwriters_stress = nwriters_stress;
12670af3fe1eSPaul E. McKenney 	else
1268630952c2SDavidlohr Bueso 		cxt.nrealwriters_stress = 2 * num_online_cpus();
1269f095bfc0SDavidlohr Bueso 
12708c52cca0SWaiman Long 	if (cxt.cur_ops->init) {
12718c52cca0SWaiman Long 		cxt.cur_ops->init();
12728c52cca0SWaiman Long 		cxt.init_called = true;
12738c52cca0SWaiman Long 	}
12748c52cca0SWaiman Long 
1275f095bfc0SDavidlohr Bueso #ifdef CONFIG_DEBUG_MUTEXES
1276c5d3c8caSChuhong Yuan 	if (str_has_prefix(torture_type, "mutex"))
1277630952c2SDavidlohr Bueso 		cxt.debug_lock = true;
1278f095bfc0SDavidlohr Bueso #endif
1279095777c4SDavidlohr Bueso #ifdef CONFIG_DEBUG_RT_MUTEXES
1280c5d3c8caSChuhong Yuan 	if (str_has_prefix(torture_type, "rtmutex"))
1281095777c4SDavidlohr Bueso 		cxt.debug_lock = true;
1282095777c4SDavidlohr Bueso #endif
1283f095bfc0SDavidlohr Bueso #ifdef CONFIG_DEBUG_SPINLOCK
1284c5d3c8caSChuhong Yuan 	if ((str_has_prefix(torture_type, "spin")) ||
1285c5d3c8caSChuhong Yuan 	    (str_has_prefix(torture_type, "rw_lock")))
1286630952c2SDavidlohr Bueso 		cxt.debug_lock = true;
1287f095bfc0SDavidlohr Bueso #endif
12880af3fe1eSPaul E. McKenney 
12890af3fe1eSPaul E. McKenney 	/* Initialize the statistics so that each run gets its own numbers. */
12902ce77d16SDavidlohr Bueso 	if (nwriters_stress) {
1291d02c6b52SZou Wei 		lock_is_write_held = false;
12926da2ec56SKees Cook 		cxt.lwsa = kmalloc_array(cxt.nrealwriters_stress,
12936da2ec56SKees Cook 					 sizeof(*cxt.lwsa),
12946da2ec56SKees Cook 					 GFP_KERNEL);
1295630952c2SDavidlohr Bueso 		if (cxt.lwsa == NULL) {
1296630952c2SDavidlohr Bueso 			VERBOSE_TOROUT_STRING("cxt.lwsa: Out of memory");
12970af3fe1eSPaul E. McKenney 			firsterr = -ENOMEM;
12980af3fe1eSPaul E. McKenney 			goto unwind;
12990af3fe1eSPaul E. McKenney 		}
13002ce77d16SDavidlohr Bueso 
1301630952c2SDavidlohr Bueso 		for (i = 0; i < cxt.nrealwriters_stress; i++) {
1302630952c2SDavidlohr Bueso 			cxt.lwsa[i].n_lock_fail = 0;
1303630952c2SDavidlohr Bueso 			cxt.lwsa[i].n_lock_acquired = 0;
13040af3fe1eSPaul E. McKenney 		}
13052ce77d16SDavidlohr Bueso 	}
13060af3fe1eSPaul E. McKenney 
1307630952c2SDavidlohr Bueso 	if (cxt.cur_ops->readlock) {
13084f6332c1SDavidlohr Bueso 		if (nreaders_stress >= 0)
1309630952c2SDavidlohr Bueso 			cxt.nrealreaders_stress = nreaders_stress;
13104f6332c1SDavidlohr Bueso 		else {
13114f6332c1SDavidlohr Bueso 			/*
13124f6332c1SDavidlohr Bueso 			 * By default distribute evenly the number of
13134f6332c1SDavidlohr Bueso 			 * readers and writers. We still run the same number
13144f6332c1SDavidlohr Bueso 			 * of threads as the writer-only locks default.
13154f6332c1SDavidlohr Bueso 			 */
13164f6332c1SDavidlohr Bueso 			if (nwriters_stress < 0) /* user doesn't care */
1317630952c2SDavidlohr Bueso 				cxt.nrealwriters_stress = num_online_cpus();
1318630952c2SDavidlohr Bueso 			cxt.nrealreaders_stress = cxt.nrealwriters_stress;
13194f6332c1SDavidlohr Bueso 		}
13200af3fe1eSPaul E. McKenney 
13212ce77d16SDavidlohr Bueso 		if (nreaders_stress) {
13226da2ec56SKees Cook 			cxt.lrsa = kmalloc_array(cxt.nrealreaders_stress,
13236da2ec56SKees Cook 						 sizeof(*cxt.lrsa),
13246da2ec56SKees Cook 						 GFP_KERNEL);
1325630952c2SDavidlohr Bueso 			if (cxt.lrsa == NULL) {
1326630952c2SDavidlohr Bueso 				VERBOSE_TOROUT_STRING("cxt.lrsa: Out of memory");
13274f6332c1SDavidlohr Bueso 				firsterr = -ENOMEM;
1328630952c2SDavidlohr Bueso 				kfree(cxt.lwsa);
1329c1c33b92SDavidlohr Bueso 				cxt.lwsa = NULL;
13304f6332c1SDavidlohr Bueso 				goto unwind;
13314f6332c1SDavidlohr Bueso 			}
13324f6332c1SDavidlohr Bueso 
1333630952c2SDavidlohr Bueso 			for (i = 0; i < cxt.nrealreaders_stress; i++) {
1334630952c2SDavidlohr Bueso 				cxt.lrsa[i].n_lock_fail = 0;
1335630952c2SDavidlohr Bueso 				cxt.lrsa[i].n_lock_acquired = 0;
13364f6332c1SDavidlohr Bueso 			}
13374f6332c1SDavidlohr Bueso 		}
13382ce77d16SDavidlohr Bueso 	}
1339c1c33b92SDavidlohr Bueso 
13407f993623SPaul E. McKenney 	firsterr = call_rcu_chain_init();
13417f993623SPaul E. McKenney 	if (torture_init_error(firsterr))
13427f993623SPaul E. McKenney 		goto unwind;
13437f993623SPaul E. McKenney 
1344630952c2SDavidlohr Bueso 	lock_torture_print_module_parms(cxt.cur_ops, "Start of test");
13454f6332c1SDavidlohr Bueso 
13464f6332c1SDavidlohr Bueso 	/* Prepare torture context. */
13470af3fe1eSPaul E. McKenney 	if (onoff_interval > 0) {
13480af3fe1eSPaul E. McKenney 		firsterr = torture_onoff_init(onoff_holdoff * HZ,
13493a6cb58fSPaul E. McKenney 					      onoff_interval * HZ, NULL);
1350b3b3cc61SPaul E. McKenney 		if (torture_init_error(firsterr))
13510af3fe1eSPaul E. McKenney 			goto unwind;
13520af3fe1eSPaul E. McKenney 	}
13530af3fe1eSPaul E. McKenney 	if (shuffle_interval > 0) {
13540af3fe1eSPaul E. McKenney 		firsterr = torture_shuffle_init(shuffle_interval);
1355b3b3cc61SPaul E. McKenney 		if (torture_init_error(firsterr))
13560af3fe1eSPaul E. McKenney 			goto unwind;
13570af3fe1eSPaul E. McKenney 	}
13580af3fe1eSPaul E. McKenney 	if (shutdown_secs > 0) {
13590af3fe1eSPaul E. McKenney 		firsterr = torture_shutdown_init(shutdown_secs,
13600af3fe1eSPaul E. McKenney 						 lock_torture_cleanup);
1361b3b3cc61SPaul E. McKenney 		if (torture_init_error(firsterr))
13620af3fe1eSPaul E. McKenney 			goto unwind;
13630af3fe1eSPaul E. McKenney 	}
13640af3fe1eSPaul E. McKenney 	if (stutter > 0) {
1365ff3bf92dSPaul E. McKenney 		firsterr = torture_stutter_init(stutter, stutter);
1366b3b3cc61SPaul E. McKenney 		if (torture_init_error(firsterr))
13670af3fe1eSPaul E. McKenney 			goto unwind;
13680af3fe1eSPaul E. McKenney 	}
13690af3fe1eSPaul E. McKenney 
13702ce77d16SDavidlohr Bueso 	if (nwriters_stress) {
13716396bb22SKees Cook 		writer_tasks = kcalloc(cxt.nrealwriters_stress,
13726396bb22SKees Cook 				       sizeof(writer_tasks[0]),
13730af3fe1eSPaul E. McKenney 				       GFP_KERNEL);
13740af3fe1eSPaul E. McKenney 		if (writer_tasks == NULL) {
137581faa4f6SLi Zhijian 			TOROUT_ERRSTRING("writer_tasks: Out of memory");
13760af3fe1eSPaul E. McKenney 			firsterr = -ENOMEM;
13770af3fe1eSPaul E. McKenney 			goto unwind;
13780af3fe1eSPaul E. McKenney 		}
13792ce77d16SDavidlohr Bueso 	}
13804f6332c1SDavidlohr Bueso 
1381b6334320SJohn Stultz 	/* cap nested_locks to MAX_NESTED_LOCKS */
1382b6334320SJohn Stultz 	if (nested_locks > MAX_NESTED_LOCKS)
1383b6334320SJohn Stultz 		nested_locks = MAX_NESTED_LOCKS;
1384b6334320SJohn Stultz 
1385630952c2SDavidlohr Bueso 	if (cxt.cur_ops->readlock) {
13866396bb22SKees Cook 		reader_tasks = kcalloc(cxt.nrealreaders_stress,
13876396bb22SKees Cook 				       sizeof(reader_tasks[0]),
13884f6332c1SDavidlohr Bueso 				       GFP_KERNEL);
13894f6332c1SDavidlohr Bueso 		if (reader_tasks == NULL) {
139081faa4f6SLi Zhijian 			TOROUT_ERRSTRING("reader_tasks: Out of memory");
1391f4dbba59SYang Shi 			kfree(writer_tasks);
1392f4dbba59SYang Shi 			writer_tasks = NULL;
13934f6332c1SDavidlohr Bueso 			firsterr = -ENOMEM;
13944f6332c1SDavidlohr Bueso 			goto unwind;
13954f6332c1SDavidlohr Bueso 		}
13964f6332c1SDavidlohr Bueso 	}
13974f6332c1SDavidlohr Bueso 
13984f6332c1SDavidlohr Bueso 	/*
13994f6332c1SDavidlohr Bueso 	 * Create the kthreads and start torturing (oh, those poor little locks).
14004f6332c1SDavidlohr Bueso 	 *
14014f6332c1SDavidlohr Bueso 	 * TODO: Note that we interleave writers with readers, giving writers a
14024f6332c1SDavidlohr Bueso 	 * slight advantage, by creating its kthread first. This can be modified
14034f6332c1SDavidlohr Bueso 	 * for very specific needs, or even let the user choose the policy, if
14044f6332c1SDavidlohr Bueso 	 * ever wanted.
14054f6332c1SDavidlohr Bueso 	 */
1406630952c2SDavidlohr Bueso 	for (i = 0, j = 0; i < cxt.nrealwriters_stress ||
1407630952c2SDavidlohr Bueso 		    j < cxt.nrealreaders_stress; i++, j++) {
1408630952c2SDavidlohr Bueso 		if (i >= cxt.nrealwriters_stress)
14094f6332c1SDavidlohr Bueso 			goto create_reader;
14104f6332c1SDavidlohr Bueso 
14114f6332c1SDavidlohr Bueso 		/* Create writer. */
14125d248bb3SDietmar Eggemann 		firsterr = torture_create_kthread_cb(lock_torture_writer, &cxt.lwsa[i],
14135d248bb3SDietmar Eggemann 						     writer_tasks[i],
14145d248bb3SDietmar Eggemann 						     writer_fifo ? sched_set_fifo : NULL);
1415b3b3cc61SPaul E. McKenney 		if (torture_init_error(firsterr))
14160af3fe1eSPaul E. McKenney 			goto unwind;
14172273799cSPaul E. McKenney 		if (cpumask_nonempty(bind_writers))
14180203b485SPaul E. McKenney 			torture_sched_setaffinity(writer_tasks[i]->pid, bind_writers, true);
14194f6332c1SDavidlohr Bueso 
14204f6332c1SDavidlohr Bueso 	create_reader:
1421630952c2SDavidlohr Bueso 		if (cxt.cur_ops->readlock == NULL || (j >= cxt.nrealreaders_stress))
14224f6332c1SDavidlohr Bueso 			continue;
14234f6332c1SDavidlohr Bueso 		/* Create reader. */
1424630952c2SDavidlohr Bueso 		firsterr = torture_create_kthread(lock_torture_reader, &cxt.lrsa[j],
14254f6332c1SDavidlohr Bueso 						  reader_tasks[j]);
1426b3b3cc61SPaul E. McKenney 		if (torture_init_error(firsterr))
14274f6332c1SDavidlohr Bueso 			goto unwind;
14282273799cSPaul E. McKenney 		if (cpumask_nonempty(bind_readers))
14290203b485SPaul E. McKenney 			torture_sched_setaffinity(reader_tasks[j]->pid, bind_readers, true);
14300af3fe1eSPaul E. McKenney 	}
14310af3fe1eSPaul E. McKenney 	if (stat_interval > 0) {
14320af3fe1eSPaul E. McKenney 		firsterr = torture_create_kthread(lock_torture_stats, NULL,
14330af3fe1eSPaul E. McKenney 						  stats_task);
1434b3b3cc61SPaul E. McKenney 		if (torture_init_error(firsterr))
14350af3fe1eSPaul E. McKenney 			goto unwind;
14360af3fe1eSPaul E. McKenney 	}
14370af3fe1eSPaul E. McKenney 	torture_init_end();
14380af3fe1eSPaul E. McKenney 	return 0;
14390af3fe1eSPaul E. McKenney 
14400af3fe1eSPaul E. McKenney unwind:
14410af3fe1eSPaul E. McKenney 	torture_init_end();
14420af3fe1eSPaul E. McKenney 	lock_torture_cleanup();
14436b74fa0aSPaul E. McKenney 	if (shutdown_secs) {
14446b74fa0aSPaul E. McKenney 		WARN_ON(!IS_MODULE(CONFIG_LOCK_TORTURE_TEST));
14456b74fa0aSPaul E. McKenney 		kernel_power_off();
14466b74fa0aSPaul E. McKenney 	}
14470af3fe1eSPaul E. McKenney 	return firsterr;
14480af3fe1eSPaul E. McKenney }
14490af3fe1eSPaul E. McKenney 
14500af3fe1eSPaul E. McKenney module_init(lock_torture_init);
14510af3fe1eSPaul E. McKenney module_exit(lock_torture_cleanup);
1452