rust: fix outdated safety note in `Revocable::revoke_internal`
Onur Özkan
reviewerPATCH 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
@@ -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
Benno Lossin
reviewerSomething went wrong with your TO addresses, merging Alex's and Boqun's...
Quoted 16 lines · Onur Özkan
The code used to use
compare_exchangein the initial version but it was changed toswapafter a reviewer suggestion (see [1]), and then the safety comment was not updated and became incorrect.Signed-off-by Onur Özkan <[email protected]>--- rust/kernel/revocable.rs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-)rust/kernel/revocable.rs +0
-0
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
-0unsafe {drop_in_place (self.data.get ()) }; }
Onur
reviewerQuoted 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_exchangein the initial version but it was changed toswapafter 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]Signed-off-by Onur Özkan <[email protected]>--- rust/kernel/revocable.rs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-)rust/kernel/revocable.rs +0
-1@@ -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_exchangeabove that takesis_available
fromtruetofalse.Quoted 8 lines
+ // SAFETY: We just used an atomic
swapto check if
the data was still marked
+ // as available. If it returnstrue, 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