]>
Commit | Line | Data |
---|---|---|
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 <[email protected]> | |
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 | |
55 | MODULE_LICENSE("GPL"); | |
56 | MODULE_AUTHOR("Paul E. McKenney <[email protected]>"); | |
57 | ||
b5daa8f3 | 58 | static char *torture_type; |
90127d60 | 59 | static 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. */ | |
65 | static int fullstop = FULLSTOP_RMMOD; | |
4622b487 | 66 | static 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 | ||
75 | static struct task_struct *onoff_task; | |
76 | static long onoff_holdoff; | |
77 | static long onoff_interval; | |
78 | static long n_offline_attempts; | |
79 | static long n_offline_successes; | |
80 | static unsigned long sum_offline; | |
81 | static int min_offline = -1; | |
82 | static int max_offline; | |
83 | static long n_online_attempts; | |
84 | static long n_online_successes; | |
85 | static unsigned long sum_online; | |
86 | static int min_online = -1; | |
87 | static 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 | */ | |
94 | bool 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 | } | |
136 | EXPORT_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 | */ | |
143 | bool 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 | } | |
185 | EXPORT_SYMBOL_GPL(torture_online); | |
186 | ||
2e9e8081 PM |
187 | /* |
188 | * Execute random CPU-hotplug operations at the interval specified | |
189 | * by the onoff_interval. | |
190 | */ | |
191 | static int | |
192 | torture_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 | |
236 | stop: | |
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 | */ | |
246 | int torture_onoff_init(long ooholdoff, long oointerval) | |
247 | { | |
47cf29b9 | 248 | #ifdef CONFIG_HOTPLUG_CPU |
2e9e8081 PM |
249 | onoff_holdoff = ooholdoff; |
250 | onoff_interval = oointerval; | |
251 | if (onoff_interval <= 0) | |
252 | return 0; | |
2a7d9688 PG |
253 | return torture_create_kthread(torture_onoff, NULL, onoff_task); |
254 | #else /* #ifdef CONFIG_HOTPLUG_CPU */ | |
255 | return 0; | |
256 | #endif /* #else #ifdef CONFIG_HOTPLUG_CPU */ | |
2e9e8081 PM |
257 | } |
258 | EXPORT_SYMBOL_GPL(torture_onoff_init); | |
259 | ||
260 | /* | |
261 | * Clean up after online/offline testing. | |
262 | */ | |
cc47ae08 | 263 | static void torture_onoff_cleanup(void) |
2e9e8081 PM |
264 | { |
265 | #ifdef CONFIG_HOTPLUG_CPU | |
266 | if (onoff_task == NULL) | |
267 | return; | |
268 | VERBOSE_TOROUT_STRING("Stopping torture_onoff task"); | |
269 | kthread_stop(onoff_task); | |
270 | onoff_task = NULL; | |
271 | #endif /* #ifdef CONFIG_HOTPLUG_CPU */ | |
272 | } | |
273 | EXPORT_SYMBOL_GPL(torture_onoff_cleanup); | |
274 | ||
275 | /* | |
276 | * Print online/offline testing statistics. | |
277 | */ | |
eea203fe | 278 | void torture_onoff_stats(void) |
2e9e8081 PM |
279 | { |
280 | #ifdef CONFIG_HOTPLUG_CPU | |
eea203fe JP |
281 | pr_cont("onoff: %ld/%ld:%ld/%ld %d,%d:%d,%d %lu:%lu (HZ=%d) ", |
282 | n_online_successes, n_online_attempts, | |
283 | n_offline_successes, n_offline_attempts, | |
284 | min_online, max_online, | |
285 | min_offline, max_offline, | |
286 | sum_online, sum_offline, HZ); | |
2e9e8081 | 287 | #endif /* #ifdef CONFIG_HOTPLUG_CPU */ |
2e9e8081 PM |
288 | } |
289 | EXPORT_SYMBOL_GPL(torture_onoff_stats); | |
290 | ||
291 | /* | |
292 | * Were all the online/offline operations successful? | |
293 | */ | |
294 | bool torture_onoff_failures(void) | |
295 | { | |
296 | #ifdef CONFIG_HOTPLUG_CPU | |
297 | return n_online_successes != n_online_attempts || | |
298 | n_offline_successes != n_offline_attempts; | |
299 | #else /* #ifdef CONFIG_HOTPLUG_CPU */ | |
300 | return false; | |
301 | #endif /* #else #ifdef CONFIG_HOTPLUG_CPU */ | |
302 | } | |
303 | EXPORT_SYMBOL_GPL(torture_onoff_failures); | |
304 | ||
51b1130e PM |
305 | #define TORTURE_RANDOM_MULT 39916801 /* prime */ |
306 | #define TORTURE_RANDOM_ADD 479001701 /* prime */ | |
307 | #define TORTURE_RANDOM_REFRESH 10000 | |
308 | ||
309 | /* | |
310 | * Crude but fast random-number generator. Uses a linear congruential | |
311 | * generator, with occasional help from cpu_clock(). | |
312 | */ | |
313 | unsigned long | |
314 | torture_random(struct torture_random_state *trsp) | |
315 | { | |
316 | if (--trsp->trs_count < 0) { | |
317 | trsp->trs_state += (unsigned long)local_clock(); | |
318 | trsp->trs_count = TORTURE_RANDOM_REFRESH; | |
319 | } | |
320 | trsp->trs_state = trsp->trs_state * TORTURE_RANDOM_MULT + | |
321 | TORTURE_RANDOM_ADD; | |
322 | return swahw32(trsp->trs_state); | |
323 | } | |
324 | EXPORT_SYMBOL_GPL(torture_random); | |
f67a3356 | 325 | |
3808dc9f PM |
326 | /* |
327 | * Variables for shuffling. The idea is to ensure that each CPU stays | |
328 | * idle for an extended period to test interactions with dyntick idle, | |
b564d62e | 329 | * as well as interactions with any per-CPU variables. |
3808dc9f PM |
330 | */ |
331 | struct shuffle_task { | |
332 | struct list_head st_l; | |
333 | struct task_struct *st_t; | |
334 | }; | |
335 | ||
336 | static long shuffle_interval; /* In jiffies. */ | |
337 | static struct task_struct *shuffler_task; | |
338 | static cpumask_var_t shuffle_tmp_mask; | |
339 | static int shuffle_idle_cpu; /* Force all torture tasks off this CPU */ | |
340 | static struct list_head shuffle_task_list = LIST_HEAD_INIT(shuffle_task_list); | |
341 | static DEFINE_MUTEX(shuffle_task_mutex); | |
342 | ||
343 | /* | |
344 | * Register a task to be shuffled. If there is no memory, just splat | |
345 | * and don't bother registering. | |
346 | */ | |
347 | void torture_shuffle_task_register(struct task_struct *tp) | |
348 | { | |
349 | struct shuffle_task *stp; | |
350 | ||
351 | if (WARN_ON_ONCE(tp == NULL)) | |
352 | return; | |
353 | stp = kmalloc(sizeof(*stp), GFP_KERNEL); | |
354 | if (WARN_ON_ONCE(stp == NULL)) | |
355 | return; | |
356 | stp->st_t = tp; | |
357 | mutex_lock(&shuffle_task_mutex); | |
358 | list_add(&stp->st_l, &shuffle_task_list); | |
359 | mutex_unlock(&shuffle_task_mutex); | |
360 | } | |
361 | EXPORT_SYMBOL_GPL(torture_shuffle_task_register); | |
362 | ||
363 | /* | |
364 | * Unregister all tasks, for example, at the end of the torture run. | |
365 | */ | |
366 | static void torture_shuffle_task_unregister_all(void) | |
367 | { | |
368 | struct shuffle_task *stp; | |
369 | struct shuffle_task *p; | |
370 | ||
371 | mutex_lock(&shuffle_task_mutex); | |
372 | list_for_each_entry_safe(stp, p, &shuffle_task_list, st_l) { | |
373 | list_del(&stp->st_l); | |
374 | kfree(stp); | |
375 | } | |
376 | mutex_unlock(&shuffle_task_mutex); | |
377 | } | |
378 | ||
379 | /* Shuffle tasks such that we allow shuffle_idle_cpu to become idle. | |
380 | * A special case is when shuffle_idle_cpu = -1, in which case we allow | |
381 | * the tasks to run on all CPUs. | |
382 | */ | |
383 | static void torture_shuffle_tasks(void) | |
384 | { | |
385 | struct shuffle_task *stp; | |
386 | ||
387 | cpumask_setall(shuffle_tmp_mask); | |
388 | get_online_cpus(); | |
389 | ||
390 | /* No point in shuffling if there is only one online CPU (ex: UP) */ | |
391 | if (num_online_cpus() == 1) { | |
392 | put_online_cpus(); | |
393 | return; | |
394 | } | |
395 | ||
396 | /* Advance to the next CPU. Upon overflow, don't idle any CPUs. */ | |
397 | shuffle_idle_cpu = cpumask_next(shuffle_idle_cpu, shuffle_tmp_mask); | |
398 | if (shuffle_idle_cpu >= nr_cpu_ids) | |
399 | shuffle_idle_cpu = -1; | |
5ed63b19 | 400 | else |
3808dc9f | 401 | cpumask_clear_cpu(shuffle_idle_cpu, shuffle_tmp_mask); |
3808dc9f PM |
402 | |
403 | mutex_lock(&shuffle_task_mutex); | |
404 | list_for_each_entry(stp, &shuffle_task_list, st_l) | |
405 | set_cpus_allowed_ptr(stp->st_t, shuffle_tmp_mask); | |
406 | mutex_unlock(&shuffle_task_mutex); | |
407 | ||
408 | put_online_cpus(); | |
409 | } | |
410 | ||
411 | /* Shuffle tasks across CPUs, with the intent of allowing each CPU in the | |
412 | * system to become idle at a time and cut off its timer ticks. This is meant | |
413 | * to test the support for such tickless idle CPU in RCU. | |
414 | */ | |
415 | static int torture_shuffle(void *arg) | |
416 | { | |
417 | VERBOSE_TOROUT_STRING("torture_shuffle task started"); | |
418 | do { | |
419 | schedule_timeout_interruptible(shuffle_interval); | |
420 | torture_shuffle_tasks(); | |
421 | torture_shutdown_absorb("torture_shuffle"); | |
422 | } while (!torture_must_stop()); | |
7fafaac5 | 423 | torture_kthread_stopping("torture_shuffle"); |
3808dc9f PM |
424 | return 0; |
425 | } | |
426 | ||
427 | /* | |
428 | * Start the shuffler, with shuffint in jiffies. | |
429 | */ | |
430 | int torture_shuffle_init(long shuffint) | |
431 | { | |
3808dc9f PM |
432 | shuffle_interval = shuffint; |
433 | ||
434 | shuffle_idle_cpu = -1; | |
435 | ||
436 | if (!alloc_cpumask_var(&shuffle_tmp_mask, GFP_KERNEL)) { | |
437 | VERBOSE_TOROUT_ERRSTRING("Failed to alloc mask"); | |
438 | return -ENOMEM; | |
439 | } | |
440 | ||
441 | /* Create the shuffler thread */ | |
47cf29b9 | 442 | return torture_create_kthread(torture_shuffle, NULL, shuffler_task); |
3808dc9f PM |
443 | } |
444 | EXPORT_SYMBOL_GPL(torture_shuffle_init); | |
445 | ||
446 | /* | |
447 | * Stop the shuffling. | |
448 | */ | |
cc47ae08 | 449 | static void torture_shuffle_cleanup(void) |
3808dc9f PM |
450 | { |
451 | torture_shuffle_task_unregister_all(); | |
452 | if (shuffler_task) { | |
453 | VERBOSE_TOROUT_STRING("Stopping torture_shuffle task"); | |
454 | kthread_stop(shuffler_task); | |
455 | free_cpumask_var(shuffle_tmp_mask); | |
456 | } | |
457 | shuffler_task = NULL; | |
458 | } | |
459 | EXPORT_SYMBOL_GPL(torture_shuffle_cleanup); | |
460 | ||
e991dbc0 PM |
461 | /* |
462 | * Variables for auto-shutdown. This allows "lights out" torture runs | |
463 | * to be fully scripted. | |
464 | */ | |
e991dbc0 | 465 | static struct task_struct *shutdown_task; |
31257c3c | 466 | static ktime_t shutdown_time; /* time to system shutdown. */ |
e991dbc0 PM |
467 | static void (*torture_shutdown_hook)(void); |
468 | ||
f67a3356 PM |
469 | /* |
470 | * Absorb kthreads into a kernel function that won't return, so that | |
471 | * they won't ever access module text or data again. | |
472 | */ | |
473 | void torture_shutdown_absorb(const char *title) | |
474 | { | |
7d0ae808 | 475 | while (READ_ONCE(fullstop) == FULLSTOP_SHUTDOWN) { |
4622b487 PM |
476 | pr_notice("torture thread %s parking due to system shutdown\n", |
477 | title); | |
f67a3356 PM |
478 | schedule_timeout_uninterruptible(MAX_SCHEDULE_TIMEOUT); |
479 | } | |
480 | } | |
481 | EXPORT_SYMBOL_GPL(torture_shutdown_absorb); | |
b5daa8f3 | 482 | |
e991dbc0 PM |
483 | /* |
484 | * Cause the torture test to shutdown the system after the test has | |
485 | * run for the time specified by the shutdown_secs parameter. | |
486 | */ | |
487 | static int torture_shutdown(void *arg) | |
488 | { | |
31257c3c | 489 | ktime_t ktime_snap; |
e991dbc0 PM |
490 | |
491 | VERBOSE_TOROUT_STRING("torture_shutdown task started"); | |
31257c3c PM |
492 | ktime_snap = ktime_get(); |
493 | while (ktime_before(ktime_snap, shutdown_time) && | |
e991dbc0 | 494 | !torture_must_stop()) { |
e991dbc0 PM |
495 | if (verbose) |
496 | pr_alert("%s" TORTURE_FLAG | |
31257c3c PM |
497 | "torture_shutdown task: %llu ms remaining\n", |
498 | torture_type, | |
499 | ktime_ms_delta(shutdown_time, ktime_snap)); | |
500 | set_current_state(TASK_INTERRUPTIBLE); | |
501 | schedule_hrtimeout(&shutdown_time, HRTIMER_MODE_ABS); | |
502 | ktime_snap = ktime_get(); | |
e991dbc0 PM |
503 | } |
504 | if (torture_must_stop()) { | |
7fafaac5 | 505 | torture_kthread_stopping("torture_shutdown"); |
e991dbc0 PM |
506 | return 0; |
507 | } | |
508 | ||
509 | /* OK, shut down the system. */ | |
510 | ||
511 | VERBOSE_TOROUT_STRING("torture_shutdown task shutting down system"); | |
512 | shutdown_task = NULL; /* Avoid self-kill deadlock. */ | |
f881825a PM |
513 | if (torture_shutdown_hook) |
514 | torture_shutdown_hook(); | |
515 | else | |
516 | VERBOSE_TOROUT_STRING("No torture_shutdown_hook(), skipping."); | |
dac95906 | 517 | rcu_ftrace_dump(DUMP_ALL); |
e991dbc0 PM |
518 | kernel_power_off(); /* Shut down the system. */ |
519 | return 0; | |
520 | } | |
521 | ||
522 | /* | |
523 | * Start up the shutdown task. | |
524 | */ | |
525 | int torture_shutdown_init(int ssecs, void (*cleanup)(void)) | |
526 | { | |
e991dbc0 | 527 | torture_shutdown_hook = cleanup; |
31257c3c PM |
528 | if (ssecs > 0) { |
529 | shutdown_time = ktime_add(ktime_get(), ktime_set(ssecs, 0)); | |
2a7d9688 | 530 | return torture_create_kthread(torture_shutdown, NULL, |
47cf29b9 | 531 | shutdown_task); |
e991dbc0 | 532 | } |
2a7d9688 | 533 | return 0; |
e991dbc0 PM |
534 | } |
535 | EXPORT_SYMBOL_GPL(torture_shutdown_init); | |
536 | ||
4622b487 PM |
537 | /* |
538 | * Detect and respond to a system shutdown. | |
539 | */ | |
540 | static int torture_shutdown_notify(struct notifier_block *unused1, | |
541 | unsigned long unused2, void *unused3) | |
542 | { | |
543 | mutex_lock(&fullstop_mutex); | |
7d0ae808 | 544 | if (READ_ONCE(fullstop) == FULLSTOP_DONTSTOP) { |
fac480ef | 545 | VERBOSE_TOROUT_STRING("Unscheduled system shutdown detected"); |
7d0ae808 | 546 | WRITE_ONCE(fullstop, FULLSTOP_SHUTDOWN); |
fac480ef | 547 | } else { |
4622b487 | 548 | pr_warn("Concurrent rmmod and shutdown illegal!\n"); |
fac480ef | 549 | } |
4622b487 PM |
550 | mutex_unlock(&fullstop_mutex); |
551 | return NOTIFY_DONE; | |
552 | } | |
553 | ||
554 | static struct notifier_block torture_shutdown_nb = { | |
555 | .notifier_call = torture_shutdown_notify, | |
556 | }; | |
557 | ||
bfefc73a PM |
558 | /* |
559 | * Shut down the shutdown task. Say what??? Heh! This can happen if | |
560 | * the torture module gets an rmmod before the shutdown time arrives. ;-) | |
561 | */ | |
562 | static void torture_shutdown_cleanup(void) | |
563 | { | |
564 | unregister_reboot_notifier(&torture_shutdown_nb); | |
565 | if (shutdown_task != NULL) { | |
566 | VERBOSE_TOROUT_STRING("Stopping torture_shutdown task"); | |
567 | kthread_stop(shutdown_task); | |
568 | } | |
569 | shutdown_task = NULL; | |
570 | } | |
571 | ||
628edaa5 PM |
572 | /* |
573 | * Variables for stuttering, which means to periodically pause and | |
574 | * restart testing in order to catch bugs that appear when load is | |
575 | * suddenly applied to or removed from the system. | |
576 | */ | |
577 | static struct task_struct *stutter_task; | |
578 | static int stutter_pause_test; | |
579 | static int stutter; | |
580 | ||
581 | /* | |
582 | * Block until the stutter interval ends. This must be called periodically | |
583 | * by all running kthreads that need to be subject to stuttering. | |
584 | */ | |
474e59b4 | 585 | bool stutter_wait(const char *title) |
628edaa5 | 586 | { |
4ced3314 PM |
587 | int spt; |
588 | ||
cee43939 | 589 | cond_resched_tasks_rcu_qs(); |
4ced3314 | 590 | spt = READ_ONCE(stutter_pause_test); |
29d39390 | 591 | for (; spt; spt = READ_ONCE(stutter_pause_test)) { |
4ced3314 PM |
592 | if (spt == 1) { |
593 | schedule_timeout_interruptible(1); | |
594 | } else if (spt == 2) { | |
595 | while (READ_ONCE(stutter_pause_test)) | |
596 | cond_resched(); | |
597 | } else { | |
628edaa5 | 598 | schedule_timeout_interruptible(round_jiffies_relative(HZ)); |
4ced3314 | 599 | } |
628edaa5 PM |
600 | torture_shutdown_absorb(title); |
601 | } | |
474e59b4 | 602 | return !!spt; |
628edaa5 PM |
603 | } |
604 | EXPORT_SYMBOL_GPL(stutter_wait); | |
605 | ||
606 | /* | |
607 | * Cause the torture test to "stutter", starting and stopping all | |
608 | * threads periodically. | |
609 | */ | |
610 | static int torture_stutter(void *arg) | |
611 | { | |
612 | VERBOSE_TOROUT_STRING("torture_stutter task started"); | |
613 | do { | |
4ced3314 | 614 | if (!torture_must_stop() && stutter > 1) { |
7d0ae808 | 615 | WRITE_ONCE(stutter_pause_test, 1); |
4ced3314 PM |
616 | schedule_timeout_interruptible(stutter - 1); |
617 | WRITE_ONCE(stutter_pause_test, 2); | |
618 | schedule_timeout_interruptible(1); | |
628edaa5 | 619 | } |
4ced3314 | 620 | WRITE_ONCE(stutter_pause_test, 0); |
628edaa5 PM |
621 | if (!torture_must_stop()) |
622 | schedule_timeout_interruptible(stutter); | |
628edaa5 PM |
623 | torture_shutdown_absorb("torture_stutter"); |
624 | } while (!torture_must_stop()); | |
7fafaac5 | 625 | torture_kthread_stopping("torture_stutter"); |
628edaa5 PM |
626 | return 0; |
627 | } | |
628 | ||
629 | /* | |
630 | * Initialize and kick off the torture_stutter kthread. | |
631 | */ | |
2a7d9688 | 632 | int torture_stutter_init(const int s) |
628edaa5 | 633 | { |
628edaa5 | 634 | stutter = s; |
2a7d9688 | 635 | return torture_create_kthread(torture_stutter, NULL, stutter_task); |
628edaa5 PM |
636 | } |
637 | EXPORT_SYMBOL_GPL(torture_stutter_init); | |
638 | ||
639 | /* | |
640 | * Cleanup after the torture_stutter kthread. | |
641 | */ | |
bfefc73a | 642 | static void torture_stutter_cleanup(void) |
628edaa5 PM |
643 | { |
644 | if (!stutter_task) | |
645 | return; | |
646 | VERBOSE_TOROUT_STRING("Stopping torture_stutter task"); | |
647 | kthread_stop(stutter_task); | |
648 | stutter_task = NULL; | |
649 | } | |
628edaa5 | 650 | |
b5daa8f3 PM |
651 | /* |
652 | * Initialize torture module. Please note that this is -not- invoked via | |
653 | * the usual module_init() mechanism, but rather by an explicit call from | |
654 | * the client torture module. This call must be paired with a later | |
655 | * torture_init_end(). | |
628edaa5 PM |
656 | * |
657 | * The runnable parameter points to a flag that controls whether or not | |
658 | * the test is currently runnable. If there is no such flag, pass in NULL. | |
b5daa8f3 | 659 | */ |
90127d60 | 660 | bool torture_init_begin(char *ttype, int v) |
b5daa8f3 PM |
661 | { |
662 | mutex_lock(&fullstop_mutex); | |
5228084e | 663 | if (torture_type != NULL) { |
9eb5188a | 664 | pr_alert("torture_init_begin: Refusing %s init: %s running.\n", |
5228084e | 665 | ttype, torture_type); |
9eb5188a | 666 | pr_alert("torture_init_begin: One torture test at a time!\n"); |
5228084e PM |
667 | mutex_unlock(&fullstop_mutex); |
668 | return false; | |
669 | } | |
b5daa8f3 PM |
670 | torture_type = ttype; |
671 | verbose = v; | |
36970bb9 | 672 | fullstop = FULLSTOP_DONTSTOP; |
5228084e | 673 | return true; |
b5daa8f3 PM |
674 | } |
675 | EXPORT_SYMBOL_GPL(torture_init_begin); | |
676 | ||
677 | /* | |
678 | * Tell the torture module that initialization is complete. | |
679 | */ | |
2b3f8ffe | 680 | void torture_init_end(void) |
b5daa8f3 PM |
681 | { |
682 | mutex_unlock(&fullstop_mutex); | |
4622b487 | 683 | register_reboot_notifier(&torture_shutdown_nb); |
b5daa8f3 PM |
684 | } |
685 | EXPORT_SYMBOL_GPL(torture_init_end); | |
cc47ae08 PM |
686 | |
687 | /* | |
688 | * Clean up torture module. Please note that this is -not- invoked via | |
689 | * the usual module_exit() mechanism, but rather by an explicit call from | |
690 | * the client torture module. Returns true if a race with system shutdown | |
bfefc73a PM |
691 | * is detected, otherwise, all kthreads started by functions in this file |
692 | * will be shut down. | |
cc47ae08 PM |
693 | * |
694 | * This must be called before the caller starts shutting down its own | |
695 | * kthreads. | |
d36a7a0d DB |
696 | * |
697 | * Both torture_cleanup_begin() and torture_cleanup_end() must be paired, | |
698 | * in order to correctly perform the cleanup. They are separated because | |
699 | * threads can still need to reference the torture_type type, thus nullify | |
700 | * only after completing all other relevant calls. | |
cc47ae08 | 701 | */ |
d36a7a0d | 702 | bool torture_cleanup_begin(void) |
cc47ae08 PM |
703 | { |
704 | mutex_lock(&fullstop_mutex); | |
7d0ae808 | 705 | if (READ_ONCE(fullstop) == FULLSTOP_SHUTDOWN) { |
cc47ae08 PM |
706 | pr_warn("Concurrent rmmod and shutdown illegal!\n"); |
707 | mutex_unlock(&fullstop_mutex); | |
708 | schedule_timeout_uninterruptible(10); | |
709 | return true; | |
710 | } | |
7d0ae808 | 711 | WRITE_ONCE(fullstop, FULLSTOP_RMMOD); |
cc47ae08 | 712 | mutex_unlock(&fullstop_mutex); |
bfefc73a | 713 | torture_shutdown_cleanup(); |
cc47ae08 | 714 | torture_shuffle_cleanup(); |
bfefc73a | 715 | torture_stutter_cleanup(); |
cc47ae08 | 716 | torture_onoff_cleanup(); |
d36a7a0d DB |
717 | return false; |
718 | } | |
719 | EXPORT_SYMBOL_GPL(torture_cleanup_begin); | |
720 | ||
721 | void torture_cleanup_end(void) | |
722 | { | |
5228084e PM |
723 | mutex_lock(&fullstop_mutex); |
724 | torture_type = NULL; | |
725 | mutex_unlock(&fullstop_mutex); | |
cc47ae08 | 726 | } |
d36a7a0d | 727 | EXPORT_SYMBOL_GPL(torture_cleanup_end); |
36970bb9 PM |
728 | |
729 | /* | |
730 | * Is it time for the current torture test to stop? | |
731 | */ | |
732 | bool torture_must_stop(void) | |
733 | { | |
734 | return torture_must_stop_irq() || kthread_should_stop(); | |
735 | } | |
736 | EXPORT_SYMBOL_GPL(torture_must_stop); | |
737 | ||
738 | /* | |
739 | * Is it time for the current torture test to stop? This is the irq-safe | |
740 | * version, hence no check for kthread_should_stop(). | |
741 | */ | |
742 | bool torture_must_stop_irq(void) | |
743 | { | |
7d0ae808 | 744 | return READ_ONCE(fullstop) != FULLSTOP_DONTSTOP; |
36970bb9 PM |
745 | } |
746 | EXPORT_SYMBOL_GPL(torture_must_stop_irq); | |
7fafaac5 PM |
747 | |
748 | /* | |
749 | * Each kthread must wait for kthread_should_stop() before returning from | |
750 | * its top-level function, otherwise segfaults ensue. This function | |
751 | * prints a "stopping" message and waits for kthread_should_stop(), and | |
752 | * should be called from all torture kthreads immediately prior to | |
753 | * returning. | |
754 | */ | |
755 | void torture_kthread_stopping(char *title) | |
756 | { | |
0d6821d5 PM |
757 | char buf[128]; |
758 | ||
759 | snprintf(buf, sizeof(buf), "Stopping %s", title); | |
760 | VERBOSE_TOROUT_STRING(buf); | |
7fafaac5 PM |
761 | while (!kthread_should_stop()) { |
762 | torture_shutdown_absorb(title); | |
763 | schedule_timeout_uninterruptible(1); | |
764 | } | |
765 | } | |
766 | EXPORT_SYMBOL_GPL(torture_kthread_stopping); | |
47cf29b9 PM |
767 | |
768 | /* | |
769 | * Create a generic torture kthread that is immediately runnable. If you | |
770 | * need the kthread to be stopped so that you can do something to it before | |
771 | * it starts, you will need to open-code your own. | |
772 | */ | |
773 | int _torture_create_kthread(int (*fn)(void *arg), void *arg, char *s, char *m, | |
774 | char *f, struct task_struct **tp) | |
775 | { | |
776 | int ret = 0; | |
777 | ||
778 | VERBOSE_TOROUT_STRING(m); | |
6945915e | 779 | *tp = kthread_run(fn, arg, "%s", s); |
47cf29b9 PM |
780 | if (IS_ERR(*tp)) { |
781 | ret = PTR_ERR(*tp); | |
782 | VERBOSE_TOROUT_ERRSTRING(f); | |
783 | *tp = NULL; | |
784 | } | |
785 | torture_shuffle_task_register(*tp); | |
786 | return ret; | |
787 | } | |
788 | EXPORT_SYMBOL_GPL(_torture_create_kthread); | |
9c029b86 PM |
789 | |
790 | /* | |
791 | * Stop a generic kthread, emitting a message. | |
792 | */ | |
793 | void _torture_stop_kthread(char *m, struct task_struct **tp) | |
794 | { | |
795 | if (*tp == NULL) | |
796 | return; | |
797 | VERBOSE_TOROUT_STRING(m); | |
798 | kthread_stop(*tp); | |
799 | *tp = NULL; | |
800 | } | |
801 | EXPORT_SYMBOL_GPL(_torture_stop_kthread); |