* [PATCH v2 0/2] rust: atomic: add `fetch_sub` and update docs
@ 2026-02-19 12:20 Andreas Hindborg
2026-02-19 12:20 ` [PATCH v2 1/2] rust: atomic: add fetch_sub Andreas Hindborg
2026-02-19 12:20 ` [PATCH v2 2/2] rust: atomic: update documentation for `fetch_add` Andreas Hindborg
0 siblings, 2 replies; 7+ messages in thread
From: Andreas Hindborg @ 2026-02-19 12:20 UTC (permalink / raw)
To: Will Deacon, Peter Zijlstra, Boqun Feng, Mark Rutland, Gary Guo,
Miguel Ojeda, Björn Roy Baron, Benno Lossin, Alice Ryhl,
Trevor Gross, Danilo Krummrich
Cc: linux-kernel, rust-for-linux, Andreas Hindborg
Add `fetch_sub` and improve documentation of `fetch_add`.
Signed-off-by: Andreas Hindborg <a.hindborg@kernel.org>
---
Changes in v2:
- Update example to be more clear.
- Add a patch that updates documentation for `fetch_add`.
- Link to v1: https://lore.kernel.org/r/20260128-atomic-sub-v1-1-f8c6abcbb067@kernel.org
---
Andreas Hindborg (2):
rust: atomic: add fetch_sub
rust: atomic: update documentation for `fetch_add`
rust/kernel/sync/atomic.rs | 52 +++++++++++++++++++++++++++++++++----
rust/kernel/sync/atomic/internal.rs | 5 ++++
2 files changed, 52 insertions(+), 5 deletions(-)
---
base-commit: 63804fed149a6750ffd28610c5c1c98cce6bd377
change-id: 20260128-atomic-sub-2ff15962df12
Best regards,
--
Andreas Hindborg <a.hindborg@kernel.org>
^ permalink raw reply [flat|nested] 7+ messages in thread* [PATCH v2 1/2] rust: atomic: add fetch_sub 2026-02-19 12:20 [PATCH v2 0/2] rust: atomic: add `fetch_sub` and update docs Andreas Hindborg @ 2026-02-19 12:20 ` Andreas Hindborg 2026-02-19 13:20 ` Miguel Ojeda 2026-02-19 12:20 ` [PATCH v2 2/2] rust: atomic: update documentation for `fetch_add` Andreas Hindborg 1 sibling, 1 reply; 7+ messages in thread From: Andreas Hindborg @ 2026-02-19 12:20 UTC (permalink / raw) To: Will Deacon, Peter Zijlstra, Boqun Feng, Mark Rutland, Gary Guo, Miguel Ojeda, Björn Roy Baron, Benno Lossin, Alice Ryhl, Trevor Gross, Danilo Krummrich Cc: linux-kernel, rust-for-linux, Andreas Hindborg Add `Atomic::fetch_sub` with implementation and documentation in line with existing `Atomic::fetch_add` implementation. Reviewed-by: Alice Ryhl <aliceryhl@google.com> Signed-off-by: Andreas Hindborg <a.hindborg@kernel.org> --- 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 4aebeacb961a2..83e03bb7ecd24 100644 --- a/rust/kernel/sync/atomic.rs +++ b/rust/kernel/sync/atomic.rs @@ -559,4 +559,47 @@ pub fn fetch_add<Rhs, Ordering: ordering::Ordering>(&self, v: Rhs, _: Ordering) // 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) } + } } diff --git a/rust/kernel/sync/atomic/internal.rs b/rust/kernel/sync/atomic/internal.rs index 6fdd8e59f45be..7eed8c66efff8 100644 --- a/rust/kernel/sync/atomic/internal.rs +++ b/rust/kernel/sync/atomic/internal.rs @@ -261,5 +261,10 @@ fn fetch_add[acquire, release, relaxed](a: &AtomicRepr<Self>, v: Self::Delta) -> // 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()` is valid and properly aligned. + unsafe { bindings::#call(v, a.as_ptr().cast()) } + } } ); -- 2.51.2 ^ permalink raw reply related [flat|nested] 7+ messages in thread
* Re: [PATCH v2 1/2] rust: atomic: add fetch_sub 2026-02-19 12:20 ` [PATCH v2 1/2] rust: atomic: add fetch_sub Andreas Hindborg @ 2026-02-19 13:20 ` Miguel Ojeda 2026-02-19 13:34 ` Andreas Hindborg 0 siblings, 1 reply; 7+ messages in thread From: Miguel Ojeda @ 2026-02-19 13:20 UTC (permalink / raw) To: Andreas Hindborg Cc: Will Deacon, Peter Zijlstra, Boqun Feng, Mark Rutland, Gary Guo, Miguel Ojeda, Björn Roy Baron, Benno Lossin, Alice Ryhl, Trevor Gross, Danilo Krummrich, linux-kernel, rust-for-linux On Thu, Feb 19, 2026 at 1:20 PM Andreas Hindborg <a.hindborg@kernel.org> wrote: > > + fn fetch_sub[acquire, release, relaxed](a: &AtomicRepr<Self>, v: Self::Delta) -> Self { > + // SAFETY: `a.as_ptr()` is valid and properly aligned. > + unsafe { bindings::#call(v, a.as_ptr().cast()) } > + } [ I see `fetch_add` does the same, so it is fine here in this patch for consistency, but I thought I would leave a note here anyway... ] The safety comment, as written, could be read as just saying something it is true without justifying it. Something like: `a.as_ptr()` guarantees the returned pointer is valid and properly aligned. would show better why it is such, rather than simply stating it. Cheers, Miguel ^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH v2 1/2] rust: atomic: add fetch_sub 2026-02-19 13:20 ` Miguel Ojeda @ 2026-02-19 13:34 ` Andreas Hindborg 0 siblings, 0 replies; 7+ messages in thread From: Andreas Hindborg @ 2026-02-19 13:34 UTC (permalink / raw) To: Miguel Ojeda Cc: Will Deacon, Peter Zijlstra, Boqun Feng, Mark Rutland, Gary Guo, Miguel Ojeda, Björn Roy Baron, Benno Lossin, Alice Ryhl, Trevor Gross, Danilo Krummrich, linux-kernel, rust-for-linux "Miguel Ojeda" <miguel.ojeda.sandonis@gmail.com> writes: > On Thu, Feb 19, 2026 at 1:20 PM Andreas Hindborg <a.hindborg@kernel.org> wrote: >> >> + fn fetch_sub[acquire, release, relaxed](a: &AtomicRepr<Self>, v: Self::Delta) -> Self { >> + // SAFETY: `a.as_ptr()` is valid and properly aligned. >> + unsafe { bindings::#call(v, a.as_ptr().cast()) } >> + } > > [ I see `fetch_add` does the same, so it is fine here in this patch > for consistency, but I thought I would leave a note here anyway... ] > > The safety comment, as written, could be read as just saying something > it is true without justifying it. Something like: > > `a.as_ptr()` guarantees the returned pointer is valid and properly aligned. I'll respin and fix this, for `fetch_add` as well. I can take the spurious newline you mentioned on same occasion. Best regards, Andreas Hindborg ^ permalink raw reply [flat|nested] 7+ messages in thread
* [PATCH v2 2/2] rust: atomic: update documentation for `fetch_add` 2026-02-19 12:20 [PATCH v2 0/2] rust: atomic: add `fetch_sub` and update docs Andreas Hindborg 2026-02-19 12:20 ` [PATCH v2 1/2] rust: atomic: add fetch_sub Andreas Hindborg @ 2026-02-19 12:20 ` Andreas Hindborg 2026-02-19 12:35 ` Alice Ryhl 2026-02-19 13:10 ` Miguel Ojeda 1 sibling, 2 replies; 7+ messages in thread From: Andreas Hindborg @ 2026-02-19 12:20 UTC (permalink / raw) To: Will Deacon, Peter Zijlstra, Boqun Feng, Mark Rutland, Gary Guo, Miguel Ojeda, Björn Roy Baron, Benno Lossin, Alice Ryhl, Trevor Gross, Danilo Krummrich Cc: linux-kernel, rust-for-linux, Andreas Hindborg The documentation for `fetch_add` does not indicate that the original value is returned by `fetch_add`. Update the documentation so this is clear. Signed-off-by: Andreas Hindborg <a.hindborg@kernel.org> --- rust/kernel/sync/atomic.rs | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/rust/kernel/sync/atomic.rs b/rust/kernel/sync/atomic.rs index 83e03bb7ecd24..ce0ebd725d697 100644 --- a/rust/kernel/sync/atomic.rs +++ b/rust/kernel/sync/atomic.rs @@ -527,16 +527,15 @@ pub fn add<Rhs>(&self, v: Rhs, _: ordering::Relaxed) /// use kernel::sync::atomic::{Atomic, Acquire, Full, Relaxed}; /// /// let x = Atomic::new(42); - /// /// assert_eq!(42, x.load(Relaxed)); - /// - /// assert_eq!(54, { x.fetch_add(12, Acquire); x.load(Relaxed) }); + /// assert_eq!(42, x.fetch_add(12, Acquire)); + /// assert_eq!(54, x.load(Relaxed)); /// /// let x = Atomic::new(42); - /// /// assert_eq!(42, x.load(Relaxed)); + /// assert_eq!(42, x.fetch_add(12, Full)); + /// assert_eq!(54, x.load(Relaxed)); /// - /// assert_eq!(54, { x.fetch_add(12, Full); x.load(Relaxed) } ); /// ``` #[inline(always)] pub fn fetch_add<Rhs, Ordering: ordering::Ordering>(&self, v: Rhs, _: Ordering) -> T -- 2.51.2 ^ permalink raw reply related [flat|nested] 7+ messages in thread
* Re: [PATCH v2 2/2] rust: atomic: update documentation for `fetch_add` 2026-02-19 12:20 ` [PATCH v2 2/2] rust: atomic: update documentation for `fetch_add` Andreas Hindborg @ 2026-02-19 12:35 ` Alice Ryhl 2026-02-19 13:10 ` Miguel Ojeda 1 sibling, 0 replies; 7+ messages in thread From: Alice Ryhl @ 2026-02-19 12:35 UTC (permalink / raw) To: Andreas Hindborg Cc: Will Deacon, Peter Zijlstra, Boqun Feng, Mark Rutland, Gary Guo, Miguel Ojeda, Björn Roy Baron, Benno Lossin, Trevor Gross, Danilo Krummrich, linux-kernel, rust-for-linux On Thu, Feb 19, 2026 at 01:20:10PM +0100, Andreas Hindborg wrote: > The documentation for `fetch_add` does not indicate that the original value > is returned by `fetch_add`. Update the documentation so this is clear. > > Signed-off-by: Andreas Hindborg <a.hindborg@kernel.org> Reviewed-by: Alice Ryhl <aliceryhl@google.com> ^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH v2 2/2] rust: atomic: update documentation for `fetch_add` 2026-02-19 12:20 ` [PATCH v2 2/2] rust: atomic: update documentation for `fetch_add` Andreas Hindborg 2026-02-19 12:35 ` Alice Ryhl @ 2026-02-19 13:10 ` Miguel Ojeda 1 sibling, 0 replies; 7+ messages in thread From: Miguel Ojeda @ 2026-02-19 13:10 UTC (permalink / raw) To: Andreas Hindborg Cc: Will Deacon, Peter Zijlstra, Boqun Feng, Mark Rutland, Gary Guo, Miguel Ojeda, Björn Roy Baron, Benno Lossin, Alice Ryhl, Trevor Gross, Danilo Krummrich, linux-kernel, rust-for-linux On Thu, Feb 19, 2026 at 1:20 PM Andreas Hindborg <a.hindborg@kernel.org> wrote: > > + /// assert_eq!(54, x.load(Relaxed)); > /// > - /// assert_eq!(54, { x.fetch_add(12, Full); x.load(Relaxed) } ); > /// ``` Doesn't this leave an empty line at the end of the example? Cheers, Miguel ^ permalink raw reply [flat|nested] 7+ messages in thread
end of thread, other threads:[~2026-02-19 13:34 UTC | newest] Thread overview: 7+ messages (download: mbox.gz follow: Atom feed -- links below jump to the message on this page -- 2026-02-19 12:20 [PATCH v2 0/2] rust: atomic: add `fetch_sub` and update docs Andreas Hindborg 2026-02-19 12:20 ` [PATCH v2 1/2] rust: atomic: add fetch_sub Andreas Hindborg 2026-02-19 13:20 ` Miguel Ojeda 2026-02-19 13:34 ` Andreas Hindborg 2026-02-19 12:20 ` [PATCH v2 2/2] rust: atomic: update documentation for `fetch_add` Andreas Hindborg 2026-02-19 12:35 ` Alice Ryhl 2026-02-19 13:10 ` Miguel Ojeda
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox