Skip to contentSkip to conversation

KVM: x86: fix restart_apic_timer

PATCH Applied Radim Krčmář 3 participants 9 messages Oct 6, 2017 – Oct 10, 2017

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

cover KVM: x86: fix restart_apic_timer

Various bugs that incorrectly injected a timer interrupt.

Going to work on kvm-unit-tests for this too.

Radim Krčmář (3):
KVM: x86: handle 0 write to TSC_DEADLINE MSR
KVM: x86: really disarm lapic timer when clearing TMICT
KVM: x86: thoroughly disarm LAPIC timer around TSC deadline switch

 arch/x86/kvm/lapic.c | 11 +++++++++--
 1 file changed, 9 insertions(+), 2 deletions(-)
Signature · 1 line
2.14.2
#1 lore raw

PATCH 1/3 KVM: x86: handle 0 write to TSC_DEADLINE MSR

0 should disable the timer, but start_hv_timer will recognize it as an expired timer instead.

---
 arch/x86/kvm/lapic.c | 3 +++
 1 file changed, 3 insertions(+)
arch/x86/kvm/lapic.c +3 -0
index 2c9e88a82738..39c1ae11ce1d 100644
@@ -1549,6 +1549,9 @@ static bool start_hv_timer(struct kvm_lapic *apic)
 	if (!apic_lvtt_period(apic) && atomic_read(&ktimer->pending))
 		return false;
 
+	if (!ktimer->tscdeadline)
+		return false;
+
 	r = kvm_x86_ops->set_hv_timer(apic->vcpu, ktimer->tscdeadline);
 	if (r < 0)
 		return false;
Signature · 1 line
2.14.2
Reviewed-by on v1 1/3
#2 lore raw

Re: [PATCH 1/3] KVM: x86: handle 0 write to TSC_DEADLINE MSR

2017-10-07 1:25 GMT+08:00 Radim Krčmář <[email protected]>:

Quoted 4 lines

0 should disable the timer, but start_hv_timer will recognize it as an expired timer instead.

Quoted 21 lines
---
 arch/x86/kvm/lapic.c | 3 +++
 1 file changed, 3 insertions(+)
arch/x86/kvm/lapic.c +3 -1
index 2c9e88a82738..39c1ae11ce1d 100644
@@ -1549,6 +1549,9 @@ static bool start_hv_timer(struct kvm_lapic *apic)
        if (!apic_lvtt_period(apic) && atomic_read(&ktimer->pending))
                return false;
 
+       if (!ktimer->tscdeadline)
+               return false;
+
        r = kvm_x86_ops->set_hv_timer(apic->vcpu, ktimer->tscdeadline);
        if (r < 0)
                return false;
--

2.14.2

#3 lore raw

PATCH 2/3 KVM: x86: really disarm lapic timer when clearing TMICT

In reply to Radim Krčmář

preemption timer only looks at tscdeadline and could inject already disarmed timer.

---
 arch/x86/kvm/lapic.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
arch/x86/kvm/lapic.c +3 -1
index 39c1ae11ce1d..96ade848ae0b 100644
@@ -1489,8 +1489,10 @@ static bool set_target_expiration(struct kvm_lapic *apic)
 	apic->lapic_timer.period = (u64)kvm_lapic_get_reg(apic, APIC_TMICT)
 		* APIC_BUS_CYCLE_NS * apic->divide_count;
 
-	if (!apic->lapic_timer.period)
+	if (!apic->lapic_timer.period) {
+		apic->lapic_timer.tscdeadline = 0;
 		return false;
+	}
 
 	limit_periodic_timer_frequency(apic);
 
Signature · 1 line
2.14.2
Reviewed-by on v1 2/3
#4 lore raw

Re: [PATCH 2/3] KVM: x86: really disarm lapic timer when clearing TMICT

2017-10-07 1:25 GMT+08:00 Radim Krčmář <[email protected]>:

Quoted 4 lines

preemption timer only looks at tscdeadline and could inject already disarmed timer.

Quoted 23 lines
---
 arch/x86/kvm/lapic.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
arch/x86/kvm/lapic.c +3 -2
index 39c1ae11ce1d..96ade848ae0b 100644
@@ -1489,8 +1489,10 @@ static bool set_target_expiration(struct kvm_lapic *apic)
        apic->lapic_timer.period = (u64)kvm_lapic_get_reg(apic, APIC_TMICT)
                * APIC_BUS_CYCLE_NS * apic->divide_count;
 
