18f8e76c0SPaul E. McKenney // SPDX-License-Identifier: GPL-2.0+
251b1130eSPaul E. McKenney /*
351b1130eSPaul E. McKenney * Common functions for in-kernel torture tests.
451b1130eSPaul E. McKenney *
551b1130eSPaul E. McKenney * Copyright (C) IBM Corporation, 2014
651b1130eSPaul E. McKenney *
78f8e76c0SPaul E. McKenney * Author: Paul E. McKenney <paulmck@linux.ibm.com>
851b1130eSPaul E. McKenney * Based on kernel/rcu/torture.c.
951b1130eSPaul E. McKenney */
1060500037SPaul E. McKenney
1160500037SPaul E. McKenney #define pr_fmt(fmt) fmt
1260500037SPaul E. McKenney
1351b1130eSPaul E. McKenney #include <linux/types.h>
1451b1130eSPaul E. McKenney #include <linux/kernel.h>
1551b1130eSPaul E. McKenney #include <linux/init.h>
1651b1130eSPaul E. McKenney #include <linux/module.h>
1751b1130eSPaul E. McKenney #include <linux/kthread.h>
1851b1130eSPaul E. McKenney #include <linux/err.h>
1951b1130eSPaul E. McKenney #include <linux/spinlock.h>
2051b1130eSPaul E. McKenney #include <linux/smp.h>
2151b1130eSPaul E. McKenney #include <linux/interrupt.h>
2251b1130eSPaul E. McKenney #include <linux/sched.h>
23e6017571SIngo Molnar #include <linux/sched/clock.h>
2451b1130eSPaul E. McKenney #include <linux/atomic.h>
2551b1130eSPaul E. McKenney #include <linux/bitops.h>
2651b1130eSPaul E. McKenney #include <linux/completion.h>
2751b1130eSPaul E. McKenney #include <linux/moduleparam.h>
2851b1130eSPaul E. McKenney #include <linux/percpu.h>
2951b1130eSPaul E. McKenney #include <linux/notifier.h>
3051b1130eSPaul E. McKenney #include <linux/reboot.h>
3151b1130eSPaul E. McKenney #include <linux/freezer.h>
3251b1130eSPaul E. McKenney #include <linux/cpu.h>
3351b1130eSPaul E. McKenney #include <linux/delay.h>
3451b1130eSPaul E. McKenney #include <linux/stat.h>
3551b1130eSPaul E. McKenney #include <linux/slab.h>
3651b1130eSPaul E. McKenney #include <linux/trace_clock.h>
3731257c3cSPaul E. McKenney #include <linux/ktime.h>
3851b1130eSPaul E. McKenney #include <asm/byteorder.h>
3951b1130eSPaul E. McKenney #include <linux/torture.h>
405d248bb3SDietmar Eggemann #include <linux/sched/rt.h>
41dac95906SPaul E. McKenney #include "rcu/rcu.h"
4251b1130eSPaul E. McKenney
4351b1130eSPaul E. McKenney MODULE_LICENSE("GPL");
448f8e76c0SPaul E. McKenney MODULE_AUTHOR("Paul E. McKenney <paulmck@linux.ibm.com>");
4551b1130eSPaul E. McKenney
468171d3e0SPaul E. McKenney static bool disable_onoff_at_boot;
478171d3e0SPaul E. McKenney module_param(disable_onoff_at_boot, bool, 0444);
488171d3e0SPaul E. McKenney
492102ad29SPaul E. McKenney static bool ftrace_dump_at_shutdown;
502102ad29SPaul E. McKenney module_param(ftrace_dump_at_shutdown, bool, 0444);
512102ad29SPaul E. McKenney
528a67a20bSPaul E. McKenney static int verbose_sleep_frequency;
538a67a20bSPaul E. McKenney module_param(verbose_sleep_frequency, int, 0444);
548a67a20bSPaul E. McKenney
558a67a20bSPaul E. McKenney static int verbose_sleep_duration = 1;
568a67a20bSPaul E. McKenney module_param(verbose_sleep_duration, int, 0444);
578a67a20bSPaul E. McKenney
58e2a0b786SConnor O'Brien static int random_shuffle;
59e2a0b786SConnor O'Brien module_param(random_shuffle, int, 0444);
60e2a0b786SConnor O'Brien
61b5daa8f3SPaul E. McKenney static char *torture_type;
6290127d60SPaul E. McKenney static int verbose;
63b5daa8f3SPaul E. McKenney
6436970bb9SPaul E. McKenney /* Mediate rmmod and system shutdown. Concurrent rmmod & shutdown illegal! */
6536970bb9SPaul E. McKenney #define FULLSTOP_DONTSTOP 0 /* Normal operation. */
6636970bb9SPaul E. McKenney #define FULLSTOP_SHUTDOWN 1 /* System shutdown with torture running. */
6736970bb9SPaul E. McKenney #define FULLSTOP_RMMOD 2 /* Normal rmmod of torture. */
6836970bb9SPaul E. McKenney static int fullstop = FULLSTOP_RMMOD;
694622b487SPaul E. McKenney static DEFINE_MUTEX(fullstop_mutex);
70f67a3356SPaul E. McKenney
718a67a20bSPaul E. McKenney static atomic_t verbose_sleep_counter;
728a67a20bSPaul E. McKenney
738a67a20bSPaul E. McKenney /*
748a67a20bSPaul E. McKenney * Sleep if needed from VERBOSE_TOROUT*().
758a67a20bSPaul E. McKenney */
verbose_torout_sleep(void)768a67a20bSPaul E. McKenney void verbose_torout_sleep(void)
778a67a20bSPaul E. McKenney {
788a67a20bSPaul E. McKenney if (verbose_sleep_frequency > 0 &&
798a67a20bSPaul E. McKenney verbose_sleep_duration > 0 &&
808a67a20bSPaul E. McKenney !(atomic_inc_return(&verbose_sleep_counter) % verbose_sleep_frequency))
818a67a20bSPaul E. McKenney schedule_timeout_uninterruptible(verbose_sleep_duration);
828a67a20bSPaul E. McKenney }
838a67a20bSPaul E. McKenney EXPORT_SYMBOL_GPL(verbose_torout_sleep);
848a67a20bSPaul E. McKenney
85ae19aaafSPaul E. McKenney /*
86ae19aaafSPaul E. McKenney * Schedule a high-resolution-timer sleep in nanoseconds, with a 32-bit
87ae19aaafSPaul E. McKenney * nanosecond random fuzz. This function and its friends desynchronize
88ae19aaafSPaul E. McKenney * testing from the timer wheel.
89ae19aaafSPaul E. McKenney */
torture_hrtimeout_ns(ktime_t baset_ns,u32 fuzzt_ns,const enum hrtimer_mode mode,struct torture_random_state * trsp)901443ec85SPaul E. McKenney int torture_hrtimeout_ns(ktime_t baset_ns, u32 fuzzt_ns, const enum hrtimer_mode mode,
911443ec85SPaul E. McKenney struct torture_random_state *trsp)
92ae19aaafSPaul E. McKenney {
93ae19aaafSPaul E. McKenney ktime_t hto = baset_ns;
94ae19aaafSPaul E. McKenney
95ae19aaafSPaul E. McKenney if (trsp)
966cab60ceSPaul E. McKenney hto += torture_random(trsp) % fuzzt_ns;
97872948c6SPaul E. McKenney set_current_state(TASK_IDLE);
981443ec85SPaul E. McKenney return schedule_hrtimeout(&hto, mode);
99ae19aaafSPaul E. McKenney }
100ae19aaafSPaul E. McKenney EXPORT_SYMBOL_GPL(torture_hrtimeout_ns);
101ae19aaafSPaul E. McKenney
102ae19aaafSPaul E. McKenney /*
103ae19aaafSPaul E. McKenney * Schedule a high-resolution-timer sleep in microseconds, with a 32-bit
104ae19aaafSPaul E. McKenney * nanosecond (not microsecond!) random fuzz.
105ae19aaafSPaul E. McKenney */
torture_hrtimeout_us(u32 baset_us,u32 fuzzt_ns,struct torture_random_state * trsp)106ae19aaafSPaul E. McKenney int torture_hrtimeout_us(u32 baset_us, u32 fuzzt_ns, struct torture_random_state *trsp)
107ae19aaafSPaul E. McKenney {
108ae19aaafSPaul E. McKenney ktime_t baset_ns = baset_us * NSEC_PER_USEC;
109ae19aaafSPaul E. McKenney
1101443ec85SPaul E. McKenney return torture_hrtimeout_ns(baset_ns, fuzzt_ns, HRTIMER_MODE_REL, trsp);
111ae19aaafSPaul E. McKenney }
112ae19aaafSPaul E. McKenney EXPORT_SYMBOL_GPL(torture_hrtimeout_us);
113ae19aaafSPaul E. McKenney
114ae19aaafSPaul E. McKenney /*
115ae19aaafSPaul E. McKenney * Schedule a high-resolution-timer sleep in milliseconds, with a 32-bit
116ae19aaafSPaul E. McKenney * microsecond (not millisecond!) random fuzz.
117ae19aaafSPaul E. McKenney */
torture_hrtimeout_ms(u32 baset_ms,u32 fuzzt_us,struct torture_random_state * trsp)118ae19aaafSPaul E. McKenney int torture_hrtimeout_ms(u32 baset_ms, u32 fuzzt_us, struct torture_random_state *trsp)
119ae19aaafSPaul E. McKenney {
120ae19aaafSPaul E. McKenney ktime_t baset_ns = baset_ms * NSEC_PER_MSEC;
121ae19aaafSPaul E. McKenney u32 fuzzt_ns;
122ae19aaafSPaul E. McKenney
123ae19aaafSPaul E. McKenney if ((u32)~0U / NSEC_PER_USEC < fuzzt_us)
124ae19aaafSPaul E. McKenney fuzzt_ns = (u32)~0U;
125ae19aaafSPaul E. McKenney else
126ae19aaafSPaul E. McKenney fuzzt_ns = fuzzt_us * NSEC_PER_USEC;
1271443ec85SPaul E. McKenney return torture_hrtimeout_ns(baset_ns, fuzzt_ns, HRTIMER_MODE_REL, trsp);
128ae19aaafSPaul E. McKenney }
129ae19aaafSPaul E. McKenney EXPORT_SYMBOL_GPL(torture_hrtimeout_ms);
130ae19aaafSPaul E. McKenney
131ae19aaafSPaul E. McKenney /*
132ae19aaafSPaul E. McKenney * Schedule a high-resolution-timer sleep in jiffies, with an
133ae19aaafSPaul E. McKenney * implied one-jiffy random fuzz. This is intended to replace calls to
134ae19aaafSPaul E. McKenney * schedule_timeout_interruptible() and friends.
135ae19aaafSPaul E. McKenney */
torture_hrtimeout_jiffies(u32 baset_j,struct torture_random_state * trsp)136ae19aaafSPaul E. McKenney int torture_hrtimeout_jiffies(u32 baset_j, struct torture_random_state *trsp)
137ae19aaafSPaul E. McKenney {
138ae19aaafSPaul E. McKenney ktime_t baset_ns = jiffies_to_nsecs(baset_j);
139ae19aaafSPaul E. McKenney
1401443ec85SPaul E. McKenney return torture_hrtimeout_ns(baset_ns, jiffies_to_nsecs(1), HRTIMER_MODE_REL, trsp);
141ae19aaafSPaul E. McKenney }
142ae19aaafSPaul E. McKenney EXPORT_SYMBOL_GPL(torture_hrtimeout_jiffies);
143ae19aaafSPaul E. McKenney
144ae19aaafSPaul E. McKenney /*
145ae19aaafSPaul E. McKenney * Schedule a high-resolution-timer sleep in milliseconds, with a 32-bit
146ae19aaafSPaul E. McKenney * millisecond (not second!) random fuzz.
147ae19aaafSPaul E. McKenney */
torture_hrtimeout_s(u32 baset_s,u32 fuzzt_ms,struct torture_random_state * trsp)148ae19aaafSPaul E. McKenney int torture_hrtimeout_s(u32 baset_s, u32 fuzzt_ms, struct torture_random_state *trsp)
149ae19aaafSPaul E. McKenney {
150ae19aaafSPaul E. McKenney ktime_t baset_ns = baset_s * NSEC_PER_SEC;
151ae19aaafSPaul E. McKenney u32 fuzzt_ns;
152ae19aaafSPaul E. McKenney
153ae19aaafSPaul E. McKenney if ((u32)~0U / NSEC_PER_MSEC < fuzzt_ms)
154ae19aaafSPaul E. McKenney fuzzt_ns = (u32)~0U;
155ae19aaafSPaul E. McKenney else
156ae19aaafSPaul E. McKenney fuzzt_ns = fuzzt_ms * NSEC_PER_MSEC;
1571443ec85SPaul E. McKenney return torture_hrtimeout_ns(baset_ns, fuzzt_ns, HRTIMER_MODE_REL, trsp);
158ae19aaafSPaul E. McKenney }
159ae19aaafSPaul E. McKenney EXPORT_SYMBOL_GPL(torture_hrtimeout_s);
160ae19aaafSPaul E. McKenney
1612e9e8081SPaul E. McKenney #ifdef CONFIG_HOTPLUG_CPU
1622e9e8081SPaul E. McKenney
1632e9e8081SPaul E. McKenney /*
1642e9e8081SPaul E. McKenney * Variables for online-offline handling. Only present if CPU hotplug
1652e9e8081SPaul E. McKenney * is enabled, otherwise does nothing.
1662e9e8081SPaul E. McKenney */
1672e9e8081SPaul E. McKenney
1682e9e8081SPaul E. McKenney static struct task_struct *onoff_task;
1692e9e8081SPaul E. McKenney static long onoff_holdoff;
1702e9e8081SPaul E. McKenney static long onoff_interval;
1713a6cb58fSPaul E. McKenney static torture_ofl_func *onoff_f;
1722e9e8081SPaul E. McKenney static long n_offline_attempts;
1732e9e8081SPaul E. McKenney static long n_offline_successes;
1742e9e8081SPaul E. McKenney static unsigned long sum_offline;
1752e9e8081SPaul E. McKenney static int min_offline = -1;
1762e9e8081SPaul E. McKenney static int max_offline;
1772e9e8081SPaul E. McKenney static long n_online_attempts;
1782e9e8081SPaul E. McKenney static long n_online_successes;
1792e9e8081SPaul E. McKenney static unsigned long sum_online;
1802e9e8081SPaul E. McKenney static int min_online = -1;
1812e9e8081SPaul E. McKenney static int max_online;
1822e9e8081SPaul E. McKenney
1831afb95feSPaul E. McKenney static int torture_online_cpus = NR_CPUS;
1841afb95feSPaul E. McKenney
1851afb95feSPaul E. McKenney /*
1861afb95feSPaul E. McKenney * Some torture testing leverages confusion as to the number of online
1871afb95feSPaul E. McKenney * CPUs. This function returns the torture-testing view of this number,
1881afb95feSPaul E. McKenney * which allows torture tests to load-balance appropriately.
1891afb95feSPaul E. McKenney */
torture_num_online_cpus(void)1901afb95feSPaul E. McKenney int torture_num_online_cpus(void)
1911afb95feSPaul E. McKenney {
1921afb95feSPaul E. McKenney return READ_ONCE(torture_online_cpus);
1931afb95feSPaul E. McKenney }
1941afb95feSPaul E. McKenney EXPORT_SYMBOL_GPL(torture_num_online_cpus);
1951afb95feSPaul E. McKenney
1962e9e8081SPaul E. McKenney /*
197d95f5ba9SPaul E. McKenney * Attempt to take a CPU offline. Return false if the CPU is already
198d95f5ba9SPaul E. McKenney * offline or if it is not subject to CPU-hotplug operations. The
199d95f5ba9SPaul E. McKenney * caller can detect other failures by looking at the statistics.
200d95f5ba9SPaul E. McKenney */
torture_offline(int cpu,long * n_offl_attempts,long * n_offl_successes,unsigned long * sum_offl,int * min_offl,int * max_offl)201d95f5ba9SPaul E. McKenney bool torture_offline(int cpu, long *n_offl_attempts, long *n_offl_successes,
202d95f5ba9SPaul E. McKenney unsigned long *sum_offl, int *min_offl, int *max_offl)
203d95f5ba9SPaul E. McKenney {
204d95f5ba9SPaul E. McKenney unsigned long delta;
205d95f5ba9SPaul E. McKenney int ret;
206a59ee765SPaul E. McKenney char *s;
207d95f5ba9SPaul E. McKenney unsigned long starttime;
208d95f5ba9SPaul E. McKenney
209d95f5ba9SPaul E. McKenney if (!cpu_online(cpu) || !cpu_is_hotpluggable(cpu))
210d95f5ba9SPaul E. McKenney return false;
21124aca4aeSPaul E. McKenney if (num_online_cpus() <= 1)
21224aca4aeSPaul E. McKenney return false; /* Can't offline the last CPU. */
213d95f5ba9SPaul E. McKenney
21490127d60SPaul E. McKenney if (verbose > 1)
215d95f5ba9SPaul E. McKenney pr_alert("%s" TORTURE_FLAG
216d95f5ba9SPaul E. McKenney "torture_onoff task: offlining %d\n",
217d95f5ba9SPaul E. McKenney torture_type, cpu);
218d95f5ba9SPaul E. McKenney starttime = jiffies;
219d95f5ba9SPaul E. McKenney (*n_offl_attempts)++;
220457bc8edSQais Yousef ret = remove_cpu(cpu);
221d95f5ba9SPaul E. McKenney if (ret) {
222a59ee765SPaul E. McKenney s = "";
223a59ee765SPaul E. McKenney if (!rcu_inkernel_boot_has_ended() && ret == -EBUSY) {
224a59ee765SPaul E. McKenney // PCI probe frequently disables hotplug during boot.
225a59ee765SPaul E. McKenney (*n_offl_attempts)--;
226a59ee765SPaul E. McKenney s = " (-EBUSY forgiven during boot)";
227a59ee765SPaul E. McKenney }
228d95f5ba9SPaul E. McKenney if (verbose)
229d95f5ba9SPaul E. McKenney pr_alert("%s" TORTURE_FLAG
230a59ee765SPaul E. McKenney "torture_onoff task: offline %d failed%s: errno %d\n",
231a59ee765SPaul E. McKenney torture_type, cpu, s, ret);
232d95f5ba9SPaul E. McKenney } else {
23390127d60SPaul E. McKenney if (verbose > 1)
234d95f5ba9SPaul E. McKenney pr_alert("%s" TORTURE_FLAG
235d95f5ba9SPaul E. McKenney "torture_onoff task: offlined %d\n",
236d95f5ba9SPaul E. McKenney torture_type, cpu);
2373a6cb58fSPaul E. McKenney if (onoff_f)
2383a6cb58fSPaul E. McKenney onoff_f();
239d95f5ba9SPaul E. McKenney (*n_offl_successes)++;
240d95f5ba9SPaul E. McKenney delta = jiffies - starttime;
241a2b2df20SPaul E. McKenney *sum_offl += delta;
242d95f5ba9SPaul E. McKenney if (*min_offl < 0) {
243d95f5ba9SPaul E. McKenney *min_offl = delta;
244d95f5ba9SPaul E. McKenney *max_offl = delta;
245d95f5ba9SPaul E. McKenney }
246d95f5ba9SPaul E. McKenney if (*min_offl > delta)
247d95f5ba9SPaul E. McKenney *min_offl = delta;
248d95f5ba9SPaul E. McKenney if (*max_offl < delta)
249d95f5ba9SPaul E. McKenney *max_offl = delta;
2501afb95feSPaul E. McKenney WRITE_ONCE(torture_online_cpus, torture_online_cpus - 1);
2511afb95feSPaul E. McKenney WARN_ON_ONCE(torture_online_cpus <= 0);
252d95f5ba9SPaul E. McKenney }
253d95f5ba9SPaul E. McKenney
254d95f5ba9SPaul E. McKenney return true;
255d95f5ba9SPaul E. McKenney }
256d95f5ba9SPaul E. McKenney EXPORT_SYMBOL_GPL(torture_offline);
257d95f5ba9SPaul E. McKenney
258d95f5ba9SPaul E. McKenney /*
259d95f5ba9SPaul E. McKenney * Attempt to bring a CPU online. Return false if the CPU is already
260d95f5ba9SPaul E. McKenney * online or if it is not subject to CPU-hotplug operations. The
261d95f5ba9SPaul E. McKenney * caller can detect other failures by looking at the statistics.
262d95f5ba9SPaul E. McKenney */
torture_online(int cpu,long * n_onl_attempts,long * n_onl_successes,unsigned long * sum_onl,int * min_onl,int * max_onl)263d95f5ba9SPaul E. McKenney bool torture_online(int cpu, long *n_onl_attempts, long *n_onl_successes,
264d95f5ba9SPaul E. McKenney unsigned long *sum_onl, int *min_onl, int *max_onl)
265d95f5ba9SPaul E. McKenney {
266d95f5ba9SPaul E. McKenney unsigned long delta;
267d95f5ba9SPaul E. McKenney int ret;
268a59ee765SPaul E. McKenney char *s;
269d95f5ba9SPaul E. McKenney unsigned long starttime;
270d95f5ba9SPaul E. McKenney
271d95f5ba9SPaul E. McKenney if (cpu_online(cpu) || !cpu_is_hotpluggable(cpu))
272d95f5ba9SPaul E. McKenney return false;
273d95f5ba9SPaul E. McKenney
27490127d60SPaul E. McKenney if (verbose > 1)
275d95f5ba9SPaul E. McKenney pr_alert("%s" TORTURE_FLAG
276d95f5ba9SPaul E. McKenney "torture_onoff task: onlining %d\n",
277d95f5ba9SPaul E. McKenney torture_type, cpu);
278d95f5ba9SPaul E. McKenney starttime = jiffies;
279d95f5ba9SPaul E. McKenney (*n_onl_attempts)++;
280457bc8edSQais Yousef ret = add_cpu(cpu);
281d95f5ba9SPaul E. McKenney if (ret) {
282a59ee765SPaul E. McKenney s = "";
283a59ee765SPaul E. McKenney if (!rcu_inkernel_boot_has_ended() && ret == -EBUSY) {
284a59ee765SPaul E. McKenney // PCI probe frequently disables hotplug during boot.
285a59ee765SPaul E. McKenney (*n_onl_attempts)--;
286a59ee765SPaul E. McKenney s = " (-EBUSY forgiven during boot)";
287a59ee765SPaul E. McKenney }
288d95f5ba9SPaul E. McKenney if (verbose)
289d95f5ba9SPaul E. McKenney pr_alert("%s" TORTURE_FLAG
290a59ee765SPaul E. McKenney "torture_onoff task: online %d failed%s: errno %d\n",
291a59ee765SPaul E. McKenney torture_type, cpu, s, ret);
292d95f5ba9SPaul E. McKenney } else {
29390127d60SPaul E. McKenney if (verbose > 1)
294d95f5ba9SPaul E. McKenney pr_alert("%s" TORTURE_FLAG
295d95f5ba9SPaul E. McKenney "torture_onoff task: onlined %d\n",
296d95f5ba9SPaul E. McKenney torture_type, cpu);
297d95f5ba9SPaul E. McKenney (*n_onl_successes)++;
298d95f5ba9SPaul E. McKenney delta = jiffies - starttime;
299d95f5ba9SPaul E. McKenney *sum_onl += delta;
300d95f5ba9SPaul E. McKenney if (*min_onl < 0) {
301d95f5ba9SPaul E. McKenney *min_onl = delta;
302d95f5ba9SPaul E. McKenney *max_onl = delta;
303d95f5ba9SPaul E. McKenney }
304d95f5ba9SPaul E. McKenney if (*min_onl > delta)
305d95f5ba9SPaul E. McKenney *min_onl = delta;
306d95f5ba9SPaul E. McKenney if (*max_onl < delta)
307d95f5ba9SPaul E. McKenney *max_onl = delta;
3081afb95feSPaul E. McKenney WRITE_ONCE(torture_online_cpus, torture_online_cpus + 1);
309d95f5ba9SPaul E. McKenney }
310d95f5ba9SPaul E. McKenney
311d95f5ba9SPaul E. McKenney return true;
312d95f5ba9SPaul E. McKenney }
313d95f5ba9SPaul E. McKenney EXPORT_SYMBOL_GPL(torture_online);
314d95f5ba9SPaul E. McKenney
315d95f5ba9SPaul E. McKenney /*
3160b962c8fSPaul E. McKenney * Get everything online at the beginning and ends of tests.
3170b962c8fSPaul E. McKenney */
torture_online_all(char * phase)3180b962c8fSPaul E. McKenney static void torture_online_all(char *phase)
3190b962c8fSPaul E. McKenney {
3200b962c8fSPaul E. McKenney int cpu;
3210b962c8fSPaul E. McKenney int ret;
3220b962c8fSPaul E. McKenney
3230b962c8fSPaul E. McKenney for_each_possible_cpu(cpu) {
3240b962c8fSPaul E. McKenney if (cpu_online(cpu))
3250b962c8fSPaul E. McKenney continue;
3260b962c8fSPaul E. McKenney ret = add_cpu(cpu);
3270b962c8fSPaul E. McKenney if (ret && verbose) {
3280b962c8fSPaul E. McKenney pr_alert("%s" TORTURE_FLAG
3290b962c8fSPaul E. McKenney "%s: %s online %d: errno %d\n",
3300b962c8fSPaul E. McKenney __func__, phase, torture_type, cpu, ret);
3310b962c8fSPaul E. McKenney }
3320b962c8fSPaul E. McKenney }
3330b962c8fSPaul E. McKenney }
3340b962c8fSPaul E. McKenney
3350b962c8fSPaul E. McKenney /*
3362e9e8081SPaul E. McKenney * Execute random CPU-hotplug operations at the interval specified
3372e9e8081SPaul E. McKenney * by the onoff_interval.
3382e9e8081SPaul E. McKenney */
3392e9e8081SPaul E. McKenney static int
torture_onoff(void * arg)3402e9e8081SPaul E. McKenney torture_onoff(void *arg)
3412e9e8081SPaul E. McKenney {
3422e9e8081SPaul E. McKenney int cpu;
3432e9e8081SPaul E. McKenney int maxcpu = -1;
3442e9e8081SPaul E. McKenney DEFINE_TORTURE_RANDOM(rand);
3452e9e8081SPaul E. McKenney
3462e9e8081SPaul E. McKenney VERBOSE_TOROUT_STRING("torture_onoff task started");
3472e9e8081SPaul E. McKenney for_each_online_cpu(cpu)
3482e9e8081SPaul E. McKenney maxcpu = cpu;
3492e9e8081SPaul E. McKenney WARN_ON(maxcpu < 0);
3500b962c8fSPaul E. McKenney torture_online_all("Initial");
351750db0f5SBoqun Feng if (maxcpu == 0) {
352750db0f5SBoqun Feng VERBOSE_TOROUT_STRING("Only one CPU, so CPU-hotplug testing is disabled");
353750db0f5SBoqun Feng goto stop;
354750db0f5SBoqun Feng }
355750db0f5SBoqun Feng
3562e9e8081SPaul E. McKenney if (onoff_holdoff > 0) {
3572e9e8081SPaul E. McKenney VERBOSE_TOROUT_STRING("torture_onoff begin holdoff");
3583f0c06e1SPaul E. McKenney torture_hrtimeout_jiffies(onoff_holdoff, &rand);
3592e9e8081SPaul E. McKenney VERBOSE_TOROUT_STRING("torture_onoff end holdoff");
3602e9e8081SPaul E. McKenney }
3612e9e8081SPaul E. McKenney while (!torture_must_stop()) {
3628171d3e0SPaul E. McKenney if (disable_onoff_at_boot && !rcu_inkernel_boot_has_ended()) {
3633f0c06e1SPaul E. McKenney torture_hrtimeout_jiffies(HZ / 10, &rand);
3648171d3e0SPaul E. McKenney continue;
3658171d3e0SPaul E. McKenney }
3666cab60ceSPaul E. McKenney cpu = torture_random(&rand) % (maxcpu + 1);
367d95f5ba9SPaul E. McKenney if (!torture_offline(cpu,
368d95f5ba9SPaul E. McKenney &n_offline_attempts, &n_offline_successes,
369d95f5ba9SPaul E. McKenney &sum_offline, &min_offline, &max_offline))
370d95f5ba9SPaul E. McKenney torture_online(cpu,
371d95f5ba9SPaul E. McKenney &n_online_attempts, &n_online_successes,
372d95f5ba9SPaul E. McKenney &sum_online, &min_online, &max_online);
3733f0c06e1SPaul E. McKenney torture_hrtimeout_jiffies(onoff_interval, &rand);
3742e9e8081SPaul E. McKenney }
375750db0f5SBoqun Feng
376750db0f5SBoqun Feng stop:
3777fafaac5SPaul E. McKenney torture_kthread_stopping("torture_onoff");
3780b962c8fSPaul E. McKenney torture_online_all("Final");
3792e9e8081SPaul E. McKenney return 0;
3802e9e8081SPaul E. McKenney }
3812e9e8081SPaul E. McKenney
3822e9e8081SPaul E. McKenney #endif /* #ifdef CONFIG_HOTPLUG_CPU */
3832e9e8081SPaul E. McKenney
3842e9e8081SPaul E. McKenney /*
3852e9e8081SPaul E. McKenney * Initiate online-offline handling.
3862e9e8081SPaul E. McKenney */
torture_onoff_init(long ooholdoff,long oointerval,torture_ofl_func * f)3873a6cb58fSPaul E. McKenney int torture_onoff_init(long ooholdoff, long oointerval, torture_ofl_func *f)
3882e9e8081SPaul E. McKenney {
38947cf29b9SPaul E. McKenney #ifdef CONFIG_HOTPLUG_CPU
3902e9e8081SPaul E. McKenney onoff_holdoff = ooholdoff;
3912e9e8081SPaul E. McKenney onoff_interval = oointerval;
3923a6cb58fSPaul E. McKenney onoff_f = f;
3932e9e8081SPaul E. McKenney if (onoff_interval <= 0)
3942e9e8081SPaul E. McKenney return 0;
3952a7d9688SPierce Griffiths return torture_create_kthread(torture_onoff, NULL, onoff_task);
3962a7d9688SPierce Griffiths #else /* #ifdef CONFIG_HOTPLUG_CPU */
3972a7d9688SPierce Griffiths return 0;
3982a7d9688SPierce Griffiths #endif /* #else #ifdef CONFIG_HOTPLUG_CPU */
3992e9e8081SPaul E. McKenney }
4002e9e8081SPaul E. McKenney EXPORT_SYMBOL_GPL(torture_onoff_init);
4012e9e8081SPaul E. McKenney
4022e9e8081SPaul E. McKenney /*
4032e9e8081SPaul E. McKenney * Clean up after online/offline testing.
4042e9e8081SPaul E. McKenney */
torture_onoff_cleanup(void)405cc47ae08SPaul E. McKenney static void torture_onoff_cleanup(void)
4062e9e8081SPaul E. McKenney {
4072e9e8081SPaul E. McKenney #ifdef CONFIG_HOTPLUG_CPU
4082e9e8081SPaul E. McKenney if (onoff_task == NULL)
4092e9e8081SPaul E. McKenney return;
4102e9e8081SPaul E. McKenney VERBOSE_TOROUT_STRING("Stopping torture_onoff task");
4112e9e8081SPaul E. McKenney kthread_stop(onoff_task);
4122e9e8081SPaul E. McKenney onoff_task = NULL;
4132e9e8081SPaul E. McKenney #endif /* #ifdef CONFIG_HOTPLUG_CPU */
4142e9e8081SPaul E. McKenney }
4152e9e8081SPaul E. McKenney
4162e9e8081SPaul E. McKenney /*
4172e9e8081SPaul E. McKenney * Print online/offline testing statistics.
4182e9e8081SPaul E. McKenney */
torture_onoff_stats(void)419eea203feSJoe Perches void torture_onoff_stats(void)
4202e9e8081SPaul E. McKenney {
4212e9e8081SPaul E. McKenney #ifdef CONFIG_HOTPLUG_CPU
422eea203feSJoe Perches pr_cont("onoff: %ld/%ld:%ld/%ld %d,%d:%d,%d %lu:%lu (HZ=%d) ",
4232e9e8081SPaul E. McKenney n_online_successes, n_online_attempts,
4242e9e8081SPaul E. McKenney n_offline_successes, n_offline_attempts,
4252e9e8081SPaul E. McKenney min_online, max_online,
4262e9e8081SPaul E. McKenney min_offline, max_offline,
4272e9e8081SPaul E. McKenney sum_online, sum_offline, HZ);
4282e9e8081SPaul E. McKenney #endif /* #ifdef CONFIG_HOTPLUG_CPU */
4292e9e8081SPaul E. McKenney }
4302e9e8081SPaul E. McKenney EXPORT_SYMBOL_GPL(torture_onoff_stats);
4312e9e8081SPaul E. McKenney
4322e9e8081SPaul E. McKenney /*
4332e9e8081SPaul E. McKenney * Were all the online/offline operations successful?
4342e9e8081SPaul E. McKenney */
torture_onoff_failures(void)4352e9e8081SPaul E. McKenney bool torture_onoff_failures(void)
4362e9e8081SPaul E. McKenney {
4372e9e8081SPaul E. McKenney #ifdef CONFIG_HOTPLUG_CPU
4382e9e8081SPaul E. McKenney return n_online_successes != n_online_attempts ||
4392e9e8081SPaul E. McKenney n_offline_successes != n_offline_attempts;
4402e9e8081SPaul E. McKenney #else /* #ifdef CONFIG_HOTPLUG_CPU */
4412e9e8081SPaul E. McKenney return false;
4422e9e8081SPaul E. McKenney #endif /* #else #ifdef CONFIG_HOTPLUG_CPU */
4432e9e8081SPaul E. McKenney }
4442e9e8081SPaul E. McKenney EXPORT_SYMBOL_GPL(torture_onoff_failures);
4452e9e8081SPaul E. McKenney
44651b1130eSPaul E. McKenney #define TORTURE_RANDOM_MULT 39916801 /* prime */
44751b1130eSPaul E. McKenney #define TORTURE_RANDOM_ADD 479001701 /* prime */
44851b1130eSPaul E. McKenney #define TORTURE_RANDOM_REFRESH 10000
44951b1130eSPaul E. McKenney
45051b1130eSPaul E. McKenney /*
45151b1130eSPaul E. McKenney * Crude but fast random-number generator. Uses a linear congruential
45251b1130eSPaul E. McKenney * generator, with occasional help from cpu_clock().
45351b1130eSPaul E. McKenney */
45451b1130eSPaul E. McKenney unsigned long
torture_random(struct torture_random_state * trsp)45551b1130eSPaul E. McKenney torture_random(struct torture_random_state *trsp)
45651b1130eSPaul E. McKenney {
45751b1130eSPaul E. McKenney if (--trsp->trs_count < 0) {
45895f93e97SPaul E. McKenney trsp->trs_state += (unsigned long)local_clock() + raw_smp_processor_id();
45951b1130eSPaul E. McKenney trsp->trs_count = TORTURE_RANDOM_REFRESH;
46051b1130eSPaul E. McKenney }
46151b1130eSPaul E. McKenney trsp->trs_state = trsp->trs_state * TORTURE_RANDOM_MULT +
46251b1130eSPaul E. McKenney TORTURE_RANDOM_ADD;
46351b1130eSPaul E. McKenney return swahw32(trsp->trs_state);
46451b1130eSPaul E. McKenney }
46551b1130eSPaul E. McKenney EXPORT_SYMBOL_GPL(torture_random);
466f67a3356SPaul E. McKenney
467f67a3356SPaul E. McKenney /*
4683808dc9fSPaul E. McKenney * Variables for shuffling. The idea is to ensure that each CPU stays
4693808dc9fSPaul E. McKenney * idle for an extended period to test interactions with dyntick idle,
470b564d62eSMasahiro Yamada * as well as interactions with any per-CPU variables.
4713808dc9fSPaul E. McKenney */
4723808dc9fSPaul E. McKenney struct shuffle_task {
4733808dc9fSPaul E. McKenney struct list_head st_l;
4743808dc9fSPaul E. McKenney struct task_struct *st_t;
4753808dc9fSPaul E. McKenney };
4763808dc9fSPaul E. McKenney
4773808dc9fSPaul E. McKenney static long shuffle_interval; /* In jiffies. */
4783808dc9fSPaul E. McKenney static struct task_struct *shuffler_task;
4793808dc9fSPaul E. McKenney static cpumask_var_t shuffle_tmp_mask;
4803808dc9fSPaul E. McKenney static int shuffle_idle_cpu; /* Force all torture tasks off this CPU */
4813808dc9fSPaul E. McKenney static struct list_head shuffle_task_list = LIST_HEAD_INIT(shuffle_task_list);
4823808dc9fSPaul E. McKenney static DEFINE_MUTEX(shuffle_task_mutex);
4833808dc9fSPaul E. McKenney
4843808dc9fSPaul E. McKenney /*
4853808dc9fSPaul E. McKenney * Register a task to be shuffled. If there is no memory, just splat
4863808dc9fSPaul E. McKenney * and don't bother registering.
4873808dc9fSPaul E. McKenney */
torture_shuffle_task_register(struct task_struct * tp)4883808dc9fSPaul E. McKenney void torture_shuffle_task_register(struct task_struct *tp)
4893808dc9fSPaul E. McKenney {
4903808dc9fSPaul E. McKenney struct shuffle_task *stp;
4913808dc9fSPaul E. McKenney
4923808dc9fSPaul E. McKenney if (WARN_ON_ONCE(tp == NULL))
4933808dc9fSPaul E. McKenney return;
4943808dc9fSPaul E. McKenney stp = kmalloc(sizeof(*stp), GFP_KERNEL);
4953808dc9fSPaul E. McKenney if (WARN_ON_ONCE(stp == NULL))
4963808dc9fSPaul E. McKenney return;
4973808dc9fSPaul E. McKenney stp->st_t = tp;
4983808dc9fSPaul E. McKenney mutex_lock(&shuffle_task_mutex);
4993808dc9fSPaul E. McKenney list_add(&stp->st_l, &shuffle_task_list);
5003808dc9fSPaul E. McKenney mutex_unlock(&shuffle_task_mutex);
5013808dc9fSPaul E. McKenney }
5023808dc9fSPaul E. McKenney EXPORT_SYMBOL_GPL(torture_shuffle_task_register);
5033808dc9fSPaul E. McKenney
5043808dc9fSPaul E. McKenney /*
5053808dc9fSPaul E. McKenney * Unregister all tasks, for example, at the end of the torture run.
5063808dc9fSPaul E. McKenney */
torture_shuffle_task_unregister_all(void)5073808dc9fSPaul E. McKenney static void torture_shuffle_task_unregister_all(void)
5083808dc9fSPaul E. McKenney {
5093808dc9fSPaul E. McKenney struct shuffle_task *stp;
5103808dc9fSPaul E. McKenney struct shuffle_task *p;
5113808dc9fSPaul E. McKenney
5123808dc9fSPaul E. McKenney mutex_lock(&shuffle_task_mutex);
5133808dc9fSPaul E. McKenney list_for_each_entry_safe(stp, p, &shuffle_task_list, st_l) {
5143808dc9fSPaul E. McKenney list_del(&stp->st_l);
5153808dc9fSPaul E. McKenney kfree(stp);
5163808dc9fSPaul E. McKenney }
5173808dc9fSPaul E. McKenney mutex_unlock(&shuffle_task_mutex);
5183808dc9fSPaul E. McKenney }
5193808dc9fSPaul E. McKenney
5203808dc9fSPaul E. McKenney /* Shuffle tasks such that we allow shuffle_idle_cpu to become idle.
5213808dc9fSPaul E. McKenney * A special case is when shuffle_idle_cpu = -1, in which case we allow
5223808dc9fSPaul E. McKenney * the tasks to run on all CPUs.
5233808dc9fSPaul E. McKenney */
torture_shuffle_tasks(void)5243808dc9fSPaul E. McKenney static void torture_shuffle_tasks(void)
5253808dc9fSPaul E. McKenney {
526e2a0b786SConnor O'Brien DEFINE_TORTURE_RANDOM(rand);
5273808dc9fSPaul E. McKenney struct shuffle_task *stp;
5283808dc9fSPaul E. McKenney
5293808dc9fSPaul E. McKenney cpumask_setall(shuffle_tmp_mask);
530ed4fa244SSebastian Andrzej Siewior cpus_read_lock();
5313808dc9fSPaul E. McKenney
5323808dc9fSPaul E. McKenney /* No point in shuffling if there is only one online CPU (ex: UP) */
5333808dc9fSPaul E. McKenney if (num_online_cpus() == 1) {
534ed4fa244SSebastian Andrzej Siewior cpus_read_unlock();
5353808dc9fSPaul E. McKenney return;
5363808dc9fSPaul E. McKenney }
5373808dc9fSPaul E. McKenney
5383808dc9fSPaul E. McKenney /* Advance to the next CPU. Upon overflow, don't idle any CPUs. */
5393808dc9fSPaul E. McKenney shuffle_idle_cpu = cpumask_next(shuffle_idle_cpu, shuffle_tmp_mask);
5403808dc9fSPaul E. McKenney if (shuffle_idle_cpu >= nr_cpu_ids)
5413808dc9fSPaul E. McKenney shuffle_idle_cpu = -1;
5425ed63b19SIulia Manda else
5433808dc9fSPaul E. McKenney cpumask_clear_cpu(shuffle_idle_cpu, shuffle_tmp_mask);
5443808dc9fSPaul E. McKenney
5453808dc9fSPaul E. McKenney mutex_lock(&shuffle_task_mutex);
546e2a0b786SConnor O'Brien list_for_each_entry(stp, &shuffle_task_list, st_l) {
547e2a0b786SConnor O'Brien if (!random_shuffle || torture_random(&rand) & 0x1)
5483808dc9fSPaul E. McKenney set_cpus_allowed_ptr(stp->st_t, shuffle_tmp_mask);
549e2a0b786SConnor O'Brien }
5503808dc9fSPaul E. McKenney mutex_unlock(&shuffle_task_mutex);
5513808dc9fSPaul E. McKenney
552ed4fa244SSebastian Andrzej Siewior cpus_read_unlock();
5533808dc9fSPaul E. McKenney }
5543808dc9fSPaul E. McKenney
5553808dc9fSPaul E. McKenney /* Shuffle tasks across CPUs, with the intent of allowing each CPU in the
5563808dc9fSPaul E. McKenney * system to become idle at a time and cut off its timer ticks. This is meant
5573808dc9fSPaul E. McKenney * to test the support for such tickless idle CPU in RCU.
5583808dc9fSPaul E. McKenney */
torture_shuffle(void * arg)5593808dc9fSPaul E. McKenney static int torture_shuffle(void *arg)
5603808dc9fSPaul E. McKenney {
561dea81dcfSPaul E. McKenney DEFINE_TORTURE_RANDOM(rand);
562dea81dcfSPaul E. McKenney
5633808dc9fSPaul E. McKenney VERBOSE_TOROUT_STRING("torture_shuffle task started");
5643808dc9fSPaul E. McKenney do {
565dea81dcfSPaul E. McKenney torture_hrtimeout_jiffies(shuffle_interval, &rand);
5663808dc9fSPaul E. McKenney torture_shuffle_tasks();
5673808dc9fSPaul E. McKenney torture_shutdown_absorb("torture_shuffle");
5683808dc9fSPaul E. McKenney } while (!torture_must_stop());
5697fafaac5SPaul E. McKenney torture_kthread_stopping("torture_shuffle");
5703808dc9fSPaul E. McKenney return 0;
5713808dc9fSPaul E. McKenney }
5723808dc9fSPaul E. McKenney
5733808dc9fSPaul E. McKenney /*
5743808dc9fSPaul E. McKenney * Start the shuffler, with shuffint in jiffies.
5753808dc9fSPaul E. McKenney */
torture_shuffle_init(long shuffint)5763808dc9fSPaul E. McKenney int torture_shuffle_init(long shuffint)
5773808dc9fSPaul E. McKenney {
5783808dc9fSPaul E. McKenney shuffle_interval = shuffint;
5793808dc9fSPaul E. McKenney
5803808dc9fSPaul E. McKenney shuffle_idle_cpu = -1;
5813808dc9fSPaul E. McKenney
5823808dc9fSPaul E. McKenney if (!alloc_cpumask_var(&shuffle_tmp_mask, GFP_KERNEL)) {
58381faa4f6SLi Zhijian TOROUT_ERRSTRING("Failed to alloc mask");
5843808dc9fSPaul E. McKenney return -ENOMEM;
5853808dc9fSPaul E. McKenney }
5863808dc9fSPaul E. McKenney
5873808dc9fSPaul E. McKenney /* Create the shuffler thread */
58847cf29b9SPaul E. McKenney return torture_create_kthread(torture_shuffle, NULL, shuffler_task);
5893808dc9fSPaul E. McKenney }
5903808dc9fSPaul E. McKenney EXPORT_SYMBOL_GPL(torture_shuffle_init);
5913808dc9fSPaul E. McKenney
5923808dc9fSPaul E. McKenney /*
5933808dc9fSPaul E. McKenney * Stop the shuffling.
5943808dc9fSPaul E. McKenney */
torture_shuffle_cleanup(void)595cc47ae08SPaul E. McKenney static void torture_shuffle_cleanup(void)
5963808dc9fSPaul E. McKenney {
5973808dc9fSPaul E. McKenney torture_shuffle_task_unregister_all();
5983808dc9fSPaul E. McKenney if (shuffler_task) {
5993808dc9fSPaul E. McKenney VERBOSE_TOROUT_STRING("Stopping torture_shuffle task");
6003808dc9fSPaul E. McKenney kthread_stop(shuffler_task);
6013808dc9fSPaul E. McKenney free_cpumask_var(shuffle_tmp_mask);
6023808dc9fSPaul E. McKenney }
6033808dc9fSPaul E. McKenney shuffler_task = NULL;
6043808dc9fSPaul E. McKenney }
6053808dc9fSPaul E. McKenney
6063808dc9fSPaul E. McKenney /*
607e991dbc0SPaul E. McKenney * Variables for auto-shutdown. This allows "lights out" torture runs
608e991dbc0SPaul E. McKenney * to be fully scripted.
609e991dbc0SPaul E. McKenney */
610e991dbc0SPaul E. McKenney static struct task_struct *shutdown_task;
61131257c3cSPaul E. McKenney static ktime_t shutdown_time; /* time to system shutdown. */
612e991dbc0SPaul E. McKenney static void (*torture_shutdown_hook)(void);
613e991dbc0SPaul E. McKenney
614e991dbc0SPaul E. McKenney /*
615f67a3356SPaul E. McKenney * Absorb kthreads into a kernel function that won't return, so that
616f67a3356SPaul E. McKenney * they won't ever access module text or data again.
617f67a3356SPaul E. McKenney */
torture_shutdown_absorb(const char * title)618f67a3356SPaul E. McKenney void torture_shutdown_absorb(const char *title)
619f67a3356SPaul E. McKenney {
6207d0ae808SPaul E. McKenney while (READ_ONCE(fullstop) == FULLSTOP_SHUTDOWN) {
6214622b487SPaul E. McKenney pr_notice("torture thread %s parking due to system shutdown\n",
622f67a3356SPaul E. McKenney title);
623f67a3356SPaul E. McKenney schedule_timeout_uninterruptible(MAX_SCHEDULE_TIMEOUT);
624f67a3356SPaul E. McKenney }
625f67a3356SPaul E. McKenney }
626f67a3356SPaul E. McKenney EXPORT_SYMBOL_GPL(torture_shutdown_absorb);
627b5daa8f3SPaul E. McKenney
628b5daa8f3SPaul E. McKenney /*
629e991dbc0SPaul E. McKenney * Cause the torture test to shutdown the system after the test has
630e991dbc0SPaul E. McKenney * run for the time specified by the shutdown_secs parameter.
631e991dbc0SPaul E. McKenney */
torture_shutdown(void * arg)632e991dbc0SPaul E. McKenney static int torture_shutdown(void *arg)
633e991dbc0SPaul E. McKenney {
63431257c3cSPaul E. McKenney ktime_t ktime_snap;
635e991dbc0SPaul E. McKenney
636e991dbc0SPaul E. McKenney VERBOSE_TOROUT_STRING("torture_shutdown task started");
63731257c3cSPaul E. McKenney ktime_snap = ktime_get();
63831257c3cSPaul E. McKenney while (ktime_before(ktime_snap, shutdown_time) &&
639e991dbc0SPaul E. McKenney !torture_must_stop()) {
640e991dbc0SPaul E. McKenney if (verbose)
641e991dbc0SPaul E. McKenney pr_alert("%s" TORTURE_FLAG
64231257c3cSPaul E. McKenney "torture_shutdown task: %llu ms remaining\n",
64331257c3cSPaul E. McKenney torture_type,
64431257c3cSPaul E. McKenney ktime_ms_delta(shutdown_time, ktime_snap));
64531257c3cSPaul E. McKenney set_current_state(TASK_INTERRUPTIBLE);
64631257c3cSPaul E. McKenney schedule_hrtimeout(&shutdown_time, HRTIMER_MODE_ABS);
64731257c3cSPaul E. McKenney ktime_snap = ktime_get();
648e991dbc0SPaul E. McKenney }
649e991dbc0SPaul E. McKenney if (torture_must_stop()) {
6507fafaac5SPaul E. McKenney torture_kthread_stopping("torture_shutdown");
651e991dbc0SPaul E. McKenney return 0;
652e991dbc0SPaul E. McKenney }
653e991dbc0SPaul E. McKenney
654e991dbc0SPaul E. McKenney /* OK, shut down the system. */
655e991dbc0SPaul E. McKenney
656e991dbc0SPaul E. McKenney VERBOSE_TOROUT_STRING("torture_shutdown task shutting down system");
657e991dbc0SPaul E. McKenney shutdown_task = NULL; /* Avoid self-kill deadlock. */
658f881825aSPaul E. McKenney if (torture_shutdown_hook)
659f881825aSPaul E. McKenney torture_shutdown_hook();
660f881825aSPaul E. McKenney else
661f881825aSPaul E. McKenney VERBOSE_TOROUT_STRING("No torture_shutdown_hook(), skipping.");
6622102ad29SPaul E. McKenney if (ftrace_dump_at_shutdown)
663dac95906SPaul E. McKenney rcu_ftrace_dump(DUMP_ALL);
664e991dbc0SPaul E. McKenney kernel_power_off(); /* Shut down the system. */
665e991dbc0SPaul E. McKenney return 0;
666e991dbc0SPaul E. McKenney }
667e991dbc0SPaul E. McKenney
668e991dbc0SPaul E. McKenney /*
669e991dbc0SPaul E. McKenney * Start up the shutdown task.
670e991dbc0SPaul E. McKenney */
torture_shutdown_init(int ssecs,void (* cleanup)(void))671e991dbc0SPaul E. McKenney int torture_shutdown_init(int ssecs, void (*cleanup)(void))
672e991dbc0SPaul E. McKenney {
673e991dbc0SPaul E. McKenney torture_shutdown_hook = cleanup;
67431257c3cSPaul E. McKenney if (ssecs > 0) {
67531257c3cSPaul E. McKenney shutdown_time = ktime_add(ktime_get(), ktime_set(ssecs, 0));
6762a7d9688SPierce Griffiths return torture_create_kthread(torture_shutdown, NULL,
67747cf29b9SPaul E. McKenney shutdown_task);
67847cf29b9SPaul E. McKenney }
6792a7d9688SPierce Griffiths return 0;
680e991dbc0SPaul E. McKenney }
681e991dbc0SPaul E. McKenney EXPORT_SYMBOL_GPL(torture_shutdown_init);
682e991dbc0SPaul E. McKenney
683e991dbc0SPaul E. McKenney /*
6844622b487SPaul E. McKenney * Detect and respond to a system shutdown.
6854622b487SPaul E. McKenney */
torture_shutdown_notify(struct notifier_block * unused1,unsigned long unused2,void * unused3)6864622b487SPaul E. McKenney static int torture_shutdown_notify(struct notifier_block *unused1,
6874622b487SPaul E. McKenney unsigned long unused2, void *unused3)
6884622b487SPaul E. McKenney {
6894622b487SPaul E. McKenney mutex_lock(&fullstop_mutex);
6907d0ae808SPaul E. McKenney if (READ_ONCE(fullstop) == FULLSTOP_DONTSTOP) {
691fac480efSPaul E. McKenney VERBOSE_TOROUT_STRING("Unscheduled system shutdown detected");
6927d0ae808SPaul E. McKenney WRITE_ONCE(fullstop, FULLSTOP_SHUTDOWN);
693fac480efSPaul E. McKenney } else {
6944622b487SPaul E. McKenney pr_warn("Concurrent rmmod and shutdown illegal!\n");
695fac480efSPaul E. McKenney }
6964622b487SPaul E. McKenney mutex_unlock(&fullstop_mutex);
6974622b487SPaul E. McKenney return NOTIFY_DONE;
6984622b487SPaul E. McKenney }
6994622b487SPaul E. McKenney
7004622b487SPaul E. McKenney static struct notifier_block torture_shutdown_nb = {
7014622b487SPaul E. McKenney .notifier_call = torture_shutdown_notify,
7024622b487SPaul E. McKenney };
7034622b487SPaul E. McKenney
7044622b487SPaul E. McKenney /*
705bfefc73aSPaul E. McKenney * Shut down the shutdown task. Say what??? Heh! This can happen if
706bfefc73aSPaul E. McKenney * the torture module gets an rmmod before the shutdown time arrives. ;-)
707bfefc73aSPaul E. McKenney */
torture_shutdown_cleanup(void)708bfefc73aSPaul E. McKenney static void torture_shutdown_cleanup(void)
709bfefc73aSPaul E. McKenney {
710bfefc73aSPaul E. McKenney unregister_reboot_notifier(&torture_shutdown_nb);
711bfefc73aSPaul E. McKenney if (shutdown_task != NULL) {
712bfefc73aSPaul E. McKenney VERBOSE_TOROUT_STRING("Stopping torture_shutdown task");
713bfefc73aSPaul E. McKenney kthread_stop(shutdown_task);
714bfefc73aSPaul E. McKenney }
715bfefc73aSPaul E. McKenney shutdown_task = NULL;
716bfefc73aSPaul E. McKenney }
717bfefc73aSPaul E. McKenney
718bfefc73aSPaul E. McKenney /*
719628edaa5SPaul E. McKenney * Variables for stuttering, which means to periodically pause and
720628edaa5SPaul E. McKenney * restart testing in order to catch bugs that appear when load is
721628edaa5SPaul E. McKenney * suddenly applied to or removed from the system.
722628edaa5SPaul E. McKenney */
723628edaa5SPaul E. McKenney static struct task_struct *stutter_task;
724*5268d95cSJoel Fernandes (Google) static ktime_t stutter_till_abs_time;
725628edaa5SPaul E. McKenney static int stutter;
726ff3bf92dSPaul E. McKenney static int stutter_gap;
727628edaa5SPaul E. McKenney
728628edaa5SPaul E. McKenney /*
729628edaa5SPaul E. McKenney * Block until the stutter interval ends. This must be called periodically
730628edaa5SPaul E. McKenney * by all running kthreads that need to be subject to stuttering.
731628edaa5SPaul E. McKenney */
stutter_wait(const char * title)732474e59b4SPaul E. McKenney bool stutter_wait(const char *title)
733628edaa5SPaul E. McKenney {
734e8516c64SPaul E. McKenney bool ret = false;
735*5268d95cSJoel Fernandes (Google) ktime_t till_ns;
7364ced3314SPaul E. McKenney
737cee43939SPaul E. McKenney cond_resched_tasks_rcu_qs();
738*5268d95cSJoel Fernandes (Google) till_ns = READ_ONCE(stutter_till_abs_time);
739*5268d95cSJoel Fernandes (Google) if (till_ns && ktime_before(ktime_get(), till_ns)) {
740*5268d95cSJoel Fernandes (Google) torture_hrtimeout_ns(till_ns, 0, HRTIMER_MODE_ABS, NULL);
741e8516c64SPaul E. McKenney ret = true;
742ab1b7880SPaul E. McKenney }
743628edaa5SPaul E. McKenney torture_shutdown_absorb(title);
744e8516c64SPaul E. McKenney return ret;
745628edaa5SPaul E. McKenney }
746628edaa5SPaul E. McKenney EXPORT_SYMBOL_GPL(stutter_wait);
747628edaa5SPaul E. McKenney
748628edaa5SPaul E. McKenney /*
749628edaa5SPaul E. McKenney * Cause the torture test to "stutter", starting and stopping all
750628edaa5SPaul E. McKenney * threads periodically.
751628edaa5SPaul E. McKenney */
torture_stutter(void * arg)752628edaa5SPaul E. McKenney static int torture_stutter(void *arg)
753628edaa5SPaul E. McKenney {
754*5268d95cSJoel Fernandes (Google) ktime_t till_ns;
755e8516c64SPaul E. McKenney
756628edaa5SPaul E. McKenney VERBOSE_TOROUT_STRING("torture_stutter task started");
757628edaa5SPaul E. McKenney do {
7584ced3314SPaul E. McKenney if (!torture_must_stop() && stutter > 1) {
759*5268d95cSJoel Fernandes (Google) till_ns = ktime_add_ns(ktime_get(),
760*5268d95cSJoel Fernandes (Google) jiffies_to_nsecs(stutter));
761*5268d95cSJoel Fernandes (Google) WRITE_ONCE(stutter_till_abs_time, till_ns);
762*5268d95cSJoel Fernandes (Google) torture_hrtimeout_jiffies(stutter - 1, NULL);
763e8516c64SPaul E. McKenney }
764628edaa5SPaul E. McKenney if (!torture_must_stop())
765ed24affaSPaul E. McKenney torture_hrtimeout_jiffies(stutter_gap, NULL);
766628edaa5SPaul E. McKenney torture_shutdown_absorb("torture_stutter");
767628edaa5SPaul E. McKenney } while (!torture_must_stop());
7687fafaac5SPaul E. McKenney torture_kthread_stopping("torture_stutter");
769628edaa5SPaul E. McKenney return 0;
770628edaa5SPaul E. McKenney }
771628edaa5SPaul E. McKenney
772628edaa5SPaul E. McKenney /*
773628edaa5SPaul E. McKenney * Initialize and kick off the torture_stutter kthread.
774628edaa5SPaul E. McKenney */
torture_stutter_init(const int s,const int sgap)775ff3bf92dSPaul E. McKenney int torture_stutter_init(const int s, const int sgap)
776628edaa5SPaul E. McKenney {
777628edaa5SPaul E. McKenney stutter = s;
778ff3bf92dSPaul E. McKenney stutter_gap = sgap;
7792a7d9688SPierce Griffiths return torture_create_kthread(torture_stutter, NULL, stutter_task);
780628edaa5SPaul E. McKenney }
781628edaa5SPaul E. McKenney EXPORT_SYMBOL_GPL(torture_stutter_init);
782628edaa5SPaul E. McKenney
783628edaa5SPaul E. McKenney /*
784628edaa5SPaul E. McKenney * Cleanup after the torture_stutter kthread.
785628edaa5SPaul E. McKenney */
torture_stutter_cleanup(void)786bfefc73aSPaul E. McKenney static void torture_stutter_cleanup(void)
787628edaa5SPaul E. McKenney {
788628edaa5SPaul E. McKenney if (!stutter_task)
789628edaa5SPaul E. McKenney return;
790628edaa5SPaul E. McKenney VERBOSE_TOROUT_STRING("Stopping torture_stutter task");
791628edaa5SPaul E. McKenney kthread_stop(stutter_task);
792628edaa5SPaul E. McKenney stutter_task = NULL;
793628edaa5SPaul E. McKenney }
794628edaa5SPaul E. McKenney
795628edaa5SPaul E. McKenney /*
796b5daa8f3SPaul E. McKenney * Initialize torture module. Please note that this is -not- invoked via
797b5daa8f3SPaul E. McKenney * the usual module_init() mechanism, but rather by an explicit call from
798b5daa8f3SPaul E. McKenney * the client torture module. This call must be paired with a later
799b5daa8f3SPaul E. McKenney * torture_init_end().
800628edaa5SPaul E. McKenney *
801628edaa5SPaul E. McKenney * The runnable parameter points to a flag that controls whether or not
802628edaa5SPaul E. McKenney * the test is currently runnable. If there is no such flag, pass in NULL.
803b5daa8f3SPaul E. McKenney */
torture_init_begin(char * ttype,int v)80490127d60SPaul E. McKenney bool torture_init_begin(char *ttype, int v)
805b5daa8f3SPaul E. McKenney {
806b5daa8f3SPaul E. McKenney mutex_lock(&fullstop_mutex);
8075228084eSPaul E. McKenney if (torture_type != NULL) {
8084ac9de07SStephen Zhang pr_alert("%s: Refusing %s init: %s running.\n",
8094ac9de07SStephen Zhang __func__, ttype, torture_type);
8104ac9de07SStephen Zhang pr_alert("%s: One torture test at a time!\n", __func__);
8115228084eSPaul E. McKenney mutex_unlock(&fullstop_mutex);
8125228084eSPaul E. McKenney return false;
8135228084eSPaul E. McKenney }
814b5daa8f3SPaul E. McKenney torture_type = ttype;
815b5daa8f3SPaul E. McKenney verbose = v;
81636970bb9SPaul E. McKenney fullstop = FULLSTOP_DONTSTOP;
8175228084eSPaul E. McKenney return true;
818b5daa8f3SPaul E. McKenney }
819b5daa8f3SPaul E. McKenney EXPORT_SYMBOL_GPL(torture_init_begin);
820b5daa8f3SPaul E. McKenney
821b5daa8f3SPaul E. McKenney /*
822b5daa8f3SPaul E. McKenney * Tell the torture module that initialization is complete.
823b5daa8f3SPaul E. McKenney */
torture_init_end(void)8242b3f8ffeSPranith Kumar void torture_init_end(void)
825b5daa8f3SPaul E. McKenney {
826b5daa8f3SPaul E. McKenney mutex_unlock(&fullstop_mutex);
8274622b487SPaul E. McKenney register_reboot_notifier(&torture_shutdown_nb);
828b5daa8f3SPaul E. McKenney }
829b5daa8f3SPaul E. McKenney EXPORT_SYMBOL_GPL(torture_init_end);
830cc47ae08SPaul E. McKenney
831cc47ae08SPaul E. McKenney /*
832cc47ae08SPaul E. McKenney * Clean up torture module. Please note that this is -not- invoked via
833cc47ae08SPaul E. McKenney * the usual module_exit() mechanism, but rather by an explicit call from
834cc47ae08SPaul E. McKenney * the client torture module. Returns true if a race with system shutdown
835bfefc73aSPaul E. McKenney * is detected, otherwise, all kthreads started by functions in this file
836bfefc73aSPaul E. McKenney * will be shut down.
837cc47ae08SPaul E. McKenney *
838cc47ae08SPaul E. McKenney * This must be called before the caller starts shutting down its own
839cc47ae08SPaul E. McKenney * kthreads.
840d36a7a0dSDavidlohr Bueso *
841d36a7a0dSDavidlohr Bueso * Both torture_cleanup_begin() and torture_cleanup_end() must be paired,
842d36a7a0dSDavidlohr Bueso * in order to correctly perform the cleanup. They are separated because
843d36a7a0dSDavidlohr Bueso * threads can still need to reference the torture_type type, thus nullify
844d36a7a0dSDavidlohr Bueso * only after completing all other relevant calls.
845cc47ae08SPaul E. McKenney */
torture_cleanup_begin(void)846d36a7a0dSDavidlohr Bueso bool torture_cleanup_begin(void)
847cc47ae08SPaul E. McKenney {
848cc47ae08SPaul E. McKenney mutex_lock(&fullstop_mutex);
8497d0ae808SPaul E. McKenney if (READ_ONCE(fullstop) == FULLSTOP_SHUTDOWN) {
850cc47ae08SPaul E. McKenney pr_warn("Concurrent rmmod and shutdown illegal!\n");
851cc47ae08SPaul E. McKenney mutex_unlock(&fullstop_mutex);
852cc47ae08SPaul E. McKenney schedule_timeout_uninterruptible(10);
853cc47ae08SPaul E. McKenney return true;
854cc47ae08SPaul E. McKenney }
8557d0ae808SPaul E. McKenney WRITE_ONCE(fullstop, FULLSTOP_RMMOD);
856cc47ae08SPaul E. McKenney mutex_unlock(&fullstop_mutex);
857bfefc73aSPaul E. McKenney torture_shutdown_cleanup();
858cc47ae08SPaul E. McKenney torture_shuffle_cleanup();
859bfefc73aSPaul E. McKenney torture_stutter_cleanup();
860cc47ae08SPaul E. McKenney torture_onoff_cleanup();
861d36a7a0dSDavidlohr Bueso return false;
862d36a7a0dSDavidlohr Bueso }
863d36a7a0dSDavidlohr Bueso EXPORT_SYMBOL_GPL(torture_cleanup_begin);
864d36a7a0dSDavidlohr Bueso
torture_cleanup_end(void)865d36a7a0dSDavidlohr Bueso void torture_cleanup_end(void)
866d36a7a0dSDavidlohr Bueso {
8675228084eSPaul E. McKenney mutex_lock(&fullstop_mutex);
8685228084eSPaul E. McKenney torture_type = NULL;
8695228084eSPaul E. McKenney mutex_unlock(&fullstop_mutex);
870cc47ae08SPaul E. McKenney }
871d36a7a0dSDavidlohr Bueso EXPORT_SYMBOL_GPL(torture_cleanup_end);
87236970bb9SPaul E. McKenney
87336970bb9SPaul E. McKenney /*
87436970bb9SPaul E. McKenney * Is it time for the current torture test to stop?
87536970bb9SPaul E. McKenney */
torture_must_stop(void)87636970bb9SPaul E. McKenney bool torture_must_stop(void)
87736970bb9SPaul E. McKenney {
87836970bb9SPaul E. McKenney return torture_must_stop_irq() || kthread_should_stop();
87936970bb9SPaul E. McKenney }
88036970bb9SPaul E. McKenney EXPORT_SYMBOL_GPL(torture_must_stop);
88136970bb9SPaul E. McKenney
88236970bb9SPaul E. McKenney /*
88336970bb9SPaul E. McKenney * Is it time for the current torture test to stop? This is the irq-safe
88436970bb9SPaul E. McKenney * version, hence no check for kthread_should_stop().
88536970bb9SPaul E. McKenney */
torture_must_stop_irq(void)88636970bb9SPaul E. McKenney bool torture_must_stop_irq(void)
88736970bb9SPaul E. McKenney {
8887d0ae808SPaul E. McKenney return READ_ONCE(fullstop) != FULLSTOP_DONTSTOP;
88936970bb9SPaul E. McKenney }
89036970bb9SPaul E. McKenney EXPORT_SYMBOL_GPL(torture_must_stop_irq);
8917fafaac5SPaul E. McKenney
8927fafaac5SPaul E. McKenney /*
8937fafaac5SPaul E. McKenney * Each kthread must wait for kthread_should_stop() before returning from
8947fafaac5SPaul E. McKenney * its top-level function, otherwise segfaults ensue. This function
8957fafaac5SPaul E. McKenney * prints a "stopping" message and waits for kthread_should_stop(), and
8967fafaac5SPaul E. McKenney * should be called from all torture kthreads immediately prior to
8977fafaac5SPaul E. McKenney * returning.
8987fafaac5SPaul E. McKenney */
torture_kthread_stopping(char * title)8997fafaac5SPaul E. McKenney void torture_kthread_stopping(char *title)
9007fafaac5SPaul E. McKenney {
9010d6821d5SPaul E. McKenney char buf[128];
9020d6821d5SPaul E. McKenney
9032b4a7f20SPaul E. McKenney snprintf(buf, sizeof(buf), "%s is stopping", title);
9040d6821d5SPaul E. McKenney VERBOSE_TOROUT_STRING(buf);
9057fafaac5SPaul E. McKenney while (!kthread_should_stop()) {
9067fafaac5SPaul E. McKenney torture_shutdown_absorb(title);
907d52d3a2bSJoel Fernandes (Google) schedule_timeout_uninterruptible(HZ / 20);
9087fafaac5SPaul E. McKenney }
9097fafaac5SPaul E. McKenney }
9107fafaac5SPaul E. McKenney EXPORT_SYMBOL_GPL(torture_kthread_stopping);
91147cf29b9SPaul E. McKenney
91247cf29b9SPaul E. McKenney /*
91347cf29b9SPaul E. McKenney * Create a generic torture kthread that is immediately runnable. If you
91447cf29b9SPaul E. McKenney * need the kthread to be stopped so that you can do something to it before
91547cf29b9SPaul E. McKenney * it starts, you will need to open-code your own.
91647cf29b9SPaul E. McKenney */
_torture_create_kthread(int (* fn)(void * arg),void * arg,char * s,char * m,char * f,struct task_struct ** tp,void (* cbf)(struct task_struct * tp))91747cf29b9SPaul E. McKenney int _torture_create_kthread(int (*fn)(void *arg), void *arg, char *s, char *m,
91867d5404dSPaul E. McKenney char *f, struct task_struct **tp, void (*cbf)(struct task_struct *tp))
91947cf29b9SPaul E. McKenney {
92047cf29b9SPaul E. McKenney int ret = 0;
92147cf29b9SPaul E. McKenney
92247cf29b9SPaul E. McKenney VERBOSE_TOROUT_STRING(m);
9236b8646a9SPaul E. McKenney *tp = kthread_create(fn, arg, "%s", s);
92447cf29b9SPaul E. McKenney if (IS_ERR(*tp)) {
92547cf29b9SPaul E. McKenney ret = PTR_ERR(*tp);
92681faa4f6SLi Zhijian TOROUT_ERRSTRING(f);
92747cf29b9SPaul E. McKenney *tp = NULL;
9286b8646a9SPaul E. McKenney return ret;
92947cf29b9SPaul E. McKenney }
93067d5404dSPaul E. McKenney
93167d5404dSPaul E. McKenney if (cbf)
93267d5404dSPaul E. McKenney cbf(*tp);
93367d5404dSPaul E. McKenney
9346b8646a9SPaul E. McKenney wake_up_process(*tp); // Process is sleeping, so ordering provided.
93547cf29b9SPaul E. McKenney torture_shuffle_task_register(*tp);
93647cf29b9SPaul E. McKenney return ret;
93747cf29b9SPaul E. McKenney }
93847cf29b9SPaul E. McKenney EXPORT_SYMBOL_GPL(_torture_create_kthread);
9399c029b86SPaul E. McKenney
9409c029b86SPaul E. McKenney /*
9419c029b86SPaul E. McKenney * Stop a generic kthread, emitting a message.
9429c029b86SPaul E. McKenney */
_torture_stop_kthread(char * m,struct task_struct ** tp)9439c029b86SPaul E. McKenney void _torture_stop_kthread(char *m, struct task_struct **tp)
9449c029b86SPaul E. McKenney {
9459c029b86SPaul E. McKenney if (*tp == NULL)
9469c029b86SPaul E. McKenney return;
9479c029b86SPaul E. McKenney VERBOSE_TOROUT_STRING(m);
9489c029b86SPaul E. McKenney kthread_stop(*tp);
9499c029b86SPaul E. McKenney *tp = NULL;
9509c029b86SPaul E. McKenney }
9519c029b86SPaul E. McKenney EXPORT_SYMBOL_GPL(_torture_stop_kthread);
952