rust-for-linux.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v2 0/6] BorrowedPage and PageOwner
@ 2025-08-06 20:50 Danilo Krummrich
  2025-08-06 20:50 ` [PATCH v2 1/6] rust: page: implement BorrowedPage Danilo Krummrich
                   ` (7 more replies)
  0 siblings, 8 replies; 21+ messages in thread
From: Danilo Krummrich @ 2025-08-06 20:50 UTC (permalink / raw)
  To: lorenzo.stoakes, vbabka, Liam.Howlett, urezki, ojeda, alex.gaynor,
	boqun.feng, gary, bjorn3_gh, lossin, a.hindborg, aliceryhl,
	tmgross, abdiel.janulgue, acourbot
  Cc: rust-for-linux, Danilo Krummrich

This patch series implements the BorrowedPage type and the PageOwner trait.

The latter can be implemented by any entity that potentially owns one or
multiple pages and allow users to borrow them.

For instance, this is useful to access and borrow the backing pages of
allocation primitives, such as Box and Vec, backing a scatterlist.

Hence, implement PageOwner for VBox and VVec.

Additionally, implement Vmalloc::to_page() and ArrayLayout::size(), which are
dependencies of the above.

Changes in v2:
  - BorrowedPage
    - Add link to Ownable
    - Use borrow_page() in the example
  - Add PageOwner, Vmalloc::to_page(), ArrayLayout, VBox, VVec patches.

Danilo Krummrich (6):
  rust: page: implement BorrowedPage
  rust: page: define PageOwner trait
  rust: alloc: vmalloc: implement Vmalloc::to_page()
  rust: alloc: layout: implement ArrayLayout::size()
  rust: alloc: kbox: implement PageOwner for VBox
  rust: alloc: kvec: implement PageOwner for VVec

 rust/bindings/bindings_helper.h |  1 +
 rust/kernel/alloc/allocator.rs  | 49 +++++++++++++++++++
 rust/kernel/alloc/kbox.rs       | 42 ++++++++++++++++
 rust/kernel/alloc/kvec.rs       | 40 +++++++++++++++
 rust/kernel/alloc/layout.rs     |  5 ++
 rust/kernel/page.rs             | 87 ++++++++++++++++++++++++++++++++-
 6 files changed, 223 insertions(+), 1 deletion(-)


base-commit: d2eedaa3909be9102d648a4a0a50ccf64f96c54f
-- 
2.50.1


^ permalink raw reply	[flat|nested] 21+ messages in thread

* [PATCH v2 1/6] rust: page: implement BorrowedPage
  2025-08-06 20:50 [PATCH v2 0/6] BorrowedPage and PageOwner Danilo Krummrich
@ 2025-08-06 20:50 ` Danilo Krummrich
  2025-08-06 20:50 ` [PATCH v2 2/6] rust: page: define PageOwner trait Danilo Krummrich
                   ` (6 subsequent siblings)
  7 siblings, 0 replies; 21+ messages in thread
From: Danilo Krummrich @ 2025-08-06 20:50 UTC (permalink / raw)
  To: lorenzo.stoakes, vbabka, Liam.Howlett, urezki, ojeda, alex.gaynor,
	boqun.feng, gary, bjorn3_gh, lossin, a.hindborg, aliceryhl,
	tmgross, abdiel.janulgue, acourbot
  Cc: rust-for-linux, Danilo Krummrich

Currently, a Page always owns the underlying struct page.

However, sometimes a struct page may be owned by some other entity, e.g.
a vmalloc allocation.

Hence, introduce BorrowedPage to support such cases, until the Ownable
solution [1] lands.

This is required by the scatterlist abstractions.

Reviewed-by: Alexandre Courbot <acourbot@nvidia.com>
Acked-by: Alice Ryhl <aliceryhl@google.com>
Link: https://lore.kernel.org/rust-for-linux/ZnCzLIly3DRK2eab@boqun-archlinux/ [1]
Signed-off-by: Danilo Krummrich <dakr@kernel.org>
---
 rust/bindings/bindings_helper.h |  1 +
 rust/kernel/page.rs             | 75 ++++++++++++++++++++++++++++++++-
 2 files changed, 75 insertions(+), 1 deletion(-)

diff --git a/rust/bindings/bindings_helper.h b/rust/bindings/bindings_helper.h
index 84d60635e8a9..0e140e07758b 100644
--- a/rust/bindings/bindings_helper.h
+++ b/rust/bindings/bindings_helper.h
@@ -57,6 +57,7 @@
 #include <linux/jiffies.h>
 #include <linux/jump_label.h>
 #include <linux/mdio.h>
+#include <linux/mm.h>
 #include <linux/miscdevice.h>
 #include <linux/of_device.h>
 #include <linux/pci.h>
diff --git a/rust/kernel/page.rs b/rust/kernel/page.rs
index 7c1b17246ed5..631718a6ad7d 100644
--- a/rust/kernel/page.rs
+++ b/rust/kernel/page.rs
@@ -9,7 +9,12 @@
     error::Result,
     uaccess::UserSliceReader,
 };
-use core::ptr::{self, NonNull};
+use core::{
+    marker::PhantomData,
+    mem::ManuallyDrop,
+    ops::Deref,
+    ptr::{self, NonNull},
+};
 
 /// A bitwise shift for the page size.
 pub const PAGE_SHIFT: usize = bindings::PAGE_SHIFT as usize;
@@ -30,6 +35,74 @@ pub const fn page_align(addr: usize) -> usize {
     (addr + (PAGE_SIZE - 1)) & PAGE_MASK
 }
 
+/// Representation of a non-owning reference to a [`Page`].
+///
+/// This type provides a borrowed version of a [`Page`] that is owned by some other entity, e.g. a
+/// [`Vmalloc`] allocation such as [`VBox`].
+///
+/// # Example
+///
+/// ```
+/// # use kernel::{bindings, prelude::*};
+/// use kernel::page::{BorrowedPage, Page, PAGE_SIZE};
+/// # use core::{mem::MaybeUninit, ptr, ptr::NonNull };
+///
+/// fn borrow_page<'a>(vbox: &'a mut VBox<MaybeUninit<[u8; PAGE_SIZE]>>) -> BorrowedPage<'a> {
+///     let ptr = ptr::from_ref(&**vbox);
+///
+///     // SAFETY: `ptr` is a valid pointer to `Vmalloc` memory.
+///     let page = unsafe { bindings::vmalloc_to_page(ptr.cast()) };
+///
+///     // SAFETY: `vmalloc_to_page` returns a valid pointer to a `struct page` for a valid
+///     // pointer to `Vmalloc` memory.
+///     let page = unsafe { NonNull::new_unchecked(page) };
+///
+///     // SAFETY:
+///     // - `self.0` is a valid pointer to a `struct page`.
+///     // - `self.0` is valid for the entire lifetime of `self`.
+///     unsafe { BorrowedPage::from_raw(page) }
+/// }
+///
+/// let mut vbox = VBox::<[u8; PAGE_SIZE]>::new_uninit(GFP_KERNEL)?;
+/// let page = borrow_page(&mut vbox);
+///
+/// // SAFETY: There is no concurrent read or write to this page.
+/// unsafe { page.fill_zero_raw(0, PAGE_SIZE)? };
+/// # Ok::<(), Error>(())
+/// ```
+///
+/// # Invariants
+///
+/// The borrowed underlying pointer to a `struct page` is valid for the entire lifetime `'a`.
+///
+/// [`VBox`]: kernel::alloc::VBox
+/// [`Vmalloc`]: kernel::alloc::allocator::Vmalloc
+pub struct BorrowedPage<'a>(ManuallyDrop<Page>, PhantomData<&'a Page>);
+
+impl<'a> BorrowedPage<'a> {
+    /// Constructs a [`BorrowedPage`] from a raw pointer to a `struct page`.
+    ///
+    /// # Safety
+    ///
+    /// - `ptr` must point to a valid `bindings::page`.
+    /// - `ptr` must remain valid for the entire lifetime `'a`.
+    pub unsafe fn from_raw(ptr: NonNull<bindings::page>) -> Self {
+        let page = Page { page: ptr };
+
+        // INVARIANT: The safety requirements guarantee that `ptr` is valid for the entire lifetime
+        // `'a`.
+        Self(ManuallyDrop::new(page), PhantomData)
+    }
+}
+
+impl<'a> Deref for BorrowedPage<'a> {
+    type Target = Page;
+
+    fn deref(&self) -> &Self::Target {
+        &self.0
+    }
+}
+
 /// A pointer to a page that owns the page allocation.
 ///
 /// # Invariants
