From: Alice Ryhl <aliceryhl@google.com>
To: Danilo Krummrich <dakr@kernel.org>
Cc: lorenzo.stoakes@oracle.com, vbabka@suse.cz,
Liam.Howlett@oracle.com, urezki@gmail.com, ojeda@kernel.org,
alex.gaynor@gmail.com, boqun.feng@gmail.com, gary@garyguo.net,
bjorn3_gh@protonmail.com, lossin@kernel.org,
a.hindborg@kernel.org, tmgross@umich.edu,
abdiel.janulgue@gmail.com, acourbot@nvidia.com,
rust-for-linux@vger.kernel.org
Subject: Re: [PATCH v4 3/7] rust: alloc: implement VmallocPageIter
Date: Tue, 19 Aug 2025 11:48:44 +0000 [thread overview]
Message-ID: <aKRknP8HT-BrPok4@google.com> (raw)
In-Reply-To: <20250814093427.19629-4-dakr@kernel.org>
On Thu, Aug 14, 2025 at 11:33:42AM +0200, Danilo Krummrich wrote:
> Introduce the VmallocPageIter type; an instance of VmallocPageIter may
> be exposed by owners of vmalloc allocations to provide borrowed access
> to the backing pages of the vmalloc allocation.
>
> For instance, this is useful to access and borrow the backing pages of
> allocation primitives, such as Box and Vec, backing a scatterlist.
>
> Suggested-by: Alice Ryhl <aliceryhl@google.com>
> Signed-off-by: Danilo Krummrich <dakr@kernel.org>
Some nits below, but:
Reviewed-by: Alice Ryhl <aliceryhl@google.com>
> rust/kernel/alloc/allocator.rs | 98 ++++++++++++++++++++++++++++++++++
> 1 file changed, 98 insertions(+)
>
> diff --git a/rust/kernel/alloc/allocator.rs b/rust/kernel/alloc/allocator.rs
> index 2315f5063011..5cff3069bff4 100644
> --- a/rust/kernel/alloc/allocator.rs
> +++ b/rust/kernel/alloc/allocator.rs
> @@ -10,6 +10,7 @@
>
> use super::Flags;
> use core::alloc::Layout;
> +use core::marker::PhantomData;
> use core::ptr;
> use core::ptr::NonNull;
>
> @@ -236,3 +237,100 @@ unsafe fn realloc(
> unsafe { ReallocFunc::KVREALLOC.call(ptr, layout, old_layout, flags) }
> }
> }
> +
> +/// An [`Iterator`] of [`page::BorrowedPage`] items owned by a [`Vmalloc`] allocation.
> +///
> +/// # Guarantees
> +///
> +/// The pages iterated by the [`Iterator`] appear in the order as they are mapped in the CPU's
> +/// virtual address space ascendingly.
> +///
> +/// # Invariants
> +///
> +/// - `buf` is a valid pointer to the beginning of a [`Vmalloc`] allocation.
> +/// - `size` is the size of the [`Vmalloc`] allocation `buf` points to.
> +pub struct VmallocPageIter<'a> {
> + /// The base address of the [`Vmalloc`] buffer.
> + buf: NonNull<u8>,
> + /// The size of the buffer pointed to by `buf` in bytes.
> + size: usize,
> + /// The current page index of the [`Iterator`].
> + index: usize,
> + _p: PhantomData<page::BorrowedPage<'a>>,
> +}
> +
> +impl<'a> Iterator for VmallocPageIter<'a> {
> + type Item = page::BorrowedPage<'a>;
> +
> + fn next(&mut self) -> Option<Self::Item> {
> + let offset = self.index.checked_mul(page::PAGE_SIZE)?;
> +
> + // Even though `self.size()` may be smaller than `Self::page_count() * page::PAGE_SIZE`, it
> + // is always a number between `(Self::page_count() - 1) * page::PAGE_SIZE` and
> + // `Self::page_count() * page::PAGE_SIZE`, hence the check below is sufficient.
> + if offset < self.size() {
> + self.index += 1;
> + } else {
> + return None;
> + }
> +
> + // TODO: Use `NonNull::add()` instead, once the minimum supported compiler version is
> + // bumped to 1.80 or later.
> + //
> + // SAFETY: `offset` is in the interval `[0, (self.page_count() - 1) * page::PAGE_SIZE]`,
> + // hence the resulting pointer is guaranteed to be within the same allocation.
> + let ptr = unsafe { self.buf.as_ptr().add(offset) };
> +
> + // SAFETY: `ptr` is guaranteed to be non-null given that it is derived from `self.buf`.
> + let ptr = unsafe { NonNull::new_unchecked(ptr) };
> +
> + // SAFETY:
> + // - `ptr` is a valid pointer to a `Vmalloc` allocation.
> + // - `ptr` is valid for the duration of `'a`.
> + Some(unsafe { Vmalloc::to_page(ptr) })
> + }
> +
> + fn size_hint(&self) -> (usize, Option<usize>) {
> + let remaining = self.page_count().saturating_sub(self.index);
> +
> + (remaining, Some(remaining))
> + }
> +}
> +
> +impl<'a> VmallocPageIter<'a> {
> + /// Creates a new [`VmallocPageIter`] instance.
> + ///
> + /// # Safety
> + ///
> + /// - `buf` must be a pointer to the beginning of a [`Vmalloc`] allocation.
It has to be the beginning of the vmalloc allocation?
> + /// - `buf` points to must at least be valid for the lifetime of `'a`.
This grammar doesn't quite compute for me.
> + /// - `size` must be the size of the [`Vmalloc`] allocation `buf` points to.
> + pub unsafe fn new(buf: NonNull<u8>, size: usize) -> Self {
> + // INVARIANT: `buf` is a valid pointer to the beginning of a `Vmalloc` allocation by the
> + // safety requirements of this function.
> + Self {
> + buf,
> + size,
> + index: 0,
> + _p: PhantomData,
> + }
> + }
> +
> + /// Returns the base address of the backing [`Vmalloc`] allocation.
> + pub fn base_address(&self) -> NonNull<u8> {
> + self.buf
> + }
> +
> + /// Returns the size of the backing [`Vmalloc`] allocation in bytes.
> + ///
> + /// Note that this is the size the [`Vmalloc`] allocation has been allocated with. Hence, this
> + /// number may be smaller than `[`Self::page_count`] * [`page::PAGE_SIZE`]`.
> + pub fn size(&self) -> usize {
How about #[inline] on all these?
> + self.size
> + }
> +
> + /// Returns the number of pages owned by the backing [`Vmalloc`] allocation.
> + pub fn page_count(&self) -> usize {
> + self.size().div_ceil(page::PAGE_SIZE)
> + }
> +}
> --
> 2.50.1
>
next prev parent reply other threads:[~2025-08-19 11:48 UTC|newest]
Thread overview: 28+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-08-14 9:33 [PATCH v4 0/7] BorrowedPage, AsPageIter and VmallocPageIter Danilo Krummrich
2025-08-14 9:33 ` [PATCH v4 1/7] rust: page: implement BorrowedPage Danilo Krummrich
2025-08-19 12:42 ` Daniel Almeida
2025-08-14 9:33 ` [PATCH v4 2/7] rust: alloc: vmalloc: implement Vmalloc::to_page() Danilo Krummrich
2025-08-19 12:46 ` Daniel Almeida
2025-08-14 9:33 ` [PATCH v4 3/7] rust: alloc: implement VmallocPageIter Danilo Krummrich
2025-08-18 12:02 ` Alexandre Courbot
2025-08-19 11:48 ` Alice Ryhl [this message]
2025-08-19 12:02 ` Danilo Krummrich
2025-08-19 12:58 ` Daniel Almeida
2025-08-14 9:33 ` [PATCH v4 4/7] rust: page: define trait AsPageIter Danilo Krummrich
2025-08-18 12:06 ` Alexandre Courbot
2025-08-18 12:07 ` Danilo Krummrich
2025-08-18 12:14 ` Alexandre Courbot
2025-08-18 12:18 ` Danilo Krummrich
2025-08-19 11:49 ` Alice Ryhl
2025-08-19 13:08 ` Daniel Almeida
2025-08-19 13:16 ` Danilo Krummrich
2025-08-14 9:33 ` [PATCH v4 5/7] rust: alloc: kbox: implement AsPageIter for VBox Danilo Krummrich
2025-08-18 12:07 ` Alexandre Courbot
2025-08-19 11:50 ` Alice Ryhl
2025-08-19 12:06 ` Danilo Krummrich
2025-08-14 9:33 ` [PATCH v4 6/7] rust: alloc: layout: implement ArrayLayout::size() Danilo Krummrich
2025-08-14 9:33 ` [PATCH v4 7/7] rust: alloc: kvec: implement AsPageIter for VVec Danilo Krummrich
2025-08-18 12:09 ` Alexandre Courbot
2025-08-19 11:51 ` Alice Ryhl
2025-08-19 13:11 ` Daniel Almeida
2025-08-18 12:11 ` [PATCH v4 0/7] BorrowedPage, AsPageIter and VmallocPageIter Alexandre Courbot
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=aKRknP8HT-BrPok4@google.com \
--to=aliceryhl@google.com \
--cc=Liam.Howlett@oracle.com \
--cc=a.hindborg@kernel.org \
--cc=abdiel.janulgue@gmail.com \
--cc=acourbot@nvidia.com \
--cc=alex.gaynor@gmail.com \
--cc=bjorn3_gh@protonmail.com \
--cc=boqun.feng@gmail.com \
--cc=dakr@kernel.org \
--cc=gary@garyguo.net \
--cc=lorenzo.stoakes@oracle.com \
--cc=lossin@kernel.org \
--cc=ojeda@kernel.org \
--cc=rust-for-linux@vger.kernel.org \
--cc=tmgross@umich.edu \
--cc=urezki@gmail.com \
--cc=vbabka@suse.cz \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.