From: David Woodhouse <dwmw2@infradead.org>
To: "Richard Cochran" <richardcochran@gmail.com>,
"Wen Gu" <guwen@linux.alibaba.com>,
"David Woodhouse" <dwmw2@infradead.org>,
"Andrew Lunn" <andrew+netdev@lunn.ch>,
"David S. Miller" <davem@davemloft.net>,
"Eric Dumazet" <edumazet@google.com>,
"Jakub Kicinski" <kuba@kernel.org>,
"Paolo Abeni" <pabeni@redhat.com>,
"John Stultz" <jstultz@google.com>,
"Thomas Gleixner" <tglx@kernel.org>,
"Stephen Boyd" <sboyd@kernel.org>,
"Anna-Maria Behnsen" <anna-maria@linutronix.de>,
"Frederic Weisbecker" <frederic@kernel.org>,
"Shuah Khan" <shuah@kernel.org>,
"Peter Zijlstra" <peterz@infradead.org>,
"Thomas Weißschuh" <thomas.weissschuh@linutronix.de>,
"Arnd Bergmann" <arnd@arndb.de>,
"Miroslav Lichvar" <mlichvar@redhat.com>,
"Julien Ridoux" <ridouxj@amazon.com>,
"Ryan Luu" <rluu@amazon.com>,
linux-kernel@vger.kernel.org
Cc: David Woodhouse <dwmw@amazon.co.uk>
Subject: [RFC PATCH v3 03/10] timekeeping: Account for monotonicity adjustment in ntp_error
Date: Wed, 20 May 2026 14:33:42 +0100 [thread overview]
Message-ID: <20260520135207.37826-4-dwmw2@infradead.org> (raw)
In-Reply-To: <20260520135207.37826-1-dwmw2@infradead.org>
From: David Woodhouse <dwmw@amazon.co.uk>
timekeeping_apply_adjustment() modifies xtime_nsec to maintain vDSO
monotonicity when mult changes:
xtime_nsec -= offset
This ensures that the time reported to userspace does not jump when the
multiplier is adjusted from one tick to the next. However, the ntp_error
accumulator which tracks the difference between intended and actual
clock position was not being updated updated to reflect this additional
discrepancy.
An earlier attempt at this compensation existed as:
ntp_error -= (interval - offset) << ntp_error_shift
but was removed in commit c2cda2a5bda9 ("timekeeping/ntp: Don't align
NTP frequency adjustments to ticks") because it was a major source of
NTP error. That's because (interval - offset) was wrong: the subtraction
of "interval" prematurely accounted for the changed xtime_interval of
the next tick, which would be correctly accounted in the next
accumulation anyway — a double subtraction.
What is actually needed is just the "offset" part: ntp_error must be
told that xtime_nsec moved by "offset" without a corresponding change
in the intended position. For the normal ±1 mult dithering this is
negligible (the adjustments cancel over time), but for larger mult
changes — such as when an external reference clock sets a new
frequency — the one-time uncompensated offset is significant.
Fix by adjusting ntp_error by the correct amount:
ntp_error += offset << ntp_error_shift
This keeps ntp_error consistent with the actual xtime_nsec position
after the adjustment.
Fixes: c2cda2a5bda9 ("timekeeping/ntp: Don't align NTP frequency adjustments to ticks")
Signed-off-by: David Woodhouse <dwmw@amazon.co.uk>
Assisted-by: Kiro:claude-opus-4.6-1m
---
kernel/time/timekeeping.c | 1 +
1 file changed, 1 insertion(+)
diff --git a/kernel/time/timekeeping.c b/kernel/time/timekeeping.c
index b84b05f9d460..95973e45d456 100644
--- a/kernel/time/timekeeping.c
+++ b/kernel/time/timekeeping.c
@@ -2317,6 +2317,7 @@ static __always_inline void timekeeping_apply_adjustment(struct timekeeper *tk,
tk->tkr_mono.mult += mult_adj;
tk->xtime_interval += interval;
tk->tkr_mono.xtime_nsec -= offset;
+ tk->ntp_error += offset << tk->ntp_error_shift;
}
/*
--
2.54.0
next prev parent reply other threads:[~2026-05-20 13:52 UTC|newest]
Thread overview: 21+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-05-20 13:33 [RFC PATCH v3 00/10] timekeeping: Fix drift tracking precision and add feed-forward discipline via vmclock David Woodhouse
2026-05-20 13:33 ` [RFC PATCH v3 01/10] MAINTAINERS: Add Miroslav as timekeeping reviewer David Woodhouse
2026-05-20 23:28 ` John Stultz
2026-05-20 13:33 ` [RFC PATCH v3 02/10] timekeeping: Remove xtime_remainder from ntp_error accumulation David Woodhouse
2026-05-22 11:41 ` David Woodhouse
2026-05-29 23:21 ` John Stultz
2026-05-30 10:51 ` David Woodhouse
2026-05-20 13:33 ` David Woodhouse [this message]
2026-05-20 23:27 ` [RFC PATCH v3 03/10] timekeeping: Account for monotonicity adjustment in ntp_error John Stultz
2026-05-20 23:36 ` David Woodhouse
2026-05-20 13:33 ` [RFC PATCH v3 04/10] timekeeping: Guard against divide-by-zero in timekeeping_adjust David Woodhouse
2026-05-20 23:29 ` John Stultz
2026-05-20 13:33 ` [RFC PATCH v3 05/10] timekeeping: Drive time_offset skew via per-tick ntp_error transfer David Woodhouse
2026-05-20 13:33 ` [RFC PATCH v3 06/10] ntp: Convert adjtime() to use time_offset instead of tick_length inflation David Woodhouse
2026-05-20 13:33 ` [RFC PATCH v3 07/10] ntp: Remove tick_length_base, use tick_length directly David Woodhouse
2026-05-20 13:33 ` [RFC PATCH v3 08/10] timekeeping: Add absolute reference for feed-forward clock discipline David Woodhouse
2026-05-20 13:33 ` [RFC PATCH v3 09/10] ptp_vmclock: Feed reference to timekeeping for feed-forward discipline David Woodhouse
2026-05-20 13:33 ` [RFC PATCH v3 10/10] kernel/time: Add /dev/vmclock_host miscdev David Woodhouse
2026-05-21 6:29 ` Wen Gu
2026-05-28 16:18 ` David Woodhouse
2026-05-23 11:49 ` [RFC PATCH v3 00/10] timekeeping: Fix drift tracking precision and add feed-forward discipline via vmclock 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=20260520135207.37826-4-dwmw2@infradead.org \
--to=dwmw2@infradead.org \
--cc=andrew+netdev@lunn.ch \
--cc=anna-maria@linutronix.de \
--cc=arnd@arndb.de \
--cc=davem@davemloft.net \
--cc=dwmw@amazon.co.uk \
--cc=edumazet@google.com \
--cc=frederic@kernel.org \
--cc=guwen@linux.alibaba.com \
--cc=jstultz@google.com \
--cc=kuba@kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=mlichvar@redhat.com \
--cc=pabeni@redhat.com \
--cc=peterz@infradead.org \
--cc=richardcochran@gmail.com \
--cc=ridouxj@amazon.com \
--cc=rluu@amazon.com \
--cc=sboyd@kernel.org \
--cc=shuah@kernel.org \
--cc=tglx@kernel.org \
--cc=thomas.weissschuh@linutronix.de \
/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®