Skip to content

Commit 09a0c3f

Browse files
Owen Hofmannbonzini
authored andcommitted
kvm: x86: Fix kvm clock versioning.
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 <[email protected]> Reviewed-by: Marcelo Tosatti <[email protected]> Signed-off-by: Paolo Bonzini <[email protected]>
1 parent ed9aad2 commit 09a0c3f

File tree

1 file changed

+5
-5
lines changed

1 file changed

+5
-5
lines changed

arch/x86/kvm/x86.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1636,16 +1636,16 @@ static int kvm_guest_time_update(struct kvm_vcpu *v)
16361636
vcpu->hv_clock.system_time = kernel_ns + v->kvm->arch.kvmclock_offset;
16371637
vcpu->last_guest_tsc = tsc_timestamp;
16381638

1639+
if (unlikely(kvm_read_guest_cached(v->kvm, &vcpu->pv_time,
1640+
&guest_hv_clock, sizeof(guest_hv_clock))))
1641+
return 0;
1642+
16391643
/*
16401644
* The interface expects us to write an even number signaling that the
16411645
* update is finished. Since the guest won't see the intermediate
16421646
* state, we just increase by 2 at the end.
16431647
*/
1644-
vcpu->hv_clock.version += 2;
1645-
1646-
if (unlikely(kvm_read_guest_cached(v->kvm, &vcpu->pv_time,
1647-
&guest_hv_clock, sizeof(guest_hv_clock))))
1648-
return 0;
1648+
vcpu->hv_clock.version = guest_hv_clock.version + 2;
16491649

16501650
/* retain PVCLOCK_GUEST_STOPPED if set in guest copy */
16511651
pvclock_flags = (guest_hv_clock.flags & PVCLOCK_GUEST_STOPPED);

0 commit comments

Comments
 (0)