• Vitaly Kuznetsov's avatar
    KVM: x86: Prevent 'hv_clock->system_time' from going negative in kvm_guest_time_update() · 77fcbe82
    Vitaly Kuznetsov authored
    When guest time is reset with KVM_SET_CLOCK(0), it is possible for
    'hv_clock->system_time' to become a small negative number. This happens
    because in KVM_SET_CLOCK handling we set 'kvm->arch.kvmclock_offset' based
    on get_kvmclock_ns(kvm) but when KVM_REQ_CLOCK_UPDATE is handled,
    kvm_guest_time_update() does (masterclock in use case):
    
    hv_clock.system_time = ka->master_kernel_ns + v->kvm->arch.kvmclock_offset;
    
    And 'master_kernel_ns' represents the last time when masterclock
    got updated, it can precede KVM_SET_CLOCK() call. Normally, this is not a
    problem, the difference is very small, e.g. I'm observing
    hv_clock.system_time = -70 ns. The issue comes from the fact that
    'hv_clock.system_time' is stored as unsigned and 'system_time / 100' in
    compute_tsc_page_parameters() becomes a very big number.
    
    Use 'master_kernel_ns' instead of get_kvmclock_ns() when masterclock is in
    use and get_kvmclock_base_ns() when it's not to prevent 'system_time' from
    going negative.
    Signed-off-by: default avatarVitaly Kuznetsov <vkuznets@redhat.com>
    Message-Id: <20210331124130.337992-2-vkuznets@redhat.com>
    Signed-off-by: default avatarPaolo Bonzini <pbonzini@redhat.com>
    77fcbe82
x86.c 306 KB