Rust for Linux List
 help / color / mirror / Atom feed
* [PATCH v1] rust: pwm: use pin_init::zeroed() to initialize pwm_ops
@ 2026-05-29 15:38 ` Francis Laniel
  2026-05-29 16:45   ` Miguel Ojeda
                     ` (2 more replies)
  0 siblings, 3 replies; 5+ messages in thread
From: Francis Laniel @ 2026-05-29 15:38 UTC (permalink / raw)
  To: Michal Wilczynski, Miguel Ojeda, Uwe Kleine-König,
	Benno Lossin, Gary Guo
  Cc: Francis Laniel, Boqun Feng, Björn Roy Baron,
	Andreas Hindborg, Alice Ryhl, Trevor Gross, Danilo Krummrich,
	linux-pwm, rust-for-linux, linux-kernel

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://github.com/Rust-for-Linux/linux/issues/1189
Signed-off-by: Francis Laniel <laniel_francis@privacyrequired.com>
---
 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);
-- 
2.47.3


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [PATCH v1] rust: pwm: use pin_init::zeroed() to initialize pwm_ops
  2026-05-29 15:38 ` [PATCH v1] rust: pwm: use pin_init::zeroed() to initialize pwm_ops Francis Laniel
@ 2026-05-29 16:45   ` Miguel Ojeda
  2026-06-02  7:04     ` Francis Laniel
  2026-05-30  4:55   ` Alexandre Courbot
  2026-05-30 17:49   ` Michal Wilczynski
  2 siblings, 1 reply; 5+ messages in thread
From: Miguel Ojeda @ 2026-05-29 16:45 UTC (permalink / raw)
  To: Francis Laniel
  Cc: Michal Wilczynski, Miguel Ojeda, Uwe Kleine-König,
	Benno Lossin, Gary Guo, Boqun Feng, Björn Roy Baron,
	Andreas Hindborg, Alice Ryhl, Trevor Gross, Danilo Krummrich,
	linux-pwm, rust-for-linux, linux-kernel

On Fri, May 29, 2026 at 5:48 PM Francis Laniel
<laniel_francis@privacyrequired.com> 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().

It is definitely safer, but it is actually fully safe, i.e. it is a
safe `const fn` after all, which is great. I would say removing
`unsafe` code is the justification, even if of course the function is
able to be safe thanks to implementing the trait only for certain
types. (Also missing `:`).

I would suggest reusing Benno's commit message from the series he
links in the issue instead, e.g.

  https://lore.kernel.org/all/20250814093046.2071971-8-lossin@kernel.org/

> Link: https://github.com/Rust-for-Linux/linux/issues/1189

Please use Suggested-by like the issue mentions :)

Suggested-by: Benno Lossin <lossin@kernel.org>

Thanks!

Cheers,
Miguel

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH v1] rust: pwm: use pin_init::zeroed() to initialize pwm_ops
  2026-05-29 15:38 ` [PATCH v1] rust: pwm: use pin_init::zeroed() to initialize pwm_ops Francis Laniel
  2026-05-29 16:45   ` Miguel Ojeda
@ 2026-05-30  4:55   ` Alexandre Courbot
  2026-05-30 17:49   ` Michal Wilczynski
  2 siblings, 0 replies; 5+ messages in thread
From: Alexandre Courbot @ 2026-05-30  4:55 UTC (permalink / raw)
  To: Francis Laniel
  Cc: Michal Wilczynski, Miguel Ojeda, Uwe Kleine-König,
	Benno Lossin, Gary Guo, Boqun Feng, Björn Roy Baron,
	Andreas Hindborg, Alice Ryhl, Trevor Gross, Danilo Krummrich,
	linux-pwm, rust-for-linux, linux-kernel

On Sat May 30, 2026 at 12:38 AM JST, 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().

Typo: core::mem::zeroed() (probably moot if you reuse Benno's commit
message as suggested by Miguel though).

> Also, we can call this because all fields in pwm_ops, i.e. function
> pointers in Option<> and usize, are Zeroable.
>
> Link: https://github.com/Rust-for-Linux/linux/issues/1189
> Signed-off-by: Francis Laniel <laniel_francis@privacyrequired.com>

Reviewed-by: Alexandre Courbot <acourbot@nvidia.com>

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH v1] rust: pwm: use pin_init::zeroed() to initialize pwm_ops
  2026-05-29 15:38 ` [PATCH v1] rust: pwm: use pin_init::zeroed() to initialize pwm_ops Francis Laniel
  2026-05-29 16:45   ` Miguel Ojeda
  2026-05-30  4:55   ` Alexandre Courbot
@ 2026-05-30 17:49   ` Michal Wilczynski
  2 siblings, 0 replies; 5+ messages in thread
From: Michal Wilczynski @ 2026-05-30 17:49 UTC (permalink / raw)
  To: Francis Laniel, Miguel Ojeda, Uwe Kleine-König, Benno Lossin,
	Gary Guo
  Cc: Boqun Feng, Björn Roy Baron, Andreas Hindborg, Alice Ryhl,
	Trevor Gross, Danilo Krummrich, linux-pwm, rust-for-linux,
	linux-kernel



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@privacyrequired.com>
> ---
>  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@samsung.com>


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH v1] rust: pwm: use pin_init::zeroed() to initialize pwm_ops
  2026-05-29 16:45   ` Miguel Ojeda
@ 2026-06-02  7:04     ` Francis Laniel
  0 siblings, 0 replies; 5+ messages in thread
From: Francis Laniel @ 2026-06-02  7:04 UTC (permalink / raw)
  To: Miguel Ojeda
  Cc: Michal Wilczynski, Miguel Ojeda, Uwe Kleine-König,
	Benno Lossin, Gary Guo, Boqun Feng, Björn Roy Baron,
	Andreas Hindborg, Alice Ryhl, Trevor Gross, Danilo Krummrich,
	linux-pwm, rust-for-linux, linux-kernel

Hi,

Le vendredi 29 mai 2026, 19:45:06 heure d’été d’Europe de l’Est Miguel Ojeda a 
écrit :
> On Fri, May 29, 2026 at 5:48 PM Francis Laniel
> 
> <laniel_francis@privacyrequired.com> 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().
> 
> It is definitely safer, but it is actually fully safe, i.e. it is a
> safe `const fn` after all, which is great. I would say removing
> `unsafe` code is the justification, even if of course the function is
> able to be safe thanks to implementing the trait only for certain
> types. (Also missing `:`).
> 
> I would suggest reusing Benno's commit message from the series he
> links in the issue instead, e.g.
> 
>   https://lore.kernel.org/all/20250814093046.2071971-8-lossin@kernel.org/
> 
> > Link: https://github.com/Rust-for-Linux/linux/issues/1189
> 
> Please use Suggested-by like the issue mentions :)
> 
> Suggested-by: Benno Lossin <lossin@kernel.org>

I read the issue too fast! I will send a v2 with the corresponding tags and 
new commit message! Thank you for your suggestions!

> Thanks!
> 
> Cheers,
> Miguel

Best regards,



^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2026-06-02  7:12 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
     [not found] <CGME20260529154838eucas1p25f3bf881090a8a80556bc2f1dfc278b4@eucas1p2.samsung.com>
2026-05-29 15:38 ` [PATCH v1] rust: pwm: use pin_init::zeroed() to initialize pwm_ops Francis Laniel
2026-05-29 16:45   ` Miguel Ojeda
2026-06-02  7:04     ` Francis Laniel
2026-05-30  4:55   ` Alexandre Courbot
2026-05-30 17:49   ` Michal Wilczynski

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox