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

"Alexandre Courbot" <[email protected]> Sun, 26 Jul 2026 09:32:00 +0900
Newsgroups dev.linux.lists.nova-gpu,dev.linux.lists.driver-core,org.kernel.vger.linux-kernel,org.kernel.vger.rust-for-linux
Message-ID <[email protected]>
On Fri Jul 17, 2026 at 6:45 AM JST, Danilo Krummrich wrote:
> 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-co=
re/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 =3D usize::from_safe_cast(sg_entry.dma_len()).div=
_ceil(GSP_PAGE_SIZE);
>> +        let num_pages =3D 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 o=
f
> fallible conversion, while our Kconfig mandates that they can never actua=
lly
> fail.

Back last year (!) I sent a RFC for this [1] but never followed up
somehow. Let me send a proper patch as it should be useful to have
globally.

[1] https://lore.kernel.org/all/20251104-as_casts-v1-1-0a0e95bd2a9f@nvidia.=
com/