Re: [PATCH 2/3] vdso/vsyscall: Keep the CLOCK_AUX base scaled
From: Thomas Weißschuh
Date: Mon Aug 31 2026 - 09:29:42 EST
On Mon, Aug 31, 2026 at 08:55:56PM +0800, Zhan Xusheng wrote:
> The vDSO basetime of a clock is stored in the scaled nanoseconds of
> tkr_mono, so that the reader can floor the base and the cycle delta
> together in vdso_calc_ns().
>
> vdso_time_update_aux() instead shifts the base down to nanoseconds, adds
> the offset, and shifts it back up, which zeroes the fractional nanoseconds
> of xtime_nsec. The reader then floors the base and the delta separately:
>
> ktime_get_aux(): base + ((delta * mult + xtime_nsec) >> shift)
> vdso: base + (xtime_nsec >> shift)
> + ((delta * mult) >> shift)
>
> Since floor(a) + floor(b) <= floor(a + b), the vDSO reports 0 or 1 ns
> below the syscall for the same clock. clock_getres() reports 1 ns for
> auxiliary clocks, so that is the full advertised granularity. It is not a
> monotonicity problem: across an update the step is
> floor(a + d) - floor(a) - floor(d), which is 0 or 1, never negative.
>
> Add the offset in scaled nanoseconds as the other high resolution clocks
> do, and normalise with __iter_div64_u64_rem() so that the stored base
> stays below one second and the userspace fast-path does not iterate more
> in __iter_div_u64_rem().
>
> monotonic_to_aux.tv_nsec is a normalised timespec64 fraction, so it stays
> below NSEC_PER_SEC even for a negative offset, and the sum stays below
> 2 * (NSEC_PER_SEC << shift). The largest shift clocks_calc_mult_shift()
> can pick is 32, which makes that 8.6e18 against a u64 limit of 1.8e19.
>
> Signed-off-by: Zhan Xusheng <zhanxusheng@xxxxxxxxxx>
> ---
> kernel/time/vsyscall.c | 11 ++++++-----
> 1 file changed, 6 insertions(+), 5 deletions(-)
>
> diff --git a/kernel/time/vsyscall.c b/kernel/time/vsyscall.c
> index 165ad9d7f154..ab3ef1e7d3c8 100644
> --- a/kernel/time/vsyscall.c
> +++ b/kernel/time/vsyscall.c
> @@ -137,8 +137,8 @@ void vdso_time_update_aux(struct timekeeper *tk)
> struct vdso_time_data *vdata = vdso_k_time_data;
> struct vdso_timestamp *vdso_ts;
> struct vdso_clock *vc;
> + u64 nsec_per_sec, nsec;
> s32 clock_mode;
> - u64 nsec;
>
> vc = &vdata->aux_clock_data[tk->id - TIMEKEEPER_AUX_FIRST];
> vdso_ts = &vc->basetime[VDSO_BASE_AUX];
> @@ -156,10 +156,11 @@ void vdso_time_update_aux(struct timekeeper *tk)
>
> vdso_ts->sec = tk->xtime_sec + tk->monotonic_to_aux.tv_sec;
>
> - nsec = tk->tkr_mono.xtime_nsec >> tk->tkr_mono.shift;
> - nsec += tk->monotonic_to_aux.tv_nsec;
> - vdso_ts->sec += __iter_div_u64_rem(nsec, NSEC_PER_SEC, &nsec);
> - nsec = nsec << tk->tkr_mono.shift;
> + nsec_per_sec = (u64)NSEC_PER_SEC << tk->tkr_mono.shift;
Same as for the previous patch, the additional variable hurts in my opinion.
> +
> + nsec = tk->tkr_mono.xtime_nsec;
> + nsec += (u64)tk->monotonic_to_aux.tv_nsec << tk->tkr_mono.shift;
> + vdso_ts->sec += __iter_div64_u64_rem(nsec, nsec_per_sec, &nsec);
> vdso_ts->nsec = nsec;
> }
>
> --
> 2.43.0
>