// SPDX-License-Identifier: GPL-2.0+ /* * Common functions for in-kernel torture tests. * * Copyright (C) IBM Corporation, 2014 * * Author: Paul E. McKenney <[email protected]> * Based on kernel/rcu/torture.c. */ #define pr_fmt(fmt) … #include <linux/types.h> #include <linux/kernel.h> #include <linux/init.h> #include <linux/module.h> #include <linux/kthread.h> #include <linux/err.h> #include <linux/spinlock.h> #include <linux/smp.h> #include <linux/interrupt.h> #include <linux/sched.h> #include <linux/sched/clock.h> #include <linux/atomic.h> #include <linux/bitops.h> #include <linux/completion.h> #include <linux/moduleparam.h> #include <linux/percpu.h> #include <linux/notifier.h> #include <linux/reboot.h> #include <linux/freezer.h> #include <linux/cpu.h> #include <linux/delay.h> #include <linux/stat.h> #include <linux/slab.h> #include <linux/trace_clock.h> #include <linux/ktime.h> #include <asm/byteorder.h> #include <linux/torture.h> #include <linux/sched/rt.h> #include "rcu/rcu.h" MODULE_DESCRIPTION(…) …; MODULE_LICENSE(…) …; MODULE_AUTHOR(…) …; static bool disable_onoff_at_boot; module_param(disable_onoff_at_boot, bool, 0444); static bool ftrace_dump_at_shutdown; module_param(ftrace_dump_at_shutdown, bool, 0444); static int verbose_sleep_frequency; module_param(verbose_sleep_frequency, int, 0444); static int verbose_sleep_duration = …; module_param(verbose_sleep_duration, int, 0444); static int random_shuffle; module_param(random_shuffle, int, 0444); static char *torture_type; static int verbose; /* Mediate rmmod and system shutdown. Concurrent rmmod & shutdown illegal! */ #define FULLSTOP_DONTSTOP … #define FULLSTOP_SHUTDOWN … #define FULLSTOP_RMMOD … static int fullstop = …; static DEFINE_MUTEX(fullstop_mutex); static atomic_t verbose_sleep_counter; /* * Sleep if needed from VERBOSE_TOROUT*(). */ void verbose_torout_sleep(void) { … } EXPORT_SYMBOL_GPL(…); /* * Schedule a high-resolution-timer sleep in nanoseconds, with a 32-bit * nanosecond random fuzz. This function and its friends desynchronize * testing from the timer wheel. */ int torture_hrtimeout_ns(ktime_t baset_ns, u32 fuzzt_ns, const enum hrtimer_mode mode, struct torture_random_state *trsp) { … } EXPORT_SYMBOL_GPL(…); /* * Schedule a high-resolution-timer sleep in microseconds, with a 32-bit * nanosecond (not microsecond!) random fuzz. */ int torture_hrtimeout_us(u32 baset_us, u32 fuzzt_ns, struct torture_random_state *trsp) { … } EXPORT_SYMBOL_GPL(…); /* * Schedule a high-resolution-timer sleep in milliseconds, with a 32-bit * microsecond (not millisecond!) random fuzz. */ int torture_hrtimeout_ms(u32 baset_ms, u32 fuzzt_us, struct torture_random_state *trsp) { … } EXPORT_SYMBOL_GPL(…); /* * Schedule a high-resolution-timer sleep in jiffies, with an * implied one-jiffy random fuzz. This is intended to replace calls to * schedule_timeout_interruptible() and friends. */ int torture_hrtimeout_jiffies(u32 baset_j, struct torture_random_state *trsp) { … } EXPORT_SYMBOL_GPL(…); /* * Schedule a high-resolution-timer sleep in milliseconds, with a 32-bit * millisecond (not second!) random fuzz. */ int torture_hrtimeout_s(u32 baset_s, u32 fuzzt_ms, struct torture_random_state *trsp) { … } EXPORT_SYMBOL_GPL(…); #ifdef CONFIG_HOTPLUG_CPU /* * Variables for online-offline handling. Only present if CPU hotplug * is enabled, otherwise does nothing. */ static struct task_struct *onoff_task; static long onoff_holdoff; static long onoff_interval; static torture_ofl_func *onoff_f; static long n_offline_attempts; static long n_offline_successes; static unsigned long sum_offline; static int min_offline = …; static int max_offline; static long n_online_attempts; static long n_online_successes; static unsigned long sum_online; static int min_online = …; static int max_online; static int torture_online_cpus = …; /* * Some torture testing leverages confusion as to the number of online * CPUs. This function returns the torture-testing view of this number, * which allows torture tests to load-balance appropriately. */ int torture_num_online_cpus(void) { … } EXPORT_SYMBOL_GPL(…); /* * Attempt to take a CPU offline. Return false if the CPU is already * offline or if it is not subject to CPU-hotplug operations. The * caller can detect other failures by looking at the statistics. */ bool torture_offline(int cpu, long *n_offl_attempts, long *n_offl_successes, unsigned long *sum_offl, int *min_offl, int *max_offl) { … } EXPORT_SYMBOL_GPL(…); /* * Attempt to bring a CPU online. Return false if the CPU is already * online or if it is not subject to CPU-hotplug operations. The * caller can detect other failures by looking at the statistics. */ bool torture_online(int cpu, long *n_onl_attempts, long *n_onl_successes, unsigned long *sum_onl, int *min_onl, int *max_onl) { … } EXPORT_SYMBOL_GPL(…); /* * Get everything online at the beginning and ends of tests. */ static void torture_online_all(char *phase) { … } /* * Execute random CPU-hotplug operations at the interval specified * by the onoff_interval. */ static int torture_onoff(void *arg) { … } #endif /* #ifdef CONFIG_HOTPLUG_CPU */ /* * Initiate online-offline handling. */ int torture_onoff_init(long ooholdoff, long oointerval, torture_ofl_func *f) { … } EXPORT_SYMBOL_GPL(…); /* * Clean up after online/offline testing. */ static void torture_onoff_cleanup(void) { … } /* * Print online/offline testing statistics. */ void torture_onoff_stats(void) { … } EXPORT_SYMBOL_GPL(…); /* * Were all the online/offline operations successful? */ bool torture_onoff_failures(void) { … } EXPORT_SYMBOL_GPL(…); #define TORTURE_RANDOM_MULT … #define TORTURE_RANDOM_ADD … #define TORTURE_RANDOM_REFRESH … /* * Crude but fast random-number generator. Uses a linear congruential * generator, with occasional help from cpu_clock(). */ unsigned long torture_random(struct torture_random_state *trsp) { … } EXPORT_SYMBOL_GPL(…); /* * Variables for shuffling. The idea is to ensure that each CPU stays * idle for an extended period to test interactions with dyntick idle, * as well as interactions with any per-CPU variables. */ struct shuffle_task { … }; static long shuffle_interval; /* In jiffies. */ static struct task_struct *shuffler_task; static cpumask_var_t shuffle_tmp_mask; static int shuffle_idle_cpu; /* Force all torture tasks off this CPU */ static struct list_head shuffle_task_list = …; static DEFINE_MUTEX(shuffle_task_mutex); /* * Register a task to be shuffled. If there is no memory, just splat * and don't bother registering. */ void torture_shuffle_task_register(struct task_struct *tp) { … } EXPORT_SYMBOL_GPL(…); /* * Unregister all tasks, for example, at the end of the torture run. */ static void torture_shuffle_task_unregister_all(void) { … } /* Shuffle tasks such that we allow shuffle_idle_cpu to become idle. * A special case is when shuffle_idle_cpu = -1, in which case we allow * the tasks to run on all CPUs. */ static void torture_shuffle_tasks(struct torture_random_state *trp) { … } /* Shuffle tasks across CPUs, with the intent of allowing each CPU in the * system to become idle at a time and cut off its timer ticks. This is meant * to test the support for such tickless idle CPU in RCU. */ static int torture_shuffle(void *arg) { … } /* * Start the shuffler, with shuffint in jiffies. */ int torture_shuffle_init(long shuffint) { … } EXPORT_SYMBOL_GPL(…); /* * Stop the shuffling. */ static void torture_shuffle_cleanup(void) { … } /* * Variables for auto-shutdown. This allows "lights out" torture runs * to be fully scripted. */ static struct task_struct *shutdown_task; static ktime_t shutdown_time; /* time to system shutdown. */ static void (*torture_shutdown_hook)(void); /* * Absorb kthreads into a kernel function that won't return, so that * they won't ever access module text or data again. */ void torture_shutdown_absorb(const char *title) { … } EXPORT_SYMBOL_GPL(…); /* * Cause the torture test to shutdown the system after the test has * run for the time specified by the shutdown_secs parameter. */ static int torture_shutdown(void *arg) { … } /* * Start up the shutdown task. */ int torture_shutdown_init(int ssecs, void (*cleanup)(void)) { … } EXPORT_SYMBOL_GPL(…); /* * Detect and respond to a system shutdown. */ static int torture_shutdown_notify(struct notifier_block *unused1, unsigned long unused2, void *unused3) { … } static struct notifier_block torture_shutdown_nb = …; /* * Shut down the shutdown task. Say what??? Heh! This can happen if * the torture module gets an rmmod before the shutdown time arrives. ;-) */ static void torture_shutdown_cleanup(void) { … } /* * Variables for stuttering, which means to periodically pause and * restart testing in order to catch bugs that appear when load is * suddenly applied to or removed from the system. */ static struct task_struct *stutter_task; static ktime_t stutter_till_abs_time; static int stutter; static int stutter_gap; /* * Block until the stutter interval ends. This must be called periodically * by all running kthreads that need to be subject to stuttering. */ bool stutter_wait(const char *title) { … } EXPORT_SYMBOL_GPL(…); /* * Cause the torture test to "stutter", starting and stopping all * threads periodically. */ static int torture_stutter(void *arg) { … } /* * Initialize and kick off the torture_stutter kthread. */ int torture_stutter_init(const int s, const int sgap) { … } EXPORT_SYMBOL_GPL(…); /* * Cleanup after the torture_stutter kthread. */ static void torture_stutter_cleanup(void) { … } static void torture_print_module_parms(void) { … } /* * Initialize torture module. Please note that this is -not- invoked via * the usual module_init() mechanism, but rather by an explicit call from * the client torture module. This call must be paired with a later * torture_init_end(). * * The runnable parameter points to a flag that controls whether or not * the test is currently runnable. If there is no such flag, pass in NULL. */ bool torture_init_begin(char *ttype, int v) { … } EXPORT_SYMBOL_GPL(…); /* * Tell the torture module that initialization is complete. */ void torture_init_end(void) { … } EXPORT_SYMBOL_GPL(…); /* * Clean up torture module. Please note that this is -not- invoked via * the usual module_exit() mechanism, but rather by an explicit call from * the client torture module. Returns true if a race with system shutdown * is detected, otherwise, all kthreads started by functions in this file * will be shut down. * * This must be called before the caller starts shutting down its own * kthreads. * * Both torture_cleanup_begin() and torture_cleanup_end() must be paired, * in order to correctly perform the cleanup. They are separated because * threads can still need to reference the torture_type type, thus nullify * only after completing all other relevant calls. */ bool torture_cleanup_begin(void) { … } EXPORT_SYMBOL_GPL(…); void torture_cleanup_end(void) { … } EXPORT_SYMBOL_GPL(…); /* * Is it time for the current torture test to stop? */ bool torture_must_stop(void) { … } EXPORT_SYMBOL_GPL(…); /* * Is it time for the current torture test to stop? This is the irq-safe * version, hence no check for kthread_should_stop(). */ bool torture_must_stop_irq(void) { … } EXPORT_SYMBOL_GPL(…); /* * Each kthread must wait for kthread_should_stop() before returning from * its top-level function, otherwise segfaults ensue. This function * prints a "stopping" message and waits for kthread_should_stop(), and * should be called from all torture kthreads immediately prior to * returning. */ void torture_kthread_stopping(char *title) { … } EXPORT_SYMBOL_GPL(…); /* * Create a generic torture kthread that is immediately runnable. If you * need the kthread to be stopped so that you can do something to it before * it starts, you will need to open-code your own. */ int _torture_create_kthread(int (*fn)(void *arg), void *arg, char *s, char *m, char *f, struct task_struct **tp, void (*cbf)(struct task_struct *tp)) { … } EXPORT_SYMBOL_GPL(…); /* * Stop a generic kthread, emitting a message. */ void _torture_stop_kthread(char *m, struct task_struct **tp) { … } EXPORT_SYMBOL_GPL(…);