Skip to content

Commit d9ebd92

Browse files
ojedagregkh
authored andcommitted
rust: use #[used(compiler)] to fix build and modpost with Rust >= 1.89.0
commit 7498159 upstream. Starting with Rust 1.89.0 (expected 2025-08-07), the Rust compiler fails to build the `rusttest` target due to undefined references such as: kernel...-cgu.0:(.text....+0x116): undefined reference to `rust_helper_kunit_get_current_test' Moreover, tooling like `modpost` gets confused: WARNING: modpost: missing MODULE_DESCRIPTION() in drivers/gpu/drm/nova/nova.o ERROR: modpost: missing MODULE_LICENSE() in drivers/gpu/nova-core/nova_core.o The reason behind both issues is that the Rust compiler will now [1] treat `#[used]` as `#[used(linker)]` instead of `#[used(compiler)]` for our targets. This means that the retain section flag (`R`, `SHF_GNU_RETAIN`) will be used and that they will be marked as `unique` too, with different IDs. In turn, that means we end up with undefined references that did not get discarded in `rusttest` and that multiple `.modinfo` sections are generated, which confuse tooling like `modpost` because they only expect one. Thus start using `#[used(compiler)]` to keep the previous behavior and to be explicit about what we want. Sadly, it is an unstable feature (`used_with_arg`) [2] -- we will talk to upstream Rust about it. The good news is that it has been available for a long time (Rust >= 1.60) [3]. The changes should also be fine for previous Rust versions, since they behave the same way as before [4]. Alternatively, we could use `#[no_mangle]` or `#[export_name = ...]` since those still behave like `#[used(compiler)]`, but of course it is not really what we want to express, and it requires other changes to avoid symbol conflicts. Cc: David Wood <[email protected]> Cc: Wesley Wiser <[email protected]> Cc: [email protected] # Needed in 6.12.y and later (Rust is pinned in older LTSs). Link: rust-lang/rust#140872 [1] Link: rust-lang/rust#93798 [2] Link: rust-lang/rust#91504 [3] Link: https://godbolt.org/z/sxzWTMfzW [4] Reviewed-by: Alice Ryhl <[email protected]> Acked-by: Björn Roy Baron <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Miguel Ojeda <[email protected]> Signed-off-by: Greg Kroah-Hartman <[email protected]>
1 parent affb46d commit d9ebd92

File tree

4 files changed

+8
-6
lines changed

4 files changed

+8
-6
lines changed

rust/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,7 @@ quiet_cmd_rustdoc_test = RUSTDOC T $<
157157
cmd_rustdoc_test = \
158158
OBJTREE=$(abspath $(objtree)) \
159159
$(RUSTDOC) --test $(rust_common_flags) \
160+
-Zcrate-attr='feature(used_with_arg)' \
160161
@$(objtree)/include/generated/rustc_cfg \
161162
$(rustc_target_flags) $(rustdoc_test_target_flags) \
162163
$(rustdoc_test_quiet) \

rust/kernel/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#![feature(inline_const)]
1919
#![feature(lint_reasons)]
2020
#![feature(unsize)]
21+
#![feature(used_with_arg)]
2122

2223
// Ensure conditional compilation based on the kernel configuration works;
2324
// otherwise we may silently break things like initcall handling.

rust/macros/module.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ impl<'a> ModInfoBuilder<'a> {
5757
{cfg}
5858
#[doc(hidden)]
5959
#[link_section = \".modinfo\"]
60-
#[used]
60+
#[used(compiler)]
6161
pub static __{module}_{counter}: [u8; {length}] = *{string};
6262
",
6363
cfg = if builtin {
@@ -230,7 +230,7 @@ pub(crate) fn module(ts: TokenStream) -> TokenStream {
230230
// key or a new section. For the moment, keep it simple.
231231
#[cfg(MODULE)]
232232
#[doc(hidden)]
233-
#[used]
233+
#[used(compiler)]
234234
static __IS_RUST_MODULE: () = ();
235235
236236
static mut __MOD: Option<{type_}> = None;
@@ -253,7 +253,7 @@ pub(crate) fn module(ts: TokenStream) -> TokenStream {
253253
254254
#[cfg(MODULE)]
255255
#[doc(hidden)]
256-
#[used]
256+
#[used(compiler)]
257257
#[link_section = \".init.data\"]
258258
static __UNIQUE_ID___addressable_init_module: unsafe extern \"C\" fn() -> i32 = init_module;
259259
@@ -273,7 +273,7 @@ pub(crate) fn module(ts: TokenStream) -> TokenStream {
273273
274274
#[cfg(MODULE)]
275275
#[doc(hidden)]
276-
#[used]
276+
#[used(compiler)]
277277
#[link_section = \".exit.data\"]
278278
static __UNIQUE_ID___addressable_cleanup_module: extern \"C\" fn() = cleanup_module;
279279
@@ -283,7 +283,7 @@ pub(crate) fn module(ts: TokenStream) -> TokenStream {
283283
#[cfg(not(CONFIG_HAVE_ARCH_PREL32_RELOCATIONS))]
284284
#[doc(hidden)]
285285
#[link_section = \"{initcall_section}\"]
286-
#[used]
286+
#[used(compiler)]
287287
pub static __{name}_initcall: extern \"C\" fn() -> kernel::ffi::c_int = __{name}_init;
288288
289289
#[cfg(not(MODULE))]

scripts/Makefile.build

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ $(obj)/%.lst: $(obj)/%.c FORCE
248248
# Compile Rust sources (.rs)
249249
# ---------------------------------------------------------------------------
250250

251-
rust_allowed_features := arbitrary_self_types,lint_reasons
251+
rust_allowed_features := arbitrary_self_types,lint_reasons,used_with_arg
252252

253253
# `--out-dir` is required to avoid temporaries being created by `rustc` in the
254254
# current working directory, which may be not accessible in the out-of-tree

0 commit comments

Comments
 (0)