[<prev] [next>] [day] [month] [year] [list]
Message-Id: <20251203-res-size-newtype-v1-1-22ed0b8a7a18@gmail.com>
Date: Wed, 03 Dec 2025 18:03:26 +0100
From: Moritz Zielke via B4 Relay <devnull+moritz.zielke.gmail.com@...nel.org>
To: Danilo Krummrich <dakr@...nel.org>, Alice Ryhl <aliceryhl@...gle.com>,
Daniel Almeida <daniel.almeida@...labora.com>,
Miguel Ojeda <ojeda@...nel.org>, Alex Gaynor <alex.gaynor@...il.com>,
Boqun Feng <boqun.feng@...il.com>, Gary Guo <gary@...yguo.net>,
Björn Roy Baron <bjorn3_gh@...tonmail.com>,
Benno Lossin <lossin@...nel.org>, Andreas Hindborg <a.hindborg@...nel.org>,
Trevor Gross <tmgross@...ch.edu>
Cc: rust-for-linux@...r.kernel.org, linux-kernel@...r.kernel.org,
Moritz Zielke <moritz.zielke@...il.com>
Subject: [PATCH] rust: io: convert ResourceSize to newtype
From: Moritz Zielke <moritz.zielke@...il.com>
Makes ResourceSize a newtype wrapper around the type for which it
previously was an alias. This should help prevent mistakes by
restricting what operations are possible with ResourceSize.
Suggested-by: Miguel Ojeda <ojeda@...nel.org>
Link: https://github.com/Rust-for-Linux/linux/issues/1203
Signed-off-by: Moritz Zielke <moritz.zielke@...il.com>
---
I think with [1] the prerequisites for making ResourceSize a newtype
have been applied to the driver-core-testing branch of driver-core.
So I developed this patch against driver-core-testing.
[1] https://lore.kernel.org/lkml/DE0C1KA14PDQ.Q2CJDDTQPWOK@kernel.org/
---
rust/kernel/io.rs | 38 ++++++++++++++++++++++++++++++++++++--
rust/kernel/io/resource.rs | 6 +++---
2 files changed, 39 insertions(+), 5 deletions(-)
diff --git a/rust/kernel/io.rs b/rust/kernel/io.rs
index 98e8b84e68d1..490f60680090 100644
--- a/rust/kernel/io.rs
+++ b/rust/kernel/io.rs
@@ -4,6 +4,8 @@
//!
//! C header: [`include/asm-generic/io.h`](srctree/include/asm-generic/io.h)
+use core::num::TryFromIntError;
+
use crate::{
bindings,
prelude::*, //
@@ -23,9 +25,41 @@
/// Resource Size type.
///
-/// This is a type alias to either `u32` or `u64` depending on the config option
+/// This is a transparent wrapper around 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 ResourceSize = bindings::resource_size_t;
+#[repr(transparent)]
+#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Default)]
+pub struct ResourceSize(bindings::phys_addr_t);
+
+impl From<ffi::c_uint> for ResourceSize {
+ #[inline]
+ fn from(value: ffi::c_uint) -> Self {
+ Self(value.into())
+ }
+}
+
+impl From<bindings::resource_size_t> for ResourceSize {
+ #[inline]
+ fn from(value: bindings::resource_size_t) -> Self {
+ Self(value.into())
+ }
+}
+
+impl TryFrom<ResourceSize> for usize {
+ type Error = TryFromIntError;
+
+ #[inline]
+ fn try_from(value: ResourceSize) -> Result<Self, Self::Error> {
+ usize::try_from(value.0)
+ }
+}
+
+impl From<ResourceSize> for bindings::resource_size_t {
+ #[inline]
+ fn from(value: ResourceSize) -> Self {
+ value.0
+ }
+}
/// Raw representation of an MMIO region.
///
diff --git a/rust/kernel/io/resource.rs b/rust/kernel/io/resource.rs
index 56cfde97ce87..841bb00b8418 100644
--- a/rust/kernel/io/resource.rs
+++ b/rust/kernel/io/resource.rs
@@ -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, size.into()) };
}
}
@@ -114,7 +114,7 @@ pub fn request_region(
bindings::__request_region(
self.0.get(),
start,
- size,
+ size.into(),
name.as_char_ptr(),
flags.0 as c_int,
)
@@ -130,7 +130,7 @@ pub fn request_region(
pub fn size(&self) -> ResourceSize {
let inner = self.0.get();
// SAFETY: Safe as per the invariants of `Resource`.
- unsafe { bindings::resource_size(inner) }
+ unsafe { bindings::resource_size(inner) }.into()
}
/// Returns the start address of the resource.
---
base-commit: 473b9f331718267815649cd93801da832200db71
change-id: 20251203-res-size-newtype-6fe140bc0038
Best regards,
--
Moritz Zielke <moritz.zielke@...il.com>
Powered by blists - more mailing lists