rcu/nocb: Few NOCB cleanup patches
Joel Fernandes
maintainerv2 cover rcu/nocb: Few NOCB cleanup patches
This series removes an unnecessary wake path (WakeOvfIsDeferred) from the NOCB code and adds debugging warnings to detect any potential lost wakeups.
The WakeOvfIsDeferred code path in __call_rcu_nocb_wake() attempts to wake rcuog when the callback count exceeds qhimark and callbacks aren't done with their GP. However, testing proves this wake is always redundant or useless - in the flooding case, rcuog is always waiting for a GP to finish, so waking it up is pointless.
All NOCB rcutorture scenarios pass (TREE01, TREE05, TREE08).
Changes since v1:
- Split into separate patches as suggested by Frederic
- Removed the pairing smp_mb__after_spin_lock() in do_nocb_deferred_wakeup_timer() as noted by Frederic.
Link to v1 (RFC):
https://lore.kernel.org/all/[email protected]/
Joel Fernandes (3):
rcu/nocb: Remove unnecessary WakeOvfIsDeferred wake path rcu/nocb: Add warning if no rcuog wake up attempt happened during
overload
rcu/nocb: Add warning to detect if overload advancement is ever useful
kernel/rcu/tree.c | 2 ++ kernel/rcu/tree.h | 2 +- kernel/rcu/tree_nocb.h | 53 +++++++++++++++++++++--------------------- 3 files changed, 29 insertions(+), 28 deletions(-)
--
2.34.1
Joel Fernandes
maintainerPATCH v2 1/3 rcu/nocb: Remove unnecessary WakeOvfIsDeferred wake path
The WakeOvfIsDeferred code path in __call_rcu_nocb_wake() attempts to wake rcuog when the callback count exceeds qhimark and callbacks aren't done with their GP (newly queued or awaiting GP). However, a lot of testing proves this wake is always redundant or useless.
In the flooding case, rcuog is always waiting for a GP to finish. So waking up the rcuog thread is pointless. The timer wakeup adds overhead, rcuog simply wakes up and goes back to sleep achieving nothing.
This path also adds a full memory barrier, and additional timer expiry modifications unnecessarily.
The root cause is that WakeOvfIsDeferred fires when !rcu_segcblist_ready_cbs() (GP not complete), but waking rcuog cannot accelerate GP completion.
This commit therefore removes this path.
Tested with rcutorture scenarios: TREE01, TREE05, TREE08 (all NOCB configurations) - all pass. Also stress tested using a kernel module that floods call_rcu() to trigger the overload conditions and made the observations confirming the findings.
--- kernel/rcu/tree.h | 1 - kernel/rcu/tree_nocb.h | 35 +++++++++-------------------------- 2 files changed, 9 insertions(+), 27 deletions(-)
kernel/rcu/tree.h +0 -1
@@ -301,7 +301,6 @@ struct rcu_data {#define RCU_NOCB_WAKE_BYPASS 1#define RCU_NOCB_WAKE_LAZY 2#define RCU_NOCB_WAKE 3 -#define RCU_NOCB_WAKE_FORCE 4#define RCU_JIFFIES_TILL_FORCE_QS (1 + (HZ > 250) + (HZ > 500))/* For jiffies_till_first_fqs and */
kernel/rcu/tree_nocb.h +9 -26
@@ -518,10 +518,8 @@ static bool rcu_nocb_try_bypass(struct rcu_data *rdp, struct rcu_head *rhp, }/* -* Awaken the no-CBs grace-period kthread if needed, either due to it -* legitimately being asleep or due to overload conditions. -* -* If warranted, also wake up the kthread servicing this CPUs queues. +* Awaken the no-CBs grace-period kthread if needed due to it legitimately +* being asleep. */ static void __call_rcu_nocb_wake (struct rcu_data *rdp,bool was_alldone,unsigned long flags)
@@ -533,7 +531,6 @@ static void __call_rcu_nocb_wake(struct rcu_data *rdp, bool was_alldone,long lazy_len;long len;struct task_struct *t; -struct rcu_data *rdp_gp = rdp->nocb_gp_rdp;// If we are being polled or there is no kthread, just leave. t =READ_ONCE (rdp->nocb_gp_kthread);
@@ -549,22 +546,22 @@ static void __call_rcu_nocb_wake(struct rcu_data *rdp, bool was_alldone, lazy_len =READ_ONCE (rdp->lazy_len);if (was_alldone) { rdp->qlen_last_fqs_check = len; +rcu_nocb_unlock (rdp);// Only lazy CBs in bypass list if (lazy_len && bypass_len == lazy_len) { -rcu_nocb_unlock (rdp);wake_nocb_gp_defer (rdp,RCU_NOCB_WAKE_LAZY ,TPS ("WakeLazy" )); }else if (!irqs_disabled_flags (flags)) {/* ... if queue was empty ... */ -rcu_nocb_unlock (rdp);wake_nocb_gp (rdp, false);trace_rcu_nocb_wake (rcu_state.name, rdp->cpu,TPS ("WakeEmpty" )); }else { -rcu_nocb_unlock (rdp);wake_nocb_gp_defer (rdp,RCU_NOCB_WAKE ,TPS ("WakeEmptyIsDeferred" )); } + +return ; }else if (len > rdp->qlen_last_fqs_check + qhimark) {/* ... or if many callbacks queued. */ rdp->qlen_last_fqs_check = len;
@@ -575,21 +572,10 @@ static void __call_rcu_nocb_wake(struct rcu_data *rdp, bool was_alldone,rcu_advance_cbs_nowake (rdp->mynode, rdp); rdp->nocb_gp_adv_time = j; } -smp_mb ();/* Enqueue before timer_pending(). */ -if ((rdp->nocb_cb_sleep || - !rcu_segcblist_ready_cbs (&rdp->cblist)) && - !timer_pending (&rdp_gp->nocb_timer)) { -rcu_nocb_unlock (rdp); -wake_nocb_gp_defer (rdp,RCU_NOCB_WAKE_FORCE , -TPS ("WakeOvfIsDeferred" )); - }else { -rcu_nocb_unlock (rdp); -trace_rcu_nocb_wake (rcu_state.name, rdp->cpu,TPS ("WakeNot" )); - } - }else { -rcu_nocb_unlock (rdp); -trace_rcu_nocb_wake (rcu_state.name, rdp->cpu,TPS ("WakeNot" )); } + +rcu_nocb_unlock (rdp); +trace_rcu_nocb_wake (rcu_state.name, rdp->cpu,TPS ("WakeNot" )); } static void call_rcu_nocb(struct rcu_data *rdp, struct rcu_head *head,
@@ -966,7 +952,6 @@ static bool do_nocb_deferred_wakeup_common(struct rcu_data *rdp_gp, unsigned long flags) __releases(rdp_gp->nocb_gp_lock) { -int ndw;int ret;if (!rcu_nocb_need_deferred_wakeup (rdp_gp, level)) {
@@ -974,8 +959,7 @@ static bool do_nocb_deferred_wakeup_common(struct rcu_data *rdp_gp,return false; } - ndw = rdp_gp->nocb_defer_wakeup; - ret =__wake_nocb_gp (rdp_gp, rdp, ndw ==RCU_NOCB_WAKE_FORCE , flags); + ret =__wake_nocb_gp (rdp_gp, rdp, false, flags);trace_rcu_nocb_wake (rcu_state.name, rdp->cpu,TPS ("DeferredWake" ));return ret;
@@ -991,7 +975,6 @@ static void do_nocb_deferred_wakeup_timer(struct timer_list *t)trace_rcu_nocb_wake (rcu_state.name, rdp->cpu,TPS ("Timer" ));raw_spin_lock_irqsave (&rdp->nocb_gp_lock, flags); -smp_mb__after_spinlock ();/* Timer expire before wakeup. */ do_nocb_deferred_wakeup_common (rdp, rdp,RCU_NOCB_WAKE_BYPASS , flags); }
Signature · 1 line
2.34.1
Joel Fernandes
maintainerPATCH v2 2/3 rcu/nocb: Add warning if no rcuog wake up attempt happened during overload
In reply to Joel FernandesTo be sure we have no rcog wake ups that were lost, add a warning to cover the case where the rdp is overloaded with callbacks but no wake up was attempted.
--- kernel/rcu/tree.c | 2 ++ kernel/rcu/tree.h | 1 + kernel/rcu/tree_nocb.h | 6 +++++- 3 files changed, 8 insertions(+), 1 deletion(-)
kernel/rcu/tree.c +2 -0
@@ -3783,6 +3783,8 @@ static void rcu_barrier_entrain(struct rcu_data *rdp)debug_rcu_head_unqueue (&rdp->barrier_head);rcu_barrier_trace (TPS ("IRQNQ" ),-1 , rcu_state.barrier_sequence); } +if (wake_nocb) + rdp->nocb_gp_wake_attempt = true;rcu_nocb_unlock (rdp);if (wake_nocb)wake_nocb_gp (rdp, false);
kernel/rcu/tree.h +1 -0
@@ -257,6 +257,7 @@ struct rcu_data {unsigned long nocb_gp_loops;/* # passes through wait code. */ struct swait_queue_head nocb_gp_wq;/* For nocb kthreads to sleep on. */ bool nocb_cb_sleep;/* Is the nocb CB thread asleep? */ +bool nocb_gp_wake_attempt;/* Was a rcuog wakeup attempted? */ struct task_struct *nocb_cb_kthread;struct list_head nocb_head_rdp;/* * Head of rcu_data list in wakeup chain,
kernel/rcu/tree_nocb.h +5 -1
@@ -546,6 +546,7 @@ static void __call_rcu_nocb_wake(struct rcu_data *rdp, bool was_alldone, lazy_len =READ_ONCE (rdp->lazy_len);if (was_alldone) { rdp->qlen_last_fqs_check = len; + rdp->nocb_gp_wake_attempt = true;rcu_nocb_unlock (rdp);// Only lazy CBs in bypass list if (lazy_len && bypass_len == lazy_len) {
@@ -563,7 +564,8 @@ static void __call_rcu_nocb_wake(struct rcu_data *rdp, bool was_alldone,return ; }else if (len > rdp->qlen_last_fqs_check + qhimark) { -/* ... or if many callbacks queued. */ +/* Callback overload condition. */ +WARN_ON_ONCE (!rdp->nocb_gp_wake_attempt); rdp->qlen_last_fqs_check = len; j = jiffies; if (j != rdp->nocb_gp_adv_time &&
@@ -688,6 +690,7 @@ static void nocb_gp_wait(struct rcu_data *my_rdp) bypass_ncbs > 2 * qhimark)) { flush_bypass = true; } else if (!bypass_ncbs && rcu_segcblist_empty(&rdp->cblist)) { + rdp->nocb_gp_wake_attempt = false;rcu_nocb_unlock_irqrestore (rdp, flags);continue ;/* No callbacks here, try next. */ }
@@ -1254,6 +1257,7 @@ lazy_rcu_shrink_scan(struct shrinker *shrink, struct shrink_control *sc)continue ; }rcu_nocb_try_flush_bypass (rdp, jiffies); + rdp->nocb_gp_wake_attempt = true;rcu_nocb_unlock_irqrestore (rdp, flags);wake_nocb_gp (rdp, false); sc->nr_to_scan -= _count;
Signature · 1 line
2.34.1
Joel Fernandes
maintainerPATCH v2 3/3 rcu/nocb: Add warning to detect if overload advancement is ever useful
In reply to Joel FernandesDuring callback overload, the NOCB code attempts an opportunistic advancement via rcu_advance_cbs_nowake().
Analysis via tracing with 300,000 callbacks flooded shows this optimization is likely dead code:
- 30 overload conditions triggered
- 0 advancements actually occurred
- 100% of time no advancement due to current GP not done.
I also ran TREE05 and TREE08 for 2 hours and cannot trigger it.
When callbacks overflow (exceed qhimark), they are waiting for a grace period that hasn't completed yet. The optimization requires the GP to be complete to advance callbacks, but the overload condition itself is caused by callbacks piling up faster than GPs can complete. This creates a logical contradiction where the advancement cannot happen.
In *theory* this might be possible, the GP completed just in the nick of time as we hit the overload, but this is just so rare that it can be considered impossible when we cannot even hit it with synthetic callback flooding even, it is a waste of cycles to even try to advance, let alone be useful and is a maintenance burden complexity we don't need.
I suggest deletion. However, add a WARN_ON_ONCE for a merge window or 2 and delete it after out of extreme caution.
--- kernel/rcu/tree_nocb.h | 12 ++++++++++++ 1 file changed, 12 insertions(+)
kernel/rcu/tree_nocb.h +12 -0
@@ -571,8 +571,20 @@ static void __call_rcu_nocb_wake(struct rcu_data *rdp, bool was_alldone,if (j != rdp->nocb_gp_adv_time &&rcu_segcblist_nextgp (&rdp->cblist, &cur_gp_seq) &&rcu_seq_done (&rdp->mynode->gp_seq, cur_gp_seq)) { +long done_before =rcu_segcblist_get_seglen (&rdp->cblist,RCU_DONE_TAIL ); +rcu_advance_cbs_nowake (rdp->mynode, rdp); rdp->nocb_gp_adv_time = j; + +/* +* The advance_cbs call above is not useful. Under an +* overload condition, nocb_gp_wait() is always waiting +* for GP completion, due to this nothing can be moved +* from WAIT to DONE, in the list. WARN if an +* advancement happened (next step is deletion of advance). +*/ +WARN_ON_ONCE (rcu_segcblist_get_seglen (&rdp->cblist, +RCU_DONE_TAIL ) > done_before); } }
Signature · 1 line
2.34.1