1 // SPDX-License-Identifier: GPL-2.0+
3 * Module-based torture test facility for locking
5 * Copyright (C) IBM Corporation, 2014
7 * Authors: Paul E. McKenney <paulmck@linux.ibm.com>
8 * Davidlohr Bueso <dave@stgolabs.net>
9 * Based on kernel/rcu/torture.c.
12 #define pr_fmt(fmt) fmt
14 #include <linux/kernel.h>
15 #include <linux/module.h>
16 #include <linux/kthread.h>
17 #include <linux/sched/rt.h>
18 #include <linux/spinlock.h>
19 #include <linux/mutex.h>
20 #include <linux/rwsem.h>
21 #include <linux/smp.h>
22 #include <linux/interrupt.h>
23 #include <linux/sched.h>
24 #include <uapi/linux/sched/types.h>
25 #include <linux/rtmutex.h>
26 #include <linux/atomic.h>
27 #include <linux/moduleparam.h>
28 #include <linux/delay.h>
29 #include <linux/slab.h>
30 #include <linux/torture.h>
31 #include <linux/reboot.h>
33 MODULE_LICENSE("GPL");
34 MODULE_AUTHOR("Paul E. McKenney <paulmck@linux.ibm.com>");
36 torture_param(int, nwriters_stress, -1,
37 "Number of write-locking stress-test threads");
38 torture_param(int, nreaders_stress, -1,
39 "Number of read-locking stress-test threads");
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");
49 torture_param(int, verbose, 1,
50 "Enable verbose debugging printk()s");
52 static char *torture_type = "spin_lock";
53 module_param(torture_type, charp, 0444);
54 MODULE_PARM_DESC(torture_type,
55 "Type of lock to torture (spin_lock, spin_lock_irq, mutex_lock, ...)");
57 static struct task_struct *stats_task;
58 static struct task_struct **writer_tasks;
59 static struct task_struct **reader_tasks;
61 static bool lock_is_write_held;
62 static bool lock_is_read_held;
63 static unsigned long last_lock_release;
65 struct lock_stress_stats {
70 /* Forward reference. */
71 static void lock_torture_cleanup(void);
74 * Operations vector for selecting different types of tests.
76 struct lock_torture_ops {
79 int (*writelock)(void);
80 void (*write_delay)(struct torture_random_state *trsp);
81 void (*task_boost)(struct torture_random_state *trsp);
82 void (*writeunlock)(void);
83 int (*readlock)(void);
84 void (*read_delay)(struct torture_random_state *trsp);
85 void (*readunlock)(void);
87 unsigned long flags; /* for irq spinlocks */
91 struct lock_torture_cxt {
92 int nrealwriters_stress;
93 int nrealreaders_stress;
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 */
101 static struct lock_torture_cxt cxt = { 0, 0, false, false,
105 * Definitions for lock torture testing.
108 static int torture_lock_busted_write_lock(void)
110 return 0; /* BUGGY, do not use in real life!!! */
113 static void torture_lock_busted_write_delay(struct torture_random_state *trsp)
115 const unsigned long longdelay_ms = 100;
117 /* We want a long delay occasionally to force massive contention. */
118 if (!(torture_random(trsp) %
119 (cxt.nrealwriters_stress * 2000 * longdelay_ms)))
120 mdelay(longdelay_ms);
121 if (!(torture_random(trsp) % (cxt.nrealwriters_stress * 20000)))
122 torture_preempt_schedule(); /* Allow test to be preempted. */
125 static void torture_lock_busted_write_unlock(void)
127 /* BUGGY, do not use in real life!!! */
130 static void torture_boost_dummy(struct torture_random_state *trsp)
132 /* Only rtmutexes care about priority */
135 static struct lock_torture_ops lock_busted_ops = {
136 .writelock = torture_lock_busted_write_lock,
137 .write_delay = torture_lock_busted_write_delay,
138 .task_boost = torture_boost_dummy,
139 .writeunlock = torture_lock_busted_write_unlock,
143 .name = "lock_busted"
146 static DEFINE_SPINLOCK(torture_spinlock);
148 static int torture_spin_lock_write_lock(void) __acquires(torture_spinlock)
150 spin_lock(&torture_spinlock);
154 static void torture_spin_lock_write_delay(struct torture_random_state *trsp)
156 const unsigned long shortdelay_us = 2;
157 const unsigned long longdelay_ms = 100;
159 /* We want a short delay mostly to emulate likely code, and
160 * we want a long delay occasionally to force massive contention.
162 if (!(torture_random(trsp) %
163 (cxt.nrealwriters_stress * 2000 * longdelay_ms)))
164 mdelay(longdelay_ms);
165 if (!(torture_random(trsp) %
166 (cxt.nrealwriters_stress * 2 * shortdelay_us)))
167 udelay(shortdelay_us);
168 if (!(torture_random(trsp) % (cxt.nrealwriters_stress * 20000)))
169 torture_preempt_schedule(); /* Allow test to be preempted. */
172 static void torture_spin_lock_write_unlock(void) __releases(torture_spinlock)
174 spin_unlock(&torture_spinlock);
177 static struct lock_torture_ops spin_lock_ops = {
178 .writelock = torture_spin_lock_write_lock,
179 .write_delay = torture_spin_lock_write_delay,
180 .task_boost = torture_boost_dummy,
181 .writeunlock = torture_spin_lock_write_unlock,
188 static int torture_spin_lock_write_lock_irq(void)
189 __acquires(torture_spinlock)
193 spin_lock_irqsave(&torture_spinlock, flags);
194 cxt.cur_ops->flags = flags;
198 static void torture_lock_spin_write_unlock_irq(void)
199 __releases(torture_spinlock)
201 spin_unlock_irqrestore(&torture_spinlock, cxt.cur_ops->flags);
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,
207 .task_boost = torture_boost_dummy,
208 .writeunlock = torture_lock_spin_write_unlock_irq,
212 .name = "spin_lock_irq"
215 static DEFINE_RWLOCK(torture_rwlock);
217 static int torture_rwlock_write_lock(void) __acquires(torture_rwlock)
219 write_lock(&torture_rwlock);
223 static void torture_rwlock_write_delay(struct torture_random_state *trsp)
225 const unsigned long shortdelay_us = 2;
226 const unsigned long longdelay_ms = 100;
228 /* We want a short delay mostly to emulate likely code, and
229 * we want a long delay occasionally to force massive contention.
231 if (!(torture_random(trsp) %
232 (cxt.nrealwriters_stress * 2000 * longdelay_ms)))
233 mdelay(longdelay_ms);
235 udelay(shortdelay_us);
238 static void torture_rwlock_write_unlock(void) __releases(torture_rwlock)
240 write_unlock(&torture_rwlock);
243 static int torture_rwlock_read_lock(void) __acquires(torture_rwlock)
245 read_lock(&torture_rwlock);
249 static void torture_rwlock_read_delay(struct torture_random_state *trsp)
251 const unsigned long shortdelay_us = 10;
252 const unsigned long longdelay_ms = 100;
254 /* We want a short delay mostly to emulate likely code, and
255 * we want a long delay occasionally to force massive contention.
257 if (!(torture_random(trsp) %
258 (cxt.nrealreaders_stress * 2000 * longdelay_ms)))
259 mdelay(longdelay_ms);
261 udelay(shortdelay_us);
264 static void torture_rwlock_read_unlock(void) __releases(torture_rwlock)
266 read_unlock(&torture_rwlock);
269 static struct lock_torture_ops rw_lock_ops = {
270 .writelock = torture_rwlock_write_lock,
271 .write_delay = torture_rwlock_write_delay,
272 .task_boost = torture_boost_dummy,
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,
280 static int torture_rwlock_write_lock_irq(void) __acquires(torture_rwlock)
284 write_lock_irqsave(&torture_rwlock, flags);
285 cxt.cur_ops->flags = flags;
289 static void torture_rwlock_write_unlock_irq(void)
290 __releases(torture_rwlock)
292 write_unlock_irqrestore(&torture_rwlock, cxt.cur_ops->flags);
295 static int torture_rwlock_read_lock_irq(void) __acquires(torture_rwlock)
299 read_lock_irqsave(&torture_rwlock, flags);
300 cxt.cur_ops->flags = flags;
304 static void torture_rwlock_read_unlock_irq(void)
305 __releases(torture_rwlock)
307 read_unlock_irqrestore(&torture_rwlock, cxt.cur_ops->flags);
310 static struct lock_torture_ops rw_lock_irq_ops = {
311 .writelock = torture_rwlock_write_lock_irq,
312 .write_delay = torture_rwlock_write_delay,
313 .task_boost = torture_boost_dummy,
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"
321 static DEFINE_MUTEX(torture_mutex);
323 static int torture_mutex_lock(void) __acquires(torture_mutex)
325 mutex_lock(&torture_mutex);
329 static void torture_mutex_delay(struct torture_random_state *trsp)
331 const unsigned long longdelay_ms = 100;
333 /* We want a long delay occasionally to force massive contention. */
334 if (!(torture_random(trsp) %
335 (cxt.nrealwriters_stress * 2000 * longdelay_ms)))
336 mdelay(longdelay_ms * 5);
338 mdelay(longdelay_ms / 5);
339 if (!(torture_random(trsp) % (cxt.nrealwriters_stress * 20000)))
340 torture_preempt_schedule(); /* Allow test to be preempted. */
343 static void torture_mutex_unlock(void) __releases(torture_mutex)
345 mutex_unlock(&torture_mutex);
348 static struct lock_torture_ops mutex_lock_ops = {
349 .writelock = torture_mutex_lock,
350 .write_delay = torture_mutex_delay,
351 .task_boost = torture_boost_dummy,
352 .writeunlock = torture_mutex_unlock,
359 #include <linux/ww_mutex.h>
360 static DEFINE_WD_CLASS(torture_ww_class);
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);
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)
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;
377 locks[0].lock = &torture_ww_mutex_0;
378 list_add(&locks[0].link, &list);
380 locks[1].lock = &torture_ww_mutex_1;
381 list_add(&locks[1].link, &list);
383 locks[2].lock = &torture_ww_mutex_2;
384 list_add(&locks[2].link, &list);
386 ww_acquire_init(&ctx, &torture_ww_class);
388 list_for_each_entry(ll, &list, link) {
391 err = ww_mutex_lock(ll->lock, &ctx);
396 list_for_each_entry_continue_reverse(ln, &list, link)
397 ww_mutex_unlock(ln->lock);
402 ww_mutex_lock_slow(ll->lock, &ctx);
403 list_move(&ll->link, &list);
406 ww_acquire_fini(&ctx);
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)
415 ww_mutex_unlock(&torture_ww_mutex_0);
416 ww_mutex_unlock(&torture_ww_mutex_1);
417 ww_mutex_unlock(&torture_ww_mutex_2);
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,
428 .name = "ww_mutex_lock"
431 #ifdef CONFIG_RT_MUTEXES
432 static DEFINE_RT_MUTEX(torture_rtmutex);
434 static int torture_rtmutex_lock(void) __acquires(torture_rtmutex)
436 rt_mutex_lock(&torture_rtmutex);
440 static void torture_rtmutex_boost(struct torture_random_state *trsp)
442 const unsigned int factor = 50000; /* yes, quite arbitrary */
444 if (!rt_task(current)) {
446 * Boost priority once every ~50k operations. When the
447 * task tries to take the lock, the rtmutex it will account
448 * for the new priority, and do any corresponding pi-dance.
450 if (trsp && !(torture_random(trsp) %
451 (cxt.nrealwriters_stress * factor))) {
452 sched_set_fifo(current);
453 } else /* common case, do nothing */
457 * The task will remain boosted for another ~500k operations,
458 * then restored back to its original prio, and so forth.
460 * When @trsp is nil, we want to force-reset the task for
461 * stopping the kthread.
463 if (!trsp || !(torture_random(trsp) %
464 (cxt.nrealwriters_stress * factor * 2))) {
465 sched_set_normal(current, 0);
466 } else /* common case, do nothing */
471 static void torture_rtmutex_delay(struct torture_random_state *trsp)
473 const unsigned long shortdelay_us = 2;
474 const unsigned long longdelay_ms = 100;
477 * We want a short delay mostly to emulate likely code, and
478 * we want a long delay occasionally to force massive contention.
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);
486 if (!(torture_random(trsp) % (cxt.nrealwriters_stress * 20000)))
487 torture_preempt_schedule(); /* Allow test to be preempted. */
490 static void torture_rtmutex_unlock(void) __releases(torture_rtmutex)
492 rt_mutex_unlock(&torture_rtmutex);
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,
503 .name = "rtmutex_lock"
507 static DECLARE_RWSEM(torture_rwsem);
508 static int torture_rwsem_down_write(void) __acquires(torture_rwsem)
510 down_write(&torture_rwsem);
514 static void torture_rwsem_write_delay(struct torture_random_state *trsp)
516 const unsigned long longdelay_ms = 100;
518 /* We want a long delay occasionally to force massive contention. */
519 if (!(torture_random(trsp) %
520 (cxt.nrealwriters_stress * 2000 * longdelay_ms)))
521 mdelay(longdelay_ms * 10);
523 mdelay(longdelay_ms / 10);
524 if (!(torture_random(trsp) % (cxt.nrealwriters_stress * 20000)))
525 torture_preempt_schedule(); /* Allow test to be preempted. */
528 static void torture_rwsem_up_write(void) __releases(torture_rwsem)
530 up_write(&torture_rwsem);
533 static int torture_rwsem_down_read(void) __acquires(torture_rwsem)
535 down_read(&torture_rwsem);
539 static void torture_rwsem_read_delay(struct torture_random_state *trsp)
541 const unsigned long longdelay_ms = 100;
543 /* We want a long delay occasionally to force massive contention. */
544 if (!(torture_random(trsp) %
545 (cxt.nrealreaders_stress * 2000 * longdelay_ms)))
546 mdelay(longdelay_ms * 2);
548 mdelay(longdelay_ms / 2);
549 if (!(torture_random(trsp) % (cxt.nrealreaders_stress * 20000)))
550 torture_preempt_schedule(); /* Allow test to be preempted. */
553 static void torture_rwsem_up_read(void) __releases(torture_rwsem)
555 up_read(&torture_rwsem);
558 static struct lock_torture_ops rwsem_lock_ops = {
559 .writelock = torture_rwsem_down_write,
560 .write_delay = torture_rwsem_write_delay,
561 .task_boost = torture_boost_dummy,
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,
569 #include <linux/percpu-rwsem.h>
570 static struct percpu_rw_semaphore pcpu_rwsem;
572 static void torture_percpu_rwsem_init(void)
574 BUG_ON(percpu_init_rwsem(&pcpu_rwsem));
577 static void torture_percpu_rwsem_exit(void)
579 percpu_free_rwsem(&pcpu_rwsem);
582 static int torture_percpu_rwsem_down_write(void) __acquires(pcpu_rwsem)
584 percpu_down_write(&pcpu_rwsem);
588 static void torture_percpu_rwsem_up_write(void) __releases(pcpu_rwsem)
590 percpu_up_write(&pcpu_rwsem);
593 static int torture_percpu_rwsem_down_read(void) __acquires(pcpu_rwsem)
595 percpu_down_read(&pcpu_rwsem);
599 static void torture_percpu_rwsem_up_read(void) __releases(pcpu_rwsem)
601 percpu_up_read(&pcpu_rwsem);
604 static struct lock_torture_ops percpu_rwsem_lock_ops = {
605 .init = torture_percpu_rwsem_init,
606 .exit = torture_percpu_rwsem_exit,
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"
618 * Lock torture writer kthread. Repeatedly acquires and releases
619 * the lock, checking for duplicate acquisitions.
621 static int lock_torture_writer(void *arg)
623 struct lock_stress_stats *lwsp = arg;
624 DEFINE_TORTURE_RANDOM(rand);
626 VERBOSE_TOROUT_STRING("lock_torture_writer task started");
627 set_user_nice(current, MAX_NICE);
630 if ((torture_random(&rand) & 0xfffff) == 0)
631 schedule_timeout_uninterruptible(1);
633 cxt.cur_ops->task_boost(&rand);
634 cxt.cur_ops->writelock();
635 if (WARN_ON_ONCE(lock_is_write_held))
637 lock_is_write_held = true;
638 if (WARN_ON_ONCE(lock_is_read_held))
639 lwsp->n_lock_fail++; /* rare, but... */
641 lwsp->n_lock_acquired++;
642 cxt.cur_ops->write_delay(&rand);
643 lock_is_write_held = false;
644 WRITE_ONCE(last_lock_release, jiffies);
645 cxt.cur_ops->writeunlock();
647 stutter_wait("lock_torture_writer");
648 } while (!torture_must_stop());
650 cxt.cur_ops->task_boost(NULL); /* reset prio */
651 torture_kthread_stopping("lock_torture_writer");
656 * Lock torture reader kthread. Repeatedly acquires and releases
659 static int lock_torture_reader(void *arg)
661 struct lock_stress_stats *lrsp = arg;
662 DEFINE_TORTURE_RANDOM(rand);
664 VERBOSE_TOROUT_STRING("lock_torture_reader task started");
665 set_user_nice(current, MAX_NICE);
668 if ((torture_random(&rand) & 0xfffff) == 0)
669 schedule_timeout_uninterruptible(1);
671 cxt.cur_ops->readlock();
672 lock_is_read_held = true;
673 if (WARN_ON_ONCE(lock_is_write_held))
674 lrsp->n_lock_fail++; /* rare, but... */
676 lrsp->n_lock_acquired++;
677 cxt.cur_ops->read_delay(&rand);
678 lock_is_read_held = false;
679 cxt.cur_ops->readunlock();
681 stutter_wait("lock_torture_reader");
682 } while (!torture_must_stop());
683 torture_kthread_stopping("lock_torture_reader");
688 * Create an lock-torture-statistics message in the specified buffer.
690 static void __torture_print_stats(char *page,
691 struct lock_stress_stats *statp, bool write)
695 long max = 0, min = statp ? statp[0].n_lock_acquired : 0;
698 n_stress = write ? cxt.nrealwriters_stress : cxt.nrealreaders_stress;
699 for (i = 0; i < n_stress; i++) {
700 if (statp[i].n_lock_fail)
702 sum += statp[i].n_lock_acquired;
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;
708 page += sprintf(page,
709 "%s: Total: %lld Max/Min: %ld/%ld %s Fail: %d %s\n",
710 write ? "Writes" : "Reads ",
712 !onoff_interval && max / 2 > min ? "???" : "",
713 fail, fail ? "!!!" : "");
715 atomic_inc(&cxt.n_lock_torture_errors);
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
726 static void lock_torture_stats_print(void)
728 int size = cxt.nrealwriters_stress * 200 + 8192;
731 if (cxt.cur_ops->readlock)
732 size += cxt.nrealreaders_stress * 200 + 8192;
734 buf = kmalloc(size, GFP_KERNEL);
736 pr_err("lock_torture_stats_print: Out of memory, need: %d",
741 __torture_print_stats(buf, cxt.lwsa, true);
745 if (cxt.cur_ops->readlock) {
746 buf = kmalloc(size, GFP_KERNEL);
748 pr_err("lock_torture_stats_print: Out of memory, need: %d",
753 __torture_print_stats(buf, cxt.lrsa, false);
760 * Periodically prints torture statistics, if periodic statistics printing
761 * was specified via the stat_interval module parameter.
763 * No need to worry about fullstop here, since this one doesn't reference
764 * volatile state or register callbacks.
766 static int lock_torture_stats(void *arg)
768 VERBOSE_TOROUT_STRING("lock_torture_stats task started");
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");
779 lock_torture_print_module_parms(struct lock_torture_ops *cur_ops,
782 pr_alert("%s" TORTURE_FLAG
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",
784 torture_type, tag, cxt.debug_lock ? " [debug]": "",
785 cxt.nrealwriters_stress, cxt.nrealreaders_stress, stat_interval,
786 verbose, shuffle_interval, stutter, shutdown_secs,
787 onoff_interval, onoff_holdoff);
790 static void lock_torture_cleanup(void)
794 if (torture_cleanup_begin())
798 * Indicates early cleanup, meaning that the test has not run,
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.
804 if (!cxt.lwsa && !cxt.lrsa)
808 for (i = 0; i < cxt.nrealwriters_stress; i++)
809 torture_stop_kthread(lock_torture_writer,
816 for (i = 0; i < cxt.nrealreaders_stress; i++)
817 torture_stop_kthread(lock_torture_reader,
823 torture_stop_kthread(lock_torture_stats, stats_task);
824 lock_torture_stats_print(); /* -After- the stats thread is stopped! */
826 if (atomic_read(&cxt.n_lock_torture_errors))
827 lock_torture_print_module_parms(cxt.cur_ops,
828 "End of test: FAILURE");
829 else if (torture_onoff_failures())
830 lock_torture_print_module_parms(cxt.cur_ops,
831 "End of test: LOCK_HOTPLUG");
833 lock_torture_print_module_parms(cxt.cur_ops,
834 "End of test: SUCCESS");
842 if (cxt.init_called) {
843 if (cxt.cur_ops->exit)
845 cxt.init_called = false;
847 torture_cleanup_end();
850 static int __init lock_torture_init(void)
854 static struct lock_torture_ops *torture_ops[] = {
856 &spin_lock_ops, &spin_lock_irq_ops,
857 &rw_lock_ops, &rw_lock_irq_ops,
860 #ifdef CONFIG_RT_MUTEXES
864 &percpu_rwsem_lock_ops,
867 if (!torture_init_begin(torture_type, verbose))
870 /* Process args and tell the world that the torturer is on the job. */
871 for (i = 0; i < ARRAY_SIZE(torture_ops); i++) {
872 cxt.cur_ops = torture_ops[i];
873 if (strcmp(torture_type, cxt.cur_ops->name) == 0)
876 if (i == ARRAY_SIZE(torture_ops)) {
877 pr_alert("lock-torture: invalid torture type: \"%s\"\n",
879 pr_alert("lock-torture types:");
880 for (i = 0; i < ARRAY_SIZE(torture_ops); i++)
881 pr_alert(" %s", torture_ops[i]->name);
887 if (nwriters_stress == 0 &&
888 (!cxt.cur_ops->readlock || nreaders_stress == 0)) {
889 pr_alert("lock-torture: must run at least one locking thread\n");
894 if (cxt.cur_ops->init) {
896 cxt.init_called = true;
899 if (nwriters_stress >= 0)
900 cxt.nrealwriters_stress = nwriters_stress;
902 cxt.nrealwriters_stress = 2 * num_online_cpus();
904 #ifdef CONFIG_DEBUG_MUTEXES
905 if (str_has_prefix(torture_type, "mutex"))
906 cxt.debug_lock = true;
908 #ifdef CONFIG_DEBUG_RT_MUTEXES
909 if (str_has_prefix(torture_type, "rtmutex"))
910 cxt.debug_lock = true;
912 #ifdef CONFIG_DEBUG_SPINLOCK
913 if ((str_has_prefix(torture_type, "spin")) ||
914 (str_has_prefix(torture_type, "rw_lock")))
915 cxt.debug_lock = true;
918 /* Initialize the statistics so that each run gets its own numbers. */
919 if (nwriters_stress) {
920 lock_is_write_held = false;
921 cxt.lwsa = kmalloc_array(cxt.nrealwriters_stress,
924 if (cxt.lwsa == NULL) {
925 VERBOSE_TOROUT_STRING("cxt.lwsa: Out of memory");
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;
936 if (cxt.cur_ops->readlock) {
937 if (nreaders_stress >= 0)
938 cxt.nrealreaders_stress = nreaders_stress;
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.
945 if (nwriters_stress < 0) /* user doesn't care */
946 cxt.nrealwriters_stress = num_online_cpus();
947 cxt.nrealreaders_stress = cxt.nrealwriters_stress;
950 if (nreaders_stress) {
951 lock_is_read_held = false;
952 cxt.lrsa = kmalloc_array(cxt.nrealreaders_stress,
955 if (cxt.lrsa == NULL) {
956 VERBOSE_TOROUT_STRING("cxt.lrsa: Out of memory");
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;
970 lock_torture_print_module_parms(cxt.cur_ops, "Start of test");
972 /* Prepare torture context. */
973 if (onoff_interval > 0) {
974 firsterr = torture_onoff_init(onoff_holdoff * HZ,
975 onoff_interval * HZ, NULL);
979 if (shuffle_interval > 0) {
980 firsterr = torture_shuffle_init(shuffle_interval);
984 if (shutdown_secs > 0) {
985 firsterr = torture_shutdown_init(shutdown_secs,
986 lock_torture_cleanup);
991 firsterr = torture_stutter_init(stutter, stutter);
996 if (nwriters_stress) {
997 writer_tasks = kcalloc(cxt.nrealwriters_stress,
998 sizeof(writer_tasks[0]),
1000 if (writer_tasks == NULL) {
1001 VERBOSE_TOROUT_ERRSTRING("writer_tasks: Out of memory");
1007 if (cxt.cur_ops->readlock) {
1008 reader_tasks = kcalloc(cxt.nrealreaders_stress,
1009 sizeof(reader_tasks[0]),
1011 if (reader_tasks == NULL) {
1012 VERBOSE_TOROUT_ERRSTRING("reader_tasks: Out of memory");
1013 kfree(writer_tasks);
1014 writer_tasks = NULL;
1021 * Create the kthreads and start torturing (oh, those poor little locks).
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
1028 for (i = 0, j = 0; i < cxt.nrealwriters_stress ||
1029 j < cxt.nrealreaders_stress; i++, j++) {
1030 if (i >= cxt.nrealwriters_stress)
1033 /* Create writer. */
1034 firsterr = torture_create_kthread(lock_torture_writer, &cxt.lwsa[i],
1040 if (cxt.cur_ops->readlock == NULL || (j >= cxt.nrealreaders_stress))
1042 /* Create reader. */
1043 firsterr = torture_create_kthread(lock_torture_reader, &cxt.lrsa[j],
1048 if (stat_interval > 0) {
1049 firsterr = torture_create_kthread(lock_torture_stats, NULL,
1059 lock_torture_cleanup();
1060 if (shutdown_secs) {
1061 WARN_ON(!IS_MODULE(CONFIG_LOCK_TORTURE_TEST));
1067 module_init(lock_torture_init);
1068 module_exit(lock_torture_cleanup);