[PATCH 3/5] timekeeping: Bound idle sleep while a phase slew is in flight

From: David Woodhouse

Date: Thu Oct 01 2026 - 16:22:03 EST


From: David Woodhouse <dwmw@xxxxxxxxxxxx>

Phase offset for adjtime() can be delivered at a rate of 500µs/s, and
for STA_PPSTIME potentially even more. The skew is recalculated in
second_overflow() each second, and if that's missed in a tickless
kernel, the skew continues to be applied for the whole of the idle
period. While skew is active, clamp timekeeping_max_deferment() to the
top of the current second to avoid the overshoot.

Signed-off-by: David Woodhouse <dwmw@xxxxxxxxxxxx>
Assisted-by: LLM
---
kernel/time/timekeeping.c | 27 +++++++++++++++++++++++++--
1 file changed, 25 insertions(+), 2 deletions(-)

diff --git a/kernel/time/timekeeping.c b/kernel/time/timekeeping.c
index e2f7e28dc16e..986649cae46d 100644
--- a/kernel/time/timekeeping.c
+++ b/kernel/time/timekeeping.c
@@ -1975,6 +1975,21 @@ u64 timekeeping_max_deferment(void)

ret = tk->tkr_mono.clock->max_idle_ns;

+ /*
+ * Skew introduced for phase offset is intended to be
+ * recalculated each second. Ensure a tickless kernel
+ * wakes up for that and does not overshoot, while skew
+ * is being applied.
+ */
+ if (abs(tk->skew_delta) > 1) {
+ u64 nsec_in_sec = tk->tkr_mono.xtime_nsec >>
+ tk->tkr_mono.shift;
+ u64 remaining = NSEC_PER_SEC - min_t(u64, nsec_in_sec,
+ NSEC_PER_SEC);
+
+ ret = min_t(u64, ret, max_t(u64, remaining, TICK_NSEC));
+ }
+
} while (read_seqcount_retry(&tk_core.seq, seq));

return ret;
@@ -3045,8 +3060,16 @@ static int __do_adjtimex(struct tk_data *tkd, struct __kernel_timex *txc,
tk_update_leap_state_all(tkd);
}

- /* Update the multiplier immediately if frequency was set directly */
- if (txc->modes & (ADJ_FREQUENCY | ADJ_TICK))
+ /*
+ * Update the multiplier immediately if the frequency was set
+ * directly, or refresh the cached skew_delta if a phase
+ * adjustment started a slew, so that it bounds idle sleep.
+ * ADJ_OFFSET_READONLY excludes the unprivileged read-only
+ * adjtime() form, which starts no slew.
+ */
+ if ((txc->modes & (ADJ_FREQUENCY | ADJ_TICK)) ||
+ ((txc->modes & ADJ_OFFSET) &&
+ !(txc->modes & ADJ_OFFSET_READONLY)))
result->clock_set |= __timekeeping_advance(tkd, TK_ADV_FREQ);

return ret;
--
2.43.0