mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH] kvm: vfio: Convert kvm_vfio_file_add() to use CLASS(fd) and guard(mutex)
@ 2026-05-23  5:02 Yash Suthar
  2026-06-12 16:15 ` Alex Williamson
  2026-06-15 17:29 ` [PATCH v2] KVM: vfio: Convert fget() to CLASS(fd) in kvm_vfio_file_add() Yash Suthar
  0 siblings, 2 replies; 4+ messages in thread
From: Yash Suthar @ 2026-05-23  5:02 UTC (permalink / raw)
  To: pbonzini, kvm, linux-kernel; +Cc: skhan, me, Yash Suthar

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


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] kvm: vfio: Convert kvm_vfio_file_add() to use CLASS(fd) and guard(mutex)
  2026-05-23  5:02 [PATCH] kvm: vfio: Convert kvm_vfio_file_add() to use CLASS(fd) and guard(mutex) Yash Suthar
@ 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
  1 sibling, 1 reply; 4+ messages in thread
From: Alex Williamson @ 2026-06-12 16:15 UTC (permalink / raw)
  To: Yash Suthar; +Cc: pbonzini, kvm, linux-kernel, skhan, me, alex

On Sat, 23 May 2026 10:32:42 +0530
Yash Suthar <yashsuthar983@gmail.com> 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@gmail.com>
> ---
>  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)


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] kvm: vfio: Convert kvm_vfio_file_add() to use CLASS(fd) and guard(mutex)
  2026-06-12 16:15 ` Alex Williamson
@ 2026-06-12 16:17   ` Alex Williamson
  0 siblings, 0 replies; 4+ messages in thread
From: Alex Williamson @ 2026-06-12 16:17 UTC (permalink / raw)
  To: Yash Suthar; +Cc: pbonzini, kvm, linux-kernel, skhan, me, alex

On Fri, 12 Jun 2026 10:15:30 -0600
Alex Williamson <alex@shazbot.org> wrote:

> On Sat, 23 May 2026 10:32:42 +0530
> Yash Suthar <yashsuthar983@gmail.com> 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@gmail.com>
> > ---
> >  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/20260313122040.1413091-3-clopez@suse.de/
 
> > 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)  
> 


^ permalink raw reply	[flat|nested] 4+ messages in thread

* [PATCH v2] KVM: vfio: Convert fget() to CLASS(fd) in kvm_vfio_file_add()
  2026-05-23  5:02 [PATCH] kvm: vfio: Convert kvm_vfio_file_add() to use CLASS(fd) and guard(mutex) Yash Suthar
  2026-06-12 16:15 ` Alex Williamson
@ 2026-06-15 17:29 ` Yash Suthar
  1 sibling, 0 replies; 4+ messages in thread
From: Yash Suthar @ 2026-06-15 17:29 UTC (permalink / raw)
  To: pbonzini, kvm, linux-kernel; +Cc: skhan, me, yashsuthar983

Convert fget() with __free(fput) cleanup to CLASS(fd, f),
using fd_empty() and fd_file().

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

diff --git a/virt/kvm/vfio.c b/virt/kvm/vfio.c
index 6cdc4e9a333a..bbd7a5dfb76e 100644
--- a/virt/kvm/vfio.c
+++ b/virt/kvm/vfio.c
@@ -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;
-- 
2.43.0


^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2026-06-15 12:00 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-05-23  5:02 [PATCH] kvm: vfio: Convert kvm_vfio_file_add() to use CLASS(fd) and guard(mutex) Yash Suthar
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

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®