Re: [PATCH v2 1/2] kbuild: rust: enable KCSAN support

From: Gary Guo

Date: Mon Oct 19 2026 - 14:32:48 EST


On Thu Oct 15, 2026 at 3:56 PM BST, Marco Elver wrote:
> Rust supports ThreadSanitizer instrumentation via LLVM, which is what
> KCSAN relies on; however, Rust code is not currently instrumented.
>
> Pass -Zsanitizer=thread and the same TSan backend options used for C
> code to rustc. Wire up KCSAN_SANITIZE in scripts/Makefile.lib for parity
> with C (though rustc currently rejects linking crates with mismatched
> instrumentation).
>
> rustc only accepts -Zsanitizer=thread for targets that declare support
> for it. Of the targets used by the kernel, this is only possible for the
> generated x86_64 target specification, so restrict instrumentation of
> Rust code to x86_64 for now via HAVE_KCSAN_RUSTC. On other architectures
> Rust code remains uninstrumented as before.
>
> All TSan options used are supported by the LLVM versions of the minimum
> supported rustc, so no rustc-option checks are needed (they would also
> fail, because x86_64-unknown-none used for option checks does not
> support -Zsanitizer=thread).
>
> Unlike Clang and GCC, an equivalent to -fno-optimize-sibling-calls is
> omitted for Rust because rustc provides no corresponding option. While
> disabling tail-call optimizations helps preserve complete stacks traces
> in reports, it is optional and not required for KCSAN to function.
>
> With this enabled we can detect data races in kernel Rust code and
> between Rust and C code.
>
> Acked-by: Miguel Ojeda <ojeda@xxxxxxxxxx>
> Acked-by: Boqun Feng <boqun@xxxxxxxxxx>
> Signed-off-by: Marco Elver <elver@xxxxxxxxxx>

Acked-by: Gary Guo <gary@xxxxxxxxxxx>

> ---
> v2:
> * Add note on -fno-optimize-sibling-calls to commit message.
> ---
> Documentation/dev-tools/kcsan.rst | 7 +++++++
> lib/Kconfig.kcsan | 7 +++++++
> scripts/Makefile.kcsan | 13 +++++++++++++
> scripts/Makefile.lib | 3 +++
> scripts/generate_rust_target.rs | 2 +-
> 5 files changed, 31 insertions(+), 1 deletion(-)