public inbox for rust-for-linux@vger.kernel.org
 help / color / mirror / Atom feed
From: "Gary Guo" <gary@garyguo.net>
To: "FUJITA Tomonori" <fujita.tomonori@gmail.com>, <aliceryhl@google.com>
Cc: <boqun.feng@gmail.com>, <ojeda@kernel.org>,
	<peterz@infradead.org>, <will@kernel.org>,
	<a.hindborg@kernel.org>, <bjorn3_gh@protonmail.com>,
	<dakr@kernel.org>, <gary@garyguo.net>, <lossin@kernel.org>,
	<mark.rutland@arm.com>, <tmgross@umich.edu>,
	<rust-for-linux@vger.kernel.org>
Subject: Re: [PATCH v3 1/2] rust: sync: atomic: Add i32-backed Flag for atomic booleans
Date: Tue, 13 Jan 2026 01:12:29 +0000	[thread overview]
Message-ID: <DFN28YJ5LQ3K.LEWE16Z5EXYF@garyguo.net> (raw)
In-Reply-To: <20260113.073003.1666900745205693004.fujita.tomonori@gmail.com>

On Mon Jan 12, 2026 at 10:30 PM GMT, FUJITA Tomonori wrote:
> On Mon, 12 Jan 2026 09:18:32 +0000
> Alice Ryhl <aliceryhl@google.com> wrote:
>
>> On Sun, Jan 11, 2026 at 02:05:57PM +0900, FUJITA Tomonori wrote:
>>> Add a new Flag enum (Clear/Set) and implement AtomicType for it, so
>>> users can use Atomic<Flag> for boolean flags.
>>> 
>>> The backing integer type is an implementation detail; it is currently
>>> i32 but may change in the future.
>>> 
>>> Document when Atomic<Flag> is generally preferable to Atomic<bool>: in
>>> particular, when RMW operations such as xchg()/cmpxchg() may be used
>>> and minimizing memory usage is not the top priority. On some
>>> architectures without byte-sized RMW instructions, Atomic<bool> can be
>>> slower for RMW operations.
>>> 
>>> Signed-off-by: FUJITA Tomonori <fujita.tomonori@gmail.com>
>>> ---
>>>  rust/kernel/sync/atomic.rs | 56 ++++++++++++++++++++++++++++++++++++++
>>>  1 file changed, 56 insertions(+)
>>> 
>>> diff --git a/rust/kernel/sync/atomic.rs b/rust/kernel/sync/atomic.rs
>>> index ca9cab77abf0..473d3c07e234 100644
>>> --- a/rust/kernel/sync/atomic.rs
>>> +++ b/rust/kernel/sync/atomic.rs
>>> @@ -566,3 +566,59 @@ pub fn fetch_add<Rhs, Ordering: ordering::Ordering>(&self, v: Rhs, _: Ordering)
>>>          unsafe { from_repr(ret) }
>>>      }
>>>  }
>>> +
>>> +/// An atomic flag type intended to be backed by a performance-optimal integer type.
>>> +///
>>> +/// The backing integer type is an implementation detail; it is currently [`i32`] but may change
>>> +/// in the future.
>> 
>> How is it, could we make it u8 on some architectures without penalty,
>> and have it be i32 on others? Or is i32 better always.
>
> Does the following look reasonable?
>
> #[derive(Clone, Copy, PartialEq, Eq)]
> #[cfg_attr(any(CONFIG_X86_64, CONFIG_UML, CONFIG_ARM, CONFIG_ARM64), repr(i8))]
> #[cfg_attr(
>     not(any(CONFIG_X86_64, CONFIG_UML, CONFIG_ARM, CONFIG_ARM64)),
>     repr(i32)
> )]

The arch list looks correct to me.

> pub enum Flag {
>     /// The flag is clear.
>     Clear = 0,
>     /// The flag is set.
>     Set = 1,
> }
>
> #[cfg(any(CONFIG_X86_64, CONFIG_UML, CONFIG_ARM, CONFIG_ARM64))]
> type FlagRepr = i8;
> #[cfg(not(any(CONFIG_X86_64, CONFIG_UML, CONFIG_ARM, CONFIG_ARM64)))]
> type FlagRepr = i32;

This doesn't need an extra typedef. You can just put `#[cfg]` on the associate
item below.

Best,
Gary

>
> unsafe impl AtomicType for Flag {
>     type Repr = FlagRepr;
> }


  reply	other threads:[~2026-01-13  1:12 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-01-11  5:05 [PATCH v3 0/2] rust: sync: atomic flag helpers FUJITA Tomonori
2026-01-11  5:05 ` [PATCH v3 1/2] rust: sync: atomic: Add i32-backed Flag for atomic booleans FUJITA Tomonori
2026-01-12  9:18   ` Alice Ryhl
2026-01-12 22:30     ` FUJITA Tomonori
2026-01-13  1:12       ` Gary Guo [this message]
2026-01-13  1:45         ` FUJITA Tomonori
2026-01-11  5:05 ` [PATCH v3 2/2] rust: sync: atomic: Add AtomicFlag bool wrapper for easier use FUJITA Tomonori
2026-01-12 12:51   ` Gary Guo
2026-01-12 22:40     ` 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=DFN28YJ5LQ3K.LEWE16Z5EXYF@garyguo.net \
    --to=gary@garyguo.net \
    --cc=a.hindborg@kernel.org \
    --cc=aliceryhl@google.com \
    --cc=bjorn3_gh@protonmail.com \
    --cc=boqun.feng@gmail.com \
    --cc=dakr@kernel.org \
    --cc=fujita.tomonori@gmail.com \
    --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