From: Danilo Krummrich <dakr@kernel.org>
To: 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, aliceryhl@google.com, tmgross@umich.edu,
abdiel.janulgue@gmail.com, acourbot@nvidia.com
Cc: rust-for-linux@vger.kernel.org, Danilo Krummrich <dakr@kernel.org>
Subject: [PATCH v2 5/6] rust: alloc: kbox: implement PageOwner for VBox
Date: Wed, 6 Aug 2025 22:50:14 +0200 [thread overview]
Message-ID: <20250806205044.85085-6-dakr@kernel.org> (raw)
In-Reply-To: <20250806205044.85085-1-dakr@kernel.org>
Implement the PageOwner trait for VBox; it allows to iterate and borrow
the backing pages of a VBox. This, for instance, is useful in
combination with VBox backing a scatterlist.
Signed-off-by: Danilo Krummrich <dakr@kernel.org>
---
rust/kernel/alloc/kbox.rs | 42 +++++++++++++++++++++++++++++++++++++++
1 file changed, 42 insertions(+)
diff --git a/rust/kernel/alloc/kbox.rs b/rust/kernel/alloc/kbox.rs
index 856d05aa60f1..bcca798d466b 100644
--- a/rust/kernel/alloc/kbox.rs
+++ b/rust/kernel/alloc/kbox.rs
@@ -18,6 +18,7 @@
use crate::ffi::c_void;
use crate::init::InPlaceInit;
+use crate::page;
use crate::types::ForeignOwnable;
use pin_init::{InPlaceWrite, Init, PinInit, ZeroableOption};
@@ -598,3 +599,44 @@ fn drop(&mut self) {
unsafe { A::free(self.0.cast(), layout) };
}
}
+
+/// # Examples
+///
+/// ```
+/// # use kernel::{page, prelude::*};
+/// use kernel::page::PageOwner;
+///
+/// let mut vbox = VBox::<[u8; page::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::PAGE_SIZE)? };
+/// # Ok::<(), Error>(())
+/// ```
+impl<T> page::PageOwner for VBox<T> {
+ fn page_iter<'a>(&'a mut self) -> impl Iterator<Item = page::BorrowedPage<'a>> {
+ (0..self.page_count()).map(|idx| {
+ let ptr = self.0.as_ptr().cast::<u8>();
+
+ // TODO: Use `NonNull::add()` instead, once the minimum supported compiler version is
+ // bumped to 1.80 or later.
+ //
+ // SAFETY: `idx` is in the interval `[0, self.page_count())`, hence the resulting
+ // pointer is guaranteed to be within the same allocation.
+ let ptr = unsafe { ptr.add(idx * page::PAGE_SIZE) };
+
+ // SAFETY: `ptr` is guaranteed to be non-null given that it is derived from `self.0`.
+ 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`.
+ unsafe { Vmalloc::to_page(ptr) }
+ })
+ }
+
+ fn page_count(&self) -> usize {
+ core::mem::size_of::<T>().div_ceil(page::PAGE_SIZE)
+ }
+}
--
2.50.1
next prev parent reply other threads:[~2025-08-06 20:51 UTC|newest]
Thread overview: 21+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-08-06 20:50 [PATCH v2 0/6] BorrowedPage and PageOwner Danilo Krummrich
2025-08-06 20:50 ` [PATCH v2 1/6] rust: page: implement BorrowedPage Danilo Krummrich
2025-08-06 20:50 ` [PATCH v2 2/6] rust: page: define PageOwner trait Danilo Krummrich
2025-08-08 3:32 ` Alexandre Courbot
2025-08-08 8:54 ` Alice Ryhl
2025-08-08 9:08 ` Danilo Krummrich
2025-08-06 20:50 ` [PATCH v2 3/6] rust: alloc: vmalloc: implement Vmalloc::to_page() Danilo Krummrich
2025-08-08 3:37 ` Alexandre Courbot
2025-08-06 20:50 ` [PATCH v2 4/6] rust: alloc: layout: implement ArrayLayout::size() Danilo Krummrich
2025-08-08 3:42 ` Alexandre Courbot
2025-08-08 8:45 ` Abdiel Janulgue
2025-08-08 9:37 ` Alice Ryhl
2025-08-06 20:50 ` Danilo Krummrich [this message]
2025-08-08 3:49 ` [PATCH v2 5/6] rust: alloc: kbox: implement PageOwner for VBox Alexandre Courbot
2025-08-08 8:44 ` Abdiel Janulgue
2025-08-06 20:50 ` [PATCH v2 6/6] rust: alloc: kvec: implement PageOwner for VVec Danilo Krummrich
2025-08-08 3:54 ` Alexandre Courbot
2025-08-08 8:44 ` Abdiel Janulgue
2025-08-07 9:12 ` [PATCH v2 0/6] BorrowedPage and PageOwner Vlastimil Babka
2025-08-07 9:46 ` Danilo Krummrich
2025-08-08 8:41 ` Abdiel Janulgue
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=20250806205044.85085-6-dakr@kernel.org \
--to=dakr@kernel.org \
--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=aliceryhl@google.com \
--cc=bjorn3_gh@protonmail.com \
--cc=boqun.feng@gmail.com \
--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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).