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

From: Marco Elver

Date: Fri Nov 27 2026 - 08:10:00 EST


On Thu, 26 Nov 2026 at 03:58, KunWu Chan <kunwu.chan@xxxxxxxxx> wrote:
> On Thu, Nov 26, 2026 at 6:48 AM Marco Elver <elver@xxxxxxxxxx> wrote:
> > 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:
>
> Thanks, that addresses my NMI concern. The trade-off makes sense
> to me.
>
> >
> > 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@xxxxxxxxx>
> >
> > Below is the complete v2 patch with the unknown-origin suppression.
>
> I tested v2 on next-20261123. A 60-second rcutorture run with
> strict KCSAN completed without any reports involving got_idle_tick
> or tick_nohz_handler.
>
> With rcutorture disabled, I ran the three new KUnit cases twice
> with interruptible watchers on and twice with them off.
> remote_write and local_noreport passed at all seven thread counts
> in all four runs. The remote_read results were:
>
> watchers on: one run passed; threads=5 failed in the other
> watchers off: threads=4 failed in one run; threads=5 and 8
> failed in the other
>
> I also saw intermittent remote_read failures on v1 with
> rcutorture enabled. Have you seen this in your runs?

I haven't. I suspect it's a flaky test because writers are also racing
among each other.

> Reviewed-by: Kunwu Chan <kunwu.chan@xxxxxxxxx>

I'll send a final v2 with the fixes incl. to make test more robust.

Thanks,
-- Marco