From: Boqun Feng <boqun.feng@gmail.com>
To: FUJITA Tomonori <fujita.tomonori@gmail.com>
Cc: acourbot@nvidia.com, alex.gaynor@gmail.com, ojeda@kernel.org,
peterz@infradead.org, will@kernel.org, a.hindborg@kernel.org,
aliceryhl@google.com, bjorn3_gh@protonmail.com, dakr@kernel.org,
gary@garyguo.net, lossin@kernel.org, mark.rutland@arm.com,
rust-for-linux@vger.kernel.org, tmgross@umich.edu
Subject: Re: [PATCH v2 3/4] rust: sync: atomic: Add i8/i16 load and store support
Date: Thu, 11 Dec 2025 08:16:40 +0900 [thread overview]
Message-ID: <aTn_WK-ldlzOxBHq@tardis-2.local> (raw)
In-Reply-To: <20251210.083147.1370409203593259538.fujita.tomonori@gmail.com>
On Wed, Dec 10, 2025 at 08:31:47AM +0900, FUJITA Tomonori wrote:
> On Tue, 09 Dec 2025 09:27:25 +0900
> "Alexandre Courbot" <acourbot@nvidia.com> wrote:
>
> >>> Can you document this macro a bit, in particular the motivations for not
> >>> leveraging the existing ones (I guess this has to be with the new
> >>> helpers, but following the code through is a bit difficult without
> >>> comments).
> >>
> >> // Since i8 and i16 are not expected to support the full feature set
> >> // of i32 and i64, using the current declare_and_impl_atomic_methods!
> >> // would require refactoring it to handle specific types or splitting
> >> // the definitions. Furthermore, supporting Atomic<bool> will require
> >> // even more significant structural changes.
> >> //
> >> // To avoid premature refactoring, a separate macro for i8 and i16 is
> >> // used for now, leaving the existing macros untouched until the overall
> >> // design requirements are settled.
> >>
> >> makes sense?
> >
> > Absolutely, thanks! Maybe also give a short explanation for *why* i8 and
> > i16 won't support the same feature set as i32 and i64.
>
> I could add, though Boqun might have a different perspective.
>
Yeah, I think it's more of an "undecided" stage. The reasons that why we
want to avoid the same feature as i32 and i64 for i8/i16 are:
1. For some architectures, 1-byte or 2-byte atomic RmW
(read-modify-write) operations are not supported, they would generate
worse binary than designing the algorithm with Atomic<i32/i64>.
2. For some architectures, atomics RmW are implemented by lock-based
operations on C side, in that case READ_ONCE()/WRITE_ONCE() based
load/store implementations are incorrect, because it would race with
lock-based RmW for example.
atomic_add_unless(v, 1, 0);
lock();
ret = READ_ONCE(v->counter); // == 1
atomic_set(v, 0);
if (ret != u) WRITE_ONCE(v->counter, 0);
WRITE_ONCE(v->counter, ret + 1);
unlock();
Hence, we need to make a design decision here, either 1) we make
Atomic<i8> support RmW and Atomic::<i8>::load() may contain locking
on certain architecture (that also means you cannot use it as
READ_ONCE()), or 2) we disallow RmW for Atomic<i8> if they use
lock-based atomic.
I feel we are not having the enough data to make the call here,
therefore I would avoid premature conclusion in the documentation.
Thanks!
Regards,
Boqun
> In my opinion, the reason is that the C side doesn't fully support
> atomic operations for i8 and i16. While the C side doesn't support
> load and store for i8/i16, we can easily support them with simple
> wrappers (as this patchset does). I believe that xchg and cmpxchg
> could also be supported easily.
>
> However, supporting AtomicArithmeticOps (like fetch_add) for i8/i16
> would likely require more than wrappers; architecture-dependent code
> (likely with assembly). I don't think that it's a good idea to
> implement such.
>
>
> My plan is to add supoprt for limited atomic operations for i8/i16,
> and then use the i8 support to implement Atomic<bool>.
>
> Once that is in place, I intend to replace the Atomic<i32> usage in
> SetOnce and OnceLite [1]. I believe the AtomicBool usage in the
> binder driver could be replaced too.
>
> [1] https://lore.kernel.org/rust-for-linux/20251117002452.4068692-1-fujita.tomonori@gmail.com/
next prev parent reply other threads:[~2025-12-10 23:16 UTC|newest]
Thread overview: 18+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-11-17 0:10 [PATCH v2 0/4] rust: Add i8 and i16 atomic support FUJITA Tomonori
2025-11-17 0:10 ` [PATCH v2 1/4] rust: sync: Add i8/i16 atomic_load_acquire/atomic_store_release helpers FUJITA Tomonori
2025-12-08 3:01 ` Alexandre Courbot
2025-12-08 21:56 ` FUJITA Tomonori
2025-11-17 0:10 ` [PATCH v2 2/4] rust: helpers: Add i8/i16 relaxed atomic helpers FUJITA Tomonori
2025-11-17 0:10 ` [PATCH v2 3/4] rust: sync: atomic: Add i8/i16 load and store support FUJITA Tomonori
2025-12-08 3:08 ` Alexandre Courbot
2025-12-08 23:14 ` FUJITA Tomonori
2025-12-09 0:27 ` Alexandre Courbot
2025-12-09 23:31 ` FUJITA Tomonori
2025-12-10 23:16 ` Boqun Feng [this message]
2025-12-11 5:17 ` FUJITA Tomonori
2025-12-10 23:02 ` Boqun Feng
2025-12-11 4:46 ` FUJITA Tomonori
2025-11-17 0:10 ` [PATCH v2 4/4] rust: sync: atomic: Add store_release/load_acquire tests FUJITA Tomonori
2025-12-03 3:41 ` [PATCH v2 0/4] rust: Add i8 and i16 atomic support FUJITA Tomonori
2025-12-04 3:40 ` Boqun Feng
2025-12-04 13:12 ` FUJITA Tomonori
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=aTn_WK-ldlzOxBHq@tardis-2.local \
--to=boqun.feng@gmail.com \
--cc=a.hindborg@kernel.org \
--cc=acourbot@nvidia.com \
--cc=alex.gaynor@gmail.com \
--cc=aliceryhl@google.com \
--cc=bjorn3_gh@protonmail.com \
--cc=dakr@kernel.org \
--cc=fujita.tomonori@gmail.com \
--cc=gary@garyguo.net \
--cc=lossin@kernel.org \
--cc=mark.rutland@arm.com \
--cc=ojeda@kernel.org \
--cc=peterz@infradead.org \
--cc=rust-for-linux@vger.kernel.org \
--cc=tmgross@umich.edu \
--cc=will@kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox