Blame view

kernel/torture.c 21.7 KB
8f8e76c09   Paul E. McKenney   torture: Convert ...
1
  // SPDX-License-Identifier: GPL-2.0+
51b1130eb   Paul E. McKenney   rcutorture: Abstr...
2
3
4
  /*
   * Common functions for in-kernel torture tests.
   *
51b1130eb   Paul E. McKenney   rcutorture: Abstr...
5
6
   * Copyright (C) IBM Corporation, 2014
   *
8f8e76c09   Paul E. McKenney   torture: Convert ...
7
   * Author: Paul E. McKenney <paulmck@linux.ibm.com>
51b1130eb   Paul E. McKenney   rcutorture: Abstr...
8
9
   *	Based on kernel/rcu/torture.c.
   */
605000376   Paul E. McKenney   torture: Keep old...
10
11
  
  #define pr_fmt(fmt) fmt
51b1130eb   Paul E. McKenney   rcutorture: Abstr...
12
13
14
15
16
17
18
19
20
21
  #include <linux/types.h>
  #include <linux/kernel.h>
  #include <linux/init.h>
  #include <linux/module.h>
  #include <linux/kthread.h>
  #include <linux/err.h>
  #include <linux/spinlock.h>
  #include <linux/smp.h>
  #include <linux/interrupt.h>
  #include <linux/sched.h>
e60175710   Ingo Molnar   sched/headers: Pr...
22
  #include <linux/sched/clock.h>
51b1130eb   Paul E. McKenney   rcutorture: Abstr...
23
24
25
26
27
28
29
30
31
32
33
34
35
  #include <linux/atomic.h>
  #include <linux/bitops.h>
  #include <linux/completion.h>
  #include <linux/moduleparam.h>
  #include <linux/percpu.h>
  #include <linux/notifier.h>
  #include <linux/reboot.h>
  #include <linux/freezer.h>
  #include <linux/cpu.h>
  #include <linux/delay.h>
  #include <linux/stat.h>
  #include <linux/slab.h>
  #include <linux/trace_clock.h>
31257c3c8   Paul E. McKenney   torture: Convert ...
36
  #include <linux/ktime.h>
51b1130eb   Paul E. McKenney   rcutorture: Abstr...
37
38
  #include <asm/byteorder.h>
  #include <linux/torture.h>
dac959060   Paul E. McKenney   torture: Suppress...
39
  #include "rcu/rcu.h"
51b1130eb   Paul E. McKenney   rcutorture: Abstr...
40
41
  
  MODULE_LICENSE("GPL");
8f8e76c09   Paul E. McKenney   torture: Convert ...
42
  MODULE_AUTHOR("Paul E. McKenney <paulmck@linux.ibm.com>");
51b1130eb   Paul E. McKenney   rcutorture: Abstr...
43

8171d3e0d   Paul E. McKenney   torture: Allow di...
44
45
  static bool disable_onoff_at_boot;
  module_param(disable_onoff_at_boot, bool, 0444);
2102ad290   Paul E. McKenney   torture: Dump ftr...
46
47
  static bool ftrace_dump_at_shutdown;
  module_param(ftrace_dump_at_shutdown, bool, 0444);
b5daa8f3b   Paul E. McKenney   rcutorture: Abstr...
48
  static char *torture_type;
90127d605   Paul E. McKenney   torture: Make onl...
49
  static int verbose;
b5daa8f3b   Paul E. McKenney   rcutorture: Abstr...
50

36970bb91   Paul E. McKenney   rcutorture: Priva...
51
52
53
54
55
  /* Mediate rmmod and system shutdown.  Concurrent rmmod & shutdown illegal! */
  #define FULLSTOP_DONTSTOP 0	/* Normal operation. */
  #define FULLSTOP_SHUTDOWN 1	/* System shutdown with torture running. */
  #define FULLSTOP_RMMOD    2	/* Normal rmmod of torture. */
  static int fullstop = FULLSTOP_RMMOD;
4622b487e   Paul E. McKenney   rcutorture: Abstr...
56
  static DEFINE_MUTEX(fullstop_mutex);
f67a33561   Paul E. McKenney   rcutorture: Abstr...
57

2e9e8081d   Paul E. McKenney   rcutorture: Abstr...
58
59
60
61
62
63
64
65
66
67
  #ifdef CONFIG_HOTPLUG_CPU
  
  /*
   * Variables for online-offline handling.  Only present if CPU hotplug
   * is enabled, otherwise does nothing.
   */
  
  static struct task_struct *onoff_task;
  static long onoff_holdoff;
  static long onoff_interval;
3a6cb58f1   Paul E. McKenney   rcutorture: Add g...
68
  static torture_ofl_func *onoff_f;
