xref: /linux/rust/kernel/alloc/kbox.rs (revision bb9749f32ad38cf343e6650a34125be6aacd65d1)
1 // SPDX-License-Identifier: GPL-2.0
2 
3 //! Implementation of [`Box`].
4 
5 #[allow(unused_imports)] // Used in doc comments.
6 use super::allocator::{KVmalloc, Kmalloc, Vmalloc};
7 use super::{AllocError, Allocator, Flags};
8 use core::alloc::Layout;
9 use core::borrow::{Borrow, BorrowMut};
10 use core::fmt;
11 use core::marker::PhantomData;
12 use core::mem::ManuallyDrop;
13 use core::mem::MaybeUninit;
14 use core::ops::{Deref, DerefMut};
15 use core::pin::Pin;
16 use core::ptr::NonNull;
17 use core::result::Result;
18 
19 use crate::ffi::c_void;
20 use crate::init::InPlaceInit;
21 use crate::types::ForeignOwnable;
22 use pin_init::{InPlaceWrite, Init, PinInit, ZeroableOption};
23 
24 /// The kernel's [`Box`] type -- a heap allocation for a single value of type `T`.
25 ///
26 /// This is the kernel's version of the Rust stdlib's `Box`. There are several differences,
27 /// for example no `noalias` attribute is emitted and partially moving out of a `Box` is not
28 /// supported. There are also several API differences, e.g. `Box` always requires an [`Allocator`]
29 /// implementation to be passed as generic, page [`Flags`] when allocating memory and all functions
30 /// that may allocate memory are fallible.
31 ///
32 /// `Box` works with any of the kernel's allocators, e.g. [`Kmalloc`], [`Vmalloc`] or [`KVmalloc`].
33 /// There are aliases for `Box` with these allocators ([`KBox`], [`VBox`], [`KVBox`]).
34 ///
35 /// When dropping a [`Box`], the value is also dropped and the heap memory is automatically freed.
36 ///
37 /// # Examples
38 ///
39 /// ```
40 /// let b = KBox::<u64>::new(24_u64, GFP_KERNEL)?;
41 ///
42 /// assert_eq!(*b, 24_u64);
43 /// # Ok::<(), Error>(())
44 /// ```
45 ///
46 /// ```
47 /// # use kernel::bindings;
48 /// const SIZE: usize = bindings::KMALLOC_MAX_SIZE as usize + 1;
49 /// struct Huge([u8; SIZE]);
50 ///
51 /// assert!(KBox::<Huge>::new_uninit(GFP_KERNEL | __GFP_NOWARN).is_err());
52 /// ```
53 ///
54 /// ```
55 /// # use kernel::bindings;
56 /// const SIZE: usize = bindings::KMALLOC_MAX_SIZE as usize + 1;
57 /// struct Huge([u8; SIZE]);
58 ///
59 /// assert!(KVBox::<Huge>::new_uninit(GFP_KERNEL).is_ok());
60 /// ```
61 ///
62 /// [`Box`]es can also be used to store trait objects by coercing their type:
63 ///
64 /// ```
65 /// trait FooTrait {}
66 ///
67 /// struct FooStruct;
68 /// impl FooTrait for FooStruct {}
69 ///
70 /// let _ = KBox::new(FooStruct, GFP_KERNEL)? as KBox<dyn FooTrait>;
71 /// # Ok::<(), Error>(())
72 /// ```
73 ///
74 /// # Invariants
75 ///
76 /// `self.0` is always properly aligned and either points to memory allocated with `A` or, for
77 /// zero-sized types, is a dangling, well aligned pointer.
78 #[repr(transparent)]
79 #[cfg_attr(CONFIG_RUSTC_HAS_COERCE_POINTEE, derive(core::marker::CoercePointee))]
80 pub struct Box<#[cfg_attr(CONFIG_RUSTC_HAS_COERCE_POINTEE, pointee)] T: ?Sized, A: Allocator>(
81     NonNull<T>,
82     PhantomData<A>,
83 );
84 
85 // This is to allow coercion from `Box<T, A>` to `Box<U, A>` if `T` can be converted to the
86 // dynamically-sized type (DST) `U`.
87 #[cfg(not(CONFIG_RUSTC_HAS_COERCE_POINTEE))]
88 impl<T, U, A> core::ops::CoerceUnsized<Box<U, A>> for Box<T, A>
89 where
90     T: ?Sized + core::marker::Unsize<U>,
91     U: ?Sized,
92     A: Allocator,
93 {
94 }
95 
96 // This is to allow `Box<U, A>` to be dispatched on when `Box<T, A>` can be coerced into `Box<U,
97 // A>`.
98 #[cfg(not(CONFIG_RUSTC_HAS_COERCE_POINTEE))]
99 impl<T, U, A> core::ops::DispatchFromDyn<Box<U, A>> for Box<T, A>
100 where
101     T: ?Sized + core::marker::Unsize<U>,
102     U: ?Sized,
103     A: Allocator,
104 {
105 }
106 
107 /// Type alias for [`Box`] with a [`Kmalloc`] allocator.
108 ///
109 /// # Examples
110 ///
111 /// ```
112 /// let b = KBox::new(24_u64, GFP_KERNEL)?;
113 ///
114 /// assert_eq!(*b, 24_u64);
115 /// # Ok::<(), Error>(())
116 /// ```
117 pub type KBox<T> = Box<T, super::allocator::Kmalloc>;
118 
119 /// Type alias for [`Box`] with a [`Vmalloc`] allocator.
120 ///
121 /// # Examples
122 ///
123 /// ```
124 /// let b = VBox::new(24_u64, GFP_KERNEL)?;
125 ///
126 /// assert_eq!(*b, 24_u64);
127 /// # Ok::<(), Error>(())
128 /// ```
129 pub type VBox<T> = Box<T, super::allocator::Vmalloc>;
130 
131 /// Type alias for [`Box`] with a [`KVmalloc`] allocator.
132 ///
133 /// # Examples
134 ///
135 /// ```
136 /// let b = KVBox::new(24_u64, GFP_KERNEL)?;
137 ///
138 /// assert_eq!(*b, 24_u64);
139 /// # Ok::<(), Error>(())
140 /// ```
141 pub type KVBox<T> = Box<T, super::allocator::KVmalloc>;
142 
143 // SAFETY: All zeros is equivalent to `None` (option layout optimization guarantee:
144 // <https://doc.rust-lang.org/stable/std/option/index.html#representation>).
145 unsafe impl<T, A: Allocator> ZeroableOption for Box<T, A> {}
146 
147 // SAFETY: `Box` is `Send` if `T` is `Send` because the `Box` owns a `T`.
148 unsafe impl<T, A> Send for Box<T, A>
149 where
150     T: Send + ?Sized,
151     A: Allocator,
152 {
153 }
154 
155 // SAFETY: `Box` is `Sync` if `T` is `Sync` because the `Box` owns a `T`.
156 unsafe impl<T, A> Sync for Box<T, A>
157 where
158     T: Sync + ?Sized,
159     A: Allocator,
160 {
161 }
162 
163 impl<T, A> Box<T, A>
164 where
165     T: ?Sized,
166     A: Allocator,
167 {
168     /// Creates a new `Box<T, A>` from a raw pointer.
169     ///
170     /// # Safety
171     ///
172     /// For non-ZSTs, `raw` must point at an allocation allocated with `A` that is sufficiently
173     /// aligned for and holds a valid `T`. The caller passes ownership of the allocation to the
174     /// `Box`.
175     ///
176     /// For ZSTs, `raw` must be a dangling, well aligned pointer.
177     #[inline]
178     pub const unsafe fn from_raw(raw: *mut T) -> Self {
179         // INVARIANT: Validity of `raw` is guaranteed by the safety preconditions of this function.
180         // SAFETY: By the safety preconditions of this function, `raw` is not a NULL pointer.
181         Self(unsafe { NonNull::new_unchecked(raw) }, PhantomData)
182     }
183 
184     /// Consumes the `Box<T, A>` and returns a raw pointer.
185     ///
186     /// This will not run the destructor of `T` and for non-ZSTs the allocation will stay alive
187     /// indefinitely. Use [`Box::from_raw`] to recover the [`Box`], drop the value and free the
188     /// allocation, if any.
189     ///
190     /// # Examples
191     ///
192     /// ```
193     /// let x = KBox::new(24, GFP_KERNEL)?;
194     /// let ptr = KBox::into_raw(x);
195     /// // SAFETY: `ptr` comes from a previous call to `KBox::into_raw`.
196     /// let x = unsafe { KBox::from_raw(ptr) };
197     ///
198     /// assert_eq!(*x, 24);
199     /// # Ok::<(), Error>(())
200     /// ```
201     #[inline]
202     pub fn into_raw(b: Self) -> *mut T {
203         ManuallyDrop::new(b).0.as_ptr()
204     }
205 
206     /// Consumes and leaks the `Box<T, A>` and returns a mutable reference.
207     ///
208     /// See [`Box::into_raw`] for more details.
209     #[inline]
210     pub fn leak<'a>(b: Self) -> &'a mut T {
211         // SAFETY: `Box::into_raw` always returns a properly aligned and dereferenceable pointer
212         // which points to an initialized instance of `T`.
213         unsafe { &mut *Box::into_raw(b) }
214     }
215 }
216 
217 impl<T, A> Box<MaybeUninit<T>, A>
218 where
219     A: Allocator,
220 {
221     /// Converts a `Box<MaybeUninit<T>, A>` to a `Box<T, A>`.
222     ///
223     /// It is undefined behavior to call this function while the value inside of `b` is not yet
224     /// fully initialized.
225     ///
226     /// # Safety
227     ///
228     /// Callers must ensure that the value inside of `b` is in an initialized state.
229     pub unsafe fn assume_init(self) -> Box<T, A> {
230         let raw = Self::into_raw(self);
231 
232         // SAFETY: `raw` comes from a previous call to `Box::into_raw`. By the safety requirements
233         // of this function, the value inside the `Box` is in an initialized state. Hence, it is
234         // safe to reconstruct the `Box` as `Box<T, A>`.
235         unsafe { Box::from_raw(raw.cast()) }
236     }
237 
238     /// Writes the value and converts to `Box<T, A>`.
239     pub fn write(mut self, value: T) -> Box<T, A> {
240         (*self).write(value);
241 
242         // SAFETY: We've just initialized `b`'s value.
243         unsafe { self.assume_init() }
244     }
245 }
246 
247 impl<T, A> Box<T, A>
248 where
249     A: Allocator,
250 {
251     /// Creates a new `Box<T, A>` and initializes its contents with `x`.
252     ///
253     /// New memory is allocated with `A`. The allocation may fail, in which case an error is
254     /// returned. For ZSTs no memory is allocated.
255     pub fn new(x: T, flags: Flags) -> Result<Self, AllocError> {
256         let b = Self::new_uninit(flags)?;
257         Ok(Box::write(b, x))
258     }
259 
260     /// Creates a new `Box<T, A>` with uninitialized contents.
261     ///
262     /// New memory is allocated with `A`. The allocation may fail, in which case an error is
263     /// returned. For ZSTs no memory is allocated.
264     ///
265     /// # Examples
266     ///
267     /// ```
268     /// let b = KBox::<u64>::new_uninit(GFP_KERNEL)?;
269     /// let b = KBox::write(b, 24);
270     ///
271     /// assert_eq!(*b, 24_u64);
272     /// # Ok::<(), Error>(())
273     /// ```
274     pub fn new_uninit(flags: Flags) -> Result<Box<MaybeUninit<T>, A>, AllocError> {
275         let layout = Layout::new::<MaybeUninit<T>>();
276         let ptr = A::alloc(layout, flags)?;
277 
278         // INVARIANT: `ptr` is either a dangling pointer or points to memory allocated with `A`,
279         // which is sufficient in size and alignment for storing a `T`.
280         Ok(Box(ptr.cast(), PhantomData))
281     }
282 
283     /// Constructs a new `Pin<Box<T, A>>`. If `T` does not implement [`Unpin`], then `x` will be
284     /// pinned in memory and can't be moved.
285     #[inline]
286     pub fn pin(x: T, flags: Flags) -> Result<Pin<Box<T, A>>, AllocError>
287     where
288         A: 'static,
289     {
290         Ok(Self::new(x, flags)?.into())
291     }
292 
293     /// Convert a [`Box<T,A>`] to a [`Pin<Box<T,A>>`]. If `T` does not implement
294     /// [`Unpin`], then `x` will be pinned in memory and can't be moved.
295     pub fn into_pin(this: Self) -> Pin<Self> {
296         this.into()
297     }
298 
299     /// Forgets the contents (does not run the destructor), but keeps the allocation.
300     fn forget_contents(this: Self) -> Box<MaybeUninit<T>, A> {
301         let ptr = Self::into_raw(this);
302 
303         // SAFETY: `ptr` is valid, because it came from `Box::into_raw`.
304         unsafe { Box::from_raw(ptr.cast()) }
305     }
306 
307     /// Drops the contents, but keeps the allocation.
308     ///
309     /// # Examples
310     ///
311     /// ```
312     /// let value = KBox::new([0; 32], GFP_KERNEL)?;
313     /// assert_eq!(*value, [0; 32]);
314     /// let value = KBox::drop_contents(value);
315     /// // Now we can re-use `value`:
316     /// let value = KBox::write(value, [1; 32]);
317     /// assert_eq!(*value, [1; 32]);
318     /// # Ok::<(), Error>(())
319     /// ```
320     pub fn drop_contents(this: Self) -> Box<MaybeUninit<T>, A> {
321         let ptr = this.0.as_ptr();
322 
323         // SAFETY: `ptr` is valid, because it came from `this`. After this call we never access the
324         // value stored in `this` again.
325         unsafe { core::ptr::drop_in_place(ptr) };
326 
327         Self::forget_contents(this)
328     }
329 
330     /// Moves the `Box`'s value out of the `Box` and consumes the `Box`.
331     pub fn into_inner(b: Self) -> T {
332         // SAFETY: By the type invariant `&*b` is valid for `read`.
333         let value = unsafe { core::ptr::read(&*b) };
334         let _ = Self::forget_contents(b);
335         value
336     }
337 }
338 
339 impl<T, A> From<Box<T, A>> for Pin<Box<T, A>>
340 where
341     T: ?Sized,
342     A: Allocator,
343 {
344     /// Converts a `Box<T, A>` into a `Pin<Box<T, A>>`. If `T` does not implement [`Unpin`], then
345     /// `*b` will be pinned in memory and can't be moved.
346     ///
347     /// This moves `b` into `Pin` without moving `*b` or allocating and copying any memory.
348     fn from(b: Box<T, A>) -> Self {
349         // SAFETY: The value wrapped inside a `Pin<Box<T, A>>` cannot be moved or replaced as long
350         // as `T` does not implement `Unpin`.
351         unsafe { Pin::new_unchecked(b) }
352     }
353 }
354 
355 impl<T, A> InPlaceWrite<T> for Box<MaybeUninit<T>, A>
356 where
357     A: Allocator + 'static,
358 {
359     type Initialized = Box<T, A>;
360 
361     fn write_init<E>(mut self, init: impl Init<T, E>) -> Result<Self::Initialized, E> {
362         let slot = self.as_mut_ptr();
363         // SAFETY: When init errors/panics, slot will get deallocated but not dropped,
364         // slot is valid.
365         unsafe { init.__init(slot)? };
366         // SAFETY: All fields have been initialized.
367         Ok(unsafe { Box::assume_init(self) })
368     }
369 
370     fn write_pin_init<E>(mut self, init: impl PinInit<T, E>) -> Result<Pin<Self::Initialized>, E> {
371         let slot = self.as_mut_ptr();
372         // SAFETY: When init errors/panics, slot will get deallocated but not dropped,
373         // slot is valid and will not be moved, because we pin it later.
374         unsafe { init.__pinned_init(slot)? };
375         // SAFETY: All fields have been initialized.
376         Ok(unsafe { Box::assume_init(self) }.into())
377     }
378 }
379 
380 impl<T, A> InPlaceInit<T> for Box<T, A>
381 where
382     A: Allocator + 'static,
383 {
384     type PinnedSelf = Pin<Self>;
385 
386     #[inline]
387     fn try_pin_init<E>(init: impl PinInit<T, E>, flags: Flags) -> Result<Pin<Self>, E>
388     where
389         E: From<AllocError>,
390     {
391         Box::<_, A>::new_uninit(flags)?.write_pin_init(init)
392     }
393 
394     #[inline]
395     fn try_init<E>(init: impl Init<T, E>, flags: Flags) -> Result<Self, E>
396     where
397         E: From<AllocError>,
398     {
399         Box::<_, A>::new_uninit(flags)?.write_init(init)
400     }
401 }
402 
403 // SAFETY: The pointer returned by `into_foreign` comes from a well aligned
404 // pointer to `T` allocated by `A`.
405 unsafe impl<T: 'static, A> ForeignOwnable for Box<T, A>
406 where
407     A: Allocator,
408 {
409     const FOREIGN_ALIGN: usize = if core::mem::align_of::<T>() < A::MIN_ALIGN {
410         A::MIN_ALIGN
411     } else {
412         core::mem::align_of::<T>()
413     };
414 
415     type Borrowed<'a> = &'a T;
416     type BorrowedMut<'a> = &'a mut T;
417 
418     fn into_foreign(self) -> *mut c_void {
419         Box::into_raw(self).cast()
420     }
421 
422     unsafe fn from_foreign(ptr: *mut c_void) -> Self {
423         // SAFETY: The safety requirements of this function ensure that `ptr` comes from a previous
424         // call to `Self::into_foreign`.
425         unsafe { Box::from_raw(ptr.cast()) }
426     }
427 
428     unsafe fn borrow<'a>(ptr: *mut c_void) -> &'a T {
429         // SAFETY: The safety requirements of this method ensure that the object remains alive and
430         // immutable for the duration of 'a.
431         unsafe { &*ptr.cast() }
432     }
433 
434     unsafe fn borrow_mut<'a>(ptr: *mut c_void) -> &'a mut T {
435         let ptr = ptr.cast();
436         // SAFETY: The safety requirements of this method ensure that the pointer is valid and that
437         // nothing else will access the value for the duration of 'a.
438         unsafe { &mut *ptr }
439     }
440 }
441 
442 // SAFETY: The pointer returned by `into_foreign` comes from a well aligned
443 // pointer to `T` allocated by `A`.
444 unsafe impl<T: 'static, A> ForeignOwnable for Pin<Box<T, A>>
445 where
446     A: Allocator,
447 {
448     const FOREIGN_ALIGN: usize = <Box<T, A> as ForeignOwnable>::FOREIGN_ALIGN;
449     type Borrowed<'a> = Pin<&'a T>;
450     type BorrowedMut<'a> = Pin<&'a mut T>;
451 
452     fn into_foreign(self) -> *mut c_void {
453         // SAFETY: We are still treating the box as pinned.
454         Box::into_raw(unsafe { Pin::into_inner_unchecked(self) }).cast()
455     }
456 
457     unsafe fn from_foreign(ptr: *mut c_void) -> Self {
458         // SAFETY: The safety requirements of this function ensure that `ptr` comes from a previous
459         // call to `Self::into_foreign`.
460         unsafe { Pin::new_unchecked(Box::from_raw(ptr.cast())) }
461     }
462 
463     unsafe fn borrow<'a>(ptr: *mut c_void) -> Pin<&'a T> {
464         // SAFETY: The safety requirements for this function ensure that the object is still alive,
465         // so it is safe to dereference the raw pointer.
466         // The safety requirements of `from_foreign` also ensure that the object remains alive for
467         // the lifetime of the returned value.
468         let r = unsafe { &*ptr.cast() };
469 
470         // SAFETY: This pointer originates from a `Pin<Box<T>>`.
471         unsafe { Pin::new_unchecked(r) }
472     }
473 
474     unsafe fn borrow_mut<'a>(ptr: *mut c_void) -> Pin<&'a mut T> {
475         let ptr = ptr.cast();
476         // SAFETY: The safety requirements for this function ensure that the object is still alive,
477         // so it is safe to dereference the raw pointer.
478         // The safety requirements of `from_foreign` also ensure that the object remains alive for
479         // the lifetime of the returned value.
480         let r = unsafe { &mut *ptr };
481 
482         // SAFETY: This pointer originates from a `Pin<Box<T>>`.
483         unsafe { Pin::new_unchecked(r) }
484     }
485 }
486 
487 impl<T, A> Deref for Box<T, A>
488 where
489     T: ?Sized,
490     A: Allocator,
491 {
492     type Target = T;
493 
494     fn deref(&self) -> &T {
495         // SAFETY: `self.0` is always properly aligned, dereferenceable and points to an initialized
496         // instance of `T`.
497         unsafe { self.0.as_ref() }
498     }
499 }
500 
501 impl<T, A> DerefMut for Box<T, A>
502 where
503     T: ?Sized,
504     A: Allocator,
505 {
506     fn deref_mut(&mut self) -> &mut T {
507         // SAFETY: `self.0` is always properly aligned, dereferenceable and points to an initialized
508         // instance of `T`.
509         unsafe { self.0.as_mut() }
510     }
511 }
512 
513 /// # Examples
514 ///
515 /// ```
516 /// # use core::borrow::Borrow;
517 /// # use kernel::alloc::KBox;
518 /// struct Foo<B: Borrow<u32>>(B);
519 ///
520 /// // Owned instance.
521 /// let owned = Foo(1);
522 ///
523 /// // Owned instance using `KBox`.
524 /// let owned_kbox = Foo(KBox::new(1, GFP_KERNEL)?);
525 ///
526 /// let i = 1;
527 /// // Borrowed from `i`.
528 /// let borrowed = Foo(&i);
529 /// # Ok::<(), Error>(())
530 /// ```
531 impl<T, A> Borrow<T> for Box<T, A>
532 where
533     T: ?Sized,
534     A: Allocator,
535 {
536     fn borrow(&self) -> &T {
537         self.deref()
538     }
539 }
540 
541 /// # Examples
542 ///
543 /// ```
544 /// # use core::borrow::BorrowMut;
545 /// # use kernel::alloc::KBox;
546 /// struct Foo<B: BorrowMut<u32>>(B);
547 ///
548 /// // Owned instance.
549 /// let owned = Foo(1);
550 ///
551 /// // Owned instance using `KBox`.
552 /// let owned_kbox = Foo(KBox::new(1, GFP_KERNEL)?);
553 ///
554 /// let mut i = 1;
555 /// // Borrowed from `i`.
556 /// let borrowed = Foo(&mut i);
557 /// # Ok::<(), Error>(())
558 /// ```
559 impl<T, A> BorrowMut<T> for Box<T, A>
560 where
561     T: ?Sized,
562     A: Allocator,
563 {
564     fn borrow_mut(&mut self) -> &mut T {
565         self.deref_mut()
566     }
567 }
568 
569 impl<T, A> fmt::Display for Box<T, A>
570 where
571     T: ?Sized + fmt::Display,
572     A: Allocator,
573 {
574     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
575         <T as fmt::Display>::fmt(&**self, f)
576     }
577 }
578 
579 impl<T, A> fmt::Debug for Box<T, A>
580 where
581     T: ?Sized + fmt::Debug,
582     A: Allocator,
583 {
584     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
585         <T as fmt::Debug>::fmt(&**self, f)
586     }
587 }
588 
589 impl<T, A> Drop for Box<T, A>
590 where
591     T: ?Sized,
592     A: Allocator,
593 {
594     fn drop(&mut self) {
595         let layout = Layout::for_value::<T>(self);
596 
597         // SAFETY: The pointer in `self.0` is guaranteed to be valid by the type invariant.
598         unsafe { core::ptr::drop_in_place::<T>(self.deref_mut()) };
599 
600         // SAFETY:
601         // - `self.0` was previously allocated with `A`.
602         // - `layout` is equal to the `Layout´ `self.0` was allocated with.
603         unsafe { A::free(self.0.cast(), layout) };
604     }
605 }
606