Skip to contentSkip to conversation

rust: miscdevice: use `pin_init::zeroed()` for C type initialization

PATCH Superseded Atharv Dubey 3 participants 5 messages Nov 28, 2025 – Dec 1, 2025

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 rust: miscdevice: use `pin_init::zeroed()` for C type initialization

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.

---
 rust/kernel/miscdevice.rs | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
rust/kernel/miscdevice.rs +3 -3
index d698cddcb4a5..e168e9da8f4a 100644
@@ -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 {
Signature · 1 line
2.43.0
#1 lore raw
Quoted 8 lines · 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.

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

Minor procedural nit: I think this is missing:

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

Thanks!

Cheers,
Miguel

#2 lore raw
In reply to Atharv Dubey
Quoted 25 lines · 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.

---
 rust/kernel/miscdevice.rs | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
rust/kernel/miscdevice.rs +1 -1
index d698cddcb4a5..e168e9da8f4a 100644
@@ -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)]

rust/kernel/miscdevice.rs impl MiscDeviceOptions { · line 36

Quoted 5 lines
rust/kernel/miscdevice.rs +1 -1
@@ -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 unsafewhat's about dropping the SAFETY comment as well? As done by Benno in [1]?

Cheers

Dirk

[1]
https://lore.kernel.org/all/[email protected]/

Quoted 12 lines
rust/kernel/miscdevice.rs +1 -1
         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 {
#4 lore raw
Quoted 8 lines · Miguel Ojeda
Quoted 3 lines · Dirk Behme

While getting rid of the unsafewhat'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:

Quoted 8 lines

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

Thanks!

Cheers,
Miguel

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

-Atharv