2e9e8081d   Paul E. McKenney   rcutorture: Abstr...
69
70
71
72
73
74
75
76
77
78
79
80
  static long n_offline_attempts;
  static long n_offline_successes;
  static unsigned long sum_offline;
  static int min_offline = -1;
  static int max_offline;
  static long n_online_attempts;
  static long n_online_successes;
  static unsigned long sum_online;
  static int min_online = -1;
  static int max_online;
  
  /*
d95f5ba90   Paul E. McKenney   torture: Break o...
81
82
83
84
85
86
87
88
89
   * Attempt to take a CPU offline.  Return false if the CPU is already
   * offline or if it is not subject to CPU-hotplug operations.  The
   * caller can detect other failures by looking at the statistics.
   */
  bool torture_offline(int cpu, long *n_offl_attempts, long *n_offl_successes,
  		     unsigned long *sum_offl, int *min_offl, int *max_offl)
  {
  	unsigned long delta;
  	int ret;
a59ee765a   Paul E. McKenney   torture: Forgive ...
90
  	char *s;
d95f5ba90   Paul E. McKenney   torture: Break o...
91
92
93
94
  	unsigned long starttime;
  
  	if (!cpu_online(cpu) || !cpu_is_hotpluggable(cpu))
  		return false;
24aca4aea   Paul E. McKenney   torture: Don't tr...
95
96
  	if (num_online_cpus() <= 1)
  		return false;  /* Can't offline the last CPU. */
d95f5ba90   Paul E. McKenney   torture: Break o...
97

90127d605   Paul E. McKenney   torture: Make onl...
98
  	if (verbose > 1)
d95f5ba90   Paul E. McKenney   torture: Break o...
99
100
101
102
103
104
  		pr_alert("%s" TORTURE_FLAG
  			 "torture_onoff task: offlining %d
  ",
  			 torture_type, cpu);
  	starttime = jiffies;
  	(*n_offl_attempts)++;
457bc8ed3   Qais Yousef   torture: Replace ...
105
  	ret = remove_cpu(cpu);
d95f5ba90   Paul E. McKenney   torture: Break o...
106
  	if (ret) {
a59ee765a   Paul E. McKenney   torture: Forgive ...
107
108
109
110
111
112
  		s = "";
  		if (!rcu_inkernel_boot_has_ended() && ret == -EBUSY) {
  			// PCI probe frequently disables hotplug during boot.
  			(*n_offl_attempts)--;
  			s = " (-EBUSY forgiven during boot)";
  		}
d95f5ba90   Paul E. McKenney   torture: Break o...
113
114
  		if (verbose)
  			pr_alert("%s" TORTURE_FLAG
a59ee765a   Paul E. McKenney   torture: Forgive ...
115
116
117
  				 "torture_onoff task: offline %d failed%s: errno %d
  ",
  				 torture_type, cpu, s, ret);
d95f5ba90   Paul E. McKenney   torture: Break o...
118
  	} else {
90127d605   Paul E. McKenney   torture: Make onl...
119
  		if (verbose > 1)
d95f5ba90   Paul E. McKenney   torture: Break o...
120
121
122
123
  			pr_alert("%s" TORTURE_FLAG
  				 "torture_onoff task: offlined %d
  ",
  				 torture_type, cpu);
3a6cb58f1   Paul E. McKenney   rcutorture: Add g...
124
125
  		if (onoff_f)
  			onoff_f();
d95f5ba90   Paul E. McKenney   torture: Break o...
126
127
  		(*n_offl_successes)++;
  		delta = jiffies - starttime;
a2b2df207   Paul E. McKenney   torture: Fix typo...
128
  		*sum_offl += delta;
d95f5ba90   Paul E. McKenney   torture: Break o...
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
  		if (*min_offl < 0) {
  			*min_offl = delta;
  			*max_offl = delta;
  		}
  		if (*min_offl > delta)
  			*min_offl = delta;
  		if (*max_offl < delta)
  			*max_offl = delta;
  	}
  
  	return true;
  }
  EXPORT_SYMBOL_GPL(torture_offline);
  
  /*
   * Attempt to bring a CPU online.  Return false if the CPU is already
   * online or if it is not subject to CPU-hotplug operations.  The
   * caller can detect other failures by looking at the statistics.
   */
  bool torture_online(int cpu, long *n_onl_attempts, long *n_onl_successes,
  		    unsigned long *sum_onl, int *min_onl, int *max_onl)
  {
  	unsigned long delta;
  	int ret;
a59ee765a   Paul E. McKenney   torture: Forgive ...
153
  	char *s;
d95f5ba90   Paul E. McKenney   torture: Break o...
154
155
156
157
  	unsigned long starttime;
  
  	if (cpu_online(cpu) || !cpu_is_hotpluggable(cpu))
  		return false;
90127d605   Paul E. McKenney   torture: Make onl...
158
  	if (verbose > 1)
d95f5ba90   Paul E. McKenney   torture: Break o...
159
160
161
162
163
164
  		pr_alert("%s" TORTURE_FLAG
  			 "torture_onoff task: onlining %d
  ",
  			 torture_type, cpu);
  	starttime = jiffies;
  	(*n_onl_attempts)++;
457bc8ed3   Qais Yousef   torture: Replace ...
165
  	ret = add_cpu(cpu);
d95f5ba90   Paul E. McKenney   torture: Break o...
166
  	if (ret) {
a59ee765a   Paul E. McKenney   torture: Forgive ...
167
168
169
170
171
172
  		s = "";
  		if (!rcu_inkernel_boot_has_ended() && ret == -EBUSY) {
  			// PCI probe frequently disables hotplug during boot.
  			(*n_onl_attempts)--;
  			s = " (-EBUSY forgiven during boot)";
  		}
d95f5ba90   Paul E. McKenney   torture: Break o...
173
174
  		if (verbose)
  			pr_alert("%s" TORTURE_FLAG
a59ee765a   Paul E. McKenney   torture: Forgive ...
175
176
177
  				 "torture_onoff task: online %d failed%s: errno %d
  ",
  				 torture_type, cpu, s, ret);
d95f5ba90   Paul E. McKenney   torture: Break o...
178
  	} else {
90127d605   Paul E. McKenney   torture: Make onl...
179
  		if (verbose > 1)
d95f5ba90   Paul E. McKenney   torture: Break o...
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
  			pr_alert("%s" TORTURE_FLAG
  				 "torture_onoff task: onlined %d
  ",
  				 torture_type, cpu);
  		(*n_onl_successes)++;
  		delta = jiffies - starttime;
  		*sum_onl += delta;
  		if (*min_onl < 0) {
  			*min_onl = delta;
  			*max_onl = delta;
  		}
  		if (*min_onl > delta)
  			*min_onl = delta;
  		if (*max_onl < delta)
  			*max_onl = delta;
  	}
  
  	return true;
  }
  EXPORT_SYMBOL_GPL(torture_online);
  
  /*
2e9e8081d   Paul E. McKenney   rcutorture: Abstr...
202
203
204
205
206
207
208
   * Execute random CPU-hotplug operations at the interval specified
   * by the onoff_interval.
   */
  static int
  torture_onoff(void *arg)
  {
  	int cpu;
2e9e8081d   Paul E. McKenney   rcutorture: Abstr...
209
210
  	int maxcpu = -1;
  	DEFINE_TORTURE_RANDOM(rand);
28cf5952f   Paul E. McKenney   torture: Bring an...
211
  	int ret;
2e9e8081d   Paul E. McKenney   rcutorture: Abstr...
212
213
214
215
216
  
  	VERBOSE_TOROUT_STRING("torture_onoff task started");
  	for_each_online_cpu(cpu)
  		maxcpu = cpu;
  	WARN_ON(maxcpu < 0);
457bc8ed3   Qais Yousef   torture: Replace ...
217
  	if (!IS_MODULE(CONFIG_TORTURE_TEST)) {
28cf5952f   Paul E. McKenney   torture: Bring an...
218
219
220
  		for_each_possible_cpu(cpu) {
  			if (cpu_online(cpu))
  				continue;
457bc8ed3   Qais Yousef   torture: Replace ...
221
  			ret = add_cpu(cpu);
28cf5952f   Paul E. McKenney   torture: Bring an...
222
223
224
225
226
227
228
  			if (ret && verbose) {
  				pr_alert("%s" TORTURE_FLAG
  					 "%s: Initial online %d: errno %d
  ",
  					 __func__, torture_type, cpu, ret);
  			}
  		}
457bc8ed3   Qais Yousef   torture: Replace ...
229
  	}
750db0f5f   Boqun Feng   torture: Stop ono...
230
231
232
233
234
  
  	if (maxcpu == 0) {
  		VERBOSE_TOROUT_STRING("Only one CPU, so CPU-hotplug testing is disabled");
  		goto stop;
  	}
2e9e8081d   Paul E. McKenney   rcutorture: Abstr...
235
236
237
238
239
240
  	if (onoff_holdoff > 0) {
  		VERBOSE_TOROUT_STRING("torture_onoff begin holdoff");
  		schedule_timeout_interruptible(onoff_holdoff);
  		VERBOSE_TOROUT_STRING("torture_onoff end holdoff");
  	}
  	while (!torture_must_stop()) {
8171d3e0d   Paul E. McKenney   torture: Allow di...
241
242
243
244
  		if (disable_onoff_at_boot && !rcu_inkernel_boot_has_ended()) {
  			schedule_timeout_interruptible(HZ / 10);
  			continue;
  		}
2e9e8081d   Paul E. McKenney   rcutorture: Abstr...
245
  		cpu = (torture_random(&rand) >> 4) % (maxcpu + 1);
d95f5ba90   Paul E. McKenney   torture: Break o...
246
247
248
249
250
251
  		if (!torture_offline(cpu,
  				     &n_offline_attempts, &n_offline_successes,
  				     &sum_offline, &min_offline, &max_offline))
  			torture_online(cpu,
  				       &n_online_attempts, &n_online_successes,
  				       &sum_online, &min_online, &max_online);
2e9e8081d   Paul E. McKenney   rcutorture: Abstr...
252
253
  		schedule_timeout_interruptible(onoff_interval);
  	}
750db0f5f   Boqun Feng   torture: Stop ono...
254
255
  
  stop:
7fafaac5b   Paul E. McKenney   rcutorture: Fix r...
256
  	torture_kthread_stopping("torture_onoff");
2e9e8081d   Paul E. McKenney   rcutorture: Abstr...
257
258
259
260
261
262
263
264
  	return 0;
  }
  
  #endif /* #ifdef CONFIG_HOTPLUG_CPU */
  
  /*
   * Initiate online-offline handling.
   */
