[PATCH 0/3] rust: add `num` module

Alexandre Courbot acourbot at nvidia.com
Fri Jun 20 13:14:50 UTC 2025


This patchset was part of the Nova FWSEC-FRTS submission [1], but has
been extracted as it was not very visible there, and seems to require
more discussion that the Nova code. I hope that presenting it separately
will help make it converge towards something that can be merged.

The motivation for this new module is to provide numerical functions not
supported (or not optimally supported for the kernel use-cases) by Rust
core. One example is how kernel code often aligns addresses or length to
some power-of-two. While Rust has `next_multiple_of` that provides the
same result, it also supports non-power-of-two values and is thus not as
efficient as the simple substract-and-mask operation that can be done
with a power of two.

Another missing operation is `last_set_bit` (`fls` in the C code), which
is again commonly used in the kernel but has no equivalent yet in Rust
core.

This patchset introduces these two features available:

- A `PowerOfTwo` newtype for unsigned integers with `align_down` and
  `align_up` operations optimized for the kernel. The newtype ensures
  that these operations cannot be used with a non-power-of-two, which
  would yield an incorrect result.
- A set of `last_set_bit` const functions for unsigned integers types.

The last patch makes use of these features in the Nova driver, as was
originally done in the patchset. It requires the v6 of the patch series
[2] to be applied.

[1] https://lore.kernel.org/rust-for-linux/DANP9ATT1T5W.1KP4992E26FTP@nvidia.com/
[2] https://lore.kernel.org/rust-for-linux/20250619-nova-frts-v6-0-ecf41ef99252@nvidia.com/

Changes since split from the nova-core series:

- Rename `fls` to `last_set_bit`,
- Generate per-type doctests,
- Add invariants section to `PowerOfTwo`.
- Do not use reference to `self` in `PowerOfTwo` methods since it
  implements `Copy`,
- Use #[derive] where possible instead of implementing traits manually,
- Remove `Deref` and `Borrow` implementations.

Signed-off-by: Alexandre Courbot <acourbot at nvidia.com>
---
Alexandre Courbot (3):
      rust: add `num` module with `PowerOfTwo` type
      rust: num: add the `last_set_bit` operation
      nova-core: use `num` module

 Documentation/gpu/nova/core/todo.rst      |  15 ---
 drivers/gpu/nova-core/falcon/hal/ga102.rs |   4 +-
 drivers/gpu/nova-core/fb.rs               |   6 +-
 drivers/gpu/nova-core/firmware/fwsec.rs   |   7 +-
 drivers/gpu/nova-core/vbios.rs            |   4 +-
 rust/kernel/lib.rs                        |   1 +
 rust/kernel/num.rs                        | 201 ++++++++++++++++++++++++++++++
 7 files changed, 211 insertions(+), 27 deletions(-)
---
base-commit: c7864f7ab73417e352d8d00e46d3e9e6a228c5ab
change-id: 20250620-num-9420281c02c7

Best regards,
-- 
Alexandre Courbot <acourbot at nvidia.com>



More information about the Nouveau mailing list