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