Re: [PATCH v2] rust: kasan/kbuild: fix rustc-option when cross-compiling

From: Miguel Ojeda

Date: Tue May 26 2026 - 04:18:58 EST


On Thu, May 7, 2026 at 1:14 PM Alice Ryhl <aliceryhl@xxxxxxxxxx> wrote:
>
> The Makefile version of rustc-option currently checks whether the option
> exists for the host target instead of the target actually being compiled
> for. It was done this way in commit 46e24a545cdb ("rust: kasan/kbuild:
> fix missing flags on first build") to avoid a circular dependency on
> target.json. However, because of this, rustc-option currently does not
> function when cross-compiling from x86_64 to aarch64 if
> CONFIG_SHADOW_CALL_STACK is enabled. This is because KBUILD_RUSTFLAGS
> contains -Zfixed-x18 under this configuration. Since that flag does not
> exist on the host target, rustc-option runs into a compilation failure
> every time, leading to all flags being rejected as unsupported.
>
> To fix this, update rustc-option to pass a --target parameter so that
> the host target is not used. For targets using target.json, use a
> built-in target that is as close as possible to the target created with
> target.json to avoid the circular dependency on target.json.
>
> One scenario where this causes a boot failure:
> * Cross-compiled from x86_64 to aarch64.
> * With CONFIG_SHADOW_CALL_STACK=y
> * With CONFIG_KASAN_SW_TAGS=y
> * With CONFIG_KASAN_INLINE=n
> Then the resulting kernel image will fail to boot when it first calls
> into Rust code with a crash along the lines of "Unable to handle kernel
> paging request at virtual address 0ffffffc08541796". This is because the
> call threshold is not specified, so rustc will inline kasan operations,
> but the kasan shadow offset is not specified, which leads to the inlined
> kasan instructions being incorrect.
>
> Note that the -Zsanitizer=kernel-hwaddress parameter itself does not
> lead to a rustc-option failure despite being aarch64-specific because
> RUSTFLAGS_KASAN has not yet been added to KBUILD_RUSTFLAGS when
> rustc-option is evaluated by the kasan Makefile.
>
> Cc: stable@xxxxxxxxxxxxxxx
> Fixes: 46e24a545cdb ("rust: kasan/kbuild: fix missing flags on first build")
> Signed-off-by: Alice Ryhl <aliceryhl@xxxxxxxxxx>

Applied to `rust-fixes` -- thanks!

[ Edited slightly:
- Reset variable to avoid using the environment.
- Use a simply expanded variable flavor for simplicity.
- Export variable so that behavior in sub-`make`s is consistent.

This matches other variables. - Miguel ]

Sashiko points out the Kconfig case, but there we only have LLVM flags
that were the expected, original use case of these, so it is OK, at
least for now.

Any further tests and Acked-by's are appreciated of course.

Cheers,
Miguel