linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] rust: fix outdated safety note in `Revocable::revoke_internal`
@ 2025-07-03 17:26 Onur Özkan
  2025-07-03 19:55 ` Benno Lossin
  0 siblings, 1 reply; 3+ messages in thread
From: Onur Özkan @ 2025-07-03 17:26 UTC (permalink / raw)
  To: rust-for-linux, linux-kernel
  Cc: ojeda, "alex.gaynor, gary, bjorn3_gh, lossin, a.hindborg,
	aliceryhl, tmgross, dakr, acourbot, joelagnelf, wedsonaf,
	Onur Özkan

The code used to use `compare_exchange` in the initial version
but it was changed to `swap` after a reviewer suggestion (see [1]),
and then the safety comment was not updated and became incorrect.

Link: https://lore.kernel.org/all/20241211104742.533392-1-benoit@dugarreau.fr [1]

Signed-off-by: Onur Özkan <work@onurozkan.dev>
---
 rust/kernel/revocable.rs | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)

diff --git a/rust/kernel/revocable.rs b/rust/kernel/revocable.rs
index 06a3cdfce344..5c0b7afa76fb 100644
--- a/rust/kernel/revocable.rs
+++ b/rust/kernel/revocable.rs
@@ -163,8 +163,10 @@ unsafe fn revoke_internal<const SYNC: bool>(&self) -> bool {
                 unsafe { bindings::synchronize_rcu() };
             }
 
-            // SAFETY: We know `self.data` is valid because only one CPU can succeed the
-            // `compare_exchange` above that takes `is_available` from `true` to `false`.
+            // SAFETY: We just used an atomic `swap` to check if the data was still marked
+            // as available. If it returns `true`, that means we are the first (and only)
+            // thread to see it as available and mark it as unavailable. So no other thread
+            // can access or drop the data after this. That makes it safe to drop the data here.
             unsafe { drop_in_place(self.data.get()) };
         }
 
-- 
2.50.0


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

* Re: [PATCH] rust: fix outdated safety note in `Revocable::revoke_internal`
  2025-07-03 17:26 [PATCH] rust: fix outdated safety note in `Revocable::revoke_internal` Onur Özkan
@ 2025-07-03 19:55 ` Benno Lossin
  2025-07-04  5:02   ` Onur
  0 siblings, 1 reply; 3+ messages in thread
From: Benno Lossin @ 2025-07-03 19:55 UTC (permalink / raw)
  To: Onur Özkan, rust-for-linux, linux-kernel
  Cc: ojeda, alex.gaynor, boqun.feng, gary, bjorn3_gh, a.hindborg,
	aliceryhl, tmgross, dakr, acourbot, joelagnelf, wedsonaf

Something went wrong with your TO addresses, merging Alex's and
Boqun's...

On Thu Jul 3, 2025 at 7:26 PM CEST, Onur Özkan wrote:
> The code used to use `compare_exchange` in the initial version
> but it was changed to `swap` after a reviewer suggestion (see [1]),
> and then the safety comment was not updated and became incorrect.
>
> Link: https://lore.kernel.org/all/20241211104742.533392-1-benoit@dugarreau.fr [1]
>
> Signed-off-by: Onur Özkan <work@onurozkan.dev>
> ---
>  rust/kernel/revocable.rs | 6 ++++--
>  1 file changed, 4 insertions(+), 2 deletions(-)
>
> diff --git a/rust/kernel/revocable.rs b/rust/kernel/revocable.rs
> index 06a3cdfce344..5c0b7afa76fb 100644
> --- a/rust/kernel/revocable.rs
> +++ b/rust/kernel/revocable.rs
> @@ -163,8 +163,10 @@ unsafe fn revoke_internal<const SYNC: bool>(&self) -> bool {
>                  unsafe { bindings::synchronize_rcu() };
>              }
>  
> -            // SAFETY: We know `self.data` is valid because only one CPU can succeed the
> -            // `compare_exchange` above that takes `is_available` from `true` to `false`.
> +            // SAFETY: We just used an atomic `swap` to check if the data was still marked
> +            // as available. If it returns `true`, that means we are the first (and only)
> +            // thread to see it as available and mark it as unavailable. So no other thread
> +            // can access or drop the data after this. That makes it safe to drop the data here.

I think this is already addressed by this series:

    https://lore.kernel.org/all/20250626165927.66498-1-marcelomoreira1905@gmail.com

---
Cheers,
Benno

>              unsafe { drop_in_place(self.data.get()) };
>          }
>  


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

* Re: [PATCH] rust: fix outdated safety note in `Revocable::revoke_internal`
  2025-07-03 19:55 ` Benno Lossin
@ 2025-07-04  5:02   ` Onur
  0 siblings, 0 replies; 3+ messages in thread
From: Onur @ 2025-07-04  5:02 UTC (permalink / raw)
  To: Benno Lossin
  Cc: rust-for-linux, linux-kernel, ojeda, alex.gaynor, boqun.feng,
	gary, bjorn3_gh, a.hindborg, aliceryhl, tmgross, dakr, acourbot,
	joelagnelf, wedsonaf

On Thu, 03 Jul 2025 21:55:44 +0200
"Benno Lossin" <lossin@kernel.org> wrote:

> Something went wrong with your TO addresses, merging Alex's and
> Boqun's...
> 
> On Thu Jul 3, 2025 at 7:26 PM CEST, Onur Özkan wrote:
> > The code used to use `compare_exchange` in the initial version
> > but it was changed to `swap` after a reviewer suggestion (see [1]),
> > and then the safety comment was not updated and became incorrect.
> >
> > Link:
> > https://lore.kernel.org/all/20241211104742.533392-1-benoit@dugarreau.fr
> > [1]
> >
> > Signed-off-by: Onur Özkan <work@onurozkan.dev>
> > ---
> >  rust/kernel/revocable.rs | 6 ++++--
> >  1 file changed, 4 insertions(+), 2 deletions(-)
> >
> > diff --git a/rust/kernel/revocable.rs b/rust/kernel/revocable.rs
> > index 06a3cdfce344..5c0b7afa76fb 100644
> > --- a/rust/kernel/revocable.rs
> > +++ b/rust/kernel/revocable.rs
> > @@ -163,8 +163,10 @@ unsafe fn revoke_internal<const SYNC:
> > bool>(&self) -> bool { unsafe { bindings::synchronize_rcu() };
> >              }
> >  
> > -            // SAFETY: We know `self.data` is valid because only
> > one CPU can succeed the
> > -            // `compare_exchange` above that takes `is_available`
> > from `true` to `false`.
> > +            // SAFETY: We just used an atomic `swap` to check if
> > the data was still marked
> > +            // as available. If it returns `true`, that means we
> > are the first (and only)
> > +            // thread to see it as available and mark it as
> > unavailable. So no other thread
> > +            // can access or drop the data after this. That makes
> > it safe to drop the data here.
> 
> I think this is already addressed by this series:
> 
>     https://lore.kernel.org/all/20250626165927.66498-1-marcelomoreira1905@gmail.com

Yeah, seems like.


Thanks,
Onur

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

end of thread, other threads:[~2025-07-04  5:08 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-07-03 17:26 [PATCH] rust: fix outdated safety note in `Revocable::revoke_internal` Onur Özkan
2025-07-03 19:55 ` Benno Lossin
2025-07-04  5:02   ` Onur

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).