From: Atharv Dubey <atharvd440@gmail.com>
To: arnd@arndb.de, gregkh@linuxfoundation.org
Cc: ojeda@kernel.org, alex.gaynor@gmail.com, boqun.feng@gmail.com,
gary@garyguo.net, bjorn3_gh@protonmail.com, lossin@kernel.org,
a.hindborg@kernel.org, aliceryhl@google.com, tmgross@umich.edu,
dakr@kernel.org, rust-for-linux@vger.kernel.org,
linux-kernel@vger.kernel.org, Atharv Dubey <atharvd440@gmail.com>
Subject: [PATCH v2] rust: miscdevice: use `pin_init::zeroed()` for C type initialization
Date: Sat, 29 Nov 2025 17:45:13 +0530 [thread overview]
Message-ID: <20251129121513.20738-1-atharvd440@gmail.com> (raw)
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>
---
Changes in v2:
- Remove the Unwanted Comments.
rust/kernel/miscdevice.rs | 8 +++-----
1 file changed, 3 insertions(+), 5 deletions(-)
diff --git a/rust/kernel/miscdevice.rs b/rust/kernel/miscdevice.rs
index d698cddcb4a5..ba64c8a858f0 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)]
@@ -32,8 +32,7 @@ pub struct MiscDeviceOptions {
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();
@@ -420,8 +419,7 @@ impl<T: MiscDevice> MiscdeviceVTable<T> {
} else {
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
next reply other threads:[~2025-11-29 12:15 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-11-29 12:15 Atharv Dubey [this message]
2025-12-01 3:37 ` [PATCH v2] rust: miscdevice: use `pin_init::zeroed()` for C type initialization Alexandre Courbot
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20251129121513.20738-1-atharvd440@gmail.com \
--to=atharvd440@gmail.com \
--cc=a.hindborg@kernel.org \
--cc=alex.gaynor@gmail.com \
--cc=aliceryhl@google.com \
--cc=arnd@arndb.de \
--cc=bjorn3_gh@protonmail.com \
--cc=boqun.feng@gmail.com \
--cc=dakr@kernel.org \
--cc=gary@garyguo.net \
--cc=gregkh@linuxfoundation.org \
--cc=linux-kernel@vger.kernel.org \
--cc=lossin@kernel.org \
--cc=ojeda@kernel.org \
--cc=rust-for-linux@vger.kernel.org \
--cc=tmgross@umich.edu \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
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).