mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
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 08/10] timekeeping: Add absolute reference for feed-forward clock discipline
Date: Wed, 20 May 2026 14:33:47 +0100	[thread overview]
Message-ID: <20260520135207.37826-9-dwmw2@infradead.org> (raw)
In-Reply-To: <20260520135207.37826-1-dwmw2@infradead.org>

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

Add timekeeping_set_reference() which allows an external clock source
(such as a hypervisor vmclock) to provide an absolute time reference.
The reference defines a linear counter-to-time mapping that the kernel
uses to set both the frequency and phase of the system clock.

When timekeeping_set_reference() is called:
 - tick_length is computed from the reference period and set via
   ntp_set_tick_length()
 - the phase delta is set via ntp_set_time_offset()

So the NTP state is entirely consistent, and the existing time_offset
skew mechanism then converges the clock to the reference, with the
ntp_error and time_offset accumulators staying accurate throughout.

Signed-off-by: David Woodhouse <dwmw@amazon.co.uk>
Assisted-by: Kiro:claude-opus-4.6-1m
---
 include/linux/timekeeping_reference.h | 19 ++++++++++++++
 kernel/time/ntp.c                     | 14 ++++++++++
 kernel/time/ntp_internal.h            |  1 +
 kernel/time/timekeeping.c             | 38 +++++++++++++++++++++++++++
 4 files changed, 72 insertions(+)
 create mode 100644 include/linux/timekeeping_reference.h

diff --git a/include/linux/timekeeping_reference.h b/include/linux/timekeeping_reference.h
new file mode 100644
index 000000000000..4c1d8a6c02f1
--- /dev/null
+++ b/include/linux/timekeeping_reference.h
@@ -0,0 +1,19 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+#ifndef _LINUX_TIMEKEEPING_REFERENCE_H
+#define _LINUX_TIMEKEEPING_REFERENCE_H
+
+#include <linux/clocksource_ids.h>
+#include <linux/types.h>
+
+struct tk_reference {
+	enum clocksource_ids	cs_id;
+	u64			counter_value;
+	u64			time_sec;
+	u64			time_frac_sec;
+	u64			period_frac_sec;
+	u8			period_shift;
+};
+
+int timekeeping_set_reference(const struct tk_reference *ref);
+
+#endif
diff --git a/kernel/time/ntp.c b/kernel/time/ntp.c
index 4494f258dd86..3dc098695665 100644
--- a/kernel/time/ntp.c
+++ b/kernel/time/ntp.c
@@ -403,6 +403,20 @@ void ntp_set_time_offset(unsigned int tkid, s64 offset_ns)
 	ntpdata->time_adjust = 0;
 }
 
+void ntp_set_tick_length(unsigned int tkid, u64 tick_length)
+{
+	struct ntp_data *ntpdata = &tk_ntp_data[tkid];
+	u64 base;
+
+	/* Compute the nominal second length (without frequency adjustment) */
+	base = (u64)(ntpdata->tick_usec * NSEC_PER_USEC * USER_HZ)
+		<< NTP_SCALE_SHIFT;
+	base += ntpdata->ntp_tick_adj;
+
+	ntpdata->time_freq = (s64)(tick_length * NTP_INTERVAL_FREQ - base);
+	ntp_update_frequency(ntpdata);
+}
+
 /**
  * ntp_get_next_leap - Returns the next leapsecond in CLOCK_REALTIME ktime_t
  * @tkid:	Timekeeper ID
diff --git a/kernel/time/ntp_internal.h b/kernel/time/ntp_internal.h
index 639860ff2baf..14ca8bc08120 100644
--- a/kernel/time/ntp_internal.h
+++ b/kernel/time/ntp_internal.h
@@ -9,6 +9,7 @@ extern u64 ntp_tick_length(unsigned int tkid);
 extern s64 ntp_get_skew_delta(unsigned int tkid);
 extern s64 ntp_drain_time_offset(unsigned int tkid, s64 amount);
 extern void ntp_set_time_offset(unsigned int tkid, s64 offset_ns);
+extern void ntp_set_tick_length(unsigned int tkid, u64 tick_length);
 extern ktime_t ntp_get_next_leap(unsigned int tkid);
 extern int second_overflow(unsigned int tkid, time64_t secs);
 extern int ntp_adjtimex(unsigned int tkid, struct __kernel_timex *txc, const struct timespec64 *ts,
diff --git a/kernel/time/timekeeping.c b/kernel/time/timekeeping.c
index 27b2a093b138..5c4b377505bc 100644
--- a/kernel/time/timekeeping.c
+++ b/kernel/time/timekeeping.c
@@ -2324,6 +2324,44 @@ static __always_inline void timekeeping_apply_adjustment(struct timekeeper *tk,
  * Adjust the timekeeper's multiplier to the correct frequency
  * and also to reduce the accumulated error value.
  */
+
+#include <linux/timekeeping_reference.h>
+
+
+int timekeeping_set_reference(const struct tk_reference *ref)
+{
+	struct timekeeper *tk = &tk_core.timekeeper;
+	u64 new_tl, delta, ref_frac;
+	s64 ref_err;
+	unsigned long flags;
+
+	raw_spin_lock_irqsave(&tk_core.lock, flags);
+
+	if (tk->cs_id != ref->cs_id) {
+		raw_spin_unlock_irqrestore(&tk_core.lock, flags);
+		return -ENODEV;
+	}
+
+	new_tl = mul_u64_u64_shr(ref->period_frac_sec,
+			(u64)tk->cycle_interval * NSEC_PER_SEC,
+			32 + ref->period_shift);
+	ntp_set_tick_length(tk->id, new_tl);
+
+	/* Compute phase offset at cycle_last and set time_offset to slew */
+	delta = tk->tkr_mono.cycle_last - ref->counter_value;
+	ref_frac = mul_u64_u64_shr(delta, ref->period_frac_sec,
+				   ref->period_shift) + ref->time_frac_sec;
+	ref_err = (s64)mul_u64_u64_shr(ref_frac,
+			(u64)NSEC_PER_SEC << tk->tkr_mono.shift, 64) -
+		  (s64)tk->tkr_mono.xtime_nsec;
+	ntp_set_time_offset(tk->id, ref_err >> tk->tkr_mono.shift);
+	tk->ntp_error = 0;
+
+	raw_spin_unlock_irqrestore(&tk_core.lock, flags);
+	return 0;
+}
+EXPORT_SYMBOL_GPL(timekeeping_set_reference);
+
 static void timekeeping_adjust(struct timekeeper *tk, s64 offset)
 {
 	u64 ntp_tl = ntp_tick_length(tk->id);
-- 
2.54.0


  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 ` [RFC PATCH v3 03/10] timekeeping: Account for monotonicity adjustment in ntp_error David Woodhouse
2026-05-20 23:27   ` 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 ` David Woodhouse [this message]
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-9-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®