All of lore.kernel.org
 help / color / mirror / Atom feed
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 v4 2/7] rust: alloc: vmalloc: implement Vmalloc::to_page()
Date: Thu, 14 Aug 2025 11:33:41 +0200	[thread overview]
Message-ID: <20250814093427.19629-3-dakr@kernel.org> (raw)
In-Reply-To: <20250814093427.19629-1-dakr@kernel.org>

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

Reviewed-by: Alexandre Courbot <acourbot@nvidia.com>
Reviewed-by: Alice Ryhl <aliceryhl@google.com>
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


  parent reply	other threads:[~2025-08-14  9:34 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 ` Danilo Krummrich [this message]
2025-08-19 12:46   ` [PATCH v4 2/7] rust: alloc: vmalloc: implement Vmalloc::to_page() 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
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=20250814093427.19629-3-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 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.