Linux kernel -stable discussions
 help / color / mirror / Atom feed
From: Sean Christopherson <seanjc@google.com>
To: Sasha Levin <sashal@kernel.org>
Cc: stable@vger.kernel.org, Stefan Teodorescu <fane@google.com>,
	 Oren Yomtov <thenameisoren@gmail.com>,
	Chris Mason <mason@kernel.org>,  Yosry Ahmed <yosry@kernel.org>,
	Paolo Bonzini <pbonzini@redhat.com>
Subject: Re: [PATCH 5.15.y 5/5] KVM: x86: Unconditionally reset MMU context when emulating INIT
Date: Mon, 16 Nov 2026 14:33:33 -0800	[thread overview]
Message-ID: <avuEvfAczcRP8a0i@seanjc-mac.roam.internal> (raw)
In-Reply-To: <20261114041301.2801291-5-sashal@kernel.org>

On Fri, Nov 13, 2026, Sasha Levin wrote:
> From: Sean Christopherson <seanjc@google.com>
> 
> [ Upstream commit c7c288d178cb0b78d3464a616b16b1aa0b316049 ]
> 
> Force an MMU reset when emulating an INIT even if paging was already
> disabled, as the MMU page role includes guest_mode (L2), i.e. an INIT while
> L2 is running with CR0.PG=0 will result in KVM running L1 with a stale MMU
> context.
> 
> Alternatively, the reset could be scoped strictly to paging being enabled
> or L2 being active, but SMM is also encoded in the page role.  Even though
> it _should_ be impossible for the vCPU to be INIT'd while SMM is active,
> there's zero value in not handling SMM.  At that point, the only scenario
> where KVM _doesn't_ need to reset the MMU context is on an INIT with the
> vCPU running in Real Mode or Unpaged Protect Mode, and while INIT in those
> modes is somewhat common, it's far from a hot path and re-initializing the
> MMU is not expensive, especially when the role isn't changing (KVM elides
> most of the re-configuration in that case).
> 
> Note, like the Fixes commit, this bug only affects nested SVM as INIT is an
> unconditional intercept on VMX, i.e. it's impossible to emulate INIT while
> L2 is active.
> 
> Temporarily keep the guest TLB flush when CR0.PG is being cleared even
> though it is fully redundant with the flush that is emitted on all INIT
> events (KVM itself only does RESET when a vCPU is being created, i.e. when
> CR0.PG must be '0').
> 
> Fixes: ed129ec9057f ("KVM: x86: forcibly leave nested mode on vCPU reset")
> Reported-by: Stefan Teodorescu <fane@google.com>
> Reported-by: Oren Yomtov <thenameisoren@gmail.com>
> Reported-by: Chris Mason <mason@kernel.org>
> Cc: stable@vger.kernel.org
> Cc: Yosry Ahmed <yosry@kernel.org>
> Signed-off-by: Sean Christopherson <seanjc@google.com>
> Message-ID: <20261022015407.279870-2-seanjc@google.com>
> Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
> Signed-off-by: Sasha Levin <sashal@kernel.org>
> ---

NAK.  Backporting 4 *optimizations* to resolve a trivial conflict is absurd.

diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c
index bbfc8ccf4fcd9..5165147407e23 100644
--- a/arch/x86/kvm/x86.c
+++ b/arch/x86/kvm/x86.c
@@ -11351,15 +11351,19 @@ void kvm_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event)
 	static_call(kvm_x86_update_exception_bitmap)(vcpu);

 	/*
-	 * Reset the MMU context if paging was enabled prior to INIT (which is
-	 * implied if CR0.PG=1 as CR0 will be '0' prior to RESET).  Unlike the
-	 * standard CR0/CR4/EFER modification paths, only CR0.PG needs to be
-	 * checked because it is unconditionally cleared on INIT and all other
-	 * paging related bits are ignored if paging is disabled, i.e. CR0.WP,
-	 * CR4, and EFER changes are all irrelevant if CR0.PG was '0'.
+	 * Unconditionally reset the MMU, even though super strictly speaking,
+	 * a MMU reset is only necessary if and only one of the page role bits
+	 * is changing.  All of the CR0/CR4/EFER bits effectively get covered
+	 * by checking CR0.PG=1, as CR0.WP and the paging-related bits in CR4
+	 * and EFER are irrelevant if CR0.PG was '0'.  Subtly, if the vCPU was
+	 * in guest mode (L2) or SMM (although emulating INIT in SMM _should_
+	 * be impossible), an MMU reset is also necessary as "guest mode" and
+	 * "smm" are both encoded in the shadow page role.  As a result, the
+	 * only scenarios where an MMU reset is *not* needed is if the vCPU is
+	 * in Real Mode or Unpaged Protected Mode, and micro-optimizing those
+	 * paths are not worth the risk of running with a stale MMU context.
 	 */
-	if (old_cr0 & X86_CR0_PG)
-		kvm_mmu_reset_context(vcpu);
+	kvm_mmu_reset_context(vcpu);

 	/*
 	 * Intel's SDM states that all TLB entries are flushed on INIT.  AMD's

  reply	other threads:[~2026-11-16 22:33 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-10  1:21 FAILED: patch "[PATCH] KVM: x86: Unconditionally reset MMU context when emulating INIT" failed to apply to 5.15-stable tree Sasha Levin
2026-11-14  4:13 ` [PATCH 5.15.y 1/5] KVM: X86: Don't reset mmu context when X86_CR4_PCIDE 1->0 Sasha Levin
2026-11-14  4:13   ` [PATCH 5.15.y 2/5] KVM: X86: Don't reset mmu context when toggling X86_CR4_PGE Sasha Levin
2026-11-14  4:13   ` [PATCH 5.15.y 3/5] KVM: X86: Cache CR3 in prev_roots when PCID is disabled Sasha Levin
2026-11-14  4:13   ` [PATCH 5.15.y 4/5] KVM: x86: flush TLB separately from MMU reset Sasha Levin
2026-11-14  4:13   ` [PATCH 5.15.y 5/5] KVM: x86: Unconditionally reset MMU context when emulating INIT Sasha Levin
2026-11-16 22:33     ` Sean Christopherson [this message]
2026-11-17  1:03       ` Sasha Levin

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=avuEvfAczcRP8a0i@seanjc-mac.roam.internal \
    --to=seanjc@google.com \
    --cc=fane@google.com \
    --cc=mason@kernel.org \
    --cc=pbonzini@redhat.com \
    --cc=sashal@kernel.org \
    --cc=stable@vger.kernel.org \
    --cc=thenameisoren@gmail.com \
    --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