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/types.h>
24 #include <linux/kernel.h>
25 #include <linux/init.h>
26 #include <linux/module.h>
27 #include <linux/kthread.h>
28 #include <linux/err.h>
29 #include <linux/spinlock.h>
30 #include <linux/mutex.h>
31 #include <linux/smp.h>
32 #include <linux/interrupt.h>
33 #include <linux/sched.h>
34 #include <linux/atomic.h>
35 #include <linux/bitops.h>
36 #include <linux/completion.h>
37 #include <linux/moduleparam.h>
38 #include <linux/percpu.h>
39 #include <linux/notifier.h>
40 #include <linux/reboot.h>
41 #include <linux/freezer.h>
42 #include <linux/cpu.h>
43 #include <linux/delay.h>
44 #include <linux/stat.h>
45 #include <linux/slab.h>
46 #include <linux/trace_clock.h>
47 #include <asm/byteorder.h>
48 #include <linux/torture.h>
50 MODULE_LICENSE("GPL");
51 MODULE_AUTHOR("Paul E. McKenney <paulmck@us.ibm.com>");
53 torture_param(int, nwriters_stress, -1,
54 "Number of write-locking stress-test threads");
55 torture_param(int, nreaders_stress, -1,
56 "Number of read-locking stress-test threads");
57 torture_param(int, onoff_holdoff, 0, "Time after boot before CPU hotplugs (s)");
58 torture_param(int, onoff_interval, 0,
59 "Time between CPU hotplugs (s), 0=disable");
60 torture_param(int, shuffle_interval, 3,
61 "Number of jiffies between shuffles, 0=disable");
62 torture_param(int, shutdown_secs, 0, "Shutdown time (j), <= zero to disable.");
63 torture_param(int, stat_interval, 60,
64 "Number of seconds between stats printk()s");
65 torture_param(int, stutter, 5, "Number of jiffies to run/halt test, 0=disable");
66 torture_param(bool, verbose, true,
67 "Enable verbose debugging printk()s");
69 static bool debug_lock = false;
70 static char *torture_type = "spin_lock";
71 module_param(torture_type, charp, 0444);
72 MODULE_PARM_DESC(torture_type,
73 "Type of lock to torture (spin_lock, spin_lock_irq, mutex_lock, ...)");
75 static atomic_t n_lock_torture_errors;
77 static struct task_struct *stats_task;
78 static struct task_struct **writer_tasks;
79 static struct task_struct **reader_tasks;
81 static int nrealwriters_stress;
82 static bool lock_is_write_held;
83 static int nrealreaders_stress;
84 static bool lock_is_read_held;
86 struct lock_stress_stats {
90 static struct lock_stress_stats *lwsa; /* writer statistics */
91 static struct lock_stress_stats *lrsa; /* reader statistics */
94 #define LOCKTORTURE_RUNNABLE_INIT 1
96 #define LOCKTORTURE_RUNNABLE_INIT 0
98 int torture_runnable = LOCKTORTURE_RUNNABLE_INIT;
99 module_param(torture_runnable, int, 0444);
100 MODULE_PARM_DESC(torture_runnable, "Start locktorture at module init");
102 /* Forward reference. */
103 static void lock_torture_cleanup(void);
106 * Operations vector for selecting different types of tests.
108 struct lock_torture_ops {
110 int (*writelock)(void);
111 void (*write_delay)(struct torture_random_state *trsp);
112 void (*writeunlock)(void);
113 int (*readlock)(void);
114 void (*read_delay)(struct torture_random_state *trsp);
115 void (*readunlock)(void);
120 static struct lock_torture_ops *cur_ops;
123 * Definitions for lock torture testing.
126 static int torture_lock_busted_write_lock(void)
128 return 0; /* BUGGY, do not use in real life!!! */
131 static void torture_lock_busted_write_delay(struct torture_random_state *trsp)
133 const unsigned long longdelay_us = 100;
135 /* We want a long delay occasionally to force massive contention. */
136 if (!(torture_random(trsp) %
137 (nrealwriters_stress * 2000 * longdelay_us)))
138 mdelay(longdelay_us);
139 #ifdef CONFIG_PREEMPT
140 if (!(torture_random(trsp) % (nrealwriters_stress * 20000)))
141 preempt_schedule(); /* Allow test to be preempted. */
145 static void torture_lock_busted_write_unlock(void)
147 /* BUGGY, do not use in real life!!! */
150 static struct lock_torture_ops lock_busted_ops = {
151 .writelock = torture_lock_busted_write_lock,
152 .write_delay = torture_lock_busted_write_delay,
153 .writeunlock = torture_lock_busted_write_unlock,
157 .name = "lock_busted"
160 static DEFINE_SPINLOCK(torture_spinlock);
162 static int torture_spin_lock_write_lock(void) __acquires(torture_spinlock)
164 spin_lock(&torture_spinlock);
168 static void torture_spin_lock_write_delay(struct torture_random_state *trsp)
170 const unsigned long shortdelay_us = 2;
171 const unsigned long longdelay_us = 100;
173 /* We want a short delay mostly to emulate likely code, and
174 * we want a long delay occasionally to force massive contention.
176 if (!(torture_random(trsp) %
177 (nrealwriters_stress * 2000 * longdelay_us)))
178 mdelay(longdelay_us);
179 if (!(torture_random(trsp) %
180 (nrealwriters_stress * 2 * shortdelay_us)))
181 udelay(shortdelay_us);
182 #ifdef CONFIG_PREEMPT
183 if (!(torture_random(trsp) % (nrealwriters_stress * 20000)))
184 preempt_schedule(); /* Allow test to be preempted. */
188 static void torture_spin_lock_write_unlock(void) __releases(torture_spinlock)
190 spin_unlock(&torture_spinlock);
193 static struct lock_torture_ops spin_lock_ops = {
194 .writelock = torture_spin_lock_write_lock,
195 .write_delay = torture_spin_lock_write_delay,
196 .writeunlock = torture_spin_lock_write_unlock,
203 static int torture_spin_lock_write_lock_irq(void)
204 __acquires(torture_spinlock_irq)
208 spin_lock_irqsave(&torture_spinlock, flags);
209 cur_ops->flags = flags;
213 static void torture_lock_spin_write_unlock_irq(void)
214 __releases(torture_spinlock)
216 spin_unlock_irqrestore(&torture_spinlock, cur_ops->flags);
219 static struct lock_torture_ops spin_lock_irq_ops = {
220 .writelock = torture_spin_lock_write_lock_irq,
221 .write_delay = torture_spin_lock_write_delay,
222 .writeunlock = torture_lock_spin_write_unlock_irq,
226 .name = "spin_lock_irq"
229 static DEFINE_MUTEX(torture_mutex);
231 static int torture_mutex_lock(void) __acquires(torture_mutex)
233 mutex_lock(&torture_mutex);
237 static void torture_mutex_delay(struct torture_random_state *trsp)
239 const unsigned long longdelay_ms = 100;
241 /* We want a long delay occasionally to force massive contention. */
242 if (!(torture_random(trsp) %
243 (nrealwriters_stress * 2000 * longdelay_ms)))
244 mdelay(longdelay_ms * 5);
246 mdelay(longdelay_ms / 5);
247 #ifdef CONFIG_PREEMPT
248 if (!(torture_random(trsp) % (nrealwriters_stress * 20000)))
249 preempt_schedule(); /* Allow test to be preempted. */
253 static void torture_mutex_unlock(void) __releases(torture_mutex)
255 mutex_unlock(&torture_mutex);
258 static struct lock_torture_ops mutex_lock_ops = {
259 .writelock = torture_mutex_lock,
260 .write_delay = torture_mutex_delay,
261 .writeunlock = torture_mutex_unlock,
269 * Lock torture writer kthread. Repeatedly acquires and releases
270 * the lock, checking for duplicate acquisitions.
272 static int lock_torture_writer(void *arg)
274 struct lock_stress_stats *lwsp = arg;
275 static DEFINE_TORTURE_RANDOM(rand);
277 VERBOSE_TOROUT_STRING("lock_torture_writer task started");
278 set_user_nice(current, MAX_NICE);
281 if ((torture_random(&rand) & 0xfffff) == 0)
282 schedule_timeout_uninterruptible(1);
283 cur_ops->writelock();
284 if (WARN_ON_ONCE(lock_is_write_held))
286 lock_is_write_held = 1;
287 lwsp->n_lock_acquired++;
288 cur_ops->write_delay(&rand);
289 lock_is_write_held = 0;
290 cur_ops->writeunlock();
291 stutter_wait("lock_torture_writer");
292 } while (!torture_must_stop());
293 torture_kthread_stopping("lock_torture_writer");
298 * Lock torture reader kthread. Repeatedly acquires and releases
301 static int lock_torture_reader(void *arg)
303 struct lock_stress_stats *lrsp = arg;
304 static DEFINE_TORTURE_RANDOM(rand);
306 VERBOSE_TOROUT_STRING("lock_torture_reader task started");
307 set_user_nice(current, MAX_NICE);
310 if ((torture_random(&rand) & 0xfffff) == 0)
311 schedule_timeout_uninterruptible(1);
313 lock_is_read_held = 1;
314 lrsp->n_lock_acquired++;
315 cur_ops->read_delay(&rand);
316 lock_is_read_held = 0;
317 cur_ops->readunlock();
318 stutter_wait("lock_torture_reader");
319 } while (!torture_must_stop());
320 torture_kthread_stopping("lock_torture_reader");
325 * Create an lock-torture-statistics message in the specified buffer.
327 static void __torture_print_stats(char *page,
328 struct lock_stress_stats *statp, bool write)
333 long min = statp[0].n_lock_acquired;
336 n_stress = write ? nrealwriters_stress : nrealreaders_stress;
337 for (i = 0; i < n_stress; i++) {
338 if (statp[i].n_lock_fail)
340 sum += statp[i].n_lock_acquired;
341 if (max < statp[i].n_lock_fail)
342 max = statp[i].n_lock_fail;
343 if (min > statp[i].n_lock_fail)
344 min = statp[i].n_lock_fail;
346 page += sprintf(page,
347 "%s: Total: %lld Max/Min: %ld/%ld %s Fail: %d %s\n",
348 write ? "Writes" : "Reads ",
349 sum, max, min, max / 2 > min ? "???" : "",
350 fail, fail ? "!!!" : "");
352 atomic_inc(&n_lock_torture_errors);
356 * Print torture statistics. Caller must ensure that there is only one
357 * call to this function at a given time!!! This is normally accomplished
358 * by relying on the module system to only have one copy of the module
359 * loaded, and then by giving the lock_torture_stats kthread full control
360 * (or the init/cleanup functions when lock_torture_stats thread is not
363 static void lock_torture_stats_print(void)
365 int size = nrealwriters_stress * 200 + 8192;
368 if (cur_ops->readlock)
369 size += nrealreaders_stress * 200 + 8192;
371 buf = kmalloc(size, GFP_KERNEL);
373 pr_err("lock_torture_stats_print: Out of memory, need: %d",
378 __torture_print_stats(buf, lwsa, true);
382 if (cur_ops->readlock) {
383 buf = kmalloc(size, GFP_KERNEL);
385 pr_err("lock_torture_stats_print: Out of memory, need: %d",
390 __torture_print_stats(buf, lrsa, false);
397 * Periodically prints torture statistics, if periodic statistics printing
398 * was specified via the stat_interval module parameter.
400 * No need to worry about fullstop here, since this one doesn't reference
401 * volatile state or register callbacks.
403 static int lock_torture_stats(void *arg)
405 VERBOSE_TOROUT_STRING("lock_torture_stats task started");
407 schedule_timeout_interruptible(stat_interval * HZ);
408 lock_torture_stats_print();
409 torture_shutdown_absorb("lock_torture_stats");
410 } while (!torture_must_stop());
411 torture_kthread_stopping("lock_torture_stats");
416 lock_torture_print_module_parms(struct lock_torture_ops *cur_ops,
419 pr_alert("%s" TORTURE_FLAG
420 "--- %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",
421 torture_type, tag, debug_lock ? " [debug]": "",
422 nrealwriters_stress, nrealreaders_stress, stat_interval,
423 verbose, shuffle_interval, stutter, shutdown_secs,
424 onoff_interval, onoff_holdoff);
427 static void lock_torture_cleanup(void)
431 if (torture_cleanup_begin())
435 for (i = 0; i < nrealwriters_stress; i++)
436 torture_stop_kthread(lock_torture_writer,
443 for (i = 0; i < nrealreaders_stress; i++)
444 torture_stop_kthread(lock_torture_reader,
450 torture_stop_kthread(lock_torture_stats, stats_task);
451 lock_torture_stats_print(); /* -After- the stats thread is stopped! */
453 if (atomic_read(&n_lock_torture_errors))
454 lock_torture_print_module_parms(cur_ops,
455 "End of test: FAILURE");
456 else if (torture_onoff_failures())
457 lock_torture_print_module_parms(cur_ops,
458 "End of test: LOCK_HOTPLUG");
460 lock_torture_print_module_parms(cur_ops,
461 "End of test: SUCCESS");
462 torture_cleanup_end();
465 static int __init lock_torture_init(void)
469 static struct lock_torture_ops *torture_ops[] = {
470 &lock_busted_ops, &spin_lock_ops, &spin_lock_irq_ops, &mutex_lock_ops,
473 if (!torture_init_begin(torture_type, verbose, &torture_runnable))
476 /* Process args and tell the world that the torturer is on the job. */
477 for (i = 0; i < ARRAY_SIZE(torture_ops); i++) {
478 cur_ops = torture_ops[i];
479 if (strcmp(torture_type, cur_ops->name) == 0)
482 if (i == ARRAY_SIZE(torture_ops)) {
483 pr_alert("lock-torture: invalid torture type: \"%s\"\n",
485 pr_alert("lock-torture types:");
486 for (i = 0; i < ARRAY_SIZE(torture_ops); i++)
487 pr_alert(" %s", torture_ops[i]->name);
493 cur_ops->init(); /* no "goto unwind" prior to this point!!! */
495 if (nwriters_stress >= 0)
496 nrealwriters_stress = nwriters_stress;
498 nrealwriters_stress = 2 * num_online_cpus();
500 #ifdef CONFIG_DEBUG_MUTEXES
501 if (strncmp(torture_type, "mutex", 5) == 0)
504 #ifdef CONFIG_DEBUG_SPINLOCK
505 if (strncmp(torture_type, "spin", 4) == 0)
509 /* Initialize the statistics so that each run gets its own numbers. */
511 lock_is_write_held = 0;
512 lwsa = kmalloc(sizeof(*lwsa) * nrealwriters_stress, GFP_KERNEL);
514 VERBOSE_TOROUT_STRING("lwsa: Out of memory");
518 for (i = 0; i < nrealwriters_stress; i++) {
519 lwsa[i].n_lock_fail = 0;
520 lwsa[i].n_lock_acquired = 0;
523 if (cur_ops->readlock) {
524 if (nreaders_stress >= 0)
525 nrealreaders_stress = nreaders_stress;
528 * By default distribute evenly the number of
529 * readers and writers. We still run the same number
530 * of threads as the writer-only locks default.
532 if (nwriters_stress < 0) /* user doesn't care */
533 nrealwriters_stress = num_online_cpus();
534 nrealreaders_stress = nrealwriters_stress;
537 lock_is_read_held = 0;
538 lrsa = kmalloc(sizeof(*lrsa) * nrealreaders_stress, GFP_KERNEL);
540 VERBOSE_TOROUT_STRING("lrsa: Out of memory");
546 for (i = 0; i < nrealreaders_stress; i++) {
547 lrsa[i].n_lock_fail = 0;
548 lrsa[i].n_lock_acquired = 0;
551 lock_torture_print_module_parms(cur_ops, "Start of test");
553 /* Prepare torture context. */
554 if (onoff_interval > 0) {
555 firsterr = torture_onoff_init(onoff_holdoff * HZ,
556 onoff_interval * HZ);
560 if (shuffle_interval > 0) {
561 firsterr = torture_shuffle_init(shuffle_interval);
565 if (shutdown_secs > 0) {
566 firsterr = torture_shutdown_init(shutdown_secs,
567 lock_torture_cleanup);
572 firsterr = torture_stutter_init(stutter);
577 writer_tasks = kzalloc(nrealwriters_stress * sizeof(writer_tasks[0]),
579 if (writer_tasks == NULL) {
580 VERBOSE_TOROUT_ERRSTRING("writer_tasks: Out of memory");
585 if (cur_ops->readlock) {
586 reader_tasks = kzalloc(nrealreaders_stress * sizeof(reader_tasks[0]),
588 if (reader_tasks == NULL) {
589 VERBOSE_TOROUT_ERRSTRING("reader_tasks: Out of memory");
596 * Create the kthreads and start torturing (oh, those poor little locks).
598 * TODO: Note that we interleave writers with readers, giving writers a
599 * slight advantage, by creating its kthread first. This can be modified
600 * for very specific needs, or even let the user choose the policy, if
603 for (i = 0, j = 0; i < nrealwriters_stress ||
604 j < nrealreaders_stress; i++, j++) {
605 if (i >= nrealwriters_stress)
609 firsterr = torture_create_kthread(lock_torture_writer, &lwsa[i],
615 if (cur_ops->readlock == NULL || (j >= nrealreaders_stress))
618 firsterr = torture_create_kthread(lock_torture_reader, &lrsa[j],
623 if (stat_interval > 0) {
624 firsterr = torture_create_kthread(lock_torture_stats, NULL,
634 lock_torture_cleanup();
638 module_init(lock_torture_init);
639 module_exit(lock_torture_cleanup);