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