Skip to contentSkip to conversation

kbuild: rust: enable KCSAN support

PATCH In review Marco Elver 4 participants 11 messages Oct 15 – Oct 20

Subsystems (6)
lore
Share & tools
lore

Marco Elver

author maintainer
#0 lore raw

PATCH v2 1/2 kbuild: rust: enable KCSAN support

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.

---
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(-)
Documentation/dev-tools/kcsan.rst +7 -0
index 8575178aa87f..3bb8754f844b 100644
@@ -14,6 +14,9 @@ Usage
 KCSAN is supported by both GCC and Clang. With GCC we require version 11 or
 later, and with Clang also require version 11 or later.
 
+Rust code is instrumented as well on architectures whose Rust target supports
+it (currently only x86_64; see ``CONFIG_HAVE_KCSAN_RUSTC``).
+
 To enable KCSAN configure the kernel with::
 
     CONFIG_KCSAN = y
@@ -121,6 +124,10 @@ the below options are available:
 
     KCSAN_SANITIZE := n
 
+  Note that ``rustc`` rejects linking crates built with and without KCSAN
+  instrumentation, so ``KCSAN_SANITIZE`` and ``KCSAN_SANITIZE_<target>.o``
+  cannot currently be used for Rust code.
+
 .. _"Marking Shared-Memory Accesses" in the LKMM: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/tools/memory-model/Documentation/access-marking.txt
 
 Furthermore, it is possible to tell KCSAN to show or hide entire classes of
lib/Kconfig.kcsan +7 -0
index 4ce4b0c0109c..f0096e1c8b61 100644
@@ -10,6 +10,13 @@ config HAVE_KCSAN_COMPILER
 	  For the list of compilers that support KCSAN, please see
 	  <file:Documentation/dev-tools/kcsan.rst>.
 
+config HAVE_KCSAN_RUSTC
+	def_bool RUST && X86_64
+	help
+	  rustc only accepts -Zsanitizer=thread for targets that declare
+	  support for it; of the kernel's Rust targets, only x86_64 does.
+	  Elsewhere, Rust code is not instrumented.
+
 menuconfig KCSAN
 	bool "KCSAN: dynamic data race detector"
 	depends on HAVE_ARCH_KCSAN && HAVE_KCSAN_COMPILER
scripts/Makefile.kcsan +13 -0
index 19f693b68a96..01af6fd71006 100644
@@ -24,3 +24,16 @@ kcsan-cflags += $(call cc-option,$(call cc-param,tsan-instrument-func-entry-exit
 endif
 
 export CFLAGS_KCSAN := $(kcsan-cflags)
+
+ifdef CONFIG_HAVE_KCSAN_RUSTC
+# The minimum supported rustc's LLVM supports all of these options.
+rustc-param = -Cllvm-args=-$(1)
+kcsan-rustflags := -Zsanitizer=thread \
+	$(call rustc-param,tsan-compound-read-before-write=1) \
+	$(call rustc-param,tsan-distinguish-volatile=1)
+ifndef CONFIG_KCSAN_WEAK_MEMORY
+kcsan-rustflags += $(call rustc-param,tsan-instrument-func-entry-exit=0)
+endif
+endif
+
+export RUSTFLAGS_KCSAN := $(kcsan-rustflags)
scripts/Makefile.lib +3 -0
index 0a4fdd8bd975..78f1c23e54be 100644
@@ -98,6 +98,9 @@ ifeq ($(CONFIG_KCSAN),y)
 _c_flags += $(if $(patsubst n%,, \
 	$(KCSAN_SANITIZE_$(target-stem).o)$(KCSAN_SANITIZE)$(is-kernel-object)), \
 	$(CFLAGS_KCSAN))
+_rust_flags += $(if $(patsubst n%,, \
+	$(KCSAN_SANITIZE_$(target-stem).o)$(KCSAN_SANITIZE)$(is-kernel-object)), \
+	$(RUSTFLAGS_KCSAN))
 # Some uninstrumented files provide implied barriers required to avoid false
 # positives: set KCSAN_INSTRUMENT_BARRIERS for barrier instrumentation only.
 _c_flags += $(if $(patsubst n%,, \
scripts/generate_rust_target.rs +1 -1
index 7687b0dd5474..4b02bffa6b46 100644
@@ -231,7 +231,7 @@ fn main() {
         }
         ts.push("features", features);
         ts.push("llvm-target", "x86_64-linux-gnu");
-        ts.push("supported-sanitizers", ["kcfi", "kernel-address"]);
+        ts.push("supported-sanitizers", ["kcfi", "kernel-address", "thread"]);
         if cfg.rustc_version_atleast(1, 91, 0) {
             ts.push("target-pointer-width", 64);
         } else {
Signature · 1 line
2.56.0.385.gd3acb90ef8-goog
author maintainer
#1 lore raw

PATCH v2 2/2 rust: kcsan: add KUnit test for data race detection

Add a minimal KUnit test that checks that KCSAN detects data races in Rust code: two threads concurrently increment a shared counter without synchronization, and a probe on the console tracepoint waits for the KCSAN report (similar to kcsan_test.c):

| BUG: KCSAN: data-race in _RNvNtNtCskctMpSfuwL9_6kernel10kcsan_test5tests8racy_inc / _RNvNtNtCskctMpSfuwL9_6kernel10kcsan_test5tests8racy_inc
|
| read-write to 0xffffffffb751ae28 of 8 bytes by task 116 on cpu 5:
|  _RNvNtNtCskctMpSfuwL9_6kernel10kcsan_test5tests8racy_inc+0x1c/0x30
|  _RNvNtNtCskctMpSfuwL9_6kernel10kcsan_test5tests28kunit_rust_wrapper_data_race+0x28c/0x850
|  kunit_try_run_case+0xdf/0x2a0
|  kunit_generic_run_threadfn_adapter+0x48/0x80
|  kthread+0x234/0x280
|  ret_from_fork+0xf8/0x280
|  ret_from_fork_asm+0x1a/0x30
|
| read-write to 0xffffffffb751ae28 of 8 bytes by task 12 on cpu 3:
|  _RNvNtNtCskctMpSfuwL9_6kernel10kcsan_test5tests8racy_inc+0x1c/0x30
|  _RNvXsb_NtCskctMpSfuwL9_6kernel9workqueueINtNtCs1G77UTwpz6o_4core3pin3PinINtNtNtB7_5alloc4kbox3BoxINtB5_11ClosureWorkNCNvNtNtB7_10kcsan_test5tests9data_races_0ENtNtB1d_9allocator7KmallocEEINtB5_15WorkItemPointerKy0_E3runB7_+0x55/0x120
|  process_scheduled_works+0x4cd/0x970
|  worker_thread+0x4de/0x6e0
|  kthread+0x234/0x280
|  ret_from_fork+0xf8/0x280
|  ret_from_fork_asm+0x1a/0x30
|
| value changed: 0x000000000000022b -> 0x00000000000005a2
| [...]

The test is intentionally kept minimal; comprehensive tests of KCSAN's runtime behaviour are in kernel/kcsan/kcsan_test.c.

Assisted-by Antigravity

---
v2:
* Fix formatting.

---
 MAINTAINERS                     |   1 +
 rust/bindings/bindings_helper.h |   1 +
 rust/kernel/Kconfig.test        |  11 +++
 rust/kernel/kcsan_test.rs       | 137 ++++++++++++++++++++++++++++++++
 rust/kernel/lib.rs              |   2 +
 5 files changed, 152 insertions(+)
 create mode 100644 rust/kernel/kcsan_test.rs
MAINTAINERS +1 -0
index cc3cae2e378b..3fbbfbb31f5d 100644
@@ -14089,6 +14089,7 @@ F:	Documentation/dev-tools/kcsan.rst
 F:	include/linux/kcsan*.h
 F:	kernel/kcsan/
 F:	lib/Kconfig.kcsan
+F:	rust/kernel/kcsan_test.rs
 F:	scripts/Makefile.kcsan
 
 KDUMP
rust/bindings/bindings_helper.h +1 -0
index 4b31aa7f432f..b06d5142d6e1 100644
@@ -97,6 +97,7 @@
 #include <linux/xarray.h>
 #include <net/genetlink.h>
 #include <net/netlink.h>
+#include <trace/events/printk.h>
 #include <trace/events/rust_sample.h>
 
 /*
rust/kernel/Kconfig.test +11 -0
index e6a5c7a795f0..caf98a23de76 100644
@@ -83,4 +83,15 @@ config RUST_BITFIELD_KUNIT_TEST
 
 	  If unsure, say N.
 
+config RUST_KCSAN_KUNIT_TEST
+	bool "KUnit test for KCSAN instrumentation of Rust code" if !KUNIT_ALL_TESTS
+	depends on KCSAN && HAVE_KCSAN_RUSTC && TRACEPOINTS
+	default KUNIT_ALL_TESTS
+	help
+	  This option enables a KUnit test checking that KCSAN detects data
+	  races in Rust code. The test needs at least 2 CPUs to reliably
+	  observe a data race.
+
+	  If unsure, say N.
+
 endif
rust/kernel/kcsan_test.rs new file +137 -0

137 lines not on this page · raw message

rust/kernel/lib.rs +2 -0
index 4d5c96ddc49c..009efda9e7a4 100644
@@ -94,6 +94,8 @@
 pub mod iov;
 pub mod irq;
 pub mod jump_label;
+#[cfg(CONFIG_RUST_KCSAN_KUNIT_TEST)]
+mod kcsan_test;
 #[cfg(CONFIG_KUNIT)]
 pub mod kunit;
 pub mod list;
Signature · 1 line
2.56.0.385.gd3acb90ef8-goog
#2 lore raw

Re: [PATCH v2 2/2] rust: kcsan: add KUnit test for data race detection

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [High] kunit: rust: KCSAN test stalls and spuriously fails on UP/PREEMPT_NONE --

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] kunit: rust: KCSAN test stalls and spuriously fails on UP/PREEMPT_NONE --

commit 8335c11e7a640669968685ad30c1312fe11f4a68
Author: Marco Elver <[email protected]>

rust: kcsan: add KUnit test for data race detection

Add a minimal KUnit test that checks that KCSAN detects data races in Rust code: two threads concurrently increment a shared counter without synchronization, and a probe on the console tracepoint waits for the KCSAN report.

rust/kernel/Kconfig.test config RUST_TIME_KUNIT_TEST · line 109

Quoted 11 lines
rust/kernel/Kconfig.test +3 -0
index 3bf4194f28c7c..26bcd91a48211 100644
@@ -104,4 +104,15 @@ config RUST_TIME_KUNIT_TEST
 
 	  If unsure, say N.
 
+config RUST_KCSAN_KUNIT_TEST
+	bool "KUnit test for KCSAN instrumentation of Rust code" if !KUNIT_ALL_TESTS
+	depends on KCSAN && HAVE_KCSAN_RUSTC && TRACEPOINTS

[Severity: High]
Should the RUST_KCSAN_KUNIT_TEST configuration in rust/kernel/Kconfig.test include a depends on SMP requirement?

The help text notes that the test requires at least 2 CPUs to reliably observe a data race. Without an SMP dependency, the test can be built and may spuriously fail on UP configurations.

Quoted 15 lines
rust/kernel/Kconfig.test +8 -0
+	default KUNIT_ALL_TESTS
+	help
+	  This option enables a KUnit test checking that KCSAN detects data
+	  races in Rust code. The test needs at least 2 CPUs to reliably
+	  observe a data race.
+
+	  If unsure, say N.
+
 endif
rust/kernel/kcsan_test.rs new file +0 -0
new file mode 100644
index 0000000000000..24810cf2970a0

[ ... ]

Quoted 2 lines
rust/kernel/kcsan_test.rs new file +2 -0
+    #[test]
+    fn data_race() -> Result {

[ ... ]

rust/kernel/kcsan_test.rs line 128

Quoted 12 lines
rust/kernel/kcsan_test.rs new file +12 -0
+        let worker = ctx.clone();
+        workqueue::system_unbound().try_spawn(GFP_KERNEL, move || {
+            while !worker.stop.load(Relaxed) {
+                racy_inc();
+            }
+            worker.done.complete_all();
+        })?;
+
+        let start = Instant::<Monotonic>::now();
+        while !OBSERVED.load(Relaxed) && start.elapsed() < Delta::from_secs(10) {
+            racy_inc();
+        }

[Severity: High]
Could this loop in data_race() lead to a CPU stall or spurious test failure on uniprocessor systems or when PREEMPT_NONE is set?

The main thread spins tightly in kernel mode for up to 10 seconds without yielding. If the worker thread is scheduled on the same CPU, it is prevented from executing.

After 10 seconds, the main thread exits the spin loop, sets the stop flag, and waits for completion. The worker finally runs, sees the stop flag immediately, and exits without performing any concurrent increments. The assert!(OBSERVED.load(Relaxed)) assertion will then spuriously fail.

Would it make sense to yield the CPU during this loop, for example, using cond_resched()?

Quoted 9 lines
rust/kernel/kcsan_test.rs new file +9 -0
+
+        ctx.stop.store(true, Relaxed);
+        ctx.done.wait_for_completion();
+        drop(_unregister);
+
+        assert!(OBSERVED.load(Relaxed));
+        Ok(())
+    }
+}
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
author maintainer
#3 lore raw

Re: [PATCH v2 2/2] rust: kcsan: add KUnit test for data race detection

Sigh, I'm repeating myself against a bot:
https://lore.kernel.org/all/CANpmjNPDKZKCWc0ubCGATLE05gwefwpR=n4D6E0FKAvsi9DCVw@mail.gmail.com/

Quoted 31 lines · sashiko-bot via kasan-dev

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] kunit: rust: KCSAN test stalls and spuriously fails on UP/PREEMPT_NONE --

commit 8335c11e7a640669968685ad30c1312fe11f4a68
Author: Marco Elver <[email protected]>

rust: kcsan: add KUnit test for data race detection

Add a minimal KUnit test that checks that KCSAN detects data races in Rust code: two threads concurrently increment a shared counter without synchronization, and a probe on the console tracepoint waits for the KCSAN report.

rust/kernel/Kconfig.test config RUST_TIME_KUNIT_TEST · line 109

Quoted 11 lines
rust/kernel/Kconfig.test +3 -0
index 3bf4194f28c7c..26bcd91a48211 100644
@@ -104,4 +104,15 @@ config RUST_TIME_KUNIT_TEST
 
        If unsure, say N.
 
+config RUST_KCSAN_KUNIT_TEST
+     bool "KUnit test for KCSAN instrumentation of Rust code" if !KUNIT_ALL_TESTS
+     depends on KCSAN && HAVE_KCSAN_RUSTC && TRACEPOINTS

[Severity: High]
Should the RUST_KCSAN_KUNIT_TEST configuration in rust/kernel/Kconfig.test include a depends on SMP requirement?

Quoted 3 lines

The help text notes that the test requires at least 2 CPUs to reliably observe a data race. Without an SMP dependency, the test can be built and may spuriously fail on UP configurations.

That's the point - if someone configures KCSAN and runs it on a system where we won't detect any data races, this test is the canary in the coalmine. The test will fail loudly and whoever configured their system (or VM) will know something is off. That's a good thing.

Quoted 40 lines
Quoted 15 lines
rust/kernel/Kconfig.test +8 -0
+     default KUNIT_ALL_TESTS
+     help
+       This option enables a KUnit test checking that KCSAN detects data
+       races in Rust code. The test needs at least 2 CPUs to reliably
+       observe a data race.
+
+       If unsure, say N.
+
 endif
rust/kernel/kcsan_test.rs new file +0 -0
new file mode 100644
index 0000000000000..24810cf2970a0

[ ... ]

Quoted 2 lines
rust/kernel/kcsan_test.rs new file +2 -0
+    #[test]
+    fn data_race() -> Result {

[ ... ]

rust/kernel/kcsan_test.rs line 128

Quoted 12 lines
rust/kernel/kcsan_test.rs new file +12 -0
+        let worker = ctx.clone();
+        workqueue::system_unbound().try_spawn(GFP_KERNEL, move || {
+            while !worker.stop.load(Relaxed) {
+                racy_inc();
+            }
+            worker.done.complete_all();
+        })?;
+
+        let start = Instant::<Monotonic>::now();
+        while !OBSERVED.load(Relaxed) && start.elapsed() < Delta::from_secs(10) {
+            racy_inc();
+        }

[Severity: High]
Could this loop in data_race() lead to a CPU stall or spurious test failure on uniprocessor systems or when PREEMPT_NONE is set?

The main thread spins tightly in kernel mode for up to 10 seconds without yielding. If the worker thread is scheduled on the same CPU, it is prevented from executing.

Quoted 7 lines

After 10 seconds, the main thread exits the spin loop, sets the stop flag, and waits for completion. The worker finally runs, sees the stop flag immediately, and exits without performing any concurrent increments. The assert!(OBSERVED.load(Relaxed)) assertion will then spuriously fail.

Would it make sense to yield the CPU during this loop, for example, using cond_resched()?

Where KCSAN works correctly, the data race is detected within milliseconds. Adding cond_resched() may in fact lead to the data race being detected much slower in the common case. If the worker is scheduled on the same CPU, the system is misconfigured (likely UP) and I want to see a test failure.

reviewer Acked-by on v2 1/2
#4 lore raw
In reply to Marco Elver
Quoted 24 lines · Marco Elver

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.

Quoted 6 lines

With this enabled we can detect data races in kernel Rust code and between Rust and C code.

Quoted 10 lines

---
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(-)
author maintainer
#5 lore raw
Quoted 26 lines · Gary Guo
Quoted 24 lines · Marco Elver

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.

Quoted 8 lines
Quoted 6 lines

With this enabled we can detect data races in kernel Rust code and between Rust and C code.

Thanks!

Which tree should this go through? The Rust tree? Probably too late for this cycle.

#6 lore raw
Quoted 3 lines · Marco Elver

Which tree should this go through? The Rust tree? Probably too late for this cycle.

I think you send KCSAN PRs, so I thought you would use that, thus the Acked-bys (I suspect others thought the same). But the Rust or Kbuild trees are also a possibility, yeah.

In any case, at least for Kbuild and Rust, the PRs were already sent (and, generally speaking, I would have expected a patch like this to be in the list for longer anyway).

Thanks!

Cheers,
Miguel

author maintainer
#7 lore raw
Quoted 9 lines · Miguel Ojeda
Quoted 3 lines · Marco Elver

Which tree should this go through? The Rust tree? Probably too late for this cycle.

I think you send KCSAN PRs, so I thought you would use that, thus the Acked-bys (I suspect others thought the same). But the Rust or Kbuild trees are also a possibility, yeah.

Happy to take it - but in the interest of avoiding conflicts, thought it might be better suited for the Rust (or Kbuild trees).

Quoted 3 lines

In any case, at least for Kbuild and Rust, the PRs were already sent (and, generally speaking, I would have expected a patch like this to be in the list for longer anyway).

Needs some -next time, too.

If I don't hear otherwise, I'll pick it up later this week / next week.

Thanks,
-- Marco

#8 lore raw
Quoted 3 lines · Marco Elver

Happy to take it - but in the interest of avoiding conflicts, thought it might be better suited for the Rust (or Kbuild trees).

Same -- happy to take it too next cycle, whatever you prefer.

Quoted 3 lines

Needs some -next time, too.

If I don't hear otherwise, I'll pick it up later this week / next week.

Do you mean for this cycle? Linus may not like that, but up to you of course.

Cheers,
Miguel

author maintainer
#9 lore raw
Quoted 6 lines · Miguel Ojeda
Quoted 3 lines · Marco Elver

Happy to take it - but in the interest of avoiding conflicts, thought it might be better suited for the Rust (or Kbuild trees).

Same -- happy to take it too next cycle, whatever you prefer.

If you think there's a low risk of conflict, I'll take it.

Quoted 5 lines
Quoted 3 lines

Needs some -next time, too.

If I don't hear otherwise, I'll pick it up later this week / next week.

Do you mean for this cycle? Linus may not like that, but up to you of course.

No, next cycle. I'd pick it up next week, push it to a branch the test robots see, and then update my -next branch after the current cycle.

Thanks,
-- Marco

#10 lore raw
Quoted 2 lines · Marco Elver

If you think there's a low risk of conflict, I'll take it.

Hard to predict everything... but conflicts are generally fine anyway.

Thanks!

Cheers,
Miguel