From: Marco Elver <elver@google.com>
To: KunWu Chan <kunwu.chan@gmail.com>
Cc: Dmitry Vyukov <dvyukov@google.com>,
"Paul E. McKenney" <paulmck@kernel.org>,
kasan-dev@googlegroups.com, linux-kernel@vger.kernel.org
Subject: Re: [RFC] KCSAN support for *_THIS_CPU()
Date: Mon, 23 Nov 2026 13:55:59 +0000 [thread overview]
Message-ID: <awRF7-R1JjQczRgn@elver.google.com> (raw)
In-Reply-To: <CAN_Opa-WpbC9wXTYPybiZCrkMjQbSTnfJvUck7YiMP9wG59y4Q@mail.gmail.com>
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/
parent reply other threads:[~2026-11-23 13:56 UTC|newest]
Thread overview: expand[flat|nested] mbox.gz Atom feed
[parent not found: <CAN_Opa-WpbC9wXTYPybiZCrkMjQbSTnfJvUck7YiMP9wG59y4Q@mail.gmail.com>]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=awRF7-R1JjQczRgn@elver.google.com \
--to=elver@google.com \
--cc=dvyukov@google.com \
--cc=kasan-dev@googlegroups.com \
--cc=kunwu.chan@gmail.com \
--cc=linux-kernel@vger.kernel.org \
--cc=paulmck@kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox