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