• David Woodhouse's avatar
    KVM: x86: Refine calculation of guest wall clock to use a single TSC read · 5d6d6a7d
    David Woodhouse authored
    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: default avatarDavid Woodhouse <dwmw@amazon.co.uk>
    Link: https://lore.kernel.org/r/bfc6d3d7cfb88c47481eabbf5a30a264c58c7789.camel@infradead.orgSigned-off-by: default avatarSean Christopherson <seanjc@google.com>
    5d6d6a7d
x86.h 15.1 KB