| 1 | |
| 2 |
|
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 |
|
| 9 | use core::{
|
| 10 | marker::PhantomData,
|
| 11 | ops::{Bound, RangeBounds},
|
| 12 | ptr,
|
| 13 | };
|
| 14 |
|
| 15 | use kernel::{
|
| 16 | alloc::Flags,
|
| 17 | error::to_result,
|
| 18 | prelude::*,
|
| 19 | types::{ForeignOwnable, Opaque},
|
| 20 | };
|
| 21 |
|
| 22 | |
| 23 | |
| 24 | |
| 25 | |
| 26 | |
| 27 | #[pin_data(PinnedDrop)]
|
| 28 | #[repr(transparent)]
|
| 29 | pub struct MapleTree<T: ForeignOwnable> {
|
| 30 | #[pin]
|
| 31 | tree: Opaque<bindings::maple_tree>,
|
| 32 | _p: PhantomData<T>,
|
| 33 | }
|
| 34 |
|
| 35 | |
| 36 | |
| 37 | |
| 38 | #[pin_data]
|
| 39 | #[repr(transparent)]
|
| 40 | pub struct MapleTreeAlloc<T: ForeignOwnable> {
|
| 41 | #[pin]
|
| 42 | tree: MapleTree<T>,
|
| 43 | }
|
| 44 |
|
| 45 | |
| 46 | impl<T: ForeignOwnable> core::ops::Deref for MapleTreeAlloc<T> {
|
| 47 | type Target = MapleTree<T>;
|
| 48 |
|
| 49 | #[inline]
|
| 50 | fn deref(&self) -> &MapleTree<T> {
|
| 51 | &self.tree
|
| 52 | }
|
| 53 | }
|
| 54 |
|
| 55 | #[inline]
|
| 56 | fn to_maple_range(range: impl RangeBounds<usize>) -> Option<(usize, usize)> {
|
| 57 | let first = match range.start_bound() {
|
| 58 | Bound::Included(start) => *start,
|
| 59 | Bound::Excluded(start) => start.checked_add(1)?,
|
| 60 | Bound::Unbounded => 0,
|
| 61 | };
|
| 62 |
|
| 63 | let last = match range.end_bound() {
|
| 64 | Bound::Included(end) => *end,
|
| 65 | Bound::Excluded(end) => end.checked_sub(1)?,
|
| 66 | Bound::Unbounded => usize::MAX,
|
| 67 | };
|
| 68 |
|
| 69 | if last < first {
|
| 70 | return None;
|
| 71 | }
|
| 72 |
|
| 73 | Some((first, last))
|
| 74 | }
|
| 75 |
|
| 76 | impl<T: ForeignOwnable> MapleTree<T> {
|
| 77 | |
| 78 | |
| 79 | |
| 80 | |
| 81 | #[inline]
|
| 82 | pub fn new() -> impl PinInit<Self> {
|
| 83 | pin_init!(MapleTree {
|
| 84 | |
| 85 | |
| 86 | tree <- Opaque::ffi_init(|slot| unsafe { bindings::mt_init_flags(slot, 0) }),
|
| 87 | _p: PhantomData,
|
| 88 | })
|
| 89 | }
|
| 90 |
|
| 91 | |
| 92 | |
| 93 | |
| 94 | |
| 95 | |
| 96 | |
| 97 | |
| 98 | |
| 99 | |
| 100 | |
| 101 | |
| 102 | |
| 103 | |
| 104 | |
| 105 | |
| 106 | |
| 107 | |
| 108 | |
| 109 | |
| 110 | |
| 111 | |
| 112 | |
| 113 | |
| 114 | |
| 115 | |
| 116 | |
| 117 | |
| 118 | |
| 119 | |
| 120 | #[inline]
|
| 121 | pub fn insert(&self, index: usize, value: T, gfp: Flags) -> Result<(), InsertError<T>> {
|
| 122 | self.insert_range(index..=index, value, gfp)
|
| 123 | }
|
| 124 |
|
| 125 | |
| 126 | |
| 127 | |
| 128 | |
| 129 | |
| 130 | |
| 131 | |
| 132 | |
| 133 | |
| 134 | |
| 135 | |
| 136 | |
| 137 | |
| 138 | |
| 139 | |
| 140 | |
| 141 | |
| 142 | |
| 143 | |
| 144 | |
| 145 | |
| 146 | |
| 147 | |
| 148 | |
| 149 | |
| 150 | |
| 151 | |
| 152 | |
| 153 | |
| 154 | |
| 155 | |
| 156 | |
| 157 | |
| 158 | |
| 159 | |
| 160 | |
| 161 | |
| 162 | |
| 163 | |
| 164 | |
| 165 | |
| 166 | |
| 167 | |
| 168 | |
| 169 | pub fn insert_range<R>(&self, range: R, value: T, gfp: Flags) -> Result<(), InsertError<T>>
|
| 170 | where
|
| 171 | R: RangeBounds<usize>,
|
| 172 | {
|
| 173 | let Some((first, last)) = to_maple_range(range) else {
|
| 174 | return Err(InsertError {
|
| 175 | value,
|
| 176 | cause: InsertErrorKind::InvalidRequest,
|
| 177 | });
|
| 178 | };
|
| 179 |
|
| 180 | let ptr = T::into_foreign(value);
|
| 181 |
|
| 182 | |
| 183 | let res = to_result(unsafe {
|
| 184 | bindings::mtree_insert_range(self.tree.get(), first, last, ptr, gfp.as_raw())
|
| 185 | });
|
| 186 |
|
| 187 | if let Err(err) = res {
|
| 188 | |
| 189 | let value = unsafe { T::from_foreign(ptr) };
|
| 190 |
|
| 191 | let cause = if err == ENOMEM {
|
| 192 | InsertErrorKind::AllocError(kernel::alloc::AllocError)
|
| 193 | } else if err == EEXIST {
|
| 194 | InsertErrorKind::Occupied
|
| 195 | } else {
|
| 196 | InsertErrorKind::InvalidRequest
|
| 197 | };
|
| 198 | Err(InsertError { value, cause })
|
| 199 | } else {
|
| 200 | Ok(())
|
| 201 | }
|
| 202 | }
|
| 203 |
|
| 204 | |
| 205 | |
| 206 | |
| 207 | |
| 208 | |
| 209 | |
| 210 | |
| 211 | |
| 212 | |
| 213 | |
| 214 | |
| 215 | |
| 216 | |
| 217 | |
| 218 | |
| 219 | |
| 220 | |
| 221 | |
| 222 | |
| 223 | |
| 224 | |
| 225 | |
| 226 | #[inline]
|
| 227 | pub fn erase(&self, index: usize) -> Option<T> {
|
| 228 | |
| 229 | let ret = unsafe { bindings::mtree_erase(self.tree.get(), index) };
|
| 230 |
|
| 231 | |
| 232 | |
| 233 | unsafe { T::try_from_foreign(ret) }
|
| 234 | }
|
| 235 |
|
| 236 | |
| 237 | #[inline]
|
| 238 | pub fn lock(&self) -> MapleGuard<'_, T> {
|
| 239 | |
| 240 | unsafe { bindings::spin_lock(self.ma_lock()) };
|
| 241 |
|
| 242 | |
| 243 | MapleGuard(self)
|
| 244 | }
|
| 245 |
|
| 246 | #[inline]
|
| 247 | fn ma_lock(&self) -> *mut bindings::spinlock_t {
|
| 248 | |
| 249 | let lock_ptr = unsafe { &raw mut (*self.tree.get()).__bindgen_anon_1.ma_lock };
|
| 250 | lock_ptr.cast()
|
| 251 | }
|
| 252 |
|
| 253 | |
| 254 | |
| 255 | |
| 256 | |
| 257 | |
| 258 | |
| 259 | |
| 260 | unsafe fn free_all_entries(self: Pin<&mut Self>) {
|
| 261 | |
| 262 | |
| 263 | let mut ma_state = unsafe { MaState::new_raw(self.into_ref().get_ref(), 0, usize::MAX) };
|
| 264 |
|
| 265 | loop {
|
| 266 | |
| 267 | |
| 268 | |
| 269 | |
| 270 | |
| 271 | |
| 272 | |
| 273 | |
| 274 | let ptr = {
|
| 275 | let _rcu = kernel::sync::rcu::Guard::new();
|
| 276 | ma_state.mas_find_raw(usize::MAX)
|
| 277 | };
|
| 278 | if ptr.is_null() {
|
| 279 | break;
|
| 280 | }
|
| 281 | |
| 282 | |
| 283 | |
| 284 | drop(unsafe { T::from_foreign(ptr) });
|
| 285 | }
|
| 286 | }
|
| 287 | }
|
| 288 |
|
| 289 | #[pinned_drop]
|
| 290 | impl<T: ForeignOwnable> PinnedDrop for MapleTree<T> {
|
| 291 | #[inline]
|
| 292 | fn drop(mut self: Pin<&mut Self>) {
|
| 293 | |
| 294 | if core::mem::needs_drop::<T>() {
|
| 295 | |
| 296 | |
| 297 | unsafe { self.as_mut().free_all_entries() };
|
| 298 | }
|
| 299 |
|
| 300 | |
| 301 | unsafe { bindings::mtree_destroy(self.tree.get()) };
|
| 302 | }
|
| 303 | }
|
| 304 |
|
| 305 | |
| 306 | |
| 307 | |
| 308 | |
| 309 | |
| 310 | #[must_use = "if unused, the lock will be immediately unlocked"]
|
| 311 | pub struct MapleGuard<'tree, T: ForeignOwnable>(&'tree MapleTree<T>);
|
| 312 |
|
| 313 | impl<'tree, T: ForeignOwnable> Drop for MapleGuard<'tree, T> {
|
| 314 | #[inline]
|
| 315 | fn drop(&mut self) {
|
| 316 | |
| 317 | unsafe { bindings::spin_unlock(self.0.ma_lock()) };
|
| 318 | }
|
| 319 | }
|
| 320 |
|
| 321 | impl<'tree, T: ForeignOwnable> MapleGuard<'tree, T> {
|
| 322 | |
| 323 | pub fn ma_state(&mut self, first: usize, end: usize) -> MaState<'_, T> {
|
| 324 | |
| 325 | |
| 326 | unsafe { MaState::new_raw(self.0, first, end) }
|
| 327 | }
|
| 328 |
|
| 329 | |
| 330 | |
| 331 | |
| 332 | |
| 333 | |
| 334 | |
| 335 | |
| 336 | |
| 337 | |
| 338 | |
| 339 | |
| 340 | |
| 341 | |
| 342 | |
| 343 | |
| 344 | |
| 345 | |
| 346 | |
| 347 | |
| 348 | |
| 349 | |
| 350 | |
| 351 | |
| 352 | |
| 353 | |
| 354 | |
| 355 | |
| 356 | |
| 357 | |
| 358 | |
| 359 | |
| 360 | |
| 361 | |
| 362 | |
| 363 | |
| 364 | |
| 365 | |
| 366 | |
| 367 | |
| 368 | |
| 369 | |
| 370 | |
| 371 | |
| 372 | |
| 373 | |
| 374 | |
| 375 | #[inline]
|
| 376 | pub fn load(&mut self, index: usize) -> Option<T::BorrowedMut<'_>> {
|
| 377 | |
| 378 | let ret = unsafe { bindings::mtree_load(self.0.tree.get(), index) };
|
| 379 | if ret.is_null() {
|
| 380 | return None;
|
| 381 | }
|
| 382 |
|
| 383 | |
| 384 | |
| 385 | |
| 386 | Some(unsafe { T::borrow_mut(ret) })
|
| 387 | }
|
| 388 | }
|
| 389 |
|
| 390 | impl<T: ForeignOwnable> MapleTreeAlloc<T> {
|
| 391 | |
| 392 | pub fn new() -> impl PinInit<Self> {
|
| 393 | let tree = pin_init!(MapleTree {
|
| 394 | |
| 395 | |
| 396 | tree <- Opaque::ffi_init(|slot| unsafe {
|
| 397 | bindings::mt_init_flags(slot, bindings::MT_FLAGS_ALLOC_RANGE)
|
| 398 | }),
|
| 399 | _p: PhantomData,
|
| 400 | });
|
| 401 |
|
| 402 | pin_init!(MapleTreeAlloc { tree <- tree })
|
| 403 | }
|
| 404 |
|
| 405 | |
| 406 | |
| 407 | |
| 408 | |
| 409 | |
| 410 | |
| 411 | |
| 412 | |
| 413 | |
| 414 | |
| 415 | |
| 416 | |
| 417 | |
| 418 | |
| 419 | |
| 420 | |
| 421 | |
| 422 | |
| 423 | |
| 424 | |
| 425 | |
| 426 | |
| 427 | |
| 428 | |
| 429 | |
| 430 | |
| 431 | |
| 432 | |
| 433 | |
| 434 | |
| 435 | |
| 436 | |
| 437 | |
| 438 | |
| 439 | |
| 440 | pub fn alloc_range<R>(
|
| 441 | &self,
|
| 442 | size: usize,
|
| 443 | value: T,
|
| 444 | range: R,
|
| 445 | gfp: Flags,
|
| 446 | ) -> Result<usize, AllocError<T>>
|
| 447 | where
|
| 448 | R: RangeBounds<usize>,
|
| 449 | {
|
| 450 | let Some((min, max)) = to_maple_range(range) else {
|
| 451 | return Err(AllocError {
|
| 452 | value,
|
| 453 | cause: AllocErrorKind::InvalidRequest,
|
| 454 | });
|
| 455 | };
|
| 456 |
|
| 457 | let ptr = T::into_foreign(value);
|
| 458 | let mut index = 0;
|
| 459 |
|
| 460 | |
| 461 | let res = to_result(unsafe {
|
| 462 | bindings::mtree_alloc_range(
|
| 463 | self.tree.tree.get(),
|
| 464 | &mut index,
|
| 465 | ptr,
|
| 466 | size,
|
| 467 | min,
|
| 468 | max,
|
| 469 | gfp.as_raw(),
|
| 470 | )
|
| 471 | });
|
| 472 |
|
| 473 | if let Err(err) = res {
|
| 474 | |
| 475 | let value = unsafe { T::from_foreign(ptr) };
|
| 476 |
|
| 477 | let cause = if err == ENOMEM {
|
| 478 | AllocErrorKind::AllocError(kernel::alloc::AllocError)
|
| 479 | } else if err == EBUSY {
|
| 480 | AllocErrorKind::Busy
|
| 481 | } else {
|
| 482 | AllocErrorKind::InvalidRequest
|
| 483 | };
|
| 484 | Err(AllocError { value, cause })
|
| 485 | } else {
|
| 486 | Ok(index)
|
| 487 | }
|
| 488 | }
|
| 489 | }
|
| 490 |
|
| 491 | |
| 492 | |
| 493 | |
| 494 | |
| 495 | |
| 496 | |
| 497 | |
| 498 | |
| 499 | pub struct MaState<'tree, T: ForeignOwnable> {
|
| 500 | state: bindings::ma_state,
|
| 501 | _phantom: PhantomData<&'tree mut MapleTree<T>>,
|
| 502 | }
|
| 503 |
|
| 504 | impl<'tree, T: ForeignOwnable> MaState<'tree, T> {
|
| 505 | |
| 506 | |
| 507 | |
| 508 | |
| 509 | |
| 510 | #[inline]
|
| 511 | unsafe fn new_raw(mt: &'tree MapleTree<T>, first: usize, end: usize) -> Self {
|
| 512 | |
| 513 | |
| 514 | |
| 515 | Self {
|
| 516 | state: bindings::ma_state {
|
| 517 | tree: mt.tree.get(),
|
| 518 | index: first,
|
| 519 | last: end,
|
| 520 | node: ptr::null_mut(),
|
| 521 | status: bindings::maple_status_ma_start,
|
| 522 | min: 0,
|
| 523 | max: usize::MAX,
|
| 524 | alloc: ptr::null_mut(),
|
| 525 | mas_flags: 0,
|
| 526 | store_type: bindings::store_type_wr_invalid,
|
| 527 | ..Default::default()
|
| 528 | },
|
| 529 | _phantom: PhantomData,
|
| 530 | }
|
| 531 | }
|
| 532 |
|
| 533 | #[inline]
|
| 534 | fn as_raw(&mut self) -> *mut bindings::ma_state {
|
| 535 | &raw mut self.state
|
| 536 | }
|
| 537 |
|
| 538 | #[inline]
|
| 539 | fn mas_find_raw(&mut self, max: usize) -> *mut c_void {
|
| 540 | |
| 541 | |
| 542 | unsafe { bindings::mas_find(self.as_raw(), max) }
|
| 543 | }
|
| 544 |
|
| 545 | |
| 546 | |
| 547 | |
| 548 | |
| 549 | |
| 550 | |
| 551 | |
| 552 | |
| 553 | |
| 554 | |
| 555 | |
| 556 | |
| 557 | |
| 558 | |
| 559 | |
| 560 | |
| 561 | |
| 562 | |
| 563 | |
| 564 | |
| 565 | |
| 566 | |
| 567 | |
| 568 | |
| 569 | |
| 570 | #[inline]
|
| 571 | pub fn find(&mut self, max: usize) -> Option<T::BorrowedMut<'_>> {
|
| 572 | let ret = self.mas_find_raw(max);
|
| 573 | if ret.is_null() {
|
| 574 | return None;
|
| 575 | }
|
| 576 |
|
| 577 | |
| 578 | |
| 579 | |
| 580 | Some(unsafe { T::borrow_mut(ret) })
|
| 581 | }
|
| 582 | }
|
| 583 |
|
| 584 | |
| 585 | pub struct InsertError<T> {
|
| 586 | |
| 587 | pub value: T,
|
| 588 | |
| 589 | pub cause: InsertErrorKind,
|
| 590 | }
|
| 591 |
|
| 592 | |
| 593 | #[derive(PartialEq, Eq, Copy, Clone, Debug)]
|
| 594 | pub enum InsertErrorKind {
|
| 595 | |
| 596 | Occupied,
|
| 597 | |
| 598 | AllocError(kernel::alloc::AllocError),
|
| 599 | |
| 600 | InvalidRequest,
|
| 601 | }
|
| 602 |
|
| 603 | impl From<InsertErrorKind> for Error {
|
| 604 | #[inline]
|
| 605 | fn from(kind: InsertErrorKind) -> Error {
|
| 606 | match kind {
|
| 607 | InsertErrorKind::Occupied => EEXIST,
|
| 608 | InsertErrorKind::AllocError(kernel::alloc::AllocError) => ENOMEM,
|
| 609 | InsertErrorKind::InvalidRequest => EINVAL,
|
| 610 | }
|
| 611 | }
|
| 612 | }
|
| 613 |
|
| 614 | impl<T> From<InsertError<T>> for Error {
|
| 615 | #[inline]
|
| 616 | fn from(insert_err: InsertError<T>) -> Error {
|
| 617 | Error::from(insert_err.cause)
|
| 618 | }
|
| 619 | }
|
| 620 |
|
| 621 | |
| 622 | pub struct AllocError<T> {
|
| 623 | |
| 624 | pub value: T,
|
| 625 | |
| 626 | pub cause: AllocErrorKind,
|
| 627 | }
|
| 628 |
|
| 629 | |
| 630 | #[derive(PartialEq, Eq, Copy, Clone)]
|
| 631 | pub enum AllocErrorKind {
|
| 632 | |
| 633 | Busy,
|
| 634 | |
| 635 | AllocError(kernel::alloc::AllocError),
|
| 636 | |
| 637 | InvalidRequest,
|
| 638 | }
|
| 639 |
|
| 640 | impl From<AllocErrorKind> for Error {
|
| 641 | #[inline]
|
| 642 | fn from(kind: AllocErrorKind) -> Error {
|
| 643 | match kind {
|
| 644 | AllocErrorKind::Busy => EBUSY,
|
| 645 | AllocErrorKind::AllocError(kernel::alloc::AllocError) => ENOMEM,
|
| 646 | AllocErrorKind::InvalidRequest => EINVAL,
|
| 647 | }
|
| 648 | }
|
| 649 | }
|
| 650 |
|
| 651 | impl<T> From<AllocError<T>> for Error {
|
| 652 | #[inline]
|
| 653 | fn from(insert_err: AllocError<T>) -> Error {
|
| 654 | Error::from(insert_err.cause)
|
| 655 | }
|
| 656 | }
|