Skip to content

rust: build_assert: document and fix use with function arguments

Alexandre Courbot linux-pm lkml rust-for-linux stable 3 versions – Applied

Status from this message: Applied to driver-core-linus, thanks!

Get series with b4
b4 am [email protected] change-id 20251127-io-build-assert-3579a5bfb81c

Versions newest first

  1. v3 Latest7 patches 15 repliesApplied
  2. v27 patches 23 repliesApplied
  3. v11 patch 4 repliesSuperseded

Changes in v3 from the cover letter

- Add "Fixes:" tags.
- CC stable on fixup patches.
- Link to v2: https://patch.msgid.link/[email protected]

Patches in v3 Applied

#Subject and filesSizeRepliesApplied
1/7rust: build_assert: add instructions for use with function argumentsrust/kernel/build_assert.rs+6 -10 replies
2/7rust: io: always inline functions using build_assert with argumentsrust/kernel/io.rs rust/kernel/io/resource.rs+8 -31 reply
3/7rust: cpufreq: always inline functions using build_assert with argumentsrust/kernel/cpufreq.rs+2 -07 replies
4/7rust: bits: always inline functions using build_assert with argumentsrust/kernel/bits.rs+4 -20 replies
5/7rust: sync: refcount: always inline functions using build_assert with argumentsrust/kernel/sync/refcount.rs+2 -12 replies
6/7rust: irq: always inline functions using build_assert with argumentsrust/kernel/irq/flags.rs+2 -00 replies
7/7rust: num: bounded: add missing comment for always inlined functionrust/kernel/num/bounded.rs+1 -00 replies

Reviews trailers per patch

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

PatchDaniel AlmeidaViresh KumarTimur TabiBoqun Feng
1/7R-b
2/7R-b T-b
3/7R-b A-b
4/7R-b
5/7R-b A-b
6/7R-b
7/7R-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 4 reviewers.

Read the v3 thread

Cover letter [PATCH v3 0/7] rust: build_assert: document and fix use with function arguments

build_assert relies on the compiler to optimize out its error path, lest build fails with the dreaded error:

ERROR: modpost: "rust_build_error" [path/to/module.ko] undefined!

It has been observed that very trivial code performing I/O accesses (sometimes even using an immediate value) would seemingly randomly fail with this error whenever CLIPPY=1 was set. The same behavior was also observed until different, very similar conditions [1][2].

The cause, as pointed out by Gary Guo [3], appears to be that the failing function is eventually using build_assert with its argument, but is only annotated with #[inline]. This gives the compiler freedom to not inline the function, which it notably did when Clippy was active, triggering the error.

The fix is to annotate functions passing their argument to build_assert with #[inline(always)], telling the compiler to be as aggressive as possible with their inlining. This is also the correct behavior as inlining is mandatory for correct behavior in these cases.

This series fixes all possible points of failure in the kernel crate, and adds documentation to build_assert explaining how to properly inline functions for which this behavior may arise.

[1] https://lore.kernel.org/all/[email protected]/
[2] https://lore.kernel.org/all/[email protected]/
[3] https://lore.kernel.org/all/[email protected]/

---
Changes in v3:
- Add "Fixes:" tags.
- CC stable on fixup patches.
- Link to v2: https://patch.msgid.link/[email protected]

Changes in v2:
- Turn into a series and address other similar cases in the kernel crate.
- Link to v1: https://patch.msgid.link/[email protected]

---
Alexandre Courbot (7):
rust: build_assert: add instructions for use with function arguments
rust: io: always inline functions using build_assert with arguments
rust: cpufreq: always inline functions using build_assert with arguments
rust: bits: always inline functions using build_assert with arguments
rust: sync: refcount: always inline functions using build_assert with arguments
rust: irq: always inline functions using build_assert with arguments
rust: num: bounded: add missing comment for always inlined function

 rust/kernel/bits.rs          | 6 ++++--
 rust/kernel/build_assert.rs  | 7 ++++++-
 rust/kernel/cpufreq.rs       | 2 ++
 rust/kernel/io.rs            | 9 ++++++---
 rust/kernel/io/resource.rs   | 2 ++
 rust/kernel/irq/flags.rs     | 2 ++
 rust/kernel/num/bounded.rs   | 1 +
 rust/kernel/sync/refcount.rs | 3 ++-
 8 files changed, 25 insertions(+), 7 deletions(-)

---
base-commit: ba65a4e7120a616d9c592750d9147f6dcafedffa
change-id: 20251127-io-build-assert-3579a5bfb81c

Best regards,

Signature · 1 line
Alexandre Courbot <[email protected]>