1 // SPDX-License-Identifier: GPL-2.0+
3 * Common functions for in-kernel torture tests.
5 * Copyright (C) IBM Corporation, 2014
7 * Author: Paul E. McKenney <paulmck@linux.ibm.com>
8 * Based on kernel/rcu/torture.c.
11 #define pr_fmt(fmt) fmt
13 #include <linux/types.h>
14 #include <linux/kernel.h>
15 #include <linux/init.h>
16 #include <linux/module.h>
17 #include <linux/kthread.h>
18 #include <linux/err.h>
19 #include <linux/spinlock.h>
20 #include <linux/smp.h>
21 #include <linux/interrupt.h>
22 #include <linux/sched.h>
23 #include <linux/sched/clock.h>
24 #include <linux/atomic.h>
25 #include <linux/bitops.h>
26 #include <linux/completion.h>
27 #include <linux/moduleparam.h>
28 #include <linux/percpu.h>
29 #include <linux/notifier.h>
30 #include <linux/reboot.h>
31 #include <linux/freezer.h>
32 #include <linux/cpu.h>
33 #include <linux/delay.h>
34 #include <linux/stat.h>
35 #include <linux/slab.h>
36 #include <linux/trace_clock.h>
37 #include <linux/ktime.h>
38 #include <asm/byteorder.h>
39 #include <linux/torture.h>
42 MODULE_LICENSE("GPL");
43 MODULE_AUTHOR("Paul E. McKenney <paulmck@linux.ibm.com>");
45 static bool disable_onoff_at_boot;
46 module_param(disable_onoff_at_boot, bool, 0444);
48 static bool ftrace_dump_at_shutdown;
49 module_param(ftrace_dump_at_shutdown, bool, 0444);
51 static int verbose_sleep_frequency;
52 module_param(verbose_sleep_frequency, int, 0444);
54 static int verbose_sleep_duration = 1;
55 module_param(verbose_sleep_duration, int, 0444);
57 static char *torture_type;
60 /* Mediate rmmod and system shutdown. Concurrent rmmod & shutdown illegal! */
61 #define FULLSTOP_DONTSTOP 0 /* Normal operation. */
62 #define FULLSTOP_SHUTDOWN 1 /* System shutdown with torture running. */
63 #define FULLSTOP_RMMOD 2 /* Normal rmmod of torture. */
64 static int fullstop = FULLSTOP_RMMOD;
65 static DEFINE_MUTEX(fullstop_mutex);
67 static atomic_t verbose_sleep_counter;
70 * Sleep if needed from VERBOSE_TOROUT*().
72 void verbose_torout_sleep(void)
74 if (verbose_sleep_frequency > 0 &&
75 verbose_sleep_duration > 0 &&
76 !(atomic_inc_return(&verbose_sleep_counter) % verbose_sleep_frequency))
77 schedule_timeout_uninterruptible(verbose_sleep_duration);
79 EXPORT_SYMBOL_GPL(verbose_torout_sleep);
82 * Schedule a high-resolution-timer sleep in nanoseconds, with a 32-bit
83 * nanosecond random fuzz. This function and its friends desynchronize
84 * testing from the timer wheel.
86 int torture_hrtimeout_ns(ktime_t baset_ns, u32 fuzzt_ns, struct torture_random_state *trsp)
88 ktime_t hto = baset_ns;
91 hto += (torture_random(trsp) >> 3) % fuzzt_ns;
92 set_current_state(TASK_UNINTERRUPTIBLE);
93 return schedule_hrtimeout(&hto, HRTIMER_MODE_REL);
95 EXPORT_SYMBOL_GPL(torture_hrtimeout_ns);
98 * Schedule a high-resolution-timer sleep in microseconds, with a 32-bit
99 * nanosecond (not microsecond!) random fuzz.
101 int torture_hrtimeout_us(u32 baset_us, u32 fuzzt_ns, struct torture_random_state *trsp)
103 ktime_t baset_ns = baset_us * NSEC_PER_USEC;
105 return torture_hrtimeout_ns(baset_ns, fuzzt_ns, trsp);
107 EXPORT_SYMBOL_GPL(torture_hrtimeout_us);
110 * Schedule a high-resolution-timer sleep in milliseconds, with a 32-bit
111 * microsecond (not millisecond!) random fuzz.
113 int torture_hrtimeout_ms(u32 baset_ms, u32 fuzzt_us, struct torture_random_state *trsp)
115 ktime_t baset_ns = baset_ms * NSEC_PER_MSEC;
118 if ((u32)~0U / NSEC_PER_USEC < fuzzt_us)
121 fuzzt_ns = fuzzt_us * NSEC_PER_USEC;
122 return torture_hrtimeout_ns(baset_ns, fuzzt_ns, trsp);
124 EXPORT_SYMBOL_GPL(torture_hrtimeout_ms);
127 * Schedule a high-resolution-timer sleep in jiffies, with an
128 * implied one-jiffy random fuzz. This is intended to replace calls to
129 * schedule_timeout_interruptible() and friends.
131 int torture_hrtimeout_jiffies(u32 baset_j, struct torture_random_state *trsp)
133 ktime_t baset_ns = jiffies_to_nsecs(baset_j);
135 return torture_hrtimeout_ns(baset_ns, jiffies_to_nsecs(1), trsp);
137 EXPORT_SYMBOL_GPL(torture_hrtimeout_jiffies);
140 * Schedule a high-resolution-timer sleep in milliseconds, with a 32-bit
141 * millisecond (not second!) random fuzz.
143 int torture_hrtimeout_s(u32 baset_s, u32 fuzzt_ms, struct torture_random_state *trsp)
145 ktime_t baset_ns = baset_s * NSEC_PER_SEC;
148 if ((u32)~0U / NSEC_PER_MSEC < fuzzt_ms)
151 fuzzt_ns = fuzzt_ms * NSEC_PER_MSEC;
152 return torture_hrtimeout_ns(baset_ns, fuzzt_ns, trsp);
154 EXPORT_SYMBOL_GPL(torture_hrtimeout_s);
156 #ifdef CONFIG_HOTPLUG_CPU
159 * Variables for online-offline handling. Only present if CPU hotplug
160 * is enabled, otherwise does nothing.
163 static struct task_struct *onoff_task;
164 static long onoff_holdoff;
165 static long onoff_interval;
166 static torture_ofl_func *onoff_f;
167 static long n_offline_attempts;
168 static long n_offline_successes;
169 static unsigned long sum_offline;
170 static int min_offline = -1;
171 static int max_offline;
172 static long n_online_attempts;
173 static long n_online_successes;
174 static unsigned long sum_online;
175 static int min_online = -1;
176 static int max_online;
179 * Attempt to take a CPU offline. Return false if the CPU is already
180 * offline or if it is not subject to CPU-hotplug operations. The
181 * caller can detect other failures by looking at the statistics.
183 bool torture_offline(int cpu, long *n_offl_attempts, long *n_offl_successes,
184 unsigned long *sum_offl, int *min_offl, int *max_offl)
189 unsigned long starttime;
191 if (!cpu_online(cpu) || !cpu_is_hotpluggable(cpu))
193 if (num_online_cpus() <= 1)
194 return false; /* Can't offline the last CPU. */
197 pr_alert("%s" TORTURE_FLAG
198 "torture_onoff task: offlining %d\n",
201 (*n_offl_attempts)++;
202 ret = remove_cpu(cpu);
205 if (!rcu_inkernel_boot_has_ended() && ret == -EBUSY) {
206 // PCI probe frequently disables hotplug during boot.
207 (*n_offl_attempts)--;
208 s = " (-EBUSY forgiven during boot)";
211 pr_alert("%s" TORTURE_FLAG
212 "torture_onoff task: offline %d failed%s: errno %d\n",
213 torture_type, cpu, s, ret);
216 pr_alert("%s" TORTURE_FLAG
217 "torture_onoff task: offlined %d\n",
221 (*n_offl_successes)++;
222 delta = jiffies - starttime;
228 if (*min_offl > delta)
230 if (*max_offl < delta)
236 EXPORT_SYMBOL_GPL(torture_offline);
239 * Attempt to bring a CPU online. Return false if the CPU is already
240 * online or if it is not subject to CPU-hotplug operations. The
241 * caller can detect other failures by looking at the statistics.
243 bool torture_online(int cpu, long *n_onl_attempts, long *n_onl_successes,
244 unsigned long *sum_onl, int *min_onl, int *max_onl)
249 unsigned long starttime;
251 if (cpu_online(cpu) || !cpu_is_hotpluggable(cpu))
255 pr_alert("%s" TORTURE_FLAG
256 "torture_onoff task: onlining %d\n",
263 if (!rcu_inkernel_boot_has_ended() && ret == -EBUSY) {
264 // PCI probe frequently disables hotplug during boot.
266 s = " (-EBUSY forgiven during boot)";
269 pr_alert("%s" TORTURE_FLAG
270 "torture_onoff task: online %d failed%s: errno %d\n",
271 torture_type, cpu, s, ret);
274 pr_alert("%s" TORTURE_FLAG
275 "torture_onoff task: onlined %d\n",
277 (*n_onl_successes)++;
278 delta = jiffies - starttime;
284 if (*min_onl > delta)
286 if (*max_onl < delta)
292 EXPORT_SYMBOL_GPL(torture_online);
295 * Execute random CPU-hotplug operations at the interval specified
296 * by the onoff_interval.
299 torture_onoff(void *arg)
303 DEFINE_TORTURE_RANDOM(rand);
306 VERBOSE_TOROUT_STRING("torture_onoff task started");
307 for_each_online_cpu(cpu)
310 if (!IS_MODULE(CONFIG_TORTURE_TEST)) {
311 for_each_possible_cpu(cpu) {
315 if (ret && verbose) {
316 pr_alert("%s" TORTURE_FLAG
317 "%s: Initial online %d: errno %d\n",
318 __func__, torture_type, cpu, ret);
324 VERBOSE_TOROUT_STRING("Only one CPU, so CPU-hotplug testing is disabled");
328 if (onoff_holdoff > 0) {
329 VERBOSE_TOROUT_STRING("torture_onoff begin holdoff");
330 schedule_timeout_interruptible(onoff_holdoff);
331 VERBOSE_TOROUT_STRING("torture_onoff end holdoff");
333 while (!torture_must_stop()) {
334 if (disable_onoff_at_boot && !rcu_inkernel_boot_has_ended()) {
335 schedule_timeout_interruptible(HZ / 10);
338 cpu = (torture_random(&rand) >> 4) % (maxcpu + 1);
339 if (!torture_offline(cpu,
340 &n_offline_attempts, &n_offline_successes,
341 &sum_offline, &min_offline, &max_offline))
343 &n_online_attempts, &n_online_successes,
344 &sum_online, &min_online, &max_online);
345 schedule_timeout_interruptible(onoff_interval);
349 torture_kthread_stopping("torture_onoff");
353 #endif /* #ifdef CONFIG_HOTPLUG_CPU */
356 * Initiate online-offline handling.
358 int torture_onoff_init(long ooholdoff, long oointerval, torture_ofl_func *f)
360 #ifdef CONFIG_HOTPLUG_CPU
361 onoff_holdoff = ooholdoff;
362 onoff_interval = oointerval;
364 if (onoff_interval <= 0)
366 return torture_create_kthread(torture_onoff, NULL, onoff_task);
367 #else /* #ifdef CONFIG_HOTPLUG_CPU */
369 #endif /* #else #ifdef CONFIG_HOTPLUG_CPU */
371 EXPORT_SYMBOL_GPL(torture_onoff_init);
374 * Clean up after online/offline testing.
376 static void torture_onoff_cleanup(void)
378 #ifdef CONFIG_HOTPLUG_CPU
379 if (onoff_task == NULL)
381 VERBOSE_TOROUT_STRING("Stopping torture_onoff task");
382 kthread_stop(onoff_task);
384 #endif /* #ifdef CONFIG_HOTPLUG_CPU */
388 * Print online/offline testing statistics.
390 void torture_onoff_stats(void)
392 #ifdef CONFIG_HOTPLUG_CPU
393 pr_cont("onoff: %ld/%ld:%ld/%ld %d,%d:%d,%d %lu:%lu (HZ=%d) ",
394 n_online_successes, n_online_attempts,
395 n_offline_successes, n_offline_attempts,
396 min_online, max_online,
397 min_offline, max_offline,
398 sum_online, sum_offline, HZ);
399 #endif /* #ifdef CONFIG_HOTPLUG_CPU */
401 EXPORT_SYMBOL_GPL(torture_onoff_stats);
404 * Were all the online/offline operations successful?
406 bool torture_onoff_failures(void)
408 #ifdef CONFIG_HOTPLUG_CPU
409 return n_online_successes != n_online_attempts ||
410 n_offline_successes != n_offline_attempts;
411 #else /* #ifdef CONFIG_HOTPLUG_CPU */
413 #endif /* #else #ifdef CONFIG_HOTPLUG_CPU */
415 EXPORT_SYMBOL_GPL(torture_onoff_failures);
417 #define TORTURE_RANDOM_MULT 39916801 /* prime */
418 #define TORTURE_RANDOM_ADD 479001701 /* prime */
419 #define TORTURE_RANDOM_REFRESH 10000
422 * Crude but fast random-number generator. Uses a linear congruential
423 * generator, with occasional help from cpu_clock().
426 torture_random(struct torture_random_state *trsp)
428 if (--trsp->trs_count < 0) {
429 trsp->trs_state += (unsigned long)local_clock();
430 trsp->trs_count = TORTURE_RANDOM_REFRESH;
432 trsp->trs_state = trsp->trs_state * TORTURE_RANDOM_MULT +
434 return swahw32(trsp->trs_state);
436 EXPORT_SYMBOL_GPL(torture_random);
439 * Variables for shuffling. The idea is to ensure that each CPU stays
440 * idle for an extended period to test interactions with dyntick idle,
441 * as well as interactions with any per-CPU variables.
443 struct shuffle_task {
444 struct list_head st_l;
445 struct task_struct *st_t;
448 static long shuffle_interval; /* In jiffies. */
449 static struct task_struct *shuffler_task;
450 static cpumask_var_t shuffle_tmp_mask;
451 static int shuffle_idle_cpu; /* Force all torture tasks off this CPU */
452 static struct list_head shuffle_task_list = LIST_HEAD_INIT(shuffle_task_list);
453 static DEFINE_MUTEX(shuffle_task_mutex);
456 * Register a task to be shuffled. If there is no memory, just splat
457 * and don't bother registering.
459 void torture_shuffle_task_register(struct task_struct *tp)
461 struct shuffle_task *stp;
463 if (WARN_ON_ONCE(tp == NULL))
465 stp = kmalloc(sizeof(*stp), GFP_KERNEL);
466 if (WARN_ON_ONCE(stp == NULL))
469 mutex_lock(&shuffle_task_mutex);
470 list_add(&stp->st_l, &shuffle_task_list);
471 mutex_unlock(&shuffle_task_mutex);
473 EXPORT_SYMBOL_GPL(torture_shuffle_task_register);
476 * Unregister all tasks, for example, at the end of the torture run.
478 static void torture_shuffle_task_unregister_all(void)
480 struct shuffle_task *stp;
481 struct shuffle_task *p;
483 mutex_lock(&shuffle_task_mutex);
484 list_for_each_entry_safe(stp, p, &shuffle_task_list, st_l) {
485 list_del(&stp->st_l);
488 mutex_unlock(&shuffle_task_mutex);
491 /* Shuffle tasks such that we allow shuffle_idle_cpu to become idle.
492 * A special case is when shuffle_idle_cpu = -1, in which case we allow
493 * the tasks to run on all CPUs.
495 static void torture_shuffle_tasks(void)
497 struct shuffle_task *stp;
499 cpumask_setall(shuffle_tmp_mask);
502 /* No point in shuffling if there is only one online CPU (ex: UP) */
503 if (num_online_cpus() == 1) {
508 /* Advance to the next CPU. Upon overflow, don't idle any CPUs. */
509 shuffle_idle_cpu = cpumask_next(shuffle_idle_cpu, shuffle_tmp_mask);
510 if (shuffle_idle_cpu >= nr_cpu_ids)
511 shuffle_idle_cpu = -1;
513 cpumask_clear_cpu(shuffle_idle_cpu, shuffle_tmp_mask);
515 mutex_lock(&shuffle_task_mutex);
516 list_for_each_entry(stp, &shuffle_task_list, st_l)
517 set_cpus_allowed_ptr(stp->st_t, shuffle_tmp_mask);
518 mutex_unlock(&shuffle_task_mutex);
523 /* Shuffle tasks across CPUs, with the intent of allowing each CPU in the
524 * system to become idle at a time and cut off its timer ticks. This is meant
525 * to test the support for such tickless idle CPU in RCU.
527 static int torture_shuffle(void *arg)
529 VERBOSE_TOROUT_STRING("torture_shuffle task started");
531 schedule_timeout_interruptible(shuffle_interval);
532 torture_shuffle_tasks();
533 torture_shutdown_absorb("torture_shuffle");
534 } while (!torture_must_stop());
535 torture_kthread_stopping("torture_shuffle");
540 * Start the shuffler, with shuffint in jiffies.
542 int torture_shuffle_init(long shuffint)
544 shuffle_interval = shuffint;
546 shuffle_idle_cpu = -1;
548 if (!alloc_cpumask_var(&shuffle_tmp_mask, GFP_KERNEL)) {
549 VERBOSE_TOROUT_ERRSTRING("Failed to alloc mask");
553 /* Create the shuffler thread */
554 return torture_create_kthread(torture_shuffle, NULL, shuffler_task);
556 EXPORT_SYMBOL_GPL(torture_shuffle_init);
559 * Stop the shuffling.
561 static void torture_shuffle_cleanup(void)
563 torture_shuffle_task_unregister_all();
565 VERBOSE_TOROUT_STRING("Stopping torture_shuffle task");
566 kthread_stop(shuffler_task);
567 free_cpumask_var(shuffle_tmp_mask);
569 shuffler_task = NULL;
573 * Variables for auto-shutdown. This allows "lights out" torture runs
574 * to be fully scripted.
576 static struct task_struct *shutdown_task;
577 static ktime_t shutdown_time; /* time to system shutdown. */
578 static void (*torture_shutdown_hook)(void);
581 * Absorb kthreads into a kernel function that won't return, so that
582 * they won't ever access module text or data again.
584 void torture_shutdown_absorb(const char *title)
586 while (READ_ONCE(fullstop) == FULLSTOP_SHUTDOWN) {
587 pr_notice("torture thread %s parking due to system shutdown\n",
589 schedule_timeout_uninterruptible(MAX_SCHEDULE_TIMEOUT);
592 EXPORT_SYMBOL_GPL(torture_shutdown_absorb);
595 * Cause the torture test to shutdown the system after the test has
596 * run for the time specified by the shutdown_secs parameter.
598 static int torture_shutdown(void *arg)
602 VERBOSE_TOROUT_STRING("torture_shutdown task started");
603 ktime_snap = ktime_get();
604 while (ktime_before(ktime_snap, shutdown_time) &&
605 !torture_must_stop()) {
607 pr_alert("%s" TORTURE_FLAG
608 "torture_shutdown task: %llu ms remaining\n",
610 ktime_ms_delta(shutdown_time, ktime_snap));
611 set_current_state(TASK_INTERRUPTIBLE);
612 schedule_hrtimeout(&shutdown_time, HRTIMER_MODE_ABS);
613 ktime_snap = ktime_get();
615 if (torture_must_stop()) {
616 torture_kthread_stopping("torture_shutdown");
620 /* OK, shut down the system. */
622 VERBOSE_TOROUT_STRING("torture_shutdown task shutting down system");
623 shutdown_task = NULL; /* Avoid self-kill deadlock. */
624 if (torture_shutdown_hook)
625 torture_shutdown_hook();
627 VERBOSE_TOROUT_STRING("No torture_shutdown_hook(), skipping.");
628 if (ftrace_dump_at_shutdown)
629 rcu_ftrace_dump(DUMP_ALL);
630 kernel_power_off(); /* Shut down the system. */
635 * Start up the shutdown task.
637 int torture_shutdown_init(int ssecs, void (*cleanup)(void))
639 torture_shutdown_hook = cleanup;
641 shutdown_time = ktime_add(ktime_get(), ktime_set(ssecs, 0));
642 return torture_create_kthread(torture_shutdown, NULL,
647 EXPORT_SYMBOL_GPL(torture_shutdown_init);
650 * Detect and respond to a system shutdown.
652 static int torture_shutdown_notify(struct notifier_block *unused1,
653 unsigned long unused2, void *unused3)
655 mutex_lock(&fullstop_mutex);
656 if (READ_ONCE(fullstop) == FULLSTOP_DONTSTOP) {
657 VERBOSE_TOROUT_STRING("Unscheduled system shutdown detected");
658 WRITE_ONCE(fullstop, FULLSTOP_SHUTDOWN);
660 pr_warn("Concurrent rmmod and shutdown illegal!\n");
662 mutex_unlock(&fullstop_mutex);
666 static struct notifier_block torture_shutdown_nb = {
667 .notifier_call = torture_shutdown_notify,
671 * Shut down the shutdown task. Say what??? Heh! This can happen if
672 * the torture module gets an rmmod before the shutdown time arrives. ;-)
674 static void torture_shutdown_cleanup(void)
676 unregister_reboot_notifier(&torture_shutdown_nb);
677 if (shutdown_task != NULL) {
678 VERBOSE_TOROUT_STRING("Stopping torture_shutdown task");
679 kthread_stop(shutdown_task);
681 shutdown_task = NULL;
685 * Variables for stuttering, which means to periodically pause and
686 * restart testing in order to catch bugs that appear when load is
687 * suddenly applied to or removed from the system.
689 static struct task_struct *stutter_task;
690 static int stutter_pause_test;
692 static int stutter_gap;
695 * Block until the stutter interval ends. This must be called periodically
696 * by all running kthreads that need to be subject to stuttering.
698 bool stutter_wait(const char *title)
704 cond_resched_tasks_rcu_qs();
705 spt = READ_ONCE(stutter_pause_test);
706 for (; spt; spt = READ_ONCE(stutter_pause_test)) {
708 sched_set_normal(current, MAX_NICE);
712 schedule_timeout_interruptible(1);
713 } else if (spt == 2) {
714 while (READ_ONCE(stutter_pause_test)) {
716 torture_hrtimeout_us(10, 0, NULL);
720 schedule_timeout_interruptible(round_jiffies_relative(HZ));
722 torture_shutdown_absorb(title);
726 EXPORT_SYMBOL_GPL(stutter_wait);
729 * Cause the torture test to "stutter", starting and stopping all
730 * threads periodically.
732 static int torture_stutter(void *arg)
734 DEFINE_TORTURE_RANDOM(rand);
737 VERBOSE_TOROUT_STRING("torture_stutter task started");
739 if (!torture_must_stop() && stutter > 1) {
742 WRITE_ONCE(stutter_pause_test, 1);
744 torture_hrtimeout_jiffies(wtime, &rand);
747 WRITE_ONCE(stutter_pause_test, 2);
748 torture_hrtimeout_jiffies(wtime, NULL);
750 WRITE_ONCE(stutter_pause_test, 0);
751 if (!torture_must_stop())
752 torture_hrtimeout_jiffies(stutter_gap, NULL);
753 torture_shutdown_absorb("torture_stutter");
754 } while (!torture_must_stop());
755 torture_kthread_stopping("torture_stutter");
760 * Initialize and kick off the torture_stutter kthread.
762 int torture_stutter_init(const int s, const int sgap)
766 return torture_create_kthread(torture_stutter, NULL, stutter_task);
768 EXPORT_SYMBOL_GPL(torture_stutter_init);
771 * Cleanup after the torture_stutter kthread.
773 static void torture_stutter_cleanup(void)
777 VERBOSE_TOROUT_STRING("Stopping torture_stutter task");
778 kthread_stop(stutter_task);
783 * Initialize torture module. Please note that this is -not- invoked via
784 * the usual module_init() mechanism, but rather by an explicit call from
785 * the client torture module. This call must be paired with a later
786 * torture_init_end().
788 * The runnable parameter points to a flag that controls whether or not
789 * the test is currently runnable. If there is no such flag, pass in NULL.
791 bool torture_init_begin(char *ttype, int v)
793 mutex_lock(&fullstop_mutex);
794 if (torture_type != NULL) {
795 pr_alert("torture_init_begin: Refusing %s init: %s running.\n",
796 ttype, torture_type);
797 pr_alert("torture_init_begin: One torture test at a time!\n");
798 mutex_unlock(&fullstop_mutex);
801 torture_type = ttype;
803 fullstop = FULLSTOP_DONTSTOP;
806 EXPORT_SYMBOL_GPL(torture_init_begin);
809 * Tell the torture module that initialization is complete.
811 void torture_init_end(void)
813 mutex_unlock(&fullstop_mutex);
814 register_reboot_notifier(&torture_shutdown_nb);
816 EXPORT_SYMBOL_GPL(torture_init_end);
819 * Clean up torture module. Please note that this is -not- invoked via
820 * the usual module_exit() mechanism, but rather by an explicit call from
821 * the client torture module. Returns true if a race with system shutdown
822 * is detected, otherwise, all kthreads started by functions in this file
825 * This must be called before the caller starts shutting down its own
828 * Both torture_cleanup_begin() and torture_cleanup_end() must be paired,
829 * in order to correctly perform the cleanup. They are separated because
830 * threads can still need to reference the torture_type type, thus nullify
831 * only after completing all other relevant calls.
833 bool torture_cleanup_begin(void)
835 mutex_lock(&fullstop_mutex);
836 if (READ_ONCE(fullstop) == FULLSTOP_SHUTDOWN) {
837 pr_warn("Concurrent rmmod and shutdown illegal!\n");
838 mutex_unlock(&fullstop_mutex);
839 schedule_timeout_uninterruptible(10);
842 WRITE_ONCE(fullstop, FULLSTOP_RMMOD);
843 mutex_unlock(&fullstop_mutex);
844 torture_shutdown_cleanup();
845 torture_shuffle_cleanup();
846 torture_stutter_cleanup();
847 torture_onoff_cleanup();
850 EXPORT_SYMBOL_GPL(torture_cleanup_begin);
852 void torture_cleanup_end(void)
854 mutex_lock(&fullstop_mutex);
856 mutex_unlock(&fullstop_mutex);
858 EXPORT_SYMBOL_GPL(torture_cleanup_end);
861 * Is it time for the current torture test to stop?
863 bool torture_must_stop(void)
865 return torture_must_stop_irq() || kthread_should_stop();
867 EXPORT_SYMBOL_GPL(torture_must_stop);
870 * Is it time for the current torture test to stop? This is the irq-safe
871 * version, hence no check for kthread_should_stop().
873 bool torture_must_stop_irq(void)
875 return READ_ONCE(fullstop) != FULLSTOP_DONTSTOP;
877 EXPORT_SYMBOL_GPL(torture_must_stop_irq);
880 * Each kthread must wait for kthread_should_stop() before returning from
881 * its top-level function, otherwise segfaults ensue. This function
882 * prints a "stopping" message and waits for kthread_should_stop(), and
883 * should be called from all torture kthreads immediately prior to
886 void torture_kthread_stopping(char *title)
890 snprintf(buf, sizeof(buf), "Stopping %s", title);
891 VERBOSE_TOROUT_STRING(buf);
892 while (!kthread_should_stop()) {
893 torture_shutdown_absorb(title);
894 schedule_timeout_uninterruptible(1);
897 EXPORT_SYMBOL_GPL(torture_kthread_stopping);
900 * Create a generic torture kthread that is immediately runnable. If you
901 * need the kthread to be stopped so that you can do something to it before
902 * it starts, you will need to open-code your own.
904 int _torture_create_kthread(int (*fn)(void *arg), void *arg, char *s, char *m,
905 char *f, struct task_struct **tp)
909 VERBOSE_TOROUT_STRING(m);
910 *tp = kthread_run(fn, arg, "%s", s);
913 VERBOSE_TOROUT_ERRSTRING(f);
916 torture_shuffle_task_register(*tp);
919 EXPORT_SYMBOL_GPL(_torture_create_kthread);
922 * Stop a generic kthread, emitting a message.
924 void _torture_stop_kthread(char *m, struct task_struct **tp)
928 VERBOSE_TOROUT_STRING(m);
932 EXPORT_SYMBOL_GPL(_torture_stop_kthread);