rcu/nocb: RCU nocb bypass-list cleanup patches
Joel Fernandes
maintainercover rcu/nocb: RCU nocb bypass-list cleanup patches
The RCU nocb logic is complex, with duplicate code patterns scattered across multiple functions. This series is a small refactor to make the bypass-list code easier to follow. In the future, I am planning to do more refactors of this code.
This series is based on top of the earlier -next series I had sent:
https://lore.kernel.org/all/[email protected]/
There is no urgency with this particular series. It is OK for v7.1 as well.
Testing:
I ran rcutorture on this series plus the earlier -next submissions for 16 hours on all RCU NOCB configs (TREE01, TREE05, TREE08). All scenarios passed.
The git tree with all patches can be found at:
https://git.kernel.org/pub/scm/linux/kernel/git/jfern/linux.git (tag: rcu-nocb-cleanup-v1-20260103)
Joel Fernandes (3):
rcu/nocb: Extract nocb_defer_wakeup_cancel() helper rcu/nocb: Consolidate rcu_nocb_cpu_offload/deoffload functions rcu/nocb: Extract nocb_bypass_needs_flush() to reduce duplication
kernel/rcu/tree_nocb.h | 143 +++++++++++++++++++++++++---------------- 1 file changed, 86 insertions(+), 57 deletions(-)
base-commit: a02abdb7c34af5364e6243276b8ba1dc50972ffe --
2.34.1
Joel Fernandes
maintainerPATCH 1/3 rcu/nocb: Extract nocb_defer_wakeup_cancel() helper
The pattern of checking nocb_defer_wakeup and deleting the timer is duplicated in __wake_nocb_gp() and nocb_gp_wait(). Extract this into a common helper function nocb_defer_wakeup_cancel().
This removes code duplication and makes it easier to maintain.
--- kernel/rcu/tree_nocb.h | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-)
kernel/rcu/tree_nocb.h +11 -8
@@ -190,6 +190,15 @@ static void rcu_init_one_nocb(struct rcu_node *rnp)init_swait_queue_head (&rnp->nocb_gp_wq[1 ]); } +/* Clear any pending deferred wakeup timer (nocb_gp_lock must be held). */ +static void nocb_defer_wakeup_cancel (struct rcu_data *rdp_gp) +{ +if (rdp_gp->nocb_defer_wakeup >RCU_NOCB_WAKE_NOT ) { +WRITE_ONCE (rdp_gp->nocb_defer_wakeup,RCU_NOCB_WAKE_NOT ); +timer_delete (&rdp_gp->nocb_timer); + } +} +static bool __wake_nocb_gp (struct rcu_data *rdp_gp,struct rcu_data *rdp,bool force,unsigned long flags)
@@ -204,10 +213,7 @@ static bool __wake_nocb_gp(struct rcu_data *rdp_gp,return false; } -if (rdp_gp->nocb_defer_wakeup >RCU_NOCB_WAKE_NOT ) { -WRITE_ONCE (rdp_gp->nocb_defer_wakeup,RCU_NOCB_WAKE_NOT ); -timer_delete (&rdp_gp->nocb_timer); - } +nocb_defer_wakeup_cancel (rdp_gp);if (force ||READ_ONCE (rdp_gp->nocb_gp_sleep)) {WRITE_ONCE (rdp_gp->nocb_gp_sleep, false);
@@ -815,10 +821,7 @@ static void nocb_gp_wait(struct rcu_data *my_rdp)if (rdp_toggling) my_rdp->nocb_toggling_rdp =NULL ; -if (my_rdp->nocb_defer_wakeup >RCU_NOCB_WAKE_NOT ) { -WRITE_ONCE (my_rdp->nocb_defer_wakeup,RCU_NOCB_WAKE_NOT ); -timer_delete (&my_rdp->nocb_timer); - } +nocb_defer_wakeup_cancel (my_rdp);WRITE_ONCE (my_rdp->nocb_gp_sleep, true);raw_spin_unlock_irqrestore (&my_rdp->nocb_gp_lock, flags); }else {
Signature · 1 line
2.34.1
Joel Fernandes
maintainerPATCH 2/3 rcu/nocb: Consolidate rcu_nocb_cpu_offload/deoffload functions
In reply to Joel FernandesThe rcu_nocb_cpu_offload() and rcu_nocb_cpu_deoffload() functions are nearly duplicates.
Therefore, extract the common logic into rcu_nocb_cpu_toggle_offload() which takes an 'offload' boolean, and make both exported functions simple wrappers.
This eliminates a bunch of duplicate code at the call sites, namely mutex locking, CPU hotplug locking and CPU online checks.
--- kernel/rcu/tree_nocb.h | 73 ++++++++++++++++++++++-------------------- 1 file changed, 38 insertions(+), 35 deletions(-)
kernel/rcu/tree_nocb.h +38 -35
@@ -1108,30 +1108,6 @@ static int rcu_nocb_rdp_deoffload(struct rcu_data *rdp)return 0 ; } -int rcu_nocb_cpu_deoffload (int cpu) -{ -struct rcu_data *rdp =per_cpu_ptr (&rcu_data, cpu); -int ret =0 ; - -cpus_read_lock (); -mutex_lock (&rcu_state.nocb_mutex); -if (rcu_rdp_is_offloaded (rdp)) { -if (!cpu_online (cpu)) { - ret =rcu_nocb_rdp_deoffload (rdp); -if (!ret) -cpumask_clear_cpu (cpu, rcu_nocb_mask); - }else { -pr_info ("NOCB: Cannot CB-deoffload online CPU %d\n" , rdp->cpu); - ret = -EINVAL ; - } - } -mutex_unlock (&rcu_state.nocb_mutex); -cpus_read_unlock (); - -return ret; -} -EXPORT_SYMBOL_GPL (rcu_nocb_cpu_deoffload); -static bool rcu_nocb_rdp_offload_wait_cond (struct rcu_data *rdp) {unsigned long flags;
@@ -1176,28 +1152,55 @@ static int rcu_nocb_rdp_offload(struct rcu_data *rdp)return 0 ; } -int rcu_nocb_cpu_offload (int cpu) +/* Common helper for CPU offload/deoffload operations. */ +static int rcu_nocb_cpu_toggle_offload (int cpu,bool offload) {struct rcu_data *rdp =per_cpu_ptr (&rcu_data, cpu); +bool currently_offloaded;int ret =0 ;cpus_read_lock ();mutex_lock (&rcu_state.nocb_mutex); -if (!rcu_rdp_is_offloaded (rdp)) { -if (!cpu_online (cpu)) { - ret =rcu_nocb_rdp_offload (rdp); -if (!ret) -cpumask_set_cpu (cpu, rcu_nocb_mask); - }else { -pr_info ("NOCB: Cannot CB-offload online CPU %d\n" , rdp->cpu); - ret = -EINVAL ; - } + + currently_offloaded =rcu_rdp_is_offloaded (rdp); + +/* Already in desired state, nothing to do. */ +if (currently_offloaded == offload) + goto out_unlock; + +if (cpu_online (cpu)) { +pr_info ("NOCB: Cannot CB-%soffload online CPU %d\n" , + offload ?"" :"de" , rdp->cpu); + ret = -EINVAL ; + goto out_unlock; } + +if (offload) { + ret =rcu_nocb_rdp_offload (rdp); +if (!ret) +cpumask_set_cpu (cpu, rcu_nocb_mask); + }else { + ret =rcu_nocb_rdp_deoffload (rdp); +if (!ret) +cpumask_clear_cpu (cpu, rcu_nocb_mask); + } + +out_unlock:mutex_unlock (&rcu_state.nocb_mutex);cpus_read_unlock (); -return ret; } + +int rcu_nocb_cpu_deoffload (int cpu) +{ +return rcu_nocb_cpu_toggle_offload (cpu, false/* de-offload */ ); +} +EXPORT_SYMBOL_GPL (rcu_nocb_cpu_deoffload); + +int rcu_nocb_cpu_offload (int cpu) +{ +return rcu_nocb_cpu_toggle_offload (cpu, true/* offload */ ); +}EXPORT_SYMBOL_GPL (rcu_nocb_cpu_offload);#ifdef CONFIG_RCU_LAZY
Signature · 1 line
2.34.1
Joel Fernandes
maintainerPATCH 3/3 rcu/nocb: Extract nocb_bypass_needs_flush() to reduce duplication
In reply to Joel FernandesThe bypass flush decision logic is duplicated in rcu_nocb_try_bypass() and nocb_gp_wait() with similar conditions.
This commit therefore extracts the functionality into a common helper function nocb_bypass_needs_flush() improving the code readability.
A flush_faster parameter is added to controlling the flushing thresholds and timeouts. This design was in the original commit d1b222c6be1f ("rcu/nocb: Add bypass callback queueing") to avoid having the GP kthread aggressively flush the bypass queue.
--- kernel/rcu/tree_nocb.h | 51 ++++++++++++++++++++++++++++++------------ 1 file changed, 37 insertions(+), 14 deletions(-)
kernel/rcu/tree_nocb.h +37 -14
@@ -378,6 +378,38 @@ static void rcu_nocb_try_flush_bypass(struct rcu_data *rdp, unsigned long j)WARN_ON_ONCE (!rcu_nocb_do_flush_bypass (rdp,NULL , j, false)); } +/* +* Determine if the bypass queue needs to be flushed based on time and size. +* For lazy-only bypass queues, use the lazy flush timeout; otherwise flush +* based on jiffy advancement. The flush_faster controls flush aggressiveness. +*/ +static bool nocb_bypass_needs_flush (struct rcu_data *rdp,long bypass_ncbs, +long lazy_ncbs,unsigned long j, +bool flush_faster) +{ +bool bypass_is_lazy; +unsigned long bypass_first; +unsigned long flush_timeout; +long qhimark_thresh; + +if (!bypass_ncbs) +return false; + + qhimark_thresh = flush_faster ? qhimark :2 * qhimark; +if (bypass_ncbs >= qhimark_thresh) +return true; + + bypass_first =READ_ONCE (rdp->nocb_bypass_first); + bypass_is_lazy = (bypass_ncbs == lazy_ncbs); + +if (bypass_is_lazy) + flush_timeout =rcu_get_jiffies_lazy_flush (); +else + flush_timeout = flush_faster ?0 :1 ; + +return time_after (j, bypass_first + flush_timeout); +} +/* * See whether it is appropriate to use the ->nocb_bypass list in order * to control contention on ->nocb_lock. A limited number of direct
@@ -404,7 +436,8 @@ static bool rcu_nocb_try_bypass(struct rcu_data *rdp, struct rcu_head *rhp,unsigned long cur_gp_seq;unsigned long j = jiffies;long ncbs =rcu_cblist_n_cbs (&rdp->nocb_bypass); -bool bypass_is_lazy = (ncbs ==READ_ONCE (rdp->lazy_len)); +long lazy_len =READ_ONCE (rdp->lazy_len); +bool bypass_is_lazy = (ncbs == lazy_len);lockdep_assert_irqs_disabled ();
@@ -456,10 +489,7 @@ static bool rcu_nocb_try_bypass(struct rcu_data *rdp, struct rcu_head *rhp,// If ->nocb_bypass has been used too long or is too full, // flush ->nocb_bypass to ->cblist. -if ((ncbs && !bypass_is_lazy && j !=READ_ONCE (rdp->nocb_bypass_first)) || - (ncbs && bypass_is_lazy && - (time_after (j,READ_ONCE (rdp->nocb_bypass_first) +rcu_get_jiffies_lazy_flush ()))) || - ncbs >= qhimark) { +if (nocb_bypass_needs_flush (rdp, ncbs, lazy_len, j, true)) {rcu_nocb_lock (rdp); *was_alldone = !rcu_segcblist_pend_cbs (&rdp->cblist);
@@ -699,15 +729,8 @@ static void nocb_gp_wait(struct rcu_data *my_rdp) bypass_ncbs =rcu_cblist_n_cbs (&rdp->nocb_bypass); lazy_ncbs =READ_ONCE (rdp->lazy_len); -if (bypass_ncbs && (lazy_ncbs == bypass_ncbs) && - (time_after (j,READ_ONCE (rdp->nocb_bypass_first) +rcu_get_jiffies_lazy_flush ()) || - bypass_ncbs >2 * qhimark)) { - flush_bypass = true; - }else if (bypass_ncbs && (lazy_ncbs != bypass_ncbs) && - (time_after (j,READ_ONCE (rdp->nocb_bypass_first) +1 ) || - bypass_ncbs >2 * qhimark)) { - flush_bypass = true; - }else if (!bypass_ncbs &&rcu_segcblist_empty (&rdp->cblist)) { + flush_bypass =nocb_bypass_needs_flush (rdp, bypass_ncbs, lazy_ncbs, j, false); +if (!flush_bypass && !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. */
Signature · 1 line
2.34.1