3a6cb58f1   Paul E. McKenney   rcutorture: Add g...
265
  int torture_onoff_init(long ooholdoff, long oointerval, torture_ofl_func *f)
2e9e8081d   Paul E. McKenney   rcutorture: Abstr...
266
  {
47cf29b9e   Paul E. McKenney   rcutorture: Abstr...
267
  #ifdef CONFIG_HOTPLUG_CPU
2e9e8081d   Paul E. McKenney   rcutorture: Abstr...
268
269
  	onoff_holdoff = ooholdoff;
  	onoff_interval = oointerval;
3a6cb58f1   Paul E. McKenney   rcutorture: Add g...
270
  	onoff_f = f;
2e9e8081d   Paul E. McKenney   rcutorture: Abstr...
271
272
  	if (onoff_interval <= 0)
  		return 0;
2a7d96881   Pierce Griffiths   torture: Remove u...
273
274
275
276
  	return torture_create_kthread(torture_onoff, NULL, onoff_task);
  #else /* #ifdef CONFIG_HOTPLUG_CPU */
  	return 0;
  #endif /* #else #ifdef CONFIG_HOTPLUG_CPU */
2e9e8081d   Paul E. McKenney   rcutorture: Abstr...
277
278
279
280
281
282
  }
  EXPORT_SYMBOL_GPL(torture_onoff_init);
  
  /*
   * Clean up after online/offline testing.
   */
cc47ae083   Paul E. McKenney   rcutorture: Abstr...
283
  static void torture_onoff_cleanup(void)
2e9e8081d   Paul E. McKenney   rcutorture: Abstr...
284
285
286
287
288
289
290
291
292
  {
  #ifdef CONFIG_HOTPLUG_CPU
  	if (onoff_task == NULL)
  		return;
  	VERBOSE_TOROUT_STRING("Stopping torture_onoff task");
  	kthread_stop(onoff_task);
  	onoff_task = NULL;
  #endif /* #ifdef CONFIG_HOTPLUG_CPU */
  }
2e9e8081d   Paul E. McKenney   rcutorture: Abstr...
293
294
295
296
  
  /*
   * Print online/offline testing statistics.
   */
eea203fea   Joe Perches   rcu: Use pr_alert...
297
  void torture_onoff_stats(void)
2e9e8081d   Paul E. McKenney   rcutorture: Abstr...
298
299
  {
  #ifdef CONFIG_HOTPLUG_CPU
eea203fea   Joe Perches   rcu: Use pr_alert...
300
301
302
303
304
305
  	pr_cont("onoff: %ld/%ld:%ld/%ld %d,%d:%d,%d %lu:%lu (HZ=%d) ",
  		n_online_successes, n_online_attempts,
  		n_offline_successes, n_offline_attempts,
  		min_online, max_online,
  		min_offline, max_offline,
  		sum_online, sum_offline, HZ);
2e9e8081d   Paul E. McKenney   rcutorture: Abstr...
306
  #endif /* #ifdef CONFIG_HOTPLUG_CPU */
2e9e8081d   Paul E. McKenney   rcutorture: Abstr...
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
  }
  EXPORT_SYMBOL_GPL(torture_onoff_stats);
  
  /*
   * Were all the online/offline operations successful?
   */
  bool torture_onoff_failures(void)
  {
  #ifdef CONFIG_HOTPLUG_CPU
  	return n_online_successes != n_online_attempts ||
  	       n_offline_successes != n_offline_attempts;
  #else /* #ifdef CONFIG_HOTPLUG_CPU */
  	return false;
  #endif /* #else #ifdef CONFIG_HOTPLUG_CPU */
  }
  EXPORT_SYMBOL_GPL(torture_onoff_failures);
