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>,
	Mathieu Desnoyers <mathieu.desnoyers@efficios.com>,
	Sebastian Andrzej Siewior <bigeasy@linutronix.de>,
	bpf@vger.kernel.org
Subject: [PATCH 27/34] srcu: Make DEFINE_SRCU_FAST() available to modules
Date: Tue, 23 Sep 2025 07:20:29 -0700	[thread overview]
Message-ID: <20250923142036.112290-27-paulmck@kernel.org> (raw)
In-Reply-To: <580ea2de-799a-4ddc-bde9-c16f3fb1e6e7@paulmck-laptop>

This commit makes DEFINE_SRCU_FAST() available to modules.  The trick
is that DEFINE_SRCU_FAST() does not allocate the per-CPU data structure
when used within a module (by design), which means that it is not possible
to compile-time-initialize any of the fields in this structure, including
in particular the ->srcu_reader_flavor field.

This commit therefore creates an ->srcu_reader_flavor field in the
srcu_struct structure, adds arguments to the DEFINE_SRCU()-related
macros to initialize this new field, and extending the checks in the
__srcu_check_read_flavor() function to include this new field.

Signed-off-by: Paul E. McKenney <paulmck@kernel.org>
Cc: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
Cc: Steven Rostedt <rostedt@goodmis.org>
Cc: Sebastian Andrzej Siewior <bigeasy@linutronix.de>
Cc: <bpf@vger.kernel.org>
---
 include/linux/notifier.h |  2 +-
 include/linux/srcutiny.h | 10 ++++++----
 include/linux/srcutree.h | 24 +++++++++++++-----------
 kernel/rcu/srcutree.c    |  3 +++
 4 files changed, 23 insertions(+), 16 deletions(-)

diff --git a/include/linux/notifier.h b/include/linux/notifier.h
index b42e6473496882..01b6c9d9956f90 100644
--- a/include/linux/notifier.h
+++ b/include/linux/notifier.h
@@ -109,7 +109,7 @@ extern void srcu_init_notifier_head(struct srcu_notifier_head *nh);
 		.mutex = __MUTEX_INITIALIZER(name.mutex),	\
 		.head = NULL,					\
 		.srcuu = __SRCU_USAGE_INIT(name.srcuu),		\
-		.srcu = __SRCU_STRUCT_INIT(name.srcu, name.srcuu, pcpu), \
+		.srcu = __SRCU_STRUCT_INIT(name.srcu, name.srcuu, pcpu, 0), \
 	}
 
 #define ATOMIC_NOTIFIER_HEAD(name)				\
