aboutsummaryrefslogtreecommitdiffstats
path: root/virt
diff options
authorCarlos López <clopez@suse.de>2026-03-13 13:20:41 +0100
committerSean Christopherson <seanjc@google.com>2026-05-13 11:12:39 -0700
commit3b20a19c592d31d90594565de89f974135b22819 (patch)
tree03613257c5bd780e5503ff050604e41b768b434f /virt
parent40f0f4bb6041b1f520445a4c5f0c8777f7c98f8e (diff)
downloadath-3b20a19c592d31d90594565de89f974135b22819.tar.gz
KVM: VFIO: deduplicate file release logic
There are two callsites which destroy files in kv->file_list: the function servicing KVM_DEV_VFIO_FILE_DEL, and the relase of the whole KVM VFIO device. The process involves several steps, so move all those into a single function, removing duplicate code. Signed-off-by: Carlos López <clopez@suse.de> Reviewed-by: Alex Williamson <alex@shazbot.org> Link: https://patch.msgid.link/20260313122040.1413091-6-clopez@suse.de Signed-off-by: Sean Christopherson <seanjc@google.com>
Diffstat (limited to 'virt')
-rw-r--r--virt/kvm/vfio.c39
1 files changed, 18 insertions, 21 deletions
diff --git a/virt/kvm/vfio.c b/virt/kvm/vfio.c
index e0d621567dbf6..2fee1e82f8f68 100644
--- a/virt/kvm/vfio.c
+++ b/virt/kvm/vfio.c
@@ -174,6 +174,17 @@ static int kvm_vfio_file_add(struct kvm_device *dev, unsigned int fd)
return 0;
}
+static void kvm_vfio_file_free(struct kvm_device *dev, struct kvm_vfio_file *kvf)
+{
+#ifdef CONFIG_SPAPR_TCE_IOMMU
+ kvm_spapr_tce_release_vfio_group(dev->kvm, kvf);
+#endif
+ kvm_vfio_file_set_kvm(kvf->file, NULL);
+ fput(kvf->file);
+ list_del(&kvf->node);
+ kfree(kvf);
+}
+
static int kvm_vfio_file_del(struct kvm_device *dev, unsigned int fd)
{
struct kvm_vfio *kv = dev->private;
@@ -189,18 +200,11 @@ static int kvm_vfio_file_del(struct kvm_device *dev, unsigned int fd)
mutex_lock(&kv->lock);
list_for_each_entry(kvf, &kv->file_list, node) {
- if (kvf->file != fd_file(f))
- continue;
-
- list_del(&kvf->node);
-#ifdef CONFIG_SPAPR_TCE_IOMMU
- kvm_spapr_tce_release_vfio_group(dev->kvm, kvf);
-#endif
- kvm_vfio_file_set_kvm(kvf->file, NULL);
- fput(kvf->file);
- kfree(kvf);
- ret = 0;
- break;
+ if (kvf->file == fd_file(f)) {
+ kvm_vfio_file_free(dev, kvf);
+ ret = 0;
+ break;
+ }
}
kvm_vfio_update_coherency(dev);
@@ -307,15 +311,8 @@ static void kvm_vfio_release(struct kvm_device *dev)
struct kvm_vfio *kv = dev->private;
struct kvm_vfio_file *kvf, *tmp;
- list_for_each_entry_safe(kvf, tmp, &kv->file_list, node) {
-#ifdef CONFIG_SPAPR_TCE_IOMMU
- kvm_spapr_tce_release_vfio_group(dev->kvm, kvf);
-#endif
- kvm_vfio_file_set_kvm(kvf->file, NULL);
- fput(kvf->file);
- list_del(&kvf->node);
- kfree(kvf);
- }
+ list_for_each_entry_safe(kvf, tmp, &kv->file_list, node)
+ kvm_vfio_file_free(dev, kvf);
kvm_vfio_update_coherency(dev);