xref: /openbmc/linux/rust/kernel/init/__internal.rs (revision c900529f3d9161bfde5cca0754f83b4d3c3e0220)
190e53c5eSBenno Lossin // SPDX-License-Identifier: Apache-2.0 OR MIT
290e53c5eSBenno Lossin 
390e53c5eSBenno Lossin //! This module contains API-internal items for pin-init.
490e53c5eSBenno Lossin //!
590e53c5eSBenno Lossin //! These items must not be used outside of
690e53c5eSBenno Lossin //! - `kernel/init.rs`
790e53c5eSBenno Lossin //! - `macros/pin_data.rs`
890e53c5eSBenno Lossin //! - `macros/pinned_drop.rs`
990e53c5eSBenno Lossin 
1090e53c5eSBenno Lossin use super::*;
1190e53c5eSBenno Lossin 
1290e53c5eSBenno Lossin /// See the [nomicon] for what subtyping is. See also [this table].
1390e53c5eSBenno Lossin ///
1490e53c5eSBenno Lossin /// [nomicon]: https://doc.rust-lang.org/nomicon/subtyping.html
1590e53c5eSBenno Lossin /// [this table]: https://doc.rust-lang.org/nomicon/phantom-data.html#table-of-phantomdata-patterns
16*7f8977a7SBenno Lossin pub(super) type Invariant<T> = PhantomData<fn(*mut T) -> *mut T>;
1790e53c5eSBenno Lossin 
1890e53c5eSBenno Lossin /// This is the module-internal type implementing `PinInit` and `Init`. It is unsafe to create this
1990e53c5eSBenno Lossin /// type, since the closure needs to fulfill the same safety requirement as the
2090e53c5eSBenno Lossin /// `__pinned_init`/`__init` functions.
2190e53c5eSBenno Lossin pub(crate) struct InitClosure<F, T: ?Sized, E>(pub(crate) F, pub(crate) Invariant<(E, T)>);
2290e53c5eSBenno Lossin 
2390e53c5eSBenno Lossin // SAFETY: While constructing the `InitClosure`, the user promised that it upholds the
2490e53c5eSBenno Lossin // `__init` invariants.
2590e53c5eSBenno Lossin unsafe impl<T: ?Sized, F, E> Init<T, E> for InitClosure<F, T, E>
2690e53c5eSBenno Lossin where
2790e53c5eSBenno Lossin     F: FnOnce(*mut T) -> Result<(), E>,
2890e53c5eSBenno Lossin {
2990e53c5eSBenno Lossin     #[inline]
__init(self, slot: *mut T) -> Result<(), E>3090e53c5eSBenno Lossin     unsafe fn __init(self, slot: *mut T) -> Result<(), E> {
3190e53c5eSBenno Lossin         (self.0)(slot)
3290e53c5eSBenno Lossin     }
3390e53c5eSBenno Lossin }
34fc6c6baaSBenno Lossin 
351a8076acSBenno Lossin // SAFETY: While constructing the `InitClosure`, the user promised that it upholds the
361a8076acSBenno Lossin // `__pinned_init` invariants.
371a8076acSBenno Lossin unsafe impl<T: ?Sized, F, E> PinInit<T, E> for InitClosure<F, T, E>
381a8076acSBenno Lossin where
391a8076acSBenno Lossin     F: FnOnce(*mut T) -> Result<(), E>,
401a8076acSBenno Lossin {
411a8076acSBenno Lossin     #[inline]
__pinned_init(self, slot: *mut T) -> Result<(), E>421a8076acSBenno Lossin     unsafe fn __pinned_init(self, slot: *mut T) -> Result<(), E> {
431a8076acSBenno Lossin         (self.0)(slot)
441a8076acSBenno Lossin     }
451a8076acSBenno Lossin }
461a8076acSBenno Lossin 
47fc6c6baaSBenno Lossin /// This trait is only implemented via the `#[pin_data]` proc-macro. It is used to facilitate
48fc6c6baaSBenno Lossin /// the pin projections within the initializers.
49fc6c6baaSBenno Lossin ///
50fc6c6baaSBenno Lossin /// # Safety
51fc6c6baaSBenno Lossin ///
52fc6c6baaSBenno Lossin /// Only the `init` module is allowed to use this trait.
53fc6c6baaSBenno Lossin pub unsafe trait HasPinData {
54fc6c6baaSBenno Lossin     type PinData: PinData;
55fc6c6baaSBenno Lossin 
__pin_data() -> Self::PinData56fc6c6baaSBenno Lossin     unsafe fn __pin_data() -> Self::PinData;
57fc6c6baaSBenno Lossin }
58fc6c6baaSBenno Lossin 
59fc6c6baaSBenno Lossin /// Marker trait for pinning data of structs.
60fc6c6baaSBenno Lossin ///
61fc6c6baaSBenno Lossin /// # Safety
62fc6c6baaSBenno Lossin ///
63fc6c6baaSBenno Lossin /// Only the `init` module is allowed to use this trait.
64fc6c6baaSBenno Lossin pub unsafe trait PinData: Copy {
65fc6c6baaSBenno Lossin     type Datee: ?Sized + HasPinData;
66fc6c6baaSBenno Lossin 
67fc6c6baaSBenno Lossin     /// Type inference helper function.
make_closure<F, O, E>(self, f: F) -> F where F: FnOnce(*mut Self::Datee) -> Result<O, E>,68fc6c6baaSBenno Lossin     fn make_closure<F, O, E>(self, f: F) -> F
69fc6c6baaSBenno Lossin     where
70fc6c6baaSBenno Lossin         F: FnOnce(*mut Self::Datee) -> Result<O, E>,
71fc6c6baaSBenno Lossin     {
72fc6c6baaSBenno Lossin         f
73fc6c6baaSBenno Lossin     }
74fc6c6baaSBenno Lossin }
75fc6c6baaSBenno Lossin 
76fc6c6baaSBenno Lossin /// This trait is automatically implemented for every type. It aims to provide the same type
77fc6c6baaSBenno Lossin /// inference help as `HasPinData`.
78fc6c6baaSBenno Lossin ///
79fc6c6baaSBenno Lossin /// # Safety
80fc6c6baaSBenno Lossin ///
81fc6c6baaSBenno Lossin /// Only the `init` module is allowed to use this trait.
82fc6c6baaSBenno Lossin pub unsafe trait HasInitData {
83fc6c6baaSBenno Lossin     type InitData: InitData;
84fc6c6baaSBenno Lossin 
__init_data() -> Self::InitData85fc6c6baaSBenno Lossin     unsafe fn __init_data() -> Self::InitData;
86fc6c6baaSBenno Lossin }
87fc6c6baaSBenno Lossin 
88fc6c6baaSBenno Lossin /// Same function as `PinData`, but for arbitrary data.
89fc6c6baaSBenno Lossin ///
90fc6c6baaSBenno Lossin /// # Safety
91fc6c6baaSBenno Lossin ///
92fc6c6baaSBenno Lossin /// Only the `init` module is allowed to use this trait.
93fc6c6baaSBenno Lossin pub unsafe trait InitData: Copy {
94fc6c6baaSBenno Lossin     type Datee: ?Sized + HasInitData;
95fc6c6baaSBenno Lossin 
96fc6c6baaSBenno Lossin     /// Type inference helper function.
make_closure<F, O, E>(self, f: F) -> F where F: FnOnce(*mut Self::Datee) -> Result<O, E>,97fc6c6baaSBenno Lossin     fn make_closure<F, O, E>(self, f: F) -> F
98fc6c6baaSBenno Lossin     where
99fc6c6baaSBenno Lossin         F: FnOnce(*mut Self::Datee) -> Result<O, E>,
100fc6c6baaSBenno Lossin     {
101fc6c6baaSBenno Lossin         f
102fc6c6baaSBenno Lossin     }
103fc6c6baaSBenno Lossin }
104fc6c6baaSBenno Lossin 
105fc6c6baaSBenno Lossin pub struct AllData<T: ?Sized>(PhantomData<fn(Box<T>) -> Box<T>>);
106fc6c6baaSBenno Lossin 
107fc6c6baaSBenno Lossin impl<T: ?Sized> Clone for AllData<T> {
clone(&self) -> Self108fc6c6baaSBenno Lossin     fn clone(&self) -> Self {
109fc6c6baaSBenno Lossin         *self
110fc6c6baaSBenno Lossin     }
111fc6c6baaSBenno Lossin }
112fc6c6baaSBenno Lossin 
113fc6c6baaSBenno Lossin impl<T: ?Sized> Copy for AllData<T> {}
114fc6c6baaSBenno Lossin 
115fc6c6baaSBenno Lossin unsafe impl<T: ?Sized> InitData for AllData<T> {
116fc6c6baaSBenno Lossin     type Datee = T;
117fc6c6baaSBenno Lossin }
118fc6c6baaSBenno Lossin 
119fc6c6baaSBenno Lossin unsafe impl<T: ?Sized> HasInitData for T {
120fc6c6baaSBenno Lossin     type InitData = AllData<T>;
121fc6c6baaSBenno Lossin 
__init_data() -> Self::InitData122fc6c6baaSBenno Lossin     unsafe fn __init_data() -> Self::InitData {
123fc6c6baaSBenno Lossin         AllData(PhantomData)
124fc6c6baaSBenno Lossin     }
125fc6c6baaSBenno Lossin }
126fc6c6baaSBenno Lossin 
1276841d45aSBenno Lossin /// Stack initializer helper type. Use [`stack_pin_init`] instead of this primitive.
1286841d45aSBenno Lossin ///
1296841d45aSBenno Lossin /// # Invariants
1306841d45aSBenno Lossin ///
1316841d45aSBenno Lossin /// If `self.is_init` is true, then `self.value` is initialized.
1326841d45aSBenno Lossin ///
1336841d45aSBenno Lossin /// [`stack_pin_init`]: kernel::stack_pin_init
1346841d45aSBenno Lossin pub struct StackInit<T> {
1356841d45aSBenno Lossin     value: MaybeUninit<T>,
1366841d45aSBenno Lossin     is_init: bool,
1376841d45aSBenno Lossin }
1386841d45aSBenno Lossin 
1396841d45aSBenno Lossin impl<T> Drop for StackInit<T> {
1406841d45aSBenno Lossin     #[inline]
drop(&mut self)1416841d45aSBenno Lossin     fn drop(&mut self) {
1426841d45aSBenno Lossin         if self.is_init {
1436841d45aSBenno Lossin             // SAFETY: As we are being dropped, we only call this once. And since `self.is_init` is
1446841d45aSBenno Lossin             // true, `self.value` is initialized.
1456841d45aSBenno Lossin             unsafe { self.value.assume_init_drop() };
1466841d45aSBenno Lossin         }
1476841d45aSBenno Lossin     }
1486841d45aSBenno Lossin }
1496841d45aSBenno Lossin 
1506841d45aSBenno Lossin impl<T> StackInit<T> {
1516841d45aSBenno Lossin     /// Creates a new [`StackInit<T>`] that is uninitialized. Use [`stack_pin_init`] instead of this
1526841d45aSBenno Lossin     /// primitive.
1536841d45aSBenno Lossin     ///
1546841d45aSBenno Lossin     /// [`stack_pin_init`]: kernel::stack_pin_init
1556841d45aSBenno Lossin     #[inline]
uninit() -> Self1566841d45aSBenno Lossin     pub fn uninit() -> Self {
1576841d45aSBenno Lossin         Self {
1586841d45aSBenno Lossin             value: MaybeUninit::uninit(),
1596841d45aSBenno Lossin             is_init: false,
1606841d45aSBenno Lossin         }
1616841d45aSBenno Lossin     }
1626841d45aSBenno Lossin 
1636841d45aSBenno Lossin     /// Initializes the contents and returns the result.
1646841d45aSBenno Lossin     #[inline]
init<E>(self: Pin<&mut Self>, init: impl PinInit<T, E>) -> Result<Pin<&mut T>, E>1656841d45aSBenno Lossin     pub fn init<E>(self: Pin<&mut Self>, init: impl PinInit<T, E>) -> Result<Pin<&mut T>, E> {
1666841d45aSBenno Lossin         // SAFETY: We never move out of `this`.
1676841d45aSBenno Lossin         let this = unsafe { Pin::into_inner_unchecked(self) };
1686841d45aSBenno Lossin         // The value is currently initialized, so it needs to be dropped before we can reuse
1696841d45aSBenno Lossin         // the memory (this is a safety guarantee of `Pin`).
1706841d45aSBenno Lossin         if this.is_init {
1716841d45aSBenno Lossin             this.is_init = false;
1726841d45aSBenno Lossin             // SAFETY: `this.is_init` was true and therefore `this.value` is initialized.
1736841d45aSBenno Lossin             unsafe { this.value.assume_init_drop() };
1746841d45aSBenno Lossin         }
1756841d45aSBenno Lossin         // SAFETY: The memory slot is valid and this type ensures that it will stay pinned.
1766841d45aSBenno Lossin         unsafe { init.__pinned_init(this.value.as_mut_ptr())? };
1776841d45aSBenno Lossin         // INVARIANT: `this.value` is initialized above.
1786841d45aSBenno Lossin         this.is_init = true;
1796841d45aSBenno Lossin         // SAFETY: The slot is now pinned, since we will never give access to `&mut T`.
1806841d45aSBenno Lossin         Ok(unsafe { Pin::new_unchecked(this.value.assume_init_mut()) })
1816841d45aSBenno Lossin     }
1826841d45aSBenno Lossin }
1836841d45aSBenno Lossin 
184fc6c6baaSBenno Lossin /// When a value of this type is dropped, it drops a `T`.
185fc6c6baaSBenno Lossin ///
186fc6c6baaSBenno Lossin /// Can be forgotten to prevent the drop.
187fc6c6baaSBenno Lossin pub struct DropGuard<T: ?Sized> {
188fc6c6baaSBenno Lossin     ptr: *mut T,
189fc6c6baaSBenno Lossin }
190fc6c6baaSBenno Lossin 
191fc6c6baaSBenno Lossin impl<T: ?Sized> DropGuard<T> {
192fc6c6baaSBenno Lossin     /// Creates a new [`DropGuard<T>`]. It will [`ptr::drop_in_place`] `ptr` when it gets dropped.
193fc6c6baaSBenno Lossin     ///
194fc6c6baaSBenno Lossin     /// # Safety
195fc6c6baaSBenno Lossin     ///
196fc6c6baaSBenno Lossin     /// `ptr` must be a valid pointer.
197fc6c6baaSBenno Lossin     ///
198fc6c6baaSBenno Lossin     /// It is the callers responsibility that `self` will only get dropped if the pointee of `ptr`:
199fc6c6baaSBenno Lossin     /// - has not been dropped,
200fc6c6baaSBenno Lossin     /// - is not accessible by any other means,
201fc6c6baaSBenno Lossin     /// - will not be dropped by any other means.
202fc6c6baaSBenno Lossin     #[inline]
new(ptr: *mut T) -> Self203fc6c6baaSBenno Lossin     pub unsafe fn new(ptr: *mut T) -> Self {
20497de919dSBenno Lossin         Self { ptr }
205fc6c6baaSBenno Lossin     }
206fc6c6baaSBenno Lossin }
207fc6c6baaSBenno Lossin 
208fc6c6baaSBenno Lossin impl<T: ?Sized> Drop for DropGuard<T> {
209fc6c6baaSBenno Lossin     #[inline]
drop(&mut self)210fc6c6baaSBenno Lossin     fn drop(&mut self) {
211fc6c6baaSBenno Lossin         // SAFETY: A `DropGuard` can only be constructed using the unsafe `new` function
212fc6c6baaSBenno Lossin         // ensuring that this operation is safe.
213fc6c6baaSBenno Lossin         unsafe { ptr::drop_in_place(self.ptr) }
214fc6c6baaSBenno Lossin     }
215fc6c6baaSBenno Lossin }
216d0fdc396SBenno Lossin 
217d0fdc396SBenno Lossin /// Token used by `PinnedDrop` to prevent calling the function without creating this unsafely
218d0fdc396SBenno Lossin /// created struct. This is needed, because the `drop` function is safe, but should not be called
219d0fdc396SBenno Lossin /// manually.
220d0fdc396SBenno Lossin pub struct OnlyCallFromDrop(());
221d0fdc396SBenno Lossin 
222d0fdc396SBenno Lossin impl OnlyCallFromDrop {
223d0fdc396SBenno Lossin     /// # Safety
224d0fdc396SBenno Lossin     ///
225d0fdc396SBenno Lossin     /// This function should only be called from the [`Drop::drop`] function and only be used to
226d0fdc396SBenno Lossin     /// delegate the destruction to the pinned destructor [`PinnedDrop::drop`] of the same type.
new() -> Self227d0fdc396SBenno Lossin     pub unsafe fn new() -> Self {
228d0fdc396SBenno Lossin         Self(())
229d0fdc396SBenno Lossin     }
230d0fdc396SBenno Lossin }
231