* Re: [RFC] KCSAN support for *_THIS_CPU()
[not found] <CAN_Opa-WpbC9wXTYPybiZCrkMjQbSTnfJvUck7YiMP9wG59y4Q@mail.gmail.com>
@ 2026-11-23 13:55 ` Marco Elver
0 siblings, 0 replies; only message in thread
From: Marco Elver @ 2026-11-23 13:55 UTC (permalink / raw)
To: KunWu Chan; +Cc: Dmitry Vyukov, Paul E. McKenney, kasan-dev, linux-kernel
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@google.com/
^ permalink raw reply [flat|nested] only message in thread
only message in thread, other threads:[~2026-11-23 13:56 UTC | newest]
Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
[not found] <CAN_Opa-WpbC9wXTYPybiZCrkMjQbSTnfJvUck7YiMP9wG59y4Q@mail.gmail.com>
2026-11-23 13:55 ` [RFC] KCSAN support for *_THIS_CPU() 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®