Re: [PATCH v3] rust: pwm: replace `core::mem::zeroed` with `pin_init::zeroed`

Michal Wilczynski <[email protected]> Mon, 27 Jul 2026 13:15:14 +0200
Newsgroups org.kernel.vger.linux-pwm,org.kernel.vger.linux-kernel,org.kernel.vger.rust-for-linux
Message-ID <[email protected]>
Hi Uwe,

On 6/3/26 18:09, Francis Laniel wrote:
> 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.
> 
> Suggested-by: Benno Lossin <[email protected]>
> Link: https://protect2.fireeye.com/v1/url?k=851847ef-eca32f5d-8519cca0-905a08a8515a-5adce8ef7cc40eb1&q=1&e=6c6d6a32-0f0e-4805-8882-718eae7641a8&u=https%3A%2F%2Fgithub.com%2FRust-for-Linux%2Flinux%2Fissues%2F1189
> Reviewed-by: Alexandre Courbot <[email protected]>
> Acked-by: Michal Wilczynski <[email protected]>
> Signed-off-by: Francis Laniel <[email protected]>
> ---
>  rust/kernel/pwm.rs | 4 +---
>  1 file changed, 1 insertion(+), 3 deletions(-)
> 
> diff --git a/rust/kernel/pwm.rs b/rust/kernel/pwm.rs
> index 6c9d667009ef..3427b7d93a03 100644
> --- a/rust/kernel/pwm.rs
> +++ b/rust/kernel/pwm.rs
> @@ -494,9 +494,7 @@ pub(crate) fn as_raw(&self) -> *const bindings::pwm_ops {
>  /// This is used to bridge Rust trait implementations to the C `struct pwm_ops`
>  /// expected by the kernel.
>  pub const fn create_pwm_ops<T: PwmOps>() -> PwmOpsVTable {
> -    // SAFETY: `core::mem::zeroed()` is unsafe. For `pwm_ops`, all fields are
> -    // `Option<extern "C" fn(...)>` or data, so a zeroed pattern (None/0) is valid initially.
> -    let mut ops: bindings::pwm_ops = unsafe { core::mem::zeroed() };
> +    let mut ops: bindings::pwm_ops = pin_init::zeroed();
>  
>      ops.request = Some(Adapter::<T>::request_callback);
>      ops.capture = Some(Adapter::<T>::capture_callback);

I believe this commit is good to go, if you could take it through your
tree it would be great.

Best regards,
-- 
Michal Wilczynski <[email protected]>