allocator.rs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. // SPDX-License-Identifier: GPL-2.0
  2. //! Allocator support.
  3. use core::alloc::{GlobalAlloc, Layout};
  4. use core::ptr;
  5. use crate::bindings;
  6. struct KernelAllocator;
  7. /// Calls `krealloc` with a proper size to alloc a new object aligned to `new_layout`'s alignment.
  8. ///
  9. /// # Safety
  10. ///
  11. /// - `ptr` can be either null or a pointer which has been allocated by this allocator.
  12. /// - `new_layout` must have a non-zero size.
  13. unsafe fn krealloc_aligned(ptr: *mut u8, new_layout: Layout, flags: bindings::gfp_t) -> *mut u8 {
  14. // Customized layouts from `Layout::from_size_align()` can have size < align, so pad first.
  15. let layout = new_layout.pad_to_align();
  16. let mut size = layout.size();
  17. if layout.align() > bindings::BINDINGS_ARCH_SLAB_MINALIGN {
  18. // The alignment requirement exceeds the slab guarantee, thus try to enlarge the size
  19. // to use the "power-of-two" size/alignment guarantee (see comments in `kmalloc()` for
  20. // more information).
  21. //
  22. // Note that `layout.size()` (after padding) is guaranteed to be a multiple of
  23. // `layout.align()`, so `next_power_of_two` gives enough alignment guarantee.
  24. size = size.next_power_of_two();
  25. }
  26. // SAFETY:
  27. // - `ptr` is either null or a pointer returned from a previous `k{re}alloc()` by the
  28. // function safety requirement.
  29. // - `size` is greater than 0 since it's either a `layout.size()` (which cannot be zero
  30. // according to the function safety requirement) or a result from `next_power_of_two()`.
  31. unsafe { bindings::krealloc(ptr as *const core::ffi::c_void, size, flags) as *mut u8 }
  32. }
  33. unsafe impl GlobalAlloc for KernelAllocator {
  34. unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
  35. // `krealloc()` is used instead of `kmalloc()` because the latter is
  36. // an inline function and cannot be bound to as a result.
  37. unsafe { bindings::krealloc(ptr::null(), layout.size(), bindings::GFP_KERNEL) as *mut u8 }
  38. }
  39. unsafe fn dealloc(&self, ptr: *mut u8, _layout: Layout) {
  40. unsafe {
  41. bindings::kfree(ptr as *const core::ffi::c_void);
  42. }
  43. }
  44. }
  45. #[global_allocator]
  46. static ALLOCATOR: KernelAllocator = KernelAllocator;
  47. // `rustc` only generates these for some crate types. Even then, we would need
  48. // to extract the object file that has them from the archive. For the moment,
  49. // let's generate them ourselves instead.
  50. //
  51. // Note: Although these are *safe* functions, they are called by the compiler
  52. // with parameters that obey the same `GlobalAlloc` function safety
  53. // requirements: size and align should form a valid layout, and size is
  54. // greater than 0.
  55. //
  56. // Note that `#[no_mangle]` implies exported too, nowadays.
  57. #[no_mangle]
  58. fn __rust_alloc(size: usize, align: usize) -> *mut u8 {
  59. // SAFETY: See assumption above.
  60. let layout = unsafe { Layout::from_size_align_unchecked(size, align) };
  61. // SAFETY: `ptr::null_mut()` is null, per assumption above the size of `layout` is greater
  62. // than 0.
  63. unsafe { krealloc_aligned(ptr::null_mut(), layout, bindings::GFP_KERNEL) }
  64. }
  65. #[no_mangle]
  66. fn __rust_dealloc(ptr: *mut u8, _size: usize, _align: usize) {
  67. unsafe { bindings::kfree(ptr as *const core::ffi::c_void) };
  68. }
  69. #[no_mangle]
  70. fn __rust_realloc(ptr: *mut u8, _old_size: usize, align: usize, new_size: usize) -> *mut u8 {
  71. // SAFETY: See assumption above.
  72. let new_layout = unsafe { Layout::from_size_align_unchecked(new_size, align) };
  73. // SAFETY: Per assumption above, `ptr` is allocated by `__rust_*` before, and the size of
  74. // `new_layout` is greater than 0.
  75. unsafe { krealloc_aligned(ptr, new_layout, bindings::GFP_KERNEL) }
  76. }
  77. #[no_mangle]
  78. fn __rust_alloc_zeroed(size: usize, align: usize) -> *mut u8 {
  79. // SAFETY: See assumption above.
  80. let layout = unsafe { Layout::from_size_align_unchecked(size, align) };
  81. // SAFETY: `ptr::null_mut()` is null, per assumption above the size of `layout` is greater
  82. // than 0.
  83. unsafe {
  84. krealloc_aligned(
  85. ptr::null_mut(),
  86. layout,
  87. bindings::GFP_KERNEL | bindings::__GFP_ZERO,
  88. )
  89. }
  90. }