rust: io: convert `PhysAddr` type alias to newtype
Subsystems (4)
PATCH 1/1 rust: io: convert `PhysAddr` type alias to newtype
Makes PhysAddr a newtype wrapper around bindings::phys_addr_t. This restricts what operations are possible with physical address values, helping prevent mistakes such as mixing them with other integer quantities.
Arithmetic operations are intentionally not provided; none of the current users perform address arithmetic in Rust.
--- rust/kernel/devres.rs | 4 +-- rust/kernel/io.rs | 52 ++++++++++++++++++++++++++++++++---- rust/kernel/io/resource.rs | 6 ++--- rust/kernel/iommu/pgtable.rs | 2 +- 4 files changed, 53 insertions(+), 11 deletions(-)
rust/kernel/devres.rs +2 -2
@@ -72,10 +72,10 @@/// /// /// /// [`paddr`, `paddr` + `SIZE`) must be a valid MMIO region that is mappable into the CPUs /// /// virtual address space. -/// unsafe fn new(paddr: usize) -> Result<Self>{ +/// unsafe fn new(paddr: PhysAddr) -> Result<Self>{ /// // SAFETY: By the safety requirements of this function [`paddr`, `paddr` + `SIZE`) is /// // valid for `ioremap`. -/// let addr = unsafe { bindings::ioremap(paddr as PhysAddr, SIZE) }; +/// let addr = unsafe { bindings::ioremap(paddr.into(), SIZE) }; /// if addr.is_null() { /// return Err(ENOMEM); /// }
rust/kernel/io.rs +47 -5
79 lines not on this page · raw message
rust/kernel/io/resource.rs +3 -3
@@ -58,7 +58,7 @@ fn drop(&mut self) { };// SAFETY: Safe as per the invariant of `Region`. -unsafe {release_fn (start, size) }; +unsafe {release_fn (start.into (), size) }; } }
@@ -113,7 +113,7 @@ pub fn request_region(let region =unsafe { bindings::__request_region ( self.0 .get (), - start, + start.into (), size, name.as_char_ptr (), flags.0 as c_int,
@@ -137,7 +137,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 { (*inner).start } +unsafe { (*inner).start }.into () }/// Returns the name of the resource.
rust/kernel/iommu/pgtable.rs +1 -1
@@ -182,7 +182,7 @@ pub unsafe fn map_pages( (map_pages)( self.raw_ops (), iova, - paddr, + paddr.into (), pgsize, pgcount, prot as i32,
Signature · 1 line
2.43.0
Danilo Krummrich
maintainerHi Albab,
Quoted 11 lines · Albab Hasan
Makes
PhysAddra newtype wrapper aroundbindings::phys_addr_t. This restricts what operations are possible with physical address values, helping prevent mistakes such as mixing them with other integer quantities.Arithmetic operations are intentionally not provided; none of the current users perform address arithmetic in Rust.
Suggested-by Miguel Ojeda <[email protected]>Signed-off-by Albab Hasan <[email protected]>
Thanks for the patch; a similar patch [1] has already been submitted slightly before this one.
May I suggest picking up another issue and / or help reviewing the other patch in [1]?
Thanks,
Danilo
Thanks for pointing it out. I'm extremely sorry. i always seem to miss previously submitted patches.
I'll take on another issue and take a look at the other patch.
Thanks
Quoted 26 lines · Danilo Krummrich
Hi Albab,
Quoted 11 lines · Albab Hasan
Makes
PhysAddra newtype wrapper aroundbindings::phys_addr_t. This restricts what operations are possible with physical address values, helping prevent mistakes such as mixing them with other integer quantities.Arithmetic operations are intentionally not provided; none of the current users perform address arithmetic in Rust.
Suggested-by Miguel Ojeda <[email protected]>Signed-off-by Albab Hasan <[email protected]>Thanks for the patch; a similar patch [1] has already been submitted slightly before this one.
May I suggest picking up another issue and / or help reviewing the other patch in [1]?
Thanks,
Danilo
kernel test robot
build botkernel test robot noticed the following build errors:
Hi Albab,
kernel test robot noticed the following build errors:
[auto build test ERROR on driver-core/driver-core-testing]
[also build test ERROR on driver-core/driver-core-next driver-core/driver-core-linus rust/rust-next linus/master v7.1-rc3 next-20260508] [If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]
url: https://github.com/intel-lab-lkp/linux/commits/Albab-Hasan/rust-io-convert-PhysAddr-type-alias-to-newtype/20260515-155255 base: driver-core/driver-core-testing patch link: https://lore.kernel.org/r/20260504045714.59248-1-albabhasan276%40gmail.com patch subject: [PATCH] rust: io: convert `PhysAddr` type alias to newtype config: loongarch-randconfig-002-20260515 (https://download.01.org/0day-ci/archive/20260516/[email protected]/config) compiler: clang version 23.0.0git (https://github.com/llvm/llvm-project 5bac06718f502014fade905512f1d26d578a18f3) rustc: rustc 1.88.0 (6b00bc388 2025-06-23) reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20260516/[email protected]/reproduce)
If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/
All errors (new ones prefixed by >>):
>> error[E0308]: mismatched types
--> rust/kernel/io/mem.rs:257:43
|
257 | unsafe { bindings::ioremap_np(res_start, size) }
| -------------------- ^^^^^^^^^ expected `u64`, found `PhysAddr`
| |
| arguments to this function are incorrect
| note: function defined here
--> rust/bindings/bindings_helpers_generated.rs:1272:12
|
1272 | pub fn ioremap_np(offset: phys_addr_t, size: usize) -> *mut ffi::c_void;
| ^^^^^^^^^^
help: call Into::into on this expression to convert PhysAddr into u64 |
257 | unsafe { bindings::ioremap_np(res_start.into(), size) }
| +++++++
--
>> error[E0308]: mismatched types
--> rust/kernel/io/mem.rs:262:40
|
262 | unsafe { bindings::ioremap(res_start, size) }
| ----------------- ^^^^^^^^^ expected `u64`, found `PhysAddr`
| |
| arguments to this function are incorrect
| note: function defined here
--> rust/bindings/bindings_helpers_generated.rs:1268:12
|
1268 | pub fn ioremap(offset: phys_addr_t, size: usize) -> *mut ffi::c_void;
| ^^^^^^^
help: call Into::into on this expression to convert PhysAddr into u64 |
262 | unsafe { bindings::ioremap(res_start.into(), size) }
| +++++++
--
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki