rcutorture: Abstract rcu_torture_random()
[deliverable/linux.git] / kernel / rcu / rcutorture.c
1 /*
2 * Read-Copy Update module-based torture test facility
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, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 *
18 * Copyright (C) IBM Corporation, 2005, 2006
19 *
20 * Authors: Paul E. McKenney <paulmck@us.ibm.com>
21 * Josh Triplett <josh@freedesktop.org>
22 *
23 * See also: Documentation/RCU/torture.txt
24 */
25 #include <linux/types.h>
26 #include <linux/kernel.h>
27 #include <linux/init.h>
28 #include <linux/module.h>
29 #include <linux/kthread.h>
30 #include <linux/err.h>
31 #include <linux/spinlock.h>
32 #include <linux/smp.h>
33 #include <linux/rcupdate.h>
34 #include <linux/interrupt.h>
35 #include <linux/sched.h>
36 #include <linux/atomic.h>
37 #include <linux/bitops.h>
38 #include <linux/completion.h>
39 #include <linux/moduleparam.h>
40 #include <linux/percpu.h>
41 #include <linux/notifier.h>
42 #include <linux/reboot.h>
43 #include <linux/freezer.h>
44 #include <linux/cpu.h>
45 #include <linux/delay.h>
46 #include <linux/stat.h>
47 #include <linux/srcu.h>
48 #include <linux/slab.h>
49 #include <linux/trace_clock.h>
50 #include <asm/byteorder.h>
51 #include <linux/torture.h>
52
53 MODULE_LICENSE("GPL");
54 MODULE_AUTHOR("Paul E. McKenney <paulmck@us.ibm.com> and Josh Triplett <josh@freedesktop.org>");
55
56 MODULE_ALIAS("rcutorture");
57 #ifdef MODULE_PARAM_PREFIX
58 #undef MODULE_PARAM_PREFIX
59 #endif
60 #define MODULE_PARAM_PREFIX "rcutorture."
61
62 static int fqs_duration;
63 module_param(fqs_duration, int, 0444);
64 MODULE_PARM_DESC(fqs_duration, "Duration of fqs bursts (us), 0 to disable");
65 static int fqs_holdoff;
66 module_param(fqs_holdoff, int, 0444);
67 MODULE_PARM_DESC(fqs_holdoff, "Holdoff time within fqs bursts (us)");
68 static int fqs_stutter = 3;
69 module_param(fqs_stutter, int, 0444);
70 MODULE_PARM_DESC(fqs_stutter, "Wait time between fqs bursts (s)");
71 static bool gp_exp;
72 module_param(gp_exp, bool, 0444);
73 MODULE_PARM_DESC(gp_exp, "Use expedited GP wait primitives");
74 static bool gp_normal;
75 module_param(gp_normal, bool, 0444);
76 MODULE_PARM_DESC(gp_normal, "Use normal (non-expedited) GP wait primitives");
77 static int irqreader = 1;
78 module_param(irqreader, int, 0444);
79 MODULE_PARM_DESC(irqreader, "Allow RCU readers from irq handlers");
80 static int n_barrier_cbs;
81 module_param(n_barrier_cbs, int, 0444);
82 MODULE_PARM_DESC(n_barrier_cbs, "# of callbacks/kthreads for barrier testing");
83 static int nfakewriters = 4;
84 module_param(nfakewriters, int, 0444);
85 MODULE_PARM_DESC(nfakewriters, "Number of RCU fake writer threads");
86 static int nreaders = -1;
87 module_param(nreaders, int, 0444);
88 MODULE_PARM_DESC(nreaders, "Number of RCU reader threads");
89 static int object_debug;
90 module_param(object_debug, int, 0444);
91 MODULE_PARM_DESC(object_debug, "Enable debug-object double call_rcu() testing");
92 static int onoff_holdoff;
93 module_param(onoff_holdoff, int, 0444);
94 MODULE_PARM_DESC(onoff_holdoff, "Time after boot before CPU hotplugs (s)");
95 static int onoff_interval;
96 module_param(onoff_interval, int, 0444);
97 MODULE_PARM_DESC(onoff_interval, "Time between CPU hotplugs (s), 0=disable");
98 static int shuffle_interval = 3;
99 module_param(shuffle_interval, int, 0444);
100 MODULE_PARM_DESC(shuffle_interval, "Number of seconds between shuffles");
101 static int shutdown_secs;
102 module_param(shutdown_secs, int, 0444);
103 MODULE_PARM_DESC(shutdown_secs, "Shutdown time (s), <= zero to disable.");
104 static int stall_cpu;
105 module_param(stall_cpu, int, 0444);
106 MODULE_PARM_DESC(stall_cpu, "Stall duration (s), zero to disable.");
107 static int stall_cpu_holdoff = 10;
108 module_param(stall_cpu_holdoff, int, 0444);
109 MODULE_PARM_DESC(stall_cpu_holdoff, "Time to wait before starting stall (s).");
110 static int stat_interval = 60;
111 module_param(stat_interval, int, 0644);
112 MODULE_PARM_DESC(stat_interval, "Number of seconds between stats printk()s");
113 static int stutter = 5;
114 module_param(stutter, int, 0444);
115 MODULE_PARM_DESC(stutter, "Number of seconds to run/halt test");
116 static int test_boost = 1;
117 module_param(test_boost, int, 0444);
118 MODULE_PARM_DESC(test_boost, "Test RCU prio boost: 0=no, 1=maybe, 2=yes.");
119 static int test_boost_duration = 4;
120 module_param(test_boost_duration, int, 0444);
121 MODULE_PARM_DESC(test_boost_duration, "Duration of each boost test, seconds.");
122 static int test_boost_interval = 7;
123 module_param(test_boost_interval, int, 0444);
124 MODULE_PARM_DESC(test_boost_interval, "Interval between boost tests, seconds.");
125 static bool test_no_idle_hz = true;
126 module_param(test_no_idle_hz, bool, 0444);
127 MODULE_PARM_DESC(test_no_idle_hz, "Test support for tickless idle CPUs");
128 static char *torture_type = "rcu";
129 module_param(torture_type, charp, 0444);
130 MODULE_PARM_DESC(torture_type, "Type of RCU to torture (rcu, rcu_bh, ...)");
131 static bool verbose;
132 module_param(verbose, bool, 0444);
133 MODULE_PARM_DESC(verbose, "Enable verbose debugging printk()s");
134
135 #define TORTURE_FLAG "-torture:"
136 #define PRINTK_STRING(s) \
137 do { pr_alert("%s" TORTURE_FLAG s "\n", torture_type); } while (0)
138 #define VERBOSE_PRINTK_STRING(s) \
139 do { if (verbose) pr_alert("%s" TORTURE_FLAG s "\n", torture_type); } while (0)
140 #define VERBOSE_PRINTK_ERRSTRING(s) \
141 do { if (verbose) pr_alert("%s" TORTURE_FLAG "!!! " s "\n", torture_type); } while (0)
142
143 static int nrealreaders;
144 static struct task_struct *writer_task;
145 static struct task_struct **fakewriter_tasks;
146 static struct task_struct **reader_tasks;
147 static struct task_struct *stats_task;
148 static struct task_struct *shuffler_task;
149 static struct task_struct *stutter_task;
150 static struct task_struct *fqs_task;
151 static struct task_struct *boost_tasks[NR_CPUS];
152 static struct task_struct *shutdown_task;
153 #ifdef CONFIG_HOTPLUG_CPU
154 static struct task_struct *onoff_task;
155 #endif /* #ifdef CONFIG_HOTPLUG_CPU */
156 static struct task_struct *stall_task;
157 static struct task_struct **barrier_cbs_tasks;
158 static struct task_struct *barrier_task;
159
160 #define RCU_TORTURE_PIPE_LEN 10
161
162 struct rcu_torture {
163 struct rcu_head rtort_rcu;
164 int rtort_pipe_count;
165 struct list_head rtort_free;
166 int rtort_mbtest;
167 };
168
169 static LIST_HEAD(rcu_torture_freelist);
170 static struct rcu_torture __rcu *rcu_torture_current;
171 static unsigned long rcu_torture_current_version;
172 static struct rcu_torture rcu_tortures[10 * RCU_TORTURE_PIPE_LEN];
173 static DEFINE_SPINLOCK(rcu_torture_lock);
174 static DEFINE_PER_CPU(long [RCU_TORTURE_PIPE_LEN + 1],
175 rcu_torture_count) = { 0 };
176 static DEFINE_PER_CPU(long [RCU_TORTURE_PIPE_LEN + 1],
177 rcu_torture_batch) = { 0 };
178 static atomic_t rcu_torture_wcount[RCU_TORTURE_PIPE_LEN + 1];
179 static atomic_t n_rcu_torture_alloc;
180 static atomic_t n_rcu_torture_alloc_fail;
181 static atomic_t n_rcu_torture_free;
182 static atomic_t n_rcu_torture_mberror;
183 static atomic_t n_rcu_torture_error;
184 static long n_rcu_torture_barrier_error;
185 static long n_rcu_torture_boost_ktrerror;
186 static long n_rcu_torture_boost_rterror;
187 static long n_rcu_torture_boost_failure;
188 static long n_rcu_torture_boosts;
189 static long n_rcu_torture_timers;
190 static long n_offline_attempts;
191 static long n_offline_successes;
192 static unsigned long sum_offline;
193 static int min_offline = -1;
194 static int max_offline;
195 static long n_online_attempts;
196 static long n_online_successes;
197 static unsigned long sum_online;
198 static int min_online = -1;
199 static int max_online;
200 static long n_barrier_attempts;
201 static long n_barrier_successes;
202 static struct list_head rcu_torture_removed;
203 static cpumask_var_t shuffle_tmp_mask;
204
205 static int stutter_pause_test;
206
207 #if defined(MODULE) || defined(CONFIG_RCU_TORTURE_TEST_RUNNABLE)
208 #define RCUTORTURE_RUNNABLE_INIT 1
209 #else
210 #define RCUTORTURE_RUNNABLE_INIT 0
211 #endif
212 int rcutorture_runnable = RCUTORTURE_RUNNABLE_INIT;
213 module_param(rcutorture_runnable, int, 0444);
214 MODULE_PARM_DESC(rcutorture_runnable, "Start rcutorture at boot");
215
216 #if defined(CONFIG_RCU_BOOST) && !defined(CONFIG_HOTPLUG_CPU)
217 #define rcu_can_boost() 1
218 #else /* #if defined(CONFIG_RCU_BOOST) && !defined(CONFIG_HOTPLUG_CPU) */
219 #define rcu_can_boost() 0
220 #endif /* #else #if defined(CONFIG_RCU_BOOST) && !defined(CONFIG_HOTPLUG_CPU) */
221
222 #ifdef CONFIG_RCU_TRACE
223 static u64 notrace rcu_trace_clock_local(void)
224 {
225 u64 ts = trace_clock_local();
226 unsigned long __maybe_unused ts_rem = do_div(ts, NSEC_PER_USEC);
227 return ts;
228 }
229 #else /* #ifdef CONFIG_RCU_TRACE */
230 static u64 notrace rcu_trace_clock_local(void)
231 {
232 return 0ULL;
233 }
234 #endif /* #else #ifdef CONFIG_RCU_TRACE */
235
236 static unsigned long shutdown_time; /* jiffies to system shutdown. */
237 static unsigned long boost_starttime; /* jiffies of next boost test start. */
238 DEFINE_MUTEX(boost_mutex); /* protect setting boost_starttime */
239 /* and boost task create/destroy. */
240 static atomic_t barrier_cbs_count; /* Barrier callbacks registered. */
241 static bool barrier_phase; /* Test phase. */
242 static atomic_t barrier_cbs_invoked; /* Barrier callbacks invoked. */
243 static wait_queue_head_t *barrier_cbs_wq; /* Coordinate barrier testing. */
244 static DECLARE_WAIT_QUEUE_HEAD(barrier_wq);
245
246 /* Mediate rmmod and system shutdown. Concurrent rmmod & shutdown illegal! */
247
248 #define FULLSTOP_DONTSTOP 0 /* Normal operation. */
249 #define FULLSTOP_SHUTDOWN 1 /* System shutdown with rcutorture running. */
250 #define FULLSTOP_RMMOD 2 /* Normal rmmod of rcutorture. */
251 static int fullstop = FULLSTOP_RMMOD;
252 /*
253 * Protect fullstop transitions and spawning of kthreads.
254 */
255 static DEFINE_MUTEX(fullstop_mutex);
256
257 /* Forward reference. */
258 static void rcu_torture_cleanup(void);
259
260 /*
261 * Detect and respond to a system shutdown.
262 */
263 static int
264 rcutorture_shutdown_notify(struct notifier_block *unused1,
265 unsigned long unused2, void *unused3)
266 {
267 mutex_lock(&fullstop_mutex);
268 if (fullstop == FULLSTOP_DONTSTOP)
269 fullstop = FULLSTOP_SHUTDOWN;
270 else
271 pr_warn(/* but going down anyway, so... */
272 "Concurrent 'rmmod rcutorture' and shutdown illegal!\n");
273 mutex_unlock(&fullstop_mutex);
274 return NOTIFY_DONE;
275 }
276
277 /*
278 * Absorb kthreads into a kernel function that won't return, so that
279 * they won't ever access module text or data again.
280 */
281 static void rcutorture_shutdown_absorb(const char *title)
282 {
283 if (ACCESS_ONCE(fullstop) == FULLSTOP_SHUTDOWN) {
284 pr_notice(
285 "rcutorture thread %s parking due to system shutdown\n",
286 title);
287 schedule_timeout_uninterruptible(MAX_SCHEDULE_TIMEOUT);
288 }
289 }
290
291 /*
292 * Allocate an element from the rcu_tortures pool.
293 */
294 static struct rcu_torture *
295 rcu_torture_alloc(void)
296 {
297 struct list_head *p;
298
299 spin_lock_bh(&rcu_torture_lock);
300 if (list_empty(&rcu_torture_freelist)) {
301 atomic_inc(&n_rcu_torture_alloc_fail);
302 spin_unlock_bh(&rcu_torture_lock);
303 return NULL;
304 }
305 atomic_inc(&n_rcu_torture_alloc);
306 p = rcu_torture_freelist.next;
307 list_del_init(p);
308 spin_unlock_bh(&rcu_torture_lock);
309 return container_of(p, struct rcu_torture, rtort_free);
310 }
311
312 /*
313 * Free an element to the rcu_tortures pool.
314 */
315 static void
316 rcu_torture_free(struct rcu_torture *p)
317 {
318 atomic_inc(&n_rcu_torture_free);
319 spin_lock_bh(&rcu_torture_lock);
320 list_add_tail(&p->rtort_free, &rcu_torture_freelist);
321 spin_unlock_bh(&rcu_torture_lock);
322 }
323
324 static void
325 rcu_stutter_wait(const char *title)
326 {
327 while (stutter_pause_test || !rcutorture_runnable) {
328 if (rcutorture_runnable)
329 schedule_timeout_interruptible(1);
330 else
331 schedule_timeout_interruptible(round_jiffies_relative(HZ));
332 rcutorture_shutdown_absorb(title);
333 }
334 }
335
336 /*
337 * Operations vector for selecting different types of tests.
338 */
339
340 struct rcu_torture_ops {
341 void (*init)(void);
342 int (*readlock)(void);
343 void (*read_delay)(struct torture_random_state *rrsp);
344 void (*readunlock)(int idx);
345 int (*completed)(void);
346 void (*deferred_free)(struct rcu_torture *p);
347 void (*sync)(void);
348 void (*exp_sync)(void);
349 void (*call)(struct rcu_head *head, void (*func)(struct rcu_head *rcu));
350 void (*cb_barrier)(void);
351 void (*fqs)(void);
352 void (*stats)(char *page);
353 int irq_capable;
354 int can_boost;
355 const char *name;
356 };
357
358 static struct rcu_torture_ops *cur_ops;
359
360 /*
361 * Definitions for rcu torture testing.
362 */
363
364 static int rcu_torture_read_lock(void) __acquires(RCU)
365 {
366 rcu_read_lock();
367 return 0;
368 }
369
370 static void rcu_read_delay(struct torture_random_state *rrsp)
371 {
372 const unsigned long shortdelay_us = 200;
373 const unsigned long longdelay_ms = 50;
374
375 /* We want a short delay sometimes to make a reader delay the grace
376 * period, and we want a long delay occasionally to trigger
377 * force_quiescent_state. */
378
379 if (!(torture_random(rrsp) % (nrealreaders * 2000 * longdelay_ms)))
380 mdelay(longdelay_ms);
381 if (!(torture_random(rrsp) % (nrealreaders * 2 * shortdelay_us)))
382 udelay(shortdelay_us);
383 #ifdef CONFIG_PREEMPT
384 if (!preempt_count() &&
385 !(torture_random(rrsp) % (nrealreaders * 20000)))
386 preempt_schedule(); /* No QS if preempt_disable() in effect */
387 #endif
388 }
389
390 static void rcu_torture_read_unlock(int idx) __releases(RCU)
391 {
392 rcu_read_unlock();
393 }
394
395 static int rcu_torture_completed(void)
396 {
397 return rcu_batches_completed();
398 }
399
400 static void
401 rcu_torture_cb(struct rcu_head *p)
402 {
403 int i;
404 struct rcu_torture *rp = container_of(p, struct rcu_torture, rtort_rcu);
405
406 if (fullstop != FULLSTOP_DONTSTOP) {
407 /* Test is ending, just drop callbacks on the floor. */
408 /* The next initialization will pick up the pieces. */
409 return;
410 }
411 i = rp->rtort_pipe_count;
412 if (i > RCU_TORTURE_PIPE_LEN)
413 i = RCU_TORTURE_PIPE_LEN;
414 atomic_inc(&rcu_torture_wcount[i]);
415 if (++rp->rtort_pipe_count >= RCU_TORTURE_PIPE_LEN) {
416 rp->rtort_mbtest = 0;
417 rcu_torture_free(rp);
418 } else {
419 cur_ops->deferred_free(rp);
420 }
421 }
422
423 static int rcu_no_completed(void)
424 {
425 return 0;
426 }
427
428 static void rcu_torture_deferred_free(struct rcu_torture *p)
429 {
430 call_rcu(&p->rtort_rcu, rcu_torture_cb);
431 }
432
433 static void rcu_sync_torture_init(void)
434 {
435 INIT_LIST_HEAD(&rcu_torture_removed);
436 }
437
438 static struct rcu_torture_ops rcu_ops = {
439 .init = rcu_sync_torture_init,
440 .readlock = rcu_torture_read_lock,
441 .read_delay = rcu_read_delay,
442 .readunlock = rcu_torture_read_unlock,
443 .completed = rcu_torture_completed,
444 .deferred_free = rcu_torture_deferred_free,
445 .sync = synchronize_rcu,
446 .exp_sync = synchronize_rcu_expedited,
447 .call = call_rcu,
448 .cb_barrier = rcu_barrier,
449 .fqs = rcu_force_quiescent_state,
450 .stats = NULL,
451 .irq_capable = 1,
452 .can_boost = rcu_can_boost(),
453 .name = "rcu"
454 };
455
456 /*
457 * Definitions for rcu_bh torture testing.
458 */
459
460 static int rcu_bh_torture_read_lock(void) __acquires(RCU_BH)
461 {
462 rcu_read_lock_bh();
463 return 0;
464 }
465
466 static void rcu_bh_torture_read_unlock(int idx) __releases(RCU_BH)
467 {
468 rcu_read_unlock_bh();
469 }
470
471 static int rcu_bh_torture_completed(void)
472 {
473 return rcu_batches_completed_bh();
474 }
475
476 static void rcu_bh_torture_deferred_free(struct rcu_torture *p)
477 {
478 call_rcu_bh(&p->rtort_rcu, rcu_torture_cb);
479 }
480
481 static struct rcu_torture_ops rcu_bh_ops = {
482 .init = rcu_sync_torture_init,
483 .readlock = rcu_bh_torture_read_lock,
484 .read_delay = rcu_read_delay, /* just reuse rcu's version. */
485 .readunlock = rcu_bh_torture_read_unlock,
486 .completed = rcu_bh_torture_completed,
487 .deferred_free = rcu_bh_torture_deferred_free,
488 .sync = synchronize_rcu_bh,
489 .exp_sync = synchronize_rcu_bh_expedited,
490 .call = call_rcu_bh,
491 .cb_barrier = rcu_barrier_bh,
492 .fqs = rcu_bh_force_quiescent_state,
493 .stats = NULL,
494 .irq_capable = 1,
495 .name = "rcu_bh"
496 };
497
498 /*
499 * Definitions for srcu torture testing.
500 */
501
502 DEFINE_STATIC_SRCU(srcu_ctl);
503
504 static int srcu_torture_read_lock(void) __acquires(&srcu_ctl)
505 {
506 return srcu_read_lock(&srcu_ctl);
507 }
508
509 static void srcu_read_delay(struct torture_random_state *rrsp)
510 {
511 long delay;
512 const long uspertick = 1000000 / HZ;
513 const long longdelay = 10;
514
515 /* We want there to be long-running readers, but not all the time. */
516
517 delay = torture_random(rrsp) %
518 (nrealreaders * 2 * longdelay * uspertick);
519 if (!delay)
520 schedule_timeout_interruptible(longdelay);
521 else
522 rcu_read_delay(rrsp);
523 }
524
525 static void srcu_torture_read_unlock(int idx) __releases(&srcu_ctl)
526 {
527 srcu_read_unlock(&srcu_ctl, idx);
528 }
529
530 static int srcu_torture_completed(void)
531 {
532 return srcu_batches_completed(&srcu_ctl);
533 }
534
535 static void srcu_torture_deferred_free(struct rcu_torture *rp)
536 {
537 call_srcu(&srcu_ctl, &rp->rtort_rcu, rcu_torture_cb);
538 }
539
540 static void srcu_torture_synchronize(void)
541 {
542 synchronize_srcu(&srcu_ctl);
543 }
544
545 static void srcu_torture_call(struct rcu_head *head,
546 void (*func)(struct rcu_head *head))
547 {
548 call_srcu(&srcu_ctl, head, func);
549 }
550
551 static void srcu_torture_barrier(void)
552 {
553 srcu_barrier(&srcu_ctl);
554 }
555
556 static void srcu_torture_stats(char *page)
557 {
558 int cpu;
559 int idx = srcu_ctl.completed & 0x1;
560
561 page += sprintf(page, "%s%s per-CPU(idx=%d):",
562 torture_type, TORTURE_FLAG, idx);
563 for_each_possible_cpu(cpu) {
564 page += sprintf(page, " %d(%lu,%lu)", cpu,
565 per_cpu_ptr(srcu_ctl.per_cpu_ref, cpu)->c[!idx],
566 per_cpu_ptr(srcu_ctl.per_cpu_ref, cpu)->c[idx]);
567 }
568 sprintf(page, "\n");
569 }
570
571 static void srcu_torture_synchronize_expedited(void)
572 {
573 synchronize_srcu_expedited(&srcu_ctl);
574 }
575
576 static struct rcu_torture_ops srcu_ops = {
577 .init = rcu_sync_torture_init,
578 .readlock = srcu_torture_read_lock,
579 .read_delay = srcu_read_delay,
580 .readunlock = srcu_torture_read_unlock,
581 .completed = srcu_torture_completed,
582 .deferred_free = srcu_torture_deferred_free,
583 .sync = srcu_torture_synchronize,
584 .exp_sync = srcu_torture_synchronize_expedited,
585 .call = srcu_torture_call,
586 .cb_barrier = srcu_torture_barrier,
587 .stats = srcu_torture_stats,
588 .name = "srcu"
589 };
590
591 /*
592 * Definitions for sched torture testing.
593 */
594
595 static int sched_torture_read_lock(void)
596 {
597 preempt_disable();
598 return 0;
599 }
600
601 static void sched_torture_read_unlock(int idx)
602 {
603 preempt_enable();
604 }
605
606 static void rcu_sched_torture_deferred_free(struct rcu_torture *p)
607 {
608 call_rcu_sched(&p->rtort_rcu, rcu_torture_cb);
609 }
610
611 static struct rcu_torture_ops sched_ops = {
612 .init = rcu_sync_torture_init,
613 .readlock = sched_torture_read_lock,
614 .read_delay = rcu_read_delay, /* just reuse rcu's version. */
615 .readunlock = sched_torture_read_unlock,
616 .completed = rcu_no_completed,
617 .deferred_free = rcu_sched_torture_deferred_free,
618 .sync = synchronize_sched,
619 .exp_sync = synchronize_sched_expedited,
620 .call = call_rcu_sched,
621 .cb_barrier = rcu_barrier_sched,
622 .fqs = rcu_sched_force_quiescent_state,
623 .stats = NULL,
624 .irq_capable = 1,
625 .name = "sched"
626 };
627
628 /*
629 * RCU torture priority-boost testing. Runs one real-time thread per
630 * CPU for moderate bursts, repeatedly registering RCU callbacks and
631 * spinning waiting for them to be invoked. If a given callback takes
632 * too long to be invoked, we assume that priority inversion has occurred.
633 */
634
635 struct rcu_boost_inflight {
636 struct rcu_head rcu;
637 int inflight;
638 };
639
640 static void rcu_torture_boost_cb(struct rcu_head *head)
641 {
642 struct rcu_boost_inflight *rbip =
643 container_of(head, struct rcu_boost_inflight, rcu);
644
645 smp_mb(); /* Ensure RCU-core accesses precede clearing ->inflight */
646 rbip->inflight = 0;
647 }
648
649 static int rcu_torture_boost(void *arg)
650 {
651 unsigned long call_rcu_time;
652 unsigned long endtime;
653 unsigned long oldstarttime;
654 struct rcu_boost_inflight rbi = { .inflight = 0 };
655 struct sched_param sp;
656
657 VERBOSE_PRINTK_STRING("rcu_torture_boost started");
658
659 /* Set real-time priority. */
660 sp.sched_priority = 1;
661 if (sched_setscheduler(current, SCHED_FIFO, &sp) < 0) {
662 VERBOSE_PRINTK_STRING("rcu_torture_boost RT prio failed!");
663 n_rcu_torture_boost_rterror++;
664 }
665
666 init_rcu_head_on_stack(&rbi.rcu);
667 /* Each pass through the following loop does one boost-test cycle. */
668 do {
669 /* Wait for the next test interval. */
670 oldstarttime = boost_starttime;
671 while (ULONG_CMP_LT(jiffies, oldstarttime)) {
672 schedule_timeout_interruptible(oldstarttime - jiffies);
673 rcu_stutter_wait("rcu_torture_boost");
674 if (kthread_should_stop() ||
675 fullstop != FULLSTOP_DONTSTOP)
676 goto checkwait;
677 }
678
679 /* Do one boost-test interval. */
680 endtime = oldstarttime + test_boost_duration * HZ;
681 call_rcu_time = jiffies;
682 while (ULONG_CMP_LT(jiffies, endtime)) {
683 /* If we don't have a callback in flight, post one. */
684 if (!rbi.inflight) {
685 smp_mb(); /* RCU core before ->inflight = 1. */
686 rbi.inflight = 1;
687 call_rcu(&rbi.rcu, rcu_torture_boost_cb);
688 if (jiffies - call_rcu_time >
689 test_boost_duration * HZ - HZ / 2) {
690 VERBOSE_PRINTK_STRING("rcu_torture_boost boosting failed");
691 n_rcu_torture_boost_failure++;
692 }
693 call_rcu_time = jiffies;
694 }
695 cond_resched();
696 rcu_stutter_wait("rcu_torture_boost");
697 if (kthread_should_stop() ||
698 fullstop != FULLSTOP_DONTSTOP)
699 goto checkwait;
700 }
701
702 /*
703 * Set the start time of the next test interval.
704 * Yes, this is vulnerable to long delays, but such
705 * delays simply cause a false negative for the next
706 * interval. Besides, we are running at RT priority,
707 * so delays should be relatively rare.
708 */
709 while (oldstarttime == boost_starttime &&
710 !kthread_should_stop()) {
711 if (mutex_trylock(&boost_mutex)) {
712 boost_starttime = jiffies +
713 test_boost_interval * HZ;
714 n_rcu_torture_boosts++;
715 mutex_unlock(&boost_mutex);
716 break;
717 }
718 schedule_timeout_uninterruptible(1);
719 }
720
721 /* Go do the stutter. */
722 checkwait: rcu_stutter_wait("rcu_torture_boost");
723 } while (!kthread_should_stop() && fullstop == FULLSTOP_DONTSTOP);
724
725 /* Clean up and exit. */
726 VERBOSE_PRINTK_STRING("rcu_torture_boost task stopping");
727 rcutorture_shutdown_absorb("rcu_torture_boost");
728 while (!kthread_should_stop() || rbi.inflight)
729 schedule_timeout_uninterruptible(1);
730 smp_mb(); /* order accesses to ->inflight before stack-frame death. */
731 destroy_rcu_head_on_stack(&rbi.rcu);
732 return 0;
733 }
734
735 /*
736 * RCU torture force-quiescent-state kthread. Repeatedly induces
737 * bursts of calls to force_quiescent_state(), increasing the probability
738 * of occurrence of some important types of race conditions.
739 */
740 static int
741 rcu_torture_fqs(void *arg)
742 {
743 unsigned long fqs_resume_time;
744 int fqs_burst_remaining;
745
746 VERBOSE_PRINTK_STRING("rcu_torture_fqs task started");
747 do {
748 fqs_resume_time = jiffies + fqs_stutter * HZ;
749 while (ULONG_CMP_LT(jiffies, fqs_resume_time) &&
750 !kthread_should_stop()) {
751 schedule_timeout_interruptible(1);
752 }
753 fqs_burst_remaining = fqs_duration;
754 while (fqs_burst_remaining > 0 &&
755 !kthread_should_stop()) {
756 cur_ops->fqs();
757 udelay(fqs_holdoff);
758 fqs_burst_remaining -= fqs_holdoff;
759 }
760 rcu_stutter_wait("rcu_torture_fqs");
761 } while (!kthread_should_stop() && fullstop == FULLSTOP_DONTSTOP);
762 VERBOSE_PRINTK_STRING("rcu_torture_fqs task stopping");
763 rcutorture_shutdown_absorb("rcu_torture_fqs");
764 while (!kthread_should_stop())
765 schedule_timeout_uninterruptible(1);
766 return 0;
767 }
768
769 /*
770 * RCU torture writer kthread. Repeatedly substitutes a new structure
771 * for that pointed to by rcu_torture_current, freeing the old structure
772 * after a series of grace periods (the "pipeline").
773 */
774 static int
775 rcu_torture_writer(void *arg)
776 {
777 bool exp;
778 int i;
779 struct rcu_torture *rp;
780 struct rcu_torture *rp1;
781 struct rcu_torture *old_rp;
782 static DEFINE_TORTURE_RANDOM(rand);
783
784 VERBOSE_PRINTK_STRING("rcu_torture_writer task started");
785 set_user_nice(current, 19);
786
787 do {
788 schedule_timeout_uninterruptible(1);
789 rp = rcu_torture_alloc();
790 if (rp == NULL)
791 continue;
792 rp->rtort_pipe_count = 0;
793 udelay(torture_random(&rand) & 0x3ff);
794 old_rp = rcu_dereference_check(rcu_torture_current,
795 current == writer_task);
796 rp->rtort_mbtest = 1;
797 rcu_assign_pointer(rcu_torture_current, rp);
798 smp_wmb(); /* Mods to old_rp must follow rcu_assign_pointer() */
799 if (old_rp) {
800 i = old_rp->rtort_pipe_count;
801 if (i > RCU_TORTURE_PIPE_LEN)
802 i = RCU_TORTURE_PIPE_LEN;
803 atomic_inc(&rcu_torture_wcount[i]);
804 old_rp->rtort_pipe_count++;
805 if (gp_normal == gp_exp)
806 exp = !!(torture_random(&rand) & 0x80);
807 else
808 exp = gp_exp;
809 if (!exp) {
810 cur_ops->deferred_free(old_rp);
811 } else {
812 cur_ops->exp_sync();
813 list_add(&old_rp->rtort_free,
814 &rcu_torture_removed);
815 list_for_each_entry_safe(rp, rp1,
816 &rcu_torture_removed,
817 rtort_free) {
818 i = rp->rtort_pipe_count;
819 if (i > RCU_TORTURE_PIPE_LEN)
820 i = RCU_TORTURE_PIPE_LEN;
821 atomic_inc(&rcu_torture_wcount[i]);
822 if (++rp->rtort_pipe_count >=
823 RCU_TORTURE_PIPE_LEN) {
824 rp->rtort_mbtest = 0;
825 list_del(&rp->rtort_free);
826 rcu_torture_free(rp);
827 }
828 }
829 }
830 }
831 rcutorture_record_progress(++rcu_torture_current_version);
832 rcu_stutter_wait("rcu_torture_writer");
833 } while (!kthread_should_stop() && fullstop == FULLSTOP_DONTSTOP);
834 VERBOSE_PRINTK_STRING("rcu_torture_writer task stopping");
835 rcutorture_shutdown_absorb("rcu_torture_writer");
836 while (!kthread_should_stop())
837 schedule_timeout_uninterruptible(1);
838 return 0;
839 }
840
841 /*
842 * RCU torture fake writer kthread. Repeatedly calls sync, with a random
843 * delay between calls.
844 */
845 static int
846 rcu_torture_fakewriter(void *arg)
847 {
848 DEFINE_TORTURE_RANDOM(rand);
849
850 VERBOSE_PRINTK_STRING("rcu_torture_fakewriter task started");
851 set_user_nice(current, 19);
852
853 do {
854 schedule_timeout_uninterruptible(1 + torture_random(&rand)%10);
855 udelay(torture_random(&rand) & 0x3ff);
856 if (cur_ops->cb_barrier != NULL &&
857 torture_random(&rand) % (nfakewriters * 8) == 0) {
858 cur_ops->cb_barrier();
859 } else if (gp_normal == gp_exp) {
860 if (torture_random(&rand) & 0x80)
861 cur_ops->sync();
862 else
863 cur_ops->exp_sync();
864 } else if (gp_normal) {
865 cur_ops->sync();
866 } else {
867 cur_ops->exp_sync();
868 }
869 rcu_stutter_wait("rcu_torture_fakewriter");
870 } while (!kthread_should_stop() && fullstop == FULLSTOP_DONTSTOP);
871
872 VERBOSE_PRINTK_STRING("rcu_torture_fakewriter task stopping");
873 rcutorture_shutdown_absorb("rcu_torture_fakewriter");
874 while (!kthread_should_stop())
875 schedule_timeout_uninterruptible(1);
876 return 0;
877 }
878
879 void rcutorture_trace_dump(void)
880 {
881 static atomic_t beenhere = ATOMIC_INIT(0);
882
883 if (atomic_read(&beenhere))
884 return;
885 if (atomic_xchg(&beenhere, 1) != 0)
886 return;
887 ftrace_dump(DUMP_ALL);
888 }
889
890 /*
891 * RCU torture reader from timer handler. Dereferences rcu_torture_current,
892 * incrementing the corresponding element of the pipeline array. The
893 * counter in the element should never be greater than 1, otherwise, the
894 * RCU implementation is broken.
895 */
896 static void rcu_torture_timer(unsigned long unused)
897 {
898 int idx;
899 int completed;
900 int completed_end;
901 static DEFINE_TORTURE_RANDOM(rand);
902 static DEFINE_SPINLOCK(rand_lock);
903 struct rcu_torture *p;
904 int pipe_count;
905 unsigned long long ts;
906
907 idx = cur_ops->readlock();
908 completed = cur_ops->completed();
909 ts = rcu_trace_clock_local();
910 p = rcu_dereference_check(rcu_torture_current,
911 rcu_read_lock_bh_held() ||
912 rcu_read_lock_sched_held() ||
913 srcu_read_lock_held(&srcu_ctl));
914 if (p == NULL) {
915 /* Leave because rcu_torture_writer is not yet underway */
916 cur_ops->readunlock(idx);
917 return;
918 }
919 if (p->rtort_mbtest == 0)
920 atomic_inc(&n_rcu_torture_mberror);
921 spin_lock(&rand_lock);
922 cur_ops->read_delay(&rand);
923 n_rcu_torture_timers++;
924 spin_unlock(&rand_lock);
925 preempt_disable();
926 pipe_count = p->rtort_pipe_count;
927 if (pipe_count > RCU_TORTURE_PIPE_LEN) {
928 /* Should not happen, but... */
929 pipe_count = RCU_TORTURE_PIPE_LEN;
930 }
931 completed_end = cur_ops->completed();
932 if (pipe_count > 1) {
933 do_trace_rcu_torture_read(cur_ops->name, &p->rtort_rcu, ts,
934 completed, completed_end);
935 rcutorture_trace_dump();
936 }
937 __this_cpu_inc(rcu_torture_count[pipe_count]);
938 completed = completed_end - completed;
939 if (completed > RCU_TORTURE_PIPE_LEN) {
940 /* Should not happen, but... */
941 completed = RCU_TORTURE_PIPE_LEN;
942 }
943 __this_cpu_inc(rcu_torture_batch[completed]);
944 preempt_enable();
945 cur_ops->readunlock(idx);
946 }
947
948 /*
949 * RCU torture reader kthread. Repeatedly dereferences rcu_torture_current,
950 * incrementing the corresponding element of the pipeline array. The
951 * counter in the element should never be greater than 1, otherwise, the
952 * RCU implementation is broken.
953 */
954 static int
955 rcu_torture_reader(void *arg)
956 {
957 int completed;
958 int completed_end;
959 int idx;
960 DEFINE_TORTURE_RANDOM(rand);
961 struct rcu_torture *p;
962 int pipe_count;
963 struct timer_list t;
964 unsigned long long ts;
965
966 VERBOSE_PRINTK_STRING("rcu_torture_reader task started");
967 set_user_nice(current, 19);
968 if (irqreader && cur_ops->irq_capable)
969 setup_timer_on_stack(&t, rcu_torture_timer, 0);
970
971 do {
972 if (irqreader && cur_ops->irq_capable) {
973 if (!timer_pending(&t))
974 mod_timer(&t, jiffies + 1);
975 }
976 idx = cur_ops->readlock();
977 completed = cur_ops->completed();
978 ts = rcu_trace_clock_local();
979 p = rcu_dereference_check(rcu_torture_current,
980 rcu_read_lock_bh_held() ||
981 rcu_read_lock_sched_held() ||
982 srcu_read_lock_held(&srcu_ctl));
983 if (p == NULL) {
984 /* Wait for rcu_torture_writer to get underway */
985 cur_ops->readunlock(idx);
986 schedule_timeout_interruptible(HZ);
987 continue;
988 }
989 if (p->rtort_mbtest == 0)
990 atomic_inc(&n_rcu_torture_mberror);
991 cur_ops->read_delay(&rand);
992 preempt_disable();
993 pipe_count = p->rtort_pipe_count;
994 if (pipe_count > RCU_TORTURE_PIPE_LEN) {
995 /* Should not happen, but... */
996 pipe_count = RCU_TORTURE_PIPE_LEN;
997 }
998 completed_end = cur_ops->completed();
999 if (pipe_count > 1) {
1000 do_trace_rcu_torture_read(cur_ops->name, &p->rtort_rcu,
1001 ts, completed, completed_end);
1002 rcutorture_trace_dump();
1003 }
1004 __this_cpu_inc(rcu_torture_count[pipe_count]);
1005 completed = completed_end - completed;
1006 if (completed > RCU_TORTURE_PIPE_LEN) {
1007 /* Should not happen, but... */
1008 completed = RCU_TORTURE_PIPE_LEN;
1009 }
1010 __this_cpu_inc(rcu_torture_batch[completed]);
1011 preempt_enable();
1012 cur_ops->readunlock(idx);
1013 schedule();
1014 rcu_stutter_wait("rcu_torture_reader");
1015 } while (!kthread_should_stop() && fullstop == FULLSTOP_DONTSTOP);
1016 VERBOSE_PRINTK_STRING("rcu_torture_reader task stopping");
1017 rcutorture_shutdown_absorb("rcu_torture_reader");
1018 if (irqreader && cur_ops->irq_capable)
1019 del_timer_sync(&t);
1020 while (!kthread_should_stop())
1021 schedule_timeout_uninterruptible(1);
1022 return 0;
1023 }
1024
1025 /*
1026 * Create an RCU-torture statistics message in the specified buffer.
1027 */
1028 static void
1029 rcu_torture_printk(char *page)
1030 {
1031 int cpu;
1032 int i;
1033 long pipesummary[RCU_TORTURE_PIPE_LEN + 1] = { 0 };
1034 long batchsummary[RCU_TORTURE_PIPE_LEN + 1] = { 0 };
1035
1036 for_each_possible_cpu(cpu) {
1037 for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++) {
1038 pipesummary[i] += per_cpu(rcu_torture_count, cpu)[i];
1039 batchsummary[i] += per_cpu(rcu_torture_batch, cpu)[i];
1040 }
1041 }
1042 for (i = RCU_TORTURE_PIPE_LEN - 1; i >= 0; i--) {
1043 if (pipesummary[i] != 0)
1044 break;
1045 }
1046 page += sprintf(page, "%s%s ", torture_type, TORTURE_FLAG);
1047 page += sprintf(page,
1048 "rtc: %p ver: %lu tfle: %d rta: %d rtaf: %d rtf: %d ",
1049 rcu_torture_current,
1050 rcu_torture_current_version,
1051 list_empty(&rcu_torture_freelist),
1052 atomic_read(&n_rcu_torture_alloc),
1053 atomic_read(&n_rcu_torture_alloc_fail),
1054 atomic_read(&n_rcu_torture_free));
1055 page += sprintf(page, "rtmbe: %d rtbke: %ld rtbre: %ld ",
1056 atomic_read(&n_rcu_torture_mberror),
1057 n_rcu_torture_boost_ktrerror,
1058 n_rcu_torture_boost_rterror);
1059 page += sprintf(page, "rtbf: %ld rtb: %ld nt: %ld ",
1060 n_rcu_torture_boost_failure,
1061 n_rcu_torture_boosts,
1062 n_rcu_torture_timers);
1063 page += sprintf(page,
1064 "onoff: %ld/%ld:%ld/%ld %d,%d:%d,%d %lu:%lu (HZ=%d) ",
1065 n_online_successes, n_online_attempts,
1066 n_offline_successes, n_offline_attempts,
1067 min_online, max_online,
1068 min_offline, max_offline,
1069 sum_online, sum_offline, HZ);
1070 page += sprintf(page, "barrier: %ld/%ld:%ld",
1071 n_barrier_successes,
1072 n_barrier_attempts,
1073 n_rcu_torture_barrier_error);
1074 page += sprintf(page, "\n%s%s ", torture_type, TORTURE_FLAG);
1075 if (atomic_read(&n_rcu_torture_mberror) != 0 ||
1076 n_rcu_torture_barrier_error != 0 ||
1077 n_rcu_torture_boost_ktrerror != 0 ||
1078 n_rcu_torture_boost_rterror != 0 ||
1079 n_rcu_torture_boost_failure != 0 ||
1080 i > 1) {
1081 page += sprintf(page, "!!! ");
1082 atomic_inc(&n_rcu_torture_error);
1083 WARN_ON_ONCE(1);
1084 }
1085 page += sprintf(page, "Reader Pipe: ");
1086 for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++)
1087 page += sprintf(page, " %ld", pipesummary[i]);
1088 page += sprintf(page, "\n%s%s ", torture_type, TORTURE_FLAG);
1089 page += sprintf(page, "Reader Batch: ");
1090 for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++)
1091 page += sprintf(page, " %ld", batchsummary[i]);
1092 page += sprintf(page, "\n%s%s ", torture_type, TORTURE_FLAG);
1093 page += sprintf(page, "Free-Block Circulation: ");
1094 for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++) {
1095 page += sprintf(page, " %d",
1096 atomic_read(&rcu_torture_wcount[i]));
1097 }
1098 page += sprintf(page, "\n");
1099 if (cur_ops->stats)
1100 cur_ops->stats(page);
1101 }
1102
1103 /*
1104 * Print torture statistics. Caller must ensure that there is only
1105 * one call to this function at a given time!!! This is normally
1106 * accomplished by relying on the module system to only have one copy
1107 * of the module loaded, and then by giving the rcu_torture_stats
1108 * kthread full control (or the init/cleanup functions when rcu_torture_stats
1109 * thread is not running).
1110 */
1111 static void
1112 rcu_torture_stats_print(void)
1113 {
1114 int size = nr_cpu_ids * 200 + 8192;
1115 char *buf;
1116
1117 buf = kmalloc(size, GFP_KERNEL);
1118 if (!buf) {
1119 pr_err("rcu-torture: Out of memory, need: %d", size);
1120 return;
1121 }
1122 rcu_torture_printk(buf);
1123 pr_alert("%s", buf);
1124 kfree(buf);
1125 }
1126
1127 /*
1128 * Periodically prints torture statistics, if periodic statistics printing
1129 * was specified via the stat_interval module parameter.
1130 *
1131 * No need to worry about fullstop here, since this one doesn't reference
1132 * volatile state or register callbacks.
1133 */
1134 static int
1135 rcu_torture_stats(void *arg)
1136 {
1137 VERBOSE_PRINTK_STRING("rcu_torture_stats task started");
1138 do {
1139 schedule_timeout_interruptible(stat_interval * HZ);
1140 rcu_torture_stats_print();
1141 rcutorture_shutdown_absorb("rcu_torture_stats");
1142 } while (!kthread_should_stop());
1143 VERBOSE_PRINTK_STRING("rcu_torture_stats task stopping");
1144 return 0;
1145 }
1146
1147 static int rcu_idle_cpu; /* Force all torture tasks off this CPU */
1148
1149 /* Shuffle tasks such that we allow @rcu_idle_cpu to become idle. A special case
1150 * is when @rcu_idle_cpu = -1, when we allow the tasks to run on all CPUs.
1151 */
1152 static void rcu_torture_shuffle_tasks(void)
1153 {
1154 int i;
1155
1156 cpumask_setall(shuffle_tmp_mask);
1157 get_online_cpus();
1158
1159 /* No point in shuffling if there is only one online CPU (ex: UP) */
1160 if (num_online_cpus() == 1) {
1161 put_online_cpus();
1162 return;
1163 }
1164
1165 if (rcu_idle_cpu != -1)
1166 cpumask_clear_cpu(rcu_idle_cpu, shuffle_tmp_mask);
1167
1168 set_cpus_allowed_ptr(current, shuffle_tmp_mask);
1169
1170 if (reader_tasks) {
1171 for (i = 0; i < nrealreaders; i++)
1172 if (reader_tasks[i])
1173 set_cpus_allowed_ptr(reader_tasks[i],
1174 shuffle_tmp_mask);
1175 }
1176 if (fakewriter_tasks) {
1177 for (i = 0; i < nfakewriters; i++)
1178 if (fakewriter_tasks[i])
1179 set_cpus_allowed_ptr(fakewriter_tasks[i],
1180 shuffle_tmp_mask);
1181 }
1182 if (writer_task)
1183 set_cpus_allowed_ptr(writer_task, shuffle_tmp_mask);
1184 if (stats_task)
1185 set_cpus_allowed_ptr(stats_task, shuffle_tmp_mask);
1186 if (stutter_task)
1187 set_cpus_allowed_ptr(stutter_task, shuffle_tmp_mask);
1188 if (fqs_task)
1189 set_cpus_allowed_ptr(fqs_task, shuffle_tmp_mask);
1190 if (shutdown_task)
1191 set_cpus_allowed_ptr(shutdown_task, shuffle_tmp_mask);
1192 #ifdef CONFIG_HOTPLUG_CPU
1193 if (onoff_task)
1194 set_cpus_allowed_ptr(onoff_task, shuffle_tmp_mask);
1195 #endif /* #ifdef CONFIG_HOTPLUG_CPU */
1196 if (stall_task)
1197 set_cpus_allowed_ptr(stall_task, shuffle_tmp_mask);
1198 if (barrier_cbs_tasks)
1199 for (i = 0; i < n_barrier_cbs; i++)
1200 if (barrier_cbs_tasks[i])
1201 set_cpus_allowed_ptr(barrier_cbs_tasks[i],
1202 shuffle_tmp_mask);
1203 if (barrier_task)
1204 set_cpus_allowed_ptr(barrier_task, shuffle_tmp_mask);
1205
1206 if (rcu_idle_cpu == -1)
1207 rcu_idle_cpu = num_online_cpus() - 1;
1208 else
1209 rcu_idle_cpu--;
1210
1211 put_online_cpus();
1212 }
1213
1214 /* Shuffle tasks across CPUs, with the intent of allowing each CPU in the
1215 * system to become idle at a time and cut off its timer ticks. This is meant
1216 * to test the support for such tickless idle CPU in RCU.
1217 */
1218 static int
1219 rcu_torture_shuffle(void *arg)
1220 {
1221 VERBOSE_PRINTK_STRING("rcu_torture_shuffle task started");
1222 do {
1223 schedule_timeout_interruptible(shuffle_interval * HZ);
1224 rcu_torture_shuffle_tasks();
1225 rcutorture_shutdown_absorb("rcu_torture_shuffle");
1226 } while (!kthread_should_stop());
1227 VERBOSE_PRINTK_STRING("rcu_torture_shuffle task stopping");
1228 return 0;
1229 }
1230
1231 /* Cause the rcutorture test to "stutter", starting and stopping all
1232 * threads periodically.
1233 */
1234 static int
1235 rcu_torture_stutter(void *arg)
1236 {
1237 VERBOSE_PRINTK_STRING("rcu_torture_stutter task started");
1238 do {
1239 schedule_timeout_interruptible(stutter * HZ);
1240 stutter_pause_test = 1;
1241 if (!kthread_should_stop())
1242 schedule_timeout_interruptible(stutter * HZ);
1243 stutter_pause_test = 0;
1244 rcutorture_shutdown_absorb("rcu_torture_stutter");
1245 } while (!kthread_should_stop());
1246 VERBOSE_PRINTK_STRING("rcu_torture_stutter task stopping");
1247 return 0;
1248 }
1249
1250 static inline void
1251 rcu_torture_print_module_parms(struct rcu_torture_ops *cur_ops, const char *tag)
1252 {
1253 pr_alert("%s" TORTURE_FLAG
1254 "--- %s: nreaders=%d nfakewriters=%d "
1255 "stat_interval=%d verbose=%d test_no_idle_hz=%d "
1256 "shuffle_interval=%d stutter=%d irqreader=%d "
1257 "fqs_duration=%d fqs_holdoff=%d fqs_stutter=%d "
1258 "test_boost=%d/%d test_boost_interval=%d "
1259 "test_boost_duration=%d shutdown_secs=%d "
1260 "stall_cpu=%d stall_cpu_holdoff=%d "
1261 "n_barrier_cbs=%d "
1262 "onoff_interval=%d onoff_holdoff=%d\n",
1263 torture_type, tag, nrealreaders, nfakewriters,
1264 stat_interval, verbose, test_no_idle_hz, shuffle_interval,
1265 stutter, irqreader, fqs_duration, fqs_holdoff, fqs_stutter,
1266 test_boost, cur_ops->can_boost,
1267 test_boost_interval, test_boost_duration, shutdown_secs,
1268 stall_cpu, stall_cpu_holdoff,
1269 n_barrier_cbs,
1270 onoff_interval, onoff_holdoff);
1271 }
1272
1273 static struct notifier_block rcutorture_shutdown_nb = {
1274 .notifier_call = rcutorture_shutdown_notify,
1275 };
1276
1277 static void rcutorture_booster_cleanup(int cpu)
1278 {
1279 struct task_struct *t;
1280
1281 if (boost_tasks[cpu] == NULL)
1282 return;
1283 mutex_lock(&boost_mutex);
1284 VERBOSE_PRINTK_STRING("Stopping rcu_torture_boost task");
1285 t = boost_tasks[cpu];
1286 boost_tasks[cpu] = NULL;
1287 mutex_unlock(&boost_mutex);
1288
1289 /* This must be outside of the mutex, otherwise deadlock! */
1290 kthread_stop(t);
1291 boost_tasks[cpu] = NULL;
1292 }
1293
1294 static int rcutorture_booster_init(int cpu)
1295 {
1296 int retval;
1297
1298 if (boost_tasks[cpu] != NULL)
1299 return 0; /* Already created, nothing more to do. */
1300
1301 /* Don't allow time recalculation while creating a new task. */
1302 mutex_lock(&boost_mutex);
1303 VERBOSE_PRINTK_STRING("Creating rcu_torture_boost task");
1304 boost_tasks[cpu] = kthread_create_on_node(rcu_torture_boost, NULL,
1305 cpu_to_node(cpu),
1306 "rcu_torture_boost");
1307 if (IS_ERR(boost_tasks[cpu])) {
1308 retval = PTR_ERR(boost_tasks[cpu]);
1309 VERBOSE_PRINTK_STRING("rcu_torture_boost task create failed");
1310 n_rcu_torture_boost_ktrerror++;
1311 boost_tasks[cpu] = NULL;
1312 mutex_unlock(&boost_mutex);
1313 return retval;
1314 }
1315 kthread_bind(boost_tasks[cpu], cpu);
1316 wake_up_process(boost_tasks[cpu]);
1317 mutex_unlock(&boost_mutex);
1318 return 0;
1319 }
1320
1321 /*
1322 * Cause the rcutorture test to shutdown the system after the test has
1323 * run for the time specified by the shutdown_secs module parameter.
1324 */
1325 static int
1326 rcu_torture_shutdown(void *arg)
1327 {
1328 long delta;
1329 unsigned long jiffies_snap;
1330
1331 VERBOSE_PRINTK_STRING("rcu_torture_shutdown task started");
1332 jiffies_snap = ACCESS_ONCE(jiffies);
1333 while (ULONG_CMP_LT(jiffies_snap, shutdown_time) &&
1334 !kthread_should_stop()) {
1335 delta = shutdown_time - jiffies_snap;
1336 if (verbose)
1337 pr_alert("%s" TORTURE_FLAG
1338 "rcu_torture_shutdown task: %lu jiffies remaining\n",
1339 torture_type, delta);
1340 schedule_timeout_interruptible(delta);
1341 jiffies_snap = ACCESS_ONCE(jiffies);
1342 }
1343 if (kthread_should_stop()) {
1344 VERBOSE_PRINTK_STRING("rcu_torture_shutdown task stopping");
1345 return 0;
1346 }
1347
1348 /* OK, shut down the system. */
1349
1350 VERBOSE_PRINTK_STRING("rcu_torture_shutdown task shutting down system");
1351 shutdown_task = NULL; /* Avoid self-kill deadlock. */
1352 rcu_torture_cleanup(); /* Get the success/failure message. */
1353 kernel_power_off(); /* Shut down the system. */
1354 return 0;
1355 }
1356
1357 #ifdef CONFIG_HOTPLUG_CPU
1358
1359 /*
1360 * Execute random CPU-hotplug operations at the interval specified
1361 * by the onoff_interval.
1362 */
1363 static int
1364 rcu_torture_onoff(void *arg)
1365 {
1366 int cpu;
1367 unsigned long delta;
1368 int maxcpu = -1;
1369 DEFINE_TORTURE_RANDOM(rand);
1370 int ret;
1371 unsigned long starttime;
1372
1373 VERBOSE_PRINTK_STRING("rcu_torture_onoff task started");
1374 for_each_online_cpu(cpu)
1375 maxcpu = cpu;
1376 WARN_ON(maxcpu < 0);
1377 if (onoff_holdoff > 0) {
1378 VERBOSE_PRINTK_STRING("rcu_torture_onoff begin holdoff");
1379 schedule_timeout_interruptible(onoff_holdoff * HZ);
1380 VERBOSE_PRINTK_STRING("rcu_torture_onoff end holdoff");
1381 }
1382 while (!kthread_should_stop()) {
1383 cpu = (torture_random(&rand) >> 4) % (maxcpu + 1);
1384 if (cpu_online(cpu) && cpu_is_hotpluggable(cpu)) {
1385 if (verbose)
1386 pr_alert("%s" TORTURE_FLAG
1387 "rcu_torture_onoff task: offlining %d\n",
1388 torture_type, cpu);
1389 starttime = jiffies;
1390 n_offline_attempts++;
1391 ret = cpu_down(cpu);
1392 if (ret) {
1393 if (verbose)
1394 pr_alert("%s" TORTURE_FLAG
1395 "rcu_torture_onoff task: offline %d failed: errno %d\n",
1396 torture_type, cpu, ret);
1397 } else {
1398 if (verbose)
1399 pr_alert("%s" TORTURE_FLAG
1400 "rcu_torture_onoff task: offlined %d\n",
1401 torture_type, cpu);
1402 n_offline_successes++;
1403 delta = jiffies - starttime;
1404 sum_offline += delta;
1405 if (min_offline < 0) {
1406 min_offline = delta;
1407 max_offline = delta;
1408 }
1409 if (min_offline > delta)
1410 min_offline = delta;
1411 if (max_offline < delta)
1412 max_offline = delta;
1413 }
1414 } else if (cpu_is_hotpluggable(cpu)) {
1415 if (verbose)
1416 pr_alert("%s" TORTURE_FLAG
1417 "rcu_torture_onoff task: onlining %d\n",
1418 torture_type, cpu);
1419 starttime = jiffies;
1420 n_online_attempts++;
1421 ret = cpu_up(cpu);
1422 if (ret) {
1423 if (verbose)
1424 pr_alert("%s" TORTURE_FLAG
1425 "rcu_torture_onoff task: online %d failed: errno %d\n",
1426 torture_type, cpu, ret);
1427 } else {
1428 if (verbose)
1429 pr_alert("%s" TORTURE_FLAG
1430 "rcu_torture_onoff task: onlined %d\n",
1431 torture_type, cpu);
1432 n_online_successes++;
1433 delta = jiffies - starttime;
1434 sum_online += delta;
1435 if (min_online < 0) {
1436 min_online = delta;
1437 max_online = delta;
1438 }
1439 if (min_online > delta)
1440 min_online = delta;
1441 if (max_online < delta)
1442 max_online = delta;
1443 }
1444 }
1445 schedule_timeout_interruptible(onoff_interval * HZ);
1446 }
1447 VERBOSE_PRINTK_STRING("rcu_torture_onoff task stopping");
1448 return 0;
1449 }
1450
1451 static int
1452 rcu_torture_onoff_init(void)
1453 {
1454 int ret;
1455
1456 if (onoff_interval <= 0)
1457 return 0;
1458 onoff_task = kthread_run(rcu_torture_onoff, NULL, "rcu_torture_onoff");
1459 if (IS_ERR(onoff_task)) {
1460 ret = PTR_ERR(onoff_task);
1461 onoff_task = NULL;
1462 return ret;
1463 }
1464 return 0;
1465 }
1466
1467 static void rcu_torture_onoff_cleanup(void)
1468 {
1469 if (onoff_task == NULL)
1470 return;
1471 VERBOSE_PRINTK_STRING("Stopping rcu_torture_onoff task");
1472 kthread_stop(onoff_task);
1473 onoff_task = NULL;
1474 }
1475
1476 #else /* #ifdef CONFIG_HOTPLUG_CPU */
1477
1478 static int
1479 rcu_torture_onoff_init(void)
1480 {
1481 return 0;
1482 }
1483
1484 static void rcu_torture_onoff_cleanup(void)
1485 {
1486 }
1487
1488 #endif /* #else #ifdef CONFIG_HOTPLUG_CPU */
1489
1490 /*
1491 * CPU-stall kthread. It waits as specified by stall_cpu_holdoff, then
1492 * induces a CPU stall for the time specified by stall_cpu.
1493 */
1494 static int rcu_torture_stall(void *args)
1495 {
1496 unsigned long stop_at;
1497
1498 VERBOSE_PRINTK_STRING("rcu_torture_stall task started");
1499 if (stall_cpu_holdoff > 0) {
1500 VERBOSE_PRINTK_STRING("rcu_torture_stall begin holdoff");
1501 schedule_timeout_interruptible(stall_cpu_holdoff * HZ);
1502 VERBOSE_PRINTK_STRING("rcu_torture_stall end holdoff");
1503 }
1504 if (!kthread_should_stop()) {
1505 stop_at = get_seconds() + stall_cpu;
1506 /* RCU CPU stall is expected behavior in following code. */
1507 pr_alert("rcu_torture_stall start.\n");
1508 rcu_read_lock();
1509 preempt_disable();
1510 while (ULONG_CMP_LT(get_seconds(), stop_at))
1511 continue; /* Induce RCU CPU stall warning. */
1512 preempt_enable();
1513 rcu_read_unlock();
1514 pr_alert("rcu_torture_stall end.\n");
1515 }
1516 rcutorture_shutdown_absorb("rcu_torture_stall");
1517 while (!kthread_should_stop())
1518 schedule_timeout_interruptible(10 * HZ);
1519 return 0;
1520 }
1521
1522 /* Spawn CPU-stall kthread, if stall_cpu specified. */
1523 static int __init rcu_torture_stall_init(void)
1524 {
1525 int ret;
1526
1527 if (stall_cpu <= 0)
1528 return 0;
1529 stall_task = kthread_run(rcu_torture_stall, NULL, "rcu_torture_stall");
1530 if (IS_ERR(stall_task)) {
1531 ret = PTR_ERR(stall_task);
1532 stall_task = NULL;
1533 return ret;
1534 }
1535 return 0;
1536 }
1537
1538 /* Clean up after the CPU-stall kthread, if one was spawned. */
1539 static void rcu_torture_stall_cleanup(void)
1540 {
1541 if (stall_task == NULL)
1542 return;
1543 VERBOSE_PRINTK_STRING("Stopping rcu_torture_stall_task.");
1544 kthread_stop(stall_task);
1545 stall_task = NULL;
1546 }
1547
1548 /* Callback function for RCU barrier testing. */
1549 void rcu_torture_barrier_cbf(struct rcu_head *rcu)
1550 {
1551 atomic_inc(&barrier_cbs_invoked);
1552 }
1553
1554 /* kthread function to register callbacks used to test RCU barriers. */
1555 static int rcu_torture_barrier_cbs(void *arg)
1556 {
1557 long myid = (long)arg;
1558 bool lastphase = 0;
1559 bool newphase;
1560 struct rcu_head rcu;
1561
1562 init_rcu_head_on_stack(&rcu);
1563 VERBOSE_PRINTK_STRING("rcu_torture_barrier_cbs task started");
1564 set_user_nice(current, 19);
1565 do {
1566 wait_event(barrier_cbs_wq[myid],
1567 (newphase =
1568 ACCESS_ONCE(barrier_phase)) != lastphase ||
1569 kthread_should_stop() ||
1570 fullstop != FULLSTOP_DONTSTOP);
1571 lastphase = newphase;
1572 smp_mb(); /* ensure barrier_phase load before ->call(). */
1573 if (kthread_should_stop() || fullstop != FULLSTOP_DONTSTOP)
1574 break;
1575 cur_ops->call(&rcu, rcu_torture_barrier_cbf);
1576 if (atomic_dec_and_test(&barrier_cbs_count))
1577 wake_up(&barrier_wq);
1578 } while (!kthread_should_stop() && fullstop == FULLSTOP_DONTSTOP);
1579 VERBOSE_PRINTK_STRING("rcu_torture_barrier_cbs task stopping");
1580 rcutorture_shutdown_absorb("rcu_torture_barrier_cbs");
1581 while (!kthread_should_stop())
1582 schedule_timeout_interruptible(1);
1583 cur_ops->cb_barrier();
1584 destroy_rcu_head_on_stack(&rcu);
1585 return 0;
1586 }
1587
1588 /* kthread function to drive and coordinate RCU barrier testing. */
1589 static int rcu_torture_barrier(void *arg)
1590 {
1591 int i;
1592
1593 VERBOSE_PRINTK_STRING("rcu_torture_barrier task starting");
1594 do {
1595 atomic_set(&barrier_cbs_invoked, 0);
1596 atomic_set(&barrier_cbs_count, n_barrier_cbs);
1597 smp_mb(); /* Ensure barrier_phase after prior assignments. */
1598 barrier_phase = !barrier_phase;
1599 for (i = 0; i < n_barrier_cbs; i++)
1600 wake_up(&barrier_cbs_wq[i]);
1601 wait_event(barrier_wq,
1602 atomic_read(&barrier_cbs_count) == 0 ||
1603 kthread_should_stop() ||
1604 fullstop != FULLSTOP_DONTSTOP);
1605 if (kthread_should_stop() || fullstop != FULLSTOP_DONTSTOP)
1606 break;
1607 n_barrier_attempts++;
1608 cur_ops->cb_barrier(); /* Implies smp_mb() for wait_event(). */
1609 if (atomic_read(&barrier_cbs_invoked) != n_barrier_cbs) {
1610 n_rcu_torture_barrier_error++;
1611 WARN_ON_ONCE(1);
1612 }
1613 n_barrier_successes++;
1614 schedule_timeout_interruptible(HZ / 10);
1615 } while (!kthread_should_stop() && fullstop == FULLSTOP_DONTSTOP);
1616 VERBOSE_PRINTK_STRING("rcu_torture_barrier task stopping");
1617 rcutorture_shutdown_absorb("rcu_torture_barrier");
1618 while (!kthread_should_stop())
1619 schedule_timeout_interruptible(1);
1620 return 0;
1621 }
1622
1623 /* Initialize RCU barrier testing. */
1624 static int rcu_torture_barrier_init(void)
1625 {
1626 int i;
1627 int ret;
1628
1629 if (n_barrier_cbs == 0)
1630 return 0;
1631 if (cur_ops->call == NULL || cur_ops->cb_barrier == NULL) {
1632 pr_alert("%s" TORTURE_FLAG
1633 " Call or barrier ops missing for %s,\n",
1634 torture_type, cur_ops->name);
1635 pr_alert("%s" TORTURE_FLAG
1636 " RCU barrier testing omitted from run.\n",
1637 torture_type);
1638 return 0;
1639 }
1640 atomic_set(&barrier_cbs_count, 0);
1641 atomic_set(&barrier_cbs_invoked, 0);
1642 barrier_cbs_tasks =
1643 kzalloc(n_barrier_cbs * sizeof(barrier_cbs_tasks[0]),
1644 GFP_KERNEL);
1645 barrier_cbs_wq =
1646 kzalloc(n_barrier_cbs * sizeof(barrier_cbs_wq[0]),
1647 GFP_KERNEL);
1648 if (barrier_cbs_tasks == NULL || !barrier_cbs_wq)
1649 return -ENOMEM;
1650 for (i = 0; i < n_barrier_cbs; i++) {
1651 init_waitqueue_head(&barrier_cbs_wq[i]);
1652 barrier_cbs_tasks[i] = kthread_run(rcu_torture_barrier_cbs,
1653 (void *)(long)i,
1654 "rcu_torture_barrier_cbs");
1655 if (IS_ERR(barrier_cbs_tasks[i])) {
1656 ret = PTR_ERR(barrier_cbs_tasks[i]);
1657 VERBOSE_PRINTK_ERRSTRING("Failed to create rcu_torture_barrier_cbs");
1658 barrier_cbs_tasks[i] = NULL;
1659 return ret;
1660 }
1661 }
1662 barrier_task = kthread_run(rcu_torture_barrier, NULL,
1663 "rcu_torture_barrier");
1664 if (IS_ERR(barrier_task)) {
1665 ret = PTR_ERR(barrier_task);
1666 VERBOSE_PRINTK_ERRSTRING("Failed to create rcu_torture_barrier");
1667 barrier_task = NULL;
1668 }
1669 return 0;
1670 }
1671
1672 /* Clean up after RCU barrier testing. */
1673 static void rcu_torture_barrier_cleanup(void)
1674 {
1675 int i;
1676
1677 if (barrier_task != NULL) {
1678 VERBOSE_PRINTK_STRING("Stopping rcu_torture_barrier task");
1679 kthread_stop(barrier_task);
1680 barrier_task = NULL;
1681 }
1682 if (barrier_cbs_tasks != NULL) {
1683 for (i = 0; i < n_barrier_cbs; i++) {
1684 if (barrier_cbs_tasks[i] != NULL) {
1685 VERBOSE_PRINTK_STRING("Stopping rcu_torture_barrier_cbs task");
1686 kthread_stop(barrier_cbs_tasks[i]);
1687 barrier_cbs_tasks[i] = NULL;
1688 }
1689 }
1690 kfree(barrier_cbs_tasks);
1691 barrier_cbs_tasks = NULL;
1692 }
1693 if (barrier_cbs_wq != NULL) {
1694 kfree(barrier_cbs_wq);
1695 barrier_cbs_wq = NULL;
1696 }
1697 }
1698
1699 static int rcutorture_cpu_notify(struct notifier_block *self,
1700 unsigned long action, void *hcpu)
1701 {
1702 long cpu = (long)hcpu;
1703
1704 switch (action) {
1705 case CPU_ONLINE:
1706 case CPU_DOWN_FAILED:
1707 (void)rcutorture_booster_init(cpu);
1708 break;
1709 case CPU_DOWN_PREPARE:
1710 rcutorture_booster_cleanup(cpu);
1711 break;
1712 default:
1713 break;
1714 }
1715 return NOTIFY_OK;
1716 }
1717
1718 static struct notifier_block rcutorture_cpu_nb = {
1719 .notifier_call = rcutorture_cpu_notify,
1720 };
1721
1722 static void
1723 rcu_torture_cleanup(void)
1724 {
1725 int i;
1726
1727 mutex_lock(&fullstop_mutex);
1728 rcutorture_record_test_transition();
1729 if (fullstop == FULLSTOP_SHUTDOWN) {
1730 pr_warn(/* but going down anyway, so... */
1731 "Concurrent 'rmmod rcutorture' and shutdown illegal!\n");
1732 mutex_unlock(&fullstop_mutex);
1733 schedule_timeout_uninterruptible(10);
1734 if (cur_ops->cb_barrier != NULL)
1735 cur_ops->cb_barrier();
1736 return;
1737 }
1738 fullstop = FULLSTOP_RMMOD;
1739 mutex_unlock(&fullstop_mutex);
1740 unregister_reboot_notifier(&rcutorture_shutdown_nb);
1741 rcu_torture_barrier_cleanup();
1742 rcu_torture_stall_cleanup();
1743 if (stutter_task) {
1744 VERBOSE_PRINTK_STRING("Stopping rcu_torture_stutter task");
1745 kthread_stop(stutter_task);
1746 }
1747 stutter_task = NULL;
1748 if (shuffler_task) {
1749 VERBOSE_PRINTK_STRING("Stopping rcu_torture_shuffle task");
1750 kthread_stop(shuffler_task);
1751 free_cpumask_var(shuffle_tmp_mask);
1752 }
1753 shuffler_task = NULL;
1754
1755 if (writer_task) {
1756 VERBOSE_PRINTK_STRING("Stopping rcu_torture_writer task");
1757 kthread_stop(writer_task);
1758 }
1759 writer_task = NULL;
1760
1761 if (reader_tasks) {
1762 for (i = 0; i < nrealreaders; i++) {
1763 if (reader_tasks[i]) {
1764 VERBOSE_PRINTK_STRING(
1765 "Stopping rcu_torture_reader task");
1766 kthread_stop(reader_tasks[i]);
1767 }
1768 reader_tasks[i] = NULL;
1769 }
1770 kfree(reader_tasks);
1771 reader_tasks = NULL;
1772 }
1773 rcu_torture_current = NULL;
1774
1775 if (fakewriter_tasks) {
1776 for (i = 0; i < nfakewriters; i++) {
1777 if (fakewriter_tasks[i]) {
1778 VERBOSE_PRINTK_STRING(
1779 "Stopping rcu_torture_fakewriter task");
1780 kthread_stop(fakewriter_tasks[i]);
1781 }
1782 fakewriter_tasks[i] = NULL;
1783 }
1784 kfree(fakewriter_tasks);
1785 fakewriter_tasks = NULL;
1786 }
1787
1788 if (stats_task) {
1789 VERBOSE_PRINTK_STRING("Stopping rcu_torture_stats task");
1790 kthread_stop(stats_task);
1791 }
1792 stats_task = NULL;
1793
1794 if (fqs_task) {
1795 VERBOSE_PRINTK_STRING("Stopping rcu_torture_fqs task");
1796 kthread_stop(fqs_task);
1797 }
1798 fqs_task = NULL;
1799 if ((test_boost == 1 && cur_ops->can_boost) ||
1800 test_boost == 2) {
1801 unregister_cpu_notifier(&rcutorture_cpu_nb);
1802 for_each_possible_cpu(i)
1803 rcutorture_booster_cleanup(i);
1804 }
1805 if (shutdown_task != NULL) {
1806 VERBOSE_PRINTK_STRING("Stopping rcu_torture_shutdown task");
1807 kthread_stop(shutdown_task);
1808 }
1809 shutdown_task = NULL;
1810 rcu_torture_onoff_cleanup();
1811
1812 /* Wait for all RCU callbacks to fire. */
1813
1814 if (cur_ops->cb_barrier != NULL)
1815 cur_ops->cb_barrier();
1816
1817 rcu_torture_stats_print(); /* -After- the stats thread is stopped! */
1818
1819 if (atomic_read(&n_rcu_torture_error) || n_rcu_torture_barrier_error)
1820 rcu_torture_print_module_parms(cur_ops, "End of test: FAILURE");
1821 else if (n_online_successes != n_online_attempts ||
1822 n_offline_successes != n_offline_attempts)
1823 rcu_torture_print_module_parms(cur_ops,
1824 "End of test: RCU_HOTPLUG");
1825 else
1826 rcu_torture_print_module_parms(cur_ops, "End of test: SUCCESS");
1827 }
1828
1829 #ifdef CONFIG_DEBUG_OBJECTS_RCU_HEAD
1830 static void rcu_torture_leak_cb(struct rcu_head *rhp)
1831 {
1832 }
1833
1834 static void rcu_torture_err_cb(struct rcu_head *rhp)
1835 {
1836 /*
1837 * This -might- happen due to race conditions, but is unlikely.
1838 * The scenario that leads to this happening is that the
1839 * first of the pair of duplicate callbacks is queued,
1840 * someone else starts a grace period that includes that
1841 * callback, then the second of the pair must wait for the
1842 * next grace period. Unlikely, but can happen. If it
1843 * does happen, the debug-objects subsystem won't have splatted.
1844 */
1845 pr_alert("rcutorture: duplicated callback was invoked.\n");
1846 }
1847 #endif /* #ifdef CONFIG_DEBUG_OBJECTS_RCU_HEAD */
1848
1849 /*
1850 * Verify that double-free causes debug-objects to complain, but only
1851 * if CONFIG_DEBUG_OBJECTS_RCU_HEAD=y. Otherwise, say that the test
1852 * cannot be carried out.
1853 */
1854 static void rcu_test_debug_objects(void)
1855 {
1856 #ifdef CONFIG_DEBUG_OBJECTS_RCU_HEAD
1857 struct rcu_head rh1;
1858 struct rcu_head rh2;
1859
1860 init_rcu_head_on_stack(&rh1);
1861 init_rcu_head_on_stack(&rh2);
1862 pr_alert("rcutorture: WARN: Duplicate call_rcu() test starting.\n");
1863
1864 /* Try to queue the rh2 pair of callbacks for the same grace period. */
1865 preempt_disable(); /* Prevent preemption from interrupting test. */
1866 rcu_read_lock(); /* Make it impossible to finish a grace period. */
1867 call_rcu(&rh1, rcu_torture_leak_cb); /* Start grace period. */
1868 local_irq_disable(); /* Make it harder to start a new grace period. */
1869 call_rcu(&rh2, rcu_torture_leak_cb);
1870 call_rcu(&rh2, rcu_torture_err_cb); /* Duplicate callback. */
1871 local_irq_enable();
1872 rcu_read_unlock();
1873 preempt_enable();
1874
1875 /* Wait for them all to get done so we can safely return. */
1876 rcu_barrier();
1877 pr_alert("rcutorture: WARN: Duplicate call_rcu() test complete.\n");
1878 destroy_rcu_head_on_stack(&rh1);
1879 destroy_rcu_head_on_stack(&rh2);
1880 #else /* #ifdef CONFIG_DEBUG_OBJECTS_RCU_HEAD */
1881 pr_alert("rcutorture: !CONFIG_DEBUG_OBJECTS_RCU_HEAD, not testing duplicate call_rcu()\n");
1882 #endif /* #else #ifdef CONFIG_DEBUG_OBJECTS_RCU_HEAD */
1883 }
1884
1885 static int __init
1886 rcu_torture_init(void)
1887 {
1888 int i;
1889 int cpu;
1890 int firsterr = 0;
1891 int retval;
1892 static struct rcu_torture_ops *torture_ops[] = {
1893 &rcu_ops, &rcu_bh_ops, &srcu_ops, &sched_ops,
1894 };
1895
1896 mutex_lock(&fullstop_mutex);
1897
1898 /* Process args and tell the world that the torturer is on the job. */
1899 for (i = 0; i < ARRAY_SIZE(torture_ops); i++) {
1900 cur_ops = torture_ops[i];
1901 if (strcmp(torture_type, cur_ops->name) == 0)
1902 break;
1903 }
1904 if (i == ARRAY_SIZE(torture_ops)) {
1905 pr_alert("rcu-torture: invalid torture type: \"%s\"\n",
1906 torture_type);
1907 pr_alert("rcu-torture types:");
1908 for (i = 0; i < ARRAY_SIZE(torture_ops); i++)
1909 pr_alert(" %s", torture_ops[i]->name);
1910 pr_alert("\n");
1911 mutex_unlock(&fullstop_mutex);
1912 return -EINVAL;
1913 }
1914 if (cur_ops->fqs == NULL && fqs_duration != 0) {
1915 pr_alert("rcu-torture: ->fqs NULL and non-zero fqs_duration, fqs disabled.\n");
1916 fqs_duration = 0;
1917 }
1918 if (cur_ops->init)
1919 cur_ops->init(); /* no "goto unwind" prior to this point!!! */
1920
1921 if (nreaders >= 0)
1922 nrealreaders = nreaders;
1923 else
1924 nrealreaders = 2 * num_online_cpus();
1925 rcu_torture_print_module_parms(cur_ops, "Start of test");
1926 fullstop = FULLSTOP_DONTSTOP;
1927
1928 /* Set up the freelist. */
1929
1930 INIT_LIST_HEAD(&rcu_torture_freelist);
1931 for (i = 0; i < ARRAY_SIZE(rcu_tortures); i++) {
1932 rcu_tortures[i].rtort_mbtest = 0;
1933 list_add_tail(&rcu_tortures[i].rtort_free,
1934 &rcu_torture_freelist);
1935 }
1936
1937 /* Initialize the statistics so that each run gets its own numbers. */
1938
1939 rcu_torture_current = NULL;
1940 rcu_torture_current_version = 0;
1941 atomic_set(&n_rcu_torture_alloc, 0);
1942 atomic_set(&n_rcu_torture_alloc_fail, 0);
1943 atomic_set(&n_rcu_torture_free, 0);
1944 atomic_set(&n_rcu_torture_mberror, 0);
1945 atomic_set(&n_rcu_torture_error, 0);
1946 n_rcu_torture_barrier_error = 0;
1947 n_rcu_torture_boost_ktrerror = 0;
1948 n_rcu_torture_boost_rterror = 0;
1949 n_rcu_torture_boost_failure = 0;
1950 n_rcu_torture_boosts = 0;
1951 for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++)
1952 atomic_set(&rcu_torture_wcount[i], 0);
1953 for_each_possible_cpu(cpu) {
1954 for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++) {
1955 per_cpu(rcu_torture_count, cpu)[i] = 0;
1956 per_cpu(rcu_torture_batch, cpu)[i] = 0;
1957 }
1958 }
1959
1960 /* Start up the kthreads. */
1961
1962 VERBOSE_PRINTK_STRING("Creating rcu_torture_writer task");
1963 writer_task = kthread_create(rcu_torture_writer, NULL,
1964 "rcu_torture_writer");
1965 if (IS_ERR(writer_task)) {
1966 firsterr = PTR_ERR(writer_task);
1967 VERBOSE_PRINTK_ERRSTRING("Failed to create writer");
1968 writer_task = NULL;
1969 goto unwind;
1970 }
1971 wake_up_process(writer_task);
1972 fakewriter_tasks = kzalloc(nfakewriters * sizeof(fakewriter_tasks[0]),
1973 GFP_KERNEL);
1974 if (fakewriter_tasks == NULL) {
1975 VERBOSE_PRINTK_ERRSTRING("out of memory");
1976 firsterr = -ENOMEM;
1977 goto unwind;
1978 }
1979 for (i = 0; i < nfakewriters; i++) {
1980 VERBOSE_PRINTK_STRING("Creating rcu_torture_fakewriter task");
1981 fakewriter_tasks[i] = kthread_run(rcu_torture_fakewriter, NULL,
1982 "rcu_torture_fakewriter");
1983 if (IS_ERR(fakewriter_tasks[i])) {
1984 firsterr = PTR_ERR(fakewriter_tasks[i]);
1985 VERBOSE_PRINTK_ERRSTRING("Failed to create fakewriter");
1986 fakewriter_tasks[i] = NULL;
1987 goto unwind;
1988 }
1989 }
1990 reader_tasks = kzalloc(nrealreaders * sizeof(reader_tasks[0]),
1991 GFP_KERNEL);
1992 if (reader_tasks == NULL) {
1993 VERBOSE_PRINTK_ERRSTRING("out of memory");
1994 firsterr = -ENOMEM;
1995 goto unwind;
1996 }
1997 for (i = 0; i < nrealreaders; i++) {
1998 VERBOSE_PRINTK_STRING("Creating rcu_torture_reader task");
1999 reader_tasks[i] = kthread_run(rcu_torture_reader, NULL,
2000 "rcu_torture_reader");
2001 if (IS_ERR(reader_tasks[i])) {
2002 firsterr = PTR_ERR(reader_tasks[i]);
2003 VERBOSE_PRINTK_ERRSTRING("Failed to create reader");
2004 reader_tasks[i] = NULL;
2005 goto unwind;
2006 }
2007 }
2008 if (stat_interval > 0) {
2009 VERBOSE_PRINTK_STRING("Creating rcu_torture_stats task");
2010 stats_task = kthread_run(rcu_torture_stats, NULL,
2011 "rcu_torture_stats");
2012 if (IS_ERR(stats_task)) {
2013 firsterr = PTR_ERR(stats_task);
2014 VERBOSE_PRINTK_ERRSTRING("Failed to create stats");
2015 stats_task = NULL;
2016 goto unwind;
2017 }
2018 }
2019 if (test_no_idle_hz) {
2020 rcu_idle_cpu = num_online_cpus() - 1;
2021
2022 if (!alloc_cpumask_var(&shuffle_tmp_mask, GFP_KERNEL)) {
2023 firsterr = -ENOMEM;
2024 VERBOSE_PRINTK_ERRSTRING("Failed to alloc mask");
2025 goto unwind;
2026 }
2027
2028 /* Create the shuffler thread */
2029 shuffler_task = kthread_run(rcu_torture_shuffle, NULL,
2030 "rcu_torture_shuffle");
2031 if (IS_ERR(shuffler_task)) {
2032 free_cpumask_var(shuffle_tmp_mask);
2033 firsterr = PTR_ERR(shuffler_task);
2034 VERBOSE_PRINTK_ERRSTRING("Failed to create shuffler");
2035 shuffler_task = NULL;
2036 goto unwind;
2037 }
2038 }
2039 if (stutter < 0)
2040 stutter = 0;
2041 if (stutter) {
2042 /* Create the stutter thread */
2043 stutter_task = kthread_run(rcu_torture_stutter, NULL,
2044 "rcu_torture_stutter");
2045 if (IS_ERR(stutter_task)) {
2046 firsterr = PTR_ERR(stutter_task);
2047 VERBOSE_PRINTK_ERRSTRING("Failed to create stutter");
2048 stutter_task = NULL;
2049 goto unwind;
2050 }
2051 }
2052 if (fqs_duration < 0)
2053 fqs_duration = 0;
2054 if (fqs_duration) {
2055 /* Create the stutter thread */
2056 fqs_task = kthread_run(rcu_torture_fqs, NULL,
2057 "rcu_torture_fqs");
2058 if (IS_ERR(fqs_task)) {
2059 firsterr = PTR_ERR(fqs_task);
2060 VERBOSE_PRINTK_ERRSTRING("Failed to create fqs");
2061 fqs_task = NULL;
2062 goto unwind;
2063 }
2064 }
2065 if (test_boost_interval < 1)
2066 test_boost_interval = 1;
2067 if (test_boost_duration < 2)
2068 test_boost_duration = 2;
2069 if ((test_boost == 1 && cur_ops->can_boost) ||
2070 test_boost == 2) {
2071
2072 boost_starttime = jiffies + test_boost_interval * HZ;
2073 register_cpu_notifier(&rcutorture_cpu_nb);
2074 for_each_possible_cpu(i) {
2075 if (cpu_is_offline(i))
2076 continue; /* Heuristic: CPU can go offline. */
2077 retval = rcutorture_booster_init(i);
2078 if (retval < 0) {
2079 firsterr = retval;
2080 goto unwind;
2081 }
2082 }
2083 }
2084 if (shutdown_secs > 0) {
2085 shutdown_time = jiffies + shutdown_secs * HZ;
2086 shutdown_task = kthread_create(rcu_torture_shutdown, NULL,
2087 "rcu_torture_shutdown");
2088 if (IS_ERR(shutdown_task)) {
2089 firsterr = PTR_ERR(shutdown_task);
2090 VERBOSE_PRINTK_ERRSTRING("Failed to create shutdown");
2091 shutdown_task = NULL;
2092 goto unwind;
2093 }
2094 wake_up_process(shutdown_task);
2095 }
2096 i = rcu_torture_onoff_init();
2097 if (i != 0) {
2098 firsterr = i;
2099 goto unwind;
2100 }
2101 register_reboot_notifier(&rcutorture_shutdown_nb);
2102 i = rcu_torture_stall_init();
2103 if (i != 0) {
2104 firsterr = i;
2105 goto unwind;
2106 }
2107 retval = rcu_torture_barrier_init();
2108 if (retval != 0) {
2109 firsterr = retval;
2110 goto unwind;
2111 }
2112 if (object_debug)
2113 rcu_test_debug_objects();
2114 rcutorture_record_test_transition();
2115 mutex_unlock(&fullstop_mutex);
2116 return 0;
2117
2118 unwind:
2119 mutex_unlock(&fullstop_mutex);
2120 rcu_torture_cleanup();
2121 return firsterr;
2122 }
2123
2124 module_init(rcu_torture_init);
2125 module_exit(rcu_torture_cleanup);
This page took 0.080336 seconds and 5 git commands to generate.