[PATCH] kvm: vfio: Convert kvm_vfio_file_add() to use CLASS(fd) and guard(mutex)

Yash Suthar posted 1 patch 1 day, 13 hours ago
virt/kvm/vfio.c | 37 ++++++++++++++-----------------------
1 file changed, 14 insertions(+), 23 deletions(-)
[PATCH] kvm: vfio: Convert kvm_vfio_file_add() to use CLASS(fd) and guard(mutex)
Posted by Yash Suthar 1 day, 13 hours ago
Convert manual fget/fput call to CLASS(fd, f) and
mutex_lock/unlock to guard(mutex).

This remove the goto based paths both out_unlock and
out_fput.

Signed-off-by: Yash Suthar <yashsuthar983@gmail.com>
---
 virt/kvm/vfio.c | 37 ++++++++++++++-----------------------
 1 file changed, 14 insertions(+), 23 deletions(-)

diff --git a/virt/kvm/vfio.c b/virt/kvm/vfio.c
index 9f9acb66cc1e..bb3694d9c0c4 100644
--- a/virt/kvm/vfio.c
+++ b/virt/kvm/vfio.c
@@ -144,45 +144,36 @@ static int kvm_vfio_file_add(struct kvm_device *dev, unsigned int fd)
 {
 	struct kvm_vfio *kv = dev->private;
 	struct kvm_vfio_file *kvf;
-	struct file *filp;
-	int ret = 0;
+	struct file *file;
+	CLASS(fd, f)(fd);
 
-	filp = fget(fd);
-	if (!filp)
+	if (fd_empty(f))
 		return -EBADF;
 
+	file = fd_file(f);
+
 	/* Ensure the FD is a vfio FD. */
-	if (!kvm_vfio_file_is_valid(filp)) {
-		ret = -EINVAL;
-		goto out_fput;
-	}
+	if (!kvm_vfio_file_is_valid(file))
+		return -EINVAL;
 
-	mutex_lock(&kv->lock);
+	guard(mutex)(&kv->lock);
 
 	list_for_each_entry(kvf, &kv->file_list, node) {
-		if (kvf->file == filp) {
-			ret = -EEXIST;
-			goto out_unlock;
-		}
+		if (kvf->file == file)
+			return -EEXIST;
 	}
 
 	kvf = kzalloc_obj(*kvf, GFP_KERNEL_ACCOUNT);
-	if (!kvf) {
-		ret = -ENOMEM;
-		goto out_unlock;
-	}
+	if (!kvf)
+		return -ENOMEM;
 
-	kvf->file = get_file(filp);
+	kvf->file = get_file(file);
 	list_add_tail(&kvf->node, &kv->file_list);
 
 	kvm_vfio_file_set_kvm(kvf->file, dev->kvm);
 	kvm_vfio_update_coherency(dev);
 
-out_unlock:
-	mutex_unlock(&kv->lock);
-out_fput:
-	fput(filp);
-	return ret;
+	return 0;
 }
 
 static int kvm_vfio_file_del(struct kvm_device *dev, unsigned int fd)
-- 
2.43.0