From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from desiato.infradead.org (desiato.infradead.org [90.155.92.199]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id AC6E23E2AD4 for ; Wed, 20 May 2026 13:52:32 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=90.155.92.199 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1779285156; cv=none; b=fDT2+nNrfxIrUDPkNhQ5btaUjQOjVqegkbInX89cZulNIRIIp2tNadj+FuWagzodX9bruUX/EinUkOGDBf/n91PCK7fE8GPjEmUlnq8LU6D6okHfB8G06xdhAwvpeJz7xC4xiLJ0+n/mQNkil7pWxKJeojQ8dgLUk1L+UccdN6g= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1779285156; c=relaxed/simple; bh=rJ7atr485VQoVPSyI3mvyEaIiBNtnrM2JzKw7P0XWnI=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=gyxkDYmCgDWWHJAYb9VorANZ7nTqiyPOmSo2vJvmVfkjeQrsdEGUGdG4F+Jd56sjAW+J+wLpBLY9soFOnrllZLNjYN4WI4uHypIE9cyX5l5Ght5/gU+mzoP1uPIRYF/1pOgi+BhsCRIdO7R0B5cd46sItmauYz4MxCYuEthetxE= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=infradead.org; spf=none smtp.mailfrom=desiato.srs.infradead.org; dkim=pass (2048-bit key) header.d=infradead.org header.i=@infradead.org header.b=qXtyiKpL; arc=none smtp.client-ip=90.155.92.199 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=infradead.org Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=desiato.srs.infradead.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=infradead.org header.i=@infradead.org header.b="qXtyiKpL" DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=desiato.20200630; h=Sender:Content-Transfer-Encoding: Content-Type:MIME-Version:References:In-Reply-To:Message-ID:Date:Subject:Cc: To:From:Reply-To:Content-ID:Content-Description; bh=xZJUJ4kXkweYJWr6wFjdyibEBFaktS6q92k69LmkIq4=; b=qXtyiKpLuUOFniOCzzmE18xFCS ghQyw1H931MbyWVIEJLMsNO/eBYAhagttSkXUXCHn1QuxQrF009022hvd619TIwTPM1AJ3GpFhNK5 37ed3rTJEQUVjyzDECA5pYkxN0kIkzEC9QgP7d2Eq60HtxXSyX4KK65QCmkeuaJ66TWcCEbmS5Ef2 5UG8yg5T8pWCkHfuP3MVWwt+DFsrpwylpxmhJnuJaZTeXmFe2ZcTIRfT17pmeJ++fe3LCI2BzP+9i TKMe2sijzBTL69FFSVVD3HRLd+eVKAJgynmXpQVP7KUhiTI7UWARM7eHPFafM4zWowX7P/6k0H57w QoFL63uA==; Received: from i7.infradead.org ([2001:8b0:10b:1:21e:67ff:fecb:7a92]) by desiato.infradead.org with esmtpsa (Exim 4.99.1 #2 (Red Hat Linux)) id 1wPhLF-0000000GzA0-3Xqu; Wed, 20 May 2026 13:52:10 +0000 Received: from dwoodhou by i7.infradead.org with local (Exim 4.99.2 #2 (Red Hat Linux)) id 1wPhLF-000000009tW-36qZ; Wed, 20 May 2026 14:52:09 +0100 From: David Woodhouse To: Richard Cochran , Wen Gu , David Woodhouse , Andrew Lunn , "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , John Stultz , Thomas Gleixner , Stephen Boyd , Anna-Maria Behnsen , Frederic Weisbecker , Shuah Khan , Peter Zijlstra , =?UTF-8?q?Thomas=20Wei=C3=9Fschuh?= , Arnd Bergmann , Miroslav Lichvar , Julien Ridoux , Ryan Luu , linux-kernel@vger.kernel.org Cc: David Woodhouse Subject: [RFC PATCH v3 06/10] ntp: Convert adjtime() to use time_offset instead of tick_length inflation Date: Wed, 20 May 2026 14:33:45 +0100 Message-ID: <20260520135207.37826-7-dwmw2@infradead.org> X-Mailer: git-send-email 2.54.0 In-Reply-To: <20260520135207.37826-1-dwmw2@infradead.org> References: <20260520135207.37826-1-dwmw2@infradead.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sender: David Woodhouse X-SRS-Rewrite: SMTP reverse-path rewritten from by desiato.infradead.org. See http://www.infradead.org/rpr.html From: David Woodhouse 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 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 87f3f5d0d13d..2b75653b456c 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