rust-for-linux.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] rust: miscdevice: use `pin_init::zeroed()` for C type initialization
@ 2025-11-28 16:23 Atharv Dubey
  2025-11-28 19:08 ` Miguel Ojeda
  2025-11-29  6:46 ` Dirk Behme
  0 siblings, 2 replies; 5+ messages in thread
From: Atharv Dubey @ 2025-11-28 16:23 UTC (permalink / raw)
  To: arnd, gregkh
  Cc: ojeda, alex.gaynor, boqun.feng, gary, bjorn3_gh, lossin,
	a.hindborg, aliceryhl, tmgross, dakr, rust-for-linux,
	linux-kernel, Atharv Dubey

Replace manual zero-initialization using
`MaybeUninit::zeroed().assume_init()` with `pin_init::zeroed()`.
The `pin_init` helper provides a safer and clearer API for
zero-initializing C structs without requiring an `unsafe` block.

Link: https://github.com/Rust-for-Linux/linux/issues/1189
Signed-off-by: Atharv Dubey <atharvd440@gmail.com>
---
 rust/kernel/miscdevice.rs | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/rust/kernel/miscdevice.rs b/rust/kernel/miscdevice.rs
index d698cddcb4a5..e168e9da8f4a 100644
--- a/rust/kernel/miscdevice.rs
+++ b/rust/kernel/miscdevice.rs
@@ -20,7 +20,7 @@
     seq_file::SeqFile,
     types::{ForeignOwnable, Opaque},
 };
-use core::{marker::PhantomData, mem::MaybeUninit, pin::Pin};
+use core::{marker::PhantomData, pin::Pin};
 
 /// Options for creating a misc device.
 #[derive(Copy, Clone)]
@@ -33,7 +33,7 @@ impl MiscDeviceOptions {
     /// Create a raw `struct miscdev` ready for registration.
     pub const fn into_raw<T: MiscDevice>(self) -> bindings::miscdevice {
         // SAFETY: All zeros is valid for this C type.
-        let mut result: bindings::miscdevice = unsafe { MaybeUninit::zeroed().assume_init() };
+        let mut result: bindings::miscdevice = pin_init::zeroed();
         result.minor = bindings::MISC_DYNAMIC_MINOR as ffi::c_int;
         result.name = crate::str::as_char_ptr_in_const_context(self.name);
         result.fops = MiscdeviceVTable::<T>::build();
@@ -421,7 +421,7 @@ impl<T: MiscDevice> MiscdeviceVTable<T> {
             None
         },
         // SAFETY: All zeros is a valid value for `bindings::file_operations`.
-        ..unsafe { MaybeUninit::zeroed().assume_init() }
+        ..pin_init::zeroed()
     };
 
     const fn build() -> &'static bindings::file_operations {
-- 
2.43.0


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

* Re: [PATCH] rust: miscdevice: use `pin_init::zeroed()` for C type initialization
  2025-11-28 16:23 [PATCH] rust: miscdevice: use `pin_init::zeroed()` for C type initialization Atharv Dubey
@ 2025-11-28 19:08 ` Miguel Ojeda
  2025-11-29  6:46 ` Dirk Behme
  1 sibling, 0 replies; 5+ messages in thread
From: Miguel Ojeda @ 2025-11-28 19:08 UTC (permalink / raw)
  To: Atharv Dubey
  Cc: arnd, gregkh, ojeda, alex.gaynor, boqun.feng, gary, bjorn3_gh,
	lossin, a.hindborg, aliceryhl, tmgross, dakr, rust-for-linux,
	linux-kernel

On Fri, Nov 28, 2025 at 5:23 PM Atharv Dubey <atharvd440@gmail.com> wrote:
>
> Replace manual zero-initialization using
> `MaybeUninit::zeroed().assume_init()` with `pin_init::zeroed()`.
> The `pin_init` helper provides a safer and clearer API for
> zero-initializing C structs without requiring an `unsafe` block.
>
> Link: https://github.com/Rust-for-Linux/linux/issues/1189
> Signed-off-by: Atharv Dubey <atharvd440@gmail.com>

Welcome! I am glad you ended up sending a patch :)

Minor procedural nit: I think this is missing:

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

It should be picked up automatically, no need to resend a v2 just for this.

Thanks!

Cheers,
Miguel

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

