All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] KVM: VMX: Optimize tscdeadline timer latency
@ 2018-05-29  6:53 Wanpeng Li
  2018-05-29 14:23 ` Radim Krčmář
  0 siblings, 1 reply; 6+ messages in thread
From: Wanpeng Li @ 2018-05-29  6:53 UTC (permalink / raw)
  To: linux-kernel, kvm; +Cc: Paolo Bonzini, Radim Krčmář

From: Wanpeng Li <wanpengli@tencent.com>

'Commit d0659d946be0 ("KVM: x86: add option to advance tscdeadline 
hrtimer expiration")' advances the tscdeadline (the timer is emulated 
by hrtimer) expiration in order that the latency which is incurred 
by hypervisor (apic_timer_fn -> vmentry) can be avoided. This patch 
adds the advance tscdeadline expiration support to which the tscdeadline 
timer is emulated by VMX preemption timer to reduce the hypervisor 
lantency (handle_preemption_timer -> vmentry). clockevents infrastruture 
can program minimum delay if hrtimer feeds a expiration in the past, 
we set delta_tsc to 1(which will be converted to 0 before vmentry) 
which can lead to an immediately vmexit when delta_tsc is not bigger 
than advance ns. 

This patch can reduce ~63% latency (~4450 cycles to ~1660 cycles on 
a haswell desktop) for kvm-unit-tests/tscdeadline_latency when testing
busy waits.

Cc: Paolo Bonzini <pbonzini@redhat.com>
Cc: Radim Krčmář <rkrcmar@redhat.com>
Signed-off-by: Wanpeng Li <wanpengli@tencent.com>
---
 arch/x86/kvm/vmx.c | 8 +++++++-
 arch/x86/kvm/x86.c | 1 +
 2 files changed, 8 insertions(+), 1 deletion(-)

diff --git a/arch/x86/kvm/vmx.c b/arch/x86/kvm/vmx.c
index e50beb7..d8b5110 100644
--- a/arch/x86/kvm/vmx.c
+++ b/arch/x86/kvm/vmx.c
@@ -12435,7 +12435,7 @@ static inline int u64_shl_div_u64(u64 a, unsigned int shift,
 static int vmx_set_hv_timer(struct kvm_vcpu *vcpu, u64 guest_deadline_tsc)
 {
 	struct vcpu_vmx *vmx;
-	u64 tscl, guest_tscl, delta_tsc;
+	u64 tscl, guest_tscl, delta_tsc, lapic_timer_advance_cycles;
 
 	if (kvm_mwait_in_guest(vcpu->kvm))
 		return -EOPNOTSUPP;
@@ -12444,6 +12444,12 @@ static int vmx_set_hv_timer(struct kvm_vcpu *vcpu, u64 guest_deadline_tsc)
 	tscl = rdtsc();
 	guest_tscl = kvm_read_l1_tsc(vcpu, tscl);
 	delta_tsc = max(guest_deadline_tsc, guest_tscl) - guest_tscl;
+	lapic_timer_advance_cycles = nsec_to_cycles(vcpu, lapic_timer_advance_ns);
+
+	if (delta_tsc > lapic_timer_advance_cycles)
+		delta_tsc -= lapic_timer_advance_cycles;
+	else
+		delta_tsc = 1;
 
 	/* Convert to host delta tsc if tsc scaling is enabled */
 	if (vcpu->arch.tsc_scaling_ratio != kvm_default_tsc_scaling_ratio &&
diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c
index 22bd20f..d8abd96 100644
--- a/arch/x86/kvm/x86.c
+++ b/arch/x86/kvm/x86.c
@@ -138,6 +138,7 @@ module_param(tsc_tolerance_ppm, uint, S_IRUGO | S_IWUSR);
 /* lapic timer advance (tscdeadline mode only) in nanoseconds */
 unsigned int __read_mostly lapic_timer_advance_ns = 0;
 module_param(lapic_timer_advance_ns, uint, S_IRUGO | S_IWUSR);
+EXPORT_SYMBOL_GPL(lapic_timer_advance_ns);
 
 static bool __read_mostly vector_hashing = true;
 module_param(vector_hashing, bool, S_IRUGO);
-- 
2.7.4

^ permalink raw reply related	[flat|nested] 6+ messages in thread

* Re: [PATCH] KVM: VMX: Optimize tscdeadline timer latency
  2018-05-29  6:53 [PATCH] KVM: VMX: Optimize tscdeadline timer latency Wanpeng Li
@ 2018-05-29 14:23 ` Radim Krčmář
  2018-05-29 14:31   ` Radim Krčmář
  0 siblings, 1 reply; 6+ messages in thread
From: Radim Krčmář @ 2018-05-29 14:23 UTC (permalink / raw)
  To: Wanpeng Li; +Cc: linux-kernel, kvm, Paolo Bonzini

2018-05-29 14:53+0800, Wanpeng Li:
> From: Wanpeng Li <wanpengli@tencent.com>
> 
> 'Commit d0659d946be0 ("KVM: x86: add option to advance tscdeadline 
> hrtimer expiration")' advances the tscdeadline (the timer is emulated 
> by hrtimer) expiration in order that the latency which is incurred 
> by hypervisor (apic_timer_fn -> vmentry) can be avoided. This patch 
> adds the advance tscdeadline expiration support to which the tscdeadline 
> timer is emulated by VMX preemption timer to reduce the hypervisor 
> lantency (handle_preemption_timer -> vmentry). clockevents infrastruture 
> can program minimum delay if hrtimer feeds a expiration in the past, 
> we set delta_tsc to 1(which will be converted to 0 before vmentry) 
> which can lead to an immediately vmexit when delta_tsc is not bigger 
> than advance ns. 
> 
> This patch can reduce ~63% latency (~4450 cycles to ~1660 cycles on 
> a haswell desktop) for kvm-unit-tests/tscdeadline_latency when testing
> busy waits.
> 
> Cc: Paolo Bonzini <pbonzini@redhat.com>
> Cc: Radim Krčmář <rkrcmar@redhat.com>
> Signed-off-by: Wanpeng Li <wanpengli@tencent.com>
> ---
> diff --git a/arch/x86/kvm/vmx.c b/arch/x86/kvm/vmx.c
> @@ -12444,6 +12444,12 @@ static int vmx_set_hv_timer(struct kvm_vcpu *vcpu, u64 guest_deadline_tsc)
>  	tscl = rdtsc();
>  	guest_tscl = kvm_read_l1_tsc(vcpu, tscl);
>  	delta_tsc = max(guest_deadline_tsc, guest_tscl) - guest_tscl;
> +	lapic_timer_advance_cycles = nsec_to_cycles(vcpu, lapic_timer_advance_ns);
> +	if (delta_tsc > lapic_timer_advance_cycles)
> +		delta_tsc -= lapic_timer_advance_cycles;
> +	else
> +		delta_tsc = 1;

Why don't we just "return 1" to say that the timer has expired?

I think "delta_tsc = 1" would just force an immediate VM exit and
a re-entry, which seems wasteful as we could just be delaying the entry
until the deadline has really passed,

thanks.

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] KVM: VMX: Optimize tscdeadline timer latency
  2018-05-29 14:23 ` Radim Krčmář
@ 2018-05-29 14:31   ` Radim Krčmář
  2018-05-29 17:08     ` Paolo Bonzini
  0 siblings, 1 reply; 6+ messages in thread
From: Radim Krčmář @ 2018-05-29 14:31 UTC (permalink / raw)
  To: Wanpeng Li; +Cc: linux-kernel, kvm, Paolo Bonzini

2018-05-29 16:23+0200, Radim Krčmář:
> 2018-05-29 14:53+0800, Wanpeng Li:
> > From: Wanpeng Li <wanpengli@tencent.com>
> > 
> > 'Commit d0659d946be0 ("KVM: x86: add option to advance tscdeadline 
> > hrtimer expiration")' advances the tscdeadline (the timer is emulated 
> > by hrtimer) expiration in order that the latency which is incurred 
> > by hypervisor (apic_timer_fn -> vmentry) can be avoided. This patch 
> > adds the advance tscdeadline expiration support to which the tscdeadline 
> > timer is emulated by VMX preemption timer to reduce the hypervisor 
> > lantency (handle_preemption_timer -> vmentry). clockevents infrastruture 
> > can program minimum delay if hrtimer feeds a expiration in the past, 
> > we set delta_tsc to 1(which will be converted to 0 before vmentry) 
> > which can lead to an immediately vmexit when delta_tsc is not bigger 
> > than advance ns. 
> > 
> > This patch can reduce ~63% latency (~4450 cycles to ~1660 cycles on 
> > a haswell desktop) for kvm-unit-tests/tscdeadline_latency when testing
> > busy waits.
> > 
> > Cc: Paolo Bonzini <pbonzini@redhat.com>
> > Cc: Radim Krčmář <rkrcmar@redhat.com>
> > Signed-off-by: Wanpeng Li <wanpengli@tencent.com>
> > ---
> > diff --git a/arch/x86/kvm/vmx.c b/arch/x86/kvm/vmx.c
> > @@ -12444,6 +12444,12 @@ static int vmx_set_hv_timer(struct kvm_vcpu *vcpu, u64 guest_deadline_tsc)
> >  	tscl = rdtsc();
> >  	guest_tscl = kvm_read_l1_tsc(vcpu, tscl);
> >  	delta_tsc = max(guest_deadline_tsc, guest_tscl) - guest_tscl;
> > +	lapic_timer_advance_cycles = nsec_to_cycles(vcpu, lapic_timer_advance_ns);
> > +	if (delta_tsc > lapic_timer_advance_cycles)
> > +		delta_tsc -= lapic_timer_advance_cycles;
> > +	else
> > +		delta_tsc = 1;
> 
> Why don't we just "return 1" to say that the timer has expired?

