]> git.ipfire.org Git - thirdparty/kernel/linux.git/commitdiff
tick/sched: Avoid hrtimer_cancel/start() sequence
authorThomas Gleixner <tglx@kernel.org>
Tue, 24 Feb 2026 16:36:10 +0000 (17:36 +0100)
committerPeter Zijlstra <peterz@infradead.org>
Fri, 27 Feb 2026 15:40:06 +0000 (16:40 +0100)
The sequence of cancel and start is inefficient. It has to do the timer
lock/unlock twice and in the worst case has to reprogram the underlying
clock event device twice.

The reason why it is done this way is the usage of hrtimer_forward_now(),
which requires the timer to be inactive.

But that can be completely avoided as the forward can be done on a variable
and does not need any of the overrun accounting provided by
hrtimer_forward_now().

Implement a trivial forwarding mechanism and replace the cancel/reprogram
sequence with hrtimer_start(..., new_expiry).

For the non high resolution case the timer is not actually armed, but used
for storage so that code checking for expiry times can unconditially look
it up in the timer. So it is safe for that case to set the new expiry time
directly.

Signed-off-by: Thomas Gleixner <tglx@kernel.org>
Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org>
Link: https://patch.msgid.link/20260224163429.542178086@kernel.org
kernel/time/tick-sched.c

index f7907fadd63f211e1aef531db239e1a4cd140361..9e5264458414a914fc923711beb5d09c5b2321df 100644 (file)
@@ -864,19 +864,32 @@ u64 get_cpu_iowait_time_us(int cpu, u64 *last_update_time)
 }
 EXPORT_SYMBOL_GPL(get_cpu_iowait_time_us);
 
+/* Simplified variant of hrtimer_forward_now() */
+static ktime_t tick_forward_now(ktime_t expires, ktime_t now)
+{
+       ktime_t delta = now - expires;
+
+       if (likely(delta < TICK_NSEC))
+               return expires + TICK_NSEC;
+
+       expires += TICK_NSEC * ktime_divns(delta, TICK_NSEC);
+       if (expires > now)
+               return expires;
+       return expires + TICK_NSEC;
+}
+
 static void tick_nohz_restart(struct tick_sched *ts, ktime_t now)
 {
-       hrtimer_cancel(&ts->sched_timer);
-       hrtimer_set_expires(&ts->sched_timer, ts->last_tick);
+       ktime_t expires = ts->last_tick;
 
-       /* Forward the time to expire in the future */
-       hrtimer_forward(&ts->sched_timer, now, TICK_NSEC);
+       if (now >= expires)
+               expires = tick_forward_now(expires, now);
 
        if (tick_sched_flag_test(ts, TS_FLAG_HIGHRES)) {
-               hrtimer_start_expires(&ts->sched_timer,
-                                     HRTIMER_MODE_ABS_PINNED_HARD);
+               hrtimer_start(&ts->sched_timer, expires, HRTIMER_MODE_ABS_PINNED_HARD);
        } else {
-               tick_program_event(hrtimer_get_expires(&ts->sched_timer), 1);
+               hrtimer_set_expires(&ts->sched_timer, expires);
+               tick_program_event(expires, 1);
        }
 
        /*