[PATCH v2] rust: bitfield: require integer storage
From: Yilin Chen
Date: Tue Sep 22 2026 - 01:29:17 EST
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@xxxxxx>
---
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
+ {}
impl ::core::convert::From<$name> for $storage {
#[inline(always)]
--
2.25.1