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