From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-pg1-f178.google.com (mail-pg1-f178.google.com [209.85.215.178]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 39CAF23ABA7 for ; Mon, 4 May 2026 04:58:05 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.215.178 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1777870687; cv=none; b=e/OeLwTR99AONzPAHxuF+1g+M/aZSN/jC9sP3HzFU+drazdhuD89DkaiI150eT7tTtncqsJE5QNN7//6MBEDTbq4/gW/NeM1LbAssf2nWtJL3YZg7/U9GnpYPKBh+qMrVnJVKCKxNhOjeTMMlDCAx9XcLJiQMP1VSnRFDbmnxPM= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1777870687; c=relaxed/simple; bh=PRXAEhg0v3BeQlsKrf8Gyufs2wJcCoRs3w2MjIzAHcM=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version; b=Km4vBRhE0McLi+uEa/0qqvwJx7q4bvUyh/VPUeqnyOUYHokXiGGVc5X6Ee2RYShXSwCHqsPFzphV+fknq+lbLDUXporiEa55AdRXIMVWkhX6wsOfU8WDwq69oeAOj1miLb1nBF4HB+CNgTL0cfpGI9DfDdbYMDH+BJDxGa41LW8= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=PdmE6ezy; arc=none smtp.client-ip=209.85.215.178 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="PdmE6ezy" Received: by mail-pg1-f178.google.com with SMTP id 41be03b00d2f7-c80148ae949so337130a12.2 for ; Sun, 03 May 2026 21:58:05 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1777870684; x=1778475484; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=LCVSMWc8XZyiIWDdmlHdj6gt0j+W5iLgLMvbf1xdc2c=; b=PdmE6ezyuhrelM2wE86/cdPEGVY0m/l9T4TnXZujd4+486TcEFauuJB92VkVIgab0D c2xUhXfXA8nW6VQU1jypdn37M0rz1rXcC3OGNnVQcdMa7+Zi+Fp+jifSn34AFyJ60gc7 TgwDHDUNYnnTssaqge/mYJAXcWxgUgOJBlgRBzim+RIvv+mM8i+acZJ1OKgVrsZ3VJpb EUwUCgD30SFFif8hblxLQeW4u9H+B9va4ItIAUTZhPLAn+AhI6RB5QrFnBjP+N7Ecn8V qa2nUWM3x9UYgDBNbnGa95oIKAfTNEqogCBhzgBrNI9VqPUhPU0ygVVCwsN5dXbgra2g unTQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1777870684; x=1778475484; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-gg:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=LCVSMWc8XZyiIWDdmlHdj6gt0j+W5iLgLMvbf1xdc2c=; b=OdIVCNyIl3D1HriDnr+2uX5+gULEbPHKqP8kYMOwUiY89KvG9O33cyCs6lfr7b/O1G 6gMX2DQeW2eeCGBsUkUcCRwr12RbHq/rANS55xKzo6/4QRrNj0+lnFDhbaTj5e5h6Uad iHCiJaWwPyJXLOK5rb39X8gpqMA6jIrC87qNkqAHLq1/m2vGBTWBGnWe+yHJK9BE9h9+ 2ZmLNp5MfvWb3/Lmc5SmoVBkpCLvnRx4c8OBKZr/Mtq3mXipqxBFjDzUB2l3ktsp3c31 rGYjrARPFdESznZZpruAgnKC0KfwvvIUjpVdq+UxMsN+ENWLfN8nrXGFM9/pAjuQxXc9 Hbtg== X-Forwarded-Encrypted: i=1; AFNElJ8ZXvno7SdZKgDq4VHN+zatNK+VIoysd1KgtR7TKWf1j8Qt7oLJzaJNCO2mY4x70lY21sc3hYAXXjYrrxc=@vger.kernel.org X-Gm-Message-State: AOJu0YySnm9VwqEfpex/1rM2HNsagUmwuoGNJLU3vpEN4vPwga95HcPw 20Js3zhyBOHlpJhx7EYufvxjarRseGKLlO0vZFtJ+udzHJy9Zae0GzL0 X-Gm-Gg: AeBDiesTKx2SWD8KhjVSk1j+ZZkePHehtZrNIfkDsuuQvZhQPNIFe9n58BPlIdqnSLJ 4buihMZ5VoYfDikDKZpOUY7mHXI3wAts/vwly8FqessPAsWNqKBXCsvXzbgqCXt46QenKve9z7b V4oWx07Xca4SIzsQAVQwJXSgBS8uzj31UAT2jGaJj7aBowUlY/nBQPce/R5PrQa8Oo5BXueI5uV Yfqp1yWj0NFFsrRRxIeBTdGNJ7yjS6twWsVKJ4zXqNCYvedheN3149QqGtNLqoe5eLoMOYJEKIO VctA3FKtqzYUjpnPdF0rMetQjh4mcAvk6O3jeoo6k9ensmViRuPM4OtTdWqKrWcAgmQHOZBhD2h 5IXPza6XmOnBIK5vefzH+Szqs2Hq/9c0QGgik6QB8pTnZtnIIRjTbmz+V6t3MeT5/8LLmZodYV1 7rs/zeHUImkUufeag76RTflo74lgMpccSuZ5G02azjnLfG7Ihfw59hI/t1xzUNVrvDM2Iq1g== X-Received: by 2002:a05:6a21:e098:b0:3a0:bc61:62e5 with SMTP id adf61e73a8af0-3a7f1c534aemr7846594637.44.1777870684446; Sun, 03 May 2026 21:58:04 -0700 (PDT) Received: from albab.. ([114.130.187.43]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-835152fec5bsm11697879b3a.0.2026.05.03.21.57.58 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 03 May 2026 21:58:03 -0700 (PDT) From: Albab Hasan To: Miguel Ojeda Cc: Albab Hasan , Danilo Krummrich , Alice Ryhl , Daniel Almeida , Boqun Feng , Gary Guo , =?UTF-8?q?Bj=C3=B6rn=20Roy=20Baron?= , Benno Lossin , Andreas Hindborg , Trevor Gross , Robin Murphy , rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH] rust: io: convert `PhysAddr` type alias to newtype Date: Mon, 4 May 2026 10:57:03 +0600 Message-ID: <20260504045714.59248-1-albabhasan276@gmail.com> X-Mailer: git-send-email 2.43.0 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 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. Suggested-by: Miguel Ojeda Link: https://github.com/Rust-for-Linux/linux/issues/1204 Signed-off-by: Albab Hasan --- 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(-) diff --git a/rust/kernel/devres.rs b/rust/kernel/devres.rs index 6afe196be42c..0de69157877b 100644 --- a/rust/kernel/devres.rs +++ b/rust/kernel/devres.rs @@ -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{ +/// unsafe fn new(paddr: PhysAddr) -> Result{ /// // 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); /// } diff --git a/rust/kernel/io.rs b/rust/kernel/io.rs index e5fba6bf6db0..7c35537edf43 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::*, // @@ -17,9 +19,48 @@ /// Physical address 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 PhysAddr = bindings::phys_addr_t; +/// +/// # Examples +/// +/// ``` +/// use kernel::{bindings, io::PhysAddr}; +/// +/// let raw: bindings::phys_addr_t = 0x1000; +/// let paddr = PhysAddr::from(raw); +/// let raw2: bindings::phys_addr_t = paddr.into(); +/// assert_eq!(raw, raw2); +/// +/// let size = usize::try_from(paddr).unwrap(); +/// assert_eq!(size, 0x1000); +/// ``` +#[repr(transparent)] +#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Default)] +pub struct PhysAddr(bindings::phys_addr_t); + +impl From for PhysAddr { + #[inline] + fn from(value: bindings::phys_addr_t) -> Self { + Self(value) + } +} + +impl From for bindings::phys_addr_t { + #[inline] + fn from(value: PhysAddr) -> Self { + value.0 + } +} + +impl TryFrom for usize { + type Error = TryFromIntError; + + #[inline] + fn try_from(value: PhysAddr) -> Result { + usize::try_from(value.0) + } +} /// Resource Size type. /// @@ -97,10 +138,10 @@ pub fn maxsize(&self) -> usize { /// /// /// /// [`paddr`, `paddr` + `SIZE`) must be a valid MMIO region that is mappable into the CPUs /// /// virtual address space. -/// unsafe fn new(paddr: usize) -> Result{ +/// unsafe fn new(paddr: PhysAddr) -> Result{ /// // 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); /// } @@ -127,7 +168,8 @@ pub fn maxsize(&self) -> usize { /// ///# fn no_run() -> Result<(), Error> { /// // SAFETY: Invalid usage for example purposes. -/// let iomem = unsafe { IoMem::<{ core::mem::size_of::() }>::new(0xBAAAAAAD)? }; +/// let addr = PhysAddr::from(0xBAAAAAAD as bindings::phys_addr_t); +/// let iomem = unsafe { IoMem::<{ core::mem::size_of::() }>::new(addr)? }; /// iomem.write32(0x42, 0x0); /// assert!(iomem.try_write32(0x42, 0x0).is_ok()); /// assert!(iomem.try_write32(0x42, 0x4).is_err()); diff --git a/rust/kernel/io/resource.rs b/rust/kernel/io/resource.rs index b7ac9faf141d..d703c7317980 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.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. diff --git a/rust/kernel/iommu/pgtable.rs b/rust/kernel/iommu/pgtable.rs index c88e38fd938a..1d75c09c47bb 100644 --- a/rust/kernel/iommu/pgtable.rs +++ b/rust/kernel/iommu/pgtable.rs @@ -182,7 +182,7 @@ pub unsafe fn map_pages( (map_pages)( self.raw_ops(), iova, - paddr, + paddr.into(), pgsize, pgcount, prot as i32, -- 2.43.0