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 57*e2a0b786SConnor O'Brien static int random_shuffle; 58*e2a0b786SConnor O'Brien module_param(random_shuffle, int, 0444); 59*e2a0b786SConnor O'Brien 60b5daa8f3SPaul E. McKenney static char *torture_type; 6190127d60SPaul E. McKenney static int verbose; 62b5daa8f3SPaul E. McKenney 6336970bb9SPaul E. McKenney /* Mediate rmmod and system shutdown. Concurrent rmmod & shutdown illegal! */ 6436970bb9SPaul E. McKenney #define FULLSTOP_DONTSTOP 0 /* Normal operation. */ 6536970bb9SPaul E. McKenney #define FULLSTOP_SHUTDOWN 1 /* System shutdown with torture running. */ 6636970bb9SPaul E. McKenney #define FULLSTOP_RMMOD 2 /* Normal rmmod of torture. */ 6736970bb9SPaul E. McKenney static int fullstop = FULLSTOP_RMMOD; 684622b487SPaul E. McKenney static DEFINE_MUTEX(fullstop_mutex); 69f67a3356SPaul E. McKenney 708a67a20bSPaul E. McKenney static atomic_t verbose_sleep_counter; 718a67a20bSPaul E. McKenney 728a67a20bSPaul E. McKenney /* 738a67a20bSPaul E. McKenney * Sleep if needed from VERBOSE_TOROUT*(). 748a67a20bSPaul E. McKenney */ 758a67a20bSPaul E. McKenney void verbose_torout_sleep(void) 768a67a20bSPaul E. McKenney { 778a67a20bSPaul E. McKenney if (verbose_sleep_frequency > 0 && 788a67a20bSPaul E. McKenney verbose_sleep_duration > 0 && 798a67a20bSPaul E. McKenney !(atomic_inc_return(&verbose_sleep_counter) % verbose_sleep_frequency)) 808a67a20bSPaul E. McKenney schedule_timeout_uninterruptible(verbose_sleep_duration); 818a67a20bSPaul E. McKenney } 828a67a20bSPaul E. McKenney EXPORT_SYMBOL_GPL(verbose_torout_sleep); 838a67a20bSPaul E. McKenney 84ae19aaafSPaul E. McKenney /* 85ae19aaafSPaul E. McKenney * Schedule a high-resolution-timer sleep in nanoseconds, with a 32-bit 86ae19aaafSPaul E. McKenney * nanosecond random fuzz. This function and its friends desynchronize 87ae19aaafSPaul E. McKenney * testing from the timer wheel. 88ae19aaafSPaul E. McKenney */ 89ae19aaafSPaul E. McKenney int torture_hrtimeout_ns(ktime_t baset_ns, u32 fuzzt_ns, struct torture_random_state *trsp) 90ae19aaafSPaul E. McKenney { 91ae19aaafSPaul E. McKenney ktime_t hto = baset_ns; 92ae19aaafSPaul E. McKenney 93ae19aaafSPaul E. McKenney if (trsp) 94ae19aaafSPaul E. McKenney hto += (torture_random(trsp) >> 3) % fuzzt_ns; 95ae19aaafSPaul E. McKenney set_current_state(TASK_UNINTERRUPTIBLE); 96ae19aaafSPaul E. McKenney return schedule_hrtimeout(&hto, HRTIMER_MODE_REL); 97ae19aaafSPaul E. McKenney } 98ae19aaafSPaul E. McKenney EXPORT_SYMBOL_GPL(torture_hrtimeout_ns); 99ae19aaafSPaul E. McKenney 100ae19aaafSPaul E. McKenney /* 101ae19aaafSPaul E. McKenney * Schedule a high-resolution-timer sleep in microseconds, with a 32-bit 102ae19aaafSPaul E. McKenney * nanosecond (not microsecond!) random fuzz. 103ae19aaafSPaul E. McKenney */ 104ae19aaafSPaul E. McKenney int torture_hrtimeout_us(u32 baset_us, u32 fuzzt_ns, struct torture_random_state *trsp) 105ae19aaafSPaul E. McKenney { 106ae19aaafSPaul E. McKenney ktime_t baset_ns = baset_us * NSEC_PER_USEC; 107ae19aaafSPaul E. McKenney 108ae19aaafSPaul E. McKenney return torture_hrtimeout_ns(baset_ns, fuzzt_ns, trsp); 109ae19aaafSPaul E. McKenney } 110ae19aaafSPaul E. McKenney EXPORT_SYMBOL_GPL(torture_hrtimeout_us); 111ae19aaafSPaul E. McKenney 112ae19aaafSPaul E. McKenney /* 113ae19aaafSPaul E. McKenney * Schedule a high-resolution-timer sleep in milliseconds, with a 32-bit 114ae19aaafSPaul E. McKenney * microsecond (not millisecond!) random fuzz. 115ae19aaafSPaul E. McKenney */ 116ae19aaafSPaul E. McKenney int torture_hrtimeout_ms(u32 baset_ms, u32 fuzzt_us, struct torture_random_state *trsp) 117ae19aaafSPaul E. McKenney { 118ae19aaafSPaul E. McKenney ktime_t baset_ns = baset_ms * NSEC_PER_MSEC; 119ae19aaafSPaul E. McKenney u32 fuzzt_ns; 120ae19aaafSPaul E. McKenney 121ae19aaafSPaul E. McKenney if ((u32)~0U / NSEC_PER_USEC < fuzzt_us) 122ae19aaafSPaul E. McKenney fuzzt_ns = (u32)~0U; 123ae19aaafSPaul E. McKenney else 124ae19aaafSPaul E. McKenney fuzzt_ns = fuzzt_us * NSEC_PER_USEC; 125ae19aaafSPaul E. McKenney return torture_hrtimeout_ns(baset_ns, fuzzt_ns, trsp); 126ae19aaafSPaul E. McKenney } 127ae19aaafSPaul E. McKenney EXPORT_SYMBOL_GPL(torture_hrtimeout_ms); 128ae19aaafSPaul E. McKenney 129ae19aaafSPaul E. McKenney /* 130ae19aaafSPaul E. McKenney * Schedule a high-resolution-timer sleep in jiffies, with an 131ae19aaafSPaul E. McKenney * implied one-jiffy random fuzz. This is intended to replace calls to 132ae19aaafSPaul E. McKenney * schedule_timeout_interruptible() and friends. 133ae19aaafSPaul E. McKenney */ 134ae19aaafSPaul E. McKenney int torture_hrtimeout_jiffies(u32 baset_j, struct torture_random_state *trsp) 135ae19aaafSPaul E. McKenney { 136ae19aaafSPaul E. McKenney ktime_t baset_ns = jiffies_to_nsecs(baset_j); 137ae19aaafSPaul E. McKenney 138ae19aaafSPaul E. McKenney return torture_hrtimeout_ns(baset_ns, jiffies_to_nsecs(1), trsp); 139ae19aaafSPaul E. McKenney } 140ae19aaafSPaul E. McKenney EXPORT_SYMBOL_GPL(torture_hrtimeout_jiffies); 141ae19aaafSPaul E. McKenney 142ae19aaafSPaul E. McKenney /* 143ae19aaafSPaul E. McKenney * Schedule a high-resolution-timer sleep in milliseconds, with a 32-bit 144ae19aaafSPaul E. McKenney * millisecond (not second!) random fuzz. 145ae19aaafSPaul E. McKenney */ 146ae19aaafSPaul E. McKenney int torture_hrtimeout_s(u32 baset_s, u32 fuzzt_ms, struct torture_random_state *trsp) 147ae19aaafSPaul E. McKenney { 148ae19aaafSPaul E. McKenney ktime_t baset_ns = baset_s * NSEC_PER_SEC; 149ae19aaafSPaul E. McKenney u32 fuzzt_ns; 150ae19aaafSPaul E. McKenney 151ae19aaafSPaul E. McKenney if ((u32)~0U / NSEC_PER_MSEC < fuzzt_ms) 152ae19aaafSPaul E. McKenney fuzzt_ns = (u32)~0U; 153ae19aaafSPaul E. McKenney else 154ae19aaafSPaul E. McKenney fuzzt_ns = fuzzt_ms * NSEC_PER_MSEC; 155ae19aaafSPaul E. McKenney return torture_hrtimeout_ns(baset_ns, fuzzt_ns, trsp); 156ae19aaafSPaul E. McKenney } 157ae19aaafSPaul E. McKenney EXPORT_SYMBOL_GPL(torture_hrtimeout_s); 158ae19aaafSPaul E. McKenney 1592e9e8081SPaul E. McKenney #ifdef CONFIG_HOTPLUG_CPU 1602e9e8081SPaul E. McKenney 1612e9e8081SPaul E. McKenney /* 1622e9e8081SPaul E. McKenney * Variables for online-offline handling. Only present if CPU hotplug 1632e9e8081SPaul E. McKenney * is enabled, otherwise does nothing. 1642e9e8081SPaul E. McKenney */ 1652e9e8081SPaul E. McKenney 1662e9e8081SPaul E. McKenney static struct task_struct *onoff_task; 1672e9e8081SPaul E. McKenney static long onoff_holdoff; 1682e9e8081SPaul E. McKenney static long onoff_interval; 1693a6cb58fSPaul E. McKenney static torture_ofl_func *onoff_f; 1702e9e8081SPaul E. McKenney static long n_offline_attempts; 1712e9e8081SPaul E. McKenney static long n_offline_successes; 1722e9e8081SPaul E. McKenney static unsigned long sum_offline; 1732e9e8081SPaul E. McKenney static int min_offline = -1; 1742e9e8081SPaul E. McKenney static int max_offline; 1752e9e8081SPaul E. McKenney static long n_online_attempts; 1762e9e8081SPaul E. McKenney static long n_online_successes; 1772e9e8081SPaul E. McKenney static unsigned long sum_online; 1782e9e8081SPaul E. McKenney static int min_online = -1; 1792e9e8081SPaul E. McKenney static int max_online; 1802e9e8081SPaul E. McKenney 1811afb95feSPaul E. McKenney static int torture_online_cpus = NR_CPUS; 1821afb95feSPaul E. McKenney 1831afb95feSPaul E. McKenney /* 1841afb95feSPaul E. McKenney * Some torture testing leverages confusion as to the number of online 1851afb95feSPaul E. McKenney * CPUs. This function returns the torture-testing view of this number, 1861afb95feSPaul E. McKenney * which allows torture tests to load-balance appropriately. 1871afb95feSPaul E. McKenney */ 1881afb95feSPaul E. McKenney int torture_num_online_cpus(void) 1891afb95feSPaul E. McKenney { 1901afb95feSPaul E. McKenney return READ_ONCE(torture_online_cpus); 1911afb95feSPaul E. McKenney } 1921afb95feSPaul E. McKenney EXPORT_SYMBOL_GPL(torture_num_online_cpus); 1931afb95feSPaul E. McKenney 1942e9e8081SPaul E. McKenney /* 195d95f5ba9SPaul E. McKenney * Attempt to take a CPU offline. Return false if the CPU is already 196d95f5ba9SPaul E. McKenney * offline or if it is not subject to CPU-hotplug operations. The 197d95f5ba9SPaul E. McKenney * caller can detect other failures by looking at the statistics. 198d95f5ba9SPaul E. McKenney */ 199d95f5ba9SPaul E. McKenney bool torture_offline(int cpu, long *n_offl_attempts, long *n_offl_successes, 200d95f5ba9SPaul E. McKenney unsigned long *sum_offl, int *min_offl, int *max_offl) 201d95f5ba9SPaul E. McKenney { 202d95f5ba9SPaul E. McKenney unsigned long delta; 203d95f5ba9SPaul E. McKenney int ret; 204a59ee765SPaul E. McKenney char *s; 205d95f5ba9SPaul E. McKenney unsigned long starttime; 206d95f5ba9SPaul E. McKenney 207d95f5ba9SPaul E. McKenney if (!cpu_online(cpu) || !cpu_is_hotpluggable(cpu)) 208d95f5ba9SPaul E. McKenney return false; 20924aca4aeSPaul E. McKenney if (num_online_cpus() <= 1) 21024aca4aeSPaul E. McKenney return false; /* Can't offline the last CPU. */ 211d95f5ba9SPaul E. McKenney 21290127d60SPaul E. McKenney if (verbose > 1) 213d95f5ba9SPaul E. McKenney pr_alert("%s" TORTURE_FLAG 214d95f5ba9SPaul E. McKenney "torture_onoff task: offlining %d\n", 215d95f5ba9SPaul E. McKenney torture_type, cpu); 216d95f5ba9SPaul E. McKenney starttime = jiffies; 217d95f5ba9SPaul E. McKenney (*n_offl_attempts)++; 218457bc8edSQais Yousef ret = remove_cpu(cpu); 219d95f5ba9SPaul E. McKenney if (ret) { 220a59ee765SPaul E. McKenney s = ""; 221a59ee765SPaul E. McKenney if (!rcu_inkernel_boot_has_ended() && ret == -EBUSY) { 222a59ee765SPaul E. McKenney // PCI probe frequently disables hotplug during boot. 223a59ee765SPaul E. McKenney (*n_offl_attempts)--; 224a59ee765SPaul E. McKenney s = " (-EBUSY forgiven during boot)"; 225a59ee765SPaul E. McKenney } 226d95f5ba9SPaul E. McKenney if (verbose) 227d95f5ba9SPaul E. McKenney pr_alert("%s" TORTURE_FLAG 228a59ee765SPaul E. McKenney "torture_onoff task: offline %d failed%s: errno %d\n", 229a59ee765SPaul E. McKenney torture_type, cpu, s, ret); 230d95f5ba9SPaul E. McKenney } else { 23190127d60SPaul E. McKenney if (verbose > 1) 232d95f5ba9SPaul E. McKenney pr_alert("%s" TORTURE_FLAG 233d95f5ba9SPaul E. McKenney "torture_onoff task: offlined %d\n", 234d95f5ba9SPaul E. McKenney torture_type, cpu); 2353a6cb58fSPaul E. McKenney if (onoff_f) 2363a6cb58fSPaul E. McKenney onoff_f(); 237d95f5ba9SPaul E. McKenney (*n_offl_successes)++; 238d95f5ba9SPaul E. McKenney delta = jiffies - starttime; 239a2b2df20SPaul E. McKenney *sum_offl += delta; 240d95f5ba9SPaul E. McKenney if (*min_offl < 0) { 241d95f5ba9SPaul E. McKenney *min_offl = delta; 242d95f5ba9SPaul E. McKenney *max_offl = delta; 243d95f5ba9SPaul E. McKenney } 244d95f5ba9SPaul E. McKenney if (*min_offl > delta) 245d95f5ba9SPaul E. McKenney *min_offl = delta; 246d95f5ba9SPaul E. McKenney if (*max_offl < delta) 247d95f5ba9SPaul E. McKenney *max_offl = delta; 2481afb95feSPaul E. McKenney WRITE_ONCE(torture_online_cpus, torture_online_cpus - 1); 2491afb95feSPaul E. McKenney WARN_ON_ONCE(torture_online_cpus <= 0); 250d95f5ba9SPaul E. McKenney } 251d95f5ba9SPaul E. McKenney 252d95f5ba9SPaul E. McKenney return true; 253d95f5ba9SPaul E. McKenney } 254d95f5ba9SPaul E. McKenney EXPORT_SYMBOL_GPL(torture_offline); 255d95f5ba9SPaul E. McKenney 256d95f5ba9SPaul E. McKenney /* 257d95f5ba9SPaul E. McKenney * Attempt to bring a CPU online. Return false if the CPU is already 258d95f5ba9SPaul E. McKenney * online or if it is not subject to CPU-hotplug operations. The 259d95f5ba9SPaul E. McKenney * caller can detect other failures by looking at the statistics. 260d95f5ba9SPaul E. McKenney */ 261d95f5ba9SPaul E. McKenney bool torture_online(int cpu, long *n_onl_attempts, long *n_onl_successes, 262d95f5ba9SPaul E. McKenney unsigned long *sum_onl, int *min_onl, int *max_onl) 263d95f5ba9SPaul E. McKenney { 264d95f5ba9SPaul E. McKenney unsigned long delta; 265d95f5ba9SPaul E. McKenney int ret; 266a59ee765SPaul E. McKenney char *s; 267d95f5ba9SPaul E. McKenney unsigned long starttime; 268d95f5ba9SPaul E. McKenney 269d95f5ba9SPaul E. McKenney if (cpu_online(cpu) || !cpu_is_hotpluggable(cpu)) 270d95f5ba9SPaul E. McKenney return false; 271d95f5ba9SPaul E. McKenney 27290127d60SPaul E. McKenney if (verbose > 1) 273d95f5ba9SPaul E. McKenney pr_alert("%s" TORTURE_FLAG 274d95f5ba9SPaul E. McKenney "torture_onoff task: onlining %d\n", 275d95f5ba9SPaul E. McKenney torture_type, cpu); 276d95f5ba9SPaul E. McKenney starttime = jiffies; 277d95f5ba9SPaul E. McKenney (*n_onl_attempts)++; 278457bc8edSQais Yousef ret = add_cpu(cpu); 279d95f5ba9SPaul E. McKenney if (ret) { 280a59ee765SPaul E. McKenney s = ""; 281a59ee765SPaul E. McKenney if (!rcu_inkernel_boot_has_ended() && ret == -EBUSY) { 282a59ee765SPaul E. McKenney // PCI probe frequently disables hotplug during boot. 283a59ee765SPaul E. McKenney (*n_onl_attempts)--; 284a59ee765SPaul E. McKenney s = " (-EBUSY forgiven during boot)"; 285a59ee765SPaul E. McKenney } 286d95f5ba9SPaul E. McKenney if (verbose) 287d95f5ba9SPaul E. McKenney pr_alert("%s" TORTURE_FLAG 288a59ee765SPaul E. McKenney "torture_onoff task: online %d failed%s: errno %d\n", 289a59ee765SPaul E. McKenney torture_type, cpu, s, ret); 290d95f5ba9SPaul E. McKenney } else { 29190127d60SPaul E. McKenney if (verbose > 1) 292d95f5ba9SPaul E. McKenney pr_alert("%s" TORTURE_FLAG 293d95f5ba9SPaul E. McKenney "torture_onoff task: onlined %d\n", 294d95f5ba9SPaul E. McKenney torture_type, cpu); 295d95f5ba9SPaul E. McKenney (*n_onl_successes)++; 296d95f5ba9SPaul E. McKenney delta = jiffies - starttime; 297d95f5ba9SPaul E. McKenney *sum_onl += delta; 298d95f5ba9SPaul E. McKenney if (*min_onl < 0) { 299d95f5ba9SPaul E. McKenney *min_onl = delta; 300d95f5ba9SPaul E. McKenney *max_onl = delta; 301d95f5ba9SPaul E. McKenney } 302d95f5ba9SPaul E. McKenney if (*min_onl > delta) 303d95f5ba9SPaul E. McKenney *min_onl = delta; 304d95f5ba9SPaul E. McKenney if (*max_onl < delta) 305d95f5ba9SPaul E. McKenney *max_onl = delta; 3061afb95feSPaul E. McKenney WRITE_ONCE(torture_online_cpus, torture_online_cpus + 1); 307d95f5ba9SPaul E. McKenney } 308d95f5ba9SPaul E. McKenney 309d95f5ba9SPaul E. McKenney return true; 310d95f5ba9SPaul E. McKenney } 311d95f5ba9SPaul E. McKenney EXPORT_SYMBOL_GPL(torture_online); 312d95f5ba9SPaul E. McKenney 313d95f5ba9SPaul E. McKenney /* 3140b962c8fSPaul E. McKenney * Get everything online at the beginning and ends of tests. 3150b962c8fSPaul E. McKenney */ 3160b962c8fSPaul E. McKenney static void torture_online_all(char *phase) 3170b962c8fSPaul E. McKenney { 3180b962c8fSPaul E. McKenney int cpu; 3190b962c8fSPaul E. McKenney int ret; 3200b962c8fSPaul E. McKenney 3210b962c8fSPaul E. McKenney for_each_possible_cpu(cpu) { 3220b962c8fSPaul E. McKenney if (cpu_online(cpu)) 3230b962c8fSPaul E. McKenney continue; 3240b962c8fSPaul E. McKenney ret = add_cpu(cpu); 3250b962c8fSPaul E. McKenney if (ret && verbose) { 3260b962c8fSPaul E. McKenney pr_alert("%s" TORTURE_FLAG 3270b962c8fSPaul E. McKenney "%s: %s online %d: errno %d\n", 3280b962c8fSPaul E. McKenney __func__, phase, torture_type, cpu, ret); 3290b962c8fSPaul E. McKenney } 3300b962c8fSPaul E. McKenney } 3310b962c8fSPaul E. McKenney } 3320b962c8fSPaul E. McKenney 3330b962c8fSPaul E. McKenney /* 3342e9e8081SPaul E. McKenney * Execute random CPU-hotplug operations at the interval specified 3352e9e8081SPaul E. McKenney * by the onoff_interval. 3362e9e8081SPaul E. McKenney */ 3372e9e8081SPaul E. McKenney static int 3382e9e8081SPaul E. McKenney torture_onoff(void *arg) 3392e9e8081SPaul E. McKenney { 3402e9e8081SPaul E. McKenney int cpu; 3412e9e8081SPaul E. McKenney int maxcpu = -1; 3422e9e8081SPaul E. McKenney DEFINE_TORTURE_RANDOM(rand); 3432e9e8081SPaul E. McKenney 3442e9e8081SPaul E. McKenney VERBOSE_TOROUT_STRING("torture_onoff task started"); 3452e9e8081SPaul E. McKenney for_each_online_cpu(cpu) 3462e9e8081SPaul E. McKenney maxcpu = cpu; 3472e9e8081SPaul E. McKenney WARN_ON(maxcpu < 0); 3480b962c8fSPaul E. McKenney torture_online_all("Initial"); 349750db0f5SBoqun Feng if (maxcpu == 0) { 350750db0f5SBoqun Feng VERBOSE_TOROUT_STRING("Only one CPU, so CPU-hotplug testing is disabled"); 351750db0f5SBoqun Feng goto stop; 352750db0f5SBoqun Feng } 353750db0f5SBoqun Feng 3542e9e8081SPaul E. McKenney if (onoff_holdoff > 0) { 3552e9e8081SPaul E. McKenney VERBOSE_TOROUT_STRING("torture_onoff begin holdoff"); 3562e9e8081SPaul E. McKenney schedule_timeout_interruptible(onoff_holdoff); 3572e9e8081SPaul E. McKenney VERBOSE_TOROUT_STRING("torture_onoff end holdoff"); 3582e9e8081SPaul E. McKenney } 3592e9e8081SPaul E. McKenney while (!torture_must_stop()) { 3608171d3e0SPaul E. McKenney if (disable_onoff_at_boot && !rcu_inkernel_boot_has_ended()) { 3618171d3e0SPaul E. McKenney schedule_timeout_interruptible(HZ / 10); 3628171d3e0SPaul E. McKenney continue; 3638171d3e0SPaul E. McKenney } 3642e9e8081SPaul E. McKenney cpu = (torture_random(&rand) >> 4) % (maxcpu + 1); 365d95f5ba9SPaul E. McKenney if (!torture_offline(cpu, 366d95f5ba9SPaul E. McKenney &n_offline_attempts, &n_offline_successes, 367d95f5ba9SPaul E. McKenney &sum_offline, &min_offline, &max_offline)) 368d95f5ba9SPaul E. McKenney torture_online(cpu, 369d95f5ba9SPaul E. McKenney &n_online_attempts, &n_online_successes, 370d95f5ba9SPaul E. McKenney &sum_online, &min_online, &max_online); 3712e9e8081SPaul E. McKenney schedule_timeout_interruptible(onoff_interval); 3722e9e8081SPaul E. McKenney } 373750db0f5SBoqun Feng 374750db0f5SBoqun Feng stop: 3757fafaac5SPaul E. McKenney torture_kthread_stopping("torture_onoff"); 3760b962c8fSPaul E. McKenney torture_online_all("Final"); 3772e9e8081SPaul E. McKenney return 0; 3782e9e8081SPaul E. McKenney } 3792e9e8081SPaul E. McKenney 3802e9e8081SPaul E. McKenney #endif /* #ifdef CONFIG_HOTPLUG_CPU */ 3812e9e8081SPaul E. McKenney 3822e9e8081SPaul E. McKenney /* 3832e9e8081SPaul E. McKenney * Initiate online-offline handling. 3842e9e8081SPaul E. McKenney */ 3853a6cb58fSPaul E. McKenney int torture_onoff_init(long ooholdoff, long oointerval, torture_ofl_func *f) 3862e9e8081SPaul E. McKenney { 38747cf29b9SPaul E. McKenney #ifdef CONFIG_HOTPLUG_CPU 3882e9e8081SPaul E. McKenney onoff_holdoff = ooholdoff; 3892e9e8081SPaul E. McKenney onoff_interval = oointerval; 3903a6cb58fSPaul E. McKenney onoff_f = f; 3912e9e8081SPaul E. McKenney if (onoff_interval <= 0) 3922e9e8081SPaul E. McKenney return 0; 3932a7d9688SPierce Griffiths return torture_create_kthread(torture_onoff, NULL, onoff_task); 3942a7d9688SPierce Griffiths #else /* #ifdef CONFIG_HOTPLUG_CPU */ 3952a7d9688SPierce Griffiths return 0; 3962a7d9688SPierce Griffiths #endif /* #else #ifdef CONFIG_HOTPLUG_CPU */ 3972e9e8081SPaul E. McKenney } 3982e9e8081SPaul E. McKenney EXPORT_SYMBOL_GPL(torture_onoff_init); 3992e9e8081SPaul E. McKenney 4002e9e8081SPaul E. McKenney /* 4012e9e8081SPaul E. McKenney * Clean up after online/offline testing. 4022e9e8081SPaul E. McKenney */ 403cc47ae08SPaul E. McKenney static void torture_onoff_cleanup(void) 4042e9e8081SPaul E. McKenney { 4052e9e8081SPaul E. McKenney #ifdef CONFIG_HOTPLUG_CPU 4062e9e8081SPaul E. McKenney if (onoff_task == NULL) 4072e9e8081SPaul E. McKenney return; 4082e9e8081SPaul E. McKenney VERBOSE_TOROUT_STRING("Stopping torture_onoff task"); 4092e9e8081SPaul E. McKenney kthread_stop(onoff_task); 4102e9e8081SPaul E. McKenney onoff_task = NULL; 4112e9e8081SPaul E. McKenney #endif /* #ifdef CONFIG_HOTPLUG_CPU */ 4122e9e8081SPaul E. McKenney } 4132e9e8081SPaul E. McKenney 4142e9e8081SPaul E. McKenney /* 4152e9e8081SPaul E. McKenney * Print online/offline testing statistics. 4162e9e8081SPaul E. McKenney */ 417eea203feSJoe Perches void torture_onoff_stats(void) 4182e9e8081SPaul E. McKenney { 4192e9e8081SPaul E. McKenney #ifdef CONFIG_HOTPLUG_CPU 420eea203feSJoe Perches pr_cont("onoff: %ld/%ld:%ld/%ld %d,%d:%d,%d %lu:%lu (HZ=%d) ", 4212e9e8081SPaul E. McKenney n_online_successes, n_online_attempts, 4222e9e8081SPaul E. McKenney n_offline_successes, n_offline_attempts, 4232e9e8081SPaul E. McKenney min_online, max_online, 4242e9e8081SPaul E. McKenney min_offline, max_offline, 4252e9e8081SPaul E. McKenney sum_online, sum_offline, HZ); 4262e9e8081SPaul E. McKenney #endif /* #ifdef CONFIG_HOTPLUG_CPU */ 4272e9e8081SPaul E. McKenney } 4282e9e8081SPaul E. McKenney EXPORT_SYMBOL_GPL(torture_onoff_stats); 4292e9e8081SPaul E. McKenney 4302e9e8081SPaul E. McKenney /* 4312e9e8081SPaul E. McKenney * Were all the online/offline operations successful? 4322e9e8081SPaul E. McKenney */ 4332e9e8081SPaul E. McKenney bool torture_onoff_failures(void) 4342e9e8081SPaul E. McKenney { 4352e9e8081SPaul E. McKenney #ifdef CONFIG_HOTPLUG_CPU 4362e9e8081SPaul E. McKenney return n_online_successes != n_online_attempts || 4372e9e8081SPaul E. McKenney n_offline_successes != n_offline_attempts; 4382e9e8081SPaul E. McKenney #else /* #ifdef CONFIG_HOTPLUG_CPU */ 4392e9e8081SPaul E. McKenney return false; 4402e9e8081SPaul E. McKenney #endif /* #else #ifdef CONFIG_HOTPLUG_CPU */ 4412e9e8081SPaul E. McKenney } 4422e9e8081SPaul E. McKenney EXPORT_SYMBOL_GPL(torture_onoff_failures); 4432e9e8081SPaul E. McKenney 44451b1130eSPaul E. McKenney #define TORTURE_RANDOM_MULT 39916801 /* prime */ 44551b1130eSPaul E. McKenney #define TORTURE_RANDOM_ADD 479001701 /* prime */ 44651b1130eSPaul E. McKenney #define TORTURE_RANDOM_REFRESH 10000 44751b1130eSPaul E. McKenney 44851b1130eSPaul E. McKenney /* 44951b1130eSPaul E. McKenney * Crude but fast random-number generator. Uses a linear congruential 45051b1130eSPaul E. McKenney * generator, with occasional help from cpu_clock(). 45151b1130eSPaul E. McKenney */ 45251b1130eSPaul E. McKenney unsigned long 45351b1130eSPaul E. McKenney torture_random(struct torture_random_state *trsp) 45451b1130eSPaul E. McKenney { 45551b1130eSPaul E. McKenney if (--trsp->trs_count < 0) { 45695f93e97SPaul E. McKenney trsp->trs_state += (unsigned long)local_clock() + raw_smp_processor_id(); 45751b1130eSPaul E. McKenney trsp->trs_count = TORTURE_RANDOM_REFRESH; 45851b1130eSPaul E. McKenney } 45951b1130eSPaul E. McKenney trsp->trs_state = trsp->trs_state * TORTURE_RANDOM_MULT + 46051b1130eSPaul E. McKenney TORTURE_RANDOM_ADD; 46151b1130eSPaul E. McKenney return swahw32(trsp->trs_state); 46251b1130eSPaul E. McKenney } 46351b1130eSPaul E. McKenney EXPORT_SYMBOL_GPL(torture_random); 464f67a3356SPaul E. McKenney 465f67a3356SPaul E. McKenney /* 4663808dc9fSPaul E. McKenney * Variables for shuffling. The idea is to ensure that each CPU stays 4673808dc9fSPaul E. McKenney * idle for an extended period to test interactions with dyntick idle, 468b564d62eSMasahiro Yamada * as well as interactions with any per-CPU variables. 4693808dc9fSPaul E. McKenney */ 4703808dc9fSPaul E. McKenney struct shuffle_task { 4713808dc9fSPaul E. McKenney struct list_head st_l; 4723808dc9fSPaul E. McKenney struct task_struct *st_t; 4733808dc9fSPaul E. McKenney }; 4743808dc9fSPaul E. McKenney 4753808dc9fSPaul E. McKenney static long shuffle_interval; /* In jiffies. */ 4763808dc9fSPaul E. McKenney static struct task_struct *shuffler_task; 4773808dc9fSPaul E. McKenney static cpumask_var_t shuffle_tmp_mask; 4783808dc9fSPaul E. McKenney static int shuffle_idle_cpu; /* Force all torture tasks off this CPU */ 4793808dc9fSPaul E. McKenney static struct list_head shuffle_task_list = LIST_HEAD_INIT(shuffle_task_list); 4803808dc9fSPaul E. McKenney static DEFINE_MUTEX(shuffle_task_mutex); 4813808dc9fSPaul E. McKenney 4823808dc9fSPaul E. McKenney /* 4833808dc9fSPaul E. McKenney * Register a task to be shuffled. If there is no memory, just splat 4843808dc9fSPaul E. McKenney * and don't bother registering. 4853808dc9fSPaul E. McKenney */ 4863808dc9fSPaul E. McKenney void torture_shuffle_task_register(struct task_struct *tp) 4873808dc9fSPaul E. McKenney { 4883808dc9fSPaul E. McKenney struct shuffle_task *stp; 4893808dc9fSPaul E. McKenney 4903808dc9fSPaul E. McKenney if (WARN_ON_ONCE(tp == NULL)) 4913808dc9fSPaul E. McKenney return; 4923808dc9fSPaul E. McKenney stp = kmalloc(sizeof(*stp), GFP_KERNEL); 4933808dc9fSPaul E. McKenney if (WARN_ON_ONCE(stp == NULL)) 4943808dc9fSPaul E. McKenney return; 4953808dc9fSPaul E. McKenney stp->st_t = tp; 4963808dc9fSPaul E. McKenney mutex_lock(&shuffle_task_mutex); 4973808dc9fSPaul E. McKenney list_add(&stp->st_l, &shuffle_task_list); 4983808dc9fSPaul E. McKenney mutex_unlock(&shuffle_task_mutex); 4993808dc9fSPaul E. McKenney } 5003808dc9fSPaul E. McKenney EXPORT_SYMBOL_GPL(torture_shuffle_task_register); 5013808dc9fSPaul E. McKenney 5023808dc9fSPaul E. McKenney /* 5033808dc9fSPaul E. McKenney * Unregister all tasks, for example, at the end of the torture run. 5043808dc9fSPaul E. McKenney */ 5053808dc9fSPaul E. McKenney static void torture_shuffle_task_unregister_all(void) 5063808dc9fSPaul E. McKenney { 5073808dc9fSPaul E. McKenney struct shuffle_task *stp; 5083808dc9fSPaul E. McKenney struct shuffle_task *p; 5093808dc9fSPaul E. McKenney 5103808dc9fSPaul E. McKenney mutex_lock(&shuffle_task_mutex); 5113808dc9fSPaul E. McKenney list_for_each_entry_safe(stp, p, &shuffle_task_list, st_l) { 5123808dc9fSPaul E. McKenney list_del(&stp->st_l); 5133808dc9fSPaul E. McKenney kfree(stp); 5143808dc9fSPaul E. McKenney } 5153808dc9fSPaul E. McKenney mutex_unlock(&shuffle_task_mutex); 5163808dc9fSPaul E. McKenney } 5173808dc9fSPaul E. McKenney 5183808dc9fSPaul E. McKenney /* Shuffle tasks such that we allow shuffle_idle_cpu to become idle. 5193808dc9fSPaul E. McKenney * A special case is when shuffle_idle_cpu = -1, in which case we allow 5203808dc9fSPaul E. McKenney * the tasks to run on all CPUs. 5213808dc9fSPaul E. McKenney */ 5223808dc9fSPaul E. McKenney static void torture_shuffle_tasks(void) 5233808dc9fSPaul E. McKenney { 524*e2a0b786SConnor O'Brien DEFINE_TORTURE_RANDOM(rand); 5253808dc9fSPaul E. McKenney struct shuffle_task *stp; 5263808dc9fSPaul E. McKenney 5273808dc9fSPaul E. McKenney cpumask_setall(shuffle_tmp_mask); 528ed4fa244SSebastian Andrzej Siewior cpus_read_lock(); 5293808dc9fSPaul E. McKenney 5303808dc9fSPaul E. McKenney /* No point in shuffling if there is only one online CPU (ex: UP) */ 5313808dc9fSPaul E. McKenney if (num_online_cpus() == 1) { 532ed4fa244SSebastian Andrzej Siewior cpus_read_unlock(); 5333808dc9fSPaul E. McKenney return; 5343808dc9fSPaul E. McKenney } 5353808dc9fSPaul E. McKenney 5363808dc9fSPaul E. McKenney /* Advance to the next CPU. Upon overflow, don't idle any CPUs. */ 5373808dc9fSPaul E. McKenney shuffle_idle_cpu = cpumask_next(shuffle_idle_cpu, shuffle_tmp_mask); 5383808dc9fSPaul E. McKenney if (shuffle_idle_cpu >= nr_cpu_ids) 5393808dc9fSPaul E. McKenney shuffle_idle_cpu = -1; 5405ed63b19SIulia Manda else 5413808dc9fSPaul E. McKenney cpumask_clear_cpu(shuffle_idle_cpu, shuffle_tmp_mask); 5423808dc9fSPaul E. McKenney 5433808dc9fSPaul E. McKenney mutex_lock(&shuffle_task_mutex); 544*e2a0b786SConnor O'Brien list_for_each_entry(stp, &shuffle_task_list, st_l) { 545*e2a0b786SConnor O'Brien if (!random_shuffle || torture_random(&rand) & 0x1) 5463808dc9fSPaul E. McKenney set_cpus_allowed_ptr(stp->st_t, shuffle_tmp_mask); 547*e2a0b786SConnor O'Brien } 5483808dc9fSPaul E. McKenney mutex_unlock(&shuffle_task_mutex); 5493808dc9fSPaul E. McKenney 550ed4fa244SSebastian Andrzej Siewior cpus_read_unlock(); 5513808dc9fSPaul E. McKenney } 5523808dc9fSPaul E. McKenney 5533808dc9fSPaul E. McKenney /* Shuffle tasks across CPUs, with the intent of allowing each CPU in the 5543808dc9fSPaul E. McKenney * system to become idle at a time and cut off its timer ticks. This is meant 5553808dc9fSPaul E. McKenney * to test the support for such tickless idle CPU in RCU. 5563808dc9fSPaul E. McKenney */ 5573808dc9fSPaul E. McKenney static int torture_shuffle(void *arg) 5583808dc9fSPaul E. McKenney { 5593808dc9fSPaul E. McKenney VERBOSE_TOROUT_STRING("torture_shuffle task started"); 5603808dc9fSPaul E. McKenney do { 5613808dc9fSPaul E. McKenney schedule_timeout_interruptible(shuffle_interval); 5623808dc9fSPaul E. McKenney torture_shuffle_tasks(); 5633808dc9fSPaul E. McKenney torture_shutdown_absorb("torture_shuffle"); 5643808dc9fSPaul E. McKenney } while (!torture_must_stop()); 5657fafaac5SPaul E. McKenney torture_kthread_stopping("torture_shuffle"); 5663808dc9fSPaul E. McKenney return 0; 5673808dc9fSPaul E. McKenney } 5683808dc9fSPaul E. McKenney 5693808dc9fSPaul E. McKenney /* 5703808dc9fSPaul E. McKenney * Start the shuffler, with shuffint in jiffies. 5713808dc9fSPaul E. McKenney */ 5723808dc9fSPaul E. McKenney int torture_shuffle_init(long shuffint) 5733808dc9fSPaul E. McKenney { 5743808dc9fSPaul E. McKenney shuffle_interval = shuffint; 5753808dc9fSPaul E. McKenney 5763808dc9fSPaul E. McKenney shuffle_idle_cpu = -1; 5773808dc9fSPaul E. McKenney 5783808dc9fSPaul E. McKenney if (!alloc_cpumask_var(&shuffle_tmp_mask, GFP_KERNEL)) { 57981faa4f6SLi Zhijian TOROUT_ERRSTRING("Failed to alloc mask"); 5803808dc9fSPaul E. McKenney return -ENOMEM; 5813808dc9fSPaul E. McKenney } 5823808dc9fSPaul E. McKenney 5833808dc9fSPaul E. McKenney /* Create the shuffler thread */ 58447cf29b9SPaul E. McKenney return torture_create_kthread(torture_shuffle, NULL, shuffler_task); 5853808dc9fSPaul E. McKenney } 5863808dc9fSPaul E. McKenney EXPORT_SYMBOL_GPL(torture_shuffle_init); 5873808dc9fSPaul E. McKenney 5883808dc9fSPaul E. McKenney /* 5893808dc9fSPaul E. McKenney * Stop the shuffling. 5903808dc9fSPaul E. McKenney */ 591cc47ae08SPaul E. McKenney static void torture_shuffle_cleanup(void) 5923808dc9fSPaul E. McKenney { 5933808dc9fSPaul E. McKenney torture_shuffle_task_unregister_all(); 5943808dc9fSPaul E. McKenney if (shuffler_task) { 5953808dc9fSPaul E. McKenney VERBOSE_TOROUT_STRING("Stopping torture_shuffle task"); 5963808dc9fSPaul E. McKenney kthread_stop(shuffler_task); 5973808dc9fSPaul E. McKenney free_cpumask_var(shuffle_tmp_mask); 5983808dc9fSPaul E. McKenney } 5993808dc9fSPaul E. McKenney shuffler_task = NULL; 6003808dc9fSPaul E. McKenney } 6013808dc9fSPaul E. McKenney 6023808dc9fSPaul E. McKenney /* 603e991dbc0SPaul E. McKenney * Variables for auto-shutdown. This allows "lights out" torture runs 604e991dbc0SPaul E. McKenney * to be fully scripted. 605e991dbc0SPaul E. McKenney */ 606e991dbc0SPaul E. McKenney static struct task_struct *shutdown_task; 60731257c3cSPaul E. McKenney static ktime_t shutdown_time; /* time to system shutdown. */ 608e991dbc0SPaul E. McKenney static void (*torture_shutdown_hook)(void); 609e991dbc0SPaul E. McKenney 610e991dbc0SPaul E. McKenney /* 611f67a3356SPaul E. McKenney * Absorb kthreads into a kernel function that won't return, so that 612f67a3356SPaul E. McKenney * they won't ever access module text or data again. 613f67a3356SPaul E. McKenney */ 614f67a3356SPaul E. McKenney void torture_shutdown_absorb(const char *title) 615f67a3356SPaul E. McKenney { 6167d0ae808SPaul E. McKenney while (READ_ONCE(fullstop) == FULLSTOP_SHUTDOWN) { 6174622b487SPaul E. McKenney pr_notice("torture thread %s parking due to system shutdown\n", 618f67a3356SPaul E. McKenney title); 619f67a3356SPaul E. McKenney schedule_timeout_uninterruptible(MAX_SCHEDULE_TIMEOUT); 620f67a3356SPaul E. McKenney } 621f67a3356SPaul E. McKenney } 622f67a3356SPaul E. McKenney EXPORT_SYMBOL_GPL(torture_shutdown_absorb); 623b5daa8f3SPaul E. McKenney 624b5daa8f3SPaul E. McKenney /* 625e991dbc0SPaul E. McKenney * Cause the torture test to shutdown the system after the test has 626e991dbc0SPaul E. McKenney * run for the time specified by the shutdown_secs parameter. 627e991dbc0SPaul E. McKenney */ 628e991dbc0SPaul E. McKenney static int torture_shutdown(void *arg) 629e991dbc0SPaul E. McKenney { 63031257c3cSPaul E. McKenney ktime_t ktime_snap; 631e991dbc0SPaul E. McKenney 632e991dbc0SPaul E. McKenney VERBOSE_TOROUT_STRING("torture_shutdown task started"); 63331257c3cSPaul E. McKenney ktime_snap = ktime_get(); 63431257c3cSPaul E. McKenney while (ktime_before(ktime_snap, shutdown_time) && 635e991dbc0SPaul E. McKenney !torture_must_stop()) { 636e991dbc0SPaul E. McKenney if (verbose) 637e991dbc0SPaul E. McKenney pr_alert("%s" TORTURE_FLAG 63831257c3cSPaul E. McKenney "torture_shutdown task: %llu ms remaining\n", 63931257c3cSPaul E. McKenney torture_type, 64031257c3cSPaul E. McKenney ktime_ms_delta(shutdown_time, ktime_snap)); 64131257c3cSPaul E. McKenney set_current_state(TASK_INTERRUPTIBLE); 64231257c3cSPaul E. McKenney schedule_hrtimeout(&shutdown_time, HRTIMER_MODE_ABS); 64331257c3cSPaul E. McKenney ktime_snap = ktime_get(); 644e991dbc0SPaul E. McKenney } 645e991dbc0SPaul E. McKenney if (torture_must_stop()) { 6467fafaac5SPaul E. McKenney torture_kthread_stopping("torture_shutdown"); 647e991dbc0SPaul E. McKenney return 0; 648e991dbc0SPaul E. McKenney } 649e991dbc0SPaul E. McKenney 650e991dbc0SPaul E. McKenney /* OK, shut down the system. */ 651e991dbc0SPaul E. McKenney 652e991dbc0SPaul E. McKenney VERBOSE_TOROUT_STRING("torture_shutdown task shutting down system"); 653e991dbc0SPaul E. McKenney shutdown_task = NULL; /* Avoid self-kill deadlock. */ 654f881825aSPaul E. McKenney if (torture_shutdown_hook) 655f881825aSPaul E. McKenney torture_shutdown_hook(); 656f881825aSPaul E. McKenney else 657f881825aSPaul E. McKenney VERBOSE_TOROUT_STRING("No torture_shutdown_hook(), skipping."); 6582102ad29SPaul E. McKenney if (ftrace_dump_at_shutdown) 659dac95906SPaul E. McKenney rcu_ftrace_dump(DUMP_ALL); 660e991dbc0SPaul E. McKenney kernel_power_off(); /* Shut down the system. */ 661e991dbc0SPaul E. McKenney return 0; 662e991dbc0SPaul E. McKenney } 663e991dbc0SPaul E. McKenney 664e991dbc0SPaul E. McKenney /* 665e991dbc0SPaul E. McKenney * Start up the shutdown task. 666e991dbc0SPaul E. McKenney */ 667e991dbc0SPaul E. McKenney int torture_shutdown_init(int ssecs, void (*cleanup)(void)) 668e991dbc0SPaul E. McKenney { 669e991dbc0SPaul E. McKenney torture_shutdown_hook = cleanup; 67031257c3cSPaul E. McKenney if (ssecs > 0) { 67131257c3cSPaul E. McKenney shutdown_time = ktime_add(ktime_get(), ktime_set(ssecs, 0)); 6722a7d9688SPierce Griffiths return torture_create_kthread(torture_shutdown, NULL, 67347cf29b9SPaul E. McKenney shutdown_task); 67447cf29b9SPaul E. McKenney } 6752a7d9688SPierce Griffiths return 0; 676e991dbc0SPaul E. McKenney } 677e991dbc0SPaul E. McKenney EXPORT_SYMBOL_GPL(torture_shutdown_init); 678e991dbc0SPaul E. McKenney 679e991dbc0SPaul E. McKenney /* 6804622b487SPaul E. McKenney * Detect and respond to a system shutdown. 6814622b487SPaul E. McKenney */ 6824622b487SPaul E. McKenney static int torture_shutdown_notify(struct notifier_block *unused1, 6834622b487SPaul E. McKenney unsigned long unused2, void *unused3) 6844622b487SPaul E. McKenney { 6854622b487SPaul E. McKenney mutex_lock(&fullstop_mutex); 6867d0ae808SPaul E. McKenney if (READ_ONCE(fullstop) == FULLSTOP_DONTSTOP) { 687fac480efSPaul E. McKenney VERBOSE_TOROUT_STRING("Unscheduled system shutdown detected"); 6887d0ae808SPaul E. McKenney WRITE_ONCE(fullstop, FULLSTOP_SHUTDOWN); 689fac480efSPaul E. McKenney } else { 6904622b487SPaul E. McKenney pr_warn("Concurrent rmmod and shutdown illegal!\n"); 691fac480efSPaul E. McKenney } 6924622b487SPaul E. McKenney mutex_unlock(&fullstop_mutex); 6934622b487SPaul E. McKenney return NOTIFY_DONE; 6944622b487SPaul E. McKenney } 6954622b487SPaul E. McKenney 6964622b487SPaul E. McKenney static struct notifier_block torture_shutdown_nb = { 6974622b487SPaul E. McKenney .notifier_call = torture_shutdown_notify, 6984622b487SPaul E. McKenney }; 6994622b487SPaul E. McKenney 7004622b487SPaul E. McKenney /* 701bfefc73aSPaul E. McKenney * Shut down the shutdown task. Say what??? Heh! This can happen if 702bfefc73aSPaul E. McKenney * the torture module gets an rmmod before the shutdown time arrives. ;-) 703bfefc73aSPaul E. McKenney */ 704bfefc73aSPaul E. McKenney static void torture_shutdown_cleanup(void) 705bfefc73aSPaul E. McKenney { 706bfefc73aSPaul E. McKenney unregister_reboot_notifier(&torture_shutdown_nb); 707bfefc73aSPaul E. McKenney if (shutdown_task != NULL) { 708bfefc73aSPaul E. McKenney VERBOSE_TOROUT_STRING("Stopping torture_shutdown task"); 709bfefc73aSPaul E. McKenney kthread_stop(shutdown_task); 710bfefc73aSPaul E. McKenney } 711bfefc73aSPaul E. McKenney shutdown_task = NULL; 712bfefc73aSPaul E. McKenney } 713bfefc73aSPaul E. McKenney 714bfefc73aSPaul E. McKenney /* 715628edaa5SPaul E. McKenney * Variables for stuttering, which means to periodically pause and 716628edaa5SPaul E. McKenney * restart testing in order to catch bugs that appear when load is 717628edaa5SPaul E. McKenney * suddenly applied to or removed from the system. 718628edaa5SPaul E. McKenney */ 719628edaa5SPaul E. McKenney static struct task_struct *stutter_task; 720628edaa5SPaul E. McKenney static int stutter_pause_test; 721628edaa5SPaul E. McKenney static int stutter; 722ff3bf92dSPaul E. McKenney static int stutter_gap; 723628edaa5SPaul E. McKenney 724628edaa5SPaul E. McKenney /* 725628edaa5SPaul E. McKenney * Block until the stutter interval ends. This must be called periodically 726628edaa5SPaul E. McKenney * by all running kthreads that need to be subject to stuttering. 727628edaa5SPaul E. McKenney */ 728474e59b4SPaul E. McKenney bool stutter_wait(const char *title) 729628edaa5SPaul E. McKenney { 73019012b78SPaul E. McKenney unsigned int i = 0; 731e8516c64SPaul E. McKenney bool ret = false; 73219012b78SPaul E. McKenney int spt; 7334ced3314SPaul E. McKenney 734cee43939SPaul E. McKenney cond_resched_tasks_rcu_qs(); 7354ced3314SPaul E. McKenney spt = READ_ONCE(stutter_pause_test); 73629d39390SPaul E. McKenney for (; spt; spt = READ_ONCE(stutter_pause_test)) { 737ab1b7880SPaul E. McKenney if (!ret) { 738ab1b7880SPaul E. McKenney sched_set_normal(current, MAX_NICE); 739e8516c64SPaul E. McKenney ret = true; 740ab1b7880SPaul E. McKenney } 7414ced3314SPaul E. McKenney if (spt == 1) { 742628edaa5SPaul E. McKenney schedule_timeout_interruptible(1); 7434ced3314SPaul E. McKenney } else if (spt == 2) { 74419012b78SPaul E. McKenney while (READ_ONCE(stutter_pause_test)) { 745ed24affaSPaul E. McKenney if (!(i++ & 0xffff)) 746ed24affaSPaul E. McKenney torture_hrtimeout_us(10, 0, NULL); 747ab7d4505SPaul E. McKenney cond_resched(); 74819012b78SPaul E. McKenney } 7494ced3314SPaul E. McKenney } else { 750628edaa5SPaul E. McKenney schedule_timeout_interruptible(round_jiffies_relative(HZ)); 7514ced3314SPaul E. McKenney } 752628edaa5SPaul E. McKenney torture_shutdown_absorb(title); 753628edaa5SPaul E. McKenney } 754e8516c64SPaul E. McKenney return ret; 755628edaa5SPaul E. McKenney } 756628edaa5SPaul E. McKenney EXPORT_SYMBOL_GPL(stutter_wait); 757628edaa5SPaul E. McKenney 758628edaa5SPaul E. McKenney /* 759628edaa5SPaul E. McKenney * Cause the torture test to "stutter", starting and stopping all 760628edaa5SPaul E. McKenney * threads periodically. 761628edaa5SPaul E. McKenney */ 762628edaa5SPaul E. McKenney static int torture_stutter(void *arg) 763628edaa5SPaul E. McKenney { 764fda5ba9eSPaul E. McKenney DEFINE_TORTURE_RANDOM(rand); 765e8516c64SPaul E. McKenney int wtime; 766e8516c64SPaul E. McKenney 767628edaa5SPaul E. McKenney VERBOSE_TOROUT_STRING("torture_stutter task started"); 768628edaa5SPaul E. McKenney do { 7694ced3314SPaul E. McKenney if (!torture_must_stop() && stutter > 1) { 770e8516c64SPaul E. McKenney wtime = stutter; 771fda5ba9eSPaul E. McKenney if (stutter > 2) { 7724ced3314SPaul E. McKenney WRITE_ONCE(stutter_pause_test, 1); 773fda5ba9eSPaul E. McKenney wtime = stutter - 3; 774ed24affaSPaul E. McKenney torture_hrtimeout_jiffies(wtime, &rand); 775fda5ba9eSPaul E. McKenney wtime = 2; 776e8516c64SPaul E. McKenney } 7777d0ae808SPaul E. McKenney WRITE_ONCE(stutter_pause_test, 2); 778ed24affaSPaul E. McKenney torture_hrtimeout_jiffies(wtime, NULL); 779628edaa5SPaul E. McKenney } 7804ced3314SPaul E. McKenney WRITE_ONCE(stutter_pause_test, 0); 781628edaa5SPaul E. McKenney if (!torture_must_stop()) 782ed24affaSPaul E. McKenney torture_hrtimeout_jiffies(stutter_gap, NULL); 783628edaa5SPaul E. McKenney torture_shutdown_absorb("torture_stutter"); 784628edaa5SPaul E. McKenney } while (!torture_must_stop()); 7857fafaac5SPaul E. McKenney torture_kthread_stopping("torture_stutter"); 786628edaa5SPaul E. McKenney return 0; 787628edaa5SPaul E. McKenney } 788628edaa5SPaul E. McKenney 789628edaa5SPaul E. McKenney /* 790628edaa5SPaul E. McKenney * Initialize and kick off the torture_stutter kthread. 791628edaa5SPaul E. McKenney */ 792ff3bf92dSPaul E. McKenney int torture_stutter_init(const int s, const int sgap) 793628edaa5SPaul E. McKenney { 794628edaa5SPaul E. McKenney stutter = s; 795ff3bf92dSPaul E. McKenney stutter_gap = sgap; 7962a7d9688SPierce Griffiths return torture_create_kthread(torture_stutter, NULL, stutter_task); 797628edaa5SPaul E. McKenney } 798628edaa5SPaul E. McKenney EXPORT_SYMBOL_GPL(torture_stutter_init); 799628edaa5SPaul E. McKenney 800628edaa5SPaul E. McKenney /* 801628edaa5SPaul E. McKenney * Cleanup after the torture_stutter kthread. 802628edaa5SPaul E. McKenney */ 803bfefc73aSPaul E. McKenney static void torture_stutter_cleanup(void) 804628edaa5SPaul E. McKenney { 805628edaa5SPaul E. McKenney if (!stutter_task) 806628edaa5SPaul E. McKenney return; 807628edaa5SPaul E. McKenney VERBOSE_TOROUT_STRING("Stopping torture_stutter task"); 808628edaa5SPaul E. McKenney kthread_stop(stutter_task); 809628edaa5SPaul E. McKenney stutter_task = NULL; 810628edaa5SPaul E. McKenney } 811628edaa5SPaul E. McKenney 812628edaa5SPaul E. McKenney /* 813b5daa8f3SPaul E. McKenney * Initialize torture module. Please note that this is -not- invoked via 814b5daa8f3SPaul E. McKenney * the usual module_init() mechanism, but rather by an explicit call from 815b5daa8f3SPaul E. McKenney * the client torture module. This call must be paired with a later 816b5daa8f3SPaul E. McKenney * torture_init_end(). 817628edaa5SPaul E. McKenney * 818628edaa5SPaul E. McKenney * The runnable parameter points to a flag that controls whether or not 819628edaa5SPaul E. McKenney * the test is currently runnable. If there is no such flag, pass in NULL. 820b5daa8f3SPaul E. McKenney */ 82190127d60SPaul E. McKenney bool torture_init_begin(char *ttype, int v) 822b5daa8f3SPaul E. McKenney { 823b5daa8f3SPaul E. McKenney mutex_lock(&fullstop_mutex); 8245228084eSPaul E. McKenney if (torture_type != NULL) { 8254ac9de07SStephen Zhang pr_alert("%s: Refusing %s init: %s running.\n", 8264ac9de07SStephen Zhang __func__, ttype, torture_type); 8274ac9de07SStephen Zhang pr_alert("%s: One torture test at a time!\n", __func__); 8285228084eSPaul E. McKenney mutex_unlock(&fullstop_mutex); 8295228084eSPaul E. McKenney return false; 8305228084eSPaul E. McKenney } 831b5daa8f3SPaul E. McKenney torture_type = ttype; 832b5daa8f3SPaul E. McKenney verbose = v; 83336970bb9SPaul E. McKenney fullstop = FULLSTOP_DONTSTOP; 8345228084eSPaul E. McKenney return true; 835b5daa8f3SPaul E. McKenney } 836b5daa8f3SPaul E. McKenney EXPORT_SYMBOL_GPL(torture_init_begin); 837b5daa8f3SPaul E. McKenney 838b5daa8f3SPaul E. McKenney /* 839b5daa8f3SPaul E. McKenney * Tell the torture module that initialization is complete. 840b5daa8f3SPaul E. McKenney */ 8412b3f8ffeSPranith Kumar void torture_init_end(void) 842b5daa8f3SPaul E. McKenney { 843b5daa8f3SPaul E. McKenney mutex_unlock(&fullstop_mutex); 8444622b487SPaul E. McKenney register_reboot_notifier(&torture_shutdown_nb); 845b5daa8f3SPaul E. McKenney } 846b5daa8f3SPaul E. McKenney EXPORT_SYMBOL_GPL(torture_init_end); 847cc47ae08SPaul E. McKenney 848cc47ae08SPaul E. McKenney /* 849cc47ae08SPaul E. McKenney * Clean up torture module. Please note that this is -not- invoked via 850cc47ae08SPaul E. McKenney * the usual module_exit() mechanism, but rather by an explicit call from 851cc47ae08SPaul E. McKenney * the client torture module. Returns true if a race with system shutdown 852bfefc73aSPaul E. McKenney * is detected, otherwise, all kthreads started by functions in this file 853bfefc73aSPaul E. McKenney * will be shut down. 854cc47ae08SPaul E. McKenney * 855cc47ae08SPaul E. McKenney * This must be called before the caller starts shutting down its own 856cc47ae08SPaul E. McKenney * kthreads. 857d36a7a0dSDavidlohr Bueso * 858d36a7a0dSDavidlohr Bueso * Both torture_cleanup_begin() and torture_cleanup_end() must be paired, 859d36a7a0dSDavidlohr Bueso * in order to correctly perform the cleanup. They are separated because 860d36a7a0dSDavidlohr Bueso * threads can still need to reference the torture_type type, thus nullify 861d36a7a0dSDavidlohr Bueso * only after completing all other relevant calls. 862cc47ae08SPaul E. McKenney */ 863d36a7a0dSDavidlohr Bueso bool torture_cleanup_begin(void) 864cc47ae08SPaul E. McKenney { 865cc47ae08SPaul E. McKenney mutex_lock(&fullstop_mutex); 8667d0ae808SPaul E. McKenney if (READ_ONCE(fullstop) == FULLSTOP_SHUTDOWN) { 867cc47ae08SPaul E. McKenney pr_warn("Concurrent rmmod and shutdown illegal!\n"); 868cc47ae08SPaul E. McKenney mutex_unlock(&fullstop_mutex); 869cc47ae08SPaul E. McKenney schedule_timeout_uninterruptible(10); 870cc47ae08SPaul E. McKenney return true; 871cc47ae08SPaul E. McKenney } 8727d0ae808SPaul E. McKenney WRITE_ONCE(fullstop, FULLSTOP_RMMOD); 873cc47ae08SPaul E. McKenney mutex_unlock(&fullstop_mutex); 874bfefc73aSPaul E. McKenney torture_shutdown_cleanup(); 875cc47ae08SPaul E. McKenney torture_shuffle_cleanup(); 876bfefc73aSPaul E. McKenney torture_stutter_cleanup(); 877cc47ae08SPaul E. McKenney torture_onoff_cleanup(); 878d36a7a0dSDavidlohr Bueso return false; 879d36a7a0dSDavidlohr Bueso } 880d36a7a0dSDavidlohr Bueso EXPORT_SYMBOL_GPL(torture_cleanup_begin); 881d36a7a0dSDavidlohr Bueso 882d36a7a0dSDavidlohr Bueso void torture_cleanup_end(void) 883d36a7a0dSDavidlohr Bueso { 8845228084eSPaul E. McKenney mutex_lock(&fullstop_mutex); 8855228084eSPaul E. McKenney torture_type = NULL; 8865228084eSPaul E. McKenney mutex_unlock(&fullstop_mutex); 887cc47ae08SPaul E. McKenney } 888d36a7a0dSDavidlohr Bueso EXPORT_SYMBOL_GPL(torture_cleanup_end); 88936970bb9SPaul E. McKenney 89036970bb9SPaul E. McKenney /* 89136970bb9SPaul E. McKenney * Is it time for the current torture test to stop? 89236970bb9SPaul E. McKenney */ 89336970bb9SPaul E. McKenney bool torture_must_stop(void) 89436970bb9SPaul E. McKenney { 89536970bb9SPaul E. McKenney return torture_must_stop_irq() || kthread_should_stop(); 89636970bb9SPaul E. McKenney } 89736970bb9SPaul E. McKenney EXPORT_SYMBOL_GPL(torture_must_stop); 89836970bb9SPaul E. McKenney 89936970bb9SPaul E. McKenney /* 90036970bb9SPaul E. McKenney * Is it time for the current torture test to stop? This is the irq-safe 90136970bb9SPaul E. McKenney * version, hence no check for kthread_should_stop(). 90236970bb9SPaul E. McKenney */ 90336970bb9SPaul E. McKenney bool torture_must_stop_irq(void) 90436970bb9SPaul E. McKenney { 9057d0ae808SPaul E. McKenney return READ_ONCE(fullstop) != FULLSTOP_DONTSTOP; 90636970bb9SPaul E. McKenney } 90736970bb9SPaul E. McKenney EXPORT_SYMBOL_GPL(torture_must_stop_irq); 9087fafaac5SPaul E. McKenney 9097fafaac5SPaul E. McKenney /* 9107fafaac5SPaul E. McKenney * Each kthread must wait for kthread_should_stop() before returning from 9117fafaac5SPaul E. McKenney * its top-level function, otherwise segfaults ensue. This function 9127fafaac5SPaul E. McKenney * prints a "stopping" message and waits for kthread_should_stop(), and 9137fafaac5SPaul E. McKenney * should be called from all torture kthreads immediately prior to 9147fafaac5SPaul E. McKenney * returning. 9157fafaac5SPaul E. McKenney */ 9167fafaac5SPaul E. McKenney void torture_kthread_stopping(char *title) 9177fafaac5SPaul E. McKenney { 9180d6821d5SPaul E. McKenney char buf[128]; 9190d6821d5SPaul E. McKenney 9202b4a7f20SPaul E. McKenney snprintf(buf, sizeof(buf), "%s is stopping", title); 9210d6821d5SPaul E. McKenney VERBOSE_TOROUT_STRING(buf); 9227fafaac5SPaul E. McKenney while (!kthread_should_stop()) { 9237fafaac5SPaul E. McKenney torture_shutdown_absorb(title); 924d52d3a2bSJoel Fernandes (Google) schedule_timeout_uninterruptible(HZ / 20); 9257fafaac5SPaul E. McKenney } 9267fafaac5SPaul E. McKenney } 9277fafaac5SPaul E. McKenney EXPORT_SYMBOL_GPL(torture_kthread_stopping); 92847cf29b9SPaul E. McKenney 92947cf29b9SPaul E. McKenney /* 93047cf29b9SPaul E. McKenney * Create a generic torture kthread that is immediately runnable. If you 93147cf29b9SPaul E. McKenney * need the kthread to be stopped so that you can do something to it before 93247cf29b9SPaul E. McKenney * it starts, you will need to open-code your own. 93347cf29b9SPaul E. McKenney */ 93447cf29b9SPaul E. McKenney int _torture_create_kthread(int (*fn)(void *arg), void *arg, char *s, char *m, 93547cf29b9SPaul E. McKenney char *f, struct task_struct **tp) 93647cf29b9SPaul E. McKenney { 93747cf29b9SPaul E. McKenney int ret = 0; 93847cf29b9SPaul E. McKenney 93947cf29b9SPaul E. McKenney VERBOSE_TOROUT_STRING(m); 9406b8646a9SPaul E. McKenney *tp = kthread_create(fn, arg, "%s", s); 94147cf29b9SPaul E. McKenney if (IS_ERR(*tp)) { 94247cf29b9SPaul E. McKenney ret = PTR_ERR(*tp); 94381faa4f6SLi Zhijian TOROUT_ERRSTRING(f); 94447cf29b9SPaul E. McKenney *tp = NULL; 9456b8646a9SPaul E. McKenney return ret; 94647cf29b9SPaul E. McKenney } 9476b8646a9SPaul E. McKenney wake_up_process(*tp); // Process is sleeping, so ordering provided. 94847cf29b9SPaul E. McKenney torture_shuffle_task_register(*tp); 94947cf29b9SPaul E. McKenney return ret; 95047cf29b9SPaul E. McKenney } 95147cf29b9SPaul E. McKenney EXPORT_SYMBOL_GPL(_torture_create_kthread); 9529c029b86SPaul E. McKenney 9539c029b86SPaul E. McKenney /* 9549c029b86SPaul E. McKenney * Stop a generic kthread, emitting a message. 9559c029b86SPaul E. McKenney */ 9569c029b86SPaul E. McKenney void _torture_stop_kthread(char *m, struct task_struct **tp) 9579c029b86SPaul E. McKenney { 9589c029b86SPaul E. McKenney if (*tp == NULL) 9599c029b86SPaul E. McKenney return; 9609c029b86SPaul E. McKenney VERBOSE_TOROUT_STRING(m); 9619c029b86SPaul E. McKenney kthread_stop(*tp); 9629c029b86SPaul E. McKenney *tp = NULL; 9639c029b86SPaul E. McKenney } 9649c029b86SPaul E. McKenney EXPORT_SYMBOL_GPL(_torture_stop_kthread); 965