Skip to contentSkip to conversation

KVM: LAPIC: Accurately guarantee busy wait for timer to expire when using hv_timer

PATCH Applied Wanpeng Li 3 participants 3 messages Apr 28, 2021 – Apr 29, 2021

Subsystems (3)
lore
Share & tools
lore

Wanpeng Li

author
#0 lore raw

PATCH 1/1 KVM: LAPIC: Accurately guarantee busy wait for timer to expire when using hv_timer

From: Wanpeng Li <[email protected]>

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 <[email protected]>
---
 arch/x86/kvm/lapic.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/x86/kvm/lapic.c +1 -1
index 152591f..c0ebef5 100644
@@ -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);
Signature · 1 line
2.7.4
maintainer Reviewed-by on v1 1/1
#1 lore raw
Quoted 11 lines · Wanpeng Li

From: Wanpeng Li <[email protected]>

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

Quoted 2 lines
Cc Sean Christopherson <[email protected]>
Quoted 21 lines
---
 arch/x86/kvm/lapic.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/x86/kvm/lapic.c +1 -1
index 152591f..c0ebef5 100644
@@ -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);
Signature · 1 line
2.7.4
maintainer Applied
#2 lore raw
Quoted 39 lines · Sean Christopherson
Quoted 11 lines · Wanpeng Li

From: Wanpeng Li <[email protected]>

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

Quoted 2 lines
Cc Sean Christopherson <[email protected]>
Quoted 16 lines
---
  arch/x86/kvm/lapic.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
arch/x86/kvm/lapic.c +1 -1
index 152591f..c0ebef5 100644
@@ -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);
 
Quoted 6 lines
Quoted 5 lines
arch/x86/kvm/lapic.c +0 -0
  	if (apic_lvtt_period(apic) && apic->lapic_timer.period) {
  		advance_periodic_target_expiration(apic);
Signature · 1 line
2.7.4

Queued, thanks.

Paolo