Skip to contentSkip to conversation

rust: helpers: add SRCU helpers

PATCH Superseded Onur Özkan 3 participants 9 messages May 27

Subsystems (4)

v6 of 10 Superseded Latest is v10 →

lore
Share & tools
lore

Onur Özkan

author maintainer
#0 lore raw

v6 cover rust: helpers: add SRCU helpers

The immediate motivation is the Tyr reset infrastructure [1] which needs to serialize reset sensitive hardware access against reset and teardown paths. That reset series started to require many independent dependencies so this SRCU support is split out as a standalone Rust API to keep the reset series focused on the reset logic and easier to review, rebase and land.

Changes since v5:

- Created separate srcu_readers_active() variants for "srcutiny.h" and "srcutree.h".

v5: https://lore.kernel.org/all/[email protected]

Changes since v4:

- Exposed srcu_readers_active from C side and wired it to the Rust helpers.
- Used srcu_readers_active() in SRCU drop and logged with pr_warn if there are leaked guards during the drop.

v4: https://lore.kernel.org/all/[email protected]

Changes since v3 (which are for Sashiko notes [2]):

- Added rust helpers for srcu_barrier() and synchronize_srcu_expedited() so the abstraction builds with CONFIG_TINY_SRCU, where these are static inline functions.
- Added missing INVARIANT comment in Srcu::new() about why the type invariants hold after successful initialization.

v3: https://lore.kernel.org/all/[email protected]

Changes since v2:

- Removed closure-based API.
- Added #[doc(hidden)] on new_srcu macro.
- Added #[must_use..] on srcu::Guard.
- Improved the clean-up path (PinnedDrop implementation) which eventually made read_lock safe with leaked guards.

v2: https://lore.kernel.org/all/[email protected]

Changes since v1:

- Made the owned SRCU read-side guard API unsafe and added a safe closure based helper for callers that do not need to keep the guard. This is to avoid UB on the C side cleanup_srcu_struct where the SRCU struct is freed while there are still active guards, which can happen if the caller leaks the guard e.g., with mem::forget().
- Improved doc comments.

v1: https://lore.kernel.org/all/[email protected]

[1]: https://lore.kernel.org/all/[email protected]
[2]: https://sashiko.dev/#/patchset/[email protected]?part=2

Onur Özkan (4):
rust: helpers: add SRCU helpers
srcu: expose srcu_readers_active()
rust: sync: add SRCU abstraction
MAINTAINERS: add Rust SRCU files to SRCU entry

 MAINTAINERS              |   3 +
 include/linux/srcutiny.h |  13 +++
 include/linux/srcutree.h |  24 ++++++
 kernel/rcu/srcutiny.c    |   2 +-
 kernel/rcu/srcutree.c    |  25 ------
 rust/helpers/helpers.c   |   1 +
 rust/helpers/srcu.c      |  39 +++++++++
 rust/kernel/sync.rs      |   2 +
 rust/kernel/sync/srcu.rs | 166 +++++++++++++++++++++++++++++++++++++++
 9 files changed, 249 insertions(+), 26 deletions(-)
 create mode 100644 rust/helpers/srcu.c
 create mode 100644 rust/kernel/sync/srcu.rs
Signature · 1 line
2.51.2
author maintainer
#1 lore raw

PATCH v6 1/4 rust: helpers: add SRCU helpers

Add helper wrappers for SRCU functions that are exposed to Rust through generated bindings.

---
 rust/helpers/helpers.c |  1 +
 rust/helpers/srcu.c    | 34 ++++++++++++++++++++++++++++++++++
 2 files changed, 35 insertions(+)
 create mode 100644 rust/helpers/srcu.c
rust/helpers/helpers.c +1 -0
index 625921e27dfb..f3562d3b3888 100644
@@ -88,6 +88,7 @@
 #include "signal.c"
 #include "slab.c"
 #include "spinlock.c"
+#include "srcu.c"
 #include "sync.c"
 #include "task.c"
 #include "time.c"
