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