From mboxrd@z Thu Jan 1 00:00:00 1970 From: Zachary Amsden Subject: [PATCH] fix kvmclock bug Date: Sat, 18 Sep 2010 14:15:09 -1000 Message-ID: <4C95560D.3050108@redhat.com> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="------------040107000900080102000402" To: Marcelo Tosatti , Avi Kivity , Jan Kiszka , kvm , Glauber Costa Return-path: Received: from mx1.redhat.com ([209.132.183.28]:25916 "EHLO mx1.redhat.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751124Ab0ISAPN (ORCPT ); Sat, 18 Sep 2010 20:15:13 -0400 Sender: kvm-owner@vger.kernel.org List-ID: This is a multi-part message in MIME format. --------------040107000900080102000402 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit For CPUs with unstable TSC, we null time offset between not just VCPU switches, but all preemptions of the kvm thread. This makes a bug much more likely where the kvmclock values are updated before a successful exit from virt, causing an underflow. The null offsetting was added at : bf0fb4a42ba7eb362f4013bd2e93209666793e66 The underflow happens with this additional patch : cf839f5da2b0779b9ec8b990f851fb4e7d681da0 There is a secondary bug, which is that TSC fails to advance with real time on unstable TSC, but the fix is much more involved (it requires the TSC catchup code). For now, this patch is sufficient to get things working again for me. --------------040107000900080102000402 Content-Type: text/plain; name="kvmclock-preempt.patch" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="kvmclock-preempt.patch" commit 1abe7e8806fd71ea802c6622ed3ce7821a18f271 Author: Zachary Amsden Date: Sat Sep 18 13:58:37 2010 -1000 Fix kvmclock bug If preempted after kvmclock values are updated, but before hardware virtualization is entered, the last tsc time as read by the guest is never set. It underflows the next time kvmclock is updated if there has not yet been a successful entry / exit into hardware virt. Fix this by simply setting last_tsc to the newly read tsc value so that any computed nsec advance of kvmclock is nulled. Signed-off-by: Zachary Amsden diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index 76db85a..09f468a 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -1101,6 +1101,7 @@ static int kvm_guest_time_update(struct kvm_vcpu *v) vcpu->hv_clock.tsc_timestamp = tsc_timestamp; vcpu->hv_clock.system_time = kernel_ns + v->kvm->arch.kvmclock_offset; vcpu->last_kernel_ns = kernel_ns; + vcpu->last_guest_tsc = tsc_timestamp; vcpu->hv_clock.flags = 0; /* --------------040107000900080102000402--