mirror of
https://github.com/raspberrypi/linux.git
synced 2025-12-06 01:49:46 +00:00
rust: alloc: kbox: implement AsPageIter for VBox
Implement AsPageIter for VBox; this allows to iterate and borrow the backing pages of a VBox. This, for instance, is useful in combination with VBox backing a scatterlist. Reviewed-by: Alice Ryhl <aliceryhl@google.com> Reviewed-by: Alexandre Courbot <acourbot@nvidia.com> Tested-by: Alexandre Courbot <acourbot@nvidia.com> Link: https://lore.kernel.org/r/20250820145434.94745-6-dakr@kernel.org Signed-off-by: Danilo Krummrich <dakr@kernel.org>
This commit is contained in:
@@ -3,7 +3,7 @@
|
||||
//! Implementation of [`Box`].
|
||||
|
||||
#[allow(unused_imports)] // Used in doc comments.
|
||||
use super::allocator::{KVmalloc, Kmalloc, Vmalloc};
|
||||
use super::allocator::{KVmalloc, Kmalloc, Vmalloc, VmallocPageIter};
|
||||
use super::{AllocError, Allocator, Flags};
|
||||
use core::alloc::Layout;
|
||||
use core::borrow::{Borrow, BorrowMut};
|
||||
@@ -18,6 +18,7 @@ use core::result::Result;
|
||||
|
||||
use crate::ffi::c_void;
|
||||
use crate::init::InPlaceInit;
|
||||
use crate::page::AsPageIter;
|
||||
use crate::types::ForeignOwnable;
|
||||
use pin_init::{InPlaceWrite, Init, PinInit, ZeroableOption};
|
||||
|
||||
@@ -598,3 +599,40 @@ where
|
||||
unsafe { A::free(self.0.cast(), layout) };
|
||||
}
|
||||
}
|
||||
|
||||
/// # Examples
|
||||
///
|
||||
/// ```
|
||||
/// # use kernel::prelude::*;
|
||||
/// use kernel::alloc::allocator::VmallocPageIter;
|
||||
/// use kernel::page::{AsPageIter, PAGE_SIZE};
|
||||
///
|
||||
/// let mut vbox = VBox::new((), GFP_KERNEL)?;
|
||||
///
|
||||
/// assert!(vbox.page_iter().next().is_none());
|
||||
///
|
||||
/// let mut vbox = VBox::<[u8; PAGE_SIZE]>::new_uninit(GFP_KERNEL)?;
|
||||
///
|
||||
/// let page = vbox.page_iter().next().expect("At least one page should be available.\n");
|
||||
///
|
||||
/// // SAFETY: There is no concurrent read or write to the same page.
|
||||
/// unsafe { page.fill_zero_raw(0, PAGE_SIZE)? };
|
||||
/// # Ok::<(), Error>(())
|
||||
/// ```
|
||||
impl<T> AsPageIter for VBox<T> {
|
||||
type Iter<'a>
|
||||
= VmallocPageIter<'a>
|
||||
where
|
||||
T: 'a;
|
||||
|
||||
fn page_iter(&mut self) -> Self::Iter<'_> {
|
||||
let ptr = self.0.cast();
|
||||
let size = core::mem::size_of::<T>();
|
||||
|
||||
// SAFETY:
|
||||
// - `ptr` is a valid pointer to the beginning of a `Vmalloc` allocation.
|
||||
// - `ptr` is guaranteed to be valid for the lifetime of `'a`.
|
||||
// - `size` is the size of the `Vmalloc` allocation `ptr` points to.
|
||||
unsafe { VmallocPageIter::new(ptr, size) }
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user