[PATCH v17 10/10] rust: page: add `from_raw()`

Onur Özkan work at onurozkan.dev
Tue Jun 23 17:52:41 UTC 2026


On Thu, 04 Jun 2026 22:11:22 +0200
Andreas Hindborg <a.hindborg at kernel.org> wrote:

> From: Andreas Hindborg <a.hindborg at samsung.com>
> 
> Add a method to `Page` that allows construction of an instance from `struct
> page` pointer.
> 
> Signed-off-by: Andreas Hindborg <a.hindborg at samsung.com>
> ---
>  rust/kernel/page.rs | 12 ++++++++++++
>  1 file changed, 12 insertions(+)
> 
> diff --git a/rust/kernel/page.rs b/rust/kernel/page.rs
> index 844c75e54134..d56ae597f692 100644
> --- a/rust/kernel/page.rs
> +++ b/rust/kernel/page.rs
> @@ -214,6 +214,18 @@ 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 convertible to a shared reference with a lifetime of `'a`.
> +    #[inline]
> +    pub unsafe fn from_raw<'a>(ptr: *const bindings::page) -> &'a Self {
> +        // SAFETY: By function safety requirements, `ptr` is not null and is convertible to a shared
> +        // reference.
> +        unsafe { &*ptr.cast() }
> +    }
> +
>      /// Runs a piece of code with this page mapped to an address.
>      ///
>      /// The page is unmapped when this call returns.
> 
> -- 
> 2.51.2
> 
> 

Reviewed-by: Onur Özkan <work at onurozkan.dev>



More information about the Linux-security-module-archive mailing list