-- 
2.50.1


^ permalink raw reply related	[flat|nested] 21+ messages in thread

* [PATCH v2 2/6] rust: page: define PageOwner trait
  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 ` Danilo Krummrich
  2025-08-08  3:32   ` Alexandre Courbot
  2025-08-08  8:54   ` Alice Ryhl
  2025-08-06 20:50 ` [PATCH v2 3/6] rust: alloc: vmalloc: implement Vmalloc::to_page() Danilo Krummrich
                   ` (5 subsequent siblings)
  7 siblings, 2 replies; 21+ messages in thread
From: Danilo Krummrich @ 2025-08-06 20:50 UTC (permalink / raw)
  To: lorenzo.stoakes, vbabka, Liam.Howlett, urezki, ojeda, alex.gaynor,
	boqun.feng, gary, bjorn3_gh, lossin, a.hindborg, aliceryhl,
	tmgross, abdiel.janulgue, acourbot
  Cc: rust-for-linux, Danilo Krummrich

Introduce the PageOwner trait, which can be implemented by any entity
that potentially owns one or multiple pages and allow users to borrow
them.

For instance, this is useful to access and borrow the backing pages of
allocation primitives, such as Box and Vec, backing a scatterlist.

Signed-off-by: Danilo Krummrich <dakr@kernel.org>
---
 rust/kernel/page.rs | 12 ++++++++++++
 1 file changed, 12 insertions(+)

diff --git a/rust/kernel/page.rs b/rust/kernel/page.rs
index 631718a6ad7d..93ce4956f782 100644
--- a/rust/kernel/page.rs
+++ b/rust/kernel/page.rs
@@ -103,6 +103,18 @@ fn deref(&self) -> &Self::Target {
     }
 }
 
+/// Represents a potential owner of one or multiple [`Page`]s.
+///
+/// This trait may be implemented by types that potentially hold ownership of memory pages. It
+/// allows users to iterate over those pages and borrow them as [`BorrowedPage`].
+pub trait PageOwner {
+    /// Returns an [`Iterator`] of [`BorrowedPage`] items over all pages owned by `self`.
+    fn page_iter<'a>(&'a mut self) -> impl Iterator<Item = BorrowedPage<'a>>;
+
+    /// Returns the number of pages currently owned by `self`.
+    fn page_count(&self) -> usize;
+}
+
 /// A pointer to a page that owns the page allocation.
 ///
 /// # Invariants
-- 
2.50.1


^ permalink raw reply related	[flat|nested] 21+ messages in thread

* [PATCH v2 3/6] rust: alloc: vmalloc: implement Vmalloc::to_page()
  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-06 20:50 ` 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
                   ` (4 subsequent siblings)
  7 siblings, 1 reply; 21+ messages in thread
From: Danilo Krummrich @ 2025-08-06 20:50 UTC (permalink / raw)
  To: lorenzo.stoakes, vbabka, Liam.Howlett, urezki, ojeda, alex.gaynor,
	boqun.feng, gary, bjorn3_gh, lossin, a.hindborg, aliceryhl,
	tmgross, abdiel.janulgue, acourbot
  Cc: rust-for-linux, Danilo Krummrich

Implement an abstraction of vmalloc_to_page() for subsequent use in the
PageOwner implementation of VBox and VVec.

Signed-off-by: Danilo Krummrich <dakr@kernel.org>
---
 rust/kernel/alloc/allocator.rs | 49 ++++++++++++++++++++++++++++++++++
 1 file changed, 49 insertions(+)

diff --git a/rust/kernel/alloc/allocator.rs b/rust/kernel/alloc/allocator.rs
index aa2dfa9dca4c..2315f5063011 100644
--- a/rust/kernel/alloc/allocator.rs
+++ b/rust/kernel/alloc/allocator.rs
@@ -15,6 +15,7 @@
 
 use crate::alloc::{AllocError, Allocator};
 use crate::bindings;
+use crate::page;
 use crate::pr_warn;
 
 /// The contiguous kernel allocator.
@@ -140,6 +141,54 @@ unsafe fn realloc(
     }
 }
 
+impl Vmalloc {
+    /// Convert a pointer to a [`Vmalloc`] allocation to a [`page::BorrowedPage`].
+    ///
+    /// # Examples
+    ///
+    /// ```
+    /// # use core::ptr::{NonNull, from_mut};
+    /// # use kernel::{page, prelude::*};
+    /// use kernel::alloc::allocator::Vmalloc;
+    ///
+    /// let mut vbox = VBox::<[u8; page::PAGE_SIZE]>::new_uninit(GFP_KERNEL)?;
+    ///
+    /// {
+    ///     // SAFETY: By the type invariant of `Box` the inner pointer of `vbox` is non-null.
+    ///     let ptr = unsafe { NonNull::new_unchecked(from_mut(&mut *vbox)) };
+    ///
+    ///     // SAFETY:
+    ///     // `ptr` is a valid pointer to a `Vmalloc` allocation.
+    ///     // `ptr` is valid for the entire lifetime of `page`.
+    ///     let page = unsafe { Vmalloc::to_page(ptr.cast()) };
+    ///
+    ///     // SAFETY: There is no concurrent read or write to the same page.
+    ///     unsafe { page.fill_zero_raw(0, page::PAGE_SIZE)? };
+    /// }
+    /// # Ok::<(), Error>(())
+    /// ```
+    ///
+    /// # Safety
+    ///
+    /// - `ptr` must be a valid pointer to a [`Vmalloc`] allocation.
+    /// - `ptr` must remain valid for the entire duration of `'a`.
+    pub unsafe fn to_page<'a>(ptr: NonNull<u8>) -> page::BorrowedPage<'a> {
+        // SAFETY: `ptr` is a valid pointer to `Vmalloc` memory.
+        let page = unsafe { bindings::vmalloc_to_page(ptr.as_ptr().cast()) };
+
+        // SAFETY: `vmalloc_to_page` returns a valid pointer to a `struct page` for a valid pointer
+        // to `Vmalloc` memory.
+        let page = unsafe { NonNull::new_unchecked(page) };
+
+        // SAFETY:
+        // - `page` is a valid pointer to a `struct page`, given that by the safety requirements of
+        //   this function `ptr` is a valid pointer to a `Vmalloc` allocation.
+        // - By the safety requirements of this function `ptr` is valid for the entire lifetime of
+        //   `'a`.
+        unsafe { page::BorrowedPage::from_raw(page) }
+    }
+}
+
 // SAFETY: `realloc` delegates to `ReallocFunc::call`, which guarantees that
 // - memory remains valid until it is explicitly freed,
 // - passing a pointer to a valid memory allocation is OK,
