From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=170.10.133.124 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796407206; b=3PAAlUrLMf/4f/aVTQ+p6V7zFwPRS2XkWGTAPF2C8A2i4AoQcKvuvWiA9Ah0Gaao/JJ+ ZoMkdxr4WT5hen5XMo6IBQbMLzSLir5GZtU1ywiNhb5xMg7t2QMuKu6g4RKQRHRzd2ebK S+C2skBbd1IHShRcqHgbGdX2eQXoBfkMlX9ils5TODaY7Ig/GuOV9bhwEzlxlK5+dT7bi KYitwf7O2LOQyVhAHePFqyCwLAAiuqFTRhEq3BD2RYfnEIaBYlFpNl9EVq/Je6mB/zJQ6 prDVzzrpiDtds9hkc2PsOxPugqg5PQKXLrBn8g1ga1L/HsdNqDget4GXehlfSR326YA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796407206; h=DMARC-Filter:DKIM-Signature:Received:X-MC-Unique: X-Mimecast-MFC-AGG-ID:Received:Received:From:To:Cc:Subject:Date: Message-ID:In-Reply-To:References:MIME-Version: Content-Transfer-Encoding:X-Scanned-By; bh=ZRcwwTaRhtUILMEDHNIPyM34JnzKjVxMp6O0WV8eb2k=; b=aiEr0+kvJ69kRCqcZtm6EgLGJLYR9E86gg54Bz1Z21Bb6PuAGUDLqy5pfg2i3X8YvTaN n0/ZHv7SBCUH5e+yoRIbYtS8QEr4MoLqzDLKTllMk0beTDpkPmLX+oRLfXFUSL9tMcRUB J22RWmo+WZKgubE7YUczL5B/yvx1ivcEbOVk2Ezy/USHyhjyMGAvp2U5sq+xOpXnKsKwa 07J15Q3+cr4q51gFkqN3NvW2RG+iKA3Ebs5HyIOiPN00sGj4fiT9sTAC/1sy6717EoAjk b8qvdIFj1xs0CgaY68Qpk6EI/oPm8G7hSkE2VeMftluGi3l0Xdp3a1qA9NvdL3dH+vw== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=EJcaGdAU; arc=none smtp.remote-ip=170.10.133.124 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id AE1C3606A6A8 for ; Fri, 4 Dec 2026 18:00:05 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org AE1C3606A6A8 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=EJcaGdAU DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1796407205; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=ZRcwwTaRhtUILMEDHNIPyM34JnzKjVxMp6O0WV8eb2k=; b=EJcaGdAUJtH0M4bJvP0qO7e5K9ZSmLXr54iO6DImsPrNAc9FgPki1GvPsBKRRLVVaH02cA PQq4URb6/pxZesRHr/rtyzhrXQ0SAmAvM893i6XPV8s1Vwrpn618LxZczbD5ULxwswryHO 2jgcyGlhXsaw9kxI305G5c4Xcc0p5Zs= Received: from mx-prod-mc-03.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS with 8BITMIME (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-622-tkwRz447O_2QymTy2HWZ7A-1; Fri, 4 Dec 2026 18:00:00 +0000 X-MC-Unique: tkwRz447O_2QymTy2HWZ7A-1 X-Mimecast-MFC-AGG-ID: tkwRz447O_2QymTy2HWZ7A_1796407199 Received: from mx-prod-int-10.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-10.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.95]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-03.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 1BDA21939037; Fri, 4 Dec 2026 17:59:59 +0000 (UTC) Received: from pbonzini-thinkpadx1carbon6th.rmtit.csb (headnet04.pony-001.prod.iad2.dc.redhat.com [10.2.32.116]) by mx-prod-int-10.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 3CECDBB9; Fri, 4 Dec 2026 17:59:57 +0000 (UTC) From: Paolo Bonzini To: linux-kernel@vger.kernel.org, kvm@vger.kernel.org Cc: Sean Christopherson , stable@vger.kernel.org, Amirmohammad Eftekhar , Chris Mason Subject: [PATCH 13/17] KVM: Invalidate all GFN=>PFN caches when VM and/or its process is dying Date: Fri, 4 Dec 2026 18:59:26 +0100 Message-ID: <20261204175930.2154542-14-pbonzini@redhat.com> In-Reply-To: <20261204175930.2154542-1-pbonzini@redhat.com> References: <20261204175930.2154542-1-pbonzini@redhat.com> X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Scanned-By: MIMEDefang 3.6 on 10.30.177.95 From: Sean Christopherson 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: 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 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 Closes: https://lore.kernel.org/all/20261017193911.40182-1-amirmohammad.eftekhar@cispa.de Suggested-by: Amirmohammad Eftekhar Reported-by: Chris Mason Suggested-by: Chris Mason Signed-off-by: Sean Christopherson Link: https://patch.msgid.link/20261120195742.55768-6-seanjc@google.com Signed-off-by: Paolo Bonzini --- 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 9276ec8c64b9..be9f4cc6e2ed 100644 --- a/virt/kvm/kvm_main.c +++ b/virt/kvm/kvm_main.c @@ -902,6 +902,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 c65cfdb14f1c..164bb12d5252 100644 --- a/virt/kvm/kvm_mm.h +++ b/virt/kvm/kvm_mm.h @@ -62,12 +62,14 @@ kvm_pfn_t hva_to_pfn(struct kvm_follow_pfn *kfp); 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 */ #endif /* __KVM_MM_H__ */ diff --git a/virt/kvm/pfncache.c b/virt/kvm/pfncache.c index 3659686b97c2..75e146571a93 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