Skip to contentSkip to conversation

KVM: x86: Exit to userspace when kvm_check_nested_events fails

PATCH In review Paolo Bonzini 3 participants 6 messages Jul 28, 2021 – Aug 2, 2021

Subsystems (3)
lore
Share & tools
lore

Paolo Bonzini

author maintainer
#0 lore raw

PATCH 1/1 KVM: x86: Exit to userspace when kvm_check_nested_events fails

From: Jim Mattson <[email protected]>

If kvm_check_nested_events fails due to raising an EXIT_REASON_INTERNAL_ERROR, propagate it to userspace immediately, even if the vCPU would otherwise be sleeping. This happens for example when the posted interrupt descriptor points outside guest memory.

---
 arch/x86/kvm/x86.c | 22 +++++++++++++++-------
 1 file changed, 15 insertions(+), 7 deletions(-)
arch/x86/kvm/x86.c +15 -7
index 348452bb16bc..916c976e99ab 100644
@@ -9752,10 +9752,14 @@ static inline int vcpu_block(struct kvm *kvm, struct kvm_vcpu *vcpu)
 	return 1;
 }
 
-static inline bool kvm_vcpu_running(struct kvm_vcpu *vcpu)
+static inline int kvm_vcpu_running(struct kvm_vcpu *vcpu)
 {
-	if (is_guest_mode(vcpu))
-		kvm_check_nested_events(vcpu);
+	int r;
+	if (is_guest_mode(vcpu)) {
+		r = kvm_check_nested_events(vcpu);
+		if (r < 0 && r != -EBUSY)
+			return r;
+	}
 
 	return (vcpu->arch.mp_state == KVM_MP_STATE_RUNNABLE &&
 		!vcpu->arch.apf.halted);
@@ -9770,12 +9774,16 @@ static int vcpu_run(struct kvm_vcpu *vcpu)
 	vcpu->arch.l1tf_flush_l1d = true;
 
 	for (;;) {
-		if (kvm_vcpu_running(vcpu)) {
-			r = vcpu_enter_guest(vcpu);
-		} else {
-			r = vcpu_block(kvm, vcpu);
+		r = kvm_vcpu_running(vcpu);
+		if (r < 0) {
+			r = 0;
+			break;
 		}
 
+		if (r)
+			r = vcpu_enter_guest(vcpu);
+		else
+			r = vcpu_block(kvm, vcpu);
 		if (r <= 0)
 			break;
 
Signature · 1 line
2.27.0
#1 lore raw
Quoted 46 lines · Paolo Bonzini

From: Jim Mattson <[email protected]>

If kvm_check_nested_events fails due to raising an EXIT_REASON_INTERNAL_ERROR, propagate it to userspace immediately, even if the vCPU would otherwise be sleeping. This happens for example when the posted interrupt descriptor points outside guest memory.

---
 arch/x86/kvm/x86.c | 22 +++++++++++++++-------
 1 file changed, 15 insertions(+), 7 deletions(-)
arch/x86/kvm/x86.c +9 -7
index 348452bb16bc..916c976e99ab 100644
@@ -9752,10 +9752,14 @@ static inline int vcpu_block(struct kvm *kvm, struct kvm_vcpu *vcpu)
 	return 1;
 }
 
-static inline bool kvm_vcpu_running(struct kvm_vcpu *vcpu)
+static inline int kvm_vcpu_running(struct kvm_vcpu *vcpu)
 {
-	if (is_guest_mode(vcpu))
-		kvm_check_nested_events(vcpu);
+	int r;
+	if (is_guest_mode(vcpu)) {
+		r = kvm_check_nested_events(vcpu);
+		if (r < 0 && r != -EBUSY)
+			return r;
+	}
 
 	return (vcpu->arch.mp_state == KVM_MP_STATE_RUNNABLE &&
 		!vcpu->arch.apf.halted);
@@ -9770,12 +9774,16 @@ static int vcpu_run(struct kvm_vcpu *vcpu)
 	vcpu->arch.l1tf_flush_l1d = true;
 
 	for (;;) {
-		if (kvm_vcpu_running(vcpu)) {
-			r = vcpu_enter_guest(vcpu);
-		} else {
-			r = vcpu_block(kvm, vcpu);
+		r = kvm_vcpu_running(vcpu);
+		if (r < 0) {

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

Quoted 10 lines
arch/x86/kvm/x86.c +6 -0
+			r = 0;
+			break;
 		}
 
+		if (r)
+			r = vcpu_enter_guest(vcpu);
+		else
+			r = vcpu_block(kvm, vcpu);
 		if (r <= 0)
 			break;

Shouldn't we also change kvm_arch_vcpu_runnable() and check 'kvm_vcpu_running() > 0' now?

Signature · 1 line
Vitaly
author maintainer
#2 lore raw
Quoted 2 lines · Vitaly Kuznetsov

Shouldn't we also change kvm_arch_vcpu_runnable() and check 'kvm_vcpu_running() > 0' now?

I think leaving kvm_vcpu_block on error is the better choice, so it should be good with returning true if kvm_vcpu_running(vcpu) < 0.

Paolo

#3 lore raw
Quoted 6 lines · Paolo Bonzini
Quoted 2 lines · Vitaly Kuznetsov

Shouldn't we also change kvm_arch_vcpu_runnable() and check 'kvm_vcpu_running() > 0' now?

I think leaving kvm_vcpu_block on error is the better choice, so it should be good with returning true if kvm_vcpu_running(vcpu) < 0.

Blech. This is all gross. There is a subtle bug lurking in both Jim's approach and in this approach. It's not detected because the selftest exercises a bad PI descriptor, not a bad vAPIC page.

In Jim's approach of returning 'true' from kvm_vcpu_running() if kvm_check_nested_events() fails due to vmx_complete_nested_posted_interrupt() detecting a bad vAPIC page, the resulting KVM_EXIT_INTERNAL_ERROR will be "lost" due to vmx->nested.pi_pending being cleared. KVM runs the vCPU, but skips over the PI check in inject_pending_event() due to vmx->nested.pi_pending==false. The selftest works because the bad PI descriptor case is handled _before_ pi_pending is cleared.

This approach mostly fixes that bug by virtue of returning immediately in the vcpu_run() case, but if the bad vAPIC page is encountered via kvm_arch_vcpu_runnable(), KVM will effectively drop the error. This can be hack-a-fixed by pre-checking the vAPIC page. That's arguably architecturally wrong as the vAPIC emulation access shouldn't occur until after PI.ON is cleared, but from KVM's perspective I think it's the least awful "fix" given the current train wreck.

Alternatively, what about punting all of this in favor of targeting the full cleanup[*] for 5.15? I believe I have the bandwidth to pick that up.

[*] https://lkml.kernel.org/r/[email protected]

arch/x86/kvm/vmx/nested.c +4 -4
index 0d0dd6580cfd..8d1c8217954a 100644
@@ -3707,6 +3707,10 @@ static int vmx_complete_nested_posted_interrupt(struct kvm_vcpu *vcpu)
        if (!vmx->nested.pi_desc)
                goto mmio_needed;
 
+       vapic_page = vmx->nested.virtual_apic_map.hva;
+       if (!vapic_page)
+               goto mmio_needed;
+
        vmx->nested.pi_pending = false;
 
        if (!pi_test_and_clear_on(vmx->nested.pi_desc))
@@ -3714,10 +3718,6 @@ static int vmx_complete_nested_posted_interrupt(struct kvm_vcpu *vcpu)
 
        max_irr = find_last_bit((unsigned long *)vmx->nested.pi_desc->pir, 256);
        if (max_irr != 256) {
-               vapic_page = vmx->nested.virtual_apic_map.hva;
-               if (!vapic_page)
-                       goto mmio_needed;
-
                __kvm_apic_update_irr(vmx->nested.pi_desc->pir,
                        vapic_page, &max_irr);
                status = vmcs_read16(GUEST_INTR_STATUS);

[*]

author maintainer
#4 lore raw
Quoted 2 lines · Sean Christopherson

Alternatively, what about punting all of this in favor of targeting the full cleanup[*] for 5.15? I believe I have the bandwidth to pick that up.

That's fine of course. I'll keep this in queue for the moment so that I can at least run Jim's testcase, but otherwise won't merge it to kvm/next.

Paolo

#5 lore raw
In reply to Paolo Bonzini
Quoted 1 line · Paolo Bonzini

From: Jim Mattson <[email protected]>

I don't think this is actually from Jim.

Quoted 17 lines

If kvm_check_nested_events fails due to raising an EXIT_REASON_INTERNAL_ERROR, propagate it to userspace immediately, even if the vCPU would otherwise be sleeping. This happens for example when the posted interrupt descriptor points outside guest memory.

---
 arch/x86/kvm/x86.c | 22 +++++++++++++++-------
 1 file changed, 15 insertions(+), 7 deletions(-)
arch/x86/kvm/x86.c +0 -0
index 348452bb16bc..916c976e99ab 100644

arch/x86/kvm/x86.c static inline int vcpu_block(struct kvm *kvm, struct kvm_vcpu *vcpu) · line 9757

Quoted 9 lines
arch/x86/kvm/x86.c +2 -3
@@ -9752,10 +9752,14 @@ static inline int vcpu_block(struct kvm *kvm, struct kvm_vcpu *vcpu)
 	return 1;
 }
 
-static inline bool kvm_vcpu_running(struct kvm_vcpu *vcpu)
+static inline int kvm_vcpu_running(struct kvm_vcpu *vcpu)
 {
-	if (is_guest_mode(vcpu))
-		kvm_check_nested_events(vcpu);
+	int r;

newline

Quoted 32 lines
arch/x86/kvm/x86.c +13 -4
+	if (is_guest_mode(vcpu)) {
+		r = kvm_check_nested_events(vcpu);
+		if (r < 0 && r != -EBUSY)
+			return r;
+	}
 
 	return (vcpu->arch.mp_state == KVM_MP_STATE_RUNNABLE &&
 		!vcpu->arch.apf.halted);
@@ -9770,12 +9774,16 @@ static int vcpu_run(struct kvm_vcpu *vcpu)
 	vcpu->arch.l1tf_flush_l1d = true;
 
 	for (;;) {
-		if (kvm_vcpu_running(vcpu)) {
-			r = vcpu_enter_guest(vcpu);
-		} else {
-			r = vcpu_block(kvm, vcpu);
+		r = kvm_vcpu_running(vcpu);
+		if (r < 0) {
+			r = 0;
+			break;
 		}
 
+		if (r)
+			r = vcpu_enter_guest(vcpu);
+		else
+			r = vcpu_block(kvm, vcpu);
 		if (r <= 0)
 			break;
 
Signature · 1 line
2.27.0