blob: a1a41484ff6d6688bef6def0974d9df9d020b278 [file] [log] [blame]
Paul E. McKenney8f8e76c2019-01-17 10:41:31 -08001// SPDX-License-Identifier: GPL-2.0+
Paul E. McKenney51b11302014-01-27 11:49:39 -08002/*
3 * Common functions for in-kernel torture tests.
4 *
Paul E. McKenney51b11302014-01-27 11:49:39 -08005 * Copyright (C) IBM Corporation, 2014
6 *
Paul E. McKenney8f8e76c2019-01-17 10:41:31 -08007 * Author: Paul E. McKenney <paulmck@linux.ibm.com>
Paul E. McKenney51b11302014-01-27 11:49:39 -08008 * Based on kernel/rcu/torture.c.
9 */
Paul E. McKenney60500032018-05-15 12:25:05 -070010
11#define pr_fmt(fmt) fmt
12
Paul E. McKenney51b11302014-01-27 11:49:39 -080013#include <linux/types.h>
14#include <linux/kernel.h>
15#include <linux/init.h>
16#include <linux/module.h>
17#include <linux/kthread.h>
18#include <linux/err.h>
19#include <linux/spinlock.h>
20#include <linux/smp.h>
21#include <linux/interrupt.h>
22#include <linux/sched.h>
Ingo Molnare6017572017-02-01 16:36:40 +010023#include <linux/sched/clock.h>
Paul E. McKenney51b11302014-01-27 11:49:39 -080024#include <linux/atomic.h>
25#include <linux/bitops.h>
26#include <linux/completion.h>
27#include <linux/moduleparam.h>
28#include <linux/percpu.h>
29#include <linux/notifier.h>
30#include <linux/reboot.h>
31#include <linux/freezer.h>
32#include <linux/cpu.h>
33#include <linux/delay.h>
34#include <linux/stat.h>
35#include <linux/slab.h>
36#include <linux/trace_clock.h>
Paul E. McKenney31257c32016-06-18 07:45:43 -070037#include <linux/ktime.h>
Paul E. McKenney51b11302014-01-27 11:49:39 -080038#include <asm/byteorder.h>
39#include <linux/torture.h>
Paul E. McKenneydac95902017-10-04 11:23:10 -070040#include "rcu/rcu.h"
Paul E. McKenney51b11302014-01-27 11:49:39 -080041
42MODULE_LICENSE("GPL");
Paul E. McKenney8f8e76c2019-01-17 10:41:31 -080043MODULE_AUTHOR("Paul E. McKenney <paulmck@linux.ibm.com>");
Paul E. McKenney51b11302014-01-27 11:49:39 -080044
Paul E. McKenney8171d3e2019-12-06 15:02:59 -080045static bool disable_onoff_at_boot;
46module_param(disable_onoff_at_boot, bool, 0444);
47
Paul E. McKenneyb5daa8f2014-01-30 13:38:09 -080048static char *torture_type;
Paul E. McKenney90127d62018-05-09 10:29:18 -070049static int verbose;
Paul E. McKenneyb5daa8f2014-01-30 13:38:09 -080050
Paul E. McKenney36970bb2014-01-30 15:49:29 -080051/* Mediate rmmod and system shutdown. Concurrent rmmod & shutdown illegal! */
52#define FULLSTOP_DONTSTOP 0 /* Normal operation. */
53#define FULLSTOP_SHUTDOWN 1 /* System shutdown with torture running. */
54#define FULLSTOP_RMMOD 2 /* Normal rmmod of torture. */
55static int fullstop = FULLSTOP_RMMOD;
Paul E. McKenney4622b482014-01-30 15:37:19 -080056static DEFINE_MUTEX(fullstop_mutex);
Paul E. McKenneyf67a3352014-01-29 07:40:27 -080057
Paul E. McKenney2e9e8082014-01-28 15:58:22 -080058#ifdef CONFIG_HOTPLUG_CPU
59
60/*
61 * Variables for online-offline handling. Only present if CPU hotplug
62 * is enabled, otherwise does nothing.
63 */
64
65static struct task_struct *onoff_task;
66static long onoff_holdoff;
67static long onoff_interval;
Paul E. McKenney3a6cb582018-12-10 09:44:52 -080068static torture_ofl_func *onoff_f;
Paul E. McKenney2e9e8082014-01-28 15:58:22 -080069static long n_offline_attempts;
70static long n_offline_successes;
71static unsigned long sum_offline;
72static int min_offline = -1;
73static int max_offline;
74static long n_online_attempts;
75static long n_online_successes;
76static unsigned long sum_online;
77static int min_online = -1;
78static int max_online;
79
80/*
Paul E. McKenneyd95f5ba2016-04-20 17:18:41 -070081 * Attempt to take a CPU offline. Return false if the CPU is already
82 * offline or if it is not subject to CPU-hotplug operations. The
83 * caller can detect other failures by looking at the statistics.
84 */
85bool torture_offline(int cpu, long *n_offl_attempts, long *n_offl_successes,
86 unsigned long *sum_offl, int *min_offl, int *max_offl)
87{
88 unsigned long delta;
89 int ret;
Paul E. McKenneya59ee762019-12-05 10:49:11 -080090 char *s;
Paul E. McKenneyd95f5ba2016-04-20 17:18:41 -070091 unsigned long starttime;
92
93 if (!cpu_online(cpu) || !cpu_is_hotpluggable(cpu))
94 return false;
Paul E. McKenney24aca4a2019-01-22 19:23:00 -080095 if (num_online_cpus() <= 1)
96 return false; /* Can't offline the last CPU. */
Paul E. McKenneyd95f5ba2016-04-20 17:18:41 -070097
Paul E. McKenney90127d62018-05-09 10:29:18 -070098 if (verbose > 1)
Paul E. McKenneyd95f5ba2016-04-20 17:18:41 -070099 pr_alert("%s" TORTURE_FLAG
100 "torture_onoff task: offlining %d\n",
101 torture_type, cpu);
102 starttime = jiffies;
103 (*n_offl_attempts)++;
Qais Yousef457bc8e2020-03-23 13:51:08 +0000104 ret = remove_cpu(cpu);
Paul E. McKenneyd95f5ba2016-04-20 17:18:41 -0700105 if (ret) {
Paul E. McKenneya59ee762019-12-05 10:49:11 -0800106 s = "";
107 if (!rcu_inkernel_boot_has_ended() && ret == -EBUSY) {
108 // PCI probe frequently disables hotplug during boot.
109 (*n_offl_attempts)--;
110 s = " (-EBUSY forgiven during boot)";
111 }
Paul E. McKenneyd95f5ba2016-04-20 17:18:41 -0700112 if (verbose)
113 pr_alert("%s" TORTURE_FLAG
Paul E. McKenneya59ee762019-12-05 10:49:11 -0800114 "torture_onoff task: offline %d failed%s: errno %d\n",
115 torture_type, cpu, s, ret);
Paul E. McKenneyd95f5ba2016-04-20 17:18:41 -0700116 } else {
Paul E. McKenney90127d62018-05-09 10:29:18 -0700117 if (verbose > 1)
Paul E. McKenneyd95f5ba2016-04-20 17:18:41 -0700118 pr_alert("%s" TORTURE_FLAG
119 "torture_onoff task: offlined %d\n",
120 torture_type, cpu);
Paul E. McKenney3a6cb582018-12-10 09:44:52 -0800121 if (onoff_f)
122 onoff_f();
Paul E. McKenneyd95f5ba2016-04-20 17:18:41 -0700123 (*n_offl_successes)++;
124 delta = jiffies - starttime;
Paul E. McKenneya2b2df22017-06-22 15:38:26 -0700125 *sum_offl += delta;
Paul E. McKenneyd95f5ba2016-04-20 17:18:41 -0700126 if (*min_offl < 0) {
127 *min_offl = delta;
128 *max_offl = delta;
129 }
130 if (*min_offl > delta)
131 *min_offl = delta;
132 if (*max_offl < delta)
133 *max_offl = delta;
134 }
135
136 return true;
137}
138EXPORT_SYMBOL_GPL(torture_offline);
139
140/*
141 * Attempt to bring a CPU online. Return false if the CPU is already
142 * online or if it is not subject to CPU-hotplug operations. The
143 * caller can detect other failures by looking at the statistics.
144 */
145bool torture_online(int cpu, long *n_onl_attempts, long *n_onl_successes,
146 unsigned long *sum_onl, int *min_onl, int *max_onl)
147{
148 unsigned long delta;
149 int ret;
Paul E. McKenneya59ee762019-12-05 10:49:11 -0800150 char *s;
Paul E. McKenneyd95f5ba2016-04-20 17:18:41 -0700151 unsigned long starttime;
152
153 if (cpu_online(cpu) || !cpu_is_hotpluggable(cpu))
154 return false;
155
Paul E. McKenney90127d62018-05-09 10:29:18 -0700156 if (verbose > 1)
Paul E. McKenneyd95f5ba2016-04-20 17:18:41 -0700157 pr_alert("%s" TORTURE_FLAG
158 "torture_onoff task: onlining %d\n",
159 torture_type, cpu);
160 starttime = jiffies;
161 (*n_onl_attempts)++;
Qais Yousef457bc8e2020-03-23 13:51:08 +0000162 ret = add_cpu(cpu);
Paul E. McKenneyd95f5ba2016-04-20 17:18:41 -0700163 if (ret) {
Paul E. McKenneya59ee762019-12-05 10:49:11 -0800164 s = "";
165 if (!rcu_inkernel_boot_has_ended() && ret == -EBUSY) {
166 // PCI probe frequently disables hotplug during boot.
167 (*n_onl_attempts)--;
168 s = " (-EBUSY forgiven during boot)";
169 }
Paul E. McKenneyd95f5ba2016-04-20 17:18:41 -0700170 if (verbose)
171 pr_alert("%s" TORTURE_FLAG
Paul E. McKenneya59ee762019-12-05 10:49:11 -0800172 "torture_onoff task: online %d failed%s: errno %d\n",
173 torture_type, cpu, s, ret);
Paul E. McKenneyd95f5ba2016-04-20 17:18:41 -0700174 } else {
Paul E. McKenney90127d62018-05-09 10:29:18 -0700175 if (verbose > 1)
Paul E. McKenneyd95f5ba2016-04-20 17:18:41 -0700176 pr_alert("%s" TORTURE_FLAG
177 "torture_onoff task: onlined %d\n",
178 torture_type, cpu);
179 (*n_onl_successes)++;
180 delta = jiffies - starttime;
181 *sum_onl += delta;
182 if (*min_onl < 0) {
183 *min_onl = delta;
184 *max_onl = delta;
185 }
186 if (*min_onl > delta)
187 *min_onl = delta;
188 if (*max_onl < delta)
189 *max_onl = delta;
190 }
191
192 return true;
193}
194EXPORT_SYMBOL_GPL(torture_online);
195
196/*
Paul E. McKenney2e9e8082014-01-28 15:58:22 -0800197 * Execute random CPU-hotplug operations at the interval specified
198 * by the onoff_interval.
199 */
200static int
201torture_onoff(void *arg)
202{
203 int cpu;
Paul E. McKenney2e9e8082014-01-28 15:58:22 -0800204 int maxcpu = -1;
205 DEFINE_TORTURE_RANDOM(rand);
Paul E. McKenney28cf5952018-08-21 15:27:16 -0700206 int ret;
Paul E. McKenney2e9e8082014-01-28 15:58:22 -0800207
208 VERBOSE_TOROUT_STRING("torture_onoff task started");
209 for_each_online_cpu(cpu)
210 maxcpu = cpu;
211 WARN_ON(maxcpu < 0);
Qais Yousef457bc8e2020-03-23 13:51:08 +0000212 if (!IS_MODULE(CONFIG_TORTURE_TEST)) {
Paul E. McKenney28cf5952018-08-21 15:27:16 -0700213 for_each_possible_cpu(cpu) {
214 if (cpu_online(cpu))
215 continue;
Qais Yousef457bc8e2020-03-23 13:51:08 +0000216 ret = add_cpu(cpu);
Paul E. McKenney28cf5952018-08-21 15:27:16 -0700217 if (ret && verbose) {
218 pr_alert("%s" TORTURE_FLAG
219 "%s: Initial online %d: errno %d\n",
220 __func__, torture_type, cpu, ret);
221 }
222 }
Qais Yousef457bc8e2020-03-23 13:51:08 +0000223 }
Boqun Feng750db0f2016-05-02 10:30:00 +0800224
225 if (maxcpu == 0) {
226 VERBOSE_TOROUT_STRING("Only one CPU, so CPU-hotplug testing is disabled");
227 goto stop;
228 }
229
Paul E. McKenney2e9e8082014-01-28 15:58:22 -0800230 if (onoff_holdoff > 0) {
231 VERBOSE_TOROUT_STRING("torture_onoff begin holdoff");
232 schedule_timeout_interruptible(onoff_holdoff);
233 VERBOSE_TOROUT_STRING("torture_onoff end holdoff");
234 }
235 while (!torture_must_stop()) {
Paul E. McKenney8171d3e2019-12-06 15:02:59 -0800236 if (disable_onoff_at_boot && !rcu_inkernel_boot_has_ended()) {
237 schedule_timeout_interruptible(HZ / 10);
238 continue;
239 }
Paul E. McKenney2e9e8082014-01-28 15:58:22 -0800240 cpu = (torture_random(&rand) >> 4) % (maxcpu + 1);
Paul E. McKenneyd95f5ba2016-04-20 17:18:41 -0700241 if (!torture_offline(cpu,
242 &n_offline_attempts, &n_offline_successes,
243 &sum_offline, &min_offline, &max_offline))
244 torture_online(cpu,
245 &n_online_attempts, &n_online_successes,
246 &sum_online, &min_online, &max_online);
Paul E. McKenney2e9e8082014-01-28 15:58:22 -0800247 schedule_timeout_interruptible(onoff_interval);
248 }
Boqun Feng750db0f2016-05-02 10:30:00 +0800249
250stop:
Paul E. McKenney7fafaac2014-01-31 17:37:28 -0800251 torture_kthread_stopping("torture_onoff");
Paul E. McKenney2e9e8082014-01-28 15:58:22 -0800252 return 0;
253}
254
255#endif /* #ifdef CONFIG_HOTPLUG_CPU */
256
257/*
258 * Initiate online-offline handling.
259 */
Paul E. McKenney3a6cb582018-12-10 09:44:52 -0800260int torture_onoff_init(long ooholdoff, long oointerval, torture_ofl_func *f)
Paul E. McKenney2e9e8082014-01-28 15:58:22 -0800261{
Paul E. McKenney47cf29b2014-02-03 11:52:27 -0800262#ifdef CONFIG_HOTPLUG_CPU
Paul E. McKenney2e9e8082014-01-28 15:58:22 -0800263 onoff_holdoff = ooholdoff;
264 onoff_interval = oointerval;
Paul E. McKenney3a6cb582018-12-10 09:44:52 -0800265 onoff_f = f;
Paul E. McKenney2e9e8082014-01-28 15:58:22 -0800266 if (onoff_interval <= 0)
267 return 0;
Pierce Griffiths2a7d9682018-09-21 20:21:31 -0500268 return torture_create_kthread(torture_onoff, NULL, onoff_task);
269#else /* #ifdef CONFIG_HOTPLUG_CPU */
270 return 0;
271#endif /* #else #ifdef CONFIG_HOTPLUG_CPU */
Paul E. McKenney2e9e8082014-01-28 15:58:22 -0800272}
273EXPORT_SYMBOL_GPL(torture_onoff_init);
274
275/*
276 * Clean up after online/offline testing.
277 */
Paul E. McKenneycc47ae02014-01-30 14:21:11 -0800278static void torture_onoff_cleanup(void)
Paul E. McKenney2e9e8082014-01-28 15:58:22 -0800279{
280#ifdef CONFIG_HOTPLUG_CPU
281 if (onoff_task == NULL)
282 return;
283 VERBOSE_TOROUT_STRING("Stopping torture_onoff task");
284 kthread_stop(onoff_task);
285 onoff_task = NULL;
286#endif /* #ifdef CONFIG_HOTPLUG_CPU */
287}
Paul E. McKenney2e9e8082014-01-28 15:58:22 -0800288
289/*
290 * Print online/offline testing statistics.
291 */
Joe Percheseea203f2014-07-14 09:16:15 -0400292void torture_onoff_stats(void)
Paul E. McKenney2e9e8082014-01-28 15:58:22 -0800293{
294#ifdef CONFIG_HOTPLUG_CPU
Joe Percheseea203f2014-07-14 09:16:15 -0400295 pr_cont("onoff: %ld/%ld:%ld/%ld %d,%d:%d,%d %lu:%lu (HZ=%d) ",
296 n_online_successes, n_online_attempts,
297 n_offline_successes, n_offline_attempts,
298 min_online, max_online,
299 min_offline, max_offline,
300 sum_online, sum_offline, HZ);
Paul E. McKenney2e9e8082014-01-28 15:58:22 -0800301#endif /* #ifdef CONFIG_HOTPLUG_CPU */
Paul E. McKenney2e9e8082014-01-28 15:58:22 -0800302}
303EXPORT_SYMBOL_GPL(torture_onoff_stats);
304
305/*
306 * Were all the online/offline operations successful?
307 */
308bool torture_onoff_failures(void)
309{
310#ifdef CONFIG_HOTPLUG_CPU
311 return n_online_successes != n_online_attempts ||
312 n_offline_successes != n_offline_attempts;
313#else /* #ifdef CONFIG_HOTPLUG_CPU */
314 return false;
315#endif /* #else #ifdef CONFIG_HOTPLUG_CPU */
316}
317EXPORT_SYMBOL_GPL(torture_onoff_failures);
318
Paul E. McKenney51b11302014-01-27 11:49:39 -0800319#define TORTURE_RANDOM_MULT 39916801 /* prime */
320#define TORTURE_RANDOM_ADD 479001701 /* prime */
321#define TORTURE_RANDOM_REFRESH 10000
322
323/*
324 * Crude but fast random-number generator. Uses a linear congruential
325 * generator, with occasional help from cpu_clock().
326 */
327unsigned long
328torture_random(struct torture_random_state *trsp)
329{
330 if (--trsp->trs_count < 0) {
331 trsp->trs_state += (unsigned long)local_clock();
332 trsp->trs_count = TORTURE_RANDOM_REFRESH;
333 }
334 trsp->trs_state = trsp->trs_state * TORTURE_RANDOM_MULT +
335 TORTURE_RANDOM_ADD;
336 return swahw32(trsp->trs_state);
337}
338EXPORT_SYMBOL_GPL(torture_random);
Paul E. McKenneyf67a3352014-01-29 07:40:27 -0800339
340/*
Paul E. McKenney3808dc92014-01-28 15:29:21 -0800341 * Variables for shuffling. The idea is to ensure that each CPU stays
342 * idle for an extended period to test interactions with dyntick idle,
Masahiro Yamadab564d622017-02-27 14:29:06 -0800343 * as well as interactions with any per-CPU variables.
Paul E. McKenney3808dc92014-01-28 15:29:21 -0800344 */
345struct shuffle_task {
346 struct list_head st_l;
347 struct task_struct *st_t;
348};
349
350static long shuffle_interval; /* In jiffies. */
351static struct task_struct *shuffler_task;
352static cpumask_var_t shuffle_tmp_mask;
353static int shuffle_idle_cpu; /* Force all torture tasks off this CPU */
354static struct list_head shuffle_task_list = LIST_HEAD_INIT(shuffle_task_list);
355static DEFINE_MUTEX(shuffle_task_mutex);
356
357/*
358 * Register a task to be shuffled. If there is no memory, just splat
359 * and don't bother registering.
360 */
361void torture_shuffle_task_register(struct task_struct *tp)
362{
363 struct shuffle_task *stp;
364
365 if (WARN_ON_ONCE(tp == NULL))
366 return;
367 stp = kmalloc(sizeof(*stp), GFP_KERNEL);
368 if (WARN_ON_ONCE(stp == NULL))
369 return;
370 stp->st_t = tp;
371 mutex_lock(&shuffle_task_mutex);
372 list_add(&stp->st_l, &shuffle_task_list);
373 mutex_unlock(&shuffle_task_mutex);
374}
375EXPORT_SYMBOL_GPL(torture_shuffle_task_register);
376
377/*
378 * Unregister all tasks, for example, at the end of the torture run.
379 */
380static void torture_shuffle_task_unregister_all(void)
381{
382 struct shuffle_task *stp;
383 struct shuffle_task *p;
384
385 mutex_lock(&shuffle_task_mutex);
386 list_for_each_entry_safe(stp, p, &shuffle_task_list, st_l) {
387 list_del(&stp->st_l);
388 kfree(stp);
389 }
390 mutex_unlock(&shuffle_task_mutex);
391}
392
393/* Shuffle tasks such that we allow shuffle_idle_cpu to become idle.
394 * A special case is when shuffle_idle_cpu = -1, in which case we allow
395 * the tasks to run on all CPUs.
396 */
397static void torture_shuffle_tasks(void)
398{
399 struct shuffle_task *stp;
400
401 cpumask_setall(shuffle_tmp_mask);
402 get_online_cpus();
403
404 /* No point in shuffling if there is only one online CPU (ex: UP) */
405 if (num_online_cpus() == 1) {
406 put_online_cpus();
407 return;
408 }
409
410 /* Advance to the next CPU. Upon overflow, don't idle any CPUs. */
411 shuffle_idle_cpu = cpumask_next(shuffle_idle_cpu, shuffle_tmp_mask);
412 if (shuffle_idle_cpu >= nr_cpu_ids)
413 shuffle_idle_cpu = -1;
Iulia Manda5ed63b12014-03-17 15:21:21 +0200414 else
Paul E. McKenney3808dc92014-01-28 15:29:21 -0800415 cpumask_clear_cpu(shuffle_idle_cpu, shuffle_tmp_mask);
Paul E. McKenney3808dc92014-01-28 15:29:21 -0800416
417 mutex_lock(&shuffle_task_mutex);
418 list_for_each_entry(stp, &shuffle_task_list, st_l)
419 set_cpus_allowed_ptr(stp->st_t, shuffle_tmp_mask);
420 mutex_unlock(&shuffle_task_mutex);
421
422 put_online_cpus();
423}
424
425/* Shuffle tasks across CPUs, with the intent of allowing each CPU in the
426 * system to become idle at a time and cut off its timer ticks. This is meant
427 * to test the support for such tickless idle CPU in RCU.
428 */
429static int torture_shuffle(void *arg)
430{
431 VERBOSE_TOROUT_STRING("torture_shuffle task started");
432 do {
433 schedule_timeout_interruptible(shuffle_interval);
434 torture_shuffle_tasks();
435 torture_shutdown_absorb("torture_shuffle");
436 } while (!torture_must_stop());
Paul E. McKenney7fafaac2014-01-31 17:37:28 -0800437 torture_kthread_stopping("torture_shuffle");
Paul E. McKenney3808dc92014-01-28 15:29:21 -0800438 return 0;
439}
440
441/*
442 * Start the shuffler, with shuffint in jiffies.
443 */
444int torture_shuffle_init(long shuffint)
445{
Paul E. McKenney3808dc92014-01-28 15:29:21 -0800446 shuffle_interval = shuffint;
447
448 shuffle_idle_cpu = -1;
449
450 if (!alloc_cpumask_var(&shuffle_tmp_mask, GFP_KERNEL)) {
451 VERBOSE_TOROUT_ERRSTRING("Failed to alloc mask");
452 return -ENOMEM;
453 }
454
455 /* Create the shuffler thread */
Paul E. McKenney47cf29b2014-02-03 11:52:27 -0800456 return torture_create_kthread(torture_shuffle, NULL, shuffler_task);
Paul E. McKenney3808dc92014-01-28 15:29:21 -0800457}
458EXPORT_SYMBOL_GPL(torture_shuffle_init);
459
460/*
461 * Stop the shuffling.
462 */
Paul E. McKenneycc47ae02014-01-30 14:21:11 -0800463static void torture_shuffle_cleanup(void)
Paul E. McKenney3808dc92014-01-28 15:29:21 -0800464{
465 torture_shuffle_task_unregister_all();
466 if (shuffler_task) {
467 VERBOSE_TOROUT_STRING("Stopping torture_shuffle task");
468 kthread_stop(shuffler_task);
469 free_cpumask_var(shuffle_tmp_mask);
470 }
471 shuffler_task = NULL;
472}
Paul E. McKenney3808dc92014-01-28 15:29:21 -0800473
474/*
Paul E. McKenneye991dbc2014-01-31 14:52:13 -0800475 * Variables for auto-shutdown. This allows "lights out" torture runs
476 * to be fully scripted.
477 */
Paul E. McKenneye991dbc2014-01-31 14:52:13 -0800478static struct task_struct *shutdown_task;
Paul E. McKenney31257c32016-06-18 07:45:43 -0700479static ktime_t shutdown_time; /* time to system shutdown. */
Paul E. McKenneye991dbc2014-01-31 14:52:13 -0800480static void (*torture_shutdown_hook)(void);
481
482/*
Paul E. McKenneyf67a3352014-01-29 07:40:27 -0800483 * Absorb kthreads into a kernel function that won't return, so that
484 * they won't ever access module text or data again.
485 */
486void torture_shutdown_absorb(const char *title)
487{
Paul E. McKenney7d0ae802015-03-03 14:57:58 -0800488 while (READ_ONCE(fullstop) == FULLSTOP_SHUTDOWN) {
Paul E. McKenney4622b482014-01-30 15:37:19 -0800489 pr_notice("torture thread %s parking due to system shutdown\n",
490 title);
Paul E. McKenneyf67a3352014-01-29 07:40:27 -0800491 schedule_timeout_uninterruptible(MAX_SCHEDULE_TIMEOUT);
492 }
493}
494EXPORT_SYMBOL_GPL(torture_shutdown_absorb);
Paul E. McKenneyb5daa8f2014-01-30 13:38:09 -0800495
496/*
Paul E. McKenneye991dbc2014-01-31 14:52:13 -0800497 * Cause the torture test to shutdown the system after the test has
498 * run for the time specified by the shutdown_secs parameter.
499 */
500static int torture_shutdown(void *arg)
501{
Paul E. McKenney31257c32016-06-18 07:45:43 -0700502 ktime_t ktime_snap;
Paul E. McKenneye991dbc2014-01-31 14:52:13 -0800503
504 VERBOSE_TOROUT_STRING("torture_shutdown task started");
Paul E. McKenney31257c32016-06-18 07:45:43 -0700505 ktime_snap = ktime_get();
506 while (ktime_before(ktime_snap, shutdown_time) &&
Paul E. McKenneye991dbc2014-01-31 14:52:13 -0800507 !torture_must_stop()) {
Paul E. McKenneye991dbc2014-01-31 14:52:13 -0800508 if (verbose)
509 pr_alert("%s" TORTURE_FLAG
Paul E. McKenney31257c32016-06-18 07:45:43 -0700510 "torture_shutdown task: %llu ms remaining\n",
511 torture_type,
512 ktime_ms_delta(shutdown_time, ktime_snap));
513 set_current_state(TASK_INTERRUPTIBLE);
514 schedule_hrtimeout(&shutdown_time, HRTIMER_MODE_ABS);
515 ktime_snap = ktime_get();
Paul E. McKenneye991dbc2014-01-31 14:52:13 -0800516 }
517 if (torture_must_stop()) {
Paul E. McKenney7fafaac2014-01-31 17:37:28 -0800518 torture_kthread_stopping("torture_shutdown");
Paul E. McKenneye991dbc2014-01-31 14:52:13 -0800519 return 0;
520 }
521
522 /* OK, shut down the system. */
523
524 VERBOSE_TOROUT_STRING("torture_shutdown task shutting down system");
525 shutdown_task = NULL; /* Avoid self-kill deadlock. */
Paul E. McKenneyf8818252014-02-07 14:42:51 -0800526 if (torture_shutdown_hook)
527 torture_shutdown_hook();
528 else
529 VERBOSE_TOROUT_STRING("No torture_shutdown_hook(), skipping.");
Paul E. McKenneydac95902017-10-04 11:23:10 -0700530 rcu_ftrace_dump(DUMP_ALL);
Paul E. McKenneye991dbc2014-01-31 14:52:13 -0800531 kernel_power_off(); /* Shut down the system. */
532 return 0;
533}
534
535/*
536 * Start up the shutdown task.
537 */
538int torture_shutdown_init(int ssecs, void (*cleanup)(void))
539{
Paul E. McKenneye991dbc2014-01-31 14:52:13 -0800540 torture_shutdown_hook = cleanup;
Paul E. McKenney31257c32016-06-18 07:45:43 -0700541 if (ssecs > 0) {
542 shutdown_time = ktime_add(ktime_get(), ktime_set(ssecs, 0));
Pierce Griffiths2a7d9682018-09-21 20:21:31 -0500543 return torture_create_kthread(torture_shutdown, NULL,
Paul E. McKenney47cf29b2014-02-03 11:52:27 -0800544 shutdown_task);
Paul E. McKenneye991dbc2014-01-31 14:52:13 -0800545 }
Pierce Griffiths2a7d9682018-09-21 20:21:31 -0500546 return 0;
Paul E. McKenneye991dbc2014-01-31 14:52:13 -0800547}
548EXPORT_SYMBOL_GPL(torture_shutdown_init);
549
550/*
Paul E. McKenney4622b482014-01-30 15:37:19 -0800551 * Detect and respond to a system shutdown.
552 */
553static int torture_shutdown_notify(struct notifier_block *unused1,
554 unsigned long unused2, void *unused3)
555{
556 mutex_lock(&fullstop_mutex);
Paul E. McKenney7d0ae802015-03-03 14:57:58 -0800557 if (READ_ONCE(fullstop) == FULLSTOP_DONTSTOP) {
Paul E. McKenneyfac480e2014-01-30 17:06:30 -0800558 VERBOSE_TOROUT_STRING("Unscheduled system shutdown detected");
Paul E. McKenney7d0ae802015-03-03 14:57:58 -0800559 WRITE_ONCE(fullstop, FULLSTOP_SHUTDOWN);
Paul E. McKenneyfac480e2014-01-30 17:06:30 -0800560 } else {
Paul E. McKenney4622b482014-01-30 15:37:19 -0800561 pr_warn("Concurrent rmmod and shutdown illegal!\n");
Paul E. McKenneyfac480e2014-01-30 17:06:30 -0800562 }
Paul E. McKenney4622b482014-01-30 15:37:19 -0800563 mutex_unlock(&fullstop_mutex);
564 return NOTIFY_DONE;
565}
566
567static struct notifier_block torture_shutdown_nb = {
568 .notifier_call = torture_shutdown_notify,
569};
570
571/*
Paul E. McKenneybfefc732014-02-04 12:35:27 -0800572 * Shut down the shutdown task. Say what??? Heh! This can happen if
573 * the torture module gets an rmmod before the shutdown time arrives. ;-)
574 */
575static void torture_shutdown_cleanup(void)
576{
577 unregister_reboot_notifier(&torture_shutdown_nb);
578 if (shutdown_task != NULL) {
579 VERBOSE_TOROUT_STRING("Stopping torture_shutdown task");
580 kthread_stop(shutdown_task);
581 }
582 shutdown_task = NULL;
583}
584
585/*
Paul E. McKenney628edaa2014-01-31 11:57:43 -0800586 * Variables for stuttering, which means to periodically pause and
587 * restart testing in order to catch bugs that appear when load is
588 * suddenly applied to or removed from the system.
589 */
590static struct task_struct *stutter_task;
591static int stutter_pause_test;
592static int stutter;
Paul E. McKenneyff3bf922019-04-09 14:44:49 -0700593static int stutter_gap;
Paul E. McKenney628edaa2014-01-31 11:57:43 -0800594
595/*
596 * Block until the stutter interval ends. This must be called periodically
597 * by all running kthreads that need to be subject to stuttering.
598 */
Paul E. McKenney474e59b2018-08-07 14:34:44 -0700599bool stutter_wait(const char *title)
Paul E. McKenney628edaa2014-01-31 11:57:43 -0800600{
Paul E. McKenney4ced3312017-11-21 15:01:02 -0800601 int spt;
Paul E. McKenneye8516c62019-04-09 11:06:32 -0700602 bool ret = false;
Paul E. McKenney4ced3312017-11-21 15:01:02 -0800603
Paul E. McKenneycee43932018-03-02 16:35:27 -0800604 cond_resched_tasks_rcu_qs();
Paul E. McKenney4ced3312017-11-21 15:01:02 -0800605 spt = READ_ONCE(stutter_pause_test);
Paul E. McKenney29d39392017-11-21 22:07:59 -0800606 for (; spt; spt = READ_ONCE(stutter_pause_test)) {
Paul E. McKenneye8516c62019-04-09 11:06:32 -0700607 ret = true;
Paul E. McKenney4ced3312017-11-21 15:01:02 -0800608 if (spt == 1) {
609 schedule_timeout_interruptible(1);
610 } else if (spt == 2) {
611 while (READ_ONCE(stutter_pause_test))
612 cond_resched();
613 } else {
Paul E. McKenney628edaa2014-01-31 11:57:43 -0800614 schedule_timeout_interruptible(round_jiffies_relative(HZ));
Paul E. McKenney4ced3312017-11-21 15:01:02 -0800615 }
Paul E. McKenney628edaa2014-01-31 11:57:43 -0800616 torture_shutdown_absorb(title);
617 }
Paul E. McKenneye8516c62019-04-09 11:06:32 -0700618 return ret;
Paul E. McKenney628edaa2014-01-31 11:57:43 -0800619}
620EXPORT_SYMBOL_GPL(stutter_wait);
621
622/*
623 * Cause the torture test to "stutter", starting and stopping all
624 * threads periodically.
625 */
626static int torture_stutter(void *arg)
627{
Paul E. McKenneye8516c62019-04-09 11:06:32 -0700628 int wtime;
629
Paul E. McKenney628edaa2014-01-31 11:57:43 -0800630 VERBOSE_TOROUT_STRING("torture_stutter task started");
631 do {
Paul E. McKenney4ced3312017-11-21 15:01:02 -0800632 if (!torture_must_stop() && stutter > 1) {
Paul E. McKenneye8516c62019-04-09 11:06:32 -0700633 wtime = stutter;
634 if (stutter > HZ + 1) {
635 WRITE_ONCE(stutter_pause_test, 1);
636 wtime = stutter - HZ - 1;
637 schedule_timeout_interruptible(wtime);
638 wtime = HZ + 1;
639 }
Paul E. McKenney4ced3312017-11-21 15:01:02 -0800640 WRITE_ONCE(stutter_pause_test, 2);
Paul E. McKenneye8516c62019-04-09 11:06:32 -0700641 schedule_timeout_interruptible(wtime);
Paul E. McKenney628edaa2014-01-31 11:57:43 -0800642 }
Paul E. McKenney4ced3312017-11-21 15:01:02 -0800643 WRITE_ONCE(stutter_pause_test, 0);
Paul E. McKenney628edaa2014-01-31 11:57:43 -0800644 if (!torture_must_stop())
Paul E. McKenneyff3bf922019-04-09 14:44:49 -0700645 schedule_timeout_interruptible(stutter_gap);
Paul E. McKenney628edaa2014-01-31 11:57:43 -0800646 torture_shutdown_absorb("torture_stutter");
647 } while (!torture_must_stop());
Paul E. McKenney7fafaac2014-01-31 17:37:28 -0800648 torture_kthread_stopping("torture_stutter");
Paul E. McKenney628edaa2014-01-31 11:57:43 -0800649 return 0;
650}
651
652/*
653 * Initialize and kick off the torture_stutter kthread.
654 */
Paul E. McKenneyff3bf922019-04-09 14:44:49 -0700655int torture_stutter_init(const int s, const int sgap)
Paul E. McKenney628edaa2014-01-31 11:57:43 -0800656{
Paul E. McKenney628edaa2014-01-31 11:57:43 -0800657 stutter = s;
Paul E. McKenneyff3bf922019-04-09 14:44:49 -0700658 stutter_gap = sgap;
Pierce Griffiths2a7d9682018-09-21 20:21:31 -0500659 return torture_create_kthread(torture_stutter, NULL, stutter_task);
Paul E. McKenney628edaa2014-01-31 11:57:43 -0800660}
661EXPORT_SYMBOL_GPL(torture_stutter_init);
662
663/*
664 * Cleanup after the torture_stutter kthread.
665 */
Paul E. McKenneybfefc732014-02-04 12:35:27 -0800666static void torture_stutter_cleanup(void)
Paul E. McKenney628edaa2014-01-31 11:57:43 -0800667{
668 if (!stutter_task)
669 return;
670 VERBOSE_TOROUT_STRING("Stopping torture_stutter task");
671 kthread_stop(stutter_task);
672 stutter_task = NULL;
673}
Paul E. McKenney628edaa2014-01-31 11:57:43 -0800674
675/*
Paul E. McKenneyb5daa8f2014-01-30 13:38:09 -0800676 * Initialize torture module. Please note that this is -not- invoked via
677 * the usual module_init() mechanism, but rather by an explicit call from
678 * the client torture module. This call must be paired with a later
679 * torture_init_end().
Paul E. McKenney628edaa2014-01-31 11:57:43 -0800680 *
681 * The runnable parameter points to a flag that controls whether or not
682 * the test is currently runnable. If there is no such flag, pass in NULL.
Paul E. McKenneyb5daa8f2014-01-30 13:38:09 -0800683 */
Paul E. McKenney90127d62018-05-09 10:29:18 -0700684bool torture_init_begin(char *ttype, int v)
Paul E. McKenneyb5daa8f2014-01-30 13:38:09 -0800685{
686 mutex_lock(&fullstop_mutex);
Paul E. McKenney52280842014-04-07 09:14:11 -0700687 if (torture_type != NULL) {
Paul E. McKenney9eb51882016-03-21 15:36:40 -0700688 pr_alert("torture_init_begin: Refusing %s init: %s running.\n",
Paul E. McKenney52280842014-04-07 09:14:11 -0700689 ttype, torture_type);
Paul E. McKenney9eb51882016-03-21 15:36:40 -0700690 pr_alert("torture_init_begin: One torture test at a time!\n");
Paul E. McKenney52280842014-04-07 09:14:11 -0700691 mutex_unlock(&fullstop_mutex);
692 return false;
693 }
Paul E. McKenneyb5daa8f2014-01-30 13:38:09 -0800694 torture_type = ttype;
695 verbose = v;
Paul E. McKenney36970bb2014-01-30 15:49:29 -0800696 fullstop = FULLSTOP_DONTSTOP;
Paul E. McKenney52280842014-04-07 09:14:11 -0700697 return true;
Paul E. McKenneyb5daa8f2014-01-30 13:38:09 -0800698}
699EXPORT_SYMBOL_GPL(torture_init_begin);
700
701/*
702 * Tell the torture module that initialization is complete.
703 */
Pranith Kumar2b3f8ff2014-04-16 13:42:09 -0700704void torture_init_end(void)
Paul E. McKenneyb5daa8f2014-01-30 13:38:09 -0800705{
706 mutex_unlock(&fullstop_mutex);
Paul E. McKenney4622b482014-01-30 15:37:19 -0800707 register_reboot_notifier(&torture_shutdown_nb);
Paul E. McKenneyb5daa8f2014-01-30 13:38:09 -0800708}
709EXPORT_SYMBOL_GPL(torture_init_end);
Paul E. McKenneycc47ae02014-01-30 14:21:11 -0800710
711/*
712 * Clean up torture module. Please note that this is -not- invoked via
713 * the usual module_exit() mechanism, but rather by an explicit call from
714 * the client torture module. Returns true if a race with system shutdown
Paul E. McKenneybfefc732014-02-04 12:35:27 -0800715 * is detected, otherwise, all kthreads started by functions in this file
716 * will be shut down.
Paul E. McKenneycc47ae02014-01-30 14:21:11 -0800717 *
718 * This must be called before the caller starts shutting down its own
719 * kthreads.
Davidlohr Buesod36a7a02014-09-11 20:40:21 -0700720 *
721 * Both torture_cleanup_begin() and torture_cleanup_end() must be paired,
722 * in order to correctly perform the cleanup. They are separated because
723 * threads can still need to reference the torture_type type, thus nullify
724 * only after completing all other relevant calls.
Paul E. McKenneycc47ae02014-01-30 14:21:11 -0800725 */
Davidlohr Buesod36a7a02014-09-11 20:40:21 -0700726bool torture_cleanup_begin(void)
Paul E. McKenneycc47ae02014-01-30 14:21:11 -0800727{
728 mutex_lock(&fullstop_mutex);
Paul E. McKenney7d0ae802015-03-03 14:57:58 -0800729 if (READ_ONCE(fullstop) == FULLSTOP_SHUTDOWN) {
Paul E. McKenneycc47ae02014-01-30 14:21:11 -0800730 pr_warn("Concurrent rmmod and shutdown illegal!\n");
731 mutex_unlock(&fullstop_mutex);
732 schedule_timeout_uninterruptible(10);
733 return true;
734 }
Paul E. McKenney7d0ae802015-03-03 14:57:58 -0800735 WRITE_ONCE(fullstop, FULLSTOP_RMMOD);
Paul E. McKenneycc47ae02014-01-30 14:21:11 -0800736 mutex_unlock(&fullstop_mutex);
Paul E. McKenneybfefc732014-02-04 12:35:27 -0800737 torture_shutdown_cleanup();
Paul E. McKenneycc47ae02014-01-30 14:21:11 -0800738 torture_shuffle_cleanup();
Paul E. McKenneybfefc732014-02-04 12:35:27 -0800739 torture_stutter_cleanup();
Paul E. McKenneycc47ae02014-01-30 14:21:11 -0800740 torture_onoff_cleanup();
Davidlohr Buesod36a7a02014-09-11 20:40:21 -0700741 return false;
742}
743EXPORT_SYMBOL_GPL(torture_cleanup_begin);
744
745void torture_cleanup_end(void)
746{
Paul E. McKenney52280842014-04-07 09:14:11 -0700747 mutex_lock(&fullstop_mutex);
748 torture_type = NULL;
749 mutex_unlock(&fullstop_mutex);
Paul E. McKenneycc47ae02014-01-30 14:21:11 -0800750}
Davidlohr Buesod36a7a02014-09-11 20:40:21 -0700751EXPORT_SYMBOL_GPL(torture_cleanup_end);
Paul E. McKenney36970bb2014-01-30 15:49:29 -0800752
753/*
754 * Is it time for the current torture test to stop?
755 */
756bool torture_must_stop(void)
757{
758 return torture_must_stop_irq() || kthread_should_stop();
759}
760EXPORT_SYMBOL_GPL(torture_must_stop);
761
762/*
763 * Is it time for the current torture test to stop? This is the irq-safe
764 * version, hence no check for kthread_should_stop().
765 */
766bool torture_must_stop_irq(void)
767{
Paul E. McKenney7d0ae802015-03-03 14:57:58 -0800768 return READ_ONCE(fullstop) != FULLSTOP_DONTSTOP;
Paul E. McKenney36970bb2014-01-30 15:49:29 -0800769}
770EXPORT_SYMBOL_GPL(torture_must_stop_irq);
Paul E. McKenney7fafaac2014-01-31 17:37:28 -0800771
772/*
773 * Each kthread must wait for kthread_should_stop() before returning from
774 * its top-level function, otherwise segfaults ensue. This function
775 * prints a "stopping" message and waits for kthread_should_stop(), and
776 * should be called from all torture kthreads immediately prior to
777 * returning.
778 */
779void torture_kthread_stopping(char *title)
780{
Paul E. McKenney0d6821d2014-03-03 16:58:03 -0800781 char buf[128];
782
783 snprintf(buf, sizeof(buf), "Stopping %s", title);
784 VERBOSE_TOROUT_STRING(buf);
Paul E. McKenney7fafaac2014-01-31 17:37:28 -0800785 while (!kthread_should_stop()) {
786 torture_shutdown_absorb(title);
787 schedule_timeout_uninterruptible(1);
788 }
789}
790EXPORT_SYMBOL_GPL(torture_kthread_stopping);
Paul E. McKenney47cf29b2014-02-03 11:52:27 -0800791
792/*
793 * Create a generic torture kthread that is immediately runnable. If you
794 * need the kthread to be stopped so that you can do something to it before
795 * it starts, you will need to open-code your own.
796 */
797int _torture_create_kthread(int (*fn)(void *arg), void *arg, char *s, char *m,
798 char *f, struct task_struct **tp)
799{
800 int ret = 0;
801
802 VERBOSE_TOROUT_STRING(m);
Kees Cook69459152014-05-22 11:51:04 -0700803 *tp = kthread_run(fn, arg, "%s", s);
Paul E. McKenney47cf29b2014-02-03 11:52:27 -0800804 if (IS_ERR(*tp)) {
805 ret = PTR_ERR(*tp);
806 VERBOSE_TOROUT_ERRSTRING(f);
807 *tp = NULL;
808 }
809 torture_shuffle_task_register(*tp);
810 return ret;
811}
812EXPORT_SYMBOL_GPL(_torture_create_kthread);
Paul E. McKenney9c029b82014-02-04 11:47:08 -0800813
814/*
815 * Stop a generic kthread, emitting a message.
816 */
817void _torture_stop_kthread(char *m, struct task_struct **tp)
818{
819 if (*tp == NULL)
820 return;
821 VERBOSE_TOROUT_STRING(m);
822 kthread_stop(*tp);
823 *tp = NULL;
824}
825EXPORT_SYMBOL_GPL(_torture_stop_kthread);