]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - kernel/locking/locktorture.c
serial: 8250_mid: Enable HSU on Intel Cedar Fork PCH
[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;
0af3fe1e 718 long max = 0;
4f6332c1 719 long min = statp[0].n_lock_acquired;
0af3fe1e
PM
720 long long sum = 0;
721
630952c2 722 n_stress = write ? cxt.nrealwriters_stress : cxt.nrealreaders_stress;
4f6332c1
DB
723 for (i = 0; i < n_stress; i++) {
724 if (statp[i].n_lock_fail)
0af3fe1e 725 fail = true;
4f6332c1
DB
726 sum += statp[i].n_lock_acquired;
727 if (max < statp[i].n_lock_fail)
728 max = statp[i].n_lock_fail;
729 if (min > statp[i].n_lock_fail)
730 min = statp[i].n_lock_fail;
0af3fe1e 731 }
0af3fe1e 732 page += sprintf(page,
4f6332c1
DB
733 "%s: Total: %lld Max/Min: %ld/%ld %s Fail: %d %s\n",
734 write ? "Writes" : "Reads ",
0af3fe1e
PM
735 sum, max, min, max / 2 > min ? "???" : "",
736 fail, fail ? "!!!" : "");
737 if (fail)
630952c2 738 atomic_inc(&cxt.n_lock_torture_errors);
0af3fe1e
PM
739}
740
741/*
742 * Print torture statistics. Caller must ensure that there is only one
743 * call to this function at a given time!!! This is normally accomplished
744 * by relying on the module system to only have one copy of the module
745 * loaded, and then by giving the lock_torture_stats kthread full control
746 * (or the init/cleanup functions when lock_torture_stats thread is not
747 * running).
748 */
749static void lock_torture_stats_print(void)
750{
630952c2 751 int size = cxt.nrealwriters_stress * 200 + 8192;
0af3fe1e
PM
752 char *buf;
753
630952c2
DB
754 if (cxt.cur_ops->readlock)
755 size += cxt.nrealreaders_stress * 200 + 8192;
4f6332c1 756
0af3fe1e
PM
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 }
4f6332c1 763
630952c2 764 __torture_print_stats(buf, cxt.lwsa, true);
0af3fe1e
PM
765 pr_alert("%s", buf);
766 kfree(buf);
4f6332c1 767
630952c2 768 if (cxt.cur_ops->readlock) {
4f6332c1
DB
769 buf = kmalloc(size, GFP_KERNEL);
770 if (!buf) {
771 pr_err("lock_torture_stats_print: Out of memory, need: %d",
772 size);
773 return;
774 }
775
630952c2 776 __torture_print_stats(buf, cxt.lrsa, false);
4f6332c1
DB
777 pr_alert("%s", buf);
778 kfree(buf);
779 }
0af3fe1e
PM
780}
781
782/*
783 * Periodically prints torture statistics, if periodic statistics printing
784 * was specified via the stat_interval module parameter.
785 *
786 * No need to worry about fullstop here, since this one doesn't reference
787 * volatile state or register callbacks.
788 */
789static int lock_torture_stats(void *arg)
790{
791 VERBOSE_TOROUT_STRING("lock_torture_stats task started");
792 do {
793 schedule_timeout_interruptible(stat_interval * HZ);
794 lock_torture_stats_print();
795 torture_shutdown_absorb("lock_torture_stats");
796 } while (!torture_must_stop());
797 torture_kthread_stopping("lock_torture_stats");
798 return 0;
799}
800
801static inline void
802lock_torture_print_module_parms(struct lock_torture_ops *cur_ops,
803 const char *tag)
804{
805 pr_alert("%s" TORTURE_FLAG
4f6332c1 806 "--- %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
807 torture_type, tag, cxt.debug_lock ? " [debug]": "",
808 cxt.nrealwriters_stress, cxt.nrealreaders_stress, stat_interval,
4f6332c1 809 verbose, shuffle_interval, stutter, shutdown_secs,
0af3fe1e
PM
810 onoff_interval, onoff_holdoff);
811}
812
813static void lock_torture_cleanup(void)
814{
815 int i;
816
d36a7a0d 817 if (torture_cleanup_begin())
0af3fe1e
PM
818 return;
819
c1c33b92
DB
820 /*
821 * Indicates early cleanup, meaning that the test has not run,
822 * such as when passing bogus args when loading the module. As
823 * such, only perform the underlying torture-specific cleanups,
824 * and avoid anything related to locktorture.
825 */
826 if (!cxt.lwsa)
827 goto end;
828
0af3fe1e 829 if (writer_tasks) {
630952c2 830 for (i = 0; i < cxt.nrealwriters_stress; i++)
0af3fe1e
PM
831 torture_stop_kthread(lock_torture_writer,
832 writer_tasks[i]);
833 kfree(writer_tasks);
834 writer_tasks = NULL;
835 }
836
4f6332c1 837 if (reader_tasks) {
630952c2 838 for (i = 0; i < cxt.nrealreaders_stress; i++)
4f6332c1
DB
839 torture_stop_kthread(lock_torture_reader,
840 reader_tasks[i]);
841 kfree(reader_tasks);
842 reader_tasks = NULL;
843 }
844
0af3fe1e
PM
845 torture_stop_kthread(lock_torture_stats, stats_task);
846 lock_torture_stats_print(); /* -After- the stats thread is stopped! */
847
630952c2
DB
848 if (atomic_read(&cxt.n_lock_torture_errors))
849 lock_torture_print_module_parms(cxt.cur_ops,
0af3fe1e
PM
850 "End of test: FAILURE");
851 else if (torture_onoff_failures())
630952c2 852 lock_torture_print_module_parms(cxt.cur_ops,
0af3fe1e
PM
853 "End of test: LOCK_HOTPLUG");
854 else
630952c2 855 lock_torture_print_module_parms(cxt.cur_ops,
0af3fe1e 856 "End of test: SUCCESS");
f4dbba59
YS
857
858 kfree(cxt.lwsa);
859 kfree(cxt.lrsa);
860
c1c33b92 861end:
d36a7a0d 862 torture_cleanup_end();
0af3fe1e
PM
863}
864
865static int __init lock_torture_init(void)
866{
4f6332c1 867 int i, j;
0af3fe1e
PM
868 int firsterr = 0;
869 static struct lock_torture_ops *torture_ops[] = {
e34191fa
DB
870 &lock_busted_ops,
871 &spin_lock_ops, &spin_lock_irq_ops,
872 &rw_lock_ops, &rw_lock_irq_ops,
873 &mutex_lock_ops,
0186a6cb 874 &ww_mutex_lock_ops,
095777c4
DB
875#ifdef CONFIG_RT_MUTEXES
876 &rtmutex_lock_ops,
877#endif
e34191fa 878 &rwsem_lock_ops,
617783dd 879 &percpu_rwsem_lock_ops,
0af3fe1e
PM
880 };
881
23a8e5c2 882 if (!torture_init_begin(torture_type, verbose, &torture_runnable))
5228084e 883 return -EBUSY;
0af3fe1e
PM
884
885 /* Process args and tell the world that the torturer is on the job. */
886 for (i = 0; i < ARRAY_SIZE(torture_ops); i++) {
630952c2
DB
887 cxt.cur_ops = torture_ops[i];
888 if (strcmp(torture_type, cxt.cur_ops->name) == 0)
0af3fe1e
PM
889 break;
890 }
891 if (i == ARRAY_SIZE(torture_ops)) {
892 pr_alert("lock-torture: invalid torture type: \"%s\"\n",
893 torture_type);
894 pr_alert("lock-torture types:");
895 for (i = 0; i < ARRAY_SIZE(torture_ops); i++)
896 pr_alert(" %s", torture_ops[i]->name);
897 pr_alert("\n");
a36a9961
PM
898 firsterr = -EINVAL;
899 goto unwind;
0af3fe1e 900 }
630952c2 901 if (cxt.cur_ops->init)
a36a9961 902 cxt.cur_ops->init();
0af3fe1e
PM
903
904 if (nwriters_stress >= 0)
630952c2 905 cxt.nrealwriters_stress = nwriters_stress;
0af3fe1e 906 else
630952c2 907 cxt.nrealwriters_stress = 2 * num_online_cpus();
f095bfc0
DB
908
909#ifdef CONFIG_DEBUG_MUTEXES
910 if (strncmp(torture_type, "mutex", 5) == 0)
630952c2 911 cxt.debug_lock = true;
f095bfc0 912#endif
095777c4
DB
913#ifdef CONFIG_DEBUG_RT_MUTEXES
914 if (strncmp(torture_type, "rtmutex", 7) == 0)
915 cxt.debug_lock = true;
916#endif
f095bfc0 917#ifdef CONFIG_DEBUG_SPINLOCK
e34191fa
DB
918 if ((strncmp(torture_type, "spin", 4) == 0) ||
919 (strncmp(torture_type, "rw_lock", 7) == 0))
630952c2 920 cxt.debug_lock = true;
f095bfc0 921#endif
0af3fe1e
PM
922
923 /* Initialize the statistics so that each run gets its own numbers. */
924
925 lock_is_write_held = 0;
630952c2
DB
926 cxt.lwsa = kmalloc(sizeof(*cxt.lwsa) * cxt.nrealwriters_stress, GFP_KERNEL);
927 if (cxt.lwsa == NULL) {
928 VERBOSE_TOROUT_STRING("cxt.lwsa: Out of memory");
0af3fe1e
PM
929 firsterr = -ENOMEM;
930 goto unwind;
931 }
630952c2
DB
932 for (i = 0; i < cxt.nrealwriters_stress; i++) {
933 cxt.lwsa[i].n_lock_fail = 0;
934 cxt.lwsa[i].n_lock_acquired = 0;
0af3fe1e
PM
935 }
936
630952c2 937 if (cxt.cur_ops->readlock) {
4f6332c1 938 if (nreaders_stress >= 0)
630952c2 939 cxt.nrealreaders_stress = nreaders_stress;
4f6332c1
DB
940 else {
941 /*
942 * By default distribute evenly the number of
943 * readers and writers. We still run the same number
944 * of threads as the writer-only locks default.
945 */
946 if (nwriters_stress < 0) /* user doesn't care */
630952c2
DB
947 cxt.nrealwriters_stress = num_online_cpus();
948 cxt.nrealreaders_stress = cxt.nrealwriters_stress;
4f6332c1
DB
949 }
950
951 lock_is_read_held = 0;
630952c2
DB
952 cxt.lrsa = kmalloc(sizeof(*cxt.lrsa) * cxt.nrealreaders_stress, GFP_KERNEL);
953 if (cxt.lrsa == NULL) {
954 VERBOSE_TOROUT_STRING("cxt.lrsa: Out of memory");
4f6332c1 955 firsterr = -ENOMEM;
630952c2 956 kfree(cxt.lwsa);
c1c33b92 957 cxt.lwsa = NULL;
4f6332c1
DB
958 goto unwind;
959 }
0af3fe1e 960
630952c2
DB
961 for (i = 0; i < cxt.nrealreaders_stress; i++) {
962 cxt.lrsa[i].n_lock_fail = 0;
963 cxt.lrsa[i].n_lock_acquired = 0;
4f6332c1
DB
964 }
965 }
c1c33b92 966
630952c2 967 lock_torture_print_module_parms(cxt.cur_ops, "Start of test");
4f6332c1
DB
968
969 /* Prepare torture context. */
0af3fe1e
PM
970 if (onoff_interval > 0) {
971 firsterr = torture_onoff_init(onoff_holdoff * HZ,
972 onoff_interval * HZ);
973 if (firsterr)
974 goto unwind;
975 }
976 if (shuffle_interval > 0) {
977 firsterr = torture_shuffle_init(shuffle_interval);
978 if (firsterr)
979 goto unwind;
980 }
981 if (shutdown_secs > 0) {
982 firsterr = torture_shutdown_init(shutdown_secs,
983 lock_torture_cleanup);
984 if (firsterr)
985 goto unwind;
986 }
987 if (stutter > 0) {
988 firsterr = torture_stutter_init(stutter);
989 if (firsterr)
990 goto unwind;
991 }
992
630952c2 993 writer_tasks = kzalloc(cxt.nrealwriters_stress * sizeof(writer_tasks[0]),
0af3fe1e
PM
994 GFP_KERNEL);
995 if (writer_tasks == NULL) {
996 VERBOSE_TOROUT_ERRSTRING("writer_tasks: Out of memory");
997 firsterr = -ENOMEM;
998 goto unwind;
999 }
4f6332c1 1000
630952c2
DB
1001 if (cxt.cur_ops->readlock) {
1002 reader_tasks = kzalloc(cxt.nrealreaders_stress * sizeof(reader_tasks[0]),
4f6332c1
DB
1003 GFP_KERNEL);
1004 if (reader_tasks == NULL) {
1005 VERBOSE_TOROUT_ERRSTRING("reader_tasks: Out of memory");
f4dbba59
YS
1006 kfree(writer_tasks);
1007 writer_tasks = NULL;
4f6332c1
DB
1008 firsterr = -ENOMEM;
1009 goto unwind;
1010 }
1011 }
1012
1013 /*
1014 * Create the kthreads and start torturing (oh, those poor little locks).
1015 *
1016 * TODO: Note that we interleave writers with readers, giving writers a
1017 * slight advantage, by creating its kthread first. This can be modified
1018 * for very specific needs, or even let the user choose the policy, if
1019 * ever wanted.
1020 */
630952c2
DB
1021 for (i = 0, j = 0; i < cxt.nrealwriters_stress ||
1022 j < cxt.nrealreaders_stress; i++, j++) {
1023 if (i >= cxt.nrealwriters_stress)
4f6332c1
DB
1024 goto create_reader;
1025
1026 /* Create writer. */
630952c2 1027 firsterr = torture_create_kthread(lock_torture_writer, &cxt.lwsa[i],
0af3fe1e
PM
1028 writer_tasks[i]);
1029 if (firsterr)
1030 goto unwind;
4f6332c1
DB
1031
1032 create_reader:
630952c2 1033 if (cxt.cur_ops->readlock == NULL || (j >= cxt.nrealreaders_stress))
4f6332c1
DB
1034 continue;
1035 /* Create reader. */
630952c2 1036 firsterr = torture_create_kthread(lock_torture_reader, &cxt.lrsa[j],
4f6332c1
DB
1037 reader_tasks[j]);
1038 if (firsterr)
1039 goto unwind;
0af3fe1e
PM
1040 }
1041 if (stat_interval > 0) {
1042 firsterr = torture_create_kthread(lock_torture_stats, NULL,
1043 stats_task);
1044 if (firsterr)
1045 goto unwind;
1046 }
1047 torture_init_end();
1048 return 0;
1049
1050unwind:
1051 torture_init_end();
1052 lock_torture_cleanup();
1053 return firsterr;
1054}
1055
1056module_init(lock_torture_init);
1057module_exit(lock_torture_cleanup);