kvm.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Marcelo Tosatti <mtosatti@redhat.com>
To: Avi Kivity <avi@redhat.com>
Cc: kvm@vger.kernel.org, Benjamin Serebrin <benjamin.serebrin@amd.com>
Subject: Re: [patch 3/3] KVM: VMX: initialize TSC offset relative to vm creation time
Date: Thu, 11 Dec 2008 20:45:05 +0100	[thread overview]
Message-ID: <20081211194505.GA5468@dmt.cnet> (raw)
In-Reply-To: <49415010.3000108@redhat.com>

On Thu, Dec 11, 2008 at 07:38:24PM +0200, Avi Kivity wrote:
> Marcelo Tosatti wrote:
>
>  
>
>>> This looks fine, but have you tested it on a host with unsync tsc?  
>>> I'm  worried that we'll get regressions there even on uniprocessor 
>>> guest.   I'd like to keep the current behaviour for the special case 
>>> of
>>> uniprocessor guest on unsync tsc host.
>>>     
>>
>> I don't see how. For UP guests the TSC is initialized to zero during
>> vcpu setup, similarly to the current behaviour.
>>
>> Can you explain?
>>
>>   
>
> On a host with an unsync tsc, when you move the vcpu to another cpu, the  
> tsc may jump backwards.

Ok, this could cause the guest tsc to be initialized to a high value
close to wraparound (in case the vcpu is migrated to a cpu with negative
difference before vmx_vcpu_setup). What other regression could the
updated patch introduce?

diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_host.h
index 97215a4..5b70d83 100644
--- a/arch/x86/include/asm/kvm_host.h
+++ b/arch/x86/include/asm/kvm_host.h
@@ -378,6 +378,7 @@ struct kvm_arch{
 
 	unsigned long irq_sources_bitmap;
 	unsigned long irq_states[KVM_IOAPIC_NUM_PINS];
+	u64 vm_init_tsc;
 };
 
 struct kvm_vm_stat {
diff --git a/arch/x86/kvm/vmx.c b/arch/x86/kvm/vmx.c
index e446f23..0879852 100644
--- a/arch/x86/kvm/vmx.c
+++ b/arch/x86/kvm/vmx.c
@@ -856,11 +856,8 @@ static u64 guest_read_tsc(void)
  * writes 'guest_tsc' into guest's timestamp counter "register"
  * guest_tsc = host_tsc + tsc_offset ==> tsc_offset = guest_tsc - host_tsc
  */
-static void guest_write_tsc(u64 guest_tsc)
+static void guest_write_tsc(u64 guest_tsc, u64 host_tsc)
 {
-	u64 host_tsc;
-
-	rdtscll(host_tsc);
 	vmcs_write64(TSC_OFFSET, guest_tsc - host_tsc);
 }
 
@@ -924,6 +921,7 @@ static int vmx_set_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data)
 {
 	struct vcpu_vmx *vmx = to_vmx(vcpu);
 	struct kvm_msr_entry *msr;
+	u64 host_tsc;
 	int ret = 0;
 
 	switch (msr_index) {
@@ -949,7 +947,8 @@ static int vmx_set_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data)
 		vmcs_writel(GUEST_SYSENTER_ESP, data);
 		break;
 	case MSR_IA32_TIME_STAMP_COUNTER:
-		guest_write_tsc(data);
+		rdtscll(host_tsc);
+		guest_write_tsc(data, host_tsc);
 		break;
 	case MSR_P6_PERFCTR0:
 	case MSR_P6_PERFCTR1:
@@ -2111,7 +2110,7 @@ static int vmx_vcpu_setup(struct vcpu_vmx *vmx)
 {
 	u32 host_sysenter_cs, msr_low, msr_high;
 	u32 junk;
-	u64 host_pat;
+	u64 host_pat, tsc_this, tsc_base;
 	unsigned long a;
 	struct descriptor_table dt;
 	int i;
@@ -2239,6 +2238,12 @@ static int vmx_vcpu_setup(struct vcpu_vmx *vmx)
 	vmcs_writel(CR0_GUEST_HOST_MASK, ~0UL);
 	vmcs_writel(CR4_GUEST_HOST_MASK, KVM_GUEST_CR4_MASK);
 
+	tsc_base = vmx->vcpu.kvm->arch.vm_init_tsc;
+	rdtscll(tsc_this);
+	if (tsc_this < vmx->vcpu.kvm->arch.vm_init_tsc)
+		tsc_base = tsc_this;
+
+	guest_write_tsc(0, tsc_base);
 
 	return 0;
 }
@@ -2331,8 +2336,6 @@ static int vmx_vcpu_reset(struct kvm_vcpu *vcpu)
 	vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, 0);
 	vmcs_write32(GUEST_PENDING_DBG_EXCEPTIONS, 0);
 
-	guest_write_tsc(0);
-
 	/* Special registers */
 	vmcs_write64(GUEST_IA32_DEBUGCTL, 0);
 
diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c
index ba10287..b2d64eb 100644
--- a/arch/x86/kvm/x86.c
+++ b/arch/x86/kvm/x86.c
@@ -4122,6 +4122,8 @@ struct  kvm *kvm_arch_create_vm(void)
 	/* Reserve bit 0 of irq_sources_bitmap for userspace irq source */
 	set_bit(KVM_USERSPACE_IRQ_SOURCE_ID, &kvm->arch.irq_sources_bitmap);
 
+	rdtscll(kvm->arch.vm_init_tsc);
+
 	return kvm;
 }
 

  reply	other threads:[~2008-12-11 22:47 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2008-12-09  1:12 [patch 0/3] synchronized TSC between vcpu's on SMP guests Marcelo Tosatti
2008-12-09  1:12 ` [patch 1/3] QEMU/KVM: x86: separate TSC load from kvm_arch_load_regs Marcelo Tosatti
2008-12-10 10:10   ` Avi Kivity
2008-12-09  1:12 ` [patch 2/3] QEMU/KVM: BIOS: revert TSC zeroing Marcelo Tosatti
2008-12-09  1:12 ` [patch 3/3] KVM: VMX: initialize TSC offset relative to vm creation time Marcelo Tosatti
2008-12-10 10:19   ` Avi Kivity
2008-12-10 16:53     ` Marcelo Tosatti
2008-12-11 17:38       ` Avi Kivity
2008-12-11 19:45         ` Marcelo Tosatti [this message]
2008-12-28  9:26           ` Avi Kivity
2008-12-09 11:38 ` [patch 0/3] synchronized TSC between vcpu's on SMP guests Michael Tokarev
2008-12-09 10:47   ` Marcelo Tosatti
2008-12-09 22:48     ` Michael Tokarev
2008-12-22 23:16 ` David S. Ahern
2008-12-28  9:33 ` 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=20081211194505.GA5468@dmt.cnet \
    --to=mtosatti@redhat.com \
    --cc=avi@redhat.com \
    --cc=benjamin.serebrin@amd.com \
    --cc=kvm@vger.kernel.org \
    /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;
as well as URLs for NNTP newsgroup(s).