* [PATCH] kcsan: Detect cross-CPU races on {READ,WRITE}_ONCE_THIS_CPU()
@ 2026-11-23 13:51 Marco Elver
2026-11-24 9:53 ` KunWu Chan
0 siblings, 1 reply; 5+ messages in thread
From: Marco Elver @ 2026-11-23 13:51 UTC (permalink / raw)
To: elver
Cc: Paul E. McKenney, Alexander Potapenko, Boqun Feng, kasan-dev,
linux-kernel, Arnd Bergmann, Dmitry Vyukov, linux-arch,
KunWu Chan, Thomas Gleixner
READ_ONCE_THIS_CPU() and WRITE_ONCE_THIS_CPU() identify accesses to
per-CPU data that are intended to race only with other contexts on the
same CPU. So far they are plain READ_ONCE() and WRITE_ONCE(): no marked
accesses involving them are reported, incl. from other CPUs.
Make the new accessors assert exclusivity but ignore same-CPU accesses:
READ_ONCE_THIS_CPU() asserts no concurrent writers,
WRITE_ONCE_THIS_CPU() asserts no concurrent accesses.
For KCSAN to ignore racing accesses from other contexts on the same CPU,
introduce KCSAN_ACCESS_THIS_CPU, which makes KCSAN keep interrupts
disabled while the watchpoint is set up.
With that, any racing accesses (incl. marked ones) from other CPUs
involving READ_ONCE_THIS_CPU() and WRITE_ONCE_THIS_CPU() will be
reported as "KCSAN: assert: race ...".
Suggested-by: KunWu Chan <kunwu.chan@gmail.com>
Cc: Thomas Gleixner <tglx@kernel.org>
Cc: Paul E. McKenney <paulmck@kernel.org>
Signed-off-by: Marco Elver <elver@google.com>
---
include/asm-generic/rwonce.h | 22 +++++++--
include/linux/kcsan-checks.h | 1 +
kernel/kcsan/core.c | 6 ++-
kernel/kcsan/kcsan_test.c | 90 ++++++++++++++++++++++++++++++++++++
4 files changed, 114 insertions(+), 5 deletions(-)
diff --git a/include/asm-generic/rwonce.h b/include/asm-generic/rwonce.h
index 3dc65a3310fb..e2d563096738 100644
--- a/include/asm-generic/rwonce.h
+++ b/include/asm-generic/rwonce.h
@@ -66,11 +66,25 @@ do { \
* which are intended to race only with accesses from other execution
* contexts on the same CPU, e.g. task and interrupt context.
*
- * For now, these are simple wrappers around READ_ONCE() and
- * WRITE_ONCE().
+ * With KCSAN, concurrent writes (for READ_ONCE_THIS_CPU()) or any concurrent
+ * accesses (for WRITE_ONCE_THIS_CPU()) from other CPUs are reported.
*/
-#define READ_ONCE_THIS_CPU(x) READ_ONCE(x)
-#define WRITE_ONCE_THIS_CPU(x, val) WRITE_ONCE(x, val)
+#define READ_ONCE_THIS_CPU(x) \
+({ \
+ auto __tc_p = &(x); \
+ kcsan_check_access(__tc_p, sizeof(*__tc_p), \
+ KCSAN_ACCESS_ASSERT | KCSAN_ACCESS_THIS_CPU);\
+ READ_ONCE(*__tc_p); \
+})
+
+#define WRITE_ONCE_THIS_CPU(x, val) \
+do { \
+ auto __tc_p = &(x); \
+ kcsan_check_access(__tc_p, sizeof(*__tc_p), \
+ KCSAN_ACCESS_ASSERT | KCSAN_ACCESS_WRITE | \
+ KCSAN_ACCESS_THIS_CPU); \
+ WRITE_ONCE(*__tc_p, val); \
+} while (0)
static __no_sanitize_or_inline
unsigned long __read_once_word_nocheck(const void *addr)
diff --git a/include/linux/kcsan-checks.h b/include/linux/kcsan-checks.h
index e135dacaa90f..afe50fe351fa 100644
--- a/include/linux/kcsan-checks.h
+++ b/include/linux/kcsan-checks.h
@@ -20,6 +20,7 @@
/* The following are special, and never due to compiler instrumentation. */
#define KCSAN_ACCESS_ASSERT (1 << 3) /* Access is an assertion. */
#define KCSAN_ACCESS_SCOPED (1 << 4) /* Access is a scoped access. */
+#define KCSAN_ACCESS_THIS_CPU (1 << 5) /* Ignore races from same-CPU accesses. */
/*
* __kcsan_*: Always calls into the runtime when KCSAN is enabled. This may be used
diff --git a/kernel/kcsan/core.c b/kernel/kcsan/core.c
index 26755add7cdb..51340bf1d4a3 100644
--- a/kernel/kcsan/core.c
+++ b/kernel/kcsan/core.c
@@ -567,9 +567,13 @@ kcsan_setup_watchpoint(const volatile void *ptr, size_t size, int type, unsigned
* 1. Races of the reordered access with interrupts. To avoid, if
* the current access is reorder_access, disable interrupts.
* 2. Avoid races of scoped accesses from nested interrupts (below).
+ *
+ * Similarly, KCSAN_ACCESS_THIS_CPU accesses may only race with
+ * interrupts on the same CPU: disable interrupts, so that only races
+ * with other CPUs are detected.
*/
is_reorder_access = find_reorder_access(ctx, ptr, size, type, ip);
- if (is_reorder_access)
+ if (is_reorder_access || (type & KCSAN_ACCESS_THIS_CPU))
interrupt_watcher = false;
/*
* Avoid races of scoped accesses from nested interrupts (or scheduler).
diff --git a/kernel/kcsan/kcsan_test.c b/kernel/kcsan/kcsan_test.c
index 1c8853f14dae..3e9689492abe 100644
--- a/kernel/kcsan/kcsan_test.c
+++ b/kernel/kcsan/kcsan_test.c
@@ -22,6 +22,7 @@
#include <linux/kcsan-checks.h>
#include <linux/kernel.h>
#include <linux/mutex.h>
+#include <linux/percpu.h>
#include <linux/sched.h>
#include <linux/seqlock.h>
#include <linux/spinlock.h>
@@ -337,6 +338,7 @@ static struct {
long val[8];
} test_struct;
static long __data_racy test_data_racy;
+static DEFINE_PER_CPU(long, test_pcpu_var);
static DEFINE_SEQLOCK(test_seqlock);
static DEFINE_SPINLOCK(test_spinlock);
static DEFINE_MUTEX(test_mutex);
@@ -403,6 +405,34 @@ static noinline void test_kernel_assert_access(void)
ASSERT_EXCLUSIVE_ACCESS(test_var);
}
+static noinline void test_kernel_read_this_cpu(void)
+{
+ sink_value(READ_ONCE_THIS_CPU(test_var));
+}
+
+static noinline void test_kernel_write_this_cpu(void)
+{
+ WRITE_ONCE_THIS_CPU(test_var, 42);
+}
+
+static noinline void test_kernel_write_this_cpu_local(void)
+{
+ preempt_disable();
+ WRITE_ONCE_THIS_CPU(*this_cpu_ptr(&test_pcpu_var), 1);
+ preempt_enable();
+}
+
+static noinline void test_kernel_reset_this_cpu_local(void)
+{
+ long *p;
+
+ preempt_disable();
+ p = this_cpu_ptr(&test_pcpu_var);
+ if (READ_ONCE_THIS_CPU(*p))
+ WRITE_ONCE_THIS_CPU(*p, 0);
+ preempt_enable();
+}
+
#define TEST_CHANGE_BITS 0xff00ff00
static noinline void test_kernel_change_bits(void)
@@ -1223,6 +1253,63 @@ static void test_assert_exclusive_access_scoped(struct kunit *test)
KUNIT_EXPECT_FALSE(test, match_expect_inscope);
}
+/* Test that remote writes racing with READ_ONCE_THIS_CPU() are reported. */
+__no_kcsan
+static void test_this_cpu_remote_write(struct kunit *test)
+{
+ struct expect_report expect = {
+ .access = {
+ { test_kernel_read_this_cpu, &test_var, sizeof(test_var), KCSAN_ACCESS_ASSERT },
+ { test_kernel_write_atomic, &test_var, sizeof(test_var), KCSAN_ACCESS_WRITE | KCSAN_ACCESS_ATOMIC },
+ },
+ };
+ bool match_expect = false;
+
+ KCSAN_TEST_REQUIRES(test, !IS_ENABLED(CONFIG_KCSAN_IGNORE_ATOMICS));
+
+ begin_test_checks(test_kernel_read_this_cpu, test_kernel_write_atomic);
+ do {
+ match_expect = report_matches(&expect);
+ } while (!end_test_checks(match_expect));
+ KUNIT_EXPECT_TRUE(test, match_expect);
+}
+
+/* Test that remote reads racing with WRITE_ONCE_THIS_CPU() are reported. */
+__no_kcsan
+static void test_this_cpu_remote_read(struct kunit *test)
+{
+ struct expect_report expect = {
+ .access = {
+ { test_kernel_write_this_cpu, &test_var, sizeof(test_var), KCSAN_ACCESS_ASSERT | KCSAN_ACCESS_WRITE },
+ { test_kernel_read, &test_var, sizeof(test_var), 0 },
+ },
+ };
+ bool match_expect = false;
+
+ begin_test_checks(test_kernel_write_this_cpu, test_kernel_read);
+ do {
+ match_expect = report_matches(&expect);
+ } while (!end_test_checks(match_expect));
+ KUNIT_EXPECT_TRUE(test, match_expect);
+}
+
+/*
+ * Test that races between task and interrupt on the same CPU's instance are not
+ * reported; the test threads' timers access the same per-CPU instance as the
+ * interrupted thread. Only meaningful with CONFIG_KCSAN_INTERRUPT_WATCHER=y.
+ */
+__no_kcsan
+static void test_this_cpu_local_noreport(struct kunit *test)
+{
+ bool match_never = false;
+
+ begin_test_checks(test_kernel_write_this_cpu_local, test_kernel_reset_this_cpu_local);
+ do {
+ match_never = report_available();
+ } while (!end_test_checks(match_never));
+ KUNIT_EXPECT_FALSE(test, match_never);
+}
+
/*
* jiffies is special (declared to be volatile) and its accesses are typically
* not marked; this test ensures that the compiler nor KCSAN gets confused about
@@ -1485,6 +1572,9 @@ static struct kunit_case kcsan_test_cases[] = {
KCSAN_KUNIT_CASE(test_assert_exclusive_bits_nochange),
KCSAN_KUNIT_CASE(test_assert_exclusive_writer_scoped),
KCSAN_KUNIT_CASE(test_assert_exclusive_access_scoped),
+ KCSAN_KUNIT_CASE(test_this_cpu_remote_write),
+ KCSAN_KUNIT_CASE(test_this_cpu_remote_read),
+ KCSAN_KUNIT_CASE(test_this_cpu_local_noreport),
KCSAN_KUNIT_CASE(test_jiffies_noreport),
KCSAN_KUNIT_CASE(test_seqlock_noreport),
KCSAN_KUNIT_CASE(test_atomic_builtins),
--
2.56.1.750.gf70dc682cf-goog
^ permalink raw reply [flat|nested] 5+ messages in thread* Re: [PATCH] kcsan: Detect cross-CPU races on {READ,WRITE}_ONCE_THIS_CPU() 2026-11-23 13:51 [PATCH] kcsan: Detect cross-CPU races on {READ,WRITE}_ONCE_THIS_CPU() Marco Elver @ 2026-11-24 9:53 ` KunWu Chan 2026-11-24 10:23 ` Marco Elver 0 siblings, 1 reply; 5+ messages in thread From: KunWu Chan @ 2026-11-24 9:53 UTC (permalink / raw) To: Marco Elver Cc: Paul E. McKenney, Alexander Potapenko, Boqun Feng, kasan-dev, linux-kernel, Arnd Bergmann, Dmitry Vyukov, linux-arch, Thomas Gleixner On Mon, Nov 23, 2026 at 9:51 PM Marco Elver <elver@google.com> wrote: > > READ_ONCE_THIS_CPU() and WRITE_ONCE_THIS_CPU() identify accesses to > per-CPU data that are intended to race only with other contexts on the > same CPU. So far they are plain READ_ONCE() and WRITE_ONCE(): no marked > accesses involving them are reported, incl. from other CPUs. > > Make the new accessors assert exclusivity but ignore same-CPU accesses: > READ_ONCE_THIS_CPU() asserts no concurrent writers, > WRITE_ONCE_THIS_CPU() asserts no concurrent accesses. > > For KCSAN to ignore racing accesses from other contexts on the same CPU, > introduce KCSAN_ACCESS_THIS_CPU, which makes KCSAN keep interrupts > disabled while the watchpoint is set up. > > With that, any racing accesses (incl. marked ones) from other CPUs > involving READ_ONCE_THIS_CPU() and WRITE_ONCE_THIS_CPU() will be > reported as "KCSAN: assert: race ...". > > Suggested-by: KunWu Chan <kunwu.chan@gmail.com> > Cc: Thomas Gleixner <tglx@kernel.org> > Cc: Paul E. McKenney <paulmck@kernel.org> > Signed-off-by: Marco Elver <elver@google.com> > --- > include/asm-generic/rwonce.h | 22 +++++++-- > include/linux/kcsan-checks.h | 1 + > kernel/kcsan/core.c | 6 ++- > kernel/kcsan/kcsan_test.c | 90 ++++++++++++++++++++++++++++++++++++ > 4 files changed, 114 insertions(+), 5 deletions(-) > > diff --git a/include/asm-generic/rwonce.h b/include/asm-generic/rwonce.h > index 3dc65a3310fb..e2d563096738 100644 > --- a/include/asm-generic/rwonce.h > +++ b/include/asm-generic/rwonce.h > @@ -66,11 +66,25 @@ do { \ > * which are intended to race only with accesses from other execution > * contexts on the same CPU, e.g. task and interrupt context. > * > - * For now, these are simple wrappers around READ_ONCE() and > - * WRITE_ONCE(). > + * With KCSAN, concurrent writes (for READ_ONCE_THIS_CPU()) or any concurrent > + * accesses (for WRITE_ONCE_THIS_CPU()) from other CPUs are reported. > */ > -#define READ_ONCE_THIS_CPU(x) READ_ONCE(x) > -#define WRITE_ONCE_THIS_CPU(x, val) WRITE_ONCE(x, val) > +#define READ_ONCE_THIS_CPU(x) \ > +({ \ > + auto __tc_p = &(x); \ > + kcsan_check_access(__tc_p, sizeof(*__tc_p), \ > + KCSAN_ACCESS_ASSERT | KCSAN_ACCESS_THIS_CPU);\ > + READ_ONCE(*__tc_p); \ > +}) > + > +#define WRITE_ONCE_THIS_CPU(x, val) \ > +do { \ > + auto __tc_p = &(x); \ > + kcsan_check_access(__tc_p, sizeof(*__tc_p), \ > + KCSAN_ACCESS_ASSERT | KCSAN_ACCESS_WRITE | \ > + KCSAN_ACCESS_THIS_CPU); \ > + WRITE_ONCE(*__tc_p, val); \ > +} while (0) > > static __no_sanitize_or_inline > unsigned long __read_once_word_nocheck(const void *addr) > diff --git a/include/linux/kcsan-checks.h b/include/linux/kcsan-checks.h > index e135dacaa90f..afe50fe351fa 100644 > --- a/include/linux/kcsan-checks.h > +++ b/include/linux/kcsan-checks.h > @@ -20,6 +20,7 @@ > /* The following are special, and never due to compiler instrumentation. */ > #define KCSAN_ACCESS_ASSERT (1 << 3) /* Access is an assertion. */ > #define KCSAN_ACCESS_SCOPED (1 << 4) /* Access is a scoped access. */ > +#define KCSAN_ACCESS_THIS_CPU (1 << 5) /* Ignore races from same-CPU accesses. */ > > /* > * __kcsan_*: Always calls into the runtime when KCSAN is enabled. This may be used > diff --git a/kernel/kcsan/core.c b/kernel/kcsan/core.c > index 26755add7cdb..51340bf1d4a3 100644 > --- a/kernel/kcsan/core.c > +++ b/kernel/kcsan/core.c > @@ -567,9 +567,13 @@ kcsan_setup_watchpoint(const volatile void *ptr, size_t size, int type, unsigned > * 1. Races of the reordered access with interrupts. To avoid, if > * the current access is reorder_access, disable interrupts. > * 2. Avoid races of scoped accesses from nested interrupts (below). > + * > + * Similarly, KCSAN_ACCESS_THIS_CPU accesses may only race with > + * interrupts on the same CPU: disable interrupts, so that only races > + * with other CPUs are detected. > */ > is_reorder_access = find_reorder_access(ctx, ptr, size, type, ip); > - if (is_reorder_access) > + if (is_reorder_access || (type & KCSAN_ACCESS_THIS_CPU)) > interrupt_watcher = false; > /* > * Avoid races of scoped accesses from nested interrupts (or scheduler). > diff --git a/kernel/kcsan/kcsan_test.c b/kernel/kcsan/kcsan_test.c > index 1c8853f14dae..3e9689492abe 100644 > --- a/kernel/kcsan/kcsan_test.c > +++ b/kernel/kcsan/kcsan_test.c > @@ -22,6 +22,7 @@ > #include <linux/kcsan-checks.h> > #include <linux/kernel.h> > #include <linux/mutex.h> > +#include <linux/percpu.h> > #include <linux/sched.h> > #include <linux/seqlock.h> > #include <linux/spinlock.h> > @@ -337,6 +338,7 @@ static struct { > long val[8]; > } test_struct; > static long __data_racy test_data_racy; > +static DEFINE_PER_CPU(long, test_pcpu_var); > static DEFINE_SEQLOCK(test_seqlock); > static DEFINE_SPINLOCK(test_spinlock); > static DEFINE_MUTEX(test_mutex); > @@ -403,6 +405,34 @@ static noinline void test_kernel_assert_access(void) > ASSERT_EXCLUSIVE_ACCESS(test_var); > } > > +static noinline void test_kernel_read_this_cpu(void) > +{ > + sink_value(READ_ONCE_THIS_CPU(test_var)); > +} > + > +static noinline void test_kernel_write_this_cpu(void) > +{ > + WRITE_ONCE_THIS_CPU(test_var, 42); > +} > + > +static noinline void test_kernel_write_this_cpu_local(void) > +{ > + preempt_disable(); > + WRITE_ONCE_THIS_CPU(*this_cpu_ptr(&test_pcpu_var), 1); > + preempt_enable(); > +} > + > +static noinline void test_kernel_reset_this_cpu_local(void) > +{ > + long *p; > + > + preempt_disable(); > + p = this_cpu_ptr(&test_pcpu_var); > + if (READ_ONCE_THIS_CPU(*p)) > + WRITE_ONCE_THIS_CPU(*p, 0); > + preempt_enable(); > +} > + > #define TEST_CHANGE_BITS 0xff00ff00 > > static noinline void test_kernel_change_bits(void) > @@ -1223,6 +1253,63 @@ static void test_assert_exclusive_access_scoped(struct kunit *test) > KUNIT_EXPECT_FALSE(test, match_expect_inscope); > } > > +/* Test that remote writes racing with READ_ONCE_THIS_CPU() are reported. */ > +__no_kcsan > +static void test_this_cpu_remote_write(struct kunit *test) > +{ > + struct expect_report expect = { > + .access = { > + { test_kernel_read_this_cpu, &test_var, sizeof(test_var), KCSAN_ACCESS_ASSERT }, > + { test_kernel_write_atomic, &test_var, sizeof(test_var), KCSAN_ACCESS_WRITE | KCSAN_ACCESS_ATOMIC }, > + }, > + }; > + bool match_expect = false; > + > + KCSAN_TEST_REQUIRES(test, !IS_ENABLED(CONFIG_KCSAN_IGNORE_ATOMICS)); > + > + begin_test_checks(test_kernel_read_this_cpu, test_kernel_write_atomic); > + do { > + match_expect = report_matches(&expect); > + } while (!end_test_checks(match_expect)); > + KUNIT_EXPECT_TRUE(test, match_expect); > +} > + > +/* Test that remote reads racing with WRITE_ONCE_THIS_CPU() are reported. */ > +__no_kcsan > +static void test_this_cpu_remote_read(struct kunit *test) > +{ > + struct expect_report expect = { > + .access = { > + { test_kernel_write_this_cpu, &test_var, sizeof(test_var), KCSAN_ACCESS_ASSERT | KCSAN_ACCESS_WRITE }, > + { test_kernel_read, &test_var, sizeof(test_var), 0 }, > + }, > + }; > + bool match_expect = false; > + > + begin_test_checks(test_kernel_write_this_cpu, test_kernel_read); > + do { > + match_expect = report_matches(&expect); > + } while (!end_test_checks(match_expect)); > + KUNIT_EXPECT_TRUE(test, match_expect); > +} > + > +/* > + * Test that races between task and interrupt on the same CPU's instance are not > + * reported; the test threads' timers access the same per-CPU instance as the > + * interrupted thread. Only meaningful with CONFIG_KCSAN_INTERRUPT_WATCHER=y. > + */ > +__no_kcsan > +static void test_this_cpu_local_noreport(struct kunit *test) > +{ > + bool match_never = false; > + > + begin_test_checks(test_kernel_write_this_cpu_local, test_kernel_reset_this_cpu_local); > + do { > + match_never = report_available(); > + } while (!end_test_checks(match_never)); > + KUNIT_EXPECT_FALSE(test, match_never); > +} > + > /* > * jiffies is special (declared to be volatile) and its accesses are typically > * not marked; this test ensures that the compiler nor KCSAN gets confused about > @@ -1485,6 +1572,9 @@ static struct kunit_case kcsan_test_cases[] = { > KCSAN_KUNIT_CASE(test_assert_exclusive_bits_nochange), > KCSAN_KUNIT_CASE(test_assert_exclusive_writer_scoped), > KCSAN_KUNIT_CASE(test_assert_exclusive_access_scoped), > + KCSAN_KUNIT_CASE(test_this_cpu_remote_write), > + KCSAN_KUNIT_CASE(test_this_cpu_remote_read), > + KCSAN_KUNIT_CASE(test_this_cpu_local_noreport), > KCSAN_KUNIT_CASE(test_jiffies_noreport), > KCSAN_KUNIT_CASE(test_seqlock_noreport), > KCSAN_KUNIT_CASE(test_atomic_builtins), > -- > 2.56.1.750.gf70dc682cf-goog > Hi Marco, I tested the patch on next-20261123. A 60-second rcutorture run with strict KCSAN and interruptible watchers completed successfully. I saw no reports involving got_idle_tick or tick_nohz_handler. There was one report involving move_linked_works / queue_work_on. The latest KUnit run with kunit.filter_glob=kcsan.test_this_cpu_* gave: local_noreport: 7/7 passed remote_write: 6/7 passed; threads=2 failed setup with -EBUSY ("rcu running") remote_read: 5/7 passed; threads=16 and 32 failed The -EBUSY log shows that rcutorture was already running. The remote_read failure at 32 threads occurred in both runs. I'll rerun the KUnit cases with rcutorture disabled and check the console for the failed cases. One test question: should local_noreport skip when interruptible watchers are disabled? In that setup the ordinary watchpoint path already masks local IRQs, so a pass wouldn't exercise the new THIS_CPU behavior. Thanks, Kunwu ^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH] kcsan: Detect cross-CPU races on {READ,WRITE}_ONCE_THIS_CPU() 2026-11-24 9:53 ` KunWu Chan @ 2026-11-24 10:23 ` Marco Elver 2026-11-25 9:40 ` KunWu Chan 0 siblings, 1 reply; 5+ messages in thread From: Marco Elver @ 2026-11-24 10:23 UTC (permalink / raw) To: KunWu Chan Cc: Paul E. McKenney, Alexander Potapenko, Boqun Feng, kasan-dev, linux-kernel, Arnd Bergmann, Dmitry Vyukov, linux-arch, Thomas Gleixner On Tue, 24 Nov 2026 at 10:54, KunWu Chan <kunwu.chan@gmail.com> wrote: > > On Mon, Nov 23, 2026 at 9:51 PM Marco Elver <elver@google.com> wrote: > > > > READ_ONCE_THIS_CPU() and WRITE_ONCE_THIS_CPU() identify accesses to > > per-CPU data that are intended to race only with other contexts on the > > same CPU. So far they are plain READ_ONCE() and WRITE_ONCE(): no marked > > accesses involving them are reported, incl. from other CPUs. > > > > Make the new accessors assert exclusivity but ignore same-CPU accesses: > > READ_ONCE_THIS_CPU() asserts no concurrent writers, > > WRITE_ONCE_THIS_CPU() asserts no concurrent accesses. > > > > For KCSAN to ignore racing accesses from other contexts on the same CPU, > > introduce KCSAN_ACCESS_THIS_CPU, which makes KCSAN keep interrupts > > disabled while the watchpoint is set up. > > > > With that, any racing accesses (incl. marked ones) from other CPUs > > involving READ_ONCE_THIS_CPU() and WRITE_ONCE_THIS_CPU() will be > > reported as "KCSAN: assert: race ...". > > > > Suggested-by: KunWu Chan <kunwu.chan@gmail.com> > > Cc: Thomas Gleixner <tglx@kernel.org> > > Cc: Paul E. McKenney <paulmck@kernel.org> > > Signed-off-by: Marco Elver <elver@google.com> > > --- > > include/asm-generic/rwonce.h | 22 +++++++-- > > include/linux/kcsan-checks.h | 1 + > > kernel/kcsan/core.c | 6 ++- > > kernel/kcsan/kcsan_test.c | 90 ++++++++++++++++++++++++++++++++++++ > > 4 files changed, 114 insertions(+), 5 deletions(-) [...] > I tested the patch on next-20261123. A 60-second rcutorture run > with strict KCSAN and interruptible watchers completed successfully. > I saw no reports involving got_idle_tick or tick_nohz_handler. > There was one report involving move_linked_works / queue_work_on. > > The latest KUnit run with kunit.filter_glob=kcsan.test_this_cpu_* gave: > > local_noreport: 7/7 passed > remote_write: 6/7 passed; threads=2 failed setup with -EBUSY > ("rcu running") > remote_read: 5/7 passed; threads=16 and 32 failed > > The -EBUSY log shows that rcutorture was already running. The > remote_read failure at 32 threads occurred in both runs. I'll > rerun the KUnit cases with rcutorture disabled and check the > console for the failed cases. Thanks! > One test question: should local_noreport skip when interruptible > watchers are disabled? In that setup the ordinary watchpoint > path already masks local IRQs, so a pass wouldn't exercise the > new THIS_CPU behavior. The test will pass in both configurations, so a regression in either would be caught. Disabling is not necessary. More coverage is always better, even if it may not seem useful today. Thanks, -- Marco ^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH] kcsan: Detect cross-CPU races on {READ,WRITE}_ONCE_THIS_CPU() 2026-11-24 10:23 ` Marco Elver @ 2026-11-25 9:40 ` KunWu Chan 2026-11-25 22:48 ` Marco Elver 0 siblings, 1 reply; 5+ messages in thread From: KunWu Chan @ 2026-11-25 9:40 UTC (permalink / raw) To: Marco Elver Cc: Paul E. McKenney, Alexander Potapenko, Boqun Feng, kasan-dev, linux-kernel, Arnd Bergmann, Dmitry Vyukov, linux-arch, Thomas Gleixner On Tue, Nov 24, 2026 at 6:24 PM Marco Elver <elver@google.com> wrote: > > On Tue, 24 Nov 2026 at 10:54, KunWu Chan <kunwu.chan@gmail.com> wrote: > > > > On Mon, Nov 23, 2026 at 9:51 PM Marco Elver <elver@google.com> wrote: > > > > > > READ_ONCE_THIS_CPU() and WRITE_ONCE_THIS_CPU() identify accesses to > > > per-CPU data that are intended to race only with other contexts on the > > > same CPU. So far they are plain READ_ONCE() and WRITE_ONCE(): no marked > > > accesses involving them are reported, incl. from other CPUs. > > > > > > Make the new accessors assert exclusivity but ignore same-CPU accesses: > > > READ_ONCE_THIS_CPU() asserts no concurrent writers, > > > WRITE_ONCE_THIS_CPU() asserts no concurrent accesses. > > > > > > For KCSAN to ignore racing accesses from other contexts on the same CPU, > > > introduce KCSAN_ACCESS_THIS_CPU, which makes KCSAN keep interrupts > > > disabled while the watchpoint is set up. > > > > > > With that, any racing accesses (incl. marked ones) from other CPUs > > > involving READ_ONCE_THIS_CPU() and WRITE_ONCE_THIS_CPU() will be > > > reported as "KCSAN: assert: race ...". > > > > > > Suggested-by: KunWu Chan <kunwu.chan@gmail.com> > > > Cc: Thomas Gleixner <tglx@kernel.org> > > > Cc: Paul E. McKenney <paulmck@kernel.org> > > > Signed-off-by: Marco Elver <elver@google.com> > > > --- > > > include/asm-generic/rwonce.h | 22 +++++++-- > > > include/linux/kcsan-checks.h | 1 + > > > kernel/kcsan/core.c | 6 ++- > > > kernel/kcsan/kcsan_test.c | 90 ++++++++++++++++++++++++++++++++++++ > > > 4 files changed, 114 insertions(+), 5 deletions(-) > [...] > > I tested the patch on next-20261123. A 60-second rcutorture run > > with strict KCSAN and interruptible watchers completed successfully. > > I saw no reports involving got_idle_tick or tick_nohz_handler. > > There was one report involving move_linked_works / queue_work_on. > > > > The latest KUnit run with kunit.filter_glob=kcsan.test_this_cpu_* gave: > > > > local_noreport: 7/7 passed > > remote_write: 6/7 passed; threads=2 failed setup with -EBUSY > > ("rcu running") > > remote_read: 5/7 passed; threads=16 and 32 failed > > > > The -EBUSY log shows that rcutorture was already running. The > > remote_read failure at 32 threads occurred in both runs. I'll > > rerun the KUnit cases with rcutorture disabled and check the > > console for the failed cases. > > Thanks! > > > One test question: should local_noreport skip when interruptible > > watchers are disabled? In that setup the ordinary watchpoint > > path already masks local IRQs, so a pass wouldn't exercise the > > new THIS_CPU behavior. > > The test will pass in both configurations, so a regression in either > would be caught. Disabling is not necessary. More coverage is always > better, even if it may not seem useful today. > > Thanks, > -- Marco Hi Marco, Makes sense to keep the test in both configurations. I reran the three new KUnit cases on next-20261123 with rcutorture disabled, using the same image with interruptible watchers on and off. Both runs passed all three cases at all seven thread counts (21/21 per run, no skips). With watchers on, I checked the console reports for both remote cases. The two accesses were on different CPUs in each report. The earlier -EBUSY and remote_read failures did not reproduce. The separate 60-second rcutorture run also completed without any reports involving got_idle_tick or tick_nohz_handler. One question about the IRQ-off path. A local NMI can still write to the watched address while its KCSAN checks are disabled. If that changes the sampled value and no other access hits the watchpoint, is an unknown-origin report expected for THIS_CPU? Tested-by: Kunwu Chan <kunwu.chan@gmail.com> Thanks, Kunwu ^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH] kcsan: Detect cross-CPU races on {READ,WRITE}_ONCE_THIS_CPU() 2026-11-25 9:40 ` KunWu Chan @ 2026-11-25 22:48 ` Marco Elver 0 siblings, 0 replies; 5+ messages in thread From: Marco Elver @ 2026-11-25 22:48 UTC (permalink / raw) To: KunWu Chan Cc: Paul E. McKenney, Alexander Potapenko, Boqun Feng, kasan-dev, linux-kernel, Arnd Bergmann, Dmitry Vyukov, linux-arch, Thomas Gleixner On Wed, Nov 25, 2026 at 05:40PM +0800, KunWu Chan wrote: [...] > Makes sense to keep the test in both configurations. > > I reran the three new KUnit cases on next-20261123 with rcutorture > disabled, using the same image with interruptible watchers on and > off. Both runs passed all three cases at all seven thread counts > (21/21 per run, no skips). > > With watchers on, I checked the console reports for both remote > cases. The two accesses were on different CPUs in each report. > The earlier -EBUSY and remote_read failures did not reproduce. > The separate 60-second rcutorture run also completed without any > reports involving got_idle_tick or tick_nohz_handler. > > One question about the IRQ-off path. A local NMI can still write > to the watched address while its KCSAN checks are disabled. If > that changes the sampled value and no other access hits the > watchpoint, is an unknown-origin report expected for THIS_CPU? Yeah, but we can suppress it: diff --git a/kernel/kcsan/core.c b/kernel/kcsan/core.c index 51340bf1d4a3..b3e0c2627712 100644 --- a/kernel/kcsan/core.c +++ b/kernel/kcsan/core.c @@ -689,7 +689,9 @@ kcsan_setup_watchpoint(const volatile void *ptr, size_t size, int type, unsigned kcsan_report_known_origin(ptr, size, type, ip, value_change, watchpoint - watchpoints, old, new, access_mask); - } else if (value_change == KCSAN_VALUE_CHANGE_TRUE) { + } else if (value_change == KCSAN_VALUE_CHANGE_TRUE && + /* May be due to a permitted write from NMI on this CPU. */ + !(type & KCSAN_ACCESS_THIS_CPU)) { /* Inferring a race, since the value should not have changed. */ atomic_long_inc(&kcsan_counters[KCSAN_COUNTER_RACES_UNKNOWN_ORIGIN]); The downside is not detecting races with uninstrumented code, but usage of *_ONCE_THIS_CPU() is still rare, and if someone wants that coverage back, we can think of something better. In general, I find the unknown- origin reports not all that helpful in many cases, so my preference is to just ignore those. > Tested-by: Kunwu Chan <kunwu.chan@gmail.com> Below is the complete v2 patch with the unknown-origin suppression. Thanks, -- Marco ------ >8 ------ From: Marco Elver <elver@google.com> Date: Mon, 23 Nov 2026 11:03:32 +0000 Subject: [PATCH v2] kcsan: Detect cross-CPU races on {READ,WRITE}_ONCE_THIS_CPU() READ_ONCE_THIS_CPU() and WRITE_ONCE_THIS_CPU() identify accesses to per-CPU data that are intended to race only with other contexts on the same CPU. So far they are plain READ_ONCE() and WRITE_ONCE(): no marked accesses involving them are reported, incl. from other CPUs. Make the new accessors assert exclusivity but ignore same-CPU accesses: READ_ONCE_THIS_CPU() asserts no concurrent writers, WRITE_ONCE_THIS_CPU() asserts no concurrent accesses. For KCSAN to ignore racing accesses from other contexts on the same CPU, introduce KCSAN_ACCESS_THIS_CPU, which makes KCSAN keep interrupts disabled while the watchpoint is set up. A value change without a racing access is not reported for these accesses, since it may be due to a permitted write from an NMI on the same CPU. With that, any racing accesses (incl. marked ones) from other CPUs involving READ_ONCE_THIS_CPU() and WRITE_ONCE_THIS_CPU() will be reported as "KCSAN: assert: race ...". Suggested-by: KunWu Chan <kunwu.chan@gmail.com> Tested-by: Kunwu Chan <kunwu.chan@gmail.com> Cc: Thomas Gleixner <tglx@kernel.org> Cc: Paul E. McKenney <paulmck@kernel.org> Signed-off-by: Marco Elver <elver@google.com> --- include/asm-generic/rwonce.h | 22 +++++++-- include/linux/kcsan-checks.h | 1 + kernel/kcsan/core.c | 10 +++- kernel/kcsan/kcsan_test.c | 90 ++++++++++++++++++++++++++++++++++++ 4 files changed, 117 insertions(+), 6 deletions(-) diff --git a/include/asm-generic/rwonce.h b/include/asm-generic/rwonce.h index 3dc65a3310fb..e2d563096738 100644 --- a/include/asm-generic/rwonce.h +++ b/include/asm-generic/rwonce.h @@ -66,11 +66,25 @@ do { \ * which are intended to race only with accesses from other execution * contexts on the same CPU, e.g. task and interrupt context. * - * For now, these are simple wrappers around READ_ONCE() and - * WRITE_ONCE(). + * With KCSAN, concurrent writes (for READ_ONCE_THIS_CPU()) or any concurrent + * accesses (for WRITE_ONCE_THIS_CPU()) from other CPUs are reported. */ -#define READ_ONCE_THIS_CPU(x) READ_ONCE(x) -#define WRITE_ONCE_THIS_CPU(x, val) WRITE_ONCE(x, val) +#define READ_ONCE_THIS_CPU(x) \ +({ \ + auto __tc_p = &(x); \ + kcsan_check_access(__tc_p, sizeof(*__tc_p), \ + KCSAN_ACCESS_ASSERT | KCSAN_ACCESS_THIS_CPU);\ + READ_ONCE(*__tc_p); \ +}) + +#define WRITE_ONCE_THIS_CPU(x, val) \ +do { \ + auto __tc_p = &(x); \ + kcsan_check_access(__tc_p, sizeof(*__tc_p), \ + KCSAN_ACCESS_ASSERT | KCSAN_ACCESS_WRITE | \ + KCSAN_ACCESS_THIS_CPU); \ + WRITE_ONCE(*__tc_p, val); \ +} while (0) static __no_sanitize_or_inline unsigned long __read_once_word_nocheck(const void *addr) diff --git a/include/linux/kcsan-checks.h b/include/linux/kcsan-checks.h index e135dacaa90f..afe50fe351fa 100644 --- a/include/linux/kcsan-checks.h +++ b/include/linux/kcsan-checks.h @@ -20,6 +20,7 @@ /* The following are special, and never due to compiler instrumentation. */ #define KCSAN_ACCESS_ASSERT (1 << 3) /* Access is an assertion. */ #define KCSAN_ACCESS_SCOPED (1 << 4) /* Access is a scoped access. */ +#define KCSAN_ACCESS_THIS_CPU (1 << 5) /* Ignore races from same-CPU accesses. */ /* * __kcsan_*: Always calls into the runtime when KCSAN is enabled. This may be used diff --git a/kernel/kcsan/core.c b/kernel/kcsan/core.c index 26755add7cdb..b3e0c2627712 100644 --- a/kernel/kcsan/core.c +++ b/kernel/kcsan/core.c @@ -567,9 +567,13 @@ kcsan_setup_watchpoint(const volatile void *ptr, size_t size, int type, unsigned * 1. Races of the reordered access with interrupts. To avoid, if * the current access is reorder_access, disable interrupts. * 2. Avoid races of scoped accesses from nested interrupts (below). + * + * Similarly, KCSAN_ACCESS_THIS_CPU accesses may only race with + * interrupts on the same CPU: disable interrupts, so that only races + * with other CPUs are detected. */ is_reorder_access = find_reorder_access(ctx, ptr, size, type, ip); - if (is_reorder_access) + if (is_reorder_access || (type & KCSAN_ACCESS_THIS_CPU)) interrupt_watcher = false; /* * Avoid races of scoped accesses from nested interrupts (or scheduler). @@ -685,7 +689,9 @@ kcsan_setup_watchpoint(const volatile void *ptr, size_t size, int type, unsigned kcsan_report_known_origin(ptr, size, type, ip, value_change, watchpoint - watchpoints, old, new, access_mask); - } else if (value_change == KCSAN_VALUE_CHANGE_TRUE) { + } else if (value_change == KCSAN_VALUE_CHANGE_TRUE && + /* May be due to a permitted write from NMI on this CPU. */ + !(type & KCSAN_ACCESS_THIS_CPU)) { /* Inferring a race, since the value should not have changed. */ atomic_long_inc(&kcsan_counters[KCSAN_COUNTER_RACES_UNKNOWN_ORIGIN]); diff --git a/kernel/kcsan/kcsan_test.c b/kernel/kcsan/kcsan_test.c index 1c8853f14dae..3e9689492abe 100644 --- a/kernel/kcsan/kcsan_test.c +++ b/kernel/kcsan/kcsan_test.c @@ -22,6 +22,7 @@ #include <linux/kcsan-checks.h> #include <linux/kernel.h> #include <linux/mutex.h> +#include <linux/percpu.h> #include <linux/sched.h> #include <linux/seqlock.h> #include <linux/spinlock.h> @@ -337,6 +338,7 @@ static struct { long val[8]; } test_struct; static long __data_racy test_data_racy; +static DEFINE_PER_CPU(long, test_pcpu_var); static DEFINE_SEQLOCK(test_seqlock); static DEFINE_SPINLOCK(test_spinlock); static DEFINE_MUTEX(test_mutex); @@ -403,6 +405,34 @@ static noinline void test_kernel_assert_access(void) ASSERT_EXCLUSIVE_ACCESS(test_var); } +static noinline void test_kernel_read_this_cpu(void) +{ + sink_value(READ_ONCE_THIS_CPU(test_var)); +} + +static noinline void test_kernel_write_this_cpu(void) +{ + WRITE_ONCE_THIS_CPU(test_var, 42); +} + +static noinline void test_kernel_write_this_cpu_local(void) +{ + preempt_disable(); + WRITE_ONCE_THIS_CPU(*this_cpu_ptr(&test_pcpu_var), 1); + preempt_enable(); +} + +static noinline void test_kernel_reset_this_cpu_local(void) +{ + long *p; + + preempt_disable(); + p = this_cpu_ptr(&test_pcpu_var); + if (READ_ONCE_THIS_CPU(*p)) + WRITE_ONCE_THIS_CPU(*p, 0); + preempt_enable(); +} + #define TEST_CHANGE_BITS 0xff00ff00 static noinline void test_kernel_change_bits(void) @@ -1223,6 +1253,63 @@ static void test_assert_exclusive_access_scoped(struct kunit *test) KUNIT_EXPECT_FALSE(test, match_expect_inscope); } +/* Test that remote writes racing with READ_ONCE_THIS_CPU() are reported. */ +__no_kcsan +static void test_this_cpu_remote_write(struct kunit *test) +{ + struct expect_report expect = { + .access = { + { test_kernel_read_this_cpu, &test_var, sizeof(test_var), KCSAN_ACCESS_ASSERT }, + { test_kernel_write_atomic, &test_var, sizeof(test_var), KCSAN_ACCESS_WRITE | KCSAN_ACCESS_ATOMIC }, + }, + }; + bool match_expect = false; + + KCSAN_TEST_REQUIRES(test, !IS_ENABLED(CONFIG_KCSAN_IGNORE_ATOMICS)); + + begin_test_checks(test_kernel_read_this_cpu, test_kernel_write_atomic); + do { + match_expect = report_matches(&expect); + } while (!end_test_checks(match_expect)); + KUNIT_EXPECT_TRUE(test, match_expect); +} + +/* Test that remote reads racing with WRITE_ONCE_THIS_CPU() are reported. */ +__no_kcsan +static void test_this_cpu_remote_read(struct kunit *test) +{ + struct expect_report expect = { + .access = { + { test_kernel_write_this_cpu, &test_var, sizeof(test_var), KCSAN_ACCESS_ASSERT | KCSAN_ACCESS_WRITE }, + { test_kernel_read, &test_var, sizeof(test_var), 0 }, + }, + }; + bool match_expect = false; + + begin_test_checks(test_kernel_write_this_cpu, test_kernel_read); + do { + match_expect = report_matches(&expect); + } while (!end_test_checks(match_expect)); + KUNIT_EXPECT_TRUE(test, match_expect); +} + +/* + * Test that races between task and interrupt on the same CPU's instance are not + * reported; the test threads' timers access the same per-CPU instance as the + * interrupted thread. Only meaningful with CONFIG_KCSAN_INTERRUPT_WATCHER=y. + */ +__no_kcsan +static void test_this_cpu_local_noreport(struct kunit *test) +{ + bool match_never = false; + + begin_test_checks(test_kernel_write_this_cpu_local, test_kernel_reset_this_cpu_local); + do { + match_never = report_available(); + } while (!end_test_checks(match_never)); + KUNIT_EXPECT_FALSE(test, match_never); +} + /* * jiffies is special (declared to be volatile) and its accesses are typically * not marked; this test ensures that the compiler nor KCSAN gets confused about @@ -1485,6 +1572,9 @@ static struct kunit_case kcsan_test_cases[] = { KCSAN_KUNIT_CASE(test_assert_exclusive_bits_nochange), KCSAN_KUNIT_CASE(test_assert_exclusive_writer_scoped), KCSAN_KUNIT_CASE(test_assert_exclusive_access_scoped), + KCSAN_KUNIT_CASE(test_this_cpu_remote_write), + KCSAN_KUNIT_CASE(test_this_cpu_remote_read), + KCSAN_KUNIT_CASE(test_this_cpu_local_noreport), KCSAN_KUNIT_CASE(test_jiffies_noreport), KCSAN_KUNIT_CASE(test_seqlock_noreport), KCSAN_KUNIT_CASE(test_atomic_builtins), -- 2.56.1.839.g22a8290db9-goog ^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2026-11-25 22:48 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-23 13:51 [PATCH] kcsan: Detect cross-CPU races on {READ,WRITE}_ONCE_THIS_CPU() Marco Elver
2026-11-24 9:53 ` KunWu Chan
2026-11-24 10:23 ` Marco Elver
2026-11-25 9:40 ` KunWu Chan
2026-11-25 22:48 ` Marco Elver
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®