KVM: guest_memfd: Fix ABBA deadlock in error_remove_folio
Subsystems (2)
PATCH 1/1 KVM: guest_memfd: Fix ABBA deadlock in error_remove_folio
From b164e59d4068226dfb33babe49292c7a685cacd9 Mon Sep 17 00:00:00 2001
From: Hao Zhang <[email protected]>
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.
--- virt/kvm/guest_memfd.c | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-)
virt/kvm/guest_memfd.c +10 -1
@@ -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
Signature · 1 line
2.15.0
[email protected]
AI reviewThank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
Pre-existing issues:
- [High] kvm_gmem_error_folio incorrectly returns the internal enum MF_DELAYED instead of 0 on success, causing memory_failure() to treat successful recovery as a failure.
- [High] Leaving the poisoned folio in the page cache causes memory_failure() to fail due to an unexpected extra reference during me_pagecache_clean(). --
commit f0c468f1d22ef5955d11ed4dde621b8eef0c2808
Author: Hao Zhang <[email protected]>
KVM: guest_memfd: Fix ABBA deadlock in error_remove_folio
This commit modifies kvm_gmem_error_folio() to use a trylock when acquiring the mapping's invalidate_lock. This prevents an ABBA deadlock with guest_memfd punch-hole operations, returning -EBUSY instead if the lock is contended.
Quoted 12 lines
virt/kvm/guest_memfd.c +3
-1@@ -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
virt/kvm/guest_memfd.c static int kvm_gmem_error_folio(struct address_space *mapping, struct folio *fol · line 514
Quoted 10 lines
virt/kvm/guest_memfd.c +7
-0+* 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);
[Severity: High]
This is a pre-existing issue, but does leaving the poisoned folio in the page cache cause memory_failure() to fail?
In kvm_gmem_error_folio(), guest_memfd intentionally does not truncate the poisoned folio from the page cache so that KVM can gracefully handle guest access to the poisoned page. However, in mm/memory-failure.c:me_pagecache_clean(), the code checks for extra references after truncate_error_folio():
extra_pins = shmem_mapping(mapping); ... if (has_extra_refcount(ps, p, extra_pins)) ret = MF_FAILED;
Because guest_memfd is not shmem, extra_pins is false. has_extra_refcount() then sees the lingering page cache reference on the folio as an unexpected user and returns true. Does this guarantee that me_pagecache_clean() will always return MF_FAILED for guest_memfd, leading to the VM being hard-killed by the MCE handler instead of KVM gracefully recovering?
[Severity: High]
This isn't a bug introduced by this patch, but should this callback return 0 on success instead of MF_DELAYED?
The VFS address_space_operations->error_remove_folio callback contract requires returning 0 on success or a standard negative error code on failure. However, kvm_gmem_error_folio() returns the internal memory-failure enum MF_DELAYED (2).
The caller truncate_error_folio() in mm/memory-failure.c checks this:
if (err != 0)
pr_info("%#lx: Failed to punch page: %d\n", pfn, err);Since it mistakenly treats the positive enum value as an error, will this log "Failed to punch page: 2" and return MF_FAILED, entirely defeating the graceful recovery feature?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Sean Christopherson
maintainer+Lisa, David, and Ackerley
Quoted 11 lines · zhanghao
Quoted 1 line
From
b164e59d4068226dfb33babe49292c7a685cacd9Mon Sep 17 00:00:00 2001From: Hao Zhang <[email protected]>
Date: Thu, 11 Jun 2026 15:27:27 +0800
memory_failure()calls ->error_remove_folio() while holding the globalmf_mutexand 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_memfdpunch-hole, which holds mapping.invalidate_lock for write and can then wait on the same folio lock intruncate_inode_pages_range().
I assume this was found by lockdep? If so, please provide a lockdep splat so that it's easier to understand exactly what all is problematic.
Quoted 15 lines
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 reportingMF_DELAYEDwithout actually zapping KVM mappings.Fixes a7800aa80ea4 ("KVM: Add KVM_CREATE_GUEST_MEMFD ioctl() for guest-specific backing memory")Signed-off-by Hao Zhang <[email protected]>--- virt/kvm/guest_memfd.c | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-)virt/kvm/guest_memfd.c +0
-0
virt/kvm/guest_memfd.c static int kvm_gmem_error_folio(struct address_space *mapping, struct folio *fol · line 503
Quoted 6 lines
virt/kvm/guest_memfd.c +2
-1@@ -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.
Why does mf_mutex matter?
virt/kvm/guest_memfd.c
Quoted 4 lines
We must not block
virt/kvm/guest_memfd.c +3
-0+* 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.
It's not just kvm_gmem_fallocate() that's problematic. Due to the fairness of r/w semaphores, waiting writers block future readers, and so any path that takes a folio lock inside of mapping->invalidate_lock will effectively create the same scenario.
I really don't like doing a trylock here, it feels like we're hacking around a poor locking scheme in guest_memfd. Can't we also fix this by using a dedicated lock for bindings (and for link_at() in the future)?
E.g. untested, but something like this?
kvm/guest_memfd.c +10 -2
@@ -32,6 +32,8 @@ struct gmem_inode {struct inode vfs_inode;struct list_head gmem_file_list; +struct rw_semaphore bindings_lock; +u64 flags; };
@@ -500,7 +502,7 @@ static int kvm_gmem_error_folio(struct address_space *mapping, struct folio *fol {pgoff_t start, end; -filemap_invalidate_lock_shared (mapping); +down_read (&GMEM_I (mapping->host)->bindings_lock); start = folio->index; end = start +folio_nr_pages (folio);
@@ -518,7 +520,7 @@ static int kvm_gmem_error_folio(struct address_space *mapping, struct folio *folkvm_gmem_invalidate_end (mapping->host, start, end); -filemap_invalidate_unlock_shared (mapping); +up_read (&GMEM_I (mapping->host)->bindings_lock);return MF_DELAYED ; }
@@ -597,6 +599,7 @@ static int __kvm_gmem_create(struct kvm *kvm, loff_t size, u64 flags)WARN_ON_ONCE (!mapping_unevictable (inode->i_mapping));GMEM_I (inode)->flags = flags; +init_rwsem (&GMEM_I (inode)->bindings_lock); file =alloc_file_pseudo (inode, kvm_gmem_mnt, name,O_RDWR , &kvm_gmem_fops);if (IS_ERR (file)) {
@@ -691,7 +694,10 @@ int kvm_gmem_bind(struct kvm *kvm, struct kvm_memory_slot *slot,if (kvm_gmem_supports_mmap (inode)) slot->flags |=KVM_MEMSLOT_GMEM_ONLY ; +down_write (&GMEM_I (inode)->bindings_lock);xa_store_range (&f->bindings, start, end -1 , slot,GFP_KERNEL ); +up_write (&GMEM_I (inode)->bindings_lock); +filemap_invalidate_unlock (inode->i_mapping);/*
@@ -746,7 +752,9 @@ void kvm_gmem_unbind(struct kvm_memory_slot *slot) }filemap_invalidate_lock (file->f_mapping); +down_write (&GMEM_I (file_inode (file))->bindings_lock);__kvm_gmem_unbind (slot, file->private_data); +up_write (&GMEM_I (file_inode (file))->bindings_lock);filemap_invalidate_unlock (file->f_mapping); }
Quoted 11 lines · Sean Christopherson
+Lisa, David, and Ackerley
Quoted 8 lines · zhanghao
Quoted 1 line
From
b164e59d4068226dfb33babe49292c7a685cacd9Mon Sep 17 00:00:00 2001From: Hao Zhang <[email protected]>
Date: Thu, 11 Jun 2026 15:27:27 +0800
memory_failure()calls ->error_remove_folio() while holding the globalmf_mutexand the poisoned folio lock. guest_memfd's implementation takes mapping.invalidate_lock for read before zapping KVM mappings.
Quoted 7 lines
Quoted 3 lines
That lock ordering can deadlock against
guest_memfdpunch-hole, which holds mapping.invalidate_lock for write and can then wait on the same folio lock intruncate_inode_pages_range().I assume this was found by lockdep? If so, please provide a lockdep splat so that it's easier to understand exactly what all is problematic.
It was reported by syzkaller as a hung-task report, not as a lockdep "possible circular locking dependency" splat. The relevant part is:
Soft offlining pfn 0x29435 at process virtual address 0x200000841000 Injecting memory failure for pfn 0x678c7 at process virtual address 0x200000ffe000
INFO: task syz.5.14516:73007 blocked for more than 122 seconds.
task:syz.5.14516 state:D pid:73007
Call Trace:
folio_wait_bit_common+0x339/0x890 mm/filemap.c:1323
__folio_lock+0x23/0x40 mm/filemap.c:1704
folio_lock include/linux/pagemap.h:1170 [inline]
truncate_inode_pages_range+0xae7/0xc40 mm/truncate.c:458
kvm_gmem_punch_hole virt/kvm/guest_memfd.c:243 [inline]
kvm_gmem_fallocate+0x4f0/0x8c0 virt/kvm/guest_memfd.c:314
vfs_fallocate+0x4a1/0xfc0 fs/open.c:340
madvise_remove mm/madvise.c:1044 [inline]
madvise_vma_behavior+0x2b2b/0x2d50 mm/madvise.c:1358
do_madvise+0x162/0x230 mm/madvise.c:2028
INFO: task syz.5.14516:73023 blocked for more than 123 seconds.
task:syz.5.14516 state:D pid:73023
Call Trace:
rwsem_down_read_slowpath+0x661/0xd30 kernel/locking/rwsem.c:1086
down_read+0xed/0x4a0 kernel/locking/rwsem.c:1539
filemap_invalidate_lock_shared include/linux/fs.h:1093 [inline]
kvm_gmem_error_folio+0x93/0x530 virt/kvm/guest_memfd.c:502
truncate_error_folio+0x80/0x130 mm/memory-failure.c:941
me_pagecache_clean+0xf2/0x1f0 mm/memory-failure.c:1063
me_pagecache_dirty+0x113/0x1e0 mm/memory-failure.c:1094
identify_page_state+0x167/0x230 mm/memory-failure.c:1661
memory_failure+0x1612/0x26e0 mm/memory-failure.c:2551
madvise_inject_error mm/madvise.c:1487 [inline]
madvise_do_behavior+0x4ae/0x8a0 mm/madvise.c:1925
do_madvise+0x162/0x230 mm/madvise.c:2028
INFO: task syz.5.14516:73023 <reader> blocked on an rw-semaphore
likely owned by task syz.5.14516:73007 <writer>So the observed dependency is:
memory_failure()/kvm_gmem_error_folio()
holds the poisoned folio lock
waits for mapping->invalidate_lock for read
MADV_REMOVE/kvm_gmem_punch_hole()
holds mapping->invalidate_lock for write
waits for the same folio lock in truncate_inode_pages_range()
Quoted 18 lines
Quoted 18 lines
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 reportingMF_DELAYEDwithout actually zapping KVM mappings.Fixes a7800aa80ea4 ("KVM: Add KVM_CREATE_GUEST_MEMFD ioctl() for guest-specific backing memory")Signed-off-by Hao Zhang <[email protected]>--- virt/kvm/guest_memfd.c | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-)virt/kvm/guest_memfd.c +0
-0@@ -499,7 +499,16 @@ static int kvm_gmem_error_folio(struct address_space *mapping, struct folio *fol {pgoff_t start, end;
Quoted 6 lines
virt/kvm/guest_memfd.c static int kvm_gmem_error_folio(struct address_space *mapping, struct folio *fol · line 503
Quoted 3 lines
virt/kvm/guest_memfd.c +2
-1-filemap_invalidate_lock_shared (mapping); +/* +* memory_failure() holds mf_mutex globally. Why does
mf_mutexmatter?
It doesn't matter for the actual deadlock. The relevant dependency is between the poisoned folio lock and mapping->invalidate_lock. I mentioned mf_mutex only because memory_failure() serializes recovery globally, but it is not needed to explain the ABBA deadlock.
Quoted 10 lines
virt/kvm/guest_memfd.c
Quoted 4 lines
We must not block
virt/kvm/guest_memfd.c +3
-0+* 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. It's not just
kvm_gmem_fallocate()that's problematic. Due to the fairness of r/w semaphores, waiting writers block future readers, and so any path that takes a folio lock inside ofmapping->invalidate_lockwill effectively create the same scenario.
Agreed. The issue is not specific to kvm_gmem_fallocate(), it is any path that holds mapping->invalidate_lock for write and then waits on the poisoned folio lock.
Quoted 4 lines
I really don't like doing a trylock here, it feels like we're hacking around a poor locking scheme in
guest_memfd. Can't we also fix this by using a dedicated lock for bindings (and forlink_at()in the future)?
Yes, that looks better. ->error_remove_folio() doesn't need to synchronize against page-cache invalidation itself, it only needs stable guest_memfd bindings while walking them to zap KVM mappings.
I will send a v2 that adds a guest_memfd-private rwsem to protect the gmem_file_list and each gmem_file's bindings, and switches kvm_gmem_error_folio() to take that lock instead of mapping->invalidate_lock.
For paths that need both locks, I plan to keep the order as:
mapping->invalidate_lock
bindings_lock
so that a writer on the bindings lock cannot queue ahead of kvm_gmem_error_folio() while a punch-hole/truncate path is blocked on the poisoned folio lock.
Quoted 61 lines
E.g. untested, but something like this?
kvm/guest_memfd.c +10
-2@@ -32,6 +32,8 @@ struct gmem_inode {struct inode vfs_inode;struct list_head gmem_file_list; +struct rw_semaphore bindings_lock; +u64 flags; };@@ -500,7 +502,7 @@ static int kvm_gmem_error_folio(struct address_space *mapping, struct folio *fol {pgoff_t start, end; -filemap_invalidate_lock_shared (mapping); +down_read (&GMEM_I (mapping->host)->bindings_lock); start = folio->index; end = start +folio_nr_pages (folio);@@ -518,7 +520,7 @@ static int kvm_gmem_error_folio(struct address_space *mapping, struct folio *folkvm_gmem_invalidate_end (mapping->host, start, end); -filemap_invalidate_unlock_shared (mapping); +up_read (&GMEM_I (mapping->host)->bindings_lock);return MF_DELAYED ; }@@ -597,6 +599,7 @@ static int __kvm_gmem_create(struct kvm *kvm, loff_t size, u64 flags)WARN_ON_ONCE (!mapping_unevictable (inode->i_mapping));GMEM_I (inode)->flags = flags; +init_rwsem (&GMEM_I (inode)->bindings_lock); file =alloc_file_pseudo (inode, kvm_gmem_mnt, name,O_RDWR , &kvm_gmem_fops);if (IS_ERR (file)) {@@ -691,7 +694,10 @@ int kvm_gmem_bind(struct kvm *kvm, struct kvm_memory_slot *slot,if (kvm_gmem_supports_mmap (inode)) slot->flags |=KVM_MEMSLOT_GMEM_ONLY ; +down_write (&GMEM_I (inode)->bindings_lock);xa_store_range (&f->bindings, start, end -1 , slot,GFP_KERNEL ); +up_write (&GMEM_I (inode)->bindings_lock); +filemap_invalidate_unlock (inode->i_mapping);/* @@ -746,7 +752,9 @@ void kvm_gmem_unbind(struct kvm_memory_slot *slot) }filemap_invalidate_lock (file->f_mapping); +down_write (&GMEM_I (file_inode (file))->bindings_lock);__kvm_gmem_unbind (slot, file->private_data); +up_write (&GMEM_I (file_inode (file))->bindings_lock);filemap_invalidate_unlock (file->f_mapping); }