xref: /openbmc/linux/kernel/torture.c (revision fac480efcba6a9f0aea91947f151fd569538b0af)
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);
61f67a3356SPaul E. McKenney 
622e9e8081SPaul E. McKenney #ifdef CONFIG_HOTPLUG_CPU
632e9e8081SPaul E. McKenney 
642e9e8081SPaul E. McKenney /*
652e9e8081SPaul E. McKenney  * Variables for online-offline handling.  Only present if CPU hotplug
662e9e8081SPaul E. McKenney  * is enabled, otherwise does nothing.
672e9e8081SPaul E. McKenney  */
682e9e8081SPaul E. McKenney 
692e9e8081SPaul E. McKenney static struct task_struct *onoff_task;
702e9e8081SPaul E. McKenney static long onoff_holdoff;
712e9e8081SPaul E. McKenney static long onoff_interval;
722e9e8081SPaul E. McKenney static long n_offline_attempts;
732e9e8081SPaul E. McKenney static long n_offline_successes;
742e9e8081SPaul E. McKenney static unsigned long sum_offline;
752e9e8081SPaul E. McKenney static int min_offline = -1;
762e9e8081SPaul E. McKenney static int max_offline;
772e9e8081SPaul E. McKenney static long n_online_attempts;
782e9e8081SPaul E. McKenney static long n_online_successes;
792e9e8081SPaul E. McKenney static unsigned long sum_online;
802e9e8081SPaul E. McKenney static int min_online = -1;
812e9e8081SPaul E. McKenney static int max_online;
822e9e8081SPaul E. McKenney 
832e9e8081SPaul E. McKenney /*
842e9e8081SPaul E. McKenney  * Execute random CPU-hotplug operations at the interval specified
852e9e8081SPaul E. McKenney  * by the onoff_interval.
862e9e8081SPaul E. McKenney  */
872e9e8081SPaul E. McKenney static int
882e9e8081SPaul E. McKenney torture_onoff(void *arg)
892e9e8081SPaul E. McKenney {
902e9e8081SPaul E. McKenney 	int cpu;
912e9e8081SPaul E. McKenney 	unsigned long delta;
922e9e8081SPaul E. McKenney 	int maxcpu = -1;
932e9e8081SPaul E. McKenney 	DEFINE_TORTURE_RANDOM(rand);
942e9e8081SPaul E. McKenney 	int ret;
952e9e8081SPaul E. McKenney 	unsigned long starttime;
962e9e8081SPaul E. McKenney 
972e9e8081SPaul E. McKenney 	VERBOSE_TOROUT_STRING("torture_onoff task started");
982e9e8081SPaul E. McKenney 	for_each_online_cpu(cpu)
992e9e8081SPaul E. McKenney 		maxcpu = cpu;
1002e9e8081SPaul E. McKenney 	WARN_ON(maxcpu < 0);
1012e9e8081SPaul E. McKenney 	if (onoff_holdoff > 0) {
1022e9e8081SPaul E. McKenney 		VERBOSE_TOROUT_STRING("torture_onoff begin holdoff");
1032e9e8081SPaul E. McKenney 		schedule_timeout_interruptible(onoff_holdoff);
1042e9e8081SPaul E. McKenney 		VERBOSE_TOROUT_STRING("torture_onoff end holdoff");
1052e9e8081SPaul E. McKenney 	}
1062e9e8081SPaul E. McKenney 	while (!torture_must_stop()) {
1072e9e8081SPaul E. McKenney 		cpu = (torture_random(&rand) >> 4) % (maxcpu + 1);
1082e9e8081SPaul E. McKenney 		if (cpu_online(cpu) && cpu_is_hotpluggable(cpu)) {
1092e9e8081SPaul E. McKenney 			if (verbose)
1102e9e8081SPaul E. McKenney 				pr_alert("%s" TORTURE_FLAG
1112e9e8081SPaul E. McKenney 					 "torture_onoff task: offlining %d\n",
1122e9e8081SPaul E. McKenney 					 torture_type, cpu);
1132e9e8081SPaul E. McKenney 			starttime = jiffies;
1142e9e8081SPaul E. McKenney 			n_offline_attempts++;
1152e9e8081SPaul E. McKenney 			ret = cpu_down(cpu);
1162e9e8081SPaul E. McKenney 			if (ret) {
1172e9e8081SPaul E. McKenney 				if (verbose)
1182e9e8081SPaul E. McKenney 					pr_alert("%s" TORTURE_FLAG
1192e9e8081SPaul E. McKenney 						 "torture_onoff task: offline %d failed: errno %d\n",
1202e9e8081SPaul E. McKenney 						 torture_type, cpu, ret);
1212e9e8081SPaul E. McKenney 			} else {
1222e9e8081SPaul E. McKenney 				if (verbose)
1232e9e8081SPaul E. McKenney 					pr_alert("%s" TORTURE_FLAG
1242e9e8081SPaul E. McKenney 						 "torture_onoff task: offlined %d\n",
1252e9e8081SPaul E. McKenney 						 torture_type, cpu);
1262e9e8081SPaul E. McKenney 				n_offline_successes++;
1272e9e8081SPaul E. McKenney 				delta = jiffies - starttime;
1282e9e8081SPaul E. McKenney 				sum_offline += delta;
1292e9e8081SPaul E. McKenney 				if (min_offline < 0) {
1302e9e8081SPaul E. McKenney 					min_offline = delta;
1312e9e8081SPaul E. McKenney 					max_offline = delta;
1322e9e8081SPaul E. McKenney 				}
1332e9e8081SPaul E. McKenney 				if (min_offline > delta)
1342e9e8081SPaul E. McKenney 					min_offline = delta;
1352e9e8081SPaul E. McKenney 				if (max_offline < delta)
1362e9e8081SPaul E. McKenney 					max_offline = delta;
1372e9e8081SPaul E. McKenney 			}
1382e9e8081SPaul E. McKenney 		} else if (cpu_is_hotpluggable(cpu)) {
1392e9e8081SPaul E. McKenney 			if (verbose)
1402e9e8081SPaul E. McKenney 				pr_alert("%s" TORTURE_FLAG
1412e9e8081SPaul E. McKenney 					 "torture_onoff task: onlining %d\n",
1422e9e8081SPaul E. McKenney 					 torture_type, cpu);
1432e9e8081SPaul E. McKenney 			starttime = jiffies;
1442e9e8081SPaul E. McKenney 			n_online_attempts++;
1452e9e8081SPaul E. McKenney 			ret = cpu_up(cpu);
1462e9e8081SPaul E. McKenney 			if (ret) {
1472e9e8081SPaul E. McKenney 				if (verbose)
1482e9e8081SPaul E. McKenney 					pr_alert("%s" TORTURE_FLAG
1492e9e8081SPaul E. McKenney 						 "torture_onoff task: online %d failed: errno %d\n",
1502e9e8081SPaul E. McKenney 						 torture_type, cpu, ret);
1512e9e8081SPaul E. McKenney 			} else {
1522e9e8081SPaul E. McKenney 				if (verbose)
1532e9e8081SPaul E. McKenney 					pr_alert("%s" TORTURE_FLAG
1542e9e8081SPaul E. McKenney 						 "torture_onoff task: onlined %d\n",
1552e9e8081SPaul E. McKenney 						 torture_type, cpu);
1562e9e8081SPaul E. McKenney 				n_online_successes++;
1572e9e8081SPaul E. McKenney 				delta = jiffies - starttime;
1582e9e8081SPaul E. McKenney 				sum_online += delta;
1592e9e8081SPaul E. McKenney 				if (min_online < 0) {
1602e9e8081SPaul E. McKenney 					min_online = delta;
1612e9e8081SPaul E. McKenney 					max_online = delta;
1622e9e8081SPaul E. McKenney 				}
1632e9e8081SPaul E. McKenney 				if (min_online > delta)
1642e9e8081SPaul E. McKenney 					min_online = delta;
1652e9e8081SPaul E. McKenney 				if (max_online < delta)
1662e9e8081SPaul E. McKenney 					max_online = delta;
1672e9e8081SPaul E. McKenney 			}
1682e9e8081SPaul E. McKenney 		}
1692e9e8081SPaul E. McKenney 		schedule_timeout_interruptible(onoff_interval);
1702e9e8081SPaul E. McKenney 	}
1712e9e8081SPaul E. McKenney 	VERBOSE_TOROUT_STRING("torture_onoff task stopping");
1722e9e8081SPaul E. McKenney 	return 0;
1732e9e8081SPaul E. McKenney }
1742e9e8081SPaul E. McKenney 
1752e9e8081SPaul E. McKenney #endif /* #ifdef CONFIG_HOTPLUG_CPU */
1762e9e8081SPaul E. McKenney 
1772e9e8081SPaul E. McKenney /*
1782e9e8081SPaul E. McKenney  * Initiate online-offline handling.
1792e9e8081SPaul E. McKenney  */
1802e9e8081SPaul E. McKenney int torture_onoff_init(long ooholdoff, long oointerval)
1812e9e8081SPaul E. McKenney {
1822e9e8081SPaul E. McKenney #ifdef CONFIG_HOTPLUG_CPU
1832e9e8081SPaul E. McKenney 	int ret;
1842e9e8081SPaul E. McKenney 
1852e9e8081SPaul E. McKenney 	onoff_holdoff = ooholdoff;
1862e9e8081SPaul E. McKenney 	onoff_interval = oointerval;
1872e9e8081SPaul E. McKenney 	if (onoff_interval <= 0)
1882e9e8081SPaul E. McKenney 		return 0;
1892e9e8081SPaul E. McKenney 	onoff_task = kthread_run(torture_onoff, NULL, "torture_onoff");
1902e9e8081SPaul E. McKenney 	if (IS_ERR(onoff_task)) {
1912e9e8081SPaul E. McKenney 		ret = PTR_ERR(onoff_task);
1922e9e8081SPaul E. McKenney 		onoff_task = NULL;
1932e9e8081SPaul E. McKenney 		return ret;
1942e9e8081SPaul E. McKenney 	}
1952e9e8081SPaul E. McKenney 	torture_shuffle_task_register(onoff_task);
1962e9e8081SPaul E. McKenney #endif /* #ifdef CONFIG_HOTPLUG_CPU */
1972e9e8081SPaul E. McKenney 	return 0;
1982e9e8081SPaul E. McKenney }
1992e9e8081SPaul E. McKenney EXPORT_SYMBOL_GPL(torture_onoff_init);
2002e9e8081SPaul E. McKenney 
2012e9e8081SPaul E. McKenney /*
2022e9e8081SPaul E. McKenney  * Clean up after online/offline testing.
2032e9e8081SPaul E. McKenney  */
204cc47ae08SPaul E. McKenney static void torture_onoff_cleanup(void)
2052e9e8081SPaul E. McKenney {
2062e9e8081SPaul E. McKenney #ifdef CONFIG_HOTPLUG_CPU
2072e9e8081SPaul E. McKenney 	if (onoff_task == NULL)
2082e9e8081SPaul E. McKenney 		return;
2092e9e8081SPaul E. McKenney 	VERBOSE_TOROUT_STRING("Stopping torture_onoff task");
2102e9e8081SPaul E. McKenney 	kthread_stop(onoff_task);
2112e9e8081SPaul E. McKenney 	onoff_task = NULL;
2122e9e8081SPaul E. McKenney #endif /* #ifdef CONFIG_HOTPLUG_CPU */
2132e9e8081SPaul E. McKenney }
2142e9e8081SPaul E. McKenney EXPORT_SYMBOL_GPL(torture_onoff_cleanup);
2152e9e8081SPaul E. McKenney 
2162e9e8081SPaul E. McKenney /*
2172e9e8081SPaul E. McKenney  * Print online/offline testing statistics.
2182e9e8081SPaul E. McKenney  */
2192e9e8081SPaul E. McKenney char *torture_onoff_stats(char *page)
2202e9e8081SPaul E. McKenney {
2212e9e8081SPaul E. McKenney #ifdef CONFIG_HOTPLUG_CPU
2222e9e8081SPaul E. McKenney 	page += sprintf(page,
2232e9e8081SPaul E. McKenney 		       "onoff: %ld/%ld:%ld/%ld %d,%d:%d,%d %lu:%lu (HZ=%d) ",
2242e9e8081SPaul E. McKenney 		       n_online_successes, n_online_attempts,
2252e9e8081SPaul E. McKenney 		       n_offline_successes, n_offline_attempts,
2262e9e8081SPaul E. McKenney 		       min_online, max_online,
2272e9e8081SPaul E. McKenney 		       min_offline, max_offline,
2282e9e8081SPaul E. McKenney 		       sum_online, sum_offline, HZ);
2292e9e8081SPaul E. McKenney #endif /* #ifdef CONFIG_HOTPLUG_CPU */
2302e9e8081SPaul E. McKenney 	return page;
2312e9e8081SPaul E. McKenney }
2322e9e8081SPaul E. McKenney EXPORT_SYMBOL_GPL(torture_onoff_stats);
2332e9e8081SPaul E. McKenney 
2342e9e8081SPaul E. McKenney /*
2352e9e8081SPaul E. McKenney  * Were all the online/offline operations successful?
2362e9e8081SPaul E. McKenney  */
2372e9e8081SPaul E. McKenney bool torture_onoff_failures(void)
2382e9e8081SPaul E. McKenney {
2392e9e8081SPaul E. McKenney #ifdef CONFIG_HOTPLUG_CPU
2402e9e8081SPaul E. McKenney 	return n_online_successes != n_online_attempts ||
2412e9e8081SPaul E. McKenney 	       n_offline_successes != n_offline_attempts;
2422e9e8081SPaul E. McKenney #else /* #ifdef CONFIG_HOTPLUG_CPU */
2432e9e8081SPaul E. McKenney 	return false;
2442e9e8081SPaul E. McKenney #endif /* #else #ifdef CONFIG_HOTPLUG_CPU */
2452e9e8081SPaul E. McKenney }
2462e9e8081SPaul E. McKenney EXPORT_SYMBOL_GPL(torture_onoff_failures);
2472e9e8081SPaul E. McKenney 
24851b1130eSPaul E. McKenney #define TORTURE_RANDOM_MULT	39916801  /* prime */
24951b1130eSPaul E. McKenney #define TORTURE_RANDOM_ADD	479001701 /* prime */
25051b1130eSPaul E. McKenney #define TORTURE_RANDOM_REFRESH	10000
25151b1130eSPaul E. McKenney 
25251b1130eSPaul E. McKenney /*
25351b1130eSPaul E. McKenney  * Crude but fast random-number generator.  Uses a linear congruential
25451b1130eSPaul E. McKenney  * generator, with occasional help from cpu_clock().
25551b1130eSPaul E. McKenney  */
25651b1130eSPaul E. McKenney unsigned long
25751b1130eSPaul E. McKenney torture_random(struct torture_random_state *trsp)
25851b1130eSPaul E. McKenney {
25951b1130eSPaul E. McKenney 	if (--trsp->trs_count < 0) {
26051b1130eSPaul E. McKenney 		trsp->trs_state += (unsigned long)local_clock();
26151b1130eSPaul E. McKenney 		trsp->trs_count = TORTURE_RANDOM_REFRESH;
26251b1130eSPaul E. McKenney 	}
26351b1130eSPaul E. McKenney 	trsp->trs_state = trsp->trs_state * TORTURE_RANDOM_MULT +
26451b1130eSPaul E. McKenney 		TORTURE_RANDOM_ADD;
26551b1130eSPaul E. McKenney 	return swahw32(trsp->trs_state);
26651b1130eSPaul E. McKenney }
26751b1130eSPaul E. McKenney EXPORT_SYMBOL_GPL(torture_random);
268f67a3356SPaul E. McKenney 
269f67a3356SPaul E. McKenney /*
2703808dc9fSPaul E. McKenney  * Variables for shuffling.  The idea is to ensure that each CPU stays
2713808dc9fSPaul E. McKenney  * idle for an extended period to test interactions with dyntick idle,
2723808dc9fSPaul E. McKenney  * as well as interactions with any per-CPU varibles.
2733808dc9fSPaul E. McKenney  */
2743808dc9fSPaul E. McKenney struct shuffle_task {
2753808dc9fSPaul E. McKenney 	struct list_head st_l;
2763808dc9fSPaul E. McKenney 	struct task_struct *st_t;
2773808dc9fSPaul E. McKenney };
2783808dc9fSPaul E. McKenney 
2793808dc9fSPaul E. McKenney static long shuffle_interval;	/* In jiffies. */
2803808dc9fSPaul E. McKenney static struct task_struct *shuffler_task;
2813808dc9fSPaul E. McKenney static cpumask_var_t shuffle_tmp_mask;
2823808dc9fSPaul E. McKenney static int shuffle_idle_cpu;	/* Force all torture tasks off this CPU */
2833808dc9fSPaul E. McKenney static struct list_head shuffle_task_list = LIST_HEAD_INIT(shuffle_task_list);
2843808dc9fSPaul E. McKenney static DEFINE_MUTEX(shuffle_task_mutex);
2853808dc9fSPaul E. McKenney 
2863808dc9fSPaul E. McKenney /*
2873808dc9fSPaul E. McKenney  * Register a task to be shuffled.  If there is no memory, just splat
2883808dc9fSPaul E. McKenney  * and don't bother registering.
2893808dc9fSPaul E. McKenney  */
2903808dc9fSPaul E. McKenney void torture_shuffle_task_register(struct task_struct *tp)
2913808dc9fSPaul E. McKenney {
2923808dc9fSPaul E. McKenney 	struct shuffle_task *stp;
2933808dc9fSPaul E. McKenney 
2943808dc9fSPaul E. McKenney 	if (WARN_ON_ONCE(tp == NULL))
2953808dc9fSPaul E. McKenney 		return;
2963808dc9fSPaul E. McKenney 	stp = kmalloc(sizeof(*stp), GFP_KERNEL);
2973808dc9fSPaul E. McKenney 	if (WARN_ON_ONCE(stp == NULL))
2983808dc9fSPaul E. McKenney 		return;
2993808dc9fSPaul E. McKenney 	stp->st_t = tp;
3003808dc9fSPaul E. McKenney 	mutex_lock(&shuffle_task_mutex);
3013808dc9fSPaul E. McKenney 	list_add(&stp->st_l, &shuffle_task_list);
3023808dc9fSPaul E. McKenney 	mutex_unlock(&shuffle_task_mutex);
3033808dc9fSPaul E. McKenney }
3043808dc9fSPaul E. McKenney EXPORT_SYMBOL_GPL(torture_shuffle_task_register);
3053808dc9fSPaul E. McKenney 
3063808dc9fSPaul E. McKenney /*
3073808dc9fSPaul E. McKenney  * Unregister all tasks, for example, at the end of the torture run.
3083808dc9fSPaul E. McKenney  */
3093808dc9fSPaul E. McKenney static void torture_shuffle_task_unregister_all(void)
3103808dc9fSPaul E. McKenney {
3113808dc9fSPaul E. McKenney 	struct shuffle_task *stp;
3123808dc9fSPaul E. McKenney 	struct shuffle_task *p;
3133808dc9fSPaul E. McKenney 
3143808dc9fSPaul E. McKenney 	mutex_lock(&shuffle_task_mutex);
3153808dc9fSPaul E. McKenney 	list_for_each_entry_safe(stp, p, &shuffle_task_list, st_l) {
3163808dc9fSPaul E. McKenney 		list_del(&stp->st_l);
3173808dc9fSPaul E. McKenney 		kfree(stp);
3183808dc9fSPaul E. McKenney 	}
3193808dc9fSPaul E. McKenney 	mutex_unlock(&shuffle_task_mutex);
3203808dc9fSPaul E. McKenney }
3213808dc9fSPaul E. McKenney 
3223808dc9fSPaul E. McKenney /* Shuffle tasks such that we allow shuffle_idle_cpu to become idle.
3233808dc9fSPaul E. McKenney  * A special case is when shuffle_idle_cpu = -1, in which case we allow
3243808dc9fSPaul E. McKenney  * the tasks to run on all CPUs.
3253808dc9fSPaul E. McKenney  */
3263808dc9fSPaul E. McKenney static void torture_shuffle_tasks(void)
3273808dc9fSPaul E. McKenney {
3283808dc9fSPaul E. McKenney 	struct shuffle_task *stp;
3293808dc9fSPaul E. McKenney 
3303808dc9fSPaul E. McKenney 	cpumask_setall(shuffle_tmp_mask);
3313808dc9fSPaul E. McKenney 	get_online_cpus();
3323808dc9fSPaul E. McKenney 
3333808dc9fSPaul E. McKenney 	/* No point in shuffling if there is only one online CPU (ex: UP) */
3343808dc9fSPaul E. McKenney 	if (num_online_cpus() == 1) {
3353808dc9fSPaul E. McKenney 		put_online_cpus();
3363808dc9fSPaul E. McKenney 		return;
3373808dc9fSPaul E. McKenney 	}
3383808dc9fSPaul E. McKenney 
3393808dc9fSPaul E. McKenney 	/* Advance to the next CPU.  Upon overflow, don't idle any CPUs. */
3403808dc9fSPaul E. McKenney 	shuffle_idle_cpu = cpumask_next(shuffle_idle_cpu, shuffle_tmp_mask);
3413808dc9fSPaul E. McKenney 	if (shuffle_idle_cpu >= nr_cpu_ids)
3423808dc9fSPaul E. McKenney 		shuffle_idle_cpu = -1;
3433808dc9fSPaul E. McKenney 	if (shuffle_idle_cpu != -1) {
3443808dc9fSPaul E. McKenney 		cpumask_clear_cpu(shuffle_idle_cpu, shuffle_tmp_mask);
3453808dc9fSPaul E. McKenney 		if (cpumask_empty(shuffle_tmp_mask)) {
3463808dc9fSPaul E. McKenney 			put_online_cpus();
3473808dc9fSPaul E. McKenney 			return;
3483808dc9fSPaul E. McKenney 		}
3493808dc9fSPaul E. McKenney 	}
3503808dc9fSPaul E. McKenney 
3513808dc9fSPaul E. McKenney 	mutex_lock(&shuffle_task_mutex);
3523808dc9fSPaul E. McKenney 	list_for_each_entry(stp, &shuffle_task_list, st_l)
3533808dc9fSPaul E. McKenney 		set_cpus_allowed_ptr(stp->st_t, shuffle_tmp_mask);
3543808dc9fSPaul E. McKenney 	mutex_unlock(&shuffle_task_mutex);
3553808dc9fSPaul E. McKenney 
3563808dc9fSPaul E. McKenney 	put_online_cpus();
3573808dc9fSPaul E. McKenney }
3583808dc9fSPaul E. McKenney 
3593808dc9fSPaul E. McKenney /* Shuffle tasks across CPUs, with the intent of allowing each CPU in the
3603808dc9fSPaul E. McKenney  * system to become idle at a time and cut off its timer ticks. This is meant
3613808dc9fSPaul E. McKenney  * to test the support for such tickless idle CPU in RCU.
3623808dc9fSPaul E. McKenney  */
3633808dc9fSPaul E. McKenney static int torture_shuffle(void *arg)
3643808dc9fSPaul E. McKenney {
3653808dc9fSPaul E. McKenney 	VERBOSE_TOROUT_STRING("torture_shuffle task started");
3663808dc9fSPaul E. McKenney 	do {
3673808dc9fSPaul E. McKenney 		schedule_timeout_interruptible(shuffle_interval);
3683808dc9fSPaul E. McKenney 		torture_shuffle_tasks();
3693808dc9fSPaul E. McKenney 		torture_shutdown_absorb("torture_shuffle");
3703808dc9fSPaul E. McKenney 	} while (!torture_must_stop());
3713808dc9fSPaul E. McKenney 	VERBOSE_TOROUT_STRING("torture_shuffle task stopping");
3723808dc9fSPaul E. McKenney 	return 0;
3733808dc9fSPaul E. McKenney }
3743808dc9fSPaul E. McKenney 
3753808dc9fSPaul E. McKenney /*
3763808dc9fSPaul E. McKenney  * Start the shuffler, with shuffint in jiffies.
3773808dc9fSPaul E. McKenney  */
3783808dc9fSPaul E. McKenney int torture_shuffle_init(long shuffint)
3793808dc9fSPaul E. McKenney {
3803808dc9fSPaul E. McKenney 	int ret;
3813808dc9fSPaul E. McKenney 
3823808dc9fSPaul E. McKenney 	shuffle_interval = shuffint;
3833808dc9fSPaul E. McKenney 
3843808dc9fSPaul E. McKenney 	shuffle_idle_cpu = -1;
3853808dc9fSPaul E. McKenney 
3863808dc9fSPaul E. McKenney 	if (!alloc_cpumask_var(&shuffle_tmp_mask, GFP_KERNEL)) {
3873808dc9fSPaul E. McKenney 		VERBOSE_TOROUT_ERRSTRING("Failed to alloc mask");
3883808dc9fSPaul E. McKenney 		return -ENOMEM;
3893808dc9fSPaul E. McKenney 	}
3903808dc9fSPaul E. McKenney 
3913808dc9fSPaul E. McKenney 	/* Create the shuffler thread */
3923808dc9fSPaul E. McKenney 	shuffler_task = kthread_run(torture_shuffle, NULL, "torture_shuffle");
3933808dc9fSPaul E. McKenney 	if (IS_ERR(shuffler_task)) {
3943808dc9fSPaul E. McKenney 		ret = PTR_ERR(shuffler_task);
3953808dc9fSPaul E. McKenney 		free_cpumask_var(shuffle_tmp_mask);
3963808dc9fSPaul E. McKenney 		VERBOSE_TOROUT_ERRSTRING("Failed to create shuffler");
3973808dc9fSPaul E. McKenney 		shuffler_task = NULL;
3983808dc9fSPaul E. McKenney 		return ret;
3993808dc9fSPaul E. McKenney 	}
4003808dc9fSPaul E. McKenney 	torture_shuffle_task_register(shuffler_task);
4013808dc9fSPaul E. McKenney 	return 0;
4023808dc9fSPaul E. McKenney }
4033808dc9fSPaul E. McKenney EXPORT_SYMBOL_GPL(torture_shuffle_init);
4043808dc9fSPaul E. McKenney 
4053808dc9fSPaul E. McKenney /*
4063808dc9fSPaul E. McKenney  * Stop the shuffling.
4073808dc9fSPaul E. McKenney  */
408cc47ae08SPaul E. McKenney static void torture_shuffle_cleanup(void)
4093808dc9fSPaul E. McKenney {
4103808dc9fSPaul E. McKenney 	torture_shuffle_task_unregister_all();
4113808dc9fSPaul E. McKenney 	if (shuffler_task) {
4123808dc9fSPaul E. McKenney 		VERBOSE_TOROUT_STRING("Stopping torture_shuffle task");
4133808dc9fSPaul E. McKenney 		kthread_stop(shuffler_task);
4143808dc9fSPaul E. McKenney 		free_cpumask_var(shuffle_tmp_mask);
4153808dc9fSPaul E. McKenney 	}
4163808dc9fSPaul E. McKenney 	shuffler_task = NULL;
4173808dc9fSPaul E. McKenney }
4183808dc9fSPaul E. McKenney EXPORT_SYMBOL_GPL(torture_shuffle_cleanup);
4193808dc9fSPaul E. McKenney 
4203808dc9fSPaul E. McKenney /*
421f67a3356SPaul E. McKenney  * Absorb kthreads into a kernel function that won't return, so that
422f67a3356SPaul E. McKenney  * they won't ever access module text or data again.
423f67a3356SPaul E. McKenney  */
424f67a3356SPaul E. McKenney void torture_shutdown_absorb(const char *title)
425f67a3356SPaul E. McKenney {
426f67a3356SPaul E. McKenney 	while (ACCESS_ONCE(fullstop) == FULLSTOP_SHUTDOWN) {
4274622b487SPaul E. McKenney 		pr_notice("torture thread %s parking due to system shutdown\n",
428f67a3356SPaul E. McKenney 			  title);
429f67a3356SPaul E. McKenney 		schedule_timeout_uninterruptible(MAX_SCHEDULE_TIMEOUT);
430f67a3356SPaul E. McKenney 	}
431f67a3356SPaul E. McKenney }
432f67a3356SPaul E. McKenney EXPORT_SYMBOL_GPL(torture_shutdown_absorb);
433b5daa8f3SPaul E. McKenney 
434b5daa8f3SPaul E. McKenney /*
4354622b487SPaul E. McKenney  * Detect and respond to a system shutdown.
4364622b487SPaul E. McKenney  */
4374622b487SPaul E. McKenney static int torture_shutdown_notify(struct notifier_block *unused1,
4384622b487SPaul E. McKenney 				   unsigned long unused2, void *unused3)
4394622b487SPaul E. McKenney {
4404622b487SPaul E. McKenney 	mutex_lock(&fullstop_mutex);
441*fac480efSPaul E. McKenney 	if (fullstop == FULLSTOP_DONTSTOP) {
442*fac480efSPaul E. McKenney 		VERBOSE_TOROUT_STRING("Unscheduled system shutdown detected");
4434622b487SPaul E. McKenney 		fullstop = FULLSTOP_SHUTDOWN;
444*fac480efSPaul E. McKenney 	} else {
4454622b487SPaul E. McKenney 		pr_warn("Concurrent rmmod and shutdown illegal!\n");
446*fac480efSPaul E. McKenney 	}
4474622b487SPaul E. McKenney 	mutex_unlock(&fullstop_mutex);
4484622b487SPaul E. McKenney 	return NOTIFY_DONE;
4494622b487SPaul E. McKenney }
4504622b487SPaul E. McKenney 
4514622b487SPaul E. McKenney static struct notifier_block torture_shutdown_nb = {
4524622b487SPaul E. McKenney 	.notifier_call = torture_shutdown_notify,
4534622b487SPaul E. McKenney };
4544622b487SPaul E. McKenney 
4554622b487SPaul E. McKenney /*
456b5daa8f3SPaul E. McKenney  * Initialize torture module.  Please note that this is -not- invoked via
457b5daa8f3SPaul E. McKenney  * the usual module_init() mechanism, but rather by an explicit call from
458b5daa8f3SPaul E. McKenney  * the client torture module.  This call must be paired with a later
459b5daa8f3SPaul E. McKenney  * torture_init_end().
460b5daa8f3SPaul E. McKenney  */
461b5daa8f3SPaul E. McKenney void __init torture_init_begin(char *ttype, bool v)
462b5daa8f3SPaul E. McKenney {
463b5daa8f3SPaul E. McKenney 	mutex_lock(&fullstop_mutex);
464b5daa8f3SPaul E. McKenney 	torture_type = ttype;
465b5daa8f3SPaul E. McKenney 	verbose = v;
46636970bb9SPaul E. McKenney 	fullstop = FULLSTOP_DONTSTOP;
467b5daa8f3SPaul E. McKenney 
468b5daa8f3SPaul E. McKenney }
469b5daa8f3SPaul E. McKenney EXPORT_SYMBOL_GPL(torture_init_begin);
470b5daa8f3SPaul E. McKenney 
471b5daa8f3SPaul E. McKenney /*
472b5daa8f3SPaul E. McKenney  * Tell the torture module that initialization is complete.
473b5daa8f3SPaul E. McKenney  */
474b5daa8f3SPaul E. McKenney void __init torture_init_end(void)
475b5daa8f3SPaul E. McKenney {
476b5daa8f3SPaul E. McKenney 	mutex_unlock(&fullstop_mutex);
4774622b487SPaul E. McKenney 	register_reboot_notifier(&torture_shutdown_nb);
478b5daa8f3SPaul E. McKenney }
479b5daa8f3SPaul E. McKenney EXPORT_SYMBOL_GPL(torture_init_end);
480cc47ae08SPaul E. McKenney 
481cc47ae08SPaul E. McKenney /*
482cc47ae08SPaul E. McKenney  * Clean up torture module.  Please note that this is -not- invoked via
483cc47ae08SPaul E. McKenney  * the usual module_exit() mechanism, but rather by an explicit call from
484cc47ae08SPaul E. McKenney  * the client torture module.  Returns true if a race with system shutdown
485cc47ae08SPaul E. McKenney  * is detected.
486cc47ae08SPaul E. McKenney  *
487cc47ae08SPaul E. McKenney  * This must be called before the caller starts shutting down its own
488cc47ae08SPaul E. McKenney  * kthreads.
489cc47ae08SPaul E. McKenney  */
490cc47ae08SPaul E. McKenney bool torture_cleanup(void)
491cc47ae08SPaul E. McKenney {
492cc47ae08SPaul E. McKenney 	mutex_lock(&fullstop_mutex);
493cc47ae08SPaul E. McKenney 	if (fullstop == FULLSTOP_SHUTDOWN) {
494cc47ae08SPaul E. McKenney 		pr_warn("Concurrent rmmod and shutdown illegal!\n");
495cc47ae08SPaul E. McKenney 		mutex_unlock(&fullstop_mutex);
496cc47ae08SPaul E. McKenney 		schedule_timeout_uninterruptible(10);
497cc47ae08SPaul E. McKenney 		return true;
498cc47ae08SPaul E. McKenney 	}
499cc47ae08SPaul E. McKenney 	fullstop = FULLSTOP_RMMOD;
500cc47ae08SPaul E. McKenney 	mutex_unlock(&fullstop_mutex);
5014622b487SPaul E. McKenney 	unregister_reboot_notifier(&torture_shutdown_nb);
502cc47ae08SPaul E. McKenney 	torture_shuffle_cleanup();
503cc47ae08SPaul E. McKenney 	torture_onoff_cleanup();
504cc47ae08SPaul E. McKenney 	return false;
505cc47ae08SPaul E. McKenney }
506cc47ae08SPaul E. McKenney EXPORT_SYMBOL_GPL(torture_cleanup);
50736970bb9SPaul E. McKenney 
50836970bb9SPaul E. McKenney /*
50936970bb9SPaul E. McKenney  * Is it time for the current torture test to stop?
51036970bb9SPaul E. McKenney  */
51136970bb9SPaul E. McKenney bool torture_must_stop(void)
51236970bb9SPaul E. McKenney {
51336970bb9SPaul E. McKenney 	return torture_must_stop_irq() || kthread_should_stop();
51436970bb9SPaul E. McKenney }
51536970bb9SPaul E. McKenney EXPORT_SYMBOL_GPL(torture_must_stop);
51636970bb9SPaul E. McKenney 
51736970bb9SPaul E. McKenney /*
51836970bb9SPaul E. McKenney  * Is it time for the current torture test to stop?  This is the irq-safe
51936970bb9SPaul E. McKenney  * version, hence no check for kthread_should_stop().
52036970bb9SPaul E. McKenney  */
52136970bb9SPaul E. McKenney bool torture_must_stop_irq(void)
52236970bb9SPaul E. McKenney {
52336970bb9SPaul E. McKenney 	return fullstop != FULLSTOP_DONTSTOP;
52436970bb9SPaul E. McKenney }
52536970bb9SPaul E. McKenney EXPORT_SYMBOL_GPL(torture_must_stop_irq);
526