OpenVZ / Virtuozzo kernel development (devel@openvz.org)
 help / color / mirror / Atom feed
From: Konstantin Khorenko <khorenko@virtuozzo.com>
To: Pavel Tikhomirov <ptikhomirov@virtuozzo.com>
Cc: devel@openvz.org
Subject: [Devel] [PATCH hci-8.1 05/14] ms/KVM: x86: Unconditionally reset MMU context when emulating INIT
Date: Thu, 26 Nov 2026 15:30:18 +0100	[thread overview]
Message-ID: <20261126-khorenko-vstor-157133-klp-and-kvm-fixes-v1-5-0ec8eaadf1bc@virtuozzo.com> (raw)
In-Reply-To: <20261126-khorenko-vstor-157133-klp-and-kvm-fixes-v1-0-0ec8eaadf1bc@virtuozzo.com>

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>
(cherry picked from commit f55a3e4e08dc004b4bab7c1e9323d7e7e96e266a)

https://virtuozzo.atlassian.net/browse/VSTOR-157389
Feature: fix ms/kvm
Signed-off-by: Konstantin Khorenko <khorenko@virtuozzo.com>
---
 arch/x86/kvm/x86.c | 25 +++++++++++++++----------
 1 file changed, 15 insertions(+), 10 deletions(-)

diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c
index e0f575bc67f47..a825a48093ae1 100644
--- a/arch/x86/kvm/x86.c
+++ b/arch/x86/kvm/x86.c
@@ -12596,18 +12596,23 @@ void kvm_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event)
 	kvm_x86_call(set_efer)(vcpu, 0);
 	kvm_x86_call(update_exception_bitmap)(vcpu);
 
+	if (old_cr0 & X86_CR0_PG)
+		kvm_make_request(KVM_REQ_TLB_FLUSH_GUEST, vcpu);
+
 	/*
-	 * On the standard CR0/CR4/EFER modification paths, there are several
-	 * complex conditions determining whether the MMU has to be reset and/or
-	 * which PCIDs have to be flushed.  However, CR0.WP and the paging-related
-	 * bits in CR4 and EFER are irrelevant if CR0.PG was '0'; and a reset+flush
-	 * is needed anyway if CR0.PG was '1' (which can only happen for INIT, as
-	 * CR0 will be '0' prior to RESET).  So we only need to check CR0.PG here.
+	 * 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_make_request(KVM_REQ_TLB_FLUSH_GUEST, vcpu);
-		kvm_mmu_reset_context(vcpu);
-	}
+	kvm_mmu_reset_context(vcpu);
 
 	/*
 	 * Intel's SDM states that all TLB entries are flushed on INIT.  AMD's

-- 
2.43.0

_______________________________________________
Devel mailing list
Devel@openvz.org
https://lists.openvz.org/mailman/listinfo/devel

  parent reply	other threads:[~2026-11-26 14:31 UTC|newest]

Thread overview: 30+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-26 14:30 [Devel] [PATCH hci-8.1 00/14] objtool/klp and KVM fixes for the 8.1 kernel Konstantin Khorenko
2026-11-26 14:30 ` [Devel] [PATCH hci-8.1 01/14] ms/KVM: x86/mmu: Conditionally resched when retrying gPTE walk on A/D update failure Konstantin Khorenko
2026-12-01  0:34   ` no-reply
2026-11-26 14:30 ` [Devel] [PATCH hci-8.1 02/14] ms/KVM: x86: Conditionally resched when restarting REP string emulation Konstantin Khorenko
2026-12-01  0:34   ` no-reply
2026-11-26 14:30 ` [Devel] [PATCH hci-8.1 03/14] ms/KVM: nVMX: Process each host MSR entry at most once when unwinding failed VM-Entry Konstantin Khorenko
2026-12-01  0:35   ` no-reply
2026-11-26 14:30 ` [Devel] [PATCH hci-8.1 04/14] ms/KVM: x86/mmu: Cap number of parent PTEs per SP at 1000 (the arbitrary rmap limit) Konstantin Khorenko
2026-12-01  0:36   ` no-reply
2026-11-26 14:30 ` Konstantin Khorenko [this message]
2026-12-01  0:37   ` [Devel] [PATCH hci-8.1 05/14] ms/KVM: x86: Unconditionally reset MMU context when emulating INIT no-reply
2026-11-26 14:30 ` [Devel] [PATCH hci-8.1 06/14] ms/KVM: SVM: Triple fault L1 on unintercepted EFER.SVME clear by L2 Konstantin Khorenko
2026-12-01  0:38   ` no-reply
2026-11-26 14:30 ` [Devel] [PATCH hci-8.1 07/14] ms/KVM: selftests: Add a test for L2 clearing EFER.SVME without intercept Konstantin Khorenko
2026-12-01  0:38   ` no-reply
2026-11-26 14:30 ` [Devel] [PATCH hci-8.1 08/14] ms/objtool: Fix data alignment in elf_add_data() Konstantin Khorenko
2026-12-01  0:39   ` no-reply
2026-11-26 14:30 ` [Devel] [PATCH hci-8.1 09/14] ms/objtool/klp: fix data alignment in __clone_symbol() Konstantin Khorenko
2026-12-01  0:39   ` no-reply
2026-11-26 14:30 ` [Devel] [PATCH hci-8.1 10/14] ms/objtool/klp: Fix module name normalization for paths with dots Konstantin Khorenko
2026-12-01  0:40   ` no-reply
2026-11-26 14:30 ` [Devel] [PATCH hci-8.1 11/14] ms/objtool/klp: Normalize Module.symvers paths to module names Konstantin Khorenko
2026-12-01  0:40   ` no-reply
2026-11-26 14:30 ` [Devel] [PATCH hci-8.1 12/14] ms/objtool/klp: Fix cross-module klp relocation section naming Konstantin Khorenko
2026-12-01  0:41   ` no-reply
2026-11-26 14:30 ` [Devel] [PATCH hci-8.1 13/14] ms/objtool/klp: Fix detection of corrupt static branch/call entries Konstantin Khorenko
2026-12-01  0:43   ` no-reply
2026-11-26 14:30 ` [Devel] [PATCH hci-8.1 14/14] ms/objtool/klp: Don't match local symbols against exports Konstantin Khorenko
2026-12-01  0:43   ` no-reply
2026-11-26 14:46 ` [Devel] [PATCH hci-8.1 00/14] objtool/klp and KVM fixes for the 8.1 kernel Konstantin Khorenko

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=20261126-khorenko-vstor-157133-klp-and-kvm-fixes-v1-5-0ec8eaadf1bc@virtuozzo.com \
    --to=khorenko@virtuozzo.com \
    --cc=devel@openvz.org \
    --cc=ptikhomirov@virtuozzo.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