mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH v2 0/2] rwonce: Introduce {READ,WRITE}_ONCE_THIS_CPU()
@ 2026-10-02 15:17 Kunwu Chan
  2026-10-02 15:17 ` [PATCH v2 1/2] rwonce: Add READ_ONCE_THIS_CPU() and WRITE_ONCE_THIS_CPU() Kunwu Chan
  2026-10-02 15:17 ` [PATCH v2 2/2] timers/nohz: Mark got_idle_tick accesses as same-CPU races Kunwu Chan
  0 siblings, 2 replies; 4+ messages in thread
From: Kunwu Chan @ 2026-10-02 15:17 UTC (permalink / raw)
  To: arnd, anna-maria, frederic, mingo, tglx
  Cc: linux-arch, linux-kernel, Kunwu Chan

This is a rework of [1] following the discussion with Thomas and Paul.

v1 marked the got_idle_tick accesses with plain READ_ONCE()/WRITE_ONCE().
However, the plain variants do not distinguish an intentional race
between execution contexts on the same CPU from an access that may race
across CPUs. This distinction allows concurrency tools such as KCSAN
to tell these cases apart, so that stricter task-vs-interrupt race
checking can be enabled without false positives on intentional
same-CPU races.

The first patch introduces READ_ONCE_THIS_CPU() and
WRITE_ONCE_THIS_CPU() for this purpose, currently as simple wrappers
around the plain variants. The second patch converts got_idle_tick
to use them.

Changes since v1 [1]:
 - Introduce {READ,WRITE}_ONCE_THIS_CPU() instead of reusing the
   plain variants. (Thomas Gleixner)

 - Expand the commit messages to state the KCSAN rationale. (Thomas
   Gleixner)

[1] https://lore.kernel.org/all/20260920085434.2918331-1-kunwu.chan@gmail.com/

Kunwu Chan (2):
  rwonce: Add READ_ONCE_THIS_CPU() and WRITE_ONCE_THIS_CPU()
  timers/nohz: Mark got_idle_tick accesses as same-CPU races

 include/asm-generic/rwonce.h | 11 +++++++++++
 kernel/time/tick-sched.c     |  6 +++---
 2 files changed, 14 insertions(+), 3 deletions(-)

-- 
2.43.0


^ permalink raw reply	[flat|nested] 4+ messages in thread

* [PATCH v2 1/2] rwonce: Add READ_ONCE_THIS_CPU() and WRITE_ONCE_THIS_CPU()
  2026-10-02 15:17 [PATCH v2 0/2] rwonce: Introduce {READ,WRITE}_ONCE_THIS_CPU() Kunwu Chan
