blob: a479689eac73d15c6946acd5da5e565a111dc0ad [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. McKenneyb5daa8f2014-01-30 13:38:09 -080045static char *torture_type;
Paul E. McKenney90127d62018-05-09 10:29:18 -070046static int verbose;
Paul E. McKenneyb5daa8f2014-01-30 13:38:09 -080047
Paul E. McKenney36970bb2014-01-30 15:49:29 -080048/* Mediate rmmod and system shutdown. Concurrent rmmod & shutdown illegal! */
49#define FULLSTOP_DONTSTOP 0 /* Normal operation. */
50#define FULLSTOP_SHUTDOWN 1 /* System shutdown with torture running. */
51#define FULLSTOP_RMMOD 2 /* Normal rmmod of torture. */
52static int fullstop = FULLSTOP_RMMOD;
Paul E. McKenney4622b482014-01-30 15:37:19 -080053static DEFINE_MUTEX(fullstop_mutex);
Paul E. McKenneyf67a3352014-01-29 07:40:27 -080054
Paul E. McKenney2e9e8082014-01-28 15:58:22 -080055#ifdef CONFIG_HOTPLUG_CPU
56
57/*
58 * Variables for online-offline handling. Only present if CPU hotplug
59 * is enabled, otherwise does nothing.
60 */
61
62static struct task_struct *onoff_task;
63static long onoff_holdoff;
64static long onoff_interval;
Paul E. McKenney3a6cb582018-12-10 09:44:52 -080065static torture_ofl_func *onoff_f;
Paul E. McKenney2e9e8082014-01-28 15:58:22 -080066static long n_offline_attempts;
67static long n_offline_successes;
68static unsigned long sum_offline;
69static int min_offline = -1;
70static int max_offline;
71static long n_online_attempts;
72static long n_online_successes;
73static unsigned long sum_online;
74static int min_online = -1;
75static int max_online;
76
77/*
Paul E. McKenneyd95f5ba2016-04-20 17:18:41 -070078 * Attempt to take a CPU offline. Return false if the CPU is already
79 * offline or if it is not subject to CPU-hotplug operations. The
80 * caller can detect other failures by looking at the statistics.
81 */
82bool torture_offline(int cpu, long *n_offl_attempts, long *n_offl_successes,
83 unsigned long *sum_offl, int *min_offl, int *max_offl)
84{
85 unsigned long delta;
86 int ret;
87 unsigned long starttime;
88
89 if (!cpu_online(cpu) || !cpu_is_hotpluggable(cpu))
90 return false;
Paul E. McKenney24aca4a2019-01-22 19:23:00 -080091 if (num_online_cpus() <= 1)
92 return false; /* Can't offline the last CPU. */
Paul E. McKenneyd95f5ba2016-04-20 17:18:41 -070093
Paul E. McKenney90127d62018-05-09 10:29:18 -070094 if (verbose > 1)
Paul E. McKenneyd95f5ba2016-04-20 17:18:41 -070095 pr_alert("%s" TORTURE_FLAG
96 "torture_onoff task: offlining %d\n",
97 torture_type, cpu);
98 starttime = jiffies;
99 (*n_offl_attempts)++;
Qais Yousef457bc8e2020-03-23 13:51:08 +0000100 ret = remove_cpu(cpu);
Paul E. McKenneyd95f5ba2016-04-20 17:18:41 -0700101 if (ret) {
102 if (verbose)
103 pr_alert("%s" TORTURE_FLAG
104 "torture_onoff task: offline %d failed: errno %d\n",
105 torture_type, cpu, ret);
106 } else {
Paul E. McKenney90127d62018-05-09 10:29:18 -0700107 if (verbose > 1)
Paul E. McKenneyd95f5ba2016-04-20 17:18:41 -0700108 pr_alert("%s" TORTURE_FLAG
109 "torture_onoff task: offlined %d\n",
110 torture_type, cpu);
Paul E. McKenney3a6cb582018-12-10 09:44:52 -0800111 if (onoff_f)
112 onoff_f();
Paul E. McKenneyd95f5ba2016-04-20 17:18:41 -0700113 (*n_offl_successes)++;
114 delta = jiffies - starttime;
Paul E. McKenneya2b2df22017-06-22 15:38:26 -0700115 *sum_offl += delta;
Paul E. McKenneyd95f5ba2016-04-20 17:18:41 -0700116 if (*min_offl < 0) {
117 *min_offl = delta;
118 *max_offl = delta;
119 }
120 if (*min_offl > delta)
121 *min_offl = delta;
122 if (*max_offl < delta)
123 *max_offl = delta;
124 }
125
126 return true;
127}
128EXPORT_SYMBOL_GPL(torture_offline);
129
130/*
131 * Attempt to bring a CPU online. Return false if the CPU is already
132 * online or if it is not subject to CPU-hotplug operations. The
133 * caller can detect other failures by looking at the statistics.
134 */
135bool torture_online(int cpu, long *n_onl_attempts, long *n_onl_successes,
136 unsigned long *sum_onl, int *min_onl, int *max_onl)
137{
138 unsigned long delta;
139 int ret;
140 unsigned long starttime;
141
142 if (cpu_online(cpu) || !cpu_is_hotpluggable(cpu))
143 return false;
144
Paul E. McKenney90127d62018-05-09 10:29:18 -0700145 if (verbose > 1)
Paul E. McKenneyd95f5ba2016-04-20 17:18:41 -0700146 pr_alert("%s" TORTURE_FLAG
147 "torture_onoff task: onlining %d\n",
148 torture_type, cpu);
149 starttime = jiffies;
150 (*n_onl_attempts)++;
Qais Yousef457bc8e2020-03-23 13:51:08 +0000151 ret = add_cpu(cpu);
Paul E. McKenneyd95f5ba2016-04-20 17:18:41 -0700152 if (ret) {
153 if (verbose)
154 pr_alert("%s" TORTURE_FLAG
155 "torture_onoff task: online %d failed: errno %d\n",
156 torture_type, cpu, ret);
157 } else {
Paul E. McKenney90127d62018-05-09 10:29:18 -0700158 if (verbose > 1)
Paul E. McKenneyd95f5ba2016-04-20 17:18:41 -0700159 pr_alert("%s" TORTURE_FLAG
160 "torture_onoff task: onlined %d\n",
161 torture_type, cpu);
162 (*n_onl_successes)++;
163 delta = jiffies - starttime;
164 *sum_onl += delta;
165 if (*min_onl < 0) {
166 *min_onl = delta;
167 *max_onl = delta;
168 }
169 if (*min_onl > delta)
170 *min_onl = delta;
171 if (*max_onl < delta)
172 *max_onl = delta;
173 }
174
175 return true;
176}
177EXPORT_SYMBOL_GPL(torture_online);
178
179/*
Paul E. McKenney2e9e8082014-01-28 15:58:22 -0800180 * Execute random CPU-hotplug operations at the interval specified
181 * by the onoff_interval.
182 */
183static int
184torture_onoff(void *arg)
185{
186 int cpu;
Paul E. McKenney2e9e8082014-01-28 15:58:22 -0800187 int maxcpu = -1;
188 DEFINE_TORTURE_RANDOM(rand);
Paul E. McKenney28cf5952018-08-21 15:27:16 -0700189 int ret;
Paul E. McKenney2e9e8082014-01-28 15:58:22 -0800190
191 VERBOSE_TOROUT_STRING("torture_onoff task started");
192 for_each_online_cpu(cpu)
193 maxcpu = cpu;
194 WARN_ON(maxcpu < 0);
Qais Yousef457bc8e2020-03-23 13:51:08 +0000195 if (!IS_MODULE(CONFIG_TORTURE_TEST)) {
Paul E. McKenney28cf5952018-08-21 15:27:16 -0700196 for_each_possible_cpu(cpu) {
197 if (cpu_online(cpu))
198 continue;
Qais Yousef457bc8e2020-03-23 13:51:08 +0000199 ret = add_cpu(cpu);
Paul E. McKenney28cf5952018-08-21 15:27:16 -0700200 if (ret && verbose) {
201 pr_alert("%s" TORTURE_FLAG
202 "%s: Initial online %d: errno %d\n",
203 __func__, torture_type, cpu, ret);
204 }
205 }
Qais Yousef457bc8e2020-03-23 13:51:08 +0000206 }
Boqun Feng750db0f2016-05-02 10:30:00 +0800207
208 if (maxcpu == 0) {
209 VERBOSE_TOROUT_STRING("Only one CPU, so CPU-hotplug testing is disabled");
210 goto stop;
211 }
212
Paul E. McKenney2e9e8082014-01-28 15:58:22 -0800213 if (onoff_holdoff > 0) {
214 VERBOSE_TOROUT_STRING("torture_onoff begin holdoff");
215 schedule_timeout_interruptible(onoff_holdoff);
216 VERBOSE_TOROUT_STRING("torture_onoff end holdoff");
217 }
218 while (!torture_must_stop()) {
219 cpu = (torture_random(&rand) >> 4) % (maxcpu + 1);
Paul E. McKenneyd95f5ba2016-04-20 17:18:41 -0700220 if (!torture_offline(cpu,
221 &n_offline_attempts, &n_offline_successes,
222 &sum_offline, &min_offline, &max_offline))
223 torture_online(cpu,
224 &n_online_attempts, &n_online_successes,
225 &sum_online, &min_online, &max_online);
Paul E. McKenney2e9e8082014-01-28 15:58:22 -0800226 schedule_timeout_interruptible(onoff_interval);
227 }
Boqun Feng750db0f2016-05-02 10:30:00 +0800228
229stop:
Paul E. McKenney7fafaac2014-01-31 17:37:28 -0800230 torture_kthread_stopping("torture_onoff");
Paul E. McKenney2e9e8082014-01-28 15:58:22 -0800231 return 0;
232}
233
234#endif /* #ifdef CONFIG_HOTPLUG_CPU */
235
236/*
237 * Initiate online-offline handling.
238 */
Paul E. McKenney3a6cb582018-12-10 09:44:52 -0800239int torture_onoff_init(long ooholdoff, long oointerval, torture_ofl_func *f)
Paul E. McKenney2e9e8082014-01-28 15:58:22 -0800240{
Paul E. McKenney47cf29b2014-02-03 11:52:27 -0800241#ifdef CONFIG_HOTPLUG_CPU
Paul E. McKenney2e9e8082014-01-28 15:58:22 -0800242 onoff_holdoff = ooholdoff;
243 onoff_interval = oointerval;
Paul E. McKenney3a6cb582018-12-10 09:44:52 -0800244 onoff_f = f;
Paul E. McKenney2e9e8082014-01-28 15:58:22 -0800245 if (onoff_interval <= 0)
246 return 0;
Pierce Griffiths2a7d9682018-09-21 20:21:31 -0500247 return torture_create_kthread(torture_onoff, NULL, onoff_task);
248#else /* #ifdef CONFIG_HOTPLUG_CPU */
249 return 0;
250#endif /* #else #ifdef CONFIG_HOTPLUG_CPU */
Paul E. McKenney2e9e8082014-01-28 15:58:22 -0800251}
252EXPORT_SYMBOL_GPL(torture_onoff_init);
253
254/*
255 * Clean up after online/offline testing.
256 */
Paul E. McKenneycc47ae02014-01-30 14:21:11 -0800257static void torture_onoff_cleanup(void)
Paul E. McKenney2e9e8082014-01-28 15:58:22 -0800258{
259#ifdef CONFIG_HOTPLUG_CPU
260 if (onoff_task == NULL)
261 return;
262 VERBOSE_TOROUT_STRING("Stopping torture_onoff task");
263 kthread_stop(onoff_task);
264 onoff_task = NULL;
265#endif /* #ifdef CONFIG_HOTPLUG_CPU */
266}
Paul E. McKenney2e9e8082014-01-28 15:58:22 -0800267
268/*
269 * Print online/offline testing statistics.
270 */
Joe Percheseea203f2014-07-14 09:16:15 -0400271void torture_onoff_stats(void)
Paul E. McKenney2e9e8082014-01-28 15:58:22 -0800272{
273#ifdef CONFIG_HOTPLUG_CPU
Joe Percheseea203f2014-07-14 09:16:15 -0400274 pr_cont("onoff: %ld/%ld:%ld/%ld %d,%d:%d,%d %lu:%lu (HZ=%d) ",
275 n_online_successes, n_online_attempts,
276 n_offline_successes, n_offline_attempts,
277 min_online, max_online,
278 min_offline, max_offline,
279 sum_online, sum_offline, HZ);
Paul E. McKenney2e9e8082014-01-28 15:58:22 -0800280#endif /* #ifdef CONFIG_HOTPLUG_CPU */
Paul E. McKenney2e9e8082014-01-28 15:58:22 -0800281}
282EXPORT_SYMBOL_GPL(torture_onoff_stats);
283
284/*
285 * Were all the online/offline operations successful?
286 */
287bool torture_onoff_failures(void)
288{
289#ifdef CONFIG_HOTPLUG_CPU
290 return n_online_successes != n_online_attempts ||
291 n_offline_successes != n_offline_attempts;
292#else /* #ifdef CONFIG_HOTPLUG_CPU */
293 return false;
294#endif /* #else #ifdef CONFIG_HOTPLUG_CPU */
295}
296EXPORT_SYMBOL_GPL(torture_onoff_failures);
297
Paul E. McKenney51b11302014-01-27 11:49:39 -0800298#define TORTURE_RANDOM_MULT 39916801 /* prime */
299#define TORTURE_RANDOM_ADD 479001701 /* prime */
300#define TORTURE_RANDOM_REFRESH 10000
301
302/*
303 * Crude but fast random-number generator. Uses a linear congruential
304 * generator, with occasional help from cpu_clock().
305 */
306unsigned long
307torture_random(struct torture_random_state *trsp)
308{
309 if (--trsp->trs_count < 0) {
310 trsp->trs_state += (unsigned long)local_clock();
311 trsp->trs_count = TORTURE_RANDOM_REFRESH;
312 }
313 trsp->trs_state = trsp->trs_state * TORTURE_RANDOM_MULT +
314 TORTURE_RANDOM_ADD;
315 return swahw32(trsp->trs_state);
316}
317EXPORT_SYMBOL_GPL(torture_random);
Paul E. McKenneyf67a3352014-01-29 07:40:27 -0800318
319/*
Paul E. McKenney3808dc92014-01-28 15:29:21 -0800320 * Variables for shuffling. The idea is to ensure that each CPU stays
321 * idle for an extended period to test interactions with dyntick idle,
Masahiro Yamadab564d622017-02-27 14:29:06 -0800322 * as well as interactions with any per-CPU variables.
Paul E. McKenney3808dc92014-01-28 15:29:21 -0800323 */
324struct shuffle_task {
325 struct list_head st_l;
326 struct task_struct *st_t;
327};
328
329static long shuffle_interval; /* In jiffies. */
330static struct task_struct *shuffler_task;
331static cpumask_var_t shuffle_tmp_mask;
332static int shuffle_idle_cpu; /* Force all torture tasks off this CPU */
333static struct list_head shuffle_task_list = LIST_HEAD_INIT(shuffle_task_list);
334static DEFINE_MUTEX(shuffle_task_mutex);
335
336/*
337 * Register a task to be shuffled. If there is no memory, just splat
338 * and don't bother registering.
339 */
340void torture_shuffle_task_register(struct task_struct *tp)
341{
342 struct shuffle_task *stp;
343
344 if (WARN_ON_ONCE(tp == NULL))
345 return;
346 stp = kmalloc(sizeof(*stp), GFP_KERNEL);
347 if (WARN_ON_ONCE(stp == NULL))
348 return;
349 stp->st_t = tp;
350 mutex_lock(&shuffle_task_mutex);
351 list_add(&stp->st_l, &shuffle_task_list);
352 mutex_unlock(&shuffle_task_mutex);
353}
354EXPORT_SYMBOL_GPL(torture_shuffle_task_register);
355
356/*
357 * Unregister all tasks, for example, at the end of the torture run.
358 */
359static void torture_shuffle_task_unregister_all(void)
360{
361 struct shuffle_task *stp;
362 struct shuffle_task *p;
363
364 mutex_lock(&shuffle_task_mutex);
365 list_for_each_entry_safe(stp, p, &shuffle_task_list, st_l) {
366 list_del(&stp->st_l);
367 kfree(stp);
368 }
369 mutex_unlock(&shuffle_task_mutex);
370}
371
372/* Shuffle tasks such that we allow shuffle_idle_cpu to become idle.
373 * A special case is when shuffle_idle_cpu = -1, in which case we allow
374 * the tasks to run on all CPUs.
375 */
376static void torture_shuffle_tasks(void)
377{
378 struct shuffle_task *stp;
379
380 cpumask_setall(shuffle_tmp_mask);
381 get_online_cpus();
382
383 /* No point in shuffling if there is only one online CPU (ex: UP) */
384 if (num_online_cpus() == 1) {
385 put_online_cpus();
386 return;
387 }
388
389 /* Advance to the next CPU. Upon overflow, don't idle any CPUs. */
390 shuffle_idle_cpu = cpumask_next(shuffle_idle_cpu, shuffle_tmp_mask);
391 if (shuffle_idle_cpu >= nr_cpu_ids)
392 shuffle_idle_cpu = -1;
Iulia Manda5ed63b12014-03-17 15:21:21 +0200393 else
Paul E. McKenney3808dc92014-01-28 15:29:21 -0800394 cpumask_clear_cpu(shuffle_idle_cpu, shuffle_tmp_mask);
Paul E. McKenney3808dc92014-01-28 15:29:21 -0800395
396 mutex_lock(&shuffle_task_mutex);
397 list_for_each_entry(stp, &shuffle_task_list, st_l)
398 set_cpus_allowed_ptr(stp->st_t, shuffle_tmp_mask);
399 mutex_unlock(&shuffle_task_mutex);
400
401 put_online_cpus();
402}
403
404/* Shuffle tasks across CPUs, with the intent of allowing each CPU in the
405 * system to become idle at a time and cut off its timer ticks. This is meant
406 * to test the support for such tickless idle CPU in RCU.
407 */
408static int torture_shuffle(void *arg)
409{
410 VERBOSE_TOROUT_STRING("torture_shuffle task started");
411 do {
412 schedule_timeout_interruptible(shuffle_interval);
413 torture_shuffle_tasks();
414 torture_shutdown_absorb("torture_shuffle");
415 } while (!torture_must_stop());
Paul E. McKenney7fafaac2014-01-31 17:37:28 -0800416 torture_kthread_stopping("torture_shuffle");
Paul E. McKenney3808dc92014-01-28 15:29:21 -0800417 return 0;
418}
419
420/*
421 * Start the shuffler, with shuffint in jiffies.
422 */
423int torture_shuffle_init(long shuffint)
424{
Paul E. McKenney3808dc92014-01-28 15:29:21 -0800425 shuffle_interval = shuffint;
426
427 shuffle_idle_cpu = -1;
428
429 if (!alloc_cpumask_var(&shuffle_tmp_mask, GFP_KERNEL)) {
430 VERBOSE_TOROUT_ERRSTRING("Failed to alloc mask");
431 return -ENOMEM;
432 }
433
434 /* Create the shuffler thread */
Paul E. McKenney47cf29b2014-02-03 11:52:27 -0800435 return torture_create_kthread(torture_shuffle, NULL, shuffler_task);
Paul E. McKenney3808dc92014-01-28 15:29:21 -0800436}
437EXPORT_SYMBOL_GPL(torture_shuffle_init);
438
439/*
440 * Stop the shuffling.
441 */
Paul E. McKenneycc47ae02014-01-30 14:21:11 -0800442static void torture_shuffle_cleanup(void)
Paul E. McKenney3808dc92014-01-28 15:29:21 -0800443{
444 torture_shuffle_task_unregister_all();
445 if (shuffler_task) {
446 VERBOSE_TOROUT_STRING("Stopping torture_shuffle task");
447 kthread_stop(shuffler_task);
448 free_cpumask_var(shuffle_tmp_mask);
449 }
450 shuffler_task = NULL;
451}
Paul E. McKenney3808dc92014-01-28 15:29:21 -0800452
453/*
Paul E. McKenneye991dbc2014-01-31 14:52:13 -0800454 * Variables for auto-shutdown. This allows "lights out" torture runs
455 * to be fully scripted.
456 */
Paul E. McKenneye991dbc2014-01-31 14:52:13 -0800457static struct task_struct *shutdown_task;
Paul E. McKenney31257c32016-06-18 07:45:43 -0700458static ktime_t shutdown_time; /* time to system shutdown. */
Paul E. McKenneye991dbc2014-01-31 14:52:13 -0800459static void (*torture_shutdown_hook)(void);
460
461/*
Paul E. McKenneyf67a3352014-01-29 07:40:27 -0800462 * Absorb kthreads into a kernel function that won't return, so that
463 * they won't ever access module text or data again.
464 */
465void torture_shutdown_absorb(const char *title)
466{
Paul E. McKenney7d0ae802015-03-03 14:57:58 -0800467 while (READ_ONCE(fullstop) == FULLSTOP_SHUTDOWN) {
Paul E. McKenney4622b482014-01-30 15:37:19 -0800468 pr_notice("torture thread %s parking due to system shutdown\n",
469 title);
Paul E. McKenneyf67a3352014-01-29 07:40:27 -0800470 schedule_timeout_uninterruptible(MAX_SCHEDULE_TIMEOUT);
471 }
472}
473EXPORT_SYMBOL_GPL(torture_shutdown_absorb);
Paul E. McKenneyb5daa8f2014-01-30 13:38:09 -0800474
475/*
Paul E. McKenneye991dbc2014-01-31 14:52:13 -0800476 * Cause the torture test to shutdown the system after the test has
477 * run for the time specified by the shutdown_secs parameter.
478 */
479static int torture_shutdown(void *arg)
480{
Paul E. McKenney31257c32016-06-18 07:45:43 -0700481 ktime_t ktime_snap;
Paul E. McKenneye991dbc2014-01-31 14:52:13 -0800482
483 VERBOSE_TOROUT_STRING("torture_shutdown task started");
Paul E. McKenney31257c32016-06-18 07:45:43 -0700484 ktime_snap = ktime_get();
485 while (ktime_before(ktime_snap, shutdown_time) &&
Paul E. McKenneye991dbc2014-01-31 14:52:13 -0800486 !torture_must_stop()) {
Paul E. McKenneye991dbc2014-01-31 14:52:13 -0800487 if (verbose)
488 pr_alert("%s" TORTURE_FLAG
Paul E. McKenney31257c32016-06-18 07:45:43 -0700489 "torture_shutdown task: %llu ms remaining\n",
490 torture_type,
491 ktime_ms_delta(shutdown_time, ktime_snap));
492 set_current_state(TASK_INTERRUPTIBLE);
493 schedule_hrtimeout(&shutdown_time, HRTIMER_MODE_ABS);
494 ktime_snap = ktime_get();
Paul E. McKenneye991dbc2014-01-31 14:52:13 -0800495 }
496 if (torture_must_stop()) {
Paul E. McKenney7fafaac2014-01-31 17:37:28 -0800497 torture_kthread_stopping("torture_shutdown");
Paul E. McKenneye991dbc2014-01-31 14:52:13 -0800498 return 0;
499 }
500
501 /* OK, shut down the system. */
502
503 VERBOSE_TOROUT_STRING("torture_shutdown task shutting down system");
504 shutdown_task = NULL; /* Avoid self-kill deadlock. */
Paul E. McKenneyf8818252014-02-07 14:42:51 -0800505 if (torture_shutdown_hook)
506 torture_shutdown_hook();
507 else
508 VERBOSE_TOROUT_STRING("No torture_shutdown_hook(), skipping.");
Paul E. McKenneydac95902017-10-04 11:23:10 -0700509 rcu_ftrace_dump(DUMP_ALL);
Paul E. McKenneye991dbc2014-01-31 14:52:13 -0800510 kernel_power_off(); /* Shut down the system. */
511 return 0;
512}
513
514/*
515 * Start up the shutdown task.
516 */
517int torture_shutdown_init(int ssecs, void (*cleanup)(void))
518{
Paul E. McKenneye991dbc2014-01-31 14:52:13 -0800519 torture_shutdown_hook = cleanup;
Paul E. McKenney31257c32016-06-18 07:45:43 -0700520 if (ssecs > 0) {
521 shutdown_time = ktime_add(ktime_get(), ktime_set(ssecs, 0));
Pierce Griffiths2a7d9682018-09-21 20:21:31 -0500522 return torture_create_kthread(torture_shutdown, NULL,
Paul E. McKenney47cf29b2014-02-03 11:52:27 -0800523 shutdown_task);
Paul E. McKenneye991dbc2014-01-31 14:52:13 -0800524 }
Pierce Griffiths2a7d9682018-09-21 20:21:31 -0500525 return 0;
Paul E. McKenneye991dbc2014-01-31 14:52:13 -0800526}
527EXPORT_SYMBOL_GPL(torture_shutdown_init);
528
529/*
Paul E. McKenney4622b482014-01-30 15:37:19 -0800530 * Detect and respond to a system shutdown.
531 */
532static int torture_shutdown_notify(struct notifier_block *unused1,
533 unsigned long unused2, void *unused3)
534{
535 mutex_lock(&fullstop_mutex);
Paul E. McKenney7d0ae802015-03-03 14:57:58 -0800536 if (READ_ONCE(fullstop) == FULLSTOP_DONTSTOP) {
Paul E. McKenneyfac480e2014-01-30 17:06:30 -0800537 VERBOSE_TOROUT_STRING("Unscheduled system shutdown detected");
Paul E. McKenney7d0ae802015-03-03 14:57:58 -0800538 WRITE_ONCE(fullstop, FULLSTOP_SHUTDOWN);
Paul E. McKenneyfac480e2014-01-30 17:06:30 -0800539 } else {
Paul E. McKenney4622b482014-01-30 15:37:19 -0800540 pr_warn("Concurrent rmmod and shutdown illegal!\n");
Paul E. McKenneyfac480e2014-01-30 17:06:30 -0800541 }
Paul E. McKenney4622b482014-01-30 15:37:19 -0800542 mutex_unlock(&fullstop_mutex);
543 return NOTIFY_DONE;
544}
545
546static struct notifier_block torture_shutdown_nb = {
547 .notifier_call = torture_shutdown_notify,
548};
549
550/*
Paul E. McKenneybfefc732014-02-04 12:35:27 -0800551 * Shut down the shutdown task. Say what??? Heh! This can happen if
552 * the torture module gets an rmmod before the shutdown time arrives. ;-)
553 */
554static void torture_shutdown_cleanup(void)
555{
556 unregister_reboot_notifier(&torture_shutdown_nb);
557 if (shutdown_task != NULL) {
558 VERBOSE_TOROUT_STRING("Stopping torture_shutdown task");
559 kthread_stop(shutdown_task);
560 }
561 shutdown_task = NULL;
562}
563
564/*
Paul E. McKenney628edaa2014-01-31 11:57:43 -0800565 * Variables for stuttering, which means to periodically pause and
566 * restart testing in order to catch bugs that appear when load is
567 * suddenly applied to or removed from the system.
568 */
569static struct task_struct *stutter_task;
570static int stutter_pause_test;
571static int stutter;
Paul E. McKenneyff3bf922019-04-09 14:44:49 -0700572static int stutter_gap;
Paul E. McKenney628edaa2014-01-31 11:57:43 -0800573
574/*
575 * Block until the stutter interval ends. This must be called periodically
576 * by all running kthreads that need to be subject to stuttering.
577 */
Paul E. McKenney474e59b2018-08-07 14:34:44 -0700578bool stutter_wait(const char *title)
Paul E. McKenney628edaa2014-01-31 11:57:43 -0800579{
Paul E. McKenney4ced3312017-11-21 15:01:02 -0800580 int spt;
Paul E. McKenneye8516c62019-04-09 11:06:32 -0700581 bool ret = false;
Paul E. McKenney4ced3312017-11-21 15:01:02 -0800582
Paul E. McKenneycee43932018-03-02 16:35:27 -0800583 cond_resched_tasks_rcu_qs();
Paul E. McKenney4ced3312017-11-21 15:01:02 -0800584 spt = READ_ONCE(stutter_pause_test);
Paul E. McKenney29d39392017-11-21 22:07:59 -0800585 for (; spt; spt = READ_ONCE(stutter_pause_test)) {
Paul E. McKenneye8516c62019-04-09 11:06:32 -0700586 ret = true;
Paul E. McKenney4ced3312017-11-21 15:01:02 -0800587 if (spt == 1) {
588 schedule_timeout_interruptible(1);
589 } else if (spt == 2) {
590 while (READ_ONCE(stutter_pause_test))
591 cond_resched();
592 } else {
Paul E. McKenney628edaa2014-01-31 11:57:43 -0800593 schedule_timeout_interruptible(round_jiffies_relative(HZ));
Paul E. McKenney4ced3312017-11-21 15:01:02 -0800594 }
Paul E. McKenney628edaa2014-01-31 11:57:43 -0800595 torture_shutdown_absorb(title);
596 }
Paul E. McKenneye8516c62019-04-09 11:06:32 -0700597 return ret;
Paul E. McKenney628edaa2014-01-31 11:57:43 -0800598}
599EXPORT_SYMBOL_GPL(stutter_wait);
600
601/*
602 * Cause the torture test to "stutter", starting and stopping all
603 * threads periodically.
604 */
605static int torture_stutter(void *arg)
606{
Paul E. McKenneye8516c62019-04-09 11:06:32 -0700607 int wtime;
608
Paul E. McKenney628edaa2014-01-31 11:57:43 -0800609 VERBOSE_TOROUT_STRING("torture_stutter task started");
610 do {
Paul E. McKenney4ced3312017-11-21 15:01:02 -0800611 if (!torture_must_stop() && stutter > 1) {
Paul E. McKenneye8516c62019-04-09 11:06:32 -0700612 wtime = stutter;
613 if (stutter > HZ + 1) {
614 WRITE_ONCE(stutter_pause_test, 1);
615 wtime = stutter - HZ - 1;
616 schedule_timeout_interruptible(wtime);
617 wtime = HZ + 1;
618 }
Paul E. McKenney4ced3312017-11-21 15:01:02 -0800619 WRITE_ONCE(stutter_pause_test, 2);
Paul E. McKenneye8516c62019-04-09 11:06:32 -0700620 schedule_timeout_interruptible(wtime);
Paul E. McKenney628edaa2014-01-31 11:57:43 -0800621 }
Paul E. McKenney4ced3312017-11-21 15:01:02 -0800622 WRITE_ONCE(stutter_pause_test, 0);
Paul E. McKenney628edaa2014-01-31 11:57:43 -0800623 if (!torture_must_stop())
Paul E. McKenneyff3bf922019-04-09 14:44:49 -0700624 schedule_timeout_interruptible(stutter_gap);
Paul E. McKenney628edaa2014-01-31 11:57:43 -0800625 torture_shutdown_absorb("torture_stutter");
626 } while (!torture_must_stop());
Paul E. McKenney7fafaac2014-01-31 17:37:28 -0800627 torture_kthread_stopping("torture_stutter");
Paul E. McKenney628edaa2014-01-31 11:57:43 -0800628 return 0;
629}
630
631/*
632 * Initialize and kick off the torture_stutter kthread.
633 */
Paul E. McKenneyff3bf922019-04-09 14:44:49 -0700634int torture_stutter_init(const int s, const int sgap)
Paul E. McKenney628edaa2014-01-31 11:57:43 -0800635{
Paul E. McKenney628edaa2014-01-31 11:57:43 -0800636 stutter = s;
Paul E. McKenneyff3bf922019-04-09 14:44:49 -0700637 stutter_gap = sgap;
Pierce Griffiths2a7d9682018-09-21 20:21:31 -0500638 return torture_create_kthread(torture_stutter, NULL, stutter_task);
Paul E. McKenney628edaa2014-01-31 11:57:43 -0800639}
640EXPORT_SYMBOL_GPL(torture_stutter_init);
641
642/*
643 * Cleanup after the torture_stutter kthread.
644 */
Paul E. McKenneybfefc732014-02-04 12:35:27 -0800645static void torture_stutter_cleanup(void)
Paul E. McKenney628edaa2014-01-31 11:57:43 -0800646{
647 if (!stutter_task)
648 return;
649 VERBOSE_TOROUT_STRING("Stopping torture_stutter task");
650 kthread_stop(stutter_task);
651 stutter_task = NULL;
652}
Paul E. McKenney628edaa2014-01-31 11:57:43 -0800653
654/*
Paul E. McKenneyb5daa8f2014-01-30 13:38:09 -0800655 * Initialize torture module. Please note that this is -not- invoked via
656 * the usual module_init() mechanism, but rather by an explicit call from
657 * the client torture module. This call must be paired with a later
658 * torture_init_end().
Paul E. McKenney628edaa2014-01-31 11:57:43 -0800659 *
660 * The runnable parameter points to a flag that controls whether or not
661 * the test is currently runnable. If there is no such flag, pass in NULL.
Paul E. McKenneyb5daa8f2014-01-30 13:38:09 -0800662 */
Paul E. McKenney90127d62018-05-09 10:29:18 -0700663bool torture_init_begin(char *ttype, int v)
Paul E. McKenneyb5daa8f2014-01-30 13:38:09 -0800664{
665 mutex_lock(&fullstop_mutex);
Paul E. McKenney52280842014-04-07 09:14:11 -0700666 if (torture_type != NULL) {
Paul E. McKenney9eb51882016-03-21 15:36:40 -0700667 pr_alert("torture_init_begin: Refusing %s init: %s running.\n",
Paul E. McKenney52280842014-04-07 09:14:11 -0700668 ttype, torture_type);
Paul E. McKenney9eb51882016-03-21 15:36:40 -0700669 pr_alert("torture_init_begin: One torture test at a time!\n");
Paul E. McKenney52280842014-04-07 09:14:11 -0700670 mutex_unlock(&fullstop_mutex);
671 return false;
672 }
Paul E. McKenneyb5daa8f2014-01-30 13:38:09 -0800673 torture_type = ttype;
674 verbose = v;
Paul E. McKenney36970bb2014-01-30 15:49:29 -0800675 fullstop = FULLSTOP_DONTSTOP;
Paul E. McKenney52280842014-04-07 09:14:11 -0700676 return true;
Paul E. McKenneyb5daa8f2014-01-30 13:38:09 -0800677}
678EXPORT_SYMBOL_GPL(torture_init_begin);
679
680/*
681 * Tell the torture module that initialization is complete.
682 */
Pranith Kumar2b3f8ff2014-04-16 13:42:09 -0700683void torture_init_end(void)
Paul E. McKenneyb5daa8f2014-01-30 13:38:09 -0800684{
685 mutex_unlock(&fullstop_mutex);
Paul E. McKenney4622b482014-01-30 15:37:19 -0800686 register_reboot_notifier(&torture_shutdown_nb);
Paul E. McKenneyb5daa8f2014-01-30 13:38:09 -0800687}
688EXPORT_SYMBOL_GPL(torture_init_end);
Paul E. McKenneycc47ae02014-01-30 14:21:11 -0800689
690/*
691 * Clean up torture module. Please note that this is -not- invoked via
692 * the usual module_exit() mechanism, but rather by an explicit call from
693 * the client torture module. Returns true if a race with system shutdown
Paul E. McKenneybfefc732014-02-04 12:35:27 -0800694 * is detected, otherwise, all kthreads started by functions in this file
695 * will be shut down.
Paul E. McKenneycc47ae02014-01-30 14:21:11 -0800696 *
697 * This must be called before the caller starts shutting down its own
698 * kthreads.
Davidlohr Buesod36a7a02014-09-11 20:40:21 -0700699 *
700 * Both torture_cleanup_begin() and torture_cleanup_end() must be paired,
701 * in order to correctly perform the cleanup. They are separated because
702 * threads can still need to reference the torture_type type, thus nullify
703 * only after completing all other relevant calls.
Paul E. McKenneycc47ae02014-01-30 14:21:11 -0800704 */
Davidlohr Buesod36a7a02014-09-11 20:40:21 -0700705bool torture_cleanup_begin(void)
Paul E. McKenneycc47ae02014-01-30 14:21:11 -0800706{
707 mutex_lock(&fullstop_mutex);
Paul E. McKenney7d0ae802015-03-03 14:57:58 -0800708 if (READ_ONCE(fullstop) == FULLSTOP_SHUTDOWN) {
Paul E. McKenneycc47ae02014-01-30 14:21:11 -0800709 pr_warn("Concurrent rmmod and shutdown illegal!\n");
710 mutex_unlock(&fullstop_mutex);
711 schedule_timeout_uninterruptible(10);
712 return true;
713 }
Paul E. McKenney7d0ae802015-03-03 14:57:58 -0800714 WRITE_ONCE(fullstop, FULLSTOP_RMMOD);
Paul E. McKenneycc47ae02014-01-30 14:21:11 -0800715 mutex_unlock(&fullstop_mutex);
Paul E. McKenneybfefc732014-02-04 12:35:27 -0800716 torture_shutdown_cleanup();
Paul E. McKenneycc47ae02014-01-30 14:21:11 -0800717 torture_shuffle_cleanup();
Paul E. McKenneybfefc732014-02-04 12:35:27 -0800718 torture_stutter_cleanup();
Paul E. McKenneycc47ae02014-01-30 14:21:11 -0800719 torture_onoff_cleanup();
Davidlohr Buesod36a7a02014-09-11 20:40:21 -0700720 return false;
721}
722EXPORT_SYMBOL_GPL(torture_cleanup_begin);
723
724void torture_cleanup_end(void)
725{
Paul E. McKenney52280842014-04-07 09:14:11 -0700726 mutex_lock(&fullstop_mutex);
727 torture_type = NULL;
728 mutex_unlock(&fullstop_mutex);
Paul E. McKenneycc47ae02014-01-30 14:21:11 -0800729}
Davidlohr Buesod36a7a02014-09-11 20:40:21 -0700730EXPORT_SYMBOL_GPL(torture_cleanup_end);
Paul E. McKenney36970bb2014-01-30 15:49:29 -0800731
732/*
733 * Is it time for the current torture test to stop?
734 */
735bool torture_must_stop(void)
736{
737 return torture_must_stop_irq() || kthread_should_stop();
738}
739EXPORT_SYMBOL_GPL(torture_must_stop);
740
741/*
742 * Is it time for the current torture test to stop? This is the irq-safe
743 * version, hence no check for kthread_should_stop().
744 */
745bool torture_must_stop_irq(void)
746{
Paul E. McKenney7d0ae802015-03-03 14:57:58 -0800747 return READ_ONCE(fullstop) != FULLSTOP_DONTSTOP;
Paul E. McKenney36970bb2014-01-30 15:49:29 -0800748}
749EXPORT_SYMBOL_GPL(torture_must_stop_irq);
Paul E. McKenney7fafaac2014-01-31 17:37:28 -0800750
751/*
752 * Each kthread must wait for kthread_should_stop() before returning from
753 * its top-level function, otherwise segfaults ensue. This function
754 * prints a "stopping" message and waits for kthread_should_stop(), and
755 * should be called from all torture kthreads immediately prior to
756 * returning.
757 */
758void torture_kthread_stopping(char *title)
759{
Paul E. McKenney0d6821d2014-03-03 16:58:03 -0800760 char buf[128];
761
762 snprintf(buf, sizeof(buf), "Stopping %s", title);
763 VERBOSE_TOROUT_STRING(buf);
Paul E. McKenney7fafaac2014-01-31 17:37:28 -0800764 while (!kthread_should_stop()) {
765 torture_shutdown_absorb(title);
766 schedule_timeout_uninterruptible(1);
767 }
768}
769EXPORT_SYMBOL_GPL(torture_kthread_stopping);
Paul E. McKenney47cf29b2014-02-03 11:52:27 -0800770
771/*
772 * Create a generic torture kthread that is immediately runnable. If you
773 * need the kthread to be stopped so that you can do something to it before
774 * it starts, you will need to open-code your own.
775 */
776int _torture_create_kthread(int (*fn)(void *arg), void *arg, char *s, char *m,
777 char *f, struct task_struct **tp)
778{
779 int ret = 0;
780
781 VERBOSE_TOROUT_STRING(m);
Kees Cook69459152014-05-22 11:51:04 -0700782 *tp = kthread_run(fn, arg, "%s", s);
Paul E. McKenney47cf29b2014-02-03 11:52:27 -0800783 if (IS_ERR(*tp)) {
784 ret = PTR_ERR(*tp);
785 VERBOSE_TOROUT_ERRSTRING(f);
786 *tp = NULL;
787 }
788 torture_shuffle_task_register(*tp);
789 return ret;
790}
791EXPORT_SYMBOL_GPL(_torture_create_kthread);
Paul E. McKenney9c029b82014-02-04 11:47:08 -0800792
793/*
794 * Stop a generic kthread, emitting a message.
795 */
796void _torture_stop_kthread(char *m, struct task_struct **tp)
797{
798 if (*tp == NULL)
799 return;
800 VERBOSE_TOROUT_STRING(m);
801 kthread_stop(*tp);
802 *tp = NULL;
803}
804EXPORT_SYMBOL_GPL(_torture_stop_kthread);