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

Lorenzo Delgado <[email protected]>
Newsgroups dev.linux.lists.driver-core,dev.linux.lists.nova-gpu,org.freedesktop.lists.dri-devel,org.kernel.vger.linux-kernel,org.kernel.vger.rust-for-linux
Message-ID <[email protected]>
On Thu Jul 16, 2026 at 11:45 PM CEST, Danilo Krummrich wrote:
> On Sun Jul 12, 2026 at 1:36 PM CEST, Lorenzo Delgado wrote:
> > -        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.

Agreed, that's a regression for nova-core, where the Kconfig guarantees
the value fits and the conversion should stay infallible. I'll drop the
try_from() there.

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

I tried that, but it doesn't build. ResourceSize wraps resource_size_t,
which is u64 on 64-bit (CONFIG_PHYS_ADDR_T_64BIT), so the impl the patch
already has,

    impl From<ResourceSize> for bindings::resource_size_t

is already From<ResourceSize> for u64 there, and a second one conflicts:

    error[E0119]: conflicting implementations of trait
                  `From<ResourceSize>` for type `u64`

There's a simpler way that stays infallible and adds nothing to io.rs.
ResourceSize already has into_raw() (io/resource.rs uses it at the C
boundaries), so nova-core can do:

    let num_pages =
        usize::from_safe_cast(sg_entry.dma_len().into_raw()).div_ceil(GSP_PAGE_SIZE);

into_raw() gives back resource_size_t, and from_safe_cast handles that
as u32 or u64 depending on the config, so it stays infallible. I'll use
that in v2 unless you'd prefer something else.

> Alternatively, we could also consider moving the FromSafeCast trait to
> rust/kernel/num.rs and add FromSafeCast<ResourceSize> impls for usize.
> [...]
> However, by making it commonly availble I do see a risk with the
> cfg-gated impls silently breaking the build.

Agreed on the risk. A FromSafeCast<ResourceSize> for usize impl would
have to be cfg-gated like the u64 one, which is the same
silent-breakage-under-randconfig case you mention.

Since into_raw() keeps this patch self-contained, moving FromSafeCast
into the kernel crate is a separate change from the newtype conversion.
It seems worth doing on its own, and I'm happy to send it as its own
series so it gets reviewed as a new core API, but it doesn't need to
block this patch. I'll post v2 with the into_raw() change.

Thanks for the review.

Lorenzo
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.