FAILED: patch "[PATCH] KVM: pfncache: use a dedicated invalidation sequence for" failed to apply to 6.18-stable tree
Subsystems (2)
[email protected]
maintainerThe patch below does not apply to the 6.18-stable tree.
If someone wants it applied there, or to any other stable or longterm tree, then please email the backport, including the original git commit id to <[email protected]>.
To reproduce the conflict and resubmit, you may use the following commands:
git fetch https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git/ linux-6.18.y
git checkout FETCH_HEAD
git cherry-pick -x da260bcb56feeb6489a83f310308453a0a7de25e
# <resolve conflicts, build, test, etc.>
git commit -s
git send-email --to '<[email protected]>' --in-reply-to '2026110445-lividly-gratified-dd5f@gregkh' --subject-prefix 'PATCH 6.18.y' 'HEAD^..'
Possible dependencies:
thanks,
greg k-h
------------------ original commit in Linus's tree ------------------
From da260bcb56feeb6489a83f310308453a0a7de25e Mon Sep 17 00:00:00 2001
From: David Woodhouse <[email protected]>
Date: Mon, 31 Aug 2026 22:26:43 +0100
Subject: [PATCH] KVM: pfncache: use a dedicated invalidation sequence for
cache refresh
The gfn_to_pfn_cache refresh path guards against mmu notifier invalidations which complete while it has dropped gpc->lock for the HVA->PFN lookup: hva_to_pfn_retry() samples kvm->mmu_invalidate_seq and retries if it changed, or if mn_active_invalidate_count is still elevated.
That is insufficient for HVA-based caches. mmu_invalidate_seq is only advanced by kvm_mmu_invalidate_end() when the invalidated range overlaps a memslot, and an HVA-based cache (e.g. the Xen shared_info page mapped with KVM_XEN_ATTR_TYPE_SHARED_INFO_HVA) need not be backed by any memslot at all. An invalidation of the cached HVA which starts and ends entirely within the lookup window is thus invisible to the retry check: mn_active_invalidate_count is back to zero and the sequence never moved. The refresh then publishes a mapping of a page which has already been freed, and the next reader dereferences it:
BUG: KASAN: use-after-free in kvm_xen_shared_info_init+0x3c6/0x440 Read of size 4 at addr ffff8880599c2900 by task syz.2.383/7257
Since gfn_to_pfn_cache_invalidate_start() deliberately skips caches which are not currently valid (including one whose refresh is in progress, as the refresh clears the valid flag before dropping the lock), the retry check is the only line of defence, and it must fire for *any* invalidation, not just those hitting a memslot.
Add a dedicated kvm->gpc_invalidate_seq, incremented by every kvm_mmu_notifier_invalidate_range_end() under mn_invalidate_lock before mn_active_invalidate_count is decremented, and check it in hva_to_pfn_retry() instead of mmu_invalidate_seq. Incrementing in range_end() in the same critical section as the in-progress count also closes the variant where the cache is activated with the contested HVA only after invalidate_range_start() has run.
The same bug is also reachable through the per-vCPU vcpu_info cache (KVM_XEN_VCPU_ATTR_TYPE_VCPU_INFO_HVA), where the stale mapping is then dereferenced by kvm_setup_guest_pvclock() on the next KVM_RUN:
BUG: KASAN: use-after-free in kvm_setup_guest_pvclock+0x5bf/0x660
This intentionally makes refresh retry on *unrelated* mmu notifier events; restoring precision (and reworking the GPC locking more generally) is left for a subsequent series.
Reproducers: https://david.woodhou.se/xen_shinfo_race.c
https://david.woodhou.se/vcpu_info_race.c
include/linux/kvm_host.h +2 -0
@@ -855,6 +855,8 @@ struct kvm {gfn_t mmu_invalidate_range_start;gfn_t mmu_invalidate_range_end; +unsigned long gpc_invalidate_seq; +struct list_head devices;u64 manual_dirty_log_protect;struct dentry *debugfs_dentry;
virt/kvm/kvm_main.c +10 -0
@@ -813,6 +813,16 @@ static void kvm_mmu_notifier_invalidate_range_end(struct mmu_notifier *mn,/* Pairs with the increment in range_start(). */ spin_lock (&kvm->mn_invalidate_lock); + kvm->gpc_invalidate_seq++; + +/* +* As with the MMU sequence counter and mmu_invalidate_in_progress, the +* GPC sequence increase must be visible before the invalidate count +* goes to zero. Pairs with the smp_rmb() in +* mmu_notifier_retry_cache(). +*/ +smp_wmb (); +if (!WARN_ON_ONCE (!kvm->mn_active_invalidate_count)) --kvm->mn_active_invalidate_count; wake = !kvm->mn_active_invalidate_count;
virt/kvm/pfncache.c +9 -9
@@ -124,7 +124,7 @@ static void gpc_unmap(kvm_pfn_t pfn, void *khva)#endif } -static inline bool mmu_notifier_retry_cache (struct kvm *kvm,unsigned long mmu_seq) +static inline bool mmu_notifier_retry_cache (struct kvm *kvm,unsigned long gpc_seq) {/* * mn_active_invalidate_count acts for all intents and purposes
@@ -136,20 +136,20 @@ static inline bool mmu_notifier_retry_cache(struct kvm *kvm, unsigned long mmu_s* Note, it does not matter that mn_active_invalidate_count * is not protected by gpc->lock. It is guaranteed to * be elevated before the mmu_notifier acquires gpc->lock, and -* isn't dropped until after mmu_invalidate_seq is updated. +* isn't dropped until after gpc_invalidate_seq is updated. */ if (kvm->mn_active_invalidate_count)return true;/* * Ensure mn_active_invalidate_count is read before -* mmu_invalidate_seq. This pairs with the smp_wmb() in -* mmu_notifier_invalidate_range_end() to guarantee either the +* gpc_invalidate_seq. This pairs with the smp_wmb() in +* kvm_mmu_notifier_invalidate_range_end() to guarantee either the * old (non-zero) value of mn_active_invalidate_count or the -* new (incremented) value of mmu_invalidate_seq is observed. +* new (incremented) value of gpc_invalidate_seq is observed. */ smp_rmb (); -return kvm->mmu_invalidate_seq != mmu_seq; +return kvm->gpc_invalidate_seq != gpc_seq; }static kvm_pfn_t hva_to_pfn_retry (struct gfn_to_pfn_cache *gpc)
@@ -158,7 +158,7 @@ static kvm_pfn_t hva_to_pfn_retry(struct gfn_to_pfn_cache *gpc)void *old_khva = (void *)PAGE_ALIGN_DOWN ((uintptr_t )gpc->khva);kvm_pfn_t new_pfn =KVM_PFN_ERR_FAULT ;void *new_khva =NULL ; -unsigned long mmu_seq; +unsigned long gpc_seq;struct page *page;struct kvm_follow_pfn kfp = {
@@ -181,7 +181,7 @@ static kvm_pfn_t hva_to_pfn_retry(struct gfn_to_pfn_cache *gpc) gpc->valid = false; do { - mmu_seq = gpc->kvm->mmu_invalidate_seq; + gpc_seq = gpc->kvm->gpc_invalidate_seq; smp_rmb(); write_unlock_irq(&gpc->lock);
@@ -232,7 +232,7 @@ static kvm_pfn_t hva_to_pfn_retry(struct gfn_to_pfn_cache *gpc)* attempting to refresh. */ WARN_ON_ONCE (gpc->valid); - }while (mmu_notifier_retry_cache (gpc->kvm, mmu_seq)); + }while (mmu_notifier_retry_cache (gpc->kvm, gpc_seq)); gpc->valid = true; gpc->pfn = new_pfn;
Sasha Levin
maintainerPATCH 1/4 KVM: s390: Use generic VIRT_XFER_TO_GUEST_WORK functions
From: Andrew Donnellan <[email protected]>
[ Upstream commit 2bd1337a1295e012e60008ee21a64375e5234e12 ]
Switch to using the generic infrastructure to check for and handle pending work before transitioning into guest mode.
xfer_to_guest_mode_handle_work() does a few more things than the current code does when deciding whether or not to exit the __vcpu_run() loop. The exittime tests from kvm-unit-tests, in my tests, were within a few percent compared to before this series, which is within noise tolerance.
Stable-dep-of: da260bcb56fe ("KVM: pfncache: use a dedicated invalidation sequence for cache refresh")
--- arch/s390/kvm/Kconfig | 1 + arch/s390/kvm/kvm-s390.c | 25 ++++++++++++++++++------- arch/s390/kvm/vsie.c | 18 +++++++++++++----- 3 files changed, 32 insertions(+), 12 deletions(-)
arch/s390/kvm/Kconfig +1 -0
@@ -30,6 +30,7 @@ config KVM select HAVE_KVM_NO_POLL select KVM_VFIO select MMU_NOTIFIER + select VIRT_XFER_TO_GUEST_WORK help Support hosting paravirtualized guest machines using the SIE virtualization capability on the mainframe. This should work
arch/s390/kvm/kvm-s390.c +18 -7
@@ -14,6 +14,7 @@#define pr_fmt (fmt) KMSG_COMPONENT ": " fmt#include <linux/compiler.h> +#include <linux/entry-virt.h> #include <linux/export.h> #include <linux/err.h> #include <linux/fs.h>
@@ -4790,9 +4791,6 @@ static int vcpu_pre_run(struct kvm_vcpu *vcpu) vcpu->arch.sie_block->gg14 = vcpu->run->s.regs.gprs[14 ]; vcpu->arch.sie_block->gg15 = vcpu->run->s.regs.gprs[15 ]; -if (need_resched ()) -schedule (); -if (!kvm_is_ucontrol (vcpu->kvm)) { rc =kvm_s390_deliver_pending_interrupts (vcpu);if (rc ||guestdbg_exit_pending (vcpu))
@@ -5102,12 +5100,12 @@ static int __vcpu_run(struct kvm_vcpu *vcpu)*/ kvm_vcpu_srcu_read_lock (vcpu); - do { +while (true) { rc =vcpu_pre_run (vcpu); +kvm_vcpu_srcu_read_unlock (vcpu);if (rc ||guestdbg_exit_pending (vcpu))break ; - kvm_vcpu_srcu_read_unlock(vcpu);/* * As PF_VCPU will be used in fault handler, between * guest_timing_enter_irqoff and guest_timing_exit_irqoff
@@ -5119,7 +5117,17 @@ static int __vcpu_run(struct kvm_vcpu *vcpu) sizeof(sie_page->pv_grregs)); } +xfer_to_guest_mode_check:local_irq_disable (); +xfer_to_guest_mode_prepare (); +if (xfer_to_guest_mode_work_pending ()) { +local_irq_enable (); + rc =kvm_xfer_to_guest_mode_handle_work (vcpu); +if (rc) +break ; + goto xfer_to_guest_mode_check; + } +guest_timing_enter_irqoff ();__disable_cpu_timer_accounting (vcpu);
@@ -5149,9 +5157,12 @@ static int __vcpu_run(struct kvm_vcpu *vcpu)kvm_vcpu_srcu_read_lock (vcpu); rc =vcpu_post_run (vcpu, exit_reason); - }while (!signal_pending (current) && !guestdbg_exit_pending (vcpu) && !rc); +if (rc ||guestdbg_exit_pending (vcpu)) { +kvm_vcpu_srcu_read_unlock (vcpu); +break ; + } + } -kvm_vcpu_srcu_read_unlock (vcpu);return rc; }
arch/s390/kvm/vsie.c +13 -5
@@ -1181,12 +1181,23 @@ static int do_vsie_run(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page) current->thread.gmap_int_code =0 ;barrier ();if (!kvm_s390_vcpu_sie_inhibited (vcpu)) { +xfer_to_guest_mode_check:local_irq_disable (); +xfer_to_guest_mode_prepare (); +if (xfer_to_guest_mode_work_pending ()) { +local_irq_enable (); + rc =kvm_xfer_to_guest_mode_handle_work (vcpu); +if (rc) + goto skip_sie; + goto xfer_to_guest_mode_check; + }guest_timing_enter_irqoff (); rc =kvm_s390_enter_exit_sie (scb_s, vcpu->run->s.regs.gprs, vsie_page->gmap->asce);guest_timing_exit_irqoff ();local_irq_enable (); } + +skip_sie:barrier (); vcpu->arch.sie_block->prog0c &= ~PROG_IN_SIE ;
@@ -1346,13 +1357,11 @@ static int vsie_run(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page)* but rewind the PSW to re-enter SIE once that's completed * instead of passing a "no action" intercept to the guest. */ -if (signal_pending (current) || -kvm_s390_vcpu_has_irq (vcpu,0 ) || +if (kvm_s390_vcpu_has_irq (vcpu,0 ) ||kvm_s390_vcpu_sie_inhibited (vcpu)) {kvm_s390_rewind_psw (vcpu,4 );break ; } -cond_resched (); }if (rc == -EFAULT ) {
@@ -1484,8 +1493,7 @@ int kvm_s390_handle_vsie(struct kvm_vcpu *vcpu)if (unlikely (scb_addr &0x1ffUL ))return kvm_s390_inject_program_int (vcpu,PGM_SPECIFICATION ); -if (signal_pending (current) ||kvm_s390_vcpu_has_irq (vcpu,0 ) || -kvm_s390_vcpu_sie_inhibited (vcpu)) { +if (kvm_s390_vcpu_has_irq (vcpu,0 ) ||kvm_s390_vcpu_sie_inhibited (vcpu)) {kvm_s390_rewind_psw (vcpu,4 );return 0 ; }
Signature · 1 line
2.53.0
Sasha Levin
maintainerPATCH 2/4 KVM: s390: Enable KVM_GENERIC_MMU_NOTIFIER
From: Claudio Imbrenda <[email protected]>
[ Upstream commit a2f2798fa6d18d91766b19a4896bd9fd0654d730 ]
Enable KVM_GENERIC_MMU_NOTIFIER, for now with empty placeholder callbacks.
Also enable KVM_MMU_LOCKLESS_AGING and define KVM_HAVE_MMU_RWLOCK.
Stable-dep-of: da260bcb56fe ("KVM: pfncache: use a dedicated invalidation sequence for cache refresh")
--- arch/s390/include/asm/kvm_host.h | 1 + arch/s390/kvm/Kconfig | 2 ++ arch/s390/kvm/kvm-s390.c | 45 +++++++++++++++++++++++++++++++- 3 files changed, 47 insertions(+), 1 deletion(-)
arch/s390/include/asm/kvm_host.h +1 -0
@@ -27,6 +27,7 @@#include <asm/isc.h> #include <asm/guarded_storage.h> +#define KVM_HAVE_MMU_RWLOCK #define KVM_MAX_VCPUS 255#define KVM_INTERNAL_MEM_SLOTS 1
arch/s390/kvm/Kconfig +2 -0
@@ -31,6 +31,8 @@ config KVM select KVM_VFIO select MMU_NOTIFIER select VIRT_XFER_TO_GUEST_WORK + select KVM_GENERIC_MMU_NOTIFIER + select KVM_MMU_LOCKLESS_AGING help Support hosting paravirtualized guest machines using the SIE virtualization capability on the mainframe. This should work
arch/s390/kvm/kvm-s390.c +44 -1
57 lines not on this page · raw message
Signature · 1 line
2.53.0
Re: [PATCH 6.18.y 2/4] KVM: s390: Enable KVM_GENERIC_MMU_NOTIFIER
I'm a little confused, why is this being backported to 6.18?
KVM_GENERIC_MMU_NOTIFIER should not be enabled on s390; s390 will not use mmu_notifiers for KVM until 7.0
Patch da260bcb56fe ("KVM: pfncache: use a dedicated invalidation sequence for cache refresh") does not seem to affect the old s390 code present in 6.18
Or am I missing something?
Quoted 119 lines · On Wed, 4 Nov 2026 18:13:02 -0500 Sasha Levin <[email protected]> wrote:
From: Claudio Imbrenda <[email protected]>
[ Upstream commit
a2f2798fa6d18d91766b19a4896bd9fd0654d730]Enable
KVM_GENERIC_MMU_NOTIFIER, for now with empty placeholder callbacks.Also enable
KVM_MMU_LOCKLESS_AGINGand defineKVM_HAVE_MMU_RWLOCK.Reviewed-by Steffen Eiden <[email protected]>Reviewed-by Christoph Schlameuss <[email protected]>Acked-by Heiko Carstens <[email protected]>Signed-off-by Claudio Imbrenda <[email protected]>Stable-dep-of:
da260bcb56fe("KVM: pfncache: use a dedicated invalidation sequence for cache refresh") Signed-off-by: Sasha Levin <[email protected]> ---arch/s390/include/asm/kvm_host.h | 1 + arch/s390/kvm/Kconfig | 2 ++ arch/s390/kvm/kvm-s390.c | 45+++++++++++++++++++++++++++++++- 3 files changed, 47 insertions(+), 1 deletion(-)
(quoted diff) +0
-0
b/arch/s390/include/asm/kvm_host.hindexc2ba3d4398c53..f5f87dae0dd9d100644 ---a/arch/s390/include/asm/kvm_host.h
+++b/arch/s390/include/asm/kvm_host.harch/s390/include/asm/kvm_host.h +1
-0@@ -27,6 +27,7 @@#include <asm/isc.h> #include <asm/guarded_storage.h> +#define KVM_HAVE_MMU_RWLOCK #define KVM_MAX_VCPUS 255#define KVM_INTERNAL_MEM_SLOTS 1arch/s390/kvm/Kconfig +2
-0@@ -31,6 +31,8 @@ config KVM select KVM_VFIO select MMU_NOTIFIER select VIRT_XFER_TO_GUEST_WORK + select KVM_GENERIC_MMU_NOTIFIER + select KVM_MMU_LOCKLESS_AGING help Support hosting paravirtualized guest machines using theSIE virtualization capability on the mainframe. This should work
arch/s390/kvm/kvm-s390.c +5
-1@@ -4919,7 +4919,7 @@ int __kvm_s390_handle_dat_fault(struct kvm_vcpu *vcpu, gfn_t gfn, gpa_t gaddr, u rc =fixup_user_fault (vcpu->arch.gmap->mm, vmaddr, fault_flags, &unlocked);if (!rc) rc =__gmap_link (vcpu->arch.gmap, gaddr, vmaddr); - scoped_guard(spinlock, &vcpu->kvm->mmu_lock) { + scoped_guard(read_lock, &vcpu->kvm->mmu_lock) {kvm_release_faultin_page (vcpu->kvm, page, false, writable); }mmap_read_unlock (vcpu->arch.gmap->mm);@@ -6141,6 +6141,49 @@ void kvm_arch_commit_memory_region(struct kvm *kvm, return; } +/** +* kvm_test_age_gfn() - test young +* @kvm: the kvm instance +* @range: the range of guest addresses whose young status needs to be cleared
+ *
+ * Context: called by KVM common code without holding the kvm mmu lock
+ * Return: true if any page in the given range is young, otherwise 0.
+ */arch/s390/kvm/kvm-s390.c +9
-0+bool kvm_test_age_gfn (struct kvm *kvm,struct kvm_gfn_range *range) +{ +return false; +} + +/** +* kvm_age_gfn() - clear young +* @kvm: the kvm instance +* @range: the range of guest addresses whose young status needs to be cleared
+ *
+ * Context: called by KVM common code without holding the kvm mmu lock
+ * Return: true if any page in the given range was young, otherwise 0.
+ */arch/s390/kvm/kvm-s390.c +11
-0+bool kvm_age_gfn (struct kvm *kvm,struct kvm_gfn_range *range) +{ +return false; +} + +/** +* kvm_unmap_gfn_range() - Unmap a range of guest addresses +* @kvm: the kvm instance +* @range: the range of guest page frames to invalidate +* +* This function always returns false because every DAT table modification
+ * has to use the appropriate DAT table manipulation instructions, which will
+ * keep the TLB coherent, hence no additional TLB flush is ever required.
+ *
+ * Context: called by KVM common code with the kvm mmu write lock held
+ * Return: false
+ */
+boolkvm_unmap_gfn_range(struct kvm *kvm, struct kvm_gfn_range *range)+{arch/s390/kvm/kvm-s390.c +3
-0+return false; +} +static inline unsigned long nonhyp_mask (int i) {unsigned int nonhyp_fai = (sclp.hmfai << i *2 ) >>30 ;
Sasha Levin
maintainerRe: [PATCH 6.18.y 2/4] KVM: s390: Enable KVM_GENERIC_MMU_NOTIFIER
Quoted 4 lines
I'm a little confused, why is this being backported to 6.18?
KVM_GENERIC_MMU_NOTIFIERshould not be enabled on s390; s390 will not usemmu_notifiersfor KVM until 7.0
Thanks for catching this. I am dropping the series from 6.18.
Signature · 2 lines
Thanks, Sasha
Sasha Levin
maintainerPATCH 3/4 KVM: remove CONFIG_KVM_GENERIC_MMU_NOTIFIER
In reply to Sasha LevinFrom: Paolo Bonzini <[email protected]>
[ Upstream commit 407fd8b8d8cce03856aa67329715de48b254b529 ]
All architectures now use MMU notifier for KVM page table management. Remove the Kconfig symbol and the code that is used when it is disabled.
Stable-dep-of: da260bcb56fe ("KVM: pfncache: use a dedicated invalidation sequence for cache refresh")
--- arch/arm64/kvm/Kconfig | 1 - arch/loongarch/kvm/Kconfig | 1 - arch/mips/kvm/Kconfig | 1 - arch/powerpc/kvm/Kconfig | 4 ---- arch/powerpc/kvm/powerpc.c | 1 - arch/riscv/kvm/Kconfig | 1 - arch/s390/kvm/Kconfig | 2 -- arch/x86/kvm/Kconfig | 1 - include/linux/kvm_host.h | 7 +------ virt/kvm/Kconfig | 9 +-------- virt/kvm/kvm_main.c | 16 ---------------- 11 files changed, 2 insertions(+), 42 deletions(-)
arch/arm64/kvm/Kconfig +0 -1
@@ -21,7 +21,6 @@ menuconfig KVM bool "Kernel-based Virtual Machine (KVM) support" select KVM_COMMON select KVM_GENERIC_HARDWARE_ENABLING - select KVM_GENERIC_MMU_NOTIFIER select HAVE_KVM_CPU_RELAX_INTERCEPT select KVM_MMIO select KVM_GENERIC_DIRTYLOG_READ_PROTECT
arch/loongarch/kvm/Kconfig +0 -1
@@ -29,7 +29,6 @@ config KVM select KVM_COMMON select KVM_GENERIC_DIRTYLOG_READ_PROTECT select KVM_GENERIC_HARDWARE_ENABLING - select KVM_GENERIC_MMU_NOTIFIER select KVM_MMIO select VIRT_XFER_TO_GUEST_WORK select SCHED_INFO
arch/mips/kvm/Kconfig +0 -1
@@ -24,7 +24,6 @@ config KVM select KVM_GENERIC_DIRTYLOG_READ_PROTECT select HAVE_KVM_VCPU_ASYNC_IOCTL select KVM_MMIO - select KVM_GENERIC_MMU_NOTIFIER select KVM_GENERIC_HARDWARE_ENABLING select HAVE_KVM_READONLY_MEM help
arch/powerpc/kvm/Kconfig +0 -4
@@ -39,7 +39,6 @@ config KVM_BOOK3S_64_HANDLER config KVM_BOOK3S_PR_POSSIBLE bool select KVM_MMIO - select KVM_GENERIC_MMU_NOTIFIER config KVM_BOOK3S_HV_POSSIBLE bool
@@ -82,7 +81,6 @@ config KVM_BOOK3S_64_HV tristate "KVM for POWER7 and later using hypervisor mode in host" depends on KVM_BOOK3S_64 && PPC_POWERNV select KVM_BOOK3S_HV_POSSIBLE - select KVM_GENERIC_MMU_NOTIFIER select KVM_BOOK3S_HV_PMU select CMA help
@@ -204,7 +202,6 @@ config KVM_E500V2 depends on !CONTEXT_TRACKING_USER select KVM select KVM_MMIO - select KVM_GENERIC_MMU_NOTIFIER help Support running unmodified E500 guest kernels in virtual machines on E500v2 host processors.
@@ -221,7 +218,6 @@ config KVM_E500MC select KVM select KVM_MMIO select KVM_BOOKE_HV - select KVM_GENERIC_MMU_NOTIFIER help Support running unmodified E500MC/E5500/E6500 guest kernels in virtual machines on E500MC/E5500/E6500 host processors.
arch/powerpc/kvm/powerpc.c +0 -1
@@ -631,7 +631,6 @@ int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)break ;#endif case KVM_CAP_SYNC_MMU : -BUILD_BUG_ON (!IS_ENABLED (CONFIG_KVM_GENERIC_MMU_NOTIFIER )); r =1 ;break ;#ifdef CONFIG_KVM_BOOK3S_HV_POSSIBLE
arch/riscv/kvm/Kconfig +0 -1
@@ -31,7 +31,6 @@ config KVM select KVM_GENERIC_HARDWARE_ENABLING select KVM_MMIO select VIRT_XFER_TO_GUEST_WORK - select KVM_GENERIC_MMU_NOTIFIER select SCHED_INFO select GUEST_PERF_EVENTS if PERF_EVENTS help
arch/s390/kvm/Kconfig +0 -2
@@ -29,9 +29,7 @@ config KVM select HAVE_KVM_INVALID_WAKEUPS select HAVE_KVM_NO_POLL select KVM_VFIO - select MMU_NOTIFIER select VIRT_XFER_TO_GUEST_WORK - select KVM_GENERIC_MMU_NOTIFIER select KVM_MMU_LOCKLESS_AGING help Support hosting paravirtualized guest machines using the SIE
arch/x86/kvm/Kconfig +0 -1
@@ -20,7 +20,6 @@ if VIRTUALIZATION config KVM_X86 def_tristate KVM if (KVM_INTEL != n || KVM_AMD != n) select KVM_COMMON - select KVM_GENERIC_MMU_NOTIFIER select KVM_ELIDE_TLB_FLUSH_IF_YOUNG select KVM_MMU_LOCKLESS_AGING select HAVE_KVM_IRQCHIP
include/linux/kvm_host.h +1 -6
@@ -253,7 +253,6 @@ bool kvm_setup_async_pf(struct kvm_vcpu *vcpu, gpa_t cr2_or_gpa,int kvm_async_pf_wakeup_all (struct kvm_vcpu *vcpu);#endif -#ifdef CONFIG_KVM_GENERIC_MMU_NOTIFIER union kvm_mmu_notifier_arg {unsigned long attributes; };
@@ -275,7 +274,6 @@ struct kvm_gfn_range {bool kvm_unmap_gfn_range (struct kvm *kvm,struct kvm_gfn_range *range);bool kvm_age_gfn (struct kvm *kvm,struct kvm_gfn_range *range);bool kvm_test_age_gfn (struct kvm *kvm,struct kvm_gfn_range *range); -#endif enum {OUTSIDE_GUEST_MODE ,
@@ -848,13 +846,12 @@ struct kvm {struct hlist_head irq_ack_notifier_list;#endif -#ifdef CONFIG_KVM_GENERIC_MMU_NOTIFIER struct mmu_notifier mmu_notifier;unsigned long mmu_invalidate_seq;long mmu_invalidate_in_progress;gfn_t mmu_invalidate_range_start;gfn_t mmu_invalidate_range_end; -#endif +struct list_head devices;u64 manual_dirty_log_protect;struct dentry *debugfs_dentry;
@@ -2100,7 +2097,6 @@ extern const struct kvm_stats_desc kvm_vm_stats_desc[];extern const struct kvm_stats_header kvm_vcpu_stats_header;extern const struct kvm_stats_desc kvm_vcpu_stats_desc[]; -#ifdef CONFIG_KVM_GENERIC_MMU_NOTIFIER static inline int mmu_invalidate_retry (struct kvm *kvm,unsigned long mmu_seq) {if (unlikely (kvm->mmu_invalidate_in_progress))
@@ -2178,7 +2174,6 @@ static inline bool mmu_invalidate_retry_gfn_unsafe(struct kvm *kvm,return READ_ONCE (kvm->mmu_invalidate_seq) != mmu_seq; } -#endif #ifdef CONFIG_HAVE_KVM_IRQ_ROUTING
virt/kvm/Kconfig +1 -8
@@ -5,6 +5,7 @@ config KVM_COMMON bool select EVENTFD select INTERVAL_TREE + select MMU_NOTIFIER select PREEMPT_NOTIFIERS config HAVE_KVM_PFNCACHE
@@ -96,24 +97,16 @@ config HAVE_KVM_PM_NOTIFIER config KVM_GENERIC_HARDWARE_ENABLING bool -config KVM_GENERIC_MMU_NOTIFIER - select MMU_NOTIFIER - bool - config KVM_ELIDE_TLB_FLUSH_IF_YOUNG - depends on KVM_GENERIC_MMU_NOTIFIER bool config KVM_MMU_LOCKLESS_AGING - depends on KVM_GENERIC_MMU_NOTIFIER bool config KVM_GENERIC_MEMORY_ATTRIBUTES - depends on KVM_GENERIC_MMU_NOTIFIER bool config KVM_GUEST_MEMFD - depends on KVM_GENERIC_MMU_NOTIFIER select XARRAY_MULTI bool
virt/kvm/kvm_main.c +0 -16
@@ -501,7 +501,6 @@ void kvm_destroy_vcpus(struct kvm *kvm) }EXPORT_SYMBOL_FOR_KVM_INTERNAL (kvm_destroy_vcpus); -#ifdef CONFIG_KVM_GENERIC_MMU_NOTIFIER static inline struct kvm *mmu_notifier_to_kvm (struct mmu_notifier *mn) { return container_of(mn, struct kvm, mmu_notifier);
@@ -900,15 +899,6 @@ static int kvm_init_mmu_notifier(struct kvm *kvm)return mmu_notifier_register (&kvm->mmu_notifier, current->mm); } -#else /* !CONFIG_KVM_GENERIC_MMU_NOTIFIER */ - -static int kvm_init_mmu_notifier (struct kvm *kvm) -{ -return 0 ; -} - -#endif /* CONFIG_KVM_GENERIC_MMU_NOTIFIER */ -#ifdef CONFIG_HAVE_KVM_PM_NOTIFIER static int kvm_pm_notifier_call(struct notifier_block *bl, unsigned long state,
@@ -1225,10 +1215,8 @@ static struct kvm *kvm_create_vm(unsigned long type, const char *fdname) out_err_no_debugfs:kvm_coalesced_mmio_free (kvm); out_no_coalesced_mmio: -#ifdef CONFIG_KVM_GENERIC_MMU_NOTIFIER if (kvm->mmu_notifier.ops)mmu_notifier_unregister (&kvm->mmu_notifier, current->mm); -#endif out_err_no_mmu_notifier:kvm_disable_virtualization (); out_err_no_disable:
@@ -1291,7 +1279,6 @@ static void kvm_destroy_vm(struct kvm *kvm) kvm->buses[i] =NULL ; }kvm_coalesced_mmio_free (kvm); -#ifdef CONFIG_KVM_GENERIC_MMU_NOTIFIER mmu_notifier_unregister (&kvm->mmu_notifier, kvm->mm);/* * At this point, pending calls to invalidate_range_start()
@@ -1310,9 +1297,6 @@ static void kvm_destroy_vm(struct kvm *kvm) kvm->mn_active_invalidate_count =0 ; else WARN_ON(kvm->mmu_invalidate_in_progress); -#else -kvm_flush_shadow_all (kvm); -#endif kvm_arch_destroy_vm (kvm);kvm_destroy_devices (kvm);for (i =0 ; i <kvm_arch_nr_memslot_as_ids (kvm); i++) {
Signature · 1 line
2.53.0
Sasha Levin
maintainerPATCH 4/4 KVM: pfncache: use a dedicated invalidation sequence for cache refresh
In reply to Sasha LevinFrom: David Woodhouse <[email protected]>
[ Upstream commit da260bcb56feeb6489a83f310308453a0a7de25e ]
The gfn_to_pfn_cache refresh path guards against mmu notifier invalidations which complete while it has dropped gpc->lock for the HVA->PFN lookup: hva_to_pfn_retry() samples kvm->mmu_invalidate_seq and retries if it changed, or if mn_active_invalidate_count is still elevated.
That is insufficient for HVA-based caches. mmu_invalidate_seq is only advanced by kvm_mmu_invalidate_end() when the invalidated range overlaps a memslot, and an HVA-based cache (e.g. the Xen shared_info page mapped with KVM_XEN_ATTR_TYPE_SHARED_INFO_HVA) need not be backed by any memslot at all. An invalidation of the cached HVA which starts and ends entirely within the lookup window is thus invisible to the retry check: mn_active_invalidate_count is back to zero and the sequence never moved. The refresh then publishes a mapping of a page which has already been freed, and the next reader dereferences it:
BUG: KASAN: use-after-free in kvm_xen_shared_info_init+0x3c6/0x440 Read of size 4 at addr ffff8880599c2900 by task syz.2.383/7257
Since gfn_to_pfn_cache_invalidate_start() deliberately skips caches which are not currently valid (including one whose refresh is in progress, as the refresh clears the valid flag before dropping the lock), the retry check is the only line of defence, and it must fire for *any* invalidation, not just those hitting a memslot.
Add a dedicated kvm->gpc_invalidate_seq, incremented by every kvm_mmu_notifier_invalidate_range_end() under mn_invalidate_lock before mn_active_invalidate_count is decremented, and check it in hva_to_pfn_retry() instead of mmu_invalidate_seq. Incrementing in range_end() in the same critical section as the in-progress count also closes the variant where the cache is activated with the contested HVA only after invalidate_range_start() has run.
The same bug is also reachable through the per-vCPU vcpu_info cache (KVM_XEN_VCPU_ATTR_TYPE_VCPU_INFO_HVA), where the stale mapping is then dereferenced by kvm_setup_guest_pvclock() on the next KVM_RUN:
BUG: KASAN: use-after-free in kvm_setup_guest_pvclock+0x5bf/0x660
This intentionally makes refresh retry on *unrelated* mmu notifier events; restoring precision (and reworking the GPC locking more generally) is left for a subsequent series.
Reproducers: https://david.woodhou.se/xen_shinfo_race.c
https://david.woodhou.se/vcpu_info_race.c
--- include/linux/kvm_host.h | 2 ++ virt/kvm/kvm_main.c | 10 ++++++++++ virt/kvm/pfncache.c | 18 +++++++++--------- 3 files changed, 21 insertions(+), 9 deletions(-)
include/linux/kvm_host.h +2 -0
@@ -852,6 +852,8 @@ struct kvm {gfn_t mmu_invalidate_range_start;gfn_t mmu_invalidate_range_end; +unsigned long gpc_invalidate_seq; +struct list_head devices;u64 manual_dirty_log_protect;struct dentry *debugfs_dentry;
virt/kvm/kvm_main.c +10 -0
@@ -816,6 +816,16 @@ static void kvm_mmu_notifier_invalidate_range_end(struct mmu_notifier *mn,/* Pairs with the increment in range_start(). */ spin_lock (&kvm->mn_invalidate_lock); + kvm->gpc_invalidate_seq++; + +/* +* As with the MMU sequence counter and mmu_invalidate_in_progress, the +* GPC sequence increase must be visible before the invalidate count +* goes to zero. Pairs with the smp_rmb() in +* mmu_notifier_retry_cache(). +*/ +smp_wmb (); +if (!WARN_ON_ONCE (!kvm->mn_active_invalidate_count)) --kvm->mn_active_invalidate_count; wake = !kvm->mn_active_invalidate_count;
virt/kvm/pfncache.c +9 -9
@@ -124,7 +124,7 @@ static void gpc_unmap(kvm_pfn_t pfn, void *khva)#endif } -static inline bool mmu_notifier_retry_cache (struct kvm *kvm,unsigned long mmu_seq) +static inline bool mmu_notifier_retry_cache (struct kvm *kvm,unsigned long gpc_seq) {/* * mn_active_invalidate_count acts for all intents and purposes
@@ -136,20 +136,20 @@ static inline bool mmu_notifier_retry_cache(struct kvm *kvm, unsigned long mmu_s* Note, it does not matter that mn_active_invalidate_count * is not protected by gpc->lock. It is guaranteed to * be elevated before the mmu_notifier acquires gpc->lock, and -* isn't dropped until after mmu_invalidate_seq is updated. +* isn't dropped until after gpc_invalidate_seq is updated. */ if (kvm->mn_active_invalidate_count)return true;/* * Ensure mn_active_invalidate_count is read before -* mmu_invalidate_seq. This pairs with the smp_wmb() in -* mmu_notifier_invalidate_range_end() to guarantee either the +* gpc_invalidate_seq. This pairs with the smp_wmb() in +* kvm_mmu_notifier_invalidate_range_end() to guarantee either the * old (non-zero) value of mn_active_invalidate_count or the -* new (incremented) value of mmu_invalidate_seq is observed. +* new (incremented) value of gpc_invalidate_seq is observed. */ smp_rmb (); -return kvm->mmu_invalidate_seq != mmu_seq; +return kvm->gpc_invalidate_seq != gpc_seq; }static kvm_pfn_t hva_to_pfn_retry (struct gfn_to_pfn_cache *gpc)
@@ -158,7 +158,7 @@ static kvm_pfn_t hva_to_pfn_retry(struct gfn_to_pfn_cache *gpc)void *old_khva = (void *)PAGE_ALIGN_DOWN ((uintptr_t )gpc->khva);kvm_pfn_t new_pfn =KVM_PFN_ERR_FAULT ;void *new_khva =NULL ; -unsigned long mmu_seq; +unsigned long gpc_seq;struct page *page;struct kvm_follow_pfn kfp = {
@@ -181,7 +181,7 @@ static kvm_pfn_t hva_to_pfn_retry(struct gfn_to_pfn_cache *gpc) gpc->valid = false; do { - mmu_seq = gpc->kvm->mmu_invalidate_seq; + gpc_seq = gpc->kvm->gpc_invalidate_seq; smp_rmb(); write_unlock_irq(&gpc->lock);
@@ -232,7 +232,7 @@ static kvm_pfn_t hva_to_pfn_retry(struct gfn_to_pfn_cache *gpc)* attempting to refresh. */ WARN_ON_ONCE (gpc->valid); - }while (mmu_notifier_retry_cache (gpc->kvm, mmu_seq)); + }while (mmu_notifier_retry_cache (gpc->kvm, gpc_seq)); gpc->valid = true; gpc->pfn = new_pfn;
Signature · 1 line
2.53.0