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

From: Alex Williamson

Date: Fri Jun 12 2026 - 12:19:20 EST


On Sat, 23 May 2026 10:32:42 +0530
Yash Suthar <yashsuthar983@xxxxxxxxx> wrote:

> 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@xxxxxxxxx>
> ---
> virt/kvm/vfio.c | 37 ++++++++++++++-----------------------
> 1 file changed, 14 insertions(+), 23 deletions(-)

Sean already took Carlos' series[1] that mostly


> 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)