[PATCH v2 02/10] rust: rcu: Add cond_synchronize_rcu()

From: Philipp Stanner

Date: Tue Nov 24 2026 - 07:30:54 EST


cond_synchronize_rcu() can be useful for anyone who does not want to
suffer the overhead of an avoidable call to synchronize_rcu().

Add an abstraction for cond_synchronize_rcu(). Add it as an unsafe
function since passing the right grace period identifier is crucial for
safety.

Signed-off-by: Philipp Stanner <phasta@xxxxxxxxxx>
---
rust/helpers/rcu.c | 5 +++++
rust/kernel/sync/rcu.rs | 13 +++++++++++++
2 files changed, 18 insertions(+)

diff --git a/rust/helpers/rcu.c b/rust/helpers/rcu.c
index 481274c05857..570ee76b7247 100644
--- a/rust/helpers/rcu.c
+++ b/rust/helpers/rcu.c
@@ -11,3 +11,8 @@ __rust_helper void rust_helper_rcu_read_unlock(void)
{
rcu_read_unlock();
}
+
+__rust_helper void rust_helper_cond_synchronize_rcu(unsigned long cookie)
+{
+ cond_synchronize_rcu(cookie);
+}
diff --git a/rust/kernel/sync/rcu.rs b/rust/kernel/sync/rcu.rs
index 7afc78b34759..c395a493746c 100644
--- a/rust/kernel/sync/rcu.rs
+++ b/rust/kernel/sync/rcu.rs
@@ -96,3 +96,16 @@ pub fn get_state_synchronize_rcu() -> usize {
// SAFETY: `get_state_synchronize_rcu()` is always safe to be called.
unsafe { bindings::get_state_synchronize_rcu() }
}
+
+/// Do a [`synchronize_rcu()`] if necessary.
+///
+/// # Safety
+///
+/// The `cookie` you provide must be one you obtained through
+/// [`get_state_synchronize_rcu()`].
+/// It must match the specific grace period you need to wait for.
+#[inline]
+pub unsafe fn cond_synchronize_rcu(cookie: usize) {
+ // SAFETY: Safe by the function's safety requirements.
+ unsafe { bindings::cond_synchronize_rcu(cookie) };
+}
--
2.56.0