From: Sean Christopherson <seanjc@google.com>
To: Sean Christopherson <seanjc@google.com>,
Paolo Bonzini <pbonzini@redhat.com>
Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: [PATCH 2/4] KVM: nSVM: Synthesize #GP instead of SHUTDOWN if L2 attempts to clear EFER.SVME
Date: Thu, 22 Oct 2026 15:30:50 -0700 [thread overview]
Message-ID: <20261022223052.646891-3-seanjc@google.com> (raw)
In-Reply-To: <20261022223052.646891-1-seanjc@google.com>
If L2 attempts to clear EFER.SVME (because L1 neglected to intercept writes
to EFER), synthesize a #GP instead of a triple-fault SHUTDOWN for L1, as
yanking the vCPU out of L2 and mixing the new (invalid) EFER with L2's
current state is unnecessarily risky for KVM since the TRIPLE_FAULT request
isn't fully handled until KVM attempts to enter the guest, and nothing
prevents userspace from ignoring the SHUTDOWN exit and continue running the
guest.
Apply the restriction to both guest and host (a.k.a. userspace) writes, as
unlike kvm_x86_ops.set_efer() => svm_set_efer(), set_efer() is used *only*
for targeted MSR writes, e.g. not for KVM_SET_SREGS{,2}, and KVM does NOT
advertise EFER as an MSR that needs to be saved (because it's handled via
sregs). I.e. there is far less chance of breaking userspace that manually
restores EFER with SVME=0 while L2 is active, because in all likelihood no
such VMM exists.
Keep the sanity check in svm_set_efer() that handles the KVM_RUN case, but
escalate it to a KVM_BUG_ON() + -EIO, as it should now be unreachable.
Cc: stable@vger.kernel.org
Signed-off-by: Sean Christopherson <seanjc@google.com>
---
arch/x86/kvm/msrs.c | 12 ++++++++++++
arch/x86/kvm/svm/svm.c | 15 +++++----------
2 files changed, 17 insertions(+), 10 deletions(-)
diff --git a/arch/x86/kvm/msrs.c b/arch/x86/kvm/msrs.c
index 2df42276fd82..c3cb1575fe66 100644
--- a/arch/x86/kvm/msrs.c
+++ b/arch/x86/kvm/msrs.c
@@ -626,6 +626,18 @@ static int set_efer(struct kvm_vcpu *vcpu, u64 efer)
if (is_paging(vcpu) && (vcpu->arch.efer & EFER_LME) != (efer & EFER_LME))
return 1;
+ /*
+ * Architecturally (per the APM), clearing EFER.SVME while a guest is
+ * running yields undefined behavior, i.e. KVM can do literally
+ * anything. Synthesize a #GP as that is the safest option for KVM,
+ * as mixing the current vCPU state (from L2) with L1 and the new EFER
+ * (if KVM simply forces the vCPU out of L2) may yield an unexpected
+ * end result.
+ */
+ if (is_guest_mode(vcpu) &&
+ (vcpu->arch.efer & EFER_SVME) && !(efer & EFER_SVME))
+ return 1;
+
efer &= ~EFER_LMA;
efer |= vcpu->arch.efer & EFER_LMA;
diff --git a/arch/x86/kvm/svm/svm.c b/arch/x86/kvm/svm/svm.c
index 487897ef11dd..a6ff9635677a 100644
--- a/arch/x86/kvm/svm/svm.c
+++ b/arch/x86/kvm/svm/svm.c
@@ -227,17 +227,12 @@ int svm_set_efer(struct kvm_vcpu *vcpu, u64 efer)
if ((old_efer & EFER_SVME) != (efer & EFER_SVME)) {
if (!(efer & EFER_SVME)) {
/*
- * Architecturally, clearing EFER.SVME while a guest is
- * running yields undefined behavior, i.e. KVM can do
- * literally anything. Force the vCPU back into L1 as
- * that is the safest option for KVM, but synthesize a
- * triple fault (for L1!) so that KVM at least doesn't
- * run random L2 code in the context of L1. Do so if
- * and only if the vCPU is actively running, e.g. to
- * avoid positives if userspace is stuffing state.
+ * KVM is supposed to synthesize #GP if L2 attempts to
+ * clear SVME.
*/
- if (is_guest_mode(vcpu) && vcpu->wants_to_run)
- kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
+ if (KVM_BUG_ON(is_guest_mode(vcpu) &&
+ vcpu->wants_to_run, vcpu->kvm))
+ return -EIO;
svm_leave_nested(vcpu);
/* #GP intercept is still needed for vmware backdoor */
--
2.56.0.257.gf7497c7d27-goog
next prev parent reply other threads:[~2026-10-22 22:30 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-10-22 22:30 [PATCH 0/4] KVM: x86: More EFER.LMA vs. CR4.PAE fixes Sean Christopherson
2026-10-22 22:30 ` [PATCH 1/4] Revert "KVM: x86: Skip EFER vs. guest CPUID checks for host-initiated writes" Sean Christopherson
2026-10-22 22:30 ` Sean Christopherson [this message]
2026-10-22 22:30 ` [PATCH 3/4] KVM: x86: Recompute EFER.LMA instead of inheriting the current value on EFER writes Sean Christopherson
2026-10-22 22:30 ` [PATCH 4/4] KVM: selftests: Update nSVM EFER test to expect #GP when clearing SVME in L2 Sean Christopherson
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=20261022223052.646891-3-seanjc@google.com \
--to=seanjc@google.com \
--cc=kvm@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=pbonzini@redhat.com \
/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
all inboxes | Powered by JetHome®