From: "Gary Guo" <gary@garyguo.net>
To: "Yilin Chen" <1479826151@qq.com>, <rust-for-linux@vger.kernel.org>
Cc: <linux-kernel@vger.kernel.org>, <acourbot@nvidia.com>,
<yury.norov@gmail.com>, <ojeda@kernel.org>
Subject: Re: [PATCH] rust: bitfield: require Zeroable storage for Zeroable impl
Date: Sun, 13 Sep 2026 20:37:49 +0100 [thread overview]
Message-ID: <DLEFXN8E20PU.3GJN24VECD90Z@garyguo.net> (raw)
In-Reply-To: <tencent_AA30574AC78ECFD88D7E042A49371119ED08@qq.com>
On Sun Sep 13, 2026 at 4:34 PM BST, Yilin Chen wrote:
> The bitfield! macro implements Zeroable for generated wrapper
> types. This assumes the storage type accepts an all-zero bit
> pattern. However, the macro accepts any type and does not encode
> that requirement.
How? The bitfield macro will fail for types other than primtive integers.
Best,
Gary
>
> Add a Zeroable bound to the generated implementation. This prevents
> invalid storage types from obtaining an unsound Zeroable implementation.
>
> Fixes: b7b8b4ccdad4 ("rust: extract `bitfield!` macro from `register!`")
> Assisted-by: Codex:GPT-5.6 Sol
> Signed-off-by: Yilin Chen <1479826151@qq.com>
> ---
> rust/kernel/bitfield.rs | 8 ++++++--
> 1 file changed, 6 insertions(+), 2 deletions(-)
>
> diff --git a/rust/kernel/bitfield.rs b/rust/kernel/bitfield.rs
> index a0d0894..3dd4dea 100644
> --- a/rust/kernel/bitfield.rs
> +++ b/rust/kernel/bitfield.rs
> @@ -330,8 +330,12 @@ macro_rules! bitfield {
> }
> }
>
> - // SAFETY: `$storage` is `Zeroable` and `$name` is transparent.
> - unsafe impl ::pin_init::Zeroable for $name {}
> + // SAFETY:
> + // - `$storage: Zeroable` guarantees that the all-zero bit pattern is valid.
> + // - `$name` is `repr(transparent)` over `$storage`.
> + unsafe impl ::pin_init::Zeroable for $name
> + where $storage: ::pin_init::Zeroable
> + {}
>
> impl ::core::convert::From<$name> for $storage {
> #[inline(always)]
next prev parent reply other threads:[~2026-09-13 19:37 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-09-13 15:34 Yilin Chen
2026-09-13 19:37 ` Gary Guo [this message]
2026-09-21 6:32 ` Yilin Chen
2026-09-21 7:18 ` Alexandre Courbot
2026-09-21 10:05 ` Gary Guo
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=DLEFXN8E20PU.3GJN24VECD90Z@garyguo.net \
--to=gary@garyguo.net \
--cc=1479826151@qq.com \
--cc=acourbot@nvidia.com \
--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®