Linux RCU subsystem development
 help / color / mirror / Atom feed
From: "Paul E. McKenney" <paulmck@kernel.org>
To: rcu@vger.kernel.org
Cc: linux-kernel@vger.kernel.org, kernel-team@meta.com,
	rostedt@goodmis.org, "Paul E. McKenney" <paulmck@kernel.org>,
	Z qiang <qiang.zhang1211@gmail.com>,
	kernel test robot <oliver.sang@intel.com>,
	Alexei Starovoitov <ast@kernel.org>,
	Andrii Nakryiko <andrii@kernel.org>,
	Peter Zijlstra <peterz@infradead.org>,
	Kent Overstreet <kent.overstreet@linux.dev>,
	bpf@vger.kernel.org
Subject: [PATCH rcu v2] 05/20] srcu: Make SRCU readers use ->srcu_ctrs for counter selection
Date: Thu, 30 Jan 2025 11:03:02 -0800	[thread overview]
Message-ID: <20250130190317.1652481-5-paulmck@kernel.org> (raw)
In-Reply-To: <1034ef54-b6b3-42bb-9bd8-4c37c164950d@paulmck-laptop>

This commit causes SRCU readers to use ->srcu_ctrs for counter
selection instead of ->srcu_idx.  This takes another step towards
array-indexing-free SRCU readers.

[ paulmck: Apply kernel test robot feedback. ]

Co-developed-by: Z qiang <qiang.zhang1211@gmail.com>
Signed-off-by: Z qiang <qiang.zhang1211@gmail.com>
Signed-off-by: Paul E. McKenney <paulmck@kernel.org>
Tested-by: kernel test robot <oliver.sang@intel.com>
Cc: Alexei Starovoitov <ast@kernel.org>
Cc: Andrii Nakryiko <andrii@kernel.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Kent Overstreet <kent.overstreet@linux.dev>
Cc: <bpf@vger.kernel.org>
---
 include/linux/srcutree.h |  9 +++++----
 kernel/rcu/srcutree.c    | 23 +++++++++++++----------
 2 files changed, 18 insertions(+), 14 deletions(-)

