public inbox for linux-arch@vger.kernel.org
 help / color / mirror / Atom feed
From: Boqun Feng <boqun.feng@gmail.com>
To: FUJITA Tomonori <fujita.tomonori@gmail.com>
Cc: ojeda@kernel.org, a.hindborg@kernel.org, aliceryhl@google.com,
	bjorn3_gh@protonmail.com, dakr@kernel.org, gary@garyguo.net,
	lossin@kernel.org, tmgross@umich.edu, acourbot@nvidia.com,
	rust-for-linux@vger.kernel.org, linux-arch@vger.kernel.org
Subject: Re: [PATCH v1 1/3] rust: sync: atomic: Prepare AtomicOps macros for i8/i16 support
Date: Mon, 29 Dec 2025 19:13:11 +0800	[thread overview]
Message-ID: <aVJiR72gcz_uonoS@tardis-2.local> (raw)
In-Reply-To: <20251228120546.1602275-2-fujita.tomonori@gmail.com>

On Sun, Dec 28, 2025 at 09:05:44PM +0900, FUJITA Tomonori wrote:
> Rework the internal AtomicOps macro plumbing to generate per-type
> implementations from a mapping list.
> 
> Capture the trait definition once and reuse it for both declaration
> and per-type impl expansion to reduce duplication and keep future
> extensions simple.
> 
> This is a preparatory refactor for enabling i8/i16 atomics cleanly.
> 
> Signed-off-by: FUJITA Tomonori <fujita.tomonori@gmail.com>

Thanks! I have an idea that uses proc-macro to generate the Atomic*Ops
impls, e.g.

    #[atomic_ops(i8, i16, i32, i64)]
    pub trait AtomicBasicOps {
        #[variant(acquire)]
        fn read(a: &AtomicRepr<Self>) -> Self {
	    unsafe { binding_call!(a.as_ptr().cast()) }
	}
    }

But I think the current solution in your patch suffices as a temporary
solution at least.

Regards,
Boqun

