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 char *torture_type;
51 /* Mediate rmmod and system shutdown. Concurrent rmmod & shutdown illegal! */
52 #define FULLSTOP_DONTSTOP 0 /* Normal operation. */
53 #define FULLSTOP_SHUTDOWN 1 /* System shutdown with torture running. */
54 #define FULLSTOP_RMMOD 2 /* Normal rmmod of torture. */
55 static int fullstop = FULLSTOP_RMMOD;
56 static DEFINE_MUTEX(fullstop_mutex);
58 #ifdef CONFIG_HOTPLUG_CPU
61 * Variables for online-offline handling. Only present if CPU hotplug
62 * is enabled, otherwise does nothing.
65 static struct task_struct *onoff_task;
66 static long onoff_holdoff;
67 static long onoff_interval;
68 static torture_ofl_func *onoff_f;
69 static long n_offline_attempts;
70 static long n_offline_successes;
71 static unsigned long sum_offline;
72 static int min_offline = -1;
73 static int max_offline;
74 static long n_online_attempts;
75 static long n_online_successes;
76 static unsigned long sum_online;
77 static int min_online = -1;
78 static int max_online;
81 * Attempt to take a CPU offline. Return false if the CPU is already
82 * offline or if it is not subject to CPU-hotplug operations. The
83 * caller can detect other failures by looking at the statistics.
85 bool torture_offline(int cpu, long *n_offl_attempts, long *n_offl_successes,
86 unsigned long *sum_offl, int *min_offl, int *max_offl)
91 unsigned long starttime;
93 if (!cpu_online(cpu) || !cpu_is_hotpluggable(cpu))
95 if (num_online_cpus() <= 1)
96 return false; /* Can't offline the last CPU. */
99 pr_alert("%s" TORTURE_FLAG
100 "torture_onoff task: offlining %d\n",
103 (*n_offl_attempts)++;
107 if (!rcu_inkernel_boot_has_ended() && ret == -EBUSY) {
108 // PCI probe frequently disables hotplug during boot.
109 (*n_offl_attempts)--;
110 s = " (-EBUSY forgiven during boot)";
113 pr_alert("%s" TORTURE_FLAG
114 "torture_onoff task: offline %d failed%s: errno %d\n",
115 torture_type, cpu, s, ret);
118 pr_alert("%s" TORTURE_FLAG
119 "torture_onoff task: offlined %d\n",
123 (*n_offl_successes)++;
124 delta = jiffies - starttime;
130 if (*min_offl > delta)
132 if (*max_offl < delta)
138 EXPORT_SYMBOL_GPL(torture_offline);
141 * Attempt to bring a CPU online. Return false if the CPU is already
142 * online or if it is not subject to CPU-hotplug operations. The
143 * caller can detect other failures by looking at the statistics.
145 bool torture_online(int cpu, long *n_onl_attempts, long *n_onl_successes,
146 unsigned long *sum_onl, int *min_onl, int *max_onl)
151 unsigned long starttime;
153 if (cpu_online(cpu) || !cpu_is_hotpluggable(cpu))
157 pr_alert("%s" TORTURE_FLAG
158 "torture_onoff task: onlining %d\n",
165 if (!rcu_inkernel_boot_has_ended() && ret == -EBUSY) {
166 // PCI probe frequently disables hotplug during boot.
168 s = " (-EBUSY forgiven during boot)";
171 pr_alert("%s" TORTURE_FLAG
172 "torture_onoff task: online %d failed%s: errno %d\n",
173 torture_type, cpu, s, ret);
176 pr_alert("%s" TORTURE_FLAG
177 "torture_onoff task: onlined %d\n",
179 (*n_onl_successes)++;
180 delta = jiffies - starttime;
186 if (*min_onl > delta)
188 if (*max_onl < delta)
194 EXPORT_SYMBOL_GPL(torture_online);
197 * Execute random CPU-hotplug operations at the interval specified
198 * by the onoff_interval.
201 torture_onoff(void *arg)
205 DEFINE_TORTURE_RANDOM(rand);
208 VERBOSE_TOROUT_STRING("torture_onoff task started");
209 for_each_online_cpu(cpu)
212 if (!IS_MODULE(CONFIG_TORTURE_TEST))
213 for_each_possible_cpu(cpu) {
217 if (ret && verbose) {
218 pr_alert("%s" TORTURE_FLAG
219 "%s: Initial online %d: errno %d\n",
220 __func__, torture_type, cpu, ret);
225 VERBOSE_TOROUT_STRING("Only one CPU, so CPU-hotplug testing is disabled");
229 if (onoff_holdoff > 0) {
230 VERBOSE_TOROUT_STRING("torture_onoff begin holdoff");
231 schedule_timeout_interruptible(onoff_holdoff);
232 VERBOSE_TOROUT_STRING("torture_onoff end holdoff");
234 while (!torture_must_stop()) {
235 if (disable_onoff_at_boot && !rcu_inkernel_boot_has_ended()) {
236 schedule_timeout_interruptible(HZ / 10);
239 cpu = (torture_random(&rand) >> 4) % (maxcpu + 1);
240 if (!torture_offline(cpu,
241 &n_offline_attempts, &n_offline_successes,
242 &sum_offline, &min_offline, &max_offline))
244 &n_online_attempts, &n_online_successes,
245 &sum_online, &min_online, &max_online);
246 schedule_timeout_interruptible(onoff_interval);
250 torture_kthread_stopping("torture_onoff");
254 #endif /* #ifdef CONFIG_HOTPLUG_CPU */
257 * Initiate online-offline handling.
259 int torture_onoff_init(long ooholdoff, long oointerval, torture_ofl_func *f)
261 #ifdef CONFIG_HOTPLUG_CPU
262 onoff_holdoff = ooholdoff;
263 onoff_interval = oointerval;
265 if (onoff_interval <= 0)
267 return torture_create_kthread(torture_onoff, NULL, onoff_task);
268 #else /* #ifdef CONFIG_HOTPLUG_CPU */
270 #endif /* #else #ifdef CONFIG_HOTPLUG_CPU */
272 EXPORT_SYMBOL_GPL(torture_onoff_init);
275 * Clean up after online/offline testing.
277 static void torture_onoff_cleanup(void)
279 #ifdef CONFIG_HOTPLUG_CPU
280 if (onoff_task == NULL)
282 VERBOSE_TOROUT_STRING("Stopping torture_onoff task");
283 kthread_stop(onoff_task);
285 #endif /* #ifdef CONFIG_HOTPLUG_CPU */
289 * Print online/offline testing statistics.
291 void torture_onoff_stats(void)
293 #ifdef CONFIG_HOTPLUG_CPU
294 pr_cont("onoff: %ld/%ld:%ld/%ld %d,%d:%d,%d %lu:%lu (HZ=%d) ",
295 n_online_successes, n_online_attempts,
296 n_offline_successes, n_offline_attempts,
297 min_online, max_online,
298 min_offline, max_offline,
299 sum_online, sum_offline, HZ);
300 #endif /* #ifdef CONFIG_HOTPLUG_CPU */
302 EXPORT_SYMBOL_GPL(torture_onoff_stats);
305 * Were all the online/offline operations successful?
307 bool torture_onoff_failures(void)
309 #ifdef CONFIG_HOTPLUG_CPU
310 return n_online_successes != n_online_attempts ||
311 n_offline_successes != n_offline_attempts;
312 #else /* #ifdef CONFIG_HOTPLUG_CPU */
314 #endif /* #else #ifdef CONFIG_HOTPLUG_CPU */
316 EXPORT_SYMBOL_GPL(torture_onoff_failures);
318 #define TORTURE_RANDOM_MULT 39916801 /* prime */
319 #define TORTURE_RANDOM_ADD 479001701 /* prime */
320 #define TORTURE_RANDOM_REFRESH 10000
323 * Crude but fast random-number generator. Uses a linear congruential
324 * generator, with occasional help from cpu_clock().
327 torture_random(struct torture_random_state *trsp)
329 if (--trsp->trs_count < 0) {
330 trsp->trs_state += (unsigned long)local_clock();
331 trsp->trs_count = TORTURE_RANDOM_REFRESH;
333 trsp->trs_state = trsp->trs_state * TORTURE_RANDOM_MULT +
335 return swahw32(trsp->trs_state);
337 EXPORT_SYMBOL_GPL(torture_random);
340 * Variables for shuffling. The idea is to ensure that each CPU stays
341 * idle for an extended period to test interactions with dyntick idle,
342 * as well as interactions with any per-CPU variables.
344 struct shuffle_task {
345 struct list_head st_l;
346 struct task_struct *st_t;
349 static long shuffle_interval; /* In jiffies. */
350 static struct task_struct *shuffler_task;
351 static cpumask_var_t shuffle_tmp_mask;
352 static int shuffle_idle_cpu; /* Force all torture tasks off this CPU */
353 static struct list_head shuffle_task_list = LIST_HEAD_INIT(shuffle_task_list);
354 static DEFINE_MUTEX(shuffle_task_mutex);
357 * Register a task to be shuffled. If there is no memory, just splat
358 * and don't bother registering.
360 void torture_shuffle_task_register(struct task_struct *tp)
362 struct shuffle_task *stp;
364 if (WARN_ON_ONCE(tp == NULL))
366 stp = kmalloc(sizeof(*stp), GFP_KERNEL);
367 if (WARN_ON_ONCE(stp == NULL))
370 mutex_lock(&shuffle_task_mutex);
371 list_add(&stp->st_l, &shuffle_task_list);
372 mutex_unlock(&shuffle_task_mutex);
374 EXPORT_SYMBOL_GPL(torture_shuffle_task_register);
377 * Unregister all tasks, for example, at the end of the torture run.
379 static void torture_shuffle_task_unregister_all(void)
381 struct shuffle_task *stp;
382 struct shuffle_task *p;
384 mutex_lock(&shuffle_task_mutex);
385 list_for_each_entry_safe(stp, p, &shuffle_task_list, st_l) {
386 list_del(&stp->st_l);
389 mutex_unlock(&shuffle_task_mutex);
392 /* Shuffle tasks such that we allow shuffle_idle_cpu to become idle.
393 * A special case is when shuffle_idle_cpu = -1, in which case we allow
394 * the tasks to run on all CPUs.
396 static void torture_shuffle_tasks(void)
398 struct shuffle_task *stp;
400 cpumask_setall(shuffle_tmp_mask);
403 /* No point in shuffling if there is only one online CPU (ex: UP) */
404 if (num_online_cpus() == 1) {
409 /* Advance to the next CPU. Upon overflow, don't idle any CPUs. */
410 shuffle_idle_cpu = cpumask_next(shuffle_idle_cpu, shuffle_tmp_mask);
411 if (shuffle_idle_cpu >= nr_cpu_ids)
412 shuffle_idle_cpu = -1;
414 cpumask_clear_cpu(shuffle_idle_cpu, shuffle_tmp_mask);
416 mutex_lock(&shuffle_task_mutex);
417 list_for_each_entry(stp, &shuffle_task_list, st_l)
418 set_cpus_allowed_ptr(stp->st_t, shuffle_tmp_mask);
419 mutex_unlock(&shuffle_task_mutex);
424 /* Shuffle tasks across CPUs, with the intent of allowing each CPU in the
425 * system to become idle at a time and cut off its timer ticks. This is meant
426 * to test the support for such tickless idle CPU in RCU.
428 static int torture_shuffle(void *arg)
430 VERBOSE_TOROUT_STRING("torture_shuffle task started");
432 schedule_timeout_interruptible(shuffle_interval);
433 torture_shuffle_tasks();
434 torture_shutdown_absorb("torture_shuffle");
435 } while (!torture_must_stop());
436 torture_kthread_stopping("torture_shuffle");
441 * Start the shuffler, with shuffint in jiffies.
443 int torture_shuffle_init(long shuffint)
445 shuffle_interval = shuffint;
447 shuffle_idle_cpu = -1;
449 if (!alloc_cpumask_var(&shuffle_tmp_mask, GFP_KERNEL)) {
450 VERBOSE_TOROUT_ERRSTRING("Failed to alloc mask");
454 /* Create the shuffler thread */
455 return torture_create_kthread(torture_shuffle, NULL, shuffler_task);
457 EXPORT_SYMBOL_GPL(torture_shuffle_init);
460 * Stop the shuffling.
462 static void torture_shuffle_cleanup(void)
464 torture_shuffle_task_unregister_all();
466 VERBOSE_TOROUT_STRING("Stopping torture_shuffle task");
467 kthread_stop(shuffler_task);
468 free_cpumask_var(shuffle_tmp_mask);
470 shuffler_task = NULL;
474 * Variables for auto-shutdown. This allows "lights out" torture runs
475 * to be fully scripted.
477 static struct task_struct *shutdown_task;
478 static ktime_t shutdown_time; /* time to system shutdown. */
479 static void (*torture_shutdown_hook)(void);
482 * Absorb kthreads into a kernel function that won't return, so that
483 * they won't ever access module text or data again.
485 void torture_shutdown_absorb(const char *title)
487 while (READ_ONCE(fullstop) == FULLSTOP_SHUTDOWN) {
488 pr_notice("torture thread %s parking due to system shutdown\n",
490 schedule_timeout_uninterruptible(MAX_SCHEDULE_TIMEOUT);
493 EXPORT_SYMBOL_GPL(torture_shutdown_absorb);
496 * Cause the torture test to shutdown the system after the test has
497 * run for the time specified by the shutdown_secs parameter.
499 static int torture_shutdown(void *arg)
503 VERBOSE_TOROUT_STRING("torture_shutdown task started");
504 ktime_snap = ktime_get();
505 while (ktime_before(ktime_snap, shutdown_time) &&
506 !torture_must_stop()) {
508 pr_alert("%s" TORTURE_FLAG
509 "torture_shutdown task: %llu ms remaining\n",
511 ktime_ms_delta(shutdown_time, ktime_snap));
512 set_current_state(TASK_INTERRUPTIBLE);
513 schedule_hrtimeout(&shutdown_time, HRTIMER_MODE_ABS);
514 ktime_snap = ktime_get();
516 if (torture_must_stop()) {
517 torture_kthread_stopping("torture_shutdown");
521 /* OK, shut down the system. */
523 VERBOSE_TOROUT_STRING("torture_shutdown task shutting down system");
524 shutdown_task = NULL; /* Avoid self-kill deadlock. */
525 if (torture_shutdown_hook)
526 torture_shutdown_hook();
528 VERBOSE_TOROUT_STRING("No torture_shutdown_hook(), skipping.");
529 rcu_ftrace_dump(DUMP_ALL);
530 kernel_power_off(); /* Shut down the system. */
535 * Start up the shutdown task.
537 int torture_shutdown_init(int ssecs, void (*cleanup)(void))
539 torture_shutdown_hook = cleanup;
541 shutdown_time = ktime_add(ktime_get(), ktime_set(ssecs, 0));
542 return torture_create_kthread(torture_shutdown, NULL,
547 EXPORT_SYMBOL_GPL(torture_shutdown_init);
550 * Detect and respond to a system shutdown.
552 static int torture_shutdown_notify(struct notifier_block *unused1,
553 unsigned long unused2, void *unused3)
555 mutex_lock(&fullstop_mutex);
556 if (READ_ONCE(fullstop) == FULLSTOP_DONTSTOP) {
557 VERBOSE_TOROUT_STRING("Unscheduled system shutdown detected");
558 WRITE_ONCE(fullstop, FULLSTOP_SHUTDOWN);
560 pr_warn("Concurrent rmmod and shutdown illegal!\n");
562 mutex_unlock(&fullstop_mutex);
566 static struct notifier_block torture_shutdown_nb = {
567 .notifier_call = torture_shutdown_notify,
571 * Shut down the shutdown task. Say what??? Heh! This can happen if
572 * the torture module gets an rmmod before the shutdown time arrives. ;-)
574 static void torture_shutdown_cleanup(void)
576 unregister_reboot_notifier(&torture_shutdown_nb);
577 if (shutdown_task != NULL) {
578 VERBOSE_TOROUT_STRING("Stopping torture_shutdown task");
579 kthread_stop(shutdown_task);
581 shutdown_task = NULL;
585 * Variables for stuttering, which means to periodically pause and
586 * restart testing in order to catch bugs that appear when load is
587 * suddenly applied to or removed from the system.
589 static struct task_struct *stutter_task;
590 static int stutter_pause_test;
592 static int stutter_gap;
595 * Block until the stutter interval ends. This must be called periodically
596 * by all running kthreads that need to be subject to stuttering.
598 bool stutter_wait(const char *title)
603 cond_resched_tasks_rcu_qs();
604 spt = READ_ONCE(stutter_pause_test);
605 for (; spt; spt = READ_ONCE(stutter_pause_test)) {
608 schedule_timeout_interruptible(1);
609 } else if (spt == 2) {
610 while (READ_ONCE(stutter_pause_test))
613 schedule_timeout_interruptible(round_jiffies_relative(HZ));
615 torture_shutdown_absorb(title);
619 EXPORT_SYMBOL_GPL(stutter_wait);
622 * Cause the torture test to "stutter", starting and stopping all
623 * threads periodically.
625 static int torture_stutter(void *arg)
629 VERBOSE_TOROUT_STRING("torture_stutter task started");
631 if (!torture_must_stop() && stutter > 1) {
633 if (stutter > HZ + 1) {
634 WRITE_ONCE(stutter_pause_test, 1);
635 wtime = stutter - HZ - 1;
636 schedule_timeout_interruptible(wtime);
639 WRITE_ONCE(stutter_pause_test, 2);
640 schedule_timeout_interruptible(wtime);
642 WRITE_ONCE(stutter_pause_test, 0);
643 if (!torture_must_stop())
644 schedule_timeout_interruptible(stutter_gap);
645 torture_shutdown_absorb("torture_stutter");
646 } while (!torture_must_stop());
647 torture_kthread_stopping("torture_stutter");
652 * Initialize and kick off the torture_stutter kthread.
654 int torture_stutter_init(const int s, const int sgap)
658 return torture_create_kthread(torture_stutter, NULL, stutter_task);
660 EXPORT_SYMBOL_GPL(torture_stutter_init);
663 * Cleanup after the torture_stutter kthread.
665 static void torture_stutter_cleanup(void)
669 VERBOSE_TOROUT_STRING("Stopping torture_stutter task");
670 kthread_stop(stutter_task);
675 * Initialize torture module. Please note that this is -not- invoked via
676 * the usual module_init() mechanism, but rather by an explicit call from
677 * the client torture module. This call must be paired with a later
678 * torture_init_end().
680 * The runnable parameter points to a flag that controls whether or not
681 * the test is currently runnable. If there is no such flag, pass in NULL.
683 bool torture_init_begin(char *ttype, int v)
685 mutex_lock(&fullstop_mutex);
686 if (torture_type != NULL) {
687 pr_alert("torture_init_begin: Refusing %s init: %s running.\n",
688 ttype, torture_type);
689 pr_alert("torture_init_begin: One torture test at a time!\n");
690 mutex_unlock(&fullstop_mutex);
693 torture_type = ttype;
695 fullstop = FULLSTOP_DONTSTOP;
698 EXPORT_SYMBOL_GPL(torture_init_begin);
701 * Tell the torture module that initialization is complete.
703 void torture_init_end(void)
705 mutex_unlock(&fullstop_mutex);
706 register_reboot_notifier(&torture_shutdown_nb);
708 EXPORT_SYMBOL_GPL(torture_init_end);
711 * Clean up torture module. Please note that this is -not- invoked via
712 * the usual module_exit() mechanism, but rather by an explicit call from
713 * the client torture module. Returns true if a race with system shutdown
714 * is detected, otherwise, all kthreads started by functions in this file
717 * This must be called before the caller starts shutting down its own
720 * Both torture_cleanup_begin() and torture_cleanup_end() must be paired,
721 * in order to correctly perform the cleanup. They are separated because
722 * threads can still need to reference the torture_type type, thus nullify
723 * only after completing all other relevant calls.
725 bool torture_cleanup_begin(void)
727 mutex_lock(&fullstop_mutex);
728 if (READ_ONCE(fullstop) == FULLSTOP_SHUTDOWN) {
729 pr_warn("Concurrent rmmod and shutdown illegal!\n");
730 mutex_unlock(&fullstop_mutex);
731 schedule_timeout_uninterruptible(10);
734 WRITE_ONCE(fullstop, FULLSTOP_RMMOD);
735 mutex_unlock(&fullstop_mutex);
736 torture_shutdown_cleanup();
737 torture_shuffle_cleanup();
738 torture_stutter_cleanup();
739 torture_onoff_cleanup();
742 EXPORT_SYMBOL_GPL(torture_cleanup_begin);
744 void torture_cleanup_end(void)
746 mutex_lock(&fullstop_mutex);
748 mutex_unlock(&fullstop_mutex);
750 EXPORT_SYMBOL_GPL(torture_cleanup_end);
753 * Is it time for the current torture test to stop?
755 bool torture_must_stop(void)
757 return torture_must_stop_irq() || kthread_should_stop();
759 EXPORT_SYMBOL_GPL(torture_must_stop);
762 * Is it time for the current torture test to stop? This is the irq-safe
763 * version, hence no check for kthread_should_stop().
765 bool torture_must_stop_irq(void)
767 return READ_ONCE(fullstop) != FULLSTOP_DONTSTOP;
769 EXPORT_SYMBOL_GPL(torture_must_stop_irq);
772 * Each kthread must wait for kthread_should_stop() before returning from
773 * its top-level function, otherwise segfaults ensue. This function
774 * prints a "stopping" message and waits for kthread_should_stop(), and
775 * should be called from all torture kthreads immediately prior to
778 void torture_kthread_stopping(char *title)
782 snprintf(buf, sizeof(buf), "Stopping %s", title);
783 VERBOSE_TOROUT_STRING(buf);
784 while (!kthread_should_stop()) {
785 torture_shutdown_absorb(title);
786 schedule_timeout_uninterruptible(1);
789 EXPORT_SYMBOL_GPL(torture_kthread_stopping);
792 * Create a generic torture kthread that is immediately runnable. If you
793 * need the kthread to be stopped so that you can do something to it before
794 * it starts, you will need to open-code your own.
796 int _torture_create_kthread(int (*fn)(void *arg), void *arg, char *s, char *m,
797 char *f, struct task_struct **tp)
801 VERBOSE_TOROUT_STRING(m);
802 *tp = kthread_run(fn, arg, "%s", s);
805 VERBOSE_TOROUT_ERRSTRING(f);
808 torture_shuffle_task_register(*tp);
811 EXPORT_SYMBOL_GPL(_torture_create_kthread);
814 * Stop a generic kthread, emitting a message.
816 void _torture_stop_kthread(char *m, struct task_struct **tp)
820 VERBOSE_TOROUT_STRING(m);
824 EXPORT_SYMBOL_GPL(_torture_stop_kthread);