-- 
2.50.1


^ permalink raw reply related	[flat|nested] 21+ messages in thread

* [PATCH v2 4/6] rust: alloc: layout: implement ArrayLayout::size()
  2025-08-06 20:50 [PATCH v2 0/6] BorrowedPage and PageOwner Danilo Krummrich
                   ` (2 preceding siblings ...)
  2025-08-06 20:50 ` [PATCH v2 3/6] rust: alloc: vmalloc: implement Vmalloc::to_page() Danilo Krummrich
@ 2025-08-06 20:50 ` Danilo Krummrich
  2025-08-08  3:42   ` Alexandre Courbot
                     ` (2 more replies)
  2025-08-06 20:50 ` [PATCH v2 5/6] rust: alloc: kbox: implement PageOwner for VBox Danilo Krummrich
                   ` (3 subsequent siblings)
  7 siblings, 3 replies; 21+ messages in thread
From: Danilo Krummrich @ 2025-08-06 20:50 UTC (permalink / raw)
  To: lorenzo.stoakes, vbabka, Liam.Howlett, urezki, ojeda, alex.gaynor,
	boqun.feng, gary, bjorn3_gh, lossin, a.hindborg, aliceryhl,
	tmgross, abdiel.janulgue, acourbot
  Cc: rust-for-linux, Danilo Krummrich

Provide a convenience method for ArrayLayout to calculate the size of
the ArrayLayout in bytes.

Signed-off-by: Danilo Krummrich <dakr@kernel.org>
---
 rust/kernel/alloc/layout.rs | 5 +++++
 1 file changed, 5 insertions(+)

diff --git a/rust/kernel/alloc/layout.rs b/rust/kernel/alloc/layout.rs
index 93ed514f7cc7..666accb7859c 100644
--- a/rust/kernel/alloc/layout.rs
+++ b/rust/kernel/alloc/layout.rs
@@ -98,6 +98,11 @@ pub const fn len(&self) -> usize {
     pub const fn is_empty(&self) -> bool {
         self.len == 0
     }
+
+    /// Returns the size of the [`ArrayLayout`] in bytes.
+    pub const fn size(&self) -> usize {
+        self.len() * core::mem::size_of::<T>()
+    }
 }
 
 impl<T> From<ArrayLayout<T>> for Layout {
-- 
2.50.1


^ permalink raw reply related	[flat|nested] 21+ messages in thread

* [PATCH v2 5/6] rust: alloc: kbox: implement PageOwner for VBox
  2025-08-06 20:50 [PATCH v2 0/6] BorrowedPage and PageOwner Danilo Krummrich
                   ` (3 preceding siblings ...)
  2025-08-06 20:50 ` [PATCH v2 4/6] rust: alloc: layout: implement ArrayLayout::size() Danilo Krummrich
@ 2025-08-06 20:50 ` Danilo Krummrich
  2025-08-08  3:49   ` 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
                   ` (2 subsequent siblings)
  7 siblings, 2 replies; 21+ messages in thread
