2 * Common functions for in-kernel torture tests.
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, you can access it online at
16 * http://www.gnu.org/licenses/gpl-2.0.html.
18 * Copyright (C) IBM Corporation, 2014
20 * Author: Paul E. McKenney <paulmck@us.ibm.com>
21 * Based on kernel/rcu/torture.c.
24 #define pr_fmt(fmt) fmt
26 #include <linux/types.h>
27 #include <linux/kernel.h>
28 #include <linux/init.h>
29 #include <linux/module.h>
30 #include <linux/kthread.h>
31 #include <linux/err.h>
32 #include <linux/spinlock.h>
33 #include <linux/smp.h>
34 #include <linux/interrupt.h>
35 #include <linux/sched.h>
36 #include <linux/sched/clock.h>
37 #include <linux/atomic.h>
38 #include <linux/bitops.h>
39 #include <linux/completion.h>
40 #include <linux/moduleparam.h>
41 #include <linux/percpu.h>
42 #include <linux/notifier.h>
43 #include <linux/reboot.h>
44 #include <linux/freezer.h>
45 #include <linux/cpu.h>
46 #include <linux/delay.h>
47 #include <linux/stat.h>
48 #include <linux/slab.h>
49 #include <linux/trace_clock.h>
50 #include <linux/ktime.h>
51 #include <asm/byteorder.h>
52 #include <linux/torture.h>
55 MODULE_LICENSE("GPL");
56 MODULE_AUTHOR("Paul E. McKenney <paulmck@us.ibm.com>");
58 static char *torture_type;
61 /* Mediate rmmod and system shutdown. Concurrent rmmod & shutdown illegal! */
62 #define FULLSTOP_DONTSTOP 0 /* Normal operation. */
63 #define FULLSTOP_SHUTDOWN 1 /* System shutdown with torture running. */
64 #define FULLSTOP_RMMOD 2 /* Normal rmmod of torture. */
65 static int fullstop = FULLSTOP_RMMOD;
66 static DEFINE_MUTEX(fullstop_mutex);
68 #ifdef CONFIG_HOTPLUG_CPU
71 * Variables for online-offline handling. Only present if CPU hotplug
72 * is enabled, otherwise does nothing.
75 static struct task_struct *onoff_task;
76 static long onoff_holdoff;
77 static long onoff_interval;
78 static long n_offline_attempts;
79 static long n_offline_successes;
80 static unsigned long sum_offline;
81 static int min_offline = -1;
82 static int max_offline;
83 static long n_online_attempts;
84 static long n_online_successes;
85 static unsigned long sum_online;
86 static int min_online = -1;
87 static int max_online;
90 * Attempt to take a CPU offline. Return false if the CPU is already
91 * offline or if it is not subject to CPU-hotplug operations. The
92 * caller can detect other failures by looking at the statistics.
94 bool torture_offline(int cpu, long *n_offl_attempts, long *n_offl_successes,
95 unsigned long *sum_offl, int *min_offl, int *max_offl)
99 unsigned long starttime;
101 if (!cpu_online(cpu) || !cpu_is_hotpluggable(cpu))
105 pr_alert("%s" TORTURE_FLAG
106 "torture_onoff task: offlining %d\n",
109 (*n_offl_attempts)++;
113 pr_alert("%s" TORTURE_FLAG
114 "torture_onoff task: offline %d failed: errno %d\n",
115 torture_type, cpu, ret);
118 pr_alert("%s" TORTURE_FLAG
119 "torture_onoff task: offlined %d\n",
121 (*n_offl_successes)++;
122 delta = jiffies - starttime;
128 if (*min_offl > delta)
130 if (*max_offl < delta)
136 EXPORT_SYMBOL_GPL(torture_offline);
139 * Attempt to bring a CPU online. Return false if the CPU is already
140 * online or if it is not subject to CPU-hotplug operations. The
141 * caller can detect other failures by looking at the statistics.
143 bool torture_online(int cpu, long *n_onl_attempts, long *n_onl_successes,
144 unsigned long *sum_onl, int *min_onl, int *max_onl)
148 unsigned long starttime;
150 if (cpu_online(cpu) || !cpu_is_hotpluggable(cpu))
154 pr_alert("%s" TORTURE_FLAG
155 "torture_onoff task: onlining %d\n",
162 pr_alert("%s" TORTURE_FLAG
163 "torture_onoff task: online %d failed: errno %d\n",
164 torture_type, cpu, ret);
167 pr_alert("%s" TORTURE_FLAG
168 "torture_onoff task: onlined %d\n",
170 (*n_onl_successes)++;
171 delta = jiffies - starttime;
177 if (*min_onl > delta)
179 if (*max_onl < delta)
185 EXPORT_SYMBOL_GPL(torture_online);
188 * Execute random CPU-hotplug operations at the interval specified
189 * by the onoff_interval.
192 torture_onoff(void *arg)
196 DEFINE_TORTURE_RANDOM(rand);
198 VERBOSE_TOROUT_STRING("torture_onoff task started");
199 for_each_online_cpu(cpu)
204 VERBOSE_TOROUT_STRING("Only one CPU, so CPU-hotplug testing is disabled");
208 if (onoff_holdoff > 0) {
209 VERBOSE_TOROUT_STRING("torture_onoff begin holdoff");
210 schedule_timeout_interruptible(onoff_holdoff);
211 VERBOSE_TOROUT_STRING("torture_onoff end holdoff");
213 while (!torture_must_stop()) {
214 cpu = (torture_random(&rand) >> 4) % (maxcpu + 1);
215 if (!torture_offline(cpu,
216 &n_offline_attempts, &n_offline_successes,
217 &sum_offline, &min_offline, &max_offline))
219 &n_online_attempts, &n_online_successes,
220 &sum_online, &min_online, &max_online);
221 schedule_timeout_interruptible(onoff_interval);
225 torture_kthread_stopping("torture_onoff");
229 #endif /* #ifdef CONFIG_HOTPLUG_CPU */
232 * Initiate online-offline handling.
234 int torture_onoff_init(long ooholdoff, long oointerval)
238 #ifdef CONFIG_HOTPLUG_CPU
239 onoff_holdoff = ooholdoff;
240 onoff_interval = oointerval;
241 if (onoff_interval <= 0)
243 ret = torture_create_kthread(torture_onoff, NULL, onoff_task);
244 #endif /* #ifdef CONFIG_HOTPLUG_CPU */
247 EXPORT_SYMBOL_GPL(torture_onoff_init);
250 * Clean up after online/offline testing.
252 static void torture_onoff_cleanup(void)
254 #ifdef CONFIG_HOTPLUG_CPU
255 if (onoff_task == NULL)
257 VERBOSE_TOROUT_STRING("Stopping torture_onoff task");
258 kthread_stop(onoff_task);
260 #endif /* #ifdef CONFIG_HOTPLUG_CPU */
262 EXPORT_SYMBOL_GPL(torture_onoff_cleanup);
265 * Print online/offline testing statistics.
267 void torture_onoff_stats(void)
269 #ifdef CONFIG_HOTPLUG_CPU
270 pr_cont("onoff: %ld/%ld:%ld/%ld %d,%d:%d,%d %lu:%lu (HZ=%d) ",
271 n_online_successes, n_online_attempts,
272 n_offline_successes, n_offline_attempts,
273 min_online, max_online,
274 min_offline, max_offline,
275 sum_online, sum_offline, HZ);
276 #endif /* #ifdef CONFIG_HOTPLUG_CPU */
278 EXPORT_SYMBOL_GPL(torture_onoff_stats);
281 * Were all the online/offline operations successful?
283 bool torture_onoff_failures(void)
285 #ifdef CONFIG_HOTPLUG_CPU
286 return n_online_successes != n_online_attempts ||
287 n_offline_successes != n_offline_attempts;
288 #else /* #ifdef CONFIG_HOTPLUG_CPU */
290 #endif /* #else #ifdef CONFIG_HOTPLUG_CPU */
292 EXPORT_SYMBOL_GPL(torture_onoff_failures);
294 #define TORTURE_RANDOM_MULT 39916801 /* prime */
295 #define TORTURE_RANDOM_ADD 479001701 /* prime */
296 #define TORTURE_RANDOM_REFRESH 10000
299 * Crude but fast random-number generator. Uses a linear congruential
300 * generator, with occasional help from cpu_clock().
303 torture_random(struct torture_random_state *trsp)
305 if (--trsp->trs_count < 0) {
306 trsp->trs_state += (unsigned long)local_clock();
307 trsp->trs_count = TORTURE_RANDOM_REFRESH;
309 trsp->trs_state = trsp->trs_state * TORTURE_RANDOM_MULT +
311 return swahw32(trsp->trs_state);
313 EXPORT_SYMBOL_GPL(torture_random);
316 * Variables for shuffling. The idea is to ensure that each CPU stays
317 * idle for an extended period to test interactions with dyntick idle,
318 * as well as interactions with any per-CPU variables.
320 struct shuffle_task {
321 struct list_head st_l;
322 struct task_struct *st_t;
325 static long shuffle_interval; /* In jiffies. */
326 static struct task_struct *shuffler_task;
327 static cpumask_var_t shuffle_tmp_mask;
328 static int shuffle_idle_cpu; /* Force all torture tasks off this CPU */
329 static struct list_head shuffle_task_list = LIST_HEAD_INIT(shuffle_task_list);
330 static DEFINE_MUTEX(shuffle_task_mutex);
333 * Register a task to be shuffled. If there is no memory, just splat
334 * and don't bother registering.
336 void torture_shuffle_task_register(struct task_struct *tp)
338 struct shuffle_task *stp;
340 if (WARN_ON_ONCE(tp == NULL))
342 stp = kmalloc(sizeof(*stp), GFP_KERNEL);
343 if (WARN_ON_ONCE(stp == NULL))
346 mutex_lock(&shuffle_task_mutex);
347 list_add(&stp->st_l, &shuffle_task_list);
348 mutex_unlock(&shuffle_task_mutex);
350 EXPORT_SYMBOL_GPL(torture_shuffle_task_register);
353 * Unregister all tasks, for example, at the end of the torture run.
355 static void torture_shuffle_task_unregister_all(void)
357 struct shuffle_task *stp;
358 struct shuffle_task *p;
360 mutex_lock(&shuffle_task_mutex);
361 list_for_each_entry_safe(stp, p, &shuffle_task_list, st_l) {
362 list_del(&stp->st_l);
365 mutex_unlock(&shuffle_task_mutex);
368 /* Shuffle tasks such that we allow shuffle_idle_cpu to become idle.
369 * A special case is when shuffle_idle_cpu = -1, in which case we allow
370 * the tasks to run on all CPUs.
372 static void torture_shuffle_tasks(void)
374 struct shuffle_task *stp;
376 cpumask_setall(shuffle_tmp_mask);
379 /* No point in shuffling if there is only one online CPU (ex: UP) */
380 if (num_online_cpus() == 1) {
385 /* Advance to the next CPU. Upon overflow, don't idle any CPUs. */
386 shuffle_idle_cpu = cpumask_next(shuffle_idle_cpu, shuffle_tmp_mask);
387 if (shuffle_idle_cpu >= nr_cpu_ids)
388 shuffle_idle_cpu = -1;
390 cpumask_clear_cpu(shuffle_idle_cpu, shuffle_tmp_mask);
392 mutex_lock(&shuffle_task_mutex);
393 list_for_each_entry(stp, &shuffle_task_list, st_l)
394 set_cpus_allowed_ptr(stp->st_t, shuffle_tmp_mask);
395 mutex_unlock(&shuffle_task_mutex);
400 /* Shuffle tasks across CPUs, with the intent of allowing each CPU in the
401 * system to become idle at a time and cut off its timer ticks. This is meant
402 * to test the support for such tickless idle CPU in RCU.
404 static int torture_shuffle(void *arg)
406 VERBOSE_TOROUT_STRING("torture_shuffle task started");
408 schedule_timeout_interruptible(shuffle_interval);
409 torture_shuffle_tasks();
410 torture_shutdown_absorb("torture_shuffle");
411 } while (!torture_must_stop());
412 torture_kthread_stopping("torture_shuffle");
417 * Start the shuffler, with shuffint in jiffies.
419 int torture_shuffle_init(long shuffint)
421 shuffle_interval = shuffint;
423 shuffle_idle_cpu = -1;
425 if (!alloc_cpumask_var(&shuffle_tmp_mask, GFP_KERNEL)) {
426 VERBOSE_TOROUT_ERRSTRING("Failed to alloc mask");
430 /* Create the shuffler thread */
431 return torture_create_kthread(torture_shuffle, NULL, shuffler_task);
433 EXPORT_SYMBOL_GPL(torture_shuffle_init);
436 * Stop the shuffling.
438 static void torture_shuffle_cleanup(void)
440 torture_shuffle_task_unregister_all();
442 VERBOSE_TOROUT_STRING("Stopping torture_shuffle task");
443 kthread_stop(shuffler_task);
444 free_cpumask_var(shuffle_tmp_mask);
446 shuffler_task = NULL;
448 EXPORT_SYMBOL_GPL(torture_shuffle_cleanup);
451 * Variables for auto-shutdown. This allows "lights out" torture runs
452 * to be fully scripted.
454 static struct task_struct *shutdown_task;
455 static ktime_t shutdown_time; /* time to system shutdown. */
456 static void (*torture_shutdown_hook)(void);
459 * Absorb kthreads into a kernel function that won't return, so that
460 * they won't ever access module text or data again.
462 void torture_shutdown_absorb(const char *title)
464 while (READ_ONCE(fullstop) == FULLSTOP_SHUTDOWN) {
465 pr_notice("torture thread %s parking due to system shutdown\n",
467 schedule_timeout_uninterruptible(MAX_SCHEDULE_TIMEOUT);
470 EXPORT_SYMBOL_GPL(torture_shutdown_absorb);
473 * Cause the torture test to shutdown the system after the test has
474 * run for the time specified by the shutdown_secs parameter.
476 static int torture_shutdown(void *arg)
480 VERBOSE_TOROUT_STRING("torture_shutdown task started");
481 ktime_snap = ktime_get();
482 while (ktime_before(ktime_snap, shutdown_time) &&
483 !torture_must_stop()) {
485 pr_alert("%s" TORTURE_FLAG
486 "torture_shutdown task: %llu ms remaining\n",
488 ktime_ms_delta(shutdown_time, ktime_snap));
489 set_current_state(TASK_INTERRUPTIBLE);
490 schedule_hrtimeout(&shutdown_time, HRTIMER_MODE_ABS);
491 ktime_snap = ktime_get();
493 if (torture_must_stop()) {
494 torture_kthread_stopping("torture_shutdown");
498 /* OK, shut down the system. */
500 VERBOSE_TOROUT_STRING("torture_shutdown task shutting down system");
501 shutdown_task = NULL; /* Avoid self-kill deadlock. */
502 if (torture_shutdown_hook)
503 torture_shutdown_hook();
505 VERBOSE_TOROUT_STRING("No torture_shutdown_hook(), skipping.");
506 rcu_ftrace_dump(DUMP_ALL);
507 kernel_power_off(); /* Shut down the system. */
512 * Start up the shutdown task.
514 int torture_shutdown_init(int ssecs, void (*cleanup)(void))
518 torture_shutdown_hook = cleanup;
520 shutdown_time = ktime_add(ktime_get(), ktime_set(ssecs, 0));
521 ret = torture_create_kthread(torture_shutdown, NULL,
526 EXPORT_SYMBOL_GPL(torture_shutdown_init);
529 * Detect and respond to a system shutdown.
531 static int torture_shutdown_notify(struct notifier_block *unused1,
532 unsigned long unused2, void *unused3)
534 mutex_lock(&fullstop_mutex);
535 if (READ_ONCE(fullstop) == FULLSTOP_DONTSTOP) {
536 VERBOSE_TOROUT_STRING("Unscheduled system shutdown detected");
537 WRITE_ONCE(fullstop, FULLSTOP_SHUTDOWN);
539 pr_warn("Concurrent rmmod and shutdown illegal!\n");
541 mutex_unlock(&fullstop_mutex);
545 static struct notifier_block torture_shutdown_nb = {
546 .notifier_call = torture_shutdown_notify,
550 * Shut down the shutdown task. Say what??? Heh! This can happen if
551 * the torture module gets an rmmod before the shutdown time arrives. ;-)
553 static void torture_shutdown_cleanup(void)
555 unregister_reboot_notifier(&torture_shutdown_nb);
556 if (shutdown_task != NULL) {
557 VERBOSE_TOROUT_STRING("Stopping torture_shutdown task");
558 kthread_stop(shutdown_task);
560 shutdown_task = NULL;
564 * Variables for stuttering, which means to periodically pause and
565 * restart testing in order to catch bugs that appear when load is
566 * suddenly applied to or removed from the system.
568 static struct task_struct *stutter_task;
569 static int stutter_pause_test;
573 * Block until the stutter interval ends. This must be called periodically
574 * by all running kthreads that need to be subject to stuttering.
576 void stutter_wait(const char *title)
580 cond_resched_tasks_rcu_qs();
581 spt = READ_ONCE(stutter_pause_test);
582 for (; spt; spt = READ_ONCE(stutter_pause_test)) {
584 schedule_timeout_interruptible(1);
585 } else if (spt == 2) {
586 while (READ_ONCE(stutter_pause_test))
589 schedule_timeout_interruptible(round_jiffies_relative(HZ));
591 torture_shutdown_absorb(title);
594 EXPORT_SYMBOL_GPL(stutter_wait);
597 * Cause the torture test to "stutter", starting and stopping all
598 * threads periodically.
600 static int torture_stutter(void *arg)
602 VERBOSE_TOROUT_STRING("torture_stutter task started");
604 if (!torture_must_stop() && stutter > 1) {
605 WRITE_ONCE(stutter_pause_test, 1);
606 schedule_timeout_interruptible(stutter - 1);
607 WRITE_ONCE(stutter_pause_test, 2);
608 schedule_timeout_interruptible(1);
610 WRITE_ONCE(stutter_pause_test, 0);
611 if (!torture_must_stop())
612 schedule_timeout_interruptible(stutter);
613 torture_shutdown_absorb("torture_stutter");
614 } while (!torture_must_stop());
615 torture_kthread_stopping("torture_stutter");
620 * Initialize and kick off the torture_stutter kthread.
622 int torture_stutter_init(int s)
627 ret = torture_create_kthread(torture_stutter, NULL, stutter_task);
630 EXPORT_SYMBOL_GPL(torture_stutter_init);
633 * Cleanup after the torture_stutter kthread.
635 static void torture_stutter_cleanup(void)
639 VERBOSE_TOROUT_STRING("Stopping torture_stutter task");
640 kthread_stop(stutter_task);
645 * Initialize torture module. Please note that this is -not- invoked via
646 * the usual module_init() mechanism, but rather by an explicit call from
647 * the client torture module. This call must be paired with a later
648 * torture_init_end().
650 * The runnable parameter points to a flag that controls whether or not
651 * the test is currently runnable. If there is no such flag, pass in NULL.
653 bool torture_init_begin(char *ttype, int v)
655 mutex_lock(&fullstop_mutex);
656 if (torture_type != NULL) {
657 pr_alert("torture_init_begin: Refusing %s init: %s running.\n",
658 ttype, torture_type);
659 pr_alert("torture_init_begin: One torture test at a time!\n");
660 mutex_unlock(&fullstop_mutex);
663 torture_type = ttype;
665 fullstop = FULLSTOP_DONTSTOP;
668 EXPORT_SYMBOL_GPL(torture_init_begin);
671 * Tell the torture module that initialization is complete.
673 void torture_init_end(void)
675 mutex_unlock(&fullstop_mutex);
676 register_reboot_notifier(&torture_shutdown_nb);
678 EXPORT_SYMBOL_GPL(torture_init_end);
681 * Clean up torture module. Please note that this is -not- invoked via
682 * the usual module_exit() mechanism, but rather by an explicit call from
683 * the client torture module. Returns true if a race with system shutdown
684 * is detected, otherwise, all kthreads started by functions in this file
687 * This must be called before the caller starts shutting down its own
690 * Both torture_cleanup_begin() and torture_cleanup_end() must be paired,
691 * in order to correctly perform the cleanup. They are separated because
692 * threads can still need to reference the torture_type type, thus nullify
693 * only after completing all other relevant calls.
695 bool torture_cleanup_begin(void)
697 mutex_lock(&fullstop_mutex);
698 if (READ_ONCE(fullstop) == FULLSTOP_SHUTDOWN) {
699 pr_warn("Concurrent rmmod and shutdown illegal!\n");
700 mutex_unlock(&fullstop_mutex);
701 schedule_timeout_uninterruptible(10);
704 WRITE_ONCE(fullstop, FULLSTOP_RMMOD);
705 mutex_unlock(&fullstop_mutex);
706 torture_shutdown_cleanup();
707 torture_shuffle_cleanup();
708 torture_stutter_cleanup();
709 torture_onoff_cleanup();
712 EXPORT_SYMBOL_GPL(torture_cleanup_begin);
714 void torture_cleanup_end(void)
716 mutex_lock(&fullstop_mutex);
718 mutex_unlock(&fullstop_mutex);
720 EXPORT_SYMBOL_GPL(torture_cleanup_end);
723 * Is it time for the current torture test to stop?
725 bool torture_must_stop(void)
727 return torture_must_stop_irq() || kthread_should_stop();
729 EXPORT_SYMBOL_GPL(torture_must_stop);
732 * Is it time for the current torture test to stop? This is the irq-safe
733 * version, hence no check for kthread_should_stop().
735 bool torture_must_stop_irq(void)
737 return READ_ONCE(fullstop) != FULLSTOP_DONTSTOP;
739 EXPORT_SYMBOL_GPL(torture_must_stop_irq);
742 * Each kthread must wait for kthread_should_stop() before returning from
743 * its top-level function, otherwise segfaults ensue. This function
744 * prints a "stopping" message and waits for kthread_should_stop(), and
745 * should be called from all torture kthreads immediately prior to
748 void torture_kthread_stopping(char *title)
752 snprintf(buf, sizeof(buf), "Stopping %s", title);
753 VERBOSE_TOROUT_STRING(buf);
754 while (!kthread_should_stop()) {
755 torture_shutdown_absorb(title);
756 schedule_timeout_uninterruptible(1);
759 EXPORT_SYMBOL_GPL(torture_kthread_stopping);
762 * Create a generic torture kthread that is immediately runnable. If you
763 * need the kthread to be stopped so that you can do something to it before
764 * it starts, you will need to open-code your own.
766 int _torture_create_kthread(int (*fn)(void *arg), void *arg, char *s, char *m,
767 char *f, struct task_struct **tp)
771 VERBOSE_TOROUT_STRING(m);
772 *tp = kthread_run(fn, arg, "%s", s);
775 VERBOSE_TOROUT_ERRSTRING(f);
778 torture_shuffle_task_register(*tp);
781 EXPORT_SYMBOL_GPL(_torture_create_kthread);
784 * Stop a generic kthread, emitting a message.
786 void _torture_stop_kthread(char *m, struct task_struct **tp)
790 VERBOSE_TOROUT_STRING(m);
794 EXPORT_SYMBOL_GPL(_torture_stop_kthread);