From: Benno Lossin Date: Sat, 8 Mar 2025 11:04:38 +0000 (+0000) Subject: rust: pin-init: move impl `Zeroable` for `Opaque` and `Option>` into the... X-Git-Tag: block-6.15-20250403~14^2~33 X-Git-Url: https://git.kernel.dk/?a=commitdiff_plain;h=9d29c682f00c3d8dd5727f6a350c4f6ecccc3913;p=linux-block.git rust: pin-init: move impl `Zeroable` for `Opaque` and `Option>` into the kernel crate In order to make pin-init a standalone crate, move kernel-specific code directly into the kernel crate. Since `Opaque` and `KBox` are part of the kernel, move their `Zeroable` implementation into the kernel crate. Signed-off-by: Benno Lossin Tested-by: Andreas Hindborg Reviewed-by: Fiona Behrens Link: https://lore.kernel.org/r/20250308110339.2997091-10-benno.lossin@proton.me Signed-off-by: Miguel Ojeda --- diff --git a/rust/kernel/alloc/kbox.rs b/rust/kernel/alloc/kbox.rs index 39a3ea7542da..9861433559dc 100644 --- a/rust/kernel/alloc/kbox.rs +++ b/rust/kernel/alloc/kbox.rs @@ -15,7 +15,7 @@ use core::pin::Pin; use core::ptr::NonNull; use core::result::Result; -use crate::init::{InPlaceWrite, Init, PinInit}; +use crate::init::{InPlaceWrite, Init, PinInit, Zeroable}; use crate::init_ext::InPlaceInit; use crate::types::ForeignOwnable; @@ -100,6 +100,12 @@ pub type VBox = Box; /// ``` pub type KVBox = Box; +// SAFETY: All zeros is equivalent to `None` (option layout optimization guarantee). +// +// In this case we are allowed to use `T: ?Sized`, since all zeros is the `None` variant and there +// is no problem with a VTABLE pointer being null. +unsafe impl Zeroable for Option> {} + // SAFETY: `Box` is `Send` if `T` is `Send` because the `Box` owns a `T`. unsafe impl Send for Box where diff --git a/rust/kernel/types.rs b/rust/kernel/types.rs index 5801eeb69dc5..7237b2224680 100644 --- a/rust/kernel/types.rs +++ b/rust/kernel/types.rs @@ -2,7 +2,7 @@ //! Kernel types. -use crate::init::{self, PinInit}; +use crate::init::{self, PinInit, Zeroable}; use core::{ cell::UnsafeCell, marker::{PhantomData, PhantomPinned}, @@ -309,6 +309,9 @@ pub struct Opaque { _pin: PhantomPinned, } +// SAFETY: `Opaque` allows the inner value to be any bit pattern, including all zeros. +unsafe impl Zeroable for Opaque {} + impl Opaque { /// Creates a new opaque value. pub const fn new(value: T) -> Self { diff --git a/rust/pin-init/src/lib.rs b/rust/pin-init/src/lib.rs index f88465e0bb76..aad6486d33fc 100644 --- a/rust/pin-init/src/lib.rs +++ b/rust/pin-init/src/lib.rs @@ -211,10 +211,7 @@ //! [`pin_data`]: ::macros::pin_data //! [`pin_init!`]: crate::pin_init! -use crate::{ - alloc::KBox, - types::{Opaque, ScopeGuard}, -}; +use crate::{alloc::KBox, types::ScopeGuard}; use core::{ cell::UnsafeCell, convert::Infallible, @@ -1342,8 +1339,6 @@ impl_zeroable! { // SAFETY: Type is allowed to take any value, including all zeros. {} MaybeUninit, - // SAFETY: Type is allowed to take any value, including all zeros. - {} Opaque, // SAFETY: `T: Zeroable` and `UnsafeCell` is `repr(transparent)`. {} UnsafeCell, @@ -1358,7 +1353,6 @@ impl_zeroable! { // // In this case we are allowed to use `T: ?Sized`, since all zeros is the `None` variant. {} Option>, - {} Option>, // SAFETY: `null` pointer is valid. //