This case might be rare, so setting delta_tsc = 0 would be safer.

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] KVM: VMX: Optimize tscdeadline timer latency
  2018-05-29 14:31   ` Radim Krčmář
@ 2018-05-29 17:08     ` Paolo Bonzini
  2018-05-30  0:46       ` Wanpeng Li
  2018-06-02  0:24       ` Wanpeng Li
  0 siblings, 2 replies; 6+ messages in thread
From: Paolo Bonzini @ 2018-05-29 17:08 UTC (permalink / raw)
  To: Radim Krčmář, Wanpeng Li; +Cc: linux-kernel, kvm

On 29/05/2018 16:31, Radim Krčmář wrote:
> 2018-05-29 16:23+0200, Radim Krčmář:
>> 2018-05-29 14:53+0800, Wanpeng Li:
>>> From: Wanpeng Li <wanpengli@tencent.com>
>>>
>>> 'Commit d0659d946be0 ("KVM: x86: add option to advance tscdeadline 
>>> hrtimer expiration")' advances the tscdeadline (the timer is emulated 
>>> by hrtimer) expiration in order that the latency which is incurred 
>>> by hypervisor (apic_timer_fn -> vmentry) can be avoided. This patch 
>>> adds the advance tscdeadline expiration support to which the tscdeadline 
>>> timer is emulated by VMX preemption timer to reduce the hypervisor 
>>> lantency (handle_preemption_timer -> vmentry). clockevents infrastruture 
>>> can program minimum delay if hrtimer feeds a expiration in the past, 
>>> we set delta_tsc to 1(which will be converted to 0 before vmentry) 
>>> which can lead to an immediately vmexit when delta_tsc is not bigger 
>>> than advance ns. 
>>>
>>> This patch can reduce ~63% latency (~4450 cycles to ~1660 cycles on 
>>> a haswell desktop) for kvm-unit-tests/tscdeadline_latency when testing
>>> busy waits.
>>>
>>> Cc: Paolo Bonzini <pbonzini@redhat.com>
>>> Cc: Radim Krčmář <rkrcmar@redhat.com>
>>> Signed-off-by: Wanpeng Li <wanpengli@tencent.com>
>>> ---
>>> diff --git a/arch/x86/kvm/vmx.c b/arch/x86/kvm/vmx.c
>>> @@ -12444,6 +12444,12 @@ static int vmx_set_hv_timer(struct kvm_vcpu *vcpu, u64 guest_deadline_tsc)
>>>  	tscl = rdtsc();
>>>  	guest_tscl = kvm_read_l1_tsc(vcpu, tscl);
>>>  	delta_tsc = max(guest_deadline_tsc, guest_tscl) - guest_tscl;
>>> +	lapic_timer_advance_cycles = nsec_to_cycles(vcpu, lapic_timer_advance_ns);
>>> +	if (delta_tsc > lapic_timer_advance_cycles)
>>> +		delta_tsc -= lapic_timer_advance_cycles;
>>> +	else
>>> +		delta_tsc = 1;
>>
>> Why don't we just "return 1" to say that the timer has expired?
> 
> This case might be rare, so setting delta_tsc = 0 would be safer.

