Re: [PATCH] kcsan: Detect cross-CPU races on {READ,WRITE}_ONCE_THIS_CPU()
From: KunWu Chan
Date: Wed Nov 25 2026 - 04:41:10 EST
On Tue, Nov 24, 2026 at 6:24 PM Marco Elver <elver@xxxxxxxxxx> wrote:
>
> 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
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@xxxxxxxxx>
Thanks,
Kunwu