mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Paolo Bonzini <pbonzini@redhat.com>
To: linux-kernel@vger.kernel.org, kvm@vger.kernel.org
Cc: Sean Christopherson <seanjc@google.com>,
	stable@vger.kernel.org, Chris Mason <mason@kernel.org>,
	Stefan Teodorescu <fane@google.com>
Subject: [PATCH 6.12.y 02/17] KVM: x86/mmu: Flush TLBs if child SPTE is zapped when splitting a huge SPTE
Date: Fri,  4 Dec 2026 19:02:30 +0100	[thread overview]
Message-ID: <20261204180245.2166967-3-pbonzini@redhat.com> (raw)
In-Reply-To: <20261204180245.2166967-1-pbonzini@redhat.com>

From: Sean Christopherson <seanjc@google.com>

[ Upstream commit d31322f7c0003c4b3cba9d44c85fd73aa877c9f9 ]

Queue a TLB flush when eagerly splitting a huge SPTE and adding a child
SPTE to the rmap fails due to hitting the recycle threshold.  If adding to
the rmap fails, __rmap_add() zaps the entire list of rmaps, including the
recently-added child SPTE.  Eliding the flush in that case is unsafe as the
huge SPTE is still PRESENT, after the TLB flush  performed by __rmap_add(),
i.e. can be (re)cached by running vCPUs.

Push the TLB flush out from __rmap_add() to its two callers by returning a
bool to indicate that a TLB flush is needed.  Return "TLB flush needed"
instead of success/failuire so that a feature cleanup can simply return the
result of kvm_zap_all_rmap_sptes() (which *should* always be true; if there
isn't at least one rmap entry, then things have really gone sideways).

Fixes: 0378739401cf ("KVM: x86/mmu: Avoid unnecessary flush on eager page split")
Cc: stable@vger.kernel.org
Reported-by: Chris Mason <mason@kernel.org>
Reported-by: Stefan Teodorescu <fane@google.com>
Signed-off-by: Sean Christopherson <seanjc@google.com>
Link: https://patch.msgid.link/20261113155010.442816-2-seanjc@google.com
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
---
 arch/x86/kvm/mmu/mmu.c | 21 ++++++++++++++++-----
 1 file changed, 16 insertions(+), 5 deletions(-)

diff --git a/arch/x86/kvm/mmu/mmu.c b/arch/x86/kvm/mmu/mmu.c
index c4f33dc6dabc..0c2bbb9bd301 100644
--- a/arch/x86/kvm/mmu/mmu.c
+++ b/arch/x86/kvm/mmu/mmu.c
@@ -1573,7 +1573,7 @@ bool kvm_unmap_gfn_range(struct kvm *kvm, struct kvm_gfn_range *range)
 
 #define RMAP_RECYCLE_THRESHOLD 1000
 
-static void __rmap_add(struct kvm *kvm,
+static bool __rmap_add(struct kvm *kvm,
 		       struct kvm_mmu_memory_cache *cache,
 		       const struct kvm_memory_slot *slot,
 		       u64 *spte, gfn_t gfn, unsigned int access)
@@ -1593,8 +1593,9 @@ static void __rmap_add(struct kvm *kvm,
 		kvm->stat.max_mmu_rmap_size = rmap_count;
 	if (rmap_count > RMAP_RECYCLE_THRESHOLD) {
 		kvm_zap_all_rmap_sptes(kvm, rmap_head);
-		kvm_flush_remote_tlbs_gfn(kvm, gfn, sp->role.level);
+		return true;
 	}
+	return false;
 }
 
 static void rmap_add(struct kvm_vcpu *vcpu, const struct kvm_memory_slot *slot,
@@ -1602,7 +1603,9 @@ static void rmap_add(struct kvm_vcpu *vcpu, const struct kvm_memory_slot *slot,
 {
 	struct kvm_mmu_memory_cache *cache = &vcpu->arch.mmu_pte_list_desc_cache;
 
-	__rmap_add(vcpu->kvm, cache, slot, spte, gfn, access);
+	if (__rmap_add(vcpu->kvm, cache, slot, spte, gfn, access))
+		kvm_flush_remote_tlbs_gfn(vcpu->kvm, gfn,
+					  sptep_to_sp(spte)->role.level);
 }
 
 static bool kvm_rmap_age_gfn_range(struct kvm *kvm,
@@ -6828,7 +6831,7 @@ static void shadow_mmu_split_huge_page(struct kvm *kvm,
 		 *
 		 * However, if a given SPTE points to a lower level page table,
 		 * that lower level page table may only be partially populated.
-		 * Installing such SPTEs would effectively unmap a potion of the
+		 * Installing such SPTEs would effectively unmap a portion of the
 		 * huge page. Unmapping guest memory always requires a TLB flush
 		 * since a subsequent operation on the unmapped regions would
 		 * fail to detect the need to flush.
@@ -6840,7 +6843,15 @@ static void shadow_mmu_split_huge_page(struct kvm *kvm,
 
 		spte = make_huge_page_split_spte(kvm, huge_spte, sp->role, index);
 		mmu_spte_set(sptep, spte);
-		__rmap_add(kvm, cache, slot, sptep, gfn, sp->role.access);
+
+		/*
+		 * Queue a TLB flush if adding the SPTE to the rmap zapped the
+		 * child SPTE that was just added.  In that case, replacing the
+		 * huge SPTE with a shadow page will effectively unmap guest
+		 * memory (see above), as the huge SPTE is still PRESENT and
+		 * can be (re)cached in the TLB before it is replaced.
+		 */
+		flush |= __rmap_add(kvm, cache, slot, sptep, gfn, sp->role.access);
 	}
 
 	__link_shadow_page(kvm, cache, huge_sptep, sp, flush);
-- 
2.56.1



  parent reply	other threads:[~2026-12-04 18:02 UTC|newest]

Thread overview: 18+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-04 18:02 [PATCH 6.12.y 00/17] Fixes for various (mostly x86) KVM vulnerabilities Paolo Bonzini
2026-12-04 18:02 ` [PATCH 6.12.y 01/17] KVM: x86: Grab lapic_timer in a local variable to cleanup periodic code Paolo Bonzini
2026-12-04 18:02 ` Paolo Bonzini [this message]
2026-12-04 18:02 ` [PATCH 6.12.y 03/17] KVM: x86/mmu: Propagate the return from kvm_zap_all_rmap_sptes() out of __rmap_add() Paolo Bonzini
2026-12-04 18:02 ` [PATCH 6.12.y 04/17] KVM: x86: Disable hypervisor (VMX Preemption) timer if TSC deadline wraps Paolo Bonzini
2026-12-04 18:02 ` [PATCH 6.12.y 05/17] KVM: x86: Reset local APIC timer metadata on RESET, INIT, and KVM_SET_LAPIC Paolo Bonzini
2026-12-04 18:02 ` [PATCH 6.12.y 06/17] LoongArch: KVM: Flush TLBs *before* dropping mmu_lock Paolo Bonzini
2026-12-04 18:02 ` [PATCH 6.12.y 07/17] KVM: x86/mmu: Flush all TLB entries, not a precise range, when yielding Paolo Bonzini
2026-12-04 18:02 ` [PATCH 6.12.y 08/17] KVM: x86/mmu: Flush TLBs in kvm_unmap_gfn_range() if the unmap may block/yield Paolo Bonzini
2026-12-04 18:02 ` [PATCH 6.12.y 09/17] KVM: x86/xen: Pin kvm->mm prior to borrowing mm when setting event channel Paolo Bonzini
2026-12-04 18:02 ` [PATCH 6.12.y 10/17] KVM: Fold kvm_flush_shadow_all() into kvm_mmu_notifier_release() Paolo Bonzini
2026-12-04 18:02 ` [PATCH 6.12.y 11/17] KVM: WARN if there are in-flight memslot updates when a VM's process exits Paolo Bonzini
2026-12-04 18:02 ` [PATCH 6.12.y 12/17] KVM: Set mn_active_invalidate_count when the VM's mm is released Paolo Bonzini
2026-12-04 18:02 ` [PATCH 6.12.y 13/17] KVM: Invalidate all GFN=>PFN caches when VM and/or its process is dying Paolo Bonzini
2026-12-04 18:02 ` [PATCH 6.12.y 14/17] x86/fpu: Bail early from guest XFD update if XFD isn't supported Paolo Bonzini
2026-12-04 18:02 ` [PATCH 6.12.y 15/17] x86/fpu: Clear XSTATE_BV[i] in guest XSAVE state if guest FPU isn't in-use Paolo Bonzini
2026-12-04 18:02 ` [PATCH 6.12.y 16/17] x86/fpu: Ensure guest FPU is resident in hardware when updating actual XFD MSR Paolo Bonzini
2026-12-04 18:02 ` [PATCH 6.12.y 17/17] KVM: VMX: Ensure pCPU is valid before sending IPI to deliver posted IRQ Paolo Bonzini

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=20261204180245.2166967-3-pbonzini@redhat.com \
    --to=pbonzini@redhat.com \
    --cc=fane@google.com \
    --cc=kvm@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mason@kernel.org \
    --cc=seanjc@google.com \
    --cc=stable@vger.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

all inboxes | Powered by JetHome®