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
Subject: [PATCH v4 6/7] ntp: Convert adjtime() to use time_offset instead of tick_length inflation
Date: Mon, 25 May 2026 14:54:38 +0100 [thread overview]
Message-ID: <20260525135904.126282-7-dwmw2@infradead.org> (raw)
In-Reply-To: <20260525135904.126282-1-dwmw2@infradead.org>
From: David Woodhouse <dwmw@amazon.co.uk>
The legacy adjtime() syscall (ADJ_ADJTIME/ADJ_OFFSET_SINGLESHOT) used
to slew the clock by inflating tick_length directly via time_adjust.
This was the last remaining user of tick_length != tick_length_base.
Convert it to fold time_adjust into time_offset each second (up to
MAX_TICKADJ per second, same rate limit as before). The existing
time_offset skew mechanism then delivers it via the per-tick ntp_error
transfer and mult adjustment.
Introduce ntp_set_time_offset() helper for setting time_offset from a
nanosecond value, and refactor ntp_update_offset() to use it. This
helper will also be used by the feed-forward reference clock API in a
subsequent commit.
This eliminates the last source of tick_length inflation, making
tick_length always equal tick_length_base.
Signed-off-by: David Woodhouse <dwmw@amazon.co.uk>
Assisted-by: Kiro:claude-opus-4.6-1m
---
kernel/time/ntp.c | 52 ++++++++++++++++++++++++--------------
kernel/time/ntp_internal.h | 1 +
2 files changed, 34 insertions(+), 19 deletions(-)
diff --git a/kernel/time/ntp.c b/kernel/time/ntp.c
index 3c1e287eb384..9470ac1f597b 100644
--- a/kernel/time/ntp.c
+++ b/kernel/time/ntp.c
@@ -329,7 +329,7 @@ static void ntp_update_offset(struct ntp_data *ntpdata, long offset)
ntpdata->time_freq = max(freq_adj, -MAXFREQ_SCALED);
- ntpdata->time_offset = div_s64(offset64 << NTP_SCALE_SHIFT, NTP_INTERVAL_FREQ);
+ ntp_set_time_offset(ntpdata - tk_ntp_data, offset64);
}
static void __ntp_clear(struct ntp_data *ntpdata)
@@ -390,6 +390,24 @@ s64 ntp_drain_time_offset(unsigned int tkid, s64 amount)
return 0;
}
+/**
+ * ntp_set_time_offset - Set the NTP time offset (phase correction)
+ * @tkid: Timekeeper ID
+ * @offset_ns: Desired offset in nanoseconds
+ *
+ * Converts nanoseconds to internal time_offset units and stores it.
+ * Also clears time_adjust since a new offset supersedes any pending
+ * adjtime() slew.
+ */
+void ntp_set_time_offset(unsigned int tkid, s64 offset_ns)
+{
+ struct ntp_data *ntpdata = &tk_ntp_data[tkid];
+
+ ntpdata->time_offset = div_s64((s64)offset_ns << NTP_SCALE_SHIFT,
+ NTP_INTERVAL_FREQ);
+ ntpdata->time_adjust = 0;
+}
+
/**
* ntp_get_next_leap - Returns the next leapsecond in CLOCK_REALTIME ktime_t
* @tkid: Timekeeper ID
@@ -498,26 +516,22 @@ int second_overflow(unsigned int tkid, time64_t secs)
/* Check PPS signal */
pps_dec_valid(ntpdata);
- if (!ntpdata->time_adjust)
- goto out;
-
- if (ntpdata->time_adjust > MAX_TICKADJ) {
- ntpdata->time_adjust -= MAX_TICKADJ;
- ntpdata->tick_length += MAX_TICKADJ_SCALED;
- goto out;
- }
-
- if (ntpdata->time_adjust < -MAX_TICKADJ) {
- ntpdata->time_adjust += MAX_TICKADJ;
- ntpdata->tick_length -= MAX_TICKADJ_SCALED;
- goto out;
+ /*
+ * Fold any pending time_adjust (from adjtime()) into time_offset.
+ * This used to inflate tick_length directly; now it uses the same
+ * per-tick skew mechanism as NTP's time_offset. Rate-limited to
+ * MAX_TICKADJ (500µs) per second.
+ */
+ if (ntpdata->time_adjust) {
+ long adj = clamp(ntpdata->time_adjust,
+ (long)-MAX_TICKADJ, (long)MAX_TICKADJ);
+
+ ntpdata->time_adjust -= adj;
+ ntpdata->time_offset += div_s64(
+ (s64)adj * NSEC_PER_USEC << NTP_SCALE_SHIFT,
+ NTP_INTERVAL_FREQ);
}
- ntpdata->tick_length += (s64)(ntpdata->time_adjust * NSEC_PER_USEC / NTP_INTERVAL_FREQ)
- << NTP_SCALE_SHIFT;
- ntpdata->time_adjust = 0;
-
-out:
return leap;
}
diff --git a/kernel/time/ntp_internal.h b/kernel/time/ntp_internal.h
index 05e5dd5e1b70..639860ff2baf 100644
--- a/kernel/time/ntp_internal.h
+++ b/kernel/time/ntp_internal.h
@@ -8,6 +8,7 @@ extern void ntp_clear(unsigned int tkid);
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 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,
--
2.54.0
next prev parent reply other threads:[~2026-05-25 13:59 UTC|newest]
Thread overview: 10+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-05-25 13:54 [PATCH v4 0/7] timekeeping/ntp: Fix drift tracking precision David Woodhouse
2026-05-25 13:54 ` [PATCH v4 1/7] MAINTAINERS: Add Miroslav as timekeeping reviewer David Woodhouse
2026-05-25 13:54 ` [PATCH v4 2/7] timekeeping: Remove xtime_remainder from ntp_error accumulation David Woodhouse
2026-05-29 14:41 ` David Woodhouse
2026-05-25 13:54 ` [PATCH v4 3/7] timekeeping: Account for monotonicity adjustment in ntp_error David Woodhouse
2026-05-25 13:54 ` [PATCH v4 4/7] timekeeping: Guard against divide-by-zero in timekeeping_adjust David Woodhouse
2026-05-25 13:54 ` [PATCH v4 5/7] timekeeping: Drive time_offset skew via per-tick ntp_error transfer David Woodhouse
2026-05-27 18:02 ` David Woodhouse
2026-05-25 13:54 ` David Woodhouse [this message]
2026-05-25 13:54 ` [PATCH v4 7/7] ntp: Remove tick_length_base, use tick_length directly 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=20260525135904.126282-7-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=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®