kvm.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] KVM: LAPIC: Accurately guarantee busy wait for timer to expire when using hv_timer
@ 2021-04-28 11:08 Wanpeng Li
  2021-04-28 22:56 ` Sean Christopherson
  0 siblings, 1 reply; 3+ messages in thread
From: Wanpeng Li @ 2021-04-28 11:08 UTC (permalink / raw)
  To: linux-kernel, kvm
  Cc: Paolo Bonzini, Sean Christopherson, Vitaly Kuznetsov, Wanpeng Li,
	Jim Mattson, Joerg Roedel

From: Wanpeng Li <wanpengli@tencent.com>

Commit ee66e453db13d (KVM: lapic: Busy wait for timer to expire when 
using hv_timer) tries to set ktime->expired_tscdeadline by checking 
ktime->hv_timer_in_use since lapic timer oneshot/periodic modes which 
are emulated by vmx preemption timer also get advanced, they leverage 
the same vmx preemption timer logic with tsc-deadline mode. However, 
ktime->hv_timer_in_use is cleared before apic_timer_expired() handling, 
let's delay this clearing in preemption-disabled region.

Fixes: ee66e453db13d (KVM: lapic: Busy wait for timer to expire when using hv_timer)
Cc: Sean Christopherson <seanjc@google.com>
Signed-off-by: Wanpeng Li <wanpengli@tencent.com>
---
 arch/x86/kvm/lapic.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/arch/x86/kvm/lapic.c b/arch/x86/kvm/lapic.c
index 152591f..c0ebef5 100644
--- a/arch/x86/kvm/lapic.c
+++ b/arch/x86/kvm/lapic.c
@@ -1913,8 +1913,8 @@ void kvm_lapic_expired_hv_timer(struct kvm_vcpu *vcpu)
 	if (!apic->lapic_timer.hv_timer_in_use)
 		goto out;
 	WARN_ON(rcuwait_active(&vcpu->wait));
-	cancel_hv_timer(apic);
 	apic_timer_expired(apic, false);
+	cancel_hv_timer(apic);
 
 	if (apic_lvtt_period(apic) && apic->lapic_timer.period) {
 		advance_periodic_target_expiration(apic);
-- 
2.7.4


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

* Re: [PATCH] KVM: LAPIC: Accurately guarantee busy wait for timer to expire when using hv_timer
  2021-04-28 11:08 [PATCH] KVM: LAPIC: Accurately guarantee busy wait for timer to expire when using hv_timer Wanpeng Li
@ 2021-04-28 22:56 ` Sean Christopherson
  2021-04-29 15:40   ` Paolo Bonzini
  0 siblings, 1 reply; 3+ messages in thread
From: Sean Christopherson @ 2021-04-28 22:56 UTC (permalink / raw)
  To: Wanpeng Li
  Cc: linux-kernel, kvm, Paolo Bonzini, Vitaly Kuznetsov, Wanpeng Li,
	Jim Mattson, Joerg Roedel

On Wed, Apr 28, 2021, Wanpeng Li wrote:
> From: Wanpeng Li <wanpengli@tencent.com>
> 
> Commit ee66e453db13d (KVM: lapic: Busy wait for timer to expire when 
> using hv_timer) tries to set ktime->expired_tscdeadline by checking 
> ktime->hv_timer_in_use since lapic timer oneshot/periodic modes which 
> are emulated by vmx preemption timer also get advanced, they leverage 
> the same vmx preemption timer logic with tsc-deadline mode. However, 
> ktime->hv_timer_in_use is cleared before apic_timer_expired() handling, 
> let's delay this clearing in preemption-disabled region.
> 
> Fixes: ee66e453db13d (KVM: lapic: Busy wait for timer to expire when using hv_timer)

Well that's embarassing.  I suspect/hope I tested the case where start_hv_timer()
detects the timer already expired.  On the plus side, start_hv_timer() calls
cancel_hv_timer() after apic_timer_expired(), so there are unlikely to be hidden
side effects (and cancel_hv_timer() is tiny).

> Cc: Sean Christopherson <seanjc@google.com>
> Signed-off-by: Wanpeng Li <wanpengli@tencent.com>

Reviewed-by: Sean Christopherson <seanjc@google.com>

> ---
>  arch/x86/kvm/lapic.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/arch/x86/kvm/lapic.c b/arch/x86/kvm/lapic.c
> index 152591f..c0ebef5 100644
> --- a/arch/x86/kvm/lapic.c
> +++ b/arch/x86/kvm/lapic.c
> @@ -1913,8 +1913,8 @@ void kvm_lapic_expired_hv_timer(struct kvm_vcpu *vcpu)
>  	if (!apic->lapic_timer.hv_timer_in_use)
>  		goto out;
>  	WARN_ON(rcuwait_active(&vcpu->wait));
> -	cancel_hv_timer(apic);
>  	apic_timer_expired(apic, false);
> +	cancel_hv_timer(apic);
>  
>  	if (apic_lvtt_period(apic) && apic->lapic_timer.period) {
>  		advance_periodic_target_expiration(apic);
> -- 
> 2.7.4
> 

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

* Re: [PATCH] KVM: LAPIC: Accurately guarantee busy wait for timer to expire when using hv_timer
  2021-04-28 22:56 ` Sean Christopherson
