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