Skip to content

Commit 41c4e22

Browse files
committed
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: Björn Roy Baron <[email protected]> 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]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Miguel Ojeda <[email protected]>
1 parent aa7b65c commit 41c4e22

File tree

6 files changed

+13
-8
lines changed

6 files changed

+13
-8
lines changed

rust/Makefile

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

rust/kernel/firmware.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ macro_rules! module_firmware {
202202
};
203203

204204
#[link_section = ".modinfo"]
205-
#[used]
205+
#[used(compiler)]
206206
static __MODULE_FIRMWARE: [u8; $($builder)*::create(__MODULE_FIRMWARE_PREFIX)
207207
.build_length()] = $($builder)*::create(__MODULE_FIRMWARE_PREFIX).build();
208208
};

rust/kernel/kunit.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,7 @@ macro_rules! kunit_unsafe_test_suite {
302302
is_init: false,
303303
};
304304

305-
#[used]
305+
#[used(compiler)]
306306
#[allow(unused_unsafe)]
307307
#[cfg_attr(not(target_os = "macos"), link_section = ".kunit_test_suites")]
308308
static mut KUNIT_TEST_SUITE_ENTRY: *const ::kernel::bindings::kunit_suite =

rust/kernel/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@
3434
// Expected to become stable.
3535
#![feature(arbitrary_self_types)]
3636
//
37+
// To be determined.
38+
#![feature(used_with_arg)]
39+
//
3740
// `feature(derive_coerce_pointee)` is expected to become stable. Before Rust
3841
// 1.84.0, it did not exist, so enable the predecessor features.
3942
#![cfg_attr(CONFIG_RUSTC_HAS_COERCE_POINTEE, feature(derive_coerce_pointee))]

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 {
@@ -249,7 +249,7 @@ pub(crate) fn module(ts: TokenStream) -> TokenStream {
249249
// key or a new section. For the moment, keep it simple.
250250
#[cfg(MODULE)]
251251
#[doc(hidden)]
252-
#[used]
252+
#[used(compiler)]
253253
static __IS_RUST_MODULE: () = ();
254254
255255
static mut __MOD: ::core::mem::MaybeUninit<{type_}> =
@@ -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 = \".init.data\"]
278278
static __UNIQUE_ID___addressable_init_module: unsafe extern \"C\" fn() -> i32 = init_module;
279279
@@ -293,7 +293,7 @@ pub(crate) fn module(ts: TokenStream) -> TokenStream {
293293
294294
#[cfg(MODULE)]
295295
#[doc(hidden)]
296-
#[used]
296+
#[used(compiler)]
297297
#[link_section = \".exit.data\"]
298298
static __UNIQUE_ID___addressable_cleanup_module: extern \"C\" fn() = cleanup_module;
299299
@@ -303,7 +303,7 @@ pub(crate) fn module(ts: TokenStream) -> TokenStream {
303303
#[cfg(not(CONFIG_HAVE_ARCH_PREL32_RELOCATIONS))]
304304
#[doc(hidden)]
305305
#[link_section = \"{initcall_section}\"]
306-
#[used]
306+
#[used(compiler)]
307307
pub static __{ident}_initcall: extern \"C\" fn() ->
308308
::kernel::ffi::c_int = __{ident}_init;
309309

scripts/Makefile.build

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -312,10 +312,11 @@ $(obj)/%.lst: $(obj)/%.c FORCE
312312
# - Stable since Rust 1.82.0: `feature(asm_const)`, `feature(raw_ref_op)`.
313313
# - Stable since Rust 1.87.0: `feature(asm_goto)`.
314314
# - Expected to become stable: `feature(arbitrary_self_types)`.
315+
# - To be determined: `feature(used_with_arg)`.
315316
#
316317
# Please see https://github.com/Rust-for-Linux/linux/issues/2 for details on
317318
# the unstable features in use.
318-
rust_allowed_features := asm_const,asm_goto,arbitrary_self_types,lint_reasons,raw_ref_op
319+
rust_allowed_features := asm_const,asm_goto,arbitrary_self_types,lint_reasons,raw_ref_op,used_with_arg
319320

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

0 commit comments

Comments
 (0)