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