Skip to content

rust: introduce cv! macro for safe const conversions of integer-like types

Eliot Courtney dri-devel lkml nova-gpu rust-for-linux 5 versions – Applied

Status from this message: Applied to `rust-next` -- thanks everyone!

Get series with b4
b4 am [email protected] change-id 20260828-cv-8d4e952fc14c

Versions newest first

  1. v5 Latest2 patches 3 repliesApplied
  2. v42 patches 6 repliesSuperseded
  3. v33 patches 11 repliesSuperseded
Earlier versions · 2
VersionPostedPatchesRepliesStatus
v23 patches6 repliesSuperseded
v13 patches0 repliesSuperseded

Changes in v5 from the cover letter

- Explain the cv name, say compile time and mention const contexts in the docs (Miguel)
- Add a Pitfalls section on unsuffixed literals defaulting to i32 (Miguel, Alex)
- Fix doc links, comment capitalization and spacing (Miguel)
- Use the kernel import style in the doctest (Danilo)
- Rebase on drm-rust-next
- Link to v4: https://patch.msgid.link/[email protected]

Patches in v5 Applied

#Subject and filesSizeRepliesApplied
1/2rust: num: add cv! macro to create values from constant expressionsrust/kernel/num.rs rust/kernel/num/bounded.rs rust/kernel/prelude.rs +1 more+177 -12 replies
2/2gpu: nova-core: use cv! for constant castsdrivers/gpu/nova-core/falcon.rs drivers/gpu/nova-core/fb/hal/gb100.rs drivers/gpu/nova-core/firmware/fwsec/bootloader.rs +5 more+28 -961 reply

Reviews trailers per patch

All 2 patches have a Reviewed-by or Acked-by.

PatchGary GuoMiguel OjedaAlice RyhlDanilo KrummrichAlexandre Courbot
1/2R-b A-b R-b A-b A-b
2/2R-b R-b

Trailers given in replies are filled; those carried in the patches from earlier versions are outlined with dashes. Scroll the table sideways for all 5 reviewers.

Read the v5 thread

Cover letter [PATCH v5 0/2] rust: introduce cv! macro for safe const conversions of integer-like types

This series introduces the cv! ("const value") macro for safe compile time conversions of integer-like types. The syntax is cv!(literal or expression), or cv!(literal or expression => type) when it needs some help with type inference. The result is that many expressions can be rewritten into a compile time safe (no as) and less visually busy style, for example:

```
- const DMA_LEN: u32 = casts::usize_into_u32::<{ MEM_BLOCK_ALIGNMENT }>();
+ const DMA_LEN: u32 = cv!(MEM_BLOCK_ALIGNMENT);

- .with_const_msg_type::<{ casts::u8_as_u32(MSG_TYPE_VENDOR_PCI) }>()
+ .with_const_msg_type(cv!(MSG_TYPE_VENDOR_PCI))

- data: [[u8; GSP_PAGE_SIZE]; casts::u32_as_usize(MSGQ_NUM_PAGES)],
+ data: [[u8; GSP_PAGE_SIZE]; cv!(MSGQ_NUM_PAGES)],

-pub const NSEC_PER_SEC: i64 = bindings::NSEC_PER_SEC as i64;
+pub const NSEC_PER_SEC: i64 = cv!(bindings::NSEC_PER_SEC);

-//! let b = Bounded::<u16, 5>::new::<0x18>();
+//! let b: Bounded<u16, 5> = cv!(0x18);
```

This works by defining a trait FromConst<const V: i128> with an associated constant VALUE. Then each implementor sets VALUE to a constant expression. This works around not having const traits. An alternative idea from Gary defined a FromLiteral::from_literal<V> function [1]. This version essentially moves the implementation of that function to the const block of the associated constant (since it has to be executable at compile time anyway).

This is based on ideas from Gary Guo [1], Alice Ryhl [2], and Alexandre Courbot [3].

One (potential) limitation is that const generic types or values can't be used with cv!, because it requires the const generic expressions feature. The const_as! [3] macro did not have this limitation. This is worked around by special casing conversions to language integral types in the cv! macro (essentially subsuming const_as!). This special casing can be removed when const generic expressions can be used.

Some code is taken with permission from Alex's const_as! series.

The structure of this series is as follows:

1. cv! macro
2. updates to nova-core to use cv!

For a follow up series (I've already written this, but to avoid spamming a ton while we iterate on cv!, I'd send this after):

3. updates to various other code to use cv!
4. misc updates to other code to remove usages of as

We have multiple ways of converting - T::from(), FromSafeCast/IntoSafeCast, cv!, *_as_*. This series uses them in that order of priority, based on Alex's suggestion [4].

This is based on drm-rust-next.

[1] https://lore.kernel.org/all/[email protected]/
[2] https://lore.kernel.org/all/CAH5fLgiGcOn+HQLj4w9yDc31V54PbqNUQv8cUFRoEuFzQrAAZA@mail.gmail.com/
[3] https://lore.kernel.org/all/[email protected]/
[4] https://lore.kernel.org/all/[email protected]/

---
Changes in v5:
- Explain the cv name, say compile time and mention const contexts in the docs (Miguel)
- Add a Pitfalls section on unsuffixed literals defaulting to i32 (Miguel, Alex)
- Fix doc links, comment capitalization and spacing (Miguel)
- Use the kernel import style in the doctest (Danilo)
- Rebase on drm-rust-next
- Link to v4: https://patch.msgid.link/[email protected]

Changes in v4:
- Squash the prelude patch into the macro patch (Alex)
- Move the new code below Integer (Alex)
- Comment the explicit primitive arms (Alex)
- FromConst to make the primitive value for NonZero, Bounded and Alignment (Alex)
- const_assert!->assert! in const contexts (Gary)
- Base on drm-rust-next
- Link to v3: https://patch.msgid.link/[email protected]

Changes in v3:
- Add comment about not using FromConst trait directly (Gary)
- Lower case on const assert/panics (Gary)
- match instead of unwrap() for NonZero + message (Gary)
- Add reviewed-by tags (thanks Gary!)
- Link to v2: https://patch.msgid.link/[email protected]

Changes in v2:
- Improve error messages (Gary)
- Remove pre-req for Alex's nova-core num->kernel series.
- Link to v1: https://patch.msgid.link/[email protected]

---
Eliot Courtney (2):
rust: num: add cv! macro to create values from constant expressions
gpu: nova-core: use cv! for constant casts

 drivers/gpu/nova-core/falcon.rs                    |   9 +-
 drivers/gpu/nova-core/fb/hal/gb100.rs              |   4 +-
 drivers/gpu/nova-core/firmware/fwsec/bootloader.rs |   7 +-
 drivers/gpu/nova-core/fsp.rs                       |   3 +-
 drivers/gpu/nova-core/gsp/cmdq.rs                  |   6 +-
 drivers/gpu/nova-core/gsp/fw.rs                    |  38 +++---
 drivers/gpu/nova-core/gsp/fw/commands.rs           |   2 +-
 drivers/gpu/nova-core/num.rs                       |  55 +-------
 rust/kernel/num.rs                                 | 149 +++++++++++++++++++++
 rust/kernel/num/bounded.rs                         |  16 +++
 rust/kernel/prelude.rs                             |   1 +
 rust/kernel/ptr.rs                                 |  12 +-
 12 files changed, 205 insertions(+), 97 deletions(-)

---
base-commit: 5a1b7121a6c4db6c0239d3ac3fd1c2bb33a40025
change-id: 20260828-cv-8d4e952fc14c

Best regards,

Signature · 1 line
Eliot Courtney <[email protected]>