* [PATCH 1/2] rust: sync: atomic: Update safety requirements for `AtomicAdd`
@ 2026-11-16 19:50 Ivan Tverdokhleb
2026-11-16 19:50 ` [PATCH 2/2] rust: sync: atomic: Fix typo in SAFETY comment for `isize` Ivan Tverdokhleb
0 siblings, 1 reply; 2+ messages in thread
From: Ivan Tverdokhleb @ 2026-11-16 19:50 UTC (permalink / raw)
To: rust-for-linux
Cc: Ivan Tverdokhleb, Will Deacon, Peter Zijlstra, Boqun Feng,
Mark Rutland, Gary Guo, Alice Ryhl, Lyude Paul, Daniel Almeida,
Onur Özkan, Miguel Ojeda, Björn Roy Baron, Benno Lossin,
Andreas Hindborg, Trevor Gross, Danilo Krummrich,
Tamir Duberstein, Alexandre Courbot, linux-kernel
The `# Safety` comment on `AtomicAdd` contained an unresolved TODO and a
broken sentence ("...to must yield...") without properly defining
`wrapping_add`. Remove the TODO, fix the phrasing, and define it as
wrapping two's-complement addition (`v.wrapping_add(delta)`), which
accurately reflects the behavior of the underlying integer primitives
since `AtomicImpl` itself has no `wrapping_add` method.
Signed-off-by: Ivan Tverdokhleb <mbkchelofficialdev@gmail.com>
---
rust/kernel/sync/atomic.rs | 9 +++++----
1 file changed, 5 insertions(+), 4 deletions(-)
diff --git a/rust/kernel/sync/atomic.rs b/rust/kernel/sync/atomic.rs
index 6d27898add42..5f6491fafd34 100644
--- a/rust/kernel/sync/atomic.rs
+++ b/rust/kernel/sync/atomic.rs
@@ -127,10 +127,11 @@ pub unsafe trait AtomicType: Sized + Copy {
///
/// # Safety
///
-// TODO: Properly defines `wrapping_add` in the following comment.
-/// `wrapping_add` any value of type `Self::Repr::Delta` obtained by [`Self::rhs_into_delta()`] to
-/// any value of type `Self::Repr` obtained through transmuting a value of type `Self` to must
-/// yield a value with a bit pattern also valid for `Self`.
+/// Given a value `v` of type `Self::Repr` obtained by transmuting a value of type [`Self`], and
+/// a value `delta` of type `<Self::Repr as AtomicImpl>::Delta` obtained by
+/// [`Self::rhs_into_delta()`], the wrapping two's-complement addition of `delta` to `v` (that is,
+/// `v.wrapping_add(delta)` on primitive integer types) must yield a value with a bit pattern also
+/// valid for [`Self`].
pub unsafe trait AtomicAdd<Rhs = Self>: AtomicType {
/// Converts `Rhs` into the `Delta` type of the atomic implementation.
fn rhs_into_delta(rhs: Rhs) -> <Self::Repr as AtomicImpl>::Delta;
--
2.56.1
^ permalink raw reply related [flat|nested] 2+ messages in thread* [PATCH 2/2] rust: sync: atomic: Fix typo in SAFETY comment for `isize`
2026-11-16 19:50 [PATCH 1/2] rust: sync: atomic: Update safety requirements for `AtomicAdd` Ivan Tverdokhleb
@ 2026-11-16 19:50 ` Ivan Tverdokhleb
0 siblings, 0 replies; 2+ messages in thread
From: Ivan Tverdokhleb @ 2026-11-16 19:50 UTC (permalink / raw)
To: rust-for-linux
Cc: Ivan Tverdokhleb, Will Deacon, Peter Zijlstra, Boqun Feng,
Mark Rutland, Gary Guo, Alice Ryhl, Lyude Paul, Daniel Almeida,
Onur Özkan, Miguel Ojeda, Björn Roy Baron, Benno Lossin,
Andreas Hindborg, Trevor Gross, Danilo Krummrich,
Tamir Duberstein, Alexandre Courbot, linux-kernel
The SAFETY comment for the `AtomicAdd<isize>` impl of `isize` says
the result is a valid `usize`. Fix it to say `isize`.
Signed-off-by: Ivan Tverdokhleb <mbkchelofficialdev@gmail.com>
---
rust/kernel/sync/atomic/predefine.rs | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/rust/kernel/sync/atomic/predefine.rs b/rust/kernel/sync/atomic/predefine.rs
index 3d63f40791fa..61394a32951a 100644
--- a/rust/kernel/sync/atomic/predefine.rs
+++ b/rust/kernel/sync/atomic/predefine.rs
@@ -106,7 +106,7 @@ unsafe impl super::AtomicType for isize {
type Repr = isize_atomic_repr;
}
-// SAFETY: The wrapping add result of two `isize_atomic_repr`s is a valid `usize`.
+// SAFETY: The wrapping add result of two `isize_atomic_repr`s is a valid `isize`.
unsafe impl super::AtomicAdd<isize> for isize {
fn rhs_into_delta(rhs: isize) -> isize_atomic_repr {
rhs as isize_atomic_repr
--
2.56.1
^ permalink raw reply related [flat|nested] 2+ messages in thread
end of thread, other threads:[~2026-11-16 19:50 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-16 19:50 [PATCH 1/2] rust: sync: atomic: Update safety requirements for `AtomicAdd` Ivan Tverdokhleb
2026-11-16 19:50 ` [PATCH 2/2] rust: sync: atomic: Fix typo in SAFETY comment for `isize` Ivan Tverdokhleb
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox