]> git.ipfire.org Git - thirdparty/linux.git/commitdiff
KVM: VFIO: update coherency only if file was deleted
authorCarlos López <clopez@suse.de>
Fri, 13 Mar 2026 12:20:42 +0000 (13:20 +0100)
committerSean Christopherson <seanjc@google.com>
Wed, 13 May 2026 18:12:40 +0000 (11:12 -0700)
When servicing a KVM_DEV_VFIO_FILE_DEL request, if a file is removed
from kv->file_list, kv->noncoherent needs to be updated, in case we
can revert to using coherent DMA. However, if we found no candidate
to remove, there is no need to re-scan the list, so do it only if a
matching file was found.

To simplify the control flow, use a mutex guard so that we can return
early from within the search loop if the maching file is found.

Signed-off-by: Carlos López <clopez@suse.de>
Reviewed-by: Alex Williamson <alex@shazbot.org>
Link: https://patch.msgid.link/20260313122040.1413091-7-clopez@suse.de
Signed-off-by: Sean Christopherson <seanjc@google.com>
virt/kvm/vfio.c

index 2fee1e82f8f687f2fd5bfd0d2a50c7e3271e45bd..6cdc4e9a333a3fc0f59a14ef06be4f312ec607a9 100644 (file)
@@ -190,27 +190,21 @@ static int kvm_vfio_file_del(struct kvm_device *dev, unsigned int fd)
        struct kvm_vfio *kv = dev->private;
        struct kvm_vfio_file *kvf;
        CLASS(fd, f)(fd);
-       int ret;
 
        if (fd_empty(f))
                return -EBADF;
 
-       ret = -ENOENT;
-
-       mutex_lock(&kv->lock);
+       guard(mutex)(&kv->lock);
 
        list_for_each_entry(kvf, &kv->file_list, node) {
                if (kvf->file == fd_file(f)) {
                        kvm_vfio_file_free(dev, kvf);
-                       ret = 0;
-                       break;
+                       kvm_vfio_update_coherency(dev);
+                       return 0;
                }
        }
 
-       kvm_vfio_update_coherency(dev);
-
-       mutex_unlock(&kv->lock);
-       return ret;
+       return -ENOENT;
 }
 
 #ifdef CONFIG_SPAPR_TCE_IOMMU