Stealing Pinheads curtain was: Apply extrapolated ntp_error to clock snapshots

jamesb192 jamesb192 at jamesb192.com
Fri Nov 6 18:25:38 UTC 2026


I suspect there will soon be a post to https://lists.ntp.org/sympa/arc/hackers (https://lists.ntp.org/sympa/arc/hackers)

repost from #ntp link https://lore.kernel.org/all/20261106030856.2538039-7-dwmw2@infradead.org/ (https://lore.kernel.org/all/20261106030856.2538039-7-dwmw2@infradead.org/)
From: David Woodhouse <dwmw at amazon.co.uk>

The time reported in ::systime of a system_time_snapshot is known to be
slightly inaccurate because of the way that the reported realtime clock
sawtooths around the *intended* time series, limited by the integer mult
value used to calculate the inter-tick times, and designed to ensure
smoothness and monotonicity for its consumers.

It is particularly inaccurate in a tickless kernel, where ntp_err_mult
is not adjusted on each tick, allowing the reported clock to diverge
from the intended time for a large number of ticks before re-converging.

It would be better for callers of get_device_system_crosststamp() and
ktime_get_snapshot_id() to receive the *accurate* time, not the
sanitized version provided to gettimeofday().

Compute the deviation in snapshot_ntp_error() and add it to the returned
::systime so the snapshot lands on the ideal line. It sums four terms in
ns << NTP_SCALE_SHIFT before converting to signed ns:

  - tk->ntp_error, the deviation as of the last update;
  - (cycle_delta * ntp_err_frac), the fractional-mult drift accrued
    since then (cycle_delta is at most a tick on a tickful kernel, but
    many ticks' worth under NO_HZ);
  - (cycle_delta * ntp_err_mult), subtracting the applied mult bias (the
    +1 dither, or the err_drain bias while a drain is in progress) over
    the same span;
  - the sub-nanosecond fraction dropped when the read was truncated to
    whole ns (low shift bits, exact despite the multiply overflowing).

None of these terms can overflow: ntp_err_mult is clamped to 50ppm
(see "timekeeping: Reinstate proportional correction of ntp_error"),
and cycle_delta is bounded by the clocksource's max_cycles, so the
dither/drain term is at most 50ppm of the maximum idle period, in
ns << 32 — around 2^57 for a 500s TSC deferment. Deltas beyond
max_cycles (a marginal clocksource left unread too long) skip the
extrapolation entirely.

The helper uses the timekeeper selected for the requested clock id, so
all NTP-disciplined clocks are corrected, including the AUX clocks (each
has its own NTP instance); only CLOCK_MONOTONIC_RAW is undisciplined and
gets no correction. The residual is then a single clocksource cycle, the
same bound as a tickful kernel — except on the final tick of a phase
slew, where ntp_drain_skew() clamps to avoid overshooting zero and the
extrapolation can briefly overstate the correction by up to one tick's
worth of skew, repaid at the next update.

Note that this *unconditionally* changes the ::systime returned by all
snapshot and cross timestamp consumers (PTP SYS_OFFSET_PRECISE/EXTENDED,
and the timestamps which PPS_FETCH and /sys/class/pps report to
userspace, since the previous commit made pps_get_ts() use the snapshot
unconditionally): it is now the ideal NTP-disciplined time rather than
the raw accumulated clock.

For the PPS consumers that means ts_real no longer exactly matches what
clock_gettime(CLOCK_REALTIME) would have returned at the same moment,
but reports the time which the kernel is *intending* to steer
CLOCK_REALTIME to. The difference is the transient error which the
kernel is already committed to eliminating — reporting it to a PPS
consumer only feeds a disturbance into their measurements which will
have disappeared again by the time they react to it.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.ntpsec.org/pipermail/devel/attachments/20261106/071b4ff2/attachment.htm>


More information about the devel mailing list