Skip to contentSkip to conversation

tick/sched: Limit non-timekeeper CPUs calling jiffies update

PATCH Applied Steve Wahl 3 participants 5 messages Oct 27, 2025 – Nov 1, 2025 Nohz, dynticks support

lore
Share & tools
lore

Steve Wahl

author
#0 lore raw

PATCH v2 1/1 tick/sched: Limit non-timekeeper CPUs calling jiffies update

On large NUMA systems, while running a test program that saturates the inter-processor and inter-NUMA links, acquiring the jiffies_lock can be very expensive. If the cpu designated to do jiffies updates (tick_do_timer_cpu) gets delayed and other cpus decide to do the jiffies update themselves, a large number of them decide to do so at the same time. The inexpensive check against tick_next_period is far quicker than actually acquiring the lock, so most of these get in line to obtain the lock. If obtaining the lock is slow enough, this spirals into the vast majority of CPUs continuously being stuck waiting for this lock, just to obtain it and find out that time has already been updated by another cpu. For example, on one random entry to kdb by manually-injected NMI, I saw 2912 of 3840 cpus stuck here.

To avoid this, allow only one non-timekeeper CPU to call tick_do_update_jiffies64() at any given time, resetting ts->stalled jiffies only if the jiffies update function is actually called.

With this change, manually interrupting the test I find at most two CPUs in the tick_do_update_jiffies64 function (the timekeeper and one other).

---

v2: Rewritten to use an atomic to gate non-timekeeping cpus calling the
jiffies update, as suggested by tglx. Title of patch has changed since trylock is no longer used.

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

 kernel/time/tick-sched.c | 30 ++++++++++++++++++++++++++----
 1 file changed, 26 insertions(+), 4 deletions(-)
kernel/time/tick-sched.c +26 -4
index c527b421c865..3ff3eb1f90d0 100644
@@ -201,6 +201,27 @@ static inline void tick_sched_flag_clear(struct tick_sched *ts,
 	ts->flags &= ~flag;
 }
 
+/*
+ * Allow only one non-timekeeper CPU at a time update jiffies from
+ * the timer tick.
+ *
+ * Returns true if update was run.
+ */
+static bool tick_limited_update_jiffies64(struct tick_sched *ts, ktime_t now)
+{
+	static atomic_t in_progress;
+	int inp;
+
+	inp = atomic_read(&in_progress);
+	if (inp || !atomic_try_cmpxchg(&in_progress, &inp, 1))
+		return false;
+
+	if (ts->last_tick_jiffies == jiffies)
+		tick_do_update_jiffies64(now);
+	atomic_set(&in_progress, 0);
+	return true;
+}
+
 #define MAX_STALLED_JIFFIES 5
 
 static void tick_sched_do_timer(struct tick_sched *ts, ktime_t now)