rust/helpers/srcu.c new file +34 -0
new file mode 100644
index 000000000000..79dd24a104ef
@@ -0,0 +1,34 @@
+// SPDX-License-Identifier: GPL-2.0
+
+#include <linux/srcu.h>
+
+__rust_helper int rust_helper_init_srcu_struct_with_key(struct srcu_struct *ssp,
+							const char *name,
+							struct lock_class_key *key)
+{
+#ifdef CONFIG_DEBUG_LOCK_ALLOC
+	return __init_srcu_struct(ssp, name, key);
+#else /* !CONFIG_DEBUG_LOCK_ALLOC */
+	return init_srcu_struct(ssp);
+#endif /* CONFIG_DEBUG_LOCK_ALLOC */
+}
+
+__rust_helper int rust_helper_srcu_read_lock(struct srcu_struct *ssp)
+{
+	return srcu_read_lock(ssp);
+}
+
+__rust_helper void rust_helper_srcu_read_unlock(struct srcu_struct *ssp, int idx)
+{
+	srcu_read_unlock(ssp, idx);
+}
+
+__rust_helper void rust_helper_srcu_barrier(struct srcu_struct *ssp)
+{
+	srcu_barrier(ssp);
+}
+
+__rust_helper void rust_helper_synchronize_srcu_expedited(struct srcu_struct *ssp)
+{
+	synchronize_srcu_expedited(ssp);
+}
Signature · 1 line
2.51.2
author maintainer
#2 lore raw

PATCH v6 2/4 srcu: expose srcu_readers_active()

In reply to Onur Özkan

This is needed by rust/helpers/srcu.c which now adds rust_helper_srcu_readers_active() as a wrapper around the SRCU helper for Rust callers.

To achive this:

1- Move the srcu_readers_active() implementation from
"kernel/rcu/srcutree.c" to "include/linux/srcutree.h".

2- Implement a matching srcu_readers_active() in
"include/linux/srcutiny.h" and use it on the existing open-coded WARN_ON() check in cleanup_srcu_struct().

---
 include/linux/srcutiny.h | 13 +++++++++++++
 include/linux/srcutree.h | 24 ++++++++++++++++++++++++
 kernel/rcu/srcutiny.c    |  2 +-
 kernel/rcu/srcutree.c    | 25 -------------------------
 rust/helpers/srcu.c      |  5 +++++
 5 files changed, 43 insertions(+), 26 deletions(-)
include/linux/srcutiny.h +13 -0
index 905b629e8fa3..fbcf13bc12d1 100644
@@ -154,4 +154,17 @@ static inline void srcu_torture_stats_print(struct srcu_struct *ssp,
 		 data_race(READ_ONCE(ssp->srcu_idx_max)));
 }
 
+/**
+ * srcu_readers_active - returns true if there are readers. and false otherwise.
+ * @ssp: which srcu_struct to count active readers (holding srcu_read_lock).
+ *
+ * Note that this is not an atomic primitive, and can therefore suffer
+ * severe errors when invoked on an active srcu_struct. That said, it
+ * can be useful as an error check at cleanup time.
+ */
+static inline bool srcu_readers_active(struct srcu_struct *ssp)
+{
+	return READ_ONCE(ssp->srcu_lock_nesting[0]) || READ_ONCE(ssp->srcu_lock_nesting[1]);
+}
+
 #endif
include/linux/srcutree.h +24 -0
index fd1a9270cb9a..75e54e4f963f 100644
@@ -374,4 +374,28 @@ static inline void srcu_check_read_flavor(struct srcu_struct *ssp, int read_flav
 		__srcu_check_read_flavor(ssp, read_flavor);
 }
 
+/**
+ * srcu_readers_active - returns true if there are readers. and false otherwise.
+ * @ssp: which srcu_struct to count active readers (holding srcu_read_lock).
+ *
+ * Note that this is not an atomic primitive, and can therefore suffer
+ * severe errors when invoked on an active srcu_struct. That said, it
+ * can be useful as an error check at cleanup time.
+ */
+static inline bool srcu_readers_active(struct srcu_struct *ssp)
+{
+	int cpu;
+	unsigned long sum = 0;
+
+	for_each_possible_cpu(cpu) {
+		struct srcu_data *sdp = per_cpu_ptr(ssp->sda, cpu);
+
+		sum += atomic_long_read(&sdp->srcu_ctrs[0].srcu_locks);
+		sum += atomic_long_read(&sdp->srcu_ctrs[1].srcu_locks);
+		sum -= atomic_long_read(&sdp->srcu_ctrs[0].srcu_unlocks);
+		sum -= atomic_long_read(&sdp->srcu_ctrs[1].srcu_unlocks);
+	}
+	return sum;
+}
+
 #endif