@ 2026-10-02 15:17 ` Kunwu Chan
  2026-10-02 15:44   ` Paul E. McKenney
  2026-10-02 15:17 ` [PATCH v2 2/2] timers/nohz: Mark got_idle_tick accesses as same-CPU races Kunwu Chan
  1 sibling, 1 reply; 4+ messages in thread
From: Kunwu Chan @ 2026-10-02 15:17 UTC (permalink / raw)
  To: arnd, anna-maria, frederic, mingo, tglx
  Cc: linux-arch, linux-kernel, Kunwu Chan, Paul E . McKenney

Per-CPU variables are sometimes accessed from different execution
contexts on the same CPU, typically task context and an interrupt or
NMI handler.

A plain READ_ONCE() or WRITE_ONCE() marks such an access as an
intentional race but does not express where the racing accesses are
expected to come from. This distinction allows concurrency tools such
as KCSAN to tell an intentional same-CPU context race apart from an
access that may race across CPUs.

Add READ_ONCE_THIS_CPU() and WRITE_ONCE_THIS_CPU() to identify accesses
to a per-CPU variable which are intended to race only with accesses
from other execution contexts on the same CPU. For now, they are
simple wrappers around READ_ONCE() and WRITE_ONCE(), so generated code
and tool behaviour are unchanged.

Suggested-by: Thomas Gleixner <tglx@kernel.org>
Suggested-by: Paul E. McKenney <paulmck@kernel.org>
Signed-off-by: Kunwu Chan <kunwu.chan@gmail.com>
---
 include/asm-generic/rwonce.h | 11 +++++++++++
 1 file changed, 11 insertions(+)

diff --git a/include/asm-generic/rwonce.h b/include/asm-generic/rwonce.h
index 52b969c7cef9..4d4e009a4489 100644
--- a/include/asm-generic/rwonce.h
+++ b/include/asm-generic/rwonce.h
@@ -61,6 +61,17 @@ do {									\
 	__WRITE_ONCE(x, val);						\
 } while (0)
 
+/*
+ * {READ,WRITE}_ONCE_THIS_CPU() identify accesses to a per-CPU variable
+ * which are intended to race only with accesses from other execution
+ * contexts on the same CPU, e.g. task and interrupt context.
+ *
+ * For now, these are simple wrappers around READ_ONCE() and
+ * WRITE_ONCE().
+ */
+#define READ_ONCE_THIS_CPU(x)		READ_ONCE(x)
+#define WRITE_ONCE_THIS_CPU(x, val)	WRITE_ONCE(x, val)
+
 static __no_sanitize_or_inline
 unsigned long __read_once_word_nocheck(const void *addr)
 {
-- 
2.43.0


^ permalink raw reply	[flat|nested] 4+ messages in thread

* [PATCH v2 2/2] timers/nohz: Mark got_idle_tick accesses as same-CPU races
  2026-10-02 15:17 [PATCH v2 0/2] rwonce: Introduce {READ,WRITE}_ONCE_THIS_CPU() Kunwu Chan
  2026-10-02 15:17 ` [PATCH v2 1/2] rwonce: Add READ_ONCE_THIS_CPU() and WRITE_ONCE_THIS_CPU() Kunwu Chan
@ 2026-10-02 15:17 ` Kunwu Chan
  1 sibling, 0 replies; 4+ messages in thread
From: Kunwu Chan @ 2026-10-02 15:17 UTC (permalink / raw)
  To: arnd, anna-maria, frederic, mingo, tglx
  Cc: linux-arch, linux-kernel, Kunwu Chan

got_idle_tick is a per-CPU flag set by the tick interrupt handler in
tick_sched_do_timer() and read and cleared by the idle path in
tick_nohz_idle_got_tick().  The flag is intentionally accessed without
ordering or locking; a concurrent set can be overwritten by the clear.

Use WRITE_ONCE_THIS_CPU() and READ_ONCE_THIS_CPU() to identify these
as intentional races between execution contexts on the same CPU,
rather than accesses that may race across CPUs.

Suggested-by: Thomas Gleixner <tglx@kernel.org>
Signed-off-by: Kunwu Chan <kunwu.chan@gmail.com>
---
 kernel/time/tick-sched.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/kernel/time/tick-sched.c b/kernel/time/tick-sched.c
index a7893a079a83..eb6065c8a90d 100644
--- a/kernel/time/tick-sched.c
+++ b/kernel/time/tick-sched.c
@@ -269,7 +269,7 @@ static void tick_sched_do_timer(struct tick_sched *ts, ktime_t now)
 	}
 
 	if (tick_sched_flag_test(ts, TS_FLAG_INIDLE))
-		ts->got_idle_tick = 1;
+		WRITE_ONCE_THIS_CPU(ts->got_idle_tick, 1);
 }
 
 static void tick_sched_handle(struct tick_sched *ts, struct pt_regs *regs)
@@ -1246,8 +1246,8 @@ bool tick_nohz_idle_got_tick(void)
 {
 	struct tick_sched *ts = this_cpu_ptr(&tick_cpu_sched);
 
-	if (ts->got_idle_tick) {
-		ts->got_idle_tick = 0;
+	if (READ_ONCE_THIS_CPU(ts->got_idle_tick)) {
+		WRITE_ONCE_THIS_CPU(ts->got_idle_tick, 0);
 		return true;
 	}
 	return false;
-- 
2.43.0


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH v2 1/2] rwonce: Add READ_ONCE_THIS_CPU() and WRITE_ONCE_THIS_CPU()
  2026-10-02 15:17 ` [PATCH v2 1/2] rwonce: Add READ_ONCE_THIS_CPU() and WRITE_ONCE_THIS_CPU() Kunwu Chan