-       if (!apic->lapic_timer.period)
+       if (!apic->lapic_timer.period) {
+               apic->lapic_timer.tscdeadline = 0;
                return false;
+       }
 
        limit_periodic_timer_frequency(apic);
 
--

2.14.2

#5 lore raw

PATCH 3/3 KVM: x86: thoroughly disarm LAPIC timer around TSC deadline switch

In reply to Radim Krčmář

Our routines look at tscdeadline and period when deciding state of a timer. The timer is disarmed when switching between TSC deadline and other modes, so we should set everything to disarmed state.

---
 arch/x86/kvm/lapic.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
arch/x86/kvm/lapic.c +3 -1
index 96ade848ae0b..a778f1ae2927 100644
@@ -1330,8 +1330,10 @@ static void apic_update_lvtt(struct kvm_lapic *apic)
 	if (apic->lapic_timer.timer_mode != timer_mode) {
 		if (apic_lvtt_tscdeadline(apic) != (timer_mode ==
 				APIC_LVT_TIMER_TSCDEADLINE)) {
-			kvm_lapic_set_reg(apic, APIC_TMICT, 0);
 			hrtimer_cancel(&apic->lapic_timer.timer);
+			kvm_lapic_set_reg(apic, APIC_TMICT, 0);
+			apic->lapic_timer.period = 0;
+			apic->lapic_timer.tscdeadline = 0;
 		}
 		apic->lapic_timer.timer_mode = timer_mode;
 		limit_periodic_timer_frequency(apic);
Signature · 1 line
2.14.2
Reviewed-by on v1 3/3
#6 lore raw

Re: [PATCH 3/3] KVM: x86: thoroughly disarm LAPIC timer around TSC deadline switch

2017-10-07 1:25 GMT+08:00 Radim Krčmář <[email protected]>:

Quoted 5 lines

Our routines look at tscdeadline and period when deciding state of a timer. The timer is disarmed when switching between TSC deadline and other modes, so we should set everything to disarmed state.

Quoted 23 lines
---
 arch/x86/kvm/lapic.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
arch/x86/kvm/lapic.c +3 -2
index 96ade848ae0b..a778f1ae2927 100644
@@ -1330,8 +1330,10 @@ static void apic_update_lvtt(struct kvm_lapic *apic)
        if (apic->lapic_timer.timer_mode != timer_mode) {
                if (apic_lvtt_tscdeadline(apic) != (timer_mode ==
                                APIC_LVT_TIMER_TSCDEADLINE)) {
-                       kvm_lapic_set_reg(apic, APIC_TMICT, 0);
                        hrtimer_cancel(&apic->lapic_timer.timer);
+                       kvm_lapic_set_reg(apic, APIC_TMICT, 0);
+                       apic->lapic_timer.period = 0;
+                       apic->lapic_timer.tscdeadline = 0;
                }
                apic->lapic_timer.timer_mode = timer_mode;
                limit_periodic_timer_frequency(apic);
--

2.14.2

#7 lore raw
In reply to Radim Krčmář

2017-10-07 1:25 GMT+08:00 Radim Krčmář <[email protected]>:

Quoted 3 lines

Various bugs that incorrectly injected a timer interrupt.

Going to work on kvm-unit-tests for this too.

Btw, there is a testcase which I posted before also can be used to test apic timer mode transition.
https://patchwork.kernel.org/patch/9976971/

Regards,
Wanpeng Li

Quoted 13 lines

Radim Krčmář (3):
KVM: x86: handle 0 write to TSC_DEADLINE MSR
KVM: x86: really disarm lapic timer when clearing TMICT
KVM: x86: thoroughly disarm LAPIC timer around TSC deadline switch

 arch/x86/kvm/lapic.c | 11 +++++++++--
 1 file changed, 9 insertions(+), 2 deletions(-)

--
2.14.2

maintainer Applied
#8 lore raw
In reply to Radim Krčmář
Quoted 5 lines · Radim Krčmář

Various bugs that incorrectly injected a timer interrupt.

Going to work on kvm-unit-tests for this too.

Quoted 8 lines

Radim Krčmář (3):
KVM: x86: handle 0 write to TSC_DEADLINE MSR
KVM: x86: really disarm lapic timer when clearing TMICT
KVM: x86: thoroughly disarm LAPIC timer around TSC deadline switch

 arch/x86/kvm/lapic.c | 11 +++++++++--
 1 file changed, 9 insertions(+), 2 deletions(-)

Queued, thanks.

Paolo