]> asedeno.scripts.mit.edu Git - linux.git/blob - kernel/rcu/rcutorture.c
rcutorture: Convert rcutorture_get_gp_data() to ->gp_seq
[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@joshtriplett.org>
22  *
23  * See also:  Documentation/RCU/torture.txt
24  */
25
26 #define pr_fmt(fmt) fmt
27
28 #include <linux/types.h>
29 #include <linux/kernel.h>
30 #include <linux/init.h>
31 #include <linux/module.h>
32 #include <linux/kthread.h>
33 #include <linux/err.h>
34 #include <linux/spinlock.h>
35 #include <linux/smp.h>
36 #include <linux/rcupdate.h>
37 #include <linux/interrupt.h>
38 #include <linux/sched/signal.h>
39 #include <uapi/linux/sched/types.h>
40 #include <linux/atomic.h>
41 #include <linux/bitops.h>
42 #include <linux/completion.h>
43 #include <linux/moduleparam.h>
44 #include <linux/percpu.h>
45 #include <linux/notifier.h>
46 #include <linux/reboot.h>
47 #include <linux/freezer.h>
48 #include <linux/cpu.h>
49 #include <linux/delay.h>
50 #include <linux/stat.h>
51 #include <linux/srcu.h>
52 #include <linux/slab.h>
53 #include <linux/trace_clock.h>
54 #include <asm/byteorder.h>
55 #include <linux/torture.h>
56 #include <linux/vmalloc.h>
57 #include <linux/sched/debug.h>
58
59 #include "rcu.h"
60
61 MODULE_LICENSE("GPL");
62 MODULE_AUTHOR("Paul E. McKenney <paulmck@us.ibm.com> and Josh Triplett <josh@joshtriplett.org>");
63
64
65 torture_param(int, cbflood_inter_holdoff, HZ,
66               "Holdoff between floods (jiffies)");
67 torture_param(int, cbflood_intra_holdoff, 1,
68               "Holdoff between bursts (jiffies)");
69 torture_param(int, cbflood_n_burst, 3, "# bursts in flood, zero to disable");
70 torture_param(int, cbflood_n_per_burst, 20000,
71               "# callbacks per burst in flood");
72 torture_param(int, fqs_duration, 0,
73               "Duration of fqs bursts (us), 0 to disable");
74 torture_param(int, fqs_holdoff, 0, "Holdoff time within fqs bursts (us)");
75 torture_param(int, fqs_stutter, 3, "Wait time between fqs bursts (s)");
76 torture_param(bool, gp_cond, false, "Use conditional/async GP wait primitives");
77 torture_param(bool, gp_exp, false, "Use expedited GP wait primitives");
78 torture_param(bool, gp_normal, false,
79              "Use normal (non-expedited) GP wait primitives");
80 torture_param(bool, gp_sync, false, "Use synchronous GP wait primitives");
81 torture_param(int, irqreader, 1, "Allow RCU readers from irq handlers");
82 torture_param(int, n_barrier_cbs, 0,
83              "# of callbacks/kthreads for barrier testing");
84 torture_param(int, nfakewriters, 4, "Number of RCU fake writer threads");
85 torture_param(int, nreaders, -1, "Number of RCU reader threads");
86 torture_param(int, object_debug, 0,
87              "Enable debug-object double call_rcu() testing");
88 torture_param(int, onoff_holdoff, 0, "Time after boot before CPU hotplugs (s)");
89 torture_param(int, onoff_interval, 0,
90              "Time between CPU hotplugs (s), 0=disable");
91 torture_param(int, shuffle_interval, 3, "Number of seconds between shuffles");
92 torture_param(int, shutdown_secs, 0, "Shutdown time (s), <= zero to disable.");
93 torture_param(int, stall_cpu, 0, "Stall duration (s), zero to disable.");
94 torture_param(int, stall_cpu_holdoff, 10,
95              "Time to wait before starting stall (s).");
96 torture_param(int, stall_cpu_irqsoff, 0, "Disable interrupts while stalling.");
97 torture_param(int, stat_interval, 60,
98              "Number of seconds between stats printk()s");
99 torture_param(int, stutter, 5, "Number of seconds to run/halt test");
100 torture_param(int, test_boost, 1, "Test RCU prio boost: 0=no, 1=maybe, 2=yes.");
101 torture_param(int, test_boost_duration, 4,
102              "Duration of each boost test, seconds.");
103 torture_param(int, test_boost_interval, 7,
104              "Interval between boost tests, seconds.");
105 torture_param(bool, test_no_idle_hz, true,
106              "Test support for tickless idle CPUs");
107 torture_param(int, verbose, 1,
108              "Enable verbose debugging printk()s");
109
110 static char *torture_type = "rcu";
111 module_param(torture_type, charp, 0444);
112 MODULE_PARM_DESC(torture_type, "Type of RCU to torture (rcu, rcu_bh, ...)");
113
114 static int nrealreaders;
115 static int ncbflooders;
116 static struct task_struct *writer_task;
117 static struct task_struct **fakewriter_tasks;
118 static struct task_struct **reader_tasks;
119 static struct task_struct *stats_task;
120 static struct task_struct **cbflood_task;
121 static struct task_struct *fqs_task;
122 static struct task_struct *boost_tasks[NR_CPUS];
123 static struct task_struct *stall_task;
124 static struct task_struct **barrier_cbs_tasks;
125 static struct task_struct *barrier_task;
126
127 #define RCU_TORTURE_PIPE_LEN 10
128
129 struct rcu_torture {
130         struct rcu_head rtort_rcu;
131         int rtort_pipe_count;
132         struct list_head rtort_free;
133         int rtort_mbtest;
134 };
135
136 static LIST_HEAD(rcu_torture_freelist);
137 static struct rcu_torture __rcu *rcu_torture_current;
138 static unsigned long rcu_torture_current_version;
139 static struct rcu_torture rcu_tortures[10 * RCU_TORTURE_PIPE_LEN];
140 static DEFINE_SPINLOCK(rcu_torture_lock);
141 static DEFINE_PER_CPU(long [RCU_TORTURE_PIPE_LEN + 1], rcu_torture_count);
142 static DEFINE_PER_CPU(long [RCU_TORTURE_PIPE_LEN + 1], rcu_torture_batch);
143 static atomic_t rcu_torture_wcount[RCU_TORTURE_PIPE_LEN + 1];
144 static atomic_t n_rcu_torture_alloc;
145 static atomic_t n_rcu_torture_alloc_fail;
146 static atomic_t n_rcu_torture_free;
147 static atomic_t n_rcu_torture_mberror;
148 static atomic_t n_rcu_torture_error;
149 static long n_rcu_torture_barrier_error;
150 static long n_rcu_torture_boost_ktrerror;
151 static long n_rcu_torture_boost_rterror;
152 static long n_rcu_torture_boost_failure;
153 static long n_rcu_torture_boosts;
154 static long n_rcu_torture_timers;
155 static long n_barrier_attempts;
156 static long n_barrier_successes;
157 static atomic_long_t n_cbfloods;
158 static struct list_head rcu_torture_removed;
159
160 static int rcu_torture_writer_state;
161 #define RTWS_FIXED_DELAY        0
162 #define RTWS_DELAY              1
163 #define RTWS_REPLACE            2
164 #define RTWS_DEF_FREE           3
165 #define RTWS_EXP_SYNC           4
166 #define RTWS_COND_GET           5
167 #define RTWS_COND_SYNC          6
168 #define RTWS_SYNC               7
169 #define RTWS_STUTTER            8
170 #define RTWS_STOPPING           9
171 static const char * const rcu_torture_writer_state_names[] = {
172         "RTWS_FIXED_DELAY",
173         "RTWS_DELAY",
174         "RTWS_REPLACE",
175         "RTWS_DEF_FREE",
176         "RTWS_EXP_SYNC",
177         "RTWS_COND_GET",
178         "RTWS_COND_SYNC",
179         "RTWS_SYNC",
180         "RTWS_STUTTER",
181         "RTWS_STOPPING",
182 };
183
184 static const char *rcu_torture_writer_state_getname(void)
185 {
186         unsigned int i = READ_ONCE(rcu_torture_writer_state);
187
188         if (i >= ARRAY_SIZE(rcu_torture_writer_state_names))
189                 return "???";
190         return rcu_torture_writer_state_names[i];
191 }
192
193 #if defined(CONFIG_RCU_BOOST) && !defined(CONFIG_HOTPLUG_CPU)
194 #define rcu_can_boost() 1
195 #else /* #if defined(CONFIG_RCU_BOOST) && !defined(CONFIG_HOTPLUG_CPU) */
196 #define rcu_can_boost() 0
197 #endif /* #else #if defined(CONFIG_RCU_BOOST) && !defined(CONFIG_HOTPLUG_CPU) */
198
199 #ifdef CONFIG_RCU_TRACE
200 static u64 notrace rcu_trace_clock_local(void)
201 {
202         u64 ts = trace_clock_local();
203
204         (void)do_div(ts, NSEC_PER_USEC);
205         return ts;
206 }
207 #else /* #ifdef CONFIG_RCU_TRACE */
208 static u64 notrace rcu_trace_clock_local(void)
209 {
210         return 0ULL;
211 }
212 #endif /* #else #ifdef CONFIG_RCU_TRACE */
213
214 static unsigned long boost_starttime;   /* jiffies of next boost test start. */
215 static DEFINE_MUTEX(boost_mutex);       /* protect setting boost_starttime */
216                                         /*  and boost task create/destroy. */
217 static atomic_t barrier_cbs_count;      /* Barrier callbacks registered. */
218 static bool barrier_phase;              /* Test phase. */
219 static atomic_t barrier_cbs_invoked;    /* Barrier callbacks invoked. */
220 static wait_queue_head_t *barrier_cbs_wq; /* Coordinate barrier testing. */
221 static DECLARE_WAIT_QUEUE_HEAD(barrier_wq);
222
223 /*
224  * Allocate an element from the rcu_tortures pool.
225  */
226 static struct rcu_torture *
227 rcu_torture_alloc(void)
228 {
229         struct list_head *p;
230
231         spin_lock_bh(&rcu_torture_lock);
232         if (list_empty(&rcu_torture_freelist)) {
233                 atomic_inc(&n_rcu_torture_alloc_fail);
234                 spin_unlock_bh(&rcu_torture_lock);
235                 return NULL;
236         }
237         atomic_inc(&n_rcu_torture_alloc);
238         p = rcu_torture_freelist.next;
239         list_del_init(p);
240         spin_unlock_bh(&rcu_torture_lock);
241         return container_of(p, struct rcu_torture, rtort_free);
242 }
243
244 /*
245  * Free an element to the rcu_tortures pool.
246  */
247 static void
248 rcu_torture_free(struct rcu_torture *p)
249 {
250         atomic_inc(&n_rcu_torture_free);
251         spin_lock_bh(&rcu_torture_lock);
252         list_add_tail(&p->rtort_free, &rcu_torture_freelist);
253         spin_unlock_bh(&rcu_torture_lock);
254 }
255
256 /*
257  * Operations vector for selecting different types of tests.
258  */
259
260 struct rcu_torture_ops {
261         int ttype;
262         void (*init)(void);
263         void (*cleanup)(void);
264         int (*readlock)(void);
265         void (*read_delay)(struct torture_random_state *rrsp);
266         void (*readunlock)(int idx);
267         unsigned long (*get_gp_seq)(void);
268         void (*deferred_free)(struct rcu_torture *p);
269         void (*sync)(void);
270         void (*exp_sync)(void);
271         unsigned long (*get_state)(void);
272         void (*cond_sync)(unsigned long oldstate);
273         call_rcu_func_t call;
274         void (*cb_barrier)(void);
275         void (*fqs)(void);
276         void (*stats)(void);
277         int irq_capable;
278         int can_boost;
279         const char *name;
280 };
281
282 static struct rcu_torture_ops *cur_ops;
283
284 /*
285  * Definitions for rcu torture testing.
286  */
287
288 static int rcu_torture_read_lock(void) __acquires(RCU)
289 {
290         rcu_read_lock();
291         return 0;
292 }
293
294 static void rcu_read_delay(struct torture_random_state *rrsp)
295 {
296         unsigned long started;
297         unsigned long completed;
298         const unsigned long shortdelay_us = 200;
299         const unsigned long longdelay_ms = 50;
300         unsigned long long ts;
301
302         /* We want a short delay sometimes to make a reader delay the grace
303          * period, and we want a long delay occasionally to trigger
304          * force_quiescent_state. */
305
306         if (!(torture_random(rrsp) % (nrealreaders * 2000 * longdelay_ms))) {
307                 started = cur_ops->get_gp_seq();
308                 ts = rcu_trace_clock_local();
309                 mdelay(longdelay_ms);
310                 completed = cur_ops->get_gp_seq();
311                 do_trace_rcu_torture_read(cur_ops->name, NULL, ts,
312                                           started, completed);
313         }
314         if (!(torture_random(rrsp) % (nrealreaders * 2 * shortdelay_us)))
315                 udelay(shortdelay_us);
316         if (!preempt_count() &&
317             !(torture_random(rrsp) % (nrealreaders * 500)))
318                 torture_preempt_schedule();  /* QS only if preemptible. */
319 }
320
321 static void rcu_torture_read_unlock(int idx) __releases(RCU)
322 {
323         rcu_read_unlock();
324 }
325
326 /*
327  * Update callback in the pipe.  This should be invoked after a grace period.
328  */
329 static bool
330 rcu_torture_pipe_update_one(struct rcu_torture *rp)
331 {
332         int i;
333
334         i = rp->rtort_pipe_count;
335         if (i > RCU_TORTURE_PIPE_LEN)
336                 i = RCU_TORTURE_PIPE_LEN;
337         atomic_inc(&rcu_torture_wcount[i]);
338         if (++rp->rtort_pipe_count >= RCU_TORTURE_PIPE_LEN) {
339                 rp->rtort_mbtest = 0;
340                 return true;
341         }
342         return false;
343 }
344
345 /*
346  * Update all callbacks in the pipe.  Suitable for synchronous grace-period
347  * primitives.
348  */
349 static void
350 rcu_torture_pipe_update(struct rcu_torture *old_rp)
351 {
352         struct rcu_torture *rp;
353         struct rcu_torture *rp1;
354
355         if (old_rp)
356                 list_add(&old_rp->rtort_free, &rcu_torture_removed);
357         list_for_each_entry_safe(rp, rp1, &rcu_torture_removed, rtort_free) {
358                 if (rcu_torture_pipe_update_one(rp)) {
359                         list_del(&rp->rtort_free);
360                         rcu_torture_free(rp);
361                 }
362         }
363 }
364
365 static void
366 rcu_torture_cb(struct rcu_head *p)
367 {
368         struct rcu_torture *rp = container_of(p, struct rcu_torture, rtort_rcu);
369
370         if (torture_must_stop_irq()) {
371                 /* Test is ending, just drop callbacks on the floor. */
372                 /* The next initialization will pick up the pieces. */
373                 return;
374         }
375         if (rcu_torture_pipe_update_one(rp))
376                 rcu_torture_free(rp);
377         else
378                 cur_ops->deferred_free(rp);
379 }
380
381 static unsigned long rcu_no_completed(void)
382 {
383         return 0;
384 }
385
386 static void rcu_torture_deferred_free(struct rcu_torture *p)
387 {
388         call_rcu(&p->rtort_rcu, rcu_torture_cb);
389 }
390
391 static void rcu_sync_torture_init(void)
392 {
393         INIT_LIST_HEAD(&rcu_torture_removed);
394 }
395
396 static struct rcu_torture_ops rcu_ops = {
397         .ttype          = RCU_FLAVOR,
398         .init           = rcu_sync_torture_init,
399         .readlock       = rcu_torture_read_lock,
400         .read_delay     = rcu_read_delay,
401         .readunlock     = rcu_torture_read_unlock,
402         .get_gp_seq     = rcu_get_gp_seq,
403         .deferred_free  = rcu_torture_deferred_free,
404         .sync           = synchronize_rcu,
405         .exp_sync       = synchronize_rcu_expedited,
406         .get_state      = get_state_synchronize_rcu,
407         .cond_sync      = cond_synchronize_rcu,
408         .call           = call_rcu,
409         .cb_barrier     = rcu_barrier,
410         .fqs            = rcu_force_quiescent_state,
411         .stats          = NULL,
412         .irq_capable    = 1,
413         .can_boost      = rcu_can_boost(),
414         .name           = "rcu"
415 };
416
417 /*
418  * Definitions for rcu_bh torture testing.
419  */
420
421 static int rcu_bh_torture_read_lock(void) __acquires(RCU_BH)
422 {
423         rcu_read_lock_bh();
424         return 0;
425 }
426
427 static void rcu_bh_torture_read_unlock(int idx) __releases(RCU_BH)
428 {
429         rcu_read_unlock_bh();
430 }
431
432 static void rcu_bh_torture_deferred_free(struct rcu_torture *p)
433 {
434         call_rcu_bh(&p->rtort_rcu, rcu_torture_cb);
435 }
436
437 static struct rcu_torture_ops rcu_bh_ops = {
438         .ttype          = RCU_BH_FLAVOR,
439         .init           = rcu_sync_torture_init,
440         .readlock       = rcu_bh_torture_read_lock,
441         .read_delay     = rcu_read_delay,  /* just reuse rcu's version. */
442         .readunlock     = rcu_bh_torture_read_unlock,
443         .get_gp_seq     = rcu_bh_get_gp_seq,
444         .deferred_free  = rcu_bh_torture_deferred_free,
445         .sync           = synchronize_rcu_bh,
446         .exp_sync       = synchronize_rcu_bh_expedited,
447         .call           = call_rcu_bh,
448         .cb_barrier     = rcu_barrier_bh,
449         .fqs            = rcu_bh_force_quiescent_state,
450         .stats          = NULL,
451         .irq_capable    = 1,
452         .name           = "rcu_bh"
453 };
454
455 /*
456  * Don't even think about trying any of these in real life!!!
457  * The names includes "busted", and they really means it!
458  * The only purpose of these functions is to provide a buggy RCU
459  * implementation to make sure that rcutorture correctly emits
460  * buggy-RCU error messages.
461  */
462 static void rcu_busted_torture_deferred_free(struct rcu_torture *p)
463 {
464         /* This is a deliberate bug for testing purposes only! */
465         rcu_torture_cb(&p->rtort_rcu);
466 }
467
468 static void synchronize_rcu_busted(void)
469 {
470         /* This is a deliberate bug for testing purposes only! */
471 }
472
473 static void
474 call_rcu_busted(struct rcu_head *head, rcu_callback_t func)
475 {
476         /* This is a deliberate bug for testing purposes only! */
477         func(head);
478 }
479
480 static struct rcu_torture_ops rcu_busted_ops = {
481         .ttype          = INVALID_RCU_FLAVOR,
482         .init           = rcu_sync_torture_init,
483         .readlock       = rcu_torture_read_lock,
484         .read_delay     = rcu_read_delay,  /* just reuse rcu's version. */
485         .readunlock     = rcu_torture_read_unlock,
486         .get_gp_seq     = rcu_no_completed,
487         .deferred_free  = rcu_busted_torture_deferred_free,
488         .sync           = synchronize_rcu_busted,
489         .exp_sync       = synchronize_rcu_busted,
490         .call           = call_rcu_busted,
491         .cb_barrier     = NULL,
492         .fqs            = NULL,
493         .stats          = NULL,
494         .irq_capable    = 1,
495         .name           = "busted"
496 };
497
498 /*
499  * Definitions for srcu torture testing.
500  */
501
502 DEFINE_STATIC_SRCU(srcu_ctl);
503 static struct srcu_struct srcu_ctld;
504 static struct srcu_struct *srcu_ctlp = &srcu_ctl;
505
506 static int srcu_torture_read_lock(void) __acquires(srcu_ctlp)
507 {
508         return srcu_read_lock(srcu_ctlp);
509 }
510
511 static void srcu_read_delay(struct torture_random_state *rrsp)
512 {
513         long delay;
514         const long uspertick = 1000000 / HZ;
515         const long longdelay = 10;
516
517         /* We want there to be long-running readers, but not all the time. */
518
519         delay = torture_random(rrsp) %
520                 (nrealreaders * 2 * longdelay * uspertick);
521         if (!delay && in_task())
522                 schedule_timeout_interruptible(longdelay);
523         else
524                 rcu_read_delay(rrsp);
525 }
526
527 static void srcu_torture_read_unlock(int idx) __releases(srcu_ctlp)
528 {
529         srcu_read_unlock(srcu_ctlp, idx);
530 }
531
532 static unsigned long srcu_torture_completed(void)
533 {
534         return srcu_batches_completed(srcu_ctlp);
535 }
536
537 static void srcu_torture_deferred_free(struct rcu_torture *rp)
538 {
539         call_srcu(srcu_ctlp, &rp->rtort_rcu, rcu_torture_cb);
540 }
541
542 static void srcu_torture_synchronize(void)
543 {
544         synchronize_srcu(srcu_ctlp);
545 }
546
547 static void srcu_torture_call(struct rcu_head *head,
548                               rcu_callback_t func)
549 {
550         call_srcu(srcu_ctlp, head, func);
551 }
552
553 static void srcu_torture_barrier(void)
554 {
555         srcu_barrier(srcu_ctlp);
556 }
557
558 static void srcu_torture_stats(void)
559 {
560         srcu_torture_stats_print(srcu_ctlp, torture_type, TORTURE_FLAG);
561 }
562
563 static void srcu_torture_synchronize_expedited(void)
564 {
565         synchronize_srcu_expedited(srcu_ctlp);
566 }
567
568 static struct rcu_torture_ops srcu_ops = {
569         .ttype          = SRCU_FLAVOR,
570         .init           = rcu_sync_torture_init,
571         .readlock       = srcu_torture_read_lock,
572         .read_delay     = srcu_read_delay,
573         .readunlock     = srcu_torture_read_unlock,
574         .get_gp_seq     = srcu_torture_completed,
575         .deferred_free  = srcu_torture_deferred_free,
576         .sync           = srcu_torture_synchronize,
577         .exp_sync       = srcu_torture_synchronize_expedited,
578         .call           = srcu_torture_call,
579         .cb_barrier     = srcu_torture_barrier,
580         .stats          = srcu_torture_stats,
581         .irq_capable    = 1,
582         .name           = "srcu"
583 };
584
585 static void srcu_torture_init(void)
586 {
587         rcu_sync_torture_init();
588         WARN_ON(init_srcu_struct(&srcu_ctld));
589         srcu_ctlp = &srcu_ctld;
590 }
591
592 static void srcu_torture_cleanup(void)
593 {
594         static DEFINE_TORTURE_RANDOM(rand);
595
596         if (torture_random(&rand) & 0x800)
597                 cleanup_srcu_struct(&srcu_ctld);
598         else
599                 cleanup_srcu_struct_quiesced(&srcu_ctld);
600         srcu_ctlp = &srcu_ctl; /* In case of a later rcutorture run. */
601 }
602
603 /* As above, but dynamically allocated. */
604 static struct rcu_torture_ops srcud_ops = {
605         .ttype          = SRCU_FLAVOR,
606         .init           = srcu_torture_init,
607         .cleanup        = srcu_torture_cleanup,
608         .readlock       = srcu_torture_read_lock,
609         .read_delay     = srcu_read_delay,
610         .readunlock     = srcu_torture_read_unlock,
611         .get_gp_seq     = srcu_torture_completed,
612         .deferred_free  = srcu_torture_deferred_free,
613         .sync           = srcu_torture_synchronize,
614         .exp_sync       = srcu_torture_synchronize_expedited,
615         .call           = srcu_torture_call,
616         .cb_barrier     = srcu_torture_barrier,
617         .stats          = srcu_torture_stats,
618         .irq_capable    = 1,
619         .name           = "srcud"
620 };
621
622 /*
623  * Definitions for sched torture testing.
624  */
625
626 static int sched_torture_read_lock(void)
627 {
628         preempt_disable();
629         return 0;
630 }
631
632 static void sched_torture_read_unlock(int idx)
633 {
634         preempt_enable();
635 }
636
637 static void rcu_sched_torture_deferred_free(struct rcu_torture *p)
638 {
639         call_rcu_sched(&p->rtort_rcu, rcu_torture_cb);
640 }
641
642 static struct rcu_torture_ops sched_ops = {
643         .ttype          = RCU_SCHED_FLAVOR,
644         .init           = rcu_sync_torture_init,
645         .readlock       = sched_torture_read_lock,
646         .read_delay     = rcu_read_delay,  /* just reuse rcu's version. */
647         .readunlock     = sched_torture_read_unlock,
648         .get_gp_seq     = rcu_sched_get_gp_seq,
649         .deferred_free  = rcu_sched_torture_deferred_free,
650         .sync           = synchronize_sched,
651         .exp_sync       = synchronize_sched_expedited,
652         .get_state      = get_state_synchronize_sched,
653         .cond_sync      = cond_synchronize_sched,
654         .call           = call_rcu_sched,
655         .cb_barrier     = rcu_barrier_sched,
656         .fqs            = rcu_sched_force_quiescent_state,
657         .stats          = NULL,
658         .irq_capable    = 1,
659         .name           = "sched"
660 };
661
662 /*
663  * Definitions for RCU-tasks torture testing.
664  */
665
666 static int tasks_torture_read_lock(void)
667 {
668         return 0;
669 }
670
671 static void tasks_torture_read_unlock(int idx)
672 {
673 }
674
675 static void rcu_tasks_torture_deferred_free(struct rcu_torture *p)
676 {
677         call_rcu_tasks(&p->rtort_rcu, rcu_torture_cb);
678 }
679
680 static struct rcu_torture_ops tasks_ops = {
681         .ttype          = RCU_TASKS_FLAVOR,
682         .init           = rcu_sync_torture_init,
683         .readlock       = tasks_torture_read_lock,
684         .read_delay     = rcu_read_delay,  /* just reuse rcu's version. */
685         .readunlock     = tasks_torture_read_unlock,
686         .get_gp_seq     = rcu_no_completed,
687         .deferred_free  = rcu_tasks_torture_deferred_free,
688         .sync           = synchronize_rcu_tasks,
689         .exp_sync       = synchronize_rcu_tasks,
690         .call           = call_rcu_tasks,
691         .cb_barrier     = rcu_barrier_tasks,
692         .fqs            = NULL,
693         .stats          = NULL,
694         .irq_capable    = 1,
695         .name           = "tasks"
696 };
697
698 static bool __maybe_unused torturing_tasks(void)
699 {
700         return cur_ops == &tasks_ops;
701 }
702
703 /*
704  * RCU torture priority-boost testing.  Runs one real-time thread per
705  * CPU for moderate bursts, repeatedly registering RCU callbacks and
706  * spinning waiting for them to be invoked.  If a given callback takes
707  * too long to be invoked, we assume that priority inversion has occurred.
708  */
709
710 struct rcu_boost_inflight {
711         struct rcu_head rcu;
712         int inflight;
713 };
714
715 static void rcu_torture_boost_cb(struct rcu_head *head)
716 {
717         struct rcu_boost_inflight *rbip =
718                 container_of(head, struct rcu_boost_inflight, rcu);
719
720         /* Ensure RCU-core accesses precede clearing ->inflight */
721         smp_store_release(&rbip->inflight, 0);
722 }
723
724 static int rcu_torture_boost(void *arg)
725 {
726         unsigned long call_rcu_time;
727         unsigned long endtime;
728         unsigned long oldstarttime;
729         struct rcu_boost_inflight rbi = { .inflight = 0 };
730         struct sched_param sp;
731
732         VERBOSE_TOROUT_STRING("rcu_torture_boost started");
733
734         /* Set real-time priority. */
735         sp.sched_priority = 1;
736         if (sched_setscheduler(current, SCHED_FIFO, &sp) < 0) {
737                 VERBOSE_TOROUT_STRING("rcu_torture_boost RT prio failed!");
738                 n_rcu_torture_boost_rterror++;
739         }
740
741         init_rcu_head_on_stack(&rbi.rcu);
742         /* Each pass through the following loop does one boost-test cycle. */
743         do {
744                 /* Wait for the next test interval. */
745                 oldstarttime = boost_starttime;
746                 while (ULONG_CMP_LT(jiffies, oldstarttime)) {
747                         schedule_timeout_interruptible(oldstarttime - jiffies);
748                         stutter_wait("rcu_torture_boost");
749                         if (torture_must_stop())
750                                 goto checkwait;
751                 }
752
753                 /* Do one boost-test interval. */
754                 endtime = oldstarttime + test_boost_duration * HZ;
755                 call_rcu_time = jiffies;
756                 while (ULONG_CMP_LT(jiffies, endtime)) {
757                         /* If we don't have a callback in flight, post one. */
758                         if (!smp_load_acquire(&rbi.inflight)) {
759                                 /* RCU core before ->inflight = 1. */
760                                 smp_store_release(&rbi.inflight, 1);
761                                 call_rcu(&rbi.rcu, rcu_torture_boost_cb);
762                                 if (jiffies - call_rcu_time >
763                                          test_boost_duration * HZ - HZ / 2) {
764                                         VERBOSE_TOROUT_STRING("rcu_torture_boost boosting failed");
765                                         n_rcu_torture_boost_failure++;
766                                 }
767                                 call_rcu_time = jiffies;
768                         }
769                         stutter_wait("rcu_torture_boost");
770                         if (torture_must_stop())
771                                 goto checkwait;
772                 }
773
774                 /*
775                  * Set the start time of the next test interval.
776                  * Yes, this is vulnerable to long delays, but such
777                  * delays simply cause a false negative for the next
778                  * interval.  Besides, we are running at RT priority,
779                  * so delays should be relatively rare.
780                  */
781                 while (oldstarttime == boost_starttime &&
782                        !kthread_should_stop()) {
783                         if (mutex_trylock(&boost_mutex)) {
784                                 boost_starttime = jiffies +
785                                                   test_boost_interval * HZ;
786                                 n_rcu_torture_boosts++;
787                                 mutex_unlock(&boost_mutex);
788                                 break;
789                         }
790                         schedule_timeout_uninterruptible(1);
791                 }
792
793                 /* Go do the stutter. */
794 checkwait:      stutter_wait("rcu_torture_boost");
795         } while (!torture_must_stop());
796
797         /* Clean up and exit. */
798         while (!kthread_should_stop() || smp_load_acquire(&rbi.inflight)) {
799                 torture_shutdown_absorb("rcu_torture_boost");
800                 schedule_timeout_uninterruptible(1);
801         }
802         destroy_rcu_head_on_stack(&rbi.rcu);
803         torture_kthread_stopping("rcu_torture_boost");
804         return 0;
805 }
806
807 static void rcu_torture_cbflood_cb(struct rcu_head *rhp)
808 {
809 }
810
811 /*
812  * RCU torture callback-flood kthread.  Repeatedly induces bursts of calls
813  * to call_rcu() or analogous, increasing the probability of occurrence
814  * of callback-overflow corner cases.
815  */
816 static int
817 rcu_torture_cbflood(void *arg)
818 {
819         int err = 1;
820         int i;
821         int j;
822         struct rcu_head *rhp;
823
824         if (cbflood_n_per_burst > 0 &&
825             cbflood_inter_holdoff > 0 &&
826             cbflood_intra_holdoff > 0 &&
827             cur_ops->call &&
828             cur_ops->cb_barrier) {
829                 rhp = vmalloc(array3_size(cbflood_n_burst,
830                                           cbflood_n_per_burst,
831                                           sizeof(*rhp)));
832                 err = !rhp;
833         }
834         if (err) {
835                 VERBOSE_TOROUT_STRING("rcu_torture_cbflood disabled: Bad args or OOM");
836                 goto wait_for_stop;
837         }
838         VERBOSE_TOROUT_STRING("rcu_torture_cbflood task started");
839         do {
840                 schedule_timeout_interruptible(cbflood_inter_holdoff);
841                 atomic_long_inc(&n_cbfloods);
842                 WARN_ON(signal_pending(current));
843                 for (i = 0; i < cbflood_n_burst; i++) {
844                         for (j = 0; j < cbflood_n_per_burst; j++) {
845                                 cur_ops->call(&rhp[i * cbflood_n_per_burst + j],
846                                               rcu_torture_cbflood_cb);
847                         }
848                         schedule_timeout_interruptible(cbflood_intra_holdoff);
849                         WARN_ON(signal_pending(current));
850                 }
851                 cur_ops->cb_barrier();
852                 stutter_wait("rcu_torture_cbflood");
853         } while (!torture_must_stop());
854         vfree(rhp);
855 wait_for_stop:
856         torture_kthread_stopping("rcu_torture_cbflood");
857         return 0;
858 }
859
860 /*
861  * RCU torture force-quiescent-state kthread.  Repeatedly induces
862  * bursts of calls to force_quiescent_state(), increasing the probability
863  * of occurrence of some important types of race conditions.
864  */
865 static int
866 rcu_torture_fqs(void *arg)
867 {
868         unsigned long fqs_resume_time;
869         int fqs_burst_remaining;
870
871         VERBOSE_TOROUT_STRING("rcu_torture_fqs task started");
872         do {
873                 fqs_resume_time = jiffies + fqs_stutter * HZ;
874                 while (ULONG_CMP_LT(jiffies, fqs_resume_time) &&
875                        !kthread_should_stop()) {
876                         schedule_timeout_interruptible(1);
877                 }
878                 fqs_burst_remaining = fqs_duration;
879                 while (fqs_burst_remaining > 0 &&
880                        !kthread_should_stop()) {
881                         cur_ops->fqs();
882                         udelay(fqs_holdoff);
883                         fqs_burst_remaining -= fqs_holdoff;
884                 }
885                 stutter_wait("rcu_torture_fqs");
886         } while (!torture_must_stop());
887         torture_kthread_stopping("rcu_torture_fqs");
888         return 0;
889 }
890
891 /*
892  * RCU torture writer kthread.  Repeatedly substitutes a new structure
893  * for that pointed to by rcu_torture_current, freeing the old structure
894  * after a series of grace periods (the "pipeline").
895  */
896 static int
897 rcu_torture_writer(void *arg)
898 {
899         bool can_expedite = !rcu_gp_is_expedited() && !rcu_gp_is_normal();
900         int expediting = 0;
901         unsigned long gp_snap;
902         bool gp_cond1 = gp_cond, gp_exp1 = gp_exp, gp_normal1 = gp_normal;
903         bool gp_sync1 = gp_sync;
904         int i;
905         struct rcu_torture *rp;
906         struct rcu_torture *old_rp;
907         static DEFINE_TORTURE_RANDOM(rand);
908         int synctype[] = { RTWS_DEF_FREE, RTWS_EXP_SYNC,
909                            RTWS_COND_GET, RTWS_SYNC };
910         int nsynctypes = 0;
911
912         VERBOSE_TOROUT_STRING("rcu_torture_writer task started");
913         if (!can_expedite)
914                 pr_alert("%s" TORTURE_FLAG
915                          " GP expediting controlled from boot/sysfs for %s.\n",
916                          torture_type, cur_ops->name);
917
918         /* Initialize synctype[] array.  If none set, take default. */
919         if (!gp_cond1 && !gp_exp1 && !gp_normal1 && !gp_sync1)
920                 gp_cond1 = gp_exp1 = gp_normal1 = gp_sync1 = true;
921         if (gp_cond1 && cur_ops->get_state && cur_ops->cond_sync) {
922                 synctype[nsynctypes++] = RTWS_COND_GET;
923                 pr_info("%s: Testing conditional GPs.\n", __func__);
924         } else if (gp_cond && (!cur_ops->get_state || !cur_ops->cond_sync)) {
925                 pr_alert("%s: gp_cond without primitives.\n", __func__);
926         }
927         if (gp_exp1 && cur_ops->exp_sync) {
928                 synctype[nsynctypes++] = RTWS_EXP_SYNC;
929                 pr_info("%s: Testing expedited GPs.\n", __func__);
930         } else if (gp_exp && !cur_ops->exp_sync) {
931                 pr_alert("%s: gp_exp without primitives.\n", __func__);
932         }
933         if (gp_normal1 && cur_ops->deferred_free) {
934                 synctype[nsynctypes++] = RTWS_DEF_FREE;
935                 pr_info("%s: Testing asynchronous GPs.\n", __func__);
936         } else if (gp_normal && !cur_ops->deferred_free) {
937                 pr_alert("%s: gp_normal without primitives.\n", __func__);
938         }
939         if (gp_sync1 && cur_ops->sync) {
940                 synctype[nsynctypes++] = RTWS_SYNC;
941                 pr_info("%s: Testing normal GPs.\n", __func__);
942         } else if (gp_sync && !cur_ops->sync) {
943                 pr_alert("%s: gp_sync without primitives.\n", __func__);
944         }
945         if (WARN_ONCE(nsynctypes == 0,
946                       "rcu_torture_writer: No update-side primitives.\n")) {
947                 /*
948                  * No updates primitives, so don't try updating.
949                  * The resulting test won't be testing much, hence the
950                  * above WARN_ONCE().
951                  */
952                 rcu_torture_writer_state = RTWS_STOPPING;
953                 torture_kthread_stopping("rcu_torture_writer");
954         }
955
956         do {
957                 rcu_torture_writer_state = RTWS_FIXED_DELAY;
958                 schedule_timeout_uninterruptible(1);
959                 rp = rcu_torture_alloc();
960                 if (rp == NULL)
961                         continue;
962                 rp->rtort_pipe_count = 0;
963                 rcu_torture_writer_state = RTWS_DELAY;
964                 udelay(torture_random(&rand) & 0x3ff);
965                 rcu_torture_writer_state = RTWS_REPLACE;
966                 old_rp = rcu_dereference_check(rcu_torture_current,
967                                                current == writer_task);
968                 rp->rtort_mbtest = 1;
969                 rcu_assign_pointer(rcu_torture_current, rp);
970                 smp_wmb(); /* Mods to old_rp must follow rcu_assign_pointer() */
971                 if (old_rp) {
972                         i = old_rp->rtort_pipe_count;
973                         if (i > RCU_TORTURE_PIPE_LEN)
974                                 i = RCU_TORTURE_PIPE_LEN;
975                         atomic_inc(&rcu_torture_wcount[i]);
976                         old_rp->rtort_pipe_count++;
977                         switch (synctype[torture_random(&rand) % nsynctypes]) {
978                         case RTWS_DEF_FREE:
979                                 rcu_torture_writer_state = RTWS_DEF_FREE;
980                                 cur_ops->deferred_free(old_rp);
981                                 break;
982                         case RTWS_EXP_SYNC:
983                                 rcu_torture_writer_state = RTWS_EXP_SYNC;
984                                 cur_ops->exp_sync();
985                                 rcu_torture_pipe_update(old_rp);
986                                 break;
987                         case RTWS_COND_GET:
988                                 rcu_torture_writer_state = RTWS_COND_GET;
989                                 gp_snap = cur_ops->get_state();
990                                 i = torture_random(&rand) % 16;
991                                 if (i != 0)
992                                         schedule_timeout_interruptible(i);
993                                 udelay(torture_random(&rand) % 1000);
994                                 rcu_torture_writer_state = RTWS_COND_SYNC;
995                                 cur_ops->cond_sync(gp_snap);
996                                 rcu_torture_pipe_update(old_rp);
997                                 break;
998                         case RTWS_SYNC:
999                                 rcu_torture_writer_state = RTWS_SYNC;
1000                                 cur_ops->sync();
1001                                 rcu_torture_pipe_update(old_rp);
1002                                 break;
1003                         default:
1004                                 WARN_ON_ONCE(1);
1005                                 break;
1006                         }
1007                 }
1008                 rcutorture_record_progress(++rcu_torture_current_version);
1009                 /* Cycle through nesting levels of rcu_expedite_gp() calls. */
1010                 if (can_expedite &&
1011                     !(torture_random(&rand) & 0xff & (!!expediting - 1))) {
1012                         WARN_ON_ONCE(expediting == 0 && rcu_gp_is_expedited());
1013                         if (expediting >= 0)
1014                                 rcu_expedite_gp();
1015                         else
1016                                 rcu_unexpedite_gp();
1017                         if (++expediting > 3)
1018                                 expediting = -expediting;
1019                 } else if (!can_expedite) { /* Disabled during boot, recheck. */
1020                         can_expedite = !rcu_gp_is_expedited() &&
1021                                        !rcu_gp_is_normal();
1022                 }
1023                 rcu_torture_writer_state = RTWS_STUTTER;
1024                 stutter_wait("rcu_torture_writer");
1025         } while (!torture_must_stop());
1026         /* Reset expediting back to unexpedited. */
1027         if (expediting > 0)
1028                 expediting = -expediting;
1029         while (can_expedite && expediting++ < 0)
1030                 rcu_unexpedite_gp();
1031         WARN_ON_ONCE(can_expedite && rcu_gp_is_expedited());
1032         if (!can_expedite)
1033                 pr_alert("%s" TORTURE_FLAG
1034                          " Dynamic grace-period expediting was disabled.\n",
1035                          torture_type);
1036         rcu_torture_writer_state = RTWS_STOPPING;
1037         torture_kthread_stopping("rcu_torture_writer");
1038         return 0;
1039 }
1040
1041 /*
1042  * RCU torture fake writer kthread.  Repeatedly calls sync, with a random
1043  * delay between calls.
1044  */
1045 static int
1046 rcu_torture_fakewriter(void *arg)
1047 {
1048         DEFINE_TORTURE_RANDOM(rand);
1049
1050         VERBOSE_TOROUT_STRING("rcu_torture_fakewriter task started");
1051         set_user_nice(current, MAX_NICE);
1052
1053         do {
1054                 schedule_timeout_uninterruptible(1 + torture_random(&rand)%10);
1055                 udelay(torture_random(&rand) & 0x3ff);
1056                 if (cur_ops->cb_barrier != NULL &&
1057                     torture_random(&rand) % (nfakewriters * 8) == 0) {
1058                         cur_ops->cb_barrier();
1059                 } else if (gp_normal == gp_exp) {
1060                         if (cur_ops->sync && torture_random(&rand) & 0x80)
1061                                 cur_ops->sync();
1062                         else if (cur_ops->exp_sync)
1063                                 cur_ops->exp_sync();
1064                 } else if (gp_normal && cur_ops->sync) {
1065                         cur_ops->sync();
1066                 } else if (cur_ops->exp_sync) {
1067                         cur_ops->exp_sync();
1068                 }
1069                 stutter_wait("rcu_torture_fakewriter");
1070         } while (!torture_must_stop());
1071
1072         torture_kthread_stopping("rcu_torture_fakewriter");
1073         return 0;
1074 }
1075
1076 static void rcu_torture_timer_cb(struct rcu_head *rhp)
1077 {
1078         kfree(rhp);
1079 }
1080
1081 /*
1082  * RCU torture reader from timer handler.  Dereferences rcu_torture_current,
1083  * incrementing the corresponding element of the pipeline array.  The
1084  * counter in the element should never be greater than 1, otherwise, the
1085  * RCU implementation is broken.
1086  */
1087 static void rcu_torture_timer(struct timer_list *unused)
1088 {
1089         int idx;
1090         unsigned long started;
1091         unsigned long completed;
1092         static DEFINE_TORTURE_RANDOM(rand);
1093         static DEFINE_SPINLOCK(rand_lock);
1094         struct rcu_torture *p;
1095         int pipe_count;
1096         unsigned long long ts;
1097
1098         idx = cur_ops->readlock();
1099         started = cur_ops->get_gp_seq();
1100         ts = rcu_trace_clock_local();
1101         p = rcu_dereference_check(rcu_torture_current,
1102                                   rcu_read_lock_bh_held() ||
1103                                   rcu_read_lock_sched_held() ||
1104                                   srcu_read_lock_held(srcu_ctlp) ||
1105                                   torturing_tasks());
1106         if (p == NULL) {
1107                 /* Leave because rcu_torture_writer is not yet underway */
1108                 cur_ops->readunlock(idx);
1109                 return;
1110         }
1111         if (p->rtort_mbtest == 0)
1112                 atomic_inc(&n_rcu_torture_mberror);
1113         spin_lock(&rand_lock);
1114         cur_ops->read_delay(&rand);
1115         n_rcu_torture_timers++;
1116         spin_unlock(&rand_lock);
1117         preempt_disable();
1118         pipe_count = p->rtort_pipe_count;
1119         if (pipe_count > RCU_TORTURE_PIPE_LEN) {
1120                 /* Should not happen, but... */
1121                 pipe_count = RCU_TORTURE_PIPE_LEN;
1122         }
1123         completed = cur_ops->get_gp_seq();
1124         if (pipe_count > 1) {
1125                 do_trace_rcu_torture_read(cur_ops->name, &p->rtort_rcu, ts,
1126                                           started, completed);
1127                 rcu_ftrace_dump(DUMP_ALL);
1128         }
1129         __this_cpu_inc(rcu_torture_count[pipe_count]);
1130         completed = completed - started;
1131         if (completed > ULONG_MAX >> 1)
1132                 completed = 0; /* Not all gp_seq have full range. */
1133         if (completed > RCU_TORTURE_PIPE_LEN) {
1134                 /* Should not happen, but... */
1135                 completed = RCU_TORTURE_PIPE_LEN;
1136         }
1137         __this_cpu_inc(rcu_torture_batch[completed]);
1138         preempt_enable();
1139         cur_ops->readunlock(idx);
1140
1141         /* Test call_rcu() invocation from interrupt handler. */
1142         if (cur_ops->call) {
1143                 struct rcu_head *rhp = kmalloc(sizeof(*rhp), GFP_NOWAIT);
1144
1145                 if (rhp)
1146                         cur_ops->call(rhp, rcu_torture_timer_cb);
1147         }
1148 }
1149
1150 /*
1151  * RCU torture reader kthread.  Repeatedly dereferences rcu_torture_current,
1152  * incrementing the corresponding element of the pipeline array.  The
1153  * counter in the element should never be greater than 1, otherwise, the
1154  * RCU implementation is broken.
1155  */
1156 static int
1157 rcu_torture_reader(void *arg)
1158 {
1159         unsigned long started;
1160         unsigned long completed;
1161         int idx;
1162         DEFINE_TORTURE_RANDOM(rand);
1163         struct rcu_torture *p;
1164         int pipe_count;
1165         struct timer_list t;
1166         unsigned long long ts;
1167
1168         VERBOSE_TOROUT_STRING("rcu_torture_reader task started");
1169         set_user_nice(current, MAX_NICE);
1170         if (irqreader && cur_ops->irq_capable)
1171                 timer_setup_on_stack(&t, rcu_torture_timer, 0);
1172
1173         do {
1174                 if (irqreader && cur_ops->irq_capable) {
1175                         if (!timer_pending(&t))
1176                                 mod_timer(&t, jiffies + 1);
1177                 }
1178                 idx = cur_ops->readlock();
1179                 started = cur_ops->get_gp_seq();
1180                 ts = rcu_trace_clock_local();
1181                 p = rcu_dereference_check(rcu_torture_current,
1182                                           rcu_read_lock_bh_held() ||
1183                                           rcu_read_lock_sched_held() ||
1184                                           srcu_read_lock_held(srcu_ctlp) ||
1185                                           torturing_tasks());
1186                 if (p == NULL) {
1187                         /* Wait for rcu_torture_writer to get underway */
1188                         cur_ops->readunlock(idx);
1189                         schedule_timeout_interruptible(HZ);
1190                         continue;
1191                 }
1192                 if (p->rtort_mbtest == 0)
1193                         atomic_inc(&n_rcu_torture_mberror);
1194                 cur_ops->read_delay(&rand);
1195                 preempt_disable();
1196                 pipe_count = p->rtort_pipe_count;
1197                 if (pipe_count > RCU_TORTURE_PIPE_LEN) {
1198                         /* Should not happen, but... */
1199                         pipe_count = RCU_TORTURE_PIPE_LEN;
1200                 }
1201                 completed = cur_ops->get_gp_seq();
1202                 if (pipe_count > 1) {
1203                         do_trace_rcu_torture_read(cur_ops->name, &p->rtort_rcu,
1204                                                   ts, started, completed);
1205                         rcu_ftrace_dump(DUMP_ALL);
1206                 }
1207                 __this_cpu_inc(rcu_torture_count[pipe_count]);
1208                 completed = completed - started;
1209                 if (completed > ULONG_MAX >> 1)
1210                         completed = 0; /* Not all gp_seq have full range. */
1211                 if (completed > RCU_TORTURE_PIPE_LEN) {
1212                         /* Should not happen, but... */
1213                         completed = RCU_TORTURE_PIPE_LEN;
1214                 }
1215                 __this_cpu_inc(rcu_torture_batch[completed]);
1216                 preempt_enable();
1217                 cur_ops->readunlock(idx);
1218                 stutter_wait("rcu_torture_reader");
1219         } while (!torture_must_stop());
1220         if (irqreader && cur_ops->irq_capable) {
1221                 del_timer_sync(&t);
1222                 destroy_timer_on_stack(&t);
1223         }
1224         torture_kthread_stopping("rcu_torture_reader");
1225         return 0;
1226 }
1227
1228 /*
1229  * Print torture statistics.  Caller must ensure that there is only
1230  * one call to this function at a given time!!!  This is normally
1231  * accomplished by relying on the module system to only have one copy
1232  * of the module loaded, and then by giving the rcu_torture_stats
1233  * kthread full control (or the init/cleanup functions when rcu_torture_stats
1234  * thread is not running).
1235  */
1236 static void
1237 rcu_torture_stats_print(void)
1238 {
1239         int cpu;
1240         int i;
1241         long pipesummary[RCU_TORTURE_PIPE_LEN + 1] = { 0 };
1242         long batchsummary[RCU_TORTURE_PIPE_LEN + 1] = { 0 };
1243         static unsigned long rtcv_snap = ULONG_MAX;
1244         static bool splatted;
1245         struct task_struct *wtp;
1246
1247         for_each_possible_cpu(cpu) {
1248                 for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++) {
1249                         pipesummary[i] += per_cpu(rcu_torture_count, cpu)[i];
1250                         batchsummary[i] += per_cpu(rcu_torture_batch, cpu)[i];
1251                 }
1252         }
1253         for (i = RCU_TORTURE_PIPE_LEN - 1; i >= 0; i--) {
1254                 if (pipesummary[i] != 0)
1255                         break;
1256         }
1257
1258         pr_alert("%s%s ", torture_type, TORTURE_FLAG);
1259         pr_cont("rtc: %p ver: %lu tfle: %d rta: %d rtaf: %d rtf: %d ",
1260                 rcu_torture_current,
1261                 rcu_torture_current_version,
1262                 list_empty(&rcu_torture_freelist),
1263                 atomic_read(&n_rcu_torture_alloc),
1264                 atomic_read(&n_rcu_torture_alloc_fail),
1265                 atomic_read(&n_rcu_torture_free));
1266         pr_cont("rtmbe: %d rtbe: %ld rtbke: %ld rtbre: %ld ",
1267                 atomic_read(&n_rcu_torture_mberror),
1268                 n_rcu_torture_barrier_error,
1269                 n_rcu_torture_boost_ktrerror,
1270                 n_rcu_torture_boost_rterror);
1271         pr_cont("rtbf: %ld rtb: %ld nt: %ld ",
1272                 n_rcu_torture_boost_failure,
1273                 n_rcu_torture_boosts,
1274                 n_rcu_torture_timers);
1275         torture_onoff_stats();
1276         pr_cont("barrier: %ld/%ld:%ld ",
1277                 n_barrier_successes,
1278                 n_barrier_attempts,
1279                 n_rcu_torture_barrier_error);
1280         pr_cont("cbflood: %ld\n", atomic_long_read(&n_cbfloods));
1281
1282         pr_alert("%s%s ", torture_type, TORTURE_FLAG);
1283         if (atomic_read(&n_rcu_torture_mberror) != 0 ||
1284             n_rcu_torture_barrier_error != 0 ||
1285             n_rcu_torture_boost_ktrerror != 0 ||
1286             n_rcu_torture_boost_rterror != 0 ||
1287             n_rcu_torture_boost_failure != 0 ||
1288             i > 1) {
1289                 pr_cont("%s", "!!! ");
1290                 atomic_inc(&n_rcu_torture_error);
1291                 WARN_ON_ONCE(1);
1292         }
1293         pr_cont("Reader Pipe: ");
1294         for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++)
1295                 pr_cont(" %ld", pipesummary[i]);
1296         pr_cont("\n");
1297
1298         pr_alert("%s%s ", torture_type, TORTURE_FLAG);
1299         pr_cont("Reader Batch: ");
1300         for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++)
1301                 pr_cont(" %ld", batchsummary[i]);
1302         pr_cont("\n");
1303
1304         pr_alert("%s%s ", torture_type, TORTURE_FLAG);
1305         pr_cont("Free-Block Circulation: ");
1306         for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++) {
1307                 pr_cont(" %d", atomic_read(&rcu_torture_wcount[i]));
1308         }
1309         pr_cont("\n");
1310
1311         if (cur_ops->stats)
1312                 cur_ops->stats();
1313         if (rtcv_snap == rcu_torture_current_version &&
1314             rcu_torture_current != NULL) {
1315                 int __maybe_unused flags = 0;
1316                 unsigned long __maybe_unused gp_seq = 0;
1317
1318                 rcutorture_get_gp_data(cur_ops->ttype,
1319                                        &flags, &gp_seq);
1320                 srcutorture_get_gp_data(cur_ops->ttype, srcu_ctlp,
1321                                         &flags, &gp_seq);
1322                 wtp = READ_ONCE(writer_task);
1323                 pr_alert("??? Writer stall state %s(%d) g%lu f%#x ->state %#lx cpu %d\n",
1324                          rcu_torture_writer_state_getname(),
1325                          rcu_torture_writer_state, gp_seq, flags,
1326                          wtp == NULL ? ~0UL : wtp->state,
1327                          wtp == NULL ? -1 : (int)task_cpu(wtp));
1328                 if (!splatted && wtp) {
1329                         sched_show_task(wtp);
1330                         splatted = true;
1331                 }
1332                 show_rcu_gp_kthreads();
1333                 rcu_ftrace_dump(DUMP_ALL);
1334         }
1335         rtcv_snap = rcu_torture_current_version;
1336 }
1337
1338 /*
1339  * Periodically prints torture statistics, if periodic statistics printing
1340  * was specified via the stat_interval module parameter.
1341  */
1342 static int
1343 rcu_torture_stats(void *arg)
1344 {
1345         VERBOSE_TOROUT_STRING("rcu_torture_stats task started");
1346         do {
1347                 schedule_timeout_interruptible(stat_interval * HZ);
1348                 rcu_torture_stats_print();
1349                 torture_shutdown_absorb("rcu_torture_stats");
1350         } while (!torture_must_stop());
1351         torture_kthread_stopping("rcu_torture_stats");
1352         return 0;
1353 }
1354
1355 static inline void
1356 rcu_torture_print_module_parms(struct rcu_torture_ops *cur_ops, const char *tag)
1357 {
1358         pr_alert("%s" TORTURE_FLAG
1359                  "--- %s: nreaders=%d nfakewriters=%d "
1360                  "stat_interval=%d verbose=%d test_no_idle_hz=%d "
1361                  "shuffle_interval=%d stutter=%d irqreader=%d "
1362                  "fqs_duration=%d fqs_holdoff=%d fqs_stutter=%d "
1363                  "test_boost=%d/%d test_boost_interval=%d "
1364                  "test_boost_duration=%d shutdown_secs=%d "
1365                  "stall_cpu=%d stall_cpu_holdoff=%d stall_cpu_irqsoff=%d "
1366                  "n_barrier_cbs=%d "
1367                  "onoff_interval=%d onoff_holdoff=%d\n",
1368                  torture_type, tag, nrealreaders, nfakewriters,
1369                  stat_interval, verbose, test_no_idle_hz, shuffle_interval,
1370                  stutter, irqreader, fqs_duration, fqs_holdoff, fqs_stutter,
1371                  test_boost, cur_ops->can_boost,
1372                  test_boost_interval, test_boost_duration, shutdown_secs,
1373                  stall_cpu, stall_cpu_holdoff, stall_cpu_irqsoff,
1374                  n_barrier_cbs,
1375                  onoff_interval, onoff_holdoff);
1376 }
1377
1378 static int rcutorture_booster_cleanup(unsigned int cpu)
1379 {
1380         struct task_struct *t;
1381
1382         if (boost_tasks[cpu] == NULL)
1383                 return 0;
1384         mutex_lock(&boost_mutex);
1385         t = boost_tasks[cpu];
1386         boost_tasks[cpu] = NULL;
1387         mutex_unlock(&boost_mutex);
1388
1389         /* This must be outside of the mutex, otherwise deadlock! */
1390         torture_stop_kthread(rcu_torture_boost, t);
1391         return 0;
1392 }
1393
1394 static int rcutorture_booster_init(unsigned int cpu)
1395 {
1396         int retval;
1397
1398         if (boost_tasks[cpu] != NULL)
1399                 return 0;  /* Already created, nothing more to do. */
1400
1401         /* Don't allow time recalculation while creating a new task. */
1402         mutex_lock(&boost_mutex);
1403         VERBOSE_TOROUT_STRING("Creating rcu_torture_boost task");
1404         boost_tasks[cpu] = kthread_create_on_node(rcu_torture_boost, NULL,
1405                                                   cpu_to_node(cpu),
1406                                                   "rcu_torture_boost");
1407         if (IS_ERR(boost_tasks[cpu])) {
1408                 retval = PTR_ERR(boost_tasks[cpu]);
1409                 VERBOSE_TOROUT_STRING("rcu_torture_boost task create failed");
1410                 n_rcu_torture_boost_ktrerror++;
1411                 boost_tasks[cpu] = NULL;
1412                 mutex_unlock(&boost_mutex);
1413                 return retval;
1414         }
1415         kthread_bind(boost_tasks[cpu], cpu);
1416         wake_up_process(boost_tasks[cpu]);
1417         mutex_unlock(&boost_mutex);
1418         return 0;
1419 }
1420
1421 /*
1422  * CPU-stall kthread.  It waits as specified by stall_cpu_holdoff, then
1423  * induces a CPU stall for the time specified by stall_cpu.
1424  */
1425 static int rcu_torture_stall(void *args)
1426 {
1427         unsigned long stop_at;
1428
1429         VERBOSE_TOROUT_STRING("rcu_torture_stall task started");
1430         if (stall_cpu_holdoff > 0) {
1431                 VERBOSE_TOROUT_STRING("rcu_torture_stall begin holdoff");
1432                 schedule_timeout_interruptible(stall_cpu_holdoff * HZ);
1433                 VERBOSE_TOROUT_STRING("rcu_torture_stall end holdoff");
1434         }
1435         if (!kthread_should_stop()) {
1436                 stop_at = get_seconds() + stall_cpu;
1437                 /* RCU CPU stall is expected behavior in following code. */
1438                 rcu_read_lock();
1439                 if (stall_cpu_irqsoff)
1440                         local_irq_disable();
1441                 else
1442                         preempt_disable();
1443                 pr_alert("rcu_torture_stall start on CPU %d.\n",
1444                          smp_processor_id());
1445                 while (ULONG_CMP_LT(get_seconds(), stop_at))
1446                         continue;  /* Induce RCU CPU stall warning. */
1447                 if (stall_cpu_irqsoff)
1448                         local_irq_enable();
1449                 else
1450                         preempt_enable();
1451                 rcu_read_unlock();
1452                 pr_alert("rcu_torture_stall end.\n");
1453         }
1454         torture_shutdown_absorb("rcu_torture_stall");
1455         while (!kthread_should_stop())
1456                 schedule_timeout_interruptible(10 * HZ);
1457         return 0;
1458 }
1459
1460 /* Spawn CPU-stall kthread, if stall_cpu specified. */
1461 static int __init rcu_torture_stall_init(void)
1462 {
1463         if (stall_cpu <= 0)
1464                 return 0;
1465         return torture_create_kthread(rcu_torture_stall, NULL, stall_task);
1466 }
1467
1468 /* Callback function for RCU barrier testing. */
1469 static void rcu_torture_barrier_cbf(struct rcu_head *rcu)
1470 {
1471         atomic_inc(&barrier_cbs_invoked);
1472 }
1473
1474 /* kthread function to register callbacks used to test RCU barriers. */
1475 static int rcu_torture_barrier_cbs(void *arg)
1476 {
1477         long myid = (long)arg;
1478         bool lastphase = 0;
1479         bool newphase;
1480         struct rcu_head rcu;
1481
1482         init_rcu_head_on_stack(&rcu);
1483         VERBOSE_TOROUT_STRING("rcu_torture_barrier_cbs task started");
1484         set_user_nice(current, MAX_NICE);
1485         do {
1486                 wait_event(barrier_cbs_wq[myid],
1487                            (newphase =
1488                             smp_load_acquire(&barrier_phase)) != lastphase ||
1489                            torture_must_stop());
1490                 lastphase = newphase;
1491                 if (torture_must_stop())
1492                         break;
1493                 /*
1494                  * The above smp_load_acquire() ensures barrier_phase load
1495                  * is ordered before the following ->call().
1496                  */
1497                 local_irq_disable(); /* Just to test no-irq call_rcu(). */
1498                 cur_ops->call(&rcu, rcu_torture_barrier_cbf);
1499                 local_irq_enable();
1500                 if (atomic_dec_and_test(&barrier_cbs_count))
1501                         wake_up(&barrier_wq);
1502         } while (!torture_must_stop());
1503         if (cur_ops->cb_barrier != NULL)
1504                 cur_ops->cb_barrier();
1505         destroy_rcu_head_on_stack(&rcu);
1506         torture_kthread_stopping("rcu_torture_barrier_cbs");
1507         return 0;
1508 }
1509
1510 /* kthread function to drive and coordinate RCU barrier testing. */
1511 static int rcu_torture_barrier(void *arg)
1512 {
1513         int i;
1514
1515         VERBOSE_TOROUT_STRING("rcu_torture_barrier task starting");
1516         do {
1517                 atomic_set(&barrier_cbs_invoked, 0);
1518                 atomic_set(&barrier_cbs_count, n_barrier_cbs);
1519                 /* Ensure barrier_phase ordered after prior assignments. */
1520                 smp_store_release(&barrier_phase, !barrier_phase);
1521                 for (i = 0; i < n_barrier_cbs; i++)
1522                         wake_up(&barrier_cbs_wq[i]);
1523                 wait_event(barrier_wq,
1524                            atomic_read(&barrier_cbs_count) == 0 ||
1525                            torture_must_stop());
1526                 if (torture_must_stop())
1527                         break;
1528                 n_barrier_attempts++;
1529                 cur_ops->cb_barrier(); /* Implies smp_mb() for wait_event(). */
1530                 if (atomic_read(&barrier_cbs_invoked) != n_barrier_cbs) {
1531                         n_rcu_torture_barrier_error++;
1532                         pr_err("barrier_cbs_invoked = %d, n_barrier_cbs = %d\n",
1533                                atomic_read(&barrier_cbs_invoked),
1534                                n_barrier_cbs);
1535                         WARN_ON_ONCE(1);
1536                 }
1537                 n_barrier_successes++;
1538                 schedule_timeout_interruptible(HZ / 10);
1539         } while (!torture_must_stop());
1540         torture_kthread_stopping("rcu_torture_barrier");
1541         return 0;
1542 }
1543
1544 /* Initialize RCU barrier testing. */
1545 static int rcu_torture_barrier_init(void)
1546 {
1547         int i;
1548         int ret;
1549
1550         if (n_barrier_cbs <= 0)
1551                 return 0;
1552         if (cur_ops->call == NULL || cur_ops->cb_barrier == NULL) {
1553                 pr_alert("%s" TORTURE_FLAG
1554                          " Call or barrier ops missing for %s,\n",
1555                          torture_type, cur_ops->name);
1556                 pr_alert("%s" TORTURE_FLAG
1557                          " RCU barrier testing omitted from run.\n",
1558                          torture_type);
1559                 return 0;
1560         }
1561         atomic_set(&barrier_cbs_count, 0);
1562         atomic_set(&barrier_cbs_invoked, 0);
1563         barrier_cbs_tasks =
1564                 kcalloc(n_barrier_cbs, sizeof(barrier_cbs_tasks[0]),
1565                         GFP_KERNEL);
1566         barrier_cbs_wq =
1567                 kcalloc(n_barrier_cbs, sizeof(barrier_cbs_wq[0]), GFP_KERNEL);
1568         if (barrier_cbs_tasks == NULL || !barrier_cbs_wq)
1569                 return -ENOMEM;
1570         for (i = 0; i < n_barrier_cbs; i++) {
1571                 init_waitqueue_head(&barrier_cbs_wq[i]);
1572                 ret = torture_create_kthread(rcu_torture_barrier_cbs,
1573                                              (void *)(long)i,
1574                                              barrier_cbs_tasks[i]);
1575                 if (ret)
1576                         return ret;
1577         }
1578         return torture_create_kthread(rcu_torture_barrier, NULL, barrier_task);
1579 }
1580
1581 /* Clean up after RCU barrier testing. */
1582 static void rcu_torture_barrier_cleanup(void)
1583 {
1584         int i;
1585
1586         torture_stop_kthread(rcu_torture_barrier, barrier_task);
1587         if (barrier_cbs_tasks != NULL) {
1588                 for (i = 0; i < n_barrier_cbs; i++)
1589                         torture_stop_kthread(rcu_torture_barrier_cbs,
1590                                              barrier_cbs_tasks[i]);
1591                 kfree(barrier_cbs_tasks);
1592                 barrier_cbs_tasks = NULL;
1593         }
1594         if (barrier_cbs_wq != NULL) {
1595                 kfree(barrier_cbs_wq);
1596                 barrier_cbs_wq = NULL;
1597         }
1598 }
1599
1600 static enum cpuhp_state rcutor_hp;
1601
1602 static void
1603 rcu_torture_cleanup(void)
1604 {
1605         int flags = 0;
1606         unsigned long gp_seq = 0;
1607         int i;
1608
1609         rcutorture_record_test_transition();
1610         if (torture_cleanup_begin()) {
1611                 if (cur_ops->cb_barrier != NULL)
1612                         cur_ops->cb_barrier();
1613                 return;
1614         }
1615
1616         rcu_torture_barrier_cleanup();
1617         torture_stop_kthread(rcu_torture_stall, stall_task);
1618         torture_stop_kthread(rcu_torture_writer, writer_task);
1619
1620         if (reader_tasks) {
1621                 for (i = 0; i < nrealreaders; i++)
1622                         torture_stop_kthread(rcu_torture_reader,
1623                                              reader_tasks[i]);
1624                 kfree(reader_tasks);
1625         }
1626         rcu_torture_current = NULL;
1627
1628         if (fakewriter_tasks) {
1629                 for (i = 0; i < nfakewriters; i++) {
1630                         torture_stop_kthread(rcu_torture_fakewriter,
1631                                              fakewriter_tasks[i]);
1632                 }
1633                 kfree(fakewriter_tasks);
1634                 fakewriter_tasks = NULL;
1635         }
1636
1637         rcutorture_get_gp_data(cur_ops->ttype, &flags, &gp_seq);
1638         srcutorture_get_gp_data(cur_ops->ttype, srcu_ctlp, &flags, &gp_seq);
1639         pr_alert("%s:  End-test grace-period state: g%lu f%#x\n",
1640                  cur_ops->name, gp_seq, flags);
1641         torture_stop_kthread(rcu_torture_stats, stats_task);
1642         torture_stop_kthread(rcu_torture_fqs, fqs_task);
1643         for (i = 0; i < ncbflooders; i++)
1644                 torture_stop_kthread(rcu_torture_cbflood, cbflood_task[i]);
1645         if ((test_boost == 1 && cur_ops->can_boost) ||
1646             test_boost == 2)
1647                 cpuhp_remove_state(rcutor_hp);
1648
1649         /*
1650          * Wait for all RCU callbacks to fire, then do flavor-specific
1651          * cleanup operations.
1652          */
1653         if (cur_ops->cb_barrier != NULL)
1654                 cur_ops->cb_barrier();
1655         if (cur_ops->cleanup != NULL)
1656                 cur_ops->cleanup();
1657
1658         rcu_torture_stats_print();  /* -After- the stats thread is stopped! */
1659
1660         if (atomic_read(&n_rcu_torture_error) || n_rcu_torture_barrier_error)
1661                 rcu_torture_print_module_parms(cur_ops, "End of test: FAILURE");
1662         else if (torture_onoff_failures())
1663                 rcu_torture_print_module_parms(cur_ops,
1664                                                "End of test: RCU_HOTPLUG");
1665         else
1666                 rcu_torture_print_module_parms(cur_ops, "End of test: SUCCESS");
1667         torture_cleanup_end();
1668 }
1669
1670 #ifdef CONFIG_DEBUG_OBJECTS_RCU_HEAD
1671 static void rcu_torture_leak_cb(struct rcu_head *rhp)
1672 {
1673 }
1674
1675 static void rcu_torture_err_cb(struct rcu_head *rhp)
1676 {
1677         /*
1678          * This -might- happen due to race conditions, but is unlikely.
1679          * The scenario that leads to this happening is that the
1680          * first of the pair of duplicate callbacks is queued,
1681          * someone else starts a grace period that includes that
1682          * callback, then the second of the pair must wait for the
1683          * next grace period.  Unlikely, but can happen.  If it
1684          * does happen, the debug-objects subsystem won't have splatted.
1685          */
1686         pr_alert("%s: duplicated callback was invoked.\n", KBUILD_MODNAME);
1687 }
1688 #endif /* #ifdef CONFIG_DEBUG_OBJECTS_RCU_HEAD */
1689
1690 /*
1691  * Verify that double-free causes debug-objects to complain, but only
1692  * if CONFIG_DEBUG_OBJECTS_RCU_HEAD=y.  Otherwise, say that the test
1693  * cannot be carried out.
1694  */
1695 static void rcu_test_debug_objects(void)
1696 {
1697 #ifdef CONFIG_DEBUG_OBJECTS_RCU_HEAD
1698         struct rcu_head rh1;
1699         struct rcu_head rh2;
1700
1701         init_rcu_head_on_stack(&rh1);
1702         init_rcu_head_on_stack(&rh2);
1703         pr_alert("%s: WARN: Duplicate call_rcu() test starting.\n", KBUILD_MODNAME);
1704
1705         /* Try to queue the rh2 pair of callbacks for the same grace period. */
1706         preempt_disable(); /* Prevent preemption from interrupting test. */
1707         rcu_read_lock(); /* Make it impossible to finish a grace period. */
1708         call_rcu(&rh1, rcu_torture_leak_cb); /* Start grace period. */
1709         local_irq_disable(); /* Make it harder to start a new grace period. */
1710         call_rcu(&rh2, rcu_torture_leak_cb);
1711         call_rcu(&rh2, rcu_torture_err_cb); /* Duplicate callback. */
1712         local_irq_enable();
1713         rcu_read_unlock();
1714         preempt_enable();
1715
1716         /* Wait for them all to get done so we can safely return. */
1717         rcu_barrier();
1718         pr_alert("%s: WARN: Duplicate call_rcu() test complete.\n", KBUILD_MODNAME);
1719         destroy_rcu_head_on_stack(&rh1);
1720         destroy_rcu_head_on_stack(&rh2);
1721 #else /* #ifdef CONFIG_DEBUG_OBJECTS_RCU_HEAD */
1722         pr_alert("%s: !CONFIG_DEBUG_OBJECTS_RCU_HEAD, not testing duplicate call_rcu()\n", KBUILD_MODNAME);
1723 #endif /* #else #ifdef CONFIG_DEBUG_OBJECTS_RCU_HEAD */
1724 }
1725
1726 static int __init
1727 rcu_torture_init(void)
1728 {
1729         int i;
1730         int cpu;
1731         int firsterr = 0;
1732         static struct rcu_torture_ops *torture_ops[] = {
1733                 &rcu_ops, &rcu_bh_ops, &rcu_busted_ops, &srcu_ops, &srcud_ops,
1734                 &sched_ops, &tasks_ops,
1735         };
1736
1737         if (!torture_init_begin(torture_type, verbose))
1738                 return -EBUSY;
1739
1740         /* Process args and tell the world that the torturer is on the job. */
1741         for (i = 0; i < ARRAY_SIZE(torture_ops); i++) {
1742                 cur_ops = torture_ops[i];
1743                 if (strcmp(torture_type, cur_ops->name) == 0)
1744                         break;
1745         }
1746         if (i == ARRAY_SIZE(torture_ops)) {
1747                 pr_alert("rcu-torture: invalid torture type: \"%s\"\n",
1748                          torture_type);
1749                 pr_alert("rcu-torture types:");
1750                 for (i = 0; i < ARRAY_SIZE(torture_ops); i++)
1751                         pr_alert(" %s", torture_ops[i]->name);
1752                 pr_alert("\n");
1753                 firsterr = -EINVAL;
1754                 goto unwind;
1755         }
1756         if (cur_ops->fqs == NULL && fqs_duration != 0) {
1757                 pr_alert("rcu-torture: ->fqs NULL and non-zero fqs_duration, fqs disabled.\n");
1758                 fqs_duration = 0;
1759         }
1760         if (cur_ops->init)
1761                 cur_ops->init();
1762
1763         if (nreaders >= 0) {
1764                 nrealreaders = nreaders;
1765         } else {
1766                 nrealreaders = num_online_cpus() - 2 - nreaders;
1767                 if (nrealreaders <= 0)
1768                         nrealreaders = 1;
1769         }
1770         rcu_torture_print_module_parms(cur_ops, "Start of test");
1771
1772         /* Set up the freelist. */
1773
1774         INIT_LIST_HEAD(&rcu_torture_freelist);
1775         for (i = 0; i < ARRAY_SIZE(rcu_tortures); i++) {
1776                 rcu_tortures[i].rtort_mbtest = 0;
1777                 list_add_tail(&rcu_tortures[i].rtort_free,
1778                               &rcu_torture_freelist);
1779         }
1780
1781         /* Initialize the statistics so that each run gets its own numbers. */
1782
1783         rcu_torture_current = NULL;
1784         rcu_torture_current_version = 0;
1785         atomic_set(&n_rcu_torture_alloc, 0);
1786         atomic_set(&n_rcu_torture_alloc_fail, 0);
1787         atomic_set(&n_rcu_torture_free, 0);
1788         atomic_set(&n_rcu_torture_mberror, 0);
1789         atomic_set(&n_rcu_torture_error, 0);
1790         n_rcu_torture_barrier_error = 0;
1791         n_rcu_torture_boost_ktrerror = 0;
1792         n_rcu_torture_boost_rterror = 0;
1793         n_rcu_torture_boost_failure = 0;
1794         n_rcu_torture_boosts = 0;
1795         for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++)
1796                 atomic_set(&rcu_torture_wcount[i], 0);
1797         for_each_possible_cpu(cpu) {
1798                 for (i = 0; i < RCU_TORTURE_PIPE_LEN + 1; i++) {
1799                         per_cpu(rcu_torture_count, cpu)[i] = 0;
1800                         per_cpu(rcu_torture_batch, cpu)[i] = 0;
1801                 }
1802         }
1803
1804         /* Start up the kthreads. */
1805
1806         firsterr = torture_create_kthread(rcu_torture_writer, NULL,
1807                                           writer_task);
1808         if (firsterr)
1809                 goto unwind;
1810         if (nfakewriters > 0) {
1811                 fakewriter_tasks = kcalloc(nfakewriters,
1812                                            sizeof(fakewriter_tasks[0]),
1813                                            GFP_KERNEL);
1814                 if (fakewriter_tasks == NULL) {
1815                         VERBOSE_TOROUT_ERRSTRING("out of memory");
1816                         firsterr = -ENOMEM;
1817                         goto unwind;
1818                 }
1819         }
1820         for (i = 0; i < nfakewriters; i++) {
1821                 firsterr = torture_create_kthread(rcu_torture_fakewriter,
1822                                                   NULL, fakewriter_tasks[i]);
1823                 if (firsterr)
1824                         goto unwind;
1825         }
1826         reader_tasks = kcalloc(nrealreaders, sizeof(reader_tasks[0]),
1827                                GFP_KERNEL);
1828         if (reader_tasks == NULL) {
1829                 VERBOSE_TOROUT_ERRSTRING("out of memory");
1830                 firsterr = -ENOMEM;
1831                 goto unwind;
1832         }
1833         for (i = 0; i < nrealreaders; i++) {
1834                 firsterr = torture_create_kthread(rcu_torture_reader, NULL,
1835                                                   reader_tasks[i]);
1836                 if (firsterr)
1837                         goto unwind;
1838         }
1839         if (stat_interval > 0) {
1840                 firsterr = torture_create_kthread(rcu_torture_stats, NULL,
1841                                                   stats_task);
1842                 if (firsterr)
1843                         goto unwind;
1844         }
1845         if (test_no_idle_hz && shuffle_interval > 0) {
1846                 firsterr = torture_shuffle_init(shuffle_interval * HZ);
1847                 if (firsterr)
1848                         goto unwind;
1849         }
1850         if (stutter < 0)
1851                 stutter = 0;
1852         if (stutter) {
1853                 firsterr = torture_stutter_init(stutter * HZ);
1854                 if (firsterr)
1855                         goto unwind;
1856         }
1857         if (fqs_duration < 0)
1858                 fqs_duration = 0;
1859         if (fqs_duration) {
1860                 /* Create the fqs thread */
1861                 firsterr = torture_create_kthread(rcu_torture_fqs, NULL,
1862                                                   fqs_task);
1863                 if (firsterr)
1864                         goto unwind;
1865         }
1866         if (test_boost_interval < 1)
1867                 test_boost_interval = 1;
1868         if (test_boost_duration < 2)
1869                 test_boost_duration = 2;
1870         if ((test_boost == 1 && cur_ops->can_boost) ||
1871             test_boost == 2) {
1872
1873                 boost_starttime = jiffies + test_boost_interval * HZ;
1874
1875                 firsterr = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "RCU_TORTURE",
1876                                              rcutorture_booster_init,
1877                                              rcutorture_booster_cleanup);
1878                 if (firsterr < 0)
1879                         goto unwind;
1880                 rcutor_hp = firsterr;
1881         }
1882         firsterr = torture_shutdown_init(shutdown_secs, rcu_torture_cleanup);
1883         if (firsterr)
1884                 goto unwind;
1885         firsterr = torture_onoff_init(onoff_holdoff * HZ, onoff_interval * HZ);
1886         if (firsterr)
1887                 goto unwind;
1888         firsterr = rcu_torture_stall_init();
1889         if (firsterr)
1890                 goto unwind;
1891         firsterr = rcu_torture_barrier_init();
1892         if (firsterr)
1893                 goto unwind;
1894         if (object_debug)
1895                 rcu_test_debug_objects();
1896         if (cbflood_n_burst > 0) {
1897                 /* Create the cbflood threads */
1898                 ncbflooders = (num_online_cpus() + 3) / 4;
1899                 cbflood_task = kcalloc(ncbflooders, sizeof(*cbflood_task),
1900                                        GFP_KERNEL);
1901                 if (!cbflood_task) {
1902                         VERBOSE_TOROUT_ERRSTRING("out of memory");
1903                         firsterr = -ENOMEM;
1904                         goto unwind;
1905                 }
1906                 for (i = 0; i < ncbflooders; i++) {
1907                         firsterr = torture_create_kthread(rcu_torture_cbflood,
1908                                                           NULL,
1909                                                           cbflood_task[i]);
1910                         if (firsterr)
1911                                 goto unwind;
1912                 }
1913         }
1914         rcutorture_record_test_transition();
1915         torture_init_end();
1916         return 0;
1917
1918 unwind:
1919         torture_init_end();
1920         rcu_torture_cleanup();
1921         return firsterr;
1922 }
1923
1924 module_init(rcu_torture_init);
1925 module_exit(rcu_torture_cleanup);