1 // SPDX-License-Identifier: GPL-2.0+
3 * Read-Copy Update module-based torture test facility
5 * Copyright (C) IBM Corporation, 2005, 2006
7 * Authors: Paul E. McKenney <paulmck@linux.ibm.com>
8 * Josh Triplett <josh@joshtriplett.org>
10 * See also: Documentation/RCU/torture.txt
13 #define pr_fmt(fmt) fmt
15 #include <linux/types.h>
16 #include <linux/kernel.h>
17 #include <linux/init.h>
18 #include <linux/module.h>
19 #include <linux/kthread.h>
20 #include <linux/err.h>
21 #include <linux/spinlock.h>
22 #include <linux/smp.h>
23 #include <linux/rcupdate.h>
24 #include <linux/interrupt.h>
25 #include <linux/sched/signal.h>
26 #include <uapi/linux/sched/types.h>
27 #include <linux/atomic.h>
28 #include <linux/bitops.h>
29 #include <linux/completion.h>
30 #include <linux/moduleparam.h>
31 #include <linux/percpu.h>
32 #include <linux/notifier.h>
33 #include <linux/reboot.h>
34 #include <linux/freezer.h>
35 #include <linux/cpu.h>
36 #include <linux/delay.h>
37 #include <linux/stat.h>
38 #include <linux/srcu.h>
39 #include <linux/slab.h>
40 #include <linux/trace_clock.h>
41 #include <asm/byteorder.h>
42 #include <linux/torture.h>
43 #include <linux/vmalloc.h>
44 #include <linux/sched/debug.h>
45 #include <linux/sched/sysctl.h>
46 #include <linux/oom.h>
50 MODULE_LICENSE("GPL");
51 MODULE_AUTHOR("Paul E. McKenney <paulmck@linux.ibm.com> and Josh Triplett <josh@joshtriplett.org>");
54 /* Bits for ->extendables field, extendables param, and related definitions. */
55 #define RCUTORTURE_RDR_SHIFT 8 /* Put SRCU index in upper bits. */
56 #define RCUTORTURE_RDR_MASK ((1 << RCUTORTURE_RDR_SHIFT) - 1)
57 #define RCUTORTURE_RDR_BH 0x01 /* Extend readers by disabling bh. */
58 #define RCUTORTURE_RDR_IRQ 0x02 /* ... disabling interrupts. */
59 #define RCUTORTURE_RDR_PREEMPT 0x04 /* ... disabling preemption. */
60 #define RCUTORTURE_RDR_RBH 0x08 /* ... rcu_read_lock_bh(). */
61 #define RCUTORTURE_RDR_SCHED 0x10 /* ... rcu_read_lock_sched(). */
62 #define RCUTORTURE_RDR_RCU 0x20 /* ... entering another RCU reader. */
63 #define RCUTORTURE_RDR_NBITS 6 /* Number of bits defined above. */
64 #define RCUTORTURE_MAX_EXTEND \
65 (RCUTORTURE_RDR_BH | RCUTORTURE_RDR_IRQ | RCUTORTURE_RDR_PREEMPT | \
66 RCUTORTURE_RDR_RBH | RCUTORTURE_RDR_SCHED)
67 #define RCUTORTURE_RDR_MAX_LOOPS 0x7 /* Maximum reader extensions. */
68 /* Must be power of two minus one. */
69 #define RCUTORTURE_RDR_MAX_SEGS (RCUTORTURE_RDR_MAX_LOOPS + 3)
71 torture_param(int, extendables
, RCUTORTURE_MAX_EXTEND
,
72 "Extend readers by disabling bh (1), irqs (2), or preempt (4)");
73 torture_param(int, fqs_duration
, 0,
74 "Duration of fqs bursts (us), 0 to disable");
75 torture_param(int, fqs_holdoff
, 0, "Holdoff time within fqs bursts (us)");
76 torture_param(int, fqs_stutter
, 3, "Wait time between fqs bursts (s)");
77 torture_param(bool, fwd_progress
, 1, "Test grace-period forward progress");
78 torture_param(int, fwd_progress_div
, 4, "Fraction of CPU stall to wait");
79 torture_param(int, fwd_progress_holdoff
, 60,
80 "Time between forward-progress tests (s)");
81 torture_param(bool, fwd_progress_need_resched
, 1,
82 "Hide cond_resched() behind need_resched()");
83 torture_param(bool, gp_cond
, false, "Use conditional/async GP wait primitives");
84 torture_param(bool, gp_exp
, false, "Use expedited GP wait primitives");
85 torture_param(bool, gp_normal
, false,
86 "Use normal (non-expedited) GP wait primitives");
87 torture_param(bool, gp_sync
, false, "Use synchronous GP wait primitives");
88 torture_param(int, irqreader
, 1, "Allow RCU readers from irq handlers");
89 torture_param(int, n_barrier_cbs
, 0,
90 "# of callbacks/kthreads for barrier testing");
91 torture_param(int, nfakewriters
, 4, "Number of RCU fake writer threads");
92 torture_param(int, nreaders
, -1, "Number of RCU reader threads");
93 torture_param(int, object_debug
, 0,
94 "Enable debug-object double call_rcu() testing");
95 torture_param(int, onoff_holdoff
, 0, "Time after boot before CPU hotplugs (s)");
96 torture_param(int, onoff_interval
, 0,
97 "Time between CPU hotplugs (jiffies), 0=disable");
98 torture_param(int, shuffle_interval
, 3, "Number of seconds between shuffles");
99 torture_param(int, shutdown_secs
, 0, "Shutdown time (s), <= zero to disable.");
100 torture_param(int, stall_cpu
, 0, "Stall duration (s), zero to disable.");
101 torture_param(int, stall_cpu_holdoff
, 10,
102 "Time to wait before starting stall (s).");
103 torture_param(int, stall_cpu_irqsoff
, 0, "Disable interrupts while stalling.");
104 torture_param(int, stat_interval
, 60,
105 "Number of seconds between stats printk()s");
106 torture_param(int, stutter
, 5, "Number of seconds to run/halt test");
107 torture_param(int, test_boost
, 1, "Test RCU prio boost: 0=no, 1=maybe, 2=yes.");
108 torture_param(int, test_boost_duration
, 4,
109 "Duration of each boost test, seconds.");
110 torture_param(int, test_boost_interval
, 7,
111 "Interval between boost tests, seconds.");
112 torture_param(bool, test_no_idle_hz
, true,
113 "Test support for tickless idle CPUs");
114 torture_param(int, verbose
, 1,
115 "Enable verbose debugging printk()s");
117 static char *torture_type
= "rcu";
118 module_param(torture_type
, charp
, 0444);
119 MODULE_PARM_DESC(torture_type
, "Type of RCU to torture (rcu, srcu, ...)");
121 static int nrealreaders
;
122 static struct task_struct
*writer_task
;
123 static struct task_struct
**fakewriter_tasks
;
124 static struct task_struct
**reader_tasks
;
125 static struct task_struct
*stats_task
;
126 static struct task_struct
*fqs_task
;
127 static struct task_struct
*boost_tasks
[NR_CPUS
];
128 static struct task_struct
*stall_task
;
129 static struct task_struct
*fwd_prog_task
;
130 static struct task_struct
**barrier_cbs_tasks
;
131 static struct task_struct
*barrier_task
;
133 #define RCU_TORTURE_PIPE_LEN 10
136 struct rcu_head rtort_rcu
;
137 int rtort_pipe_count
;
138 struct list_head rtort_free
;
142 static LIST_HEAD(rcu_torture_freelist
);
143 static struct rcu_torture __rcu
*rcu_torture_current
;
144 static unsigned long rcu_torture_current_version
;
145 static struct rcu_torture rcu_tortures
[10 * RCU_TORTURE_PIPE_LEN
];
146 static DEFINE_SPINLOCK(rcu_torture_lock
);
147 static DEFINE_PER_CPU(long [RCU_TORTURE_PIPE_LEN
+ 1], rcu_torture_count
);
148 static DEFINE_PER_CPU(long [RCU_TORTURE_PIPE_LEN
+ 1], rcu_torture_batch
);
149 static atomic_t rcu_torture_wcount
[RCU_TORTURE_PIPE_LEN
+ 1];
150 static atomic_t n_rcu_torture_alloc
;
151 static atomic_t n_rcu_torture_alloc_fail
;
152 static atomic_t n_rcu_torture_free
;
153 static atomic_t n_rcu_torture_mberror
;
154 static atomic_t n_rcu_torture_error
;
155 static long n_rcu_torture_barrier_error
;
156 static long n_rcu_torture_boost_ktrerror
;
157 static long n_rcu_torture_boost_rterror
;
158 static long n_rcu_torture_boost_failure
;
159 static long n_rcu_torture_boosts
;
160 static atomic_long_t n_rcu_torture_timers
;
161 static long n_barrier_attempts
;
162 static long n_barrier_successes
; /* did rcu_barrier test succeed? */
163 static struct list_head rcu_torture_removed
;
165 static int rcu_torture_writer_state
;
166 #define RTWS_FIXED_DELAY 0
168 #define RTWS_REPLACE 2
169 #define RTWS_DEF_FREE 3
170 #define RTWS_EXP_SYNC 4
171 #define RTWS_COND_GET 5
172 #define RTWS_COND_SYNC 6
174 #define RTWS_STUTTER 8
175 #define RTWS_STOPPING 9
176 static const char * const rcu_torture_writer_state_names
[] = {
189 /* Record reader segment types and duration for first failing read. */
192 unsigned long rt_delay_jiffies
;
193 unsigned long rt_delay_ms
;
194 unsigned long rt_delay_us
;
197 static int err_segs_recorded
;
198 static struct rt_read_seg err_segs
[RCUTORTURE_RDR_MAX_SEGS
];
199 static int rt_read_nsegs
;
201 static const char *rcu_torture_writer_state_getname(void)
203 unsigned int i
= READ_ONCE(rcu_torture_writer_state
);
205 if (i
>= ARRAY_SIZE(rcu_torture_writer_state_names
))
207 return rcu_torture_writer_state_names
[i
];
210 #if defined(CONFIG_RCU_BOOST) && !defined(CONFIG_HOTPLUG_CPU)
211 #define rcu_can_boost() 1
212 #else /* #if defined(CONFIG_RCU_BOOST) && !defined(CONFIG_HOTPLUG_CPU) */
213 #define rcu_can_boost() 0
214 #endif /* #else #if defined(CONFIG_RCU_BOOST) && !defined(CONFIG_HOTPLUG_CPU) */
216 #ifdef CONFIG_RCU_TRACE
217 static u64 notrace
rcu_trace_clock_local(void)
219 u64 ts
= trace_clock_local();
221 (void)do_div(ts
, NSEC_PER_USEC
);
224 #else /* #ifdef CONFIG_RCU_TRACE */
225 static u64 notrace
rcu_trace_clock_local(void)
229 #endif /* #else #ifdef CONFIG_RCU_TRACE */
231 static unsigned long boost_starttime
; /* jiffies of next boost test start. */
232 static DEFINE_MUTEX(boost_mutex
); /* protect setting boost_starttime */
233 /* and boost task create/destroy. */
234 static atomic_t barrier_cbs_count
; /* Barrier callbacks registered. */
235 static bool barrier_phase
; /* Test phase. */
236 static atomic_t barrier_cbs_invoked
; /* Barrier callbacks invoked. */
237 static wait_queue_head_t
*barrier_cbs_wq
; /* Coordinate barrier testing. */
238 static DECLARE_WAIT_QUEUE_HEAD(barrier_wq
);
240 static bool rcu_fwd_cb_nodelay
; /* Short rcu_torture_delay() delays. */
243 * Allocate an element from the rcu_tortures pool.
245 static struct rcu_torture
*
246 rcu_torture_alloc(void)
250 spin_lock_bh(&rcu_torture_lock
);
251 if (list_empty(&rcu_torture_freelist
)) {
252 atomic_inc(&n_rcu_torture_alloc_fail
);
253 spin_unlock_bh(&rcu_torture_lock
);
256 atomic_inc(&n_rcu_torture_alloc
);
257 p
= rcu_torture_freelist
.next
;
259 spin_unlock_bh(&rcu_torture_lock
);
260 return container_of(p
, struct rcu_torture
, rtort_free
);
264 * Free an element to the rcu_tortures pool.
267 rcu_torture_free(struct rcu_torture
*p
)
269 atomic_inc(&n_rcu_torture_free
);
270 spin_lock_bh(&rcu_torture_lock
);
271 list_add_tail(&p
->rtort_free
, &rcu_torture_freelist
);
272 spin_unlock_bh(&rcu_torture_lock
);
276 * Operations vector for selecting different types of tests.
279 struct rcu_torture_ops
{
282 void (*cleanup
)(void);
283 int (*readlock
)(void);
284 void (*read_delay
)(struct torture_random_state
*rrsp
,
285 struct rt_read_seg
*rtrsp
);
286 void (*readunlock
)(int idx
);
287 unsigned long (*get_gp_seq
)(void);
288 unsigned long (*gp_diff
)(unsigned long new, unsigned long old
);
289 void (*deferred_free
)(struct rcu_torture
*p
);
291 void (*exp_sync
)(void);
292 unsigned long (*get_state
)(void);
293 void (*cond_sync
)(unsigned long oldstate
);
294 call_rcu_func_t call
;
295 void (*cb_barrier
)(void);
298 int (*stall_dur
)(void);
306 static struct rcu_torture_ops
*cur_ops
;
309 * Definitions for rcu torture testing.
312 static int rcu_torture_read_lock(void) __acquires(RCU
)
319 rcu_read_delay(struct torture_random_state
*rrsp
, struct rt_read_seg
*rtrsp
)
321 unsigned long started
;
322 unsigned long completed
;
323 const unsigned long shortdelay_us
= 200;
324 unsigned long longdelay_ms
= 300;
325 unsigned long long ts
;
327 /* We want a short delay sometimes to make a reader delay the grace
328 * period, and we want a long delay occasionally to trigger
329 * force_quiescent_state. */
331 if (!rcu_fwd_cb_nodelay
&&
332 !(torture_random(rrsp
) % (nrealreaders
* 2000 * longdelay_ms
))) {
333 started
= cur_ops
->get_gp_seq();
334 ts
= rcu_trace_clock_local();
335 if (preempt_count() & (SOFTIRQ_MASK
| HARDIRQ_MASK
))
336 longdelay_ms
= 5; /* Avoid triggering BH limits. */
337 mdelay(longdelay_ms
);
338 rtrsp
->rt_delay_ms
= longdelay_ms
;
339 completed
= cur_ops
->get_gp_seq();
340 do_trace_rcu_torture_read(cur_ops
->name
, NULL
, ts
,
343 if (!(torture_random(rrsp
) % (nrealreaders
* 2 * shortdelay_us
))) {
344 udelay(shortdelay_us
);
345 rtrsp
->rt_delay_us
= shortdelay_us
;
347 if (!preempt_count() &&
348 !(torture_random(rrsp
) % (nrealreaders
* 500))) {
349 torture_preempt_schedule(); /* QS only if preemptible. */
350 rtrsp
->rt_preempted
= true;
354 static void rcu_torture_read_unlock(int idx
) __releases(RCU
)
360 * Update callback in the pipe. This should be invoked after a grace period.
363 rcu_torture_pipe_update_one(struct rcu_torture
*rp
)
367 i
= rp
->rtort_pipe_count
;
368 if (i
> RCU_TORTURE_PIPE_LEN
)
369 i
= RCU_TORTURE_PIPE_LEN
;
370 atomic_inc(&rcu_torture_wcount
[i
]);
371 if (++rp
->rtort_pipe_count
>= RCU_TORTURE_PIPE_LEN
) {
372 rp
->rtort_mbtest
= 0;
379 * Update all callbacks in the pipe. Suitable for synchronous grace-period
383 rcu_torture_pipe_update(struct rcu_torture
*old_rp
)
385 struct rcu_torture
*rp
;
386 struct rcu_torture
*rp1
;
389 list_add(&old_rp
->rtort_free
, &rcu_torture_removed
);
390 list_for_each_entry_safe(rp
, rp1
, &rcu_torture_removed
, rtort_free
) {
391 if (rcu_torture_pipe_update_one(rp
)) {
392 list_del(&rp
->rtort_free
);
393 rcu_torture_free(rp
);
399 rcu_torture_cb(struct rcu_head
*p
)
401 struct rcu_torture
*rp
= container_of(p
, struct rcu_torture
, rtort_rcu
);
403 if (torture_must_stop_irq()) {
404 /* Test is ending, just drop callbacks on the floor. */
405 /* The next initialization will pick up the pieces. */
408 if (rcu_torture_pipe_update_one(rp
))
409 rcu_torture_free(rp
);
411 cur_ops
->deferred_free(rp
);
414 static unsigned long rcu_no_completed(void)
419 static void rcu_torture_deferred_free(struct rcu_torture
*p
)
421 call_rcu(&p
->rtort_rcu
, rcu_torture_cb
);
424 static void rcu_sync_torture_init(void)
426 INIT_LIST_HEAD(&rcu_torture_removed
);
429 static struct rcu_torture_ops rcu_ops
= {
431 .init
= rcu_sync_torture_init
,
432 .readlock
= rcu_torture_read_lock
,
433 .read_delay
= rcu_read_delay
,
434 .readunlock
= rcu_torture_read_unlock
,
435 .get_gp_seq
= rcu_get_gp_seq
,
436 .gp_diff
= rcu_seq_diff
,
437 .deferred_free
= rcu_torture_deferred_free
,
438 .sync
= synchronize_rcu
,
439 .exp_sync
= synchronize_rcu_expedited
,
440 .get_state
= get_state_synchronize_rcu
,
441 .cond_sync
= cond_synchronize_rcu
,
443 .cb_barrier
= rcu_barrier
,
444 .fqs
= rcu_force_quiescent_state
,
446 .stall_dur
= rcu_jiffies_till_stall_check
,
448 .can_boost
= rcu_can_boost(),
449 .extendables
= RCUTORTURE_MAX_EXTEND
,
454 * Don't even think about trying any of these in real life!!!
455 * The names includes "busted", and they really means it!
456 * The only purpose of these functions is to provide a buggy RCU
457 * implementation to make sure that rcutorture correctly emits
458 * buggy-RCU error messages.
460 static void rcu_busted_torture_deferred_free(struct rcu_torture
*p
)
462 /* This is a deliberate bug for testing purposes only! */
463 rcu_torture_cb(&p
->rtort_rcu
);
466 static void synchronize_rcu_busted(void)
468 /* This is a deliberate bug for testing purposes only! */
472 call_rcu_busted(struct rcu_head
*head
, rcu_callback_t func
)
474 /* This is a deliberate bug for testing purposes only! */
478 static struct rcu_torture_ops rcu_busted_ops
= {
479 .ttype
= INVALID_RCU_FLAVOR
,
480 .init
= rcu_sync_torture_init
,
481 .readlock
= rcu_torture_read_lock
,
482 .read_delay
= rcu_read_delay
, /* just reuse rcu's version. */
483 .readunlock
= rcu_torture_read_unlock
,
484 .get_gp_seq
= rcu_no_completed
,
485 .deferred_free
= rcu_busted_torture_deferred_free
,
486 .sync
= synchronize_rcu_busted
,
487 .exp_sync
= synchronize_rcu_busted
,
488 .call
= call_rcu_busted
,
497 * Definitions for srcu torture testing.
500 DEFINE_STATIC_SRCU(srcu_ctl
);
501 static struct srcu_struct srcu_ctld
;
502 static struct srcu_struct
*srcu_ctlp
= &srcu_ctl
;
504 static int srcu_torture_read_lock(void) __acquires(srcu_ctlp
)
506 return srcu_read_lock(srcu_ctlp
);
510 srcu_read_delay(struct torture_random_state
*rrsp
, struct rt_read_seg
*rtrsp
)
513 const long uspertick
= 1000000 / HZ
;
514 const long longdelay
= 10;
516 /* We want there to be long-running readers, but not all the time. */
518 delay
= torture_random(rrsp
) %
519 (nrealreaders
* 2 * longdelay
* uspertick
);
520 if (!delay
&& in_task()) {
521 schedule_timeout_interruptible(longdelay
);
522 rtrsp
->rt_delay_jiffies
= longdelay
;
524 rcu_read_delay(rrsp
, rtrsp
);
528 static void srcu_torture_read_unlock(int idx
) __releases(srcu_ctlp
)
530 srcu_read_unlock(srcu_ctlp
, idx
);
533 static unsigned long srcu_torture_completed(void)
535 return srcu_batches_completed(srcu_ctlp
);
538 static void srcu_torture_deferred_free(struct rcu_torture
*rp
)
540 call_srcu(srcu_ctlp
, &rp
->rtort_rcu
, rcu_torture_cb
);
543 static void srcu_torture_synchronize(void)
545 synchronize_srcu(srcu_ctlp
);
548 static void srcu_torture_call(struct rcu_head
*head
,
551 call_srcu(srcu_ctlp
, head
, func
);
554 static void srcu_torture_barrier(void)
556 srcu_barrier(srcu_ctlp
);
559 static void srcu_torture_stats(void)
561 srcu_torture_stats_print(srcu_ctlp
, torture_type
, TORTURE_FLAG
);
564 static void srcu_torture_synchronize_expedited(void)
566 synchronize_srcu_expedited(srcu_ctlp
);
569 static struct rcu_torture_ops srcu_ops
= {
570 .ttype
= SRCU_FLAVOR
,
571 .init
= rcu_sync_torture_init
,
572 .readlock
= srcu_torture_read_lock
,
573 .read_delay
= srcu_read_delay
,
574 .readunlock
= srcu_torture_read_unlock
,
575 .get_gp_seq
= srcu_torture_completed
,
576 .deferred_free
= srcu_torture_deferred_free
,
577 .sync
= srcu_torture_synchronize
,
578 .exp_sync
= srcu_torture_synchronize_expedited
,
579 .call
= srcu_torture_call
,
580 .cb_barrier
= srcu_torture_barrier
,
581 .stats
= srcu_torture_stats
,
586 static void srcu_torture_init(void)
588 rcu_sync_torture_init();
589 WARN_ON(init_srcu_struct(&srcu_ctld
));
590 srcu_ctlp
= &srcu_ctld
;
593 static void srcu_torture_cleanup(void)
595 cleanup_srcu_struct(&srcu_ctld
);
596 srcu_ctlp
= &srcu_ctl
; /* In case of a later rcutorture run. */
599 /* As above, but dynamically allocated. */
600 static struct rcu_torture_ops srcud_ops
= {
601 .ttype
= SRCU_FLAVOR
,
602 .init
= srcu_torture_init
,
603 .cleanup
= srcu_torture_cleanup
,
604 .readlock
= srcu_torture_read_lock
,
605 .read_delay
= srcu_read_delay
,
606 .readunlock
= srcu_torture_read_unlock
,
607 .get_gp_seq
= srcu_torture_completed
,
608 .deferred_free
= srcu_torture_deferred_free
,
609 .sync
= srcu_torture_synchronize
,
610 .exp_sync
= srcu_torture_synchronize_expedited
,
611 .call
= srcu_torture_call
,
612 .cb_barrier
= srcu_torture_barrier
,
613 .stats
= srcu_torture_stats
,
618 /* As above, but broken due to inappropriate reader extension. */
619 static struct rcu_torture_ops busted_srcud_ops
= {
620 .ttype
= SRCU_FLAVOR
,
621 .init
= srcu_torture_init
,
622 .cleanup
= srcu_torture_cleanup
,
623 .readlock
= srcu_torture_read_lock
,
624 .read_delay
= rcu_read_delay
,
625 .readunlock
= srcu_torture_read_unlock
,
626 .get_gp_seq
= srcu_torture_completed
,
627 .deferred_free
= srcu_torture_deferred_free
,
628 .sync
= srcu_torture_synchronize
,
629 .exp_sync
= srcu_torture_synchronize_expedited
,
630 .call
= srcu_torture_call
,
631 .cb_barrier
= srcu_torture_barrier
,
632 .stats
= srcu_torture_stats
,
634 .extendables
= RCUTORTURE_MAX_EXTEND
,
635 .name
= "busted_srcud"
639 * Definitions for RCU-tasks torture testing.
642 static int tasks_torture_read_lock(void)
647 static void tasks_torture_read_unlock(int idx
)
651 static void rcu_tasks_torture_deferred_free(struct rcu_torture
*p
)
653 call_rcu_tasks(&p
->rtort_rcu
, rcu_torture_cb
);
656 static struct rcu_torture_ops tasks_ops
= {
657 .ttype
= RCU_TASKS_FLAVOR
,
658 .init
= rcu_sync_torture_init
,
659 .readlock
= tasks_torture_read_lock
,
660 .read_delay
= rcu_read_delay
, /* just reuse rcu's version. */
661 .readunlock
= tasks_torture_read_unlock
,
662 .get_gp_seq
= rcu_no_completed
,
663 .deferred_free
= rcu_tasks_torture_deferred_free
,
664 .sync
= synchronize_rcu_tasks
,
665 .exp_sync
= synchronize_rcu_tasks
,
666 .call
= call_rcu_tasks
,
667 .cb_barrier
= rcu_barrier_tasks
,
676 * Definitions for trivial CONFIG_PREEMPT=n-only torture testing.
677 * This implementation does not necessarily work well with CPU hotplug.
680 static void synchronize_rcu_trivial(void)
684 for_each_online_cpu(cpu
) {
685 rcutorture_sched_setaffinity(current
->pid
, cpumask_of(cpu
));
686 WARN_ON_ONCE(raw_smp_processor_id() != cpu
);
690 static int rcu_torture_read_lock_trivial(void) __acquires(RCU
)
696 static void rcu_torture_read_unlock_trivial(int idx
) __releases(RCU
)
701 static struct rcu_torture_ops trivial_ops
= {
702 .ttype
= RCU_TRIVIAL_FLAVOR
,
703 .init
= rcu_sync_torture_init
,
704 .readlock
= rcu_torture_read_lock_trivial
,
705 .read_delay
= rcu_read_delay
, /* just reuse rcu's version. */
706 .readunlock
= rcu_torture_read_unlock_trivial
,
707 .get_gp_seq
= rcu_no_completed
,
708 .sync
= synchronize_rcu_trivial
,
709 .exp_sync
= synchronize_rcu_trivial
,
716 static unsigned long rcutorture_seq_diff(unsigned long new, unsigned long old
)
718 if (!cur_ops
->gp_diff
)
720 return cur_ops
->gp_diff(new, old
);
723 static bool __maybe_unused
torturing_tasks(void)
725 return cur_ops
== &tasks_ops
;
729 * RCU torture priority-boost testing. Runs one real-time thread per
730 * CPU for moderate bursts, repeatedly registering RCU callbacks and
731 * spinning waiting for them to be invoked. If a given callback takes
732 * too long to be invoked, we assume that priority inversion has occurred.
735 struct rcu_boost_inflight
{
740 static void rcu_torture_boost_cb(struct rcu_head
*head
)
742 struct rcu_boost_inflight
*rbip
=
743 container_of(head
, struct rcu_boost_inflight
, rcu
);
745 /* Ensure RCU-core accesses precede clearing ->inflight */
746 smp_store_release(&rbip
->inflight
, 0);
749 static int old_rt_runtime
= -1;
751 static void rcu_torture_disable_rt_throttle(void)
754 * Disable RT throttling so that rcutorture's boost threads don't get
755 * throttled. Only possible if rcutorture is built-in otherwise the
756 * user should manually do this by setting the sched_rt_period_us and
757 * sched_rt_runtime sysctls.
759 if (!IS_BUILTIN(CONFIG_RCU_TORTURE_TEST
) || old_rt_runtime
!= -1)
762 old_rt_runtime
= sysctl_sched_rt_runtime
;
763 sysctl_sched_rt_runtime
= -1;
766 static void rcu_torture_enable_rt_throttle(void)
768 if (!IS_BUILTIN(CONFIG_RCU_TORTURE_TEST
) || old_rt_runtime
== -1)
771 sysctl_sched_rt_runtime
= old_rt_runtime
;
775 static bool rcu_torture_boost_failed(unsigned long start
, unsigned long end
)
777 if (end
- start
> test_boost_duration
* HZ
- HZ
/ 2) {
778 VERBOSE_TOROUT_STRING("rcu_torture_boost boosting failed");
779 n_rcu_torture_boost_failure
++;
781 return true; /* failed */
784 return false; /* passed */
787 static int rcu_torture_boost(void *arg
)
789 unsigned long call_rcu_time
;
790 unsigned long endtime
;
791 unsigned long oldstarttime
;
792 struct rcu_boost_inflight rbi
= { .inflight
= 0 };
793 struct sched_param sp
;
795 VERBOSE_TOROUT_STRING("rcu_torture_boost started");
797 /* Set real-time priority. */
798 sp
.sched_priority
= 1;
799 if (sched_setscheduler(current
, SCHED_FIFO
, &sp
) < 0) {
800 VERBOSE_TOROUT_STRING("rcu_torture_boost RT prio failed!");
801 n_rcu_torture_boost_rterror
++;
804 init_rcu_head_on_stack(&rbi
.rcu
);
805 /* Each pass through the following loop does one boost-test cycle. */
807 /* Track if the test failed already in this test interval? */
810 /* Increment n_rcu_torture_boosts once per boost-test */
811 while (!kthread_should_stop()) {
812 if (mutex_trylock(&boost_mutex
)) {
813 n_rcu_torture_boosts
++;
814 mutex_unlock(&boost_mutex
);
817 schedule_timeout_uninterruptible(1);
819 if (kthread_should_stop())
822 /* Wait for the next test interval. */
823 oldstarttime
= boost_starttime
;
824 while (ULONG_CMP_LT(jiffies
, oldstarttime
)) {
825 schedule_timeout_interruptible(oldstarttime
- jiffies
);
826 stutter_wait("rcu_torture_boost");
827 if (torture_must_stop())
831 /* Do one boost-test interval. */
832 endtime
= oldstarttime
+ test_boost_duration
* HZ
;
833 call_rcu_time
= jiffies
;
834 while (ULONG_CMP_LT(jiffies
, endtime
)) {
835 /* If we don't have a callback in flight, post one. */
836 if (!smp_load_acquire(&rbi
.inflight
)) {
837 /* RCU core before ->inflight = 1. */
838 smp_store_release(&rbi
.inflight
, 1);
839 call_rcu(&rbi
.rcu
, rcu_torture_boost_cb
);
840 /* Check if the boost test failed */
842 rcu_torture_boost_failed(call_rcu_time
,
844 call_rcu_time
= jiffies
;
846 stutter_wait("rcu_torture_boost");
847 if (torture_must_stop())
852 * If boost never happened, then inflight will always be 1, in
853 * this case the boost check would never happen in the above
854 * loop so do another one here.
856 if (!failed
&& smp_load_acquire(&rbi
.inflight
))
857 rcu_torture_boost_failed(call_rcu_time
, jiffies
);
860 * Set the start time of the next test interval.
861 * Yes, this is vulnerable to long delays, but such
862 * delays simply cause a false negative for the next
863 * interval. Besides, we are running at RT priority,
864 * so delays should be relatively rare.
866 while (oldstarttime
== boost_starttime
&&
867 !kthread_should_stop()) {
868 if (mutex_trylock(&boost_mutex
)) {
869 boost_starttime
= jiffies
+
870 test_boost_interval
* HZ
;
871 mutex_unlock(&boost_mutex
);
874 schedule_timeout_uninterruptible(1);
877 /* Go do the stutter. */
878 checkwait
: stutter_wait("rcu_torture_boost");
879 } while (!torture_must_stop());
881 /* Clean up and exit. */
882 while (!kthread_should_stop() || smp_load_acquire(&rbi
.inflight
)) {
883 torture_shutdown_absorb("rcu_torture_boost");
884 schedule_timeout_uninterruptible(1);
886 destroy_rcu_head_on_stack(&rbi
.rcu
);
887 torture_kthread_stopping("rcu_torture_boost");
892 * RCU torture force-quiescent-state kthread. Repeatedly induces
893 * bursts of calls to force_quiescent_state(), increasing the probability
894 * of occurrence of some important types of race conditions.
897 rcu_torture_fqs(void *arg
)
899 unsigned long fqs_resume_time
;
900 int fqs_burst_remaining
;
902 VERBOSE_TOROUT_STRING("rcu_torture_fqs task started");
904 fqs_resume_time
= jiffies
+ fqs_stutter
* HZ
;
905 while (ULONG_CMP_LT(jiffies
, fqs_resume_time
) &&
906 !kthread_should_stop()) {
907 schedule_timeout_interruptible(1);
909 fqs_burst_remaining
= fqs_duration
;
910 while (fqs_burst_remaining
> 0 &&
911 !kthread_should_stop()) {
914 fqs_burst_remaining
-= fqs_holdoff
;
916 stutter_wait("rcu_torture_fqs");
917 } while (!torture_must_stop());
918 torture_kthread_stopping("rcu_torture_fqs");
923 * RCU torture writer kthread. Repeatedly substitutes a new structure
924 * for that pointed to by rcu_torture_current, freeing the old structure
925 * after a series of grace periods (the "pipeline").
928 rcu_torture_writer(void *arg
)
930 bool can_expedite
= !rcu_gp_is_expedited() && !rcu_gp_is_normal();
932 unsigned long gp_snap
;
933 bool gp_cond1
= gp_cond
, gp_exp1
= gp_exp
, gp_normal1
= gp_normal
;
934 bool gp_sync1
= gp_sync
;
936 struct rcu_torture
*rp
;
937 struct rcu_torture
*old_rp
;
938 static DEFINE_TORTURE_RANDOM(rand
);
939 int synctype
[] = { RTWS_DEF_FREE
, RTWS_EXP_SYNC
,
940 RTWS_COND_GET
, RTWS_SYNC
};
943 VERBOSE_TOROUT_STRING("rcu_torture_writer task started");
945 pr_alert("%s" TORTURE_FLAG
946 " GP expediting controlled from boot/sysfs for %s.\n",
947 torture_type
, cur_ops
->name
);
949 /* Initialize synctype[] array. If none set, take default. */
950 if (!gp_cond1
&& !gp_exp1
&& !gp_normal1
&& !gp_sync1
)
951 gp_cond1
= gp_exp1
= gp_normal1
= gp_sync1
= true;
952 if (gp_cond1
&& cur_ops
->get_state
&& cur_ops
->cond_sync
) {
953 synctype
[nsynctypes
++] = RTWS_COND_GET
;
954 pr_info("%s: Testing conditional GPs.\n", __func__
);
955 } else if (gp_cond
&& (!cur_ops
->get_state
|| !cur_ops
->cond_sync
)) {
956 pr_alert("%s: gp_cond without primitives.\n", __func__
);
958 if (gp_exp1
&& cur_ops
->exp_sync
) {
959 synctype
[nsynctypes
++] = RTWS_EXP_SYNC
;
960 pr_info("%s: Testing expedited GPs.\n", __func__
);
961 } else if (gp_exp
&& !cur_ops
->exp_sync
) {
962 pr_alert("%s: gp_exp without primitives.\n", __func__
);
964 if (gp_normal1
&& cur_ops
->deferred_free
) {
965 synctype
[nsynctypes
++] = RTWS_DEF_FREE
;
966 pr_info("%s: Testing asynchronous GPs.\n", __func__
);
967 } else if (gp_normal
&& !cur_ops
->deferred_free
) {
968 pr_alert("%s: gp_normal without primitives.\n", __func__
);
970 if (gp_sync1
&& cur_ops
->sync
) {
971 synctype
[nsynctypes
++] = RTWS_SYNC
;
972 pr_info("%s: Testing normal GPs.\n", __func__
);
973 } else if (gp_sync
&& !cur_ops
->sync
) {
974 pr_alert("%s: gp_sync without primitives.\n", __func__
);
976 if (WARN_ONCE(nsynctypes
== 0,
977 "rcu_torture_writer: No update-side primitives.\n")) {
979 * No updates primitives, so don't try updating.
980 * The resulting test won't be testing much, hence the
983 rcu_torture_writer_state
= RTWS_STOPPING
;
984 torture_kthread_stopping("rcu_torture_writer");
988 rcu_torture_writer_state
= RTWS_FIXED_DELAY
;
989 schedule_timeout_uninterruptible(1);
990 rp
= rcu_torture_alloc();
993 rp
->rtort_pipe_count
= 0;
994 rcu_torture_writer_state
= RTWS_DELAY
;
995 udelay(torture_random(&rand
) & 0x3ff);
996 rcu_torture_writer_state
= RTWS_REPLACE
;
997 old_rp
= rcu_dereference_check(rcu_torture_current
,
998 current
== writer_task
);
999 rp
->rtort_mbtest
= 1;
1000 rcu_assign_pointer(rcu_torture_current
, rp
);
1001 smp_wmb(); /* Mods to old_rp must follow rcu_assign_pointer() */
1003 i
= old_rp
->rtort_pipe_count
;
1004 if (i
> RCU_TORTURE_PIPE_LEN
)
1005 i
= RCU_TORTURE_PIPE_LEN
;
1006 atomic_inc(&rcu_torture_wcount
[i
]);
1007 old_rp
->rtort_pipe_count
++;
1008 switch (synctype
[torture_random(&rand
) % nsynctypes
]) {
1010 rcu_torture_writer_state
= RTWS_DEF_FREE
;
1011 cur_ops
->deferred_free(old_rp
);
1014 rcu_torture_writer_state
= RTWS_EXP_SYNC
;
1015 cur_ops
->exp_sync();
1016 rcu_torture_pipe_update(old_rp
);
1019 rcu_torture_writer_state
= RTWS_COND_GET
;
1020 gp_snap
= cur_ops
->get_state();
1021 i
= torture_random(&rand
) % 16;
1023 schedule_timeout_interruptible(i
);
1024 udelay(torture_random(&rand
) % 1000);
1025 rcu_torture_writer_state
= RTWS_COND_SYNC
;
1026 cur_ops
->cond_sync(gp_snap
);
1027 rcu_torture_pipe_update(old_rp
);
1030 rcu_torture_writer_state
= RTWS_SYNC
;
1032 rcu_torture_pipe_update(old_rp
);
1039 WRITE_ONCE(rcu_torture_current_version
,
1040 rcu_torture_current_version
+ 1);
1041 /* Cycle through nesting levels of rcu_expedite_gp() calls. */
1043 !(torture_random(&rand
) & 0xff & (!!expediting
- 1))) {
1044 WARN_ON_ONCE(expediting
== 0 && rcu_gp_is_expedited());
1045 if (expediting
>= 0)
1048 rcu_unexpedite_gp();
1049 if (++expediting
> 3)
1050 expediting
= -expediting
;
1051 } else if (!can_expedite
) { /* Disabled during boot, recheck. */
1052 can_expedite
= !rcu_gp_is_expedited() &&
1053 !rcu_gp_is_normal();
1055 rcu_torture_writer_state
= RTWS_STUTTER
;
1056 if (stutter_wait("rcu_torture_writer") &&
1057 !READ_ONCE(rcu_fwd_cb_nodelay
) &&
1058 !cur_ops
->slow_gps
&&
1059 !torture_must_stop())
1060 for (i
= 0; i
< ARRAY_SIZE(rcu_tortures
); i
++)
1061 if (list_empty(&rcu_tortures
[i
].rtort_free
) &&
1062 rcu_access_pointer(rcu_torture_current
) !=
1064 rcu_ftrace_dump(DUMP_ALL
);
1065 WARN(1, "%s: rtort_pipe_count: %d\n", __func__
, rcu_tortures
[i
].rtort_pipe_count
);
1067 } while (!torture_must_stop());
1068 /* Reset expediting back to unexpedited. */
1070 expediting
= -expediting
;
1071 while (can_expedite
&& expediting
++ < 0)
1072 rcu_unexpedite_gp();
1073 WARN_ON_ONCE(can_expedite
&& rcu_gp_is_expedited());
1075 pr_alert("%s" TORTURE_FLAG
1076 " Dynamic grace-period expediting was disabled.\n",
1078 rcu_torture_writer_state
= RTWS_STOPPING
;
1079 torture_kthread_stopping("rcu_torture_writer");
1084 * RCU torture fake writer kthread. Repeatedly calls sync, with a random
1085 * delay between calls.
1088 rcu_torture_fakewriter(void *arg
)
1090 DEFINE_TORTURE_RANDOM(rand
);
1092 VERBOSE_TOROUT_STRING("rcu_torture_fakewriter task started");
1093 set_user_nice(current
, MAX_NICE
);
1096 schedule_timeout_uninterruptible(1 + torture_random(&rand
)%10);
1097 udelay(torture_random(&rand
) & 0x3ff);
1098 if (cur_ops
->cb_barrier
!= NULL
&&
1099 torture_random(&rand
) % (nfakewriters
* 8) == 0) {
1100 cur_ops
->cb_barrier();
1101 } else if (gp_normal
== gp_exp
) {
1102 if (cur_ops
->sync
&& torture_random(&rand
) & 0x80)
1104 else if (cur_ops
->exp_sync
)
1105 cur_ops
->exp_sync();
1106 } else if (gp_normal
&& cur_ops
->sync
) {
1108 } else if (cur_ops
->exp_sync
) {
1109 cur_ops
->exp_sync();
1111 stutter_wait("rcu_torture_fakewriter");
1112 } while (!torture_must_stop());
1114 torture_kthread_stopping("rcu_torture_fakewriter");
1118 static void rcu_torture_timer_cb(struct rcu_head
*rhp
)
1124 * Do one extension of an RCU read-side critical section using the
1125 * current reader state in readstate (set to zero for initial entry
1126 * to extended critical section), set the new state as specified by
1127 * newstate (set to zero for final exit from extended critical section),
1128 * and random-number-generator state in trsp. If this is neither the
1129 * beginning or end of the critical section and if there was actually a
1130 * change, do a ->read_delay().
1132 static void rcutorture_one_extend(int *readstate
, int newstate
,
1133 struct torture_random_state
*trsp
,
1134 struct rt_read_seg
*rtrsp
)
1137 int idxold
= *readstate
;
1138 int statesnew
= ~*readstate
& newstate
;
1139 int statesold
= *readstate
& ~newstate
;
1141 WARN_ON_ONCE(idxold
< 0);
1142 WARN_ON_ONCE((idxold
>> RCUTORTURE_RDR_SHIFT
) > 1);
1143 rtrsp
->rt_readstate
= newstate
;
1145 /* First, put new protection in place to avoid critical-section gap. */
1146 if (statesnew
& RCUTORTURE_RDR_BH
)
1148 if (statesnew
& RCUTORTURE_RDR_IRQ
)
1149 local_irq_disable();
1150 if (statesnew
& RCUTORTURE_RDR_PREEMPT
)
1152 if (statesnew
& RCUTORTURE_RDR_RBH
)
1154 if (statesnew
& RCUTORTURE_RDR_SCHED
)
1155 rcu_read_lock_sched();
1156 if (statesnew
& RCUTORTURE_RDR_RCU
)
1157 idxnew
= cur_ops
->readlock() << RCUTORTURE_RDR_SHIFT
;
1159 /* Next, remove old protection, irq first due to bh conflict. */
1160 if (statesold
& RCUTORTURE_RDR_IRQ
)
1162 if (statesold
& RCUTORTURE_RDR_BH
)
1164 if (statesold
& RCUTORTURE_RDR_PREEMPT
)
1166 if (statesold
& RCUTORTURE_RDR_RBH
)
1167 rcu_read_unlock_bh();
1168 if (statesold
& RCUTORTURE_RDR_SCHED
)
1169 rcu_read_unlock_sched();
1170 if (statesold
& RCUTORTURE_RDR_RCU
)
1171 cur_ops
->readunlock(idxold
>> RCUTORTURE_RDR_SHIFT
);
1173 /* Delay if neither beginning nor end and there was a change. */
1174 if ((statesnew
|| statesold
) && *readstate
&& newstate
)
1175 cur_ops
->read_delay(trsp
, rtrsp
);
1177 /* Update the reader state. */
1179 idxnew
= idxold
& ~RCUTORTURE_RDR_MASK
;
1180 WARN_ON_ONCE(idxnew
< 0);
1181 WARN_ON_ONCE((idxnew
>> RCUTORTURE_RDR_SHIFT
) > 1);
1182 *readstate
= idxnew
| newstate
;
1183 WARN_ON_ONCE((*readstate
>> RCUTORTURE_RDR_SHIFT
) < 0);
1184 WARN_ON_ONCE((*readstate
>> RCUTORTURE_RDR_SHIFT
) > 1);
1187 /* Return the biggest extendables mask given current RCU and boot parameters. */
1188 static int rcutorture_extend_mask_max(void)
1192 WARN_ON_ONCE(extendables
& ~RCUTORTURE_MAX_EXTEND
);
1193 mask
= extendables
& RCUTORTURE_MAX_EXTEND
& cur_ops
->extendables
;
1194 mask
= mask
| RCUTORTURE_RDR_RCU
;
1198 /* Return a random protection state mask, but with at least one bit set. */
1200 rcutorture_extend_mask(int oldmask
, struct torture_random_state
*trsp
)
1202 int mask
= rcutorture_extend_mask_max();
1203 unsigned long randmask1
= torture_random(trsp
) >> 8;
1204 unsigned long randmask2
= randmask1
>> 3;
1206 WARN_ON_ONCE(mask
>> RCUTORTURE_RDR_SHIFT
);
1207 /* Mostly only one bit (need preemption!), sometimes lots of bits. */
1208 if (!(randmask1
& 0x7))
1209 mask
= mask
& randmask2
;
1211 mask
= mask
& (1 << (randmask2
% RCUTORTURE_RDR_NBITS
));
1212 /* Can't enable bh w/irq disabled. */
1213 if ((mask
& RCUTORTURE_RDR_IRQ
) &&
1214 ((!(mask
& RCUTORTURE_RDR_BH
) && (oldmask
& RCUTORTURE_RDR_BH
)) ||
1215 (!(mask
& RCUTORTURE_RDR_RBH
) && (oldmask
& RCUTORTURE_RDR_RBH
))))
1216 mask
|= RCUTORTURE_RDR_BH
| RCUTORTURE_RDR_RBH
;
1217 return mask
?: RCUTORTURE_RDR_RCU
;
1221 * Do a randomly selected number of extensions of an existing RCU read-side
1224 static struct rt_read_seg
*
1225 rcutorture_loop_extend(int *readstate
, struct torture_random_state
*trsp
,
1226 struct rt_read_seg
*rtrsp
)
1230 int mask
= rcutorture_extend_mask_max();
1232 WARN_ON_ONCE(!*readstate
); /* -Existing- RCU read-side critsect! */
1233 if (!((mask
- 1) & mask
))
1234 return rtrsp
; /* Current RCU reader not extendable. */
1235 /* Bias towards larger numbers of loops. */
1236 i
= (torture_random(trsp
) >> 3);
1237 i
= ((i
| (i
>> 3)) & RCUTORTURE_RDR_MAX_LOOPS
) + 1;
1238 for (j
= 0; j
< i
; j
++) {
1239 mask
= rcutorture_extend_mask(*readstate
, trsp
);
1240 rcutorture_one_extend(readstate
, mask
, trsp
, &rtrsp
[j
]);
1246 * Do one read-side critical section, returning false if there was
1247 * no data to read. Can be invoked both from process context and
1248 * from a timer handler.
1250 static bool rcu_torture_one_read(struct torture_random_state
*trsp
)
1253 unsigned long started
;
1254 unsigned long completed
;
1256 struct rcu_torture
*p
;
1259 struct rt_read_seg rtseg
[RCUTORTURE_RDR_MAX_SEGS
] = { { 0 } };
1260 struct rt_read_seg
*rtrsp
= &rtseg
[0];
1261 struct rt_read_seg
*rtrsp1
;
1262 unsigned long long ts
;
1264 newstate
= rcutorture_extend_mask(readstate
, trsp
);
1265 rcutorture_one_extend(&readstate
, newstate
, trsp
, rtrsp
++);
1266 started
= cur_ops
->get_gp_seq();
1267 ts
= rcu_trace_clock_local();
1268 p
= rcu_dereference_check(rcu_torture_current
,
1269 rcu_read_lock_bh_held() ||
1270 rcu_read_lock_sched_held() ||
1271 srcu_read_lock_held(srcu_ctlp
) ||
1274 /* Wait for rcu_torture_writer to get underway */
1275 rcutorture_one_extend(&readstate
, 0, trsp
, rtrsp
);
1278 if (p
->rtort_mbtest
== 0)
1279 atomic_inc(&n_rcu_torture_mberror
);
1280 rtrsp
= rcutorture_loop_extend(&readstate
, trsp
, rtrsp
);
1282 pipe_count
= p
->rtort_pipe_count
;
1283 if (pipe_count
> RCU_TORTURE_PIPE_LEN
) {
1284 /* Should not happen, but... */
1285 pipe_count
= RCU_TORTURE_PIPE_LEN
;
1287 completed
= cur_ops
->get_gp_seq();
1288 if (pipe_count
> 1) {
1289 do_trace_rcu_torture_read(cur_ops
->name
, &p
->rtort_rcu
,
1290 ts
, started
, completed
);
1291 rcu_ftrace_dump(DUMP_ALL
);
1293 __this_cpu_inc(rcu_torture_count
[pipe_count
]);
1294 completed
= rcutorture_seq_diff(completed
, started
);
1295 if (completed
> RCU_TORTURE_PIPE_LEN
) {
1296 /* Should not happen, but... */
1297 completed
= RCU_TORTURE_PIPE_LEN
;
1299 __this_cpu_inc(rcu_torture_batch
[completed
]);
1301 rcutorture_one_extend(&readstate
, 0, trsp
, rtrsp
);
1302 WARN_ON_ONCE(readstate
& RCUTORTURE_RDR_MASK
);
1304 /* If error or close call, record the sequence of reader protections. */
1305 if ((pipe_count
> 1 || completed
> 1) && !xchg(&err_segs_recorded
, 1)) {
1307 for (rtrsp1
= &rtseg
[0]; rtrsp1
< rtrsp
; rtrsp1
++)
1308 err_segs
[i
++] = *rtrsp1
;
1315 static DEFINE_TORTURE_RANDOM_PERCPU(rcu_torture_timer_rand
);
1318 * RCU torture reader from timer handler. Dereferences rcu_torture_current,
1319 * incrementing the corresponding element of the pipeline array. The
1320 * counter in the element should never be greater than 1, otherwise, the
1321 * RCU implementation is broken.
1323 static void rcu_torture_timer(struct timer_list
*unused
)
1325 atomic_long_inc(&n_rcu_torture_timers
);
1326 (void)rcu_torture_one_read(this_cpu_ptr(&rcu_torture_timer_rand
));
1328 /* Test call_rcu() invocation from interrupt handler. */
1329 if (cur_ops
->call
) {
1330 struct rcu_head
*rhp
= kmalloc(sizeof(*rhp
), GFP_NOWAIT
);
1333 cur_ops
->call(rhp
, rcu_torture_timer_cb
);
1338 * RCU torture reader kthread. Repeatedly dereferences rcu_torture_current,
1339 * incrementing the corresponding element of the pipeline array. The
1340 * counter in the element should never be greater than 1, otherwise, the
1341 * RCU implementation is broken.
1344 rcu_torture_reader(void *arg
)
1346 unsigned long lastsleep
= jiffies
;
1347 long myid
= (long)arg
;
1348 int mynumonline
= myid
;
1349 DEFINE_TORTURE_RANDOM(rand
);
1350 struct timer_list t
;
1352 VERBOSE_TOROUT_STRING("rcu_torture_reader task started");
1353 set_user_nice(current
, MAX_NICE
);
1354 if (irqreader
&& cur_ops
->irq_capable
)
1355 timer_setup_on_stack(&t
, rcu_torture_timer
, 0);
1358 if (irqreader
&& cur_ops
->irq_capable
) {
1359 if (!timer_pending(&t
))
1360 mod_timer(&t
, jiffies
+ 1);
1362 if (!rcu_torture_one_read(&rand
))
1363 schedule_timeout_interruptible(HZ
);
1364 if (time_after(jiffies
, lastsleep
)) {
1365 schedule_timeout_interruptible(1);
1366 lastsleep
= jiffies
+ 10;
1368 while (num_online_cpus() < mynumonline
&& !torture_must_stop())
1369 schedule_timeout_interruptible(HZ
/ 5);
1370 stutter_wait("rcu_torture_reader");
1371 } while (!torture_must_stop());
1372 if (irqreader
&& cur_ops
->irq_capable
) {
1374 destroy_timer_on_stack(&t
);
1376 torture_kthread_stopping("rcu_torture_reader");
1381 * Print torture statistics. Caller must ensure that there is only
1382 * one call to this function at a given time!!! This is normally
1383 * accomplished by relying on the module system to only have one copy
1384 * of the module loaded, and then by giving the rcu_torture_stats
1385 * kthread full control (or the init/cleanup functions when rcu_torture_stats
1386 * thread is not running).
1389 rcu_torture_stats_print(void)
1393 long pipesummary
[RCU_TORTURE_PIPE_LEN
+ 1] = { 0 };
1394 long batchsummary
[RCU_TORTURE_PIPE_LEN
+ 1] = { 0 };
1395 static unsigned long rtcv_snap
= ULONG_MAX
;
1396 static bool splatted
;
1397 struct task_struct
*wtp
;
1399 for_each_possible_cpu(cpu
) {
1400 for (i
= 0; i
< RCU_TORTURE_PIPE_LEN
+ 1; i
++) {
1401 pipesummary
[i
] += per_cpu(rcu_torture_count
, cpu
)[i
];
1402 batchsummary
[i
] += per_cpu(rcu_torture_batch
, cpu
)[i
];
1405 for (i
= RCU_TORTURE_PIPE_LEN
- 1; i
>= 0; i
--) {
1406 if (pipesummary
[i
] != 0)
1410 pr_alert("%s%s ", torture_type
, TORTURE_FLAG
);
1411 pr_cont("rtc: %p %s: %lu tfle: %d rta: %d rtaf: %d rtf: %d ",
1412 rcu_torture_current
,
1413 rcu_torture_current
? "ver" : "VER",
1414 rcu_torture_current_version
,
1415 list_empty(&rcu_torture_freelist
),
1416 atomic_read(&n_rcu_torture_alloc
),
1417 atomic_read(&n_rcu_torture_alloc_fail
),
1418 atomic_read(&n_rcu_torture_free
));
1419 pr_cont("rtmbe: %d rtbe: %ld rtbke: %ld rtbre: %ld ",
1420 atomic_read(&n_rcu_torture_mberror
),
1421 n_rcu_torture_barrier_error
,
1422 n_rcu_torture_boost_ktrerror
,
1423 n_rcu_torture_boost_rterror
);
1424 pr_cont("rtbf: %ld rtb: %ld nt: %ld ",
1425 n_rcu_torture_boost_failure
,
1426 n_rcu_torture_boosts
,
1427 atomic_long_read(&n_rcu_torture_timers
));
1428 torture_onoff_stats();
1429 pr_cont("barrier: %ld/%ld:%ld\n",
1430 n_barrier_successes
,
1432 n_rcu_torture_barrier_error
);
1434 pr_alert("%s%s ", torture_type
, TORTURE_FLAG
);
1435 if (atomic_read(&n_rcu_torture_mberror
) != 0 ||
1436 n_rcu_torture_barrier_error
!= 0 ||
1437 n_rcu_torture_boost_ktrerror
!= 0 ||
1438 n_rcu_torture_boost_rterror
!= 0 ||
1439 n_rcu_torture_boost_failure
!= 0 ||
1441 pr_cont("%s", "!!! ");
1442 atomic_inc(&n_rcu_torture_error
);
1445 pr_cont("Reader Pipe: ");
1446 for (i
= 0; i
< RCU_TORTURE_PIPE_LEN
+ 1; i
++)
1447 pr_cont(" %ld", pipesummary
[i
]);
1450 pr_alert("%s%s ", torture_type
, TORTURE_FLAG
);
1451 pr_cont("Reader Batch: ");
1452 for (i
= 0; i
< RCU_TORTURE_PIPE_LEN
+ 1; i
++)
1453 pr_cont(" %ld", batchsummary
[i
]);
1456 pr_alert("%s%s ", torture_type
, TORTURE_FLAG
);
1457 pr_cont("Free-Block Circulation: ");
1458 for (i
= 0; i
< RCU_TORTURE_PIPE_LEN
+ 1; i
++) {
1459 pr_cont(" %d", atomic_read(&rcu_torture_wcount
[i
]));
1465 if (rtcv_snap
== rcu_torture_current_version
&&
1466 rcu_torture_current
!= NULL
) {
1467 int __maybe_unused flags
= 0;
1468 unsigned long __maybe_unused gp_seq
= 0;
1470 rcutorture_get_gp_data(cur_ops
->ttype
,
1472 srcutorture_get_gp_data(cur_ops
->ttype
, srcu_ctlp
,
1474 wtp
= READ_ONCE(writer_task
);
1475 pr_alert("??? Writer stall state %s(%d) g%lu f%#x ->state %#lx cpu %d\n",
1476 rcu_torture_writer_state_getname(),
1477 rcu_torture_writer_state
, gp_seq
, flags
,
1478 wtp
== NULL
? ~0UL : wtp
->state
,
1479 wtp
== NULL
? -1 : (int)task_cpu(wtp
));
1480 if (!splatted
&& wtp
) {
1481 sched_show_task(wtp
);
1484 show_rcu_gp_kthreads();
1485 rcu_ftrace_dump(DUMP_ALL
);
1487 rtcv_snap
= rcu_torture_current_version
;
1491 * Periodically prints torture statistics, if periodic statistics printing
1492 * was specified via the stat_interval module parameter.
1495 rcu_torture_stats(void *arg
)
1497 VERBOSE_TOROUT_STRING("rcu_torture_stats task started");
1499 schedule_timeout_interruptible(stat_interval
* HZ
);
1500 rcu_torture_stats_print();
1501 torture_shutdown_absorb("rcu_torture_stats");
1502 } while (!torture_must_stop());
1503 torture_kthread_stopping("rcu_torture_stats");
1508 rcu_torture_print_module_parms(struct rcu_torture_ops
*cur_ops
, const char *tag
)
1510 pr_alert("%s" TORTURE_FLAG
1511 "--- %s: nreaders=%d nfakewriters=%d "
1512 "stat_interval=%d verbose=%d test_no_idle_hz=%d "
1513 "shuffle_interval=%d stutter=%d irqreader=%d "
1514 "fqs_duration=%d fqs_holdoff=%d fqs_stutter=%d "
1515 "test_boost=%d/%d test_boost_interval=%d "
1516 "test_boost_duration=%d shutdown_secs=%d "
1517 "stall_cpu=%d stall_cpu_holdoff=%d stall_cpu_irqsoff=%d "
1519 "onoff_interval=%d onoff_holdoff=%d\n",
1520 torture_type
, tag
, nrealreaders
, nfakewriters
,
1521 stat_interval
, verbose
, test_no_idle_hz
, shuffle_interval
,
1522 stutter
, irqreader
, fqs_duration
, fqs_holdoff
, fqs_stutter
,
1523 test_boost
, cur_ops
->can_boost
,
1524 test_boost_interval
, test_boost_duration
, shutdown_secs
,
1525 stall_cpu
, stall_cpu_holdoff
, stall_cpu_irqsoff
,
1527 onoff_interval
, onoff_holdoff
);
1530 static int rcutorture_booster_cleanup(unsigned int cpu
)
1532 struct task_struct
*t
;
1534 if (boost_tasks
[cpu
] == NULL
)
1536 mutex_lock(&boost_mutex
);
1537 t
= boost_tasks
[cpu
];
1538 boost_tasks
[cpu
] = NULL
;
1539 rcu_torture_enable_rt_throttle();
1540 mutex_unlock(&boost_mutex
);
1542 /* This must be outside of the mutex, otherwise deadlock! */
1543 torture_stop_kthread(rcu_torture_boost
, t
);
1547 static int rcutorture_booster_init(unsigned int cpu
)
1551 if (boost_tasks
[cpu
] != NULL
)
1552 return 0; /* Already created, nothing more to do. */
1554 /* Don't allow time recalculation while creating a new task. */
1555 mutex_lock(&boost_mutex
);
1556 rcu_torture_disable_rt_throttle();
1557 VERBOSE_TOROUT_STRING("Creating rcu_torture_boost task");
1558 boost_tasks
[cpu
] = kthread_create_on_node(rcu_torture_boost
, NULL
,
1560 "rcu_torture_boost");
1561 if (IS_ERR(boost_tasks
[cpu
])) {
1562 retval
= PTR_ERR(boost_tasks
[cpu
]);
1563 VERBOSE_TOROUT_STRING("rcu_torture_boost task create failed");
1564 n_rcu_torture_boost_ktrerror
++;
1565 boost_tasks
[cpu
] = NULL
;
1566 mutex_unlock(&boost_mutex
);
1569 kthread_bind(boost_tasks
[cpu
], cpu
);
1570 wake_up_process(boost_tasks
[cpu
]);
1571 mutex_unlock(&boost_mutex
);
1576 * CPU-stall kthread. It waits as specified by stall_cpu_holdoff, then
1577 * induces a CPU stall for the time specified by stall_cpu.
1579 static int rcu_torture_stall(void *args
)
1581 unsigned long stop_at
;
1583 VERBOSE_TOROUT_STRING("rcu_torture_stall task started");
1584 if (stall_cpu_holdoff
> 0) {
1585 VERBOSE_TOROUT_STRING("rcu_torture_stall begin holdoff");
1586 schedule_timeout_interruptible(stall_cpu_holdoff
* HZ
);
1587 VERBOSE_TOROUT_STRING("rcu_torture_stall end holdoff");
1589 if (!kthread_should_stop()) {
1590 stop_at
= ktime_get_seconds() + stall_cpu
;
1591 /* RCU CPU stall is expected behavior in following code. */
1593 if (stall_cpu_irqsoff
)
1594 local_irq_disable();
1597 pr_alert("rcu_torture_stall start on CPU %d.\n",
1598 smp_processor_id());
1599 while (ULONG_CMP_LT((unsigned long)ktime_get_seconds(),
1601 continue; /* Induce RCU CPU stall warning. */
1602 if (stall_cpu_irqsoff
)
1607 pr_alert("rcu_torture_stall end.\n");
1609 torture_shutdown_absorb("rcu_torture_stall");
1610 while (!kthread_should_stop())
1611 schedule_timeout_interruptible(10 * HZ
);
1615 /* Spawn CPU-stall kthread, if stall_cpu specified. */
1616 static int __init
rcu_torture_stall_init(void)
1620 return torture_create_kthread(rcu_torture_stall
, NULL
, stall_task
);
1623 /* State structure for forward-progress self-propagating RCU callback. */
1624 struct fwd_cb_state
{
1630 * Forward-progress self-propagating RCU callback function. Because
1631 * callbacks run from softirq, this function is an implicit RCU read-side
1634 static void rcu_torture_fwd_prog_cb(struct rcu_head
*rhp
)
1636 struct fwd_cb_state
*fcsp
= container_of(rhp
, struct fwd_cb_state
, rh
);
1638 if (READ_ONCE(fcsp
->stop
)) {
1639 WRITE_ONCE(fcsp
->stop
, 2);
1642 cur_ops
->call(&fcsp
->rh
, rcu_torture_fwd_prog_cb
);
1645 /* State for continuous-flood RCU callbacks. */
1648 struct rcu_fwd_cb
*rfc_next
;
1651 static DEFINE_SPINLOCK(rcu_fwd_lock
);
1652 static struct rcu_fwd_cb
*rcu_fwd_cb_head
;
1653 static struct rcu_fwd_cb
**rcu_fwd_cb_tail
= &rcu_fwd_cb_head
;
1654 static long n_launders_cb
;
1655 static unsigned long rcu_fwd_startat
;
1656 static bool rcu_fwd_emergency_stop
;
1657 #define MAX_FWD_CB_JIFFIES (8 * HZ) /* Maximum CB test duration. */
1658 #define MIN_FWD_CB_LAUNDERS 3 /* This many CB invocations to count. */
1659 #define MIN_FWD_CBS_LAUNDERED 100 /* Number of counted CBs. */
1660 #define FWD_CBS_HIST_DIV 10 /* Histogram buckets/second. */
1661 struct rcu_launder_hist
{
1663 unsigned long launder_gp_seq
;
1665 #define N_LAUNDERS_HIST (2 * MAX_FWD_CB_JIFFIES / (HZ / FWD_CBS_HIST_DIV))
1666 static struct rcu_launder_hist n_launders_hist
[N_LAUNDERS_HIST
];
1667 static unsigned long rcu_launder_gp_seq_start
;
1669 static void rcu_torture_fwd_cb_hist(void)
1672 unsigned long gps_old
;
1676 for (i
= ARRAY_SIZE(n_launders_hist
) - 1; i
> 0; i
--)
1677 if (n_launders_hist
[i
].n_launders
> 0)
1679 pr_alert("%s: Callback-invocation histogram (duration %lu jiffies):",
1680 __func__
, jiffies
- rcu_fwd_startat
);
1681 gps_old
= rcu_launder_gp_seq_start
;
1682 for (j
= 0; j
<= i
; j
++) {
1683 gps
= n_launders_hist
[j
].launder_gp_seq
;
1684 pr_cont(" %ds/%d: %ld:%ld",
1685 j
+ 1, FWD_CBS_HIST_DIV
, n_launders_hist
[j
].n_launders
,
1686 rcutorture_seq_diff(gps
, gps_old
));
1692 /* Callback function for continuous-flood RCU callbacks. */
1693 static void rcu_torture_fwd_cb_cr(struct rcu_head
*rhp
)
1695 unsigned long flags
;
1697 struct rcu_fwd_cb
*rfcp
= container_of(rhp
, struct rcu_fwd_cb
, rh
);
1698 struct rcu_fwd_cb
**rfcpp
;
1700 rfcp
->rfc_next
= NULL
;
1702 spin_lock_irqsave(&rcu_fwd_lock
, flags
);
1703 rfcpp
= rcu_fwd_cb_tail
;
1704 rcu_fwd_cb_tail
= &rfcp
->rfc_next
;
1705 WRITE_ONCE(*rfcpp
, rfcp
);
1706 WRITE_ONCE(n_launders_cb
, n_launders_cb
+ 1);
1707 i
= ((jiffies
- rcu_fwd_startat
) / (HZ
/ FWD_CBS_HIST_DIV
));
1708 if (i
>= ARRAY_SIZE(n_launders_hist
))
1709 i
= ARRAY_SIZE(n_launders_hist
) - 1;
1710 n_launders_hist
[i
].n_launders
++;
1711 n_launders_hist
[i
].launder_gp_seq
= cur_ops
->get_gp_seq();
1712 spin_unlock_irqrestore(&rcu_fwd_lock
, flags
);
1715 // Give the scheduler a chance, even on nohz_full CPUs.
1716 static void rcu_torture_fwd_prog_cond_resched(void)
1718 if (IS_ENABLED(CONFIG_PREEMPT
) && IS_ENABLED(CONFIG_NO_HZ_FULL
)) {
1727 * Free all callbacks on the rcu_fwd_cb_head list, either because the
1728 * test is over or because we hit an OOM event.
1730 static unsigned long rcu_torture_fwd_prog_cbfree(void)
1732 unsigned long flags
;
1733 unsigned long freed
= 0;
1734 struct rcu_fwd_cb
*rfcp
;
1737 spin_lock_irqsave(&rcu_fwd_lock
, flags
);
1738 rfcp
= rcu_fwd_cb_head
;
1740 spin_unlock_irqrestore(&rcu_fwd_lock
, flags
);
1743 rcu_fwd_cb_head
= rfcp
->rfc_next
;
1744 if (!rcu_fwd_cb_head
)
1745 rcu_fwd_cb_tail
= &rcu_fwd_cb_head
;
1746 spin_unlock_irqrestore(&rcu_fwd_lock
, flags
);
1749 rcu_torture_fwd_prog_cond_resched();
1754 /* Carry out need_resched()/cond_resched() forward-progress testing. */
1755 static void rcu_torture_fwd_prog_nr(int *tested
, int *tested_tries
)
1759 struct fwd_cb_state fcs
;
1764 bool selfpropcb
= false;
1765 unsigned long stopat
;
1766 static DEFINE_TORTURE_RANDOM(trs
);
1768 if (cur_ops
->call
&& cur_ops
->sync
&& cur_ops
->cb_barrier
) {
1769 init_rcu_head_on_stack(&fcs
.rh
);
1773 /* Tight loop containing cond_resched(). */
1774 WRITE_ONCE(rcu_fwd_cb_nodelay
, true);
1775 cur_ops
->sync(); /* Later readers see above write. */
1777 WRITE_ONCE(fcs
.stop
, 0);
1778 cur_ops
->call(&fcs
.rh
, rcu_torture_fwd_prog_cb
);
1780 cver
= READ_ONCE(rcu_torture_current_version
);
1781 gps
= cur_ops
->get_gp_seq();
1782 sd
= cur_ops
->stall_dur() + 1;
1783 sd4
= (sd
+ fwd_progress_div
- 1) / fwd_progress_div
;
1784 dur
= sd4
+ torture_random(&trs
) % (sd
- sd4
);
1785 WRITE_ONCE(rcu_fwd_startat
, jiffies
);
1786 stopat
= rcu_fwd_startat
+ dur
;
1787 while (time_before(jiffies
, stopat
) &&
1788 !READ_ONCE(rcu_fwd_emergency_stop
) && !torture_must_stop()) {
1789 idx
= cur_ops
->readlock();
1791 cur_ops
->readunlock(idx
);
1792 if (!fwd_progress_need_resched
|| need_resched())
1793 rcu_torture_fwd_prog_cond_resched();
1796 if (!time_before(jiffies
, stopat
) &&
1797 !READ_ONCE(rcu_fwd_emergency_stop
) && !torture_must_stop()) {
1799 cver
= READ_ONCE(rcu_torture_current_version
) - cver
;
1800 gps
= rcutorture_seq_diff(cur_ops
->get_gp_seq(), gps
);
1801 WARN_ON(!cver
&& gps
< 2);
1802 pr_alert("%s: Duration %ld cver %ld gps %ld\n", __func__
, dur
, cver
, gps
);
1805 WRITE_ONCE(fcs
.stop
, 1);
1806 cur_ops
->sync(); /* Wait for running CB to complete. */
1807 cur_ops
->cb_barrier(); /* Wait for queued callbacks. */
1811 WARN_ON(READ_ONCE(fcs
.stop
) != 2);
1812 destroy_rcu_head_on_stack(&fcs
.rh
);
1814 schedule_timeout_uninterruptible(HZ
/ 10); /* Let kthreads recover. */
1815 WRITE_ONCE(rcu_fwd_cb_nodelay
, false);
1818 /* Carry out call_rcu() forward-progress testing. */
1819 static void rcu_torture_fwd_prog_cr(void)
1825 long n_launders_cb_snap
;
1829 struct rcu_fwd_cb
*rfcp
;
1830 struct rcu_fwd_cb
*rfcpn
;
1831 unsigned long stopat
;
1832 unsigned long stoppedat
;
1834 if (READ_ONCE(rcu_fwd_emergency_stop
))
1835 return; /* Get out of the way quickly, no GP wait! */
1837 return; /* Can't do call_rcu() fwd prog without ->call. */
1839 /* Loop continuously posting RCU callbacks. */
1840 WRITE_ONCE(rcu_fwd_cb_nodelay
, true);
1841 cur_ops
->sync(); /* Later readers see above write. */
1842 WRITE_ONCE(rcu_fwd_startat
, jiffies
);
1843 stopat
= rcu_fwd_startat
+ MAX_FWD_CB_JIFFIES
;
1849 for (i
= 0; i
< ARRAY_SIZE(n_launders_hist
); i
++)
1850 n_launders_hist
[i
].n_launders
= 0;
1851 cver
= READ_ONCE(rcu_torture_current_version
);
1852 gps
= cur_ops
->get_gp_seq();
1853 rcu_launder_gp_seq_start
= gps
;
1854 while (time_before(jiffies
, stopat
) &&
1855 !READ_ONCE(rcu_fwd_emergency_stop
) && !torture_must_stop()) {
1856 rfcp
= READ_ONCE(rcu_fwd_cb_head
);
1859 rfcpn
= READ_ONCE(rfcp
->rfc_next
);
1861 if (rfcp
->rfc_gps
>= MIN_FWD_CB_LAUNDERS
&&
1862 ++n_max_gps
>= MIN_FWD_CBS_LAUNDERED
)
1864 rcu_fwd_cb_head
= rfcpn
;
1868 rfcp
= kmalloc(sizeof(*rfcp
), GFP_KERNEL
);
1869 if (WARN_ON_ONCE(!rfcp
)) {
1870 schedule_timeout_interruptible(1);
1877 cur_ops
->call(&rfcp
->rh
, rcu_torture_fwd_cb_cr
);
1878 rcu_torture_fwd_prog_cond_resched();
1880 stoppedat
= jiffies
;
1881 n_launders_cb_snap
= READ_ONCE(n_launders_cb
);
1882 cver
= READ_ONCE(rcu_torture_current_version
) - cver
;
1883 gps
= rcutorture_seq_diff(cur_ops
->get_gp_seq(), gps
);
1884 cur_ops
->cb_barrier(); /* Wait for callbacks to be invoked. */
1885 (void)rcu_torture_fwd_prog_cbfree();
1887 if (!torture_must_stop() && !READ_ONCE(rcu_fwd_emergency_stop
)) {
1888 WARN_ON(n_max_gps
< MIN_FWD_CBS_LAUNDERED
);
1889 pr_alert("%s Duration %lu barrier: %lu pending %ld n_launders: %ld n_launders_sa: %ld n_max_gps: %ld n_max_cbs: %ld cver %ld gps %ld\n",
1891 stoppedat
- rcu_fwd_startat
, jiffies
- stoppedat
,
1892 n_launders
+ n_max_cbs
- n_launders_cb_snap
,
1893 n_launders
, n_launders_sa
,
1894 n_max_gps
, n_max_cbs
, cver
, gps
);
1895 rcu_torture_fwd_cb_hist();
1897 schedule_timeout_uninterruptible(HZ
); /* Let CBs drain. */
1898 WRITE_ONCE(rcu_fwd_cb_nodelay
, false);
1903 * OOM notifier, but this only prints diagnostic information for the
1904 * current forward-progress test.
1906 static int rcutorture_oom_notify(struct notifier_block
*self
,
1907 unsigned long notused
, void *nfreed
)
1909 WARN(1, "%s invoked upon OOM during forward-progress testing.\n",
1911 rcu_torture_fwd_cb_hist();
1912 rcu_fwd_progress_check(1 + (jiffies
- READ_ONCE(rcu_fwd_startat
)) / 2);
1913 WRITE_ONCE(rcu_fwd_emergency_stop
, true);
1914 smp_mb(); /* Emergency stop before free and wait to avoid hangs. */
1915 pr_info("%s: Freed %lu RCU callbacks.\n",
1916 __func__
, rcu_torture_fwd_prog_cbfree());
1918 pr_info("%s: Freed %lu RCU callbacks.\n",
1919 __func__
, rcu_torture_fwd_prog_cbfree());
1921 pr_info("%s: Freed %lu RCU callbacks.\n",
1922 __func__
, rcu_torture_fwd_prog_cbfree());
1923 smp_mb(); /* Frees before return to avoid redoing OOM. */
1924 (*(unsigned long *)nfreed
)++; /* Forward progress CBs freed! */
1925 pr_info("%s returning after OOM processing.\n", __func__
);
1929 static struct notifier_block rcutorture_oom_nb
= {
1930 .notifier_call
= rcutorture_oom_notify
1933 /* Carry out grace-period forward-progress testing. */
1934 static int rcu_torture_fwd_prog(void *args
)
1937 int tested_tries
= 0;
1939 VERBOSE_TOROUT_STRING("rcu_torture_fwd_progress task started");
1940 rcu_bind_current_to_nocb();
1941 if (!IS_ENABLED(CONFIG_SMP
) || !IS_ENABLED(CONFIG_RCU_BOOST
))
1942 set_user_nice(current
, MAX_NICE
);
1944 schedule_timeout_interruptible(fwd_progress_holdoff
* HZ
);
1945 WRITE_ONCE(rcu_fwd_emergency_stop
, false);
1946 register_oom_notifier(&rcutorture_oom_nb
);
1947 rcu_torture_fwd_prog_nr(&tested
, &tested_tries
);
1948 rcu_torture_fwd_prog_cr();
1949 unregister_oom_notifier(&rcutorture_oom_nb
);
1951 /* Avoid slow periods, better to test when busy. */
1952 stutter_wait("rcu_torture_fwd_prog");
1953 } while (!torture_must_stop());
1954 /* Short runs might not contain a valid forward-progress attempt. */
1955 WARN_ON(!tested
&& tested_tries
>= 5);
1956 pr_alert("%s: tested %d tested_tries %d\n", __func__
, tested
, tested_tries
);
1957 torture_kthread_stopping("rcu_torture_fwd_prog");
1961 /* If forward-progress checking is requested and feasible, spawn the thread. */
1962 static int __init
rcu_torture_fwd_prog_init(void)
1965 return 0; /* Not requested, so don't do it. */
1966 if (!cur_ops
->stall_dur
|| cur_ops
->stall_dur() <= 0 ||
1967 cur_ops
== &rcu_busted_ops
) {
1968 VERBOSE_TOROUT_STRING("rcu_torture_fwd_prog_init: Disabled, unsupported by RCU flavor under test");
1971 if (stall_cpu
> 0) {
1972 VERBOSE_TOROUT_STRING("rcu_torture_fwd_prog_init: Disabled, conflicts with CPU-stall testing");
1973 if (IS_MODULE(CONFIG_RCU_TORTURE_TESTS
))
1974 return -EINVAL
; /* In module, can fail back to user. */
1975 WARN_ON(1); /* Make sure rcutorture notices conflict. */
1978 if (fwd_progress_holdoff
<= 0)
1979 fwd_progress_holdoff
= 1;
1980 if (fwd_progress_div
<= 0)
1981 fwd_progress_div
= 4;
1982 return torture_create_kthread(rcu_torture_fwd_prog
,
1983 NULL
, fwd_prog_task
);
1986 /* Callback function for RCU barrier testing. */
1987 static void rcu_torture_barrier_cbf(struct rcu_head
*rcu
)
1989 atomic_inc(&barrier_cbs_invoked
);
1992 /* kthread function to register callbacks used to test RCU barriers. */
1993 static int rcu_torture_barrier_cbs(void *arg
)
1995 long myid
= (long)arg
;
1998 struct rcu_head rcu
;
2000 init_rcu_head_on_stack(&rcu
);
2001 VERBOSE_TOROUT_STRING("rcu_torture_barrier_cbs task started");
2002 set_user_nice(current
, MAX_NICE
);
2004 wait_event(barrier_cbs_wq
[myid
],
2006 smp_load_acquire(&barrier_phase
)) != lastphase
||
2007 torture_must_stop());
2008 lastphase
= newphase
;
2009 if (torture_must_stop())
2012 * The above smp_load_acquire() ensures barrier_phase load
2013 * is ordered before the following ->call().
2015 local_irq_disable(); /* Just to test no-irq call_rcu(). */
2016 cur_ops
->call(&rcu
, rcu_torture_barrier_cbf
);
2018 if (atomic_dec_and_test(&barrier_cbs_count
))
2019 wake_up(&barrier_wq
);
2020 } while (!torture_must_stop());
2021 if (cur_ops
->cb_barrier
!= NULL
)
2022 cur_ops
->cb_barrier();
2023 destroy_rcu_head_on_stack(&rcu
);
2024 torture_kthread_stopping("rcu_torture_barrier_cbs");
2028 /* kthread function to drive and coordinate RCU barrier testing. */
2029 static int rcu_torture_barrier(void *arg
)
2033 VERBOSE_TOROUT_STRING("rcu_torture_barrier task starting");
2035 atomic_set(&barrier_cbs_invoked
, 0);
2036 atomic_set(&barrier_cbs_count
, n_barrier_cbs
);
2037 /* Ensure barrier_phase ordered after prior assignments. */
2038 smp_store_release(&barrier_phase
, !barrier_phase
);
2039 for (i
= 0; i
< n_barrier_cbs
; i
++)
2040 wake_up(&barrier_cbs_wq
[i
]);
2041 wait_event(barrier_wq
,
2042 atomic_read(&barrier_cbs_count
) == 0 ||
2043 torture_must_stop());
2044 if (torture_must_stop())
2046 n_barrier_attempts
++;
2047 cur_ops
->cb_barrier(); /* Implies smp_mb() for wait_event(). */
2048 if (atomic_read(&barrier_cbs_invoked
) != n_barrier_cbs
) {
2049 n_rcu_torture_barrier_error
++;
2050 pr_err("barrier_cbs_invoked = %d, n_barrier_cbs = %d\n",
2051 atomic_read(&barrier_cbs_invoked
),
2055 n_barrier_successes
++;
2057 schedule_timeout_interruptible(HZ
/ 10);
2058 } while (!torture_must_stop());
2059 torture_kthread_stopping("rcu_torture_barrier");
2063 /* Initialize RCU barrier testing. */
2064 static int rcu_torture_barrier_init(void)
2069 if (n_barrier_cbs
<= 0)
2071 if (cur_ops
->call
== NULL
|| cur_ops
->cb_barrier
== NULL
) {
2072 pr_alert("%s" TORTURE_FLAG
2073 " Call or barrier ops missing for %s,\n",
2074 torture_type
, cur_ops
->name
);
2075 pr_alert("%s" TORTURE_FLAG
2076 " RCU barrier testing omitted from run.\n",
2080 atomic_set(&barrier_cbs_count
, 0);
2081 atomic_set(&barrier_cbs_invoked
, 0);
2083 kcalloc(n_barrier_cbs
, sizeof(barrier_cbs_tasks
[0]),
2086 kcalloc(n_barrier_cbs
, sizeof(barrier_cbs_wq
[0]), GFP_KERNEL
);
2087 if (barrier_cbs_tasks
== NULL
|| !barrier_cbs_wq
)
2089 for (i
= 0; i
< n_barrier_cbs
; i
++) {
2090 init_waitqueue_head(&barrier_cbs_wq
[i
]);
2091 ret
= torture_create_kthread(rcu_torture_barrier_cbs
,
2093 barrier_cbs_tasks
[i
]);
2097 return torture_create_kthread(rcu_torture_barrier
, NULL
, barrier_task
);
2100 /* Clean up after RCU barrier testing. */
2101 static void rcu_torture_barrier_cleanup(void)
2105 torture_stop_kthread(rcu_torture_barrier
, barrier_task
);
2106 if (barrier_cbs_tasks
!= NULL
) {
2107 for (i
= 0; i
< n_barrier_cbs
; i
++)
2108 torture_stop_kthread(rcu_torture_barrier_cbs
,
2109 barrier_cbs_tasks
[i
]);
2110 kfree(barrier_cbs_tasks
);
2111 barrier_cbs_tasks
= NULL
;
2113 if (barrier_cbs_wq
!= NULL
) {
2114 kfree(barrier_cbs_wq
);
2115 barrier_cbs_wq
= NULL
;
2119 static bool rcu_torture_can_boost(void)
2121 static int boost_warn_once
;
2124 if (!(test_boost
== 1 && cur_ops
->can_boost
) && test_boost
!= 2)
2127 prio
= rcu_get_gp_kthreads_prio();
2132 if (boost_warn_once
== 1)
2135 pr_alert("%s: WARN: RCU kthread priority too low to test boosting. Skipping RCU boost test. Try passing rcutree.kthread_prio > 1 on the kernel command line.\n", KBUILD_MODNAME
);
2136 boost_warn_once
= 1;
2143 static enum cpuhp_state rcutor_hp
;
2146 rcu_torture_cleanup(void)
2150 unsigned long gp_seq
= 0;
2153 if (torture_cleanup_begin()) {
2154 if (cur_ops
->cb_barrier
!= NULL
)
2155 cur_ops
->cb_barrier();
2159 torture_cleanup_end();
2163 rcu_torture_barrier_cleanup();
2164 torture_stop_kthread(rcu_torture_fwd_prog
, fwd_prog_task
);
2165 torture_stop_kthread(rcu_torture_stall
, stall_task
);
2166 torture_stop_kthread(rcu_torture_writer
, writer_task
);
2169 for (i
= 0; i
< nrealreaders
; i
++)
2170 torture_stop_kthread(rcu_torture_reader
,
2172 kfree(reader_tasks
);
2174 rcu_torture_current
= NULL
;
2176 if (fakewriter_tasks
) {
2177 for (i
= 0; i
< nfakewriters
; i
++) {
2178 torture_stop_kthread(rcu_torture_fakewriter
,
2179 fakewriter_tasks
[i
]);
2181 kfree(fakewriter_tasks
);
2182 fakewriter_tasks
= NULL
;
2185 rcutorture_get_gp_data(cur_ops
->ttype
, &flags
, &gp_seq
);
2186 srcutorture_get_gp_data(cur_ops
->ttype
, srcu_ctlp
, &flags
, &gp_seq
);
2187 pr_alert("%s: End-test grace-period state: g%lu f%#x\n",
2188 cur_ops
->name
, gp_seq
, flags
);
2189 torture_stop_kthread(rcu_torture_stats
, stats_task
);
2190 torture_stop_kthread(rcu_torture_fqs
, fqs_task
);
2191 if (rcu_torture_can_boost())
2192 cpuhp_remove_state(rcutor_hp
);
2195 * Wait for all RCU callbacks to fire, then do torture-type-specific
2196 * cleanup operations.
2198 if (cur_ops
->cb_barrier
!= NULL
)
2199 cur_ops
->cb_barrier();
2200 if (cur_ops
->cleanup
!= NULL
)
2203 rcu_torture_stats_print(); /* -After- the stats thread is stopped! */
2205 if (err_segs_recorded
) {
2206 pr_alert("Failure/close-call rcutorture reader segments:\n");
2207 if (rt_read_nsegs
== 0)
2208 pr_alert("\t: No segments recorded!!!\n");
2210 for (i
= 0; i
< rt_read_nsegs
; i
++) {
2211 pr_alert("\t%d: %#x ", i
, err_segs
[i
].rt_readstate
);
2212 if (err_segs
[i
].rt_delay_jiffies
!= 0) {
2213 pr_cont("%s%ldjiffies", firsttime
? "" : "+",
2214 err_segs
[i
].rt_delay_jiffies
);
2217 if (err_segs
[i
].rt_delay_ms
!= 0) {
2218 pr_cont("%s%ldms", firsttime
? "" : "+",
2219 err_segs
[i
].rt_delay_ms
);
2222 if (err_segs
[i
].rt_delay_us
!= 0) {
2223 pr_cont("%s%ldus", firsttime
? "" : "+",
2224 err_segs
[i
].rt_delay_us
);
2228 err_segs
[i
].rt_preempted
? "preempted" : "");
2232 if (atomic_read(&n_rcu_torture_error
) || n_rcu_torture_barrier_error
)
2233 rcu_torture_print_module_parms(cur_ops
, "End of test: FAILURE");
2234 else if (torture_onoff_failures())
2235 rcu_torture_print_module_parms(cur_ops
,
2236 "End of test: RCU_HOTPLUG");
2238 rcu_torture_print_module_parms(cur_ops
, "End of test: SUCCESS");
2239 torture_cleanup_end();
2242 #ifdef CONFIG_DEBUG_OBJECTS_RCU_HEAD
2243 static void rcu_torture_leak_cb(struct rcu_head
*rhp
)
2247 static void rcu_torture_err_cb(struct rcu_head
*rhp
)
2250 * This -might- happen due to race conditions, but is unlikely.
2251 * The scenario that leads to this happening is that the
2252 * first of the pair of duplicate callbacks is queued,
2253 * someone else starts a grace period that includes that
2254 * callback, then the second of the pair must wait for the
2255 * next grace period. Unlikely, but can happen. If it
2256 * does happen, the debug-objects subsystem won't have splatted.
2258 pr_alert("%s: duplicated callback was invoked.\n", KBUILD_MODNAME
);
2260 #endif /* #ifdef CONFIG_DEBUG_OBJECTS_RCU_HEAD */
2263 * Verify that double-free causes debug-objects to complain, but only
2264 * if CONFIG_DEBUG_OBJECTS_RCU_HEAD=y. Otherwise, say that the test
2265 * cannot be carried out.
2267 static void rcu_test_debug_objects(void)
2269 #ifdef CONFIG_DEBUG_OBJECTS_RCU_HEAD
2270 struct rcu_head rh1
;
2271 struct rcu_head rh2
;
2273 init_rcu_head_on_stack(&rh1
);
2274 init_rcu_head_on_stack(&rh2
);
2275 pr_alert("%s: WARN: Duplicate call_rcu() test starting.\n", KBUILD_MODNAME
);
2277 /* Try to queue the rh2 pair of callbacks for the same grace period. */
2278 preempt_disable(); /* Prevent preemption from interrupting test. */
2279 rcu_read_lock(); /* Make it impossible to finish a grace period. */
2280 call_rcu(&rh1
, rcu_torture_leak_cb
); /* Start grace period. */
2281 local_irq_disable(); /* Make it harder to start a new grace period. */
2282 call_rcu(&rh2
, rcu_torture_leak_cb
);
2283 call_rcu(&rh2
, rcu_torture_err_cb
); /* Duplicate callback. */
2288 /* Wait for them all to get done so we can safely return. */
2290 pr_alert("%s: WARN: Duplicate call_rcu() test complete.\n", KBUILD_MODNAME
);
2291 destroy_rcu_head_on_stack(&rh1
);
2292 destroy_rcu_head_on_stack(&rh2
);
2293 #else /* #ifdef CONFIG_DEBUG_OBJECTS_RCU_HEAD */
2294 pr_alert("%s: !CONFIG_DEBUG_OBJECTS_RCU_HEAD, not testing duplicate call_rcu()\n", KBUILD_MODNAME
);
2295 #endif /* #else #ifdef CONFIG_DEBUG_OBJECTS_RCU_HEAD */
2298 static void rcutorture_sync(void)
2300 static unsigned long n
;
2302 if (cur_ops
->sync
&& !(++n
& 0xfff))
2307 rcu_torture_init(void)
2312 static struct rcu_torture_ops
*torture_ops
[] = {
2313 &rcu_ops
, &rcu_busted_ops
, &srcu_ops
, &srcud_ops
,
2314 &busted_srcud_ops
, &tasks_ops
, &trivial_ops
,
2317 if (!torture_init_begin(torture_type
, verbose
))
2320 /* Process args and tell the world that the torturer is on the job. */
2321 for (i
= 0; i
< ARRAY_SIZE(torture_ops
); i
++) {
2322 cur_ops
= torture_ops
[i
];
2323 if (strcmp(torture_type
, cur_ops
->name
) == 0)
2326 if (i
== ARRAY_SIZE(torture_ops
)) {
2327 pr_alert("rcu-torture: invalid torture type: \"%s\"\n",
2329 pr_alert("rcu-torture types:");
2330 for (i
= 0; i
< ARRAY_SIZE(torture_ops
); i
++)
2331 pr_cont(" %s", torture_ops
[i
]->name
);
2333 WARN_ON(!IS_MODULE(CONFIG_RCU_TORTURE_TEST
));
2338 if (cur_ops
->fqs
== NULL
&& fqs_duration
!= 0) {
2339 pr_alert("rcu-torture: ->fqs NULL and non-zero fqs_duration, fqs disabled.\n");
2345 if (nreaders
>= 0) {
2346 nrealreaders
= nreaders
;
2348 nrealreaders
= num_online_cpus() - 2 - nreaders
;
2349 if (nrealreaders
<= 0)
2352 rcu_torture_print_module_parms(cur_ops
, "Start of test");
2354 /* Set up the freelist. */
2356 INIT_LIST_HEAD(&rcu_torture_freelist
);
2357 for (i
= 0; i
< ARRAY_SIZE(rcu_tortures
); i
++) {
2358 rcu_tortures
[i
].rtort_mbtest
= 0;
2359 list_add_tail(&rcu_tortures
[i
].rtort_free
,
2360 &rcu_torture_freelist
);
2363 /* Initialize the statistics so that each run gets its own numbers. */
2365 rcu_torture_current
= NULL
;
2366 rcu_torture_current_version
= 0;
2367 atomic_set(&n_rcu_torture_alloc
, 0);
2368 atomic_set(&n_rcu_torture_alloc_fail
, 0);
2369 atomic_set(&n_rcu_torture_free
, 0);
2370 atomic_set(&n_rcu_torture_mberror
, 0);
2371 atomic_set(&n_rcu_torture_error
, 0);
2372 n_rcu_torture_barrier_error
= 0;
2373 n_rcu_torture_boost_ktrerror
= 0;
2374 n_rcu_torture_boost_rterror
= 0;
2375 n_rcu_torture_boost_failure
= 0;
2376 n_rcu_torture_boosts
= 0;
2377 for (i
= 0; i
< RCU_TORTURE_PIPE_LEN
+ 1; i
++)
2378 atomic_set(&rcu_torture_wcount
[i
], 0);
2379 for_each_possible_cpu(cpu
) {
2380 for (i
= 0; i
< RCU_TORTURE_PIPE_LEN
+ 1; i
++) {
2381 per_cpu(rcu_torture_count
, cpu
)[i
] = 0;
2382 per_cpu(rcu_torture_batch
, cpu
)[i
] = 0;
2385 err_segs_recorded
= 0;
2388 /* Start up the kthreads. */
2390 firsterr
= torture_create_kthread(rcu_torture_writer
, NULL
,
2394 if (nfakewriters
> 0) {
2395 fakewriter_tasks
= kcalloc(nfakewriters
,
2396 sizeof(fakewriter_tasks
[0]),
2398 if (fakewriter_tasks
== NULL
) {
2399 VERBOSE_TOROUT_ERRSTRING("out of memory");
2404 for (i
= 0; i
< nfakewriters
; i
++) {
2405 firsterr
= torture_create_kthread(rcu_torture_fakewriter
,
2406 NULL
, fakewriter_tasks
[i
]);
2410 reader_tasks
= kcalloc(nrealreaders
, sizeof(reader_tasks
[0]),
2412 if (reader_tasks
== NULL
) {
2413 VERBOSE_TOROUT_ERRSTRING("out of memory");
2417 for (i
= 0; i
< nrealreaders
; i
++) {
2418 firsterr
= torture_create_kthread(rcu_torture_reader
, (void *)i
,
2423 if (stat_interval
> 0) {
2424 firsterr
= torture_create_kthread(rcu_torture_stats
, NULL
,
2429 if (test_no_idle_hz
&& shuffle_interval
> 0) {
2430 firsterr
= torture_shuffle_init(shuffle_interval
* HZ
);
2439 t
= cur_ops
->stall_dur
? cur_ops
->stall_dur() : stutter
* HZ
;
2440 firsterr
= torture_stutter_init(stutter
* HZ
, t
);
2444 if (fqs_duration
< 0)
2447 /* Create the fqs thread */
2448 firsterr
= torture_create_kthread(rcu_torture_fqs
, NULL
,
2453 if (test_boost_interval
< 1)
2454 test_boost_interval
= 1;
2455 if (test_boost_duration
< 2)
2456 test_boost_duration
= 2;
2457 if (rcu_torture_can_boost()) {
2459 boost_starttime
= jiffies
+ test_boost_interval
* HZ
;
2461 firsterr
= cpuhp_setup_state(CPUHP_AP_ONLINE_DYN
, "RCU_TORTURE",
2462 rcutorture_booster_init
,
2463 rcutorture_booster_cleanup
);
2466 rcutor_hp
= firsterr
;
2468 firsterr
= torture_shutdown_init(shutdown_secs
, rcu_torture_cleanup
);
2471 firsterr
= torture_onoff_init(onoff_holdoff
* HZ
, onoff_interval
,
2475 firsterr
= rcu_torture_stall_init();
2478 firsterr
= rcu_torture_fwd_prog_init();
2481 firsterr
= rcu_torture_barrier_init();
2485 rcu_test_debug_objects();
2491 rcu_torture_cleanup();
2495 module_init(rcu_torture_init
);
2496 module_exit(rcu_torture_cleanup
);