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

From: Alexandre Courbot

Date: Sat Jul 25 2026 - 20:32:18 EST


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

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@xxxxxxxxxx/