]> 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>
Sun, 1 Oct 2023 17:06:31 +0000 (18:06 +0100)
committerDavid Woodhouse <dwmw@amazon.co.uk>
Mon, 2 Oct 2023 10:15:36 +0000 (11:15 +0100)
commitb71c616ce3d939b8ed833bef73c0c0fe184ab41c
tree4c58698c09ae62946b659005b5afe2eaf822e6f5
parent2c61644967ace554db1fd3c380976ccacb38fe76
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>
arch/x86/kvm/x86.c
arch/x86/kvm/x86.h
arch/x86/kvm/xen.c