[PATCH v4 2/3] rust: uaccess: add method to get UserPtr from u64
From: Deborah Brouwer
Date: Tue Oct 20 2026 - 20:07:38 EST
Currently a UserPtr can be created from a usize integer representing a
userspace address. To create a UserPtr from a u64 integer, the u64 must
be cast to usize. On 32-bit systems, this cast will discard the upper 32
bits of the address.
Provide a new method to create a UserPtr from a u64 integer. This avoids
the caller having to cast before creating a UserPtr and also clarifies
that the truncation is intended. This is equivalent to the C-side
macro u64_to_user_ptr() that provides typechecking and casting to a __user
pointer.
Signed-off-by: Deborah Brouwer <deborah.brouwer@xxxxxxxxxxxxx>
---
rust/kernel/uaccess.rs | 9 +++++++++
1 file changed, 9 insertions(+)
diff --git a/rust/kernel/uaccess.rs b/rust/kernel/uaccess.rs
index 8e18285ec44b..a6fd1af155b3 100644
--- a/rust/kernel/uaccess.rs
+++ b/rust/kernel/uaccess.rs
@@ -31,6 +31,15 @@ pub fn from_addr(addr: usize) -> Self {
Self(addr as *mut c_void)
}
+ /// Create a `UserPtr` from a u64 userspace address.
+ ///
+ /// Deliberately truncate upper bits for 32-bit systems.
+ #[doc(alias = "u64_to_user_ptr")]
+ #[inline]
+ pub fn from_u64(addr: u64) -> Self {
+ Self::from_addr(addr as usize)
+ }
+
/// Create a `UserPtr` from a pointer representing the userspace address.
#[inline]
pub fn from_ptr(addr: *mut c_void) -> Self {
--
2.56.0