Skip to content

Commit 1c104fa

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 fd6abf8 commit 1c104fa

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
@@ -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
@@ -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 {
@@ -247,7 +247,7 @@ pub(crate) fn module(ts: TokenStream) -> TokenStream {
247247
// key or a new section. For the moment, keep it simple.
248248
#[cfg(MODULE)]
249249
#[doc(hidden)]
250-
#[used]
250+
#[used(compiler)]
251251
static __IS_RUST_MODULE: () = ();
252252
253253
static mut __MOD: core::mem::MaybeUninit<{type_}> =
@@ -271,7 +271,7 @@ pub(crate) fn module(ts: TokenStream) -> TokenStream {
271271
272272
#[cfg(MODULE)]
273273
#[doc(hidden)]
274-
#[used]
274+
#[used(compiler)]
275275
#[link_section = \".init.data\"]
276276
static __UNIQUE_ID___addressable_init_module: unsafe extern \"C\" fn() -> i32 = init_module;
277277
@@ -291,7 +291,7 @@ pub(crate) fn module(ts: TokenStream) -> TokenStream {
291291
292292
#[cfg(MODULE)]
293293
#[doc(hidden)]
294-
#[used]
294+
#[used(compiler)]
295295
#[link_section = \".exit.data\"]
296296
static __UNIQUE_ID___addressable_cleanup_module: extern \"C\" fn() = cleanup_module;
297297
@@ -301,7 +301,7 @@ pub(crate) fn module(ts: TokenStream) -> TokenStream {
301301
#[cfg(not(CONFIG_HAVE_ARCH_PREL32_RELOCATIONS))]
302302
#[doc(hidden)]
303303
#[link_section = \"{initcall_section}\"]
304-
#[used]
304+
#[used(compiler)]
305305
pub static __{name}_initcall: extern \"C\" fn() -> kernel::ffi::c_int = __{name}_init;
306306
307307
#[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)