18f8e76c0SPaul E. McKenney // SPDX-License-Identifier: GPL-2.0+ 251b1130eSPaul E. McKenney /* 351b1130eSPaul E. McKenney * Common functions for in-kernel torture tests. 451b1130eSPaul E. McKenney * 551b1130eSPaul E. McKenney * Copyright (C) IBM Corporation, 2014 651b1130eSPaul E. McKenney * 78f8e76c0SPaul E. McKenney * Author: Paul E. McKenney <paulmck@linux.ibm.com> 851b1130eSPaul E. McKenney * Based on kernel/rcu/torture.c. 951b1130eSPaul E. McKenney */ 1060500037SPaul E. McKenney 1160500037SPaul E. McKenney #define pr_fmt(fmt) fmt 1260500037SPaul E. McKenney 1351b1130eSPaul E. McKenney #include <linux/types.h> 1451b1130eSPaul E. McKenney #include <linux/kernel.h> 1551b1130eSPaul E. McKenney #include <linux/init.h> 1651b1130eSPaul E. McKenney #include <linux/module.h> 1751b1130eSPaul E. McKenney #include <linux/kthread.h> 1851b1130eSPaul E. McKenney #include <linux/err.h> 1951b1130eSPaul E. McKenney #include <linux/spinlock.h> 2051b1130eSPaul E. McKenney #include <linux/smp.h> 2151b1130eSPaul E. McKenney #include <linux/interrupt.h> 2251b1130eSPaul E. McKenney #include <linux/sched.h> 23e6017571SIngo Molnar #include <linux/sched/clock.h> 2451b1130eSPaul E. McKenney #include <linux/atomic.h> 2551b1130eSPaul E. McKenney #include <linux/bitops.h> 2651b1130eSPaul E. McKenney #include <linux/completion.h> 2751b1130eSPaul E. McKenney #include <linux/moduleparam.h> 2851b1130eSPaul E. McKenney #include <linux/percpu.h> 2951b1130eSPaul E. McKenney #include <linux/notifier.h> 3051b1130eSPaul E. McKenney #include <linux/reboot.h> 3151b1130eSPaul E. McKenney #include <linux/freezer.h> 3251b1130eSPaul E. McKenney #include <linux/cpu.h> 3351b1130eSPaul E. McKenney #include <linux/delay.h> 3451b1130eSPaul E. McKenney #include <linux/stat.h> 3551b1130eSPaul E. McKenney #include <linux/slab.h> 3651b1130eSPaul E. McKenney #include <linux/trace_clock.h> 3731257c3cSPaul E. McKenney #include <linux/ktime.h> 3851b1130eSPaul E. McKenney #include <asm/byteorder.h> 3951b1130eSPaul E. McKenney #include <linux/torture.h> 40dac95906SPaul E. McKenney #include "rcu/rcu.h" 4151b1130eSPaul E. McKenney 4251b1130eSPaul E. McKenney MODULE_LICENSE("GPL"); 438f8e76c0SPaul E. McKenney MODULE_AUTHOR("Paul E. McKenney <paulmck@linux.ibm.com>"); 4451b1130eSPaul E. McKenney 458171d3e0SPaul E. McKenney static bool disable_onoff_at_boot; 468171d3e0SPaul E. McKenney module_param(disable_onoff_at_boot, bool, 0444); 478171d3e0SPaul E. McKenney 482102ad29SPaul E. McKenney static bool ftrace_dump_at_shutdown; 492102ad29SPaul E. McKenney module_param(ftrace_dump_at_shutdown, bool, 0444); 502102ad29SPaul E. McKenney 518a67a20bSPaul E. McKenney static int verbose_sleep_frequency; 528a67a20bSPaul E. McKenney module_param(verbose_sleep_frequency, int, 0444); 538a67a20bSPaul E. McKenney 548a67a20bSPaul E. McKenney static int verbose_sleep_duration = 1; 558a67a20bSPaul E. McKenney module_param(verbose_sleep_duration, int, 0444); 568a67a20bSPaul E. McKenney 57b5daa8f3SPaul E. McKenney static char *torture_type; 5890127d60SPaul E. McKenney static int verbose; 59b5daa8f3SPaul E. McKenney 6036970bb9SPaul E. McKenney /* Mediate rmmod and system shutdown. Concurrent rmmod & shutdown illegal! */ 6136970bb9SPaul E. McKenney #define FULLSTOP_DONTSTOP 0 /* Normal operation. */ 6236970bb9SPaul E. McKenney #define FULLSTOP_SHUTDOWN 1 /* System shutdown with torture running. */ 6336970bb9SPaul E. McKenney #define FULLSTOP_RMMOD 2 /* Normal rmmod of torture. */ 6436970bb9SPaul E. McKenney static int fullstop = FULLSTOP_RMMOD; 654622b487SPaul E. McKenney static DEFINE_MUTEX(fullstop_mutex); 66f67a3356SPaul E. McKenney 678a67a20bSPaul E. McKenney static atomic_t verbose_sleep_counter; 688a67a20bSPaul E. McKenney 698a67a20bSPaul E. McKenney /* 708a67a20bSPaul E. McKenney * Sleep if needed from VERBOSE_TOROUT*(). 718a67a20bSPaul E. McKenney */ 728a67a20bSPaul E. McKenney void verbose_torout_sleep(void) 738a67a20bSPaul E. McKenney { 748a67a20bSPaul E. McKenney if (verbose_sleep_frequency > 0 && 758a67a20bSPaul E. McKenney verbose_sleep_duration > 0 && 768a67a20bSPaul E. McKenney !(atomic_inc_return(&verbose_sleep_counter) % verbose_sleep_frequency)) 778a67a20bSPaul E. McKenney schedule_timeout_uninterruptible(verbose_sleep_duration); 788a67a20bSPaul E. McKenney } 798a67a20bSPaul E. McKenney EXPORT_SYMBOL_GPL(verbose_torout_sleep); 808a67a20bSPaul E. McKenney 81ae19aaafSPaul E. McKenney /* 82ae19aaafSPaul E. McKenney * Schedule a high-resolution-timer sleep in nanoseconds, with a 32-bit 83ae19aaafSPaul E. McKenney * nanosecond random fuzz. This function and its friends desynchronize 84ae19aaafSPaul E. McKenney * testing from the timer wheel. 85ae19aaafSPaul E. McKenney */ 86ae19aaafSPaul E. McKenney int torture_hrtimeout_ns(ktime_t baset_ns, u32 fuzzt_ns, struct torture_random_state *trsp) 87ae19aaafSPaul E. McKenney { 88ae19aaafSPaul E. McKenney ktime_t hto = baset_ns; 89ae19aaafSPaul E. McKenney 90ae19aaafSPaul E. McKenney if (trsp) 91ae19aaafSPaul E. McKenney hto += (torture_random(trsp) >> 3) % fuzzt_ns; 92ae19aaafSPaul E. McKenney set_current_state(TASK_UNINTERRUPTIBLE); 93ae19aaafSPaul E. McKenney return schedule_hrtimeout(&hto, HRTIMER_MODE_REL); 94ae19aaafSPaul E. McKenney } 95ae19aaafSPaul E. McKenney EXPORT_SYMBOL_GPL(torture_hrtimeout_ns); 96ae19aaafSPaul E. McKenney 97ae19aaafSPaul E. McKenney /* 98ae19aaafSPaul E. McKenney * Schedule a high-resolution-timer sleep in microseconds, with a 32-bit 99ae19aaafSPaul E. McKenney * nanosecond (not microsecond!) random fuzz. 100ae19aaafSPaul E. McKenney */ 101ae19aaafSPaul E. McKenney int torture_hrtimeout_us(u32 baset_us, u32 fuzzt_ns, struct torture_random_state *trsp) 102ae19aaafSPaul E. McKenney { 103ae19aaafSPaul E. McKenney ktime_t baset_ns = baset_us * NSEC_PER_USEC; 104ae19aaafSPaul E. McKenney 105ae19aaafSPaul E. McKenney return torture_hrtimeout_ns(baset_ns, fuzzt_ns, trsp); 106ae19aaafSPaul E. McKenney } 107ae19aaafSPaul E. McKenney EXPORT_SYMBOL_GPL(torture_hrtimeout_us); 108ae19aaafSPaul E. McKenney 109ae19aaafSPaul E. McKenney /* 110ae19aaafSPaul E. McKenney * Schedule a high-resolution-timer sleep in milliseconds, with a 32-bit 111ae19aaafSPaul E. McKenney * microsecond (not millisecond!) random fuzz. 112ae19aaafSPaul E. McKenney */ 113ae19aaafSPaul E. McKenney int torture_hrtimeout_ms(u32 baset_ms, u32 fuzzt_us, struct torture_random_state *trsp) 114ae19aaafSPaul E. McKenney { 115ae19aaafSPaul E. McKenney ktime_t baset_ns = baset_ms * NSEC_PER_MSEC; 116ae19aaafSPaul E. McKenney u32 fuzzt_ns; 117ae19aaafSPaul E. McKenney 118ae19aaafSPaul E. McKenney if ((u32)~0U / NSEC_PER_USEC < fuzzt_us) 119ae19aaafSPaul E. McKenney fuzzt_ns = (u32)~0U; 120ae19aaafSPaul E. McKenney else 121ae19aaafSPaul E. McKenney fuzzt_ns = fuzzt_us * NSEC_PER_USEC; 122ae19aaafSPaul E. McKenney return torture_hrtimeout_ns(baset_ns, fuzzt_ns, trsp); 123ae19aaafSPaul E. McKenney } 124ae19aaafSPaul E. McKenney EXPORT_SYMBOL_GPL(torture_hrtimeout_ms); 125ae19aaafSPaul E. McKenney 126ae19aaafSPaul E. McKenney /* 127ae19aaafSPaul E. McKenney * Schedule a high-resolution-timer sleep in jiffies, with an 128ae19aaafSPaul E. McKenney * implied one-jiffy random fuzz. This is intended to replace calls to 129ae19aaafSPaul E. McKenney * schedule_timeout_interruptible() and friends. 130ae19aaafSPaul E. McKenney */ 131ae19aaafSPaul E. McKenney int torture_hrtimeout_jiffies(u32 baset_j, struct torture_random_state *trsp) 132ae19aaafSPaul E. McKenney { 133ae19aaafSPaul E. McKenney ktime_t baset_ns = jiffies_to_nsecs(baset_j); 134ae19aaafSPaul E. McKenney 135ae19aaafSPaul E. McKenney return torture_hrtimeout_ns(baset_ns, jiffies_to_nsecs(1), trsp); 136ae19aaafSPaul E. McKenney } 137ae19aaafSPaul E. McKenney EXPORT_SYMBOL_GPL(torture_hrtimeout_jiffies); 138ae19aaafSPaul E. McKenney 139ae19aaafSPaul E. McKenney /* 140ae19aaafSPaul E. McKenney * Schedule a high-resolution-timer sleep in milliseconds, with a 32-bit 141ae19aaafSPaul E. McKenney * millisecond (not second!) random fuzz. 142ae19aaafSPaul E. McKenney */ 143ae19aaafSPaul E. McKenney int torture_hrtimeout_s(u32 baset_s, u32 fuzzt_ms, struct torture_random_state *trsp) 144ae19aaafSPaul E. McKenney { 145ae19aaafSPaul E. McKenney ktime_t baset_ns = baset_s * NSEC_PER_SEC; 146ae19aaafSPaul E. McKenney u32 fuzzt_ns; 147ae19aaafSPaul E. McKenney 148ae19aaafSPaul E. McKenney if ((u32)~0U / NSEC_PER_MSEC < fuzzt_ms) 149ae19aaafSPaul E. McKenney fuzzt_ns = (u32)~0U; 150ae19aaafSPaul E. McKenney else 151ae19aaafSPaul E. McKenney fuzzt_ns = fuzzt_ms * NSEC_PER_MSEC; 152ae19aaafSPaul E. McKenney return torture_hrtimeout_ns(baset_ns, fuzzt_ns, trsp); 153ae19aaafSPaul E. McKenney } 154ae19aaafSPaul E. McKenney EXPORT_SYMBOL_GPL(torture_hrtimeout_s); 155ae19aaafSPaul E. McKenney 1562e9e8081SPaul E. McKenney #ifdef CONFIG_HOTPLUG_CPU 1572e9e8081SPaul E. McKenney 1582e9e8081SPaul E. McKenney /* 1592e9e8081SPaul E. McKenney * Variables for online-offline handling. Only present if CPU hotplug 1602e9e8081SPaul E. McKenney * is enabled, otherwise does nothing. 1612e9e8081SPaul E. McKenney */ 1622e9e8081SPaul E. McKenney 1632e9e8081SPaul E. McKenney static struct task_struct *onoff_task; 1642e9e8081SPaul E. McKenney static long onoff_holdoff; 1652e9e8081SPaul E. McKenney static long onoff_interval; 1663a6cb58fSPaul E. McKenney static torture_ofl_func *onoff_f; 1672e9e8081SPaul E. McKenney static long n_offline_attempts; 1682e9e8081SPaul E. McKenney static long n_offline_successes; 1692e9e8081SPaul E. McKenney static unsigned long sum_offline; 1702e9e8081SPaul E. McKenney static int min_offline = -1; 1712e9e8081SPaul E. McKenney static int max_offline; 1722e9e8081SPaul E. McKenney static long n_online_attempts; 1732e9e8081SPaul E. McKenney static long n_online_successes; 1742e9e8081SPaul E. McKenney static unsigned long sum_online; 1752e9e8081SPaul E. McKenney static int min_online = -1; 1762e9e8081SPaul E. McKenney static int max_online; 1772e9e8081SPaul E. McKenney 1782e9e8081SPaul E. McKenney /* 179d95f5ba9SPaul E. McKenney * Attempt to take a CPU offline. Return false if the CPU is already 180d95f5ba9SPaul E. McKenney * offline or if it is not subject to CPU-hotplug operations. The 181d95f5ba9SPaul E. McKenney * caller can detect other failures by looking at the statistics. 182d95f5ba9SPaul E. McKenney */ 183d95f5ba9SPaul E. McKenney bool torture_offline(int cpu, long *n_offl_attempts, long *n_offl_successes, 184d95f5ba9SPaul E. McKenney unsigned long *sum_offl, int *min_offl, int *max_offl) 185d95f5ba9SPaul E. McKenney { 186d95f5ba9SPaul E. McKenney unsigned long delta; 187d95f5ba9SPaul E. McKenney int ret; 188a59ee765SPaul E. McKenney char *s; 189d95f5ba9SPaul E. McKenney unsigned long starttime; 190d95f5ba9SPaul E. McKenney 191d95f5ba9SPaul E. McKenney if (!cpu_online(cpu) || !cpu_is_hotpluggable(cpu)) 192d95f5ba9SPaul E. McKenney return false; 19324aca4aeSPaul E. McKenney if (num_online_cpus() <= 1) 19424aca4aeSPaul E. McKenney return false; /* Can't offline the last CPU. */ 195d95f5ba9SPaul E. McKenney 19690127d60SPaul E. McKenney if (verbose > 1) 197d95f5ba9SPaul E. McKenney pr_alert("%s" TORTURE_FLAG 198d95f5ba9SPaul E. McKenney "torture_onoff task: offlining %d\n", 199d95f5ba9SPaul E. McKenney torture_type, cpu); 200d95f5ba9SPaul E. McKenney starttime = jiffies; 201d95f5ba9SPaul E. McKenney (*n_offl_attempts)++; 202457bc8edSQais Yousef ret = remove_cpu(cpu); 203d95f5ba9SPaul E. McKenney if (ret) { 204a59ee765SPaul E. McKenney s = ""; 205a59ee765SPaul E. McKenney if (!rcu_inkernel_boot_has_ended() && ret == -EBUSY) { 206a59ee765SPaul E. McKenney // PCI probe frequently disables hotplug during boot. 207a59ee765SPaul E. McKenney (*n_offl_attempts)--; 208a59ee765SPaul E. McKenney s = " (-EBUSY forgiven during boot)"; 209a59ee765SPaul E. McKenney } 210d95f5ba9SPaul E. McKenney if (verbose) 211d95f5ba9SPaul E. McKenney pr_alert("%s" TORTURE_FLAG 212a59ee765SPaul E. McKenney "torture_onoff task: offline %d failed%s: errno %d\n", 213a59ee765SPaul E. McKenney torture_type, cpu, s, ret); 214d95f5ba9SPaul E. McKenney } else { 21590127d60SPaul E. McKenney if (verbose > 1) 216d95f5ba9SPaul E. McKenney pr_alert("%s" TORTURE_FLAG 217d95f5ba9SPaul E. McKenney "torture_onoff task: offlined %d\n", 218d95f5ba9SPaul E. McKenney torture_type, cpu); 2193a6cb58fSPaul E. McKenney if (onoff_f) 2203a6cb58fSPaul E. McKenney onoff_f(); 221d95f5ba9SPaul E. McKenney (*n_offl_successes)++; 222d95f5ba9SPaul E. McKenney delta = jiffies - starttime; 223a2b2df20SPaul E. McKenney *sum_offl += delta; 224d95f5ba9SPaul E. McKenney if (*min_offl < 0) { 225d95f5ba9SPaul E. McKenney *min_offl = delta; 226d95f5ba9SPaul E. McKenney *max_offl = delta; 227d95f5ba9SPaul E. McKenney } 228d95f5ba9SPaul E. McKenney if (*min_offl > delta) 229d95f5ba9SPaul E. McKenney *min_offl = delta; 230d95f5ba9SPaul E. McKenney if (*max_offl < delta) 231d95f5ba9SPaul E. McKenney *max_offl = delta; 232d95f5ba9SPaul E. McKenney } 233d95f5ba9SPaul E. McKenney 234d95f5ba9SPaul E. McKenney return true; 235d95f5ba9SPaul E. McKenney } 236d95f5ba9SPaul E. McKenney EXPORT_SYMBOL_GPL(torture_offline); 237d95f5ba9SPaul E. McKenney 238d95f5ba9SPaul E. McKenney /* 239d95f5ba9SPaul E. McKenney * Attempt to bring a CPU online. Return false if the CPU is already 240d95f5ba9SPaul E. McKenney * online or if it is not subject to CPU-hotplug operations. The 241d95f5ba9SPaul E. McKenney * caller can detect other failures by looking at the statistics. 242d95f5ba9SPaul E. McKenney */ 243d95f5ba9SPaul E. McKenney bool torture_online(int cpu, long *n_onl_attempts, long *n_onl_successes, 244d95f5ba9SPaul E. McKenney unsigned long *sum_onl, int *min_onl, int *max_onl) 245d95f5ba9SPaul E. McKenney { 246d95f5ba9SPaul E. McKenney unsigned long delta; 247d95f5ba9SPaul E. McKenney int ret; 248a59ee765SPaul E. McKenney char *s; 249d95f5ba9SPaul E. McKenney unsigned long starttime; 250d95f5ba9SPaul E. McKenney 251d95f5ba9SPaul E. McKenney if (cpu_online(cpu) || !cpu_is_hotpluggable(cpu)) 252d95f5ba9SPaul E. McKenney return false; 253d95f5ba9SPaul E. McKenney 25490127d60SPaul E. McKenney if (verbose > 1) 255d95f5ba9SPaul E. McKenney pr_alert("%s" TORTURE_FLAG 256d95f5ba9SPaul E. McKenney "torture_onoff task: onlining %d\n", 257d95f5ba9SPaul E. McKenney torture_type, cpu); 258d95f5ba9SPaul E. McKenney starttime = jiffies; 259d95f5ba9SPaul E. McKenney (*n_onl_attempts)++; 260457bc8edSQais Yousef ret = add_cpu(cpu); 261d95f5ba9SPaul E. McKenney if (ret) { 262a59ee765SPaul E. McKenney s = ""; 263a59ee765SPaul E. McKenney if (!rcu_inkernel_boot_has_ended() && ret == -EBUSY) { 264a59ee765SPaul E. McKenney // PCI probe frequently disables hotplug during boot. 265a59ee765SPaul E. McKenney (*n_onl_attempts)--; 266a59ee765SPaul E. McKenney s = " (-EBUSY forgiven during boot)"; 267a59ee765SPaul E. McKenney } 268d95f5ba9SPaul E. McKenney if (verbose) 269d95f5ba9SPaul E. McKenney pr_alert("%s" TORTURE_FLAG 270a59ee765SPaul E. McKenney "torture_onoff task: online %d failed%s: errno %d\n", 271a59ee765SPaul E. McKenney torture_type, cpu, s, ret); 272d95f5ba9SPaul E. McKenney } else { 27390127d60SPaul E. McKenney if (verbose > 1) 274d95f5ba9SPaul E. McKenney pr_alert("%s" TORTURE_FLAG 275d95f5ba9SPaul E. McKenney "torture_onoff task: onlined %d\n", 276d95f5ba9SPaul E. McKenney torture_type, cpu); 277d95f5ba9SPaul E. McKenney (*n_onl_successes)++; 278d95f5ba9SPaul E. McKenney delta = jiffies - starttime; 279d95f5ba9SPaul E. McKenney *sum_onl += delta; 280d95f5ba9SPaul E. McKenney if (*min_onl < 0) { 281d95f5ba9SPaul E. McKenney *min_onl = delta; 282d95f5ba9SPaul E. McKenney *max_onl = delta; 283d95f5ba9SPaul E. McKenney } 284d95f5ba9SPaul E. McKenney if (*min_onl > delta) 285d95f5ba9SPaul E. McKenney *min_onl = delta; 286d95f5ba9SPaul E. McKenney if (*max_onl < delta) 287d95f5ba9SPaul E. McKenney *max_onl = delta; 288d95f5ba9SPaul E. McKenney } 289d95f5ba9SPaul E. McKenney 290d95f5ba9SPaul E. McKenney return true; 291d95f5ba9SPaul E. McKenney } 292d95f5ba9SPaul E. McKenney EXPORT_SYMBOL_GPL(torture_online); 293d95f5ba9SPaul E. McKenney 294d95f5ba9SPaul E. McKenney /* 295*0b962c8fSPaul E. McKenney * Get everything online at the beginning and ends of tests. 296*0b962c8fSPaul E. McKenney */ 297*0b962c8fSPaul E. McKenney static void torture_online_all(char *phase) 298*0b962c8fSPaul E. McKenney { 299*0b962c8fSPaul E. McKenney int cpu; 300*0b962c8fSPaul E. McKenney int ret; 301*0b962c8fSPaul E. McKenney 302*0b962c8fSPaul E. McKenney for_each_possible_cpu(cpu) { 303*0b962c8fSPaul E. McKenney if (cpu_online(cpu)) 304*0b962c8fSPaul E. McKenney continue; 305*0b962c8fSPaul E. McKenney ret = add_cpu(cpu); 306*0b962c8fSPaul E. McKenney if (ret && verbose) { 307*0b962c8fSPaul E. McKenney pr_alert("%s" TORTURE_FLAG 308*0b962c8fSPaul E. McKenney "%s: %s online %d: errno %d\n", 309*0b962c8fSPaul E. McKenney __func__, phase, torture_type, cpu, ret); 310*0b962c8fSPaul E. McKenney } 311*0b962c8fSPaul E. McKenney } 312*0b962c8fSPaul E. McKenney } 313*0b962c8fSPaul E. McKenney 314*0b962c8fSPaul E. McKenney /* 3152e9e8081SPaul E. McKenney * Execute random CPU-hotplug operations at the interval specified 3162e9e8081SPaul E. McKenney * by the onoff_interval. 3172e9e8081SPaul E. McKenney */ 3182e9e8081SPaul E. McKenney static int 3192e9e8081SPaul E. McKenney torture_onoff(void *arg) 3202e9e8081SPaul E. McKenney { 3212e9e8081SPaul E. McKenney int cpu; 3222e9e8081SPaul E. McKenney int maxcpu = -1; 3232e9e8081SPaul E. McKenney DEFINE_TORTURE_RANDOM(rand); 3242e9e8081SPaul E. McKenney 3252e9e8081SPaul E. McKenney VERBOSE_TOROUT_STRING("torture_onoff task started"); 3262e9e8081SPaul E. McKenney for_each_online_cpu(cpu) 3272e9e8081SPaul E. McKenney maxcpu = cpu; 3282e9e8081SPaul E. McKenney WARN_ON(maxcpu < 0); 329*0b962c8fSPaul E. McKenney torture_online_all("Initial"); 330750db0f5SBoqun Feng if (maxcpu == 0) { 331750db0f5SBoqun Feng VERBOSE_TOROUT_STRING("Only one CPU, so CPU-hotplug testing is disabled"); 332750db0f5SBoqun Feng goto stop; 333750db0f5SBoqun Feng } 334750db0f5SBoqun Feng 3352e9e8081SPaul E. McKenney if (onoff_holdoff > 0) { 3362e9e8081SPaul E. McKenney VERBOSE_TOROUT_STRING("torture_onoff begin holdoff"); 3372e9e8081SPaul E. McKenney schedule_timeout_interruptible(onoff_holdoff); 3382e9e8081SPaul E. McKenney VERBOSE_TOROUT_STRING("torture_onoff end holdoff"); 3392e9e8081SPaul E. McKenney } 3402e9e8081SPaul E. McKenney while (!torture_must_stop()) { 3418171d3e0SPaul E. McKenney if (disable_onoff_at_boot && !rcu_inkernel_boot_has_ended()) { 3428171d3e0SPaul E. McKenney schedule_timeout_interruptible(HZ / 10); 3438171d3e0SPaul E. McKenney continue; 3448171d3e0SPaul E. McKenney } 3452e9e8081SPaul E. McKenney cpu = (torture_random(&rand) >> 4) % (maxcpu + 1); 346d95f5ba9SPaul E. McKenney if (!torture_offline(cpu, 347d95f5ba9SPaul E. McKenney &n_offline_attempts, &n_offline_successes, 348d95f5ba9SPaul E. McKenney &sum_offline, &min_offline, &max_offline)) 349d95f5ba9SPaul E. McKenney torture_online(cpu, 350d95f5ba9SPaul E. McKenney &n_online_attempts, &n_online_successes, 351d95f5ba9SPaul E. McKenney &sum_online, &min_online, &max_online); 3522e9e8081SPaul E. McKenney schedule_timeout_interruptible(onoff_interval); 3532e9e8081SPaul E. McKenney } 354750db0f5SBoqun Feng 355750db0f5SBoqun Feng stop: 3567fafaac5SPaul E. McKenney torture_kthread_stopping("torture_onoff"); 357*0b962c8fSPaul E. McKenney torture_online_all("Final"); 3582e9e8081SPaul E. McKenney return 0; 3592e9e8081SPaul E. McKenney } 3602e9e8081SPaul E. McKenney 3612e9e8081SPaul E. McKenney #endif /* #ifdef CONFIG_HOTPLUG_CPU */ 3622e9e8081SPaul E. McKenney 3632e9e8081SPaul E. McKenney /* 3642e9e8081SPaul E. McKenney * Initiate online-offline handling. 3652e9e8081SPaul E. McKenney */ 3663a6cb58fSPaul E. McKenney int torture_onoff_init(long ooholdoff, long oointerval, torture_ofl_func *f) 3672e9e8081SPaul E. McKenney { 36847cf29b9SPaul E. McKenney #ifdef CONFIG_HOTPLUG_CPU 3692e9e8081SPaul E. McKenney onoff_holdoff = ooholdoff; 3702e9e8081SPaul E. McKenney onoff_interval = oointerval; 3713a6cb58fSPaul E. McKenney onoff_f = f; 3722e9e8081SPaul E. McKenney if (onoff_interval <= 0) 3732e9e8081SPaul E. McKenney return 0; 3742a7d9688SPierce Griffiths return torture_create_kthread(torture_onoff, NULL, onoff_task); 3752a7d9688SPierce Griffiths #else /* #ifdef CONFIG_HOTPLUG_CPU */ 3762a7d9688SPierce Griffiths return 0; 3772a7d9688SPierce Griffiths #endif /* #else #ifdef CONFIG_HOTPLUG_CPU */ 3782e9e8081SPaul E. McKenney } 3792e9e8081SPaul E. McKenney EXPORT_SYMBOL_GPL(torture_onoff_init); 3802e9e8081SPaul E. McKenney 3812e9e8081SPaul E. McKenney /* 3822e9e8081SPaul E. McKenney * Clean up after online/offline testing. 3832e9e8081SPaul E. McKenney */ 384cc47ae08SPaul E. McKenney static void torture_onoff_cleanup(void) 3852e9e8081SPaul E. McKenney { 3862e9e8081SPaul E. McKenney #ifdef CONFIG_HOTPLUG_CPU 3872e9e8081SPaul E. McKenney if (onoff_task == NULL) 3882e9e8081SPaul E. McKenney return; 3892e9e8081SPaul E. McKenney VERBOSE_TOROUT_STRING("Stopping torture_onoff task"); 3902e9e8081SPaul E. McKenney kthread_stop(onoff_task); 3912e9e8081SPaul E. McKenney onoff_task = NULL; 3922e9e8081SPaul E. McKenney #endif /* #ifdef CONFIG_HOTPLUG_CPU */ 3932e9e8081SPaul E. McKenney } 3942e9e8081SPaul E. McKenney 3952e9e8081SPaul E. McKenney /* 3962e9e8081SPaul E. McKenney * Print online/offline testing statistics. 3972e9e8081SPaul E. McKenney */ 398eea203feSJoe Perches void torture_onoff_stats(void) 3992e9e8081SPaul E. McKenney { 4002e9e8081SPaul E. McKenney #ifdef CONFIG_HOTPLUG_CPU 401eea203feSJoe Perches pr_cont("onoff: %ld/%ld:%ld/%ld %d,%d:%d,%d %lu:%lu (HZ=%d) ", 4022e9e8081SPaul E. McKenney n_online_successes, n_online_attempts, 4032e9e8081SPaul E. McKenney n_offline_successes, n_offline_attempts, 4042e9e8081SPaul E. McKenney min_online, max_online, 4052e9e8081SPaul E. McKenney min_offline, max_offline, 4062e9e8081SPaul E. McKenney sum_online, sum_offline, HZ); 4072e9e8081SPaul E. McKenney #endif /* #ifdef CONFIG_HOTPLUG_CPU */ 4082e9e8081SPaul E. McKenney } 4092e9e8081SPaul E. McKenney EXPORT_SYMBOL_GPL(torture_onoff_stats); 4102e9e8081SPaul E. McKenney 4112e9e8081SPaul E. McKenney /* 4122e9e8081SPaul E. McKenney * Were all the online/offline operations successful? 4132e9e8081SPaul E. McKenney */ 4142e9e8081SPaul E. McKenney bool torture_onoff_failures(void) 4152e9e8081SPaul E. McKenney { 4162e9e8081SPaul E. McKenney #ifdef CONFIG_HOTPLUG_CPU 4172e9e8081SPaul E. McKenney return n_online_successes != n_online_attempts || 4182e9e8081SPaul E. McKenney n_offline_successes != n_offline_attempts; 4192e9e8081SPaul E. McKenney #else /* #ifdef CONFIG_HOTPLUG_CPU */ 4202e9e8081SPaul E. McKenney return false; 4212e9e8081SPaul E. McKenney #endif /* #else #ifdef CONFIG_HOTPLUG_CPU */ 4222e9e8081SPaul E. McKenney } 4232e9e8081SPaul E. McKenney EXPORT_SYMBOL_GPL(torture_onoff_failures); 4242e9e8081SPaul E. McKenney 42551b1130eSPaul E. McKenney #define TORTURE_RANDOM_MULT 39916801 /* prime */ 42651b1130eSPaul E. McKenney #define TORTURE_RANDOM_ADD 479001701 /* prime */ 42751b1130eSPaul E. McKenney #define TORTURE_RANDOM_REFRESH 10000 42851b1130eSPaul E. McKenney 42951b1130eSPaul E. McKenney /* 43051b1130eSPaul E. McKenney * Crude but fast random-number generator. Uses a linear congruential 43151b1130eSPaul E. McKenney * generator, with occasional help from cpu_clock(). 43251b1130eSPaul E. McKenney */ 43351b1130eSPaul E. McKenney unsigned long 43451b1130eSPaul E. McKenney torture_random(struct torture_random_state *trsp) 43551b1130eSPaul E. McKenney { 43651b1130eSPaul E. McKenney if (--trsp->trs_count < 0) { 43751b1130eSPaul E. McKenney trsp->trs_state += (unsigned long)local_clock(); 43851b1130eSPaul E. McKenney trsp->trs_count = TORTURE_RANDOM_REFRESH; 43951b1130eSPaul E. McKenney } 44051b1130eSPaul E. McKenney trsp->trs_state = trsp->trs_state * TORTURE_RANDOM_MULT + 44151b1130eSPaul E. McKenney TORTURE_RANDOM_ADD; 44251b1130eSPaul E. McKenney return swahw32(trsp->trs_state); 44351b1130eSPaul E. McKenney } 44451b1130eSPaul E. McKenney EXPORT_SYMBOL_GPL(torture_random); 445f67a3356SPaul E. McKenney 446f67a3356SPaul E. McKenney /* 4473808dc9fSPaul E. McKenney * Variables for shuffling. The idea is to ensure that each CPU stays 4483808dc9fSPaul E. McKenney * idle for an extended period to test interactions with dyntick idle, 449b564d62eSMasahiro Yamada * as well as interactions with any per-CPU variables. 4503808dc9fSPaul E. McKenney */ 4513808dc9fSPaul E. McKenney struct shuffle_task { 4523808dc9fSPaul E. McKenney struct list_head st_l; 4533808dc9fSPaul E. McKenney struct task_struct *st_t; 4543808dc9fSPaul E. McKenney }; 4553808dc9fSPaul E. McKenney 4563808dc9fSPaul E. McKenney static long shuffle_interval; /* In jiffies. */ 4573808dc9fSPaul E. McKenney static struct task_struct *shuffler_task; 4583808dc9fSPaul E. McKenney static cpumask_var_t shuffle_tmp_mask; 4593808dc9fSPaul E. McKenney static int shuffle_idle_cpu; /* Force all torture tasks off this CPU */ 4603808dc9fSPaul E. McKenney static struct list_head shuffle_task_list = LIST_HEAD_INIT(shuffle_task_list); 4613808dc9fSPaul E. McKenney static DEFINE_MUTEX(shuffle_task_mutex); 4623808dc9fSPaul E. McKenney 4633808dc9fSPaul E. McKenney /* 4643808dc9fSPaul E. McKenney * Register a task to be shuffled. If there is no memory, just splat 4653808dc9fSPaul E. McKenney * and don't bother registering. 4663808dc9fSPaul E. McKenney */ 4673808dc9fSPaul E. McKenney void torture_shuffle_task_register(struct task_struct *tp) 4683808dc9fSPaul E. McKenney { 4693808dc9fSPaul E. McKenney struct shuffle_task *stp; 4703808dc9fSPaul E. McKenney 4713808dc9fSPaul E. McKenney if (WARN_ON_ONCE(tp == NULL)) 4723808dc9fSPaul E. McKenney return; 4733808dc9fSPaul E. McKenney stp = kmalloc(sizeof(*stp), GFP_KERNEL); 4743808dc9fSPaul E. McKenney if (WARN_ON_ONCE(stp == NULL)) 4753808dc9fSPaul E. McKenney return; 4763808dc9fSPaul E. McKenney stp->st_t = tp; 4773808dc9fSPaul E. McKenney mutex_lock(&shuffle_task_mutex); 4783808dc9fSPaul E. McKenney list_add(&stp->st_l, &shuffle_task_list); 4793808dc9fSPaul E. McKenney mutex_unlock(&shuffle_task_mutex); 4803808dc9fSPaul E. McKenney } 4813808dc9fSPaul E. McKenney EXPORT_SYMBOL_GPL(torture_shuffle_task_register); 4823808dc9fSPaul E. McKenney 4833808dc9fSPaul E. McKenney /* 4843808dc9fSPaul E. McKenney * Unregister all tasks, for example, at the end of the torture run. 4853808dc9fSPaul E. McKenney */ 4863808dc9fSPaul E. McKenney static void torture_shuffle_task_unregister_all(void) 4873808dc9fSPaul E. McKenney { 4883808dc9fSPaul E. McKenney struct shuffle_task *stp; 4893808dc9fSPaul E. McKenney struct shuffle_task *p; 4903808dc9fSPaul E. McKenney 4913808dc9fSPaul E. McKenney mutex_lock(&shuffle_task_mutex); 4923808dc9fSPaul E. McKenney list_for_each_entry_safe(stp, p, &shuffle_task_list, st_l) { 4933808dc9fSPaul E. McKenney list_del(&stp->st_l); 4943808dc9fSPaul E. McKenney kfree(stp); 4953808dc9fSPaul E. McKenney } 4963808dc9fSPaul E. McKenney mutex_unlock(&shuffle_task_mutex); 4973808dc9fSPaul E. McKenney } 4983808dc9fSPaul E. McKenney 4993808dc9fSPaul E. McKenney /* Shuffle tasks such that we allow shuffle_idle_cpu to become idle. 5003808dc9fSPaul E. McKenney * A special case is when shuffle_idle_cpu = -1, in which case we allow 5013808dc9fSPaul E. McKenney * the tasks to run on all CPUs. 5023808dc9fSPaul E. McKenney */ 5033808dc9fSPaul E. McKenney static void torture_shuffle_tasks(void) 5043808dc9fSPaul E. McKenney { 5053808dc9fSPaul E. McKenney struct shuffle_task *stp; 5063808dc9fSPaul E. McKenney 5073808dc9fSPaul E. McKenney cpumask_setall(shuffle_tmp_mask); 5083808dc9fSPaul E. McKenney get_online_cpus(); 5093808dc9fSPaul E. McKenney 5103808dc9fSPaul E. McKenney /* No point in shuffling if there is only one online CPU (ex: UP) */ 5113808dc9fSPaul E. McKenney if (num_online_cpus() == 1) { 5123808dc9fSPaul E. McKenney put_online_cpus(); 5133808dc9fSPaul E. McKenney return; 5143808dc9fSPaul E. McKenney } 5153808dc9fSPaul E. McKenney 5163808dc9fSPaul E. McKenney /* Advance to the next CPU. Upon overflow, don't idle any CPUs. */ 5173808dc9fSPaul E. McKenney shuffle_idle_cpu = cpumask_next(shuffle_idle_cpu, shuffle_tmp_mask); 5183808dc9fSPaul E. McKenney if (shuffle_idle_cpu >= nr_cpu_ids) 5193808dc9fSPaul E. McKenney shuffle_idle_cpu = -1; 5205ed63b19SIulia Manda else 5213808dc9fSPaul E. McKenney cpumask_clear_cpu(shuffle_idle_cpu, shuffle_tmp_mask); 5223808dc9fSPaul E. McKenney 5233808dc9fSPaul E. McKenney mutex_lock(&shuffle_task_mutex); 5243808dc9fSPaul E. McKenney list_for_each_entry(stp, &shuffle_task_list, st_l) 5253808dc9fSPaul E. McKenney set_cpus_allowed_ptr(stp->st_t, shuffle_tmp_mask); 5263808dc9fSPaul E. McKenney mutex_unlock(&shuffle_task_mutex); 5273808dc9fSPaul E. McKenney 5283808dc9fSPaul E. McKenney put_online_cpus(); 5293808dc9fSPaul E. McKenney } 5303808dc9fSPaul E. McKenney 5313808dc9fSPaul E. McKenney /* Shuffle tasks across CPUs, with the intent of allowing each CPU in the 5323808dc9fSPaul E. McKenney * system to become idle at a time and cut off its timer ticks. This is meant 5333808dc9fSPaul E. McKenney * to test the support for such tickless idle CPU in RCU. 5343808dc9fSPaul E. McKenney */ 5353808dc9fSPaul E. McKenney static int torture_shuffle(void *arg) 5363808dc9fSPaul E. McKenney { 5373808dc9fSPaul E. McKenney VERBOSE_TOROUT_STRING("torture_shuffle task started"); 5383808dc9fSPaul E. McKenney do { 5393808dc9fSPaul E. McKenney schedule_timeout_interruptible(shuffle_interval); 5403808dc9fSPaul E. McKenney torture_shuffle_tasks(); 5413808dc9fSPaul E. McKenney torture_shutdown_absorb("torture_shuffle"); 5423808dc9fSPaul E. McKenney } while (!torture_must_stop()); 5437fafaac5SPaul E. McKenney torture_kthread_stopping("torture_shuffle"); 5443808dc9fSPaul E. McKenney return 0; 5453808dc9fSPaul E. McKenney } 5463808dc9fSPaul E. McKenney 5473808dc9fSPaul E. McKenney /* 5483808dc9fSPaul E. McKenney * Start the shuffler, with shuffint in jiffies. 5493808dc9fSPaul E. McKenney */ 5503808dc9fSPaul E. McKenney int torture_shuffle_init(long shuffint) 5513808dc9fSPaul E. McKenney { 5523808dc9fSPaul E. McKenney shuffle_interval = shuffint; 5533808dc9fSPaul E. McKenney 5543808dc9fSPaul E. McKenney shuffle_idle_cpu = -1; 5553808dc9fSPaul E. McKenney 5563808dc9fSPaul E. McKenney if (!alloc_cpumask_var(&shuffle_tmp_mask, GFP_KERNEL)) { 5573808dc9fSPaul E. McKenney VERBOSE_TOROUT_ERRSTRING("Failed to alloc mask"); 5583808dc9fSPaul E. McKenney return -ENOMEM; 5593808dc9fSPaul E. McKenney } 5603808dc9fSPaul E. McKenney 5613808dc9fSPaul E. McKenney /* Create the shuffler thread */ 56247cf29b9SPaul E. McKenney return torture_create_kthread(torture_shuffle, NULL, shuffler_task); 5633808dc9fSPaul E. McKenney } 5643808dc9fSPaul E. McKenney EXPORT_SYMBOL_GPL(torture_shuffle_init); 5653808dc9fSPaul E. McKenney 5663808dc9fSPaul E. McKenney /* 5673808dc9fSPaul E. McKenney * Stop the shuffling. 5683808dc9fSPaul E. McKenney */ 569cc47ae08SPaul E. McKenney static void torture_shuffle_cleanup(void) 5703808dc9fSPaul E. McKenney { 5713808dc9fSPaul E. McKenney torture_shuffle_task_unregister_all(); 5723808dc9fSPaul E. McKenney if (shuffler_task) { 5733808dc9fSPaul E. McKenney VERBOSE_TOROUT_STRING("Stopping torture_shuffle task"); 5743808dc9fSPaul E. McKenney kthread_stop(shuffler_task); 5753808dc9fSPaul E. McKenney free_cpumask_var(shuffle_tmp_mask); 5763808dc9fSPaul E. McKenney } 5773808dc9fSPaul E. McKenney shuffler_task = NULL; 5783808dc9fSPaul E. McKenney } 5793808dc9fSPaul E. McKenney 5803808dc9fSPaul E. McKenney /* 581e991dbc0SPaul E. McKenney * Variables for auto-shutdown. This allows "lights out" torture runs 582e991dbc0SPaul E. McKenney * to be fully scripted. 583e991dbc0SPaul E. McKenney */ 584e991dbc0SPaul E. McKenney static struct task_struct *shutdown_task; 58531257c3cSPaul E. McKenney static ktime_t shutdown_time; /* time to system shutdown. */ 586e991dbc0SPaul E. McKenney static void (*torture_shutdown_hook)(void); 587e991dbc0SPaul E. McKenney 588e991dbc0SPaul E. McKenney /* 589f67a3356SPaul E. McKenney * Absorb kthreads into a kernel function that won't return, so that 590f67a3356SPaul E. McKenney * they won't ever access module text or data again. 591f67a3356SPaul E. McKenney */ 592f67a3356SPaul E. McKenney void torture_shutdown_absorb(const char *title) 593f67a3356SPaul E. McKenney { 5947d0ae808SPaul E. McKenney while (READ_ONCE(fullstop) == FULLSTOP_SHUTDOWN) { 5954622b487SPaul E. McKenney pr_notice("torture thread %s parking due to system shutdown\n", 596f67a3356SPaul E. McKenney title); 597f67a3356SPaul E. McKenney schedule_timeout_uninterruptible(MAX_SCHEDULE_TIMEOUT); 598f67a3356SPaul E. McKenney } 599f67a3356SPaul E. McKenney } 600f67a3356SPaul E. McKenney EXPORT_SYMBOL_GPL(torture_shutdown_absorb); 601b5daa8f3SPaul E. McKenney 602b5daa8f3SPaul E. McKenney /* 603e991dbc0SPaul E. McKenney * Cause the torture test to shutdown the system after the test has 604e991dbc0SPaul E. McKenney * run for the time specified by the shutdown_secs parameter. 605e991dbc0SPaul E. McKenney */ 606e991dbc0SPaul E. McKenney static int torture_shutdown(void *arg) 607e991dbc0SPaul E. McKenney { 60831257c3cSPaul E. McKenney ktime_t ktime_snap; 609e991dbc0SPaul E. McKenney 610e991dbc0SPaul E. McKenney VERBOSE_TOROUT_STRING("torture_shutdown task started"); 61131257c3cSPaul E. McKenney ktime_snap = ktime_get(); 61231257c3cSPaul E. McKenney while (ktime_before(ktime_snap, shutdown_time) && 613e991dbc0SPaul E. McKenney !torture_must_stop()) { 614e991dbc0SPaul E. McKenney if (verbose) 615e991dbc0SPaul E. McKenney pr_alert("%s" TORTURE_FLAG 61631257c3cSPaul E. McKenney "torture_shutdown task: %llu ms remaining\n", 61731257c3cSPaul E. McKenney torture_type, 61831257c3cSPaul E. McKenney ktime_ms_delta(shutdown_time, ktime_snap)); 61931257c3cSPaul E. McKenney set_current_state(TASK_INTERRUPTIBLE); 62031257c3cSPaul E. McKenney schedule_hrtimeout(&shutdown_time, HRTIMER_MODE_ABS); 62131257c3cSPaul E. McKenney ktime_snap = ktime_get(); 622e991dbc0SPaul E. McKenney } 623e991dbc0SPaul E. McKenney if (torture_must_stop()) { 6247fafaac5SPaul E. McKenney torture_kthread_stopping("torture_shutdown"); 625e991dbc0SPaul E. McKenney return 0; 626e991dbc0SPaul E. McKenney } 627e991dbc0SPaul E. McKenney 628e991dbc0SPaul E. McKenney /* OK, shut down the system. */ 629e991dbc0SPaul E. McKenney 630e991dbc0SPaul E. McKenney VERBOSE_TOROUT_STRING("torture_shutdown task shutting down system"); 631e991dbc0SPaul E. McKenney shutdown_task = NULL; /* Avoid self-kill deadlock. */ 632f881825aSPaul E. McKenney if (torture_shutdown_hook) 633f881825aSPaul E. McKenney torture_shutdown_hook(); 634f881825aSPaul E. McKenney else 635f881825aSPaul E. McKenney VERBOSE_TOROUT_STRING("No torture_shutdown_hook(), skipping."); 6362102ad29SPaul E. McKenney if (ftrace_dump_at_shutdown) 637dac95906SPaul E. McKenney rcu_ftrace_dump(DUMP_ALL); 638e991dbc0SPaul E. McKenney kernel_power_off(); /* Shut down the system. */ 639e991dbc0SPaul E. McKenney return 0; 640e991dbc0SPaul E. McKenney } 641e991dbc0SPaul E. McKenney 642e991dbc0SPaul E. McKenney /* 643e991dbc0SPaul E. McKenney * Start up the shutdown task. 644e991dbc0SPaul E. McKenney */ 645e991dbc0SPaul E. McKenney int torture_shutdown_init(int ssecs, void (*cleanup)(void)) 646e991dbc0SPaul E. McKenney { 647e991dbc0SPaul E. McKenney torture_shutdown_hook = cleanup; 64831257c3cSPaul E. McKenney if (ssecs > 0) { 64931257c3cSPaul E. McKenney shutdown_time = ktime_add(ktime_get(), ktime_set(ssecs, 0)); 6502a7d9688SPierce Griffiths return torture_create_kthread(torture_shutdown, NULL, 65147cf29b9SPaul E. McKenney shutdown_task); 65247cf29b9SPaul E. McKenney } 6532a7d9688SPierce Griffiths return 0; 654e991dbc0SPaul E. McKenney } 655e991dbc0SPaul E. McKenney EXPORT_SYMBOL_GPL(torture_shutdown_init); 656e991dbc0SPaul E. McKenney 657e991dbc0SPaul E. McKenney /* 6584622b487SPaul E. McKenney * Detect and respond to a system shutdown. 6594622b487SPaul E. McKenney */ 6604622b487SPaul E. McKenney static int torture_shutdown_notify(struct notifier_block *unused1, 6614622b487SPaul E. McKenney unsigned long unused2, void *unused3) 6624622b487SPaul E. McKenney { 6634622b487SPaul E. McKenney mutex_lock(&fullstop_mutex); 6647d0ae808SPaul E. McKenney if (READ_ONCE(fullstop) == FULLSTOP_DONTSTOP) { 665fac480efSPaul E. McKenney VERBOSE_TOROUT_STRING("Unscheduled system shutdown detected"); 6667d0ae808SPaul E. McKenney WRITE_ONCE(fullstop, FULLSTOP_SHUTDOWN); 667fac480efSPaul E. McKenney } else { 6684622b487SPaul E. McKenney pr_warn("Concurrent rmmod and shutdown illegal!\n"); 669fac480efSPaul E. McKenney } 6704622b487SPaul E. McKenney mutex_unlock(&fullstop_mutex); 6714622b487SPaul E. McKenney return NOTIFY_DONE; 6724622b487SPaul E. McKenney } 6734622b487SPaul E. McKenney 6744622b487SPaul E. McKenney static struct notifier_block torture_shutdown_nb = { 6754622b487SPaul E. McKenney .notifier_call = torture_shutdown_notify, 6764622b487SPaul E. McKenney }; 6774622b487SPaul E. McKenney 6784622b487SPaul E. McKenney /* 679bfefc73aSPaul E. McKenney * Shut down the shutdown task. Say what??? Heh! This can happen if 680bfefc73aSPaul E. McKenney * the torture module gets an rmmod before the shutdown time arrives. ;-) 681bfefc73aSPaul E. McKenney */ 682bfefc73aSPaul E. McKenney static void torture_shutdown_cleanup(void) 683bfefc73aSPaul E. McKenney { 684bfefc73aSPaul E. McKenney unregister_reboot_notifier(&torture_shutdown_nb); 685bfefc73aSPaul E. McKenney if (shutdown_task != NULL) { 686bfefc73aSPaul E. McKenney VERBOSE_TOROUT_STRING("Stopping torture_shutdown task"); 687bfefc73aSPaul E. McKenney kthread_stop(shutdown_task); 688bfefc73aSPaul E. McKenney } 689bfefc73aSPaul E. McKenney shutdown_task = NULL; 690bfefc73aSPaul E. McKenney } 691bfefc73aSPaul E. McKenney 692bfefc73aSPaul E. McKenney /* 693628edaa5SPaul E. McKenney * Variables for stuttering, which means to periodically pause and 694628edaa5SPaul E. McKenney * restart testing in order to catch bugs that appear when load is 695628edaa5SPaul E. McKenney * suddenly applied to or removed from the system. 696628edaa5SPaul E. McKenney */ 697628edaa5SPaul E. McKenney static struct task_struct *stutter_task; 698628edaa5SPaul E. McKenney static int stutter_pause_test; 699628edaa5SPaul E. McKenney static int stutter; 700ff3bf92dSPaul E. McKenney static int stutter_gap; 701628edaa5SPaul E. McKenney 702628edaa5SPaul E. McKenney /* 703628edaa5SPaul E. McKenney * Block until the stutter interval ends. This must be called periodically 704628edaa5SPaul E. McKenney * by all running kthreads that need to be subject to stuttering. 705628edaa5SPaul E. McKenney */ 706474e59b4SPaul E. McKenney bool stutter_wait(const char *title) 707628edaa5SPaul E. McKenney { 70819012b78SPaul E. McKenney unsigned int i = 0; 709e8516c64SPaul E. McKenney bool ret = false; 71019012b78SPaul E. McKenney int spt; 7114ced3314SPaul E. McKenney 712cee43939SPaul E. McKenney cond_resched_tasks_rcu_qs(); 7134ced3314SPaul E. McKenney spt = READ_ONCE(stutter_pause_test); 71429d39390SPaul E. McKenney for (; spt; spt = READ_ONCE(stutter_pause_test)) { 715ab1b7880SPaul E. McKenney if (!ret) { 716ab1b7880SPaul E. McKenney sched_set_normal(current, MAX_NICE); 717e8516c64SPaul E. McKenney ret = true; 718ab1b7880SPaul E. McKenney } 7194ced3314SPaul E. McKenney if (spt == 1) { 720628edaa5SPaul E. McKenney schedule_timeout_interruptible(1); 7214ced3314SPaul E. McKenney } else if (spt == 2) { 72219012b78SPaul E. McKenney while (READ_ONCE(stutter_pause_test)) { 723ed24affaSPaul E. McKenney if (!(i++ & 0xffff)) 724ed24affaSPaul E. McKenney torture_hrtimeout_us(10, 0, NULL); 725ab7d4505SPaul E. McKenney cond_resched(); 72619012b78SPaul E. McKenney } 7274ced3314SPaul E. McKenney } else { 728628edaa5SPaul E. McKenney schedule_timeout_interruptible(round_jiffies_relative(HZ)); 7294ced3314SPaul E. McKenney } 730628edaa5SPaul E. McKenney torture_shutdown_absorb(title); 731628edaa5SPaul E. McKenney } 732e8516c64SPaul E. McKenney return ret; 733628edaa5SPaul E. McKenney } 734628edaa5SPaul E. McKenney EXPORT_SYMBOL_GPL(stutter_wait); 735628edaa5SPaul E. McKenney 736628edaa5SPaul E. McKenney /* 737628edaa5SPaul E. McKenney * Cause the torture test to "stutter", starting and stopping all 738628edaa5SPaul E. McKenney * threads periodically. 739628edaa5SPaul E. McKenney */ 740628edaa5SPaul E. McKenney static int torture_stutter(void *arg) 741628edaa5SPaul E. McKenney { 742fda5ba9eSPaul E. McKenney DEFINE_TORTURE_RANDOM(rand); 743e8516c64SPaul E. McKenney int wtime; 744e8516c64SPaul E. McKenney 745628edaa5SPaul E. McKenney VERBOSE_TOROUT_STRING("torture_stutter task started"); 746628edaa5SPaul E. McKenney do { 7474ced3314SPaul E. McKenney if (!torture_must_stop() && stutter > 1) { 748e8516c64SPaul E. McKenney wtime = stutter; 749fda5ba9eSPaul E. McKenney if (stutter > 2) { 7504ced3314SPaul E. McKenney WRITE_ONCE(stutter_pause_test, 1); 751fda5ba9eSPaul E. McKenney wtime = stutter - 3; 752ed24affaSPaul E. McKenney torture_hrtimeout_jiffies(wtime, &rand); 753fda5ba9eSPaul E. McKenney wtime = 2; 754e8516c64SPaul E. McKenney } 7557d0ae808SPaul E. McKenney WRITE_ONCE(stutter_pause_test, 2); 756ed24affaSPaul E. McKenney torture_hrtimeout_jiffies(wtime, NULL); 757628edaa5SPaul E. McKenney } 7584ced3314SPaul E. McKenney WRITE_ONCE(stutter_pause_test, 0); 759628edaa5SPaul E. McKenney if (!torture_must_stop()) 760ed24affaSPaul E. McKenney torture_hrtimeout_jiffies(stutter_gap, NULL); 761628edaa5SPaul E. McKenney torture_shutdown_absorb("torture_stutter"); 762628edaa5SPaul E. McKenney } while (!torture_must_stop()); 7637fafaac5SPaul E. McKenney torture_kthread_stopping("torture_stutter"); 764628edaa5SPaul E. McKenney return 0; 765628edaa5SPaul E. McKenney } 766628edaa5SPaul E. McKenney 767628edaa5SPaul E. McKenney /* 768628edaa5SPaul E. McKenney * Initialize and kick off the torture_stutter kthread. 769628edaa5SPaul E. McKenney */ 770ff3bf92dSPaul E. McKenney int torture_stutter_init(const int s, const int sgap) 771628edaa5SPaul E. McKenney { 772628edaa5SPaul E. McKenney stutter = s; 773ff3bf92dSPaul E. McKenney stutter_gap = sgap; 7742a7d9688SPierce Griffiths return torture_create_kthread(torture_stutter, NULL, stutter_task); 775628edaa5SPaul E. McKenney } 776628edaa5SPaul E. McKenney EXPORT_SYMBOL_GPL(torture_stutter_init); 777628edaa5SPaul E. McKenney 778628edaa5SPaul E. McKenney /* 779628edaa5SPaul E. McKenney * Cleanup after the torture_stutter kthread. 780628edaa5SPaul E. McKenney */ 781bfefc73aSPaul E. McKenney static void torture_stutter_cleanup(void) 782628edaa5SPaul E. McKenney { 783628edaa5SPaul E. McKenney if (!stutter_task) 784628edaa5SPaul E. McKenney return; 785628edaa5SPaul E. McKenney VERBOSE_TOROUT_STRING("Stopping torture_stutter task"); 786628edaa5SPaul E. McKenney kthread_stop(stutter_task); 787628edaa5SPaul E. McKenney stutter_task = NULL; 788628edaa5SPaul E. McKenney } 789628edaa5SPaul E. McKenney 790628edaa5SPaul E. McKenney /* 791b5daa8f3SPaul E. McKenney * Initialize torture module. Please note that this is -not- invoked via 792b5daa8f3SPaul E. McKenney * the usual module_init() mechanism, but rather by an explicit call from 793b5daa8f3SPaul E. McKenney * the client torture module. This call must be paired with a later 794b5daa8f3SPaul E. McKenney * torture_init_end(). 795628edaa5SPaul E. McKenney * 796628edaa5SPaul E. McKenney * The runnable parameter points to a flag that controls whether or not 797628edaa5SPaul E. McKenney * the test is currently runnable. If there is no such flag, pass in NULL. 798b5daa8f3SPaul E. McKenney */ 79990127d60SPaul E. McKenney bool torture_init_begin(char *ttype, int v) 800b5daa8f3SPaul E. McKenney { 801b5daa8f3SPaul E. McKenney mutex_lock(&fullstop_mutex); 8025228084eSPaul E. McKenney if (torture_type != NULL) { 8039eb5188aSPaul E. McKenney pr_alert("torture_init_begin: Refusing %s init: %s running.\n", 8045228084eSPaul E. McKenney ttype, torture_type); 8059eb5188aSPaul E. McKenney pr_alert("torture_init_begin: One torture test at a time!\n"); 8065228084eSPaul E. McKenney mutex_unlock(&fullstop_mutex); 8075228084eSPaul E. McKenney return false; 8085228084eSPaul E. McKenney } 809b5daa8f3SPaul E. McKenney torture_type = ttype; 810b5daa8f3SPaul E. McKenney verbose = v; 81136970bb9SPaul E. McKenney fullstop = FULLSTOP_DONTSTOP; 8125228084eSPaul E. McKenney return true; 813b5daa8f3SPaul E. McKenney } 814b5daa8f3SPaul E. McKenney EXPORT_SYMBOL_GPL(torture_init_begin); 815b5daa8f3SPaul E. McKenney 816b5daa8f3SPaul E. McKenney /* 817b5daa8f3SPaul E. McKenney * Tell the torture module that initialization is complete. 818b5daa8f3SPaul E. McKenney */ 8192b3f8ffeSPranith Kumar void torture_init_end(void) 820b5daa8f3SPaul E. McKenney { 821b5daa8f3SPaul E. McKenney mutex_unlock(&fullstop_mutex); 8224622b487SPaul E. McKenney register_reboot_notifier(&torture_shutdown_nb); 823b5daa8f3SPaul E. McKenney } 824b5daa8f3SPaul E. McKenney EXPORT_SYMBOL_GPL(torture_init_end); 825cc47ae08SPaul E. McKenney 826cc47ae08SPaul E. McKenney /* 827cc47ae08SPaul E. McKenney * Clean up torture module. Please note that this is -not- invoked via 828cc47ae08SPaul E. McKenney * the usual module_exit() mechanism, but rather by an explicit call from 829cc47ae08SPaul E. McKenney * the client torture module. Returns true if a race with system shutdown 830bfefc73aSPaul E. McKenney * is detected, otherwise, all kthreads started by functions in this file 831bfefc73aSPaul E. McKenney * will be shut down. 832cc47ae08SPaul E. McKenney * 833cc47ae08SPaul E. McKenney * This must be called before the caller starts shutting down its own 834cc47ae08SPaul E. McKenney * kthreads. 835d36a7a0dSDavidlohr Bueso * 836d36a7a0dSDavidlohr Bueso * Both torture_cleanup_begin() and torture_cleanup_end() must be paired, 837d36a7a0dSDavidlohr Bueso * in order to correctly perform the cleanup. They are separated because 838d36a7a0dSDavidlohr Bueso * threads can still need to reference the torture_type type, thus nullify 839d36a7a0dSDavidlohr Bueso * only after completing all other relevant calls. 840cc47ae08SPaul E. McKenney */ 841d36a7a0dSDavidlohr Bueso bool torture_cleanup_begin(void) 842cc47ae08SPaul E. McKenney { 843cc47ae08SPaul E. McKenney mutex_lock(&fullstop_mutex); 8447d0ae808SPaul E. McKenney if (READ_ONCE(fullstop) == FULLSTOP_SHUTDOWN) { 845cc47ae08SPaul E. McKenney pr_warn("Concurrent rmmod and shutdown illegal!\n"); 846cc47ae08SPaul E. McKenney mutex_unlock(&fullstop_mutex); 847cc47ae08SPaul E. McKenney schedule_timeout_uninterruptible(10); 848cc47ae08SPaul E. McKenney return true; 849cc47ae08SPaul E. McKenney } 8507d0ae808SPaul E. McKenney WRITE_ONCE(fullstop, FULLSTOP_RMMOD); 851cc47ae08SPaul E. McKenney mutex_unlock(&fullstop_mutex); 852bfefc73aSPaul E. McKenney torture_shutdown_cleanup(); 853cc47ae08SPaul E. McKenney torture_shuffle_cleanup(); 854bfefc73aSPaul E. McKenney torture_stutter_cleanup(); 855cc47ae08SPaul E. McKenney torture_onoff_cleanup(); 856d36a7a0dSDavidlohr Bueso return false; 857d36a7a0dSDavidlohr Bueso } 858d36a7a0dSDavidlohr Bueso EXPORT_SYMBOL_GPL(torture_cleanup_begin); 859d36a7a0dSDavidlohr Bueso 860d36a7a0dSDavidlohr Bueso void torture_cleanup_end(void) 861d36a7a0dSDavidlohr Bueso { 8625228084eSPaul E. McKenney mutex_lock(&fullstop_mutex); 8635228084eSPaul E. McKenney torture_type = NULL; 8645228084eSPaul E. McKenney mutex_unlock(&fullstop_mutex); 865cc47ae08SPaul E. McKenney } 866d36a7a0dSDavidlohr Bueso EXPORT_SYMBOL_GPL(torture_cleanup_end); 86736970bb9SPaul E. McKenney 86836970bb9SPaul E. McKenney /* 86936970bb9SPaul E. McKenney * Is it time for the current torture test to stop? 87036970bb9SPaul E. McKenney */ 87136970bb9SPaul E. McKenney bool torture_must_stop(void) 87236970bb9SPaul E. McKenney { 87336970bb9SPaul E. McKenney return torture_must_stop_irq() || kthread_should_stop(); 87436970bb9SPaul E. McKenney } 87536970bb9SPaul E. McKenney EXPORT_SYMBOL_GPL(torture_must_stop); 87636970bb9SPaul E. McKenney 87736970bb9SPaul E. McKenney /* 87836970bb9SPaul E. McKenney * Is it time for the current torture test to stop? This is the irq-safe 87936970bb9SPaul E. McKenney * version, hence no check for kthread_should_stop(). 88036970bb9SPaul E. McKenney */ 88136970bb9SPaul E. McKenney bool torture_must_stop_irq(void) 88236970bb9SPaul E. McKenney { 8837d0ae808SPaul E. McKenney return READ_ONCE(fullstop) != FULLSTOP_DONTSTOP; 88436970bb9SPaul E. McKenney } 88536970bb9SPaul E. McKenney EXPORT_SYMBOL_GPL(torture_must_stop_irq); 8867fafaac5SPaul E. McKenney 8877fafaac5SPaul E. McKenney /* 8887fafaac5SPaul E. McKenney * Each kthread must wait for kthread_should_stop() before returning from 8897fafaac5SPaul E. McKenney * its top-level function, otherwise segfaults ensue. This function 8907fafaac5SPaul E. McKenney * prints a "stopping" message and waits for kthread_should_stop(), and 8917fafaac5SPaul E. McKenney * should be called from all torture kthreads immediately prior to 8927fafaac5SPaul E. McKenney * returning. 8937fafaac5SPaul E. McKenney */ 8947fafaac5SPaul E. McKenney void torture_kthread_stopping(char *title) 8957fafaac5SPaul E. McKenney { 8960d6821d5SPaul E. McKenney char buf[128]; 8970d6821d5SPaul E. McKenney 8980d6821d5SPaul E. McKenney snprintf(buf, sizeof(buf), "Stopping %s", title); 8990d6821d5SPaul E. McKenney VERBOSE_TOROUT_STRING(buf); 9007fafaac5SPaul E. McKenney while (!kthread_should_stop()) { 9017fafaac5SPaul E. McKenney torture_shutdown_absorb(title); 9027fafaac5SPaul E. McKenney schedule_timeout_uninterruptible(1); 9037fafaac5SPaul E. McKenney } 9047fafaac5SPaul E. McKenney } 9057fafaac5SPaul E. McKenney EXPORT_SYMBOL_GPL(torture_kthread_stopping); 90647cf29b9SPaul E. McKenney 90747cf29b9SPaul E. McKenney /* 90847cf29b9SPaul E. McKenney * Create a generic torture kthread that is immediately runnable. If you 90947cf29b9SPaul E. McKenney * need the kthread to be stopped so that you can do something to it before 91047cf29b9SPaul E. McKenney * it starts, you will need to open-code your own. 91147cf29b9SPaul E. McKenney */ 91247cf29b9SPaul E. McKenney int _torture_create_kthread(int (*fn)(void *arg), void *arg, char *s, char *m, 91347cf29b9SPaul E. McKenney char *f, struct task_struct **tp) 91447cf29b9SPaul E. McKenney { 91547cf29b9SPaul E. McKenney int ret = 0; 91647cf29b9SPaul E. McKenney 91747cf29b9SPaul E. McKenney VERBOSE_TOROUT_STRING(m); 9186945915eSKees Cook *tp = kthread_run(fn, arg, "%s", s); 91947cf29b9SPaul E. McKenney if (IS_ERR(*tp)) { 92047cf29b9SPaul E. McKenney ret = PTR_ERR(*tp); 92147cf29b9SPaul E. McKenney VERBOSE_TOROUT_ERRSTRING(f); 92247cf29b9SPaul E. McKenney *tp = NULL; 92347cf29b9SPaul E. McKenney } 92447cf29b9SPaul E. McKenney torture_shuffle_task_register(*tp); 92547cf29b9SPaul E. McKenney return ret; 92647cf29b9SPaul E. McKenney } 92747cf29b9SPaul E. McKenney EXPORT_SYMBOL_GPL(_torture_create_kthread); 9289c029b86SPaul E. McKenney 9299c029b86SPaul E. McKenney /* 9309c029b86SPaul E. McKenney * Stop a generic kthread, emitting a message. 9319c029b86SPaul E. McKenney */ 9329c029b86SPaul E. McKenney void _torture_stop_kthread(char *m, struct task_struct **tp) 9339c029b86SPaul E. McKenney { 9349c029b86SPaul E. McKenney if (*tp == NULL) 9359c029b86SPaul E. McKenney return; 9369c029b86SPaul E. McKenney VERBOSE_TOROUT_STRING(m); 9379c029b86SPaul E. McKenney kthread_stop(*tp); 9389c029b86SPaul E. McKenney *tp = NULL; 9399c029b86SPaul E. McKenney } 9409c029b86SPaul E. McKenney EXPORT_SYMBOL_GPL(_torture_stop_kthread); 941