x86/vdso: Cleanups, simmplifications and CLOCK_TAI support
Subsystems (5)
PATCH 1/11 clocksource: Provide clocksource_arch_init()
In reply to Thomas GleixnerArchitectures have extra archdata in the clocksource, e.g. for VDSO support. There are no sanity checks or general initializations for this available. Add support for that.
--- include/linux/clocksource.h | 5 +++++ kernel/time/Kconfig | 4 ++++ kernel/time/clocksource.c | 2 ++ 3 files changed, 11 insertions(+)
include/linux/clocksource.h +5 -0
@@ -241,6 +241,11 @@ static inline void __clocksource_update___clocksource_update_freq_scale (cs,1000 , khz); } +#ifdef CONFIG_ARCH_CLOCKSOURCE_INIT +extern void clocksource_arch_init (struct clocksource *cs); +#else +static inline void clocksource_arch_init (struct clocksource *cs) { } +#endif extern int timekeeping_notify (struct clocksource *clock);
kernel/time/Kconfig +4 -0
@@ -12,6 +12,10 @@ config CLOCKSOURCE_WATCHDOG config ARCH_CLOCKSOURCE_DATA bool +# Architecture has extra clocksource init called from registration +config ARCH_CLOCKSOURCE_INIT + bool + # Clocksources require validation of the clocksource against the last # cycle update - x86/TSC misfeature config CLOCKSOURCE_VALIDATE_LAST_CYCLE
kernel/time/clocksource.c +2 -0
@@ -937,6 +937,8 @@ int __clocksource_register_scale(struct {unsigned long flags; +clocksource_arch_init (cs); +/* Initialize mult/shift and max_idle_ns */ __clocksource_update_freq_scale (cs, scale, freq);
PATCH 2/11 x86/time: Implement clocksource_arch_init()
In reply to Thomas GleixnerRuntime validate the VCLOCK_MODE in clocksource::archdata and disable VCLOCK if invalid, which disables the VDSO but keeps the system running.
--- arch/x86/Kconfig | 1 + arch/x86/kernel/time.c | 16 ++++++++++++++++ 2 files changed, 17 insertions(+)
arch/x86/Kconfig +1 -0
@@ -48,6 +48,7 @@ config X86 select ACPI_SYSTEM_POWER_STATES_SUPPORT if ACPI select ANON_INODES select ARCH_CLOCKSOURCE_DATA + select ARCH_CLOCKSOURCE_INIT select ARCH_DISCARD_MEMBLOCK select ARCH_HAS_ACPI_TABLE_UPGRADE if ACPI select ARCH_HAS_DEBUG_VIRTUAL
arch/x86/kernel/time.c +16 -0
@@ -10,6 +10,7 @@* */ +#include <linux/clocksource.h> #include <linux/clockchips.h> #include <linux/interrupt.h> #include <linux/irq.h>
@@ -105,3 +106,18 @@ void __init time_init(void) { late_time_init = x86_late_time_init; } + +/* +* Sanity check the vdso related archdata content. +*/ +void clocksource_arch_init (struct clocksource *cs) +{ +if (cs->archdata.vclock_mode ==VCLOCK_NONE ) +return ; + +if (cs->archdata.vclock_mode >=VCLOCK_MAX ) { +pr_warn ("clocksource %s registered with invalid vclock_mode %d. Disabling vclock.\n" , + cs->name, cs->archdata.vclock_mode); + cs->archdata.vclock_mode =VCLOCK_NONE ; + } +}
PATCH 3/11 x86/vdso: Enforce 64bit clocksource
In reply to Thomas GleixnerAll VDSO clock sources are TSC based and use CLOCKSOURCE_MASK(64). There is no point in masking with all FF. Get rid of it and enforce the mask in the sanity checker.
--- arch/x86/entry/vdso/vclock_gettime.c | 2 +- arch/x86/kernel/time.c | 6 ++++++ 2 files changed, 7 insertions(+), 1 deletion(-)
arch/x86/entry/vdso/vclock_gettime.c +1 -1
@@ -199,7 +199,7 @@ notrace static inline u64 vgetsns(int *m#endif else return 0; - v = (cycles - gtod->cycle_last) & gtod->mask; + v = cycles - gtod->cycle_last;return v * gtod->mult; }
arch/x86/kernel/time.c +6 -0
@@ -120,4 +120,10 @@ void clocksource_arch_init(struct clocks cs->name, cs->archdata.vclock_mode); cs->archdata.vclock_mode =VCLOCK_NONE ; } + +if (cs->mask !=CLOCKSOURCE_MASK (64 )) { +pr_warn ("clocksource %s registered with invalid mask %016llx. Disabling vclock.\n" , + cs->name, cs->mask); + cs->archdata.vclock_mode =VCLOCK_NONE ; + } }
PATCH 4/11 x86/vdso: Use unsigned int consistently for vsyscall_gtod_data::seq
In reply to Thomas GleixnerThe sequence count in vgtod_data is unsigned int, but the call sites use unsigned long, which is a pointless exercise. Fix the call sites and replace 'unsigned' with unsinged 'int' while at it.
--- arch/x86/entry/vdso/vclock_gettime.c | 8 ++++---- arch/x86/include/asm/vgtod.h | 10 +++++----- 2 files changed, 9 insertions(+), 9 deletions(-)
arch/x86/entry/vdso/vclock_gettime.c +4 -4
@@ -206,7 +206,7 @@ notrace static inline u64 vgetsns(int *m/* Code size doesn't matter (vdso is 4k anyway) and this is faster. */ notrace static int __always_inline do_realtime(struct timespec *ts) { -unsigned long seq; +unsigned int seq;u64 ns;int mode;
@@ -227,7 +227,7 @@ notrace static int __always_inline do_re notrace static int __always_inline do_monotonic(struct timespec *ts) { -unsigned long seq; +unsigned int seq;u64 ns;int mode;
@@ -248,7 +248,7 @@ notrace static int __always_inline do_mo notrace static void do_realtime_coarse(struct timespec *ts) { -unsigned long seq; +unsigned int seq; do { seq = gtod_read_begin(gtod); ts->tv_sec = gtod->wall_time_coarse_sec;
@@ -258,7 +258,7 @@ notrace static void do_realtime_coarse(s notrace static void do_monotonic_coarse(struct timespec *ts) { -unsigned long seq; +unsigned int seq; do { seq = gtod_read_begin(gtod); ts->tv_sec = gtod->monotonic_time_coarse_sec;
arch/x86/include/asm/vgtod.h +5 -5
@@ -15,9 +15,9 @@ typedef unsigned long gtod_long_t;* so be carefull by modifying this structure. */ struct vsyscall_gtod_data { -unsigned seq; +unsigned int seq; -int vclock_mode; +int vclock_mode;u64 cycle_last;u64 mask;u32 mult;
@@ -44,9 +44,9 @@ static inline bool vclock_was_used(int vreturn READ_ONCE (vclocks_used) & (1 << vclock); } -static inline unsigned gtod_read_begin (const struct vsyscall_gtod_data *s) +static inline unsigned int gtod_read_begin (const struct vsyscall_gtod_data *s) { -unsigned ret; +unsigned int ret; repeat: ret =READ_ONCE (s->seq);
@@ -59,7 +59,7 @@ static inline unsigned gtod_read_begin(c }static inline int gtod_read_retry (const struct vsyscall_gtod_data *s, -unsigned start) +unsigned int start) {smp_rmb ();return unlikely (s->seq != start);
PATCH 5/11 x86/vdso: Introduce and use vgtod_ts
In reply to Thomas GleixnerIt's desired to support more clocks in the VDSO, e.g. CLOCK_TAI. This results either in indirect calls due to the larger switch case, which then requires retpolines or when the compiler is forced to avoid jump tables it results in even more conditionals.
To avoid both variants which are bad for performance the high resolution functions and the coarse grained functions will be collapsed into one for each. That requires to store the clock specific base time in an array.
Introcude struct vgtod_ts for storage and convert the data store, the update function and the individual clock functions over to use it.
The new storage does not longer use gtod_long_t for seconds depending on 32 or 64 bit compile because this needs to be the full 64bit value even for 32bit when a Y2038 function is added. No point in keeping the distinction alive in the internal representation.
--- arch/x86/entry/vdso/vclock_gettime.c | 24 +++++++++------ arch/x86/entry/vsyscall/vsyscall_gtod.c | 51 ++++++++++++++++---------------- arch/x86/include/asm/vgtod.h | 36 ++++++++++++---------- 3 files changed, 61 insertions(+), 50 deletions(-)
arch/x86/entry/vdso/vclock_gettime.c +15 -9
@@ -206,6 +206,7 @@ notrace static inline u64 vgetsns(int *m/* Code size doesn't matter (vdso is 4k anyway) and this is faster. */ notrace static int __always_inline do_realtime(struct timespec *ts) { +struct vgtod_ts *base = >od->basetime[CLOCK_REALTIME ];unsigned int seq;u64 ns;int mode;
@@ -213,8 +214,8 @@ notrace static int __always_inline do_redo { seq =gtod_read_begin (gtod); mode = gtod->vclock_mode; - ts->tv_sec = gtod->wall_time_sec; - ns = gtod->wall_time_snsec; + ts->tv_sec = base->sec; + ns = base->nsec; ns +=vgetsns (&mode); ns >>= gtod->shift; }while (unlikely (gtod_read_retry (gtod, seq)));
@@ -227,6 +228,7 @@ notrace static int __always_inline do_re notrace static int __always_inline do_monotonic(struct timespec *ts) { +struct vgtod_ts *base = >od->basetime[CLOCK_MONOTONIC ];unsigned int seq;u64 ns;int mode;
@@ -234,8 +236,8 @@ notrace static int __always_inline do_modo { seq =gtod_read_begin (gtod); mode = gtod->vclock_mode; - ts->tv_sec = gtod->monotonic_time_sec; - ns = gtod->monotonic_time_snsec; + ts->tv_sec = base->sec; + ns = base->nsec; ns +=vgetsns (&mode); ns >>= gtod->shift; }while (unlikely (gtod_read_retry (gtod, seq)));
@@ -248,21 +250,25 @@ notrace static int __always_inline do_mo notrace static void do_realtime_coarse(struct timespec *ts) { +struct vgtod_ts *base = >od->basetime[CLOCK_REALTIME_COARSE ];unsigned int seq; +do { seq =gtod_read_begin (gtod); - ts->tv_sec = gtod->wall_time_coarse_sec; - ts->tv_nsec = gtod->wall_time_coarse_nsec; + ts->tv_sec = base->sec; + ts->tv_nsec = base->nsec; }while (unlikely (gtod_read_retry (gtod, seq))); } notrace static void do_monotonic_coarse(struct timespec *ts) { +struct vgtod_ts *base = >od->basetime[CLOCK_MONOTONIC_COARSE ];unsigned int seq; +do { seq =gtod_read_begin (gtod); - ts->tv_sec = gtod->monotonic_time_coarse_sec; - ts->tv_nsec = gtod->monotonic_time_coarse_nsec; + ts->tv_sec = base->sec; + ts->tv_nsec = base->nsec; }while (unlikely (gtod_read_retry (gtod, seq))); }
@@ -318,7 +324,7 @@ int gettimeofday(struct timeval *, struc notrace time_t __vdso_time(time_t *t) {/* This is atomic on x86 so we don't need any locks. */ -time_t result =READ_ONCE (gtod->wall_time_sec); +time_t result =READ_ONCE (gtod->basetime[CLOCK_REALTIME ].sec);if (t) *t = result;
arch/x86/entry/vsyscall/vsyscall_gtod.c +26 -25
66 lines not on this page · raw message
arch/x86/include/asm/vgtod.h +20 -16
@@ -5,33 +5,37 @@#include <linux/compiler.h> #include <linux/clocksource.h> +#include <uapi/linux/time.h> +#ifdef BUILD_VDSO32_64 typedef u64 gtod_long_t ;#else typedef unsigned long gtod_long_t ;#endif + +struct vgtod_ts { +u64 sec; +u64 nsec; +}; + +#define VGTOD_BASES (CLOCK_MONOTONIC_COARSE + 1) +#define VGTOD_HRES (BIT(CLOCK_REALTIME) | BIT(CLOCK_MONOTONIC)) +#define VGTOD_COARSE (BIT(CLOCK_REALTIME_COARSE) | BIT(CLOCK_MONOTONIC_COARSE)) +/* * vsyscall_gtod_data will be accessed by 32 and 64 bit code at the same time * so be carefull by modifying this structure. */ struct vsyscall_gtod_data { -unsigned int seq; +unsigned int seq; + +int vclock_mode; +u64 cycle_last; +u64 mask; +u32 mult; +u32 shift; -int vclock_mode; -u64 cycle_last; -u64 mask; -u32 mult; -u32 shift; - -/* open coded 'struct timespec' */ -u64 wall_time_snsec; -gtod_long_t wall_time_sec; -gtod_long_t monotonic_time_sec; -u64 monotonic_time_snsec; -gtod_long_t wall_time_coarse_sec; -gtod_long_t wall_time_coarse_nsec; -gtod_long_t monotonic_time_coarse_sec; -gtod_long_t monotonic_time_coarse_nsec; +struct vgtod_ts basetime[VGTOD_BASES ];int tz_minuteswest;int tz_dsttime;
PATCH 6/11 x86/vdso: Collapse high resolution functions
In reply to Thomas Gleixnerdo_realtime() and do_monotonic() are now the same except for the storage array index. Hand the index in as an argument and collapse the functions.
--- arch/x86/entry/vdso/vclock_gettime.c | 35 +++++++---------------------------- 1 file changed, 7 insertions(+), 28 deletions(-)
arch/x86/entry/vdso/vclock_gettime.c +7 -28
@@ -203,35 +203,12 @@ notrace static inline u64 vgetsns(int *mreturn v * gtod->mult; } -/* Code size doesn't matter (vdso is 4k anyway) and this is faster. */ -notrace static int __always_inline do_realtime(struct timespec *ts) +notrace static int do_hres(clockid_t clk, struct timespec *ts) { -struct vgtod_ts *base = >od->basetime[CLOCK_REALTIME ]; +struct vgtod_ts *base = >od->basetime[clk];unsigned int seq; -u64 ns;int mode; - -do { - seq =gtod_read_begin (gtod); - mode = gtod->vclock_mode; - ts->tv_sec = base->sec; - ns = base->nsec; - ns +=vgetsns (&mode); - ns >>= gtod->shift; - }while (unlikely (gtod_read_retry (gtod, seq))); - - ts->tv_sec +=__iter_div_u64_rem (ns,NSEC_PER_SEC , &ns); - ts->tv_nsec = ns; - -return mode; -} - -notrace static int __always_inline do_monotonic(struct timespec *ts) -{ -struct vgtod_ts *base = >od->basetime[CLOCK_MONOTONIC ]; -unsigned int seq;u64 ns; -int mode; do { seq = gtod_read_begin(gtod);
@@ -276,11 +253,11 @@ notrace int __vdso_clock_gettime(clockid {switch (clock) {case CLOCK_REALTIME : -if (do_realtime (ts) ==VCLOCK_NONE ) +if (do_hres (CLOCK_REALTIME , ts) ==VCLOCK_NONE ) goto fallback;break ;case CLOCK_MONOTONIC : -if (do_monotonic (ts) ==VCLOCK_NONE ) +if (do_hres (CLOCK_MONOTONIC , ts) ==VCLOCK_NONE ) goto fallback;break ;case CLOCK_REALTIME_COARSE :
@@ -303,7 +280,9 @@ int clock_gettime(clockid_t, struct time notrace int __vdso_gettimeofday(struct timeval *tv, struct timezone *tz) {if (likely (tv !=NULL )) { -if (unlikely (do_realtime ((struct timespec *)tv) ==VCLOCK_NONE )) +struct timespec *ts = (struct timespec *) tv; + +if (unlikely (do_hres (CLOCK_REALTIME , ts) ==VCLOCK_NONE ))return vdso_fallback_gtod (tv, tz); tv->tv_usec /=1000 ; }
PATCH 7/11 x86/vdso: Collapse coarse functions
In reply to Thomas Gleixnerdo_realtime_coarse() and do_monotonic_coarse() are now the same except for the storage array index. Hand the index in as an argument and collapse the functions.
--- arch/x86/entry/vdso/vclock_gettime.c | 20 ++++---------------- 1 file changed, 4 insertions(+), 16 deletions(-)
arch/x86/entry/vdso/vclock_gettime.c +4 -16
@@ -225,21 +225,9 @@ notrace static int do_hres(clockid_t clkreturn mode; } -notrace static void do_realtime_coarse(struct timespec *ts) +notrace static void do_coarse(clockid_t clk, struct timespec *ts) { -struct vgtod_ts *base = >od->basetime[CLOCK_REALTIME_COARSE ]; -unsigned int seq; - -do { - seq =gtod_read_begin (gtod); - ts->tv_sec = base->sec; - ts->tv_nsec = base->nsec; - }while (unlikely (gtod_read_retry (gtod, seq))); -} - -notrace static void do_monotonic_coarse(struct timespec *ts) -{ -struct vgtod_ts *base = >od->basetime[CLOCK_MONOTONIC_COARSE ]; +struct vgtod_ts *base = >od->basetime[clk];unsigned int seq; do {
@@ -261,10 +249,10 @@ notrace int __vdso_clock_gettime(clockid goto fallback;break ;case CLOCK_REALTIME_COARSE : -do_realtime_coarse (ts); +do_coarse (CLOCK_REALTIME_COARSE , ts);break ;case CLOCK_MONOTONIC_COARSE : -do_monotonic_coarse (ts); +do_coarse (CLOCK_MONOTONIC_COARSE , ts);break ;default : goto fallback;
PATCH 8/11 x86/vdso: Replace the clockid switch case
In reply to Thomas GleixnerNow that the time getter functions use the clockid as index into the storage array for the base time access, the switch case can be replaced.
- Check for clockid >= MAX_CLOCKS and for negative clockid (CPU/FD) first and call the fallback function right away.
- After establishing that clockid is < MAX_CLOCKS, convert the clockid to a bitmask
- Check for the supported high resolution and coarse functions by anding the bitmask of supported clocks and check whether a bit is set.
This completely avoids jump tables, reduces the number of conditionals and makes the VDSO extensible for other clock ids.
--- arch/x86/entry/vdso/vclock_gettime.c | 38 ++++++++++++++++------------------- 1 file changed, 18 insertions(+), 20 deletions(-)
arch/x86/entry/vdso/vclock_gettime.c +18 -20
@@ -239,29 +239,27 @@ notrace static void do_coarse(clockid_t notrace int __vdso_clock_gettime(clockid_t clock, struct timespec *ts) { -switch (clock) { -case CLOCK_REALTIME : -if (do_hres (CLOCK_REALTIME , ts) ==VCLOCK_NONE ) - goto fallback; -break ; -case CLOCK_MONOTONIC : -if (do_hres (CLOCK_MONOTONIC , ts) ==VCLOCK_NONE ) - goto fallback; -break ; -case CLOCK_REALTIME_COARSE : -do_coarse (CLOCK_REALTIME_COARSE , ts); -break ; -case CLOCK_MONOTONIC_COARSE : -do_coarse (CLOCK_MONOTONIC_COARSE , ts); -break ; -default : - goto fallback; - } +unsigned int msk; + +/* Sort out negative (CPU/FD) and invalid clocks */ +if (unlikely ((unsigned int ) clock >=MAX_CLOCKS )) +return vdso_fallback_gettime (clock, ts); -return 0 ; -fallback: +/* +* Convert the clockid to a bitmask and use it to check which +* clocks are handled in the VDSO directly. +*/ + msk =1U << clock; +if (likely (msk &VGTOD_HRES )) { +if (do_hres (clock, ts) !=VCLOCK_NONE ) +return 0 ; + }else if (msk &VGTOD_COARSE ) { +do_coarse (clock, ts); +return 0 ; + }return vdso_fallback_gettime (clock, ts); } +int clock_gettime (clockid_t ,struct timespec *) __attribute__((weak,alias ("__vdso_clock_gettime" )));
PATCH 9/11 x86/vdso: Simplify the invalid vclock case
In reply to Thomas GleixnerThe code flow for the vclocks is convoluted as it requires the vclocks which can be invalidated separately from the vsyscall_gtod_data sequence to store the fact in a separate variable. That's inefficient.
Restructure the code so the vclock readout returns cycles and the conversion to nanoseconds is handled at the call site.
If the clock gets invalidated or vclock is already VCLOCK_NONE, return U64_MAX as the cycle value, which is invalid for all clocks and leave the sequence loop immediately in that case by calling the fallback function directly.
This allows to remove the gettimeofday fallback as it now uses the clock_gettime() fallback and does the nanoseconds to microseconds conversion in the same way as it does when the vclock is functional. It does not make a difference whether the division by 1000 happens in the kernel fallback or in userspace.
Generates way better code and gains a few cycles back.
--- arch/x86/entry/vdso/vclock_gettime.c | 81 +++++++++-------------------------- 1 file changed, 21 insertions(+), 60 deletions(-)
arch/x86/entry/vdso/vclock_gettime.c +21 -60
@@ -48,16 +48,6 @@ notrace static long vdso_fallback_gettimreturn ret; } -notrace static long vdso_fallback_gtod(struct timeval *tv, struct timezone *tz) -{ -long ret; - - asm("syscall" :"=a" (ret) : -"0" (__NR_gettimeofday),"D" (tv),"S" (tz) :"memory" ); -return ret; -} - -#else notrace static long vdso_fallback_gettime(long clock, struct timespec *ts)
@@ -75,21 +65,6 @@ notrace static long vdso_fallback_gettim return ret; } -notrace static long vdso_fallback_gtod(struct timeval *tv, struct timezone *tz) -{ -long ret; - - asm( -"mov %%ebx, %%edx \n" -"mov %2, %%ebx \n" -"call __kernel_vsyscall \n" -"mov %%edx, %%ebx \n" - :"=a" (ret) - :"0" (__NR_gettimeofday),"g" (tv),"c" (tz) - :"memory" ,"edx" ); -return ret; -} -#endif #ifdef CONFIG_PARAVIRT_CLOCK
@@ -98,7 +73,7 @@ static notrace const struct pvclock_vsysreturn (const struct pvclock_vsyscall_time_info *)&pvclock_page; } -static notrace u64 vread_pvclock(int *mode) +static notrace u64 vread_pvclock(void) {const struct pvclock_vcpu_time_info *pvti = &get_pvti0 ()->pvti;u64 ret;
@@ -130,10 +105,8 @@ static notrace u64 vread_pvclock(int *modo { version =pvclock_read_begin (pvti); -if (unlikely (!(pvti->flags &PVCLOCK_TSC_STABLE_BIT ))) { - *mode =VCLOCK_NONE ; -return 0 ; - } +if (unlikely (!(pvti->flags &PVCLOCK_TSC_STABLE_BIT ))) +return U64_MAX ; ret =__pvclock_read_cycles (pvti,rdtsc_ordered ()); }while (pvclock_read_retry (pvti, version));
@@ -148,17 +121,12 @@ static notrace u64 vread_pvclock(int *mo }#endif #ifdef CONFIG_HYPERV_TSCPAGE -static notrace u64 vread_hvclock(int *mode) +static notrace u64 vread_hvclock(void) {const struct ms_hyperv_tsc_page *tsc_pg = (const struct ms_hyperv_tsc_page *)&hvclock_page; -u64 current_tick =hv_read_tsc_page (tsc_pg); - -if (current_tick !=U64_MAX ) -return current_tick; - *mode =VCLOCK_NONE ; -return 0 ; +return hv_read_tsc_page (tsc_pg); }#endif
@@ -182,47 +150,42 @@ notrace static u64 vread_tsc(void)return last; } -notrace static inline u64 vgetsns(int *mode) +notrace static inline u64 vgetcyc(int mode) { -u64 v; -cycles_t cycles; - -if (gtod->vclock_mode ==VCLOCK_TSC ) - cycles =vread_tsc (); +if (mode ==VCLOCK_TSC ) +return vread_tsc ();#ifdef CONFIG_PARAVIRT_CLOCK - else if (gtod->vclock_mode == VCLOCK_PVCLOCK) - cycles =vread_pvclock (mode); + else if (mode == VCLOCK_PVCLOCK) + return vread_pvclock();#endif #ifdef CONFIG_HYPERV_TSCPAGE - else if (gtod->vclock_mode == VCLOCK_HVCLOCK) - cycles =vread_hvclock (mode); + else if (mode == VCLOCK_HVCLOCK) + return vread_hvclock();#endif - else - return 0; - v = cycles - gtod->cycle_last; -return v * gtod->mult; +return U64_MAX ; } notrace static int do_hres(clockid_t clk, struct timespec *ts) {struct vgtod_ts *base = >od->basetime[clk];unsigned int seq; -int mode; -u64 ns; +u64 cycles, ns;do { seq =gtod_read_begin (gtod); - mode = gtod->vclock_mode; ts->tv_sec = base->sec; ns = base->nsec; - ns +=vgetsns (&mode); + cycles =vgetcyc (gtod->vclock_mode); +if (unlikely ((s64 )cycles <0 )) +return vdso_fallback_gettime (clk, ts); + ns += (cycles - gtod->cycle_last) * gtod->mult; ns >>= gtod->shift; }while (unlikely (gtod_read_retry (gtod, seq))); ts->tv_sec +=__iter_div_u64_rem (ns,NSEC_PER_SEC , &ns); ts->tv_nsec = ns; -return mode; +return 0 ; } notrace static void do_coarse(clockid_t clk, struct timespec *ts)
@@ -251,8 +214,7 @@ notrace int __vdso_clock_gettime(clockid*/ msk =1U << clock;if (likely (msk &VGTOD_HRES )) { -if (do_hres (clock, ts) !=VCLOCK_NONE ) -return 0 ; +return do_hres (clock, ts); }else if (msk &VGTOD_COARSE ) {do_coarse (clock, ts);return 0 ;
@@ -268,8 +230,7 @@ notrace int __vdso_gettimeofday(struct tif (likely (tv !=NULL )) {struct timespec *ts = (struct timespec *) tv; -if (unlikely (do_hres (CLOCK_REALTIME , ts) ==VCLOCK_NONE )) -return vdso_fallback_gtod (tv, tz); +do_hres (CLOCK_REALTIME , ts); tv->tv_usec /=1000 ; }if (unlikely (tz !=NULL )) {
PATCH 10/11 x86/vdso: Move cycle_last handling into the caller
In reply to Thomas GleixnerDereferencing gtod->cycle_last all over the place and foing the cycles < last comparison in the vclock read functions generates horrible code. Doing it at the call site is much better and gains a few cycles both for TSC and pvclock.
Caveat: This adds the comparison to the hyperv vclock as well, but I have no way to test that.
--- arch/x86/entry/vdso/vclock_gettime.c | 39 ++++++----------------------------- 1 file changed, 7 insertions(+), 32 deletions(-)
arch/x86/entry/vdso/vclock_gettime.c +7 -32
@@ -76,9 +76,8 @@ static notrace const struct pvclock_vsys static notrace u64 vread_pvclock(void) {const struct pvclock_vcpu_time_info *pvti = &get_pvti0 ()->pvti; -u64 ret; -u64 last;u32 version; +u64 ret;/* * Note: The kernel and hypervisor must guarantee that cpu ID
@@ -111,13 +110,7 @@ static notrace u64 vread_pvclock(void) ret = __pvclock_read_cycles(pvti, rdtsc_ordered()); }while (pvclock_read_retry (pvti, version)); -/* refer to vread_tsc() comment for rationale */ - last = gtod->cycle_last; - -if (likely (ret >= last)) -return ret; - -return last; +return ret; }#endif #ifdef CONFIG_HYPERV_TSCPAGE
@@ -130,30 +123,10 @@ static notrace u64 vread_hvclock(void) }#endif -notrace static u64 vread_tsc(void) -{ -u64 ret = (u64 )rdtsc_ordered (); -u64 last = gtod->cycle_last; - -if (likely (ret >= last)) -return ret; - -/* -* GCC likes to generate cmov here, but this branch is extremely -* predictable (it's just a function of time and the likely is -* very likely) and there's a data dependence, so force GCC -* to generate a branch instead. I don't barrier() because -* we don't actually need a barrier, and if this function -* ever gets inlined it will generate worse code. -*/ - asmvolatile ("" ); -return last; -} - notrace static inline u64 vgetcyc(int mode) {if (mode ==VCLOCK_TSC ) -return vread_tsc (); +return (u64 )rdtsc_ordered ();#ifdef CONFIG_PARAVIRT_CLOCK else if (mode == VCLOCK_PVCLOCK) return vread_pvclock();
@@ -168,17 +141,19 @@ notrace static inline u64 vgetcyc(int mo notrace static int do_hres(clockid_t clk, struct timespec *ts) {struct vgtod_ts *base = >od->basetime[clk]; +u64 cycles, last, ns;unsigned int seq; -u64 cycles, ns;do { seq =gtod_read_begin (gtod); ts->tv_sec = base->sec; ns = base->nsec; + last = gtod->cycle_last; cycles =vgetcyc (gtod->vclock_mode);if (unlikely ((s64 )cycles <0 ))return vdso_fallback_gettime (clk, ts); - ns += (cycles - gtod->cycle_last) * gtod->mult; +if (cycles > last) + ns += (cycles - last) * gtod->mult; ns >>= gtod->shift; }while (unlikely (gtod_read_retry (gtod, seq)));
PATCH 11/11 x66/vdso: Add CLOCK_TAI support
In reply to Thomas GleixnerWith the storage array in place it's now trivial to support CLOCK_TAI in the vdso. Instead of extending the array to accomodate CLOCK_TAI, make use of the fact that:
- CLOCK ids are set in stone
- CLOCK_THREAD_CPUTIME is never going to be supported in the VDSO so the array slot 3 is unused
- CLOCK_TAI is id 11 which results in 3 when masked with 0x3
Add the mask to the basetime array lookup and set up the CLOCK_TAI base time in update_vsyscall().
The performance impact of the mask operation is within the noise.
--- arch/x86/entry/vdso/vclock_gettime.c | 2 +- arch/x86/entry/vsyscall/vsyscall_gtod.c | 4 ++++ arch/x86/include/asm/vgtod.h | 6 +++++- 3 files changed, 10 insertions(+), 2 deletions(-)
arch/x86/entry/vdso/vclock_gettime.c +1 -1
@@ -140,7 +140,7 @@ notrace static inline u64 vgetcyc(int mo notrace static int do_hres(clockid_t clk, struct timespec *ts) { -struct vgtod_ts *base = >od->basetime[clk]; +struct vgtod_ts *base = >od->basetime[clk &VGTOD_HRES_MASK ];u64 cycles, last, ns;unsigned int seq;
arch/x86/entry/vsyscall/vsyscall_gtod.c +4 -0
@@ -51,6 +51,10 @@ void update_vsyscall(struct timekeeper * base->sec = tk->xtime_sec; base->nsec = tk->tkr_mono.xtime_nsec; + base = &vdata->basetime[VGTOD_TAI ]; + base->sec = tk->xtime_sec + (s64 )tk->tai_offset; + base->nsec = tk->tkr_mono.xtime_nsec; + base = &vdata->basetime[CLOCK_MONOTONIC ]; base->sec = tk->xtime_sec + tk->wall_to_monotonic.tv_sec; nsec = tk->tkr_mono.xtime_nsec;
arch/x86/include/asm/vgtod.h +5 -1
@@ -19,9 +19,13 @@ struct vgtod_ts { };#define VGTOD_BASES (CLOCK_MONOTONIC_COARSE + 1) -#define VGTOD_HRES (BIT(CLOCK_REALTIME) | BIT(CLOCK_MONOTONIC)) +#define VGTOD_HRES (BIT(CLOCK_REALTIME) | BIT(CLOCK_MONOTONIC) | BIT(CLOCK_TAI))#define VGTOD_COARSE (BIT(CLOCK_REALTIME_COARSE) | BIT(CLOCK_MONOTONIC_COARSE)) +/* Abuse CLOCK_THREAD_CPUTIME_ID for VGTOD CLOCK TAI */ +#define VGTOD_HRES_MASK 0x3 +#define VGTOD_TAI (CLOCK_TAI & VGTOD_HRES_MASK) +/* * vsyscall_gtod_data will be accessed by 32 and 64 bit code at the same time * so be carefull by modifying this structure.
PATCH 2/11 x86/time: Implement clocksource_arch_init()
In reply to Thomas GleixnerRuntime validate the VCLOCK_MODE in clocksource::archdata and disable VCLOCK if invalid, which disables the VDSO but keeps the system running.
--- arch/x86/Kconfig | 1 + arch/x86/kernel/time.c | 16 ++++++++++++++++ 2 files changed, 17 insertions(+)
arch/x86/Kconfig +1 -0
@@ -48,6 +48,7 @@ config X86 select ACPI_SYSTEM_POWER_STATES_SUPPORT if ACPI select ANON_INODES select ARCH_CLOCKSOURCE_DATA + select ARCH_CLOCKSOURCE_INIT select ARCH_DISCARD_MEMBLOCK select ARCH_HAS_ACPI_TABLE_UPGRADE if ACPI select ARCH_HAS_DEBUG_VIRTUAL
arch/x86/kernel/time.c +16 -0
@@ -10,6 +10,7 @@* */ +#include <linux/clocksource.h> #include <linux/clockchips.h> #include <linux/interrupt.h> #include <linux/irq.h>
@@ -105,3 +106,18 @@ void __init time_init(void) { late_time_init = x86_late_time_init; } + +/* +* Sanity check the vdso related archdata content. +*/ +void clocksource_arch_init (struct clocksource *cs) +{ +if (cs->archdata.vclock_mode ==VCLOCK_NONE ) +return ; + +if (cs->archdata.vclock_mode >=VCLOCK_MAX ) { +pr_warn ("clocksource %s registered with invalid vclock_mode %d. Disabling vclock.\n" , + cs->name, cs->archdata.vclock_mode); + cs->archdata.vclock_mode =VCLOCK_NONE ; + } +}
PATCH 3/11 x86/vdso: Enforce 64bit clocksource
In reply to Thomas GleixnerAll VDSO clock sources are TSC based and use CLOCKSOURCE_MASK(64). There is no point in masking with all FF. Get rid of it and enforce the mask in the sanity checker.
--- arch/x86/entry/vdso/vclock_gettime.c | 2 +- arch/x86/kernel/time.c | 6 ++++++ 2 files changed, 7 insertions(+), 1 deletion(-)
arch/x86/entry/vdso/vclock_gettime.c +1 -1
@@ -199,7 +199,7 @@ notrace static inline u64 vgetsns(int *m#endif else return 0; - v = (cycles - gtod->cycle_last) & gtod->mask; + v = cycles - gtod->cycle_last;return v * gtod->mult; }
arch/x86/kernel/time.c +6 -0
@@ -120,4 +120,10 @@ void clocksource_arch_init(struct clocks cs->name, cs->archdata.vclock_mode); cs->archdata.vclock_mode =VCLOCK_NONE ; } + +if (cs->mask !=CLOCKSOURCE_MASK (64 )) { +pr_warn ("clocksource %s registered with invalid mask %016llx. Disabling vclock.\n" , + cs->name, cs->mask); + cs->archdata.vclock_mode =VCLOCK_NONE ; + } }
PATCH 1/11 clocksource: Provide clocksource_arch_init()
In reply to Thomas GleixnerArchitectures have extra archdata in the clocksource, e.g. for VDSO support. There are no sanity checks or general initializations for this available. Add support for that.
--- include/linux/clocksource.h | 5 +++++ kernel/time/Kconfig | 4 ++++ kernel/time/clocksource.c | 2 ++ 3 files changed, 11 insertions(+)
include/linux/clocksource.h +5 -0
@@ -241,6 +241,11 @@ static inline void __clocksource_update___clocksource_update_freq_scale (cs,1000 , khz); } +#ifdef CONFIG_ARCH_CLOCKSOURCE_INIT +extern void clocksource_arch_init (struct clocksource *cs); +#else +static inline void clocksource_arch_init (struct clocksource *cs) { } +#endif extern int timekeeping_notify (struct clocksource *clock);
kernel/time/Kconfig +4 -0
@@ -12,6 +12,10 @@ config CLOCKSOURCE_WATCHDOG config ARCH_CLOCKSOURCE_DATA bool +# Architecture has extra clocksource init called from registration +config ARCH_CLOCKSOURCE_INIT + bool + # Clocksources require validation of the clocksource against the last # cycle update - x86/TSC misfeature config CLOCKSOURCE_VALIDATE_LAST_CYCLE
kernel/time/clocksource.c +2 -0
@@ -937,6 +937,8 @@ int __clocksource_register_scale(struct {unsigned long flags; +clocksource_arch_init (cs); +/* Initialize mult/shift and max_idle_ns */ __clocksource_update_freq_scale (cs, scale, freq);
PATCH 8/11 x86/vdso: Replace the clockid switch case
In reply to Thomas GleixnerNow that the time getter functions use the clockid as index into the storage array for the base time access, the switch case can be replaced.
- Check for clockid >= MAX_CLOCKS and for negative clockid (CPU/FD) first and call the fallback function right away.
- After establishing that clockid is < MAX_CLOCKS, convert the clockid to a bitmask
- Check for the supported high resolution and coarse functions by anding the bitmask of supported clocks and check whether a bit is set.
This completely avoids jump tables, reduces the number of conditionals and makes the VDSO extensible for other clock ids.
--- arch/x86/entry/vdso/vclock_gettime.c | 38 ++++++++++++++++------------------- 1 file changed, 18 insertions(+), 20 deletions(-)
arch/x86/entry/vdso/vclock_gettime.c +18 -20
@@ -239,29 +239,27 @@ notrace static void do_coarse(clockid_t notrace int __vdso_clock_gettime(clockid_t clock, struct timespec *ts) { -switch (clock) { -case CLOCK_REALTIME : -if (do_hres (CLOCK_REALTIME , ts) ==VCLOCK_NONE ) - goto fallback; -break ; -case CLOCK_MONOTONIC : -if (do_hres (CLOCK_MONOTONIC , ts) ==VCLOCK_NONE ) - goto fallback; -break ; -case CLOCK_REALTIME_COARSE : -do_coarse (CLOCK_REALTIME_COARSE , ts); -break ; -case CLOCK_MONOTONIC_COARSE : -do_coarse (CLOCK_MONOTONIC_COARSE , ts); -break ; -default : - goto fallback; - } +unsigned int msk; + +/* Sort out negative (CPU/FD) and invalid clocks */ +if (unlikely ((unsigned int ) clock >=MAX_CLOCKS )) +return vdso_fallback_gettime (clock, ts); -return 0 ; -fallback: +/* +* Convert the clockid to a bitmask and use it to check which +* clocks are handled in the VDSO directly. +*/ + msk =1U << clock; +if (likely (msk &VGTOD_HRES )) { +if (do_hres (clock, ts) !=VCLOCK_NONE ) +return 0 ; + }else if (msk &VGTOD_COARSE ) { +do_coarse (clock, ts); +return 0 ; + }return vdso_fallback_gettime (clock, ts); } +int clock_gettime (clockid_t ,struct timespec *) __attribute__((weak,alias ("__vdso_clock_gettime" )));
PATCH 9/11 x86/vdso: Simplify the invalid vclock case
In reply to Thomas GleixnerThe code flow for the vclocks is convoluted as it requires the vclocks which can be invalidated separately from the vsyscall_gtod_data sequence to store the fact in a separate variable. That's inefficient.
Restructure the code so the vclock readout returns cycles and the conversion to nanoseconds is handled at the call site.
If the clock gets invalidated or vclock is already VCLOCK_NONE, return U64_MAX as the cycle value, which is invalid for all clocks and leave the sequence loop immediately in that case by calling the fallback function directly.
This allows to remove the gettimeofday fallback as it now uses the clock_gettime() fallback and does the nanoseconds to microseconds conversion in the same way as it does when the vclock is functional. It does not make a difference whether the division by 1000 happens in the kernel fallback or in userspace.
Generates way better code and gains a few cycles back.
--- arch/x86/entry/vdso/vclock_gettime.c | 81 +++++++++-------------------------- 1 file changed, 21 insertions(+), 60 deletions(-)
arch/x86/entry/vdso/vclock_gettime.c +21 -60
@@ -48,16 +48,6 @@ notrace static long vdso_fallback_gettimreturn ret; } -notrace static long vdso_fallback_gtod(struct timeval *tv, struct timezone *tz) -{ -long ret; - - asm("syscall" :"=a" (ret) : -"0" (__NR_gettimeofday),"D" (tv),"S" (tz) :"memory" ); -return ret; -} - -#else notrace static long vdso_fallback_gettime(long clock, struct timespec *ts)
@@ -75,21 +65,6 @@ notrace static long vdso_fallback_gettim return ret; } -notrace static long vdso_fallback_gtod(struct timeval *tv, struct timezone *tz) -{ -long ret; - - asm( -"mov %%ebx, %%edx \n" -"mov %2, %%ebx \n" -"call __kernel_vsyscall \n" -"mov %%edx, %%ebx \n" - :"=a" (ret) - :"0" (__NR_gettimeofday),"g" (tv),"c" (tz) - :"memory" ,"edx" ); -return ret; -} -#endif #ifdef CONFIG_PARAVIRT_CLOCK
@@ -98,7 +73,7 @@ static notrace const struct pvclock_vsysreturn (const struct pvclock_vsyscall_time_info *)&pvclock_page; } -static notrace u64 vread_pvclock(int *mode) +static notrace u64 vread_pvclock(void) {const struct pvclock_vcpu_time_info *pvti = &get_pvti0 ()->pvti;u64 ret;
@@ -130,10 +105,8 @@ static notrace u64 vread_pvclock(int *modo { version =pvclock_read_begin (pvti); -if (unlikely (!(pvti->flags &PVCLOCK_TSC_STABLE_BIT ))) { - *mode =VCLOCK_NONE ; -return 0 ; - } +if (unlikely (!(pvti->flags &PVCLOCK_TSC_STABLE_BIT ))) +return U64_MAX ; ret =__pvclock_read_cycles (pvti,rdtsc_ordered ()); }while (pvclock_read_retry (pvti, version));
@@ -148,17 +121,12 @@ static notrace u64 vread_pvclock(int *mo }#endif #ifdef CONFIG_HYPERV_TSCPAGE -static notrace u64 vread_hvclock(int *mode) +static notrace u64 vread_hvclock(void) {const struct ms_hyperv_tsc_page *tsc_pg = (const struct ms_hyperv_tsc_page *)&hvclock_page; -u64 current_tick =hv_read_tsc_page (tsc_pg); - -if (current_tick !=U64_MAX ) -return current_tick; - *mode =VCLOCK_NONE ; -return 0 ; +return hv_read_tsc_page (tsc_pg); }#endif
@@ -182,47 +150,42 @@ notrace static u64 vread_tsc(void)return last; } -notrace static inline u64 vgetsns(int *mode) +notrace static inline u64 vgetcyc(int mode) { -u64 v; -cycles_t cycles; - -if (gtod->vclock_mode ==VCLOCK_TSC ) - cycles =vread_tsc (); +if (mode ==VCLOCK_TSC ) +return vread_tsc ();#ifdef CONFIG_PARAVIRT_CLOCK - else if (gtod->vclock_mode == VCLOCK_PVCLOCK) - cycles =vread_pvclock (mode); + else if (mode == VCLOCK_PVCLOCK) + return vread_pvclock();#endif #ifdef CONFIG_HYPERV_TSCPAGE - else if (gtod->vclock_mode == VCLOCK_HVCLOCK) - cycles =vread_hvclock (mode); + else if (mode == VCLOCK_HVCLOCK) + return vread_hvclock();#endif - else - return 0; - v = cycles - gtod->cycle_last; -return v * gtod->mult; +return U64_MAX ; } notrace static int do_hres(clockid_t clk, struct timespec *ts) {struct vgtod_ts *base = >od->basetime[clk];unsigned int seq; -int mode; -u64 ns; +u64 cycles, ns;do { seq =gtod_read_begin (gtod); - mode = gtod->vclock_mode; ts->tv_sec = base->sec; ns = base->nsec; - ns +=vgetsns (&mode); + cycles =vgetcyc (gtod->vclock_mode); +if (unlikely ((s64 )cycles <0 )) +return vdso_fallback_gettime (clk, ts); + ns += (cycles - gtod->cycle_last) * gtod->mult; ns >>= gtod->shift; }while (unlikely (gtod_read_retry (gtod, seq))); ts->tv_sec +=__iter_div_u64_rem (ns,NSEC_PER_SEC , &ns); ts->tv_nsec = ns; -return mode; +return 0 ; } notrace static void do_coarse(clockid_t clk, struct timespec *ts)
@@ -251,8 +214,7 @@ notrace int __vdso_clock_gettime(clockid*/ msk =1U << clock;if (likely (msk &VGTOD_HRES )) { -if (do_hres (clock, ts) !=VCLOCK_NONE ) -return 0 ; +return do_hres (clock, ts); }else if (msk &VGTOD_COARSE ) {do_coarse (clock, ts);return 0 ;
@@ -268,8 +230,7 @@ notrace int __vdso_gettimeofday(struct tif (likely (tv !=NULL )) {struct timespec *ts = (struct timespec *) tv; -if (unlikely (do_hres (CLOCK_REALTIME , ts) ==VCLOCK_NONE )) -return vdso_fallback_gtod (tv, tz); +do_hres (CLOCK_REALTIME , ts); tv->tv_usec /=1000 ; }if (unlikely (tz !=NULL )) {
PATCH 10/11 x86/vdso: Move cycle_last handling into the caller
In reply to Thomas GleixnerDereferencing gtod->cycle_last all over the place and foing the cycles < last comparison in the vclock read functions generates horrible code. Doing it at the call site is much better and gains a few cycles both for TSC and pvclock.
Caveat: This adds the comparison to the hyperv vclock as well, but I have no way to test that.
--- arch/x86/entry/vdso/vclock_gettime.c | 39 ++++++----------------------------- 1 file changed, 7 insertions(+), 32 deletions(-)
arch/x86/entry/vdso/vclock_gettime.c +7 -32
@@ -76,9 +76,8 @@ static notrace const struct pvclock_vsys static notrace u64 vread_pvclock(void) {const struct pvclock_vcpu_time_info *pvti = &get_pvti0 ()->pvti; -u64 ret; -u64 last;u32 version; +u64 ret;/* * Note: The kernel and hypervisor must guarantee that cpu ID
@@ -111,13 +110,7 @@ static notrace u64 vread_pvclock(void) ret = __pvclock_read_cycles(pvti, rdtsc_ordered()); }while (pvclock_read_retry (pvti, version)); -/* refer to vread_tsc() comment for rationale */ - last = gtod->cycle_last; - -if (likely (ret >= last)) -return ret; - -return last; +return ret; }#endif #ifdef CONFIG_HYPERV_TSCPAGE
@@ -130,30 +123,10 @@ static notrace u64 vread_hvclock(void) }#endif -notrace static u64 vread_tsc(void) -{ -u64 ret = (u64 )rdtsc_ordered (); -u64 last = gtod->cycle_last; - -if (likely (ret >= last)) -return ret; - -/* -* GCC likes to generate cmov here, but this branch is extremely -* predictable (it's just a function of time and the likely is -* very likely) and there's a data dependence, so force GCC -* to generate a branch instead. I don't barrier() because -* we don't actually need a barrier, and if this function -* ever gets inlined it will generate worse code. -*/ - asmvolatile ("" ); -return last; -} - notrace static inline u64 vgetcyc(int mode) {if (mode ==VCLOCK_TSC ) -return vread_tsc (); +return (u64 )rdtsc_ordered ();#ifdef CONFIG_PARAVIRT_CLOCK else if (mode == VCLOCK_PVCLOCK) return vread_pvclock();
@@ -168,17 +141,19 @@ notrace static inline u64 vgetcyc(int mo notrace static int do_hres(clockid_t clk, struct timespec *ts) {struct vgtod_ts *base = >od->basetime[clk]; +u64 cycles, last, ns;unsigned int seq; -u64 cycles, ns;do { seq =gtod_read_begin (gtod); ts->tv_sec = base->sec; ns = base->nsec; + last = gtod->cycle_last; cycles =vgetcyc (gtod->vclock_mode);if (unlikely ((s64 )cycles <0 ))return vdso_fallback_gettime (clk, ts); - ns += (cycles - gtod->cycle_last) * gtod->mult; +if (cycles > last) + ns += (cycles - last) * gtod->mult; ns >>= gtod->shift; }while (unlikely (gtod_read_retry (gtod, seq)));
PATCH 11/11 x66/vdso: Add CLOCK_TAI support
In reply to Thomas GleixnerWith the storage array in place it's now trivial to support CLOCK_TAI in the vdso. Instead of extending the array to accomodate CLOCK_TAI, make use of the fact that:
- CLOCK ids are set in stone
- CLOCK_THREAD_CPUTIME is never going to be supported in the VDSO so the array slot 3 is unused
- CLOCK_TAI is id 11 which results in 3 when masked with 0x3
Add the mask to the basetime array lookup and set up the CLOCK_TAI base time in update_vsyscall().
The performance impact of the mask operation is within the noise.
--- arch/x86/entry/vdso/vclock_gettime.c | 2 +- arch/x86/entry/vsyscall/vsyscall_gtod.c | 4 ++++ arch/x86/include/asm/vgtod.h | 6 +++++- 3 files changed, 10 insertions(+), 2 deletions(-)
arch/x86/entry/vdso/vclock_gettime.c +1 -1
@@ -140,7 +140,7 @@ notrace static inline u64 vgetcyc(int mo notrace static int do_hres(clockid_t clk, struct timespec *ts) { -struct vgtod_ts *base = >od->basetime[clk]; +struct vgtod_ts *base = >od->basetime[clk &VGTOD_HRES_MASK ];u64 cycles, last, ns;unsigned int seq;
arch/x86/entry/vsyscall/vsyscall_gtod.c +4 -0
@@ -51,6 +51,10 @@ void update_vsyscall(struct timekeeper * base->sec = tk->xtime_sec; base->nsec = tk->tkr_mono.xtime_nsec; + base = &vdata->basetime[VGTOD_TAI ]; + base->sec = tk->xtime_sec + (s64 )tk->tai_offset; + base->nsec = tk->tkr_mono.xtime_nsec; + base = &vdata->basetime[CLOCK_MONOTONIC ]; base->sec = tk->xtime_sec + tk->wall_to_monotonic.tv_sec; nsec = tk->tkr_mono.xtime_nsec;
arch/x86/include/asm/vgtod.h +5 -1
@@ -19,9 +19,13 @@ struct vgtod_ts { };#define VGTOD_BASES (CLOCK_MONOTONIC_COARSE + 1) -#define VGTOD_HRES (BIT(CLOCK_REALTIME) | BIT(CLOCK_MONOTONIC)) +#define VGTOD_HRES (BIT(CLOCK_REALTIME) | BIT(CLOCK_MONOTONIC) | BIT(CLOCK_TAI))#define VGTOD_COARSE (BIT(CLOCK_REALTIME_COARSE) | BIT(CLOCK_MONOTONIC_COARSE)) +/* Abuse CLOCK_THREAD_CPUTIME_ID for VGTOD CLOCK TAI */ +#define VGTOD_HRES_MASK 0x3 +#define VGTOD_TAI (CLOCK_TAI & VGTOD_HRES_MASK) +/* * vsyscall_gtod_data will be accessed by 32 and 64 bit code at the same time * so be carefull by modifying this structure.
PATCH 5/11 x86/vdso: Introduce and use vgtod_ts
In reply to Thomas GleixnerIt's desired to support more clocks in the VDSO, e.g. CLOCK_TAI. This results either in indirect calls due to the larger switch case, which then requires retpolines or when the compiler is forced to avoid jump tables it results in even more conditionals.
To avoid both variants which are bad for performance the high resolution functions and the coarse grained functions will be collapsed into one for each. That requires to store the clock specific base time in an array.
Introcude struct vgtod_ts for storage and convert the data store, the update function and the individual clock functions over to use it.
The new storage does not longer use gtod_long_t for seconds depending on 32 or 64 bit compile because this needs to be the full 64bit value even for 32bit when a Y2038 function is added. No point in keeping the distinction alive in the internal representation.
--- arch/x86/entry/vdso/vclock_gettime.c | 24 +++++++++------ arch/x86/entry/vsyscall/vsyscall_gtod.c | 51 ++++++++++++++++---------------- arch/x86/include/asm/vgtod.h | 36 ++++++++++++---------- 3 files changed, 61 insertions(+), 50 deletions(-)
arch/x86/entry/vdso/vclock_gettime.c +15 -9
@@ -206,6 +206,7 @@ notrace static inline u64 vgetsns(int *m/* Code size doesn't matter (vdso is 4k anyway) and this is faster. */ notrace static int __always_inline do_realtime(struct timespec *ts) { +struct vgtod_ts *base = >od->basetime[CLOCK_REALTIME ];unsigned int seq;u64 ns;int mode;
@@ -213,8 +214,8 @@ notrace static int __always_inline do_redo { seq =gtod_read_begin (gtod); mode = gtod->vclock_mode; - ts->tv_sec = gtod->wall_time_sec; - ns = gtod->wall_time_snsec; + ts->tv_sec = base->sec; + ns = base->nsec; ns +=vgetsns (&mode); ns >>= gtod->shift; }while (unlikely (gtod_read_retry (gtod, seq)));
@@ -227,6 +228,7 @@ notrace static int __always_inline do_re notrace static int __always_inline do_monotonic(struct timespec *ts) { +struct vgtod_ts *base = >od->basetime[CLOCK_MONOTONIC ];unsigned int seq;u64 ns;int mode;
@@ -234,8 +236,8 @@ notrace static int __always_inline do_modo { seq =gtod_read_begin (gtod); mode = gtod->vclock_mode; - ts->tv_sec = gtod->monotonic_time_sec; - ns = gtod->monotonic_time_snsec; + ts->tv_sec = base->sec; + ns = base->nsec; ns +=vgetsns (&mode); ns >>= gtod->shift; }while (unlikely (gtod_read_retry (gtod, seq)));
@@ -248,21 +250,25 @@ notrace static int __always_inline do_mo notrace static void do_realtime_coarse(struct timespec *ts) { +struct vgtod_ts *base = >od->basetime[CLOCK_REALTIME_COARSE ];unsigned int seq; +do { seq =gtod_read_begin (gtod); - ts->tv_sec = gtod->wall_time_coarse_sec; - ts->tv_nsec = gtod->wall_time_coarse_nsec; + ts->tv_sec = base->sec; + ts->tv_nsec = base->nsec; }while (unlikely (gtod_read_retry (gtod, seq))); } notrace static void do_monotonic_coarse(struct timespec *ts) { +struct vgtod_ts *base = >od->basetime[CLOCK_MONOTONIC_COARSE ];unsigned int seq; +do { seq =gtod_read_begin (gtod); - ts->tv_sec = gtod->monotonic_time_coarse_sec; - ts->tv_nsec = gtod->monotonic_time_coarse_nsec; + ts->tv_sec = base->sec; + ts->tv_nsec = base->nsec; }while (unlikely (gtod_read_retry (gtod, seq))); }
@@ -318,7 +324,7 @@ int gettimeofday(struct timeval *, struc notrace time_t __vdso_time(time_t *t) {/* This is atomic on x86 so we don't need any locks. */ -time_t result =READ_ONCE (gtod->wall_time_sec); +time_t result =READ_ONCE (gtod->basetime[CLOCK_REALTIME ].sec);if (t) *t = result;
arch/x86/entry/vsyscall/vsyscall_gtod.c +26 -25
66 lines not on this page · raw message
arch/x86/include/asm/vgtod.h +20 -16
@@ -5,33 +5,37 @@#include <linux/compiler.h> #include <linux/clocksource.h> +#include <uapi/linux/time.h> +#ifdef BUILD_VDSO32_64 typedef u64 gtod_long_t ;#else typedef unsigned long gtod_long_t ;#endif + +struct vgtod_ts { +u64 sec; +u64 nsec; +}; + +#define VGTOD_BASES (CLOCK_MONOTONIC_COARSE + 1) +#define VGTOD_HRES (BIT(CLOCK_REALTIME) | BIT(CLOCK_MONOTONIC)) +#define VGTOD_COARSE (BIT(CLOCK_REALTIME_COARSE) | BIT(CLOCK_MONOTONIC_COARSE)) +/* * vsyscall_gtod_data will be accessed by 32 and 64 bit code at the same time * so be carefull by modifying this structure. */ struct vsyscall_gtod_data { -unsigned int seq; +unsigned int seq; + +int vclock_mode; +u64 cycle_last; +u64 mask; +u32 mult; +u32 shift; -int vclock_mode; -u64 cycle_last; -u64 mask; -u32 mult; -u32 shift; - -/* open coded 'struct timespec' */ -u64 wall_time_snsec; -gtod_long_t wall_time_sec; -gtod_long_t monotonic_time_sec; -u64 monotonic_time_snsec; -gtod_long_t wall_time_coarse_sec; -gtod_long_t wall_time_coarse_nsec; -gtod_long_t monotonic_time_coarse_sec; -gtod_long_t monotonic_time_coarse_nsec; +struct vgtod_ts basetime[VGTOD_BASES ];int tz_minuteswest;int tz_dsttime;
PATCH 4/11 x86/vdso: Use unsigned int consistently for vsyscall_gtod_data::seq
In reply to Thomas GleixnerThe sequence count in vgtod_data is unsigned int, but the call sites use unsigned long, which is a pointless exercise. Fix the call sites and replace 'unsigned' with unsinged 'int' while at it.
--- arch/x86/entry/vdso/vclock_gettime.c | 8 ++++---- arch/x86/include/asm/vgtod.h | 10 +++++----- 2 files changed, 9 insertions(+), 9 deletions(-)
arch/x86/entry/vdso/vclock_gettime.c +4 -4
@@ -206,7 +206,7 @@ notrace static inline u64 vgetsns(int *m/* Code size doesn't matter (vdso is 4k anyway) and this is faster. */ notrace static int __always_inline do_realtime(struct timespec *ts) { -unsigned long seq; +unsigned int seq;u64 ns;int mode;
@@ -227,7 +227,7 @@ notrace static int __always_inline do_re notrace static int __always_inline do_monotonic(struct timespec *ts) { -unsigned long seq; +unsigned int seq;u64 ns;int mode;
@@ -248,7 +248,7 @@ notrace static int __always_inline do_mo notrace static void do_realtime_coarse(struct timespec *ts) { -unsigned long seq; +unsigned int seq; do { seq = gtod_read_begin(gtod); ts->tv_sec = gtod->wall_time_coarse_sec;
@@ -258,7 +258,7 @@ notrace static void do_realtime_coarse(s notrace static void do_monotonic_coarse(struct timespec *ts) { -unsigned long seq; +unsigned int seq; do { seq = gtod_read_begin(gtod); ts->tv_sec = gtod->monotonic_time_coarse_sec;
arch/x86/include/asm/vgtod.h +5 -5
@@ -15,9 +15,9 @@ typedef unsigned long gtod_long_t;* so be carefull by modifying this structure. */ struct vsyscall_gtod_data { -unsigned seq; +unsigned int seq; -int vclock_mode; +int vclock_mode;u64 cycle_last;u64 mask;u32 mult;
@@ -44,9 +44,9 @@ static inline bool vclock_was_used(int vreturn READ_ONCE (vclocks_used) & (1 << vclock); } -static inline unsigned gtod_read_begin (const struct vsyscall_gtod_data *s) +static inline unsigned int gtod_read_begin (const struct vsyscall_gtod_data *s) { -unsigned ret; +unsigned int ret; repeat: ret =READ_ONCE (s->seq);
@@ -59,7 +59,7 @@ static inline unsigned gtod_read_begin(c }static inline int gtod_read_retry (const struct vsyscall_gtod_data *s, -unsigned start) +unsigned int start) {smp_rmb ();return unlikely (s->seq != start);
PATCH 6/11 x86/vdso: Collapse high resolution functions
In reply to Thomas Gleixnerdo_realtime() and do_monotonic() are now the same except for the storage array index. Hand the index in as an argument and collapse the functions.
--- arch/x86/entry/vdso/vclock_gettime.c | 35 +++++++---------------------------- 1 file changed, 7 insertions(+), 28 deletions(-)
arch/x86/entry/vdso/vclock_gettime.c +7 -28
@@ -203,35 +203,12 @@ notrace static inline u64 vgetsns(int *mreturn v * gtod->mult; } -/* Code size doesn't matter (vdso is 4k anyway) and this is faster. */ -notrace static int __always_inline do_realtime(struct timespec *ts) +notrace static int do_hres(clockid_t clk, struct timespec *ts) { -struct vgtod_ts *base = >od->basetime[CLOCK_REALTIME ]; +struct vgtod_ts *base = >od->basetime[clk];unsigned int seq; -u64 ns;int mode; - -do { - seq =gtod_read_begin (gtod); - mode = gtod->vclock_mode; - ts->tv_sec = base->sec; - ns = base->nsec; - ns +=vgetsns (&mode); - ns >>= gtod->shift; - }while (unlikely (gtod_read_retry (gtod, seq))); - - ts->tv_sec +=__iter_div_u64_rem (ns,NSEC_PER_SEC , &ns); - ts->tv_nsec = ns; - -return mode; -} - -notrace static int __always_inline do_monotonic(struct timespec *ts) -{ -struct vgtod_ts *base = >od->basetime[CLOCK_MONOTONIC ]; -unsigned int seq;u64 ns; -int mode; do { seq = gtod_read_begin(gtod);
@@ -276,11 +253,11 @@ notrace int __vdso_clock_gettime(clockid {switch (clock) {case CLOCK_REALTIME : -if (do_realtime (ts) ==VCLOCK_NONE ) +if (do_hres (CLOCK_REALTIME , ts) ==VCLOCK_NONE ) goto fallback;break ;case CLOCK_MONOTONIC : -if (do_monotonic (ts) ==VCLOCK_NONE ) +if (do_hres (CLOCK_MONOTONIC , ts) ==VCLOCK_NONE ) goto fallback;break ;case CLOCK_REALTIME_COARSE :
@@ -303,7 +280,9 @@ int clock_gettime(clockid_t, struct time notrace int __vdso_gettimeofday(struct timeval *tv, struct timezone *tz) {if (likely (tv !=NULL )) { -if (unlikely (do_realtime ((struct timespec *)tv) ==VCLOCK_NONE )) +struct timespec *ts = (struct timespec *) tv; + +if (unlikely (do_hres (CLOCK_REALTIME , ts) ==VCLOCK_NONE ))return vdso_fallback_gtod (tv, tz); tv->tv_usec /=1000 ; }
PATCH 7/11 x86/vdso: Collapse coarse functions
In reply to Thomas Gleixnerdo_realtime_coarse() and do_monotonic_coarse() are now the same except for the storage array index. Hand the index in as an argument and collapse the functions.
--- arch/x86/entry/vdso/vclock_gettime.c | 20 ++++---------------- 1 file changed, 4 insertions(+), 16 deletions(-)
arch/x86/entry/vdso/vclock_gettime.c +4 -16
@@ -225,21 +225,9 @@ notrace static int do_hres(clockid_t clkreturn mode; } -notrace static void do_realtime_coarse(struct timespec *ts) +notrace static void do_coarse(clockid_t clk, struct timespec *ts) { -struct vgtod_ts *base = >od->basetime[CLOCK_REALTIME_COARSE ]; -unsigned int seq; - -do { - seq =gtod_read_begin (gtod); - ts->tv_sec = base->sec; - ts->tv_nsec = base->nsec; - }while (unlikely (gtod_read_retry (gtod, seq))); -} - -notrace static void do_monotonic_coarse(struct timespec *ts) -{ -struct vgtod_ts *base = >od->basetime[CLOCK_MONOTONIC_COARSE ]; +struct vgtod_ts *base = >od->basetime[clk];unsigned int seq; do {
@@ -261,10 +249,10 @@ notrace int __vdso_clock_gettime(clockid goto fallback;break ;case CLOCK_REALTIME_COARSE : -do_realtime_coarse (ts); +do_coarse (CLOCK_REALTIME_COARSE , ts);break ;case CLOCK_MONOTONIC_COARSE : -do_monotonic_coarse (ts); +do_coarse (CLOCK_MONOTONIC_COARSE , ts);break ;default : goto fallback;
cover x86/vdso: Cleanups, simmplifications and CLOCK_TAI support
Matt attempted to add CLOCK_TAI support to the VDSO clock_gettime() implementation, which extended the clockid switch case and added yet another slightly different copy of the same code.
Especially the extended switch case is problematic as the compiler tends to generate a jump table which then requires to use retpolines. If jump tables are disabled it adds yet another conditional to the existing maze.
This series takes a different approach by consolidating the almost identical functions into one implementation for high resolution clocks and one for the coarse grained clock ids by storing the base data for each clock id in an array which is indexed by the clock id.
This completely eliminates the switch case and allows further simplifications of the code base, which at the end all together gain a few cycles performance or at least stay on par with todays code. The resulting performance depends heavily on the micro architecture and the compiler.
Thanks,
tglx
8<-------------------
arch/x86/Kconfig | 1 arch/x86/entry/vdso/vclock_gettime.c | 199 ++++++++------------------------ arch/x86/entry/vsyscall/vsyscall_gtod.c | 55 ++++---- arch/x86/include/asm/vgtod.h | 46 ++++--- arch/x86/kernel/time.c | 22 +++ include/linux/clocksource.h | 5 kernel/time/Kconfig | 4 kernel/time/clocksource.c | 2 8 files changed, 144 insertions(+), 190 deletions(-)
Quoted 6 lines · Thomas Gleixner
Matt attempted to add
CLOCK_TAIsupport to the VDSOclock_gettime()implementation, which extended the clockid switch case and added yet another slightly different copy of the same code.Especially the extended switch case is problematic as the compiler tends to generate a jump table which then requires to use retpolines.
Does vDSO code really have to use retpolines? It's in userspace, after all.
Thanks,
Florian
Quoted 9 lines · Florian Weimer
Quoted 6 lines · Thomas Gleixner
Matt attempted to add
CLOCK_TAIsupport to the VDSOclock_gettime()implementation, which extended the clockid switch case and added yet another slightly different copy of the same code.Especially the extended switch case is problematic as the compiler tends to generate a jump table which then requires to use retpolines.
Does vDSO code really have to use retpolines? It's in userspace, after all.
Unless you have IBRS/STIPB enabled, you need user space ratpoutine as well.
Thanks,
tglx
Quoted 9 lines · Florian Weimer
Quoted 6 lines · Thomas Gleixner
Matt attempted to add
CLOCK_TAIsupport to the VDSOclock_gettime()implementation, which extended the clockid switch case and added yet another slightly different copy of the same code.Especially the extended switch case is problematic as the compiler tends to generate a jump table which then requires to use retpolines.
Does vDSO code really have to use retpolines? It's in userspace, after all.
Unless you have IBRS/STIPB enabled, you need user space ratpoutine as well.
Thanks,
tglx
Quoted 7 lines · Thomas Gleixner
Quoted 5 lines · Florian Weimer
Quoted 4 lines · Thomas Gleixner
Matt attempted to add
CLOCK_TAIsupport to the VDSOclock_gettime()implementation, which extended the clockid switch case and added yet another slightly different copy of the same code.
Quoted 6 lines
Quoted 4 lines
Quoted 2 lines
Especially the extended switch case is problematic as the compiler tends to generate a jump table which then requires to use retpolines.
Does vDSO code really have to use retpolines? It's in userspace, after all.
Unless you have IBRS/STIPB enabled, you need user space ratpoutine as well.
I don't think this is a consensus position, and it obviously depends on the (sub)architecture.
Thanks,
Florian
Quoted 9 lines · Florian Weimer
Quoted 6 lines · Thomas Gleixner
Matt attempted to add
CLOCK_TAIsupport to the VDSOclock_gettime()implementation, which extended the clockid switch case and added yet another slightly different copy of the same code.Especially the extended switch case is problematic as the compiler tends to generate a jump table which then requires to use retpolines.
Does vDSO code really have to use retpolines? It's in userspace, after all.
Userspace is equally susceptible to spectre-v2. Ideally we'd recompile world with retpoline, but given the amount of inline asm in say things like openssl and similar projects, validating that there are indeed no indirect calls/jumps left is nontrivial.
There are currently pending patches to otherwise address user-user spectre-v2 attacks.
Quoted 9 lines · Florian Weimer
Quoted 6 lines · Thomas Gleixner
Matt attempted to add
CLOCK_TAIsupport to the VDSOclock_gettime()implementation, which extended the clockid switch case and added yet another slightly different copy of the same code.Especially the extended switch case is problematic as the compiler tends to generate a jump table which then requires to use retpolines.
Does vDSO code really have to use retpolines? It's in userspace, after all.
Userspace is equally susceptible to spectre-v2. Ideally we'd recompile world with retpoline, but given the amount of inline asm in say things like openssl and similar projects, validating that there are indeed no indirect calls/jumps left is nontrivial.
There are currently pending patches to otherwise address user-user spectre-v2 attacks.
Quoted 12 lines · Florian Weimer
Quoted 11 lines · Thomas Gleixner
Quoted 9 lines · Florian Weimer
Quoted 7 lines · Thomas Gleixner
Matt attempted to add
CLOCK_TAIsupport to the VDSOclock_gettime()implementation, which extended the clockid switch case and added yet another slightly different copy of the same code.Especially the extended switch case is problematic as the compiler tends to
generate a jump table which then requires to use retpolines.
Quoted 7 lines
Quoted 4 lines
Quoted 2 lines
Does vDSO code really have to use retpolines? It's in userspace, after all.
Unless you have IBRS/STIPB enabled, you need user space ratpoutine as well.
I don't think this is a consensus position, and it obviously depends on the (sub)architecture.
It does, but we are not building kernels for specific micro architectures nor do distros AFAIK.
But that aside, even with jump tables the generated code (ratpoutine disabled) is not any better than with the current approach. It's even worse and needs the same optimizations at the end and then the main difference is that you have 3 copies of one function and 2 of the other. Not a win at all.
Thanks,
tglx
Quoted 12 lines · Florian Weimer
Quoted 11 lines · Thomas Gleixner
Quoted 9 lines · Florian Weimer
Quoted 7 lines · Thomas Gleixner
Matt attempted to add
CLOCK_TAIsupport to the VDSOclock_gettime()implementation, which extended the clockid switch case and added yet another slightly different copy of the same code.Especially the extended switch case is problematic as the compiler tends to
generate a jump table which then requires to use retpolines.
Quoted 7 lines
Quoted 4 lines
Quoted 2 lines
Does vDSO code really have to use retpolines? It's in userspace, after all.
Unless you have IBRS/STIPB enabled, you need user space ratpoutine as well.
I don't think this is a consensus position, and it obviously depends on the (sub)architecture.
It does, but we are not building kernels for specific micro architectures nor do distros AFAIK.
But that aside, even with jump tables the generated code (ratpoutine disabled) is not any better than with the current approach. It's even worse and needs the same optimizations at the end and then the main difference is that you have 3 copies of one function and 2 of the other. Not a win at all.
Thanks,
tglx
Re: [patch 11/11] x66/vdso: Add CLOCK_TAI support
In reply to Thomas GleixnerQuoted 8 lines · Thomas Gleixner
With the storage array in place it's now trivial to support
CLOCK_TAIin the vdso. Instead of extending the array to accomodateCLOCK_TAI, make use of the fact that:
Quoted 7 lines
- CLOCK ids are set in stone
-CLOCK_THREAD_CPUTIMEis never going to be supported in the VDSO so the array slot 3 is unused
-CLOCK_TAIis id 11 which results in 3 when masked with0x3Add the mask to the basetime array lookup and set up the
CLOCK_TAIbase time inupdate_vsyscall().
That’s... horrible. In an amazing way. Can you add BUILD_BUG_ON somewhere to assert that this actually works?
Quoted 43 lines
The performance impact of the mask operation is within the noise.
Signed-off-by Thomas Gleixner <[email protected]>---
arch/x86/entry/vdso/vclock_gettime.c | 2 +- arch/x86/entry/vsyscall/vsyscall_gtod.c | 4 ++++ arch/x86/include/asm/vgtod.h | 6 +++++- 3 files changed, 10 insertions(+), 2 deletions(-)arch/x86/entry/vdso/vclock_gettime.c +1
-1@@ -140,7 +140,7 @@ notrace static inline u64 vgetcyc(int mo notrace static int do_hres(clockid_t clk, struct timespec *ts) { -struct vgtod_ts *base = >od->basetime[clk]; +struct vgtod_ts *base = >od->basetime[clk &VGTOD_HRES_MASK ];u64 cycles, last, ns;unsigned int seq;arch/x86/entry/vsyscall/vsyscall_gtod.c +4
-0@@ -51,6 +51,10 @@ void update_vsyscall(struct timekeeper * base->sec = tk->xtime_sec; base->nsec = tk->tkr_mono.xtime_nsec; + base = &vdata->basetime[VGTOD_TAI ]; + base->sec = tk->xtime_sec + (s64 )tk->tai_offset; + base->nsec = tk->tkr_mono.xtime_nsec; + base = &vdata->basetime[CLOCK_MONOTONIC ]; base->sec = tk->xtime_sec + tk->wall_to_monotonic.tv_sec; nsec = tk->tkr_mono.xtime_nsec;arch/x86/include/asm/vgtod.h +0
-1@@ -19,9 +19,13 @@ struct vgtod_ts { };#define VGTOD_BASES (CLOCK_MONOTONIC_COARSE + 1) -#define VGTOD_HRES (BIT(CLOCK_REALTIME) | BIT(CLOCK_MONOTONIC))
arch/x86/include/asm/vgtod.h struct vgtod_ts { · line 31
Quoted 10 lines
arch/x86/include/asm/vgtod.h +5
-0+#define VGTOD_HRES (BIT(CLOCK_REALTIME) | BIT(CLOCK_MONOTONIC) | BIT(CLOCK_TAI))#define VGTOD_COARSE (BIT(CLOCK_REALTIME_COARSE) | BIT(CLOCK_MONOTONIC_COARSE)) +/* Abuse CLOCK_THREAD_CPUTIME_ID for VGTOD CLOCK TAI */ +#define VGTOD_HRES_MASK 0x3 +#define VGTOD_TAI (CLOCK_TAI & VGTOD_HRES_MASK) +/* * vsyscall_gtod_data will be accessed by 32 and 64 bit code at the same time * so be carefull by modifying this structure.
_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
Re: [patch 11/11] x66/vdso: Add CLOCK_TAI support
In reply to Thomas GleixnerQuoted 8 lines · Thomas Gleixner
With the storage array in place it's now trivial to support
CLOCK_TAIin the vdso. Instead of extending the array to accomodateCLOCK_TAI, make use of the fact that:
Quoted 7 lines
- CLOCK ids are set in stone
-CLOCK_THREAD_CPUTIMEis never going to be supported in the VDSO so the array slot 3 is unused
-CLOCK_TAIis id 11 which results in 3 when masked with0x3Add the mask to the basetime array lookup and set up the
CLOCK_TAIbase time inupdate_vsyscall().
That’s... horrible. In an amazing way. Can you add BUILD_BUG_ON somewhere to assert that this actually works?
Quoted 53 lines
The performance impact of the mask operation is within the noise.
Signed-off-by Thomas Gleixner <[email protected]>---
arch/x86/entry/vdso/vclock_gettime.c | 2 +- arch/x86/entry/vsyscall/vsyscall_gtod.c | 4 ++++ arch/x86/include/asm/vgtod.h | 6 +++++- 3 files changed, 10 insertions(+), 2 deletions(-)arch/x86/entry/vdso/vclock_gettime.c +1
-1@@ -140,7 +140,7 @@ notrace static inline u64 vgetcyc(int mo notrace static int do_hres(clockid_t clk, struct timespec *ts) { -struct vgtod_ts *base = >od->basetime[clk]; +struct vgtod_ts *base = >od->basetime[clk &VGTOD_HRES_MASK ];u64 cycles, last, ns;unsigned int seq;arch/x86/entry/vsyscall/vsyscall_gtod.c +4
-0@@ -51,6 +51,10 @@ void update_vsyscall(struct timekeeper * base->sec = tk->xtime_sec; base->nsec = tk->tkr_mono.xtime_nsec; + base = &vdata->basetime[VGTOD_TAI ]; + base->sec = tk->xtime_sec + (s64 )tk->tai_offset; + base->nsec = tk->tkr_mono.xtime_nsec; + base = &vdata->basetime[CLOCK_MONOTONIC ]; base->sec = tk->xtime_sec + tk->wall_to_monotonic.tv_sec; nsec = tk->tkr_mono.xtime_nsec;arch/x86/include/asm/vgtod.h +5
-1@@ -19,9 +19,13 @@ struct vgtod_ts { };#define VGTOD_BASES (CLOCK_MONOTONIC_COARSE + 1) -#define VGTOD_HRES (BIT(CLOCK_REALTIME) | BIT(CLOCK_MONOTONIC)) +#define VGTOD_HRES (BIT(CLOCK_REALTIME) | BIT(CLOCK_MONOTONIC) | BIT(CLOCK_TAI))#define VGTOD_COARSE (BIT(CLOCK_REALTIME_COARSE) | BIT(CLOCK_MONOTONIC_COARSE)) +/* Abuse CLOCK_THREAD_CPUTIME_ID for VGTOD CLOCK TAI */ +#define VGTOD_HRES_MASK 0x3 +#define VGTOD_TAI (CLOCK_TAI & VGTOD_HRES_MASK) +/* * vsyscall_gtod_data will be accessed by 32 and 64 bit code at the same time * so be carefull by modifying this structure.
Quoted 14 lines · Thomas Gleixner
Matt attempted to add
CLOCK_TAIsupport to the VDSOclock_gettime()implementation, which extended the clockid switch case and added yet another slightly different copy of the same code.Especially the extended switch case is problematic as the compiler tends to generate a jump table which then requires to use retpolines. If jump tables are disabled it adds yet another conditional to the existing maze.
This series takes a different approach by consolidating the almost identical functions into one implementation for high resolution clocks and one for the coarse grained clock ids by storing the base data for each clock id in an array which is indexed by the clock id.
Quoted 4 lines
This completely eliminates the switch case and allows further simplifications of the code base, which at the end all together gain a few cycles performance or at least stay on par with todays code. The resulting performance depends heavily on the micro architecture and the compiler.
No objections from my side, just a few general remarks: Deepa and I have discussed the vdso in the past for 2038. I have started a patch that I'll have to redo on top of yours, but that is fine, your cleanup is only going to help here.
More generally speaking, Deepa said that she would like to see some generalization on the vdso before adding the time64_t based variants. Your series probably makes x86 diverge more from the others, which makes it harder to consolidate them again, but we might just as well use your new implementation to base the generic one on, and just move the other ones over to that.
A couple of architectures (s390, ia64, riscv, powerpc, arm64) implement the vdso as assembler code at the moment, so they won't be as easy to consolidate (other than outright replacing all the code).
The other five:arch/x86/entry/vdso/vclock_gettime.carch/sparc/vdso/vclock_gettime.carch/nds32/kernel/vdso/gettimeofday.carch/mips/vdso/gettimeofday.carch/arm/vdso/vgettimeofday.c
are basically all minor variations of the same code base and could be consolidated to some degree.
Any suggestions here? Should we plan to do that consolitdation based on your new version, or just add clock_gettime64 in arm32 and x86-32, and then be done with it? The other ones will obviously still be fast for 32-bit time_t and will have a working non-vdso sys_clock_getttime64().
I also wonder about clock_getres(): half the architectures seem to implement it in vdso, but notably arm32 and x86 don't, and I had not expected it to be performance critical given that the result is easily cached in user space.
Arnd
Quoted 14 lines · Thomas Gleixner
Matt attempted to add
CLOCK_TAIsupport to the VDSOclock_gettime()implementation, which extended the clockid switch case and added yet another slightly different copy of the same code.Especially the extended switch case is problematic as the compiler tends to generate a jump table which then requires to use retpolines. If jump tables are disabled it adds yet another conditional to the existing maze.
This series takes a different approach by consolidating the almost identical functions into one implementation for high resolution clocks and one for the coarse grained clock ids by storing the base data for each clock id in an array which is indexed by the clock id.
Quoted 4 lines
This completely eliminates the switch case and allows further simplifications of the code base, which at the end all together gain a few cycles performance or at least stay on par with todays code. The resulting performance depends heavily on the micro architecture and the compiler.
No objections from my side, just a few general remarks: Deepa and I have discussed the vdso in the past for 2038. I have started a patch that I'll have to redo on top of yours, but that is fine, your cleanup is only going to help here.
More generally speaking, Deepa said that she would like to see some generalization on the vdso before adding the time64_t based variants. Your series probably makes x86 diverge more from the others, which makes it harder to consolidate them again, but we might just as well use your new implementation to base the generic one on, and just move the other ones over to that.
A couple of architectures (s390, ia64, riscv, powerpc, arm64) implement the vdso as assembler code at the moment, so they won't be as easy to consolidate (other than outright replacing all the code).
The other five:arch/x86/entry/vdso/vclock_gettime.carch/sparc/vdso/vclock_gettime.carch/nds32/kernel/vdso/gettimeofday.carch/mips/vdso/gettimeofday.carch/arm/vdso/vgettimeofday.c
are basically all minor variations of the same code base and could be consolidated to some degree.
Any suggestions here? Should we plan to do that consolitdation based on your new version, or just add clock_gettime64 in arm32 and x86-32, and then be done with it? The other ones will obviously still be fast for 32-bit time_t and will have a working non-vdso sys_clock_getttime64().
I also wonder about clock_getres(): half the architectures seem to implement it in vdso, but notably arm32 and x86 don't, and I had not expected it to be performance critical given that the result is easily cached in user space.
Arnd
Re: [patch 11/11] x66/vdso: Add CLOCK_TAI support
In reply to Andy LutomirskiQuoted 11 lines · Andy Lutomirski
Quoted 11 lines · Thomas Gleixner
With the storage array in place it's now trivial to support
CLOCK_TAIin the vdso. Instead of extending the array to accomodateCLOCK_TAI, make use of the fact that:- CLOCK ids are set in stone
-CLOCK_THREAD_CPUTIMEis never going to be supported in the VDSO so the array slot 3 is unused
-CLOCK_TAIis id 11 which results in 3 when masked with0x3
Quoted 5 lines
Quoted 2 lines
Add the mask to the basetime array lookup and set up the
CLOCK_TAIbase time inupdate_vsyscall().That’s... horrible. In an amazing way. Can you add
BUILD_BUG_ONsomewhere to assert that this actually works?
Sure, but changing any of the clock ids will cause more wreckage than that.
Thanks,
tglx
_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
Re: [patch 11/11] x66/vdso: Add CLOCK_TAI support
In reply to Andy LutomirskiQuoted 11 lines · Andy Lutomirski
Quoted 11 lines · Thomas Gleixner
With the storage array in place it's now trivial to support
CLOCK_TAIin the vdso. Instead of extending the array to accomodateCLOCK_TAI, make use of the fact that:- CLOCK ids are set in stone
-CLOCK_THREAD_CPUTIMEis never going to be supported in the VDSO so the array slot 3 is unused
-CLOCK_TAIis id 11 which results in 3 when masked with0x3
Quoted 5 lines
Quoted 2 lines
Add the mask to the basetime array lookup and set up the
CLOCK_TAIbase time inupdate_vsyscall().That’s... horrible. In an amazing way. Can you add
BUILD_BUG_ONsomewhere to assert that this actually works?
Sure, but changing any of the clock ids will cause more wreckage than that.
Thanks,
tglx
Re: [patch 11/11] x66/vdso: Add CLOCK_TAI support
Quoted 16 lines · Thomas Gleixner
Quoted 11 lines · Andy Lutomirski
Quoted 11 lines · Thomas Gleixner
With the storage array in place it's now trivial to support
CLOCK_TAIin the vdso. Instead of extending the array to accomodateCLOCK_TAI, make use of the fact that:- CLOCK ids are set in stone
-CLOCK_THREAD_CPUTIMEis never going to be supported in the VDSO so the array slot 3 is unused
-CLOCK_TAIis id 11 which results in 3 when masked with0x3
Quoted 8 lines
Quoted 5 lines
Quoted 2 lines
Add the mask to the basetime array lookup and set up the
CLOCK_TAIbase time inupdate_vsyscall().That’s... horrible. In an amazing way. Can you add
BUILD_BUG_ONsomewhere to assert that this actually works?Sure, but changing any of the clock ids will cause more wreckage than that.
I’m more concerned that we add a new one and break the magic masking. Maybe two start sharing the same slot.
Quoted 3 lines
Thanks,
tglx
_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
Re: [patch 11/11] x66/vdso: Add CLOCK_TAI support
In reply to Thomas GleixnerQuoted 16 lines · Thomas Gleixner
Quoted 11 lines · Andy Lutomirski
Quoted 11 lines · Thomas Gleixner
With the storage array in place it's now trivial to support
CLOCK_TAIin the vdso. Instead of extending the array to accomodateCLOCK_TAI, make use of the fact that:- CLOCK ids are set in stone
-CLOCK_THREAD_CPUTIMEis never going to be supported in the VDSO so the array slot 3 is unused
-CLOCK_TAIis id 11 which results in 3 when masked with0x3
Quoted 8 lines
Quoted 5 lines
Quoted 2 lines
Add the mask to the basetime array lookup and set up the
CLOCK_TAIbase time inupdate_vsyscall().That’s... horrible. In an amazing way. Can you add
BUILD_BUG_ONsomewhere to assert that this actually works?Sure, but changing any of the clock ids will cause more wreckage than that.
I’m more concerned that we add a new one and break the magic masking. Maybe two start sharing the same slot.
Quoted 3 lines
Thanks,
tglx
Re: [patch 10/11] x86/vdso: Move cycle_last handling into the caller
In reply to Thomas GleixnerQuoted 7 lines · Thomas Gleixner
Dereferencing
gtod->cycle_lastall over the place and foing the cycles < last comparison in the vclock read functions generates horrible code. Doing it at the call site is much better and gains a few cycles both for TSC and pvclock.Caveat: This adds the comparison to the hyperv vclock as well, but I have no way to test that.
While the change looks obviously correct for Hyper-V vclock too, by saying that you encouraged me to give the whole series a shot. I did and turns out Hyper-V vclock is broken: simple clock_gettime(CLOCK_REALTIME, ) test goes from 70 cpu cycles to 1000 (meaning that we're falling back to syscall I guess). Bisecting now, stay tuned!
Signature · 1 line
Vitaly
Re: [patch 10/11] x86/vdso: Move cycle_last handling into the caller
In reply to Thomas GleixnerQuoted 7 lines · Thomas Gleixner
Dereferencing
gtod->cycle_lastall over the place and foing the cycles < last comparison in the vclock read functions generates horrible code. Doing it at the call site is much better and gains a few cycles both for TSC and pvclock.Caveat: This adds the comparison to the hyperv vclock as well, but I have no way to test that.
While the change looks obviously correct for Hyper-V vclock too, by saying that you encouraged me to give the whole series a shot. I did and turns out Hyper-V vclock is broken: simple clock_gettime(CLOCK_REALTIME, ) test goes from 70 cpu cycles to 1000 (meaning that we're falling back to syscall I guess). Bisecting now, stay tuned!
Signature · 1 line
Vitaly
Re: [patch 02/11] x86/time: Implement clocksource_arch_init()
In reply to Thomas GleixnerQuoted 33 lines · Thomas Gleixner
Runtime validate the
VCLOCK_MODEinclocksource::archdataand disable VCLOCK if invalid, which disables the VDSO but keeps the system running.Signed-off-by Thomas Gleixner <[email protected]>--- arch/x86/Kconfig | 1 + arch/x86/kernel/time.c | 16 ++++++++++++++++ 2 files changed, 17 insertions(+)arch/x86/Kconfig +1
-0@@ -48,6 +48,7 @@ config X86 select ACPI_SYSTEM_POWER_STATES_SUPPORT if ACPI select ANON_INODES select ARCH_CLOCKSOURCE_DATA + select ARCH_CLOCKSOURCE_INIT select ARCH_DISCARD_MEMBLOCK select ARCH_HAS_ACPI_TABLE_UPGRADE if ACPI select ARCH_HAS_DEBUG_VIRTUALarch/x86/kernel/time.c +1
-0@@ -10,6 +10,7 @@* */ +#include <linux/clocksource.h> #include <linux/clockchips.h> #include <linux/interrupt.h> #include <linux/irq.h> @@ -105,3 +106,18 @@ void __init time_init(void) { late_time_init = x86_late_time_init; }
arch/x86/kernel/time.c void __init time_init(void) · line 118
Quoted 10 lines
arch/x86/kernel/time.c +10
-0+ +/* +* Sanity check the vdso related archdata content. +*/ +void clocksource_arch_init (struct clocksource *cs) +{ +if (cs->archdata.vclock_mode ==VCLOCK_NONE ) +return ; + +if (cs->archdata.vclock_mode >=VCLOCK_MAX ) {
It should be '>' as VCLOCK_MAX == VCLOCK_HVCLOCK == 3
Quoted 5 lines
arch/x86/kernel/time.c +5
-0+pr_warn ("clocksource %s registered with invalid vclock_mode %d. Disabling vclock.\n" , + cs->name, cs->archdata.vclock_mode); + cs->archdata.vclock_mode =VCLOCK_NONE ; + } +}
Signature · 1 line
Vitaly
Re: [patch 02/11] x86/time: Implement clocksource_arch_init()
In reply to Thomas GleixnerQuoted 33 lines · Thomas Gleixner
Runtime validate the
VCLOCK_MODEinclocksource::archdataand disable VCLOCK if invalid, which disables the VDSO but keeps the system running.Signed-off-by Thomas Gleixner <[email protected]>--- arch/x86/Kconfig | 1 + arch/x86/kernel/time.c | 16 ++++++++++++++++ 2 files changed, 17 insertions(+)arch/x86/Kconfig +1
-0@@ -48,6 +48,7 @@ config X86 select ACPI_SYSTEM_POWER_STATES_SUPPORT if ACPI select ANON_INODES select ARCH_CLOCKSOURCE_DATA + select ARCH_CLOCKSOURCE_INIT select ARCH_DISCARD_MEMBLOCK select ARCH_HAS_ACPI_TABLE_UPGRADE if ACPI select ARCH_HAS_DEBUG_VIRTUALarch/x86/kernel/time.c +1
-0@@ -10,6 +10,7 @@* */ +#include <linux/clocksource.h> #include <linux/clockchips.h> #include <linux/interrupt.h> #include <linux/irq.h> @@ -105,3 +106,18 @@ void __init time_init(void) { late_time_init = x86_late_time_init; }
arch/x86/kernel/time.c void __init time_init(void) · line 118
Quoted 10 lines
arch/x86/kernel/time.c +10
-0+ +/* +* Sanity check the vdso related archdata content. +*/ +void clocksource_arch_init (struct clocksource *cs) +{ +if (cs->archdata.vclock_mode ==VCLOCK_NONE ) +return ; + +if (cs->archdata.vclock_mode >=VCLOCK_MAX ) {
It should be '>' as VCLOCK_MAX == VCLOCK_HVCLOCK == 3
Quoted 5 lines
arch/x86/kernel/time.c +5
-0+pr_warn ("clocksource %s registered with invalid vclock_mode %d. Disabling vclock.\n" , + cs->name, cs->archdata.vclock_mode); + cs->archdata.vclock_mode =VCLOCK_NONE ; + } +}
Signature · 1 line
Vitaly
Re: [patch 02/11] x86/time: Implement clocksource_arch_init()
Quoted 5 lines · Vitaly Kuznetsov
Review
Quoted 2 lines · Thomas Gleixner
(quoted diff) +2
-0+ + if (cs->archdata.vclock_mode >= VCLOCK_MAX) {It should be '>' as
VCLOCK_MAX==VCLOCK_HVCLOCK== 3
Indeed.
Thanks,
tglx
Re: [patch 02/11] x86/time: Implement clocksource_arch_init()
In reply to Vitaly KuznetsovQuoted 5 lines · Vitaly Kuznetsov
Review
Quoted 2 lines · Thomas Gleixner
(quoted diff) +2
-0+ + if (cs->archdata.vclock_mode >= VCLOCK_MAX) {It should be '>' as
VCLOCK_MAX==VCLOCK_HVCLOCK== 3
Indeed.
Thanks,
tglx
Re: [patch 11/11] x66/vdso: Add CLOCK_TAI support
In reply to Andy LutomirskiQuoted 9 lines · Andy Lutomirski
Quoted 7 lines · Thomas Gleixner
Quoted 2 lines · Andy Lutomirski
That’s... horrible. In an amazing way. Can you add
BUILD_BUG_ONsomewhere to assert that this actually works?Sure, but changing any of the clock ids will cause more wreckage than that.
I’m more concerned that we add a new one and break the magic masking. Maybe two start sharing the same slot.
You are right. The obvious extension is CLOCK_BOOTTIME. That's id 7 which indeed conflicts. I'll remove the magic.
Thanks,
tglx
_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
Re: [patch 11/11] x66/vdso: Add CLOCK_TAI support
In reply to Andy LutomirskiQuoted 9 lines · Andy Lutomirski
Quoted 7 lines · Thomas Gleixner
Quoted 2 lines · Andy Lutomirski
That’s... horrible. In an amazing way. Can you add
BUILD_BUG_ONsomewhere to assert that this actually works?Sure, but changing any of the clock ids will cause more wreckage than that.
I’m more concerned that we add a new one and break the magic masking. Maybe two start sharing the same slot.
You are right. The obvious extension is CLOCK_BOOTTIME. That's id 7 which indeed conflicts. I'll remove the magic.
Thanks,
tglx
Quoted 13 lines · Arnd Bergmann
On Fri, Sep 14, 2018 at 2:52 PM Thomas Gleixner <[email protected]> wrote:
A couple of architectures (s390, ia64, riscv, powerpc, arm64) implement the vdso as assembler code at the moment, so they won't be as easy to consolidate (other than outright replacing all the code).The other five:
arch/x86/entry/vdso/vclock_gettime.carch/sparc/vdso/vclock_gettime.carch/nds32/kernel/vdso/gettimeofday.carch/mips/vdso/gettimeofday.carch/arm/vdso/vgettimeofday.c
Quoted 6 lines
are basically all minor variations of the same code base and could be consolidated to some degree.
Any suggestions here? Should we plan to do that consolitdation based on your new version, or just addclock_gettime64in arm32 and x86-32, and then be done with it? The other ones will obviously still be fast for 32-bittime_tand will have a working non-vdsosys_clock_getttime64().
In principle consolidating all those implementations should be possible to some extent and probably worthwhile. What's arch specific are the actual accessors to the hardware clocks.
Quoted 3 lines
I also wonder about
clock_getres(): half the architectures seem to implement it in vdso, but notably arm32 and x86 don't, and I had not expected it to be performance critical given that the result is easily cached in user space.
getres() is not really performance critical, but adding it does not create a huge problem either.
Thanks,
tglx
Quoted 13 lines · Arnd Bergmann
On Fri, Sep 14, 2018 at 2:52 PM Thomas Gleixner <[email protected]> wrote:
A couple of architectures (s390, ia64, riscv, powerpc, arm64) implement the vdso as assembler code at the moment, so they won't be as easy to consolidate (other than outright replacing all the code).The other five:
arch/x86/entry/vdso/vclock_gettime.carch/sparc/vdso/vclock_gettime.carch/nds32/kernel/vdso/gettimeofday.carch/mips/vdso/gettimeofday.carch/arm/vdso/vgettimeofday.c
Quoted 6 lines
are basically all minor variations of the same code base and could be consolidated to some degree.
Any suggestions here? Should we plan to do that consolitdation based on your new version, or just addclock_gettime64in arm32 and x86-32, and then be done with it? The other ones will obviously still be fast for 32-bittime_tand will have a working non-vdsosys_clock_getttime64().
In principle consolidating all those implementations should be possible to some extent and probably worthwhile. What's arch specific are the actual accessors to the hardware clocks.
Quoted 3 lines
I also wonder about
clock_getres(): half the architectures seem to implement it in vdso, but notably arm32 and x86 don't, and I had not expected it to be performance critical given that the result is easily cached in user space.
getres() is not really performance critical, but adding it does not create a huge problem either.
Thanks,
tglx
Andy Lutomirski
maintainerRe: [patch 09/11] x86/vdso: Simplify the invalid vclock case
In reply to Thomas GleixnerQuoted 12 lines · Thomas Gleixner
The code flow for the vclocks is convoluted as it requires the vclocks which can be invalidated separately from the
vsyscall_gtod_datasequence to store the fact in a separate variable. That's inefficient.notrace static int
do_hres(clockid_t clk, struct timespec *ts){ struct vgtod_ts *base = >od->basetime[clk]; unsigned int seq;arch/x86/entry/vdso/vclock_gettime.c +1
-2-int mode; -u64 ns; +u64 cycles, ns;
arch/x86/entry/vdso/vclock_gettime.c notrace static u64 vread_tsc(void) · line 180
Quoted 10 lines
arch/x86/entry/vdso/vclock_gettime.c +3
-2do { seq = gtod_read_begin(gtod); - mode = gtod->vclock_mode; ts->tv_sec = base->sec; ns = base->nsec; - ns += vgetsns(&mode); + cycles = vgetcyc(gtod->vclock_mode); + if (unlikely((s64)cycles < 0)) + return vdso_fallback_gettime(clk, ts);
i was contemplating this, and I would suggest one of two optimizations:
1. have all the helpers return a struct containing a u64 and a bool, and use that bool. The compiler should do okay.
2. Be sneaky. Later in the series, you do:
if (unlikely((s64)cycles < 0))
return vdso_fallback_gettime(clk, ts);- ns += (cycles - gtod->cycle_last) * gtod->mult;
+ if (cycles > last)
+ ns += (cycles - last) * gtod->mult;
How about:
if (unlikely((s64)cycles <= last)) {
if (cycles < 0) [or cycles == -1 or whatever]
return vdso_fallback_gettime;
} else { ns += (cycles - last) * gtod->mult;
}
which should actually make this whole mess be essentially free.
Also, I'm not entirely convinced that this "last" thing is needed at all. John, what's the scenario under which we need it?
--Andy
--Andy
Re: [patch 09/11] x86/vdso: Simplify the invalid vclock case
Quoted 43 lines · Andy Lutomirski
Quoted 12 lines · Thomas Gleixner
The code flow for the vclocks is convoluted as it requires the vclocks which can be invalidated separately from the
vsyscall_gtod_datasequence to store the fact in a separate variable. That's inefficient.notrace static int
do_hres(clockid_t clk, struct timespec *ts){ struct vgtod_ts *base = >od->basetime[clk]; unsigned int seq;arch/x86/entry/vdso/vclock_gettime.c +1
-2-int mode; -u64 ns; +u64 cycles, ns;arch/x86/entry/vdso/vclock_gettime.c notrace static u64 vread_tsc(void) · line 180
Quoted 10 lines
arch/x86/entry/vdso/vclock_gettime.c +3
-2do { seq = gtod_read_begin(gtod); - mode = gtod->vclock_mode; ts->tv_sec = base->sec; ns = base->nsec; - ns += vgetsns(&mode); + cycles = vgetcyc(gtod->vclock_mode); + if (unlikely((s64)cycles < 0)) + return vdso_fallback_gettime(clk, ts);i was contemplating this, and I would suggest one of two optimizations:
1. have all the helpers return a struct containing a u64 and a bool, and use that bool. The compiler should do okay.
2. Be sneaky. Later in the series, you do:
if (unlikely((s64)cycles < 0)) return vdso_fallback_gettime(clk, ts);(quoted diff) +2
-1- ns += (cycles - gtod->cycle_last) * gtod->mult; + if (cycles > last) + ns += (cycles - last) * gtod->mult;How about:
if (unlikely((s64)cycles <= last)) {
if (cycles < 0) [or cycles == -1 or whatever]return vdso_fallback_gettime; } else {
Quoted 7 lines
ns += (cycles - last) *
gtod->mult;
}which should actually make this whole mess be essentially free.
Also, I'm not entirely convinced that this "last" thing is needed at all. John, what's the scenario under which we need it?
So my memory is probably a bit foggy, but I recall that as we accelerated gettimeofday, we found that even on systems that claimed to have synced TSCs, they were actually just slightly out of sync. Enough that right after cycles_last had been updated, a read on another cpu could come in just behind cycles_last, resulting in a negative interval causing lots of havoc.
So the sanity check is needed to avoid that case.
thanks
-john
Re: [patch 09/11] x86/vdso: Simplify the invalid vclock case
Quoted 4 lines · John Stultz
Quoted 2 lines · Andy Lutomirski
Also, I'm not entirely convinced that this "last" thing is needed at all. John, what's the scenario under which we need it?
Quoted 8 lines
So my memory is probably a bit foggy, but I recall that as we accelerated gettimeofday, we found that even on systems that claimed to have synced TSCs, they were actually just slightly out of sync. Enough that right after
cycles_lasthad been updated, a read on another cpu could come in just behindcycles_last, resulting in a negative interval causing lots of havoc.So the sanity check is needed to avoid that case.
Your memory serves you right. That's indeed observable on CPUs which lack TSC_ADJUST.
@Andy: Welcome to the wonderful world of TSC.
Thanks,
tglx
Re: [patch 09/11] x86/vdso: Simplify the invalid vclock case
In reply to John StultzQuoted 4 lines · John Stultz
Quoted 2 lines · Andy Lutomirski
Also, I'm not entirely convinced that this "last" thing is needed at all. John, what's the scenario under which we need it?
Quoted 8 lines
So my memory is probably a bit foggy, but I recall that as we accelerated gettimeofday, we found that even on systems that claimed to have synced TSCs, they were actually just slightly out of sync. Enough that right after
cycles_lasthad been updated, a read on another cpu could come in just behindcycles_last, resulting in a negative interval causing lots of havoc.So the sanity check is needed to avoid that case.
Your memory serves you right. That's indeed observable on CPUs which lack TSC_ADJUST.
@Andy: Welcome to the wonderful world of TSC.
Thanks,
tglx
Re: [patch 09/11] x86/vdso: Simplify the invalid vclock case
Quoted 12 lines · Thomas Gleixner
Quoted 11 lines · John Stultz
Quoted 2 lines · Andy Lutomirski
Also, I'm not entirely convinced that this "last" thing is needed at all. John, what's the scenario under which we need it?
So my memory is probably a bit foggy, but I recall that as we accelerated gettimeofday, we found that even on systems that claimed to have synced TSCs, they were actually just slightly out of sync. Enough that right after
cycles_lasthad been updated, a read on another cpu could come in just behindcycles_last, resulting in a negative interval causing lots of havoc.
Quoted 4 lines
Quoted 1 line
So the sanity check is needed to avoid that case.
Your memory serves you right. That's indeed observable on CPUs which lack
TSC_ADJUST.
But, if the gtod code can observe this, then why doesn't the code that checks the sync?
Re: [patch 09/11] x86/vdso: Simplify the invalid vclock case
Quoted 13 lines · Peter Zijlstra
Quoted 12 lines · Thomas Gleixner
Quoted 11 lines · John Stultz
Quoted 2 lines · Andy Lutomirski
Also, I'm not entirely convinced that this "last" thing is needed at all. John, what's the scenario under which we need it?
So my memory is probably a bit foggy, but I recall that as we accelerated gettimeofday, we found that even on systems that claimed to have synced TSCs, they were actually just slightly out of sync. Enough that right after
cycles_lasthad been updated, a read on another cpu could come in just behindcycles_last, resulting in a negative interval causing lots of havoc.
Quoted 7 lines
Quoted 4 lines
Quoted 1 line
So the sanity check is needed to avoid that case.
Your memory serves you right. That's indeed observable on CPUs which lack
TSC_ADJUST.But, if the gtod code can observe this, then why doesn't the code that checks the sync?
Because it depends where the involved CPUs are in the topology. The sync code might just run on the same package an simply not see it. Yes, w/o TSC_ADJUST the TSC sync code can just fail to see the havoc.
Thanks,
tglx
Re: [patch 09/11] x86/vdso: Simplify the invalid vclock case
In reply to Peter ZijlstraQuoted 13 lines · Peter Zijlstra
Quoted 12 lines · Thomas Gleixner
Quoted 11 lines · John Stultz
Quoted 2 lines · Andy Lutomirski
Also, I'm not entirely convinced that this "last" thing is needed at all. John, what's the scenario under which we need it?
So my memory is probably a bit foggy, but I recall that as we accelerated gettimeofday, we found that even on systems that claimed to have synced TSCs, they were actually just slightly out of sync. Enough that right after
cycles_lasthad been updated, a read on another cpu could come in just behindcycles_last, resulting in a negative interval causing lots of havoc.
Quoted 7 lines
Quoted 4 lines
Quoted 1 line
So the sanity check is needed to avoid that case.
Your memory serves you right. That's indeed observable on CPUs which lack
TSC_ADJUST.But, if the gtod code can observe this, then why doesn't the code that checks the sync?
Because it depends where the involved CPUs are in the topology. The sync code might just run on the same package an simply not see it. Yes, w/o TSC_ADJUST the TSC sync code can just fail to see the havoc.
Thanks,
tglx
Re: [patch 09/11] x86/vdso: Simplify the invalid vclock case
Quoted 10 lines · Thomas Gleixner
Quoted 5 lines · Peter Zijlstra
Quoted 2 lines
Your memory serves you right. That's indeed observable on CPUs which lack
TSC_ADJUST.But, if the gtod code can observe this, then why doesn't the code that checks the sync?
Because it depends where the involved CPUs are in the topology. The sync code might just run on the same package an simply not see it. Yes, w/o
TSC_ADJUSTthe TSC sync code can just fail to see the havoc.
Even with TSC adjust the TSC can be slightly off by design on multi-socket systems.
Thanks,
tglx
Re: [patch 09/11] x86/vdso: Simplify the invalid vclock case
In reply to Thomas GleixnerQuoted 10 lines · Thomas Gleixner
Quoted 5 lines · Peter Zijlstra
Quoted 2 lines
Your memory serves you right. That's indeed observable on CPUs which lack
TSC_ADJUST.But, if the gtod code can observe this, then why doesn't the code that checks the sync?
Because it depends where the involved CPUs are in the topology. The sync code might just run on the same package an simply not see it. Yes, w/o
TSC_ADJUSTthe TSC sync code can just fail to see the havoc.
Even with TSC adjust the TSC can be slightly off by design on multi-socket systems.
Thanks,
tglx
Re: [patch 09/11] x86/vdso: Simplify the invalid vclock case
Quoted 8 lines · Thomas Gleixner
Quoted 7 lines · Thomas Gleixner
Quoted 5 lines · Peter Zijlstra
Quoted 2 lines
Your memory serves you right. That's indeed observable on CPUs which lack
TSC_ADJUST.But, if the gtod code can observe this, then why doesn't the code that checks the sync?
Quoted 6 lines
Quoted 3 lines
Because it depends where the involved CPUs are in the topology. The sync code might just run on the same package an simply not see it. Yes, w/o
TSC_ADJUSTthe TSC sync code can just fail to see the havoc.Even with TSC adjust the TSC can be slightly off by design on multi-socket systems.
Here are the gory details:
https://lore.kernel.org/lkml/[email protected]/
The changelog has an explanation as well.
d8bb6f4c1670 ("x86: tsc prevent time going backwards")I still have one of the machines which is affected by this.
Thanks,
tglx
Re: [patch 09/11] x86/vdso: Simplify the invalid vclock case
In reply to Thomas GleixnerQuoted 8 lines · Thomas Gleixner
Quoted 7 lines · Thomas Gleixner
Quoted 5 lines · Peter Zijlstra
Quoted 2 lines
Your memory serves you right. That's indeed observable on CPUs which lack
TSC_ADJUST.But, if the gtod code can observe this, then why doesn't the code that checks the sync?
Quoted 6 lines
Quoted 3 lines
Because it depends where the involved CPUs are in the topology. The sync code might just run on the same package an simply not see it. Yes, w/o
TSC_ADJUSTthe TSC sync code can just fail to see the havoc.Even with TSC adjust the TSC can be slightly off by design on multi-socket systems.
Here are the gory details:
https://lore.kernel.org/lkml/[email protected]/
The changelog has an explanation as well.
d8bb6f4c1670 ("x86: tsc prevent time going backwards")I still have one of the machines which is affected by this.
Thanks,
tglx
Re: [patch 09/11] x86/vdso: Simplify the invalid vclock case
Quoted 18 lines · Thomas Gleixner
Quoted 8 lines · Thomas Gleixner
Quoted 7 lines · Thomas Gleixner
Quoted 5 lines · Peter Zijlstra
Quoted 2 lines
Your memory serves you right. That's indeed observable on CPUs which lack
TSC_ADJUST.But, if the gtod code can observe this, then why doesn't the code that checks the sync?
Quoted 6 lines
Quoted 3 lines
Because it depends where the involved CPUs are in the topology. The sync code might just run on the same package an simply not see it. Yes, w/o
TSC_ADJUSTthe TSC sync code can just fail to see the havoc.Even with TSC adjust the TSC can be slightly off by design on multi-socket systems.
Here are the gory details:
Quoted 7 lines
https://lore.kernel.org/lkml/[email protected]/The changelog has an explanation as well.
d8bb6f4c1670 ("x86: tsc prevent time going backwards")I still have one of the machines which is affected by this.
Are we sure this isn't a load vs rdtsc reorder? Because if I look at the current code:
notrace static u64 vread_tsc(void)
{
u64 ret = (u64)rdtsc_ordered();
u64 last = gtod->cycle_last;
if (likely(ret >= last))
return ret;
/*
* GCC likes to generate cmov here, but this branch is extremely
* predictable (it's just a function of time and the likely is
* very likely) and there's a data dependence, so force GCC
* to generate a branch instead. I don't barrier() because
* we don't actually need a barrier, and if this function
* ever gets inlined it will generate worse code.
*/
asm volatile ("");
return last;
}That does:
lfence rdtsc load gtod->cycle_last
Which obviously allows us to observe a cycles_last that is later than the rdtsc itself, and thus time can trivially go backwards.
The new code:
last = gtod->cycle_last; cycles = vgetcyc(gtod->vclock_mode); if (unlikely((s64)cycles < 0)) return vdso_fallback_gettime(clk, ts); if (cycles > last) ns += (cycles - last) * gtod->mult;
looks like:
load gtod->cycle_last lfence rdtsc
which avoids that possibility, the cycle_last load must have completed before the rdtsc.
Re: [patch 09/11] x86/vdso: Simplify the invalid vclock case
Quoted 5 lines · Peter Zijlstra
Quoted 1 line · Thomas Gleixner
I still have one of the machines which is affected by this.
Are we sure this isn't a load vs rdtsc reorder? Because if I look at the current code:
The load order of last vs. rdtsc does not matter at all.
CPU0 CPU1
....
now0 = rdtsc_ordered();
...tk->cycle_last = now0;
gtod->seq++;gtod->cycle_last = tk->cycle_last;
...
gtod->seq++; seq_begin(gtod->seq); now1 = rdtsc_ordered();
So if the TSC on CPU1 is slightly behind the TSC on CPU0 then now1 can be smaller than cycle_last. The TSC sync stuff does not catch the small delta for unknown raisins. I'll go and find that machine and test that again.
Thanks,
tglx
Re: [patch 09/11] x86/vdso: Simplify the invalid vclock case
In reply to Peter ZijlstraQuoted 5 lines · Peter Zijlstra
Quoted 1 line · Thomas Gleixner
I still have one of the machines which is affected by this.
Are we sure this isn't a load vs rdtsc reorder? Because if I look at the current code:
The load order of last vs. rdtsc does not matter at all.
CPU0 CPU1
....
now0 = rdtsc_ordered();
...tk->cycle_last = now0;
gtod->seq++;gtod->cycle_last = tk->cycle_last;
...
gtod->seq++; seq_begin(gtod->seq); now1 = rdtsc_ordered();
So if the TSC on CPU1 is slightly behind the TSC on CPU0 then now1 can be smaller than cycle_last. The TSC sync stuff does not catch the small delta for unknown raisins. I'll go and find that machine and test that again.
Thanks,
tglx
Re: [patch 09/11] x86/vdso: Simplify the invalid vclock case
Quoted 19 lines · Thomas Gleixner
Quoted 5 lines · Peter Zijlstra
Quoted 1 line · Thomas Gleixner
I still have one of the machines which is affected by this.
Are we sure this isn't a load vs rdtsc reorder? Because if I look at the current code:
The load order of last vs. rdtsc does not matter at all.
CPU0 CPU1
....
now0 =rdtsc_ordered();
...tk->cycle_last= now0;gtod->seq++;
gtod->cycle_last=tk->cycle_last;
...
Quoted 7 lines
gtod->seq++; seq_begin(gtod->seq); now1 = rdtsc_ordered();So if the TSC on CPU1 is slightly behind the TSC on CPU0 then now1 can be smaller than
cycle_last. The TSC sync stuff does not catch the small delta for unknown raisins. I'll go and find that machine and test that again.
Yeah, somehow I forgot about rseq.. maybe I should go sleep or something.
Re: [patch 09/11] x86/vdso: Simplify the invalid vclock case
In reply to Thomas GleixnerQuoted 15 lines · Thomas Gleixner
Quoted 13 lines · John Stultz
Quoted 3 lines · Andy Lutomirski
Also, I'm not entirely convinced that this "last" thing is needed at all. John, what's the scenario under which we need it?
So my memory is probably a bit foggy, but I recall that as we accelerated gettimeofday, we found that even on systems that claimed to have synced TSCs, they were actually just slightly out of sync. Enough that right after
cycles_lasthad been updated, a read on another cpu could come in just behindcycles_last, resulting in a negative interval causing lots of havoc.
Quoted 7 lines
Quoted 1 line
So the sanity check is needed to avoid that case.
Your memory serves you right. That's indeed observable on CPUs which lack
TSC_ADJUST.@Andy: Welcome to the wonderful world of TSC.
Do we do better if we use signed arithmetic for the whole calculation? Then a small backwards movement would result in a small backwards result. Or we could offset everything so that we’d have to go back several hundred ms before we cross zero.
_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
Re: [patch 09/11] x86/vdso: Simplify the invalid vclock case
In reply to Thomas GleixnerQuoted 15 lines · Thomas Gleixner
Quoted 13 lines · John Stultz
Quoted 3 lines · Andy Lutomirski
Also, I'm not entirely convinced that this "last" thing is needed at all. John, what's the scenario under which we need it?
So my memory is probably a bit foggy, but I recall that as we accelerated gettimeofday, we found that even on systems that claimed to have synced TSCs, they were actually just slightly out of sync. Enough that right after
cycles_lasthad been updated, a read on another cpu could come in just behindcycles_last, resulting in a negative interval causing lots of havoc.
Quoted 7 lines
Quoted 1 line
So the sanity check is needed to avoid that case.
Your memory serves you right. That's indeed observable on CPUs which lack
TSC_ADJUST.@Andy: Welcome to the wonderful world of TSC.
Do we do better if we use signed arithmetic for the whole calculation? Then a small backwards movement would result in a small backwards result. Or we could offset everything so that we’d have to go back several hundred ms before we cross zero.
Re: [patch 09/11] x86/vdso: Simplify the invalid vclock case
In reply to Thomas GleixnerQuoted 3 lines · Thomas Gleixner
So if the TSC on CPU1 is slightly behind the TSC on CPU0 then now1 can be smaller than
cycle_last. The TSC sync stuff does not catch the small delta for unknown raisins. I'll go and find that machine and test that again.
Of course it does not trigger anymore. We accumulated code between the point in timekeeping_advance() where the TSC is read and the update of the VDSO data.
I'll might have to get an 2.6ish kernel booted on that machine and try with that again. /me shudders
Thanks,
tglx
Re: [patch 09/11] x86/vdso: Simplify the invalid vclock case
In reply to Thomas GleixnerQuoted 3 lines · Thomas Gleixner
So if the TSC on CPU1 is slightly behind the TSC on CPU0 then now1 can be smaller than
cycle_last. The TSC sync stuff does not catch the small delta for unknown raisins. I'll go and find that machine and test that again.
Of course it does not trigger anymore. We accumulated code between the point in timekeeping_advance() where the TSC is read and the update of the VDSO data.
I'll might have to get an 2.6ish kernel booted on that machine and try with that again. /me shudders
Thanks,
tglx
Re: [patch 09/11] x86/vdso: Simplify the invalid vclock case
In reply to Andy LutomirskiQuoted 19 lines · Andy Lutomirski
Quoted 19 lines · Thomas Gleixner
Quoted 6 lines · John Stultz
Quoted 3 lines · Andy Lutomirski
Also, I'm not entirely convinced that this "last" thing is needed at all. John, what's the scenario under which we need it?
Quoted 8 lines
So my memory is probably a bit foggy, but I recall that as we accelerated gettimeofday, we found that even on systems that claimed to have synced TSCs, they were actually just slightly out of sync. Enough that right after
cycles_lasthad been updated, a read on another cpu could come in just behindcycles_last, resulting in a negative interval causing lots of havoc.So the sanity check is needed to avoid that case.
Your memory serves you right. That's indeed observable on CPUs which lack
TSC_ADJUST.
Quoted 7 lines
Quoted 2 lines
@Andy: Welcome to the wonderful world of TSC.
Do we do better if we use signed arithmetic for the whole calculation? Then a small backwards movement would result in a small backwards result. Or we could offset everything so that we’d have to go back several hundred ms before we cross zero.
That would be probably the better solution as signed math would be problematic when the resulting ns value becomes negative. As the delta is really small, otherwise the TSC sync check would have caught it, the caller should never be able to observe time going backwards.
I'll have a look into that. It needs some thought vs. the fractional part of the base time, but it should be not rocket science to get that correct. Famous last words...
Thanks,
tglx
_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
Re: [patch 09/11] x86/vdso: Simplify the invalid vclock case
In reply to Andy LutomirskiQuoted 19 lines · Andy Lutomirski
Quoted 19 lines · Thomas Gleixner
Quoted 6 lines · John Stultz
Quoted 3 lines · Andy Lutomirski
Also, I'm not entirely convinced that this "last" thing is needed at all. John, what's the scenario under which we need it?
Quoted 8 lines
So my memory is probably a bit foggy, but I recall that as we accelerated gettimeofday, we found that even on systems that claimed to have synced TSCs, they were actually just slightly out of sync. Enough that right after
cycles_lasthad been updated, a read on another cpu could come in just behindcycles_last, resulting in a negative interval causing lots of havoc.So the sanity check is needed to avoid that case.
Your memory serves you right. That's indeed observable on CPUs which lack
TSC_ADJUST.
Quoted 7 lines
Quoted 2 lines
@Andy: Welcome to the wonderful world of TSC.
Do we do better if we use signed arithmetic for the whole calculation? Then a small backwards movement would result in a small backwards result. Or we could offset everything so that we’d have to go back several hundred ms before we cross zero.
That would be probably the better solution as signed math would be problematic when the resulting ns value becomes negative. As the delta is really small, otherwise the TSC sync check would have caught it, the caller should never be able to observe time going backwards.
I'll have a look into that. It needs some thought vs. the fractional part of the base time, but it should be not rocket science to get that correct. Famous last words...
Thanks,
tglx
Re: [patch 09/11] x86/vdso: Simplify the invalid vclock case
Quoted 32 lines · Thomas Gleixner
Quoted 19 lines · Andy Lutomirski
Quoted 19 lines · Thomas Gleixner
Quoted 6 lines
Quoted 5 lines · John Stultz
On Mon, Sep 17, 2018 at 12:25 PM, Andy Lutomirski <[email protected]> wrote:
Also, I'm not entirely convinced that this "last" thing is needed at all. John, what's the scenario under which we need it?Quoted 8 lines
So my memory is probably a bit foggy, but I recall that as we accelerated gettimeofday, we found that even on systems that claimed to have synced TSCs, they were actually just slightly out of sync. Enough that right after
cycles_lasthad been updated, a read on another cpu could come in just behindcycles_last, resulting in a negative interval causing lots of havoc.So the sanity check is needed to avoid that case.
Your memory serves you right. That's indeed observable on CPUs which lack
TSC_ADJUST.Quoted 7 lines
Quoted 2 lines
@Andy: Welcome to the wonderful world of TSC.
Do we do better if we use signed arithmetic for the whole calculation? Then a small backwards movement would result in a small backwards result. Or we could offset everything so that we’d have to go back several hundred ms before we cross zero.
Quoted 9 lines
That would be probably the better solution as signed math would be problematic when the resulting ns value becomes negative. As the delta is really small, otherwise the TSC sync check would have caught it, the caller should never be able to observe time going backwards.
I'll have a look into that. It needs some thought vs. the fractional part of the base time, but it should be not rocket science to get that correct. Famous last words...
It’s also fiddly to tune. If you offset it too much, then the fancy divide-by-repeated-subtraction loop will hurt more than the comparison to last.
_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
Re: [patch 09/11] x86/vdso: Simplify the invalid vclock case
In reply to Thomas GleixnerQuoted 32 lines · Thomas Gleixner
Quoted 19 lines · Andy Lutomirski
Quoted 19 lines · Thomas Gleixner
Quoted 6 lines
Quoted 5 lines · John Stultz
On Mon, Sep 17, 2018 at 12:25 PM, Andy Lutomirski <[email protected]> wrote:
Also, I'm not entirely convinced that this "last" thing is needed at all. John, what's the scenario under which we need it?Quoted 8 lines
So my memory is probably a bit foggy, but I recall that as we accelerated gettimeofday, we found that even on systems that claimed to have synced TSCs, they were actually just slightly out of sync. Enough that right after
cycles_lasthad been updated, a read on another cpu could come in just behindcycles_last, resulting in a negative interval causing lots of havoc.So the sanity check is needed to avoid that case.
Your memory serves you right. That's indeed observable on CPUs which lack
TSC_ADJUST.Quoted 7 lines
Quoted 2 lines
@Andy: Welcome to the wonderful world of TSC.
Do we do better if we use signed arithmetic for the whole calculation? Then a small backwards movement would result in a small backwards result. Or we could offset everything so that we’d have to go back several hundred ms before we cross zero.
Quoted 9 lines
That would be probably the better solution as signed math would be problematic when the resulting ns value becomes negative. As the delta is really small, otherwise the TSC sync check would have caught it, the caller should never be able to observe time going backwards.
I'll have a look into that. It needs some thought vs. the fractional part of the base time, but it should be not rocket science to get that correct. Famous last words...
It’s also fiddly to tune. If you offset it too much, then the fancy divide-by-repeated-subtraction loop will hurt more than the comparison to last.
Re: [patch 09/11] x86/vdso: Simplify the invalid vclock case
Quoted 12 lines · Andy Lutomirski
Quoted 12 lines · Thomas Gleixner
Quoted 4 lines · Andy Lutomirski
Do we do better if we use signed arithmetic for the whole calculation? Then a small backwards movement would result in a small backwards result. Or we could offset everything so that we’d have to go back several hundred ms before we cross zero.
That would be probably the better solution as signed math would be problematic when the resulting ns value becomes negative. As the delta is really small, otherwise the TSC sync check would have caught it, the caller should never be able to observe time going backwards.
Quoted 8 lines
Quoted 4 lines
I'll have a look into that. It needs some thought vs. the fractional part of the base time, but it should be not rocket science to get that correct. Famous last words...
It’s also fiddly to tune. If you offset it too much, then the fancy divide-by-repeated-subtraction loop will hurt more than the comparison to last.
Not really. It's sufficient to offset it by at max. 1000 cycles or so. That won't hurt the magic loop, but it will definitely cover that slight offset case.
Thanks,
tglx
_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
Re: [patch 09/11] x86/vdso: Simplify the invalid vclock case
In reply to Andy LutomirskiQuoted 12 lines · Andy Lutomirski
Quoted 12 lines · Thomas Gleixner
Quoted 4 lines · Andy Lutomirski
Do we do better if we use signed arithmetic for the whole calculation? Then a small backwards movement would result in a small backwards result. Or we could offset everything so that we’d have to go back several hundred ms before we cross zero.
That would be probably the better solution as signed math would be problematic when the resulting ns value becomes negative. As the delta is really small, otherwise the TSC sync check would have caught it, the caller should never be able to observe time going backwards.
Quoted 8 lines
Quoted 4 lines
I'll have a look into that. It needs some thought vs. the fractional part of the base time, but it should be not rocket science to get that correct. Famous last words...
It’s also fiddly to tune. If you offset it too much, then the fancy divide-by-repeated-subtraction loop will hurt more than the comparison to last.
Not really. It's sufficient to offset it by at max. 1000 cycles or so. That won't hurt the magic loop, but it will definitely cover that slight offset case.
Thanks,
tglx
Re: [patch 09/11] x86/vdso: Simplify the invalid vclock case
In reply to Thomas GleixnerQuoted 8 lines · Thomas Gleixner
Quoted 6 lines · Andy Lutomirski
Quoted 1 line
Do we do better if we use signed arithmetic for the whole calculation? Then a small backwards movement would result in a small backwards result. Or we could offset everything so that we’d have to go back several hundred ms before we cross zero.
Quoted 8 lines
That would be probably the better solution as signed math would be problematic when the resulting ns value becomes negative. As the delta is really small, otherwise the TSC sync check would have caught it, the caller should never be able to observe time going backwards.
I'll have a look into that. It needs some thought vs. the fractional part of the base time, but it should be not rocket science to get that correct. Famous last words...
Does the sentinel need to be U64_MAX? What if vgetcyc and its minions returned gtod->cycle_last-1 (for some value of 1), and the caller just does "if ((s64)cycles - (s64)last < 0) return fallback; ns += (cycles-last)* ...". That should just be a "sub ; js ; ". It's an extra load of ->cycle_last, but only on the path where we're heading for the fallback anyway. The value of 1 can be adjusted so that in the "js" path, we could detect and accept an rdtsc_ordered() call that's just a few 10s of cycles behind last and treat that as 0 and continue back on the normal path. But maybe it's hard to get gcc to generate the expected code.
Rasmus
Re: [patch 09/11] x86/vdso: Simplify the invalid vclock case
Quoted 18 lines · Rasmus Villemoes
Quoted 8 lines · Thomas Gleixner
Quoted 6 lines · Andy Lutomirski
Quoted 1 line
Do we do better if we use signed arithmetic for the whole calculation? Then a small backwards movement would result in a small backwards result. Or we could offset everything so that we’d have to go back several hundred ms before we cross zero.
Quoted 8 lines
That would be probably the better solution as signed math would be problematic when the resulting ns value becomes negative. As the delta is really small, otherwise the TSC sync check would have caught it, the caller should never be able to observe time going backwards.
I'll have a look into that. It needs some thought vs. the fractional part of the base time, but it should be not rocket science to get that correct. Famous last words...
Quoted 10 lines
Does the sentinel need to be
U64_MAX? What if vgetcyc and its minions returned gtod->cycle_last-1 (for some value of 1), and the caller just does "if ((s64)cycles - (s64)last < 0) return fallback; ns += (cycles-last)* ...". That should just be a "sub ; js ; ". It's an extra load of ->cycle_last, but only on the path where we're heading for the fallback anyway. The value of 1 can be adjusted so that in the "js" path, we could detect and accept anrdtsc_ordered()call that's just a few 10s of cycles behind last and treat that as 0 and continue back on the normal path. But maybe it's hard to get gcc to generate the expected code.
I played around with a lot of variants and GCC generates all kinds of interesting ASM. And at some point optimizing that math code is not buying anything because the LFENCE before RDTSC is dominating all of it.
Thanks,
tglx
_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
Re: [patch 09/11] x86/vdso: Simplify the invalid vclock case
In reply to Rasmus VillemoesQuoted 18 lines · Rasmus Villemoes
Quoted 8 lines · Thomas Gleixner
Quoted 6 lines · Andy Lutomirski
Quoted 1 line
Do we do better if we use signed arithmetic for the whole calculation? Then a small backwards movement would result in a small backwards result. Or we could offset everything so that we’d have to go back several hundred ms before we cross zero.
Quoted 8 lines
That would be probably the better solution as signed math would be problematic when the resulting ns value becomes negative. As the delta is really small, otherwise the TSC sync check would have caught it, the caller should never be able to observe time going backwards.
I'll have a look into that. It needs some thought vs. the fractional part of the base time, but it should be not rocket science to get that correct. Famous last words...
Quoted 10 lines
Does the sentinel need to be
U64_MAX? What if vgetcyc and its minions returned gtod->cycle_last-1 (for some value of 1), and the caller just does "if ((s64)cycles - (s64)last < 0) return fallback; ns += (cycles-last)* ...". That should just be a "sub ; js ; ". It's an extra load of ->cycle_last, but only on the path where we're heading for the fallback anyway. The value of 1 can be adjusted so that in the "js" path, we could detect and accept anrdtsc_ordered()call that's just a few 10s of cycles behind last and treat that as 0 and continue back on the normal path. But maybe it's hard to get gcc to generate the expected code.
I played around with a lot of variants and GCC generates all kinds of interesting ASM. And at some point optimizing that math code is not buying anything because the LFENCE before RDTSC is dominating all of it.
Thanks,
tglx
Quoted 22 lines · Thomas Gleixner
Quoted 13 lines · Arnd Bergmann
On Fri, Sep 14, 2018 at 2:52 PM Thomas Gleixner <[email protected]> wrote:
A couple of architectures (s390, ia64, riscv, powerpc, arm64) implement the vdso as assembler code at the moment, so they won't be as easy to consolidate (other than outright replacing all the code).The other five:
arch/x86/entry/vdso/vclock_gettime.carch/sparc/vdso/vclock_gettime.carch/nds32/kernel/vdso/gettimeofday.carch/mips/vdso/gettimeofday.carch/arm/vdso/vgettimeofday.cQuoted 6 lines
are basically all minor variations of the same code base and could be consolidated to some degree.
Any suggestions here? Should we plan to do that consolitdation based on your new version, or just addclock_gettime64in arm32 and x86-32, and then be done with it? The other ones will obviously still be fast for 32-bittime_tand will have a working non-vdsosys_clock_getttime64().
Quoted 3 lines
In principle consolidating all those implementations should be possible to some extent and probably worthwhile. What's arch specific are the actual accessors to the hardware clocks.
Ok.
Quoted 6 lines
Quoted 3 lines
I also wonder about
clock_getres(): half the architectures seem to implement it in vdso, but notably arm32 and x86 don't, and I had not expected it to be performance critical given that the result is easily cached in user space.
getres()is not really performance critical, but adding it does not create a huge problem either.
Right, I'd just not add a getres_time64() to the vdso then.
Arnd
Quoted 22 lines · Thomas Gleixner
Quoted 13 lines · Arnd Bergmann
On Fri, Sep 14, 2018 at 2:52 PM Thomas Gleixner <[email protected]> wrote:
A couple of architectures (s390, ia64, riscv, powerpc, arm64) implement the vdso as assembler code at the moment, so they won't be as easy to consolidate (other than outright replacing all the code).The other five:
arch/x86/entry/vdso/vclock_gettime.carch/sparc/vdso/vclock_gettime.carch/nds32/kernel/vdso/gettimeofday.carch/mips/vdso/gettimeofday.carch/arm/vdso/vgettimeofday.cQuoted 6 lines
are basically all minor variations of the same code base and could be consolidated to some degree.
Any suggestions here? Should we plan to do that consolitdation based on your new version, or just addclock_gettime64in arm32 and x86-32, and then be done with it? The other ones will obviously still be fast for 32-bittime_tand will have a working non-vdsosys_clock_getttime64().
Quoted 3 lines
In principle consolidating all those implementations should be possible to some extent and probably worthwhile. What's arch specific are the actual accessors to the hardware clocks.
Ok.
Quoted 6 lines
Quoted 3 lines
I also wonder about
clock_getres(): half the architectures seem to implement it in vdso, but notably arm32 and x86 don't, and I had not expected it to be performance critical given that the result is easily cached in user space.
getres()is not really performance critical, but adding it does not create a huge problem either.
Right, I'd just not add a getres_time64() to the vdso then.
Arnd
Re: [patch 09/11] x86/vdso: Simplify the invalid vclock case
In reply to Thomas GleixnerQuoted 18 lines · Thomas Gleixner
Quoted 17 lines · Andy Lutomirski
Quoted 16 lines · Thomas Gleixner
Quoted 4 lines · Andy Lutomirski
Do we do better if we use signed arithmetic for the whole calculation? Then a small backwards movement would result in a small backwards result. Or we could offset everything so that we’d have to go back several hundred ms before we cross zero.
That would be probably the better solution as signed math would be problematic when the resulting ns value becomes negative. As the delta is really small, otherwise the TSC sync check would have caught it, the caller should never be able to observe time going backwards.
I'll have a look into that. It needs some thought vs. the fractional part of the base time, but it should be not rocket science to get that correct. Famous last words...
Quoted 7 lines
Quoted 3 lines
It’s also fiddly to tune. If you offset it too much, then the fancy divide-by-repeated-subtraction loop will hurt more than the comparison to last.
Not really. It's sufficient to offset it by at max. 1000 cycles or so. That won't hurt the magic loop, but it will definitely cover that slight offset case.
I got it working, but first of all the gain is close to 0.
There is this other subtle issue that we've seen TSCs slowly drifting apart which is caught by the TSC watchdog eventually, but if it exeeds the offset _before_ the watchdog triggers, we're back to square one.
So I rather stay on the safe side and just accept that we have to deal with that. Sigh.
Thanks,
tglx
_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
Re: [patch 09/11] x86/vdso: Simplify the invalid vclock case
In reply to Thomas GleixnerQuoted 18 lines · Thomas Gleixner
Quoted 17 lines · Andy Lutomirski
Quoted 16 lines · Thomas Gleixner
Quoted 4 lines · Andy Lutomirski
Do we do better if we use signed arithmetic for the whole calculation? Then a small backwards movement would result in a small backwards result. Or we could offset everything so that we’d have to go back several hundred ms before we cross zero.
That would be probably the better solution as signed math would be problematic when the resulting ns value becomes negative. As the delta is really small, otherwise the TSC sync check would have caught it, the caller should never be able to observe time going backwards.
I'll have a look into that. It needs some thought vs. the fractional part of the base time, but it should be not rocket science to get that correct. Famous last words...
Quoted 7 lines
Quoted 3 lines
It’s also fiddly to tune. If you offset it too much, then the fancy divide-by-repeated-subtraction loop will hurt more than the comparison to last.
Not really. It's sufficient to offset it by at max. 1000 cycles or so. That won't hurt the magic loop, but it will definitely cover that slight offset case.
I got it working, but first of all the gain is close to 0.
There is this other subtle issue that we've seen TSCs slowly drifting apart which is caught by the TSC watchdog eventually, but if it exeeds the offset _before_ the watchdog triggers, we're back to square one.
So I rather stay on the safe side and just accept that we have to deal with that. Sigh.
Thanks,
tglx
Re: [patch 09/11] x86/vdso: Simplify the invalid vclock case
Quoted 31 lines · Thomas Gleixner
Quoted 20 lines · Thomas Gleixner
Quoted 17 lines · Andy Lutomirski
Quoted 16 lines · Thomas Gleixner
Quoted 5 lines · Andy Lutomirski
Do we do better if we use signed arithmetic for the whole calculation? Then a small backwards movement would result in a small backwards result. Or we could offset everything so that we’d have to go back several hundred ms before we cross zero.
That would be probably the better solution as signed math would be problematic when the resulting ns value becomes negative. As the delta is really small, otherwise the TSC sync check would have caught it, the caller should never be able to observe time going backwards.
I'll have a look into that. It needs some thought vs. the fractional part of the base time, but it should be not rocket science to get that correct. Famous last words...
Quoted 7 lines
Quoted 3 lines
It’s also fiddly to tune. If you offset it too much, then the fancy divide-by-repeated-subtraction loop will hurt more than the comparison to last.
Not really. It's sufficient to offset it by at max. 1000 cycles or so. That won't hurt the magic loop, but it will definitely cover that slight offset case.
Quoted 10 lines
I got it working, but first of all the gain is close to 0.
There is this other subtle issue that we've seen TSCs slowly drifting apart which is caught by the TSC watchdog eventually, but if it exeeds the offset _before_ the watchdog triggers, we're back to square one.
So I rather stay on the safe side and just accept that we have to deal with that. Sigh.
Seems okay to me. Oh well.
_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
Re: [patch 09/11] x86/vdso: Simplify the invalid vclock case
In reply to Thomas GleixnerQuoted 31 lines · Thomas Gleixner
Quoted 20 lines · Thomas Gleixner
Quoted 17 lines · Andy Lutomirski
Quoted 16 lines · Thomas Gleixner
Quoted 5 lines · Andy Lutomirski
Do we do better if we use signed arithmetic for the whole calculation? Then a small backwards movement would result in a small backwards result. Or we could offset everything so that we’d have to go back several hundred ms before we cross zero.
That would be probably the better solution as signed math would be problematic when the resulting ns value becomes negative. As the delta is really small, otherwise the TSC sync check would have caught it, the caller should never be able to observe time going backwards.
I'll have a look into that. It needs some thought vs. the fractional part of the base time, but it should be not rocket science to get that correct. Famous last words...
Quoted 7 lines
Quoted 3 lines
It’s also fiddly to tune. If you offset it too much, then the fancy divide-by-repeated-subtraction loop will hurt more than the comparison to last.
Not really. It's sufficient to offset it by at max. 1000 cycles or so. That won't hurt the magic loop, but it will definitely cover that slight offset case.
Quoted 10 lines
I got it working, but first of all the gain is close to 0.
There is this other subtle issue that we've seen TSCs slowly drifting apart which is caught by the TSC watchdog eventually, but if it exeeds the offset _before_ the watchdog triggers, we're back to square one.
So I rather stay on the safe side and just accept that we have to deal with that. Sigh.
Seems okay to me. Oh well.
Re: [patch 09/11] x86/vdso: Simplify the invalid vclock case
In reply to Thomas GleixnerQuoted 11 lines · Thomas Gleixner
Quoted 3 lines · Thomas Gleixner
So if the TSC on CPU1 is slightly behind the TSC on CPU0 then now1 can be smaller than
cycle_last. The TSC sync stuff does not catch the small delta for unknown raisins. I'll go and find that machine and test that again.Of course it does not trigger anymore. We accumulated code between the point in
timekeeping_advance()where the TSC is read and the update of the VDSO data.I'll might have to get an 2.6ish kernel booted on that machine and try with that again. /me shudders
Actually it does happen, because the TSC is very slowly drifting apart due to SMI wreckage trying to hide itself. It just takes a very long time.
Thanks,
tglx
Re: [patch 09/11] x86/vdso: Simplify the invalid vclock case
In reply to Thomas GleixnerQuoted 11 lines · Thomas Gleixner
Quoted 3 lines · Thomas Gleixner
So if the TSC on CPU1 is slightly behind the TSC on CPU0 then now1 can be smaller than
cycle_last. The TSC sync stuff does not catch the small delta for unknown raisins. I'll go and find that machine and test that again.Of course it does not trigger anymore. We accumulated code between the point in
timekeeping_advance()where the TSC is read and the update of the VDSO data.I'll might have to get an 2.6ish kernel booted on that machine and try with that again. /me shudders
Actually it does happen, because the TSC is very slowly drifting apart due to SMI wreckage trying to hide itself. It just takes a very long time.
Thanks,
tglx
Andy Lutomirski
maintainerHi Vitaly, Paolo, Radim, etc.,
Quoted 5 lines · Thomas Gleixner
Matt attempted to add
CLOCK_TAIsupport to the VDSOclock_gettime()implementation, which extended the clockid switch case and added yet another slightly different copy of the same code.
Quoted 9 lines
Especially the extended switch case is problematic as the compiler tends to generate a jump table which then requires to use retpolines. If jump tables are disabled it adds yet another conditional to the existing maze.
This series takes a different approach by consolidating the almost identical functions into one implementation for high resolution clocks and one for the coarse grained clock ids by storing the base data for each clock id in an array which is indexed by the clock id.
I was trying to understand more of the implications of this patch series, and I was again reminded that there is an entire extra copy of the vclock reading code in arch/x86/kvm/x86.c. And the purpose of that code is very, very opaque.
Can one of you explain what the code is even doing? From a couple of attempts to read through it, it's a whole bunch of probably-extremely-buggy code that, drumroll please, tries to atomically read the TSC value and the time. And decide whether the result is "based on the TSC". And then synthesizes a TSC-to-ns multiplier and shift, based on *something other than the actual multiply and shift used*.
IOW, unless I'm totally misunderstanding it, the code digs into the private arch clocksource data intended for the vDSO, uses a poorly maintained copy of the vDSO code to read the time (instead of doing the sane thing and using the kernel interfaces for this), and propagates a totally made up copy to the guest. And gets it entirely wrong when doing nested virt, since, unless there's some secret in this maze, it doesn't acutlaly use the scaling factor from the host when it tells the guest what to do.
I am really, seriously tempted to send a patch to simply delete all this code. The correct way to do it is to hook
And I don't see how it's even possible to pass kvmclock correctly to the L2 guest when L0 is hyperv. KVM could pass *hyperv's* clock, but L1 isn't notified when the data structure changes, so how the heck is it supposed to update the kvmclock structure?
--Andy
Quoted 40 lines · Andy Lutomirski
Hi Vitaly, Paolo, Radim, etc.,
Quoted 5 lines · Thomas Gleixner
Matt attempted to add
CLOCK_TAIsupport to the VDSOclock_gettime()implementation, which extended the clockid switch case and added yet another slightly different copy of the same code.Quoted 9 lines
Especially the extended switch case is problematic as the compiler tends to generate a jump table which then requires to use retpolines. If jump tables are disabled it adds yet another conditional to the existing maze.
This series takes a different approach by consolidating the almost identical functions into one implementation for high resolution clocks and one for the coarse grained clock ids by storing the base data for each clock id in an array which is indexed by the clock id.
I was trying to understand more of the implications of this patch series, and I was again reminded that there is an entire extra copy of the vclock reading code in
arch/x86/kvm/x86.c. And the purpose of that code is very, very opaque.Can one of you explain what the code is even doing? From a couple of attempts to read through it, it's a whole bunch of probably-extremely-buggy code that, drumroll please, tries to atomically read the TSC value and the time. And decide whether the result is "based on the TSC". And then synthesizes a TSC-to-ns multiplier and shift, based on *something other than the actual multiply and shift used*.
IOW, unless I'm totally misunderstanding it, the code digs into the private arch clocksource data intended for the vDSO, uses a poorly maintained copy of the vDSO code to read the time (instead of doing the sane thing and using the kernel interfaces for this), and propagates a totally made up copy to the guest. And gets it entirely wrong when doing nested virt, since, unless there's some secret in this maze, it doesn't acutlaly use the scaling factor from the host when it tells the guest what to do.
Quoted 2 lines
I am really, seriously tempted to send a patch to simply delete all this code. The correct way to do it is to hook
"I have discovered a truly marvelous proof of this, which this margin is too narrow to contain" :-)
There is a very long history of different (hardware) issues Marcelo was fighting with and the current code is the survived Frankenstein. E.g. it is very, very unclear what "catchup", "always catchup" and masterclock-less mode in general are and if we still need them.
That said I'm all for simplification. I'm not sure if we still need to care about buggy hardware though.
Quoted 5 lines
And I don't see how it's even possible to pass kvmclock correctly to the L2 guest when L0 is hyperv. KVM could pass *hyperv's* clock, but L1 isn't notified when the data structure changes, so how the heck is it supposed to update the kvmclock structure?
Well, this kind of works in the the followin way:
L1's clocksource is 'tsc_page' which is, basically, a compliment to TSC:
two numbers provided by L0: offset and scale and KVM was tought to treat this clocksource as a good one (see b0c39dc68e3b "x86/kvm: Pass stable clocksource to guests when running nested on Hyper-V").
The notification you're talking about exists, it is called Reenligntenment, see 0092e4346f49 "x86/kvm: Support Hyper-V reenlightenment"). When TSC page changes (and this only happens when L1 is migrated to a different host with a different TSC frequency and TSC scaling is not supported by the CPU) we receive an interrupt in L1 (at this moment all TSC accesses are emulated which guarantees the correctness of the readings), pause all L2 guests, update their kvmclock structures with new data (we already know the new TSC frequency) and then tell L0 that we're done and it can stop emulating TSC accesses.
(Nothing like this exists for KVM-on-KVM, by the way, when L1's clocksource is 'kvmclock' L2s won't get a stable kvmclock clocksource.)
Signature · 1 line
Vitaly
Quoted 40 lines · Andy Lutomirski
Hi Vitaly, Paolo, Radim, etc.,
Quoted 5 lines · Thomas Gleixner
Matt attempted to add
CLOCK_TAIsupport to the VDSOclock_gettime()implementation, which extended the clockid switch case and added yet another slightly different copy of the same code.Quoted 9 lines
Especially the extended switch case is problematic as the compiler tends to generate a jump table which then requires to use retpolines. If jump tables are disabled it adds yet another conditional to the existing maze.
This series takes a different approach by consolidating the almost identical functions into one implementation for high resolution clocks and one for the coarse grained clock ids by storing the base data for each clock id in an array which is indexed by the clock id.
I was trying to understand more of the implications of this patch series, and I was again reminded that there is an entire extra copy of the vclock reading code in
arch/x86/kvm/x86.c. And the purpose of that code is very, very opaque.Can one of you explain what the code is even doing? From a couple of attempts to read through it, it's a whole bunch of probably-extremely-buggy code that, drumroll please, tries to atomically read the TSC value and the time. And decide whether the result is "based on the TSC". And then synthesizes a TSC-to-ns multiplier and shift, based on *something other than the actual multiply and shift used*.
IOW, unless I'm totally misunderstanding it, the code digs into the private arch clocksource data intended for the vDSO, uses a poorly maintained copy of the vDSO code to read the time (instead of doing the sane thing and using the kernel interfaces for this), and propagates a totally made up copy to the guest. And gets it entirely wrong when doing nested virt, since, unless there's some secret in this maze, it doesn't acutlaly use the scaling factor from the host when it tells the guest what to do.
Quoted 2 lines
I am really, seriously tempted to send a patch to simply delete all this code. The correct way to do it is to hook
"I have discovered a truly marvelous proof of this, which this margin is too narrow to contain" :-)
There is a very long history of different (hardware) issues Marcelo was fighting with and the current code is the survived Frankenstein. E.g. it is very, very unclear what "catchup", "always catchup" and masterclock-less mode in general are and if we still need them.
That said I'm all for simplification. I'm not sure if we still need to care about buggy hardware though.
Quoted 5 lines
And I don't see how it's even possible to pass kvmclock correctly to the L2 guest when L0 is hyperv. KVM could pass *hyperv's* clock, but L1 isn't notified when the data structure changes, so how the heck is it supposed to update the kvmclock structure?
Well, this kind of works in the the followin way:
L1's clocksource is 'tsc_page' which is, basically, a compliment to TSC:
two numbers provided by L0: offset and scale and KVM was tought to treat this clocksource as a good one (see b0c39dc68e3b "x86/kvm: Pass stable clocksource to guests when running nested on Hyper-V").
The notification you're talking about exists, it is called Reenligntenment, see 0092e4346f49 "x86/kvm: Support Hyper-V reenlightenment"). When TSC page changes (and this only happens when L1 is migrated to a different host with a different TSC frequency and TSC scaling is not supported by the CPU) we receive an interrupt in L1 (at this moment all TSC accesses are emulated which guarantees the correctness of the readings), pause all L2 guests, update their kvmclock structures with new data (we already know the new TSC frequency) and then tell L0 that we're done and it can stop emulating TSC accesses.
(Nothing like this exists for KVM-on-KVM, by the way, when L1's clocksource is 'kvmclock' L2s won't get a stable kvmclock clocksource.)
Signature · 1 line
Vitaly
Quoted 72 lines · Vitaly Kuznetsov
Quoted 40 lines · Andy Lutomirski
Hi Vitaly, Paolo, Radim, etc.,
Quoted 6 lines · Thomas Gleixner
Matt attempted to add
CLOCK_TAIsupport to the VDSOclock_gettime()implementation, which extended the clockid switch case and added yet another slightly different copy of the same code.Quoted 9 lines
Especially the extended switch case is problematic as the compiler tends to generate a jump table which then requires to use retpolines. If jump tables are disabled it adds yet another conditional to the existing maze.
This series takes a different approach by consolidating the almost identical functions into one implementation for high resolution clocks and one for the coarse grained clock ids by storing the base data for each clock id in an array which is indexed by the clock id.
I was trying to understand more of the implications of this patch series, and I was again reminded that there is an entire extra copy of the vclock reading code in
arch/x86/kvm/x86.c. And the purpose of that code is very, very opaque.Can one of you explain what the code is even doing? From a couple of attempts to read through it, it's a whole bunch of probably-extremely-buggy code that, drumroll please, tries to atomically read the TSC value and the time. And decide whether the result is "based on the TSC". And then synthesizes a TSC-to-ns multiplier and shift, based on *something other than the actual multiply and shift used*.
IOW, unless I'm totally misunderstanding it, the code digs into the private arch clocksource data intended for the vDSO, uses a poorly maintained copy of the vDSO code to read the time (instead of doing the sane thing and using the kernel interfaces for this), and propagates a totally made up copy to the guest. And gets it entirely wrong when doing nested virt, since, unless there's some secret in this maze, it doesn't acutlaly use the scaling factor from the host when it tells the guest what to do.
Quoted 2 lines
I am really, seriously tempted to send a patch to simply delete all this code. The correct way to do it is to hook
"I have discovered a truly marvelous proof of this, which this margin is too narrow to contain" :-)
There is a very long history of different (hardware) issues Marcelo was fighting with and the current code is the survived Frankenstein. E.g. it is very, very unclear what "catchup", "always catchup" and masterclock-less mode in general are and if we still need them.
That said I'm all for simplification. I'm not sure if we still need to care about buggy hardware though.
Quoted 5 lines
And I don't see how it's even possible to pass kvmclock correctly to the L2 guest when L0 is hyperv. KVM could pass *hyperv's* clock, but L1 isn't notified when the data structure changes, so how the heck is it supposed to update the kvmclock structure?
Well, this kind of works in the the followin way:
L1's clocksource is 'tsc_page' which is, basically, a compliment to TSC:
two numbers provided by L0: offset and scale and KVM was tought to treat this clocksource as a good one (seeb0c39dc68e3b"x86/kvm: Pass stable clocksource to guests when running nested on Hyper-V").
Quoted 9 lines
The notification you're talking about exists, it is called Reenligntenment, see
0092e4346f49"x86/kvm: Support Hyper-V reenlightenment"). When TSC page changes (and this only happens when L1 is migrated to a different host with a different TSC frequency and TSC scaling is not supported by the CPU) we receive an interrupt in L1 (at this moment all TSC accesses are emulated which guarantees the correctness of the readings), pause all L2 guests, update their kvmclock structures with new data (we already know the new TSC frequency) and then tell L0 that we're done and it can stop emulating TSC accesses.
That’s delightful! Does the emulation magic also work for L1 user mode? If so, couldn’t we drop the HyperV vclock entirely and just fold the adjustment into the core timekeeping data? (Preferably the actual core data, which would require core changes, but it could plausibly be done in arch code, too.)
Quoted 5 lines
(Nothing like this exists for KVM-on-KVM, by the way, when L1's clocksource is 'kvmclock' L2s won't get a stable kvmclock clocksource.)
_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
Quoted 16 lines · Andy Lutomirski
Quoted 6 lines · Vitaly Kuznetsov
Quoted 2 lines · Andy Lutomirski
Hi Vitaly, Paolo, Radim, etc.,
Quoted 9 lines
The notification you're talking about exists, it is called Reenligntenment, see
0092e4346f49"x86/kvm: Support Hyper-V reenlightenment"). When TSC page changes (and this only happens when L1 is migrated to a different host with a different TSC frequency and TSC scaling is not supported by the CPU) we receive an interrupt in L1 (at this moment all TSC accesses are emulated which guarantees the correctness of the readings), pause all L2 guests, update their kvmclock structures with new data (we already know the new TSC frequency) and then tell L0 that we're done and it can stop emulating TSC accesses.
Quoted 2 lines
That’s delightful! Does the emulation magic also work for L1 user mode?
As far as I understand - yes, all rdtsc* calls will trap into L0.
Quoted 4 lines
If so, couldn’t we drop the HyperV vclock entirely and just
fold the adjustment into the core timekeeping data? (Preferably the actual core data, which would require core changes, but it could plausibly be done in arch code, too.)
Not all Hyper-V hosts support reenlightenment notifications (and, if I'm not mistaken, you need to enable nesting for the VM to get the feature - and most VMs don't have this) so I think we'll have to keep Hyper-V vclock for the time being.
Signature · 5 lines
Vitaly _______________________________________________ Virtualization mailing list [email protected] https://lists.linuxfoundation.org/mailman/listinfo/virtualization
Quoted 16 lines · Andy Lutomirski
Quoted 6 lines · Vitaly Kuznetsov
Quoted 2 lines · Andy Lutomirski
Hi Vitaly, Paolo, Radim, etc.,
Quoted 9 lines
The notification you're talking about exists, it is called Reenligntenment, see
0092e4346f49"x86/kvm: Support Hyper-V reenlightenment"). When TSC page changes (and this only happens when L1 is migrated to a different host with a different TSC frequency and TSC scaling is not supported by the CPU) we receive an interrupt in L1 (at this moment all TSC accesses are emulated which guarantees the correctness of the readings), pause all L2 guests, update their kvmclock structures with new data (we already know the new TSC frequency) and then tell L0 that we're done and it can stop emulating TSC accesses.
Quoted 2 lines
That’s delightful! Does the emulation magic also work for L1 user mode?
As far as I understand - yes, all rdtsc* calls will trap into L0.
Quoted 4 lines
If so, couldn’t we drop the HyperV vclock entirely and just
fold the adjustment into the core timekeeping data? (Preferably the actual core data, which would require core changes, but it could plausibly be done in arch code, too.)
Not all Hyper-V hosts support reenlightenment notifications (and, if I'm not mistaken, you need to enable nesting for the VM to get the feature - and most VMs don't have this) so I think we'll have to keep Hyper-V vclock for the time being.
Signature · 1 line
Vitaly
Quoted 32 lines · Vitaly Kuznetsov
Quoted 16 lines · Andy Lutomirski
Quoted 6 lines · Vitaly Kuznetsov
Quoted 2 lines · Andy Lutomirski
Hi Vitaly, Paolo, Radim, etc.,
Quoted 9 lines
The notification you're talking about exists, it is called Reenligntenment, see
0092e4346f49"x86/kvm: Support Hyper-V reenlightenment"). When TSC page changes (and this only happens when L1 is migrated to a different host with a different TSC frequency and TSC scaling is not supported by the CPU) we receive an interrupt in L1 (at this moment all TSC accesses are emulated which guarantees the correctness of the readings), pause all L2 guests, update their kvmclock structures with new data (we already know the new TSC frequency) and then tell L0 that we're done and it can stop emulating TSC accesses.Quoted 2 lines
That’s delightful! Does the emulation magic also work for L1 user mode?
As far as I understand - yes, all rdtsc* calls will trap into L0.
Quoted 4 lines
If so, couldn’t we drop the HyperV vclock entirely and just fold the adjustment into the core timekeeping data? (Preferably the actual core data, which would require core changes, but it could plausibly be done in arch code, too.)
Quoted 6 lines
Not all Hyper-V hosts support reenlightenment notifications (and, if I'm not mistaken, you need to enable nesting for the VM to get the feature - and most VMs don't have this) so I think we'll have to keep Hyper-V vclock for the time being.
But this does suggest that the correct way to pass a clock through to an L2 guest where L0 is HV is to make L1 use the “tsc” clock and L2 use kvmclock (or something newer and better). This would require adding support for atomic frequency changes all the way through the timekeeping and arch code.
John, tglx, would that be okay or crazy?
_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
Quoted 32 lines · Vitaly Kuznetsov
Quoted 16 lines · Andy Lutomirski
Quoted 6 lines · Vitaly Kuznetsov
Quoted 2 lines · Andy Lutomirski
Hi Vitaly, Paolo, Radim, etc.,
Quoted 9 lines
The notification you're talking about exists, it is called Reenligntenment, see
0092e4346f49"x86/kvm: Support Hyper-V reenlightenment"). When TSC page changes (and this only happens when L1 is migrated to a different host with a different TSC frequency and TSC scaling is not supported by the CPU) we receive an interrupt in L1 (at this moment all TSC accesses are emulated which guarantees the correctness of the readings), pause all L2 guests, update their kvmclock structures with new data (we already know the new TSC frequency) and then tell L0 that we're done and it can stop emulating TSC accesses.Quoted 2 lines
That’s delightful! Does the emulation magic also work for L1 user mode?
As far as I understand - yes, all rdtsc* calls will trap into L0.
Quoted 4 lines
If so, couldn’t we drop the HyperV vclock entirely and just fold the adjustment into the core timekeeping data? (Preferably the actual core data, which would require core changes, but it could plausibly be done in arch code, too.)
Quoted 6 lines
Not all Hyper-V hosts support reenlightenment notifications (and, if I'm not mistaken, you need to enable nesting for the VM to get the feature - and most VMs don't have this) so I think we'll have to keep Hyper-V vclock for the time being.
But this does suggest that the correct way to pass a clock through to an L2 guest where L0 is HV is to make L1 use the “tsc” clock and L2 use kvmclock (or something newer and better). This would require adding support for atomic frequency changes all the way through the timekeeping and arch code.
John, tglx, would that be okay or crazy?
Quoted 6 lines · Andy Lutomirski
Quoted 6 lines · Vitaly Kuznetsov
Not all Hyper-V hosts support reenlightenment notifications (and, if I'm not mistaken, you need to enable nesting for the VM to get the feature - and most VMs don't have this) so I think we'll have to keep Hyper-V vclock for the time being.
Quoted 7 lines
But this does suggest that the correct way to pass a clock through to an L2 guest where L0 is HV is to make L1 use the “tsc” clock and L2 use kvmclock (or something newer and better). This would require adding support for atomic frequency changes all the way through the timekeeping and arch code.
John, tglx, would that be okay or crazy?
Not sure what you mean. I think I lost you somewhere on the way.
Thanks,
tglx
_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
Quoted 6 lines · Andy Lutomirski
Quoted 6 lines · Vitaly Kuznetsov
Not all Hyper-V hosts support reenlightenment notifications (and, if I'm not mistaken, you need to enable nesting for the VM to get the feature - and most VMs don't have this) so I think we'll have to keep Hyper-V vclock for the time being.
Quoted 7 lines
But this does suggest that the correct way to pass a clock through to an L2 guest where L0 is HV is to make L1 use the “tsc” clock and L2 use kvmclock (or something newer and better). This would require adding support for atomic frequency changes all the way through the timekeeping and arch code.
John, tglx, would that be okay or crazy?
Not sure what you mean. I think I lost you somewhere on the way.
Thanks,
tglx
Quoted 14 lines · Thomas Gleixner
Quoted 12 lines · Andy Lutomirski
Quoted 6 lines · Vitaly Kuznetsov
Not all Hyper-V hosts support reenlightenment notifications (and, if I'm not mistaken, you need to enable nesting for the VM to get the feature - and most VMs don't have this) so I think we'll have to keep Hyper-V vclock for the time being.
But this does suggest that the correct way to pass a clock through to an L2 guest where L0 is HV is to make L1 use the “tsc” clock and L2 use kvmclock (or something newer and better). This would require adding support for atomic frequency changes all the way through the timekeeping and arch code.
Quoted 4 lines
Quoted 1 line
John, tglx, would that be okay or crazy?
Not sure what you mean. I think I lost you somewhere on the way.
What I mean is: currently we have a clocksource called ""hyperv_clocksource_tsc_page". Reading it does:
static u64 read_hv_clock_tsc(struct clocksource *arg)
{
u64 current_tick = hv_read_tsc_page(tsc_pg);
if (current_tick == U64_MAX)
rdmsrl(HV_X64_MSR_TIME_REF_COUNT, current_tick);
return current_tick;
}From Vitaly's email, it sounds like, on most (all?) hyperv systems with nesting enabled, this clock is better behaved than it appears. It sounds like the read behavior is that current_tick will never be U64_MAX -- instead, the clock always works and, more importantly, the actual scaling factor and offset only change observably on *guest* request.
So why don't we we improve the actual "tsc" clocksource to understand this? ISTM the best model would be where the __clocksource_update_freq_xyz() mechanism gets called so we can use it like this:
clocksource_begin_update();clocksource_update_mult_shift();tell_hv_that_we_reenlightened();clocksource_end_update();
Where clocksource_begin_update() bumps the seqcount for the vDSO and takes all the locks, clocksource_update_mult_shift() updates everything, and clocksource_end_update() makes the updated parameters usable.
(AFAICT there are currently no clocksources at all in the entire kernel that update their frequency on the fly using __clocksource_update_xyz(). Unless I'm missing something, the x86 tsc cpufreq hooks don't call into the core timekeeping at all, so I'm assuming that the tsc clocksource is just unusable as a clocksource on systems that change its frequency.)
Or we could keep the hyperv_clocksource_tsc_page clocksource but make it use VCLOCK_TSC and a similar update mechanism.
I don't personally want to do this, because the timekeeping code is subtle and I'm unfamiliar with it. And I don't have *that* many spare cycles :)
_______________________________________________
Virtualization mailing list [email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
Quoted 14 lines · Thomas Gleixner
Quoted 12 lines · Andy Lutomirski
Quoted 6 lines · Vitaly Kuznetsov
Not all Hyper-V hosts support reenlightenment notifications (and, if I'm not mistaken, you need to enable nesting for the VM to get the feature - and most VMs don't have this) so I think we'll have to keep Hyper-V vclock for the time being.
But this does suggest that the correct way to pass a clock through to an L2 guest where L0 is HV is to make L1 use the “tsc” clock and L2 use kvmclock (or something newer and better). This would require adding support for atomic frequency changes all the way through the timekeeping and arch code.
Quoted 4 lines
Quoted 1 line
John, tglx, would that be okay or crazy?
Not sure what you mean. I think I lost you somewhere on the way.
What I mean is: currently we have a clocksource called ""hyperv_clocksource_tsc_page". Reading it does:
static u64 read_hv_clock_tsc(struct clocksource *arg)
{
u64 current_tick = hv_read_tsc_page(tsc_pg);
if (current_tick == U64_MAX)
rdmsrl(HV_X64_MSR_TIME_REF_COUNT, current_tick);
return current_tick;
}From Vitaly's email, it sounds like, on most (all?) hyperv systems with nesting enabled, this clock is better behaved than it appears. It sounds like the read behavior is that current_tick will never be U64_MAX -- instead, the clock always works and, more importantly, the actual scaling factor and offset only change observably on *guest* request.
So why don't we we improve the actual "tsc" clocksource to understand this? ISTM the best model would be where the __clocksource_update_freq_xyz() mechanism gets called so we can use it like this:
clocksource_begin_update();clocksource_update_mult_shift();tell_hv_that_we_reenlightened();clocksource_end_update();
Where clocksource_begin_update() bumps the seqcount for the vDSO and takes all the locks, clocksource_update_mult_shift() updates everything, and clocksource_end_update() makes the updated parameters usable.
(AFAICT there are currently no clocksources at all in the entire kernel that update their frequency on the fly using __clocksource_update_xyz(). Unless I'm missing something, the x86 tsc cpufreq hooks don't call into the core timekeeping at all, so I'm assuming that the tsc clocksource is just unusable as a clocksource on systems that change its frequency.)
Or we could keep the hyperv_clocksource_tsc_page clocksource but make it use VCLOCK_TSC and a similar update mechanism.
I don't personally want to do this, because the timekeeping code is subtle and I'm unfamiliar with it. And I don't have *that* many spare cycles :)
Quoted 18 lines · Andy Lutomirski
Hi Vitaly, Paolo, Radim, etc.,
Quoted 5 lines · Thomas Gleixner
Matt attempted to add
CLOCK_TAIsupport to the VDSOclock_gettime()implementation, which extended the clockid switch case and added yet another slightly different copy of the same code.Quoted 9 lines
Especially the extended switch case is problematic as the compiler tends to generate a jump table which then requires to use retpolines. If jump tables are disabled it adds yet another conditional to the existing maze.
This series takes a different approach by consolidating the almost identical functions into one implementation for high resolution clocks and one for the coarse grained clock ids by storing the base data for each clock id in an array which is indexed by the clock id.
Quoted 8 lines
I was trying to understand more of the implications of this patch series, and I was again reminded that there is an entire extra copy of the vclock reading code in
arch/x86/kvm/x86.c. And the purpose of that code is very, very opaque.Can one of you explain what the code is even doing? From a couple of attempts to read through it, it's a whole bunch of probably-extremely-buggy code that,
Yes, probably.
Quoted 2 lines
drumroll please, tries to atomically read the TSC value and the time. And decide whether the result is "based on the TSC".
I think "based on the TSC" refers to whether TSC clocksource is being used.
Quoted 9 lines
And then synthesizes a TSC-to-ns
multiplier and shift, based on *something other than the actual multiply and shift used*.IOW, unless I'm totally misunderstanding it, the code digs into the private arch clocksource data intended for the vDSO, uses a poorly maintained copy of the vDSO code to read the time (instead of doing the sane thing and using the kernel interfaces for this), and propagates a totally made up copy to the guest.
I posted kernel interfaces for this, and it was suggested to instead write a "in-kernel user of pvclock data".
If you can get kernel interfaces to replace that, go for it. I prefer kernel interfaces as well.
Quoted 7 lines
And gets it entirely
wrong when doing nested virt, since, unless there's some secret in this maze, it doesn't acutlaly use the scaling factor from the host when it tells the guest what to do.I am really, seriously tempted to send a patch to simply delete all this code.
If your patch which deletes the code gets the necessary features right, sure, go for it.
Quoted 1 line
The correct way to do it is to hook
Can you expand on the correct way to do it?
Quoted 4 lines
And I don't see how it's even possible to pass kvmclock correctly to the L2 guest when L0 is hyperv. KVM could pass *hyperv's* clock, but L1 isn't notified when the data structure changes, so how the heck is it supposed to update the kvmclock structure?
I don't parse your question.
Quoted 2 lines
--Andy
Quoted 18 lines · Andy Lutomirski
Hi Vitaly, Paolo, Radim, etc.,
Quoted 5 lines · Thomas Gleixner
Matt attempted to add
CLOCK_TAIsupport to the VDSOclock_gettime()implementation, which extended the clockid switch case and added yet another slightly different copy of the same code.Quoted 9 lines
Especially the extended switch case is problematic as the compiler tends to generate a jump table which then requires to use retpolines. If jump tables are disabled it adds yet another conditional to the existing maze.
This series takes a different approach by consolidating the almost identical functions into one implementation for high resolution clocks and one for the coarse grained clock ids by storing the base data for each clock id in an array which is indexed by the clock id.
Quoted 8 lines
I was trying to understand more of the implications of this patch series, and I was again reminded that there is an entire extra copy of the vclock reading code in
arch/x86/kvm/x86.c. And the purpose of that code is very, very opaque.Can one of you explain what the code is even doing? From a couple of attempts to read through it, it's a whole bunch of probably-extremely-buggy code that,
Yes, probably.
Quoted 2 lines
drumroll please, tries to atomically read the TSC value and the time. And decide whether the result is "based on the TSC".
I think "based on the TSC" refers to whether TSC clocksource is being used.
Quoted 9 lines
And then synthesizes a TSC-to-ns
multiplier and shift, based on *something other than the actual multiply and shift used*.IOW, unless I'm totally misunderstanding it, the code digs into the private arch clocksource data intended for the vDSO, uses a poorly maintained copy of the vDSO code to read the time (instead of doing the sane thing and using the kernel interfaces for this), and propagates a totally made up copy to the guest.
I posted kernel interfaces for this, and it was suggested to instead write a "in-kernel user of pvclock data".
If you can get kernel interfaces to replace that, go for it. I prefer kernel interfaces as well.
Quoted 7 lines
And gets it entirely
wrong when doing nested virt, since, unless there's some secret in this maze, it doesn't acutlaly use the scaling factor from the host when it tells the guest what to do.I am really, seriously tempted to send a patch to simply delete all this code.
If your patch which deletes the code gets the necessary features right, sure, go for it.
Quoted 1 line
The correct way to do it is to hook
Can you expand on the correct way to do it?
Quoted 4 lines
And I don't see how it's even possible to pass kvmclock correctly to the L2 guest when L0 is hyperv. KVM could pass *hyperv's* clock, but L1 isn't notified when the data structure changes, so how the heck is it supposed to update the kvmclock structure?
I don't parse your question.
Quoted 2 lines
--Andy
Re: [patch 00/11] x86/vdso: Cleanups, simmplifications and CLOCK_TAI support\
Quoted 46 lines · Marcelo Tosatti
Quoted 18 lines · Andy Lutomirski
Hi Vitaly, Paolo, Radim, etc.,
Quoted 5 lines · Thomas Gleixner
Matt attempted to add
CLOCK_TAIsupport to the VDSOclock_gettime()implementation, which extended the clockid switch case and added yet another slightly different copy of the same code.Quoted 9 lines
Especially the extended switch case is problematic as the compiler tends to generate a jump table which then requires to use retpolines. If jump tables are disabled it adds yet another conditional to the existing maze.
This series takes a different approach by consolidating the almost identical functions into one implementation for high resolution clocks and one for the coarse grained clock ids by storing the base data for each clock id in an array which is indexed by the clock id.
Quoted 8 lines
I was trying to understand more of the implications of this patch series, and I was again reminded that there is an entire extra copy of the vclock reading code in
arch/x86/kvm/x86.c. And the purpose of that code is very, very opaque.Can one of you explain what the code is even doing? From a couple of attempts to read through it, it's a whole bunch of probably-extremely-buggy code that,
Yes, probably.
Quoted 2 lines
drumroll please, tries to atomically read the TSC value and the time. And decide whether the result is "based on the TSC".
I think "based on the TSC" refers to whether TSC clocksource is being used.
Quoted 9 lines
And then synthesizes a TSC-to-ns
multiplier and shift, based on *something other than the actual multiply and shift used*.IOW, unless I'm totally misunderstanding it, the code digs into the private arch clocksource data intended for the vDSO, uses a poorly maintained copy of the vDSO code to read the time (instead of doing the sane thing and using the kernel interfaces for this), and propagates a totally made up copy to the guest.
Quoted 5 lines
I posted kernel interfaces for this, and it was suggested to instead write a "in-kernel user of pvclock data".
If you can get kernel interfaces to replace that, go for it. I prefer kernel interfaces as well.
And cleanup patches, to make that code look nicer, are also very welcome!
Quoted 42 lines · Vitaly Kuznetsov
Quoted 40 lines · Andy Lutomirski
Hi Vitaly, Paolo, Radim, etc.,
Quoted 5 lines · Thomas Gleixner
Matt attempted to add
CLOCK_TAIsupport to the VDSOclock_gettime()implementation, which extended the clockid switch case and added yet another slightly different copy of the same code.Quoted 9 lines
Especially the extended switch case is problematic as the compiler tends to generate a jump table which then requires to use retpolines. If jump tables are disabled it adds yet another conditional to the existing maze.
This series takes a different approach by consolidating the almost identical functions into one implementation for high resolution clocks and one for the coarse grained clock ids by storing the base data for each clock id in an array which is indexed by the clock id.
I was trying to understand more of the implications of this patch series, and I was again reminded that there is an entire extra copy of the vclock reading code in
arch/x86/kvm/x86.c. And the purpose of that code is very, very opaque.Can one of you explain what the code is even doing? From a couple of attempts to read through it, it's a whole bunch of probably-extremely-buggy code that, drumroll please, tries to atomically read the TSC value and the time. And decide whether the result is "based on the TSC". And then synthesizes a TSC-to-ns multiplier and shift, based on *something other than the actual multiply and shift used*.
IOW, unless I'm totally misunderstanding it, the code digs into the private arch clocksource data intended for the vDSO, uses a poorly maintained copy of the vDSO code to read the time (instead of doing the sane thing and using the kernel interfaces for this), and propagates a totally made up copy to the guest. And gets it entirely wrong when doing nested virt, since, unless there's some secret in this maze, it doesn't acutlaly use the scaling factor from the host when it tells the guest what to do.
Quoted 8 lines
Quoted 2 lines
I am really, seriously tempted to send a patch to simply delete all this code. The correct way to do it is to hook
"I have discovered a truly marvelous proof of this, which this margin is too narrow to contain" :-)
There is a very long history of different (hardware) issues Marcelo was fighting with and the current code is the survived Frankenstein.
Right, the code has to handle different TSC modes.
Quoted 3 lines
E.g. it
is very, very unclear what "catchup", "always catchup" and masterclock-less mode in general are and if we still need them.
Catchup means handling exposed (to guest) TSC frequency smaller than HW TSC frequency.
That is "frankenstein" code, could be removed.
Quoted 2 lines
That said I'm all for simplification. I'm not sure if we still need to care about buggy hardware though.
What simplification is that again?
Quoted 27 lines
Quoted 5 lines
And I don't see how it's even possible to pass kvmclock correctly to the L2 guest when L0 is hyperv. KVM could pass *hyperv's* clock, but L1 isn't notified when the data structure changes, so how the heck is it supposed to update the kvmclock structure?
Well, this kind of works in the the followin way:
L1's clocksource is 'tsc_page' which is, basically, a compliment to TSC:
two numbers provided by L0: offset and scale and KVM was tought to treat this clocksource as a good one (seeb0c39dc68e3b"x86/kvm: Pass stable clocksource to guests when running nested on Hyper-V").The notification you're talking about exists, it is called Reenligntenment, see
0092e4346f49"x86/kvm: Support Hyper-V reenlightenment"). When TSC page changes (and this only happens when L1 is migrated to a different host with a different TSC frequency and TSC scaling is not supported by the CPU) we receive an interrupt in L1 (at this moment all TSC accesses are emulated which guarantees the correctness of the readings), pause all L2 guests, update their kvmclock structures with new data (we already know the new TSC frequency) and then tell L0 that we're done and it can stop emulating TSC accesses.(Nothing like this exists for KVM-on-KVM, by the way, when L1's clocksource is 'kvmclock' L2s won't get a stable kvmclock clocksource.)
Signature · 1 line
Vitaly
Quoted 42 lines · Vitaly Kuznetsov
Quoted 40 lines · Andy Lutomirski
Hi Vitaly, Paolo, Radim, etc.,
Quoted 5 lines · Thomas Gleixner
Matt attempted to add
CLOCK_TAIsupport to the VDSOclock_gettime()implementation, which extended the clockid switch case and added yet another slightly different copy of the same code.Quoted 9 lines
Especially the extended switch case is problematic as the compiler tends to generate a jump table which then requires to use retpolines. If jump tables are disabled it adds yet another conditional to the existing maze.
This series takes a different approach by consolidating the almost identical functions into one implementation for high resolution clocks and one for the coarse grained clock ids by storing the base data for each clock id in an array which is indexed by the clock id.
I was trying to understand more of the implications of this patch series, and I was again reminded that there is an entire extra copy of the vclock reading code in
arch/x86/kvm/x86.c. And the purpose of that code is very, very opaque.Can one of you explain what the code is even doing? From a couple of attempts to read through it, it's a whole bunch of probably-extremely-buggy code that, drumroll please, tries to atomically read the TSC value and the time. And decide whether the result is "based on the TSC". And then synthesizes a TSC-to-ns multiplier and shift, based on *something other than the actual multiply and shift used*.
IOW, unless I'm totally misunderstanding it, the code digs into the private arch clocksource data intended for the vDSO, uses a poorly maintained copy of the vDSO code to read the time (instead of doing the sane thing and using the kernel interfaces for this), and propagates a totally made up copy to the guest. And gets it entirely wrong when doing nested virt, since, unless there's some secret in this maze, it doesn't acutlaly use the scaling factor from the host when it tells the guest what to do.
Quoted 8 lines
Quoted 2 lines
I am really, seriously tempted to send a patch to simply delete all this code. The correct way to do it is to hook
"I have discovered a truly marvelous proof of this, which this margin is too narrow to contain" :-)
There is a very long history of different (hardware) issues Marcelo was fighting with and the current code is the survived Frankenstein.
Right, the code has to handle different TSC modes.
Quoted 3 lines
E.g. it
is very, very unclear what "catchup", "always catchup" and masterclock-less mode in general are and if we still need them.
Catchup means handling exposed (to guest) TSC frequency smaller than HW TSC frequency.
That is "frankenstein" code, could be removed.
Quoted 2 lines
That said I'm all for simplification. I'm not sure if we still need to care about buggy hardware though.
What simplification is that again?
Quoted 27 lines
Quoted 5 lines
And I don't see how it's even possible to pass kvmclock correctly to the L2 guest when L0 is hyperv. KVM could pass *hyperv's* clock, but L1 isn't notified when the data structure changes, so how the heck is it supposed to update the kvmclock structure?
Well, this kind of works in the the followin way:
L1's clocksource is 'tsc_page' which is, basically, a compliment to TSC:
two numbers provided by L0: offset and scale and KVM was tought to treat this clocksource as a good one (seeb0c39dc68e3b"x86/kvm: Pass stable clocksource to guests when running nested on Hyper-V").The notification you're talking about exists, it is called Reenligntenment, see
0092e4346f49"x86/kvm: Support Hyper-V reenlightenment"). When TSC page changes (and this only happens when L1 is migrated to a different host with a different TSC frequency and TSC scaling is not supported by the CPU) we receive an interrupt in L1 (at this moment all TSC accesses are emulated which guarantees the correctness of the readings), pause all L2 guests, update their kvmclock structures with new data (we already know the new TSC frequency) and then tell L0 that we're done and it can stop emulating TSC accesses.(Nothing like this exists for KVM-on-KVM, by the way, when L1's clocksource is 'kvmclock' L2s won't get a stable kvmclock clocksource.)
Signature · 1 line
Vitaly
Andy Lutomirski
maintainerQuoted 66 lines · Marcelo Tosatti
Quoted 18 lines · Andy Lutomirski
Hi Vitaly, Paolo, Radim, etc.,
Quoted 5 lines · Thomas Gleixner
Matt attempted to add
CLOCK_TAIsupport to the VDSOclock_gettime()implementation, which extended the clockid switch case and added yet another slightly different copy of the same code.Quoted 9 lines
Especially the extended switch case is problematic as the compiler tends to generate a jump table which then requires to use retpolines. If jump tables are disabled it adds yet another conditional to the existing maze.
This series takes a different approach by consolidating the almost identical functions into one implementation for high resolution clocks and one for the coarse grained clock ids by storing the base data for each clock id in an array which is indexed by the clock id.
Quoted 8 lines
I was trying to understand more of the implications of this patch series, and I was again reminded that there is an entire extra copy of the vclock reading code in
arch/x86/kvm/x86.c. And the purpose of that code is very, very opaque.Can one of you explain what the code is even doing? From a couple of attempts to read through it, it's a whole bunch of probably-extremely-buggy code that,
Yes, probably.
Quoted 2 lines
drumroll please, tries to atomically read the TSC value and the time. And decide whether the result is "based on the TSC".
I think "based on the TSC" refers to whether TSC clocksource is being used.
Quoted 9 lines
And then synthesizes a TSC-to-ns
multiplier and shift, based on *something other than the actual multiply and shift used*.IOW, unless I'm totally misunderstanding it, the code digs into the private arch clocksource data intended for the vDSO, uses a poorly maintained copy of the vDSO code to read the time (instead of doing the sane thing and using the kernel interfaces for this), and propagates a totally made up copy to the guest.
I posted kernel interfaces for this, and it was suggested to instead write a "in-kernel user of pvclock data".
If you can get kernel interfaces to replace that, go for it. I prefer kernel interfaces as well.
Quoted 7 lines
And gets it entirely
wrong when doing nested virt, since, unless there's some secret in this maze, it doesn't acutlaly use the scaling factor from the host when it tells the guest what to do.I am really, seriously tempted to send a patch to simply delete all this code.
If your patch which deletes the code gets the necessary features right, sure, go for it.
Quoted 1 line
The correct way to do it is to hook
Quoted 8 lines
Can you expand on the correct way to do it?
Quoted 4 lines
And I don't see how it's even possible to pass kvmclock correctly to the L2 guest when L0 is hyperv. KVM could pass *hyperv's* clock, but L1 isn't notified when the data structure changes, so how the heck is it supposed to update the kvmclock structure?
I don't parse your question.
Let me ask it more intelligently: when the "reenlightenment" IRQ happens, what tells KVM to do its own update for its guests?
Quoted 14 lines · Marcelo Tosatti
Quoted 3 lines · Vitaly Kuznetsov
There is a very long history of different (hardware) issues Marcelo was fighting with and the current code is the survived Frankenstein.
Right, the code has to handle different TSC modes.
Quoted 3 lines
E.g. it
is very, very unclear what "catchup", "always catchup" and masterclock-less mode in general are and if we still need them.Catchup means handling exposed (to guest) TSC frequency smaller than HW TSC frequency.
Quoted 7 lines
That is "frankenstein" code, could be removed.
Quoted 2 lines
That said I'm all for simplification. I'm not sure if we still need to care about buggy hardware though.
What simplification is that again?
I was hoping to hear this from you :-) If I am to suggest how we can move forward I'd propose:
- Check if pure TSC can be used on SkyLake+ systems (where TSC scaling is supported).
- Check if non-masterclock mode is still needed. E.g. HyperV's TSC page clocksource is a single page for the whole VM, not a per-cpu thing. Can we think that all the buggy hardware is already gone?
Signature · 1 line
Vitaly
Quoted 14 lines · Marcelo Tosatti
Quoted 3 lines · Vitaly Kuznetsov
There is a very long history of different (hardware) issues Marcelo was fighting with and the current code is the survived Frankenstein.
Right, the code has to handle different TSC modes.
Quoted 3 lines
E.g. it
is very, very unclear what "catchup", "always catchup" and masterclock-less mode in general are and if we still need them.Catchup means handling exposed (to guest) TSC frequency smaller than HW TSC frequency.
Quoted 7 lines
That is "frankenstein" code, could be removed.
Quoted 2 lines
That said I'm all for simplification. I'm not sure if we still need to care about buggy hardware though.
What simplification is that again?
I was hoping to hear this from you :-) If I am to suggest how we can move forward I'd propose:
- Check if pure TSC can be used on SkyLake+ systems (where TSC scaling is supported).
- Check if non-masterclock mode is still needed. E.g. HyperV's TSC page clocksource is a single page for the whole VM, not a per-cpu thing. Can we think that all the buggy hardware is already gone?
Signature · 1 line
Vitaly
Quoted 7 lines · Vitaly Kuznetsov
I was hoping to hear this from you :-) If I am to suggest how we can move forward I'd propose:
- Check if pure TSC can be used on SkyLake+ systems (where TSC scaling is supported).
- Check if non-masterclock mode is still needed. E.g. HyperV's TSC page clocksource is a single page for the whole VM, not a per-cpu thing. Can we think that all the buggy hardware is already gone?
No, and it is not the hardware you have to worry about (mostly), it is the frigging PoS firmware people put on it.
Ever since Nehalem TSC is stable (unless you get to >4 socket systems, after which it still can be, but bets are off). But even relatively recent systems fail the TSC sync test because firmware messes it up by writing to either MSR_TSC or MSR_TSC_ADJUST.
But the thing is, if the TSC is not synced, you cannot use it for timekeeping, full stop. So having a single page is fine, it either contains a mult/shift that is valid, or it indicates TSC is messed up and you fall back to something else.
There is no inbetween there.
For sched_clock we can still use the global page, because the rate will still be the same for each cpu, it's just offset between CPUs and the code compensates for that.
Quoted 7 lines · Vitaly Kuznetsov
I was hoping to hear this from you :-) If I am to suggest how we can move forward I'd propose:
- Check if pure TSC can be used on SkyLake+ systems (where TSC scaling is supported).
- Check if non-masterclock mode is still needed. E.g. HyperV's TSC page clocksource is a single page for the whole VM, not a per-cpu thing. Can we think that all the buggy hardware is already gone?
No, and it is not the hardware you have to worry about (mostly), it is the frigging PoS firmware people put on it.
Ever since Nehalem TSC is stable (unless you get to >4 socket systems, after which it still can be, but bets are off). But even relatively recent systems fail the TSC sync test because firmware messes it up by writing to either MSR_TSC or MSR_TSC_ADJUST.
But the thing is, if the TSC is not synced, you cannot use it for timekeeping, full stop. So having a single page is fine, it either contains a mult/shift that is valid, or it indicates TSC is messed up and you fall back to something else.
There is no inbetween there.
For sched_clock we can still use the global page, because the rate will still be the same for each cpu, it's just offset between CPUs and the code compensates for that.
Quoted 2 lines · Vitaly Kuznetsov
- Check if pure TSC can be used on SkyLake+ systems (where TSC scaling is supported).
Not if you want to migrate to pre-Skylake systems.
Quoted 3 lines
- Check if non-masterclock mode is still needed. E.g. HyperV's TSC page clocksource is a single page for the whole VM, not a per-cpu thing. Can we think that all the buggy hardware is already gone?
No. :( We still get reports whenever we break 2007-2008 hardware.
Paolo
Quoted 2 lines · Vitaly Kuznetsov
- Check if pure TSC can be used on SkyLake+ systems (where TSC scaling is supported).
Not if you want to migrate to pre-Skylake systems.
Quoted 3 lines
- Check if non-masterclock mode is still needed. E.g. HyperV's TSC page clocksource is a single page for the whole VM, not a per-cpu thing. Can we think that all the buggy hardware is already gone?
No. :( We still get reports whenever we break 2007-2008 hardware.
Paolo
Quoted 26 lines · Peter Zijlstra
Quoted 9 lines · Vitaly Kuznetsov
I was hoping to hear this from you :-) If I am to suggest how we can move forward I'd propose:
- Check if pure TSC can be used on SkyLake+ systems (where TSC scaling is supported).
- Check if non-masterclock mode is still needed. E.g. HyperV's TSC page clocksource is a single page for the whole VM, not a per-cpu thing. Can we think that all the buggy hardware is already gone?No, and it is not the hardware you have to worry about (mostly), it is the frigging PoS firmware people put on it.
Ever since Nehalem TSC is stable (unless you get to >4 socket systems, after which it still can be, but bets are off). But even relatively recent systems fail the TSC sync test because firmware messes it up by writing to either
MSR_TSCorMSR_TSC_ADJUST.But the thing is, if the TSC is not synced, you cannot use it for timekeeping, full stop. So having a single page is fine, it either contains a mult/shift that is valid, or it indicates TSC is messed up and you fall back to something else.
Quoted 5 lines
There is no inbetween there.
For
sched_clockwe can still use the global page, because the rate will still be the same for each cpu, it's just offset between CPUs and the code compensates for that.
But if we’re in a KVM guest, then the clock will jump around on the same *vCPU* when the vCPU migrates.
But I don’t see how kvmclock helps here, since I don’t think it’s used for sched_clock.
_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
Quoted 26 lines · Peter Zijlstra
Quoted 9 lines · Vitaly Kuznetsov
I was hoping to hear this from you :-) If I am to suggest how we can move forward I'd propose:
- Check if pure TSC can be used on SkyLake+ systems (where TSC scaling is supported).
- Check if non-masterclock mode is still needed. E.g. HyperV's TSC page clocksource is a single page for the whole VM, not a per-cpu thing. Can we think that all the buggy hardware is already gone?No, and it is not the hardware you have to worry about (mostly), it is the frigging PoS firmware people put on it.
Ever since Nehalem TSC is stable (unless you get to >4 socket systems, after which it still can be, but bets are off). But even relatively recent systems fail the TSC sync test because firmware messes it up by writing to either
MSR_TSCorMSR_TSC_ADJUST.But the thing is, if the TSC is not synced, you cannot use it for timekeeping, full stop. So having a single page is fine, it either contains a mult/shift that is valid, or it indicates TSC is messed up and you fall back to something else.
Quoted 5 lines
There is no inbetween there.
For
sched_clockwe can still use the global page, because the rate will still be the same for each cpu, it's just offset between CPUs and the code compensates for that.
But if we’re in a KVM guest, then the clock will jump around on the same *vCPU* when the vCPU migrates.
But I don’t see how kvmclock helps here, since I don’t think it’s used for sched_clock.
Quoted 7 lines · Paolo Bonzini
Quoted 4 lines · Vitaly Kuznetsov
- Check if pure TSC can be used on SkyLake+ systems (where TSC scaling is supported).
Quoted 9 lines
Not if you want to migrate to pre-Skylake systems.
Quoted 3 lines
- Check if non-masterclock mode is still needed. E.g. HyperV's TSC page clocksource is a single page for the whole VM, not a per-cpu thing. Can we think that all the buggy hardware is already gone?
No. :( We still get reports whenever we break 2007-2008 hardware.
Does the KVM non-masterclock mode actually help? It’s not clear to me exactly how it’s supposed to work, but it seems like it’s trying to expose per-vCPU adjustments to the guest. Which is dubious at best, since the guest can’t validly use them for anything other than sched_clock, since they aren’t fully corrected by anything KVM can do.
_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
Quoted 74 lines · Andy Lutomirski
Quoted 73 lines · Marcelo Tosatti
Quoted 18 lines · Andy Lutomirski
Hi Vitaly, Paolo, Radim, etc.,
Quoted 5 lines · Thomas Gleixner
Matt attempted to add
CLOCK_TAIsupport to the VDSOclock_gettime()implementation, which extended the clockid switch case and added yet another slightly different copy of the same code.Quoted 9 lines
Especially the extended switch case is problematic as the compiler tends to generate a jump table which then requires to use retpolines. If jump tables are disabled it adds yet another conditional to the existing maze.
This series takes a different approach by consolidating the almost identical functions into one implementation for high resolution clocks and one for the coarse grained clock ids by storing the base data for each clock id in an array which is indexed by the clock id.
Quoted 8 lines
I was trying to understand more of the implications of this patch series, and I was again reminded that there is an entire extra copy of the vclock reading code in
arch/x86/kvm/x86.c. And the purpose of that code is very, very opaque.Can one of you explain what the code is even doing? From a couple of attempts to read through it, it's a whole bunch of probably-extremely-buggy code that,
Yes, probably.
Quoted 2 lines
drumroll please, tries to atomically read the TSC value and the time. And decide whether the result is "based on the TSC".
I think "based on the TSC" refers to whether TSC clocksource is being used.
Quoted 9 lines
And then synthesizes a TSC-to-ns
multiplier and shift, based on *something other than the actual multiply and shift used*.IOW, unless I'm totally misunderstanding it, the code digs into the private arch clocksource data intended for the vDSO, uses a poorly maintained copy of the vDSO code to read the time (instead of doing the sane thing and using the kernel interfaces for this), and propagates a totally made up copy to the guest.
I posted kernel interfaces for this, and it was suggested to instead write a "in-kernel user of pvclock data".
If you can get kernel interfaces to replace that, go for it. I prefer kernel interfaces as well.
Quoted 7 lines
And gets it entirely
wrong when doing nested virt, since, unless there's some secret in this maze, it doesn't acutlaly use the scaling factor from the host when it tells the guest what to do.I am really, seriously tempted to send a patch to simply delete all this code.
If your patch which deletes the code gets the necessary features right, sure, go for it.
Quoted 1 line
The correct way to do it is to hook
Can you expand on the correct way to do it?
Quoted 4 lines
And I don't see how it's even possible to pass kvmclock correctly to the L2 guest when L0 is hyperv. KVM could pass *hyperv's* clock, but L1 isn't notified when the data structure changes, so how the heck is it supposed to update the kvmclock structure?
Quoted 4 lines
Quoted 1 line
I don't parse your question.
Let me ask it more intelligently: when the "reenlightenment" IRQ happens, what tells KVM to do its own update for its guests?
Update of what, and why it needs to update anything from IRQ?
The update i can think of is from host kernel clocksource, which there is a notifier for.
Quoted 74 lines · Andy Lutomirski
Quoted 73 lines · Marcelo Tosatti
Quoted 18 lines · Andy Lutomirski
Hi Vitaly, Paolo, Radim, etc.,
Quoted 5 lines · Thomas Gleixner
Matt attempted to add
CLOCK_TAIsupport to the VDSOclock_gettime()implementation, which extended the clockid switch case and added yet another slightly different copy of the same code.Quoted 9 lines
Especially the extended switch case is problematic as the compiler tends to generate a jump table which then requires to use retpolines. If jump tables are disabled it adds yet another conditional to the existing maze.
This series takes a different approach by consolidating the almost identical functions into one implementation for high resolution clocks and one for the coarse grained clock ids by storing the base data for each clock id in an array which is indexed by the clock id.
Quoted 8 lines
I was trying to understand more of the implications of this patch series, and I was again reminded that there is an entire extra copy of the vclock reading code in
arch/x86/kvm/x86.c. And the purpose of that code is very, very opaque.Can one of you explain what the code is even doing? From a couple of attempts to read through it, it's a whole bunch of probably-extremely-buggy code that,
Yes, probably.
Quoted 2 lines
drumroll please, tries to atomically read the TSC value and the time. And decide whether the result is "based on the TSC".
I think "based on the TSC" refers to whether TSC clocksource is being used.
Quoted 9 lines
And then synthesizes a TSC-to-ns
multiplier and shift, based on *something other than the actual multiply and shift used*.IOW, unless I'm totally misunderstanding it, the code digs into the private arch clocksource data intended for the vDSO, uses a poorly maintained copy of the vDSO code to read the time (instead of doing the sane thing and using the kernel interfaces for this), and propagates a totally made up copy to the guest.
I posted kernel interfaces for this, and it was suggested to instead write a "in-kernel user of pvclock data".
If you can get kernel interfaces to replace that, go for it. I prefer kernel interfaces as well.
Quoted 7 lines
And gets it entirely
wrong when doing nested virt, since, unless there's some secret in this maze, it doesn't acutlaly use the scaling factor from the host when it tells the guest what to do.I am really, seriously tempted to send a patch to simply delete all this code.
If your patch which deletes the code gets the necessary features right, sure, go for it.
Quoted 1 line
The correct way to do it is to hook
Can you expand on the correct way to do it?
Quoted 4 lines
And I don't see how it's even possible to pass kvmclock correctly to the L2 guest when L0 is hyperv. KVM could pass *hyperv's* clock, but L1 isn't notified when the data structure changes, so how the heck is it supposed to update the kvmclock structure?
Quoted 4 lines
Quoted 1 line
I don't parse your question.
Let me ask it more intelligently: when the "reenlightenment" IRQ happens, what tells KVM to do its own update for its guests?
Update of what, and why it needs to update anything from IRQ?
The update i can think of is from host kernel clocksource, which there is a notifier for.
Andy Lutomirski
maintainerQuoted 78 lines · Marcelo Tosatti
Quoted 76 lines · Andy Lutomirski
Quoted 74 lines · Marcelo Tosatti
Quoted 18 lines · Andy Lutomirski
Hi Vitaly, Paolo, Radim, etc.,
Quoted 5 lines · Thomas Gleixner
Matt attempted to add
CLOCK_TAIsupport to the VDSOclock_gettime()implementation, which extended the clockid switch case and added yet another slightly different copy of the same code.Quoted 9 lines
Especially the extended switch case is problematic as the compiler tends to generate a jump table which then requires to use retpolines. If jump tables are disabled it adds yet another conditional to the existing maze.
This series takes a different approach by consolidating the almost identical functions into one implementation for high resolution clocks and one for the coarse grained clock ids by storing the base data for each clock id in an array which is indexed by the clock id.
Quoted 8 lines
I was trying to understand more of the implications of this patch series, and I was again reminded that there is an entire extra copy of the vclock reading code in
arch/x86/kvm/x86.c. And the purpose of that code is very, very opaque.Can one of you explain what the code is even doing? From a couple of attempts to read through it, it's a whole bunch of probably-extremely-buggy code that,
Yes, probably.
Quoted 2 lines
drumroll please, tries to atomically read the TSC value and the time. And decide whether the result is "based on the TSC".
I think "based on the TSC" refers to whether TSC clocksource is being used.
Quoted 9 lines
And then synthesizes a TSC-to-ns
multiplier and shift, based on *something other than the actual multiply and shift used*.IOW, unless I'm totally misunderstanding it, the code digs into the private arch clocksource data intended for the vDSO, uses a poorly maintained copy of the vDSO code to read the time (instead of doing the sane thing and using the kernel interfaces for this), and propagates a totally made up copy to the guest.
I posted kernel interfaces for this, and it was suggested to instead write a "in-kernel user of pvclock data".
If you can get kernel interfaces to replace that, go for it. I prefer kernel interfaces as well.
Quoted 7 lines
And gets it entirely
wrong when doing nested virt, since, unless there's some secret in this maze, it doesn't acutlaly use the scaling factor from the host when it tells the guest what to do.I am really, seriously tempted to send a patch to simply delete all this code.
If your patch which deletes the code gets the necessary features right, sure, go for it.
Quoted 1 line
The correct way to do it is to hook
Can you expand on the correct way to do it?
Quoted 4 lines
And I don't see how it's even possible to pass kvmclock correctly to the L2 guest when L0 is hyperv. KVM could pass *hyperv's* clock, but L1 isn't notified when the data structure changes, so how the heck is it supposed to update the kvmclock structure?
I don't parse your question.
Quoted 9 lines
Quoted 2 lines
Let me ask it more intelligently: when the "reenlightenment" IRQ happens, what tells KVM to do its own update for its guests?
Update of what, and why it needs to update anything from IRQ?
The update i can think of is from host kernel clocksource, which there is a notifier for.
Unless I've missed some serious magic, L2 guests see kvmclock, not hv. So we have the following sequence of events:
- L0 migrates the whole VM. Starting now, RDTSC is emulated to match
the old host, which applies in L1 and L2.
- An IRQ is queued to L1.
- L1 acknowledges that it noticed the TSC change. RDTSC stops being
emulated for L1 and L2.
- L2 reads the TSC. It has no idea that anything changed, and it
gets the wrong answer.
- At some point, kvm clock updates.
What prevents this? Vitaly, am I missing some subtlety of what actually happens?
Andy Lutomirski
maintainerQuoted 78 lines · Marcelo Tosatti
Quoted 76 lines · Andy Lutomirski
Quoted 74 lines · Marcelo Tosatti
Quoted 18 lines · Andy Lutomirski
Hi Vitaly, Paolo, Radim, etc.,
Quoted 5 lines · Thomas Gleixner
Matt attempted to add
CLOCK_TAIsupport to the VDSOclock_gettime()implementation, which extended the clockid switch case and added yet another slightly different copy of the same code.Quoted 9 lines
Especially the extended switch case is problematic as the compiler tends to generate a jump table which then requires to use retpolines. If jump tables are disabled it adds yet another conditional to the existing maze.
This series takes a different approach by consolidating the almost identical functions into one implementation for high resolution clocks and one for the coarse grained clock ids by storing the base data for each clock id in an array which is indexed by the clock id.
Quoted 8 lines
I was trying to understand more of the implications of this patch series, and I was again reminded that there is an entire extra copy of the vclock reading code in
arch/x86/kvm/x86.c. And the purpose of that code is very, very opaque.Can one of you explain what the code is even doing? From a couple of attempts to read through it, it's a whole bunch of probably-extremely-buggy code that,
Yes, probably.
Quoted 2 lines
drumroll please, tries to atomically read the TSC value and the time. And decide whether the result is "based on the TSC".
I think "based on the TSC" refers to whether TSC clocksource is being used.
Quoted 9 lines
And then synthesizes a TSC-to-ns
multiplier and shift, based on *something other than the actual multiply and shift used*.IOW, unless I'm totally misunderstanding it, the code digs into the private arch clocksource data intended for the vDSO, uses a poorly maintained copy of the vDSO code to read the time (instead of doing the sane thing and using the kernel interfaces for this), and propagates a totally made up copy to the guest.
I posted kernel interfaces for this, and it was suggested to instead write a "in-kernel user of pvclock data".
If you can get kernel interfaces to replace that, go for it. I prefer kernel interfaces as well.
Quoted 7 lines
And gets it entirely
wrong when doing nested virt, since, unless there's some secret in this maze, it doesn't acutlaly use the scaling factor from the host when it tells the guest what to do.I am really, seriously tempted to send a patch to simply delete all this code.
If your patch which deletes the code gets the necessary features right, sure, go for it.
Quoted 1 line
The correct way to do it is to hook
Can you expand on the correct way to do it?
Quoted 4 lines
And I don't see how it's even possible to pass kvmclock correctly to the L2 guest when L0 is hyperv. KVM could pass *hyperv's* clock, but L1 isn't notified when the data structure changes, so how the heck is it supposed to update the kvmclock structure?
I don't parse your question.
Quoted 9 lines
Quoted 2 lines
Let me ask it more intelligently: when the "reenlightenment" IRQ happens, what tells KVM to do its own update for its guests?
Update of what, and why it needs to update anything from IRQ?
The update i can think of is from host kernel clocksource, which there is a notifier for.
Unless I've missed some serious magic, L2 guests see kvmclock, not hv. So we have the following sequence of events:
- L0 migrates the whole VM. Starting now, RDTSC is emulated to match
the old host, which applies in L1 and L2.
- An IRQ is queued to L1.
- L1 acknowledges that it noticed the TSC change. RDTSC stops being
emulated for L1 and L2.
- L2 reads the TSC. It has no idea that anything changed, and it
gets the wrong answer.
- At some point, kvm clock updates.
What prevents this? Vitaly, am I missing some subtlety of what actually happens?
Quoted 92 lines · Andy Lutomirski
Quoted 78 lines · Marcelo Tosatti
Quoted 76 lines · Andy Lutomirski
Quoted 74 lines · Marcelo Tosatti
Quoted 18 lines · Andy Lutomirski
Hi Vitaly, Paolo, Radim, etc.,
Quoted 5 lines · Thomas Gleixner
Matt attempted to add
CLOCK_TAIsupport to the VDSOclock_gettime()implementation, which extended the clockid switch case and added yet another slightly different copy of the same code.Quoted 9 lines
Especially the extended switch case is problematic as the compiler tends to generate a jump table which then requires to use retpolines. If jump tables are disabled it adds yet another conditional to the existing maze.
This series takes a different approach by consolidating the almost identical functions into one implementation for high resolution clocks and one for the coarse grained clock ids by storing the base data for each clock id in an array which is indexed by the clock id.
Quoted 8 lines
I was trying to understand more of the implications of this patch series, and I was again reminded that there is an entire extra copy of the vclock reading code in
arch/x86/kvm/x86.c. And the purpose of that code is very, very opaque.Can one of you explain what the code is even doing? From a couple of attempts to read through it, it's a whole bunch of probably-extremely-buggy code that,
Yes, probably.
Quoted 2 lines
drumroll please, tries to atomically read the TSC value and the time. And decide whether the result is "based on the TSC".
I think "based on the TSC" refers to whether TSC clocksource is being used.
Quoted 9 lines
And then synthesizes a TSC-to-ns
multiplier and shift, based on *something other than the actual multiply and shift used*.IOW, unless I'm totally misunderstanding it, the code digs into the private arch clocksource data intended for the vDSO, uses a poorly maintained copy of the vDSO code to read the time (instead of doing the sane thing and using the kernel interfaces for this), and propagates a totally made up copy to the guest.
I posted kernel interfaces for this, and it was suggested to instead write a "in-kernel user of pvclock data".
If you can get kernel interfaces to replace that, go for it. I prefer kernel interfaces as well.
Quoted 7 lines
And gets it entirely
wrong when doing nested virt, since, unless there's some secret in this maze, it doesn't acutlaly use the scaling factor from the host when it tells the guest what to do.I am really, seriously tempted to send a patch to simply delete all this code.
If your patch which deletes the code gets the necessary features right, sure, go for it.
Quoted 1 line
The correct way to do it is to hook
Can you expand on the correct way to do it?
Quoted 4 lines
And I don't see how it's even possible to pass kvmclock correctly to the L2 guest when L0 is hyperv. KVM could pass *hyperv's* clock, but L1 isn't notified when the data structure changes, so how the heck is it supposed to update the kvmclock structure?
I don't parse your question.
Quoted 9 lines
Quoted 2 lines
Let me ask it more intelligently: when the "reenlightenment" IRQ happens, what tells KVM to do its own update for its guests?
Update of what, and why it needs to update anything from IRQ?
The update i can think of is from host kernel clocksource, which there is a notifier for.
Unless I've missed some serious magic, L2 guests see kvmclock, not hv. So we have the following sequence of events:
Quoted 6 lines
- L0 migrates the whole VM. Starting now, RDTSC is emulated to match
the old host, which applies in L1 and L2.- An IRQ is queued to L1.
- L1 acknowledges that it noticed the TSC change.
Before the acknowledgement we actually pause all guests so they don't notice the change ....
Quoted 1 line
RDTSC stops being emulated for L1 and L2.
.... and right after that we update all kvmclocks for all L2s and unpause them so all their readings are still correct (see kvm_hyperv_tsc_notifier()).
Quoted 3 lines
- L2 reads the TSC. It has no idea that anything changed, and it
gets the wrong answer.
I have to admit I forgot what happens if L2 uses raw TSC. I *think* that we actually adjust TSC offset along with adjusting kvmclocks so the reading is still correct. I'll have to check this.
All bets are off in case L2 was using TSC for time interval
measurements: frequency, of course, changes.
Quoted 5 lines
- At some point, kvm clock updates.
What prevents this? Vitaly, am I missing some subtlety of what actually happens?
Signature · 1 line
Vitaly
Quoted 24 lines · Andy Lutomirski
Quoted 24 lines · Peter Zijlstra
Quoted 9 lines · Vitaly Kuznetsov
I was hoping to hear this from you :-) If I am to suggest how we can move forward I'd propose:
- Check if pure TSC can be used on SkyLake+ systems (where TSC scaling is supported).
- Check if non-masterclock mode is still needed. E.g. HyperV's TSC page clocksource is a single page for the whole VM, not a per-cpu thing. Can we think that all the buggy hardware is already gone?No, and it is not the hardware you have to worry about (mostly), it is the frigging PoS firmware people put on it.
Ever since Nehalem TSC is stable (unless you get to >4 socket systems, after which it still can be, but bets are off). But even relatively recent systems fail the TSC sync test because firmware messes it up by writing to either
MSR_TSCorMSR_TSC_ADJUST.But the thing is, if the TSC is not synced, you cannot use it for timekeeping, full stop. So having a single page is fine, it either contains a mult/shift that is valid, or it indicates TSC is messed up and you fall back to something else.
Quoted 8 lines
Quoted 5 lines
There is no inbetween there.
For
sched_clockwe can still use the global page, because the rate will still be the same for each cpu, it's just offset between CPUs and the code compensates for that.But if we’re in a KVM guest, then the clock will jump around on the same *vCPU* when the vCPU migrates.
Urgh yes..
Quoted 2 lines
But I don’t see how kvmclock helps here, since I don’t think it’s used for
sched_clock.
I get hits on kvm_sched_clock, but haven't looked further.
Anyway, Most of the argument still holds, either TSC is synced or it is not and it _really_ should not be used. Not much middle ground there.
_______________________________________________
Virtualization mailing list [email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
Quoted 24 lines · Andy Lutomirski
Quoted 24 lines · Peter Zijlstra
Quoted 9 lines · Vitaly Kuznetsov
I was hoping to hear this from you :-) If I am to suggest how we can move forward I'd propose:
- Check if pure TSC can be used on SkyLake+ systems (where TSC scaling is supported).
- Check if non-masterclock mode is still needed. E.g. HyperV's TSC page clocksource is a single page for the whole VM, not a per-cpu thing. Can we think that all the buggy hardware is already gone?No, and it is not the hardware you have to worry about (mostly), it is the frigging PoS firmware people put on it.
Ever since Nehalem TSC is stable (unless you get to >4 socket systems, after which it still can be, but bets are off). But even relatively recent systems fail the TSC sync test because firmware messes it up by writing to either
MSR_TSCorMSR_TSC_ADJUST.But the thing is, if the TSC is not synced, you cannot use it for timekeeping, full stop. So having a single page is fine, it either contains a mult/shift that is valid, or it indicates TSC is messed up and you fall back to something else.
Quoted 8 lines
Quoted 5 lines
There is no inbetween there.
For
sched_clockwe can still use the global page, because the rate will still be the same for each cpu, it's just offset between CPUs and the code compensates for that.But if we’re in a KVM guest, then the clock will jump around on the same *vCPU* when the vCPU migrates.
Urgh yes..
Quoted 2 lines
But I don’t see how kvmclock helps here, since I don’t think it’s used for
sched_clock.
I get hits on kvm_sched_clock, but haven't looked further.
Anyway, Most of the argument still holds, either TSC is synced or it is not and it _really_ should not be used. Not much middle ground there.
Quoted 38 lines · Peter Zijlstra
Quoted 24 lines · Andy Lutomirski
Quoted 24 lines · Peter Zijlstra
Quoted 9 lines · Vitaly Kuznetsov
I was hoping to hear this from you :-) If I am to suggest how we can move forward I'd propose:
- Check if pure TSC can be used on SkyLake+ systems (where TSC scaling is supported).
- Check if non-masterclock mode is still needed. E.g. HyperV's TSC page clocksource is a single page for the whole VM, not a per-cpu thing. Can we think that all the buggy hardware is already gone?No, and it is not the hardware you have to worry about (mostly), it is the frigging PoS firmware people put on it.
Ever since Nehalem TSC is stable (unless you get to >4 socket systems, after which it still can be, but bets are off). But even relatively recent systems fail the TSC sync test because firmware messes it up by writing to either
MSR_TSCorMSR_TSC_ADJUST.But the thing is, if the TSC is not synced, you cannot use it for timekeeping, full stop. So having a single page is fine, it either contains a mult/shift that is valid, or it indicates TSC is messed up and you fall back to something else.
Quoted 8 lines
Quoted 5 lines
There is no inbetween there.
For
sched_clockwe can still use the global page, because the rate will still be the same for each cpu, it's just offset between CPUs and the code compensates for that.But if we’re in a KVM guest, then the clock will jump around on the same *vCPU* when the vCPU migrates.
Quoted 6 lines
Urgh yes..
Quoted 2 lines
But I don’t see how kvmclock helps here, since I don’t think it’s used for
sched_clock.I get hits on
kvm_sched_clock, but haven't looked further.
Ok, so KVM is using the per-vCPU pvclock data for sched_clock. Which hopefully does something intelligent.
Regardless of any TSC syncing issues, a paravirt clock should presumably be used for sched_clock to account for time that the vCPU was stopped.
It would be fantastic if this stuff were documented much better, both in terms of the data structures and the Linux code.
_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
Quoted 38 lines · Peter Zijlstra
Quoted 24 lines · Andy Lutomirski
Quoted 24 lines · Peter Zijlstra
Quoted 9 lines · Vitaly Kuznetsov
I was hoping to hear this from you :-) If I am to suggest how we can move forward I'd propose:
- Check if pure TSC can be used on SkyLake+ systems (where TSC scaling is supported).
- Check if non-masterclock mode is still needed. E.g. HyperV's TSC page clocksource is a single page for the whole VM, not a per-cpu thing. Can we think that all the buggy hardware is already gone?No, and it is not the hardware you have to worry about (mostly), it is the frigging PoS firmware people put on it.
Ever since Nehalem TSC is stable (unless you get to >4 socket systems, after which it still can be, but bets are off). But even relatively recent systems fail the TSC sync test because firmware messes it up by writing to either
MSR_TSCorMSR_TSC_ADJUST.But the thing is, if the TSC is not synced, you cannot use it for timekeeping, full stop. So having a single page is fine, it either contains a mult/shift that is valid, or it indicates TSC is messed up and you fall back to something else.
Quoted 8 lines
Quoted 5 lines
There is no inbetween there.
For
sched_clockwe can still use the global page, because the rate will still be the same for each cpu, it's just offset between CPUs and the code compensates for that.But if we’re in a KVM guest, then the clock will jump around on the same *vCPU* when the vCPU migrates.
Quoted 6 lines
Urgh yes..
Quoted 2 lines
But I don’t see how kvmclock helps here, since I don’t think it’s used for
sched_clock.I get hits on
kvm_sched_clock, but haven't looked further.
Ok, so KVM is using the per-vCPU pvclock data for sched_clock. Which hopefully does something intelligent.
Regardless of any TSC syncing issues, a paravirt clock should presumably be used for sched_clock to account for time that the vCPU was stopped.
It would be fantastic if this stuff were documented much better, both in terms of the data structures and the Linux code.
Andy Lutomirski
maintainerQuoted 14 lines · Thomas Gleixner
Matt attempted to add
CLOCK_TAIsupport to the VDSOclock_gettime()implementation, which extended the clockid switch case and added yet another slightly different copy of the same code.Especially the extended switch case is problematic as the compiler tends to generate a jump table which then requires to use retpolines. If jump tables are disabled it adds yet another conditional to the existing maze.
This series takes a different approach by consolidating the almost identical functions into one implementation for high resolution clocks and one for the coarse grained clock ids by storing the base data for each clock id in an array which is indexed by the clock id.
Quoted 4 lines
This completely eliminates the switch case and allows further simplifications of the code base, which at the end all together gain a few cycles performance or at least stay on par with todays code. The resulting performance depends heavily on the micro architecture and the compiler.
tglx, please consider this whole series Acked-by: Andy Lutomirski <[email protected]>
Please feel free to push it top tip:x86/vdso, as long as you pull in tip/x86/urgent first. Once it lands, I'll email out a couple of follow-up patches.
--Andy
Andy Lutomirski
maintainerFor better or for worse, I'm trying to understand this code. So far, I've come up with this patch:
Is it correct, or am I missing some subtlety?
Quoted 21 lines · Vitaly Kuznetsov
Quoted 19 lines · Marcelo Tosatti
Quoted 3 lines · Vitaly Kuznetsov
There is a very long history of different (hardware) issues Marcelo was fighting with and the current code is the survived Frankenstein.
Right, the code has to handle different TSC modes.
Quoted 3 lines
E.g. it
is very, very unclear what "catchup", "always catchup" and masterclock-less mode in general are and if we still need them.Catchup means handling exposed (to guest) TSC frequency smaller than HW TSC frequency.
That is "frankenstein" code, could be removed.
Quoted 2 lines
That said I'm all for simplification. I'm not sure if we still need to care about buggy hardware though.
Quoted 7 lines
Quoted 2 lines
What simplification is that again?
I was hoping to hear this from you :-) If I am to suggest how we can move forward I'd propose:
- Check if pure TSC can be used on SkyLake+ systems (where TSC scaling is supported).
In that case just use TSC clocksource on the guest directly: i am writing code for that now (its faster than pvclock syscall).
Quoted 3 lines
- Check if non-masterclock mode is still needed. E.g. HyperV's TSC page clocksource is a single page for the whole VM, not a per-cpu thing. Can we think that all the buggy hardware is already gone?
As Peter mentioned, non sync TSC hardware might exist in the future. And older hardware must be supported.
Quoted 21 lines · Vitaly Kuznetsov
Quoted 19 lines · Marcelo Tosatti
Quoted 3 lines · Vitaly Kuznetsov
There is a very long history of different (hardware) issues Marcelo was fighting with and the current code is the survived Frankenstein.
Right, the code has to handle different TSC modes.
Quoted 3 lines
E.g. it
is very, very unclear what "catchup", "always catchup" and masterclock-less mode in general are and if we still need them.Catchup means handling exposed (to guest) TSC frequency smaller than HW TSC frequency.
That is "frankenstein" code, could be removed.
Quoted 2 lines
That said I'm all for simplification. I'm not sure if we still need to care about buggy hardware though.
Quoted 7 lines
Quoted 2 lines
What simplification is that again?
I was hoping to hear this from you :-) If I am to suggest how we can move forward I'd propose:
- Check if pure TSC can be used on SkyLake+ systems (where TSC scaling is supported).
In that case just use TSC clocksource on the guest directly: i am writing code for that now (its faster than pvclock syscall).
Quoted 3 lines
- Check if non-masterclock mode is still needed. E.g. HyperV's TSC page clocksource is a single page for the whole VM, not a per-cpu thing. Can we think that all the buggy hardware is already gone?
As Peter mentioned, non sync TSC hardware might exist in the future. And older hardware must be supported.
Quoted 6 lines · Andy Lutomirski
For better or for worse, I'm trying to understand this code. So far, I've come up with this patch:
Is it correct, or am I missing some subtlety?
The master clock, when initialized, has a pair
masterclockvalues=(TSC value, time-of-day data).
When updating the guest clock, we only update relative to (TSC value) that was read on masterclock initialization.
See the following comment on x86.c:
/*
*
* Assuming a stable TSC across physical CPUS, and a stable TSC
* across virtual CPUs, the following condition is possible.
* Each numbered line represents an event visible to both
* CPUs at the next numbered event.
...
When updating the "masterclockvalues" pair, all vcpus are stopped.
Quoted 6 lines · Andy Lutomirski
For better or for worse, I'm trying to understand this code. So far, I've come up with this patch:
Is it correct, or am I missing some subtlety?
"In the non-fallback case, a bunch of gnarly arithmetic is done: a hopefully matched pair of (TSC, boot time) is read, then all locks are dropped, then the TSC frequency is read, a branch new multiplier and shift is read, and the result is turned into a time.
This seems quite racy to me. Because locks are not held, I don't see what keeps TSC frequency used in sync with the master clock data."
If tsc_khz changes, the host TSC clocksource will not be used, which disables masterclock:
if ((val == CPUFREQ_PRECHANGE && freq->old < freq->new) ||
(val == CPUFREQ_POSTCHANGE && freq->old >
freq->new)) {
*lpj = cpufreq_scale(loops_per_jiffy_ref, ref_freq,freq->new);
tsc_khz = cpufreq_scale(tsc_khz_ref, ref_freq,
freq->new);
if (!(freq->flags & CPUFREQ_CONST_LOOPS))
mark_tsc_unstable("cpufreq changes");In which case it ends up in:
- spin_lock(&ka->pvclock_gtod_sync_lock);
- if (!ka->use_master_clock) {
- spin_unlock(&ka->pvclock_gtod_sync_lock);
- return ktime_get_boot_ns() + ka->kvmclock_offset;
- }
masterclock -> non masterclock transition sets
a REQUEST bit on each vCPU, so as to invalidate any previous clock reads.
static void kvm_gen_update_masterclock(struct kvm *kvm) {
#ifdef CONFIG_X86_64
int i;
struct kvm_vcpu *vcpu;
struct kvm_arch *ka = &kvm->arch;
spin_lock(&ka->pvclock_gtod_sync_lock);
kvm_make_mclock_inprogress_request(kvm);
/* no guest entries from this point */
pvclock_update_vm_gtod_copy(kvm);
kvm_for_each_vcpu(i, vcpu, kvm)
kvm_make_request(KVM_REQ_CLOCK_UPDATE, vcpu);
/* guest entries allowed */
kvm_for_each_vcpu(i, vcpu, kvm)
kvm_clear_request(KVM_REQ_MCLOCK_INPROGRESS, vcpu);
spin_unlock(&ka->pvclock_gtod_sync_lock);#endif
/*
* If the host uses TSC clock, then passthrough TSC as stable
* to the guest.
*/
host_tsc_clocksource = kvm_get_time_and_clockread(
&ka->master_kernel_ns,
&ka->master_cycle_now);
ka->use_master_clock = host_tsc_clocksource && vcpus_matched
&& !ka->backwards_tsc_observed
&& !ka->boot_vcpu_runs_old_kvmclock;Andy Lutomirski
maintainerQuoted 7 lines · Marcelo Tosatti
Quoted 5 lines · Andy Lutomirski
For better or for worse, I'm trying to understand this code. So far, I've come up with this patch:
Quoted 8 lines
Quoted 1 line
Is it correct, or am I missing some subtlety?
The master clock, when initialized, has a pair
masterclockvalues=(TSC value, time-of-day data).
When updating the guest clock, we only update relative to (TSC value) that was read on masterclock initialization.
I don't see the problem. The masterclock data is updated here:
host_tsc_clocksource = kvm_get_time_and_clockread(
&ka->master_kernel_ns,
&ka->master_cycle_now);kvm_get_time_and_clockread() gets those values from do_monotonic_boot(), which, barring bugs, should cause get_kvmclock_ns() to return exactly the same thing as ktime_get_boot_ns() + ka->kvmclock_offset, albeit in a rather roundabout manner.
So what am I missing? Is there actually something wrong with my patch?
Quoted 2 lines
See the following comment on
x86.c:
I read that comment, and it's not obvious to me how it's related.
Andy Lutomirski
maintainerQuoted 7 lines · Marcelo Tosatti
Quoted 5 lines · Andy Lutomirski
For better or for worse, I'm trying to understand this code. So far, I've come up with this patch:
Quoted 8 lines
Quoted 1 line
Is it correct, or am I missing some subtlety?
The master clock, when initialized, has a pair
masterclockvalues=(TSC value, time-of-day data).
When updating the guest clock, we only update relative to (TSC value) that was read on masterclock initialization.
I don't see the problem. The masterclock data is updated here:
host_tsc_clocksource = kvm_get_time_and_clockread(
&ka->master_kernel_ns,
&ka->master_cycle_now);kvm_get_time_and_clockread() gets those values from do_monotonic_boot(), which, barring bugs, should cause get_kvmclock_ns() to return exactly the same thing as ktime_get_boot_ns() + ka->kvmclock_offset, albeit in a rather roundabout manner.
So what am I missing? Is there actually something wrong with my patch?
Quoted 2 lines
See the following comment on
x86.c:
I read that comment, and it's not obvious to me how it's related.
Quoted 6 lines · Andy Lutomirski
For better or for worse, I'm trying to understand this code. So far, I've come up with this patch:
Is it correct, or am I missing some subtlety?
"In the non-fallback case, a bunch of gnarly arithmetic is done: a hopefully matched pair of (TSC, boot time) is read, then all locks are dropped, then the TSC frequency is read, a branch new multiplier and shift is read, and the result is turned into a time.
This seems quite racy to me. Because locks are not held, I don't see what keeps TSC frequency used in sync with the master clock data."
If tsc_khz changes, the host TSC clocksource will not be used, which disables masterclock:
if ((val == CPUFREQ_PRECHANGE && freq->old < freq->new) ||
(val == CPUFREQ_POSTCHANGE && freq->old >
freq->new)) {
*lpj = cpufreq_scale(loops_per_jiffy_ref, ref_freq,freq->new);
tsc_khz = cpufreq_scale(tsc_khz_ref, ref_freq,
freq->new);
if (!(freq->flags & CPUFREQ_CONST_LOOPS))
mark_tsc_unstable("cpufreq changes");In which case it ends up in:
- spin_lock(&ka->pvclock_gtod_sync_lock);
- if (!ka->use_master_clock) {
- spin_unlock(&ka->pvclock_gtod_sync_lock);
- return ktime_get_boot_ns() + ka->kvmclock_offset;
- }
masterclock -> non masterclock transition sets
a REQUEST bit on each vCPU, so as to invalidate any previous clock reads.
static void kvm_gen_update_masterclock(struct kvm *kvm) {
#ifdef CONFIG_X86_64
int i;
struct kvm_vcpu *vcpu;
struct kvm_arch *ka = &kvm->arch;
spin_lock(&ka->pvclock_gtod_sync_lock);
kvm_make_mclock_inprogress_request(kvm);
/* no guest entries from this point */
pvclock_update_vm_gtod_copy(kvm);
kvm_for_each_vcpu(i, vcpu, kvm)
kvm_make_request(KVM_REQ_CLOCK_UPDATE, vcpu);
/* guest entries allowed */
kvm_for_each_vcpu(i, vcpu, kvm)
kvm_clear_request(KVM_REQ_MCLOCK_INPROGRESS, vcpu);
spin_unlock(&ka->pvclock_gtod_sync_lock);#endif
/*
* If the host uses TSC clock, then passthrough TSC as stable
* to the guest.
*/
host_tsc_clocksource = kvm_get_time_and_clockread(
&ka->master_kernel_ns,
&ka->master_cycle_now);
ka->use_master_clock = host_tsc_clocksource && vcpus_matched
&& !ka->backwards_tsc_observed
&& !ka->boot_vcpu_runs_old_kvmclock;Quoted 23 lines · Andy Lutomirski
Quoted 7 lines · Marcelo Tosatti
Quoted 5 lines · Andy Lutomirski
For better or for worse, I'm trying to understand this code. So far, I've come up with this patch:
Quoted 8 lines
Quoted 1 line
Is it correct, or am I missing some subtlety?
The master clock, when initialized, has a pair
masterclockvalues=(TSC value, time-of-day data).
When updating the guest clock, we only update relative to (TSC value) that was read on masterclock initialization.
I don't see the problem. The masterclock data is updated here:
host_tsc_clocksource = kvm_get_time_and_clockread( &ka->master_kernel_ns, &ka->master_cycle_now);
Quoted 7 lines
kvm_get_time_and_clockread()gets those values fromdo_monotonic_boot(), which, barring bugs, should causeget_kvmclock_ns()to return exactly the same thing asktime_get_boot_ns()+ka->kvmclock_offset, albeit in a rather roundabout manner.So what am I missing? Is there actually something wrong with my patch?
For the bug mentioned in the comment not to happen, you must only read TSC and add it as offset to (TSC value, time-of-day data).
Its more than "a roundabout manner".
Read the comment again.
Quoted 6 lines
Quoted 4 lines
See the following comment on
x86.c:I read that comment, and it's not obvious to me how it's related.
Quoted 23 lines · Andy Lutomirski
Quoted 7 lines · Marcelo Tosatti
Quoted 5 lines · Andy Lutomirski
For better or for worse, I'm trying to understand this code. So far, I've come up with this patch:
Quoted 8 lines
Quoted 1 line
Is it correct, or am I missing some subtlety?
The master clock, when initialized, has a pair
masterclockvalues=(TSC value, time-of-day data).
When updating the guest clock, we only update relative to (TSC value) that was read on masterclock initialization.
I don't see the problem. The masterclock data is updated here:
host_tsc_clocksource = kvm_get_time_and_clockread( &ka->master_kernel_ns, &ka->master_cycle_now);
Quoted 7 lines
kvm_get_time_and_clockread()gets those values fromdo_monotonic_boot(), which, barring bugs, should causeget_kvmclock_ns()to return exactly the same thing asktime_get_boot_ns()+ka->kvmclock_offset, albeit in a rather roundabout manner.So what am I missing? Is there actually something wrong with my patch?
For the bug mentioned in the comment not to happen, you must only read TSC and add it as offset to (TSC value, time-of-day data).
Its more than "a roundabout manner".
Read the comment again.
Quoted 6 lines
Quoted 4 lines
See the following comment on
x86.c:I read that comment, and it's not obvious to me how it's related.
Andy Lutomirski
maintainerQuoted 31 lines · Marcelo Tosatti
Quoted 29 lines · Andy Lutomirski
Quoted 7 lines · Marcelo Tosatti
Quoted 5 lines · Andy Lutomirski
For better or for worse, I'm trying to understand this code. So far, I've come up with this patch:
Quoted 8 lines
Quoted 1 line
Is it correct, or am I missing some subtlety?
The master clock, when initialized, has a pair
masterclockvalues=(TSC value, time-of-day data).
When updating the guest clock, we only update relative to (TSC value) that was read on masterclock initialization.
I don't see the problem. The masterclock data is updated here:
host_tsc_clocksource = kvm_get_time_and_clockread( &ka->master_kernel_ns, &ka->master_cycle_now);
kvm_get_time_and_clockread()gets those values fromdo_monotonic_boot(), which, barring bugs, should causeget_kvmclock_ns()to return exactly the same thing asktime_get_boot_ns()+ka->kvmclock_offset, albeit in a rather roundabout manner.
Quoted 9 lines
Quoted 1 line
So what am I missing? Is there actually something wrong with my patch?
For the bug mentioned in the comment not to happen, you must only read TSC and add it as offset to (TSC value, time-of-day data).
Its more than "a roundabout manner".
Read the comment again.
I read the comment three more times and even dug through the git history. It seems like what you're saying is that, under certain conditions (which arguably would be bugs in the core Linux timing code), actually calling ktime_get_boot_ns() could be non-monotonic with respect to the kvmclock timing. But get_kvmclock_ns() isn't used for VM timing as such -- it's used for the IOCTL interfaces for updating the time offset. So can you explain how my patch is incorrect?
--Andy
Andy Lutomirski
maintainerQuoted 31 lines · Marcelo Tosatti
Quoted 29 lines · Andy Lutomirski
Quoted 7 lines · Marcelo Tosatti
Quoted 5 lines · Andy Lutomirski
For better or for worse, I'm trying to understand this code. So far, I've come up with this patch:
Quoted 8 lines
Quoted 1 line
Is it correct, or am I missing some subtlety?
The master clock, when initialized, has a pair
masterclockvalues=(TSC value, time-of-day data).
When updating the guest clock, we only update relative to (TSC value) that was read on masterclock initialization.
I don't see the problem. The masterclock data is updated here:
host_tsc_clocksource = kvm_get_time_and_clockread( &ka->master_kernel_ns, &ka->master_cycle_now);
kvm_get_time_and_clockread()gets those values fromdo_monotonic_boot(), which, barring bugs, should causeget_kvmclock_ns()to return exactly the same thing asktime_get_boot_ns()+ka->kvmclock_offset, albeit in a rather roundabout manner.
Quoted 9 lines
Quoted 1 line
So what am I missing? Is there actually something wrong with my patch?
For the bug mentioned in the comment not to happen, you must only read TSC and add it as offset to (TSC value, time-of-day data).
Its more than "a roundabout manner".
Read the comment again.
I read the comment three more times and even dug through the git history. It seems like what you're saying is that, under certain conditions (which arguably would be bugs in the core Linux timing code), actually calling ktime_get_boot_ns() could be non-monotonic with respect to the kvmclock timing. But get_kvmclock_ns() isn't used for VM timing as such -- it's used for the IOCTL interfaces for updating the time offset. So can you explain how my patch is incorrect?
--Andy
Quoted 39 lines · Andy Lutomirski
Quoted 38 lines · Marcelo Tosatti
Quoted 23 lines · Andy Lutomirski
Quoted 7 lines · Marcelo Tosatti
Quoted 5 lines · Andy Lutomirski
For better or for worse, I'm trying to understand this code. So far, I've come up with this patch:
Quoted 8 lines
Quoted 1 line
Is it correct, or am I missing some subtlety?
The master clock, when initialized, has a pair
masterclockvalues=(TSC value, time-of-day data).
When updating the guest clock, we only update relative to (TSC value) that was read on masterclock initialization.
I don't see the problem. The masterclock data is updated here:
host_tsc_clocksource = kvm_get_time_and_clockread( &ka->master_kernel_ns, &ka->master_cycle_now);Quoted 7 lines
kvm_get_time_and_clockread()gets those values fromdo_monotonic_boot(), which, barring bugs, should causeget_kvmclock_ns()to return exactly the same thing asktime_get_boot_ns()+ka->kvmclock_offset, albeit in a rather roundabout manner.So what am I missing? Is there actually something wrong with my patch?
For the bug mentioned in the comment not to happen, you must only read TSC and add it as offset to (TSC value, time-of-day data).
Its more than "a roundabout manner".
Quoted 7 lines
Quoted 2 lines
Read the comment again.
I read the comment three more times and even dug through the git history. It seems like what you're saying is that, under certain conditions (which arguably would be bugs in the core Linux timing code),
I don't see that as a bug. Its just a side effect of reading two different clocks (one is CLOCK_MONOTONIC and the other is TSC), and using those two clocks to as a "base + offset".
As the comment explains, if you do that, can't guarantee monotonicity.
Quoted 5 lines
actually calling
ktime_get_boot_ns()could be non-monotonic with respect to the kvmclock timing. Butget_kvmclock_ns()isn't used for VM timing as such -- it's used for the IOCTL interfaces for updating the time offset. So can you explain how my patch is incorrect?
ktime_get_boot_ns() has frequency correction applied, while reading masterclock + TSC offset does not.
So the clock reads differ.
Quoted 39 lines · Andy Lutomirski
Quoted 38 lines · Marcelo Tosatti
Quoted 23 lines · Andy Lutomirski
Quoted 7 lines · Marcelo Tosatti
Quoted 5 lines · Andy Lutomirski
For better or for worse, I'm trying to understand this code. So far, I've come up with this patch:
Quoted 8 lines
Quoted 1 line
Is it correct, or am I missing some subtlety?
The master clock, when initialized, has a pair
masterclockvalues=(TSC value, time-of-day data).
When updating the guest clock, we only update relative to (TSC value) that was read on masterclock initialization.
I don't see the problem. The masterclock data is updated here:
host_tsc_clocksource = kvm_get_time_and_clockread( &ka->master_kernel_ns, &ka->master_cycle_now);Quoted 7 lines
kvm_get_time_and_clockread()gets those values fromdo_monotonic_boot(), which, barring bugs, should causeget_kvmclock_ns()to return exactly the same thing asktime_get_boot_ns()+ka->kvmclock_offset, albeit in a rather roundabout manner.So what am I missing? Is there actually something wrong with my patch?
For the bug mentioned in the comment not to happen, you must only read TSC and add it as offset to (TSC value, time-of-day data).
Its more than "a roundabout manner".
Quoted 7 lines
Quoted 2 lines
Read the comment again.
I read the comment three more times and even dug through the git history. It seems like what you're saying is that, under certain conditions (which arguably would be bugs in the core Linux timing code),
I don't see that as a bug. Its just a side effect of reading two different clocks (one is CLOCK_MONOTONIC and the other is TSC), and using those two clocks to as a "base + offset".
As the comment explains, if you do that, can't guarantee monotonicity.
Quoted 5 lines
actually calling
ktime_get_boot_ns()could be non-monotonic with respect to the kvmclock timing. Butget_kvmclock_ns()isn't used for VM timing as such -- it's used for the IOCTL interfaces for updating the time offset. So can you explain how my patch is incorrect?
ktime_get_boot_ns() has frequency correction applied, while reading masterclock + TSC offset does not.
So the clock reads differ.
Andy Lutomirski
maintainerQuoted 21 lines · Marcelo Tosatti
Quoted 6 lines · Andy Lutomirski
On Mon, Oct 8, 2018 at 8:27 AM Marcelo Tosatti <[email protected]> wrote:
I read the comment three more times and even dug through the git history. It seems like what you're saying is that, under certain conditions (which arguably would be bugs in the core Linux timing code),
I don't see that as a bug. Its just a side effect of reading two different clocks (one is
CLOCK_MONOTONICand the other is TSC), and using those two clocks to as a "base + offset".As the comment explains, if you do that, can't guarantee monotonicity.
Quoted 5 lines
actually calling
ktime_get_boot_ns()could be non-monotonic with respect to the kvmclock timing. Butget_kvmclock_ns()isn't used for VM timing as such -- it's used for the IOCTL interfaces for updating the time offset. So can you explain how my patch is incorrect?
Quoted 5 lines
ktime_get_boot_ns()has frequency correction applied, while reading masterclock + TSC offset does not.So the clock reads differ.
Ah, okay, I finally think I see what's going on. In the kvmclock data exposed to the guest, tsc_shift and tsc_to_system_mul come from tgt_tsc_khz, whereas master_kernel_ns and master_cycle_now come from CLOCK_BOOTTIME. So the kvmclock and kernel clock drift apart at a rate given by the frequency shift and then suddenly agree again every time the pvclock data is updated.
Is there a reason to do it this way?
Andy Lutomirski
maintainerQuoted 21 lines · Marcelo Tosatti
Quoted 6 lines · Andy Lutomirski
On Mon, Oct 8, 2018 at 8:27 AM Marcelo Tosatti <[email protected]> wrote:
I read the comment three more times and even dug through the git history. It seems like what you're saying is that, under certain conditions (which arguably would be bugs in the core Linux timing code),
I don't see that as a bug. Its just a side effect of reading two different clocks (one is
CLOCK_MONOTONICand the other is TSC), and using those two clocks to as a "base + offset".As the comment explains, if you do that, can't guarantee monotonicity.
Quoted 5 lines
actually calling
ktime_get_boot_ns()could be non-monotonic with respect to the kvmclock timing. Butget_kvmclock_ns()isn't used for VM timing as such -- it's used for the IOCTL interfaces for updating the time offset. So can you explain how my patch is incorrect?
Quoted 5 lines
ktime_get_boot_ns()has frequency correction applied, while reading masterclock + TSC offset does not.So the clock reads differ.
Ah, okay, I finally think I see what's going on. In the kvmclock data exposed to the guest, tsc_shift and tsc_to_system_mul come from tgt_tsc_khz, whereas master_kernel_ns and master_cycle_now come from CLOCK_BOOTTIME. So the kvmclock and kernel clock drift apart at a rate given by the frequency shift and then suddenly agree again every time the pvclock data is updated.
Is there a reason to do it this way?
Quoted 28 lines · Andy Lutomirski
Quoted 21 lines · Marcelo Tosatti
Quoted 6 lines · Andy Lutomirski
On Mon, Oct 8, 2018 at 8:27 AM Marcelo Tosatti <[email protected]> wrote:
I read the comment three more times and even dug through the git history. It seems like what you're saying is that, under certain conditions (which arguably would be bugs in the core Linux timing code),
I don't see that as a bug. Its just a side effect of reading two different clocks (one is
CLOCK_MONOTONICand the other is TSC), and using those two clocks to as a "base + offset".As the comment explains, if you do that, can't guarantee monotonicity.
Quoted 5 lines
actually calling
ktime_get_boot_ns()could be non-monotonic with respect to the kvmclock timing. Butget_kvmclock_ns()isn't used for VM timing as such -- it's used for the IOCTL interfaces for updating the time offset. So can you explain how my patch is incorrect?Quoted 5 lines
ktime_get_boot_ns()has frequency correction applied, while reading masterclock + TSC offset does not.So the clock reads differ.
Quoted 6 lines
Ah, okay, I finally think I see what's going on. In the kvmclock data exposed to the guest,
tsc_shiftandtsc_to_system_mulcome fromtgt_tsc_khz, whereasmaster_kernel_nsandmaster_cycle_nowcome fromCLOCK_BOOTTIME. So the kvmclock and kernel clock drift apart at a rate given by the frequency shift and then suddenly agree again every time the pvclock data is updated.
Yes.
Quoted 1 line
Is there a reason to do it this way?
Since pvclock updates which update system_timestamp are expensive (must stop all vcpus), they should be avoided.
So only HW TSC counts, and used as offset against vcpu's tsc_timestamp.
Andy Lutomirski
maintainerQuoted 37 lines · Marcelo Tosatti
Quoted 28 lines · Andy Lutomirski
Quoted 21 lines · Marcelo Tosatti
Quoted 6 lines · Andy Lutomirski
On Mon, Oct 8, 2018 at 8:27 AM Marcelo Tosatti <[email protected]> wrote:
I read the comment three more times and even dug through the git history. It seems like what you're saying is that, under certain conditions (which arguably would be bugs in the core Linux timing code),
I don't see that as a bug. Its just a side effect of reading two different clocks (one is
CLOCK_MONOTONICand the other is TSC), and using those two clocks to as a "base + offset".As the comment explains, if you do that, can't guarantee monotonicity.
Quoted 5 lines
actually calling
ktime_get_boot_ns()could be non-monotonic with respect to the kvmclock timing. Butget_kvmclock_ns()isn't used for VM timing as such -- it's used for the IOCTL interfaces for updating the time offset. So can you explain how my patch is incorrect?Quoted 5 lines
ktime_get_boot_ns()has frequency correction applied, while reading masterclock + TSC offset does not.So the clock reads differ.
Quoted 6 lines
Ah, okay, I finally think I see what's going on. In the kvmclock data exposed to the guest,
tsc_shiftandtsc_to_system_mulcome fromtgt_tsc_khz, whereasmaster_kernel_nsandmaster_cycle_nowcome fromCLOCK_BOOTTIME. So the kvmclock and kernel clock drift apart at a rate given by the frequency shift and then suddenly agree again every time the pvclock data is updated.
Quoted 7 lines
Yes.
Quoted 1 line
Is there a reason to do it this way?
Since pvclock updates which update
system_timestampare expensive (must stop all vcpus), they should be avoided.
Fair enough.
Quoted 1 line
So only HW TSC counts
makes sense.
Quoted 2 lines
, and used as offset against vcpu's
tsc_timestamp.
Why don't you just expose CLOCK_MONTONIC_RAW or CLOCK_MONOTONIC_RAW plus suspend time, though? Then you would actually be tracking a real kernel timekeeping mode, and you wouldn't need all this complicated offsetting work to avoid accidentally going backwards.
Quoted 52 lines · Andy Lutomirski
Quoted 37 lines · Marcelo Tosatti
Quoted 28 lines · Andy Lutomirski
Quoted 21 lines · Marcelo Tosatti
Quoted 6 lines · Andy Lutomirski
On Mon, Oct 8, 2018 at 8:27 AM Marcelo Tosatti <[email protected]> wrote:
I read the comment three more times and even dug through the git history. It seems like what you're saying is that, under certain conditions (which arguably would be bugs in the core Linux timing code),
I don't see that as a bug. Its just a side effect of reading two different clocks (one is
CLOCK_MONOTONICand the other is TSC), and using those two clocks to as a "base + offset".As the comment explains, if you do that, can't guarantee monotonicity.
Quoted 5 lines
actually calling
ktime_get_boot_ns()could be non-monotonic with respect to the kvmclock timing. Butget_kvmclock_ns()isn't used for VM timing as such -- it's used for the IOCTL interfaces for updating the time offset. So can you explain how my patch is incorrect?Quoted 5 lines
ktime_get_boot_ns()has frequency correction applied, while reading masterclock + TSC offset does not.So the clock reads differ.
Quoted 6 lines
Ah, okay, I finally think I see what's going on. In the kvmclock data exposed to the guest,
tsc_shiftandtsc_to_system_mulcome fromtgt_tsc_khz, whereasmaster_kernel_nsandmaster_cycle_nowcome fromCLOCK_BOOTTIME. So the kvmclock and kernel clock drift apart at a rate given by the frequency shift and then suddenly agree again every time the pvclock data is updated.Quoted 7 lines
Yes.
Quoted 1 line
Is there a reason to do it this way?
Since pvclock updates which update
system_timestampare expensive (must stop all vcpus), they should be avoided.Fair enough.
Quoted 1 line
So only HW TSC counts
makes sense.
Quoted 7 lines
Quoted 2 lines
, and used as offset against vcpu's
tsc_timestamp.Why don't you just expose
CLOCK_MONTONIC_RAWorCLOCK_MONOTONIC_RAWplus suspend time, though? Then you would actually be tracking a real kernel timekeeping mode, and you wouldn't need all this complicated offsetting work to avoid accidentally going backwards.
Can you outline how that would work ?
Quoted 52 lines · Andy Lutomirski
Quoted 37 lines · Marcelo Tosatti
Quoted 28 lines · Andy Lutomirski
Quoted 21 lines · Marcelo Tosatti
Quoted 6 lines · Andy Lutomirski
On Mon, Oct 8, 2018 at 8:27 AM Marcelo Tosatti <[email protected]> wrote:
I read the comment three more times and even dug through the git history. It seems like what you're saying is that, under certain conditions (which arguably would be bugs in the core Linux timing code),
I don't see that as a bug. Its just a side effect of reading two different clocks (one is
CLOCK_MONOTONICand the other is TSC), and using those two clocks to as a "base + offset".As the comment explains, if you do that, can't guarantee monotonicity.
Quoted 5 lines
actually calling
ktime_get_boot_ns()could be non-monotonic with respect to the kvmclock timing. Butget_kvmclock_ns()isn't used for VM timing as such -- it's used for the IOCTL interfaces for updating the time offset. So can you explain how my patch is incorrect?Quoted 5 lines
ktime_get_boot_ns()has frequency correction applied, while reading masterclock + TSC offset does not.So the clock reads differ.
Quoted 6 lines
Ah, okay, I finally think I see what's going on. In the kvmclock data exposed to the guest,
tsc_shiftandtsc_to_system_mulcome fromtgt_tsc_khz, whereasmaster_kernel_nsandmaster_cycle_nowcome fromCLOCK_BOOTTIME. So the kvmclock and kernel clock drift apart at a rate given by the frequency shift and then suddenly agree again every time the pvclock data is updated.Quoted 7 lines
Yes.
Quoted 1 line
Is there a reason to do it this way?
Since pvclock updates which update
system_timestampare expensive (must stop all vcpus), they should be avoided.Fair enough.
Quoted 1 line
So only HW TSC counts
makes sense.
Quoted 7 lines
Quoted 2 lines
, and used as offset against vcpu's
tsc_timestamp.Why don't you just expose
CLOCK_MONTONIC_RAWorCLOCK_MONOTONIC_RAWplus suspend time, though? Then you would actually be tracking a real kernel timekeeping mode, and you wouldn't need all this complicated offsetting work to avoid accidentally going backwards.
Can you outline how that would work ?