rust-for-linux.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v2] rust: cpumask: Replace `MaybeUninit` and `mem::zeroed` with `Opaque` APIs
@ 2025-07-13 19:02 Ritvik Gupta
  2025-07-14  4:54 ` Viresh Kumar
  0 siblings, 1 reply; 7+ messages in thread
From: Ritvik Gupta @ 2025-07-13 19:02 UTC (permalink / raw)
  To: viresh.kumar, yury.norov, ojeda, alex.gaynor, boqun.feng, gary,
	bjorn3_gh, a.hindborg, tmgross, dakr
  Cc: rust-for-linux, skhan, Benno Lossin, Alice Ryhl

Replace the following unsafe initializations:
1. `MaybeUninit::uninit().assume_init()` with `Opaque::uninit()`
2. `core::mem::zeroed()` with `Opaque::zeroed()`

Suggested-by: Benno Lossin <lossin@kernel.org>
Link: https://github.com/Rust-for-Linux/linux/issues/1178
Suggested-by: Alice Ryhl <aliceryhl@google.com>
Link: https://lore.kernel.org/rust-for-linux/CAH5fLgj0OoCn56OkNUmiPQ=RAVa_VmS-yMZ4TNBSpGPNtZ5D0A@mail.gmail.com/
Reviewed-by: Benno Lossin <lossin@kernel.org>
Reviewed-by: Alice Ryhl <aliceryhl@google.com>
Signed-off-by: Ritvik Gupta <ritvikfoss@gmail.com>
---

Carried forward 'Reviewed-by' tags from v1 since there're no substantial changes, just added suggested changes :)
(reviewers from v1 cc'd)

Changes in v2:
 - Replaced `core::mem::zeroed()` with `Opaque::zeroed()` in `CpumaskVar::new_zero()` (thanks, Alice!)

Link to v1: https://lore.kernel.org/rust-for-linux/20250710123836.146825-1-ritvikfoss@gmail.com/

---
 rust/kernel/cpumask.rs | 13 ++-----------
 1 file changed, 2 insertions(+), 11 deletions(-)

diff --git a/rust/kernel/cpumask.rs b/rust/kernel/cpumask.rs
index 4bce230a73b6..3fcbff438670 100644
--- a/rust/kernel/cpumask.rs
+++ b/rust/kernel/cpumask.rs
@@ -14,9 +14,6 @@
 #[cfg(CONFIG_CPUMASK_OFFSTACK)]
 use core::ptr::{self, NonNull};
 
-#[cfg(not(CONFIG_CPUMASK_OFFSTACK))]
-use core::mem::MaybeUninit;
-
 use core::ops::{Deref, DerefMut};
 
 /// A CPU Mask.
@@ -239,10 +236,7 @@ pub fn new_zero(_flags: Flags) -> Result<Self, AllocError> {
             },
 
             #[cfg(not(CONFIG_CPUMASK_OFFSTACK))]
-            // SAFETY: FFI type is valid to be zero-initialized.
-            //
-            // INVARIANT: The associated memory is freed when the `CpumaskVar` goes out of scope.
-            mask: unsafe { core::mem::zeroed() },
+            mask: Cpumask(Opaque::zeroed()),
         })
     }
 
@@ -266,10 +260,7 @@ pub unsafe fn new(_flags: Flags) -> Result<Self, AllocError> {
                 NonNull::new(ptr.cast()).ok_or(AllocError)?
             },
             #[cfg(not(CONFIG_CPUMASK_OFFSTACK))]
-            // SAFETY: Guaranteed by the safety requirements of the function.
-            //
-            // INVARIANT: The associated memory is freed when the `CpumaskVar` goes out of scope.
-            mask: unsafe { MaybeUninit::uninit().assume_init() },
+            mask: Cpumask(Opaque::uninit()),
         })
     }
 

base-commit: 2009a2d5696944d85c34d75e691a6f3884e787c0
-- 
2.50.1


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

end of thread, other threads:[~2025-07-16 12:40 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-07-13 19:02 [PATCH v2] rust: cpumask: Replace `MaybeUninit` and `mem::zeroed` with `Opaque` APIs Ritvik Gupta
2025-07-14  4:54 ` Viresh Kumar
2025-07-14 16:53   ` Miguel Ojeda
2025-07-15  3:06     ` Viresh Kumar
2025-07-15 11:18       ` Miguel Ojeda
2025-07-16  4:15         ` Viresh Kumar
2025-07-16 12:40           ` Miguel Ojeda

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).