rust-for-linux.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v2 0/7] rust: build_assert: document and fix use with function arguments
@ 2025-11-28  2:11 Alexandre Courbot
  2025-11-28  2:11 ` [PATCH v2 1/7] rust: build_assert: add instructions for " Alexandre Courbot
                   ` (8 more replies)
  0 siblings, 9 replies; 31+ messages in thread
From: Alexandre Courbot @ 2025-11-28  2:11 UTC (permalink / raw)
  To: Danilo Krummrich, Alice Ryhl, Daniel Almeida, Miguel Ojeda,
	Alex Gaynor, Boqun Feng, Gary Guo, Björn Roy Baron,
	Benno Lossin, Andreas Hindborg, Trevor Gross, Rafael J. Wysocki,
	Viresh Kumar, Will Deacon, Peter Zijlstra, Mark Rutland
  Cc: rust-for-linux, linux-kernel, linux-pm, Alexandre Courbot

`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/DEEUYUOAEZU3.1J1HM2YQ10EX1@nvidia.com/
[2] https://lore.kernel.org/all/A1A280D4-836E-4D75-863E-30B1C276C80C@collabora.com/
[3] https://lore.kernel.org/all/20251121143008.2f5acc33.gary@garyguo.net/

Signed-off-by: Alexandre Courbot <acourbot@nvidia.com>
---
Changes in v2:
- Turn into a series and address other similar cases in the kernel crate.
- Link to v1: https://patch.msgid.link/20251127-io-build-assert-v1-1-04237f2e5850@nvidia.com

---
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: 54e3eae855629702c566bd2e130d9f40e7f35bde
change-id: 20251127-io-build-assert-3579a5bfb81c

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


^ permalink raw reply	[flat|nested] 31+ messages in thread

end of thread, other threads:[~2025-12-03 18:37 UTC | newest]

Thread overview: 31+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-11-28  2:11 [PATCH v2 0/7] rust: build_assert: document and fix use with function arguments Alexandre Courbot
2025-11-28  2:11 ` [PATCH v2 1/7] rust: build_assert: add instructions for " Alexandre Courbot
2025-11-30 21:44   ` John Hubbard
2025-11-30 21:56     ` Miguel Ojeda
2025-11-30 22:00       ` John Hubbard
2025-11-30 22:42         ` Miguel Ojeda
2025-12-01  0:52           ` John Hubbard
2025-12-01  3:44             ` Miguel Ojeda
2025-12-01  4:36               ` John Hubbard
2025-12-01 16:43                 ` Miguel Ojeda
2025-12-01 19:31                   ` John Hubbard
2025-12-01 23:06                     ` Miguel Ojeda
2025-12-02  1:38                       ` John Hubbard
2025-12-01 19:53   ` Edwin Peer
2025-12-03  3:18     ` Alexandre Courbot
2025-11-28  2:11 ` [PATCH v2 2/7] rust: io: always inline functions using build_assert with arguments Alexandre Courbot
2025-12-01 20:06   ` Edwin Peer
2025-12-02 10:14     ` Alice Ryhl
2025-11-28  2:11 ` [PATCH v2 3/7] rust: cpufreq: " Alexandre Courbot
2025-11-28  6:12   ` Viresh Kumar
2025-11-28  9:32     ` Alice Ryhl
2025-11-28  9:55       ` Viresh Kumar
2025-11-28  2:11 ` [PATCH v2 4/7] rust: bits: " Alexandre Courbot
2025-11-28  2:11 ` [PATCH v2 5/7] rust: sync: refcount: " Alexandre Courbot
2025-11-28  2:11 ` [PATCH v2 6/7] rust: irq: " Alexandre Courbot
2025-11-28  2:11 ` [PATCH v2 7/7] rust: num: bounded: add missing comment for always inlined function Alexandre Courbot
2025-11-28 14:02 ` [PATCH v2 0/7] rust: build_assert: document and fix use with function arguments Daniel Almeida
2025-11-30 16:09 ` Daniel Almeida
2025-11-30 17:48   ` Miguel Ojeda
2025-12-03  3:19     ` Alexandre Courbot
2025-12-03 18:37       ` Miguel Ojeda

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).