Thread (21 messages) 21 messages, 5 authors, 2026-02-05
STALE170d
Revisions (4)
  1. v14 current
  2. v15 [diff vs current]
  3. v16 [diff vs current]
  4. v17 [diff vs current]

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

From: Andreas Hindborg <a.hindborg@kernel.org>
Date: 2026-02-04 11:58:41
Also in: dri-devel, linux-block, linux-fsdevel, linux-mm, linux-pci, linux-pm, lkml, rust-for-linux
Subsystem: memory management - rust, rust, the rest · Maintainers: Alice Ryhl, Miguel Ojeda, Linus Torvalds

Add a method to `Page` that allows construction of an instance from `struct
page` pointer.

Signed-off-by: Andreas Hindborg <redacted>
---
 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>() }
+    }
+
     /// Runs a piece of code with this page mapped to an address.
     ///
     /// The page is unmapped when this call returns.
-- 
2.51.2

Keyboard shortcuts
hback out one level
jnext message in thread
kprevious message in thread
ldrill in
Escclose help / fold thread tree
?toggle this help