blob: 32244186f1f2ae0e7a6343ad084f416aa0cda055 [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 */
Paul E. McKenney0af3fe12014-02-04 15:51:41 -080023#include <linux/kernel.h>
Paul E. McKenney0af3fe12014-02-04 15:51:41 -080024#include <linux/module.h>
25#include <linux/kthread.h>
Paul E. McKenney0af3fe12014-02-04 15:51:41 -080026#include <linux/spinlock.h>
Davidlohr Buesoe34191f2014-09-29 06:14:23 -070027#include <linux/rwlock.h>
Davidlohr Bueso42ddc752014-09-11 20:40:18 -070028#include <linux/mutex.h>
Davidlohr Buesoc98fed92014-09-29 06:14:26 -070029#include <linux/rwsem.h>
Paul E. McKenney0af3fe12014-02-04 15:51:41 -080030#include <linux/smp.h>
31#include <linux/interrupt.h>
32#include <linux/sched.h>
33#include <linux/atomic.h>
Paul E. McKenney0af3fe12014-02-04 15:51:41 -080034#include <linux/moduleparam.h>
Paul E. McKenney0af3fe12014-02-04 15:51:41 -080035#include <linux/delay.h>
Paul E. McKenney0af3fe12014-02-04 15:51:41 -080036#include <linux/slab.h>
Paul E. McKenney0af3fe12014-02-04 15:51:41 -080037#include <linux/torture.h>
38
39MODULE_LICENSE("GPL");
40MODULE_AUTHOR("Paul E. McKenney <paulmck@us.ibm.com>");
41
42torture_param(int, nwriters_stress, -1,
43 "Number of write-locking stress-test threads");
Davidlohr Bueso4f6332c2014-09-11 21:40:41 -070044torture_param(int, nreaders_stress, -1,
45 "Number of read-locking stress-test threads");
Paul E. McKenney0af3fe12014-02-04 15:51:41 -080046torture_param(int, onoff_holdoff, 0, "Time after boot before CPU hotplugs (s)");
47torture_param(int, onoff_interval, 0,
48 "Time between CPU hotplugs (s), 0=disable");
49torture_param(int, shuffle_interval, 3,
50 "Number of jiffies between shuffles, 0=disable");
51torture_param(int, shutdown_secs, 0, "Shutdown time (j), <= zero to disable.");
52torture_param(int, stat_interval, 60,
53 "Number of seconds between stats printk()s");
54torture_param(int, stutter, 5, "Number of jiffies to run/halt test, 0=disable");
55torture_param(bool, verbose, true,
56 "Enable verbose debugging printk()s");
57
58static char *torture_type = "spin_lock";
59module_param(torture_type, charp, 0444);
60MODULE_PARM_DESC(torture_type,
Davidlohr Bueso42ddc752014-09-11 20:40:18 -070061 "Type of lock to torture (spin_lock, spin_lock_irq, mutex_lock, ...)");
Paul E. McKenney0af3fe12014-02-04 15:51:41 -080062
Paul E. McKenney0af3fe12014-02-04 15:51:41 -080063static struct task_struct *stats_task;
64static struct task_struct **writer_tasks;
Davidlohr Bueso4f6332c2014-09-11 21:40:41 -070065static struct task_struct **reader_tasks;
Paul E. McKenney0af3fe12014-02-04 15:51:41 -080066
Paul E. McKenney0af3fe12014-02-04 15:51:41 -080067static bool lock_is_write_held;
Davidlohr Bueso4f6332c2014-09-11 21:40:41 -070068static bool lock_is_read_held;
Paul E. McKenney0af3fe12014-02-04 15:51:41 -080069
Davidlohr Bueso1e6757a2014-09-11 20:40:20 -070070struct lock_stress_stats {
71 long n_lock_fail;
72 long n_lock_acquired;
Paul E. McKenney0af3fe12014-02-04 15:51:41 -080073};
Paul E. McKenney0af3fe12014-02-04 15:51:41 -080074
Paul E. McKenneyd065eac2014-04-04 17:17:35 -070075#if defined(MODULE)
Paul E. McKenney0af3fe12014-02-04 15:51:41 -080076#define LOCKTORTURE_RUNNABLE_INIT 1
77#else
78#define LOCKTORTURE_RUNNABLE_INIT 0
79#endif
Davidlohr Bueso23a8e5c2014-09-11 20:40:16 -070080int torture_runnable = LOCKTORTURE_RUNNABLE_INIT;
81module_param(torture_runnable, int, 0444);
82MODULE_PARM_DESC(torture_runnable, "Start locktorture at module init");
Paul E. McKenney0af3fe12014-02-04 15:51:41 -080083
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);
94 void (*writeunlock)(void);
Davidlohr Bueso4f6332c2014-09-11 21:40:41 -070095 int (*readlock)(void);
96 void (*read_delay)(struct torture_random_state *trsp);
97 void (*readunlock)(void);
Paul E. McKenney0af3fe12014-02-04 15:51:41 -080098 unsigned long flags;
99 const char *name;
100};
101
Davidlohr Bueso630952c2014-09-11 21:42:25 -0700102struct lock_torture_cxt {
103 int nrealwriters_stress;
104 int nrealreaders_stress;
105 bool debug_lock;
106 atomic_t n_lock_torture_errors;
107 struct lock_torture_ops *cur_ops;
108 struct lock_stress_stats *lwsa; /* writer statistics */
109 struct lock_stress_stats *lrsa; /* reader statistics */
110};
111static struct lock_torture_cxt cxt = { 0, 0, false,
112 ATOMIC_INIT(0),
113 NULL, NULL};
Paul E. McKenney0af3fe12014-02-04 15:51:41 -0800114/*
115 * Definitions for lock torture testing.
116 */
117
Paul E. McKenneye0864812014-02-11 08:05:07 -0800118static int torture_lock_busted_write_lock(void)
119{
120 return 0; /* BUGGY, do not use in real life!!! */
121}
122
123static void torture_lock_busted_write_delay(struct torture_random_state *trsp)
124{
Paul E. McKenney61d49d22015-04-01 08:42:27 -0700125 const unsigned long longdelay_ms = 100;
Paul E. McKenneye0864812014-02-11 08:05:07 -0800126
127 /* We want a long delay occasionally to force massive contention. */
128 if (!(torture_random(trsp) %
Paul E. McKenney61d49d22015-04-01 08:42:27 -0700129 (cxt.nrealwriters_stress * 2000 * longdelay_ms)))
130 mdelay(longdelay_ms);
Paul E. McKenneye0864812014-02-11 08:05:07 -0800131#ifdef CONFIG_PREEMPT
Davidlohr Bueso630952c2014-09-11 21:42:25 -0700132 if (!(torture_random(trsp) % (cxt.nrealwriters_stress * 20000)))
Paul E. McKenneye0864812014-02-11 08:05:07 -0800133 preempt_schedule(); /* Allow test to be preempted. */
134#endif
135}
136
137static void torture_lock_busted_write_unlock(void)
138{
139 /* BUGGY, do not use in real life!!! */
140}
141
142static struct lock_torture_ops lock_busted_ops = {
143 .writelock = torture_lock_busted_write_lock,
144 .write_delay = torture_lock_busted_write_delay,
145 .writeunlock = torture_lock_busted_write_unlock,
Davidlohr Bueso4f6332c2014-09-11 21:40:41 -0700146 .readlock = NULL,
147 .read_delay = NULL,
148 .readunlock = NULL,
Paul E. McKenneye0864812014-02-11 08:05:07 -0800149 .name = "lock_busted"
150};
151
Paul E. McKenney0af3fe12014-02-04 15:51:41 -0800152static DEFINE_SPINLOCK(torture_spinlock);
153
154static int torture_spin_lock_write_lock(void) __acquires(torture_spinlock)
155{
156 spin_lock(&torture_spinlock);
157 return 0;
158}
159
160static void torture_spin_lock_write_delay(struct torture_random_state *trsp)
161{
162 const unsigned long shortdelay_us = 2;
Paul E. McKenney61d49d22015-04-01 08:42:27 -0700163 const unsigned long longdelay_ms = 100;
Paul E. McKenney0af3fe12014-02-04 15:51:41 -0800164
165 /* We want a short delay mostly to emulate likely code, and
166 * we want a long delay occasionally to force massive contention.
167 */
168 if (!(torture_random(trsp) %
Paul E. McKenney61d49d22015-04-01 08:42:27 -0700169 (cxt.nrealwriters_stress * 2000 * longdelay_ms)))
170 mdelay(longdelay_ms);
Paul E. McKenney0af3fe12014-02-04 15:51:41 -0800171 if (!(torture_random(trsp) %
Davidlohr Bueso630952c2014-09-11 21:42:25 -0700172 (cxt.nrealwriters_stress * 2 * shortdelay_us)))
Paul E. McKenney0af3fe12014-02-04 15:51:41 -0800173 udelay(shortdelay_us);
174#ifdef CONFIG_PREEMPT
Davidlohr Bueso630952c2014-09-11 21:42:25 -0700175 if (!(torture_random(trsp) % (cxt.nrealwriters_stress * 20000)))
Paul E. McKenney0af3fe12014-02-04 15:51:41 -0800176 preempt_schedule(); /* Allow test to be preempted. */
177#endif
178}
179
180static void torture_spin_lock_write_unlock(void) __releases(torture_spinlock)
181{
182 spin_unlock(&torture_spinlock);
183}
184
185static struct lock_torture_ops spin_lock_ops = {
186 .writelock = torture_spin_lock_write_lock,
187 .write_delay = torture_spin_lock_write_delay,
188 .writeunlock = torture_spin_lock_write_unlock,
Davidlohr Bueso4f6332c2014-09-11 21:40:41 -0700189 .readlock = NULL,
190 .read_delay = NULL,
191 .readunlock = NULL,
Paul E. McKenney0af3fe12014-02-04 15:51:41 -0800192 .name = "spin_lock"
193};
194
195static int torture_spin_lock_write_lock_irq(void)
Davidlohr Bueso219f8002014-09-29 06:14:24 -0700196__acquires(torture_spinlock)
Paul E. McKenney0af3fe12014-02-04 15:51:41 -0800197{
198 unsigned long flags;
199
200 spin_lock_irqsave(&torture_spinlock, flags);
Davidlohr Bueso630952c2014-09-11 21:42:25 -0700201 cxt.cur_ops->flags = flags;
Paul E. McKenney0af3fe12014-02-04 15:51:41 -0800202 return 0;
203}
204
205static void torture_lock_spin_write_unlock_irq(void)
206__releases(torture_spinlock)
207{
Davidlohr Bueso630952c2014-09-11 21:42:25 -0700208 spin_unlock_irqrestore(&torture_spinlock, cxt.cur_ops->flags);
Paul E. McKenney0af3fe12014-02-04 15:51:41 -0800209}
210
211static struct lock_torture_ops spin_lock_irq_ops = {
212 .writelock = torture_spin_lock_write_lock_irq,
213 .write_delay = torture_spin_lock_write_delay,
214 .writeunlock = torture_lock_spin_write_unlock_irq,
Davidlohr Bueso4f6332c2014-09-11 21:40:41 -0700215 .readlock = NULL,
216 .read_delay = NULL,
217 .readunlock = NULL,
Paul E. McKenney0af3fe12014-02-04 15:51:41 -0800218 .name = "spin_lock_irq"
219};
220
Davidlohr Buesoe34191f2014-09-29 06:14:23 -0700221static DEFINE_RWLOCK(torture_rwlock);
222
223static int torture_rwlock_write_lock(void) __acquires(torture_rwlock)
224{
225 write_lock(&torture_rwlock);
226 return 0;
227}
228
229static void torture_rwlock_write_delay(struct torture_random_state *trsp)
230{
231 const unsigned long shortdelay_us = 2;
232 const unsigned long longdelay_ms = 100;
233
234 /* We want a short delay mostly to emulate likely code, and
235 * we want a long delay occasionally to force massive contention.
236 */
237 if (!(torture_random(trsp) %
238 (cxt.nrealwriters_stress * 2000 * longdelay_ms)))
239 mdelay(longdelay_ms);
240 else
241 udelay(shortdelay_us);
242}
243
244static void torture_rwlock_write_unlock(void) __releases(torture_rwlock)
245{
246 write_unlock(&torture_rwlock);
247}
248
249static int torture_rwlock_read_lock(void) __acquires(torture_rwlock)
250{
251 read_lock(&torture_rwlock);
252 return 0;
253}
254
255static void torture_rwlock_read_delay(struct torture_random_state *trsp)
256{
257 const unsigned long shortdelay_us = 10;
258 const unsigned long longdelay_ms = 100;
259
260 /* We want a short delay mostly to emulate likely code, and
261 * we want a long delay occasionally to force massive contention.
262 */
263 if (!(torture_random(trsp) %
264 (cxt.nrealreaders_stress * 2000 * longdelay_ms)))
265 mdelay(longdelay_ms);
266 else
267 udelay(shortdelay_us);
268}
269
270static void torture_rwlock_read_unlock(void) __releases(torture_rwlock)
271{
272 read_unlock(&torture_rwlock);
273}
274
275static struct lock_torture_ops rw_lock_ops = {
276 .writelock = torture_rwlock_write_lock,
277 .write_delay = torture_rwlock_write_delay,
278 .writeunlock = torture_rwlock_write_unlock,
279 .readlock = torture_rwlock_read_lock,
280 .read_delay = torture_rwlock_read_delay,
281 .readunlock = torture_rwlock_read_unlock,
282 .name = "rw_lock"
283};
284
285static int torture_rwlock_write_lock_irq(void) __acquires(torture_rwlock)
286{
287 unsigned long flags;
288
289 write_lock_irqsave(&torture_rwlock, flags);
290 cxt.cur_ops->flags = flags;
291 return 0;
292}
293
294static void torture_rwlock_write_unlock_irq(void)
295__releases(torture_rwlock)
296{
297 write_unlock_irqrestore(&torture_rwlock, cxt.cur_ops->flags);
298}
299
300static int torture_rwlock_read_lock_irq(void) __acquires(torture_rwlock)
301{
302 unsigned long flags;
303
304 read_lock_irqsave(&torture_rwlock, flags);
305 cxt.cur_ops->flags = flags;
306 return 0;
307}
308
309static void torture_rwlock_read_unlock_irq(void)
310__releases(torture_rwlock)
311{
Alexey Kodanevf548d992015-03-07 03:06:53 +0300312 read_unlock_irqrestore(&torture_rwlock, cxt.cur_ops->flags);
Davidlohr Buesoe34191f2014-09-29 06:14:23 -0700313}
314
315static struct lock_torture_ops rw_lock_irq_ops = {
316 .writelock = torture_rwlock_write_lock_irq,
317 .write_delay = torture_rwlock_write_delay,
318 .writeunlock = torture_rwlock_write_unlock_irq,
319 .readlock = torture_rwlock_read_lock_irq,
320 .read_delay = torture_rwlock_read_delay,
321 .readunlock = torture_rwlock_read_unlock_irq,
322 .name = "rw_lock_irq"
323};
324
Davidlohr Bueso42ddc752014-09-11 20:40:18 -0700325static DEFINE_MUTEX(torture_mutex);
326
327static int torture_mutex_lock(void) __acquires(torture_mutex)
328{
329 mutex_lock(&torture_mutex);
330 return 0;
331}
332
333static void torture_mutex_delay(struct torture_random_state *trsp)
334{
335 const unsigned long longdelay_ms = 100;
336
337 /* We want a long delay occasionally to force massive contention. */
338 if (!(torture_random(trsp) %
Davidlohr Bueso630952c2014-09-11 21:42:25 -0700339 (cxt.nrealwriters_stress * 2000 * longdelay_ms)))
Davidlohr Bueso42ddc752014-09-11 20:40:18 -0700340 mdelay(longdelay_ms * 5);
341 else
342 mdelay(longdelay_ms / 5);
343#ifdef CONFIG_PREEMPT
Davidlohr Bueso630952c2014-09-11 21:42:25 -0700344 if (!(torture_random(trsp) % (cxt.nrealwriters_stress * 20000)))
Davidlohr Bueso42ddc752014-09-11 20:40:18 -0700345 preempt_schedule(); /* Allow test to be preempted. */
346#endif
347}
348
349static void torture_mutex_unlock(void) __releases(torture_mutex)
350{
351 mutex_unlock(&torture_mutex);
352}
353
354static struct lock_torture_ops mutex_lock_ops = {
355 .writelock = torture_mutex_lock,
356 .write_delay = torture_mutex_delay,
357 .writeunlock = torture_mutex_unlock,
Davidlohr Bueso4f6332c2014-09-11 21:40:41 -0700358 .readlock = NULL,
359 .read_delay = NULL,
360 .readunlock = NULL,
Davidlohr Bueso42ddc752014-09-11 20:40:18 -0700361 .name = "mutex_lock"
362};
363
Davidlohr Bueso4a3b4272014-09-11 21:41:30 -0700364static DECLARE_RWSEM(torture_rwsem);
365static int torture_rwsem_down_write(void) __acquires(torture_rwsem)
366{
367 down_write(&torture_rwsem);
368 return 0;
369}
370
371static void torture_rwsem_write_delay(struct torture_random_state *trsp)
372{
373 const unsigned long longdelay_ms = 100;
374
375 /* We want a long delay occasionally to force massive contention. */
376 if (!(torture_random(trsp) %
Davidlohr Bueso630952c2014-09-11 21:42:25 -0700377 (cxt.nrealwriters_stress * 2000 * longdelay_ms)))
Davidlohr Bueso4a3b4272014-09-11 21:41:30 -0700378 mdelay(longdelay_ms * 10);
379 else
380 mdelay(longdelay_ms / 10);
381#ifdef CONFIG_PREEMPT
Davidlohr Bueso630952c2014-09-11 21:42:25 -0700382 if (!(torture_random(trsp) % (cxt.nrealwriters_stress * 20000)))
Davidlohr Bueso4a3b4272014-09-11 21:41:30 -0700383 preempt_schedule(); /* Allow test to be preempted. */
384#endif
385}
386
387static void torture_rwsem_up_write(void) __releases(torture_rwsem)
388{
389 up_write(&torture_rwsem);
390}
391
392static int torture_rwsem_down_read(void) __acquires(torture_rwsem)
393{
394 down_read(&torture_rwsem);
395 return 0;
396}
397
398static void torture_rwsem_read_delay(struct torture_random_state *trsp)
399{
400 const unsigned long longdelay_ms = 100;
401
402 /* We want a long delay occasionally to force massive contention. */
403 if (!(torture_random(trsp) %
Davidlohr Bueso630952c2014-09-11 21:42:25 -0700404 (cxt.nrealwriters_stress * 2000 * longdelay_ms)))
Davidlohr Bueso4a3b4272014-09-11 21:41:30 -0700405 mdelay(longdelay_ms * 2);
406 else
407 mdelay(longdelay_ms / 2);
408#ifdef CONFIG_PREEMPT
Davidlohr Bueso630952c2014-09-11 21:42:25 -0700409 if (!(torture_random(trsp) % (cxt.nrealreaders_stress * 20000)))
Davidlohr Bueso4a3b4272014-09-11 21:41:30 -0700410 preempt_schedule(); /* Allow test to be preempted. */
411#endif
412}
413
414static void torture_rwsem_up_read(void) __releases(torture_rwsem)
415{
416 up_read(&torture_rwsem);
417}
418
419static struct lock_torture_ops rwsem_lock_ops = {
420 .writelock = torture_rwsem_down_write,
421 .write_delay = torture_rwsem_write_delay,
422 .writeunlock = torture_rwsem_up_write,
423 .readlock = torture_rwsem_down_read,
424 .read_delay = torture_rwsem_read_delay,
425 .readunlock = torture_rwsem_up_read,
426 .name = "rwsem_lock"
427};
428
Paul E. McKenney0af3fe12014-02-04 15:51:41 -0800429/*
430 * Lock torture writer kthread. Repeatedly acquires and releases
431 * the lock, checking for duplicate acquisitions.
432 */
433static int lock_torture_writer(void *arg)
434{
Davidlohr Bueso1e6757a2014-09-11 20:40:20 -0700435 struct lock_stress_stats *lwsp = arg;
Paul E. McKenney0af3fe12014-02-04 15:51:41 -0800436 static DEFINE_TORTURE_RANDOM(rand);
437
438 VERBOSE_TOROUT_STRING("lock_torture_writer task started");
Dongsheng Yang8698a742014-03-11 18:09:12 +0800439 set_user_nice(current, MAX_NICE);
Paul E. McKenney0af3fe12014-02-04 15:51:41 -0800440
441 do {
Paul E. McKenneyda601c62014-02-26 12:14:51 -0800442 if ((torture_random(&rand) & 0xfffff) == 0)
443 schedule_timeout_uninterruptible(1);
Davidlohr Buesoa1229492014-09-29 06:14:25 -0700444
Davidlohr Bueso630952c2014-09-11 21:42:25 -0700445 cxt.cur_ops->writelock();
Paul E. McKenney0af3fe12014-02-04 15:51:41 -0800446 if (WARN_ON_ONCE(lock_is_write_held))
Davidlohr Bueso1e6757a2014-09-11 20:40:20 -0700447 lwsp->n_lock_fail++;
Paul E. McKenney0af3fe12014-02-04 15:51:41 -0800448 lock_is_write_held = 1;
Davidlohr Buesoa1229492014-09-29 06:14:25 -0700449 if (WARN_ON_ONCE(lock_is_read_held))
450 lwsp->n_lock_fail++; /* rare, but... */
451
Davidlohr Bueso1e6757a2014-09-11 20:40:20 -0700452 lwsp->n_lock_acquired++;
Davidlohr Bueso630952c2014-09-11 21:42:25 -0700453 cxt.cur_ops->write_delay(&rand);
Paul E. McKenney0af3fe12014-02-04 15:51:41 -0800454 lock_is_write_held = 0;
Davidlohr Bueso630952c2014-09-11 21:42:25 -0700455 cxt.cur_ops->writeunlock();
Davidlohr Buesoa1229492014-09-29 06:14:25 -0700456
Paul E. McKenney0af3fe12014-02-04 15:51:41 -0800457 stutter_wait("lock_torture_writer");
458 } while (!torture_must_stop());
459 torture_kthread_stopping("lock_torture_writer");
460 return 0;
461}
462
463/*
Davidlohr Bueso4f6332c2014-09-11 21:40:41 -0700464 * Lock torture reader kthread. Repeatedly acquires and releases
465 * the reader lock.
466 */
467static int lock_torture_reader(void *arg)
468{
469 struct lock_stress_stats *lrsp = arg;
470 static DEFINE_TORTURE_RANDOM(rand);
471
472 VERBOSE_TOROUT_STRING("lock_torture_reader task started");
473 set_user_nice(current, MAX_NICE);
474
475 do {
476 if ((torture_random(&rand) & 0xfffff) == 0)
477 schedule_timeout_uninterruptible(1);
Davidlohr Buesoa1229492014-09-29 06:14:25 -0700478
Davidlohr Bueso630952c2014-09-11 21:42:25 -0700479 cxt.cur_ops->readlock();
Davidlohr Bueso4f6332c2014-09-11 21:40:41 -0700480 lock_is_read_held = 1;
Davidlohr Buesoa1229492014-09-29 06:14:25 -0700481 if (WARN_ON_ONCE(lock_is_write_held))
482 lrsp->n_lock_fail++; /* rare, but... */
483
Davidlohr Bueso4f6332c2014-09-11 21:40:41 -0700484 lrsp->n_lock_acquired++;
Davidlohr Bueso630952c2014-09-11 21:42:25 -0700485 cxt.cur_ops->read_delay(&rand);
Davidlohr Bueso4f6332c2014-09-11 21:40:41 -0700486 lock_is_read_held = 0;
Davidlohr Bueso630952c2014-09-11 21:42:25 -0700487 cxt.cur_ops->readunlock();
Davidlohr Buesoa1229492014-09-29 06:14:25 -0700488
Davidlohr Bueso4f6332c2014-09-11 21:40:41 -0700489 stutter_wait("lock_torture_reader");
490 } while (!torture_must_stop());
491 torture_kthread_stopping("lock_torture_reader");
492 return 0;
493}
494
495/*
Paul E. McKenney0af3fe12014-02-04 15:51:41 -0800496 * Create an lock-torture-statistics message in the specified buffer.
497 */
Davidlohr Bueso4f6332c2014-09-11 21:40:41 -0700498static void __torture_print_stats(char *page,
499 struct lock_stress_stats *statp, bool write)
Paul E. McKenney0af3fe12014-02-04 15:51:41 -0800500{
501 bool fail = 0;
Davidlohr Bueso4f6332c2014-09-11 21:40:41 -0700502 int i, n_stress;
Paul E. McKenney0af3fe12014-02-04 15:51:41 -0800503 long max = 0;
Davidlohr Bueso4f6332c2014-09-11 21:40:41 -0700504 long min = statp[0].n_lock_acquired;
Paul E. McKenney0af3fe12014-02-04 15:51:41 -0800505 long long sum = 0;
506
Davidlohr Bueso630952c2014-09-11 21:42:25 -0700507 n_stress = write ? cxt.nrealwriters_stress : cxt.nrealreaders_stress;
Davidlohr Bueso4f6332c2014-09-11 21:40:41 -0700508 for (i = 0; i < n_stress; i++) {
509 if (statp[i].n_lock_fail)
Paul E. McKenney0af3fe12014-02-04 15:51:41 -0800510 fail = true;
Davidlohr Bueso4f6332c2014-09-11 21:40:41 -0700511 sum += statp[i].n_lock_acquired;
512 if (max < statp[i].n_lock_fail)
513 max = statp[i].n_lock_fail;
514 if (min > statp[i].n_lock_fail)
515 min = statp[i].n_lock_fail;
Paul E. McKenney0af3fe12014-02-04 15:51:41 -0800516 }
Paul E. McKenney0af3fe12014-02-04 15:51:41 -0800517 page += sprintf(page,
Davidlohr Bueso4f6332c2014-09-11 21:40:41 -0700518 "%s: Total: %lld Max/Min: %ld/%ld %s Fail: %d %s\n",
519 write ? "Writes" : "Reads ",
Paul E. McKenney0af3fe12014-02-04 15:51:41 -0800520 sum, max, min, max / 2 > min ? "???" : "",
521 fail, fail ? "!!!" : "");
522 if (fail)
Davidlohr Bueso630952c2014-09-11 21:42:25 -0700523 atomic_inc(&cxt.n_lock_torture_errors);
Paul E. McKenney0af3fe12014-02-04 15:51:41 -0800524}
525
526/*
527 * Print torture statistics. Caller must ensure that there is only one
528 * call to this function at a given time!!! This is normally accomplished
529 * by relying on the module system to only have one copy of the module
530 * loaded, and then by giving the lock_torture_stats kthread full control
531 * (or the init/cleanup functions when lock_torture_stats thread is not
532 * running).
533 */
534static void lock_torture_stats_print(void)
535{
Davidlohr Bueso630952c2014-09-11 21:42:25 -0700536 int size = cxt.nrealwriters_stress * 200 + 8192;
Paul E. McKenney0af3fe12014-02-04 15:51:41 -0800537 char *buf;
538
Davidlohr Bueso630952c2014-09-11 21:42:25 -0700539 if (cxt.cur_ops->readlock)
540 size += cxt.nrealreaders_stress * 200 + 8192;
Davidlohr Bueso4f6332c2014-09-11 21:40:41 -0700541
Paul E. McKenney0af3fe12014-02-04 15:51:41 -0800542 buf = kmalloc(size, GFP_KERNEL);
543 if (!buf) {
544 pr_err("lock_torture_stats_print: Out of memory, need: %d",
545 size);
546 return;
547 }
Davidlohr Bueso4f6332c2014-09-11 21:40:41 -0700548
Davidlohr Bueso630952c2014-09-11 21:42:25 -0700549 __torture_print_stats(buf, cxt.lwsa, true);
Paul E. McKenney0af3fe12014-02-04 15:51:41 -0800550 pr_alert("%s", buf);
551 kfree(buf);
Davidlohr Bueso4f6332c2014-09-11 21:40:41 -0700552
Davidlohr Bueso630952c2014-09-11 21:42:25 -0700553 if (cxt.cur_ops->readlock) {
Davidlohr Bueso4f6332c2014-09-11 21:40:41 -0700554 buf = kmalloc(size, GFP_KERNEL);
555 if (!buf) {
556 pr_err("lock_torture_stats_print: Out of memory, need: %d",
557 size);
558 return;
559 }
560
Davidlohr Bueso630952c2014-09-11 21:42:25 -0700561 __torture_print_stats(buf, cxt.lrsa, false);
Davidlohr Bueso4f6332c2014-09-11 21:40:41 -0700562 pr_alert("%s", buf);
563 kfree(buf);
564 }
Paul E. McKenney0af3fe12014-02-04 15:51:41 -0800565}
566
567/*
568 * Periodically prints torture statistics, if periodic statistics printing
569 * was specified via the stat_interval module parameter.
570 *
571 * No need to worry about fullstop here, since this one doesn't reference
572 * volatile state or register callbacks.
573 */
574static int lock_torture_stats(void *arg)
575{
576 VERBOSE_TOROUT_STRING("lock_torture_stats task started");
577 do {
578 schedule_timeout_interruptible(stat_interval * HZ);
579 lock_torture_stats_print();
580 torture_shutdown_absorb("lock_torture_stats");
581 } while (!torture_must_stop());
582 torture_kthread_stopping("lock_torture_stats");
583 return 0;
584}
585
586static inline void
587lock_torture_print_module_parms(struct lock_torture_ops *cur_ops,
588 const char *tag)
589{
590 pr_alert("%s" TORTURE_FLAG
Davidlohr Bueso4f6332c2014-09-11 21:40:41 -0700591 "--- %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",
Davidlohr Bueso630952c2014-09-11 21:42:25 -0700592 torture_type, tag, cxt.debug_lock ? " [debug]": "",
593 cxt.nrealwriters_stress, cxt.nrealreaders_stress, stat_interval,
Davidlohr Bueso4f6332c2014-09-11 21:40:41 -0700594 verbose, shuffle_interval, stutter, shutdown_secs,
Paul E. McKenney0af3fe12014-02-04 15:51:41 -0800595 onoff_interval, onoff_holdoff);
596}
597
598static void lock_torture_cleanup(void)
599{
600 int i;
601
Davidlohr Buesod36a7a02014-09-11 20:40:21 -0700602 if (torture_cleanup_begin())
Paul E. McKenney0af3fe12014-02-04 15:51:41 -0800603 return;
604
605 if (writer_tasks) {
Davidlohr Bueso630952c2014-09-11 21:42:25 -0700606 for (i = 0; i < cxt.nrealwriters_stress; i++)
Paul E. McKenney0af3fe12014-02-04 15:51:41 -0800607 torture_stop_kthread(lock_torture_writer,
608 writer_tasks[i]);
609 kfree(writer_tasks);
610 writer_tasks = NULL;
611 }
612
Davidlohr Bueso4f6332c2014-09-11 21:40:41 -0700613 if (reader_tasks) {
Davidlohr Bueso630952c2014-09-11 21:42:25 -0700614 for (i = 0; i < cxt.nrealreaders_stress; i++)
Davidlohr Bueso4f6332c2014-09-11 21:40:41 -0700615 torture_stop_kthread(lock_torture_reader,
616 reader_tasks[i]);
617 kfree(reader_tasks);
618 reader_tasks = NULL;
619 }
620
Paul E. McKenney0af3fe12014-02-04 15:51:41 -0800621 torture_stop_kthread(lock_torture_stats, stats_task);
622 lock_torture_stats_print(); /* -After- the stats thread is stopped! */
623
Davidlohr Bueso630952c2014-09-11 21:42:25 -0700624 if (atomic_read(&cxt.n_lock_torture_errors))
625 lock_torture_print_module_parms(cxt.cur_ops,
Paul E. McKenney0af3fe12014-02-04 15:51:41 -0800626 "End of test: FAILURE");
627 else if (torture_onoff_failures())
Davidlohr Bueso630952c2014-09-11 21:42:25 -0700628 lock_torture_print_module_parms(cxt.cur_ops,
Paul E. McKenney0af3fe12014-02-04 15:51:41 -0800629 "End of test: LOCK_HOTPLUG");
630 else
Davidlohr Bueso630952c2014-09-11 21:42:25 -0700631 lock_torture_print_module_parms(cxt.cur_ops,
Paul E. McKenney0af3fe12014-02-04 15:51:41 -0800632 "End of test: SUCCESS");
Davidlohr Buesod36a7a02014-09-11 20:40:21 -0700633 torture_cleanup_end();
Paul E. McKenney0af3fe12014-02-04 15:51:41 -0800634}
635
636static int __init lock_torture_init(void)
637{
Davidlohr Bueso4f6332c2014-09-11 21:40:41 -0700638 int i, j;
Paul E. McKenney0af3fe12014-02-04 15:51:41 -0800639 int firsterr = 0;
640 static struct lock_torture_ops *torture_ops[] = {
Davidlohr Buesoe34191f2014-09-29 06:14:23 -0700641 &lock_busted_ops,
642 &spin_lock_ops, &spin_lock_irq_ops,
643 &rw_lock_ops, &rw_lock_irq_ops,
644 &mutex_lock_ops,
645 &rwsem_lock_ops,
Paul E. McKenney0af3fe12014-02-04 15:51:41 -0800646 };
647
Davidlohr Bueso23a8e5c2014-09-11 20:40:16 -0700648 if (!torture_init_begin(torture_type, verbose, &torture_runnable))
Paul E. McKenney52280842014-04-07 09:14:11 -0700649 return -EBUSY;
Paul E. McKenney0af3fe12014-02-04 15:51:41 -0800650
651 /* Process args and tell the world that the torturer is on the job. */
652 for (i = 0; i < ARRAY_SIZE(torture_ops); i++) {
Davidlohr Bueso630952c2014-09-11 21:42:25 -0700653 cxt.cur_ops = torture_ops[i];
654 if (strcmp(torture_type, cxt.cur_ops->name) == 0)
Paul E. McKenney0af3fe12014-02-04 15:51:41 -0800655 break;
656 }
657 if (i == ARRAY_SIZE(torture_ops)) {
658 pr_alert("lock-torture: invalid torture type: \"%s\"\n",
659 torture_type);
660 pr_alert("lock-torture types:");
661 for (i = 0; i < ARRAY_SIZE(torture_ops); i++)
662 pr_alert(" %s", torture_ops[i]->name);
663 pr_alert("\n");
664 torture_init_end();
665 return -EINVAL;
666 }
Davidlohr Bueso630952c2014-09-11 21:42:25 -0700667 if (cxt.cur_ops->init)
668 cxt.cur_ops->init(); /* no "goto unwind" prior to this point!!! */
Paul E. McKenney0af3fe12014-02-04 15:51:41 -0800669
670 if (nwriters_stress >= 0)
Davidlohr Bueso630952c2014-09-11 21:42:25 -0700671 cxt.nrealwriters_stress = nwriters_stress;
Paul E. McKenney0af3fe12014-02-04 15:51:41 -0800672 else
Davidlohr Bueso630952c2014-09-11 21:42:25 -0700673 cxt.nrealwriters_stress = 2 * num_online_cpus();
Davidlohr Buesof095bfc2014-09-11 20:40:19 -0700674
675#ifdef CONFIG_DEBUG_MUTEXES
676 if (strncmp(torture_type, "mutex", 5) == 0)
Davidlohr Bueso630952c2014-09-11 21:42:25 -0700677 cxt.debug_lock = true;
Davidlohr Buesof095bfc2014-09-11 20:40:19 -0700678#endif
679#ifdef CONFIG_DEBUG_SPINLOCK
Davidlohr Buesoe34191f2014-09-29 06:14:23 -0700680 if ((strncmp(torture_type, "spin", 4) == 0) ||
681 (strncmp(torture_type, "rw_lock", 7) == 0))
Davidlohr Bueso630952c2014-09-11 21:42:25 -0700682 cxt.debug_lock = true;
Davidlohr Buesof095bfc2014-09-11 20:40:19 -0700683#endif
Paul E. McKenney0af3fe12014-02-04 15:51:41 -0800684
685 /* Initialize the statistics so that each run gets its own numbers. */
686
687 lock_is_write_held = 0;
Davidlohr Bueso630952c2014-09-11 21:42:25 -0700688 cxt.lwsa = kmalloc(sizeof(*cxt.lwsa) * cxt.nrealwriters_stress, GFP_KERNEL);
689 if (cxt.lwsa == NULL) {
690 VERBOSE_TOROUT_STRING("cxt.lwsa: Out of memory");
Paul E. McKenney0af3fe12014-02-04 15:51:41 -0800691 firsterr = -ENOMEM;
692 goto unwind;
693 }
Davidlohr Bueso630952c2014-09-11 21:42:25 -0700694 for (i = 0; i < cxt.nrealwriters_stress; i++) {
695 cxt.lwsa[i].n_lock_fail = 0;
696 cxt.lwsa[i].n_lock_acquired = 0;
Paul E. McKenney0af3fe12014-02-04 15:51:41 -0800697 }
698
Davidlohr Bueso630952c2014-09-11 21:42:25 -0700699 if (cxt.cur_ops->readlock) {
Davidlohr Bueso4f6332c2014-09-11 21:40:41 -0700700 if (nreaders_stress >= 0)
Davidlohr Bueso630952c2014-09-11 21:42:25 -0700701 cxt.nrealreaders_stress = nreaders_stress;
Davidlohr Bueso4f6332c2014-09-11 21:40:41 -0700702 else {
703 /*
704 * By default distribute evenly the number of
705 * readers and writers. We still run the same number
706 * of threads as the writer-only locks default.
707 */
708 if (nwriters_stress < 0) /* user doesn't care */
Davidlohr Bueso630952c2014-09-11 21:42:25 -0700709 cxt.nrealwriters_stress = num_online_cpus();
710 cxt.nrealreaders_stress = cxt.nrealwriters_stress;
Davidlohr Bueso4f6332c2014-09-11 21:40:41 -0700711 }
Paul E. McKenney0af3fe12014-02-04 15:51:41 -0800712
Davidlohr Bueso4f6332c2014-09-11 21:40:41 -0700713 lock_is_read_held = 0;
Davidlohr Bueso630952c2014-09-11 21:42:25 -0700714 cxt.lrsa = kmalloc(sizeof(*cxt.lrsa) * cxt.nrealreaders_stress, GFP_KERNEL);
715 if (cxt.lrsa == NULL) {
716 VERBOSE_TOROUT_STRING("cxt.lrsa: Out of memory");
Davidlohr Bueso4f6332c2014-09-11 21:40:41 -0700717 firsterr = -ENOMEM;
Davidlohr Bueso630952c2014-09-11 21:42:25 -0700718 kfree(cxt.lwsa);
Davidlohr Bueso4f6332c2014-09-11 21:40:41 -0700719 goto unwind;
720 }
721
Davidlohr Bueso630952c2014-09-11 21:42:25 -0700722 for (i = 0; i < cxt.nrealreaders_stress; i++) {
723 cxt.lrsa[i].n_lock_fail = 0;
724 cxt.lrsa[i].n_lock_acquired = 0;
Davidlohr Bueso4f6332c2014-09-11 21:40:41 -0700725 }
726 }
Davidlohr Bueso630952c2014-09-11 21:42:25 -0700727 lock_torture_print_module_parms(cxt.cur_ops, "Start of test");
Davidlohr Bueso4f6332c2014-09-11 21:40:41 -0700728
729 /* Prepare torture context. */
Paul E. McKenney0af3fe12014-02-04 15:51:41 -0800730 if (onoff_interval > 0) {
731 firsterr = torture_onoff_init(onoff_holdoff * HZ,
732 onoff_interval * HZ);
733 if (firsterr)
734 goto unwind;
735 }
736 if (shuffle_interval > 0) {
737 firsterr = torture_shuffle_init(shuffle_interval);
738 if (firsterr)
739 goto unwind;
740 }
741 if (shutdown_secs > 0) {
742 firsterr = torture_shutdown_init(shutdown_secs,
743 lock_torture_cleanup);
744 if (firsterr)
745 goto unwind;
746 }
747 if (stutter > 0) {
748 firsterr = torture_stutter_init(stutter);
749 if (firsterr)
750 goto unwind;
751 }
752
Davidlohr Bueso630952c2014-09-11 21:42:25 -0700753 writer_tasks = kzalloc(cxt.nrealwriters_stress * sizeof(writer_tasks[0]),
Paul E. McKenney0af3fe12014-02-04 15:51:41 -0800754 GFP_KERNEL);
755 if (writer_tasks == NULL) {
756 VERBOSE_TOROUT_ERRSTRING("writer_tasks: Out of memory");
757 firsterr = -ENOMEM;
758 goto unwind;
759 }
Davidlohr Bueso4f6332c2014-09-11 21:40:41 -0700760
Davidlohr Bueso630952c2014-09-11 21:42:25 -0700761 if (cxt.cur_ops->readlock) {
762 reader_tasks = kzalloc(cxt.nrealreaders_stress * sizeof(reader_tasks[0]),
Davidlohr Bueso4f6332c2014-09-11 21:40:41 -0700763 GFP_KERNEL);
764 if (reader_tasks == NULL) {
765 VERBOSE_TOROUT_ERRSTRING("reader_tasks: Out of memory");
766 firsterr = -ENOMEM;
767 goto unwind;
768 }
769 }
770
771 /*
772 * Create the kthreads and start torturing (oh, those poor little locks).
773 *
774 * TODO: Note that we interleave writers with readers, giving writers a
775 * slight advantage, by creating its kthread first. This can be modified
776 * for very specific needs, or even let the user choose the policy, if
777 * ever wanted.
778 */
Davidlohr Bueso630952c2014-09-11 21:42:25 -0700779 for (i = 0, j = 0; i < cxt.nrealwriters_stress ||
780 j < cxt.nrealreaders_stress; i++, j++) {
781 if (i >= cxt.nrealwriters_stress)
Davidlohr Bueso4f6332c2014-09-11 21:40:41 -0700782 goto create_reader;
783
784 /* Create writer. */
Davidlohr Bueso630952c2014-09-11 21:42:25 -0700785 firsterr = torture_create_kthread(lock_torture_writer, &cxt.lwsa[i],
Paul E. McKenney0af3fe12014-02-04 15:51:41 -0800786 writer_tasks[i]);
787 if (firsterr)
788 goto unwind;
Davidlohr Bueso4f6332c2014-09-11 21:40:41 -0700789
790 create_reader:
Davidlohr Bueso630952c2014-09-11 21:42:25 -0700791 if (cxt.cur_ops->readlock == NULL || (j >= cxt.nrealreaders_stress))
Davidlohr Bueso4f6332c2014-09-11 21:40:41 -0700792 continue;
793 /* Create reader. */
Davidlohr Bueso630952c2014-09-11 21:42:25 -0700794 firsterr = torture_create_kthread(lock_torture_reader, &cxt.lrsa[j],
Davidlohr Bueso4f6332c2014-09-11 21:40:41 -0700795 reader_tasks[j]);
796 if (firsterr)
797 goto unwind;
Paul E. McKenney0af3fe12014-02-04 15:51:41 -0800798 }
799 if (stat_interval > 0) {
800 firsterr = torture_create_kthread(lock_torture_stats, NULL,
801 stats_task);
802 if (firsterr)
803 goto unwind;
804 }
805 torture_init_end();
806 return 0;
807
808unwind:
809 torture_init_end();
810 lock_torture_cleanup();
811 return firsterr;
812}
813
814module_init(lock_torture_init);
815module_exit(lock_torture_cleanup);