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

From: Frederic Weisbecker

Date: Sat Oct 31 2026 - 09:55:06 EST


Le Wed, Oct 28, 2026 at 06:37:22PM +0100, Stian Halseth a écrit :
> Hi Frederic,
>
> On Wed, 2026-10-28 at 15:14 +0100, Frederic Weisbecker wrote:
> >
> > So let me depict that:
> >
> > // clocksource = jiffies ?
> > // clockevent = lapic ?
> > tick_setup_device()
> >    tick_next_period = ktime_get() // jiffies_to_nsecs(jiffies)
> >
> > tick_periodic()
> >    tick_next_period += TICK_NSECS
> >    jiffies++ // ktime_get() == tick_next_period
> >
> > // jiffies > tsc_early
> > clocksource_select()
> >
> > tick_periodic()
> >    //one tick has passed, so ktime_get() == jiffies + 1
>
> Here is where it goes wrong. One tick has passed in real time, but not
> in ktime. The switch happened d after the last tick, jiffies never
> counted that d, and the tsc only counts from the switch on, so here
> ktime_get() == jiffies + TICK_NSEC - d.

So that assume that during the clocksource switch, the new clock synchronize
with the old one so that ktime_get() from the new clock should start from the
last value of the old clock. I'm having troubles to verify if
tk_setup_internals() does exactly that. Trying to recollect the few pieces of
my brain lost in the process.

I just added this patch to see what happens in practice:

diff --git a/kernel/time/timekeeping.c b/kernel/time/timekeeping.c
index ea2e6e55f37b..e8f3fc435933 100644
--- a/kernel/time/timekeeping.c
+++ b/kernel/time/timekeeping.c
@@ -1854,6 +1854,7 @@ static int change_clocksource(void *data)
{
struct clocksource *new = data, *old = NULL;

+ printk("old= %lld\n", ktime_get());
/*
* If the clocksource is in a module, get a module reference.
* Succeeds for built-in code (owner == NULL) as well. Abort if the
@@ -1876,7 +1877,7 @@ static int change_clocksource(void *data)
tk_setup_internals(tks, new);
timekeeping_update_from_shadow(&tk_core, TK_UPDATE_ALL);
}
-
+ printk("new= %lld\n", ktime_get());
tk_aux_update_clocksource();

if (old) {

And I get this dump when jiffies switched to tsc_early:

[ 1.549194] old= 1352575566
[ 1.549930] new= 1352578748
[ 1.550636] clocksource: Switched to clocksource tsc-early

That's 3 us, it could be the simple difference between the 2 printk.

But the switch from tsc-early to tsc is much more:

[ 2.639818] old= 2445754540
[ 2.640172] new= 2446107950
[ 2.640927] clocksource: Switched to clocksource tsc

That's 353 us.

What do you think?