Sequence counter related RCU changes for v6.16
Subsystems (2)
Joel Fernandes
maintainercover Sequence counter related RCU changes for v6.16
Hi,
Please find the upcoming sequence Counter related RCU changes. The changes can also be found at:
git://git.kernel.org/pub/scm/linux/kernel/git/jfern/linux.git seq.2025.04.18a
Thanks.
Frederic Weisbecker (1):
rcu: Comment on the extraneous delta test on rcu_seq_done_exact()
Joel Fernandes (3):
rcu: Replace magic number with meaningful constant in
rcu_seq_done_exact()
rcu: Add warning to ensure rcu_seq_done_exact() is working
srcu: Use rcu_seq_done_exact() for polling API
kernel/rcu/rcu.h | 14 +++++++++++++- kernel/rcu/srcutree.c | 2 +- kernel/rcu/tree.c | 6 ++++++ 3 files changed, 20 insertions(+), 2 deletions(-)
Signature · 1 line
2.43.0
Joel Fernandes
maintainerPATCH 1/4 rcu: Replace magic number with meaningful constant in rcu_seq_done_exact()
The rcu_seq_done_exact() function checks if a grace period has completed by comparing sequence numbers. It includes a guard band to handle sequence number wraparound, which was previously expressed using the magic number calculation '3 * RCU_SEQ_STATE_MASK + 1'.
This magic number is not immediately obvious in terms of what it represents.
Instead, the reason we need this tiny guardband is because of the lag between the setting of rcu_state.gp_seq_polled and root rnp's gp_seq in rcu_gp_init().
This guardband needs to be at least 2 GPs worth of counts, to avoid recognizing the newly started GP as completed immediately, due to the following sequence which arises due to the delay between update of rcu_state.gp_seq_polled and root rnp's gp_seq:
rnp->gp_seq = rcu_state.gp_seq = 0
CPU 0 CPU 1
----- -----
// rcu_state.gp_seq = 1
rcu_seq_start(&rcu_state.gp_seq)
// snap = 8
snap = rcu_seq_snap(&rcu_state.gp_seq)
// Two full GP differences
rcu_seq_done_exact(&rnp->gp_seq, snap)
// rnp->gp_seq = 1
WRITE_ONCE(rnp->gp_seq, rcu_state.gp_seq);This can happen due to get_state_synchronize_rcu_full() sampling rcu_state.gp_seq_polled, however the poll_state_synchronize_rcu_full() sampling the root rnp's gp_seq. The delay between the update of the 2 counters occurs in rcu_gp_init() during which the counters briefly go out of sync.
Make the guardband explictly 2 GPs. This improves code readability and maintainability by making the intent clearer as well.
--- kernel/rcu/rcu.h | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-)
kernel/rcu/rcu.h +4 -1
@@ -57,6 +57,9 @@/* Low-order bit definition for polled grace-period APIs. */ #define RCU_GET_STATE_COMPLETED 0x1 +/* A complete grace period count */ +#define RCU_SEQ_GP (RCU_SEQ_STATE_MASK + 1) +extern int sysctl_sched_rt_runtime;/*
@@ -162,7 +165,7 @@ static inline bool rcu_seq_done_exact(unsigned long *sp, unsigned long s) {unsigned long cur_s =READ_ONCE (*sp); -return ULONG_CMP_GE (cur_s, s) ||ULONG_CMP_LT (cur_s, s - (3 *RCU_SEQ_STATE_MASK +1 )); +return ULONG_CMP_GE (cur_s, s) ||ULONG_CMP_LT (cur_s, s - (2 *RCU_SEQ_GP )); }/*
Signature · 1 line
2.43.0
Joel Fernandes
maintainerPATCH 2/4 rcu: Add warning to ensure rcu_seq_done_exact() is working
In reply to Joel FernandesThe previous patch improved the rcu_seq_done_exact() function by adding a meaningful constant for the guardband.
Ensure that this is working for the future by a quick check during rcu_gp_init().
--- kernel/rcu/tree.c | 6 ++++++ 1 file changed, 6 insertions(+)
kernel/rcu/tree.c +6 -0
@@ -1798,6 +1798,7 @@ static noinline_for_stack bool rcu_gp_init(void)struct rcu_data *rdp;struct rcu_node *rnp =rcu_get_root ();bool start_new_poll; +unsigned long old_gp_seq;WRITE_ONCE (rcu_state.gp_activity, jiffies);raw_spin_lock_irq_rcu_node (rnp);
@@ -1825,7 +1826,12 @@ static noinline_for_stack bool rcu_gp_init(void)*/ start_new_poll =rcu_sr_normal_gp_init ();/* Record GP times before starting GP, hence rcu_seq_start(). */ + old_gp_seq = rcu_state.gp_seq;rcu_seq_start (&rcu_state.gp_seq); +/* Ensure that rcu_seq_done_exact() guardband doesn't give false positives. */ +WARN_ON_ONCE (IS_ENABLED (CONFIG_PROVE_RCU ) && +rcu_seq_done_exact (&old_gp_seq,rcu_seq_snap (&rcu_state.gp_seq))); +ASSERT_EXCLUSIVE_WRITER (rcu_state.gp_seq);trace_rcu_grace_period (rcu_state.name, rcu_state.gp_seq,TPS ("start" ));rcu_poll_gp_seq_start (&rcu_state.gp_seq_polled_snap);
Signature · 1 line
2.43.0
Joel Fernandes
maintainerPATCH 3/4 rcu: Comment on the extraneous delta test on rcu_seq_done_exact()
In reply to Joel FernandesFrom: Frederic Weisbecker <[email protected]>
The numbers used in rcu_seq_done_exact() lack some explanation behind their magic. Especially after the commit:
85aad7cc4178 ("rcu: Fix get_state_synchronize_rcu_full() GP-start detection")
which reported a subtle issue where a new GP sequence snapshot was taken on the root node state while a grace period had already been started and reflected on the global state sequence but not yet on the root node sequence, making a polling user waiting on a wrong already started grace period that would ignore freshly online CPUs.
The fix involved taking the snaphot on the global state sequence and waiting on the root node sequence. And since a grace period is first started on the global state and only afterward reflected on the root node, a snapshot taken on the global state sequence might be two full grace periods ahead of the root node as in the following example:
rnp->gp_seq = rcu_state.gp_seq = 0
CPU 0 CPU 1
----- -----
// rcu_state.gp_seq = 1
rcu_seq_start(&rcu_state.gp_seq)
// snap = 8
snap = rcu_seq_snap(&rcu_state.gp_seq)
// Two full GP differences
rcu_seq_done_exact(&rnp->gp_seq, snap)
// rnp->gp_seq = 1
WRITE_ONCE(rnp->gp_seq, rcu_state.gp_seq);Add a comment about those expectations and to clarify the magic within the relevant function.
Note that the issue arises mainly with the use of rcu_seq_done_exact() which has a much tigher guardband (of 2 GPs) to ensure the false-negative window of the API during wraparound is limited to just 2 GPs. rcu_seq_done() does not have such strict requirements, however its large false-negative window of ULONG_MAX/2 is not ideal for the polling API. However, this also means care is needed to ensure the guardband is as large as needed to avoid the example scenario describe above which a warning added in an earlier patch does.
[ Comment wordsmithing by Joel ]
--- kernel/rcu/rcu.h | 9 +++++++++ 1 file changed, 9 insertions(+)
kernel/rcu/rcu.h +9 -0
@@ -160,6 +160,15 @@ static inline bool rcu_seq_done(unsigned long *sp, unsigned long s)* Given a snapshot from rcu_seq_snap(), determine whether or not a * full update-side operation has occurred, but do not allow the * (ULONG_MAX / 2) safety-factor/guard-band. +* +* The token returned by get_state_synchronize_rcu_full() is based on +* rcu_state.gp_seq but it is tested in poll_state_synchronize_rcu_full() +* against the root rnp->gp_seq. Since rcu_seq_start() is first called +* on rcu_state.gp_seq and only later reflected on the root rnp->gp_seq, +* it is possible that rcu_seq_snap(rcu_state.gp_seq) returns 2 full grace +* periods ahead of the root rnp->gp_seq. To prevent false-positives with the +* full polling API that a wrap around instantly completed the GP, when nothing +* like that happened, adjust for the 2 GPs in the ULONG_CMP_LT(). */ static inline bool rcu_seq_done_exact (unsigned long *sp,unsigned long s) {
Signature · 1 line
2.43.0
Joel Fernandes
maintainerPATCH 4/4 srcu: Use rcu_seq_done_exact() for polling API
In reply to Joel Fernandespoll_state_synchronize_srcu() uses rcu_seq_done() unlike poll_state_synchronize_rcu() which uses rcu_seq_done_exact().
The rcu_seq_done_exact() makes more sense for polling API, as with this API, there is a higher chance that there is a significant delay between the get_state..() and poll_state..() calls since a cookie can be stored and reused at a later time. During such a delay, if the gp_seq counter progresses more than ULONG_MAX/2 distance, then poll_state..() may return false for a long time unwantedly.
Fix by using the more accurate rcu_seq_done_exact() API which is exactly what straight RCU's polling does.
It may make sense, as future work, to add debug code here as well, where we compare a physical timestamp between get_state..() and poll_state() calls and yell if significant time has past but the grace period has still not progressed.
--- kernel/rcu/srcutree.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
kernel/rcu/srcutree.c +1 -1
@@ -1589,7 +1589,7 @@ EXPORT_SYMBOL_GPL(start_poll_synchronize_srcu);bool poll_state_synchronize_srcu (struct srcu_struct *ssp,unsigned long cookie) {if (cookie !=SRCU_GET_STATE_COMPLETED && - !rcu_seq_done (&ssp->srcu_sup->srcu_gp_seq, cookie)) + !rcu_seq_done_exact (&ssp->srcu_sup->srcu_gp_seq, cookie))return false;// Ensure that the end of the SRCU grace period happens before // any subsequent code that the caller might execute.
Signature · 1 line
2.43.0