mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH] rust: revocable: use LKMM atomics instead of Rust atomics
@ 2026-07-16 14:55 Gary Guo
  2026-07-17  5:26 ` FUJITA Tomonori
                   ` (3 more replies)
  0 siblings, 4 replies; 5+ messages in thread
From: Gary Guo @ 2026-07-16 14:55 UTC (permalink / raw)
  To: Miguel Ojeda, Boqun Feng, Gary Guo, Björn Roy Baron,
	Benno Lossin, Andreas Hindborg, Alice Ryhl, Trevor Gross,
	Danilo Krummrich, Daniel Almeida, Tamir Duberstein,
	Alexandre Courbot, Onur Özkan
  Cc: rust-for-linux, linux-kernel

From: Gary Guo <gary@garyguo.net>

Kernel code should use LKMM atomics. The existing code is `AtomicBool` with
the need to use `xchg`, so convert it to `AtomicFlag`.

Signed-off-by: Gary Guo <gary@garyguo.net>
---
 rust/kernel/revocable.rs | 26 ++++++++++++++++++--------
 1 file changed, 18 insertions(+), 8 deletions(-)

diff --git a/rust/kernel/revocable.rs b/rust/kernel/revocable.rs
index 2af212ea3b6d..4d177744520a 100644
--- a/rust/kernel/revocable.rs
+++ b/rust/kernel/revocable.rs
@@ -7,12 +7,22 @@
 
 use pin_init::Wrapper;
 
-use crate::{bindings, prelude::*, sync::rcu, types::Opaque};
+use crate::{
+    bindings,
+    prelude::*,
+    sync::{
+        atomic::{
+            AtomicFlag,
+            Relaxed, //
+        },
+        rcu, //
+    },
+    types::Opaque, //
+};
 use core::{
     marker::PhantomData,
     ops::Deref,
-    ptr::drop_in_place,
-    sync::atomic::{AtomicBool, Ordering},
+    ptr::drop_in_place, //
 };
 
 /// An object that can become inaccessible at runtime.
@@ -66,7 +76,7 @@
 /// ```
 #[pin_data(PinnedDrop)]
 pub struct Revocable<T> {
-    is_available: AtomicBool,
+    is_available: AtomicFlag,
     #[pin]
     data: Opaque<T>,
 }
