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, onoff_holdoff, 0, "Time after boot before CPU hotplugs (s)");
56 torture_param(int, onoff_interval, 0,
57 "Time between CPU hotplugs (s), 0=disable");
58 torture_param(int, shuffle_interval, 3,
59 "Number of jiffies between shuffles, 0=disable");
60 torture_param(int, shutdown_secs, 0, "Shutdown time (j), <= zero to disable.");
61 torture_param(int, stat_interval, 60,
62 "Number of seconds between stats printk()s");
63 torture_param(int, stutter, 5, "Number of jiffies to run/halt test, 0=disable");
64 torture_param(bool, verbose, true,
65 "Enable verbose debugging printk()s");
67 static char *torture_type = "spin_lock";
68 module_param(torture_type, charp, 0444);
69 MODULE_PARM_DESC(torture_type,
70 "Type of lock to torture (spin_lock, spin_lock_irq, mutex_lock, ...)");
72 static atomic_t n_lock_torture_errors;
74 static struct task_struct *stats_task;
75 static struct task_struct **writer_tasks;
77 static int nrealwriters_stress;
78 static bool lock_is_write_held;
80 struct lock_writer_stress_stats {
81 long n_write_lock_fail;
82 long n_write_lock_acquired;
84 static struct lock_writer_stress_stats *lwsa;
87 #define LOCKTORTURE_RUNNABLE_INIT 1
89 #define LOCKTORTURE_RUNNABLE_INIT 0
91 int torture_runnable = LOCKTORTURE_RUNNABLE_INIT;
92 module_param(torture_runnable, int, 0444);
93 MODULE_PARM_DESC(torture_runnable, "Start locktorture at module init");
95 /* Forward reference. */
96 static void lock_torture_cleanup(void);
99 * Operations vector for selecting different types of tests.
101 struct lock_torture_ops {
103 int (*writelock)(void);
104 void (*write_delay)(struct torture_random_state *trsp);
105 void (*writeunlock)(void);
110 static struct lock_torture_ops *cur_ops;
113 * Definitions for lock torture testing.
116 static int torture_lock_busted_write_lock(void)
118 return 0; /* BUGGY, do not use in real life!!! */
121 static void torture_lock_busted_write_delay(struct torture_random_state *trsp)
123 const unsigned long longdelay_us = 100;
125 /* We want a long delay occasionally to force massive contention. */
126 if (!(torture_random(trsp) %
127 (nrealwriters_stress * 2000 * longdelay_us)))
128 mdelay(longdelay_us);
129 #ifdef CONFIG_PREEMPT
130 if (!(torture_random(trsp) % (nrealwriters_stress * 20000)))
131 preempt_schedule(); /* Allow test to be preempted. */
135 static void torture_lock_busted_write_unlock(void)
137 /* BUGGY, do not use in real life!!! */
140 static struct lock_torture_ops lock_busted_ops = {
141 .writelock = torture_lock_busted_write_lock,
142 .write_delay = torture_lock_busted_write_delay,
143 .writeunlock = torture_lock_busted_write_unlock,
144 .name = "lock_busted"
147 static DEFINE_SPINLOCK(torture_spinlock);
149 static int torture_spin_lock_write_lock(void) __acquires(torture_spinlock)
151 spin_lock(&torture_spinlock);
155 static void torture_spin_lock_write_delay(struct torture_random_state *trsp)
157 const unsigned long shortdelay_us = 2;
158 const unsigned long longdelay_us = 100;
160 /* We want a short delay mostly to emulate likely code, and
161 * we want a long delay occasionally to force massive contention.
163 if (!(torture_random(trsp) %
164 (nrealwriters_stress * 2000 * longdelay_us)))
165 mdelay(longdelay_us);
166 if (!(torture_random(trsp) %
167 (nrealwriters_stress * 2 * shortdelay_us)))
168 udelay(shortdelay_us);
169 #ifdef CONFIG_PREEMPT
170 if (!(torture_random(trsp) % (nrealwriters_stress * 20000)))
171 preempt_schedule(); /* Allow test to be preempted. */
175 static void torture_spin_lock_write_unlock(void) __releases(torture_spinlock)
177 spin_unlock(&torture_spinlock);
180 static struct lock_torture_ops spin_lock_ops = {
181 .writelock = torture_spin_lock_write_lock,
182 .write_delay = torture_spin_lock_write_delay,
183 .writeunlock = torture_spin_lock_write_unlock,
187 static int torture_spin_lock_write_lock_irq(void)
188 __acquires(torture_spinlock_irq)
192 spin_lock_irqsave(&torture_spinlock, flags);
193 cur_ops->flags = flags;
197 static void torture_lock_spin_write_unlock_irq(void)
198 __releases(torture_spinlock)
200 spin_unlock_irqrestore(&torture_spinlock, cur_ops->flags);
203 static struct lock_torture_ops spin_lock_irq_ops = {
204 .writelock = torture_spin_lock_write_lock_irq,
205 .write_delay = torture_spin_lock_write_delay,
206 .writeunlock = torture_lock_spin_write_unlock_irq,
207 .name = "spin_lock_irq"
210 static DEFINE_MUTEX(torture_mutex);
212 static int torture_mutex_lock(void) __acquires(torture_mutex)
214 mutex_lock(&torture_mutex);
218 static void torture_mutex_delay(struct torture_random_state *trsp)
220 const unsigned long longdelay_ms = 100;
222 /* We want a long delay occasionally to force massive contention. */
223 if (!(torture_random(trsp) %
224 (nrealwriters_stress * 2000 * longdelay_ms)))
225 mdelay(longdelay_ms * 5);
227 mdelay(longdelay_ms / 5);
228 #ifdef CONFIG_PREEMPT
229 if (!(torture_random(trsp) % (nrealwriters_stress * 20000)))
230 preempt_schedule(); /* Allow test to be preempted. */
234 static void torture_mutex_unlock(void) __releases(torture_mutex)
236 mutex_unlock(&torture_mutex);
239 static struct lock_torture_ops mutex_lock_ops = {
240 .writelock = torture_mutex_lock,
241 .write_delay = torture_mutex_delay,
242 .writeunlock = torture_mutex_unlock,
247 * Lock torture writer kthread. Repeatedly acquires and releases
248 * the lock, checking for duplicate acquisitions.
250 static int lock_torture_writer(void *arg)
252 struct lock_writer_stress_stats *lwsp = arg;
253 static DEFINE_TORTURE_RANDOM(rand);
255 VERBOSE_TOROUT_STRING("lock_torture_writer task started");
256 set_user_nice(current, MAX_NICE);
259 if ((torture_random(&rand) & 0xfffff) == 0)
260 schedule_timeout_uninterruptible(1);
261 cur_ops->writelock();
262 if (WARN_ON_ONCE(lock_is_write_held))
263 lwsp->n_write_lock_fail++;
264 lock_is_write_held = 1;
265 lwsp->n_write_lock_acquired++;
266 cur_ops->write_delay(&rand);
267 lock_is_write_held = 0;
268 cur_ops->writeunlock();
269 stutter_wait("lock_torture_writer");
270 } while (!torture_must_stop());
271 torture_kthread_stopping("lock_torture_writer");
276 * Create an lock-torture-statistics message in the specified buffer.
278 static void lock_torture_printk(char *page)
283 long min = lwsa[0].n_write_lock_acquired;
286 for (i = 0; i < nrealwriters_stress; i++) {
287 if (lwsa[i].n_write_lock_fail)
289 sum += lwsa[i].n_write_lock_acquired;
290 if (max < lwsa[i].n_write_lock_fail)
291 max = lwsa[i].n_write_lock_fail;
292 if (min > lwsa[i].n_write_lock_fail)
293 min = lwsa[i].n_write_lock_fail;
295 page += sprintf(page, "%s%s ", torture_type, TORTURE_FLAG);
296 page += sprintf(page,
297 "Writes: Total: %lld Max/Min: %ld/%ld %s Fail: %d %s\n",
298 sum, max, min, max / 2 > min ? "???" : "",
299 fail, fail ? "!!!" : "");
301 atomic_inc(&n_lock_torture_errors);
305 * Print torture statistics. Caller must ensure that there is only one
306 * call to this function at a given time!!! This is normally accomplished
307 * by relying on the module system to only have one copy of the module
308 * loaded, and then by giving the lock_torture_stats kthread full control
309 * (or the init/cleanup functions when lock_torture_stats thread is not
312 static void lock_torture_stats_print(void)
314 int size = nrealwriters_stress * 200 + 8192;
317 buf = kmalloc(size, GFP_KERNEL);
319 pr_err("lock_torture_stats_print: Out of memory, need: %d",
323 lock_torture_printk(buf);
329 * Periodically prints torture statistics, if periodic statistics printing
330 * was specified via the stat_interval module parameter.
332 * No need to worry about fullstop here, since this one doesn't reference
333 * volatile state or register callbacks.
335 static int lock_torture_stats(void *arg)
337 VERBOSE_TOROUT_STRING("lock_torture_stats task started");
339 schedule_timeout_interruptible(stat_interval * HZ);
340 lock_torture_stats_print();
341 torture_shutdown_absorb("lock_torture_stats");
342 } while (!torture_must_stop());
343 torture_kthread_stopping("lock_torture_stats");
348 lock_torture_print_module_parms(struct lock_torture_ops *cur_ops,
351 pr_alert("%s" TORTURE_FLAG
352 "--- %s: nwriters_stress=%d stat_interval=%d verbose=%d shuffle_interval=%d stutter=%d shutdown_secs=%d onoff_interval=%d onoff_holdoff=%d\n",
353 torture_type, tag, nrealwriters_stress, stat_interval, verbose,
354 shuffle_interval, stutter, shutdown_secs,
355 onoff_interval, onoff_holdoff);
358 static void lock_torture_cleanup(void)
362 if (torture_cleanup())
366 for (i = 0; i < nrealwriters_stress; i++)
367 torture_stop_kthread(lock_torture_writer,
373 torture_stop_kthread(lock_torture_stats, stats_task);
374 lock_torture_stats_print(); /* -After- the stats thread is stopped! */
376 if (atomic_read(&n_lock_torture_errors))
377 lock_torture_print_module_parms(cur_ops,
378 "End of test: FAILURE");
379 else if (torture_onoff_failures())
380 lock_torture_print_module_parms(cur_ops,
381 "End of test: LOCK_HOTPLUG");
383 lock_torture_print_module_parms(cur_ops,
384 "End of test: SUCCESS");
387 static int __init lock_torture_init(void)
391 static struct lock_torture_ops *torture_ops[] = {
392 &lock_busted_ops, &spin_lock_ops, &spin_lock_irq_ops, &mutex_lock_ops,
395 if (!torture_init_begin(torture_type, verbose, &torture_runnable))
398 /* Process args and tell the world that the torturer is on the job. */
399 for (i = 0; i < ARRAY_SIZE(torture_ops); i++) {
400 cur_ops = torture_ops[i];
401 if (strcmp(torture_type, cur_ops->name) == 0)
404 if (i == ARRAY_SIZE(torture_ops)) {
405 pr_alert("lock-torture: invalid torture type: \"%s\"\n",
407 pr_alert("lock-torture types:");
408 for (i = 0; i < ARRAY_SIZE(torture_ops); i++)
409 pr_alert(" %s", torture_ops[i]->name);
415 cur_ops->init(); /* no "goto unwind" prior to this point!!! */
417 if (nwriters_stress >= 0)
418 nrealwriters_stress = nwriters_stress;
420 nrealwriters_stress = 2 * num_online_cpus();
421 lock_torture_print_module_parms(cur_ops, "Start of test");
423 /* Initialize the statistics so that each run gets its own numbers. */
425 lock_is_write_held = 0;
426 lwsa = kmalloc(sizeof(*lwsa) * nrealwriters_stress, GFP_KERNEL);
428 VERBOSE_TOROUT_STRING("lwsa: Out of memory");
432 for (i = 0; i < nrealwriters_stress; i++) {
433 lwsa[i].n_write_lock_fail = 0;
434 lwsa[i].n_write_lock_acquired = 0;
437 /* Start up the kthreads. */
439 if (onoff_interval > 0) {
440 firsterr = torture_onoff_init(onoff_holdoff * HZ,
441 onoff_interval * HZ);
445 if (shuffle_interval > 0) {
446 firsterr = torture_shuffle_init(shuffle_interval);
450 if (shutdown_secs > 0) {
451 firsterr = torture_shutdown_init(shutdown_secs,
452 lock_torture_cleanup);
457 firsterr = torture_stutter_init(stutter);
462 writer_tasks = kzalloc(nrealwriters_stress * sizeof(writer_tasks[0]),
464 if (writer_tasks == NULL) {
465 VERBOSE_TOROUT_ERRSTRING("writer_tasks: Out of memory");
469 for (i = 0; i < nrealwriters_stress; i++) {
470 firsterr = torture_create_kthread(lock_torture_writer, &lwsa[i],
475 if (stat_interval > 0) {
476 firsterr = torture_create_kthread(lock_torture_stats, NULL,
486 lock_torture_cleanup();
490 module_init(lock_torture_init);
491 module_exit(lock_torture_cleanup);