Skip to contentSkip to conversation

sched/cputime: Don't warn when the tick period starts after idle exit

PATCH Applied Stian Halseth 4 participants 5 messages Oct 31 – Nov 1

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 sched/cputime: Don't warn when the tick period starts after idle exit

kcpustat_dyntick_stop() warns if idle ends before the tick period that the next tick will account has started. That happens on x86 in a CPU's first idle period with the tick stopped, right after the switch from the jiffies clocksource to tsc-early.

On the jiffies clocksource ktime only moves at the tick. When the clocksource switches some time d after a tick, that d is lost: ktime continues from the value of the last tick. The next tick comes TICK_NSEC after the last one, but ktime has only moved TICK_NSEC - d, while tick_periodic() still adds a full TICK_NSEC to tick_next_period, which is now d ahead of ktime. When NOHZ starts, tick_init_jiffy_update() rounds it up to a multiple of TICK_NSEC and the sched timer is armed there, up to nearly two ticks after now. A tick stop saves that as ts->last_tick, and an idle exit before now reaches ts->last_tick - TICK_NSEC sees the tick period in the future.

Nothing is accounted twice then. The first tick covers a period that starts after the idle exit, and the code already handles it. Drop the warning.

---
v2: rewrite the commit message, which named the wrong variable and had the wrong clocksource. The code is unchanged from v1. Add the reports from Breno and Linus.
v1: https://lore.kernel.org/all/[email protected]/

 kernel/sched/cputime.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
kernel/sched/cputime.c +6 -1
index 9e0fb8633d823..17f3cbd5d403a 100644
@@ -478,7 +478,12 @@ void kcpustat_dyntick_stop(u64 now, u64 tick_start)
 			kc->idle_tick_overlap = 0;
 		}
 		tick_start = max(tick_start, kc->idle_dyntick_entry);
-		if (!WARN_ON_ONCE(now < tick_start))
+		/*
+		 * After a switch from the jiffies clocksource the tick can be
+		 * armed more than a tick ahead, so the period may not have
+		 * started yet.
+		 */
+		if (now > tick_start)
 			kc->idle_tick_overlap += now - tick_start;
 		kcpustat_idle_stop(kc, now);
 		kc->idle_dyntick = false;
Signature · 1 line
2.43.0
Reviewed-by on v2 1/1
#1 lore raw
Quoted 23 lines · Stian Halseth
 kernel/sched/cputime.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
kernel/sched/cputime.c +6 -1
index 9e0fb8633d823..17f3cbd5d403a 100644
@@ -478,7 +478,12 @@ void kcpustat_dyntick_stop(u64 now, u64 tick_start)
 			kc->idle_tick_overlap = 0;
 		}
 		tick_start = max(tick_start, kc->idle_dyntick_entry);
-		if (!WARN_ON_ONCE(now < tick_start))
+		/*
+		 * After a switch from the jiffies clocksource the tick can be
+		 * armed more than a tick ahead, so the period may not have
+		 * started yet.
+		 */
+		if (now > tick_start)
 			kc->idle_tick_overlap += now - tick_start;
 		kcpustat_idle_stop(kc, now);
 		kc->idle_dyntick = false;
Signature · 1 line
2.43.0

TICK_NSEC = 4

         switch
            |
            v

Real: 8 9 10 11 12 13 14 15 16 17 18

Time: 8 8 8 9 10 11 12 13 14 15 16

Tick: 8 8 8 8 10 10 10 10 10 10 16

So if we enter idle at real-12, and exit at real-13 this happens:

last_tick is 16, so last_tick - 4 = 12. But normally, last_tick needs to be 14 and tick_start needs to be 10. Or am I missing something?

IDK if there is a better solution tho.
Maybe fixing it from the root would be better. If its even possible to do it cleanly...

But this code works correct.

Thanks,

Kayra

#2 lore raw

Hi Kayra,

Quoted 10 lines · Kayra Cizmeci

TICK_NSEC = 4

         switch
            |
            v
Real: 8 9 10 11 12 13 14 15 16 17 18

Time: 8 8 8  9  10 11 12 13 14 15 16

Quoted 7 lines

Tick: 8 8 8  8  10 10 10 10 10 10 16

So if we enter idle at real-12, and exit at real-13 this happens:

last_tick is 16, so last_tick - 4 = 12. But normally, last_tick needs to be 14 and tick_start needs to be 10. Or am I missing something?