@ 2021-04-29 15:40   ` Paolo Bonzini
  0 siblings, 0 replies; 3+ messages in thread
From: Paolo Bonzini @ 2021-04-29 15:40 UTC (permalink / raw)
  To: Sean Christopherson, Wanpeng Li
  Cc: linux-kernel, kvm, Vitaly Kuznetsov, Wanpeng Li, Jim Mattson,
	Joerg Roedel

On 29/04/21 00:56, Sean Christopherson wrote:
> On Wed, Apr 28, 2021, Wanpeng Li wrote:
>> From: Wanpeng Li <wanpengli@tencent.com>
>>
>> Commit ee66e453db13d (KVM: lapic: Busy wait for timer to expire when
>> using hv_timer) tries to set ktime->expired_tscdeadline by checking
>> ktime->hv_timer_in_use since lapic timer oneshot/periodic modes which
>> are emulated by vmx preemption timer also get advanced, they leverage
>> the same vmx preemption timer logic with tsc-deadline mode. However,
>> ktime->hv_timer_in_use is cleared before apic_timer_expired() handling,
>> let's delay this clearing in preemption-disabled region.
>>
>> Fixes: ee66e453db13d (KVM: lapic: Busy wait for timer to expire when using hv_timer)
> 
> Well that's embarassing.  I suspect/hope I tested the case where start_hv_timer()
> detects the timer already expired.  On the plus side, start_hv_timer() calls
> cancel_hv_timer() after apic_timer_expired(), so there are unlikely to be hidden
> side effects (and cancel_hv_timer() is tiny).
> 
>> Cc: Sean Christopherson <seanjc@google.com>
>> Signed-off-by: Wanpeng Li <wanpengli@tencent.com>
> 
> Reviewed-by: Sean Christopherson <seanjc@google.com>
> 
>> ---
>>   arch/x86/kvm/lapic.c | 2 +-
>>   1 file changed, 1 insertion(+), 1 deletion(-)
>>
>> diff --git a/arch/x86/kvm/lapic.c b/arch/x86/kvm/lapic.c
>> index 152591f..c0ebef5 100644
>> --- a/arch/x86/kvm/lapic.c
>> +++ b/arch/x86/kvm/lapic.c
>> @@ -1913,8 +1913,8 @@ void kvm_lapic_expired_hv_timer(struct kvm_vcpu *vcpu)
>>   	if (!apic->lapic_timer.hv_timer_in_use)
>>   		goto out;
>>   	WARN_ON(rcuwait_active(&vcpu->wait));
>> -	cancel_hv_timer(apic);
>>   	apic_timer_expired(apic, false);
>> +	cancel_hv_timer(apic);
>>   
>>   	if (apic_lvtt_period(apic) && apic->lapic_timer.period) {
>>   		advance_periodic_target_expiration(apic);
>> -- 
>> 2.7.4
>>
> 

Queued, thanks.

Paolo


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

end of thread, other threads:[~2021-04-29 15:40 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-04-28 11:08 [PATCH] KVM: LAPIC: Accurately guarantee busy wait for timer to expire when using hv_timer Wanpeng Li
2021-04-28 22:56 ` Sean Christopherson
2021-04-29 15:40   ` Paolo Bonzini

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).