Re: [RFC] KCSAN support for *_THIS_CPU()

From: KunWu Chan

Date: Tue Nov 24 2026 - 04:51:40 EST


On Mon, Nov 23, 2026 at 9:56 PM Marco Elver <elver@xxxxxxxxxx> wrote:
>
> On Mon, Nov 23, 2026 at 04:15PM +0800, KunWu Chan wrote:
> > Hi Marco and Dmitry,
> >
> > Paul suggested I get in touch about helping with KCSAN support for
> > *_THIS_CPU(). The wrappers [1] and the got_idle_tick annotation [2]
> > are now in tip's timers/nohz branch. The idle task and the tick
> > hardirq intentionally race on their CPU's flag. A conflicting access
> > from another CPU to the same instance should still be detectable.
> >
> > I'd start with static per-CPU objects in strict mode. One option is
> > to keep the lvalue API and pass the actual address and a THIS_CPU
> > tag to KCSAN, which could warn on a known remote instance. This
> > would let us check the pointer the tick callback gets from
> > container_of().
> >
> > If both accesses are tagged and verified as local, a hardirq hit on
> > the same byte range would leave the task's watchpoint armed for a
> > later cross-CPU hit. With a per-CPU record, the sampled task would
> > stay non-preemptible while the record is live. We'd also record
> > permitted local writes, since they can explain a value change when
> > no other hit is captured.
> >
> > Does leaving the watchpoint armed on a permitted local hit fit
> > KCSAN? For the ownership check, would you prefer a separate check in
> > KASAN or in the primitives? Would it be better for the primitives
> > to take a __percpu pointer and resolve the instance themselves?
>
> This is a lot more complex than needed. KCSAN already has the building
> blocks:
>
> 1. Same-CPU races: if the watchpoint is armed with interrupts (and
> NMI checking) disabled, a local hardirq can never hit it, and the
> interrupted task can never hit the hardirq's watchpoint. KCSAN
> already does this by default, and for reordered accesses even with
> CONFIG_KCSAN_INTERRUPT_WATCHER=y. No per-CPU record, no "permitted
> local hit" state, no recording of local writes.
>
> 2. Cross-CPU races: READ_ONCE/WRITE_ONCE are atomic to KCSAN, so today
> the *_THIS_CPU() wrappers aren't checked at all. Making them
> ASSERT_EXCLUSIVE_WRITER/ACCESS-style checks reports any concurrent
> access from another CPU, marked or not.
>
> So the whole thing is a new KCSAN_ACCESS_THIS_CPU flag that forces
> interrupts off for that watchpoint, plus the wrappers calling
> kcsan_check_access() with ASSERT|THIS_CPU. It works for any object
> (static or dynamic per-CPU, container_of() or not) and in both strict
> and non-strict mode. A separate ownership check (KASAN, a __percpu
> pointer API, "known remote instance" lookups) is out of scope; it's
> a different class of bug, and if a wrong instance is used concurrently,
> the assertion catches it anyway.
>
> I have a patch with tests [1]; would you mind giving it a spin on
> your end?
>
> Thanks,
> -- Marco
>
> [1] https://lore.kernel.org/all/20261123135148.1146372-1-elver@xxxxxxxxxx/

Hi Marco,

Thanks for the patch. I see how the existing assertions and the
IRQ-off watchpoint path fit together. I've run it on my end and
will post the results and a test comment on the patch thread.

Thanks,
Kunwu