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 AC5853E2ABF 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=1779285154; cv=none; b=VLeuqPNY5tNRSez2WenGRgg5tc2vYWXTfKDFs+Vi5BvfnGw3bhy9ihv6u7UjZ1zllXQGF+XmN0Us/vsc3U4BTwOXUhTecN6CbHfM2IF1BrVxolxysXrWyRj/9GBx+f5FG2xLAfrtNlqeMgTaCB01RQgnpJjEbdiBmkKBbUE2PCA= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1779285154; c=relaxed/simple; bh=elZZytGWoxchOMsQRajf9pPz2TD4QUN75F28hVqnwQY=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=GlaP4dvIyw1bmz7LOJnq2Df6iNiHoE3VJg41f+1zk+dtLzcz/pY7VBICfGCgGnkg4tXEJDwF5PcQ7gExpw1gATS7DQ2Ph0lgBOdcEGbHpg34Lxv8UWKWYj9zohb4dMnFdy0QfMBYGbTAXGhWJobk/2lhVxObRHynxgm4MXOi++g= 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=T0gaY4C6; 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="T0gaY4C6" DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=desiato.20200630; h=Sender:Content-Transfer-Encoding: MIME-Version:References:In-Reply-To:Message-ID:Date:Subject:Cc:To:From: Reply-To:Content-Type:Content-ID:Content-Description; bh=loyO5FKM9whCWGZYzcNDlJKIKuqvDbYyNAGZZlLdonU=; b=T0gaY4C6kd7gD8QISSPQvnJrDF YMULasnRURHSYKDRtoIrbLw/ZUF6DD7GehWAhuDBOAV0uEk1UGXJ4jbFyggOY0TY2v3oGFlgxDynS glHCK4whKRavjdZg0k0XzNV3uVe6aEyDzJV5/Lu+H80MoBF0Gq+5UP2sSWZ0fH3T7AoS0711dTYJV kx2oh9uv03bh0cj+j6SOk727Zcdogtq3jN/ZfYkv/f693K2g+TFEOP9M1D+HScwekNCmXvqCnre9n bcSj/JXiFWWCWc6YOeQ4c5SSoRs+vyKulc78ILvv6w7JXB7Fmk16UHcme9+jyDwGEDS7N/Tkql6E9 uXDbdY8A==; 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-0000000GzA2-3Zs7; 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-000000009tK-2243; 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 02/10] timekeeping: Remove xtime_remainder from ntp_error accumulation Date: Wed, 20 May 2026 14:33:41 +0100 Message-ID: <20260520135207.37826-3-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-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 ntp_error accumulator tracks the difference between the time actually reported to consumers in xtime, and the *intended* time. The former is subject to a sawtooth effect due to the quantisation of 'mult', which means that it actually advances by 'xtime_interval' each tick, while the intended clock advances by 'ntp_tick'. By dithering between adjacent integer values of 'mult' which result in an 'xtime_interval' slightly higher/lower than the intended tick length, the advancement of xtime is kept on average to the intended rate. The accounting should therefore adjust ntp_error by adding ntp_tick and subtracting xtime_interval on each tick. Since commit a386b5af8edd ("time: Compensate for rounding on odd-frequency clocksources") the value subtracted has been (xtime_interval + xtime_remainder), which is wrong. The effect is a systematic drift whose magnitude depends on the value of xtime_remainder and the NTP frequency correction. NTP masks this by continuously adjusting the frequency to compensate, but with a fixed frequency (or an external reference clock like vmclock), the drift is exposed. The value of xtime_remainder actually does represent the difference between the tick period and xtime_interval, so simply adding it instead of (+ tick length - xtime_remainder) might have made sense... except that it's only calculated once at boot time, so it's inaccurate anyway. So just kill it with fire. Also remove it from the mult computation in timekeeping_adjust(), which used it to offset the division for the same (incorrect) reason. Fixes: a386b5af8edd ("time: Compensate for rounding on odd-frequency clocksources") Signed-off-by: David Woodhouse Assisted-by: Kiro:claude-opus-4.6-1m --- include/linux/timekeeper_internal.h | 3 --- kernel/time/timekeeping.c | 8 +++----- 2 files changed, 3 insertions(+), 8 deletions(-) diff --git a/include/linux/timekeeper_internal.h b/include/linux/timekeeper_internal.h index e36d11e33e0c..da6cf383bedc 100644 --- a/include/linux/timekeeper_internal.h +++ b/include/linux/timekeeper_internal.h @@ -84,8 +84,6 @@ struct tk_read_base { * @cycle_interval: Number of clock cycles in one NTP interval * @xtime_interval: Number of clock shifted nano seconds in one NTP * interval. - * @xtime_remainder: Shifted nano seconds left over when rounding - * @cycle_interval * @raw_interval: Shifted raw nano seconds accumulated per NTP interval. * @next_leap_ktime: CLOCK_MONOTONIC time value of a pending leap-second * @ntp_tick: The ntp_tick_length() value currently being @@ -178,7 +176,6 @@ struct timekeeper { u64 cycle_interval; u64 xtime_interval; - s64 xtime_remainder; u64 raw_interval; ktime_t next_leap_ktime; diff --git a/kernel/time/timekeeping.c b/kernel/time/timekeeping.c index c493a4010305..b84b05f9d460 100644 --- a/kernel/time/timekeeping.c +++ b/kernel/time/timekeeping.c @@ -360,7 +360,6 @@ static void tk_setup_internals(struct timekeeper *tk, struct clocksource *clock) /* Go back from cycles -> shifted ns */ tk->xtime_interval = interval * clock->mult; - tk->xtime_remainder = ntpinterval - tk->xtime_interval; tk->raw_interval = interval * clock->mult; /* if changing clocks, convert xtime_nsec shift units */ @@ -2337,8 +2336,8 @@ static void timekeeping_adjust(struct timekeeper *tk, s64 offset) mult = tk->tkr_mono.mult - tk->ntp_err_mult; } else { tk->ntp_tick = ntp_tl; - mult = div64_u64((tk->ntp_tick >> tk->ntp_error_shift) - - tk->xtime_remainder, tk->cycle_interval); + mult = div64_u64(tk->ntp_tick >> tk->ntp_error_shift, + tk->cycle_interval); } /* @@ -2463,8 +2462,7 @@ static u64 logarithmic_accumulation(struct timekeeper *tk, u64 offset, /* Accumulate error between NTP and clock interval */ tk->ntp_error += tk->ntp_tick << shift; - tk->ntp_error -= (tk->xtime_interval + tk->xtime_remainder) << - (tk->ntp_error_shift + shift); + tk->ntp_error -= tk->xtime_interval << (tk->ntp_error_shift + shift); return offset; } -- 2.54.0