Skip to content

Commit 6e640bb

Browse files
committed
KVM: SVM: Handle #MCs in guest outside of fastpath
Handle Machine Checks (#MC) that happen in the guest (by forwarding them to the host) outside of KVM's fastpath so that as much host state as possible is re-loaded before invoking the kernel's #MC handler. The only requirement is that KVM invokes the #MC handler before enabling IRQs (and even that could _probably_ be relaxed to handling #MCs before enabling preemption). Waiting to handle #MCs until "more" host state is loaded hardens KVM against flaws in the #MC handler, which has historically been quite brittle. E.g. prior to commit 5567d11 ("x86/mce: Send #MC singal from task work"), the #MC code could trigger a schedule() with IRQs and preemption disabled. That led to a KVM hack-a-fix in commit 1811d97 ("x86/kvm: move kvm_load/put_guest_xcr0 into atomic context"). Note, except for #MCs on VM-Enter, VMX already handles #MCs outside of the fastpath. Reviewed-by: Rick Edgecombe <rick.p.edgecombe@intel.com> Reviewed-By: Jon Kohler <jon@nutanix.com> Link: https://patch.msgid.link/20251030224246.3456492-2-seanjc@google.com Signed-off-by: Sean Christopherson <seanjc@google.com>
1 parent 995d504 commit 6e640bb

File tree

1 file changed

+9
-9
lines changed

1 file changed

+9
-9
lines changed

arch/x86/kvm/svm/svm.c

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4325,14 +4325,6 @@ static __no_kcsan fastpath_t svm_vcpu_run(struct kvm_vcpu *vcpu, u64 run_flags)
43254325

43264326
vcpu->arch.regs_avail &= ~SVM_REGS_LAZY_LOAD_SET;
43274327

4328-
/*
4329-
* We need to handle MC intercepts here before the vcpu has a chance to
4330-
* change the physical cpu
4331-
*/
4332-
if (unlikely(svm->vmcb->control.exit_code ==
4333-
SVM_EXIT_EXCP_BASE + MC_VECTOR))
4334-
svm_handle_mce(vcpu);
4335-
43364328
trace_kvm_exit(vcpu, KVM_ISA_SVM);
43374329

43384330
svm_complete_interrupts(vcpu);
@@ -4621,8 +4613,16 @@ static int svm_check_intercept(struct kvm_vcpu *vcpu,
46214613

46224614
static void svm_handle_exit_irqoff(struct kvm_vcpu *vcpu)
46234615
{
4624-
if (to_svm(vcpu)->vmcb->control.exit_code == SVM_EXIT_INTR)
4616+
switch (to_svm(vcpu)->vmcb->control.exit_code) {
4617+
case SVM_EXIT_EXCP_BASE + MC_VECTOR:
4618+
svm_handle_mce(vcpu);
4619+
break;
4620+
case SVM_EXIT_INTR:
46254621
vcpu->arch.at_instruction_boundary = true;
4622+
break;
4623+
default:
4624+
break;
4625+
}
46264626
}
46274627

46284628
static void svm_setup_mce(struct kvm_vcpu *vcpu)

0 commit comments

Comments
 (0)