Skip to contentSkip to conversation

Fix data races in timers

RFC PATCH New Paul E. McKenney 2 participants 5 messages Sep 19 – Sep 29 High-resolution timers, timer wheel, clockevents

lore
Share & tools
lore
#0 lore raw

RFC PATCH 1/1 Fix data races in timers

Hello!

In CONFIG_KCSAN_STRICT=y mode, KCSAN finds (a very few) data races, one set on the hlist_node ->pprev field and another set on tmigr_event structure's ->ignore field. Therefore, fix these applying READ_ONCE() and WRITE_ONCE().

1. Mark updates to hlist_node ->pprev field.

2. Mark updates to tmigr_event ->field.

Thanx, Paul

------------------------------------------------------------------------

 include/linux/list.h          |    6 +++---
 kernel/time/timer.c           |    2 +-
 kernel/time/timer_migration.c |    6 +++---
 3 files changed, 7 insertions(+), 7 deletions(-)
#1 lore raw

PATCH 1/2 timers: Mark updates to hlist_node ->pprev field

The hlist_unhashed_lockless() function locklessly samples the hlist_node structure's ->pprev field, but detach_timer(), hlist_move_list(), and hlist_splice_init() all use plain C-language stores to update this field, despite the "The READ_ONCE() is paired with the various WRITE_ONCE() in hlist helpers that are defined below" in the hlist_unhashed_lockless() header comment.

Therefore use WRITE_ONCE() for these ->pprev updates.

KCSAN located this issue.

Cc Anna-Maria Behnsen <[email protected]>
Cc Frederic Weisbecker <[email protected]>
Cc Thomas Gleixner <[email protected]>
Cc Andy Shevchenko <[email protected]>
---
 include/linux/list.h | 6 +++---
 kernel/time/timer.c  | 2 +-
 2 files changed, 4 insertions(+), 4 deletions(-)
include/linux/list.h +3 -3
index 77fb62f79928..e3753695e76c 100644
@@ -1172,7 +1172,7 @@ static inline void hlist_move_list(struct hlist_head *old,
 {
 	new->first = old->first;
 	if (new->first)
-		new->first->pprev = &new->first;
+		WRITE_ONCE(new->first->pprev, &new->first);
 	old->first = NULL;
 }
 
@@ -1189,10 +1189,10 @@ static inline void hlist_splice_init(struct hlist_head *from,
 				     struct hlist_head *to)
 {
 	if (to->first)
-		to->first->pprev = &last->next;
+		WRITE_ONCE(to->first->pprev, &last->next);
 	last->next = to->first;
 	to->first = from->first;
-	from->first->pprev = &to->first;
+	WRITE_ONCE(from->first->pprev, &to->first);
 	from->first = NULL;
 }
 
kernel/time/timer.c +1 -1
index ae9abf14688e..42afdcb229d8 100644
@@ -890,7 +890,7 @@ static inline void detach_timer(struct timer_list *timer, bool clear_pending)
 
 	__hlist_del(entry);
 	if (clear_pending)
-		entry->pprev = NULL;
+		WRITE_ONCE(entry->pprev, NULL);
 	entry->next = LIST_POISON2;
 }
 
Signature · 1 line
2.40.1
#2 lore raw

[tip: timers/core] timers: Mark racy updates to hlist_node::pprev field

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

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

Commit-ID:     1159ad0a6aaa414dc726a33ccdcb702752d1b8eb
Gitweb:        https://git.kernel.org/tip/1159ad0a6aaa414dc726a33ccdcb702752d1b8eb
Author:        Paul E. McKenney <[email protected]>
AuthorDate:    Fri, 18 Sep 2026 17:25:22 -07:00
Committer:     Thomas Gleixner <[email protected]>
CommitterDate: Tue, 29 Sep 2026 21:07:15 +02:00

timers: Mark racy updates to hlist_node::pprev field

hlist_unhashed_lockless() locklessly samples the hlist_node structure's pprev field, but detach_timer(), hlist_move_list(), and hlist_splice_init() all use plain C-language stores to update this field, despite the "The READ_ONCE() is paired with the various WRITE_ONCE() in hlist helpers that are defined below" in the hlist_unhashed_lockless() header comment.

Therefore use WRITE_ONCE() for these hlist_node::pprev updates.

KCSAN located this issue.

---
 include/linux/list.h | 6 +++---
 kernel/time/timer.c  | 2 +-
 2 files changed, 4 insertions(+), 4 deletions(-)
include/linux/list.h +3 -3
index 77fb62f..e375369 100644
@@ -1172,7 +1172,7 @@ static inline void hlist_move_list(struct hlist_head *old,
 {
 	new->first = old->first;
 	if (new->first)
-		new->first->pprev = &new->first;
+		WRITE_ONCE(new->first->pprev, &new->first);
 	old->first = NULL;
 }
 
@@ -1189,10 +1189,10 @@ static inline void hlist_splice_init(struct hlist_head *from,
 				     struct hlist_head *to)
 {
 	if (to->first)
-		to->first->pprev = &last->next;
+		WRITE_ONCE(to->first->pprev, &last->next);
 	last->next = to->first;
 	to->first = from->first;
-	from->first->pprev = &to->first;
+	WRITE_ONCE(from->first->pprev, &to->first);
 	from->first = NULL;
 }
 
kernel/time/timer.c +1 -1
index ae9abf1..42afdcb 100644
@@ -890,7 +890,7 @@ static inline void detach_timer(struct timer_list *timer, bool clear_pending)
 
 	__hlist_del(entry);
 	if (clear_pending)
-		entry->pprev = NULL;
+		WRITE_ONCE(entry->pprev, NULL);
 	entry->next = LIST_POISON2;
 }
 
