KVM: x86: hyper-v: Fix TSC page update after KVM_SET_CLOCK(0) call
v3 cover KVM: x86: hyper-v: Fix TSC page update after KVM_SET_CLOCK(0) call
Changes since v2:
- Fix the issue by using master_kernel_ns/get_kvmclock_base_ns() instead of get_kvmclock_ns() when handling KVM_SET_CLOCK.
- Rebase on Paolo's "KVM: x86: fix lockdep splat due to Xen runstate update" series and use spin_lock_irq()/spin_unlock_irq() [Paolo]
Original description:
I discovered that after KVM_SET_CLOCK(0) TSC page value in the guest can go through the roof and apparently we have a signedness issue when the update is performed. Fix the issue and add a selftest.
Vitaly Kuznetsov (2):
KVM: x86: Prevent 'hv_clock->system_time' from going negative in
kvm_guest_time_update()
selftests: kvm: Check that TSC page value is small after
KVM_SET_CLOCK(0)
arch/x86/kvm/x86.c | 19 +++++++++++++++++-- .../selftests/kvm/x86_64/hyperv_clock.c | 13 +++++++++++-- 2 files changed, 28 insertions(+), 4 deletions(-)
Signature · 1 line
2.30.2
PATCH v3 1/2 KVM: x86: Prevent 'hv_clock->system_time' from going negative in kvm_guest_time_update()
When guest time is reset with KVM_SET_CLOCK(0), it is possible for 'hv_clock->system_time' to become a small negative number. This happens because in KVM_SET_CLOCK handling we set 'kvm->arch.kvmclock_offset' based on get_kvmclock_ns(kvm) but when KVM_REQ_CLOCK_UPDATE is handled, kvm_guest_time_update() does (masterclock in use case):
hv_clock.system_time = ka->master_kernel_ns + v->kvm->arch.kvmclock_offset;
And 'master_kernel_ns' represents the last time when masterclock got updated, it can precede KVM_SET_CLOCK() call. Normally, this is not a problem, the difference is very small, e.g. I'm observing hv_clock.system_time = -70 ns. The issue comes from the fact that 'hv_clock.system_time' is stored as unsigned and 'system_time / 100' in compute_tsc_page_parameters() becomes a very big number.
Use 'master_kernel_ns' instead of get_kvmclock_ns() when masterclock is in use and get_kvmclock_base_ns() when it's not to prevent 'system_time' from going negative.
--- arch/x86/kvm/x86.c | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-)
arch/x86/kvm/x86.c +17 -2
@@ -5728,6 +5728,7 @@ long kvm_arch_vm_ioctl(struct file *filp, }#endif case KVM_SET_CLOCK : { +struct kvm_arch *ka = &kvm->arch;struct kvm_clock_data user_ns;u64 now_ns;
@@ -5746,8 +5747,22 @@ long kvm_arch_vm_ioctl(struct file *filp,* pvclock_update_vm_gtod_copy(). */ kvm_gen_update_masterclock (kvm); - now_ns =get_kvmclock_ns (kvm); - kvm->arch.kvmclock_offset += user_ns.clock - now_ns; + +/* +* This pairs with kvm_guest_time_update(): when masterclock is +* in use, we use master_kernel_ns + kvmclock_offset to set +* unsigned 'system_time' so if we use get_kvmclock_ns() (which +* is slightly ahead) here we risk going negative on unsigned +* 'system_time' when 'user_ns.clock' is very small. +*/ +spin_lock_irq (&ka->pvclock_gtod_sync_lock); +if (kvm->arch.use_master_clock) + now_ns = ka->master_kernel_ns; +else + now_ns =get_kvmclock_base_ns (); + ka->kvmclock_offset = user_ns.clock - now_ns; +spin_unlock_irq (&ka->pvclock_gtod_sync_lock); +kvm_make_all_cpus_request (kvm,KVM_REQ_CLOCK_UPDATE );break ; }
Signature · 1 line
2.30.2
PATCH v3 2/2 selftests: kvm: Check that TSC page value is small after KVM_SET_CLOCK(0)
In reply to Vitaly KuznetsovAdd a test for the issue when KVM_SET_CLOCK(0) call could cause TSC page value to go very big because of a signedness issue around hv_clock->system_time.
--- tools/testing/selftests/kvm/x86_64/hyperv_clock.c | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-)
tools/testing/selftests/kvm/x86_64/hyperv_clock.c +11 -2
@@ -80,19 +80,24 @@ static inline void check_tsc_msr_rdtsc(void)GUEST_ASSERT (delta_ns *100 < (t2 - t1) *100 ); } +static inline u64 get_tscpage_ts (struct ms_hyperv_tsc_page *tsc_page) +{ +return mul_u64_u64_shr64 (rdtsc (), tsc_page->tsc_scale) + tsc_page->tsc_offset; +} +static inline void check_tsc_msr_tsc_page (struct ms_hyperv_tsc_page *tsc_page) {u64 r1, r2, t1, t2;/* Compare TSC page clocksource with HV_X64_MSR_TIME_REF_COUNT */ - t1 =mul_u64_u64_shr64 (rdtsc (), tsc_page->tsc_scale) + tsc_page->tsc_offset; + t1 =get_tscpage_ts (tsc_page); r1 =rdmsr (HV_X64_MSR_TIME_REF_COUNT );/* 10 ms tolerance */ GUEST_ASSERT (r1 >= t1 && r1 - t1 <100000 );nop_loop (); - t2 =mul_u64_u64_shr64 (rdtsc (), tsc_page->tsc_scale) + tsc_page->tsc_offset; + t2 =get_tscpage_ts (tsc_page); r2 =rdmsr (HV_X64_MSR_TIME_REF_COUNT );GUEST_ASSERT (r2 >= t1 && r2 - t2 <100000 ); }
@@ -130,7 +135,11 @@ static void guest_main(struct ms_hyperv_tsc_page *tsc_page, vm_paddr_t tsc_page_ tsc_offset = tsc_page->tsc_offset;/* Call KVM_SET_CLOCK from userspace, check that TSC page was updated */ +GUEST_SYNC (7 ); +/* Sanity check TSC page timestamp, it should be close to 0 */ +GUEST_ASSERT (get_tscpage_ts (tsc_page) <100000 ); +GUEST_ASSERT (tsc_page->tsc_offset != tsc_offset);nop_loop ();
Signature · 1 line
2.30.2
Quoted 16 lines · Vitaly Kuznetsov
Changes since v2:
- Fix the issue by using master_kernel_ns/get_kvmclock_base_ns() instead ofget_kvmclock_ns()when handlingKVM_SET_CLOCK.
- Rebase on Paolo's "KVM: x86: fix lockdep splat due to Xen runstate update" series and use spin_lock_irq()/spin_unlock_irq() [Paolo]Original description:
I discovered that after
KVM_SET_CLOCK(0)TSC page value in the guest can go through the roof and apparently we have a signedness issue when the update is performed. Fix the issue and add a selftest.Vitaly Kuznetsov (2):
KVM: x86: Prevent 'hv_clock->system_time' from going negative in
kvm_guest_time_update()
selftests: kvm: Check that TSC page value is small after
Quoted 6 lines
KVM_SET_CLOCK(0)arch/x86/kvm/x86.c | 19 +++++++++++++++++-- .../selftests/kvm/x86_64/hyperv_clock.c | 13 +++++++++++-- 2 files changed, 28 insertions(+), 4 deletions(-)
Queued, thanks.
Paolo