diff --git a/include/linux/srcutree.h b/include/linux/srcutree.h
index c794d599db5c..1b01ced61a45 100644
--- a/include/linux/srcutree.h
+++ b/include/linux/srcutree.h
@@ -101,6 +101,7 @@ struct srcu_usage {
  */
 struct srcu_struct {
 	unsigned int srcu_idx;			/* Current rdr array element. */
+	struct srcu_ctr __percpu *srcu_ctrp;
 	struct srcu_data __percpu *sda;		/* Per-CPU srcu_data array. */
 	struct lockdep_map dep_map;
 	struct srcu_usage *srcu_sup;		/* Update-side data. */
@@ -167,6 +168,7 @@ struct srcu_struct {
 #define __SRCU_STRUCT_INIT(name, usage_name, pcpu_name)						\
 {												\
 	.sda = &pcpu_name,									\
+	.srcu_ctrp = &pcpu_name.srcu_ctrs[0],							\
 	__SRCU_STRUCT_INIT_COMMON(name, usage_name)						\
 }
 
@@ -222,13 +224,12 @@ void srcu_torture_stats_print(struct srcu_struct *ssp, char *tt, char *tf);
  */
 static inline int __srcu_read_lock_lite(struct srcu_struct *ssp)
 {
-	int idx;
+	struct srcu_ctr __percpu *scp = READ_ONCE(ssp->srcu_ctrp);
 
 	RCU_LOCKDEP_WARN(!rcu_is_watching(), "RCU must be watching srcu_read_lock_lite().");
-	idx = READ_ONCE(ssp->srcu_idx) & 0x1;
-	this_cpu_inc(ssp->sda->srcu_ctrs[idx].srcu_locks.counter); /* Y */
+	this_cpu_inc(scp->srcu_locks.counter); /* Y */
 	barrier(); /* Avoid leaking the critical section. */
-	return idx;
+	return scp - &ssp->sda->srcu_ctrs[0];
 }
 
 /*
diff --git a/kernel/rcu/srcutree.c b/kernel/rcu/srcutree.c
index d4e9cd917a69..9af86ce2dd24 100644
--- a/kernel/rcu/srcutree.c
+++ b/kernel/rcu/srcutree.c
@@ -253,8 +253,10 @@ static int init_srcu_struct_fields(struct srcu_struct *ssp, bool is_static)
 	atomic_set(&ssp->srcu_sup->srcu_barrier_cpu_cnt, 0);
 	INIT_DELAYED_WORK(&ssp->srcu_sup->work, process_srcu);
 	ssp->srcu_sup->sda_is_static = is_static;
-	if (!is_static)
+	if (!is_static) {
 		ssp->sda = alloc_percpu(struct srcu_data);
+		ssp->srcu_ctrp = &ssp->sda->srcu_ctrs[0];
+	}
 	if (!ssp->sda)
 		goto err_free_sup;
 	init_srcu_struct_data(ssp);
@@ -742,12 +744,11 @@ EXPORT_SYMBOL_GPL(__srcu_check_read_flavor);
  */
 int __srcu_read_lock(struct srcu_struct *ssp)
 {
-	int idx;
+	struct srcu_ctr __percpu *scp = READ_ONCE(ssp->srcu_ctrp);
 
-	idx = READ_ONCE(ssp->srcu_idx) & 0x1;
-	this_cpu_inc(ssp->sda->srcu_ctrs[idx].srcu_locks.counter);
+	this_cpu_inc(scp->srcu_locks.counter);
 	smp_mb(); /* B */  /* Avoid leaking the critical section. */
-	return idx;
+	return scp - &ssp->sda->srcu_ctrs[0];
 }
 EXPORT_SYMBOL_GPL(__srcu_read_lock);
 
@@ -772,13 +773,12 @@ EXPORT_SYMBOL_GPL(__srcu_read_unlock);
  */
 int __srcu_read_lock_nmisafe(struct srcu_struct *ssp)
 {
-	int idx;
-	struct srcu_data *sdp = raw_cpu_ptr(ssp->sda);
+	struct srcu_ctr __percpu *scpp = READ_ONCE(ssp->srcu_ctrp);
+	struct srcu_ctr *scp = raw_cpu_ptr(scpp);
 
-	idx = READ_ONCE(ssp->srcu_idx) & 0x1;
-	atomic_long_inc(&sdp->srcu_ctrs[idx].srcu_locks);
+	atomic_long_inc(&scp->srcu_locks);
 	smp_mb__after_atomic(); /* B */  /* Avoid leaking the critical section. */
-	return idx;
+	return scpp - &ssp->sda->srcu_ctrs[0];
 }
 EXPORT_SYMBOL_GPL(__srcu_read_lock_nmisafe);
 
@@ -1152,6 +1152,8 @@ static void srcu_flip(struct srcu_struct *ssp)
 	smp_mb(); /* E */  /* Pairs with B and C. */
 
 	WRITE_ONCE(ssp->srcu_idx, ssp->srcu_idx + 1); // Flip the counter.
+	WRITE_ONCE(ssp->srcu_ctrp,
+		   &ssp->sda->srcu_ctrs[!(ssp->srcu_ctrp - &ssp->sda->srcu_ctrs[0])]);
 
 	/*
 	 * Ensure that if the updater misses an __srcu_read_unlock()
@@ -2004,6 +2006,7 @@ static int srcu_module_coming(struct module *mod)
 		ssp->sda = alloc_percpu(struct srcu_data);
 		if (WARN_ON_ONCE(!ssp->sda))
 			return -ENOMEM;
+		ssp->srcu_ctrp = &ssp->sda->srcu_ctrs[0];
 	}
 	return 0;
 }
-- 
2.40.1


  parent reply	other threads:[~2025-01-30 19:03 UTC|newest]

Thread overview: 47+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-01-16 20:21 [PATCH rcu 0/17] SRCU updates, including SRCU-fast Paul E. McKenney
2025-01-16 20:20 ` [PATCH rcu 01/17] srcu: Make Tiny SRCU able to operate in preemptible kernels Paul E. McKenney
2025-01-16 20:20 ` [PATCH rcu 02/17] srcu: Define SRCU_READ_FLAVOR_ALL in terms of symbols Paul E. McKenney
2025-01-16 20:20 ` [PATCH rcu 03/17] srcu: Use ->srcu_gp_seq for rcutorture reader batch Paul E. McKenney
2025-01-16 20:20 ` [PATCH rcu 04/17] srcu: Pull ->srcu_{un,}lock_count into a new srcu_ctr structure Paul E. McKenney
2025-01-16 20:21 ` [PATCH rcu 05/17] srcu: Make SRCU readers use ->srcu_ctrs for counter selection Paul E. McKenney
2025-01-16 20:21 ` [PATCH rcu 06/17] srcu: Make Tree SRCU updates independent of ->srcu_idx Paul E. McKenney
2025-01-16 20:21 ` [PATCH rcu 07/17] srcu: Force synchronization for srcu_get_delay() Paul E. McKenney
2025-01-16 20:21 ` [PATCH rcu 08/17] srcu: Rename srcu_check_read_flavor_lite() to srcu_check_read_flavor_force() Paul E. McKenney
2025-01-16 20:21 ` [PATCH rcu 09/17] srcu: Add SRCU_READ_FLAVOR_SLOWGP to flag need for synchronize_rcu() Paul E. McKenney
2025-01-16 20:21 ` [PATCH rcu 10/17] srcu: Pull pointer-to-integer conversion into __srcu_ptr_to_ctr() Paul E. McKenney
2025-01-16 20:21 ` [PATCH rcu 11/17] srcu: Pull integer-to-pointer conversion into __srcu_ctr_to_ptr() Paul E. McKenney
2025-01-16 20:21 ` [PATCH rcu 12/17] srcu: Move SRCU Tree/Tiny definitions from srcu.h Paul E. McKenney
2025-01-16 20:21 ` [PATCH rcu 13/17] srcu: Add SRCU-fast readers Paul E. McKenney
2025-01-16 21:00   ` Alexei Starovoitov
2025-01-16 21:38     ` Peter Zijlstra
2025-01-16 21:55     ` Paul E. McKenney
2025-01-16 22:58       ` Alexei Starovoitov
2025-01-16 21:52   ` Andrii Nakryiko
2025-01-16 22:54     ` Paul E. McKenney
2025-01-16 22:57       ` Andrii Nakryiko
2025-01-17  0:07         ` Paul E. McKenney
2025-01-16 20:21 ` [PATCH rcu 14/17] rcutorture: Add ability to test srcu_read_{,un}lock_fast() Paul E. McKenney
2025-01-16 20:21 ` [PATCH rcu 15/17] refscale: Add srcu_read_lock_fast() support using "srcu-fast" Paul E. McKenney
2025-01-16 20:21 ` [PATCH rcu 16/17] rcutorture: Make scenario SRCU-P use srcu_read_lock_fast() Paul E. McKenney
2025-01-16 20:21 ` [PATCH rcu 17/17] srcu: Fix srcu_read_unlock_{lite,nmisafe}() kernel-doc Paul E. McKenney
2025-01-30 19:03 ` [PATCH rcu 0/17] SRCU updates, including SRCU-fast Paul E. McKenney
2025-01-30 19:02   ` [PATCH rcu v2] 01/20] srcu: Make Tiny SRCU able to operate in preemptible kernels Paul E. McKenney
2025-01-30 19:02   ` [PATCH rcu v2] 02/20] srcu: Define SRCU_READ_FLAVOR_ALL in terms of symbols Paul E. McKenney
2025-01-30 19:03   ` [PATCH rcu v2] 03/20] srcu: Use ->srcu_gp_seq for rcutorture reader batch Paul E. McKenney
2025-01-30 19:03   ` [PATCH rcu v2] 04/20] srcu: Pull ->srcu_{un,}lock_count into a new srcu_ctr structure Paul E. McKenney
2025-01-30 19:03   ` Paul E. McKenney [this message]
2025-01-30 19:03   ` [PATCH rcu v2] 06/20] srcu: Make Tree SRCU updates independent of ->srcu_idx Paul E. McKenney
2025-01-30 19:03   ` [PATCH rcu v2] 07/20] srcu: Force synchronization for srcu_get_delay() Paul E. McKenney
2025-01-30 19:03   ` [PATCH rcu v2] 08/20] srcu: Rename srcu_check_read_flavor_lite() to srcu_check_read_flavor_force() Paul E. McKenney
2025-01-30 19:03   ` [PATCH rcu v2] 09/20] srcu: Add SRCU_READ_FLAVOR_SLOWGP to flag need for synchronize_rcu() Paul E. McKenney
2025-01-30 19:03   ` [PATCH rcu v2] 10/20] srcu: Pull pointer-to-integer conversion into __srcu_ptr_to_ctr() Paul E. McKenney
2025-01-30 19:03   ` [PATCH rcu v2] 11/20] srcu: Pull integer-to-pointer conversion into __srcu_ctr_to_ptr() Paul E. McKenney
2025-01-30 19:03   ` [PATCH rcu v2] 12/20] srcu: Move SRCU Tree/Tiny definitions from srcu.h Paul E. McKenney
2025-01-30 19:03   ` [PATCH rcu v2] 13/20] srcu: Add SRCU-fast readers Paul E. McKenney
2025-01-30 19:03   ` [PATCH rcu v2] 14/20] rcutorture: Add ability to test srcu_read_{,un}lock_fast() Paul E. McKenney
2025-01-30 19:03   ` [PATCH rcu v2] 15/20] refscale: Add srcu_read_lock_fast() support using "srcu-fast" Paul E. McKenney
2025-01-30 19:03   ` [PATCH rcu v2] 16/20] rcutorture: Make scenario SRCU-P use srcu_read_lock_fast() Paul E. McKenney
2025-01-30 19:03   ` [PATCH rcu v2] 17/20] srcu: Fix srcu_read_unlock_{lite,nmisafe}() kernel-doc Paul E. McKenney
2025-01-30 19:03   ` [PATCH rcu v2] 18/20] srcu: Document that srcu_{read_lock,down_read}() can share srcu_struct Paul E. McKenney
2025-01-30 19:03   ` [PATCH rcu v2] 19/20] srcu: Add srcu_down_read_fast() and srcu_up_read_fast() Paul E. McKenney
2025-01-30 19:03   ` [PATCH rcu v2] 20/20] srcu: Make SRCU-fast also be NMI-safe Paul E. McKenney

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20250130190317.1652481-5-paulmck@kernel.org \
    --to=paulmck@kernel.org \
    --cc=andrii@kernel.org \
    --cc=ast@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=kent.overstreet@linux.dev \
    --cc=kernel-team@meta.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=oliver.sang@intel.com \
    --cc=peterz@infradead.org \
    --cc=qiang.zhang1211@gmail.com \
    --cc=rcu@vger.kernel.org \
    --cc=rostedt@goodmis.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox