~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/rust/kernel/init/__internal.rs

Version: ~ [ linux-6.12-rc7 ] ~ [ linux-6.11.7 ] ~ [ linux-6.10.14 ] ~ [ linux-6.9.12 ] ~ [ linux-6.8.12 ] ~ [ linux-6.7.12 ] ~ [ linux-6.6.60 ] ~ [ linux-6.5.13 ] ~ [ linux-6.4.16 ] ~ [ linux-6.3.13 ] ~ [ linux-6.2.16 ] ~ [ linux-6.1.116 ] ~ [ linux-6.0.19 ] ~ [ linux-5.19.17 ] ~ [ linux-5.18.19 ] ~ [ linux-5.17.15 ] ~ [ linux-5.16.20 ] ~ [ linux-5.15.171 ] ~ [ linux-5.14.21 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.229 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.285 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.323 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.336 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.337 ] ~ [ linux-4.4.302 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.12 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

Diff markup

Differences between /rust/kernel/init/__internal.rs (Version linux-6.12-rc7) and /rust/kernel/init/__internal.rs (Version linux-6.7.12)


  1 // SPDX-License-Identifier: Apache-2.0 OR MIT       1 // SPDX-License-Identifier: Apache-2.0 OR MIT
  2                                                     2 
  3 //! This module contains API-internal items fo      3 //! This module contains API-internal items for pin-init.
  4 //!                                                 4 //!
  5 //! These items must not be used outside of         5 //! These items must not be used outside of
  6 //! - `kernel/init.rs`                              6 //! - `kernel/init.rs`
  7 //! - `macros/pin_data.rs`                          7 //! - `macros/pin_data.rs`
  8 //! - `macros/pinned_drop.rs`                       8 //! - `macros/pinned_drop.rs`
  9                                                     9 
 10 use super::*;                                      10 use super::*;
 11                                                    11 
 12 /// See the [nomicon] for what subtyping is. S     12 /// See the [nomicon] for what subtyping is. See also [this table].
 13 ///                                                13 ///
 14 /// [nomicon]: https://doc.rust-lang.org/nomic     14 /// [nomicon]: https://doc.rust-lang.org/nomicon/subtyping.html
 15 /// [this table]: https://doc.rust-lang.org/no     15 /// [this table]: https://doc.rust-lang.org/nomicon/phantom-data.html#table-of-phantomdata-patterns
 16 pub(super) type Invariant<T> = PhantomData<fn(     16 pub(super) type Invariant<T> = PhantomData<fn(*mut T) -> *mut T>;
 17                                                    17 
 18 /// This is the module-internal type implement     18 /// This is the module-internal type implementing `PinInit` and `Init`. It is unsafe to create this
 19 /// type, since the closure needs to fulfill t     19 /// type, since the closure needs to fulfill the same safety requirement as the
 20 /// `__pinned_init`/`__init` functions.            20 /// `__pinned_init`/`__init` functions.
 21 pub(crate) struct InitClosure<F, T: ?Sized, E>     21 pub(crate) struct InitClosure<F, T: ?Sized, E>(pub(crate) F, pub(crate) Invariant<(E, T)>);
 22                                                    22 
 23 // SAFETY: While constructing the `InitClosure     23 // SAFETY: While constructing the `InitClosure`, the user promised that it upholds the
 24 // `__init` invariants.                            24 // `__init` invariants.
 25 unsafe impl<T: ?Sized, F, E> Init<T, E> for In     25 unsafe impl<T: ?Sized, F, E> Init<T, E> for InitClosure<F, T, E>
 26 where                                              26 where
 27     F: FnOnce(*mut T) -> Result<(), E>,            27     F: FnOnce(*mut T) -> Result<(), E>,
 28 {                                                  28 {
 29     #[inline]                                      29     #[inline]
 30     unsafe fn __init(self, slot: *mut T) -> Re     30     unsafe fn __init(self, slot: *mut T) -> Result<(), E> {
 31         (self.0)(slot)                             31         (self.0)(slot)
 32     }                                              32     }
 33 }                                                  33 }
 34                                                    34 
 35 // SAFETY: While constructing the `InitClosure     35 // SAFETY: While constructing the `InitClosure`, the user promised that it upholds the
 36 // `__pinned_init` invariants.                     36 // `__pinned_init` invariants.
 37 unsafe impl<T: ?Sized, F, E> PinInit<T, E> for     37 unsafe impl<T: ?Sized, F, E> PinInit<T, E> for InitClosure<F, T, E>
 38 where                                              38 where
 39     F: FnOnce(*mut T) -> Result<(), E>,            39     F: FnOnce(*mut T) -> Result<(), E>,
 40 {                                                  40 {
 41     #[inline]                                      41     #[inline]
 42     unsafe fn __pinned_init(self, slot: *mut T     42     unsafe fn __pinned_init(self, slot: *mut T) -> Result<(), E> {
 43         (self.0)(slot)                             43         (self.0)(slot)
 44     }                                              44     }
 45 }                                                  45 }
 46                                                    46 
 47 /// This trait is only implemented via the `#[     47 /// This trait is only implemented via the `#[pin_data]` proc-macro. It is used to facilitate
 48 /// the pin projections within the initializer     48 /// the pin projections within the initializers.
 49 ///                                                49 ///
 50 /// # Safety                                       50 /// # Safety
 51 ///                                                51 ///
 52 /// Only the `init` module is allowed to use t     52 /// Only the `init` module is allowed to use this trait.
 53 pub unsafe trait HasPinData {                      53 pub unsafe trait HasPinData {
 54     type PinData: PinData;                         54     type PinData: PinData;
 55                                                    55 
 56     unsafe fn __pin_data() -> Self::PinData;       56     unsafe fn __pin_data() -> Self::PinData;
 57 }                                                  57 }
 58                                                    58 
 59 /// Marker trait for pinning data of structs.      59 /// Marker trait for pinning data of structs.
 60 ///                                                60 ///
 61 /// # Safety                                       61 /// # Safety
 62 ///                                                62 ///
 63 /// Only the `init` module is allowed to use t     63 /// Only the `init` module is allowed to use this trait.
 64 pub unsafe trait PinData: Copy {                   64 pub unsafe trait PinData: Copy {
 65     type Datee: ?Sized + HasPinData;               65     type Datee: ?Sized + HasPinData;
 66                                                    66 
 67     /// Type inference helper function.            67     /// Type inference helper function.
 68     fn make_closure<F, O, E>(self, f: F) -> F      68     fn make_closure<F, O, E>(self, f: F) -> F
 69     where                                          69     where
 70         F: FnOnce(*mut Self::Datee) -> Result<     70         F: FnOnce(*mut Self::Datee) -> Result<O, E>,
 71     {                                              71     {
 72         f                                          72         f
 73     }                                              73     }
 74 }                                                  74 }
 75                                                    75 
 76 /// This trait is automatically implemented fo     76 /// This trait is automatically implemented for every type. It aims to provide the same type
 77 /// inference help as `HasPinData`.                77 /// inference help as `HasPinData`.
 78 ///                                                78 ///
 79 /// # Safety                                       79 /// # Safety
 80 ///                                                80 ///
 81 /// Only the `init` module is allowed to use t     81 /// Only the `init` module is allowed to use this trait.
 82 pub unsafe trait HasInitData {                     82 pub unsafe trait HasInitData {
 83     type InitData: InitData;                       83     type InitData: InitData;
 84                                                    84 
 85     unsafe fn __init_data() -> Self::InitData;     85     unsafe fn __init_data() -> Self::InitData;
 86 }                                                  86 }
 87                                                    87 
 88 /// Same function as `PinData`, but for arbitr     88 /// Same function as `PinData`, but for arbitrary data.
 89 ///                                                89 ///
 90 /// # Safety                                       90 /// # Safety
 91 ///                                                91 ///
 92 /// Only the `init` module is allowed to use t     92 /// Only the `init` module is allowed to use this trait.
 93 pub unsafe trait InitData: Copy {                  93 pub unsafe trait InitData: Copy {
 94     type Datee: ?Sized + HasInitData;              94     type Datee: ?Sized + HasInitData;
 95                                                    95 
 96     /// Type inference helper function.            96     /// Type inference helper function.
 97     fn make_closure<F, O, E>(self, f: F) -> F      97     fn make_closure<F, O, E>(self, f: F) -> F
 98     where                                          98     where
 99         F: FnOnce(*mut Self::Datee) -> Result<     99         F: FnOnce(*mut Self::Datee) -> Result<O, E>,
100     {                                             100     {
101         f                                         101         f
102     }                                             102     }
103 }                                                 103 }
104                                                   104 
105 pub struct AllData<T: ?Sized>(PhantomData<fn(B    105 pub struct AllData<T: ?Sized>(PhantomData<fn(Box<T>) -> Box<T>>);
106                                                   106 
107 impl<T: ?Sized> Clone for AllData<T> {            107 impl<T: ?Sized> Clone for AllData<T> {
108     fn clone(&self) -> Self {                     108     fn clone(&self) -> Self {
109         *self                                     109         *self
110     }                                             110     }
111 }                                                 111 }
112                                                   112 
113 impl<T: ?Sized> Copy for AllData<T> {}            113 impl<T: ?Sized> Copy for AllData<T> {}
114                                                   114 
115 unsafe impl<T: ?Sized> InitData for AllData<T>    115 unsafe impl<T: ?Sized> InitData for AllData<T> {
116     type Datee = T;                               116     type Datee = T;
117 }                                                 117 }
118                                                   118 
119 unsafe impl<T: ?Sized> HasInitData for T {        119 unsafe impl<T: ?Sized> HasInitData for T {
120     type InitData = AllData<T>;                   120     type InitData = AllData<T>;
121                                                   121 
122     unsafe fn __init_data() -> Self::InitData     122     unsafe fn __init_data() -> Self::InitData {
123         AllData(PhantomData)                      123         AllData(PhantomData)
124     }                                             124     }
125 }                                                 125 }
126                                                   126 
127 /// Stack initializer helper type. Use [`stack    127 /// Stack initializer helper type. Use [`stack_pin_init`] instead of this primitive.
128 ///                                               128 ///
129 /// # Invariants                                  129 /// # Invariants
130 ///                                               130 ///
131 /// If `self.is_init` is true, then `self.valu    131 /// If `self.is_init` is true, then `self.value` is initialized.
132 ///                                               132 ///
133 /// [`stack_pin_init`]: kernel::stack_pin_init    133 /// [`stack_pin_init`]: kernel::stack_pin_init
134 pub struct StackInit<T> {                         134 pub struct StackInit<T> {
135     value: MaybeUninit<T>,                        135     value: MaybeUninit<T>,
136     is_init: bool,                                136     is_init: bool,
137 }                                                 137 }
138                                                   138 
139 impl<T> Drop for StackInit<T> {                   139 impl<T> Drop for StackInit<T> {
140     #[inline]                                     140     #[inline]
141     fn drop(&mut self) {                          141     fn drop(&mut self) {
142         if self.is_init {                         142         if self.is_init {
143             // SAFETY: As we are being dropped    143             // SAFETY: As we are being dropped, we only call this once. And since `self.is_init` is
144             // true, `self.value` is initializ    144             // true, `self.value` is initialized.
145             unsafe { self.value.assume_init_dr    145             unsafe { self.value.assume_init_drop() };
146         }                                         146         }
147     }                                             147     }
148 }                                                 148 }
149                                                   149 
150 impl<T> StackInit<T> {                            150 impl<T> StackInit<T> {
151     /// Creates a new [`StackInit<T>`] that is    151     /// Creates a new [`StackInit<T>`] that is uninitialized. Use [`stack_pin_init`] instead of this
152     /// primitive.                                152     /// primitive.
153     ///                                           153     ///
154     /// [`stack_pin_init`]: kernel::stack_pin_    154     /// [`stack_pin_init`]: kernel::stack_pin_init
155     #[inline]                                     155     #[inline]
156     pub fn uninit() -> Self {                     156     pub fn uninit() -> Self {
157         Self {                                    157         Self {
158             value: MaybeUninit::uninit(),         158             value: MaybeUninit::uninit(),
159             is_init: false,                       159             is_init: false,
160         }                                         160         }
161     }                                             161     }
162                                                   162 
163     /// Initializes the contents and returns t    163     /// Initializes the contents and returns the result.
164     #[inline]                                     164     #[inline]
165     pub fn init<E>(self: Pin<&mut Self>, init:    165     pub fn init<E>(self: Pin<&mut Self>, init: impl PinInit<T, E>) -> Result<Pin<&mut T>, E> {
166         // SAFETY: We never move out of `this`    166         // SAFETY: We never move out of `this`.
167         let this = unsafe { Pin::into_inner_un    167         let this = unsafe { Pin::into_inner_unchecked(self) };
168         // The value is currently initialized,    168         // The value is currently initialized, so it needs to be dropped before we can reuse
169         // the memory (this is a safety guaran    169         // the memory (this is a safety guarantee of `Pin`).
170         if this.is_init {                         170         if this.is_init {
171             this.is_init = false;                 171             this.is_init = false;
172             // SAFETY: `this.is_init` was true    172             // SAFETY: `this.is_init` was true and therefore `this.value` is initialized.
173             unsafe { this.value.assume_init_dr    173             unsafe { this.value.assume_init_drop() };
174         }                                         174         }
175         // SAFETY: The memory slot is valid an    175         // SAFETY: The memory slot is valid and this type ensures that it will stay pinned.
176         unsafe { init.__pinned_init(this.value    176         unsafe { init.__pinned_init(this.value.as_mut_ptr())? };
177         // INVARIANT: `this.value` is initiali    177         // INVARIANT: `this.value` is initialized above.
178         this.is_init = true;                      178         this.is_init = true;
179         // SAFETY: The slot is now pinned, sin    179         // SAFETY: The slot is now pinned, since we will never give access to `&mut T`.
180         Ok(unsafe { Pin::new_unchecked(this.va    180         Ok(unsafe { Pin::new_unchecked(this.value.assume_init_mut()) })
181     }                                             181     }
182 }                                                 182 }
183                                                   183 
184 /// When a value of this type is dropped, it d    184 /// When a value of this type is dropped, it drops a `T`.
185 ///                                               185 ///
186 /// Can be forgotten to prevent the drop.         186 /// Can be forgotten to prevent the drop.
187 pub struct DropGuard<T: ?Sized> {                 187 pub struct DropGuard<T: ?Sized> {
188     ptr: *mut T,                                  188     ptr: *mut T,
189 }                                                 189 }
190                                                   190 
191 impl<T: ?Sized> DropGuard<T> {                    191 impl<T: ?Sized> DropGuard<T> {
192     /// Creates a new [`DropGuard<T>`]. It wil    192     /// Creates a new [`DropGuard<T>`]. It will [`ptr::drop_in_place`] `ptr` when it gets dropped.
193     ///                                           193     ///
194     /// # Safety                                  194     /// # Safety
195     ///                                           195     ///
196     /// `ptr` must be a valid pointer.            196     /// `ptr` must be a valid pointer.
197     ///                                           197     ///
198     /// It is the callers responsibility that     198     /// It is the callers responsibility that `self` will only get dropped if the pointee of `ptr`:
199     /// - has not been dropped,                   199     /// - has not been dropped,
200     /// - is not accessible by any other means    200     /// - is not accessible by any other means,
201     /// - will not be dropped by any other mea    201     /// - will not be dropped by any other means.
202     #[inline]                                     202     #[inline]
203     pub unsafe fn new(ptr: *mut T) -> Self {      203     pub unsafe fn new(ptr: *mut T) -> Self {
204         Self { ptr }                              204         Self { ptr }
205     }                                             205     }
206 }                                                 206 }
207                                                   207 
208 impl<T: ?Sized> Drop for DropGuard<T> {           208 impl<T: ?Sized> Drop for DropGuard<T> {
209     #[inline]                                     209     #[inline]
210     fn drop(&mut self) {                          210     fn drop(&mut self) {
211         // SAFETY: A `DropGuard` can only be c    211         // SAFETY: A `DropGuard` can only be constructed using the unsafe `new` function
212         // ensuring that this operation is saf    212         // ensuring that this operation is safe.
213         unsafe { ptr::drop_in_place(self.ptr)     213         unsafe { ptr::drop_in_place(self.ptr) }
214     }                                             214     }
215 }                                                 215 }
216                                                   216 
217 /// Token used by `PinnedDrop` to prevent call    217 /// Token used by `PinnedDrop` to prevent calling the function without creating this unsafely
218 /// created struct. This is needed, because th    218 /// created struct. This is needed, because the `drop` function is safe, but should not be called
219 /// manually.                                     219 /// manually.
220 pub struct OnlyCallFromDrop(());                  220 pub struct OnlyCallFromDrop(());
221                                                   221 
222 impl OnlyCallFromDrop {                           222 impl OnlyCallFromDrop {
223     /// # Safety                                  223     /// # Safety
224     ///                                           224     ///
225     /// This function should only be called fr    225     /// This function should only be called from the [`Drop::drop`] function and only be used to
226     /// delegate the destruction to the pinned    226     /// delegate the destruction to the pinned destructor [`PinnedDrop::drop`] of the same type.
227     pub unsafe fn new() -> Self {                 227     pub unsafe fn new() -> Self {
228         Self(())                                  228         Self(())
229     }                                             229     }
230 }                                                 230 }
231                                                << 
232 /// Initializer that always fails.             << 
233 ///                                            << 
234 /// Used by [`assert_pinned!`].                << 
235 ///                                            << 
236 /// [`assert_pinned!`]: crate::assert_pinned   << 
237 pub struct AlwaysFail<T: ?Sized> {             << 
238     _t: PhantomData<T>,                        << 
239 }                                              << 
240                                                << 
241 impl<T: ?Sized> AlwaysFail<T> {                << 
242     /// Creates a new initializer that always  << 
243     pub fn new() -> Self {                     << 
244         Self { _t: PhantomData }               << 
245     }                                          << 
246 }                                              << 
247                                                << 
248 impl<T: ?Sized> Default for AlwaysFail<T> {    << 
249     fn default() -> Self {                     << 
250         Self::new()                            << 
251     }                                          << 
252 }                                              << 
253                                                << 
254 // SAFETY: `__pinned_init` always fails, which << 
255 unsafe impl<T: ?Sized> PinInit<T, ()> for Alwa << 
256     unsafe fn __pinned_init(self, _slot: *mut  << 
257         Err(())                                << 
258     }                                          << 
259 }                                              << 
                                                      

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

sflogo.php