[PATCH 6.1.y 20/24] KVM: Invalidate all GFN=>PFN caches when VM and/or its process is dying

From: Paolo Bonzini

Date: Fri Dec 04 2026 - 13:10:57 EST


From: Sean Christopherson <seanjc@xxxxxxxxxx>

[ 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@xxxxxxxxxxxxxxx
Reported-by: Amirmohammad Eftekhar <amirmohammad.eftekhar@xxxxxxxx>
Closes: https://lore.kernel.org/all/20261017193911.40182-1-amirmohammad.eftekhar@xxxxxxxx
Suggested-by: Amirmohammad Eftekhar <amirmohammad.eftekhar@xxxxxxxx>
Reported-by: Chris Mason <mason@xxxxxxxxxx>
Suggested-by: Chris Mason <mason@xxxxxxxxxx>
Signed-off-by: Sean Christopherson <seanjc@xxxxxxxxxx>
Link: https://patch.msgid.link/20261120195742.55768-6-seanjc@xxxxxxxxxx
Signed-off-by: Paolo Bonzini <pbonzini@xxxxxxxxxx>
Signed-off-by: Paolo Bonzini <pbonzini@xxxxxxxxxx>
---
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 bb46945cc0e4..8c9163ab16ee 100644
--- a/virt/kvm/kvm_main.c
+++ b/virt/kvm/kvm_main.c
@@ -914,6 +914,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 41da467d99c9..3b21b82c1323 100644
--- a/virt/kvm/kvm_mm.h
+++ b/virt/kvm/kvm_mm.h
@@ -32,6 +32,7 @@ void gfn_to_pfn_cache_invalidate_start(struct kvm *kvm,
unsigned long start,
unsigned long end,
bool may_block);
+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,
@@ -39,6 +40,7 @@ static inline void gfn_to_pfn_cache_invalidate_start(struct kvm *kvm,
bool may_block)
{
}
+static inline void gfn_to_pfn_cache_invalidate_all(struct kvm *kvm) { }
#endif /* HAVE_KVM_PFNCACHE */

#endif /* __KVM_MM_H__ */
diff --git a/virt/kvm/pfncache.c b/virt/kvm/pfncache.c
index 7c248193ca26..679138404b0f 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