| 1 | |
| 2 |
|
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 |
|
| 11 | use super::Flags;
|
| 12 | use core::alloc::Layout;
|
| 13 | use core::ptr;
|
| 14 | use core::ptr::NonNull;
|
| 15 |
|
| 16 | use crate::alloc::{AllocError, Allocator, NumaNode};
|
| 17 | use crate::bindings;
|
| 18 | use crate::page;
|
| 19 |
|
| 20 | const ARCH_KMALLOC_MINALIGN: usize = bindings::ARCH_KMALLOC_MINALIGN;
|
| 21 |
|
| 22 | mod iter;
|
| 23 | pub use self::iter::VmallocPageIter;
|
| 24 |
|
| 25 | |
| 26 | |
| 27 | |
| 28 | |
| 29 | |
| 30 | |
| 31 | pub struct Kmalloc;
|
| 32 |
|
| 33 | |
| 34 | |
| 35 | |
| 36 | |
| 37 | |
| 38 | |
| 39 | |
| 40 | pub struct Vmalloc;
|
| 41 |
|
| 42 | |
| 43 | |
| 44 | |
| 45 | |
| 46 | |
| 47 | |
| 48 | |
| 49 | pub struct KVmalloc;
|
| 50 |
|
| 51 | |
| 52 | |
| 53 | |
| 54 | struct ReallocFunc(
|
| 55 | unsafe extern "C" fn(
|
| 56 | *const crate::ffi::c_void,
|
| 57 | usize,
|
| 58 | crate::ffi::c_ulong,
|
| 59 | u32,
|
| 60 | crate::ffi::c_int,
|
| 61 | ) -> *mut crate::ffi::c_void,
|
| 62 | );
|
| 63 |
|
| 64 | impl ReallocFunc {
|
| 65 | |
| 66 | const KREALLOC: Self = Self(bindings::krealloc_node_align);
|
| 67 |
|
| 68 | |
| 69 | const VREALLOC: Self = Self(bindings::vrealloc_node_align);
|
| 70 |
|
| 71 | |
| 72 | const KVREALLOC: Self = Self(bindings::kvrealloc_node_align);
|
| 73 |
|
| 74 | |
| 75 | |
| 76 | |
| 77 | |
| 78 | |
| 79 | |
| 80 | |
| 81 | |
| 82 | |
| 83 | #[inline]
|
| 84 | unsafe fn call(
|
| 85 | &self,
|
| 86 | ptr: Option<NonNull<u8>>,
|
| 87 | layout: Layout,
|
| 88 | old_layout: Layout,
|
| 89 | flags: Flags,
|
| 90 | nid: NumaNode,
|
| 91 | ) -> Result<NonNull<[u8]>, AllocError> {
|
| 92 | let size = layout.size();
|
| 93 | let ptr = match ptr {
|
| 94 | Some(ptr) => {
|
| 95 | if old_layout.size() == 0 {
|
| 96 | ptr::null()
|
| 97 | } else {
|
| 98 | ptr.as_ptr()
|
| 99 | }
|
| 100 | }
|
| 101 | None => ptr::null(),
|
| 102 | };
|
| 103 |
|
| 104 | |
| 105 | |
| 106 | |
| 107 | |
| 108 | |
| 109 | |
| 110 | |
| 111 | |
| 112 | let raw_ptr = unsafe {
|
| 113 | |
| 114 | self.0(ptr.cast(), size, layout.align(), flags.0, nid.0).cast()
|
| 115 | };
|
| 116 |
|
| 117 | let ptr = if size == 0 {
|
| 118 | crate::alloc::dangling_from_layout(layout)
|
| 119 | } else {
|
| 120 | NonNull::new(raw_ptr).ok_or(AllocError)?
|
| 121 | };
|
| 122 |
|
| 123 | Ok(NonNull::slice_from_raw_parts(ptr, size))
|
| 124 | }
|
| 125 | }
|
| 126 |
|
| 127 | impl Kmalloc {
|
| 128 | |
| 129 | |
| 130 | pub fn aligned_layout(layout: Layout) -> Layout {
|
| 131 | |
| 132 | |
| 133 | |
| 134 | layout.pad_to_align()
|
| 135 | }
|
| 136 | }
|
| 137 |
|
| 138 | |
| 139 | |
| 140 | |
| 141 | |
| 142 | unsafe impl Allocator for Kmalloc {
|
| 143 | const MIN_ALIGN: usize = ARCH_KMALLOC_MINALIGN;
|
| 144 |
|
| 145 | #[inline]
|
| 146 | unsafe fn realloc(
|
| 147 | ptr: Option<NonNull<u8>>,
|
| 148 | layout: Layout,
|
| 149 | old_layout: Layout,
|
| 150 | flags: Flags,
|
| 151 | nid: NumaNode,
|
| 152 | ) -> Result<NonNull<[u8]>, AllocError> {
|
| 153 | let layout = Kmalloc::aligned_layout(layout);
|
| 154 |
|
| 155 | |
| 156 | unsafe { ReallocFunc::KREALLOC.call(ptr, layout, old_layout, flags, nid) }
|
| 157 | }
|
| 158 | }
|
| 159 |
|
| 160 | impl Vmalloc {
|
| 161 | |
| 162 | |
| 163 | |
| 164 | |
| 165 | |
| 166 | |
| 167 | |
| 168 | |
| 169 | |
| 170 | |
| 171 | |
| 172 | |
| 173 | |
| 174 | |
| 175 | |
| 176 | |
| 177 | |
| 178 | |
| 179 | |
| 180 | |
| 181 | |
| 182 | |
| 183 | |
| 184 | |
| 185 | |
| 186 | |
| 187 | |
| 188 | |
| 189 | |
| 190 | |
| 191 | pub unsafe fn to_page<'a>(ptr: NonNull<u8>) -> page::BorrowedPage<'a> {
|
| 192 | |
| 193 | let page = unsafe { bindings::vmalloc_to_page(ptr.as_ptr().cast()) };
|
| 194 |
|
| 195 | |
| 196 | |
| 197 | let page = unsafe { NonNull::new_unchecked(page) };
|
| 198 |
|
| 199 | |
| 200 | |
| 201 | |
| 202 | |
| 203 | |
| 204 | unsafe { page::BorrowedPage::from_raw(page) }
|
| 205 | }
|
| 206 | }
|
| 207 |
|
| 208 | |
| 209 | |
| 210 | |
| 211 | |
| 212 | unsafe impl Allocator for Vmalloc {
|
| 213 | const MIN_ALIGN: usize = kernel::page::PAGE_SIZE;
|
| 214 |
|
| 215 | #[inline]
|
| 216 | unsafe fn realloc(
|
| 217 | ptr: Option<NonNull<u8>>,
|
| 218 | layout: Layout,
|
| 219 | old_layout: Layout,
|
| 220 | flags: Flags,
|
| 221 | nid: NumaNode,
|
| 222 | ) -> Result<NonNull<[u8]>, AllocError> {
|
| 223 | |
| 224 | |
| 225 | unsafe { ReallocFunc::VREALLOC.call(ptr, layout, old_layout, flags, nid) }
|
| 226 | }
|
| 227 | }
|
| 228 |
|
| 229 | |
| 230 | |
| 231 | |
| 232 | |
| 233 | unsafe impl Allocator for KVmalloc {
|
| 234 | const MIN_ALIGN: usize = ARCH_KMALLOC_MINALIGN;
|
| 235 |
|
| 236 | #[inline]
|
| 237 | unsafe fn realloc(
|
| 238 | ptr: Option<NonNull<u8>>,
|
| 239 | layout: Layout,
|
| 240 | old_layout: Layout,
|
| 241 | flags: Flags,
|
| 242 | nid: NumaNode,
|
| 243 | ) -> Result<NonNull<[u8]>, AllocError> {
|
| 244 | |
| 245 | |
| 246 | let layout = Kmalloc::aligned_layout(layout);
|
| 247 |
|
| 248 | |
| 249 | |
| 250 | unsafe { ReallocFunc::KVREALLOC.call(ptr, layout, old_layout, flags, nid) }
|
| 251 | }
|
| 252 | }
|
| 253 |
|
| 254 | #[macros::kunit_tests(rust_allocator)]
|
| 255 | mod tests {
|
| 256 | use super::*;
|
| 257 | use core::mem::MaybeUninit;
|
| 258 | use kernel::prelude::*;
|
| 259 |
|
| 260 | #[test]
|
| 261 | fn test_alignment() -> Result {
|
| 262 | const TEST_SIZE: usize = 1024;
|
| 263 | const TEST_LARGE_ALIGN_SIZE: usize = kernel::page::PAGE_SIZE * 4;
|
| 264 |
|
| 265 | |
| 266 | |
| 267 | #[expect(dead_code)]
|
| 268 | #[repr(align(128))]
|
| 269 | struct Blob([u8; TEST_SIZE]);
|
| 270 | #[expect(dead_code)]
|
| 271 | #[repr(align(8192))]
|
| 272 | struct LargeAlignBlob([u8; TEST_LARGE_ALIGN_SIZE]);
|
| 273 |
|
| 274 | struct TestAlign<T, A: Allocator>(Box<MaybeUninit<T>, A>);
|
| 275 | impl<T, A: Allocator> TestAlign<T, A> {
|
| 276 | fn new() -> Result<Self> {
|
| 277 | Ok(Self(Box::<_, A>::new_uninit(GFP_KERNEL)?))
|
| 278 | }
|
| 279 |
|
| 280 | fn is_aligned_to(&self, align: usize) -> bool {
|
| 281 | assert!(align.is_power_of_two());
|
| 282 |
|
| 283 | let addr = self.0.as_ptr() as usize;
|
| 284 | addr & (align - 1) == 0
|
| 285 | }
|
| 286 | }
|
| 287 |
|
| 288 | let ta = TestAlign::<Blob, Kmalloc>::new()?;
|
| 289 | assert!(ta.is_aligned_to(128));
|
| 290 |
|
| 291 | let ta = TestAlign::<LargeAlignBlob, Kmalloc>::new()?;
|
| 292 | assert!(ta.is_aligned_to(8192));
|
| 293 |
|
| 294 | let ta = TestAlign::<Blob, Vmalloc>::new()?;
|
| 295 | assert!(ta.is_aligned_to(128));
|
| 296 |
|
| 297 | let ta = TestAlign::<LargeAlignBlob, Vmalloc>::new()?;
|
| 298 | assert!(ta.is_aligned_to(8192));
|
| 299 |
|
| 300 | let ta = TestAlign::<Blob, KVmalloc>::new()?;
|
| 301 | assert!(ta.is_aligned_to(128));
|
| 302 |
|
| 303 | let ta = TestAlign::<LargeAlignBlob, KVmalloc>::new()?;
|
| 304 | assert!(ta.is_aligned_to(8192));
|
| 305 |
|
| 306 | Ok(())
|
| 307 | }
|
| 308 | }
|