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,
	Amirmohammad Eftekhar <amirmohammad.eftekhar@cispa.de>,
	Chris Mason <mason@kernel.org>
Subject: [PATCH 6.12.y 13/17] KVM: Invalidate all GFN=>PFN caches when VM and/or its process is dying
Date: Fri,  4 Dec 2026 19:02:41 +0100	[thread overview]
Message-ID: <20261204180245.2166967-14-pbonzini@redhat.com> (raw)
In-Reply-To: <20261204180245.2166967-1-pbonzini@redhat.com>

From: Sean Christopherson <seanjc@google.com>

[ Upstream commit c4424cbaaf04f3d3113d838ef836529a533b390a ]

Invalidate all GPCs in kvm_flush_shadow_all(), i.e. when a VM or its owning
process is dying, to ensure a GPC can't be used after the associated mm has
exited, e.g. via a timer callback.  Take care to invalidate the caches
before notifying arch code that memory has been reclaimed, as all in-flight
accesses must complete before KVM SEV performs cache maintenance on the
affected memory.

  BUG: KASAN: use-after-free in kvm_xen_set_evtchn_fast+0x3fe/0xfd0 [kvm]
  Write of size 8 at addr ffff88852702c800 by task swapper/4/0
  CPU: 4 UID: 0 PID: 0 Comm: swapper/4 Not tainted 7.3.0-rc3-master-g587858367581-dirty-kasan #1 PREEMPT(lazy)
  Hardware name: LENOVO 21HMCTO1WW/21HMCTO1WW, BIOS N3XET48W (1.23 ) 11/01/2023
  Call Trace:
   <IRQ>
   dump_stack_lvl+0x91/0xf0
   print_report+0xd1/0x630
   kasan_report+0xf3/0x130
   kasan_check_range+0x11c/0x200
   __kasan_check_write+0x14/0x30
   kvm_xen_set_evtchn_fast+0x3fe/0xfd0 [kvm]
   xen_timer_callback+0x1eb/0x2b0 [kvm]
   __hrtimer_run_queues+0x43e/0x8b0
   hrtimer_interrupt+0x3aa/0xa00
   __sysvec_apic_timer_interrupt+0xc1/0x3a0
   sysvec_apic_timer_interrupt+0x8d/0xd0
   </IRQ>
   <TASK>
   asm_sysvec_apic_timer_interrupt+0x1b/0x20
  The buggy address belongs to the physical page:
  page: refcount:0 mapcount:0 mapping:0000000000000000 index:0x7965608db pfn:0x52702c
  flags: 0x17ffffc0000000(node=0|zone=2|lastcpupid=0x1fffff)
  raw: 0017ffffc0000000 ffffea0014864108 ffffea001c118e48 0000000000000000
  raw: 00000007965608db 0000000000000000 00000000ffffffff 0000000000000000
  page dumped because: kasan: bad access detected

Alternatively, invalidate_all() could reuse invalidate_start(), but that'd
require tweaking invalidate_start() to special case "all", as simply
in 0=>ULONG_MAX is subtly misleading/wrong.  Because invalidate_start()
takes an exclusive @end, using ULONG_MAX wouldn't strictly guarantee that
all GPCs are invalidated.  That's somewhat of a moot point since GPCs only
ever deal with user addresses and CONFIG_HAVE_KVM_PFNCACHE is only selected
by x86, which guarantees ULONG_MAX is a kernel address.  But getting the
logic "right" the first time is easy enough.

Reusing invalidate_start() would also be suboptimal, as it requires taking
the per-GPC lock twice.  That's extremely unlikely to matter in practice,
but again, open coding invalidate_all() isn't complex, and avoids making
invalidate_start() itself (slightly) more complex.

Fixes: 982ed0de4753 ("KVM: Reinstate gfn_to_pfn_cache with invalidation support")
Cc: stable@vger.kernel.org
Reported-by: Amirmohammad Eftekhar <amirmohammad.eftekhar@cispa.de>
Closes: https://lore.kernel.org/all/20261017193911.40182-1-amirmohammad.eftekhar@cispa.de
Suggested-by: Amirmohammad Eftekhar <amirmohammad.eftekhar@cispa.de>
Reported-by: Chris Mason <mason@kernel.org>
Suggested-by: Chris Mason <mason@kernel.org>
Signed-off-by: Sean Christopherson <seanjc@google.com>
Link: https://patch.msgid.link/20261120195742.55768-6-seanjc@google.com
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
---
 virt/kvm/kvm_main.c |  1 +
 virt/kvm/kvm_mm.h   |  2 ++
 virt/kvm/pfncache.c | 12 ++++++++++++
 3 files changed, 15 insertions(+)

diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c
index 29c723a06f68..e0d82b339006 100644
--- a/virt/kvm/kvm_main.c
+++ b/virt/kvm/kvm_main.c
@@ -931,6 +931,7 @@ static void kvm_mmu_notifier_release(struct mmu_notifier *mn,
 
 	idx = srcu_read_lock(&kvm->srcu);
 	kvm_arch_flush_shadow_all(kvm);
+	gfn_to_pfn_cache_invalidate_all(kvm);
 	kvm_arch_guest_memory_reclaimed(kvm);
 	srcu_read_unlock(&kvm->srcu, idx);
 }
diff --git a/virt/kvm/kvm_mm.h b/virt/kvm/kvm_mm.h
index 92ec2bde6532..b21af4254e96 100644
--- a/virt/kvm/kvm_mm.h
+++ b/virt/kvm/kvm_mm.h
@@ -30,12 +30,14 @@ kvm_pfn_t hva_to_pfn(unsigned long addr, bool atomic, bool interruptible,
 void gfn_to_pfn_cache_invalidate_start(struct kvm *kvm,
 				       unsigned long start,
 				       unsigned long end);
+void gfn_to_pfn_cache_invalidate_all(struct kvm *kvm);
 #else
 static inline void gfn_to_pfn_cache_invalidate_start(struct kvm *kvm,
 						     unsigned long start,
 						     unsigned long end)
 {
 }
+static inline void gfn_to_pfn_cache_invalidate_all(struct kvm *kvm) { }
 #endif /* HAVE_KVM_PFNCACHE */
 
 #ifdef CONFIG_KVM_PRIVATE_MEM
diff --git a/virt/kvm/pfncache.c b/virt/kvm/pfncache.c
index f0039efb9e1e..cc3f6edf5c8a 100644
--- a/virt/kvm/pfncache.c
+++ b/virt/kvm/pfncache.c
@@ -19,6 +19,18 @@
 
 #include "kvm_mm.h"
 
+void gfn_to_pfn_cache_invalidate_all(struct kvm *kvm)
+{
+	struct gfn_to_pfn_cache *gpc;
+
+	guard(spinlock)(&kvm->gpc_lock);
+
+	list_for_each_entry(gpc, &kvm->gpc_list, list) {
+		guard(write_lock_irq)(&gpc->lock);
+		gpc->valid = false;
+	}
+}
+
 /*
  * MMU notifier 'invalidate_range_start' hook.
  */
-- 
2.56.1



  parent reply	other threads:[~2026-12-04 18:03 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 ` [PATCH 6.12.y 02/17] KVM: x86/mmu: Flush TLBs if child SPTE is zapped when splitting a huge SPTE Paolo Bonzini
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 ` Paolo Bonzini [this message]
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-14-pbonzini@redhat.com \
    --to=pbonzini@redhat.com \
    --cc=amirmohammad.eftekhar@cispa.de \
    --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®