[PATCH v15 9/9] rust: page: add `from_raw()`

Alice Ryhl aliceryhl at google.com
Fri Feb 20 10:49:06 UTC 2026


On Fri, Feb 20, 2026 at 10:52 AM Andreas Hindborg <a.hindborg at kernel.org> wrote:
>
> 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 | 11 +++++++++++
>  1 file changed, 11 insertions(+)
>
> diff --git a/rust/kernel/page.rs b/rust/kernel/page.rs
> index 4591b7b01c3d2..803f3e3d76b22 100644
> --- a/rust/kernel/page.rs
> +++ b/rust/kernel/page.rs
> @@ -191,6 +191,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: *const bindings::page) -> &'a Self {
> +        // SAFETY: By function safety requirements, ptr is not null and is
> +        // valid for use as a reference.
> +        unsafe { &*Opaque::cast_from(ptr).cast::<Self>() }

If you're going to do a pointer cast, then keep it simple and just do
&*ptr.cast().

Alice



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