blob: de703a769c1dae1cd61aff14906dd2844423e5c1 [file] [log] [blame]
Paul E. McKenney0af3fe12014-02-04 15:51:41 -08001/*
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 *
20 * Author: Paul E. McKenney <paulmck@us.ibm.com>
21 * Based on kernel/rcu/torture.c.
22 */
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>
Davidlohr Bueso42ddc752014-09-11 20:40:18 -070030#include <linux/mutex.h>
Paul E. McKenney0af3fe12014-02-04 15:51:41 -080031#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>
49
50MODULE_LICENSE("GPL");
51MODULE_AUTHOR("Paul E. McKenney <paulmck@us.ibm.com>");
52
53torture_param(int, nwriters_stress, -1,
54 "Number of write-locking stress-test threads");
55torture_param(int, onoff_holdoff, 0, "Time after boot before CPU hotplugs (s)");
56torture_param(int, onoff_interval, 0,
57 "Time between CPU hotplugs (s), 0=disable");
58torture_param(int, shuffle_interval, 3,
59 "Number of jiffies between shuffles, 0=disable");
60torture_param(int, shutdown_secs, 0, "Shutdown time (j), <= zero to disable.");
61torture_param(int, stat_interval, 60,
62 "Number of seconds between stats printk()s");
63torture_param(int, stutter, 5, "Number of jiffies to run/halt test, 0=disable");
64torture_param(bool, verbose, true,
65 "Enable verbose debugging printk()s");
66
Davidlohr Buesof095bfc2014-09-11 20:40:19 -070067static bool debug_lock = false;
Paul E. McKenney0af3fe12014-02-04 15:51:41 -080068static char *torture_type = "spin_lock";
69module_param(torture_type, charp, 0444);
70MODULE_PARM_DESC(torture_type,
Davidlohr Bueso42ddc752014-09-11 20:40:18 -070071 "Type of lock to torture (spin_lock, spin_lock_irq, mutex_lock, ...)");
Paul E. McKenney0af3fe12014-02-04 15:51:41 -080072
73static atomic_t n_lock_torture_errors;
74
75static struct task_struct *stats_task;
76static struct task_struct **writer_tasks;
77
78static int nrealwriters_stress;
79static bool lock_is_write_held;
80
Davidlohr Bueso1e6757a2014-09-11 20:40:20 -070081struct lock_stress_stats {
82 long n_lock_fail;
83 long n_lock_acquired;
Paul E. McKenney0af3fe12014-02-04 15:51:41 -080084};
Davidlohr Bueso1e6757a2014-09-11 20:40:20 -070085static struct lock_stress_stats *lwsa; /* writer statistics */
Paul E. McKenney0af3fe12014-02-04 15:51:41 -080086
Paul E. McKenneyd065eac2014-04-04 17:17:35 -070087#if defined(MODULE)
Paul E. McKenney0af3fe12014-02-04 15:51:41 -080088#define LOCKTORTURE_RUNNABLE_INIT 1
89#else
90#define LOCKTORTURE_RUNNABLE_INIT 0
91#endif
Davidlohr Bueso23a8e5c2014-09-11 20:40:16 -070092int torture_runnable = LOCKTORTURE_RUNNABLE_INIT;
93module_param(torture_runnable, int, 0444);
94MODULE_PARM_DESC(torture_runnable, "Start locktorture at module init");
Paul E. McKenney0af3fe12014-02-04 15:51:41 -080095
96/* Forward reference. */
97static void lock_torture_cleanup(void);
98
99/*
100 * Operations vector for selecting different types of tests.
101 */
102struct lock_torture_ops {
103 void (*init)(void);
104 int (*writelock)(void);
105 void (*write_delay)(struct torture_random_state *trsp);
106 void (*writeunlock)(void);
107 unsigned long flags;
108 const char *name;
109};
110
111static struct lock_torture_ops *cur_ops;
112
113/*
114 * Definitions for lock torture testing.
115 */
116
Paul E. McKenneye0864812014-02-11 08:05:07 -0800117static int torture_lock_busted_write_lock(void)
118{
119 return 0; /* BUGGY, do not use in real life!!! */
120}
121
122static void torture_lock_busted_write_delay(struct torture_random_state *trsp)
123{
124 const unsigned long longdelay_us = 100;
125
126 /* We want a long delay occasionally to force massive contention. */
127 if (!(torture_random(trsp) %
128 (nrealwriters_stress * 2000 * longdelay_us)))
129 mdelay(longdelay_us);
130#ifdef CONFIG_PREEMPT
131 if (!(torture_random(trsp) % (nrealwriters_stress * 20000)))
132 preempt_schedule(); /* Allow test to be preempted. */
133#endif
134}
135
136static void torture_lock_busted_write_unlock(void)
137{
138 /* BUGGY, do not use in real life!!! */
139}
140
141static 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,
145 .name = "lock_busted"
146};
147
Paul E. McKenney0af3fe12014-02-04 15:51:41 -0800148static DEFINE_SPINLOCK(torture_spinlock);
149
150static int torture_spin_lock_write_lock(void) __acquires(torture_spinlock)
151{
152 spin_lock(&torture_spinlock);
153 return 0;
154}
155
156static void torture_spin_lock_write_delay(struct torture_random_state *trsp)
157{
158 const unsigned long shortdelay_us = 2;
159 const unsigned long longdelay_us = 100;
160
161 /* We want a short delay mostly to emulate likely code, and
162 * we want a long delay occasionally to force massive contention.
163 */
164 if (!(torture_random(trsp) %
165 (nrealwriters_stress * 2000 * longdelay_us)))
166 mdelay(longdelay_us);
167 if (!(torture_random(trsp) %
168 (nrealwriters_stress * 2 * shortdelay_us)))
169 udelay(shortdelay_us);
170#ifdef CONFIG_PREEMPT
171 if (!(torture_random(trsp) % (nrealwriters_stress * 20000)))
172 preempt_schedule(); /* Allow test to be preempted. */
173#endif
174}
175
176static void torture_spin_lock_write_unlock(void) __releases(torture_spinlock)
177{
178 spin_unlock(&torture_spinlock);
179}
180
181static struct lock_torture_ops spin_lock_ops = {
182 .writelock = torture_spin_lock_write_lock,
183 .write_delay = torture_spin_lock_write_delay,
184 .writeunlock = torture_spin_lock_write_unlock,
185 .name = "spin_lock"
186};
187
188static int torture_spin_lock_write_lock_irq(void)
189__acquires(torture_spinlock_irq)
190{
191 unsigned long flags;
192
193 spin_lock_irqsave(&torture_spinlock, flags);
194 cur_ops->flags = flags;
195 return 0;
196}
197
198static void torture_lock_spin_write_unlock_irq(void)
199__releases(torture_spinlock)
200{
201 spin_unlock_irqrestore(&torture_spinlock, cur_ops->flags);
202}
203
204static struct lock_torture_ops spin_lock_irq_ops = {
205 .writelock = torture_spin_lock_write_lock_irq,
206 .write_delay = torture_spin_lock_write_delay,
207 .writeunlock = torture_lock_spin_write_unlock_irq,
208 .name = "spin_lock_irq"
209};
210
Davidlohr Bueso42ddc752014-09-11 20:40:18 -0700211static DEFINE_MUTEX(torture_mutex);
212
213static int torture_mutex_lock(void) __acquires(torture_mutex)
214{
215 mutex_lock(&torture_mutex);
216 return 0;
217}
218
219static void torture_mutex_delay(struct torture_random_state *trsp)
220{
221 const unsigned long longdelay_ms = 100;
222
223 /* We want a long delay occasionally to force massive contention. */
224 if (!(torture_random(trsp) %
225 (nrealwriters_stress * 2000 * longdelay_ms)))
226 mdelay(longdelay_ms * 5);
227 else
228 mdelay(longdelay_ms / 5);
229#ifdef CONFIG_PREEMPT
230 if (!(torture_random(trsp) % (nrealwriters_stress * 20000)))
231 preempt_schedule(); /* Allow test to be preempted. */
232#endif
233}
234
235static void torture_mutex_unlock(void) __releases(torture_mutex)
236{
237 mutex_unlock(&torture_mutex);
238}
239
240static struct lock_torture_ops mutex_lock_ops = {
241 .writelock = torture_mutex_lock,
242 .write_delay = torture_mutex_delay,
243 .writeunlock = torture_mutex_unlock,
244 .name = "mutex_lock"
245};
246
Paul E. McKenney0af3fe12014-02-04 15:51:41 -0800247/*
248 * Lock torture writer kthread. Repeatedly acquires and releases
249 * the lock, checking for duplicate acquisitions.
250 */
251static int lock_torture_writer(void *arg)
252{
Davidlohr Bueso1e6757a2014-09-11 20:40:20 -0700253 struct lock_stress_stats *lwsp = arg;
Paul E. McKenney0af3fe12014-02-04 15:51:41 -0800254 static DEFINE_TORTURE_RANDOM(rand);
255
256 VERBOSE_TOROUT_STRING("lock_torture_writer task started");
Dongsheng Yang8698a742014-03-11 18:09:12 +0800257 set_user_nice(current, MAX_NICE);
Paul E. McKenney0af3fe12014-02-04 15:51:41 -0800258
259 do {
Paul E. McKenneyda601c62014-02-26 12:14:51 -0800260 if ((torture_random(&rand) & 0xfffff) == 0)
261 schedule_timeout_uninterruptible(1);
Paul E. McKenney0af3fe12014-02-04 15:51:41 -0800262 cur_ops->writelock();
263 if (WARN_ON_ONCE(lock_is_write_held))
Davidlohr Bueso1e6757a2014-09-11 20:40:20 -0700264 lwsp->n_lock_fail++;
Paul E. McKenney0af3fe12014-02-04 15:51:41 -0800265 lock_is_write_held = 1;
Davidlohr Bueso1e6757a2014-09-11 20:40:20 -0700266 lwsp->n_lock_acquired++;
Paul E. McKenney0af3fe12014-02-04 15:51:41 -0800267 cur_ops->write_delay(&rand);
268 lock_is_write_held = 0;
269 cur_ops->writeunlock();
270 stutter_wait("lock_torture_writer");
271 } while (!torture_must_stop());
272 torture_kthread_stopping("lock_torture_writer");
273 return 0;
274}
275
276/*
277 * Create an lock-torture-statistics message in the specified buffer.
278 */
279static void lock_torture_printk(char *page)
280{
281 bool fail = 0;
282 int i;
283 long max = 0;
Davidlohr Bueso1e6757a2014-09-11 20:40:20 -0700284 long min = lwsa[0].n_lock_acquired;
Paul E. McKenney0af3fe12014-02-04 15:51:41 -0800285 long long sum = 0;
286
287 for (i = 0; i < nrealwriters_stress; i++) {
Davidlohr Bueso1e6757a2014-09-11 20:40:20 -0700288 if (lwsa[i].n_lock_fail)
Paul E. McKenney0af3fe12014-02-04 15:51:41 -0800289 fail = true;
Davidlohr Bueso1e6757a2014-09-11 20:40:20 -0700290 sum += lwsa[i].n_lock_acquired;
291 if (max < lwsa[i].n_lock_fail)
292 max = lwsa[i].n_lock_fail;
293 if (min > lwsa[i].n_lock_fail)
294 min = lwsa[i].n_lock_fail;
Paul E. McKenney0af3fe12014-02-04 15:51:41 -0800295 }
296 page += sprintf(page, "%s%s ", torture_type, TORTURE_FLAG);
297 page += sprintf(page,
298 "Writes: Total: %lld Max/Min: %ld/%ld %s Fail: %d %s\n",
299 sum, max, min, max / 2 > min ? "???" : "",
300 fail, fail ? "!!!" : "");
301 if (fail)
302 atomic_inc(&n_lock_torture_errors);
303}
304
305/*
306 * Print torture statistics. Caller must ensure that there is only one
307 * call to this function at a given time!!! This is normally accomplished
308 * by relying on the module system to only have one copy of the module
309 * loaded, and then by giving the lock_torture_stats kthread full control
310 * (or the init/cleanup functions when lock_torture_stats thread is not
311 * running).
312 */
313static void lock_torture_stats_print(void)
314{
315 int size = nrealwriters_stress * 200 + 8192;
316 char *buf;
317
318 buf = kmalloc(size, GFP_KERNEL);
319 if (!buf) {
320 pr_err("lock_torture_stats_print: Out of memory, need: %d",
321 size);
322 return;
323 }
324 lock_torture_printk(buf);
325 pr_alert("%s", buf);
326 kfree(buf);
327}
328
329/*
330 * Periodically prints torture statistics, if periodic statistics printing
331 * was specified via the stat_interval module parameter.
332 *
333 * No need to worry about fullstop here, since this one doesn't reference
334 * volatile state or register callbacks.
335 */
336static int lock_torture_stats(void *arg)
337{
338 VERBOSE_TOROUT_STRING("lock_torture_stats task started");
339 do {
340 schedule_timeout_interruptible(stat_interval * HZ);
341 lock_torture_stats_print();
342 torture_shutdown_absorb("lock_torture_stats");
343 } while (!torture_must_stop());
344 torture_kthread_stopping("lock_torture_stats");
345 return 0;
346}
347
348static inline void
349lock_torture_print_module_parms(struct lock_torture_ops *cur_ops,
350 const char *tag)
351{
352 pr_alert("%s" TORTURE_FLAG
Davidlohr Buesof095bfc2014-09-11 20:40:19 -0700353 "--- %s%s: nwriters_stress=%d stat_interval=%d verbose=%d shuffle_interval=%d stutter=%d shutdown_secs=%d onoff_interval=%d onoff_holdoff=%d\n",
354 torture_type, tag, debug_lock ? " [debug]": "",
355 nrealwriters_stress, stat_interval, verbose,
Paul E. McKenney0af3fe12014-02-04 15:51:41 -0800356 shuffle_interval, stutter, shutdown_secs,
357 onoff_interval, onoff_holdoff);
358}
359
360static void lock_torture_cleanup(void)
361{
362 int i;
363
364 if (torture_cleanup())
365 return;
366
367 if (writer_tasks) {
368 for (i = 0; i < nrealwriters_stress; i++)
369 torture_stop_kthread(lock_torture_writer,
370 writer_tasks[i]);
371 kfree(writer_tasks);
372 writer_tasks = NULL;
373 }
374
375 torture_stop_kthread(lock_torture_stats, stats_task);
376 lock_torture_stats_print(); /* -After- the stats thread is stopped! */
377
378 if (atomic_read(&n_lock_torture_errors))
379 lock_torture_print_module_parms(cur_ops,
380 "End of test: FAILURE");
381 else if (torture_onoff_failures())
382 lock_torture_print_module_parms(cur_ops,
383 "End of test: LOCK_HOTPLUG");
384 else
385 lock_torture_print_module_parms(cur_ops,
386 "End of test: SUCCESS");
387}
388
389static int __init lock_torture_init(void)
390{
391 int i;
392 int firsterr = 0;
393 static struct lock_torture_ops *torture_ops[] = {
Davidlohr Bueso42ddc752014-09-11 20:40:18 -0700394 &lock_busted_ops, &spin_lock_ops, &spin_lock_irq_ops, &mutex_lock_ops,
Paul E. McKenney0af3fe12014-02-04 15:51:41 -0800395 };
396
Davidlohr Bueso23a8e5c2014-09-11 20:40:16 -0700397 if (!torture_init_begin(torture_type, verbose, &torture_runnable))
Paul E. McKenney52280842014-04-07 09:14:11 -0700398 return -EBUSY;
Paul E. McKenney0af3fe12014-02-04 15:51:41 -0800399
400 /* Process args and tell the world that the torturer is on the job. */
401 for (i = 0; i < ARRAY_SIZE(torture_ops); i++) {
402 cur_ops = torture_ops[i];
403 if (strcmp(torture_type, cur_ops->name) == 0)
404 break;
405 }
406 if (i == ARRAY_SIZE(torture_ops)) {
407 pr_alert("lock-torture: invalid torture type: \"%s\"\n",
408 torture_type);
409 pr_alert("lock-torture types:");
410 for (i = 0; i < ARRAY_SIZE(torture_ops); i++)
411 pr_alert(" %s", torture_ops[i]->name);
412 pr_alert("\n");
413 torture_init_end();
414 return -EINVAL;
415 }
416 if (cur_ops->init)
417 cur_ops->init(); /* no "goto unwind" prior to this point!!! */
418
419 if (nwriters_stress >= 0)
420 nrealwriters_stress = nwriters_stress;
421 else
422 nrealwriters_stress = 2 * num_online_cpus();
Davidlohr Buesof095bfc2014-09-11 20:40:19 -0700423
424#ifdef CONFIG_DEBUG_MUTEXES
425 if (strncmp(torture_type, "mutex", 5) == 0)
426 debug_lock = true;
427#endif
428#ifdef CONFIG_DEBUG_SPINLOCK
429 if (strncmp(torture_type, "spin", 4) == 0)
430 debug_lock = true;
431#endif
Paul E. McKenney0af3fe12014-02-04 15:51:41 -0800432 lock_torture_print_module_parms(cur_ops, "Start of test");
433
434 /* Initialize the statistics so that each run gets its own numbers. */
435
436 lock_is_write_held = 0;
437 lwsa = kmalloc(sizeof(*lwsa) * nrealwriters_stress, GFP_KERNEL);
438 if (lwsa == NULL) {
439 VERBOSE_TOROUT_STRING("lwsa: Out of memory");
440 firsterr = -ENOMEM;
441 goto unwind;
442 }
443 for (i = 0; i < nrealwriters_stress; i++) {
Davidlohr Bueso1e6757a2014-09-11 20:40:20 -0700444 lwsa[i].n_lock_fail = 0;
445 lwsa[i].n_lock_acquired = 0;
Paul E. McKenney0af3fe12014-02-04 15:51:41 -0800446 }
447
448 /* Start up the kthreads. */
449
450 if (onoff_interval > 0) {
451 firsterr = torture_onoff_init(onoff_holdoff * HZ,
452 onoff_interval * HZ);
453 if (firsterr)
454 goto unwind;
455 }
456 if (shuffle_interval > 0) {
457 firsterr = torture_shuffle_init(shuffle_interval);
458 if (firsterr)
459 goto unwind;
460 }
461 if (shutdown_secs > 0) {
462 firsterr = torture_shutdown_init(shutdown_secs,
463 lock_torture_cleanup);
464 if (firsterr)
465 goto unwind;
466 }
467 if (stutter > 0) {
468 firsterr = torture_stutter_init(stutter);
469 if (firsterr)
470 goto unwind;
471 }
472
473 writer_tasks = kzalloc(nrealwriters_stress * sizeof(writer_tasks[0]),
474 GFP_KERNEL);
475 if (writer_tasks == NULL) {
476 VERBOSE_TOROUT_ERRSTRING("writer_tasks: Out of memory");
477 firsterr = -ENOMEM;
478 goto unwind;
479 }
480 for (i = 0; i < nrealwriters_stress; i++) {
481 firsterr = torture_create_kthread(lock_torture_writer, &lwsa[i],
482 writer_tasks[i]);
483 if (firsterr)
484 goto unwind;
485 }
486 if (stat_interval > 0) {
487 firsterr = torture_create_kthread(lock_torture_stats, NULL,
488 stats_task);
489 if (firsterr)
490 goto unwind;
491 }
492 torture_init_end();
493 return 0;
494
495unwind:
496 torture_init_end();
497 lock_torture_cleanup();
498 return firsterr;
499}
500
501module_init(lock_torture_init);
502module_exit(lock_torture_cleanup);