Re: [PATCH] rust: io: convert ResourceSize into a transparent newtype

"Danilo Krummrich" <[email protected]>
Newsgroups org.kernel.vger.rust-for-linux,dev.linux.lists.driver-core,dev.linux.lists.nova-gpu,org.freedesktop.lists.dri-devel,org.kernel.vger.linux-kernel
Message-ID <[email protected]>
On Sun Jul 12, 2026 at 1:36 PM CEST, Lorenzo Delgado wrote:
> diff --git a/drivers/gpu/nova-core/firmware/gsp.rs b/drivers/gpu/nova-core/firmware/gsp.rs
> index 99a302bae567..d7593888e65b 100644
> --- a/drivers/gpu/nova-core/firmware/gsp.rs
> +++ b/drivers/gpu/nova-core/firmware/gsp.rs
> @@ -175,7 +175,7 @@ pub(crate) fn radix3_dma_handle(&self) -> DmaAddress {
>  fn map_into_lvl(sg_table: &SGTable<Owned<VVec<u8>>>, mut dst: VVec<u8>) -> Result<VVec<u8>> {
>      for sg_entry in 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::try_from(sg_entry.dma_len())?.div_ceil(GSP_PAGE_SIZE);

I think this is worse, as the conversion becomes fallible.

You could implement From<ResourceSize> for u64 and then keep using
usize::from_safe_cast() in nova-core.

Alternatively, we could also consider moving the FromSafeCast trait to
rust/kernel/num.rs and add FromSafeCast<ResourceSize> impls for usize.

I originally proposed the approach [1], since in nova-core we had a lot of
fallible conversion, while our Kconfig mandates that they can never actually
fail.

However, by making it commonly availble I do see a risk with the cfg-gated impls
silently breaking the build.

Now, technically that's the feature and 0-day etc. should usually catch it, but
on the other hand we also usually try to avoid cfg-gates in the kernel.

Miguel, I think you were in favor of this as well, what do you think?

[1] https://lore.kernel.org/rust-for-linux/[email protected]/
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.