#3 lore raw

PATCH 2/2 timers/migration: Mark updates to tmigr_event ->field

In reply to Paul E. McKenney

The tmigr_event structure's ->ignore field are sometimes accessed locklessly, but the __tmigr_cpu_activate(), tmigr_cpu_new_timer(), and __tmigr_cpu_deactivate() functions do not mark accesses to this field.

Therefore, use READ_ONCE() for the tmigr_cpu_new_timer() function's lockless load and WRITE_ONCE() for the __tmigr_cpu_activate() and __tmigr_cpu_deactivate() functions' stores.

KCSAN located this issue.

Cc Anna-Maria Behnsen <[email protected]>
Cc Frederic Weisbecker <[email protected]>
Cc Thomas Gleixner <[email protected]>
---
 kernel/time/timer_migration.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
kernel/time/timer_migration.c +3 -3
index 059d43355e65..f920e73fff51 100644
@@ -715,7 +715,7 @@ static void __tmigr_cpu_activate(struct tmigr_cpu *tmc)
 
 	trace_tmigr_cpu_active(tmc);
 
-	tmc->cpuevt.ignore = true;
+	WRITE_ONCE(tmc->cpuevt.ignore, true);
 	WRITE_ONCE(tmc->wakeup, KTIME_MAX);
 
 	walk_groups(&tmigr_active_up, &data, tmc);
@@ -1258,7 +1258,7 @@ u64 tmigr_cpu_new_timer(u64 nextexp)
 	ret = READ_ONCE(tmc->wakeup);
 	if (nextexp != KTIME_MAX) {
 		if (nextexp != tmc->cpuevt.nextevt.expires ||
-		    tmc->cpuevt.ignore) {
+		    READ_ONCE(tmc->cpuevt.ignore)) {
 			ret = tmigr_new_timer(tmc, nextexp);
 			/*
 			 * Make sure the reevaluation of timers in idle path
@@ -1362,7 +1362,7 @@ static u64 __tmigr_cpu_deactivate(struct tmigr_cpu *tmc, u64 nextexp)
 	 * or CPU goes offline.
 	 */
 	if (nextexp != KTIME_MAX)
-		tmc->cpuevt.ignore = false;
+		WRITE_ONCE(tmc->cpuevt.ignore, false);
 
 	walk_groups(&tmigr_inactive_up, &data, tmc);
 	return data.firstexp;
Signature · 1 line
2.40.1
#4 lore raw

[tip: timers/core] timers/migration: Mark racy updates to tmigr_event::ignore field

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

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

Commit-ID:     bb41ece16463b76b4d73fc47e6648fd823236765
Gitweb:        https://git.kernel.org/tip/bb41ece16463b76b4d73fc47e6648fd823236765
Author:        Paul E. McKenney <[email protected]>
AuthorDate:    Fri, 18 Sep 2026 17:25:23 -07:00
Committer:     Thomas Gleixner <[email protected]>
CommitterDate: Tue, 29 Sep 2026 21:07:15 +02:00

timers/migration: Mark racy updates to tmigr_event::ignore field

The tmigr_event structure's ignore field is sometimes accessed locklessly, but the __tmigr_cpu_activate(), tmigr_cpu_new_timer(), and __tmigr_cpu_deactivate() functions do not mark accesses to this field.

Therefore, use READ_ONCE() for the tmigr_cpu_new_timer() function's lockless load and WRITE_ONCE() for the __tmigr_cpu_activate() and __tmigr_cpu_deactivate() functions' stores.

KCSAN located this issue.

---
 kernel/time/timer_migration.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
kernel/time/timer_migration.c +3 -3
index 059d433..f920e73 100644
@@ -715,7 +715,7 @@ static void __tmigr_cpu_activate(struct tmigr_cpu *tmc)
 
 	trace_tmigr_cpu_active(tmc);
 
-	tmc->cpuevt.ignore = true;
+	WRITE_ONCE(tmc->cpuevt.ignore, true);
 	WRITE_ONCE(tmc->wakeup, KTIME_MAX);
 
 	walk_groups(&tmigr_active_up, &data, tmc);
@@ -1258,7 +1258,7 @@ u64 tmigr_cpu_new_timer(u64 nextexp)
 	ret = READ_ONCE(tmc->wakeup);
 	if (nextexp != KTIME_MAX) {
 		if (nextexp != tmc->cpuevt.nextevt.expires ||
-		    tmc->cpuevt.ignore) {
+		    READ_ONCE(tmc->cpuevt.ignore)) {
 			ret = tmigr_new_timer(tmc, nextexp);
 			/*
 			 * Make sure the reevaluation of timers in idle path
@@ -1362,7 +1362,7 @@ static u64 __tmigr_cpu_deactivate(struct tmigr_cpu *tmc, u64 nextexp)
 	 * or CPU goes offline.
 	 */
 	if (nextexp != KTIME_MAX)
-		tmc->cpuevt.ignore = false;
+		WRITE_ONCE(tmc->cpuevt.ignore, false);
 
 	walk_groups(&tmigr_inactive_up, &data, tmc);
 	return data.firstexp;