blob: 52781e838541aa655552dcc1f311d1b84b54011b [file] [log] [blame]
Paul E. McKenney51b11302014-01-27 11:49:39 -08001/*
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>
Ingo Molnare6017572017-02-01 16:36:40 +010033#include <linux/sched/clock.h>
Paul E. McKenney51b11302014-01-27 11:49:39 -080034#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>
Paul E. McKenney31257c32016-06-18 07:45:43 -070047#include <linux/ktime.h>
Paul E. McKenney51b11302014-01-27 11:49:39 -080048#include <asm/byteorder.h>
49#include <linux/torture.h>
Paul E. McKenneydac95902017-10-04 11:23:10 -070050#include "rcu/rcu.h"
Paul E. McKenney51b11302014-01-27 11:49:39 -080051
52MODULE_LICENSE("GPL");
53MODULE_AUTHOR("Paul E. McKenney <paulmck@us.ibm.com>");
54
Paul E. McKenneyb5daa8f2014-01-30 13:38:09 -080055static char *torture_type;
56static bool verbose;
57
Paul E. McKenney36970bb2014-01-30 15:49:29 -080058/* 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;
Paul E. McKenney4622b482014-01-30 15:37:19 -080063static DEFINE_MUTEX(fullstop_mutex);
Paul E. McKenney628edaa2014-01-31 11:57:43 -080064static int *torture_runnable;
Paul E. McKenneyf67a3352014-01-29 07:40:27 -080065
Paul E. McKenney2e9e8082014-01-28 15:58:22 -080066#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
87/*
Paul E. McKenneyd95f5ba2016-04-20 17:18:41 -070088 * 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;
Paul E. McKenneya2b2df22017-06-22 15:38:26 -0700121 *sum_offl += delta;
Paul E. McKenneyd95f5ba2016-04-20 17:18:41 -0700122 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
185/*
Paul E. McKenney2e9e8082014-01-28 15:58:22 -0800186 * 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;
Paul E. McKenney2e9e8082014-01-28 15:58:22 -0800193 int maxcpu = -1;
194 DEFINE_TORTURE_RANDOM(rand);
Paul E. McKenney2e9e8082014-01-28 15:58:22 -0800195
196 VERBOSE_TOROUT_STRING("torture_onoff task started");
197 for_each_online_cpu(cpu)
198 maxcpu = cpu;
199 WARN_ON(maxcpu < 0);
Boqun Feng750db0f2016-05-02 10:30:00 +0800200
201 if (maxcpu == 0) {
202 VERBOSE_TOROUT_STRING("Only one CPU, so CPU-hotplug testing is disabled");
203 goto stop;
204 }
205
Paul E. McKenney2e9e8082014-01-28 15:58:22 -0800206 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);
Paul E. McKenneyd95f5ba2016-04-20 17:18:41 -0700213 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);
Paul E. McKenney2e9e8082014-01-28 15:58:22 -0800219 schedule_timeout_interruptible(onoff_interval);
220 }
Boqun Feng750db0f2016-05-02 10:30:00 +0800221
222stop:
Paul E. McKenney7fafaac2014-01-31 17:37:28 -0800223 torture_kthread_stopping("torture_onoff");
Paul E. McKenney2e9e8082014-01-28 15:58:22 -0800224 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{
Paul E. McKenney47cf29b2014-02-03 11:52:27 -0800234 int ret = 0;
Paul E. McKenney2e9e8082014-01-28 15:58:22 -0800235
Paul E. McKenney47cf29b2014-02-03 11:52:27 -0800236#ifdef CONFIG_HOTPLUG_CPU
Paul E. McKenney2e9e8082014-01-28 15:58:22 -0800237 onoff_holdoff = ooholdoff;
238 onoff_interval = oointerval;
239 if (onoff_interval <= 0)
240 return 0;
Paul E. McKenney47cf29b2014-02-03 11:52:27 -0800241 ret = torture_create_kthread(torture_onoff, NULL, onoff_task);
Paul E. McKenney2e9e8082014-01-28 15:58:22 -0800242#endif /* #ifdef CONFIG_HOTPLUG_CPU */
Paul E. McKenney47cf29b2014-02-03 11:52:27 -0800243 return ret;
Paul E. McKenney2e9e8082014-01-28 15:58:22 -0800244}
245EXPORT_SYMBOL_GPL(torture_onoff_init);
246
247/*
248 * Clean up after online/offline testing.
249 */
Paul E. McKenneycc47ae02014-01-30 14:21:11 -0800250static void torture_onoff_cleanup(void)
Paul E. McKenney2e9e8082014-01-28 15:58:22 -0800251{
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 */
Joe Percheseea203f2014-07-14 09:16:15 -0400265void torture_onoff_stats(void)
Paul E. McKenney2e9e8082014-01-28 15:58:22 -0800266{
267#ifdef CONFIG_HOTPLUG_CPU
Joe Percheseea203f2014-07-14 09:16:15 -0400268 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);
Paul E. McKenney2e9e8082014-01-28 15:58:22 -0800274#endif /* #ifdef CONFIG_HOTPLUG_CPU */
Paul E. McKenney2e9e8082014-01-28 15:58:22 -0800275}
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
Paul E. McKenney51b11302014-01-27 11:49:39 -0800292#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);
Paul E. McKenneyf67a3352014-01-29 07:40:27 -0800312
313/*
Paul E. McKenney3808dc92014-01-28 15:29:21 -0800314 * Variables for shuffling. The idea is to ensure that each CPU stays
315 * idle for an extended period to test interactions with dyntick idle,
Masahiro Yamadab564d622017-02-27 14:29:06 -0800316 * as well as interactions with any per-CPU variables.
Paul E. McKenney3808dc92014-01-28 15:29:21 -0800317 */
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;
Iulia Manda5ed63b12014-03-17 15:21:21 +0200387 else
Paul E. McKenney3808dc92014-01-28 15:29:21 -0800388 cpumask_clear_cpu(shuffle_idle_cpu, shuffle_tmp_mask);
Paul E. McKenney3808dc92014-01-28 15:29:21 -0800389
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());
Paul E. McKenney7fafaac2014-01-31 17:37:28 -0800410 torture_kthread_stopping("torture_shuffle");
Paul E. McKenney3808dc92014-01-28 15:29:21 -0800411 return 0;
412}
413
414/*
415 * Start the shuffler, with shuffint in jiffies.
416 */
417int torture_shuffle_init(long shuffint)
418{
Paul E. McKenney3808dc92014-01-28 15:29:21 -0800419 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 */
Paul E. McKenney47cf29b2014-02-03 11:52:27 -0800429 return torture_create_kthread(torture_shuffle, NULL, shuffler_task);
Paul E. McKenney3808dc92014-01-28 15:29:21 -0800430}
431EXPORT_SYMBOL_GPL(torture_shuffle_init);
432
433/*
434 * Stop the shuffling.
435 */
Paul E. McKenneycc47ae02014-01-30 14:21:11 -0800436static void torture_shuffle_cleanup(void)
Paul E. McKenney3808dc92014-01-28 15:29:21 -0800437{
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
448/*
Paul E. McKenneye991dbc2014-01-31 14:52:13 -0800449 * Variables for auto-shutdown. This allows "lights out" torture runs
450 * to be fully scripted.
451 */
Paul E. McKenneye991dbc2014-01-31 14:52:13 -0800452static struct task_struct *shutdown_task;
Paul E. McKenney31257c32016-06-18 07:45:43 -0700453static ktime_t shutdown_time; /* time to system shutdown. */
Paul E. McKenneye991dbc2014-01-31 14:52:13 -0800454static void (*torture_shutdown_hook)(void);
455
456/*
Paul E. McKenneyf67a3352014-01-29 07:40:27 -0800457 * 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{
Paul E. McKenney7d0ae802015-03-03 14:57:58 -0800462 while (READ_ONCE(fullstop) == FULLSTOP_SHUTDOWN) {
Paul E. McKenney4622b482014-01-30 15:37:19 -0800463 pr_notice("torture thread %s parking due to system shutdown\n",
464 title);
Paul E. McKenneyf67a3352014-01-29 07:40:27 -0800465 schedule_timeout_uninterruptible(MAX_SCHEDULE_TIMEOUT);
466 }
467}
468EXPORT_SYMBOL_GPL(torture_shutdown_absorb);
Paul E. McKenneyb5daa8f2014-01-30 13:38:09 -0800469
470/*
Paul E. McKenneye991dbc2014-01-31 14:52:13 -0800471 * 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{
Paul E. McKenney31257c32016-06-18 07:45:43 -0700476 ktime_t ktime_snap;
Paul E. McKenneye991dbc2014-01-31 14:52:13 -0800477
478 VERBOSE_TOROUT_STRING("torture_shutdown task started");
Paul E. McKenney31257c32016-06-18 07:45:43 -0700479 ktime_snap = ktime_get();
480 while (ktime_before(ktime_snap, shutdown_time) &&
Paul E. McKenneye991dbc2014-01-31 14:52:13 -0800481 !torture_must_stop()) {
Paul E. McKenneye991dbc2014-01-31 14:52:13 -0800482 if (verbose)
483 pr_alert("%s" TORTURE_FLAG
Paul E. McKenney31257c32016-06-18 07:45:43 -0700484 "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();
Paul E. McKenneye991dbc2014-01-31 14:52:13 -0800490 }
491 if (torture_must_stop()) {
Paul E. McKenney7fafaac2014-01-31 17:37:28 -0800492 torture_kthread_stopping("torture_shutdown");
Paul E. McKenneye991dbc2014-01-31 14:52:13 -0800493 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. */
Paul E. McKenneyf8818252014-02-07 14:42:51 -0800500 if (torture_shutdown_hook)
501 torture_shutdown_hook();
502 else
503 VERBOSE_TOROUT_STRING("No torture_shutdown_hook(), skipping.");
Paul E. McKenneydac95902017-10-04 11:23:10 -0700504 rcu_ftrace_dump(DUMP_ALL);
Paul E. McKenneye991dbc2014-01-31 14:52:13 -0800505 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{
Paul E. McKenney47cf29b2014-02-03 11:52:27 -0800514 int ret = 0;
Paul E. McKenneye991dbc2014-01-31 14:52:13 -0800515
Paul E. McKenneye991dbc2014-01-31 14:52:13 -0800516 torture_shutdown_hook = cleanup;
Paul E. McKenney31257c32016-06-18 07:45:43 -0700517 if (ssecs > 0) {
518 shutdown_time = ktime_add(ktime_get(), ktime_set(ssecs, 0));
Paul E. McKenney47cf29b2014-02-03 11:52:27 -0800519 ret = torture_create_kthread(torture_shutdown, NULL,
520 shutdown_task);
Paul E. McKenneye991dbc2014-01-31 14:52:13 -0800521 }
Paul E. McKenney47cf29b2014-02-03 11:52:27 -0800522 return ret;
Paul E. McKenneye991dbc2014-01-31 14:52:13 -0800523}
524EXPORT_SYMBOL_GPL(torture_shutdown_init);
525
526/*
Paul E. McKenney4622b482014-01-30 15:37:19 -0800527 * 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);
Paul E. McKenney7d0ae802015-03-03 14:57:58 -0800533 if (READ_ONCE(fullstop) == FULLSTOP_DONTSTOP) {
Paul E. McKenneyfac480e2014-01-30 17:06:30 -0800534 VERBOSE_TOROUT_STRING("Unscheduled system shutdown detected");
Paul E. McKenney7d0ae802015-03-03 14:57:58 -0800535 WRITE_ONCE(fullstop, FULLSTOP_SHUTDOWN);
Paul E. McKenneyfac480e2014-01-30 17:06:30 -0800536 } else {
Paul E. McKenney4622b482014-01-30 15:37:19 -0800537 pr_warn("Concurrent rmmod and shutdown illegal!\n");
Paul E. McKenneyfac480e2014-01-30 17:06:30 -0800538 }
Paul E. McKenney4622b482014-01-30 15:37:19 -0800539 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
547/*
Paul E. McKenneybfefc732014-02-04 12:35:27 -0800548 * 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
561/*
Paul E. McKenney628edaa2014-01-31 11:57:43 -0800562 * 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{
Paul E. McKenney3836f532015-08-30 03:29:58 -0700576 cond_resched_rcu_qs();
Paul E. McKenney7d0ae802015-03-03 14:57:58 -0800577 while (READ_ONCE(stutter_pause_test) ||
578 (torture_runnable && !READ_ONCE(*torture_runnable))) {
Paul E. McKenney628edaa2014-01-31 11:57:43 -0800579 if (stutter_pause_test)
Paul E. McKenney7d0ae802015-03-03 14:57:58 -0800580 if (READ_ONCE(stutter_pause_test) == 1)
Paul E. McKenneyab7d45052014-03-04 11:03:21 -0800581 schedule_timeout_interruptible(1);
582 else
Paul E. McKenney7d0ae802015-03-03 14:57:58 -0800583 while (READ_ONCE(stutter_pause_test))
Paul E. McKenneyab7d45052014-03-04 11:03:21 -0800584 cond_resched();
Paul E. McKenney628edaa2014-01-31 11:57:43 -0800585 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()) {
Paul E. McKenneyab7d45052014-03-04 11:03:21 -0800601 if (stutter > 1) {
602 schedule_timeout_interruptible(stutter - 1);
Paul E. McKenney7d0ae802015-03-03 14:57:58 -0800603 WRITE_ONCE(stutter_pause_test, 2);
Paul E. McKenneyab7d45052014-03-04 11:03:21 -0800604 }
605 schedule_timeout_interruptible(1);
Paul E. McKenney7d0ae802015-03-03 14:57:58 -0800606 WRITE_ONCE(stutter_pause_test, 1);
Paul E. McKenney628edaa2014-01-31 11:57:43 -0800607 }
608 if (!torture_must_stop())
609 schedule_timeout_interruptible(stutter);
Paul E. McKenney7d0ae802015-03-03 14:57:58 -0800610 WRITE_ONCE(stutter_pause_test, 0);
Paul E. McKenney628edaa2014-01-31 11:57:43 -0800611 torture_shutdown_absorb("torture_stutter");
612 } while (!torture_must_stop());
Paul E. McKenney7fafaac2014-01-31 17:37:28 -0800613 torture_kthread_stopping("torture_stutter");
Paul E. McKenney628edaa2014-01-31 11:57:43 -0800614 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;
Paul E. McKenney47cf29b2014-02-03 11:52:27 -0800625 ret = torture_create_kthread(torture_stutter, NULL, stutter_task);
626 return ret;
Paul E. McKenney628edaa2014-01-31 11:57:43 -0800627}
628EXPORT_SYMBOL_GPL(torture_stutter_init);
629
630/*
631 * Cleanup after the torture_stutter kthread.
632 */
Paul E. McKenneybfefc732014-02-04 12:35:27 -0800633static void torture_stutter_cleanup(void)
Paul E. McKenney628edaa2014-01-31 11:57:43 -0800634{
635 if (!stutter_task)
636 return;
637 VERBOSE_TOROUT_STRING("Stopping torture_stutter task");
638 kthread_stop(stutter_task);
639 stutter_task = NULL;
640}
Paul E. McKenney628edaa2014-01-31 11:57:43 -0800641
642/*
Paul E. McKenneyb5daa8f2014-01-30 13:38:09 -0800643 * 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().
Paul E. McKenney628edaa2014-01-31 11:57:43 -0800647 *
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.
Paul E. McKenneyb5daa8f2014-01-30 13:38:09 -0800650 */
Pranith Kumar2b3f8ff2014-04-16 13:42:09 -0700651bool torture_init_begin(char *ttype, bool v, int *runnable)
Paul E. McKenneyb5daa8f2014-01-30 13:38:09 -0800652{
653 mutex_lock(&fullstop_mutex);
Paul E. McKenney52280842014-04-07 09:14:11 -0700654 if (torture_type != NULL) {
Paul E. McKenney9eb51882016-03-21 15:36:40 -0700655 pr_alert("torture_init_begin: Refusing %s init: %s running.\n",
Paul E. McKenney52280842014-04-07 09:14:11 -0700656 ttype, torture_type);
Paul E. McKenney9eb51882016-03-21 15:36:40 -0700657 pr_alert("torture_init_begin: One torture test at a time!\n");
Paul E. McKenney52280842014-04-07 09:14:11 -0700658 mutex_unlock(&fullstop_mutex);
659 return false;
660 }
Paul E. McKenneyb5daa8f2014-01-30 13:38:09 -0800661 torture_type = ttype;
662 verbose = v;
Paul E. McKenney628edaa2014-01-31 11:57:43 -0800663 torture_runnable = runnable;
Paul E. McKenney36970bb2014-01-30 15:49:29 -0800664 fullstop = FULLSTOP_DONTSTOP;
Paul E. McKenney52280842014-04-07 09:14:11 -0700665 return true;
Paul E. McKenneyb5daa8f2014-01-30 13:38:09 -0800666}
667EXPORT_SYMBOL_GPL(torture_init_begin);
668
669/*
670 * Tell the torture module that initialization is complete.
671 */
Pranith Kumar2b3f8ff2014-04-16 13:42:09 -0700672void torture_init_end(void)
Paul E. McKenneyb5daa8f2014-01-30 13:38:09 -0800673{
674 mutex_unlock(&fullstop_mutex);
Paul E. McKenney4622b482014-01-30 15:37:19 -0800675 register_reboot_notifier(&torture_shutdown_nb);
Paul E. McKenneyb5daa8f2014-01-30 13:38:09 -0800676}
677EXPORT_SYMBOL_GPL(torture_init_end);
Paul E. McKenneycc47ae02014-01-30 14:21:11 -0800678
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
Paul E. McKenneybfefc732014-02-04 12:35:27 -0800683 * is detected, otherwise, all kthreads started by functions in this file
684 * will be shut down.
Paul E. McKenneycc47ae02014-01-30 14:21:11 -0800685 *
686 * This must be called before the caller starts shutting down its own
687 * kthreads.
Davidlohr Buesod36a7a02014-09-11 20:40:21 -0700688 *
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.
Paul E. McKenneycc47ae02014-01-30 14:21:11 -0800693 */
Davidlohr Buesod36a7a02014-09-11 20:40:21 -0700694bool torture_cleanup_begin(void)
Paul E. McKenneycc47ae02014-01-30 14:21:11 -0800695{
696 mutex_lock(&fullstop_mutex);
Paul E. McKenney7d0ae802015-03-03 14:57:58 -0800697 if (READ_ONCE(fullstop) == FULLSTOP_SHUTDOWN) {
Paul E. McKenneycc47ae02014-01-30 14:21:11 -0800698 pr_warn("Concurrent rmmod and shutdown illegal!\n");
699 mutex_unlock(&fullstop_mutex);
700 schedule_timeout_uninterruptible(10);
701 return true;
702 }
Paul E. McKenney7d0ae802015-03-03 14:57:58 -0800703 WRITE_ONCE(fullstop, FULLSTOP_RMMOD);
Paul E. McKenneycc47ae02014-01-30 14:21:11 -0800704 mutex_unlock(&fullstop_mutex);
Paul E. McKenneybfefc732014-02-04 12:35:27 -0800705 torture_shutdown_cleanup();
Paul E. McKenneycc47ae02014-01-30 14:21:11 -0800706 torture_shuffle_cleanup();
Paul E. McKenneybfefc732014-02-04 12:35:27 -0800707 torture_stutter_cleanup();
Paul E. McKenneycc47ae02014-01-30 14:21:11 -0800708 torture_onoff_cleanup();
Davidlohr Buesod36a7a02014-09-11 20:40:21 -0700709 return false;
710}
711EXPORT_SYMBOL_GPL(torture_cleanup_begin);
712
713void torture_cleanup_end(void)
714{
Paul E. McKenney52280842014-04-07 09:14:11 -0700715 mutex_lock(&fullstop_mutex);
716 torture_type = NULL;
717 mutex_unlock(&fullstop_mutex);
Paul E. McKenneycc47ae02014-01-30 14:21:11 -0800718}
Davidlohr Buesod36a7a02014-09-11 20:40:21 -0700719EXPORT_SYMBOL_GPL(torture_cleanup_end);
Paul E. McKenney36970bb2014-01-30 15:49:29 -0800720
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{
Paul E. McKenney7d0ae802015-03-03 14:57:58 -0800736 return READ_ONCE(fullstop) != FULLSTOP_DONTSTOP;
Paul E. McKenney36970bb2014-01-30 15:49:29 -0800737}
738EXPORT_SYMBOL_GPL(torture_must_stop_irq);
Paul E. McKenney7fafaac2014-01-31 17:37:28 -0800739
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{
Paul E. McKenney0d6821d2014-03-03 16:58:03 -0800749 char buf[128];
750
751 snprintf(buf, sizeof(buf), "Stopping %s", title);
752 VERBOSE_TOROUT_STRING(buf);
Paul E. McKenney7fafaac2014-01-31 17:37:28 -0800753 while (!kthread_should_stop()) {
754 torture_shutdown_absorb(title);
755 schedule_timeout_uninterruptible(1);
756 }
757}
758EXPORT_SYMBOL_GPL(torture_kthread_stopping);
Paul E. McKenney47cf29b2014-02-03 11:52:27 -0800759
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);
Kees Cook69459152014-05-22 11:51:04 -0700771 *tp = kthread_run(fn, arg, "%s", s);
Paul E. McKenney47cf29b2014-02-03 11:52:27 -0800772 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);
Paul E. McKenney9c029b82014-02-04 11:47:08 -0800781
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);