From: Danilo Krummrich @ 2025-08-06 20:50 UTC (permalink / raw)
  To: lorenzo.stoakes, vbabka, Liam.Howlett, urezki, ojeda, alex.gaynor,
	boqun.feng, gary, bjorn3_gh, lossin, a.hindborg, aliceryhl,
	tmgross, abdiel.janulgue, acourbot
  Cc: rust-for-linux, Danilo Krummrich

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


^ permalink raw reply related	[flat|nested] 21+ messages in thread

* [PATCH v2 6/6] rust: alloc: kvec: implement PageOwner for VVec
  2025-08-06 20:50 [PATCH v2 0/6] BorrowedPage and PageOwner Danilo Krummrich
                   ` (4 preceding siblings ...)
  2025-08-06 20:50 ` [PATCH v2 5/6] rust: alloc: kbox: implement PageOwner for VBox Danilo Krummrich
@ 2025-08-06 20:50 ` 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-08  8:41 ` Abdiel Janulgue
  7 siblings, 2 replies; 21+ messages in thread
From: Danilo Krummrich @ 2025-08-06 20:50 UTC (permalink / raw)
  To: lorenzo.stoakes, vbabka, Liam.Howlett, urezki, ojeda, alex.gaynor,
	boqun.feng, gary, bjorn3_gh, lossin, a.hindborg, aliceryhl,
	tmgross, abdiel.janulgue, acourbot
  Cc: rust-for-linux, Danilo Krummrich

Implement the PageOwner trait for VVec; it allows to iterate and borrow
the backing pages of a VVec. This, for instance, is useful in combination
with VBox backing a scatterlist.

Signed-off-by: Danilo Krummrich <dakr@kernel.org>
---
 rust/kernel/alloc/kvec.rs | 40 +++++++++++++++++++++++++++++++++++++++
 1 file changed, 40 insertions(+)

diff --git a/rust/kernel/alloc/kvec.rs b/rust/kernel/alloc/kvec.rs
index 3c72e0bdddb8..88ad30cfc427 100644
--- a/rust/kernel/alloc/kvec.rs
+++ b/rust/kernel/alloc/kvec.rs
@@ -21,6 +21,7 @@
     slice,
     slice::SliceIndex,
 };
+use kernel::page;
 
 mod errors;
 pub use self::errors::{InsertError, PushError, RemoveError};
@@ -1017,6 +1018,45 @@ fn into_iter(self) -> Self::IntoIter {
     }
 }
 
+/// # Examples
+///
+/// ```
+/// # use kernel::{page, prelude::*};
+/// use kernel::page::PageOwner;
+///
+/// let mut vec = VVec::<u8>::new();
+/// vec.reserve(page::PAGE_SIZE, GFP_KERNEL)?;
+///
+/// let page = vec.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 VVec<T> {
+    fn page_iter<'a>(&'a mut self) -> impl Iterator<Item = page::BorrowedPage<'a>> {
+        (0..self.page_count()).map(|idx| {
+            let ptr = self.as_mut_ptr().cast::<u8>();
+
+            // 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 {
+        self.layout.size().div_ceil(page::PAGE_SIZE)
+    }
+}
+
 /// An [`Iterator`] implementation for [`Vec`] that moves elements out of a vector.
 ///
 /// This structure is created by the [`Vec::into_iter`] method on [`Vec`] (provided by the
-- 
2.50.1


^ permalink raw reply related	[flat|nested] 21+ messages in thread

* Re: [PATCH v2 0/6] BorrowedPage and PageOwner
  2025-08-06 20:50 [PATCH v2 0/6] BorrowedPage and PageOwner Danilo Krummrich
                   ` (5 preceding siblings ...)
  2025-08-06 20:50 ` [PATCH v2 6/6] rust: alloc: kvec: implement PageOwner for VVec Danilo Krummrich
