[PATCH 0/7] gpu: nova-core: remove use of `as` for integer conversions

Alexandre Courbot posted 7 patches 3 months, 2 weeks ago
There is a newer version of this series
drivers/gpu/nova-core/falcon.rs           |   9 +-
drivers/gpu/nova-core/falcon/hal/ga102.rs |   6 +-
drivers/gpu/nova-core/fb.rs               |   7 +-
drivers/gpu/nova-core/fb/hal/ga100.rs     |   4 +
drivers/gpu/nova-core/fb/hal/tu102.rs     |  16 ++-
drivers/gpu/nova-core/firmware.rs         |   7 +-
drivers/gpu/nova-core/firmware/booter.rs  |  31 +++---
drivers/gpu/nova-core/firmware/fwsec.rs   |  23 ++---
drivers/gpu/nova-core/firmware/gsp.rs     |   5 +-
drivers/gpu/nova-core/firmware/riscv.rs   |   7 +-
drivers/gpu/nova-core/nova_core.rs        |   1 +
drivers/gpu/nova-core/num.rs              | 159 ++++++++++++++++++++++++++++++
drivers/gpu/nova-core/regs.rs             |   5 +-
drivers/gpu/nova-core/vbios.rs            |  60 ++++++-----
14 files changed, 261 insertions(+), 79 deletions(-)
[PATCH 0/7] gpu: nova-core: remove use of `as` for integer conversions
Posted by Alexandre Courbot 3 months, 2 weeks ago
Using the `as` operator for integer conversions is discouraged, as it
silently strips data if the destination type is smaller than the source.
Many such conversions can be replaced with `from`/`into` or (when
justified) `try_from`/`try_into`, but these traits cannot unfortunately
cover all conversions satisfyingly.

There is for instance the case of converting a `usize` to `u64`, which,
in the case of the kernel today, is completely lossless but cannot be
done because the Rust standard library does not provide a `From`
implementation for conversions that are not future-proof.

Still, in the kernel it is very practical to be able to perform such
conversions when they are safe to do for the current build target.

This patchset tries to eradicate the use of `as` in nova-core, by using
existing means and introducing new ones.

The first 4 patches use the already-available `From` and `TryFrom` trait
where it is possible or advisable.

The fifth patch introduces a new module that proposes conversion
functions for those that are infallible under the current build target.
This is done through a set of const functions, and the `FromAs` and
`IntoAs` extension traits which, as their names lightly suggest, offer
conversion for those types on which the `as` operator can be used
losslessly.

This new module is put to use in the sixth patch.

The idea was first suggested by Danilo, and I hope I captured it
properly. :)

As Danilo suggested, this could eventually find its place in the kernel
crate if the implementation is deemed to be fit, but for now let's
review and let it mature in nova-core.

Suggested-by: Danilo Krummrich <dakr@kernel.org>
Link: https://lore.kernel.org/rust-for-linux/DDK4KADWJHMG.1FUPL3SDR26XF@kernel.org/
Signed-off-by: Alexandre Courbot <acourbot@nvidia.com>
---
Alexandre Courbot (7):
      gpu: nova-core: replace `as` with `from` conversions where possible
      gpu: nova-core: vbios: remove unneeded u8 conversions
      gpu: nova-core: vbios: add conversion to u8 for BiosImageType
      gpu: nova-core: use `try_from` instead of `as` for u32 conversions
      gpu: nova-core: add extra conversion functions and traits
      gpu: nova-core: replace use of `as` with functions from `num`
      gpu: nova-core: justify remaining uses of `as`

 drivers/gpu/nova-core/falcon.rs           |   9 +-
 drivers/gpu/nova-core/falcon/hal/ga102.rs |   6 +-
 drivers/gpu/nova-core/fb.rs               |   7 +-
 drivers/gpu/nova-core/fb/hal/ga100.rs     |   4 +
 drivers/gpu/nova-core/fb/hal/tu102.rs     |  16 ++-
 drivers/gpu/nova-core/firmware.rs         |   7 +-
 drivers/gpu/nova-core/firmware/booter.rs  |  31 +++---
 drivers/gpu/nova-core/firmware/fwsec.rs   |  23 ++---
 drivers/gpu/nova-core/firmware/gsp.rs     |   5 +-
 drivers/gpu/nova-core/firmware/riscv.rs   |   7 +-
 drivers/gpu/nova-core/nova_core.rs        |   1 +
 drivers/gpu/nova-core/num.rs              | 159 ++++++++++++++++++++++++++++++
 drivers/gpu/nova-core/regs.rs             |   5 +-
 drivers/gpu/nova-core/vbios.rs            |  60 ++++++-----
 14 files changed, 261 insertions(+), 79 deletions(-)
