mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: David Woodhouse <dwmw2@infradead.org>
To: Thomas Gleixner <tglx@kernel.org>, John Stultz <jstultz@google.com>
Cc: Stephen Boyd <sboyd@kernel.org>,
	Miroslav Lichvar <mlichvar@redhat.com>,
	Rodolfo Giometti <giometti@enneenne.com>,
	Ryan Luu <rluu@amazon.com>, Julien Ridoux <ridouxj@amazon.com>,
	linux-kernel@vger.kernel.org,
	David Woodhouse <dwmw2@infradead.org>,
	David Woodhouse <dwmw@amazon.co.uk>
Subject: [PATCH 3/5] timekeeping: Bound idle sleep while a phase slew is in flight
Date: Thu,  1 Oct 2026 21:21:32 +0100	[thread overview]
Message-ID: <20261001202134.33929-4-dwmw2@infradead.org> (raw)
In-Reply-To: <20261001202134.33929-1-dwmw2@infradead.org>

From: David Woodhouse <dwmw@amazon.co.uk>

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@amazon.co.uk>
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


  parent reply	other threads:[~2026-10-01 20:21 UTC|newest]

Thread overview: 27+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-10-01 20:21 [PATCH 0/5] timekeeping: Reduce magnitude of ntp_error David Woodhouse
2026-10-01 20:21 ` [PATCH 1/5] timekeeping: Allow tick_length changes to apply mid-tick David Woodhouse
2026-10-21 19:01   ` John Stultz
2026-10-22 13:39     ` David Woodhouse
2026-10-22 19:40       ` John Stultz
2026-10-01 20:21 ` [PATCH 2/5] ntp: Recalculate skew_delta when the phase offset changes David Woodhouse
2026-10-21 19:05   ` John Stultz
2026-10-01 20:21 ` David Woodhouse [this message]
2026-10-22 21:14   ` [PATCH 3/5] timekeeping: Bound idle sleep while a phase slew is in flight John Stultz
2026-10-22 21:49     ` David Woodhouse
2026-10-01 20:21 ` [PATCH 4/5] timekeeping: Reinstate proportional correction of ntp_error David Woodhouse
2026-10-05 10:04   ` Miroslav Lichvar
2026-10-05 13:17     ` David Woodhouse
2026-10-22 21:09   ` John Stultz
2026-10-01 20:21 ` [PATCH 5/5] [DO NOT MERGE] timekeeping: Apply extrapolated ntp_error to clock snapshots David Woodhouse
2026-10-02  8:30   ` Rodolfo Giometti
2026-10-02  9:14     ` David Woodhouse
2026-10-02 10:26       ` Rodolfo Giometti
2026-10-02 22:47         ` David Woodhouse
2026-10-05  8:15           ` Rodolfo Giometti
2026-10-05  8:49             ` David Woodhouse
2026-10-05 10:15               ` Rodolfo Giometti
2026-10-05 10:49                 ` David Woodhouse
2026-10-21 14:37 ` [PATCH 0/5] timekeeping: Reduce magnitude of ntp_error David Woodhouse
2026-10-22  9:05   ` Rodolfo Giometti
2026-10-22 13:52     ` Miroslav Lichvar
2026-10-22 16:17       ` David Woodhouse

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261001202134.33929-4-dwmw2@infradead.org \
    --to=dwmw2@infradead.org \
    --cc=dwmw@amazon.co.uk \
    --cc=giometti@enneenne.com \
    --cc=jstultz@google.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mlichvar@redhat.com \
    --cc=ridouxj@amazon.com \
    --cc=rluu@amazon.com \
    --cc=sboyd@kernel.org \
    --cc=tglx@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®