2 * Module-based torture test facility for locking
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.
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.
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.
18 * Copyright (C) IBM Corporation, 2014
20 * Author: Paul E. McKenney <paulmck@us.ibm.com>
21 * Based on kernel/rcu/torture.c.
23 #include <linux/kernel.h>
24 #include <linux/module.h>
25 #include <linux/kthread.h>
26 #include <linux/spinlock.h>
27 #include <linux/mutex.h>
28 #include <linux/rwsem.h>
29 #include <linux/smp.h>
30 #include <linux/interrupt.h>
31 #include <linux/sched.h>
32 #include <linux/atomic.h>
33 #include <linux/moduleparam.h>
34 #include <linux/delay.h>
35 #include <linux/slab.h>
36 #include <linux/torture.h>
38 MODULE_LICENSE("GPL");
39 MODULE_AUTHOR("Paul E. McKenney <paulmck@us.ibm.com>");
41 torture_param(int, nwriters_stress, -1,
42 "Number of write-locking stress-test threads");
43 torture_param(int, nreaders_stress, -1,
44 "Number of read-locking stress-test threads");
45 torture_param(int, onoff_holdoff, 0, "Time after boot before CPU hotplugs (s)");
46 torture_param(int, onoff_interval, 0,
47 "Time between CPU hotplugs (s), 0=disable");
48 torture_param(int, shuffle_interval, 3,
49 "Number of jiffies between shuffles, 0=disable");
50 torture_param(int, shutdown_secs, 0, "Shutdown time (j), <= zero to disable.");
51 torture_param(int, stat_interval, 60,
52 "Number of seconds between stats printk()s");
53 torture_param(int, stutter, 5, "Number of jiffies to run/halt test, 0=disable");
54 torture_param(bool, verbose, true,
55 "Enable verbose debugging printk()s");
57 static char *torture_type = "spin_lock";
58 module_param(torture_type, charp, 0444);
59 MODULE_PARM_DESC(torture_type,
60 "Type of lock to torture (spin_lock, spin_lock_irq, mutex_lock, ...)");
62 static struct task_struct *stats_task;
63 static struct task_struct **writer_tasks;
64 static struct task_struct **reader_tasks;
66 static bool lock_is_write_held;
67 static bool lock_is_read_held;
69 struct lock_stress_stats {
75 #define LOCKTORTURE_RUNNABLE_INIT 1
77 #define LOCKTORTURE_RUNNABLE_INIT 0
79 int torture_runnable = LOCKTORTURE_RUNNABLE_INIT;
80 module_param(torture_runnable, int, 0444);
81 MODULE_PARM_DESC(torture_runnable, "Start locktorture at module init");
83 /* Forward reference. */
84 static void lock_torture_cleanup(void);
87 * Operations vector for selecting different types of tests.
89 struct lock_torture_ops {
91 int (*writelock)(void);
92 void (*write_delay)(struct torture_random_state *trsp);
93 void (*writeunlock)(void);
94 int (*readlock)(void);
95 void (*read_delay)(struct torture_random_state *trsp);
96 void (*readunlock)(void);
101 struct lock_torture_cxt {
102 int nrealwriters_stress;
103 int nrealreaders_stress;
105 atomic_t n_lock_torture_errors;
106 struct lock_torture_ops *cur_ops;
107 struct lock_stress_stats *lwsa; /* writer statistics */
108 struct lock_stress_stats *lrsa; /* reader statistics */
110 static struct lock_torture_cxt cxt = { 0, 0, false,
114 * Definitions for lock torture testing.
117 static int torture_lock_busted_write_lock(void)
119 return 0; /* BUGGY, do not use in real life!!! */
122 static void torture_lock_busted_write_delay(struct torture_random_state *trsp)
124 const unsigned long longdelay_us = 100;
126 /* We want a long delay occasionally to force massive contention. */
127 if (!(torture_random(trsp) %
128 (cxt.nrealwriters_stress * 2000 * longdelay_us)))
129 mdelay(longdelay_us);
130 #ifdef CONFIG_PREEMPT
131 if (!(torture_random(trsp) % (cxt.nrealwriters_stress * 20000)))
132 preempt_schedule(); /* Allow test to be preempted. */
136 static void torture_lock_busted_write_unlock(void)
138 /* BUGGY, do not use in real life!!! */
141 static struct lock_torture_ops lock_busted_ops = {
142 .writelock = torture_lock_busted_write_lock,
143 .write_delay = torture_lock_busted_write_delay,
144 .writeunlock = torture_lock_busted_write_unlock,
148 .name = "lock_busted"
151 static DEFINE_SPINLOCK(torture_spinlock);
153 static int torture_spin_lock_write_lock(void) __acquires(torture_spinlock)
155 spin_lock(&torture_spinlock);
159 static void torture_spin_lock_write_delay(struct torture_random_state *trsp)
161 const unsigned long shortdelay_us = 2;
162 const unsigned long longdelay_us = 100;
164 /* We want a short delay mostly to emulate likely code, and
165 * we want a long delay occasionally to force massive contention.
167 if (!(torture_random(trsp) %
168 (cxt.nrealwriters_stress * 2000 * longdelay_us)))
169 mdelay(longdelay_us);
170 if (!(torture_random(trsp) %
171 (cxt.nrealwriters_stress * 2 * shortdelay_us)))
172 udelay(shortdelay_us);
173 #ifdef CONFIG_PREEMPT
174 if (!(torture_random(trsp) % (cxt.nrealwriters_stress * 20000)))
175 preempt_schedule(); /* Allow test to be preempted. */
179 static void torture_spin_lock_write_unlock(void) __releases(torture_spinlock)
181 spin_unlock(&torture_spinlock);
184 static struct lock_torture_ops spin_lock_ops = {
185 .writelock = torture_spin_lock_write_lock,
186 .write_delay = torture_spin_lock_write_delay,
187 .writeunlock = torture_spin_lock_write_unlock,
194 static int torture_spin_lock_write_lock_irq(void)
195 __acquires(torture_spinlock)
199 spin_lock_irqsave(&torture_spinlock, flags);
200 cxt.cur_ops->flags = flags;
204 static void torture_lock_spin_write_unlock_irq(void)
205 __releases(torture_spinlock)
207 spin_unlock_irqrestore(&torture_spinlock, cxt.cur_ops->flags);
210 static struct lock_torture_ops spin_lock_irq_ops = {
211 .writelock = torture_spin_lock_write_lock_irq,
212 .write_delay = torture_spin_lock_write_delay,
213 .writeunlock = torture_lock_spin_write_unlock_irq,
217 .name = "spin_lock_irq"
220 static DEFINE_RWLOCK(torture_rwlock);
222 static int torture_rwlock_write_lock(void) __acquires(torture_rwlock)
224 write_lock(&torture_rwlock);
228 static void torture_rwlock_write_delay(struct torture_random_state *trsp)
230 const unsigned long shortdelay_us = 2;
231 const unsigned long longdelay_ms = 100;
233 /* We want a short delay mostly to emulate likely code, and
234 * we want a long delay occasionally to force massive contention.
236 if (!(torture_random(trsp) %
237 (cxt.nrealwriters_stress * 2000 * longdelay_ms)))
238 mdelay(longdelay_ms);
240 udelay(shortdelay_us);
243 static void torture_rwlock_write_unlock(void) __releases(torture_rwlock)
245 write_unlock(&torture_rwlock);
248 static int torture_rwlock_read_lock(void) __acquires(torture_rwlock)
250 read_lock(&torture_rwlock);
254 static void torture_rwlock_read_delay(struct torture_random_state *trsp)
256 const unsigned long shortdelay_us = 10;
257 const unsigned long longdelay_ms = 100;
259 /* We want a short delay mostly to emulate likely code, and
260 * we want a long delay occasionally to force massive contention.
262 if (!(torture_random(trsp) %
263 (cxt.nrealreaders_stress * 2000 * longdelay_ms)))
264 mdelay(longdelay_ms);
266 udelay(shortdelay_us);
269 static void torture_rwlock_read_unlock(void) __releases(torture_rwlock)
271 read_unlock(&torture_rwlock);
274 static struct lock_torture_ops rw_lock_ops = {
275 .writelock = torture_rwlock_write_lock,
276 .write_delay = torture_rwlock_write_delay,
277 .writeunlock = torture_rwlock_write_unlock,
278 .readlock = torture_rwlock_read_lock,
279 .read_delay = torture_rwlock_read_delay,
280 .readunlock = torture_rwlock_read_unlock,
284 static int torture_rwlock_write_lock_irq(void) __acquires(torture_rwlock)
288 write_lock_irqsave(&torture_rwlock, flags);
289 cxt.cur_ops->flags = flags;
293 static void torture_rwlock_write_unlock_irq(void)
294 __releases(torture_rwlock)
296 write_unlock_irqrestore(&torture_rwlock, cxt.cur_ops->flags);
299 static int torture_rwlock_read_lock_irq(void) __acquires(torture_rwlock)
303 read_lock_irqsave(&torture_rwlock, flags);
304 cxt.cur_ops->flags = flags;
308 static void torture_rwlock_read_unlock_irq(void)
309 __releases(torture_rwlock)
311 write_unlock_irqrestore(&torture_rwlock, cxt.cur_ops->flags);
314 static struct lock_torture_ops rw_lock_irq_ops = {
315 .writelock = torture_rwlock_write_lock_irq,
316 .write_delay = torture_rwlock_write_delay,
317 .writeunlock = torture_rwlock_write_unlock_irq,
318 .readlock = torture_rwlock_read_lock_irq,
319 .read_delay = torture_rwlock_read_delay,
320 .readunlock = torture_rwlock_read_unlock_irq,
321 .name = "rw_lock_irq"
324 static DEFINE_MUTEX(torture_mutex);
326 static int torture_mutex_lock(void) __acquires(torture_mutex)
328 mutex_lock(&torture_mutex);
332 static void torture_mutex_delay(struct torture_random_state *trsp)
334 const unsigned long longdelay_ms = 100;
336 /* We want a long delay occasionally to force massive contention. */
337 if (!(torture_random(trsp) %
338 (cxt.nrealwriters_stress * 2000 * longdelay_ms)))
339 mdelay(longdelay_ms * 5);
341 mdelay(longdelay_ms / 5);
342 #ifdef CONFIG_PREEMPT
343 if (!(torture_random(trsp) % (cxt.nrealwriters_stress * 20000)))
344 preempt_schedule(); /* Allow test to be preempted. */
348 static void torture_mutex_unlock(void) __releases(torture_mutex)
350 mutex_unlock(&torture_mutex);
353 static struct lock_torture_ops mutex_lock_ops = {
354 .writelock = torture_mutex_lock,
355 .write_delay = torture_mutex_delay,
356 .writeunlock = torture_mutex_unlock,
363 static DECLARE_RWSEM(torture_rwsem);
364 static int torture_rwsem_down_write(void) __acquires(torture_rwsem)
366 down_write(&torture_rwsem);
370 static void torture_rwsem_write_delay(struct torture_random_state *trsp)
372 const unsigned long longdelay_ms = 100;
374 /* We want a long delay occasionally to force massive contention. */
375 if (!(torture_random(trsp) %
376 (cxt.nrealwriters_stress * 2000 * longdelay_ms)))
377 mdelay(longdelay_ms * 10);
379 mdelay(longdelay_ms / 10);
380 #ifdef CONFIG_PREEMPT
381 if (!(torture_random(trsp) % (cxt.nrealwriters_stress * 20000)))
382 preempt_schedule(); /* Allow test to be preempted. */
386 static void torture_rwsem_up_write(void) __releases(torture_rwsem)
388 up_write(&torture_rwsem);
391 static int torture_rwsem_down_read(void) __acquires(torture_rwsem)
393 down_read(&torture_rwsem);
397 static void torture_rwsem_read_delay(struct torture_random_state *trsp)
399 const unsigned long longdelay_ms = 100;
401 /* We want a long delay occasionally to force massive contention. */
402 if (!(torture_random(trsp) %
403 (cxt.nrealwriters_stress * 2000 * longdelay_ms)))
404 mdelay(longdelay_ms * 2);
406 mdelay(longdelay_ms / 2);
407 #ifdef CONFIG_PREEMPT
408 if (!(torture_random(trsp) % (cxt.nrealreaders_stress * 20000)))
409 preempt_schedule(); /* Allow test to be preempted. */
413 static void torture_rwsem_up_read(void) __releases(torture_rwsem)
415 up_read(&torture_rwsem);
418 static struct lock_torture_ops rwsem_lock_ops = {
419 .writelock = torture_rwsem_down_write,
420 .write_delay = torture_rwsem_write_delay,
421 .writeunlock = torture_rwsem_up_write,
422 .readlock = torture_rwsem_down_read,
423 .read_delay = torture_rwsem_read_delay,
424 .readunlock = torture_rwsem_up_read,
429 * Lock torture writer kthread. Repeatedly acquires and releases
430 * the lock, checking for duplicate acquisitions.
432 static int lock_torture_writer(void *arg)
434 struct lock_stress_stats *lwsp = arg;
435 static DEFINE_TORTURE_RANDOM(rand);
437 VERBOSE_TOROUT_STRING("lock_torture_writer task started");
438 set_user_nice(current, MAX_NICE);
441 if ((torture_random(&rand) & 0xfffff) == 0)
442 schedule_timeout_uninterruptible(1);
444 cxt.cur_ops->writelock();
445 if (WARN_ON_ONCE(lock_is_write_held))
447 lock_is_write_held = 1;
448 if (WARN_ON_ONCE(lock_is_read_held))
449 lwsp->n_lock_fail++; /* rare, but... */
451 lwsp->n_lock_acquired++;
452 cxt.cur_ops->write_delay(&rand);
453 lock_is_write_held = 0;
454 cxt.cur_ops->writeunlock();
456 stutter_wait("lock_torture_writer");
457 } while (!torture_must_stop());
458 torture_kthread_stopping("lock_torture_writer");
463 * Lock torture reader kthread. Repeatedly acquires and releases
466 static int lock_torture_reader(void *arg)
468 struct lock_stress_stats *lrsp = arg;
469 static DEFINE_TORTURE_RANDOM(rand);
471 VERBOSE_TOROUT_STRING("lock_torture_reader task started");
472 set_user_nice(current, MAX_NICE);
475 if ((torture_random(&rand) & 0xfffff) == 0)
476 schedule_timeout_uninterruptible(1);
478 cxt.cur_ops->readlock();
479 lock_is_read_held = 1;
480 if (WARN_ON_ONCE(lock_is_write_held))
481 lrsp->n_lock_fail++; /* rare, but... */
483 lrsp->n_lock_acquired++;
484 cxt.cur_ops->read_delay(&rand);
485 lock_is_read_held = 0;
486 cxt.cur_ops->readunlock();
488 stutter_wait("lock_torture_reader");
489 } while (!torture_must_stop());
490 torture_kthread_stopping("lock_torture_reader");
495 * Create an lock-torture-statistics message in the specified buffer.
497 static void __torture_print_stats(char *page,
498 struct lock_stress_stats *statp, bool write)
503 long min = statp[0].n_lock_acquired;
506 n_stress = write ? cxt.nrealwriters_stress : cxt.nrealreaders_stress;
507 for (i = 0; i < n_stress; i++) {
508 if (statp[i].n_lock_fail)
510 sum += statp[i].n_lock_acquired;
511 if (max < statp[i].n_lock_fail)
512 max = statp[i].n_lock_fail;
513 if (min > statp[i].n_lock_fail)
514 min = statp[i].n_lock_fail;
516 page += sprintf(page,
517 "%s: Total: %lld Max/Min: %ld/%ld %s Fail: %d %s\n",
518 write ? "Writes" : "Reads ",
519 sum, max, min, max / 2 > min ? "???" : "",
520 fail, fail ? "!!!" : "");
522 atomic_inc(&cxt.n_lock_torture_errors);
526 * Print torture statistics. Caller must ensure that there is only one
527 * call to this function at a given time!!! This is normally accomplished
528 * by relying on the module system to only have one copy of the module
529 * loaded, and then by giving the lock_torture_stats kthread full control
530 * (or the init/cleanup functions when lock_torture_stats thread is not
533 static void lock_torture_stats_print(void)
535 int size = cxt.nrealwriters_stress * 200 + 8192;
538 if (cxt.cur_ops->readlock)
539 size += cxt.nrealreaders_stress * 200 + 8192;
541 buf = kmalloc(size, GFP_KERNEL);
543 pr_err("lock_torture_stats_print: Out of memory, need: %d",
548 __torture_print_stats(buf, cxt.lwsa, true);
552 if (cxt.cur_ops->readlock) {
553 buf = kmalloc(size, GFP_KERNEL);
555 pr_err("lock_torture_stats_print: Out of memory, need: %d",
560 __torture_print_stats(buf, cxt.lrsa, false);
567 * Periodically prints torture statistics, if periodic statistics printing
568 * was specified via the stat_interval module parameter.
570 * No need to worry about fullstop here, since this one doesn't reference
571 * volatile state or register callbacks.
573 static int lock_torture_stats(void *arg)
575 VERBOSE_TOROUT_STRING("lock_torture_stats task started");
577 schedule_timeout_interruptible(stat_interval * HZ);
578 lock_torture_stats_print();
579 torture_shutdown_absorb("lock_torture_stats");
580 } while (!torture_must_stop());
581 torture_kthread_stopping("lock_torture_stats");
586 lock_torture_print_module_parms(struct lock_torture_ops *cur_ops,
589 pr_alert("%s" TORTURE_FLAG
590 "--- %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",
591 torture_type, tag, cxt.debug_lock ? " [debug]": "",
592 cxt.nrealwriters_stress, cxt.nrealreaders_stress, stat_interval,
593 verbose, shuffle_interval, stutter, shutdown_secs,
594 onoff_interval, onoff_holdoff);
597 static void lock_torture_cleanup(void)
601 if (torture_cleanup_begin())
605 for (i = 0; i < cxt.nrealwriters_stress; i++)
606 torture_stop_kthread(lock_torture_writer,
613 for (i = 0; i < cxt.nrealreaders_stress; i++)
614 torture_stop_kthread(lock_torture_reader,
620 torture_stop_kthread(lock_torture_stats, stats_task);
621 lock_torture_stats_print(); /* -After- the stats thread is stopped! */
623 if (atomic_read(&cxt.n_lock_torture_errors))
624 lock_torture_print_module_parms(cxt.cur_ops,
625 "End of test: FAILURE");
626 else if (torture_onoff_failures())
627 lock_torture_print_module_parms(cxt.cur_ops,
628 "End of test: LOCK_HOTPLUG");
630 lock_torture_print_module_parms(cxt.cur_ops,
631 "End of test: SUCCESS");
632 torture_cleanup_end();
635 static int __init lock_torture_init(void)
639 static struct lock_torture_ops *torture_ops[] = {
641 &spin_lock_ops, &spin_lock_irq_ops,
642 &rw_lock_ops, &rw_lock_irq_ops,
647 if (!torture_init_begin(torture_type, verbose, &torture_runnable))
650 /* Process args and tell the world that the torturer is on the job. */
651 for (i = 0; i < ARRAY_SIZE(torture_ops); i++) {
652 cxt.cur_ops = torture_ops[i];
653 if (strcmp(torture_type, cxt.cur_ops->name) == 0)
656 if (i == ARRAY_SIZE(torture_ops)) {
657 pr_alert("lock-torture: invalid torture type: \"%s\"\n",
659 pr_alert("lock-torture types:");
660 for (i = 0; i < ARRAY_SIZE(torture_ops); i++)
661 pr_alert(" %s", torture_ops[i]->name);
666 if (cxt.cur_ops->init)
667 cxt.cur_ops->init(); /* no "goto unwind" prior to this point!!! */
669 if (nwriters_stress >= 0)
670 cxt.nrealwriters_stress = nwriters_stress;
672 cxt.nrealwriters_stress = 2 * num_online_cpus();
674 #ifdef CONFIG_DEBUG_MUTEXES
675 if (strncmp(torture_type, "mutex", 5) == 0)
676 cxt.debug_lock = true;
678 #ifdef CONFIG_DEBUG_SPINLOCK
679 if ((strncmp(torture_type, "spin", 4) == 0) ||
680 (strncmp(torture_type, "rw_lock", 7) == 0))
681 cxt.debug_lock = true;
684 /* Initialize the statistics so that each run gets its own numbers. */
686 lock_is_write_held = 0;
687 cxt.lwsa = kmalloc(sizeof(*cxt.lwsa) * cxt.nrealwriters_stress, GFP_KERNEL);
688 if (cxt.lwsa == NULL) {
689 VERBOSE_TOROUT_STRING("cxt.lwsa: Out of memory");
693 for (i = 0; i < cxt.nrealwriters_stress; i++) {
694 cxt.lwsa[i].n_lock_fail = 0;
695 cxt.lwsa[i].n_lock_acquired = 0;
698 if (cxt.cur_ops->readlock) {
699 if (nreaders_stress >= 0)
700 cxt.nrealreaders_stress = nreaders_stress;
703 * By default distribute evenly the number of
704 * readers and writers. We still run the same number
705 * of threads as the writer-only locks default.
707 if (nwriters_stress < 0) /* user doesn't care */
708 cxt.nrealwriters_stress = num_online_cpus();
709 cxt.nrealreaders_stress = cxt.nrealwriters_stress;
712 lock_is_read_held = 0;
713 cxt.lrsa = kmalloc(sizeof(*cxt.lrsa) * cxt.nrealreaders_stress, GFP_KERNEL);
714 if (cxt.lrsa == NULL) {
715 VERBOSE_TOROUT_STRING("cxt.lrsa: Out of memory");
721 for (i = 0; i < cxt.nrealreaders_stress; i++) {
722 cxt.lrsa[i].n_lock_fail = 0;
723 cxt.lrsa[i].n_lock_acquired = 0;
726 lock_torture_print_module_parms(cxt.cur_ops, "Start of test");
728 /* Prepare torture context. */
729 if (onoff_interval > 0) {
730 firsterr = torture_onoff_init(onoff_holdoff * HZ,
731 onoff_interval * HZ);
735 if (shuffle_interval > 0) {
736 firsterr = torture_shuffle_init(shuffle_interval);
740 if (shutdown_secs > 0) {
741 firsterr = torture_shutdown_init(shutdown_secs,
742 lock_torture_cleanup);
747 firsterr = torture_stutter_init(stutter);
752 writer_tasks = kzalloc(cxt.nrealwriters_stress * sizeof(writer_tasks[0]),
754 if (writer_tasks == NULL) {
755 VERBOSE_TOROUT_ERRSTRING("writer_tasks: Out of memory");
760 if (cxt.cur_ops->readlock) {
761 reader_tasks = kzalloc(cxt.nrealreaders_stress * sizeof(reader_tasks[0]),
763 if (reader_tasks == NULL) {
764 VERBOSE_TOROUT_ERRSTRING("reader_tasks: Out of memory");
771 * Create the kthreads and start torturing (oh, those poor little locks).
773 * TODO: Note that we interleave writers with readers, giving writers a
774 * slight advantage, by creating its kthread first. This can be modified
775 * for very specific needs, or even let the user choose the policy, if
778 for (i = 0, j = 0; i < cxt.nrealwriters_stress ||
779 j < cxt.nrealreaders_stress; i++, j++) {
780 if (i >= cxt.nrealwriters_stress)
784 firsterr = torture_create_kthread(lock_torture_writer, &cxt.lwsa[i],
790 if (cxt.cur_ops->readlock == NULL || (j >= cxt.nrealreaders_stress))
793 firsterr = torture_create_kthread(lock_torture_reader, &cxt.lrsa[j],
798 if (stat_interval > 0) {
799 firsterr = torture_create_kthread(lock_torture_stats, NULL,
809 lock_torture_cleanup();
813 module_init(lock_torture_init);
814 module_exit(lock_torture_cleanup);