---
base-commit: ca16b15e78f4dee1631c0a68693f5e7d9b3bb3ec
change-id: 20251022-nova-as-8ece4142872a

Best regards,
-- 
Alexandre Courbot <acourbot@nvidia.com>
Re: [PATCH 0/7] gpu: nova-core: remove use of `as` for integer conversions
Posted by Danilo Krummrich 3 months, 2 weeks ago
On 10/26/25 3:39 PM, Alexandre Courbot wrote:
> This patchset tries to eradicate the use of `as` in nova-core, by using
> existing means and introducing new ones.

Thanks a lot for working on this cleanup!
> The first 4 patches use the already-available `From` and `TryFrom` trait
> where it is possible or advisable.
> 
> The fifth patch introduces a new module that proposes conversion
> functions for those that are infallible under the current build target.
> This is done through a set of const functions, and the `FromAs` and
> `IntoAs` extension traits which, as their names lightly suggest, offer
> conversion for those types on which the `as` operator can be used
> losslessly.
> 
> This new module is put to use in the sixth patch.
> 
> The idea was first suggested by Danilo, and I hope I captured it
> properly. :)

Yes, this is what I thought of.
> As Danilo suggested, this could eventually find its place in the kernel
> crate if the implementation is deemed to be fit, but for now let's
> review and let it mature in nova-core.

Yeah, I think this approach makes sense. Though, I think we want to move this to
the kernel crate sooner than later, it's definitely something we want for core
code and other drivers as well.

Depending on the initial feedback, it might even make sense to do it right away.

I won't have time for a thorough review, but at a first glance the patches all
look good!

Acked-by: Danilo Krummrich <dakr@kernel.org>
Re: [PATCH 0/7] gpu: nova-core: remove use of `as` for integer conversions
Posted by Alexandre Courbot 3 months, 1 week ago
On Mon Oct 27, 2025 at 12:35 AM JST, Danilo Krummrich wrote:
> On 10/26/25 3:39 PM, Alexandre Courbot wrote:
>> This patchset tries to eradicate the use of `as` in nova-core, by using
>> existing means and introducing new ones.
>
> Thanks a lot for working on this cleanup!
>> The first 4 patches use the already-available `From` and `TryFrom` trait
>> where it is possible or advisable.
>> 
>> The fifth patch introduces a new module that proposes conversion
>> functions for those that are infallible under the current build target.
>> This is done through a set of const functions, and the `FromAs` and
>> `IntoAs` extension traits which, as their names lightly suggest, offer
>> conversion for those types on which the `as` operator can be used
>> losslessly.
>> 
>> This new module is put to use in the sixth patch.
>> 
>> The idea was first suggested by Danilo, and I hope I captured it
>> properly. :)
>
> Yes, this is what I thought of.

Great!

>> As Danilo suggested, this could eventually find its place in the kernel
>> crate if the implementation is deemed to be fit, but for now let's
>> review and let it mature in nova-core.
>
> Yeah, I think this approach makes sense. Though, I think we want to move this to
> the kernel crate sooner than later, it's definitely something we want for core
> code and other drivers as well.
>
> Depending on the initial feedback, it might even make sense to do it right away.

Moving to the kernel crate sounds good to me, but this work has been
initiated from the Nova GSP boot series and is now a dependency of it -
so taking it through another tree than `drm-rust` would delay the GSP
boot some more.