Skip to contentSkip to conversation

KVM: prevent spurious exit to userspace during task switch emulation.

PATCH In review Gleb Natapov 3 participants 3 messages Apr 15, 2010

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

PATCH v2 1/1 KVM: prevent spurious exit to userspace during task switch emulation.

If kvm_task_switch() fails code exits to userspace without specifying exit reason, so the previous exit reason is reused by userspace. Fix this by specifying exit reason correctly.

---
Changelog:
v1->v2:
- report emulation error to userspace instead of ignoring it silently.

Should be applied after "KVM: fix emulator_task_switch() return value." since it relies on new return value from kvm_task_switch().

arch/x86/kvm/svm.c +7 -1
index c773a46..4a7a9ff 100644
@@ -2254,7 +2254,13 @@ static int task_switch_interception(struct vcpu_svm *svm)
 	     (int_vec == OF_VECTOR || int_vec == BP_VECTOR)))
 		skip_emulated_instruction(&svm->vcpu);
 
-	return kvm_task_switch(&svm->vcpu, tss_selector, reason);
+	if (kvm_task_switch(&svm->vcpu, tss_selector, reason) == EMULATE_FAIL) {
+		svm->vcpu.run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
+		svm->vcpu.run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
+		svm->vcpu.run->internal.ndata = 0;
+		return 0;
+	}
+	return 1;
 }
 
 static int cpuid_interception(struct vcpu_svm *svm)
arch/x86/kvm/vmx.c +5 -1
index 453f080..9517cbc 100644
@@ -3306,8 +3306,12 @@ static int handle_task_switch(struct kvm_vcpu *vcpu)
 		       type != INTR_TYPE_NMI_INTR))
 		skip_emulated_instruction(vcpu);
 
-	if (!kvm_task_switch(vcpu, tss_selector, reason))
+	if (kvm_task_switch(vcpu, tss_selector, reason) == EMULATE_FAIL) {
+		vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
+		vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
+		vcpu->run->internal.ndata = 0;
 		return 0;
+	}
 
 	/* clear all local breakpoint enable flags */
 	vmcs_writel(GUEST_DR7, vmcs_readl(GUEST_DR7) & ~55);

--
Gleb.

#1 lore raw
Quoted 11 lines · Gleb Natapov

If kvm_task_switch() fails code exits to userspace without specifying exit reason, so the previous exit reason is reused by userspace. Fix this by specifying exit reason correctly.

---
Changelog:
v1->v2:
- report emulation error to userspace instead of ignoring it silently.

Yeah, better than logging to dmesg.

Signature · 2 lines
I have a truly marvellous patch that fixes the bug which this
signature is too narrow to contain.
#2 lore raw
In reply to Gleb Natapov
Quoted 10 lines · Gleb Natapov

If kvm_task_switch() fails code exits to userspace without specifying exit reason, so the previous exit reason is reused by userspace. Fix this by specifying exit reason correctly.

---
Changelog:
v1->v2:
- report emulation error to userspace instead of ignoring it silently.

Quoted 4 lines

Should be applied after "KVM: fix emulator_task_switch() return value." since it relies on new return value from kvm_task_switch().

Does not apply cleanly anymore, please regenerate against -next branch, sorry.