Skip to contentSkip to conversation

Add and use abstraction for synchronize_rcu()

PATCH Applied Philipp Stanner 3 participants 8 messages Jun 24 – Aug 7

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

v3 cover Add and use abstraction for synchronize_rcu()

Changes since v2:
- Compromise docu for "RCU versus atomic context" a bit better
- Add respective R-b's

Changes since v1:
- Vertically format imports. (Onur)
- Make the new function #[inline]. (Alice)
- Add Alice's R-b.

Philipp Stanner (3):
rust: sync: Add abstraction for synchronize_rcu()
rust: revocable: Use safe synchronize_rcu() abstraction
rust: sync: Use safe synchronize_rcu() abstraction in poll

 rust/kernel/revocable.rs |  9 ++++++---
 rust/kernel/sync/poll.rs | 10 ++++++----
 rust/kernel/sync/rcu.rs  | 16 ++++++++++++++++
 3 files changed, 28 insertions(+), 7 deletions(-)

base-commit: 43a393185e33e573a374c1d4f7ddf6481484ef8d

Signature · 1 line
2.54.0
#1 lore raw

PATCH v3 1/3 rust: sync: Add abstraction for synchronize_rcu()

synchronize_rcu() is a frequently used C function which is always safe to be called.

Add a safe abstraction for synchronize_rcu().

---
 rust/kernel/sync/rcu.rs | 16 ++++++++++++++++
 1 file changed, 16 insertions(+)
rust/kernel/sync/rcu.rs +16 -0
index a32bef6e490b..2bae76d229f0 100644
@@ -50,3 +50,19 @@ fn drop(&mut self) {
 pub fn read_lock() -> Guard {
     Guard::new()
 }
+
+/// Wait for one RCU grace period.
+///
+/// Waits for all RCU read-side critical sections (such as those established by
+/// a [`rcu::Guard`]) at the moment of the function call to finish.
+///
+/// Does not prevent new read-side critical sections from starting, which may
+/// begin and run while this call is blocking.
+///
+/// Note that this is one of the RCU primitives which must not be called in
+/// atomic context.
+#[inline]
+pub fn synchronize_rcu() {
+    // SAFETY: `synchronize_rcu()` is always safe to be called from process context.
+    unsafe { bindings::synchronize_rcu() };
+}
Signature · 1 line
2.54.0
#3 lore raw

PATCH v3 2/3 rust: revocable: Use safe synchronize_rcu() abstraction

In reply to Philipp Stanner

We now have a safe wrapper for the foreign function synchronize_rcu().

Use it in revocable.rs.

---
 rust/kernel/revocable.rs | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)
rust/kernel/revocable.rs +6 -3
index 0f4ae673256d..f539603349f1 100644
@@ -7,7 +7,11 @@
 
 use pin_init::Wrapper;
 
-use crate::{bindings, prelude::*, sync::rcu, types::Opaque};
+use crate::{
+    prelude::*,
+    sync::rcu,
+    types::Opaque, //
+};
 use core::{
     marker::PhantomData,
     ops::Deref,
@@ -161,8 +165,7 @@ unsafe fn revoke_internal<const SYNC: bool>(&self) -> bool {
 
         if revoke {
             if SYNC {
-                // SAFETY: Just an FFI call, there are no further requirements.
-                unsafe { bindings::synchronize_rcu() };
+                rcu::synchronize_rcu();
             }
 
             // SAFETY: We know `self.data` is valid because only one CPU can succeed the
Signature · 1 line
2.54.0
#5 lore raw

PATCH v3 3/3 rust: sync: Use safe synchronize_rcu() abstraction in poll

In reply to Philipp Stanner

We now have a safe wrapper for the foreign function synchronize_rcu().

Use it in poll.rs.

---
 rust/kernel/sync/poll.rs | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)
rust/kernel/sync/poll.rs +6 -4
index 0ec985d560c8..30ebeea1702f 100644
@@ -8,7 +8,11 @@
     bindings,
     fs::File,
     prelude::*,
-    sync::{CondVar, LockClassKey},
+    sync::{
+        CondVar,
+        LockClassKey,
+        rcu::synchronize_rcu, //
+    }, //
 };
 use core::{marker::PhantomData, ops::Deref};
 
@@ -99,8 +103,6 @@ fn drop(self: Pin<&mut Self>) {
         unsafe { bindings::__wake_up_pollfree(self.inner.wait_queue_head.get()) };
 
         // Wait for epoll items to be properly removed.
-        //
-        // SAFETY: Just an FFI call.
-        unsafe { bindings::synchronize_rcu() };
+        synchronize_rcu();
     }
 }
Signature · 1 line
2.54.0
maintainer
#7 lore raw
In reply to Philipp Stanner
Quoted 14 lines · Philipp Stanner

Changes since v2:
- Compromise docu for "RCU versus atomic context" a bit better
- Add respective R-b's

Changes since v1:
- Vertically format imports. (Onur)
- Make the new function #[inline]. (Alice)
- Add Alice's R-b.

Philipp Stanner (3):
rust: sync: Add abstraction for synchronize_rcu()
rust: revocable: Use safe synchronize_rcu() abstraction
rust: sync: Use safe synchronize_rcu() abstraction in poll

Quoted 5 lines
 rust/kernel/revocable.rs |  9 ++++++---
 rust/kernel/sync/poll.rs | 10 ++++++----
 rust/kernel/sync/rcu.rs  | 16 ++++++++++++++++
 3 files changed, 28 insertions(+), 7 deletions(-)

Queue it in rust-sync

