* Re: [RFC] KCSAN support for *_THIS_CPU()
[not found] <CAN_Opa-WpbC9wXTYPybiZCrkMjQbSTnfJvUck7YiMP9wG59y4Q@mail.gmail.com>
@ 2026-11-23 13:55 ` Marco Elver
2026-11-24 9:48 ` KunWu Chan
0 siblings, 1 reply; 2+ messages 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] 2+ messages in thread
* Re: [RFC] KCSAN support for *_THIS_CPU()
2026-11-23 13:55 ` [RFC] KCSAN support for *_THIS_CPU() Marco Elver
@ 2026-11-24 9:48 ` KunWu Chan
0 siblings, 0 replies; 2+ messages in thread
From: KunWu Chan @ 2026-11-24 9:48 UTC (permalink / raw)
To: Marco Elver; +Cc: Dmitry Vyukov, Paul E. McKenney, kasan-dev, linux-kernel
On Mon, Nov 23, 2026 at 9:56 PM Marco Elver <elver@google.com> 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@google.com/
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
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2026-11-24 9:48 UTC | newest]
Thread overview: 2+ messages (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
2026-11-24 9:48 ` KunWu Chan
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.