Skip to contentSkip to conversation

rust: add SRCU abstraction

PATCH Superseded Onur Özkan 1 participant 4 messages May 22

Subsystems (4)

v3 of 10 Superseded Latest is v10 →

lore
Share & tools
lore

Onur Özkan

author maintainer
#0 lore raw

v3 cover rust: add SRCU abstraction

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.

[1]: 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]

Onur Özkan (3):
rust: helpers: add SRCU helpers
rust: sync: add SRCU abstraction
MAINTAINERS: add Rust SRCU files to SRCU entry

 MAINTAINERS              |   3 +
 rust/helpers/helpers.c   |   1 +
 rust/helpers/srcu.c      |  24 ++++++
 rust/kernel/sync.rs      |   2 +
 rust/kernel/sync/srcu.rs | 156 +++++++++++++++++++++++++++++++++++++++
 5 files changed, 186 insertions(+)
 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 v3 1/3 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    | 24 ++++++++++++++++++++++++
 2 files changed, 25 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 +24 -0
new file mode 100644
index 000000000000..e9f723d7f8c9
@@ -0,0 +1,24 @@
+// 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);
+}
Signature · 1 line
2.51.2
author maintainer
#2 lore raw

PATCH v3 2/3 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 | 156 +++++++++++++++++++++++++++++++++++++++
 2 files changed, 158 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 +156 -0

156 lines not on this page · raw message

Signature · 1 line
2.51.2
author maintainer
#3 lore raw

PATCH v3 3/3 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