All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] KVM: guest_memfd: Fix ABBA deadlock in error_remove_folio
@ 2026-06-11  8:18 zhanghao
  2026-06-11 10:32 ` sashiko-bot
  2026-06-11 14:18 ` Sean Christopherson
  0 siblings, 2 replies; 3+ messages in thread
From: zhanghao @ 2026-06-11  8:18 UTC (permalink / raw)
  To: Sean Christopherson; +Cc: Paolo Bonzini, kvm

From b164e59d4068226dfb33babe49292c7a685cacd9 Mon Sep 17 00:00:00 2001
From: Hao Zhang <zhanghao1@kylinos.cn>
Date: Thu, 11 Jun 2026 15:27:27 +0800

memory_failure() calls ->error_remove_folio() while holding the global
mf_mutex and the poisoned folio lock. guest_memfd's implementation takes
mapping.invalidate_lock for read before zapping KVM mappings.

That lock ordering can deadlock against guest_memfd punch-hole, which
holds mapping.invalidate_lock for write and can then wait on the same
folio lock in truncate_inode_pages_range().

Use a trylock in kvm_gmem_error_folio(). If mapping.invalidate_lock is
contended, fail recovery instead of blocking in the memory-failure path,
and instead of reporting MF_DELAYED without actually zapping KVM mappings.

Fixes: a7800aa80ea4 ("KVM: Add KVM_CREATE_GUEST_MEMFD ioctl() for guest-specific backing memory")
Signed-off-by: Hao Zhang <zhanghao1@kylinos.cn>
---
 virt/kvm/guest_memfd.c | 11 ++++++++++-
 1 file changed, 10 insertions(+), 1 deletion(-)

diff --git a/virt/kvm/guest_memfd.c b/virt/kvm/guest_memfd.c
index 69c9d6d546b2..9417be3049cf 100644
--- a/virt/kvm/guest_memfd.c
+++ b/virt/kvm/guest_memfd.c
@@ -499,7 +499,16 @@ static int kvm_gmem_error_folio(struct address_space *mapping, struct folio *fol
 {
 	pgoff_t start, end;
 
-	filemap_invalidate_lock_shared(mapping);
+	/*
+	 * memory_failure() holds mf_mutex globally.  We must not block
+	 * on filemap_invalidate_lock here, as it can be held exclusive
+	 * by kvm_gmem_fallocate() (MADV_REMOVE/FALLOC_FL_PUNCH_HOLE
+	 * path), creating an ABBA deadlock with the poisoned folio lock.
+	 * If the invalidation lock is contended, fail the recovery instead
+	 * of reporting MF_DELAYED without zapping KVM mappings.
+	 */
+	if (!filemap_invalidate_trylock_shared(mapping))
+		return -EBUSY;
 
 	start = folio->index;
 	end = start + folio_nr_pages(folio);

base-commit: 9716c086c8e8b141d35aa61f2e96a2e83de212a7
-- 
2.15.0


^ permalink raw reply related	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2026-06-11 14:18 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-06-11  8:18 [PATCH] KVM: guest_memfd: Fix ABBA deadlock in error_remove_folio zhanghao
2026-06-11 10:32 ` sashiko-bot
2026-06-11 14:18 ` Sean Christopherson

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.