mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: David Woodhouse <dwmw2@infradead.org>
To: John Stultz <jstultz@google.com>
Cc: Thomas Gleixner <tglx@kernel.org>,
	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
Subject: Re: [PATCH 1/5] timekeeping: Allow tick_length changes to apply mid-tick
Date: Thu, 22 Oct 2026 14:39:23 +0100	[thread overview]
Message-ID: <16216028b040ea38556bb59b3a8ce6e588ac5b38.camel@infradead.org> (raw)
In-Reply-To: <CANDhNCqY+RRCCXV+HfMNKf5Rmco7Y+kZ0RHv5sYC2dnw2U+V=A@mail.gmail.com>

[-- Attachment #1: Type: text/plain, Size: 6917 bytes --]

On Wed, 2026-10-21 at 12:01 -0700, John Stultz wrote:
> On Thu, Oct 1, 2026 at 1:21 PM David Woodhouse <dwmw2@infradead.org> wrote:
> > 
> > From: David Woodhouse <dwmw@amazon.co.uk>
> > 
> > When the NTP tick length changes, timekeeping_apply_adjustment() applies
> > the accounting for the new rate to ntp_error retrospectively for the
> > partial tick which is currently in progress. So the ideal line of the
> > clock conceptually changes from the moment of cycle_last (which can be
> > almost a full tick ago), and ntp_error grows to show the difference
> > between that and what userspace actually saw already.
> > 
> > This can lead to a significant value (10s of microseconds) landing in
> > ntp_error which can take *days* to drain through the natural mult ±1
> > dithering.
> > 
> > The slow-draining ntp_error then applies a consistent bias to that ±1
> > dithering, meaning that the precision of the effective frequency
> > requested by userspace is limited to integer 'mult' values, since the
> > dithering is no longer effective to achieve the fractional parts.
> > 
> > Fix this by allowing the rate change to be conceptually applied at
> > 'offset' into the partial tick, rather than the start of the tick.
> > This means that the amount of delta which gets accumulated into
> > ntp_error remains low.
> > 
> > Rather than factoring this into timekeeping_apply_adjustment() which
> > already makes my eyes bleed, pre-correct it when the rate changes, to
> > adjust the 'intended' clock position according to the delta between
> > old and new tick lengths (but leaving skew as it should be).
> > 
> > Signed-off-by: David Woodhouse <dwmw@amazon.co.uk>
> > Assisted-by: LLM
> > ---
> >  kernel/time/timekeeping.c | 17 +++++++++++++++++
> >  1 file changed, 17 insertions(+)
> > 
> > diff --git a/kernel/time/timekeeping.c b/kernel/time/timekeeping.c
> > index ea2e6e55f37b..e2f7e28dc16e 100644
> > --- a/kernel/time/timekeeping.c
> > +++ b/kernel/time/timekeeping.c
> > @@ -2444,6 +2444,8 @@ static void timekeeping_adjust(struct timekeeper *tk, s64 offset)
> >                 /* Revert to the base mult rate. */
> >                 mult = tk->tkr_mono.mult - tk->ntp_err_mult;
> >         } else {
> > +               u64 old_ntp_tick = tk->ntp_tick;
> > +
> >                 tk->ntp_tick = ntp_tl;
> >                 tk->skew_delta = skew;
> >                 /*
> > @@ -2453,6 +2455,21 @@ static void timekeeping_adjust(struct timekeeper *tk, s64 offset)
> >                 skew *= NTP_INTERVAL_FREQ;
> >                 mult = div64_u64((tk->ntp_tick + skew) >> tk->ntp_error_shift,
> >                                  tk->cycle_interval);
> > +
> > +               /*
> > +                * Rate adjustments are conceptually applied mid-tick in order
> > +                * to avoid accumulating large deltas in ntp_error which can
> > +                * take days to drain. Offset the ntp_error that will be
> > +                * introduced by timekeeping_apply_adjustment(), accordingly.
> > +                */
> > +               if (tk->ntp_tick != old_ntp_tick) {
> > +                       u64 old_dividend = (old_ntp_tick + skew) >> tk->ntp_error_shift;
> > +                       s64 old_base_mult = div64_u64(old_dividend, tk->cycle_interval);
> 
> Total nit, but splitting the math up this way makes it less clear to
> dim-old-me as to what you're calculating.
> Maybe instead:
> s64 old_mult_w_skew = div64_u64((old_ntp_tick + skew) >> tk->ntp_error_shift,
>                                 tk->cycle_interval);
> 
> Might more obviously visually match the logic above where mult is
> calculated (or alternatively you could alter the mult calcualtion to
> match what you have here)?
> 

In fact I already do the latter in patch 5 (as I need 'dividend' again
to calculate the ntp_err_frac divergence rate), so it ends up looking
like this, and does match:

→		dividend = (tk->ntp_tick + skew) >> tk->ntp_error_shift;
→		mult = div64_u64(dividend, tk->cycle_interval);
		/*
		 * Stash the fractional part of the per-cycle ideal mult that
		 * the integer @mult discards, scaled by 2^32, in clock-shifted
		 * ns per cycle.
		 */
		dividend -= (u64)mult * tk->cycle_interval;
		tk->ntp_err_frac = div64_u64(dividend << 32, tk->cycle_interval);

		/*
		 * Rate adjustments are conceptually applied mid-tick in order
		 * to avoid accumulating large deltas in ntp_error which can
		 * take days to drain. Offset the ntp_error that will be
		 * introduced by timekeeping_apply_adjustment(), accordingly.
		 */
		if (tk->ntp_tick != old_ntp_tick) {
→			u64 old_dividend = (old_ntp_tick + skew) >> tk->ntp_error_shift;
→			s64 old_base_mult = div64_u64(old_dividend, tk->cycle_interval);
			s64 mult_delta = (s64)mult - old_base_mult;

			tk->ntp_error -= ((s64)offset * mult_delta)
					 << tk->ntp_error_shift;
		}


I can happily pull the cosmetic part up to this patch, leaving those
first two highlighted lines exactly as they are for symmetry, before
patch 5 adds the extra bit in the middle.


> > +                       s64 mult_delta = (s64)mult - old_base_mult;
> 
> It might be useful to add a comment clarifying exactly what the
> mult_delta here represents vs the (mult - tk->tkr_mono.mult) value
> later passed to timekeeping_apply_adjustment?

Rather than "add a comment", I think that should actually be "rewrite
the existing four-line comment cited above that clearly isn't doing its
job".  How about this...

	/*
	 * In timekeeping_apply_adjustment(), the mult_adj value is
	 * retrospectively applied as if it had come into effect at
	 * the *start* of the current tick.  Any discrepancy due to
	 * the fact that time has *actually* been counted at the old
	 * mult rate between the start of the tick and now ('offset'
	 * cycles into that tick) is dumped into ntp_error.
	 *
	 * When tick_length is changed by a large amount, that can
	 * lead to tens of microseconds of ntp_error being accrued.
	 *
	 * Fix this by conceptually applying the tick_length change
	 * now (at 'offset'), rather than the start of the tick.
	 *
	 * That means calculating what 'mult' would have been with
	 * the old ntp_tick length (but the *current* value of skew),
	 * and pre-adjusting ntp_error to compensate.
	 */

It's more than four lines now, but if we want it fully stated in the
code rather than just the commit message, I don't think it could be
much smaller.

[-- Attachment #2: smime.p7s --]
[-- Type: application/pkcs7-signature, Size: 6179 bytes --]

  reply	other threads:[~2026-10-22 13:39 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 [this message]
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 ` [PATCH 3/5] timekeeping: Bound idle sleep while a phase slew is in flight David Woodhouse
2026-10-22 21:14   ` 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=16216028b040ea38556bb59b3a8ce6e588ac5b38.camel@infradead.org \
    --to=dwmw2@infradead.org \
    --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®