Skip to content

Commit ed9b1b4

Browse files
ojedamcgrof
authored andcommitted
rust: use #[used(compiler)] to fix build and modpost with Rust >= 1.89.0
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 <david@davidtw.co> Cc: Wesley Wiser <wwiser@gmail.com> Cc: stable@vger.kernel.org # 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 <aliceryhl@google.com> Acked-by: Björn Roy Baron <bjorn3_gh@protonmail.com> Link: https://lore.kernel.org/r/20250712160103.1244945-3-ojeda@kernel.org Signed-off-by: Miguel Ojeda <ojeda@kernel.org> (cherry picked from commit 7498159) Signed-off-by: Miguel Ojeda <ojeda@kernel.org>
1 parent fd08609 commit ed9b1b4

File tree

6 files changed

+11
-8
lines changed

6 files changed

+11
-8
lines changed

rust/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,7 @@ quiet_cmd_rustdoc_test = RUSTDOC T $<
192192
RUST_MODFILE=test.rs \
193193
OBJTREE=$(abspath $(objtree)) \
194194
$(RUSTDOC) --test $(rust_common_flags) \
195+
-Zcrate-attr='feature(used_with_arg)' \
195196
@$(objtree)/include/generated/rustc_cfg \
196197
$(rustc_target_flags) $(rustdoc_test_target_flags) \
197198
$(rustdoc_test_quiet) \

rust/kernel/firmware.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ macro_rules! module_firmware {
198198
};
199199

200200
#[link_section = ".modinfo"]
201-
#[used]
201+
#[used(compiler)]
202202
static __MODULE_FIRMWARE: [u8; $($builder)*::create(__MODULE_FIRMWARE_PREFIX)
203203
.build_length()] = $($builder)*::create(__MODULE_FIRMWARE_PREFIX).build();
204204
};

rust/kernel/kunit.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,7 @@ macro_rules! kunit_unsafe_test_suite {
278278
is_init: false,
279279
};
280280

281-
#[used]
281+
#[used(compiler)]
282282
#[allow(unused_unsafe)]
283283
#[cfg_attr(not(target_os = "macos"), link_section = ".kunit_test_suites")]
284284
static mut KUNIT_TEST_SUITE_ENTRY: *const ::kernel::bindings::kunit_suite =

rust/kernel/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626
#![feature(const_mut_refs)]
2727
#![feature(const_ptr_write)]
2828
#![feature(const_refs_to_cell)]
29+
// To be determined.
30+
#![feature(used_with_arg)]
2931

3032
// Ensure conditional compilation based on the kernel configuration works;
3133
// 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
#[cfg_attr(not(target_os = \"macos\"), link_section = \".modinfo\")]
60-
#[used]
60+
#[used(compiler)]
6161
pub static __{module}_{counter}: [u8; {length}] = *{string};
6262
",
6363
cfg = if builtin {
@@ -256,7 +256,7 @@ pub(crate) fn module(ts: TokenStream) -> TokenStream {
256256
// key or a new section. For the moment, keep it simple.
257257
#[cfg(MODULE)]
258258
#[doc(hidden)]
259-
#[used]
259+
#[used(compiler)]
260260
static __IS_RUST_MODULE: () = ();
261261
262262
static mut __MOD: core::mem::MaybeUninit<{type_}> =
@@ -280,7 +280,7 @@ pub(crate) fn module(ts: TokenStream) -> TokenStream {
280280
281281
#[cfg(MODULE)]
282282
#[doc(hidden)]
283-
#[used]
283+
#[used(compiler)]
284284
#[link_section = \".init.data\"]
285285
static __UNIQUE_ID___addressable_init_module: unsafe extern \"C\" fn() -> i32 = init_module;
286286
@@ -299,7 +299,7 @@ pub(crate) fn module(ts: TokenStream) -> TokenStream {
299299
300300
#[cfg(MODULE)]
301301
#[doc(hidden)]
302-
#[used]
302+
#[used(compiler)]
303303
#[link_section = \".exit.data\"]
304304
static __UNIQUE_ID___addressable_cleanup_module: extern \"C\" fn() = cleanup_module;
305305
@@ -309,7 +309,7 @@ pub(crate) fn module(ts: TokenStream) -> TokenStream {
309309
#[cfg(not(CONFIG_HAVE_ARCH_PREL32_RELOCATIONS))]
310310
#[doc(hidden)]
311311
#[link_section = \"{initcall_section}\"]
312-
#[used]
312+
#[used(compiler)]
313313
pub static __{name}_initcall: extern \"C\" fn() -> kernel::ffi::c_int = __{name}_init;
314314
315315
#[cfg(not(MODULE))]

scripts/Makefile.build

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

225-
rust_allowed_features := asm_const,asm_goto,arbitrary_self_types,lint_reasons,raw_ref_op
225+
rust_allowed_features := asm_const,asm_goto,arbitrary_self_types,lint_reasons,raw_ref_op,used_with_arg
226226

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

0 commit comments

Comments
 (0)