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 */ 2360500037SPaul E. McKenney 2460500037SPaul E. McKenney #define pr_fmt(fmt) fmt 2560500037SPaul E. McKenney 2651b1130eSPaul E. McKenney #include <linux/types.h> 2751b1130eSPaul E. McKenney #include <linux/kernel.h> 2851b1130eSPaul E. McKenney #include <linux/init.h> 2951b1130eSPaul E. McKenney #include <linux/module.h> 3051b1130eSPaul E. McKenney #include <linux/kthread.h> 3151b1130eSPaul E. McKenney #include <linux/err.h> 3251b1130eSPaul E. McKenney #include <linux/spinlock.h> 3351b1130eSPaul E. McKenney #include <linux/smp.h> 3451b1130eSPaul E. McKenney #include <linux/interrupt.h> 3551b1130eSPaul E. McKenney #include <linux/sched.h> 36e6017571SIngo Molnar #include <linux/sched/clock.h> 3751b1130eSPaul E. McKenney #include <linux/atomic.h> 3851b1130eSPaul E. McKenney #include <linux/bitops.h> 3951b1130eSPaul E. McKenney #include <linux/completion.h> 4051b1130eSPaul E. McKenney #include <linux/moduleparam.h> 4151b1130eSPaul E. McKenney #include <linux/percpu.h> 4251b1130eSPaul E. McKenney #include <linux/notifier.h> 4351b1130eSPaul E. McKenney #include <linux/reboot.h> 4451b1130eSPaul E. McKenney #include <linux/freezer.h> 4551b1130eSPaul E. McKenney #include <linux/cpu.h> 4651b1130eSPaul E. McKenney #include <linux/delay.h> 4751b1130eSPaul E. McKenney #include <linux/stat.h> 4851b1130eSPaul E. McKenney #include <linux/slab.h> 4951b1130eSPaul E. McKenney #include <linux/trace_clock.h> 5031257c3cSPaul E. McKenney #include <linux/ktime.h> 5151b1130eSPaul E. McKenney #include <asm/byteorder.h> 5251b1130eSPaul E. McKenney #include <linux/torture.h> 53dac95906SPaul E. McKenney #include "rcu/rcu.h" 5451b1130eSPaul E. McKenney 5551b1130eSPaul E. McKenney MODULE_LICENSE("GPL"); 5651b1130eSPaul E. McKenney MODULE_AUTHOR("Paul E. McKenney <paulmck@us.ibm.com>"); 5751b1130eSPaul E. McKenney 58b5daa8f3SPaul E. McKenney static char *torture_type; 5990127d60SPaul E. McKenney static int verbose; 60b5daa8f3SPaul E. McKenney 6136970bb9SPaul E. McKenney /* Mediate rmmod and system shutdown. Concurrent rmmod & shutdown illegal! */ 6236970bb9SPaul E. McKenney #define FULLSTOP_DONTSTOP 0 /* Normal operation. */ 6336970bb9SPaul E. McKenney #define FULLSTOP_SHUTDOWN 1 /* System shutdown with torture running. */ 6436970bb9SPaul E. McKenney #define FULLSTOP_RMMOD 2 /* Normal rmmod of torture. */ 6536970bb9SPaul E. McKenney static int fullstop = FULLSTOP_RMMOD; 664622b487SPaul E. McKenney static DEFINE_MUTEX(fullstop_mutex); 67f67a3356SPaul E. McKenney 682e9e8081SPaul E. McKenney #ifdef CONFIG_HOTPLUG_CPU 692e9e8081SPaul E. McKenney 702e9e8081SPaul E. McKenney /* 712e9e8081SPaul E. McKenney * Variables for online-offline handling. Only present if CPU hotplug 722e9e8081SPaul E. McKenney * is enabled, otherwise does nothing. 732e9e8081SPaul E. McKenney */ 742e9e8081SPaul E. McKenney 752e9e8081SPaul E. McKenney static struct task_struct *onoff_task; 762e9e8081SPaul E. McKenney static long onoff_holdoff; 772e9e8081SPaul E. McKenney static long onoff_interval; 782e9e8081SPaul E. McKenney static long n_offline_attempts; 792e9e8081SPaul E. McKenney static long n_offline_successes; 802e9e8081SPaul E. McKenney static unsigned long sum_offline; 812e9e8081SPaul E. McKenney static int min_offline = -1; 822e9e8081SPaul E. McKenney static int max_offline; 832e9e8081SPaul E. McKenney static long n_online_attempts; 842e9e8081SPaul E. McKenney static long n_online_successes; 852e9e8081SPaul E. McKenney static unsigned long sum_online; 862e9e8081SPaul E. McKenney static int min_online = -1; 872e9e8081SPaul E. McKenney static int max_online; 882e9e8081SPaul E. McKenney 892e9e8081SPaul E. McKenney /* 90d95f5ba9SPaul E. McKenney * Attempt to take a CPU offline. Return false if the CPU is already 91d95f5ba9SPaul E. McKenney * offline or if it is not subject to CPU-hotplug operations. The 92d95f5ba9SPaul E. McKenney * caller can detect other failures by looking at the statistics. 93d95f5ba9SPaul E. McKenney */ 94d95f5ba9SPaul E. McKenney bool torture_offline(int cpu, long *n_offl_attempts, long *n_offl_successes, 95d95f5ba9SPaul E. McKenney unsigned long *sum_offl, int *min_offl, int *max_offl) 96d95f5ba9SPaul E. McKenney { 97d95f5ba9SPaul E. McKenney unsigned long delta; 98d95f5ba9SPaul E. McKenney int ret; 99d95f5ba9SPaul E. McKenney unsigned long starttime; 100d95f5ba9SPaul E. McKenney 101d95f5ba9SPaul E. McKenney if (!cpu_online(cpu) || !cpu_is_hotpluggable(cpu)) 102d95f5ba9SPaul E. McKenney return false; 103d95f5ba9SPaul E. McKenney 10490127d60SPaul E. McKenney if (verbose > 1) 105d95f5ba9SPaul E. McKenney pr_alert("%s" TORTURE_FLAG 106d95f5ba9SPaul E. McKenney "torture_onoff task: offlining %d\n", 107d95f5ba9SPaul E. McKenney torture_type, cpu); 108d95f5ba9SPaul E. McKenney starttime = jiffies; 109d95f5ba9SPaul E. McKenney (*n_offl_attempts)++; 110d95f5ba9SPaul E. McKenney ret = cpu_down(cpu); 111d95f5ba9SPaul E. McKenney if (ret) { 112d95f5ba9SPaul E. McKenney if (verbose) 113d95f5ba9SPaul E. McKenney pr_alert("%s" TORTURE_FLAG 114d95f5ba9SPaul E. McKenney "torture_onoff task: offline %d failed: errno %d\n", 115d95f5ba9SPaul E. McKenney torture_type, cpu, ret); 116d95f5ba9SPaul E. McKenney } else { 11790127d60SPaul E. McKenney if (verbose > 1) 118d95f5ba9SPaul E. McKenney pr_alert("%s" TORTURE_FLAG 119d95f5ba9SPaul E. McKenney "torture_onoff task: offlined %d\n", 120d95f5ba9SPaul E. McKenney torture_type, cpu); 121d95f5ba9SPaul E. McKenney (*n_offl_successes)++; 122d95f5ba9SPaul E. McKenney delta = jiffies - starttime; 123a2b2df20SPaul E. McKenney *sum_offl += delta; 124d95f5ba9SPaul E. McKenney if (*min_offl < 0) { 125d95f5ba9SPaul E. McKenney *min_offl = delta; 126d95f5ba9SPaul E. McKenney *max_offl = delta; 127d95f5ba9SPaul E. McKenney } 128d95f5ba9SPaul E. McKenney if (*min_offl > delta) 129d95f5ba9SPaul E. McKenney *min_offl = delta; 130d95f5ba9SPaul E. McKenney if (*max_offl < delta) 131d95f5ba9SPaul E. McKenney *max_offl = delta; 132d95f5ba9SPaul E. McKenney } 133d95f5ba9SPaul E. McKenney 134d95f5ba9SPaul E. McKenney return true; 135d95f5ba9SPaul E. McKenney } 136d95f5ba9SPaul E. McKenney EXPORT_SYMBOL_GPL(torture_offline); 137d95f5ba9SPaul E. McKenney 138d95f5ba9SPaul E. McKenney /* 139d95f5ba9SPaul E. McKenney * Attempt to bring a CPU online. Return false if the CPU is already 140d95f5ba9SPaul E. McKenney * online or if it is not subject to CPU-hotplug operations. The 141d95f5ba9SPaul E. McKenney * caller can detect other failures by looking at the statistics. 142d95f5ba9SPaul E. McKenney */ 143d95f5ba9SPaul E. McKenney bool torture_online(int cpu, long *n_onl_attempts, long *n_onl_successes, 144d95f5ba9SPaul E. McKenney unsigned long *sum_onl, int *min_onl, int *max_onl) 145d95f5ba9SPaul E. McKenney { 146d95f5ba9SPaul E. McKenney unsigned long delta; 147d95f5ba9SPaul E. McKenney int ret; 148d95f5ba9SPaul E. McKenney unsigned long starttime; 149d95f5ba9SPaul E. McKenney 150d95f5ba9SPaul E. McKenney if (cpu_online(cpu) || !cpu_is_hotpluggable(cpu)) 151d95f5ba9SPaul E. McKenney return false; 152d95f5ba9SPaul E. McKenney 15390127d60SPaul E. McKenney if (verbose > 1) 154d95f5ba9SPaul E. McKenney pr_alert("%s" TORTURE_FLAG 155d95f5ba9SPaul E. McKenney "torture_onoff task: onlining %d\n", 156d95f5ba9SPaul E. McKenney torture_type, cpu); 157d95f5ba9SPaul E. McKenney starttime = jiffies; 158d95f5ba9SPaul E. McKenney (*n_onl_attempts)++; 159d95f5ba9SPaul E. McKenney ret = cpu_up(cpu); 160d95f5ba9SPaul E. McKenney if (ret) { 161d95f5ba9SPaul E. McKenney if (verbose) 162d95f5ba9SPaul E. McKenney pr_alert("%s" TORTURE_FLAG 163d95f5ba9SPaul E. McKenney "torture_onoff task: online %d failed: errno %d\n", 164d95f5ba9SPaul E. McKenney torture_type, cpu, ret); 165d95f5ba9SPaul E. McKenney } else { 16690127d60SPaul E. McKenney if (verbose > 1) 167d95f5ba9SPaul E. McKenney pr_alert("%s" TORTURE_FLAG 168d95f5ba9SPaul E. McKenney "torture_onoff task: onlined %d\n", 169d95f5ba9SPaul E. McKenney torture_type, cpu); 170d95f5ba9SPaul E. McKenney (*n_onl_successes)++; 171d95f5ba9SPaul E. McKenney delta = jiffies - starttime; 172d95f5ba9SPaul E. McKenney *sum_onl += delta; 173d95f5ba9SPaul E. McKenney if (*min_onl < 0) { 174d95f5ba9SPaul E. McKenney *min_onl = delta; 175d95f5ba9SPaul E. McKenney *max_onl = delta; 176d95f5ba9SPaul E. McKenney } 177d95f5ba9SPaul E. McKenney if (*min_onl > delta) 178d95f5ba9SPaul E. McKenney *min_onl = delta; 179d95f5ba9SPaul E. McKenney if (*max_onl < delta) 180d95f5ba9SPaul E. McKenney *max_onl = delta; 181d95f5ba9SPaul E. McKenney } 182d95f5ba9SPaul E. McKenney 183d95f5ba9SPaul E. McKenney return true; 184d95f5ba9SPaul E. McKenney } 185d95f5ba9SPaul E. McKenney EXPORT_SYMBOL_GPL(torture_online); 186d95f5ba9SPaul E. McKenney 187d95f5ba9SPaul E. McKenney /* 1882e9e8081SPaul E. McKenney * Execute random CPU-hotplug operations at the interval specified 1892e9e8081SPaul E. McKenney * by the onoff_interval. 1902e9e8081SPaul E. McKenney */ 1912e9e8081SPaul E. McKenney static int 1922e9e8081SPaul E. McKenney torture_onoff(void *arg) 1932e9e8081SPaul E. McKenney { 1942e9e8081SPaul E. McKenney int cpu; 1952e9e8081SPaul E. McKenney int maxcpu = -1; 1962e9e8081SPaul E. McKenney DEFINE_TORTURE_RANDOM(rand); 19728cf5952SPaul E. McKenney int ret; 1982e9e8081SPaul E. McKenney 1992e9e8081SPaul E. McKenney VERBOSE_TOROUT_STRING("torture_onoff task started"); 2002e9e8081SPaul E. McKenney for_each_online_cpu(cpu) 2012e9e8081SPaul E. McKenney maxcpu = cpu; 2022e9e8081SPaul E. McKenney WARN_ON(maxcpu < 0); 20328cf5952SPaul E. McKenney if (!IS_MODULE(CONFIG_TORTURE_TEST)) 20428cf5952SPaul E. McKenney for_each_possible_cpu(cpu) { 20528cf5952SPaul E. McKenney if (cpu_online(cpu)) 20628cf5952SPaul E. McKenney continue; 20728cf5952SPaul E. McKenney ret = cpu_up(cpu); 20828cf5952SPaul E. McKenney if (ret && verbose) { 20928cf5952SPaul E. McKenney pr_alert("%s" TORTURE_FLAG 21028cf5952SPaul E. McKenney "%s: Initial online %d: errno %d\n", 21128cf5952SPaul E. McKenney __func__, torture_type, cpu, ret); 21228cf5952SPaul E. McKenney } 21328cf5952SPaul E. McKenney } 214750db0f5SBoqun Feng 215750db0f5SBoqun Feng if (maxcpu == 0) { 216750db0f5SBoqun Feng VERBOSE_TOROUT_STRING("Only one CPU, so CPU-hotplug testing is disabled"); 217750db0f5SBoqun Feng goto stop; 218750db0f5SBoqun Feng } 219750db0f5SBoqun Feng 2202e9e8081SPaul E. McKenney if (onoff_holdoff > 0) { 2212e9e8081SPaul E. McKenney VERBOSE_TOROUT_STRING("torture_onoff begin holdoff"); 2222e9e8081SPaul E. McKenney schedule_timeout_interruptible(onoff_holdoff); 2232e9e8081SPaul E. McKenney VERBOSE_TOROUT_STRING("torture_onoff end holdoff"); 2242e9e8081SPaul E. McKenney } 2252e9e8081SPaul E. McKenney while (!torture_must_stop()) { 2262e9e8081SPaul E. McKenney cpu = (torture_random(&rand) >> 4) % (maxcpu + 1); 227d95f5ba9SPaul E. McKenney if (!torture_offline(cpu, 228d95f5ba9SPaul E. McKenney &n_offline_attempts, &n_offline_successes, 229d95f5ba9SPaul E. McKenney &sum_offline, &min_offline, &max_offline)) 230d95f5ba9SPaul E. McKenney torture_online(cpu, 231d95f5ba9SPaul E. McKenney &n_online_attempts, &n_online_successes, 232d95f5ba9SPaul E. McKenney &sum_online, &min_online, &max_online); 2332e9e8081SPaul E. McKenney schedule_timeout_interruptible(onoff_interval); 2342e9e8081SPaul E. McKenney } 235750db0f5SBoqun Feng 236750db0f5SBoqun Feng stop: 2377fafaac5SPaul E. McKenney torture_kthread_stopping("torture_onoff"); 2382e9e8081SPaul E. McKenney return 0; 2392e9e8081SPaul E. McKenney } 2402e9e8081SPaul E. McKenney 2412e9e8081SPaul E. McKenney #endif /* #ifdef CONFIG_HOTPLUG_CPU */ 2422e9e8081SPaul E. McKenney 2432e9e8081SPaul E. McKenney /* 2442e9e8081SPaul E. McKenney * Initiate online-offline handling. 2452e9e8081SPaul E. McKenney */ 2462e9e8081SPaul E. McKenney int torture_onoff_init(long ooholdoff, long oointerval) 2472e9e8081SPaul E. McKenney { 24847cf29b9SPaul E. McKenney #ifdef CONFIG_HOTPLUG_CPU 2492e9e8081SPaul E. McKenney onoff_holdoff = ooholdoff; 2502e9e8081SPaul E. McKenney onoff_interval = oointerval; 2512e9e8081SPaul E. McKenney if (onoff_interval <= 0) 2522e9e8081SPaul E. McKenney return 0; 253*2a7d9688SPierce Griffiths return torture_create_kthread(torture_onoff, NULL, onoff_task); 254*2a7d9688SPierce Griffiths #else /* #ifdef CONFIG_HOTPLUG_CPU */ 255*2a7d9688SPierce Griffiths return 0; 256*2a7d9688SPierce Griffiths #endif /* #else #ifdef CONFIG_HOTPLUG_CPU */ 2572e9e8081SPaul E. McKenney } 2582e9e8081SPaul E. McKenney EXPORT_SYMBOL_GPL(torture_onoff_init); 2592e9e8081SPaul E. McKenney 2602e9e8081SPaul E. McKenney /* 2612e9e8081SPaul E. McKenney * Clean up after online/offline testing. 2622e9e8081SPaul E. McKenney */ 263cc47ae08SPaul E. McKenney static void torture_onoff_cleanup(void) 2642e9e8081SPaul E. McKenney { 2652e9e8081SPaul E. McKenney #ifdef CONFIG_HOTPLUG_CPU 2662e9e8081SPaul E. McKenney if (onoff_task == NULL) 2672e9e8081SPaul E. McKenney return; 2682e9e8081SPaul E. McKenney VERBOSE_TOROUT_STRING("Stopping torture_onoff task"); 2692e9e8081SPaul E. McKenney kthread_stop(onoff_task); 2702e9e8081SPaul E. McKenney onoff_task = NULL; 2712e9e8081SPaul E. McKenney #endif /* #ifdef CONFIG_HOTPLUG_CPU */ 2722e9e8081SPaul E. McKenney } 2732e9e8081SPaul E. McKenney EXPORT_SYMBOL_GPL(torture_onoff_cleanup); 2742e9e8081SPaul E. McKenney 2752e9e8081SPaul E. McKenney /* 2762e9e8081SPaul E. McKenney * Print online/offline testing statistics. 2772e9e8081SPaul E. McKenney */ 278eea203feSJoe Perches void torture_onoff_stats(void) 2792e9e8081SPaul E. McKenney { 2802e9e8081SPaul E. McKenney #ifdef CONFIG_HOTPLUG_CPU 281eea203feSJoe Perches pr_cont("onoff: %ld/%ld:%ld/%ld %d,%d:%d,%d %lu:%lu (HZ=%d) ", 2822e9e8081SPaul E. McKenney n_online_successes, n_online_attempts, 2832e9e8081SPaul E. McKenney n_offline_successes, n_offline_attempts, 2842e9e8081SPaul E. McKenney min_online, max_online, 2852e9e8081SPaul E. McKenney min_offline, max_offline, 2862e9e8081SPaul E. McKenney sum_online, sum_offline, HZ); 2872e9e8081SPaul E. McKenney #endif /* #ifdef CONFIG_HOTPLUG_CPU */ 2882e9e8081SPaul E. McKenney } 2892e9e8081SPaul E. McKenney EXPORT_SYMBOL_GPL(torture_onoff_stats); 2902e9e8081SPaul E. McKenney 2912e9e8081SPaul E. McKenney /* 2922e9e8081SPaul E. McKenney * Were all the online/offline operations successful? 2932e9e8081SPaul E. McKenney */ 2942e9e8081SPaul E. McKenney bool torture_onoff_failures(void) 2952e9e8081SPaul E. McKenney { 2962e9e8081SPaul E. McKenney #ifdef CONFIG_HOTPLUG_CPU 2972e9e8081SPaul E. McKenney return n_online_successes != n_online_attempts || 2982e9e8081SPaul E. McKenney n_offline_successes != n_offline_attempts; 2992e9e8081SPaul E. McKenney #else /* #ifdef CONFIG_HOTPLUG_CPU */ 3002e9e8081SPaul E. McKenney return false; 3012e9e8081SPaul E. McKenney #endif /* #else #ifdef CONFIG_HOTPLUG_CPU */ 3022e9e8081SPaul E. McKenney } 3032e9e8081SPaul E. McKenney EXPORT_SYMBOL_GPL(torture_onoff_failures); 3042e9e8081SPaul E. McKenney 30551b1130eSPaul E. McKenney #define TORTURE_RANDOM_MULT 39916801 /* prime */ 30651b1130eSPaul E. McKenney #define TORTURE_RANDOM_ADD 479001701 /* prime */ 30751b1130eSPaul E. McKenney #define TORTURE_RANDOM_REFRESH 10000 30851b1130eSPaul E. McKenney 30951b1130eSPaul E. McKenney /* 31051b1130eSPaul E. McKenney * Crude but fast random-number generator. Uses a linear congruential 31151b1130eSPaul E. McKenney * generator, with occasional help from cpu_clock(). 31251b1130eSPaul E. McKenney */ 31351b1130eSPaul E. McKenney unsigned long 31451b1130eSPaul E. McKenney torture_random(struct torture_random_state *trsp) 31551b1130eSPaul E. McKenney { 31651b1130eSPaul E. McKenney if (--trsp->trs_count < 0) { 31751b1130eSPaul E. McKenney trsp->trs_state += (unsigned long)local_clock(); 31851b1130eSPaul E. McKenney trsp->trs_count = TORTURE_RANDOM_REFRESH; 31951b1130eSPaul E. McKenney } 32051b1130eSPaul E. McKenney trsp->trs_state = trsp->trs_state * TORTURE_RANDOM_MULT + 32151b1130eSPaul E. McKenney TORTURE_RANDOM_ADD; 32251b1130eSPaul E. McKenney return swahw32(trsp->trs_state); 32351b1130eSPaul E. McKenney } 32451b1130eSPaul E. McKenney EXPORT_SYMBOL_GPL(torture_random); 325f67a3356SPaul E. McKenney 326f67a3356SPaul E. McKenney /* 3273808dc9fSPaul E. McKenney * Variables for shuffling. The idea is to ensure that each CPU stays 3283808dc9fSPaul E. McKenney * idle for an extended period to test interactions with dyntick idle, 329b564d62eSMasahiro Yamada * as well as interactions with any per-CPU variables. 3303808dc9fSPaul E. McKenney */ 3313808dc9fSPaul E. McKenney struct shuffle_task { 3323808dc9fSPaul E. McKenney struct list_head st_l; 3333808dc9fSPaul E. McKenney struct task_struct *st_t; 3343808dc9fSPaul E. McKenney }; 3353808dc9fSPaul E. McKenney 3363808dc9fSPaul E. McKenney static long shuffle_interval; /* In jiffies. */ 3373808dc9fSPaul E. McKenney static struct task_struct *shuffler_task; 3383808dc9fSPaul E. McKenney static cpumask_var_t shuffle_tmp_mask; 3393808dc9fSPaul E. McKenney static int shuffle_idle_cpu; /* Force all torture tasks off this CPU */ 3403808dc9fSPaul E. McKenney static struct list_head shuffle_task_list = LIST_HEAD_INIT(shuffle_task_list); 3413808dc9fSPaul E. McKenney static DEFINE_MUTEX(shuffle_task_mutex); 3423808dc9fSPaul E. McKenney 3433808dc9fSPaul E. McKenney /* 3443808dc9fSPaul E. McKenney * Register a task to be shuffled. If there is no memory, just splat 3453808dc9fSPaul E. McKenney * and don't bother registering. 3463808dc9fSPaul E. McKenney */ 3473808dc9fSPaul E. McKenney void torture_shuffle_task_register(struct task_struct *tp) 3483808dc9fSPaul E. McKenney { 3493808dc9fSPaul E. McKenney struct shuffle_task *stp; 3503808dc9fSPaul E. McKenney 3513808dc9fSPaul E. McKenney if (WARN_ON_ONCE(tp == NULL)) 3523808dc9fSPaul E. McKenney return; 3533808dc9fSPaul E. McKenney stp = kmalloc(sizeof(*stp), GFP_KERNEL); 3543808dc9fSPaul E. McKenney if (WARN_ON_ONCE(stp == NULL)) 3553808dc9fSPaul E. McKenney return; 3563808dc9fSPaul E. McKenney stp->st_t = tp; 3573808dc9fSPaul E. McKenney mutex_lock(&shuffle_task_mutex); 3583808dc9fSPaul E. McKenney list_add(&stp->st_l, &shuffle_task_list); 3593808dc9fSPaul E. McKenney mutex_unlock(&shuffle_task_mutex); 3603808dc9fSPaul E. McKenney } 3613808dc9fSPaul E. McKenney EXPORT_SYMBOL_GPL(torture_shuffle_task_register); 3623808dc9fSPaul E. McKenney 3633808dc9fSPaul E. McKenney /* 3643808dc9fSPaul E. McKenney * Unregister all tasks, for example, at the end of the torture run. 3653808dc9fSPaul E. McKenney */ 3663808dc9fSPaul E. McKenney static void torture_shuffle_task_unregister_all(void) 3673808dc9fSPaul E. McKenney { 3683808dc9fSPaul E. McKenney struct shuffle_task *stp; 3693808dc9fSPaul E. McKenney struct shuffle_task *p; 3703808dc9fSPaul E. McKenney 3713808dc9fSPaul E. McKenney mutex_lock(&shuffle_task_mutex); 3723808dc9fSPaul E. McKenney list_for_each_entry_safe(stp, p, &shuffle_task_list, st_l) { 3733808dc9fSPaul E. McKenney list_del(&stp->st_l); 3743808dc9fSPaul E. McKenney kfree(stp); 3753808dc9fSPaul E. McKenney } 3763808dc9fSPaul E. McKenney mutex_unlock(&shuffle_task_mutex); 3773808dc9fSPaul E. McKenney } 3783808dc9fSPaul E. McKenney 3793808dc9fSPaul E. McKenney /* Shuffle tasks such that we allow shuffle_idle_cpu to become idle. 3803808dc9fSPaul E. McKenney * A special case is when shuffle_idle_cpu = -1, in which case we allow 3813808dc9fSPaul E. McKenney * the tasks to run on all CPUs. 3823808dc9fSPaul E. McKenney */ 3833808dc9fSPaul E. McKenney static void torture_shuffle_tasks(void) 3843808dc9fSPaul E. McKenney { 3853808dc9fSPaul E. McKenney struct shuffle_task *stp; 3863808dc9fSPaul E. McKenney 3873808dc9fSPaul E. McKenney cpumask_setall(shuffle_tmp_mask); 3883808dc9fSPaul E. McKenney get_online_cpus(); 3893808dc9fSPaul E. McKenney 3903808dc9fSPaul E. McKenney /* No point in shuffling if there is only one online CPU (ex: UP) */ 3913808dc9fSPaul E. McKenney if (num_online_cpus() == 1) { 3923808dc9fSPaul E. McKenney put_online_cpus(); 3933808dc9fSPaul E. McKenney return; 3943808dc9fSPaul E. McKenney } 3953808dc9fSPaul E. McKenney 3963808dc9fSPaul E. McKenney /* Advance to the next CPU. Upon overflow, don't idle any CPUs. */ 3973808dc9fSPaul E. McKenney shuffle_idle_cpu = cpumask_next(shuffle_idle_cpu, shuffle_tmp_mask); 3983808dc9fSPaul E. McKenney if (shuffle_idle_cpu >= nr_cpu_ids) 3993808dc9fSPaul E. McKenney shuffle_idle_cpu = -1; 4005ed63b19SIulia Manda else 4013808dc9fSPaul E. McKenney cpumask_clear_cpu(shuffle_idle_cpu, shuffle_tmp_mask); 4023808dc9fSPaul E. McKenney 4033808dc9fSPaul E. McKenney mutex_lock(&shuffle_task_mutex); 4043808dc9fSPaul E. McKenney list_for_each_entry(stp, &shuffle_task_list, st_l) 4053808dc9fSPaul E. McKenney set_cpus_allowed_ptr(stp->st_t, shuffle_tmp_mask); 4063808dc9fSPaul E. McKenney mutex_unlock(&shuffle_task_mutex); 4073808dc9fSPaul E. McKenney 4083808dc9fSPaul E. McKenney put_online_cpus(); 4093808dc9fSPaul E. McKenney } 4103808dc9fSPaul E. McKenney 4113808dc9fSPaul E. McKenney /* Shuffle tasks across CPUs, with the intent of allowing each CPU in the 4123808dc9fSPaul E. McKenney * system to become idle at a time and cut off its timer ticks. This is meant 4133808dc9fSPaul E. McKenney * to test the support for such tickless idle CPU in RCU. 4143808dc9fSPaul E. McKenney */ 4153808dc9fSPaul E. McKenney static int torture_shuffle(void *arg) 4163808dc9fSPaul E. McKenney { 4173808dc9fSPaul E. McKenney VERBOSE_TOROUT_STRING("torture_shuffle task started"); 4183808dc9fSPaul E. McKenney do { 4193808dc9fSPaul E. McKenney schedule_timeout_interruptible(shuffle_interval); 4203808dc9fSPaul E. McKenney torture_shuffle_tasks(); 4213808dc9fSPaul E. McKenney torture_shutdown_absorb("torture_shuffle"); 4223808dc9fSPaul E. McKenney } while (!torture_must_stop()); 4237fafaac5SPaul E. McKenney torture_kthread_stopping("torture_shuffle"); 4243808dc9fSPaul E. McKenney return 0; 4253808dc9fSPaul E. McKenney } 4263808dc9fSPaul E. McKenney 4273808dc9fSPaul E. McKenney /* 4283808dc9fSPaul E. McKenney * Start the shuffler, with shuffint in jiffies. 4293808dc9fSPaul E. McKenney */ 4303808dc9fSPaul E. McKenney int torture_shuffle_init(long shuffint) 4313808dc9fSPaul E. McKenney { 4323808dc9fSPaul E. McKenney shuffle_interval = shuffint; 4333808dc9fSPaul E. McKenney 4343808dc9fSPaul E. McKenney shuffle_idle_cpu = -1; 4353808dc9fSPaul E. McKenney 4363808dc9fSPaul E. McKenney if (!alloc_cpumask_var(&shuffle_tmp_mask, GFP_KERNEL)) { 4373808dc9fSPaul E. McKenney VERBOSE_TOROUT_ERRSTRING("Failed to alloc mask"); 4383808dc9fSPaul E. McKenney return -ENOMEM; 4393808dc9fSPaul E. McKenney } 4403808dc9fSPaul E. McKenney 4413808dc9fSPaul E. McKenney /* Create the shuffler thread */ 44247cf29b9SPaul E. McKenney return torture_create_kthread(torture_shuffle, NULL, shuffler_task); 4433808dc9fSPaul E. McKenney } 4443808dc9fSPaul E. McKenney EXPORT_SYMBOL_GPL(torture_shuffle_init); 4453808dc9fSPaul E. McKenney 4463808dc9fSPaul E. McKenney /* 4473808dc9fSPaul E. McKenney * Stop the shuffling. 4483808dc9fSPaul E. McKenney */ 449cc47ae08SPaul E. McKenney static void torture_shuffle_cleanup(void) 4503808dc9fSPaul E. McKenney { 4513808dc9fSPaul E. McKenney torture_shuffle_task_unregister_all(); 4523808dc9fSPaul E. McKenney if (shuffler_task) { 4533808dc9fSPaul E. McKenney VERBOSE_TOROUT_STRING("Stopping torture_shuffle task"); 4543808dc9fSPaul E. McKenney kthread_stop(shuffler_task); 4553808dc9fSPaul E. McKenney free_cpumask_var(shuffle_tmp_mask); 4563808dc9fSPaul E. McKenney } 4573808dc9fSPaul E. McKenney shuffler_task = NULL; 4583808dc9fSPaul E. McKenney } 4593808dc9fSPaul E. McKenney EXPORT_SYMBOL_GPL(torture_shuffle_cleanup); 4603808dc9fSPaul E. McKenney 4613808dc9fSPaul E. McKenney /* 462e991dbc0SPaul E. McKenney * Variables for auto-shutdown. This allows "lights out" torture runs 463e991dbc0SPaul E. McKenney * to be fully scripted. 464e991dbc0SPaul E. McKenney */ 465e991dbc0SPaul E. McKenney static struct task_struct *shutdown_task; 46631257c3cSPaul E. McKenney static ktime_t shutdown_time; /* time to system shutdown. */ 467e991dbc0SPaul E. McKenney static void (*torture_shutdown_hook)(void); 468e991dbc0SPaul E. McKenney 469e991dbc0SPaul E. McKenney /* 470f67a3356SPaul E. McKenney * Absorb kthreads into a kernel function that won't return, so that 471f67a3356SPaul E. McKenney * they won't ever access module text or data again. 472f67a3356SPaul E. McKenney */ 473f67a3356SPaul E. McKenney void torture_shutdown_absorb(const char *title) 474f67a3356SPaul E. McKenney { 4757d0ae808SPaul E. McKenney while (READ_ONCE(fullstop) == FULLSTOP_SHUTDOWN) { 4764622b487SPaul E. McKenney pr_notice("torture thread %s parking due to system shutdown\n", 477f67a3356SPaul E. McKenney title); 478f67a3356SPaul E. McKenney schedule_timeout_uninterruptible(MAX_SCHEDULE_TIMEOUT); 479f67a3356SPaul E. McKenney } 480f67a3356SPaul E. McKenney } 481f67a3356SPaul E. McKenney EXPORT_SYMBOL_GPL(torture_shutdown_absorb); 482b5daa8f3SPaul E. McKenney 483b5daa8f3SPaul E. McKenney /* 484e991dbc0SPaul E. McKenney * Cause the torture test to shutdown the system after the test has 485e991dbc0SPaul E. McKenney * run for the time specified by the shutdown_secs parameter. 486e991dbc0SPaul E. McKenney */ 487e991dbc0SPaul E. McKenney static int torture_shutdown(void *arg) 488e991dbc0SPaul E. McKenney { 48931257c3cSPaul E. McKenney ktime_t ktime_snap; 490e991dbc0SPaul E. McKenney 491e991dbc0SPaul E. McKenney VERBOSE_TOROUT_STRING("torture_shutdown task started"); 49231257c3cSPaul E. McKenney ktime_snap = ktime_get(); 49331257c3cSPaul E. McKenney while (ktime_before(ktime_snap, shutdown_time) && 494e991dbc0SPaul E. McKenney !torture_must_stop()) { 495e991dbc0SPaul E. McKenney if (verbose) 496e991dbc0SPaul E. McKenney pr_alert("%s" TORTURE_FLAG 49731257c3cSPaul E. McKenney "torture_shutdown task: %llu ms remaining\n", 49831257c3cSPaul E. McKenney torture_type, 49931257c3cSPaul E. McKenney ktime_ms_delta(shutdown_time, ktime_snap)); 50031257c3cSPaul E. McKenney set_current_state(TASK_INTERRUPTIBLE); 50131257c3cSPaul E. McKenney schedule_hrtimeout(&shutdown_time, HRTIMER_MODE_ABS); 50231257c3cSPaul E. McKenney ktime_snap = ktime_get(); 503e991dbc0SPaul E. McKenney } 504e991dbc0SPaul E. McKenney if (torture_must_stop()) { 5057fafaac5SPaul E. McKenney torture_kthread_stopping("torture_shutdown"); 506e991dbc0SPaul E. McKenney return 0; 507e991dbc0SPaul E. McKenney } 508e991dbc0SPaul E. McKenney 509e991dbc0SPaul E. McKenney /* OK, shut down the system. */ 510e991dbc0SPaul E. McKenney 511e991dbc0SPaul E. McKenney VERBOSE_TOROUT_STRING("torture_shutdown task shutting down system"); 512e991dbc0SPaul E. McKenney shutdown_task = NULL; /* Avoid self-kill deadlock. */ 513f881825aSPaul E. McKenney if (torture_shutdown_hook) 514f881825aSPaul E. McKenney torture_shutdown_hook(); 515f881825aSPaul E. McKenney else 516f881825aSPaul E. McKenney VERBOSE_TOROUT_STRING("No torture_shutdown_hook(), skipping."); 517dac95906SPaul E. McKenney rcu_ftrace_dump(DUMP_ALL); 518e991dbc0SPaul E. McKenney kernel_power_off(); /* Shut down the system. */ 519e991dbc0SPaul E. McKenney return 0; 520e991dbc0SPaul E. McKenney } 521e991dbc0SPaul E. McKenney 522e991dbc0SPaul E. McKenney /* 523e991dbc0SPaul E. McKenney * Start up the shutdown task. 524e991dbc0SPaul E. McKenney */ 525e991dbc0SPaul E. McKenney int torture_shutdown_init(int ssecs, void (*cleanup)(void)) 526e991dbc0SPaul E. McKenney { 527e991dbc0SPaul E. McKenney torture_shutdown_hook = cleanup; 52831257c3cSPaul E. McKenney if (ssecs > 0) { 52931257c3cSPaul E. McKenney shutdown_time = ktime_add(ktime_get(), ktime_set(ssecs, 0)); 530*2a7d9688SPierce Griffiths return torture_create_kthread(torture_shutdown, NULL, 53147cf29b9SPaul E. McKenney shutdown_task); 53247cf29b9SPaul E. McKenney } 533*2a7d9688SPierce Griffiths return 0; 534e991dbc0SPaul E. McKenney } 535e991dbc0SPaul E. McKenney EXPORT_SYMBOL_GPL(torture_shutdown_init); 536e991dbc0SPaul E. McKenney 537e991dbc0SPaul E. McKenney /* 5384622b487SPaul E. McKenney * Detect and respond to a system shutdown. 5394622b487SPaul E. McKenney */ 5404622b487SPaul E. McKenney static int torture_shutdown_notify(struct notifier_block *unused1, 5414622b487SPaul E. McKenney unsigned long unused2, void *unused3) 5424622b487SPaul E. McKenney { 5434622b487SPaul E. McKenney mutex_lock(&fullstop_mutex); 5447d0ae808SPaul E. McKenney if (READ_ONCE(fullstop) == FULLSTOP_DONTSTOP) { 545fac480efSPaul E. McKenney VERBOSE_TOROUT_STRING("Unscheduled system shutdown detected"); 5467d0ae808SPaul E. McKenney WRITE_ONCE(fullstop, FULLSTOP_SHUTDOWN); 547fac480efSPaul E. McKenney } else { 5484622b487SPaul E. McKenney pr_warn("Concurrent rmmod and shutdown illegal!\n"); 549fac480efSPaul E. McKenney } 5504622b487SPaul E. McKenney mutex_unlock(&fullstop_mutex); 5514622b487SPaul E. McKenney return NOTIFY_DONE; 5524622b487SPaul E. McKenney } 5534622b487SPaul E. McKenney 5544622b487SPaul E. McKenney static struct notifier_block torture_shutdown_nb = { 5554622b487SPaul E. McKenney .notifier_call = torture_shutdown_notify, 5564622b487SPaul E. McKenney }; 5574622b487SPaul E. McKenney 5584622b487SPaul E. McKenney /* 559bfefc73aSPaul E. McKenney * Shut down the shutdown task. Say what??? Heh! This can happen if 560bfefc73aSPaul E. McKenney * the torture module gets an rmmod before the shutdown time arrives. ;-) 561bfefc73aSPaul E. McKenney */ 562bfefc73aSPaul E. McKenney static void torture_shutdown_cleanup(void) 563bfefc73aSPaul E. McKenney { 564bfefc73aSPaul E. McKenney unregister_reboot_notifier(&torture_shutdown_nb); 565bfefc73aSPaul E. McKenney if (shutdown_task != NULL) { 566bfefc73aSPaul E. McKenney VERBOSE_TOROUT_STRING("Stopping torture_shutdown task"); 567bfefc73aSPaul E. McKenney kthread_stop(shutdown_task); 568bfefc73aSPaul E. McKenney } 569bfefc73aSPaul E. McKenney shutdown_task = NULL; 570bfefc73aSPaul E. McKenney } 571bfefc73aSPaul E. McKenney 572bfefc73aSPaul E. McKenney /* 573628edaa5SPaul E. McKenney * Variables for stuttering, which means to periodically pause and 574628edaa5SPaul E. McKenney * restart testing in order to catch bugs that appear when load is 575628edaa5SPaul E. McKenney * suddenly applied to or removed from the system. 576628edaa5SPaul E. McKenney */ 577628edaa5SPaul E. McKenney static struct task_struct *stutter_task; 578628edaa5SPaul E. McKenney static int stutter_pause_test; 579628edaa5SPaul E. McKenney static int stutter; 580628edaa5SPaul E. McKenney 581628edaa5SPaul E. McKenney /* 582628edaa5SPaul E. McKenney * Block until the stutter interval ends. This must be called periodically 583628edaa5SPaul E. McKenney * by all running kthreads that need to be subject to stuttering. 584628edaa5SPaul E. McKenney */ 585474e59b4SPaul E. McKenney bool stutter_wait(const char *title) 586628edaa5SPaul E. McKenney { 5874ced3314SPaul E. McKenney int spt; 5884ced3314SPaul E. McKenney 589cee43939SPaul E. McKenney cond_resched_tasks_rcu_qs(); 5904ced3314SPaul E. McKenney spt = READ_ONCE(stutter_pause_test); 59129d39390SPaul E. McKenney for (; spt; spt = READ_ONCE(stutter_pause_test)) { 5924ced3314SPaul E. McKenney if (spt == 1) { 593628edaa5SPaul E. McKenney schedule_timeout_interruptible(1); 5944ced3314SPaul E. McKenney } else if (spt == 2) { 5957d0ae808SPaul E. McKenney while (READ_ONCE(stutter_pause_test)) 596ab7d4505SPaul E. McKenney cond_resched(); 5974ced3314SPaul E. McKenney } else { 598628edaa5SPaul E. McKenney schedule_timeout_interruptible(round_jiffies_relative(HZ)); 5994ced3314SPaul E. McKenney } 600628edaa5SPaul E. McKenney torture_shutdown_absorb(title); 601628edaa5SPaul E. McKenney } 602474e59b4SPaul E. McKenney return !!spt; 603628edaa5SPaul E. McKenney } 604628edaa5SPaul E. McKenney EXPORT_SYMBOL_GPL(stutter_wait); 605628edaa5SPaul E. McKenney 606628edaa5SPaul E. McKenney /* 607628edaa5SPaul E. McKenney * Cause the torture test to "stutter", starting and stopping all 608628edaa5SPaul E. McKenney * threads periodically. 609628edaa5SPaul E. McKenney */ 610628edaa5SPaul E. McKenney static int torture_stutter(void *arg) 611628edaa5SPaul E. McKenney { 612628edaa5SPaul E. McKenney VERBOSE_TOROUT_STRING("torture_stutter task started"); 613628edaa5SPaul E. McKenney do { 6144ced3314SPaul E. McKenney if (!torture_must_stop() && stutter > 1) { 6154ced3314SPaul E. McKenney WRITE_ONCE(stutter_pause_test, 1); 616ab7d4505SPaul E. McKenney schedule_timeout_interruptible(stutter - 1); 6177d0ae808SPaul E. McKenney WRITE_ONCE(stutter_pause_test, 2); 618ab7d4505SPaul E. McKenney schedule_timeout_interruptible(1); 619628edaa5SPaul E. McKenney } 6204ced3314SPaul E. McKenney WRITE_ONCE(stutter_pause_test, 0); 621628edaa5SPaul E. McKenney if (!torture_must_stop()) 622628edaa5SPaul E. McKenney schedule_timeout_interruptible(stutter); 623628edaa5SPaul E. McKenney torture_shutdown_absorb("torture_stutter"); 624628edaa5SPaul E. McKenney } while (!torture_must_stop()); 6257fafaac5SPaul E. McKenney torture_kthread_stopping("torture_stutter"); 626628edaa5SPaul E. McKenney return 0; 627628edaa5SPaul E. McKenney } 628628edaa5SPaul E. McKenney 629628edaa5SPaul E. McKenney /* 630628edaa5SPaul E. McKenney * Initialize and kick off the torture_stutter kthread. 631628edaa5SPaul E. McKenney */ 632*2a7d9688SPierce Griffiths int torture_stutter_init(const int s) 633628edaa5SPaul E. McKenney { 634628edaa5SPaul E. McKenney stutter = s; 635*2a7d9688SPierce Griffiths return torture_create_kthread(torture_stutter, NULL, stutter_task); 636628edaa5SPaul E. McKenney } 637628edaa5SPaul E. McKenney EXPORT_SYMBOL_GPL(torture_stutter_init); 638628edaa5SPaul E. McKenney 639628edaa5SPaul E. McKenney /* 640628edaa5SPaul E. McKenney * Cleanup after the torture_stutter kthread. 641628edaa5SPaul E. McKenney */ 642bfefc73aSPaul E. McKenney static void torture_stutter_cleanup(void) 643628edaa5SPaul E. McKenney { 644628edaa5SPaul E. McKenney if (!stutter_task) 645628edaa5SPaul E. McKenney return; 646628edaa5SPaul E. McKenney VERBOSE_TOROUT_STRING("Stopping torture_stutter task"); 647628edaa5SPaul E. McKenney kthread_stop(stutter_task); 648628edaa5SPaul E. McKenney stutter_task = NULL; 649628edaa5SPaul E. McKenney } 650628edaa5SPaul E. McKenney 651628edaa5SPaul E. McKenney /* 652b5daa8f3SPaul E. McKenney * Initialize torture module. Please note that this is -not- invoked via 653b5daa8f3SPaul E. McKenney * the usual module_init() mechanism, but rather by an explicit call from 654b5daa8f3SPaul E. McKenney * the client torture module. This call must be paired with a later 655b5daa8f3SPaul E. McKenney * torture_init_end(). 656628edaa5SPaul E. McKenney * 657628edaa5SPaul E. McKenney * The runnable parameter points to a flag that controls whether or not 658628edaa5SPaul E. McKenney * the test is currently runnable. If there is no such flag, pass in NULL. 659b5daa8f3SPaul E. McKenney */ 66090127d60SPaul E. McKenney bool torture_init_begin(char *ttype, int v) 661b5daa8f3SPaul E. McKenney { 662b5daa8f3SPaul E. McKenney mutex_lock(&fullstop_mutex); 6635228084eSPaul E. McKenney if (torture_type != NULL) { 6649eb5188aSPaul E. McKenney pr_alert("torture_init_begin: Refusing %s init: %s running.\n", 6655228084eSPaul E. McKenney ttype, torture_type); 6669eb5188aSPaul E. McKenney pr_alert("torture_init_begin: One torture test at a time!\n"); 6675228084eSPaul E. McKenney mutex_unlock(&fullstop_mutex); 6685228084eSPaul E. McKenney return false; 6695228084eSPaul E. McKenney } 670b5daa8f3SPaul E. McKenney torture_type = ttype; 671b5daa8f3SPaul E. McKenney verbose = v; 67236970bb9SPaul E. McKenney fullstop = FULLSTOP_DONTSTOP; 6735228084eSPaul E. McKenney return true; 674b5daa8f3SPaul E. McKenney } 675b5daa8f3SPaul E. McKenney EXPORT_SYMBOL_GPL(torture_init_begin); 676b5daa8f3SPaul E. McKenney 677b5daa8f3SPaul E. McKenney /* 678b5daa8f3SPaul E. McKenney * Tell the torture module that initialization is complete. 679b5daa8f3SPaul E. McKenney */ 6802b3f8ffeSPranith Kumar void torture_init_end(void) 681b5daa8f3SPaul E. McKenney { 682b5daa8f3SPaul E. McKenney mutex_unlock(&fullstop_mutex); 6834622b487SPaul E. McKenney register_reboot_notifier(&torture_shutdown_nb); 684b5daa8f3SPaul E. McKenney } 685b5daa8f3SPaul E. McKenney EXPORT_SYMBOL_GPL(torture_init_end); 686cc47ae08SPaul E. McKenney 687cc47ae08SPaul E. McKenney /* 688cc47ae08SPaul E. McKenney * Clean up torture module. Please note that this is -not- invoked via 689cc47ae08SPaul E. McKenney * the usual module_exit() mechanism, but rather by an explicit call from 690cc47ae08SPaul E. McKenney * the client torture module. Returns true if a race with system shutdown 691bfefc73aSPaul E. McKenney * is detected, otherwise, all kthreads started by functions in this file 692bfefc73aSPaul E. McKenney * will be shut down. 693cc47ae08SPaul E. McKenney * 694cc47ae08SPaul E. McKenney * This must be called before the caller starts shutting down its own 695cc47ae08SPaul E. McKenney * kthreads. 696d36a7a0dSDavidlohr Bueso * 697d36a7a0dSDavidlohr Bueso * Both torture_cleanup_begin() and torture_cleanup_end() must be paired, 698d36a7a0dSDavidlohr Bueso * in order to correctly perform the cleanup. They are separated because 699d36a7a0dSDavidlohr Bueso * threads can still need to reference the torture_type type, thus nullify 700d36a7a0dSDavidlohr Bueso * only after completing all other relevant calls. 701cc47ae08SPaul E. McKenney */ 702d36a7a0dSDavidlohr Bueso bool torture_cleanup_begin(void) 703cc47ae08SPaul E. McKenney { 704cc47ae08SPaul E. McKenney mutex_lock(&fullstop_mutex); 7057d0ae808SPaul E. McKenney if (READ_ONCE(fullstop) == FULLSTOP_SHUTDOWN) { 706cc47ae08SPaul E. McKenney pr_warn("Concurrent rmmod and shutdown illegal!\n"); 707cc47ae08SPaul E. McKenney mutex_unlock(&fullstop_mutex); 708cc47ae08SPaul E. McKenney schedule_timeout_uninterruptible(10); 709cc47ae08SPaul E. McKenney return true; 710cc47ae08SPaul E. McKenney } 7117d0ae808SPaul E. McKenney WRITE_ONCE(fullstop, FULLSTOP_RMMOD); 712cc47ae08SPaul E. McKenney mutex_unlock(&fullstop_mutex); 713bfefc73aSPaul E. McKenney torture_shutdown_cleanup(); 714cc47ae08SPaul E. McKenney torture_shuffle_cleanup(); 715bfefc73aSPaul E. McKenney torture_stutter_cleanup(); 716cc47ae08SPaul E. McKenney torture_onoff_cleanup(); 717d36a7a0dSDavidlohr Bueso return false; 718d36a7a0dSDavidlohr Bueso } 719d36a7a0dSDavidlohr Bueso EXPORT_SYMBOL_GPL(torture_cleanup_begin); 720d36a7a0dSDavidlohr Bueso 721d36a7a0dSDavidlohr Bueso void torture_cleanup_end(void) 722d36a7a0dSDavidlohr Bueso { 7235228084eSPaul E. McKenney mutex_lock(&fullstop_mutex); 7245228084eSPaul E. McKenney torture_type = NULL; 7255228084eSPaul E. McKenney mutex_unlock(&fullstop_mutex); 726cc47ae08SPaul E. McKenney } 727d36a7a0dSDavidlohr Bueso EXPORT_SYMBOL_GPL(torture_cleanup_end); 72836970bb9SPaul E. McKenney 72936970bb9SPaul E. McKenney /* 73036970bb9SPaul E. McKenney * Is it time for the current torture test to stop? 73136970bb9SPaul E. McKenney */ 73236970bb9SPaul E. McKenney bool torture_must_stop(void) 73336970bb9SPaul E. McKenney { 73436970bb9SPaul E. McKenney return torture_must_stop_irq() || kthread_should_stop(); 73536970bb9SPaul E. McKenney } 73636970bb9SPaul E. McKenney EXPORT_SYMBOL_GPL(torture_must_stop); 73736970bb9SPaul E. McKenney 73836970bb9SPaul E. McKenney /* 73936970bb9SPaul E. McKenney * Is it time for the current torture test to stop? This is the irq-safe 74036970bb9SPaul E. McKenney * version, hence no check for kthread_should_stop(). 74136970bb9SPaul E. McKenney */ 74236970bb9SPaul E. McKenney bool torture_must_stop_irq(void) 74336970bb9SPaul E. McKenney { 7447d0ae808SPaul E. McKenney return READ_ONCE(fullstop) != FULLSTOP_DONTSTOP; 74536970bb9SPaul E. McKenney } 74636970bb9SPaul E. McKenney EXPORT_SYMBOL_GPL(torture_must_stop_irq); 7477fafaac5SPaul E. McKenney 7487fafaac5SPaul E. McKenney /* 7497fafaac5SPaul E. McKenney * Each kthread must wait for kthread_should_stop() before returning from 7507fafaac5SPaul E. McKenney * its top-level function, otherwise segfaults ensue. This function 7517fafaac5SPaul E. McKenney * prints a "stopping" message and waits for kthread_should_stop(), and 7527fafaac5SPaul E. McKenney * should be called from all torture kthreads immediately prior to 7537fafaac5SPaul E. McKenney * returning. 7547fafaac5SPaul E. McKenney */ 7557fafaac5SPaul E. McKenney void torture_kthread_stopping(char *title) 7567fafaac5SPaul E. McKenney { 7570d6821d5SPaul E. McKenney char buf[128]; 7580d6821d5SPaul E. McKenney 7590d6821d5SPaul E. McKenney snprintf(buf, sizeof(buf), "Stopping %s", title); 7600d6821d5SPaul E. McKenney VERBOSE_TOROUT_STRING(buf); 7617fafaac5SPaul E. McKenney while (!kthread_should_stop()) { 7627fafaac5SPaul E. McKenney torture_shutdown_absorb(title); 7637fafaac5SPaul E. McKenney schedule_timeout_uninterruptible(1); 7647fafaac5SPaul E. McKenney } 7657fafaac5SPaul E. McKenney } 7667fafaac5SPaul E. McKenney EXPORT_SYMBOL_GPL(torture_kthread_stopping); 76747cf29b9SPaul E. McKenney 76847cf29b9SPaul E. McKenney /* 76947cf29b9SPaul E. McKenney * Create a generic torture kthread that is immediately runnable. If you 77047cf29b9SPaul E. McKenney * need the kthread to be stopped so that you can do something to it before 77147cf29b9SPaul E. McKenney * it starts, you will need to open-code your own. 77247cf29b9SPaul E. McKenney */ 77347cf29b9SPaul E. McKenney int _torture_create_kthread(int (*fn)(void *arg), void *arg, char *s, char *m, 77447cf29b9SPaul E. McKenney char *f, struct task_struct **tp) 77547cf29b9SPaul E. McKenney { 77647cf29b9SPaul E. McKenney int ret = 0; 77747cf29b9SPaul E. McKenney 77847cf29b9SPaul E. McKenney VERBOSE_TOROUT_STRING(m); 7796945915eSKees Cook *tp = kthread_run(fn, arg, "%s", s); 78047cf29b9SPaul E. McKenney if (IS_ERR(*tp)) { 78147cf29b9SPaul E. McKenney ret = PTR_ERR(*tp); 78247cf29b9SPaul E. McKenney VERBOSE_TOROUT_ERRSTRING(f); 78347cf29b9SPaul E. McKenney *tp = NULL; 78447cf29b9SPaul E. McKenney } 78547cf29b9SPaul E. McKenney torture_shuffle_task_register(*tp); 78647cf29b9SPaul E. McKenney return ret; 78747cf29b9SPaul E. McKenney } 78847cf29b9SPaul E. McKenney EXPORT_SYMBOL_GPL(_torture_create_kthread); 7899c029b86SPaul E. McKenney 7909c029b86SPaul E. McKenney /* 7919c029b86SPaul E. McKenney * Stop a generic kthread, emitting a message. 7929c029b86SPaul E. McKenney */ 7939c029b86SPaul E. McKenney void _torture_stop_kthread(char *m, struct task_struct **tp) 7949c029b86SPaul E. McKenney { 7959c029b86SPaul E. McKenney if (*tp == NULL) 7969c029b86SPaul E. McKenney return; 7979c029b86SPaul E. McKenney VERBOSE_TOROUT_STRING(m); 7989c029b86SPaul E. McKenney kthread_stop(*tp); 7999c029b86SPaul E. McKenney *tp = NULL; 8009c029b86SPaul E. McKenney } 8019c029b86SPaul E. McKenney EXPORT_SYMBOL_GPL(_torture_stop_kthread); 802