[PATCH 03/17] srcu: Try idle expedited GPs in the caller
From: Kunwu Chan
Date: Tue Dec 01 2026 - 11:19:10 EST
Avoid the completion callback round trip for expedited GPs that can
finish in the caller. If the cookie is already complete after taking
the locks, return. When starting a GP, scan each retired rank once
and use normal GP completion. The fastpath preserves the guarantee
that a cookie obtained before synchronize_srcu_expedited() polls
complete after it returns.
Disable preemption before taking either mutex. On a scan miss, queue
irq_work before unlocking because the fallback callback may need a
later GP.
Signed-off-by: Kunwu Chan <kunwu.chan@xxxxxxxxx>
---
kernel/rcu/srcutree.c | 113 +++++++++++++++++++++++++++++++++++++++++-
1 file changed, 112 insertions(+), 1 deletion(-)
diff --git a/kernel/rcu/srcutree.c b/kernel/rcu/srcutree.c
index 16b1a6b677e1..d7773825e814 100644
--- a/kernel/rcu/srcutree.c
+++ b/kernel/rcu/srcutree.c
@@ -588,6 +588,25 @@ static unsigned long srcu_readers_unlock_idx(struct srcu_struct *ssp, int idx, u
return sum;
}
+/*
+ * Check the inactive rank once under srcu_gp_mutex. Retirement limits
+ * late increments for the wrap bound in srcu_readers_active_idx_check().
+ * Reject SLOWGP even without PROVE_RCU.
+ */
+static bool srcu_readers_inactive_check(struct srcu_struct *ssp)
+{
+ int idx = !(ssp->srcu_ctrp - &ssp->sda->srcu_ctrs[0]);
+ unsigned long rdm;
+ unsigned long unlocks;
+
+ lockdep_assert_held(&ssp->srcu_sup->srcu_gp_mutex);
+ unlocks = srcu_readers_unlock_idx(ssp, idx, &rdm);
+ if (rdm & SRCU_READ_FLAVOR_SLOWGP)
+ return false;
+ smp_mb(); /* A, pairs with the readers' B and C. */
+ return srcu_readers_lock_idx(ssp, idx, false, unlocks);
+}
+
/*
* Return true if the number of pre-existing readers is determined to
* be zero.
@@ -1693,8 +1712,96 @@ void call_srcu(struct srcu_struct *ssp, struct rcu_head *rhp,
EXPORT_SYMBOL_GPL(call_srcu);
/*
- * Helper function for synchronize_srcu() and synchronize_srcu_expedited().
+ * Try one reader-free GP without waiting for mutexes or readers.
+ * Take both mutexes before GP start to exclude an older callback tail.
+ * Disable preemption across both mutexes to keep the GP moving.
*/
+static bool srcu_expedited_fastpath(struct srcu_struct *ssp)
+{
+ unsigned long cookie, flags;
+ int cpu;
+ int ss_state;
+ struct srcu_usage *sup = ssp->srcu_sup;
+
+ if (!srcu_init_done || rcu_scheduler_active != RCU_SCHEDULER_RUNNING ||
+ (READ_ONCE(ssp->srcu_reader_flavor) & SRCU_READ_FLAVOR_SLOWGP))
+ return false;
+
+ /* Pending callbacks are a hint to use the worker. */
+ for_each_possible_cpu(cpu)
+ if (rcu_segcblist_n_cbs(&per_cpu_ptr(ssp->sda, cpu)->srcu_cblist))
+ return false;
+
+ /* Reader hint. Ordered scans follow GP start. */
+ if (srcu_readers_active(ssp))
+ return false;
+ cookie = get_state_synchronize_srcu(ssp);
+
+ preempt_disable();
+ if (!mutex_trylock(&sup->srcu_gp_mutex))
+ goto out_preempt;
+ if (!mutex_trylock(&sup->srcu_cb_mutex))
+ goto out_gp;
+ raw_spin_lock_irqsave_rcu_node(sup, flags);
+ if (poll_state_synchronize_srcu(ssp, cookie)) {
+ raw_spin_unlock_irqrestore_rcu_node(sup, flags);
+ mutex_unlock(&sup->srcu_cb_mutex);
+ mutex_unlock(&sup->srcu_gp_mutex);
+ preempt_enable();
+ return true;
+ }
+
+ ss_state = smp_load_acquire(&sup->srcu_size_state); /* See init_srcu_struct_nodes(). */
+ /* Keep per-CPU sequence maintenance in the normal driver. */
+ if (rcu_seq_state(READ_ONCE(sup->srcu_gp_seq)) != SRCU_STATE_IDLE ||
+ ULONG_CMP_LT(sup->srcu_gp_seq, sup->srcu_gp_seq_needed) ||
+ !((sup->srcu_gp_seq + RCU_SEQ_GP) & counter_wrap_check) ||
+ (ss_state != SRCU_SIZE_SMALL && ss_state != SRCU_SIZE_BIG)) {
+ raw_spin_unlock_irqrestore_rcu_node(sup, flags);
+ goto out_cb;
+ }
+
+ smp_store_release(&sup->srcu_gp_seq_needed, cookie); /* See srcu_funnel_gp_start(). */
+ WRITE_ONCE(sup->srcu_gp_seq_needed_exp, cookie);
+ srcu_gp_start(ssp);
+ raw_spin_unlock_irqrestore_rcu_node(sup, flags);
+
+ /* Match the scan and flip order in srcu_advance_state(). */
+ if (!srcu_readers_inactive_check(ssp))
+ goto out_reschedule;
+
+ srcu_flip(ssp);
+ raw_spin_lock_irqsave_rcu_node(sup, flags);
+ rcu_seq_set_state(&sup->srcu_gp_seq, SRCU_STATE_SCAN2);
+ sup->srcu_n_exp_nodelay = 0;
+ raw_spin_unlock_irqrestore_rcu_node(sup, flags);
+
+ if (!srcu_readers_inactive_check(ssp))
+ goto out_reschedule;
+
+ /*
+ * Contention can set ALLOC during a SMALL GP. Both states queue
+ * callbacks on the boot CPU. Leave allocation to the worker.
+ */
+ __srcu_gp_end(ssp, false, ss_state);
+ preempt_enable();
+ return poll_state_synchronize_srcu(ssp, cookie);
+
+out_reschedule:
+ /*
+ * Hand off this GP before unlocking. The caller's callback
+ * may need another GP.
+ */
+ irq_work_queue(&sup->irq_work);
+out_cb:
+ mutex_unlock(&sup->srcu_cb_mutex);
+out_gp:
+ mutex_unlock(&sup->srcu_gp_mutex);
+out_preempt:
+ preempt_enable();
+ return false;
+}
+
static void __synchronize_srcu(struct srcu_struct *ssp, bool do_norm)
{
struct rcu_synchronize rcu;
@@ -1716,6 +1823,10 @@ static void __synchronize_srcu(struct srcu_struct *ssp, bool do_norm)
}
might_sleep();
check_init_srcu_struct(ssp, false);
+
+ if (!do_norm && srcu_expedited_fastpath(ssp))
+ return;
+
init_completion(&rcu.completion);
init_rcu_head_on_stack(&rcu.head);
__call_srcu(ssp, &rcu.head, wakeme_after_rcu, do_norm);
--
2.43.0