diff --git a/include/linux/srcutiny.h b/include/linux/srcutiny.h
index 941e8210479607..b5f62259d56fb1 100644
--- a/include/linux/srcutiny.h
+++ b/include/linux/srcutiny.h
@@ -31,7 +31,7 @@ struct srcu_struct {
 
 void srcu_drive_gp(struct work_struct *wp);
 
-#define __SRCU_STRUCT_INIT(name, __ignored, ___ignored)			\
+#define __SRCU_STRUCT_INIT(name, __ignored, ___ignored, ____ignored)	\
 {									\
 	.srcu_wq = __SWAIT_QUEUE_HEAD_INITIALIZER(name.srcu_wq),	\
 	.srcu_cb_tail = &name.srcu_cb_head,				\
@@ -44,10 +44,12 @@ void srcu_drive_gp(struct work_struct *wp);
  * Tree SRCU, which needs some per-CPU data.
  */
 #define DEFINE_SRCU(name) \
-	struct srcu_struct name = __SRCU_STRUCT_INIT(name, name, name)
-#define DEFINE_SRCU_FAST(name) DEFINE_SRCU(name)
+	struct srcu_struct name = __SRCU_STRUCT_INIT(name, name, name, name)
 #define DEFINE_STATIC_SRCU(name) \
-	static struct srcu_struct name = __SRCU_STRUCT_INIT(name, name, name)
+	static struct srcu_struct name = __SRCU_STRUCT_INIT(name, name, name, name)
+#define DEFINE_SRCU_FAST(name) DEFINE_SRCU(name)
+#define DEFINE_STATIC_SRCU_FAST(name) \
+	static struct srcu_struct name = __SRCU_STRUCT_INIT(name, name, name, name)
 
 // Dummy structure for srcu_notifier_head.
 struct srcu_usage { };
diff --git a/include/linux/srcutree.h b/include/linux/srcutree.h
index 4a5d1c0e7db361..05400f70baa40a 100644
--- a/include/linux/srcutree.h
+++ b/include/linux/srcutree.h
@@ -104,6 +104,7 @@ struct srcu_usage {
 struct srcu_struct {
 	struct srcu_ctr __percpu *srcu_ctrp;
 	struct srcu_data __percpu *sda;		/* Per-CPU srcu_data array. */
+	u8 srcu_reader_flavor;
 	struct lockdep_map dep_map;
 	struct srcu_usage *srcu_sup;		/* Update-side data. */
 };
@@ -162,20 +163,21 @@ struct srcu_struct {
 	.work = __DELAYED_WORK_INITIALIZER(name.work, NULL, 0),					\
 }
 
-#define __SRCU_STRUCT_INIT_COMMON(name, usage_name)						\
+#define __SRCU_STRUCT_INIT_COMMON(name, usage_name, fast)					\
 	.srcu_sup = &usage_name,								\
+	.srcu_reader_flavor = fast,								\
 	__SRCU_DEP_MAP_INIT(name)
 
-#define __SRCU_STRUCT_INIT_MODULE(name, usage_name)						\
+#define __SRCU_STRUCT_INIT_MODULE(name, usage_name, fast)					\
 {												\
-	__SRCU_STRUCT_INIT_COMMON(name, usage_name)						\
+	__SRCU_STRUCT_INIT_COMMON(name, usage_name, fast)					\
 }
 
-#define __SRCU_STRUCT_INIT(name, usage_name, pcpu_name)						\
+#define __SRCU_STRUCT_INIT(name, usage_name, pcpu_name, fast)					\
 {												\
 	.sda = &pcpu_name,									\
 	.srcu_ctrp = &pcpu_name.srcu_ctrs[0],							\
-	__SRCU_STRUCT_INIT_COMMON(name, usage_name)						\
+	__SRCU_STRUCT_INIT_COMMON(name, usage_name, fast)						\
 }
 
 /*
@@ -207,22 +209,22 @@ struct srcu_struct {
 #ifdef MODULE
 # define __DEFINE_SRCU(name, fast, is_static)							\
 	static struct srcu_usage name##_srcu_usage = __SRCU_USAGE_INIT(name##_srcu_usage);	\
-	is_static struct srcu_struct name = __SRCU_STRUCT_INIT_MODULE(name, name##_srcu_usage);	\
+	is_static struct srcu_struct name = __SRCU_STRUCT_INIT_MODULE(name, name##_srcu_usage,	\
+								      fast);			\
 	extern struct srcu_struct * const __srcu_struct_##name;					\
 	struct srcu_struct * const __srcu_struct_##name						\
 		__section("___srcu_struct_ptrs") = &name
 #else
 # define __DEFINE_SRCU(name, fast, is_static)							\
-	static DEFINE_PER_CPU(struct srcu_data, name##_srcu_data) = {				\
-		.srcu_reader_flavor = fast,							\
-	};											\
+	static DEFINE_PER_CPU(struct srcu_data, name##_srcu_data);				\
 	static struct srcu_usage name##_srcu_usage = __SRCU_USAGE_INIT(name##_srcu_usage);	\
 	is_static struct srcu_struct name =							\
-		__SRCU_STRUCT_INIT(name, name##_srcu_usage, name##_srcu_data)
-#define DEFINE_SRCU_FAST(name)		__DEFINE_SRCU(name, SRCU_READ_FLAVOR_FAST, /* not static */)
+		__SRCU_STRUCT_INIT(name, name##_srcu_usage, name##_srcu_data, fast)
 #endif
 #define DEFINE_SRCU(name)		__DEFINE_SRCU(name, 0, /* not static */)
 #define DEFINE_STATIC_SRCU(name)	__DEFINE_SRCU(name, 0, static)
+#define DEFINE_SRCU_FAST(name)		__DEFINE_SRCU(name, SRCU_READ_FLAVOR_FAST, /* not static */)
+#define DEFINE_STATIC_SRCU_FAST(name)	__DEFINE_SRCU(name, SRCU_READ_FLAVOR_FAST, static)
 
 int __srcu_read_lock(struct srcu_struct *ssp) __acquires(ssp);
 void synchronize_srcu_expedited(struct srcu_struct *ssp);
diff --git a/kernel/rcu/srcutree.c b/kernel/rcu/srcutree.c
index f2f11492e6936e..7c8b39d4dd3162 100644
--- a/kernel/rcu/srcutree.c
+++ b/kernel/rcu/srcutree.c
@@ -734,6 +734,9 @@ void __srcu_check_read_flavor(struct srcu_struct *ssp, int read_flavor)
 
 	sdp = raw_cpu_ptr(ssp->sda);
 	old_read_flavor = READ_ONCE(sdp->srcu_reader_flavor);
+	WARN_ON_ONCE(ssp->srcu_reader_flavor && read_flavor != ssp->srcu_reader_flavor);
+	WARN_ON_ONCE(old_read_flavor && ssp->srcu_reader_flavor &&
+		     old_read_flavor != ssp->srcu_reader_flavor);
 	if (!old_read_flavor) {
 		old_read_flavor = cmpxchg(&sdp->srcu_reader_flavor, 0, read_flavor);
 		if (!old_read_flavor)
-- 
2.40.1


  parent reply	other threads:[~2025-09-23 14:21 UTC|newest]

Thread overview: 48+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-09-23 14:20 [PATCH 0/34] Implement RCU Tasks Trace in terms of SRCU-fast and optimize Paul E. McKenney
2025-09-23 14:20 ` [PATCH 01/34] rcu: Re-implement RCU Tasks Trace in terms of SRCU-fast Paul E. McKenney
2025-09-25 18:39   ` Andrii Nakryiko
2025-09-25 18:58     ` Paul E. McKenney
2025-09-23 14:20 ` [PATCH 02/34] rcu: Remove unused ->trc_ipi_to_cpu and ->trc_blkd_cpu from task_struct Paul E. McKenney
2025-09-23 14:20 ` [PATCH 03/34] rcu: Remove ->trc_blkd_node " Paul E. McKenney
2025-09-23 14:20 ` [PATCH 04/34] rcu: Remove ->trc_holdout_list " Paul E. McKenney
2025-09-23 14:20 ` [PATCH 05/34] rcu: Remove rcu_tasks_trace_qs() and the functions that it calls Paul E. McKenney
2025-09-23 14:20 ` [PATCH 06/34] context_tracking: Remove rcu_task_trace_heavyweight_{enter,exit}() Paul E. McKenney
2025-09-23 14:20 ` [PATCH 07/34] rcu: Remove ->trc_reader_special from task_struct Paul E. McKenney
2025-09-23 14:20 ` [PATCH 08/34] rcu: Remove now-empty RCU Tasks Trace functions and calls to them Paul E. McKenney
2025-09-23 14:20 ` [PATCH 09/34] rcu: Remove unused rcu_tasks_trace_lazy_ms and trc_stall_chk_rdr struct Paul E. McKenney
2025-09-23 14:20 ` [PATCH 10/34] rcu: Remove now-empty show_rcu_tasks_trace_gp_kthread() function Paul E. McKenney
2025-09-23 14:20 ` [PATCH 11/34] rcu: Remove now-empty rcu_tasks_trace_get_gp_data() function Paul E. McKenney
2025-09-23 14:20 ` [PATCH 12/34] rcu: Remove now-empty rcu_tasks_trace_torture_stats_print() function Paul E. McKenney
2025-09-23 14:20 ` [PATCH 13/34] rcu: Remove now-empty get_rcu_tasks_trace_gp_kthread() function Paul E. McKenney
2025-09-23 14:20 ` [PATCH 14/34] rcu: Move rcu_tasks_trace_srcu_struct out of #ifdef CONFIG_TASKS_RCU_GENERIC Paul E. McKenney
2025-09-23 14:20 ` [PATCH 15/34] rcu: Add noinstr-fast rcu_read_{,un}lock_tasks_trace() APIs Paul E. McKenney
2025-09-23 17:32   ` Peter Zijlstra
2025-09-24  8:44     ` Paul E. McKenney
2025-09-24  9:08       ` Peter Zijlstra
2025-09-23 14:20 ` [PATCH 16/34] rcu: Remove now-unused rcu_task_ipi_delay and TASKS_TRACE_RCU_READ_MB Paul E. McKenney
2025-09-23 14:20 ` [PATCH 17/34] srcu: Create a DEFINE_SRCU_FAST() Paul E. McKenney
2025-09-23 14:20 ` [PATCH 18/34] rcu: Use smp_mb() only when necessary in RCU Tasks Trace readers Paul E. McKenney
2025-09-23 14:20 ` [PATCH 19/34] rcu: Update Requirements.rst for RCU Tasks Trace Paul E. McKenney
2025-09-25 18:40   ` Andrii Nakryiko
2025-09-25 18:53     ` Paul E. McKenney
2025-09-23 14:20 ` [PATCH 20/34] checkpatch: Deprecate rcu_read_{,un}lock_trace() Paul E. McKenney
2025-09-23 15:47   ` Joe Perches
2025-09-23 17:01     ` Paul E. McKenney
2025-09-23 14:20 ` [PATCH 21/34] rcu: Mark diagnostic functions as notrace Paul E. McKenney
2025-09-23 14:20 ` [PATCH 22/34] tracing: Guard __DECLARE_TRACE() use of __DO_TRACE_CALL() with SRCU-fast Paul E. McKenney
2025-09-23 14:20 ` [PATCH 23/34] srcu: Create an srcu_expedite_current() function Paul E. McKenney
2025-09-24  0:10   ` Zqiang
2025-09-24  8:56     ` Paul E. McKenney
2025-09-23 14:20 ` [PATCH 24/34] rcutorture: Test srcu_expedite_current() Paul E. McKenney
2025-09-23 14:20 ` [PATCH 25/34] srcu: Create an rcu_tasks_trace_expedite_current() function Paul E. McKenney
2025-09-23 14:20 ` [PATCH 26/34] rcutorture: Test rcu_tasks_trace_expedite_current() Paul E. McKenney
2025-09-23 14:20 ` Paul E. McKenney [this message]
2025-09-23 14:20 ` [PATCH 28/34] srcu: Make SRCU-fast available to heap srcu_struct structures Paul E. McKenney
2025-09-23 14:20 ` [PATCH 29/34] srcu: Make grace-period determination use ssp->srcu_reader_flavor Paul E. McKenney
2025-09-23 14:20 ` [PATCH 30/34] rcutorture: Exercise DEFINE_STATIC_SRCU_FAST() and init_srcu_struct_fast() Paul E. McKenney
2025-09-23 14:20 ` [PATCH 31/34] refscale: " Paul E. McKenney
2025-09-23 14:20 ` [PATCH 32/34] srcu: Require special srcu_struct define/init for SRCU-fast readers Paul E. McKenney
2025-09-23 14:20 ` [PATCH 33/34] srcu: Make SRCU-fast readers enforce use of SRCU-fast definition/init Paul E. McKenney
2025-09-23 14:20 ` [PATCH 34/34] doc: Update for SRCU-fast definitions and initialization Paul E. McKenney
2025-09-24  7:49 ` [PATCH 0/34] Implement RCU Tasks Trace in terms of SRCU-fast and optimize Alexei Starovoitov
2025-09-24  8:20   ` 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=20250923142036.112290-27-paulmck@kernel.org \
    --to=paulmck@kernel.org \
    --cc=bigeasy@linutronix.de \
    --cc=bpf@vger.kernel.org \
    --cc=kernel-team@meta.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mathieu.desnoyers@efficios.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