Re: [PATCH] kcsan: Detect cross-CPU races on {READ,WRITE}_ONCE_THIS_CPU()

From: Marco Elver

Date: Tue Nov 24 2026 - 05:24:14 EST


On Tue, 24 Nov 2026 at 10:54, KunWu Chan <kunwu.chan@xxxxxxxxx> wrote:
>
> On Mon, Nov 23, 2026 at 9:51 PM Marco Elver <elver@xxxxxxxxxx> 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@xxxxxxxxx>
> > Cc: Thomas Gleixner <tglx@xxxxxxxxxx>
> > Cc: Paul E. McKenney <paulmck@xxxxxxxxxx>
> > Signed-off-by: Marco Elver <elver@xxxxxxxxxx>
> > ---
> > 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