Skip to contentSkip to conversation

rust: fix outdated safety note in `Revocable::revoke_internal`

PATCH In review Onur Özkan 2 participants 3 messages Jul 3, 2025 – Jul 4, 2025 Rust

lore
Share & tools
lore

Onur Özkan

author reviewer
#0 lore raw

PATCH 1/1 rust: fix outdated safety note in `Revocable::revoke_internal`

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.

---
 rust/kernel/revocable.rs | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
rust/kernel/revocable.rs +4 -2
index 06a3cdfce344..5c0b7afa76fb 100644
@@ -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()) };
         }
 
Signature · 1 line
2.50.0
reviewer
#1 lore raw

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

Quoted 16 lines · 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.

---
 rust/kernel/revocable.rs | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
rust/kernel/revocable.rs +0 -0
index 06a3cdfce344..5c0b7afa76fb 100644

rust/kernel/revocable.rs unsafe fn revoke_internal<const SYNC: bool>(&self) -> bool { · line 169

Quoted 9 lines
rust/kernel/revocable.rs +4 -2
@@ -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/[email protected]

---
Cheers,
Benno

Quoted 3 lines
rust/kernel/revocable.rs +0 -0
             unsafe { drop_in_place(self.data.get()) };
         }
 
author reviewer
#2 lore raw
Quoted 40 lines · On Thu, 03 Jul 2025 21:55:44 +0200 "Benno Lossin" <[email protected]> wrote:

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

Quoted 25 lines · 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/[email protected]
[1]

---
 rust/kernel/revocable.rs | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
rust/kernel/revocable.rs +0 -1
index 06a3cdfce344..5c0b7afa76fb 100644
@@ -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.

Quoted 8 lines

+ // 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:

Quoted 1 line
    https://lore.kernel.org/all/[email protected]

Yeah, seems like.

Thanks,
Onur