make L2 kvm-clock stable
Subsystems (10)
cover make L2 kvm-clock stable
The goal of the series is to make L2's kvm-clock guest stable when possible.
It's possible when the L2 is running over L1 with a stable paravirtualized clocksource.
To acomplish the goal I can see two approaches:
1. Use currently existing in KVM "monotonic timekeeper shadow".
This approach repeats functionality of time calculation from the kerenl monotonic timekeeper. To acomplish the goal it's needed to add time calculation functions for paravirtualized clocksources to KVM which implies repeating the functionality from the timekeeper.
It seems to me that this approach is not the best one because of code repetition, data shadowing, keeping logic consistent with timekeeper's one. I would consider using the next approach.
2. Use existing timekeeper functionality with extended interface
This approach deligates all time related calculations to the kernel timekeeper instead of having timekeeper shadow and time calculating logic in KVM.
Using this approach will allow to remove the monotonic timekeeping shadow, but ask to change timekeeper interface in a way that will add an ability to return the timestamp value used for time calculations (if any) because this value is needed in KVM (and possibly somewhere else in the future).
This patch series implements the 2nd approach (for now, for x86 only). Could you please give me some feedback about it?
Denis Plotnikov (2):
timekeeper: change interface of clocksource reding functions
KVM: x86: add support of kvm-clock stablity in L2
arch/x86/hyperv/hv_init.c | 4 +- arch/x86/include/asm/kvm_host.h | 2 +- arch/x86/include/asm/pvclock.h | 2 +- arch/x86/kernel/hpet.c | 4 +- arch/x86/kernel/kvmclock.c | 19 ++- arch/x86/kernel/pvclock.c | 11 +- arch/x86/kernel/tsc.c | 7 +- arch/x86/kvm/x86.c | 294 ++++++++++-------------------------- arch/x86/lguest/boot.c | 2 +- arch/x86/platform/uv/uv_time.c | 10 +- arch/x86/xen/time.c | 21 ++- arch/x86/xen/xen-ops.h | 2 +- drivers/clocksource/acpi_pm.c | 12 +- drivers/hv/hv_util.c | 6 +- include/linux/clocksource.h | 9 +- include/linux/timekeeper_internal.h | 2 +- include/linux/timekeeping.h | 34 ++++- kernel/time/clocksource.c | 4 +- kernel/time/jiffies.c | 2 +- kernel/time/timekeeping.c | 66 +++++--- 20 files changed, 221 insertions(+), 292 deletions(-)
Signature · 1 line
2.7.4
RFC PATCH 2/2 KVM: x86: add support of kvm-clock stablity in L2
Get rid of complex shadow monotonic timekeeper support in KVM. Extend and use timekeeper infrastructure instead.
Make kvm-clock stable in L2 using the changed timekeeper
--- arch/x86/include/asm/kvm_host.h | 2 +- arch/x86/kvm/x86.c | 294 +++++++++++----------------------------- include/linux/timekeeping.h | 19 ++- kernel/time/timekeeping.c | 22 ++- 4 files changed, 117 insertions(+), 220 deletions(-)
arch/x86/include/asm/kvm_host.h +1 -1
@@ -781,7 +781,7 @@ struct kvm_arch {u64 cur_tsc_generation;int nr_vcpus_matched_tsc; -spinlock_t pvclock_gtod_sync_lock; +spinlock_t master_clock_lock;bool use_master_clock;u64 master_kernel_ns;u64 master_cycle_now;
arch/x86/kvm/x86.c +78 -216
443 lines not on this page · raw message
include/linux/timekeeping.h +18 -1
@@ -171,7 +171,10 @@ enum tk_offsets { };extern ktime_t ktime_get (void ); +extern ktime_t ktime_get_with_cycles (u64 *cycles);extern ktime_t ktime_get_with_offset (enum tk_offsets offs,u64 *cycles); +extern const seqcount_t *get_tk_seq (void ); +extern int get_tk_mono_clock_mode (void );extern ktime_t ktime_mono_to_any (ktime_t tmono,enum tk_offsets offs);extern ktime_t ktime_get_raw (void );extern u32 ktime_get_resolution_ns (void );
@@ -184,6 +187,10 @@ static inline ktime_t ktime_get_real(void)return ktime_get_with_offset (TK_OFFS_REAL ,NULL ); } +static inline ktime_t ktime_get_real_with_cycles (u64 *cycles) +{ +return ktime_get_with_offset (TK_OFFS_REAL , cycles); +}/** * ktime_get_boottime - Returns monotonic time since boot in ktime_t format *
@@ -220,17 +227,27 @@ static inline u64 ktime_get_ns(void)return ktime_to_ns (ktime_get ()); } +static inline u64 ktime_get_ns_with_cycles (u64 *cycles) +{ +return ktime_to_ns (ktime_get_with_cycles (cycles)); +} +static inline u64 ktime_get_real_ns (void ) {return ktime_to_ns (ktime_get_real ()); } +static inline u64 ktime_get_real_ns_with_cycles (u64 *cycles) +{ +return ktime_to_ns (ktime_get_real_with_cycles (cycles)); +} +static inline u64 ktime_get_boot_ns (void ) {return ktime_to_ns (ktime_get_boottime ()); } -static inline u64 ktime_get_boot_ns_and_cycles (u64 *cycles) +static inline u64 ktime_get_boot_ns_with_cycles (u64 *cycles) {return ktime_to_ns (ktime_get_boottime_and_cycles (cycles)); }
kernel/time/timekeeping.c +20 -2
@@ -717,6 +717,12 @@ EXPORT_SYMBOL(getnstimeofday64);ktime_t ktime_get (void ) { +return ktime_get_with_cycles (NULL ); +} +EXPORT_SYMBOL_GPL (ktime_get); + +ktime_t ktime_get_with_cycles (u64 *cycles) +{struct timekeeper *tk = &tk_core.timekeeper;unsigned int seq;ktime_t base;
@@ -727,13 +733,13 @@ ktime_t ktime_get(void)do { seq =read_seqcount_begin (&tk_core.seq); base = tk->tkr_mono.base; - nsecs =timekeeping_get_ns (&tk->tkr_mono,NULL ); + nsecs =timekeeping_get_ns (&tk->tkr_mono, cycles); }while (read_seqcount_retry (&tk_core.seq, seq));return ktime_add_ns (base, nsecs); } -EXPORT_SYMBOL_GPL (ktime_get); +EXPORT_SYMBOL_GPL (ktime_get_with_cycles);u32 ktime_get_resolution_ns (void ) {
@@ -779,6 +785,18 @@ ktime_t ktime_get_with_offset(enum tk_offsets offs, u64 *tsc_stamp) }EXPORT_SYMBOL_GPL (ktime_get_with_offset); +const seqcount_t *get_tk_seq () +{ +return &tk_core.seq; +} +EXPORT_SYMBOL (get_tk_seq); + +int get_tk_mono_clock_mode () +{ +return tk_core.timekeeper.tkr_mono.clock->archdata.vclock_mode; +} +EXPORT_SYMBOL (get_tk_mono_clock_mode); +/** * ktime_mono_to_any() - convert mononotic time to any other time * @tmono: time to convert.
Signature · 1 line
2.7.4
RFC PATCH 1/2 timekeeper: change interface of clocksource reding functions
In reply to Denis PlotnikovWhen using timekeepeing API in some cases it is useful to return timestamp value used along with the time calculated to use that timestamp value for other purpuses (e.g. in KVM master clock timestamp)
--- arch/x86/hyperv/hv_init.c | 4 ++-- arch/x86/include/asm/pvclock.h | 2 +- arch/x86/kernel/hpet.c | 4 ++-- arch/x86/kernel/kvmclock.c | 19 +++++++++------ arch/x86/kernel/pvclock.c | 11 ++++++--- arch/x86/kernel/tsc.c | 7 ++++-- arch/x86/lguest/boot.c | 2 +- arch/x86/platform/uv/uv_time.c | 10 ++++---- arch/x86/xen/time.c | 21 ++++++++++------- arch/x86/xen/xen-ops.h | 2 +- drivers/clocksource/acpi_pm.c | 12 +++++----- drivers/hv/hv_util.c | 6 ++--- include/linux/clocksource.h | 9 ++++++-- include/linux/timekeeper_internal.h | 2 +- include/linux/timekeeping.h | 17 ++++++++++---- kernel/time/clocksource.c | 4 ++-- kernel/time/jiffies.c | 2 +- kernel/time/timekeeping.c | 46 ++++++++++++++++++------------------- 18 files changed, 106 insertions(+), 74 deletions(-)
arch/x86/hyperv/hv_init.c +2 -2
@@ -36,7 +36,7 @@ struct ms_hyperv_tsc_page *hv_get_tsc_page(void)return tsc_pg; } -static u64 read_hv_clock_tsc (struct clocksource *arg) +static u64 read_hv_clock_tsc (struct clocksource *arg,u64 *tsc_stamp) {u64 current_tick =hv_read_tsc_page (tsc_pg);
@@ -55,7 +55,7 @@ static struct clocksource hyperv_cs_tsc = { };#endif -static u64 read_hv_clock_msr (struct clocksource *arg) +static u64 read_hv_clock_msr (struct clocksource *arg,u64 *tsc_stamp) {u64 current_tick;/*
arch/x86/include/asm/pvclock.h +1 -1
@@ -14,7 +14,7 @@ static inline struct pvclock_vsyscall_time_info *pvclock_pvti_cpu0_va(void)#endif /* some helper functions for xen and kvm pv clock sources */ -u64 pvclock_clocksource_read (struct pvclock_vcpu_time_info *src); +u64 pvclock_clocksource_read (struct pvclock_vcpu_time_info *src,u64 *tsc_stamp);u8 pvclock_read_flags (struct pvclock_vcpu_time_info *src);void pvclock_set_flags (u8 flags);unsigned long pvclock_tsc_khz (struct pvclock_vcpu_time_info *src);
arch/x86/kernel/hpet.c +2 -2
@@ -792,7 +792,7 @@ static union hpet_lock hpet __cacheline_aligned = { { .lock = __ARCH_SPIN_LOCK_UNLOCKED, }, }; -static u64 read_hpet (struct clocksource *cs) +static u64 read_hpet (struct clocksource *cs,u64 *tsc_stamp) {unsigned long flags;union hpet_lock old, new;
@@ -850,7 +850,7 @@ static u64 read_hpet(struct clocksource *cs)/* * For UP or 32-bit. */ -static u64 read_hpet (struct clocksource *cs) +static u64 read_hpet (struct clocksource *cs,u64 *tsc_stamp) {return (u64 )hpet_readl (HPET_COUNTER ); }
arch/x86/kernel/kvmclock.c +12 -7
@@ -82,7 +82,7 @@ static int kvm_set_wallclock(const struct timespec *now)return -1 ; } -static u64 kvm_clock_read (void ) +static u64 kvm_clock_read (u64 *tsc_stamp) {struct pvclock_vcpu_time_info *src;u64 ret;
@@ -91,30 +91,35 @@ static u64 kvm_clock_read(void)preempt_disable_notrace (); cpu =smp_processor_id (); src = &hv_clock[cpu].pvti; - ret =pvclock_clocksource_read (src); + ret =pvclock_clocksource_read (src, tsc_stamp);preempt_enable_notrace ();return ret; } -static u64 kvm_clock_get_cycles (struct clocksource *cs) +static u64 kvm_clock_get_cycles (struct clocksource *cs,u64 *tsc_stamp) { -return kvm_clock_read (); +return kvm_clock_read (tsc_stamp); +} + +static u64 kvm_clock_read_single (void ) +{ +return kvm_clock_read (NULL ); }static u64 kvm_sched_clock_read (void ) { -return kvm_clock_read () - kvm_sched_clock_offset; +return kvm_clock_read_single () - kvm_sched_clock_offset; }static inline void kvm_sched_clock_init (bool stable) {if (!stable) { - pv_time_ops.sched_clock = kvm_clock_read; + pv_time_ops.sched_clock = kvm_clock_read_single;clear_sched_clock_stable ();return ; } - kvm_sched_clock_offset =kvm_clock_read (); + kvm_sched_clock_offset =kvm_clock_read_single (); pv_time_ops.sched_clock = kvm_sched_clock_read; printk(KERN_INFO "kvm-clock: using sched offset of %llu cycles\n",
arch/x86/kernel/pvclock.c +8 -3
@@ -72,17 +72,22 @@ u8 pvclock_read_flags(struct pvclock_vcpu_time_info *src)return flags & valid_flags; } +EXPORT_SYMBOL (pvclock_read_flags); -u64 pvclock_clocksource_read (struct pvclock_vcpu_time_info *src) +u64 pvclock_clocksource_read (struct pvclock_vcpu_time_info *src,u64 *tsc_stamp) {unsigned version;u64 ret;u64 last;u8 flags; +u64 tsc;do { version =pvclock_read_begin (src); - ret =__pvclock_read_cycles (src,rdtsc_ordered ()); + tsc =rdtsc_ordered (); +if (tsc_stamp) + *tsc_stamp = tsc; + ret =__pvclock_read_cycles (src, tsc); flags = src->flags; }while (pvclock_read_retry (src, version));
@@ -136,7 +141,7 @@ void pvclock_read_wallclock(struct pvclock_wall_clock *wall_clock,rmb ();/* fetch time before checking version */ }while ((wall_clock->version &1 ) || (version != wall_clock->version)); - delta =pvclock_clocksource_read (vcpu_time);/* time since system boot */ + delta =pvclock_clocksource_read (vcpu_time,NULL );/* time since system boot */ delta += now.tv_sec * (u64 )NSEC_PER_SEC + now.tv_nsec; now.tv_nsec =do_div (delta,NSEC_PER_SEC );
arch/x86/kernel/tsc.c +5 -2
@@ -1110,9 +1110,12 @@ static void tsc_resume(struct clocksource *cs)* checking the result of read_tsc() - cycle_last for being negative. * That works because CLOCKSOURCE_MASK(64) does not mask out any bit. */ -static u64 read_tsc (struct clocksource *cs) +static u64 read_tsc (struct clocksource *cs,u64 *tsc_stamp) { -return (u64 )rdtsc_ordered (); +u64 tsc = (u64 )rdtsc_ordered (); +if (tsc_stamp) + *tsc_stamp = tsc; +return tsc; }static void tsc_cs_mark_unstable (struct clocksource *cs)
arch/x86/lguest/boot.c +1 -1
@@ -916,7 +916,7 @@ static unsigned long lguest_tsc_khz(void)* If we can't use the TSC, the kernel falls back to our lower-priority * "lguest_clock", where we read the time value given to us by the Host. */ -static u64 lguest_clock_read (struct clocksource *cs) +static u64 lguest_clock_read (struct clocksource *cs,u64 *tsc_stamp) {unsigned long sec, nsec;
arch/x86/platform/uv/uv_time.c +5 -5
@@ -30,7 +30,7 @@#define RTC_NAME "sgi_rtc" -static u64 uv_read_rtc (struct clocksource *cs); +static u64 uv_read_rtc (struct clocksource *cs,u64 *tsc_stamp);static int uv_rtc_next_event (unsigned long ,struct clock_event_device *);static int uv_rtc_shutdown (struct clock_event_device *evt);
@@ -133,7 +133,7 @@ static int uv_setup_intr(int cpu, u64 expires)/* Initialize comparator value */ uv_write_global_mmr64 (pnode,UVH_INT_CMPB , expires); -if (uv_read_rtc (NULL ) <= expires) +if (uv_read_rtc (NULL ,NULL ) <= expires)return 0 ;return !uv_intr_pending (pnode);
@@ -269,7 +269,7 @@ static int uv_rtc_unset_timer(int cpu, int force)spin_lock_irqsave (&head->lock, flags); -if ((head->next_cpu == bcpu &&uv_read_rtc (NULL ) >= *t) || force) +if ((head->next_cpu == bcpu &&uv_read_rtc (NULL ,NULL ) >= *t) || force) rc =1 ;if (rc) {
@@ -296,7 +296,7 @@ static int uv_rtc_unset_timer(int cpu, int force)* cachelines of it's own page. This allows faster simultaneous reads * from a given socket. */ -static u64 uv_read_rtc (struct clocksource *cs) +static u64 uv_read_rtc (struct clocksource *cs,u64 *tsc_stamp) {unsigned long offset;
@@ -316,7 +316,7 @@ static int uv_rtc_next_event(unsigned long delta, {int ced_cpu =cpumask_first (ced->cpumask); -return uv_rtc_set_timer (ced_cpu, delta +uv_read_rtc (NULL )); +return uv_rtc_set_timer (ced_cpu, delta +uv_read_rtc (NULL ,NULL )); }/*
arch/x86/xen/time.c +13 -8
@@ -39,21 +39,21 @@ static unsigned long xen_tsc_khz(void)return pvclock_tsc_khz (info); } -u64 xen_clocksource_read (void ) +u64 xen_clocksource_read (u64 *tsc_stamp) {struct pvclock_vcpu_time_info *src;u64 ret;preempt_disable_notrace (); src = &__this_cpu_read (xen_vcpu)->time; - ret =pvclock_clocksource_read (src); + ret =pvclock_clocksource_read (src, tsc_stamp);preempt_enable_notrace ();return ret; } -static u64 xen_clocksource_get_cycles (struct clocksource *cs) +static u64 xen_clocksource_get_cycles (struct clocksource *cs,u64 *tsc_stamp) { -return xen_clocksource_read (); +return xen_clocksource_read (tsc_stamp); }static void xen_read_wallclock (struct timespec *ts)
@@ -105,12 +105,12 @@ static int xen_pvclock_gtod_notify(struct notifier_block *nb, op.u.settime64.mbz =0 ; op.u.settime64.secs = now.tv_sec; op.u.settime64.nsecs = now.tv_nsec; - op.u.settime64.system_time =xen_clocksource_read (); + op.u.settime64.system_time =xen_clocksource_read (NULL ); }else { op.cmd = XENPF_settime32; op.u.settime32.secs = now.tv_sec; op.u.settime32.nsecs = now.tv_nsec; - op.u.settime32.system_time =xen_clocksource_read (); + op.u.settime32.system_time =xen_clocksource_read (NULL ); } ret =HYPERVISOR_platform_op (&op);
@@ -178,7 +178,7 @@ static struct clocksource xen_clocksource __read_mostly = {*/ static s64 get_abs_timeout (unsigned long delta) { -return xen_clocksource_read () + delta; +return xen_clocksource_read (NULL ) + delta; }static int xen_timerop_shutdown (struct clock_event_device *evt)
@@ -366,8 +366,13 @@ void xen_timer_resume(void) } } +static u64 xen_clocksource_read_data (void ) +{ +return xen_clocksource_read (NULL ); +} +static const struct pv_time_ops xen_time_ops__initconst = { - .sched_clock = xen_clocksource_read, + .sched_clock = xen_clocksource_read_data, .steal_clock = xen_steal_clock, };
arch/x86/xen/xen-ops.h +1 -1
@@ -67,7 +67,7 @@ void xen_init_irq_ops(void);void xen_setup_timer (int cpu);void xen_setup_runstate_info (int cpu);void xen_teardown_timer (int cpu); -u64 xen_clocksource_read (void ); +u64 xen_clocksource_read (u64 *tsc_stamp);void xen_setup_cpu_clockevents (void );void __init xen_init_time_ops (void );void __init xen_hvm_init_time_ops (void );
drivers/clocksource/acpi_pm.c +6 -6
@@ -58,7 +58,7 @@ u32 acpi_pm_read_verified(void)return v2; } -static u64 acpi_pm_read (struct clocksource *cs) +static u64 acpi_pm_read (struct clocksource *cs,u64 *tsc_stamp) {return (u64 )read_pmtmr (); }
@@ -81,7 +81,7 @@ static int __init acpi_pm_good_setup(char *__str) }__setup ("acpi_pm_good" , acpi_pm_good_setup); -static u64 acpi_pm_read_slow (struct clocksource *cs) +static u64 acpi_pm_read_slow (struct clocksource *cs,u64 *tsc_stamp) {return (u64 )acpi_pm_read_verified (); }
@@ -149,9 +149,9 @@ static int verify_pmtmr_rate(void)unsigned long count, delta;mach_prepare_counter (); - value1 = clocksource_acpi_pm.read (&clocksource_acpi_pm); + value1 = clocksource_acpi_pm.read (&clocksource_acpi_pm,NULL );mach_countup (&count); - value2 = clocksource_acpi_pm.read (&clocksource_acpi_pm); + value2 = clocksource_acpi_pm.read (&clocksource_acpi_pm,NULL ); delta = (value2 - value1) &ACPI_PM_MASK ;/* Check that the PMTMR delta is within 5% of what we expect */
@@ -184,9 +184,9 @@ static int __init init_acpi_pm_clocksource(void)/* "verify" this timing source: */ for (j =0 ; j <ACPI_PM_MONOTONICITY_CHECKS ; j++) {udelay (100 * j); - value1 = clocksource_acpi_pm.read (&clocksource_acpi_pm); + value1 = clocksource_acpi_pm.read (&clocksource_acpi_pm,NULL );for (i =0 ; i <ACPI_PM_READ_CHECKS ; i++) { - value2 = clocksource_acpi_pm.read (&clocksource_acpi_pm); + value2 = clocksource_acpi_pm.read (&clocksource_acpi_pm,NULL );if (value2 == value1)continue ;if (value2 > value1)
drivers/hv/hv_util.c +3 -3
@@ -218,7 +218,7 @@ static void hv_set_host_time(struct work_struct *work) wrk = container_of(work, struct adj_time_work, work); - reftime = hyperv_cs->read (hyperv_cs); + reftime = hyperv_cs->read (hyperv_cs,NULL ); newtime = wrk->host_time + (reftime - wrk->ref_time); host_ts =ns_to_timespec64 ((newtime -WLTIMEDELTA ) *100 );
@@ -278,7 +278,7 @@ static inline void adj_guesttime(u64 hosttime, u64 reftime, u8 adj_flags)*/ spin_lock_irqsave (&host_ts.lock, flags); - cur_reftime = hyperv_cs->read (hyperv_cs); + cur_reftime = hyperv_cs->read (hyperv_cs,NULL ); host_ts.host_time = hosttime; host_ts.ref_time = cur_reftime;ktime_get_snapshot (&host_ts.snap);
@@ -530,7 +530,7 @@ static int hv_ptp_gettime(struct ptp_clock_info *info, struct timespec64 *ts)u64 newtime, reftime;spin_lock_irqsave (&host_ts.lock, flags); - reftime = hyperv_cs->read (hyperv_cs); + reftime = hyperv_cs->read (hyperv_cs,NULL ); newtime = host_ts.host_time + (reftime - host_ts.ref_time); *ts =ns_to_timespec64 ((newtime -WLTIMEDELTA ) *100 );spin_unlock_irqrestore (&host_ts.lock, flags);
include/linux/clocksource.h +7 -2
@@ -48,7 +48,12 @@ struct module;* 400-499: Perfect * The ideal clocksource. A must-use where * available. -* @read: returns a cycle value, passes clocksource as argument +* @read: returns a cycle value, passes clocksource and +* a pointer where tsc value is stored which was used in +* calcualtion of the cycle value if any, +* otherwise the pointer value is untouched. Must check +* if the pointer is not-NULL +* this value is used in kvm code for storing tsc_timestamp * @enable: optional function to enable the clocksource * @disable: optional function to disable the clocksource * @mask: bitmask for two's complement
@@ -77,7 +82,7 @@ struct module;* structure. */ struct clocksource { -u64 (*read)(struct clocksource *cs); +u64 (*read)(struct clocksource *cs,u64 *tsc_stamp);u64 mask;u32 mult;u32 shift;
include/linux/timekeeper_internal.h +1 -1
@@ -29,7 +29,7 @@*/ struct tk_read_base {struct clocksource *clock; -u64 (*read)(struct clocksource *cs); +u64 (*read)(struct clocksource *cs,u64 *tsc_stamp);u64 mask;u64 cycle_last;u32 mult;
include/linux/timekeeping.h +13 -4
@@ -171,7 +171,7 @@ enum tk_offsets { };extern ktime_t ktime_get (void ); -extern ktime_t ktime_get_with_offset (enum tk_offsets offs); +extern ktime_t ktime_get_with_offset (enum tk_offsets offs,u64 *cycles);extern ktime_t ktime_mono_to_any (ktime_t tmono,enum tk_offsets offs);extern ktime_t ktime_get_raw (void );extern u32 ktime_get_resolution_ns (void );
@@ -181,7 +181,7 @@ extern u32 ktime_get_resolution_ns(void);*/ static inline ktime_t ktime_get_real (void ) { -return ktime_get_with_offset (TK_OFFS_REAL ); +return ktime_get_with_offset (TK_OFFS_REAL ,NULL ); }/**
@@ -192,15 +192,19 @@ static inline ktime_t ktime_get_real(void)*/ static inline ktime_t ktime_get_boottime (void ) { -return ktime_get_with_offset (TK_OFFS_BOOT ); +return ktime_get_with_offset (TK_OFFS_BOOT ,NULL ); } +static inline ktime_t ktime_get_boottime_and_cycles (u64 *cycles) +{ +return ktime_get_with_offset (TK_OFFS_BOOT , cycles); +}/** * ktime_get_clocktai - Returns the TAI time of day in ktime_t format */ static inline ktime_t ktime_get_clocktai (void ) { -return ktime_get_with_offset (TK_OFFS_TAI ); +return ktime_get_with_offset (TK_OFFS_TAI ,NULL ); }/**
@@ -226,6 +230,11 @@ static inline u64 ktime_get_boot_ns(void)return ktime_to_ns (ktime_get_boottime ()); } +static inline u64 ktime_get_boot_ns_and_cycles (u64 *cycles) +{ +return ktime_to_ns (ktime_get_boottime_and_cycles (cycles)); +} +static inline u64 ktime_get_tai_ns (void ) {return ktime_to_ns (ktime_get_clocktai ());
kernel/time/clocksource.c +2 -2
@@ -194,8 +194,8 @@ static void clocksource_watchdog(unsigned long data) }local_irq_disable (); - csnow = cs->read (cs); - wdnow = watchdog->read (watchdog); + csnow = cs->read (cs,NULL ); + wdnow = watchdog->read (watchdog,NULL );local_irq_enable ();/* Clocksource initialized ? */
kernel/time/jiffies.c +1 -1
@@ -48,7 +48,7 @@#define JIFFIES_SHIFT 8#endif -static u64 jiffies_read (struct clocksource *cs) +static u64 jiffies_read (struct clocksource *cs,u64 *tsc_stamp) {return (u64 ) jiffies; }
kernel/time/timekeeping.c +23 -23
179 lines not on this page · raw message
Signature · 1 line
2.7.4
ping!
Quoted 56 lines · Denis Plotnikov
The goal of the series is to make L2's kvm-clock guest stable when possible.
It's possible when the L2 is running over L1 with a stable paravirtualized clocksource.
To acomplish the goal I can see two approaches:
1. Use currently existing in KVM "monotonic timekeeper shadow".
This approach repeats functionality of time calculation from the kerenl monotonic timekeeper. To acomplish the goal it's needed to add time calculation functions for paravirtualized clocksources to KVM which implies repeating the functionality from the timekeeper.
It seems to me that this approach is not the best one because of code repetition, data shadowing, keeping logic consistent with timekeeper's one. I would consider using the next approach.2. Use existing timekeeper functionality with extended interface
This approach deligates all time related calculations to the kernel timekeeper instead of having timekeeper shadow and time calculating logic in KVM.
Using this approach will allow to remove the monotonic timekeeping shadow, but ask to change timekeeper interface in a way that will add an ability to return the timestamp value used for time calculations (if any) because this value is needed in KVM (and possibly somewhere else in the future).This patch series implements the 2nd approach (for now, for x86 only). Could you please give me some feedback about it?
Denis Plotnikov (2):
timekeeper: change interface of clocksource reding functions
KVM: x86: add support of kvm-clock stablity in L2arch/x86/hyperv/hv_init.c | 4 +- arch/x86/include/asm/kvm_host.h | 2 +- arch/x86/include/asm/pvclock.h | 2 +- arch/x86/kernel/hpet.c | 4 +- arch/x86/kernel/kvmclock.c | 19 ++- arch/x86/kernel/pvclock.c | 11 +- arch/x86/kernel/tsc.c | 7 +- arch/x86/kvm/x86.c | 294 ++++++++++-------------------------- arch/x86/lguest/boot.c | 2 +- arch/x86/platform/uv/uv_time.c | 10 +- arch/x86/xen/time.c | 21 ++- arch/x86/xen/xen-ops.h | 2 +- drivers/clocksource/acpi_pm.c | 12 +- drivers/hv/hv_util.c | 6 +- include/linux/clocksource.h | 9 +- include/linux/timekeeper_internal.h | 2 +- include/linux/timekeeping.h | 34 ++++- kernel/time/clocksource.c | 4 +- kernel/time/jiffies.c | 2 +- kernel/time/timekeeping.c | 66 +++++--- 20 files changed, 221 insertions(+), 292 deletions(-)
Signature · 2 lines
Best, Denis
Paolo Bonzini
maintainerQuoted 1 line · Denis Plotnikov
ping!
Sorry, I have not looked at this patch since I was busy with pre-merge window testing and this of course won't be in 4.13 (the merge window has opened already).
Paolo
Quoted 3 lines · Denis Plotnikov
Quoted 2 lines
The goal of the series is to make L2's kvm-clock guest stable when possible.
ping! ping!
Quoted 67 lines · Denis Plotnikov
ping!
Quoted 63 lines · Denis Plotnikov
The goal of the series is to make L2's kvm-clock guest stable when possible.
It's possible when the L2 is running over L1 with a stable paravirtualized clocksource.
To acomplish the goal I can see two approaches:
1. Use currently existing in KVM "monotonic timekeeper shadow".
This approach repeats functionality of time calculation from the
kerenl
monotonic timekeeper. To acomplish the goal it's needed to add time calculation functions for paravirtualized clocksources to KVM which implies repeating the functionality from the timekeeper. It seems to me that this approach is not the best one because of code repetition, data shadowing, keeping logic consistent with
timekeeper's one.
I would consider using the next approach.2. Use existing timekeeper functionality with extended interface
This approach deligates all time related calculations to the kernel timekeeper instead of having timekeeper shadow and time
calculating logic
in KVM.
Using this approach will allow to remove the monotonic timekeeping
shadow,
but ask to change timekeeper interface in a way that will add an
ability
to return the timestamp value used for time calculations (if any)
because this
value is needed in KVM (and possibly somewhere else in the future).This patch series implements the 2nd approach (for now, for x86 only). Could you please give me some feedback about it?
Denis Plotnikov (2):
timekeeper: change interface of clocksource reding functions
KVM: x86: add support of kvm-clock stablity in L2arch/x86/hyperv/hv_init.c | 4 +- arch/x86/include/asm/kvm_host.h | 2 +- arch/x86/include/asm/pvclock.h | 2 +- arch/x86/kernel/hpet.c | 4 +- arch/x86/kernel/kvmclock.c | 19 ++- arch/x86/kernel/pvclock.c | 11 +- arch/x86/kernel/tsc.c | 7 +- arch/x86/kvm/x86.c | 294 ++++++++++-------------------------- arch/x86/lguest/boot.c | 2 +- arch/x86/platform/uv/uv_time.c | 10 +- arch/x86/xen/time.c | 21 ++- arch/x86/xen/xen-ops.h | 2 +- drivers/clocksource/acpi_pm.c | 12 +- drivers/hv/hv_util.c | 6 +- include/linux/clocksource.h | 9 +- include/linux/timekeeper_internal.h | 2 +- include/linux/timekeeping.h | 34 ++++- kernel/time/clocksource.c | 4 +- kernel/time/jiffies.c | 2 +- kernel/time/timekeeping.c | 66 +++++--- 20 files changed, 221 insertions(+), 292 deletions(-)
Signature · 2 lines
Best, Denis
Re: [RFC PATCH 1/2] timekeeper: change interface of clocksource reding functions
In reply to Denis Plotnikov2017-06-28 13:55+0300, Denis Plotnikov:
Quoted 3 lines
When using timekeepeing API in some cases it is useful to return timestamp value used along with the time calculated to use that timestamp value for other purpuses (e.g. in KVM master clock timestamp)
Makes sense. What I don't like about this interface is the TSC centric approach in a code that isn't even x86 specific -- other architectures might have a similar counter they'd like to use.
Could we get it a bit more generic?
At least returning the type of the clock and its counter value. (kvmclock is a bit problematic for the generic solution, because it is
TSC based, but should pass through the kvmclock value if we were going to make it clean ...)
---
Actually, we might be overengineering it. With the master clock we have now, I'm thinking that the gtod is not crucial and we could as well be using something like:
static bool kvm_get_time_and_clockread(s64 *kernel_ns, u64 *cycle_now)
{
local_irq_save(flags);
*cycle_now = rdtsc();
*kernel_ns = ktime_get_boot_ns();
local_irq_restore(flags);
return todo_tsc_hardware_is_good_enough();
}because kvmclock is shifted when master clock is recomputed or disabled, so it should never happen. (And if it happens, then this bit of imprecision doesn't matter much.)
The host doesn't need to use TSC either, because master clock uses TSC frequency that differs from boot_ns -- we only need to be sure that the TSC hardware doesn't change its frequency.
The small delta introduced with this (rdtsc <-> ktime_get_boot_ns) is the same that the guest would see if we didn't enable master clock. kvmclock updates are then normally using the same cycle_now/kernel_ns pair to remain stable.
Re: [RFC PATCH 2/2] KVM: x86: add support of kvm-clock stablity in L2
In reply to Denis Plotnikov2017-06-28 13:55+0300, Denis Plotnikov:
Quoted 4 lines
Get rid of complex shadow monotonic timekeeper support in KVM. Extend and use timekeeper infrastructure instead.
Make kvm-clock stable in L2 using the changed timekeeper
I see this patch as two logical changes:
1) refactoring that drops host time caching (gtod)
2) stable kvmclock for L2 when L1 is using kvmclock
Doing it in two patches would definitely be nicer and would allow us to focus on the (less controversial) L2 enablement.
About (1), the removed pvclock_gtod_notify() also verifid whether the host is using TSC and disabled master clock otherwise. Other changes don't explain why it's not needed anymore -- is it because any changes that make TSC unusable would be caught elsewhere?
About (2), it seems that there is no event in case the kvmclock is not reliable anymore. We probably don't need it now, but it would be good to have it thought out -- would that use a new notifier?
Thanks.
Re: [RFC PATCH 2/2] KVM: x86: add support of kvm-clock stablity in L2
2017-07-10 15:25+0200, Radim Krčmář:
Quoted 6 lines
2017-06-28 13:55+0300, Denis Plotnikov:
Quoted 4 lines
Get rid of complex shadow monotonic timekeeper support in KVM. Extend and use timekeeper infrastructure instead.
Make kvm-clock stable in L2 using the changed timekeeper
Quoted 7 lines
I see this patch as two logical changes:
1) refactoring that drops host time caching (gtod)
2) stable kvmclock for L2 when L1 is using kvmclockDoing it in two patches would definitely be nicer and would allow us to focus on the (less controversial) L2 enablement.
I just realized that (2) has more complex dependency on (1): we'd need extra code to get boot_ns from TSC via kvmclock, so deciding the refactoring first was a good call.
Re: [RFC PATCH 1/2] timekeeper: change interface of clocksource reding functions
In reply to Radim KrčmářQuoted 10 lines · Radim Krčmář
2017-06-28 13:55+0300, Denis Plotnikov:
Quoted 3 lines
When using timekeepeing API in some cases it is useful to return timestamp value used along with the time calculated to use that timestamp value for other purpuses (e.g. in KVM master clock timestamp)
Makes sense. What I don't like about this interface is the TSC centric approach in a code that isn't even x86 specific -- other architectures might have a similar counter they'd like to use.
Could we get it a bit more generic?
Yes, sure
Quoted 28 lines
At least returning the type of the clock and its counter value. (kvmclock is a bit problematic for the generic solution, because it is
TSC based, but should pass through the kvmclock value if we were going to make it clean ...)---
Actually, we might be overengineering it. With the master clock we have now, I'm thinking that the gtod is not crucial and we could as well be using something like:static bool kvm_get_time_and_clockread(s64 *kernel_ns, u64 *cycle_now) { local_irq_save(flags); *cycle_now = rdtsc(); *kernel_ns = ktime_get_boot_ns(); local_irq_restore(flags); return todo_tsc_hardware_is_good_enough(); }because kvmclock is shifted when master clock is recomputed or disabled, so it should never happen. (And if it happens, then this bit of imprecision doesn't matter much.)
The host doesn't need to use TSC either, because master clock uses TSC frequency that differs fromboot_ns-- we only need to be sure that the TSC hardware doesn't change its frequency.
Quoted 5 lines
The small delta introduced with this (rdtsc <->
ktime_get_boot_ns) is the same that the guest would see if we didn't enable master clock. kvmclock updates are then normally using the same cycle_now/kernel_ns pair to remain stable.
I thought about that (and actually tried that first). The concern is about L2 guests running over L1 KVM where the margin between cycle_now and kernel_ns is unpredictable because L1 KVM can be scheduled at any time for relatively long period. That's why I decided to rework timekeeper and get kernel_time
and corresponding cycle value from there
Signature · 2 lines
Best, Denis
Re: [RFC PATCH 2/2] KVM: x86: add support of kvm-clock stablity in L2
In reply to Radim KrčmářQuoted 12 lines · Radim Krčmář
2017-07-10 15:25+0200, Radim Krčmář:
Quoted 11 lines
2017-06-28 13:55+0300, Denis Plotnikov:
Quoted 4 lines
Get rid of complex shadow monotonic timekeeper support in KVM. Extend and use timekeeper infrastructure instead.
Make kvm-clock stable in L2 using the changed timekeeper
I see this patch as two logical changes:
1) refactoring that drops host time caching (gtod)
2) stable kvmclock for L2 when L1 is using kvmclock
Quoted 7 lines
Quoted 2 lines
Doing it in two patches would definitely be nicer and would allow us to focus on the (less controversial) L2 enablement.
I just realized that (2) has more complex dependency on (1): we'd need extra code to get
boot_nsfrom TSC via kvmclock, so deciding the refactoring first was a good call.
I agree and will return with the set of patches shortly. Thanks for reviewing!
Signature · 2 lines
Best, Denis