]>
Commit | Line | Data |
---|---|---|
5a4eb3cb | 1 | // SPDX-License-Identifier: GPL-2.0+ |
0af3fe1e PM |
2 | /* |
3 | * Module-based torture test facility for locking | |
4 | * | |
0af3fe1e PM |
5 | * Copyright (C) IBM Corporation, 2014 |
6 | * | |
5a4eb3cb | 7 | * Authors: Paul E. McKenney <paulmck@linux.ibm.com> |
095777c4 | 8 | * Davidlohr Bueso <dave@stgolabs.net> |
0af3fe1e PM |
9 | * Based on kernel/rcu/torture.c. |
10 | */ | |
60500037 PM |
11 | |
12 | #define pr_fmt(fmt) fmt | |
13 | ||
0af3fe1e | 14 | #include <linux/kernel.h> |
0af3fe1e PM |
15 | #include <linux/module.h> |
16 | #include <linux/kthread.h> | |
095777c4 | 17 | #include <linux/sched/rt.h> |
0af3fe1e | 18 | #include <linux/spinlock.h> |
42ddc75d | 19 | #include <linux/mutex.h> |
c98fed9f | 20 | #include <linux/rwsem.h> |
0af3fe1e PM |
21 | #include <linux/smp.h> |
22 | #include <linux/interrupt.h> | |
23 | #include <linux/sched.h> | |
ae7e81c0 | 24 | #include <uapi/linux/sched/types.h> |
037741a6 | 25 | #include <linux/rtmutex.h> |
0af3fe1e | 26 | #include <linux/atomic.h> |
0af3fe1e | 27 | #include <linux/moduleparam.h> |
0af3fe1e | 28 | #include <linux/delay.h> |
0af3fe1e | 29 | #include <linux/slab.h> |
0af3fe1e | 30 | #include <linux/torture.h> |
6b74fa0a | 31 | #include <linux/reboot.h> |
0af3fe1e PM |
32 | |
33 | MODULE_LICENSE("GPL"); | |
5a4eb3cb | 34 | MODULE_AUTHOR("Paul E. McKenney <paulmck@linux.ibm.com>"); |
0af3fe1e PM |
35 | |
36 | torture_param(int, nwriters_stress, -1, | |
37 | "Number of write-locking stress-test threads"); | |
4f6332c1 DB |
38 | torture_param(int, nreaders_stress, -1, |
39 | "Number of read-locking stress-test threads"); | |
0af3fe1e PM |
40 | torture_param(int, onoff_holdoff, 0, "Time after boot before CPU hotplugs (s)"); |
41 | torture_param(int, onoff_interval, 0, | |
42 | "Time between CPU hotplugs (s), 0=disable"); | |
43 | torture_param(int, shuffle_interval, 3, | |
44 | "Number of jiffies between shuffles, 0=disable"); | |
45 | torture_param(int, shutdown_secs, 0, "Shutdown time (j), <= zero to disable."); | |
46 | torture_param(int, stat_interval, 60, | |
47 | "Number of seconds between stats printk()s"); | |
48 | torture_param(int, stutter, 5, "Number of jiffies to run/halt test, 0=disable"); | |
90127d60 | 49 | torture_param(int, verbose, 1, |
0af3fe1e PM |
50 | "Enable verbose debugging printk()s"); |
51 | ||
52 | static char *torture_type = "spin_lock"; | |
53 | module_param(torture_type, charp, 0444); | |
54 | MODULE_PARM_DESC(torture_type, | |
42ddc75d | 55 | "Type of lock to torture (spin_lock, spin_lock_irq, mutex_lock, ...)"); |
0af3fe1e | 56 | |
0af3fe1e PM |
57 | static struct task_struct *stats_task; |
58 | static struct task_struct **writer_tasks; | |
4f6332c1 | 59 | static struct task_struct **reader_tasks; |
0af3fe1e | 60 | |
0af3fe1e | 61 | static bool lock_is_write_held; |
4f6332c1 | 62 | static bool lock_is_read_held; |
3480d677 | 63 | static unsigned long last_lock_release; |
0af3fe1e | 64 | |
1e6757a9 DB |
65 | struct lock_stress_stats { |
66 | long n_lock_fail; | |
67 | long n_lock_acquired; | |
0af3fe1e | 68 | }; |
0af3fe1e | 69 | |
0af3fe1e PM |
70 | /* Forward reference. */ |
71 | static void lock_torture_cleanup(void); | |
72 | ||
73 | /* | |
74 | * Operations vector for selecting different types of tests. | |
75 | */ | |
76 | struct lock_torture_ops { | |
77 | void (*init)(void); | |
0d720287 | 78 | void (*exit)(void); |
0af3fe1e PM |
79 | int (*writelock)(void); |
80 | void (*write_delay)(struct torture_random_state *trsp); | |
095777c4 | 81 | void (*task_boost)(struct torture_random_state *trsp); |
0af3fe1e | 82 | void (*writeunlock)(void); |
4f6332c1 DB |
83 | int (*readlock)(void); |
84 | void (*read_delay)(struct torture_random_state *trsp); | |
85 | void (*readunlock)(void); | |
095777c4 DB |
86 | |
87 | unsigned long flags; /* for irq spinlocks */ | |
0af3fe1e PM |
88 | const char *name; |
89 | }; | |
90 | ||
630952c2 DB |
91 | struct lock_torture_cxt { |
92 | int nrealwriters_stress; | |
93 | int nrealreaders_stress; | |
94 | bool debug_lock; | |
0d720287 | 95 | bool init_called; |
630952c2 DB |
96 | atomic_t n_lock_torture_errors; |
97 | struct lock_torture_ops *cur_ops; | |
98 | struct lock_stress_stats *lwsa; /* writer statistics */ | |
99 | struct lock_stress_stats *lrsa; /* reader statistics */ | |
100 | }; | |
0d720287 | 101 | static struct lock_torture_cxt cxt = { 0, 0, false, false, |
630952c2 DB |
102 | ATOMIC_INIT(0), |
103 | NULL, NULL}; | |
0af3fe1e PM |
104 | /* |
105 | * Definitions for lock torture testing. | |
106 | */ | |
107 | ||
e086481b PM |
108 | static int torture_lock_busted_write_lock(void) |
109 | { | |
110 | return 0; /* BUGGY, do not use in real life!!! */ | |
111 | } | |
112 | ||
113 | static void torture_lock_busted_write_delay(struct torture_random_state *trsp) | |
114 | { | |
61d49d2f | 115 | const unsigned long longdelay_ms = 100; |
e086481b PM |
116 | |
117 | /* We want a long delay occasionally to force massive contention. */ | |
118 | if (!(torture_random(trsp) % | |
61d49d2f PM |
119 | (cxt.nrealwriters_stress * 2000 * longdelay_ms))) |
120 | mdelay(longdelay_ms); | |
630952c2 | 121 | if (!(torture_random(trsp) % (cxt.nrealwriters_stress * 20000))) |
cc1321c9 | 122 | torture_preempt_schedule(); /* Allow test to be preempted. */ |
e086481b PM |
123 | } |
124 | ||
125 | static void torture_lock_busted_write_unlock(void) | |
126 | { | |
127 | /* BUGGY, do not use in real life!!! */ | |
128 | } | |
129 | ||
095777c4 DB |
130 | static void torture_boost_dummy(struct torture_random_state *trsp) |
131 | { | |
132 | /* Only rtmutexes care about priority */ | |
133 | } | |
134 | ||
e086481b PM |
135 | static struct lock_torture_ops lock_busted_ops = { |
136 | .writelock = torture_lock_busted_write_lock, | |
137 | .write_delay = torture_lock_busted_write_delay, | |
095777c4 | 138 | .task_boost = torture_boost_dummy, |
e086481b | 139 | .writeunlock = torture_lock_busted_write_unlock, |
4f6332c1 DB |
140 | .readlock = NULL, |
141 | .read_delay = NULL, | |
142 | .readunlock = NULL, | |
e086481b PM |
143 | .name = "lock_busted" |
144 | }; | |
145 | ||
0af3fe1e PM |
146 | static DEFINE_SPINLOCK(torture_spinlock); |
147 | ||
148 | static int torture_spin_lock_write_lock(void) __acquires(torture_spinlock) | |
149 | { | |
150 | spin_lock(&torture_spinlock); | |
151 | return 0; | |
152 | } | |
153 | ||
154 | static void torture_spin_lock_write_delay(struct torture_random_state *trsp) | |
155 | { | |
156 | const unsigned long shortdelay_us = 2; | |
61d49d2f | 157 | const unsigned long longdelay_ms = 100; |
0af3fe1e PM |
158 | |
159 | /* We want a short delay mostly to emulate likely code, and | |
160 | * we want a long delay occasionally to force massive contention. | |
161 | */ | |
162 | if (!(torture_random(trsp) % | |
61d49d2f PM |
163 | (cxt.nrealwriters_stress * 2000 * longdelay_ms))) |
164 | mdelay(longdelay_ms); | |
0af3fe1e | 165 | if (!(torture_random(trsp) % |
630952c2 | 166 | (cxt.nrealwriters_stress * 2 * shortdelay_us))) |
0af3fe1e | 167 | udelay(shortdelay_us); |
630952c2 | 168 | if (!(torture_random(trsp) % (cxt.nrealwriters_stress * 20000))) |
cc1321c9 | 169 | torture_preempt_schedule(); /* Allow test to be preempted. */ |
0af3fe1e PM |
170 | } |
171 | ||
172 | static void torture_spin_lock_write_unlock(void) __releases(torture_spinlock) | |
173 | { | |
174 | spin_unlock(&torture_spinlock); | |
175 | } | |
176 | ||
177 | static struct lock_torture_ops spin_lock_ops = { | |
178 | .writelock = torture_spin_lock_write_lock, | |
179 | .write_delay = torture_spin_lock_write_delay, | |
095777c4 | 180 | .task_boost = torture_boost_dummy, |
0af3fe1e | 181 | .writeunlock = torture_spin_lock_write_unlock, |
4f6332c1 DB |
182 | .readlock = NULL, |
183 | .read_delay = NULL, | |
184 | .readunlock = NULL, | |
0af3fe1e PM |
185 | .name = "spin_lock" |
186 | }; | |
187 | ||
188 | static int torture_spin_lock_write_lock_irq(void) | |
219f800f | 189 | __acquires(torture_spinlock) |
0af3fe1e PM |
190 | { |
191 | unsigned long flags; | |
192 | ||
193 | spin_lock_irqsave(&torture_spinlock, flags); | |
630952c2 | 194 | cxt.cur_ops->flags = flags; |
0af3fe1e PM |
195 | return 0; |
196 | } | |
197 | ||
198 | static void torture_lock_spin_write_unlock_irq(void) | |
199 | __releases(torture_spinlock) | |
200 | { | |
630952c2 | 201 | spin_unlock_irqrestore(&torture_spinlock, cxt.cur_ops->flags); |
0af3fe1e PM |
202 | } |
203 | ||
204 | static struct lock_torture_ops spin_lock_irq_ops = { | |
205 | .writelock = torture_spin_lock_write_lock_irq, | |
206 | .write_delay = torture_spin_lock_write_delay, | |
095777c4 | 207 | .task_boost = torture_boost_dummy, |
0af3fe1e | 208 | .writeunlock = torture_lock_spin_write_unlock_irq, |
4f6332c1 DB |
209 | .readlock = NULL, |
210 | .read_delay = NULL, | |
211 | .readunlock = NULL, | |
0af3fe1e PM |
212 | .name = "spin_lock_irq" |
213 | }; | |
214 | ||
e34191fa DB |
215 | static DEFINE_RWLOCK(torture_rwlock); |
216 | ||
217 | static int torture_rwlock_write_lock(void) __acquires(torture_rwlock) | |
218 | { | |
219 | write_lock(&torture_rwlock); | |
220 | return 0; | |
221 | } | |
222 | ||
223 | static void torture_rwlock_write_delay(struct torture_random_state *trsp) | |
224 | { | |
225 | const unsigned long shortdelay_us = 2; | |
226 | const unsigned long longdelay_ms = 100; | |
227 | ||
228 | /* We want a short delay mostly to emulate likely code, and | |
229 | * we want a long delay occasionally to force massive contention. | |
230 | */ | |
231 | if (!(torture_random(trsp) % | |
232 | (cxt.nrealwriters_stress * 2000 * longdelay_ms))) | |
233 | mdelay(longdelay_ms); | |
234 | else | |
235 | udelay(shortdelay_us); | |
236 | } | |
237 | ||
238 | static void torture_rwlock_write_unlock(void) __releases(torture_rwlock) | |
239 | { | |
240 | write_unlock(&torture_rwlock); | |
241 | } | |
242 | ||
243 | static int torture_rwlock_read_lock(void) __acquires(torture_rwlock) | |
244 | { | |
245 | read_lock(&torture_rwlock); | |
246 | return 0; | |
247 | } | |
248 | ||
249 | static void torture_rwlock_read_delay(struct torture_random_state *trsp) | |
250 | { | |
251 | const unsigned long shortdelay_us = 10; | |
252 | const unsigned long longdelay_ms = 100; | |
253 | ||
254 | /* We want a short delay mostly to emulate likely code, and | |
255 | * we want a long delay occasionally to force massive contention. | |
256 | */ | |
257 | if (!(torture_random(trsp) % | |
258 | (cxt.nrealreaders_stress * 2000 * longdelay_ms))) | |
259 | mdelay(longdelay_ms); | |
260 | else | |
261 | udelay(shortdelay_us); | |
262 | } | |
263 | ||
264 | static void torture_rwlock_read_unlock(void) __releases(torture_rwlock) | |
265 | { | |
266 | read_unlock(&torture_rwlock); | |
267 | } | |
268 | ||
269 | static struct lock_torture_ops rw_lock_ops = { | |
270 | .writelock = torture_rwlock_write_lock, | |
271 | .write_delay = torture_rwlock_write_delay, | |
095777c4 | 272 | .task_boost = torture_boost_dummy, |
e34191fa DB |
273 | .writeunlock = torture_rwlock_write_unlock, |
274 | .readlock = torture_rwlock_read_lock, | |
275 | .read_delay = torture_rwlock_read_delay, | |
276 | .readunlock = torture_rwlock_read_unlock, | |
277 | .name = "rw_lock" | |
278 | }; | |
279 | ||
280 | static int torture_rwlock_write_lock_irq(void) __acquires(torture_rwlock) | |
281 | { | |
282 | unsigned long flags; | |
283 | ||
284 | write_lock_irqsave(&torture_rwlock, flags); | |
285 | cxt.cur_ops->flags = flags; | |
286 | return 0; | |
287 | } | |
288 | ||
289 | static void torture_rwlock_write_unlock_irq(void) | |
290 | __releases(torture_rwlock) | |
291 | { | |
292 | write_unlock_irqrestore(&torture_rwlock, cxt.cur_ops->flags); | |
293 | } | |
294 | ||
295 | static int torture_rwlock_read_lock_irq(void) __acquires(torture_rwlock) | |
296 | { | |
297 | unsigned long flags; | |
298 | ||
299 | read_lock_irqsave(&torture_rwlock, flags); | |
300 | cxt.cur_ops->flags = flags; | |
301 | return 0; | |
302 | } | |
303 | ||
304 | static void torture_rwlock_read_unlock_irq(void) | |
305 | __releases(torture_rwlock) | |
306 | { | |
f548d99e | 307 | read_unlock_irqrestore(&torture_rwlock, cxt.cur_ops->flags); |
e34191fa DB |
308 | } |
309 | ||
310 | static struct lock_torture_ops rw_lock_irq_ops = { | |
311 | .writelock = torture_rwlock_write_lock_irq, | |
312 | .write_delay = torture_rwlock_write_delay, | |
095777c4 | 313 | .task_boost = torture_boost_dummy, |
e34191fa DB |
314 | .writeunlock = torture_rwlock_write_unlock_irq, |
315 | .readlock = torture_rwlock_read_lock_irq, | |
316 | .read_delay = torture_rwlock_read_delay, | |
317 | .readunlock = torture_rwlock_read_unlock_irq, | |
318 | .name = "rw_lock_irq" | |
319 | }; | |
320 | ||
42ddc75d DB |
321 | static DEFINE_MUTEX(torture_mutex); |
322 | ||
323 | static int torture_mutex_lock(void) __acquires(torture_mutex) | |
324 | { | |
325 | mutex_lock(&torture_mutex); | |
326 | return 0; | |
327 | } | |
328 | ||
329 | static void torture_mutex_delay(struct torture_random_state *trsp) | |
330 | { | |
331 | const unsigned long longdelay_ms = 100; | |
332 | ||
333 | /* We want a long delay occasionally to force massive contention. */ | |
334 | if (!(torture_random(trsp) % | |
630952c2 | 335 | (cxt.nrealwriters_stress * 2000 * longdelay_ms))) |
42ddc75d DB |
336 | mdelay(longdelay_ms * 5); |
337 | else | |
338 | mdelay(longdelay_ms / 5); | |
630952c2 | 339 | if (!(torture_random(trsp) % (cxt.nrealwriters_stress * 20000))) |
cc1321c9 | 340 | torture_preempt_schedule(); /* Allow test to be preempted. */ |
42ddc75d DB |
341 | } |
342 | ||
343 | static void torture_mutex_unlock(void) __releases(torture_mutex) | |
344 | { | |
345 | mutex_unlock(&torture_mutex); | |
346 | } | |
347 | ||
348 | static struct lock_torture_ops mutex_lock_ops = { | |
349 | .writelock = torture_mutex_lock, | |
350 | .write_delay = torture_mutex_delay, | |
095777c4 | 351 | .task_boost = torture_boost_dummy, |
42ddc75d | 352 | .writeunlock = torture_mutex_unlock, |
4f6332c1 DB |
353 | .readlock = NULL, |
354 | .read_delay = NULL, | |
355 | .readunlock = NULL, | |
42ddc75d DB |
356 | .name = "mutex_lock" |
357 | }; | |
358 | ||
0186a6cb | 359 | #include <linux/ww_mutex.h> |
08295b3b | 360 | static DEFINE_WD_CLASS(torture_ww_class); |
0186a6cb CW |
361 | static DEFINE_WW_MUTEX(torture_ww_mutex_0, &torture_ww_class); |
362 | static DEFINE_WW_MUTEX(torture_ww_mutex_1, &torture_ww_class); | |
363 | static DEFINE_WW_MUTEX(torture_ww_mutex_2, &torture_ww_class); | |
364 | ||
365 | static int torture_ww_mutex_lock(void) | |
366 | __acquires(torture_ww_mutex_0) | |
367 | __acquires(torture_ww_mutex_1) | |
368 | __acquires(torture_ww_mutex_2) | |
369 | { | |
370 | LIST_HEAD(list); | |
371 | struct reorder_lock { | |
372 | struct list_head link; | |
373 | struct ww_mutex *lock; | |
374 | } locks[3], *ll, *ln; | |
375 | struct ww_acquire_ctx ctx; | |
376 | ||
377 | locks[0].lock = &torture_ww_mutex_0; | |
378 | list_add(&locks[0].link, &list); | |
379 | ||
380 | locks[1].lock = &torture_ww_mutex_1; | |
381 | list_add(&locks[1].link, &list); | |
382 | ||
383 | locks[2].lock = &torture_ww_mutex_2; | |
384 | list_add(&locks[2].link, &list); | |
385 | ||
386 | ww_acquire_init(&ctx, &torture_ww_class); | |
387 | ||
388 | list_for_each_entry(ll, &list, link) { | |
389 | int err; | |
390 | ||
391 | err = ww_mutex_lock(ll->lock, &ctx); | |
392 | if (!err) | |
393 | continue; | |
394 | ||
395 | ln = ll; | |
396 | list_for_each_entry_continue_reverse(ln, &list, link) | |
397 | ww_mutex_unlock(ln->lock); | |
398 | ||
399 | if (err != -EDEADLK) | |
400 | return err; | |
401 | ||
402 | ww_mutex_lock_slow(ll->lock, &ctx); | |
403 | list_move(&ll->link, &list); | |
404 | } | |
405 | ||
406 | ww_acquire_fini(&ctx); | |
407 | return 0; | |
408 | } | |
409 | ||
410 | static void torture_ww_mutex_unlock(void) | |
411 | __releases(torture_ww_mutex_0) | |
412 | __releases(torture_ww_mutex_1) | |
413 | __releases(torture_ww_mutex_2) | |
414 | { | |
415 | ww_mutex_unlock(&torture_ww_mutex_0); | |
416 | ww_mutex_unlock(&torture_ww_mutex_1); | |
417 | ww_mutex_unlock(&torture_ww_mutex_2); | |
418 | } | |
419 | ||
420 | static struct lock_torture_ops ww_mutex_lock_ops = { | |
421 | .writelock = torture_ww_mutex_lock, | |
422 | .write_delay = torture_mutex_delay, | |
423 | .task_boost = torture_boost_dummy, | |
424 | .writeunlock = torture_ww_mutex_unlock, | |
425 | .readlock = NULL, | |
426 | .read_delay = NULL, | |
427 | .readunlock = NULL, | |
428 | .name = "ww_mutex_lock" | |
429 | }; | |
430 | ||
095777c4 DB |
431 | #ifdef CONFIG_RT_MUTEXES |
432 | static DEFINE_RT_MUTEX(torture_rtmutex); | |
433 | ||
434 | static int torture_rtmutex_lock(void) __acquires(torture_rtmutex) | |
435 | { | |
436 | rt_mutex_lock(&torture_rtmutex); | |
437 | return 0; | |
438 | } | |
439 | ||
440 | static void torture_rtmutex_boost(struct torture_random_state *trsp) | |
441 | { | |
095777c4 DB |
442 | const unsigned int factor = 50000; /* yes, quite arbitrary */ |
443 | ||
444 | if (!rt_task(current)) { | |
445 | /* | |
1f190931 | 446 | * Boost priority once every ~50k operations. When the |
095777c4 DB |
447 | * task tries to take the lock, the rtmutex it will account |
448 | * for the new priority, and do any corresponding pi-dance. | |
449 | */ | |
1f190931 DB |
450 | if (trsp && !(torture_random(trsp) % |
451 | (cxt.nrealwriters_stress * factor))) { | |
93db9129 | 452 | sched_set_fifo(current); |
095777c4 DB |
453 | } else /* common case, do nothing */ |
454 | return; | |
455 | } else { | |
456 | /* | |
457 | * The task will remain boosted for another ~500k operations, | |
458 | * then restored back to its original prio, and so forth. | |
459 | * | |
460 | * When @trsp is nil, we want to force-reset the task for | |
461 | * stopping the kthread. | |
462 | */ | |
463 | if (!trsp || !(torture_random(trsp) % | |
464 | (cxt.nrealwriters_stress * factor * 2))) { | |
93db9129 | 465 | sched_set_normal(current, 0); |
095777c4 DB |
466 | } else /* common case, do nothing */ |
467 | return; | |
468 | } | |
095777c4 DB |
469 | } |
470 | ||
471 | static void torture_rtmutex_delay(struct torture_random_state *trsp) | |
472 | { | |
473 | const unsigned long shortdelay_us = 2; | |
474 | const unsigned long longdelay_ms = 100; | |
475 | ||
476 | /* | |
477 | * We want a short delay mostly to emulate likely code, and | |
478 | * we want a long delay occasionally to force massive contention. | |
479 | */ | |
480 | if (!(torture_random(trsp) % | |
481 | (cxt.nrealwriters_stress * 2000 * longdelay_ms))) | |
482 | mdelay(longdelay_ms); | |
483 | if (!(torture_random(trsp) % | |
484 | (cxt.nrealwriters_stress * 2 * shortdelay_us))) | |
485 | udelay(shortdelay_us); | |
095777c4 | 486 | if (!(torture_random(trsp) % (cxt.nrealwriters_stress * 20000))) |
cc1321c9 | 487 | torture_preempt_schedule(); /* Allow test to be preempted. */ |
095777c4 DB |
488 | } |
489 | ||
490 | static void torture_rtmutex_unlock(void) __releases(torture_rtmutex) | |
491 | { | |
492 | rt_mutex_unlock(&torture_rtmutex); | |
493 | } | |
494 | ||
495 | static struct lock_torture_ops rtmutex_lock_ops = { | |
496 | .writelock = torture_rtmutex_lock, | |
497 | .write_delay = torture_rtmutex_delay, | |
498 | .task_boost = torture_rtmutex_boost, | |
499 | .writeunlock = torture_rtmutex_unlock, | |
500 | .readlock = NULL, | |
501 | .read_delay = NULL, | |
502 | .readunlock = NULL, | |
503 | .name = "rtmutex_lock" | |
504 | }; | |
505 | #endif | |
506 | ||
4a3b427f DB |
507 | static DECLARE_RWSEM(torture_rwsem); |
508 | static int torture_rwsem_down_write(void) __acquires(torture_rwsem) | |
509 | { | |
510 | down_write(&torture_rwsem); | |
511 | return 0; | |
512 | } | |
513 | ||
514 | static void torture_rwsem_write_delay(struct torture_random_state *trsp) | |
515 | { | |
516 | const unsigned long longdelay_ms = 100; | |
517 | ||
518 | /* We want a long delay occasionally to force massive contention. */ | |
519 | if (!(torture_random(trsp) % | |
630952c2 | 520 | (cxt.nrealwriters_stress * 2000 * longdelay_ms))) |
4a3b427f DB |
521 | mdelay(longdelay_ms * 10); |
522 | else | |
523 | mdelay(longdelay_ms / 10); | |
630952c2 | 524 | if (!(torture_random(trsp) % (cxt.nrealwriters_stress * 20000))) |
cc1321c9 | 525 | torture_preempt_schedule(); /* Allow test to be preempted. */ |
4a3b427f DB |
526 | } |
527 | ||
528 | static void torture_rwsem_up_write(void) __releases(torture_rwsem) | |
529 | { | |
530 | up_write(&torture_rwsem); | |
531 | } | |
532 | ||
533 | static int torture_rwsem_down_read(void) __acquires(torture_rwsem) | |
534 | { | |
535 | down_read(&torture_rwsem); | |
536 | return 0; | |
537 | } | |
538 | ||
539 | static void torture_rwsem_read_delay(struct torture_random_state *trsp) | |
540 | { | |
541 | const unsigned long longdelay_ms = 100; | |
542 | ||
543 | /* We want a long delay occasionally to force massive contention. */ | |
544 | if (!(torture_random(trsp) % | |
f2f76260 | 545 | (cxt.nrealreaders_stress * 2000 * longdelay_ms))) |
4a3b427f DB |
546 | mdelay(longdelay_ms * 2); |
547 | else | |
548 | mdelay(longdelay_ms / 2); | |
630952c2 | 549 | if (!(torture_random(trsp) % (cxt.nrealreaders_stress * 20000))) |
cc1321c9 | 550 | torture_preempt_schedule(); /* Allow test to be preempted. */ |
4a3b427f DB |
551 | } |
552 | ||
553 | static void torture_rwsem_up_read(void) __releases(torture_rwsem) | |
554 | { | |
555 | up_read(&torture_rwsem); | |
556 | } | |
557 | ||
558 | static struct lock_torture_ops rwsem_lock_ops = { | |
559 | .writelock = torture_rwsem_down_write, | |
560 | .write_delay = torture_rwsem_write_delay, | |
095777c4 | 561 | .task_boost = torture_boost_dummy, |
4a3b427f DB |
562 | .writeunlock = torture_rwsem_up_write, |
563 | .readlock = torture_rwsem_down_read, | |
564 | .read_delay = torture_rwsem_read_delay, | |
565 | .readunlock = torture_rwsem_up_read, | |
566 | .name = "rwsem_lock" | |
567 | }; | |
568 | ||
617783dd PM |
569 | #include <linux/percpu-rwsem.h> |
570 | static struct percpu_rw_semaphore pcpu_rwsem; | |
571 | ||
d49bed9a | 572 | static void torture_percpu_rwsem_init(void) |
617783dd PM |
573 | { |
574 | BUG_ON(percpu_init_rwsem(&pcpu_rwsem)); | |
575 | } | |
576 | ||
0d720287 HT |
577 | static void torture_percpu_rwsem_exit(void) |
578 | { | |
579 | percpu_free_rwsem(&pcpu_rwsem); | |
580 | } | |
581 | ||
617783dd PM |
582 | static int torture_percpu_rwsem_down_write(void) __acquires(pcpu_rwsem) |
583 | { | |
584 | percpu_down_write(&pcpu_rwsem); | |
585 | return 0; | |
586 | } | |
587 | ||
588 | static void torture_percpu_rwsem_up_write(void) __releases(pcpu_rwsem) | |
589 | { | |
590 | percpu_up_write(&pcpu_rwsem); | |
591 | } | |
592 | ||
593 | static int torture_percpu_rwsem_down_read(void) __acquires(pcpu_rwsem) | |
594 | { | |
595 | percpu_down_read(&pcpu_rwsem); | |
596 | return 0; | |
597 | } | |
598 | ||
599 | static void torture_percpu_rwsem_up_read(void) __releases(pcpu_rwsem) | |
600 | { | |
601 | percpu_up_read(&pcpu_rwsem); | |
602 | } | |
603 | ||
604 | static struct lock_torture_ops percpu_rwsem_lock_ops = { | |
605 | .init = torture_percpu_rwsem_init, | |
0d720287 | 606 | .exit = torture_percpu_rwsem_exit, |
617783dd PM |
607 | .writelock = torture_percpu_rwsem_down_write, |
608 | .write_delay = torture_rwsem_write_delay, | |
609 | .task_boost = torture_boost_dummy, | |
610 | .writeunlock = torture_percpu_rwsem_up_write, | |
611 | .readlock = torture_percpu_rwsem_down_read, | |
612 | .read_delay = torture_rwsem_read_delay, | |
613 | .readunlock = torture_percpu_rwsem_up_read, | |
614 | .name = "percpu_rwsem_lock" | |
615 | }; | |
616 | ||
0af3fe1e PM |
617 | /* |
618 | * Lock torture writer kthread. Repeatedly acquires and releases | |
619 | * the lock, checking for duplicate acquisitions. | |
620 | */ | |
621 | static int lock_torture_writer(void *arg) | |
622 | { | |
1e6757a9 | 623 | struct lock_stress_stats *lwsp = arg; |
c0e1472d | 624 | DEFINE_TORTURE_RANDOM(rand); |
0af3fe1e PM |
625 | |
626 | VERBOSE_TOROUT_STRING("lock_torture_writer task started"); | |
8698a745 | 627 | set_user_nice(current, MAX_NICE); |
0af3fe1e PM |
628 | |
629 | do { | |
da601c63 PM |
630 | if ((torture_random(&rand) & 0xfffff) == 0) |
631 | schedule_timeout_uninterruptible(1); | |
a1229491 | 632 | |
095777c4 | 633 | cxt.cur_ops->task_boost(&rand); |
630952c2 | 634 | cxt.cur_ops->writelock(); |
0af3fe1e | 635 | if (WARN_ON_ONCE(lock_is_write_held)) |
1e6757a9 | 636 | lwsp->n_lock_fail++; |
d02c6b52 | 637 | lock_is_write_held = true; |
a1229491 DB |
638 | if (WARN_ON_ONCE(lock_is_read_held)) |
639 | lwsp->n_lock_fail++; /* rare, but... */ | |
640 | ||
1e6757a9 | 641 | lwsp->n_lock_acquired++; |
630952c2 | 642 | cxt.cur_ops->write_delay(&rand); |
d02c6b52 | 643 | lock_is_write_held = false; |
3480d677 | 644 | WRITE_ONCE(last_lock_release, jiffies); |
630952c2 | 645 | cxt.cur_ops->writeunlock(); |
a1229491 | 646 | |
0af3fe1e PM |
647 | stutter_wait("lock_torture_writer"); |
648 | } while (!torture_must_stop()); | |
095777c4 DB |
649 | |
650 | cxt.cur_ops->task_boost(NULL); /* reset prio */ | |
0af3fe1e PM |
651 | torture_kthread_stopping("lock_torture_writer"); |
652 | return 0; | |
653 | } | |
654 | ||
4f6332c1 DB |
655 | /* |
656 | * Lock torture reader kthread. Repeatedly acquires and releases | |
657 | * the reader lock. | |
658 | */ | |
659 | static int lock_torture_reader(void *arg) | |
660 | { | |
661 | struct lock_stress_stats *lrsp = arg; | |
c0e1472d | 662 | DEFINE_TORTURE_RANDOM(rand); |
4f6332c1 DB |
663 | |
664 | VERBOSE_TOROUT_STRING("lock_torture_reader task started"); | |
665 | set_user_nice(current, MAX_NICE); | |
666 | ||
667 | do { | |
668 | if ((torture_random(&rand) & 0xfffff) == 0) | |
669 | schedule_timeout_uninterruptible(1); | |
a1229491 | 670 | |
630952c2 | 671 | cxt.cur_ops->readlock(); |
d02c6b52 | 672 | lock_is_read_held = true; |
a1229491 DB |
673 | if (WARN_ON_ONCE(lock_is_write_held)) |
674 | lrsp->n_lock_fail++; /* rare, but... */ | |
675 | ||
4f6332c1 | 676 | lrsp->n_lock_acquired++; |
630952c2 | 677 | cxt.cur_ops->read_delay(&rand); |
d02c6b52 | 678 | lock_is_read_held = false; |
630952c2 | 679 | cxt.cur_ops->readunlock(); |
a1229491 | 680 | |
4f6332c1 DB |
681 | stutter_wait("lock_torture_reader"); |
682 | } while (!torture_must_stop()); | |
683 | torture_kthread_stopping("lock_torture_reader"); | |
684 | return 0; | |
685 | } | |
686 | ||
0af3fe1e PM |
687 | /* |
688 | * Create an lock-torture-statistics message in the specified buffer. | |
689 | */ | |
4f6332c1 DB |
690 | static void __torture_print_stats(char *page, |
691 | struct lock_stress_stats *statp, bool write) | |
0af3fe1e | 692 | { |
d02c6b52 | 693 | bool fail = false; |
4f6332c1 | 694 | int i, n_stress; |
2ce77d16 | 695 | long max = 0, min = statp ? statp[0].n_lock_acquired : 0; |
0af3fe1e PM |
696 | long long sum = 0; |
697 | ||
630952c2 | 698 | n_stress = write ? cxt.nrealwriters_stress : cxt.nrealreaders_stress; |
4f6332c1 DB |
699 | for (i = 0; i < n_stress; i++) { |
700 | if (statp[i].n_lock_fail) | |
0af3fe1e | 701 | fail = true; |
4f6332c1 | 702 | sum += statp[i].n_lock_acquired; |
80c503e0 PM |
703 | if (max < statp[i].n_lock_acquired) |
704 | max = statp[i].n_lock_acquired; | |
705 | if (min > statp[i].n_lock_acquired) | |
706 | min = statp[i].n_lock_acquired; | |
0af3fe1e | 707 | } |
0af3fe1e | 708 | page += sprintf(page, |
4f6332c1 DB |
709 | "%s: Total: %lld Max/Min: %ld/%ld %s Fail: %d %s\n", |
710 | write ? "Writes" : "Reads ", | |
28e09a2e PM |
711 | sum, max, min, |
712 | !onoff_interval && max / 2 > min ? "???" : "", | |
0af3fe1e PM |
713 | fail, fail ? "!!!" : ""); |
714 | if (fail) | |
630952c2 | 715 | atomic_inc(&cxt.n_lock_torture_errors); |
0af3fe1e PM |
716 | } |
717 | ||
718 | /* | |
719 | * Print torture statistics. Caller must ensure that there is only one | |
720 | * call to this function at a given time!!! This is normally accomplished | |
721 | * by relying on the module system to only have one copy of the module | |
722 | * loaded, and then by giving the lock_torture_stats kthread full control | |
723 | * (or the init/cleanup functions when lock_torture_stats thread is not | |
724 | * running). | |
725 | */ | |
726 | static void lock_torture_stats_print(void) | |
727 | { | |
630952c2 | 728 | int size = cxt.nrealwriters_stress * 200 + 8192; |
0af3fe1e PM |
729 | char *buf; |
730 | ||
630952c2 DB |
731 | if (cxt.cur_ops->readlock) |
732 | size += cxt.nrealreaders_stress * 200 + 8192; | |
4f6332c1 | 733 | |
0af3fe1e PM |
734 | buf = kmalloc(size, GFP_KERNEL); |
735 | if (!buf) { | |
736 | pr_err("lock_torture_stats_print: Out of memory, need: %d", | |
737 | size); | |
738 | return; | |
739 | } | |
4f6332c1 | 740 | |
630952c2 | 741 | __torture_print_stats(buf, cxt.lwsa, true); |
0af3fe1e PM |
742 | pr_alert("%s", buf); |
743 | kfree(buf); | |
4f6332c1 | 744 | |
630952c2 | 745 | if (cxt.cur_ops->readlock) { |
4f6332c1 DB |
746 | buf = kmalloc(size, GFP_KERNEL); |
747 | if (!buf) { | |
748 | pr_err("lock_torture_stats_print: Out of memory, need: %d", | |
749 | size); | |
750 | return; | |
751 | } | |
752 | ||
630952c2 | 753 | __torture_print_stats(buf, cxt.lrsa, false); |
4f6332c1 DB |
754 | pr_alert("%s", buf); |
755 | kfree(buf); | |
756 | } | |
0af3fe1e PM |
757 | } |
758 | ||
759 | /* | |
760 | * Periodically prints torture statistics, if periodic statistics printing | |
761 | * was specified via the stat_interval module parameter. | |
762 | * | |
763 | * No need to worry about fullstop here, since this one doesn't reference | |
764 | * volatile state or register callbacks. | |
765 | */ | |
766 | static int lock_torture_stats(void *arg) | |
767 | { | |
768 | VERBOSE_TOROUT_STRING("lock_torture_stats task started"); | |
769 | do { | |
770 | schedule_timeout_interruptible(stat_interval * HZ); | |
771 | lock_torture_stats_print(); | |
772 | torture_shutdown_absorb("lock_torture_stats"); | |
773 | } while (!torture_must_stop()); | |
774 | torture_kthread_stopping("lock_torture_stats"); | |
775 | return 0; | |
776 | } | |
777 | ||
778 | static inline void | |
779 | lock_torture_print_module_parms(struct lock_torture_ops *cur_ops, | |
780 | const char *tag) | |
781 | { | |
782 | pr_alert("%s" TORTURE_FLAG | |
4f6332c1 | 783 | "--- %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", |
630952c2 DB |
784 | torture_type, tag, cxt.debug_lock ? " [debug]": "", |
785 | cxt.nrealwriters_stress, cxt.nrealreaders_stress, stat_interval, | |
4f6332c1 | 786 | verbose, shuffle_interval, stutter, shutdown_secs, |
0af3fe1e PM |
787 | onoff_interval, onoff_holdoff); |
788 | } | |
789 | ||
790 | static void lock_torture_cleanup(void) | |
791 | { | |
792 | int i; | |
793 | ||
d36a7a0d | 794 | if (torture_cleanup_begin()) |
0af3fe1e PM |
795 | return; |
796 | ||
c1c33b92 DB |
797 | /* |
798 | * Indicates early cleanup, meaning that the test has not run, | |
0d720287 HT |
799 | * such as when passing bogus args when loading the module. |
800 | * However cxt->cur_ops.init() may have been invoked, so beside | |
801 | * perform the underlying torture-specific cleanups, cur_ops.exit() | |
802 | * will be invoked if needed. | |
c1c33b92 | 803 | */ |
2ce77d16 | 804 | if (!cxt.lwsa && !cxt.lrsa) |
c1c33b92 DB |
805 | goto end; |
806 | ||
0af3fe1e | 807 | if (writer_tasks) { |
630952c2 | 808 | for (i = 0; i < cxt.nrealwriters_stress; i++) |
0af3fe1e PM |
809 | torture_stop_kthread(lock_torture_writer, |
810 | writer_tasks[i]); | |
811 | kfree(writer_tasks); | |
812 | writer_tasks = NULL; | |
813 | } | |
814 | ||
4f6332c1 | 815 | if (reader_tasks) { |
630952c2 | 816 | for (i = 0; i < cxt.nrealreaders_stress; i++) |
4f6332c1 DB |
817 | torture_stop_kthread(lock_torture_reader, |
818 | reader_tasks[i]); | |
819 | kfree(reader_tasks); | |
820 | reader_tasks = NULL; | |
821 | } | |
822 | ||
0af3fe1e PM |
823 | torture_stop_kthread(lock_torture_stats, stats_task); |
824 | lock_torture_stats_print(); /* -After- the stats thread is stopped! */ | |
825 | ||
630952c2 DB |
826 | if (atomic_read(&cxt.n_lock_torture_errors)) |
827 | lock_torture_print_module_parms(cxt.cur_ops, | |
0af3fe1e PM |
828 | "End of test: FAILURE"); |
829 | else if (torture_onoff_failures()) | |
630952c2 | 830 | lock_torture_print_module_parms(cxt.cur_ops, |
0af3fe1e PM |
831 | "End of test: LOCK_HOTPLUG"); |
832 | else | |
630952c2 | 833 | lock_torture_print_module_parms(cxt.cur_ops, |
0af3fe1e | 834 | "End of test: SUCCESS"); |
f4dbba59 YS |
835 | |
836 | kfree(cxt.lwsa); | |
a9d6938d | 837 | cxt.lwsa = NULL; |
f4dbba59 | 838 | kfree(cxt.lrsa); |
a9d6938d | 839 | cxt.lrsa = NULL; |
f4dbba59 | 840 | |
c1c33b92 | 841 | end: |
0d720287 HT |
842 | if (cxt.init_called) { |
843 | if (cxt.cur_ops->exit) | |
844 | cxt.cur_ops->exit(); | |
845 | cxt.init_called = false; | |
846 | } | |
d36a7a0d | 847 | torture_cleanup_end(); |
0af3fe1e PM |
848 | } |
849 | ||
850 | static int __init lock_torture_init(void) | |
851 | { | |
4f6332c1 | 852 | int i, j; |
0af3fe1e PM |
853 | int firsterr = 0; |
854 | static struct lock_torture_ops *torture_ops[] = { | |
e34191fa DB |
855 | &lock_busted_ops, |
856 | &spin_lock_ops, &spin_lock_irq_ops, | |
857 | &rw_lock_ops, &rw_lock_irq_ops, | |
858 | &mutex_lock_ops, | |
0186a6cb | 859 | &ww_mutex_lock_ops, |
095777c4 DB |
860 | #ifdef CONFIG_RT_MUTEXES |
861 | &rtmutex_lock_ops, | |
862 | #endif | |
e34191fa | 863 | &rwsem_lock_ops, |
617783dd | 864 | &percpu_rwsem_lock_ops, |
0af3fe1e PM |
865 | }; |
866 | ||
a2f2577d | 867 | if (!torture_init_begin(torture_type, verbose)) |
5228084e | 868 | return -EBUSY; |
0af3fe1e PM |
869 | |
870 | /* Process args and tell the world that the torturer is on the job. */ | |
871 | for (i = 0; i < ARRAY_SIZE(torture_ops); i++) { | |
630952c2 DB |
872 | cxt.cur_ops = torture_ops[i]; |
873 | if (strcmp(torture_type, cxt.cur_ops->name) == 0) | |
0af3fe1e PM |
874 | break; |
875 | } | |
876 | if (i == ARRAY_SIZE(torture_ops)) { | |
877 | pr_alert("lock-torture: invalid torture type: \"%s\"\n", | |
878 | torture_type); | |
879 | pr_alert("lock-torture types:"); | |
880 | for (i = 0; i < ARRAY_SIZE(torture_ops); i++) | |
881 | pr_alert(" %s", torture_ops[i]->name); | |
882 | pr_alert("\n"); | |
a36a9961 PM |
883 | firsterr = -EINVAL; |
884 | goto unwind; | |
0af3fe1e | 885 | } |
2ce77d16 | 886 | |
e5ace37d HT |
887 | if (nwriters_stress == 0 && |
888 | (!cxt.cur_ops->readlock || nreaders_stress == 0)) { | |
2ce77d16 DB |
889 | pr_alert("lock-torture: must run at least one locking thread\n"); |
890 | firsterr = -EINVAL; | |
891 | goto unwind; | |
892 | } | |
893 | ||
0d720287 | 894 | if (cxt.cur_ops->init) { |
a36a9961 | 895 | cxt.cur_ops->init(); |
0d720287 HT |
896 | cxt.init_called = true; |
897 | } | |
0af3fe1e PM |
898 | |
899 | if (nwriters_stress >= 0) | |
630952c2 | 900 | cxt.nrealwriters_stress = nwriters_stress; |
0af3fe1e | 901 | else |
630952c2 | 902 | cxt.nrealwriters_stress = 2 * num_online_cpus(); |
f095bfc0 DB |
903 | |
904 | #ifdef CONFIG_DEBUG_MUTEXES | |
c5d3c8ca | 905 | if (str_has_prefix(torture_type, "mutex")) |
630952c2 | 906 | cxt.debug_lock = true; |
f095bfc0 | 907 | #endif |
095777c4 | 908 | #ifdef CONFIG_DEBUG_RT_MUTEXES |
c5d3c8ca | 909 | if (str_has_prefix(torture_type, "rtmutex")) |
095777c4 DB |
910 | cxt.debug_lock = true; |
911 | #endif | |
f095bfc0 | 912 | #ifdef CONFIG_DEBUG_SPINLOCK |
c5d3c8ca CY |
913 | if ((str_has_prefix(torture_type, "spin")) || |
914 | (str_has_prefix(torture_type, "rw_lock"))) | |
630952c2 | 915 | cxt.debug_lock = true; |
f095bfc0 | 916 | #endif |
0af3fe1e PM |
917 | |
918 | /* Initialize the statistics so that each run gets its own numbers. */ | |
2ce77d16 | 919 | if (nwriters_stress) { |
d02c6b52 | 920 | lock_is_write_held = false; |
6da2ec56 KC |
921 | cxt.lwsa = kmalloc_array(cxt.nrealwriters_stress, |
922 | sizeof(*cxt.lwsa), | |
923 | GFP_KERNEL); | |
2ce77d16 DB |
924 | if (cxt.lwsa == NULL) { |
925 | VERBOSE_TOROUT_STRING("cxt.lwsa: Out of memory"); | |
926 | firsterr = -ENOMEM; | |
927 | goto unwind; | |
928 | } | |
0af3fe1e | 929 | |
2ce77d16 DB |
930 | for (i = 0; i < cxt.nrealwriters_stress; i++) { |
931 | cxt.lwsa[i].n_lock_fail = 0; | |
932 | cxt.lwsa[i].n_lock_acquired = 0; | |
933 | } | |
0af3fe1e PM |
934 | } |
935 | ||
630952c2 | 936 | if (cxt.cur_ops->readlock) { |
4f6332c1 | 937 | if (nreaders_stress >= 0) |
630952c2 | 938 | cxt.nrealreaders_stress = nreaders_stress; |
4f6332c1 DB |
939 | else { |
940 | /* | |
941 | * By default distribute evenly the number of | |
942 | * readers and writers. We still run the same number | |
943 | * of threads as the writer-only locks default. | |
944 | */ | |
945 | if (nwriters_stress < 0) /* user doesn't care */ | |
630952c2 DB |
946 | cxt.nrealwriters_stress = num_online_cpus(); |
947 | cxt.nrealreaders_stress = cxt.nrealwriters_stress; | |
4f6332c1 DB |
948 | } |
949 | ||
2ce77d16 | 950 | if (nreaders_stress) { |
d02c6b52 | 951 | lock_is_read_held = false; |
6da2ec56 KC |
952 | cxt.lrsa = kmalloc_array(cxt.nrealreaders_stress, |
953 | sizeof(*cxt.lrsa), | |
954 | GFP_KERNEL); | |
2ce77d16 DB |
955 | if (cxt.lrsa == NULL) { |
956 | VERBOSE_TOROUT_STRING("cxt.lrsa: Out of memory"); | |
957 | firsterr = -ENOMEM; | |
958 | kfree(cxt.lwsa); | |
959 | cxt.lwsa = NULL; | |
960 | goto unwind; | |
961 | } | |
962 | ||
963 | for (i = 0; i < cxt.nrealreaders_stress; i++) { | |
964 | cxt.lrsa[i].n_lock_fail = 0; | |
965 | cxt.lrsa[i].n_lock_acquired = 0; | |
966 | } | |
4f6332c1 DB |
967 | } |
968 | } | |
c1c33b92 | 969 | |
630952c2 | 970 | lock_torture_print_module_parms(cxt.cur_ops, "Start of test"); |
4f6332c1 DB |
971 | |
972 | /* Prepare torture context. */ | |
0af3fe1e PM |
973 | if (onoff_interval > 0) { |
974 | firsterr = torture_onoff_init(onoff_holdoff * HZ, | |
3a6cb58f | 975 | onoff_interval * HZ, NULL); |
0af3fe1e PM |
976 | if (firsterr) |
977 | goto unwind; | |
978 | } | |
979 | if (shuffle_interval > 0) { | |
980 | firsterr = torture_shuffle_init(shuffle_interval); | |
981 | if (firsterr) | |
982 | goto unwind; | |
983 | } | |
984 | if (shutdown_secs > 0) { | |
985 | firsterr = torture_shutdown_init(shutdown_secs, | |
986 | lock_torture_cleanup); | |
987 | if (firsterr) | |
988 | goto unwind; | |
989 | } | |
990 | if (stutter > 0) { | |
ff3bf92d | 991 | firsterr = torture_stutter_init(stutter, stutter); |
0af3fe1e PM |
992 | if (firsterr) |
993 | goto unwind; | |
994 | } | |
995 | ||
2ce77d16 | 996 | if (nwriters_stress) { |
6396bb22 KC |
997 | writer_tasks = kcalloc(cxt.nrealwriters_stress, |
998 | sizeof(writer_tasks[0]), | |
2ce77d16 DB |
999 | GFP_KERNEL); |
1000 | if (writer_tasks == NULL) { | |
1001 | VERBOSE_TOROUT_ERRSTRING("writer_tasks: Out of memory"); | |
1002 | firsterr = -ENOMEM; | |
1003 | goto unwind; | |
1004 | } | |
0af3fe1e | 1005 | } |
4f6332c1 | 1006 | |
630952c2 | 1007 | if (cxt.cur_ops->readlock) { |
6396bb22 KC |
1008 | reader_tasks = kcalloc(cxt.nrealreaders_stress, |
1009 | sizeof(reader_tasks[0]), | |
4f6332c1 DB |
1010 | GFP_KERNEL); |
1011 | if (reader_tasks == NULL) { | |
1012 | VERBOSE_TOROUT_ERRSTRING("reader_tasks: Out of memory"); | |
f4dbba59 YS |
1013 | kfree(writer_tasks); |
1014 | writer_tasks = NULL; | |
4f6332c1 DB |
1015 | firsterr = -ENOMEM; |
1016 | goto unwind; | |
1017 | } | |
1018 | } | |
1019 | ||
1020 | /* | |
1021 | * Create the kthreads and start torturing (oh, those poor little locks). | |
1022 | * | |
1023 | * TODO: Note that we interleave writers with readers, giving writers a | |
1024 | * slight advantage, by creating its kthread first. This can be modified | |
1025 | * for very specific needs, or even let the user choose the policy, if | |
1026 | * ever wanted. | |
1027 | */ | |
630952c2 DB |
1028 | for (i = 0, j = 0; i < cxt.nrealwriters_stress || |
1029 | j < cxt.nrealreaders_stress; i++, j++) { | |
1030 | if (i >= cxt.nrealwriters_stress) | |
4f6332c1 DB |
1031 | goto create_reader; |
1032 | ||
1033 | /* Create writer. */ | |
630952c2 | 1034 | firsterr = torture_create_kthread(lock_torture_writer, &cxt.lwsa[i], |
0af3fe1e PM |
1035 | writer_tasks[i]); |
1036 | if (firsterr) | |
1037 | goto unwind; | |
4f6332c1 DB |
1038 | |
1039 | create_reader: | |
630952c2 | 1040 | if (cxt.cur_ops->readlock == NULL || (j >= cxt.nrealreaders_stress)) |
4f6332c1 DB |
1041 | continue; |
1042 | /* Create reader. */ | |
630952c2 | 1043 | firsterr = torture_create_kthread(lock_torture_reader, &cxt.lrsa[j], |
4f6332c1 DB |
1044 | reader_tasks[j]); |
1045 | if (firsterr) | |
1046 | goto unwind; | |
0af3fe1e PM |
1047 | } |
1048 | if (stat_interval > 0) { | |
1049 | firsterr = torture_create_kthread(lock_torture_stats, NULL, | |
1050 | stats_task); | |
1051 | if (firsterr) | |
1052 | goto unwind; | |
1053 | } | |
1054 | torture_init_end(); | |
1055 | return 0; | |
1056 | ||
1057 | unwind: | |
1058 | torture_init_end(); | |
1059 | lock_torture_cleanup(); | |
6b74fa0a PM |
1060 | if (shutdown_secs) { |
1061 | WARN_ON(!IS_MODULE(CONFIG_LOCK_TORTURE_TEST)); | |
1062 | kernel_power_off(); | |
1063 | } | |
0af3fe1e PM |
1064 | return firsterr; |
1065 | } | |
1066 | ||
1067 | module_init(lock_torture_init); | |
1068 | module_exit(lock_torture_cleanup); |