// SPDX-License-Identifier: GPL-2.0+ /* * Common functions for in-kernel torture tests. * * Copyright (C) IBM Corporation, 2014 * * Author: Paul E. McKenney <paulmck@linux.ibm.com> * Based on kernel/rcu/torture.c.
*/
/* Mediate rmmod and system shutdown. Concurrent rmmod & shutdown illegal! */ #define FULLSTOP_DONTSTOP 0 /* Normal operation. */ #define FULLSTOP_SHUTDOWN 1 /* System shutdown with torture running. */ #define FULLSTOP_RMMOD 2 /* Normal rmmod of torture. */ staticint fullstop = FULLSTOP_RMMOD; static DEFINE_MUTEX(fullstop_mutex);
static atomic_t verbose_sleep_counter;
/* * Sleep if needed from VERBOSE_TOROUT*().
*/ void verbose_torout_sleep(void)
{ if (verbose_sleep_frequency > 0 &&
verbose_sleep_duration > 0 &&
!(atomic_inc_return(&verbose_sleep_counter) % verbose_sleep_frequency))
schedule_timeout_uninterruptible(verbose_sleep_duration);
}
EXPORT_SYMBOL_GPL(verbose_torout_sleep);
/* * 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, constenum hrtimer_mode mode, struct torture_random_state *trsp)
{
ktime_t hto = baset_ns;
/* * 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)
{
ktime_t baset_ns = jiffies_to_nsecs(baset_j);
/* * 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)
{ return READ_ONCE(torture_online_cpus);
}
EXPORT_SYMBOL_GPL(torture_num_online_cpus);
/* * 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, unsignedlong *sum_offl, int *min_offl, int *max_offl)
{ unsignedlong delta; int ret; char *s; unsignedlong starttime;
if (!cpu_online(cpu) || !cpu_is_hotpluggable(cpu)) returnfalse; if (num_online_cpus() <= 1) returnfalse; /* Can't offline the last CPU. */
if (verbose > 1)
pr_alert("%s" TORTURE_FLAG "torture_onoff task: offlining %d\n",
torture_type, cpu);
starttime = jiffies;
(*n_offl_attempts)++;
ret = remove_cpu(cpu); if (ret) {
s = ""; if (!rcu_inkernel_boot_has_ended() && ret == -EBUSY) { // PCI probe frequently disables hotplug during boot.
(*n_offl_attempts)--;
s = " (-EBUSY forgiven during boot)";
} if (verbose)
pr_alert("%s" TORTURE_FLAG "torture_onoff task: offline %d failed%s: errno %d\n",
torture_type, cpu, s, ret);
} else { if (verbose > 1)
pr_alert("%s" TORTURE_FLAG "torture_onoff task: offlined %d\n",
torture_type, cpu); if (onoff_f)
onoff_f();
(*n_offl_successes)++;
delta = jiffies - starttime;
*sum_offl += delta; if (*min_offl < 0) {
*min_offl = delta;
*max_offl = delta;
} if (*min_offl > delta)
*min_offl = delta; if (*max_offl < delta)
*max_offl = delta;
WRITE_ONCE(torture_online_cpus, torture_online_cpus - 1);
WARN_ON_ONCE(torture_online_cpus <= 0);
}
returntrue;
}
EXPORT_SYMBOL_GPL(torture_offline);
/* * 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, unsignedlong *sum_onl, int *min_onl, int *max_onl)
{ unsignedlong delta; int ret; char *s; unsignedlong starttime;
if (cpu_online(cpu) || !cpu_is_hotpluggable(cpu)) returnfalse;
if (verbose > 1)
pr_alert("%s" TORTURE_FLAG "torture_onoff task: onlining %d\n",
torture_type, cpu);
starttime = jiffies;
(*n_onl_attempts)++;
ret = add_cpu(cpu); if (ret) {
s = ""; if (!rcu_inkernel_boot_has_ended() && ret == -EBUSY) { // PCI probe frequently disables hotplug during boot.
(*n_onl_attempts)--;
s = " (-EBUSY forgiven during boot)";
} if (verbose)
pr_alert("%s" TORTURE_FLAG "torture_onoff task: online %d failed%s: errno %d\n",
torture_type, cpu, s, ret);
} else { if (verbose > 1)
pr_alert("%s" TORTURE_FLAG "torture_onoff task: onlined %d\n",
torture_type, cpu);
(*n_onl_successes)++;
delta = jiffies - starttime;
*sum_onl += delta; if (*min_onl < 0) {
*min_onl = delta;
*max_onl = delta;
} if (*min_onl > delta)
*min_onl = delta; if (*max_onl < delta)
*max_onl = delta;
WRITE_ONCE(torture_online_cpus, torture_online_cpus + 1);
}
returntrue;
}
EXPORT_SYMBOL_GPL(torture_online);
/* * Get everything online at the beginning and ends of tests.
*/ staticvoid torture_online_all(char *phase)
{ int cpu; int ret;
for_each_possible_cpu(cpu) { if (cpu_online(cpu)) continue;
ret = add_cpu(cpu); if (ret && verbose) {
pr_alert("%s" TORTURE_FLAG "%s: %s online %d: errno %d\n",
__func__, phase, torture_type, cpu, ret);
}
}
}
/* * Execute random CPU-hotplug operations at the interval specified * by the onoff_interval.
*/ staticint
torture_onoff(void *arg)
{ int cpu; int maxcpu = -1;
DEFINE_TORTURE_RANDOM(rand);
VERBOSE_TOROUT_STRING("torture_onoff task started");
for_each_online_cpu(cpu)
maxcpu = cpu;
WARN_ON(maxcpu < 0);
torture_online_all("Initial"); if (maxcpu == 0) {
VERBOSE_TOROUT_STRING("Only one CPU, so CPU-hotplug testing is disabled"); goto stop;
}
if (onoff_holdoff > 0) {
VERBOSE_TOROUT_STRING("torture_onoff begin holdoff");
torture_hrtimeout_jiffies(onoff_holdoff, &rand);
VERBOSE_TOROUT_STRING("torture_onoff end holdoff");
} while (!torture_must_stop()) { if (disable_onoff_at_boot && !rcu_inkernel_boot_has_ended()) {
torture_hrtimeout_jiffies(HZ / 10, &rand); continue;
}
cpu = torture_random(&rand) % (maxcpu + 1); if (!torture_offline(cpu,
&n_offline_attempts, &n_offline_successes,
&sum_offline, &min_offline, &max_offline))
torture_online(cpu,
&n_online_attempts, &n_online_successes,
&sum_online, &min_online, &max_online);
torture_hrtimeout_jiffies(onoff_interval, &rand);
}
#define TORTURE_RANDOM_MULT 39916801 /* prime */ #define TORTURE_RANDOM_ADD 479001701 /* prime */ #define TORTURE_RANDOM_REFRESH 10000
/* * Crude but fast random-number generator. Uses a linear congruential * generator, with occasional help from cpu_clock().
*/ unsignedlong
torture_random(struct torture_random_state *trsp)
{ if (--trsp->trs_count < 0) {
trsp->trs_state += (unsignedlong)local_clock() + raw_smp_processor_id();
trsp->trs_count = TORTURE_RANDOM_REFRESH;
}
trsp->trs_state = trsp->trs_state * TORTURE_RANDOM_MULT +
TORTURE_RANDOM_ADD; return swahw32(trsp->trs_state);
}
EXPORT_SYMBOL_GPL(torture_random);
/* * 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 { struct list_head st_l; struct task_struct *st_t;
};
staticlong shuffle_interval; /* In jiffies. */ staticstruct task_struct *shuffler_task; static cpumask_var_t shuffle_tmp_mask; staticint shuffle_idle_cpu; /* Force all torture tasks off this CPU */ staticstruct list_head shuffle_task_list = LIST_HEAD_INIT(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)
{ struct shuffle_task *stp;
/* * Unregister all tasks, for example, at the end of the torture run.
*/ staticvoid torture_shuffle_task_unregister_all(void)
{ struct shuffle_task *stp; struct shuffle_task *p;
/* 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.
*/ staticvoid torture_shuffle_tasks(struct torture_random_state *trp)
{ struct shuffle_task *stp;
/* No point in shuffling if there is only one online CPU (ex: UP) */ if (num_online_cpus() == 1) {
cpus_read_unlock(); return;
}
/* Advance to the next CPU. Upon overflow, don't idle any CPUs. */
shuffle_idle_cpu = cpumask_next(shuffle_idle_cpu, shuffle_tmp_mask); if (shuffle_idle_cpu >= nr_cpu_ids)
shuffle_idle_cpu = -1; else
cpumask_clear_cpu(shuffle_idle_cpu, shuffle_tmp_mask);
/* 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.
*/ staticint torture_shuffle(void *arg)
{
DEFINE_TORTURE_RANDOM(rand);
VERBOSE_TOROUT_STRING("torture_shuffle task started"); do {
torture_hrtimeout_jiffies(shuffle_interval, &rand);
torture_shuffle_tasks(&rand);
torture_shutdown_absorb("torture_shuffle");
} while (!torture_must_stop());
torture_kthread_stopping("torture_shuffle"); return 0;
}
/* * Start the shuffler, with shuffint in jiffies.
*/ int torture_shuffle_init(long shuffint)
{
shuffle_interval = shuffint;
shuffle_idle_cpu = -1;
if (!alloc_cpumask_var(&shuffle_tmp_mask, GFP_KERNEL)) {
TOROUT_ERRSTRING("Failed to alloc mask"); return -ENOMEM;
}
/* * Variables for auto-shutdown. This allows "lights out" torture runs * to be fully scripted.
*/ staticstruct task_struct *shutdown_task; static ktime_t shutdown_time; /* time to system shutdown. */ staticvoid (*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(constchar *title)
{ while (READ_ONCE(fullstop) == FULLSTOP_SHUTDOWN) {
pr_notice("torture thread %s parking due to system shutdown\n",
title);
schedule_timeout_uninterruptible(MAX_SCHEDULE_TIMEOUT);
}
}
EXPORT_SYMBOL_GPL(torture_shutdown_absorb);
/* * Cause the torture test to shutdown the system after the test has * run for the time specified by the shutdown_secs parameter.
*/ staticint torture_shutdown(void *arg)
{
ktime_t ktime_snap;
VERBOSE_TOROUT_STRING("torture_shutdown task started");
ktime_snap = ktime_get(); while (ktime_before(ktime_snap, shutdown_time) &&
!torture_must_stop()) { if (verbose)
pr_alert("%s" TORTURE_FLAG "torture_shutdown task: %llu ms remaining\n",
torture_type,
ktime_ms_delta(shutdown_time, ktime_snap));
set_current_state(TASK_INTERRUPTIBLE);
schedule_hrtimeout(&shutdown_time, HRTIMER_MODE_ABS);
ktime_snap = ktime_get();
} if (torture_must_stop()) {
torture_kthread_stopping("torture_shutdown"); return 0;
}
/* OK, shut down the system. */
VERBOSE_TOROUT_STRING("torture_shutdown task shutting down system");
shutdown_task = NULL; /* Avoid self-kill deadlock. */ if (torture_shutdown_hook)
torture_shutdown_hook(); else
VERBOSE_TOROUT_STRING("No torture_shutdown_hook(), skipping."); if (ftrace_dump_at_shutdown)
rcu_ftrace_dump(DUMP_ALL);
kernel_power_off(); /* Shut down the system. */ return 0;
}
/* * Start up the shutdown task.
*/ int torture_shutdown_init(int ssecs, void (*cleanup)(void))
{
torture_shutdown_hook = cleanup; if (ssecs > 0) {
shutdown_time = ktime_add(ktime_get(), ktime_set(ssecs, 0)); return torture_create_kthread(torture_shutdown, NULL,
shutdown_task);
} return 0;
}
EXPORT_SYMBOL_GPL(torture_shutdown_init);
/* * Detect and respond to a system shutdown.
*/ staticint torture_shutdown_notify(struct notifier_block *unused1, unsignedlong unused2, void *unused3)
{
mutex_lock(&fullstop_mutex); if (READ_ONCE(fullstop) == FULLSTOP_DONTSTOP) {
VERBOSE_TOROUT_STRING("Unscheduled system shutdown detected");
WRITE_ONCE(fullstop, FULLSTOP_SHUTDOWN);
} else {
pr_warn("Concurrent rmmod and shutdown illegal!\n");
}
mutex_unlock(&fullstop_mutex); return NOTIFY_DONE;
}
/* * Shut down the shutdown task. Say what??? Heh! This can happen if * the torture module gets an rmmod before the shutdown time arrives. ;-)
*/ staticvoid torture_shutdown_cleanup(void)
{
unregister_reboot_notifier(&torture_shutdown_nb); if (shutdown_task != NULL) {
VERBOSE_TOROUT_STRING("Stopping torture_shutdown task");
kthread_stop(shutdown_task);
}
shutdown_task = NULL;
}
/* * 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.
*/ staticstruct task_struct *stutter_task; static ktime_t stutter_till_abs_time; staticint stutter; staticint 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(constchar *title)
{ bool ret = false;
ktime_t till_ns;
/* * 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)
{
mutex_lock(&fullstop_mutex); if (torture_type != NULL) {
pr_alert("%s: Refusing %s init: %s running.\n",
__func__, ttype, torture_type);
pr_alert("%s: One torture test at a time!\n", __func__);
mutex_unlock(&fullstop_mutex); returnfalse;
}
torture_type = ttype;
verbose = v;
fullstop = FULLSTOP_DONTSTOP;
WRITE_ONCE(torture_init_jiffies, jiffies); // Lockless reads.
torture_print_module_parms(); returntrue;
}
EXPORT_SYMBOL_GPL(torture_init_begin);
/* * Tell the torture module that initialization is complete.
*/ void torture_init_end(void)
{
mutex_unlock(&fullstop_mutex);
register_reboot_notifier(&torture_shutdown_nb);
}
EXPORT_SYMBOL_GPL(torture_init_end);
/* * Get the torture_init_begin()-time value of the jiffies counter.
*/ unsignedlong get_torture_init_jiffies(void)
{ return READ_ONCE(torture_init_jiffies);
}
EXPORT_SYMBOL_GPL(get_torture_init_jiffies);
/* * 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)
{
mutex_lock(&fullstop_mutex); if (READ_ONCE(fullstop) == FULLSTOP_SHUTDOWN) {
pr_warn("Concurrent rmmod and shutdown illegal!\n");
mutex_unlock(&fullstop_mutex);
schedule_timeout_uninterruptible(10); returntrue;
}
WRITE_ONCE(fullstop, FULLSTOP_RMMOD);
mutex_unlock(&fullstop_mutex);
torture_shutdown_cleanup();
torture_shuffle_cleanup();
torture_stutter_cleanup();
torture_onoff_cleanup(); returnfalse;
}
EXPORT_SYMBOL_GPL(torture_cleanup_begin);
/* * Is it time for the current torture test to stop?
*/ bool torture_must_stop(void)
{ return torture_must_stop_irq() || kthread_should_stop();
}
EXPORT_SYMBOL_GPL(torture_must_stop);
/* * 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)
{ return READ_ONCE(fullstop) != FULLSTOP_DONTSTOP;
}
EXPORT_SYMBOL_GPL(torture_must_stop_irq);
/* * 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)
{ char buf[128];
snprintf(buf, sizeof(buf), "%s is stopping", title);
VERBOSE_TOROUT_STRING(buf); while (!kthread_should_stop()) {
torture_shutdown_absorb(title);
schedule_timeout_uninterruptible(HZ / 20);
}
}
EXPORT_SYMBOL_GPL(torture_kthread_stopping);
/* * 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))
{ int ret = 0;
VERBOSE_TOROUT_STRING(m);
*tp = kthread_create(fn, arg, "%s", s); if (IS_ERR(*tp)) {
ret = PTR_ERR(*tp);
TOROUT_ERRSTRING(f);
*tp = NULL; return ret;
}
if (cbf)
cbf(*tp);
wake_up_process(*tp); // Process is sleeping, so ordering provided.
torture_shuffle_task_register(*tp); return ret;
}
EXPORT_SYMBOL_GPL(_torture_create_kthread);
/* * Stop a generic kthread, emitting a message.
*/ void _torture_stop_kthread(char *m, struct task_struct **tp)
{ if (*tp == NULL) return;
VERBOSE_TOROUT_STRING(m);
kthread_stop(*tp);
*tp = NULL;
}
EXPORT_SYMBOL_GPL(_torture_stop_kthread);
¤ Dauer der Verarbeitung: 0.40 Sekunden
(vorverarbeitet)
¤
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung ist noch experimentell.