1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 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 121 122 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 170 171 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 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 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 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 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 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 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 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647
// SPDX-License-Identifier: GPL-2.0
//! Maple trees.
//!
//! C header: [`include/linux/maple_tree.h`](srctree/include/linux/maple_tree.h)
//!
//! Reference: <https://docs.kernel.org/core-api/maple_tree.html>
use core::{
marker::PhantomData,
ops::{Bound, RangeBounds},
ptr,
};
use kernel::{
alloc::Flags,
error::to_result,
prelude::*,
types::{ForeignOwnable, Opaque},
};
/// A maple tree optimized for storing non-overlapping ranges.
///
/// # Invariants
///
/// Each range in the maple tree owns an instance of `T`.
#[pin_data(PinnedDrop)]
#[repr(transparent)]
pub struct MapleTree<T: ForeignOwnable> {
#[pin]
tree: Opaque<bindings::maple_tree>,
_p: PhantomData<T>,
}
/// A maple tree with `MT_FLAGS_ALLOC_RANGE` set.
///
/// All methods on [`MapleTree`] are also accessible on this type.
#[pin_data]
#[repr(transparent)]
pub struct MapleTreeAlloc<T: ForeignOwnable> {
#[pin]
tree: MapleTree<T>,
}
// Make MapleTree methods usable on MapleTreeAlloc.
impl<T: ForeignOwnable> core::ops::Deref for MapleTreeAlloc<T> {
type Target = MapleTree<T>;
#[inline]
fn deref(&self) -> &MapleTree<T> {
&self.tree
}
}
#[inline]
fn to_maple_range(range: impl RangeBounds<usize>) -> Option<(usize, usize)> {
let first = match range.start_bound() {
Bound::Included(start) => *start,
Bound::Excluded(start) => start.checked_add(1)?,
Bound::Unbounded => 0,
};
let last = match range.end_bound() {
Bound::Included(end) => *end,
Bound::Excluded(end) => end.checked_sub(1)?,
Bound::Unbounded => usize::MAX,
};
if last < first {
return None;
}
Some((first, last))
}
impl<T: ForeignOwnable> MapleTree<T> {
/// Create a new maple tree.
///
/// The tree will use the regular implementation with a higher branching factor, rather than
/// the allocation tree.
#[inline]
pub fn new() -> impl PinInit<Self> {
pin_init!(MapleTree {
// SAFETY: This initializes a maple tree into a pinned slot. The maple tree will be
// destroyed in Drop before the memory location becomes invalid.
tree <- Opaque::ffi_init(|slot| unsafe { bindings::mt_init_flags(slot, 0) }),
_p: PhantomData,
})
}
/// Insert the value at the given index.
///
/// # Errors
///
/// If the maple tree already contains a range using the given index, then this call will
/// return an [`InsertErrorKind::Occupied`]. It may also fail if memory allocation fails.
///
/// # Examples
///
/// ```
/// use kernel::maple_tree::{InsertErrorKind, MapleTree};
///
/// let tree = KBox::pin_init(MapleTree::<KBox<i32>>::new(), GFP_KERNEL)?;
///
/// let ten = KBox::new(10, GFP_KERNEL)?;
/// let twenty = KBox::new(20, GFP_KERNEL)?;
/// let the_answer = KBox::new(42, GFP_KERNEL)?;
///
/// // These calls will succeed.
/// tree.insert(100, ten, GFP_KERNEL)?;
/// tree.insert(101, twenty, GFP_KERNEL)?;
///
/// // This will fail because the index is already in use.
/// assert_eq!(
/// tree.insert(100, the_answer, GFP_KERNEL).unwrap_err().cause,
/// InsertErrorKind::Occupied,
/// );
/// # Ok::<_, Error>(())
/// ```
#[inline]
pub fn insert(&self, index: usize, value: T, gfp: Flags) -> Result<(), InsertError<T>> {
self.insert_range(index..=index, value, gfp)
}
/// Insert a value to the specified range, failing on overlap.
///
/// This accepts the usual types of Rust ranges using the `..` and `..=` syntax for exclusive
/// and inclusive ranges respectively. The range must not be empty, and must not overlap with
/// any existing range.
///
/// # Errors
///
/// If the maple tree already contains an overlapping range, then this call will return an
/// [`InsertErrorKind::Occupied`]. It may also fail if memory allocation fails or if the
/// requested range is invalid (e.g. empty).
///
/// # Examples
///
/// ```
/// use kernel::maple_tree::{InsertErrorKind, MapleTree};
///
/// let tree = KBox::pin_init(MapleTree::<KBox<i32>>::new(), GFP_KERNEL)?;
///
/// let ten = KBox::new(10, GFP_KERNEL)?;
/// let twenty = KBox::new(20, GFP_KERNEL)?;
/// let the_answer = KBox::new(42, GFP_KERNEL)?;
/// let hundred = KBox::new(100, GFP_KERNEL)?;
///
/// // Insert the value 10 at the indices 100 to 499.
/// tree.insert_range(100..500, ten, GFP_KERNEL)?;
///
/// // Insert the value 20 at the indices 500 to 1000.
/// tree.insert_range(500..=1000, twenty, GFP_KERNEL)?;
///
/// // This will fail due to overlap with the previous range on index 1000.
/// assert_eq!(
/// tree.insert_range(1000..1200, the_answer, GFP_KERNEL).unwrap_err().cause,
/// InsertErrorKind::Occupied,
/// );
///
/// // When using .. to specify the range, you must be careful to ensure that the range is
/// // non-empty.
/// assert_eq!(
/// tree.insert_range(72..72, hundred, GFP_KERNEL).unwrap_err().cause,
/// InsertErrorKind::InvalidRequest,
/// );
/// # Ok::<_, Error>(())
/// ```
pub fn insert_range<R>(&self, range: R, value: T, gfp: Flags) -> Result<(), InsertError<T>>
where
R: RangeBounds<usize>,
{
let Some((first, last)) = to_maple_range(range) else {
return Err(InsertError {
value,
cause: InsertErrorKind::InvalidRequest,
});
};
let ptr = T::into_foreign(value);
// SAFETY: The tree is valid, and we are passing a pointer to an owned instance of `T`.
let res = to_result(unsafe {
bindings::mtree_insert_range(self.tree.get(), first, last, ptr, gfp.as_raw())
});
if let Err(err) = res {
// SAFETY: As `mtree_insert_range` failed, it is safe to take back ownership.
let value = unsafe { T::from_foreign(ptr) };
let cause = if err == ENOMEM {
InsertErrorKind::AllocError(kernel::alloc::AllocError)
} else if err == EEXIST {
InsertErrorKind::Occupied
} else {
InsertErrorKind::InvalidRequest
};
Err(InsertError { value, cause })
} else {
Ok(())
}
}
/// Erase the range containing the given index.
///
/// # Examples
///
/// ```
/// use kernel::maple_tree::MapleTree;
///
/// let tree = KBox::pin_init(MapleTree::<KBox<i32>>::new(), GFP_KERNEL)?;
///
/// let ten = KBox::new(10, GFP_KERNEL)?;
/// let twenty = KBox::new(20, GFP_KERNEL)?;
///
/// tree.insert_range(100..500, ten, GFP_KERNEL)?;
/// tree.insert(67, twenty, GFP_KERNEL)?;
///
/// assert_eq!(tree.erase(67).map(|v| *v), Some(20));
/// assert_eq!(tree.erase(275).map(|v| *v), Some(10));
///
/// // The previous call erased the entire range, not just index 275.
/// assert!(tree.erase(127).is_none());
/// # Ok::<_, Error>(())
/// ```
#[inline]
pub fn erase(&self, index: usize) -> Option<T> {
// SAFETY: `self.tree` contains a valid maple tree.
let ret = unsafe { bindings::mtree_erase(self.tree.get(), index) };
// SAFETY: If the pointer is not null, then we took ownership of a valid instance of `T`
// from the tree.
unsafe { T::try_from_foreign(ret) }
}
/// Lock the internal spinlock.
#[inline]
pub fn lock(&self) -> MapleGuard<'_, T> {
// SAFETY: It's safe to lock the spinlock in a maple tree.
unsafe { bindings::spin_lock(self.ma_lock()) };
// INVARIANT: We just took the spinlock.
MapleGuard(self)
}
#[inline]
fn ma_lock(&self) -> *mut bindings::spinlock_t {
// SAFETY: This pointer offset operation stays in-bounds.
let lock_ptr = unsafe { &raw mut (*self.tree.get()).__bindgen_anon_1.ma_lock };
lock_ptr.cast()
}
/// Free all `T` instances in this tree.
///
/// # Safety
///
/// This frees Rust data referenced by the maple tree without removing it from the maple tree,
/// leaving it in an invalid state. The caller must ensure that this invalid state cannot be
/// observed by the end-user.
unsafe fn free_all_entries(self: Pin<&mut Self>) {
// SAFETY: The caller provides exclusive access to the entire maple tree, so we have
// exclusive access to the entire maple tree despite not holding the lock.
let mut ma_state = unsafe { MaState::new_raw(self.into_ref().get_ref(), 0, usize::MAX) };
loop {
// This uses the raw accessor because we're destroying pointers without removing them
// from the maple tree, which is only valid because this is the destructor.
let ptr = ma_state.mas_find_raw(usize::MAX);
if ptr.is_null() {
break;
}
// SAFETY: By the type invariants, this pointer references a valid value of type `T`.
// By the safety requirements, it is okay to free it without removing it from the maple
// tree.
drop(unsafe { T::from_foreign(ptr) });
}
}
}
#[pinned_drop]
impl<T: ForeignOwnable> PinnedDrop for MapleTree<T> {
#[inline]
fn drop(mut self: Pin<&mut Self>) {
// We only iterate the tree if the Rust value has a destructor.
if core::mem::needs_drop::<T>() {
// SAFETY: Other than the below `mtree_destroy` call, the tree will not be accessed
// after this call.
unsafe { self.as_mut().free_all_entries() };
}
// SAFETY: The tree is valid, and will not be accessed after this call.
unsafe { bindings::mtree_destroy(self.tree.get()) };
}
}
/// A reference to a [`MapleTree`] that owns the inner lock.
///
/// # Invariants
///
/// This guard owns the inner spinlock.
#[must_use = "if unused, the lock will be immediately unlocked"]
pub struct MapleGuard<'tree, T: ForeignOwnable>(&'tree MapleTree<T>);
impl<'tree, T: ForeignOwnable> Drop for MapleGuard<'tree, T> {
#[inline]
fn drop(&mut self) {
// SAFETY: By the type invariants, we hold this spinlock.
unsafe { bindings::spin_unlock(self.0.ma_lock()) };
}
}
impl<'tree, T: ForeignOwnable> MapleGuard<'tree, T> {
/// Create a [`MaState`] protected by this lock guard.
pub fn ma_state(&mut self, first: usize, end: usize) -> MaState<'_, T> {
// SAFETY: The `MaState` borrows this `MapleGuard`, so it can also borrow the `MapleGuard`s
// read/write permissions to the maple tree.
unsafe { MaState::new_raw(self.0, first, end) }
}
/// Load the value at the given index.
///
/// # Examples
///
/// Read the value while holding the spinlock.
///
/// ```
/// use kernel::maple_tree::MapleTree;
///
/// let tree = KBox::pin_init(MapleTree::<KBox<i32>>::new(), GFP_KERNEL)?;
///
/// let ten = KBox::new(10, GFP_KERNEL)?;
/// let twenty = KBox::new(20, GFP_KERNEL)?;
/// tree.insert(100, ten, GFP_KERNEL)?;
/// tree.insert(200, twenty, GFP_KERNEL)?;
///
/// let mut lock = tree.lock();
/// assert_eq!(lock.load(100).map(|v| *v), Some(10));
/// assert_eq!(lock.load(200).map(|v| *v), Some(20));
/// assert_eq!(lock.load(300).map(|v| *v), None);
/// # Ok::<_, Error>(())
/// ```
///
/// Increment refcount under the lock, to keep value alive afterwards.
///
/// ```
/// use kernel::maple_tree::MapleTree;
/// use kernel::sync::Arc;
///
/// let tree = KBox::pin_init(MapleTree::<Arc<i32>>::new(), GFP_KERNEL)?;
///
/// let ten = Arc::new(10, GFP_KERNEL)?;
/// let twenty = Arc::new(20, GFP_KERNEL)?;
/// tree.insert(100, ten, GFP_KERNEL)?;
/// tree.insert(200, twenty, GFP_KERNEL)?;
///
/// // Briefly take the lock to increment the refcount.
/// let value = tree.lock().load(100).map(Arc::from);
///
/// // At this point, another thread might remove the value.
/// tree.erase(100);
///
/// // But we can still access it because we took a refcount.
/// assert_eq!(value.map(|v| *v), Some(10));
/// # Ok::<_, Error>(())
/// ```
#[inline]
pub fn load(&mut self, index: usize) -> Option<T::BorrowedMut<'_>> {
// SAFETY: `self.tree` contains a valid maple tree.
let ret = unsafe { bindings::mtree_load(self.0.tree.get(), index) };
if ret.is_null() {
return None;
}
// SAFETY: If the pointer is not null, then it references a valid instance of `T`. It is
// safe to borrow the instance mutably because the signature of this function enforces that
// the mutable borrow is not used after the spinlock is dropped.
Some(unsafe { T::borrow_mut(ret) })
}
}
impl<T: ForeignOwnable> MapleTreeAlloc<T> {
/// Create a new allocation tree.
pub fn new() -> impl PinInit<Self> {
let tree = pin_init!(MapleTree {
// SAFETY: This initializes a maple tree into a pinned slot. The maple tree will be
// destroyed in Drop before the memory location becomes invalid.
tree <- Opaque::ffi_init(|slot| unsafe {
bindings::mt_init_flags(slot, bindings::MT_FLAGS_ALLOC_RANGE)
}),
_p: PhantomData,
});
pin_init!(MapleTreeAlloc { tree <- tree })
}
/// Insert an entry with the given size somewhere in the given range.
///
/// The maple tree will search for a location in the given range where there is space to insert
/// the new range. If there is not enough available space, then an error will be returned.
///
/// The index of the new range is returned.
///
/// # Examples
///
/// ```
/// use kernel::maple_tree::{MapleTreeAlloc, AllocErrorKind};
///
/// let tree = KBox::pin_init(MapleTreeAlloc::<KBox<i32>>::new(), GFP_KERNEL)?;
///
/// let ten = KBox::new(10, GFP_KERNEL)?;
/// let twenty = KBox::new(20, GFP_KERNEL)?;
/// let thirty = KBox::new(30, GFP_KERNEL)?;
/// let hundred = KBox::new(100, GFP_KERNEL)?;
///
/// // Allocate three ranges.
/// let idx1 = tree.alloc_range(100, ten, ..1000, GFP_KERNEL)?;
/// let idx2 = tree.alloc_range(100, twenty, ..1000, GFP_KERNEL)?;
/// let idx3 = tree.alloc_range(100, thirty, ..1000, GFP_KERNEL)?;
///
/// assert_eq!(idx1, 0);
/// assert_eq!(idx2, 100);
/// assert_eq!(idx3, 200);
///
/// // This will fail because the remaining space is too small.
/// assert_eq!(
/// tree.alloc_range(800, hundred, ..1000, GFP_KERNEL).unwrap_err().cause,
/// AllocErrorKind::Busy,
/// );
/// # Ok::<_, Error>(())
/// ```
pub fn alloc_range<R>(
&self,
size: usize,
value: T,
range: R,
gfp: Flags,
) -> Result<usize, AllocError<T>>
where
R: RangeBounds<usize>,
{
let Some((min, max)) = to_maple_range(range) else {
return Err(AllocError {
value,
cause: AllocErrorKind::InvalidRequest,
});
};
let ptr = T::into_foreign(value);
let mut index = 0;
// SAFETY: The tree is valid, and we are passing a pointer to an owned instance of `T`.
let res = to_result(unsafe {
bindings::mtree_alloc_range(
self.tree.tree.get(),
&mut index,
ptr,
size,
min,
max,
gfp.as_raw(),
)
});
if let Err(err) = res {
// SAFETY: As `mtree_alloc_range` failed, it is safe to take back ownership.
let value = unsafe { T::from_foreign(ptr) };
let cause = if err == ENOMEM {
AllocErrorKind::AllocError(kernel::alloc::AllocError)
} else if err == EBUSY {
AllocErrorKind::Busy
} else {
AllocErrorKind::InvalidRequest
};
Err(AllocError { value, cause })
} else {
Ok(index)
}
}
}
/// A helper type used for navigating a [`MapleTree`].
///
/// # Invariants
///
/// For the duration of `'tree`:
///
/// * The `ma_state` references a valid `MapleTree<T>`.
/// * The `ma_state` has read/write access to the tree.
pub struct MaState<'tree, T: ForeignOwnable> {
state: bindings::ma_state,
_phantom: PhantomData<&'tree mut MapleTree<T>>,
}
impl<'tree, T: ForeignOwnable> MaState<'tree, T> {
/// Initialize a new `MaState` with the given tree.
///
/// # Safety
///
/// The caller must ensure that this `MaState` has read/write access to the maple tree.
#[inline]
unsafe fn new_raw(mt: &'tree MapleTree<T>, first: usize, end: usize) -> Self {
// INVARIANT:
// * Having a reference ensures that the `MapleTree<T>` is valid for `'tree`.
// * The caller ensures that we have read/write access.
Self {
state: bindings::ma_state {
tree: mt.tree.get(),
index: first,
last: end,
node: ptr::null_mut(),
status: bindings::maple_status_ma_start,
min: 0,
max: usize::MAX,
alloc: ptr::null_mut(),
mas_flags: 0,
store_type: bindings::store_type_wr_invalid,
..Default::default()
},
_phantom: PhantomData,
}
}
#[inline]
fn as_raw(&mut self) -> *mut bindings::ma_state {
&raw mut self.state
}
#[inline]
fn mas_find_raw(&mut self, max: usize) -> *mut c_void {
// SAFETY: By the type invariants, the `ma_state` is active and we have read/write access
// to the tree.
unsafe { bindings::mas_find(self.as_raw(), max) }
}
/// Find the next entry in the maple tree.
///
/// # Examples
///
/// Iterate the maple tree.
///
/// ```
/// use kernel::maple_tree::MapleTree;
/// use kernel::sync::Arc;
///
/// let tree = KBox::pin_init(MapleTree::<Arc<i32>>::new(), GFP_KERNEL)?;
///
/// let ten = Arc::new(10, GFP_KERNEL)?;
/// let twenty = Arc::new(20, GFP_KERNEL)?;
/// tree.insert(100, ten, GFP_KERNEL)?;
/// tree.insert(200, twenty, GFP_KERNEL)?;
///
/// let mut ma_lock = tree.lock();
/// let mut iter = ma_lock.ma_state(0, usize::MAX);
///
/// assert_eq!(iter.find(usize::MAX).map(|v| *v), Some(10));
/// assert_eq!(iter.find(usize::MAX).map(|v| *v), Some(20));
/// assert!(iter.find(usize::MAX).is_none());
/// # Ok::<_, Error>(())
/// ```
#[inline]
pub fn find(&mut self, max: usize) -> Option<T::BorrowedMut<'_>> {
let ret = self.mas_find_raw(max);
if ret.is_null() {
return None;
}
// SAFETY: If the pointer is not null, then it references a valid instance of `T`. It's
// safe to access it mutably as the returned reference borrows this `MaState`, and the
// `MaState` has read/write access to the maple tree.
Some(unsafe { T::borrow_mut(ret) })
}
}
/// Error type for failure to insert a new value.
pub struct InsertError<T> {
/// The value that could not be inserted.
pub value: T,
/// The reason for the failure to insert.
pub cause: InsertErrorKind,
}
/// The reason for the failure to insert.
#[derive(PartialEq, Eq, Copy, Clone, Debug)]
pub enum InsertErrorKind {
/// There is already a value in the requested range.
Occupied,
/// Failure to allocate memory.
AllocError(kernel::alloc::AllocError),
/// The insertion request was invalid.
InvalidRequest,
}
impl From<InsertErrorKind> for Error {
#[inline]
fn from(kind: InsertErrorKind) -> Error {
match kind {
InsertErrorKind::Occupied => EEXIST,
InsertErrorKind::AllocError(kernel::alloc::AllocError) => ENOMEM,
InsertErrorKind::InvalidRequest => EINVAL,
}
}
}
impl<T> From<InsertError<T>> for Error {
#[inline]
fn from(insert_err: InsertError<T>) -> Error {
Error::from(insert_err.cause)
}
}
/// Error type for failure to insert a new value.
pub struct AllocError<T> {
/// The value that could not be inserted.
pub value: T,
/// The reason for the failure to insert.
pub cause: AllocErrorKind,
}
/// The reason for the failure to insert.
#[derive(PartialEq, Eq, Copy, Clone)]
pub enum AllocErrorKind {
/// There is not enough space for the requested allocation.
Busy,
/// Failure to allocate memory.
AllocError(kernel::alloc::AllocError),
/// The insertion request was invalid.
InvalidRequest,
}
impl From<AllocErrorKind> for Error {
#[inline]
fn from(kind: AllocErrorKind) -> Error {
match kind {
AllocErrorKind::Busy => EBUSY,
AllocErrorKind::AllocError(kernel::alloc::AllocError) => ENOMEM,
AllocErrorKind::InvalidRequest => EINVAL,
}
}
}
impl<T> From<AllocError<T>> for Error {
#[inline]
fn from(insert_err: AllocError<T>) -> Error {
Error::from(insert_err.cause)
}
}