1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0+
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun * Module-based torture test facility for locking
4*4882a593Smuzhiyun *
5*4882a593Smuzhiyun * Copyright (C) IBM Corporation, 2014
6*4882a593Smuzhiyun *
7*4882a593Smuzhiyun * Authors: Paul E. McKenney <paulmck@linux.ibm.com>
8*4882a593Smuzhiyun * Davidlohr Bueso <dave@stgolabs.net>
9*4882a593Smuzhiyun * Based on kernel/rcu/torture.c.
10*4882a593Smuzhiyun */
11*4882a593Smuzhiyun
12*4882a593Smuzhiyun #define pr_fmt(fmt) fmt
13*4882a593Smuzhiyun
14*4882a593Smuzhiyun #include <linux/kernel.h>
15*4882a593Smuzhiyun #include <linux/module.h>
16*4882a593Smuzhiyun #include <linux/kthread.h>
17*4882a593Smuzhiyun #include <linux/sched/rt.h>
18*4882a593Smuzhiyun #include <linux/spinlock.h>
19*4882a593Smuzhiyun #include <linux/mutex.h>
20*4882a593Smuzhiyun #include <linux/rwsem.h>
21*4882a593Smuzhiyun #include <linux/smp.h>
22*4882a593Smuzhiyun #include <linux/interrupt.h>
23*4882a593Smuzhiyun #include <linux/sched.h>
24*4882a593Smuzhiyun #include <uapi/linux/sched/types.h>
25*4882a593Smuzhiyun #include <linux/rtmutex.h>
26*4882a593Smuzhiyun #include <linux/atomic.h>
27*4882a593Smuzhiyun #include <linux/moduleparam.h>
28*4882a593Smuzhiyun #include <linux/delay.h>
29*4882a593Smuzhiyun #include <linux/slab.h>
30*4882a593Smuzhiyun #include <linux/percpu-rwsem.h>
31*4882a593Smuzhiyun #include <linux/torture.h>
32*4882a593Smuzhiyun
33*4882a593Smuzhiyun MODULE_LICENSE("GPL");
34*4882a593Smuzhiyun MODULE_AUTHOR("Paul E. McKenney <paulmck@linux.ibm.com>");
35*4882a593Smuzhiyun
36*4882a593Smuzhiyun torture_param(int, nwriters_stress, -1,
37*4882a593Smuzhiyun "Number of write-locking stress-test threads");
38*4882a593Smuzhiyun torture_param(int, nreaders_stress, -1,
39*4882a593Smuzhiyun "Number of read-locking stress-test threads");
40*4882a593Smuzhiyun torture_param(int, onoff_holdoff, 0, "Time after boot before CPU hotplugs (s)");
41*4882a593Smuzhiyun torture_param(int, onoff_interval, 0,
42*4882a593Smuzhiyun "Time between CPU hotplugs (s), 0=disable");
43*4882a593Smuzhiyun torture_param(int, shuffle_interval, 3,
44*4882a593Smuzhiyun "Number of jiffies between shuffles, 0=disable");
45*4882a593Smuzhiyun torture_param(int, shutdown_secs, 0, "Shutdown time (j), <= zero to disable.");
46*4882a593Smuzhiyun torture_param(int, stat_interval, 60,
47*4882a593Smuzhiyun "Number of seconds between stats printk()s");
48*4882a593Smuzhiyun torture_param(int, stutter, 5, "Number of jiffies to run/halt test, 0=disable");
49*4882a593Smuzhiyun torture_param(int, verbose, 1,
50*4882a593Smuzhiyun "Enable verbose debugging printk()s");
51*4882a593Smuzhiyun
52*4882a593Smuzhiyun static char *torture_type = "spin_lock";
53*4882a593Smuzhiyun module_param(torture_type, charp, 0444);
54*4882a593Smuzhiyun MODULE_PARM_DESC(torture_type,
55*4882a593Smuzhiyun "Type of lock to torture (spin_lock, spin_lock_irq, mutex_lock, ...)");
56*4882a593Smuzhiyun
57*4882a593Smuzhiyun static struct task_struct *stats_task;
58*4882a593Smuzhiyun static struct task_struct **writer_tasks;
59*4882a593Smuzhiyun static struct task_struct **reader_tasks;
60*4882a593Smuzhiyun
61*4882a593Smuzhiyun static bool lock_is_write_held;
62*4882a593Smuzhiyun static bool lock_is_read_held;
63*4882a593Smuzhiyun
64*4882a593Smuzhiyun struct lock_stress_stats {
65*4882a593Smuzhiyun long n_lock_fail;
66*4882a593Smuzhiyun long n_lock_acquired;
67*4882a593Smuzhiyun };
68*4882a593Smuzhiyun
69*4882a593Smuzhiyun /* Forward reference. */
70*4882a593Smuzhiyun static void lock_torture_cleanup(void);
71*4882a593Smuzhiyun
72*4882a593Smuzhiyun /*
73*4882a593Smuzhiyun * Operations vector for selecting different types of tests.
74*4882a593Smuzhiyun */
75*4882a593Smuzhiyun struct lock_torture_ops {
76*4882a593Smuzhiyun void (*init)(void);
77*4882a593Smuzhiyun int (*writelock)(void);
78*4882a593Smuzhiyun void (*write_delay)(struct torture_random_state *trsp);
79*4882a593Smuzhiyun void (*task_boost)(struct torture_random_state *trsp);
80*4882a593Smuzhiyun void (*writeunlock)(void);
81*4882a593Smuzhiyun int (*readlock)(void);
82*4882a593Smuzhiyun void (*read_delay)(struct torture_random_state *trsp);
83*4882a593Smuzhiyun void (*readunlock)(void);
84*4882a593Smuzhiyun
85*4882a593Smuzhiyun unsigned long flags; /* for irq spinlocks */
86*4882a593Smuzhiyun const char *name;
87*4882a593Smuzhiyun };
88*4882a593Smuzhiyun
89*4882a593Smuzhiyun struct lock_torture_cxt {
90*4882a593Smuzhiyun int nrealwriters_stress;
91*4882a593Smuzhiyun int nrealreaders_stress;
92*4882a593Smuzhiyun bool debug_lock;
93*4882a593Smuzhiyun atomic_t n_lock_torture_errors;
94*4882a593Smuzhiyun struct lock_torture_ops *cur_ops;
95*4882a593Smuzhiyun struct lock_stress_stats *lwsa; /* writer statistics */
96*4882a593Smuzhiyun struct lock_stress_stats *lrsa; /* reader statistics */
97*4882a593Smuzhiyun };
98*4882a593Smuzhiyun static struct lock_torture_cxt cxt = { 0, 0, false,
99*4882a593Smuzhiyun ATOMIC_INIT(0),
100*4882a593Smuzhiyun NULL, NULL};
101*4882a593Smuzhiyun /*
102*4882a593Smuzhiyun * Definitions for lock torture testing.
103*4882a593Smuzhiyun */
104*4882a593Smuzhiyun
torture_lock_busted_write_lock(void)105*4882a593Smuzhiyun static int torture_lock_busted_write_lock(void)
106*4882a593Smuzhiyun {
107*4882a593Smuzhiyun return 0; /* BUGGY, do not use in real life!!! */
108*4882a593Smuzhiyun }
109*4882a593Smuzhiyun
torture_lock_busted_write_delay(struct torture_random_state * trsp)110*4882a593Smuzhiyun static void torture_lock_busted_write_delay(struct torture_random_state *trsp)
111*4882a593Smuzhiyun {
112*4882a593Smuzhiyun const unsigned long longdelay_ms = 100;
113*4882a593Smuzhiyun
114*4882a593Smuzhiyun /* We want a long delay occasionally to force massive contention. */
115*4882a593Smuzhiyun if (!(torture_random(trsp) %
116*4882a593Smuzhiyun (cxt.nrealwriters_stress * 2000 * longdelay_ms)))
117*4882a593Smuzhiyun mdelay(longdelay_ms);
118*4882a593Smuzhiyun if (!(torture_random(trsp) % (cxt.nrealwriters_stress * 20000)))
119*4882a593Smuzhiyun torture_preempt_schedule(); /* Allow test to be preempted. */
120*4882a593Smuzhiyun }
121*4882a593Smuzhiyun
torture_lock_busted_write_unlock(void)122*4882a593Smuzhiyun static void torture_lock_busted_write_unlock(void)
123*4882a593Smuzhiyun {
124*4882a593Smuzhiyun /* BUGGY, do not use in real life!!! */
125*4882a593Smuzhiyun }
126*4882a593Smuzhiyun
torture_boost_dummy(struct torture_random_state * trsp)127*4882a593Smuzhiyun static void torture_boost_dummy(struct torture_random_state *trsp)
128*4882a593Smuzhiyun {
129*4882a593Smuzhiyun /* Only rtmutexes care about priority */
130*4882a593Smuzhiyun }
131*4882a593Smuzhiyun
132*4882a593Smuzhiyun static struct lock_torture_ops lock_busted_ops = {
133*4882a593Smuzhiyun .writelock = torture_lock_busted_write_lock,
134*4882a593Smuzhiyun .write_delay = torture_lock_busted_write_delay,
135*4882a593Smuzhiyun .task_boost = torture_boost_dummy,
136*4882a593Smuzhiyun .writeunlock = torture_lock_busted_write_unlock,
137*4882a593Smuzhiyun .readlock = NULL,
138*4882a593Smuzhiyun .read_delay = NULL,
139*4882a593Smuzhiyun .readunlock = NULL,
140*4882a593Smuzhiyun .name = "lock_busted"
141*4882a593Smuzhiyun };
142*4882a593Smuzhiyun
143*4882a593Smuzhiyun static DEFINE_SPINLOCK(torture_spinlock);
144*4882a593Smuzhiyun
torture_spin_lock_write_lock(void)145*4882a593Smuzhiyun static int torture_spin_lock_write_lock(void) __acquires(torture_spinlock)
146*4882a593Smuzhiyun {
147*4882a593Smuzhiyun spin_lock(&torture_spinlock);
148*4882a593Smuzhiyun return 0;
149*4882a593Smuzhiyun }
150*4882a593Smuzhiyun
torture_spin_lock_write_delay(struct torture_random_state * trsp)151*4882a593Smuzhiyun static void torture_spin_lock_write_delay(struct torture_random_state *trsp)
152*4882a593Smuzhiyun {
153*4882a593Smuzhiyun const unsigned long shortdelay_us = 2;
154*4882a593Smuzhiyun const unsigned long longdelay_ms = 100;
155*4882a593Smuzhiyun
156*4882a593Smuzhiyun /* We want a short delay mostly to emulate likely code, and
157*4882a593Smuzhiyun * we want a long delay occasionally to force massive contention.
158*4882a593Smuzhiyun */
159*4882a593Smuzhiyun if (!(torture_random(trsp) %
160*4882a593Smuzhiyun (cxt.nrealwriters_stress * 2000 * longdelay_ms)))
161*4882a593Smuzhiyun mdelay(longdelay_ms);
162*4882a593Smuzhiyun if (!(torture_random(trsp) %
163*4882a593Smuzhiyun (cxt.nrealwriters_stress * 2 * shortdelay_us)))
164*4882a593Smuzhiyun udelay(shortdelay_us);
165*4882a593Smuzhiyun if (!(torture_random(trsp) % (cxt.nrealwriters_stress * 20000)))
166*4882a593Smuzhiyun torture_preempt_schedule(); /* Allow test to be preempted. */
167*4882a593Smuzhiyun }
168*4882a593Smuzhiyun
torture_spin_lock_write_unlock(void)169*4882a593Smuzhiyun static void torture_spin_lock_write_unlock(void) __releases(torture_spinlock)
170*4882a593Smuzhiyun {
171*4882a593Smuzhiyun spin_unlock(&torture_spinlock);
172*4882a593Smuzhiyun }
173*4882a593Smuzhiyun
174*4882a593Smuzhiyun static struct lock_torture_ops spin_lock_ops = {
175*4882a593Smuzhiyun .writelock = torture_spin_lock_write_lock,
176*4882a593Smuzhiyun .write_delay = torture_spin_lock_write_delay,
177*4882a593Smuzhiyun .task_boost = torture_boost_dummy,
178*4882a593Smuzhiyun .writeunlock = torture_spin_lock_write_unlock,
179*4882a593Smuzhiyun .readlock = NULL,
180*4882a593Smuzhiyun .read_delay = NULL,
181*4882a593Smuzhiyun .readunlock = NULL,
182*4882a593Smuzhiyun .name = "spin_lock"
183*4882a593Smuzhiyun };
184*4882a593Smuzhiyun
torture_spin_lock_write_lock_irq(void)185*4882a593Smuzhiyun static int torture_spin_lock_write_lock_irq(void)
186*4882a593Smuzhiyun __acquires(torture_spinlock)
187*4882a593Smuzhiyun {
188*4882a593Smuzhiyun unsigned long flags;
189*4882a593Smuzhiyun
190*4882a593Smuzhiyun spin_lock_irqsave(&torture_spinlock, flags);
191*4882a593Smuzhiyun cxt.cur_ops->flags = flags;
192*4882a593Smuzhiyun return 0;
193*4882a593Smuzhiyun }
194*4882a593Smuzhiyun
torture_lock_spin_write_unlock_irq(void)195*4882a593Smuzhiyun static void torture_lock_spin_write_unlock_irq(void)
196*4882a593Smuzhiyun __releases(torture_spinlock)
197*4882a593Smuzhiyun {
198*4882a593Smuzhiyun spin_unlock_irqrestore(&torture_spinlock, cxt.cur_ops->flags);
199*4882a593Smuzhiyun }
200*4882a593Smuzhiyun
201*4882a593Smuzhiyun static struct lock_torture_ops spin_lock_irq_ops = {
202*4882a593Smuzhiyun .writelock = torture_spin_lock_write_lock_irq,
203*4882a593Smuzhiyun .write_delay = torture_spin_lock_write_delay,
204*4882a593Smuzhiyun .task_boost = torture_boost_dummy,
205*4882a593Smuzhiyun .writeunlock = torture_lock_spin_write_unlock_irq,
206*4882a593Smuzhiyun .readlock = NULL,
207*4882a593Smuzhiyun .read_delay = NULL,
208*4882a593Smuzhiyun .readunlock = NULL,
209*4882a593Smuzhiyun .name = "spin_lock_irq"
210*4882a593Smuzhiyun };
211*4882a593Smuzhiyun
212*4882a593Smuzhiyun static DEFINE_RWLOCK(torture_rwlock);
213*4882a593Smuzhiyun
torture_rwlock_write_lock(void)214*4882a593Smuzhiyun static int torture_rwlock_write_lock(void) __acquires(torture_rwlock)
215*4882a593Smuzhiyun {
216*4882a593Smuzhiyun write_lock(&torture_rwlock);
217*4882a593Smuzhiyun return 0;
218*4882a593Smuzhiyun }
219*4882a593Smuzhiyun
torture_rwlock_write_delay(struct torture_random_state * trsp)220*4882a593Smuzhiyun static void torture_rwlock_write_delay(struct torture_random_state *trsp)
221*4882a593Smuzhiyun {
222*4882a593Smuzhiyun const unsigned long shortdelay_us = 2;
223*4882a593Smuzhiyun const unsigned long longdelay_ms = 100;
224*4882a593Smuzhiyun
225*4882a593Smuzhiyun /* We want a short delay mostly to emulate likely code, and
226*4882a593Smuzhiyun * we want a long delay occasionally to force massive contention.
227*4882a593Smuzhiyun */
228*4882a593Smuzhiyun if (!(torture_random(trsp) %
229*4882a593Smuzhiyun (cxt.nrealwriters_stress * 2000 * longdelay_ms)))
230*4882a593Smuzhiyun mdelay(longdelay_ms);
231*4882a593Smuzhiyun else
232*4882a593Smuzhiyun udelay(shortdelay_us);
233*4882a593Smuzhiyun }
234*4882a593Smuzhiyun
torture_rwlock_write_unlock(void)235*4882a593Smuzhiyun static void torture_rwlock_write_unlock(void) __releases(torture_rwlock)
236*4882a593Smuzhiyun {
237*4882a593Smuzhiyun write_unlock(&torture_rwlock);
238*4882a593Smuzhiyun }
239*4882a593Smuzhiyun
torture_rwlock_read_lock(void)240*4882a593Smuzhiyun static int torture_rwlock_read_lock(void) __acquires(torture_rwlock)
241*4882a593Smuzhiyun {
242*4882a593Smuzhiyun read_lock(&torture_rwlock);
243*4882a593Smuzhiyun return 0;
244*4882a593Smuzhiyun }
245*4882a593Smuzhiyun
torture_rwlock_read_delay(struct torture_random_state * trsp)246*4882a593Smuzhiyun static void torture_rwlock_read_delay(struct torture_random_state *trsp)
247*4882a593Smuzhiyun {
248*4882a593Smuzhiyun const unsigned long shortdelay_us = 10;
249*4882a593Smuzhiyun const unsigned long longdelay_ms = 100;
250*4882a593Smuzhiyun
251*4882a593Smuzhiyun /* We want a short delay mostly to emulate likely code, and
252*4882a593Smuzhiyun * we want a long delay occasionally to force massive contention.
253*4882a593Smuzhiyun */
254*4882a593Smuzhiyun if (!(torture_random(trsp) %
255*4882a593Smuzhiyun (cxt.nrealreaders_stress * 2000 * longdelay_ms)))
256*4882a593Smuzhiyun mdelay(longdelay_ms);
257*4882a593Smuzhiyun else
258*4882a593Smuzhiyun udelay(shortdelay_us);
259*4882a593Smuzhiyun }
260*4882a593Smuzhiyun
torture_rwlock_read_unlock(void)261*4882a593Smuzhiyun static void torture_rwlock_read_unlock(void) __releases(torture_rwlock)
262*4882a593Smuzhiyun {
263*4882a593Smuzhiyun read_unlock(&torture_rwlock);
264*4882a593Smuzhiyun }
265*4882a593Smuzhiyun
266*4882a593Smuzhiyun static struct lock_torture_ops rw_lock_ops = {
267*4882a593Smuzhiyun .writelock = torture_rwlock_write_lock,
268*4882a593Smuzhiyun .write_delay = torture_rwlock_write_delay,
269*4882a593Smuzhiyun .task_boost = torture_boost_dummy,
270*4882a593Smuzhiyun .writeunlock = torture_rwlock_write_unlock,
271*4882a593Smuzhiyun .readlock = torture_rwlock_read_lock,
272*4882a593Smuzhiyun .read_delay = torture_rwlock_read_delay,
273*4882a593Smuzhiyun .readunlock = torture_rwlock_read_unlock,
274*4882a593Smuzhiyun .name = "rw_lock"
275*4882a593Smuzhiyun };
276*4882a593Smuzhiyun
torture_rwlock_write_lock_irq(void)277*4882a593Smuzhiyun static int torture_rwlock_write_lock_irq(void) __acquires(torture_rwlock)
278*4882a593Smuzhiyun {
279*4882a593Smuzhiyun unsigned long flags;
280*4882a593Smuzhiyun
281*4882a593Smuzhiyun write_lock_irqsave(&torture_rwlock, flags);
282*4882a593Smuzhiyun cxt.cur_ops->flags = flags;
283*4882a593Smuzhiyun return 0;
284*4882a593Smuzhiyun }
285*4882a593Smuzhiyun
torture_rwlock_write_unlock_irq(void)286*4882a593Smuzhiyun static void torture_rwlock_write_unlock_irq(void)
287*4882a593Smuzhiyun __releases(torture_rwlock)
288*4882a593Smuzhiyun {
289*4882a593Smuzhiyun write_unlock_irqrestore(&torture_rwlock, cxt.cur_ops->flags);
290*4882a593Smuzhiyun }
291*4882a593Smuzhiyun
torture_rwlock_read_lock_irq(void)292*4882a593Smuzhiyun static int torture_rwlock_read_lock_irq(void) __acquires(torture_rwlock)
293*4882a593Smuzhiyun {
294*4882a593Smuzhiyun unsigned long flags;
295*4882a593Smuzhiyun
296*4882a593Smuzhiyun read_lock_irqsave(&torture_rwlock, flags);
297*4882a593Smuzhiyun cxt.cur_ops->flags = flags;
298*4882a593Smuzhiyun return 0;
299*4882a593Smuzhiyun }
300*4882a593Smuzhiyun
torture_rwlock_read_unlock_irq(void)301*4882a593Smuzhiyun static void torture_rwlock_read_unlock_irq(void)
302*4882a593Smuzhiyun __releases(torture_rwlock)
303*4882a593Smuzhiyun {
304*4882a593Smuzhiyun read_unlock_irqrestore(&torture_rwlock, cxt.cur_ops->flags);
305*4882a593Smuzhiyun }
306*4882a593Smuzhiyun
307*4882a593Smuzhiyun static struct lock_torture_ops rw_lock_irq_ops = {
308*4882a593Smuzhiyun .writelock = torture_rwlock_write_lock_irq,
309*4882a593Smuzhiyun .write_delay = torture_rwlock_write_delay,
310*4882a593Smuzhiyun .task_boost = torture_boost_dummy,
311*4882a593Smuzhiyun .writeunlock = torture_rwlock_write_unlock_irq,
312*4882a593Smuzhiyun .readlock = torture_rwlock_read_lock_irq,
313*4882a593Smuzhiyun .read_delay = torture_rwlock_read_delay,
314*4882a593Smuzhiyun .readunlock = torture_rwlock_read_unlock_irq,
315*4882a593Smuzhiyun .name = "rw_lock_irq"
316*4882a593Smuzhiyun };
317*4882a593Smuzhiyun
318*4882a593Smuzhiyun static DEFINE_MUTEX(torture_mutex);
319*4882a593Smuzhiyun
torture_mutex_lock(void)320*4882a593Smuzhiyun static int torture_mutex_lock(void) __acquires(torture_mutex)
321*4882a593Smuzhiyun {
322*4882a593Smuzhiyun mutex_lock(&torture_mutex);
323*4882a593Smuzhiyun return 0;
324*4882a593Smuzhiyun }
325*4882a593Smuzhiyun
torture_mutex_delay(struct torture_random_state * trsp)326*4882a593Smuzhiyun static void torture_mutex_delay(struct torture_random_state *trsp)
327*4882a593Smuzhiyun {
328*4882a593Smuzhiyun const unsigned long longdelay_ms = 100;
329*4882a593Smuzhiyun
330*4882a593Smuzhiyun /* We want a long delay occasionally to force massive contention. */
331*4882a593Smuzhiyun if (!(torture_random(trsp) %
332*4882a593Smuzhiyun (cxt.nrealwriters_stress * 2000 * longdelay_ms)))
333*4882a593Smuzhiyun mdelay(longdelay_ms * 5);
334*4882a593Smuzhiyun else
335*4882a593Smuzhiyun mdelay(longdelay_ms / 5);
336*4882a593Smuzhiyun if (!(torture_random(trsp) % (cxt.nrealwriters_stress * 20000)))
337*4882a593Smuzhiyun torture_preempt_schedule(); /* Allow test to be preempted. */
338*4882a593Smuzhiyun }
339*4882a593Smuzhiyun
torture_mutex_unlock(void)340*4882a593Smuzhiyun static void torture_mutex_unlock(void) __releases(torture_mutex)
341*4882a593Smuzhiyun {
342*4882a593Smuzhiyun mutex_unlock(&torture_mutex);
343*4882a593Smuzhiyun }
344*4882a593Smuzhiyun
345*4882a593Smuzhiyun static struct lock_torture_ops mutex_lock_ops = {
346*4882a593Smuzhiyun .writelock = torture_mutex_lock,
347*4882a593Smuzhiyun .write_delay = torture_mutex_delay,
348*4882a593Smuzhiyun .task_boost = torture_boost_dummy,
349*4882a593Smuzhiyun .writeunlock = torture_mutex_unlock,
350*4882a593Smuzhiyun .readlock = NULL,
351*4882a593Smuzhiyun .read_delay = NULL,
352*4882a593Smuzhiyun .readunlock = NULL,
353*4882a593Smuzhiyun .name = "mutex_lock"
354*4882a593Smuzhiyun };
355*4882a593Smuzhiyun
356*4882a593Smuzhiyun #include <linux/ww_mutex.h>
357*4882a593Smuzhiyun static DEFINE_WD_CLASS(torture_ww_class);
358*4882a593Smuzhiyun static DEFINE_WW_MUTEX(torture_ww_mutex_0, &torture_ww_class);
359*4882a593Smuzhiyun static DEFINE_WW_MUTEX(torture_ww_mutex_1, &torture_ww_class);
360*4882a593Smuzhiyun static DEFINE_WW_MUTEX(torture_ww_mutex_2, &torture_ww_class);
361*4882a593Smuzhiyun
torture_ww_mutex_lock(void)362*4882a593Smuzhiyun static int torture_ww_mutex_lock(void)
363*4882a593Smuzhiyun __acquires(torture_ww_mutex_0)
364*4882a593Smuzhiyun __acquires(torture_ww_mutex_1)
365*4882a593Smuzhiyun __acquires(torture_ww_mutex_2)
366*4882a593Smuzhiyun {
367*4882a593Smuzhiyun LIST_HEAD(list);
368*4882a593Smuzhiyun struct reorder_lock {
369*4882a593Smuzhiyun struct list_head link;
370*4882a593Smuzhiyun struct ww_mutex *lock;
371*4882a593Smuzhiyun } locks[3], *ll, *ln;
372*4882a593Smuzhiyun struct ww_acquire_ctx ctx;
373*4882a593Smuzhiyun
374*4882a593Smuzhiyun locks[0].lock = &torture_ww_mutex_0;
375*4882a593Smuzhiyun list_add(&locks[0].link, &list);
376*4882a593Smuzhiyun
377*4882a593Smuzhiyun locks[1].lock = &torture_ww_mutex_1;
378*4882a593Smuzhiyun list_add(&locks[1].link, &list);
379*4882a593Smuzhiyun
380*4882a593Smuzhiyun locks[2].lock = &torture_ww_mutex_2;
381*4882a593Smuzhiyun list_add(&locks[2].link, &list);
382*4882a593Smuzhiyun
383*4882a593Smuzhiyun ww_acquire_init(&ctx, &torture_ww_class);
384*4882a593Smuzhiyun
385*4882a593Smuzhiyun list_for_each_entry(ll, &list, link) {
386*4882a593Smuzhiyun int err;
387*4882a593Smuzhiyun
388*4882a593Smuzhiyun err = ww_mutex_lock(ll->lock, &ctx);
389*4882a593Smuzhiyun if (!err)
390*4882a593Smuzhiyun continue;
391*4882a593Smuzhiyun
392*4882a593Smuzhiyun ln = ll;
393*4882a593Smuzhiyun list_for_each_entry_continue_reverse(ln, &list, link)
394*4882a593Smuzhiyun ww_mutex_unlock(ln->lock);
395*4882a593Smuzhiyun
396*4882a593Smuzhiyun if (err != -EDEADLK)
397*4882a593Smuzhiyun return err;
398*4882a593Smuzhiyun
399*4882a593Smuzhiyun ww_mutex_lock_slow(ll->lock, &ctx);
400*4882a593Smuzhiyun list_move(&ll->link, &list);
401*4882a593Smuzhiyun }
402*4882a593Smuzhiyun
403*4882a593Smuzhiyun ww_acquire_fini(&ctx);
404*4882a593Smuzhiyun return 0;
405*4882a593Smuzhiyun }
406*4882a593Smuzhiyun
torture_ww_mutex_unlock(void)407*4882a593Smuzhiyun static void torture_ww_mutex_unlock(void)
408*4882a593Smuzhiyun __releases(torture_ww_mutex_0)
409*4882a593Smuzhiyun __releases(torture_ww_mutex_1)
410*4882a593Smuzhiyun __releases(torture_ww_mutex_2)
411*4882a593Smuzhiyun {
412*4882a593Smuzhiyun ww_mutex_unlock(&torture_ww_mutex_0);
413*4882a593Smuzhiyun ww_mutex_unlock(&torture_ww_mutex_1);
414*4882a593Smuzhiyun ww_mutex_unlock(&torture_ww_mutex_2);
415*4882a593Smuzhiyun }
416*4882a593Smuzhiyun
417*4882a593Smuzhiyun static struct lock_torture_ops ww_mutex_lock_ops = {
418*4882a593Smuzhiyun .writelock = torture_ww_mutex_lock,
419*4882a593Smuzhiyun .write_delay = torture_mutex_delay,
420*4882a593Smuzhiyun .task_boost = torture_boost_dummy,
421*4882a593Smuzhiyun .writeunlock = torture_ww_mutex_unlock,
422*4882a593Smuzhiyun .readlock = NULL,
423*4882a593Smuzhiyun .read_delay = NULL,
424*4882a593Smuzhiyun .readunlock = NULL,
425*4882a593Smuzhiyun .name = "ww_mutex_lock"
426*4882a593Smuzhiyun };
427*4882a593Smuzhiyun
428*4882a593Smuzhiyun #ifdef CONFIG_RT_MUTEXES
429*4882a593Smuzhiyun static DEFINE_RT_MUTEX(torture_rtmutex);
430*4882a593Smuzhiyun
torture_rtmutex_lock(void)431*4882a593Smuzhiyun static int torture_rtmutex_lock(void) __acquires(torture_rtmutex)
432*4882a593Smuzhiyun {
433*4882a593Smuzhiyun rt_mutex_lock(&torture_rtmutex);
434*4882a593Smuzhiyun return 0;
435*4882a593Smuzhiyun }
436*4882a593Smuzhiyun
torture_rtmutex_boost(struct torture_random_state * trsp)437*4882a593Smuzhiyun static void torture_rtmutex_boost(struct torture_random_state *trsp)
438*4882a593Smuzhiyun {
439*4882a593Smuzhiyun const unsigned int factor = 50000; /* yes, quite arbitrary */
440*4882a593Smuzhiyun
441*4882a593Smuzhiyun if (!rt_task(current)) {
442*4882a593Smuzhiyun /*
443*4882a593Smuzhiyun * Boost priority once every ~50k operations. When the
444*4882a593Smuzhiyun * task tries to take the lock, the rtmutex it will account
445*4882a593Smuzhiyun * for the new priority, and do any corresponding pi-dance.
446*4882a593Smuzhiyun */
447*4882a593Smuzhiyun if (trsp && !(torture_random(trsp) %
448*4882a593Smuzhiyun (cxt.nrealwriters_stress * factor))) {
449*4882a593Smuzhiyun sched_set_fifo(current);
450*4882a593Smuzhiyun } else /* common case, do nothing */
451*4882a593Smuzhiyun return;
452*4882a593Smuzhiyun } else {
453*4882a593Smuzhiyun /*
454*4882a593Smuzhiyun * The task will remain boosted for another ~500k operations,
455*4882a593Smuzhiyun * then restored back to its original prio, and so forth.
456*4882a593Smuzhiyun *
457*4882a593Smuzhiyun * When @trsp is nil, we want to force-reset the task for
458*4882a593Smuzhiyun * stopping the kthread.
459*4882a593Smuzhiyun */
460*4882a593Smuzhiyun if (!trsp || !(torture_random(trsp) %
461*4882a593Smuzhiyun (cxt.nrealwriters_stress * factor * 2))) {
462*4882a593Smuzhiyun sched_set_normal(current, 0);
463*4882a593Smuzhiyun } else /* common case, do nothing */
464*4882a593Smuzhiyun return;
465*4882a593Smuzhiyun }
466*4882a593Smuzhiyun }
467*4882a593Smuzhiyun
torture_rtmutex_delay(struct torture_random_state * trsp)468*4882a593Smuzhiyun static void torture_rtmutex_delay(struct torture_random_state *trsp)
469*4882a593Smuzhiyun {
470*4882a593Smuzhiyun const unsigned long shortdelay_us = 2;
471*4882a593Smuzhiyun const unsigned long longdelay_ms = 100;
472*4882a593Smuzhiyun
473*4882a593Smuzhiyun /*
474*4882a593Smuzhiyun * We want a short delay mostly to emulate likely code, and
475*4882a593Smuzhiyun * we want a long delay occasionally to force massive contention.
476*4882a593Smuzhiyun */
477*4882a593Smuzhiyun if (!(torture_random(trsp) %
478*4882a593Smuzhiyun (cxt.nrealwriters_stress * 2000 * longdelay_ms)))
479*4882a593Smuzhiyun mdelay(longdelay_ms);
480*4882a593Smuzhiyun if (!(torture_random(trsp) %
481*4882a593Smuzhiyun (cxt.nrealwriters_stress * 2 * shortdelay_us)))
482*4882a593Smuzhiyun udelay(shortdelay_us);
483*4882a593Smuzhiyun if (!(torture_random(trsp) % (cxt.nrealwriters_stress * 20000)))
484*4882a593Smuzhiyun torture_preempt_schedule(); /* Allow test to be preempted. */
485*4882a593Smuzhiyun }
486*4882a593Smuzhiyun
torture_rtmutex_unlock(void)487*4882a593Smuzhiyun static void torture_rtmutex_unlock(void) __releases(torture_rtmutex)
488*4882a593Smuzhiyun {
489*4882a593Smuzhiyun rt_mutex_unlock(&torture_rtmutex);
490*4882a593Smuzhiyun }
491*4882a593Smuzhiyun
492*4882a593Smuzhiyun static struct lock_torture_ops rtmutex_lock_ops = {
493*4882a593Smuzhiyun .writelock = torture_rtmutex_lock,
494*4882a593Smuzhiyun .write_delay = torture_rtmutex_delay,
495*4882a593Smuzhiyun .task_boost = torture_rtmutex_boost,
496*4882a593Smuzhiyun .writeunlock = torture_rtmutex_unlock,
497*4882a593Smuzhiyun .readlock = NULL,
498*4882a593Smuzhiyun .read_delay = NULL,
499*4882a593Smuzhiyun .readunlock = NULL,
500*4882a593Smuzhiyun .name = "rtmutex_lock"
501*4882a593Smuzhiyun };
502*4882a593Smuzhiyun #endif
503*4882a593Smuzhiyun
504*4882a593Smuzhiyun static DECLARE_RWSEM(torture_rwsem);
torture_rwsem_down_write(void)505*4882a593Smuzhiyun static int torture_rwsem_down_write(void) __acquires(torture_rwsem)
506*4882a593Smuzhiyun {
507*4882a593Smuzhiyun down_write(&torture_rwsem);
508*4882a593Smuzhiyun return 0;
509*4882a593Smuzhiyun }
510*4882a593Smuzhiyun
torture_rwsem_write_delay(struct torture_random_state * trsp)511*4882a593Smuzhiyun static void torture_rwsem_write_delay(struct torture_random_state *trsp)
512*4882a593Smuzhiyun {
513*4882a593Smuzhiyun const unsigned long longdelay_ms = 100;
514*4882a593Smuzhiyun
515*4882a593Smuzhiyun /* We want a long delay occasionally to force massive contention. */
516*4882a593Smuzhiyun if (!(torture_random(trsp) %
517*4882a593Smuzhiyun (cxt.nrealwriters_stress * 2000 * longdelay_ms)))
518*4882a593Smuzhiyun mdelay(longdelay_ms * 10);
519*4882a593Smuzhiyun else
520*4882a593Smuzhiyun mdelay(longdelay_ms / 10);
521*4882a593Smuzhiyun if (!(torture_random(trsp) % (cxt.nrealwriters_stress * 20000)))
522*4882a593Smuzhiyun torture_preempt_schedule(); /* Allow test to be preempted. */
523*4882a593Smuzhiyun }
524*4882a593Smuzhiyun
torture_rwsem_up_write(void)525*4882a593Smuzhiyun static void torture_rwsem_up_write(void) __releases(torture_rwsem)
526*4882a593Smuzhiyun {
527*4882a593Smuzhiyun up_write(&torture_rwsem);
528*4882a593Smuzhiyun }
529*4882a593Smuzhiyun
torture_rwsem_down_read(void)530*4882a593Smuzhiyun static int torture_rwsem_down_read(void) __acquires(torture_rwsem)
531*4882a593Smuzhiyun {
532*4882a593Smuzhiyun down_read(&torture_rwsem);
533*4882a593Smuzhiyun return 0;
534*4882a593Smuzhiyun }
535*4882a593Smuzhiyun
torture_rwsem_read_delay(struct torture_random_state * trsp)536*4882a593Smuzhiyun static void torture_rwsem_read_delay(struct torture_random_state *trsp)
537*4882a593Smuzhiyun {
538*4882a593Smuzhiyun const unsigned long longdelay_ms = 100;
539*4882a593Smuzhiyun
540*4882a593Smuzhiyun /* We want a long delay occasionally to force massive contention. */
541*4882a593Smuzhiyun if (!(torture_random(trsp) %
542*4882a593Smuzhiyun (cxt.nrealreaders_stress * 2000 * longdelay_ms)))
543*4882a593Smuzhiyun mdelay(longdelay_ms * 2);
544*4882a593Smuzhiyun else
545*4882a593Smuzhiyun mdelay(longdelay_ms / 2);
546*4882a593Smuzhiyun if (!(torture_random(trsp) % (cxt.nrealreaders_stress * 20000)))
547*4882a593Smuzhiyun torture_preempt_schedule(); /* Allow test to be preempted. */
548*4882a593Smuzhiyun }
549*4882a593Smuzhiyun
torture_rwsem_up_read(void)550*4882a593Smuzhiyun static void torture_rwsem_up_read(void) __releases(torture_rwsem)
551*4882a593Smuzhiyun {
552*4882a593Smuzhiyun up_read(&torture_rwsem);
553*4882a593Smuzhiyun }
554*4882a593Smuzhiyun
555*4882a593Smuzhiyun static struct lock_torture_ops rwsem_lock_ops = {
556*4882a593Smuzhiyun .writelock = torture_rwsem_down_write,
557*4882a593Smuzhiyun .write_delay = torture_rwsem_write_delay,
558*4882a593Smuzhiyun .task_boost = torture_boost_dummy,
559*4882a593Smuzhiyun .writeunlock = torture_rwsem_up_write,
560*4882a593Smuzhiyun .readlock = torture_rwsem_down_read,
561*4882a593Smuzhiyun .read_delay = torture_rwsem_read_delay,
562*4882a593Smuzhiyun .readunlock = torture_rwsem_up_read,
563*4882a593Smuzhiyun .name = "rwsem_lock"
564*4882a593Smuzhiyun };
565*4882a593Smuzhiyun
566*4882a593Smuzhiyun #include <linux/percpu-rwsem.h>
567*4882a593Smuzhiyun static struct percpu_rw_semaphore pcpu_rwsem;
568*4882a593Smuzhiyun
torture_percpu_rwsem_init(void)569*4882a593Smuzhiyun static void torture_percpu_rwsem_init(void)
570*4882a593Smuzhiyun {
571*4882a593Smuzhiyun BUG_ON(percpu_init_rwsem(&pcpu_rwsem));
572*4882a593Smuzhiyun }
573*4882a593Smuzhiyun
torture_percpu_rwsem_down_write(void)574*4882a593Smuzhiyun static int torture_percpu_rwsem_down_write(void) __acquires(pcpu_rwsem)
575*4882a593Smuzhiyun {
576*4882a593Smuzhiyun percpu_down_write(&pcpu_rwsem);
577*4882a593Smuzhiyun return 0;
578*4882a593Smuzhiyun }
579*4882a593Smuzhiyun
torture_percpu_rwsem_up_write(void)580*4882a593Smuzhiyun static void torture_percpu_rwsem_up_write(void) __releases(pcpu_rwsem)
581*4882a593Smuzhiyun {
582*4882a593Smuzhiyun percpu_up_write(&pcpu_rwsem);
583*4882a593Smuzhiyun }
584*4882a593Smuzhiyun
torture_percpu_rwsem_down_read(void)585*4882a593Smuzhiyun static int torture_percpu_rwsem_down_read(void) __acquires(pcpu_rwsem)
586*4882a593Smuzhiyun {
587*4882a593Smuzhiyun percpu_down_read(&pcpu_rwsem);
588*4882a593Smuzhiyun return 0;
589*4882a593Smuzhiyun }
590*4882a593Smuzhiyun
torture_percpu_rwsem_up_read(void)591*4882a593Smuzhiyun static void torture_percpu_rwsem_up_read(void) __releases(pcpu_rwsem)
592*4882a593Smuzhiyun {
593*4882a593Smuzhiyun percpu_up_read(&pcpu_rwsem);
594*4882a593Smuzhiyun }
595*4882a593Smuzhiyun
596*4882a593Smuzhiyun static struct lock_torture_ops percpu_rwsem_lock_ops = {
597*4882a593Smuzhiyun .init = torture_percpu_rwsem_init,
598*4882a593Smuzhiyun .writelock = torture_percpu_rwsem_down_write,
599*4882a593Smuzhiyun .write_delay = torture_rwsem_write_delay,
600*4882a593Smuzhiyun .task_boost = torture_boost_dummy,
601*4882a593Smuzhiyun .writeunlock = torture_percpu_rwsem_up_write,
602*4882a593Smuzhiyun .readlock = torture_percpu_rwsem_down_read,
603*4882a593Smuzhiyun .read_delay = torture_rwsem_read_delay,
604*4882a593Smuzhiyun .readunlock = torture_percpu_rwsem_up_read,
605*4882a593Smuzhiyun .name = "percpu_rwsem_lock"
606*4882a593Smuzhiyun };
607*4882a593Smuzhiyun
608*4882a593Smuzhiyun /*
609*4882a593Smuzhiyun * Lock torture writer kthread. Repeatedly acquires and releases
610*4882a593Smuzhiyun * the lock, checking for duplicate acquisitions.
611*4882a593Smuzhiyun */
lock_torture_writer(void * arg)612*4882a593Smuzhiyun static int lock_torture_writer(void *arg)
613*4882a593Smuzhiyun {
614*4882a593Smuzhiyun struct lock_stress_stats *lwsp = arg;
615*4882a593Smuzhiyun DEFINE_TORTURE_RANDOM(rand);
616*4882a593Smuzhiyun
617*4882a593Smuzhiyun VERBOSE_TOROUT_STRING("lock_torture_writer task started");
618*4882a593Smuzhiyun set_user_nice(current, MAX_NICE);
619*4882a593Smuzhiyun
620*4882a593Smuzhiyun do {
621*4882a593Smuzhiyun if ((torture_random(&rand) & 0xfffff) == 0)
622*4882a593Smuzhiyun schedule_timeout_uninterruptible(1);
623*4882a593Smuzhiyun
624*4882a593Smuzhiyun cxt.cur_ops->task_boost(&rand);
625*4882a593Smuzhiyun cxt.cur_ops->writelock();
626*4882a593Smuzhiyun if (WARN_ON_ONCE(lock_is_write_held))
627*4882a593Smuzhiyun lwsp->n_lock_fail++;
628*4882a593Smuzhiyun lock_is_write_held = true;
629*4882a593Smuzhiyun if (WARN_ON_ONCE(lock_is_read_held))
630*4882a593Smuzhiyun lwsp->n_lock_fail++; /* rare, but... */
631*4882a593Smuzhiyun
632*4882a593Smuzhiyun lwsp->n_lock_acquired++;
633*4882a593Smuzhiyun cxt.cur_ops->write_delay(&rand);
634*4882a593Smuzhiyun lock_is_write_held = false;
635*4882a593Smuzhiyun cxt.cur_ops->writeunlock();
636*4882a593Smuzhiyun
637*4882a593Smuzhiyun stutter_wait("lock_torture_writer");
638*4882a593Smuzhiyun } while (!torture_must_stop());
639*4882a593Smuzhiyun
640*4882a593Smuzhiyun cxt.cur_ops->task_boost(NULL); /* reset prio */
641*4882a593Smuzhiyun torture_kthread_stopping("lock_torture_writer");
642*4882a593Smuzhiyun return 0;
643*4882a593Smuzhiyun }
644*4882a593Smuzhiyun
645*4882a593Smuzhiyun /*
646*4882a593Smuzhiyun * Lock torture reader kthread. Repeatedly acquires and releases
647*4882a593Smuzhiyun * the reader lock.
648*4882a593Smuzhiyun */
lock_torture_reader(void * arg)649*4882a593Smuzhiyun static int lock_torture_reader(void *arg)
650*4882a593Smuzhiyun {
651*4882a593Smuzhiyun struct lock_stress_stats *lrsp = arg;
652*4882a593Smuzhiyun DEFINE_TORTURE_RANDOM(rand);
653*4882a593Smuzhiyun
654*4882a593Smuzhiyun VERBOSE_TOROUT_STRING("lock_torture_reader task started");
655*4882a593Smuzhiyun set_user_nice(current, MAX_NICE);
656*4882a593Smuzhiyun
657*4882a593Smuzhiyun do {
658*4882a593Smuzhiyun if ((torture_random(&rand) & 0xfffff) == 0)
659*4882a593Smuzhiyun schedule_timeout_uninterruptible(1);
660*4882a593Smuzhiyun
661*4882a593Smuzhiyun cxt.cur_ops->readlock();
662*4882a593Smuzhiyun lock_is_read_held = true;
663*4882a593Smuzhiyun if (WARN_ON_ONCE(lock_is_write_held))
664*4882a593Smuzhiyun lrsp->n_lock_fail++; /* rare, but... */
665*4882a593Smuzhiyun
666*4882a593Smuzhiyun lrsp->n_lock_acquired++;
667*4882a593Smuzhiyun cxt.cur_ops->read_delay(&rand);
668*4882a593Smuzhiyun lock_is_read_held = false;
669*4882a593Smuzhiyun cxt.cur_ops->readunlock();
670*4882a593Smuzhiyun
671*4882a593Smuzhiyun stutter_wait("lock_torture_reader");
672*4882a593Smuzhiyun } while (!torture_must_stop());
673*4882a593Smuzhiyun torture_kthread_stopping("lock_torture_reader");
674*4882a593Smuzhiyun return 0;
675*4882a593Smuzhiyun }
676*4882a593Smuzhiyun
677*4882a593Smuzhiyun /*
678*4882a593Smuzhiyun * Create an lock-torture-statistics message in the specified buffer.
679*4882a593Smuzhiyun */
__torture_print_stats(char * page,struct lock_stress_stats * statp,bool write)680*4882a593Smuzhiyun static void __torture_print_stats(char *page,
681*4882a593Smuzhiyun struct lock_stress_stats *statp, bool write)
682*4882a593Smuzhiyun {
683*4882a593Smuzhiyun bool fail = false;
684*4882a593Smuzhiyun int i, n_stress;
685*4882a593Smuzhiyun long max = 0, min = statp ? statp[0].n_lock_acquired : 0;
686*4882a593Smuzhiyun long long sum = 0;
687*4882a593Smuzhiyun
688*4882a593Smuzhiyun n_stress = write ? cxt.nrealwriters_stress : cxt.nrealreaders_stress;
689*4882a593Smuzhiyun for (i = 0; i < n_stress; i++) {
690*4882a593Smuzhiyun if (statp[i].n_lock_fail)
691*4882a593Smuzhiyun fail = true;
692*4882a593Smuzhiyun sum += statp[i].n_lock_acquired;
693*4882a593Smuzhiyun if (max < statp[i].n_lock_acquired)
694*4882a593Smuzhiyun max = statp[i].n_lock_acquired;
695*4882a593Smuzhiyun if (min > statp[i].n_lock_acquired)
696*4882a593Smuzhiyun min = statp[i].n_lock_acquired;
697*4882a593Smuzhiyun }
698*4882a593Smuzhiyun page += sprintf(page,
699*4882a593Smuzhiyun "%s: Total: %lld Max/Min: %ld/%ld %s Fail: %d %s\n",
700*4882a593Smuzhiyun write ? "Writes" : "Reads ",
701*4882a593Smuzhiyun sum, max, min,
702*4882a593Smuzhiyun !onoff_interval && max / 2 > min ? "???" : "",
703*4882a593Smuzhiyun fail, fail ? "!!!" : "");
704*4882a593Smuzhiyun if (fail)
705*4882a593Smuzhiyun atomic_inc(&cxt.n_lock_torture_errors);
706*4882a593Smuzhiyun }
707*4882a593Smuzhiyun
708*4882a593Smuzhiyun /*
709*4882a593Smuzhiyun * Print torture statistics. Caller must ensure that there is only one
710*4882a593Smuzhiyun * call to this function at a given time!!! This is normally accomplished
711*4882a593Smuzhiyun * by relying on the module system to only have one copy of the module
712*4882a593Smuzhiyun * loaded, and then by giving the lock_torture_stats kthread full control
713*4882a593Smuzhiyun * (or the init/cleanup functions when lock_torture_stats thread is not
714*4882a593Smuzhiyun * running).
715*4882a593Smuzhiyun */
lock_torture_stats_print(void)716*4882a593Smuzhiyun static void lock_torture_stats_print(void)
717*4882a593Smuzhiyun {
718*4882a593Smuzhiyun int size = cxt.nrealwriters_stress * 200 + 8192;
719*4882a593Smuzhiyun char *buf;
720*4882a593Smuzhiyun
721*4882a593Smuzhiyun if (cxt.cur_ops->readlock)
722*4882a593Smuzhiyun size += cxt.nrealreaders_stress * 200 + 8192;
723*4882a593Smuzhiyun
724*4882a593Smuzhiyun buf = kmalloc(size, GFP_KERNEL);
725*4882a593Smuzhiyun if (!buf) {
726*4882a593Smuzhiyun pr_err("lock_torture_stats_print: Out of memory, need: %d",
727*4882a593Smuzhiyun size);
728*4882a593Smuzhiyun return;
729*4882a593Smuzhiyun }
730*4882a593Smuzhiyun
731*4882a593Smuzhiyun __torture_print_stats(buf, cxt.lwsa, true);
732*4882a593Smuzhiyun pr_alert("%s", buf);
733*4882a593Smuzhiyun kfree(buf);
734*4882a593Smuzhiyun
735*4882a593Smuzhiyun if (cxt.cur_ops->readlock) {
736*4882a593Smuzhiyun buf = kmalloc(size, GFP_KERNEL);
737*4882a593Smuzhiyun if (!buf) {
738*4882a593Smuzhiyun pr_err("lock_torture_stats_print: Out of memory, need: %d",
739*4882a593Smuzhiyun size);
740*4882a593Smuzhiyun return;
741*4882a593Smuzhiyun }
742*4882a593Smuzhiyun
743*4882a593Smuzhiyun __torture_print_stats(buf, cxt.lrsa, false);
744*4882a593Smuzhiyun pr_alert("%s", buf);
745*4882a593Smuzhiyun kfree(buf);
746*4882a593Smuzhiyun }
747*4882a593Smuzhiyun }
748*4882a593Smuzhiyun
749*4882a593Smuzhiyun /*
750*4882a593Smuzhiyun * Periodically prints torture statistics, if periodic statistics printing
751*4882a593Smuzhiyun * was specified via the stat_interval module parameter.
752*4882a593Smuzhiyun *
753*4882a593Smuzhiyun * No need to worry about fullstop here, since this one doesn't reference
754*4882a593Smuzhiyun * volatile state or register callbacks.
755*4882a593Smuzhiyun */
lock_torture_stats(void * arg)756*4882a593Smuzhiyun static int lock_torture_stats(void *arg)
757*4882a593Smuzhiyun {
758*4882a593Smuzhiyun VERBOSE_TOROUT_STRING("lock_torture_stats task started");
759*4882a593Smuzhiyun do {
760*4882a593Smuzhiyun schedule_timeout_interruptible(stat_interval * HZ);
761*4882a593Smuzhiyun lock_torture_stats_print();
762*4882a593Smuzhiyun torture_shutdown_absorb("lock_torture_stats");
763*4882a593Smuzhiyun } while (!torture_must_stop());
764*4882a593Smuzhiyun torture_kthread_stopping("lock_torture_stats");
765*4882a593Smuzhiyun return 0;
766*4882a593Smuzhiyun }
767*4882a593Smuzhiyun
768*4882a593Smuzhiyun static inline void
lock_torture_print_module_parms(struct lock_torture_ops * cur_ops,const char * tag)769*4882a593Smuzhiyun lock_torture_print_module_parms(struct lock_torture_ops *cur_ops,
770*4882a593Smuzhiyun const char *tag)
771*4882a593Smuzhiyun {
772*4882a593Smuzhiyun pr_alert("%s" TORTURE_FLAG
773*4882a593Smuzhiyun "--- %s%s: nwriters_stress=%d nreaders_stress=%d stat_interval=%d verbose=%d shuffle_interval=%d stutter=%d shutdown_secs=%d onoff_interval=%d onoff_holdoff=%d\n",
774*4882a593Smuzhiyun torture_type, tag, cxt.debug_lock ? " [debug]": "",
775*4882a593Smuzhiyun cxt.nrealwriters_stress, cxt.nrealreaders_stress, stat_interval,
776*4882a593Smuzhiyun verbose, shuffle_interval, stutter, shutdown_secs,
777*4882a593Smuzhiyun onoff_interval, onoff_holdoff);
778*4882a593Smuzhiyun }
779*4882a593Smuzhiyun
lock_torture_cleanup(void)780*4882a593Smuzhiyun static void lock_torture_cleanup(void)
781*4882a593Smuzhiyun {
782*4882a593Smuzhiyun int i;
783*4882a593Smuzhiyun
784*4882a593Smuzhiyun if (torture_cleanup_begin())
785*4882a593Smuzhiyun return;
786*4882a593Smuzhiyun
787*4882a593Smuzhiyun /*
788*4882a593Smuzhiyun * Indicates early cleanup, meaning that the test has not run,
789*4882a593Smuzhiyun * such as when passing bogus args when loading the module. As
790*4882a593Smuzhiyun * such, only perform the underlying torture-specific cleanups,
791*4882a593Smuzhiyun * and avoid anything related to locktorture.
792*4882a593Smuzhiyun */
793*4882a593Smuzhiyun if (!cxt.lwsa && !cxt.lrsa)
794*4882a593Smuzhiyun goto end;
795*4882a593Smuzhiyun
796*4882a593Smuzhiyun if (writer_tasks) {
797*4882a593Smuzhiyun for (i = 0; i < cxt.nrealwriters_stress; i++)
798*4882a593Smuzhiyun torture_stop_kthread(lock_torture_writer,
799*4882a593Smuzhiyun writer_tasks[i]);
800*4882a593Smuzhiyun kfree(writer_tasks);
801*4882a593Smuzhiyun writer_tasks = NULL;
802*4882a593Smuzhiyun }
803*4882a593Smuzhiyun
804*4882a593Smuzhiyun if (reader_tasks) {
805*4882a593Smuzhiyun for (i = 0; i < cxt.nrealreaders_stress; i++)
806*4882a593Smuzhiyun torture_stop_kthread(lock_torture_reader,
807*4882a593Smuzhiyun reader_tasks[i]);
808*4882a593Smuzhiyun kfree(reader_tasks);
809*4882a593Smuzhiyun reader_tasks = NULL;
810*4882a593Smuzhiyun }
811*4882a593Smuzhiyun
812*4882a593Smuzhiyun torture_stop_kthread(lock_torture_stats, stats_task);
813*4882a593Smuzhiyun lock_torture_stats_print(); /* -After- the stats thread is stopped! */
814*4882a593Smuzhiyun
815*4882a593Smuzhiyun if (atomic_read(&cxt.n_lock_torture_errors))
816*4882a593Smuzhiyun lock_torture_print_module_parms(cxt.cur_ops,
817*4882a593Smuzhiyun "End of test: FAILURE");
818*4882a593Smuzhiyun else if (torture_onoff_failures())
819*4882a593Smuzhiyun lock_torture_print_module_parms(cxt.cur_ops,
820*4882a593Smuzhiyun "End of test: LOCK_HOTPLUG");
821*4882a593Smuzhiyun else
822*4882a593Smuzhiyun lock_torture_print_module_parms(cxt.cur_ops,
823*4882a593Smuzhiyun "End of test: SUCCESS");
824*4882a593Smuzhiyun
825*4882a593Smuzhiyun kfree(cxt.lwsa);
826*4882a593Smuzhiyun cxt.lwsa = NULL;
827*4882a593Smuzhiyun kfree(cxt.lrsa);
828*4882a593Smuzhiyun cxt.lrsa = NULL;
829*4882a593Smuzhiyun
830*4882a593Smuzhiyun end:
831*4882a593Smuzhiyun torture_cleanup_end();
832*4882a593Smuzhiyun }
833*4882a593Smuzhiyun
lock_torture_init(void)834*4882a593Smuzhiyun static int __init lock_torture_init(void)
835*4882a593Smuzhiyun {
836*4882a593Smuzhiyun int i, j;
837*4882a593Smuzhiyun int firsterr = 0;
838*4882a593Smuzhiyun static struct lock_torture_ops *torture_ops[] = {
839*4882a593Smuzhiyun &lock_busted_ops,
840*4882a593Smuzhiyun &spin_lock_ops, &spin_lock_irq_ops,
841*4882a593Smuzhiyun &rw_lock_ops, &rw_lock_irq_ops,
842*4882a593Smuzhiyun &mutex_lock_ops,
843*4882a593Smuzhiyun &ww_mutex_lock_ops,
844*4882a593Smuzhiyun #ifdef CONFIG_RT_MUTEXES
845*4882a593Smuzhiyun &rtmutex_lock_ops,
846*4882a593Smuzhiyun #endif
847*4882a593Smuzhiyun &rwsem_lock_ops,
848*4882a593Smuzhiyun &percpu_rwsem_lock_ops,
849*4882a593Smuzhiyun };
850*4882a593Smuzhiyun
851*4882a593Smuzhiyun if (!torture_init_begin(torture_type, verbose))
852*4882a593Smuzhiyun return -EBUSY;
853*4882a593Smuzhiyun
854*4882a593Smuzhiyun /* Process args and tell the world that the torturer is on the job. */
855*4882a593Smuzhiyun for (i = 0; i < ARRAY_SIZE(torture_ops); i++) {
856*4882a593Smuzhiyun cxt.cur_ops = torture_ops[i];
857*4882a593Smuzhiyun if (strcmp(torture_type, cxt.cur_ops->name) == 0)
858*4882a593Smuzhiyun break;
859*4882a593Smuzhiyun }
860*4882a593Smuzhiyun if (i == ARRAY_SIZE(torture_ops)) {
861*4882a593Smuzhiyun pr_alert("lock-torture: invalid torture type: \"%s\"\n",
862*4882a593Smuzhiyun torture_type);
863*4882a593Smuzhiyun pr_alert("lock-torture types:");
864*4882a593Smuzhiyun for (i = 0; i < ARRAY_SIZE(torture_ops); i++)
865*4882a593Smuzhiyun pr_alert(" %s", torture_ops[i]->name);
866*4882a593Smuzhiyun pr_alert("\n");
867*4882a593Smuzhiyun firsterr = -EINVAL;
868*4882a593Smuzhiyun goto unwind;
869*4882a593Smuzhiyun }
870*4882a593Smuzhiyun
871*4882a593Smuzhiyun if (nwriters_stress == 0 && nreaders_stress == 0) {
872*4882a593Smuzhiyun pr_alert("lock-torture: must run at least one locking thread\n");
873*4882a593Smuzhiyun firsterr = -EINVAL;
874*4882a593Smuzhiyun goto unwind;
875*4882a593Smuzhiyun }
876*4882a593Smuzhiyun
877*4882a593Smuzhiyun if (cxt.cur_ops->init)
878*4882a593Smuzhiyun cxt.cur_ops->init();
879*4882a593Smuzhiyun
880*4882a593Smuzhiyun if (nwriters_stress >= 0)
881*4882a593Smuzhiyun cxt.nrealwriters_stress = nwriters_stress;
882*4882a593Smuzhiyun else
883*4882a593Smuzhiyun cxt.nrealwriters_stress = 2 * num_online_cpus();
884*4882a593Smuzhiyun
885*4882a593Smuzhiyun #ifdef CONFIG_DEBUG_MUTEXES
886*4882a593Smuzhiyun if (str_has_prefix(torture_type, "mutex"))
887*4882a593Smuzhiyun cxt.debug_lock = true;
888*4882a593Smuzhiyun #endif
889*4882a593Smuzhiyun #ifdef CONFIG_DEBUG_RT_MUTEXES
890*4882a593Smuzhiyun if (str_has_prefix(torture_type, "rtmutex"))
891*4882a593Smuzhiyun cxt.debug_lock = true;
892*4882a593Smuzhiyun #endif
893*4882a593Smuzhiyun #ifdef CONFIG_DEBUG_SPINLOCK
894*4882a593Smuzhiyun if ((str_has_prefix(torture_type, "spin")) ||
895*4882a593Smuzhiyun (str_has_prefix(torture_type, "rw_lock")))
896*4882a593Smuzhiyun cxt.debug_lock = true;
897*4882a593Smuzhiyun #endif
898*4882a593Smuzhiyun
899*4882a593Smuzhiyun /* Initialize the statistics so that each run gets its own numbers. */
900*4882a593Smuzhiyun if (nwriters_stress) {
901*4882a593Smuzhiyun lock_is_write_held = false;
902*4882a593Smuzhiyun cxt.lwsa = kmalloc_array(cxt.nrealwriters_stress,
903*4882a593Smuzhiyun sizeof(*cxt.lwsa),
904*4882a593Smuzhiyun GFP_KERNEL);
905*4882a593Smuzhiyun if (cxt.lwsa == NULL) {
906*4882a593Smuzhiyun VERBOSE_TOROUT_STRING("cxt.lwsa: Out of memory");
907*4882a593Smuzhiyun firsterr = -ENOMEM;
908*4882a593Smuzhiyun goto unwind;
909*4882a593Smuzhiyun }
910*4882a593Smuzhiyun
911*4882a593Smuzhiyun for (i = 0; i < cxt.nrealwriters_stress; i++) {
912*4882a593Smuzhiyun cxt.lwsa[i].n_lock_fail = 0;
913*4882a593Smuzhiyun cxt.lwsa[i].n_lock_acquired = 0;
914*4882a593Smuzhiyun }
915*4882a593Smuzhiyun }
916*4882a593Smuzhiyun
917*4882a593Smuzhiyun if (cxt.cur_ops->readlock) {
918*4882a593Smuzhiyun if (nreaders_stress >= 0)
919*4882a593Smuzhiyun cxt.nrealreaders_stress = nreaders_stress;
920*4882a593Smuzhiyun else {
921*4882a593Smuzhiyun /*
922*4882a593Smuzhiyun * By default distribute evenly the number of
923*4882a593Smuzhiyun * readers and writers. We still run the same number
924*4882a593Smuzhiyun * of threads as the writer-only locks default.
925*4882a593Smuzhiyun */
926*4882a593Smuzhiyun if (nwriters_stress < 0) /* user doesn't care */
927*4882a593Smuzhiyun cxt.nrealwriters_stress = num_online_cpus();
928*4882a593Smuzhiyun cxt.nrealreaders_stress = cxt.nrealwriters_stress;
929*4882a593Smuzhiyun }
930*4882a593Smuzhiyun
931*4882a593Smuzhiyun if (nreaders_stress) {
932*4882a593Smuzhiyun lock_is_read_held = false;
933*4882a593Smuzhiyun cxt.lrsa = kmalloc_array(cxt.nrealreaders_stress,
934*4882a593Smuzhiyun sizeof(*cxt.lrsa),
935*4882a593Smuzhiyun GFP_KERNEL);
936*4882a593Smuzhiyun if (cxt.lrsa == NULL) {
937*4882a593Smuzhiyun VERBOSE_TOROUT_STRING("cxt.lrsa: Out of memory");
938*4882a593Smuzhiyun firsterr = -ENOMEM;
939*4882a593Smuzhiyun kfree(cxt.lwsa);
940*4882a593Smuzhiyun cxt.lwsa = NULL;
941*4882a593Smuzhiyun goto unwind;
942*4882a593Smuzhiyun }
943*4882a593Smuzhiyun
944*4882a593Smuzhiyun for (i = 0; i < cxt.nrealreaders_stress; i++) {
945*4882a593Smuzhiyun cxt.lrsa[i].n_lock_fail = 0;
946*4882a593Smuzhiyun cxt.lrsa[i].n_lock_acquired = 0;
947*4882a593Smuzhiyun }
948*4882a593Smuzhiyun }
949*4882a593Smuzhiyun }
950*4882a593Smuzhiyun
951*4882a593Smuzhiyun lock_torture_print_module_parms(cxt.cur_ops, "Start of test");
952*4882a593Smuzhiyun
953*4882a593Smuzhiyun /* Prepare torture context. */
954*4882a593Smuzhiyun if (onoff_interval > 0) {
955*4882a593Smuzhiyun firsterr = torture_onoff_init(onoff_holdoff * HZ,
956*4882a593Smuzhiyun onoff_interval * HZ, NULL);
957*4882a593Smuzhiyun if (firsterr)
958*4882a593Smuzhiyun goto unwind;
959*4882a593Smuzhiyun }
960*4882a593Smuzhiyun if (shuffle_interval > 0) {
961*4882a593Smuzhiyun firsterr = torture_shuffle_init(shuffle_interval);
962*4882a593Smuzhiyun if (firsterr)
963*4882a593Smuzhiyun goto unwind;
964*4882a593Smuzhiyun }
965*4882a593Smuzhiyun if (shutdown_secs > 0) {
966*4882a593Smuzhiyun firsterr = torture_shutdown_init(shutdown_secs,
967*4882a593Smuzhiyun lock_torture_cleanup);
968*4882a593Smuzhiyun if (firsterr)
969*4882a593Smuzhiyun goto unwind;
970*4882a593Smuzhiyun }
971*4882a593Smuzhiyun if (stutter > 0) {
972*4882a593Smuzhiyun firsterr = torture_stutter_init(stutter, stutter);
973*4882a593Smuzhiyun if (firsterr)
974*4882a593Smuzhiyun goto unwind;
975*4882a593Smuzhiyun }
976*4882a593Smuzhiyun
977*4882a593Smuzhiyun if (nwriters_stress) {
978*4882a593Smuzhiyun writer_tasks = kcalloc(cxt.nrealwriters_stress,
979*4882a593Smuzhiyun sizeof(writer_tasks[0]),
980*4882a593Smuzhiyun GFP_KERNEL);
981*4882a593Smuzhiyun if (writer_tasks == NULL) {
982*4882a593Smuzhiyun VERBOSE_TOROUT_ERRSTRING("writer_tasks: Out of memory");
983*4882a593Smuzhiyun firsterr = -ENOMEM;
984*4882a593Smuzhiyun goto unwind;
985*4882a593Smuzhiyun }
986*4882a593Smuzhiyun }
987*4882a593Smuzhiyun
988*4882a593Smuzhiyun if (cxt.cur_ops->readlock) {
989*4882a593Smuzhiyun reader_tasks = kcalloc(cxt.nrealreaders_stress,
990*4882a593Smuzhiyun sizeof(reader_tasks[0]),
991*4882a593Smuzhiyun GFP_KERNEL);
992*4882a593Smuzhiyun if (reader_tasks == NULL) {
993*4882a593Smuzhiyun VERBOSE_TOROUT_ERRSTRING("reader_tasks: Out of memory");
994*4882a593Smuzhiyun kfree(writer_tasks);
995*4882a593Smuzhiyun writer_tasks = NULL;
996*4882a593Smuzhiyun firsterr = -ENOMEM;
997*4882a593Smuzhiyun goto unwind;
998*4882a593Smuzhiyun }
999*4882a593Smuzhiyun }
1000*4882a593Smuzhiyun
1001*4882a593Smuzhiyun /*
1002*4882a593Smuzhiyun * Create the kthreads and start torturing (oh, those poor little locks).
1003*4882a593Smuzhiyun *
1004*4882a593Smuzhiyun * TODO: Note that we interleave writers with readers, giving writers a
1005*4882a593Smuzhiyun * slight advantage, by creating its kthread first. This can be modified
1006*4882a593Smuzhiyun * for very specific needs, or even let the user choose the policy, if
1007*4882a593Smuzhiyun * ever wanted.
1008*4882a593Smuzhiyun */
1009*4882a593Smuzhiyun for (i = 0, j = 0; i < cxt.nrealwriters_stress ||
1010*4882a593Smuzhiyun j < cxt.nrealreaders_stress; i++, j++) {
1011*4882a593Smuzhiyun if (i >= cxt.nrealwriters_stress)
1012*4882a593Smuzhiyun goto create_reader;
1013*4882a593Smuzhiyun
1014*4882a593Smuzhiyun /* Create writer. */
1015*4882a593Smuzhiyun firsterr = torture_create_kthread(lock_torture_writer, &cxt.lwsa[i],
1016*4882a593Smuzhiyun writer_tasks[i]);
1017*4882a593Smuzhiyun if (firsterr)
1018*4882a593Smuzhiyun goto unwind;
1019*4882a593Smuzhiyun
1020*4882a593Smuzhiyun create_reader:
1021*4882a593Smuzhiyun if (cxt.cur_ops->readlock == NULL || (j >= cxt.nrealreaders_stress))
1022*4882a593Smuzhiyun continue;
1023*4882a593Smuzhiyun /* Create reader. */
1024*4882a593Smuzhiyun firsterr = torture_create_kthread(lock_torture_reader, &cxt.lrsa[j],
1025*4882a593Smuzhiyun reader_tasks[j]);
1026*4882a593Smuzhiyun if (firsterr)
1027*4882a593Smuzhiyun goto unwind;
1028*4882a593Smuzhiyun }
1029*4882a593Smuzhiyun if (stat_interval > 0) {
1030*4882a593Smuzhiyun firsterr = torture_create_kthread(lock_torture_stats, NULL,
1031*4882a593Smuzhiyun stats_task);
1032*4882a593Smuzhiyun if (firsterr)
1033*4882a593Smuzhiyun goto unwind;
1034*4882a593Smuzhiyun }
1035*4882a593Smuzhiyun torture_init_end();
1036*4882a593Smuzhiyun return 0;
1037*4882a593Smuzhiyun
1038*4882a593Smuzhiyun unwind:
1039*4882a593Smuzhiyun torture_init_end();
1040*4882a593Smuzhiyun lock_torture_cleanup();
1041*4882a593Smuzhiyun return firsterr;
1042*4882a593Smuzhiyun }
1043*4882a593Smuzhiyun
1044*4882a593Smuzhiyun module_init(lock_torture_init);
1045*4882a593Smuzhiyun module_exit(lock_torture_cleanup);
1046