2 * Common functions for in-kernel torture tests.
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.
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.
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.
18 * Copyright (C) IBM Corporation, 2014
20 * Author: Paul E. McKenney <paulmck@us.ibm.com>
21 * Based on kernel/rcu/torture.c.
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>
49 MODULE_LICENSE("GPL");
50 MODULE_AUTHOR("Paul E. McKenney <paulmck@us.ibm.com>");
52 static char *torture_type;
55 /* Mediate rmmod and system shutdown. Concurrent rmmod & shutdown illegal! */
56 #define FULLSTOP_DONTSTOP 0 /* Normal operation. */
57 #define FULLSTOP_SHUTDOWN 1 /* System shutdown with torture running. */
58 #define FULLSTOP_RMMOD 2 /* Normal rmmod of torture. */
59 static int fullstop = FULLSTOP_RMMOD;
60 static DEFINE_MUTEX(fullstop_mutex);
62 #ifdef CONFIG_HOTPLUG_CPU
65 * Variables for online-offline handling. Only present if CPU hotplug
66 * is enabled, otherwise does nothing.
69 static struct task_struct *onoff_task;
70 static long onoff_holdoff;
71 static long onoff_interval;
72 static long n_offline_attempts;
73 static long n_offline_successes;
74 static unsigned long sum_offline;
75 static int min_offline = -1;
76 static int max_offline;
77 static long n_online_attempts;
78 static long n_online_successes;
79 static unsigned long sum_online;
80 static int min_online = -1;
81 static int max_online;
84 * Execute random CPU-hotplug operations at the interval specified
85 * by the onoff_interval.
88 torture_onoff(void *arg)
93 DEFINE_TORTURE_RANDOM(rand);
95 unsigned long starttime;
97 VERBOSE_TOROUT_STRING("torture_onoff task started");
98 for_each_online_cpu(cpu)
101 if (onoff_holdoff > 0) {
102 VERBOSE_TOROUT_STRING("torture_onoff begin holdoff");
103 schedule_timeout_interruptible(onoff_holdoff);
104 VERBOSE_TOROUT_STRING("torture_onoff end holdoff");
106 while (!torture_must_stop()) {
107 cpu = (torture_random(&rand) >> 4) % (maxcpu + 1);
108 if (cpu_online(cpu) && cpu_is_hotpluggable(cpu)) {
110 pr_alert("%s" TORTURE_FLAG
111 "torture_onoff task: offlining %d\n",
114 n_offline_attempts++;
118 pr_alert("%s" TORTURE_FLAG
119 "torture_onoff task: offline %d failed: errno %d\n",
120 torture_type, cpu, ret);
123 pr_alert("%s" TORTURE_FLAG
124 "torture_onoff task: offlined %d\n",
126 n_offline_successes++;
127 delta = jiffies - starttime;
128 sum_offline += delta;
129 if (min_offline < 0) {
133 if (min_offline > delta)
135 if (max_offline < delta)
138 } else if (cpu_is_hotpluggable(cpu)) {
140 pr_alert("%s" TORTURE_FLAG
141 "torture_onoff task: onlining %d\n",
148 pr_alert("%s" TORTURE_FLAG
149 "torture_onoff task: online %d failed: errno %d\n",
150 torture_type, cpu, ret);
153 pr_alert("%s" TORTURE_FLAG
154 "torture_onoff task: onlined %d\n",
156 n_online_successes++;
157 delta = jiffies - starttime;
159 if (min_online < 0) {
163 if (min_online > delta)
165 if (max_online < delta)
169 schedule_timeout_interruptible(onoff_interval);
171 VERBOSE_TOROUT_STRING("torture_onoff task stopping");
175 #endif /* #ifdef CONFIG_HOTPLUG_CPU */
178 * Initiate online-offline handling.
180 int torture_onoff_init(long ooholdoff, long oointerval)
182 #ifdef CONFIG_HOTPLUG_CPU
185 onoff_holdoff = ooholdoff;
186 onoff_interval = oointerval;
187 if (onoff_interval <= 0)
189 onoff_task = kthread_run(torture_onoff, NULL, "torture_onoff");
190 if (IS_ERR(onoff_task)) {
191 ret = PTR_ERR(onoff_task);
195 torture_shuffle_task_register(onoff_task);
196 #endif /* #ifdef CONFIG_HOTPLUG_CPU */
199 EXPORT_SYMBOL_GPL(torture_onoff_init);
202 * Clean up after online/offline testing.
204 static void torture_onoff_cleanup(void)
206 #ifdef CONFIG_HOTPLUG_CPU
207 if (onoff_task == NULL)
209 VERBOSE_TOROUT_STRING("Stopping torture_onoff task");
210 kthread_stop(onoff_task);
212 #endif /* #ifdef CONFIG_HOTPLUG_CPU */
214 EXPORT_SYMBOL_GPL(torture_onoff_cleanup);
217 * Print online/offline testing statistics.
219 char *torture_onoff_stats(char *page)
221 #ifdef CONFIG_HOTPLUG_CPU
222 page += sprintf(page,
223 "onoff: %ld/%ld:%ld/%ld %d,%d:%d,%d %lu:%lu (HZ=%d) ",
224 n_online_successes, n_online_attempts,
225 n_offline_successes, n_offline_attempts,
226 min_online, max_online,
227 min_offline, max_offline,
228 sum_online, sum_offline, HZ);
229 #endif /* #ifdef CONFIG_HOTPLUG_CPU */
232 EXPORT_SYMBOL_GPL(torture_onoff_stats);
235 * Were all the online/offline operations successful?
237 bool torture_onoff_failures(void)
239 #ifdef CONFIG_HOTPLUG_CPU
240 return n_online_successes != n_online_attempts ||
241 n_offline_successes != n_offline_attempts;
242 #else /* #ifdef CONFIG_HOTPLUG_CPU */
244 #endif /* #else #ifdef CONFIG_HOTPLUG_CPU */
246 EXPORT_SYMBOL_GPL(torture_onoff_failures);
248 #define TORTURE_RANDOM_MULT 39916801 /* prime */
249 #define TORTURE_RANDOM_ADD 479001701 /* prime */
250 #define TORTURE_RANDOM_REFRESH 10000
253 * Crude but fast random-number generator. Uses a linear congruential
254 * generator, with occasional help from cpu_clock().
257 torture_random(struct torture_random_state *trsp)
259 if (--trsp->trs_count < 0) {
260 trsp->trs_state += (unsigned long)local_clock();
261 trsp->trs_count = TORTURE_RANDOM_REFRESH;
263 trsp->trs_state = trsp->trs_state * TORTURE_RANDOM_MULT +
265 return swahw32(trsp->trs_state);
267 EXPORT_SYMBOL_GPL(torture_random);
270 * Variables for shuffling. The idea is to ensure that each CPU stays
271 * idle for an extended period to test interactions with dyntick idle,
272 * as well as interactions with any per-CPU varibles.
274 struct shuffle_task {
275 struct list_head st_l;
276 struct task_struct *st_t;
279 static long shuffle_interval; /* In jiffies. */
280 static struct task_struct *shuffler_task;
281 static cpumask_var_t shuffle_tmp_mask;
282 static int shuffle_idle_cpu; /* Force all torture tasks off this CPU */
283 static struct list_head shuffle_task_list = LIST_HEAD_INIT(shuffle_task_list);
284 static DEFINE_MUTEX(shuffle_task_mutex);
287 * Register a task to be shuffled. If there is no memory, just splat
288 * and don't bother registering.
290 void torture_shuffle_task_register(struct task_struct *tp)
292 struct shuffle_task *stp;
294 if (WARN_ON_ONCE(tp == NULL))
296 stp = kmalloc(sizeof(*stp), GFP_KERNEL);
297 if (WARN_ON_ONCE(stp == NULL))
300 mutex_lock(&shuffle_task_mutex);
301 list_add(&stp->st_l, &shuffle_task_list);
302 mutex_unlock(&shuffle_task_mutex);
304 EXPORT_SYMBOL_GPL(torture_shuffle_task_register);
307 * Unregister all tasks, for example, at the end of the torture run.
309 static void torture_shuffle_task_unregister_all(void)
311 struct shuffle_task *stp;
312 struct shuffle_task *p;
314 mutex_lock(&shuffle_task_mutex);
315 list_for_each_entry_safe(stp, p, &shuffle_task_list, st_l) {
316 list_del(&stp->st_l);
319 mutex_unlock(&shuffle_task_mutex);
322 /* Shuffle tasks such that we allow shuffle_idle_cpu to become idle.
323 * A special case is when shuffle_idle_cpu = -1, in which case we allow
324 * the tasks to run on all CPUs.
326 static void torture_shuffle_tasks(void)
328 struct shuffle_task *stp;
330 cpumask_setall(shuffle_tmp_mask);
333 /* No point in shuffling if there is only one online CPU (ex: UP) */
334 if (num_online_cpus() == 1) {
339 /* Advance to the next CPU. Upon overflow, don't idle any CPUs. */
340 shuffle_idle_cpu = cpumask_next(shuffle_idle_cpu, shuffle_tmp_mask);
341 if (shuffle_idle_cpu >= nr_cpu_ids)
342 shuffle_idle_cpu = -1;
343 if (shuffle_idle_cpu != -1) {
344 cpumask_clear_cpu(shuffle_idle_cpu, shuffle_tmp_mask);
345 if (cpumask_empty(shuffle_tmp_mask)) {
351 mutex_lock(&shuffle_task_mutex);
352 list_for_each_entry(stp, &shuffle_task_list, st_l)
353 set_cpus_allowed_ptr(stp->st_t, shuffle_tmp_mask);
354 mutex_unlock(&shuffle_task_mutex);
359 /* Shuffle tasks across CPUs, with the intent of allowing each CPU in the
360 * system to become idle at a time and cut off its timer ticks. This is meant
361 * to test the support for such tickless idle CPU in RCU.
363 static int torture_shuffle(void *arg)
365 VERBOSE_TOROUT_STRING("torture_shuffle task started");
367 schedule_timeout_interruptible(shuffle_interval);
368 torture_shuffle_tasks();
369 torture_shutdown_absorb("torture_shuffle");
370 } while (!torture_must_stop());
371 VERBOSE_TOROUT_STRING("torture_shuffle task stopping");
376 * Start the shuffler, with shuffint in jiffies.
378 int torture_shuffle_init(long shuffint)
382 shuffle_interval = shuffint;
384 shuffle_idle_cpu = -1;
386 if (!alloc_cpumask_var(&shuffle_tmp_mask, GFP_KERNEL)) {
387 VERBOSE_TOROUT_ERRSTRING("Failed to alloc mask");
391 /* Create the shuffler thread */
392 shuffler_task = kthread_run(torture_shuffle, NULL, "torture_shuffle");
393 if (IS_ERR(shuffler_task)) {
394 ret = PTR_ERR(shuffler_task);
395 free_cpumask_var(shuffle_tmp_mask);
396 VERBOSE_TOROUT_ERRSTRING("Failed to create shuffler");
397 shuffler_task = NULL;
400 torture_shuffle_task_register(shuffler_task);
403 EXPORT_SYMBOL_GPL(torture_shuffle_init);
406 * Stop the shuffling.
408 static void torture_shuffle_cleanup(void)
410 torture_shuffle_task_unregister_all();
412 VERBOSE_TOROUT_STRING("Stopping torture_shuffle task");
413 kthread_stop(shuffler_task);
414 free_cpumask_var(shuffle_tmp_mask);
416 shuffler_task = NULL;
418 EXPORT_SYMBOL_GPL(torture_shuffle_cleanup);
421 * Absorb kthreads into a kernel function that won't return, so that
422 * they won't ever access module text or data again.
424 void torture_shutdown_absorb(const char *title)
426 while (ACCESS_ONCE(fullstop) == FULLSTOP_SHUTDOWN) {
427 pr_notice("torture thread %s parking due to system shutdown\n",
429 schedule_timeout_uninterruptible(MAX_SCHEDULE_TIMEOUT);
432 EXPORT_SYMBOL_GPL(torture_shutdown_absorb);
435 * Detect and respond to a system shutdown.
437 static int torture_shutdown_notify(struct notifier_block *unused1,
438 unsigned long unused2, void *unused3)
440 mutex_lock(&fullstop_mutex);
441 if (fullstop == FULLSTOP_DONTSTOP)
442 fullstop = FULLSTOP_SHUTDOWN;
444 pr_warn("Concurrent rmmod and shutdown illegal!\n");
445 mutex_unlock(&fullstop_mutex);
449 static struct notifier_block torture_shutdown_nb = {
450 .notifier_call = torture_shutdown_notify,
454 * Initialize torture module. Please note that this is -not- invoked via
455 * the usual module_init() mechanism, but rather by an explicit call from
456 * the client torture module. This call must be paired with a later
457 * torture_init_end().
459 void __init torture_init_begin(char *ttype, bool v)
461 mutex_lock(&fullstop_mutex);
462 torture_type = ttype;
464 fullstop = FULLSTOP_DONTSTOP;
467 EXPORT_SYMBOL_GPL(torture_init_begin);
470 * Tell the torture module that initialization is complete.
472 void __init torture_init_end(void)
474 mutex_unlock(&fullstop_mutex);
475 register_reboot_notifier(&torture_shutdown_nb);
477 EXPORT_SYMBOL_GPL(torture_init_end);
480 * Clean up torture module. Please note that this is -not- invoked via
481 * the usual module_exit() mechanism, but rather by an explicit call from
482 * the client torture module. Returns true if a race with system shutdown
485 * This must be called before the caller starts shutting down its own
488 bool torture_cleanup(void)
490 mutex_lock(&fullstop_mutex);
491 if (fullstop == FULLSTOP_SHUTDOWN) {
492 pr_warn("Concurrent rmmod and shutdown illegal!\n");
493 mutex_unlock(&fullstop_mutex);
494 schedule_timeout_uninterruptible(10);
497 fullstop = FULLSTOP_RMMOD;
498 mutex_unlock(&fullstop_mutex);
499 unregister_reboot_notifier(&torture_shutdown_nb);
500 torture_shuffle_cleanup();
501 torture_onoff_cleanup();
504 EXPORT_SYMBOL_GPL(torture_cleanup);
507 * Is it time for the current torture test to stop?
509 bool torture_must_stop(void)
511 return torture_must_stop_irq() || kthread_should_stop();
513 EXPORT_SYMBOL_GPL(torture_must_stop);
516 * Is it time for the current torture test to stop? This is the irq-safe
517 * version, hence no check for kthread_should_stop().
519 bool torture_must_stop_irq(void)
521 return fullstop != FULLSTOP_DONTSTOP;
523 EXPORT_SYMBOL_GPL(torture_must_stop_irq);