@ 2025-08-07  9:12 ` Vlastimil Babka
  2025-08-07  9:46   ` Danilo Krummrich
  2025-08-08  8:41 ` Abdiel Janulgue
  7 siblings, 1 reply; 21+ messages in thread
From: Vlastimil Babka @ 2025-08-07  9:12 UTC (permalink / raw)
  To: Danilo Krummrich, lorenzo.stoakes, Liam.Howlett, urezki, ojeda,
	alex.gaynor, boqun.feng, gary, bjorn3_gh, lossin, a.hindborg,
	aliceryhl, tmgross, abdiel.janulgue, acourbot, Oscar Salvador
  Cc: rust-for-linux

On 8/6/25 22:50, Danilo Krummrich wrote:
> This patch series implements the BorrowedPage type and the PageOwner trait.

Note that we have mm/page_owner.c already so there might be a bit of a name
clash and confuse someone. But maybe it's sufficiently distinct.

> The latter can be implemented by any entity that potentially owns one or
> multiple pages and allow users to borrow them.
> 
> For instance, this is useful to access and borrow the backing pages of
> allocation primitives, such as Box and Vec, backing a scatterlist.
> 
> Hence, implement PageOwner for VBox and VVec.
> 
> Additionally, implement Vmalloc::to_page() and ArrayLayout::size(), which are
> dependencies of the above.
> 
> Changes in v2:
>   - BorrowedPage
>     - Add link to Ownable
>     - Use borrow_page() in the example
>   - Add PageOwner, Vmalloc::to_page(), ArrayLayout, VBox, VVec patches.
> 
> Danilo Krummrich (6):
>   rust: page: implement BorrowedPage
>   rust: page: define PageOwner trait
>   rust: alloc: vmalloc: implement Vmalloc::to_page()
>   rust: alloc: layout: implement ArrayLayout::size()
>   rust: alloc: kbox: implement PageOwner for VBox
>   rust: alloc: kvec: implement PageOwner for VVec
> 
>  rust/bindings/bindings_helper.h |  1 +
>  rust/kernel/alloc/allocator.rs  | 49 +++++++++++++++++++
>  rust/kernel/alloc/kbox.rs       | 42 ++++++++++++++++
>  rust/kernel/alloc/kvec.rs       | 40 +++++++++++++++
>  rust/kernel/alloc/layout.rs     |  5 ++
>  rust/kernel/page.rs             | 87 ++++++++++++++++++++++++++++++++-
>  6 files changed, 223 insertions(+), 1 deletion(-)
> 
> 
> base-commit: d2eedaa3909be9102d648a4a0a50ccf64f96c54f


^ permalink raw reply	[flat|nested] 21+ messages in thread

* Re: [PATCH v2 0/6] BorrowedPage and PageOwner
  2025-08-07  9:12 ` [PATCH v2 0/6] BorrowedPage and PageOwner Vlastimil Babka
@ 2025-08-07  9:46   ` Danilo Krummrich
  0 siblings, 0 replies; 21+ messages in thread
From: Danilo Krummrich @ 2025-08-07  9:46 UTC (permalink / raw)
  To: Vlastimil Babka
  Cc: lorenzo.stoakes, Liam.Howlett, urezki, ojeda, alex.gaynor,
	boqun.feng, gary, bjorn3_gh, lossin, a.hindborg, aliceryhl,
	tmgross, abdiel.janulgue, acourbot, Oscar Salvador,
	rust-for-linux

On Thu Aug 7, 2025 at 11:12 AM CEST, Vlastimil Babka wrote:
> On 8/6/25 22:50, Danilo Krummrich wrote:
>> This patch series implements the BorrowedPage type and the PageOwner trait.
>
> Note that we have mm/page_owner.c already so there might be a bit of a name
> clash and confuse someone. But maybe it's sufficiently distinct.

Yeah, I'm aware of this debugging feature. But as you say, I also thought it's
sufficiently distinct; and given that it's just the name of a trait I don't
think it will lead to confusion for people interacting with the API.

^ permalink raw reply	[flat|nested] 21+ messages in thread

* Re: [PATCH v2 2/6] rust: page: define PageOwner trait
  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
  1 sibling, 0 replies; 21+ messages in thread
From: Alexandre Courbot @ 2025-08-08  3:32 UTC (permalink / raw)
  To: Danilo Krummrich, lorenzo.stoakes, vbabka, Liam.Howlett, urezki,
	ojeda, alex.gaynor, boqun.feng, gary, bjorn3_gh, lossin,
	a.hindborg, aliceryhl, tmgross, abdiel.janulgue
  Cc: rust-for-linux

On Thu Aug 7, 2025 at 5:50 AM JST, Danilo Krummrich wrote:
> Introduce the PageOwner trait, which can be implemented by any entity
> that potentially owns one or multiple pages and allow users to borrow
> them.
>
> For instance, this is useful to access and borrow the backing pages of
> allocation primitives, such as Box and Vec, backing a scatterlist.
>
> Signed-off-by: Danilo Krummrich <dakr@kernel.org>
> ---
>  rust/kernel/page.rs | 12 ++++++++++++
>  1 file changed, 12 insertions(+)
>
> diff --git a/rust/kernel/page.rs b/rust/kernel/page.rs
> index 631718a6ad7d..93ce4956f782 100644
> --- a/rust/kernel/page.rs
> +++ b/rust/kernel/page.rs
> @@ -103,6 +103,18 @@ fn deref(&self) -> &Self::Target {
>      }
>  }
>  
> +/// Represents a potential owner of one or multiple [`Page`]s.
> +///
> +/// This trait may be implemented by types that potentially hold ownership of memory pages. It

Why "potentially"? I'd expect types implement this to be certain about
their ownership of the memory they share. :)

> +/// allows users to iterate over those pages and borrow them as [`BorrowedPage`].
> +pub trait PageOwner {
> +    /// Returns an [`Iterator`] of [`BorrowedPage`] items over all pages owned by `self`.
> +    fn page_iter<'a>(&'a mut self) -> impl Iterator<Item = BorrowedPage<'a>>;
> +
> +    /// Returns the number of pages currently owned by `self`.
> +    fn page_count(&self) -> usize;

Should we add a mention that `page_iter` is guaranteed to yield
`page_count` pages? If this is part of the contract, should the trait be
made `unsafe` as this guarantee cannot be held by a safe trait?

If so, I am wondering whether we should also take the next step and make
`page_iter` return an `ExactSizeIterator` while requiring that its `len`
method is accurate. This would allow us to remove `page_count`
altogether.

^ permalink raw reply	[flat|nested] 21+ messages in thread

* Re: [PATCH v2 3/6] rust: alloc: vmalloc: implement Vmalloc::to_page()
  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
  0 siblings, 0 replies; 21+ messages in thread
From: Alexandre Courbot @ 2025-08-08  3:37 UTC (permalink / raw)
  To: Danilo Krummrich, lorenzo.stoakes, vbabka, Liam.Howlett, urezki,
	ojeda, alex.gaynor, boqun.feng, gary, bjorn3_gh, lossin,
	a.hindborg, aliceryhl, tmgross, abdiel.janulgue
  Cc: rust-for-linux

On Thu Aug 7, 2025 at 5:50 AM JST, Danilo Krummrich wrote:
> Implement an abstraction of vmalloc_to_page() for subsequent use in the
> PageOwner implementation of VBox and VVec.
>
> Signed-off-by: Danilo Krummrich <dakr@kernel.org>

Reviewed-by: Alexandre Courbot <acourbot@nvidia.com>

One minor nit about the example below.

> ---
>  rust/kernel/alloc/allocator.rs | 49 ++++++++++++++++++++++++++++++++++
>  1 file changed, 49 insertions(+)
>
> diff --git a/rust/kernel/alloc/allocator.rs b/rust/kernel/alloc/allocator.rs
> index aa2dfa9dca4c..2315f5063011 100644
> --- a/rust/kernel/alloc/allocator.rs
> +++ b/rust/kernel/alloc/allocator.rs
> @@ -15,6 +15,7 @@
>  
>  use crate::alloc::{AllocError, Allocator};
>  use crate::bindings;
> +use crate::page;
>  use crate::pr_warn;
>  
>  /// The contiguous kernel allocator.
> @@ -140,6 +141,54 @@ unsafe fn realloc(
>      }
>  }
>  
> +impl Vmalloc {
> +    /// Convert a pointer to a [`Vmalloc`] allocation to a [`page::BorrowedPage`].
> +    ///
> +    /// # Examples
> +    ///
> +    /// ```
> +    /// # use core::ptr::{NonNull, from_mut};

