xref: /openbmc/linux/kernel/torture.c (revision 8171d3e0dafd37a9c833904e5a936f4154a1e95b)
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>
40dac95906SPaul E. McKenney #include "rcu/rcu.h"
4151b1130eSPaul E. McKenney 
4251b1130eSPaul E. McKenney MODULE_LICENSE("GPL");
438f8e76c0SPaul E. McKenney MODULE_AUTHOR("Paul E. McKenney <paulmck@linux.ibm.com>");
4451b1130eSPaul E. McKenney 
45*8171d3e0SPaul E. McKenney static bool disable_onoff_at_boot;
46*8171d3e0SPaul E. McKenney module_param(disable_onoff_at_boot, bool, 0444);
47*8171d3e0SPaul E. McKenney 
48b5daa8f3SPaul E. McKenney static char *torture_type;
4990127d60SPaul E. McKenney static int verbose;
50b5daa8f3SPaul E. McKenney 
5136970bb9SPaul E. McKenney /* Mediate rmmod and system shutdown.  Concurrent rmmod & shutdown illegal! */
5236970bb9SPaul E. McKenney #define FULLSTOP_DONTSTOP 0	/* Normal operation. */
5336970bb9SPaul E. McKenney #define FULLSTOP_SHUTDOWN 1	/* System shutdown with torture running. */
5436970bb9SPaul E. McKenney #define FULLSTOP_RMMOD    2	/* Normal rmmod of torture. */
5536970bb9SPaul E. McKenney static int fullstop = FULLSTOP_RMMOD;
564622b487SPaul E. McKenney static DEFINE_MUTEX(fullstop_mutex);
57f67a3356SPaul E. McKenney 
582e9e8081SPaul E. McKenney #ifdef CONFIG_HOTPLUG_CPU
592e9e8081SPaul E. McKenney 
602e9e8081SPaul E. McKenney /*
612e9e8081SPaul E. McKenney  * Variables for online-offline handling.  Only present if CPU hotplug
622e9e8081SPaul E. McKenney  * is enabled, otherwise does nothing.
632e9e8081SPaul E. McKenney  */
642e9e8081SPaul E. McKenney 
652e9e8081SPaul E. McKenney static struct task_struct *onoff_task;
662e9e8081SPaul E. McKenney static long onoff_holdoff;
672e9e8081SPaul E. McKenney static long onoff_interval;
683a6cb58fSPaul E. McKenney static torture_ofl_func *onoff_f;
692e9e8081SPaul E. McKenney static long n_offline_attempts;
702e9e8081SPaul E. McKenney static long n_offline_successes;
712e9e8081SPaul E. McKenney static unsigned long sum_offline;
722e9e8081SPaul E. McKenney static int min_offline = -1;
732e9e8081SPaul E. McKenney static int max_offline;
742e9e8081SPaul E. McKenney static long n_online_attempts;
752e9e8081SPaul E. McKenney static long n_online_successes;
762e9e8081SPaul E. McKenney static unsigned long sum_online;
772e9e8081SPaul E. McKenney static int min_online = -1;
782e9e8081SPaul E. McKenney static int max_online;
792e9e8081SPaul E. McKenney 
802e9e8081SPaul E. McKenney /*
81d95f5ba9SPaul E. McKenney  * Attempt to take a CPU offline.  Return false if the CPU is already
82d95f5ba9SPaul E. McKenney  * offline or if it is not subject to CPU-hotplug operations.  The
83d95f5ba9SPaul E. McKenney  * caller can detect other failures by looking at the statistics.
84d95f5ba9SPaul E. McKenney  */
85d95f5ba9SPaul E. McKenney bool torture_offline(int cpu, long *n_offl_attempts, long *n_offl_successes,
86d95f5ba9SPaul E. McKenney 		     unsigned long *sum_offl, int *min_offl, int *max_offl)
87d95f5ba9SPaul E. McKenney {
88d95f5ba9SPaul E. McKenney 	unsigned long delta;
89d95f5ba9SPaul E. McKenney 	int ret;
90a59ee765SPaul E. McKenney 	char *s;
91d95f5ba9SPaul E. McKenney 	unsigned long starttime;
92d95f5ba9SPaul E. McKenney 
93d95f5ba9SPaul E. McKenney 	if (!cpu_online(cpu) || !cpu_is_hotpluggable(cpu))
94d95f5ba9SPaul E. McKenney 		return false;
9524aca4aeSPaul E. McKenney 	if (num_online_cpus() <= 1)
9624aca4aeSPaul E. McKenney 		return false;  /* Can't offline the last CPU. */
97d95f5ba9SPaul E. McKenney 
9890127d60SPaul E. McKenney 	if (verbose > 1)
99d95f5ba9SPaul E. McKenney 		pr_alert("%s" TORTURE_FLAG
100d95f5ba9SPaul E. McKenney 			 "torture_onoff task: offlining %d\n",
101d95f5ba9SPaul E. McKenney 			 torture_type, cpu);
102d95f5ba9SPaul E. McKenney 	starttime = jiffies;
103d95f5ba9SPaul E. McKenney 	(*n_offl_attempts)++;
104d95f5ba9SPaul E. McKenney 	ret = cpu_down(cpu);
105d95f5ba9SPaul E. McKenney 	if (ret) {
106a59ee765SPaul E. McKenney 		s = "";
107a59ee765SPaul E. McKenney 		if (!rcu_inkernel_boot_has_ended() && ret == -EBUSY) {
108a59ee765SPaul E. McKenney 			// PCI probe frequently disables hotplug during boot.
109a59ee765SPaul E. McKenney 			(*n_offl_attempts)--;
110a59ee765SPaul E. McKenney 			s = " (-EBUSY forgiven during boot)";
111a59ee765SPaul E. McKenney 		}
112d95f5ba9SPaul E. McKenney 		if (verbose)
113d95f5ba9SPaul E. McKenney 			pr_alert("%s" TORTURE_FLAG
114a59ee765SPaul E. McKenney 				 "torture_onoff task: offline %d failed%s: errno %d\n",
115a59ee765SPaul E. McKenney 				 torture_type, cpu, s, ret);
116d95f5ba9SPaul E. McKenney 	} else {
11790127d60SPaul E. McKenney 		if (verbose > 1)
118d95f5ba9SPaul E. McKenney 			pr_alert("%s" TORTURE_FLAG
119d95f5ba9SPaul E. McKenney 				 "torture_onoff task: offlined %d\n",
120d95f5ba9SPaul E. McKenney 				 torture_type, cpu);
1213a6cb58fSPaul E. McKenney 		if (onoff_f)
1223a6cb58fSPaul E. McKenney 			onoff_f();
123d95f5ba9SPaul E. McKenney 		(*n_offl_successes)++;
124d95f5ba9SPaul E. McKenney 		delta = jiffies - starttime;
125a2b2df20SPaul E. McKenney 		*sum_offl += delta;
126d95f5ba9SPaul E. McKenney 		if (*min_offl < 0) {
127d95f5ba9SPaul E. McKenney 			*min_offl = delta;
128d95f5ba9SPaul E. McKenney 			*max_offl = delta;
129d95f5ba9SPaul E. McKenney 		}
130d95f5ba9SPaul E. McKenney 		if (*min_offl > delta)
131d95f5ba9SPaul E. McKenney 			*min_offl = delta;
132d95f5ba9SPaul E. McKenney 		if (*max_offl < delta)
133d95f5ba9SPaul E. McKenney 			*max_offl = delta;
134d95f5ba9SPaul E. McKenney 	}
135d95f5ba9SPaul E. McKenney 
136d95f5ba9SPaul E. McKenney 	return true;
137d95f5ba9SPaul E. McKenney }
138d95f5ba9SPaul E. McKenney EXPORT_SYMBOL_GPL(torture_offline);
139d95f5ba9SPaul E. McKenney 
140d95f5ba9SPaul E. McKenney /*
141d95f5ba9SPaul E. McKenney  * Attempt to bring a CPU online.  Return false if the CPU is already
142d95f5ba9SPaul E. McKenney  * online or if it is not subject to CPU-hotplug operations.  The
143d95f5ba9SPaul E. McKenney  * caller can detect other failures by looking at the statistics.
144d95f5ba9SPaul E. McKenney  */
145d95f5ba9SPaul E. McKenney bool torture_online(int cpu, long *n_onl_attempts, long *n_onl_successes,
146d95f5ba9SPaul E. McKenney 		    unsigned long *sum_onl, int *min_onl, int *max_onl)
147d95f5ba9SPaul E. McKenney {
148d95f5ba9SPaul E. McKenney 	unsigned long delta;
149d95f5ba9SPaul E. McKenney 	int ret;
150a59ee765SPaul E. McKenney 	char *s;
151d95f5ba9SPaul E. McKenney 	unsigned long starttime;
152d95f5ba9SPaul E. McKenney 
153d95f5ba9SPaul E. McKenney 	if (cpu_online(cpu) || !cpu_is_hotpluggable(cpu))
154d95f5ba9SPaul E. McKenney 		return false;
155d95f5ba9SPaul E. McKenney 
15690127d60SPaul E. McKenney 	if (verbose > 1)
157d95f5ba9SPaul E. McKenney 		pr_alert("%s" TORTURE_FLAG
158d95f5ba9SPaul E. McKenney 			 "torture_onoff task: onlining %d\n",
159d95f5ba9SPaul E. McKenney 			 torture_type, cpu);
160d95f5ba9SPaul E. McKenney 	starttime = jiffies;
161d95f5ba9SPaul E. McKenney 	(*n_onl_attempts)++;
162d95f5ba9SPaul E. McKenney 	ret = cpu_up(cpu);
163d95f5ba9SPaul E. McKenney 	if (ret) {
164a59ee765SPaul E. McKenney 		s = "";
165a59ee765SPaul E. McKenney 		if (!rcu_inkernel_boot_has_ended() && ret == -EBUSY) {
166a59ee765SPaul E. McKenney 			// PCI probe frequently disables hotplug during boot.
167a59ee765SPaul E. McKenney 			(*n_onl_attempts)--;
168a59ee765SPaul E. McKenney 			s = " (-EBUSY forgiven during boot)";
169a59ee765SPaul E. McKenney 		}
170d95f5ba9SPaul E. McKenney 		if (verbose)
171d95f5ba9SPaul E. McKenney 			pr_alert("%s" TORTURE_FLAG
172a59ee765SPaul E. McKenney 				 "torture_onoff task: online %d failed%s: errno %d\n",
173a59ee765SPaul E. McKenney 				 torture_type, cpu, s, ret);
174d95f5ba9SPaul E. McKenney 	} else {
17590127d60SPaul E. McKenney 		if (verbose > 1)
176d95f5ba9SPaul E. McKenney 			pr_alert("%s" TORTURE_FLAG
177d95f5ba9SPaul E. McKenney 				 "torture_onoff task: onlined %d\n",
178d95f5ba9SPaul E. McKenney 				 torture_type, cpu);
179d95f5ba9SPaul E. McKenney 		(*n_onl_successes)++;
180d95f5ba9SPaul E. McKenney 		delta = jiffies - starttime;
181d95f5ba9SPaul E. McKenney 		*sum_onl += delta;
182d95f5ba9SPaul E. McKenney 		if (*min_onl < 0) {
183d95f5ba9SPaul E. McKenney 			*min_onl = delta;
184d95f5ba9SPaul E. McKenney 			*max_onl = delta;
185d95f5ba9SPaul E. McKenney 		}
186d95f5ba9SPaul E. McKenney 		if (*min_onl > delta)
187d95f5ba9SPaul E. McKenney 			*min_onl = delta;
188d95f5ba9SPaul E. McKenney 		if (*max_onl < delta)
189d95f5ba9SPaul E. McKenney 			*max_onl = delta;
190d95f5ba9SPaul E. McKenney 	}
191d95f5ba9SPaul E. McKenney 
192d95f5ba9SPaul E. McKenney 	return true;
193d95f5ba9SPaul E. McKenney }
194d95f5ba9SPaul E. McKenney EXPORT_SYMBOL_GPL(torture_online);
195d95f5ba9SPaul E. McKenney 
196d95f5ba9SPaul E. McKenney /*
1972e9e8081SPaul E. McKenney  * Execute random CPU-hotplug operations at the interval specified
1982e9e8081SPaul E. McKenney  * by the onoff_interval.
1992e9e8081SPaul E. McKenney  */
2002e9e8081SPaul E. McKenney static int
2012e9e8081SPaul E. McKenney torture_onoff(void *arg)
2022e9e8081SPaul E. McKenney {
2032e9e8081SPaul E. McKenney 	int cpu;
2042e9e8081SPaul E. McKenney 	int maxcpu = -1;
2052e9e8081SPaul E. McKenney 	DEFINE_TORTURE_RANDOM(rand);
20628cf5952SPaul E. McKenney 	int ret;
2072e9e8081SPaul E. McKenney 
2082e9e8081SPaul E. McKenney 	VERBOSE_TOROUT_STRING("torture_onoff task started");
2092e9e8081SPaul E. McKenney 	for_each_online_cpu(cpu)
2102e9e8081SPaul E. McKenney 		maxcpu = cpu;
2112e9e8081SPaul E. McKenney 	WARN_ON(maxcpu < 0);
21228cf5952SPaul E. McKenney 	if (!IS_MODULE(CONFIG_TORTURE_TEST))
21328cf5952SPaul E. McKenney 		for_each_possible_cpu(cpu) {
21428cf5952SPaul E. McKenney 			if (cpu_online(cpu))
21528cf5952SPaul E. McKenney 				continue;
21628cf5952SPaul E. McKenney 			ret = cpu_up(cpu);
21728cf5952SPaul E. McKenney 			if (ret && verbose) {
21828cf5952SPaul E. McKenney 				pr_alert("%s" TORTURE_FLAG
21928cf5952SPaul E. McKenney 					 "%s: Initial online %d: errno %d\n",
22028cf5952SPaul E. McKenney 					 __func__, torture_type, cpu, ret);
22128cf5952SPaul E. McKenney 			}
22228cf5952SPaul E. McKenney 		}
223750db0f5SBoqun Feng 
224750db0f5SBoqun Feng 	if (maxcpu == 0) {
225750db0f5SBoqun Feng 		VERBOSE_TOROUT_STRING("Only one CPU, so CPU-hotplug testing is disabled");
226750db0f5SBoqun Feng 		goto stop;
227750db0f5SBoqun Feng 	}
228750db0f5SBoqun Feng 
2292e9e8081SPaul E. McKenney 	if (onoff_holdoff > 0) {
2302e9e8081SPaul E. McKenney 		VERBOSE_TOROUT_STRING("torture_onoff begin holdoff");
2312e9e8081SPaul E. McKenney 		schedule_timeout_interruptible(onoff_holdoff);
2322e9e8081SPaul E. McKenney 		VERBOSE_TOROUT_STRING("torture_onoff end holdoff");
2332e9e8081SPaul E. McKenney 	}
2342e9e8081SPaul E. McKenney 	while (!torture_must_stop()) {
235*8171d3e0SPaul E. McKenney 		if (disable_onoff_at_boot && !rcu_inkernel_boot_has_ended()) {
236*8171d3e0SPaul E. McKenney 			schedule_timeout_interruptible(HZ / 10);
237*8171d3e0SPaul E. McKenney 			continue;
238*8171d3e0SPaul E. McKenney 		}
2392e9e8081SPaul E. McKenney 		cpu = (torture_random(&rand) >> 4) % (maxcpu + 1);
240d95f5ba9SPaul E. McKenney 		if (!torture_offline(cpu,
241d95f5ba9SPaul E. McKenney 				     &n_offline_attempts, &n_offline_successes,
242d95f5ba9SPaul E. McKenney 				     &sum_offline, &min_offline, &max_offline))
243d95f5ba9SPaul E. McKenney 			torture_online(cpu,
244d95f5ba9SPaul E. McKenney 				       &n_online_attempts, &n_online_successes,
245d95f5ba9SPaul E. McKenney 				       &sum_online, &min_online, &max_online);
2462e9e8081SPaul E. McKenney 		schedule_timeout_interruptible(onoff_interval);
2472e9e8081SPaul E. McKenney 	}
248750db0f5SBoqun Feng 
249750db0f5SBoqun Feng stop:
2507fafaac5SPaul E. McKenney 	torture_kthread_stopping("torture_onoff");
2512e9e8081SPaul E. McKenney 	return 0;
2522e9e8081SPaul E. McKenney }
2532e9e8081SPaul E. McKenney 
2542e9e8081SPaul E. McKenney #endif /* #ifdef CONFIG_HOTPLUG_CPU */
2552e9e8081SPaul E. McKenney 
2562e9e8081SPaul E. McKenney /*
2572e9e8081SPaul E. McKenney  * Initiate online-offline handling.
2582e9e8081SPaul E. McKenney  */
2593a6cb58fSPaul E. McKenney int torture_onoff_init(long ooholdoff, long oointerval, torture_ofl_func *f)
2602e9e8081SPaul E. McKenney {
26147cf29b9SPaul E. McKenney #ifdef CONFIG_HOTPLUG_CPU
2622e9e8081SPaul E. McKenney 	onoff_holdoff = ooholdoff;
2632e9e8081SPaul E. McKenney 	onoff_interval = oointerval;
2643a6cb58fSPaul E. McKenney 	onoff_f = f;
2652e9e8081SPaul E. McKenney 	if (onoff_interval <= 0)
2662e9e8081SPaul E. McKenney 		return 0;
2672a7d9688SPierce Griffiths 	return torture_create_kthread(torture_onoff, NULL, onoff_task);
2682a7d9688SPierce Griffiths #else /* #ifdef CONFIG_HOTPLUG_CPU */
2692a7d9688SPierce Griffiths 	return 0;
2702a7d9688SPierce Griffiths #endif /* #else #ifdef CONFIG_HOTPLUG_CPU */
2712e9e8081SPaul E. McKenney }
2722e9e8081SPaul E. McKenney EXPORT_SYMBOL_GPL(torture_onoff_init);
2732e9e8081SPaul E. McKenney 
2742e9e8081SPaul E. McKenney /*
2752e9e8081SPaul E. McKenney  * Clean up after online/offline testing.
2762e9e8081SPaul E. McKenney  */
277cc47ae08SPaul E. McKenney static void torture_onoff_cleanup(void)
2782e9e8081SPaul E. McKenney {
2792e9e8081SPaul E. McKenney #ifdef CONFIG_HOTPLUG_CPU
2802e9e8081SPaul E. McKenney 	if (onoff_task == NULL)
2812e9e8081SPaul E. McKenney 		return;
2822e9e8081SPaul E. McKenney 	VERBOSE_TOROUT_STRING("Stopping torture_onoff task");
2832e9e8081SPaul E. McKenney 	kthread_stop(onoff_task);
2842e9e8081SPaul E. McKenney 	onoff_task = NULL;
2852e9e8081SPaul E. McKenney #endif /* #ifdef CONFIG_HOTPLUG_CPU */
2862e9e8081SPaul E. McKenney }
2872e9e8081SPaul E. McKenney 
2882e9e8081SPaul E. McKenney /*
2892e9e8081SPaul E. McKenney  * Print online/offline testing statistics.
2902e9e8081SPaul E. McKenney  */
291eea203feSJoe Perches void torture_onoff_stats(void)
2922e9e8081SPaul E. McKenney {
2932e9e8081SPaul E. McKenney #ifdef CONFIG_HOTPLUG_CPU
294eea203feSJoe Perches 	pr_cont("onoff: %ld/%ld:%ld/%ld %d,%d:%d,%d %lu:%lu (HZ=%d) ",
2952e9e8081SPaul E. McKenney 		n_online_successes, n_online_attempts,
2962e9e8081SPaul E. McKenney 		n_offline_successes, n_offline_attempts,
2972e9e8081SPaul E. McKenney 		min_online, max_online,
2982e9e8081SPaul E. McKenney 		min_offline, max_offline,
2992e9e8081SPaul E. McKenney 		sum_online, sum_offline, HZ);
3002e9e8081SPaul E. McKenney #endif /* #ifdef CONFIG_HOTPLUG_CPU */
3012e9e8081SPaul E. McKenney }
3022e9e8081SPaul E. McKenney EXPORT_SYMBOL_GPL(torture_onoff_stats);
3032e9e8081SPaul E. McKenney 
3042e9e8081SPaul E. McKenney /*
3052e9e8081SPaul E. McKenney  * Were all the online/offline operations successful?
3062e9e8081SPaul E. McKenney  */
3072e9e8081SPaul E. McKenney bool torture_onoff_failures(void)
3082e9e8081SPaul E. McKenney {
3092e9e8081SPaul E. McKenney #ifdef CONFIG_HOTPLUG_CPU
3102e9e8081SPaul E. McKenney 	return n_online_successes != n_online_attempts ||
3112e9e8081SPaul E. McKenney 	       n_offline_successes != n_offline_attempts;
3122e9e8081SPaul E. McKenney #else /* #ifdef CONFIG_HOTPLUG_CPU */
3132e9e8081SPaul E. McKenney 	return false;
3142e9e8081SPaul E. McKenney #endif /* #else #ifdef CONFIG_HOTPLUG_CPU */
3152e9e8081SPaul E. McKenney }
3162e9e8081SPaul E. McKenney EXPORT_SYMBOL_GPL(torture_onoff_failures);
3172e9e8081SPaul E. McKenney 
31851b1130eSPaul E. McKenney #define TORTURE_RANDOM_MULT	39916801  /* prime */
31951b1130eSPaul E. McKenney #define TORTURE_RANDOM_ADD	479001701 /* prime */
32051b1130eSPaul E. McKenney #define TORTURE_RANDOM_REFRESH	10000
32151b1130eSPaul E. McKenney 
32251b1130eSPaul E. McKenney /*
32351b1130eSPaul E. McKenney  * Crude but fast random-number generator.  Uses a linear congruential
32451b1130eSPaul E. McKenney  * generator, with occasional help from cpu_clock().
32551b1130eSPaul E. McKenney  */
32651b1130eSPaul E. McKenney unsigned long
32751b1130eSPaul E. McKenney torture_random(struct torture_random_state *trsp)
32851b1130eSPaul E. McKenney {
32951b1130eSPaul E. McKenney 	if (--trsp->trs_count < 0) {
33051b1130eSPaul E. McKenney 		trsp->trs_state += (unsigned long)local_clock();
33151b1130eSPaul E. McKenney 		trsp->trs_count = TORTURE_RANDOM_REFRESH;
33251b1130eSPaul E. McKenney 	}
33351b1130eSPaul E. McKenney 	trsp->trs_state = trsp->trs_state * TORTURE_RANDOM_MULT +
33451b1130eSPaul E. McKenney 		TORTURE_RANDOM_ADD;
33551b1130eSPaul E. McKenney 	return swahw32(trsp->trs_state);
33651b1130eSPaul E. McKenney }
33751b1130eSPaul E. McKenney EXPORT_SYMBOL_GPL(torture_random);
338f67a3356SPaul E. McKenney 
339f67a3356SPaul E. McKenney /*
3403808dc9fSPaul E. McKenney  * Variables for shuffling.  The idea is to ensure that each CPU stays
3413808dc9fSPaul E. McKenney  * idle for an extended period to test interactions with dyntick idle,
342b564d62eSMasahiro Yamada  * as well as interactions with any per-CPU variables.
3433808dc9fSPaul E. McKenney  */
3443808dc9fSPaul E. McKenney struct shuffle_task {
3453808dc9fSPaul E. McKenney 	struct list_head st_l;
3463808dc9fSPaul E. McKenney 	struct task_struct *st_t;
3473808dc9fSPaul E. McKenney };
3483808dc9fSPaul E. McKenney 
3493808dc9fSPaul E. McKenney static long shuffle_interval;	/* In jiffies. */
3503808dc9fSPaul E. McKenney static struct task_struct *shuffler_task;
3513808dc9fSPaul E. McKenney static cpumask_var_t shuffle_tmp_mask;
3523808dc9fSPaul E. McKenney static int shuffle_idle_cpu;	/* Force all torture tasks off this CPU */
3533808dc9fSPaul E. McKenney static struct list_head shuffle_task_list = LIST_HEAD_INIT(shuffle_task_list);
3543808dc9fSPaul E. McKenney static DEFINE_MUTEX(shuffle_task_mutex);
3553808dc9fSPaul E. McKenney 
3563808dc9fSPaul E. McKenney /*
3573808dc9fSPaul E. McKenney  * Register a task to be shuffled.  If there is no memory, just splat
3583808dc9fSPaul E. McKenney  * and don't bother registering.
3593808dc9fSPaul E. McKenney  */
3603808dc9fSPaul E. McKenney void torture_shuffle_task_register(struct task_struct *tp)
3613808dc9fSPaul E. McKenney {
3623808dc9fSPaul E. McKenney 	struct shuffle_task *stp;
3633808dc9fSPaul E. McKenney 
3643808dc9fSPaul E. McKenney 	if (WARN_ON_ONCE(tp == NULL))
3653808dc9fSPaul E. McKenney 		return;
3663808dc9fSPaul E. McKenney 	stp = kmalloc(sizeof(*stp), GFP_KERNEL);
3673808dc9fSPaul E. McKenney 	if (WARN_ON_ONCE(stp == NULL))
3683808dc9fSPaul E. McKenney 		return;
3693808dc9fSPaul E. McKenney 	stp->st_t = tp;
3703808dc9fSPaul E. McKenney 	mutex_lock(&shuffle_task_mutex);
3713808dc9fSPaul E. McKenney 	list_add(&stp->st_l, &shuffle_task_list);
3723808dc9fSPaul E. McKenney 	mutex_unlock(&shuffle_task_mutex);
3733808dc9fSPaul E. McKenney }
3743808dc9fSPaul E. McKenney EXPORT_SYMBOL_GPL(torture_shuffle_task_register);
3753808dc9fSPaul E. McKenney 
3763808dc9fSPaul E. McKenney /*
3773808dc9fSPaul E. McKenney  * Unregister all tasks, for example, at the end of the torture run.
3783808dc9fSPaul E. McKenney  */
3793808dc9fSPaul E. McKenney static void torture_shuffle_task_unregister_all(void)
3803808dc9fSPaul E. McKenney {
3813808dc9fSPaul E. McKenney 	struct shuffle_task *stp;
3823808dc9fSPaul E. McKenney 	struct shuffle_task *p;
3833808dc9fSPaul E. McKenney 
3843808dc9fSPaul E. McKenney 	mutex_lock(&shuffle_task_mutex);
3853808dc9fSPaul E. McKenney 	list_for_each_entry_safe(stp, p, &shuffle_task_list, st_l) {
3863808dc9fSPaul E. McKenney 		list_del(&stp->st_l);
3873808dc9fSPaul E. McKenney 		kfree(stp);
3883808dc9fSPaul E. McKenney 	}
3893808dc9fSPaul E. McKenney 	mutex_unlock(&shuffle_task_mutex);
3903808dc9fSPaul E. McKenney }
3913808dc9fSPaul E. McKenney 
3923808dc9fSPaul E. McKenney /* Shuffle tasks such that we allow shuffle_idle_cpu to become idle.
3933808dc9fSPaul E. McKenney  * A special case is when shuffle_idle_cpu = -1, in which case we allow
3943808dc9fSPaul E. McKenney  * the tasks to run on all CPUs.
3953808dc9fSPaul E. McKenney  */
3963808dc9fSPaul E. McKenney static void torture_shuffle_tasks(void)
3973808dc9fSPaul E. McKenney {
3983808dc9fSPaul E. McKenney 	struct shuffle_task *stp;
3993808dc9fSPaul E. McKenney 
4003808dc9fSPaul E. McKenney 	cpumask_setall(shuffle_tmp_mask);
4013808dc9fSPaul E. McKenney 	get_online_cpus();
4023808dc9fSPaul E. McKenney 
4033808dc9fSPaul E. McKenney 	/* No point in shuffling if there is only one online CPU (ex: UP) */
4043808dc9fSPaul E. McKenney 	if (num_online_cpus() == 1) {
4053808dc9fSPaul E. McKenney 		put_online_cpus();
4063808dc9fSPaul E. McKenney 		return;
4073808dc9fSPaul E. McKenney 	}
4083808dc9fSPaul E. McKenney 
4093808dc9fSPaul E. McKenney 	/* Advance to the next CPU.  Upon overflow, don't idle any CPUs. */
4103808dc9fSPaul E. McKenney 	shuffle_idle_cpu = cpumask_next(shuffle_idle_cpu, shuffle_tmp_mask);
4113808dc9fSPaul E. McKenney 	if (shuffle_idle_cpu >= nr_cpu_ids)
4123808dc9fSPaul E. McKenney 		shuffle_idle_cpu = -1;
4135ed63b19SIulia Manda 	else
4143808dc9fSPaul E. McKenney 		cpumask_clear_cpu(shuffle_idle_cpu, shuffle_tmp_mask);
4153808dc9fSPaul E. McKenney 
4163808dc9fSPaul E. McKenney 	mutex_lock(&shuffle_task_mutex);
4173808dc9fSPaul E. McKenney 	list_for_each_entry(stp, &shuffle_task_list, st_l)
4183808dc9fSPaul E. McKenney 		set_cpus_allowed_ptr(stp->st_t, shuffle_tmp_mask);
4193808dc9fSPaul E. McKenney 	mutex_unlock(&shuffle_task_mutex);
4203808dc9fSPaul E. McKenney 
4213808dc9fSPaul E. McKenney 	put_online_cpus();
4223808dc9fSPaul E. McKenney }
4233808dc9fSPaul E. McKenney 
4243808dc9fSPaul E. McKenney /* Shuffle tasks across CPUs, with the intent of allowing each CPU in the
4253808dc9fSPaul E. McKenney  * system to become idle at a time and cut off its timer ticks. This is meant
4263808dc9fSPaul E. McKenney  * to test the support for such tickless idle CPU in RCU.
4273808dc9fSPaul E. McKenney  */
4283808dc9fSPaul E. McKenney static int torture_shuffle(void *arg)
4293808dc9fSPaul E. McKenney {
4303808dc9fSPaul E. McKenney 	VERBOSE_TOROUT_STRING("torture_shuffle task started");
4313808dc9fSPaul E. McKenney 	do {
4323808dc9fSPaul E. McKenney 		schedule_timeout_interruptible(shuffle_interval);
4333808dc9fSPaul E. McKenney 		torture_shuffle_tasks();
4343808dc9fSPaul E. McKenney 		torture_shutdown_absorb("torture_shuffle");
4353808dc9fSPaul E. McKenney 	} while (!torture_must_stop());
4367fafaac5SPaul E. McKenney 	torture_kthread_stopping("torture_shuffle");
4373808dc9fSPaul E. McKenney 	return 0;
4383808dc9fSPaul E. McKenney }
4393808dc9fSPaul E. McKenney 
4403808dc9fSPaul E. McKenney /*
4413808dc9fSPaul E. McKenney  * Start the shuffler, with shuffint in jiffies.
4423808dc9fSPaul E. McKenney  */
4433808dc9fSPaul E. McKenney int torture_shuffle_init(long shuffint)
4443808dc9fSPaul E. McKenney {
4453808dc9fSPaul E. McKenney 	shuffle_interval = shuffint;
4463808dc9fSPaul E. McKenney 
4473808dc9fSPaul E. McKenney 	shuffle_idle_cpu = -1;
4483808dc9fSPaul E. McKenney 
4493808dc9fSPaul E. McKenney 	if (!alloc_cpumask_var(&shuffle_tmp_mask, GFP_KERNEL)) {
4503808dc9fSPaul E. McKenney 		VERBOSE_TOROUT_ERRSTRING("Failed to alloc mask");
4513808dc9fSPaul E. McKenney 		return -ENOMEM;
4523808dc9fSPaul E. McKenney 	}
4533808dc9fSPaul E. McKenney 
4543808dc9fSPaul E. McKenney 	/* Create the shuffler thread */
45547cf29b9SPaul E. McKenney 	return torture_create_kthread(torture_shuffle, NULL, shuffler_task);
4563808dc9fSPaul E. McKenney }
4573808dc9fSPaul E. McKenney EXPORT_SYMBOL_GPL(torture_shuffle_init);
4583808dc9fSPaul E. McKenney 
4593808dc9fSPaul E. McKenney /*
4603808dc9fSPaul E. McKenney  * Stop the shuffling.
4613808dc9fSPaul E. McKenney  */
462cc47ae08SPaul E. McKenney static void torture_shuffle_cleanup(void)
4633808dc9fSPaul E. McKenney {
4643808dc9fSPaul E. McKenney 	torture_shuffle_task_unregister_all();
4653808dc9fSPaul E. McKenney 	if (shuffler_task) {
4663808dc9fSPaul E. McKenney 		VERBOSE_TOROUT_STRING("Stopping torture_shuffle task");
4673808dc9fSPaul E. McKenney 		kthread_stop(shuffler_task);
4683808dc9fSPaul E. McKenney 		free_cpumask_var(shuffle_tmp_mask);
4693808dc9fSPaul E. McKenney 	}
4703808dc9fSPaul E. McKenney 	shuffler_task = NULL;
4713808dc9fSPaul E. McKenney }
4723808dc9fSPaul E. McKenney 
4733808dc9fSPaul E. McKenney /*
474e991dbc0SPaul E. McKenney  * Variables for auto-shutdown.  This allows "lights out" torture runs
475e991dbc0SPaul E. McKenney  * to be fully scripted.
476e991dbc0SPaul E. McKenney  */
477e991dbc0SPaul E. McKenney static struct task_struct *shutdown_task;
47831257c3cSPaul E. McKenney static ktime_t shutdown_time;		/* time to system shutdown. */
479e991dbc0SPaul E. McKenney static void (*torture_shutdown_hook)(void);
480e991dbc0SPaul E. McKenney 
481e991dbc0SPaul E. McKenney /*
482f67a3356SPaul E. McKenney  * Absorb kthreads into a kernel function that won't return, so that
483f67a3356SPaul E. McKenney  * they won't ever access module text or data again.
484f67a3356SPaul E. McKenney  */
485f67a3356SPaul E. McKenney void torture_shutdown_absorb(const char *title)
486f67a3356SPaul E. McKenney {
4877d0ae808SPaul E. McKenney 	while (READ_ONCE(fullstop) == FULLSTOP_SHUTDOWN) {
4884622b487SPaul E. McKenney 		pr_notice("torture thread %s parking due to system shutdown\n",
489f67a3356SPaul E. McKenney 			  title);
490f67a3356SPaul E. McKenney 		schedule_timeout_uninterruptible(MAX_SCHEDULE_TIMEOUT);
491f67a3356SPaul E. McKenney 	}
492f67a3356SPaul E. McKenney }
493f67a3356SPaul E. McKenney EXPORT_SYMBOL_GPL(torture_shutdown_absorb);
494b5daa8f3SPaul E. McKenney 
495b5daa8f3SPaul E. McKenney /*
496e991dbc0SPaul E. McKenney  * Cause the torture test to shutdown the system after the test has
497e991dbc0SPaul E. McKenney  * run for the time specified by the shutdown_secs parameter.
498e991dbc0SPaul E. McKenney  */
499e991dbc0SPaul E. McKenney static int torture_shutdown(void *arg)
500e991dbc0SPaul E. McKenney {
50131257c3cSPaul E. McKenney 	ktime_t ktime_snap;
502e991dbc0SPaul E. McKenney 
503e991dbc0SPaul E. McKenney 	VERBOSE_TOROUT_STRING("torture_shutdown task started");
50431257c3cSPaul E. McKenney 	ktime_snap = ktime_get();
50531257c3cSPaul E. McKenney 	while (ktime_before(ktime_snap, shutdown_time) &&
506e991dbc0SPaul E. McKenney 	       !torture_must_stop()) {
507e991dbc0SPaul E. McKenney 		if (verbose)
508e991dbc0SPaul E. McKenney 			pr_alert("%s" TORTURE_FLAG
50931257c3cSPaul E. McKenney 				 "torture_shutdown task: %llu ms remaining\n",
51031257c3cSPaul E. McKenney 				 torture_type,
51131257c3cSPaul E. McKenney 				 ktime_ms_delta(shutdown_time, ktime_snap));
51231257c3cSPaul E. McKenney 		set_current_state(TASK_INTERRUPTIBLE);
51331257c3cSPaul E. McKenney 		schedule_hrtimeout(&shutdown_time, HRTIMER_MODE_ABS);
51431257c3cSPaul E. McKenney 		ktime_snap = ktime_get();
515e991dbc0SPaul E. McKenney 	}
516e991dbc0SPaul E. McKenney 	if (torture_must_stop()) {
5177fafaac5SPaul E. McKenney 		torture_kthread_stopping("torture_shutdown");
518e991dbc0SPaul E. McKenney 		return 0;
519e991dbc0SPaul E. McKenney 	}
520e991dbc0SPaul E. McKenney 
521e991dbc0SPaul E. McKenney 	/* OK, shut down the system. */
522e991dbc0SPaul E. McKenney 
523e991dbc0SPaul E. McKenney 	VERBOSE_TOROUT_STRING("torture_shutdown task shutting down system");
524e991dbc0SPaul E. McKenney 	shutdown_task = NULL;	/* Avoid self-kill deadlock. */
525f881825aSPaul E. McKenney 	if (torture_shutdown_hook)
526f881825aSPaul E. McKenney 		torture_shutdown_hook();
527f881825aSPaul E. McKenney 	else
528f881825aSPaul E. McKenney 		VERBOSE_TOROUT_STRING("No torture_shutdown_hook(), skipping.");
529dac95906SPaul E. McKenney 	rcu_ftrace_dump(DUMP_ALL);
530e991dbc0SPaul E. McKenney 	kernel_power_off();	/* Shut down the system. */
531e991dbc0SPaul E. McKenney 	return 0;
532e991dbc0SPaul E. McKenney }
533e991dbc0SPaul E. McKenney 
534e991dbc0SPaul E. McKenney /*
535e991dbc0SPaul E. McKenney  * Start up the shutdown task.
536e991dbc0SPaul E. McKenney  */
537e991dbc0SPaul E. McKenney int torture_shutdown_init(int ssecs, void (*cleanup)(void))
538e991dbc0SPaul E. McKenney {
539e991dbc0SPaul E. McKenney 	torture_shutdown_hook = cleanup;
54031257c3cSPaul E. McKenney 	if (ssecs > 0) {
54131257c3cSPaul E. McKenney 		shutdown_time = ktime_add(ktime_get(), ktime_set(ssecs, 0));
5422a7d9688SPierce Griffiths 		return torture_create_kthread(torture_shutdown, NULL,
54347cf29b9SPaul E. McKenney 					     shutdown_task);
54447cf29b9SPaul E. McKenney 	}
5452a7d9688SPierce Griffiths 	return 0;
546e991dbc0SPaul E. McKenney }
547e991dbc0SPaul E. McKenney EXPORT_SYMBOL_GPL(torture_shutdown_init);
548e991dbc0SPaul E. McKenney 
549e991dbc0SPaul E. McKenney /*
5504622b487SPaul E. McKenney  * Detect and respond to a system shutdown.
5514622b487SPaul E. McKenney  */
5524622b487SPaul E. McKenney static int torture_shutdown_notify(struct notifier_block *unused1,
5534622b487SPaul E. McKenney 				   unsigned long unused2, void *unused3)
5544622b487SPaul E. McKenney {
5554622b487SPaul E. McKenney 	mutex_lock(&fullstop_mutex);
5567d0ae808SPaul E. McKenney 	if (READ_ONCE(fullstop) == FULLSTOP_DONTSTOP) {
557fac480efSPaul E. McKenney 		VERBOSE_TOROUT_STRING("Unscheduled system shutdown detected");
5587d0ae808SPaul E. McKenney 		WRITE_ONCE(fullstop, FULLSTOP_SHUTDOWN);
559fac480efSPaul E. McKenney 	} else {
5604622b487SPaul E. McKenney 		pr_warn("Concurrent rmmod and shutdown illegal!\n");
561fac480efSPaul E. McKenney 	}
5624622b487SPaul E. McKenney 	mutex_unlock(&fullstop_mutex);
5634622b487SPaul E. McKenney 	return NOTIFY_DONE;
5644622b487SPaul E. McKenney }
5654622b487SPaul E. McKenney 
5664622b487SPaul E. McKenney static struct notifier_block torture_shutdown_nb = {
5674622b487SPaul E. McKenney 	.notifier_call = torture_shutdown_notify,
5684622b487SPaul E. McKenney };
5694622b487SPaul E. McKenney 
5704622b487SPaul E. McKenney /*
571bfefc73aSPaul E. McKenney  * Shut down the shutdown task.  Say what???  Heh!  This can happen if
572bfefc73aSPaul E. McKenney  * the torture module gets an rmmod before the shutdown time arrives.  ;-)
573bfefc73aSPaul E. McKenney  */
574bfefc73aSPaul E. McKenney static void torture_shutdown_cleanup(void)
575bfefc73aSPaul E. McKenney {
576bfefc73aSPaul E. McKenney 	unregister_reboot_notifier(&torture_shutdown_nb);
577bfefc73aSPaul E. McKenney 	if (shutdown_task != NULL) {
578bfefc73aSPaul E. McKenney 		VERBOSE_TOROUT_STRING("Stopping torture_shutdown task");
579bfefc73aSPaul E. McKenney 		kthread_stop(shutdown_task);
580bfefc73aSPaul E. McKenney 	}
581bfefc73aSPaul E. McKenney 	shutdown_task = NULL;
582bfefc73aSPaul E. McKenney }
583bfefc73aSPaul E. McKenney 
584bfefc73aSPaul E. McKenney /*
585628edaa5SPaul E. McKenney  * Variables for stuttering, which means to periodically pause and
586628edaa5SPaul E. McKenney  * restart testing in order to catch bugs that appear when load is
587628edaa5SPaul E. McKenney  * suddenly applied to or removed from the system.
588628edaa5SPaul E. McKenney  */
589628edaa5SPaul E. McKenney static struct task_struct *stutter_task;
590628edaa5SPaul E. McKenney static int stutter_pause_test;
591628edaa5SPaul E. McKenney static int stutter;
592ff3bf92dSPaul E. McKenney static int stutter_gap;
593628edaa5SPaul E. McKenney 
594628edaa5SPaul E. McKenney /*
595628edaa5SPaul E. McKenney  * Block until the stutter interval ends.  This must be called periodically
596628edaa5SPaul E. McKenney  * by all running kthreads that need to be subject to stuttering.
597628edaa5SPaul E. McKenney  */
598474e59b4SPaul E. McKenney bool stutter_wait(const char *title)
599628edaa5SPaul E. McKenney {
6004ced3314SPaul E. McKenney 	int spt;
601e8516c64SPaul E. McKenney 	bool ret = false;
6024ced3314SPaul E. McKenney 
603cee43939SPaul E. McKenney 	cond_resched_tasks_rcu_qs();
6044ced3314SPaul E. McKenney 	spt = READ_ONCE(stutter_pause_test);
60529d39390SPaul E. McKenney 	for (; spt; spt = READ_ONCE(stutter_pause_test)) {
606e8516c64SPaul E. McKenney 		ret = true;
6074ced3314SPaul E. McKenney 		if (spt == 1) {
608628edaa5SPaul E. McKenney 			schedule_timeout_interruptible(1);
6094ced3314SPaul E. McKenney 		} else if (spt == 2) {
6107d0ae808SPaul E. McKenney 			while (READ_ONCE(stutter_pause_test))
611ab7d4505SPaul E. McKenney 				cond_resched();
6124ced3314SPaul E. McKenney 		} else {
613628edaa5SPaul E. McKenney 			schedule_timeout_interruptible(round_jiffies_relative(HZ));
6144ced3314SPaul E. McKenney 		}
615628edaa5SPaul E. McKenney 		torture_shutdown_absorb(title);
616628edaa5SPaul E. McKenney 	}
617e8516c64SPaul E. McKenney 	return ret;
618628edaa5SPaul E. McKenney }
619628edaa5SPaul E. McKenney EXPORT_SYMBOL_GPL(stutter_wait);
620628edaa5SPaul E. McKenney 
621628edaa5SPaul E. McKenney /*
622628edaa5SPaul E. McKenney  * Cause the torture test to "stutter", starting and stopping all
623628edaa5SPaul E. McKenney  * threads periodically.
624628edaa5SPaul E. McKenney  */
625628edaa5SPaul E. McKenney static int torture_stutter(void *arg)
626628edaa5SPaul E. McKenney {
627e8516c64SPaul E. McKenney 	int wtime;
628e8516c64SPaul E. McKenney 
629628edaa5SPaul E. McKenney 	VERBOSE_TOROUT_STRING("torture_stutter task started");
630628edaa5SPaul E. McKenney 	do {
6314ced3314SPaul E. McKenney 		if (!torture_must_stop() && stutter > 1) {
632e8516c64SPaul E. McKenney 			wtime = stutter;
633e8516c64SPaul E. McKenney 			if (stutter > HZ + 1) {
6344ced3314SPaul E. McKenney 				WRITE_ONCE(stutter_pause_test, 1);
635e8516c64SPaul E. McKenney 				wtime = stutter - HZ - 1;
636e8516c64SPaul E. McKenney 				schedule_timeout_interruptible(wtime);
637e8516c64SPaul E. McKenney 				wtime = HZ + 1;
638e8516c64SPaul E. McKenney 			}
6397d0ae808SPaul E. McKenney 			WRITE_ONCE(stutter_pause_test, 2);
640e8516c64SPaul E. McKenney 			schedule_timeout_interruptible(wtime);
641628edaa5SPaul E. McKenney 		}
6424ced3314SPaul E. McKenney 		WRITE_ONCE(stutter_pause_test, 0);
643628edaa5SPaul E. McKenney 		if (!torture_must_stop())
644ff3bf92dSPaul E. McKenney 			schedule_timeout_interruptible(stutter_gap);
645628edaa5SPaul E. McKenney 		torture_shutdown_absorb("torture_stutter");
646628edaa5SPaul E. McKenney 	} while (!torture_must_stop());
6477fafaac5SPaul E. McKenney 	torture_kthread_stopping("torture_stutter");
648628edaa5SPaul E. McKenney 	return 0;
649628edaa5SPaul E. McKenney }
650628edaa5SPaul E. McKenney 
651628edaa5SPaul E. McKenney /*
652628edaa5SPaul E. McKenney  * Initialize and kick off the torture_stutter kthread.
653628edaa5SPaul E. McKenney  */
654ff3bf92dSPaul E. McKenney int torture_stutter_init(const int s, const int sgap)
655628edaa5SPaul E. McKenney {
656628edaa5SPaul E. McKenney 	stutter = s;
657ff3bf92dSPaul E. McKenney 	stutter_gap = sgap;
6582a7d9688SPierce Griffiths 	return torture_create_kthread(torture_stutter, NULL, stutter_task);
659628edaa5SPaul E. McKenney }
660628edaa5SPaul E. McKenney EXPORT_SYMBOL_GPL(torture_stutter_init);
661628edaa5SPaul E. McKenney 
662628edaa5SPaul E. McKenney /*
663628edaa5SPaul E. McKenney  * Cleanup after the torture_stutter kthread.
664628edaa5SPaul E. McKenney  */
665bfefc73aSPaul E. McKenney static void torture_stutter_cleanup(void)
666628edaa5SPaul E. McKenney {
667628edaa5SPaul E. McKenney 	if (!stutter_task)
668628edaa5SPaul E. McKenney 		return;
669628edaa5SPaul E. McKenney 	VERBOSE_TOROUT_STRING("Stopping torture_stutter task");
670628edaa5SPaul E. McKenney 	kthread_stop(stutter_task);
671628edaa5SPaul E. McKenney 	stutter_task = NULL;
672628edaa5SPaul E. McKenney }
673628edaa5SPaul E. McKenney 
674628edaa5SPaul E. McKenney /*
675b5daa8f3SPaul E. McKenney  * Initialize torture module.  Please note that this is -not- invoked via
676b5daa8f3SPaul E. McKenney  * the usual module_init() mechanism, but rather by an explicit call from
677b5daa8f3SPaul E. McKenney  * the client torture module.  This call must be paired with a later
678b5daa8f3SPaul E. McKenney  * torture_init_end().
679628edaa5SPaul E. McKenney  *
680628edaa5SPaul E. McKenney  * The runnable parameter points to a flag that controls whether or not
681628edaa5SPaul E. McKenney  * the test is currently runnable.  If there is no such flag, pass in NULL.
682b5daa8f3SPaul E. McKenney  */
68390127d60SPaul E. McKenney bool torture_init_begin(char *ttype, int v)
684b5daa8f3SPaul E. McKenney {
685b5daa8f3SPaul E. McKenney 	mutex_lock(&fullstop_mutex);
6865228084eSPaul E. McKenney 	if (torture_type != NULL) {
6879eb5188aSPaul E. McKenney 		pr_alert("torture_init_begin: Refusing %s init: %s running.\n",
6885228084eSPaul E. McKenney 			 ttype, torture_type);
6899eb5188aSPaul E. McKenney 		pr_alert("torture_init_begin: One torture test at a time!\n");
6905228084eSPaul E. McKenney 		mutex_unlock(&fullstop_mutex);
6915228084eSPaul E. McKenney 		return false;
6925228084eSPaul E. McKenney 	}
693b5daa8f3SPaul E. McKenney 	torture_type = ttype;
694b5daa8f3SPaul E. McKenney 	verbose = v;
69536970bb9SPaul E. McKenney 	fullstop = FULLSTOP_DONTSTOP;
6965228084eSPaul E. McKenney 	return true;
697b5daa8f3SPaul E. McKenney }
698b5daa8f3SPaul E. McKenney EXPORT_SYMBOL_GPL(torture_init_begin);
699b5daa8f3SPaul E. McKenney 
700b5daa8f3SPaul E. McKenney /*
701b5daa8f3SPaul E. McKenney  * Tell the torture module that initialization is complete.
702b5daa8f3SPaul E. McKenney  */
7032b3f8ffeSPranith Kumar void torture_init_end(void)
704b5daa8f3SPaul E. McKenney {
705b5daa8f3SPaul E. McKenney 	mutex_unlock(&fullstop_mutex);
7064622b487SPaul E. McKenney 	register_reboot_notifier(&torture_shutdown_nb);
707b5daa8f3SPaul E. McKenney }
708b5daa8f3SPaul E. McKenney EXPORT_SYMBOL_GPL(torture_init_end);
709cc47ae08SPaul E. McKenney 
710cc47ae08SPaul E. McKenney /*
711cc47ae08SPaul E. McKenney  * Clean up torture module.  Please note that this is -not- invoked via
712cc47ae08SPaul E. McKenney  * the usual module_exit() mechanism, but rather by an explicit call from
713cc47ae08SPaul E. McKenney  * the client torture module.  Returns true if a race with system shutdown
714bfefc73aSPaul E. McKenney  * is detected, otherwise, all kthreads started by functions in this file
715bfefc73aSPaul E. McKenney  * will be shut down.
716cc47ae08SPaul E. McKenney  *
717cc47ae08SPaul E. McKenney  * This must be called before the caller starts shutting down its own
718cc47ae08SPaul E. McKenney  * kthreads.
719d36a7a0dSDavidlohr Bueso  *
720d36a7a0dSDavidlohr Bueso  * Both torture_cleanup_begin() and torture_cleanup_end() must be paired,
721d36a7a0dSDavidlohr Bueso  * in order to correctly perform the cleanup. They are separated because
722d36a7a0dSDavidlohr Bueso  * threads can still need to reference the torture_type type, thus nullify
723d36a7a0dSDavidlohr Bueso  * only after completing all other relevant calls.
724cc47ae08SPaul E. McKenney  */
725d36a7a0dSDavidlohr Bueso bool torture_cleanup_begin(void)
726cc47ae08SPaul E. McKenney {
727cc47ae08SPaul E. McKenney 	mutex_lock(&fullstop_mutex);
7287d0ae808SPaul E. McKenney 	if (READ_ONCE(fullstop) == FULLSTOP_SHUTDOWN) {
729cc47ae08SPaul E. McKenney 		pr_warn("Concurrent rmmod and shutdown illegal!\n");
730cc47ae08SPaul E. McKenney 		mutex_unlock(&fullstop_mutex);
731cc47ae08SPaul E. McKenney 		schedule_timeout_uninterruptible(10);
732cc47ae08SPaul E. McKenney 		return true;
733cc47ae08SPaul E. McKenney 	}
7347d0ae808SPaul E. McKenney 	WRITE_ONCE(fullstop, FULLSTOP_RMMOD);
735cc47ae08SPaul E. McKenney 	mutex_unlock(&fullstop_mutex);
736bfefc73aSPaul E. McKenney 	torture_shutdown_cleanup();
737cc47ae08SPaul E. McKenney 	torture_shuffle_cleanup();
738bfefc73aSPaul E. McKenney 	torture_stutter_cleanup();
739cc47ae08SPaul E. McKenney 	torture_onoff_cleanup();
740d36a7a0dSDavidlohr Bueso 	return false;
741d36a7a0dSDavidlohr Bueso }
742d36a7a0dSDavidlohr Bueso EXPORT_SYMBOL_GPL(torture_cleanup_begin);
743d36a7a0dSDavidlohr Bueso 
744d36a7a0dSDavidlohr Bueso void torture_cleanup_end(void)
745d36a7a0dSDavidlohr Bueso {
7465228084eSPaul E. McKenney 	mutex_lock(&fullstop_mutex);
7475228084eSPaul E. McKenney 	torture_type = NULL;
7485228084eSPaul E. McKenney 	mutex_unlock(&fullstop_mutex);
749cc47ae08SPaul E. McKenney }
750d36a7a0dSDavidlohr Bueso EXPORT_SYMBOL_GPL(torture_cleanup_end);
75136970bb9SPaul E. McKenney 
75236970bb9SPaul E. McKenney /*
75336970bb9SPaul E. McKenney  * Is it time for the current torture test to stop?
75436970bb9SPaul E. McKenney  */
75536970bb9SPaul E. McKenney bool torture_must_stop(void)
75636970bb9SPaul E. McKenney {
75736970bb9SPaul E. McKenney 	return torture_must_stop_irq() || kthread_should_stop();
75836970bb9SPaul E. McKenney }
75936970bb9SPaul E. McKenney EXPORT_SYMBOL_GPL(torture_must_stop);
76036970bb9SPaul E. McKenney 
76136970bb9SPaul E. McKenney /*
76236970bb9SPaul E. McKenney  * Is it time for the current torture test to stop?  This is the irq-safe
76336970bb9SPaul E. McKenney  * version, hence no check for kthread_should_stop().
76436970bb9SPaul E. McKenney  */
76536970bb9SPaul E. McKenney bool torture_must_stop_irq(void)
76636970bb9SPaul E. McKenney {
7677d0ae808SPaul E. McKenney 	return READ_ONCE(fullstop) != FULLSTOP_DONTSTOP;
76836970bb9SPaul E. McKenney }
76936970bb9SPaul E. McKenney EXPORT_SYMBOL_GPL(torture_must_stop_irq);
7707fafaac5SPaul E. McKenney 
7717fafaac5SPaul E. McKenney /*
7727fafaac5SPaul E. McKenney  * Each kthread must wait for kthread_should_stop() before returning from
7737fafaac5SPaul E. McKenney  * its top-level function, otherwise segfaults ensue.  This function
7747fafaac5SPaul E. McKenney  * prints a "stopping" message and waits for kthread_should_stop(), and
7757fafaac5SPaul E. McKenney  * should be called from all torture kthreads immediately prior to
7767fafaac5SPaul E. McKenney  * returning.
7777fafaac5SPaul E. McKenney  */
7787fafaac5SPaul E. McKenney void torture_kthread_stopping(char *title)
7797fafaac5SPaul E. McKenney {
7800d6821d5SPaul E. McKenney 	char buf[128];
7810d6821d5SPaul E. McKenney 
7820d6821d5SPaul E. McKenney 	snprintf(buf, sizeof(buf), "Stopping %s", title);
7830d6821d5SPaul E. McKenney 	VERBOSE_TOROUT_STRING(buf);
7847fafaac5SPaul E. McKenney 	while (!kthread_should_stop()) {
7857fafaac5SPaul E. McKenney 		torture_shutdown_absorb(title);
7867fafaac5SPaul E. McKenney 		schedule_timeout_uninterruptible(1);
7877fafaac5SPaul E. McKenney 	}
7887fafaac5SPaul E. McKenney }
7897fafaac5SPaul E. McKenney EXPORT_SYMBOL_GPL(torture_kthread_stopping);
79047cf29b9SPaul E. McKenney 
79147cf29b9SPaul E. McKenney /*
79247cf29b9SPaul E. McKenney  * Create a generic torture kthread that is immediately runnable.  If you
79347cf29b9SPaul E. McKenney  * need the kthread to be stopped so that you can do something to it before
79447cf29b9SPaul E. McKenney  * it starts, you will need to open-code your own.
79547cf29b9SPaul E. McKenney  */
79647cf29b9SPaul E. McKenney int _torture_create_kthread(int (*fn)(void *arg), void *arg, char *s, char *m,
79747cf29b9SPaul E. McKenney 			    char *f, struct task_struct **tp)
79847cf29b9SPaul E. McKenney {
79947cf29b9SPaul E. McKenney 	int ret = 0;
80047cf29b9SPaul E. McKenney 
80147cf29b9SPaul E. McKenney 	VERBOSE_TOROUT_STRING(m);
8026945915eSKees Cook 	*tp = kthread_run(fn, arg, "%s", s);
80347cf29b9SPaul E. McKenney 	if (IS_ERR(*tp)) {
80447cf29b9SPaul E. McKenney 		ret = PTR_ERR(*tp);
80547cf29b9SPaul E. McKenney 		VERBOSE_TOROUT_ERRSTRING(f);
80647cf29b9SPaul E. McKenney 		*tp = NULL;
80747cf29b9SPaul E. McKenney 	}
80847cf29b9SPaul E. McKenney 	torture_shuffle_task_register(*tp);
80947cf29b9SPaul E. McKenney 	return ret;
81047cf29b9SPaul E. McKenney }
81147cf29b9SPaul E. McKenney EXPORT_SYMBOL_GPL(_torture_create_kthread);
8129c029b86SPaul E. McKenney 
8139c029b86SPaul E. McKenney /*
8149c029b86SPaul E. McKenney  * Stop a generic kthread, emitting a message.
8159c029b86SPaul E. McKenney  */
8169c029b86SPaul E. McKenney void _torture_stop_kthread(char *m, struct task_struct **tp)
8179c029b86SPaul E. McKenney {
8189c029b86SPaul E. McKenney 	if (*tp == NULL)
8199c029b86SPaul E. McKenney 		return;
8209c029b86SPaul E. McKenney 	VERBOSE_TOROUT_STRING(m);
8219c029b86SPaul E. McKenney 	kthread_stop(*tp);
8229c029b86SPaul E. McKenney 	*tp = NULL;
8239c029b86SPaul E. McKenney }
8249c029b86SPaul E. McKenney EXPORT_SYMBOL_GPL(_torture_stop_kthread);
825