Your diagram is correct AFAICT, and that's the case this patch is about.

10 and 14 would follow the old periodic ticks, but after the switch the tick runs on a grid that is d ahead of them.

Idle ends at 11, before tick_start at 12. The code already accounted that correctly but warned about it, and v2 drops the warning.

Quoted 4 lines

IDK if there is a better solution tho.
Maybe fixing it from the root would be better. If its even possible to do it cleanly...

Someone might have a better idea, but maybe we could realign tick_next_period when the clocksource switches away from jiffies.

I kept v2 to the warning though, since this is a regression in the current release, and a change to the tick setup could be a potential follow-up.

Thanks for the review!

/Stian

Reviewed-by on v2 1/1
#3 lore raw
In reply to Stian Halseth
Quoted 20 lines · Stian Halseth

kcpustat_dyntick_stop() warns if idle ends before the tick period that the next tick will account has started. That happens on x86 in a CPU's first idle period with the tick stopped, right after the switch from the jiffies clocksource to tsc-early.

On the jiffies clocksource ktime only moves at the tick. When the clocksource switches some time d after a tick, that d is lost: ktime continues from the value of the last tick. The next tick comes TICK_NSEC after the last one, but ktime has only moved TICK_NSEC - d, while tick_periodic() still adds a full TICK_NSEC to tick_next_period, which is now d ahead of ktime. When NOHZ starts, tick_init_jiffy_update() rounds it up to a multiple of TICK_NSEC and the sched timer is armed there, up to nearly two ticks after now. A tick stop saves that as ts->last_tick, and an idle exit before now reaches ts->last_tick - TICK_NSEC sees the tick period in the future.

Nothing is accounted twice then. The first tick covers a period that starts after the idle exit, and the code already handles it. Drop the warning.

Quoted 8 lines

Thanks!

Signature · 2 lines
Frederic Weisbecker
SUSE Labs
merge bot Applied
#4 lore raw
In reply to Stian Halseth
The following commit has been merged into the sched/urgent branch of tip:

The following commit has been merged into the sched/urgent branch of tip:

Commit-ID:     b327e63683a2b894b500027375342c25222b58ef
Gitweb:        https://git.kernel.org/tip/b327e63683a2b894b500027375342c25222b58ef
Author:        Stian Halseth <[email protected]>
AuthorDate:    Sat, 31 Oct 2026 09:14:07 +01:00
Committer:     Ingo Molnar <[email protected]>
CommitterDate: Sun, 01 Nov 2026 07:32:38 +01:00

sched/cputime: Don't warn when the tick period starts after idle exit

kcpustat_dyntick_stop() warns if idle ends before the tick period that the next tick will account has started. That happens on x86 in a CPU's first idle period with the tick stopped, right after the switch from the jiffies clocksource to tsc-early.

On the jiffies clocksource ktime only moves at the tick. When the clocksource switches some time d after a tick, that d is lost: ktime continues from the value of the last tick. The next tick comes TICK_NSEC after the last one, but ktime has only moved TICK_NSEC - d, while tick_periodic() still adds a full TICK_NSEC to tick_next_period, which is now d ahead of ktime. When NOHZ starts, tick_init_jiffy_update() rounds it up to a multiple of TICK_NSEC and the sched timer is armed there, up to nearly two ticks after now. A tick stop saves that as ts->last_tick, and an idle exit before now reaches ts->last_tick - TICK_NSEC sees the tick period in the future.

Nothing is accounted twice then. The first tick covers a period that starts after the idle exit, and the code already handles it. Drop the warning.

--

 kernel/sched/cputime.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
---
 kernel/sched/cputime.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
kernel/sched/cputime.c +6 -1
index 9e0fb86..17f3cbd 100644
@@ -478,7 +478,12 @@ void kcpustat_dyntick_stop(u64 now, u64 tick_start)
 			kc->idle_tick_overlap = 0;
 		}
 		tick_start = max(tick_start, kc->idle_dyntick_entry);
-		if (!WARN_ON_ONCE(now < tick_start))
+		/*
+		 * After a switch from the jiffies clocksource the tick can be
+		 * armed more than a tick ahead, so the period may not have
+		 * started yet.
+		 */
+		if (now > tick_start)
 			kc->idle_tick_overlap += now - tick_start;
 		kcpustat_idle_stop(kc, now);
 		kc->idle_dyntick = false;