Skip to contentSkip to conversation

Add and use abstraction for synchronize_rcu()

PATCH Superseded Philipp Stanner 5 participants 13 messages Jun 18 – Jun 19

Subsystems (4)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

cover Add and use abstraction for synchronize_rcu()

Just compile tested this series, but I did use commit 1 in my DmaFence and it worked well.

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 | 5 ++---
 rust/kernel/sync/poll.rs | 6 ++----
 rust/kernel/sync/rcu.rs  | 8 ++++++++
 3 files changed, 12 insertions(+), 7 deletions(-)

base-commit: 43a393185e33e573a374c1d4f7ddf6481484ef8d

Signature · 1 line
2.54.0
#1 lore raw

PATCH 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 | 8 ++++++++
 1 file changed, 8 insertions(+)
rust/kernel/sync/rcu.rs +8 -0
index a32bef6e490b..9b9addf8fedf 100644
@@ -50,3 +50,11 @@ fn drop(&mut self) {
 pub fn read_lock() -> Guard {
     Guard::new()
 }
+
+/// Wait for one RCU grace period.
+///
+/// You typically do this to wait for everyone holding a [`Guard`].
+pub fn synchronize_rcu() {
+    // SAFETY: `synchronize_rcu()` is always safe to be called. It just waits for a grace period.
+    unsafe { bindings::synchronize_rcu() };
+}
Signature · 1 line
2.54.0
#2 lore raw

Re: [PATCH 1/3] rust: sync: Add abstraction for synchronize_rcu()

Quoted 15 lines · Philipp Stanner

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 | 8 ++++++++

 1 file changed, 8 insertions(+)
rust/kernel/sync/rcu.rs +0 -0
index a32bef6e490b..9b9addf8fedf 100644

rust/kernel/sync/rcu.rs fn drop(&mut self) { · line 57

Quoted 8 lines
rust/kernel/sync/rcu.rs +5 -0
@@ -50,3 +50,11 @@ fn drop(&mut self) {
  pub fn read_lock() -> Guard {
      Guard::new()
  }
+
+/// Wait for one RCU grace period.
+///
+/// You typically do this to wait for everyone holding a [`Guard`].
+pub fn synchronize_rcu() {

Hm, should it actually be #[inline]?

In C nowadays usage of inline is discouraged ("compiler knows better"). Unsure how Rust handles it; is its compiler different?

Moreover, this would be an opportunity to change the naming convention to rcu::synchronize()

But since Boqun & Alice are pushing for rcu::RcuKBox for the reason that it seems desirable to explicitly highlight that that's a special Box, I guess we should be consistent with that and also have "rcu" in the name here.

P.

Quoted 3 lines
rust/kernel/sync/rcu.rs +3 -0
+    // SAFETY: `synchronize_rcu()` is always safe to be called. It just waits for a grace period.
+    unsafe { bindings::synchronize_rcu() };
+}
maintainer
#3 lore raw

Re: [PATCH 1/3] rust: sync: Add abstraction for synchronize_rcu()

Quoted 27 lines · Philipp Stanner
Quoted 15 lines · Philipp Stanner

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 | 8 ++++++++

 1 file changed, 8 insertions(+)
rust/kernel/sync/rcu.rs +0 -0
index a32bef6e490b..9b9addf8fedf 100644

rust/kernel/sync/rcu.rs fn drop(&mut self) { · line 57

Quoted 8 lines
rust/kernel/sync/rcu.rs +5 -0
@@ -50,3 +50,11 @@ fn drop(&mut self) {
  pub fn read_lock() -> Guard {
      Guard::new()
  }
+
+/// Wait for one RCU grace period.
+///
+/// You typically do this to wait for everyone holding a [`Guard`].
+pub fn synchronize_rcu() {

Hm, should it actually be #[inline]?

Quoted 2 lines

In C nowadays usage of inline is discouraged ("compiler knows better"). Unsure how Rust handles it; is its compiler different?

If you don't mark it #[inline], then Rust is going to generate a function for the Rust synchronize_rcu() wrapper just in case a module wants to call it. The module might still decide to inline it, but because there's the possibility that a module could choose not to inline it, it will generate a wrapper in the core kernel.

For simple wrappers around C functions, we generally just want the module to invoke the C function directly, so #[inline] is good here to avoid the core kernel function.

Quoted 7 lines

Moreover, this would be an opportunity to change the naming convention to rcu::synchronize()

But since Boqun & Alice are pushing for rcu::RcuKBox for the reason that it seems desirable to explicitly highlight that that's a special Box, I guess we should be consistent with that and also have "rcu" in the name here.

I prefer the naming of synchronize_rcu() here.

Alice

#4 lore raw

Re: [PATCH 1/3] rust: sync: Add abstraction for synchronize_rcu()

Quoted 10 lines · Alice Ryhl
Quoted 7 lines · Philipp Stanner

Moreover, this would be an opportunity to change the naming convention to rcu::synchronize()

But since Boqun & Alice are pushing for rcu::RcuKBox for the reason that it seems desirable to explicitly highlight that that's a special Box, I guess we should be consistent with that and also have "rcu" in the name here.

I prefer the naming of synchronize_rcu() here.

We might be tempted to at least call it rcu_synchronize(), though. It's also RcuKBox, not KBoxRcu.

But I don't have hard emotions on that topic.

P.

Quoted 2 lines

Alice

maintainer
#5 lore raw

Re: [PATCH 1/3] rust: sync: Add abstraction for synchronize_rcu()

Quoted 11 lines · Philipp Stanner
Quoted 9 lines · Alice Ryhl
Quoted 7 lines · Philipp Stanner

Moreover, this would be an opportunity to change the naming convention to rcu::synchronize()

But since Boqun & Alice are pushing for rcu::RcuKBox for the reason that it seems desirable to explicitly highlight that that's a special Box, I guess we should be consistent with that and also have "rcu" in the name here.

Quoted 6 lines
Quoted 1 line

I prefer the naming of synchronize_rcu() here.

We might be tempted to at least call it rcu_synchronize(), though. It's also RcuKBox, not KBoxRcu.

But I don't have hard emotions on that topic.

I don't think we should change the C name without quite good reasons.

Alice

#6 lore raw

PATCH 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 | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)
rust/kernel/revocable.rs +2 -3
index 0f4ae673256d..66d4ee38754c 100644
@@ -7,7 +7,7 @@
 
 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 +161,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
#7 lore raw

PATCH 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 | 6 ++----
 1 file changed, 2 insertions(+), 4 deletions(-)
rust/kernel/sync/poll.rs +2 -4
index 0ec985d560c8..401cf4d3f35f 100644
@@ -8,7 +8,7 @@
     bindings,
     fs::File,
     prelude::*,
-    sync::{CondVar, LockClassKey},
+    sync::{rcu::synchronize_rcu, CondVar, LockClassKey},
 };
 use core::{marker::PhantomData, ops::Deref};
 
@@ -99,8 +99,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
reviewer
#9 lore raw

Re: [PATCH 3/3] rust: sync: Use safe synchronize_rcu() abstraction in poll

In reply to Philipp Stanner
Quoted 14 lines · On Thu, 18 Jun 2026 11:59:52 +0200 Philipp Stanner <[email protected]> wrote:

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

Use it in poll.rs.

---
 rust/kernel/sync/poll.rs | 6 ++----
 1 file changed, 2 insertions(+), 4 deletions(-)
rust/kernel/sync/poll.rs +0 -0
index 0ec985d560c8..401cf4d3f35f 100644

rust/kernel/sync/poll.rs line 11

Quoted 5 lines
rust/kernel/sync/poll.rs +1 -1
@@ -8,7 +8,7 @@
     bindings,
     fs::File,
     prelude::*,
-    sync::{CondVar, LockClassKey},
+    sync::{rcu::synchronize_rcu, CondVar, LockClassKey},

nit: Missing vertical import. Same in the other module as well.

Thanks,
Onur

Quoted 16 lines
rust/kernel/sync/poll.rs +1 -3
 };
 use core::{marker::PhantomData, ops::Deref};
 
@@ -99,8 +99,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
#10 lore raw

Re: [PATCH 3/3] rust: sync: Use safe synchronize_rcu() abstraction in poll

Quoted 12 lines · Onur Özkan
Quoted 9 lines · On Thu, 18 Jun 2026 11:59:52 +0200 Philipp Stanner <[email protected]> wrote:

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

Use it in poll.rs.

---
 rust/kernel/sync/poll.rs | 6 ++----

 1 file changed, 2 insertions(+), 4 deletions(-)
Quoted 12 lines

rust/kernel/sync/poll.rs line 11

Quoted 10 lines
rust/kernel/sync/poll.rs +1 -1
index 0ec985d560c8..401cf4d3f35f 100644
@@ -8,7 +8,7 @@
      bindings,
      fs::File,
      prelude::*,
-    sync::{CondVar, LockClassKey},
+    sync::{rcu::synchronize_rcu, CondVar, LockClassKey},

nit: Missing vertical import. Same in the other module as well.

Was already like that when I came here ;)

But if it helps I can format it while I'm at it..

P.

reviewer
#11 lore raw

Re: [PATCH 3/3] rust: sync: Use safe synchronize_rcu() abstraction in poll

Quoted 26 lines · On Fri, 19 Jun 2026 08:55:44 +0200 Philipp Stanner <[email protected]> wrote:
Quoted 12 lines · Onur Özkan
Quoted 9 lines · On Thu, 18 Jun 2026 11:59:52 +0200 Philipp Stanner <[email protected]> wrote:

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

Use it in poll.rs.

---
 rust/kernel/sync/poll.rs | 6 ++----

 1 file changed, 2 insertions(+), 4 deletions(-)
Quoted 12 lines

rust/kernel/sync/poll.rs line 11

Quoted 10 lines
rust/kernel/sync/poll.rs +1 -1
index 0ec985d560c8..401cf4d3f35f 100644
@@ -8,7 +8,7 @@
      bindings,
      fs::File,
      prelude::*,
-    sync::{CondVar, LockClassKey},
+    sync::{rcu::synchronize_rcu, CondVar, LockClassKey},

nit: Missing vertical import. Same in the other module as well.

Quoted 3 lines

Was already like that when I came here ;)

But if it helps I can format it while I'm at it..

Yeah there are still horizontal imports in certain modules and it's a perfect opportunity to fix them if we already making changes on them.

- Onur

Quoted 3 lines

P.

#12 lore raw

Re: [PATCH 3/3] rust: sync: Use safe synchronize_rcu() abstraction in poll

In reply to Philipp Stanner
Quoted 4 lines · Philipp Stanner

Was already like that when I came here ;)

But if it helps I can format it while I'm at it..

Yeah, we are trying to clean them as updates happen to them, i.e. to avoid adding new ones in the old style.

Cheers,
Miguel