https://git.kernel.org/pub/scm/linux/kernel/git/boqun/linux.git/ rust-sync

Thank you all!

Regards,
Boqun

Quoted 5 lines

base-commit: 43a393185e33e573a374c1d4f7ddf6481484ef8d

Signature · 1 line
2.54.0
#6 lore raw

[tip: locking/core] rust: sync: Use safe synchronize_rcu() abstraction in poll

In reply to Philipp Stanner
The following commit has been merged into the locking/core branch of tip:

The following commit has been merged into the locking/core branch of tip:

Commit-ID:     47c3367ff096e66f614ed3cdadece26ffc04b5e8
Gitweb:        https://git.kernel.org/tip/47c3367ff096e66f614ed3cdadece26ffc04b5e8
Author:        Philipp Stanner <[email protected]>
AuthorDate:    Wed, 24 Jun 2026 17:07:04 +02:00
Committer:     Boqun Feng <[email protected]>
CommitterDate: Tue, 04 Aug 2026 05:52:54 -07:00

rust: sync: Use safe synchronize_rcu() abstraction in poll

We now have a safe wrapper for the foreign function synchronize_rcu().

Use it in poll.rs.

---
 rust/kernel/sync/poll.rs | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)
rust/kernel/sync/poll.rs +6 -4
index 0ec985d..5aa0ce9 100644
@@ -8,7 +8,11 @@ use crate::{
     bindings,
     fs::File,
     prelude::*,
-    sync::{CondVar, LockClassKey},
+    sync::{
+        rcu::synchronize_rcu,
+        CondVar,
+        LockClassKey, //
+    }, //
 };
 use core::{marker::PhantomData, ops::Deref};
 
@@ -99,8 +103,6 @@ impl PinnedDrop for PollCondVar {
         unsafe { bindings::__wake_up_pollfree(self.inner.wait_queue_head.get()) };
 
         // Wait for epoll items to be properly removed.
-        //
-        // SAFETY: Just an FFI call.
-        unsafe { bindings::synchronize_rcu() };
+        synchronize_rcu();
     }
 }
#4 lore raw

[tip: locking/core] rust: revocable: Use safe synchronize_rcu() abstraction

In reply to Philipp Stanner
The following commit has been merged into the locking/core branch of tip:

The following commit has been merged into the locking/core branch of tip:

Commit-ID:     042278f5fa9e681420ab486d6cbf464e26667461
Gitweb:        https://git.kernel.org/tip/042278f5fa9e681420ab486d6cbf464e26667461
Author:        Philipp Stanner <[email protected]>
AuthorDate:    Wed, 24 Jun 2026 17:07:03 +02:00
Committer:     Boqun Feng <[email protected]>
CommitterDate: Tue, 04 Aug 2026 05:52:53 -07:00

rust: revocable: Use safe synchronize_rcu() abstraction

We now have a safe wrapper for the foreign function synchronize_rcu().

Use it in revocable.rs.

---
 rust/kernel/revocable.rs |  9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)
rust/kernel/revocable.rs +6 -3
index 0f4ae67..f539603 100644
@@ -7,7 +7,11 @@
 
 use pin_init::Wrapper;
 
-use crate::{bindings, prelude::*, sync::rcu, types::Opaque};
+use crate::{
+    prelude::*,
+    sync::rcu,
+    types::Opaque, //
+};
 use core::{
     marker::PhantomData,
     ops::Deref,
@@ -161,8 +165,7 @@ impl<T> Revocable<T> {
 
         if revoke {
             if SYNC {
-                // SAFETY: Just an FFI call, there are no further requirements.
-                unsafe { bindings::synchronize_rcu() };
+                rcu::synchronize_rcu();
             }
 
             // SAFETY: We know `self.data` is valid because only one CPU can succeed the
#2 lore raw

[tip: locking/core] rust: sync: Add abstraction for synchronize_rcu()

In reply to Philipp Stanner
The following commit has been merged into the locking/core branch of tip:

The following commit has been merged into the locking/core branch of tip:

Commit-ID:     1fb92e0625596985b3941925afe7906fef41214b
Gitweb:        https://git.kernel.org/tip/1fb92e0625596985b3941925afe7906fef41214b
Author:        Philipp Stanner <[email protected]>
AuthorDate:    Wed, 24 Jun 2026 17:07:02 +02:00
Committer:     Boqun Feng <[email protected]>
CommitterDate: Tue, 04 Aug 2026 05:52:53 -07:00

rust: sync: Add abstraction for synchronize_rcu()

synchronize_rcu() is a frequently used C function which is always safe to be called.

Add a safe abstraction for synchronize_rcu().

---
 rust/kernel/sync/rcu.rs | 16 ++++++++++++++++
 1 file changed, 16 insertions(+)
rust/kernel/sync/rcu.rs +16 -0
index a32bef6..d867240 100644
@@ -50,3 +50,19 @@ impl Drop for Guard {
 pub fn read_lock() -> Guard {
     Guard::new()
 }
+
+/// Wait for one RCU grace period.
+///
+/// Waits for all RCU read-side critical sections (such as those established by
+/// a [`Guard`]) at the moment of the function call to finish.
+///
+/// Does not prevent new read-side critical sections from starting, which may
+/// begin and run while this call is blocking.
+///
+/// Note that this is one of the RCU primitives which must not be called in
+/// atomic context.
+#[inline]
+pub fn synchronize_rcu() {
+    // SAFETY: `synchronize_rcu()` is always safe to be called from process context.
+    unsafe { bindings::synchronize_rcu() };
+}