kvm: arm/arm64: Fix emulated physical timer IRQ injection
Subsystems (2)
PATCH 1/1 kvm: arm/arm64: Fix emulated physical timer IRQ injection
When KVM emulates a physical timer, we keep track of the interrupt condition and try to inject an IRQ to the guest when needed. This works if the timer expires when either the guest is running or KVM does work on behalf of it, since it calls kvm_timer_update_state(). However when the guest's VCPU is not scheduled (for instance because the guest issued a WFI instruction before), we miss injecting the interrupt when the VCPU's state gets restored back in kvm_timer_vcpu_load().
Fix this by moving the interrupt injection check into the phys_timer_emulate() function, so that all possible paths of execution are covered.
This fixes the physical timer emulation, which broke when it got changed in the 4.15 merge window.
The respective kvm-unit-test check has been posted already.
--- virt/kvm/arm/arch_timer.c | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-)
virt/kvm/arm/arch_timer.c +13 -8
@@ -294,17 +294,26 @@ static void phys_timer_emulate(struct kvm_vcpu *vcpu)struct arch_timer_cpu *timer = &vcpu->arch.timer_cpu;struct arch_timer_context *ptimer =vcpu_ptimer (vcpu); +/* If the timer cannot fire at all, then we don't need a soft timer. */ +if (!kvm_timer_irq_can_fire (ptimer)) { +soft_timer_cancel (&timer->phys_timer,NULL ); +kvm_timer_update_irq (vcpu, false, ptimer); +return ; + } +/* -* If the timer can fire now we have just raised the IRQ line and we -* don't need to have a soft timer scheduled for the future. If the -* timer cannot fire at all, then we also don't need a soft timer. +* If the timer can fire now, we don't need to have a soft timer +* scheduled for the future, as we also raise the IRQ line. */ -if (kvm_timer_should_fire (ptimer) || !kvm_timer_irq_can_fire (ptimer)) { +if (kvm_timer_should_fire (ptimer)) {soft_timer_cancel (&timer->phys_timer,NULL ); +kvm_timer_update_irq (vcpu, true, ptimer); +return ; }soft_timer_start (&timer->phys_timer,kvm_timer_compute_delta (ptimer)); +kvm_timer_update_irq (vcpu, false, ptimer); }/*
@@ -316,7 +325,6 @@ static void kvm_timer_update_state(struct kvm_vcpu *vcpu) {struct arch_timer_cpu *timer = &vcpu->arch.timer_cpu;struct arch_timer_context *vtimer =vcpu_vtimer (vcpu); -struct arch_timer_context *ptimer =vcpu_ptimer (vcpu);bool level;if (unlikely (!timer->enabled))
@@ -332,9 +340,6 @@ static void kvm_timer_update_state(struct kvm_vcpu *vcpu) level =kvm_timer_should_fire (vtimer);kvm_timer_update_irq (vcpu, level, vtimer); -if (kvm_timer_should_fire (ptimer) != ptimer->irq.level) -kvm_timer_update_irq (vcpu, !ptimer->irq.level, ptimer); -phys_timer_emulate (vcpu); }
Signature · 1 line
2.14.4
Quoted 44 lines · Andre Przywara
When KVM emulates a physical timer, we keep track of the interrupt condition and try to inject an IRQ to the guest when needed. This works if the timer expires when either the guest is running or KVM does work on behalf of it, since it calls
kvm_timer_update_state(). However when the guest's VCPU is not scheduled (for instance because the guest issued a WFI instruction before), we miss injecting the interrupt when the VCPU's state gets restored back inkvm_timer_vcpu_load().Fix this by moving the interrupt injection check into the
phys_timer_emulate()function, so that all possible paths of execution are covered.This fixes the physical timer emulation, which broke when it got changed in the 4.15 merge window.
The respective kvm-unit-test check has been posted already.Cc Stable <[email protected]> # 4.15+Signed-off-by Andre Przywara <[email protected]>--- virt/kvm/arm/arch_timer.c | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-)virt/kvm/arm/arch_timer.c +9
-3@@ -294,17 +294,26 @@ static void phys_timer_emulate(struct kvm_vcpu *vcpu)struct arch_timer_cpu *timer = &vcpu->arch.timer_cpu;struct arch_timer_context *ptimer =vcpu_ptimer (vcpu); +/* If the timer cannot fire at all, then we don't need a soft timer. */ +if (!kvm_timer_irq_can_fire (ptimer)) { +soft_timer_cancel (&timer->phys_timer,NULL ); +kvm_timer_update_irq (vcpu, false, ptimer); +return ; + } +/* -* If the timer can fire now we have just raised the IRQ line and we -* don't need to have a soft timer scheduled for the future. If the -* timer cannot fire at all, then we also don't need a soft timer. +* If the timer can fire now, we don't need to have a soft timer +* scheduled for the future, as we also raise the IRQ line. */
virt/kvm/arm/arch_timer.c static void phys_timer_emulate(struct kvm_vcpu *vcpu) · line 316
Quoted 10 lines
virt/kvm/arm/arch_timer.c +4
-1-if (kvm_timer_should_fire (ptimer) || !kvm_timer_irq_can_fire (ptimer)) { +if (kvm_timer_should_fire (ptimer)) {soft_timer_cancel (&timer->phys_timer,NULL ); +kvm_timer_update_irq (vcpu, true, ptimer); +return ; }soft_timer_start (&timer->phys_timer,kvm_timer_compute_delta (ptimer)); +kvm_timer_update_irq (vcpu, false, ptimer);
I think this bit is wrong. The soft timer could have expired by the time you get to lower the line, and you would just loose that interrupt. Lowering the line *before* starting the soft timer would make a lot more sense to me.
Quoted 13 lines
virt/kvm/arm/arch_timer.c +0
-1}/* @@ -316,7 +325,6 @@ static void kvm_timer_update_state(struct kvm_vcpu *vcpu) {struct arch_timer_cpu *timer = &vcpu->arch.timer_cpu;struct arch_timer_context *vtimer =vcpu_vtimer (vcpu); -struct arch_timer_context *ptimer =vcpu_ptimer (vcpu);bool level;if (unlikely (!timer->enabled))
virt/kvm/arm/arch_timer.c static void kvm_timer_update_state(struct kvm_vcpu *vcpu) · line 345
Quoted 9 lines
virt/kvm/arm/arch_timer.c +0
-3@@ -332,9 +340,6 @@ static void kvm_timer_update_state(struct kvm_vcpu *vcpu) level =kvm_timer_should_fire (vtimer);kvm_timer_update_irq (vcpu, level, vtimer); -if (kvm_timer_should_fire (ptimer) != ptimer->irq.level) -kvm_timer_update_irq (vcpu, !ptimer->irq.level, ptimer); -phys_timer_emulate (vcpu); }
Thanks,
M.
Signature · 1 line
Jazz is not dead. It just smells funny...
Quoted 27 lines · Andre Przywara
When KVM emulates a physical timer, we keep track of the interrupt condition and try to inject an IRQ to the guest when needed. This works if the timer expires when either the guest is running or KVM does work on behalf of it, since it calls
kvm_timer_update_state(). However when the guest's VCPU is not scheduled (for instance because the guest issued a WFI instruction before), we miss injecting the interrupt when the VCPU's state gets restored back inkvm_timer_vcpu_load().Fix this by moving the interrupt injection check into the
phys_timer_emulate()function, so that all possible paths of execution are covered.This fixes the physical timer emulation, which broke when it got changed in the 4.15 merge window.
The respective kvm-unit-test check has been posted already.Cc Stable <[email protected]> # 4.15+Signed-off-by Andre Przywara <[email protected]>--- virt/kvm/arm/arch_timer.c | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-)virt/kvm/arm/arch_timer.c +0
-0
virt/kvm/arm/arch_timer.c static void phys_timer_emulate(struct kvm_vcpu *vcpu) · line 302
Quoted 9 lines
virt/kvm/arm/arch_timer.c +6
-0@@ -294,17 +294,26 @@ static void phys_timer_emulate(struct kvm_vcpu *vcpu)struct arch_timer_cpu *timer = &vcpu->arch.timer_cpu;struct arch_timer_context *ptimer =vcpu_ptimer (vcpu); +/* If the timer cannot fire at all, then we don't need a soft timer. */ +if (!kvm_timer_irq_can_fire (ptimer)) { +soft_timer_cancel (&timer->phys_timer,NULL ); +kvm_timer_update_irq (vcpu, false, ptimer); +return ; + }
This stuff is breaking what the intention is with the phys_timer_emulate() function. See the comment above the function, which says that this is about scheduling the background soft timer, and not about managing the rest of the state.
At least that function needs updating.
Quoted 8 lines
virt/kvm/arm/arch_timer.c +3
-3+/* -* If the timer can fire now we have just raised the IRQ line and we -* don't need to have a soft timer scheduled for the future. If the -* timer cannot fire at all, then we also don't need a soft timer. +* If the timer can fire now, we don't need to have a soft timer +* scheduled for the future, as we also raise the IRQ line. */
virt/kvm/arm/arch_timer.c static void phys_timer_emulate(struct kvm_vcpu *vcpu) · line 316
Quoted 10 lines
virt/kvm/arm/arch_timer.c +4
-1-if (kvm_timer_should_fire (ptimer) || !kvm_timer_irq_can_fire (ptimer)) { +if (kvm_timer_should_fire (ptimer)) {soft_timer_cancel (&timer->phys_timer,NULL ); +kvm_timer_update_irq (vcpu, true, ptimer); +return ; }soft_timer_start (&timer->phys_timer,kvm_timer_compute_delta (ptimer)); +kvm_timer_update_irq (vcpu, false, ptimer);
This is clearly racy as you'll lower the interrupt that may have just fired between these two lines.
Quoted 24 lines
virt/kvm/arm/arch_timer.c +0
-4}/* @@ -316,7 +325,6 @@ static void kvm_timer_update_state(struct kvm_vcpu *vcpu) {struct arch_timer_cpu *timer = &vcpu->arch.timer_cpu;struct arch_timer_context *vtimer =vcpu_vtimer (vcpu); -struct arch_timer_context *ptimer =vcpu_ptimer (vcpu);bool level;if (unlikely (!timer->enabled))@@ -332,9 +340,6 @@ static void kvm_timer_update_state(struct kvm_vcpu *vcpu) level =kvm_timer_should_fire (vtimer);kvm_timer_update_irq (vcpu, level, vtimer); -if (kvm_timer_should_fire (ptimer) != ptimer->irq.level) -kvm_timer_update_irq (vcpu, !ptimer->irq.level, ptimer); -phys_timer_emulate (vcpu); }Signature · 1 line
2.14.4
Overall, I think this approach is wrong.
Please see the following suggestion for doing this in two patches instead:
Quoted 1 line
From
08b40157cc6a69d72a37afff38fce9d0b482953fMon Sep 17 00:00:00 2001
From: Christoffer Dall <[email protected]>
Date: Tue, 17 Jul 2018 11:53:39 +0200
Subject: [PATCH 1/2] KVM: arm/arm64: Fix potential loss of ptimer interrupts
kvm_timer_update_state() is called when changing the phys timer configuration registers, either via vcpu reset, as a result of a trap from the guest, or when userspace programs the registers.
phys_timer_emulate() is in turn called by kvm_timer_update_state() to either cancel an existing software timer, or program a new software timer, to emulate the behavior of a real phys timer, based on the change in configuration registers.
Unfortunately, the interaction between these two functions left a small race; if the conceptual emulated phys timer should actually fire, but the soft timer hasn't executed its callback yet, we cancel the timer in phys_timer_emulate without injecting an irq. This only happens if the check in kvm_timer_update_state is called before the timer should fire, which is relatively unlikely, but possible.
The solution is to update the state of the phys timer after calling phys_timer_emulate, which will pick up the pending timer state and update the interrupt value.
Note that this leaves the opportunity of raising the interrupt twice, once in the just-programmed soft timer, and once in kvm_timer_update_state. Since this always happens synchronously with the VCPU execution, there is no harm in this, and the guest ever only sees a single timer interrupt.
--- virt/kvm/arm/arch_timer.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
virt/kvm/arm/arch_timer.c +2 -2
@@ -332,10 +332,10 @@ static void kvm_timer_update_state(struct kvm_vcpu *vcpu) level =kvm_timer_should_fire (vtimer);kvm_timer_update_irq (vcpu, level, vtimer); +phys_timer_emulate (vcpu); +if (kvm_timer_should_fire (ptimer) != ptimer->irq.level)kvm_timer_update_irq (vcpu, !ptimer->irq.level, ptimer); - -phys_timer_emulate (vcpu); }static void vtimer_save_state (struct kvm_vcpu *vcpu)
Signature · 1 line
2.7.4
Quoted 1 line
From
bce5c9ec3d7e0e2e7b8d4c733a18c0a987be16f5Mon Sep 17 00:00:00 2001
From: Christoffer Dall <[email protected]>
Date: Tue, 17 Jul 2018 12:04:28 +0200
Subject: [PATCH 2/2] KVM: arm/arm64: Fix lost IRQs from emulated physcial
timer when blocked
When the VCPU is blocked (for example from WFI) we don't inject the physical timer interrupt if it should fire while the CPU is blocked, but instead we just wake up the VCPU and expect kvm_timer_vcpu_load to take care of injecting the interrupt.
Unfortunately, kvm_timer_vcpu_load() doesn't actually do that, it only has support to schedule a soft timer if the emulated phys timer is expected to fire in the future.
Follow the same pattern as kvm_timer_update_state() and update the irq state after potentially scheduling a soft timer.
--- virt/kvm/arm/arch_timer.c | 4 ++++ 1 file changed, 4 insertions(+)
virt/kvm/arm/arch_timer.c +4 -0
@@ -502,6 +502,10 @@ void kvm_timer_vcpu_load(struct kvm_vcpu *vcpu)/* Set the background timer for the physical timer emulation. */ phys_timer_emulate (vcpu); + +/* If the timer fired while we weren't running, inject it now */ +if (kvm_timer_should_fire (ptimer) != ptimer->irq.level) +kvm_timer_update_irq (vcpu, !ptimer->irq.level, ptimer); }bool kvm_timer_should_notify_user (struct kvm_vcpu *vcpu)
Signature · 6 lines
2.7.4 Thanks, -Christoffer
Hi,
Quoted 43 lines · Christoffer Dall
Quoted 27 lines · Andre Przywara
When KVM emulates a physical timer, we keep track of the interrupt condition and try to inject an IRQ to the guest when needed. This works if the timer expires when either the guest is running or KVM does work on behalf of it, since it calls
kvm_timer_update_state(). However when the guest's VCPU is not scheduled (for instance because the guest issued a WFI instruction before), we miss injecting the interrupt when the VCPU's state gets restored back inkvm_timer_vcpu_load().Fix this by moving the interrupt injection check into the
phys_timer_emulate()function, so that all possible paths of execution are covered.This fixes the physical timer emulation, which broke when it got changed in the 4.15 merge window.
The respective kvm-unit-test check has been posted already.Cc Stable <[email protected]> # 4.15+Signed-off-by Andre Przywara <[email protected]>--- virt/kvm/arm/arch_timer.c | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-)virt/kvm/arm/arch_timer.c +0
-0virt/kvm/arm/arch_timer.c static void phys_timer_emulate(struct kvm_vcpu *vcpu) · line 302
Quoted 9 lines
virt/kvm/arm/arch_timer.c +6
-0@@ -294,17 +294,26 @@ static void phys_timer_emulate(struct kvm_vcpu *vcpu)struct arch_timer_cpu *timer = &vcpu->arch.timer_cpu;struct arch_timer_context *ptimer =vcpu_ptimer (vcpu); +/* If the timer cannot fire at all, then we don't need a soft timer. */ +if (!kvm_timer_irq_can_fire (ptimer)) { +soft_timer_cancel (&timer->phys_timer,NULL ); +kvm_timer_update_irq (vcpu, false, ptimer); +return ; + }This stuff is breaking what the intention is with the
phys_timer_emulate()function. See the comment above the function, which says that this is about scheduling the background soft timer, and not about managing the rest of the state.
Quoted 1 line
At least that function needs updating.
Well, my naive understanding of "phys_timer_emulate()" was that it takes care of the whole task, and injecting the IRQ is an integral part of the package. As it stands with your patches now, the caller of phys_timer_emulate() has to take care of the IRQ injection, which smells a bit fragile. So since we have this "phys_timer_emulate(); if() kvm_timer_update_irq() ...;" sequence now exactly twice, I found it more logical to move that into the function.
But I don't really care (or we wrap this once more as Marc suggested). And as your version also works (tested with both kvm-unit-tests and by running a hacked Linux guest which always uses the physical timer), feel free to send your patches. You might want to declare ptimer in kvm_timer_vcpu_load() though ;-) and also amend this comment in phys_timer_emulate().
I think having this separate bites me a bit in the nested virt rework (because it's more than one timer to emulate), but I can fix this there.
Quoted 16 lines
Quoted 16 lines
virt/kvm/arm/arch_timer.c +6
-4+/* -* If the timer can fire now we have just raised the IRQ line and we -* don't need to have a soft timer scheduled for the future. If the -* timer cannot fire at all, then we also don't need a soft timer. +* If the timer can fire now, we don't need to have a soft timer +* scheduled for the future, as we also raise the IRQ line. */ -if (kvm_timer_should_fire (ptimer) || !kvm_timer_irq_can_fire (ptimer)) { +if (kvm_timer_should_fire (ptimer)) {soft_timer_cancel (&timer->phys_timer,NULL ); +kvm_timer_update_irq (vcpu, true, ptimer); +return ; }
Quoted 5 lines
virt/kvm/arm/arch_timer.c static void phys_timer_emulate(struct kvm_vcpu *vcpu) · line 316
Quoted 2 lines
virt/kvm/arm/arch_timer.c +1
-0soft_timer_start (&timer->phys_timer,kvm_timer_compute_delta (ptimer)); +kvm_timer_update_irq (vcpu, false, ptimer);This is clearly racy as you'll lower the interrupt that may have just fired between these two lines.
True, Marc pointed that out already and I fixed in in v2.
Cheers,
Andre.
Quoted 25 lines
Quoted 24 lines
virt/kvm/arm/arch_timer.c +0
-4}/* @@ -316,7 +325,6 @@ static void kvm_timer_update_state(struct kvm_vcpu *vcpu) {struct arch_timer_cpu *timer = &vcpu->arch.timer_cpu;struct arch_timer_context *vtimer =vcpu_vtimer (vcpu); -struct arch_timer_context *ptimer =vcpu_ptimer (vcpu);bool level;if (unlikely (!timer->enabled))@@ -332,9 +340,6 @@ static void kvm_timer_update_state(struct kvm_vcpu *vcpu) level =kvm_timer_should_fire (vtimer);kvm_timer_update_irq (vcpu, level, vtimer); -if (kvm_timer_should_fire (ptimer) != ptimer->irq.level) -kvm_timer_update_irq (vcpu, !ptimer->irq.level, ptimer); -phys_timer_emulate (vcpu); }Signature · 1 line
2.14.4
Quoted 46 lines · Andre Przywara
Hi,
Quoted 43 lines · Christoffer Dall
Quoted 27 lines · Andre Przywara
When KVM emulates a physical timer, we keep track of the interrupt condition and try to inject an IRQ to the guest when needed. This works if the timer expires when either the guest is running or KVM does work on behalf of it, since it calls
kvm_timer_update_state(). However when the guest's VCPU is not scheduled (for instance because the guest issued a WFI instruction before), we miss injecting the interrupt when the VCPU's state gets restored back inkvm_timer_vcpu_load().Fix this by moving the interrupt injection check into the
phys_timer_emulate()function, so that all possible paths of execution are covered.This fixes the physical timer emulation, which broke when it got changed in the 4.15 merge window.
The respective kvm-unit-test check has been posted already.Cc Stable <[email protected]> # 4.15+Signed-off-by Andre Przywara <[email protected]>--- virt/kvm/arm/arch_timer.c | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-)virt/kvm/arm/arch_timer.c +0
-0virt/kvm/arm/arch_timer.c static void phys_timer_emulate(struct kvm_vcpu *vcpu) · line 302
Quoted 9 lines
virt/kvm/arm/arch_timer.c +6
-0@@ -294,17 +294,26 @@ static void phys_timer_emulate(struct kvm_vcpu *vcpu)struct arch_timer_cpu *timer = &vcpu->arch.timer_cpu;struct arch_timer_context *ptimer =vcpu_ptimer (vcpu); +/* If the timer cannot fire at all, then we don't need a soft timer. */ +if (!kvm_timer_irq_can_fire (ptimer)) { +soft_timer_cancel (&timer->phys_timer,NULL ); +kvm_timer_update_irq (vcpu, false, ptimer); +return ; + }This stuff is breaking what the intention is with the
phys_timer_emulate()function. See the comment above the function, which says that this is about scheduling the background soft timer, and not about managing the rest of the state.
Quoted 5 lines
Quoted 1 line
At least that function needs updating.
Well, my naive understanding of "
phys_timer_emulate()" was that it takes care of the whole task, and injecting the IRQ is an integral part of the package.
I don't understand how you could come to that conclusion given that the comment says otherwise and the function doesn't do anything with IRQs, but ok...
Quoted 5 lines
As it stands with your patches now, the caller of
phys_timer_emulate()has to take care of the IRQ injection, which smells a bit fragile. So since we have this "phys_timer_emulate();if()kvm_timer_update_irq()...;" sequence now exactly twice, I found it more logical to move that into the function.
Quoted 9 lines
But I don't really care (or we wrap this once more as Marc suggested). And as your version also works (tested with both kvm-unit-tests and by running a hacked Linux guest which always uses the physical timer), feel free to send your patches. You might want to declare ptimer in
kvm_timer_vcpu_load()though ;-) and also amend this comment inphys_timer_emulate().
I think having this separate bites me a bit in the nested virt rework (because it's more than one timer to emulate), but I can fix this there.
That's a resonable argument for having it embedded in a single function.
I'll send another version of my patches then.
Thanks,
-Christoffer