kernel/rcu/srcutiny.c +1 -1
index a2e2d516e51b..5dc26af604bf 100644
@@ -85,7 +85,7 @@ EXPORT_SYMBOL_GPL(init_srcu_struct);
  */
 void cleanup_srcu_struct(struct srcu_struct *ssp)
 {
-	WARN_ON(ssp->srcu_lock_nesting[0] || ssp->srcu_lock_nesting[1]);
+	WARN_ON(srcu_readers_active(ssp));
 	irq_work_sync(&ssp->srcu_irq_work);
 	flush_work(&ssp->srcu_work);
 	WARN_ON(ssp->srcu_gp_running);
kernel/rcu/srcutree.c +0 -25
index 0d01cd8c4b4a..b1e97ba2e53f 100644
@@ -598,31 +598,6 @@ static bool srcu_readers_active_idx_check(struct srcu_struct *ssp, int idx)
 	return srcu_readers_lock_idx(ssp, idx, did_gp, unlocks);
 }
 
-/**
- * srcu_readers_active - returns true if there are readers. and false
- *                       otherwise
- * @ssp: which srcu_struct to count active readers (holding srcu_read_lock).
- *
- * Note that this is not an atomic primitive, and can therefore suffer
- * severe errors when invoked on an active srcu_struct.  That said, it
- * can be useful as an error check at cleanup time.
- */
-static bool srcu_readers_active(struct srcu_struct *ssp)
-{
-	int cpu;
-	unsigned long sum = 0;
-
-	for_each_possible_cpu(cpu) {
-		struct srcu_data *sdp = per_cpu_ptr(ssp->sda, cpu);
-
-		sum += atomic_long_read(&sdp->srcu_ctrs[0].srcu_locks);
-		sum += atomic_long_read(&sdp->srcu_ctrs[1].srcu_locks);
-		sum -= atomic_long_read(&sdp->srcu_ctrs[0].srcu_unlocks);
-		sum -= atomic_long_read(&sdp->srcu_ctrs[1].srcu_unlocks);
-	}
-	return sum;
-}
-
 /*
  * We use an adaptive strategy for synchronize_srcu() and especially for
  * synchronize_srcu_expedited().  We spin for a fixed time period
rust/helpers/srcu.c +5 -0
index 79dd24a104ef..fa4b5879dda5 100644
@@ -13,6 +13,11 @@ __rust_helper int rust_helper_init_srcu_struct_with_key(struct srcu_struct *ssp,
 #endif /* CONFIG_DEBUG_LOCK_ALLOC */
 }
 