@@ -85,7 +95,7 @@ impl<T> Revocable<T> {
     /// Creates a new revocable instance of the given data.
     pub fn new<E>(data: impl PinInit<T, E>) -> impl PinInit<Self, E> {
         try_pin_init!(Self {
-            is_available: AtomicBool::new(true),
+            is_available: AtomicFlag::new(true),
             data <- Opaque::pin_init(data),
         }? E)
     }
@@ -99,7 +109,7 @@ pub fn new<E>(data: impl PinInit<T, E>) -> impl PinInit<Self, E> {
     /// because another CPU may be waiting to complete the revocation of this object.
     pub fn try_access(&self) -> Option<RevocableGuard<'_, T>> {
         let guard = rcu::read_lock();
-        if self.is_available.load(Ordering::Relaxed) {
+        if self.is_available.load(Relaxed) {
             // Since `self.is_available` is true, data is initialised and has to remain valid
             // because the RCU read side lock prevents it from being dropped.
             Some(RevocableGuard::new(self.data.get(), guard))
@@ -117,7 +127,7 @@ pub fn try_access(&self) -> Option<RevocableGuard<'_, T>> {
     /// allowed to sleep because another CPU may be waiting to complete the revocation of this
     /// object.
     pub fn try_access_with_guard<'a>(&'a self, _guard: &'a rcu::Guard) -> Option<&'a T> {
-        if self.is_available.load(Ordering::Relaxed) {
+        if self.is_available.load(Relaxed) {
             // SAFETY: Since `self.is_available` is true, data is initialised and has to remain
             // valid because the RCU read side lock prevents it from being dropped.
             Some(unsafe { &*self.data.get() })
@@ -158,7 +168,7 @@ pub unsafe fn access(&self) -> &T {
     ///
     /// Callers must ensure that there are no more concurrent users of the revocable object.
     unsafe fn revoke_internal<const SYNC: bool>(&self) -> bool {
-        let revoke = self.is_available.swap(false, Ordering::Relaxed);
+        let revoke = self.is_available.xchg(false, Relaxed);
 
         if revoke {
             if SYNC {

base-commit: b8809969e1d7a591e0f49dd464a5d04b3cf02ab1
-- 
2.54.0


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] rust: revocable: use LKMM atomics instead of Rust atomics
  2026-07-16 14:55 [PATCH] rust: revocable: use LKMM atomics instead of Rust atomics Gary Guo
@ 2026-07-17  5:26 ` FUJITA Tomonori
  2026-07-22 18:14 ` Alice Ryhl
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 5+ messages in thread
From: FUJITA Tomonori @ 2026-07-17  5:26 UTC (permalink / raw)
  To: gary, gary
  Cc: ojeda, boqun, bjorn3_gh, lossin, a.hindborg, aliceryhl, tmgross,
	dakr, daniel.almeida, tamird, acourbot, work, rust-for-linux,
	linux-kernel

On Thu, 16 Jul 2026 15:55:35 +0100
Gary Guo <gary@kernel.org> wrote:

> From: Gary Guo <gary@garyguo.net>
> 
> Kernel code should use LKMM atomics. The existing code is `AtomicBool` with
> the need to use `xchg`, so convert it to `AtomicFlag`.
> 
> Signed-off-by: Gary Guo <gary@garyguo.net>
> ---
>  rust/kernel/revocable.rs | 26 ++++++++++++++++++--------
>  1 file changed, 18 insertions(+), 8 deletions(-)

Reviewed-by: FUJITA Tomonori <fujita.tomonori@gmail.com>

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] rust: revocable: use LKMM atomics instead of Rust atomics
  2026-07-16 14:55 [PATCH] rust: revocable: use LKMM atomics instead of Rust atomics Gary Guo
  2026-07-17  5:26 ` FUJITA Tomonori
@ 2026-07-22 18:14 ` Alice Ryhl
  2026-07-25 14:09 ` Boqun Feng
  2026-08-07 16:05 ` [tip: locking/core] rust: revocable: Use " tip-bot2 for Gary Guo
  3 siblings, 0 replies; 5+ messages in thread
From: Alice Ryhl @ 2026-07-22 18:14 UTC (permalink / raw)
  To: Gary Guo
  Cc: Miguel Ojeda, Boqun Feng, Björn Roy Baron, Benno Lossin,
	Andreas Hindborg, Trevor Gross, Danilo Krummrich, Daniel Almeida,
	Tamir Duberstein, Alexandre Courbot, Onur Özkan,
	rust-for-linux, linux-kernel

On Thu, Jul 16, 2026 at 4:56 PM Gary Guo <gary@kernel.org> wrote:
>
> From: Gary Guo <gary@garyguo.net>
>
> Kernel code should use LKMM atomics. The existing code is `AtomicBool` with
> the need to use `xchg`, so convert it to `AtomicFlag`.
>
> Signed-off-by: Gary Guo <gary@garyguo.net>

Reviewed-by: Alice Ryhl <aliceryhl@google.com>

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] rust: revocable: use LKMM atomics instead of Rust atomics
  2026-07-16 14:55 [PATCH] rust: revocable: use LKMM atomics instead of Rust atomics Gary Guo
  2026-07-17  5:26 ` FUJITA Tomonori
  2026-07-22 18:14 ` Alice Ryhl
@ 2026-07-25 14:09 ` Boqun Feng
  2026-08-07 16:05 ` [tip: locking/core] rust: revocable: Use " tip-bot2 for Gary Guo
  3 siblings, 0 replies; 5+ messages in thread
From: Boqun Feng @ 2026-07-25 14:09 UTC (permalink / raw)
  To: Gary Guo
  Cc: Miguel Ojeda, Björn Roy Baron, Benno Lossin,
	Andreas Hindborg, Alice Ryhl, Trevor Gross, Danilo Krummrich,
	Daniel Almeida, Tamir Duberstein, Alexandre Courbot,
	Onur Özkan, rust-for-linux, linux-kernel

On Thu, Jul 16, 2026 at 03:55:35PM +0100, Gary Guo wrote:
> From: Gary Guo <gary@garyguo.net>
> 
> Kernel code should use LKMM atomics. The existing code is `AtomicBool` with
> the need to use `xchg`, so convert it to `AtomicFlag`.
> 
> Signed-off-by: Gary Guo <gary@garyguo.net>

Queued it in rust-sync, thank you all!

Regards,
Boqun

> ---
>  rust/kernel/revocable.rs | 26 ++++++++++++++++++--------
>  1 file changed, 18 insertions(+), 8 deletions(-)
> 
> diff --git a/rust/kernel/revocable.rs b/rust/kernel/revocable.rs
> index 2af212ea3b6d..4d177744520a 100644
> --- a/rust/kernel/revocable.rs
> +++ b/rust/kernel/revocable.rs
> @@ -7,12 +7,22 @@
>  
>  use pin_init::Wrapper;
>  
> -use crate::{bindings, prelude::*, sync::rcu, types::Opaque};
> +use crate::{
> +    bindings,
> +    prelude::*,
> +    sync::{
> +        atomic::{
> +            AtomicFlag,
> +            Relaxed, //
> +        },
> +        rcu, //
> +    },
> +    types::Opaque, //
> +};
>  use core::{
>      marker::PhantomData,
>      ops::Deref,
> -    ptr::drop_in_place,
> -    sync::atomic::{AtomicBool, Ordering},
> +    ptr::drop_in_place, //
>  };
>  
>  /// An object that can become inaccessible at runtime.
> @@ -66,7 +76,7 @@
>  /// ```
>  #[pin_data(PinnedDrop)]
>  pub struct Revocable<T> {
> -    is_available: AtomicBool,
> +    is_available: AtomicFlag,
>      #[pin]
>      data: Opaque<T>,
>  }
> @@ -85,7 +95,7 @@ impl<T> Revocable<T> {
>      /// Creates a new revocable instance of the given data.
>      pub fn new<E>(data: impl PinInit<T, E>) -> impl PinInit<Self, E> {
>          try_pin_init!(Self {
> -            is_available: AtomicBool::new(true),
> +            is_available: AtomicFlag::new(true),
>              data <- Opaque::pin_init(data),
>          }? E)
>      }
> @@ -99,7 +109,7 @@ pub fn new<E>(data: impl PinInit<T, E>) -> impl PinInit<Self, E> {
>      /// because another CPU may be waiting to complete the revocation of this object.
>      pub fn try_access(&self) -> Option<RevocableGuard<'_, T>> {
>          let guard = rcu::read_lock();
> -        if self.is_available.load(Ordering::Relaxed) {
> +        if self.is_available.load(Relaxed) {
>              // Since `self.is_available` is true, data is initialised and has to remain valid
>              // because the RCU read side lock prevents it from being dropped.
>              Some(RevocableGuard::new(self.data.get(), guard))
> @@ -117,7 +127,7 @@ pub fn try_access(&self) -> Option<RevocableGuard<'_, T>> {
>      /// allowed to sleep because another CPU may be waiting to complete the revocation of this
>      /// object.
>      pub fn try_access_with_guard<'a>(&'a self, _guard: &'a rcu::Guard) -> Option<&'a T> {
> -        if self.is_available.load(Ordering::Relaxed) {
> +        if self.is_available.load(Relaxed) {
>              // SAFETY: Since `self.is_available` is true, data is initialised and has to remain
>              // valid because the RCU read side lock prevents it from being dropped.
>              Some(unsafe { &*self.data.get() })
> @@ -158,7 +168,7 @@ pub unsafe fn access(&self) -> &T {
>      ///
>      /// Callers must ensure that there are no more concurrent users of the revocable object.
>      unsafe fn revoke_internal<const SYNC: bool>(&self) -> bool {
> -        let revoke = self.is_available.swap(false, Ordering::Relaxed);
> +        let revoke = self.is_available.xchg(false, Relaxed);
>  
>          if revoke {
>              if SYNC {
> 
> base-commit: b8809969e1d7a591e0f49dd464a5d04b3cf02ab1
> -- 
> 2.54.0
> 

^ permalink raw reply	[flat|nested] 5+ messages in thread

* [tip: locking/core] rust: revocable: Use LKMM atomics instead of Rust atomics
  2026-07-16 14:55 [PATCH] rust: revocable: use LKMM atomics instead of Rust atomics Gary Guo
                   ` (2 preceding siblings ...)
  2026-07-25 14:09 ` Boqun Feng
@ 2026-08-07 16:05 ` tip-bot2 for Gary Guo
  3 siblings, 0 replies; 5+ messages in thread
From: tip-bot2 for Gary Guo @ 2026-08-07 16:05 UTC (permalink / raw)
  To: linux-tip-commits
  Cc: Gary Guo, Alice Ryhl, FUJITA Tomonori, Boqun Feng, x86, linux-kernel

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

Commit-ID:     3f90c16d413b2f68407f1c5bb112a1b16ea35dc4
Gitweb:        https://git.kernel.org/tip/3f90c16d413b2f68407f1c5bb112a1b16ea35dc4
Author:        Gary Guo <gary@garyguo.net>
AuthorDate:    Thu, 16 Jul 2026 15:55:35 +01:00
Committer:     Boqun Feng <boqun@kernel.org>
CommitterDate: Tue, 04 Aug 2026 05:52:54 -07:00

rust: revocable: Use LKMM atomics instead of Rust atomics

Kernel code should use LKMM atomics. The existing code is `AtomicBool`
with the need to use `xchg`, so convert it to `AtomicFlag`.

Signed-off-by: Gary Guo <gary@garyguo.net>
Reviewed-by: Alice Ryhl <aliceryhl@google.com>
Reviewed-by: FUJITA Tomonori <fujita.tomonori@gmail.com>
Signed-off-by: Boqun Feng <boqun@kernel.org>
Link: https://patch.msgid.link/20260716145536.3681630-1-gary@kernel.org
---
 rust/kernel/revocable.rs | 21 +++++++++++++--------
 1 file changed, 13 insertions(+), 8 deletions(-)

diff --git a/rust/kernel/revocable.rs b/rust/kernel/revocable.rs
index f539603..0e55e2a 100644
--- a/rust/kernel/revocable.rs
+++ b/rust/kernel/revocable.rs
@@ -9,14 +9,19 @@ use pin_init::Wrapper;
 
 use crate::{
     prelude::*,
-    sync::rcu,
+    sync::{
+        atomic::{
+            AtomicFlag,
+            Relaxed, //
+        },
+        rcu, //
+    },
     types::Opaque, //
 };
 use core::{
     marker::PhantomData,
     ops::Deref,
-    ptr::drop_in_place,
-    sync::atomic::{AtomicBool, Ordering},
+    ptr::drop_in_place, //
 };
 
 /// An object that can become inaccessible at runtime.
@@ -69,7 +74,7 @@ use core::{
 /// ```
 #[pin_data(PinnedDrop)]
 pub struct Revocable<T> {
-    is_available: AtomicBool,
+    is_available: AtomicFlag,
     #[pin]
     data: Opaque<T>,
 }
@@ -88,7 +93,7 @@ impl<T> Revocable<T> {
     /// Creates a new revocable instance of the given data.
     pub fn new<E>(data: impl PinInit<T, E>) -> impl PinInit<Self, E> {
         try_pin_init!(Self {
-            is_available: AtomicBool::new(true),
+            is_available: AtomicFlag::new(true),
             data <- Opaque::pin_init(data),
         }? E)
     }
@@ -102,7 +107,7 @@ impl<T> Revocable<T> {
     /// because another CPU may be waiting to complete the revocation of this object.
     pub fn try_access(&self) -> Option<RevocableGuard<'_, T>> {
         let guard = rcu::read_lock();
-        if self.is_available.load(Ordering::Relaxed) {
+        if self.is_available.load(Relaxed) {
             // Since `self.is_available` is true, data is initialised and has to remain valid
             // because the RCU read side lock prevents it from being dropped.
             Some(RevocableGuard::new(self.data.get(), guard))
@@ -120,7 +125,7 @@ impl<T> Revocable<T> {
     /// allowed to sleep because another CPU may be waiting to complete the revocation of this
     /// object.
     pub fn try_access_with_guard<'a>(&'a self, _guard: &'a rcu::Guard) -> Option<&'a T> {
-        if self.is_available.load(Ordering::Relaxed) {
+        if self.is_available.load(Relaxed) {
             // SAFETY: Since `self.is_available` is true, data is initialised and has to remain
             // valid because the RCU read side lock prevents it from being dropped.
             Some(unsafe { &*self.data.get() })
@@ -161,7 +166,7 @@ impl<T> Revocable<T> {
     ///
     /// Callers must ensure that there are no more concurrent users of the revocable object.
     unsafe fn revoke_internal<const SYNC: bool>(&self) -> bool {
-        let revoke = self.is_available.swap(false, Ordering::Relaxed);
+        let revoke = self.is_available.xchg(false, Relaxed);
 
         if revoke {
             if SYNC {

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2026-08-07 16:05 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-07-16 14:55 [PATCH] rust: revocable: use LKMM atomics instead of Rust atomics Gary Guo
2026-07-17  5:26 ` FUJITA Tomonori
2026-07-22 18:14 ` Alice Ryhl
2026-07-25 14:09 ` Boqun Feng
2026-08-07 16:05 ` [tip: locking/core] rust: revocable: Use " tip-bot2 for Gary Guo

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®