Queued with this change.  Indeed this case matches vmx_arm_hv_timer so
it's preferrable.

Paolo

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] KVM: VMX: Optimize tscdeadline timer latency
  2018-05-29 17:08     ` Paolo Bonzini
@ 2018-05-30  0:46       ` Wanpeng Li
  2018-06-02  0:24       ` Wanpeng Li
  1 sibling, 0 replies; 6+ messages in thread
From: Wanpeng Li @ 2018-05-30  0:46 UTC (permalink / raw)
  To: Paolo Bonzini; +Cc: Radim Krcmar, LKML, kvm

On Wed, 30 May 2018 at 01:08, Paolo Bonzini <pbonzini@redhat.com> wrote:

> On 29/05/2018 16:31, Radim Krčmář wrote:
> > 2018-05-29 16:23+0200, Radim Krčmář:
> >> 2018-05-29 14:53+0800, Wanpeng Li:
> >>> From: Wanpeng Li <wanpengli@tencent.com>
> >>>
> >>> 'Commit d0659d946be0 ("KVM: x86: add option to advance tscdeadline
> >>> hrtimer expiration")' advances the tscdeadline (the timer is emulated
> >>> by hrtimer) expiration in order that the latency which is incurred
> >>> by hypervisor (apic_timer_fn -> vmentry) can be avoided. This patch
> >>> adds the advance tscdeadline expiration support to which the
tscdeadline
> >>> timer is emulated by VMX preemption timer to reduce the hypervisor
> >>> lantency (handle_preemption_timer -> vmentry). clockevents
infrastruture
> >>> can program minimum delay if hrtimer feeds a expiration in the past,
> >>> we set delta_tsc to 1(which will be converted to 0 before vmentry)
> >>> which can lead to an immediately vmexit when delta_tsc is not bigger
> >>> than advance ns.
> >>>
> >>> This patch can reduce ~63% latency (~4450 cycles to ~1660 cycles on
> >>> a haswell desktop) for kvm-unit-tests/tscdeadline_latency when testing
> >>> busy waits.
> >>>
> >>> Cc: Paolo Bonzini <pbonzini@redhat.com>
> >>> Cc: Radim Krčmář <rkrcmar@redhat.com>
> >>> Signed-off-by: Wanpeng Li <wanpengli@tencent.com>
> >>> ---
> >>> diff --git a/arch/x86/kvm/vmx.c b/arch/x86/kvm/vmx.c
> >>> @@ -12444,6 +12444,12 @@ static int vmx_set_hv_timer(struct kvm_vcpu
*vcpu, u64 guest_deadline_tsc)
> >>>     tscl = rdtsc();
> >>>     guest_tscl = kvm_read_l1_tsc(vcpu, tscl);
> >>>     delta_tsc = max(guest_deadline_tsc, guest_tscl) - guest_tscl;
> >>> +   lapic_timer_advance_cycles = nsec_to_cycles(vcpu,
lapic_timer_advance_ns);
> >>> +   if (delta_tsc > lapic_timer_advance_cycles)
> >>> +           delta_tsc -= lapic_timer_advance_cycles;
> >>> +   else
> >>> +           delta_tsc = 1;
> >>
> >> Why don't we just "return 1" to say that the timer has expired?
> >
> > This case might be rare, so setting delta_tsc = 0 would be safer.

> Queued with this change.  Indeed this case matches vmx_arm_hv_timer so
> it's preferrable.

Agreed, thanks.

Regards,
Wanpeng Li

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] KVM: VMX: Optimize tscdeadline timer latency
  2018-05-29 17:08     ` Paolo Bonzini
  2018-05-30  0:46       ` Wanpeng Li
@ 2018-06-02  0:24       ` Wanpeng Li
  1 sibling, 0 replies; 6+ messages in thread
From: Wanpeng Li @ 2018-06-02  0:24 UTC (permalink / raw)
  To: Paolo Bonzini; +Cc: Radim Krcmar, LKML, kvm

On Wed, 30 May 2018 at 01:08, Paolo Bonzini <pbonzini@redhat.com> wrote:
>
> On 29/05/2018 16:31, Radim Krčmář wrote:
> > 2018-05-29 16:23+0200, Radim Krčmář:
> >> 2018-05-29 14:53+0800, Wanpeng Li:
> >>> From: Wanpeng Li <wanpengli@tencent.com>
> >>>
> >>> 'Commit d0659d946be0 ("KVM: x86: add option to advance tscdeadline
> >>> hrtimer expiration")' advances the tscdeadline (the timer is emulated
> >>> by hrtimer) expiration in order that the latency which is incurred
> >>> by hypervisor (apic_timer_fn -> vmentry) can be avoided. This patch
> >>> adds the advance tscdeadline expiration support to which the tscdeadline
> >>> timer is emulated by VMX preemption timer to reduce the hypervisor
> >>> lantency (handle_preemption_timer -> vmentry). clockevents infrastruture
> >>> can program minimum delay if hrtimer feeds a expiration in the past,
> >>> we set delta_tsc to 1(which will be converted to 0 before vmentry)
> >>> which can lead to an immediately vmexit when delta_tsc is not bigger
> >>> than advance ns.
> >>>
> >>> This patch can reduce ~63% latency (~4450 cycles to ~1660 cycles on
> >>> a haswell desktop) for kvm-unit-tests/tscdeadline_latency when testing
> >>> busy waits.
> >>>
> >>> Cc: Paolo Bonzini <pbonzini@redhat.com>
> >>> Cc: Radim Krčmář <rkrcmar@redhat.com>
> >>> Signed-off-by: Wanpeng Li <wanpengli@tencent.com>
> >>> ---
> >>> diff --git a/arch/x86/kvm/vmx.c b/arch/x86/kvm/vmx.c
> >>> @@ -12444,6 +12444,12 @@ static int vmx_set_hv_timer(struct kvm_vcpu *vcpu, u64 guest_deadline_tsc)
> >>>     tscl = rdtsc();
> >>>     guest_tscl = kvm_read_l1_tsc(vcpu, tscl);
> >>>     delta_tsc = max(guest_deadline_tsc, guest_tscl) - guest_tscl;
> >>> +   lapic_timer_advance_cycles = nsec_to_cycles(vcpu, lapic_timer_advance_ns);
> >>> +   if (delta_tsc > lapic_timer_advance_cycles)
> >>> +           delta_tsc -= lapic_timer_advance_cycles;
> >>> +   else
> >>> +           delta_tsc = 1;
> >>
> >> Why don't we just "return 1" to say that the timer has expired?
> >
> > This case might be rare, so setting delta_tsc = 0 would be safer.
>
> Queued with this change.  Indeed this case matches vmx_arm_hv_timer so

The patch description should also be updated in kvm/queue I think.

Regards,
Wanpeng Li

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2018-06-02  0:25 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-05-29  6:53 [PATCH] KVM: VMX: Optimize tscdeadline timer latency Wanpeng Li
2018-05-29 14:23 ` Radim Krčmář
2018-05-29 14:31   ` Radim Krčmář
2018-05-29 17:08     ` Paolo Bonzini
2018-05-30  0:46       ` Wanpeng Li
2018-06-02  0:24       ` Wanpeng Li

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.