Skip to content

Commit 9d29c68

Browse files
BennoLossinojeda
authored andcommitted
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 <[email protected]> Tested-by: Andreas Hindborg <[email protected]> Reviewed-by: Fiona Behrens <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Miguel Ojeda <[email protected]>
1 parent 114ca41 commit 9d29c68

File tree

3 files changed

+12
-9
lines changed

3 files changed

+12
-9
lines changed

rust/kernel/alloc/kbox.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use core::pin::Pin;
1515
use core::ptr::NonNull;
1616
use core::result::Result;
1717

18-
use crate::init::{InPlaceWrite, Init, PinInit};
18+
use crate::init::{InPlaceWrite, Init, PinInit, Zeroable};
1919
use crate::init_ext::InPlaceInit;
2020
use crate::types::ForeignOwnable;
2121

@@ -100,6 +100,12 @@ pub type VBox<T> = Box<T, super::allocator::Vmalloc>;
100100
/// ```
101101
pub type KVBox<T> = Box<T, super::allocator::KVmalloc>;
102102

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>> {}
108+
103109
// SAFETY: `Box` is `Send` if `T` is `Send` because the `Box` owns a `T`.
104110
unsafe impl<T, A> Send for Box<T, A>
105111
where

rust/kernel/types.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
//! Kernel types.
44
5-
use crate::init::{self, PinInit};
5+
use crate::init::{self, PinInit, Zeroable};
66
use core::{
77
cell::UnsafeCell,
88
marker::{PhantomData, PhantomPinned},
@@ -309,6 +309,9 @@ pub struct Opaque<T> {
309309
_pin: PhantomPinned,
310310
}
311311

312+
// SAFETY: `Opaque<T>` allows the inner value to be any bit pattern, including all zeros.
313+
unsafe impl<T> Zeroable for Opaque<T> {}
314+
312315
impl<T> Opaque<T> {
313316
/// Creates a new opaque value.
314317
pub const fn new(value: T) -> Self {

rust/pin-init/src/lib.rs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -211,10 +211,7 @@
211211
//! [`pin_data`]: ::macros::pin_data
212212
//! [`pin_init!`]: crate::pin_init!
213213
214-
use crate::{
215-
alloc::KBox,
216-
types::{Opaque, ScopeGuard},
217-
};
214+
use crate::{alloc::KBox, types::ScopeGuard};
218215
use core::{
219216
cell::UnsafeCell,
220217
convert::Infallible,
@@ -1342,8 +1339,6 @@ impl_zeroable! {
13421339

13431340
// SAFETY: Type is allowed to take any value, including all zeros.
13441341
{<T>} MaybeUninit<T>,
1345-
// SAFETY: Type is allowed to take any value, including all zeros.
1346-
{<T>} Opaque<T>,
13471342

13481343
// SAFETY: `T: Zeroable` and `UnsafeCell` is `repr(transparent)`.
13491344
{<T: ?Sized + Zeroable>} UnsafeCell<T>,
@@ -1358,7 +1353,6 @@ impl_zeroable! {
13581353
//
13591354
// In this case we are allowed to use `T: ?Sized`, since all zeros is the `None` variant.
13601355
{<T: ?Sized>} Option<NonNull<T>>,
1361-
{<T: ?Sized>} Option<KBox<T>>,
13621356

13631357
// SAFETY: `null` pointer is valid.
13641358
//

0 commit comments

Comments
 (0)