rust/kernel/page.rs | 11 +++++++++++ 1 file changed, 11 insertions(+)
Add a method to `Page` that allows construction of a reference instance
from `struct page` pointer.
Signed-off-by: Andreas Hindborg <a.hindborg@samsung.com>
---
Signed-off-by: Andreas Hindborg <a.hindborg@kernel.org>
---
rust/kernel/page.rs | 11 +++++++++++
1 file changed, 11 insertions(+)
diff --git a/rust/kernel/page.rs b/rust/kernel/page.rs
index 432fc0297d4a8..f666c92fcf451 100644
--- a/rust/kernel/page.rs
+++ b/rust/kernel/page.rs
@@ -176,6 +176,17 @@ pub fn nid(&self) -> i32 {
unsafe { bindings::page_to_nid(self.as_ptr()) }
}
+ /// Create a `&Page` from a raw `struct page` pointer
+ ///
+ /// # Safety
+ ///
+ /// `ptr` must be valid for use as a reference for the duration of `'a`.
+ pub unsafe fn from_raw<'a>(ptr: *mut bindings::page) -> &'a Self {
+ // SAFETY: By function safety requirements, ptr is not null and is
+ // valid for use as a reference.
+ unsafe { NonNull::new_unchecked(ptr).cast().as_ref() }
+ }
+
/// Runs a piece of code with this page mapped to an address.
///
/// The page is unmapped when this call returns.
---
base-commit: 63804fed149a6750ffd28610c5c1c98cce6bd377
change-id: 20260128-page-from-raw-e8e1f14e3f6d
Best regards,
--
Andreas Hindborg <a.hindborg@kernel.org>
On Wed Jan 28, 2026 at 1:22 PM GMT, Andreas Hindborg wrote:
> Add a method to `Page` that allows construction of a reference instance
> from `struct page` pointer.
>
> Signed-off-by: Andreas Hindborg <a.hindborg@samsung.com>
> ---
> Signed-off-by: Andreas Hindborg <a.hindborg@kernel.org>
> ---
> rust/kernel/page.rs | 11 +++++++++++
> 1 file changed, 11 insertions(+)
>
> diff --git a/rust/kernel/page.rs b/rust/kernel/page.rs
> index 432fc0297d4a8..f666c92fcf451 100644
> --- a/rust/kernel/page.rs
> +++ b/rust/kernel/page.rs
> @@ -176,6 +176,17 @@ pub fn nid(&self) -> i32 {
> unsafe { bindings::page_to_nid(self.as_ptr()) }
> }
>
> + /// Create a `&Page` from a raw `struct page` pointer
> + ///
> + /// # Safety
> + ///
> + /// `ptr` must be valid for use as a reference for the duration of `'a`.
> + pub unsafe fn from_raw<'a>(ptr: *mut bindings::page) -> &'a Self {
> + // SAFETY: By function safety requirements, ptr is not null and is
> + // valid for use as a reference.
> + unsafe { NonNull::new_unchecked(ptr).cast().as_ref() }
> + }
This is wrong. `Page` is a wrapper of `struct page*`, not `struct page`.
Best,
Gary
> +
> /// Runs a piece of code with this page mapped to an address.
> ///
> /// The page is unmapped when this call returns.
>
> ---
> base-commit: 63804fed149a6750ffd28610c5c1c98cce6bd377
> change-id: 20260128-page-from-raw-e8e1f14e3f6d
>
> Best regards,
"Gary Guo" <gary@garyguo.net> writes:
> On Wed Jan 28, 2026 at 1:22 PM GMT, Andreas Hindborg wrote:
>> Add a method to `Page` that allows construction of a reference instance
>> from `struct page` pointer.
>>
>> Signed-off-by: Andreas Hindborg <a.hindborg@samsung.com>
>> ---
>> Signed-off-by: Andreas Hindborg <a.hindborg@kernel.org>
>> ---
>> rust/kernel/page.rs | 11 +++++++++++
>> 1 file changed, 11 insertions(+)
>>
>> diff --git a/rust/kernel/page.rs b/rust/kernel/page.rs
>> index 432fc0297d4a8..f666c92fcf451 100644
>> --- a/rust/kernel/page.rs
>> +++ b/rust/kernel/page.rs
>> @@ -176,6 +176,17 @@ pub fn nid(&self) -> i32 {
>> unsafe { bindings::page_to_nid(self.as_ptr()) }
>> }
>>
>> + /// Create a `&Page` from a raw `struct page` pointer
>> + ///
>> + /// # Safety
>> + ///
>> + /// `ptr` must be valid for use as a reference for the duration of `'a`.
>> + pub unsafe fn from_raw<'a>(ptr: *mut bindings::page) -> &'a Self {
>> + // SAFETY: By function safety requirements, ptr is not null and is
>> + // valid for use as a reference.
>> + unsafe { NonNull::new_unchecked(ptr).cast().as_ref() }
>> + }
>
> This is wrong. `Page` is a wrapper of `struct page*`, not `struct page`.
Oops. Sorry. This goes on top of a conversion of page to `Ownable` that
I did not send yet.
Please disregard.
Best regards,
Andreas Hindborg
© 2016 - 2026 Red Hat, Inc.