]> www.infradead.org Git - users/dwmw2/linux.git/commit
KVM: x86: Refine calculation of guest wall clock to use a single TSC read
authorDavid Woodhouse <dwmw@amazon.co.uk>
Thu, 5 Oct 2023 09:16:10 +0000 (10:16 +0100)
committerSean Christopherson <seanjc@google.com>
Fri, 6 Oct 2023 02:36:16 +0000 (19:36 -0700)
commit5d6d6a7d7e66a98bb3432478d226c68c219aaba3
tree06103341dba2f402552861ab5f0e114d3afb3c4d
parente47d86083c66525b89c7fc66cdd64d5937725563
KVM: x86: Refine calculation of guest wall clock to use a single TSC read

When populating the guest's PV wall clock information, KVM currently does
a simple 'kvm_get_real_ns() - get_kvmclock_ns(kvm)'. This is an antipattern
which should be avoided; when working with the relationship between two
clocks, it's never correct to obtain one of them "now" and then the other
at a slightly different "now" after an unspecified period of preemption
(which might not even be under the control of the kernel, if this is an
L1 hosting an L2 guest under nested virtualization).

Add a kvm_get_wall_clock_epoch() function to return the guest wall clock
epoch in nanoseconds using the same method as __get_kvmclock() — by using
kvm_get_walltime_and_clockread() to calculate both the wall clock and KVM
clock time from a *single* TSC reading.

The condition using get_cpu_tsc_khz() is equivalent to the version in
__get_kvmclock() which separately checks for the CONSTANT_TSC feature or
the per-CPU cpu_tsc_khz. Which is what get_cpu_tsc_khz() does anyway.

Signed-off-by: David Woodhouse <dwmw@amazon.co.uk>
Link: https://lore.kernel.org/r/bfc6d3d7cfb88c47481eabbf5a30a264c58c7789.camel@infradead.org
Signed-off-by: Sean Christopherson <seanjc@google.com>
arch/x86/kvm/x86.c
arch/x86/kvm/x86.h
arch/x86/kvm/xen.c