From: Sean Christopherson <seanjc@google.com>
To: Yosry Ahmed <yosry@kernel.org>
Cc: Paolo Bonzini <pbonzini@redhat.com>,
kvm@vger.kernel.org, linux-kernel@vger.kernel.org,
stable@vger.kernel.org
Subject: Re: [PATCH v6 16/31] KVM: nSVM: Unify handling of VMRUN failures with proper cleanup
Date: Fri, 27 Feb 2026 16:07:16 -0800 [thread overview]
Message-ID: <aaIxtBYRNCHdEvsV@google.com> (raw)
In-Reply-To: <20260224223405.3270433-17-yosry@kernel.org>
On Tue, Feb 24, 2026, Yosry Ahmed wrote:
> There are currently two possible causes of VMRUN failures emulated by
> KVM:
>
> 1) Consistency checks failures. In this case, KVM updates the exit code
> in the mapped VMCB12 and exits early in nested_svm_vmrun(). This
> causes a few problems:
>
> A) KVM does not clear the GIF if the early consistency checks fail
> (because nested_svm_vmexit() is not called). Nothing requires
> GIF=0 before a VMRUN, from the APM:
>
> It is assumed that VMM software cleared GIF some time before
> executing the VMRUN instruction, to ensure an atomic state
> switch.
>
> So an early #VMEXIT from early consistency checks could leave the
> GIF set.
>
> B) svm_leave_smm() is missing consistency checks on the newly loaded
> guest state, because the checks aren't performed by
> enter_svm_guest_mode().
This is flat out wrong. RSM isn't missing any consistency checks that are
provided by nested_vmcb_check_save().
if (CC(!(save->efer & EFER_SVME))) <=== irrelevant given KVM's implementation
return false;
if (CC((save->cr0 & X86_CR0_CD) == 0 && (save->cr0 & X86_CR0_NW)) || <== kvm_set_cr0() in rsm_enter_protected_mode()
CC(save->cr0 & ~0xffffffffULL))
return false;
if (CC(!kvm_dr6_valid(save->dr6)) || CC(!kvm_dr7_valid(save->dr7))) <== kvm_set_dr() in rsm_load_state_{32,64}
return false;
/*
* These checks are also performed by KVM_SET_SREGS,
* except that EFER.LMA is not checked by SVM against
* CR0.PG && EFER.LME.
*/
if ((save->efer & EFER_LME) && (save->cr0 & X86_CR0_PG)) {
if (CC(!(save->cr4 & X86_CR4_PAE)) || <== kvm_set_cr4() in rsm_enter_protected_mode()
CC(!(save->cr0 & X86_CR0_PE)) || <== kvm_set_cr0() in rsm_enter_protected_mode()
CC(!kvm_vcpu_is_legal_cr3(vcpu, save->cr3))) <== kvm_set_cr3() in rsm_enter_protected_mode()
return false;
}
/* Note, SVM doesn't have any additional restrictions on CR4. */
if (CC(!__kvm_is_valid_cr4(vcpu, save->cr4))) <== kvm_set_cr4() in rsm_enter_protected_mode()
return false;
if (CC(!kvm_valid_efer(vcpu, save->efer))) <== __kvm_emulate_msr_write() in rsm_load_state_64()
return false;
Even if RSM were missing checks on the L2 state being loaded, I'm not willing to
take on _any_ complexity in nested VMRUN to make RSM suck a little less. KVM's
L2 => SMM => RSM => L2 is fundamentally broken. Anyone that argues otherwise is
ignoring architecturally defined behavior in the SDM and APM.
If someone wants to actually put in the effort to properly emulating SMI => RSM
from L2, then I'd be happy to take on some complexity, but even then it's not at
all clear that it would be necessary.
> 2) Failure to load L2's CR3 or merge the MSR bitmaps. In this case, a
> fully-fledged #VMEXIT injection is performed as VMCB02 is already
> prepared.
>
> Arguably all VMRUN failures should be handled before the VMCB02 is
> prepared, but with proper cleanup (e.g. clear the GIF).
Eh, so long as KVM cleans up after itself, I don't see anything wrong with
preparing some of vmcb02.
So after staring at this for some time, us having gone through multiple attempts
to get things right, and this being tagged for stable@, unless I'm missing some
massive simplification this provides down the road, I am strongly against refactoring
this code, and 100% against reworking things to "fix" SMM.
And so for the stable@ patches, I'm also opposed to all of these:
KVM: nSVM: Refactor minimal #VMEXIT handling out of nested_svm_vmexit()
KVM: nSVM: Call nested_svm_init_mmu_context() before switching to VMCB02
KVM: nSVM: Call nested_svm_merge_msrpm() from enter_svm_guest_mode()
KVM: nSVM: Make nested_svm_merge_msrpm() return an errno
KVM: nSVM: Call enter_guest_mode() before switching to VMCB02
KVM: nSVM: Drop nested_vmcb_check_{save/control}() wrappers
unless they're *needed* by some later commit (I didn't look super closely).
For stable@, just fix the GIF case and move on.
diff --git a/arch/x86/kvm/svm/nested.c b/arch/x86/kvm/svm/nested.c
index d734cd5eef5e..d9790e37d4e8 100644
--- a/arch/x86/kvm/svm/nested.c
+++ b/arch/x86/kvm/svm/nested.c
@@ -1036,6 +1036,7 @@ int nested_svm_vmrun(struct kvm_vcpu *vcpu)
vmcb12->control.exit_code = SVM_EXIT_ERR;
vmcb12->control.exit_info_1 = 0;
vmcb12->control.exit_info_2 = 0;
+ svm_set_gif(svm, false);
goto out;
}
Sorry for not catching this earlier, I didn't actually read the changelog until
this version. /facepalm
next prev parent reply other threads:[~2026-02-28 0:07 UTC|newest]
Thread overview: 30+ messages / expand[flat|nested] mbox.gz Atom feed top
[not found] <20260224223405.3270433-1-yosry@kernel.org>
2026-02-24 22:33 ` [PATCH v6 01/31] KVM: nSVM: Avoid clearing VMCB_LBR in vmcb12 Yosry Ahmed
2026-02-24 22:33 ` [PATCH v6 02/31] KVM: SVM: Switch svm_copy_lbrs() to a macro Yosry Ahmed
2026-02-24 22:33 ` [PATCH v6 03/31] KVM: SVM: Add missing save/restore handling of LBR MSRs Yosry Ahmed
2026-03-03 0:02 ` Yosry Ahmed
2026-02-24 22:33 ` [PATCH v6 05/31] KVM: nSVM: Always inject a #GP if mapping VMCB12 fails on nested VMRUN Yosry Ahmed
2026-02-24 22:33 ` [PATCH v6 06/31] KVM: nSVM: Refactor checking LBRV enablement in vmcb12 into a helper Yosry Ahmed
2026-02-24 22:33 ` [PATCH v6 07/31] KVM: nSVM: Refactor writing vmcb12 on nested #VMEXIT as " Yosry Ahmed
2026-02-24 22:33 ` [PATCH v6 08/31] KVM: nSVM: Triple fault if mapping VMCB12 fails on nested #VMEXIT Yosry Ahmed
2026-02-24 22:33 ` [PATCH v6 09/31] KVM: nSVM: Triple fault if restore host CR3 " Yosry Ahmed
2026-02-24 22:33 ` [PATCH v6 10/31] KVM: nSVM: Drop nested_vmcb_check_{save/control}() wrappers Yosry Ahmed
2026-02-24 22:33 ` [PATCH v6 11/31] KVM: nSVM: Call enter_guest_mode() before switching to VMCB02 Yosry Ahmed
2026-02-24 22:33 ` [PATCH v6 12/31] KVM: nSVM: Make nested_svm_merge_msrpm() return an errno Yosry Ahmed
2026-02-24 22:33 ` [PATCH v6 13/31] KVM: nSVM: Call nested_svm_merge_msrpm() from enter_svm_guest_mode() Yosry Ahmed
2026-02-24 22:33 ` [PATCH v6 14/31] KVM: nSVM: Call nested_svm_init_mmu_context() before switching to VMCB02 Yosry Ahmed
2026-02-24 22:33 ` [PATCH v6 15/31] KVM: nSVM: Refactor minimal #VMEXIT handling out of nested_svm_vmexit() Yosry Ahmed
2026-02-24 22:33 ` [PATCH v6 16/31] KVM: nSVM: Unify handling of VMRUN failures with proper cleanup Yosry Ahmed
2026-02-25 22:44 ` Yosry Ahmed
2026-02-28 0:07 ` Sean Christopherson [this message]
2026-02-28 0:46 ` Yosry Ahmed
2026-02-28 1:05 ` Sean Christopherson
2026-03-02 16:02 ` Yosry Ahmed
2026-03-02 16:11 ` Sean Christopherson
2026-02-24 22:33 ` [PATCH v6 17/31] KVM: nSVM: Clear EVENTINJ field in VMCB12 on nested #VMEXIT Yosry Ahmed
2026-02-24 22:33 ` [PATCH v6 18/31] KVM: nSVM: Drop the non-architectural consistency check for NP_ENABLE Yosry Ahmed
2026-02-24 22:33 ` [PATCH v6 19/31] KVM: nSVM: Add missing consistency check for nCR3 validity Yosry Ahmed
2026-02-24 22:33 ` [PATCH v6 20/31] KVM: nSVM: Add missing consistency check for hCR0.PG and NP_ENABLE Yosry Ahmed
2026-03-03 0:00 ` Yosry Ahmed
2026-02-24 22:33 ` [PATCH v6 21/31] KVM: nSVM: Add missing consistency check for EFER, CR0, CR4, and CS Yosry Ahmed
2026-03-03 0:03 ` Yosry Ahmed
2026-02-24 22:33 ` [PATCH v6 22/31] KVM: nSVM: Add missing consistency check for EVENTINJ Yosry Ahmed
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=aaIxtBYRNCHdEvsV@google.com \
--to=seanjc@google.com \
--cc=kvm@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=pbonzini@redhat.com \
--cc=stable@vger.kernel.org \
--cc=yosry@kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox