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 <paulmck@us.ibm.com> | |
21 | * Based on kernel/rcu/torture.c. | |
22 | */ | |
23 | #include <linux/types.h> | |
24 | #include <linux/kernel.h> | |
25 | #include <linux/init.h> | |
26 | #include <linux/module.h> | |
27 | #include <linux/kthread.h> | |
28 | #include <linux/err.h> | |
29 | #include <linux/spinlock.h> | |
30 | #include <linux/smp.h> | |
31 | #include <linux/interrupt.h> | |
32 | #include <linux/sched.h> | |
33 | #include <linux/atomic.h> | |
34 | #include <linux/bitops.h> | |
35 | #include <linux/completion.h> | |
36 | #include <linux/moduleparam.h> | |
37 | #include <linux/percpu.h> | |
38 | #include <linux/notifier.h> | |
39 | #include <linux/reboot.h> | |
40 | #include <linux/freezer.h> | |
41 | #include <linux/cpu.h> | |
42 | #include <linux/delay.h> | |
43 | #include <linux/stat.h> | |
44 | #include <linux/slab.h> | |
45 | #include <linux/trace_clock.h> | |
46 | #include <asm/byteorder.h> | |
47 | #include <linux/torture.h> | |
48 | ||
49 | MODULE_LICENSE("GPL"); | |
50 | MODULE_AUTHOR("Paul E. McKenney <paulmck@us.ibm.com>"); | |
51 | ||
b5daa8f3 PM |
52 | static char *torture_type; |
53 | static bool verbose; | |
54 | ||
f67a3356 PM |
55 | int fullstop = FULLSTOP_RMMOD; |
56 | EXPORT_SYMBOL_GPL(fullstop); | |
4622b487 | 57 | static DEFINE_MUTEX(fullstop_mutex); |
f67a3356 | 58 | |
2e9e8081 PM |
59 | #ifdef CONFIG_HOTPLUG_CPU |
60 | ||
61 | /* | |
62 | * Variables for online-offline handling. Only present if CPU hotplug | |
63 | * is enabled, otherwise does nothing. | |
64 | */ | |
65 | ||
66 | static struct task_struct *onoff_task; | |
67 | static long onoff_holdoff; | |
68 | static long onoff_interval; | |
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 | ||
80 | /* | |
81 | * Execute random CPU-hotplug operations at the interval specified | |
82 | * by the onoff_interval. | |
83 | */ | |
84 | static int | |
85 | torture_onoff(void *arg) | |
86 | { | |
87 | int cpu; | |
88 | unsigned long delta; | |
89 | int maxcpu = -1; | |
90 | DEFINE_TORTURE_RANDOM(rand); | |
91 | int ret; | |
92 | unsigned long starttime; | |
93 | ||
94 | VERBOSE_TOROUT_STRING("torture_onoff task started"); | |
95 | for_each_online_cpu(cpu) | |
96 | maxcpu = cpu; | |
97 | WARN_ON(maxcpu < 0); | |
98 | if (onoff_holdoff > 0) { | |
99 | VERBOSE_TOROUT_STRING("torture_onoff begin holdoff"); | |
100 | schedule_timeout_interruptible(onoff_holdoff); | |
101 | VERBOSE_TOROUT_STRING("torture_onoff end holdoff"); | |
102 | } | |
103 | while (!torture_must_stop()) { | |
104 | cpu = (torture_random(&rand) >> 4) % (maxcpu + 1); | |
105 | if (cpu_online(cpu) && cpu_is_hotpluggable(cpu)) { | |
106 | if (verbose) | |
107 | pr_alert("%s" TORTURE_FLAG | |
108 | "torture_onoff task: offlining %d\n", | |
109 | torture_type, cpu); | |
110 | starttime = jiffies; | |
111 | n_offline_attempts++; | |
112 | ret = cpu_down(cpu); | |
113 | if (ret) { | |
114 | if (verbose) | |
115 | pr_alert("%s" TORTURE_FLAG | |
116 | "torture_onoff task: offline %d failed: errno %d\n", | |
117 | torture_type, cpu, ret); | |
118 | } else { | |
119 | if (verbose) | |
120 | pr_alert("%s" TORTURE_FLAG | |
121 | "torture_onoff task: offlined %d\n", | |
122 | torture_type, cpu); | |
123 | n_offline_successes++; | |
124 | delta = jiffies - starttime; | |
125 | sum_offline += delta; | |
126 | if (min_offline < 0) { | |
127 | min_offline = delta; | |
128 | max_offline = delta; | |
129 | } | |
130 | if (min_offline > delta) | |
131 | min_offline = delta; | |
132 | if (max_offline < delta) | |
133 | max_offline = delta; | |
134 | } | |
135 | } else if (cpu_is_hotpluggable(cpu)) { | |
136 | if (verbose) | |
137 | pr_alert("%s" TORTURE_FLAG | |
138 | "torture_onoff task: onlining %d\n", | |
139 | torture_type, cpu); | |
140 | starttime = jiffies; | |
141 | n_online_attempts++; | |
142 | ret = cpu_up(cpu); | |
143 | if (ret) { | |
144 | if (verbose) | |
145 | pr_alert("%s" TORTURE_FLAG | |
146 | "torture_onoff task: online %d failed: errno %d\n", | |
147 | torture_type, cpu, ret); | |
148 | } else { | |
149 | if (verbose) | |
150 | pr_alert("%s" TORTURE_FLAG | |
151 | "torture_onoff task: onlined %d\n", | |
152 | torture_type, cpu); | |
153 | n_online_successes++; | |
154 | delta = jiffies - starttime; | |
155 | sum_online += delta; | |
156 | if (min_online < 0) { | |
157 | min_online = delta; | |
158 | max_online = delta; | |
159 | } | |
160 | if (min_online > delta) | |
161 | min_online = delta; | |
162 | if (max_online < delta) | |
163 | max_online = delta; | |
164 | } | |
165 | } | |
166 | schedule_timeout_interruptible(onoff_interval); | |
167 | } | |
168 | VERBOSE_TOROUT_STRING("torture_onoff task stopping"); | |
169 | return 0; | |
170 | } | |
171 | ||
172 | #endif /* #ifdef CONFIG_HOTPLUG_CPU */ | |
173 | ||
174 | /* | |
175 | * Initiate online-offline handling. | |
176 | */ | |
177 | int torture_onoff_init(long ooholdoff, long oointerval) | |
178 | { | |
179 | #ifdef CONFIG_HOTPLUG_CPU | |
180 | int ret; | |
181 | ||
182 | onoff_holdoff = ooholdoff; | |
183 | onoff_interval = oointerval; | |
184 | if (onoff_interval <= 0) | |
185 | return 0; | |
186 | onoff_task = kthread_run(torture_onoff, NULL, "torture_onoff"); | |
187 | if (IS_ERR(onoff_task)) { | |
188 | ret = PTR_ERR(onoff_task); | |
189 | onoff_task = NULL; | |
190 | return ret; | |
191 | } | |
192 | torture_shuffle_task_register(onoff_task); | |
193 | #endif /* #ifdef CONFIG_HOTPLUG_CPU */ | |
194 | return 0; | |
195 | } | |
196 | EXPORT_SYMBOL_GPL(torture_onoff_init); | |
197 | ||
198 | /* | |
199 | * Clean up after online/offline testing. | |
200 | */ | |
cc47ae08 | 201 | static void torture_onoff_cleanup(void) |
2e9e8081 PM |
202 | { |
203 | #ifdef CONFIG_HOTPLUG_CPU | |
204 | if (onoff_task == NULL) | |
205 | return; | |
206 | VERBOSE_TOROUT_STRING("Stopping torture_onoff task"); | |
207 | kthread_stop(onoff_task); | |
208 | onoff_task = NULL; | |
209 | #endif /* #ifdef CONFIG_HOTPLUG_CPU */ | |
210 | } | |
211 | EXPORT_SYMBOL_GPL(torture_onoff_cleanup); | |
212 | ||
213 | /* | |
214 | * Print online/offline testing statistics. | |
215 | */ | |
216 | char *torture_onoff_stats(char *page) | |
217 | { | |
218 | #ifdef CONFIG_HOTPLUG_CPU | |
219 | page += sprintf(page, | |
220 | "onoff: %ld/%ld:%ld/%ld %d,%d:%d,%d %lu:%lu (HZ=%d) ", | |
221 | n_online_successes, n_online_attempts, | |
222 | n_offline_successes, n_offline_attempts, | |
223 | min_online, max_online, | |
224 | min_offline, max_offline, | |
225 | sum_online, sum_offline, HZ); | |
226 | #endif /* #ifdef CONFIG_HOTPLUG_CPU */ | |
227 | return page; | |
228 | } | |
229 | EXPORT_SYMBOL_GPL(torture_onoff_stats); | |
230 | ||
231 | /* | |
232 | * Were all the online/offline operations successful? | |
233 | */ | |
234 | bool torture_onoff_failures(void) | |
235 | { | |
236 | #ifdef CONFIG_HOTPLUG_CPU | |
237 | return n_online_successes != n_online_attempts || | |
238 | n_offline_successes != n_offline_attempts; | |
239 | #else /* #ifdef CONFIG_HOTPLUG_CPU */ | |
240 | return false; | |
241 | #endif /* #else #ifdef CONFIG_HOTPLUG_CPU */ | |
242 | } | |
243 | EXPORT_SYMBOL_GPL(torture_onoff_failures); | |
244 | ||
51b1130e PM |
245 | #define TORTURE_RANDOM_MULT 39916801 /* prime */ |
246 | #define TORTURE_RANDOM_ADD 479001701 /* prime */ | |
247 | #define TORTURE_RANDOM_REFRESH 10000 | |
248 | ||
249 | /* | |
250 | * Crude but fast random-number generator. Uses a linear congruential | |
251 | * generator, with occasional help from cpu_clock(). | |
252 | */ | |
253 | unsigned long | |
254 | torture_random(struct torture_random_state *trsp) | |
255 | { | |
256 | if (--trsp->trs_count < 0) { | |
257 | trsp->trs_state += (unsigned long)local_clock(); | |
258 | trsp->trs_count = TORTURE_RANDOM_REFRESH; | |
259 | } | |
260 | trsp->trs_state = trsp->trs_state * TORTURE_RANDOM_MULT + | |
261 | TORTURE_RANDOM_ADD; | |
262 | return swahw32(trsp->trs_state); | |
263 | } | |
264 | EXPORT_SYMBOL_GPL(torture_random); | |
f67a3356 | 265 | |
3808dc9f PM |
266 | /* |
267 | * Variables for shuffling. The idea is to ensure that each CPU stays | |
268 | * idle for an extended period to test interactions with dyntick idle, | |
269 | * as well as interactions with any per-CPU varibles. | |
270 | */ | |
271 | struct shuffle_task { | |
272 | struct list_head st_l; | |
273 | struct task_struct *st_t; | |
274 | }; | |
275 | ||
276 | static long shuffle_interval; /* In jiffies. */ | |
277 | static struct task_struct *shuffler_task; | |
278 | static cpumask_var_t shuffle_tmp_mask; | |
279 | static int shuffle_idle_cpu; /* Force all torture tasks off this CPU */ | |
280 | static struct list_head shuffle_task_list = LIST_HEAD_INIT(shuffle_task_list); | |
281 | static DEFINE_MUTEX(shuffle_task_mutex); | |
282 | ||
283 | /* | |
284 | * Register a task to be shuffled. If there is no memory, just splat | |
285 | * and don't bother registering. | |
286 | */ | |
287 | void torture_shuffle_task_register(struct task_struct *tp) | |
288 | { | |
289 | struct shuffle_task *stp; | |
290 | ||
291 | if (WARN_ON_ONCE(tp == NULL)) | |
292 | return; | |
293 | stp = kmalloc(sizeof(*stp), GFP_KERNEL); | |
294 | if (WARN_ON_ONCE(stp == NULL)) | |
295 | return; | |
296 | stp->st_t = tp; | |
297 | mutex_lock(&shuffle_task_mutex); | |
298 | list_add(&stp->st_l, &shuffle_task_list); | |
299 | mutex_unlock(&shuffle_task_mutex); | |
300 | } | |
301 | EXPORT_SYMBOL_GPL(torture_shuffle_task_register); | |
302 | ||
303 | /* | |
304 | * Unregister all tasks, for example, at the end of the torture run. | |
305 | */ | |
306 | static void torture_shuffle_task_unregister_all(void) | |
307 | { | |
308 | struct shuffle_task *stp; | |
309 | struct shuffle_task *p; | |
310 | ||
311 | mutex_lock(&shuffle_task_mutex); | |
312 | list_for_each_entry_safe(stp, p, &shuffle_task_list, st_l) { | |
313 | list_del(&stp->st_l); | |
314 | kfree(stp); | |
315 | } | |
316 | mutex_unlock(&shuffle_task_mutex); | |
317 | } | |
318 | ||
319 | /* Shuffle tasks such that we allow shuffle_idle_cpu to become idle. | |
320 | * A special case is when shuffle_idle_cpu = -1, in which case we allow | |
321 | * the tasks to run on all CPUs. | |
322 | */ | |
323 | static void torture_shuffle_tasks(void) | |
324 | { | |
325 | struct shuffle_task *stp; | |
326 | ||
327 | cpumask_setall(shuffle_tmp_mask); | |
328 | get_online_cpus(); | |
329 | ||
330 | /* No point in shuffling if there is only one online CPU (ex: UP) */ | |
331 | if (num_online_cpus() == 1) { | |
332 | put_online_cpus(); | |
333 | return; | |
334 | } | |
335 | ||
336 | /* Advance to the next CPU. Upon overflow, don't idle any CPUs. */ | |
337 | shuffle_idle_cpu = cpumask_next(shuffle_idle_cpu, shuffle_tmp_mask); | |
338 | if (shuffle_idle_cpu >= nr_cpu_ids) | |
339 | shuffle_idle_cpu = -1; | |
340 | if (shuffle_idle_cpu != -1) { | |
341 | cpumask_clear_cpu(shuffle_idle_cpu, shuffle_tmp_mask); | |
342 | if (cpumask_empty(shuffle_tmp_mask)) { | |
343 | put_online_cpus(); | |
344 | return; | |
345 | } | |
346 | } | |
347 | ||
348 | mutex_lock(&shuffle_task_mutex); | |
349 | list_for_each_entry(stp, &shuffle_task_list, st_l) | |
350 | set_cpus_allowed_ptr(stp->st_t, shuffle_tmp_mask); | |
351 | mutex_unlock(&shuffle_task_mutex); | |
352 | ||
353 | put_online_cpus(); | |
354 | } | |
355 | ||
356 | /* Shuffle tasks across CPUs, with the intent of allowing each CPU in the | |
357 | * system to become idle at a time and cut off its timer ticks. This is meant | |
358 | * to test the support for such tickless idle CPU in RCU. | |
359 | */ | |
360 | static int torture_shuffle(void *arg) | |
361 | { | |
362 | VERBOSE_TOROUT_STRING("torture_shuffle task started"); | |
363 | do { | |
364 | schedule_timeout_interruptible(shuffle_interval); | |
365 | torture_shuffle_tasks(); | |
366 | torture_shutdown_absorb("torture_shuffle"); | |
367 | } while (!torture_must_stop()); | |
368 | VERBOSE_TOROUT_STRING("torture_shuffle task stopping"); | |
369 | return 0; | |
370 | } | |
371 | ||
372 | /* | |
373 | * Start the shuffler, with shuffint in jiffies. | |
374 | */ | |
375 | int torture_shuffle_init(long shuffint) | |
376 | { | |
377 | int ret; | |
378 | ||
379 | shuffle_interval = shuffint; | |
380 | ||
381 | shuffle_idle_cpu = -1; | |
382 | ||
383 | if (!alloc_cpumask_var(&shuffle_tmp_mask, GFP_KERNEL)) { | |
384 | VERBOSE_TOROUT_ERRSTRING("Failed to alloc mask"); | |
385 | return -ENOMEM; | |
386 | } | |
387 | ||
388 | /* Create the shuffler thread */ | |
389 | shuffler_task = kthread_run(torture_shuffle, NULL, "torture_shuffle"); | |
390 | if (IS_ERR(shuffler_task)) { | |
391 | ret = PTR_ERR(shuffler_task); | |
392 | free_cpumask_var(shuffle_tmp_mask); | |
393 | VERBOSE_TOROUT_ERRSTRING("Failed to create shuffler"); | |
394 | shuffler_task = NULL; | |
395 | return ret; | |
396 | } | |
397 | torture_shuffle_task_register(shuffler_task); | |
398 | return 0; | |
399 | } | |
400 | EXPORT_SYMBOL_GPL(torture_shuffle_init); | |
401 | ||
402 | /* | |
403 | * Stop the shuffling. | |
404 | */ | |
cc47ae08 | 405 | static void torture_shuffle_cleanup(void) |
3808dc9f PM |
406 | { |
407 | torture_shuffle_task_unregister_all(); | |
408 | if (shuffler_task) { | |
409 | VERBOSE_TOROUT_STRING("Stopping torture_shuffle task"); | |
410 | kthread_stop(shuffler_task); | |
411 | free_cpumask_var(shuffle_tmp_mask); | |
412 | } | |
413 | shuffler_task = NULL; | |
414 | } | |
415 | EXPORT_SYMBOL_GPL(torture_shuffle_cleanup); | |
416 | ||
f67a3356 PM |
417 | /* |
418 | * Absorb kthreads into a kernel function that won't return, so that | |
419 | * they won't ever access module text or data again. | |
420 | */ | |
421 | void torture_shutdown_absorb(const char *title) | |
422 | { | |
423 | while (ACCESS_ONCE(fullstop) == FULLSTOP_SHUTDOWN) { | |
4622b487 PM |
424 | pr_notice("torture thread %s parking due to system shutdown\n", |
425 | title); | |
f67a3356 PM |
426 | schedule_timeout_uninterruptible(MAX_SCHEDULE_TIMEOUT); |
427 | } | |
428 | } | |
429 | EXPORT_SYMBOL_GPL(torture_shutdown_absorb); | |
b5daa8f3 | 430 | |
4622b487 PM |
431 | /* |
432 | * Detect and respond to a system shutdown. | |
433 | */ | |
434 | static int torture_shutdown_notify(struct notifier_block *unused1, | |
435 | unsigned long unused2, void *unused3) | |
436 | { | |
437 | mutex_lock(&fullstop_mutex); | |
438 | if (fullstop == FULLSTOP_DONTSTOP) | |
439 | fullstop = FULLSTOP_SHUTDOWN; | |
440 | else | |
441 | pr_warn("Concurrent rmmod and shutdown illegal!\n"); | |
442 | mutex_unlock(&fullstop_mutex); | |
443 | return NOTIFY_DONE; | |
444 | } | |
445 | ||
446 | static struct notifier_block torture_shutdown_nb = { | |
447 | .notifier_call = torture_shutdown_notify, | |
448 | }; | |
449 | ||
b5daa8f3 PM |
450 | /* |
451 | * Initialize torture module. Please note that this is -not- invoked via | |
452 | * the usual module_init() mechanism, but rather by an explicit call from | |
453 | * the client torture module. This call must be paired with a later | |
454 | * torture_init_end(). | |
455 | */ | |
456 | void __init torture_init_begin(char *ttype, bool v) | |
457 | { | |
458 | mutex_lock(&fullstop_mutex); | |
459 | torture_type = ttype; | |
460 | verbose = v; | |
461 | ||
462 | } | |
463 | EXPORT_SYMBOL_GPL(torture_init_begin); | |
464 | ||
465 | /* | |
466 | * Tell the torture module that initialization is complete. | |
467 | */ | |
468 | void __init torture_init_end(void) | |
469 | { | |
470 | mutex_unlock(&fullstop_mutex); | |
4622b487 | 471 | register_reboot_notifier(&torture_shutdown_nb); |
b5daa8f3 PM |
472 | } |
473 | EXPORT_SYMBOL_GPL(torture_init_end); | |
cc47ae08 PM |
474 | |
475 | /* | |
476 | * Clean up torture module. Please note that this is -not- invoked via | |
477 | * the usual module_exit() mechanism, but rather by an explicit call from | |
478 | * the client torture module. Returns true if a race with system shutdown | |
479 | * is detected. | |
480 | * | |
481 | * This must be called before the caller starts shutting down its own | |
482 | * kthreads. | |
483 | */ | |
484 | bool torture_cleanup(void) | |
485 | { | |
486 | mutex_lock(&fullstop_mutex); | |
487 | if (fullstop == FULLSTOP_SHUTDOWN) { | |
488 | pr_warn("Concurrent rmmod and shutdown illegal!\n"); | |
489 | mutex_unlock(&fullstop_mutex); | |
490 | schedule_timeout_uninterruptible(10); | |
491 | return true; | |
492 | } | |
493 | fullstop = FULLSTOP_RMMOD; | |
494 | mutex_unlock(&fullstop_mutex); | |
4622b487 | 495 | unregister_reboot_notifier(&torture_shutdown_nb); |
cc47ae08 PM |
496 | torture_shuffle_cleanup(); |
497 | torture_onoff_cleanup(); | |
498 | return false; | |
499 | } | |
500 | EXPORT_SYMBOL_GPL(torture_cleanup); |