linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] vfio: Use filp instead of fd
@ 2022-09-26  6:54 Deming Wang
  2022-09-26 13:23 ` Al Viro
  2022-09-26 17:07 ` Jason Gunthorpe
  0 siblings, 2 replies; 7+ messages in thread
From: Deming Wang @ 2022-09-26  6:54 UTC (permalink / raw)
  To: pbonzini; +Cc: kvm, linux-kernel, Deming Wang

The function of kvm_vfio_group_set_spapr_tce and kvm_vfio_group_del
use fd indirectly.But,it only be used for fd.file. So,we can directly
use the struct of file instead.

Signed-off-by: Deming Wang <wangdeming@inspur.com>
---
 virt/kvm/vfio.c | 24 ++++++++++++------------
 1 file changed, 12 insertions(+), 12 deletions(-)

diff --git a/virt/kvm/vfio.c b/virt/kvm/vfio.c
index ce1b01d02c51..3be84d82f905 100644
--- a/virt/kvm/vfio.c
+++ b/virt/kvm/vfio.c
@@ -178,11 +178,11 @@ static int kvm_vfio_group_del(struct kvm_device *dev, unsigned int fd)
 {
 	struct kvm_vfio *kv = dev->private;
 	struct kvm_vfio_group *kvg;
-	struct fd f;
+	struct file *filp;
 	int ret;
 
-	f = fdget(fd);
-	if (!f.file)
+	filp = fget(fd);
+	if (!filp)
 		return -EBADF;
 
 	ret = -ENOENT;
@@ -190,7 +190,7 @@ static int kvm_vfio_group_del(struct kvm_device *dev, unsigned int fd)
 	mutex_lock(&kv->lock);
 
 	list_for_each_entry(kvg, &kv->group_list, node) {
-		if (kvg->file != f.file)
+		if (kvg->file != filp)
 			continue;
 
 		list_del(&kvg->node);
@@ -207,7 +207,7 @@ static int kvm_vfio_group_del(struct kvm_device *dev, unsigned int fd)
 
 	mutex_unlock(&kv->lock);
 
-	fdput(f);
+	fput(filp);
 
 	kvm_vfio_update_coherency(dev);
 
@@ -221,14 +221,14 @@ static int kvm_vfio_group_set_spapr_tce(struct kvm_device *dev,
 	struct kvm_vfio_spapr_tce param;
 	struct kvm_vfio *kv = dev->private;
 	struct kvm_vfio_group *kvg;
-	struct fd f;
+	struct file *filp;
 	int ret;
 
 	if (copy_from_user(&param, arg, sizeof(struct kvm_vfio_spapr_tce)))
 		return -EFAULT;
 
-	f = fdget(param.groupfd);
-	if (!f.file)
+	filp = fget(param.groupfd);
+	if (!filp)
 		return -EBADF;
 
 	ret = -ENOENT;
@@ -238,13 +238,13 @@ static int kvm_vfio_group_set_spapr_tce(struct kvm_device *dev,
 	list_for_each_entry(kvg, &kv->group_list, node) {
 		struct iommu_group *grp;
 
-		if (kvg->file != f.file)
+		if (kvg->file != filp)
 			continue;
 
 		grp = kvm_vfio_file_iommu_group(kvg->file);
 		if (WARN_ON_ONCE(!grp)) {
 			ret = -EIO;
-			goto err_fdput;
+			goto err_fput;
 		}
 
 		ret = kvm_spapr_tce_attach_iommu_group(dev->kvm, param.tablefd,
@@ -252,9 +252,9 @@ static int kvm_vfio_group_set_spapr_tce(struct kvm_device *dev,
 		break;
 	}
 
-err_fdput:
+err_fput:
 	mutex_unlock(&kv->lock);
-	fdput(f);
+	fput(filp);
 	return ret;
 }
 #endif
-- 
2.27.0


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

* Re: [PATCH] vfio: Use filp instead of fd
  2022-09-26  6:54 [PATCH] vfio: Use filp instead of fd Deming Wang
@ 2022-09-26 13:23 ` Al Viro
  2022-09-26 17:07 ` Jason Gunthorpe
  1 sibling, 0 replies; 7+ messages in thread
From: Al Viro @ 2022-09-26 13:23 UTC (permalink / raw)
  To: Deming Wang; +Cc: pbonzini, kvm, linux-kernel

On Mon, Sep 26, 2022 at 02:54:07AM -0400, Deming Wang wrote:
> The function of kvm_vfio_group_set_spapr_tce and kvm_vfio_group_del
> use fd indirectly.But,it only be used for fd.file. So,we can directly
> use the struct of file instead.
> 
> Signed-off-by: Deming Wang <wangdeming@inspur.com>

NAK.  fget() is for the cases when we must keep the reference across the
syscall boundary/pass to another thread/etc.

If fdget() is applicable, it's a better alternative.  And I would suggest
you to look at the generated code - it pretty much turns into
	struct file *file;
	int need_fput;

	r = __fdget(fd);
	need_fput = r & 3;
	r &= ~3;
	file = (struct file *)r;

	....

	if (unlikely(need_fput))
		fput(file);

Note that we are *not* actually passing a structure out of a function, etc. -
fdget() is inlined and out-of-line part returns unsigned long.  Lower two bits
carry flags, the rest - file pointer.  Rearrangement into struct fd is done
in the caller and compiler manages to dissolve that struct into a couple of
local variables.

Incidentally, pretty much the only thing in struct fd besides struct file pointer
is that "have we failed to skip bumping the reference count?" flag.  Between
fdget() and fdput() only the file pointer is used - in all users.  fdput()
uses the flag to decide whether it needs to bother with refcount at all.

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

* Re: [PATCH] vfio: Use filp instead of fd
  2022-09-26  6:54 [PATCH] vfio: Use filp instead of fd Deming Wang
  2022-09-26 13:23 ` Al Viro
@ 2022-09-26 17:07 ` Jason Gunthorpe
  2022-09-26 19:38   ` Al Viro
  1 sibling, 1 reply; 7+ messages in thread
From: Jason Gunthorpe @ 2022-09-26 17:07 UTC (permalink / raw)
  To: Deming Wang; +Cc: pbonzini, kvm, linux-kernel

On Mon, Sep 26, 2022 at 02:54:07AM -0400, Deming Wang wrote:
> The function of kvm_vfio_group_set_spapr_tce and kvm_vfio_group_del
> use fd indirectly.But,it only be used for fd.file. So,we can directly
> use the struct of file instead.
> 
> Signed-off-by: Deming Wang <wangdeming@inspur.com>
> ---
>  virt/kvm/vfio.c | 24 ++++++++++++------------
>  1 file changed, 12 insertions(+), 12 deletions(-)

I thought about changing this too when I was looking at
this. fdget/fdput includes a tiny micro-optimization that is legal
here, however I doubt anyone cares about performance on this path.

Reviewed-by: Jason Gunthorpe <jgg@nvidia.com>

Jason

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

* Re: [PATCH] vfio: Use filp instead of fd
  2022-09-26 17:07 ` Jason Gunthorpe
@ 2022-09-26 19:38   ` Al Viro
  0 siblings, 0 replies; 7+ messages in thread
From: Al Viro @ 2022-09-26 19:38 UTC (permalink / raw)
  To: Jason Gunthorpe; +Cc: Deming Wang, pbonzini, kvm, linux-kernel

On Mon, Sep 26, 2022 at 02:07:43PM -0300, Jason Gunthorpe wrote:
> On Mon, Sep 26, 2022 at 02:54:07AM -0400, Deming Wang wrote:
> > The function of kvm_vfio_group_set_spapr_tce and kvm_vfio_group_del
> > use fd indirectly.But,it only be used for fd.file. So,we can directly
> > use the struct of file instead.
> > 
> > Signed-off-by: Deming Wang <wangdeming@inspur.com>
> > ---
> >  virt/kvm/vfio.c | 24 ++++++++++++------------
> >  1 file changed, 12 insertions(+), 12 deletions(-)
> 
> I thought about changing this too when I was looking at
> this. fdget/fdput includes a tiny micro-optimization that is legal
> here, however I doubt anyone cares about performance on this path.

Microoptimization or not, I'd rather keep fget() limited to cases where
we really need it.  There are non-trivial cases and having them easy
to find is a good thing.

Again, the preferred way to do descriptor lookups is fdget() family,
not fget() one.

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

* Re: [PATCH] vfio: Use filp instead of fd
  2022-09-27  1:21 tomorrow Wang (王德明)
@ 2022-09-27  1:37 ` Al Viro
  0 siblings, 0 replies; 7+ messages in thread
From: Al Viro @ 2022-09-27  1:37 UTC (permalink / raw)
  To: tomorrow Wang (王德明); +Cc: jgg, pbonzini, kvm, linux-kernel

On Tue, Sep 27, 2022 at 01:21:50AM +0000, tomorrow Wang (王德明) wrote:
> Hi
> 
> Why kvm_vfio_group_add use file.
>   
>   struct file *filp;
>   .....
>   filp = fget(fd);
> 
>   .....
>   kvg->file = filp;

This is why.  Note that you've slightly misquoted it - it's actually
either "stick into kvg->file" or "fput() it".

>   ......
>   fput(filp);

The reference we are getting here is non-transient one; it will be retained
in shared data structures after we return from function - hell, after we
return to userland.

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

* Re: [PATCH] vfio: Use filp instead of fd
@ 2022-09-27  1:25 tomorrow Wang (王德明)
  0 siblings, 0 replies; 7+ messages in thread
From: tomorrow Wang (王德明) @ 2022-09-27  1:25 UTC (permalink / raw)
  To: viro, jgg; +Cc: pbonzini, kvm, linux-kernel

[-- Attachment #1: Type: text/plain, Size: 1389 bytes --]

Hi,

Why kvm_vfio_group_add use file? 

  struct file *filp;
  ......
  filp = fget(fd);
  ......
  fput(filp);



>--------
> send: Al Viro <viro@ftp.linux.org.uk> 代表 Al Viro
> 发送时间: 2022年9月27日 3:38
>to: Jason Gunthorpe <jgg@nvidia.com>
> pbonzini@redhat.com; kvm@vger.kernel.org; linux-kernel@vger.kernel.org
> sub: Re: [PATCH] vfio: Use filp instead of fd
> 
> On Mon, Sep 26, 2022 at 02:07:43PM -0300, Jason Gunthorpe wrote:
> > On Mon, Sep 26, 2022 at 02:54:07AM -0400, Deming Wang wrote:
> > > The function of kvm_vfio_group_set_spapr_tce and kvm_vfio_group_del
> > > use fd indirectly.But,it only be used for fd.file. So,we can
> > > directly use the struct of file instead.
> > >
> > > Signed-off-by: Deming Wang <wangdeming@inspur.com>
> > > ---
> > >  virt/kvm/vfio.c | 24 ++++++++++++------------
> > >  1 file changed, 12 insertions(+), 12 deletions(-)
> >
> > I thought about changing this too when I was looking at this.
> > fdget/fdput includes a tiny micro-optimization that is legal here,
> > however I doubt anyone cares about performance on this path.
> 
> Microoptimization or not, I'd rather keep fget() limited to cases where we
> really need it.  There are non-trivial cases and having them easy to find
is a
> good thing.
> 
> Again, the preferred way to do descriptor lookups is fdget() family, not
fget()
> one.

[-- Attachment #2: smime.p7s --]
[-- Type: application/pkcs7-signature, Size: 3780 bytes --]

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

* Re: [PATCH] vfio: Use filp instead of fd
@ 2022-09-27  1:21 tomorrow Wang (王德明)
  2022-09-27  1:37 ` Al Viro
  0 siblings, 1 reply; 7+ messages in thread
From: tomorrow Wang (王德明) @ 2022-09-27  1:21 UTC (permalink / raw)
  To: viro, jgg; +Cc: pbonzini, kvm, linux-kernel

[-- Attachment #1: Type: text/plain, Size: 1398 bytes --]

Hi

Why kvm_vfio_group_add use file.
  
  struct file *filp;
  .....
  filp = fget(fd);

  .....
  kvg->file = filp;
  ......
  fput(filp);

> from: Al Viro <viro@ftp.linux.org.uk> 代表 Al Viro
> time: 2022-9-22 3:38
> to: Jason Gunthorpe <jgg@nvidia.com>
> pbonzini@redhat.com; kvm@vger.kernel.org; linux-kernel@vger.kernel.org
> sub: Re: [PATCH] vfio: Use filp instead of fd
> 
> On Mon, Sep 26, 2022 at 02:07:43PM -0300, Jason Gunthorpe wrote:
> > On Mon, Sep 26, 2022 at 02:54:07AM -0400, Deming Wang wrote:
> > > The function of kvm_vfio_group_set_spapr_tce and kvm_vfio_group_del
> > > use fd indirectly.But,it only be used for fd.file. So,we can
> > > directly use the struct of file instead.
> > >
> > > Signed-off-by: Deming Wang <wangdeming@inspur.com>
> > > ---
> > >  virt/kvm/vfio.c | 24 ++++++++++++------------
> > >  1 file changed, 12 insertions(+), 12 deletions(-)
> >
> > I thought about changing this too when I was looking at this.
> > fdget/fdput includes a tiny micro-optimization that is legal here,
> > however I doubt anyone cares about performance on this path.
> 
> Microoptimization or not, I'd rather keep fget() limited to cases where we
> really need it.  There are non-trivial cases and having them easy to find
is a
> good thing.
> 
> Again, the preferred way to do descriptor lookups is fdget() family, not
fget()
> one.

[-- Attachment #2: smime.p7s --]
[-- Type: application/pkcs7-signature, Size: 3780 bytes --]

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

end of thread, other threads:[~2022-09-27  1:37 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-09-26  6:54 [PATCH] vfio: Use filp instead of fd Deming Wang
2022-09-26 13:23 ` Al Viro
2022-09-26 17:07 ` Jason Gunthorpe
2022-09-26 19:38   ` Al Viro
2022-09-27  1:21 tomorrow Wang (王德明)
2022-09-27  1:37 ` Al Viro
2022-09-27  1:25 tomorrow Wang (王德明)

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).