From: Yash Suthar <yashsuthar983@gmail.com>
To: pbonzini@redhat.com, kvm@vger.kernel.org, linux-kernel@vger.kernel.org
Cc: skhan@linuxfoundation.org, me@brighamcampbell.com,
Yash Suthar <yashsuthar983@gmail.com>
Subject: [PATCH] kvm: vfio: Convert kvm_vfio_file_add() to use CLASS(fd) and guard(mutex)
Date: Sat, 23 May 2026 10:32:42 +0530 [thread overview]
Message-ID: <20260523050242.41472-1-yashsuthar983@gmail.com> (raw)
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
next reply other threads:[~2026-05-23 5:02 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-05-23 5:02 Yash Suthar [this message]
2026-06-12 16:15 ` Alex Williamson
2026-06-12 16:17 ` Alex Williamson
2026-06-15 17:29 ` [PATCH v2] KVM: vfio: Convert fget() to CLASS(fd) in kvm_vfio_file_add() Yash Suthar
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20260523050242.41472-1-yashsuthar983@gmail.com \
--to=yashsuthar983@gmail.com \
--cc=kvm@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=me@brighamcampbell.com \
--cc=pbonzini@redhat.com \
--cc=skhan@linuxfoundation.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®