blob: 57bef4fbfb31cb65788caaa2e8ee654378aa3246 [file] [log] [blame]
Paul E. McKenney0af3fe12014-02-04 15:51:41 -08001/*
2 * Module-based torture test facility for locking
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, you can access it online at
16 * http://www.gnu.org/licenses/gpl-2.0.html.
17 *
18 * Copyright (C) IBM Corporation, 2014
19 *
Davidlohr Bueso095777c2015-07-22 14:07:27 -070020 * Authors: Paul E. McKenney <paulmck@us.ibm.com>
21 * Davidlohr Bueso <dave@stgolabs.net>
Paul E. McKenney0af3fe12014-02-04 15:51:41 -080022 * Based on kernel/rcu/torture.c.
23 */
Paul E. McKenney60500032018-05-15 12:25:05 -070024
25#define pr_fmt(fmt) fmt
26
Paul E. McKenney0af3fe12014-02-04 15:51:41 -080027#include <linux/kernel.h>
Paul E. McKenney0af3fe12014-02-04 15:51:41 -080028#include <linux/module.h>
29#include <linux/kthread.h>
Davidlohr Bueso095777c2015-07-22 14:07:27 -070030#include <linux/sched/rt.h>
Paul E. McKenney0af3fe12014-02-04 15:51:41 -080031#include <linux/spinlock.h>
Davidlohr Buesoe34191f2014-09-29 06:14:23 -070032#include <linux/rwlock.h>
Davidlohr Bueso42ddc752014-09-11 20:40:18 -070033#include <linux/mutex.h>
Davidlohr Buesoc98fed92014-09-29 06:14:26 -070034#include <linux/rwsem.h>
Paul E. McKenney0af3fe12014-02-04 15:51:41 -080035#include <linux/smp.h>
36#include <linux/interrupt.h>
37#include <linux/sched.h>
Ingo Molnarae7e81c2017-02-01 18:07:51 +010038#include <uapi/linux/sched/types.h>
Ingo Molnar037741a2017-02-03 10:08:30 +010039#include <linux/rtmutex.h>
Paul E. McKenney0af3fe12014-02-04 15:51:41 -080040#include <linux/atomic.h>
Paul E. McKenney0af3fe12014-02-04 15:51:41 -080041#include <linux/moduleparam.h>
Paul E. McKenney0af3fe12014-02-04 15:51:41 -080042#include <linux/delay.h>
Paul E. McKenney0af3fe12014-02-04 15:51:41 -080043#include <linux/slab.h>
Paul E. McKenney617783d2015-08-29 14:46:29 -070044#include <linux/percpu-rwsem.h>
Paul E. McKenney0af3fe12014-02-04 15:51:41 -080045#include <linux/torture.h>
46
47MODULE_LICENSE("GPL");
48MODULE_AUTHOR("Paul E. McKenney <paulmck@us.ibm.com>");
49
50torture_param(int, nwriters_stress, -1,
51 "Number of write-locking stress-test threads");
Davidlohr Bueso4f6332c2014-09-11 21:40:41 -070052torture_param(int, nreaders_stress, -1,
53 "Number of read-locking stress-test threads");
Paul E. McKenney0af3fe12014-02-04 15:51:41 -080054torture_param(int, onoff_holdoff, 0, "Time after boot before CPU hotplugs (s)");
55torture_param(int, onoff_interval, 0,
56 "Time between CPU hotplugs (s), 0=disable");
57torture_param(int, shuffle_interval, 3,
58 "Number of jiffies between shuffles, 0=disable");
59torture_param(int, shutdown_secs, 0, "Shutdown time (j), <= zero to disable.");
60torture_param(int, stat_interval, 60,
61 "Number of seconds between stats printk()s");
62torture_param(int, stutter, 5, "Number of jiffies to run/halt test, 0=disable");
Paul E. McKenney90127d62018-05-09 10:29:18 -070063torture_param(int, verbose, 1,
Paul E. McKenney0af3fe12014-02-04 15:51:41 -080064 "Enable verbose debugging printk()s");
65
66static char *torture_type = "spin_lock";
67module_param(torture_type, charp, 0444);
68MODULE_PARM_DESC(torture_type,
Davidlohr Bueso42ddc752014-09-11 20:40:18 -070069 "Type of lock to torture (spin_lock, spin_lock_irq, mutex_lock, ...)");
Paul E. McKenney0af3fe12014-02-04 15:51:41 -080070
Paul E. McKenney0af3fe12014-02-04 15:51:41 -080071static struct task_struct *stats_task;
72static struct task_struct **writer_tasks;
Davidlohr Bueso4f6332c2014-09-11 21:40:41 -070073static struct task_struct **reader_tasks;
Paul E. McKenney0af3fe12014-02-04 15:51:41 -080074
Paul E. McKenney0af3fe12014-02-04 15:51:41 -080075static bool lock_is_write_held;
Davidlohr Bueso4f6332c2014-09-11 21:40:41 -070076static bool lock_is_read_held;
Paul E. McKenney0af3fe12014-02-04 15:51:41 -080077
Davidlohr Bueso1e6757a2014-09-11 20:40:20 -070078struct lock_stress_stats {
79 long n_lock_fail;
80 long n_lock_acquired;
Paul E. McKenney0af3fe12014-02-04 15:51:41 -080081};
Paul E. McKenney0af3fe12014-02-04 15:51:41 -080082
Paul E. McKenney0af3fe12014-02-04 15:51:41 -080083/* Forward reference. */
84static void lock_torture_cleanup(void);
85
86/*
87 * Operations vector for selecting different types of tests.
88 */
89struct lock_torture_ops {
90 void (*init)(void);
91 int (*writelock)(void);
92 void (*write_delay)(struct torture_random_state *trsp);
Davidlohr Bueso095777c2015-07-22 14:07:27 -070093 void (*task_boost)(struct torture_random_state *trsp);
Paul E. McKenney0af3fe12014-02-04 15:51:41 -080094 void (*writeunlock)(void);
Davidlohr Bueso4f6332c2014-09-11 21:40:41 -070095 int (*readlock)(void);
96 void (*read_delay)(struct torture_random_state *trsp);
97 void (*readunlock)(void);
Davidlohr Bueso095777c2015-07-22 14:07:27 -070098
99 unsigned long flags; /* for irq spinlocks */
Paul E. McKenney0af3fe12014-02-04 15:51:41 -0800100 const char *name;
101};
102
Davidlohr Bueso630952c2014-09-11 21:42:25 -0700103struct lock_torture_cxt {
104 int nrealwriters_stress;
105 int nrealreaders_stress;
106 bool debug_lock;
107 atomic_t n_lock_torture_errors;
108 struct lock_torture_ops *cur_ops;
109 struct lock_stress_stats *lwsa; /* writer statistics */
110 struct lock_stress_stats *lrsa; /* reader statistics */
111};
112static struct lock_torture_cxt cxt = { 0, 0, false,
113 ATOMIC_INIT(0),
114 NULL, NULL};
Paul E. McKenney0af3fe12014-02-04 15:51:41 -0800115/*
116 * Definitions for lock torture testing.
117 */
118
Paul E. McKenneye0864812014-02-11 08:05:07 -0800119static int torture_lock_busted_write_lock(void)
120{
121 return 0; /* BUGGY, do not use in real life!!! */
122}
123
124static void torture_lock_busted_write_delay(struct torture_random_state *trsp)
125{
Paul E. McKenney61d49d22015-04-01 08:42:27 -0700126 const unsigned long longdelay_ms = 100;
Paul E. McKenneye0864812014-02-11 08:05:07 -0800127
128 /* We want a long delay occasionally to force massive contention. */
129 if (!(torture_random(trsp) %
Paul E. McKenney61d49d22015-04-01 08:42:27 -0700130 (cxt.nrealwriters_stress * 2000 * longdelay_ms)))
131 mdelay(longdelay_ms);
Davidlohr Bueso630952c2014-09-11 21:42:25 -0700132 if (!(torture_random(trsp) % (cxt.nrealwriters_stress * 20000)))
Paul E. McKenneycc1321c2017-10-16 11:05:03 -0700133 torture_preempt_schedule(); /* Allow test to be preempted. */
Paul E. McKenneye0864812014-02-11 08:05:07 -0800134}
135
136static void torture_lock_busted_write_unlock(void)
137{
138 /* BUGGY, do not use in real life!!! */
139}
140
Davidlohr Bueso095777c2015-07-22 14:07:27 -0700141static void torture_boost_dummy(struct torture_random_state *trsp)
142{
143 /* Only rtmutexes care about priority */
144}
145
Paul E. McKenneye0864812014-02-11 08:05:07 -0800146static struct lock_torture_ops lock_busted_ops = {
147 .writelock = torture_lock_busted_write_lock,
148 .write_delay = torture_lock_busted_write_delay,
Davidlohr Bueso095777c2015-07-22 14:07:27 -0700149 .task_boost = torture_boost_dummy,
Paul E. McKenneye0864812014-02-11 08:05:07 -0800150 .writeunlock = torture_lock_busted_write_unlock,
Davidlohr Bueso4f6332c2014-09-11 21:40:41 -0700151 .readlock = NULL,
152 .read_delay = NULL,
153 .readunlock = NULL,
Paul E. McKenneye0864812014-02-11 08:05:07 -0800154 .name = "lock_busted"
155};
156
Paul E. McKenney0af3fe12014-02-04 15:51:41 -0800157static DEFINE_SPINLOCK(torture_spinlock);
158
159static int torture_spin_lock_write_lock(void) __acquires(torture_spinlock)
160{
161 spin_lock(&torture_spinlock);
162 return 0;
163}
164
165static void torture_spin_lock_write_delay(struct torture_random_state *trsp)
166{
167 const unsigned long shortdelay_us = 2;
Paul E. McKenney61d49d22015-04-01 08:42:27 -0700168 const unsigned long longdelay_ms = 100;
Paul E. McKenney0af3fe12014-02-04 15:51:41 -0800169
170 /* We want a short delay mostly to emulate likely code, and
171 * we want a long delay occasionally to force massive contention.
172 */
173 if (!(torture_random(trsp) %
Paul E. McKenney61d49d22015-04-01 08:42:27 -0700174 (cxt.nrealwriters_stress * 2000 * longdelay_ms)))
175 mdelay(longdelay_ms);
Paul E. McKenney0af3fe12014-02-04 15:51:41 -0800176 if (!(torture_random(trsp) %
Davidlohr Bueso630952c2014-09-11 21:42:25 -0700177 (cxt.nrealwriters_stress * 2 * shortdelay_us)))
Paul E. McKenney0af3fe12014-02-04 15:51:41 -0800178 udelay(shortdelay_us);
Davidlohr Bueso630952c2014-09-11 21:42:25 -0700179 if (!(torture_random(trsp) % (cxt.nrealwriters_stress * 20000)))
Paul E. McKenneycc1321c2017-10-16 11:05:03 -0700180 torture_preempt_schedule(); /* Allow test to be preempted. */
Paul E. McKenney0af3fe12014-02-04 15:51:41 -0800181}
182
183static void torture_spin_lock_write_unlock(void) __releases(torture_spinlock)
184{
185 spin_unlock(&torture_spinlock);
186}
187
188static struct lock_torture_ops spin_lock_ops = {
189 .writelock = torture_spin_lock_write_lock,
190 .write_delay = torture_spin_lock_write_delay,
Davidlohr Bueso095777c2015-07-22 14:07:27 -0700191 .task_boost = torture_boost_dummy,
Paul E. McKenney0af3fe12014-02-04 15:51:41 -0800192 .writeunlock = torture_spin_lock_write_unlock,
Davidlohr Bueso4f6332c2014-09-11 21:40:41 -0700193 .readlock = NULL,
194 .read_delay = NULL,
195 .readunlock = NULL,
Paul E. McKenney0af3fe12014-02-04 15:51:41 -0800196 .name = "spin_lock"
197};
198
199static int torture_spin_lock_write_lock_irq(void)
Davidlohr Bueso219f8002014-09-29 06:14:24 -0700200__acquires(torture_spinlock)
Paul E. McKenney0af3fe12014-02-04 15:51:41 -0800201{
202 unsigned long flags;
203
204 spin_lock_irqsave(&torture_spinlock, flags);
Davidlohr Bueso630952c2014-09-11 21:42:25 -0700205 cxt.cur_ops->flags = flags;
Paul E. McKenney0af3fe12014-02-04 15:51:41 -0800206 return 0;
207}
208
209static void torture_lock_spin_write_unlock_irq(void)
210__releases(torture_spinlock)
211{
Davidlohr Bueso630952c2014-09-11 21:42:25 -0700212 spin_unlock_irqrestore(&torture_spinlock, cxt.cur_ops->flags);
Paul E. McKenney0af3fe12014-02-04 15:51:41 -0800213}
214
215static struct lock_torture_ops spin_lock_irq_ops = {
216 .writelock = torture_spin_lock_write_lock_irq,
217 .write_delay = torture_spin_lock_write_delay,
Davidlohr Bueso095777c2015-07-22 14:07:27 -0700218 .task_boost = torture_boost_dummy,
Paul E. McKenney0af3fe12014-02-04 15:51:41 -0800219 .writeunlock = torture_lock_spin_write_unlock_irq,
Davidlohr Bueso4f6332c2014-09-11 21:40:41 -0700220 .readlock = NULL,
221 .read_delay = NULL,
222 .readunlock = NULL,
Paul E. McKenney0af3fe12014-02-04 15:51:41 -0800223 .name = "spin_lock_irq"
224};
225
Davidlohr Buesoe34191f2014-09-29 06:14:23 -0700226static DEFINE_RWLOCK(torture_rwlock);
227
228static int torture_rwlock_write_lock(void) __acquires(torture_rwlock)
229{
230 write_lock(&torture_rwlock);
231 return 0;
232}
233
234static void torture_rwlock_write_delay(struct torture_random_state *trsp)
235{
236 const unsigned long shortdelay_us = 2;
237 const unsigned long longdelay_ms = 100;
238
239 /* We want a short delay mostly to emulate likely code, and
240 * we want a long delay occasionally to force massive contention.
241 */
242 if (!(torture_random(trsp) %
243 (cxt.nrealwriters_stress * 2000 * longdelay_ms)))
244 mdelay(longdelay_ms);
245 else
246 udelay(shortdelay_us);
247}
248
249static void torture_rwlock_write_unlock(void) __releases(torture_rwlock)
250{
251 write_unlock(&torture_rwlock);
252}
253
254static int torture_rwlock_read_lock(void) __acquires(torture_rwlock)
255{
256 read_lock(&torture_rwlock);
257 return 0;
258}
259
260static void torture_rwlock_read_delay(struct torture_random_state *trsp)
261{
262 const unsigned long shortdelay_us = 10;
263 const unsigned long longdelay_ms = 100;
264
265 /* We want a short delay mostly to emulate likely code, and
266 * we want a long delay occasionally to force massive contention.
267 */
268 if (!(torture_random(trsp) %
269 (cxt.nrealreaders_stress * 2000 * longdelay_ms)))
270 mdelay(longdelay_ms);
271 else
272 udelay(shortdelay_us);
273}
274
275static void torture_rwlock_read_unlock(void) __releases(torture_rwlock)
276{
277 read_unlock(&torture_rwlock);
278}
279
280static struct lock_torture_ops rw_lock_ops = {
281 .writelock = torture_rwlock_write_lock,
282 .write_delay = torture_rwlock_write_delay,
Davidlohr Bueso095777c2015-07-22 14:07:27 -0700283 .task_boost = torture_boost_dummy,
Davidlohr Buesoe34191f2014-09-29 06:14:23 -0700284 .writeunlock = torture_rwlock_write_unlock,
285 .readlock = torture_rwlock_read_lock,
286 .read_delay = torture_rwlock_read_delay,
287 .readunlock = torture_rwlock_read_unlock,
288 .name = "rw_lock"
289};
290
291static int torture_rwlock_write_lock_irq(void) __acquires(torture_rwlock)
292{
293 unsigned long flags;
294
295 write_lock_irqsave(&torture_rwlock, flags);
296 cxt.cur_ops->flags = flags;
297 return 0;
298}
299
300static void torture_rwlock_write_unlock_irq(void)
301__releases(torture_rwlock)
302{
303 write_unlock_irqrestore(&torture_rwlock, cxt.cur_ops->flags);
304}
305
306static int torture_rwlock_read_lock_irq(void) __acquires(torture_rwlock)
307{
308 unsigned long flags;
309
310 read_lock_irqsave(&torture_rwlock, flags);
311 cxt.cur_ops->flags = flags;
312 return 0;
313}
314
315static void torture_rwlock_read_unlock_irq(void)
316__releases(torture_rwlock)
317{
Alexey Kodanevf548d992015-03-07 03:06:53 +0300318 read_unlock_irqrestore(&torture_rwlock, cxt.cur_ops->flags);
Davidlohr Buesoe34191f2014-09-29 06:14:23 -0700319}
320
321static struct lock_torture_ops rw_lock_irq_ops = {
322 .writelock = torture_rwlock_write_lock_irq,
323 .write_delay = torture_rwlock_write_delay,
Davidlohr Bueso095777c2015-07-22 14:07:27 -0700324 .task_boost = torture_boost_dummy,
Davidlohr Buesoe34191f2014-09-29 06:14:23 -0700325 .writeunlock = torture_rwlock_write_unlock_irq,
326 .readlock = torture_rwlock_read_lock_irq,
327 .read_delay = torture_rwlock_read_delay,
328 .readunlock = torture_rwlock_read_unlock_irq,
329 .name = "rw_lock_irq"
330};
331
Davidlohr Bueso42ddc752014-09-11 20:40:18 -0700332static DEFINE_MUTEX(torture_mutex);
333
334static int torture_mutex_lock(void) __acquires(torture_mutex)
335{
336 mutex_lock(&torture_mutex);
337 return 0;
338}
339
340static void torture_mutex_delay(struct torture_random_state *trsp)
341{
342 const unsigned long longdelay_ms = 100;
343
344 /* We want a long delay occasionally to force massive contention. */
345 if (!(torture_random(trsp) %
Davidlohr Bueso630952c2014-09-11 21:42:25 -0700346 (cxt.nrealwriters_stress * 2000 * longdelay_ms)))
Davidlohr Bueso42ddc752014-09-11 20:40:18 -0700347 mdelay(longdelay_ms * 5);
348 else
349 mdelay(longdelay_ms / 5);
Davidlohr Bueso630952c2014-09-11 21:42:25 -0700350 if (!(torture_random(trsp) % (cxt.nrealwriters_stress * 20000)))
Paul E. McKenneycc1321c2017-10-16 11:05:03 -0700351 torture_preempt_schedule(); /* Allow test to be preempted. */
Davidlohr Bueso42ddc752014-09-11 20:40:18 -0700352}
353
354static void torture_mutex_unlock(void) __releases(torture_mutex)
355{
356 mutex_unlock(&torture_mutex);
357}
358
359static struct lock_torture_ops mutex_lock_ops = {
360 .writelock = torture_mutex_lock,
361 .write_delay = torture_mutex_delay,
Davidlohr Bueso095777c2015-07-22 14:07:27 -0700362 .task_boost = torture_boost_dummy,
Davidlohr Bueso42ddc752014-09-11 20:40:18 -0700363 .writeunlock = torture_mutex_unlock,
Davidlohr Bueso4f6332c2014-09-11 21:40:41 -0700364 .readlock = NULL,
365 .read_delay = NULL,
366 .readunlock = NULL,
Davidlohr Bueso42ddc752014-09-11 20:40:18 -0700367 .name = "mutex_lock"
368};
369
Chris Wilson0186a6c2016-12-01 11:47:05 +0000370#include <linux/ww_mutex.h>
371static DEFINE_WW_CLASS(torture_ww_class);
372static DEFINE_WW_MUTEX(torture_ww_mutex_0, &torture_ww_class);
373static DEFINE_WW_MUTEX(torture_ww_mutex_1, &torture_ww_class);
374static DEFINE_WW_MUTEX(torture_ww_mutex_2, &torture_ww_class);
375
376static int torture_ww_mutex_lock(void)
377__acquires(torture_ww_mutex_0)
378__acquires(torture_ww_mutex_1)
379__acquires(torture_ww_mutex_2)
380{
381 LIST_HEAD(list);
382 struct reorder_lock {
383 struct list_head link;
384 struct ww_mutex *lock;
385 } locks[3], *ll, *ln;
386 struct ww_acquire_ctx ctx;
387
388 locks[0].lock = &torture_ww_mutex_0;
389 list_add(&locks[0].link, &list);
390
391 locks[1].lock = &torture_ww_mutex_1;
392 list_add(&locks[1].link, &list);
393
394 locks[2].lock = &torture_ww_mutex_2;
395 list_add(&locks[2].link, &list);
396
397 ww_acquire_init(&ctx, &torture_ww_class);
398
399 list_for_each_entry(ll, &list, link) {
400 int err;
401
402 err = ww_mutex_lock(ll->lock, &ctx);
403 if (!err)
404 continue;
405
406 ln = ll;
407 list_for_each_entry_continue_reverse(ln, &list, link)
408 ww_mutex_unlock(ln->lock);
409
410 if (err != -EDEADLK)
411 return err;
412
413 ww_mutex_lock_slow(ll->lock, &ctx);
414 list_move(&ll->link, &list);
415 }
416
417 ww_acquire_fini(&ctx);
418 return 0;
419}
420
421static void torture_ww_mutex_unlock(void)
422__releases(torture_ww_mutex_0)
423__releases(torture_ww_mutex_1)
424__releases(torture_ww_mutex_2)
425{
426 ww_mutex_unlock(&torture_ww_mutex_0);
427 ww_mutex_unlock(&torture_ww_mutex_1);
428 ww_mutex_unlock(&torture_ww_mutex_2);
429}
430
431static struct lock_torture_ops ww_mutex_lock_ops = {
432 .writelock = torture_ww_mutex_lock,
433 .write_delay = torture_mutex_delay,
434 .task_boost = torture_boost_dummy,
435 .writeunlock = torture_ww_mutex_unlock,
436 .readlock = NULL,
437 .read_delay = NULL,
438 .readunlock = NULL,
439 .name = "ww_mutex_lock"
440};
441
Davidlohr Bueso095777c2015-07-22 14:07:27 -0700442#ifdef CONFIG_RT_MUTEXES
443static DEFINE_RT_MUTEX(torture_rtmutex);
444
445static int torture_rtmutex_lock(void) __acquires(torture_rtmutex)
446{
447 rt_mutex_lock(&torture_rtmutex);
448 return 0;
449}
450
451static void torture_rtmutex_boost(struct torture_random_state *trsp)
452{
453 int policy;
454 struct sched_param param;
455 const unsigned int factor = 50000; /* yes, quite arbitrary */
456
457 if (!rt_task(current)) {
458 /*
Davidlohr Bueso1f190932016-04-12 08:47:17 -0700459 * Boost priority once every ~50k operations. When the
Davidlohr Bueso095777c2015-07-22 14:07:27 -0700460 * task tries to take the lock, the rtmutex it will account
461 * for the new priority, and do any corresponding pi-dance.
462 */
Davidlohr Bueso1f190932016-04-12 08:47:17 -0700463 if (trsp && !(torture_random(trsp) %
464 (cxt.nrealwriters_stress * factor))) {
Davidlohr Bueso095777c2015-07-22 14:07:27 -0700465 policy = SCHED_FIFO;
466 param.sched_priority = MAX_RT_PRIO - 1;
467 } else /* common case, do nothing */
468 return;
469 } else {
470 /*
471 * The task will remain boosted for another ~500k operations,
472 * then restored back to its original prio, and so forth.
473 *
474 * When @trsp is nil, we want to force-reset the task for
475 * stopping the kthread.
476 */
477 if (!trsp || !(torture_random(trsp) %
478 (cxt.nrealwriters_stress * factor * 2))) {
479 policy = SCHED_NORMAL;
480 param.sched_priority = 0;
481 } else /* common case, do nothing */
482 return;
483 }
484
485 sched_setscheduler_nocheck(current, policy, &param);
486}
487
488static void torture_rtmutex_delay(struct torture_random_state *trsp)
489{
490 const unsigned long shortdelay_us = 2;
491 const unsigned long longdelay_ms = 100;
492
493 /*
494 * We want a short delay mostly to emulate likely code, and
495 * we want a long delay occasionally to force massive contention.
496 */
497 if (!(torture_random(trsp) %
498 (cxt.nrealwriters_stress * 2000 * longdelay_ms)))
499 mdelay(longdelay_ms);
500 if (!(torture_random(trsp) %
501 (cxt.nrealwriters_stress * 2 * shortdelay_us)))
502 udelay(shortdelay_us);
Davidlohr Bueso095777c2015-07-22 14:07:27 -0700503 if (!(torture_random(trsp) % (cxt.nrealwriters_stress * 20000)))
Paul E. McKenneycc1321c2017-10-16 11:05:03 -0700504 torture_preempt_schedule(); /* Allow test to be preempted. */
Davidlohr Bueso095777c2015-07-22 14:07:27 -0700505}
506
507static void torture_rtmutex_unlock(void) __releases(torture_rtmutex)
508{
509 rt_mutex_unlock(&torture_rtmutex);
510}
511
512static struct lock_torture_ops rtmutex_lock_ops = {
513 .writelock = torture_rtmutex_lock,
514 .write_delay = torture_rtmutex_delay,
515 .task_boost = torture_rtmutex_boost,
516 .writeunlock = torture_rtmutex_unlock,
517 .readlock = NULL,
518 .read_delay = NULL,
519 .readunlock = NULL,
520 .name = "rtmutex_lock"
521};
522#endif
523
Davidlohr Bueso4a3b4272014-09-11 21:41:30 -0700524static DECLARE_RWSEM(torture_rwsem);
525static int torture_rwsem_down_write(void) __acquires(torture_rwsem)
526{
527 down_write(&torture_rwsem);
528 return 0;
529}
530
531static void torture_rwsem_write_delay(struct torture_random_state *trsp)
532{
533 const unsigned long longdelay_ms = 100;
534
535 /* We want a long delay occasionally to force massive contention. */
536 if (!(torture_random(trsp) %
Davidlohr Bueso630952c2014-09-11 21:42:25 -0700537 (cxt.nrealwriters_stress * 2000 * longdelay_ms)))
Davidlohr Bueso4a3b4272014-09-11 21:41:30 -0700538 mdelay(longdelay_ms * 10);
539 else
540 mdelay(longdelay_ms / 10);
Davidlohr Bueso630952c2014-09-11 21:42:25 -0700541 if (!(torture_random(trsp) % (cxt.nrealwriters_stress * 20000)))
Paul E. McKenneycc1321c2017-10-16 11:05:03 -0700542 torture_preempt_schedule(); /* Allow test to be preempted. */
Davidlohr Bueso4a3b4272014-09-11 21:41:30 -0700543}
544
545static void torture_rwsem_up_write(void) __releases(torture_rwsem)
546{
547 up_write(&torture_rwsem);
548}
549
550static int torture_rwsem_down_read(void) __acquires(torture_rwsem)
551{
552 down_read(&torture_rwsem);
553 return 0;
554}
555
556static void torture_rwsem_read_delay(struct torture_random_state *trsp)
557{
558 const unsigned long longdelay_ms = 100;
559
560 /* We want a long delay occasionally to force massive contention. */
561 if (!(torture_random(trsp) %
Davidlohr Buesof2f76262017-05-15 02:07:22 -0700562 (cxt.nrealreaders_stress * 2000 * longdelay_ms)))
Davidlohr Bueso4a3b4272014-09-11 21:41:30 -0700563 mdelay(longdelay_ms * 2);
564 else
565 mdelay(longdelay_ms / 2);
Davidlohr Bueso630952c2014-09-11 21:42:25 -0700566 if (!(torture_random(trsp) % (cxt.nrealreaders_stress * 20000)))
Paul E. McKenneycc1321c2017-10-16 11:05:03 -0700567 torture_preempt_schedule(); /* Allow test to be preempted. */
Davidlohr Bueso4a3b4272014-09-11 21:41:30 -0700568}
569
570static void torture_rwsem_up_read(void) __releases(torture_rwsem)
571{
572 up_read(&torture_rwsem);
573}
574
575static struct lock_torture_ops rwsem_lock_ops = {
576 .writelock = torture_rwsem_down_write,
577 .write_delay = torture_rwsem_write_delay,
Davidlohr Bueso095777c2015-07-22 14:07:27 -0700578 .task_boost = torture_boost_dummy,
Davidlohr Bueso4a3b4272014-09-11 21:41:30 -0700579 .writeunlock = torture_rwsem_up_write,
580 .readlock = torture_rwsem_down_read,
581 .read_delay = torture_rwsem_read_delay,
582 .readunlock = torture_rwsem_up_read,
583 .name = "rwsem_lock"
584};
585
Paul E. McKenney617783d2015-08-29 14:46:29 -0700586#include <linux/percpu-rwsem.h>
587static struct percpu_rw_semaphore pcpu_rwsem;
588
589void torture_percpu_rwsem_init(void)
590{
591 BUG_ON(percpu_init_rwsem(&pcpu_rwsem));
592}
593
594static int torture_percpu_rwsem_down_write(void) __acquires(pcpu_rwsem)
595{
596 percpu_down_write(&pcpu_rwsem);
597 return 0;
598}
599
600static void torture_percpu_rwsem_up_write(void) __releases(pcpu_rwsem)
601{
602 percpu_up_write(&pcpu_rwsem);
603}
604
605static int torture_percpu_rwsem_down_read(void) __acquires(pcpu_rwsem)
606{
607 percpu_down_read(&pcpu_rwsem);
608 return 0;
609}
610
611static void torture_percpu_rwsem_up_read(void) __releases(pcpu_rwsem)
612{
613 percpu_up_read(&pcpu_rwsem);
614}
615
616static struct lock_torture_ops percpu_rwsem_lock_ops = {
617 .init = torture_percpu_rwsem_init,
618 .writelock = torture_percpu_rwsem_down_write,
619 .write_delay = torture_rwsem_write_delay,
620 .task_boost = torture_boost_dummy,
621 .writeunlock = torture_percpu_rwsem_up_write,
622 .readlock = torture_percpu_rwsem_down_read,
623 .read_delay = torture_rwsem_read_delay,
624 .readunlock = torture_percpu_rwsem_up_read,
625 .name = "percpu_rwsem_lock"
626};
627
Paul E. McKenney0af3fe12014-02-04 15:51:41 -0800628/*
629 * Lock torture writer kthread. Repeatedly acquires and releases
630 * the lock, checking for duplicate acquisitions.
631 */
632static int lock_torture_writer(void *arg)
633{
Davidlohr Bueso1e6757a2014-09-11 20:40:20 -0700634 struct lock_stress_stats *lwsp = arg;
Paul E. McKenney0af3fe12014-02-04 15:51:41 -0800635 static DEFINE_TORTURE_RANDOM(rand);
636
637 VERBOSE_TOROUT_STRING("lock_torture_writer task started");
Dongsheng Yang8698a742014-03-11 18:09:12 +0800638 set_user_nice(current, MAX_NICE);
Paul E. McKenney0af3fe12014-02-04 15:51:41 -0800639
640 do {
Paul E. McKenneyda601c62014-02-26 12:14:51 -0800641 if ((torture_random(&rand) & 0xfffff) == 0)
642 schedule_timeout_uninterruptible(1);
Davidlohr Buesoa1229492014-09-29 06:14:25 -0700643
Davidlohr Bueso095777c2015-07-22 14:07:27 -0700644 cxt.cur_ops->task_boost(&rand);
Davidlohr Bueso630952c2014-09-11 21:42:25 -0700645 cxt.cur_ops->writelock();
Paul E. McKenney0af3fe12014-02-04 15:51:41 -0800646 if (WARN_ON_ONCE(lock_is_write_held))
Davidlohr Bueso1e6757a2014-09-11 20:40:20 -0700647 lwsp->n_lock_fail++;
Paul E. McKenney0af3fe12014-02-04 15:51:41 -0800648 lock_is_write_held = 1;
Davidlohr Buesoa1229492014-09-29 06:14:25 -0700649 if (WARN_ON_ONCE(lock_is_read_held))
650 lwsp->n_lock_fail++; /* rare, but... */
651
Davidlohr Bueso1e6757a2014-09-11 20:40:20 -0700652 lwsp->n_lock_acquired++;
Davidlohr Bueso630952c2014-09-11 21:42:25 -0700653 cxt.cur_ops->write_delay(&rand);
Paul E. McKenney0af3fe12014-02-04 15:51:41 -0800654 lock_is_write_held = 0;
Davidlohr Bueso630952c2014-09-11 21:42:25 -0700655 cxt.cur_ops->writeunlock();
Davidlohr Buesoa1229492014-09-29 06:14:25 -0700656
Paul E. McKenney0af3fe12014-02-04 15:51:41 -0800657 stutter_wait("lock_torture_writer");
658 } while (!torture_must_stop());
Davidlohr Bueso095777c2015-07-22 14:07:27 -0700659
660 cxt.cur_ops->task_boost(NULL); /* reset prio */
Paul E. McKenney0af3fe12014-02-04 15:51:41 -0800661 torture_kthread_stopping("lock_torture_writer");
662 return 0;
663}
664
665/*
Davidlohr Bueso4f6332c2014-09-11 21:40:41 -0700666 * Lock torture reader kthread. Repeatedly acquires and releases
667 * the reader lock.
668 */
669static int lock_torture_reader(void *arg)
670{
671 struct lock_stress_stats *lrsp = arg;
672 static DEFINE_TORTURE_RANDOM(rand);
673
674 VERBOSE_TOROUT_STRING("lock_torture_reader task started");
675 set_user_nice(current, MAX_NICE);
676
677 do {
678 if ((torture_random(&rand) & 0xfffff) == 0)
679 schedule_timeout_uninterruptible(1);
Davidlohr Buesoa1229492014-09-29 06:14:25 -0700680
Davidlohr Bueso630952c2014-09-11 21:42:25 -0700681 cxt.cur_ops->readlock();
Davidlohr Bueso4f6332c2014-09-11 21:40:41 -0700682 lock_is_read_held = 1;
Davidlohr Buesoa1229492014-09-29 06:14:25 -0700683 if (WARN_ON_ONCE(lock_is_write_held))
684 lrsp->n_lock_fail++; /* rare, but... */
685
Davidlohr Bueso4f6332c2014-09-11 21:40:41 -0700686 lrsp->n_lock_acquired++;
Davidlohr Bueso630952c2014-09-11 21:42:25 -0700687 cxt.cur_ops->read_delay(&rand);
Davidlohr Bueso4f6332c2014-09-11 21:40:41 -0700688 lock_is_read_held = 0;
Davidlohr Bueso630952c2014-09-11 21:42:25 -0700689 cxt.cur_ops->readunlock();
Davidlohr Buesoa1229492014-09-29 06:14:25 -0700690
Davidlohr Bueso4f6332c2014-09-11 21:40:41 -0700691 stutter_wait("lock_torture_reader");
692 } while (!torture_must_stop());
693 torture_kthread_stopping("lock_torture_reader");
694 return 0;
695}
696
697/*
Paul E. McKenney0af3fe12014-02-04 15:51:41 -0800698 * Create an lock-torture-statistics message in the specified buffer.
699 */
Davidlohr Bueso4f6332c2014-09-11 21:40:41 -0700700static void __torture_print_stats(char *page,
701 struct lock_stress_stats *statp, bool write)
Paul E. McKenney0af3fe12014-02-04 15:51:41 -0800702{
703 bool fail = 0;
Davidlohr Bueso4f6332c2014-09-11 21:40:41 -0700704 int i, n_stress;
Davidlohr Bueso2ce77d12017-05-15 02:07:23 -0700705 long max = 0, min = statp ? statp[0].n_lock_acquired : 0;
Paul E. McKenney0af3fe12014-02-04 15:51:41 -0800706 long long sum = 0;
707
Davidlohr Bueso630952c2014-09-11 21:42:25 -0700708 n_stress = write ? cxt.nrealwriters_stress : cxt.nrealreaders_stress;
Davidlohr Bueso4f6332c2014-09-11 21:40:41 -0700709 for (i = 0; i < n_stress; i++) {
710 if (statp[i].n_lock_fail)
Paul E. McKenney0af3fe12014-02-04 15:51:41 -0800711 fail = true;
Davidlohr Bueso4f6332c2014-09-11 21:40:41 -0700712 sum += statp[i].n_lock_acquired;
713 if (max < statp[i].n_lock_fail)
714 max = statp[i].n_lock_fail;
715 if (min > statp[i].n_lock_fail)
716 min = statp[i].n_lock_fail;
Paul E. McKenney0af3fe12014-02-04 15:51:41 -0800717 }
Paul E. McKenney0af3fe12014-02-04 15:51:41 -0800718 page += sprintf(page,
Davidlohr Bueso4f6332c2014-09-11 21:40:41 -0700719 "%s: Total: %lld Max/Min: %ld/%ld %s Fail: %d %s\n",
720 write ? "Writes" : "Reads ",
Paul E. McKenney0af3fe12014-02-04 15:51:41 -0800721 sum, max, min, max / 2 > min ? "???" : "",
722 fail, fail ? "!!!" : "");
723 if (fail)
Davidlohr Bueso630952c2014-09-11 21:42:25 -0700724 atomic_inc(&cxt.n_lock_torture_errors);
Paul E. McKenney0af3fe12014-02-04 15:51:41 -0800725}
726
727/*
728 * Print torture statistics. Caller must ensure that there is only one
729 * call to this function at a given time!!! This is normally accomplished
730 * by relying on the module system to only have one copy of the module
731 * loaded, and then by giving the lock_torture_stats kthread full control
732 * (or the init/cleanup functions when lock_torture_stats thread is not
733 * running).
734 */
735static void lock_torture_stats_print(void)
736{
Davidlohr Bueso630952c2014-09-11 21:42:25 -0700737 int size = cxt.nrealwriters_stress * 200 + 8192;
Paul E. McKenney0af3fe12014-02-04 15:51:41 -0800738 char *buf;
739
Davidlohr Bueso630952c2014-09-11 21:42:25 -0700740 if (cxt.cur_ops->readlock)
741 size += cxt.nrealreaders_stress * 200 + 8192;
Davidlohr Bueso4f6332c2014-09-11 21:40:41 -0700742
Paul E. McKenney0af3fe12014-02-04 15:51:41 -0800743 buf = kmalloc(size, GFP_KERNEL);
744 if (!buf) {
745 pr_err("lock_torture_stats_print: Out of memory, need: %d",
746 size);
747 return;
748 }
Davidlohr Bueso4f6332c2014-09-11 21:40:41 -0700749
Davidlohr Bueso630952c2014-09-11 21:42:25 -0700750 __torture_print_stats(buf, cxt.lwsa, true);
Paul E. McKenney0af3fe12014-02-04 15:51:41 -0800751 pr_alert("%s", buf);
752 kfree(buf);
Davidlohr Bueso4f6332c2014-09-11 21:40:41 -0700753
Davidlohr Bueso630952c2014-09-11 21:42:25 -0700754 if (cxt.cur_ops->readlock) {
Davidlohr Bueso4f6332c2014-09-11 21:40:41 -0700755 buf = kmalloc(size, GFP_KERNEL);
756 if (!buf) {
757 pr_err("lock_torture_stats_print: Out of memory, need: %d",
758 size);
759 return;
760 }
761
Davidlohr Bueso630952c2014-09-11 21:42:25 -0700762 __torture_print_stats(buf, cxt.lrsa, false);
Davidlohr Bueso4f6332c2014-09-11 21:40:41 -0700763 pr_alert("%s", buf);
764 kfree(buf);
765 }
Paul E. McKenney0af3fe12014-02-04 15:51:41 -0800766}
767
768/*
769 * Periodically prints torture statistics, if periodic statistics printing
770 * was specified via the stat_interval module parameter.
771 *
772 * No need to worry about fullstop here, since this one doesn't reference
773 * volatile state or register callbacks.
774 */
775static int lock_torture_stats(void *arg)
776{
777 VERBOSE_TOROUT_STRING("lock_torture_stats task started");
778 do {
779 schedule_timeout_interruptible(stat_interval * HZ);
780 lock_torture_stats_print();
781 torture_shutdown_absorb("lock_torture_stats");
782 } while (!torture_must_stop());
783 torture_kthread_stopping("lock_torture_stats");
784 return 0;
785}
786
787static inline void
788lock_torture_print_module_parms(struct lock_torture_ops *cur_ops,
789 const char *tag)
790{
791 pr_alert("%s" TORTURE_FLAG
Davidlohr Bueso4f6332c2014-09-11 21:40:41 -0700792 "--- %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",
Davidlohr Bueso630952c2014-09-11 21:42:25 -0700793 torture_type, tag, cxt.debug_lock ? " [debug]": "",
794 cxt.nrealwriters_stress, cxt.nrealreaders_stress, stat_interval,
Davidlohr Bueso4f6332c2014-09-11 21:40:41 -0700795 verbose, shuffle_interval, stutter, shutdown_secs,
Paul E. McKenney0af3fe12014-02-04 15:51:41 -0800796 onoff_interval, onoff_holdoff);
797}
798
799static void lock_torture_cleanup(void)
800{
801 int i;
802
Davidlohr Buesod36a7a02014-09-11 20:40:21 -0700803 if (torture_cleanup_begin())
Paul E. McKenney0af3fe12014-02-04 15:51:41 -0800804 return;
805
Davidlohr Buesoc1c33b92016-04-12 08:47:18 -0700806 /*
807 * Indicates early cleanup, meaning that the test has not run,
808 * such as when passing bogus args when loading the module. As
809 * such, only perform the underlying torture-specific cleanups,
810 * and avoid anything related to locktorture.
811 */
Davidlohr Bueso2ce77d12017-05-15 02:07:23 -0700812 if (!cxt.lwsa && !cxt.lrsa)
Davidlohr Buesoc1c33b92016-04-12 08:47:18 -0700813 goto end;
814
Paul E. McKenney0af3fe12014-02-04 15:51:41 -0800815 if (writer_tasks) {
Davidlohr Bueso630952c2014-09-11 21:42:25 -0700816 for (i = 0; i < cxt.nrealwriters_stress; i++)
Paul E. McKenney0af3fe12014-02-04 15:51:41 -0800817 torture_stop_kthread(lock_torture_writer,
818 writer_tasks[i]);
819 kfree(writer_tasks);
820 writer_tasks = NULL;
821 }
822
Davidlohr Bueso4f6332c2014-09-11 21:40:41 -0700823 if (reader_tasks) {
Davidlohr Bueso630952c2014-09-11 21:42:25 -0700824 for (i = 0; i < cxt.nrealreaders_stress; i++)
Davidlohr Bueso4f6332c2014-09-11 21:40:41 -0700825 torture_stop_kthread(lock_torture_reader,
826 reader_tasks[i]);
827 kfree(reader_tasks);
828 reader_tasks = NULL;
829 }
830
Paul E. McKenney0af3fe12014-02-04 15:51:41 -0800831 torture_stop_kthread(lock_torture_stats, stats_task);
832 lock_torture_stats_print(); /* -After- the stats thread is stopped! */
833
Davidlohr Bueso630952c2014-09-11 21:42:25 -0700834 if (atomic_read(&cxt.n_lock_torture_errors))
835 lock_torture_print_module_parms(cxt.cur_ops,
Paul E. McKenney0af3fe12014-02-04 15:51:41 -0800836 "End of test: FAILURE");
837 else if (torture_onoff_failures())
Davidlohr Bueso630952c2014-09-11 21:42:25 -0700838 lock_torture_print_module_parms(cxt.cur_ops,
Paul E. McKenney0af3fe12014-02-04 15:51:41 -0800839 "End of test: LOCK_HOTPLUG");
840 else
Davidlohr Bueso630952c2014-09-11 21:42:25 -0700841 lock_torture_print_module_parms(cxt.cur_ops,
Paul E. McKenney0af3fe12014-02-04 15:51:41 -0800842 "End of test: SUCCESS");
Yang Shif4dbba52016-11-10 13:06:39 -0800843
844 kfree(cxt.lwsa);
845 kfree(cxt.lrsa);
846
Davidlohr Buesoc1c33b92016-04-12 08:47:18 -0700847end:
Davidlohr Buesod36a7a02014-09-11 20:40:21 -0700848 torture_cleanup_end();
Paul E. McKenney0af3fe12014-02-04 15:51:41 -0800849}
850
851static int __init lock_torture_init(void)
852{
Davidlohr Bueso4f6332c2014-09-11 21:40:41 -0700853 int i, j;
Paul E. McKenney0af3fe12014-02-04 15:51:41 -0800854 int firsterr = 0;
855 static struct lock_torture_ops *torture_ops[] = {
Davidlohr Buesoe34191f2014-09-29 06:14:23 -0700856 &lock_busted_ops,
857 &spin_lock_ops, &spin_lock_irq_ops,
858 &rw_lock_ops, &rw_lock_irq_ops,
859 &mutex_lock_ops,
Chris Wilson0186a6c2016-12-01 11:47:05 +0000860 &ww_mutex_lock_ops,
Davidlohr Bueso095777c2015-07-22 14:07:27 -0700861#ifdef CONFIG_RT_MUTEXES
862 &rtmutex_lock_ops,
863#endif
Davidlohr Buesoe34191f2014-09-29 06:14:23 -0700864 &rwsem_lock_ops,
Paul E. McKenney617783d2015-08-29 14:46:29 -0700865 &percpu_rwsem_lock_ops,
Paul E. McKenney0af3fe12014-02-04 15:51:41 -0800866 };
867
Paul E. McKenneya2f25772017-11-21 20:19:17 -0800868 if (!torture_init_begin(torture_type, verbose))
Paul E. McKenney52280842014-04-07 09:14:11 -0700869 return -EBUSY;
Paul E. McKenney0af3fe12014-02-04 15:51:41 -0800870
871 /* Process args and tell the world that the torturer is on the job. */
872 for (i = 0; i < ARRAY_SIZE(torture_ops); i++) {
Davidlohr Bueso630952c2014-09-11 21:42:25 -0700873 cxt.cur_ops = torture_ops[i];
874 if (strcmp(torture_type, cxt.cur_ops->name) == 0)
Paul E. McKenney0af3fe12014-02-04 15:51:41 -0800875 break;
876 }
877 if (i == ARRAY_SIZE(torture_ops)) {
878 pr_alert("lock-torture: invalid torture type: \"%s\"\n",
879 torture_type);
880 pr_alert("lock-torture types:");
881 for (i = 0; i < ARRAY_SIZE(torture_ops); i++)
882 pr_alert(" %s", torture_ops[i]->name);
883 pr_alert("\n");
Paul E. McKenneya36a9962015-08-30 20:01:48 -0700884 firsterr = -EINVAL;
885 goto unwind;
Paul E. McKenney0af3fe12014-02-04 15:51:41 -0800886 }
Davidlohr Bueso2ce77d12017-05-15 02:07:23 -0700887
888 if (nwriters_stress == 0 && nreaders_stress == 0) {
889 pr_alert("lock-torture: must run at least one locking thread\n");
890 firsterr = -EINVAL;
891 goto unwind;
892 }
893
Davidlohr Bueso630952c2014-09-11 21:42:25 -0700894 if (cxt.cur_ops->init)
Paul E. McKenneya36a9962015-08-30 20:01:48 -0700895 cxt.cur_ops->init();
Paul E. McKenney0af3fe12014-02-04 15:51:41 -0800896
897 if (nwriters_stress >= 0)
Davidlohr Bueso630952c2014-09-11 21:42:25 -0700898 cxt.nrealwriters_stress = nwriters_stress;
Paul E. McKenney0af3fe12014-02-04 15:51:41 -0800899 else
Davidlohr Bueso630952c2014-09-11 21:42:25 -0700900 cxt.nrealwriters_stress = 2 * num_online_cpus();
Davidlohr Buesof095bfc2014-09-11 20:40:19 -0700901
902#ifdef CONFIG_DEBUG_MUTEXES
903 if (strncmp(torture_type, "mutex", 5) == 0)
Davidlohr Bueso630952c2014-09-11 21:42:25 -0700904 cxt.debug_lock = true;
Davidlohr Buesof095bfc2014-09-11 20:40:19 -0700905#endif
Davidlohr Bueso095777c2015-07-22 14:07:27 -0700906#ifdef CONFIG_DEBUG_RT_MUTEXES
907 if (strncmp(torture_type, "rtmutex", 7) == 0)
908 cxt.debug_lock = true;
909#endif
Davidlohr Buesof095bfc2014-09-11 20:40:19 -0700910#ifdef CONFIG_DEBUG_SPINLOCK
Davidlohr Buesoe34191f2014-09-29 06:14:23 -0700911 if ((strncmp(torture_type, "spin", 4) == 0) ||
912 (strncmp(torture_type, "rw_lock", 7) == 0))
Davidlohr Bueso630952c2014-09-11 21:42:25 -0700913 cxt.debug_lock = true;
Davidlohr Buesof095bfc2014-09-11 20:40:19 -0700914#endif
Paul E. McKenney0af3fe12014-02-04 15:51:41 -0800915
916 /* Initialize the statistics so that each run gets its own numbers. */
Davidlohr Bueso2ce77d12017-05-15 02:07:23 -0700917 if (nwriters_stress) {
918 lock_is_write_held = 0;
Kees Cook6da2ec52018-06-12 13:55:00 -0700919 cxt.lwsa = kmalloc_array(cxt.nrealwriters_stress,
920 sizeof(*cxt.lwsa),
921 GFP_KERNEL);
Davidlohr Bueso2ce77d12017-05-15 02:07:23 -0700922 if (cxt.lwsa == NULL) {
923 VERBOSE_TOROUT_STRING("cxt.lwsa: Out of memory");
924 firsterr = -ENOMEM;
925 goto unwind;
926 }
Paul E. McKenney0af3fe12014-02-04 15:51:41 -0800927
Davidlohr Bueso2ce77d12017-05-15 02:07:23 -0700928 for (i = 0; i < cxt.nrealwriters_stress; i++) {
929 cxt.lwsa[i].n_lock_fail = 0;
930 cxt.lwsa[i].n_lock_acquired = 0;
931 }
Paul E. McKenney0af3fe12014-02-04 15:51:41 -0800932 }
933
Davidlohr Bueso630952c2014-09-11 21:42:25 -0700934 if (cxt.cur_ops->readlock) {
Davidlohr Bueso4f6332c2014-09-11 21:40:41 -0700935 if (nreaders_stress >= 0)
Davidlohr Bueso630952c2014-09-11 21:42:25 -0700936 cxt.nrealreaders_stress = nreaders_stress;
Davidlohr Bueso4f6332c2014-09-11 21:40:41 -0700937 else {
938 /*
939 * By default distribute evenly the number of
940 * readers and writers. We still run the same number
941 * of threads as the writer-only locks default.
942 */
943 if (nwriters_stress < 0) /* user doesn't care */
Davidlohr Bueso630952c2014-09-11 21:42:25 -0700944 cxt.nrealwriters_stress = num_online_cpus();
945 cxt.nrealreaders_stress = cxt.nrealwriters_stress;
Davidlohr Bueso4f6332c2014-09-11 21:40:41 -0700946 }
Paul E. McKenney0af3fe12014-02-04 15:51:41 -0800947
Davidlohr Bueso2ce77d12017-05-15 02:07:23 -0700948 if (nreaders_stress) {
949 lock_is_read_held = 0;
Kees Cook6da2ec52018-06-12 13:55:00 -0700950 cxt.lrsa = kmalloc_array(cxt.nrealreaders_stress,
951 sizeof(*cxt.lrsa),
952 GFP_KERNEL);
Davidlohr Bueso2ce77d12017-05-15 02:07:23 -0700953 if (cxt.lrsa == NULL) {
954 VERBOSE_TOROUT_STRING("cxt.lrsa: Out of memory");
955 firsterr = -ENOMEM;
956 kfree(cxt.lwsa);
957 cxt.lwsa = NULL;
958 goto unwind;
959 }
Davidlohr Bueso4f6332c2014-09-11 21:40:41 -0700960
Davidlohr Bueso2ce77d12017-05-15 02:07:23 -0700961 for (i = 0; i < cxt.nrealreaders_stress; i++) {
962 cxt.lrsa[i].n_lock_fail = 0;
963 cxt.lrsa[i].n_lock_acquired = 0;
964 }
Davidlohr Bueso4f6332c2014-09-11 21:40:41 -0700965 }
966 }
Davidlohr Buesoc1c33b92016-04-12 08:47:18 -0700967
Davidlohr Bueso630952c2014-09-11 21:42:25 -0700968 lock_torture_print_module_parms(cxt.cur_ops, "Start of test");
Davidlohr Bueso4f6332c2014-09-11 21:40:41 -0700969
970 /* Prepare torture context. */
Paul E. McKenney0af3fe12014-02-04 15:51:41 -0800971 if (onoff_interval > 0) {
972 firsterr = torture_onoff_init(onoff_holdoff * HZ,
973 onoff_interval * HZ);
974 if (firsterr)
975 goto unwind;
976 }
977 if (shuffle_interval > 0) {
978 firsterr = torture_shuffle_init(shuffle_interval);
979 if (firsterr)
980 goto unwind;
981 }
982 if (shutdown_secs > 0) {
983 firsterr = torture_shutdown_init(shutdown_secs,
984 lock_torture_cleanup);
985 if (firsterr)
986 goto unwind;
987 }
988 if (stutter > 0) {
989 firsterr = torture_stutter_init(stutter);
990 if (firsterr)
991 goto unwind;
992 }
993
Davidlohr Bueso2ce77d12017-05-15 02:07:23 -0700994 if (nwriters_stress) {
Kees Cook6396bb22018-06-12 14:03:40 -0700995 writer_tasks = kcalloc(cxt.nrealwriters_stress,
996 sizeof(writer_tasks[0]),
Davidlohr Bueso2ce77d12017-05-15 02:07:23 -0700997 GFP_KERNEL);
998 if (writer_tasks == NULL) {
999 VERBOSE_TOROUT_ERRSTRING("writer_tasks: Out of memory");
1000 firsterr = -ENOMEM;
1001 goto unwind;
1002 }
Paul E. McKenney0af3fe12014-02-04 15:51:41 -08001003 }
Davidlohr Bueso4f6332c2014-09-11 21:40:41 -07001004
Davidlohr Bueso630952c2014-09-11 21:42:25 -07001005 if (cxt.cur_ops->readlock) {
Kees Cook6396bb22018-06-12 14:03:40 -07001006 reader_tasks = kcalloc(cxt.nrealreaders_stress,
1007 sizeof(reader_tasks[0]),
Davidlohr Bueso4f6332c2014-09-11 21:40:41 -07001008 GFP_KERNEL);
1009 if (reader_tasks == NULL) {
1010 VERBOSE_TOROUT_ERRSTRING("reader_tasks: Out of memory");
Yang Shif4dbba52016-11-10 13:06:39 -08001011 kfree(writer_tasks);
1012 writer_tasks = NULL;
Davidlohr Bueso4f6332c2014-09-11 21:40:41 -07001013 firsterr = -ENOMEM;
1014 goto unwind;
1015 }
1016 }
1017
1018 /*
1019 * Create the kthreads and start torturing (oh, those poor little locks).
1020 *
1021 * TODO: Note that we interleave writers with readers, giving writers a
1022 * slight advantage, by creating its kthread first. This can be modified
1023 * for very specific needs, or even let the user choose the policy, if
1024 * ever wanted.
1025 */
Davidlohr Bueso630952c2014-09-11 21:42:25 -07001026 for (i = 0, j = 0; i < cxt.nrealwriters_stress ||
1027 j < cxt.nrealreaders_stress; i++, j++) {
1028 if (i >= cxt.nrealwriters_stress)
Davidlohr Bueso4f6332c2014-09-11 21:40:41 -07001029 goto create_reader;
1030
1031 /* Create writer. */
Davidlohr Bueso630952c2014-09-11 21:42:25 -07001032 firsterr = torture_create_kthread(lock_torture_writer, &cxt.lwsa[i],
Paul E. McKenney0af3fe12014-02-04 15:51:41 -08001033 writer_tasks[i]);
1034 if (firsterr)
1035 goto unwind;
Davidlohr Bueso4f6332c2014-09-11 21:40:41 -07001036
1037 create_reader:
Davidlohr Bueso630952c2014-09-11 21:42:25 -07001038 if (cxt.cur_ops->readlock == NULL || (j >= cxt.nrealreaders_stress))
Davidlohr Bueso4f6332c2014-09-11 21:40:41 -07001039 continue;
1040 /* Create reader. */
Davidlohr Bueso630952c2014-09-11 21:42:25 -07001041 firsterr = torture_create_kthread(lock_torture_reader, &cxt.lrsa[j],
Davidlohr Bueso4f6332c2014-09-11 21:40:41 -07001042 reader_tasks[j]);
1043 if (firsterr)
1044 goto unwind;
Paul E. McKenney0af3fe12014-02-04 15:51:41 -08001045 }
1046 if (stat_interval > 0) {
1047 firsterr = torture_create_kthread(lock_torture_stats, NULL,
1048 stats_task);
1049 if (firsterr)
1050 goto unwind;
1051 }
1052 torture_init_end();
1053 return 0;
1054
1055unwind:
1056 torture_init_end();
1057 lock_torture_cleanup();
1058 return firsterr;
1059}
1060
1061module_init(lock_torture_init);
1062module_exit(lock_torture_cleanup);