FAILED: patch "[PATCH] KVM: guest_memfd: take the invalidate lock when unbinding a" failed to apply to 6.18-stable tree
Subsystems (3)
[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 0ee2c883b62deb74d8c43007e8049dc006d3a2fb
# <resolve conflicts, build, test, etc.>
git commit -s
git send-email --to '<[email protected]>' --in-reply-to '2026110442-recast-sequel-2c6a@gregkh' --subject-prefix 'PATCH 6.18.y' 'HEAD^..'
Possible dependencies:
thanks,
greg k-h
------------------ original commit in Linus's tree ------------------
From 0ee2c883b62deb74d8c43007e8049dc006d3a2fb Mon Sep 17 00:00:00 2001
From: Shivank Garg <[email protected]>
Date: Tue, 1 Sep 2026 07:20:38 +0000
Subject: [PATCH] KVM: guest_memfd: take the invalidate lock when unbinding a
dying file
kvm_gmem_unbind() takes mapping->invalidate_lock only if the get_file_active() succeeds. When the guest_memfd file is dying, its reference count is already zero so get_file_active fails and the binding is removed without holding the invalidate lock.
kvm_gmem_invalidate_{start,end}() checks f->bindings independently to decide whether to begin or end KVM MMU invalidation. So, the bindings must therefore remain stable between the two calls. Otherwise, unbind can remove a binding after start increments mmu_invalidate_in_progress, but before end finds the binding and decrements it.
Example, unbind race with memory failure:
CPU 0: memory failure CPU 1: memslot delete
---------------------------------- ---------------------------
(guest_memfd file is dying)
kvm_gmem_error_folio()
kvm_gmem_invalidate_start()
finds binding
mmu_invalidate_in_progress++
kvm_gmem_unbind()
get_file_active() fails removes binding
kvm_gmem_invalidate_end()
no binding found
counter stays elevated
mmu_invalidate_retry() then returns 1 forever, so guest page faults retry without ever installing a mapping and the guest hangs.
Remove the distinction between live and dying files in the unbind path. Always use slot->gmem.file and take the invalidate lock when it's non-NULL. Normal unbind callers hold slots_lock, which prevents release from clearing the pointer or freeing the file state until unbind completes. Final VM teardown can only see a NULL pointer because guest_memfd pins the KVM until release has cleared all bindings.
virt/kvm/guest_memfd.c +23 -31
@@ -668,11 +668,33 @@ int kvm_gmem_bind(struct kvm *kvm, struct kvm_memory_slot *slot,return r; } -static void __kvm_gmem_unbind (struct kvm_memory_slot *slot,struct gmem_file *f) +void kvm_gmem_unbind (struct kvm_memory_slot *slot) { +struct file *file = slot->gmem.file;unsigned long start = slot->gmem.pgoff;unsigned long end = start + slot->npages; +struct gmem_file *f; +/* +* Nothing to do if the underlying file was _already_ closed, as +* kvm_gmem_release() invalidates and nullifies all bindings. +*/ +if (!file) +return ; + +/* +* However, if the file is _being_ closed, then the bindings need to be +* removed as kvm_gmem_release() might not run until after the memslot +* is freed. Modifying the bindings is safe even if the file is dying +* as kvm_gmem_release() nullifies slot->gmem.file under slots_lock, +* and only puts its reference to KVM after destroying all bindings. +* I.e. reaching this point means kvm_gmem_release() hasn't destroyed +* the bindings or freed the gmem_file and can't do so until the caller +* drops slots_lock, so there's no need to verify the file is live. +*/ + f = file->private_data; + +filemap_invalidate_lock (file->f_mapping);xa_store_range (&f->bindings, start, end -1 ,NULL ,GFP_KERNEL );/*
@@ -680,36 +702,6 @@ static void __kvm_gmem_unbind(struct kvm_memory_slot *slot, struct gmem_file *f)* cannot see this memslot. */ WRITE_ONCE (slot->gmem.file,NULL ); -} - -void kvm_gmem_unbind (struct kvm_memory_slot *slot) -{ -/* -* Nothing to do if the underlying file was _already_ closed, as -* kvm_gmem_release() invalidates and nullifies all bindings. -*/ -if (!slot->gmem.file) -return ; - -CLASS (gmem_get_file, file)(slot); - -/* -* However, if the file is _being_ closed, then the bindings need to be -* removed as kvm_gmem_release() might not run until after the memslot -* is freed. Note, modifying the bindings is safe even though the file -* is dying as kvm_gmem_release() nullifies slot->gmem.file under -* slots_lock, and only puts its reference to KVM after destroying all -* bindings. I.e. reaching this point means kvm_gmem_release() hasn't -* yet destroyed the bindings or freed the gmem_file, and can't do so -* until the caller drops slots_lock. -*/ -if (!file) { -__kvm_gmem_unbind (slot, slot->gmem.file->private_data); -return ; - } - -filemap_invalidate_lock (file->f_mapping); -__kvm_gmem_unbind (slot, file->private_data);filemap_invalidate_unlock (file->f_mapping); }
Sasha Levin
maintainerPATCH 1/1 KVM: guest_memfd: take the invalidate lock when unbinding a dying file
From: Shivank Garg <[email protected]>
[ Upstream commit 0ee2c883b62deb74d8c43007e8049dc006d3a2fb ]
kvm_gmem_unbind() takes mapping->invalidate_lock only if the get_file_active() succeeds. When the guest_memfd file is dying, its reference count is already zero so get_file_active fails and the binding is removed without holding the invalidate lock.
kvm_gmem_invalidate_{start,end}() checks f->bindings independently to decide whether to begin or end KVM MMU invalidation. So, the bindings must therefore remain stable between the two calls. Otherwise, unbind can remove a binding after start increments mmu_invalidate_in_progress, but before end finds the binding and decrements it.
Example, unbind race with memory failure:
CPU 0: memory failure CPU 1: memslot delete
---------------------------------- ---------------------------
(guest_memfd file is dying)
kvm_gmem_error_folio()
kvm_gmem_invalidate_start()
finds binding
mmu_invalidate_in_progress++
kvm_gmem_unbind()
get_file_active() fails removes binding
kvm_gmem_invalidate_end()
no binding found
counter stays elevated
mmu_invalidate_retry() then returns 1 forever, so guest page faults retry without ever installing a mapping and the guest hangs.
Remove the distinction between live and dying files in the unbind path. Always use slot->gmem.file and take the invalidate lock when it's non-NULL. Normal unbind callers hold slots_lock, which prevents release from clearing the pointer or freeing the file state until unbind completes. Final VM teardown can only see a NULL pointer because guest_memfd pins the KVM until release has cleared all bindings.
--- virt/kvm/guest_memfd.c | 48 ++++++++++++++++-------------------------- 1 file changed, 18 insertions(+), 30 deletions(-)
virt/kvm/guest_memfd.c +18 -30
@@ -627,53 +627,41 @@ int kvm_gmem_bind(struct kvm *kvm, struct kvm_memory_slot *slot,return r; } -static void __kvm_gmem_unbind (struct kvm_memory_slot *slot,struct kvm_gmem *gmem) +void kvm_gmem_unbind (struct kvm_memory_slot *slot) { +struct file *file = slot->gmem.file;unsigned long start = slot->gmem.pgoff;unsigned long end = start + slot->npages; - -xa_store_range (&gmem->bindings, start, end -1 ,NULL ,GFP_KERNEL ); - -/* -* synchronize_srcu(&kvm->srcu) ensured that kvm_gmem_get_pfn() -* cannot see this memslot. -*/ -WRITE_ONCE (slot->gmem.file,NULL ); -} - -void kvm_gmem_unbind (struct kvm_memory_slot *slot) -{ -struct file *file; +struct kvm_gmem *gmem;/* * Nothing to do if the underlying file was _already_ closed, as * kvm_gmem_release() invalidates and nullifies all bindings. */ -if (!slot->gmem.file) +if (!file)return ; - file =kvm_gmem_get_file (slot); -/* * However, if the file is _being_ closed, then the bindings need to be * removed as kvm_gmem_release() might not run until after the memslot -* is freed. Note, modifying the bindings is safe even though the file -* is dying as kvm_gmem_release() nullifies slot->gmem.file under -* slots_lock, and only puts its reference to KVM after destroying all -* bindings. I.e. reaching this point means kvm_gmem_release() hasn't -* yet destroyed the bindings or freed the gmem_file, and can't do so -* until the caller drops slots_lock. +* is freed. Modifying the bindings is safe even if the file is dying +* as kvm_gmem_release() nullifies slot->gmem.file under slots_lock, +* and only puts its reference to KVM after destroying all bindings. +* I.e. reaching this point means kvm_gmem_release() hasn't destroyed +* the bindings or freed the gmem_file and can't do so until the caller +* drops slots_lock, so there's no need to verify the file is live. */ -if (!file) { -__kvm_gmem_unbind (slot, slot->gmem.file->private_data); -return ; - } + gmem = file->private_data;filemap_invalidate_lock (file->f_mapping); -__kvm_gmem_unbind (slot, file->private_data); -filemap_invalidate_unlock (file->f_mapping); +xa_store_range (&gmem->bindings, start, end -1 ,NULL ,GFP_KERNEL ); -fput (file); +/* +* synchronize_srcu(&kvm->srcu) ensured that kvm_gmem_get_pfn() +* cannot see this memslot. +*/ +WRITE_ONCE (slot->gmem.file,NULL ); +filemap_invalidate_unlock (file->f_mapping); }/* Returns a locked folio on success. */
Signature · 1 line
2.53.0