[PATCH] rust: of: replace `core::mem::zeroed` with `pin_init::zeroed`
From: Nils Lehnen
Date: Fri Aug 28 2026 - 08:31:10 EST
From: Benno Lossin <lossin@xxxxxxxxxx>
All types in `bindings` implement `Zeroable` if they can, so use
`pin_init::zeroed` instead of relying on `unsafe` code.
If this ends up not compiling in the future, something in bindgen or on
the C side changed and is most likely incorrect.
Link: https://patch.msgid.link/20250814093046.2071971-7-lossin@xxxxxxxxxx
Link: https://github.com/Rust-for-Linux/linux/issues/1189
Signed-off-by: Benno Lossin <lossin@xxxxxxxxxx>
Signed-off-by: Nils Lehnen <nils.lehnen@xxxxxxxxx>
---
Resend of Benno's patch from the Zeroable series [1], as the issue [2]
asks; same shape as the security.rs resend Miguel confirmed today [3].
Rebased onto rust-next: one context conflict from the
`as_bytes_with_nul` -> `to_bytes_with_nul` rename, resolved keeping the
current name; the change itself is Benno's, unmodified. An incremental
defconfig build with CONFIG_RUST=y passes, so `pin_init::zeroed()` is
fine in the `const fn` context.
Moritz sent an equivalent patch earlier [4]; Miguel's review asked only
for a formatting fix and no v2 followed.
[1] https://lore.kernel.org/r/20250814093046.2071971-1-lossin@xxxxxxxxxx
[2] https://github.com/Rust-for-Linux/linux/issues/1189
[3] https://lore.kernel.org/r/CANiq72mn2vvCL2VP8yJxmzXM4n+jfV4R7arUcj4UXSi04y5sfQ@xxxxxxxxxxxxxx
[4] https://lore.kernel.org/r/20251030-zeroed-of-rs-v1-1-1c46d025128e@xxxxxxxxx
rust/kernel/of.rs | 4 +---
1 file changed, 1 insertion(+), 3 deletions(-)
diff --git a/rust/kernel/of.rs b/rust/kernel/of.rs
index d0318f62afd7..75d5004d2221 100644
--- a/rust/kernel/of.rs
+++ b/rust/kernel/of.rs
@@ -31,9 +31,7 @@ impl DeviceId {
/// Create a new device id from an OF 'compatible' string.
pub const fn new(compatible: &'static CStr) -> Self {
let src = compatible.to_bytes_with_nul();
- // Replace with `bindings::of_device_id::default()` once stabilized for `const`.
- // SAFETY: FFI type is valid to be zero-initialized.
- let mut of: bindings::of_device_id = unsafe { core::mem::zeroed() };
+ let mut of: bindings::of_device_id = pin_init::zeroed();
// TODO: Use `copy_from_slice` once stabilized for `const`.
let mut i = 0;
base-commit: 73e3f0710014fe6d4ed98cfc02292f6121db7558
--
2.43.0