kvm.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: "David S. Ahern" <daahern@cisco.com>
To: Marcelo Tosatti <mtosatti@redhat.com>
Cc: kvm-devel <kvm@vger.kernel.org>, Chris Wright <chrisw@redhat.com>,
	Glauber de Oliveira Costa <gcosta@redhat.com>
Subject: Re: RFC: VMX: initialize TSC offset relative to vm creation time
Date: Wed, 10 Sep 2008 22:58:53 -0600	[thread overview]
Message-ID: <48C8A58D.40806@cisco.com> (raw)
In-Reply-To: <20080910205842.GA12514@dmt.cnet>

Hi Marcelo:

Dramatic improvement. The following is an example with kvm-75 and this
patch.  Without cpu affinity from a kvm perspective (vcpu-to-pcpu):

cpu 0: 1221107886.020298
cpu 1: 1221107886.020290 *
cpu 2: 1221107886.020555
cpu 3: 1221107886.020549 *

cpu 0: 1221107887.030244
cpu 1: 1221107887.030236 *
cpu 2: 1221107887.030498
cpu 3: 1221107887.030493 *

cpu 0: 1221107888.040248
cpu 1: 1221107888.040262
cpu 2: 1221107888.040314
cpu 3: 1221107888.040470

cpu 0: 1221107889.050305
cpu 1: 1221107889.050300 *
cpu 2: 1221107889.050354
cpu 3: 1221107889.050394

cpu 0: 1221107890.060384
cpu 1: 1221107890.060489
cpu 2: 1221107890.060753
cpu 3: 1221107890.060918

cpu 0: 1221107891.083559
cpu 1: 1221107891.083558 *
cpu 2: 1221107891.083614
cpu 3: 1221107891.083613 *

cpu 0: 1221107892.091705
cpu 1: 1221107892.091699 *
cpu 2: 1221107892.092998
cpu 3: 1221107892.093011

Setting vcpu-pcpu affinity well after guest startup, tracking is a bit
better (fewer time travels).

I do not believe there's a way to set affinity as kvm/qemu threads are
spawned (short of modifying qemu).

As before, RHEL3 guest. DL380G5 host.

david


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.
> 
> 
> Signed-off-by: Marcelo Tosatti <mtosatti@redhat.com>
> 
> 
> 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 {
> --
> To unsubscribe from this list: send the line "unsubscribe kvm" in
> the body of a message to majordomo@vger.kernel.org
> More majordomo info at  http://vger.kernel.org/majordomo-info.html

  parent reply	other threads:[~2008-09-11  4:59 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2008-09-10 20:58 RFC: VMX: initialize TSC offset relative to vm creation time Marcelo Tosatti
2008-09-10 22:18 ` Glauber Costa
2008-09-11  8:32   ` Marcelo Tosatti
2008-09-11  4:58 ` David S. Ahern [this message]
2008-09-13  4:55 ` Avi Kivity
2008-10-27 23:42   ` Marcelo Tosatti
2008-10-28 18:36     ` David S. Ahern
2008-10-30 10:20       ` Marcelo Tosatti
2008-10-30 14:00         ` David S. Ahern
2008-10-30 10:34       ` Avi Kivity
2008-10-13 13:12 ` David S. Ahern

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=48C8A58D.40806@cisco.com \
    --to=daahern@cisco.com \
    --cc=chrisw@redhat.com \
    --cc=gcosta@redhat.com \
    --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;
as well as URLs for NNTP newsgroup(s).