@@ -239,10 +260,11 @@ static void tick_sched_do_timer(struct tick_sched *ts, ktime_t now)
 		ts->stalled_jiffies = 0;
 		ts->last_tick_jiffies = READ_ONCE(jiffies);
 	} else {
-		if (++ts->stalled_jiffies == MAX_STALLED_JIFFIES) {
-			tick_do_update_jiffies64(now);
-			ts->stalled_jiffies = 0;
-			ts->last_tick_jiffies = READ_ONCE(jiffies);
+		if (++ts->stalled_jiffies >= MAX_STALLED_JIFFIES) {
+			if (tick_limited_update_jiffies64(ts, now)) {
+				ts->stalled_jiffies = 0;
+				ts->last_tick_jiffies = READ_ONCE(jiffies);
+			}
 		}
 	}
 
Signature · 1 line
2.43.0
Acked-by on v2 1/1
#1 lore raw
Quoted 46 lines · Steve Wahl

On large NUMA systems, while running a test program that saturates the inter-processor and inter-NUMA links, acquiring the jiffies_lock can be very expensive. If the cpu designated to do jiffies updates (tick_do_timer_cpu) gets delayed and other cpus decide to do the jiffies update themselves, a large number of them decide to do so at the same time. The inexpensive check against tick_next_period is far quicker than actually acquiring the lock, so most of these get in line to obtain the lock. If obtaining the lock is slow enough, this spirals into the vast majority of CPUs continuously being stuck waiting for this lock, just to obtain it and find out that time has already been updated by another cpu. For example, on one random entry to kdb by manually-injected NMI, I saw 2912 of 3840 cpus stuck here.

To avoid this, allow only one non-timekeeper CPU to call tick_do_update_jiffies64() at any given time, resetting ts->stalled jiffies only if the jiffies update function is actually called.

With this change, manually interrupting the test I find at most two CPUs in the tick_do_update_jiffies64 function (the timekeeper and one other).

---

v2: Rewritten to use an atomic to gate non-timekeeping cpus calling the
jiffies update, as suggested by tglx. Title of patch has changed since trylock is no longer used.

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

  kernel/time/tick-sched.c | 30 ++++++++++++++++++++++++++----
  1 file changed, 26 insertions(+), 4 deletions(-)
kernel/time/tick-sched.c +5 -0
index c527b421c865..3ff3eb1f90d0 100644
@@ -201,6 +201,27 @@ static inline void tick_sched_flag_clear(struct tick_sched *ts,
  	ts->flags &= ~flag;
  }
 
+/*
+ * Allow only one non-timekeeper CPU at a time update jiffies from
+ * the timer tick.
+ *
+ * Returns true if update was run.

kernel/time/tick-sched.c static inline void tick_sched_flag_clear(struct tick_sched *ts, · line 218

Quoted 10 lines
kernel/time/tick-sched.c +10 -0
+ */
+static bool tick_limited_update_jiffies64(struct tick_sched *ts, ktime_t now)
+{
+	static atomic_t in_progress;
+	int inp;
+
+	inp = atomic_read(&in_progress);
+	if (inp || !atomic_try_cmpxchg(&in_progress, &inp, 1))
+		return false;
+

You come here if (ts->last_tick_jiffies == jiffies). So it may be not necessary to check again.

Quoted 15 lines
kernel/time/tick-sched.c +6 -2
+	if (ts->last_tick_jiffies == jiffies)
+		tick_do_update_jiffies64(now);
+	atomic_set(&in_progress, 0);
+	return true;
+}
+
  #define MAX_STALLED_JIFFIES 5
 
  static void tick_sched_do_timer(struct tick_sched *ts, ktime_t now)
@@ -239,10 +260,11 @@ static void tick_sched_do_timer(struct tick_sched *ts, ktime_t now)
  		ts->stalled_jiffies = 0;
  		ts->last_tick_jiffies = READ_ONCE(jiffies);
  	} else {
-		if (++ts->stalled_jiffies == MAX_STALLED_JIFFIES) {
-			tick_do_update_jiffies64(now);

kernel/time/tick-sched.c static void tick_sched_do_timer(struct tick_sched *ts, ktime_t now) · line 270

Quoted 10 lines
kernel/time/tick-sched.c +5 -2
-			ts->stalled_jiffies = 0;
-			ts->last_tick_jiffies = READ_ONCE(jiffies);
+		if (++ts->stalled_jiffies >= MAX_STALLED_JIFFIES) {
+			if (tick_limited_update_jiffies64(ts, now)) {
+				ts->stalled_jiffies = 0;
+				ts->last_tick_jiffies = READ_ONCE(jiffies);
+			}
  		}
  	}
 

Yes. This could help large systems.

author
#2 lore raw
Quoted 53 lines · Shrikanth Hegde
Quoted 50 lines · Steve Wahl

On large NUMA systems, while running a test program that saturates the inter-processor and inter-NUMA links, acquiring the jiffies_lock can be very expensive. If the cpu designated to do jiffies updates (tick_do_timer_cpu) gets delayed and other cpus decide to do the jiffies update themselves, a large number of them decide to do so at the same time. The inexpensive check against tick_next_period is far quicker than actually acquiring the lock, so most of these get in line to obtain the lock. If obtaining the lock is slow enough, this spirals into the vast majority of CPUs continuously being stuck waiting for this lock, just to obtain it and find out that time has already been updated by another cpu. For example, on one random entry to kdb by manually-injected NMI, I saw 2912 of 3840 cpus stuck here.

To avoid this, allow only one non-timekeeper CPU to call tick_do_update_jiffies64() at any given time, resetting ts->stalled jiffies only if the jiffies update function is actually called.

With this change, manually interrupting the test I find at most two CPUs in the tick_do_update_jiffies64 function (the timekeeper and one other).

---

v2: Rewritten to use an atomic to gate non-timekeeping cpus calling the
jiffies update, as suggested by tglx. Title of patch has changed since trylock is no longer used.

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

  kernel/time/tick-sched.c | 30 ++++++++++++++++++++++++++----
  1 file changed, 26 insertions(+), 4 deletions(-)
kernel/time/tick-sched.c +9 -0
index c527b421c865..3ff3eb1f90d0 100644
@@ -201,6 +201,27 @@ static inline void tick_sched_flag_clear(struct tick_sched *ts,
  	ts->flags &= ~flag;
  }
+/*
+ * Allow only one non-timekeeper CPU at a time update jiffies from
+ * the timer tick.
+ *
+ * Returns true if update was run.
+ */
+static bool tick_limited_update_jiffies64(struct tick_sched *ts, ktime_t now)
+{
+	static atomic_t in_progress;
Quoted 8 lines

kernel/time/tick-sched.c static inline void tick_sched_flag_clear(struct tick_sched *ts, · line 218

Quoted 6 lines
kernel/time/tick-sched.c +6 -0
+	int inp;
+
+	inp = atomic_read(&in_progress);
+	if (inp || !atomic_try_cmpxchg(&in_progress, &inp, 1))
+		return false;
+

You come here if (ts->last_tick_jiffies == jiffies). So it may be not necessary to check again.

TGLX had this in his rewrite suggestion, and I looked pretty intensely at this test.

The situation I'm looking to resolve is caused by inter-NUMA links being abnormally swamped with traffic. Especially for writes, access to shared memory locations, such as the atomic operations to in_progress right above this, take longer than one usually would expect. So to me it makes sense that things may have changed since the atomic_try_cmpxchg was initiated, and so I left the check in place.

Quoted 27 lines
Quoted 13 lines
kernel/time/tick-sched.c +6 -1
+	if (ts->last_tick_jiffies == jiffies)
+		tick_do_update_jiffies64(now);
+	atomic_set(&in_progress, 0);
+	return true;
+}
+
  #define MAX_STALLED_JIFFIES 5
  static void tick_sched_do_timer(struct tick_sched *ts, ktime_t now)
@@ -239,10 +260,11 @@ static void tick_sched_do_timer(struct tick_sched *ts, ktime_t now)
  		ts->stalled_jiffies = 0;
  		ts->last_tick_jiffies = READ_ONCE(jiffies);
  	} else {
-		if (++ts->stalled_jiffies == MAX_STALLED_JIFFIES) {

kernel/time/tick-sched.c static void tick_sched_do_timer(struct tick_sched *ts, ktime_t now) · line 269

Quoted 10 lines
kernel/time/tick-sched.c +5 -3
-			tick_do_update_jiffies64(now);
-			ts->stalled_jiffies = 0;
-			ts->last_tick_jiffies = READ_ONCE(jiffies);
+		if (++ts->stalled_jiffies >= MAX_STALLED_JIFFIES) {
+			if (tick_limited_update_jiffies64(ts, now)) {
+				ts->stalled_jiffies = 0;
+				ts->last_tick_jiffies = READ_ONCE(jiffies);
+			}
  		}
  	}

Yes. This could help large systems.

Quoted 1 line

Thanks for your time reviewing!

--> Steve Wahl

Signature · 1 line
Steve Wahl, Hewlett Packard Enterprise
#3 lore raw
Quoted 66 lines · Steve Wahl
Quoted 53 lines · Shrikanth Hegde
Quoted 50 lines · Steve Wahl

On large NUMA systems, while running a test program that saturates the inter-processor and inter-NUMA links, acquiring the jiffies_lock can be very expensive. If the cpu designated to do jiffies updates (tick_do_timer_cpu) gets delayed and other cpus decide to do the jiffies update themselves, a large number of them decide to do so at the same time. The inexpensive check against tick_next_period is far quicker than actually acquiring the lock, so most of these get in line to obtain the lock. If obtaining the lock is slow enough, this spirals into the vast majority of CPUs continuously being stuck waiting for this lock, just to obtain it and find out that time has already been updated by another cpu. For example, on one random entry to kdb by manually-injected NMI, I saw 2912 of 3840 cpus stuck here.

To avoid this, allow only one non-timekeeper CPU to call tick_do_update_jiffies64() at any given time, resetting ts->stalled jiffies only if the jiffies update function is actually called.

With this change, manually interrupting the test I find at most two CPUs in the tick_do_update_jiffies64 function (the timekeeper and one other).

---

v2: Rewritten to use an atomic to gate non-timekeeping cpus calling the
jiffies update, as suggested by tglx. Title of patch has changed since trylock is no longer used.

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

   kernel/time/tick-sched.c | 30 ++++++++++++++++++++++++++----
   1 file changed, 26 insertions(+), 4 deletions(-)
kernel/time/tick-sched.c +9 -0
index c527b421c865..3ff3eb1f90d0 100644
@@ -201,6 +201,27 @@ static inline void tick_sched_flag_clear(struct tick_sched *ts,
   	ts->flags &= ~flag;
   }
+/*
+ * Allow only one non-timekeeper CPU at a time update jiffies from
+ * the timer tick.
+ *
+ * Returns true if update was run.
+ */
+static bool tick_limited_update_jiffies64(struct tick_sched *ts, ktime_t now)
+{
+	static atomic_t in_progress;
Quoted 8 lines

kernel/time/tick-sched.c static inline void tick_sched_flag_clear(struct tick_sched *ts, · line 218

Quoted 6 lines
kernel/time/tick-sched.c +6 -0
+	int inp;
+
+	inp = atomic_read(&in_progress);
+	if (inp || !atomic_try_cmpxchg(&in_progress, &inp, 1))
+		return false;
+

You come here if (ts->last_tick_jiffies == jiffies). So it may be not necessary to check again.

TGLX had this in his rewrite suggestion, and I looked pretty intensely at this test.

Quoted 8 lines

The situation I'm looking to resolve is caused by inter-NUMA links being abnormally swamped with traffic. Especially for writes, access to shared memory locations, such as the atomic operations to in_progress right above this, take longer than one usually would expect. So to me it makes sense that things may have changed since the atomic_try_cmpxchg was initiated, and so I left the check in place.

I see, one possibility is

- if it runs in parallel by that time on tick_cpu.( which always updates it)

Quoted 33 lines
Quoted 27 lines
Quoted 13 lines
kernel/time/tick-sched.c +6 -1
+	if (ts->last_tick_jiffies == jiffies)
+		tick_do_update_jiffies64(now);
+	atomic_set(&in_progress, 0);
+	return true;
+}
+
   #define MAX_STALLED_JIFFIES 5
   static void tick_sched_do_timer(struct tick_sched *ts, ktime_t now)
@@ -239,10 +260,11 @@ static void tick_sched_do_timer(struct tick_sched *ts, ktime_t now)
   		ts->stalled_jiffies = 0;
   		ts->last_tick_jiffies = READ_ONCE(jiffies);
   	} else {
-		if (++ts->stalled_jiffies == MAX_STALLED_JIFFIES) {

kernel/time/tick-sched.c static void tick_sched_do_timer(struct tick_sched *ts, ktime_t now) · line 269

Quoted 10 lines
kernel/time/tick-sched.c +5 -3
-			tick_do_update_jiffies64(now);
-			ts->stalled_jiffies = 0;
-			ts->last_tick_jiffies = READ_ONCE(jiffies);
+		if (++ts->stalled_jiffies >= MAX_STALLED_JIFFIES) {
+			if (tick_limited_update_jiffies64(ts, now)) {
+				ts->stalled_jiffies = 0;
+				ts->last_tick_jiffies = READ_ONCE(jiffies);
+			}
   		}
   	}

Yes. This could help large systems.

Quoted 1 line

Thanks for your time reviewing!

--> Steve Wahl

merge bot Applied
#4 lore raw
In reply to Steve Wahl
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:     4138787408aa47e9e107f28876cb59b42d78bb99
Gitweb:        https://git.kernel.org/tip/4138787408aa47e9e107f28876cb59b42d78bb99
Author:        Steve Wahl <[email protected]>
AuthorDate:    Mon, 27 Oct 2025 13:34:56 -05:00
Committer:     Thomas Gleixner <[email protected]>
CommitterDate: Sat, 01 Nov 2025 20:25:53 +01:00

tick/sched: Limit non-timekeeper CPUs calling jiffies update

On large NUMA systems, while running a test program that saturates the inter-processor and inter-NUMA links, acquiring the jiffies_lock can be very expensive.

If the cpu designated to do jiffies updates (tick_do_timer_cpu) gets delayed and other cpus decide to do the jiffies update themselves, a large number of them decide to do so at the same time.

The inexpensive check against tick_next_period is far quicker than actually acquiring the lock, so most of these get in line to obtain the lock. If obtaining the lock is slow enough, this spirals into the vast majority of CPUs continuously being stuck waiting for this lock, just to obtain it and find out that time has already been updated by another cpu. For example, on one random entry to kdb by manually-injected NMI, 2912 of 3840 CPUs were observed to be stuck there.

To avoid this, allow only one non-timekeeper CPU to call tick_do_update_jiffies64() at any given time, resetting ts->stalled jiffies only if the jiffies update function is actually called.

With this change, manually interrupting the test at most two CPUs are observed to invoke tick_do_update_jiffies64() - the timekeeper and one other.

---
 kernel/time/tick-sched.c | 30 ++++++++++++++++++++++++++----
 1 file changed, 26 insertions(+), 4 deletions(-)
kernel/time/tick-sched.c +26 -4
index c527b42..3ff3eb1 100644
@@ -201,6 +201,27 @@ static inline void tick_sched_flag_clear(struct tick_sched *ts,
 	ts->flags &= ~flag;
 }
 
+/*
+ * Allow only one non-timekeeper CPU at a time update jiffies from
+ * the timer tick.
+ *
+ * Returns true if update was run.
+ */
+static bool tick_limited_update_jiffies64(struct tick_sched *ts, ktime_t now)
+{
+	static atomic_t in_progress;
+	int inp;
+
+	inp = atomic_read(&in_progress);
+	if (inp || !atomic_try_cmpxchg(&in_progress, &inp, 1))
+		return false;
+
+	if (ts->last_tick_jiffies == jiffies)
+		tick_do_update_jiffies64(now);
+	atomic_set(&in_progress, 0);
+	return true;
+}
+
 #define MAX_STALLED_JIFFIES 5
 
 static void tick_sched_do_timer(struct tick_sched *ts, ktime_t now)
@@ -239,10 +260,11 @@ static void tick_sched_do_timer(struct tick_sched *ts, ktime_t now)
 		ts->stalled_jiffies = 0;
 		ts->last_tick_jiffies = READ_ONCE(jiffies);
 	} else {
-		if (++ts->stalled_jiffies == MAX_STALLED_JIFFIES) {
-			tick_do_update_jiffies64(now);
-			ts->stalled_jiffies = 0;
-			ts->last_tick_jiffies = READ_ONCE(jiffies);
+		if (++ts->stalled_jiffies >= MAX_STALLED_JIFFIES) {
+			if (tick_limited_update_jiffies64(ts, now)) {
+				ts->stalled_jiffies = 0;
+				ts->last_tick_jiffies = READ_ONCE(jiffies);
+			}
 		}
 	}