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 1781afb95feSPaul E. McKenney static int torture_online_cpus = NR_CPUS; 1791afb95feSPaul E. McKenney 1801afb95feSPaul E. McKenney /* 1811afb95feSPaul E. McKenney * Some torture testing leverages confusion as to the number of online 1821afb95feSPaul E. McKenney * CPUs. This function returns the torture-testing view of this number, 1831afb95feSPaul E. McKenney * which allows torture tests to load-balance appropriately. 1841afb95feSPaul E. McKenney */ 1851afb95feSPaul E. McKenney int torture_num_online_cpus(void) 1861afb95feSPaul E. McKenney { 1871afb95feSPaul E. McKenney return READ_ONCE(torture_online_cpus); 1881afb95feSPaul E. McKenney } 1891afb95feSPaul E. McKenney EXPORT_SYMBOL_GPL(torture_num_online_cpus); 1901afb95feSPaul E. McKenney 1912e9e8081SPaul E. McKenney /* 192d95f5ba9SPaul E. McKenney * Attempt to take a CPU offline. Return false if the CPU is already 193d95f5ba9SPaul E. McKenney * offline or if it is not subject to CPU-hotplug operations. The 194d95f5ba9SPaul E. McKenney * caller can detect other failures by looking at the statistics. 195d95f5ba9SPaul E. McKenney */ 196d95f5ba9SPaul E. McKenney bool torture_offline(int cpu, long *n_offl_attempts, long *n_offl_successes, 197d95f5ba9SPaul E. McKenney unsigned long *sum_offl, int *min_offl, int *max_offl) 198d95f5ba9SPaul E. McKenney { 199d95f5ba9SPaul E. McKenney unsigned long delta; 200d95f5ba9SPaul E. McKenney int ret; 201a59ee765SPaul E. McKenney char *s; 202d95f5ba9SPaul E. McKenney unsigned long starttime; 203d95f5ba9SPaul E. McKenney 204d95f5ba9SPaul E. McKenney if (!cpu_online(cpu) || !cpu_is_hotpluggable(cpu)) 205d95f5ba9SPaul E. McKenney return false; 20624aca4aeSPaul E. McKenney if (num_online_cpus() <= 1) 20724aca4aeSPaul E. McKenney return false; /* Can't offline the last CPU. */ 208d95f5ba9SPaul E. McKenney 20990127d60SPaul E. McKenney if (verbose > 1) 210d95f5ba9SPaul E. McKenney pr_alert("%s" TORTURE_FLAG 211d95f5ba9SPaul E. McKenney "torture_onoff task: offlining %d\n", 212d95f5ba9SPaul E. McKenney torture_type, cpu); 213d95f5ba9SPaul E. McKenney starttime = jiffies; 214d95f5ba9SPaul E. McKenney (*n_offl_attempts)++; 215457bc8edSQais Yousef ret = remove_cpu(cpu); 216d95f5ba9SPaul E. McKenney if (ret) { 217a59ee765SPaul E. McKenney s = ""; 218a59ee765SPaul E. McKenney if (!rcu_inkernel_boot_has_ended() && ret == -EBUSY) { 219a59ee765SPaul E. McKenney // PCI probe frequently disables hotplug during boot. 220a59ee765SPaul E. McKenney (*n_offl_attempts)--; 221a59ee765SPaul E. McKenney s = " (-EBUSY forgiven during boot)"; 222a59ee765SPaul E. McKenney } 223d95f5ba9SPaul E. McKenney if (verbose) 224d95f5ba9SPaul E. McKenney pr_alert("%s" TORTURE_FLAG 225a59ee765SPaul E. McKenney "torture_onoff task: offline %d failed%s: errno %d\n", 226a59ee765SPaul E. McKenney torture_type, cpu, s, ret); 227d95f5ba9SPaul E. McKenney } else { 22890127d60SPaul E. McKenney if (verbose > 1) 229d95f5ba9SPaul E. McKenney pr_alert("%s" TORTURE_FLAG 230d95f5ba9SPaul E. McKenney "torture_onoff task: offlined %d\n", 231d95f5ba9SPaul E. McKenney torture_type, cpu); 2323a6cb58fSPaul E. McKenney if (onoff_f) 2333a6cb58fSPaul E. McKenney onoff_f(); 234d95f5ba9SPaul E. McKenney (*n_offl_successes)++; 235d95f5ba9SPaul E. McKenney delta = jiffies - starttime; 236a2b2df20SPaul E. McKenney *sum_offl += delta; 237d95f5ba9SPaul E. McKenney if (*min_offl < 0) { 238d95f5ba9SPaul E. McKenney *min_offl = delta; 239d95f5ba9SPaul E. McKenney *max_offl = delta; 240d95f5ba9SPaul E. McKenney } 241d95f5ba9SPaul E. McKenney if (*min_offl > delta) 242d95f5ba9SPaul E. McKenney *min_offl = delta; 243d95f5ba9SPaul E. McKenney if (*max_offl < delta) 244d95f5ba9SPaul E. McKenney *max_offl = delta; 2451afb95feSPaul E. McKenney WRITE_ONCE(torture_online_cpus, torture_online_cpus - 1); 2461afb95feSPaul E. McKenney WARN_ON_ONCE(torture_online_cpus <= 0); 247d95f5ba9SPaul E. McKenney } 248d95f5ba9SPaul E. McKenney 249d95f5ba9SPaul E. McKenney return true; 250d95f5ba9SPaul E. McKenney } 251d95f5ba9SPaul E. McKenney EXPORT_SYMBOL_GPL(torture_offline); 252d95f5ba9SPaul E. McKenney 253d95f5ba9SPaul E. McKenney /* 254d95f5ba9SPaul E. McKenney * Attempt to bring a CPU online. Return false if the CPU is already 255d95f5ba9SPaul E. McKenney * online or if it is not subject to CPU-hotplug operations. The 256d95f5ba9SPaul E. McKenney * caller can detect other failures by looking at the statistics. 257d95f5ba9SPaul E. McKenney */ 258d95f5ba9SPaul E. McKenney bool torture_online(int cpu, long *n_onl_attempts, long *n_onl_successes, 259d95f5ba9SPaul E. McKenney unsigned long *sum_onl, int *min_onl, int *max_onl) 260d95f5ba9SPaul E. McKenney { 261d95f5ba9SPaul E. McKenney unsigned long delta; 262d95f5ba9SPaul E. McKenney int ret; 263a59ee765SPaul E. McKenney char *s; 264d95f5ba9SPaul E. McKenney unsigned long starttime; 265d95f5ba9SPaul E. McKenney 266d95f5ba9SPaul E. McKenney if (cpu_online(cpu) || !cpu_is_hotpluggable(cpu)) 267d95f5ba9SPaul E. McKenney return false; 268d95f5ba9SPaul E. McKenney 26990127d60SPaul E. McKenney if (verbose > 1) 270d95f5ba9SPaul E. McKenney pr_alert("%s" TORTURE_FLAG 271d95f5ba9SPaul E. McKenney "torture_onoff task: onlining %d\n", 272d95f5ba9SPaul E. McKenney torture_type, cpu); 273d95f5ba9SPaul E. McKenney starttime = jiffies; 274d95f5ba9SPaul E. McKenney (*n_onl_attempts)++; 275457bc8edSQais Yousef ret = add_cpu(cpu); 276d95f5ba9SPaul E. McKenney if (ret) { 277a59ee765SPaul E. McKenney s = ""; 278a59ee765SPaul E. McKenney if (!rcu_inkernel_boot_has_ended() && ret == -EBUSY) { 279a59ee765SPaul E. McKenney // PCI probe frequently disables hotplug during boot. 280a59ee765SPaul E. McKenney (*n_onl_attempts)--; 281a59ee765SPaul E. McKenney s = " (-EBUSY forgiven during boot)"; 282a59ee765SPaul E. McKenney } 283d95f5ba9SPaul E. McKenney if (verbose) 284d95f5ba9SPaul E. McKenney pr_alert("%s" TORTURE_FLAG 285a59ee765SPaul E. McKenney "torture_onoff task: online %d failed%s: errno %d\n", 286a59ee765SPaul E. McKenney torture_type, cpu, s, ret); 287d95f5ba9SPaul E. McKenney } else { 28890127d60SPaul E. McKenney if (verbose > 1) 289d95f5ba9SPaul E. McKenney pr_alert("%s" TORTURE_FLAG 290d95f5ba9SPaul E. McKenney "torture_onoff task: onlined %d\n", 291d95f5ba9SPaul E. McKenney torture_type, cpu); 292d95f5ba9SPaul E. McKenney (*n_onl_successes)++; 293d95f5ba9SPaul E. McKenney delta = jiffies - starttime; 294d95f5ba9SPaul E. McKenney *sum_onl += delta; 295d95f5ba9SPaul E. McKenney if (*min_onl < 0) { 296d95f5ba9SPaul E. McKenney *min_onl = delta; 297d95f5ba9SPaul E. McKenney *max_onl = delta; 298d95f5ba9SPaul E. McKenney } 299d95f5ba9SPaul E. McKenney if (*min_onl > delta) 300d95f5ba9SPaul E. McKenney *min_onl = delta; 301d95f5ba9SPaul E. McKenney if (*max_onl < delta) 302d95f5ba9SPaul E. McKenney *max_onl = delta; 3031afb95feSPaul E. McKenney WRITE_ONCE(torture_online_cpus, torture_online_cpus + 1); 304d95f5ba9SPaul E. McKenney } 305d95f5ba9SPaul E. McKenney 306d95f5ba9SPaul E. McKenney return true; 307d95f5ba9SPaul E. McKenney } 308d95f5ba9SPaul E. McKenney EXPORT_SYMBOL_GPL(torture_online); 309d95f5ba9SPaul E. McKenney 310d95f5ba9SPaul E. McKenney /* 3110b962c8fSPaul E. McKenney * Get everything online at the beginning and ends of tests. 3120b962c8fSPaul E. McKenney */ 3130b962c8fSPaul E. McKenney static void torture_online_all(char *phase) 3140b962c8fSPaul E. McKenney { 3150b962c8fSPaul E. McKenney int cpu; 3160b962c8fSPaul E. McKenney int ret; 3170b962c8fSPaul E. McKenney 3180b962c8fSPaul E. McKenney for_each_possible_cpu(cpu) { 3190b962c8fSPaul E. McKenney if (cpu_online(cpu)) 3200b962c8fSPaul E. McKenney continue; 3210b962c8fSPaul E. McKenney ret = add_cpu(cpu); 3220b962c8fSPaul E. McKenney if (ret && verbose) { 3230b962c8fSPaul E. McKenney pr_alert("%s" TORTURE_FLAG 3240b962c8fSPaul E. McKenney "%s: %s online %d: errno %d\n", 3250b962c8fSPaul E. McKenney __func__, phase, torture_type, cpu, ret); 3260b962c8fSPaul E. McKenney } 3270b962c8fSPaul E. McKenney } 3280b962c8fSPaul E. McKenney } 3290b962c8fSPaul E. McKenney 3300b962c8fSPaul E. McKenney /* 3312e9e8081SPaul E. McKenney * Execute random CPU-hotplug operations at the interval specified 3322e9e8081SPaul E. McKenney * by the onoff_interval. 3332e9e8081SPaul E. McKenney */ 3342e9e8081SPaul E. McKenney static int 3352e9e8081SPaul E. McKenney torture_onoff(void *arg) 3362e9e8081SPaul E. McKenney { 3372e9e8081SPaul E. McKenney int cpu; 3382e9e8081SPaul E. McKenney int maxcpu = -1; 3392e9e8081SPaul E. McKenney DEFINE_TORTURE_RANDOM(rand); 3402e9e8081SPaul E. McKenney 3412e9e8081SPaul E. McKenney VERBOSE_TOROUT_STRING("torture_onoff task started"); 3422e9e8081SPaul E. McKenney for_each_online_cpu(cpu) 3432e9e8081SPaul E. McKenney maxcpu = cpu; 3442e9e8081SPaul E. McKenney WARN_ON(maxcpu < 0); 3450b962c8fSPaul E. McKenney torture_online_all("Initial"); 346750db0f5SBoqun Feng if (maxcpu == 0) { 347750db0f5SBoqun Feng VERBOSE_TOROUT_STRING("Only one CPU, so CPU-hotplug testing is disabled"); 348750db0f5SBoqun Feng goto stop; 349750db0f5SBoqun Feng } 350750db0f5SBoqun Feng 3512e9e8081SPaul E. McKenney if (onoff_holdoff > 0) { 3522e9e8081SPaul E. McKenney VERBOSE_TOROUT_STRING("torture_onoff begin holdoff"); 3532e9e8081SPaul E. McKenney schedule_timeout_interruptible(onoff_holdoff); 3542e9e8081SPaul E. McKenney VERBOSE_TOROUT_STRING("torture_onoff end holdoff"); 3552e9e8081SPaul E. McKenney } 3562e9e8081SPaul E. McKenney while (!torture_must_stop()) { 3578171d3e0SPaul E. McKenney if (disable_onoff_at_boot && !rcu_inkernel_boot_has_ended()) { 3588171d3e0SPaul E. McKenney schedule_timeout_interruptible(HZ / 10); 3598171d3e0SPaul E. McKenney continue; 3608171d3e0SPaul E. McKenney } 3612e9e8081SPaul E. McKenney cpu = (torture_random(&rand) >> 4) % (maxcpu + 1); 362d95f5ba9SPaul E. McKenney if (!torture_offline(cpu, 363d95f5ba9SPaul E. McKenney &n_offline_attempts, &n_offline_successes, 364d95f5ba9SPaul E. McKenney &sum_offline, &min_offline, &max_offline)) 365d95f5ba9SPaul E. McKenney torture_online(cpu, 366d95f5ba9SPaul E. McKenney &n_online_attempts, &n_online_successes, 367d95f5ba9SPaul E. McKenney &sum_online, &min_online, &max_online); 3682e9e8081SPaul E. McKenney schedule_timeout_interruptible(onoff_interval); 3692e9e8081SPaul E. McKenney } 370750db0f5SBoqun Feng 371750db0f5SBoqun Feng stop: 3727fafaac5SPaul E. McKenney torture_kthread_stopping("torture_onoff"); 3730b962c8fSPaul E. McKenney torture_online_all("Final"); 3742e9e8081SPaul E. McKenney return 0; 3752e9e8081SPaul E. McKenney } 3762e9e8081SPaul E. McKenney 3772e9e8081SPaul E. McKenney #endif /* #ifdef CONFIG_HOTPLUG_CPU */ 3782e9e8081SPaul E. McKenney 3792e9e8081SPaul E. McKenney /* 3802e9e8081SPaul E. McKenney * Initiate online-offline handling. 3812e9e8081SPaul E. McKenney */ 3823a6cb58fSPaul E. McKenney int torture_onoff_init(long ooholdoff, long oointerval, torture_ofl_func *f) 3832e9e8081SPaul E. McKenney { 38447cf29b9SPaul E. McKenney #ifdef CONFIG_HOTPLUG_CPU 3852e9e8081SPaul E. McKenney onoff_holdoff = ooholdoff; 3862e9e8081SPaul E. McKenney onoff_interval = oointerval; 3873a6cb58fSPaul E. McKenney onoff_f = f; 3882e9e8081SPaul E. McKenney if (onoff_interval <= 0) 3892e9e8081SPaul E. McKenney return 0; 3902a7d9688SPierce Griffiths return torture_create_kthread(torture_onoff, NULL, onoff_task); 3912a7d9688SPierce Griffiths #else /* #ifdef CONFIG_HOTPLUG_CPU */ 3922a7d9688SPierce Griffiths return 0; 3932a7d9688SPierce Griffiths #endif /* #else #ifdef CONFIG_HOTPLUG_CPU */ 3942e9e8081SPaul E. McKenney } 3952e9e8081SPaul E. McKenney EXPORT_SYMBOL_GPL(torture_onoff_init); 3962e9e8081SPaul E. McKenney 3972e9e8081SPaul E. McKenney /* 3982e9e8081SPaul E. McKenney * Clean up after online/offline testing. 3992e9e8081SPaul E. McKenney */ 400cc47ae08SPaul E. McKenney static void torture_onoff_cleanup(void) 4012e9e8081SPaul E. McKenney { 4022e9e8081SPaul E. McKenney #ifdef CONFIG_HOTPLUG_CPU 4032e9e8081SPaul E. McKenney if (onoff_task == NULL) 4042e9e8081SPaul E. McKenney return; 4052e9e8081SPaul E. McKenney VERBOSE_TOROUT_STRING("Stopping torture_onoff task"); 4062e9e8081SPaul E. McKenney kthread_stop(onoff_task); 4072e9e8081SPaul E. McKenney onoff_task = NULL; 4082e9e8081SPaul E. McKenney #endif /* #ifdef CONFIG_HOTPLUG_CPU */ 4092e9e8081SPaul E. McKenney } 4102e9e8081SPaul E. McKenney 4112e9e8081SPaul E. McKenney /* 4122e9e8081SPaul E. McKenney * Print online/offline testing statistics. 4132e9e8081SPaul E. McKenney */ 414eea203feSJoe Perches void torture_onoff_stats(void) 4152e9e8081SPaul E. McKenney { 4162e9e8081SPaul E. McKenney #ifdef CONFIG_HOTPLUG_CPU 417eea203feSJoe Perches pr_cont("onoff: %ld/%ld:%ld/%ld %d,%d:%d,%d %lu:%lu (HZ=%d) ", 4182e9e8081SPaul E. McKenney n_online_successes, n_online_attempts, 4192e9e8081SPaul E. McKenney n_offline_successes, n_offline_attempts, 4202e9e8081SPaul E. McKenney min_online, max_online, 4212e9e8081SPaul E. McKenney min_offline, max_offline, 4222e9e8081SPaul E. McKenney sum_online, sum_offline, HZ); 4232e9e8081SPaul E. McKenney #endif /* #ifdef CONFIG_HOTPLUG_CPU */ 4242e9e8081SPaul E. McKenney } 4252e9e8081SPaul E. McKenney EXPORT_SYMBOL_GPL(torture_onoff_stats); 4262e9e8081SPaul E. McKenney 4272e9e8081SPaul E. McKenney /* 4282e9e8081SPaul E. McKenney * Were all the online/offline operations successful? 4292e9e8081SPaul E. McKenney */ 4302e9e8081SPaul E. McKenney bool torture_onoff_failures(void) 4312e9e8081SPaul E. McKenney { 4322e9e8081SPaul E. McKenney #ifdef CONFIG_HOTPLUG_CPU 4332e9e8081SPaul E. McKenney return n_online_successes != n_online_attempts || 4342e9e8081SPaul E. McKenney n_offline_successes != n_offline_attempts; 4352e9e8081SPaul E. McKenney #else /* #ifdef CONFIG_HOTPLUG_CPU */ 4362e9e8081SPaul E. McKenney return false; 4372e9e8081SPaul E. McKenney #endif /* #else #ifdef CONFIG_HOTPLUG_CPU */ 4382e9e8081SPaul E. McKenney } 4392e9e8081SPaul E. McKenney EXPORT_SYMBOL_GPL(torture_onoff_failures); 4402e9e8081SPaul E. McKenney 44151b1130eSPaul E. McKenney #define TORTURE_RANDOM_MULT 39916801 /* prime */ 44251b1130eSPaul E. McKenney #define TORTURE_RANDOM_ADD 479001701 /* prime */ 44351b1130eSPaul E. McKenney #define TORTURE_RANDOM_REFRESH 10000 44451b1130eSPaul E. McKenney 44551b1130eSPaul E. McKenney /* 44651b1130eSPaul E. McKenney * Crude but fast random-number generator. Uses a linear congruential 44751b1130eSPaul E. McKenney * generator, with occasional help from cpu_clock(). 44851b1130eSPaul E. McKenney */ 44951b1130eSPaul E. McKenney unsigned long 45051b1130eSPaul E. McKenney torture_random(struct torture_random_state *trsp) 45151b1130eSPaul E. McKenney { 45251b1130eSPaul E. McKenney if (--trsp->trs_count < 0) { 45351b1130eSPaul E. McKenney trsp->trs_state += (unsigned long)local_clock(); 45451b1130eSPaul E. McKenney trsp->trs_count = TORTURE_RANDOM_REFRESH; 45551b1130eSPaul E. McKenney } 45651b1130eSPaul E. McKenney trsp->trs_state = trsp->trs_state * TORTURE_RANDOM_MULT + 45751b1130eSPaul E. McKenney TORTURE_RANDOM_ADD; 45851b1130eSPaul E. McKenney return swahw32(trsp->trs_state); 45951b1130eSPaul E. McKenney } 46051b1130eSPaul E. McKenney EXPORT_SYMBOL_GPL(torture_random); 461f67a3356SPaul E. McKenney 462f67a3356SPaul E. McKenney /* 4633808dc9fSPaul E. McKenney * Variables for shuffling. The idea is to ensure that each CPU stays 4643808dc9fSPaul E. McKenney * idle for an extended period to test interactions with dyntick idle, 465b564d62eSMasahiro Yamada * as well as interactions with any per-CPU variables. 4663808dc9fSPaul E. McKenney */ 4673808dc9fSPaul E. McKenney struct shuffle_task { 4683808dc9fSPaul E. McKenney struct list_head st_l; 4693808dc9fSPaul E. McKenney struct task_struct *st_t; 4703808dc9fSPaul E. McKenney }; 4713808dc9fSPaul E. McKenney 4723808dc9fSPaul E. McKenney static long shuffle_interval; /* In jiffies. */ 4733808dc9fSPaul E. McKenney static struct task_struct *shuffler_task; 4743808dc9fSPaul E. McKenney static cpumask_var_t shuffle_tmp_mask; 4753808dc9fSPaul E. McKenney static int shuffle_idle_cpu; /* Force all torture tasks off this CPU */ 4763808dc9fSPaul E. McKenney static struct list_head shuffle_task_list = LIST_HEAD_INIT(shuffle_task_list); 4773808dc9fSPaul E. McKenney static DEFINE_MUTEX(shuffle_task_mutex); 4783808dc9fSPaul E. McKenney 4793808dc9fSPaul E. McKenney /* 4803808dc9fSPaul E. McKenney * Register a task to be shuffled. If there is no memory, just splat 4813808dc9fSPaul E. McKenney * and don't bother registering. 4823808dc9fSPaul E. McKenney */ 4833808dc9fSPaul E. McKenney void torture_shuffle_task_register(struct task_struct *tp) 4843808dc9fSPaul E. McKenney { 4853808dc9fSPaul E. McKenney struct shuffle_task *stp; 4863808dc9fSPaul E. McKenney 4873808dc9fSPaul E. McKenney if (WARN_ON_ONCE(tp == NULL)) 4883808dc9fSPaul E. McKenney return; 4893808dc9fSPaul E. McKenney stp = kmalloc(sizeof(*stp), GFP_KERNEL); 4903808dc9fSPaul E. McKenney if (WARN_ON_ONCE(stp == NULL)) 4913808dc9fSPaul E. McKenney return; 4923808dc9fSPaul E. McKenney stp->st_t = tp; 4933808dc9fSPaul E. McKenney mutex_lock(&shuffle_task_mutex); 4943808dc9fSPaul E. McKenney list_add(&stp->st_l, &shuffle_task_list); 4953808dc9fSPaul E. McKenney mutex_unlock(&shuffle_task_mutex); 4963808dc9fSPaul E. McKenney } 4973808dc9fSPaul E. McKenney EXPORT_SYMBOL_GPL(torture_shuffle_task_register); 4983808dc9fSPaul E. McKenney 4993808dc9fSPaul E. McKenney /* 5003808dc9fSPaul E. McKenney * Unregister all tasks, for example, at the end of the torture run. 5013808dc9fSPaul E. McKenney */ 5023808dc9fSPaul E. McKenney static void torture_shuffle_task_unregister_all(void) 5033808dc9fSPaul E. McKenney { 5043808dc9fSPaul E. McKenney struct shuffle_task *stp; 5053808dc9fSPaul E. McKenney struct shuffle_task *p; 5063808dc9fSPaul E. McKenney 5073808dc9fSPaul E. McKenney mutex_lock(&shuffle_task_mutex); 5083808dc9fSPaul E. McKenney list_for_each_entry_safe(stp, p, &shuffle_task_list, st_l) { 5093808dc9fSPaul E. McKenney list_del(&stp->st_l); 5103808dc9fSPaul E. McKenney kfree(stp); 5113808dc9fSPaul E. McKenney } 5123808dc9fSPaul E. McKenney mutex_unlock(&shuffle_task_mutex); 5133808dc9fSPaul E. McKenney } 5143808dc9fSPaul E. McKenney 5153808dc9fSPaul E. McKenney /* Shuffle tasks such that we allow shuffle_idle_cpu to become idle. 5163808dc9fSPaul E. McKenney * A special case is when shuffle_idle_cpu = -1, in which case we allow 5173808dc9fSPaul E. McKenney * the tasks to run on all CPUs. 5183808dc9fSPaul E. McKenney */ 5193808dc9fSPaul E. McKenney static void torture_shuffle_tasks(void) 5203808dc9fSPaul E. McKenney { 5213808dc9fSPaul E. McKenney struct shuffle_task *stp; 5223808dc9fSPaul E. McKenney 5233808dc9fSPaul E. McKenney cpumask_setall(shuffle_tmp_mask); 524ed4fa244SSebastian Andrzej Siewior cpus_read_lock(); 5253808dc9fSPaul E. McKenney 5263808dc9fSPaul E. McKenney /* No point in shuffling if there is only one online CPU (ex: UP) */ 5273808dc9fSPaul E. McKenney if (num_online_cpus() == 1) { 528ed4fa244SSebastian Andrzej Siewior cpus_read_unlock(); 5293808dc9fSPaul E. McKenney return; 5303808dc9fSPaul E. McKenney } 5313808dc9fSPaul E. McKenney 5323808dc9fSPaul E. McKenney /* Advance to the next CPU. Upon overflow, don't idle any CPUs. */ 5333808dc9fSPaul E. McKenney shuffle_idle_cpu = cpumask_next(shuffle_idle_cpu, shuffle_tmp_mask); 5343808dc9fSPaul E. McKenney if (shuffle_idle_cpu >= nr_cpu_ids) 5353808dc9fSPaul E. McKenney shuffle_idle_cpu = -1; 5365ed63b19SIulia Manda else 5373808dc9fSPaul E. McKenney cpumask_clear_cpu(shuffle_idle_cpu, shuffle_tmp_mask); 5383808dc9fSPaul E. McKenney 5393808dc9fSPaul E. McKenney mutex_lock(&shuffle_task_mutex); 5403808dc9fSPaul E. McKenney list_for_each_entry(stp, &shuffle_task_list, st_l) 5413808dc9fSPaul E. McKenney set_cpus_allowed_ptr(stp->st_t, shuffle_tmp_mask); 5423808dc9fSPaul E. McKenney mutex_unlock(&shuffle_task_mutex); 5433808dc9fSPaul E. McKenney 544ed4fa244SSebastian Andrzej Siewior cpus_read_unlock(); 5453808dc9fSPaul E. McKenney } 5463808dc9fSPaul E. McKenney 5473808dc9fSPaul E. McKenney /* Shuffle tasks across CPUs, with the intent of allowing each CPU in the 5483808dc9fSPaul E. McKenney * system to become idle at a time and cut off its timer ticks. This is meant 5493808dc9fSPaul E. McKenney * to test the support for such tickless idle CPU in RCU. 5503808dc9fSPaul E. McKenney */ 5513808dc9fSPaul E. McKenney static int torture_shuffle(void *arg) 5523808dc9fSPaul E. McKenney { 5533808dc9fSPaul E. McKenney VERBOSE_TOROUT_STRING("torture_shuffle task started"); 5543808dc9fSPaul E. McKenney do { 5553808dc9fSPaul E. McKenney schedule_timeout_interruptible(shuffle_interval); 5563808dc9fSPaul E. McKenney torture_shuffle_tasks(); 5573808dc9fSPaul E. McKenney torture_shutdown_absorb("torture_shuffle"); 5583808dc9fSPaul E. McKenney } while (!torture_must_stop()); 5597fafaac5SPaul E. McKenney torture_kthread_stopping("torture_shuffle"); 5603808dc9fSPaul E. McKenney return 0; 5613808dc9fSPaul E. McKenney } 5623808dc9fSPaul E. McKenney 5633808dc9fSPaul E. McKenney /* 5643808dc9fSPaul E. McKenney * Start the shuffler, with shuffint in jiffies. 5653808dc9fSPaul E. McKenney */ 5663808dc9fSPaul E. McKenney int torture_shuffle_init(long shuffint) 5673808dc9fSPaul E. McKenney { 5683808dc9fSPaul E. McKenney shuffle_interval = shuffint; 5693808dc9fSPaul E. McKenney 5703808dc9fSPaul E. McKenney shuffle_idle_cpu = -1; 5713808dc9fSPaul E. McKenney 5723808dc9fSPaul E. McKenney if (!alloc_cpumask_var(&shuffle_tmp_mask, GFP_KERNEL)) { 573*81faa4f6SLi Zhijian TOROUT_ERRSTRING("Failed to alloc mask"); 5743808dc9fSPaul E. McKenney return -ENOMEM; 5753808dc9fSPaul E. McKenney } 5763808dc9fSPaul E. McKenney 5773808dc9fSPaul E. McKenney /* Create the shuffler thread */ 57847cf29b9SPaul E. McKenney return torture_create_kthread(torture_shuffle, NULL, shuffler_task); 5793808dc9fSPaul E. McKenney } 5803808dc9fSPaul E. McKenney EXPORT_SYMBOL_GPL(torture_shuffle_init); 5813808dc9fSPaul E. McKenney 5823808dc9fSPaul E. McKenney /* 5833808dc9fSPaul E. McKenney * Stop the shuffling. 5843808dc9fSPaul E. McKenney */ 585cc47ae08SPaul E. McKenney static void torture_shuffle_cleanup(void) 5863808dc9fSPaul E. McKenney { 5873808dc9fSPaul E. McKenney torture_shuffle_task_unregister_all(); 5883808dc9fSPaul E. McKenney if (shuffler_task) { 5893808dc9fSPaul E. McKenney VERBOSE_TOROUT_STRING("Stopping torture_shuffle task"); 5903808dc9fSPaul E. McKenney kthread_stop(shuffler_task); 5913808dc9fSPaul E. McKenney free_cpumask_var(shuffle_tmp_mask); 5923808dc9fSPaul E. McKenney } 5933808dc9fSPaul E. McKenney shuffler_task = NULL; 5943808dc9fSPaul E. McKenney } 5953808dc9fSPaul E. McKenney 5963808dc9fSPaul E. McKenney /* 597e991dbc0SPaul E. McKenney * Variables for auto-shutdown. This allows "lights out" torture runs 598e991dbc0SPaul E. McKenney * to be fully scripted. 599e991dbc0SPaul E. McKenney */ 600e991dbc0SPaul E. McKenney static struct task_struct *shutdown_task; 60131257c3cSPaul E. McKenney static ktime_t shutdown_time; /* time to system shutdown. */ 602e991dbc0SPaul E. McKenney static void (*torture_shutdown_hook)(void); 603e991dbc0SPaul E. McKenney 604e991dbc0SPaul E. McKenney /* 605f67a3356SPaul E. McKenney * Absorb kthreads into a kernel function that won't return, so that 606f67a3356SPaul E. McKenney * they won't ever access module text or data again. 607f67a3356SPaul E. McKenney */ 608f67a3356SPaul E. McKenney void torture_shutdown_absorb(const char *title) 609f67a3356SPaul E. McKenney { 6107d0ae808SPaul E. McKenney while (READ_ONCE(fullstop) == FULLSTOP_SHUTDOWN) { 6114622b487SPaul E. McKenney pr_notice("torture thread %s parking due to system shutdown\n", 612f67a3356SPaul E. McKenney title); 613f67a3356SPaul E. McKenney schedule_timeout_uninterruptible(MAX_SCHEDULE_TIMEOUT); 614f67a3356SPaul E. McKenney } 615f67a3356SPaul E. McKenney } 616f67a3356SPaul E. McKenney EXPORT_SYMBOL_GPL(torture_shutdown_absorb); 617b5daa8f3SPaul E. McKenney 618b5daa8f3SPaul E. McKenney /* 619e991dbc0SPaul E. McKenney * Cause the torture test to shutdown the system after the test has 620e991dbc0SPaul E. McKenney * run for the time specified by the shutdown_secs parameter. 621e991dbc0SPaul E. McKenney */ 622e991dbc0SPaul E. McKenney static int torture_shutdown(void *arg) 623e991dbc0SPaul E. McKenney { 62431257c3cSPaul E. McKenney ktime_t ktime_snap; 625e991dbc0SPaul E. McKenney 626e991dbc0SPaul E. McKenney VERBOSE_TOROUT_STRING("torture_shutdown task started"); 62731257c3cSPaul E. McKenney ktime_snap = ktime_get(); 62831257c3cSPaul E. McKenney while (ktime_before(ktime_snap, shutdown_time) && 629e991dbc0SPaul E. McKenney !torture_must_stop()) { 630e991dbc0SPaul E. McKenney if (verbose) 631e991dbc0SPaul E. McKenney pr_alert("%s" TORTURE_FLAG 63231257c3cSPaul E. McKenney "torture_shutdown task: %llu ms remaining\n", 63331257c3cSPaul E. McKenney torture_type, 63431257c3cSPaul E. McKenney ktime_ms_delta(shutdown_time, ktime_snap)); 63531257c3cSPaul E. McKenney set_current_state(TASK_INTERRUPTIBLE); 63631257c3cSPaul E. McKenney schedule_hrtimeout(&shutdown_time, HRTIMER_MODE_ABS); 63731257c3cSPaul E. McKenney ktime_snap = ktime_get(); 638e991dbc0SPaul E. McKenney } 639e991dbc0SPaul E. McKenney if (torture_must_stop()) { 6407fafaac5SPaul E. McKenney torture_kthread_stopping("torture_shutdown"); 641e991dbc0SPaul E. McKenney return 0; 642e991dbc0SPaul E. McKenney } 643e991dbc0SPaul E. McKenney 644e991dbc0SPaul E. McKenney /* OK, shut down the system. */ 645e991dbc0SPaul E. McKenney 646e991dbc0SPaul E. McKenney VERBOSE_TOROUT_STRING("torture_shutdown task shutting down system"); 647e991dbc0SPaul E. McKenney shutdown_task = NULL; /* Avoid self-kill deadlock. */ 648f881825aSPaul E. McKenney if (torture_shutdown_hook) 649f881825aSPaul E. McKenney torture_shutdown_hook(); 650f881825aSPaul E. McKenney else 651f881825aSPaul E. McKenney VERBOSE_TOROUT_STRING("No torture_shutdown_hook(), skipping."); 6522102ad29SPaul E. McKenney if (ftrace_dump_at_shutdown) 653dac95906SPaul E. McKenney rcu_ftrace_dump(DUMP_ALL); 654e991dbc0SPaul E. McKenney kernel_power_off(); /* Shut down the system. */ 655e991dbc0SPaul E. McKenney return 0; 656e991dbc0SPaul E. McKenney } 657e991dbc0SPaul E. McKenney 658e991dbc0SPaul E. McKenney /* 659e991dbc0SPaul E. McKenney * Start up the shutdown task. 660e991dbc0SPaul E. McKenney */ 661e991dbc0SPaul E. McKenney int torture_shutdown_init(int ssecs, void (*cleanup)(void)) 662e991dbc0SPaul E. McKenney { 663e991dbc0SPaul E. McKenney torture_shutdown_hook = cleanup; 66431257c3cSPaul E. McKenney if (ssecs > 0) { 66531257c3cSPaul E. McKenney shutdown_time = ktime_add(ktime_get(), ktime_set(ssecs, 0)); 6662a7d9688SPierce Griffiths return torture_create_kthread(torture_shutdown, NULL, 66747cf29b9SPaul E. McKenney shutdown_task); 66847cf29b9SPaul E. McKenney } 6692a7d9688SPierce Griffiths return 0; 670e991dbc0SPaul E. McKenney } 671e991dbc0SPaul E. McKenney EXPORT_SYMBOL_GPL(torture_shutdown_init); 672e991dbc0SPaul E. McKenney 673e991dbc0SPaul E. McKenney /* 6744622b487SPaul E. McKenney * Detect and respond to a system shutdown. 6754622b487SPaul E. McKenney */ 6764622b487SPaul E. McKenney static int torture_shutdown_notify(struct notifier_block *unused1, 6774622b487SPaul E. McKenney unsigned long unused2, void *unused3) 6784622b487SPaul E. McKenney { 6794622b487SPaul E. McKenney mutex_lock(&fullstop_mutex); 6807d0ae808SPaul E. McKenney if (READ_ONCE(fullstop) == FULLSTOP_DONTSTOP) { 681fac480efSPaul E. McKenney VERBOSE_TOROUT_STRING("Unscheduled system shutdown detected"); 6827d0ae808SPaul E. McKenney WRITE_ONCE(fullstop, FULLSTOP_SHUTDOWN); 683fac480efSPaul E. McKenney } else { 6844622b487SPaul E. McKenney pr_warn("Concurrent rmmod and shutdown illegal!\n"); 685fac480efSPaul E. McKenney } 6864622b487SPaul E. McKenney mutex_unlock(&fullstop_mutex); 6874622b487SPaul E. McKenney return NOTIFY_DONE; 6884622b487SPaul E. McKenney } 6894622b487SPaul E. McKenney 6904622b487SPaul E. McKenney static struct notifier_block torture_shutdown_nb = { 6914622b487SPaul E. McKenney .notifier_call = torture_shutdown_notify, 6924622b487SPaul E. McKenney }; 6934622b487SPaul E. McKenney 6944622b487SPaul E. McKenney /* 695bfefc73aSPaul E. McKenney * Shut down the shutdown task. Say what??? Heh! This can happen if 696bfefc73aSPaul E. McKenney * the torture module gets an rmmod before the shutdown time arrives. ;-) 697bfefc73aSPaul E. McKenney */ 698bfefc73aSPaul E. McKenney static void torture_shutdown_cleanup(void) 699bfefc73aSPaul E. McKenney { 700bfefc73aSPaul E. McKenney unregister_reboot_notifier(&torture_shutdown_nb); 701bfefc73aSPaul E. McKenney if (shutdown_task != NULL) { 702bfefc73aSPaul E. McKenney VERBOSE_TOROUT_STRING("Stopping torture_shutdown task"); 703bfefc73aSPaul E. McKenney kthread_stop(shutdown_task); 704bfefc73aSPaul E. McKenney } 705bfefc73aSPaul E. McKenney shutdown_task = NULL; 706bfefc73aSPaul E. McKenney } 707bfefc73aSPaul E. McKenney 708bfefc73aSPaul E. McKenney /* 709628edaa5SPaul E. McKenney * Variables for stuttering, which means to periodically pause and 710628edaa5SPaul E. McKenney * restart testing in order to catch bugs that appear when load is 711628edaa5SPaul E. McKenney * suddenly applied to or removed from the system. 712628edaa5SPaul E. McKenney */ 713628edaa5SPaul E. McKenney static struct task_struct *stutter_task; 714628edaa5SPaul E. McKenney static int stutter_pause_test; 715628edaa5SPaul E. McKenney static int stutter; 716ff3bf92dSPaul E. McKenney static int stutter_gap; 717628edaa5SPaul E. McKenney 718628edaa5SPaul E. McKenney /* 719628edaa5SPaul E. McKenney * Block until the stutter interval ends. This must be called periodically 720628edaa5SPaul E. McKenney * by all running kthreads that need to be subject to stuttering. 721628edaa5SPaul E. McKenney */ 722474e59b4SPaul E. McKenney bool stutter_wait(const char *title) 723628edaa5SPaul E. McKenney { 72419012b78SPaul E. McKenney unsigned int i = 0; 725e8516c64SPaul E. McKenney bool ret = false; 72619012b78SPaul E. McKenney int spt; 7274ced3314SPaul E. McKenney 728cee43939SPaul E. McKenney cond_resched_tasks_rcu_qs(); 7294ced3314SPaul E. McKenney spt = READ_ONCE(stutter_pause_test); 73029d39390SPaul E. McKenney for (; spt; spt = READ_ONCE(stutter_pause_test)) { 731ab1b7880SPaul E. McKenney if (!ret) { 732ab1b7880SPaul E. McKenney sched_set_normal(current, MAX_NICE); 733e8516c64SPaul E. McKenney ret = true; 734ab1b7880SPaul E. McKenney } 7354ced3314SPaul E. McKenney if (spt == 1) { 736628edaa5SPaul E. McKenney schedule_timeout_interruptible(1); 7374ced3314SPaul E. McKenney } else if (spt == 2) { 73819012b78SPaul E. McKenney while (READ_ONCE(stutter_pause_test)) { 739ed24affaSPaul E. McKenney if (!(i++ & 0xffff)) 740ed24affaSPaul E. McKenney torture_hrtimeout_us(10, 0, NULL); 741ab7d4505SPaul E. McKenney cond_resched(); 74219012b78SPaul E. McKenney } 7434ced3314SPaul E. McKenney } else { 744628edaa5SPaul E. McKenney schedule_timeout_interruptible(round_jiffies_relative(HZ)); 7454ced3314SPaul E. McKenney } 746628edaa5SPaul E. McKenney torture_shutdown_absorb(title); 747628edaa5SPaul E. McKenney } 748e8516c64SPaul E. McKenney return ret; 749628edaa5SPaul E. McKenney } 750628edaa5SPaul E. McKenney EXPORT_SYMBOL_GPL(stutter_wait); 751628edaa5SPaul E. McKenney 752628edaa5SPaul E. McKenney /* 753628edaa5SPaul E. McKenney * Cause the torture test to "stutter", starting and stopping all 754628edaa5SPaul E. McKenney * threads periodically. 755628edaa5SPaul E. McKenney */ 756628edaa5SPaul E. McKenney static int torture_stutter(void *arg) 757628edaa5SPaul E. McKenney { 758fda5ba9eSPaul E. McKenney DEFINE_TORTURE_RANDOM(rand); 759e8516c64SPaul E. McKenney int wtime; 760e8516c64SPaul E. McKenney 761628edaa5SPaul E. McKenney VERBOSE_TOROUT_STRING("torture_stutter task started"); 762628edaa5SPaul E. McKenney do { 7634ced3314SPaul E. McKenney if (!torture_must_stop() && stutter > 1) { 764e8516c64SPaul E. McKenney wtime = stutter; 765fda5ba9eSPaul E. McKenney if (stutter > 2) { 7664ced3314SPaul E. McKenney WRITE_ONCE(stutter_pause_test, 1); 767fda5ba9eSPaul E. McKenney wtime = stutter - 3; 768ed24affaSPaul E. McKenney torture_hrtimeout_jiffies(wtime, &rand); 769fda5ba9eSPaul E. McKenney wtime = 2; 770e8516c64SPaul E. McKenney } 7717d0ae808SPaul E. McKenney WRITE_ONCE(stutter_pause_test, 2); 772ed24affaSPaul E. McKenney torture_hrtimeout_jiffies(wtime, NULL); 773628edaa5SPaul E. McKenney } 7744ced3314SPaul E. McKenney WRITE_ONCE(stutter_pause_test, 0); 775628edaa5SPaul E. McKenney if (!torture_must_stop()) 776ed24affaSPaul E. McKenney torture_hrtimeout_jiffies(stutter_gap, NULL); 777628edaa5SPaul E. McKenney torture_shutdown_absorb("torture_stutter"); 778628edaa5SPaul E. McKenney } while (!torture_must_stop()); 7797fafaac5SPaul E. McKenney torture_kthread_stopping("torture_stutter"); 780628edaa5SPaul E. McKenney return 0; 781628edaa5SPaul E. McKenney } 782628edaa5SPaul E. McKenney 783628edaa5SPaul E. McKenney /* 784628edaa5SPaul E. McKenney * Initialize and kick off the torture_stutter kthread. 785628edaa5SPaul E. McKenney */ 786ff3bf92dSPaul E. McKenney int torture_stutter_init(const int s, const int sgap) 787628edaa5SPaul E. McKenney { 788628edaa5SPaul E. McKenney stutter = s; 789ff3bf92dSPaul E. McKenney stutter_gap = sgap; 7902a7d9688SPierce Griffiths return torture_create_kthread(torture_stutter, NULL, stutter_task); 791628edaa5SPaul E. McKenney } 792628edaa5SPaul E. McKenney EXPORT_SYMBOL_GPL(torture_stutter_init); 793628edaa5SPaul E. McKenney 794628edaa5SPaul E. McKenney /* 795628edaa5SPaul E. McKenney * Cleanup after the torture_stutter kthread. 796628edaa5SPaul E. McKenney */ 797bfefc73aSPaul E. McKenney static void torture_stutter_cleanup(void) 798628edaa5SPaul E. McKenney { 799628edaa5SPaul E. McKenney if (!stutter_task) 800628edaa5SPaul E. McKenney return; 801628edaa5SPaul E. McKenney VERBOSE_TOROUT_STRING("Stopping torture_stutter task"); 802628edaa5SPaul E. McKenney kthread_stop(stutter_task); 803628edaa5SPaul E. McKenney stutter_task = NULL; 804628edaa5SPaul E. McKenney } 805628edaa5SPaul E. McKenney 806628edaa5SPaul E. McKenney /* 807b5daa8f3SPaul E. McKenney * Initialize torture module. Please note that this is -not- invoked via 808b5daa8f3SPaul E. McKenney * the usual module_init() mechanism, but rather by an explicit call from 809b5daa8f3SPaul E. McKenney * the client torture module. This call must be paired with a later 810b5daa8f3SPaul E. McKenney * torture_init_end(). 811628edaa5SPaul E. McKenney * 812628edaa5SPaul E. McKenney * The runnable parameter points to a flag that controls whether or not 813628edaa5SPaul E. McKenney * the test is currently runnable. If there is no such flag, pass in NULL. 814b5daa8f3SPaul E. McKenney */ 81590127d60SPaul E. McKenney bool torture_init_begin(char *ttype, int v) 816b5daa8f3SPaul E. McKenney { 817b5daa8f3SPaul E. McKenney mutex_lock(&fullstop_mutex); 8185228084eSPaul E. McKenney if (torture_type != NULL) { 8194ac9de07SStephen Zhang pr_alert("%s: Refusing %s init: %s running.\n", 8204ac9de07SStephen Zhang __func__, ttype, torture_type); 8214ac9de07SStephen Zhang pr_alert("%s: One torture test at a time!\n", __func__); 8225228084eSPaul E. McKenney mutex_unlock(&fullstop_mutex); 8235228084eSPaul E. McKenney return false; 8245228084eSPaul E. McKenney } 825b5daa8f3SPaul E. McKenney torture_type = ttype; 826b5daa8f3SPaul E. McKenney verbose = v; 82736970bb9SPaul E. McKenney fullstop = FULLSTOP_DONTSTOP; 8285228084eSPaul E. McKenney return true; 829b5daa8f3SPaul E. McKenney } 830b5daa8f3SPaul E. McKenney EXPORT_SYMBOL_GPL(torture_init_begin); 831b5daa8f3SPaul E. McKenney 832b5daa8f3SPaul E. McKenney /* 833b5daa8f3SPaul E. McKenney * Tell the torture module that initialization is complete. 834b5daa8f3SPaul E. McKenney */ 8352b3f8ffeSPranith Kumar void torture_init_end(void) 836b5daa8f3SPaul E. McKenney { 837b5daa8f3SPaul E. McKenney mutex_unlock(&fullstop_mutex); 8384622b487SPaul E. McKenney register_reboot_notifier(&torture_shutdown_nb); 839b5daa8f3SPaul E. McKenney } 840b5daa8f3SPaul E. McKenney EXPORT_SYMBOL_GPL(torture_init_end); 841cc47ae08SPaul E. McKenney 842cc47ae08SPaul E. McKenney /* 843cc47ae08SPaul E. McKenney * Clean up torture module. Please note that this is -not- invoked via 844cc47ae08SPaul E. McKenney * the usual module_exit() mechanism, but rather by an explicit call from 845cc47ae08SPaul E. McKenney * the client torture module. Returns true if a race with system shutdown 846bfefc73aSPaul E. McKenney * is detected, otherwise, all kthreads started by functions in this file 847bfefc73aSPaul E. McKenney * will be shut down. 848cc47ae08SPaul E. McKenney * 849cc47ae08SPaul E. McKenney * This must be called before the caller starts shutting down its own 850cc47ae08SPaul E. McKenney * kthreads. 851d36a7a0dSDavidlohr Bueso * 852d36a7a0dSDavidlohr Bueso * Both torture_cleanup_begin() and torture_cleanup_end() must be paired, 853d36a7a0dSDavidlohr Bueso * in order to correctly perform the cleanup. They are separated because 854d36a7a0dSDavidlohr Bueso * threads can still need to reference the torture_type type, thus nullify 855d36a7a0dSDavidlohr Bueso * only after completing all other relevant calls. 856cc47ae08SPaul E. McKenney */ 857d36a7a0dSDavidlohr Bueso bool torture_cleanup_begin(void) 858cc47ae08SPaul E. McKenney { 859cc47ae08SPaul E. McKenney mutex_lock(&fullstop_mutex); 8607d0ae808SPaul E. McKenney if (READ_ONCE(fullstop) == FULLSTOP_SHUTDOWN) { 861cc47ae08SPaul E. McKenney pr_warn("Concurrent rmmod and shutdown illegal!\n"); 862cc47ae08SPaul E. McKenney mutex_unlock(&fullstop_mutex); 863cc47ae08SPaul E. McKenney schedule_timeout_uninterruptible(10); 864cc47ae08SPaul E. McKenney return true; 865cc47ae08SPaul E. McKenney } 8667d0ae808SPaul E. McKenney WRITE_ONCE(fullstop, FULLSTOP_RMMOD); 867cc47ae08SPaul E. McKenney mutex_unlock(&fullstop_mutex); 868bfefc73aSPaul E. McKenney torture_shutdown_cleanup(); 869cc47ae08SPaul E. McKenney torture_shuffle_cleanup(); 870bfefc73aSPaul E. McKenney torture_stutter_cleanup(); 871cc47ae08SPaul E. McKenney torture_onoff_cleanup(); 872d36a7a0dSDavidlohr Bueso return false; 873d36a7a0dSDavidlohr Bueso } 874d36a7a0dSDavidlohr Bueso EXPORT_SYMBOL_GPL(torture_cleanup_begin); 875d36a7a0dSDavidlohr Bueso 876d36a7a0dSDavidlohr Bueso void torture_cleanup_end(void) 877d36a7a0dSDavidlohr Bueso { 8785228084eSPaul E. McKenney mutex_lock(&fullstop_mutex); 8795228084eSPaul E. McKenney torture_type = NULL; 8805228084eSPaul E. McKenney mutex_unlock(&fullstop_mutex); 881cc47ae08SPaul E. McKenney } 882d36a7a0dSDavidlohr Bueso EXPORT_SYMBOL_GPL(torture_cleanup_end); 88336970bb9SPaul E. McKenney 88436970bb9SPaul E. McKenney /* 88536970bb9SPaul E. McKenney * Is it time for the current torture test to stop? 88636970bb9SPaul E. McKenney */ 88736970bb9SPaul E. McKenney bool torture_must_stop(void) 88836970bb9SPaul E. McKenney { 88936970bb9SPaul E. McKenney return torture_must_stop_irq() || kthread_should_stop(); 89036970bb9SPaul E. McKenney } 89136970bb9SPaul E. McKenney EXPORT_SYMBOL_GPL(torture_must_stop); 89236970bb9SPaul E. McKenney 89336970bb9SPaul E. McKenney /* 89436970bb9SPaul E. McKenney * Is it time for the current torture test to stop? This is the irq-safe 89536970bb9SPaul E. McKenney * version, hence no check for kthread_should_stop(). 89636970bb9SPaul E. McKenney */ 89736970bb9SPaul E. McKenney bool torture_must_stop_irq(void) 89836970bb9SPaul E. McKenney { 8997d0ae808SPaul E. McKenney return READ_ONCE(fullstop) != FULLSTOP_DONTSTOP; 90036970bb9SPaul E. McKenney } 90136970bb9SPaul E. McKenney EXPORT_SYMBOL_GPL(torture_must_stop_irq); 9027fafaac5SPaul E. McKenney 9037fafaac5SPaul E. McKenney /* 9047fafaac5SPaul E. McKenney * Each kthread must wait for kthread_should_stop() before returning from 9057fafaac5SPaul E. McKenney * its top-level function, otherwise segfaults ensue. This function 9067fafaac5SPaul E. McKenney * prints a "stopping" message and waits for kthread_should_stop(), and 9077fafaac5SPaul E. McKenney * should be called from all torture kthreads immediately prior to 9087fafaac5SPaul E. McKenney * returning. 9097fafaac5SPaul E. McKenney */ 9107fafaac5SPaul E. McKenney void torture_kthread_stopping(char *title) 9117fafaac5SPaul E. McKenney { 9120d6821d5SPaul E. McKenney char buf[128]; 9130d6821d5SPaul E. McKenney 9140d6821d5SPaul E. McKenney snprintf(buf, sizeof(buf), "Stopping %s", title); 9150d6821d5SPaul E. McKenney VERBOSE_TOROUT_STRING(buf); 9167fafaac5SPaul E. McKenney while (!kthread_should_stop()) { 9177fafaac5SPaul E. McKenney torture_shutdown_absorb(title); 9187fafaac5SPaul E. McKenney schedule_timeout_uninterruptible(1); 9197fafaac5SPaul E. McKenney } 9207fafaac5SPaul E. McKenney } 9217fafaac5SPaul E. McKenney EXPORT_SYMBOL_GPL(torture_kthread_stopping); 92247cf29b9SPaul E. McKenney 92347cf29b9SPaul E. McKenney /* 92447cf29b9SPaul E. McKenney * Create a generic torture kthread that is immediately runnable. If you 92547cf29b9SPaul E. McKenney * need the kthread to be stopped so that you can do something to it before 92647cf29b9SPaul E. McKenney * it starts, you will need to open-code your own. 92747cf29b9SPaul E. McKenney */ 92847cf29b9SPaul E. McKenney int _torture_create_kthread(int (*fn)(void *arg), void *arg, char *s, char *m, 92947cf29b9SPaul E. McKenney char *f, struct task_struct **tp) 93047cf29b9SPaul E. McKenney { 93147cf29b9SPaul E. McKenney int ret = 0; 93247cf29b9SPaul E. McKenney 93347cf29b9SPaul E. McKenney VERBOSE_TOROUT_STRING(m); 9346945915eSKees Cook *tp = kthread_run(fn, arg, "%s", s); 93547cf29b9SPaul E. McKenney if (IS_ERR(*tp)) { 93647cf29b9SPaul E. McKenney ret = PTR_ERR(*tp); 937*81faa4f6SLi Zhijian TOROUT_ERRSTRING(f); 93847cf29b9SPaul E. McKenney *tp = NULL; 93947cf29b9SPaul E. McKenney } 94047cf29b9SPaul E. McKenney torture_shuffle_task_register(*tp); 94147cf29b9SPaul E. McKenney return ret; 94247cf29b9SPaul E. McKenney } 94347cf29b9SPaul E. McKenney EXPORT_SYMBOL_GPL(_torture_create_kthread); 9449c029b86SPaul E. McKenney 9459c029b86SPaul E. McKenney /* 9469c029b86SPaul E. McKenney * Stop a generic kthread, emitting a message. 9479c029b86SPaul E. McKenney */ 9489c029b86SPaul E. McKenney void _torture_stop_kthread(char *m, struct task_struct **tp) 9499c029b86SPaul E. McKenney { 9509c029b86SPaul E. McKenney if (*tp == NULL) 9519c029b86SPaul E. McKenney return; 9529c029b86SPaul E. McKenney VERBOSE_TOROUT_STRING(m); 9539c029b86SPaul E. McKenney kthread_stop(*tp); 9549c029b86SPaul E. McKenney *tp = NULL; 9559c029b86SPaul E. McKenney } 9569c029b86SPaul E. McKenney EXPORT_SYMBOL_GPL(_torture_stop_kthread); 957