Maybe make these imports visible, as otherwise the reader might wonder
where `from_mut` comes from (I did :)).


^ permalink raw reply	[flat|nested] 21+ messages in thread

* Re: [PATCH v2 4/6] rust: alloc: layout: implement ArrayLayout::size()
  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
  2 siblings, 0 replies; 21+ messages in thread
From: Alexandre Courbot @ 2025-08-08  3:42 UTC (permalink / raw)
  To: Danilo Krummrich, lorenzo.stoakes, vbabka, Liam.Howlett, urezki,
	ojeda, alex.gaynor, boqun.feng, gary, bjorn3_gh, lossin,
	a.hindborg, aliceryhl, tmgross, abdiel.janulgue
  Cc: rust-for-linux

On Thu Aug 7, 2025 at 5:50 AM JST, Danilo Krummrich wrote:
> Provide a convenience method for ArrayLayout to calculate the size of
> the ArrayLayout in bytes.
>
> Signed-off-by: Danilo Krummrich <dakr@kernel.org>

Reviewed-by: Alexandre Courbot <acourbot@nvidia.com>

One question below for my own education.

> ---
>  rust/kernel/alloc/layout.rs | 5 +++++
>  1 file changed, 5 insertions(+)
>
> diff --git a/rust/kernel/alloc/layout.rs b/rust/kernel/alloc/layout.rs
> index 93ed514f7cc7..666accb7859c 100644
> --- a/rust/kernel/alloc/layout.rs
> +++ b/rust/kernel/alloc/layout.rs
> @@ -98,6 +98,11 @@ pub const fn len(&self) -> usize {
>      pub const fn is_empty(&self) -> bool {
>          self.len == 0
>      }
> +
> +    /// Returns the size of the [`ArrayLayout`] in bytes.
> +    pub const fn size(&self) -> usize {
> +        self.len() * core::mem::size_of::<T>()
> +    }

The type invariant stipulates that `len * size_of::<T>() <= isize::MAX`,
should we mention it here to highlight that this method cannot panic,
and/or use `wrapping_mul` to potentially simplify the generated code?


^ permalink raw reply	[flat|nested] 21+ messages in thread

* Re: [PATCH v2 5/6] rust: alloc: kbox: implement PageOwner for VBox
  2025-08-06 20:50 ` [PATCH v2 5/6] rust: alloc: kbox: implement PageOwner for VBox Danilo Krummrich
@ 2025-08-08  3:49   ` Alexandre Courbot
  2025-08-08  8:44   ` Abdiel Janulgue
  1 sibling, 0 replies; 21+ messages in thread
From: Alexandre Courbot @ 2025-08-08  3:49 UTC (permalink / raw)
  To: Danilo Krummrich, lorenzo.stoakes, vbabka, Liam.Howlett, urezki,
	ojeda, alex.gaynor, boqun.feng, gary, bjorn3_gh, lossin,
	a.hindborg, aliceryhl, tmgross, abdiel.janulgue
  Cc: rust-for-linux

On Thu Aug 7, 2025 at 5:50 AM JST, Danilo Krummrich wrote:
> 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>

Reviewed-by: Alexandre Courbot <acourbot@nvidia.com>


^ permalink raw reply	[flat|nested] 21+ messages in thread

* Re: [PATCH v2 6/6] rust: alloc: kvec: implement PageOwner for VVec
  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
  1 sibling, 0 replies; 21+ messages in thread
From: Alexandre Courbot @ 2025-08-08  3:54 UTC (permalink / raw)
  To: Danilo Krummrich, lorenzo.stoakes, vbabka, Liam.Howlett, urezki,
	ojeda, alex.gaynor, boqun.feng, gary, bjorn3_gh, lossin,
	a.hindborg, aliceryhl, tmgross, abdiel.janulgue
  Cc: rust-for-linux

On Thu Aug 7, 2025 at 5:50 AM JST, Danilo Krummrich wrote:
> Implement the PageOwner trait for VVec; it allows to iterate and borrow
> the backing pages of a VVec. This, for instance, is useful in combination
> with VBox backing a scatterlist.
>
> Signed-off-by: Danilo Krummrich <dakr@kernel.org>

Reviewed-by: Alexandre Courbot <acourbot@nvidia.com>


^ permalink raw reply	[flat|nested] 21+ messages in thread

* Re: [PATCH v2 0/6] BorrowedPage and PageOwner
  2025-08-06 20:50 [PATCH v2 0/6] BorrowedPage and PageOwner Danilo Krummrich
                   ` (6 preceding siblings ...)
  2025-08-07  9:12 ` [PATCH v2 0/6] BorrowedPage and PageOwner Vlastimil Babka
@ 2025-08-08  8:41 ` Abdiel Janulgue
  7 siblings, 0 replies; 21+ messages in thread
From: Abdiel Janulgue @ 2025-08-08  8:41 UTC (permalink / raw)
  To: Danilo Krummrich, lorenzo.stoakes, vbabka, Liam.Howlett, urezki,
	ojeda, alex.gaynor, boqun.feng, gary, bjorn3_gh, lossin,
	a.hindborg, aliceryhl, tmgross, acourbot
  Cc: rust-for-linux

Just shaped up the 4th revision of scatterlist on top of these patches 
and works great. I can confirm that it is now able to directly work with 
a VVec buffer, eliminating the need for a custom vmalloc_to_page() glue. 
Will send the next revision soon after some minor clean-ups.

Tested-by: Abdiel Janulgue <abdiel.janulgue@gmail.com>

On 06/08/2025 23:50, Danilo Krummrich wrote:
> This patch series implements the BorrowedPage type and the PageOwner trait.
> 
> The latter can be implemented by any entity that potentially owns one or
> multiple pages and allow users to borrow them.
> 
> For instance, this is useful to access and borrow the backing pages of
> allocation primitives, such as Box and Vec, backing a scatterlist.
> 
> Hence, implement PageOwner for VBox and VVec.
> 
> Additionally, implement Vmalloc::to_page() and ArrayLayout::size(), which are
> dependencies of the above.
> 
> Changes in v2:
>    - BorrowedPage
>      - Add link to Ownable
>      - Use borrow_page() in the example
>    - Add PageOwner, Vmalloc::to_page(), ArrayLayout, VBox, VVec patches.
> 
> Danilo Krummrich (6):
>    rust: page: implement BorrowedPage
>    rust: page: define PageOwner trait
>    rust: alloc: vmalloc: implement Vmalloc::to_page()
>    rust: alloc: layout: implement ArrayLayout::size()
>    rust: alloc: kbox: implement PageOwner for VBox
>    rust: alloc: kvec: implement PageOwner for VVec
> 
>   rust/bindings/bindings_helper.h |  1 +
>   rust/kernel/alloc/allocator.rs  | 49 +++++++++++++++++++
>   rust/kernel/alloc/kbox.rs       | 42 ++++++++++++++++
>   rust/kernel/alloc/kvec.rs       | 40 +++++++++++++++
>   rust/kernel/alloc/layout.rs     |  5 ++
>   rust/kernel/page.rs             | 87 ++++++++++++++++++++++++++++++++-
>   6 files changed, 223 insertions(+), 1 deletion(-)
> 
> 
> base-commit: d2eedaa3909be9102d648a4a0a50ccf64f96c54f


^ permalink raw reply	[flat|nested] 21+ messages in thread

* Re: [PATCH v2 6/6] rust: alloc: kvec: implement PageOwner for VVec
  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
  1 sibling, 0 replies; 21+ messages in thread
From: Abdiel Janulgue @ 2025-08-08  8:44 UTC (permalink / raw)
  To: Danilo Krummrich, lorenzo.stoakes, vbabka, Liam.Howlett, urezki,
	ojeda, alex.gaynor, boqun.feng, gary, bjorn3_gh, lossin,
	a.hindborg, aliceryhl, tmgross, acourbot
  Cc: rust-for-linux



On 06/08/2025 23:50, Danilo Krummrich wrote:
> Implement the PageOwner trait for VVec; it allows to iterate and borrow
> the backing pages of a VVec. This, for instance, is useful in combination
> with VBox backing a scatterlist.
> 
> Signed-off-by: Danilo Krummrich <dakr@kernel.org>
> ---

Reviewed-by: Abdiel Janulgue <abdiel.janulgue@gmail.com>


^ permalink raw reply	[flat|nested] 21+ messages in thread

* Re: [PATCH v2 5/6] rust: alloc: kbox: implement PageOwner for VBox
  2025-08-06 20:50 ` [PATCH v2 5/6] rust: alloc: kbox: implement PageOwner for VBox Danilo Krummrich
  2025-08-08  3:49   ` Alexandre Courbot
@ 2025-08-08  8:44   ` Abdiel Janulgue
  1 sibling, 0 replies; 21+ messages in thread
From: Abdiel Janulgue @ 2025-08-08  8:44 UTC (permalink / raw)
  To: Danilo Krummrich, lorenzo.stoakes, vbabka, Liam.Howlett, urezki,
	ojeda, alex.gaynor, boqun.feng, gary, bjorn3_gh, lossin,
	a.hindborg, aliceryhl, tmgross, acourbot
  Cc: rust-for-linux



On 06/08/2025 23:50, Danilo Krummrich wrote:
> 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>
> ---

Reviewed-by: Abdiel Janulgue <abdiel.janulgue@gmail.com>

^ permalink raw reply	[flat|nested] 21+ messages in thread

* Re: [PATCH v2 4/6] rust: alloc: layout: implement ArrayLayout::size()
  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
  2 siblings, 0 replies; 21+ messages in thread
From: Abdiel Janulgue @ 2025-08-08  8:45 UTC (permalink / raw)
  To: Danilo Krummrich, lorenzo.stoakes, vbabka, Liam.Howlett, urezki,
	ojeda, alex.gaynor, boqun.feng, gary, bjorn3_gh, lossin,
	a.hindborg, aliceryhl, tmgross, acourbot
  Cc: rust-for-linux



On 06/08/2025 23:50, Danilo Krummrich wrote:
> Provide a convenience method for ArrayLayout to calculate the size of
> the ArrayLayout in bytes.
> 
> Signed-off-by: Danilo Krummrich <dakr@kernel.org>
> ---
Reviewed-by: Abdiel Janulgue <abdiel.janulgue@gmail.com>

^ permalink raw reply	[flat|nested] 21+ messages in thread

* Re: [PATCH v2 2/6] rust: page: define PageOwner trait
  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
  1 sibling, 1 reply; 21+ messages in thread
From: Alice Ryhl @ 2025-08-08  8:54 UTC (permalink / raw)
  To: Danilo Krummrich
  Cc: lorenzo.stoakes, vbabka, Liam.Howlett, urezki, ojeda, alex.gaynor,
	boqun.feng, gary, bjorn3_gh, lossin, a.hindborg, tmgross,
	abdiel.janulgue, acourbot, rust-for-linux

On Wed, Aug 06, 2025 at 10:50:11PM +0200, Danilo Krummrich wrote:
> Introduce the PageOwner trait, which can be implemented by any entity
> that potentially owns one or multiple pages and allow users to borrow
> them.
> 
> For instance, this is useful to access and borrow the backing pages of
> allocation primitives, such as Box and Vec, backing a scatterlist.
> 
> Signed-off-by: Danilo Krummrich <dakr@kernel.org>
> ---
>  rust/kernel/page.rs | 12 ++++++++++++
>  1 file changed, 12 insertions(+)
> 
> diff --git a/rust/kernel/page.rs b/rust/kernel/page.rs
> index 631718a6ad7d..93ce4956f782 100644
> --- a/rust/kernel/page.rs
> +++ b/rust/kernel/page.rs
> @@ -103,6 +103,18 @@ fn deref(&self) -> &Self::Target {
>      }
>  }
>  
> +/// Represents a potential owner of one or multiple [`Page`]s.
> +///
> +/// This trait may be implemented by types that potentially hold ownership of memory pages. It
> +/// allows users to iterate over those pages and borrow them as [`BorrowedPage`].
> +pub trait PageOwner {
> +    /// Returns an [`Iterator`] of [`BorrowedPage`] items over all pages owned by `self`.
> +    fn page_iter<'a>(&'a mut self) -> impl Iterator<Item = BorrowedPage<'a>>;
> +
> +    /// Returns the number of pages currently owned by `self`.
> +    fn page_count(&self) -> usize;
> +}

Honestly, I don't entirely love this trait on its own merits. But I'd
need to know more about how it will be used to give a clearer opinion.

Either way, I think the implementation of VBox and VVec could be merged
to some shared VmallocPageIter that iterates a region of pages.

Alice

^ permalink raw reply	[flat|nested] 21+ messages in thread

* Re: [PATCH v2 2/6] rust: page: define PageOwner trait
  2025-08-08  8:54   ` Alice Ryhl
@ 2025-08-08  9:08     ` Danilo Krummrich
  0 siblings, 0 replies; 21+ messages in thread
From: Danilo Krummrich @ 2025-08-08  9:08 UTC (permalink / raw)
  To: Alice Ryhl
  Cc: lorenzo.stoakes, vbabka, Liam.Howlett, urezki, ojeda, alex.gaynor,
	boqun.feng, gary, bjorn3_gh, lossin, a.hindborg, tmgross,
	abdiel.janulgue, acourbot, rust-for-linux

On 8/8/25 10:54 AM, Alice Ryhl wrote:
> Either way, I think the implementation of VBox and VVec could be merged
> to some shared VmallocPageIter that iterates a region of pages.

I really like this idea. A VmallocPageIter type allows us to make more
assumptions about the pages, e.g. that they're (contiguously) mapped in the
virtual address space, which is useful as well.

I'm going to drop the PageOwner trait for now and provide a VmallocPageIter type
instead.

- Danilo

^ permalink raw reply	[flat|nested] 21+ messages in thread

* Re: [PATCH v2 4/6] rust: alloc: layout: implement ArrayLayout::size()
  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
  2 siblings, 0 replies; 21+ messages in thread
From: Alice Ryhl @ 2025-08-08  9:37 UTC (permalink / raw)
  To: Danilo Krummrich
  Cc: lorenzo.stoakes, vbabka, Liam.Howlett, urezki, ojeda, alex.gaynor,
	boqun.feng, gary, bjorn3_gh, lossin, a.hindborg, tmgross,
	abdiel.janulgue, acourbot, rust-for-linux

On Wed, Aug 6, 2025 at 10:51 PM Danilo Krummrich <dakr@kernel.org> wrote:
>
> Provide a convenience method for ArrayLayout to calculate the size of
> the ArrayLayout in bytes.
>
> Signed-off-by: Danilo Krummrich <dakr@kernel.org>

Reviewed-by: Alice Ryhl <aliceryhl@google.com>

> ---
>  rust/kernel/alloc/layout.rs | 5 +++++
>  1 file changed, 5 insertions(+)
>
> diff --git a/rust/kernel/alloc/layout.rs b/rust/kernel/alloc/layout.rs
> index 93ed514f7cc7..666accb7859c 100644
> --- a/rust/kernel/alloc/layout.rs
> +++ b/rust/kernel/alloc/layout.rs
> @@ -98,6 +98,11 @@ pub const fn len(&self) -> usize {
>      pub const fn is_empty(&self) -> bool {
>          self.len == 0
>      }
> +
> +    /// Returns the size of the [`ArrayLayout`] in bytes.
> +    pub const fn size(&self) -> usize {
> +        self.len() * core::mem::size_of::<T>()
> +    }

I believe Miguel added size_of to the prelude.

Alice

^ permalink raw reply	[flat|nested] 21+ messages in thread

end of thread, other threads:[~2025-08-08  9:38 UTC | newest]

Thread overview: 21+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
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 ` [PATCH v2 5/6] rust: alloc: kbox: implement PageOwner for VBox Danilo Krummrich
2025-08-08  3:49   ` 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

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).