@ 2026-10-02 15:44   ` Paul E. McKenney
  0 siblings, 0 replies; 4+ messages in thread
From: Paul E. McKenney @ 2026-10-02 15:44 UTC (permalink / raw)
  To: Kunwu Chan
  Cc: arnd, anna-maria, frederic, mingo, tglx, linux-arch, linux-kernel

On Fri, Oct 02, 2026 at 11:17:55PM +0800, Kunwu Chan wrote:
> Per-CPU variables are sometimes accessed from different execution
> contexts on the same CPU, typically task context and an interrupt or
> NMI handler.
> 
> A plain READ_ONCE() or WRITE_ONCE() marks such an access as an
> intentional race but does not express where the racing accesses are
> expected to come from. This distinction allows concurrency tools such
> as KCSAN to tell an intentional same-CPU context race apart from an
> access that may race across CPUs.
> 
> Add READ_ONCE_THIS_CPU() and WRITE_ONCE_THIS_CPU() to identify accesses
> to a per-CPU variable which are intended to race only with accesses
> from other execution contexts on the same CPU. For now, they are
> simple wrappers around READ_ONCE() and WRITE_ONCE(), so generated code
> and tool behaviour are unchanged.
> 
> Suggested-by: Thomas Gleixner <tglx@kernel.org>
> Suggested-by: Paul E. McKenney <paulmck@kernel.org>
> Signed-off-by: Kunwu Chan <kunwu.chan@gmail.com>

Reviewed-by: Paul E. McKenney <paulmck@kernel.org>

> ---
>  include/asm-generic/rwonce.h | 11 +++++++++++
>  1 file changed, 11 insertions(+)
> 
> diff --git a/include/asm-generic/rwonce.h b/include/asm-generic/rwonce.h
> index 52b969c7cef9..4d4e009a4489 100644
> --- a/include/asm-generic/rwonce.h
> +++ b/include/asm-generic/rwonce.h
> @@ -61,6 +61,17 @@ do {									\
>  	__WRITE_ONCE(x, val);						\
>  } while (0)
>  
> +/*
> + * {READ,WRITE}_ONCE_THIS_CPU() identify accesses to a per-CPU variable
> + * which are intended to race only with accesses from other execution
> + * contexts on the same CPU, e.g. task and interrupt context.
> + *
> + * For now, these are simple wrappers around READ_ONCE() and
> + * WRITE_ONCE().
> + */
> +#define READ_ONCE_THIS_CPU(x)		READ_ONCE(x)
> +#define WRITE_ONCE_THIS_CPU(x, val)	WRITE_ONCE(x, val)
> +
>  static __no_sanitize_or_inline
>  unsigned long __read_once_word_nocheck(const void *addr)
>  {
> -- 
> 2.43.0
> 

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2026-10-02 15:44 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-10-02 15:17 [PATCH v2 0/2] rwonce: Introduce {READ,WRITE}_ONCE_THIS_CPU() Kunwu Chan
2026-10-02 15:17 ` [PATCH v2 1/2] rwonce: Add READ_ONCE_THIS_CPU() and WRITE_ONCE_THIS_CPU() Kunwu Chan
2026-10-02 15:44   ` Paul E. McKenney
2026-10-02 15:17 ` [PATCH v2 2/2] timers/nohz: Mark got_idle_tick accesses as same-CPU races Kunwu Chan

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®