+__rust_helper bool rust_helper_srcu_readers_active(struct srcu_struct *ssp)
+{
+	return srcu_readers_active(ssp);
+}
+
 __rust_helper int rust_helper_srcu_read_lock(struct srcu_struct *ssp)
 {
 	return srcu_read_lock(ssp);
Signature · 1 line
2.51.2
author maintainer
#3 lore raw

PATCH v6 3/4 rust: sync: add SRCU abstraction

In reply to Onur Özkan

Add a Rust abstraction for sleepable RCU (SRCU), backed by C srcu_struct. Provide FFI helpers and a safe wrapper with a guard-based API for read-side critical sections.

Cleanup is handled via PinnedDrop, which explicitly drains pending grace periods and callbacks via synchronize_srcu and srcu_barrier before executing cleanup_srcu_struct to guarantee memory safety e.g. when there are leaked guards (via mem::forget($guard)).

---
 rust/kernel/sync.rs      |   2 +
 rust/kernel/sync/srcu.rs | 166 +++++++++++++++++++++++++++++++++++++++
 2 files changed, 168 insertions(+)
 create mode 100644 rust/kernel/sync/srcu.rs
rust/kernel/sync.rs +2 -0
index 993dbf2caa0e..0d6a5f1300c3 100644
@@ -21,6 +21,7 @@
 pub mod rcu;
 mod refcount;
 mod set_once;
+pub mod srcu;
 
 pub use arc::{Arc, ArcBorrow, UniqueArc};
 pub use completion::Completion;
@@ -31,6 +32,7 @@
 pub use locked_by::LockedBy;
 pub use refcount::Refcount;
 pub use set_once::SetOnce;
+pub use srcu::Srcu;
 
 /// Represents a lockdep class.
 ///
rust/kernel/sync/srcu.rs new file +166 -0

166 lines not on this page · raw message

Signature · 1 line
2.51.2
author maintainer
#4 lore raw

PATCH v6 4/4 MAINTAINERS: add Rust SRCU files to SRCU entry

In reply to Onur Özkan

Include Rust side implementation files to the SRCU maintainer entry.

---
 MAINTAINERS | 3 +++
 1 file changed, 3 insertions(+)
MAINTAINERS +3 -0
index e0b307b2108c..7739a435f258 100644
@@ -24624,6 +24624,7 @@ SLEEPABLE READ-COPY UPDATE (SRCU)
 M:	Lai Jiangshan <[email protected]>
 M:	"Paul E. McKenney" <[email protected]>
 M:	Josh Triplett <[email protected]>
+M:	Onur Özkan <[email protected]> (RUST)
 R:	Steven Rostedt <[email protected]>
 R:	Mathieu Desnoyers <[email protected]>
 L:	[email protected]
@@ -24632,6 +24633,8 @@ W:	http://www.rdrop.com/users/paulmck/RCU/
 T:	git git://git.kernel.org/pub/scm/linux/kernel/git/rcu/linux.git rcu/dev
 F:	include/linux/srcu*.h
 F:	kernel/rcu/srcu*.c
+F:	rust/helpers/srcu.c
+F:	rust/kernel/sync/srcu.rs
 
 SMACK SECURITY MODULE
 M:	Casey Schaufler <[email protected]>
Signature · 1 line
2.51.2
maintainer
#5 lore raw

Re: [PATCH v6 4/4] MAINTAINERS: add Rust SRCU files to SRCU entry

Quoted 4 lines · Onur Özkan

Include Rust side implementation files to the SRCU maintainer entry.

@Paul, could you help review this? Also the patch #2 since it changes the C side.

Of course, your feedbacks will be much appreciated for the whole series as well.

Regards,
Boqun

Quoted 28 lines
---
 MAINTAINERS | 3 +++
 1 file changed, 3 insertions(+)
MAINTAINERS +3 -0
index e0b307b2108c..7739a435f258 100644
@@ -24624,6 +24624,7 @@ SLEEPABLE READ-COPY UPDATE (SRCU)
 M:	Lai Jiangshan <[email protected]>
 M:	"Paul E. McKenney" <[email protected]>
 M:	Josh Triplett <[email protected]>
+M:	Onur Özkan <[email protected]> (RUST)
 R:	Steven Rostedt <[email protected]>
 R:	Mathieu Desnoyers <[email protected]>
 L:	[email protected]
@@ -24632,6 +24633,8 @@ W:	http://www.rdrop.com/users/paulmck/RCU/
 T:	git git://git.kernel.org/pub/scm/linux/kernel/git/rcu/linux.git rcu/dev
 F:	include/linux/srcu*.h
 F:	kernel/rcu/srcu*.c
+F:	rust/helpers/srcu.c
+F:	rust/kernel/sync/srcu.rs
 
 SMACK SECURITY MODULE
 M:	Casey Schaufler <[email protected]>
Signature · 1 line
2.51.2
maintainer
#6 lore raw

Re: [PATCH v6 4/4] MAINTAINERS: add Rust SRCU files to SRCU entry

Quoted 11 lines · Boqun Feng
Quoted 4 lines · Onur Özkan

Include Rust side implementation files to the SRCU maintainer entry.

@Paul, could you help review this? Also the patch #2 since it changes the C side.

Of course, your feedbacks will be much appreciated for the whole series as well.

Done, though I am leaving srcu.rs to you guys, unless you have a specific question for me. ;-)

Thanx, Paul

Quoted 31 lines

Regards,
Boqun

Quoted 28 lines
---
 MAINTAINERS | 3 +++
 1 file changed, 3 insertions(+)
MAINTAINERS +3 -0
index e0b307b2108c..7739a435f258 100644
@@ -24624,6 +24624,7 @@ SLEEPABLE READ-COPY UPDATE (SRCU)
 M:	Lai Jiangshan <[email protected]>
 M:	"Paul E. McKenney" <[email protected]>
 M:	Josh Triplett <[email protected]>
+M:	Onur Özkan <[email protected]> (RUST)
 R:	Steven Rostedt <[email protected]>
 R:	Mathieu Desnoyers <[email protected]>
 L:	[email protected]
@@ -24632,6 +24633,8 @@ W:	http://www.rdrop.com/users/paulmck/RCU/
 T:	git git://git.kernel.org/pub/scm/linux/kernel/git/rcu/linux.git rcu/dev
 F:	include/linux/srcu*.h
 F:	kernel/rcu/srcu*.c
+F:	rust/helpers/srcu.c
+F:	rust/kernel/sync/srcu.rs
 
 SMACK SECURITY MODULE
 M:	Casey Schaufler <[email protected]>
Signature · 1 line
2.51.2
maintainer
#7 lore raw

Re: [PATCH v6 4/4] MAINTAINERS: add Rust SRCU files to SRCU entry

Quoted 7 lines · Paul E. McKenney
Quoted 6 lines · Boqun Feng
Quoted 4 lines · Onur Özkan

Include Rust side implementation files to the SRCU maintainer entry.

Quoted 9 lines
Quoted 5 lines

@Paul, could you help review this? Also the patch #2 since it changes the C side.

Of course, your feedbacks will be much appreciated for the whole series as well.

Done, though I am leaving srcu.rs to you guys, unless you have a specific question for me. ;-)

Thank you! I saw you left reviews on v5 and I just want to let you know that in v6 (the latest version), my comment on patch #2 had been resolved.

Regards,
Boqun

Quoted 33 lines

Thanx, Paul

Quoted 31 lines

Regards,
Boqun

Quoted 28 lines
---
 MAINTAINERS | 3 +++
 1 file changed, 3 insertions(+)
MAINTAINERS +3 -0
index e0b307b2108c..7739a435f258 100644
@@ -24624,6 +24624,7 @@ SLEEPABLE READ-COPY UPDATE (SRCU)
 M:	Lai Jiangshan <[email protected]>
 M:	"Paul E. McKenney" <[email protected]>
 M:	Josh Triplett <[email protected]>
+M:	Onur Özkan <[email protected]> (RUST)
 R:	Steven Rostedt <[email protected]>
 R:	Mathieu Desnoyers <[email protected]>
 L:	[email protected]
@@ -24632,6 +24633,8 @@ W:	http://www.rdrop.com/users/paulmck/RCU/
 T:	git git://git.kernel.org/pub/scm/linux/kernel/git/rcu/linux.git rcu/dev
 F:	include/linux/srcu*.h
 F:	kernel/rcu/srcu*.c
+F:	rust/helpers/srcu.c
+F:	rust/kernel/sync/srcu.rs
 
 SMACK SECURITY MODULE
 M:	Casey Schaufler <[email protected]>
Signature · 1 line
2.51.2
maintainer
#8 lore raw

Re: [PATCH v6 4/4] MAINTAINERS: add Rust SRCU files to SRCU entry

Quoted 14 lines · Boqun Feng
Quoted 13 lines · Paul E. McKenney
Quoted 11 lines · Boqun Feng
Quoted 4 lines · Onur Özkan

Include Rust side implementation files to the SRCU maintainer entry.

@Paul, could you help review this? Also the patch #2 since it changes the C side.

Of course, your feedbacks will be much appreciated for the whole series as well.

Quoted 6 lines
Quoted 2 lines

Done, though I am leaving srcu.rs to you guys, unless you have a specific question for me. ;-)

Thank you! I saw you left reviews on v5 and I just want to let you know that in v6 (the latest version), my comment on patch #2 had been resolved.

Ah, I did misfile v6, apologies!

It look like my feedback on the other patch still applies, but please let me know if I am missing something.

Thanx, Paul

Quoted 31 lines
Quoted 31 lines
Quoted 31 lines

Regards,
Boqun

Quoted 28 lines
---
 MAINTAINERS | 3 +++
 1 file changed, 3 insertions(+)
MAINTAINERS +3 -0
index e0b307b2108c..7739a435f258 100644
@@ -24624,6 +24624,7 @@ SLEEPABLE READ-COPY UPDATE (SRCU)
 M:	Lai Jiangshan <[email protected]>
 M:	"Paul E. McKenney" <[email protected]>
 M:	Josh Triplett <[email protected]>
+M:	Onur Özkan <[email protected]> (RUST)
 R:	Steven Rostedt <[email protected]>
 R:	Mathieu Desnoyers <[email protected]>
 L:	[email protected]
@@ -24632,6 +24633,8 @@ W:	http://www.rdrop.com/users/paulmck/RCU/
 T:	git git://git.kernel.org/pub/scm/linux/kernel/git/rcu/linux.git rcu/dev
 F:	include/linux/srcu*.h
 F:	kernel/rcu/srcu*.c
+F:	rust/helpers/srcu.c
+F:	rust/kernel/sync/srcu.rs
 
 SMACK SECURITY MODULE
 M:	Casey Schaufler <[email protected]>
Signature · 1 line
2.51.2