Skip to contentSkip to conversation

rust: cpumask: Replace `MaybeUninit` and `mem::zeroed` with `Opaque` APIs

PATCH In review Ritvik Gupta 3 participants 7 messages Jul 13, 2025 – Jul 16, 2025

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 rust: cpumask: Replace `MaybeUninit` and `mem::zeroed` with `Opaque` APIs

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

---

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/[email protected]/

---
 rust/kernel/cpumask.rs | 13 ++-----------
 1 file changed, 2 insertions(+), 11 deletions(-)
rust/kernel/cpumask.rs +2 -11
index 4bce230a73b6..3fcbff438670 100644
@@ -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

Signature · 1 line
2.50.1
maintainer Acked-by on v2 1/1
#1 lore raw
Quoted 11 lines · Ritvik Gupta

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

Signature · 1 line
viresh
maintainer
#3 lore raw
Quoted 5 lines · Miguel Ojeda
Quoted 2 lines · Viresh Kumar

Wouldn't this go via your pm tree to Rafael? Or am I confused?

Applied now.

I manage pull requests for cpufreq and OPP normally. I had to to cpumask, clk, etc., as dependencies for my Rust stuff but I am not sure about what's the best way to get them merged.

For now I can take this one though.

Signature · 1 line
viresh
#4 lore raw
Quoted 8 lines · Viresh Kumar

Applied now.

I manage pull requests for cpufreq and OPP normally. I had to to cpumask, clk, etc., as dependencies for my Rust stuff but I am not sure about what's the best way to get them merged.

For now I can take this one though.

Ah, I thought the idea was to continue like that.

If that was not the case, then it should be clarified. Do you have a pointer to the latest discussion on this?

Thanks!

Cheers,
Miguel

maintainer
#5 lore raw
Quoted 1 line · Miguel Ojeda

Ah, I thought the idea was to continue like that.

I will keep pushing the changes via the PM tree for now.

Signature · 1 line
viresh
#6 lore raw
Quoted 2 lines · Viresh Kumar

I will keep pushing the changes via the PM tree for now.

Ok, great, thanks -- please let me know if there is a renewed discussion on that or if we can help somehow in the future.

Cheers,
Miguel