From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=170.10.129.124 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796407403; b=tY6sB6PiyFlQZvyOK9uPGL2Ar+PGqe76M2uRhPgu8AB5XyBpr/zT8rSkjpEkWM4w2+1m rfJNAG/xcm5PkWCiSrchxll0dau3ga7lrw4Jpllbb/ICK14Z89pZoAn+b2x2s9NYG/hqG HtHj0j1d+o3UkDWoOup++SvJJNvjWja4/pxq8HpTkQBm0bUp2So7PCbXSMMqxUGpQ8Ywe DC3FKHEv9m3qm4dZGIVg8A15guXZnAFU568WemHPj68NWhMaPgL2d53xNUF024mr5uWA1 WAnp/wXpuqx6ITNbcDSal3If8Lkkf3zKhx9/+u3x89D+1eDQH4eW7zbozi14ybukylw== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796407403; 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=4XcxpXizxsXLGUuGlVEKue7Y/Z4gnAOi1KYKXtXD2Jo=; b=H9BaPjSHpWxsiFGeFQ16/mVQ4Kdy/fINElM6I7ew+mmvo4h2wavmh4XPAP4DIfDcVWZW 5CC3tONwx2thIUlvvMV83iFRKwoySo3rtUQi3+DLCJPoMLhIL58TG9UOCYdQB9gem8P5d ODb1WHMcIKs9C0Cmm2lqWjYm3R4GFh9cxQkCVW9AByT0VZZiiCPTYU3IniB/LT7Xmn7O4 ncNMj37t3W3kRJDgUDd+FUR4u2Vso3vBTUwMJG0L9+WdZEeYevBrdfLh3Ngr+RGHP0B+V 2imeuq9gEG4RXVO0G0EQF70TMm7Y3EFRUqaN5un47LiPKbuwmf78YzYwAdRAdCd2X6w== 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="Q/DGWPs8"; arc=none smtp.remote-ip=170.10.129.124 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.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 B286560A6F48 for ; Fri, 4 Dec 2026 18:03:22 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org B286560A6F48 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=Q/DGWPs8 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1796407402; 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=4XcxpXizxsXLGUuGlVEKue7Y/Z4gnAOi1KYKXtXD2Jo=; b=Q/DGWPs8zC/PuS2bnc1iULR4EJwsxn/cNI3Lf1y625SOkTY5Hg4fVCTUa5L1Cf/CiCR22X Mkbywx2+ximMkracP+Y/fBRQCycAKhVh6A+zakaTWBz7tL7SJfJ8fp6Hi4ornK9DdaEod7 6S69F2oNbYEEhbxieKpZkQZZfH0Uh18= Received: from mx-prod-mc-05.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-227-QRtrgF4PMGqcN5gPQvXE5A-1; Fri, 4 Dec 2026 18:03:15 +0000 X-MC-Unique: QRtrgF4PMGqcN5gPQvXE5A-1 X-Mimecast-MFC-AGG-ID: QRtrgF4PMGqcN5gPQvXE5A_1796407392 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-05.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id ADBCE1909054; Fri, 4 Dec 2026 18:03:12 +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 ED73EBAC; Fri, 4 Dec 2026 18:03:10 +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 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 Message-ID: <20261204180245.2166967-14-pbonzini@redhat.com> In-Reply-To: <20261204180245.2166967-1-pbonzini@redhat.com> References: <20261204180245.2166967-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 [ 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: 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 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 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