Re: [PATCH 02/10] KVM: x86: Improve accuracy of KVM clock when TSC scaling is in force

From: Paul Durrant
Date: Fri Apr 19 2024 - 11:30:51 EST


On 18/04/2024 20:34, David Woodhouse wrote:
From: David Woodhouse <dwmw@xxxxxxxxxxxx>

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.

Signed-off-by: David Woodhouse <dwmw@xxxxxxxxxxxx>
Fixes: 78db6a503796 ("KVM: x86: rewrite handling of scaled TSC for kvmclock")
---
arch/x86/include/asm/kvm_host.h | 2 +-
arch/x86/kvm/x86.c | 17 +++++++++--------
arch/x86/kvm/xen.c | 2 +-
3 files changed, 11 insertions(+), 10 deletions(-)


Reviewed-by: Paul Durrant <paul@xxxxxxx>