kvm: vfio: Convert kvm_vfio_file_add() to use CLASS(fd) and guard(mutex)
PATCH 1/1 kvm: vfio: Convert kvm_vfio_file_add() to use CLASS(fd) and guard(mutex)
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.
--- virt/kvm/vfio.c | 37 ++++++++++++++----------------------- 1 file changed, 14 insertions(+), 23 deletions(-)
virt/kvm/vfio.c +14 -23
@@ -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)
Signature · 1 line
2.43.0
Quoted 10 lines · On Sat, 23 May 2026 10:32:42 +0530 Yash Suthar <[email protected]> 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_unlockandout_fput.Signed-off-by Yash Suthar <[email protected]>--- virt/kvm/vfio.c | 37 ++++++++++++++----------------------- 1 file changed, 14 insertions(+), 23 deletions(-)
Sean already took Carlos' series[1] that mostly
Quoted 64 lines
virt/kvm/vfio.c +14
-23@@ -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)
Quoted 9 lines · On Fri, 12 Jun 2026 10:15:30 -0600 Alex Williamson <[email protected]> wrote:
Quoted 6 lines · On Sat, 23 May 2026 10:32:42 +0530 Yash Suthar <[email protected]> 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_unlockandout_fput.
Quoted 6 lines
Quoted 4 lines
Signed-off-by Yash Suthar <[email protected]>--- virt/kvm/vfio.c | 37 ++++++++++++++----------------------- 1 file changed, 14 insertions(+), 23 deletions(-)Sean already took Carlos' series[1] that mostly
Sent too soon, [1] mostly handles this, only maybe the CLASS change remains, if that's still worthwhile on its own. Thanks,
Alex
[1]https://lore.kernel.org/all/[email protected]/
Quoted 65 lines
Quoted 64 lines
virt/kvm/vfio.c +14
-23@@ -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)
PATCH v2 1/1 KVM: vfio: Convert fget() to CLASS(fd) in kvm_vfio_file_add()
In reply to Yash SutharConvert fget() with __free(fput) cleanup to CLASS(fd, f), using fd_empty() and fd_file().
--- virt/kvm/vfio.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-)
virt/kvm/vfio.c +5 -3
@@ -144,12 +144,14 @@ 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 __free(fput) = NULL; +CLASS (fd, f)(fd); +struct file *filp =NULL ; - filp =fget (fd); -if (!filp) +if (fd_empty (f))return -EBADF ; + filp =fd_file (f); +/* Ensure the FD is a vfio FD. */ if (!kvm_vfio_file_is_valid (filp))return -EINVAL ;
Signature · 1 line
2.43.0