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