]> www.infradead.org Git - users/dwmw2/linux.git/commit
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>
Fri, 2 Aug 2024 14:49:23 +0000 (15:49 +0100)
commit0e2b3cfa7238f1d96f66bf8340050cc84bc32f67
treeefc6ab693d2e155ce4ab415ee096b5ecfa92bcc0
parent3c4838070d5bf85078cb708e6fbe88d19428853b
KVM: x86: Improve accuracy of KVM clock when TSC scaling is in force

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