> ---
>  rust/kernel/sync/atomic/internal.rs | 85 ++++++++++++++++++++++-------
>  1 file changed, 66 insertions(+), 19 deletions(-)
> 
> diff --git a/rust/kernel/sync/atomic/internal.rs b/rust/kernel/sync/atomic/internal.rs
> index 51c5750d7986..0634368d10d2 100644
> --- a/rust/kernel/sync/atomic/internal.rs
> +++ b/rust/kernel/sync/atomic/internal.rs
> @@ -169,16 +169,17 @@ fn [< atomic_ $func >]($($arg: $arg_type,)*) $(-> $ret)? {
>      }
>  }
>  
> -// Delcares $ops trait with methods and implements the trait for `i32` and `i64`.
> -macro_rules! declare_and_impl_atomic_methods {
> -    ($(#[$attr:meta])* $pub:vis trait $ops:ident {
> -        $(
> -            $(#[doc=$doc:expr])*
> -            fn $func:ident [$($variant:ident),*]($($arg_sig:tt)*) $( -> $ret:ty)? {
> -                $unsafe:tt { bindings::#call($($arg:tt)*) }
> -            }
> -        )*
> -    }) => {
> +macro_rules! declare_atomic_ops_trait {
> +    (
> +        $(#[$attr:meta])* $pub:vis trait $ops:ident {
> +            $(
> +                $(#[doc=$doc:expr])*
> +                fn $func:ident [$($variant:ident),*]($($arg_sig:tt)*) $( -> $ret:ty)? {
> +                    $unsafe:tt { bindings::#call($($arg:tt)*) }
> +                }
> +            )*
> +        }
> +    ) => {
>          $(#[$attr])*
>          $pub trait $ops: AtomicImpl {
>              $(
> @@ -188,21 +189,25 @@ fn $func:ident [$($variant:ident),*]($($arg_sig:tt)*) $( -> $ret:ty)? {
>                  );
>              )*
>          }
> +    }
> +}
>  
> -        impl $ops for i32 {
> +macro_rules! impl_atomic_ops_for_one {
> +    (
> +        $ty:ty => $ctype:ident,
> +        $(#[$attr:meta])* $pub:vis trait $ops:ident {
>              $(
> -                impl_atomic_method!(
> -                    (atomic) $func[$($variant)*]($($arg_sig)*) $(-> $ret)? {
> -                        $unsafe { call($($arg)*) }
> -                    }
> -                );
> +                $(#[doc=$doc:expr])*
> +                fn $func:ident [$($variant:ident),*]($($arg_sig:tt)*) $( -> $ret:ty)? {
> +                    $unsafe:tt { bindings::#call($($arg:tt)*) }
> +                }
>              )*
>          }
> -
> -        impl $ops for i64 {
> +    ) => {
> +        impl $ops for $ty {
>              $(
>                  impl_atomic_method!(
> -                    (atomic64) $func[$($variant)*]($($arg_sig)*) $(-> $ret)? {
> +                    ($ctype) $func[$($variant)*]($($arg_sig)*) $(-> $ret)? {
>                          $unsafe { call($($arg)*) }
>                      }
>                  );
> @@ -211,7 +216,47 @@ impl $ops for i64 {
>      }
>  }
>  
> +// Declares $ops trait with methods and implements the trait.
> +macro_rules! declare_and_impl_atomic_methods {
> +    (
> +        [ $($map:tt)* ]
> +        $(#[$attr:meta])* $pub:vis trait $ops:ident { $($body:tt)* }
> +    ) => {
> +        declare_and_impl_atomic_methods!(
> +            @with_ops_def
> +            [ $($map)* ]
> +            ( $(#[$attr])* $pub trait $ops { $($body)* } )
> +        );
> +    };
> +
> +    (@with_ops_def [ $($map:tt)* ] ( $($ops_def:tt)* )) => {
> +        declare_atomic_ops_trait!( $($ops_def)* );
> +
> +        declare_and_impl_atomic_methods!(
> +            @munch
> +            [ $($map)* ]
> +            ( $($ops_def)* )
> +        );
> +    };
> +
> +    (@munch [] ( $($ops_def:tt)* )) => {};
> +
> +    (@munch [ $ty:ty => $ctype:ident $(, $($rest:tt)*)? ] ( $($ops_def:tt)* )) => {
> +        impl_atomic_ops_for_one!(
> +            $ty => $ctype,
> +            $($ops_def)*
> +        );
> +
> +        declare_and_impl_atomic_methods!(
> +            @munch
> +            [ $($($rest)*)? ]
> +            ( $($ops_def)* )
> +        );
> +    };
> +}
> +
>  declare_and_impl_atomic_methods!(
> +    [ i32 => atomic, i64 => atomic64 ]
>      /// Basic atomic operations
>      pub trait AtomicBasicOps {
>          /// Atomic read (load).
> @@ -238,6 +283,7 @@ fn set[release](a: &AtomicRepr<Self>, v: Self) {
>  // used for now, leaving the existing macros untouched until the overall
>  // design requirements are settled.
>  declare_and_impl_atomic_methods!(
> +    [ i32 => atomic, i64 => atomic64 ]
>      /// Exchange and compare-and-exchange atomic operations
>      pub trait AtomicExchangeOps {
>          /// Atomic exchange.
> @@ -265,6 +311,7 @@ fn try_cmpxchg[acquire, release, relaxed](
>  );
>  
>  declare_and_impl_atomic_methods!(
> +    [ i32 => atomic, i64 => atomic64 ]
>      /// Atomic arithmetic operations
>      pub trait AtomicArithmeticOps {
>          /// Atomic add (wrapping).
> -- 
> 2.43.0
> 

  reply	other threads:[~2025-12-29 11:13 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-12-28 12:05 [PATCH v1 0/3] rust: Add xchg and cmpxchg support on i8/i16 FUJITA Tomonori
2025-12-28 12:05 ` [PATCH v1 1/3] rust: sync: atomic: Prepare AtomicOps macros for i8/i16 support FUJITA Tomonori
2025-12-29 11:13   ` Boqun Feng [this message]
2025-12-29 11:54     ` FUJITA Tomonori
2025-12-29 16:36     ` Gary Guo
2025-12-30  0:17       ` Boqun Feng
2026-01-02 11:25         ` Gary Guo
2025-12-28 12:05 ` [PATCH v1 2/3] rust: sync: atomic: Remove workaround macro for i8/i16 BasicOps FUJITA Tomonori
2025-12-29 11:58   ` Boqun Feng
2025-12-29 12:55     ` FUJITA Tomonori
2025-12-29 13:19       ` Boqun Feng
2025-12-28 12:05 ` [PATCH v1 3/3] rust: sync: atomic: Add i8/i16 xchg and cmpxchg support FUJITA Tomonori
2025-12-29 12:27   ` Boqun Feng
2025-12-29 12:30     ` Boqun Feng
2025-12-29 13:04       ` FUJITA Tomonori
2025-12-29 13:13         ` Boqun Feng

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=aVJiR72gcz_uonoS@tardis-2.local \
    --to=boqun.feng@gmail.com \
    --cc=a.hindborg@kernel.org \
    --cc=acourbot@nvidia.com \
    --cc=aliceryhl@google.com \
    --cc=bjorn3_gh@protonmail.com \
    --cc=dakr@kernel.org \
    --cc=fujita.tomonori@gmail.com \
    --cc=gary@garyguo.net \
    --cc=linux-arch@vger.kernel.org \
    --cc=lossin@kernel.org \
    --cc=ojeda@kernel.org \
    --cc=rust-for-linux@vger.kernel.org \
    --cc=tmgross@umich.edu \
    /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