Skip to contentSkip to conversation

rust: Convert PhysAddr type alias into newtype

PATCH In review Gent Binaku 2 participants 2 messages Nov 20, 2025

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 rust: Convert PhysAddr type alias into newtype

Convert the use of phys_addr_t in the Rust kernel bindings into a dedicated PhysAddr newtype to provide stronger type safety around physical addresses.

By wrapping phys_addr_t in a transparent Rust newtype, we avoid accidentally mixing physical addresses with virtual addresses or other integer-like values. This allows the API to clearly express intent at call sites and makes misuse harder.

The newtype provides:
- Explicit construction and extraction via from_raw() and as_raw().
- Arithmetic helpers (checked_add, wrapping_add, wrapping_sub) with well-defined wrapping behaviour and operators.
- Alignment helpers (align_down, align_up) for common patterns.
- Formatting implementations (Debug, Display, hex, octal, binary).

Existing Rust code that interacts with C helpers such as ioremap* and resource APIs is updated to use PhysAddr at the Rust boundary while still passing the underlying phys_addr_t to C via as_raw().

This addresses Rust-for-Linux issue #1204 by defining a dedicated PhysAddr type on top of phys_addr_t, constraining operations and avoiding confusion with other integer types.

rust/kernel/io/resource.rs +2 -2
index d0064724a01a..3c5a02d161d8 100644
@@ -8,10 +8,10 @@
 use core::ops::Deref;
 use core::ptr::NonNull;
 
+use crate::phys_addr::PhysAddr;
 use crate::prelude::*;
 use crate::str::{CStr, CString};
 use crate::types::Opaque;
-use crate::phys_addr::PhysAddr;
 
 pub use super::{
     ResourceSize, //
@@ -133,7 +133,7 @@ pub fn size(&self) -> ResourceSize {
     pub fn start(&self) -> PhysAddr {
         let inner = self.0.get();
         // SAFETY: Safe as per the invariants of `Resource`.
-        unsafe { PhysAddr((*inner).start) }
+        unsafe { PhysAddr::from_raw((*inner).start) }
     }
 
     /// Returns the name of the resource.
rust/kernel/lib.rs +1 -0
index 8c50c8147851..5402ab654a50 100644
@@ -115,6 +115,7 @@
 pub mod page;
 #[cfg(CONFIG_PCI)]
 pub mod pci;
+pub mod phys_addr;
 pub mod pid_namespace;
 pub mod platform;
 pub mod prelude;
rust/kernel/phys_addr.rs +109 -30

242 lines not on this page · raw message

---
 rust/kernel/io/resource.rs |   4 +-
 rust/kernel/lib.rs         |   1 +
 rust/kernel/phys_addr.rs   | 139 +++++++++++++++++++++++++++++--------
 3 files changed, 112 insertions(+), 32 deletions(-)
rust/kernel/io/resource.rs +2 -2
index d0064724a01a..3c5a02d161d8 100644
@@ -8,10 +8,10 @@
 use core::ops::Deref;
 use core::ptr::NonNull;
 
+use crate::phys_addr::PhysAddr;
 use crate::prelude::*;
 use crate::str::{CStr, CString};
 use crate::types::Opaque;
-use crate::phys_addr::PhysAddr;
 
 pub use super::{
     ResourceSize, //
@@ -133,7 +133,7 @@ pub fn size(&self) -> ResourceSize {
     pub fn start(&self) -> PhysAddr {
         let inner = self.0.get();
         // SAFETY: Safe as per the invariants of `Resource`.
-        unsafe { PhysAddr((*inner).start) }
+        unsafe { PhysAddr::from_raw((*inner).start) }
     }
 
     /// Returns the name of the resource.
rust/kernel/lib.rs +1 -0
index 8c50c8147851..5402ab654a50 100644
@@ -115,6 +115,7 @@
 pub mod page;
 #[cfg(CONFIG_PCI)]
 pub mod pci;
+pub mod phys_addr;
 pub mod pid_namespace;
 pub mod platform;
 pub mod prelude;
rust/kernel/phys_addr.rs +109 -30

242 lines not on this page · raw message

Signature · 1 line
2.52.0
#1 lore raw

i/rust/kernel/lib.rs

Quoted 2 lines · Gent Binaku
i/rust/kernel/lib.rs renamed from c/rust/kernel/lib.rs +0 -0

It seems the patch is mangled somehow: diffs appear twice, and the deltas for rust/kernel/phys_addr.rs seem to be w.r.t. some intermediate state (?).

Did you perhaps edit it manually?

I would suggest re-generating it with git format-patch and applying it to a clean tree with git am to double-check that it works well etc.

I hope that helps, and thanks for the patch!

Cheers,
Miguel