Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

rust: pin-init: move impl `Zeroable` for `Opaque` and `Option<KBox<T>>` into the kernel crate

In order to make pin-init a standalone crate, move kernel-specific code
directly into the kernel crate. Since `Opaque<T>` and `KBox<T>` are part
of the kernel, move their `Zeroable` implementation into the kernel
crate.

Signed-off-by: Benno Lossin <benno.lossin@proton.me>
Tested-by: Andreas Hindborg <a.hindborg@kernel.org>
Reviewed-by: Fiona Behrens <me@kloenk.dev>
Link: https://lore.kernel.org/r/20250308110339.2997091-10-benno.lossin@proton.me
Signed-off-by: Miguel Ojeda <ojeda@kernel.org>

authored by

Benno Lossin and committed by
Miguel Ojeda
9d29c682 114ca41f

+12 -9
+7 -1
rust/kernel/alloc/kbox.rs
··· 15 15 use core::ptr::NonNull; 16 16 use core::result::Result; 17 17 18 - use crate::init::{InPlaceWrite, Init, PinInit}; 18 + use crate::init::{InPlaceWrite, Init, PinInit, Zeroable}; 19 19 use crate::init_ext::InPlaceInit; 20 20 use crate::types::ForeignOwnable; 21 21 ··· 99 99 /// # Ok::<(), Error>(()) 100 100 /// ``` 101 101 pub type KVBox<T> = Box<T, super::allocator::KVmalloc>; 102 + 103 + // SAFETY: All zeros is equivalent to `None` (option layout optimization guarantee). 104 + // 105 + // In this case we are allowed to use `T: ?Sized`, since all zeros is the `None` variant and there 106 + // is no problem with a VTABLE pointer being null. 107 + unsafe impl<T: ?Sized, A: Allocator> Zeroable for Option<Box<T, A>> {} 102 108 103 109 // SAFETY: `Box` is `Send` if `T` is `Send` because the `Box` owns a `T`. 104 110 unsafe impl<T, A> Send for Box<T, A>
+4 -1
rust/kernel/types.rs
··· 2 2 3 3 //! Kernel types. 4 4 5 - use crate::init::{self, PinInit}; 5 + use crate::init::{self, PinInit, Zeroable}; 6 6 use core::{ 7 7 cell::UnsafeCell, 8 8 marker::{PhantomData, PhantomPinned}, ··· 308 308 value: UnsafeCell<MaybeUninit<T>>, 309 309 _pin: PhantomPinned, 310 310 } 311 + 312 + // SAFETY: `Opaque<T>` allows the inner value to be any bit pattern, including all zeros. 313 + unsafe impl<T> Zeroable for Opaque<T> {} 311 314 312 315 impl<T> Opaque<T> { 313 316 /// Creates a new opaque value.
+1 -7
rust/pin-init/src/lib.rs
··· 211 211 //! [`pin_data`]: ::macros::pin_data 212 212 //! [`pin_init!`]: crate::pin_init! 213 213 214 - use crate::{ 215 - alloc::KBox, 216 - types::{Opaque, ScopeGuard}, 217 - }; 214 + use crate::{alloc::KBox, types::ScopeGuard}; 218 215 use core::{ 219 216 cell::UnsafeCell, 220 217 convert::Infallible, ··· 1339 1342 1340 1343 // SAFETY: Type is allowed to take any value, including all zeros. 1341 1344 {<T>} MaybeUninit<T>, 1342 - // SAFETY: Type is allowed to take any value, including all zeros. 1343 - {<T>} Opaque<T>, 1344 1345 1345 1346 // SAFETY: `T: Zeroable` and `UnsafeCell` is `repr(transparent)`. 1346 1347 {<T: ?Sized + Zeroable>} UnsafeCell<T>, ··· 1353 1358 // 1354 1359 // In this case we are allowed to use `T: ?Sized`, since all zeros is the `None` variant. 1355 1360 {<T: ?Sized>} Option<NonNull<T>>, 1356 - {<T: ?Sized>} Option<KBox<T>>, 1357 1361 1358 1362 // SAFETY: `null` pointer is valid. 1359 1363 //