mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: "Alexandre Courbot" <acourbot@nvidia.com>
To: "Yilin Chen" <1479826151@qq.com>
Cc: <gary@garyguo.net>, <rust-for-linux@vger.kernel.org>,
	<linux-kernel@vger.kernel.org>, <yury.norov@gmail.com>,
	<ojeda@kernel.org>
Subject: Re: [PATCH v2] rust: bitfield: require integer storage
Date: Tue, 22 Sep 2026 22:53:10 +0900	[thread overview]
Message-ID: <DLLW8NY8GF79.3U3VGJF2C3Z63@nvidia.com> (raw)
In-Reply-To: <tencent_6ED5B37F63268169D25F84557BD655D9B106@qq.com>

On Tue Sep 22, 2026 at 2:28 PM JST, Yilin Chen wrote:
> The bitfield! macro generates an unconditional Zeroable implementation
> for its wrapper type. An empty field list generates no Bounded usage, so
> the storage type can bypass the Integer requirement.
>
> Require the storage type to implement the sealed Integer trait for the
> generated Zeroable implementation. This ensures that bitfield storage is
> limited to primitive integer types with a valid all-zero bit pattern.
>
> Fixes: b7b8b4ccdad4 ("rust: extract `bitfield!` macro from `register!`")
> Assisted-by: GPT-5.6 Sol
> Signed-off-by: Yilin Chen <1479826151@qq.com>

Looks like my wish [1] has been instantly granted.

[1] https://lore.kernel.org/rust-for-linux/DLLVY7G20JJA.2B0KTTODUU9FG@nvidia.com/

> ---
> Changes in v2:
> - Add `where $storage: ::kernel::num::Integer` bound.
> - Update `// SAFETY` section.
> ---
>
> I track the default rust-next branch, and there is not any code about
> `AsRepr` in that branch. So in patch v1, I didn't know that case could
> not compile. Thank you for your feedback!
>
>  rust/kernel/bitfield.rs | 9 +++++++--
>  1 file changed, 7 insertions(+), 2 deletions(-)
>
> diff --git a/rust/kernel/bitfield.rs b/rust/kernel/bitfield.rs
> index a0d089423f21..b1fc98d7f8c3 100644
> --- a/rust/kernel/bitfield.rs
> +++ b/rust/kernel/bitfield.rs
> @@ -330,8 +330,13 @@ impl $name {
>              }
>          }
>  
> -        // SAFETY: `$storage` is `Zeroable` and `$name` is transparent.
> -        unsafe impl ::pin_init::Zeroable for $name {}
> +        // SAFETY:
> +        // - `$storage: Integer` is sealed to primitive integer types, for which the all-zero bit
> +        //   pattern is valid.
> +        // - `$name` is `repr(transparent)` over `$storage`.
> +        unsafe impl ::pin_init::Zeroable for $name
> +        where $storage: ::kernel::num::Integer
> +        {}

While I guess that would somehow work, I think the proper place to do
this is the struct definition, i.e:

  $vis struct $name
  where
      $storage: $crate::num::Integer,
  {
      inner: $storage,
  }

This makes the error message also more explicit about what the problem
is (a bitfield requires an `Integer`, not merely the ability to
initialize it to zero).

  reply	other threads:[~2026-09-22 13:53 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-09-13 15:34 [PATCH] rust: bitfield: require Zeroable storage for Zeroable impl Yilin Chen
2026-09-13 19:37 ` Gary Guo
2026-09-21  6:32   ` Yilin Chen
2026-09-21  7:18     ` Alexandre Courbot
2026-09-21 10:05       ` Gary Guo
2026-09-22  5:28         ` [PATCH v2] rust: bitfield: require integer storage Yilin Chen
2026-09-22 13:53           ` Alexandre Courbot [this message]
2026-09-22 14:02             ` Gary Guo
2026-09-22 13:39         ` [PATCH] rust: bitfield: require Zeroable storage for Zeroable impl Alexandre Courbot

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=DLLW8NY8GF79.3U3VGJF2C3Z63@nvidia.com \
    --to=acourbot@nvidia.com \
    --cc=1479826151@qq.com \
    --cc=gary@garyguo.net \
    --cc=linux-kernel@vger.kernel.org \
    --cc=ojeda@kernel.org \
    --cc=rust-for-linux@vger.kernel.org \
    --cc=yury.norov@gmail.com \
    /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

all inboxes | Powered by JetHome®