]> www.infradead.org Git - users/dwmw2/linux.git/commitdiff
KVM: x86: Improve accuracy of KVM clock when TSC scaling is in force
authorDavid Woodhouse <dwmw@amazon.co.uk>
Wed, 17 Apr 2024 18:49:01 +0000 (19:49 +0100)
committerDavid Woodhouse <dwmw@amazon.co.uk>
Tue, 4 Jun 2024 10:31:45 +0000 (11:31 +0100)
The kvm_guest_time_update() function scales the host TSC frequency to
the guest's using kvm_scale_tsc() and the v->arch.l1_tsc_scaling_ratio
scaling ratio previously calculated for that vCPU. Then calcuates the
scaling factors for the KVM clock itself based on that guest TSC
frequency.

However, it uses kHz as the unit when scaling, and then multiplies by
1000 only at the end.

With a host TSC frequency of 3000MHz and a guest set to 2500MHz, the
result of kvm_scale_tsc() will actually come out at 2,499,999kHz. So
the KVM clock advertised to the guest is based on a frequency of
2,499,999,000 Hz.

By using Hz as the unit from the beginning, the KVM clock would be based
on a more accurate frequency of 2,499,999,999 Hz in this example.

Fixes: 78db6a503796 ("KVM: x86: rewrite handling of scaled TSC for kvmclock")
Signed-off-by: David Woodhouse <dwmw@amazon.co.uk>
Reviewed-by: Paul Durrant <paul@xen.org>
arch/x86/include/asm/kvm_host.h
arch/x86/kvm/x86.c
arch/x86/kvm/xen.c

index 9bb2e164c523823236bf417f4b9a028f6a59d0f6..e1b5cf21fcc743793600f1d57af72a4163949528 100644 (file)
@@ -892,7 +892,7 @@ struct kvm_vcpu_arch {
 
        gpa_t time;
        struct pvclock_vcpu_time_info hv_clock;
-       unsigned int hw_tsc_khz;
+       unsigned int hw_tsc_hz;
        struct gfn_to_pfn_cache pv_time;
        /* set guest stopped flag in pvclock flags field */
        bool pvclock_set_guest_stopped_request;
index fa064864ad2ccc9c67301dc83462face40e9459a..48cb3f7f5c24b680722d42940a98b8ff2e898785 100644 (file)
@@ -3205,7 +3205,8 @@ static void kvm_setup_guest_pvclock(struct kvm_vcpu *v,
 
 static int kvm_guest_time_update(struct kvm_vcpu *v)
 {
-       unsigned long flags, tgt_tsc_khz;
+       unsigned long flags;
+       uint64_t tgt_tsc_hz;
        unsigned seq;
        struct kvm_vcpu_arch *vcpu = &v->arch;
        struct kvm_arch *ka = &v->kvm->arch;
@@ -3242,8 +3243,8 @@ static int kvm_guest_time_update(struct kvm_vcpu *v)
 
        /* Keep irq disabled to prevent changes to the clock */
        local_irq_save(flags);
-       tgt_tsc_khz = get_cpu_tsc_khz();
-       if (unlikely(tgt_tsc_khz == 0)) {
+       tgt_tsc_hz = get_cpu_tsc_khz() * 1000LL;
+       if (unlikely(tgt_tsc_hz == 0)) {
                local_irq_restore(flags);
                kvm_make_request(KVM_REQ_CLOCK_UPDATE, v);
                return 1;
@@ -3278,14 +3279,14 @@ static int kvm_guest_time_update(struct kvm_vcpu *v)
        /* With all the info we got, fill in the values */
 
        if (kvm_caps.has_tsc_control)
-               tgt_tsc_khz = kvm_scale_tsc(tgt_tsc_khz,
-                                           v->arch.l1_tsc_scaling_ratio);
+               tgt_tsc_hz = kvm_scale_tsc(tgt_tsc_hz,
+                                          v->arch.l1_tsc_scaling_ratio);
 
-       if (unlikely(vcpu->hw_tsc_khz != tgt_tsc_khz)) {
-               kvm_get_time_scale(NSEC_PER_SEC, tgt_tsc_khz * 1000LL,
+       if (unlikely(vcpu->hw_tsc_hz != tgt_tsc_hz)) {
+               kvm_get_time_scale(NSEC_PER_SEC, tgt_tsc_hz,
                                   &vcpu->hv_clock.tsc_shift,
                                   &vcpu->hv_clock.tsc_to_system_mul);
-               vcpu->hw_tsc_khz = tgt_tsc_khz;
+               vcpu->hw_tsc_hz = tgt_tsc_hz;
                kvm_xen_update_tsc_info(v);
        }
 
index 5a83a8154b79723088d1bc97371d527d6f8f0d8a..014048c226524079ce4e998c1dd56548c2cb13fd 100644 (file)
@@ -2273,7 +2273,7 @@ void kvm_xen_update_tsc_info(struct kvm_vcpu *vcpu)
 
        entry = kvm_find_cpuid_entry_index(vcpu, function, 2);
        if (entry)
-               entry->eax = vcpu->arch.hw_tsc_khz;
+               entry->eax = vcpu->arch.hw_tsc_hz / 1000;
 }
 
 void kvm_xen_init_vm(struct kvm *kvm)