rcutorture: Affinity forward-progress test to avoid housekeeping CPUs
[linux-2.6-block.git] / kernel / torture.c
CommitLineData
51b1130e
PM
1/*
2 * Common functions for in-kernel torture tests.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, you can access it online at
16 * http://www.gnu.org/licenses/gpl-2.0.html.
17 *
18 * Copyright (C) IBM Corporation, 2014
19 *
20 * Author: Paul E. McKenney <paulmck@us.ibm.com>
21 * Based on kernel/rcu/torture.c.
22 */
60500037
PM
23
24#define pr_fmt(fmt) fmt
25
51b1130e
PM
26#include <linux/types.h>
27#include <linux/kernel.h>
28#include <linux/init.h>
29#include <linux/module.h>
30#include <linux/kthread.h>
31#include <linux/err.h>
32#include <linux/spinlock.h>
33#include <linux/smp.h>
34#include <linux/interrupt.h>
35#include <linux/sched.h>
e6017571 36#include <linux/sched/clock.h>
51b1130e
PM
37#include <linux/atomic.h>
38#include <linux/bitops.h>
39#include <linux/completion.h>
40#include <linux/moduleparam.h>
41#include <linux/percpu.h>
42#include <linux/notifier.h>
43#include <linux/reboot.h>
44#include <linux/freezer.h>
45#include <linux/cpu.h>
46#include <linux/delay.h>
47#include <linux/stat.h>
48#include <linux/slab.h>
49#include <linux/trace_clock.h>
31257c3c 50#include <linux/ktime.h>
51b1130e
PM
51#include <asm/byteorder.h>
52#include <linux/torture.h>
dac95906 53#include "rcu/rcu.h"
51b1130e
PM
54
55MODULE_LICENSE("GPL");
56MODULE_AUTHOR("Paul E. McKenney <paulmck@us.ibm.com>");
57
b5daa8f3 58static char *torture_type;
90127d60 59static int verbose;
b5daa8f3 60
36970bb9
PM
61/* Mediate rmmod and system shutdown. Concurrent rmmod & shutdown illegal! */
62#define FULLSTOP_DONTSTOP 0 /* Normal operation. */
63#define FULLSTOP_SHUTDOWN 1 /* System shutdown with torture running. */
64#define FULLSTOP_RMMOD 2 /* Normal rmmod of torture. */
65static int fullstop = FULLSTOP_RMMOD;
4622b487 66static DEFINE_MUTEX(fullstop_mutex);
f67a3356 67
2e9e8081
PM
68#ifdef CONFIG_HOTPLUG_CPU
69
70/*
71 * Variables for online-offline handling. Only present if CPU hotplug
72 * is enabled, otherwise does nothing.
73 */
74
75static struct task_struct *onoff_task;
76static long onoff_holdoff;
77static long onoff_interval;
78static long n_offline_attempts;
79static long n_offline_successes;
80static unsigned long sum_offline;
81static int min_offline = -1;
82static int max_offline;
83static long n_online_attempts;
84static long n_online_successes;
85static unsigned long sum_online;
86static int min_online = -1;
87static int max_online;
88
d95f5ba9
PM
89/*
90 * Attempt to take a CPU offline. Return false if the CPU is already
91 * offline or if it is not subject to CPU-hotplug operations. The
92 * caller can detect other failures by looking at the statistics.
93 */
94bool torture_offline(int cpu, long *n_offl_attempts, long *n_offl_successes,
95 unsigned long *sum_offl, int *min_offl, int *max_offl)
96{
97 unsigned long delta;
98 int ret;
99 unsigned long starttime;
100
101 if (!cpu_online(cpu) || !cpu_is_hotpluggable(cpu))
102 return false;
103
90127d60 104 if (verbose > 1)
d95f5ba9
PM
105 pr_alert("%s" TORTURE_FLAG
106 "torture_onoff task: offlining %d\n",
107 torture_type, cpu);
108 starttime = jiffies;
109 (*n_offl_attempts)++;
110 ret = cpu_down(cpu);
111 if (ret) {
112 if (verbose)
113 pr_alert("%s" TORTURE_FLAG
114 "torture_onoff task: offline %d failed: errno %d\n",
115 torture_type, cpu, ret);
116 } else {
90127d60 117 if (verbose > 1)
d95f5ba9
PM
118 pr_alert("%s" TORTURE_FLAG
119 "torture_onoff task: offlined %d\n",
120 torture_type, cpu);
121 (*n_offl_successes)++;
122 delta = jiffies - starttime;
a2b2df20 123 *sum_offl += delta;
d95f5ba9
PM
124 if (*min_offl < 0) {
125 *min_offl = delta;
126 *max_offl = delta;
127 }
128 if (*min_offl > delta)
129 *min_offl = delta;
130 if (*max_offl < delta)
131 *max_offl = delta;
132 }
133
134 return true;
135}
136EXPORT_SYMBOL_GPL(torture_offline);
137
138/*
139 * Attempt to bring a CPU online. Return false if the CPU is already
140 * online or if it is not subject to CPU-hotplug operations. The
141 * caller can detect other failures by looking at the statistics.
142 */
143bool torture_online(int cpu, long *n_onl_attempts, long *n_onl_successes,
144 unsigned long *sum_onl, int *min_onl, int *max_onl)
145{
146 unsigned long delta;
147 int ret;
148 unsigned long starttime;
149
150 if (cpu_online(cpu) || !cpu_is_hotpluggable(cpu))
151 return false;
152
90127d60 153 if (verbose > 1)
d95f5ba9
PM
154 pr_alert("%s" TORTURE_FLAG
155 "torture_onoff task: onlining %d\n",
156 torture_type, cpu);
157 starttime = jiffies;
158 (*n_onl_attempts)++;
159 ret = cpu_up(cpu);
160 if (ret) {
161 if (verbose)
162 pr_alert("%s" TORTURE_FLAG
163 "torture_onoff task: online %d failed: errno %d\n",
164 torture_type, cpu, ret);
165 } else {
90127d60 166 if (verbose > 1)
d95f5ba9
PM
167 pr_alert("%s" TORTURE_FLAG
168 "torture_onoff task: onlined %d\n",
169 torture_type, cpu);
170 (*n_onl_successes)++;
171 delta = jiffies - starttime;
172 *sum_onl += delta;
173 if (*min_onl < 0) {
174 *min_onl = delta;
175 *max_onl = delta;
176 }
177 if (*min_onl > delta)
178 *min_onl = delta;
179 if (*max_onl < delta)
180 *max_onl = delta;
181 }
182
183 return true;
184}
185EXPORT_SYMBOL_GPL(torture_online);
186
2e9e8081
PM
187/*
188 * Execute random CPU-hotplug operations at the interval specified
189 * by the onoff_interval.
190 */
191static int
192torture_onoff(void *arg)
193{
194 int cpu;
2e9e8081
PM
195 int maxcpu = -1;
196 DEFINE_TORTURE_RANDOM(rand);
28cf5952 197 int ret;
2e9e8081
PM
198
199 VERBOSE_TOROUT_STRING("torture_onoff task started");
200 for_each_online_cpu(cpu)
201 maxcpu = cpu;
202 WARN_ON(maxcpu < 0);
28cf5952
PM
203 if (!IS_MODULE(CONFIG_TORTURE_TEST))
204 for_each_possible_cpu(cpu) {
205 if (cpu_online(cpu))
206 continue;
207 ret = cpu_up(cpu);
208 if (ret && verbose) {
209 pr_alert("%s" TORTURE_FLAG
210 "%s: Initial online %d: errno %d\n",
211 __func__, torture_type, cpu, ret);
212 }
213 }
750db0f5
BF
214
215 if (maxcpu == 0) {
216 VERBOSE_TOROUT_STRING("Only one CPU, so CPU-hotplug testing is disabled");
217 goto stop;
218 }
219
2e9e8081
PM
220 if (onoff_holdoff > 0) {
221 VERBOSE_TOROUT_STRING("torture_onoff begin holdoff");
222 schedule_timeout_interruptible(onoff_holdoff);
223 VERBOSE_TOROUT_STRING("torture_onoff end holdoff");
224 }
225 while (!torture_must_stop()) {
226 cpu = (torture_random(&rand) >> 4) % (maxcpu + 1);
d95f5ba9
PM
227 if (!torture_offline(cpu,
228 &n_offline_attempts, &n_offline_successes,
229 &sum_offline, &min_offline, &max_offline))
230 torture_online(cpu,
231 &n_online_attempts, &n_online_successes,
232 &sum_online, &min_online, &max_online);
2e9e8081
PM
233 schedule_timeout_interruptible(onoff_interval);
234 }
750db0f5
BF
235
236stop:
7fafaac5 237 torture_kthread_stopping("torture_onoff");
2e9e8081
PM
238 return 0;
239}
240
241#endif /* #ifdef CONFIG_HOTPLUG_CPU */
242
243/*
244 * Initiate online-offline handling.
245 */
246int torture_onoff_init(long ooholdoff, long oointerval)
247{
47cf29b9 248 int ret = 0;
2e9e8081 249
47cf29b9 250#ifdef CONFIG_HOTPLUG_CPU
2e9e8081
PM
251 onoff_holdoff = ooholdoff;
252 onoff_interval = oointerval;
253 if (onoff_interval <= 0)
254 return 0;
47cf29b9 255 ret = torture_create_kthread(torture_onoff, NULL, onoff_task);
2e9e8081 256#endif /* #ifdef CONFIG_HOTPLUG_CPU */
47cf29b9 257 return ret;
2e9e8081
PM
258}
259EXPORT_SYMBOL_GPL(torture_onoff_init);
260
261/*
262 * Clean up after online/offline testing.
263 */
cc47ae08 264static void torture_onoff_cleanup(void)
2e9e8081
PM
265{
266#ifdef CONFIG_HOTPLUG_CPU
267 if (onoff_task == NULL)
268 return;
269 VERBOSE_TOROUT_STRING("Stopping torture_onoff task");
270 kthread_stop(onoff_task);
271 onoff_task = NULL;
272#endif /* #ifdef CONFIG_HOTPLUG_CPU */
273}
274EXPORT_SYMBOL_GPL(torture_onoff_cleanup);
275
276/*
277 * Print online/offline testing statistics.
278 */
eea203fe 279void torture_onoff_stats(void)
2e9e8081
PM
280{
281#ifdef CONFIG_HOTPLUG_CPU
eea203fe
JP
282 pr_cont("onoff: %ld/%ld:%ld/%ld %d,%d:%d,%d %lu:%lu (HZ=%d) ",
283 n_online_successes, n_online_attempts,
284 n_offline_successes, n_offline_attempts,
285 min_online, max_online,
286 min_offline, max_offline,
287 sum_online, sum_offline, HZ);
2e9e8081 288#endif /* #ifdef CONFIG_HOTPLUG_CPU */
2e9e8081
PM
289}
290EXPORT_SYMBOL_GPL(torture_onoff_stats);
291
292/*
293 * Were all the online/offline operations successful?
294 */
295bool torture_onoff_failures(void)
296{
297#ifdef CONFIG_HOTPLUG_CPU
298 return n_online_successes != n_online_attempts ||
299 n_offline_successes != n_offline_attempts;
300#else /* #ifdef CONFIG_HOTPLUG_CPU */
301 return false;
302#endif /* #else #ifdef CONFIG_HOTPLUG_CPU */
303}
304EXPORT_SYMBOL_GPL(torture_onoff_failures);
305
51b1130e
PM
306#define TORTURE_RANDOM_MULT 39916801 /* prime */
307#define TORTURE_RANDOM_ADD 479001701 /* prime */
308#define TORTURE_RANDOM_REFRESH 10000
309
310/*
311 * Crude but fast random-number generator. Uses a linear congruential
312 * generator, with occasional help from cpu_clock().
313 */
314unsigned long
315torture_random(struct torture_random_state *trsp)
316{
317 if (--trsp->trs_count < 0) {
318 trsp->trs_state += (unsigned long)local_clock();
319 trsp->trs_count = TORTURE_RANDOM_REFRESH;
320 }
321 trsp->trs_state = trsp->trs_state * TORTURE_RANDOM_MULT +
322 TORTURE_RANDOM_ADD;
323 return swahw32(trsp->trs_state);
324}
325EXPORT_SYMBOL_GPL(torture_random);
f67a3356 326
3808dc9f
PM
327/*
328 * Variables for shuffling. The idea is to ensure that each CPU stays
329 * idle for an extended period to test interactions with dyntick idle,
b564d62e 330 * as well as interactions with any per-CPU variables.
3808dc9f
PM
331 */
332struct shuffle_task {
333 struct list_head st_l;
334 struct task_struct *st_t;
335};
336
337static long shuffle_interval; /* In jiffies. */
338static struct task_struct *shuffler_task;
339static cpumask_var_t shuffle_tmp_mask;
340static int shuffle_idle_cpu; /* Force all torture tasks off this CPU */
341static struct list_head shuffle_task_list = LIST_HEAD_INIT(shuffle_task_list);
342static DEFINE_MUTEX(shuffle_task_mutex);
343
344/*
345 * Register a task to be shuffled. If there is no memory, just splat
346 * and don't bother registering.
347 */
348void torture_shuffle_task_register(struct task_struct *tp)
349{
350 struct shuffle_task *stp;
351
352 if (WARN_ON_ONCE(tp == NULL))
353 return;
354 stp = kmalloc(sizeof(*stp), GFP_KERNEL);
355 if (WARN_ON_ONCE(stp == NULL))
356 return;
357 stp->st_t = tp;
358 mutex_lock(&shuffle_task_mutex);
359 list_add(&stp->st_l, &shuffle_task_list);
360 mutex_unlock(&shuffle_task_mutex);
361}
362EXPORT_SYMBOL_GPL(torture_shuffle_task_register);
363
364/*
365 * Unregister all tasks, for example, at the end of the torture run.
366 */
367static void torture_shuffle_task_unregister_all(void)
368{
369 struct shuffle_task *stp;
370 struct shuffle_task *p;
371
372 mutex_lock(&shuffle_task_mutex);
373 list_for_each_entry_safe(stp, p, &shuffle_task_list, st_l) {
374 list_del(&stp->st_l);
375 kfree(stp);
376 }
377 mutex_unlock(&shuffle_task_mutex);
378}
379
380/* Shuffle tasks such that we allow shuffle_idle_cpu to become idle.
381 * A special case is when shuffle_idle_cpu = -1, in which case we allow
382 * the tasks to run on all CPUs.
383 */
384static void torture_shuffle_tasks(void)
385{
386 struct shuffle_task *stp;
387
388 cpumask_setall(shuffle_tmp_mask);
389 get_online_cpus();
390
391 /* No point in shuffling if there is only one online CPU (ex: UP) */
392 if (num_online_cpus() == 1) {
393 put_online_cpus();
394 return;
395 }
396
397 /* Advance to the next CPU. Upon overflow, don't idle any CPUs. */
398 shuffle_idle_cpu = cpumask_next(shuffle_idle_cpu, shuffle_tmp_mask);
399 if (shuffle_idle_cpu >= nr_cpu_ids)
400 shuffle_idle_cpu = -1;
5ed63b19 401 else
3808dc9f 402 cpumask_clear_cpu(shuffle_idle_cpu, shuffle_tmp_mask);
3808dc9f
PM
403
404 mutex_lock(&shuffle_task_mutex);
405 list_for_each_entry(stp, &shuffle_task_list, st_l)
406 set_cpus_allowed_ptr(stp->st_t, shuffle_tmp_mask);
407 mutex_unlock(&shuffle_task_mutex);
408
409 put_online_cpus();
410}
411
412/* Shuffle tasks across CPUs, with the intent of allowing each CPU in the
413 * system to become idle at a time and cut off its timer ticks. This is meant
414 * to test the support for such tickless idle CPU in RCU.
415 */
416static int torture_shuffle(void *arg)
417{
418 VERBOSE_TOROUT_STRING("torture_shuffle task started");
419 do {
420 schedule_timeout_interruptible(shuffle_interval);
421 torture_shuffle_tasks();
422 torture_shutdown_absorb("torture_shuffle");
423 } while (!torture_must_stop());
7fafaac5 424 torture_kthread_stopping("torture_shuffle");
3808dc9f
PM
425 return 0;
426}
427
428/*
429 * Start the shuffler, with shuffint in jiffies.
430 */
431int torture_shuffle_init(long shuffint)
432{
3808dc9f
PM
433 shuffle_interval = shuffint;
434
435 shuffle_idle_cpu = -1;
436
437 if (!alloc_cpumask_var(&shuffle_tmp_mask, GFP_KERNEL)) {
438 VERBOSE_TOROUT_ERRSTRING("Failed to alloc mask");
439 return -ENOMEM;
440 }
441
442 /* Create the shuffler thread */
47cf29b9 443 return torture_create_kthread(torture_shuffle, NULL, shuffler_task);
3808dc9f
PM
444}
445EXPORT_SYMBOL_GPL(torture_shuffle_init);
446
447/*
448 * Stop the shuffling.
449 */
cc47ae08 450static void torture_shuffle_cleanup(void)
3808dc9f
PM
451{
452 torture_shuffle_task_unregister_all();
453 if (shuffler_task) {
454 VERBOSE_TOROUT_STRING("Stopping torture_shuffle task");
455 kthread_stop(shuffler_task);
456 free_cpumask_var(shuffle_tmp_mask);
457 }
458 shuffler_task = NULL;
459}
460EXPORT_SYMBOL_GPL(torture_shuffle_cleanup);
461
e991dbc0
PM
462/*
463 * Variables for auto-shutdown. This allows "lights out" torture runs
464 * to be fully scripted.
465 */
e991dbc0 466static struct task_struct *shutdown_task;
31257c3c 467static ktime_t shutdown_time; /* time to system shutdown. */
e991dbc0
PM
468static void (*torture_shutdown_hook)(void);
469
f67a3356
PM
470/*
471 * Absorb kthreads into a kernel function that won't return, so that
472 * they won't ever access module text or data again.
473 */
474void torture_shutdown_absorb(const char *title)
475{
7d0ae808 476 while (READ_ONCE(fullstop) == FULLSTOP_SHUTDOWN) {
4622b487
PM
477 pr_notice("torture thread %s parking due to system shutdown\n",
478 title);
f67a3356
PM
479 schedule_timeout_uninterruptible(MAX_SCHEDULE_TIMEOUT);
480 }
481}
482EXPORT_SYMBOL_GPL(torture_shutdown_absorb);
b5daa8f3 483
e991dbc0
PM
484/*
485 * Cause the torture test to shutdown the system after the test has
486 * run for the time specified by the shutdown_secs parameter.
487 */
488static int torture_shutdown(void *arg)
489{
31257c3c 490 ktime_t ktime_snap;
e991dbc0
PM
491
492 VERBOSE_TOROUT_STRING("torture_shutdown task started");
31257c3c
PM
493 ktime_snap = ktime_get();
494 while (ktime_before(ktime_snap, shutdown_time) &&
e991dbc0 495 !torture_must_stop()) {
e991dbc0
PM
496 if (verbose)
497 pr_alert("%s" TORTURE_FLAG
31257c3c
PM
498 "torture_shutdown task: %llu ms remaining\n",
499 torture_type,
500 ktime_ms_delta(shutdown_time, ktime_snap));
501 set_current_state(TASK_INTERRUPTIBLE);
502 schedule_hrtimeout(&shutdown_time, HRTIMER_MODE_ABS);
503 ktime_snap = ktime_get();
e991dbc0
PM
504 }
505 if (torture_must_stop()) {
7fafaac5 506 torture_kthread_stopping("torture_shutdown");
e991dbc0
PM
507 return 0;
508 }
509
510 /* OK, shut down the system. */
511
512 VERBOSE_TOROUT_STRING("torture_shutdown task shutting down system");
513 shutdown_task = NULL; /* Avoid self-kill deadlock. */
f881825a
PM
514 if (torture_shutdown_hook)
515 torture_shutdown_hook();
516 else
517 VERBOSE_TOROUT_STRING("No torture_shutdown_hook(), skipping.");
dac95906 518 rcu_ftrace_dump(DUMP_ALL);
e991dbc0
PM
519 kernel_power_off(); /* Shut down the system. */
520 return 0;
521}
522
523/*
524 * Start up the shutdown task.
525 */
526int torture_shutdown_init(int ssecs, void (*cleanup)(void))
527{
47cf29b9 528 int ret = 0;
e991dbc0 529
e991dbc0 530 torture_shutdown_hook = cleanup;
31257c3c
PM
531 if (ssecs > 0) {
532 shutdown_time = ktime_add(ktime_get(), ktime_set(ssecs, 0));
47cf29b9
PM
533 ret = torture_create_kthread(torture_shutdown, NULL,
534 shutdown_task);
e991dbc0 535 }
47cf29b9 536 return ret;
e991dbc0
PM
537}
538EXPORT_SYMBOL_GPL(torture_shutdown_init);
539
4622b487
PM
540/*
541 * Detect and respond to a system shutdown.
542 */
543static int torture_shutdown_notify(struct notifier_block *unused1,
544 unsigned long unused2, void *unused3)
545{
546 mutex_lock(&fullstop_mutex);
7d0ae808 547 if (READ_ONCE(fullstop) == FULLSTOP_DONTSTOP) {
fac480ef 548 VERBOSE_TOROUT_STRING("Unscheduled system shutdown detected");
7d0ae808 549 WRITE_ONCE(fullstop, FULLSTOP_SHUTDOWN);
fac480ef 550 } else {
4622b487 551 pr_warn("Concurrent rmmod and shutdown illegal!\n");
fac480ef 552 }
4622b487
PM
553 mutex_unlock(&fullstop_mutex);
554 return NOTIFY_DONE;
555}
556
557static struct notifier_block torture_shutdown_nb = {
558 .notifier_call = torture_shutdown_notify,
559};
560
bfefc73a
PM
561/*
562 * Shut down the shutdown task. Say what??? Heh! This can happen if
563 * the torture module gets an rmmod before the shutdown time arrives. ;-)
564 */
565static void torture_shutdown_cleanup(void)
566{
567 unregister_reboot_notifier(&torture_shutdown_nb);
568 if (shutdown_task != NULL) {
569 VERBOSE_TOROUT_STRING("Stopping torture_shutdown task");
570 kthread_stop(shutdown_task);
571 }
572 shutdown_task = NULL;
573}
574
628edaa5
PM
575/*
576 * Variables for stuttering, which means to periodically pause and
577 * restart testing in order to catch bugs that appear when load is
578 * suddenly applied to or removed from the system.
579 */
580static struct task_struct *stutter_task;
581static int stutter_pause_test;
582static int stutter;
583
584/*
585 * Block until the stutter interval ends. This must be called periodically
586 * by all running kthreads that need to be subject to stuttering.
587 */
474e59b4 588bool stutter_wait(const char *title)
628edaa5 589{
4ced3314
PM
590 int spt;
591
cee43939 592 cond_resched_tasks_rcu_qs();
4ced3314 593 spt = READ_ONCE(stutter_pause_test);
29d39390 594 for (; spt; spt = READ_ONCE(stutter_pause_test)) {
4ced3314
PM
595 if (spt == 1) {
596 schedule_timeout_interruptible(1);
597 } else if (spt == 2) {
598 while (READ_ONCE(stutter_pause_test))
599 cond_resched();
600 } else {
628edaa5 601 schedule_timeout_interruptible(round_jiffies_relative(HZ));
4ced3314 602 }
628edaa5
PM
603 torture_shutdown_absorb(title);
604 }
474e59b4 605 return !!spt;
628edaa5
PM
606}
607EXPORT_SYMBOL_GPL(stutter_wait);
608
609/*
610 * Cause the torture test to "stutter", starting and stopping all
611 * threads periodically.
612 */
613static int torture_stutter(void *arg)
614{
615 VERBOSE_TOROUT_STRING("torture_stutter task started");
616 do {
4ced3314 617 if (!torture_must_stop() && stutter > 1) {
7d0ae808 618 WRITE_ONCE(stutter_pause_test, 1);
4ced3314
PM
619 schedule_timeout_interruptible(stutter - 1);
620 WRITE_ONCE(stutter_pause_test, 2);
621 schedule_timeout_interruptible(1);
628edaa5 622 }
4ced3314 623 WRITE_ONCE(stutter_pause_test, 0);
628edaa5
PM
624 if (!torture_must_stop())
625 schedule_timeout_interruptible(stutter);
628edaa5
PM
626 torture_shutdown_absorb("torture_stutter");
627 } while (!torture_must_stop());
7fafaac5 628 torture_kthread_stopping("torture_stutter");
628edaa5
PM
629 return 0;
630}
631
632/*
633 * Initialize and kick off the torture_stutter kthread.
634 */
635int torture_stutter_init(int s)
636{
637 int ret;
638
639 stutter = s;
47cf29b9
PM
640 ret = torture_create_kthread(torture_stutter, NULL, stutter_task);
641 return ret;
628edaa5
PM
642}
643EXPORT_SYMBOL_GPL(torture_stutter_init);
644
645/*
646 * Cleanup after the torture_stutter kthread.
647 */
bfefc73a 648static void torture_stutter_cleanup(void)
628edaa5
PM
649{
650 if (!stutter_task)
651 return;
652 VERBOSE_TOROUT_STRING("Stopping torture_stutter task");
653 kthread_stop(stutter_task);
654 stutter_task = NULL;
655}
628edaa5 656
b5daa8f3
PM
657/*
658 * Initialize torture module. Please note that this is -not- invoked via
659 * the usual module_init() mechanism, but rather by an explicit call from
660 * the client torture module. This call must be paired with a later
661 * torture_init_end().
628edaa5
PM
662 *
663 * The runnable parameter points to a flag that controls whether or not
664 * the test is currently runnable. If there is no such flag, pass in NULL.
b5daa8f3 665 */
90127d60 666bool torture_init_begin(char *ttype, int v)
b5daa8f3
PM
667{
668 mutex_lock(&fullstop_mutex);
5228084e 669 if (torture_type != NULL) {
9eb5188a 670 pr_alert("torture_init_begin: Refusing %s init: %s running.\n",
5228084e 671 ttype, torture_type);
9eb5188a 672 pr_alert("torture_init_begin: One torture test at a time!\n");
5228084e
PM
673 mutex_unlock(&fullstop_mutex);
674 return false;
675 }
b5daa8f3
PM
676 torture_type = ttype;
677 verbose = v;
36970bb9 678 fullstop = FULLSTOP_DONTSTOP;
5228084e 679 return true;
b5daa8f3
PM
680}
681EXPORT_SYMBOL_GPL(torture_init_begin);
682
683/*
684 * Tell the torture module that initialization is complete.
685 */
2b3f8ffe 686void torture_init_end(void)
b5daa8f3
PM
687{
688 mutex_unlock(&fullstop_mutex);
4622b487 689 register_reboot_notifier(&torture_shutdown_nb);
b5daa8f3
PM
690}
691EXPORT_SYMBOL_GPL(torture_init_end);
cc47ae08
PM
692
693/*
694 * Clean up torture module. Please note that this is -not- invoked via
695 * the usual module_exit() mechanism, but rather by an explicit call from
696 * the client torture module. Returns true if a race with system shutdown
bfefc73a
PM
697 * is detected, otherwise, all kthreads started by functions in this file
698 * will be shut down.
cc47ae08
PM
699 *
700 * This must be called before the caller starts shutting down its own
701 * kthreads.
d36a7a0d
DB
702 *
703 * Both torture_cleanup_begin() and torture_cleanup_end() must be paired,
704 * in order to correctly perform the cleanup. They are separated because
705 * threads can still need to reference the torture_type type, thus nullify
706 * only after completing all other relevant calls.
cc47ae08 707 */
d36a7a0d 708bool torture_cleanup_begin(void)
cc47ae08
PM
709{
710 mutex_lock(&fullstop_mutex);
7d0ae808 711 if (READ_ONCE(fullstop) == FULLSTOP_SHUTDOWN) {
cc47ae08
PM
712 pr_warn("Concurrent rmmod and shutdown illegal!\n");
713 mutex_unlock(&fullstop_mutex);
714 schedule_timeout_uninterruptible(10);
715 return true;
716 }
7d0ae808 717 WRITE_ONCE(fullstop, FULLSTOP_RMMOD);
cc47ae08 718 mutex_unlock(&fullstop_mutex);
bfefc73a 719 torture_shutdown_cleanup();
cc47ae08 720 torture_shuffle_cleanup();
bfefc73a 721 torture_stutter_cleanup();
cc47ae08 722 torture_onoff_cleanup();
d36a7a0d
DB
723 return false;
724}
725EXPORT_SYMBOL_GPL(torture_cleanup_begin);
726
727void torture_cleanup_end(void)
728{
5228084e
PM
729 mutex_lock(&fullstop_mutex);
730 torture_type = NULL;
731 mutex_unlock(&fullstop_mutex);
cc47ae08 732}
d36a7a0d 733EXPORT_SYMBOL_GPL(torture_cleanup_end);
36970bb9
PM
734
735/*
736 * Is it time for the current torture test to stop?
737 */
738bool torture_must_stop(void)
739{
740 return torture_must_stop_irq() || kthread_should_stop();
741}
742EXPORT_SYMBOL_GPL(torture_must_stop);
743
744/*
745 * Is it time for the current torture test to stop? This is the irq-safe
746 * version, hence no check for kthread_should_stop().
747 */
748bool torture_must_stop_irq(void)
749{
7d0ae808 750 return READ_ONCE(fullstop) != FULLSTOP_DONTSTOP;
36970bb9
PM
751}
752EXPORT_SYMBOL_GPL(torture_must_stop_irq);
7fafaac5
PM
753
754/*
755 * Each kthread must wait for kthread_should_stop() before returning from
756 * its top-level function, otherwise segfaults ensue. This function
757 * prints a "stopping" message and waits for kthread_should_stop(), and
758 * should be called from all torture kthreads immediately prior to
759 * returning.
760 */
761void torture_kthread_stopping(char *title)
762{
0d6821d5
PM
763 char buf[128];
764
765 snprintf(buf, sizeof(buf), "Stopping %s", title);
766 VERBOSE_TOROUT_STRING(buf);
7fafaac5
PM
767 while (!kthread_should_stop()) {
768 torture_shutdown_absorb(title);
769 schedule_timeout_uninterruptible(1);
770 }
771}
772EXPORT_SYMBOL_GPL(torture_kthread_stopping);
47cf29b9
PM
773
774/*
775 * Create a generic torture kthread that is immediately runnable. If you
776 * need the kthread to be stopped so that you can do something to it before
777 * it starts, you will need to open-code your own.
778 */
779int _torture_create_kthread(int (*fn)(void *arg), void *arg, char *s, char *m,
780 char *f, struct task_struct **tp)
781{
782 int ret = 0;
783
784 VERBOSE_TOROUT_STRING(m);
6945915e 785 *tp = kthread_run(fn, arg, "%s", s);
47cf29b9
PM
786 if (IS_ERR(*tp)) {
787 ret = PTR_ERR(*tp);
788 VERBOSE_TOROUT_ERRSTRING(f);
789 *tp = NULL;
790 }
791 torture_shuffle_task_register(*tp);
792 return ret;
793}
794EXPORT_SYMBOL_GPL(_torture_create_kthread);
9c029b86
PM
795
796/*
797 * Stop a generic kthread, emitting a message.
798 */
799void _torture_stop_kthread(char *m, struct task_struct **tp)
800{
801 if (*tp == NULL)
802 return;
803 VERBOSE_TOROUT_STRING(m);
804 kthread_stop(*tp);
805 *tp = NULL;
806}
807EXPORT_SYMBOL_GPL(_torture_stop_kthread);