693a90fcee83873b5107664b41f20a6c92b78512
[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, you can access it online at
16 * http://www.gnu.org/licenses/gpl-2.0.html.
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
57 torture_param(int, fqs_duration, 0,
58 "Duration of fqs bursts (us), 0 to disable");
59 torture_param(int, fqs_holdoff, 0, "Holdoff time within fqs bursts (us)");
60 torture_param(int, fqs_stutter, 3, "Wait time between fqs bursts (s)");
61 torture_param(bool, gp_exp, false, "Use expedited GP wait primitives");
62 torture_param(bool, gp_normal, false,
63 "Use normal (non-expedited) GP wait primitives");
64 torture_param(int, irqreader, 1, "Allow RCU readers from irq handlers");
65 torture_param(int, n_barrier_cbs, 0,
66 "# of callbacks/kthreads for barrier testing");
67 torture_param(int, nfakewriters, 4, "Number of RCU fake writer threads");
68 torture_param(int, nreaders, -1, "Number of RCU reader threads");
69 torture_param(int, object_debug, 0,
70 "Enable debug-object double call_rcu() testing");
71 torture_param(int, onoff_holdoff, 0, "Time after boot before CPU hotplugs (s)");
72 torture_param(int, onoff_interval, 0,
73 "Time between CPU hotplugs (s), 0=disable");
74 torture_param(int, shuffle_interval, 3, "Number of seconds between shuffles");
75 torture_param(int, shutdown_secs, 0, "Shutdown time (s), <= zero to disable.");
76 torture_param(int, stall_cpu, 0, "Stall duration (s), zero to disable.");
77 torture_param(int, stall_cpu_holdoff, 10,
78 "Time to wait before starting stall (s).");
79 torture_param(int, stat_interval, 60,
80 "Number of seconds between stats printk()s");
81 torture_param(int, stutter, 5, "Number of seconds to run/halt test");
82 torture_param(int, test_boost, 1, "Test RCU prio boost: 0=no, 1=maybe, 2=yes.");
83 torture_param(int, test_boost_duration, 4,
84 "Duration of each boost test, seconds.");
85 torture_param(int, test_boost_interval, 7,
86 "Interval between boost tests, seconds.");
87 torture_param(bool, test_no_idle_hz, true,
88 "Test support for tickless idle CPUs");
89 torture_param(bool, verbose, true,
90 "Enable verbose debugging printk()s");
91
92 static char *torture_type = "rcu";
93 module_param(torture_type, charp, 0444);
94 MODULE_PARM_DESC(torture_type, "Type of RCU to torture (rcu, rcu_bh, ...)");
95
96 static int nrealreaders;
97 static struct task_struct *writer_task;
98 static struct task_struct **fakewriter_tasks;
99 static struct task_struct **reader_tasks;
100 static struct task_struct *stats_task;
101 static struct task_struct *fqs_task;
102 static struct task_struct *boost_tasks[NR_CPUS];
103 static struct task_struct *stall_task;
104 static struct task_struct **barrier_cbs_tasks;
105 static struct task_struct *barrier_task;
106
107 #define RCU_TORTURE_PIPE_LEN 10
108
109 struct rcu_torture {
110 struct rcu_head rtort_rcu;
111 int rtort_pipe_count;
112 struct list_head rtort_free;
113 int rtort_mbtest;
114 };
115
116 static LIST_HEAD(rcu_torture_freelist);
117 static struct rcu_torture __rcu *rcu_torture_current;
118 static unsigned long rcu_torture_current_version;
119 static struct rcu_torture rcu_tortures[10 * RCU_TORTURE_PIPE_LEN];
120 static DEFINE_SPINLOCK(rcu_torture_lock);
121 static DEFINE_PER_CPU(long [RCU_TORTURE_PIPE_LEN + 1],
122 rcu_torture_count) = { 0 };
123 static DEFINE_PER_CPU(long [RCU_TORTURE_PIPE_LEN + 1],
124 rcu_torture_batch) = { 0 };
125 static atomic_t rcu_torture_wcount[RCU_TORTURE_PIPE_LEN + 1];
126 static atomic_t n_rcu_torture_alloc;
127 static atomic_t n_rcu_torture_alloc_fail;
128 static atomic_t n_rcu_torture_free;
129 static atomic_t n_rcu_torture_mberror;
130 static atomic_t n_rcu_torture_error;
131 static long n_rcu_torture_barrier_error;
132 static long n_rcu_torture_boost_ktrerror;
133 static long n_rcu_torture_boost_rterror;
134 static long n_rcu_torture_boost_failure;
135 static long n_rcu_torture_boosts;
136 static long n_rcu_torture_timers;
137 static long n_barrier_attempts;
138 static long n_barrier_successes;
139 static struct list_head rcu_torture_removed;
140
141 static int rcu_torture_writer_state;
142 #define RTWS_FIXED_DELAY 0
143 #define RTWS_DELAY 1
144 #define RTWS_REPLACE 2
145 #define RTWS_DEF_FREE 3
146 #define RTWS_EXP_SYNC 4
147 #define RTWS_STUTTER 5
148 #define RTWS_STOPPING 6
149
150 #if defined(MODULE) || defined(CONFIG_RCU_TORTURE_TEST_RUNNABLE)
151 #define RCUTORTURE_RUNNABLE_INIT 1
152 #else
153 #define RCUTORTURE_RUNNABLE_INIT 0
154 #endif
155 int rcutorture_runnable = RCUTORTURE_RUNNABLE_INIT;
156 module_param(rcutorture_runnable, int, 0444);
157 MODULE_PARM_DESC(rcutorture_runnable, "Start rcutorture at boot");
158
159 #if defined(CONFIG_RCU_BOOST) && !defined(CONFIG_HOTPLUG_CPU)
160 #define rcu_can_boost() 1
161 #else /* #if defined(CONFIG_RCU_BOOST) && !defined(CONFIG_HOTPLUG_CPU) */
162 #define rcu_can_boost() 0
163 #endif /* #else #if defined(CONFIG_RCU_BOOST) && !defined(CONFIG_HOTPLUG_CPU) */
164
165 #ifdef CONFIG_RCU_TRACE
166 static u64 notrace rcu_trace_clock_local(void)
167 {
168 u64 ts = trace_clock_local();
169 unsigned long __maybe_unused ts_rem = do_div(ts, NSEC_PER_USEC);
170 return ts;
171 }
172 #else /* #ifdef CONFIG_RCU_TRACE */
173 static u64 notrace rcu_trace_clock_local(void)
174 {
175 return 0ULL;
176 }
177 #endif /* #else #ifdef CONFIG_RCU_TRACE */
178
179 static unsigned long boost_starttime; /* jiffies of next boost test start. */
180 DEFINE_MUTEX(boost_mutex); /* protect setting boost_starttime */
181 /* and boost task create/destroy. */
182 static atomic_t barrier_cbs_count; /* Barrier callbacks registered. */
183 static bool barrier_phase; /* Test phase. */
184 static atomic_t barrier_cbs_invoked; /* Barrier callbacks invoked. */
185 static wait_queue_head_t *barrier_cbs_wq; /* Coordinate barrier testing. */
186 static DECLARE_WAIT_QUEUE_HEAD(barrier_wq);
187
188 /*
189 * Allocate an element from the rcu_tortures pool.
190 */
191 static struct rcu_torture *
192 rcu_torture_alloc(void)
193 {
194 struct list_head *p;
195
196 spin_lock_bh(&rcu_torture_lock);
197 if (list_empty(&rcu_torture_freelist)) {
198 atomic_inc(&n_rcu_torture_alloc_fail);
199 spin_unlock_bh(&rcu_torture_lock);
200 return NULL;
201 }
202 atomic_inc(&n_rcu_torture_alloc);
203 p = rcu_torture_freelist.next;
204 list_del_init(p);
205 spin_unlock_bh(&rcu_torture_lock);
206 return container_of(p, struct rcu_torture, rtort_free);
207 }
208
209 /*
210 * Free an element to the rcu_tortures pool.
211 */
212 static void
213 rcu_torture_free(struct rcu_torture *p)
214 {
215 atomic_inc(&n_rcu_torture_free);
216 spin_lock_bh(&rcu_torture_lock);
217 list_add_tail(&p->rtort_free, &rcu_torture_freelist);
218 spin_unlock_bh(&rcu_torture_lock);
219 }
220
221 /*
222 * Operations vector for selecting different types of tests.
223 */
224
225 struct rcu_torture_ops {
226 int ttype;
227 void (*init)(void);
228 int (*readlock)(void);
229 void (*read_delay)(struct torture_random_state *rrsp);
230 void (*readunlock)(int idx);
231 int (*completed)(void);
232 void (*deferred_free)(struct rcu_torture *p);
233 void (*sync)(void);
234 void (*exp_sync)(void);
235 void (*call)(struct rcu_head *head, void (*func)(struct rcu_head *rcu));
236 void (*cb_barrier)(void);
237 void (*fqs)(void);
238 void (*stats)(char *page);
239 int irq_capable;
240 int can_boost;
241 const char *name;
242 };
243
244 static struct rcu_torture_ops *cur_ops;
245
246 /*
247 * Definitions for rcu torture testing.
248 */
249
250 static int rcu_torture_read_lock(void) __acquires(RCU)
251 {
252 rcu_read_lock();
253 return 0;
254 }
255
256 static void rcu_read_delay(struct torture_random_state *rrsp)
257 {
258 const unsigned long shortdelay_us = 200;
259 const unsigned long longdelay_ms = 50;
260
261 /* We want a short delay sometimes to make a reader delay the grace
262 * period, and we want a long delay occasionally to trigger
263 * force_quiescent_state. */
264
265 if (!(torture_random(rrsp) % (nrealreaders * 2000 * longdelay_ms)))
266 mdelay(longdelay_ms);
267 if (!(torture_random(rrsp) % (nrealreaders * 2 * shortdelay_us)))
268 udelay(shortdelay_us);
269 #ifdef CONFIG_PREEMPT
270 if (!preempt_count() &&
271 !(torture_random(rrsp) % (nrealreaders * 20000)))
272 preempt_schedule(); /* No QS if preempt_disable() in effect */
273 #endif
274 }
275
276 static void rcu_torture_read_unlock(int idx) __releases(RCU)
277 {
278 rcu_read_unlock();
279 }
280
281 static int rcu_torture_completed(void)
282 {
283 return rcu_batches_completed();
284 }
285
286 static void
287 rcu_torture_cb(struct rcu_head *p)
288 {
289 int i;
290 struct rcu_torture *rp = container_of(p, struct rcu_torture, rtort_rcu);
291
292 if (torture_must_stop_irq()) {
293 /* Test is ending, just drop callbacks on the floor. */
294 /* The next initialization will pick up the pieces. */
295 return;
296 }
297 i = rp->rtort_pipe_count;
298 if (i > RCU_TORTURE_PIPE_LEN)
299 i = RCU_TORTURE_PIPE_LEN;
300 atomic_inc(&rcu_torture_wcount[i]);
301 if (++rp->rtort_pipe_count >= RCU_TORTURE_PIPE_LEN) {
302 rp->rtort_mbtest = 0;
303 rcu_torture_free(rp);
304 } else {
305 cur_ops->deferred_free(rp);
306 }
307 }
308
309 static int rcu_no_completed(void)
310 {
311 return 0;
312 }
313
314 static void rcu_torture_deferred_free(struct rcu_torture *p)
315 {
316 call_rcu(&p->rtort_rcu, rcu_torture_cb);
317 }
318
319 static void rcu_sync_torture_init(void)
320 {
321 INIT_LIST_HEAD(&rcu_torture_removed);
322 }
323
324 static struct rcu_torture_ops rcu_ops = {
325 .ttype = RCU_FLAVOR,
326 .init = rcu_sync_torture_init,
327 .readlock = rcu_torture_read_lock,
328 .read_delay = rcu_read_delay,
329 .readunlock = rcu_torture_read_unlock,
330 .completed = rcu_torture_completed,
331 .deferred_free = rcu_torture_deferred_free,
332 .sync = synchronize_rcu,
333 .exp_sync = synchronize_rcu_expedited,
334 .call = call_rcu,
335 .cb_barrier = rcu_barrier,
336 .fqs = rcu_force_quiescent_state,
337 .stats = NULL,
338 .irq_capable = 1,
339 .can_boost = rcu_can_boost(),
340 .name = "rcu"
341 };
342
343 /*
344 * Definitions for rcu_bh torture testing.
345 */
346
347 static int rcu_bh_torture_read_lock(void) __acquires(RCU_BH)
348 {
349 rcu_read_lock_bh();
350 return 0;
351 }
352
353 static void rcu_bh_torture_read_unlock(int idx) __releases(RCU_BH)
354 {
355 rcu_read_unlock_bh();
356 }
357
358 static int rcu_bh_torture_completed(void)
359 {
360 return rcu_batches_completed_bh();
361 }
362
363 static void rcu_bh_torture_deferred_free(struct rcu_torture *p)
364 {
365 call_rcu_bh(&p->rtort_rcu, rcu_torture_cb);
366 }
367
368 static struct rcu_torture_ops rcu_bh_ops = {
369 .ttype = RCU_BH_FLAVOR,
370 .init = rcu_sync_torture_init,
371 .readlock = rcu_bh_torture_read_lock,
372 .read_delay = rcu_read_delay, /* just reuse rcu's version. */
373 .readunlock = rcu_bh_torture_read_unlock,
374 .completed = rcu_bh_torture_completed,
375 .deferred_free = rcu_bh_torture_deferred_free,
376 .sync = synchronize_rcu_bh,
377 .exp_sync = synchronize_rcu_bh_expedited,
378 .call = call_rcu_bh,
379 .cb_barrier = rcu_barrier_bh,
380 .fqs = rcu_bh_force_quiescent_state,
381 .stats = NULL,
382 .irq_capable = 1,
383 .name = "rcu_bh"
384 };
385
386 /*
387 * Don't even think about trying any of these in real life!!!
388 * The names includes "busted", and they really means it!
389 * The only purpose of these functions is to provide a buggy RCU
390 * implementation to make sure that rcutorture correctly emits
391 * buggy-RCU error messages.
392 */
393 static void rcu_busted_torture_deferred_free(struct rcu_torture *p)
394 {
395 /* This is a deliberate bug for testing purposes only! */
396 rcu_torture_cb(&p->rtort_rcu);
397 }
398
399 static void synchronize_rcu_busted(void)
400 {
401 /* This is a deliberate bug for testing purposes only! */
402 }
403
404 static void
405 call_rcu_busted(struct rcu_head *head, void (*func)(struct rcu_head *rcu))
406 {
407 /* This is a deliberate bug for testing purposes only! */
408 func(head);
409 }
410
411 static struct rcu_torture_ops rcu_busted_ops = {
412 .ttype = INVALID_RCU_FLAVOR,
413 .init = rcu_sync_torture_init,
414 .readlock = rcu_torture_read_lock,
415 .read_delay = rcu_read_delay, /* just reuse rcu's version. */
416 .readunlock = rcu_torture_read_unlock,
417 .completed = rcu_no_completed,
418 .deferred_free = rcu_busted_torture_deferred_free,
419 .sync = synchronize_rcu_busted,
420 .exp_sync = synchronize_rcu_busted,
421 .call = call_rcu_busted,
422 .cb_barrier = NULL,
423 .fqs = NULL,
424 .stats = NULL,
425 .irq_capable = 1,
426 .name = "rcu_busted"
427 };
428
429 /*
430 * Definitions for srcu torture testing.
431 */
432
433 DEFINE_STATIC_SRCU(srcu_ctl);
434
435 static int srcu_torture_read_lock(void) __acquires(&srcu_ctl)
436 {
437 return srcu_read_lock(&srcu_ctl);
438 }
439
440 static void srcu_read_delay(struct torture_random_state *rrsp)
441 {
442 long delay;
443 const long uspertick = 1000000 / HZ;
444 const long longdelay = 10;
445
446 /* We want there to be long-running readers, but not all the time. */
447
448 delay = torture_random(rrsp) %
449 (nrealreaders * 2 * longdelay * uspertick);
450 if (!delay)
451 schedule_timeout_interruptible(longdelay);
452 else
453 rcu_read_delay(rrsp);
454 }
455
456 static void srcu_torture_read_unlock(int idx) __releases(&srcu_ctl)
457 {
458 srcu_read_unlock(&srcu_ctl, idx);
459 }
460
461 static int srcu_torture_completed(void)
462 {
463 return srcu_batches_completed(&srcu_ctl);
464 }
465
466 static void srcu_torture_deferred_free(struct rcu_torture *rp)
467 {
468 call_srcu(&srcu_ctl, &rp->rtort_rcu, rcu_torture_cb);
469 }
470
471 static void srcu_torture_synchronize(void)
472 {
473 synchronize_srcu(&srcu_ctl);
474 }
475
476 static void srcu_torture_call(struct rcu_head *head,
477 void (*func)(struct rcu_head *head))
478 {
479 call_srcu(&srcu_ctl, head, func);
480 }
481
482 static void srcu_torture_barrier(void)
483 {
484 srcu_barrier(&srcu_ctl);
485 }
486
487 static void srcu_torture_stats(char *page)
488 {
489 int cpu;
490 int idx = srcu_ctl.completed & 0x1;
491
492 page += sprintf(page, "%s%s per-CPU(idx=%d):",
493 torture_type, TORTURE_FLAG, idx);
494 for_each_possible_cpu(cpu) {
495 long c0, c1;
496
497 c0 = (long)per_cpu_ptr(srcu_ctl.per_cpu_ref, cpu)->c[!idx];
498 c1 = (long)per_cpu_ptr(srcu_ctl.per_cpu_ref, cpu)->c[idx];
499 page += sprintf(page, " %d(%ld,%ld)", cpu, c0, c1);
500 }
501 sprintf(page, "\n");
502 }
503
504 static void srcu_torture_synchronize_expedited(void)
505 {
506 synchronize_srcu_expedited(&srcu_ctl);
507 }
508
509 static struct rcu_torture_ops srcu_ops = {
510 .ttype = SRCU_FLAVOR,
511 .init = rcu_sync_torture_init,
512 .readlock = srcu_torture_read_lock,
513 .read_delay = srcu_read_delay,
514 .readunlock = srcu_torture_read_unlock,
515 .completed = srcu_torture_completed,
516 .deferred_free = srcu_torture_deferred_free,
517 .sync = srcu_torture_synchronize,
518 .exp_sync = srcu_torture_synchronize_expedited,
519 .call = srcu_torture_call,
520 .cb_barrier = srcu_torture_barrier,
521 .stats = srcu_torture_stats,
522 .name = "srcu"
523 };
524
525 /*
526 * Definitions for sched torture testing.
527 */
528
529 static int sched_torture_read_lock(void)
530 {
531 preempt_disable();
532 return 0;
533 }
534
535 static void sched_torture_read_unlock(int idx)
536 {
537 preempt_enable();
538 }
539
540 static void rcu_sched_torture_deferred_free(struct rcu_torture *p)
541 {
542 call_rcu_sched(&p->rtort_rcu, rcu_torture_cb);
543 }
544
545 static struct rcu_torture_ops sched_ops = {
546 .ttype = RCU_SCHED_FLAVOR,
547 .init = rcu_sync_torture_init,
548 .readlock = sched_torture_read_lock,
549 .read_delay = rcu_read_delay, /* just reuse rcu's version. */
550 .readunlock = sched_torture_read_unlock,
551 .completed = rcu_no_completed,
552 .deferred_free = rcu_sched_torture_deferred_free,
553 .sync = synchronize_sched,
554 .exp_sync = synchronize_sched_expedited,
555 .call = call_rcu_sched,
556 .cb_barrier = rcu_barrier_sched,
557 .fqs = rcu_sched_force_quiescent_state,
558 .stats = NULL,
559 .irq_capable = 1,
560 .name = "sched"
561 };
562
563 /*
564 * RCU torture priority-boost testing. Runs one real-time thread per
565 * CPU for moderate bursts, repeatedly registering RCU callbacks and
566 * spinning waiting for them to be invoked. If a given callback takes
567 * too long to be invoked, we assume that priority inversion has occurred.
568 */
569
570 struct rcu_boost_inflight {
571 struct rcu_head rcu;
572 int inflight;
573 };
574
575 static void rcu_torture_boost_cb(struct rcu_head *head)
576 {
577 struct rcu_boost_inflight *rbip =
578 container_of(head, struct rcu_boost_inflight, rcu);
579
580 smp_mb(); /* Ensure RCU-core accesses precede clearing ->inflight */
581 rbip->inflight = 0;
582 }
583
584 static int rcu_torture_boost(void *arg)
585 {
586 unsigned long call_rcu_time;
587 unsigned long endtime;
588 unsigned long oldstarttime;
589 struct rcu_boost_inflight rbi = { .inflight = 0 };
590 struct sched_param sp;
591
592 VERBOSE_TOROUT_STRING("rcu_torture_boost started");
593
594 /* Set real-time priority. */
595 sp.sched_priority = 1;
596 if (sched_setscheduler(current, SCHED_FIFO, &sp) < 0) {
597 VERBOSE_TOROUT_STRING("rcu_torture_boost RT prio failed!");
598 n_rcu_torture_boost_rterror++;
599 }
600
601 init_rcu_head_on_stack(&rbi.rcu);
602 /* Each pass through the following loop does one boost-test cycle. */
603 do {
604 /* Wait for the next test interval. */
605 oldstarttime = boost_starttime;
606 while (ULONG_CMP_LT(jiffies, oldstarttime)) {
607 schedule_timeout_interruptible(oldstarttime - jiffies);
608 stutter_wait("rcu_torture_boost");
609 if (torture_must_stop())
610 goto checkwait;
611 }
612
613 /* Do one boost-test interval. */
614 endtime = oldstarttime + test_boost_duration * HZ;
615 call_rcu_time = jiffies;
616 while (ULONG_CMP_LT(jiffies, endtime)) {
617 /* If we don't have a callback in flight, post one. */
618 if (!rbi.inflight) {
619 smp_mb(); /* RCU core before ->inflight = 1. */
620 rbi.inflight = 1;
621 call_rcu(&rbi.rcu, rcu_torture_boost_cb);
622 if (jiffies - call_rcu_time >
623 test_boost_duration * HZ - HZ / 2) {
624 VERBOSE_TOROUT_STRING("rcu_torture_boost boosting failed");
625 n_rcu_torture_boost_failure++;
626 }
627 call_rcu_time = jiffies;
628 }
629 cond_resched();
630 stutter_wait("rcu_torture_boost");
631 if (torture_must_stop())
632 goto checkwait;
633 }
634
635 /*
636 * Set the start time of the next test interval.
637 * Yes, this is vulnerable to long delays, but such
638 * delays simply cause a false negative for the next
639 * interval. Besides, we are running at RT priority,
640 * so delays should be relatively rare.
641 */
642 while (oldstarttime == boost_starttime &&
643 !kthread_should_stop()) {
644 if (mutex_trylock(&boost_mutex)) {
645 boost_starttime = jiffies +
646 test_boost_interval * HZ;
647 n_rcu_torture_boosts++;
648 mutex_unlock(&boost_mutex);
649 break;
650 }
651 schedule_timeout_uninterruptible(1);
652 }
653
654 /* Go do the stutter. */
655 checkwait: stutter_wait("rcu_torture_boost");
656 } while (!torture_must_stop());
657
658 /* Clean up and exit. */
659 while (!kthread_should_stop() || rbi.inflight) {
660 torture_shutdown_absorb("rcu_torture_boost");
661 schedule_timeout_uninterruptible(1);
662 }
663 smp_mb(); /* order accesses to ->inflight before stack-frame death. */
664 destroy_rcu_head_on_stack(&rbi.rcu);
665 torture_kthread_stopping("rcu_torture_boost");
666 return 0;
667 }
668
669 /*
670 * RCU torture force-quiescent-state kthread. Repeatedly induces
671 * bursts of calls to force_quiescent_state(), increasing the probability
672 * of occurrence of some important types of race conditions.
673 */
674 static int
675 rcu_torture_fqs(void *arg)
676 {
677 unsigned long fqs_resume_time;
678 int fqs_burst_remaining;
679
680 VERBOSE_TOROUT_STRING("rcu_torture_fqs task started");
681 do {
682 fqs_resume_time = jiffies + fqs_stutter * HZ;
683 while (ULONG_CMP_LT(jiffies, fqs_resume_time) &&
684 !kthread_should_stop()) {
685 schedule_timeout_interruptible(1);
686 }
687 fqs_burst_remaining = fqs_duration;
688 while (fqs_burst_remaining > 0 &&
689 !kthread_should_stop()) {
690 cur_ops->fqs();
691 udelay(fqs_holdoff);
692 fqs_burst_remaining -= fqs_holdoff;
693 }
694 stutter_wait("rcu_torture_fqs");
695 } while (!torture_must_stop());
696 torture_kthread_stopping("rcu_torture_fqs");
697 return 0;
698 }
699
700 /*
701 * RCU torture writer kthread. Repeatedly substitutes a new structure
702 * for that pointed to by rcu_torture_current, freeing the old structure
703 * after a series of grace periods (the "pipeline").
704 */
705 static int
706 rcu_torture_writer(void *arg)
707 {
708 bool exp;
709 int i;
710 struct rcu_torture *rp;
711 struct rcu_torture *rp1;
712 struct rcu_torture *old_rp;
713 static DEFINE_TORTURE_RANDOM(rand);
714
715 VERBOSE_TOROUT_STRING("rcu_torture_writer task started");
716 set_user_nice(current, MAX_NICE);
717
718 do {
719 rcu_torture_writer_state = RTWS_FIXED_DELAY;
720 schedule_timeout_uninterruptible(1);
721 rp = rcu_torture_alloc();
722 if (rp == NULL)
723 continue;
724 rp->rtort_pipe_count = 0;
725 rcu_torture_writer_state = RTWS_DELAY;
726 udelay(torture_random(&rand) & 0x3ff);
727 rcu_torture_writer_state = RTWS_REPLACE;
728 old_rp = rcu_dereference_check(rcu_torture_current,
729 current == writer_task);
730 rp->rtort_mbtest = 1;
731 rcu_assign_pointer(rcu_torture_current, rp);
732 smp_wmb(); /* Mods to old_rp must follow rcu_assign_pointer() */
733 if (old_rp) {
734 i = old_rp->rtort_pipe_count;
735 if (i > RCU_TORTURE_PIPE_LEN)
736 i = RCU_TORTURE_PIPE_LEN;
737 atomic_inc(&rcu_torture_wcount[i]);
738 old_rp->rtort_pipe_count++;
739 if (gp_normal == gp_exp)
740 exp = !!(torture_random(&rand) & 0x80);
741 else
742 exp = gp_exp;
743 if (!exp) {
744 rcu_torture_writer_state = RTWS_DEF_FREE;
745 cur_ops->deferred_free(old_rp);
746 } else {
747 rcu_torture_writer_state = RTWS_EXP_SYNC;
748 cur_ops->exp_sync();
749 list_add(&old_rp->rtort_free,
750 &rcu_torture_removed);
751 list_for_each_entry_safe(rp, rp1,
752 &rcu_torture_removed,
753 rtort_free) {
754 i = rp->rtort_pipe_count;
755 if (i > RCU_TORTURE_PIPE_LEN)
756 i = RCU_TORTURE_PIPE_LEN;
757 atomic_inc(&rcu_torture_wcount[i]);
758 if (++rp->rtort_pipe_count >=
759 RCU_TORTURE_PIPE_LEN) {
760 rp->rtort_mbtest = 0;
761 list_del(&rp->rtort_free);
762 rcu_torture_free(rp);
763 }
764 }
765 }
766 }
767 rcutorture_record_progress(++rcu_torture_current_version);
768 rcu_torture_writer_state = RTWS_STUTTER;
769 stutter_wait("rcu_torture_writer");
770 } while (!torture_must_stop());
771 rcu_torture_writer_state = RTWS_STOPPING;
772 torture_kthread_stopping("rcu_torture_writer");
773 return 0;
774 }
775
776 /*
777 * RCU torture fake writer kthread. Repeatedly calls sync, with a random
778 * delay between calls.
779 */
780 static int
781 rcu_torture_fakewriter(void *arg)
782 {
783 DEFINE_TORTURE_RANDOM(rand);
784
785 VERBOSE_TOROUT_STRING("rcu_torture_fakewriter task started");
786 set_user_nice(current, MAX_NICE);
787
788 do {
789 schedule_timeout_uninterruptible(1 + torture_random(&rand)%10);
790 udelay(torture_random(&rand) & 0x3ff);
791 if (cur_ops->cb_barrier != NULL &&
792 torture_random(&rand) % (nfakewriters * 8) == 0) {
793 cur_ops->cb_barrier();
794 } else if (gp_normal == gp_exp) {
795 if (torture_random(&rand) & 0x80)
796 cur_ops->sync();
797 else
798 cur_ops->exp_sync();
799 } else if (gp_normal) {
800 cur_ops->sync();
801 } else {
802 cur_ops->exp_sync();
803 }
804 stutter_wait("rcu_torture_fakewriter");
805 } while (!torture_must_stop());
806
807 torture_kthread_stopping("rcu_torture_fakewriter");
808 return 0;
809 }
810
811 static void rcutorture_trace_dump(void)
812 {
813 static atomic_t beenhere = ATOMIC_INIT(0);
814
815 if (atomic_read(&beenhere))
816 return;
817 if (atomic_xchg(&beenhere, 1) != 0)
818 return;
819 ftrace_dump(DUMP_ALL);
820 }
821
822 /*
823 * RCU torture reader from timer handler. Dereferences rcu_torture_current,
824 * incrementing the corresponding element of the pipeline array. The
825 * counter in the element should never be greater than 1, otherwise, the
826 * RCU implementation is broken.
827 */
828 static void rcu_torture_timer(unsigned long unused)
829 {
830 int idx;
831 int completed;
832 int completed_end;
833 static DEFINE_TORTURE_RANDOM(rand);
834 static DEFINE_SPINLOCK(rand_lock);
835 struct rcu_torture *p;
836 int pipe_count;
837 unsigned long long ts;
838
839 idx = cur_ops->readlock();
840 completed = cur_ops->completed();
841 ts = rcu_trace_clock_local();
842 p = rcu_dereference_check(rcu_torture_current,
843 rcu_read_lock_bh_held() ||
844 rcu_read_lock_sched_held() ||
845 srcu_read_lock_held(&srcu_ctl));
846 if (p == NULL) {
847 /* Leave because rcu_torture_writer is not yet underway */
848 cur_ops->readunlock(idx);
849 return;
850 }
851 if (p->rtort_mbtest == 0)
852 atomic_inc(&n_rcu_torture_mberror);
853 spin_lock(&rand_lock);
854 cur_ops->read_delay(&rand);
855 n_rcu_torture_timers++;
856 spin_unlock(&rand_lock);
857 preempt_disable();
858 pipe_count = p->rtort_pipe_count;
859 if (pipe_count > RCU_TORTURE_PIPE_LEN) {
860 /* Should not happen, but... */
861 pipe_count = RCU_TORTURE_PIPE_LEN;
862 }
863 completed_end = cur_ops->completed();
864 if (pipe_count > 1) {
865 do_trace_rcu_torture_read(cur_ops->name, &p->rtort_rcu, ts,
866 completed, completed_end);
867 rcutorture_trace_dump();
868 }
869 __this_cpu_inc(rcu_torture_count[pipe_count]);
870 completed = completed_end - completed;
871 if (completed > RCU_TORTURE_PIPE_LEN) {
872 /* Should not happen, but... */
873 completed = RCU_TORTURE_PIPE_LEN;
874 }
875 __this_cpu_inc(rcu_torture_batch[completed]);
876 preempt_enable();
877 cur_ops->readunlock(idx);
878 }
879
880 /*
881 * RCU torture reader kthread. Repeatedly dereferences rcu_torture_current,
882 * incrementing the corresponding element of the pipeline array. The
883 * counter in the element should never be greater than 1, otherwise, the
884 * RCU implementation is broken.
885 */
886 static int
887 rcu_torture_reader(void *arg)
888 {
889 int completed;
890 int completed_end;
891 int idx;
892 DEFINE_TORTURE_RANDOM(rand);
893 struct rcu_torture *p;
894 int pipe_count;
895 struct timer_list t;
896 unsigned long long ts;
897
898 VERBOSE_TOROUT_STRING("rcu_torture_reader task started");
899 set_user_nice(current, MAX_NICE);
900 if (irqreader && cur_ops->irq_capable)
901 setup_timer_on_stack(&t, rcu_torture_timer, 0);
902
903 do {
904 if (irqreader && cur_ops->irq_capable) {
905 if (!timer_pending(&t))
906 mod_timer(&t, jiffies + 1);
907 }
908 idx = cur_ops->readlock();
909 completed = cur_ops->completed();
910 ts = rcu_trace_clock_local();
911 p = rcu_dereference_check(rcu_torture_current,
912 rcu_read_lock_bh_held() ||
913 rcu_read_lock_sched_held() ||
914 srcu_read_lock_held(&srcu_ctl));
915 if (p == NULL) {
916 /* Wait for rcu_torture_writer to get underway */
917 cur_ops->readunlock(idx);
918 schedule_timeout_interruptible(HZ);
919 continue;
920 }
921 if (p->rtort_mbtest == 0)
922 atomic_inc(&n_rcu_torture_mberror);
923 cur_ops->read_delay(&rand);
924 preempt_disable();
925 pipe_count = p->rtort_pipe_count;
926 if (pipe_count > RCU_TORTURE_PIPE_LEN) {
927 /* Should not happen, but... */
928 pipe_count = RCU_TORTURE_PIPE_LEN;
929 }
930 completed_end = cur_ops->completed();
931 if (pipe_count > 1) {
932 do_trace_rcu_torture_read(cur_ops->name, &p->rtort_rcu,
933 ts, completed, completed_end);
934 rcutorture_trace_dump();
935 }
936 __this_cpu_inc(rcu_torture_count[pipe_count]);
937 completed = completed_end - completed;
938 if (completed > RCU_TORTURE_PIPE_LEN) {
939 /* Should not happen, but... */
940 completed = RCU_TORTURE_PIPE_LEN;
941 }
942 __this_cpu_inc(rcu_torture_batch[completed]);
943 preempt_enable();
944 cur_ops->readunlock(idx);
945 cond_resched();
946 stutter_wait("rcu_torture_reader");
947 } while (!torture_must_stop());
948 if (irqreader && cur_ops->irq_capable)
949 del_timer_sync(&t);
950 torture_kthread_stopping("rcu_torture_reader");
951 return 0;
952 }
953
954 /*
955 * Create an RCU-torture statistics message in the specified buffer.
956 */
957 static void
958 rcu_torture_printk(char *page)
959 {
960 int cpu;
961 int i;
962 long pipesummary[RCU_TORTURE_PIPE_LEN + 1] = { 0 };
963 long batchsummary[RCU_TORTURE_PIPE_LEN + 1] = { 0 };
964 static unsigned long rtcv_snap = ULONG_MAX;
965
966 for_each_possible_cpu(cpu) {
967 for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++) {
968 pipesummary[i] += per_cpu(rcu_torture_count, cpu)[i];
969 batchsummary[i] += per_cpu(rcu_torture_batch, cpu)[i];
970 }
971 }
972 for (i = RCU_TORTURE_PIPE_LEN - 1; i >= 0; i--) {
973 if (pipesummary[i] != 0)
974 break;
975 }
976 page += sprintf(page, "%s%s ", torture_type, TORTURE_FLAG);
977 page += sprintf(page,
978 "rtc: %p ver: %lu tfle: %d rta: %d rtaf: %d rtf: %d ",
979 rcu_torture_current,
980 rcu_torture_current_version,
981 list_empty(&rcu_torture_freelist),
982 atomic_read(&n_rcu_torture_alloc),
983 atomic_read(&n_rcu_torture_alloc_fail),
984 atomic_read(&n_rcu_torture_free));
985 page += sprintf(page, "rtmbe: %d rtbke: %ld rtbre: %ld ",
986 atomic_read(&n_rcu_torture_mberror),
987 n_rcu_torture_boost_ktrerror,
988 n_rcu_torture_boost_rterror);
989 page += sprintf(page, "rtbf: %ld rtb: %ld nt: %ld ",
990 n_rcu_torture_boost_failure,
991 n_rcu_torture_boosts,
992 n_rcu_torture_timers);
993 page = torture_onoff_stats(page);
994 page += sprintf(page, "barrier: %ld/%ld:%ld",
995 n_barrier_successes,
996 n_barrier_attempts,
997 n_rcu_torture_barrier_error);
998 page += sprintf(page, "\n%s%s ", torture_type, TORTURE_FLAG);
999 if (atomic_read(&n_rcu_torture_mberror) != 0 ||
1000 n_rcu_torture_barrier_error != 0 ||
1001 n_rcu_torture_boost_ktrerror != 0 ||
1002 n_rcu_torture_boost_rterror != 0 ||
1003 n_rcu_torture_boost_failure != 0 ||
1004 i > 1) {
1005 page += sprintf(page, "!!! ");
1006 atomic_inc(&n_rcu_torture_error);
1007 WARN_ON_ONCE(1);
1008 }
1009 page += sprintf(page, "Reader Pipe: ");
1010 for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++)
1011 page += sprintf(page, " %ld", pipesummary[i]);
1012 page += sprintf(page, "\n%s%s ", torture_type, TORTURE_FLAG);
1013 page += sprintf(page, "Reader Batch: ");
1014 for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++)
1015 page += sprintf(page, " %ld", batchsummary[i]);
1016 page += sprintf(page, "\n%s%s ", torture_type, TORTURE_FLAG);
1017 page += sprintf(page, "Free-Block Circulation: ");
1018 for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++) {
1019 page += sprintf(page, " %d",
1020 atomic_read(&rcu_torture_wcount[i]));
1021 }
1022 page += sprintf(page, "\n");
1023 if (cur_ops->stats)
1024 cur_ops->stats(page);
1025 if (rtcv_snap == rcu_torture_current_version &&
1026 rcu_torture_current != NULL) {
1027 int __maybe_unused flags;
1028 unsigned long __maybe_unused gpnum;
1029 unsigned long __maybe_unused completed;
1030
1031 rcutorture_get_gp_data(cur_ops->ttype,
1032 &flags, &gpnum, &completed);
1033 page += sprintf(page,
1034 "??? Writer stall state %d g%lu c%lu f%#x\n",
1035 rcu_torture_writer_state,
1036 gpnum, completed, flags);
1037 show_rcu_gp_kthreads();
1038 rcutorture_trace_dump();
1039 }
1040 rtcv_snap = rcu_torture_current_version;
1041 }
1042
1043 /*
1044 * Print torture statistics. Caller must ensure that there is only
1045 * one call to this function at a given time!!! This is normally
1046 * accomplished by relying on the module system to only have one copy
1047 * of the module loaded, and then by giving the rcu_torture_stats
1048 * kthread full control (or the init/cleanup functions when rcu_torture_stats
1049 * thread is not running).
1050 */
1051 static void
1052 rcu_torture_stats_print(void)
1053 {
1054 int size = nr_cpu_ids * 200 + 8192;
1055 char *buf;
1056
1057 buf = kmalloc(size, GFP_KERNEL);
1058 if (!buf) {
1059 pr_err("rcu-torture: Out of memory, need: %d", size);
1060 return;
1061 }
1062 rcu_torture_printk(buf);
1063 pr_alert("%s", buf);
1064 kfree(buf);
1065 }
1066
1067 /*
1068 * Periodically prints torture statistics, if periodic statistics printing
1069 * was specified via the stat_interval module parameter.
1070 */
1071 static int
1072 rcu_torture_stats(void *arg)
1073 {
1074 VERBOSE_TOROUT_STRING("rcu_torture_stats task started");
1075 do {
1076 schedule_timeout_interruptible(stat_interval * HZ);
1077 rcu_torture_stats_print();
1078 torture_shutdown_absorb("rcu_torture_stats");
1079 } while (!torture_must_stop());
1080 torture_kthread_stopping("rcu_torture_stats");
1081 return 0;
1082 }
1083
1084 static inline void
1085 rcu_torture_print_module_parms(struct rcu_torture_ops *cur_ops, const char *tag)
1086 {
1087 pr_alert("%s" TORTURE_FLAG
1088 "--- %s: nreaders=%d nfakewriters=%d "
1089 "stat_interval=%d verbose=%d test_no_idle_hz=%d "
1090 "shuffle_interval=%d stutter=%d irqreader=%d "
1091 "fqs_duration=%d fqs_holdoff=%d fqs_stutter=%d "
1092 "test_boost=%d/%d test_boost_interval=%d "
1093 "test_boost_duration=%d shutdown_secs=%d "
1094 "stall_cpu=%d stall_cpu_holdoff=%d "
1095 "n_barrier_cbs=%d "
1096 "onoff_interval=%d onoff_holdoff=%d\n",
1097 torture_type, tag, nrealreaders, nfakewriters,
1098 stat_interval, verbose, test_no_idle_hz, shuffle_interval,
1099 stutter, irqreader, fqs_duration, fqs_holdoff, fqs_stutter,
1100 test_boost, cur_ops->can_boost,
1101 test_boost_interval, test_boost_duration, shutdown_secs,
1102 stall_cpu, stall_cpu_holdoff,
1103 n_barrier_cbs,
1104 onoff_interval, onoff_holdoff);
1105 }
1106
1107 static void rcutorture_booster_cleanup(int cpu)
1108 {
1109 struct task_struct *t;
1110
1111 if (boost_tasks[cpu] == NULL)
1112 return;
1113 mutex_lock(&boost_mutex);
1114 t = boost_tasks[cpu];
1115 boost_tasks[cpu] = NULL;
1116 mutex_unlock(&boost_mutex);
1117
1118 /* This must be outside of the mutex, otherwise deadlock! */
1119 torture_stop_kthread(rcu_torture_boost, t);
1120 }
1121
1122 static int rcutorture_booster_init(int cpu)
1123 {
1124 int retval;
1125
1126 if (boost_tasks[cpu] != NULL)
1127 return 0; /* Already created, nothing more to do. */
1128
1129 /* Don't allow time recalculation while creating a new task. */
1130 mutex_lock(&boost_mutex);
1131 VERBOSE_TOROUT_STRING("Creating rcu_torture_boost task");
1132 boost_tasks[cpu] = kthread_create_on_node(rcu_torture_boost, NULL,
1133 cpu_to_node(cpu),
1134 "rcu_torture_boost");
1135 if (IS_ERR(boost_tasks[cpu])) {
1136 retval = PTR_ERR(boost_tasks[cpu]);
1137 VERBOSE_TOROUT_STRING("rcu_torture_boost task create failed");
1138 n_rcu_torture_boost_ktrerror++;
1139 boost_tasks[cpu] = NULL;
1140 mutex_unlock(&boost_mutex);
1141 return retval;
1142 }
1143 kthread_bind(boost_tasks[cpu], cpu);
1144 wake_up_process(boost_tasks[cpu]);
1145 mutex_unlock(&boost_mutex);
1146 return 0;
1147 }
1148
1149 /*
1150 * CPU-stall kthread. It waits as specified by stall_cpu_holdoff, then
1151 * induces a CPU stall for the time specified by stall_cpu.
1152 */
1153 static int rcu_torture_stall(void *args)
1154 {
1155 unsigned long stop_at;
1156
1157 VERBOSE_TOROUT_STRING("rcu_torture_stall task started");
1158 if (stall_cpu_holdoff > 0) {
1159 VERBOSE_TOROUT_STRING("rcu_torture_stall begin holdoff");
1160 schedule_timeout_interruptible(stall_cpu_holdoff * HZ);
1161 VERBOSE_TOROUT_STRING("rcu_torture_stall end holdoff");
1162 }
1163 if (!kthread_should_stop()) {
1164 stop_at = get_seconds() + stall_cpu;
1165 /* RCU CPU stall is expected behavior in following code. */
1166 pr_alert("rcu_torture_stall start.\n");
1167 rcu_read_lock();
1168 preempt_disable();
1169 while (ULONG_CMP_LT(get_seconds(), stop_at))
1170 continue; /* Induce RCU CPU stall warning. */
1171 preempt_enable();
1172 rcu_read_unlock();
1173 pr_alert("rcu_torture_stall end.\n");
1174 }
1175 torture_shutdown_absorb("rcu_torture_stall");
1176 while (!kthread_should_stop())
1177 schedule_timeout_interruptible(10 * HZ);
1178 return 0;
1179 }
1180
1181 /* Spawn CPU-stall kthread, if stall_cpu specified. */
1182 static int __init rcu_torture_stall_init(void)
1183 {
1184 if (stall_cpu <= 0)
1185 return 0;
1186 return torture_create_kthread(rcu_torture_stall, NULL, stall_task);
1187 }
1188
1189 /* Callback function for RCU barrier testing. */
1190 static void rcu_torture_barrier_cbf(struct rcu_head *rcu)
1191 {
1192 atomic_inc(&barrier_cbs_invoked);
1193 }
1194
1195 /* kthread function to register callbacks used to test RCU barriers. */
1196 static int rcu_torture_barrier_cbs(void *arg)
1197 {
1198 long myid = (long)arg;
1199 bool lastphase = 0;
1200 bool newphase;
1201 struct rcu_head rcu;
1202
1203 init_rcu_head_on_stack(&rcu);
1204 VERBOSE_TOROUT_STRING("rcu_torture_barrier_cbs task started");
1205 set_user_nice(current, MAX_NICE);
1206 do {
1207 wait_event(barrier_cbs_wq[myid],
1208 (newphase =
1209 ACCESS_ONCE(barrier_phase)) != lastphase ||
1210 torture_must_stop());
1211 lastphase = newphase;
1212 smp_mb(); /* ensure barrier_phase load before ->call(). */
1213 if (torture_must_stop())
1214 break;
1215 cur_ops->call(&rcu, rcu_torture_barrier_cbf);
1216 if (atomic_dec_and_test(&barrier_cbs_count))
1217 wake_up(&barrier_wq);
1218 } while (!torture_must_stop());
1219 cur_ops->cb_barrier();
1220 destroy_rcu_head_on_stack(&rcu);
1221 torture_kthread_stopping("rcu_torture_barrier_cbs");
1222 return 0;
1223 }
1224
1225 /* kthread function to drive and coordinate RCU barrier testing. */
1226 static int rcu_torture_barrier(void *arg)
1227 {
1228 int i;
1229
1230 VERBOSE_TOROUT_STRING("rcu_torture_barrier task starting");
1231 do {
1232 atomic_set(&barrier_cbs_invoked, 0);
1233 atomic_set(&barrier_cbs_count, n_barrier_cbs);
1234 smp_mb(); /* Ensure barrier_phase after prior assignments. */
1235 barrier_phase = !barrier_phase;
1236 for (i = 0; i < n_barrier_cbs; i++)
1237 wake_up(&barrier_cbs_wq[i]);
1238 wait_event(barrier_wq,
1239 atomic_read(&barrier_cbs_count) == 0 ||
1240 torture_must_stop());
1241 if (torture_must_stop())
1242 break;
1243 n_barrier_attempts++;
1244 cur_ops->cb_barrier(); /* Implies smp_mb() for wait_event(). */
1245 if (atomic_read(&barrier_cbs_invoked) != n_barrier_cbs) {
1246 n_rcu_torture_barrier_error++;
1247 WARN_ON_ONCE(1);
1248 }
1249 n_barrier_successes++;
1250 schedule_timeout_interruptible(HZ / 10);
1251 } while (!torture_must_stop());
1252 torture_kthread_stopping("rcu_torture_barrier");
1253 return 0;
1254 }
1255
1256 /* Initialize RCU barrier testing. */
1257 static int rcu_torture_barrier_init(void)
1258 {
1259 int i;
1260 int ret;
1261
1262 if (n_barrier_cbs == 0)
1263 return 0;
1264 if (cur_ops->call == NULL || cur_ops->cb_barrier == NULL) {
1265 pr_alert("%s" TORTURE_FLAG
1266 " Call or barrier ops missing for %s,\n",
1267 torture_type, cur_ops->name);
1268 pr_alert("%s" TORTURE_FLAG
1269 " RCU barrier testing omitted from run.\n",
1270 torture_type);
1271 return 0;
1272 }
1273 atomic_set(&barrier_cbs_count, 0);
1274 atomic_set(&barrier_cbs_invoked, 0);
1275 barrier_cbs_tasks =
1276 kzalloc(n_barrier_cbs * sizeof(barrier_cbs_tasks[0]),
1277 GFP_KERNEL);
1278 barrier_cbs_wq =
1279 kzalloc(n_barrier_cbs * sizeof(barrier_cbs_wq[0]),
1280 GFP_KERNEL);
1281 if (barrier_cbs_tasks == NULL || !barrier_cbs_wq)
1282 return -ENOMEM;
1283 for (i = 0; i < n_barrier_cbs; i++) {
1284 init_waitqueue_head(&barrier_cbs_wq[i]);
1285 ret = torture_create_kthread(rcu_torture_barrier_cbs,
1286 (void *)(long)i,
1287 barrier_cbs_tasks[i]);
1288 if (ret)
1289 return ret;
1290 }
1291 return torture_create_kthread(rcu_torture_barrier, NULL, barrier_task);
1292 }
1293
1294 /* Clean up after RCU barrier testing. */
1295 static void rcu_torture_barrier_cleanup(void)
1296 {
1297 int i;
1298
1299 torture_stop_kthread(rcu_torture_barrier, barrier_task);
1300 if (barrier_cbs_tasks != NULL) {
1301 for (i = 0; i < n_barrier_cbs; i++)
1302 torture_stop_kthread(rcu_torture_barrier_cbs,
1303 barrier_cbs_tasks[i]);
1304 kfree(barrier_cbs_tasks);
1305 barrier_cbs_tasks = NULL;
1306 }
1307 if (barrier_cbs_wq != NULL) {
1308 kfree(barrier_cbs_wq);
1309 barrier_cbs_wq = NULL;
1310 }
1311 }
1312
1313 static int rcutorture_cpu_notify(struct notifier_block *self,
1314 unsigned long action, void *hcpu)
1315 {
1316 long cpu = (long)hcpu;
1317
1318 switch (action) {
1319 case CPU_ONLINE:
1320 case CPU_DOWN_FAILED:
1321 (void)rcutorture_booster_init(cpu);
1322 break;
1323 case CPU_DOWN_PREPARE:
1324 rcutorture_booster_cleanup(cpu);
1325 break;
1326 default:
1327 break;
1328 }
1329 return NOTIFY_OK;
1330 }
1331
1332 static struct notifier_block rcutorture_cpu_nb = {
1333 .notifier_call = rcutorture_cpu_notify,
1334 };
1335
1336 static void
1337 rcu_torture_cleanup(void)
1338 {
1339 int i;
1340
1341 rcutorture_record_test_transition();
1342 if (torture_cleanup()) {
1343 if (cur_ops->cb_barrier != NULL)
1344 cur_ops->cb_barrier();
1345 return;
1346 }
1347
1348 rcu_torture_barrier_cleanup();
1349 torture_stop_kthread(rcu_torture_stall, stall_task);
1350 torture_stop_kthread(rcu_torture_writer, writer_task);
1351
1352 if (reader_tasks) {
1353 for (i = 0; i < nrealreaders; i++)
1354 torture_stop_kthread(rcu_torture_reader,
1355 reader_tasks[i]);
1356 kfree(reader_tasks);
1357 }
1358 rcu_torture_current = NULL;
1359
1360 if (fakewriter_tasks) {
1361 for (i = 0; i < nfakewriters; i++) {
1362 torture_stop_kthread(rcu_torture_fakewriter,
1363 fakewriter_tasks[i]);
1364 }
1365 kfree(fakewriter_tasks);
1366 fakewriter_tasks = NULL;
1367 }
1368
1369 torture_stop_kthread(rcu_torture_stats, stats_task);
1370 torture_stop_kthread(rcu_torture_fqs, fqs_task);
1371 if ((test_boost == 1 && cur_ops->can_boost) ||
1372 test_boost == 2) {
1373 unregister_cpu_notifier(&rcutorture_cpu_nb);
1374 for_each_possible_cpu(i)
1375 rcutorture_booster_cleanup(i);
1376 }
1377
1378 /* Wait for all RCU callbacks to fire. */
1379
1380 if (cur_ops->cb_barrier != NULL)
1381 cur_ops->cb_barrier();
1382
1383 rcu_torture_stats_print(); /* -After- the stats thread is stopped! */
1384
1385 if (atomic_read(&n_rcu_torture_error) || n_rcu_torture_barrier_error)
1386 rcu_torture_print_module_parms(cur_ops, "End of test: FAILURE");
1387 else if (torture_onoff_failures())
1388 rcu_torture_print_module_parms(cur_ops,
1389 "End of test: RCU_HOTPLUG");
1390 else
1391 rcu_torture_print_module_parms(cur_ops, "End of test: SUCCESS");
1392 }
1393
1394 #ifdef CONFIG_DEBUG_OBJECTS_RCU_HEAD
1395 static void rcu_torture_leak_cb(struct rcu_head *rhp)
1396 {
1397 }
1398
1399 static void rcu_torture_err_cb(struct rcu_head *rhp)
1400 {
1401 /*
1402 * This -might- happen due to race conditions, but is unlikely.
1403 * The scenario that leads to this happening is that the
1404 * first of the pair of duplicate callbacks is queued,
1405 * someone else starts a grace period that includes that
1406 * callback, then the second of the pair must wait for the
1407 * next grace period. Unlikely, but can happen. If it
1408 * does happen, the debug-objects subsystem won't have splatted.
1409 */
1410 pr_alert("rcutorture: duplicated callback was invoked.\n");
1411 }
1412 #endif /* #ifdef CONFIG_DEBUG_OBJECTS_RCU_HEAD */
1413
1414 /*
1415 * Verify that double-free causes debug-objects to complain, but only
1416 * if CONFIG_DEBUG_OBJECTS_RCU_HEAD=y. Otherwise, say that the test
1417 * cannot be carried out.
1418 */
1419 static void rcu_test_debug_objects(void)
1420 {
1421 #ifdef CONFIG_DEBUG_OBJECTS_RCU_HEAD
1422 struct rcu_head rh1;
1423 struct rcu_head rh2;
1424
1425 init_rcu_head_on_stack(&rh1);
1426 init_rcu_head_on_stack(&rh2);
1427 pr_alert("rcutorture: WARN: Duplicate call_rcu() test starting.\n");
1428
1429 /* Try to queue the rh2 pair of callbacks for the same grace period. */
1430 preempt_disable(); /* Prevent preemption from interrupting test. */
1431 rcu_read_lock(); /* Make it impossible to finish a grace period. */
1432 call_rcu(&rh1, rcu_torture_leak_cb); /* Start grace period. */
1433 local_irq_disable(); /* Make it harder to start a new grace period. */
1434 call_rcu(&rh2, rcu_torture_leak_cb);
1435 call_rcu(&rh2, rcu_torture_err_cb); /* Duplicate callback. */
1436 local_irq_enable();
1437 rcu_read_unlock();
1438 preempt_enable();
1439
1440 /* Wait for them all to get done so we can safely return. */
1441 rcu_barrier();
1442 pr_alert("rcutorture: WARN: Duplicate call_rcu() test complete.\n");
1443 destroy_rcu_head_on_stack(&rh1);
1444 destroy_rcu_head_on_stack(&rh2);
1445 #else /* #ifdef CONFIG_DEBUG_OBJECTS_RCU_HEAD */
1446 pr_alert("rcutorture: !CONFIG_DEBUG_OBJECTS_RCU_HEAD, not testing duplicate call_rcu()\n");
1447 #endif /* #else #ifdef CONFIG_DEBUG_OBJECTS_RCU_HEAD */
1448 }
1449
1450 static int __init
1451 rcu_torture_init(void)
1452 {
1453 int i;
1454 int cpu;
1455 int firsterr = 0;
1456 static struct rcu_torture_ops *torture_ops[] = {
1457 &rcu_ops, &rcu_bh_ops, &rcu_busted_ops, &srcu_ops, &sched_ops,
1458 };
1459
1460 torture_init_begin(torture_type, verbose, &rcutorture_runnable);
1461
1462 /* Process args and tell the world that the torturer is on the job. */
1463 for (i = 0; i < ARRAY_SIZE(torture_ops); i++) {
1464 cur_ops = torture_ops[i];
1465 if (strcmp(torture_type, cur_ops->name) == 0)
1466 break;
1467 }
1468 if (i == ARRAY_SIZE(torture_ops)) {
1469 pr_alert("rcu-torture: invalid torture type: \"%s\"\n",
1470 torture_type);
1471 pr_alert("rcu-torture types:");
1472 for (i = 0; i < ARRAY_SIZE(torture_ops); i++)
1473 pr_alert(" %s", torture_ops[i]->name);
1474 pr_alert("\n");
1475 torture_init_end();
1476 return -EINVAL;
1477 }
1478 if (cur_ops->fqs == NULL && fqs_duration != 0) {
1479 pr_alert("rcu-torture: ->fqs NULL and non-zero fqs_duration, fqs disabled.\n");
1480 fqs_duration = 0;
1481 }
1482 if (cur_ops->init)
1483 cur_ops->init(); /* no "goto unwind" prior to this point!!! */
1484
1485 if (nreaders >= 0) {
1486 nrealreaders = nreaders;
1487 } else {
1488 nrealreaders = num_online_cpus() - 1;
1489 if (nrealreaders <= 0)
1490 nrealreaders = 1;
1491 }
1492 rcu_torture_print_module_parms(cur_ops, "Start of test");
1493
1494 /* Set up the freelist. */
1495
1496 INIT_LIST_HEAD(&rcu_torture_freelist);
1497 for (i = 0; i < ARRAY_SIZE(rcu_tortures); i++) {
1498 rcu_tortures[i].rtort_mbtest = 0;
1499 list_add_tail(&rcu_tortures[i].rtort_free,
1500 &rcu_torture_freelist);
1501 }
1502
1503 /* Initialize the statistics so that each run gets its own numbers. */
1504
1505 rcu_torture_current = NULL;
1506 rcu_torture_current_version = 0;
1507 atomic_set(&n_rcu_torture_alloc, 0);
1508 atomic_set(&n_rcu_torture_alloc_fail, 0);
1509 atomic_set(&n_rcu_torture_free, 0);
1510 atomic_set(&n_rcu_torture_mberror, 0);
1511 atomic_set(&n_rcu_torture_error, 0);
1512 n_rcu_torture_barrier_error = 0;
1513 n_rcu_torture_boost_ktrerror = 0;
1514 n_rcu_torture_boost_rterror = 0;
1515 n_rcu_torture_boost_failure = 0;
1516 n_rcu_torture_boosts = 0;
1517 for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++)
1518 atomic_set(&rcu_torture_wcount[i], 0);
1519 for_each_possible_cpu(cpu) {
1520 for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++) {
1521 per_cpu(rcu_torture_count, cpu)[i] = 0;
1522 per_cpu(rcu_torture_batch, cpu)[i] = 0;
1523 }
1524 }
1525
1526 /* Start up the kthreads. */
1527
1528 firsterr = torture_create_kthread(rcu_torture_writer, NULL,
1529 writer_task);
1530 if (firsterr)
1531 goto unwind;
1532 fakewriter_tasks = kzalloc(nfakewriters * sizeof(fakewriter_tasks[0]),
1533 GFP_KERNEL);
1534 if (fakewriter_tasks == NULL) {
1535 VERBOSE_TOROUT_ERRSTRING("out of memory");
1536 firsterr = -ENOMEM;
1537 goto unwind;
1538 }
1539 for (i = 0; i < nfakewriters; i++) {
1540 firsterr = torture_create_kthread(rcu_torture_fakewriter,
1541 NULL, fakewriter_tasks[i]);
1542 if (firsterr)
1543 goto unwind;
1544 }
1545 reader_tasks = kzalloc(nrealreaders * sizeof(reader_tasks[0]),
1546 GFP_KERNEL);
1547 if (reader_tasks == NULL) {
1548 VERBOSE_TOROUT_ERRSTRING("out of memory");
1549 firsterr = -ENOMEM;
1550 goto unwind;
1551 }
1552 for (i = 0; i < nrealreaders; i++) {
1553 firsterr = torture_create_kthread(rcu_torture_reader, NULL,
1554 reader_tasks[i]);
1555 if (firsterr)
1556 goto unwind;
1557 }
1558 if (stat_interval > 0) {
1559 firsterr = torture_create_kthread(rcu_torture_stats, NULL,
1560 stats_task);
1561 if (firsterr)
1562 goto unwind;
1563 }
1564 if (test_no_idle_hz) {
1565 firsterr = torture_shuffle_init(shuffle_interval * HZ);
1566 if (firsterr)
1567 goto unwind;
1568 }
1569 if (stutter < 0)
1570 stutter = 0;
1571 if (stutter) {
1572 firsterr = torture_stutter_init(stutter * HZ);
1573 if (firsterr)
1574 goto unwind;
1575 }
1576 if (fqs_duration < 0)
1577 fqs_duration = 0;
1578 if (fqs_duration) {
1579 /* Create the fqs thread */
1580 torture_create_kthread(rcu_torture_fqs, NULL, fqs_task);
1581 if (firsterr)
1582 goto unwind;
1583 }
1584 if (test_boost_interval < 1)
1585 test_boost_interval = 1;
1586 if (test_boost_duration < 2)
1587 test_boost_duration = 2;
1588 if ((test_boost == 1 && cur_ops->can_boost) ||
1589 test_boost == 2) {
1590
1591 boost_starttime = jiffies + test_boost_interval * HZ;
1592 register_cpu_notifier(&rcutorture_cpu_nb);
1593 for_each_possible_cpu(i) {
1594 if (cpu_is_offline(i))
1595 continue; /* Heuristic: CPU can go offline. */
1596 firsterr = rcutorture_booster_init(i);
1597 if (firsterr)
1598 goto unwind;
1599 }
1600 }
1601 firsterr = torture_shutdown_init(shutdown_secs, rcu_torture_cleanup);
1602 if (firsterr)
1603 goto unwind;
1604 firsterr = torture_onoff_init(onoff_holdoff * HZ, onoff_interval * HZ);
1605 if (firsterr)
1606 goto unwind;
1607 firsterr = rcu_torture_stall_init();
1608 if (firsterr)
1609 goto unwind;
1610 firsterr = rcu_torture_barrier_init();
1611 if (firsterr)
1612 goto unwind;
1613 if (object_debug)
1614 rcu_test_debug_objects();
1615 rcutorture_record_test_transition();
1616 torture_init_end();
1617 return 0;
1618
1619 unwind:
1620 torture_init_end();
1621 rcu_torture_cleanup();
1622 return firsterr;
1623 }
1624
1625 module_init(rcu_torture_init);
1626 module_exit(rcu_torture_cleanup);
This page took 0.062765 seconds and 4 git commands to generate.