From mboxrd@z Thu Jan 1 00:00:00 1970 From: Owen Hofmann Subject: [PATCH] kvm: x86: Fix kvm clock versioning. Date: Mon, 3 Nov 2014 16:57:18 -0800 Message-ID: <1415062638-26950-1-git-send-email-osh@google.com> Cc: mtosatti@redhat.com, Owen Hofmann To: kvm@vger.kernel.org Return-path: Received: from mail-ig0-f174.google.com ([209.85.213.174]:43531 "EHLO mail-ig0-f174.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751017AbaKDA71 (ORCPT ); Mon, 3 Nov 2014 19:59:27 -0500 Received: by mail-ig0-f174.google.com with SMTP id hn18so5810932igb.1 for ; Mon, 03 Nov 2014 16:59:26 -0800 (PST) Sender: kvm-owner@vger.kernel.org List-ID: kvm updates the version number for the guest paravirt clock structure by incrementing the version of its private copy. It does not read the guest version, so will write version = 2 in the first update for every new VM, including after restoring a saved state. If guest state is saved during reading the clock, it could read and accept struct fields and guest TSC from two different updates. This changes the code to increment the guest version and write it back. Signed-off-by: Owen Hofmann --- arch/x86/kvm/x86.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index 0033df3..c25c9d8 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -1637,16 +1637,16 @@ static int kvm_guest_time_update(struct kvm_vcpu *v) vcpu->hv_clock.system_time = kernel_ns + v->kvm->arch.kvmclock_offset; vcpu->last_guest_tsc = tsc_timestamp; + if (unlikely(kvm_read_guest_cached(v->kvm, &vcpu->pv_time, + &guest_hv_clock, sizeof(guest_hv_clock)))) + return 0; + /* * The interface expects us to write an even number signaling that the * update is finished. Since the guest won't see the intermediate * state, we just increase by 2 at the end. */ - vcpu->hv_clock.version += 2; - - if (unlikely(kvm_read_guest_cached(v->kvm, &vcpu->pv_time, - &guest_hv_clock, sizeof(guest_hv_clock)))) - return 0; + vcpu->hv_clock.version = guest_hv_clock.version + 2; /* retain PVCLOCK_GUEST_STOPPED if set in guest copy */ pvclock_flags = (guest_hv_clock.flags & PVCLOCK_GUEST_STOPPED); -- 2.1.0.rc2.206.gedb03e5