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, Kevin Liu <kevin.liu@openai.com>,
	Chris Mason <mason@kernel.org>, Kyle Zeng <kylebot@openai.com>
Subject: [PATCH 08/17] KVM: x86/mmu: Flush TLBs in kvm_unmap_gfn_range() if the unmap may block/yield
Date: Fri,  4 Dec 2026 18:59:21 +0100	[thread overview]
Message-ID: <20261204175930.2154542-9-pbonzini@redhat.com> (raw)
In-Reply-To: <20261204175930.2154542-1-pbonzini@redhat.com>

From: Sean Christopherson <seanjc@google.com>

Immediately flush TLBs if a flush is pending after unmapping a GFN range,
*before* returning to the caller, if the overall unmap operation may block,
i.e. if a *future* call to kvm_unmap_gfn_range() may yield and drop
mmu_lock.  Because kvm_unmap_gfn_range() starts with "flush = false", KVM
incorrectly loses track of the pending flush if an overall unmap operation
straddles multiple memslots.  KVM will still service the TLB flush before
the *caller* completes, but KVM relies on its mappings being completely
unreachable prior to dropping mmu_lock.

E.g. if a MMU notifier invalidation event runs after mmu_lock is dropped,
but before TLBs are flushed, then the MMU notifier task can observe the
missing PTE and return without issuing its own TLB flush, resulting in a
use-after-free (if the to-be-invalidated page was freed or moved).

Hack a fix into x86, but leave a TODO behind to undo the hack, or at least
make it less awful.  By dumb luck, x86 is the only arch that conditionally
yields *and* utilizes the common TLB flushing mechanism.

Fixes: e1eed5847b09 ("KVM: x86/mmu: Allow yielding during MMU notifier unmap/zap, if possible")
Cc: stable@vger.kernel.org
Reported-by: Kevin Liu <kevin.liu@openai.com>
Reported-by: Chris Mason <mason@kernel.org>
Cc: Kyle Zeng <kylebot@openai.com>
Signed-off-by: Sean Christopherson <seanjc@google.com>
Link: https://patch.msgid.link/20261119172647.64295-5-seanjc@google.com
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
---
 arch/x86/kvm/mmu/mmu.c | 15 +++++++++++++++
 1 file changed, 15 insertions(+)

diff --git a/arch/x86/kvm/mmu/mmu.c b/arch/x86/kvm/mmu/mmu.c
index a891b45c6a56..842945fb18d2 100644
--- a/arch/x86/kvm/mmu/mmu.c
+++ b/arch/x86/kvm/mmu/mmu.c
@@ -1735,6 +1735,21 @@ bool kvm_unmap_gfn_range(struct kvm *kvm, struct kvm_gfn_range *range)
 	    range->slot->id == APIC_ACCESS_PAGE_PRIVATE_MEMSLOT)
 		kvm_make_all_cpus_request(kvm, KVM_REQ_APIC_PAGE_RELOAD);
 
+	/*
+	 * Immediately flush TLBs if a *future* invocation of the current unmap
+	 * operation may block, in which case the pending flush will be dropped
+	 * due to starting with "flush = false" above.
+	 *
+	 * TODO: Plumb @flush around common KVM so that pending flushes can be
+	 *       processed if and only if KVM actually yields, and so that this
+	 *       code doesn't rely on may_block being constant for the entirety
+	 *       of an unmap operation.
+	 */
+	if (flush && range->may_block) {
+		kvm_flush_remote_tlbs(kvm);
+		return false;
+	}
+
 	return flush;
 }
 
-- 
2.56.1



  parent reply	other threads:[~2026-12-04 17:59 UTC|newest]

Thread overview: 18+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-04 17:59 [PATCH 00/17] Fixes for various (mostly x86) KVM vulnerabilities Paolo Bonzini
2026-12-04 17:59 ` [PATCH 01/17] KVM: x86/mmu: Flush TLBs if child SPTE is zapped when splitting a huge SPTE Paolo Bonzini
2026-12-04 17:59 ` [PATCH 02/17] KVM: x86/mmu: Propagate the return from kvm_zap_all_rmap_sptes() out of __rmap_add() Paolo Bonzini
2026-12-04 17:59 ` [PATCH 03/17] KVM: x86: Disable hypervisor (VMX Preemption) timer if TSC deadline wraps Paolo Bonzini
2026-12-04 17:59 ` [PATCH 04/17] KVM: x86: Reset local APIC timer metadata on RESET, INIT, and KVM_SET_LAPIC Paolo Bonzini
2026-12-04 17:59 ` [PATCH 05/17] RISC-V: KVM: Flush TLBs *before* dropping mmu_lock, e.g. when yielding Paolo Bonzini
2026-12-04 17:59 ` [PATCH 06/17] LoongArch: KVM: Flush TLBs *before* dropping mmu_lock Paolo Bonzini
2026-12-04 17:59 ` [PATCH 07/17] KVM: x86/mmu: Flush all TLB entries, not a precise range, when yielding Paolo Bonzini
2026-12-04 17:59 ` Paolo Bonzini [this message]
2026-12-04 17:59 ` [PATCH 09/17] KVM: x86/xen: Pin kvm->mm prior to borrowing mm when setting event channel Paolo Bonzini
2026-12-04 17:59 ` [PATCH 10/17] KVM: Fold kvm_flush_shadow_all() into kvm_mmu_notifier_release() Paolo Bonzini
2026-12-04 17:59 ` [PATCH 11/17] KVM: WARN if there are in-flight memslot updates when a VM's process exits Paolo Bonzini
2026-12-04 17:59 ` [PATCH 12/17] KVM: Set mn_active_invalidate_count when the VM's mm is released Paolo Bonzini
2026-12-04 17:59 ` [PATCH 13/17] KVM: Invalidate all GFN=>PFN caches when VM and/or its process is dying Paolo Bonzini
2026-12-04 17:59 ` [PATCH 14/17] x86/fpu: Bail early from guest XFD update if XFD isn't supported Paolo Bonzini
2026-12-04 17:59 ` [PATCH 15/17] x86/fpu: Clear XSTATE_BV[i] in guest XSAVE state if guest FPU isn't in-use Paolo Bonzini
2026-12-04 17:59 ` [PATCH 16/17] x86/fpu: Ensure guest FPU is resident in hardware when updating actual XFD MSR Paolo Bonzini
2026-12-04 17:59 ` [PATCH 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=20261204175930.2154542-9-pbonzini@redhat.com \
    --to=pbonzini@redhat.com \
    --cc=kevin.liu@openai.com \
    --cc=kvm@vger.kernel.org \
    --cc=kylebot@openai.com \
    --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®