Skip to contentSkip to conversation

KVM: x86: accept userspace interrupt only if no event is injected

PATCH Superseded Paolo Bonzini 2 participants 3 messages Jul 27, 2021

Subsystems (4)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Paolo Bonzini

author maintainer
#0 lore raw

PATCH v2 1/1 KVM: x86: accept userspace interrupt only if no event is injected

Once an exception has been injected, any side effects related to the exception (such as setting CR2 or DR6) have been taked place. Therefore, once KVM sets the VM-entry interruption information field or the AMD EVENTINJ field, the next VM-entry must deliver that exception.

Pending interrupts are processed after injected exceptions, so in theory it would not be a problem to use KVM_INTERRUPT when an injected exception is present. However, DOSEMU is using run->ready_for_interrupt_injection to detect interrupt windows and then using KVM_SET_SREGS/KVM_SET_REGS to inject the interrupt manually. For this to work, the interrupt window must be delayed after the completion of the previous event injection.

Fixes 71cc849b7093 ("KVM: x86: Fix split-irqchip vs interrupt injection window request")
(cherry picked from commit 043264d97e9ab74cc9661c8b1f9c00c8ce24cad9)
---
 arch/x86/kvm/x86.c | 12 +++++++++++-
 1 file changed, 11 insertions(+), 1 deletion(-)
arch/x86/kvm/x86.c +11 -1
index 4116567f3d44..5e921f1e00db 100644
@@ -4358,8 +4358,18 @@ static int kvm_cpu_accept_dm_intr(struct kvm_vcpu *vcpu)
 
 static int kvm_vcpu_ready_for_interrupt_injection(struct kvm_vcpu *vcpu)
 {
+	/*
+	 * Do not cause an interrupt window exit if an exception
+	 * is pending or an event needs reinjection; userspace
+	 * might want to inject the interrupt manually using KVM_SET_REGS
+	 * or KVM_SET_SREGS.  For that to work, we must be at an
+	 * instruction boundary and with no events half-injected.
+	 */
 	return kvm_arch_interrupt_allowed(vcpu) &&
-		kvm_cpu_accept_dm_intr(vcpu);
+		kvm_cpu_accept_dm_intr(vcpu) &&
+	        !kvm_event_needs_reinjection(vcpu)
+	        !vcpu->arch.exception.pending;
+
 }
 
 static int kvm_vcpu_ioctl_interrupt(struct kvm_vcpu *vcpu,
Signature · 1 line
2.27.0
#1 lore raw
Quoted 15 lines · Paolo Bonzini

Once an exception has been injected, any side effects related to the exception (such as setting CR2 or DR6) have been taked place. Therefore, once KVM sets the VM-entry interruption information field or the AMD EVENTINJ field, the next VM-entry must deliver that exception.

Pending interrupts are processed after injected exceptions, so in theory it would not be a problem to use KVM_INTERRUPT when an injected exception is present. However, DOSEMU is using run->ready_for_interrupt_injection to detect interrupt windows and then using KVM_SET_SREGS/KVM_SET_REGS to inject the interrupt manually. For this to work, the interrupt window must be delayed after the completion of the previous event injection.

Quoted 6 lines
Fixes 71cc849b7093 ("KVM: x86: Fix split-irqchip vs interrupt injection window request")
(cherry picked from commit 043264d97e9ab74cc9661c8b1f9c00c8ce24cad9)

? I can't find 043264d97e9ab74cc9661c8b1f9c00c8ce24cad9 anywhere.

Quoted 12 lines
---
 arch/x86/kvm/x86.c | 12 +++++++++++-
 1 file changed, 11 insertions(+), 1 deletion(-)
arch/x86/kvm/x86.c +0 -0
index 4116567f3d44..5e921f1e00db 100644
@@ -4358,8 +4358,18 @@ static int kvm_cpu_accept_dm_intr(struct kvm_vcpu *vcpu)
 
 static int kvm_vcpu_ready_for_interrupt_injection(struct kvm_vcpu *vcpu)
 {

arch/x86/kvm/x86.c static int kvm_cpu_accept_dm_intr(struct kvm_vcpu *vcpu) · line 4369

Quoted 10 lines
arch/x86/kvm/x86.c +8 -1
+	/*
+	 * Do not cause an interrupt window exit if an exception
+	 * is pending or an event needs reinjection; userspace
+	 * might want to inject the interrupt manually using KVM_SET_REGS
+	 * or KVM_SET_SREGS.  For that to work, we must be at an
+	 * instruction boundary and with no events half-injected.
+	 */
 	return kvm_arch_interrupt_allowed(vcpu) &&
-		kvm_cpu_accept_dm_intr(vcpu);
+		kvm_cpu_accept_dm_intr(vcpu) &&

Opportunistically align this indentation?

arch/x86/kvm/x86.c static int kvm_cpu_accept_dm_intr(struct kvm_vcpu *vcpu) · line 4370

Quoted 1 line
arch/x86/kvm/x86.c +1 -0
+	        !kvm_event_needs_reinjection(vcpu)

Missing &&, apparently the mysterious cherry-pick didn't go so well :-)

Quoted 8 lines
arch/x86/kvm/x86.c +2 -0
+	        !vcpu->arch.exception.pending;
+
 }
 
 static int kvm_vcpu_ioctl_interrupt(struct kvm_vcpu *vcpu,
Signature · 1 line
2.27.0
author maintainer
#2 lore raw
Quoted 10 lines · Sean Christopherson
Quoted 10 lines
---
  arch/x86/kvm/x86.c | 12 +++++++++++-
  1 file changed, 11 insertions(+), 1 deletion(-)
arch/x86/kvm/x86.c +0 -0
index 4116567f3d44..5e921f1e00db 100644
@@ -4358,8 +4358,18 @@ static int kvm_cpu_accept_dm_intr(struct kvm_vcpu *vcpu)
 
Quoted 14 lines

arch/x86/kvm/x86.c static int kvm_cpu_accept_dm_intr(struct kvm_vcpu *vcpu) · line 4369

Quoted 12 lines
arch/x86/kvm/x86.c +8 -1
  static int kvm_vcpu_ready_for_interrupt_injection(struct kvm_vcpu *vcpu)
  {
+	/*
+	 * Do not cause an interrupt window exit if an exception
+	 * is pending or an event needs reinjection; userspace
+	 * might want to inject the interrupt manually using KVM_SET_REGS
+	 * or KVM_SET_SREGS.  For that to work, we must be at an
+	 * instruction boundary and with no events half-injected.
+	 */
  	return kvm_arch_interrupt_allowed(vcpu) &&
-		kvm_cpu_accept_dm_intr(vcpu);
+		kvm_cpu_accept_dm_intr(vcpu) &&

Opportunistically align this indentation?

Yep, good idea.

Quoted 3 lines

arch/x86/kvm/x86.c static int kvm_cpu_accept_dm_intr(struct kvm_vcpu *vcpu) · line 4370

Quoted 1 line
arch/x86/kvm/x86.c +1 -0
+	        !kvm_event_needs_reinjection(vcpu)

Missing &&, apparently the mysterious cherry-pick didn't go so well :-)

Well, yeah. The only way I can excuse myself, is by not being the kind of person that yells for such stupid things...

Paolo