rust: kernel: Introduce newtype for `PhysAddr`
Subsystems (3)
PATCH 1/1 rust: kernel: Introduce newtype for `PhysAddr`
This commit introduces a newtype for the PhysAddr type alias, replacing the previous to constrain the operations allowed on physical addresses and prevent mistakes. This change aligns the usage of PhysAddr with the goals of safety and clarity in the Rust-for-Linux code.
Updates include:
- Definition of the PhysAddr newtype in rust/kernel/phys_addr.rs
- Adjustments to relevant files (devres.rs, io.rs, io/mem.rs, io/resource.rs, lib.rs) to adopt the newtype.
- Addition of tests and examples to ensure coverage of the newtype and its methods.
This work builds upon the discussed patches linked at:
https://lore.kernel.org/rust-for-linux/CANiq72maV_j1uV=2nPGbTgRabnk8cpc7TNN_FQ+ou52OpZ=k6Q@mail.gmail.com/
--- rust/kernel/devres.rs | 5 +- rust/kernel/io.rs | 11 +- rust/kernel/io/mem.rs | 4 +- rust/kernel/io/resource.rs | 8 +- rust/kernel/lib.rs | 1 + rust/kernel/phys_addr.rs | 325 +++++++++++++++++++++++++++++++++++++ 6 files changed, 338 insertions(+), 16 deletions(-) create mode 100644 rust/kernel/phys_addr.rs
rust/kernel/devres.rs +3 -2
@@ -62,8 +62,8 @@ struct Inner<T: Send> {/// io::{ /// Io, /// IoRaw, -/// PhysAddr, /// }, +/// phys_addr::PhysAddr /// }; /// use core::ops::Deref; ///
@@ -78,7 +78,8 @@ struct Inner<T: Send> {/// unsafe fn new(paddr: usize) -> 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 physAddr = PhysAddr::from_raw(paddr as bindings::phys_addr_t); +/// let addr = unsafe { bindings::ioremap( physAddr.as_raw(), SIZE) }; /// if addr.is_null() { /// return Err(ENOMEM); /// }
rust/kernel/io.rs +3 -8
@@ -13,12 +13,6 @@pub use resource::Resource ; -/// Physical address type. -/// -/// This is a type alias to either `u32` or `u64` depending on the config option -/// `CONFIG_PHYS_ADDR_T_64BIT`, and it can be a u64 even on 32-bit architectures. -pub type PhysAddr = bindings::phys_addr_t ; -/// Resource Size type. /// /// This is a type alias to either `u32` or `u64` depending on the config option
@@ -80,8 +74,8 @@ pub fn maxsize(&self) -> usize {/// io::{ /// Io, /// IoRaw, -/// PhysAddr, /// }, +/// phys_addr::PhysAddr /// }; /// use core::ops::Deref; ///
@@ -96,7 +90,8 @@ pub fn maxsize(&self) -> usize {/// unsafe fn new(paddr: usize) -> 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 physAddr = PhysAddr::from_raw(paddr as bindings::phys_addr_t); +/// let addr = unsafe { bindings::ioremap( physAddr.as_raw(), SIZE) }; /// if addr.is_null() { /// return Err(ENOMEM); /// }
rust/kernel/io/mem.rs +2 -2
@@ -235,12 +235,12 @@ fn ioremap(resource: &Resource) -> Result<Self> {// SAFETY: // - `res_start` and `size` are read from a presumably valid `struct resource`. // - `size` is known not to be zero at this point. -unsafe { bindings::ioremap_np (res_start, size) } +unsafe { bindings::ioremap_np (res_start.into (), size) } }else {// SAFETY: // - `res_start` and `size` are read from a presumably valid `struct resource`. // - `size` is known not to be zero at this point. -unsafe { bindings::ioremap (res_start, size) } +unsafe { bindings::ioremap (res_start.into (), size) } };if addr.is_null () {
rust/kernel/io/resource.rs +4 -4
@@ -8,12 +8,12 @@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 ;pub use super ::{ -PhysAddr ,ResourceSize ,// };
@@ -54,7 +54,7 @@ fn drop(&mut self) { };// SAFETY: Safe as per the invariant of `Region`. -unsafe {release_fn (start, size) }; +unsafe {release_fn (start.into (), size) }; } }
@@ -109,7 +109,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,
@@ -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 { (*inner).start } +unsafe {PhysAddr ::from ((*inner).start) } }/// Returns the name of the resource.
rust/kernel/lib.rs +1 -0
@@ -116,6 +116,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 new file +325 -0
325 lines not on this page · raw message
Signature · 1 line
2.52.0