xref: /openbmc/linux/kernel/torture.c (revision 6945915e7f949e213c525d4eb244be87ec92f3d1)
151b1130eSPaul E. McKenney /*
251b1130eSPaul E. McKenney  * Common functions for in-kernel torture tests.
351b1130eSPaul E. McKenney  *
451b1130eSPaul E. McKenney  * This program is free software; you can redistribute it and/or modify
551b1130eSPaul E. McKenney  * it under the terms of the GNU General Public License as published by
651b1130eSPaul E. McKenney  * the Free Software Foundation; either version 2 of the License, or
751b1130eSPaul E. McKenney  * (at your option) any later version.
851b1130eSPaul E. McKenney  *
951b1130eSPaul E. McKenney  * This program is distributed in the hope that it will be useful,
1051b1130eSPaul E. McKenney  * but WITHOUT ANY WARRANTY; without even the implied warranty of
1151b1130eSPaul E. McKenney  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1251b1130eSPaul E. McKenney  * GNU General Public License for more details.
1351b1130eSPaul E. McKenney  *
1451b1130eSPaul E. McKenney  * You should have received a copy of the GNU General Public License
1551b1130eSPaul E. McKenney  * along with this program; if not, you can access it online at
1651b1130eSPaul E. McKenney  * http://www.gnu.org/licenses/gpl-2.0.html.
1751b1130eSPaul E. McKenney  *
1851b1130eSPaul E. McKenney  * Copyright (C) IBM Corporation, 2014
1951b1130eSPaul E. McKenney  *
2051b1130eSPaul E. McKenney  * Author: Paul E. McKenney <paulmck@us.ibm.com>
2151b1130eSPaul E. McKenney  *	Based on kernel/rcu/torture.c.
2251b1130eSPaul E. McKenney  */
2351b1130eSPaul E. McKenney #include <linux/types.h>
2451b1130eSPaul E. McKenney #include <linux/kernel.h>
2551b1130eSPaul E. McKenney #include <linux/init.h>
2651b1130eSPaul E. McKenney #include <linux/module.h>
2751b1130eSPaul E. McKenney #include <linux/kthread.h>
2851b1130eSPaul E. McKenney #include <linux/err.h>
2951b1130eSPaul E. McKenney #include <linux/spinlock.h>
3051b1130eSPaul E. McKenney #include <linux/smp.h>
3151b1130eSPaul E. McKenney #include <linux/interrupt.h>
3251b1130eSPaul E. McKenney #include <linux/sched.h>
3351b1130eSPaul E. McKenney #include <linux/atomic.h>
3451b1130eSPaul E. McKenney #include <linux/bitops.h>
3551b1130eSPaul E. McKenney #include <linux/completion.h>
3651b1130eSPaul E. McKenney #include <linux/moduleparam.h>
3751b1130eSPaul E. McKenney #include <linux/percpu.h>
3851b1130eSPaul E. McKenney #include <linux/notifier.h>
3951b1130eSPaul E. McKenney #include <linux/reboot.h>
4051b1130eSPaul E. McKenney #include <linux/freezer.h>
4151b1130eSPaul E. McKenney #include <linux/cpu.h>
4251b1130eSPaul E. McKenney #include <linux/delay.h>
4351b1130eSPaul E. McKenney #include <linux/stat.h>
4451b1130eSPaul E. McKenney #include <linux/slab.h>
4551b1130eSPaul E. McKenney #include <linux/trace_clock.h>
4651b1130eSPaul E. McKenney #include <asm/byteorder.h>
4751b1130eSPaul E. McKenney #include <linux/torture.h>
4851b1130eSPaul E. McKenney 
4951b1130eSPaul E. McKenney MODULE_LICENSE("GPL");
5051b1130eSPaul E. McKenney MODULE_AUTHOR("Paul E. McKenney <paulmck@us.ibm.com>");
5151b1130eSPaul E. McKenney 
52b5daa8f3SPaul E. McKenney static char *torture_type;
53b5daa8f3SPaul E. McKenney static bool verbose;
54b5daa8f3SPaul E. McKenney 
5536970bb9SPaul E. McKenney /* Mediate rmmod and system shutdown.  Concurrent rmmod & shutdown illegal! */
5636970bb9SPaul E. McKenney #define FULLSTOP_DONTSTOP 0	/* Normal operation. */
5736970bb9SPaul E. McKenney #define FULLSTOP_SHUTDOWN 1	/* System shutdown with torture running. */
5836970bb9SPaul E. McKenney #define FULLSTOP_RMMOD    2	/* Normal rmmod of torture. */
5936970bb9SPaul E. McKenney static int fullstop = FULLSTOP_RMMOD;
604622b487SPaul E. McKenney static DEFINE_MUTEX(fullstop_mutex);
61628edaa5SPaul E. McKenney static int *torture_runnable;
62f67a3356SPaul E. McKenney 
632e9e8081SPaul E. McKenney #ifdef CONFIG_HOTPLUG_CPU
642e9e8081SPaul E. McKenney 
652e9e8081SPaul E. McKenney /*
662e9e8081SPaul E. McKenney  * Variables for online-offline handling.  Only present if CPU hotplug
672e9e8081SPaul E. McKenney  * is enabled, otherwise does nothing.
682e9e8081SPaul E. McKenney  */
692e9e8081SPaul E. McKenney 
702e9e8081SPaul E. McKenney static struct task_struct *onoff_task;
712e9e8081SPaul E. McKenney static long onoff_holdoff;
722e9e8081SPaul E. McKenney static long onoff_interval;
732e9e8081SPaul E. McKenney static long n_offline_attempts;
742e9e8081SPaul E. McKenney static long n_offline_successes;
752e9e8081SPaul E. McKenney static unsigned long sum_offline;
762e9e8081SPaul E. McKenney static int min_offline = -1;
772e9e8081SPaul E. McKenney static int max_offline;
782e9e8081SPaul E. McKenney static long n_online_attempts;
792e9e8081SPaul E. McKenney static long n_online_successes;
802e9e8081SPaul E. McKenney static unsigned long sum_online;
812e9e8081SPaul E. McKenney static int min_online = -1;
822e9e8081SPaul E. McKenney static int max_online;
832e9e8081SPaul E. McKenney 
842e9e8081SPaul E. McKenney /*
852e9e8081SPaul E. McKenney  * Execute random CPU-hotplug operations at the interval specified
862e9e8081SPaul E. McKenney  * by the onoff_interval.
872e9e8081SPaul E. McKenney  */
882e9e8081SPaul E. McKenney static int
892e9e8081SPaul E. McKenney torture_onoff(void *arg)
902e9e8081SPaul E. McKenney {
912e9e8081SPaul E. McKenney 	int cpu;
922e9e8081SPaul E. McKenney 	unsigned long delta;
932e9e8081SPaul E. McKenney 	int maxcpu = -1;
942e9e8081SPaul E. McKenney 	DEFINE_TORTURE_RANDOM(rand);
952e9e8081SPaul E. McKenney 	int ret;
962e9e8081SPaul E. McKenney 	unsigned long starttime;
972e9e8081SPaul E. McKenney 
982e9e8081SPaul E. McKenney 	VERBOSE_TOROUT_STRING("torture_onoff task started");
992e9e8081SPaul E. McKenney 	for_each_online_cpu(cpu)
1002e9e8081SPaul E. McKenney 		maxcpu = cpu;
1012e9e8081SPaul E. McKenney 	WARN_ON(maxcpu < 0);
1022e9e8081SPaul E. McKenney 	if (onoff_holdoff > 0) {
1032e9e8081SPaul E. McKenney 		VERBOSE_TOROUT_STRING("torture_onoff begin holdoff");
1042e9e8081SPaul E. McKenney 		schedule_timeout_interruptible(onoff_holdoff);
1052e9e8081SPaul E. McKenney 		VERBOSE_TOROUT_STRING("torture_onoff end holdoff");
1062e9e8081SPaul E. McKenney 	}
1072e9e8081SPaul E. McKenney 	while (!torture_must_stop()) {
1082e9e8081SPaul E. McKenney 		cpu = (torture_random(&rand) >> 4) % (maxcpu + 1);
1092e9e8081SPaul E. McKenney 		if (cpu_online(cpu) && cpu_is_hotpluggable(cpu)) {
1102e9e8081SPaul E. McKenney 			if (verbose)
1112e9e8081SPaul E. McKenney 				pr_alert("%s" TORTURE_FLAG
1122e9e8081SPaul E. McKenney 					 "torture_onoff task: offlining %d\n",
1132e9e8081SPaul E. McKenney 					 torture_type, cpu);
1142e9e8081SPaul E. McKenney 			starttime = jiffies;
1152e9e8081SPaul E. McKenney 			n_offline_attempts++;
1162e9e8081SPaul E. McKenney 			ret = cpu_down(cpu);
1172e9e8081SPaul E. McKenney 			if (ret) {
1182e9e8081SPaul E. McKenney 				if (verbose)
1192e9e8081SPaul E. McKenney 					pr_alert("%s" TORTURE_FLAG
1202e9e8081SPaul E. McKenney 						 "torture_onoff task: offline %d failed: errno %d\n",
1212e9e8081SPaul E. McKenney 						 torture_type, cpu, ret);
1222e9e8081SPaul E. McKenney 			} else {
1232e9e8081SPaul E. McKenney 				if (verbose)
1242e9e8081SPaul E. McKenney 					pr_alert("%s" TORTURE_FLAG
1252e9e8081SPaul E. McKenney 						 "torture_onoff task: offlined %d\n",
1262e9e8081SPaul E. McKenney 						 torture_type, cpu);
1272e9e8081SPaul E. McKenney 				n_offline_successes++;
1282e9e8081SPaul E. McKenney 				delta = jiffies - starttime;
1292e9e8081SPaul E. McKenney 				sum_offline += delta;
1302e9e8081SPaul E. McKenney 				if (min_offline < 0) {
1312e9e8081SPaul E. McKenney 					min_offline = delta;
1322e9e8081SPaul E. McKenney 					max_offline = delta;
1332e9e8081SPaul E. McKenney 				}
1342e9e8081SPaul E. McKenney 				if (min_offline > delta)
1352e9e8081SPaul E. McKenney 					min_offline = delta;
1362e9e8081SPaul E. McKenney 				if (max_offline < delta)
1372e9e8081SPaul E. McKenney 					max_offline = delta;
1382e9e8081SPaul E. McKenney 			}
1392e9e8081SPaul E. McKenney 		} else if (cpu_is_hotpluggable(cpu)) {
1402e9e8081SPaul E. McKenney 			if (verbose)
1412e9e8081SPaul E. McKenney 				pr_alert("%s" TORTURE_FLAG
1422e9e8081SPaul E. McKenney 					 "torture_onoff task: onlining %d\n",
1432e9e8081SPaul E. McKenney 					 torture_type, cpu);
1442e9e8081SPaul E. McKenney 			starttime = jiffies;
1452e9e8081SPaul E. McKenney 			n_online_attempts++;
1462e9e8081SPaul E. McKenney 			ret = cpu_up(cpu);
1472e9e8081SPaul E. McKenney 			if (ret) {
1482e9e8081SPaul E. McKenney 				if (verbose)
1492e9e8081SPaul E. McKenney 					pr_alert("%s" TORTURE_FLAG
1502e9e8081SPaul E. McKenney 						 "torture_onoff task: online %d failed: errno %d\n",
1512e9e8081SPaul E. McKenney 						 torture_type, cpu, ret);
1522e9e8081SPaul E. McKenney 			} else {
1532e9e8081SPaul E. McKenney 				if (verbose)
1542e9e8081SPaul E. McKenney 					pr_alert("%s" TORTURE_FLAG
1552e9e8081SPaul E. McKenney 						 "torture_onoff task: onlined %d\n",
1562e9e8081SPaul E. McKenney 						 torture_type, cpu);
1572e9e8081SPaul E. McKenney 				n_online_successes++;
1582e9e8081SPaul E. McKenney 				delta = jiffies - starttime;
1592e9e8081SPaul E. McKenney 				sum_online += delta;
1602e9e8081SPaul E. McKenney 				if (min_online < 0) {
1612e9e8081SPaul E. McKenney 					min_online = delta;
1622e9e8081SPaul E. McKenney 					max_online = delta;
1632e9e8081SPaul E. McKenney 				}
1642e9e8081SPaul E. McKenney 				if (min_online > delta)
1652e9e8081SPaul E. McKenney 					min_online = delta;
1662e9e8081SPaul E. McKenney 				if (max_online < delta)
1672e9e8081SPaul E. McKenney 					max_online = delta;
1682e9e8081SPaul E. McKenney 			}
1692e9e8081SPaul E. McKenney 		}
1702e9e8081SPaul E. McKenney 		schedule_timeout_interruptible(onoff_interval);
1712e9e8081SPaul E. McKenney 	}
1727fafaac5SPaul E. McKenney 	torture_kthread_stopping("torture_onoff");
1732e9e8081SPaul E. McKenney 	return 0;
1742e9e8081SPaul E. McKenney }
1752e9e8081SPaul E. McKenney 
1762e9e8081SPaul E. McKenney #endif /* #ifdef CONFIG_HOTPLUG_CPU */
1772e9e8081SPaul E. McKenney 
1782e9e8081SPaul E. McKenney /*
1792e9e8081SPaul E. McKenney  * Initiate online-offline handling.
1802e9e8081SPaul E. McKenney  */
1812e9e8081SPaul E. McKenney int torture_onoff_init(long ooholdoff, long oointerval)
1822e9e8081SPaul E. McKenney {
18347cf29b9SPaul E. McKenney 	int ret = 0;
1842e9e8081SPaul E. McKenney 
18547cf29b9SPaul E. McKenney #ifdef CONFIG_HOTPLUG_CPU
1862e9e8081SPaul E. McKenney 	onoff_holdoff = ooholdoff;
1872e9e8081SPaul E. McKenney 	onoff_interval = oointerval;
1882e9e8081SPaul E. McKenney 	if (onoff_interval <= 0)
1892e9e8081SPaul E. McKenney 		return 0;
19047cf29b9SPaul E. McKenney 	ret = torture_create_kthread(torture_onoff, NULL, onoff_task);
1912e9e8081SPaul E. McKenney #endif /* #ifdef CONFIG_HOTPLUG_CPU */
19247cf29b9SPaul E. McKenney 	return ret;
1932e9e8081SPaul E. McKenney }
1942e9e8081SPaul E. McKenney EXPORT_SYMBOL_GPL(torture_onoff_init);
1952e9e8081SPaul E. McKenney 
1962e9e8081SPaul E. McKenney /*
1972e9e8081SPaul E. McKenney  * Clean up after online/offline testing.
1982e9e8081SPaul E. McKenney  */
199cc47ae08SPaul E. McKenney static void torture_onoff_cleanup(void)
2002e9e8081SPaul E. McKenney {
2012e9e8081SPaul E. McKenney #ifdef CONFIG_HOTPLUG_CPU
2022e9e8081SPaul E. McKenney 	if (onoff_task == NULL)
2032e9e8081SPaul E. McKenney 		return;
2042e9e8081SPaul E. McKenney 	VERBOSE_TOROUT_STRING("Stopping torture_onoff task");
2052e9e8081SPaul E. McKenney 	kthread_stop(onoff_task);
2062e9e8081SPaul E. McKenney 	onoff_task = NULL;
2072e9e8081SPaul E. McKenney #endif /* #ifdef CONFIG_HOTPLUG_CPU */
2082e9e8081SPaul E. McKenney }
2092e9e8081SPaul E. McKenney EXPORT_SYMBOL_GPL(torture_onoff_cleanup);
2102e9e8081SPaul E. McKenney 
2112e9e8081SPaul E. McKenney /*
2122e9e8081SPaul E. McKenney  * Print online/offline testing statistics.
2132e9e8081SPaul E. McKenney  */
2142e9e8081SPaul E. McKenney char *torture_onoff_stats(char *page)
2152e9e8081SPaul E. McKenney {
2162e9e8081SPaul E. McKenney #ifdef CONFIG_HOTPLUG_CPU
2172e9e8081SPaul E. McKenney 	page += sprintf(page,
2182e9e8081SPaul E. McKenney 		       "onoff: %ld/%ld:%ld/%ld %d,%d:%d,%d %lu:%lu (HZ=%d) ",
2192e9e8081SPaul E. McKenney 		       n_online_successes, n_online_attempts,
2202e9e8081SPaul E. McKenney 		       n_offline_successes, n_offline_attempts,
2212e9e8081SPaul E. McKenney 		       min_online, max_online,
2222e9e8081SPaul E. McKenney 		       min_offline, max_offline,
2232e9e8081SPaul E. McKenney 		       sum_online, sum_offline, HZ);
2242e9e8081SPaul E. McKenney #endif /* #ifdef CONFIG_HOTPLUG_CPU */
2252e9e8081SPaul E. McKenney 	return page;
2262e9e8081SPaul E. McKenney }
2272e9e8081SPaul E. McKenney EXPORT_SYMBOL_GPL(torture_onoff_stats);
2282e9e8081SPaul E. McKenney 
2292e9e8081SPaul E. McKenney /*
2302e9e8081SPaul E. McKenney  * Were all the online/offline operations successful?
2312e9e8081SPaul E. McKenney  */
2322e9e8081SPaul E. McKenney bool torture_onoff_failures(void)
2332e9e8081SPaul E. McKenney {
2342e9e8081SPaul E. McKenney #ifdef CONFIG_HOTPLUG_CPU
2352e9e8081SPaul E. McKenney 	return n_online_successes != n_online_attempts ||
2362e9e8081SPaul E. McKenney 	       n_offline_successes != n_offline_attempts;
2372e9e8081SPaul E. McKenney #else /* #ifdef CONFIG_HOTPLUG_CPU */
2382e9e8081SPaul E. McKenney 	return false;
2392e9e8081SPaul E. McKenney #endif /* #else #ifdef CONFIG_HOTPLUG_CPU */
2402e9e8081SPaul E. McKenney }
2412e9e8081SPaul E. McKenney EXPORT_SYMBOL_GPL(torture_onoff_failures);
2422e9e8081SPaul E. McKenney 
24351b1130eSPaul E. McKenney #define TORTURE_RANDOM_MULT	39916801  /* prime */
24451b1130eSPaul E. McKenney #define TORTURE_RANDOM_ADD	479001701 /* prime */
24551b1130eSPaul E. McKenney #define TORTURE_RANDOM_REFRESH	10000
24651b1130eSPaul E. McKenney 
24751b1130eSPaul E. McKenney /*
24851b1130eSPaul E. McKenney  * Crude but fast random-number generator.  Uses a linear congruential
24951b1130eSPaul E. McKenney  * generator, with occasional help from cpu_clock().
25051b1130eSPaul E. McKenney  */
25151b1130eSPaul E. McKenney unsigned long
25251b1130eSPaul E. McKenney torture_random(struct torture_random_state *trsp)
25351b1130eSPaul E. McKenney {
25451b1130eSPaul E. McKenney 	if (--trsp->trs_count < 0) {
25551b1130eSPaul E. McKenney 		trsp->trs_state += (unsigned long)local_clock();
25651b1130eSPaul E. McKenney 		trsp->trs_count = TORTURE_RANDOM_REFRESH;
25751b1130eSPaul E. McKenney 	}
25851b1130eSPaul E. McKenney 	trsp->trs_state = trsp->trs_state * TORTURE_RANDOM_MULT +
25951b1130eSPaul E. McKenney 		TORTURE_RANDOM_ADD;
26051b1130eSPaul E. McKenney 	return swahw32(trsp->trs_state);
26151b1130eSPaul E. McKenney }
26251b1130eSPaul E. McKenney EXPORT_SYMBOL_GPL(torture_random);
263f67a3356SPaul E. McKenney 
264f67a3356SPaul E. McKenney /*
2653808dc9fSPaul E. McKenney  * Variables for shuffling.  The idea is to ensure that each CPU stays
2663808dc9fSPaul E. McKenney  * idle for an extended period to test interactions with dyntick idle,
2673808dc9fSPaul E. McKenney  * as well as interactions with any per-CPU varibles.
2683808dc9fSPaul E. McKenney  */
2693808dc9fSPaul E. McKenney struct shuffle_task {
2703808dc9fSPaul E. McKenney 	struct list_head st_l;
2713808dc9fSPaul E. McKenney 	struct task_struct *st_t;
2723808dc9fSPaul E. McKenney };
2733808dc9fSPaul E. McKenney 
2743808dc9fSPaul E. McKenney static long shuffle_interval;	/* In jiffies. */
2753808dc9fSPaul E. McKenney static struct task_struct *shuffler_task;
2763808dc9fSPaul E. McKenney static cpumask_var_t shuffle_tmp_mask;
2773808dc9fSPaul E. McKenney static int shuffle_idle_cpu;	/* Force all torture tasks off this CPU */
2783808dc9fSPaul E. McKenney static struct list_head shuffle_task_list = LIST_HEAD_INIT(shuffle_task_list);
2793808dc9fSPaul E. McKenney static DEFINE_MUTEX(shuffle_task_mutex);
2803808dc9fSPaul E. McKenney 
2813808dc9fSPaul E. McKenney /*
2823808dc9fSPaul E. McKenney  * Register a task to be shuffled.  If there is no memory, just splat
2833808dc9fSPaul E. McKenney  * and don't bother registering.
2843808dc9fSPaul E. McKenney  */
2853808dc9fSPaul E. McKenney void torture_shuffle_task_register(struct task_struct *tp)
2863808dc9fSPaul E. McKenney {
2873808dc9fSPaul E. McKenney 	struct shuffle_task *stp;
2883808dc9fSPaul E. McKenney 
2893808dc9fSPaul E. McKenney 	if (WARN_ON_ONCE(tp == NULL))
2903808dc9fSPaul E. McKenney 		return;
2913808dc9fSPaul E. McKenney 	stp = kmalloc(sizeof(*stp), GFP_KERNEL);
2923808dc9fSPaul E. McKenney 	if (WARN_ON_ONCE(stp == NULL))
2933808dc9fSPaul E. McKenney 		return;
2943808dc9fSPaul E. McKenney 	stp->st_t = tp;
2953808dc9fSPaul E. McKenney 	mutex_lock(&shuffle_task_mutex);
2963808dc9fSPaul E. McKenney 	list_add(&stp->st_l, &shuffle_task_list);
2973808dc9fSPaul E. McKenney 	mutex_unlock(&shuffle_task_mutex);
2983808dc9fSPaul E. McKenney }
2993808dc9fSPaul E. McKenney EXPORT_SYMBOL_GPL(torture_shuffle_task_register);
3003808dc9fSPaul E. McKenney 
3013808dc9fSPaul E. McKenney /*
3023808dc9fSPaul E. McKenney  * Unregister all tasks, for example, at the end of the torture run.
3033808dc9fSPaul E. McKenney  */
3043808dc9fSPaul E. McKenney static void torture_shuffle_task_unregister_all(void)
3053808dc9fSPaul E. McKenney {
3063808dc9fSPaul E. McKenney 	struct shuffle_task *stp;
3073808dc9fSPaul E. McKenney 	struct shuffle_task *p;
3083808dc9fSPaul E. McKenney 
3093808dc9fSPaul E. McKenney 	mutex_lock(&shuffle_task_mutex);
3103808dc9fSPaul E. McKenney 	list_for_each_entry_safe(stp, p, &shuffle_task_list, st_l) {
3113808dc9fSPaul E. McKenney 		list_del(&stp->st_l);
3123808dc9fSPaul E. McKenney 		kfree(stp);
3133808dc9fSPaul E. McKenney 	}
3143808dc9fSPaul E. McKenney 	mutex_unlock(&shuffle_task_mutex);
3153808dc9fSPaul E. McKenney }
3163808dc9fSPaul E. McKenney 
3173808dc9fSPaul E. McKenney /* Shuffle tasks such that we allow shuffle_idle_cpu to become idle.
3183808dc9fSPaul E. McKenney  * A special case is when shuffle_idle_cpu = -1, in which case we allow
3193808dc9fSPaul E. McKenney  * the tasks to run on all CPUs.
3203808dc9fSPaul E. McKenney  */
3213808dc9fSPaul E. McKenney static void torture_shuffle_tasks(void)
3223808dc9fSPaul E. McKenney {
3233808dc9fSPaul E. McKenney 	struct shuffle_task *stp;
3243808dc9fSPaul E. McKenney 
3253808dc9fSPaul E. McKenney 	cpumask_setall(shuffle_tmp_mask);
3263808dc9fSPaul E. McKenney 	get_online_cpus();
3273808dc9fSPaul E. McKenney 
3283808dc9fSPaul E. McKenney 	/* No point in shuffling if there is only one online CPU (ex: UP) */
3293808dc9fSPaul E. McKenney 	if (num_online_cpus() == 1) {
3303808dc9fSPaul E. McKenney 		put_online_cpus();
3313808dc9fSPaul E. McKenney 		return;
3323808dc9fSPaul E. McKenney 	}
3333808dc9fSPaul E. McKenney 
3343808dc9fSPaul E. McKenney 	/* Advance to the next CPU.  Upon overflow, don't idle any CPUs. */
3353808dc9fSPaul E. McKenney 	shuffle_idle_cpu = cpumask_next(shuffle_idle_cpu, shuffle_tmp_mask);
3363808dc9fSPaul E. McKenney 	if (shuffle_idle_cpu >= nr_cpu_ids)
3373808dc9fSPaul E. McKenney 		shuffle_idle_cpu = -1;
3385ed63b19SIulia Manda 	else
3393808dc9fSPaul E. McKenney 		cpumask_clear_cpu(shuffle_idle_cpu, shuffle_tmp_mask);
3403808dc9fSPaul E. McKenney 
3413808dc9fSPaul E. McKenney 	mutex_lock(&shuffle_task_mutex);
3423808dc9fSPaul E. McKenney 	list_for_each_entry(stp, &shuffle_task_list, st_l)
3433808dc9fSPaul E. McKenney 		set_cpus_allowed_ptr(stp->st_t, shuffle_tmp_mask);
3443808dc9fSPaul E. McKenney 	mutex_unlock(&shuffle_task_mutex);
3453808dc9fSPaul E. McKenney 
3463808dc9fSPaul E. McKenney 	put_online_cpus();
3473808dc9fSPaul E. McKenney }
3483808dc9fSPaul E. McKenney 
3493808dc9fSPaul E. McKenney /* Shuffle tasks across CPUs, with the intent of allowing each CPU in the
3503808dc9fSPaul E. McKenney  * system to become idle at a time and cut off its timer ticks. This is meant
3513808dc9fSPaul E. McKenney  * to test the support for such tickless idle CPU in RCU.
3523808dc9fSPaul E. McKenney  */
3533808dc9fSPaul E. McKenney static int torture_shuffle(void *arg)
3543808dc9fSPaul E. McKenney {
3553808dc9fSPaul E. McKenney 	VERBOSE_TOROUT_STRING("torture_shuffle task started");
3563808dc9fSPaul E. McKenney 	do {
3573808dc9fSPaul E. McKenney 		schedule_timeout_interruptible(shuffle_interval);
3583808dc9fSPaul E. McKenney 		torture_shuffle_tasks();
3593808dc9fSPaul E. McKenney 		torture_shutdown_absorb("torture_shuffle");
3603808dc9fSPaul E. McKenney 	} while (!torture_must_stop());
3617fafaac5SPaul E. McKenney 	torture_kthread_stopping("torture_shuffle");
3623808dc9fSPaul E. McKenney 	return 0;
3633808dc9fSPaul E. McKenney }
3643808dc9fSPaul E. McKenney 
3653808dc9fSPaul E. McKenney /*
3663808dc9fSPaul E. McKenney  * Start the shuffler, with shuffint in jiffies.
3673808dc9fSPaul E. McKenney  */
3683808dc9fSPaul E. McKenney int torture_shuffle_init(long shuffint)
3693808dc9fSPaul E. McKenney {
3703808dc9fSPaul E. McKenney 	shuffle_interval = shuffint;
3713808dc9fSPaul E. McKenney 
3723808dc9fSPaul E. McKenney 	shuffle_idle_cpu = -1;
3733808dc9fSPaul E. McKenney 
3743808dc9fSPaul E. McKenney 	if (!alloc_cpumask_var(&shuffle_tmp_mask, GFP_KERNEL)) {
3753808dc9fSPaul E. McKenney 		VERBOSE_TOROUT_ERRSTRING("Failed to alloc mask");
3763808dc9fSPaul E. McKenney 		return -ENOMEM;
3773808dc9fSPaul E. McKenney 	}
3783808dc9fSPaul E. McKenney 
3793808dc9fSPaul E. McKenney 	/* Create the shuffler thread */
38047cf29b9SPaul E. McKenney 	return torture_create_kthread(torture_shuffle, NULL, shuffler_task);
3813808dc9fSPaul E. McKenney }
3823808dc9fSPaul E. McKenney EXPORT_SYMBOL_GPL(torture_shuffle_init);
3833808dc9fSPaul E. McKenney 
3843808dc9fSPaul E. McKenney /*
3853808dc9fSPaul E. McKenney  * Stop the shuffling.
3863808dc9fSPaul E. McKenney  */
387cc47ae08SPaul E. McKenney static void torture_shuffle_cleanup(void)
3883808dc9fSPaul E. McKenney {
3893808dc9fSPaul E. McKenney 	torture_shuffle_task_unregister_all();
3903808dc9fSPaul E. McKenney 	if (shuffler_task) {
3913808dc9fSPaul E. McKenney 		VERBOSE_TOROUT_STRING("Stopping torture_shuffle task");
3923808dc9fSPaul E. McKenney 		kthread_stop(shuffler_task);
3933808dc9fSPaul E. McKenney 		free_cpumask_var(shuffle_tmp_mask);
3943808dc9fSPaul E. McKenney 	}
3953808dc9fSPaul E. McKenney 	shuffler_task = NULL;
3963808dc9fSPaul E. McKenney }
3973808dc9fSPaul E. McKenney EXPORT_SYMBOL_GPL(torture_shuffle_cleanup);
3983808dc9fSPaul E. McKenney 
3993808dc9fSPaul E. McKenney /*
400e991dbc0SPaul E. McKenney  * Variables for auto-shutdown.  This allows "lights out" torture runs
401e991dbc0SPaul E. McKenney  * to be fully scripted.
402e991dbc0SPaul E. McKenney  */
403e991dbc0SPaul E. McKenney static int shutdown_secs;		/* desired test duration in seconds. */
404e991dbc0SPaul E. McKenney static struct task_struct *shutdown_task;
405e991dbc0SPaul E. McKenney static unsigned long shutdown_time;	/* jiffies to system shutdown. */
406e991dbc0SPaul E. McKenney static void (*torture_shutdown_hook)(void);
407e991dbc0SPaul E. McKenney 
408e991dbc0SPaul E. McKenney /*
409f67a3356SPaul E. McKenney  * Absorb kthreads into a kernel function that won't return, so that
410f67a3356SPaul E. McKenney  * they won't ever access module text or data again.
411f67a3356SPaul E. McKenney  */
412f67a3356SPaul E. McKenney void torture_shutdown_absorb(const char *title)
413f67a3356SPaul E. McKenney {
414f67a3356SPaul E. McKenney 	while (ACCESS_ONCE(fullstop) == FULLSTOP_SHUTDOWN) {
4154622b487SPaul E. McKenney 		pr_notice("torture thread %s parking due to system shutdown\n",
416f67a3356SPaul E. McKenney 			  title);
417f67a3356SPaul E. McKenney 		schedule_timeout_uninterruptible(MAX_SCHEDULE_TIMEOUT);
418f67a3356SPaul E. McKenney 	}
419f67a3356SPaul E. McKenney }
420f67a3356SPaul E. McKenney EXPORT_SYMBOL_GPL(torture_shutdown_absorb);
421b5daa8f3SPaul E. McKenney 
422b5daa8f3SPaul E. McKenney /*
423e991dbc0SPaul E. McKenney  * Cause the torture test to shutdown the system after the test has
424e991dbc0SPaul E. McKenney  * run for the time specified by the shutdown_secs parameter.
425e991dbc0SPaul E. McKenney  */
426e991dbc0SPaul E. McKenney static int torture_shutdown(void *arg)
427e991dbc0SPaul E. McKenney {
428e991dbc0SPaul E. McKenney 	long delta;
429e991dbc0SPaul E. McKenney 	unsigned long jiffies_snap;
430e991dbc0SPaul E. McKenney 
431e991dbc0SPaul E. McKenney 	VERBOSE_TOROUT_STRING("torture_shutdown task started");
432e991dbc0SPaul E. McKenney 	jiffies_snap = jiffies;
433e991dbc0SPaul E. McKenney 	while (ULONG_CMP_LT(jiffies_snap, shutdown_time) &&
434e991dbc0SPaul E. McKenney 	       !torture_must_stop()) {
435e991dbc0SPaul E. McKenney 		delta = shutdown_time - jiffies_snap;
436e991dbc0SPaul E. McKenney 		if (verbose)
437e991dbc0SPaul E. McKenney 			pr_alert("%s" TORTURE_FLAG
438e991dbc0SPaul E. McKenney 				 "torture_shutdown task: %lu jiffies remaining\n",
439e991dbc0SPaul E. McKenney 				 torture_type, delta);
440e991dbc0SPaul E. McKenney 		schedule_timeout_interruptible(delta);
441e991dbc0SPaul E. McKenney 		jiffies_snap = jiffies;
442e991dbc0SPaul E. McKenney 	}
443e991dbc0SPaul E. McKenney 	if (torture_must_stop()) {
4447fafaac5SPaul E. McKenney 		torture_kthread_stopping("torture_shutdown");
445e991dbc0SPaul E. McKenney 		return 0;
446e991dbc0SPaul E. McKenney 	}
447e991dbc0SPaul E. McKenney 
448e991dbc0SPaul E. McKenney 	/* OK, shut down the system. */
449e991dbc0SPaul E. McKenney 
450e991dbc0SPaul E. McKenney 	VERBOSE_TOROUT_STRING("torture_shutdown task shutting down system");
451e991dbc0SPaul E. McKenney 	shutdown_task = NULL;	/* Avoid self-kill deadlock. */
452f881825aSPaul E. McKenney 	if (torture_shutdown_hook)
453f881825aSPaul E. McKenney 		torture_shutdown_hook();
454f881825aSPaul E. McKenney 	else
455f881825aSPaul E. McKenney 		VERBOSE_TOROUT_STRING("No torture_shutdown_hook(), skipping.");
456e991dbc0SPaul E. McKenney 	kernel_power_off();	/* Shut down the system. */
457e991dbc0SPaul E. McKenney 	return 0;
458e991dbc0SPaul E. McKenney }
459e991dbc0SPaul E. McKenney 
460e991dbc0SPaul E. McKenney /*
461e991dbc0SPaul E. McKenney  * Start up the shutdown task.
462e991dbc0SPaul E. McKenney  */
463e991dbc0SPaul E. McKenney int torture_shutdown_init(int ssecs, void (*cleanup)(void))
464e991dbc0SPaul E. McKenney {
46547cf29b9SPaul E. McKenney 	int ret = 0;
466e991dbc0SPaul E. McKenney 
467e991dbc0SPaul E. McKenney 	shutdown_secs = ssecs;
468e991dbc0SPaul E. McKenney 	torture_shutdown_hook = cleanup;
469e991dbc0SPaul E. McKenney 	if (shutdown_secs > 0) {
470e991dbc0SPaul E. McKenney 		shutdown_time = jiffies + shutdown_secs * HZ;
47147cf29b9SPaul E. McKenney 		ret = torture_create_kthread(torture_shutdown, NULL,
47247cf29b9SPaul E. McKenney 					     shutdown_task);
47347cf29b9SPaul E. McKenney 	}
474e991dbc0SPaul E. McKenney 	return ret;
475e991dbc0SPaul E. McKenney }
476e991dbc0SPaul E. McKenney EXPORT_SYMBOL_GPL(torture_shutdown_init);
477e991dbc0SPaul E. McKenney 
478e991dbc0SPaul E. McKenney /*
4794622b487SPaul E. McKenney  * Detect and respond to a system shutdown.
4804622b487SPaul E. McKenney  */
4814622b487SPaul E. McKenney static int torture_shutdown_notify(struct notifier_block *unused1,
4824622b487SPaul E. McKenney 				   unsigned long unused2, void *unused3)
4834622b487SPaul E. McKenney {
4844622b487SPaul E. McKenney 	mutex_lock(&fullstop_mutex);
48557a2fe90SPaul E. McKenney 	if (ACCESS_ONCE(fullstop) == FULLSTOP_DONTSTOP) {
486fac480efSPaul E. McKenney 		VERBOSE_TOROUT_STRING("Unscheduled system shutdown detected");
48757a2fe90SPaul E. McKenney 		ACCESS_ONCE(fullstop) = FULLSTOP_SHUTDOWN;
488fac480efSPaul E. McKenney 	} else {
4894622b487SPaul E. McKenney 		pr_warn("Concurrent rmmod and shutdown illegal!\n");
490fac480efSPaul E. McKenney 	}
4914622b487SPaul E. McKenney 	mutex_unlock(&fullstop_mutex);
4924622b487SPaul E. McKenney 	return NOTIFY_DONE;
4934622b487SPaul E. McKenney }
4944622b487SPaul E. McKenney 
4954622b487SPaul E. McKenney static struct notifier_block torture_shutdown_nb = {
4964622b487SPaul E. McKenney 	.notifier_call = torture_shutdown_notify,
4974622b487SPaul E. McKenney };
4984622b487SPaul E. McKenney 
4994622b487SPaul E. McKenney /*
500bfefc73aSPaul E. McKenney  * Shut down the shutdown task.  Say what???  Heh!  This can happen if
501bfefc73aSPaul E. McKenney  * the torture module gets an rmmod before the shutdown time arrives.  ;-)
502bfefc73aSPaul E. McKenney  */
503bfefc73aSPaul E. McKenney static void torture_shutdown_cleanup(void)
504bfefc73aSPaul E. McKenney {
505bfefc73aSPaul E. McKenney 	unregister_reboot_notifier(&torture_shutdown_nb);
506bfefc73aSPaul E. McKenney 	if (shutdown_task != NULL) {
507bfefc73aSPaul E. McKenney 		VERBOSE_TOROUT_STRING("Stopping torture_shutdown task");
508bfefc73aSPaul E. McKenney 		kthread_stop(shutdown_task);
509bfefc73aSPaul E. McKenney 	}
510bfefc73aSPaul E. McKenney 	shutdown_task = NULL;
511bfefc73aSPaul E. McKenney }
512bfefc73aSPaul E. McKenney 
513bfefc73aSPaul E. McKenney /*
514628edaa5SPaul E. McKenney  * Variables for stuttering, which means to periodically pause and
515628edaa5SPaul E. McKenney  * restart testing in order to catch bugs that appear when load is
516628edaa5SPaul E. McKenney  * suddenly applied to or removed from the system.
517628edaa5SPaul E. McKenney  */
518628edaa5SPaul E. McKenney static struct task_struct *stutter_task;
519628edaa5SPaul E. McKenney static int stutter_pause_test;
520628edaa5SPaul E. McKenney static int stutter;
521628edaa5SPaul E. McKenney 
522628edaa5SPaul E. McKenney /*
523628edaa5SPaul E. McKenney  * Block until the stutter interval ends.  This must be called periodically
524628edaa5SPaul E. McKenney  * by all running kthreads that need to be subject to stuttering.
525628edaa5SPaul E. McKenney  */
526628edaa5SPaul E. McKenney void stutter_wait(const char *title)
527628edaa5SPaul E. McKenney {
528628edaa5SPaul E. McKenney 	while (ACCESS_ONCE(stutter_pause_test) ||
529628edaa5SPaul E. McKenney 	       (torture_runnable && !ACCESS_ONCE(*torture_runnable))) {
530628edaa5SPaul E. McKenney 		if (stutter_pause_test)
531ab7d4505SPaul E. McKenney 			if (ACCESS_ONCE(stutter_pause_test) == 1)
532628edaa5SPaul E. McKenney 				schedule_timeout_interruptible(1);
533628edaa5SPaul E. McKenney 			else
534ab7d4505SPaul E. McKenney 				while (ACCESS_ONCE(stutter_pause_test))
535ab7d4505SPaul E. McKenney 					cond_resched();
536ab7d4505SPaul E. McKenney 		else
537628edaa5SPaul E. McKenney 			schedule_timeout_interruptible(round_jiffies_relative(HZ));
538628edaa5SPaul E. McKenney 		torture_shutdown_absorb(title);
539628edaa5SPaul E. McKenney 	}
540628edaa5SPaul E. McKenney }
541628edaa5SPaul E. McKenney EXPORT_SYMBOL_GPL(stutter_wait);
542628edaa5SPaul E. McKenney 
543628edaa5SPaul E. McKenney /*
544628edaa5SPaul E. McKenney  * Cause the torture test to "stutter", starting and stopping all
545628edaa5SPaul E. McKenney  * threads periodically.
546628edaa5SPaul E. McKenney  */
547628edaa5SPaul E. McKenney static int torture_stutter(void *arg)
548628edaa5SPaul E. McKenney {
549628edaa5SPaul E. McKenney 	VERBOSE_TOROUT_STRING("torture_stutter task started");
550628edaa5SPaul E. McKenney 	do {
551628edaa5SPaul E. McKenney 		if (!torture_must_stop()) {
552ab7d4505SPaul E. McKenney 			if (stutter > 1) {
553ab7d4505SPaul E. McKenney 				schedule_timeout_interruptible(stutter - 1);
554ab7d4505SPaul E. McKenney 				ACCESS_ONCE(stutter_pause_test) = 2;
555ab7d4505SPaul E. McKenney 			}
556ab7d4505SPaul E. McKenney 			schedule_timeout_interruptible(1);
557628edaa5SPaul E. McKenney 			ACCESS_ONCE(stutter_pause_test) = 1;
558628edaa5SPaul E. McKenney 		}
559628edaa5SPaul E. McKenney 		if (!torture_must_stop())
560628edaa5SPaul E. McKenney 			schedule_timeout_interruptible(stutter);
561628edaa5SPaul E. McKenney 		ACCESS_ONCE(stutter_pause_test) = 0;
562628edaa5SPaul E. McKenney 		torture_shutdown_absorb("torture_stutter");
563628edaa5SPaul E. McKenney 	} while (!torture_must_stop());
5647fafaac5SPaul E. McKenney 	torture_kthread_stopping("torture_stutter");
565628edaa5SPaul E. McKenney 	return 0;
566628edaa5SPaul E. McKenney }
567628edaa5SPaul E. McKenney 
568628edaa5SPaul E. McKenney /*
569628edaa5SPaul E. McKenney  * Initialize and kick off the torture_stutter kthread.
570628edaa5SPaul E. McKenney  */
571628edaa5SPaul E. McKenney int torture_stutter_init(int s)
572628edaa5SPaul E. McKenney {
573628edaa5SPaul E. McKenney 	int ret;
574628edaa5SPaul E. McKenney 
575628edaa5SPaul E. McKenney 	stutter = s;
57647cf29b9SPaul E. McKenney 	ret = torture_create_kthread(torture_stutter, NULL, stutter_task);
577628edaa5SPaul E. McKenney 	return ret;
578628edaa5SPaul E. McKenney }
579628edaa5SPaul E. McKenney EXPORT_SYMBOL_GPL(torture_stutter_init);
580628edaa5SPaul E. McKenney 
581628edaa5SPaul E. McKenney /*
582628edaa5SPaul E. McKenney  * Cleanup after the torture_stutter kthread.
583628edaa5SPaul E. McKenney  */
584bfefc73aSPaul E. McKenney static void torture_stutter_cleanup(void)
585628edaa5SPaul E. McKenney {
586628edaa5SPaul E. McKenney 	if (!stutter_task)
587628edaa5SPaul E. McKenney 		return;
588628edaa5SPaul E. McKenney 	VERBOSE_TOROUT_STRING("Stopping torture_stutter task");
589628edaa5SPaul E. McKenney 	kthread_stop(stutter_task);
590628edaa5SPaul E. McKenney 	stutter_task = NULL;
591628edaa5SPaul E. McKenney }
592628edaa5SPaul E. McKenney 
593628edaa5SPaul E. McKenney /*
594b5daa8f3SPaul E. McKenney  * Initialize torture module.  Please note that this is -not- invoked via
595b5daa8f3SPaul E. McKenney  * the usual module_init() mechanism, but rather by an explicit call from
596b5daa8f3SPaul E. McKenney  * the client torture module.  This call must be paired with a later
597b5daa8f3SPaul E. McKenney  * torture_init_end().
598628edaa5SPaul E. McKenney  *
599628edaa5SPaul E. McKenney  * The runnable parameter points to a flag that controls whether or not
600628edaa5SPaul E. McKenney  * the test is currently runnable.  If there is no such flag, pass in NULL.
601b5daa8f3SPaul E. McKenney  */
6022b3f8ffeSPranith Kumar bool torture_init_begin(char *ttype, bool v, int *runnable)
603b5daa8f3SPaul E. McKenney {
604b5daa8f3SPaul E. McKenney 	mutex_lock(&fullstop_mutex);
6055228084eSPaul E. McKenney 	if (torture_type != NULL) {
6065228084eSPaul E. McKenney 		pr_alert("torture_init_begin: refusing %s init: %s running",
6075228084eSPaul E. McKenney 			 ttype, torture_type);
6085228084eSPaul E. McKenney 		mutex_unlock(&fullstop_mutex);
6095228084eSPaul E. McKenney 		return false;
6105228084eSPaul E. McKenney 	}
611b5daa8f3SPaul E. McKenney 	torture_type = ttype;
612b5daa8f3SPaul E. McKenney 	verbose = v;
613628edaa5SPaul E. McKenney 	torture_runnable = runnable;
61436970bb9SPaul E. McKenney 	fullstop = FULLSTOP_DONTSTOP;
6155228084eSPaul E. McKenney 	return true;
616b5daa8f3SPaul E. McKenney }
617b5daa8f3SPaul E. McKenney EXPORT_SYMBOL_GPL(torture_init_begin);
618b5daa8f3SPaul E. McKenney 
619b5daa8f3SPaul E. McKenney /*
620b5daa8f3SPaul E. McKenney  * Tell the torture module that initialization is complete.
621b5daa8f3SPaul E. McKenney  */
6222b3f8ffeSPranith Kumar void torture_init_end(void)
623b5daa8f3SPaul E. McKenney {
624b5daa8f3SPaul E. McKenney 	mutex_unlock(&fullstop_mutex);
6254622b487SPaul E. McKenney 	register_reboot_notifier(&torture_shutdown_nb);
626b5daa8f3SPaul E. McKenney }
627b5daa8f3SPaul E. McKenney EXPORT_SYMBOL_GPL(torture_init_end);
628cc47ae08SPaul E. McKenney 
629cc47ae08SPaul E. McKenney /*
630cc47ae08SPaul E. McKenney  * Clean up torture module.  Please note that this is -not- invoked via
631cc47ae08SPaul E. McKenney  * the usual module_exit() mechanism, but rather by an explicit call from
632cc47ae08SPaul E. McKenney  * the client torture module.  Returns true if a race with system shutdown
633bfefc73aSPaul E. McKenney  * is detected, otherwise, all kthreads started by functions in this file
634bfefc73aSPaul E. McKenney  * will be shut down.
635cc47ae08SPaul E. McKenney  *
636cc47ae08SPaul E. McKenney  * This must be called before the caller starts shutting down its own
637cc47ae08SPaul E. McKenney  * kthreads.
638cc47ae08SPaul E. McKenney  */
639cc47ae08SPaul E. McKenney bool torture_cleanup(void)
640cc47ae08SPaul E. McKenney {
641cc47ae08SPaul E. McKenney 	mutex_lock(&fullstop_mutex);
64257a2fe90SPaul E. McKenney 	if (ACCESS_ONCE(fullstop) == FULLSTOP_SHUTDOWN) {
643cc47ae08SPaul E. McKenney 		pr_warn("Concurrent rmmod and shutdown illegal!\n");
644cc47ae08SPaul E. McKenney 		mutex_unlock(&fullstop_mutex);
645cc47ae08SPaul E. McKenney 		schedule_timeout_uninterruptible(10);
646cc47ae08SPaul E. McKenney 		return true;
647cc47ae08SPaul E. McKenney 	}
64857a2fe90SPaul E. McKenney 	ACCESS_ONCE(fullstop) = FULLSTOP_RMMOD;
649cc47ae08SPaul E. McKenney 	mutex_unlock(&fullstop_mutex);
650bfefc73aSPaul E. McKenney 	torture_shutdown_cleanup();
651cc47ae08SPaul E. McKenney 	torture_shuffle_cleanup();
652bfefc73aSPaul E. McKenney 	torture_stutter_cleanup();
653cc47ae08SPaul E. McKenney 	torture_onoff_cleanup();
6545228084eSPaul E. McKenney 	mutex_lock(&fullstop_mutex);
6555228084eSPaul E. McKenney 	torture_type = NULL;
6565228084eSPaul E. McKenney 	mutex_unlock(&fullstop_mutex);
657cc47ae08SPaul E. McKenney 	return false;
658cc47ae08SPaul E. McKenney }
659cc47ae08SPaul E. McKenney EXPORT_SYMBOL_GPL(torture_cleanup);
66036970bb9SPaul E. McKenney 
66136970bb9SPaul E. McKenney /*
66236970bb9SPaul E. McKenney  * Is it time for the current torture test to stop?
66336970bb9SPaul E. McKenney  */
66436970bb9SPaul E. McKenney bool torture_must_stop(void)
66536970bb9SPaul E. McKenney {
66636970bb9SPaul E. McKenney 	return torture_must_stop_irq() || kthread_should_stop();
66736970bb9SPaul E. McKenney }
66836970bb9SPaul E. McKenney EXPORT_SYMBOL_GPL(torture_must_stop);
66936970bb9SPaul E. McKenney 
67036970bb9SPaul E. McKenney /*
67136970bb9SPaul E. McKenney  * Is it time for the current torture test to stop?  This is the irq-safe
67236970bb9SPaul E. McKenney  * version, hence no check for kthread_should_stop().
67336970bb9SPaul E. McKenney  */
67436970bb9SPaul E. McKenney bool torture_must_stop_irq(void)
67536970bb9SPaul E. McKenney {
67657a2fe90SPaul E. McKenney 	return ACCESS_ONCE(fullstop) != FULLSTOP_DONTSTOP;
67736970bb9SPaul E. McKenney }
67836970bb9SPaul E. McKenney EXPORT_SYMBOL_GPL(torture_must_stop_irq);
6797fafaac5SPaul E. McKenney 
6807fafaac5SPaul E. McKenney /*
6817fafaac5SPaul E. McKenney  * Each kthread must wait for kthread_should_stop() before returning from
6827fafaac5SPaul E. McKenney  * its top-level function, otherwise segfaults ensue.  This function
6837fafaac5SPaul E. McKenney  * prints a "stopping" message and waits for kthread_should_stop(), and
6847fafaac5SPaul E. McKenney  * should be called from all torture kthreads immediately prior to
6857fafaac5SPaul E. McKenney  * returning.
6867fafaac5SPaul E. McKenney  */
6877fafaac5SPaul E. McKenney void torture_kthread_stopping(char *title)
6887fafaac5SPaul E. McKenney {
6890d6821d5SPaul E. McKenney 	char buf[128];
6900d6821d5SPaul E. McKenney 
6910d6821d5SPaul E. McKenney 	snprintf(buf, sizeof(buf), "Stopping %s", title);
6920d6821d5SPaul E. McKenney 	VERBOSE_TOROUT_STRING(buf);
6937fafaac5SPaul E. McKenney 	while (!kthread_should_stop()) {
6947fafaac5SPaul E. McKenney 		torture_shutdown_absorb(title);
6957fafaac5SPaul E. McKenney 		schedule_timeout_uninterruptible(1);
6967fafaac5SPaul E. McKenney 	}
6977fafaac5SPaul E. McKenney }
6987fafaac5SPaul E. McKenney EXPORT_SYMBOL_GPL(torture_kthread_stopping);
69947cf29b9SPaul E. McKenney 
70047cf29b9SPaul E. McKenney /*
70147cf29b9SPaul E. McKenney  * Create a generic torture kthread that is immediately runnable.  If you
70247cf29b9SPaul E. McKenney  * need the kthread to be stopped so that you can do something to it before
70347cf29b9SPaul E. McKenney  * it starts, you will need to open-code your own.
70447cf29b9SPaul E. McKenney  */
70547cf29b9SPaul E. McKenney int _torture_create_kthread(int (*fn)(void *arg), void *arg, char *s, char *m,
70647cf29b9SPaul E. McKenney 			    char *f, struct task_struct **tp)
70747cf29b9SPaul E. McKenney {
70847cf29b9SPaul E. McKenney 	int ret = 0;
70947cf29b9SPaul E. McKenney 
71047cf29b9SPaul E. McKenney 	VERBOSE_TOROUT_STRING(m);
711*6945915eSKees Cook 	*tp = kthread_run(fn, arg, "%s", s);
71247cf29b9SPaul E. McKenney 	if (IS_ERR(*tp)) {
71347cf29b9SPaul E. McKenney 		ret = PTR_ERR(*tp);
71447cf29b9SPaul E. McKenney 		VERBOSE_TOROUT_ERRSTRING(f);
71547cf29b9SPaul E. McKenney 		*tp = NULL;
71647cf29b9SPaul E. McKenney 	}
71747cf29b9SPaul E. McKenney 	torture_shuffle_task_register(*tp);
71847cf29b9SPaul E. McKenney 	return ret;
71947cf29b9SPaul E. McKenney }
72047cf29b9SPaul E. McKenney EXPORT_SYMBOL_GPL(_torture_create_kthread);
7219c029b86SPaul E. McKenney 
7229c029b86SPaul E. McKenney /*
7239c029b86SPaul E. McKenney  * Stop a generic kthread, emitting a message.
7249c029b86SPaul E. McKenney  */
7259c029b86SPaul E. McKenney void _torture_stop_kthread(char *m, struct task_struct **tp)
7269c029b86SPaul E. McKenney {
7279c029b86SPaul E. McKenney 	if (*tp == NULL)
7289c029b86SPaul E. McKenney 		return;
7299c029b86SPaul E. McKenney 	VERBOSE_TOROUT_STRING(m);
7309c029b86SPaul E. McKenney 	kthread_stop(*tp);
7319c029b86SPaul E. McKenney 	*tp = NULL;
7329c029b86SPaul E. McKenney }
7339c029b86SPaul E. McKenney EXPORT_SYMBOL_GPL(_torture_stop_kthread);
734