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> 33e6017571SIngo Molnar #include <linux/sched/clock.h> 3451b1130eSPaul E. McKenney #include <linux/atomic.h> 3551b1130eSPaul E. McKenney #include <linux/bitops.h> 3651b1130eSPaul E. McKenney #include <linux/completion.h> 3751b1130eSPaul E. McKenney #include <linux/moduleparam.h> 3851b1130eSPaul E. McKenney #include <linux/percpu.h> 3951b1130eSPaul E. McKenney #include <linux/notifier.h> 4051b1130eSPaul E. McKenney #include <linux/reboot.h> 4151b1130eSPaul E. McKenney #include <linux/freezer.h> 4251b1130eSPaul E. McKenney #include <linux/cpu.h> 4351b1130eSPaul E. McKenney #include <linux/delay.h> 4451b1130eSPaul E. McKenney #include <linux/stat.h> 4551b1130eSPaul E. McKenney #include <linux/slab.h> 4651b1130eSPaul E. McKenney #include <linux/trace_clock.h> 4731257c3cSPaul E. McKenney #include <linux/ktime.h> 4851b1130eSPaul E. McKenney #include <asm/byteorder.h> 4951b1130eSPaul E. McKenney #include <linux/torture.h> 50dac95906SPaul E. McKenney #include "rcu/rcu.h" 5151b1130eSPaul E. McKenney 5251b1130eSPaul E. McKenney MODULE_LICENSE("GPL"); 5351b1130eSPaul E. McKenney MODULE_AUTHOR("Paul E. McKenney <paulmck@us.ibm.com>"); 5451b1130eSPaul E. McKenney 55b5daa8f3SPaul E. McKenney static char *torture_type; 56*90127d60SPaul E. McKenney static int verbose; 57b5daa8f3SPaul E. McKenney 5836970bb9SPaul E. McKenney /* Mediate rmmod and system shutdown. Concurrent rmmod & shutdown illegal! */ 5936970bb9SPaul E. McKenney #define FULLSTOP_DONTSTOP 0 /* Normal operation. */ 6036970bb9SPaul E. McKenney #define FULLSTOP_SHUTDOWN 1 /* System shutdown with torture running. */ 6136970bb9SPaul E. McKenney #define FULLSTOP_RMMOD 2 /* Normal rmmod of torture. */ 6236970bb9SPaul E. McKenney static int fullstop = FULLSTOP_RMMOD; 634622b487SPaul E. McKenney static DEFINE_MUTEX(fullstop_mutex); 64f67a3356SPaul E. McKenney 652e9e8081SPaul E. McKenney #ifdef CONFIG_HOTPLUG_CPU 662e9e8081SPaul E. McKenney 672e9e8081SPaul E. McKenney /* 682e9e8081SPaul E. McKenney * Variables for online-offline handling. Only present if CPU hotplug 692e9e8081SPaul E. McKenney * is enabled, otherwise does nothing. 702e9e8081SPaul E. McKenney */ 712e9e8081SPaul E. McKenney 722e9e8081SPaul E. McKenney static struct task_struct *onoff_task; 732e9e8081SPaul E. McKenney static long onoff_holdoff; 742e9e8081SPaul E. McKenney static long onoff_interval; 752e9e8081SPaul E. McKenney static long n_offline_attempts; 762e9e8081SPaul E. McKenney static long n_offline_successes; 772e9e8081SPaul E. McKenney static unsigned long sum_offline; 782e9e8081SPaul E. McKenney static int min_offline = -1; 792e9e8081SPaul E. McKenney static int max_offline; 802e9e8081SPaul E. McKenney static long n_online_attempts; 812e9e8081SPaul E. McKenney static long n_online_successes; 822e9e8081SPaul E. McKenney static unsigned long sum_online; 832e9e8081SPaul E. McKenney static int min_online = -1; 842e9e8081SPaul E. McKenney static int max_online; 852e9e8081SPaul E. McKenney 862e9e8081SPaul E. McKenney /* 87d95f5ba9SPaul E. McKenney * Attempt to take a CPU offline. Return false if the CPU is already 88d95f5ba9SPaul E. McKenney * offline or if it is not subject to CPU-hotplug operations. The 89d95f5ba9SPaul E. McKenney * caller can detect other failures by looking at the statistics. 90d95f5ba9SPaul E. McKenney */ 91d95f5ba9SPaul E. McKenney bool torture_offline(int cpu, long *n_offl_attempts, long *n_offl_successes, 92d95f5ba9SPaul E. McKenney unsigned long *sum_offl, int *min_offl, int *max_offl) 93d95f5ba9SPaul E. McKenney { 94d95f5ba9SPaul E. McKenney unsigned long delta; 95d95f5ba9SPaul E. McKenney int ret; 96d95f5ba9SPaul E. McKenney unsigned long starttime; 97d95f5ba9SPaul E. McKenney 98d95f5ba9SPaul E. McKenney if (!cpu_online(cpu) || !cpu_is_hotpluggable(cpu)) 99d95f5ba9SPaul E. McKenney return false; 100d95f5ba9SPaul E. McKenney 101*90127d60SPaul E. McKenney if (verbose > 1) 102d95f5ba9SPaul E. McKenney pr_alert("%s" TORTURE_FLAG 103d95f5ba9SPaul E. McKenney "torture_onoff task: offlining %d\n", 104d95f5ba9SPaul E. McKenney torture_type, cpu); 105d95f5ba9SPaul E. McKenney starttime = jiffies; 106d95f5ba9SPaul E. McKenney (*n_offl_attempts)++; 107d95f5ba9SPaul E. McKenney ret = cpu_down(cpu); 108d95f5ba9SPaul E. McKenney if (ret) { 109d95f5ba9SPaul E. McKenney if (verbose) 110d95f5ba9SPaul E. McKenney pr_alert("%s" TORTURE_FLAG 111d95f5ba9SPaul E. McKenney "torture_onoff task: offline %d failed: errno %d\n", 112d95f5ba9SPaul E. McKenney torture_type, cpu, ret); 113d95f5ba9SPaul E. McKenney } else { 114*90127d60SPaul E. McKenney if (verbose > 1) 115d95f5ba9SPaul E. McKenney pr_alert("%s" TORTURE_FLAG 116d95f5ba9SPaul E. McKenney "torture_onoff task: offlined %d\n", 117d95f5ba9SPaul E. McKenney torture_type, cpu); 118d95f5ba9SPaul E. McKenney (*n_offl_successes)++; 119d95f5ba9SPaul E. McKenney delta = jiffies - starttime; 120a2b2df20SPaul E. McKenney *sum_offl += delta; 121d95f5ba9SPaul E. McKenney if (*min_offl < 0) { 122d95f5ba9SPaul E. McKenney *min_offl = delta; 123d95f5ba9SPaul E. McKenney *max_offl = delta; 124d95f5ba9SPaul E. McKenney } 125d95f5ba9SPaul E. McKenney if (*min_offl > delta) 126d95f5ba9SPaul E. McKenney *min_offl = delta; 127d95f5ba9SPaul E. McKenney if (*max_offl < delta) 128d95f5ba9SPaul E. McKenney *max_offl = delta; 129d95f5ba9SPaul E. McKenney } 130d95f5ba9SPaul E. McKenney 131d95f5ba9SPaul E. McKenney return true; 132d95f5ba9SPaul E. McKenney } 133d95f5ba9SPaul E. McKenney EXPORT_SYMBOL_GPL(torture_offline); 134d95f5ba9SPaul E. McKenney 135d95f5ba9SPaul E. McKenney /* 136d95f5ba9SPaul E. McKenney * Attempt to bring a CPU online. Return false if the CPU is already 137d95f5ba9SPaul E. McKenney * online or if it is not subject to CPU-hotplug operations. The 138d95f5ba9SPaul E. McKenney * caller can detect other failures by looking at the statistics. 139d95f5ba9SPaul E. McKenney */ 140d95f5ba9SPaul E. McKenney bool torture_online(int cpu, long *n_onl_attempts, long *n_onl_successes, 141d95f5ba9SPaul E. McKenney unsigned long *sum_onl, int *min_onl, int *max_onl) 142d95f5ba9SPaul E. McKenney { 143d95f5ba9SPaul E. McKenney unsigned long delta; 144d95f5ba9SPaul E. McKenney int ret; 145d95f5ba9SPaul E. McKenney unsigned long starttime; 146d95f5ba9SPaul E. McKenney 147d95f5ba9SPaul E. McKenney if (cpu_online(cpu) || !cpu_is_hotpluggable(cpu)) 148d95f5ba9SPaul E. McKenney return false; 149d95f5ba9SPaul E. McKenney 150*90127d60SPaul E. McKenney if (verbose > 1) 151d95f5ba9SPaul E. McKenney pr_alert("%s" TORTURE_FLAG 152d95f5ba9SPaul E. McKenney "torture_onoff task: onlining %d\n", 153d95f5ba9SPaul E. McKenney torture_type, cpu); 154d95f5ba9SPaul E. McKenney starttime = jiffies; 155d95f5ba9SPaul E. McKenney (*n_onl_attempts)++; 156d95f5ba9SPaul E. McKenney ret = cpu_up(cpu); 157d95f5ba9SPaul E. McKenney if (ret) { 158d95f5ba9SPaul E. McKenney if (verbose) 159d95f5ba9SPaul E. McKenney pr_alert("%s" TORTURE_FLAG 160d95f5ba9SPaul E. McKenney "torture_onoff task: online %d failed: errno %d\n", 161d95f5ba9SPaul E. McKenney torture_type, cpu, ret); 162d95f5ba9SPaul E. McKenney } else { 163*90127d60SPaul E. McKenney if (verbose > 1) 164d95f5ba9SPaul E. McKenney pr_alert("%s" TORTURE_FLAG 165d95f5ba9SPaul E. McKenney "torture_onoff task: onlined %d\n", 166d95f5ba9SPaul E. McKenney torture_type, cpu); 167d95f5ba9SPaul E. McKenney (*n_onl_successes)++; 168d95f5ba9SPaul E. McKenney delta = jiffies - starttime; 169d95f5ba9SPaul E. McKenney *sum_onl += delta; 170d95f5ba9SPaul E. McKenney if (*min_onl < 0) { 171d95f5ba9SPaul E. McKenney *min_onl = delta; 172d95f5ba9SPaul E. McKenney *max_onl = delta; 173d95f5ba9SPaul E. McKenney } 174d95f5ba9SPaul E. McKenney if (*min_onl > delta) 175d95f5ba9SPaul E. McKenney *min_onl = delta; 176d95f5ba9SPaul E. McKenney if (*max_onl < delta) 177d95f5ba9SPaul E. McKenney *max_onl = delta; 178d95f5ba9SPaul E. McKenney } 179d95f5ba9SPaul E. McKenney 180d95f5ba9SPaul E. McKenney return true; 181d95f5ba9SPaul E. McKenney } 182d95f5ba9SPaul E. McKenney EXPORT_SYMBOL_GPL(torture_online); 183d95f5ba9SPaul E. McKenney 184d95f5ba9SPaul E. McKenney /* 1852e9e8081SPaul E. McKenney * Execute random CPU-hotplug operations at the interval specified 1862e9e8081SPaul E. McKenney * by the onoff_interval. 1872e9e8081SPaul E. McKenney */ 1882e9e8081SPaul E. McKenney static int 1892e9e8081SPaul E. McKenney torture_onoff(void *arg) 1902e9e8081SPaul E. McKenney { 1912e9e8081SPaul E. McKenney int cpu; 1922e9e8081SPaul E. McKenney int maxcpu = -1; 1932e9e8081SPaul E. McKenney DEFINE_TORTURE_RANDOM(rand); 1942e9e8081SPaul E. McKenney 1952e9e8081SPaul E. McKenney VERBOSE_TOROUT_STRING("torture_onoff task started"); 1962e9e8081SPaul E. McKenney for_each_online_cpu(cpu) 1972e9e8081SPaul E. McKenney maxcpu = cpu; 1982e9e8081SPaul E. McKenney WARN_ON(maxcpu < 0); 199750db0f5SBoqun Feng 200750db0f5SBoqun Feng if (maxcpu == 0) { 201750db0f5SBoqun Feng VERBOSE_TOROUT_STRING("Only one CPU, so CPU-hotplug testing is disabled"); 202750db0f5SBoqun Feng goto stop; 203750db0f5SBoqun Feng } 204750db0f5SBoqun Feng 2052e9e8081SPaul E. McKenney if (onoff_holdoff > 0) { 2062e9e8081SPaul E. McKenney VERBOSE_TOROUT_STRING("torture_onoff begin holdoff"); 2072e9e8081SPaul E. McKenney schedule_timeout_interruptible(onoff_holdoff); 2082e9e8081SPaul E. McKenney VERBOSE_TOROUT_STRING("torture_onoff end holdoff"); 2092e9e8081SPaul E. McKenney } 2102e9e8081SPaul E. McKenney while (!torture_must_stop()) { 2112e9e8081SPaul E. McKenney cpu = (torture_random(&rand) >> 4) % (maxcpu + 1); 212d95f5ba9SPaul E. McKenney if (!torture_offline(cpu, 213d95f5ba9SPaul E. McKenney &n_offline_attempts, &n_offline_successes, 214d95f5ba9SPaul E. McKenney &sum_offline, &min_offline, &max_offline)) 215d95f5ba9SPaul E. McKenney torture_online(cpu, 216d95f5ba9SPaul E. McKenney &n_online_attempts, &n_online_successes, 217d95f5ba9SPaul E. McKenney &sum_online, &min_online, &max_online); 2182e9e8081SPaul E. McKenney schedule_timeout_interruptible(onoff_interval); 2192e9e8081SPaul E. McKenney } 220750db0f5SBoqun Feng 221750db0f5SBoqun Feng stop: 2227fafaac5SPaul E. McKenney torture_kthread_stopping("torture_onoff"); 2232e9e8081SPaul E. McKenney return 0; 2242e9e8081SPaul E. McKenney } 2252e9e8081SPaul E. McKenney 2262e9e8081SPaul E. McKenney #endif /* #ifdef CONFIG_HOTPLUG_CPU */ 2272e9e8081SPaul E. McKenney 2282e9e8081SPaul E. McKenney /* 2292e9e8081SPaul E. McKenney * Initiate online-offline handling. 2302e9e8081SPaul E. McKenney */ 2312e9e8081SPaul E. McKenney int torture_onoff_init(long ooholdoff, long oointerval) 2322e9e8081SPaul E. McKenney { 23347cf29b9SPaul E. McKenney int ret = 0; 2342e9e8081SPaul E. McKenney 23547cf29b9SPaul E. McKenney #ifdef CONFIG_HOTPLUG_CPU 2362e9e8081SPaul E. McKenney onoff_holdoff = ooholdoff; 2372e9e8081SPaul E. McKenney onoff_interval = oointerval; 2382e9e8081SPaul E. McKenney if (onoff_interval <= 0) 2392e9e8081SPaul E. McKenney return 0; 24047cf29b9SPaul E. McKenney ret = torture_create_kthread(torture_onoff, NULL, onoff_task); 2412e9e8081SPaul E. McKenney #endif /* #ifdef CONFIG_HOTPLUG_CPU */ 24247cf29b9SPaul E. McKenney return ret; 2432e9e8081SPaul E. McKenney } 2442e9e8081SPaul E. McKenney EXPORT_SYMBOL_GPL(torture_onoff_init); 2452e9e8081SPaul E. McKenney 2462e9e8081SPaul E. McKenney /* 2472e9e8081SPaul E. McKenney * Clean up after online/offline testing. 2482e9e8081SPaul E. McKenney */ 249cc47ae08SPaul E. McKenney static void torture_onoff_cleanup(void) 2502e9e8081SPaul E. McKenney { 2512e9e8081SPaul E. McKenney #ifdef CONFIG_HOTPLUG_CPU 2522e9e8081SPaul E. McKenney if (onoff_task == NULL) 2532e9e8081SPaul E. McKenney return; 2542e9e8081SPaul E. McKenney VERBOSE_TOROUT_STRING("Stopping torture_onoff task"); 2552e9e8081SPaul E. McKenney kthread_stop(onoff_task); 2562e9e8081SPaul E. McKenney onoff_task = NULL; 2572e9e8081SPaul E. McKenney #endif /* #ifdef CONFIG_HOTPLUG_CPU */ 2582e9e8081SPaul E. McKenney } 2592e9e8081SPaul E. McKenney EXPORT_SYMBOL_GPL(torture_onoff_cleanup); 2602e9e8081SPaul E. McKenney 2612e9e8081SPaul E. McKenney /* 2622e9e8081SPaul E. McKenney * Print online/offline testing statistics. 2632e9e8081SPaul E. McKenney */ 264eea203feSJoe Perches void torture_onoff_stats(void) 2652e9e8081SPaul E. McKenney { 2662e9e8081SPaul E. McKenney #ifdef CONFIG_HOTPLUG_CPU 267eea203feSJoe Perches pr_cont("onoff: %ld/%ld:%ld/%ld %d,%d:%d,%d %lu:%lu (HZ=%d) ", 2682e9e8081SPaul E. McKenney n_online_successes, n_online_attempts, 2692e9e8081SPaul E. McKenney n_offline_successes, n_offline_attempts, 2702e9e8081SPaul E. McKenney min_online, max_online, 2712e9e8081SPaul E. McKenney min_offline, max_offline, 2722e9e8081SPaul E. McKenney sum_online, sum_offline, HZ); 2732e9e8081SPaul E. McKenney #endif /* #ifdef CONFIG_HOTPLUG_CPU */ 2742e9e8081SPaul E. McKenney } 2752e9e8081SPaul E. McKenney EXPORT_SYMBOL_GPL(torture_onoff_stats); 2762e9e8081SPaul E. McKenney 2772e9e8081SPaul E. McKenney /* 2782e9e8081SPaul E. McKenney * Were all the online/offline operations successful? 2792e9e8081SPaul E. McKenney */ 2802e9e8081SPaul E. McKenney bool torture_onoff_failures(void) 2812e9e8081SPaul E. McKenney { 2822e9e8081SPaul E. McKenney #ifdef CONFIG_HOTPLUG_CPU 2832e9e8081SPaul E. McKenney return n_online_successes != n_online_attempts || 2842e9e8081SPaul E. McKenney n_offline_successes != n_offline_attempts; 2852e9e8081SPaul E. McKenney #else /* #ifdef CONFIG_HOTPLUG_CPU */ 2862e9e8081SPaul E. McKenney return false; 2872e9e8081SPaul E. McKenney #endif /* #else #ifdef CONFIG_HOTPLUG_CPU */ 2882e9e8081SPaul E. McKenney } 2892e9e8081SPaul E. McKenney EXPORT_SYMBOL_GPL(torture_onoff_failures); 2902e9e8081SPaul E. McKenney 29151b1130eSPaul E. McKenney #define TORTURE_RANDOM_MULT 39916801 /* prime */ 29251b1130eSPaul E. McKenney #define TORTURE_RANDOM_ADD 479001701 /* prime */ 29351b1130eSPaul E. McKenney #define TORTURE_RANDOM_REFRESH 10000 29451b1130eSPaul E. McKenney 29551b1130eSPaul E. McKenney /* 29651b1130eSPaul E. McKenney * Crude but fast random-number generator. Uses a linear congruential 29751b1130eSPaul E. McKenney * generator, with occasional help from cpu_clock(). 29851b1130eSPaul E. McKenney */ 29951b1130eSPaul E. McKenney unsigned long 30051b1130eSPaul E. McKenney torture_random(struct torture_random_state *trsp) 30151b1130eSPaul E. McKenney { 30251b1130eSPaul E. McKenney if (--trsp->trs_count < 0) { 30351b1130eSPaul E. McKenney trsp->trs_state += (unsigned long)local_clock(); 30451b1130eSPaul E. McKenney trsp->trs_count = TORTURE_RANDOM_REFRESH; 30551b1130eSPaul E. McKenney } 30651b1130eSPaul E. McKenney trsp->trs_state = trsp->trs_state * TORTURE_RANDOM_MULT + 30751b1130eSPaul E. McKenney TORTURE_RANDOM_ADD; 30851b1130eSPaul E. McKenney return swahw32(trsp->trs_state); 30951b1130eSPaul E. McKenney } 31051b1130eSPaul E. McKenney EXPORT_SYMBOL_GPL(torture_random); 311f67a3356SPaul E. McKenney 312f67a3356SPaul E. McKenney /* 3133808dc9fSPaul E. McKenney * Variables for shuffling. The idea is to ensure that each CPU stays 3143808dc9fSPaul E. McKenney * idle for an extended period to test interactions with dyntick idle, 315b564d62eSMasahiro Yamada * as well as interactions with any per-CPU variables. 3163808dc9fSPaul E. McKenney */ 3173808dc9fSPaul E. McKenney struct shuffle_task { 3183808dc9fSPaul E. McKenney struct list_head st_l; 3193808dc9fSPaul E. McKenney struct task_struct *st_t; 3203808dc9fSPaul E. McKenney }; 3213808dc9fSPaul E. McKenney 3223808dc9fSPaul E. McKenney static long shuffle_interval; /* In jiffies. */ 3233808dc9fSPaul E. McKenney static struct task_struct *shuffler_task; 3243808dc9fSPaul E. McKenney static cpumask_var_t shuffle_tmp_mask; 3253808dc9fSPaul E. McKenney static int shuffle_idle_cpu; /* Force all torture tasks off this CPU */ 3263808dc9fSPaul E. McKenney static struct list_head shuffle_task_list = LIST_HEAD_INIT(shuffle_task_list); 3273808dc9fSPaul E. McKenney static DEFINE_MUTEX(shuffle_task_mutex); 3283808dc9fSPaul E. McKenney 3293808dc9fSPaul E. McKenney /* 3303808dc9fSPaul E. McKenney * Register a task to be shuffled. If there is no memory, just splat 3313808dc9fSPaul E. McKenney * and don't bother registering. 3323808dc9fSPaul E. McKenney */ 3333808dc9fSPaul E. McKenney void torture_shuffle_task_register(struct task_struct *tp) 3343808dc9fSPaul E. McKenney { 3353808dc9fSPaul E. McKenney struct shuffle_task *stp; 3363808dc9fSPaul E. McKenney 3373808dc9fSPaul E. McKenney if (WARN_ON_ONCE(tp == NULL)) 3383808dc9fSPaul E. McKenney return; 3393808dc9fSPaul E. McKenney stp = kmalloc(sizeof(*stp), GFP_KERNEL); 3403808dc9fSPaul E. McKenney if (WARN_ON_ONCE(stp == NULL)) 3413808dc9fSPaul E. McKenney return; 3423808dc9fSPaul E. McKenney stp->st_t = tp; 3433808dc9fSPaul E. McKenney mutex_lock(&shuffle_task_mutex); 3443808dc9fSPaul E. McKenney list_add(&stp->st_l, &shuffle_task_list); 3453808dc9fSPaul E. McKenney mutex_unlock(&shuffle_task_mutex); 3463808dc9fSPaul E. McKenney } 3473808dc9fSPaul E. McKenney EXPORT_SYMBOL_GPL(torture_shuffle_task_register); 3483808dc9fSPaul E. McKenney 3493808dc9fSPaul E. McKenney /* 3503808dc9fSPaul E. McKenney * Unregister all tasks, for example, at the end of the torture run. 3513808dc9fSPaul E. McKenney */ 3523808dc9fSPaul E. McKenney static void torture_shuffle_task_unregister_all(void) 3533808dc9fSPaul E. McKenney { 3543808dc9fSPaul E. McKenney struct shuffle_task *stp; 3553808dc9fSPaul E. McKenney struct shuffle_task *p; 3563808dc9fSPaul E. McKenney 3573808dc9fSPaul E. McKenney mutex_lock(&shuffle_task_mutex); 3583808dc9fSPaul E. McKenney list_for_each_entry_safe(stp, p, &shuffle_task_list, st_l) { 3593808dc9fSPaul E. McKenney list_del(&stp->st_l); 3603808dc9fSPaul E. McKenney kfree(stp); 3613808dc9fSPaul E. McKenney } 3623808dc9fSPaul E. McKenney mutex_unlock(&shuffle_task_mutex); 3633808dc9fSPaul E. McKenney } 3643808dc9fSPaul E. McKenney 3653808dc9fSPaul E. McKenney /* Shuffle tasks such that we allow shuffle_idle_cpu to become idle. 3663808dc9fSPaul E. McKenney * A special case is when shuffle_idle_cpu = -1, in which case we allow 3673808dc9fSPaul E. McKenney * the tasks to run on all CPUs. 3683808dc9fSPaul E. McKenney */ 3693808dc9fSPaul E. McKenney static void torture_shuffle_tasks(void) 3703808dc9fSPaul E. McKenney { 3713808dc9fSPaul E. McKenney struct shuffle_task *stp; 3723808dc9fSPaul E. McKenney 3733808dc9fSPaul E. McKenney cpumask_setall(shuffle_tmp_mask); 3743808dc9fSPaul E. McKenney get_online_cpus(); 3753808dc9fSPaul E. McKenney 3763808dc9fSPaul E. McKenney /* No point in shuffling if there is only one online CPU (ex: UP) */ 3773808dc9fSPaul E. McKenney if (num_online_cpus() == 1) { 3783808dc9fSPaul E. McKenney put_online_cpus(); 3793808dc9fSPaul E. McKenney return; 3803808dc9fSPaul E. McKenney } 3813808dc9fSPaul E. McKenney 3823808dc9fSPaul E. McKenney /* Advance to the next CPU. Upon overflow, don't idle any CPUs. */ 3833808dc9fSPaul E. McKenney shuffle_idle_cpu = cpumask_next(shuffle_idle_cpu, shuffle_tmp_mask); 3843808dc9fSPaul E. McKenney if (shuffle_idle_cpu >= nr_cpu_ids) 3853808dc9fSPaul E. McKenney shuffle_idle_cpu = -1; 3865ed63b19SIulia Manda else 3873808dc9fSPaul E. McKenney cpumask_clear_cpu(shuffle_idle_cpu, shuffle_tmp_mask); 3883808dc9fSPaul E. McKenney 3893808dc9fSPaul E. McKenney mutex_lock(&shuffle_task_mutex); 3903808dc9fSPaul E. McKenney list_for_each_entry(stp, &shuffle_task_list, st_l) 3913808dc9fSPaul E. McKenney set_cpus_allowed_ptr(stp->st_t, shuffle_tmp_mask); 3923808dc9fSPaul E. McKenney mutex_unlock(&shuffle_task_mutex); 3933808dc9fSPaul E. McKenney 3943808dc9fSPaul E. McKenney put_online_cpus(); 3953808dc9fSPaul E. McKenney } 3963808dc9fSPaul E. McKenney 3973808dc9fSPaul E. McKenney /* Shuffle tasks across CPUs, with the intent of allowing each CPU in the 3983808dc9fSPaul E. McKenney * system to become idle at a time and cut off its timer ticks. This is meant 3993808dc9fSPaul E. McKenney * to test the support for such tickless idle CPU in RCU. 4003808dc9fSPaul E. McKenney */ 4013808dc9fSPaul E. McKenney static int torture_shuffle(void *arg) 4023808dc9fSPaul E. McKenney { 4033808dc9fSPaul E. McKenney VERBOSE_TOROUT_STRING("torture_shuffle task started"); 4043808dc9fSPaul E. McKenney do { 4053808dc9fSPaul E. McKenney schedule_timeout_interruptible(shuffle_interval); 4063808dc9fSPaul E. McKenney torture_shuffle_tasks(); 4073808dc9fSPaul E. McKenney torture_shutdown_absorb("torture_shuffle"); 4083808dc9fSPaul E. McKenney } while (!torture_must_stop()); 4097fafaac5SPaul E. McKenney torture_kthread_stopping("torture_shuffle"); 4103808dc9fSPaul E. McKenney return 0; 4113808dc9fSPaul E. McKenney } 4123808dc9fSPaul E. McKenney 4133808dc9fSPaul E. McKenney /* 4143808dc9fSPaul E. McKenney * Start the shuffler, with shuffint in jiffies. 4153808dc9fSPaul E. McKenney */ 4163808dc9fSPaul E. McKenney int torture_shuffle_init(long shuffint) 4173808dc9fSPaul E. McKenney { 4183808dc9fSPaul E. McKenney shuffle_interval = shuffint; 4193808dc9fSPaul E. McKenney 4203808dc9fSPaul E. McKenney shuffle_idle_cpu = -1; 4213808dc9fSPaul E. McKenney 4223808dc9fSPaul E. McKenney if (!alloc_cpumask_var(&shuffle_tmp_mask, GFP_KERNEL)) { 4233808dc9fSPaul E. McKenney VERBOSE_TOROUT_ERRSTRING("Failed to alloc mask"); 4243808dc9fSPaul E. McKenney return -ENOMEM; 4253808dc9fSPaul E. McKenney } 4263808dc9fSPaul E. McKenney 4273808dc9fSPaul E. McKenney /* Create the shuffler thread */ 42847cf29b9SPaul E. McKenney return torture_create_kthread(torture_shuffle, NULL, shuffler_task); 4293808dc9fSPaul E. McKenney } 4303808dc9fSPaul E. McKenney EXPORT_SYMBOL_GPL(torture_shuffle_init); 4313808dc9fSPaul E. McKenney 4323808dc9fSPaul E. McKenney /* 4333808dc9fSPaul E. McKenney * Stop the shuffling. 4343808dc9fSPaul E. McKenney */ 435cc47ae08SPaul E. McKenney static void torture_shuffle_cleanup(void) 4363808dc9fSPaul E. McKenney { 4373808dc9fSPaul E. McKenney torture_shuffle_task_unregister_all(); 4383808dc9fSPaul E. McKenney if (shuffler_task) { 4393808dc9fSPaul E. McKenney VERBOSE_TOROUT_STRING("Stopping torture_shuffle task"); 4403808dc9fSPaul E. McKenney kthread_stop(shuffler_task); 4413808dc9fSPaul E. McKenney free_cpumask_var(shuffle_tmp_mask); 4423808dc9fSPaul E. McKenney } 4433808dc9fSPaul E. McKenney shuffler_task = NULL; 4443808dc9fSPaul E. McKenney } 4453808dc9fSPaul E. McKenney EXPORT_SYMBOL_GPL(torture_shuffle_cleanup); 4463808dc9fSPaul E. McKenney 4473808dc9fSPaul E. McKenney /* 448e991dbc0SPaul E. McKenney * Variables for auto-shutdown. This allows "lights out" torture runs 449e991dbc0SPaul E. McKenney * to be fully scripted. 450e991dbc0SPaul E. McKenney */ 451e991dbc0SPaul E. McKenney static struct task_struct *shutdown_task; 45231257c3cSPaul E. McKenney static ktime_t shutdown_time; /* time to system shutdown. */ 453e991dbc0SPaul E. McKenney static void (*torture_shutdown_hook)(void); 454e991dbc0SPaul E. McKenney 455e991dbc0SPaul E. McKenney /* 456f67a3356SPaul E. McKenney * Absorb kthreads into a kernel function that won't return, so that 457f67a3356SPaul E. McKenney * they won't ever access module text or data again. 458f67a3356SPaul E. McKenney */ 459f67a3356SPaul E. McKenney void torture_shutdown_absorb(const char *title) 460f67a3356SPaul E. McKenney { 4617d0ae808SPaul E. McKenney while (READ_ONCE(fullstop) == FULLSTOP_SHUTDOWN) { 4624622b487SPaul E. McKenney pr_notice("torture thread %s parking due to system shutdown\n", 463f67a3356SPaul E. McKenney title); 464f67a3356SPaul E. McKenney schedule_timeout_uninterruptible(MAX_SCHEDULE_TIMEOUT); 465f67a3356SPaul E. McKenney } 466f67a3356SPaul E. McKenney } 467f67a3356SPaul E. McKenney EXPORT_SYMBOL_GPL(torture_shutdown_absorb); 468b5daa8f3SPaul E. McKenney 469b5daa8f3SPaul E. McKenney /* 470e991dbc0SPaul E. McKenney * Cause the torture test to shutdown the system after the test has 471e991dbc0SPaul E. McKenney * run for the time specified by the shutdown_secs parameter. 472e991dbc0SPaul E. McKenney */ 473e991dbc0SPaul E. McKenney static int torture_shutdown(void *arg) 474e991dbc0SPaul E. McKenney { 47531257c3cSPaul E. McKenney ktime_t ktime_snap; 476e991dbc0SPaul E. McKenney 477e991dbc0SPaul E. McKenney VERBOSE_TOROUT_STRING("torture_shutdown task started"); 47831257c3cSPaul E. McKenney ktime_snap = ktime_get(); 47931257c3cSPaul E. McKenney while (ktime_before(ktime_snap, shutdown_time) && 480e991dbc0SPaul E. McKenney !torture_must_stop()) { 481e991dbc0SPaul E. McKenney if (verbose) 482e991dbc0SPaul E. McKenney pr_alert("%s" TORTURE_FLAG 48331257c3cSPaul E. McKenney "torture_shutdown task: %llu ms remaining\n", 48431257c3cSPaul E. McKenney torture_type, 48531257c3cSPaul E. McKenney ktime_ms_delta(shutdown_time, ktime_snap)); 48631257c3cSPaul E. McKenney set_current_state(TASK_INTERRUPTIBLE); 48731257c3cSPaul E. McKenney schedule_hrtimeout(&shutdown_time, HRTIMER_MODE_ABS); 48831257c3cSPaul E. McKenney ktime_snap = ktime_get(); 489e991dbc0SPaul E. McKenney } 490e991dbc0SPaul E. McKenney if (torture_must_stop()) { 4917fafaac5SPaul E. McKenney torture_kthread_stopping("torture_shutdown"); 492e991dbc0SPaul E. McKenney return 0; 493e991dbc0SPaul E. McKenney } 494e991dbc0SPaul E. McKenney 495e991dbc0SPaul E. McKenney /* OK, shut down the system. */ 496e991dbc0SPaul E. McKenney 497e991dbc0SPaul E. McKenney VERBOSE_TOROUT_STRING("torture_shutdown task shutting down system"); 498e991dbc0SPaul E. McKenney shutdown_task = NULL; /* Avoid self-kill deadlock. */ 499f881825aSPaul E. McKenney if (torture_shutdown_hook) 500f881825aSPaul E. McKenney torture_shutdown_hook(); 501f881825aSPaul E. McKenney else 502f881825aSPaul E. McKenney VERBOSE_TOROUT_STRING("No torture_shutdown_hook(), skipping."); 503dac95906SPaul E. McKenney rcu_ftrace_dump(DUMP_ALL); 504e991dbc0SPaul E. McKenney kernel_power_off(); /* Shut down the system. */ 505e991dbc0SPaul E. McKenney return 0; 506e991dbc0SPaul E. McKenney } 507e991dbc0SPaul E. McKenney 508e991dbc0SPaul E. McKenney /* 509e991dbc0SPaul E. McKenney * Start up the shutdown task. 510e991dbc0SPaul E. McKenney */ 511e991dbc0SPaul E. McKenney int torture_shutdown_init(int ssecs, void (*cleanup)(void)) 512e991dbc0SPaul E. McKenney { 51347cf29b9SPaul E. McKenney int ret = 0; 514e991dbc0SPaul E. McKenney 515e991dbc0SPaul E. McKenney torture_shutdown_hook = cleanup; 51631257c3cSPaul E. McKenney if (ssecs > 0) { 51731257c3cSPaul E. McKenney shutdown_time = ktime_add(ktime_get(), ktime_set(ssecs, 0)); 51847cf29b9SPaul E. McKenney ret = torture_create_kthread(torture_shutdown, NULL, 51947cf29b9SPaul E. McKenney shutdown_task); 52047cf29b9SPaul E. McKenney } 521e991dbc0SPaul E. McKenney return ret; 522e991dbc0SPaul E. McKenney } 523e991dbc0SPaul E. McKenney EXPORT_SYMBOL_GPL(torture_shutdown_init); 524e991dbc0SPaul E. McKenney 525e991dbc0SPaul E. McKenney /* 5264622b487SPaul E. McKenney * Detect and respond to a system shutdown. 5274622b487SPaul E. McKenney */ 5284622b487SPaul E. McKenney static int torture_shutdown_notify(struct notifier_block *unused1, 5294622b487SPaul E. McKenney unsigned long unused2, void *unused3) 5304622b487SPaul E. McKenney { 5314622b487SPaul E. McKenney mutex_lock(&fullstop_mutex); 5327d0ae808SPaul E. McKenney if (READ_ONCE(fullstop) == FULLSTOP_DONTSTOP) { 533fac480efSPaul E. McKenney VERBOSE_TOROUT_STRING("Unscheduled system shutdown detected"); 5347d0ae808SPaul E. McKenney WRITE_ONCE(fullstop, FULLSTOP_SHUTDOWN); 535fac480efSPaul E. McKenney } else { 5364622b487SPaul E. McKenney pr_warn("Concurrent rmmod and shutdown illegal!\n"); 537fac480efSPaul E. McKenney } 5384622b487SPaul E. McKenney mutex_unlock(&fullstop_mutex); 5394622b487SPaul E. McKenney return NOTIFY_DONE; 5404622b487SPaul E. McKenney } 5414622b487SPaul E. McKenney 5424622b487SPaul E. McKenney static struct notifier_block torture_shutdown_nb = { 5434622b487SPaul E. McKenney .notifier_call = torture_shutdown_notify, 5444622b487SPaul E. McKenney }; 5454622b487SPaul E. McKenney 5464622b487SPaul E. McKenney /* 547bfefc73aSPaul E. McKenney * Shut down the shutdown task. Say what??? Heh! This can happen if 548bfefc73aSPaul E. McKenney * the torture module gets an rmmod before the shutdown time arrives. ;-) 549bfefc73aSPaul E. McKenney */ 550bfefc73aSPaul E. McKenney static void torture_shutdown_cleanup(void) 551bfefc73aSPaul E. McKenney { 552bfefc73aSPaul E. McKenney unregister_reboot_notifier(&torture_shutdown_nb); 553bfefc73aSPaul E. McKenney if (shutdown_task != NULL) { 554bfefc73aSPaul E. McKenney VERBOSE_TOROUT_STRING("Stopping torture_shutdown task"); 555bfefc73aSPaul E. McKenney kthread_stop(shutdown_task); 556bfefc73aSPaul E. McKenney } 557bfefc73aSPaul E. McKenney shutdown_task = NULL; 558bfefc73aSPaul E. McKenney } 559bfefc73aSPaul E. McKenney 560bfefc73aSPaul E. McKenney /* 561628edaa5SPaul E. McKenney * Variables for stuttering, which means to periodically pause and 562628edaa5SPaul E. McKenney * restart testing in order to catch bugs that appear when load is 563628edaa5SPaul E. McKenney * suddenly applied to or removed from the system. 564628edaa5SPaul E. McKenney */ 565628edaa5SPaul E. McKenney static struct task_struct *stutter_task; 566628edaa5SPaul E. McKenney static int stutter_pause_test; 567628edaa5SPaul E. McKenney static int stutter; 568628edaa5SPaul E. McKenney 569628edaa5SPaul E. McKenney /* 570628edaa5SPaul E. McKenney * Block until the stutter interval ends. This must be called periodically 571628edaa5SPaul E. McKenney * by all running kthreads that need to be subject to stuttering. 572628edaa5SPaul E. McKenney */ 573628edaa5SPaul E. McKenney void stutter_wait(const char *title) 574628edaa5SPaul E. McKenney { 5754ced3314SPaul E. McKenney int spt; 5764ced3314SPaul E. McKenney 577cee43939SPaul E. McKenney cond_resched_tasks_rcu_qs(); 5784ced3314SPaul E. McKenney spt = READ_ONCE(stutter_pause_test); 57929d39390SPaul E. McKenney for (; spt; spt = READ_ONCE(stutter_pause_test)) { 5804ced3314SPaul E. McKenney if (spt == 1) { 581628edaa5SPaul E. McKenney schedule_timeout_interruptible(1); 5824ced3314SPaul E. McKenney } else if (spt == 2) { 5837d0ae808SPaul E. McKenney while (READ_ONCE(stutter_pause_test)) 584ab7d4505SPaul E. McKenney cond_resched(); 5854ced3314SPaul E. McKenney } else { 586628edaa5SPaul E. McKenney schedule_timeout_interruptible(round_jiffies_relative(HZ)); 5874ced3314SPaul E. McKenney } 588628edaa5SPaul E. McKenney torture_shutdown_absorb(title); 589628edaa5SPaul E. McKenney } 590628edaa5SPaul E. McKenney } 591628edaa5SPaul E. McKenney EXPORT_SYMBOL_GPL(stutter_wait); 592628edaa5SPaul E. McKenney 593628edaa5SPaul E. McKenney /* 594628edaa5SPaul E. McKenney * Cause the torture test to "stutter", starting and stopping all 595628edaa5SPaul E. McKenney * threads periodically. 596628edaa5SPaul E. McKenney */ 597628edaa5SPaul E. McKenney static int torture_stutter(void *arg) 598628edaa5SPaul E. McKenney { 599628edaa5SPaul E. McKenney VERBOSE_TOROUT_STRING("torture_stutter task started"); 600628edaa5SPaul E. McKenney do { 6014ced3314SPaul E. McKenney if (!torture_must_stop() && stutter > 1) { 6024ced3314SPaul E. McKenney WRITE_ONCE(stutter_pause_test, 1); 603ab7d4505SPaul E. McKenney schedule_timeout_interruptible(stutter - 1); 6047d0ae808SPaul E. McKenney WRITE_ONCE(stutter_pause_test, 2); 605ab7d4505SPaul E. McKenney schedule_timeout_interruptible(1); 606628edaa5SPaul E. McKenney } 6074ced3314SPaul E. McKenney WRITE_ONCE(stutter_pause_test, 0); 608628edaa5SPaul E. McKenney if (!torture_must_stop()) 609628edaa5SPaul E. McKenney schedule_timeout_interruptible(stutter); 610628edaa5SPaul E. McKenney torture_shutdown_absorb("torture_stutter"); 611628edaa5SPaul E. McKenney } while (!torture_must_stop()); 6127fafaac5SPaul E. McKenney torture_kthread_stopping("torture_stutter"); 613628edaa5SPaul E. McKenney return 0; 614628edaa5SPaul E. McKenney } 615628edaa5SPaul E. McKenney 616628edaa5SPaul E. McKenney /* 617628edaa5SPaul E. McKenney * Initialize and kick off the torture_stutter kthread. 618628edaa5SPaul E. McKenney */ 619628edaa5SPaul E. McKenney int torture_stutter_init(int s) 620628edaa5SPaul E. McKenney { 621628edaa5SPaul E. McKenney int ret; 622628edaa5SPaul E. McKenney 623628edaa5SPaul E. McKenney stutter = s; 62447cf29b9SPaul E. McKenney ret = torture_create_kthread(torture_stutter, NULL, stutter_task); 625628edaa5SPaul E. McKenney return ret; 626628edaa5SPaul E. McKenney } 627628edaa5SPaul E. McKenney EXPORT_SYMBOL_GPL(torture_stutter_init); 628628edaa5SPaul E. McKenney 629628edaa5SPaul E. McKenney /* 630628edaa5SPaul E. McKenney * Cleanup after the torture_stutter kthread. 631628edaa5SPaul E. McKenney */ 632bfefc73aSPaul E. McKenney static void torture_stutter_cleanup(void) 633628edaa5SPaul E. McKenney { 634628edaa5SPaul E. McKenney if (!stutter_task) 635628edaa5SPaul E. McKenney return; 636628edaa5SPaul E. McKenney VERBOSE_TOROUT_STRING("Stopping torture_stutter task"); 637628edaa5SPaul E. McKenney kthread_stop(stutter_task); 638628edaa5SPaul E. McKenney stutter_task = NULL; 639628edaa5SPaul E. McKenney } 640628edaa5SPaul E. McKenney 641628edaa5SPaul E. McKenney /* 642b5daa8f3SPaul E. McKenney * Initialize torture module. Please note that this is -not- invoked via 643b5daa8f3SPaul E. McKenney * the usual module_init() mechanism, but rather by an explicit call from 644b5daa8f3SPaul E. McKenney * the client torture module. This call must be paired with a later 645b5daa8f3SPaul E. McKenney * torture_init_end(). 646628edaa5SPaul E. McKenney * 647628edaa5SPaul E. McKenney * The runnable parameter points to a flag that controls whether or not 648628edaa5SPaul E. McKenney * the test is currently runnable. If there is no such flag, pass in NULL. 649b5daa8f3SPaul E. McKenney */ 650*90127d60SPaul E. McKenney bool torture_init_begin(char *ttype, int v) 651b5daa8f3SPaul E. McKenney { 652b5daa8f3SPaul E. McKenney mutex_lock(&fullstop_mutex); 6535228084eSPaul E. McKenney if (torture_type != NULL) { 6549eb5188aSPaul E. McKenney pr_alert("torture_init_begin: Refusing %s init: %s running.\n", 6555228084eSPaul E. McKenney ttype, torture_type); 6569eb5188aSPaul E. McKenney pr_alert("torture_init_begin: One torture test at a time!\n"); 6575228084eSPaul E. McKenney mutex_unlock(&fullstop_mutex); 6585228084eSPaul E. McKenney return false; 6595228084eSPaul E. McKenney } 660b5daa8f3SPaul E. McKenney torture_type = ttype; 661b5daa8f3SPaul E. McKenney verbose = v; 66236970bb9SPaul E. McKenney fullstop = FULLSTOP_DONTSTOP; 6635228084eSPaul E. McKenney return true; 664b5daa8f3SPaul E. McKenney } 665b5daa8f3SPaul E. McKenney EXPORT_SYMBOL_GPL(torture_init_begin); 666b5daa8f3SPaul E. McKenney 667b5daa8f3SPaul E. McKenney /* 668b5daa8f3SPaul E. McKenney * Tell the torture module that initialization is complete. 669b5daa8f3SPaul E. McKenney */ 6702b3f8ffeSPranith Kumar void torture_init_end(void) 671b5daa8f3SPaul E. McKenney { 672b5daa8f3SPaul E. McKenney mutex_unlock(&fullstop_mutex); 6734622b487SPaul E. McKenney register_reboot_notifier(&torture_shutdown_nb); 674b5daa8f3SPaul E. McKenney } 675b5daa8f3SPaul E. McKenney EXPORT_SYMBOL_GPL(torture_init_end); 676cc47ae08SPaul E. McKenney 677cc47ae08SPaul E. McKenney /* 678cc47ae08SPaul E. McKenney * Clean up torture module. Please note that this is -not- invoked via 679cc47ae08SPaul E. McKenney * the usual module_exit() mechanism, but rather by an explicit call from 680cc47ae08SPaul E. McKenney * the client torture module. Returns true if a race with system shutdown 681bfefc73aSPaul E. McKenney * is detected, otherwise, all kthreads started by functions in this file 682bfefc73aSPaul E. McKenney * will be shut down. 683cc47ae08SPaul E. McKenney * 684cc47ae08SPaul E. McKenney * This must be called before the caller starts shutting down its own 685cc47ae08SPaul E. McKenney * kthreads. 686d36a7a0dSDavidlohr Bueso * 687d36a7a0dSDavidlohr Bueso * Both torture_cleanup_begin() and torture_cleanup_end() must be paired, 688d36a7a0dSDavidlohr Bueso * in order to correctly perform the cleanup. They are separated because 689d36a7a0dSDavidlohr Bueso * threads can still need to reference the torture_type type, thus nullify 690d36a7a0dSDavidlohr Bueso * only after completing all other relevant calls. 691cc47ae08SPaul E. McKenney */ 692d36a7a0dSDavidlohr Bueso bool torture_cleanup_begin(void) 693cc47ae08SPaul E. McKenney { 694cc47ae08SPaul E. McKenney mutex_lock(&fullstop_mutex); 6957d0ae808SPaul E. McKenney if (READ_ONCE(fullstop) == FULLSTOP_SHUTDOWN) { 696cc47ae08SPaul E. McKenney pr_warn("Concurrent rmmod and shutdown illegal!\n"); 697cc47ae08SPaul E. McKenney mutex_unlock(&fullstop_mutex); 698cc47ae08SPaul E. McKenney schedule_timeout_uninterruptible(10); 699cc47ae08SPaul E. McKenney return true; 700cc47ae08SPaul E. McKenney } 7017d0ae808SPaul E. McKenney WRITE_ONCE(fullstop, FULLSTOP_RMMOD); 702cc47ae08SPaul E. McKenney mutex_unlock(&fullstop_mutex); 703bfefc73aSPaul E. McKenney torture_shutdown_cleanup(); 704cc47ae08SPaul E. McKenney torture_shuffle_cleanup(); 705bfefc73aSPaul E. McKenney torture_stutter_cleanup(); 706cc47ae08SPaul E. McKenney torture_onoff_cleanup(); 707d36a7a0dSDavidlohr Bueso return false; 708d36a7a0dSDavidlohr Bueso } 709d36a7a0dSDavidlohr Bueso EXPORT_SYMBOL_GPL(torture_cleanup_begin); 710d36a7a0dSDavidlohr Bueso 711d36a7a0dSDavidlohr Bueso void torture_cleanup_end(void) 712d36a7a0dSDavidlohr Bueso { 7135228084eSPaul E. McKenney mutex_lock(&fullstop_mutex); 7145228084eSPaul E. McKenney torture_type = NULL; 7155228084eSPaul E. McKenney mutex_unlock(&fullstop_mutex); 716cc47ae08SPaul E. McKenney } 717d36a7a0dSDavidlohr Bueso EXPORT_SYMBOL_GPL(torture_cleanup_end); 71836970bb9SPaul E. McKenney 71936970bb9SPaul E. McKenney /* 72036970bb9SPaul E. McKenney * Is it time for the current torture test to stop? 72136970bb9SPaul E. McKenney */ 72236970bb9SPaul E. McKenney bool torture_must_stop(void) 72336970bb9SPaul E. McKenney { 72436970bb9SPaul E. McKenney return torture_must_stop_irq() || kthread_should_stop(); 72536970bb9SPaul E. McKenney } 72636970bb9SPaul E. McKenney EXPORT_SYMBOL_GPL(torture_must_stop); 72736970bb9SPaul E. McKenney 72836970bb9SPaul E. McKenney /* 72936970bb9SPaul E. McKenney * Is it time for the current torture test to stop? This is the irq-safe 73036970bb9SPaul E. McKenney * version, hence no check for kthread_should_stop(). 73136970bb9SPaul E. McKenney */ 73236970bb9SPaul E. McKenney bool torture_must_stop_irq(void) 73336970bb9SPaul E. McKenney { 7347d0ae808SPaul E. McKenney return READ_ONCE(fullstop) != FULLSTOP_DONTSTOP; 73536970bb9SPaul E. McKenney } 73636970bb9SPaul E. McKenney EXPORT_SYMBOL_GPL(torture_must_stop_irq); 7377fafaac5SPaul E. McKenney 7387fafaac5SPaul E. McKenney /* 7397fafaac5SPaul E. McKenney * Each kthread must wait for kthread_should_stop() before returning from 7407fafaac5SPaul E. McKenney * its top-level function, otherwise segfaults ensue. This function 7417fafaac5SPaul E. McKenney * prints a "stopping" message and waits for kthread_should_stop(), and 7427fafaac5SPaul E. McKenney * should be called from all torture kthreads immediately prior to 7437fafaac5SPaul E. McKenney * returning. 7447fafaac5SPaul E. McKenney */ 7457fafaac5SPaul E. McKenney void torture_kthread_stopping(char *title) 7467fafaac5SPaul E. McKenney { 7470d6821d5SPaul E. McKenney char buf[128]; 7480d6821d5SPaul E. McKenney 7490d6821d5SPaul E. McKenney snprintf(buf, sizeof(buf), "Stopping %s", title); 7500d6821d5SPaul E. McKenney VERBOSE_TOROUT_STRING(buf); 7517fafaac5SPaul E. McKenney while (!kthread_should_stop()) { 7527fafaac5SPaul E. McKenney torture_shutdown_absorb(title); 7537fafaac5SPaul E. McKenney schedule_timeout_uninterruptible(1); 7547fafaac5SPaul E. McKenney } 7557fafaac5SPaul E. McKenney } 7567fafaac5SPaul E. McKenney EXPORT_SYMBOL_GPL(torture_kthread_stopping); 75747cf29b9SPaul E. McKenney 75847cf29b9SPaul E. McKenney /* 75947cf29b9SPaul E. McKenney * Create a generic torture kthread that is immediately runnable. If you 76047cf29b9SPaul E. McKenney * need the kthread to be stopped so that you can do something to it before 76147cf29b9SPaul E. McKenney * it starts, you will need to open-code your own. 76247cf29b9SPaul E. McKenney */ 76347cf29b9SPaul E. McKenney int _torture_create_kthread(int (*fn)(void *arg), void *arg, char *s, char *m, 76447cf29b9SPaul E. McKenney char *f, struct task_struct **tp) 76547cf29b9SPaul E. McKenney { 76647cf29b9SPaul E. McKenney int ret = 0; 76747cf29b9SPaul E. McKenney 76847cf29b9SPaul E. McKenney VERBOSE_TOROUT_STRING(m); 7696945915eSKees Cook *tp = kthread_run(fn, arg, "%s", s); 77047cf29b9SPaul E. McKenney if (IS_ERR(*tp)) { 77147cf29b9SPaul E. McKenney ret = PTR_ERR(*tp); 77247cf29b9SPaul E. McKenney VERBOSE_TOROUT_ERRSTRING(f); 77347cf29b9SPaul E. McKenney *tp = NULL; 77447cf29b9SPaul E. McKenney } 77547cf29b9SPaul E. McKenney torture_shuffle_task_register(*tp); 77647cf29b9SPaul E. McKenney return ret; 77747cf29b9SPaul E. McKenney } 77847cf29b9SPaul E. McKenney EXPORT_SYMBOL_GPL(_torture_create_kthread); 7799c029b86SPaul E. McKenney 7809c029b86SPaul E. McKenney /* 7819c029b86SPaul E. McKenney * Stop a generic kthread, emitting a message. 7829c029b86SPaul E. McKenney */ 7839c029b86SPaul E. McKenney void _torture_stop_kthread(char *m, struct task_struct **tp) 7849c029b86SPaul E. McKenney { 7859c029b86SPaul E. McKenney if (*tp == NULL) 7869c029b86SPaul E. McKenney return; 7879c029b86SPaul E. McKenney VERBOSE_TOROUT_STRING(m); 7889c029b86SPaul E. McKenney kthread_stop(*tp); 7899c029b86SPaul E. McKenney *tp = NULL; 7909c029b86SPaul E. McKenney } 7919c029b86SPaul E. McKenney EXPORT_SYMBOL_GPL(_torture_stop_kthread); 792