Skip to contentSkip to conversation

rust: sync: atomic: Update safety requirements for `AtomicAdd`

PATCH New Ivan Tverdokhleb 1 participant 2 messages Nov 16

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/2 rust: sync: atomic: Update safety requirements for `AtomicAdd`

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.

---
 rust/kernel/sync/atomic.rs | 9 +++++----
 1 file changed, 5 insertions(+), 4 deletions(-)
rust/kernel/sync/atomic.rs +5 -4
index 6d27898add42..5f6491fafd34 100644
@@ -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;
Signature · 1 line
2.56.1
#1 lore raw

PATCH 2/2 rust: sync: atomic: Fix typo in SAFETY comment for `isize`

The SAFETY comment for the AtomicAdd<isize> impl of isize says the result is a valid usize. Fix it to say isize.

---
 rust/kernel/sync/atomic/predefine.rs | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
rust/kernel/sync/atomic/predefine.rs +1 -1
index 3d63f40791fa..61394a32951a 100644
@@ -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
Signature · 1 line
2.56.1