From mboxrd@z Thu Jan 1 00:00:00 1970 From: Glauber Costa Subject: Re: RFC: VMX: initialize TSC offset relative to vm creation time Date: Wed, 10 Sep 2008 19:18:43 -0300 Message-ID: <20080910221843.GF17969@poweredge.glommer> References: <20080910205842.GA12514@dmt.cnet> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Cc: kvm-devel , "David S. Ahern" , Chris Wright , Glauber de Oliveira Costa To: Marcelo Tosatti Return-path: Received: from mx2.redhat.com ([66.187.237.31]:32868 "EHLO mx2.redhat.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1753494AbYIJWTt (ORCPT ); Wed, 10 Sep 2008 18:19:49 -0400 Content-Disposition: inline In-Reply-To: <20080910205842.GA12514@dmt.cnet> Sender: kvm-owner@vger.kernel.org List-ID: On Wed, Sep 10, 2008 at 05:58:42PM -0300, Marcelo Tosatti wrote: > > VMX initializes the TSC offset for each vcpu at different times, and > also reinitializes it for vcpus other than 0 on APIC SIPI message. > > This bug causes the TSC's to appear unsynchronized in the guest, even if > the host is good. > > Older Linux kernels don't handle the situation very well, so > gettimeofday is likely to go backwards in time: > > http://www.mail-archive.com/kvm@vger.kernel.org/msg02955.html > http://sourceforge.net/tracker/index.php?func=detail&aid=2025534&group_id=180599&atid=893831 > > Fix it by initializating the offset of each vcpu relative to vm creation > time, and moving it from vmx_vcpu_reset to vmx_vcpu_setup, out of the > APIC MP init path. How does it work if # vcpu > # pcpus? I remember that when I tried it, the big biting dog were cases in which all cpus tried to sync, but they naturally put the value "0" in different points of time (for obvious reasons), and would still appear unsynchronized to the guests. > > > Signed-off-by: Marcelo Tosatti > > > Index: kvm.tip/arch/x86/kvm/vmx.c > =================================================================== > --- kvm.tip.orig/arch/x86/kvm/vmx.c > +++ kvm.tip/arch/x86/kvm/vmx.c > @@ -850,11 +850,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); > } > > @@ -918,6 +915,7 @@ static int vmx_set_msr(struct kvm_vcpu * > { > struct vcpu_vmx *vmx = to_vmx(vcpu); > struct kvm_msr_entry *msr; > + u64 host_tsc; > int ret = 0; > > switch (msr_index) { > @@ -943,7 +941,8 @@ static int vmx_set_msr(struct kvm_vcpu * > 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: > @@ -2202,6 +2201,7 @@ static int vmx_vcpu_setup(struct vcpu_vm > vmcs_writel(CR0_GUEST_HOST_MASK, ~0UL); > vmcs_writel(CR4_GUEST_HOST_MASK, KVM_GUEST_CR4_MASK); > > + guest_write_tsc(0, vmx->vcpu.kvm->arch.vm_init_tsc); > > return 0; > } > @@ -2292,8 +2292,6 @@ static int vmx_vcpu_reset(struct kvm_vcp > 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); > > Index: kvm.tip/arch/x86/kvm/x86.c > =================================================================== > --- kvm.tip.orig/arch/x86/kvm/x86.c > +++ kvm.tip/arch/x86/kvm/x86.c > @@ -4250,6 +4250,8 @@ struct kvm *kvm_arch_create_vm(void) > INIT_LIST_HEAD(&kvm->arch.active_mmu_pages); > INIT_LIST_HEAD(&kvm->arch.assigned_dev_head); > > + rdtscll(kvm->arch.vm_init_tsc); > + > return kvm; > } > > Index: kvm.tip/include/asm-x86/kvm_host.h > =================================================================== > --- kvm.tip.orig/include/asm-x86/kvm_host.h > +++ kvm.tip/include/asm-x86/kvm_host.h > @@ -377,6 +377,7 @@ struct kvm_arch{ > > struct page *ept_identity_pagetable; > bool ept_identity_pagetable_done; > + u64 vm_init_tsc; > }; > > struct kvm_vm_stat {