public inbox for kvm@vger.kernel.org
 help / color / mirror / Atom feed
From: Zachary Amsden <zamsden@redhat.com>
To: Marcelo Tosatti <mtosatti@redhat.com>,
	Avi Kivity <avi@redhat.com>, Jan Kiszka <jan.kiszka@web.de>,
	kvm <kvm@vger.kernel.org>, Glauber Costa <glommer@redhat.com>
Subject: [PATCH] fix kvmclock bug
Date: Sat, 18 Sep 2010 14:15:09 -1000	[thread overview]
Message-ID: <4C95560D.3050108@redhat.com> (raw)

[-- Attachment #1: Type: text/plain, Size: 661 bytes --]

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.

[-- Attachment #2: kvmclock-preempt.patch --]
[-- Type: text/plain, Size: 1078 bytes --]

commit 1abe7e8806fd71ea802c6622ed3ce7821a18f271
Author: Zachary Amsden <zamsden@redhat.com>
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 <zamsden@redhat.com>

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;
 
 	/*

             reply	other threads:[~2010-09-19  0:15 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2010-09-19  0:15 Zachary Amsden [this message]
2010-09-24  7:28 ` [PATCH] fix kvmclock bug Jan Kiszka
2010-09-26  9:54   ` Jan Kiszka
2010-09-27 19:00     ` Zachary Amsden
2010-09-28  8:58       ` Jan Kiszka
2010-09-29  8:58 ` Avi Kivity

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=4C95560D.3050108@redhat.com \
    --to=zamsden@redhat.com \
    --cc=avi@redhat.com \
    --cc=glommer@redhat.com \
    --cc=jan.kiszka@web.de \
    --cc=kvm@vger.kernel.org \
    --cc=mtosatti@redhat.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox