Skip to contentSkip to conversation

rwonce: Introduce {READ,WRITE}_ONCE_THIS_CPU()

PATCH Applied Kunwu Chan 3 participants 7 messages Oct 7 – Nov 9

Subsystems (2)
lore
Share & tools
lore

Kunwu Chan

author
#0 lore raw

v3 cover rwonce: Introduce {READ,WRITE}_ONCE_THIS_CPU()

Hi,

This is a rebase of v2 onto v7.3-rc6 with review tags collected since v2.

Changes since v2 [1]:
- Rebased onto v7.3-rc6.
- Collected Reviewed-by and Acked-by tags.

[1] https://lore.kernel.org/lkml/[email protected]/

Changes since v1 [2]:
- 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)

[2] https://lore.kernel.org/all/[email protected]/

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(-)

base-commit: a90ee4305c4a5df72c11b31dacfdc76e00fcf78a

Signature · 1 line
2.43.0
author
#1 lore raw

PATCH v3 1/2 rwonce: Add READ_ONCE_THIS_CPU() and WRITE_ONCE_THIS_CPU()

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.

---
 include/asm-generic/rwonce.h | 11 +++++++++++
 1 file changed, 11 insertions(+)
include/asm-generic/rwonce.h +11 -0
index 52b969c7c..4d4e009a4 100644
@@ -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)
 {
Signature · 1 line
2.43.0
maintainer Acked-by on v3 1/2
#2 lore raw

Re: [PATCH v3 1/2] rwonce: Add READ_ONCE_THIS_CPU() and WRITE_ONCE_THIS_CPU()

Quoted 16 lines · Kunwu Chan

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.

Quoted 5 lines

No objections here, but since I missed the background, I don't see what a future implementation of the new macros might do that is different from the existing macros, maybe you can still clarify that.

For taking this through something other than my asm-generic tree:

author
#3 lore raw

Re: [PATCH v3 1/2] rwonce: Add READ_ONCE_THIS_CPU() and WRITE_ONCE_THIS_CPU()

Quoted 24 lines · Arnd Bergmann
Quoted 16 lines · Kunwu Chan

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.

Quoted 5 lines
Quoted 5 lines

No objections here, but since I missed the background, I don't see what a future implementation of the new macros might do that is different from the existing macros, maybe you can still clarify that.

Hi Arnd,

Thanks for the Ack!

The background was discussed in the v1 thread:
https://lore.kernel.org/all/87qzici6yb.ffs@fw13/

The idea is that the *_THIS_CPU() variants make the intended access scope explicit: a race between different execution contexts on the same CPU, rather than a race that may occur across CPUs.

For now, they are just wrappers around READ_ONCE() and WRITE_ONCE(), as Thomas suggested. A future KCSAN implementation could use the annotation to distinguish these same-CPU races from cross-CPU races.

Thanks,
Kunwu

Quoted 3 lines

For taking this through something other than my asm-generic tree:

merge bot Applied
#4 lore raw

[tip: timers/nohz] rwonce: Add READ_ONCE_THIS_CPU() and WRITE_ONCE_THIS_CPU()

In reply to Kunwu Chan
The following commit has been merged into the timers/nohz branch of tip:

The following commit has been merged into the timers/nohz branch of tip:

Commit-ID:     3f1eb85d954ae5aa16e85e87afe982b7fed91588
Gitweb:        https://git.kernel.org/tip/3f1eb85d954ae5aa16e85e87afe982b7fed91588
Author:        Kunwu Chan <[email protected]>
AuthorDate:    Wed, 07 Oct 2026 10:28:58 +08:00
Committer:     Thomas Gleixner <[email protected]>
CommitterDate: Mon, 09 Nov 2026 17:50:54 +01:00

rwonce: Add READ_ONCE_THIS_CPU() and WRITE_ONCE_THIS_CPU()

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.

---
 include/asm-generic/rwonce.h | 11 +++++++++++
 1 file changed, 11 insertions(+)
include/asm-generic/rwonce.h +11 -0
index 52b969c..4d4e009 100644
@@ -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)
 {
author
#5 lore raw

PATCH v3 2/2 timers/nohz: Mark got_idle_tick accesses as same-CPU races

In reply to 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.

---
 kernel/time/tick-sched.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
kernel/time/tick-sched.c +3 -3
index 6c3fea386..a136bb247 100644
@@ -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)
@@ -1248,8 +1248,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;
Signature · 1 line
2.43.0
merge bot Applied
#6 lore raw

[tip: timers/nohz] timers/nohz: Mark got_idle_tick accesses as same-CPU races

The following commit has been merged into the timers/nohz branch of tip:

The following commit has been merged into the timers/nohz branch of tip:

Commit-ID:     109fad8c8a7142b25818e64b9e12aed3935408d9
Gitweb:        https://git.kernel.org/tip/109fad8c8a7142b25818e64b9e12aed3935408d9
Author:        Kunwu Chan <[email protected]>
AuthorDate:    Wed, 07 Oct 2026 10:28:59 +08:00
Committer:     Thomas Gleixner <[email protected]>
CommitterDate: Mon, 09 Nov 2026 17:50:54 +01:00

timers/nohz: Mark got_idle_tick accesses as same-CPU races

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.

---
 kernel/time/tick-sched.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
kernel/time/tick-sched.c +3 -3
index 5e96c2e..2554883 100644
@@ -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)
@@ -1264,8 +1264,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;