blob: 93eeeb2b3d88cff7918d8e1c1ba1589998fe782c [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. McKenney2102ad22020-06-16 15:38:24 -070048static bool ftrace_dump_at_shutdown;
49module_param(ftrace_dump_at_shutdown, bool, 0444);
50
Paul E. McKenney8a67a202020-11-25 13:00:04 -080051static int verbose_sleep_frequency;
52module_param(verbose_sleep_frequency, int, 0444);
53
54static int verbose_sleep_duration = 1;
55module_param(verbose_sleep_duration, int, 0444);
56
Paul E. McKenneyb5daa8f2014-01-30 13:38:09 -080057static char *torture_type;
Paul E. McKenney90127d62018-05-09 10:29:18 -070058static int verbose;
Paul E. McKenneyb5daa8f2014-01-30 13:38:09 -080059
Paul E. McKenney36970bb2014-01-30 15:49:29 -080060/* Mediate rmmod and system shutdown. Concurrent rmmod & shutdown illegal! */
61#define FULLSTOP_DONTSTOP 0 /* Normal operation. */
62#define FULLSTOP_SHUTDOWN 1 /* System shutdown with torture running. */
63#define FULLSTOP_RMMOD 2 /* Normal rmmod of torture. */
64static int fullstop = FULLSTOP_RMMOD;
Paul E. McKenney4622b482014-01-30 15:37:19 -080065static DEFINE_MUTEX(fullstop_mutex);
Paul E. McKenneyf67a3352014-01-29 07:40:27 -080066
Paul E. McKenney8a67a202020-11-25 13:00:04 -080067static atomic_t verbose_sleep_counter;
68
69/*
70 * Sleep if needed from VERBOSE_TOROUT*().
71 */
72void verbose_torout_sleep(void)
73{
74 if (verbose_sleep_frequency > 0 &&
75 verbose_sleep_duration > 0 &&
76 !(atomic_inc_return(&verbose_sleep_counter) % verbose_sleep_frequency))
77 schedule_timeout_uninterruptible(verbose_sleep_duration);
78}
79EXPORT_SYMBOL_GPL(verbose_torout_sleep);
80
Paul E. McKenneyae19aaa2020-11-17 11:30:18 -080081/*
82 * Schedule a high-resolution-timer sleep in nanoseconds, with a 32-bit
83 * nanosecond random fuzz. This function and its friends desynchronize
84 * testing from the timer wheel.
85 */
86int torture_hrtimeout_ns(ktime_t baset_ns, u32 fuzzt_ns, struct torture_random_state *trsp)
87{
88 ktime_t hto = baset_ns;
89
90 if (trsp)
91 hto += (torture_random(trsp) >> 3) % fuzzt_ns;
92 set_current_state(TASK_UNINTERRUPTIBLE);
93 return schedule_hrtimeout(&hto, HRTIMER_MODE_REL);
94}
95EXPORT_SYMBOL_GPL(torture_hrtimeout_ns);
96
97/*
98 * Schedule a high-resolution-timer sleep in microseconds, with a 32-bit
99 * nanosecond (not microsecond!) random fuzz.
100 */
101int torture_hrtimeout_us(u32 baset_us, u32 fuzzt_ns, struct torture_random_state *trsp)
102{
103 ktime_t baset_ns = baset_us * NSEC_PER_USEC;
104
105 return torture_hrtimeout_ns(baset_ns, fuzzt_ns, trsp);
106}
107EXPORT_SYMBOL_GPL(torture_hrtimeout_us);
108
109/*
110 * Schedule a high-resolution-timer sleep in milliseconds, with a 32-bit
111 * microsecond (not millisecond!) random fuzz.
112 */
113int torture_hrtimeout_ms(u32 baset_ms, u32 fuzzt_us, struct torture_random_state *trsp)
114{
115 ktime_t baset_ns = baset_ms * NSEC_PER_MSEC;
116 u32 fuzzt_ns;
117
118 if ((u32)~0U / NSEC_PER_USEC < fuzzt_us)
119 fuzzt_ns = (u32)~0U;
120 else
121 fuzzt_ns = fuzzt_us * NSEC_PER_USEC;
122 return torture_hrtimeout_ns(baset_ns, fuzzt_ns, trsp);
123}
124EXPORT_SYMBOL_GPL(torture_hrtimeout_ms);
125
126/*
127 * Schedule a high-resolution-timer sleep in jiffies, with an
128 * implied one-jiffy random fuzz. This is intended to replace calls to
129 * schedule_timeout_interruptible() and friends.
130 */
131int torture_hrtimeout_jiffies(u32 baset_j, struct torture_random_state *trsp)
132{
133 ktime_t baset_ns = jiffies_to_nsecs(baset_j);
134
135 return torture_hrtimeout_ns(baset_ns, jiffies_to_nsecs(1), trsp);
136}
137EXPORT_SYMBOL_GPL(torture_hrtimeout_jiffies);
138
139/*
140 * Schedule a high-resolution-timer sleep in milliseconds, with a 32-bit
141 * millisecond (not second!) random fuzz.
142 */
143int torture_hrtimeout_s(u32 baset_s, u32 fuzzt_ms, struct torture_random_state *trsp)
144{
145 ktime_t baset_ns = baset_s * NSEC_PER_SEC;
146 u32 fuzzt_ns;
147
148 if ((u32)~0U / NSEC_PER_MSEC < fuzzt_ms)
149 fuzzt_ns = (u32)~0U;
150 else
151 fuzzt_ns = fuzzt_ms * NSEC_PER_MSEC;
152 return torture_hrtimeout_ns(baset_ns, fuzzt_ns, trsp);
153}
154EXPORT_SYMBOL_GPL(torture_hrtimeout_s);
155
Paul E. McKenney2e9e8082014-01-28 15:58:22 -0800156#ifdef CONFIG_HOTPLUG_CPU
157
158/*
159 * Variables for online-offline handling. Only present if CPU hotplug
160 * is enabled, otherwise does nothing.
161 */
162
163static struct task_struct *onoff_task;
164static long onoff_holdoff;
165static long onoff_interval;
Paul E. McKenney3a6cb582018-12-10 09:44:52 -0800166static torture_ofl_func *onoff_f;
Paul E. McKenney2e9e8082014-01-28 15:58:22 -0800167static long n_offline_attempts;
168static long n_offline_successes;
169static unsigned long sum_offline;
170static int min_offline = -1;
171static int max_offline;
172static long n_online_attempts;
173static long n_online_successes;
174static unsigned long sum_online;
175static int min_online = -1;
176static int max_online;
177
178/*
Paul E. McKenneyd95f5ba2016-04-20 17:18:41 -0700179 * Attempt to take a CPU offline. Return false if the CPU is already
180 * offline or if it is not subject to CPU-hotplug operations. The
181 * caller can detect other failures by looking at the statistics.
182 */
183bool torture_offline(int cpu, long *n_offl_attempts, long *n_offl_successes,
184 unsigned long *sum_offl, int *min_offl, int *max_offl)
185{
186 unsigned long delta;
187 int ret;
Paul E. McKenneya59ee762019-12-05 10:49:11 -0800188 char *s;
Paul E. McKenneyd95f5ba2016-04-20 17:18:41 -0700189 unsigned long starttime;
190
191 if (!cpu_online(cpu) || !cpu_is_hotpluggable(cpu))
192 return false;
Paul E. McKenney24aca4a2019-01-22 19:23:00 -0800193 if (num_online_cpus() <= 1)
194 return false; /* Can't offline the last CPU. */
Paul E. McKenneyd95f5ba2016-04-20 17:18:41 -0700195
Paul E. McKenney90127d62018-05-09 10:29:18 -0700196 if (verbose > 1)
Paul E. McKenneyd95f5ba2016-04-20 17:18:41 -0700197 pr_alert("%s" TORTURE_FLAG
198 "torture_onoff task: offlining %d\n",
199 torture_type, cpu);
200 starttime = jiffies;
201 (*n_offl_attempts)++;
Qais Yousef457bc8e2020-03-23 13:51:08 +0000202 ret = remove_cpu(cpu);
Paul E. McKenneyd95f5ba2016-04-20 17:18:41 -0700203 if (ret) {
Paul E. McKenneya59ee762019-12-05 10:49:11 -0800204 s = "";
205 if (!rcu_inkernel_boot_has_ended() && ret == -EBUSY) {
206 // PCI probe frequently disables hotplug during boot.
207 (*n_offl_attempts)--;
208 s = " (-EBUSY forgiven during boot)";
209 }
Paul E. McKenneyd95f5ba2016-04-20 17:18:41 -0700210 if (verbose)
211 pr_alert("%s" TORTURE_FLAG
Paul E. McKenneya59ee762019-12-05 10:49:11 -0800212 "torture_onoff task: offline %d failed%s: errno %d\n",
213 torture_type, cpu, s, ret);
Paul E. McKenneyd95f5ba2016-04-20 17:18:41 -0700214 } else {
Paul E. McKenney90127d62018-05-09 10:29:18 -0700215 if (verbose > 1)
Paul E. McKenneyd95f5ba2016-04-20 17:18:41 -0700216 pr_alert("%s" TORTURE_FLAG
217 "torture_onoff task: offlined %d\n",
218 torture_type, cpu);
Paul E. McKenney3a6cb582018-12-10 09:44:52 -0800219 if (onoff_f)
220 onoff_f();
Paul E. McKenneyd95f5ba2016-04-20 17:18:41 -0700221 (*n_offl_successes)++;
222 delta = jiffies - starttime;
Paul E. McKenneya2b2df22017-06-22 15:38:26 -0700223 *sum_offl += delta;
Paul E. McKenneyd95f5ba2016-04-20 17:18:41 -0700224 if (*min_offl < 0) {
225 *min_offl = delta;
226 *max_offl = delta;
227 }
228 if (*min_offl > delta)
229 *min_offl = delta;
230 if (*max_offl < delta)
231 *max_offl = delta;
232 }
233
234 return true;
235}
236EXPORT_SYMBOL_GPL(torture_offline);
237
238/*
239 * Attempt to bring a CPU online. Return false if the CPU is already
240 * online or if it is not subject to CPU-hotplug operations. The
241 * caller can detect other failures by looking at the statistics.
242 */
243bool torture_online(int cpu, long *n_onl_attempts, long *n_onl_successes,
244 unsigned long *sum_onl, int *min_onl, int *max_onl)
245{
246 unsigned long delta;
247 int ret;
Paul E. McKenneya59ee762019-12-05 10:49:11 -0800248 char *s;
Paul E. McKenneyd95f5ba2016-04-20 17:18:41 -0700249 unsigned long starttime;
250
251 if (cpu_online(cpu) || !cpu_is_hotpluggable(cpu))
252 return false;
253
Paul E. McKenney90127d62018-05-09 10:29:18 -0700254 if (verbose > 1)
Paul E. McKenneyd95f5ba2016-04-20 17:18:41 -0700255 pr_alert("%s" TORTURE_FLAG
256 "torture_onoff task: onlining %d\n",
257 torture_type, cpu);
258 starttime = jiffies;
259 (*n_onl_attempts)++;
Qais Yousef457bc8e2020-03-23 13:51:08 +0000260 ret = add_cpu(cpu);
Paul E. McKenneyd95f5ba2016-04-20 17:18:41 -0700261 if (ret) {
Paul E. McKenneya59ee762019-12-05 10:49:11 -0800262 s = "";
263 if (!rcu_inkernel_boot_has_ended() && ret == -EBUSY) {
264 // PCI probe frequently disables hotplug during boot.
265 (*n_onl_attempts)--;
266 s = " (-EBUSY forgiven during boot)";
267 }
Paul E. McKenneyd95f5ba2016-04-20 17:18:41 -0700268 if (verbose)
269 pr_alert("%s" TORTURE_FLAG
Paul E. McKenneya59ee762019-12-05 10:49:11 -0800270 "torture_onoff task: online %d failed%s: errno %d\n",
271 torture_type, cpu, s, ret);
Paul E. McKenneyd95f5ba2016-04-20 17:18:41 -0700272 } else {
Paul E. McKenney90127d62018-05-09 10:29:18 -0700273 if (verbose > 1)
Paul E. McKenneyd95f5ba2016-04-20 17:18:41 -0700274 pr_alert("%s" TORTURE_FLAG
275 "torture_onoff task: onlined %d\n",
276 torture_type, cpu);
277 (*n_onl_successes)++;
278 delta = jiffies - starttime;
279 *sum_onl += delta;
280 if (*min_onl < 0) {
281 *min_onl = delta;
282 *max_onl = delta;
283 }
284 if (*min_onl > delta)
285 *min_onl = delta;
286 if (*max_onl < delta)
287 *max_onl = delta;
288 }
289
290 return true;
291}
292EXPORT_SYMBOL_GPL(torture_online);
293
294/*
Paul E. McKenney2e9e8082014-01-28 15:58:22 -0800295 * Execute random CPU-hotplug operations at the interval specified
296 * by the onoff_interval.
297 */
298static int
299torture_onoff(void *arg)
300{
301 int cpu;
Paul E. McKenney2e9e8082014-01-28 15:58:22 -0800302 int maxcpu = -1;
303 DEFINE_TORTURE_RANDOM(rand);
Paul E. McKenney28cf5952018-08-21 15:27:16 -0700304 int ret;
Paul E. McKenney2e9e8082014-01-28 15:58:22 -0800305
306 VERBOSE_TOROUT_STRING("torture_onoff task started");
307 for_each_online_cpu(cpu)
308 maxcpu = cpu;
309 WARN_ON(maxcpu < 0);
Qais Yousef457bc8e2020-03-23 13:51:08 +0000310 if (!IS_MODULE(CONFIG_TORTURE_TEST)) {
Paul E. McKenney28cf5952018-08-21 15:27:16 -0700311 for_each_possible_cpu(cpu) {
312 if (cpu_online(cpu))
313 continue;
Qais Yousef457bc8e2020-03-23 13:51:08 +0000314 ret = add_cpu(cpu);
Paul E. McKenney28cf5952018-08-21 15:27:16 -0700315 if (ret && verbose) {
316 pr_alert("%s" TORTURE_FLAG
317 "%s: Initial online %d: errno %d\n",
318 __func__, torture_type, cpu, ret);
319 }
320 }
Qais Yousef457bc8e2020-03-23 13:51:08 +0000321 }
Boqun Feng750db0f2016-05-02 10:30:00 +0800322
323 if (maxcpu == 0) {
324 VERBOSE_TOROUT_STRING("Only one CPU, so CPU-hotplug testing is disabled");
325 goto stop;
326 }
327
Paul E. McKenney2e9e8082014-01-28 15:58:22 -0800328 if (onoff_holdoff > 0) {
329 VERBOSE_TOROUT_STRING("torture_onoff begin holdoff");
330 schedule_timeout_interruptible(onoff_holdoff);
331 VERBOSE_TOROUT_STRING("torture_onoff end holdoff");
332 }
333 while (!torture_must_stop()) {
Paul E. McKenney8171d3e2019-12-06 15:02:59 -0800334 if (disable_onoff_at_boot && !rcu_inkernel_boot_has_ended()) {
335 schedule_timeout_interruptible(HZ / 10);
336 continue;
337 }
Paul E. McKenney2e9e8082014-01-28 15:58:22 -0800338 cpu = (torture_random(&rand) >> 4) % (maxcpu + 1);
Paul E. McKenneyd95f5ba2016-04-20 17:18:41 -0700339 if (!torture_offline(cpu,
340 &n_offline_attempts, &n_offline_successes,
341 &sum_offline, &min_offline, &max_offline))
342 torture_online(cpu,
343 &n_online_attempts, &n_online_successes,
344 &sum_online, &min_online, &max_online);
Paul E. McKenney2e9e8082014-01-28 15:58:22 -0800345 schedule_timeout_interruptible(onoff_interval);
346 }
Boqun Feng750db0f2016-05-02 10:30:00 +0800347
348stop:
Paul E. McKenney7fafaac2014-01-31 17:37:28 -0800349 torture_kthread_stopping("torture_onoff");
Paul E. McKenney2e9e8082014-01-28 15:58:22 -0800350 return 0;
351}
352
353#endif /* #ifdef CONFIG_HOTPLUG_CPU */
354
355/*
356 * Initiate online-offline handling.
357 */
Paul E. McKenney3a6cb582018-12-10 09:44:52 -0800358int torture_onoff_init(long ooholdoff, long oointerval, torture_ofl_func *f)
Paul E. McKenney2e9e8082014-01-28 15:58:22 -0800359{
Paul E. McKenney47cf29b2014-02-03 11:52:27 -0800360#ifdef CONFIG_HOTPLUG_CPU
Paul E. McKenney2e9e8082014-01-28 15:58:22 -0800361 onoff_holdoff = ooholdoff;
362 onoff_interval = oointerval;
Paul E. McKenney3a6cb582018-12-10 09:44:52 -0800363 onoff_f = f;
Paul E. McKenney2e9e8082014-01-28 15:58:22 -0800364 if (onoff_interval <= 0)
365 return 0;
Pierce Griffiths2a7d9682018-09-21 20:21:31 -0500366 return torture_create_kthread(torture_onoff, NULL, onoff_task);
367#else /* #ifdef CONFIG_HOTPLUG_CPU */
368 return 0;
369#endif /* #else #ifdef CONFIG_HOTPLUG_CPU */
Paul E. McKenney2e9e8082014-01-28 15:58:22 -0800370}
371EXPORT_SYMBOL_GPL(torture_onoff_init);
372
373/*
374 * Clean up after online/offline testing.
375 */
Paul E. McKenneycc47ae02014-01-30 14:21:11 -0800376static void torture_onoff_cleanup(void)
Paul E. McKenney2e9e8082014-01-28 15:58:22 -0800377{
378#ifdef CONFIG_HOTPLUG_CPU
379 if (onoff_task == NULL)
380 return;
381 VERBOSE_TOROUT_STRING("Stopping torture_onoff task");
382 kthread_stop(onoff_task);
383 onoff_task = NULL;
384#endif /* #ifdef CONFIG_HOTPLUG_CPU */
385}
Paul E. McKenney2e9e8082014-01-28 15:58:22 -0800386
387/*
388 * Print online/offline testing statistics.
389 */
Joe Percheseea203f2014-07-14 09:16:15 -0400390void torture_onoff_stats(void)
Paul E. McKenney2e9e8082014-01-28 15:58:22 -0800391{
392#ifdef CONFIG_HOTPLUG_CPU
Joe Percheseea203f2014-07-14 09:16:15 -0400393 pr_cont("onoff: %ld/%ld:%ld/%ld %d,%d:%d,%d %lu:%lu (HZ=%d) ",
394 n_online_successes, n_online_attempts,
395 n_offline_successes, n_offline_attempts,
396 min_online, max_online,
397 min_offline, max_offline,
398 sum_online, sum_offline, HZ);
Paul E. McKenney2e9e8082014-01-28 15:58:22 -0800399#endif /* #ifdef CONFIG_HOTPLUG_CPU */
Paul E. McKenney2e9e8082014-01-28 15:58:22 -0800400}
401EXPORT_SYMBOL_GPL(torture_onoff_stats);
402
403/*
404 * Were all the online/offline operations successful?
405 */
406bool torture_onoff_failures(void)
407{
408#ifdef CONFIG_HOTPLUG_CPU
409 return n_online_successes != n_online_attempts ||
410 n_offline_successes != n_offline_attempts;
411#else /* #ifdef CONFIG_HOTPLUG_CPU */
412 return false;
413#endif /* #else #ifdef CONFIG_HOTPLUG_CPU */
414}
415EXPORT_SYMBOL_GPL(torture_onoff_failures);
416
Paul E. McKenney51b11302014-01-27 11:49:39 -0800417#define TORTURE_RANDOM_MULT 39916801 /* prime */
418#define TORTURE_RANDOM_ADD 479001701 /* prime */
419#define TORTURE_RANDOM_REFRESH 10000
420
421/*
422 * Crude but fast random-number generator. Uses a linear congruential
423 * generator, with occasional help from cpu_clock().
424 */
425unsigned long
426torture_random(struct torture_random_state *trsp)
427{
428 if (--trsp->trs_count < 0) {
429 trsp->trs_state += (unsigned long)local_clock();
430 trsp->trs_count = TORTURE_RANDOM_REFRESH;
431 }
432 trsp->trs_state = trsp->trs_state * TORTURE_RANDOM_MULT +
433 TORTURE_RANDOM_ADD;
434 return swahw32(trsp->trs_state);
435}
436EXPORT_SYMBOL_GPL(torture_random);
Paul E. McKenneyf67a3352014-01-29 07:40:27 -0800437
438/*
Paul E. McKenney3808dc92014-01-28 15:29:21 -0800439 * Variables for shuffling. The idea is to ensure that each CPU stays
440 * idle for an extended period to test interactions with dyntick idle,
Masahiro Yamadab564d622017-02-27 14:29:06 -0800441 * as well as interactions with any per-CPU variables.
Paul E. McKenney3808dc92014-01-28 15:29:21 -0800442 */
443struct shuffle_task {
444 struct list_head st_l;
445 struct task_struct *st_t;
446};
447
448static long shuffle_interval; /* In jiffies. */
449static struct task_struct *shuffler_task;
450static cpumask_var_t shuffle_tmp_mask;
451static int shuffle_idle_cpu; /* Force all torture tasks off this CPU */
452static struct list_head shuffle_task_list = LIST_HEAD_INIT(shuffle_task_list);
453static DEFINE_MUTEX(shuffle_task_mutex);
454
455/*
456 * Register a task to be shuffled. If there is no memory, just splat
457 * and don't bother registering.
458 */
459void torture_shuffle_task_register(struct task_struct *tp)
460{
461 struct shuffle_task *stp;
462
463 if (WARN_ON_ONCE(tp == NULL))
464 return;
465 stp = kmalloc(sizeof(*stp), GFP_KERNEL);
466 if (WARN_ON_ONCE(stp == NULL))
467 return;
468 stp->st_t = tp;
469 mutex_lock(&shuffle_task_mutex);
470 list_add(&stp->st_l, &shuffle_task_list);
471 mutex_unlock(&shuffle_task_mutex);
472}
473EXPORT_SYMBOL_GPL(torture_shuffle_task_register);
474
475/*
476 * Unregister all tasks, for example, at the end of the torture run.
477 */
478static void torture_shuffle_task_unregister_all(void)
479{
480 struct shuffle_task *stp;
481 struct shuffle_task *p;
482
483 mutex_lock(&shuffle_task_mutex);
484 list_for_each_entry_safe(stp, p, &shuffle_task_list, st_l) {
485 list_del(&stp->st_l);
486 kfree(stp);
487 }
488 mutex_unlock(&shuffle_task_mutex);
489}
490
491/* Shuffle tasks such that we allow shuffle_idle_cpu to become idle.
492 * A special case is when shuffle_idle_cpu = -1, in which case we allow
493 * the tasks to run on all CPUs.
494 */
495static void torture_shuffle_tasks(void)
496{
497 struct shuffle_task *stp;
498
499 cpumask_setall(shuffle_tmp_mask);
500 get_online_cpus();
501
502 /* No point in shuffling if there is only one online CPU (ex: UP) */
503 if (num_online_cpus() == 1) {
504 put_online_cpus();
505 return;
506 }
507
508 /* Advance to the next CPU. Upon overflow, don't idle any CPUs. */
509 shuffle_idle_cpu = cpumask_next(shuffle_idle_cpu, shuffle_tmp_mask);
510 if (shuffle_idle_cpu >= nr_cpu_ids)
511 shuffle_idle_cpu = -1;
Iulia Manda5ed63b12014-03-17 15:21:21 +0200512 else
Paul E. McKenney3808dc92014-01-28 15:29:21 -0800513 cpumask_clear_cpu(shuffle_idle_cpu, shuffle_tmp_mask);
Paul E. McKenney3808dc92014-01-28 15:29:21 -0800514
515 mutex_lock(&shuffle_task_mutex);
516 list_for_each_entry(stp, &shuffle_task_list, st_l)
517 set_cpus_allowed_ptr(stp->st_t, shuffle_tmp_mask);
518 mutex_unlock(&shuffle_task_mutex);
519
520 put_online_cpus();
521}
522
523/* Shuffle tasks across CPUs, with the intent of allowing each CPU in the
524 * system to become idle at a time and cut off its timer ticks. This is meant
525 * to test the support for such tickless idle CPU in RCU.
526 */
527static int torture_shuffle(void *arg)
528{
529 VERBOSE_TOROUT_STRING("torture_shuffle task started");
530 do {
531 schedule_timeout_interruptible(shuffle_interval);
532 torture_shuffle_tasks();
533 torture_shutdown_absorb("torture_shuffle");
534 } while (!torture_must_stop());
Paul E. McKenney7fafaac2014-01-31 17:37:28 -0800535 torture_kthread_stopping("torture_shuffle");
Paul E. McKenney3808dc92014-01-28 15:29:21 -0800536 return 0;
537}
538
539/*
540 * Start the shuffler, with shuffint in jiffies.
541 */
542int torture_shuffle_init(long shuffint)
543{
Paul E. McKenney3808dc92014-01-28 15:29:21 -0800544 shuffle_interval = shuffint;
545
546 shuffle_idle_cpu = -1;
547
548 if (!alloc_cpumask_var(&shuffle_tmp_mask, GFP_KERNEL)) {
549 VERBOSE_TOROUT_ERRSTRING("Failed to alloc mask");
550 return -ENOMEM;
551 }
552
553 /* Create the shuffler thread */
Paul E. McKenney47cf29b2014-02-03 11:52:27 -0800554 return torture_create_kthread(torture_shuffle, NULL, shuffler_task);
Paul E. McKenney3808dc92014-01-28 15:29:21 -0800555}
556EXPORT_SYMBOL_GPL(torture_shuffle_init);
557
558/*
559 * Stop the shuffling.
560 */
Paul E. McKenneycc47ae02014-01-30 14:21:11 -0800561static void torture_shuffle_cleanup(void)
Paul E. McKenney3808dc92014-01-28 15:29:21 -0800562{
563 torture_shuffle_task_unregister_all();
564 if (shuffler_task) {
565 VERBOSE_TOROUT_STRING("Stopping torture_shuffle task");
566 kthread_stop(shuffler_task);
567 free_cpumask_var(shuffle_tmp_mask);
568 }
569 shuffler_task = NULL;
570}
Paul E. McKenney3808dc92014-01-28 15:29:21 -0800571
572/*
Paul E. McKenneye991dbc2014-01-31 14:52:13 -0800573 * Variables for auto-shutdown. This allows "lights out" torture runs
574 * to be fully scripted.
575 */
Paul E. McKenneye991dbc2014-01-31 14:52:13 -0800576static struct task_struct *shutdown_task;
Paul E. McKenney31257c32016-06-18 07:45:43 -0700577static ktime_t shutdown_time; /* time to system shutdown. */
Paul E. McKenneye991dbc2014-01-31 14:52:13 -0800578static void (*torture_shutdown_hook)(void);
579
580/*
Paul E. McKenneyf67a3352014-01-29 07:40:27 -0800581 * Absorb kthreads into a kernel function that won't return, so that
582 * they won't ever access module text or data again.
583 */
584void torture_shutdown_absorb(const char *title)
585{
Paul E. McKenney7d0ae802015-03-03 14:57:58 -0800586 while (READ_ONCE(fullstop) == FULLSTOP_SHUTDOWN) {
Paul E. McKenney4622b482014-01-30 15:37:19 -0800587 pr_notice("torture thread %s parking due to system shutdown\n",
588 title);
Paul E. McKenneyf67a3352014-01-29 07:40:27 -0800589 schedule_timeout_uninterruptible(MAX_SCHEDULE_TIMEOUT);
590 }
591}
592EXPORT_SYMBOL_GPL(torture_shutdown_absorb);
Paul E. McKenneyb5daa8f2014-01-30 13:38:09 -0800593
594/*
Paul E. McKenneye991dbc2014-01-31 14:52:13 -0800595 * Cause the torture test to shutdown the system after the test has
596 * run for the time specified by the shutdown_secs parameter.
597 */
598static int torture_shutdown(void *arg)
599{
Paul E. McKenney31257c32016-06-18 07:45:43 -0700600 ktime_t ktime_snap;
Paul E. McKenneye991dbc2014-01-31 14:52:13 -0800601
602 VERBOSE_TOROUT_STRING("torture_shutdown task started");
Paul E. McKenney31257c32016-06-18 07:45:43 -0700603 ktime_snap = ktime_get();
604 while (ktime_before(ktime_snap, shutdown_time) &&
Paul E. McKenneye991dbc2014-01-31 14:52:13 -0800605 !torture_must_stop()) {
Paul E. McKenneye991dbc2014-01-31 14:52:13 -0800606 if (verbose)
607 pr_alert("%s" TORTURE_FLAG
Paul E. McKenney31257c32016-06-18 07:45:43 -0700608 "torture_shutdown task: %llu ms remaining\n",
609 torture_type,
610 ktime_ms_delta(shutdown_time, ktime_snap));
611 set_current_state(TASK_INTERRUPTIBLE);
612 schedule_hrtimeout(&shutdown_time, HRTIMER_MODE_ABS);
613 ktime_snap = ktime_get();
Paul E. McKenneye991dbc2014-01-31 14:52:13 -0800614 }
615 if (torture_must_stop()) {
Paul E. McKenney7fafaac2014-01-31 17:37:28 -0800616 torture_kthread_stopping("torture_shutdown");
Paul E. McKenneye991dbc2014-01-31 14:52:13 -0800617 return 0;
618 }
619
620 /* OK, shut down the system. */
621
622 VERBOSE_TOROUT_STRING("torture_shutdown task shutting down system");
623 shutdown_task = NULL; /* Avoid self-kill deadlock. */
Paul E. McKenneyf8818252014-02-07 14:42:51 -0800624 if (torture_shutdown_hook)
625 torture_shutdown_hook();
626 else
627 VERBOSE_TOROUT_STRING("No torture_shutdown_hook(), skipping.");
Paul E. McKenney2102ad22020-06-16 15:38:24 -0700628 if (ftrace_dump_at_shutdown)
629 rcu_ftrace_dump(DUMP_ALL);
Paul E. McKenneye991dbc2014-01-31 14:52:13 -0800630 kernel_power_off(); /* Shut down the system. */
631 return 0;
632}
633
634/*
635 * Start up the shutdown task.
636 */
637int torture_shutdown_init(int ssecs, void (*cleanup)(void))
638{
Paul E. McKenneye991dbc2014-01-31 14:52:13 -0800639 torture_shutdown_hook = cleanup;
Paul E. McKenney31257c32016-06-18 07:45:43 -0700640 if (ssecs > 0) {
641 shutdown_time = ktime_add(ktime_get(), ktime_set(ssecs, 0));
Pierce Griffiths2a7d9682018-09-21 20:21:31 -0500642 return torture_create_kthread(torture_shutdown, NULL,
Paul E. McKenney47cf29b2014-02-03 11:52:27 -0800643 shutdown_task);
Paul E. McKenneye991dbc2014-01-31 14:52:13 -0800644 }
Pierce Griffiths2a7d9682018-09-21 20:21:31 -0500645 return 0;
Paul E. McKenneye991dbc2014-01-31 14:52:13 -0800646}
647EXPORT_SYMBOL_GPL(torture_shutdown_init);
648
649/*
Paul E. McKenney4622b482014-01-30 15:37:19 -0800650 * Detect and respond to a system shutdown.
651 */
652static int torture_shutdown_notify(struct notifier_block *unused1,
653 unsigned long unused2, void *unused3)
654{
655 mutex_lock(&fullstop_mutex);
Paul E. McKenney7d0ae802015-03-03 14:57:58 -0800656 if (READ_ONCE(fullstop) == FULLSTOP_DONTSTOP) {
Paul E. McKenneyfac480e2014-01-30 17:06:30 -0800657 VERBOSE_TOROUT_STRING("Unscheduled system shutdown detected");
Paul E. McKenney7d0ae802015-03-03 14:57:58 -0800658 WRITE_ONCE(fullstop, FULLSTOP_SHUTDOWN);
Paul E. McKenneyfac480e2014-01-30 17:06:30 -0800659 } else {
Paul E. McKenney4622b482014-01-30 15:37:19 -0800660 pr_warn("Concurrent rmmod and shutdown illegal!\n");
Paul E. McKenneyfac480e2014-01-30 17:06:30 -0800661 }
Paul E. McKenney4622b482014-01-30 15:37:19 -0800662 mutex_unlock(&fullstop_mutex);
663 return NOTIFY_DONE;
664}
665
666static struct notifier_block torture_shutdown_nb = {
667 .notifier_call = torture_shutdown_notify,
668};
669
670/*
Paul E. McKenneybfefc732014-02-04 12:35:27 -0800671 * Shut down the shutdown task. Say what??? Heh! This can happen if
672 * the torture module gets an rmmod before the shutdown time arrives. ;-)
673 */
674static void torture_shutdown_cleanup(void)
675{
676 unregister_reboot_notifier(&torture_shutdown_nb);
677 if (shutdown_task != NULL) {
678 VERBOSE_TOROUT_STRING("Stopping torture_shutdown task");
679 kthread_stop(shutdown_task);
680 }
681 shutdown_task = NULL;
682}
683
684/*
Paul E. McKenney628edaa2014-01-31 11:57:43 -0800685 * Variables for stuttering, which means to periodically pause and
686 * restart testing in order to catch bugs that appear when load is
687 * suddenly applied to or removed from the system.
688 */
689static struct task_struct *stutter_task;
690static int stutter_pause_test;
691static int stutter;
Paul E. McKenneyff3bf922019-04-09 14:44:49 -0700692static int stutter_gap;
Paul E. McKenney628edaa2014-01-31 11:57:43 -0800693
694/*
695 * Block until the stutter interval ends. This must be called periodically
696 * by all running kthreads that need to be subject to stuttering.
697 */
Paul E. McKenney474e59b2018-08-07 14:34:44 -0700698bool stutter_wait(const char *title)
Paul E. McKenney628edaa2014-01-31 11:57:43 -0800699{
Paul E. McKenney19012b72020-09-01 16:58:41 -0700700 unsigned int i = 0;
Paul E. McKenneye8516c62019-04-09 11:06:32 -0700701 bool ret = false;
Paul E. McKenney19012b72020-09-01 16:58:41 -0700702 int spt;
Paul E. McKenney4ced3312017-11-21 15:01:02 -0800703
Paul E. McKenneycee43932018-03-02 16:35:27 -0800704 cond_resched_tasks_rcu_qs();
Paul E. McKenney4ced3312017-11-21 15:01:02 -0800705 spt = READ_ONCE(stutter_pause_test);
Paul E. McKenney29d39392017-11-21 22:07:59 -0800706 for (; spt; spt = READ_ONCE(stutter_pause_test)) {
Paul E. McKenneyab1b7882020-09-22 16:42:42 -0700707 if (!ret) {
708 sched_set_normal(current, MAX_NICE);
709 ret = true;
710 }
Paul E. McKenney4ced3312017-11-21 15:01:02 -0800711 if (spt == 1) {
712 schedule_timeout_interruptible(1);
713 } else if (spt == 2) {
Paul E. McKenney19012b72020-09-01 16:58:41 -0700714 while (READ_ONCE(stutter_pause_test)) {
Paul E. McKenneyed24aff2020-11-17 12:17:42 -0800715 if (!(i++ & 0xffff))
716 torture_hrtimeout_us(10, 0, NULL);
Paul E. McKenney4ced3312017-11-21 15:01:02 -0800717 cond_resched();
Paul E. McKenney19012b72020-09-01 16:58:41 -0700718 }
Paul E. McKenney4ced3312017-11-21 15:01:02 -0800719 } else {
Paul E. McKenney628edaa2014-01-31 11:57:43 -0800720 schedule_timeout_interruptible(round_jiffies_relative(HZ));
Paul E. McKenney4ced3312017-11-21 15:01:02 -0800721 }
Paul E. McKenney628edaa2014-01-31 11:57:43 -0800722 torture_shutdown_absorb(title);
723 }
Paul E. McKenneye8516c62019-04-09 11:06:32 -0700724 return ret;
Paul E. McKenney628edaa2014-01-31 11:57:43 -0800725}
726EXPORT_SYMBOL_GPL(stutter_wait);
727
728/*
729 * Cause the torture test to "stutter", starting and stopping all
730 * threads periodically.
731 */
732static int torture_stutter(void *arg)
733{
Paul E. McKenneyfda5ba92020-09-02 21:08:41 -0700734 DEFINE_TORTURE_RANDOM(rand);
Paul E. McKenneye8516c62019-04-09 11:06:32 -0700735 int wtime;
736
Paul E. McKenney628edaa2014-01-31 11:57:43 -0800737 VERBOSE_TOROUT_STRING("torture_stutter task started");
738 do {
Paul E. McKenney4ced3312017-11-21 15:01:02 -0800739 if (!torture_must_stop() && stutter > 1) {
Paul E. McKenneye8516c62019-04-09 11:06:32 -0700740 wtime = stutter;
Paul E. McKenneyfda5ba92020-09-02 21:08:41 -0700741 if (stutter > 2) {
Paul E. McKenneye8516c62019-04-09 11:06:32 -0700742 WRITE_ONCE(stutter_pause_test, 1);
Paul E. McKenneyfda5ba92020-09-02 21:08:41 -0700743 wtime = stutter - 3;
Paul E. McKenneyed24aff2020-11-17 12:17:42 -0800744 torture_hrtimeout_jiffies(wtime, &rand);
Paul E. McKenneyfda5ba92020-09-02 21:08:41 -0700745 wtime = 2;
Paul E. McKenneye8516c62019-04-09 11:06:32 -0700746 }
Paul E. McKenney4ced3312017-11-21 15:01:02 -0800747 WRITE_ONCE(stutter_pause_test, 2);
Paul E. McKenneyed24aff2020-11-17 12:17:42 -0800748 torture_hrtimeout_jiffies(wtime, NULL);
Paul E. McKenney628edaa2014-01-31 11:57:43 -0800749 }
Paul E. McKenney4ced3312017-11-21 15:01:02 -0800750 WRITE_ONCE(stutter_pause_test, 0);
Paul E. McKenney628edaa2014-01-31 11:57:43 -0800751 if (!torture_must_stop())
Paul E. McKenneyed24aff2020-11-17 12:17:42 -0800752 torture_hrtimeout_jiffies(stutter_gap, NULL);
Paul E. McKenney628edaa2014-01-31 11:57:43 -0800753 torture_shutdown_absorb("torture_stutter");
754 } while (!torture_must_stop());
Paul E. McKenney7fafaac2014-01-31 17:37:28 -0800755 torture_kthread_stopping("torture_stutter");
Paul E. McKenney628edaa2014-01-31 11:57:43 -0800756 return 0;
757}
758
759/*
760 * Initialize and kick off the torture_stutter kthread.
761 */
Paul E. McKenneyff3bf922019-04-09 14:44:49 -0700762int torture_stutter_init(const int s, const int sgap)
Paul E. McKenney628edaa2014-01-31 11:57:43 -0800763{
Paul E. McKenney628edaa2014-01-31 11:57:43 -0800764 stutter = s;
Paul E. McKenneyff3bf922019-04-09 14:44:49 -0700765 stutter_gap = sgap;
Pierce Griffiths2a7d9682018-09-21 20:21:31 -0500766 return torture_create_kthread(torture_stutter, NULL, stutter_task);
Paul E. McKenney628edaa2014-01-31 11:57:43 -0800767}
768EXPORT_SYMBOL_GPL(torture_stutter_init);
769
770/*
771 * Cleanup after the torture_stutter kthread.
772 */
Paul E. McKenneybfefc732014-02-04 12:35:27 -0800773static void torture_stutter_cleanup(void)
Paul E. McKenney628edaa2014-01-31 11:57:43 -0800774{
775 if (!stutter_task)
776 return;
777 VERBOSE_TOROUT_STRING("Stopping torture_stutter task");
778 kthread_stop(stutter_task);
779 stutter_task = NULL;
780}
Paul E. McKenney628edaa2014-01-31 11:57:43 -0800781
782/*
Paul E. McKenneyb5daa8f2014-01-30 13:38:09 -0800783 * Initialize torture module. Please note that this is -not- invoked via
784 * the usual module_init() mechanism, but rather by an explicit call from
785 * the client torture module. This call must be paired with a later
786 * torture_init_end().
Paul E. McKenney628edaa2014-01-31 11:57:43 -0800787 *
788 * The runnable parameter points to a flag that controls whether or not
789 * the test is currently runnable. If there is no such flag, pass in NULL.
Paul E. McKenneyb5daa8f2014-01-30 13:38:09 -0800790 */
Paul E. McKenney90127d62018-05-09 10:29:18 -0700791bool torture_init_begin(char *ttype, int v)
Paul E. McKenneyb5daa8f2014-01-30 13:38:09 -0800792{
793 mutex_lock(&fullstop_mutex);
Paul E. McKenney52280842014-04-07 09:14:11 -0700794 if (torture_type != NULL) {
Paul E. McKenney9eb51882016-03-21 15:36:40 -0700795 pr_alert("torture_init_begin: Refusing %s init: %s running.\n",
Paul E. McKenney52280842014-04-07 09:14:11 -0700796 ttype, torture_type);
Paul E. McKenney9eb51882016-03-21 15:36:40 -0700797 pr_alert("torture_init_begin: One torture test at a time!\n");
Paul E. McKenney52280842014-04-07 09:14:11 -0700798 mutex_unlock(&fullstop_mutex);
799 return false;
800 }
Paul E. McKenneyb5daa8f2014-01-30 13:38:09 -0800801 torture_type = ttype;
802 verbose = v;
Paul E. McKenney36970bb2014-01-30 15:49:29 -0800803 fullstop = FULLSTOP_DONTSTOP;
Paul E. McKenney52280842014-04-07 09:14:11 -0700804 return true;
Paul E. McKenneyb5daa8f2014-01-30 13:38:09 -0800805}
806EXPORT_SYMBOL_GPL(torture_init_begin);
807
808/*
809 * Tell the torture module that initialization is complete.
810 */
Pranith Kumar2b3f8ff2014-04-16 13:42:09 -0700811void torture_init_end(void)
Paul E. McKenneyb5daa8f2014-01-30 13:38:09 -0800812{
813 mutex_unlock(&fullstop_mutex);
Paul E. McKenney4622b482014-01-30 15:37:19 -0800814 register_reboot_notifier(&torture_shutdown_nb);
Paul E. McKenneyb5daa8f2014-01-30 13:38:09 -0800815}
816EXPORT_SYMBOL_GPL(torture_init_end);
Paul E. McKenneycc47ae02014-01-30 14:21:11 -0800817
818/*
819 * Clean up torture module. Please note that this is -not- invoked via
820 * the usual module_exit() mechanism, but rather by an explicit call from
821 * the client torture module. Returns true if a race with system shutdown
Paul E. McKenneybfefc732014-02-04 12:35:27 -0800822 * is detected, otherwise, all kthreads started by functions in this file
823 * will be shut down.
Paul E. McKenneycc47ae02014-01-30 14:21:11 -0800824 *
825 * This must be called before the caller starts shutting down its own
826 * kthreads.
Davidlohr Buesod36a7a02014-09-11 20:40:21 -0700827 *
828 * Both torture_cleanup_begin() and torture_cleanup_end() must be paired,
829 * in order to correctly perform the cleanup. They are separated because
830 * threads can still need to reference the torture_type type, thus nullify
831 * only after completing all other relevant calls.
Paul E. McKenneycc47ae02014-01-30 14:21:11 -0800832 */
Davidlohr Buesod36a7a02014-09-11 20:40:21 -0700833bool torture_cleanup_begin(void)
Paul E. McKenneycc47ae02014-01-30 14:21:11 -0800834{
835 mutex_lock(&fullstop_mutex);
Paul E. McKenney7d0ae802015-03-03 14:57:58 -0800836 if (READ_ONCE(fullstop) == FULLSTOP_SHUTDOWN) {
Paul E. McKenneycc47ae02014-01-30 14:21:11 -0800837 pr_warn("Concurrent rmmod and shutdown illegal!\n");
838 mutex_unlock(&fullstop_mutex);
839 schedule_timeout_uninterruptible(10);
840 return true;
841 }
Paul E. McKenney7d0ae802015-03-03 14:57:58 -0800842 WRITE_ONCE(fullstop, FULLSTOP_RMMOD);
Paul E. McKenneycc47ae02014-01-30 14:21:11 -0800843 mutex_unlock(&fullstop_mutex);
Paul E. McKenneybfefc732014-02-04 12:35:27 -0800844 torture_shutdown_cleanup();
Paul E. McKenneycc47ae02014-01-30 14:21:11 -0800845 torture_shuffle_cleanup();
Paul E. McKenneybfefc732014-02-04 12:35:27 -0800846 torture_stutter_cleanup();
Paul E. McKenneycc47ae02014-01-30 14:21:11 -0800847 torture_onoff_cleanup();
Davidlohr Buesod36a7a02014-09-11 20:40:21 -0700848 return false;
849}
850EXPORT_SYMBOL_GPL(torture_cleanup_begin);
851
852void torture_cleanup_end(void)
853{
Paul E. McKenney52280842014-04-07 09:14:11 -0700854 mutex_lock(&fullstop_mutex);
855 torture_type = NULL;
856 mutex_unlock(&fullstop_mutex);
Paul E. McKenneycc47ae02014-01-30 14:21:11 -0800857}
Davidlohr Buesod36a7a02014-09-11 20:40:21 -0700858EXPORT_SYMBOL_GPL(torture_cleanup_end);
Paul E. McKenney36970bb2014-01-30 15:49:29 -0800859
860/*
861 * Is it time for the current torture test to stop?
862 */
863bool torture_must_stop(void)
864{
865 return torture_must_stop_irq() || kthread_should_stop();
866}
867EXPORT_SYMBOL_GPL(torture_must_stop);
868
869/*
870 * Is it time for the current torture test to stop? This is the irq-safe
871 * version, hence no check for kthread_should_stop().
872 */
873bool torture_must_stop_irq(void)
874{
Paul E. McKenney7d0ae802015-03-03 14:57:58 -0800875 return READ_ONCE(fullstop) != FULLSTOP_DONTSTOP;
Paul E. McKenney36970bb2014-01-30 15:49:29 -0800876}
877EXPORT_SYMBOL_GPL(torture_must_stop_irq);
Paul E. McKenney7fafaac2014-01-31 17:37:28 -0800878
879/*
880 * Each kthread must wait for kthread_should_stop() before returning from
881 * its top-level function, otherwise segfaults ensue. This function
882 * prints a "stopping" message and waits for kthread_should_stop(), and
883 * should be called from all torture kthreads immediately prior to
884 * returning.
885 */
886void torture_kthread_stopping(char *title)
887{
Paul E. McKenney0d6821d2014-03-03 16:58:03 -0800888 char buf[128];
889
890 snprintf(buf, sizeof(buf), "Stopping %s", title);
891 VERBOSE_TOROUT_STRING(buf);
Paul E. McKenney7fafaac2014-01-31 17:37:28 -0800892 while (!kthread_should_stop()) {
893 torture_shutdown_absorb(title);
894 schedule_timeout_uninterruptible(1);
895 }
896}
897EXPORT_SYMBOL_GPL(torture_kthread_stopping);
Paul E. McKenney47cf29b2014-02-03 11:52:27 -0800898
899/*
900 * Create a generic torture kthread that is immediately runnable. If you
901 * need the kthread to be stopped so that you can do something to it before
902 * it starts, you will need to open-code your own.
903 */
904int _torture_create_kthread(int (*fn)(void *arg), void *arg, char *s, char *m,
905 char *f, struct task_struct **tp)
906{
907 int ret = 0;
908
909 VERBOSE_TOROUT_STRING(m);
Kees Cook69459152014-05-22 11:51:04 -0700910 *tp = kthread_run(fn, arg, "%s", s);
Paul E. McKenney47cf29b2014-02-03 11:52:27 -0800911 if (IS_ERR(*tp)) {
912 ret = PTR_ERR(*tp);
913 VERBOSE_TOROUT_ERRSTRING(f);
914 *tp = NULL;
915 }
916 torture_shuffle_task_register(*tp);
917 return ret;
918}
919EXPORT_SYMBOL_GPL(_torture_create_kthread);
Paul E. McKenney9c029b82014-02-04 11:47:08 -0800920
921/*
922 * Stop a generic kthread, emitting a message.
923 */
924void _torture_stop_kthread(char *m, struct task_struct **tp)
925{
926 if (*tp == NULL)
927 return;
928 VERBOSE_TOROUT_STRING(m);
929 kthread_stop(*tp);
930 *tp = NULL;
931}
932EXPORT_SYMBOL_GPL(_torture_stop_kthread);