Re: [PATCH v1] rust: pwm: use pin_init::zeroed() to initialize pwm_ops
From: Michal Wilczynski
Date: Sat May 30 2026 - 13:55:35 EST
On 5/29/26 17:38, Francis Laniel wrote:
> Replace unsafe block containing core::mem::zeroed by calling
> pin_init::zeroed().
> This is safer, as this function is guarded by trait contrary to
> core::mem:zeroed().
> Also, we can call this because all fields in pwm_ops, i.e. function
> pointers in Option<> and usize, are Zeroable.
>
> Link: https://protect2.fireeye.com/v1/url?k=c68066dd-991b5fb8-c681ed92-000babff32e3-8cf7da03bc5f790c&q=1&e=7bb43a54-acba-4578-8682-f88365e5bdf6&u=https%3A%2F%2Fgithub.com%2FRust-for-Linux%2Flinux%2Fissues%2F1189
> Signed-off-by: Francis Laniel <laniel_francis@xxxxxxxxxxxxxxxxxxx>
> ---
> 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);
Acked-by: Michal Wilczynski <m.wilczynski@xxxxxxxxxxx>