rust: io: convert ResourceSize into a transparent newtype
Subsystems (9)
PATCH v3 1/1 rust: io: convert ResourceSize into a transparent newtype
ResourceSize is a bare type alias for resource_size_t, so it inherits every integer operation and as cast. That allows operations that make no sense for the size of a hardware resource, such as mixing it with unrelated integers or truncating it with a cast.
Wrap it in a #[repr(transparent)] newtype so each conversion at a boundary is explicit. The representation is unchanged, so this is ABI-identical; only the spelling at the FFI boundary changes. Provide from_raw/into_raw, From in both directions, and a fallible TryFrom<ResourceSize> for usize. On 64-bit, where resource_size_t is a u64, also implement FromSafeCastArch<ResourceSize> for usize, so code that is specific to 64-bit can keep the conversion infallible.
Update the producers, Resource::size and SGEntry::dma_len, and the Region and request_region consumers. nova-core only builds on 64-bit, so its page count in gsp.rs uses from_safe_cast_arch(). tyr widens dma_len() to u64; go through into_raw() so that still builds on 32-bit ARM, where resource_size_t is a u32.
---
Changes in v3:
- Rebase on rust-next (v7.3-rc4).
- Implement FromSafeCastArch<ResourceSize> for usize in the kernel crate, next to the type, now that the lossless casts live in kernel::num::casts. Drop the impl from nova-core's num.rs, and switch gsp.rs to usize::from_safe_cast_arch(), the same line the nova-core conversion to kernel::num uses.
- Convert the dma_len() user in tyr, added since v2. Without it, tyr fails to build on 32-bit ARM without LPAE.
- Link to v2: https://lore.kernel.org/r/[email protected]
- Link to v1: https://lore.kernel.org/r/[email protected]
--- drivers/gpu/drm/tyr/vm.rs | 2 +- drivers/gpu/nova-core/firmware/gsp.rs | 3 +- rust/kernel/io.rs | 83 ++++++++++++++++++++++++++++++++--- rust/kernel/io/resource.rs | 6 +-- rust/kernel/scatterlist.rs | 2 +- 5 files changed, 85 insertions(+), 11 deletions(-)
drivers/gpu/drm/tyr/vm.rs +1 -1
@@ -645,7 +645,7 @@ fn sm_step_map<'op>(#[allow(clippy::useless_conversion)] let mut paddr = u64::from (sgt_entry.dma_address ());#[allow(clippy::useless_conversion)] -let mut sgt_entry_length = u64::from (sgt_entry.dma_len ()); +let mut sgt_entry_length = u64::from (sgt_entry.dma_len ().into_raw ());if bytes_left_to_map ==0 {break ;
drivers/gpu/nova-core/firmware/gsp.rs +2 -1
@@ -9,6 +9,7 @@ DmaAddress, // }, firmware, + num::casts::arch::FromSafeCastArch, prelude::*, scatterlist::{Owned ,
@@ -154,7 +155,7 @@ pub(crate) fn radix3_dma_address(&self) -> DmaAddress {fn map_into_lvl (sg_table: &SGTable <Owned <VVec <u8 >>>,mut dst:VVec <u8 >) ->Result <VVec <u8 >> {for sg_entryin sg_table.iter () {// Number of pages we need to map. -let num_pages = usize::from_safe_cast (sg_entry.dma_len ()).div_ceil (GSP_PAGE_SIZE ); +let num_pages = usize::from_safe_cast_arch (sg_entry.dma_len ()).div_ceil (GSP_PAGE_SIZE );for iin 0 ..num_pages {let entry = sg_entry.dma_address ()
rust/kernel/io.rs +78 -5
100 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, size.into_raw ()) }; } }
@@ -114,7 +114,7 @@ pub fn request_region( bindings::__request_region ( self.0 .get (), start, - size, + size.into_raw (), 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) } +ResourceSize ::from_raw (unsafe { bindings::resource_size (inner) }) }/// Returns the start address of the resource.
rust/kernel/scatterlist.rs +1 -1
@@ -93,7 +93,7 @@ pub fn dma_address(&self) -> dma::DmaAddress {pub fn dma_len (&self) ->ResourceSize {#[allow(clippy::useless_conversion)] // SAFETY: `self.as_raw()` is a valid pointer to a `struct scatterlist`. -unsafe { bindings::sg_dma_len (self.as_raw ()) }.into () +ResourceSize ::from_raw (unsafe { bindings::sg_dma_len (self.as_raw ()) }.into ()) } }
---
base-commit: 40288c9206c17eb66a603262e06a58d300d0f279
change-id: 20260920-resource-size-newtype-63a82d4f2cdb
Best regards,
Signature · 1 line
Lorenzo Delgado <[email protected]>
Danilo Krummrich
maintainerQuoted 5 lines · Lorenzo Delgado
ResourceSizeis a bare type alias forresource_size_t, so it inherits every integer operation andascast. That allows operations that make no sense for the size of a hardware resource, such as mixing it with unrelated integers or truncating it with a cast.
Quoted 7 lines
Wrap it in a
#[repr(transparent)]newtype so each conversion at a boundary is explicit. The representation is unchanged, so this is ABI-identical; only the spelling at the FFI boundary changes. Providefrom_raw/into_raw,Fromin both directions, and a fallibleTryFrom<ResourceSize> for usize. On 64-bit, whereresource_size_tis au64, also implementFromSafeCastArch<ResourceSize> for usize, so code that is specific to 64-bit can keep the conversion infallible.
This looks good now, but it makes me notice that dma_len() shouldn't return ResourceSize in the first place.
The length of a single SG segment is bounded by max_segment_size, which is also unsigned int.
So, I think we should change dma_len() to just return u32 (I'm also fine with a new type, but it might be slighly overkill).
I'd usually pick this patch regardless and leave that for a follow-up, as it is a separate issue. But if we change dma_len() first, we only have to update tyr once, whereas if change it after we end up touching nova and tyr twice.
Thanks,
Danilo
Quoted 6 lines · Danilo Krummrich
So, I think we should change
dma_len()to just return u32 (I'm also fine with a new type, but it might be slighly overkill).I'd usually pick this patch regardless and leave that for a follow-up, as it is a separate issue. But if we change
dma_len()first, we only have to update tyr once, whereas if change it after we end up touching nova and tyr twice.
Makes sense, thanks. I sent v4 with a patch in front that makes dma_len() return u32, so the newtype patch no longer touches tyr or nova-core:
https://lore.kernel.org/r/[email protected]
Heads-up: patch 1 interacts with Alexandre's nova_num on one line in gsp.rs. Whichever lands second needs a two-line fixup; details in the cover letter.