]> git.ipfire.org Git - thirdparty/kernel/linux.git/commitdiff
rust: init: implement `Zeroable` for `UnsafeCell<T>` and `Opaque<T>`
authorBenno Lossin <benno.lossin@proton.me>
Mon, 14 Aug 2023 08:47:32 +0000 (08:47 +0000)
committerMiguel Ojeda <ojeda@kernel.org>
Mon, 21 Aug 2023 12:31:49 +0000 (14:31 +0200)
`UnsafeCell<T>` and `T` have the same layout so if `T` is `Zeroable`
then so should `UnsafeCell<T>` be. This allows using the derive macro
for `Zeroable` on types that contain an `UnsafeCell<T>`.
Since `Opaque<T>` contains a `MaybeUninit<T>`, all bytes zero is a valid
bit pattern for that type.

Reviewed-by: Gary Guo <gary@garyguo.net>
Reviewed-by: Martin Rodriguez Reboredo <yakoyoku@gmail.com>
Signed-off-by: Benno Lossin <benno.lossin@proton.me>
Link: https://lore.kernel.org/r/20230814084602.25699-11-benno.lossin@proton.me
Signed-off-by: Miguel Ojeda <ojeda@kernel.org>
rust/kernel/init.rs

index 429b485d8825c0c0d175589e00f0e80d1be996cb..0e44b3cc2eeda322cdbadd9e6d743ee0856a3f8f 100644 (file)
 use crate::{
     error::{self, Error},
     sync::UniqueArc,
-    types::ScopeGuard,
+    types::{Opaque, ScopeGuard},
 };
 use alloc::boxed::Box;
 use core::{
     alloc::AllocError,
+    cell::UnsafeCell,
     convert::Infallible,
     marker::PhantomData,
     mem::MaybeUninit,
@@ -1151,6 +1152,11 @@ impl_zeroable! {
 
     // SAFETY: Type is allowed to take any value, including all zeros.
     {<T>} MaybeUninit<T>,
+    // SAFETY: Type is allowed to take any value, including all zeros.
+    {<T>} Opaque<T>,
+
+    // SAFETY: `T: Zeroable` and `UnsafeCell` is `repr(transparent)`.
+    {<T: ?Sized + Zeroable>} UnsafeCell<T>,
 
     // SAFETY: All zeros is equivalent to `None` (option layout optimization guarantee).
     Option<NonZeroU8>, Option<NonZeroU16>, Option<NonZeroU32>, Option<NonZeroU64>,