51b1130eb   Paul E. McKenney   rcutorture: Abstr...
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
  #define TORTURE_RANDOM_MULT	39916801  /* prime */
  #define TORTURE_RANDOM_ADD	479001701 /* prime */
  #define TORTURE_RANDOM_REFRESH	10000
  
  /*
   * Crude but fast random-number generator.  Uses a linear congruential
   * generator, with occasional help from cpu_clock().
   */
  unsigned long
  torture_random(struct torture_random_state *trsp)
  {
  	if (--trsp->trs_count < 0) {
  		trsp->trs_state += (unsigned long)local_clock();
  		trsp->trs_count = TORTURE_RANDOM_REFRESH;
  	}
  	trsp->trs_state = trsp->trs_state * TORTURE_RANDOM_MULT +
  		TORTURE_RANDOM_ADD;
  	return swahw32(trsp->trs_state);
  }
  EXPORT_SYMBOL_GPL(torture_random);
f67a33561   Paul E. McKenney   rcutorture: Abstr...
343
344
  
  /*
3808dc9fa   Paul E. McKenney   rcutorture: Abstr...
345
346
   * Variables for shuffling.  The idea is to ensure that each CPU stays
   * idle for an extended period to test interactions with dyntick idle,
b564d62e6   Masahiro Yamada   scripts/spelling....
347
   * as well as interactions with any per-CPU variables.
3808dc9fa   Paul E. McKenney   rcutorture: Abstr...
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
   */
  struct shuffle_task {
  	struct list_head st_l;
  	struct task_struct *st_t;
  };
  
  static long shuffle_interval;	/* In jiffies. */
  static struct task_struct *shuffler_task;
  static cpumask_var_t shuffle_tmp_mask;
  static int shuffle_idle_cpu;	/* Force all torture tasks off this CPU */
  static struct list_head shuffle_task_list = LIST_HEAD_INIT(shuffle_task_list);
  static DEFINE_MUTEX(shuffle_task_mutex);
  
  /*
   * Register a task to be shuffled.  If there is no memory, just splat
   * and don't bother registering.
   */
  void torture_shuffle_task_register(struct task_struct *tp)
  {
  	struct shuffle_task *stp;
  
  	if (WARN_ON_ONCE(tp == NULL))
  		return;
  	stp = kmalloc(sizeof(*stp), GFP_KERNEL);
  	if (WARN_ON_ONCE(stp == NULL))
  		return;
  	stp->st_t = tp;
  	mutex_lock(&shuffle_task_mutex);
  	list_add(&stp->st_l, &shuffle_task_list);
  	mutex_unlock(&shuffle_task_mutex);
  }
  EXPORT_SYMBOL_GPL(torture_shuffle_task_register);
  
  /*
   * Unregister all tasks, for example, at the end of the torture run.
   */
  static void torture_shuffle_task_unregister_all(void)
  {
  	struct shuffle_task *stp;
  	struct shuffle_task *p;
  
  	mutex_lock(&shuffle_task_mutex);
  	list_for_each_entry_safe(stp, p, &shuffle_task_list, st_l) {
  		list_del(&stp->st_l);
  		kfree(stp);
  	}
  	mutex_unlock(&shuffle_task_mutex);
  }
  
  /* Shuffle tasks such that we allow shuffle_idle_cpu to become idle.
   * A special case is when shuffle_idle_cpu = -1, in which case we allow
   * the tasks to run on all CPUs.
   */
  static void torture_shuffle_tasks(void)
  {
  	struct shuffle_task *stp;
  
  	cpumask_setall(shuffle_tmp_mask);
  	get_online_cpus();
  
  	/* No point in shuffling if there is only one online CPU (ex: UP) */
  	if (num_online_cpus() == 1) {
  		put_online_cpus();
  		return;
  	}
  
  	/* Advance to the next CPU.  Upon overflow, don't idle any CPUs. */
  	shuffle_idle_cpu = cpumask_next(shuffle_idle_cpu, shuffle_tmp_mask);
  	if (shuffle_idle_cpu >= nr_cpu_ids)
  		shuffle_idle_cpu = -1;
5ed63b199   Iulia Manda   torture: Notice i...
418
  	else
3808dc9fa   Paul E. McKenney   rcutorture: Abstr...
419
  		cpumask_clear_cpu(shuffle_idle_cpu, shuffle_tmp_mask);
3808dc9fa   Paul E. McKenney   rcutorture: Abstr...
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
  
  	mutex_lock(&shuffle_task_mutex);
  	list_for_each_entry(stp, &shuffle_task_list, st_l)
  		set_cpus_allowed_ptr(stp->st_t, shuffle_tmp_mask);
  	mutex_unlock(&shuffle_task_mutex);
  
  	put_online_cpus();
  }
  
  /* Shuffle tasks across CPUs, with the intent of allowing each CPU in the
   * system to become idle at a time and cut off its timer ticks. This is meant
   * to test the support for such tickless idle CPU in RCU.
   */
  static int torture_shuffle(void *arg)
  {
  	VERBOSE_TOROUT_STRING("torture_shuffle task started");
  	do {
  		schedule_timeout_interruptible(shuffle_interval);
  		torture_shuffle_tasks();
  		torture_shutdown_absorb("torture_shuffle");
  	} while (!torture_must_stop());
7fafaac5b   Paul E. McKenney   rcutorture: Fix r...
441
  	torture_kthread_stopping("torture_shuffle");
3808dc9fa   Paul E. McKenney   rcutorture: Abstr...
442
443
444
445
446
447
448
449
  	return 0;
  }
  
  /*
   * Start the shuffler, with shuffint in jiffies.
   */
  int torture_shuffle_init(long shuffint)
  {
3808dc9fa   Paul E. McKenney   rcutorture: Abstr...
450
451
452
453
454
455
456
457
458
459
  	shuffle_interval = shuffint;
  
  	shuffle_idle_cpu = -1;
  
  	if (!alloc_cpumask_var(&shuffle_tmp_mask, GFP_KERNEL)) {
  		VERBOSE_TOROUT_ERRSTRING("Failed to alloc mask");
  		return -ENOMEM;
  	}
  
  	/* Create the shuffler thread */
47cf29b9e   Paul E. McKenney   rcutorture: Abstr...
460
  	return torture_create_kthread(torture_shuffle, NULL, shuffler_task);
3808dc9fa   Paul E. McKenney   rcutorture: Abstr...
461
462
463
464
465
466
  }
  EXPORT_SYMBOL_GPL(torture_shuffle_init);
  
  /*
   * Stop the shuffling.
   */
