Skip to content

Commit 9d90b93

Browse files
committed
lib/vdso: Make delta calculation work correctly
The x86 vdso implementation on which the generic vdso library is based on has subtle (unfortunately undocumented) twists: 1) The code assumes that the clocksource mask is U64_MAX which means that no bits are masked. Which is true for any valid x86 VDSO clocksource. Stupidly it still did the mask operation for no reason and at the wrong place right after reading the clocksource. 2) It contains a sanity check to catch the case where slightly unsynchronized TSC values can be observed which would cause the delta calculation to make a huge jump. It therefore checks whether the current TSC value is larger than the value on which the current conversion is based on. If it's not larger the base value is used to prevent time jumps. raspberrypi#1 Is not only stupid for the X86 case because it does the masking for no reason it is also completely wrong for clocksources with a smaller mask which can legitimately wrap around during a conversion period. The core timekeeping code does it correct by applying the mask after the delta calculation: (now - base) & mask raspberrypi#2 is equally broken for clocksources which have smaller masks and can wrap around during a conversion period because there the now > base check is just wrong and causes stale time stamps and time going backwards issues. Unbreak it by: 1) Removing the mask operation from the clocksource read which makes the fallback detection work for all clocksources 2) Replacing the conditional delta calculation with a overrideable inline function. raspberrypi#2 could reuse clocksource_delta() from the timekeeping code but that results in a significant performance hit for the x86 VSDO. The timekeeping core code must have the non optimized version as it has to operate correctly with clocksources which have smaller masks as well to handle the case where TSC is discarded as timekeeper clocksource and replaced by HPET or pmtimer. For the VDSO there is no replacement clocksource. If TSC is unusable the syscall is enforced which does the right thing. To accommodate to the needs of various architectures provide an override-able inline function which defaults to the regular delta calculation with masking: (now - base) & mask Override it for x86 with the non-masking and checking version. This unbreaks the ARM64 syscall fallback operation, allows to use clocksources with arbitrary width and preserves the performance optimization for x86. Signed-off-by: Thomas Gleixner <tglx@linutronix.de> Reviewed-by: Vincenzo Frascino <vincenzo.frascino@arm.com> Cc: linux-arch@vger.kernel.org Cc: LAK <linux-arm-kernel@lists.infradead.org> Cc: linux-mips@vger.kernel.org Cc: linux-kselftest@vger.kernel.org Cc: catalin.marinas@arm.com Cc: Will Deacon <will.deacon@arm.com> Cc: Arnd Bergmann <arnd@arndb.de> Cc: linux@armlinux.org.uk Cc: Ralf Baechle <ralf@linux-mips.org> Cc: paul.burton@mips.com Cc: Daniel Lezcano <daniel.lezcano@linaro.org> Cc: salyzyn@android.com Cc: pcc@google.com Cc: shuah@kernel.org Cc: 0x7f454c46@gmail.com Cc: linux@rasmusvillemoes.dk Cc: huw@codeweavers.com Cc: sthotton@marvell.com Cc: andre.przywara@arm.com Cc: Andy Lutomirski <luto@kernel.org> Link: https://lkml.kernel.org/r/alpine.DEB.2.21.1906261159230.32342@nanos.tec.linutronix.de
1 parent e709803 commit 9d90b93

2 files changed

Lines changed: 42 additions & 4 deletions

File tree

‎arch/x86/include/asm/vdso/gettimeofday.h‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,33 @@ static __always_inline const struct vdso_data *__arch_get_vdso_data(void)
229229
return __vdso_data;
230230
}
231231

232+
/*
233+
* x86 specific delta calculation.
234+
*
235+
* The regular implementation assumes that clocksource reads are globally
236+
* monotonic. The TSC can be slightly off across sockets which can cause
237+
* the regular delta calculation (@cycles - @last) to return a huge time
238+
* jump.
239+
*
240+
* Therefore it needs to be verified that @cycles are greater than
241+
* @last. If not then use @last, which is the base time of the current
242+
* conversion period.
243+
*
244+
* This variant also removes the masking of the subtraction because the
245+
* clocksource mask of all VDSO capable clocksources on x86 is U64_MAX
246+
* which would result in a pointless operation. The compiler cannot
247+
* optimize it away as the mask comes from the vdso data and is not compile
248+
* time constant.
249+
*/
250+
static __always_inline
251+
u64 vdso_calc_delta(u64 cycles, u64 last, u64 mask, u32 mult)
252+
{
253+
if (cycles > last)
254+
return (cycles - last) * mult;
255+
return 0;
256+
}
257+
#define vdso_calc_delta vdso_calc_delta
258+
232259
#endif /* !__ASSEMBLY__ */
233260

234261
#endif /* __ASM_VDSO_GETTIMEOFDAY_H */

‎lib/vdso/gettimeofday.c‎

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,18 @@
2626
#include <asm/vdso/gettimeofday.h>
2727
#endif /* ENABLE_COMPAT_VDSO */
2828

29+
#ifndef vdso_calc_delta
30+
/*
31+
* Default implementation which works for all sane clocksources. That
32+
* obviously excludes x86/TSC.
33+
*/
34+
static __always_inline
35+
u64 vdso_calc_delta(u64 cycles, u64 last, u64 mask, u32 mult)
36+
{
37+
return ((cycles - last) & mask) * mult;
38+
}
39+
#endif
40+
2941
static int do_hres(const struct vdso_data *vd, clockid_t clk,
3042
struct __kernel_timespec *ts)
3143
{
@@ -35,14 +47,13 @@ static int do_hres(const struct vdso_data *vd, clockid_t clk,
3547

3648
do {
3749
seq = vdso_read_begin(vd);
38-
cycles = __arch_get_hw_counter(vd->clock_mode) &
39-
vd->mask;
50+
cycles = __arch_get_hw_counter(vd->clock_mode);
4051
ns = vdso_ts->nsec;
4152
last = vd->cycle_last;
4253
if (unlikely((s64)cycles < 0))
4354
return clock_gettime_fallback(clk, ts);
44-
if (cycles > last)
45-
ns += (cycles - last) * vd->mult;
55+
56+
ns += vdso_calc_delta(cycles, last, vd->mask, vd->mult);
4657
ns >>= vd->shift;
4758
sec = vdso_ts->sec;
4859
} while (unlikely(vdso_read_retry(vd, seq)));

0 commit comments

Comments
 (0)