[tip: locking/core] rust: sync: atomic: Add fetch_sub()
From: tip-bot2 for Andreas Hindborg
Date: Mon Mar 09 2026 - 15:49:32 EST
The following commit has been merged into the locking/core branch of tip:
Commit-ID: c49cf341090b53d2afa4dc7c8007ddeefbb3b37f
Gitweb: https://git.kernel.org/tip/c49cf341090b53d2afa4dc7c8007ddeefbb3b37f
Author: Andreas Hindborg <a.hindborg@xxxxxxxxxx>
AuthorDate: Tue, 03 Mar 2026 12:16:59 -08:00
Committer: Peter Zijlstra <peterz@xxxxxxxxxxxxx>
CommitterDate: Sun, 08 Mar 2026 11:06:50 +01:00
rust: sync: atomic: Add fetch_sub()
Add `Atomic::fetch_sub()` with implementation and documentation in line
with existing `Atomic::fetch_add()` implementation.
Signed-off-by: Andreas Hindborg <a.hindborg@xxxxxxxxxx>
Signed-off-by: Boqun Feng <boqun@xxxxxxxxxx>
Signed-off-by: Peter Zijlstra (Intel) <peterz@xxxxxxxxxxxxx>
Reviewed-by: Alice Ryhl <aliceryhl@xxxxxxxxxx>
Link: https://patch.msgid.link/20260220-atomic-sub-v3-1-e63cbed1d2aa@xxxxxxxxxx
Link: https://patch.msgid.link/20260303201701.12204-12-boqun@xxxxxxxxxx
---
rust/kernel/sync/atomic.rs | 43 ++++++++++++++++++++++++++++-
rust/kernel/sync/atomic/internal.rs | 5 +++-
2 files changed, 48 insertions(+)
diff --git a/rust/kernel/sync/atomic.rs b/rust/kernel/sync/atomic.rs
index 1bb1fc2..545a8d3 100644
--- a/rust/kernel/sync/atomic.rs
+++ b/rust/kernel/sync/atomic.rs
@@ -577,6 +577,49 @@ where
// SAFETY: `ret` comes from reading `self.0`, which is a valid `T` per type invariants.
unsafe { from_repr(ret) }
}
+
+ /// Atomic fetch and subtract.
+ ///
+ /// Atomically updates `*self` to `(*self).wrapping_sub(v)`, and returns the value of `*self`
+ /// before the update.
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// use kernel::sync::atomic::{Atomic, Acquire, Full, Relaxed};
+ ///
+ /// let x = Atomic::new(42);
+ /// assert_eq!(42, x.load(Relaxed));
+ /// assert_eq!(42, x.fetch_sub(12, Acquire));
+ /// assert_eq!(30, x.load(Relaxed));
+ ///
+ /// let x = Atomic::new(42);
+ /// assert_eq!(42, x.load(Relaxed));
+ /// assert_eq!(42, x.fetch_sub(12, Full));
+ /// assert_eq!(30, x.load(Relaxed));
+ /// ```
+ #[inline(always)]
+ pub fn fetch_sub<Rhs, Ordering: ordering::Ordering>(&self, v: Rhs, _: Ordering) -> T
+ where
+ // Types that support addition also support subtraction.
+ T: AtomicAdd<Rhs>,
+ {
+ let v = T::rhs_into_delta(v);
+
+ // INVARIANT: `self.0` is a valid `T` after `atomic_fetch_sub*()` due to safety requirement
+ // of `AtomicAdd`.
+ let ret = {
+ match Ordering::TYPE {
+ OrderingType::Full => T::Repr::atomic_fetch_sub(&self.0, v),
+ OrderingType::Acquire => T::Repr::atomic_fetch_sub_acquire(&self.0, v),
+ OrderingType::Release => T::Repr::atomic_fetch_sub_release(&self.0, v),
+ OrderingType::Relaxed => T::Repr::atomic_fetch_sub_relaxed(&self.0, v),
+ }
+ };
+
+ // SAFETY: `ret` comes from reading `self.0`, which is a valid `T` per type invariants.
+ unsafe { from_repr(ret) }
+ }
}
#[cfg(any(CONFIG_X86_64, CONFIG_UML, CONFIG_ARM, CONFIG_ARM64))]
diff --git a/rust/kernel/sync/atomic/internal.rs b/rust/kernel/sync/atomic/internal.rs
index e301db4..b762dbd 100644
--- a/rust/kernel/sync/atomic/internal.rs
+++ b/rust/kernel/sync/atomic/internal.rs
@@ -340,5 +340,10 @@ declare_and_impl_atomic_methods!(
// SAFETY: `a.as_ptr()` is valid and properly aligned.
unsafe { bindings::#call(v, a.as_ptr().cast()) }
}
+
+ fn fetch_sub[acquire, release, relaxed](a: &AtomicRepr<Self>, v: Self::Delta) -> Self {
+ // SAFETY: `a.as_ptr()` guarantees the returned pointer is valid and properly aligned.
+ unsafe { bindings::#call(v, a.as_ptr().cast()) }
+ }
}
);