cc47ae083   Paul E. McKenney   rcutorture: Abstr...
467
  static void torture_shuffle_cleanup(void)
3808dc9fa   Paul E. McKenney   rcutorture: Abstr...
468
469
470
471
472
473
474
475
476
  {
  	torture_shuffle_task_unregister_all();
  	if (shuffler_task) {
  		VERBOSE_TOROUT_STRING("Stopping torture_shuffle task");
  		kthread_stop(shuffler_task);
  		free_cpumask_var(shuffle_tmp_mask);
  	}
  	shuffler_task = NULL;
  }
3808dc9fa   Paul E. McKenney   rcutorture: Abstr...
477
478
  
  /*
e991dbc07   Paul E. McKenney   rcutorture: Abstr...
479
480
481
   * Variables for auto-shutdown.  This allows "lights out" torture runs
   * to be fully scripted.
   */
e991dbc07   Paul E. McKenney   rcutorture: Abstr...
482
  static struct task_struct *shutdown_task;
31257c3c8   Paul E. McKenney   torture: Convert ...
483
  static ktime_t shutdown_time;		/* time to system shutdown. */
e991dbc07   Paul E. McKenney   rcutorture: Abstr...
484
485
486
  static void (*torture_shutdown_hook)(void);
  
  /*
f67a33561   Paul E. McKenney   rcutorture: Abstr...
487
488
489
490
491
   * Absorb kthreads into a kernel function that won't return, so that
   * they won't ever access module text or data again.
   */
  void torture_shutdown_absorb(const char *title)
  {
7d0ae8086   Paul E. McKenney   rcu: Convert ACCE...
492
  	while (READ_ONCE(fullstop) == FULLSTOP_SHUTDOWN) {
4622b487e   Paul E. McKenney   rcutorture: Abstr...
493
494
495
  		pr_notice("torture thread %s parking due to system shutdown
  ",
  			  title);
f67a33561   Paul E. McKenney   rcutorture: Abstr...
496
497
498
499
  		schedule_timeout_uninterruptible(MAX_SCHEDULE_TIMEOUT);
  	}
  }
  EXPORT_SYMBOL_GPL(torture_shutdown_absorb);
b5daa8f3b   Paul E. McKenney   rcutorture: Abstr...
500
501
  
  /*
e991dbc07   Paul E. McKenney   rcutorture: Abstr...
502
503
504
505
506
   * Cause the torture test to shutdown the system after the test has
   * run for the time specified by the shutdown_secs parameter.
   */
  static int torture_shutdown(void *arg)
  {
31257c3c8   Paul E. McKenney   torture: Convert ...
507
  	ktime_t ktime_snap;
e991dbc07   Paul E. McKenney   rcutorture: Abstr...
508
509
  
  	VERBOSE_TOROUT_STRING("torture_shutdown task started");
31257c3c8   Paul E. McKenney   torture: Convert ...
510
511
  	ktime_snap = ktime_get();
  	while (ktime_before(ktime_snap, shutdown_time) &&
e991dbc07   Paul E. McKenney   rcutorture: Abstr...
512
  	       !torture_must_stop()) {
e991dbc07   Paul E. McKenney   rcutorture: Abstr...
513
514
  		if (verbose)
  			pr_alert("%s" TORTURE_FLAG
31257c3c8   Paul E. McKenney   torture: Convert ...
515
516
517
518
519
520
521
  				 "torture_shutdown task: %llu ms remaining
  ",
  				 torture_type,
  				 ktime_ms_delta(shutdown_time, ktime_snap));
  		set_current_state(TASK_INTERRUPTIBLE);
  		schedule_hrtimeout(&shutdown_time, HRTIMER_MODE_ABS);
  		ktime_snap = ktime_get();
e991dbc07   Paul E. McKenney   rcutorture: Abstr...
522
523
  	}
  	if (torture_must_stop()) {
7fafaac5b   Paul E. McKenney   rcutorture: Fix r...
524
  		torture_kthread_stopping("torture_shutdown");
e991dbc07   Paul E. McKenney   rcutorture: Abstr...
525
526
527
528
529
530
531
  		return 0;
  	}
  
  	/* OK, shut down the system. */
  
  	VERBOSE_TOROUT_STRING("torture_shutdown task shutting down system");
  	shutdown_task = NULL;	/* Avoid self-kill deadlock. */
f881825a7   Paul E. McKenney   rcutorture: Grace...
532
533
534
535
  	if (torture_shutdown_hook)
  		torture_shutdown_hook();
  	else
  		VERBOSE_TOROUT_STRING("No torture_shutdown_hook(), skipping.");
2102ad290   Paul E. McKenney   torture: Dump ftr...
536
537
  	if (ftrace_dump_at_shutdown)
  		rcu_ftrace_dump(DUMP_ALL);
e991dbc07   Paul E. McKenney   rcutorture: Abstr...
538
539
540
541
542
543
544
545
546
  	kernel_power_off();	/* Shut down the system. */
  	return 0;
  }
  
  /*
   * Start up the shutdown task.
   */
  int torture_shutdown_init(int ssecs, void (*cleanup)(void))
  {
e991dbc07   Paul E. McKenney   rcutorture: Abstr...
547
  	torture_shutdown_hook = cleanup;
31257c3c8   Paul E. McKenney   torture: Convert ...
548
549
  	if (ssecs > 0) {
  		shutdown_time = ktime_add(ktime_get(), ktime_set(ssecs, 0));
2a7d96881   Pierce Griffiths   torture: Remove u...
550
  		return torture_create_kthread(torture_shutdown, NULL,
47cf29b9e   Paul E. McKenney   rcutorture: Abstr...
551
  					     shutdown_task);
e991dbc07   Paul E. McKenney   rcutorture: Abstr...
552
  	}
2a7d96881   Pierce Griffiths   torture: Remove u...
553
  	return 0;
e991dbc07   Paul E. McKenney   rcutorture: Abstr...
554
555
556
557
  }
  EXPORT_SYMBOL_GPL(torture_shutdown_init);
  
  /*
4622b487e   Paul E. McKenney   rcutorture: Abstr...
558
559
560
561
562
563
   * Detect and respond to a system shutdown.
   */
  static int torture_shutdown_notify(struct notifier_block *unused1,
  				   unsigned long unused2, void *unused3)
  {
  	mutex_lock(&fullstop_mutex);
7d0ae8086   Paul E. McKenney   rcu: Convert ACCE...
564
  	if (READ_ONCE(fullstop) == FULLSTOP_DONTSTOP) {
fac480efc   Paul E. McKenney   rcutorture: Add d...
565
  		VERBOSE_TOROUT_STRING("Unscheduled system shutdown detected");
7d0ae8086   Paul E. McKenney   rcu: Convert ACCE...
566
  		WRITE_ONCE(fullstop, FULLSTOP_SHUTDOWN);
fac480efc   Paul E. McKenney   rcutorture: Add d...
567
  	} else {
4622b487e   Paul E. McKenney   rcutorture: Abstr...
568
569
  		pr_warn("Concurrent rmmod and shutdown illegal!
  ");
fac480efc   Paul E. McKenney   rcutorture: Add d...
570
  	}
4622b487e   Paul E. McKenney   rcutorture: Abstr...
571
572
573
574
575
576
577
578
579
  	mutex_unlock(&fullstop_mutex);
  	return NOTIFY_DONE;
  }
  
  static struct notifier_block torture_shutdown_nb = {
  	.notifier_call = torture_shutdown_notify,
  };
  
  /*
bfefc73aa   Paul E. McKenney   rcutorture: Stop ...
580
581
582
583
584
585
586
587
588
589
590
591
592
593
   * Shut down the shutdown task.  Say what???  Heh!  This can happen if
   * the torture module gets an rmmod before the shutdown time arrives.  ;-)
   */
  static void torture_shutdown_cleanup(void)
  {
  	unregister_reboot_notifier(&torture_shutdown_nb);
  	if (shutdown_task != NULL) {
  		VERBOSE_TOROUT_STRING("Stopping torture_shutdown task");
  		kthread_stop(shutdown_task);
  	}
  	shutdown_task = NULL;
  }
  
  /*
628edaa50   Paul E. McKenney   rcutorture: Abstr...
594
595
596
597
598
599
600
   * Variables for stuttering, which means to periodically pause and
   * restart testing in order to catch bugs that appear when load is
   * suddenly applied to or removed from the system.
   */
  static struct task_struct *stutter_task;
  static int stutter_pause_test;
  static int stutter;
ff3bf92d9   Paul E. McKenney   torture: Allow in...
601
  static int stutter_gap;
628edaa50   Paul E. McKenney   rcutorture: Abstr...
602
603
604
605
606
  
  /*
   * Block until the stutter interval ends.  This must be called periodically
   * by all running kthreads that need to be subject to stuttering.
   */
474e59b47   Paul E. McKenney   rcutorture: Check...
607
  bool stutter_wait(const char *title)
628edaa50   Paul E. McKenney   rcutorture: Abstr...
608
  {
4ced3314f   Paul E. McKenney   torture: Make stu...
609
  	int spt;
e8516c64f   Paul E. McKenney   rcutorture: Fix s...
610
  	bool ret = false;
4ced3314f   Paul E. McKenney   torture: Make stu...
611

cee439398   Paul E. McKenney   rcu: Rename cond_...
612
  	cond_resched_tasks_rcu_qs();
4ced3314f   Paul E. McKenney   torture: Make stu...
613
  	spt = READ_ONCE(stutter_pause_test);
29d393908   Paul E. McKenney   torture: Save a l...
614
  	for (; spt; spt = READ_ONCE(stutter_pause_test)) {
e8516c64f   Paul E. McKenney   rcutorture: Fix s...
615
  		ret = true;
4ced3314f   Paul E. McKenney   torture: Make stu...
616
617
618
619
620
621
  		if (spt == 1) {
  			schedule_timeout_interruptible(1);
  		} else if (spt == 2) {
  			while (READ_ONCE(stutter_pause_test))
  				cond_resched();
  		} else {
628edaa50   Paul E. McKenney   rcutorture: Abstr...
622
  			schedule_timeout_interruptible(round_jiffies_relative(HZ));
4ced3314f   Paul E. McKenney   torture: Make stu...
623
  		}
628edaa50   Paul E. McKenney   rcutorture: Abstr...
624
625
  		torture_shutdown_absorb(title);
  	}
e8516c64f   Paul E. McKenney   rcutorture: Fix s...
626
  	return ret;
628edaa50   Paul E. McKenney   rcutorture: Abstr...
627
628
629
630
631
632
633
634
635
  }
  EXPORT_SYMBOL_GPL(stutter_wait);
  
  /*
   * Cause the torture test to "stutter", starting and stopping all
   * threads periodically.
   */
  static int torture_stutter(void *arg)
  {
e8516c64f   Paul E. McKenney   rcutorture: Fix s...
636
  	int wtime;
628edaa50   Paul E. McKenney   rcutorture: Abstr...
637
638
  	VERBOSE_TOROUT_STRING("torture_stutter task started");
  	do {
4ced3314f   Paul E. McKenney   torture: Make stu...
639
  		if (!torture_must_stop() && stutter > 1) {
e8516c64f   Paul E. McKenney   rcutorture: Fix s...
640
641
642
643
644
645
646
  			wtime = stutter;
  			if (stutter > HZ + 1) {
  				WRITE_ONCE(stutter_pause_test, 1);
  				wtime = stutter - HZ - 1;
  				schedule_timeout_interruptible(wtime);
  				wtime = HZ + 1;
  			}
4ced3314f   Paul E. McKenney   torture: Make stu...
647
  			WRITE_ONCE(stutter_pause_test, 2);
e8516c64f   Paul E. McKenney   rcutorture: Fix s...
648
  			schedule_timeout_interruptible(wtime);
628edaa50   Paul E. McKenney   rcutorture: Abstr...
649
  		}
4ced3314f   Paul E. McKenney   torture: Make stu...
650
  		WRITE_ONCE(stutter_pause_test, 0);
628edaa50   Paul E. McKenney   rcutorture: Abstr...
651
  		if (!torture_must_stop())
ff3bf92d9   Paul E. McKenney   torture: Allow in...
652
  			schedule_timeout_interruptible(stutter_gap);
628edaa50   Paul E. McKenney   rcutorture: Abstr...
653
654
  		torture_shutdown_absorb("torture_stutter");
  	} while (!torture_must_stop());
7fafaac5b   Paul E. McKenney   rcutorture: Fix r...
655
  	torture_kthread_stopping("torture_stutter");
628edaa50   Paul E. McKenney   rcutorture: Abstr...
656
657
658
659
660
661
  	return 0;
  }
  
  /*
   * Initialize and kick off the torture_stutter kthread.
   */
ff3bf92d9   Paul E. McKenney   torture: Allow in...
662
  int torture_stutter_init(const int s, const int sgap)
628edaa50   Paul E. McKenney   rcutorture: Abstr...
663
  {
628edaa50   Paul E. McKenney   rcutorture: Abstr...
664
  	stutter = s;
ff3bf92d9   Paul E. McKenney   torture: Allow in...
665
  	stutter_gap = sgap;
2a7d96881   Pierce Griffiths   torture: Remove u...
666
  	return torture_create_kthread(torture_stutter, NULL, stutter_task);
628edaa50   Paul E. McKenney   rcutorture: Abstr...
667
668
669
670
671
672
  }
  EXPORT_SYMBOL_GPL(torture_stutter_init);
  
  /*
   * Cleanup after the torture_stutter kthread.
   */
bfefc73aa   Paul E. McKenney   rcutorture: Stop ...
673
  static void torture_stutter_cleanup(void)
628edaa50   Paul E. McKenney   rcutorture: Abstr...
674
675
676
677
678
679
680
  {
  	if (!stutter_task)
  		return;
  	VERBOSE_TOROUT_STRING("Stopping torture_stutter task");
  	kthread_stop(stutter_task);
  	stutter_task = NULL;
  }
628edaa50   Paul E. McKenney   rcutorture: Abstr...
681
682
  
  /*
b5daa8f3b   Paul E. McKenney   rcutorture: Abstr...
683
684
685
686
   * Initialize torture module.  Please note that this is -not- invoked via
   * the usual module_init() mechanism, but rather by an explicit call from
   * the client torture module.  This call must be paired with a later
   * torture_init_end().
628edaa50   Paul E. McKenney   rcutorture: Abstr...
687
688
689
   *
   * The runnable parameter points to a flag that controls whether or not
   * the test is currently runnable.  If there is no such flag, pass in NULL.
b5daa8f3b   Paul E. McKenney   rcutorture: Abstr...
690
   */
90127d605   Paul E. McKenney   torture: Make onl...
691
  bool torture_init_begin(char *ttype, int v)
b5daa8f3b   Paul E. McKenney   rcutorture: Abstr...
692
693
  {
  	mutex_lock(&fullstop_mutex);
5228084ee   Paul E. McKenney   torture: Check fo...
694
  	if (torture_type != NULL) {
9eb5188a0   Paul E. McKenney   torture: Clarify ...
695
696
  		pr_alert("torture_init_begin: Refusing %s init: %s running.
  ",
5228084ee   Paul E. McKenney   torture: Check fo...
697
  			 ttype, torture_type);
9eb5188a0   Paul E. McKenney   torture: Clarify ...
698
699
  		pr_alert("torture_init_begin: One torture test at a time!
  ");
5228084ee   Paul E. McKenney   torture: Check fo...
700
701
702
  		mutex_unlock(&fullstop_mutex);
  		return false;
  	}
b5daa8f3b   Paul E. McKenney   rcutorture: Abstr...
703
704
  	torture_type = ttype;
  	verbose = v;
36970bb91   Paul E. McKenney   rcutorture: Priva...
705
  	fullstop = FULLSTOP_DONTSTOP;
5228084ee   Paul E. McKenney   torture: Check fo...
706
  	return true;
b5daa8f3b   Paul E. McKenney   rcutorture: Abstr...
707
708
709
710
711
712
  }
  EXPORT_SYMBOL_GPL(torture_init_begin);
  
  /*
   * Tell the torture module that initialization is complete.
   */
2b3f8ffe4   Pranith Kumar   torture: Remove _...
713
  void torture_init_end(void)
b5daa8f3b   Paul E. McKenney   rcutorture: Abstr...
714
715
  {
  	mutex_unlock(&fullstop_mutex);
4622b487e   Paul E. McKenney   rcutorture: Abstr...
716
  	register_reboot_notifier(&torture_shutdown_nb);
b5daa8f3b   Paul E. McKenney   rcutorture: Abstr...
717
718
  }
  EXPORT_SYMBOL_GPL(torture_init_end);
cc47ae083   Paul E. McKenney   rcutorture: Abstr...
719
720
721
722
723
  
  /*
   * Clean up torture module.  Please note that this is -not- invoked via
   * the usual module_exit() mechanism, but rather by an explicit call from
   * the client torture module.  Returns true if a race with system shutdown
bfefc73aa   Paul E. McKenney   rcutorture: Stop ...
724
725
   * is detected, otherwise, all kthreads started by functions in this file
   * will be shut down.
cc47ae083   Paul E. McKenney   rcutorture: Abstr...
726
727
728
   *
   * This must be called before the caller starts shutting down its own
   * kthreads.
d36a7a0d5   Davidlohr Bueso   torture: Address ...
729
730
731
732
733
   *
   * Both torture_cleanup_begin() and torture_cleanup_end() must be paired,
   * in order to correctly perform the cleanup. They are separated because
   * threads can still need to reference the torture_type type, thus nullify
   * only after completing all other relevant calls.
cc47ae083   Paul E. McKenney   rcutorture: Abstr...
734
   */
d36a7a0d5   Davidlohr Bueso   torture: Address ...
735
  bool torture_cleanup_begin(void)
cc47ae083   Paul E. McKenney   rcutorture: Abstr...
736
737
  {
  	mutex_lock(&fullstop_mutex);
7d0ae8086   Paul E. McKenney   rcu: Convert ACCE...
738
  	if (READ_ONCE(fullstop) == FULLSTOP_SHUTDOWN) {
cc47ae083   Paul E. McKenney   rcutorture: Abstr...
739
740
741
742
743
744
  		pr_warn("Concurrent rmmod and shutdown illegal!
  ");
  		mutex_unlock(&fullstop_mutex);
  		schedule_timeout_uninterruptible(10);
  		return true;
  	}
7d0ae8086   Paul E. McKenney   rcu: Convert ACCE...
745
  	WRITE_ONCE(fullstop, FULLSTOP_RMMOD);
cc47ae083   Paul E. McKenney   rcutorture: Abstr...
746
  	mutex_unlock(&fullstop_mutex);
bfefc73aa   Paul E. McKenney   rcutorture: Stop ...
747
  	torture_shutdown_cleanup();
cc47ae083   Paul E. McKenney   rcutorture: Abstr...
748
  	torture_shuffle_cleanup();
bfefc73aa   Paul E. McKenney   rcutorture: Stop ...
749
  	torture_stutter_cleanup();
cc47ae083   Paul E. McKenney   rcutorture: Abstr...
750
  	torture_onoff_cleanup();
d36a7a0d5   Davidlohr Bueso   torture: Address ...
751
752
753
754
755
756
  	return false;
  }
  EXPORT_SYMBOL_GPL(torture_cleanup_begin);
  
  void torture_cleanup_end(void)
  {
5228084ee   Paul E. McKenney   torture: Check fo...
757
758
759
  	mutex_lock(&fullstop_mutex);
  	torture_type = NULL;
  	mutex_unlock(&fullstop_mutex);
cc47ae083   Paul E. McKenney   rcutorture: Abstr...
760
  }
d36a7a0d5   Davidlohr Bueso   torture: Address ...
761
  EXPORT_SYMBOL_GPL(torture_cleanup_end);
36970bb91   Paul E. McKenney   rcutorture: Priva...
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
  
  /*
   * Is it time for the current torture test to stop?
   */
  bool torture_must_stop(void)
  {
  	return torture_must_stop_irq() || kthread_should_stop();
  }
  EXPORT_SYMBOL_GPL(torture_must_stop);
  
  /*
   * Is it time for the current torture test to stop?  This is the irq-safe
   * version, hence no check for kthread_should_stop().
   */
  bool torture_must_stop_irq(void)
  {
7d0ae8086   Paul E. McKenney   rcu: Convert ACCE...
778
  	return READ_ONCE(fullstop) != FULLSTOP_DONTSTOP;
36970bb91   Paul E. McKenney   rcutorture: Priva...
779
780
  }
  EXPORT_SYMBOL_GPL(torture_must_stop_irq);
7fafaac5b   Paul E. McKenney   rcutorture: Fix r...
781
782
783
784
785
786
787
788
789
790
  
  /*
   * Each kthread must wait for kthread_should_stop() before returning from
   * its top-level function, otherwise segfaults ensue.  This function
   * prints a "stopping" message and waits for kthread_should_stop(), and
   * should be called from all torture kthreads immediately prior to
   * returning.
   */
  void torture_kthread_stopping(char *title)
  {
0d6821d5f   Paul E. McKenney   torture: Include ...
791
792
793
794
  	char buf[128];
  
  	snprintf(buf, sizeof(buf), "Stopping %s", title);
  	VERBOSE_TOROUT_STRING(buf);
7fafaac5b   Paul E. McKenney   rcutorture: Fix r...
795
796
797
798
799
800
  	while (!kthread_should_stop()) {
  		torture_shutdown_absorb(title);
  		schedule_timeout_uninterruptible(1);
  	}
  }
  EXPORT_SYMBOL_GPL(torture_kthread_stopping);
47cf29b9e   Paul E. McKenney   rcutorture: Abstr...
801
802
803
804
805
806
807
808
809
810
811
812
  
  /*
   * Create a generic torture kthread that is immediately runnable.  If you
   * need the kthread to be stopped so that you can do something to it before
   * it starts, you will need to open-code your own.
   */
  int _torture_create_kthread(int (*fn)(void *arg), void *arg, char *s, char *m,
  			    char *f, struct task_struct **tp)
  {
  	int ret = 0;
  
  	VERBOSE_TOROUT_STRING(m);
6945915e7   Kees Cook   torture: Avoid fo...
813
  	*tp = kthread_run(fn, arg, "%s", s);
47cf29b9e   Paul E. McKenney   rcutorture: Abstr...
814
815
816
817
818
819
820
821
822
  	if (IS_ERR(*tp)) {
  		ret = PTR_ERR(*tp);
  		VERBOSE_TOROUT_ERRSTRING(f);
  		*tp = NULL;
  	}
  	torture_shuffle_task_register(*tp);
  	return ret;
  }
  EXPORT_SYMBOL_GPL(_torture_create_kthread);
9c029b860   Paul E. McKenney   rcutorture: Abstr...
823
824
825
826
827
828
829
830
831
832
833
834
835
  
  /*
   * Stop a generic kthread, emitting a message.
   */
  void _torture_stop_kthread(char *m, struct task_struct **tp)
  {
  	if (*tp == NULL)
  		return;
  	VERBOSE_TOROUT_STRING(m);
  	kthread_stop(*tp);
  	*tp = NULL;
  }
  EXPORT_SYMBOL_GPL(_torture_stop_kthread);