* Re: [PATCH] rust: miscdevice: use `pin_init::zeroed()` for C type initialization
  2025-11-28 16:23 [PATCH] rust: miscdevice: use `pin_init::zeroed()` for C type initialization Atharv Dubey
  2025-11-28 19:08 ` Miguel Ojeda
@ 2025-11-29  6:46 ` Dirk Behme
  2025-11-29 16:33   ` Miguel Ojeda
  1 sibling, 1 reply; 5+ messages in thread
From: Dirk Behme @ 2025-11-29  6:46 UTC (permalink / raw)
  To: Atharv Dubey, arnd, gregkh
  Cc: ojeda, alex.gaynor, boqun.feng, gary, bjorn3_gh, lossin,
	a.hindborg, aliceryhl, tmgross, dakr, rust-for-linux,
	linux-kernel

On 28.11.25 17:23, Atharv Dubey wrote:
> Replace manual zero-initialization using
> `MaybeUninit::zeroed().assume_init()` with `pin_init::zeroed()`.
> The `pin_init` helper provides a safer and clearer API for
> zero-initializing C structs without requiring an `unsafe` block.
> 
> Link: https://github.com/Rust-for-Linux/linux/issues/1189
> Signed-off-by: Atharv Dubey <atharvd440@gmail.com>
> ---
>  rust/kernel/miscdevice.rs | 6 +++---
>  1 file changed, 3 insertions(+), 3 deletions(-)
> 
> diff --git a/rust/kernel/miscdevice.rs b/rust/kernel/miscdevice.rs
> index d698cddcb4a5..e168e9da8f4a 100644
> --- a/rust/kernel/miscdevice.rs
> +++ b/rust/kernel/miscdevice.rs
> @@ -20,7 +20,7 @@
>      seq_file::SeqFile,
>      types::{ForeignOwnable, Opaque},
>  };
> -use core::{marker::PhantomData, mem::MaybeUninit, pin::Pin};
> +use core::{marker::PhantomData, pin::Pin};
>  
>  /// Options for creating a misc device.
>  #[derive(Copy, Clone)]
> @@ -33,7 +33,7 @@ impl MiscDeviceOptions {
>      /// Create a raw `struct miscdev` ready for registration.
>      pub const fn into_raw<T: MiscDevice>(self) -> bindings::miscdevice {
>          // SAFETY: All zeros is valid for this C type.
> -        let mut result: bindings::miscdevice = unsafe { MaybeUninit::zeroed().assume_init() };
> +        let mut result: bindings::miscdevice = pin_init::zeroed();

While getting rid of the `unsafe`what's about dropping the `SAFETY`
comment as well? As done by Benno in [1]?

Cheers

Dirk

[1]
https://lore.kernel.org/all/20250814093046.2071971-4-lossin@kernel.org/


>          result.minor = bindings::MISC_DYNAMIC_MINOR as ffi::c_int;
>          result.name = crate::str::as_char_ptr_in_const_context(self.name);
>          result.fops = MiscdeviceVTable::<T>::build();
> @@ -421,7 +421,7 @@ impl<T: MiscDevice> MiscdeviceVTable<T> {
>              None
>          },
>          // SAFETY: All zeros is a valid value for `bindings::file_operations`.
> -        ..unsafe { MaybeUninit::zeroed().assume_init() }
> +        ..pin_init::zeroed()
>      };
>  
>      const fn build() -> &'static bindings::file_operations {


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

* Re: [PATCH] rust: miscdevice: use `pin_init::zeroed()` for C type initialization
  2025-11-29  6:46 ` Dirk Behme
@ 2025-11-29 16:33   ` Miguel Ojeda
  2025-12-01 14:24     ` Atharv Dubey
  0 siblings, 1 reply; 5+ messages in thread
From: Miguel Ojeda @ 2025-11-29 16:33 UTC (permalink / raw)
  To: Dirk Behme
  Cc: Atharv Dubey, arnd, gregkh, ojeda, alex.gaynor, boqun.feng, gary,
	bjorn3_gh, lossin, a.hindborg, aliceryhl, tmgross, dakr,
	rust-for-linux, linux-kernel

On Sat, Nov 29, 2025 at 7:46 AM Dirk Behme <dirk.behme@gmail.com> wrote:
>
> While getting rid of the `unsafe`what's about dropping the `SAFETY`
> comment as well? As done by Benno in [1]?

Yeah -- Atharv: please make sure to check your patches with `CLIPPY=1`
(which should detect that), `rustdoc`, `rusttest`... Some notes at:

    https://rust-for-linux.com/contributing#submit-checklist-addendum

Thanks!

Cheers,
Miguel

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

* Re: [PATCH] rust: miscdevice: use `pin_init::zeroed()` for C type initialization
  2025-11-29 16:33   ` Miguel Ojeda
@ 2025-12-01 14:24     ` Atharv Dubey
  0 siblings, 0 replies; 5+ messages in thread
From: Atharv Dubey @ 2025-12-01 14:24 UTC (permalink / raw)
  To: Miguel Ojeda
  Cc: Dirk Behme, arnd, gregkh, ojeda, alex.gaynor, boqun.feng, gary,
	bjorn3_gh, lossin, a.hindborg, aliceryhl, tmgross, dakr,
	rust-for-linux, linux-kernel

On Sat, Nov 29, 2025 at 05:33:28PM +0100, Miguel Ojeda wrote:
> On Sat, Nov 29, 2025 at 7:46 AM Dirk Behme <dirk.behme@gmail.com> wrote:
> >
> > While getting rid of the `unsafe`what's about dropping the `SAFETY`
> > comment as well? As done by Benno in [1]?
> 
> Yeah -- Atharv: please make sure to check your patches with `CLIPPY=1`
> (which should detect that), `rustdoc`, `rusttest`... Some notes at:
> 
>     https://rust-for-linux.com/contributing#submit-checklist-addendum
> 
> Thanks!
> 
> Cheers,
> Miguel 

>Sure,will keep this in mind while sending out patches.  

-Atharv

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

end of thread, other threads:[~2025-12-01 14:24 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-11-28 16:23 [PATCH] rust: miscdevice: use `pin_init::zeroed()` for C type initialization Atharv Dubey
2025-11-28 19:08 ` Miguel Ojeda
2025-11-29  6:46 ` Dirk Behme
2025-11-29 16:33   ` Miguel Ojeda
2025-12-01 14:24     ` Atharv Dubey

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).